Lines Matching +full:deadlock +full:- +full:detector
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
46 ** autoconf-based build
59 ** An array of names of all compile-time options. This array should
60 ** be sorted A-Z.
63 ** only a handful of compile-time options, so most times this array is usually
99 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
103 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
105 "COMPILER=gcc-" __VERSION__,
803 ** NO_TEST - The branches on this line are not
808 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
812 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
816 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
821 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
887 ** 2015-03-02
925 ** large file support, or if the OS is windows, these should be no-ops.
931 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
940 ** on 2008-11-28.) These days, all Linux kernels support large files, so
957 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
964 ** so the GCC_VERSION macro will be set to a correct non-zero value even
996 ** some MinGW-specific macros). When compiling for MinGW, either the
1026 ** 2001-09-15
1037 ** presents to client programs. If a C-function, structure, datatype,
1048 ** The official C-language API documentation for SQLite is derived
1095 ** These no-op macros are used in front of interfaces to mark those
1097 ** should not use deprecated interfaces - they are supported for backwards
1121 ** CAPI3REF: Compile-Time Library Version Numbers
1137 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
1139 ** a string which identifies a particular check-in of SQLite
1141 ** string contains the date and time of the check-in (UTC) and a SHA1
1142 ** or SHA3-256 hash of the entire source tree. If the source code has
1152 #define SQLITE_SOURCE_ID "2019-09-03 18:36:11 68b898381ac2942965a3dbd416a45ddf813d6df7ea160f50…
1155 ** CAPI3REF: Run-Time Library Version Numbers
1192 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1201 ** returning the N-th compile time option string. ^If N is out of range,
1223 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
1241 ** This interface only reports on the compile-time mutex setting
1247 ** sqlite3_threadsafe() function shows only the compile-time setting of
1248 ** thread safety, not any run-time changes to that setting made by
1273 ** CAPI3REF: 64-Bit Integer Types
1276 ** Because there is no cross-platform way to specify 64-bit integer types
1277 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
1284 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1307 ** substitute integer for floating-point.
1353 ** argument is a harmless no-op.
1366 ** CAPI3REF: One-Step Query Execution Interface
1374 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1375 ** semicolon-separate SQL statements passed into its 2nd argument,
1397 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1447 /* beginning-of-error-codes */
1478 /* end-of-error-codes */
1486 ** these result codes are too coarse-grained. They do not provide as
1616 ** read-only media and cannot be changed even by processes with
1674 ** (Third-party VFS implementations might also make the distinction
1709 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1742 ** locking strategy (for example to use dot-file locks), to inquire
1789 ** fails to zero-fill short reads might seem to work. However,
1790 ** failure to zero-fill short reads will eventually lead to
1836 ** compile-time option is used.
1850 ** point to an integer (type int) containing the new chunk-size to use
1852 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1863 ** the [rollback journal] or the [write-ahead log]) for a particular database
1876 ** this file-control is NULL. However, if the database file is being synced
1877 ** as part of a multi-database commit, the argument points to a nul-terminated
1878 ** string containing the transactions master-journal file name. VFSes that
1895 ** anti-virus programs. By default, the windows VFS will retry file read,
1920 ** WAL mode. If the integer is -1, then it is overwritten with the current
1925 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1929 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1930 ** mode. If the integer is -1, then it is overwritten with the current
1931 ** zero-damage mode setting.
1942 ** final bottom-level VFS are written into memory obtained from
1946 ** all file-control actions, there is no guarantee that this will actually
1948 ** pointer in case this file-control is not implemented. This file-control
1952 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1956 ** to a pointer to the top-level VFS.)^
1958 ** upper-most shim only.
1975 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1977 ** of the result string if the string is non-NULL.
1983 ** it is able to override built-in [PRAGMA] statements.
1987 ** file-control may be invoked by SQLite on the database file handle
1989 ** to the connections busy-handler callback. The argument is of type (void **)
1990 ** - an array of two (void *) values. The first (void *) actually points
1992 ** busy-handler, this function should be invoked with the second (void *) in
1993 ** the array as the only argument. If it returns non-zero, then the operation
1998 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2008 ** maximum number of bytes that will be used for memory-mapped I/O.
2014 ** file-control is used internally to implement [PRAGMA mmap_size].
2020 ** The argument is a zero-terminated string. Higher layers in the
2022 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2047 ** Applications should <em>not</em> use this file-control.
2182 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
2200 ** consist of a single "-" character followed by no more than
2201 ** 11 alphanumeric and/or "-" characters.
2216 ** If xOpen() opens a file read-only then it sets *pOutFlags to
2236 ** the open of a journal file a no-op. Writes to this journal would
2237 ** also be no-ops, and any attempt to read the journal would return
2239 ** file will be doing page-aligned sector reads and writes in a random
2291 ** of good-quality randomness into zOut. The return value is
2299 ** a 24-hour day).
2325 void *pAppData; /* Pointer to application-specific data */
2435 ** are harmless no-ops.)^
2440 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2463 ** compile-time option, then the automatic calls to sqlite3_initialize()
2472 ** The sqlite3_os_init() routine does operating-system specific
2488 ** (using the [SQLITE_OS_OTHER=1] compile-time
2490 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2519 ** implementation of an application-defined [sqlite3_os_init()].
2529 ** then this routine returns a non-zero [error code].
2543 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2556 ** and low-level memory allocation routines.
2568 ** Note that SQLite comes with several [built-in memory allocators]
2573 ** memory allocator that simulates memory out-of-memory conditions in
2638 ** non-zero [error code] if a discontinued or unsupported configuration option
2644 ** [threading mode] to Single-thread. In other words, it disables
2647 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2649 ** value of Single-thread and so [sqlite3_config()] will return
2655 ** [threading mode] to Multi-thread. In other words, it disables
2659 ** are enabled so that SQLite will be safe to use in a multi-threaded
2662 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2663 ** it is not possible to set the Multi-thread [threading mode] and
2678 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2687 ** alternative low-level memory allocation routines to be used in place of
2715 ** disabled, the following SQLite interfaces become non-operational:
2735 ** This configuration option is a no-op if an application-define page
2738 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2746 ** argument must be either a NULL pointer or a pointer to an 8-byte
2753 ** ^If pMem is NULL and N is non-zero, then each database connection
2756 ** of -1024*N bytes if N is negative, . ^If additional
2769 ** An 8-byte aligned pointer to the memory,
2776 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2784 ** The argument specifies alternative low-level mutex routines to be used
2788 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2801 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2834 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2836 ** passed through as the first parameter to the application-defined logger
2844 ** In a multi-threaded application, the application-defined logger
2849 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2866 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2867 ** if that compile-time option is omitted.
2877 ** They are retained for backwards compatibility but are now no-ops.
2883 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2899 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2906 ** compile-time maximum mmap size set by the
2907 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2909 ** changed to its compile-time default.
2914 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2915 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2931 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2940 ** becomes the [statement journal] spill-to-disk threshold.
2943 ** Or if the threshold is -1, statement journals are always held
2949 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2965 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2966 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2990 ** non-zero [error code] if a discontinued or unsupported configuration option
3005 ** must be aligned to an 8-byte boundary. ^If the second argument to
3037 ** <dd> ^This option is used to enable or disable the two-argument
3039 ** [FTS3] full-text search engine extension.
3053 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3055 ** When the first argument to this interface is 1, then only the C-API is
3057 ** this interface is 0, then both the C-API and the SQL function are disabled.
3058 ** If the first argument is -1, then no changes are made to state of either the
3059 ** C-API or the SQL function.
3081 ** is an integer - non-zero to disable checkpoints-on-close, or zero (the
3083 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
3084 ** have been disabled - 0 if they are not disabled, 1 if they are.
3100 ** behavior. The first parameter passed to this operation is an integer -
3101 ** non-zero to enable output for trigger programs, or zero to disable it.
3103 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3134 ** has a unique 64-bit signed
3212 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3234 ** any changes performed by sub-triggers, as the sqlite3_changes()
3235 ** value will be saved and restored after each sub-trigger has run.)^
3279 ** CAPI3REF: Interrupt A Long-Running Query
3285 ** or Ctrl-C where the user wants a long query operation to halt
3310 ** SQL statements is a no-op and has no effect on SQL statements
3318 ** These routines are useful during command-line input to determine if the
3324 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3339 ** then the return value from sqlite3_complete16() will be non-zero
3342 ** The input to [sqlite3_complete()] must be a zero-terminated
3343 ** UTF-8 string.
3345 ** The input to [sqlite3_complete16()] must be a zero-terminated
3346 ** UTF-16 string in native byte order.
3353 ** KEYWORDS: {busy-handler callback} {busy handler}
3375 ** ^If the callback returns non-zero, then another attempt
3380 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3451 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3453 ** to zero-terminated strings that contain the names of the columns.
3455 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3467 ** -----------------------
3489 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3521 ** These routines are work-alikes of the "printf()" family of functions
3524 ** plus some additional non-standard formats, detailed below.
3526 ** C-library standards are omitted from this implementation.
3549 ** guarantees that the buffer is always zero-terminated. ^The first
3552 ** written will be n-1 characters.
3561 ** ^(The %q option works like %s in that it substitutes a nul-terminated
3613 ** be contained within double-quotes instead of single quotes, and it
3614 ** escapes the double-quote character instead of the single-quote
3632 ** does not include operating-system specific VFS implementation. The
3643 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3644 ** of a signed 32-bit integer.
3649 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3654 ** might result if sqlite3_free() is called with a non-NULL pointer that
3674 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3675 ** of a 32-bit signed integer.
3690 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3694 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
3696 ** is no longer provided. Only built-in memory allocators can be used.
3700 ** filenames between the UTF-8 encoding used by SQLite
3727 ** routines, which form the built-in memory allocation subsystem.
3732 ** value of [sqlite3_memory_used()] since the high-water mark
3739 ** ^The memory high-water mark is reset to the current value of
3742 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3749 ** CAPI3REF: Pseudo-Random Number Generator
3751 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3754 ** the build-in random() and randomblob() SQL functions. This interface allows
3765 ** non-NULL P then the pseudo-randomness is generated
3772 ** CAPI3REF: Compile-Time Authorization Callbacks
3803 ** to the callback are either NULL pointers or zero-terminated strings
3831 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3850 ** statement might be re-prepared during [sqlite3_step()] due to a
3896 ** is the name of the inner-most trigger or view that is responsible for
3898 ** top-level SQL code.
3948 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3952 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3954 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3959 ** the original statement text and an estimate of wall-clock time
3979 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4001 ** interface by using the X argument when X begins with "--" and invoking
4008 ** X argument points to a 64-bit integer which is the estimated of
4038 ** M argument should be the bitwise OR-ed combination of
4088 ** ^If the progress callback returns non-zero, the operation is
4105 ** filename argument. ^The filename argument is interpreted as UTF-8 for
4106 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4117 ** ^The default encoding will be UTF-8 for databases created using
4119 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
4135 ** <dd>The database is opened in read-only mode. If the database does not
4155 ** opens in the multi-thread [threading mode] as long as the single-thread
4156 ** mode has not been set at compile-time or start-time. ^If the
4158 ** in the serialized [threading mode] unless single-thread was
4159 ** previously selected at compile-time or start-time.
4171 ** ^If the filename is ":memory:", then a private, temporary in-memory database
4172 ** is created for the connection. ^This in-memory database will vanish when
4180 ** on-disk database will be created. ^This private database will be
4190 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4213 ** SQLite and its built-in [VFSes] interpret the
4228 ** ^If "ro" is specified, then the database is opened for read-only
4231 ** "rw", then the database is opened for read-write (but not create)
4235 ** set to "memory" then a pure [in-memory database] that never reads
4262 ** read-only media. ^When immutable is set, SQLite assumes that the
4264 ** privilege, and so the database is opened read-only and all locking
4289 ** <tr><td style="white-space:nowrap">
4293 ** necessary - space characters can be used literally
4296 ** Open file "data.db" in the current directory for read-only access.
4297 ** Regardless of whether or not shared-cache mode is enabled by
4299 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4300 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4301 ** that uses dot-files in place of posix advisory locking.
4308 ** percent sign - "%" - followed by exactly two hexadecimal digits
4310 ** URI filename are interpreted, they are encoded using UTF-8 and all
4312 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4316 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4318 ** characters must be converted to UTF-8 prior to passing them into
4328 const char *filename, /* Database filename (UTF-8) */
4332 const void *filename, /* Database filename (UTF-16) */
4336 const char *filename, /* Database filename (UTF-8) */
4363 ** case or if the value begins with a non-zero number. The
4371 ** 64-bit signed integer and returns that integer, or D if P does not
4401 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4402 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4408 ** ^The sqlite3_errstr() interface returns the English-language text
4409 ** that describes the [result code], as UTF-8.
4445 ** The life-cycle of a prepared statement object usually goes like this:
4460 ** CAPI3REF: Run-time Limits
4473 ** set at compile-time by a C preprocessor macro called
4482 ** simply invoke this interface with the third parameter set to -1.
4484 ** Run-time limits are intended for use in applications that manage
4497 ** New run-time limit categories may be added in future releases.
4502 ** CAPI3REF: Run-Time Limit Categories
4506 ** that can be lowered at run-time using [sqlite3_limit()].
4600 ** To execute an SQL statement, it must first be compiled into a byte-code
4609 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4610 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4611 ** as a convenience. The UTF-16 interfaces work by converting the
4612 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4619 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4621 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4622 ** and sqlite3_prepare16_v3() use UTF-16.
4628 ** If the caller knows that the supplied string is nul-terminated, then
4631 ** the nul-terminator.
4681 ** ^The specific value of WHERE-clause [parameter] might influence the
4682 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4684 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4696 const char *zSql, /* SQL statement, UTF-8 encoded */
4703 const char *zSql, /* SQL statement, UTF-8 encoded */
4710 const char *zSql, /* SQL statement, UTF-8 encoded */
4718 const void *zSql, /* SQL statement, UTF-16 encoded */
4725 const void *zSql, /* SQL statement, UTF-16 encoded */
4732 const void *zSql, /* SQL statement, UTF-16 encoded */
4743 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4747 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4761 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4762 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4778 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4782 ** Note that [application-defined SQL functions] or
4786 ** change the database file through side-effects:
4814 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4851 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4862 ** implementation of [application-defined SQL functions] are protected.
4878 ** is always first parameter to [application-defined SQL functions].
4879 ** The application-defined SQL function implementation will pass this
4937 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4971 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4973 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5045 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5056 ** ^If the value N is out of range or if the N-th parameter is
5058 ** always in UTF-8 encoding even if the named parameter was
5059 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5076 ** name must be given in UTF-8 even if the original statement
5077 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5118 ** interface returns a pointer to a zero-terminated UTF-8 string
5119 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5120 ** UTF-16 string. ^The first parameter is the [prepared statement]
5131 ** (for example during a conversion from UTF-8 to UTF-16) then a
5150 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5159 ** ^The names returned are the original un-aliased names of the
5165 ** ^The left-most column is column 0 for these routines.
5174 ** UTF-16 encoded strings and the other functions return UTF-8.
5177 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5205 ** ^The returned string is always UTF-8 encoded.
5218 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5268 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5294 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5324 ** will return non-zero if previous call to [sqlite3_step](P) returned
5326 ** where it always returns zero since each step of that multi-step
5340 ** <li> 64-bit signed integer
5341 ** <li> 64-bit IEEE floating point number
5374 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5375 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5376 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5377 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5382 ** or a UTF-8 TEXT result in bytes
5384 ** <td>→ <td>Size of UTF-16
5436 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5438 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5439 ** the string to UTF-8 and then returns the number of bytes.
5441 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5445 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5447 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5448 ** the string to UTF-16 and then returns the number of bytes.
5450 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5461 ** even empty strings, are always zero-terminated. ^The return
5462 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5474 ** [application-defined SQL functions] or [virtual tables], not within
5475 ** top-level application code.
5493 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5514 ** sqlite3_column_text16() is called. A zero-terminator might
5516 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5518 ** to UTF-16.</li>
5519 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5521 ** to UTF-8.</li>
5524 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5589 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5604 ** object back to its initial state, ready to be re-executed.
5629 ** KEYWORDS: {application-defined SQL function}
5630 ** KEYWORDS: {application-defined SQL functions}
5643 ** connection then application-defined SQL functions must be added
5647 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5648 ** representation, exclusive of the zero-terminator. ^Note that the name
5649 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5655 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5658 ** parameter is less than -1 or greater than 127 then the behavior is
5677 ** deterministic. The built-in [random()] SQL function is an example of a
5686 ** pointers to C-language functions that implement the SQL function or
5708 ** SQL function is used. ^A function implementation with a non-negative
5717 ** ^Built-in functions may be overloaded by new application-defined functions.
5719 ** ^An application-defined function is permitted to call other
5762 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5763 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5764 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5807 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5808 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5810 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5811 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5813 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5814 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5817 ** or a UTF-8 TEXT in bytes
5819 ** <td>→ <td>Size of UTF-16
5835 ** [application-defined SQL functions] and [virtual tables].
5845 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5846 ** in the native byte-order of the host machine. ^The
5848 ** extract UTF-16 strings as big-endian and little-endian respectively.
5918 ** an [application-defined SQL function] argument V. The subtype
5937 ** then sqlite3_value_free(V) is a harmless no-op.
5998 ** the application-defined function is running.
6018 ** These functions may be used by (non-aggregate) SQL functions to
6022 ** of where this might be useful is in a regular-expression matching
6031 ** value to the application-defined function. ^N is zero for the left-most
6036 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6037 ** argument of the application-defined function. ^Subsequent
6061 ** function parameters that are compile-time constants, including literal
6064 ** The value of the N parameter to these interfaces should be non-negative.
6091 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6107 ** an application-defined function to be the BLOB whose content is pointed
6112 ** interfaces set the result of the application-defined function to be
6116 ** an application-defined function to be a floating point value specified
6124 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6125 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
6130 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6148 ** of the application-defined function to be the 32-bit signed integer
6151 ** of the application-defined function to be the 64-bit signed integer
6155 ** of the application-defined function to be NULL.
6159 ** set the return value of the application-defined function to be
6160 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6161 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6163 ** application-defined function to be a text string in an encoding
6172 ** is non-negative, then as many bytes (not characters) of the text
6173 ** pointed to by the 2nd parameter are taken as the application-defined
6174 ** function result. If the 3rd parameter is non-negative, then it
6181 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6195 ** the application-defined function to be a copy of the
6206 ** also associates the host-language pointer P or type T with that
6208 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6216 ** than the one containing the application-defined function that received
6248 ** the result from the [application-defined SQL function] with
6264 ** ^The name of the collation is a UTF-8 string
6266 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6328 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6371 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6372 ** the names are passed as UTF-16 in machine native byte order.
6373 ** ^A call to either function replaces the existing collation-needed callback.
6477 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6519 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6520 ** TemporaryFolder->Path->Data();
6523 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6536 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6568 ** CAPI3REF: Test For Auto-Commit Mode
6572 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6576 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6578 ** If certain kinds of errors occur on a statement within a multi-statement
6611 ** connection D, or if database N is a temporary or in-memory database, then
6622 ** CAPI3REF: Determine if a database is read-only
6626 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6660 ** ^If the callback on a commit hook function returns non-zero,
6682 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6684 ** hook returning non-zero, just as it would be with any other rollback.
6779 ** shared cache mode should be enabled per-database connection via
6783 ** 32-bit integer is atomic.
6785 ** See Also: [SQLite Shared-Cache Mode]
6793 ** of heap memory by deallocating non-essential memory allocations
6795 ** pages to improve performance is an example of non-essential memory.
6798 ** ^The sqlite3_release_memory() routine is a no-op returning zero
6812 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6847 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
6848 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
6859 ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
6891 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
6895 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
6962 int *pAutoinc /* OUTPUT: True if column is auto-increment */
6974 ** with various operating-system specific extensions added.
6983 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7022 ** [extension loading] while evaluating user-entered SQL, the following API
7030 ** ^This interface enables or disables both the C-API
7033 ** to enable or disable only the C-API.)^
7073 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7102 ** The interface to the virtual-table mechanism is currently considered
7106 ** When the virtual-table mechanism stabilizes, we will declare the
7177 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7189 ** expr on the right-hand side can be evaluated (and thus the constraint
7204 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7210 ** non-zero.
7214 ** the right-hand side of the corresponding aConstraint[] is evaluated
7215 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7238 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7269 int iColumn; /* Column constrained. -1 for ROWID */
7272 int iTermOffset; /* Used internally - xBestIndex should ignore */
7442 ** The interface to the virtual-table mechanism defined above (back up
7447 ** When the virtual-table mechanism stabilizes, we will declare the
7484 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7486 ** read-only access.
7522 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7538 ** and the built-in [zeroblob] SQL function may be used to create a
7539 ** zero-filled blob to read or write using the incremental-blob interface.
7569 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7591 ** ^If the blob handle being closed was opened for read-write access, and if
7592 ** the database is in auto-commit mode and there are no other open read-write
7600 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7627 ** caller-supplied buffer. N bytes of data are copied into buffer Z
7656 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
7705 ** ^Names are zero-terminated UTF-8 strings.
7736 ** is selected automatically at compile-time. The following
7747 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7752 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7789 ** cases where it really needs one. If a faster non-recursive mutex
7836 ** behave as no-ops.
7849 ** An instance of this structure defines the low-level routines
7899 ** xMutexInit() must be no-ops.
7945 ** the routine should return 1. This seems counter-intuitive since
7949 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
7965 ** next. Applications that override the built-in mutex logic must be
7981 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
7992 ** ^If the [threading mode] is Single-thread or Multi-thread then this
7998 ** CAPI3REF: Low-Level Control Of Database Files
8017 ** case is a short-circuit path which does not actually invoke the
8106 ** SQLITE_OK on success and a non-zero [error code] on failure.
8109 ** be represented by a 32-bit integer, then the values returned by
8127 ** These integer constants designate various run-time status parameters
8135 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8220 ** non-zero [error code] on failure.
8236 ** The [sqlite3_db_status()] interface will return a non-zero error code
8246 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8254 ** Only the high-water value is meaningful;
8262 ** Only the high-water value is meaningful;
8285 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8386 ** A non-zero value in this counter may indicate an opportunity to
8392 ** A non-zero value in this counter may indicate an opportunity to
8479 ** The built-in page cache is recommended for most uses.
8494 ** built-in default page cache is used instead of the application defined
8526 ** false if it is used for an in-memory database. The cache implementation
8537 ** suggested maximum cache-size (number of pages stored by) the cache
8583 ** as its second argument. If the third parameter, discard, is non-zero,
8617 ** is not obligated to free any memory, but well-behaved implementations should
8676 ** for copying in-memory databases to or from persistent files.
8682 ** ^The source database is read-locked only while it is being read;
8716 ** there is already a read or read-write transaction open on the
8748 ** <li> the destination database was opened read-only, or
8749 ** <li> the destination database is using write-ahead-log journaling
8751 ** <li> the destination database is an in-memory database and the
8755 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
8756 ** the [sqlite3_busy_handler | busy-handler function]
8758 ** busy-handler returns non-zero before the lock is available, then
8779 ** sqlite3_backup_step(), the source database may be modified mid-way
8796 ** active write-transaction on the destination database is rolled back.
8803 ** ^If an out-of-memory condition or IO error occurred during any prior
8841 ** backup is in progress might also also cause a mutex deadlock.
8872 ** ^When running in shared-cache mode, a database operation may fail with
8873 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
8874 ** individual tables within the shared-cache cannot be obtained. See
8875 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
8879 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
8883 ** ^Shared-cache locks are released when a database connection concludes
8887 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
8897 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
8903 ** ^If the blocked connection is attempting to obtain a write-lock on a
8904 ** shared-cache table, and more than one other connection currently holds
8905 ** a read-lock on the same table, then SQLite arbitrarily selects one of
8908 ** ^(There may be at most one unlock-notify callback registered by a
8910 ** blocked connection already has a registered unlock-notify callback,
8913 ** unlock-notify callback is canceled. ^The blocked connections
8914 ** unlock-notify callback may also be canceled by closing the blocked
8917 ** The unlock-notify callback is not reentrant. If an application invokes
8918 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
8919 ** crash or deadlock may be the result.
8921 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
8926 ** When an unlock-notify callback is registered, the application provides a
8930 ** an unlock-notify callback is a pointer to an array of void* pointers,
8934 ** more than one blocked connection that has registered for an unlock-notify
8942 ** <b>Deadlock Detection</b>
8944 ** Assuming that after registering for an unlock-notify callback a
8947 ** application to deadlock. For example, if connection X is waiting for
8952 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
8955 ** unlock-notify callback is registered. The system is said to be in
8956 ** a deadlocked state if connection A has registered for an unlock-notify
8958 ** B has itself registered for an unlock-notify callback when connection
8959 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
8961 ** registered for an unlock-notify callback on the conclusion of connection
8973 ** sqlite3_unlock_notify() results in the unlock-notify callback being
8974 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
8994 ** and extensions to compare the contents of two buffers containing UTF-8
8995 ** strings in a case-independent fashion, using the same definition of "case
9011 ** Note that this routine returns zero on a match and non-zero if the strings
9028 ** insensitive - equivalent upper and lower case ASCII characters match
9034 ** Note that this routine returns zero on a match and non-zero if the strings
9058 ** a fixed-length buffer on the stack. If the log message is longer than
9065 ** CAPI3REF: Write-Ahead Log Commit Hook
9072 ** the associated write-lock on the database released)^, so the implementation
9078 ** ^The third parameter is the name of the database that was written to -
9079 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9080 ** is the number of pages currently in the write-ahead log file,
9091 ** A single database handle may have at most a single write-ahead log callback
9093 ** previously registered write-ahead log callback. ^Note that the
9105 ** CAPI3REF: Configure an auto-checkpoint
9112 ** more frames in the [write-ahead log] file. ^Passing zero or
9127 ** ^Every new [database connection] defaults to having the auto-checkpoint
9143 ** [write-ahead log] for database X on [database connection] D to be
9144 ** transferred into the database file and for the write-ahead log to
9170 ** in the log were checkpointed. ^The [busy-handler callback]
9177 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9186 ** [busy-handler callback])
9199 ** the log file or to -1 if the checkpoint could not run because
9203 ** was called) or to -1 if the checkpoint could not run due to an error or
9211 ** busy-handler configured, it will not be invoked in this case.
9215 ** obtained immediately, and a busy-handler is configured, it is invoked and
9216 ** the writer lock retried until either the busy-handler returns 0 or the lock
9217 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9218 ** database readers as described above. ^If the busy-handler returns 0 before
9221 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9237 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9306 ** If X is non-zero, then the virtual table implementation guarantees
9347 ** a lighter-weight value to return that the corresponding [xUpdate] method
9348 ** understands as a "no-change" value.
9407 ** set to the total number of times that the X-th loop has run.</dd>
9411 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
9416 ** iteration of the X-th loop. If the query planner's estimates was accurate,
9423 ** to a zero-terminated UTF-8 string containing the name of the index or table
9424 ** used for the X-th loop.
9428 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9429 ** description for the X-th loop.
9433 ** "select-id" for the X-th loop. The select-id identifies which query or
9434 ** subquery the loop is part of. The main query has a select-id of zero.
9435 ** The select-id is the same value as is output in the first column
9457 ** compile-time option.
9465 ** Loops are numbered starting from zero. ^If idx is out of range - less than
9467 ** the statement - a non-zero value is returned and the variable that pOut
9472 ** as if the loop did not exist - it returns non-zero and leave the variable
9485 ** CAPI3REF: Zero Scan-Status Counters
9490 ** This API is only available if the library is built with pre-processor
9496 ** CAPI3REF: Flush caches to disk mid-transaction
9498 ** ^If a write-transaction is open on [database connection] D when the
9500 ** pages in the pager-cache that are not currently in use are written out
9504 ** interface flushes caches for all schemas - "main", "temp", and
9509 ** immediately and there is a busy-handler callback configured, it is invoked
9517 ** example an IO error or out-of-memory condition), then processing is
9528 ** CAPI3REF: The pre-update hook.
9531 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9603 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
9604 ** triggers; or 2 for changes resulting from triggers called by top-level
9630 ** CAPI3REF: Low-level system error code
9634 ** The return value is OS-dependent. For example, on unix systems, after
9680 ** If there is not already a read-transaction open on schema S when
9712 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9750 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9767 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9858 /* The double-precision datatype used by RTree depends on the
9859 ** SQLITE_RTREE_INT_ONLY compile-time option.
9869 ** R-Tree geometry query as follows:
9894 ** Register a 2nd-generation geometry callback named zScore that can be
9895 ** used as part of an R-Tree geometry query as follows:
9994 ** is not possible for an application to register a pre-update hook on a
9997 ** which a pre-update hook is already defined. The results of attempting
10031 ** disabled - it does not. A newly created session object is enabled.
10038 ** no-op, and may be used to query the current state of the session.
10113 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
10119 ** zero-length blob back to a NULL value when updating the sqlite_stat1
10123 ** conflict-handler callback) then the X'' value is returned. The application
10169 ** database row along with the updated values for each updated non-primary-key
10178 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10180 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
10193 ** are sorted in the same order in which they were attached (or auto-attached)
10207 ** recorded once - the first time a row with said primary key is inserted,
10214 ** The session object therefore accumulates two types of records - those
10232 ** found and one or more of the non-primary key fields have been
10249 ** This may appear to have some counter-intuitive effects if a single row
10270 ** does not have a primary key, this function is a no-op (but does not return
10290 ** used to update the table in database zFrom (call this the "from-table")
10292 ** object (call this the "to-table"). Specifically:
10295 ** <li> For each row (primary key) that exists in the to-table but not in
10296 ** the from-table, an INSERT record is added to the session object.
10298 ** <li> For each row (primary key) that exists in the to-table but not in
10299 ** the from-table, a DELETE record is added to the session object.
10302 ** different non-PK values in each, an UPDATE record is added to the
10346 ** Because the non-primary key "old.*" fields are omitted, no
10365 ** Return non-zero if no changes to attached tables have been recorded by
10373 ** are restored. However, if this function returns non-zero, then it is
10424 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10447 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10453 ** nul-terminated utf-8 encoded string containing the name of the table
10456 ** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
10504 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
10512 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10542 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10575 ** conflict-handler callback by [sqlite3changeset_apply()] with either
10586 ** "conflicting row" associated with the current conflict-handler callback
10622 ** function with an iterator passed to a conflict-handler by
10630 ** returned. This is to allow the following pattern (pseudo-code):
10774 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
10775 ** <tr><th style="white-space:pre">Existing Change </th>
10776 ** <th style="white-space:pre">New Change </th>
10821 ** returned. Or, if an out-of-memory condition occurs during processing, this
10877 ** the table. Otherwise, if the return value is non-zero or the xFilter
10914 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
10918 ** returned by each invocation of the conflict-handler function. Refer to
10927 ** stored in all non-primary key columns also match the values stored in
10931 ** the non-primary key fields contains a value different from the original
10932 ** row value stored in the changeset, the conflict-handler function is
10935 ** only the values of those non-primary key fields are compared against
10936 ** the current database contents - any trailing database table columns
10940 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10945 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
10964 ** This includes the case where the INSERT operation is re-attempted because
10972 ** stored in all modified non-primary key columns also match the values
10976 ** the modified non-primary key fields contains a value different from an
10977 ** original row value stored in the changeset, the conflict-handler function
10979 ** UPDATE changes only contain values for non-primary key fields that are
10981 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
10984 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10988 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11025 ** Values that may be passed as the second argument to a conflict-handler.
11032 ** (non primary-key) fields modified by the update do not contain the
11122 ** corresponding non-streaming API functions:
11124 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
11125 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
11134 ** Non-streaming functions that accept changesets (or patchsets) as input
11139 ** low-memory environment is required to handle very large changesets, the
11334 ** non-negative but less than the number of columns in the table, return
11349 ** non-negative but less than the number of columns in the table, set
11363 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
11395 ** set to -1.
11465 ** If the bClear argument is non-zero, then the auxiliary data is cleared
11487 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
11489 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
11495 ** modify this structure directly - it should only be used as shown above
11503 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
11517 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
11519 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
11528 ** xPhraseFirstColumn() set iCol to -1).
11597 ** The second and third arguments are an array of nul-terminated strings
11615 ** by argument pText. pText may or may not be nul-terminated. The first
11623 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
11628 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
11632 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
11637 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
11670 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
11704 ** still appears to contain just two phrases - "(first OR 1st)"
11715 ** when tokenizing query text (it should not - to do would be
11762 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
11858 ** autoconf-based build
11886 ** The hard limit is the ability of a 32-bit signed integer
11887 ** to count the size: 2^31-1 or 2147483647.
11905 ** tell you that in a well-normalized database, you usually should
11967 ** The suggested maximum number of in-memory pages to use for
11970 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
11972 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
11973 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
11976 # define SQLITE_DEFAULT_CACHE_SIZE -2000
11990 ** counted using a signed 8-bit integer which has a maximum value of 127
12006 ** imposed by the use of 16-bit offsets within each page.
12012 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
12013 ** compiled with the default page-size limit will not be able to rollback
12036 ** device characteristics (sector-size and atomic write() support),
12053 ** This value can be lowered (or raised) at run-time using that the
12084 #pragma warn -rch /* unreachable code */
12085 #pragma warn -ccc /* Condition is always true or false */
12086 #pragma warn -aus /* Assigned value is never used */
12087 #pragma warn -csu /* Comparing signed and unsigned */
12088 #pragma warn -spa /* Suspicious pointer arithmetic */
12112 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
12122 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
12126 #else /* Generates a warning - but it always works */
12166 ** level of threadsafety. 2 means the library is multithreaded - multiple
12174 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
12182 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
12188 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
12195 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
12224 # error "Two or more of the following compile-time configuration options\
12262 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
12330 ** of SQLite to unexpected behavior - to make the code "self-healing"
12336 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
12415 ** Return true (non-zero) if the input is an integer that is too large
12416 ** to fit in 32-bits. This macro is used inside of various testcase()
12417 ** macros to verify that we have tested SQLite for large-file support.
12444 ** This is the header file for the generic hash-table implementation
12455 ** The internals of this structure are intended to be opaque -- client
12462 ** All elements of the hash table are on a single doubly-linked list.
12466 ** the global doubly-linked list. The contents of the bucket are the
12467 ** element pointed to plus the next _ht.count-1 elements in the list.
12486 ** structure. All elements are stored on a single doubly-linked list.
12517 #define sqliteHashFirst(H) ((H)->first)
12518 #define sqliteHashNext(E) ((E)->next)
12519 #define sqliteHashData(E) ((E)->data)
12520 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
12521 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
12526 /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
12718 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
12723 ** substitute integer for floating-point
12754 ** the VDBE-level file format changes. The following macros define the
12765 ** changed at run-time using a pragma.
12773 ** on the command-line
12803 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
12805 ** The default value of "20" was choosen to minimize the run-time of the
12806 ** speedtest1 test program with options: --shrink-memory --reprepare
12813 ** The compile-time options SQLITE_MMAP_READWRITE and
12826 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
12857 ** types can be conveniently redefined at compile-type. Like this:
12859 ** cc '-DUINTPTR_TYPE=long long int' ...
12899 typedef sqlite_int64 i64; /* 8-byte signed integer */
12900 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
12901 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
12902 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
12903 typedef INT16_TYPE i16; /* 2-byte signed integer */
12904 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
12905 typedef INT8_TYPE i8; /* 1-byte signed integer */
12913 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
12918 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
12919 ** can be used at compile-time if desired.
12922 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
12924 typedef u32 tRowcnt; /* 32-bit is the default */
12928 ** Estimated quantities used for query planning are stored as 16-bit
12930 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
12939 ** 1 -> 0 20 -> 43 10000 -> 132
12940 ** 2 -> 10 25 -> 46 25000 -> 146
12941 ** 3 -> 16 100 -> 66 1000000 -> 199
12942 ** 4 -> 20 1000 -> 99 1048576 -> 200
12943 ** 10 -> 33 1024 -> 100 4294967296 -> 320
12948 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
12989 ** and whether or not that determination is run-time or compile-time.
12991 ** For best performance, an attempt is made to guess at the byte-order
12992 ** using C-preprocessor macros. If that is unsuccessful, or if
12993 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
12994 ** at run-time.
13028 ** Constants for the largest and smallest possible 64-bit signed integers.
13029 ** These macros are designed to work correctly on both 32-bit and 64-bit
13033 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
13037 ** to force 8-byte alignment on 64-bit architectures.
13047 ** Assert that the pointer X is aligned to an 8-byte boundary. This
13052 ** underlying malloc() implementation might return us 4-byte aligned
13053 ** pointers. In that case, only verify 4-byte alignment.
13056 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
13058 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
13070 ** Default maximum size of memory used by memory-mapped I/O in the VFS
13090 ** default MMAP_SIZE is specified at compile-time, make sure that it does
13126 ** An instance of the following structure is used to store the busy-handler
13131 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
13150 ** The root-page of the master database table.
13168 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
13187 ** directly, we use its constant as a key to lookup the run-time allocated
13189 ** for the run-time allocated buffer.
13192 ** macros become no-ops and have zero performance impact.
13301 ** This header file defines the interface that the sqlite B-Tree file
13314 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
13321 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
13322 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
13335 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
13350 #define BTREE_MEMORY 2 /* This is an in-memory DB */
13351 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
13397 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
13400 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
13403 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
13404 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
13424 ** For example, the free-page-count field is located at byte offset 36 of
13425 ** the database file header. The incr-vacuum-flag field is located at
13429 ** It is a read-only number computed by the pager. But we merge it with
13442 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
13456 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
13457 ** column of the b-tree of the cursor. The Expr tree will not contain
13458 ** any function calls nor subqueries nor references to b-trees other than
13461 ** The design of the _RANGE hint is aid b-tree implementations that try
13462 ** to prefetch content from remote machines - to provide those
13487 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
13492 ** For read-only cursors the wrFlag argument is always zero. For read-write
13503 ** by this, the native b-tree engine of SQLite, but it is available to
13505 ** b-tree system. For alternative storage engines in which a delete of
13509 ** and DELETE operations as no-ops, and any READ operation against a
13512 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
13518 int wrFlag, /* 1 for writing. 0 for read-only */
13626 ** Enter and Leave procedures no-ops.
13713 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
13745 int iSrcLine; /* Source-code line that generated this opcode */
13752 ** A sub-routine used to implement a trigger program.
13755 VdbeOp *aOp; /* Array of opcodes for sub-program */
13761 SubProgram *pNext; /* Next sub-program already visited */
13781 #define P4_STATIC (-1) /* Pointer to a static string */
13782 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
13783 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
13784 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
13785 #define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
13786 #define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
13788 #define P4_FREE_IF_LE (-7)
13789 #define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
13790 #define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
13791 #define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
13792 #define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
13793 #define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
13794 #define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
13795 #define P4_REAL (-13) /* P4 is a 64-bit floating point value */
13796 #define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
13797 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
13798 #define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
13799 #define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
13832 #define ADDR(X) (-1-(X))
13890 #define OP_IfPos 48 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
13891 #define OP_IfNotZero 49 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
13901 #define OP_DecrJumpZero 59 /* jump, synopsis: if (--r[P1])==0 goto P2 */
13931 #define OP_Subtract 89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
13936 #define OP_Compare 94 /* synopsis: r[P1@P3] <-> r[P2@P3] */
13990 #define OP_OffsetLimit 148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
14059 ** Additional non-public SQLITE_PREPARE_* flags
14149 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
14187 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
14188 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
14243 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14266 ** is devoted to storing a master journal name - there are no more pages to
14270 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14278 #define PAGER_MEMORY 0x0002 /* In-memory database */
14283 #define PAGER_LOCKINGMODE_QUERY -1
14294 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14299 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14300 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14306 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14328 ** that make up the Pager sub-system API. See source code comments for
14545 /* Modify the page-size after the cache has been created. */
14567 /* Change a page number. Used by incr-vacuum. */
14609 /* Set and get the suggested cache-size for the specified pager-cache.
14612 ** the total number of pages cached by purgeable pager-caches to the sum
14613 ** of the suggested cache-sizes.
14620 /* Set or get the suggested spill-size for the specified pager-cache.
14622 ** The spill-size is the minimum number of pages in cache before the cache
14666 ** This header file (together with is companion C source-code file
14670 ** This header file is #include-ed by sqliteInt.h and thus ends up
14678 ** necessary pre-processor macros for it.
14694 ** This file contains pre-processor directives related to operating system
14744 ** a no-op
14764 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
14766 ** 2006-10-31: The default prefix used to be "sqlite_". But then
14767 ** Mcafee started using SQLite in their anti-virus product and it
14813 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
14921 ** sqlite3_malloc() to obtain space for the file-handle structure.
14960 ** at start-time.
14962 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
14965 ** start-time.
14967 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
14969 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
14986 ** If this is a no-op implementation, implement everything as macros.
15006 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
15026 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
15027 ** In other words, the zero-based numbers are used for all external interfaces
15028 ** and the one-based values are used internally.
15056 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
15085 ** Db.pSchema->flags field.
15087 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
15088 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
15089 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
15090 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
15093 ** Allowed values for the DB.pSchema->flags field.
15114 ** Lookaside malloc is a set of fixed-size buffers that can be used
15149 ** A hash table for built-in function definitions. (Application-defined
15222 u32 nSchemaLock; /* Do not reset the schema when non-zero */
15229 u8 autoCommit; /* The auto-commit flag. */
15233 u8 dfltLockMode; /* Default locking-mode for attached dbs */
15239 u8 skipBtreeMutex; /* True if no shared-cache backends */
15276 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
15313 int nSavepoint; /* Number of non-transaction savepoints */
15314 int nStatement; /* Number of nested statement-transactions */
15343 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
15344 #define ENC(db) ((db)->enc)
15367 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
15396 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
15413 #define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
15422 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
15423 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
15429 #define ConstFactorOk(P) ((P)->okConstFactor)
15445 ** structure. For global built-in functions (ex: substr(), max(), count())
15447 ** For per-connection application-defined functions, a pointer to this
15448 ** structure is held in the db->aHash hash table.
15450 ** The u.pHash field is used by the global built-ins. The u.pDestructor
15451 ** field is used by per-connection app-def functions.
15454 i8 nArg; /* Number of arguments. -1 means unlimited */
15458 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
15468 ** This structure encapsulates a user-function destructor callback (as
15502 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
15505 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
15506 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
15507 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
15508 #define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
15509 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
15513 ** single query - might change over time */
15514 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
15515 #define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
15525 ** as the user-data (sqlite3_user_data()) for the function. If
15535 ** a single query. The iArg is ignored. The user-data is always set
15541 ** ignored and the user-data for these functions is set to an
15542 ** arbitrary non-NULL pointer. The bNC parameter is not used.
15554 ** available as the function user-data (sqlite3_user_data()). The
15593 char *zName; /* Savepoint name (nul-terminated) */
15651 char *zName; /* Name of the collating sequence, UTF-8 encoded */
15663 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
15717 ** database connections, even when the rest of the in-memory database
15727 ** database schema are initially stored in a linked-list pointed to by
15734 ** When an in-memory Table object is deleted (for example when the
15741 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
15778 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
15801 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
15802 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
15806 #define TF_Readonly 0x0001 /* Read-only system table */
15812 #define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */
15813 #define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */
15824 # define IsVirtual(X) ((X)->nModuleArg)
15831 ** only works for non-virtual tables (ordinary tables and views) and is
15836 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15837 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15839 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15848 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
15849 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
15864 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
15867 ** from-table == child-table
15868 ** to-table == parent-table
15871 ** which is attached to the from-table. The to-table need not exist when
15872 ** the from-table is created. The existence of the to-table is not checked.
15886 /* EV: R-30323-21917 */
15947 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
15969 ** pKeyInfo->nField.
15973 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
15977 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
15978 ** multiple entries in the b-tree with the same key (when only looking
15979 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
15984 ** get and equal results when comparing this structure to a b-tree record.
15988 ** b-tree.
15991 KeyInfo *pKeyInfo; /* Collation and sort-order information */
16025 ** algorithm to employ whenever an attempt is made to insert a non-unique
16033 ** number (it cannot - the database page is not allocated until the VDBE
16058 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
16065 IndexSample *aSample; /* Samples of the left-most key */
16066 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
16067 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
16079 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
16082 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
16087 #define XN_ROWID (-1) /* Indexed column is the rowid */
16088 #define XN_EXPR (-2) /* Indexed column is an expression */
16112 const char *z; /* Text of the token. Not NULL-terminated! */
16135 int sortingIdxPTab; /* Cursor number of pseudo-table */
16161 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
16162 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
16163 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
16168 ** the option is available (at compile-time).
16183 ** to represent the greater-than-or-equal-to operator in the expression
16197 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
16216 ** subquery gives a constant result, then iTable is -1. If the subquery
16221 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
16245 int iValue; /* Non-negative integer value if EP_IntValue */
16270 ** TK_TRIGGER: 1 -> new, 0 -> old
16273 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
16276 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
16324 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
16325 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
16326 #define ExprSetProperty(E,P) (E)->flags|=(P)
16327 #define ExprClearProperty(E,P) (E)->flags&=~(P)
16331 ** processes but is a no-op for delivery.
16334 # define ExprSetVVAProperty(E,P) (E)->flags|=(P)
16352 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
16362 ** By default the Expr.zSpan field holds a human-readable description of
16403 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
16416 ** Changing this from a 64-bit to a 32-bit type limits the number of
16436 #define ALLBITS ((Bitmask)-1)
16454 ** In the colUsed field, the high-order bit (bit 63) is set if the table
16455 ** contains more than 63 columns and the 64-th or later column is used.
16469 int regResult; /* Registers holding results of a co-routine */
16474 unsigned isTabFunc :1; /* True if table-valued-function syntax */
16475 unsigned isCorrelated :1; /* True if sub-query is correlated */
16476 unsigned viaCoroutine :1; /* Implemented as a co-routine */
16480 u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
16488 ExprList *pFuncArg; /* Arguments to table-valued-function */
16513 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
16516 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
16519 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
16548 ** NameContexts can be nested. When resolving names, the inner-most
16563 ExprList *pEList; /* Optional list of result-set columns */
16593 ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
16666 ** identified by pDest->iSDParm.
16668 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
16670 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
16675 ** the side-effects of functions.
16685 ** in register pDest->iSDParm then abandon the rest
16689 ** row of result as the key in table pDest->iSDParm.
16690 ** Apply the affinity pDest->affSdst before storing
16693 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
16699 ** SRT_Coroutine Generate a co-routine that returns a new row of
16701 ** of the co-routine is stored in register pDest->iSDParm
16702 ** and the result row is stored in pDest->nDest registers
16703 ** starting with pDest->iSdst.
16705 ** SRT_Table Store results in temporary table pDest->iSDParm.
16711 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
16712 ** But also use temporary table pDest->iSDParm+1 as
16716 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
16720 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
16722 ** index at pDest->iSDParm+1 hold all prior stores.
16734 #define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
16760 ** the code generator needs. We have to keep per-table autoincrement
16786 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
16834 ** The nTableLock and aTableLock variables are only used if the shared-cache
16835 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
16836 ** used to store the set of table-locks required by the statement being
16863 ** of the base register during check-constraint eval */
16874 int nMaxArg; /* Max args passed to user function by sub-program */
16881 TableLock *aTableLock; /* Required table locks for shared-cache mode */
16927 int nHeight; /* Expression tree height of current sub-select */
16937 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
16953 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
16962 #define IN_DECLARE_VTAB (pParse->declareVtab)
16986 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
16989 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
16993 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
17026 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
17027 the <column-list> is stored here */
17046 * that is a part of a trigger-program.
17051 * the first step of the trigger-program.
17058 * orconf -> stores the ON CONFLICT algorithm
17059 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
17061 * zTarget -> Dequoted name of the table to insert into.
17062 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
17064 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
17065 * statement, then this stores the column-names to be
17069 * zTarget -> Dequoted name of the table to delete from.
17070 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
17074 * zTarget -> Dequoted name of the table to update.
17075 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
17077 * pExprList -> A list of the columns to update and the expressions to update
17092 TriggerStep *pNext; /* Next in the link-list */
17093 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
17107 const char *zType; /* Type of the container - used for error messages */
17108 const Token *pName; /* Name of the container - used for error messages */
17126 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
17130 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
17154 int bUseCis; /* Use covering indices for full-scans */
17157 int neverCorrupt; /* Database is always well-formed */
17160 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
17161 sqlite3_mem_methods m; /* Low-level memory allocation interface */
17162 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
17163 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
17173 int sharedCacheEnabled; /* true if shared-cache mode enabled */
17175 /* The above might be initialized to non-zero. The following need to always
17206 ** the assert is only valid on a well-formed database. Instead of:
17218 ** things that are always true for well-formed databases.
17223 ** Context pointer passed down through the tree-walk.
17261 ** Return code from the parse-tree walking primitives and their
17286 ** data structures on sqlite3DebugPrintf() using a tree-like view.
17295 ** Assuming zIn points to the first byte of a UTF-8 character,
17296 ** advance zIn to point to the first byte of the next UTF-8 character.
17307 ** routines that report the line-number on which the error originated
17349 ** The ctype.h header is needed for non-ASCII systems. It is also
17565 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
17803 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
17804 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
17864 ** Routines to read and write variable-length integers. These used to
18033 ** The interface to the LEMON-generated parser
18091 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
18129 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
18131 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
18133 ** provided (enforcement of FK constraints requires the triggers sub-system).
18182 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
18235 ** sqlite3IoTrace is a pointer to a printf-like routine used to
18268 ** passed back to non-lookaside free() routines. Asserts such as the
18269 ** example above are placed on the non-lookaside free() routines to verify
18272 ** All of this is no-op for a production build. It only comes into
18273 ** play when the SQLITE_MEMDEBUG compile-time option is used.
18280 # define sqlite3MemdebugSetType(X,Y) /* no-op */
18333 /* An array to map all upper-case characters into their corresponding
18334 ** lower-case character.
18336 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
18379 ** The following 256 byte lookup table is used to support SQLites built-in
18392 ** case. i.e. if the character is a lower-case ASCII character.
18393 ** If x is a lower-case ASCII character, then its upper-case equivalent
18394 ** is (x - 0x20). Therefore toupper() can be implemented as:
18401 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
18403 ** non-ASCII UTF character. Hence the test for whether or not a character is
18413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
18446 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
18450 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
18451 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
18453 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
18468 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
18469 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
18470 ** that compile-time option is omitted.
18486 ** before 3.12.0). -1 means always keep the entire statement journal in
18496 ** The default lookaside-configuration, the format "SZ,N". SZ is the
18498 ** and N is the number of slots. The lookaside-configuration can be
18499 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
18500 ** or at run-time for an individual database connection using
18563 ** Hash table for global functions - functions common to all
18565 ** read-only.
18580 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
18700 ** * A b-tree cursor
18701 ** - In the main database or in an ephemeral database
18702 ** - On either an index or a table
18705 ** * A one-row "pseudotable" stored in a single register
18710 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
18719 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
18772 ** When a sub-program is executed (OP_Program), a structure of this type
18775 ** values stored in the Vdbe struct. When the sub-program is finished,
18777 ** restoring the state of the VM to as it was before the sub-program
18811 int nDbChange; /* Value of db->nChange */
18841 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
18899 /* Return TRUE if Mem X contains dynamically allocated content - anything
18903 (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
18909 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
18916 #define memIsValid(M) ((M)->flags & MEM_Undefined)==0
18955 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
18970 int iSelectID; /* The "Select-ID" for this loop */
19013 i64 startTime; /* Time when query started - used for profiling */
19024 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
19026 bft changeCntOn:1; /* True to update the change-counter */
19031 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
19040 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
19186 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
19231 ** we have to locate the state vector at run-time. In the more common
19261 ** The value of N is added to the current status value and the high-water
19265 ** mark is unchanged. N must be non-negative for StatusDown().
19285 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
19357 p = p->pNext; in countLookasideSlots()
19367 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
19368 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
19369 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
19370 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
19380 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
19381 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
19389 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
19394 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
19396 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
19397 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
19398 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
19399 db->lookaside.pFree = 0; in sqlite3_db_status()
19411 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
19412 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
19414 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
19416 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
19431 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19432 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
19458 db->pnBytesFreed = &nByte; in sqlite3_db_status()
19459 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19460 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
19465 pSchema->tblHash.count in sqlite3_db_status()
19466 + pSchema->trigHash.count in sqlite3_db_status()
19467 + pSchema->idxHash.count in sqlite3_db_status()
19468 + pSchema->fkeyHash.count in sqlite3_db_status()
19470 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
19471 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
19472 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
19473 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
19475 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
19478 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
19483 db->pnBytesFreed = 0; in sqlite3_db_status()
19500 db->pnBytesFreed = &nByte; in sqlite3_db_status()
19501 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ in sqlite3_db_status()
19505 db->pnBytesFreed = 0; in sqlite3_db_status()
19507 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
19526 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19527 if( db->aDb[i].pBt ){ in sqlite3_db_status()
19528 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
19532 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
19533 /* IMP: R-54100-20147 */ in sqlite3_db_status()
19534 /* IMP: R-29431-39229 */ in sqlite3_db_status()
19539 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
19544 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
19545 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
19553 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
19573 ** There is only one exported symbol in this file - the function
19582 ** 1970-01-01 00:00:00 is JD 2440587.5
19583 ** 2000-01-01 00:00:00 is JD 2451544.5
19585 ** This implementation requires years to be expressed as a 4-digit number
19586 ** which means that only dates between 0000-01-01 and 9999-12-31 can
19592 ** use the julian calendar for dates prior to 1582-10-15 and for some
19600 ** ISBN 0-943396-61-1
19601 ** Willmann-Bell, Inc
19647 ** of a four-character format specifiers ABCD is:
19661 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
19662 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
19663 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
19664 ** the 2-digit day which is the last integer in the set.
19677 char N = zFormat[0] - '0'; in getDigits()
19678 char min = zFormat[1] - '0'; in getDigits()
19683 max = aMx[zFormat[2] - 'a']; in getDigits()
19686 while( N-- ){ in getDigits()
19690 val = val*10 + *zDate - '0'; in getDigits()
19707 ** Parse a timezone extension on the end of a date-time.
19710 ** (+/-)HH:MM
19717 ** of change in p->tz and return 0. If a parser error occurs,
19718 ** return non-zero.
19727 p->tz = 0; in parseTimezone()
19729 if( c=='-' ){ in parseTimezone()
19730 sgn = -1; in parseTimezone()
19744 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
19747 p->tzSet = 1; in parseTimezone()
19775 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
19784 p->validJD = 0; in parseHhMmSs()
19785 p->rawS = 0; in parseHhMmSs()
19786 p->validHMS = 1; in parseHhMmSs()
19787 p->h = h; in parseHhMmSs()
19788 p->m = m; in parseHhMmSs()
19789 p->s = s + ms; in parseHhMmSs()
19791 p->validTZ = (p->tz!=0)?1:0; in parseHhMmSs()
19800 p->isError = 1; in datetimeError()
19804 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
19805 ** that the YYYY-MM-DD is according to the Gregorian calendar.
19812 if( p->validJD ) return; in computeJD()
19813 if( p->validYMD ){ in computeJD()
19814 Y = p->Y; in computeJD()
19815 M = p->M; in computeJD()
19816 D = p->D; in computeJD()
19818 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
19822 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
19827 Y--; in computeJD()
19831 B = 2 - A + (A/4); in computeJD()
19834 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
19835 p->validJD = 1; in computeJD()
19836 if( p->validHMS ){ in computeJD()
19837 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); in computeJD()
19838 if( p->validTZ ){ in computeJD()
19839 p->iJD -= p->tz*60000; in computeJD()
19840 p->validYMD = 0; in computeJD()
19841 p->validHMS = 0; in computeJD()
19842 p->validTZ = 0; in computeJD()
19850 ** YYYY-MM-DD HH:MM:SS.FFF
19851 ** YYYY-MM-DD HH:MM:SS
19852 ** YYYY-MM-DD HH:MM
19853 ** YYYY-MM-DD
19856 ** on success and 1 if the input string is not a well-formed
19862 if( zDate[0]=='-' ){ in parseYyyyMmDd()
19868 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
19876 p->validHMS = 0; in parseYyyyMmDd()
19880 p->validJD = 0; in parseYyyyMmDd()
19881 p->validYMD = 1; in parseYyyyMmDd()
19882 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
19883 p->M = M; in parseYyyyMmDd()
19884 p->D = D; in parseYyyyMmDd()
19885 if( p->validTZ ){ in parseYyyyMmDd()
19897 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
19898 if( p->iJD>0 ){ in setDateTimeToCurrent()
19899 p->validJD = 1; in setDateTimeToCurrent()
19910 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
19913 p->s = r; in setRawDateNumber()
19914 p->rawS = 1; in setRawDateNumber()
19916 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
19917 p->validJD = 1; in setRawDateNumber()
19927 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
19931 ** In the first form, the +/-HH:MM is always optional. The fractional
19956 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
19979 if( p->validYMD ) return; in computeYMD()
19980 if( !p->validJD ){ in computeYMD()
19981 p->Y = 2000; in computeYMD()
19982 p->M = 1; in computeYMD()
19983 p->D = 1; in computeYMD()
19984 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
19988 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
19989 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
19990 A = Z + 1 + A - (A/4); in computeYMD()
19992 C = (int)((B - 122.1)/365.25); in computeYMD()
19994 E = (int)((B-D)/30.6001); in computeYMD()
19996 p->D = B - D - X1; in computeYMD()
19997 p->M = E<14 ? E-1 : E-13; in computeYMD()
19998 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
20000 p->validYMD = 1; in computeYMD()
20008 if( p->validHMS ) return; in computeHMS()
20010 s = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
20011 p->s = s/1000.0; in computeHMS()
20012 s = (int)p->s; in computeHMS()
20013 p->s -= s; in computeHMS()
20014 p->h = s/3600; in computeHMS()
20015 s -= p->h*3600; in computeHMS()
20016 p->m = s/60; in computeHMS()
20017 p->s += s - p->m*60; in computeHMS()
20018 p->rawS = 0; in computeHMS()
20019 p->validHMS = 1; in computeHMS()
20034 p->validYMD = 0; in clearYMD_HMS_TZ()
20035 p->validHMS = 0; in clearYMD_HMS_TZ()
20036 p->validTZ = 0; in clearYMD_HMS_TZ()
20046 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
20060 ** using whatever operating-system specific localtime facility that
20062 ** non-zero on any kind of error.
20067 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
20125 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in localtimeOffset()
20143 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in localtimeOffset()
20145 sqlite3_result_error(pCtx, "local time unavailable", -1); in localtimeOffset()
20163 return y.iJD - x.iJD; in localtimeOffset()
20172 ** Where NNN is an arbitrary floating-point number and "days" can be one
20191 ** Process a modifier to a date-time stamp. The modifiers are
20232 p->iJD += localtimeOffset(p, pCtx, &rc); in parseModifier()
20242 ** Treat the current value of p->s as the number of in parseModifier()
20245 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
20246 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
20249 p->iJD = (sqlite3_int64)r; in parseModifier()
20250 p->validJD = 1; in parseModifier()
20251 p->rawS = 0; in parseModifier()
20257 if( p->tzSet==0 ){ in parseModifier()
20262 p->iJD -= c1; in parseModifier()
20264 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); in parseModifier()
20266 p->tzSet = 1; in parseModifier()
20280 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
20287 p->validTZ = 0; in parseModifier()
20288 p->validJD = 0; in parseModifier()
20290 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
20291 if( Z>n ) Z -= 7; in parseModifier()
20292 p->iJD += (n - Z)*86400000; in parseModifier()
20306 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
20309 p->validHMS = 1; in parseModifier()
20310 p->h = p->m = 0; in parseModifier()
20311 p->s = 0.0; in parseModifier()
20312 p->rawS = 0; in parseModifier()
20313 p->validTZ = 0; in parseModifier()
20314 p->validJD = 0; in parseModifier()
20316 p->D = 1; in parseModifier()
20319 p->M = 1; in parseModifier()
20320 p->D = 1; in parseModifier()
20328 case '-': in parseModifier()
20347 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
20359 tx.iJD -= 43200000; in parseModifier()
20361 tx.iJD -= day*86400000; in parseModifier()
20362 if( z[0]=='-' ) tx.iJD = -tx.iJD; in parseModifier()
20365 p->iJD += tx.iJD; in parseModifier()
20376 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
20379 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
20383 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
20389 p->M += (int)r; in parseModifier()
20390 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
20391 p->Y += x; in parseModifier()
20392 p->M -= x*12; in parseModifier()
20393 p->validJD = 0; in parseModifier()
20394 r -= (int)r; in parseModifier()
20400 p->Y += y; in parseModifier()
20401 p->validJD = 0; in parseModifier()
20402 r -= (int)r; in parseModifier()
20407 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); in parseModifier()
20423 ** Process time function arguments. argv[0] is a date-time stamp.
20459 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
20489 ** Return YYYY-MM-DD HH:MM:SS
20500 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d", in datetimeFunc()
20502 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in datetimeFunc()
20521 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in timeFunc()
20528 ** Return YYYY-MM-DD
20539 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); in dateFunc()
20540 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in dateFunc()
20551 ** %H hour 00-24
20552 ** %j day of year 000-366
20554 ** %m month 01-12
20555 ** %M minute 00-59
20556 ** %s seconds since 1970-01-01
20557 ** %S seconds 00-59
20558 ** %w day of week 0-6 sunday==0
20559 ** %W week of year 00-53
20560 ** %Y year 0000-9999
20577 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
20612 testcase( n==sizeof(zBuf)-1 ); in strftimeFunc()
20614 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); in strftimeFunc()
20615 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); in strftimeFunc()
20618 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ in strftimeFunc()
20653 nDay = (int)((x.iJD-y.iJD+43200000)/86400000); in strftimeFunc()
20657 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); in strftimeFunc()
20674 (i64)(x.iJD/1000 - 21086676*(i64)10000)); in strftimeFunc()
20692 sqlite3_result_text(context, z, -1, in strftimeFunc()
20741 ** If the library is compiled to omit the full-scale date and time
20747 ** This function uses the C-library functions time(), gmtime()
20749 ** as the user-data for the function.
20768 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
20779 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
20792 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
20793 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
20794 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
20795 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
20796 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
20802 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
20803 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
20835 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
20853 ** from within OsOpen()), but some third-party implementations may.
20893 if( pId->pMethods ){ in sqlite3OsClose()
20894 pId->pMethods->xClose(pId); in sqlite3OsClose()
20895 pId->pMethods = 0; in sqlite3OsClose()
20900 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
20904 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
20907 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
20911 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
20915 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
20919 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
20922 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
20926 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
20943 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM in sqlite3OsFileControl()
20953 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
20956 (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
20960 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
20964 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
20968 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
20971 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
20974 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
20984 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
20992 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
20995 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
20998 /* No-op stubs to use when memory-mapped I/O is disabled */
21025 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut); in sqlite3OsOpen()
21026 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
21032 return pVfs->xDelete(pVfs, zPath, dirSync); in sqlite3OsDelete()
21041 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
21051 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
21055 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
21058 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
21061 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
21064 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
21068 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
21071 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
21074 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
21078 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
21084 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
21085 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
21088 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
21103 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
21158 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
21160 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
21172 /* No-op */ in vfsUnlink()
21174 vfsList = pVfs->pNext; in vfsUnlink()
21177 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
21178 p = p->pNext; in vfsUnlink()
21180 if( p->pNext==pVfs ){ in vfsUnlink()
21181 p->pNext = pVfs->pNext; in vfsUnlink()
21205 pVfs->pNext = vfsList; in sqlite3_vfs_register()
21208 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
21209 vfsList->pNext = pVfs; in sqlite3_vfs_register()
21248 ** Most malloc failures are non-benign. After they occur, SQLite
21272 ** we have to locate the state vector at run-time. In the more common
21302 ** indicates that subsequent malloc failures are non-benign.
21333 ** This file contains a no-op memory allocation drivers for use when
21343 ** used when no other memory allocator is specified using compile-time
21349 ** No-op versions of all memory allocation routines
21362 ** Populate the low-level memory allocation function pointers in
21395 ** This file contains low-level memory allocation drivers for when
21396 ** SQLite will use the standard C-library malloc/realloc/free interface
21399 ** This file contains implementations of the low-level memory allocation
21407 ** C-preprocessor macro summary:
21414 ** a different name, using a separate -D
21430 ** used when no other memory allocator is specified using compile-time
21450 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
21455 ** Use standard C library malloc and free on non-Apple systems.
21472 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
21506 ** For this low-level routine, we are guaranteed that nByte>0 because
21540 ** For this low-level routine, we already know that pPrior!=0 since
21542 ** by higher-level routines.
21550 p--; in sqlite3MemFree()
21567 p--; in sqlite3MemSize()
21576 ** For this low-level interface, we know that pPrior!=0. Cases where
21577 ** pPrior==0 while have been intercepted by higher-level routine and
21579 ** cases where nByte<=0 will have been intercepted by higher-level
21595 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
21596 p--; in sqlite3MemRealloc()
21656 ** Populate the low-level memory allocation function pointers in
21689 ** This file contains low-level memory allocation drivers for when
21690 ** SQLite will use the standard C-library malloc/realloc/free interface
21695 ** This file contains implementations of the low-level memory allocation
21721 ** ------------------------------------------------------------------------
21723 ** ------------------------------------------------------------------------
21807 if( i>NCSIZE-1 ){ in adjustStats()
21808 i = NCSIZE - 1; in adjustStats()
21817 mem.nCurrent[i]--; in adjustStats()
21835 p--; in sqlite3MemsysGetHeader()
21836 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
21837 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
21845 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
21858 return (int)pHdr->iSize; in sqlite3MemSize()
21891 ** Fill a buffer with pseudo-random bytes. This is used to preset
21900 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
21905 nByte -= 4; in randomFill()
21907 while( nByte-- > 0 ){ in randomFill()
21908 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
21936 pHdr->pNext = 0; in sqlite3MemMalloc()
21937 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
21939 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
21944 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
21945 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
21946 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
21947 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
21950 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
21951 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
21954 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
21957 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
21962 pHdr->iSize = nByte; in sqlite3MemMalloc()
21967 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
21985 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
21987 if( pHdr->pPrev ){ in sqlite3MemFree()
21988 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
21989 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
21992 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
21994 if( pHdr->pNext ){ in sqlite3MemFree()
21995 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
21996 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
21999 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
22002 z -= pHdr->nTitle; in sqlite3MemFree()
22003 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
22004 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
22005 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
22023 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
22027 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
22028 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
22029 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
22037 ** Populate the low-level memory allocation function pointers in
22061 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
22062 pHdr->eType = eType; in sqlite3MemdebugSetType()
22080 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
22081 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
22102 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
22103 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
22132 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
22141 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
22143 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
22144 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
22163 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
22165 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
22167 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
22168 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
22171 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
22172 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
22177 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
22183 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
22185 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
22186 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
22238 ** mean that the library will use a memory-pool by default, just that
22279 ** two fields form a double-linked list of chunks of related sizes.
22316 ** True if we are evaluating an out-of-memory callback.
22344 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
22377 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
22379 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
22380 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
22383 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
22412 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
22413 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
22414 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
22417 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
22456 ** size parameters for check-out and return a pointer to the
22463 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
22464 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
22465 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
22466 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
22467 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
22468 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
22480 if( nBlock>=mem3.szMaster-1 ){ in memsys3FromMaster()
22490 newi = mem3.iMaster + mem3.szMaster - nBlock; in memsys3FromMaster()
22492 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock; in memsys3FromMaster()
22493 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2; in memsys3FromMaster()
22494 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromMaster()
22495 mem3.szMaster -= nBlock; in memsys3FromMaster()
22496 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster; in memsys3FromMaster()
22497 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FromMaster()
22498 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FromMaster()
22528 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
22532 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
22533 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
22538 size = i + size/4 - prev; in memsys3Merge()
22539 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
22540 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
22541 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
22581 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
22583 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
22589 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
22622 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
22649 i = p - mem3.aPool; in memsys3FreeUnsafe()
22650 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
22651 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
22653 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
22654 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
22655 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
22660 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
22661 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize; in memsys3FreeUnsafe()
22662 mem3.iMaster -= size; in memsys3FreeUnsafe()
22665 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
22666 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FreeUnsafe()
22667 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; in memsys3FreeUnsafe()
22669 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
22670 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
22672 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
22673 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FreeUnsafe()
22674 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; in memsys3FreeUnsafe()
22681 ** size returned omits the 8-byte header overhead. This only
22688 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
22689 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
22699 return ((n+11)&~7) - 4; in memsys3Roundup()
22739 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
22768 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
22814 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
22820 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
22825 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
22831 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
22833 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
22837 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
22842 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
22851 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
22856 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8); in sqlite3Memsys3Dump()
22857 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8); in sqlite3Memsys3Dump()
22873 ** Populate the low-level memory allocation function pointers in
22934 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
22943 ** N >= M*(1 + log2(n)/2) - n + 1
22972 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
23007 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
23038 ** structures, return a pointer to the idx-th such link.
23052 next = MEM5LINK(i)->next; in memsys5Unlink()
23053 prev = MEM5LINK(i)->prev; in memsys5Unlink()
23057 MEM5LINK(prev)->next = next; in memsys5Unlink()
23060 MEM5LINK(next)->prev = prev; in memsys5Unlink()
23075 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
23076 MEM5LINK(i)->prev = -1; in memsys5Link()
23079 MEM5LINK(x)->prev = i; in memsys5Link()
23101 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
23156 iBin--; in memsys5MallocUnsafe()
23167 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
23194 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
23196 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
23198 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
23203 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
23206 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
23211 mem5.currentCount--; in memsys5FreeUnsafe()
23212 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
23221 iBuddy = iBlock - size; in memsys5FreeUnsafe()
23283 ** memsys5Round(). Hence nBytes is always a non-negative power
23292 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
23315 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
23328 ** Examples: memsys5Log(1) -> 0
23329 ** memsys5Log(2) -> 1
23330 ** memsys5Log(4) -> 2
23331 ** memsys5Log(5) -> 3
23332 ** memsys5Log(8) -> 3
23333 ** memsys5Log(9) -> 4
23337 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
23362 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
23380 mem5.aiFreelist[ii] = -1; in memsys5Init()
23384 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
23434 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
23525 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
23536 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
23546 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
23549 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
23580 p->iType = iType; in checkMutexAlloc()
23583 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
23588 p = &staticMutexes[iType-2]; in checkMutexAlloc()
23591 if( p->mutex==0 ){ in checkMutexAlloc()
23592 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
23593 if( p->mutex==0 ){ in checkMutexAlloc()
23613 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
23617 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
23632 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
23633 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
23637 "illegal multi-threaded access to database connection" in checkMutexEnter()
23640 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
23648 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
23656 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
23686 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
23687 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
23715 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
23716 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
23717 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
23718 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
23719 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
23720 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
23721 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
23722 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
23724 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
23812 ** this function is a no-op.
23857 ** here are place-holders. Applications can substitute working
23858 ** mutex routines at start-time using the
23931 return p==0 || p->cnt>0; in debugMutexHeld()
23935 return p==0 || p->cnt==0; in debugMutexNotheld()
23950 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
23957 pNew->id = id; in debugMutexAlloc()
23958 pNew->cnt = 0; in debugMutexAlloc()
23964 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
23969 pNew = &aStatic[id-2]; in debugMutexAlloc()
23970 pNew->id = id; in debugMutexAlloc()
23982 assert( p->cnt==0 ); in debugMutexFree()
23983 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
24005 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
24006 p->cnt++; in debugMutexEnter()
24010 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
24011 p->cnt++; in debugMutexTry()
24024 p->cnt--; in debugMutexLeave()
24025 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
24047 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
24048 ** is used regardless of the run-time threadsafety setting.
24088 ** home-grown mutexes. Encapsulate these conditions into a single #define.
24130 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
24136 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
24139 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
24192 ** cases where it really needs one. If a faster non-recursive mutex
24233 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
24239 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
24248 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
24254 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
24259 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
24264 if( p ) p->id = iType; in pthreadMutexAlloc()
24276 assert( p->nRef==0 ); in pthreadMutexFree()
24278 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
24281 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
24303 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
24308 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
24309 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
24311 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
24318 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
24319 p->nRef++; in pthreadMutexEnter()
24321 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
24322 assert( p->nRef==0 ); in pthreadMutexEnter()
24323 p->owner = self; in pthreadMutexEnter()
24324 p->nRef = 1; in pthreadMutexEnter()
24328 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
24330 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
24332 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
24333 p->owner = pthread_self(); in pthreadMutexEnter()
24334 p->nRef++; in pthreadMutexEnter()
24339 if( p->trace ){ in pthreadMutexEnter()
24340 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
24346 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
24351 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
24352 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
24354 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
24361 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
24362 p->nRef++; in pthreadMutexTry()
24364 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
24365 assert( p->nRef==0 ); in pthreadMutexTry()
24366 p->owner = self; in pthreadMutexTry()
24367 p->nRef = 1; in pthreadMutexTry()
24374 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
24376 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
24378 p->owner = pthread_self(); in pthreadMutexTry()
24379 p->nRef++; in pthreadMutexTry()
24388 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
24389 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
24404 p->nRef--; in pthreadMutexLeave()
24405 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
24407 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
24410 if( p->nRef==0 ){ in pthreadMutexLeave()
24411 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
24414 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
24418 if( p->trace ){ in pthreadMutexLeave()
24419 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
24483 ** all of the platform-specific files (os_*.c) and is #included into those
24495 ** switch. The following code should catch this problem at compile-time.
24509 ** high-performance timing routines.
24525 ** This file contains inline asm code for retrieving "high-performance"
24532 ** The following routine only works on pentium-class (or newer) processors.
24534 ** processor and returns that value. This can be used for high-res
24605 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
24629 || sqlite3_io_error_pending-- == 1 ) \
24644 sqlite3_diskfull_pending--; \
24722 ** Determine if we are dealing with Windows CE - which has a much reduced
24750 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
24805 return p->nRef!=0 && p->owner==GetCurrentThreadId();
24809 return p->nRef==0 || p->owner!=tid;
24854 static int winMutex_isNt = -1; /* <0 means "need to query" */
24932 ** cases where it really needs one. If a faster non-recursive mutex
24958 p->id = iType;
24961 p->trace = 1;
24965 InitializeCriticalSectionEx(&p->mutex, 0, 0);
24967 InitializeCriticalSection(&p->mutex);
24974 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
24979 p = &winMutex_staticMutexes[iType-2];
24980 p->id = iType;
24983 p->trace = 1;
25000 assert( p->nRef==0 && p->owner==0 );
25001 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
25002 DeleteCriticalSection(&p->mutex);
25028 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
25033 EnterCriticalSection(&p->mutex);
25035 assert( p->nRef>0 || p->owner==0 );
25036 p->owner = tid;
25037 p->nRef++;
25038 if( p->trace ){
25039 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
25040 tid, p->id, p, p->trace, p->nRef));
25051 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
25065 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
25070 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
25072 p->owner = tid;
25073 p->nRef++;
25081 if( p->trace ){
25082 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
25083 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
25101 assert( p->nRef>0 );
25102 assert( p->owner==tid );
25103 p->nRef--;
25104 if( p->nRef==0 ) p->owner = 0;
25105 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
25108 LeaveCriticalSection(&p->mutex);
25110 if( p->trace ){
25111 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
25112 tid, p->id, p, p->trace, p->nRef));
25159 ** Attempt to release up to n bytes of non-essential memory currently
25160 ** held by SQLite. An example of non-essential memory is memory used to
25167 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
25168 ** is a no-op returning zero if SQLite is not compiled with
25202 ** no-op.
25217 ** Set the soft heap-size limit for the library. Passing a zero or
25226 if( rc ) return -1;
25238 excess = sqlite3_memory_used() - n;
25268 ** Return true if the heap is currently under memory pressure - in other
25326 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
25328 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
25343 if( nUsed >= mem0.alarmThreshold - nFull ){
25385 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
25412 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
25440 assert( sqlite3_mutex_held(db->mutex) );
25441 return db->lookaside.sz;
25454 if( p==0 ) return; /* IMP: R-49053-54554 */
25470 ** *db->pnBytesFreed.
25473 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
25478 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
25479 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
25482 assert( db==0 || sqlite3_mutex_held(db->mutex) );
25485 if( db->pnBytesFreed ){
25493 memset(p, 0xaa, db->lookaside.sz);
25495 pBuf->pNext = db->lookaside.pFree;
25496 db->lookaside.pFree = pBuf;
25507 assert( db==0 || sqlite3_mutex_held(db->mutex) );
25520 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
25523 sqlite3_free(pOld); /* IMP: R-26507-47431 */
25531 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
25540 nDiff = nNew - nOld;
25542 mem0.alarmThreshold-nDiff ){
25552 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
25558 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
25570 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
25614 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
25623 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
25651 assert( sqlite3_mutex_held(db->mutex) );
25652 assert( db->pnBytesFreed==0 );
25653 if( db->lookaside.bDisable==0 ){
25654 assert( db->mallocFailed==0 );
25655 if( n>db->lookaside.sz ){
25656 db->lookaside.anStat[1]++;
25657 }else if( (pBuf = db->lookaside.pFree)!=0 ){
25658 db->lookaside.pFree = pBuf->pNext;
25659 db->lookaside.anStat[0]++;
25661 }else if( (pBuf = db->lookaside.pInit)!=0 ){
25662 db->lookaside.pInit = pBuf->pNext;
25663 db->lookaside.anStat[0]++;
25666 db->lookaside.anStat[2]++;
25668 }else if( db->mallocFailed ){
25673 assert( sqlite3_mutex_held(db->mutex) );
25674 assert( db->pnBytesFreed==0 );
25675 if( db->mallocFailed ){
25692 assert( sqlite3_mutex_held(db->mutex) );
25693 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
25700 if( db->mallocFailed==0 ){
25704 memcpy(pNew, p, db->lookaside.sz);
25716 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
25778 n = (int)(zEnd - zStart);
25779 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
25792 ** Call this routine to record the fact that an OOM (out-of-memory) error
25793 ** has happened. This routine will set db->mallocFailed, and also
25798 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
25799 db->mallocFailed = 1;
25800 if( db->nVdbeExec>0 ){
25801 db->u1.isInterrupted = 1;
25803 db->lookaside.bDisable++;
25809 ** db->mallocFailed flag as necessary.
25815 if( db->mallocFailed && db->nVdbeExec==0 ){
25816 db->mallocFailed = 0;
25817 db->u1.isInterrupted = 0;
25818 assert( db->lookaside.bDisable>0 );
25819 db->lookaside.bDisable--;
25841 ** If an OOM as occurred, then the connection error-code (the value
25846 ** Otherwise the read (and possible write) of db->mallocFailed
25850 assert( sqlite3_mutex_held(db->mutex) );
25851 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
25854 return rc & db->errMask;
25865 ** This file contains code for a set of "printf"-like routines. These
25876 #define etRADIX 0 /* non-decimal integer types. %x %o */
25892 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
25893 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
25900 ** An "etByte" is an 8-bit unsigned value.
25929 static const char aPrefix[] = "-x0\000X0";
25975 ** 16 (the number of significant digits in a 64-bit float) '0' is
25982 (*cnt)--;
25986 *val = (*val - d)*10.0;
25996 p->accError = eError;
25997 p->nAlloc = 0;
26004 if( p->nArg<=p->nUsed ) return 0;
26005 return sqlite3_value_int64(p->apArg[p->nUsed++]);
26008 if( p->nArg<=p->nUsed ) return 0.0;
26009 return sqlite3_value_double(p->apArg[p->nUsed++]);
26012 if( p->nArg<=p->nUsed ) return 0;
26013 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
26040 etByte flag_leftjustify; /* True if "-" flag is present */
26050 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
26068 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
26082 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
26095 case '-': flag_leftjustify = 1; break;
26114 width = width >= -2147483647 ? -width : 0;
26120 wx = wx*10 + c - '0';
26144 precision = precision >= -2147483647 ? -precision : -1;
26149 px = px*10 + c - '0';
26156 precision = -1;
26158 assert( precision>=(-1) );
26183 xtype = infop->type;
26194 ** flag_leftjustify TRUE if a '-' is present or if the
26199 ** always non-negative. Zero is the default.
26201 ** is -1.
26215 if( infop->flags & FLAG_SIGNED ){
26232 longvalue = -v;
26234 prefix = '-';
26254 if( flag_zeropad && precision<width-(prefix!=0) ){
26255 precision = width-(prefix!=0);
26257 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
26269 bufpt = &zOut[nOut-1];
26276 *(--bufpt) = zOrd[x*2+1];
26277 *(--bufpt) = zOrd[x*2];
26280 const char *cset = &aDigits[infop->charset];
26281 u8 base = infop->base;
26283 *(--bufpt) = cset[longvalue%base];
26287 length = (int)(&zOut[nOut-1]-bufpt);
26289 *(--bufpt) = '0'; /* Zero pad */
26293 int nn = (length - 1)/3; /* Number of "," to insert */
26294 int ix = (length - 1)%3 + 1;
26295 bufpt -= nn;
26298 ix--;
26301 nn--;
26306 if( prefix ) *(--bufpt) = prefix; /* Add sign */
26307 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
26310 pre = &aPrefix[infop->prefix];
26311 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
26313 length = (int)(&zOut[nOut-1]-bufpt);
26328 realvalue = -realvalue;
26329 prefix = '-';
26333 if( xtype==etGENERIC && precision>0 ) precision--;
26335 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
26350 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
26351 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
26371 if( exp<-4 || exp>precision ){
26374 precision = precision - exp;
26385 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
26404 for(; e2>=0; e2--){
26414 for(e2++; e2<0; precision--, e2++){
26419 while( (precision--)>0 ){
26424 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
26426 if( bufpt[-1]=='.' ){
26430 *(--bufpt) = 0;
26436 *(bufpt++) = aDigits[infop->charset];
26438 *(bufpt++) = '-'; exp = -exp;
26454 length = (int)(bufpt-zOut);
26461 int nPad = width - length;
26462 for(i=width; i>=nPad; i--){
26463 bufpt[i] = bufpt[i-nPad];
26466 while( nPad-- ) bufpt[i++] = '0';
26473 *(va_arg(ap,int*)) = pAccum->nChar;
26490 width -= precision-1;
26492 sqlite3AppendChar(pAccum, width-1, ' ');
26495 sqlite3AppendChar(pAccum, precision-1, c);
26537 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
26568 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26571 if( pToken && pToken->n ){
26572 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
26581 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26584 pItem = &pSrc->a[k];
26586 assert( k>=0 && k<pSrc->nSrc );
26587 if( pItem->zDatabase ){
26588 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
26591 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
26605 width -= length;
26615 sqlite3DbFree(pAccum->db, zExtra);
26630 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
26631 if( p->accError ){
26632 testcase(p->accError==STRACCUM_TOOBIG);
26633 testcase(p->accError==STRACCUM_NOMEM);
26636 if( p->mxAlloc==0 ){
26637 N = p->nAlloc - p->nChar - 1;
26641 char *zOld = isMalloced(p) ? p->zText : 0;
26642 i64 szNew = p->nChar;
26644 if( szNew+p->nChar<=p->mxAlloc ){
26647 szNew += p->nChar;
26649 if( szNew > p->mxAlloc ){
26654 p->nAlloc = (int)szNew;
26656 if( p->db ){
26657 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
26659 zNew = sqlite3_realloc64(zOld, p->nAlloc);
26662 assert( p->zText!=0 || p->nChar==0 );
26663 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
26664 p->zText = zNew;
26665 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
26666 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
26680 testcase( p->nChar + (i64)N > 0x7fffffff );
26681 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
26684 while( (N--)>0 ) p->zText[p->nChar++] = c;
26691 ** This is a helper routine to sqlite3StrAccumAppend() that does special-case
26698 memcpy(&p->zText[p->nChar], z, N);
26699 p->nChar += N;
26709 assert( p->zText!=0 || p->nChar==0 || p->accError );
26711 assert( p->accError==0 || p->nAlloc==0 );
26712 if( p->nChar+N >= p->nAlloc ){
26715 assert( p->zText );
26716 p->nChar += N;
26717 memcpy(&p->zText[p->nChar-N], z, N);
26722 ** Append the complete text of zero-terminated string z[] to the p string.
26730 ** Finish off a string by making sure it is zero-terminated.
26736 assert( p->mxAlloc>0 && !isMalloced(p) );
26737 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
26739 memcpy(zText, p->zText, p->nChar+1);
26740 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
26744 p->zText = zText;
26748 if( p->zText ){
26749 p->zText[p->nChar] = 0;
26750 if( p->mxAlloc>0 && !isMalloced(p) ){
26754 return p->zText;
26762 sqlite3DbFree(p->db, p->zText);
26763 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
26765 p->zText = 0;
26773 ** memory is used if not NULL. db->mallocFailed is set appropriately
26783 p->zText = zBase;
26784 p->db = db;
26785 p->nAlloc = n;
26786 p->mxAlloc = mx;
26787 p->nChar = 0;
26788 p->accError = 0;
26789 p->printfFlags = 0;
26794 ** %-conversion extensions.
26802 db->aLimit[SQLITE_LIMIT_LENGTH]);
26814 ** %-conversion extensions.
26827 ** %-conversion extensions.
26851 ** %-conversion extensions.
26905 ** stack space on small-stack systems when logging is disabled.
26967 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
26980 ** 2015-06-08
27011 p->iLevel++;
27014 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
27023 p->iLevel--;
27024 if( p->iLevel<0 ) sqlite3_free(p);
27038 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
27039 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
27041 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
27047 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
27062 ** Generate a human-readable description of a WITH clause.
27067 if( pWith->nCte==0 ) return;
27068 if( pWith->pOuter ){
27069 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
27073 if( pWith->nCte>0 ){
27075 for(i=0; i<pWith->nCte; i++){
27078 const struct Cte *pCte = &pWith->a[i];
27080 sqlite3XPrintf(&x, "%s", pCte->zName);
27081 if( pCte->pCols && pCte->pCols->nExpr>0 ){
27084 for(j=0; j<pCte->pCols->nExpr; j++){
27085 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
27092 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
27093 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
27102 ** Generate a human-readable description of a Select object.
27108 sqlite3TreeViewLine(pView, "nil-SELECT");
27112 if( p->pWith ){
27113 sqlite3TreeViewWith(pView, p->pWith, 1);
27119 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
27120 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
27121 (int)p->nSelectRow
27124 if( p->pPrior ){
27128 if( p->pSrc && p->pSrc->nSrc ) n++;
27129 if( p->pWhere ) n++;
27130 if( p->pGroupBy ) n++;
27131 if( p->pHaving ) n++;
27132 if( p->pOrderBy ) n++;
27133 if( p->pLimit ) n++;
27135 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
27136 if( p->pSrc && p->pSrc->nSrc ){
27138 pView = sqlite3TreeViewPush(pView, (n--)>0);
27140 for(i=0; i<p->pSrc->nSrc; i++){
27141 struct SrcList_item *pItem = &p->pSrc->a[i];
27145 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
27146 if( pItem->zDatabase ){
27147 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
27148 }else if( pItem->zName ){
27149 sqlite3XPrintf(&x, " %s", pItem->zName);
27151 if( pItem->pTab ){
27152 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
27154 if( pItem->zAlias ){
27155 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
27157 if( pItem->fg.jointype & JT_LEFT ){
27158 sqlite3XPrintf(&x, " LEFT-JOIN");
27161 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
27162 if( pItem->pSelect ){
27163 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
27165 if( pItem->fg.isTabFunc ){
27166 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
27172 if( p->pWhere ){
27173 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
27174 sqlite3TreeViewExpr(pView, p->pWhere, 0);
27177 if( p->pGroupBy ){
27178 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
27180 if( p->pHaving ){
27181 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
27182 sqlite3TreeViewExpr(pView, p->pHaving, 0);
27185 if( p->pOrderBy ){
27186 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
27188 if( p->pLimit ){
27189 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
27190 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
27191 if( p->pLimit->pRight ){
27192 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
27193 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
27198 if( p->pPrior ){
27200 switch( p->op ){
27207 p = p->pPrior;
27213 ** Generate a human-readable explanation of an expression tree.
27225 if( pExpr->flags ){
27228 pExpr->flags, pExpr->iRightJoinTable);
27230 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
27235 switch( pExpr->op ){
27238 pExpr->iTable, pExpr->iColumn, zFlgs);
27242 if( pExpr->iTable<0 ){
27244 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
27247 pExpr->iTable, pExpr->iColumn, zFlgs);
27252 if( pExpr->flags & EP_IntValue ){
27253 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
27255 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
27261 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
27266 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
27275 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
27281 pExpr->u.zToken, pExpr->iColumn);
27285 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
27289 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
27295 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
27296 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27330 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
27331 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27336 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
27337 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27347 pFarg = pExpr->x.pList;
27349 if( pExpr->op==TK_AGG_FUNCTION ){
27351 pExpr->op2, pExpr->u.zToken);
27353 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
27362 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
27363 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27367 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
27368 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27372 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
27373 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27375 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27377 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
27390 ** X is stored in pExpr->pLeft.
27391 ** Y is stored in pExpr->pList->a[0].pExpr.
27392 ** Z is stored in pExpr->pList->a[1].pExpr.
27395 Expr *pX = pExpr->pLeft;
27396 Expr *pY = pExpr->x.pList->a[0].pExpr;
27397 Expr *pZ = pExpr->x.pList->a[1].pExpr;
27406 ** to a column in the new.* or old.* pseudo-tables available to
27408 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
27409 ** is set to the column of the pseudo-table to read, or to -1 to
27413 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
27418 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27419 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
27425 switch( pExpr->affinity ){
27431 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
27437 pExpr->iTable, pExpr->iColumn, zFlgs);
27438 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
27442 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
27446 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
27447 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
27451 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
27452 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27456 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
27462 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27463 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
27466 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27473 ** Generate a human-readable explanation of an expression list.
27486 for(i=0; i<pList->nExpr; i++){
27487 int j = pList->a[i].u.x.iOrderByCol;
27488 char *zName = pList->a[i].zName;
27498 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
27531 ** This file contains code to implement a pseudo-random number
27556 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
27558 ** we have to locate the state vector at run-time. In the more common
27594 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
27624 }while( --N );
27669 ** This file presents a simple cross-platform threading interface for
27682 ** single-threaded if desired.
27694 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
27717 /* This routine is never used in single-threaded mode */
27724 p->xTask = xTask;
27725 p->pIn = pIn;
27733 rc = pthread_create(&p->tid, 0, xTask, pIn);
27736 p->done = 1;
27737 p->pOut = xTask(pIn);
27749 if( p->done ){
27750 *ppOut = p->pOut;
27753 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
27766 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
27792 assert( p->id==GetCurrentThreadId() );
27794 assert( p->xTask!=0 );
27795 p->pResult = p->xTask(p->pIn);
27822 p->xTask = xTask;
27823 p->pIn = pIn;
27824 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
27825 if( p->tid==0 ){
27829 if( p->xTask==0 ){
27830 p->id = GetCurrentThreadId();
27831 p->pResult = xTask(pIn);
27846 if( p->xTask==0 ){
27847 /* assert( p->id==GetCurrentThreadId() ); */
27849 assert( p->tid==0 );
27851 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
27852 rc = sqlite3Win32Wait((HANDLE)p->tid);
27854 bRc = CloseHandle((HANDLE)p->tid);
27857 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
27866 /********************************* Single-Threaded **************************/
27895 p->xTask = xTask;
27896 p->pIn = pIn;
27898 p->xTask = 0;
27899 p->pResult = xTask(pIn);
27910 if( p->xTask ){
27911 *ppOut = p->xTask(p->pIn);
27913 *ppOut = p->pResult;
27929 /****************************** End Single-Threaded *************************/
27945 ** This file contains routines used to translate between UTF-8,
27946 ** UTF-16, UTF-16BE, and UTF-16LE.
27948 ** Notes on UTF-8:
27950 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
27957 ** Notes on UTF-16: (with wwww+1==uuuuu)
27959 ** Word-0 Word-1 Value
27965 ** 0xff 0xfe little-endian utf-16 follows
27966 ** 0xfe 0xff big-endian utf-16 follows
27983 ** a multi-byte UTF8 character.
28022 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
28023 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
28034 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
28035 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
28062 ** Translate a single UTF-8 character. Return the unicode value.
28069 ** Notes On Invalid UTF-8:
28071 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
28072 ** be encoded as a multi-byte character. Any multi-byte character that
28076 ** If a multi-byte character attempts to encode a value between
28080 ** byte of a character are interpreted as single-byte characters
28084 ** * This routine accepts over-length UTF8 encodings
28085 ** for unicode values 0x80 and greater. It does not change over-length
28091 c = sqlite3Utf8Trans1[c-0xc0]; \
28105 ** For this routine, we assume the UTF8 string is always zero-terminated.
28109 c = sqlite3Utf8Trans1[c-0xc0];
28143 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
28144 assert( pMem->flags&MEM_Str );
28145 assert( pMem->enc!=desiredEnc );
28146 assert( pMem->enc!=0 );
28147 assert( pMem->n>=0 );
28157 /* If the translation is between UTF-16 little and big endian, then
28161 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
28169 zIn = (u8*)pMem->z;
28170 zTerm = &zIn[pMem->n&~1];
28177 pMem->enc = desiredEnc;
28183 /* When converting from UTF-16, the maximum growth results from
28184 ** translating a 2-byte character to a 4-byte UTF-8 character.
28186 ** nul-terminator.
28188 pMem->n &= ~1;
28189 len = pMem->n * 2 + 1;
28191 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
28192 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
28194 ** nul-terminator.
28196 len = pMem->n * 2 + 2;
28205 zIn = (u8*)pMem->z;
28206 zTerm = &zIn[pMem->n];
28207 zOut = sqlite3DbMallocRaw(pMem->db, len);
28213 if( pMem->enc==SQLITE_UTF8 ){
28215 /* UTF-8 -> UTF-16 Little-endian */
28222 /* UTF-8 -> UTF-16 Big-endian */
28228 pMem->n = (int)(z - zOut);
28232 if( pMem->enc==SQLITE_UTF16LE ){
28233 /* UTF-16 Little-endian -> UTF-8 */
28239 /* UTF-16 Big-endian -> UTF-8 */
28245 pMem->n = (int)(z - zOut);
28248 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
28250 c = pMem->flags;
28252 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
28253 pMem->enc = desiredEnc;
28254 pMem->z = (char*)zOut;
28255 pMem->zMalloc = pMem->z;
28256 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
28272 ** This routine checks for a byte-order mark at the beginning of the
28273 ** UTF-16 string stored in *pMem. If one is present, it is removed and
28275 ** byte-swapping, it just sets Mem.enc appropriately.
28284 assert( pMem->n>=0 );
28285 if( pMem->n>1 ){
28286 u8 b1 = *(u8 *)pMem->z;
28287 u8 b2 = *(((u8 *)pMem->z) + 1);
28299 pMem->n -= 2;
28300 memmove(pMem->z, &pMem->z[2], pMem->n);
28301 pMem->z[pMem->n] = '\0';
28302 pMem->z[pMem->n+1] = '\0';
28303 pMem->flags |= MEM_Term;
28304 pMem->enc = bom;
28312 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
28325 zTerm = (const u8*)(-1);
28335 /* This test function is not currently used by the automated test-suite.
28340 ** Translate UTF-8 to UTF-8.
28342 ** This has the effect of making sure that the string is well-formed
28343 ** UTF-8. Miscoded characters are removed.
28345 ** The translation is done in-place and aborted if the output
28360 return (int)(zOut - zStart);
28366 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
28367 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
28378 if( db->mallocFailed ){
28382 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
28383 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
28384 assert( m.z || db->mallocFailed );
28389 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
28391 ** in pZ. nChar must be non-negative.
28409 return (int)(z-(unsigned char const *)zIn);
28428 n = (int)(z-zBuf);
28437 assert( (z-zBuf)==n );
28443 n = (int)(z-zBuf);
28449 assert( (z-zBuf)==n );
28455 n = (int)(z-zBuf);
28461 assert( (z-zBuf)==n );
28532 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
28536 ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
28537 ** On the other hand, the use of -ffast-math comes with the following
28540 ** This option [-ffast-math] should never be turned on by any
28541 ** -O option since it can result in incorrect output for programs
28545 ** Under MSVC, this NaN test may fail if compiled with a floating-
28554 # error SQLite will not work correctly with the -ffast-math option of GCC.
28569 ** lower 30 bits of a 32-bit signed integer.
28584 ** The column type is an extra string stored after the zero-terminator on
28588 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
28589 return pCol->zName + strlen(pCol->zName) + 1;
28593 ** Helper function for sqlite3Error() - called rarely. Broken out into
28598 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
28609 db->errCode = err_code;
28610 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
28621 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
28640 ** encoded in UTF-8.
28648 db->errCode = err_code;
28652 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
28658 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
28663 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
28682 sqlite3 *db = pParse->db;
28686 if( db->suppressErr ){
28689 pParse->nErr++;
28690 sqlite3DbFree(db, pParse->zErrMsg);
28691 pParse->zErrMsg = zMsg;
28692 pParse->rc = SQLITE_ERROR;
28697 ** Convert an SQL-style quoted string into a normal string by removing
28698 ** the quote characters. The conversion is done in-place. If the
28700 ** is a no-op.
28702 ** The input string must be zero-terminated. A new zero-terminator
28705 ** The return value is -1 if no dequoting occurs or the length of the
28709 ** 2002-Feb-14: This routine is extended to remove MS-Access style
28710 ** brackets from around identifiers. For example: "[a-b-c]" becomes
28711 ** "a-b-c".
28740 p->z = z;
28741 p->n = sqlite3Strlen30(z);
28744 /* Convenient short-hand */
28751 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
28753 ** the contents of two buffers containing UTF-8 strings in a
28754 ** case-independent fashion, using the same definition of "case
28759 return zRight ? -1 : 0;
28771 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
28781 return zRight ? -1 : 0;
28787 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
28788 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
28792 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
28835 ** uses the encoding enc. The string is not necessarily zero-terminated.
28841 ** [+-]digits[E[+-]digits]
28842 ** [+-]digits.[digits][E[+-]digits]
28843 ** [+-].digits[E[+-]digits]
28862 int eValid = 1; /* True exponent is either not used or is well-formed */
28865 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
28876 for(i=3-enc; i<length && z[i]==0; i+=2){}
28887 if( *z=='-' ){
28888 sign = -1;
28895 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
28896 s = s*10 + (*z - '0');
28900 /* skip non-significant significand digits
28911 if( s<((LARGEST_INT64-9)/10) ){
28912 s = s*10 + (*z - '0');
28913 d--;
28928 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
28931 if( *z=='-' ){
28932 esign = -1;
28939 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
28952 esign = -1;
28953 e *= -1;
28960 result = sign<0 ? -(double)0 : (double)0;
28968 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
28970 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
28973 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
28976 e--;
28980 s = sign<0 ? -s : s;
28982 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
28986 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
28987 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
28988 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
29021 /* return true if number and no extra non-whitespace chracters after */
29029 ** Compare the 19-character string zNum against the text representation
29040 ** will return -8.
29048 c = (zNum[i*incr]-pow63[i])*10;
29051 c = zNum[18*incr] - '8';
29052 testcase( c==(-1) );
29060 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
29065 ** 0 Successful transformation. Fits in a 64-bit signed integer.
29067 ** 2 Integer too large for a 64-bit signed integer or is malformed
29071 ** The string is not necessarily zero-terminated. The encoding is
29080 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
29090 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
29097 if( *zNum=='-' ){
29107 u = u*10 + c - '0';
29112 *pNum = -(i64)u;
29121 || nonNum /* UTF16 with high-order bytes non-zero */
29128 /* zNum is empty or contains non-numeric text or is longer
29136 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
29148 assert( u-1==LARGEST_INT64 );
29155 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
29156 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
29161 ** 0 Successful transformation. Fits in a 64-bit signed integer.
29163 ** 2 Integer too large for a 64-bit signed integer or is malformed
29178 return (z[k]==0 && k-i<=16) ? 0 : 2;
29187 ** If zNum represents an integer that will fit in 32-bits, then set
29192 ** Any non-numeric characters that following zNum are ignored.
29194 ** input number to be zero-terminated.
29200 if( zNum[0]=='-' ){
29227 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
29234 ** 2^31 -> 2147483648
29240 testcase( v-neg==2147483647 );
29241 if( v-neg>2147483647 ){
29245 v = -v;
29252 ** Return a 32-bit integer value extracted from a string. If the
29262 ** The variable-length integer encoding is as follows:
29269 ** 7 bits - A
29270 ** 14 bits - BA
29271 ** 21 bits - BBA
29272 ** 28 bits - BBBA
29273 ** 35 bits - BBBBA
29274 ** 42 bits - BBBBBA
29275 ** 49 bits - BBBBBBA
29276 ** 56 bits - BBBBBBBA
29277 ** 64 bits - BBBBBBBBC
29281 ** Write a 64-bit variable-length integer to memory starting at p[0].
29285 ** A variable-length integer consists of the lower 7 bits of each byte
29296 for(i=7; i>=0; i--){
29309 for(i=0, j=n-1; j>=0; j--, i++){
29341 ** Read a 64-bit variable-length integer from memory starting at p[0].
29494 b = p[-4];
29505 ** Read a 32-bit variable-length integer from memory starting at p[0].
29508 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
29512 ** single-byte case. All code should use the MACRO version as
29513 ** this function assumes the single-byte case has already been handled.
29518 /* The 1-byte case. Overwhelmingly the most common. Handled inline
29531 /* The 2-byte case */
29544 /* The 3-byte case */
29559 /* A 32-bit varint is used to store size information in btrees.
29560 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
29561 ** A 3-byte varint is sufficient, for example, to record the size
29562 ** of a 1048569-byte BLOB or string.
29564 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
29565 ** rare larger cases can be handled by the slower 64-bit varint
29573 p -= 2;
29586 ** unrolling for the 3- and 4-byte varint cases. This code is
29619 ** slow) general-purpose sqlite3GetVarint() routine to extract the
29625 p -= 4;
29636 ** 64-bit integer.
29646 ** Read or write a four-byte big-endian integer value.
29713 n--;
29756 magic = db->magic;
29769 magic = db->magic;
29782 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
29783 ** the other 64-bit signed integer at *pA and store the result in *pA.
29793 testcase( iB==-1 ); testcase( iB==0 );
29795 testcase( iA>0 && LARGEST_INT64 - iA == iB );
29796 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
29797 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
29799 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
29800 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
29801 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
29813 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
29815 *pA -= iB;
29818 return sqlite3AddInt64(pA, -iB);
29836 if( -iA>LARGEST_INT64/-iB ) return 1;
29845 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
29846 ** if the integer has a value of -2147483648, return +2147483647
29851 return -x;
29856 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
29862 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
29867 ** test.db-journal => test.nal
29868 ** test.db-wal => test.wal
29869 ** test.db-shm => test.shm
29870 ** test.db-mj7f3319fa => test.9fa
29879 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
29880 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
29898 5, 5, 5, /* 12-14 */
29899 4, 4, 4, 4, /* 15-18 */
29900 3, 3, 3, 3, 3, 3, /* 19-24 */
29901 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
29906 return a+x[a-b];
29910 return b+x[b-a];
29923 while( x<8 ){ y -= 10; x <<= 1; }
29926 int i = 60 - __builtin_clzll(x);
29930 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
29934 return a[x&7] + y - 10;
29949 e = (a>>52) - 1022;
29961 ** non-standard compile-time options is enabled.
29967 if( n>=5 ) n -= 2;
29968 else if( n>=1 ) n -= 1;
29977 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
29985 ** db->mallocFailed flag is set.
29998 ** is always zero-terminated.
30097 ** This is the implementation of generic hash-tables
30110 pNew->first = 0;
30111 pNew->count = 0;
30112 pNew->htsize = 0;
30113 pNew->ht = 0;
30124 elem = pH->first;
30125 pH->first = 0;
30126 sqlite3_free(pH->ht);
30127 pH->ht = 0;
30128 pH->htsize = 0;
30130 HashElem *next_elem = elem->next;
30134 pH->count = 0;
30143 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
30146 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
30164 pHead = pEntry->count ? pEntry->chain : 0;
30165 pEntry->count++;
30166 pEntry->chain = pNew;
30171 pNew->next = pHead;
30172 pNew->prev = pHead->prev;
30173 if( pHead->prev ){ pHead->prev->next = pNew; }
30174 else { pH->first = pNew; }
30175 pHead->prev = pNew;
30177 pNew->next = pH->first;
30178 if( pH->first ){ pH->first->prev = pNew; }
30179 pNew->prev = 0;
30180 pH->first = pNew;
30199 if( new_size==pH->htsize ) return 0;
30215 sqlite3_free(pH->ht);
30216 pH->ht = new_ht;
30217 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
30219 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
30220 unsigned int h = strHash(elem->pKey) % new_size;
30221 next_elem = elem->next;
30242 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
30244 h = strHash(pKey) % pH->htsize;
30245 pEntry = &pH->ht[h];
30246 elem = pEntry->chain;
30247 count = pEntry->count;
30250 elem = pH->first;
30251 count = pH->count;
30254 while( count-- ){
30256 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
30259 elem = elem->next;
30273 if( elem->prev ){
30274 elem->prev->next = elem->next;
30276 pH->first = elem->next;
30278 if( elem->next ){
30279 elem->next->prev = elem->prev;
30281 if( pH->ht ){
30282 pEntry = &pH->ht[h];
30283 if( pEntry->chain==elem ){
30284 pEntry->chain = elem->next;
30286 pEntry->count--;
30287 assert( pEntry->count>=0 );
30290 pH->count--;
30291 if( pH->count==0 ){
30292 assert( pH->first==0 );
30293 assert( pH->count==0 );
30305 return findElementWithHash(pH, pKey, 0)->data;
30330 if( elem->data ){
30331 void *old_data = elem->data;
30335 elem->data = data;
30336 elem->pKey = pKey;
30343 new_elem->pKey = pKey;
30344 new_elem->data = data;
30345 pH->count++;
30346 if( pH->count>=10 && pH->count > 2*pH->htsize ){
30347 if( rehash(pH, pH->count*2) ){
30348 assert( pH->htsize>0 );
30349 h = strHash(pKey) % pH->htsize;
30352 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
30418 /* 48 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
30419 /* 49 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
30429 /* 59 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
30459 /* 89 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
30464 /* 94 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
30518 /* 148 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30559 ** This file contains the VFS implementation for unix-like operating systems
30565 ** use flock(), dot-files, various proprietary locking schemas, or simply
30575 ** * General-purpose declarations and utility functions.
30579 ** + for no-op locks
30580 ** + for dot-file locks
30588 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
30601 ** 3. Dot-file locking,
30685 ** If we are to be thread-safe, include the pthreads header and define
30708 ** Maximum supported path-length.
30730 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
30759 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
30783 ** transaction counter in bytes 24-27 of database files are updated
30843 ** all of the platform-specific files (os_*.c) and is #included into those
30855 ** switch. The following code should catch this problem at compile-time.
30869 ** high-performance timing routines.
30885 ** This file contains inline asm code for retrieving "high-performance"
30892 ** The following routine only works on pentium-class (or newer) processors.
30894 ** processor and returns that value. This can be used for high-res
30965 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
30989 || sqlite3_io_error_pending-- == 1 ) \
31004 sqlite3_diskfull_pending--; \
31046 ** The threadid macro resolves to the thread-id or to 0. Used for
31067 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
31068 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
31076 ** Linux-specific IOCTL magic numbers used for controlling F2FS
31094 ** which always has the same well-defined interface.
31105 ** Many system calls are accessed through pointer-to-functions so that
31134 ** DJGPP. But it is DOS - what did you expect?
31272 ** log if they come from non-root processes. So avoid calling fchown() if
31352 int i = -1;
31356 for(i=0; i<ArraySize(aSyscall)-1; i++){
31384 ** The m parameter will be non-zero only when creating -wal, -journal,
31385 ** and -shm files. We want those files to have *exactly* the same
31387 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
31409 fd = -1;
31460 ** integer lock-type.
31479 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
31480 ** command-line option on the compiler. This code is normally
31496 if( p->l_type==F_RDLCK ){
31498 }else if( p->l_type==F_WRLCK ){
31500 }else if( p->l_type==F_UNLCK ){
31505 assert( p->l_whence==SEEK_SET );
31509 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
31510 (int)p->l_pid, s);
31511 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
31524 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
31544 /* On Android, ftruncate() always uses 32-bit offsets, even if
31605 ** a performance-critical path, so it is sufficient to put these
31630 ** Changes are made in-place. Return the new name length.
31632 ** The original filename is in z[0..n-1]. Return the number of
31637 while( n>1 && z[n-1]=='/' ){ n--; }
31646 while( j>0 && z[j-1]!='/' ){ j--; }
31647 if( j>0 ){ j--; }
31678 pNew->zCanonicalName = (char*)&pNew[1];
31679 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
31680 n = vxworksSimplifyName(pNew->zCanonicalName, n);
31687 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
31688 if( pCandidate->nName==n
31689 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
31692 pCandidate->nRef++;
31699 pNew->nRef = 1;
31700 pNew->nName = n;
31701 pNew->pNext = vxworksFileList;
31713 assert( pId->nRef>0 );
31714 pId->nRef--;
31715 if( pId->nRef==0 ){
31717 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
31719 *pp = pId->pNext;
31777 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
31805 ** on linux - with NPTL a lock created by thread A can override locks
31806 ** in thread B. But there is no way to know at compile-time which
31808 ** compile-time whether or not thread A can override locks on thread B.
31809 ** One has to do a run-time check to discover the behavior of the
31829 ** sizes ino_t at only 32-bits instead of 64-bits. (See
31830 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
31831 ** To work around this, always allocate 64-bits for the inode number.
31832 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
31876 ** This function - unixLogErrorAtLine(), is only ever called via the macro
31881 ** errno and, if possible, the human-readable equivalent from strerror() or
31887 ** failed (e.g. "unlink", "open") and the associated file-system path,
31901 ** the strerror() function to obtain the human-readable error message
31924 strerror_r(iErrno, aErr, sizeof(aErr)-1);
31930 /* Non-threadsafe build, use strerror(). */
31936 "os_unix.c:%d: (%d) %s(%s) - %s",
31959 pFile ? pFile->zPath : 0, lineno);
31964 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
31968 pFile->lastErrno = error;
31972 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
31975 unixInodeInfo *pInode = pFile->pInode;
31978 for(p=pInode->pUnused; p; p=pNext){
31979 pNext = p->pNext;
31980 robust_close(pFile, p->fd, __LINE__);
31982 nUnusedFd--;
31984 pInode->pUnused = 0;
31994 unixInodeInfo *pInode = pFile->pInode;
31997 pInode->nRef--;
31998 if( pInode->nRef==0 ){
31999 assert( pInode->pShmNode==0 );
32001 if( pInode->pPrev ){
32002 assert( pInode->pPrev->pNext==pInode );
32003 pInode->pPrev->pNext = pInode->pNext;
32006 inodeList = pInode->pNext;
32008 if( pInode->pNext ){
32009 assert( pInode->pNext->pPrev==pInode );
32010 pInode->pNext->pPrev = pInode->pPrev;
32035 struct stat statbuf; /* Low-level file information */
32040 /* Get low-level information about the file that we can used to
32043 fd = pFile->h;
32048 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
32055 ** incorrectly for zero-size files. See ticket #3260. To work
32064 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
32081 fileId.pId = pFile->pId;
32087 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
32088 pInode = pInode->pNext;
32096 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
32097 pInode->nRef = 1;
32098 pInode->pNext = inodeList;
32099 pInode->pPrev = 0;
32100 if( inodeList ) inodeList->pPrev = pInode;
32103 pInode->nRef++;
32114 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
32117 return pFile->pInode!=0 &&
32118 (osStat(pFile->zPath, &buf)!=0
32119 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
32138 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
32140 rc = osFstat(pFile->h, &buf);
32142 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
32146 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
32150 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
32154 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
32163 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32174 assert( pFile->eFileLock<=SHARED_LOCK );
32175 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
32178 if( pFile->pInode->eFileLock>SHARED_LOCK ){
32185 if( !reserved && !pFile->pInode->bProcessLock ){
32191 if( osFcntl(pFile->h, F_GETLK, &lock) ){
32201 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
32208 ** Attempt to set a system-lock on the file pFile. The lock is
32211 ** If the pFile was opened read/write from unix-excl, then the only lock
32214 ** operations become no-ops. Locking operations still happen internally,
32219 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
32220 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
32221 ** and is read-only.
32223 ** Zero is returned if the call completes successfully, or -1 if a call
32228 unixInodeInfo *pInode = pFile->pInode;
32231 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
32232 if( pInode->bProcessLock==0 ){
32234 assert( pInode->nLock==0 );
32239 rc = osFcntl(pFile->h, F_SETLK, &lock);
32241 pInode->bProcessLock = 1;
32242 pInode->nLock++;
32247 rc = osFcntl(pFile->h, F_SETLK, pLock);
32253 ** Lock the file with the lock specified by parameter eFileLock - one
32267 ** UNLOCKED -> SHARED
32268 ** SHARED -> RESERVED
32269 ** SHARED -> (PENDING) -> EXCLUSIVE
32270 ** RESERVED -> (PENDING) -> EXCLUSIVE
32271 ** PENDING -> EXCLUSIVE
32279 ** lock primitives (called read-locks and write-locks below, to avoid
32288 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
32289 ** byte'. If this is successful, 'shared byte range' is read-locked
32292 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
32294 ** Windows95 is now pretty much extinct, but this work-around for the
32295 ** lack of shared-locks on Windows95 lives on, for backwards
32299 ** A RESERVED lock is implemented by grabbing a write-lock on the
32303 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
32311 ** implemented by obtaining a write-lock on the entire 'shared byte
32312 ** range'. Since all other locks require a read-lock on one of the bytes
32323 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
32324 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
32325 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
32332 if( pFile->eFileLock>=eFileLock ){
32333 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
32343 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
32345 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
32347 /* This mutex is needed because pFile->pInode is shared across threads
32350 pInode = pFile->pInode;
32355 if( (pFile->eFileLock!=pInode->eFileLock &&
32356 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
32367 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
32369 assert( pFile->eFileLock==0 );
32370 assert( pInode->nShared>0 );
32371 pFile->eFileLock = SHARED_LOCK;
32372 pInode->nShared++;
32373 pInode->nLock++;
32385 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
32404 assert( pInode->nShared==0 );
32405 assert( pInode->eFileLock==0 );
32408 /* Now get the read-lock */
32432 pFile->eFileLock = SHARED_LOCK;
32433 pInode->nLock++;
32434 pInode->nShared = 1;
32436 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
32445 assert( 0!=pFile->eFileLock );
32468 /* Set up the transaction-counter change checking flags when
32474 && pFile->eFileLock<=SHARED_LOCK
32477 pFile->transCntrChng = 0;
32478 pFile->dbUpdate = 0;
32479 pFile->inNormalWrite = 1;
32485 pFile->eFileLock = eFileLock;
32486 pInode->eFileLock = eFileLock;
32488 pFile->eFileLock = PENDING_LOCK;
32489 pInode->eFileLock = PENDING_LOCK;
32494 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
32504 unixInodeInfo *pInode = pFile->pInode;
32505 UnixUnusedFd *p = pFile->pPreallocatedUnused;
32506 p->pNext = pInode->pUnused;
32507 pInode->pUnused = p;
32508 pFile->h = -1;
32509 pFile->pPreallocatedUnused = 0;
32518 ** the requested locking level, this routine is a no-op.
32533 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
32534 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
32538 if( pFile->eFileLock<=eFileLock ){
32542 pInode = pFile->pInode;
32543 assert( pInode->nShared!=0 );
32544 if( pFile->eFileLock>SHARED_LOCK ){
32545 assert( pInode->eFileLock==pFile->eFileLock );
32556 pFile->inNormalWrite = 0;
32560 ** before establishing the readlock - to avoid a race condition we downgrade
32576 off_t divSize = SHARED_SIZE - 1;
32582 if( unixFileLock(pFile, &lock)==(-1) ){
32592 if( unixFileLock(pFile, &lock)==(-1) ){
32603 lock.l_len = SHARED_SIZE-divSize;
32604 if( unixFileLock(pFile, &lock)==(-1) ){
32635 pInode->eFileLock = SHARED_LOCK;
32647 pInode->nShared--;
32648 if( pInode->nShared==0 ){
32653 pInode->eFileLock = NO_LOCK;
32657 pInode->eFileLock = NO_LOCK;
32658 pFile->eFileLock = NO_LOCK;
32666 pInode->nLock--;
32667 assert( pInode->nLock>=0 );
32668 if( pInode->nLock==0 ){
32675 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
32684 ** the requested locking level, this routine is a no-op.
32688 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
32713 if( pFile->h>=0 ){
32714 robust_close(pFile, pFile->h, __LINE__);
32715 pFile->h = -1;
32718 if( pFile->pId ){
32719 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
32720 osUnlink(pFile->pId->zCanonicalName);
32722 vxworksReleaseFileId(pFile->pId);
32723 pFile->pId = 0;
32727 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
32728 osUnlink(pFile->zPath);
32729 sqlite3_free(*(char**)&pFile->zPath);
32730 pFile->zPath = 0;
32733 OSTRACE(("CLOSE %-3d\n", pFile->h));
32734 OpenCounter(-1);
32735 sqlite3_free(pFile->pPreallocatedUnused);
32753 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
32754 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
32757 ** descriptor to pInode->pUnused list. It will be automatically closed
32772 ****************************** No-op Locking **********************************
32778 ** This locking mode is appropriate for use on read-only databases
32779 ** (ex: databases that are burned into CD-ROM, for example.) It can
32809 /******************* End of the no-op lock implementation *********************
32813 ************************* Begin dot-file Locking ******************************
32843 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32858 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
32859 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
32865 ** Lock the file with the lock specified by parameter eFileLock - one
32879 ** UNLOCKED -> SHARED
32880 ** SHARED -> RESERVED
32881 ** SHARED -> (PENDING) -> EXCLUSIVE
32882 ** RESERVED -> (PENDING) -> EXCLUSIVE
32883 ** PENDING -> EXCLUSIVE
32893 char *zLockFile = (char *)pFile->lockingContext;
32900 if( pFile->eFileLock > NO_LOCK ){
32901 pFile->eFileLock = eFileLock;
32928 pFile->eFileLock = eFileLock;
32937 ** the requested locking level, this routine is a no-op.
32943 char *zLockFile = (char *)pFile->lockingContext;
32947 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
32948 pFile->eFileLock, osGetpid(0)));
32951 /* no-op if possible */
32952 if( pFile->eFileLock==eFileLock ){
32960 pFile->eFileLock = SHARED_LOCK;
32977 pFile->eFileLock = NO_LOCK;
32988 sqlite3_free(pFile->lockingContext);
32991 /****************** End of the dot-file lock implementation *******************
32999 ** flock() locking is like dot-file locking in that the various
33000 ** fine-grain locking levels supported by SQLite are collapsed into
33027 ** to a non-zero value otherwise *pResOut is set to zero. The return value
33040 if( pFile->eFileLock>SHARED_LOCK ){
33047 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
33050 lrc = robust_flock(pFile->h, LOCK_UN);
33069 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
33082 ** Lock the file with the lock specified by parameter eFileLock - one
33096 ** UNLOCKED -> SHARED
33097 ** SHARED -> RESERVED
33098 ** SHARED -> (PENDING) -> EXCLUSIVE
33099 ** RESERVED -> (PENDING) -> EXCLUSIVE
33100 ** PENDING -> EXCLUSIVE
33118 if (pFile->eFileLock > NO_LOCK) {
33119 pFile->eFileLock = eFileLock;
33125 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
33134 pFile->eFileLock = eFileLock;
33136 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
33152 ** the requested locking level, this routine is a no-op.
33158 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
33159 pFile->eFileLock, osGetpid(0)));
33162 /* no-op if possible */
33163 if( pFile->eFileLock==eFileLock ){
33169 pFile->eFileLock = eFileLock;
33174 if( robust_flock(pFile->h, LOCK_UN) ){
33180 pFile->eFileLock = NO_LOCK;
33204 ** Semaphore locking is like dot-lock and flock in that it really only
33214 ** to a non-zero value otherwise *pResOut is set to zero. The return value
33227 if( pFile->eFileLock>SHARED_LOCK ){
33233 sem_t *pSem = pFile->pInode->pSem;
33235 if( sem_trywait(pSem)==-1 ){
33242 reserved = (pFile->eFileLock < SHARED_LOCK);
33249 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
33256 ** Lock the file with the lock specified by parameter eFileLock - one
33270 ** UNLOCKED -> SHARED
33271 ** SHARED -> RESERVED
33272 ** SHARED -> (PENDING) -> EXCLUSIVE
33273 ** RESERVED -> (PENDING) -> EXCLUSIVE
33274 ** PENDING -> EXCLUSIVE
33286 sem_t *pSem = pFile->pInode->pSem;
33291 if (pFile->eFileLock > NO_LOCK) {
33292 pFile->eFileLock = eFileLock;
33298 if( sem_trywait(pSem)==-1 ){
33304 pFile->eFileLock = eFileLock;
33315 ** the requested locking level, this routine is a no-op.
33319 sem_t *pSem = pFile->pInode->pSem;
33323 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
33324 pFile->eFileLock, osGetpid(0)));
33327 /* no-op if possible */
33328 if( pFile->eFileLock==eFileLock ){
33334 pFile->eFileLock = eFileLock;
33339 if ( sem_post(pSem)==-1 ) {
33347 pFile->eFileLock = NO_LOCK;
33379 ** on Apple Macintosh computers - both OS9 and OSX.
33381 ** Third-party implementations of AFP are available. But this code here
33408 ** This is a utility for setting or clearing a bit-range lock on an
33427 pb.fd = pFile->h;
33430 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
33433 if ( err==-1 ) {
33456 ** to a non-zero value otherwise *pResOut is set to zero. The return value
33468 context = (afpLockingContext *) pFile->lockingContext;
33469 if( context->reserved ){
33473 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
33476 if( pFile->pInode->eFileLock>SHARED_LOCK ){
33484 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
33488 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
33499 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
33506 ** Lock the file with the lock specified by parameter eFileLock - one
33520 ** UNLOCKED -> SHARED
33521 ** SHARED -> RESERVED
33522 ** SHARED -> (PENDING) -> EXCLUSIVE
33523 ** RESERVED -> (PENDING) -> EXCLUSIVE
33524 ** PENDING -> EXCLUSIVE
33532 unixInodeInfo *pInode = pFile->pInode;
33533 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
33536 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
33537 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
33538 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
33544 if( pFile->eFileLock>=eFileLock ){
33545 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
33555 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
33557 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
33559 /* This mutex is needed because pFile->pInode is shared across threads
33562 pInode = pFile->pInode;
33567 if( (pFile->eFileLock!=pInode->eFileLock &&
33568 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
33579 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
33581 assert( pFile->eFileLock==0 );
33582 assert( pInode->nShared>0 );
33583 pFile->eFileLock = SHARED_LOCK;
33584 pInode->nShared++;
33585 pInode->nLock++;
33594 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
33597 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
33611 assert( pInode->nShared==0 );
33612 assert( pInode->eFileLock==0 );
33615 /* Now get the read-lock SHARED_LOCK */
33618 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
33619 lrc1 = afpSetLock(context->dbPath, pFile,
33620 SHARED_FIRST+pInode->sharedByte, 1, 1);
33622 lrc1Errno = pFile->lastErrno;
33625 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
33637 pFile->eFileLock = SHARED_LOCK;
33638 pInode->nLock++;
33639 pInode->nShared = 1;
33641 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
33651 assert( 0!=pFile->eFileLock );
33652 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
33654 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
33656 context->reserved = 1;
33665 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
33666 pInode->sharedByte, 1, 0)) ){
33669 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
33671 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
33672 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
33690 pFile->eFileLock = eFileLock;
33691 pInode->eFileLock = eFileLock;
33693 pFile->eFileLock = PENDING_LOCK;
33694 pInode->eFileLock = PENDING_LOCK;
33699 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
33709 ** the requested locking level, this routine is a no-op.
33715 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
33718 int h = pFile->h;
33722 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
33723 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
33727 if( pFile->eFileLock<=eFileLock ){
33731 pInode = pFile->pInode;
33732 assert( pInode->nShared!=0 );
33733 if( pFile->eFileLock>SHARED_LOCK ){
33734 assert( pInode->eFileLock==pFile->eFileLock );
33736 SimulateIOError( h=(-1) )
33748 assert( pFile->inNormalWrite==0
33749 || pFile->dbUpdate==0
33750 || pFile->transCntrChng==1 );
33751 pFile->inNormalWrite = 0;
33754 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
33755 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
33756 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
33757 /* only re-establish the shared lock if necessary */
33758 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
33759 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
33764 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
33765 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
33767 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
33768 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
33770 context->reserved = 0;
33773 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
33774 pInode->eFileLock = SHARED_LOCK;
33783 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
33784 pInode->nShared--;
33785 if( pInode->nShared==0 ){
33787 SimulateIOError( h=(-1) )
33790 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
33793 pInode->eFileLock = NO_LOCK;
33794 pFile->eFileLock = NO_LOCK;
33798 pInode->nLock--;
33799 assert( pInode->nLock>=0 );
33800 if( pInode->nLock==0 ){
33807 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
33820 if( pFile->pInode && pFile->pInode->nLock ){
33823 ** descriptor to pInode->aPending. It will be automatically closed when
33829 sqlite3_free(pFile->lockingContext);
33839 ** is available. If you don't compile for a mac, then the "unix-afp"
33854 ** the requested locking level, this routine is a no-op.
33870 **************** Non-locking sqlite3_file methods *****************************
33900 assert( id->h>2 );
33903 got = osPread(id->h, pBuf, cnt, offset);
33904 SimulateIOError( got = -1 );
33906 got = osPread64(id->h, pBuf, cnt, offset);
33907 SimulateIOError( got = -1 );
33909 newOffset = lseek(id->h, offset, SEEK_SET);
33910 SimulateIOError( newOffset = -1 );
33913 return -1;
33915 got = osRead(id->h, pBuf, cnt);
33924 cnt -= got;
33931 OSTRACE(("READ %-3d %5d %7lld %llu\n",
33932 id->h, got+prior, offset-prior, TIMER_ELAPSED));
33953 /* If this is a database file (not a journal, master-journal or temp
33956 assert( pFile->pPreallocatedUnused==0
33965 if( offset<pFile->mmapSize ){
33966 if( offset+amt <= pFile->mmapSize ){
33967 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
33970 int nCopy = pFile->mmapSize - offset;
33971 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
33973 amt -= nCopy;
33987 /* Unread parts of the buffer must be zero-filled */
33988 memset(&((char*)pBuf)[got], 0, amt-got);
33994 ** Attempt to seek the file-descriptor passed as the first argument to
33996 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
34022 SimulateIOError( iSeek = -1 );
34024 rc = -1;
34032 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
34040 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
34047 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
34066 /* If this is a database file (not a journal, master-journal or temp
34069 assert( pFile->pPreallocatedUnused==0
34077 ** doing a hot-journal rollback or a write to some file other than a
34082 if( pFile->inNormalWrite ){
34083 pFile->dbUpdate = 1; /* The database has been modified */
34090 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
34091 pFile->transCntrChng = 1; /* The transaction counter has changed */
34100 if( offset<pFile->mmapSize ){
34101 if( offset+amt <= pFile->mmapSize ){
34102 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
34105 int nCopy = pFile->mmapSize - offset;
34106 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
34108 amt -= nCopy;
34115 amt -= wrote;
34119 SimulateIOError(( wrote=(-1), amt=1 ));
34123 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
34148 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
34218 ** no-op. But go ahead and call fstat() to validate the file
34251 if( rc==-1 && errno==ENOTSUP ){
34257 if( OS_VXWORKS && rc!= -1 ){
34270 ** The directory file descriptor is used for only one thing - to
34279 ** replace this routine with a harmless no-op. To make this routine
34280 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
34288 int fd = -1;
34292 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
34301 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
34316 ** has been created by fsync-ing the directory that contains the file.
34321 ** will not roll back - possibly leading to database corruption.
34341 OSTRACE(("SYNC %-3d\n", pFile->h));
34342 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
34346 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
34350 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
34353 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
34355 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
34357 rc = osOpenDirectory(pFile->zPath, &dirfd);
34365 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
34379 /* If the user has configured a chunk-size for this file, truncate the
34384 if( pFile->szChunk>0 ){
34385 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
34388 rc = robust_ftruncate(pFile->h, nByte);
34391 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
34395 ** doing a hot-journal rollback or a write to some file other than a
34398 ** when restoring a database using the backup API from a zero-length
34401 if( pFile->inNormalWrite && nByte==0 ){
34402 pFile->transCntrChng = 1;
34411 if( nByte<pFile->mmapSize ){
34412 pFile->mmapSize = nByte;
34427 rc = osFstat(((unixFile*)id)->h, &buf);
34435 /* When opening a zero-size database, the findInodeInfo() procedure
34437 ** in the OS-X msdos filesystem. In order to avoid problems with upper
34449 ** Handler for proxy-locking file-control verbs. Defined below in the
34457 ** file-control operation. Enlarge the database to nBytes in size
34458 ** (rounded up to the next chunk-size). If the database is already
34459 ** nBytes or larger, this routine is a no-op.
34462 if( pFile->szChunk>0 ){
34466 if( osFstat(pFile->h, &buf) ){
34470 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
34479 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
34486 ** at offset (nSize-1), to set the size of the file correctly.
34490 int nBlk = buf.st_blksize; /* File-system block size */
34494 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
34497 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
34498 if( iWrite>=nSize ) iWrite = nSize - 1;
34507 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
34509 if( pFile->szChunk<=0 ){
34510 if( robust_ftruncate(pFile->h, nByte) ){
34512 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
34526 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34528 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34532 *pArg = (pFile->ctrlFlags & mask)!=0;
34534 pFile->ctrlFlags &= ~mask;
34536 pFile->ctrlFlags |= mask;
34551 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
34555 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
34559 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
34565 *(int*)pArg = pFile->eFileLock;
34569 *(int*)pArg = pFile->lastErrno;
34573 pFile->szChunk = *(int *)pArg;
34592 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
34596 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
34598 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
34617 ** 64-bit type. */
34622 *(i64*)pArg = pFile->mmapSizeMax;
34623 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
34624 pFile->mmapSizeMax = newLimit;
34625 if( pFile->mmapSize>0 ){
34627 rc = unixMapfile(pFile, -1);
34640 ((unixFile*)id)->dbUpdate = 0;
34655 ** If pFd->sectorSize is non-zero when this function is called, it is a
34656 ** no-op. Otherwise, the values of pFd->sectorSize and
34657 ** pFd->deviceCharacteristics are set according to the file-system
34665 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
34666 if( pFd->sectorSize==0 ){
34672 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
34674 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
34679 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
34680 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
34683 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
34690 if( pFile->sectorSize == 0 ){
34693 /* Set defaults for non-supported filesystems */
34694 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
34695 pFile->deviceCharacteristics = 0;
34696 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
34701 pFile->sectorSize = fsInfo.f_bsize;
34702 pFile->deviceCharacteristics =
34710 pFile->sectorSize = fsInfo.f_bsize;
34711 pFile->deviceCharacteristics =
34713 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
34720 pFile->sectorSize = fsInfo.f_bsize;
34721 pFile->deviceCharacteristics =
34729 pFile->sectorSize = fsInfo.f_bsize;
34730 pFile->deviceCharacteristics =
34732 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
34737 pFile->sectorSize = fsInfo.f_bsize;
34738 pFile->deviceCharacteristics =
34740 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
34745 pFile->deviceCharacteristics =
34754 if( pFile->sectorSize % 512 != 0 ){
34755 pFile->deviceCharacteristics = 0;
34756 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
34767 ** if two files are created in the same file-system directory (i.e.
34774 return pFd->sectorSize;
34783 ** words, after a power-loss event, parts of the file that were never
34784 ** written might end up being altered.) However, non-PSOW behavior is very,
34787 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
34793 return pFd->deviceCharacteristics;
34821 ** When multiple threads all reference the same wal-index, each thread
34823 ** of this unixShmNode object. In other words, each wal-index is opened
34838 ** The following fields are read-only after the object is created:
34852 int szRegion; /* Size of shared-memory regions */
34854 u8 isReadonly; /* True if read-only */
34856 char **apRegion; /* Array of mapped shared-memory regions */
34871 ** are read-only thereafter:
34876 ** All other fields are read/write. The unixShm.pFile->mutex must be held
34895 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
34897 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
34906 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
34911 pShmNode = pFile->pInode->pShmNode;
34912 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) );
34920 if( pShmNode->h>=0 ){
34928 rc = osFcntl(pShmNode->h, F_SETLK, &f);
34929 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
34935 OSTRACE(("SHM-LOCK "));
34936 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
34940 pShmNode->exclMask &= ~mask;
34941 pShmNode->sharedMask &= ~mask;
34943 OSTRACE(("read-lock %d ok", ofst));
34944 pShmNode->exclMask &= ~mask;
34945 pShmNode->sharedMask |= mask;
34948 OSTRACE(("write-lock %d ok", ofst));
34949 pShmNode->exclMask |= mask;
34950 pShmNode->sharedMask &= ~mask;
34956 OSTRACE(("read-lock failed"));
34959 OSTRACE(("write-lock %d failed", ofst));
34962 OSTRACE((" - afterwards %03x,%03x\n",
34963 pShmNode->sharedMask, pShmNode->exclMask));
34973 ** current system page-size.
34976 ** to use 64KB pages - in this case each mapping must cover at least two
34982 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
34990 ** This is not a VFS shared-memory method; it is a utility function called
34991 ** by VFS shared-memory methods.
34994 unixShmNode *p = pFd->pInode->pShmNode;
34996 if( p && ALWAYS(p->nRef==0) ){
34999 assert( p->pInode==pFd->pInode );
35000 sqlite3_mutex_free(p->mutex);
35001 for(i=0; i<p->nRegion; i+=nShmPerMap){
35002 if( p->h>=0 ){
35003 osMunmap(p->apRegion[i], p->szRegion);
35005 sqlite3_free(p->apRegion[i]);
35008 sqlite3_free(p->apRegion);
35009 if( p->h>=0 ){
35010 robust_close(pFd, p->h, __LINE__);
35011 p->h = -1;
35013 p->pInode->pShmNode = 0;
35025 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
35034 ** and proceed with opening the *-shm file.
35038 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
35045 ** EXCLUSIVE failed just before truncating the *-shm file, then this
35046 ** process might open and use the *-shm file without truncating it.
35047 ** And if the *-shm file has been corrupted by a power failure or
35053 if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
35056 if( pShmNode->isReadonly ){
35057 pShmNode->isUnlocked = 1;
35061 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){
35062 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
35077 ** Open a shared-memory area associated with open database file pDbFd.
35080 ** The file used to implement shared-memory is in the same directory
35082 ** file with the "-shm" suffix added. For example, if the database file
35084 ** for shared memory will be called "/home/user1/config.db-shm".
35090 ** database to end up using different files for shared memory -
35091 ** meaning that their memory would not really be shared - resulting
35093 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
35094 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
35098 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
35101 ** When opening a new shared-memory file, if no other instances of that
35105 ** If the original database file (pDbFd) is using the "unix-excl" VFS
35123 assert( pDbFd->pShm==0 );
35129 pInode = pDbFd->pInode;
35130 pShmNode = pInode->pShmNode;
35134 const char *zBasePath = pDbFd->zPath;
35138 ** a new *-shm file is created, an attempt will be made to create it
35141 if( osFstat(pDbFd->h, &sStat) ){
35157 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
35160 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
35163 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
35164 sqlite3FileSuffix3(pDbFd->zPath, zShm);
35166 pShmNode->h = -1;
35167 pDbFd->pInode->pShmNode = pShmNode;
35168 pShmNode->pInode = pDbFd->pInode;
35170 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
35171 if( pShmNode->mutex==0 ){
35177 if( pInode->bProcessLock==0 ){
35178 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
35179 pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777));
35181 if( pShmNode->h<0 ){
35182 pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
35183 if( pShmNode->h<0 ){
35187 pShmNode->isReadonly = 1;
35194 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
35202 p->pShmNode = pShmNode;
35204 p->id = pShmNode->nextShmId++;
35206 pShmNode->nRef++;
35207 pDbFd->pShm = p;
35214 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
35217 sqlite3_mutex_enter(pShmNode->mutex);
35218 p->pNext = pShmNode->pFirst;
35219 pShmNode->pFirst = p;
35220 sqlite3_mutex_leave(pShmNode->mutex);
35233 ** shared-memory associated with the database file fd. Shared-memory regions
35234 ** are numbered starting from zero. Each shared-memory region is szRegion
35239 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
35242 ** bExtend is non-zero and the requested shared-memory region has not yet
35245 ** If the shared-memory region has already been allocated or is allocated by
35264 /* If the shared-memory file has not yet been opened, open it now. */
35265 if( pDbFd->pShm==0 ){
35270 p = pDbFd->pShm;
35271 pShmNode = p->pShmNode;
35272 sqlite3_mutex_enter(pShmNode->mutex);
35273 if( pShmNode->isUnlocked ){
35276 pShmNode->isUnlocked = 0;
35278 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
35279 assert( pShmNode->pInode==pDbFd->pInode );
35280 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
35281 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
35286 if( pShmNode->nRegion<nReqRegion ){
35291 pShmNode->szRegion = szRegion;
35293 if( pShmNode->h>=0 ){
35295 ** Check to see if it has been allocated (i.e. if the wal-index file is
35298 if( osFstat(pShmNode->h, &sStat) ){
35326 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
35327 const char *zFile = pShmNode->zFilename;
35338 pShmNode->apRegion, nReqRegion*sizeof(char *)
35344 pShmNode->apRegion = apNew;
35345 while( pShmNode->nRegion<nReqRegion ){
35349 if( pShmNode->h>=0 ){
35351 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
35352 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
35355 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
35368 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
35370 pShmNode->nRegion += nShmPerMap;
35375 if( pShmNode->nRegion>iRegion ){
35376 *pp = pShmNode->apRegion[iRegion];
35380 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
35381 sqlite3_mutex_leave(pShmNode->mutex);
35386 ** Change the lock state for a shared-memory segment.
35400 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
35402 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
35406 assert( pShmNode==pDbFd->pInode->pShmNode );
35407 assert( pShmNode->pInode==pDbFd->pInode );
35415 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
35416 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
35418 mask = (1<<(ofst+n)) - (1<<ofst);
35420 sqlite3_mutex_enter(pShmNode->mutex);
35425 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
35427 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
35428 allMask |= pX->sharedMask;
35431 /* Unlock the system-level locks */
35440 p->exclMask &= ~mask;
35441 p->sharedMask &= ~mask;
35450 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
35451 if( (pX->exclMask & mask)!=0 ){
35455 allShared |= pX->sharedMask;
35469 p->sharedMask |= mask;
35475 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
35476 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
35488 assert( (p->sharedMask & mask)==0 );
35489 p->exclMask |= mask;
35493 sqlite3_mutex_leave(pShmNode->mutex);
35494 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
35495 p->id, osGetpid(0), p->sharedMask, p->exclMask));
35509 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
35515 ** Close a connection to shared-memory. Delete the underlying
35519 ** routine is a harmless no-op.
35523 int deleteFlag /* Delete shared-memory if true */
35526 unixShmNode *pShmNode; /* The underlying shared-memory file */
35531 p = pDbFd->pShm;
35533 pShmNode = p->pShmNode;
35535 assert( pShmNode==pDbFd->pInode->pShmNode );
35536 assert( pShmNode->pInode==pDbFd->pInode );
35540 sqlite3_mutex_enter(pShmNode->mutex);
35541 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
35542 *pp = p->pNext;
35546 pDbFd->pShm = 0;
35547 sqlite3_mutex_leave(pShmNode->mutex);
35549 /* If pShmNode->nRef has reached 0, then close the underlying
35550 ** shared-memory file, too */
35552 assert( pShmNode->nRef>0 );
35553 pShmNode->nRef--;
35554 if( pShmNode->nRef==0 ){
35555 if( deleteFlag && pShmNode->h>=0 ){
35556 osUnlink(pShmNode->zFilename);
35578 assert( pFd->nFetchOut==0 );
35579 if( pFd->pMapRegion ){
35580 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
35581 pFd->pMapRegion = 0;
35582 pFd->mmapSize = 0;
35583 pFd->mmapSizeActual = 0;
35607 int h = pFd->h; /* File descriptor open on db file */
35608 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
35609 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
35613 assert( pFd->nFetchOut==0 );
35614 assert( nNew>pFd->mmapSize );
35615 assert( nNew<=pFd->mmapSizeMax );
35617 assert( pFd->mmapSizeActual>=pFd->mmapSize );
35621 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
35626 i64 nReuse = pFd->mmapSize;
35629 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
35635 osMunmap(pReq, nOrig-nReuse);
35642 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
35645 osMunmap(pNew, nNew - nReuse);
35667 unixLogError(SQLITE_OK, zErr, pFd->zPath);
35672 pFd->mmapSizeMax = 0;
35674 pFd->pMapRegion = (void *)pNew;
35675 pFd->mmapSize = pFd->mmapSizeActual = nNew;
35679 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35682 ** outstanding xFetch() references to it, this function is a no-op.
35684 ** If parameter nByte is non-negative, then it is the requested size of
35695 assert( nMap>=0 || pFd->nFetchOut==0 );
35696 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
35697 if( pFd->nFetchOut>0 ) return SQLITE_OK;
35700 struct stat statbuf; /* Low-level file information */
35701 if( osFstat(pFd->h, &statbuf) ){
35706 if( nMap>pFd->mmapSizeMax ){
35707 nMap = pFd->mmapSizeMax;
35710 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
35711 if( nMap!=pFd->mmapSize ){
35738 if( pFd->mmapSizeMax>0 ){
35739 if( pFd->pMapRegion==0 ){
35740 int rc = unixMapfile(pFd, -1);
35743 if( pFd->mmapSize >= iOff+nAmt ){
35744 *pp = &((u8 *)pFd->pMapRegion)[iOff];
35745 pFd->nFetchOut++;
35753 ** If the third argument is non-NULL, then this function releases a
35770 assert( (p==0)==(pFd->nFetchOut==0) );
35773 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
35776 pFd->nFetchOut--;
35781 assert( pFd->nFetchOut>=0 );
35799 ** of "finder" functions. A finder-function is used to locate the appropriate
35802 ** the correct finder-function for that VFS.
35805 ** object. The only interesting finder-function is autolockIoFinder, which
35809 ** For finder-function F, two objects are created:
35811 ** (1) The real finder-function named "FImpt()".
35818 ** directly at the finder-function since C90 rules prevent a void*
35935 ** The proxy locking method is a "super-method" in the sense that it
35937 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
36006 if( statfs(filePath, &fsInfo) != -1 ){
36018 ** Test byte-range lock using fcntl(). If the call succeeds,
36019 ** assume that the file-system supports POSIX style locks.
36025 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
36065 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
36103 assert( pNew->pInode==NULL );
36108 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
36109 pNew->h = h;
36110 pNew->pVfs = pVfs;
36111 pNew->zPath = zFilename;
36112 pNew->ctrlFlags = (u8)ctrlFlags;
36114 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
36118 pNew->ctrlFlags |= UNIXFILE_PSOW;
36120 if( strcmp(pVfs->zName,"unix-excl")==0 ){
36121 pNew->ctrlFlags |= UNIXFILE_EXCL;
36125 pNew->pId = vxworksFindFileId(zFilename);
36126 if( pNew->pId==0 ){
36135 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
36140 pNew->lockingContext = (void*)zFilename;
36150 rc = findInodeInfo(pNew, &pNew->pInode);
36163 ** handle h - as it is guaranteed that no posix locks will be released
36171 h = -1;
36182 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
36189 pCtx->dbPath = zFilename;
36190 pCtx->reserved = 0;
36193 rc = findInodeInfo(pNew, &pNew->pInode);
36195 sqlite3_free(pNew->lockingContext);
36197 h = -1;
36218 pNew->lockingContext = zLockFile;
36227 rc = findInodeInfo(pNew, &pNew->pInode);
36228 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
36229 char *zSemName = pNew->pInode->aSemName;
36232 pNew->pId->zCanonicalName);
36235 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
36236 if( pNew->pInode->pSem == SEM_FAILED ){
36238 pNew->pInode->aSemName[0] = '\0';
36249 h = -1;
36251 pNew->ctrlFlags |= UNIXFILE_DELETE;
36257 pNew->pMethod = pLockingStyle;
36300 ** pVfs->mxPathname bytes.
36306 /* It's odd to simulate an io-error here, but really this is just
36307 ** using the io-error infrastructure to test that SQLite handles this
36319 zBuf[nBuf-2] = 0;
36322 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
36329 ** Routine to transform a unixFile into a proxy-locking unixFile.
36330 ** Implementation in the proxy-lock division, but used by unixOpen()
36338 ** file (not a journal or master-journal file) identified by pathname
36344 ** other file descriptor open on the same file is holding a file-lock.
36350 ** such file descriptor is located, -1 is returned.
36368 ** ignored and -1 is returned. The caller will try to open a new file
36377 while( pInode && (pInode->fileId.dev!=sStat.st_dev
36378 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
36379 pInode = pInode->pNext;
36383 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
36386 nUnusedFd--;
36387 *pp = pUnused->pNext;
36428 ** this function queries the file-system for the permissions on the
36457 ** "<path to db>-journal"
36458 ** "<path to db>-wal"
36459 ** "<path to db>-journalNN"
36460 ** "<path to db>-walNN"
36465 nDb = sqlite3Strlen30(zPath) - 1;
36466 while( zPath[nDb]!='-' ){
36468 ** a '-' character. However in 8+3 filename mode, or if a corrupt
36470 ** the '-' might be missing. */
36472 nDb--;
36505 ** ReadWrite() -> (READWRITE | CREATE)
36506 ** ReadOnly() -> (READONLY)
36507 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
36509 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
36523 int fd = -1; /* File descriptor returned by open() */
36542 /* If creating a master or main-file journal, this function will open
36543 ** a file-descriptor on the directory too. The first time unixSync()
36577 /* Assert that the upper layer has set one of the "file-type" flags. */
36600 fd = pUnused->fd;
36607 p->pPreallocatedUnused = pUnused;
36609 /* Database filenames are double-zero terminated if they are not
36617 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
36623 /* Generated temporary filenames are always double-zero terminated
36644 assert( !p->pPreallocatedUnused );
36649 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
36657 /* Failed to open the file for read/write access. Try read-only. */
36685 if( p->pPreallocatedUnused ){
36686 p->pPreallocatedUnused->fd = fd;
36687 p->pPreallocatedUnused->flags = flags;
36705 p->openFlags = openFlags;
36710 if( fstatfs(fd, &fsInfo) == -1 ){
36716 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
36719 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
36735 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
36740 ** never use proxy, NULL means use proxy for non-local files only. */
36753 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
36771 sqlite3_free(p->pPreallocatedUnused);
36789 if( osUnlink(zPath)==(-1) ){
36863 if( osGetcwd(zOut, nOut-2)==0 ){
36870 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
36875 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
36881 ** is stored as a nul-terminated string in the buffer pointed to by
36885 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
36903 assert( pVfs->mxPathname==MAX_PATHNAME );
36906 /* It's odd to simulate an io-error here, but really this is just
36907 ** using the io-error infrastructure to test that SQLite handles this
36936 nByte = osReadlink(zIn, zDel, nOut-1);
36942 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
37002 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
37005 ** use dlsym() with -pedantic-errors?
37013 ** This work-around is unlikely to work correctly on any system where
37042 ** errors. The reports issued by valgrind are incorrect - we would
37044 ** uninitialized space in zBuf - but valgrind errors tend to worry
37106 ** The following variable, if set to a non-zero value, is interpreted as
37170 ** low-level error message when operating-system problems come up
37189 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
37191 ** meta-layer over top of the primitive locking implemented above. For
37194 ** been defined - so that the primitive locking methods are available
37199 ** The default locking schemes in SQLite use byte-range locks on the
37204 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
37212 ** SHARED_RANGE 0x40000002 -> 0x40000200
37216 ** the read cache when byte-range locks are present. Enabling the read
37219 ** close-to-open semantics for ensuring cache coherency
37223 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
37232 ** -----------------
37264 ** -----------------------
37274 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
37275 ** by taking an sqlite-style shared lock on the conch file, reading the
37279 ** is patterned after the database file name as ".<databasename>-conch".
37287 ** The proxy file - a single-byte file used for all advisory file locks
37303 ** ---------------------
37307 ** Database files accessed on non-local file systems are
37352 int conchHeld; /* 1 if the conch is held, -1 if lockless */
37385 if( lPath[len-1]!='/' ){
37414 if( lockPath[i] == '/' && (i - start > 0) ){
37416 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
37417 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
37449 int fd = -1;
37458 ** 2. if that fails, and this is a lock file (not-conch), try creating
37460 ** 3. if that fails, try to open the file read-only
37465 fd = pUnused->fd;
37506 pNew->openFlags = openFlags;
37510 pUnused->fd = fd;
37511 pUnused->flags = openFlags;
37512 pNew->pPreallocatedUnused = pUnused;
37570 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
37582 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37583 unixFile *conchFile = pCtx->conchFile;
37586 char *cPath = pCtx->conchFilePath;
37590 int fd = -1;
37591 int rc = -1;
37594 /* create a new path by replace the trailing '-conch' with '-break' */
37597 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
37602 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
37623 robust_close(pFile, conchFile->h, __LINE__);
37624 conchFile->h = fd;
37625 conchFile->openFlags = O_RDWR | O_CREAT;
37642 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37643 unixFile *conchFile = pCtx->conchFile;
37650 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
37660 if( osFstat(conchFile->h, &buf) ){
37679 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
37701 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
37704 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
37714 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
37720 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37722 if( pCtx->conchHeld!=0 ){
37725 unixFile *conchFile = pCtx->conchFile;
37738 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
37739 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
37755 storeLastErrno(pFile, conchFile->lastErrno);
37767 ** retry with a new auto-generated path
37775 if( !pCtx->lockProxyPath ){
37776 /* for auto-named local lock file, just check the host ID and we'll
37780 size_t pathLen = (readLen - PROXY_PATHINDEX);
37783 pathLen=MAXPATHLEN-1;
37793 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
37794 readLen-PROXY_PATHINDEX)
37802 if( (conchFile->openFlags&O_RDWR) == 0 ){
37808 if( !pCtx->lockProxyPath ){
37809 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
37818 futimes(conchFile->h, NULL);
37820 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
37836 if( pCtx->lockProxyPath!=NULL ){
37837 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
37843 robust_ftruncate(conchFile->h, writeSize);
37845 full_fsync(conchFile->h,0,0);
37851 int err = osFstat(pFile->h, &buf);
37857 osFchmod(conchFile->h, cmode);
37860 rc = osFchmod(conchFile->h, cmode);
37861 }while( rc==(-1) && errno==EINTR );
37877 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
37880 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
37881 if( rc==SQLITE_OK && pFile->openFlags ){
37883 if( pFile->h>=0 ){
37884 robust_close(pFile, pFile->h, __LINE__);
37886 pFile->h = -1;
37887 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
37890 pFile->h = fd;
37896 if( rc==SQLITE_OK && !pCtx->lockProxy ){
37897 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
37898 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
37901 ** so try again via auto-naming
37913 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
37914 if( !pCtx->lockProxyPath ){
37920 pCtx->conchHeld = 1;
37922 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
37924 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
37925 afpCtx->dbPath = pCtx->lockProxyPath;
37928 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
37930 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
37933 } while (1); /* in case we need to retry the :auto: lock file -
37946 pCtx = (proxyLockingContext *)pFile->lockingContext;
37947 conchFile = pCtx->conchFile;
37948 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
37949 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
37951 if( pCtx->conchHeld>0 ){
37952 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
37954 pCtx->conchHeld = 0;
37955 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
37973 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
37985 for( i=(len-1); i>=0; i-- ){
37997 /* append the "-conch" suffix to the file */
37998 memcpy(&conchPath[i+1], "-conch", 7);
38005 /* Takes a fully configured proxy locking-style unix file and switches
38009 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
38010 char *oldPath = pCtx->lockProxyPath;
38013 if( pFile->eFileLock!=NO_LOCK ){
38022 unixFile *lockProxy = pCtx->lockProxy;
38023 pCtx->lockProxy=NULL;
38024 pCtx->conchHeld = 0;
38026 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
38031 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
38046 if( pFile->pMethod == &afpIoMethods ){
38049 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
38050 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
38054 if( pFile->pMethod == &dotlockIoMethods ){
38057 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
38058 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
38061 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
38062 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
38072 ** ->lockingContext
38073 ** ->pMethod
38081 if( pFile->eFileLock!=NO_LOCK ){
38091 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
38100 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
38102 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
38103 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
38105 ** (c) the file system is read-only, then enable no-locking access.
38113 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
38115 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
38120 pCtx->conchHeld = -1; /* read only FS/ lockless */
38126 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
38130 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
38131 if( pCtx->dbPath==NULL ){
38139 pCtx->oldLockingContext = pFile->lockingContext;
38140 pFile->lockingContext = pCtx;
38141 pCtx->pOldMethod = pFile->pMethod;
38142 pFile->pMethod = &proxyIoMethods;
38144 if( pCtx->conchFile ){
38145 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
38146 sqlite3_free(pCtx->conchFile);
38148 sqlite3DbFree(0, pCtx->lockProxyPath);
38149 sqlite3_free(pCtx->conchFilePath);
38152 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
38166 if( pFile->pMethod == &proxyIoMethods ){
38167 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
38169 if( pCtx->lockProxyPath ){
38170 *(const char **)pArg = pCtx->lockProxyPath;
38182 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
38185 /* turn off proxy locking - not supported. If support is added for
38191 /* turn off proxy locking - already off - NOOP */
38198 (proxyLockingContext*)pFile->lockingContext;
38200 || (pCtx->lockProxyPath &&
38201 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
38224 ** above this point are all utilities. The lock-related methods of the
38225 ** proxy-locking sqlite3_io_method object follow.
38232 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38239 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
38240 if( pCtx->conchHeld>0 ){
38241 unixFile *proxy = pCtx->lockProxy;
38242 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
38251 ** Lock the file with the lock specified by parameter eFileLock - one
38265 ** UNLOCKED -> SHARED
38266 ** SHARED -> RESERVED
38267 ** SHARED -> (PENDING) -> EXCLUSIVE
38268 ** RESERVED -> (PENDING) -> EXCLUSIVE
38269 ** PENDING -> EXCLUSIVE
38278 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
38279 if( pCtx->conchHeld>0 ){
38280 unixFile *proxy = pCtx->lockProxy;
38281 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
38282 pFile->eFileLock = proxy->eFileLock;
38296 ** the requested locking level, this routine is a no-op.
38302 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
38303 if( pCtx->conchHeld>0 ){
38304 unixFile *proxy = pCtx->lockProxy;
38305 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
38306 pFile->eFileLock = proxy->eFileLock;
38320 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
38321 unixFile *lockProxy = pCtx->lockProxy;
38322 unixFile *conchFile = pCtx->conchFile;
38326 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
38328 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
38331 pCtx->lockProxy = 0;
38334 if( pCtx->conchHeld ){
38338 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
38342 sqlite3DbFree(0, pCtx->lockProxyPath);
38343 sqlite3_free(pCtx->conchFilePath);
38344 sqlite3DbFree(0, pCtx->dbPath);
38346 pFile->lockingContext = pCtx->oldLockingContext;
38347 pFile->pMethod = pCtx->pOldMethod;
38349 return pFile->pMethod->xClose(id);
38369 ** This routine registers all VFS implementations for unix-like operating
38386 ** when compiling with -pedantic-errors on GCC.)
38389 ** finder-function. The finder-function returns a pointer to the
38392 ** macro for addition information on finder-functions.
38440 UNIXVFS("unix-none", nolockIoFinder ),
38441 UNIXVFS("unix-dotfile", dotlockIoFinder ),
38442 UNIXVFS("unix-excl", posixIoFinder ),
38444 UNIXVFS("unix-namedsem", semIoFinder ),
38447 UNIXVFS("unix-posix", posixIoFinder ),
38450 UNIXVFS("unix-flock", flockIoFinder ),
38453 UNIXVFS("unix-afp", afpIoFinder ),
38454 UNIXVFS("unix-nfs", nfsIoFinder ),
38455 UNIXVFS("unix-proxy", proxyIoFinder ),
38460 /* Double-check that the aSyscall[] array has been constructed
38476 ** This routine is a no-op for unix.
38521 ** all of the platform-specific files (os_*.c) and is #included into those
38533 ** switch. The following code should catch this problem at compile-time.
38547 ** high-performance timing routines.
38563 ** This file contains inline asm code for retrieving "high-performance"
38570 ** The following routine only works on pentium-class (or newer) processors.
38572 ** processor and returns that value. This can be used for high-res
38643 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
38667 || sqlite3_io_error_pending-- == 1 ) \
38682 sqlite3_diskfull_pending--; \
38727 ** based on the sub-platform)?
38735 ** based on the sub-platform)?
38823 ** characters, so we allocate 4 bytes per character assuming worst-case of
38824 ** 4-bytes-per-character for UTF8.
38847 ** Returns non-zero if the character should be treated as a directory
38911 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
38924 typedef struct winShm winShm; /* A connection to shared-memory */
38925 typedef struct winShmNode winShmNode; /* A region of shared-memory */
38984 BOOL bNoLock; /* Non-zero if locking is disabled. */
38998 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
39024 * If this is non-zero, an isolated heap will be created by the native Win32
39030 * WARNING: It is important to note that when this setting is non-zero and the
39042 * This is the maximum possible initial size of the Win32-specific heap, in
39050 * This is the extra space for the initial size of the Win32-specific heap,
39063 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
39070 * Win32-specific heap. It cannot be negative.
39076 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
39082 * initial size of the Win32-specific heap to exceed the maximum amount
39091 * The initial size of the Win32-specific heap. This value may be zero.
39100 * The maximum size of the Win32-specific heap. This value may be zero.
39116 ** The winMemData structure stores information required by the Win32-specific
39200 ** Many system calls are accessed through pointer-to-functions so that
39817 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
39929 int i = -1;
39933 for(i=0; i<ArraySize(aSyscall)-1; i++){
39948 ** "pnLargest" argument, if non-zero, will be used to return the size of the
40038 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
40039 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
40040 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
40106 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
40134 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
40159 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
40254 if( n==(SIZE_T)-1 ){
40276 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
40277 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
40280 if( !pWinMemData->hHeap ){
40288 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
40290 if( !pWinMemData->hHeap ){
40297 pWinMemData->bOwned = TRUE;
40298 assert( pWinMemData->bOwned );
40301 pWinMemData->hHeap = osGetProcessHeap();
40302 if( !pWinMemData->hHeap ){
40307 pWinMemData->bOwned = FALSE;
40308 assert( !pWinMemData->bOwned );
40310 assert( pWinMemData->hHeap!=0 );
40311 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
40313 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
40325 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
40326 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
40328 if( pWinMemData->hHeap ){
40329 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
40331 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
40333 if( pWinMemData->bOwned ){
40334 if( !osHeapDestroy(pWinMemData->hHeap) ){
40336 osGetLastError(), (void*)pWinMemData->hHeap);
40338 pWinMemData->bOwned = FALSE;
40340 pWinMemData->hHeap = NULL;
40345 ** Populate the low-level memory allocation function pointers in
40372 ** Convert a UTF-8 string to Microsoft Unicode.
40380 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
40388 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
40398 ** Convert a Microsoft Unicode string to UTF-8.
40406 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
40414 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
40434 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
40443 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
40453 ** Convert a Microsoft Unicode string to a multi-byte character string,
40463 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
40471 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
40481 ** Convert a multi-byte character string to UTF-8.
40499 ** Convert a UTF-8 string to a multi-byte character string.
40651 ** is zero if the error message fits in the buffer, or non-zero
40731 ** This function - winLogErrorAtLine() - is only ever called via the macro
40736 ** error code and, if possible, the human-readable equivalent from
40742 ** failed and the associated file-system path, if any.
40762 "os_win.c:%d: (%lu) %s(%s) - %s",
40771 ** will be retried following a locking error - probably caused by
40788 ** non-zero if the error code is transient in nature and the operation
40876 y.tm_year = pTm.wYear - 1900;
40877 y.tm_mon = pTm.wMonth - 1;
40891 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
40925 memset(&pFile->local, 0, sizeof(pFile->local));
40935 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
40936 if (!pFile->hMutex){
40937 pFile->lastErrno = osGetLastError();
40939 return winLogError(SQLITE_IOERR, pFile->lastErrno,
40944 winceMutexAcquire(pFile->hMutex);
40947 ** case-sensitive, take advantage of that by uppercasing the mutex name
40951 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
40956 ** must be zero-initialized */
40965 if( pFile->hShared ){
40966 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
40969 if( !pFile->shared ){
40970 pFile->lastErrno = osGetLastError();
40971 winLogError(SQLITE_IOERR, pFile->lastErrno,
40974 osCloseHandle(pFile->hShared);
40975 pFile->hShared = NULL;
40980 if( pFile->hShared==NULL ){
40982 pFile->lastErrno = lastErrno;
40983 winLogError(SQLITE_IOERR, pFile->lastErrno,
40987 winceMutexRelease(pFile->hMutex);
40988 osCloseHandle(pFile->hMutex);
40989 pFile->hMutex = NULL;
40995 memset(pFile->shared, 0, sizeof(winceLock));
40998 winceMutexRelease(pFile->hMutex);
41006 if (pFile->hMutex){
41008 winceMutexAcquire(pFile->hMutex);
41012 if (pFile->local.nReaders){
41013 pFile->shared->nReaders --;
41015 if (pFile->local.bReserved){
41016 pFile->shared->bReserved = FALSE;
41018 if (pFile->local.bPending){
41019 pFile->shared->bPending = FALSE;
41021 if (pFile->local.bExclusive){
41022 pFile->shared->bExclusive = FALSE;
41025 /* De-reference and close our copy of the shared memory handle */
41026 osUnmapViewOfFile(pFile->shared);
41027 osCloseHandle(pFile->hShared);
41030 winceMutexRelease(pFile->hMutex);
41031 osCloseHandle(pFile->hMutex);
41032 pFile->hMutex = NULL;
41052 if (!pFile->hMutex) return TRUE;
41053 winceMutexAcquire(pFile->hMutex);
41058 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
41059 pFile->shared->bExclusive = TRUE;
41060 pFile->local.bExclusive = TRUE;
41065 /* Want a read-only lock? */
41068 if (pFile->shared->bExclusive == 0){
41069 pFile->local.nReaders ++;
41070 if (pFile->local.nReaders == 1){
41071 pFile->shared->nReaders ++;
41081 if (pFile->shared->bPending == 0) {
41082 pFile->shared->bPending = TRUE;
41083 pFile->local.bPending = TRUE;
41091 if (pFile->shared->bReserved == 0) {
41092 pFile->shared->bReserved = TRUE;
41093 pFile->local.bReserved = TRUE;
41098 winceMutexRelease(pFile->hMutex);
41118 if (!pFile->hMutex) return TRUE;
41119 winceMutexAcquire(pFile->hMutex);
41124 if (pFile->local.bExclusive){
41126 pFile->local.bExclusive = FALSE;
41127 pFile->shared->bExclusive = FALSE;
41132 else if (pFile->local.nReaders){
41135 pFile->local.nReaders --;
41136 if (pFile->local.nReaders == 0)
41138 pFile->shared->nReaders --;
41147 if (pFile->local.bPending){
41148 pFile->local.bPending = FALSE;
41149 pFile->shared->bPending = FALSE;
41156 if (pFile->local.bReserved) {
41157 pFile->local.bReserved = FALSE;
41158 pFile->shared->bReserved = FALSE;
41163 winceMutexRelease(pFile->hMutex);
41243 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
41249 ** Otherwise, set pFile->lastErrno and return non-zero.
41258 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
41264 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
41270 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
41274 pFile->lastErrno = lastErrno;
41275 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
41276 "winSeekFile", pFile->zPath);
41277 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
41281 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
41292 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
41295 pFile->lastErrno = osGetLastError();
41296 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
41297 "winSeekFile", pFile->zPath);
41298 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
41302 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
41330 assert( pFile->pShm==0 );
41332 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
41334 osGetCurrentProcessId(), pFile, pFile->h));
41341 rc = osCloseHandle(pFile->h);
41347 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
41348 if( pAppData==NULL || !pAppData->bNoLock ){
41352 if( pFile->zDeleteOnClose ){
41355 osDeleteFileW(pFile->zDeleteOnClose)==0
41356 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
41361 sqlite3_free(pFile->zDeleteOnClose);
41365 pFile->h = NULL;
41367 OpenCounter(-1);
41369 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
41372 "winClose", pFile->zPath);
41399 pFile->h, pBuf, amt, offset, pFile->locktype));
41404 if( offset<pFile->mmapSize ){
41405 if( offset+amt <= pFile->mmapSize ){
41406 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
41407 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
41408 osGetCurrentProcessId(), pFile, pFile->h));
41411 int nCopy = (int)(pFile->mmapSize - offset);
41412 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
41414 amt -= nCopy;
41423 osGetCurrentProcessId(), pFile, pFile->h));
41426 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
41431 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
41436 pFile->lastErrno = lastErrno;
41438 osGetCurrentProcessId(), pFile, pFile->h));
41439 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
41440 "winRead", pFile->zPath);
41444 /* Unread parts of the buffer must be zero-filled */
41445 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
41447 osGetCurrentProcessId(), pFile, pFile->h));
41452 osGetCurrentProcessId(), pFile, pFile->h));
41477 pFile->h, pBuf, amt, offset, pFile->locktype));
41482 if( offset<pFile->mmapSize ){
41483 if( offset+amt <= pFile->mmapSize ){
41484 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41485 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
41486 osGetCurrentProcessId(), pFile, pFile->h));
41489 int nCopy = (int)(pFile->mmapSize - offset);
41490 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41492 amt -= nCopy;
41520 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
41522 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
41538 nRem -= nWrite;
41541 pFile->lastErrno = lastErrno;
41547 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
41548 || ( pFile->lastErrno==ERROR_DISK_FULL )){
41550 osGetCurrentProcessId(), pFile, pFile->h));
41551 return winLogError(SQLITE_FULL, pFile->lastErrno,
41552 "winWrite1", pFile->zPath);
41555 osGetCurrentProcessId(), pFile, pFile->h));
41556 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
41557 "winWrite2", pFile->zPath);
41562 osGetCurrentProcessId(), pFile, pFile->h));
41577 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
41579 /* If the user has configured a chunk-size for this file, truncate the
41584 if( pFile->szChunk>0 ){
41585 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
41588 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
41590 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
41591 "winTruncate1", pFile->zPath);
41592 }else if( 0==osSetEndOfFile(pFile->h) &&
41594 pFile->lastErrno = lastErrno;
41595 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
41596 "winTruncate2", pFile->zPath);
41604 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
41605 pFile->mmapSize = nByte;
41610 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
41656 osGetCurrentProcessId(), pFile, pFile->h, flags,
41657 pFile->locktype));
41669 ** no-op
41672 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
41673 osGetCurrentProcessId(), pFile, pFile->h));
41677 if( pFile->pMapRegion ){
41678 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
41679 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
41681 pFile, pFile->pMapRegion));
41683 pFile->lastErrno = osGetLastError();
41684 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
41686 pFile, pFile->pMapRegion));
41687 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
41688 "winSync1", pFile->zPath);
41692 rc = osFlushFileBuffers(pFile->h);
41696 osGetCurrentProcessId(), pFile, pFile->h));
41699 pFile->lastErrno = osGetLastError();
41701 osGetCurrentProcessId(), pFile, pFile->h));
41702 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
41703 "winSync2", pFile->zPath);
41718 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
41723 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
41727 pFile->lastErrno = osGetLastError();
41728 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
41729 "winFileSize", pFile->zPath);
41738 lowerBits = osGetFileSize(pFile->h, &upperBits);
41742 pFile->lastErrno = lastErrno;
41743 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
41744 "winFileSize", pFile->zPath);
41749 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
41791 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
41798 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
41800 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
41808 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
41809 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
41810 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
41814 pFile->lastErrno = osGetLastError();
41817 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
41827 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
41829 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
41833 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
41837 pFile->lastErrno = lastErrno;
41838 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
41839 "winUnlockReadLock", pFile->zPath);
41841 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
41846 ** Lock the file with the lock specified by parameter locktype - one
41860 ** UNLOCKED -> SHARED
41861 ** SHARED -> RESERVED
41862 ** SHARED -> (PENDING) -> EXCLUSIVE
41863 ** RESERVED -> (PENDING) -> EXCLUSIVE
41864 ** PENDING -> EXCLUSIVE
41874 int newLocktype; /* Set pFile->locktype to this value before exiting */
41881 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
41887 if( pFile->locktype>=locktype ){
41888 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
41892 /* Do not allow any kind of write-lock on a read-only database
41894 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
41900 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
41902 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
41908 newLocktype = pFile->locktype;
41909 if( pFile->locktype==NO_LOCK
41910 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
41913 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
41916 ** around problems caused by indexing and/or anti-virus software on
41922 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
41923 pFile->h, cnt, res));
41925 pFile->lastErrno = lastErrno;
41927 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
41928 pFile->h, cnt, sqlite3ErrName(rc)));
41942 assert( pFile->locktype==NO_LOCK );
41954 assert( pFile->locktype==SHARED_LOCK );
41955 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
41973 assert( pFile->locktype>=SHARED_LOCK );
41975 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
41989 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
41998 pFile->lastErrno = lastErrno;
42000 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
42001 pFile->h, locktype, newLocktype));
42003 pFile->locktype = (u8)newLocktype;
42005 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
42012 ** non-zero, otherwise zero.
42019 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
42022 if( pFile->locktype>=RESERVED_LOCK ){
42024 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
42026 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
42028 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
42031 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
42034 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
42035 pFile->h, pResOut, *pResOut));
42044 ** the requested locking level, this routine is a no-op.
42057 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
42058 type = pFile->locktype;
42060 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
42065 "winUnlock", pFile->zPath);
42069 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
42075 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
42077 pFile->locktype = (u8)locktype;
42079 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
42084 ****************************** No-op Locking **********************************
42090 ** This locking mode is appropriate for use on read-only databases
42091 ** (ex: databases that are burned into CD-ROM, for example.) It can
42118 /******************* End of the no-op lock implementation *********************
42123 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42125 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42129 *pArg = (pFile->ctrlFlags & mask)!=0;
42131 pFile->ctrlFlags &= ~mask;
42133 pFile->ctrlFlags |= mask;
42147 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
42150 *(int*)pArg = pFile->locktype;
42151 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42155 *(int*)pArg = (int)pFile->lastErrno;
42156 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42160 pFile->szChunk = *(int *)pArg;
42161 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42165 if( pFile->szChunk>0 ){
42176 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
42179 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42184 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42189 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42193 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
42194 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42209 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42214 *phFile = pFile->h;
42215 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
42221 HANDLE hOldFile = pFile->h;
42222 pFile->h = *phFile;
42225 hOldFile, pFile->h));
42231 int rc = winGetTempname(pFile->pVfs, &zTFile);
42235 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
42248 ** least a 64-bit type. */
42253 *(i64*)pArg = pFile->mmapSizeMax;
42254 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
42255 pFile->mmapSizeMax = newLimit;
42256 if( pFile->mmapSize>0 ){
42258 rc = winMapfile(pFile, -1);
42261 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
42266 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
42276 ** if two files are created in the same file-system directory (i.e.
42291 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
42332 ** log-summary, each thread has its own winFile object, but they all
42334 ** log-summary is opened only once per process.
42342 ** The following fields are read-only after the object is created:
42357 int szRegion; /* Size of shared-memory regions */
42359 u8 isReadonly; /* True if read-only */
42388 ** are read-only thereafter:
42393 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
42420 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
42428 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
42430 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
42431 pFile->hFile.h, lockType, ofst, nByte));
42433 /* Release/Acquire the system-level lock */
42435 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
42440 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
42446 pFile->lastErrno = osGetLastError();
42450 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
42451 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
42452 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
42464 ** This is not a VFS shared-memory method; it is a utility function called
42465 ** by VFS shared-memory methods.
42471 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
42475 if( p->nRef==0 ){
42477 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
42478 for(i=0; i<p->nRegion; i++){
42479 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
42480 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
42483 bRc = osCloseHandle(p->aRegion[i].hMap);
42484 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
42488 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
42490 winClose((sqlite3_file *)&p->hFile);
42496 winDelete(pVfs, p->zFilename, 0);
42500 *pp = p->pNext;
42501 sqlite3_free(p->aRegion);
42504 pp = &p->pNext;
42516 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
42522 if( pShmNode->isReadonly ){
42523 pShmNode->isUnlocked = 1;
42526 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
42529 "winLockSharedMemory", pShmNode->zFilename);
42541 ** Open the shared-memory area associated with database file pDbFd.
42543 ** When opening a new shared-memory file, if no other instances of that
42554 assert( pDbFd->pShm==0 ); /* Not previously opened */
42561 nName = sqlite3Strlen30(pDbFd->zPath);
42567 pNew->zFilename = (char*)&pNew[1];
42568 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
42569 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
42575 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
42579 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
42589 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
42590 pShmNode->pNext = winShmNodeList;
42594 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
42595 if( pShmNode->mutex==0 ){
42601 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
42606 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
42607 (sqlite3_file*)&pShmNode->hFile,
42611 pShmNode->zFilename);
42614 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
42621 p->pShmNode = pShmNode;
42623 p->id = pShmNode->nextShmId++;
42625 pShmNode->nRef++;
42626 pDbFd->pShm = p;
42633 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
42636 sqlite3_mutex_enter(pShmNode->mutex);
42637 p->pNext = pShmNode->pFirst;
42638 pShmNode->pFirst = p;
42639 sqlite3_mutex_leave(pShmNode->mutex);
42645 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
42653 ** Close a connection to shared-memory. Delete the underlying
42660 winFile *pDbFd; /* Database holding shared-memory */
42662 winShmNode *pShmNode; /* The underlying shared-memory file */
42666 p = pDbFd->pShm;
42668 pShmNode = p->pShmNode;
42672 sqlite3_mutex_enter(pShmNode->mutex);
42673 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
42674 *pp = p->pNext;
42678 pDbFd->pShm = 0;
42679 sqlite3_mutex_leave(pShmNode->mutex);
42681 /* If pShmNode->nRef has reached 0, then close the underlying
42682 ** shared-memory file, too */
42684 assert( pShmNode->nRef>0 );
42685 pShmNode->nRef--;
42686 if( pShmNode->nRef==0 ){
42687 winShmPurge(pDbFd->pVfs, deleteFlag);
42695 ** Change the lock state for a shared-memory segment.
42704 winShm *p = pDbFd->pShm; /* The shared memory being locked */
42706 winShmNode *pShmNode = p->pShmNode;
42718 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
42720 sqlite3_mutex_enter(pShmNode->mutex);
42725 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42727 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
42728 allMask |= pX->sharedMask;
42731 /* Unlock the system-level locks */
42740 p->exclMask &= ~mask;
42741 p->sharedMask &= ~mask;
42750 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42751 if( (pX->exclMask & mask)!=0 ){
42755 allShared |= pX->sharedMask;
42769 p->sharedMask |= mask;
42775 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42776 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
42788 assert( (p->sharedMask & mask)==0 );
42789 p->exclMask |= mask;
42793 sqlite3_mutex_leave(pShmNode->mutex);
42794 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
42795 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
42810 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42817 ** shared-memory associated with the database file fd. Shared-memory regions
42818 ** are numbered starting from zero. Each shared-memory region is szRegion
42823 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
42826 ** isWrite is non-zero and the requested shared-memory region has not yet
42829 ** If the shared-memory region has already been allocated or is allocated by
42842 winShm *pShm = pDbFd->pShm;
42851 pShm = pDbFd->pShm;
42853 pShmNode = pShm->pShmNode;
42855 sqlite3_mutex_enter(pShmNode->mutex);
42856 if( pShmNode->isUnlocked ){
42859 pShmNode->isUnlocked = 0;
42861 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
42863 if( pShmNode->nRegion<=iRegion ){
42866 sqlite3_int64 sz; /* Current size of wal-index file */
42868 pShmNode->szRegion = szRegion;
42871 ** Check to see if it has been allocated (i.e. if the wal-index file is
42874 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
42877 "winShmMap1", pDbFd->zPath);
42885 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
42889 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
42892 "winShmMap2", pDbFd->zPath);
42899 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
42905 pShmNode->aRegion = apNew;
42907 if( pShmNode->isReadonly ){
42912 while( pShmNode->nRegion<=iRegion ){
42913 HANDLE hMap = NULL; /* file-mapping handle */
42917 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
42921 hMap = osCreateFileMappingW(pShmNode->hFile.h,
42925 hMap = osCreateFileMappingA(pShmNode->hFile.h,
42929 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
42930 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
42933 int iOffset = pShmNode->nRegion*szRegion;
42937 iOffset - iOffsetShift, szRegion + iOffsetShift
42941 0, iOffset - iOffsetShift, szRegion + iOffsetShift
42944 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
42945 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
42949 pShmNode->lastErrno = osGetLastError();
42950 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
42951 "winShmMap3", pDbFd->zPath);
42956 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
42957 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
42958 pShmNode->nRegion++;
42963 if( pShmNode->nRegion>iRegion ){
42966 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
42971 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
42972 sqlite3_mutex_leave(pShmNode->mutex);
42989 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
42991 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
42992 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
42993 if( pFile->pMapRegion ){
42994 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
42995 pFile->lastErrno = osGetLastError();
42996 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
42998 pFile->pMapRegion));
42999 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43000 "winUnmapfile1", pFile->zPath);
43002 pFile->pMapRegion = 0;
43003 pFile->mmapSize = 0;
43004 pFile->mmapSizeActual = 0;
43006 if( pFile->hMap!=NULL ){
43007 if( !osCloseHandle(pFile->hMap) ){
43008 pFile->lastErrno = osGetLastError();
43009 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
43010 osGetCurrentProcessId(), pFile, pFile->hMap));
43011 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43012 "winUnmapfile2", pFile->zPath);
43014 pFile->hMap = NULL;
43016 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
43022 ** Memory map or remap the file opened by file-descriptor pFd (if the file
43025 ** outstanding xFetch() references to it, this function is a no-op.
43027 ** If parameter nByte is non-negative, then it is the requested size of
43041 assert( nMap>=0 || pFd->nFetchOut==0 );
43042 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
43045 if( pFd->nFetchOut>0 ) return SQLITE_OK;
43050 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
43055 if( nMap>pFd->mmapSizeMax ){
43056 nMap = pFd->mmapSizeMax;
43058 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
43060 if( nMap==0 && pFd->mmapSize>0 ){
43063 if( nMap!=pFd->mmapSize ){
43070 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
43076 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
43078 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
43082 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
43086 if( pFd->hMap==NULL ){
43087 pFd->lastErrno = osGetLastError();
43088 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
43089 "winMapfile1", pFd->zPath);
43091 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
43098 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
43100 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
43103 osCloseHandle(pFd->hMap);
43104 pFd->hMap = NULL;
43105 pFd->lastErrno = osGetLastError();
43106 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
43107 "winMapfile2", pFd->zPath);
43109 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
43113 pFd->pMapRegion = pNew;
43114 pFd->mmapSize = nMap;
43115 pFd->mmapSizeActual = nMap;
43118 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
43146 if( pFd->mmapSizeMax>0 ){
43147 if( pFd->pMapRegion==0 ){
43148 int rc = winMapfile(pFd, -1);
43155 if( pFd->mmapSize >= iOff+nAmt ){
43156 *pp = &((u8 *)pFd->pMapRegion)[iOff];
43157 pFd->nFetchOut++;
43168 ** If the third argument is non-NULL, then this function releases a
43184 assert( (p==0)==(pFd->nFetchOut==0) );
43187 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
43193 pFd->nFetchOut--;
43197 ** is unnecessary can be omitted - potentially improving
43202 assert( pFd->nFetchOut>=0 );
43290 ** supports for filenames into UTF-8. Space to hold the result is
43309 ** Convert a UTF-8 filename into whatever form the underlying
43329 ** This function returns non-zero if the specified UTF-8 string buffer
43337 if( winIsDirSep(zBuf[nLen-1]) ){
43363 /* It's odd to simulate an io-error here, but really this is just
43364 ** using the io-error infrastructure to test that SQLite handles this
43372 nMax = pVfs->mxPathname; nBuf = nMax + 2;
43375 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43383 nDir = nMax - (nPre + 15);
43388 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
43393 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
43433 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43446 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43454 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
43461 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
43467 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43485 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43491 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
43503 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43513 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43518 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
43528 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43542 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
43560 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
43564 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
43569 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
43575 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
43621 const char *zName, /* Name of the file (UTF-8) */
43638 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
43687 /* Assert that the upper layer has set one of the "file-type" flags. */
43696 pFile->h = INVALID_HANDLE_VALUE;
43718 /* Database filenames are double-zero terminated if they are not
43860 pFile->lastErrno = lastErrno;
43861 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
43878 pAppData = (winVfsAppData*)pVfs->pAppData;
43883 && ((pAppData==NULL) || !pAppData->bNoLock)
43889 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
43894 pFile->zDeleteOnClose = zConverted;
43902 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
43903 pFile->pVfs = pVfs;
43904 pFile->h = h;
43906 pFile->ctrlFlags |= WINFILE_RDONLY;
43909 pFile->ctrlFlags |= WINFILE_PSOW;
43911 pFile->lastErrno = NO_ERROR;
43912 pFile->zPath = zName;
43914 pFile->hMap = NULL;
43915 pFile->pMapRegion = 0;
43916 pFile->mmapSize = 0;
43917 pFile->mmapSizeActual = 0;
43918 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
44074 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
44120 ** Returns non-zero if the specified path name starts with a drive letter
44130 ** Returns non-zero if the specified path name should be used verbatim. If
44131 ** non-zero is returned from this function, the calling function must simply
44132 ** use the provided path name verbatim -OR- resolve it into a full path name
44168 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
44193 assert( nFull>=pVfs->mxPathname );
44201 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
44207 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
44217 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
44223 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
44229 zRelative, zOut, pVfs->mxPathname+1)<0 ){
44239 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
44258 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
44261 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
44267 /* It's odd to simulate an io-error here, but really this is just
44268 ** using the io-error infrastructure to test that SQLite handles this
44280 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
44341 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
44358 int nFull = pVfs->mxPathname+1;
44433 for(j=0, k=p->i; j<sz; j++){
44434 p->a[k++] ^= x[j];
44435 if( k>=p->na ) k = 0;
44437 p->i = k;
44438 p->nXor += sz;
44509 ** The following variable, if set to a non-zero value, is interpreted as
44528 /* FILETIME structure is a 64-bit value representing the number of
44529 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
44536 /* 2^32 - to avoid use of LL and warnings in gcc */
44586 ** buffer with a nul-terminated UTF-8 encoded error message
44592 ** is zero if the error message fits in the buffer, or non-zero
44593 ** otherwise (if the message was truncated). If non-zero is returned,
44594 ** then it is not necessary to include the nul-terminator character
44651 "win32-longpath", /* zName */
44676 "win32-none", /* zName */
44701 "win32-longpath-none", /* zName */
44722 /* Double-check that the aSyscall[] array has been constructed
44776 ** This file implements an object that represents a fixed-length
44780 ** journalled during a transaction, or which pages have the "dont-write"
44809 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
44826 ** sub-dividing and re-hashing. */
44831 ** no fewer collisions than the no-op *1. */
44851 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
44860 u32 nSet; /* Number of bits that are set - only valid for aHash
44884 p->iSize = iSize;
44890 ** Check to see if the i-th bit is set. Return true or false.
44896 i--;
44897 if( i>=p->iSize ) return 0;
44898 while( p->iDivisor ){
44899 u32 bin = i/p->iDivisor;
44900 i = i%p->iDivisor;
44901 p = p->u.apSub[bin];
44906 if( p->iSize<=BITVEC_NBIT ){
44907 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
44910 while( p->u.aHash[h] ){
44911 if( p->u.aHash[h]==i ) return 1;
44922 ** Set the i-th bit. Return 0 on success and an error code if
44925 ** This routine might cause sub-bitmaps to be allocated. Failing
44926 ** to get the memory needed to hold the sub-bitmap is the only
44937 assert( i<=p->iSize );
44938 i--;
44939 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
44940 u32 bin = i/p->iDivisor;
44941 i = i%p->iDivisor;
44942 if( p->u.apSub[bin]==0 ){
44943 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
44944 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
44946 p = p->u.apSub[bin];
44948 if( p->iSize<=BITVEC_NBIT ){
44949 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
44955 /* worring about sub-dividing and re-hashing. */
44956 if( !p->u.aHash[h] ){
44957 if (p->nSet<(BITVEC_NINT-1)) {
44966 if( p->u.aHash[h]==i ) return SQLITE_OK;
44969 } while( p->u.aHash[h] );
44974 if( p->nSet>=BITVEC_MXHASH ){
44977 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
44981 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
44982 memset(p->u.apSub, 0, sizeof(p->u.apSub));
44983 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
44993 p->nSet++;
44994 p->u.aHash[h] = i;
44999 ** Clear the i-th bit.
45007 i--;
45008 while( p->iDivisor ){
45009 u32 bin = i/p->iDivisor;
45010 i = i%p->iDivisor;
45011 p = p->u.apSub[bin];
45016 if( p->iSize<=BITVEC_NBIT ){
45017 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
45021 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
45022 memset(p->u.aHash, 0, sizeof(p->u.aHash));
45023 p->nSet = 0;
45026 u32 h = BITVEC_HASH(aiValues[j]-1);
45027 p->nSet++;
45028 while( p->u.aHash[h] ){
45032 p->u.aHash[h] = aiValues[j];
45043 if( p->iDivisor ){
45046 sqlite3BitvecDestroy(p->u.apSub[i]);
45057 return p->iSize;
45099 ** If a memory allocation error occurs, return -1.
45104 int rc = -1;
45127 i = aOp[pc+2] - 1;
45139 if( (--aOp[pc+1]) > 0 ) nx = 0;
45155 ** match (rc==0). Change rc to non-zero if a discrepancy
45160 + (sqlite3BitvecSize(pBitvec) - sz);
45206 ** such that p was added to the list more recently than p->pDirtyNext.
45211 ** page to eject from the cache mid-transaction. It is better to eject
45255 if( pCache->pCache==0 ) return;
45259 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
45261 pPg = (PgHdr*)pLower->pExtra;
45262 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
45263 a = (unsigned char *)pLower->pBuf;
45266 if( pPg->pPage==0 ){
45267 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
45289 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
45290 pCache = pPg->pCache;
45292 if( pPg->flags & PGHDR_CLEAN ){
45293 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
45294 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
45295 assert( pCache->pDirtyTail!=pPg );
45298 if( pPg->flags & PGHDR_WRITEABLE ){
45299 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
45337 PCache *p = pPage->pCache;
45341 pPage->pgno));
45343 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
45344 assert( pPage->pDirtyPrev || pPage==p->pDirty );
45347 if( p->pSynced==pPage ){
45348 p->pSynced = pPage->pDirtyPrev;
45351 if( pPage->pDirtyNext ){
45352 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
45354 assert( pPage==p->pDirtyTail );
45355 p->pDirtyTail = pPage->pDirtyPrev;
45357 if( pPage->pDirtyPrev ){
45358 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
45364 assert( pPage==p->pDirty );
45365 p->pDirty = pPage->pDirtyNext;
45366 assert( p->bPurgeable || p->eCreate==2 );
45367 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
45368 assert( p->bPurgeable==0 || p->eCreate==1 );
45369 p->eCreate = 2;
45374 pPage->pDirtyPrev = 0;
45375 pPage->pDirtyNext = p->pDirty;
45376 if( pPage->pDirtyNext ){
45377 assert( pPage->pDirtyNext->pDirtyPrev==0 );
45378 pPage->pDirtyNext->pDirtyPrev = pPage;
45380 p->pDirtyTail = pPage;
45381 if( p->bPurgeable ){
45382 assert( p->eCreate==2 );
45383 p->eCreate = 1;
45386 p->pDirty = pPage;
45392 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
45393 if( !p->pSynced
45394 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
45396 p->pSynced = pPage;
45404 ** being used for an in-memory database, this function is a no-op.
45407 if( p->pCache->bPurgeable ){
45408 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
45409 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
45410 pcacheDump(p->pCache);
45415 ** Compute the number of pages of cache requested. p->szCache is the
45419 if( p->szCache>=0 ){
45420 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
45422 return p->szCache;
45424 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
45427 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
45438 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
45439 ** built-in default page cache is used instead of the application defined
45447 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
45478 p->szPage = 1;
45479 p->szExtra = szExtra;
45481 p->bPurgeable = bPurgeable;
45482 p->eCreate = 2;
45483 p->xStress = xStress;
45484 p->pStress = pStress;
45485 p->szCache = 100;
45486 p->szSpill = 1;
45496 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
45497 if( pCache->szPage ){
45500 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
45501 pCache->bPurgeable
45505 if( pCache->pCache ){
45506 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
45508 pCache->pCache = pNew;
45509 pCache->szPage = szPage;
45548 assert( pCache->pCache!=0 );
45550 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
45559 eCreate = createFlag & pCache->eCreate;
45561 assert( createFlag==0 || pCache->eCreate==eCreate );
45562 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
45563 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
45586 if( pCache->eCreate==2 ) return 0;
45588 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
45589 /* Find a dirty page to write-out and recycle. First try to find a
45590 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
45598 for(pPg=pCache->pSynced;
45599 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
45600 pPg=pPg->pDirtyPrev
45602 pCache->pSynced = pPg;
45604 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
45610 "spill page %d making room for %d - cache used: %d/%d",
45611 pPg->pgno, pgno,
45612 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
45615 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
45616 rc = pCache->xStress(pCache->pStress, pPg);
45623 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
45643 pPgHdr = (PgHdr*)pPage->pExtra;
45644 assert( pPgHdr->pPage==0 );
45645 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
45646 pPgHdr->pPage = pPage;
45647 pPgHdr->pData = pPage->pBuf;
45648 pPgHdr->pExtra = (void *)&pPgHdr[1];
45649 memset(pPgHdr->pExtra, 0, 8);
45650 pPgHdr->pCache = pCache;
45651 pPgHdr->pgno = pgno;
45652 pPgHdr->flags = PGHDR_CLEAN;
45670 pPgHdr = (PgHdr *)pPage->pExtra;
45672 if( !pPgHdr->pPage ){
45675 pCache->nRefSum++;
45676 pPgHdr->nRef++;
45686 assert( p->nRef>0 );
45687 p->pCache->nRefSum--;
45688 if( (--p->nRef)==0 ){
45689 if( p->flags&PGHDR_CLEAN ){
45701 assert(p->nRef>0);
45703 p->nRef++;
45704 p->pCache->nRefSum++;
45713 assert( p->nRef==1 );
45715 if( p->flags&PGHDR_DIRTY ){
45718 p->pCache->nRefSum--;
45719 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
45727 assert( p->nRef>0 );
45729 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
45730 p->flags &= ~PGHDR_DONT_WRITE;
45731 if( p->flags & PGHDR_CLEAN ){
45732 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
45733 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
45734 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
45747 assert( (p->flags & PGHDR_DIRTY)!=0 );
45748 assert( (p->flags & PGHDR_CLEAN)==0 );
45750 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
45751 p->flags |= PGHDR_CLEAN;
45752 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
45754 if( p->nRef==0 ){
45764 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
45765 while( (p = pCache->pDirty)!=0 ){
45775 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
45776 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45777 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
45779 pCache->pSynced = pCache->pDirtyTail;
45787 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45788 p->flags &= ~PGHDR_NEED_SYNC;
45790 pCache->pSynced = pCache->pDirtyTail;
45797 PCache *pCache = p->pCache;
45798 assert( p->nRef>0 );
45801 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
45802 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
45803 p->pgno = newPgno;
45804 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
45819 if( pCache->pCache ){
45823 for(p=pCache->pDirty; p; p=pNext){
45824 pNext = p->pDirtyNext;
45829 assert( p->pgno>0 );
45830 if( p->pgno>pgno ){
45831 assert( p->flags&PGHDR_DIRTY );
45835 if( pgno==0 && pCache->nRefSum ){
45837 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
45839 ** pCache->nRefSum>0 */
45840 memset(pPage1->pBuf, 0, pCache->szPage);
45844 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
45852 assert( pCache->pCache!=0 );
45854 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
45873 if( pA->pgno<pB->pgno ){
45874 pTail->pDirty = pA;
45876 pA = pA->pDirty;
45878 pTail->pDirty = pB;
45882 pTail->pDirty = pB;
45884 pB = pB->pDirty;
45886 pTail->pDirty = pA;
45911 pIn = p->pDirty;
45912 p->pDirty = 0;
45913 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
45922 if( NEVER(i==N_SORT_BUCKET-1) ){
45942 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45943 p->pDirty = p->pDirtyNext;
45945 return pcacheSortDirtyList(pCache->pDirty);
45955 return pCache->nRefSum;
45962 return p->nRef;
45969 assert( pCache->pCache!=0 );
45970 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
45975 ** Get the suggested cache-size value.
45983 ** Set the suggested cache-size value.
45986 assert( pCache->pCache!=0 );
45987 pCache->szCache = mxPage;
45988 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
45993 ** Set the suggested cache-spill value. Make no changes if if the
45994 ** argument is zero. Return the effective cache-spill size, which will
45999 assert( p->pCache!=0 );
46002 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
46004 p->szSpill = mxPage;
46007 if( res<p->szSpill ) res = p->szSpill;
46015 assert( pCache->pCache!=0 );
46016 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
46021 ** in the page-cache hierarchy.
46033 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
46045 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
46073 ** -------------------------------------------------------------
46075 ** -------------------------------------------------------------
46091 ** size can vary according to architecture, compile-time options, and
46099 ** power of two, and leaving the rounded-up space unused.
46111 ** (1) The general-purpose memory allocator - sqlite3Malloc()
46112 ** (2) Global page-cache memory provided using sqlite3_config() with
46114 ** (3) PCache-local bulk allocation.
46124 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
46128 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
46145 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
46162 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
46163 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
46191 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
46197 ** open database file (including each in-memory database and each
46212 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
46229 PgHdr1 *pFree; /* List of unused pcache-local pages */
46230 void *pBulk; /* Bulk memory used by pcache-local */
46264 ** reading because (1) most platforms read a 32-bit integer atomically and
46281 # define pcache1EnterMutex(X) assert((X)->mutex==0)
46282 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
46285 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
46286 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
46296 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
46315 while( n-- ){
46317 p->pNext = pcache1.pFree;
46326 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
46327 ** true if pCache->pFree ends up containing one or more free pages.
46334 if( pCache->nMax<3 ) return 0;
46337 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
46339 szBulk = -1024 * (i64)pcache1.nInitPage;
46341 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
46342 szBulk = pCache->szAlloc*(i64)pCache->nMax;
46344 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
46347 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
46349 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
46350 pX->page.pBuf = zBulk;
46351 pX->page.pExtra = &pX[1];
46352 pX->isBulkLocal = 1;
46353 pX->isAnchor = 0;
46354 pX->pNext = pCache->pFree;
46355 pCache->pFree = pX;
46356 zBulk += pCache->szAlloc;
46357 }while( --nBulk );
46359 return pCache->pFree!=0;
46378 pcache1.pFree = pcache1.pFree->pNext;
46379 pcache1.nFreeSlot--;
46416 pSlot->pNext = pcache1.pFree;
46463 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46464 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
46465 p = pCache->pFree;
46466 pCache->pFree = p->pNext;
46467 p->pNext = 0;
46474 assert( pCache->pGroup==&pcache1.grp );
46475 pcache1LeaveMutex(pCache->pGroup);
46479 pPg = pcache1Alloc(pCache->szPage);
46480 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
46487 pPg = pcache1Alloc(pCache->szAlloc);
46488 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
46492 pcache1EnterMutex(pCache->pGroup);
46495 p->page.pBuf = pPg;
46496 p->page.pExtra = &p[1];
46497 p->isBulkLocal = 0;
46498 p->isAnchor = 0;
46500 (*pCache->pnPurgeable)++;
46510 pCache = p->pCache;
46511 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
46512 if( p->isBulkLocal ){
46513 p->pNext = pCache->pFree;
46514 pCache->pFree = p;
46516 pcache1Free(p->page.pBuf);
46521 (*pCache->pnPurgeable)--;
46558 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
46579 assert( sqlite3_mutex_held(p->pGroup->mutex) );
46581 nNew = p->nHash*2;
46586 pcache1LeaveMutex(p->pGroup);
46587 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
46589 if( p->nHash ){ sqlite3EndBenignMalloc(); }
46590 pcache1EnterMutex(p->pGroup);
46592 for(i=0; i<p->nHash; i++){
46594 PgHdr1 *pNext = p->apHash[i];
46596 unsigned int h = pPage->iKey % nNew;
46597 pNext = pPage->pNext;
46598 pPage->pNext = apNew[h];
46602 sqlite3_free(p->apHash);
46603 p->apHash = apNew;
46604 p->nHash = nNew;
46611 ** LRU list, then this function is a no-op.
46618 assert( pPage->pLruNext );
46619 assert( pPage->pLruPrev );
46620 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
46621 pPage->pLruPrev->pLruNext = pPage->pLruNext;
46622 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
46623 pPage->pLruNext = 0;
46624 pPage->pLruPrev = 0;
46625 assert( pPage->isAnchor==0 );
46626 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
46627 pPage->pCache->nRecyclable--;
46641 PCache1 *pCache = pPage->pCache;
46644 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46645 h = pPage->iKey % pCache->nHash;
46646 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
46647 *pp = (*pp)->pNext;
46649 pCache->nPage--;
46658 PGroup *pGroup = pCache->pGroup;
46660 assert( sqlite3_mutex_held(pGroup->mutex) );
46661 while( pGroup->nPurgeable>pGroup->nMaxPage
46662 && (p=pGroup->lru.pLruPrev)->isAnchor==0
46664 assert( p->pCache->pGroup==pGroup );
46669 if( pCache->nPage==0 && pCache->pBulk ){
46670 sqlite3_free(pCache->pBulk);
46671 pCache->pBulk = pCache->pFree = 0;
46686 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
46688 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46689 assert( pCache->iMaxKey >= iLimit );
46690 assert( pCache->nHash > 0 );
46691 if( pCache->iMaxKey - iLimit < pCache->nHash ){
46696 h = iLimit % pCache->nHash;
46697 iStop = pCache->iMaxKey % pCache->nHash;
46698 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
46702 h = pCache->nHash/2;
46703 iStop = h - 1;
46708 assert( h<pCache->nHash );
46709 pp = &pCache->apHash[h];
46711 if( pPage->iKey>=iLimit ){
46712 pCache->nPage--;
46713 *pp = pPage->pNext;
46717 pp = &pPage->pNext;
46722 h = (h+1) % pCache->nHash;
46724 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
46741 ** private PGroup (mode-1). pcache1.separateCache is false if the single
46742 ** PGroup in pcache1.grp is used for all page caches (mode-2).
46744 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
46746 ** * Use a unified cache in single-threaded applications that have
46747 ** configured a start-time buffer for use as page-cache memory using
46748 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
46751 ** * Otherwise use separate caches (mode-1)
46805 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
46813 pGroup->mxPinned = 10;
46817 if( pGroup->lru.isAnchor==0 ){
46818 pGroup->lru.isAnchor = 1;
46819 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
46821 pCache->pGroup = pGroup;
46822 pCache->szPage = szPage;
46823 pCache->szExtra = szExtra;
46824 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
46825 pCache->bPurgeable = (bPurgeable ? 1 : 0);
46829 pCache->nMin = 10;
46830 pGroup->nMinPage += pCache->nMin;
46831 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
46832 pCache->pnPurgeable = &pGroup->nPurgeable;
46835 pCache->pnPurgeable = &dummyCurrentPage;
46838 if( pCache->nHash==0 ){
46853 if( pCache->bPurgeable ){
46854 PGroup *pGroup = pCache->pGroup;
46856 pGroup->nMaxPage += (nMax - pCache->nMax);
46857 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
46858 pCache->nMax = nMax;
46859 pCache->n90pct = pCache->nMax*9/10;
46872 if( pCache->bPurgeable ){
46873 PGroup *pGroup = pCache->pGroup;
46876 savedMaxPage = pGroup->nMaxPage;
46877 pGroup->nMaxPage = 0;
46879 pGroup->nMaxPage = savedMaxPage;
46890 pcache1EnterMutex(pCache->pGroup);
46891 n = pCache->nPage;
46892 pcache1LeaveMutex(pCache->pGroup);
46911 PGroup *pGroup = pCache->pGroup;
46915 assert( pCache->nPage >= pCache->nRecyclable );
46916 nPinned = pCache->nPage - pCache->nRecyclable;
46917 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
46918 assert( pCache->n90pct == pCache->nMax*9/10 );
46920 nPinned>=pGroup->mxPinned
46921 || nPinned>=pCache->n90pct
46922 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
46927 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
46928 assert( pCache->nHash>0 && pCache->apHash );
46931 if( pCache->bPurgeable
46932 && !pGroup->lru.pLruPrev->isAnchor
46933 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
46936 pPage = pGroup->lru.pLruPrev;
46940 pOther = pPage->pCache;
46941 if( pOther->szAlloc != pCache->szAlloc ){
46945 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
46957 unsigned int h = iKey % pCache->nHash;
46958 pCache->nPage++;
46959 pPage->iKey = iKey;
46960 pPage->pNext = pCache->apHash[h];
46961 pPage->pCache = pCache;
46962 pPage->pLruPrev = 0;
46963 pPage->pLruNext = 0;
46964 *(void **)pPage->page.pExtra = 0;
46965 pCache->apHash[h] = pPage;
46966 if( iKey>pCache->iMaxKey ){
46967 pCache->iMaxKey = iKey;
46983 ** For a non-purgeable cache (a cache used as the storage for an in-memory
46986 ** a non-purgeable cache.
47029 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
47041 pPage = pCache->apHash[iKey % pCache->nHash];
47042 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
47070 pcache1EnterMutex(pCache->pGroup);
47072 assert( pPage==0 || pCache->iMaxKey>=iKey );
47073 pcache1LeaveMutex(pCache->pGroup);
47087 assert( pCache->bPurgeable || createFlag!=1 );
47088 assert( pCache->bPurgeable || pCache->nMin==0 );
47089 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
47090 assert( pCache->nMin==0 || pCache->bPurgeable );
47091 assert( pCache->nHash>0 );
47093 if( pCache->pGroup->mutex ){
47115 PGroup *pGroup = pCache->pGroup;
47117 assert( pPage->pCache==pCache );
47123 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
47126 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
47130 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
47131 pPage->pLruPrev = &pGroup->lru;
47132 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
47134 pCache->nRecyclable++;
47137 pcache1LeaveMutex(pCache->pGroup);
47153 assert( pPage->iKey==iOld );
47154 assert( pPage->pCache==pCache );
47156 pcache1EnterMutex(pCache->pGroup);
47158 h = iOld%pCache->nHash;
47159 pp = &pCache->apHash[h];
47161 pp = &(*pp)->pNext;
47163 *pp = pPage->pNext;
47165 h = iNew%pCache->nHash;
47166 pPage->iKey = iNew;
47167 pPage->pNext = pCache->apHash[h];
47168 pCache->apHash[h] = pPage;
47169 if( iNew>pCache->iMaxKey ){
47170 pCache->iMaxKey = iNew;
47173 pcache1LeaveMutex(pCache->pGroup);
47185 pcache1EnterMutex(pCache->pGroup);
47186 if( iLimit<=pCache->iMaxKey ){
47188 pCache->iMaxKey = iLimit-1;
47190 pcache1LeaveMutex(pCache->pGroup);
47200 PGroup *pGroup = pCache->pGroup;
47201 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
47203 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
47204 assert( pGroup->nMaxPage >= pCache->nMax );
47205 pGroup->nMaxPage -= pCache->nMax;
47206 assert( pGroup->nMinPage >= pCache->nMin );
47207 pGroup->nMinPage -= pCache->nMin;
47208 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
47211 sqlite3_free(pCache->pBulk);
47212 sqlite3_free(pCache->apHash);
47272 && p->isAnchor==0
47274 nFree += pcache1MemSize(p->page.pBuf);
47301 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
47361 ** a non-zero batch number, it will see all prior INSERTs.
47389 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
47455 p->pChunk = 0;
47456 p->db = db;
47457 p->pEntry = 0;
47458 p->pLast = 0;
47459 p->pForest = 0;
47460 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
47461 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
47462 p->rsFlags = ROWSET_SORTED;
47463 p->iBatch = 0;
47474 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
47475 pNextChunk = pChunk->pNextChunk;
47476 sqlite3DbFree(p->db, pChunk);
47478 p->pChunk = 0;
47479 p->nFresh = 0;
47480 p->pEntry = 0;
47481 p->pLast = 0;
47482 p->pForest = 0;
47483 p->rsFlags = ROWSET_SORTED;
47491 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
47496 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
47500 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
47504 pNew->pNextChunk = p->pChunk;
47505 p->pChunk = pNew;
47506 p->pFresh = pNew->aEntry;
47507 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
47509 p->nFresh--;
47510 return p->pFresh++;
47524 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
47528 pEntry->v = rowid;
47529 pEntry->pRight = 0;
47530 pLast = p->pLast;
47532 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
47535 p->rsFlags &= ~ROWSET_SORTED;
47537 pLast->pRight = pEntry;
47539 p->pEntry = pEntry;
47541 p->pLast = pEntry;
47560 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
47561 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
47562 if( pA->v<=pB->v ){
47563 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
47564 pA = pA->pRight;
47566 pTail->pRight = pB;
47570 pTail = pTail->pRight = pB;
47571 pB = pB->pRight;
47573 pTail->pRight = pA;
47591 pNext = pIn->pRight;
47592 pIn->pRight = 0;
47620 if( pIn->pLeft ){
47622 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
47623 p->pRight = pIn;
47627 if( pIn->pRight ){
47628 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
47632 assert( (*ppLast)->pRight==0 );
47655 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
47659 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
47663 pLeft = rowSetNDeepTree(ppList, iDepth-1);
47665 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
47670 p->pLeft = pLeft;
47671 *ppList = p->pRight;
47672 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
47675 *ppList = p->pRight;
47676 p->pLeft = p->pRight = 0;
47692 pList = p->pRight;
47693 p->pLeft = p->pRight = 0;
47697 pList = p->pRight;
47698 p->pLeft = pLeft;
47699 p->pRight = rowSetNDeepTree(&pList, iDepth);
47719 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
47722 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47723 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47724 p->pEntry = rowSetEntrySort(p->pEntry);
47726 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
47730 if( p->pEntry ){
47731 *pRowid = p->pEntry->v;
47732 p->pEntry = p->pEntry->pRight;
47733 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
47748 ** on pRowSet->pEntry, then sort those entries into the forest at
47749 ** pRowSet->pForest so that they can be tested.
47755 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
47760 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
47761 p = pRowSet->pEntry;
47763 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
47764 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47768 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
47769 ppPrevTree = &pTree->pRight;
47770 if( pTree->pLeft==0 ){
47771 pTree->pLeft = rowSetListToTree(p);
47775 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
47776 pTree->pLeft = 0;
47783 pTree->v = 0;
47784 pTree->pRight = 0;
47785 pTree->pLeft = rowSetListToTree(p);
47788 pRowSet->pEntry = 0;
47789 pRowSet->pLast = 0;
47790 pRowSet->rsFlags |= ROWSET_SORTED;
47792 pRowSet->iBatch = iBatch;
47798 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
47799 p = pTree->pLeft;
47801 if( p->v<iRowid ){
47802 p = p->pRight;
47803 }else if( p->v>iRowid ){
47804 p = p->pLeft;
47850 ** This header file defines the interface to the write-ahead logging
47890 /* Connection to a write-ahead log (WAL) file.
47895 /* Open and close a connection to a write-ahead log. */
47903 ** snapshot is like a read-transaction. It is the state of the database
47912 /* Read a page from the write-ahead log, if it is present. */
47939 Wal *pWal, /* Write-ahead log connection */
47963 /* Return true if the argument is non-NULL and the WAL module is using
47964 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
47965 ** WAL module is using shared-memory, return false.
47977 ** stored in each frame (i.e. the db page-size when the WAL was created).
48056 ** method is a no-op, but that does not change the fact the SQLite will
48067 ** (11) A database file is well-formed at the beginning and at the conclusion
48091 ** to print out file-descriptors.
48094 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
48097 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
48105 ** OPEN <------+------+
48108 ** +---------> READER-------+ |
48111 ** |<-------WRITER_LOCKED------> ERROR
48114 ** |<------WRITER_CACHEMOD-------->|
48117 ** |<-------WRITER_DBMOD---------->|
48120 ** +<------WRITER_FINISHED-------->+
48125 ** OPEN -> READER [sqlite3PagerSharedLock]
48126 ** READER -> OPEN [pager_unlock]
48128 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
48129 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
48130 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
48131 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
48132 ** WRITER_*** -> READER [pager_end_transaction]
48134 ** WRITER_*** -> ERROR [pager_error]
48135 ** ERROR -> OPEN [pager_unlock]
48141 ** state - the file may or may not be locked and the database size is
48151 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
48152 ** was) in exclusive-locking mode, a user-level read transaction is
48156 ** it opens a read-transaction on the database and returns to state
48157 ** OPEN after the read-transaction is completed. However a connection
48159 ** this state even after the read-transaction is closed. The only way
48163 ** * A read transaction may be active (but a write-transaction cannot).
48165 ** * The dbSize variable may be trusted (even if a user-level read
48169 ** * Even if a read-transaction is not open, it is guaranteed that
48170 ** there is no hot-journal in the file-system.
48174 ** The pager moves to this state from READER when a write-transaction
48176 ** required to start a write-transaction are held, but no actual
48191 ** * If the connection is open in rollback-mode, a RESERVED or greater
48193 ** * If the connection is open in WAL-mode, a WAL write transaction
48232 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
48248 ** The ERROR state is entered when an IO or disk-full error (including
48250 ** difficult to be sure that the in-memory pager state (cache contents,
48251 ** db size etc.) are consistent with the contents of the file-system.
48253 ** Temporary pager files may enter the ERROR state, but in-memory pagers
48257 ** the contents of the page-cache may be left in an inconsistent state.
48269 ** page-cache and any other in-memory state at the same time. Everything
48270 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
48271 ** when a read-transaction is next opened on the pager (transitioning
48287 ** In other cases, the error is returned to the b-tree layer. The b-tree
48291 ** Condition (3) is necessary because it can be triggered by a read-only
48293 ** code were simply returned to the user, the b-tree layer would not
48295 ** read-only statement cannot leave the pager in an internally inconsistent
48301 ** * The pager is not an in-memory pager.
48311 ** state. There are two exceptions: immediately after exclusive-mode has
48327 ** following locking-states, according to the lock currently held on
48335 ** pagerUnlockDb() take a conservative approach - eLock is always updated
48346 ** from ERROR to OPEN state. At this point there may be a hot-journal file
48347 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
48351 ** of hot-journal detection.
48357 ** a hot-journal may be mistaken for a journal being created by an active
48364 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
48365 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
48366 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
48380 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
48382 if( P->xCodec==0 ){ O=(char*)D; }else \
48383 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
48385 # define CODEC1(P,D,N,X,E) /* NO-OP */
48405 ** set to 0. If a journal-header is written into the main journal while
48408 ** journal before the journal-header. This is required during savepoint
48417 Pgno iSubRec; /* Index of first record in sub-journal */
48441 ** For a real on-disk database, the current lock held on the database file -
48444 ** For a temporary or in-memory database (neither of which require any
48456 ** This boolean variable is used to make sure that the change-counter
48457 ** (the 4-byte header field at byte offset 24 of the database file) is
48460 ** It is set to true when the change-counter field is updated, which
48465 ** updating the change-counter is omitted for the current transaction.
48468 ** need only update the change-counter once, for the first transaction
48474 ** (or may not) specify a master-journal name to be written into the
48477 ** Whether or not a journal file contains a master-journal pointer affects
48480 ** If a journal file does not contain a master-journal pointer, it is
48482 ** it does contain a master-journal pointer the journal file is finalized
48487 ** simply by overwriting the first journal-header with zeroes, as the
48488 ** master journal pointer could interfere with hot-journal rollback of any
48498 ** This variables control the behavior of cache-spills (calls made by
48500 ** to the file-system in order to free up memory).
48512 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
48513 ** is larger than the database page-size in order to prevent a journal sync
48518 ** This is a boolean variable. If true, then any required sub-journal
48519 ** is opened as an in-memory journal file. If false, then in-memory
48520 ** sub-journals are only used for in-memory pager files.
48523 ** write-transaction is opened.
48534 ** is not an integer multiple of the page-size, the value stored in
48535 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
48537 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
48540 ** During a write-transaction, if pages with page-numbers greater than
48551 ** Throughout a write-transaction, dbFileSize contains the size of
48553 ** write-transaction is first opened, and updated when VFS calls are made
48573 ** write-transaction is opened (at the same time as dbFileSize and
48576 ** size-hint passed to the method call. See pager_write_pagelist() for
48584 ** sub-codes.
48610 u8 readOnly; /* True for a read-only database */
48624 u8 changeCountDone; /* Set after incrementing the change-counter */
48625 u8 setMaster; /* True if a m-j name has been written to jrnl */
48626 u8 doNotSpill; /* Do not spill the cache when non-zero */
48627 u8 subjInMemory; /* True to use in-memory sub-journals */
48635 int nRec; /* Pages journalled since last j-header written */
48636 u32 cksumInit; /* Quasi-random value added to every checksum */
48637 u32 nSubRec; /* Number of records written to sub-journal */
48641 sqlite3_file *sjfd; /* File descriptor for sub-journal */
48654 ** End of the routinely-changing class members
48657 u16 nExtra; /* Add this many bytes to each in-memory page */
48683 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
48684 char *zWal; /* File name for write-ahead log */
48700 ** a non-testing build. These variables are not thread-safe.
48719 ** written, semi-random garbage data might appear in the journal
48726 ** of a 32-bit checksum on each page of data. The checksum covers both
48727 ** the page number and the pPager->pageSize bytes of data for the page.
48728 ** This cksum is initialized to a 32-bit random value that appears in the
48744 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
48750 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
48753 ** The macro MEMDB is true if we are dealing with an in-memory database.
48761 # define MEMDB pPager->memDb
48766 ** interfaces to access the database using memory-mapped I/O.
48769 # define USEFETCH(x) ((x)->bUseFetch)
48775 ** The maximum legal page number is (2^31 - 1).
48781 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
48785 ** if( isOpen(pPager->jfd) ){ ...
48789 ** if( pPager->jfd->pMethods ){ ...
48791 #define isOpen(pFd) ((pFd)->pMethods!=0)
48794 ** Return true if this pager uses a write-ahead log to read page pgno.
48801 if( pPager->pWal==0 ) return 0;
48802 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
48807 # define pagerUseWal(x) ((x)->pWal!=0)
48829 assert( p->eState==PAGER_OPEN
48830 || p->eState==PAGER_READER
48831 || p->eState==PAGER_WRITER_LOCKED
48832 || p->eState==PAGER_WRITER_CACHEMOD
48833 || p->eState==PAGER_WRITER_DBMOD
48834 || p->eState==PAGER_WRITER_FINISHED
48835 || p->eState==PAGER_ERROR
48838 /* Regardless of the current state, a temp-file connection always behaves
48840 ** the change-counter field, so the changeCountDone flag is always set.
48842 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
48843 assert( p->tempFile==0 || pPager->changeCountDone );
48845 /* If the useJournal flag is clear, the journal-mode must be "OFF".
48846 ** And if the journal-mode is "OFF", the journal file must not be open.
48848 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
48849 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
48851 /* Check that MEMDB implies noSync. And an in-memory journal. Since
48852 ** this means an in-memory pager performs no IO at all, it cannot encounter
48854 ** a journal file. (although the in-memory journal implementation may
48856 ** is therefore not possible for an in-memory pager to enter the ERROR
48860 assert( !isOpen(p->fd) );
48861 assert( p->noSync );
48862 assert( p->journalMode==PAGER_JOURNALMODE_OFF
48863 || p->journalMode==PAGER_JOURNALMODE_MEMORY
48865 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
48872 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
48873 assert( p->eLock!=PENDING_LOCK );
48875 switch( p->eState ){
48878 assert( pPager->errCode==SQLITE_OK );
48879 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
48883 assert( pPager->errCode==SQLITE_OK );
48884 assert( p->eLock!=UNKNOWN_LOCK );
48885 assert( p->eLock>=SHARED_LOCK );
48889 assert( p->eLock!=UNKNOWN_LOCK );
48890 assert( pPager->errCode==SQLITE_OK );
48892 assert( p->eLock>=RESERVED_LOCK );
48894 assert( pPager->dbSize==pPager->dbOrigSize );
48895 assert( pPager->dbOrigSize==pPager->dbFileSize );
48896 assert( pPager->dbOrigSize==pPager->dbHintSize );
48897 assert( pPager->setMaster==0 );
48901 assert( p->eLock!=UNKNOWN_LOCK );
48902 assert( pPager->errCode==SQLITE_OK );
48909 assert( p->eLock>=RESERVED_LOCK );
48910 assert( isOpen(p->jfd)
48911 || p->journalMode==PAGER_JOURNALMODE_OFF
48912 || p->journalMode==PAGER_JOURNALMODE_WAL
48915 assert( pPager->dbOrigSize==pPager->dbFileSize );
48916 assert( pPager->dbOrigSize==pPager->dbHintSize );
48920 assert( p->eLock==EXCLUSIVE_LOCK );
48921 assert( pPager->errCode==SQLITE_OK );
48923 assert( p->eLock>=EXCLUSIVE_LOCK );
48924 assert( isOpen(p->jfd)
48925 || p->journalMode==PAGER_JOURNALMODE_OFF
48926 || p->journalMode==PAGER_JOURNALMODE_WAL
48927 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
48929 assert( pPager->dbOrigSize<=pPager->dbHintSize );
48933 assert( p->eLock==EXCLUSIVE_LOCK );
48934 assert( pPager->errCode==SQLITE_OK );
48936 assert( isOpen(p->jfd)
48937 || p->journalMode==PAGER_JOURNALMODE_OFF
48938 || p->journalMode==PAGER_JOURNALMODE_WAL
48939 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
48948 assert( pPager->errCode!=SQLITE_OK );
48949 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
48978 , p->zFilename
48979 , p->eState==PAGER_OPEN ? "OPEN" :
48980 p->eState==PAGER_READER ? "READER" :
48981 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
48982 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
48983 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
48984 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
48985 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
48986 , (int)p->errCode
48987 , p->eLock==NO_LOCK ? "NO_LOCK" :
48988 p->eLock==RESERVED_LOCK ? "RESERVED" :
48989 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
48990 p->eLock==SHARED_LOCK ? "SHARED" :
48991 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
48992 , p->exclusiveMode ? "exclusive" : "normal"
48993 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
48994 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
48995 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
48996 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
48997 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
48998 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
48999 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
49000 , p->journalOff, p->journalHdr
49001 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
49020 if( pPager->errCode ){
49021 pPager->xGet = getPageError;
49025 && pPager->xCodec==0
49028 pPager->xGet = getPageMMap;
49031 pPager->xGet = getPageNormal;
49036 ** Return true if it is necessary to write page *pPg into the sub-journal.
49037 ** A page needs to be written into the sub-journal if there exists one
49040 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
49041 ** * The bit corresponding to the page-number is not set in
49045 Pager *pPager = pPg->pPager;
49047 Pgno pgno = pPg->pgno;
49049 for(i=0; i<pPager->nSavepoint; i++){
49050 p = &pPager->aSavepoint[i];
49051 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
49063 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
49068 ** Read a 32-bit integer from the given file descriptor. Store the integer
49072 ** All values are stored on disk as big-endian.
49084 ** Write a 32-bit integer into a string buffer in big-endian byte order.
49090 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
49111 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
49114 if( isOpen(pPager->fd) ){
49115 assert( pPager->eLock>=eLock );
49116 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
49117 if( pPager->eLock!=UNKNOWN_LOCK ){
49118 pPager->eLock = (u8)eLock;
49139 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
49140 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
49141 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
49142 pPager->eLock = (u8)eLock;
49150 ** This function determines whether or not the atomic-write or
49151 ** atomic-batch-write optimizations can be used with this pager. The
49152 ** atomic-write optimization can be used if:
49162 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
49163 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
49175 assert( isOpen(pPager->fd) );
49176 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
49183 return -1;
49189 int nSector = pPager->sectorSize;
49190 int szPage = pPager->pageSize;
49212 ** Return a 32-bit hash of the page data for pPage.
49223 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
49226 pPage->pageHash = pager_pagehash(pPage);
49232 ** that the page is either dirty or still matches the calculated page-hash.
49236 Pager *pPager = pPg->pPager;
49237 assert( pPager->eState!=PAGER_ERROR );
49238 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
49259 ** nul-terminator), then this is handled as if no master journal name
49264 ** nul-terminator byte is appended to the buffer following the master
49284 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
49286 || len>szJ-16
49288 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
49289 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
49291 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
49298 cksum -= zMaster[u];
49304 ** master-journal filename.
49315 ** following the value in pPager->journalOff, assuming a sector
49316 ** size of pPager->sectorSize bytes.
49321 ** ---------------------------------------
49330 i64 c = pPager->journalOff;
49332 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
49336 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
49343 ** This function is a no-op if the journal file has not been written to
49346 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
49348 ** zero the 28-byte header at the start of the journal file. In either case,
49349 ** if the pager is not in no-sync mode, sync the journal file immediately
49352 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
49363 assert( isOpen(pPager->jfd) );
49364 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
49365 if( pPager->journalOff ){
49366 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
49370 rc = sqlite3OsTruncate(pPager->jfd, 0);
49373 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
49375 if( rc==SQLITE_OK && !pPager->noSync ){
49376 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
49387 rc = sqlite3OsFileSize(pPager->jfd, &sz);
49389 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
49402 ** - 8 bytes: Magic identifying journal format.
49403 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
49404 ** - 4 bytes: Random number used for page hash.
49405 ** - 4 bytes: Initial database page count.
49406 ** - 4 bytes: Sector size used by the process that wrote this journal.
49407 ** - 4 bytes: Database page size.
49409 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
49413 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
49414 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
49418 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
49428 for(ii=0; ii<pPager->nSavepoint; ii++){
49429 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
49430 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
49434 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
49437 ** Write the nRec Field - the number of page records that follow this
49440 ** if in full-sync mode), the zero is overwritten with the true number
49450 ** * When the pager is in no-sync mode. Corruption can follow a
49456 assert( isOpen(pPager->fd) || pPager->noSync );
49457 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
49458 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
49466 /* The random check-hash initializer */
49467 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
49468 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
49470 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
49472 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
49475 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
49483 nHeader-(sizeof(aJournalMagic)+20));
49494 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
49497 ** The loop is required here in case the sector-size is larger than the
49503 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
49504 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
49505 assert( pPager->journalHdr <= pPager->journalOff );
49506 pPager->journalOff += nHeader;
49516 ** pPager->journalOff. See comments above function writeJournalHdr() for
49521 ** database before the transaction began, in pages. Also, pPager->cksumInit
49540 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
49546 pPager->journalOff = journalHdrOffset(pPager);
49547 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
49550 iHdrOff = pPager->journalOff;
49557 if( isHot || iHdrOff!=pPager->journalHdr ){
49558 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
49567 /* Read the first three 32-bit fields of the journal header: The nRec
49568 ** field, the checksum-initializer and the database size at the start
49571 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
49572 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
49573 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
49578 if( pPager->journalOff==0 ){
49579 u32 iPageSize; /* Page-size field of journal header */
49580 u32 iSectorSize; /* Sector-size field of journal header */
49582 /* Read the page-size and sector-size journal header fields. */
49583 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
49584 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
49589 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
49594 iPageSize = pPager->pageSize;
49597 /* Check that the values read from the page-size and sector-size fields
49604 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
49606 /* If the either the page-size or sector-size in the journal-header is
49607 ** invalid, then the process that wrote the journal-header must have
49614 /* Update the page-size to match the value read from the journal.
49618 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
49621 /* Update the assumed sector-size to match the value used by
49627 pPager->sectorSize = iSectorSize;
49630 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
49638 ** thing written to a journal file. If the pager is in full-sync mode, the
49643 ** + N bytes: Master journal filename in utf-8.
49644 ** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
49649 ** journal name, where each byte is interpreted as a signed 8-bit integer.
49652 ** this call is a no-op.
49661 assert( pPager->setMaster==0 );
49665 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
49666 || !isOpen(pPager->jfd)
49670 pPager->setMaster = 1;
49671 assert( pPager->journalHdr <= pPager->journalOff );
49678 /* If in full-sync mode, advance to the next disk sector before writing
49682 if( pPager->fullSync ){
49683 pPager->journalOff = journalHdrOffset(pPager);
49685 iHdrOff = pPager->journalOff;
49690 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
49691 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
49692 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
49693 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
49694 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
49699 pPager->journalOff += (nMaster+20);
49701 /* If the pager is in peristent-journal mode, then the physical
49702 ** journal-file may extend past the end of the master-journal name
49704 ** dangerous because the code to rollback a hot-journal file
49705 ** will not be able to find the master-journal name to determine
49711 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
49712 && jrnlSize>pPager->journalOff
49714 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
49720 ** Discard the entire contents of the in-memory page-cache.
49723 pPager->iDataVersion++;
49724 sqlite3BackupRestart(pPager->pBackup);
49725 sqlite3PcacheClear(pPager->pPCache);
49729 ** Return the pPager->iDataVersion value
49732 assert( pPager->eState>PAGER_OPEN );
49733 return pPager->iDataVersion;
49738 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
49743 for(ii=0; ii<pPager->nSavepoint; ii++){
49744 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
49746 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
49747 sqlite3OsClose(pPager->sjfd);
49749 sqlite3_free(pPager->aSavepoint);
49750 pPager->aSavepoint = 0;
49751 pPager->nSavepoint = 0;
49752 pPager->nSubRec = 0;
49764 for(ii=0; ii<pPager->nSavepoint; ii++){
49765 PagerSavepoint *p = &pPager->aSavepoint[ii];
49766 if( pgno<=p->nOrig ){
49767 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
49776 ** This function is a no-op if the pager is in exclusive mode and not
49780 ** If the pager is not in exclusive-access mode, the database file is
49781 ** completely unlocked. If the file is unlocked and the file-system does
49787 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
49788 ** or not, any journal file left in the file-system will be treated
49789 ** as a hot-journal and rolled back the next time a read-transaction
49794 assert( pPager->eState==PAGER_READER
49795 || pPager->eState==PAGER_OPEN
49796 || pPager->eState==PAGER_ERROR
49799 sqlite3BitvecDestroy(pPager->pInJournal);
49800 pPager->pInJournal = 0;
49804 assert( !isOpen(pPager->jfd) );
49805 sqlite3WalEndReadTransaction(pPager->pWal);
49806 pPager->eState = PAGER_OPEN;
49807 }else if( !pPager->exclusiveMode ){
49809 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
49823 || 1!=(pPager->journalMode & 5)
49825 sqlite3OsClose(pPager->jfd);
49834 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
49835 pPager->eLock = UNKNOWN_LOCK;
49839 ** without clearing the error code. This is intentional - the error
49842 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
49843 pPager->changeCountDone = 0;
49844 pPager->eState = PAGER_OPEN;
49850 ** normal and exclusive-locking mode.
49852 assert( pPager->errCode==SQLITE_OK || !MEMDB );
49853 if( pPager->errCode ){
49854 if( pPager->tempFile==0 ){
49856 pPager->changeCountDone = 0;
49857 pPager->eState = PAGER_OPEN;
49859 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
49861 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
49862 pPager->errCode = SQLITE_OK;
49866 pPager->journalOff = 0;
49867 pPager->journalHdr = 0;
49868 pPager->setMaster = 0;
49875 ** the error-code about to be returned by a pager API function. The
49879 ** IOERR sub-codes, the pager enters the ERROR state and the error code
49883 ** The ERROR state indicates that the contents of the pager-cache
49885 ** the contents of the pager-cache. If a transaction was active when
49888 ** it were a hot-journal).
49894 pPager->errCode==SQLITE_FULL ||
49895 pPager->errCode==SQLITE_OK ||
49896 (pPager->errCode & 0xff)==SQLITE_IOERR
49899 pPager->errCode = rc;
49900 pPager->eState = PAGER_ERROR;
49916 ** * For non-TEMP databases, always sync to disk. This is necessary
49925 if( pPager->tempFile==0 ) return 1;
49927 if( !isOpen(pPager->fd) ) return 0;
49928 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
49934 ** after rollback of a hot-journal, or if an error occurs while opening
49935 ** the journal file or writing the very first journal-header of a
49940 ** exclusive than a RESERVED lock, it is a no-op.
49946 ** transaction. Nor will it be considered to be a hot-journal by this
49949 ** the current journal-mode (Pager.journalMode value), as follows:
49953 ** in-memory journal.
49972 ** If running in non-exclusive rollback mode, the lock on the file is
49990 ** is no write-transaction active but a RESERVED or greater lock is
49993 ** 1. After a successful hot-journal rollback, it is called with
49998 ** read-transaction, this function is called with eState==PAGER_READER
49999 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
50002 assert( pPager->eState!=PAGER_ERROR );
50003 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
50008 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
50009 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
50011 if( isOpen(pPager->jfd) ){
50015 if( sqlite3JournalIsInMemory(pPager->jfd) ){
50016 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
50017 sqlite3OsClose(pPager->jfd);
50018 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
50019 if( pPager->journalOff==0 ){
50022 rc = sqlite3OsTruncate(pPager->jfd, 0);
50023 if( rc==SQLITE_OK && pPager->fullSync ){
50029 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
50032 pPager->journalOff = 0;
50033 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
50034 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
50036 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
50037 pPager->journalOff = 0;
50040 ** a hot-journal was just rolled back. In this case the journal
50042 ** the database file, it will do so using an in-memory journal.
50044 int bDelete = !pPager->tempFile;
50045 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
50046 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
50047 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
50048 || pPager->journalMode==PAGER_JOURNALMODE_WAL
50050 sqlite3OsClose(pPager->jfd);
50052 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
50058 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
50059 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
50062 p->pageHash = 0;
50068 sqlite3BitvecDestroy(pPager->pInJournal);
50069 pPager->pInJournal = 0;
50070 pPager->nRec = 0;
50073 sqlite3PcacheCleanAll(pPager->pPCache);
50075 sqlite3PcacheClearWritable(pPager->pPCache);
50077 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
50081 /* Drop the WAL write-lock, if any. Also, if the connection was in
50085 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
50087 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
50088 /* This branch is taken when committing a transaction in rollback-journal
50094 assert( pPager->eLock==EXCLUSIVE_LOCK );
50095 rc = pager_truncate(pPager, pPager->dbSize);
50098 if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
50099 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
50103 if( !pPager->exclusiveMode
50104 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
50107 pPager->changeCountDone = 0;
50109 pPager->eState = PAGER_READER;
50110 pPager->setMaster = 0;
50121 ** call to pager_unlock() will discard all in-memory pages, unlock
50123 ** means that there is a hot-journal left in the file-system, the next
50133 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
50135 if( pPager->eState>=PAGER_WRITER_LOCKED ){
50139 }else if( !pPager->exclusiveMode ){
50140 assert( pPager->eState==PAGER_READER );
50148 ** Parameter aData must point to a buffer of pPager->pageSize bytes
50150 ** page of data and the current value of pPager->cksumInit.
50153 ** random initial value (pPager->cksumInit) and every 200th byte
50154 ** of the page data, starting with byte offset (pPager->pageSize%200).
50155 ** Each byte is interpreted as an 8-bit unsigned integer.
50167 u32 cksum = pPager->cksumInit; /* Checksum value to return */
50168 int i = pPager->pageSize-200; /* Loop counter */
50171 i -= 200;
50182 if( pPager->xCodecSizeChng ){
50183 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
50184 (int)pPager->nReserve);
50188 # define pagerReportSize(X) /* No-op if we do not support a codec */
50198 if( pDest->nReserve!=pSrc->nReserve ){
50199 pDest->nReserve = pSrc->nReserve;
50207 ** from the sub-journal (if isMainJrnl==0) and playback that page.
50211 ** The main rollback journal uses checksums - the statement journal does
50214 ** If the page number of the page record read from the (sub-)journal file
50224 ** If the page record is successfully read from the (sub-)journal file
50226 ** while reading the record from the (sub-)journal file or while writing
50228 ** is successfully read from the (sub-)journal file but appears to be
50232 ** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
50246 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
50258 ** the codec. It is false for pure in-memory journals. */
50259 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
50264 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
50265 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
50267 aData = pPager->pTmpSpace;
50273 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
50274 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
50275 ** only reads from the main journal, not the sub-journal.
50277 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
50278 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
50280 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
50282 /* Read the page number and page data from the journal or sub-journal
50285 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
50288 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
50290 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
50301 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
50305 rc = read32bits(jfd, (*pOffset)-4, &cksum);
50321 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
50322 pPager->nReserve = ((u8*)aData)[20];
50330 ** An exception to the above rule: If the database is in no-sync mode
50340 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
50357 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
50367 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
50369 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
50370 (isMainJrnl?"main-journal":"sub-journal")
50373 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
50375 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
50377 if( isOpen(pPager->fd)
50378 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
50381 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
50382 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
50386 ** This is usually safe even for an encrypted database - as the data
50388 ** is if the data was just read from an in-memory sub-journal. In that
50394 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
50398 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
50400 if( pgno>pPager->dbFileSize ){
50401 pPager->dbFileSize = pgno;
50403 if( pPager->pBackup ){
50407 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
50411 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
50415 ** the database and the page is not in-memory, there is a potential
50416 ** problem. When the page is next fetched by the b-tree layer, it
50422 ** if the page is on the free-list at the start of the transaction, then
50425 ** The solution is to add an in-memory page to the cache containing
50426 ** the data just read from the sub-journal. Mark the page as dirty
50427 ** and if the pager requires a journal-sync, then mark the page as
50428 ** requiring a journal-sync before it is written.
50431 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
50432 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
50434 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
50435 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
50447 pData = pPg->pData;
50448 memcpy(pData, (u8*)aData, pPager->pageSize);
50449 pPager->xReiniter(pPg);
50459 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
50464 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
50481 ** of all of its child journals, one after another, formatted as utf-8
50483 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
50486 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
50491 ** This function reads the contents of the master-journal file into
50502 ** the file-system using sqlite3OsDelete().
50511 ** a couple of kilobytes or so - potentially larger than the page
50515 sqlite3_vfs *pVfs = pPager->pVfs;
50517 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
50518 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
50528 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
50529 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
50541 ** journal files extracted from regular rollback-journals.
50545 nMasterPtr = pVfs->mxPathname+1;
50557 while( (zJournal-zMasterJournal)<nMasterJournal ){
50606 ** file in the file-system. This only happens when committing a transaction,
50607 ** or rolling back a transaction (including rolling back a hot-journal).
50610 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
50611 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
50626 assert( pPager->eState!=PAGER_ERROR );
50627 assert( pPager->eState!=PAGER_READER );
50629 if( isOpen(pPager->fd)
50630 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
50633 int szPage = pPager->pageSize;
50634 assert( pPager->eLock==EXCLUSIVE_LOCK );
50636 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
50640 rc = sqlite3OsTruncate(pPager->fd, newSize);
50642 char *pTmp = pPager->pTmpSpace;
50644 testcase( (newSize-szPage) == currentSize );
50645 testcase( (newSize-szPage) > currentSize );
50646 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
50649 pPager->dbFileSize = nPage;
50657 ** Return a sanitized version of the sector-size of OS file pFile. The
50680 ** Otherwise, for non-temporary files, the effective sector size is
50687 ** pPager->sectorSize is to define the "blast radius" of bytes that
50695 assert( isOpen(pPager->fd) || pPager->tempFile );
50697 if( pPager->tempFile
50698 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
50704 pPager->sectorSize = 512;
50706 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
50717 ** (2) 4 byte big-endian integer which is the number of valid page records
50720 ** (3) 4 byte big-endian integer which is the initial value for the
50724 ** (5) 4 byte big-endian integer which is the sector size. The header
50726 ** (6) 4 byte big-endian integer which is the page size.
50730 ** + pPager->pageSize bytes of data.
50747 ** no-sync option for the journal. A power failure could lead to corruption
50751 ** If the file opened as the journal file is not a well-formed
50757 ** If an I/O or malloc() error occurs, the journal-file is not deleted
50768 sqlite3_vfs *pVfs = pPager->pVfs;
50778 u32 savedPageSize = pPager->pageSize;
50783 assert( isOpen(pPager->jfd) );
50784 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
50796 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
50800 zMaster = pPager->pTmpSpace;
50801 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
50809 pPager->journalOff = 0;
50831 ** working in no-sync mode. This means that the rest of the journal
50836 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
50837 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
50855 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
50856 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
50862 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
50867 pPager->dbSize = mxPg;
50878 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
50883 pPager->journalOff = szJ;
50909 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
50913 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
50917 if( pPager->fd->pMethods ){
50918 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
50923 ** malloc error that occurred after the change-counter was updated but
50924 ** before the transaction was committed, then the change-counter
50927 ** update the change-counter at all. This may lead to cache inconsistency
50931 pPager->changeCountDone = pPager->tempFile;
50934 zMaster = pPager->pTmpSpace;
50935 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
50939 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
50956 nPlayback, pPager->zJournal);
50971 ** pPg->pData. A shared lock or greater must be held on the database
50981 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
50987 assert( pPager->eState>=PAGER_READER && !MEMDB );
50988 assert( isOpen(pPager->fd) );
50991 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
50995 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
50999 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
51000 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
51006 if( pPg->pgno==1 ){
51013 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
51020 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
51022 u8 *dbFileVers = &((u8*)pPg->pData)[24];
51023 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
51026 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
51029 PAGER_INCR(pPager->nRead);
51030 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
51032 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
51038 ** Update the value of the change-counter at offsets 24 and 92 in
51042 ** routine which only updates the change-counter if the update is actually
51043 ** needed, as determined by the pPager->changeCountDone state variable.
51049 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
51050 put32bits(((char*)pPg->pData)+24, change_counter);
51055 put32bits(((char*)pPg->pData)+92, change_counter);
51056 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
51085 pPager->xReiniter(pPg);
51099 sqlite3BackupRestart(pPager->pBackup);
51118 pPager->dbSize = pPager->dbOrigSize;
51119 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
51120 pList = sqlite3PcacheDirtyList(pPager->pPCache);
51122 PgHdr *pNext = pList->pDirty;
51123 rc = pagerUndoCallback((void *)pPager, pList->pgno);
51149 assert( pPager->pWal );
51153 for(p=pList; p && p->pDirty; p=p->pDirty){
51154 assert( p->pgno < p->pDirty->pgno );
51158 assert( pList->pDirty==0 || isCommit );
51166 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
51167 if( p->pgno<=nTruncate ){
51168 ppNext = &p->pDirty;
51176 pPager->aStat[PAGER_STAT_WRITE] += nList;
51178 if( pList->pgno==1 ) pager_write_changecounter(pList);
51179 rc = sqlite3WalFrames(pPager->pWal,
51180 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
51182 if( rc==SQLITE_OK && pPager->pBackup ){
51183 for(p=pList; p; p=p->pDirty){
51184 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
51189 pList = sqlite3PcacheDirtyList(pPager->pPCache);
51190 for(p=pList; p; p=p->pDirty){
51211 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
51218 sqlite3WalEndReadTransaction(pPager->pWal);
51220 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
51223 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
51242 /* Query the WAL sub-system for the database size. The WalDbsize()
51245 ** available from the WAL sub-system if the log file is empty or
51248 assert( pPager->eState==PAGER_OPEN );
51249 assert( pPager->eLock>=SHARED_LOCK );
51250 assert( isOpen(pPager->fd) );
51251 assert( pPager->tempFile==0 );
51252 nPage = sqlite3WalDbsize(pPager->pWal);
51255 ** WAL sub-system, determine the page count based on the size of
51257 ** integer multiple of the page-size, round up the result.
51259 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
51261 int rc = sqlite3OsFileSize(pPager->fd, &n);
51265 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
51272 if( nPage>pPager->mxPgno ){
51273 pPager->mxPgno = (Pgno)nPage;
51282 ** Check if the *-wal file that corresponds to the database opened by pPager
51283 ** exists if the database is not empy, or verify that the *-wal file does
51286 ** If the database is not empty and the *-wal file exists, open the pager
51287 ** in WAL mode. If the database is empty or if no *-wal file exists and
51295 ** a WAL on a none-empty database, this ensures there is no race condition
51301 assert( pPager->eState==PAGER_OPEN );
51302 assert( pPager->eLock>=SHARED_LOCK );
51304 if( !pPager->tempFile ){
51307 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
51316 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
51318 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
51321 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
51322 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
51336 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
51349 ** * Pages are then played back from the sub-journal file, starting
51363 ** (or transaction). No page with a page-number greater than this value
51368 i64 iHdrOff; /* End of first segment of main-journal records */
51372 assert( pPager->eState!=PAGER_ERROR );
51373 assert( pPager->eState>=PAGER_WRITER_LOCKED );
51377 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
51386 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
51387 pPager->changeCountDone = pPager->tempFile;
51393 /* Use pPager->journalOff as the effective size of the main rollback
51396 ** past pPager->journalOff is off-limits to us.
51398 szJ = pPager->journalOff;
51404 ** greater than the current database size (pPager->dbSize) but those
51409 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
51410 pPager->journalOff = pSavepoint->iOffset;
51411 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
51412 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
51416 pPager->journalOff = 0;
51421 ** of the main journal file. Continue to skip out-of-range pages and
51424 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
51432 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
51437 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
51439 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
51441 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
51442 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
51446 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
51448 /* Finally, rollback pages from the sub-journal. Page that were
51450 ** will be skipped. Out-of-range pages are also skipped.
51454 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
51457 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
51459 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
51460 assert( offset==(i64)ii*(4+pPager->pageSize) );
51468 pPager->journalOff = szJ;
51475 ** Change the maximum number of in-memory pages that are allowed
51479 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
51483 ** Change the maximum number of in-memory pages that are allowed
51487 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
51495 sqlite3_file *fd = pPager->fd;
51496 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
51498 sz = pPager->szMmap;
51499 pPager->bUseFetch = (sz>0);
51501 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
51510 pPager->szMmap = szMmap;
51518 sqlite3PcacheShrink(pPager->pPCache);
51540 ** database (with some additional information - the nRec field
51541 ** of the journal header - being written in between the two
51551 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
51562 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
51578 if( pPager->tempFile ){
51579 pPager->noSync = 1;
51580 pPager->fullSync = 0;
51581 pPager->extraSync = 0;
51583 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
51584 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
51585 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
51587 if( pPager->noSync ){
51588 pPager->syncFlags = 0;
51590 pPager->syncFlags = SQLITE_SYNC_FULL;
51592 pPager->syncFlags = SQLITE_SYNC_NORMAL;
51594 pPager->walSyncFlags = (pPager->syncFlags<<2);
51595 if( pPager->fullSync ){
51596 pPager->walSyncFlags |= pPager->syncFlags;
51598 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
51599 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
51602 pPager->doNotSpill &= ~SPILLFLAG_OFF;
51604 pPager->doNotSpill |= SPILLFLAG_OFF;
51646 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
51654 ** The pager invokes the busy-handler if sqlite3OsLock() returns
51655 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
51659 ** (which occurs during hot-journal rollback). Summary:
51662 ** --------------------------------------------------------
51663 ** NO_LOCK -> SHARED_LOCK | Yes
51664 ** SHARED_LOCK -> RESERVED_LOCK | No
51665 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
51666 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
51668 ** If the busy-handler callback returns non-zero, the lock is
51674 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
51677 pPager->xBusyHandler = xBusyHandler;
51678 pPager->pBusyHandlerArg = pBusyHandlerArg;
51680 if( isOpen(pPager->fd) ){
51681 void **ap = (void **)&pPager->xBusyHandler;
51684 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
51693 ** is a no-op. The value returned is the error state error code (i.e.
51694 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
51703 ** * the database is either not an in-memory database or it is
51704 ** an in-memory database that currently consists of zero pages.
51728 ** is a no-op for that case anyhow.
51733 if( (pPager->memDb==0 || pPager->dbSize==0)
51734 && sqlite3PcacheRefCount(pPager->pPCache)==0
51735 && pageSize && pageSize!=(u32)pPager->pageSize
51740 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
51741 rc = sqlite3OsFileSize(pPager->fd, &nByte);
51750 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
51753 sqlite3PageFree(pPager->pTmpSpace);
51754 pPager->pTmpSpace = pNew;
51755 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
51756 pPager->pageSize = pageSize;
51762 *pPageSize = pPager->pageSize;
51764 if( nReserve<0 ) nReserve = pPager->nReserve;
51766 pPager->nReserve = (i16)nReserve;
51782 return pPager->pTmpSpace;
51794 pPager->mxPgno = mxPage;
51796 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
51797 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
51798 return pPager->mxPgno;
51806 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
51815 sqlite3_io_error_pending = -1;
51842 assert( isOpen(pPager->fd) || pPager->tempFile );
51850 if( isOpen(pPager->fd) ){
51852 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
51861 ** This function may only be called when a read-transaction is open on
51864 ** However, if the file is between 1 and <page-size> bytes in size, then
51868 assert( pPager->eState>=PAGER_READER );
51869 assert( pPager->eState!=PAGER_WRITER_FINISHED );
51870 *pnPage = (int)pPager->dbSize;
51876 ** a similar or greater lock is already held, this function is a no-op
51891 /* Check that this is either a no-op (because the requested lock is
51892 ** already held), or one of the transitions that the busy-handler
51896 assert( (pPager->eLock>=locktype)
51897 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
51898 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
51903 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
51909 ** following is true for all dirty pages currently in the page-cache:
51915 ** be necessary to write the current content out to the sub-journal
51925 ** sub-journal rolled back the content could not be restored and the
51931 assert( pPg->flags&PGHDR_DIRTY );
51932 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
51935 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
51942 ** Truncate the in-memory database file image to nPage pages. This
51953 assert( pPager->dbSize>=nPage );
51954 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
51955 pPager->dbSize = nPage;
51970 ** This function is called before attempting a hot-journal rollback. It
51971 ** syncs the journal file to disk, then sets pPager->journalHdr to the
51975 ** Syncing a hot-journal to disk before attempting to roll it back ensures
51976 ** that if a power-failure occurs during the rollback, the process that
51985 if( !pPager->noSync ){
51986 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
51989 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
52013 if( pPager->pMmapFreelist ){
52014 *ppPage = p = pPager->pMmapFreelist;
52015 pPager->pMmapFreelist = p->pDirty;
52016 p->pDirty = 0;
52017 assert( pPager->nExtra>=8 );
52018 memset(p->pExtra, 0, 8);
52020 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
52022 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
52025 p->pExtra = (void *)&p[1];
52026 p->flags = PGHDR_MMAP;
52027 p->nRef = 1;
52028 p->pPager = pPager;
52031 assert( p->pExtra==(void *)&p[1] );
52032 assert( p->pPage==0 );
52033 assert( p->flags==PGHDR_MMAP );
52034 assert( p->pPager==pPager );
52035 assert( p->nRef==1 );
52037 p->pgno = pgno;
52038 p->pData = pData;
52039 pPager->nMmapOut++;
52050 Pager *pPager = pPg->pPager;
52051 pPager->nMmapOut--;
52052 pPg->pDirty = pPager->pMmapFreelist;
52053 pPager->pMmapFreelist = pPg;
52055 assert( pPager->fd->pMethods->iVersion>=3 );
52056 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
52065 for(p=pPager->pMmapFreelist; p; p=pNext){
52066 pNext = p->pDirty;
52087 u8 *pTmp = (u8 *)pPager->pTmpSpace;
52094 /* pPager->errCode = 0; */
52095 pPager->exclusiveMode = 0;
52097 assert( db || pPager->pWal==0 );
52098 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
52099 (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
52101 pPager->pWal = 0;
52115 ** back or finalize it. The next database user will have to do hot-journal
52118 if( isOpen(pPager->jfd) ){
52127 sqlite3OsClose(pPager->jfd);
52128 sqlite3OsClose(pPager->fd);
52130 sqlite3PcacheClose(pPager->pPCache);
52133 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
52136 assert( !pPager->aSavepoint && !pPager->pInJournal );
52137 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
52148 return pPg->pgno;
52162 ** disk and can be restored in the event of a hot-journal rollback.
52164 ** If the Pager.noSync flag is set, then this function is a no-op.
52165 ** Otherwise, the actions required depend on the journal-mode and the
52166 ** device characteristics of the file-system, as follows:
52168 ** * If the journal file is an in-memory journal file, no action need
52174 ** been written following it. If the pager is operating in full-sync
52180 ** Or, in pseudo-code:
52182 ** if( NOT <in-memory journal> ){
52184 ** if( <full-sync mode> ) xSync(<journal file>);
52197 assert( pPager->eState==PAGER_WRITER_CACHEMOD
52198 || pPager->eState==PAGER_WRITER_DBMOD
52206 if( !pPager->noSync ){
52207 assert( !pPager->tempFile );
52208 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
52209 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
52210 assert( isOpen(pPager->jfd) );
52214 ** that wrote to this database was operating in persistent-journal
52217 ** file happens to be a journal-header (written as part of the
52218 ** previous connection's transaction), and a crash or power-failure
52222 ** hot-journal rollback following recovery. It may roll back all
52224 ** out-of-date data that follows it. Database corruption.
52240 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
52243 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
52246 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
52253 ** full-synchronous mode, sync the journal first. This ensures that
52263 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
52266 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
52269 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
52271 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
52278 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
52279 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
52284 pPager->journalHdr = pPager->journalOff;
52286 pPager->nRec = 0;
52291 pPager->journalHdr = pPager->journalOff;
52299 sqlite3PcacheClearSyncFlags(pPager->pPCache);
52300 pPager->eState = PAGER_WRITER_DBMOD;
52308 ** in-memory pages in the list to the database file. The argument may
52310 ** a no-op.
52317 ** If the pager is a temp-file pager and the actual file-system file
52342 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
52343 assert( pPager->eLock==EXCLUSIVE_LOCK );
52344 assert( isOpen(pPager->fd) || pList->pDirty==0 );
52346 /* If the file is a temp-file has not yet been opened, open it now. It
52348 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
52350 if( !isOpen(pPager->fd) ){
52351 assert( pPager->tempFile && rc==SQLITE_OK );
52352 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
52358 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
52360 && pPager->dbHintSize<pPager->dbSize
52361 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
52363 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
52364 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
52365 pPager->dbHintSize = pPager->dbSize;
52369 Pgno pgno = pList->pgno;
52373 ** make the file smaller (presumably by auto-vacuum code). Do not write
52379 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
52380 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
52383 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
52384 if( pList->pgno==1 ) pager_write_changecounter(pList);
52387 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
52390 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
52397 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
52399 if( pgno>pPager->dbFileSize ){
52400 pPager->dbFileSize = pgno;
52402 pPager->aStat[PAGER_STAT_WRITE]++;
52405 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
52415 pList = pList->pDirty;
52422 ** Ensure that the sub-journal file is open. If it is already open, this
52423 ** function is a no-op.
52431 if( !isOpen(pPager->sjfd) ){
52436 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
52437 nStmtSpill = -1;
52439 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
52445 ** Append a record of the current state of page pPg to the sub-journal.
52447 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
52451 ** error code if the attempt to write to the sub-journal fails, or
52457 Pager *pPager = pPg->pPager;
52458 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
52460 /* Open the sub-journal, if it has not already been opened */
52461 assert( pPager->useJournal );
52462 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
52463 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
52466 || pPg->pgno>pPager->dbOrigSize
52470 /* If the sub-journal was opened successfully (or was already open),
52473 void *pData = pPg->pData;
52474 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
52478 if( !pPager->subjInMemory ){
52479 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
52483 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
52484 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
52486 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
52491 pPager->nSubRec++;
52492 assert( pPager->nSavepoint>0 );
52493 rc = addToSavepointBitvecs(pPager, pPg->pgno);
52508 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
52528 assert( pPg->pPager==pPager );
52529 assert( pPg->flags&PGHDR_DIRTY );
52547 if( NEVER(pPager->errCode) ) return SQLITE_OK;
52548 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
52549 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
52550 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
52551 if( pPager->doNotSpill
52552 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
52553 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
52558 pPg->pDirty = 0;
52568 if( pPager->tempFile==0 ){
52569 rc = sqlite3JournalCreate(pPager->jfd);
52575 if( pPg->flags&PGHDR_NEED_SYNC
52576 || pPager->eState==PAGER_WRITER_CACHEMOD
52583 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
52590 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
52601 int rc = pPager->errCode;
52603 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
52606 PgHdr *pNext = pList->pDirty;
52607 if( pList->nRef==0 ){
52623 ** If zFilename is NULL then a randomly-named temporary file is created
52627 ** This can be used to implement an in-memory database.
52653 int nExtra, /* Extra bytes append to each in-memory page */
52661 int tempFile = 0; /* True for temp files (incl. in-memory files) */
52662 int memDb = 0; /* True if this is an in-memory file */
52663 int readOnly = 0; /* True if this is a read-only file */
52673 /* Figure out how much space is required for each journal file-handle
52674 ** (there are two of them, the main journal and the sub-journal). */
52698 nPathname = pVfs->mxPathname+1;
52711 nUri = (int)(&z[1] - zUri);
52713 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
52715 ** the database being opened will be more than pVfs->mxPathname
52718 ** check for a hot-journal before reading.
52734 ** Database file handle (pVfs->szOsFile bytes)
52735 ** Sub-journal file handle (journalFileSize bytes)
52743 ROUND8(pVfs->szOsFile) + /* The main db file */
52757 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
52758 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
52759 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
52760 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
52761 pPager->zFilename = (char*)(pPtr += journalFileSize);
52762 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
52767 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
52768 memcpy(pPager->zFilename, zPathname, nPathname);
52769 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
52770 memcpy(pPager->zJournal, zPathname, nPathname);
52771 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
52772 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
52774 pPager->zWal = &pPager->zJournal[nPathname+8+1];
52775 memcpy(pPager->zWal, zPathname, nPathname);
52776 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
52777 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
52781 pPager->pVfs = pVfs;
52782 pPager->vfsFlags = vfsFlags;
52788 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
52801 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
52805 if( szPageDflt<pPager->sectorSize ){
52806 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
52809 szPageDflt = (u32)pPager->sectorSize;
52826 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
52838 ** This branch is also run for an in-memory database. An in-memory
52839 ** database is the same as a temp-file that is never written out to
52840 ** disk and uses an in-memory rollback journal.
52846 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
52847 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
52848 pPager->noLock = 1; /* Do no locking */
52856 assert( pPager->memDb==0 );
52857 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
52866 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
52872 sqlite3OsClose(pPager->fd);
52873 sqlite3PageFree(pPager->pTmpSpace);
52878 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
52879 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
52881 pPager->useJournal = (u8)useJournal;
52882 /* pPager->stmtOpen = 0; */
52883 /* pPager->stmtInUse = 0; */
52884 /* pPager->nRef = 0; */
52885 /* pPager->stmtSize = 0; */
52886 /* pPager->stmtJSize = 0; */
52887 /* pPager->nPage = 0; */
52888 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
52889 /* pPager->state = PAGER_UNLOCK; */
52890 /* pPager->errMask = 0; */
52891 pPager->tempFile = (u8)tempFile;
52895 pPager->exclusiveMode = (u8)tempFile;
52896 pPager->changeCountDone = pPager->tempFile;
52897 pPager->memDb = (u8)memDb;
52898 pPager->readOnly = (u8)readOnly;
52899 assert( useJournal || pPager->tempFile );
52900 pPager->noSync = pPager->tempFile;
52901 if( pPager->noSync ){
52902 assert( pPager->fullSync==0 );
52903 assert( pPager->extraSync==0 );
52904 assert( pPager->syncFlags==0 );
52905 assert( pPager->walSyncFlags==0 );
52907 pPager->fullSync = 1;
52908 pPager->extraSync = 0;
52909 pPager->syncFlags = SQLITE_SYNC_NORMAL;
52910 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
52912 /* pPager->pFirst = 0; */
52913 /* pPager->pFirstSynced = 0; */
52914 /* pPager->pLast = 0; */
52915 pPager->nExtra = (u16)nExtra;
52916 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
52917 assert( isOpen(pPager->fd) || tempFile );
52920 pPager->journalMode = PAGER_JOURNALMODE_OFF;
52922 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
52924 /* pPager->xBusyHandler = 0; */
52925 /* pPager->pBusyHandlerArg = 0; */
52926 pPager->xReiniter = xReinit;
52928 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
52929 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
52938 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52945 if( pPager->tempFile ) return SQLITE_OK;
52946 if( pPager->dbSize==0 ) return SQLITE_OK;
52947 assert( pPager->zFilename && pPager->zFilename[0] );
52948 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52950 /* If the HAS_MOVED file-control is unimplemented, assume that the file
52964 ** the file-system for the given pager. A hot journal is one that
52965 ** needs to be played back. According to this function, a hot-journal
52982 ** case this routine will return a false-positive. The pager_playback()
52986 ** If a hot-journal file is found to exist, *pExists is set to 1 and
52987 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
52989 ** to determine whether or not a hot-journal file exists, the IO error
52993 sqlite3_vfs * const pVfs = pPager->pVfs;
52996 int jrnlOpen = !!isOpen(pPager->jfd);
52998 assert( pPager->useJournal );
52999 assert( isOpen(pPager->fd) );
53000 assert( pPager->eState==PAGER_OPEN );
53002 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
53008 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
53018 ** in fact there is none. This results in a false-positive which will
53021 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
53025 assert( pPager->tempFile==0 );
53039 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
53040 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
53046 ** at least one non-zero bytes at the start of the journal file.
53052 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
53056 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
53061 sqlite3OsClose(pPager->jfd);
53088 ** has been successfully called. If a shared-lock is already held when
53089 ** this function is called, it is a no-op.
53096 ** the SHARED lock, the file-system is checked for a hot-journal,
53097 ** which is played back if present. Following any hot-journal
53099 ** the 'change-counter' field of the database file header and
53102 ** 2) If the pager is running in exclusive-mode, and there are currently
53109 ** occurs while locking the database, checking for a hot-journal file or
53115 /* This routine is only called from b-tree and only when there are no
53119 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
53121 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
53122 assert( pPager->errCode==SQLITE_OK );
53124 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
53125 int bHotJournal = 1; /* True if there exists a hot journal-file */
53128 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
53132 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
53139 if( pPager->eLock<=SHARED_LOCK ){
53146 if( pPager->readOnly ){
53156 ** hot-journal back.
53173 ** in exclusive-access mode the file descriptor will be kept open
53174 ** and possibly used for a transaction later on. Also, write-access
53181 ** may mean that the pager was in the error-state when this
53184 if( !isOpen(pPager->jfd) ){
53185 sqlite3_vfs * const pVfs = pPager->pVfs;
53188 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
53192 assert( !pPager->tempFile );
53193 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
53194 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
53197 sqlite3OsClose(pPager->jfd);
53204 ** playing back the hot-journal so that we don't end up with
53210 if( isOpen(pPager->jfd) ){
53214 rc = pager_playback(pPager, !pPager->tempFile);
53215 pPager->eState = PAGER_OPEN;
53217 }else if( !pPager->exclusiveMode ){
53223 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
53242 assert( pPager->eState==PAGER_OPEN );
53243 assert( (pPager->eLock==SHARED_LOCK)
53244 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
53248 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
53249 /* The shared-lock has just been acquired then check to
53253 ** single unnecessary sqlite3OsRead() call at the start-up.
53257 ** a 32-bit counter that is incremented with each change. The
53265 char dbFileVers[sizeof(pPager->dbFileVers)];
53268 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
53276 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
53286 sqlite3OsUnfetch(pPager->fd, 0, 0);
53291 /* If there is a WAL file in the file-system, open this database in WAL
53292 ** mode. Otherwise, the following function call is a no-op.
53296 assert( pPager->pWal==0 || rc==SQLITE_OK );
53305 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
53306 rc = pagerPagecount(pPager, &pPager->dbSize);
53313 assert( pPager->eState==PAGER_OPEN );
53315 pPager->eState = PAGER_READER;
53316 pPager->hasHeldSharedLock = 1;
53327 ** nothing to rollback, so this routine is a no-op.
53330 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
53331 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
53344 ** getPageNormal() -- The normal getter
53345 ** getPageError() -- Used if the pager is in an error state
53346 ** getPageMmap() -- Used if memory-mapped I/O is enabled
53368 ** a) When reading a free-list leaf page from the database, and
53386 ** to find a page in the in-memory cache first. If the page is not already
53388 ** just returns 0. This routine acquires a read-lock the first time it
53404 assert( pPager->errCode==SQLITE_OK );
53405 assert( pPager->eState>=PAGER_READER );
53407 assert( pPager->hasHeldSharedLock==1 );
53410 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
53413 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
53420 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
53422 assert( pPg->pgno==pgno );
53423 assert( pPg->pPager==pPager || pPg->pPager==0 );
53426 if( pPg->pPager && !noContent ){
53430 pPager->aStat[PAGER_STAT_HIT]++;
53445 pPg->pPager = pPager;
53447 assert( !isOpen(pPager->fd) || !MEMDB );
53448 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
53449 if( pgno>pPager->mxPgno ){
53454 /* Failure to set the bits in the InJournal bit-vectors is benign.
53461 if( pgno<=pPager->dbOrigSize ){
53462 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
53469 memset(pPg->pData, 0, pPager->pageSize);
53472 assert( pPg->pPager==pPager );
53473 pPager->aStat[PAGER_STAT_MISS]++;
53494 /* The page getter for when memory-mapped I/O is enabled */
53505 /* It is acceptable to use a read-only (mmap) page for any page except
53506 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
53508 ** temporary or in-memory database. */
53510 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
53515 assert( pPager->xCodec==0 );
53525 assert( pPager->eState>=PAGER_READER );
53527 assert( pPager->hasHeldSharedLock==1 );
53528 assert( pPager->errCode==SQLITE_OK );
53531 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
53539 rc = sqlite3OsFetch(pPager->fd,
53540 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
53543 if( pPager->eState>PAGER_READER || pPager->tempFile ){
53549 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
53575 assert( pPager->errCode!=SQLITE_OK );
53577 return pPager->errCode;
53589 return pPager->xGet(pPager, pgno, ppPage, flags);
53593 ** Acquire a page if it is already in the in-memory cache. Do
53607 assert( pPager->pPCache!=0 );
53608 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
53609 assert( pPage==0 || pPager->hasHeldSharedLock );
53611 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
53627 TESTONLY( Pager *pPager = pPg->pPager; )
53629 if( pPg->flags & PGHDR_MMAP ){
53630 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
53636 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
53644 assert( pPg->pgno==1 );
53645 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
53646 pPager = pPg->pPager;
53657 ** to the start of it. If there are active savepoints, open the sub-journal
53675 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
53677 assert( pPager->eState==PAGER_WRITER_LOCKED );
53679 assert( pPager->pInJournal==0 );
53681 /* If already in the error state, this function is a no-op. But on
53684 if( NEVER(pPager->errCode) ) return pPager->errCode;
53686 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
53687 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
53688 if( pPager->pInJournal==0 ){
53693 if( !isOpen(pPager->jfd) ){
53694 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
53695 sqlite3MemJournalOpen(pPager->jfd);
53700 if( pPager->tempFile ){
53713 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
53717 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
53722 ** the sub-journal if necessary.
53726 pPager->nRec = 0;
53727 pPager->journalOff = 0;
53728 pPager->setMaster = 0;
53729 pPager->journalHdr = 0;
53735 sqlite3BitvecDestroy(pPager->pInJournal);
53736 pPager->pInJournal = 0;
53738 assert( pPager->eState==PAGER_WRITER_LOCKED );
53739 pPager->eState = PAGER_WRITER_CACHEMOD;
53746 ** Begin a write-transaction on the specified pager object. If a
53747 ** write-transaction has already been opened, this function is a no-op.
53754 ** If the subjInMemory argument is non-zero, then any sub-journal opened
53755 ** within this transaction will be opened as an in-memory file. This
53756 ** has no effect if the sub-journal is already opened (as it may be when
53758 ** sub-journal. If the subjInMemory argument is zero, then any required
53759 ** sub-journal is implemented in-memory if pPager is an in-memory database,
53765 if( pPager->errCode ) return pPager->errCode;
53766 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
53767 pPager->subjInMemory = (u8)subjInMemory;
53769 if( ALWAYS(pPager->eState==PAGER_READER) ){
53770 assert( pPager->pInJournal==0 );
53776 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
53781 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
53786 ** The busy-handler is not invoked if another connection already
53787 ** holds the write-lock. If possible, the upper layer will call it.
53789 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
53793 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
53808 ** transactions may copy data from the sub-journal into the database
53812 pPager->eState = PAGER_WRITER_LOCKED;
53813 pPager->dbHintSize = pPager->dbSize;
53814 pPager->dbFileSize = pPager->dbSize;
53815 pPager->dbOrigSize = pPager->dbSize;
53816 pPager->journalOff = 0;
53819 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
53820 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
53832 Pager *pPager = pPg->pPager;
53836 i64 iOff = pPager->journalOff;
53841 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
53843 assert( pPager->journalHdr<=pPager->journalOff );
53844 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
53848 ** page in the block above, set the need-sync flag for the page.
53854 pPg->flags |= PGHDR_NEED_SYNC;
53856 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
53858 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
53860 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
53863 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
53864 pPager->journalOff, pPager->pageSize));
53867 PAGERID(pPager), pPg->pgno,
53868 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
53870 pPager->journalOff += 8 + pPager->pageSize;
53871 pPager->nRec++;
53872 assert( pPager->pInJournal!=0 );
53873 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
53876 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
53883 ** main journal or sub-journal as required. If the page is written into
53889 Pager *pPager = pPg->pPager;
53892 /* This routine is not called unless a write-transaction has already
53896 assert( pPager->eState==PAGER_WRITER_LOCKED
53897 || pPager->eState==PAGER_WRITER_CACHEMOD
53898 || pPager->eState==PAGER_WRITER_DBMOD
53901 assert( pPager->errCode==0 );
53902 assert( pPager->readOnly==0 );
53906 ** obtained the necessary locks to begin the write-transaction, but the
53914 if( pPager->eState==PAGER_WRITER_LOCKED ){
53918 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
53928 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
53929 if( pPager->pInJournal!=0
53930 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
53933 if( pPg->pgno<=pPager->dbOrigSize ){
53939 if( pPager->eState!=PAGER_WRITER_DBMOD ){
53940 pPg->flags |= PGHDR_NEED_SYNC;
53943 PAGERID(pPager), pPg->pgno,
53944 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
53948 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
53953 pPg->flags |= PGHDR_WRITEABLE;
53958 if( pPager->nSavepoint>0 ){
53963 if( pPager->dbSize<pPg->pgno ){
53964 pPager->dbSize = pPg->pgno;
53987 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
53988 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
53995 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
53996 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
53998 /* This trick assumes that both the page-size and sector-size are
54002 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
54004 nPageCount = pPager->dbSize;
54005 if( pPg->pgno>nPageCount ){
54006 nPage = (pPg->pgno - pg1)+1;
54007 }else if( (pg1+nPagePerSector-1)>nPageCount ){
54008 nPage = nPageCount+1-pg1;
54013 assert(pg1<=pPg->pgno);
54014 assert((pg1+nPage)>pPg->pgno);
54019 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
54024 if( pPage->flags&PGHDR_NEED_SYNC ){
54031 if( pPage->flags&PGHDR_NEED_SYNC ){
54049 pPage->flags |= PGHDR_NEED_SYNC;
54055 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
54056 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
54068 ** fit on a single disk sector. In this case all co-resident pages
54075 Pager *pPager = pPg->pPager;
54076 assert( (pPg->flags & PGHDR_MMAP)==0 );
54077 assert( pPager->eState>=PAGER_WRITER_LOCKED );
54079 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
54080 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
54082 }else if( pPager->errCode ){
54083 return pPager->errCode;
54084 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
54085 assert( pPager->tempFile==0 );
54099 return pPg->flags & PGHDR_WRITEABLE;
54117 ** This optimization cannot be used with a temp-file, as the page may
54124 Pager *pPager = pPg->pPager;
54125 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
54126 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
54127 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
54128 pPg->flags |= PGHDR_DONT_WRITE;
54129 pPg->flags &= ~PGHDR_WRITEABLE;
54130 testcase( pPg->flags & PGHDR_NEED_SYNC );
54137 ** change-counter, stored as a 4-byte big-endian integer starting at
54141 ** But this only happens if the pPager->changeCountDone flag is false.
54151 ** The isDirectMode flag may only be non-zero if the library was compiled
54153 ** if isDirect is non-zero, then the database file is updated directly
54160 assert( pPager->eState==PAGER_WRITER_CACHEMOD
54161 || pPager->eState==PAGER_WRITER_DBMOD
54166 ** atomic-write optimization is enabled in this build, then isDirect
54170 ** The idea is that if the atomic-write optimization is not
54183 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
54186 assert( !pPager->tempFile && isOpen(pPager->fd) );
54193 ** operating in direct-mode, make page 1 writable. When not in
54195 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
54208 assert( pPager->dbFileSize>0 );
54209 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
54211 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
54212 pPager->aStat[PAGER_STAT_WRITE]++;
54215 /* Update the pager's copy of the change-counter. Otherwise, the
54217 ** flushed (as the change-counter values will not match). */
54219 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
54220 pPager->changeCountDone = 1;
54223 pPager->changeCountDone = 1;
54234 ** Sync the database file to disk. This is a no-op for in-memory databases
54237 ** If successful, or if called on a pager for which it is a no-op, this
54243 if( isOpen(pPager->fd) ){
54245 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
54248 if( rc==SQLITE_OK && !pPager->noSync ){
54250 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
54256 ** This function may only be called while a write-transaction is active in
54257 ** rollback. If the connection is in WAL mode, this call is a no-op.
54267 int rc = pPager->errCode;
54270 assert( pPager->eState==PAGER_WRITER_CACHEMOD
54271 || pPager->eState==PAGER_WRITER_DBMOD
54272 || pPager->eState==PAGER_WRITER_LOCKED
54290 ** * The database file change-counter is updated,
54291 ** * the journal is synced (unless the atomic-write optimization is used),
54303 ** If the final parameter - noSync - is true, then the database file itself
54315 assert( pPager->eState==PAGER_WRITER_LOCKED
54316 || pPager->eState==PAGER_WRITER_CACHEMOD
54317 || pPager->eState==PAGER_WRITER_DBMOD
54318 || pPager->eState==PAGER_ERROR
54323 if( NEVER(pPager->errCode) ) return pPager->errCode;
54329 pPager->zFilename, zMaster, pPager->dbSize));
54332 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
54334 assert( MEMDB==0 || pPager->tempFile );
54335 assert( isOpen(pPager->fd) || pPager->tempFile );
54337 /* If this is an in-memory db, or no pages have been written to, or this
54338 ** function has already been called, it is mostly a no-op. However, any
54340 sqlite3BackupRestart(pPager->pBackup);
54343 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
54347 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
54350 pList->pDirty = 0;
54354 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
54358 sqlite3PcacheCleanAll(pPager->pPCache);
54361 /* The bBatch boolean is true if the batch-atomic-write commit method
54362 ** should be used. No rollback journal is created if batch-atomic-write
54365 sqlite3_file *fd = pPager->fd;
54369 && !pPager->noSync
54370 && sqlite3JournalIsInMemory(pPager->jfd);
54376 /* The following block updates the change-counter. Exactly how it
54377 ** does this depends on whether or not the atomic-update optimization
54381 ** * The file-system supports the atomic-write property for
54382 ** blocks of size page-size, and
54383 ** * This commit is not part of a multi-file transaction, and
54388 ** counter in 'indirect-mode'. If the optimization is compiled in but
54391 ** pager_incr_changecounter() to update the change-counter in indirect
54395 ** then call pager_incr_changecounter() to update the change-counter
54401 assert( isOpen(pPager->jfd)
54402 || pPager->journalMode==PAGER_JOURNALMODE_OFF
54403 || pPager->journalMode==PAGER_JOURNALMODE_WAL
54405 if( !zMaster && isOpen(pPager->jfd)
54406 && pPager->journalOff==jrnlBufferSize(pPager)
54407 && pPager->dbSize>=pPager->dbOrigSize
54408 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
54410 /* Update the db file change counter via the direct-write method. The
54411 ** following call will modify the in-memory representation of page 1
54413 ** directly to the database file. Because of the atomic-write
54414 ** property of the host file-system, this is safe.
54418 rc = sqlite3JournalCreate(pPager->jfd);
54427 rc = sqlite3JournalCreate(pPager->jfd);
54437 ** or if zMaster is NULL (no master journal), then this call is a no-op.
54443 ** If the atomic-update optimization is being used, this sync will not
54446 ** Because the change-counter page was just modified, unless the
54447 ** atomic-update optimization is used it is almost certain that the
54451 ** xSync() call will be changed to a no-op by the OS anyhow.
54460 sqlite3OsClose(pPager->jfd);
54464 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
54477 sqlite3PcacheCleanAll(pPager->pPCache);
54482 ** last page in the db image moved to the free-list. In this case the
54485 if( pPager->dbSize>pPager->dbFileSize ){
54486 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
54487 assert( pPager->eState==PAGER_WRITER_DBMOD );
54502 pPager->eState = PAGER_WRITER_FINISHED;
54511 ** synced to disk. The journal file still exists in the file-system
54513 ** be used as a hot-journal and the current transaction rolled back.
54517 ** for hot-journal rollback. Once this is done the transaction is
54529 if( NEVER(pPager->errCode) ) return pPager->errCode;
54531 assert( pPager->eState==PAGER_WRITER_LOCKED
54532 || pPager->eState==PAGER_WRITER_FINISHED
54533 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
54538 ** this transaction, the pager is running in exclusive-mode and is
54539 ** using persistent journals, then this function is a no-op.
54543 ** a hot-journal during hot-journal rollback, 0 changes will be made
54548 if( pPager->eState==PAGER_WRITER_LOCKED
54549 && pPager->exclusiveMode
54550 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
54552 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
54553 pPager->eState = PAGER_READER;
54558 pPager->iDataVersion++;
54559 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
54565 ** transaction are reverted and the current write-transaction is closed.
54575 ** in-memory cache pages to the state they were in when the transaction
54584 ** In WAL mode, all cache-entries containing data modified within the
54586 ** their pre-transaction state by re-reading data from the database or
54593 /* PagerRollback() is a no-op if called in READER or OPEN state. If
54598 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
54599 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
54603 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
54604 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
54606 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
54607 int eState = pPager->eState;
54614 pPager->errCode = SQLITE_ABORT;
54615 pPager->eState = PAGER_ERROR;
54623 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
54636 ** Return TRUE if the database file is opened read-only. Return FALSE
54640 return pPager->readOnly;
54648 return sqlite3PcacheRefCount(pPager->pPCache);
54657 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
54659 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
54661 + pPager->pageSize;
54677 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
54678 a[1] = sqlite3PcachePagecount(pPager->pPCache);
54679 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
54680 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
54681 a[4] = pPager->eState;
54682 a[5] = pPager->errCode;
54683 a[6] = pPager->aStat[PAGER_STAT_HIT];
54684 a[7] = pPager->aStat[PAGER_STAT_MISS];
54685 a[8] = 0; /* Used to be pPager->nOvfl */
54686 a[9] = pPager->nRead;
54687 a[10] = pPager->aStat[PAGER_STAT_WRITE];
54696 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
54710 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
54712 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
54717 ** Return true if this is an in-memory or temp-file backed pager.
54720 return pPager->tempFile;
54727 ** equal to nSavepoint, then this function is a no-op.
54730 ** occurs while opening the sub-journal file, then an IO error code is
54735 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
54739 assert( pPager->eState>=PAGER_WRITER_LOCKED );
54741 assert( nSavepoint>nCurrent && pPager->useJournal );
54748 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
54753 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
54754 pPager->aSavepoint = aNew;
54758 aNew[ii].nOrig = pPager->dbSize;
54759 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
54760 aNew[ii].iOffset = pPager->journalOff;
54764 aNew[ii].iSubRec = pPager->nSubRec;
54765 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
54770 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
54772 pPager->nSavepoint = ii+1;
54774 assert( pPager->nSavepoint==nSavepoint );
54779 assert( pPager->eState>=PAGER_WRITER_LOCKED );
54782 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
54802 ** (the first created). A value of (Pager.nSavepoint-1) means operate
54804 ** (Pager.nSavepoint-1), then this function is a no-op.
54821 int rc = pPager->errCode;
54830 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
54839 for(ii=nNew; ii<pPager->nSavepoint; ii++){
54840 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
54842 pPager->nSavepoint = nNew;
54845 ** the sub-journal to zero bytes in size. */
54847 if( nNew==0 && isOpen(pPager->sjfd) ){
54848 /* Only truncate if it is an in-memory sub-journal. */
54849 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
54850 rc = sqlite3OsTruncate(pPager->sjfd, 0);
54853 pPager->nSubRec = 0;
54857 ** If this is a temp-file, it is possible that the journal file has
54861 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
54862 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
54873 pPager->journalMode==PAGER_JOURNALMODE_OFF
54874 && pPager->eState>=PAGER_WRITER_CACHEMOD
54876 pPager->errCode = SQLITE_ABORT;
54877 pPager->eState = PAGER_ERROR;
54889 ** Except, if the pager is in-memory only, then return an empty string if
54893 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
54894 ** participate in shared-cache.
54897 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
54904 return pPager->pVfs;
54913 return pPager->fd;
54922 return pPager->jfd;
54924 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
54932 return pPager->zJournal;
54946 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
54947 pPager->xCodec = pPager->memDb ? 0 : xCodec;
54948 pPager->xCodecSizeChng = xCodecSizeChng;
54949 pPager->xCodecFree = xCodecFree;
54950 pPager->pCodec = pCodec;
54955 return pPager->pCodec;
54967 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
54975 return pPager->eState;
54989 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
54997 ** If the fourth argument, isCommit, is non-zero, then this page is being
55007 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
55011 assert( pPg->nRef>0 );
55012 assert( pPager->eState==PAGER_WRITER_CACHEMOD
55013 || pPager->eState==PAGER_WRITER_DBMOD
55017 /* In order to be able to rollback, an in-memory database must journal
55020 assert( pPager->tempFile || !MEMDB );
55021 if( pPager->tempFile ){
55028 ** sub-journal now. This is required to handle the following scenario:
55036 ** If page X were not written to the sub-journal here, it would not
55040 ** subjournalPage() may need to allocate space to store pPg->pgno into
55044 if( (pPg->flags & PGHDR_DIRTY)!=0
55051 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
55052 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
55054 /* If the journal needs to be sync()ed before page pPg->pgno can
55055 ** be written to, store pPg->pgno in local variable needSyncPgno.
55058 ** the journal needs to be sync()ed before database page pPg->pgno
55061 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
55062 needSyncPgno = pPg->pgno;
55063 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
55064 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
55065 assert( pPg->flags&PGHDR_DIRTY );
55068 /* If the cache contains a page with page-number pgno, remove it
55073 pPg->flags &= ~PGHDR_NEED_SYNC;
55075 assert( !pPgOld || pPgOld->nRef==1 );
55077 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
55078 if( pPager->tempFile ){
55079 /* Do not discard pages from an in-memory database since we might
55081 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
55087 origPgno = pPg->pgno;
55091 /* For an in-memory database, make sure the original page continues
55095 if( pPager->tempFile && pPgOld ){
55101 /* If needSyncPgno is non-zero, then the journal file needs to be
55103 ** Currently, no such page exists in the page-cache and the
55105 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
55108 ** If the attempt to load the page into the page-cache fails, (due
55118 if( needSyncPgno<=pPager->dbOrigSize ){
55119 assert( pPager->pTmpSpace!=0 );
55120 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
55124 pPgHdr->flags |= PGHDR_NEED_SYNC;
55140 assert( pPg->pgno!=iNew );
55141 pPg->flags = flags;
55149 assert( pPg->nRef>0 || pPg->pPager->memDb );
55150 return pPg->pData;
55158 return pPg->pExtra;
55162 ** Get/set the locking-mode for this pager. Parameter eMode must be one
55165 ** the locking-mode is set to the value specified.
55169 ** locking-mode.
55177 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
55178 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
55179 pPager->exclusiveMode = (u8)eMode;
55181 return (int)pPager->exclusiveMode;
55185 ** Set the journal-mode for this pager. Parameter eMode must be one of:
55197 ** * An in-memory database can only have its journal_mode set to _OFF
55202 ** The returned indicate the current (possibly updated) journal-mode.
55205 u8 eOld = pPager->journalMode; /* Prior journalmode */
55226 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
55228 /* Do allow the journalmode of an in-memory database to be set to
55241 assert( pPager->eState!=PAGER_ERROR );
55242 pPager->journalMode = (u8)eMode;
55255 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
55256 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
55266 sqlite3OsClose(pPager->jfd);
55267 if( pPager->eLock>=RESERVED_LOCK ){
55268 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
55271 int state = pPager->eState;
55276 if( pPager->eState==PAGER_READER ){
55281 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
55288 assert( state==pPager->eState );
55291 sqlite3OsClose(pPager->jfd);
55296 return (int)pPager->journalMode;
55303 return (int)pPager->journalMode;
55313 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
55314 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
55319 ** Get/set the size-limit used for persistent journal files.
55321 ** Setting the size limit to -1 means no limit is enforced.
55322 ** An attempt to set a limit smaller than -1 is a no-op.
55325 if( iLimit>=-1 ){
55326 pPager->journalSizeLimit = iLimit;
55327 sqlite3WalLimit(pPager->pWal, iLimit);
55329 return pPager->journalSizeLimit;
55333 ** Return a pointer to the pPager->pBackup variable. The backup module
55339 return &pPager->pBackup;
55344 ** Unless this is an in-memory or temporary database, clear the pager cache.
55347 assert( MEMDB==0 || pPager->tempFile );
55348 if( pPager->tempFile==0 ) pager_reset(pPager);
55369 if( pPager->pWal ){
55370 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
55371 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
55372 pPager->pBusyHandlerArg,
55373 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
55381 return sqlite3WalCallback(pPager->pWal);
55386 ** primitives necessary for write-ahead logging.
55389 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
55390 if( pPager->noLock ) return 0;
55391 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
55401 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
55414 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
55415 ** lock on the database file and use heap-memory to store the wal-index
55416 ** in. Otherwise, use the normal shared-memory.
55421 assert( pPager->pWal==0 && pPager->tempFile==0 );
55422 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
55424 /* If the pager is already in exclusive-mode, the WAL module will use
55425 ** heap-memory for the wal-index instead of the VFS shared-memory
55429 if( pPager->exclusiveMode ){
55437 rc = sqlite3WalOpen(pPager->pVfs,
55438 pPager->fd, pPager->zWal, pPager->exclusiveMode,
55439 pPager->journalSizeLimit, &pPager->pWal
55453 ** file (not a temp file or an in-memory database), and the WAL file
55459 ** If the pager is open on a temp-file (or in-memory database), or if
55465 int *pbOpen /* OUT: Set to true if call is a no-op */
55470 assert( pPager->eState==PAGER_OPEN || pbOpen );
55471 assert( pPager->eState==PAGER_READER || !pbOpen );
55473 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
55475 if( !pPager->tempFile && !pPager->pWal ){
55479 sqlite3OsClose(pPager->jfd);
55483 pPager->journalMode = PAGER_JOURNALMODE_WAL;
55484 pPager->eState = PAGER_OPEN;
55505 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
55507 /* If the log file is not already open, but does exist in the file-system,
55511 if( !pPager->pWal ){
55516 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
55525 ** the database file, the log and log-summary files will be deleted.
55527 if( rc==SQLITE_OK && pPager->pWal ){
55530 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
55531 pPager->pageSize, (u8*)pPager->pTmpSpace);
55532 pPager->pWal = 0;
55534 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
55547 if( pPager->pWal ){
55548 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
55560 if( pPager->pWal ){
55561 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
55574 if( pPager->pWal ){
55575 rc = sqlite3WalSnapshotRecover(pPager->pWal);
55586 ** A read-lock must be held on the pager when this function is called. If
55593 assert( pPager->eState>=PAGER_READER );
55594 return sqlite3WalFramesize(pPager->pWal);
55614 ** This file contains the implementation of a write-ahead log (WAL) used in
55617 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
55636 ** big-endian 32-bit unsigned integer values:
55642 ** 16: Salt-1, random integer incremented with each checkpoint
55643 ** 20: Salt-2, a different random integer changing with each ckpt
55644 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
55645 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
55647 ** Immediately following the wal-header are zero or more frames. Each
55648 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
55649 ** of page data. The frame-header is six big-endian 32-bit unsigned
55655 ** 8: Salt-1 (copied from the header)
55656 ** 12: Salt-2 (copied from the header)
55657 ** 16: Checksum-1.
55658 ** 20: Checksum-2.
55663 ** (1) The salt-1 and salt-2 values in the frame-header match
55664 ** salt values in the wal-header
55666 ** (2) The checksum values in the final 8 bytes of the frame-header
55671 ** The checksum is computed using 32-bit big-endian integers if the
55673 ** is computed using little-endian if the magic number is 0x377f0682.
55675 ** big-endian format regardless of which byte order is used to compute
55677 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
55680 ** for i from 0 to n-1 step 2:
55687 ** of the sequence being summed.) The s1 value spans all 32-bit
55690 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
55691 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
55692 ** The VFS.xSync operations serve as write barriers - all writes launched
55696 ** After each checkpoint, the salt-1 value is incremented and the salt-2
55725 ** data structure called the wal-index is maintained to expedite the
55728 ** WAL-INDEX FORMAT
55730 ** Conceptually, the wal-index is shared memory, though VFS implementations
55731 ** might choose to implement the wal-index using a mmapped file. Because
55732 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
55736 ** In the default unix and windows implementation, the wal-index is a mmapped
55737 ** file whose name is the database name with a "-shm" suffix added. For that
55738 ** reason, the wal-index is sometimes called the "shm" file.
55740 ** The wal-index is transient. After a crash, the wal-index can (and should
55742 ** to either truncate or zero the header of the wal-index when the last
55743 ** connection to it closes. Because the wal-index is transient, it can
55744 ** use an architecture-specific format; it does not have to be cross-platform.
55746 ** as big endian, the wal-index can store multi-byte values in the native
55749 ** The purpose of the wal-index is to answer this question quickly: Given
55754 ** The wal-index consists of a header region, followed by an one or
55757 ** The wal-index header contains the total number of frames within the WAL
55763 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
55765 ** wal-index.
55767 ** Each index block contains two sections, a page-mapping that contains the
55768 ** database page number associated with each wal frame, and a hash-table
55770 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
55771 ** for the first index block) 32-bit page numbers. The first entry in the
55772 ** first index-block contains the database page number corresponding to the
55777 ** The last index block in a wal-index usually contains less than the full
55778 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
55780 ** allocated size of the page-mapping array - the page-mapping array merely
55784 ** can be found by scanning the page-mapping sections of each index block
55790 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
55795 ** 1-based index of an entry in the mapping section of the same
55796 ** index block. Let K be the 1-based index of the largest entry in
55811 ** wrap-around.) Because the hash table is never more than half full,
55816 ** current index block. Otherwise the iMax-th mapping entry of the
55834 ** and to the wal-index) might be using a different value K1, where K1>K0.
55840 ** in the first place - which is what reader one wants. Meanwhile, the
55863 ** The maximum (and only) versions of the wal and wal-index formats
55867 ** values in the wal-header are correct and (b) the version field is not
55870 ** Similarly, if a client successfully reads a wal-index header (i.e. the
55872 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
55887 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
55897 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
55907 ** The following object holds a copy of the wal-index header content.
55909 ** The actual header in the wal-index consists of two copies of this
55916 ** Or it can be 1 to represent a 65536-byte page. The latter case was
55920 u32 iVersion; /* Wal-index version */
55924 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
55934 ** A copy of the following object occurs in the wal-index immediately
55957 ** holds read-lock K, then the value in aReadMark[K] is no greater than
55960 ** a special case; its value is never used and it exists as a place-holder
55989 ** We assume that 32-bit loads are atomic and so no locks are needed in
56004 ** only support mandatory file-locks, we do not read or write data
56017 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
56018 ** big-endian format in the first 4 bytes of a WAL file.
56021 ** file are calculated by treating all data as an array of 32-bit
56022 ** big-endian words. Otherwise, they are calculated by interpreting
56023 ** all data as 32-bit little-endian words.
56028 ** Return the offset of frame iFrame in the write-ahead log file,
56030 ** is to the start of the write-ahead log frame-header.
56033 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
56037 ** An open write-ahead log file is represented by an instance of the
56048 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
56050 i16 readLock; /* Which read lock is being held. -1 for none */
56052 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
56059 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
56060 WalIndexHdr hdr; /* Wal-index header for current transaction */
56064 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
56088 ** Each page of the wal-index mapping contains a hash-table made up of
56102 ** walIteratorInit() - Create a new iterator,
56103 ** walIteratorNext() - Step an iterator,
56104 ** walIteratorFree() - Free an iterator.
56117 } aSegment[1]; /* One for every 32KB page in the wal-index */
56121 ** Define the parameters of the hash tables in the wal-index file. There
56122 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
56123 ** wal-index.
56125 ** Changing any of these constants will alter the wal-index format and
56133 ** The block of page numbers associated with the first hash-table in a
56134 ** wal-index is smaller than usual. This is so that there is a complete
56135 ** hash-table on each aligned 32KB page of the wal-index.
56137 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
56139 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
56145 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
56146 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
56149 ** If the wal-index is currently smaller the iPage pages then the size
56150 ** of the wal-index might be increased, but only if it is safe to do
56151 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
56152 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
56154 ** If this call is successful, *ppPage is set to point to the wal-index
56161 /* Enlarge the pWal->apWiData[] array if required */
56162 if( pWal->nWiData<=iPage ){
56165 apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
56170 memset((void*)&apNew[pWal->nWiData], 0,
56171 sizeof(u32*)*(iPage+1-pWal->nWiData));
56172 pWal->apWiData = apNew;
56173 pWal->nWiData = iPage+1;
56177 if( pWal->apWiData[iPage]==0 ){
56178 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
56179 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
56180 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
56182 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
56183 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
56185 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
56186 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
56188 pWal->readOnly |= WAL_SHM_RDONLY;
56196 *ppPage = pWal->apWiData[iPage];
56202 ** Return a pointer to the WalCkptInfo structure in the wal-index.
56205 assert( pWal->nWiData>0 && pWal->apWiData[0] );
56206 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
56210 ** Return a pointer to the WalIndexHdr structure in the wal-index.
56213 assert( pWal->nWiData>0 && pWal->apWiData[0] );
56214 return (volatile WalIndexHdr*)pWal->apWiData[0];
56218 ** The argument to this macro must be of type u32. On a little-endian
56220 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
56222 ** of the input value as a little-endian integer.
56239 int nativeCksum, /* True for native byte-order, false for non-native */
56277 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
56278 sqlite3OsShmBarrier(pWal->pDbFd);
56283 ** Write the header information in pWal->hdr into the wal-index.
56285 ** The checksum on pWal->hdr is updated before it is written.
56291 assert( pWal->writeLock );
56292 pWal->hdr.isInit = 1;
56293 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
56294 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
56295 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
56297 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
56302 ** supplied by the caller. A frame-header is made up of a series of
56303 ** 4-byte big-endian integers, as follows:
56308 ** 8: Salt-1 (copied from the wal-header)
56309 ** 12: Salt-2 (copied from the wal-header)
56310 ** 16: Checksum-1.
56311 ** 20: Checksum-2.
56314 Wal *pWal, /* The write-ahead log */
56316 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
56320 int nativeCksum; /* True for native byte-order checksums */
56321 u32 *aCksum = pWal->hdr.aFrameCksum;
56325 if( pWal->iReCksum==0 ){
56326 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
56328 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
56330 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
56345 Wal *pWal, /* The write-ahead log */
56351 int nativeCksum; /* True for native byte-order checksums */
56352 u32 *aCksum = pWal->hdr.aFrameCksum;
56356 /* A frame is only valid if the salt values in the frame-header
56357 ** match the salt values in the wal-header.
56359 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
56371 ** all prior frams, the first 16 bytes of this frame-header,
56372 ** and the frame-data matches the checksum in the last 8
56373 ** bytes of this frame-header.
56375 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
56377 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
56401 return "WRITE-LOCK";
56403 return "CKPT-LOCK";
56405 return "RECOVER-LOCK";
56408 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
56409 lockIdx-WAL_READ_LOCK(0));
56418 ** A lock cannot be moved directly between shared and exclusive - it must go
56421 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
56425 if( pWal->exclusiveMode ) return SQLITE_OK;
56426 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
56428 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
56430 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
56434 if( pWal->exclusiveMode ) return;
56435 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
56437 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
56441 if( pWal->exclusiveMode ) return SQLITE_OK;
56442 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
56444 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
56446 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
56450 if( pWal->exclusiveMode ) return;
56451 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
56453 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
56459 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
56464 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
56465 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
56468 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
56473 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
56477 ** in the wal-index file. Set *piZero to one less than the frame
56507 iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
56510 *paPgno = &aPgno[-1];
56518 ** Return the number of the wal-index page that contains the hash-table
56519 ** and page-number array that contain entries corresponding to WAL frame
56520 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
56524 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
56540 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
56542 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
56547 ** than pWal->hdr.mxFrame.
56549 ** This function is called whenever pWal->hdr.mxFrame is decreased due
56552 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
56554 ** pWal->hdr.mxFrame advances to the point where those hash tables are
56565 assert( pWal->writeLock );
56566 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
56567 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
56568 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
56570 if( pWal->hdr.mxFrame==0 ) return;
56572 /* Obtain pointers to the hash-table and page-number array containing
56573 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
56574 ** that the page said hash-table and array reside on is already mapped.
56576 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
56577 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
56578 walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
56580 /* Zero all hash-table entries that correspond to frame numbers greater
56581 ** than pWal->hdr.mxFrame.
56583 iLimit = pWal->hdr.mxFrame - iZero;
56592 ** frame numbers greater than pWal->hdr.mxFrame.
56594 nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
56616 ** Set an entry in the wal-index that will map database page number
56627 /* Assuming the wal-index file was successfully mapped, populate the
56632 int idx; /* Value to write to hash-table slot */
56635 idx = iFrame - iZero;
56638 /* If this is the first entry to be added to this hash-table, zero the
56642 int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
56650 ** the hash-table before writing any new entries.
56657 /* Write the aPgno[] array entry and the hash-table slot. */
56660 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
56678 ** thing to check, so only do this occasionally - not on every
56699 ** Recover the wal-index by reading the write-ahead log file.
56702 ** wal-index to prevent other threads/processes from doing anything
56703 ** with the WAL or wal-index while recovery is running. The
56720 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
56723 assert( pWal->writeLock );
56724 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
56725 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
56727 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
56729 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
56738 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
56740 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
56758 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
56771 || szPage&(szPage-1)
56777 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
56778 pWal->szPage = szPage;
56779 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
56780 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
56783 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
56784 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
56786 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
56787 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
56817 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
56824 /* If nTruncate is non-zero, this is a commit record. */
56826 pWal->hdr.mxFrame = iFrame;
56827 pWal->hdr.nPage = nTruncate;
56828 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
56831 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
56832 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
56843 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
56844 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
56847 /* Reset the checkpoint-header. This is safe because this thread is
56852 pInfo->nBackfill = 0;
56853 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
56854 pInfo->aReadMark[0] = 0;
56855 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
56856 if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
56863 if( pWal->hdr.nPage ){
56866 pWal->hdr.mxFrame, pWal->zWalName
56873 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
56874 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
56879 ** Close an open wal-index.
56882 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
56884 for(i=0; i<pWal->nWiData; i++){
56885 sqlite3_free((void *)pWal->apWiData[i]);
56886 pWal->apWiData[i] = 0;
56889 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
56890 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
56901 ** client from unlinking the WAL or wal-index file. If another process
56910 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
56913 int bNoShm, /* True to run in heap-memory mode */
56942 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
56947 pRet->pVfs = pVfs;
56948 pRet->pWalFd = (sqlite3_file *)&pRet[1];
56949 pRet->pDbFd = pDbFd;
56950 pRet->readLock = -1;
56951 pRet->mxWalSize = mxWalSize;
56952 pRet->zWalName = zWalName;
56953 pRet->syncHeader = 1;
56954 pRet->padToSectorBoundary = 1;
56955 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
56957 /* Open file handle on the write-ahead log file. */
56959 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
56961 pRet->readOnly = WAL_RDONLY;
56966 sqlite3OsClose(pRet->pWalFd);
56970 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
56972 pRet->padToSectorBoundary = 0;
56984 if( pWal ) pWal->mxWalSize = iLimit;
57006 iMin = p->iPrior;
57008 for(i=p->nSegment-1; i>=0; i--){
57009 struct WalSegment *pSegment = &p->aSegment[i];
57010 while( pSegment->iNext<pSegment->nEntry ){
57011 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
57015 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
57019 pSegment->iNext++;
57023 *piPage = p->iPrior = iRet;
57051 const u32 *aContent, /* Pages in wal - keys for the sort */
57115 ht_slot *aList; /* Pointer to sub-list content */
57123 struct Sublist aSub[13]; /* Array of sub-lists */
57127 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
57136 assert( p->aList && p->nList<=(1<<iSub) );
57137 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
57138 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
57149 assert( p->nList<=(1<<iSub) );
57150 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
57151 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
57161 assert( aContent[aList[i]] > aContent[aList[i-1]] );
57192 ht_slot *aTmp; /* Temp space used by merge-sort */
57198 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
57199 iLast = pWal->hdr.mxFrame;
57204 + (nSegment-1)*sizeof(struct WalSegment)
57211 p->nSegment = nSegment;
57213 /* Allocate temporary space used by the merge-sort routine. This block
57236 nEntry = (int)(iLast - iZero);
57238 nEntry = (int)((u32*)aHash - (u32*)aPgno);
57240 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
57247 p->aSegment[i].iZero = iZero;
57248 p->aSegment[i].nEntry = nEntry;
57249 p->aSegment[i].aIndex = aIndex;
57250 p->aSegment[i].aPgno = (u32 *)aPgno;
57265 ** busy-handler function. Invoke it and retry the lock until either the
57266 ** lock is successfully obtained or the busy-handler returns 0.
57283 ** The cache of the wal-index header must be valid to call this function.
57284 ** Return the page-size in bytes used by the database.
57287 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
57296 ** file - there are no readers that may attempt to read a frame from
57299 ** This function updates the shared-memory structures so that the next
57304 ** new wal-index header. It should be passed a pseudo-random value (i.e.
57310 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
57311 pWal->nCkpt++;
57312 pWal->hdr.mxFrame = 0;
57314 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
57316 pInfo->nBackfill = 0;
57317 pInfo->nBackfillAttempted = 0;
57318 pInfo->aReadMark[1] = 0;
57319 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
57320 assert( pInfo->aReadMark[0]==0 );
57332 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
57338 ** in the WAL and can be recovered following a power-loss or hard reset.
57345 ** This routine uses and updates the nBackfill field of the wal-index header.
57364 int szPage; /* Database page-size */
57377 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
57386 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
57395 mxSafeFrame = pWal->hdr.mxFrame;
57396 mxPage = pWal->hdr.nPage;
57398 /* Thread-sanitizer reports that the following is an unsafe read,
57406 u32 y = pInfo->aReadMark[i];
57408 assert( y<=pWal->hdr.mxFrame );
57411 pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
57422 if( pInfo->nBackfill<mxSafeFrame
57426 u32 nBackfill = pInfo->nBackfill;
57428 pInfo->nBackfillAttempted = mxSafeFrame;
57431 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
57438 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
57440 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
57449 if( db->u1.isInterrupted ){
57450 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
57458 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
57460 iOffset = (iDbpage-1)*(i64)szPage;
57462 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
57468 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
57469 i64 szDb = pWal->hdr.nPage*(i64)szPage;
57471 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
57473 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
57477 pInfo->nBackfill = mxSafeFrame;
57498 assert( pWal->writeLock );
57499 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
57504 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
57505 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
57508 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
57514 ** wal-index header in shared memory, as all subsequent reader or
57518 ** the wal-index header do not match the contents of the
57519 ** file-system. To avoid this, update the wal-index header to
57522 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
57524 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
57542 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
57544 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
57548 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
57564 int isDelete = 0; /* True to unlink wal and wal-index files */
57567 ** ordinary, rollback-mode locking methods, this guarantees that the
57570 ** the wal and wal-index files.
57575 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
57577 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
57578 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
57584 int bPersist = -1;
57586 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
57590 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
57593 }else if( pWal->mxWalSize>=0 ){
57597 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
57606 sqlite3OsClose(pWal->pWalFd);
57609 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
57613 sqlite3_free((void *)pWal->apWiData);
57620 ** Try to read the wal-index header. Return 0 on success and 1 if
57623 ** The wal-index is in shared memory. Another thread or process might
57630 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
57633 ** If the checksum cannot be verified return non-zero. If the header
57641 /* The first page of the wal-index must be mapped at this point. */
57642 assert( pWal->nWiData>0 && pWal->apWiData[0] );
57647 ** from the file. If this happens, return non-zero.
57649 ** There are two copies of the header at the beginning of the wal-index.
57663 return 1; /* Malformed header - probably all zeros */
57665 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
57670 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
57672 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
57673 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
57674 testcase( pWal->szPage<=32768 );
57675 testcase( pWal->szPage>=65536 );
57686 #define WAL_RETRY (-1)
57689 ** Read the wal-index header from the wal-index and into pWal->hdr.
57690 ** If the wal-header appears to be corrupt, try to reconstruct the
57691 ** wal-index from the WAL before returning.
57693 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
57694 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
57697 ** If the wal-index header is successfully read, return SQLITE_OK.
57703 volatile u32 *page0; /* Chunk of wal-index containing header */
57705 /* Ensure that page 0 of the wal-index (the page that contains the
57706 ** wal-index header) is mapped. Return early if an error occurs here.
57713 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
57715 ** confirm that another write-capable connection has the shared-memory
57716 ** open, and hence the content of the shared-memory is unreliable,
57717 ** since the shared-memory might be inconsistent with the WAL file
57720 assert( pWal->writeLock==0 );
57721 assert( pWal->readOnly & WAL_SHM_RDONLY );
57722 pWal->bShmUnreliable = 1;
57723 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
57726 return rc; /* Any other non-OK return is just an error */
57729 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
57733 assert( page0!=0 || pWal->writeLock==0 );
57735 /* If the first page of the wal-index has been mapped, try to read the
57736 ** wal-index header immediately, without holding any lock. This usually
57737 ** works, but may fail if the wal-index header is corrupt or currently
57745 assert( badHdr==0 || pWal->writeLock==0 );
57747 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
57753 pWal->writeLock = 1;
57757 /* If the wal-index header is still malformed even while holding
57765 pWal->writeLock = 0;
57771 ** sure the wal-index was not constructed with some future format that
57774 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
57777 if( pWal->bShmUnreliable ){
57780 pWal->bShmUnreliable = 0;
57781 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
57787 pWal->exclusiveMode = WAL_NORMAL_MODE;
57794 ** Open a transaction in a connection where the shared-memory is read-only
57795 ** and where we cannot verify that there is a separate write-capable connection
57796 ** on hand to keep the shared-memory up-to-date with the WAL file.
57798 ** This can happen, for example, when the shared-memory is implemented by
57799 ** memory-mapping a *-shm file, where a prior writer has shut down and
57800 ** left the *-shm file on disk, and now the present connection is trying
57801 ** to use that database but lacks write permission on the *-shm file.
57806 ** The *-wal file has been read and an appropriate wal-index has been
57807 ** constructed in pWal->apWiData[] using heap memory instead of shared
57814 ** WAL_RETRY, then the heap memory wal-index has been discarded and
57816 ** beginning (including attempting to map the *-shm file).
57829 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
57831 assert( pWal->bShmUnreliable );
57832 assert( pWal->readOnly & WAL_SHM_RDONLY );
57833 assert( pWal->nWiData>0 && pWal->apWiData[0] );
57843 pWal->readLock = 0;
57845 /* Check to see if a separate writer has attached to the shared-memory area,
57846 ** thus making the shared-memory "reliable" again. Do this by invoking
57850 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
57851 ** cause the heap-memory WAL-index to be discarded and the actual
57863 ** even if some external agent does a "chmod" to make the shared-memory
57867 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
57868 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
57874 /* We reach this point only if the real shared-memory is still unreliable.
57875 ** Assume the in-memory WAL-index substitute is correct and load it
57876 ** into pWal->hdr.
57878 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
57883 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
57888 /* If the wal file is too small to contain a wal-header and the
57889 ** wal-index header has mxFrame==0, then it must be safe to proceed
57895 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
57900 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
57904 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
57906 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
57913 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
57922 ** wal file since the heap-memory wal-index was created. If so, the
57923 ** heap-memory wal-index is discarded and WAL_RETRY returned to
57925 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
57926 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
57927 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
57935 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
57939 /* If nTruncate is non-zero, then a complete transaction has been
57947 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
57948 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
57954 for(i=0; i<pWal->nWiData; i++){
57955 sqlite3_free((void*)pWal->apWiData[i]);
57956 pWal->apWiData[i] = 0;
57958 pWal->bShmUnreliable = 0;
57977 ** to make a copy of the wal-index header into pWal->hdr. If the
57978 ** wal-index header has changed, *pChanged is set to 1 (as an indication
57980 ** flushed.) When useWal==1, the wal-index header is assumed to already
57991 ** bad luck when there is lots of contention for the wal-index, but that
57995 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
57996 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
57999 ** including frame number aReadMark[pWal->readLock]. The reader will
58000 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
58001 ** Or if pWal->readLock==0, then the reader will ignore the WAL
58004 ** this routine will always set pWal->readLock>0 on success.
58006 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
58016 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
58023 assert( pWal->readLock<0 ); /* Not currently locked */
58026 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
58034 ** paged out or take a page-fault that is time-consuming to resolve,
58048 VVA_ONLY( pWal->lockError = 1; )
58051 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
58052 sqlite3OsSleep(pWal->pVfs, nDelay);
58057 if( pWal->bShmUnreliable==0 ){
58069 if( pWal->apWiData[0]==0 ){
58074 ** code that determines whether or not the shared-memory region
58088 else if( pWal->bShmUnreliable ){
58093 assert( pWal->nWiData>0 );
58094 assert( pWal->apWiData[0]!=0 );
58096 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
58098 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
58107 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
58124 pWal->readLock = 0;
58134 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
58138 mxFrame = pWal->hdr.mxFrame;
58140 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
58141 mxFrame = pWal->pSnapshot->mxFrame;
58145 u32 thisMark = pInfo->aReadMark[i];
58152 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
58158 mxReadMark = pInfo->aReadMark[i] = mxFrame;
58168 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
58176 /* Now that the read-lock has been obtained, check that neither the
58177 ** value in the aReadMark[] array or the contents of the wal-index
58180 ** It is necessary to check that the wal-index header did not change
58181 ** between the time it was read and when the shared-lock was obtained
58184 ** that occur later in the log than pWal->hdr.mxFrame may have been
58187 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
58190 ** Before checking that the live wal-index header has not changed
58193 ** to read any frames earlier than minFrame from the wal file - they
58197 ** nBackfill and checking that the wal-header in shared-memory still
58198 ** matches the one cached in pWal->hdr, it is guaranteed that the
58199 ** checkpointer that set nBackfill was not working with a wal-index
58200 ** header newer than that cached in pWal->hdr. If it were, that could
58202 ** a version of page X that lies before pWal->minFrame (call that version
58205 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
58208 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
58210 pWal->minFrame = pInfo->nBackfill+1;
58212 if( pInfo->aReadMark[mxI]!=mxReadMark
58213 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
58218 assert( mxReadMark<=pWal->hdr.mxFrame );
58219 pWal->readLock = (i16)mxI;
58233 ** This is only really safe if the file-system is such that any page
58247 assert( pWal->readLock>=0 );
58251 int szPage = (int)pWal->szPage;
58254 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
58261 u32 i = pInfo->nBackfillAttempted;
58262 for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
58272 pgno = aPgno[i-iZero];
58273 iDbOff = (i64)(pgno-1) * szPage;
58277 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
58280 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
58288 pInfo->nBackfillAttempted = i-1;
58306 ** it takes a snapshot of the state of the WAL and wal-index for the current
58308 ** Other threads might append new content to the WAL and wal-index but
58322 WalIndexHdr *pSnapshot = pWal->pSnapshot;
58323 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
58338 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
58340 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
58341 ** is populated with the wal-index header corresponding to the head
58349 ** pSnapshot->mxFrame into the database file. Note that the
58354 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
58355 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
58361 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
58364 ** before checking pInfo->nBackfillAttempted.
58374 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
58375 ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr
58378 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
58379 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
58381 assert( pWal->readLock>0 );
58382 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
58404 ** read-lock.
58408 if( pWal->readLock>=0 ){
58409 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
58410 pWal->readLock = -1;
58428 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
58433 assert( pWal->readLock>=0 || pWal->lockError );
58435 /* If the "last page" field of the wal-index header snapshot is 0, then
58437 ** in this case as an optimization. Likewise, if pWal->readLock==0,
58441 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
58451 ** that adds entries to the wal-index (and possibly to this hash
58455 ** read transaction was opened may have been written incorrectly -
58462 ** if we had exclusive access to the hash-table:
58465 ** This condition filters out normal hash-table collisions.
58469 ** table after the current read-transaction had started.
58471 iMinHash = walFramePage(pWal->minFrame);
58472 for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
58487 if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){
58491 if( (nCollide--)==0 ){
58499 ** of the wal-index file content. Make sure the results agree with the
58504 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
58505 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
58532 sz = pWal->hdr.szPage;
58538 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
58545 if( pWal && ALWAYS(pWal->readLock>=0) ){
58546 return pWal->hdr.nPage;
58570 assert( pWal->readLock>=0 );
58571 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
58573 if( pWal->readOnly ){
58584 pWal->writeLock = 1;
58590 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
58592 pWal->writeLock = 0;
58604 if( pWal->writeLock ){
58606 pWal->writeLock = 0;
58607 pWal->iReCksum = 0;
58608 pWal->truncateOnCommit = 0;
58615 ** function moves the write-pointer back to the start of the transaction.
58627 if( ALWAYS(pWal->writeLock) ){
58628 Pgno iMax = pWal->hdr.mxFrame;
58631 /* Restore the clients cache of the wal-index header to the state it
58634 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
58636 for(iFrame=pWal->hdr.mxFrame+1;
58642 ** (b) has an outstanding reference, then xUndo is either a no-op
58654 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
58666 assert( pWal->writeLock );
58667 aWalData[0] = pWal->hdr.mxFrame;
58668 aWalData[1] = pWal->hdr.aFrameCksum[0];
58669 aWalData[2] = pWal->hdr.aFrameCksum[1];
58670 aWalData[3] = pWal->nCkpt;
58682 assert( pWal->writeLock );
58683 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
58685 if( aWalData[3]!=pWal->nCkpt ){
58686 /* This savepoint was opened immediately after the write-transaction
58691 aWalData[3] = pWal->nCkpt;
58694 if( aWalData[0]<pWal->hdr.mxFrame ){
58695 pWal->hdr.mxFrame = aWalData[0];
58696 pWal->hdr.aFrameCksum[0] = aWalData[1];
58697 pWal->hdr.aFrameCksum[1] = aWalData[2];
58709 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
58713 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
58720 if( pWal->readLock==0 ){
58722 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
58723 if( pInfo->nBackfill>0 ){
58726 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
58731 ** wal-index header to reflect this.
58734 ** at this point. But updating the actual wal-index header is also
58738 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
58744 pWal->readLock = -1;
58760 ** the next fsync should occur - passed from sqlite3WalFrames() into
58773 ** Do a sync when crossing the p->iSyncPoint boundary.
58786 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
58787 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
58788 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
58791 iAmt -= iFirstAmt;
58793 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
58794 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
58797 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
58812 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
58816 pData = pPage->pData;
58818 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
58822 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
58835 const int szPage = pWal->szPage;/* Database page size */
58838 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
58848 ** wal-file header. Otherwise, read them from the frame header of the
58850 assert( pWal->iReCksum>0 );
58851 if( pWal->iReCksum==1 ){
58854 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
58856 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
58857 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
58858 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
58860 iRead = pWal->iReCksum;
58861 pWal->iReCksum = 0;
58864 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
58871 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
58880 ** Write a set of frames to the log. The caller must hold the write-lock
58885 int szPage, /* Database page-size in bytes */
58903 assert( pWal->writeLock );
58910 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
58912 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
58917 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
58918 iFirst = pLive->mxFrame+1;
58922 ** log file, instead of appending to it at pWal->hdr.mxFrame.
58932 iFrame = pWal->hdr.mxFrame;
58934 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
58935 u32 aCksum[2]; /* Checksum for wal-header */
58940 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
58941 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
58942 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
58943 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
58947 pWal->szPage = szPage;
58948 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
58949 pWal->hdr.aFrameCksum[0] = aCksum[0];
58950 pWal->hdr.aFrameCksum[1] = aCksum[1];
58951 pWal->truncateOnCommit = 1;
58953 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
58954 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
58961 ** an out-of-order write following a WAL restart could result in
58966 if( pWal->syncHeader ){
58967 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
58971 assert( (int)pWal->szPage==szPage );
58975 w.pFd = pWal->pWalFd;
58983 for(p=pList; p; p=p->pDirty){
58988 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
58990 if( iFirst && (p->pDirty || isCommit==0) ){
58992 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
58997 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
58998 pWal->iReCksum = iWrite;
59003 pData = p->pData;
59005 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
59007 p->flags &= ~PGHDR_WAL_APPEND;
59014 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
59019 p->flags |= PGHDR_WAL_APPEND;
59023 if( isCommit && pWal->iReCksum ){
59044 if( pWal->padToSectorBoundary ){
59045 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
59046 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
59066 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
59067 i64 sz = pWal->mxWalSize;
59068 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
59072 pWal->truncateOnCommit = 0;
59075 /* Append data to the wal-index. It is not necessary to lock the
59076 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
59080 iFrame = pWal->hdr.mxFrame;
59081 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
59082 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
59084 rc = walIndexAppend(pWal, iFrame, p->pgno);
59088 nExtra--;
59089 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
59094 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
59097 pWal->hdr.mxFrame = iFrame;
59099 pWal->hdr.iChange++;
59100 pWal->hdr.nPage = nTruncate;
59102 /* If this is a commit, update the wal-index header too. */
59105 pWal->iCallback = iFrame;
59120 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
59136 int isChanged = 0; /* True if a new wal-index header is loaded */
59140 assert( pWal->ckptLock==0 );
59141 assert( pWal->writeLock==0 );
59143 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
59147 if( pWal->readOnly ) return SQLITE_READONLY;
59150 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
59154 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
59157 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
59164 pWal->ckptLock = 1;
59166 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
59170 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
59171 ** immediately, and a busy-handler is configured, it is invoked and the
59172 ** writer lock retried until either the busy-handler returns 0 or the
59178 pWal->writeLock = 1;
59186 /* Read the wal-index header. */
59189 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
59190 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
59197 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
59205 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
59206 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
59211 /* If a new wal-index header was loaded before the checkpoint was
59212 ** performed, then the pager-cache associated with pWal is now
59213 ** out of date. So zero the cached wal-index header to ensure that
59217 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
59223 pWal->ckptLock = 0;
59236 ret = pWal->iCallback;
59237 pWal->iCallback = 0;
59248 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
59250 ** transition out of exclusive-mode is successful, return 1. This
59255 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
59257 ** WAL is already in exclusive-locking mode - meaning that this
59258 ** routine is a no-op. The pager must already hold the exclusive lock
59261 ** If op is negative, then do a dry-run of the op==1 case but do
59268 assert( pWal->writeLock==0 );
59269 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
59271 /* pWal->readLock is usually set, but might be -1 if there was a
59272 ** prior error while attempting to acquire are read-lock. This cannot
59275 ** upgrade to exclusive-mode following such an error.
59277 assert( pWal->readLock>=0 || pWal->lockError );
59278 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
59281 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
59282 pWal->exclusiveMode = WAL_NORMAL_MODE;
59283 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
59284 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
59286 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
59292 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
59293 assert( pWal->readLock>=0 );
59294 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
59295 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
59298 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
59304 ** Return true if the argument is non-NULL and the WAL module is using
59305 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
59306 ** WAL module is using shared-memory, return false.
59309 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
59322 assert( pWal->readLock>=0 && pWal->writeLock==0 );
59324 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
59332 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
59339 /* Try to open on pSnapshot when the next read-transaction starts
59342 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
59346 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
59355 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
59356 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
59357 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
59358 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
59366 ** read-lock. This function returns the database page-size if it is known,
59370 assert( pWal==0 || pWal->readLock>=0 );
59371 return (pWal ? pWal->szPage : 0);
59378 return pWal->pWalFd;
59415 ** This file implements an external (disk-based) database using BTrees.
59419 ** "Sorting And Searching", pages 473-480. Addison-Wesley
59425 ** ----------------------------------------------------------------
59426 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
59427 ** ----------------------------------------------------------------
59432 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
59454 ** page, or a pointer-map page.
59473 ** 40 60 15 4-byte meta values passed to higher layers
59478 ** 52 4 Largest root-page (auto/incr_vacuum)
59479 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
59482 ** 68 4 Application-ID
59484 ** 92 4 The version-valid-for number
59487 ** All of the integer values are big-endian (most significant byte first).
59495 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
59510 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
59513 ** |----------------|
59515 ** |----------------|
59517 ** |----------------|
59521 ** |----------------|
59524 ** |----------------| ^ Grows upwards
59527 ** |----------------|
59546 ** The cell pointer array contains zero or more 2-byte numbers which are
59577 ** appears first. A variable-length integer may not be more than 9 bytes long.
59579 ** allows a 64-bit integer to be encoded in 9 bytes.
59602 ** filled with data (pagesize - 4 bytes). The last page can have as little
59625 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
59632 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
59643 ** You can change this value at compile-time by specifying a
59644 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
59645 ** header must be exactly 16 bytes including the zero-terminator so
59657 ** first byte of on-disk image of every BTree page.
59667 ** is derived from the raw on-disk page content.
59674 ** stored in MemPage.pBt->mutex.
59679 u8 intKey; /* True if table b-trees. False for index b-trees */
59695 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
59696 ** non-overflow cell */
59756 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
59767 ** If the shared-data extension is enabled, there may be multiple users
59794 ** If a BtShared client fails to obtain a write-lock on a database
59795 ** table (because there exists one or more read-locks on the table),
59796 ** the shared-cache enters 'pending-lock' state and isPending is
59799 ** The shared-cache leaves the 'pending lock' state when either of
59805 ** while in the 'pending-lock' state, no connection may start a new
59808 ** This feature is included to help prevent writer-starvation.
59817 u8 autoVacuum; /* True if auto-vacuum is enabled */
59818 u8 incrVacuum; /* True if incr-vacuum is enabled */
59822 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
59827 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
59828 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
59837 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
59838 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
59842 BtLock *pLock; /* List of locks held on this shared-btree struct */
59857 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
59859 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
59871 u16 nSize; /* Size of the cell content on the main b-tree page */
59875 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
59878 ** root-node and 3 for all other internal nodes.
59887 ** b-tree within a database file.
59897 ** found at self->pBt->mutex.
59900 ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op.
59901 ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op.
59924 u8 curIntKey; /* Value of apPage[0]->intKey */
59926 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
59929 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
59954 ** Cursor is valid except that the Cursor.skipNext field is non-zero
59956 ** operation should be a no-op.
59984 ** These macros define the location of the pointer-map entry for a
59989 ** PTRMAP_PAGENO returns the database page number of the pointer-map
59993 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
59995 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
59999 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
60016 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
60019 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
60027 ** overflow pages. The page-number identifies the previous
60030 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
60043 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
60044 assert( p->pBt->inTransaction>=p->inTrans );
60049 ** if the database supports auto-vacuum or not. Because it is used
60055 #define ISAUTOVACUUM (pBt->autoVacuum)
60066 ** the database. As the integrity-check proceeds, for each page used in
60067 ** the database the corresponding bit is set. This allows integrity-check to
60074 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
60083 u32 *heap; /* Min-heap used for analyzing cell coverage */
60087 ** Routines to read or write a two- and four-byte big-endian integer values.
60096 ** two-byte aligned address. get2bytea() is only used for accessing the
60115 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
60117 ** p->locked boolean to true.
60120 assert( p->locked==0 );
60121 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
60122 assert( sqlite3_mutex_held(p->db->mutex) );
60124 sqlite3_mutex_enter(p->pBt->mutex);
60125 p->pBt->db = p->db;
60126 p->locked = 1;
60130 ** Release the BtShared mutex associated with B-Tree handle p and
60131 ** clear the p->locked boolean.
60134 BtShared *pBt = p->pBt;
60135 assert( p->locked==1 );
60136 assert( sqlite3_mutex_held(pBt->mutex) );
60137 assert( sqlite3_mutex_held(p->db->mutex) );
60138 assert( p->db==pBt->db );
60140 sqlite3_mutex_leave(pBt->mutex);
60141 p->locked = 0;
60151 ** and this routine is a no-op. The underlying mutex is non-recursive.
60156 ** by all database connections. The p->pNext is a list of other
60159 ** p, then first unlock all of the others on p->pNext, then wait
60168 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
60169 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
60170 assert( p->pNext==0 || p->pNext->db==p->db );
60171 assert( p->pPrev==0 || p->pPrev->db==p->db );
60172 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
60175 assert( !p->locked || p->wantToLock>0 );
60176 assert( p->sharable || p->wantToLock==0 );
60179 assert( sqlite3_mutex_held(p->db->mutex) );
60183 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
60185 if( !p->sharable ) return;
60186 p->wantToLock++;
60187 if( p->locked ) return;
60204 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
60205 p->pBt->db = p->db;
60206 p->locked = 1;
60210 /* To avoid deadlock, first release all locks with a larger
60215 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
60216 assert( pLater->sharable );
60217 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
60218 assert( !pLater->locked || pLater->wantToLock>0 );
60219 if( pLater->locked ){
60224 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
60225 if( pLater->wantToLock ){
60236 assert( sqlite3_mutex_held(p->db->mutex) );
60237 if( p->sharable ){
60238 assert( p->wantToLock>0 );
60239 p->wantToLock--;
60240 if( p->wantToLock==0 ){
60249 ** B-Tree is not marked as sharable.
60254 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
60255 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
60256 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
60257 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
60259 return (p->sharable==0 || p->locked);
60271 ** There is a corresponding leave-all procedures.
60274 ** to avoid the possibility of deadlock when two threads with
60282 assert( sqlite3_mutex_held(db->mutex) );
60283 for(i=0; i<db->nDb; i++){
60284 p = db->aDb[i].pBt;
60285 if( p && p->sharable ){
60290 db->skipBtreeMutex = skipOk;
60293 if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
60298 assert( sqlite3_mutex_held(db->mutex) );
60299 for(i=0; i<db->nDb; i++){
60300 p = db->aDb[i].pBt;
60305 if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
60317 if( !sqlite3_mutex_held(db->mutex) ){
60320 for(i=0; i<db->nDb; i++){
60322 p = db->aDb[i].pBt;
60323 if( p && p->sharable &&
60324 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
60335 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
60339 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
60348 assert( iDb>=0 && iDb<db->nDb );
60349 if( !sqlite3_mutex_held(db->mutex) ) return 0;
60351 p = db->aDb[iDb].pBt;
60353 return p->sharable==0 || p->locked==1;
60361 ** these two routines, all mutex operations are no-ops in that case and
60365 ** the ones below, are no-ops and are null #defines in btree.h.
60369 p->pBt->db = p->db;
60373 for(i=0; i<db->nDb; i++){
60374 Btree *p = db->aDb[i].pBt;
60376 p->pBt->db = p->db;
60391 sqlite3BtreeEnter(pCur->pBtree);
60395 sqlite3BtreeLeave(pCur->pBtree);
60415 ** This file implements an external (disk-based) database using BTrees.
60439 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
60444 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
60447 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
60460 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
60505 ** shared-cache table level locks. If the library is compiled with the
60506 ** shared-cache feature disabled, then there is only ever one user
60508 ** So define the lock related functions as no-ops.
60524 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
60532 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
60543 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
60555 ** For example, when writing to a table with root-page iRoot via
60568 ** hold a write-lock on the schema table (root page 1). This is also
60573 Pgno iRoot, /* Root page of b-tree */
60574 int isIndex, /* True if iRoot is the root of an index b-tree */
60577 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
60582 ** and has the read-uncommitted flag set, then no lock is required.
60585 if( (pBtree->sharable==0)
60586 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
60593 ** the correct locks are held. So do not bother - just return true.
60596 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
60600 /* Figure out the root-page that the lock should be held on. For table
60601 ** b-trees, this is just the root page of the b-tree being read or
60602 ** written. For index b-trees, it is the root page of the associated
60606 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
60608 if( pIdx->tnum==(int)iRoot ){
60615 iTab = pIdx->pTable->tnum;
60622 /* Search for the required lock. Either a write-lock on root-page iTab, a
60623 ** write-lock on the schema table, or (if the client is reading) a
60624 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
60625 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
60626 if( pLock->pBtree==pBtree
60627 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
60628 && pLock->eLock>=eLockType
60650 ** read-uncommitted flag set, then it is OK for the other object to
60660 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
60661 if( p->pgnoRoot==iRoot
60662 && p->pBtree!=pBtree
60663 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
60674 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
60679 BtShared *pBt = p->pBt;
60684 assert( p->db!=0 );
60685 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
60687 /* If requesting a write-lock, then the Btree must have an open write
60691 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
60692 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
60694 /* This routine is a no-op if the shared-cache is not enabled */
60695 if( !p->sharable ){
60702 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
60703 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
60707 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
60708 /* The condition (pIter->eLock!=eLock) in the following if(...)
60711 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
60717 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
60718 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
60719 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
60720 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
60722 assert( p==pBt->pWriter );
60723 pBt->btsFlags |= BTS_PENDING;
60734 ** Add a lock on the table with root-page iTable to the shared-btree used
60751 BtShared *pBt = p->pBt;
60757 assert( p->db!=0 );
60759 /* A connection with the read-uncommitted flag set will never try to
60760 ** obtain a read-lock using this function. The only read-lock obtained
60761 ** by a connection in read-uncommitted mode is on the sqlite_master
60763 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
60765 /* This function should only be called on a sharable b-tree after it
60766 ** has been determined that no other b-tree holds a conflicting lock. */
60767 assert( p->sharable );
60771 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
60772 if( pIter->iTable==iTable && pIter->pBtree==p ){
60786 pLock->iTable = iTable;
60787 pLock->pBtree = p;
60788 pLock->pNext = pBt->pLock;
60789 pBt->pLock = pLock;
60793 ** and the requested lock. This means if a write-lock was already held
60794 ** and a read-lock requested, we don't incorrectly downgrade the lock.
60797 if( eLock>pLock->eLock ){
60798 pLock->eLock = eLock;
60815 BtShared *pBt = p->pBt;
60816 BtLock **ppIter = &pBt->pLock;
60819 assert( p->sharable || 0==*ppIter );
60820 assert( p->inTrans>0 );
60824 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
60825 assert( pLock->pBtree->inTrans>=pLock->eLock );
60826 if( pLock->pBtree==p ){
60827 *ppIter = pLock->pNext;
60828 assert( pLock->iTable!=1 || pLock==&p->lock );
60829 if( pLock->iTable!=1 ){
60833 ppIter = &pLock->pNext;
60837 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
60838 if( pBt->pWriter==p ){
60839 pBt->pWriter = 0;
60840 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
60841 }else if( pBt->nTransaction==2 ){
60851 pBt->btsFlags &= ~BTS_PENDING;
60856 ** This function changes all write-locks held by Btree p into read-locks.
60859 BtShared *pBt = p->pBt;
60860 if( pBt->pWriter==p ){
60862 pBt->pWriter = 0;
60863 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
60864 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
60865 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
60866 pLock->eLock = READ_LOCK;
60884 return sqlite3_mutex_held(p->pBt->mutex);
60888 ** database connetion. This is important in shared-cache mode. If the database
60889 ** connection pointers get out-of-sync, it is possible for routines like
60892 ** statements only and for the purpose of double-checking that the btree code
60893 ** does keep the database connection pointers up-to-date.
60897 return (p->pBtree->db==p->pBt->db);
60905 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
60908 ** Invalidate the overflow page-list cache for all cursors opened
60913 assert( sqlite3_mutex_held(pBt->mutex) );
60914 for(p=pBt->pCursor; p; p=p->pNext){
60927 ** cursors open on any row within the table with root-page pgnoRoot.
60940 if( pBtree->hasIncrblobCur==0 ) return;
60942 pBtree->hasIncrblobCur = 0;
60943 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
60944 if( (p->curFlags & BTCF_Incrblob)!=0 ){
60945 pBtree->hasIncrblobCur = 1;
60946 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
60947 p->eState = CURSOR_INVALID;
60960 ** when a page that previously contained data becomes a free-list leaf
60965 ** free-list leaf pages:
60968 ** a free-list leaf page, the page is not written to the database
60969 ** (as free-list leaf pages contain no meaningful data). Sometimes
60973 ** 2) When a free-list leaf page is reused, its content is not read
60979 ** a page is moved to the free-list and then reused within the same
60981 ** it is moved to the free-list and it is also not journalled when it
60982 ** is extracted from the free-list and reused, then the original data
60987 ** moved to become a free-list leaf page, the corresponding bit is
60988 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
60995 if( !pBt->pHasContent ){
60996 assert( pgno<=pBt->nPage );
60997 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
60998 if( !pBt->pHasContent ){
61002 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
61003 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
61011 ** This function is called when a free-list leaf page is removed from the
61012 ** free-list for reuse. It returns false if it is safe to retrieve the
61013 ** page from the pager layer with the 'no-content' flag set. True otherwise.
61016 Bitvec *p = pBt->pHasContent;
61022 ** invoked at the conclusion of each write-transaction.
61025 sqlite3BitvecDestroy(pBt->pHasContent);
61026 pBt->pHasContent = 0;
61034 if( pCur->iPage>=0 ){
61035 for(i=0; i<pCur->iPage; i++){
61036 releasePageNotNull(pCur->apPage[i]);
61038 releasePageNotNull(pCur->pPage);
61039 pCur->iPage = -1;
61046 ** function saves the current cursor key in variables pCur->nKey and
61047 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
61051 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
61052 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
61053 ** set to point to a malloced buffer pCur->nKey bytes in size containing
61058 assert( CURSOR_VALID==pCur->eState );
61059 assert( 0==pCur->pKey );
61062 if( pCur->curIntKey ){
61064 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
61068 pCur->nKey = sqlite3BtreePayloadSize(pCur);
61069 pKey = sqlite3Malloc( pCur->nKey );
61071 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
61073 pCur->pKey = pKey;
61081 assert( !pCur->curIntKey || !pCur->pKey );
61095 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
61096 assert( 0==pCur->pKey );
61099 if( pCur->eState==CURSOR_SKIPNEXT ){
61100 pCur->eState = CURSOR_VALID;
61102 pCur->skipNext = 0;
61108 pCur->eState = CURSOR_REQUIRESEEK;
61111 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
61120 ** the table with root-page iRoot. "Saving the cursor position" means that
61131 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
61141 assert( sqlite3_mutex_held(pBt->mutex) );
61142 assert( pExcept==0 || pExcept->pBt==pBt );
61143 for(p=pBt->pCursor; p; p=p->pNext){
61144 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
61147 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
61162 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
61163 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
61169 testcase( p->iPage>=0 );
61173 p = p->pNext;
61183 sqlite3_free(pCur->pKey);
61184 pCur->pKey = 0;
61185 pCur->eState = CURSOR_INVALID;
61205 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
61207 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
61208 if( pIdxKey->nField==0 ){
61218 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
61234 assert( pCur->eState>=CURSOR_REQUIRESEEK );
61235 if( pCur->eState==CURSOR_FAULT ){
61236 return pCur->skipNext;
61238 pCur->eState = CURSOR_INVALID;
61239 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
61241 sqlite3_free(pCur->pKey);
61242 pCur->pKey = 0;
61243 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
61244 pCur->skipNext |= skipNext;
61245 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
61246 pCur->eState = CURSOR_SKIPNEXT;
61253 (p->eState>=CURSOR_REQUIRESEEK ? \
61270 return pCur->eState!=CURSOR_VALID;
61301 assert( pCur->eState!=CURSOR_VALID );
61307 if( pCur->eState!=CURSOR_VALID ){
61310 assert( pCur->skipNext==0 );
61332 pCur->hints = x;
61339 ** number for the pointer-map page that contains the entry for the
61349 assert( sqlite3_mutex_held(pBt->mutex) );
61351 nPagesPerMapPage = (pBt->usableSize/5)+1;
61352 iPtrMap = (pgno-2)/nPagesPerMapPage;
61366 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
61367 ** a no-op. If an error occurs, the appropriate error code is written
61379 assert( sqlite3_mutex_held(pBt->mutex) );
61380 /* The master-journal page number must never be used as a pointer map page */
61383 assert( pBt->autoVacuum );
61389 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
61399 assert( offset <= (int)pBt->usableSize-5 );
61403 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
61429 assert( sqlite3_mutex_held(pBt->mutex) );
61432 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
61443 assert( offset <= (int)pBt->usableSize-5 );
61465 ** 4-byte child pointer found on interior pages, if there is one.
61470 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
61472 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
61478 ** on a single B-tree page. Make necessary adjustments to the CellInfo
61499 minLocal = pPage->minLocal;
61500 maxLocal = pPage->maxLocal;
61501 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
61505 pInfo->nLocal = (u16)surplus;
61507 pInfo->nLocal = (u16)minLocal;
61509 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
61531 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61532 assert( pPage->leaf==0 );
61533 assert( pPage->childPtrSize==4 );
61537 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
61538 pInfo->nPayload = 0;
61539 pInfo->nLocal = 0;
61540 pInfo->pPayload = 0;
61552 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61553 assert( pPage->leaf==0 || pPage->leaf==1 );
61554 assert( pPage->intKeyLeaf );
61555 assert( pPage->childPtrSize==0 );
61576 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
61595 pInfo->nKey = *(i64*)&iKey;
61596 pInfo->nPayload = nPayload;
61597 pInfo->pPayload = pIter;
61598 testcase( nPayload==pPage->maxLocal );
61599 testcase( nPayload==pPage->maxLocal+1 );
61600 if( nPayload<=pPage->maxLocal ){
61604 pInfo->nSize = nPayload + (u16)(pIter - pCell);
61605 if( pInfo->nSize<4 ) pInfo->nSize = 4;
61606 pInfo->nLocal = (u16)nPayload;
61619 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61620 assert( pPage->leaf==0 || pPage->leaf==1 );
61621 assert( pPage->intKeyLeaf==0 );
61622 pIter = pCell + pPage->childPtrSize;
61632 pInfo->nKey = nPayload;
61633 pInfo->nPayload = nPayload;
61634 pInfo->pPayload = pIter;
61635 testcase( nPayload==pPage->maxLocal );
61636 testcase( nPayload==pPage->maxLocal+1 );
61637 if( nPayload<=pPage->maxLocal ){
61641 pInfo->nSize = nPayload + (u16)(pIter - pCell);
61642 if( pInfo->nSize<4 ) pInfo->nSize = 4;
61643 pInfo->nLocal = (u16)nPayload;
61653 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
61661 ** data area of the btree-page. The return number includes the cell
61669 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
61679 pPage->xParseCell(pPage, pCell, &debuginfo);
61691 if( pPage->intKey ){
61692 /* pIter now points at the 64-bit integer key value, a variable length
61698 testcase( nSize==pPage->maxLocal );
61699 testcase( nSize==pPage->maxLocal+1 );
61700 if( nSize<=pPage->maxLocal ){
61701 nSize += (u32)(pIter - pCell);
61704 int minLocal = pPage->minLocal;
61705 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
61706 testcase( nSize==pPage->maxLocal );
61707 testcase( nSize==pPage->maxLocal+1 );
61708 if( nSize>pPage->maxLocal ){
61711 nSize += 4 + (u16)(pIter - pCell);
61726 pPage->xParseCell(pPage, pCell, &debuginfo);
61731 assert( pPage->childPtrSize==4 );
61734 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
61735 return (u16)(pIter - pCell);
61743 return pPage->xCellSize(pPage, findCell(pPage, iCell));
61750 ** to an overflow page, insert an entry into the pointer-map
61757 pPage->xParseCell(pPage, pCell, &info);
61759 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
61760 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
61768 ** page so that there are no free-blocks on the free-block list.
61773 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
61774 ** b-tree page so that there are no freeblocks or fragment bytes, all
61780 int pc; /* Address of the i-th cell */
61793 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61794 assert( pPage->pBt!=0 );
61795 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
61796 assert( pPage->nOverflow==0 );
61797 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61799 src = data = pPage->aData;
61800 hdr = pPage->hdrOffset;
61801 cellOffset = pPage->cellOffset;
61802 nCell = pPage->nCell;
61805 usableSize = pPage->pBt->usableSize;
61810 ** offsets to each pointer in the cell-pointer array than it is to
61839 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
61840 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
61844 assert( cbrk+(iFree-top) <= usableSize );
61845 memmove(&data[cbrk], &data[top], iFree-top);
61857 iCellLast = usableSize - 4;
61859 u8 *pAddr; /* The i-th cell pointer */
61871 size = pPage->xCellSize(pPage, &src[pc]);
61872 cbrk -= size;
61883 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
61885 memcpy(&temp[x], &data[x], (cbrk+size) - x);
61893 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
61900 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
61901 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61906 ** Search the free-list on page pPg for space to store a cell nByte bytes in
61908 ** from the free-list.
61910 ** If no suitable space can be found on the free-list, return NULL.
61920 const int hdr = pPg->hdrOffset;
61921 u8 * const aData = pPg->aData;
61925 int usableSize = pPg->pBt->usableSize;
61929 while( pc<=usableSize-4 ){
61930 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
61931 ** freeblock form a big-endian integer which is the size of the freeblock
61932 ** in bytes, including the 4-byte header. */
61934 if( (x = size - nByte)>=0 ){
61941 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
61945 /* Remove the slot from the free-list. Update the number of
61950 /* The slot remains on the free-list. Reduce its size to account
61968 ** Allocate nByte bytes of space from within the B-Tree page passed
61969 ** as the first argument. Write into *pIdx the index into pPage->aData[]
61981 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
61982 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
61987 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61988 assert( pPage->pBt );
61989 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61991 assert( pPage->nFree>=nByte );
61992 assert( pPage->nOverflow==0 );
61993 assert( nByte < (int)(pPage->pBt->usableSize-8) );
61995 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
61996 gap = pPage->cellOffset + 2*pPage->nCell;
61998 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
62001 ** However, that integer is too large to be stored in a 2-byte unsigned
62004 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
62006 if( top==0 && pPage->pBt->usableSize==65536 ){
62023 assert( pSpace>=data && (pSpace - data)<65536 );
62024 *pIdx = (int)(pSpace - data);
62036 assert( pPage->nCell>0 || CORRUPT_DB );
62037 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
62050 top -= nByte;
62052 assert( top+nByte <= (int)pPage->pBt->usableSize );
62058 ** Return a section of the pPage->aData to the freelist.
62059 ** The first byte of the new free block is pPage->aData[iStart]
62078 unsigned char *data = pPage->aData; /* Page content */
62080 assert( pPage->pBt!=0 );
62081 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
62082 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
62083 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
62084 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62086 assert( iStart<=pPage->pBt->usableSize-4 );
62091 hdr = pPage->hdrOffset;
62103 if( iFreeBlk>pPage->pBt->usableSize-4 ){
62115 nFrag = iFreeBlk - iEnd;
62118 if( iEnd > pPage->pBt->usableSize ){
62121 iSize = iEnd - iStart;
62133 nFrag += iStart - iPtrEnd;
62134 iSize = iEnd - iPtr;
62139 data[hdr+7] -= nFrag;
62153 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
62160 pPage->nFree += iOrigSize;
62177 BtShared *pBt; /* A copy of pPage->pBt */
62179 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
62180 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62181 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
62183 pPage->childPtrSize = 4-4*pPage->leaf;
62184 pPage->xCellSize = cellSizePtr;
62185 pBt = pPage->pBt;
62187 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
62188 ** interior table b-tree page. */
62190 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
62191 ** leaf table b-tree page. */
62193 pPage->intKey = 1;
62194 if( pPage->leaf ){
62195 pPage->intKeyLeaf = 1;
62196 pPage->xParseCell = btreeParseCellPtr;
62198 pPage->intKeyLeaf = 0;
62199 pPage->xCellSize = cellSizePtrNoPayload;
62200 pPage->xParseCell = btreeParseCellPtrNoPayload;
62202 pPage->maxLocal = pBt->maxLeaf;
62203 pPage->minLocal = pBt->minLeaf;
62205 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
62206 ** interior index b-tree page. */
62208 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
62209 ** leaf index b-tree page. */
62211 pPage->intKey = 0;
62212 pPage->intKeyLeaf = 0;
62213 pPage->xParseCell = btreeParseCellPtrIndex;
62214 pPage->maxLocal = pBt->maxLocal;
62215 pPage->minLocal = pBt->minLocal;
62217 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
62221 pPage->max1bytePayload = pBt->max1bytePayload;
62229 ** not contain a well-formed database page, then return
62231 ** guarantee that the page is well-formed. It only shows that
62235 int pc; /* Address of a freeblock within pPage->aData[] */
62237 u8 *data; /* Equal to pPage->aData */
62246 assert( pPage->pBt!=0 );
62247 assert( pPage->pBt->db!=0 );
62248 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62249 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
62250 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
62251 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
62252 assert( pPage->isInit==0 );
62254 pBt = pPage->pBt;
62255 hdr = pPage->hdrOffset;
62256 data = pPage->aData;
62257 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
62258 ** the b-tree page type. */
62262 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
62263 pPage->maskPage = (u16)(pBt->pageSize - 1);
62264 pPage->nOverflow = 0;
62265 usableSize = pBt->usableSize;
62266 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
62267 pPage->aDataEnd = &data[usableSize];
62268 pPage->aCellIdx = &data[cellOffset];
62269 pPage->aDataOfst = &data[pPage->childPtrSize];
62270 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
62274 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
62276 pPage->nCell = get2byte(&data[hdr+3]);
62277 if( pPage->nCell>MX_CELL(pBt) ){
62281 testcase( pPage->nCell==MX_CELL(pBt) );
62282 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
62286 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
62295 iCellFirst = cellOffset + 2*pPage->nCell;
62296 iCellLast = usableSize - 4;
62297 if( pBt->db->flags & SQLITE_CellSizeCk ){
62301 if( !pPage->leaf ) iCellLast--;
62302 for(i=0; i<pPage->nCell; i++){
62309 sz = pPage->xCellSize(pPage, &data[pc]);
62315 if( !pPage->leaf ) iCellLast++;
62319 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
62323 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
62327 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
62354 ** of the cell-content area plus the number of free bytes within
62355 ** the cell-content area. If this is greater than the usable-size
62357 ** serves to verify that the offset to the start of the cell-content
62363 pPage->nFree = (u16)(nFree - iCellFirst);
62364 pPage->isInit = 1;
62373 unsigned char *data = pPage->aData;
62374 BtShared *pBt = pPage->pBt;
62375 u8 hdr = pPage->hdrOffset;
62378 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
62379 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
62380 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
62381 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
62382 assert( sqlite3_mutex_held(pBt->mutex) );
62383 if( pBt->btsFlags & BTS_FAST_SECURE ){
62384 memset(&data[hdr], 0, pBt->usableSize - hdr);
62390 put2byte(&data[hdr+5], pBt->usableSize);
62391 pPage->nFree = (u16)(pBt->usableSize - first);
62393 pPage->cellOffset = first;
62394 pPage->aDataEnd = &data[pBt->usableSize];
62395 pPage->aCellIdx = &data[first];
62396 pPage->aDataOfst = &data[pPage->childPtrSize];
62397 pPage->nOverflow = 0;
62398 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
62399 pPage->maskPage = (u16)(pBt->pageSize - 1);
62400 pPage->nCell = 0;
62401 pPage->isInit = 1;
62411 if( pgno!=pPage->pgno ){
62412 pPage->aData = sqlite3PagerGetData(pDbPage);
62413 pPage->pDbPage = pDbPage;
62414 pPage->pBt = pBt;
62415 pPage->pgno = pgno;
62416 pPage->hdrOffset = pgno==1 ? 100 : 0;
62418 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
62443 assert( sqlite3_mutex_held(pBt->mutex) );
62444 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
62457 assert( sqlite3_mutex_held(pBt->mutex) );
62458 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
62467 ** error, return ((unsigned int)-1).
62470 return pBt->nPage;
62474 assert( ((p->pBt->nPage)&0x80000000)==0 );
62475 return btreePagecount(p->pBt);
62483 ** And if the fetch fails, this routine must decrement pCur->iPage.
62485 ** The page is fetched as read-write unless pCur is not NULL and is
62486 ** a read-only cursor.
62496 int bReadOnly /* True for a read-only page */
62500 assert( sqlite3_mutex_held(pBt->mutex) );
62501 assert( pCur==0 || ppPage==&pCur->pPage );
62502 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
62503 assert( pCur==0 || pCur->iPage>0 );
62509 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
62514 if( (*ppPage)->isInit==0 ){
62522 assert( (*ppPage)->pgno==pgno );
62523 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
62527 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
62536 pCur->iPage--;
62537 pCur->pPage = pCur->apPage[pCur->iPage];
62551 assert( pPage->aData );
62552 assert( pPage->pBt );
62553 assert( pPage->pDbPage!=0 );
62554 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
62555 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
62556 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62557 sqlite3PagerUnrefNotNull(pPage->pDbPage);
62564 assert( pPage->aData );
62565 assert( pPage->pBt );
62566 assert( pPage->pDbPage!=0 );
62567 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
62568 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
62569 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62570 sqlite3PagerUnrefPageOne(pPage->pDbPage);
62590 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
62595 (*ppPage)->isInit = 0;
62615 if( pPage->isInit ){
62616 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62617 pPage->isInit = 0;
62624 ** the call for every page that comes in for re-initing. */
62635 assert( pBt->db );
62636 assert( sqlite3_mutex_held(pBt->db->mutex) );
62637 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
62645 ** be exclusively in memory, or it might use a disk-based memory cache.
62649 ** If zFilename is ":memory:" then an in-memory database is created
62662 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
62679 /* Set the variable isMemdb to true for an in-memory database, or
62680 ** false for a file-based database.
62692 assert( sqlite3_mutex_held(db->mutex) );
62711 p->inTrans = TRANS_NONE;
62712 p->db = db;
62714 p->lock.pBtree = p;
62715 p->lock.iTable = 1;
62726 int nFullPathname = pVfs->mxPathname+1;
62730 p->sharable = 1;
62752 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
62753 assert( pBt->nRef>0 );
62754 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
62755 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
62757 for(iDb=db->nDb-1; iDb>=0; iDb--){
62758 Btree *pExisting = db->aDb[iDb].pBt;
62759 if( pExisting && pExisting->pBt==pBt ){
62767 p->pBt = pBt;
62768 pBt->nRef++;
62782 p->sharable = 1;
62804 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
62807 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
62808 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
62813 pBt->openFlags = (u8)flags;
62814 pBt->db = db;
62815 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
62816 p->pBt = pBt;
62818 pBt->pCursor = 0;
62819 pBt->pPage1 = 0;
62820 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
62822 pBt->btsFlags |= BTS_SECURE_DELETE;
62824 pBt->btsFlags |= BTS_OVERWRITE;
62826 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
62827 ** determined by the 2-byte integer located at an offset of 16 bytes from
62829 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
62830 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
62831 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
62832 pBt->pageSize = 0;
62834 /* If the magic name ":memory:" will create an in-memory database, then
62835 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
62838 ** regular file-name. In this case the auto-vacuum applies as per normal.
62841 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
62842 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
62847 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
62848 ** determined by the one-byte unsigned integer found at an offset of 20
62851 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
62853 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
62854 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
62857 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
62859 pBt->usableSize = pBt->pageSize - nReserve;
62860 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
62865 pBt->nRef = 1;
62866 if( p->sharable ){
62870 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
62871 if( pBt->mutex==0 ){
62877 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
62889 if( p->sharable ){
62892 for(i=0; i<db->nDb; i++){
62893 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
62894 while( pSib->pPrev ){ pSib = pSib->pPrev; }
62895 if( (uptr)p->pBt<(uptr)pSib->pBt ){
62896 p->pNext = pSib;
62897 p->pPrev = 0;
62898 pSib->pPrev = p;
62900 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
62901 pSib = pSib->pNext;
62903 p->pNext = pSib->pNext;
62904 p->pPrev = pSib;
62905 if( p->pNext ){
62906 p->pNext->pPrev = p;
62908 pSib->pNext = p;
62919 if( pBt && pBt->pPager ){
62920 sqlite3PagerClose(pBt->pPager, 0);
62928 /* If the B-Tree was successfully opened, set the pager-cache size to the
62929 ** default value. Except, when opening on an existing shared pager-cache,
62930 ** do not change the pager-cache size.
62933 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
62936 pFile = sqlite3PagerFile(pBt->pPager);
62937 if( pFile->pMethods ){
62938 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
62961 assert( sqlite3_mutex_notheld(pBt->mutex) );
62964 pBt->nRef--;
62965 if( pBt->nRef<=0 ){
62967 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
62970 while( ALWAYS(pList) && pList->pNext!=pBt ){
62971 pList=pList->pNext;
62974 pList->pNext = pBt->pNext;
62978 sqlite3_mutex_free(pBt->mutex);
62990 ** Make sure pBt->pTmpSpace points to an allocation of
62991 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
62995 if( !pBt->pTmpSpace ){
62996 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
62998 /* One of the uses of pBt->pTmpSpace is to format cells before
63011 ** left-child pointer to the beginning of a cell.
63013 if( pBt->pTmpSpace ){
63014 memset(pBt->pTmpSpace, 0, 8);
63015 pBt->pTmpSpace += 4;
63021 ** Free the pBt->pTmpSpace allocation
63024 if( pBt->pTmpSpace ){
63025 pBt->pTmpSpace -= 4;
63026 sqlite3PageFree(pBt->pTmpSpace);
63027 pBt->pTmpSpace = 0;
63035 BtShared *pBt = p->pBt;
63039 assert( sqlite3_mutex_held(p->db->mutex) );
63041 pCur = pBt->pCursor;
63044 pCur = pCur->pNext;
63045 if( pTmp->pBtree==p ){
63051 ** The call to sqlite3BtreeRollback() drops any table-locks held by
63057 /* If there are still other outstanding references to the shared-btree
63059 ** up the shared-btree.
63061 assert( p->wantToLock==0 && p->locked==0 );
63062 if( !p->sharable || removeFromSharingList(pBt) ){
63068 assert( !pBt->pCursor );
63069 sqlite3PagerClose(pBt->pPager, p->db);
63070 if( pBt->xFreeSchema && pBt->pSchema ){
63071 pBt->xFreeSchema(pBt->pSchema);
63073 sqlite3DbFree(0, pBt->pSchema);
63079 assert( p->wantToLock==0 );
63080 assert( p->locked==0 );
63081 if( p->pPrev ) p->pPrev->pNext = p->pNext;
63082 if( p->pNext ) p->pNext->pPrev = p->pPrev;
63097 BtShared *pBt = p->pBt;
63098 assert( sqlite3_mutex_held(p->db->mutex) );
63100 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
63116 BtShared *pBt = p->pBt;
63118 assert( sqlite3_mutex_held(p->db->mutex) );
63120 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
63131 BtShared *pBt = p->pBt;
63132 assert( sqlite3_mutex_held(p->db->mutex) );
63134 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
63145 ** is a very low but non-zero probability of damage. Level 3 reduces the
63153 BtShared *pBt = p->pBt;
63154 assert( sqlite3_mutex_held(p->db->mutex) );
63156 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
63184 BtShared *pBt = p->pBt;
63185 assert( nReserve>=-1 && nReserve<=255 );
63188 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
63190 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
63195 nReserve = pBt->pageSize - pBt->usableSize;
63199 ((pageSize-1)&pageSize)==0 ){
63201 assert( !pBt->pCursor );
63202 pBt->pageSize = (u32)pageSize;
63205 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
63206 pBt->usableSize = pBt->pageSize - (u16)nReserve;
63207 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
63216 return p->pBt->pageSize;
63221 ** may only be called if it is guaranteed that the b-tree mutex is already
63225 ** known that the shared b-tree mutex is held, but the mutex on the
63232 assert( sqlite3_mutex_held(p->pBt->mutex) );
63233 n = p->pBt->pageSize - p->pBt->usableSize;
63251 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
63266 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
63277 ** newFlag==(-1) No changes
63282 ** freelist leaf pages are not written back to the database. Thus in-page
63296 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
63297 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
63299 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
63305 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
63306 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
63307 ** is disabled. The default value for the auto-vacuum property is
63314 BtShared *pBt = p->pBt;
63319 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
63322 pBt->autoVacuum = av ?1:0;
63323 pBt->incrVacuum = av==2 ?1:0;
63331 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
63341 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
63342 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
63351 ** If the user has not set the safety-level for this database connection
63352 ** using "PRAGMA synchronous", and if the safety-level is not already
63361 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
63362 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
63363 if( pDb->bSyncSet==0
63364 && pDb->safety_level!=safety_level
63365 && pDb!=&db->aDb[1]
63367 pDb->safety_level = safety_level;
63368 sqlite3PagerSetFlags(pBt->pPager,
63369 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
63382 ** well-formed database file, then SQLITE_CORRUPT is returned.
63393 assert( sqlite3_mutex_held(pBt->mutex) );
63394 assert( pBt->pPage1==0 );
63395 rc = sqlite3PagerSharedLock(pBt->pPager);
63403 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
63404 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
63405 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
63411 u8 *page1 = pPage1->aData;
63413 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
63422 pBt->btsFlags |= BTS_READ_ONLY;
63429 pBt->btsFlags |= BTS_READ_ONLY;
63440 ** may not be the latest version - there may be a newer one in the log
63443 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
63445 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
63461 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
63470 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
63471 ** determined by the 2-byte integer located at an offset of 16 bytes from
63474 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
63476 if( ((pageSize-1)&pageSize)!=0
63483 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
63487 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
63488 ** determined by the one-byte unsigned integer found at an offset of 20
63490 usableSize = pageSize - page1[20];
63491 if( (u32)pageSize!=pBt->pageSize ){
63493 ** of BtShared.pageSize, we have discovered that the page-size is
63494 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
63496 ** again with the correct page-size.
63499 pBt->usableSize = usableSize;
63500 pBt->pageSize = pageSize;
63502 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
63503 pageSize-usableSize);
63506 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
63510 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
63516 pBt->pageSize = pageSize;
63517 pBt->usableSize = usableSize;
63519 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
63520 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
63526 ** cells can will fit on one page. We assume a 10-byte page header.
63528 ** 2-byte pointer to the cell
63529 ** 4-byte child pointer
63530 ** 9-byte nKey value
63531 ** 4-byte nData value
63532 ** 4-byte overflow page pointer
63533 ** So a cell consists of a 2-byte pointer, a header which is as much as
63537 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
63538 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
63539 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
63540 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
63541 if( pBt->maxLocal>127 ){
63542 pBt->max1bytePayload = 127;
63544 pBt->max1bytePayload = (u8)pBt->maxLocal;
63546 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
63547 pBt->pPage1 = pPage1;
63548 pBt->nPage = nPage;
63553 pBt->pPage1 = 0;
63573 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
63574 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
63575 && pCur->eState!=CURSOR_FAULT ) r++;
63587 ** If there is a transaction in progress, this routine is a no-op.
63590 assert( sqlite3_mutex_held(pBt->mutex) );
63591 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
63592 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
63593 MemPage *pPage1 = pBt->pPage1;
63594 assert( pPage1->aData );
63595 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
63596 pBt->pPage1 = 0;
63611 assert( sqlite3_mutex_held(pBt->mutex) );
63612 if( pBt->nPage>0 ){
63615 pP1 = pBt->pPage1;
63617 data = pP1->aData;
63618 rc = sqlite3PagerWrite(pP1->pDbPage);
63622 data[16] = (u8)((pBt->pageSize>>8)&0xff);
63623 data[17] = (u8)((pBt->pageSize>>16)&0xff);
63626 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
63627 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
63631 memset(&data[24], 0, 100-24);
63633 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
63635 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
63636 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
63637 put4byte(&data[36 + 4*4], pBt->autoVacuum);
63638 put4byte(&data[36 + 7*4], pBt->incrVacuum);
63640 pBt->nPage = 1;
63653 p->pBt->nPage = 0;
63654 rc = newDatabase(p->pBt);
63660 ** Attempt to start a new transaction. A write-transaction
63661 ** is started if the second argument is nonzero, otherwise a read-
63665 ** upgraded to exclusive by calling this routine a second time - the
63668 ** A write-transaction must be started before attempting any
63682 ** if there is one. But if there was previously a read-lock, do not
63683 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
63684 ** returned when there is already a read-lock in order to avoid a deadlock.
63695 BtShared *pBt = p->pBt;
63701 /* If the btree is already in a write-transaction, or it
63702 ** is already in a read-transaction and a read-transaction
63703 ** is requested, this is a no-op.
63705 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
63708 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
63710 /* Write transactions are not possible on a read-only database */
63711 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
63720 ** on this shared-btree structure and a second write transaction is
63723 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
63724 || (pBt->btsFlags & BTS_PENDING)!=0
63726 pBlock = pBt->pWriter->db;
63729 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63730 if( pIter->pBtree!=p ){
63731 pBlock = pIter->pBtree->db;
63737 sqlite3ConnectionBlocked(p->db, pBlock);
63744 /* Any read-only or read-write transaction implies a read-lock on
63745 ** page 1. So if some other shared-cache client already has a write-lock
63750 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
63751 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
63753 /* Call lockBtree() until either pBt->pPage1 is populated or
63755 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
63756 ** reading page 1 it discovers that the page-size of the database
63757 ** file is not pBt->pageSize. In this case lockBtree() will update
63758 ** pBt->pageSize to the page-size of the file on disk.
63760 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
63763 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
63766 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
63776 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
63780 if( p->inTrans==TRANS_NONE ){
63781 pBt->nTransaction++;
63783 if( p->sharable ){
63784 assert( p->lock.pBtree==p && p->lock.iTable==1 );
63785 p->lock.eLock = READ_LOCK;
63786 p->lock.pNext = pBt->pLock;
63787 pBt->pLock = &p->lock;
63791 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
63792 if( p->inTrans>pBt->inTransaction ){
63793 pBt->inTransaction = p->inTrans;
63796 MemPage *pPage1 = pBt->pPage1;
63798 assert( !pBt->pWriter );
63799 pBt->pWriter = p;
63800 pBt->btsFlags &= ~BTS_EXCLUSIVE;
63801 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
63804 /* If the db-size header field is incorrect (as it may be if an old
63807 ** re-read the database size from page 1 if a savepoint or transaction
63810 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
63811 rc = sqlite3PagerWrite(pPage1->pDbPage);
63813 put4byte(&pPage1->aData[28], pBt->nPage);
63824 ** the sub-journal is not already open, then it will be opened here.
63826 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
63837 ** Set the pointer-map entries for all children of page pPage. Also, if
63845 BtShared *pBt = pPage->pBt;
63846 Pgno pgno = pPage->pgno;
63848 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
63849 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
63851 nCell = pPage->nCell;
63858 if( !pPage->leaf ){
63864 if( !pPage->leaf ){
63865 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
63877 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
63880 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
63883 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
63887 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
63888 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
63891 if( get4byte(pPage->aData)!=iFrom ){
63894 put4byte(pPage->aData, iTo);
63900 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
63902 nCell = pPage->nCell;
63908 pPage->xParseCell(pPage, pCell, &info);
63910 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
63913 if( iFrom==get4byte(pCell+info.nSize-4) ){
63914 put4byte(pCell+info.nSize-4, iTo);
63928 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
63931 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
63943 ** the journal needs to be sync()ed before database page pDbPage->pgno
63951 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
63956 Pgno iDbPage = pDbPage->pgno;
63957 Pager *pPager = pBt->pPager;
63962 assert( sqlite3_mutex_held(pBt->mutex) );
63963 assert( pDbPage->pBt==pBt );
63968 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
63972 pDbPage->pgno = iFreePage;
63974 /* If pDbPage was a btree-page, then it may have child pages and/or cells
63988 Pgno nextOvfl = get4byte(pDbPage->aData);
64006 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
64024 ** Perform a single step of an incremental-vacuum. If successful, return
64029 ** More specifically, this function attempts to re-organize the database so
64035 ** If the bCommit parameter is non-zero, this function assumes that the
64037 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
64041 Pgno nFreeList; /* Number of pages still on the free-list */
64044 assert( sqlite3_mutex_held(pBt->mutex) );
64051 nFreeList = get4byte(&pBt->pPage1->aData[36]);
64066 /* Remove the page from the files free-list. This is not required
64067 ** if bCommit is non-zero. In that case, the free-list will be
64095 ** looping until a free-page located within the first nFin pages
64123 iLastPg--;
64125 pBt->bDoTruncate = 1;
64126 pBt->nPage = iLastPg;
64132 ** The database opened by the first argument is an auto-vacuum database
64134 ** size of the database in pages following an auto-vacuum operation.
64141 nEntry = pBt->usableSize/5;
64142 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
64143 nFin = nOrig - nFree - nPtrmap;
64145 nFin--;
64148 nFin--;
64155 ** A write-transaction must be opened before calling this function.
64164 BtShared *pBt = p->pBt;
64167 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
64168 if( !pBt->autoVacuum ){
64172 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
64184 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
64185 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
64197 ** is committed for an auto-vacuum database.
64206 Pager *pPager = pBt->pPager;
64209 assert( sqlite3_mutex_held(pBt->mutex) );
64211 assert(pBt->autoVacuum);
64212 if( !pBt->incrVacuum ){
64221 ** is either a pointer-map page or the pending-byte page. If one
64227 nFree = get4byte(&pBt->pPage1->aData[36]);
64233 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
64237 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
64238 put4byte(&pBt->pPage1->aData[32], 0);
64239 put4byte(&pBt->pPage1->aData[36], 0);
64240 put4byte(&pBt->pPage1->aData[28], nFin);
64241 pBt->bDoTruncate = 1;
64242 pBt->nPage = nFin;
64258 ** This routine does the first phase of a two-phase commit. This routine
64270 ** This call is a no-op if no write-transaction is currently active on pBt.
64281 ** the write-transaction for this database file is to delete the journal.
64285 if( p->inTrans==TRANS_WRITE ){
64286 BtShared *pBt = p->pBt;
64289 if( pBt->autoVacuum ){
64296 if( pBt->bDoTruncate ){
64297 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
64300 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
64311 BtShared *pBt = p->pBt;
64312 sqlite3 *db = p->db;
64316 pBt->bDoTruncate = 0;
64318 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
64320 ** handle, downgrade to a read-only transaction. The other statements
64323 p->inTrans = TRANS_READ;
64329 if( p->inTrans!=TRANS_NONE ){
64331 pBt->nTransaction--;
64332 if( 0==pBt->nTransaction ){
64333 pBt->inTransaction = TRANS_NONE;
64339 p->inTrans = TRANS_NONE;
64349 ** This routine implements the second phase of a 2-phase commit. The
64361 ** is non-zero then this b-tree transaction is part of a multi-file
64365 ** reset the b-tree objects internal state to indicate that the write
64374 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
64378 /* If the handle has a write-transaction open, commit the shared-btrees
64381 if( p->inTrans==TRANS_WRITE ){
64383 BtShared *pBt = p->pBt;
64384 assert( pBt->inTransaction==TRANS_WRITE );
64385 assert( pBt->nTransaction>0 );
64386 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
64391 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
64392 pBt->inTransaction = TRANS_READ;
64426 ** flag is true, then only write-cursors need be tripped - read-only
64430 ** rolled back modified the database schema. In this case b-tree root
64435 ** saving the current position of a read-only cursor, all cursors,
64436 ** including all read-cursors are tripped.
64448 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
64449 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
64450 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
64459 p->eState = CURSOR_FAULT;
64460 p->skipNext = errCode;
64482 BtShared *pBt = p->pBt;
64501 if( p->inTrans==TRANS_WRITE ){
64504 assert( TRANS_WRITE==pBt->inTransaction );
64505 rc2 = sqlite3PagerRollback(pBt->pPager);
64510 /* The rollback may have destroyed the pPage1->aData value. So
64512 ** sure pPage1->aData is set correctly. */
64514 int nPage = get4byte(28+(u8*)pPage1->aData);
64516 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
64517 testcase( pBt->nPage!=nPage );
64518 pBt->nPage = nPage;
64522 pBt->inTransaction = TRANS_READ;
64542 ** A statement sub-transaction is implemented as an anonymous savepoint. The
64544 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
64545 ** are no active savepoints and no other statement-transactions open,
64551 BtShared *pBt = p->pBt;
64553 assert( p->inTrans==TRANS_WRITE );
64554 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
64556 assert( iStatement>p->db->nSavepoint );
64557 assert( pBt->inTransaction==TRANS_WRITE );
64563 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
64575 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
64582 if( p && p->inTrans==TRANS_WRITE ){
64583 BtShared *pBt = p->pBt;
64585 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
64591 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
64594 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
64595 pBt->nPage = 0;
64598 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
64603 assert( pBt->nPage>0 );
64612 ** iTable. If a read-only cursor is requested, it is assumed that
64613 ** the caller already has at least a read-only transaction open
64614 ** on the database already. If a write-cursor is requested, then
64631 ** 3: The database must be writable (not on read-only media)
64642 ** operations on this cursor can be no-ops and all READ operations can
64643 ** return a null row (2-bytes: 0x01 0x00).
64646 ** root page of a b-tree. If it is not, then the cursor acquired
64655 int wrFlag, /* 1 to write. 0 read-only */
64659 BtShared *pBt = p->pBt; /* Shared b-tree handle */
64669 ** b-tree database, the connection is holding the required table locks,
64676 assert( p->inTrans>TRANS_NONE );
64677 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
64678 assert( pBt->pPage1 && pBt->pPage1->aData );
64679 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
64683 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
64692 pCur->pgnoRoot = (Pgno)iTable;
64693 pCur->iPage = -1;
64694 pCur->pKeyInfo = pKeyInfo;
64695 pCur->pBtree = p;
64696 pCur->pBt = pBt;
64697 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
64698 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
64701 for(pX=pBt->pCursor; pX; pX=pX->pNext){
64702 if( pX->pgnoRoot==(Pgno)iTable ){
64703 pX->curFlags |= BTCF_Multiple;
64704 pCur->curFlags |= BTCF_Multiple;
64707 pCur->pNext = pBt->pCursor;
64708 pBt->pCursor = pCur;
64709 pCur->eState = CURSOR_INVALID;
64715 int wrFlag, /* 1 to write. 0 read-only */
64735 ** to users so they cannot do the sizeof() themselves - they must call
64748 ** of run-time by skipping the initialization of those elements.
64759 Btree *pBtree = pCur->pBtree;
64761 BtShared *pBt = pCur->pBt;
64763 assert( pBt->pCursor!=0 );
64764 if( pBt->pCursor==pCur ){
64765 pBt->pCursor = pCur->pNext;
64767 BtCursor *pPrev = pBt->pCursor;
64769 if( pPrev->pNext==pCur ){
64770 pPrev->pNext = pCur->pNext;
64773 pPrev = pPrev->pNext;
64778 sqlite3_free(pCur->aOverflow);
64779 sqlite3_free(pCur->pKey);
64797 btreeParseCell(pCur->pPage, pCur->ix, &info);
64798 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
64804 if( pCur->info.nSize==0 ){
64805 pCur->curFlags |= BTCF_ValidNKey;
64806 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
64815 ** that is currently pointing to a row in a (non-empty) table.
64819 return pCur && pCur->eState==CURSOR_VALID;
64824 return pCur->eState==CURSOR_VALID;
64835 assert( pCur->eState==CURSOR_VALID );
64836 assert( pCur->curIntKey );
64838 return pCur->info.nKey;
64848 assert( pCur->eState==CURSOR_VALID );
64850 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
64851 (i64)(pCur->info.pPayload - pCur->pPage->aData);
64860 ** The caller must guarantee that the cursor is pointing to a non-NULL
64866 assert( pCur->eState==CURSOR_VALID );
64868 return pCur->info.nPayload;
64874 ** linked list of overflow pages. If possible, it uses the auto-vacuum
64875 ** pointer-map data instead of reading the content of page ovfl to do so.
64887 ** the pointer-map was used to obtain the value for *pPgnoNext), then
64900 assert( sqlite3_mutex_held(pBt->mutex) );
64905 ** autovacuum pointer-map pages. Guess that the next page in
64910 if( pBt->autoVacuum ){
64934 next = get4byte(pPage->aData);
64962 int eOp, /* 0 -> copy from page, 1 -> copy to page */
64995 ** the overflow page-list cache array (BtCursor.aOverflow).
64999 ** Once an overflow page-list cache has been allocated, it must be
65001 ** the cursor is moved to a different row. Additionally, in auto-vacuum
65002 ** mode, the following events may invalidate an overflow page-list cache.
65013 int eOp /* zero to read. non-zero to write. */
65018 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
65019 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
65026 assert( pCur->eState==CURSOR_VALID );
65027 assert( pCur->ix<pPage->nCell );
65031 aPayload = pCur->info.pPayload;
65032 assert( offset+amt <= pCur->info.nPayload );
65034 assert( aPayload > pPage->aData );
65035 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
65038 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
65045 if( offset<pCur->info.nLocal ){
65047 if( a+offset>pCur->info.nLocal ){
65048 a = pCur->info.nLocal - offset;
65050 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
65053 amt -= a;
65055 offset -= pCur->info.nLocal;
65060 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
65063 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
65072 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
65073 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
65074 if( nOvfl>pCur->nOvflAlloc ){
65076 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
65081 pCur->nOvflAlloc = nOvfl*2;
65082 pCur->aOverflow = aNew;
65085 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
65086 pCur->curFlags |= BTCF_ValidOvfl;
65088 /* If the overflow page-list cache has been allocated and the
65092 if( pCur->aOverflow[offset/ovflSize] ){
65094 nextPage = pCur->aOverflow[iIdx];
65101 /* If required, populate the overflow page-list cache. */
65102 assert( pCur->aOverflow[iIdx]==0
65103 || pCur->aOverflow[iIdx]==nextPage
65105 pCur->aOverflow[iIdx] = nextPage;
65111 ** page-list cache, if any, then fall back to the getOverflowPage()
65114 assert( pCur->curFlags & BTCF_ValidOvfl );
65115 assert( pCur->pBtree->db==pBt->db );
65116 if( pCur->aOverflow[iIdx+1] ){
65117 nextPage = pCur->aOverflow[iIdx+1];
65121 offset -= ovflSize;
65131 a = ovflSize - offset;
65139 ** 3) there is no open write-transaction, and
65140 ** 4) the database is file-backed, and
65145 ** output buffer, bypassing the page-cache altogether. This speeds
65150 && pBt->inTransaction==TRANS_READ /* (3) */
65151 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
65152 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
65153 && &pBuf[-4]>=pBufStart /* (6) */
65156 u8 *aWrite = &pBuf[-4];
65159 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
65167 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
65178 amt -= a;
65199 ** pCur can be pointing to either a table or an index b-tree.
65201 ** pCur is pointing to an index b-tree then the key section is read.
65213 assert( pCur->eState==CURSOR_VALID );
65214 assert( pCur->iPage>=0 && pCur->pPage );
65215 assert( pCur->ix<pCur->pPage->nCell );
65232 if ( pCur->eState==CURSOR_INVALID ){
65240 if( pCur->eState==CURSOR_VALID ){
65252 ** the key if index btrees (pPage->intKey==0) and is the data for
65253 ** table btrees (pPage->intKey==1). The number of bytes of available
65273 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
65274 assert( pCur->eState==CURSOR_VALID );
65275 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
65277 assert( pCur->ix<pCur->pPage->nCell );
65278 assert( pCur->info.nSize>0 );
65279 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
65280 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
65281 amt = pCur->info.nLocal;
65282 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
65286 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
65289 return (void*)pCur->info.pPayload;
65296 ** b-tree page. Write the number of available bytes into *pAmt.
65316 ** This function returns SQLITE_CORRUPT if the page-header flags field of
65318 ** if an intkey page appears to be the parent of a non-intkey page, or
65319 ** vice-versa).
65322 BtShared *pBt = pCur->pBt;
65325 assert( pCur->eState==CURSOR_VALID );
65326 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
65327 assert( pCur->iPage>=0 );
65328 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
65331 pCur->info.nSize = 0;
65332 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
65333 pCur->aiIdx[pCur->iPage] = pCur->ix;
65334 pCur->apPage[pCur->iPage] = pCur->pPage;
65335 pCur->ix = 0;
65336 pCur->iPage++;
65337 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
65342 ** Page pParent is an internal (non-leaf) tree page. This function
65343 ** asserts that page number iChild is the left-child if the iIdx'th
65345 ** cells in pParent, that page number iChild is the right-child of
65351 assert( iIdx<=pParent->nCell );
65352 if( iIdx==pParent->nCell ){
65353 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
65365 ** pCur->idx is set to the cell index that contains the pointer
65367 ** right-most child page then pCur->idx is set to one more than
65373 assert( pCur->eState==CURSOR_VALID );
65374 assert( pCur->iPage>0 );
65375 assert( pCur->pPage );
65377 pCur->apPage[pCur->iPage-1],
65378 pCur->aiIdx[pCur->iPage-1],
65379 pCur->pPage->pgno
65381 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
65382 pCur->info.nSize = 0;
65383 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
65384 pCur->ix = pCur->aiIdx[pCur->iPage-1];
65385 pLeaf = pCur->pPage;
65386 pCur->pPage = pCur->apPage[--pCur->iPage];
65391 ** Move the cursor to point to the root page of its b-tree structure.
65398 ** If the b-tree structure is empty, the cursor state is set to
65404 ** page-header flags indicate that the [virtual] root-page is the expected
65405 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
65407 ** indicating a table b-tree, or if the caller did specify a KeyInfo
65409 ** b-tree).
65419 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
65420 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
65422 if( pCur->iPage>=0 ){
65423 if( pCur->iPage ){
65424 releasePageNotNull(pCur->pPage);
65425 while( --pCur->iPage ){
65426 releasePageNotNull(pCur->apPage[pCur->iPage]);
65428 pCur->pPage = pCur->apPage[0];
65431 }else if( pCur->pgnoRoot==0 ){
65432 pCur->eState = CURSOR_INVALID;
65435 assert( pCur->iPage==(-1) );
65436 if( pCur->eState>=CURSOR_REQUIRESEEK ){
65437 if( pCur->eState==CURSOR_FAULT ){
65438 assert( pCur->skipNext!=SQLITE_OK );
65439 return pCur->skipNext;
65443 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
65444 0, pCur->curPagerFlags);
65446 pCur->eState = CURSOR_INVALID;
65449 pCur->iPage = 0;
65450 pCur->curIntKey = pCur->pPage->intKey;
65452 pRoot = pCur->pPage;
65453 assert( pRoot->pgno==pCur->pgnoRoot );
65455 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
65456 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
65457 ** NULL, the caller expects a table b-tree. If this is not the case,
65462 ** if pCur->iPage>=0). But this is not so if the database is corrupted
65463 ** in such a way that page pRoot is linked into a second b-tree table
65465 assert( pRoot->intKey==1 || pRoot->intKey==0 );
65466 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
65467 return SQLITE_CORRUPT_PAGE(pCur->pPage);
65471 pCur->ix = 0;
65472 pCur->info.nSize = 0;
65473 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
65475 pRoot = pCur->pPage;
65476 if( pRoot->nCell>0 ){
65477 pCur->eState = CURSOR_VALID;
65478 }else if( !pRoot->leaf ){
65480 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
65481 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
65482 pCur->eState = CURSOR_VALID;
65485 pCur->eState = CURSOR_INVALID;
65492 ** Move the cursor down to the left-most leaf entry beneath the
65495 ** The left-most leaf is the one with the smallest key - the first
65504 assert( pCur->eState==CURSOR_VALID );
65505 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
65506 assert( pCur->ix<pPage->nCell );
65507 pgno = get4byte(findCell(pPage, pCur->ix));
65514 ** Move the cursor down to the right-most leaf entry beneath the
65517 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
65518 ** finds the right-most entry beneath the *page*.
65520 ** The right-most entry is the one with the largest key - the last
65529 assert( pCur->eState==CURSOR_VALID );
65530 while( !(pPage = pCur->pPage)->leaf ){
65531 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
65532 pCur->ix = pPage->nCell;
65536 pCur->ix = pPage->nCell-1;
65537 assert( pCur->info.nSize==0 );
65538 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
65550 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
65553 assert( pCur->pPage->nCell>0 );
65557 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
65572 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
65574 /* If the cursor already points to the last entry, this is a no-op. */
65575 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
65578 ** to the last entry in the b-tree. */
65580 for(ii=0; ii<pCur->iPage; ii++){
65581 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
65583 assert( pCur->ix==pCur->pPage->nCell-1 );
65584 assert( pCur->pPage->leaf );
65591 assert( pCur->eState==CURSOR_VALID );
65595 pCur->curFlags |= BTCF_AtLast;
65597 pCur->curFlags &= ~BTCF_AtLast;
65600 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
65634 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
65648 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
65650 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
65651 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
65656 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
65658 if( pCur->info.nKey==intKey ){
65662 if( pCur->info.nKey<intKey ){
65663 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
65664 *pRes = -1;
65671 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
65676 if( pCur->info.nKey==intKey ){
65690 pIdxKey->errCode = 0;
65691 assert( pIdxKey->default_rc==1
65692 || pIdxKey->default_rc==0
65693 || pIdxKey->default_rc==-1
65702 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
65703 *pRes = -1;
65708 assert( pCur->pPage );
65709 assert( pCur->pPage->isInit );
65710 assert( pCur->eState==CURSOR_VALID );
65711 assert( pCur->pPage->nCell > 0 );
65712 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
65713 assert( pCur->curIntKey || pIdxKey );
65717 MemPage *pPage = pCur->pPage;
65720 /* pPage->nCell must be greater than zero. If this is the root-page
65722 ** not run. If this is not the root-page, then the moveToChild() routine
65724 ** be the right kind (index or table) of b-tree page. Otherwise
65726 assert( pPage->nCell>0 );
65727 assert( pPage->intKey==(pIdxKey==0) );
65729 upr = pPage->nCell-1;
65731 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
65732 pCur->ix = (u16)idx;
65737 if( pPage->intKeyLeaf ){
65739 if( pCell>=pPage->aDataEnd ){
65747 if( lwr>upr ){ c = -1; break; }
65749 upr = idx-1;
65753 pCur->ix = (u16)idx;
65754 if( !pPage->leaf ){
65758 pCur->curFlags |= BTCF_ValidNKey;
65759 pCur->info.nKey = nCellKey;
65760 pCur->info.nSize = 0;
65773 /* The maximum supported page-size is 65536 bytes. This means that
65774 ** the maximum number of record bytes stored on an index B-Tree
65775 ** page is less than 16384 bytes and may be stored as a 2-byte
65778 ** stored entirely within the b-tree page by inspecting the first
65782 if( nCell<=pPage->max1bytePayload ){
65783 /* This branch runs if the record-size field of the cell is a
65785 ** b-tree page. */
65786 testcase( pCell+nCell+1==pPage->aDataEnd );
65789 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
65791 /* The record-size field is a 2 byte varint and the record
65792 ** fits entirely on the main b-tree page. */
65793 testcase( pCell+nCell+2==pPage->aDataEnd );
65806 u8 * const pCellBody = pCell - pPage->childPtrSize;
65807 pPage->xParseCell(pPage, pCellBody, &pCur->info);
65808 nCell = (int)pCur->info.nKey;
65822 pCur->ix = (u16)idx;
65824 pCur->curFlags &= ~BTCF_ValidOvfl;
65833 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
65834 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
65839 upr = idx-1;
65844 pCur->ix = (u16)idx;
65845 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
65853 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
65854 assert( pPage->isInit );
65855 if( pPage->leaf ){
65856 assert( pCur->ix<pCur->pPage->nCell );
65857 pCur->ix = (u16)idx;
65863 if( lwr>=pPage->nCell ){
65864 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
65868 pCur->ix = (u16)lwr;
65873 pCur->info.nSize = 0;
65874 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
65891 return (CURSOR_VALID!=pCur->eState);
65904 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
65909 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
65910 if( NEVER(pCur->pPage->leaf==0) ) return -1;
65912 n = pCur->pPage->nCell;
65913 for(i=0; i<pCur->iPage; i++){
65914 n *= pCur->apPage[i]->nCell;
65945 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
65946 if( pCur->eState!=CURSOR_VALID ){
65947 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
65952 if( CURSOR_INVALID==pCur->eState ){
65955 if( pCur->skipNext ){
65956 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
65957 pCur->eState = CURSOR_VALID;
65958 if( pCur->skipNext>0 ){
65959 pCur->skipNext = 0;
65962 pCur->skipNext = 0;
65966 pPage = pCur->pPage;
65967 idx = ++pCur->ix;
65968 assert( pPage->isInit );
65974 ** page into more than one b-tree structure. */
65975 testcase( idx>pPage->nCell );
65977 if( idx>=pPage->nCell ){
65978 if( !pPage->leaf ){
65979 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
65984 if( pCur->iPage==0 ){
65985 pCur->eState = CURSOR_INVALID;
65989 pPage = pCur->pPage;
65990 }while( pCur->ix>=pPage->nCell );
65991 if( pPage->intKey ){
65997 if( pPage->leaf ){
66008 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
66009 pCur->info.nSize = 0;
66010 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
66011 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
66012 pPage = pCur->pPage;
66013 if( (++pCur->ix)>=pPage->nCell ){
66014 pCur->ix--;
66017 if( pPage->leaf ){
66049 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
66050 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
66051 assert( pCur->info.nSize==0 );
66052 if( pCur->eState!=CURSOR_VALID ){
66057 if( CURSOR_INVALID==pCur->eState ){
66060 if( pCur->skipNext ){
66061 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
66062 pCur->eState = CURSOR_VALID;
66063 if( pCur->skipNext<0 ){
66064 pCur->skipNext = 0;
66067 pCur->skipNext = 0;
66071 pPage = pCur->pPage;
66072 assert( pPage->isInit );
66073 if( !pPage->leaf ){
66074 int idx = pCur->ix;
66079 while( pCur->ix==0 ){
66080 if( pCur->iPage==0 ){
66081 pCur->eState = CURSOR_INVALID;
66086 assert( pCur->info.nSize==0 );
66087 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
66089 pCur->ix--;
66090 pPage = pCur->pPage;
66091 if( pPage->intKey && !pPage->leaf ){
66102 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
66104 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
66105 pCur->info.nSize = 0;
66106 if( pCur->eState!=CURSOR_VALID
66107 || pCur->ix==0
66108 || pCur->pPage->leaf==0
66112 pCur->ix--;
66133 ** anywhere on the free-list, then it is guaranteed to be returned. If
66153 assert( sqlite3_mutex_held(pBt->mutex) );
66154 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
66155 pPage1 = pBt->pPage1;
66157 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
66159 n = get4byte(&pPage1->aData[36]);
66160 testcase( n==mxPage-1 );
66167 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
66170 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
66171 ** shows that the page 'nearby' is somewhere on the free-list, then
66172 ** the entire-list will be searched for that page.
66179 assert( pBt->autoVacuum );
66191 /* Decrement the free-list count by 1. Set iTrunk to the index of the
66192 ** first free-list trunk page. iPrevTrunk is initially 1.
66194 rc = sqlite3PagerWrite(pPage1->pDbPage);
66196 put4byte(&pPage1->aData[36], n-1);
66199 ** is not true. Otherwise, it runs once for each trunk-page on the
66200 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
66206 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
66209 iTrunk = get4byte(&pPrevTrunk->aData[0]);
66211 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
66214 iTrunk = get4byte(&pPage1->aData[32]);
66218 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
66227 assert( pTrunk->aData!=0 );
66228 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
66230 k = get4byte(&pTrunk->aData[4]);
66236 rc = sqlite3PagerWrite(pTrunk->pDbPage);
66241 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
66244 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
66245 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
66259 rc = sqlite3PagerWrite(pTrunk->pDbPage);
66265 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
66267 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
66271 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
66275 ** pointers to free-list leaves. The first leaf becomes a trunk
66279 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
66289 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
66294 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
66295 put4byte(&pNewTrunk->aData[4], k-1);
66296 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
66299 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
66300 put4byte(&pPage1->aData[32], iNewTrunk);
66302 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
66306 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
66310 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
66316 unsigned char *aData = pTrunk->aData;
66330 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
66332 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
66357 *pPgno, closest+1, k, pTrunk->pgno, n-1));
66358 rc = sqlite3PagerWrite(pTrunk->pDbPage);
66360 if( closest<k-1 ){
66363 put4byte(&aData[4], k-1);
66367 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
66384 ** pager layer with the 'no-content' flag set. This prevents the pager
66386 ** current transaction has already run one or more incremental-vacuum
66389 ** not set the no-content flag. This causes the pager to load and journal
66394 ** file on disk. So the effects of disabling the no-content optimization
66398 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
66400 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
66402 pBt->nPage++;
66403 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
66406 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
66407 /* If *pPgno refers to a pointer-map page, allocate two new pages
66409 ** becomes a new pointer-map page, the second is used by the caller.
66412 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
66413 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
66414 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
66416 rc = sqlite3PagerWrite(pPg->pDbPage);
66420 pBt->nPage++;
66421 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
66424 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
66425 *pPgno = pBt->nPage;
66430 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
66443 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
66444 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
66449 ** This function is used to add page iPage to the database file free-list.
66450 ** It is assumed that the page is not already a part of the free-list.
66461 MemPage *pTrunk = 0; /* Free-list trunk page */
66462 Pgno iTrunk = 0; /* Page number of free-list trunk page */
66463 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
66466 int nFree; /* Initial number of pages on free-list */
66468 assert( sqlite3_mutex_held(pBt->mutex) );
66470 assert( !pMemPage || pMemPage->pgno==iPage );
66475 sqlite3PagerRef(pPage->pDbPage);
66481 rc = sqlite3PagerWrite(pPage1->pDbPage);
66483 nFree = get4byte(&pPage1->aData[36]);
66484 put4byte(&pPage1->aData[36], nFree+1);
66486 if( pBt->btsFlags & BTS_SECURE_DELETE ){
66491 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
66495 memset(pPage->aData, 0, pPage->pBt->pageSize);
66498 /* If the database supports auto-vacuum, write an entry in the pointer-map
66506 /* Now manipulate the actual database free-list structure. There are two
66507 ** possibilities. If the free-list is currently empty, or if the first
66508 ** trunk page in the free-list is full, then this page will become a
66509 ** new free-list trunk page. Otherwise, it will become a leaf of the
66510 ** first trunk page in the current free-list. This block tests if it
66511 ** is possible to add the page as a new free-list leaf.
66516 iTrunk = get4byte(&pPage1->aData[32]);
66522 nLeaf = get4byte(&pTrunk->aData[4]);
66523 assert( pBt->usableSize>32 );
66524 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
66528 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
66533 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
66536 ** usableSize/4 - 8 entries will be reported as corrupt. In order
66538 ** we will continue to restrict the number of entries to usableSize/4 - 8
66541 ** to read "usableSize/4-2" instead of "usableSize/4-8".
66543 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
66548 rc = sqlite3PagerWrite(pTrunk->pDbPage);
66550 put4byte(&pTrunk->aData[4], nLeaf+1);
66551 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
66552 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
66553 sqlite3PagerDontWrite(pPage->pDbPage);
66557 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
66563 ** the page being freed as a leaf page of the first trunk in the free-list.
66564 ** Possibly because the free-list is empty, or possibly because the
66565 ** first trunk in the free-list is full. Either way, the page being freed
66566 ** will become the new first trunk page in the free-list.
66571 rc = sqlite3PagerWrite(pPage->pDbPage);
66575 put4byte(pPage->aData, iTrunk);
66576 put4byte(&pPage->aData[4], 0);
66577 put4byte(&pPage1->aData[32], iPage);
66578 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
66582 pPage->isInit = 0;
66590 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
66610 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66611 pPage->xParseCell(pPage, pCell, pInfo);
66612 if( pInfo->nLocal==pInfo->nPayload ){
66615 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
66619 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
66620 pBt = pPage->pBt;
66621 assert( pBt->usableSize > 4 );
66622 ovflPageSize = pBt->usableSize - 4;
66623 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
66625 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
66627 while( nOvfl-- ){
66642 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
66649 ** freePage2() may zero the page contents if secure-delete mode is
66660 sqlite3PagerUnref(pOvfl->pDbPage);
66675 ** Note that pCell does not necessary need to point to the pPage->aData
66677 ** be constructed in this temporary area then copied into pPage->aData
66697 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66701 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
66702 || sqlite3PagerIswriteable(pPage->pDbPage) );
66705 nHeader = pPage->childPtrSize;
66706 if( pPage->intKey ){
66707 nPayload = pX->nData + pX->nZero;
66708 pSrc = pX->pData;
66709 nSrc = pX->nData;
66710 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
66712 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
66714 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
66715 nSrc = nPayload = (int)pX->nKey;
66716 pSrc = pX->pKey;
66722 if( nPayload<=pPage->maxLocal ){
66733 memset(pPayload+nSrc, 0, nPayload-nSrc);
66740 mn = pPage->minLocal;
66741 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
66742 testcase( n==pPage->maxLocal );
66743 testcase( n==pPage->maxLocal+1 );
66744 if( n > pPage->maxLocal ) n = mn;
66750 pBt = pPage->pBt;
66767 pPage->xParseCell(pPage, pCell, &info);
66768 assert( nHeader==(int)(info.pPayload - pCell) );
66769 assert( info.nKey==pX->nKey );
66782 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
66786 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
66787 || sqlite3PagerIswriteable(pPage->pDbPage) );
66797 nPayload -= n;
66801 nSrc -= n;
66802 spaceLeft -= n;
66806 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
66807 if( pBt->autoVacuum ){
66817 /* If the database supports auto-vacuum, and the second or subsequent
66818 ** overflow page is being allocated, add an entry to the pointer-map
66822 ** to the pointer-map. If we write nothing to this pointer-map slot,
66827 if( pBt->autoVacuum && rc==SQLITE_OK ){
66842 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
66846 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
66847 || sqlite3PagerIswriteable(pPage->pDbPage) );
66852 pPrior = pOvfl->aData;
66854 pPayload = &pOvfl->aData[4];
66855 spaceLeft = pBt->usableSize - 4;
66863 ** Remove the i-th cell from pPage. This routine effects pPage only.
66872 u8 *data; /* pPage->aData */
66878 assert( idx>=0 && idx<pPage->nCell );
66880 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66881 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66882 data = pPage->aData;
66883 ptr = &pPage->aCellIdx[2*idx];
66885 hdr = pPage->hdrOffset;
66887 testcase( pc+sz==pPage->pBt->usableSize );
66888 if( pc+sz > pPage->pBt->usableSize ){
66897 pPage->nCell--;
66898 if( pPage->nCell==0 ){
66901 put2byte(&data[hdr+5], pPage->pBt->usableSize);
66902 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
66903 - pPage->childPtrSize - 8;
66905 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
66906 put2byte(&data[hdr+3], pPage->nCell);
66907 pPage->nFree += 2;
66918 ** in pPage->apOvfl[] and make it point to the cell content (either
66920 ** Allocating a new entry in pPage->aCell[] implies that
66921 ** pPage->nOverflow is incremented.
66927 int i, /* New cell becomes the i-th cell of the page */
66931 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
66937 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
66940 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
66941 assert( MX_CELL(pPage->pBt)<=10921 );
66942 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
66943 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
66944 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
66945 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66949 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
66951 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
66952 if( pPage->nOverflow || sz+2>pPage->nFree ){
66960 j = pPage->nOverflow++;
66961 /* Comparison against ArraySize-1 since we hold back one extra slot
66964 assert( j < ArraySize(pPage->apOvfl)-1 );
66965 pPage->apOvfl[j] = pCell;
66966 pPage->aiOvfl[j] = (u16)i;
66973 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
66974 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
66976 int rc = sqlite3PagerWrite(pPage->pDbPage);
66981 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66982 data = pPage->aData;
66983 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
66989 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
66990 assert( idx+sz <= (int)pPage->pBt->usableSize );
66991 pPage->nFree -= (u16)(2 + sz);
66996 pIns = pPage->aCellIdx + i*2;
66997 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
66999 pPage->nCell++;
67001 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
67002 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
67004 if( pPage->pBt->autoVacuum ){
67027 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
67031 assert( idx>=0 && idx+N<=p->nCell );
67033 assert( p->apCell[idx]!=0 );
67034 if( p->szCell[idx]==0 ){
67035 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
67038 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
67041 N--;
67049 assert( N>=0 && N<p->nCell );
67050 assert( p->szCell[N]==0 );
67051 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
67052 return p->szCell[N];
67055 assert( N>=0 && N<p->nCell );
67056 if( p->szCell[N] ) return p->szCell[N];
67061 ** Array apCell[] contains pointers to nCell b-tree page cells. The
67079 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
67080 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
67081 const int usableSize = pPg->pBt->usableSize;
67084 u8 *pCellptr = pPg->aCellIdx;
67085 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
67089 memcpy(&pTmp[i], &aData[i], usableSize - i);
67095 pCell = &pTmp[pCell - aData];
67097 pData -= szCell[i];
67098 put2byte(pCellptr, (pData - aData));
67102 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
67103 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
67106 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
67107 pPg->nCell = nCell;
67108 pPg->nOverflow = 0;
67111 put2byte(&aData[hdr+3], pPg->nCell);
67112 put2byte(&aData[hdr+5], pData - aData);
67118 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
67121 ** the page needs to be defragmented before the cells will fit), non-zero
67125 ** Argument pCellptr points to the first entry in the cell-pointer array
67127 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
67129 ** that it is safe to overwrite this part of the cell-pointer array.
67137 ** end of the space required by this page for the cell-pointer area (for
67138 ** all cells - not just those inserted by the current call). If the content
67140 ** cells in apCell[], then the cells do not fit and non-zero is returned.
67144 u8 *pBegin, /* End of cell-pointer array */
67145 u8 **ppData, /* IN/OUT: Page content -area pointer */
67146 u8 *pCellptr, /* Pointer to cell-pointer area */
67152 u8 *aData = pPg->aData;
67155 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
67161 if( (pData - pBegin)<sz ) return 1;
67162 pData -= sz;
67165 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
67168 assert( (pSlot+sz)<=pCArray->apCell[i]
67169 || pSlot>=(pCArray->apCell[i]+sz)
67171 memmove(pSlot, pCArray->apCell[i], sz);
67172 put2byte(pCellptr, (pSlot - aData));
67180 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
67183 ** within the body of pPg to the pPg free-list. The cell-pointers and other
67186 ** This function returns the total number of cells added to the free-list.
67194 u8 * const aData = pPg->aData;
67195 u8 * const pEnd = &aData[pPg->pBt->usableSize];
67196 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
67204 u8 *pCell = pCArray->apCell[i];
67210 sz = pCArray->szCell[i]; assert( sz>0 );
67213 assert( pFree>aData && (pFree - aData)<65536 );
67214 freeSpace(pPg, (u16)(pFree - aData), szFree);
67227 assert( pFree>aData && (pFree - aData)<65536 );
67228 freeSpace(pPg, (u16)(pFree - aData), szFree);
67235 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting
67242 ** The pPg->nFree field is invalid when this function returns. It is the
67252 u8 * const aData = pPg->aData;
67253 const int hdr = pPg->hdrOffset;
67254 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
67255 int nCell = pPg->nCell; /* Cells stored on pPg */
67259 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
67263 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
67264 memcpy(pTmp, aData, pPg->pBt->usableSize);
67269 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
67270 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
67271 nCell -= nShift;
67274 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
67282 int nAdd = MIN(nNew,iOld-iNew);
67283 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
67284 pCellptr = pPg->aCellIdx;
67294 for(i=0; i<pPg->nOverflow; i++){
67295 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
67297 pCellptr = &pPg->aCellIdx[iCell * 2];
67298 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
67308 pCellptr = &pPg->aCellIdx[nCell*2];
67311 iNew+nCell, nNew-nCell, pCArray
67314 pPg->nCell = nNew;
67315 pPg->nOverflow = 0;
67317 put2byte(&aData[hdr+3], pPg->nCell);
67318 put2byte(&aData[hdr+5], pData - aData);
67322 u8 *pCell = pCArray->apCell[i+iNew];
67323 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
67324 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
67325 pCell = &pTmp[pCell - aData];
67328 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
67336 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
67358 ** a new entry is being inserted on the extreme right-end of the
67362 ** Instead of trying to balance the 3 right-most leaf pages, just add
67363 ** a new page to the right-hand side and put the one new entry in
67369 ** pPage is the leaf page which is the right-most page in the tree.
67371 ** which is also the right-most entry on the page.
67380 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
67385 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67386 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
67387 assert( pPage->nOverflow==1 );
67390 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
67392 /* Allocate a new page. This page will become the right-sibling of
67401 u8 *pCell = pPage->apOvfl[0];
67402 u16 szCell = pPage->xCellSize(pPage, pCell);
67405 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
67406 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
67410 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
67412 /* If this is an auto-vacuum database, update the pointer map
67422 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
67423 if( szCell>pNew->minLocal ){
67429 ** consists of a 4-byte page number (the page number of pPage) and
67433 ** To find the largest key value on pPage, first find the right-most
67435 ** record-length (a variable length integer at most 32-bits in size)
67437 ** The first of the while(...) loops below skips over the record-length
67441 pCell = findCell(pPage, pPage->nCell-1);
67449 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
67450 0, pPage->pgno, &rc);
67453 /* Set the right-child pointer of pParent to point to the new page. */
67454 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
67468 ** for setting pointer-map entries.
67476 BtShared *pBt = pPage->pBt;
67477 assert( pPage->isInit );
67479 for(j=0; j<pPage->nCell; j++){
67484 pPage->xParseCell(pPage, z, &info);
67486 Pgno ovfl = get4byte(&z[info.nSize-4]);
67488 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
67490 if( !pPage->leaf ){
67493 assert( n==pPage->pgno && e==PTRMAP_BTREE );
67496 if( !pPage->leaf ){
67497 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
67499 assert( n==pPage->pgno && e==PTRMAP_BTREE );
67507 ** This function is used to copy the contents of the b-tree node stored
67509 ** the pointer-map entries for each child page are updated so that the
67525 BtShared * const pBt = pFrom->pBt;
67526 u8 * const aFrom = pFrom->aData;
67527 u8 * const aTo = pTo->aData;
67528 int const iFromHdr = pFrom->hdrOffset;
67529 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
67534 assert( pFrom->isInit );
67535 assert( pFrom->nFree>=iToHdr );
67536 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
67538 /* Copy the b-tree node content from page pFrom to page pTo. */
67540 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
67541 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
67548 pTo->isInit = 0;
67555 /* If this is an auto-vacuum database, update the pointer-map entries
67556 ** for any b-tree or overflow pages that pTo now contains the pointers to.
67598 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
67607 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
67608 int isRoot, /* True if pParent is a root-page */
67616 int nxDiv; /* Next divider slot in pParent->aCell[] */
67621 int pageFlags; /* Value of pPage->aData[0] */
67627 u8 *pRight; /* Location in parent of right-sibling pointer */
67628 u8 *apDiv[NB-1]; /* Divider cells in pParent */
67629 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
67631 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
67643 pBt = pParent->pBt;
67644 assert( sqlite3_mutex_held(pBt->mutex) );
67645 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
67648 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
67656 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
67657 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
67674 i = pParent->nOverflow + pParent->nCell;
67682 nxDiv = i-2+bBulk;
67684 nxDiv = iParentIdx-1;
67686 i = 2-bBulk;
67689 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
67690 pRight = &pParent->aData[pParent->hdrOffset+8];
67692 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
67701 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
67702 if( (i--)==0 ) break;
67704 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
67705 apDiv[i] = pParent->apOvfl[0];
67707 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
67708 pParent->nOverflow = 0;
67710 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
67712 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
67721 ** But not if we are in secure-delete mode. In secure-delete mode,
67726 if( pBt->btsFlags & BTS_FAST_SECURE ){
67729 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
67730 if( (iOff+szNew[i])>(int)pBt->usableSize ){
67736 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
67739 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
67743 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
67753 + pBt->pageSize; /* aSpace1 */
67755 assert( szScratch<=6*(int)pBt->pageSize );
67782 leafCorrection = b.pRef->leaf*4;
67783 leafData = b.pRef->intKeyLeaf;
67786 int limit = pOld->nCell;
67787 u8 *aData = pOld->aData;
67788 u16 maskPage = pOld->maskPage;
67789 u8 *piCell = aData + pOld->cellOffset;
67792 /* Verify that all sibling pages are of the same "type" (table-leaf,
67793 ** table-interior, index-leaf, or index-interior).
67795 if( pOld->aData[0]!=apOld[0]->aData[0] ){
67817 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
67818 if( pOld->nOverflow>0 ){
67819 limit = pOld->aiOvfl[0];
67825 for(k=0; k<pOld->nOverflow; k++){
67826 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
67827 b.apCell[b.nCell] = pOld->apOvfl[k];
67831 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
67840 if( i<nOld-1 && !leafData){
67847 assert( sz<=pBt->maxLocal+23 );
67848 assert( iSpace1 <= (int)pBt->pageSize );
67852 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
67853 if( !pOld->leaf ){
67855 assert( pOld->hdrOffset==0 );
67858 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
67865 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
67877 ** size of all cells on the i-th page and cntNew[] which is the index
67884 ** szNew[i]: Spaced used on the i-th sibling page.
67886 ** the right of the i-th sibling page.
67890 usableSpace = pBt->usableSize - 12 + leafCorrection;
67893 szNew[i] = usableSpace - p->nFree;
67894 for(j=0; j<p->nOverflow; j++){
67895 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
67906 szNew[k-1] = 0;
67907 cntNew[k-1] = b.nCell;
67909 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
67910 szNew[i] -= sz;
67919 cntNew[i]--;
67933 szNew[i+1] -= sz;
67937 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
67946 ** always nearly full, while the right-most sibling might be nearly empty.
67951 ** be so out of balance as to be illegal. For example, the right-most
67954 for(i=k-1; i>0; i--){
67956 int szLeft = szNew[i-1]; /* Size of sibling on the left */
67957 int r; /* Index of right-most cell in left sibling */
67960 r = cntNew[i-1] - 1;
67961 d = r + 1 - leafData;
67968 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
67972 szLeft -= b.szCell[r] + 2;
67973 cntNew[i-1] = r;
67974 r--;
67975 d--;
67978 szNew[i-1] = szLeft;
67979 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
67985 /* Sanity check: For a non-corrupt database file one of the follwing
67992 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
67994 apOld[0]->pgno, apOld[0]->nCell,
67995 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
67996 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
68002 pageFlags = apOld[0]->aData[0];
68008 rc = sqlite3PagerWrite(pNew->pDbPage);
68020 /* Set the pointer-map entry for the new sibling page. */
68022 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
68043 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
68044 aPgFlags[i] = apNew[i]->pDbPage->flags;
68068 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
68070 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
68071 apNew[i]->pgno = pgno;
68077 apNew[0]->pgno, szNew[0], cntNew[0],
68078 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
68079 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
68080 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
68081 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
68082 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
68083 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
68084 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
68085 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
68088 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
68089 put4byte(pRight, apNew[nNew-1]->pgno);
68091 /* If the sibling pages are not leaves, ensure that the right-child pointer
68092 ** of the right-most new sibling page is set to the value that was
68093 ** originally in the same field of the right-most old sibling page. */
68095 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
68096 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
68111 ** associated with the right-child of each sibling may also need to be
68117 u8 *aOld = pNew->aData;
68118 int cntOldNext = pNew->nCell + pNew->nOverflow;
68119 int usableSize = pBt->usableSize;
68127 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
68128 aOld = pOld->aData;
68142 || pNew->pgno!=aPgno[iOld]
68146 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
68148 if( cachedCellSize(&b,i)>pNew->minLocal ){
68157 for(i=0; i<nNew-1; i++){
68169 if( !pNew->leaf ){
68170 memcpy(&pNew->aData[8], pCell, 4);
68172 /* If the tree is a leaf-data tree, and the siblings are leaves,
68174 ** cell consists of the integer key for the right-most cell of
68175 ** the sibling-page assembled above only.
68178 j--;
68179 pNew->xParseCell(pNew, b.apCell[j], &info);
68184 pCell -= 4;
68185 /* Obscure case for non-leaf-data trees: If the cell at pCell was
68192 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
68198 sz = pParent->xCellSize(pParent, pCell);
68202 assert( sz<=pBt->maxLocal+23 );
68203 assert( iOvflSpace <= (int)pBt->pageSize );
68204 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
68206 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
68213 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
68215 ** the left-hand sibling apNew[iPg-1] has been updated.
68219 ** the right-hand sibling apNew[iPg+1] has been updated.
68223 ** The iPg value in the following loop starts at nNew-1 goes down
68224 ** to 0, then back up to nNew-1 again, thus making two passes over
68231 for(i=1-nNew; i<nNew; i++){
68232 int iPg = i<0 ? -i : i;
68236 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
68243 ** only after iPg-1 has already been updated. */
68244 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
68254 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
68255 iNew = cntNew[iPg-1] + !leafData;
68256 nNewCell = cntNew[iPg] - iNew;
68262 apNew[iPg]->nFree = usableSpace-szNew[iPg];
68263 assert( apNew[iPg]->nOverflow==0 );
68264 assert( apNew[iPg]->nCell==nNewCell );
68274 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
68275 /* The root page of the b-tree now contains no cells. The only sibling
68276 ** page is the right-child of the parent. Copy the contents of the
68278 ** b-tree structure by one. This is described as the "balance-shallower"
68279 ** sub-algorithm in some documentation.
68281 ** If this is an auto-vacuum database, the call to copyNodeContent()
68282 ** sets all pointer-map entries corresponding to database image pages
68291 rc = defragmentPage(apNew[0], -1);
68293 assert( apNew[0]->nFree ==
68294 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
68300 /* Fix the pointer map entries associated with the right-child of each
68304 u32 key = get4byte(&apNew[i]->aData[8]);
68305 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
68309 assert( pParent->isInit );
68320 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
68347 ** This function is called when the root page of a b-tree structure is
68352 ** page is then overwritten to make it an empty page with the right-child
68355 ** Before returning, all pointer-map entries corresponding to pages
68356 ** that the new child-page now contains pointers to are updated. The
68357 ** entry corresponding to the new right-child pointer of the root
68369 BtShared *pBt = pRoot->pBt; /* The BTree */
68371 assert( pRoot->nOverflow>0 );
68372 assert( sqlite3_mutex_held(pBt->mutex) );
68374 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
68375 ** page that will become the new right-child of pPage. Copy the contents
68378 rc = sqlite3PagerWrite(pRoot->pDbPage);
68380 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
68383 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
68391 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
68392 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
68393 assert( pChild->nCell==pRoot->nCell );
68395 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
68398 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
68399 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
68400 memcpy(pChild->apOvfl, pRoot->apOvfl,
68401 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
68402 pChild->nOverflow = pRoot->nOverflow;
68404 /* Zero the contents of pRoot. Then install pChild as the right-child. */
68405 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
68406 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
68424 const int nMin = pCur->pBt->usableSize * 2 / 3;
68432 int iPage = pCur->iPage;
68433 MemPage *pPage = pCur->pPage;
68436 if( pPage->nOverflow ){
68437 /* The root page of the b-tree is overfull. In this case call the
68438 ** balance_deeper() function to create a new child for the root-page
68439 ** and copy the current contents of the root-page to it. The
68440 ** next iteration of the do-loop will balance the child page.
68444 rc = balance_deeper(pPage, &pCur->apPage[1]);
68446 pCur->iPage = 1;
68447 pCur->ix = 0;
68448 pCur->aiIdx[0] = 0;
68449 pCur->apPage[0] = pPage;
68450 pCur->pPage = pCur->apPage[1];
68451 assert( pCur->pPage->nOverflow );
68456 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
68459 MemPage * const pParent = pCur->apPage[iPage-1];
68460 int const iIdx = pCur->aiIdx[iPage-1];
68462 rc = sqlite3PagerWrite(pParent->pDbPage);
68465 if( pPage->intKeyLeaf
68466 && pPage->nOverflow==1
68467 && pPage->aiOvfl[0]==pPage->nCell
68468 && pParent->pgno!=1
68469 && pParent->nCell==iIdx
68474 ** happens, the next iteration of the do-loop will balance pParent
68493 ** become overfull or underfull. The next iteration of the do-loop
68498 ** A subsequent iteration of the do-loop will deal with this by
68500 ** but it doesn't deal with overflow cells - just moves them to a
68507 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
68509 pCur->hints&BTREE_BULKLOAD);
68525 pPage->nOverflow = 0;
68527 /* The next iteration of the do-loop balances the parent page. */
68529 pCur->iPage--;
68530 assert( pCur->iPage>=0 );
68531 pCur->pPage = pCur->apPage[pCur->iPage];
68557 ** If the seekResult parameter is non-zero, then a successful call to
68568 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
68569 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
68579 int loc = seekResult; /* -1: before desired location +1: after */
68583 Btree *p = pCur->pBtree;
68584 BtShared *pBt = p->pBt;
68590 if( pCur->eState==CURSOR_FAULT ){
68591 assert( pCur->skipNext!=SQLITE_OK );
68592 return pCur->skipNext;
68596 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
68597 && pBt->inTransaction==TRANS_WRITE
68598 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
68599 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
68602 ** expecting an index b-tree, then the caller should be inserting blob
68606 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
68610 ** In some cases, the call to btreeMoveto() below is a no-op. For
68611 ** example, when inserting data into a table with auto-generated integer
68614 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
68619 if( pCur->curFlags & BTCF_Multiple ){
68620 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
68624 if( pCur->pKeyInfo==0 ){
68625 assert( pX->pKey==0 );
68626 /* If this is an insert into a table b-tree, invalidate any incrblob
68628 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
68633 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
68638 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
68641 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
68645 if( pX->nMem ){
68647 r.pKeyInfo = pCur->pKeyInfo;
68648 r.aMem = pX->aMem;
68649 r.nField = pX->nMem;
68657 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
68661 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
68663 pPage = pCur->pPage;
68664 assert( pPage->intKey || pX->nKey>=0 );
68665 assert( pPage->leaf || !pPage->intKey );
68668 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
68670 assert( pPage->isInit );
68671 newCell = pBt->pTmpSpace;
68675 assert( szNew==pPage->xCellSize(pPage, newCell) );
68677 idx = pCur->ix;
68680 assert( idx<pPage->nCell );
68681 rc = sqlite3PagerWrite(pPage->pDbPage);
68686 if( !pPage->leaf ){
68691 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
68701 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
68703 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
68709 }else if( loc<0 && pPage->nCell>0 ){
68710 assert( pPage->leaf );
68711 idx = ++pCur->ix;
68712 pCur->curFlags &= ~BTCF_ValidNKey;
68714 assert( pPage->leaf );
68717 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
68718 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
68732 ** multiple records into an intkey b-tree using a single cursor (as can
68735 ** the b-tree if possible. If the cursor is left pointing to the last
68740 pCur->info.nSize = 0;
68741 if( pPage->nOverflow ){
68743 pCur->curFlags &= ~(BTCF_ValidNKey);
68750 pCur->pPage->nOverflow = 0;
68751 pCur->eState = CURSOR_INVALID;
68754 if( pCur->pKeyInfo ){
68755 assert( pCur->pKey==0 );
68756 pCur->pKey = sqlite3Malloc( pX->nKey );
68757 if( pCur->pKey==0 ){
68760 memcpy(pCur->pKey, pX->pKey, pX->nKey);
68763 pCur->eState = CURSOR_REQUIRESEEK;
68764 pCur->nKey = pX->nKey;
68767 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
68786 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
68791 Btree *p = pCur->pBtree;
68792 BtShared *pBt = p->pBt;
68803 assert( pBt->inTransaction==TRANS_WRITE );
68804 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
68805 assert( pCur->curFlags & BTCF_WriteFlag );
68806 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
68807 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
68808 assert( pCur->ix<pCur->pPage->nCell );
68809 assert( pCur->eState==CURSOR_VALID );
68812 iCellDepth = pCur->iPage;
68813 iCellIdx = pCur->ix;
68814 pPage = pCur->pPage;
68819 ** will cause a b-tree rebalance, then this is done by saving the cursor
68827 if( !pPage->leaf
68828 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
68830 /* A b-tree rebalance will be required after deleting this entry.
68844 ** sub-tree headed by the child page of the cell being deleted. This makes
68846 if( !pPage->leaf ){
68854 if( pCur->curFlags & BTCF_Multiple ){
68855 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
68859 /* If this is a delete operation to remove a row from a table b-tree,
68861 if( pCur->pKeyInfo==0 ){
68862 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
68868 rc = sqlite3PagerWrite(pPage->pDbPage);
68875 ** is currently pointing to the largest entry in the sub-tree headed
68876 ** by the child-page of the cell that was just deleted from an internal
68879 if( !pPage->leaf ){
68880 MemPage *pLeaf = pCur->pPage;
68885 if( iCellDepth<pCur->iPage-1 ){
68886 n = pCur->apPage[iCellDepth+1]->pgno;
68888 n = pCur->pPage->pgno;
68890 pCell = findCell(pLeaf, pLeaf->nCell-1);
68891 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
68892 nCell = pLeaf->xCellSize(pLeaf, pCell);
68894 pTmp = pBt->pTmpSpace;
68896 rc = sqlite3PagerWrite(pLeaf->pDbPage);
68898 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
68900 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
68920 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
68921 releasePageNotNull(pCur->pPage);
68922 pCur->iPage--;
68923 while( pCur->iPage>iCellDepth ){
68924 releasePage(pCur->apPage[pCur->iPage--]);
68926 pCur->pPage = pCur->apPage[pCur->iPage];
68932 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
68933 assert( pPage==pCur->pPage || CORRUPT_DB );
68934 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
68935 pCur->eState = CURSOR_SKIPNEXT;
68936 if( iCellIdx>=pPage->nCell ){
68937 pCur->skipNext = -1;
68938 pCur->ix = pPage->nCell-1;
68940 pCur->skipNext = 1;
68946 pCur->eState = CURSOR_REQUIRESEEK;
68966 BtShared *pBt = p->pBt;
68970 int ptfFlags; /* Page-type flage for the root page of new table */
68973 assert( pBt->inTransaction==TRANS_WRITE );
68974 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
68982 if( pBt->autoVacuum ){
68983 Pgno pgnoMove; /* Move a page here to make room for the root-page */
68988 ** out to be an overflow page, delete all overflow page-map caches
68994 ** root page of the new table should go. meta[3] is the largest root-page
68995 ** created so far, so the new root-page is (meta[3]+1).
69000 /* The new root-page may not be allocated on a pointer-map page, or the
69020 /* pgnoRoot is the page that will be used for the root-page of
69064 rc = sqlite3PagerWrite(pRoot->pDbPage);
69073 /* Update the pointer-map and meta-data with the new root-page number. */
69084 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
69096 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
69103 sqlite3PagerUnref(pRoot->pDbPage);
69104 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
69133 assert( sqlite3_mutex_held(pBt->mutex) );
69139 if( pPage->bBusy ){
69143 pPage->bBusy = 1;
69144 hdr = pPage->hdrOffset;
69145 for(i=0; i<pPage->nCell; i++){
69147 if( !pPage->leaf ){
69154 if( !pPage->leaf ){
69155 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
69158 assert( pPage->intKey || CORRUPT_DB );
69159 testcase( !pPage->intKey );
69160 *pnChange += pPage->nCell;
69164 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
69165 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
69169 pPage->bBusy = 0;
69189 BtShared *pBt = p->pBt;
69191 assert( p->inTrans==TRANS_WRITE );
69197 ** is the root of a table b-tree - if it is not, the following call is
69198 ** a no-op). */
69212 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
69238 BtShared *pBt = p->pBt;
69241 assert( p->inTrans==TRANS_WRITE );
69258 if( pBt->autoVacuum ){
69263 /* If the table being dropped is the table with the largest root-page
69272 /* The table being dropped does not have the largest root-page
69274 ** gap left by the deleted root-page.
69297 /* Set the new 'max-root-page' value in the database header. This
69299 ** be a root-page number, less one again if that is the
69302 maxRootPgno--;
69305 maxRootPgno--;
69327 ** This function may only be called if the b-tree connection already
69330 ** Read the meta-information out of a database file. Meta[0]
69333 ** is read-only, the others are read/write.
69347 BtShared *pBt = p->pBt;
69350 assert( p->inTrans>TRANS_NONE );
69352 assert( pBt->pPage1 );
69356 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
69358 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
69361 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
69362 ** database, mark the database as read-only. */
69365 pBt->btsFlags |= BTS_READ_ONLY;
69373 ** Write meta-information back into the database. Meta[0] is
69374 ** read-only and may not be written.
69377 BtShared *pBt = p->pBt;
69382 assert( p->inTrans==TRANS_WRITE );
69383 assert( pBt->pPage1!=0 );
69384 pP1 = pBt->pPage1->aData;
69385 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
69390 assert( pBt->autoVacuum || iMeta==0 );
69392 pBt->incrVacuum = (u8)iMeta;
69402 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
69403 ** number of entries in the b-tree and write the result to *pnEntry.
69420 ** page in the B-Tree structure (not including overflow pages).
69424 MemPage *pPage; /* Current page of the b-tree */
69426 /* If this is a leaf page or the tree is not an int-key tree, then
69430 pPage = pCur->pPage;
69431 if( pPage->leaf || !pPage->intKey ){
69432 nEntry += pPage->nCell;
69438 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
69440 ** to visit is the right-child of its parent.
69445 if( pPage->leaf ){
69447 if( pCur->iPage==0 ){
69448 /* All pages of the b-tree have been visited. Return successfully. */
69453 }while ( pCur->ix>=pCur->pPage->nCell );
69455 pCur->ix++;
69456 pPage = pCur->pPage;
69460 ** points at. This is the right-child if (iIdx==pPage->nCell).
69462 iIdx = pCur->ix;
69463 if( iIdx==pPage->nCell ){
69464 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
69480 return p->pBt->pPager;
69493 if( !pCheck->mxErr ) return;
69494 pCheck->mxErr--;
69495 pCheck->nErr++;
69497 if( pCheck->errMsg.nChar ){
69498 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
69500 if( pCheck->zPfx ){
69501 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
69503 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
69505 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
69506 pCheck->mallocFailed = 1;
69514 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
69518 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
69519 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
69526 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
69527 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
69533 ** reference to the page, add an error message to pCheck->zErrMsg.
69541 if( iPage>pCheck->nPage ){
69555 ** Check that the entry in the pointer-map for page iChild maps to
69569 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
69571 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
69597 while( N-- > 0 && pCheck->mxErr ){
69607 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
69615 if( pCheck->pBt->autoVacuum ){
69619 if( n>(int)pCheck->pBt->usableSize/4-2 ){
69622 N--;
69627 if( pCheck->pBt->autoVacuum ){
69633 N -= n;
69638 /* If this database supports auto-vacuum and iPage is not the last
69639 ** page in this overflow list, check that the pointer-map entry for
69642 if( pCheck->pBt->autoVacuum && N>0 ){
69652 checkAppendMsg(pCheck, "free-page count in header is too small");
69659 ** An implementation of a min-heap.
69669 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
69696 aHeap[0]--;
69733 int depth = -1, d2; /* Depth of a subtree */
69749 u32 *heap = 0; /* Min-heap used for checking cell coverage */
69750 u32 x, prev = 0; /* Next and previous entry on the min-heap */
69751 const char *saved_zPfx = pCheck->zPfx;
69752 int saved_v1 = pCheck->v1;
69753 int saved_v2 = pCheck->v2;
69758 pBt = pCheck->pBt;
69759 usableSize = pBt->usableSize;
69762 pCheck->zPfx = "Page %d: ";
69763 pCheck->v1 = iPage;
69772 savedIsInit = pPage->isInit;
69773 pPage->isInit = 0;
69780 data = pPage->aData;
69781 hdr = pPage->hdrOffset;
69784 pCheck->zPfx = "On tree page %d cell %d: ";
69788 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
69791 assert( pPage->nCell==nCell );
69793 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
69794 ** immediately follows the b-tree page header. */
69795 cellStart = hdr + 12 - 4*pPage->leaf;
69796 assert( pPage->aCellIdx==&data[cellStart] );
69797 pCellIdx = &data[cellStart + 2*(nCell-1)];
69799 if( !pPage->leaf ){
69800 /* Analyze the right-child page of internal pages */
69803 if( pBt->autoVacuum ){
69804 pCheck->zPfx = "On page %d at right child: ";
69813 heap = pCheck->heap;
69817 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
69819 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
69823 pCheck->v2 = i;
69826 pCellIdx -= 2;
69827 if( pc<contentOffset || pc>usableSize-4 ){
69829 pc, contentOffset, usableSize-4);
69834 pPage->xParseCell(pPage, pCell, &info);
69842 if( pPage->intKey ){
69854 assert( pc + info.nSize - 4 <= usableSize );
69855 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
69856 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
69858 if( pBt->autoVacuum ){
69865 if( !pPage->leaf ){
69869 if( pBt->autoVacuum ){
69880 /* Populate the coverage-checking heap for leaf pages */
69881 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
69888 pCheck->zPfx = 0;
69889 if( doCoverageCheck && pCheck->mxErr>0 ){
69890 /* For leaf pages, the min-heap has already been initialized and the
69893 if( !pPage->leaf ){
69894 heap = pCheck->heap;
69896 for(i=nCell-1; i>=0; i--){
69899 size = pPage->xCellSize(pPage, &data[pc]);
69900 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
69903 /* Add the freeblocks to the min-heap
69905 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
69912 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
69915 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
69916 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
69917 ** big-endian integer which is the offset in the b-tree page of the next
69921 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
69924 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
69927 /* Analyze the min-heap looking for overlap between cells and/or
69930 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
69935 ** The loop below pulls entries from the min-heap in order and compares
69941 prev = contentOffset - 1; /* Implied first min-heap entry */
69948 nFrag += (x>>16) - (prev&0xffff) - 1;
69952 nFrag += usableSize - (prev&0xffff) - 1;
69953 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
69954 ** is stored in the fifth field of the b-tree page header.
69955 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
69966 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
69968 pCheck->zPfx = saved_zPfx;
69969 pCheck->v1 = saved_v1;
69970 pCheck->v2 = saved_v2;
69981 ** A read-only or read-write transaction must be opened before calling
69986 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
69998 BtShared *pBt = p->pBt;
69999 int savedDbFlags = pBt->db->flags;
70004 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
70005 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
70008 sCheck.pPager = pBt->pPager;
70029 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
70041 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
70042 get4byte(&pBt->pPage1->aData[36]));
70047 testcase( pBt->db->flags & SQLITE_CellSizeCk );
70048 pBt->db->flags &= ~SQLITE_CellSizeCk;
70053 if( pBt->autoVacuum && aRoot[i]>1 ){
70059 pBt->db->flags = savedDbFlags;
70069 /* If the database supports auto-vacuum, make sure no tables contain
70070 ** references to pointer-map pages.
70073 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
70077 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
70095 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
70103 ** an empty string if the database is in-memory or a TEMP database.
70109 assert( p->pBt->pPager!=0 );
70110 return sqlite3PagerFilename(p->pBt->pPager, 1);
70122 assert( p->pBt->pPager!=0 );
70123 return sqlite3PagerJournalname(p->pBt->pPager);
70127 ** Return non-zero if a transaction is active.
70130 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
70131 return (p && (p->inTrans==TRANS_WRITE));
70139 ** transaction on the shared-cache the argument Btree is connected to.
70146 BtShared *pBt = p->pBt;
70148 if( pBt->inTransaction!=TRANS_NONE ){
70151 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
70160 ** Return non-zero if a read (or write) transaction is active.
70164 assert( sqlite3_mutex_held(p->db->mutex) );
70165 return p->inTrans!=TRANS_NONE;
70170 assert( sqlite3_mutex_held(p->db->mutex) );
70171 return p->nBackup!=0;
70176 ** a single shared-btree. The memory is used by client code for its own
70177 ** purposes (for example, to store a high-level schema associated with
70178 ** the shared-btree). The btree layer manages reference counting issues.
70180 ** The first time this is called on a shared-btree, nBytes bytes of memory
70189 ** Just before the shared-btree is closed, the function passed as the
70195 BtShared *pBt = p->pBt;
70197 if( !pBt->pSchema && nBytes ){
70198 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
70199 pBt->xFreeSchema = xFree;
70202 return pBt->pSchema;
70212 assert( sqlite3_mutex_held(p->db->mutex) );
70229 assert( p->inTrans!=TRANS_NONE );
70230 if( p->sharable ){
70260 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
70261 assert( pCsr->curFlags & BTCF_Incrblob );
70267 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
70268 if( pCsr->eState!=CURSOR_VALID ){
70274 ** version of the b-tree page modified by the accessPayload call below.
70280 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
70286 ** (c) the connection holds a write-lock on the table (if required),
70287 ** (d) there are no conflicting read-locks, and
70290 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
70293 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
70294 && pCsr->pBt->inTransaction==TRANS_WRITE );
70295 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
70296 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
70297 assert( pCsr->pPage->intKey );
70306 pCur->curFlags |= BTCF_Incrblob;
70307 pCur->pBtree->hasIncrblobCur = 1;
70317 BtShared *pBt = pBtree->pBt;
70325 pBt->btsFlags &= ~BTS_NO_WAL;
70326 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
70330 u8 *aData = pBt->pPage1->aData;
70334 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70343 pBt->btsFlags &= ~BTS_NO_WAL;
70352 return (pCsr->hints & mask)!=0;
70356 ** Return true if the given Btree is read-only.
70359 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
70372 return p->sharable;
70381 testcase( p->sharable );
70382 return p->pBt->nRef;
70410 Btree *pDest; /* Destination b-tree file */
70412 int bDestLocked; /* True once a write-transaction is open on pDest */
70416 Btree *pSrc; /* Source b-tree file */
70434 ** structure may be accessed via two groups of thread-safe entry points:
70448 ** backup_pagecount() are not thread-safe functions. If they are called
70456 ** Non-sharable Btrees (in-memory databases for example), do not have
70493 return pDb->aDb[i].pBt;
70502 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
70507 ** Check that there is no open read-transaction on the b-tree passed as the
70509 ** is an open read-transaction, return SQLITE_ERROR and leave an error
70549 ** a malfunction or a deadlock.
70551 sqlite3_mutex_enter(pSrcDb->mutex);
70552 sqlite3_mutex_enter(pDestDb->mutex);
70561 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
70572 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
70573 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
70574 p->pDestDb = pDestDb;
70575 p->pSrcDb = pSrcDb;
70576 p->iNext = 1;
70577 p->isAttached = 0;
70579 if( 0==p->pSrc || 0==p->pDest
70580 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
70592 p->pSrc->nBackup++;
70595 sqlite3_mutex_leave(pDestDb->mutex);
70596 sqlite3_mutex_leave(pSrcDb->mutex);
70620 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
70621 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
70622 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
70626 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
70627 ** guaranteed that the shared-mutex is held by this thread, handle
70628 ** p->pSrc may not actually be the owner. */
70629 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
70630 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
70635 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
70636 assert( p->bDestLocked );
70637 assert( !isFatalError(p->rc) );
70638 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
70641 /* Catch the case where the destination is an in-memory database and the
70672 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
70675 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
70693 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
70705 ** this function is a no-op.
70725 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
70726 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
70727 p->pNext = *pp;
70729 p->isAttached = 1;
70733 ** Copy nPage pages from the source b-tree to the destination.
70744 sqlite3_mutex_enter(p->pSrcDb->mutex);
70745 sqlite3BtreeEnter(p->pSrc);
70746 if( p->pDestDb ){
70747 sqlite3_mutex_enter(p->pDestDb->mutex);
70750 rc = p->rc;
70752 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
70753 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
70755 int nSrcPage = -1; /* Size of source db in pages */
70758 /* If the source pager is currently in a write-transaction, return
70761 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
70767 /* If there is no open read-transaction on the source database, open
70771 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
70772 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
70782 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
70787 if( SQLITE_OK==rc && p->bDestLocked==0
70788 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
70790 p->bDestLocked = 1;
70791 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
70796 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
70797 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
70798 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
70803 /* Now that there is a read-lock on the source database, query the
70806 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
70808 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
70809 const Pgno iSrcPg = p->iNext; /* Source page number */
70810 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
70818 p->iNext++;
70821 p->nPagecount = nSrcPage;
70822 p->nRemaining = nSrcPage+1-p->iNext;
70823 if( p->iNext>(Pgno)nSrcPage ){
70825 }else if( !p->isAttached ){
70831 ** is to make sure that the schema-version really does change in
70837 rc = sqlite3BtreeNewDb(p->pDest);
70841 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
70844 if( p->pDestDb ){
70845 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
70848 rc = sqlite3BtreeSetVersion(p->pDest, 2);
70865 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
70866 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
70869 nDestTruncate = (nSrcPage+ratio-1)/ratio;
70870 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
70871 nDestTruncate--;
70879 /* If the source page-size is smaller than the destination page-size,
70885 ** pending-byte page in the source database may need to be
70898 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
70910 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
70954 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
70964 ** "committing" a read-only transaction cannot fail.
70968 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
70969 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
70976 p->rc = rc;
70978 if( p->pDestDb ){
70979 sqlite3_mutex_leave(p->pDestDb->mutex);
70981 sqlite3BtreeLeave(p->pSrc);
70982 sqlite3_mutex_leave(p->pSrcDb->mutex);
70996 pSrcDb = p->pSrcDb;
70997 sqlite3_mutex_enter(pSrcDb->mutex);
70998 sqlite3BtreeEnter(p->pSrc);
70999 if( p->pDestDb ){
71000 sqlite3_mutex_enter(p->pDestDb->mutex);
71004 if( p->pDestDb ){
71005 p->pSrc->nBackup--;
71007 if( p->isAttached ){
71008 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
71010 pp = &(*pp)->pNext;
71012 *pp = p->pNext;
71016 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
71019 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
71020 if( p->pDestDb ){
71021 sqlite3Error(p->pDestDb, rc);
71024 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
71026 sqlite3BtreeLeave(p->pSrc);
71027 if( p->pDestDb ){
71028 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
71048 return p->nRemaining;
71062 return p->nPagecount;
71084 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
71085 if( !isFatalError(p->rc) && iPage<p->iNext ){
71091 assert( p->pDestDb );
71092 sqlite3_mutex_enter(p->pDestDb->mutex);
71094 sqlite3_mutex_leave(p->pDestDb->mutex);
71097 p->rc = rc;
71100 }while( (p = p->pNext)!=0 );
71119 for(p=pBackup; p; p=p->pNext){
71120 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
71121 p->iNext = 1;
71143 if( pFd->pMethods ){
71156 b.pSrcDb = pFrom->db;
71169 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
71176 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
71222 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
71228 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
71231 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
71233 if( p->flags & MEM_Null ){
71235 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob
71243 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
71246 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
71247 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
71248 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
71251 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
71259 assert( (p->flags & MEM_Cleared)==0 );
71263 assert( p->szMalloc==0
71264 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
71274 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
71276 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
71277 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
71278 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
71279 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
71294 ** routine is a no-op.
71304 assert( (pMem->flags&MEM_RowSet)==0 );
71307 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
71310 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71320 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
71321 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
71327 ** Make sure pMem->z points to a writable allocation of at least
71331 ** pMem->z into the new allocation. pMem must be either a string or
71333 ** in pMem->z is discarded.
71337 assert( (pMem->flags&MEM_RowSet)==0 );
71338 testcase( pMem->db==0 );
71342 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
71343 testcase( bPreserve && pMem->z==0 );
71345 assert( pMem->szMalloc==0
71346 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
71348 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
71349 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
71352 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
71353 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
71355 if( pMem->zMalloc==0 ){
71357 pMem->z = 0;
71358 pMem->szMalloc = 0;
71361 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
71364 if( bPreserve && pMem->z ){
71365 assert( pMem->z!=pMem->zMalloc );
71366 memcpy(pMem->zMalloc, pMem->z, pMem->n);
71368 if( (pMem->flags&MEM_Dyn)!=0 ){
71369 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
71370 pMem->xDel((void *)(pMem->z));
71373 pMem->z = pMem->zMalloc;
71374 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
71379 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
71380 ** If pMem->zMalloc already meets or exceeds the requested size, this
71381 ** routine is a no-op.
71384 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
71393 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
71394 if( pMem->szMalloc<szNew ){
71397 assert( (pMem->flags & MEM_Dyn)==0 );
71398 pMem->z = pMem->zMalloc;
71399 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
71408 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
71411 pMem->z[pMem->n] = 0;
71412 pMem->z[pMem->n+1] = 0;
71413 pMem->flags |= MEM_Term;
71424 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71425 assert( (pMem->flags&MEM_RowSet)==0 );
71426 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
71428 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
71433 pMem->flags &= ~MEM_Ephem;
71435 pMem->pScopyFrom = 0;
71442 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
71448 assert( pMem->flags & MEM_Zero );
71449 assert( pMem->flags&MEM_Blob );
71450 assert( (pMem->flags&MEM_RowSet)==0 );
71451 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71454 nByte = pMem->n + pMem->u.nZero;
71462 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
71463 pMem->n += pMem->u.nZero;
71464 pMem->flags &= ~(MEM_Zero|MEM_Term);
71473 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71474 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
71475 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
71476 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
71486 ** is a no-op.
71498 int fg = pMem->flags;
71501 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71505 assert( (pMem->flags&MEM_RowSet)==0 );
71510 pMem->enc = 0;
71514 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
71516 ** is UTF-16le or UTF-16be do a translation.
71518 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
71521 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
71524 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
71526 pMem->n = sqlite3Strlen30(pMem->z);
71527 pMem->enc = SQLITE_UTF8;
71528 pMem->flags |= MEM_Str|MEM_Term;
71529 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
71546 assert( pFunc->xFinalize!=0 );
71547 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
71548 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71552 t.db = pMem->db;
71556 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
71557 assert( (pMem->flags & MEM_Dyn)==0 );
71558 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
71573 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
71575 if( p->flags&MEM_Agg ){
71576 sqlite3VdbeMemFinalize(p, p->u.pDef);
71577 assert( (p->flags & MEM_Agg)==0 );
71578 testcase( p->flags & MEM_Dyn );
71580 if( p->flags&MEM_Dyn ){
71581 assert( (p->flags&MEM_RowSet)==0 );
71582 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
71583 p->xDel((void *)p->z);
71584 }else if( p->flags&MEM_RowSet ){
71585 sqlite3RowSetClear(p->u.pRowSet);
71586 }else if( p->flags&MEM_Frame ){
71587 VdbeFrame *pFrame = p->u.pFrame;
71588 pFrame->pParent = pFrame->v->pDelFrame;
71589 pFrame->v->pDelFrame = pFrame;
71591 p->flags = MEM_Null;
71596 ** by p->xDel and memory in p->zMalloc.
71606 if( p->szMalloc ){
71607 sqlite3DbFreeNN(p->db, p->zMalloc);
71608 p->szMalloc = 0;
71610 p->z = 0;
71625 if( VdbeMemDynamic(p) || p->szMalloc ){
71631 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
71632 ** If the double is out of range of a 64-bit signed integer then
71633 ** return the closest available 64-bit signed integer.
71637 /* When floating-point is omitted, double and int64 are the same thing */
71642 ** minimum and maximum 64-bit integers, or they define them
71645 ** larger than a 32-bit integer constant.
71664 ** a floating-point then the value returned is the integer part.
71667 ** an SQL-NULL value, return 0.
71673 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
71678 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71680 flags = pMem->flags;
71682 return pMem->u.i;
71684 return doubleToInt64(pMem->u.r);
71686 assert( pMem->z || pMem->n==0 );
71702 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
71706 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71708 if( pMem->flags & MEM_Real ){
71709 return pMem->u.r;
71710 }else if( pMem->flags & MEM_Int ){
71711 return (double)pMem->u.i;
71712 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
71726 assert( pMem->flags & MEM_Real );
71727 assert( (pMem->flags & MEM_RowSet)==0 );
71728 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71731 ix = doubleToInt64(pMem->u.r);
71735 ** (1) the round-trip conversion real->int->real is a no-op, and
71743 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
71744 pMem->u.i = ix;
71753 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71754 assert( (pMem->flags & MEM_RowSet)==0 );
71757 pMem->u.i = sqlite3VdbeIntValue(pMem);
71767 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71770 pMem->u.r = sqlite3VdbeRealValue(pMem);
71784 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
71786 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
71787 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71788 rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
71792 i64 i = pMem->u.i;
71793 sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
71794 if( rc==1 && pMem->u.r==(double)i ){
71795 pMem->u.i = i;
71802 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
71803 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
71815 if( pMem->flags & MEM_Null ) return;
71818 if( (pMem->flags & MEM_Blob)==0 ){
71820 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
71821 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
71823 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
71842 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
71844 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
71845 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
71858 pMem->flags = flags;
71859 pMem->db = db;
71860 pMem->szMalloc = 0;
71880 pMem->flags = MEM_Null;
71893 pMem->flags = MEM_Blob|MEM_Zero;
71894 pMem->n = 0;
71896 pMem->u.nZero = n;
71897 pMem->enc = SQLITE_UTF8;
71898 pMem->z = 0;
71904 ** a 64-bit integer.
71908 pMem->u.i = val;
71909 pMem->flags = MEM_Int;
71920 pMem->u.i = val;
71921 pMem->flags = MEM_Int;
71925 /* A no-op destructor */
71938 assert( pMem->flags==MEM_Null );
71939 pMem->u.zPType = zPType ? zPType : "";
71940 pMem->z = pPtr;
71941 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
71942 pMem->eSubtype = 'p';
71943 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
71954 pMem->u.r = val;
71955 pMem->flags = MEM_Real;
71965 sqlite3 *db = pMem->db;
71967 assert( (pMem->flags & MEM_RowSet)==0 );
71969 pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
71970 if( db->mallocFailed ){
71971 pMem->flags = MEM_Null;
71972 pMem->szMalloc = 0;
71974 assert( pMem->zMalloc );
71975 pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
71976 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
71977 assert( pMem->u.pRowSet!=0 );
71978 pMem->flags = MEM_RowSet;
71984 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
71987 assert( p->db!=0 );
71988 if( p->flags & (MEM_Str|MEM_Blob) ){
71989 int n = p->n;
71990 if( p->flags & MEM_Zero ){
71991 n += p->u.nZero;
71993 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
72004 ** This is used for testing and debugging only - to make sure shallow
72010 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
72011 if( pX->pScopyFrom==pMem ){
72012 pX->flags |= MEM_Undefined;
72013 pX->pScopyFrom = 0;
72016 pMem->pScopyFrom = 0;
72023 ** pTo are freed. The pFrom->z field is not duplicated. If
72024 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
72033 assert( (pFrom->flags & MEM_RowSet)==0 );
72034 assert( pTo->db==pFrom->db );
72037 if( (pFrom->flags&MEM_Static)==0 ){
72038 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
72040 pTo->flags |= srcType;
72051 assert( (pFrom->flags & MEM_RowSet)==0 );
72054 pTo->flags &= ~MEM_Dyn;
72055 if( pTo->flags&(MEM_Str|MEM_Blob) ){
72056 if( 0==(pFrom->flags&MEM_Static) ){
72057 pTo->flags |= MEM_Ephem;
72072 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
72073 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
72074 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
72078 pFrom->flags = MEM_Null;
72079 pFrom->szMalloc = 0;
72104 int nByte = n; /* New value for pMem->n */
72106 u16 flags = 0; /* New value for pMem->flags */
72108 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
72109 assert( (pMem->flags & MEM_RowSet)==0 );
72117 if( pMem->db ){
72118 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
72152 memcpy(pMem->z, z, nAlloc);
72155 pMem->zMalloc = pMem->z = (char *)z;
72156 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
72159 pMem->z = (char *)z;
72160 pMem->xDel = xDel;
72164 pMem->n = nByte;
72165 pMem->flags = flags;
72166 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
72169 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
72188 ** pMem->zMalloc to hold the content from the btree, if possible. New
72189 ** pMem->zMalloc space will be allocated if necessary. The calling routine
72203 pMem->flags = MEM_Null;
72205 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
72207 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
72208 pMem->flags = MEM_Blob;
72209 pMem->n = (int)amt;
72231 assert( (pMem->flags & MEM_RowSet)==0 );
72236 pMem->z = &zData[offset];
72237 pMem->flags = MEM_Blob|MEM_Ephem;
72238 pMem->n = (int)amt;
72249 ** to a zero-terminated version of that string.
72253 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
72255 assert( (pVal->flags & MEM_RowSet)==0 );
72256 assert( (pVal->flags & (MEM_Null))==0 );
72257 if( pVal->flags & (MEM_Blob|MEM_Str) ){
72259 pVal->flags |= MEM_Str;
72260 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
72263 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
72264 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
72269 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
72272 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
72274 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
72275 || pVal->db->mallocFailed );
72276 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
72277 return pVal->z;
72289 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
72295 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
72297 assert( (pVal->flags & MEM_RowSet)==0 );
72298 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
72299 return pVal->z;
72301 if( pVal->flags&MEM_Null ){
72313 p->flags = MEM_Null;
72314 p->db = db;
72335 ** Otherwise, if the second argument is non-zero, then this function is
72344 UnpackedRecord *pRec = p->ppRec[0];
72347 Index *pIdx = p->pIdx; /* Index being probed */
72350 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
72355 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
72356 if( pRec->pKeyInfo ){
72357 assert( pRec->pKeyInfo->nAllField==nCol );
72358 assert( pRec->pKeyInfo->enc==ENC(db) );
72359 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
72361 pRec->aMem[i].flags = MEM_Null;
72362 pRec->aMem[i].db = db;
72370 p->ppRec[0] = pRec;
72373 pRec->nField = p->iVal+1;
72374 return &pRec->aMem[p->iVal];
72421 assert( (p->flags & EP_TokenOnly)==0 );
72422 pList = p->x.pList;
72423 if( pList ) nVal = pList->nExpr;
72424 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
72426 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
72427 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
72439 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
72450 assert( pCtx->pParse->rc==SQLITE_OK );
72454 pFunc->xSFunc(&ctx, nVal, apVal);
72457 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
72464 pCtx->pParse->nErr++;
72467 pCtx->pParse->rc = rc;
72513 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
72515 if( op==TK_REGISTER ) op = pExpr->op2;
72517 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
72524 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
72527 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
72528 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
72538 ** case when the value is -9223372036854775808.
72541 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
72542 pExpr = pExpr->pLeft;
72543 op = pExpr->op;
72544 negInt = -1;
72545 zNeg = "-";
72552 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
72554 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
72556 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
72563 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
72568 /* This branch happens for multiple negative signs. Ex: -(-5) */
72569 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
72573 if( pVal->flags & MEM_Real ){
72574 pVal->u.r = -pVal->u.r;
72575 }else if( pVal->u.i==SMALLEST_INT64 ){
72576 pVal->u.r = -(double)SMALLEST_INT64;
72579 pVal->u.i = -pVal->u.i;
72591 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
72592 assert( pExpr->u.zToken[1]=='\'' );
72595 zVal = &pExpr->u.zToken[2];
72596 nVal = sqlite3Strlen30(zVal)-1;
72614 if( pCtx==0 || pCtx->pParse->nErr==0 )
72688 ** Register built-in functions used to help read ANALYZE data.
72724 sqlite3 *db = pParse->db;
72729 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
72735 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
72737 int iBindVar = pExpr->iColumn;
72738 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
72739 if( (v = pParse->pReprepare)!=0 ){
72742 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
72744 pVal->db = pParse->db;
72751 assert( pVal==0 || pVal->db==db );
72802 if( pExpr==0 || pExpr->op!=TK_SELECT ){
72813 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
72845 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
72887 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
72888 pMem->enc = ENC(db);
72900 int nCol = pRec->pKeyInfo->nAllField;
72901 Mem *aMem = pRec->aMem;
72906 sqlite3KeyInfoUnref(pRec->pKeyInfo);
72931 sqlite3DbFreeNN(((Mem*)v)->db, v);
72940 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
72944 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
72945 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
72946 return p->n;
72948 if( (p->flags & MEM_Blob)!=0 ){
72949 if( p->flags & MEM_Zero ){
72950 return p->n + p->u.nZero;
72952 return p->n;
72955 if( p->flags & MEM_Null ) return 0;
72982 sqlite3 *db = pParse->db;
72986 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
72987 p->db = db;
72988 if( db->pVdbe ){
72989 db->pVdbe->pPrev = p;
72991 p->pNext = db->pVdbe;
72992 p->pPrev = 0;
72993 db->pVdbe = p;
72994 p->magic = VDBE_MAGIC_INIT;
72995 p->pParse = pParse;
72996 pParse->pVdbe = p;
72997 assert( pParse->aLabel==0 );
72998 assert( pParse->nLabel==0 );
72999 assert( pParse->nOpAlloc==0 );
73000 assert( pParse->szOpAlloc==0 );
73010 sqlite3DbFree(p->db, p->zErrMsg);
73012 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
73021 p->prepFlags = prepFlags;
73023 p->expmask = 0;
73025 assert( p->zSql==0 );
73026 p->zSql = sqlite3DbStrNDup(p->db, z, n);
73035 assert( pA->db==pB->db );
73039 pTmp = pA->pNext;
73040 pA->pNext = pB->pNext;
73041 pB->pNext = pTmp;
73042 pTmp = pA->pPrev;
73043 pA->pPrev = pB->pPrev;
73044 pB->pPrev = pTmp;
73045 zTmp = pA->zSql;
73046 pA->zSql = pB->zSql;
73047 pB->zSql = zTmp;
73048 pB->expmask = pA->expmask;
73049 pB->prepFlags = pA->prepFlags;
73050 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
73051 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
73059 ** If an out-of-memory error occurs while resizing the array, return
73066 Parse *p = v->pParse;
73068 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
73076 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
73078 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
73083 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
73084 sqlite3OomFault(p->db);
73089 assert( nNew>=(p->nOpAlloc+nOp) );
73090 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
73092 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
73093 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
73094 v->aOp = pNew;
73127 assert( p->pParse->nOpAlloc<=p->nOp );
73129 assert( p->pParse->nOpAlloc>p->nOp );
73136 i = p->nOp;
73137 assert( p->magic==VDBE_MAGIC_INIT );
73139 if( p->pParse->nOpAlloc<=i ){
73142 p->nOp++;
73143 pOp = &p->aOp[i];
73144 pOp->opcode = (u8)op;
73145 pOp->p5 = 0;
73146 pOp->p1 = p1;
73147 pOp->p2 = p2;
73148 pOp->p3 = p3;
73149 pOp->p4.p = 0;
73150 pOp->p4type = P4_NOTUSED;
73152 pOp->zComment = 0;
73155 if( p->db->flags & SQLITE_VdbeAddopTrace ){
73157 Parse *pParse = p->pParse;
73158 for(jj=kk=0; jj<pParse->nColCache; jj++){
73159 struct yColCache *x = pParse->aColCache + jj;
73160 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
73164 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
73169 pOp->cycles = 0;
73170 pOp->cnt = 0;
73173 pOp->iSrcLine = 0;
73277 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
73292 if( p->db->mallocFailed==0 ){
73293 VdbeOp *pOp = &p->aOp[addr];
73294 pOp->p4type = P4_INT32;
73295 pOp->p4.i = p4;
73300 /* Insert the end of a co-routine
73306 ** co-routine has its own independent set of registers, because co-routines
73308 ** that could cause problems if two or more co-routines are using the same
73311 v->pParse->nTempReg = 0;
73312 v->pParse->nRangeReg = 0;
73324 ** always negative and P2 values are suppose to be non-negative.
73330 Parse *p = v->pParse;
73331 int i = p->nLabel++;
73332 assert( v->magic==VDBE_MAGIC_INIT );
73333 if( (i & (i-1))==0 ){
73334 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
73335 (i*2+1)*sizeof(p->aLabel[0]));
73337 if( p->aLabel ){
73338 p->aLabel[i] = -1;
73349 Parse *p = v->pParse;
73351 assert( v->magic==VDBE_MAGIC_INIT );
73352 assert( j<p->nLabel );
73354 if( p->aLabel ){
73355 p->aLabel[j] = v->nOp;
73363 p->runOnlyOnce = 1;
73370 p->runOnlyOnce = 0;
73377 ** in a Vdbe main program and each of the sub-programs (triggers) it may
73388 ** sqlite3DbFree(v->db, sIter.apSub);
73397 int iSub; /* 0 = main program, 1 = first sub-program etc. */
73400 Vdbe *v = p->v;
73405 if( p->iSub<=p->nSub ){
73407 if( p->iSub==0 ){
73408 aOp = v->aOp;
73409 nOp = v->nOp;
73411 aOp = p->apSub[p->iSub-1]->aOp;
73412 nOp = p->apSub[p->iSub-1]->nOp;
73414 assert( p->iAddr<nOp );
73416 pRet = &aOp[p->iAddr];
73417 p->iAddr++;
73418 if( p->iAddr==nOp ){
73419 p->iSub++;
73420 p->iAddr = 0;
73423 if( pRet->p4type==P4_SUBPROGRAM ){
73424 int nByte = (p->nSub+1)*sizeof(SubProgram*);
73426 for(j=0; j<p->nSub; j++){
73427 if( p->apSub[j]==pRet->p4.pProgram ) break;
73429 if( j==p->nSub ){
73430 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
73431 if( !p->apSub ){
73434 p->apSub[p->nSub++] = pRet->p4.pProgram;
73447 ** sub-programs contains any of the following:
73463 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
73476 int opcode = pOp->opcode;
73479 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
73484 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
73487 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
73492 sqlite3DbFree(v->db, sIter.apSub);
73499 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
73502 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
73528 Parse *pParse = p->pParse;
73529 int *aLabel = pParse->aLabel;
73530 p->readOnly = 1;
73531 p->bIsReader = 0;
73532 pOp = &p->aOp[p->nOp-1];
73541 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
73544 switch( pOp->opcode ){
73546 if( pOp->p2!=0 ) p->readOnly = 0;
73551 p->bIsReader = 1;
73559 p->readOnly = 0;
73560 p->bIsReader = 1;
73566 pOp->p4.xAdvance = sqlite3BtreeNext;
73567 pOp->p4type = P4_ADVANCE;
73571 assert( pOp->p2>=0 );
73576 pOp->p4.xAdvance = sqlite3BtreePrevious;
73577 pOp->p4type = P4_ADVANCE;
73581 assert( pOp->p2>=0 );
73586 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
73591 assert( (pOp - p->aOp) >= 3 );
73592 assert( pOp[-1].opcode==OP_Integer );
73593 n = pOp[-1].p1;
73599 if( pOp->p2<0 ){
73601 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
73602 ** have non-negative values for P2. */
73603 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
73604 assert( ADDR(pOp->p2)<pParse->nLabel );
73605 pOp->p2 = aLabel[ADDR(pOp->p2)];
73611 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
73612 ** have non-negative values for P2. */
73613 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
73615 if( pOp==p->aOp ) break;
73616 pOp--;
73618 sqlite3DbFree(p->db, pParse->aLabel);
73619 pParse->aLabel = 0;
73620 pParse->nLabel = 0;
73622 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
73629 assert( p->magic==VDBE_MAGIC_INIT );
73630 return p->nOp;
73639 ** sqlite3VdbeAddOpList() will always be non-NULL.
73643 assert( p->nOp + N <= p->pParse->nOpAlloc );
73657 for(i=0; i<p->nOp; i++){
73658 assert( p->aOp[i].opcode!=OP_ResultRow );
73675 VdbeOp *aOp = p->aOp;
73676 assert( aOp && !p->db->mallocFailed );
73679 assert( DbMaskAllZero(p->btreeMask) );
73682 *pnOp = p->nOp;
73683 p->aOp = 0;
73691 ** Non-zero P2 arguments to jump instructions are automatically adjusted
73698 int iLineno /* Source-file line number of first opcode */
73703 assert( p->magic==VDBE_MAGIC_INIT );
73704 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
73707 pFirst = pOut = &p->aOp[p->nOp];
73709 pOut->opcode = aOp->opcode;
73710 pOut->p1 = aOp->p1;
73711 pOut->p2 = aOp->p2;
73712 assert( aOp->p2>=0 );
73713 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
73714 pOut->p2 += p->nOp;
73716 pOut->p3 = aOp->p3;
73717 pOut->p4type = P4_NOTUSED;
73718 pOut->p4.p = 0;
73719 pOut->p5 = 0;
73721 pOut->zComment = 0;
73724 pOut->iSrcLine = iLineno+i;
73729 if( p->db->flags & SQLITE_VdbeAddopTrace ){
73730 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
73734 p->nOp += nOp;
73750 int nByte = (p->nScan+1) * sizeof(ScanStatus);
73752 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
73754 ScanStatus *pNew = &aNew[p->nScan++];
73755 pNew->addrExplain = addrExplain;
73756 pNew->addrLoop = addrLoop;
73757 pNew->addrVisit = addrVisit;
73758 pNew->nEst = nEst;
73759 pNew->zName = sqlite3DbStrDup(p->db, zName);
73760 p->aScan = aNew;
73771 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
73774 sqlite3VdbeGetOp(p,addr)->p1 = val;
73777 sqlite3VdbeGetOp(p,addr)->p2 = val;
73780 sqlite3VdbeGetOp(p,addr)->p3 = val;
73783 assert( p->nOp>0 || p->db->mallocFailed );
73784 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
73792 sqlite3VdbeChangeP2(p, addr, p->nOp);
73801 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
73812 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
73816 freeEphemeralFunction(db, p->pFunc);
73835 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
73849 if( db->pnBytesFreed==0 ){
73857 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
73871 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
73872 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
73874 sqlite3DbFree(db, pOp->zComment);
73883 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
73887 p->pNext = pVdbe->pProgram;
73888 pVdbe->pProgram = p;
73896 if( p->db->mallocFailed ) return 0;
73897 assert( addr>=0 && addr<p->nOp );
73898 pOp = &p->aOp[addr];
73899 freeP4(p->db, pOp->p4type, pOp->p4.p);
73900 pOp->p4type = P4_NOTUSED;
73901 pOp->p4.z = 0;
73902 pOp->opcode = OP_Noop;
73911 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
73912 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
73941 if( pOp->p4type ){
73942 freeP4(p->db, pOp->p4type, pOp->p4.p);
73943 pOp->p4type = 0;
73944 pOp->p4.p = 0;
73947 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
73950 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
73951 pOp->p4type = P4_DYNAMIC;
73958 db = p->db;
73959 assert( p->magic==VDBE_MAGIC_INIT );
73960 assert( p->aOp!=0 || db->mallocFailed );
73961 if( db->mallocFailed ){
73965 assert( p->nOp>0 );
73966 assert( addr<p->nOp );
73968 addr = p->nOp - 1;
73970 pOp = &p->aOp[addr];
73971 if( n>=0 || pOp->p4type ){
73978 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
73979 pOp->p4type = P4_INT32;
73982 pOp->p4.p = (void*)zP4;
73983 pOp->p4type = (signed char)n;
73990 ** to the value defined by the arguments. This is a high-speed
74001 if( p->db->mallocFailed ){
74002 freeP4(p->db, n, pP4);
74005 assert( p->nOp>0 );
74006 pOp = &p->aOp[p->nOp-1];
74007 assert( pOp->p4type==P4_NOTUSED );
74008 pOp->p4type = n;
74009 pOp->p4.p = pP4;
74018 Vdbe *v = pParse->pVdbe;
74029 ** insert a No-op and add the comment to that new instruction. This
74034 assert( p->nOp>0 || p->aOp==0 );
74035 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
74036 if( p->nOp ){
74037 assert( p->aOp );
74038 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
74039 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
74066 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
74071 ** Return the opcode for a given address. If the address is -1, then
74087 assert( p->magic==VDBE_MAGIC_INIT );
74089 addr = p->nOp - 1;
74091 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
74092 if( p->db->mallocFailed ){
74095 return &p->aOp[addr];
74105 if( c=='1' ) return pOp->p1;
74106 if( c=='2' ) return pOp->p2;
74107 if( c=='3' ) return pOp->p3;
74108 if( c=='4' ) return pOp->p4.i;
74109 return pOp->p5;
74120 ** "PX" -> "r[X]"
74121 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
74122 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
74123 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
74136 zOpName = sqlite3OpcodeName(pOp->opcode);
74143 if( pOp->p5 & SQLITE_STOREP2 ){
74150 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
74154 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
74156 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
74161 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
74171 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
74173 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
74182 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
74183 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
74187 }else if( pOp->zComment ){
74188 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
74205 switch( pExpr->op ){
74207 sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
74210 sqlite3XPrintf(p, "%d", pExpr->u.iValue);
74216 sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
74220 if( pExpr->iColumn<0 ){
74223 sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
74261 displayP4Expr(p, pExpr->pLeft);
74262 if( pExpr->pRight ){
74264 displayP4Expr(p, pExpr->pRight);
74282 switch( pOp->p4type ){
74285 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
74286 assert( pKeyInfo->aSortOrder!=0 );
74287 sqlite3XPrintf(&x, "k(%d", pKeyInfo->nKeyField);
74288 for(j=0; j<pKeyInfo->nKeyField; j++){
74289 CollSeq *pColl = pKeyInfo->aColl[j];
74290 const char *zColl = pColl ? pColl->zName : "";
74292 sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
74299 displayP4Expr(&x, pOp->p4.pExpr);
74304 CollSeq *pColl = pOp->p4.pColl;
74305 sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
74309 FuncDef *pDef = pOp->p4.pFunc;
74310 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
74315 FuncDef *pDef = pOp->p4.pCtx->pFunc;
74316 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
74321 sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
74325 sqlite3XPrintf(&x, "%d", pOp->p4.i);
74329 sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
74333 Mem *pMem = pOp->p4.pMem;
74334 if( pMem->flags & MEM_Str ){
74335 zP4 = pMem->z;
74336 }else if( pMem->flags & MEM_Int ){
74337 sqlite3XPrintf(&x, "%lld", pMem->u.i);
74338 }else if( pMem->flags & MEM_Real ){
74339 sqlite3XPrintf(&x, "%.16g", pMem->u.r);
74340 }else if( pMem->flags & MEM_Null ){
74343 assert( pMem->flags & MEM_Blob );
74350 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
74357 int *ai = pOp->p4.ai;
74377 sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
74381 zP4 = pOp->p4.z;
74395 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
74399 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
74400 ** p->btreeMask of databases that will require a lock.
74403 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
74404 assert( i<(int)sizeof(p->btreeMask)*8 );
74405 DbMaskSet(p->btreeMask, i);
74406 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
74407 DbMaskSet(p->lockMask, i);
74413 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
74417 ** that the correct busy-handler callback is invoked if required.
74419 ** If SQLite is not threadsafe but does support shared-cache mode, then
74424 ** If SQLite is not threadsafe and does not support shared-cache mode, this
74425 ** function is a no-op.
74427 ** The p->btreeMask field is a bitmask of all btrees that the prepared
74428 ** statement p will ever use. Let N be the number of bits in p->btreeMask
74438 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
74439 db = p->db;
74440 aDb = db->aDb;
74441 nDb = db->nDb;
74443 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
74459 db = p->db;
74460 aDb = db->aDb;
74461 nDb = db->nDb;
74463 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
74469 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
74482 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
74494 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
74505 while( (N--)>0 ){
74506 p->db = db;
74507 p->flags = flags;
74508 p->szMalloc = 0;
74510 p->pScopyFrom = 0;
74522 sqlite3 *db = p->db;
74523 if( db->pnBytesFreed ){
74525 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
74539 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
74540 ** sqlite3MemRelease() were called from here. With -O2, this jumps
74545 testcase( p->flags & MEM_Agg );
74546 testcase( p->flags & MEM_Dyn );
74547 testcase( p->flags & MEM_Frame );
74548 testcase( p->flags & MEM_RowSet );
74549 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
74551 }else if( p->szMalloc ){
74552 sqlite3DbFreeNN(db, p->zMalloc);
74553 p->szMalloc = 0;
74556 p->flags = MEM_Undefined;
74568 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
74569 for(i=0; i<p->nChildCsr; i++){
74570 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
74572 releaseMemArray(aMem, p->nChildMem);
74573 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
74574 sqlite3DbFree(p->v->db, p);
74585 ** When p->explain==1, each instruction is listed. When
74586 ** p->explain==2, only OP_Explain instructions are listed and these
74587 ** are shown in a different format. p->explain==2 is used to implement
74590 ** When p->explain==1, first the main program is listed, then each of
74597 int nSub = 0; /* Number of sub-vdbes seen so far */
74598 SubProgram **apSub = 0; /* Array of sub-vdbes */
74600 sqlite3 *db = p->db; /* The database connection */
74603 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
74604 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
74607 assert( p->explain );
74608 assert( p->magic==VDBE_MAGIC_RUN );
74609 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
74613 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
74616 p->pResultSet = 0;
74618 if( p->rc==SQLITE_NOMEM ){
74630 ** encountered, but p->pc will eventually catch up to nRow.
74632 nRow = p->nOp;
74638 assert( p->nMem>9 );
74639 pSub = &p->aMem[9];
74640 if( pSub->flags&MEM_Blob ){
74643 nSub = pSub->n/sizeof(Vdbe*);
74644 apSub = (SubProgram **)pSub->z;
74647 nRow += apSub[i]->nOp;
74652 i = p->pc++;
74654 p->rc = SQLITE_OK;
74658 if( i<p->nOp ){
74661 pOp = &p->aOp[i];
74666 i -= p->nOp;
74667 for(j=0; i>=apSub[j]->nOp; j++){
74668 i -= apSub[j]->nOp;
74670 pOp = &apSub[j]->aOp[i];
74675 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
74678 if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
74682 if( apSub[j]==pOp->p4.pProgram ) break;
74685 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
74686 if( p->rc!=SQLITE_OK ){
74690 apSub = (SubProgram **)pSub->z;
74691 apSub[nSub++] = pOp->p4.pProgram;
74692 pSub->flags |= MEM_Blob;
74693 pSub->n = nSub*sizeof(SubProgram*);
74694 nRow += pOp->p4.pProgram->nOp;
74697 }while( p->explain==2 && pOp->opcode!=OP_Explain );
74700 if( db->u1.isInterrupted ){
74701 p->rc = SQLITE_INTERRUPT;
74703 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
74706 if( p->explain==1 ){
74707 pMem->flags = MEM_Int;
74708 pMem->u.i = i; /* Program counter */
74711 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
74712 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
74713 assert( pMem->z!=0 );
74714 pMem->n = sqlite3Strlen30(pMem->z);
74715 pMem->enc = SQLITE_UTF8;
74719 pMem->flags = MEM_Int;
74720 pMem->u.i = pOp->p1; /* P1 */
74723 pMem->flags = MEM_Int;
74724 pMem->u.i = pOp->p2; /* P2 */
74727 pMem->flags = MEM_Int;
74728 pMem->u.i = pOp->p3; /* P3 */
74732 assert( p->db->mallocFailed );
74735 pMem->flags = MEM_Str|MEM_Term;
74736 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
74737 if( zP4!=pMem->z ){
74738 pMem->n = 0;
74739 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
74741 assert( pMem->z!=0 );
74742 pMem->n = sqlite3Strlen30(pMem->z);
74743 pMem->enc = SQLITE_UTF8;
74747 if( p->explain==1 ){
74749 assert( p->db->mallocFailed );
74752 pMem->flags = MEM_Str|MEM_Term;
74753 pMem->n = 2;
74754 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
74755 pMem->enc = SQLITE_UTF8;
74760 assert( p->db->mallocFailed );
74763 pMem->flags = MEM_Str|MEM_Term;
74764 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
74765 pMem->enc = SQLITE_UTF8;
74767 pMem->flags = MEM_Null; /* Comment */
74771 p->nResColumn = 8 - 4*(p->explain-1);
74772 p->pResultSet = &p->aMem[1];
74773 p->rc = SQLITE_OK;
74787 if( p->zSql ){
74788 z = p->zSql;
74789 }else if( p->nOp>=1 ){
74790 const VdbeOp *pOp = &p->aOp[0];
74791 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
74792 z = pOp->p4.z;
74805 int nOp = p->nOp;
74809 pOp = &p->aOp[0];
74810 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
74813 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
74817 if( z[i-1]!=' ' ){
74840 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
74859 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
74862 if( nByte <= p->nFree ){
74863 p->nFree -= nByte;
74864 pBuf = &p->pSpace[p->nFree];
74866 p->nNeeded += nByte;
74882 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
74886 assert( p->nOp>0 );
74889 p->magic = VDBE_MAGIC_RUN;
74892 for(i=0; i<p->nMem; i++){
74893 assert( p->aMem[i].db==p->db );
74896 p->pc = -1;
74897 p->rc = SQLITE_OK;
74898 p->errorAction = OE_Abort;
74899 p->nChange = 0;
74900 p->cacheCtr = 1;
74901 p->minWriteFileFormat = 255;
74902 p->iStatement = 0;
74903 p->nFkConstraint = 0;
74905 for(i=0; i<p->nOp; i++){
74906 p->aOp[i].cnt = 0;
74907 p->aOp[i].cycles = 0;
74943 assert( p->nOp>0 );
74945 assert( p->magic==VDBE_MAGIC_INIT );
74946 assert( pParse==p->pParse );
74947 db = p->db;
74948 assert( db->mallocFailed==0 );
74949 nVar = pParse->nVar;
74950 nMem = pParse->nMem;
74951 nCursor = pParse->nTab;
74952 nArg = pParse->nMaxArg;
74966 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
74967 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
74969 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
74974 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
74975 if( pParse->explain && nMem<10 ){
74978 p->expired = 0;
74986 ** This two-pass approach that reuses as much memory as possible from
74992 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
74993 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
74994 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
74995 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
74997 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
75000 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
75002 }while( !db->mallocFailed );
75004 p->pVList = pParse->pVList;
75005 pParse->pVList = 0;
75006 p->explain = pParse->explain;
75007 if( db->mallocFailed ){
75008 p->nVar = 0;
75009 p->nCursor = 0;
75010 p->nMem = 0;
75012 p->nCursor = nCursor;
75013 p->nVar = (ynVar)nVar;
75014 initMemArray(p->aVar, nVar, db, MEM_Null);
75015 p->nMem = nMem;
75016 initMemArray(p->aMem, nMem, db, MEM_Undefined);
75017 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
75019 memset(p->anExec, 0, p->nOp*sizeof(i64));
75033 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
75034 switch( pCx->eCurType ){
75036 sqlite3VdbeSorterClose(p->db, pCx);
75040 if( pCx->isEphemeral ){
75041 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
75042 /* The pCx->pCursor will be close automatically, if it exists, by
75045 assert( pCx->uc.pCursor!=0 );
75046 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
75052 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
75053 const sqlite3_module *pModule = pVCur->pVtab->pModule;
75054 assert( pVCur->pVtab->nRef>0 );
75055 pVCur->pVtab->nRef--;
75056 pModule->xClose(pVCur);
75067 if( p->apCsr ){
75069 for(i=0; i<p->nCursor; i++){
75070 VdbeCursor *pC = p->apCsr[i];
75073 p->apCsr[i] = 0;
75081 ** is used, for example, when a trigger sub-program is halted to restore
75085 Vdbe *v = pFrame->v;
75088 v->anExec = pFrame->anExec;
75090 v->aOp = pFrame->aOp;
75091 v->nOp = pFrame->nOp;
75092 v->aMem = pFrame->aMem;
75093 v->nMem = pFrame->nMem;
75094 v->apCsr = pFrame->apCsr;
75095 v->nCursor = pFrame->nCursor;
75096 v->db->lastRowid = pFrame->lastRowid;
75097 v->nChange = pFrame->nChange;
75098 v->db->nChange = pFrame->nDbChange;
75099 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
75100 v->pAuxData = pFrame->pAuxData;
75101 pFrame->pAuxData = 0;
75102 return pFrame->pc;
75114 if( p->pFrame ){
75116 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
75118 p->pFrame = 0;
75119 p->nFrame = 0;
75121 assert( p->nFrame==0 );
75123 if( p->aMem ){
75124 releaseMemArray(p->aMem, p->nMem);
75126 while( p->pDelFrame ){
75127 VdbeFrame *pDel = p->pDelFrame;
75128 p->pDelFrame = pDel->pParent;
75133 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
75134 assert( p->pAuxData==0 );
75145 sqlite3 *db = p->db;
75147 if( p->nResColumn ){
75148 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
75149 sqlite3DbFree(db, p->aColName);
75152 p->nResColumn = (u16)nResColumn;
75153 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
75154 if( p->aColName==0 ) return;
75155 initMemArray(p->aColName, n, db, MEM_Null);
75177 assert( idx<p->nResColumn );
75179 if( p->db->mallocFailed ){
75183 assert( p->aColName!=0 );
75184 pColName = &(p->aColName[idx+var*p->nResColumn]);
75185 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
75186 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
75193 ** write-transaction spanning more than one database file, this routine
75198 int nTrans = 0; /* Number of databases with an active write-transaction
75199 ** that are candidates for a two-phase commit using a
75200 ** master-journal */
75225 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
75226 Btree *pBt = db->aDb[i].pBt;
75243 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
75258 /* If there are any write-transactions at all, invoke the commit hook */
75259 if( needXcommit && db->xCommitCallback ){
75260 rc = db->xCommitCallback(db->pCommitArg);
75266 /* The simple case - no more than one database file (not counting the
75268 ** master-journal.
75272 ** that case we do not support atomic multi-file commits, so use the
75275 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
75278 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
75279 Btree *pBt = db->aDb[i].pBt;
75290 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
75291 Btree *pBt = db->aDb[i].pBt;
75301 /* The complex case - There is a multi-file write-transaction active.
75307 sqlite3_vfs *pVfs = db->pVfs;
75308 char *zMaster = 0; /* File-name for the master journal */
75309 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
75318 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
75333 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
75337 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
75359 for(i=0; i<db->nDb; i++){
75360 Btree *pBt = db->aDb[i].pBt;
75400 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
75401 Btree *pBt = db->aDb[i].pBt;
75433 for(i=0; i<db->nDb; i++){
75434 Btree *pBt = db->aDb[i].pBt;
75453 ** This is an internal self-check only - it is not an essential processing
75456 ** This is a no-op if NDEBUG is defined.
75464 p = db->pVdbe;
75468 if( p->readOnly==0 ) nWrite++;
75469 if( p->bIsReader ) nRead++;
75471 p = p->pNext;
75473 assert( cnt==db->nVdbeActive );
75474 assert( nWrite==db->nVdbeWrite );
75475 assert( nRead==db->nVdbeRead );
75482 ** If the Vdbe passed as the first argument opened a statement-transaction,
75492 sqlite3 *const db = p->db;
75495 const int iSavepoint = p->iStatement-1;
75498 assert( db->nStatement>0 );
75499 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
75501 for(i=0; i<db->nDb; i++){
75503 Btree *pBt = db->aDb[i].pBt;
75516 db->nStatement--;
75517 p->iStatement = 0;
75532 db->nDeferredCons = p->nStmtDefCons;
75533 db->nDeferredImmCons = p->nStmtDefImmCons;
75538 if( p->db->nStatement && p->iStatement ){
75557 sqlite3 *db = p->db;
75558 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
75559 || (!deferred && p->nFkConstraint>0)
75561 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
75562 p->errorAction = OE_Abort;
75585 sqlite3 *db = p->db;
75603 if( p->magic!=VDBE_MAGIC_RUN ){
75606 if( db->mallocFailed ){
75607 p->rc = SQLITE_NOMEM_BKPT;
75614 if( p->pc>=0 && p->bIsReader ){
75615 int mrc; /* Primary error code from p->rc */
75623 mrc = p->rc & 0xff;
75627 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
75632 ** Even if the statement is read-only, it is important to perform
75634 ** occurred while writing to the journal, sub-journal or database
75639 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
75640 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
75648 db->autoCommit = 1;
75649 p->nChange = 0;
75655 if( p->rc==SQLITE_OK ){
75659 /* If the auto-commit flag is set and this is the only active writer
75666 && db->autoCommit
75667 && db->nVdbeWrite==(p->readOnly==0)
75669 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
75672 if( NEVER(p->readOnly) ){
75678 /* The auto-commit flag is true, the vdbe program was successful
75684 if( rc==SQLITE_BUSY && p->readOnly ){
75688 p->rc = rc;
75690 p->nChange = 0;
75692 db->nDeferredCons = 0;
75693 db->nDeferredImmCons = 0;
75694 db->flags &= ~SQLITE_DeferFKs;
75699 p->nChange = 0;
75701 db->nStatement = 0;
75703 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
75705 }else if( p->errorAction==OE_Abort ){
75710 db->autoCommit = 1;
75711 p->nChange = 0;
75715 /* If eStatementOp is non-zero, then a statement transaction needs to
75724 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
75725 p->rc = rc;
75726 sqlite3DbFree(db, p->zErrMsg);
75727 p->zErrMsg = 0;
75731 db->autoCommit = 1;
75732 p->nChange = 0;
75737 ** has been rolled back, update the database connection change-counter.
75739 if( p->changeCntOn ){
75741 sqlite3VdbeSetChanges(db, p->nChange);
75745 p->nChange = 0;
75753 if( p->pc>=0 ){
75754 db->nVdbeActive--;
75755 if( !p->readOnly ) db->nVdbeWrite--;
75756 if( p->bIsReader ) db->nVdbeRead--;
75757 assert( db->nVdbeActive>=db->nVdbeRead );
75758 assert( db->nVdbeRead>=db->nVdbeWrite );
75759 assert( db->nVdbeWrite>=0 );
75761 p->magic = VDBE_MAGIC_HALT;
75763 if( db->mallocFailed ){
75764 p->rc = SQLITE_NOMEM_BKPT;
75767 /* If the auto-commit flag is set to true, then any locks that were held
75769 ** to invoke any required unlock-notify callbacks.
75771 if( db->autoCommit ){
75775 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
75776 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
75782 ** in p->rc. This routine sets that result back to SQLITE_OK.
75785 p->rc = SQLITE_OK;
75797 sqlite3 *db = p->db;
75798 int rc = p->rc;
75799 if( p->zErrMsg ){
75800 db->bBenignMalloc++;
75802 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
75803 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
75805 db->bBenignMalloc--;
75806 }else if( db->pErr ){
75807 sqlite3ValueSetNull(db->pErr);
75809 db->errCode = rc;
75819 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
75820 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
75821 assert( v->db->init.busy==0 );
75824 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
75826 sqlite3DbFree(v->db, zExpanded);
75851 db = p->db;
75864 if( p->pc>=0 ){
75867 if( p->runOnlyOnce ) p->expired = 1;
75868 }else if( p->rc && p->expired ){
75873 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
75881 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
75882 if( p->aMem ){
75883 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
75886 sqlite3DbFree(db, p->zErrMsg);
75887 p->zErrMsg = 0;
75888 p->pResultSet = 0;
75896 fprintf(out, "---- ");
75897 for(i=0; i<p->nOp; i++){
75898 fprintf(out, "%02x", p->aOp[i].opcode);
75901 if( p->zSql ){
75903 fprintf(out, "-- ");
75904 for(i=0; (c = p->zSql[i])!=0; i++){
75905 if( pc=='\n' ) fprintf(out, "-- ");
75911 for(i=0; i<p->nOp; i++){
75914 p->aOp[i].cnt,
75915 p->aOp[i].cycles,
75916 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
75919 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
75925 p->magic = VDBE_MAGIC_RESET;
75926 return p->rc & db->errMask;
75935 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
75937 assert( (rc & p->db->errMask)==rc );
75963 || (pAux->iAuxOp==iOp
75964 && pAux->iAuxArg>=0
75965 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
75967 testcase( pAux->iAuxArg==31 );
75968 if( pAux->xDeleteAux ){
75969 pAux->xDeleteAux(pAux->pAux);
75971 *pp = pAux->pNextAux;
75974 pp= &pAux->pNextAux;
75989 assert( p->db==0 || p->db==db );
75990 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
75991 for(pSub=p->pProgram; pSub; pSub=pNext){
75992 pNext = pSub->pNext;
75993 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
75996 if( p->magic!=VDBE_MAGIC_INIT ){
75997 releaseMemArray(p->aVar, p->nVar);
75998 sqlite3DbFree(db, p->pVList);
75999 sqlite3DbFree(db, p->pFree);
76001 vdbeFreeOpArray(db, p->aOp, p->nOp);
76002 sqlite3DbFree(db, p->aColName);
76003 sqlite3DbFree(db, p->zSql);
76007 for(i=0; i<p->nScan; i++){
76008 sqlite3DbFree(db, p->aScan[i].zName);
76010 sqlite3DbFree(db, p->aScan);
76022 db = p->db;
76023 assert( sqlite3_mutex_held(db->mutex) );
76025 if( p->pPrev ){
76026 p->pPrev->pNext = p->pNext;
76028 assert( db->pVdbe==p );
76029 db->pVdbe = p->pNext;
76031 if( p->pNext ){
76032 p->pNext->pPrev = p->pPrev;
76034 p->magic = VDBE_MAGIC_DEAD;
76035 p->db = 0;
76049 assert( p->deferredMoveto );
76050 assert( p->isTable );
76051 assert( p->eCurType==CURTYPE_BTREE );
76052 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
76058 p->deferredMoveto = 0;
76059 p->cacheStatus = CACHE_STALE;
76072 assert( p->eCurType==CURTYPE_BTREE );
76073 assert( p->uc.pCursor!=0 );
76074 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
76075 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
76076 p->cacheStatus = CACHE_STALE;
76077 if( isDifferentRow ) p->nullRow = 1;
76086 assert( p->eCurType==CURTYPE_BTREE );
76087 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
76104 ** not been deleted out from under the cursor, then this routine is a no-op.
76108 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
76109 if( p->deferredMoveto ){
76111 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
76112 *pp = p->pAltCursor;
76113 *piCol = iMap - 1;
76118 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
76135 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
76142 ** serial-type and data blob separately.
76147 ** -------------- --------------- ---------------
76159 ** N>=12 and even (N-12)/2 BLOB
76160 ** N>=13 and odd (N-13)/2 text
76167 ** Return the serial-type for the value stored in pMem.
76170 int flags = pMem->flags;
76180 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
76181 i64 i = pMem->u.i;
76208 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
76209 assert( pMem->n>=0 );
76210 n = (u32)pMem->n;
76212 n += pMem->u.nZero;
76239 ** Return the length of the data corresponding to the supplied serial-type.
76243 return (serial_type-12)/2;
76246 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
76256 ** If we are on an architecture with mixed-endian floating
76260 ** For most architectures, this is a no-op.
76262 ** (later): It is reported to me that the mixed-endian problem
76264 ** that early versions of GCC stored the two words of a 64-bit
76272 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
76276 ** (2007-08-30) Frank van Vugt has studied this problem closely
76279 ** emulation that uses only 32-bit mantissas instead of a full
76280 ** 48-bits as required by the IEEE standard. (This is the
76283 ** the necessary byte swapping is carried out using a 64-bit integer
76284 ** rather than a 64-bit float. Frank assures us that the code here
76315 ** of the pMem->u.nZero bytes for a MEM_Zero value.
76318 ** of bytes in the zero-filled tail is included in the return value only
76329 assert( sizeof(v)==sizeof(pMem->u.r) );
76330 memcpy(&v, &pMem->u.r, sizeof(v));
76333 v = pMem->u.i;
76338 buf[--i] = (u8)(v&0xFF);
76346 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
76348 len = pMem->n;
76349 if( len>0 ) memcpy(buf, pMem->z, len);
76358 ** big-endian integer. Return the equivalent native integer
76384 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
76385 ** twos-complement integer. */
76386 pMem->u.i = *(i64*)&x;
76387 pMem->flags = MEM_Int;
76388 testcase( pMem->u.i<0 );
76390 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
76395 ** defined that 64-bit floating point values really are mixed
76404 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
76406 memcpy(&pMem->u.r, &x, sizeof(x));
76407 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
76418 ** UPDATE no-change flag set */
76419 pMem->flags = MEM_Null|MEM_Zero;
76420 pMem->n = 0;
76421 pMem->u.nZero = 0;
76426 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
76427 pMem->flags = MEM_Null;
76431 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
76433 pMem->u.i = ONE_BYTE_INT(buf);
76434 pMem->flags = MEM_Int;
76435 testcase( pMem->u.i<0 );
76438 case 2: { /* 2-byte signed integer */
76439 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
76440 ** twos-complement integer. */
76441 pMem->u.i = TWO_BYTE_INT(buf);
76442 pMem->flags = MEM_Int;
76443 testcase( pMem->u.i<0 );
76446 case 3: { /* 3-byte signed integer */
76447 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
76448 ** twos-complement integer. */
76449 pMem->u.i = THREE_BYTE_INT(buf);
76450 pMem->flags = MEM_Int;
76451 testcase( pMem->u.i<0 );
76454 case 4: { /* 4-byte signed integer */
76455 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
76456 ** twos-complement integer. */
76457 pMem->u.i = FOUR_BYTE_INT(buf);
76459 /* Work around a sign-extension bug in the HP compiler for HP/UX */
76460 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
76462 pMem->flags = MEM_Int;
76463 testcase( pMem->u.i<0 );
76466 case 5: { /* 6-byte signed integer */
76467 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
76468 ** twos-complement integer. */
76469 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
76470 pMem->flags = MEM_Int;
76471 testcase( pMem->u.i<0 );
76474 case 6: /* 8-byte signed integer */
76482 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
76483 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
76484 pMem->u.i = serial_type-8;
76485 pMem->flags = MEM_Int;
76489 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
76491 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
76492 ** (N-13)/2 bytes in length. */
76494 pMem->z = (char *)buf;
76495 pMem->n = (serial_type-12)/2;
76496 pMem->flags = aFlag[serial_type&1];
76497 return pMem->n;
76521 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
76522 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
76524 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
76525 assert( pKeyInfo->aSortOrder!=0 );
76526 p->pKeyInfo = pKeyInfo;
76527 p->nField = pKeyInfo->nKeyField + 1;
76532 ** Given the nKey-byte encoding of a record in pKey[], populate the
76547 Mem *pMem = p->aMem;
76549 p->default_rc = 0;
76558 pMem->enc = pKeyInfo->enc;
76559 pMem->db = pKeyInfo->db;
76560 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
76561 pMem->szMalloc = 0;
76562 pMem->z = 0;
76565 if( (++u)>=p->nField ) break;
76567 assert( u<=pKeyInfo->nKeyField + 1 );
76568 p->nField = u;
76597 pKeyInfo = pPKey2->pKeyInfo;
76598 if( pKeyInfo->db==0 ) return 1;
76599 mem1.enc = pKeyInfo->enc;
76600 mem1.db = pKeyInfo->db;
76616 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
76617 assert( pKeyInfo->aSortOrder!=0 );
76618 assert( pKeyInfo->nKeyField>0 );
76644 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
76647 if( pKeyInfo->aSortOrder[i] ){
76648 rc = -rc; /* Invert the result for DESC sort order. */
76653 }while( idx1<szHdr1 && i<pPKey2->nField );
76664 rc = pPKey2->default_rc;
76671 if( pKeyInfo->db->mallocFailed ) return 1;
76680 ** limit given by pKeyInfo->nAllField.
76682 ** If this constraint is not satisfied, it means that the high-speed
76706 assert( nField <= pKeyInfo->nAllField );
76716 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
76724 if( pMem1->enc==pColl->enc ){
76727 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
76733 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
76734 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
76737 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
76738 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
76743 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
76753 ** with MEM_Zero. Return true if it could be a zero-blob.
76770 int n1 = pB1->n;
76771 int n2 = pB2->n;
76773 /* It is possible to have a Blob value that has some non-zero content
76777 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
76778 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
76780 if( (pB1->flags|pB2->flags) & MEM_Zero ){
76781 if( pB1->flags & pB2->flags & MEM_Zero ){
76782 return pB1->u.nZero - pB2->u.nZero;
76783 }else if( pB1->flags & MEM_Zero ){
76784 if( !isAllZero(pB2->z, pB2->n) ) return -1;
76785 return pB1->u.nZero - n2;
76787 if( !isAllZero(pB1->z, pB1->n) ) return +1;
76788 return n1 - pB2->u.nZero;
76791 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
76793 return n1 - n2;
76797 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
76804 if( x<r ) return -1;
76810 if( r<-9223372036854775808.0 ) return +1;
76811 if( r>9223372036854775807.0 ) return -1;
76813 if( i<y ) return -1;
76815 if( y==SMALLEST_INT64 && r>0.0 ) return -1;
76819 if( s<r ) return -1;
76838 f1 = pMem1->flags;
76839 f2 = pMem2->flags;
76847 return (f2&MEM_Null) - (f1&MEM_Null);
76854 if( pMem1->u.i < pMem2->u.i ) return -1;
76855 if( pMem1->u.i > pMem2->u.i ) return +1;
76859 if( pMem1->u.r < pMem2->u.r ) return -1;
76860 if( pMem1->u.r > pMem2->u.r ) return +1;
76865 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
76867 return -1;
76872 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
76874 return -1;
76888 return -1;
76891 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
76892 assert( pMem1->enc==SQLITE_UTF8 ||
76893 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
76899 assert( !pColl || pColl->xCmp );
76914 ** The first argument passed to this function is a serial-type that
76915 ** corresponds to an integer - all values between 1 and 9 inclusive
76951 return (serial_type - 8);
76963 ** If argument bSkip is non-zero, it is assumed that the caller has already
76967 ** fields that appear in both keys are equal, then pPKey2->default_rc is
76970 ** If database corruption is discovered, set pPKey2->errCode to
76972 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
76973 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
76985 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
76986 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
77004 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
77011 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
77013 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
77014 assert( pPKey2->pKeyInfo->nKeyField>0 );
77020 if( pRhs->flags & MEM_Int ){
77026 rc = -1;
77029 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
77032 i64 rhs = pRhs->u.i;
77034 rc = -1;
77042 else if( pRhs->flags & MEM_Real ){
77051 rc = -1;
77055 if( mem1.u.r<pRhs->u.r ){
77056 rc = -1;
77057 }else if( mem1.u.r>pRhs->u.r ){
77061 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
77067 else if( pRhs->flags & MEM_Str ){
77071 rc = -1;
77075 mem1.n = (serial_type - 12) / 2;
77079 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
77081 }else if( pKeyInfo->aColl[i] ){
77082 mem1.enc = pKeyInfo->enc;
77083 mem1.db = pKeyInfo->db;
77087 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
77090 int nCmp = MIN(mem1.n, pRhs->n);
77091 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
77092 if( rc==0 ) rc = mem1.n - pRhs->n;
77098 else if( pRhs->flags & MEM_Blob ){
77099 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
77103 rc = -1;
77105 int nStr = (serial_type - 12) / 2;
77109 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
77111 }else if( pRhs->flags & MEM_Zero ){
77115 rc = nStr - pRhs->u.nZero;
77118 int nCmp = MIN(nStr, pRhs->n);
77119 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
77120 if( rc==0 ) rc = nStr - pRhs->n;
77132 if( pKeyInfo->aSortOrder[i] ){
77133 rc = -rc;
77144 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
77155 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
77156 || pKeyInfo->db->mallocFailed
77158 pPKey2->eqSeen = 1;
77159 return pPKey2->default_rc;
77172 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
77190 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
77193 case 1: { /* 1-byte signed integer */
77198 case 2: { /* 2-byte signed integer */
77203 case 3: { /* 3-byte signed integer */
77208 case 4: { /* 4-byte signed integer */
77214 case 5: { /* 6-byte signed integer */
77219 case 6: { /* 8-byte signed integer */
77246 v = pPKey2->aMem[0].u.i;
77248 res = pPKey2->r1;
77250 res = pPKey2->r2;
77251 }else if( pPKey2->nField>1 ){
77257 ** fields. Return pPKey2->default_rc in this case. */
77258 res = pPKey2->default_rc;
77259 pPKey2->eqSeen = 1;
77269 ** uses the collation sequence BINARY and (c) that the size-of-header varint
77280 assert( pPKey2->aMem[0].flags & MEM_Str );
77281 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
77284 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
77286 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
77292 nStr = (serial_type-12) / 2;
77294 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
77297 nCmp = MIN( pPKey2->aMem[0].n, nStr );
77298 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
77301 res = nStr - pPKey2->aMem[0].n;
77303 if( pPKey2->nField>1 ){
77306 res = pPKey2->default_rc;
77307 pPKey2->eqSeen = 1;
77310 res = pPKey2->r2;
77312 res = pPKey2->r1;
77315 res = pPKey2->r2;
77317 res = pPKey2->r1;
77323 || pPKey2->pKeyInfo->db->mallocFailed
77335 ** that the size-of-header varint that occurs at the start of each record
77347 if( p->pKeyInfo->nAllField<=13 ){
77348 int flags = p->aMem[0].flags;
77349 if( p->pKeyInfo->aSortOrder[0] ){
77350 p->r1 = 1;
77351 p->r2 = -1;
77353 p->r1 = -1;
77354 p->r2 = 1;
77362 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
77388 ** than 2GiB are support - anything large must be database corruption.
77390 ** this code can safely assume that nCellKey is 32-bits
77411 /* The last field of the index should be an integer - the ROWID.
77413 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
77432 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
77467 assert( pC->eCurType==CURTYPE_BTREE );
77468 pCur = pC->uc.pCursor;
77492 assert( sqlite3_mutex_held(db->mutex) );
77493 db->nChange = nChange;
77494 db->nTotalChange += nChange;
77502 v->changeCntOn = 1;
77511 ** programs obsolete. Removing user-defined functions or collating
77517 for(p = db->pVdbe; p; p=p->pNext){
77518 p->expired = 1;
77526 return v->db;
77533 return v->prepFlags;
77547 Mem *pMem = &v->aVar[iVar-1];
77548 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
77549 if( 0==(pMem->flags & MEM_Null) ){
77550 sqlite3_value *pRet = sqlite3ValueNew(v->db);
77563 ** to sqlite3_reoptimize() that re-preparing the statement may result
77568 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
77570 v->expmask |= 0x80000000;
77572 v->expmask |= ((u32)1 << (iVar-1));
77581 ** throw an error if it is given inputs that would make it non-deterministic.
77582 ** This routine is invoked by date/time functions that use non-deterministic
77587 if( pCtx->pVdbe==0 ) return 1;
77589 if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
77591 "non-deterministic function in index expression or CHECK constraint",
77592 -1);
77605 if( pVtab->zErrMsg ){
77606 sqlite3 *db = p->db;
77607 sqlite3DbFree(db, p->zErrMsg);
77608 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
77609 sqlite3_free(pVtab->zErrMsg);
77610 pVtab->zErrMsg = 0;
77619 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
77629 Mem *pMem = &p->aMem[i];
77630 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
77639 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
77645 Vdbe *v, /* Vdbe pre-update hook is invoked by */
77653 sqlite3 *db = v->db;
77656 const char *zTbl = pTab->zName;
77659 assert( db->pPreUpdate==0 );
77666 iKey2 = v->aMem[iReg].u.i;
77672 assert( pCsr->nField==pTab->nCol
77673 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
77682 preupdate.keyinfo.nKeyField = pTab->nCol;
77688 db->pPreUpdate = &preupdate;
77689 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
77690 db->pPreUpdate = 0;
77696 for(i=0; i<pCsr->nField; i++){
77726 ** Return TRUE (non-zero) of the statement supplied as an argument needs
77735 return p==0 || p->expired;
77745 if( p->db==0 ){
77769 assert( p->startTime>0 );
77770 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
77771 assert( db->init.busy==0 );
77772 assert( p->zSql!=0 );
77773 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
77774 iElapse = (iNow - p->startTime)*1000000;
77775 if( db->xProfile ){
77776 db->xProfile(db->pProfileArg, p->zSql, iElapse);
77778 if( db->mTrace & SQLITE_TRACE_PROFILE ){
77779 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
77781 p->startTime = 0;
77788 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
77790 # define checkProfileCallback(DB,P) /*no-op*/
77805 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
77806 ** pointer is a harmless no-op. */
77810 sqlite3 *db = v->db;
77812 sqlite3_mutex_enter(db->mutex);
77835 sqlite3 *db = v->db;
77836 sqlite3_mutex_enter(db->mutex);
77840 assert( (rc & (db->errMask))==rc );
77842 sqlite3_mutex_leave(db->mutex);
77855 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
77858 for(i=0; i<p->nVar; i++){
77859 sqlite3VdbeMemRelease(&p->aVar[i]);
77860 p->aVar[i].flags = MEM_Null;
77862 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
77863 if( p->expmask ){
77864 p->expired = 1;
77877 if( p->flags & (MEM_Blob|MEM_Str) ){
77879 assert( p->flags==MEM_Null && p->z==0 );
77882 p->flags |= MEM_Blob;
77883 return p->n ? p->z : 0;
77905 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
77909 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
77912 && p->eSubtype=='p'
77913 && strcmp(p->u.zPType, zPType)==0
77915 return (void*)p->z;
77934 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
77935 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
77973 return aType[pVal->flags&MEM_AffMask];
77978 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
77990 pNew->flags &= ~MEM_Dyn;
77991 pNew->db = 0;
77992 if( pNew->flags&(MEM_Str|MEM_Blob) ){
77993 pNew->flags &= ~(MEM_Static|MEM_Dyn);
77994 pNew->flags |= MEM_Ephem;
78012 ** The following routines are used by user-defined functions to specify
78029 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
78056 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78065 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78074 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78075 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
78078 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78079 pCtx->isError = SQLITE_ERROR;
78080 pCtx->fErrorOrAux = 1;
78081 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
78085 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78086 pCtx->isError = SQLITE_ERROR;
78087 pCtx->fErrorOrAux = 1;
78088 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
78092 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78093 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
78096 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78097 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
78100 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78101 sqlite3VdbeMemSetNull(pCtx->pOut);
78109 Mem *pOut = pCtx->pOut;
78110 assert( sqlite3_mutex_held(pOut->db->mutex) );
78112 pOut->flags = MEM_Null;
78116 Mem *pOut = pCtx->pOut;
78117 assert( sqlite3_mutex_held(pOut->db->mutex) );
78118 pOut->eSubtype = eSubtype & 0xff;
78119 pOut->flags |= MEM_Subtype;
78127 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78137 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78153 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78162 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78171 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78176 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78177 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
78180 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78181 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
78184 Mem *pOut = pCtx->pOut;
78185 assert( sqlite3_mutex_held(pOut->db->mutex) );
78186 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
78189 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
78193 pCtx->isError = errCode;
78194 pCtx->fErrorOrAux = 1;
78196 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
78198 if( pCtx->pOut->flags & MEM_Null ){
78199 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
78206 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78207 pCtx->isError = SQLITE_TOOBIG;
78208 pCtx->fErrorOrAux = 1;
78209 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
78215 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78216 sqlite3VdbeMemSetNull(pCtx->pOut);
78217 pCtx->isError = SQLITE_NOMEM_BKPT;
78218 pCtx->fErrorOrAux = 1;
78219 sqlite3OomFault(pCtx->pOut->db);
78230 for(i=0; i<db->nDb; i++){
78231 Btree *pBt = db->aDb[i].pBt;
78237 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
78238 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
78261 if( p->magic!=VDBE_MAGIC_RUN ){
78266 ** This "automatic-reset" change is not technically an incompatibility,
78272 ** returns, and those were broken by the automatic-reset change. As a
78273 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
78279 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
78290 db = p->db;
78291 if( db->mallocFailed ){
78292 p->rc = SQLITE_NOMEM;
78296 if( p->pc<=0 && p->expired ){
78297 p->rc = SQLITE_SCHEMA;
78301 if( p->pc<0 ){
78306 if( db->nVdbeActive==0 ){
78307 db->u1.isInterrupted = 0;
78310 assert( db->nVdbeWrite>0 || db->autoCommit==0
78311 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
78315 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
78316 && !db->init.busy && p->zSql ){
78317 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
78319 assert( p->startTime==0 );
78323 db->nVdbeActive++;
78324 if( p->readOnly==0 ) db->nVdbeWrite++;
78325 if( p->bIsReader ) db->nVdbeRead++;
78326 p->pc = 0;
78329 p->rcApp = SQLITE_OK;
78332 if( p->explain ){
78337 db->nVdbeExec++;
78339 db->nVdbeExec--;
78347 if( rc==SQLITE_DONE && db->autoCommit ){
78348 assert( p->rc==SQLITE_OK );
78349 p->rc = doWalCallbacks(db);
78350 if( p->rc!=SQLITE_OK ){
78355 db->errCode = rc;
78356 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
78357 p->rc = SQLITE_NOMEM_BKPT;
78363 ** be one of the values in the first assert() below. Variable p->rc
78370 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
78371 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
78376 ** error has occurred, then return the error code in p->rc to the
78381 return (rc&db->errMask);
78385 ** This is the top-level implementation of sqlite3_step(). Call
78398 db = v->db;
78399 sqlite3_mutex_enter(db->mutex);
78400 v->doingRerun = 0;
78403 int savedPc = v->pc;
78414 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
78415 sqlite3DbFree(db, v->zErrMsg);
78416 if( !db->mallocFailed ){
78417 v->zErrMsg = sqlite3DbStrDup(db, zErr);
78418 v->rc = rc = sqlite3ApiExit(db, rc);
78420 v->zErrMsg = 0;
78421 v->rc = rc = SQLITE_NOMEM_BKPT;
78426 if( savedPc>=0 ) v->doingRerun = 1;
78427 assert( v->expired==0 );
78429 sqlite3_mutex_leave(db->mutex);
78439 assert( p && p->pFunc );
78440 return p->pFunc->pUserData;
78447 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
78454 assert( p && p->pOut );
78455 return p->pOut->db;
78469 ** performance by substituting a NULL result, or some other light-weight
78474 return sqlite3_value_nochange(p->pOut);
78487 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
78488 assert( p->pVdbe!=0 );
78491 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
78494 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
78513 const char *zName = context->pFunc->zName;
78518 sqlite3_result_error(context, zErr, -1);
78524 ** its pMem->z element.
78527 Mem *pMem = p->pMem;
78528 assert( (pMem->flags & MEM_Agg)==0 );
78531 pMem->z = 0;
78534 pMem->flags = MEM_Agg;
78535 pMem->u.pDef = p->pFunc;
78536 if( pMem->z ){
78537 memset(pMem->z, 0, nByte);
78540 return (void*)pMem->z;
78549 assert( p && p->pFunc && p->pFunc->xFinalize );
78550 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
78552 if( (p->pMem->flags & MEM_Agg)==0 ){
78555 return (void*)p->pMem->z;
78561 ** the user-function defined by pCtx.
78563 ** The left-most argument is 0.
78572 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78574 if( pCtx->pVdbe==0 ) return 0;
78576 assert( pCtx->pVdbe!=0 );
78578 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
78579 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
78580 return pAuxData->pAux;
78588 ** argument to the user-function defined by pCtx. Any previous value is
78591 ** The left-most argument is 0.
78604 Vdbe *pVdbe = pCtx->pVdbe;
78606 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78613 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
78614 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
78619 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
78621 pAuxData->iAuxOp = pCtx->iOp;
78622 pAuxData->iAuxArg = iArg;
78623 pAuxData->pNextAux = pVdbe->pAuxData;
78624 pVdbe->pAuxData = pAuxData;
78625 if( pCtx->fErrorOrAux==0 ){
78626 pCtx->isError = 0;
78627 pCtx->fErrorOrAux = 1;
78629 }else if( pAuxData->xDeleteAux ){
78630 pAuxData->xDeleteAux(pAuxData->pAux);
78633 pAuxData->pAux = pAux;
78634 pAuxData->xDeleteAux = xDelete;
78654 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
78655 return p->pMem->n;
78664 return pVm ? pVm->nResColumn : 0;
78673 if( pVm==0 || pVm->pResultSet==0 ) return 0;
78674 return pVm->nResColumn;
78683 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
78684 ** instead of an 8-byte one. This all works fine, except that when
78686 ** that a Mem structure is located on an 8-byte boundary. To prevent
78688 ** using gcc, we force nullMem to be 8-byte aligned using the magical
78726 assert( pVm->db );
78727 sqlite3_mutex_enter(pVm->db->mutex);
78728 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
78729 pOut = &pVm->pResultSet[i];
78731 sqlite3Error(pVm->db, SQLITE_RANGE);
78764 assert( p->db!=0 );
78765 assert( sqlite3_mutex_held(p->db->mutex) );
78766 p->rc = sqlite3ApiExit(p->db, p->rc);
78767 sqlite3_mutex_leave(p->db->mutex);
78817 if( pOut->flags&MEM_Static ){
78818 pOut->flags &= ~MEM_Static;
78819 pOut->flags |= MEM_Ephem;
78838 ** Convert the N-th element of pStmt->pColName[] into a string using
78871 db = p->db;
78876 sqlite3_mutex_enter(db->mutex);
78877 assert( db->mallocFailed==0 );
78878 ret = xFunc(&p->aColName[N]);
78882 if( db->mallocFailed ){
78886 sqlite3_mutex_leave(db->mutex);
78995 ** The error code stored in database p->db is overwritten with the return
79003 sqlite3_mutex_enter(p->db->mutex);
79004 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
79005 sqlite3Error(p->db, SQLITE_MISUSE);
79006 sqlite3_mutex_leave(p->db->mutex);
79008 "bind on a busy prepared statement: [%s]", p->zSql);
79011 if( i<1 || i>p->nVar ){
79012 sqlite3Error(p->db, SQLITE_RANGE);
79013 sqlite3_mutex_leave(p->db->mutex);
79016 i--;
79017 pVar = &p->aVar[i];
79019 pVar->flags = MEM_Null;
79020 sqlite3Error(p->db, SQLITE_OK);
79025 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
79031 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
79032 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
79033 p->expired = 1;
79056 pVar = &p->aVar[i-1];
79059 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
79062 sqlite3Error(p->db, rc);
79063 rc = sqlite3ApiExit(p->db, rc);
79066 sqlite3_mutex_leave(p->db->mutex);
79108 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
79109 sqlite3_mutex_leave(p->db->mutex);
79121 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
79122 sqlite3_mutex_leave(p->db->mutex);
79131 sqlite3_mutex_leave(p->db->mutex);
79146 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
79147 sqlite3_mutex_leave(p->db->mutex);
79193 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
79197 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
79201 if( pValue->flags & MEM_Zero ){
79202 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
79204 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
79209 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
79210 pValue->enc);
79225 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
79226 sqlite3_mutex_leave(p->db->mutex);
79233 sqlite3_mutex_enter(p->db->mutex);
79234 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
79240 rc = sqlite3ApiExit(p->db, rc);
79241 sqlite3_mutex_leave(p->db->mutex);
79251 return p ? p->nVar : 0;
79258 ** The result is always UTF-8.
79263 return sqlite3VListNumToName(p->pVList, i);
79273 return sqlite3VListNameToNum(p->pVList, zName, nName);
79286 assert( pTo->db==pFrom->db );
79287 assert( pTo->nVar==pFrom->nVar );
79288 sqlite3_mutex_enter(pTo->db->mutex);
79289 for(i=0; i<pFrom->nVar; i++){
79290 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
79292 sqlite3_mutex_leave(pTo->db->mutex);
79312 if( pFrom->nVar!=pTo->nVar ){
79315 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
79316 if( pTo->expmask ){
79317 pTo->expired = 1;
79319 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
79320 if( pFrom->expmask ){
79321 pFrom->expired = 1;
79334 return pStmt ? ((Vdbe*)pStmt)->db : 0;
79342 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
79350 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
79367 sqlite3_mutex_enter(pDb->mutex);
79369 pNext = (sqlite3_stmt*)pDb->pVdbe;
79371 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
79373 sqlite3_mutex_leave(pDb->mutex);
79390 sqlite3 *db = pVdbe->db;
79391 sqlite3_mutex_enter(db->mutex);
79393 db->pnBytesFreed = (int*)&v;
79396 db->pnBytesFreed = 0;
79397 sqlite3_mutex_leave(db->mutex);
79399 v = pVdbe->aCounter[op];
79400 if( resetFlag ) pVdbe->aCounter[op] = 0;
79410 return p ? p->zSql : 0;
79430 sqlite3_mutex_enter(p->db->mutex);
79432 sqlite3_mutex_leave(p->db->mutex);
79453 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
79460 ** This function is called from within a pre-update callback to retrieve
79464 PreUpdate *p = db->pPreUpdate;
79469 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
79470 if( !p || p->op==SQLITE_INSERT ){
79474 if( p->pPk ){
79475 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
79477 if( iIdx>=p->pCsr->nField || iIdx<0 ){
79483 if( p->pUnpacked==0 ){
79487 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
79490 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
79492 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
79493 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
79499 p->aRecord = aRec;
79502 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
79503 if( iIdx==p->pTab->iPKey ){
79504 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
79505 }else if( iIdx>=p->pUnpacked->nField ){
79507 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
79508 if( pMem->flags & MEM_Int ){
79521 ** This function is called from within a pre-update callback to retrieve
79525 PreUpdate *p = db->pPreUpdate;
79526 return (p ? p->keyinfo.nKeyField : 0);
79532 ** This function is designed to be called from within a pre-update callback
79536 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
79537 ** top-level trigger etc.).
79543 PreUpdate *p = db->pPreUpdate;
79544 return (p ? p->v->nFrame : 0);
79550 ** This function is called from within a pre-update callback to retrieve
79554 PreUpdate *p = db->pPreUpdate;
79558 if( !p || p->op==SQLITE_DELETE ){
79562 if( p->pPk && p->op!=SQLITE_UPDATE ){
79563 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
79565 if( iIdx>=p->pCsr->nField || iIdx<0 ){
79570 if( p->op==SQLITE_INSERT ){
79571 /* For an INSERT, memory cell p->iNewReg contains the serialized record
79573 UnpackedRecord *pUnpack = p->pNewUnpacked;
79575 Mem *pData = &p->v->aMem[p->iNewReg];
79578 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
79583 p->pNewUnpacked = pUnpack;
79585 pMem = &pUnpack->aMem[iIdx];
79586 if( iIdx==p->pTab->iPKey ){
79587 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
79588 }else if( iIdx>=pUnpack->nField ){
79592 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
79597 assert( p->op==SQLITE_UPDATE );
79598 if( !p->aNew ){
79599 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
79600 if( !p->aNew ){
79605 assert( iIdx>=0 && iIdx<p->pCsr->nField );
79606 pMem = &p->aNew[iIdx];
79607 if( pMem->flags==0 ){
79608 if( iIdx==p->pTab->iPKey ){
79609 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
79611 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
79636 if( idx<0 || idx>=p->nScan ) return 1;
79637 pScan = &p->aScan[idx];
79640 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
79644 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
79649 LogEst x = pScan->nEst;
79658 *(const char**)pOut = pScan->zName;
79662 if( pScan->addrExplain ){
79663 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
79670 if( pScan->addrExplain ){
79671 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
79673 *(int*)pOut = -1;
79689 memset(p->anExec, 0, p->nOp * sizeof(i64));
79710 ** The Vdbe parse-tree explainer is also found here.
79718 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
79743 ** This function returns a pointer to a nul-terminated string in memory
79747 ** then the returned string holds a copy of zRawSql with "-- " prepended
79753 ** with large (multi-megabyte) strings and blobs.
79763 ** parameter index is known, locate the value in p->aVar[]. Then render
79783 db = p->db;
79785 db->aLimit[SQLITE_LIMIT_LENGTH]);
79786 if( db->nVdbeExec>1 ){
79790 sqlite3StrAccumAppend(&out, "-- ", 3);
79791 assert( (zRawSql - zStart) > 0 );
79792 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
79794 }else if( p->nVar==0 ){
79823 assert( idx>0 && idx<=p->nVar );
79824 pVar = &p->aVar[idx-1];
79825 if( pVar->flags & MEM_Null ){
79827 }else if( pVar->flags & MEM_Int ){
79828 sqlite3XPrintf(&out, "%lld", pVar->u.i);
79829 }else if( pVar->flags & MEM_Real ){
79830 sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
79831 }else if( pVar->flags & MEM_Str ){
79838 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
79846 nOut = pVar->n;
79850 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
79853 sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
79855 if( nOut<pVar->n ){
79856 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
79862 }else if( pVar->flags & MEM_Zero ){
79863 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
79866 assert( pVar->flags & MEM_Blob );
79868 nOut = pVar->n;
79873 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
79877 if( nOut<pVar->n ){
79878 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
79967 ** use this information to make sure that the zero-blob functionality
79974 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
79975 sqlite3_max_blobsize = p->n;
79985 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
79987 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
80030 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
80047 ** already. Return non-zero if a malloc() fails.
80050 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
80065 if( ((P)->flags&MEM_Ephem)!=0 \
80069 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
80079 int iDb, /* Database the cursor belongs to, or -1 */
80097 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
80098 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
80100 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
80108 assert( iCur>=0 && iCur<p->nCursor );
80109 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
80110 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
80111 p->apCsr[iCur] = 0;
80114 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
80116 pCx->eCurType = eCurType;
80117 pCx->iDb = iDb;
80118 pCx->nField = nField;
80119 pCx->aOffset = &pCx->aType[nField];
80121 pCx->uc.pCursor = (BtCursor*)
80122 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
80123 sqlite3BtreeCursorZero(pCx->uc.pCursor);
80147 u8 enc = pRec->enc;
80148 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
80149 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
80150 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
80151 pRec->u.i = iValue;
80152 pRec->flags |= MEM_Int;
80154 pRec->u.r = rValue;
80155 pRec->flags |= MEM_Real;
80167 ** floating-point representation if an integer representation
80176 ** No-op. pRec is unchanged.
80186 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
80187 if( (pRec->flags & MEM_Real)==0 ){
80188 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
80199 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
80200 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
80204 pRec->flags &= ~(MEM_Real|MEM_Int);
80239 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
80243 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
80244 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
80245 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
80248 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
80258 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
80259 ** But it does set pMem->u.r and pMem->u.i appropriately.
80262 if( pMem->flags & (MEM_Int|MEM_Real) ){
80263 return pMem->flags & (MEM_Int|MEM_Real);
80265 if( pMem->flags & (MEM_Str|MEM_Blob) ){
80278 int f = pMem->flags;
80298 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
80300 for(i=0; i<16 && i<pMem->n; i++){
80301 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
80304 for(i=0; i<16 && i<pMem->n; i++){
80305 char z = pMem->z[i];
80311 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
80331 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
80334 for(j=0; j<15 && j<pMem->n; j++){
80335 u8 c = pMem->z[j];
80343 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
80355 if( p->flags & MEM_Undefined ){
80357 }else if( p->flags & MEM_Null ){
80358 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
80359 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
80360 printf(" si:%lld", p->u.i);
80361 }else if( p->flags & MEM_Int ){
80362 printf(" i:%lld", p->u.i);
80364 }else if( p->flags & MEM_Real ){
80365 printf(" r:%g", p->u.r);
80367 }else if( p->flags & MEM_RowSet ){
80374 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
80385 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
80395 ** high-performance timing routines.
80411 ** This file contains inline asm code for retrieving "high-performance"
80418 ** The following routine only works on pentium-class (or newer) processors.
80420 ** processor and returns that value. This can be used for high-res
80494 ** the number of non-transaction savepoints currently in the
80504 for(p=db->pSavepoint; p; p=p->pNext) n++;
80505 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
80511 ** Return the register of pOp->p2 after first preparing it to be
80516 pOut->flags = MEM_Int;
80521 assert( pOp->p2>0 );
80522 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
80523 pOut = &p->aMem[pOp->p2];
80525 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
80528 pOut->flags = MEM_Int;
80541 Op *aOp = p->aOp; /* Copy of p->aOp */
80550 sqlite3 *db = p->db; /* The database */
80558 Mem *aMem = p->aMem; /* Copy of p->aMem */
80568 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
80570 if( p->rc==SQLITE_NOMEM ){
80575 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
80576 assert( p->bIsReader || p->readOnly!=0 );
80577 p->iCurrentTime = 0;
80578 assert( p->explain==0 );
80579 p->pResultSet = 0;
80580 db->busyHandler.nBusy = 0;
80581 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
80584 if( db->xProgress ){
80585 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
80586 assert( 0 < db->nProgressOps );
80587 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
80594 if( p->pc==0
80595 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
80600 if( p->db->flags & SQLITE_VdbeListing ){
80602 for(i=0; i<p->nOp; i++){
80606 if( p->db->flags & SQLITE_VdbeEQP ){
80607 for(i=0; i<p->nOp; i++){
80615 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
80619 for(pOp=&aOp[p->pc]; 1; pOp++){
80624 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
80630 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
80636 if( db->flags & SQLITE_VdbeTrace ){
80637 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
80647 sqlite3_interrupt_count--;
80657 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
80659 assert( pOp->p1>0 );
80660 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
80661 assert( memIsValid(&aMem[pOp->p1]) );
80662 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
80663 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
80666 assert( pOp->p2>0 );
80667 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
80668 assert( memIsValid(&aMem[pOp->p2]) );
80669 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
80670 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
80673 assert( pOp->p3>0 );
80674 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
80675 assert( memIsValid(&aMem[pOp->p3]) );
80676 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
80677 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
80680 assert( pOp->p2>0 );
80681 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
80682 memAboutToChange(p, &aMem[pOp->p2]);
80685 assert( pOp->p3>0 );
80686 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
80687 memAboutToChange(p, &aMem[pOp->p3]);
80695 switch( pOp->opcode ){
80702 ** the switch statement will break with convention and be flush-left. Another
80740 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
80746 pOp = &aOp[pOp->p2 - 1];
80757 ** faster according to "valgrind --tool=cachegrind" */
80759 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
80764 ** If the progress callback returns non-zero, exit the virtual machine with
80767 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
80768 assert( db->nProgressOps!=0 );
80769 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
80770 if( db->xProgress(db->pProgressArg) ){
80786 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
80787 pIn1 = &aMem[pOp->p1];
80790 pIn1->flags = MEM_Int;
80791 pIn1->u.i = (int)(pOp-aOp);
80792 REGISTER_TRACE(pOp->p1, pIn1);
80797 pOp = &aOp[pOp->p2 - 1];
80807 pIn1 = &aMem[pOp->p1];
80808 assert( pIn1->flags==MEM_Int );
80809 pOp = &aOp[pIn1->u.i];
80810 pIn1->flags = MEM_Undefined;
80826 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
80827 assert( pOp->p2>=0 && pOp->p2<p->nOp );
80828 assert( pOp->p3>=0 && pOp->p3<p->nOp );
80829 pOut = &aMem[pOp->p1];
80831 pOut->u.i = pOp->p3 - 1;
80832 pOut->flags = MEM_Int;
80833 if( pOp->p2 ) goto jump_to_p2;
80847 pIn1 = &aMem[pOp->p1];
80848 assert( pIn1->flags==MEM_Int );
80849 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
80850 pCaller = &aOp[pIn1->u.i];
80851 assert( pCaller->opcode==OP_Yield );
80852 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
80853 pOp = &aOp[pCaller->p2 - 1];
80854 pIn1->flags = MEM_Undefined;
80873 pIn1 = &aMem[pOp->p1];
80875 pIn1->flags = MEM_Int;
80876 pcDest = (int)pIn1->u.i;
80877 pIn1->u.i = (int)(pOp - aOp);
80878 REGISTER_TRACE(pOp->p1, pIn1);
80888 ** value in register P3 is not NULL, then this routine is a no-op.
80892 pIn3 = &aMem[pOp->p3];
80893 if( (pIn3->flags & MEM_Null)==0 ) break;
80931 pcx = (int)(pOp - aOp);
80932 if( pOp->p1==SQLITE_OK && p->pFrame ){
80933 /* Halt the sub-program. Return control to the parent frame. */
80934 pFrame = p->pFrame;
80935 p->pFrame = pFrame->pParent;
80936 p->nFrame--;
80937 sqlite3VdbeSetChanges(db, p->nChange);
80939 if( pOp->p2==OE_Ignore ){
80940 /* Instruction pcx is the OP_Program that invoked the sub-program
80942 ** instruction is set to OE_Ignore, then the sub-program is throwing
80945 pcx = p->aOp[pcx].p2-1;
80947 aOp = p->aOp;
80948 aMem = p->aMem;
80952 p->rc = pOp->p1;
80953 p->errorAction = (u8)pOp->p2;
80954 p->pc = pcx;
80955 assert( pOp->p5<=4 );
80956 if( p->rc ){
80957 if( pOp->p5 ){
80960 testcase( pOp->p5==1 );
80961 testcase( pOp->p5==2 );
80962 testcase( pOp->p5==3 );
80963 testcase( pOp->p5==4 );
80964 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
80965 if( pOp->p4.z ){
80966 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
80969 sqlite3VdbeError(p, "%s", pOp->p4.z);
80971 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
80976 p->rc = SQLITE_BUSY;
80978 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
80979 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
80980 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
80988 ** The 32-bit integer value P1 is written into register P2.
80992 pOut->u.i = pOp->p1;
80999 ** P4 is a pointer to a 64-bit integer value.
81004 assert( pOp->p4.pI64!=0 );
81005 pOut->u.i = *pOp->p4.pI64;
81013 ** P4 is a pointer to a 64-bit floating point value.
81018 pOut->flags = MEM_Real;
81019 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
81020 pOut->u.r = *pOp->p4.pReal;
81028 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
81034 assert( pOp->p4.z!=0 );
81036 pOp->opcode = OP_String;
81037 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
81041 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
81044 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
81046 pOut->szMalloc = 0;
81047 pOut->flags |= MEM_Static;
81048 if( pOp->p4type==P4_DYNAMIC ){
81049 sqlite3DbFree(db, pOp->p4.z);
81051 pOp->p4type = P4_DYNAMIC;
81052 pOp->p4.z = pOut->z;
81053 pOp->p1 = pOut->n;
81057 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
81077 assert( pOp->p4.z!=0 );
81079 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
81080 pOut->z = pOp->p4.z;
81081 pOut->n = pOp->p1;
81082 pOut->enc = encoding;
81085 if( pOp->p3>0 ){
81086 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
81087 pIn3 = &aMem[pOp->p3];
81088 assert( pIn3->flags & MEM_Int );
81089 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
81103 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
81111 cnt = pOp->p3-pOp->p2;
81112 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
81113 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
81114 pOut->n = 0;
81119 pOut->flags = nullFlag;
81120 pOut->n = 0;
81121 cnt--;
81135 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
81136 pOut = &aMem[pOp->p1];
81137 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
81148 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
81150 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
81151 pOut->enc = encoding;
81167 assert( pOp->p1>0 && pOp->p1<=p->nVar );
81168 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
81169 pVar = &p->aVar[pOp->p1 - 1];
81173 pOut = &aMem[pOp->p2];
81182 ** Move the P3 values in register P1..P1+P3-1 over into
81183 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
81185 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
81193 n = pOp->p3;
81194 p1 = pOp->p1;
81195 p2 = pOp->p2;
81202 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
81203 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
81208 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
81209 pOut->pScopyFrom += pOp->p2 - p1;
81216 }while( --n );
81231 n = pOp->p3;
81232 pIn1 = &aMem[pOp->p1];
81233 pOut = &aMem[pOp->p2];
81239 pOut->pScopyFrom = 0;
81241 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
81242 if( (n--)==0 ) break;
81263 pIn1 = &aMem[pOp->p1];
81264 pOut = &aMem[pOp->p2];
81268 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
81282 pIn1 = &aMem[pOp->p1];
81283 assert( (pIn1->flags & MEM_Int)!=0 );
81284 pOut = &aMem[pOp->p2];
81285 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
81292 ** The registers P1 through P1+P2-1 contain a single row of
81295 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
81301 assert( p->nResColumn==pOp->p2 );
81302 assert( pOp->p1>0 );
81303 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
81308 if( db->xProgress!=0
81310 && db->xProgress(db->pProgressArg)!=0
81321 assert( db->flags&SQLITE_CountRows );
81322 assert( p->usesStmtJournal );
81333 ** ensure that statement-transactions are always nested, not overlapping.
81334 ** If the open statement-transaction is not closed here, then the user
81338 ** The statement transaction is never a top-level transaction. Hence
81341 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
81346 p->cacheCtr = (p->cacheCtr + 2)|1;
81349 ** and have an assigned type. The results are de-ephemeralized as
81352 pMem = p->pResultSet = &aMem[pOp->p1];
81353 for(i=0; i<pOp->p2; i++){
81359 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
81361 if( db->mallocFailed ) goto no_mem;
81363 if( db->mTrace & SQLITE_TRACE_ROW ){
81364 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
81369 p->pc = (int)(pOp - aOp) + 1;
81390 pIn1 = &aMem[pOp->p1];
81391 pIn2 = &aMem[pOp->p2];
81392 pOut = &aMem[pOp->p3];
81394 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
81401 nByte = pIn1->n + pIn2->n;
81402 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
81410 memcpy(pOut->z, pIn2->z, pIn2->n);
81412 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
81413 pOut->z[nByte]=0;
81414 pOut->z[nByte+1] = 0;
81415 pOut->flags |= MEM_Term;
81416 pOut->n = (int)nByte;
81417 pOut->enc = encoding;
81438 ** Synopsis: r[P3]=r[P2]-r[P1]
81474 pIn1 = &aMem[pOp->p1];
81476 pIn2 = &aMem[pOp->p2];
81478 pOut = &aMem[pOp->p3];
81479 flags = pIn1->flags | pIn2->flags;
81481 iA = pIn1->u.i;
81482 iB = pIn2->u.i;
81484 switch( pOp->opcode ){
81490 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
81496 if( iA==-1 ) iA = 1;
81501 pOut->u.i = iB;
81510 switch( pOp->opcode ){
81512 case OP_Subtract: rB -= rA; break;
81524 if( iA==-1 ) iA = 1;
81530 pOut->u.i = rB;
81536 pOut->u.r = rB;
81554 ** be returned. This is used by the built-in min(), max() and nullif()
81563 ** publicly. Only built-in functions have access to this feature.
81566 assert( pOp->p4type==P4_COLLSEQ );
81567 if( pOp->p1 ){
81568 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
81576 ** Take the bit-wise AND of the values in register P1 and P2 and
81583 ** Take the bit-wise OR of the values in register P1 and P2 and
81612 pIn1 = &aMem[pOp->p1];
81613 pIn2 = &aMem[pOp->p2];
81614 pOut = &aMem[pOp->p3];
81615 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
81621 op = pOp->opcode;
81632 op = 2*OP_ShiftLeft + 1 - op;
81633 iB = iB>(-64) ? -iB : 64;
81637 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
81644 /* Sign-extend on a right shift of a negative number */
81645 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
81650 pOut->u.i = iA;
81664 pIn1 = &aMem[pOp->p1];
81667 pIn1->u.i += pOp->p2;
81679 pIn1 = &aMem[pOp->p1];
81680 if( (pIn1->flags & MEM_Int)==0 ){
81682 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
81683 if( (pIn1->flags & MEM_Int)==0 ){
81684 if( pOp->p2==0 ){
81707 pIn1 = &aMem[pOp->p1];
81708 if( pIn1->flags & MEM_Int ){
81732 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
81733 testcase( pOp->p2==SQLITE_AFF_TEXT );
81734 testcase( pOp->p2==SQLITE_AFF_BLOB );
81735 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
81736 testcase( pOp->p2==SQLITE_AFF_INTEGER );
81737 testcase( pOp->p2==SQLITE_AFF_REAL );
81738 pIn1 = &aMem[pOp->p1];
81741 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
81755 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
81805 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
81852 u16 flags1; /* Copy of initial value of pIn1->flags */
81853 u16 flags3; /* Copy of initial value of pIn3->flags */
81855 pIn1 = &aMem[pOp->p1];
81856 pIn3 = &aMem[pOp->p3];
81857 flags1 = pIn1->flags;
81858 flags3 = pIn3->flags;
81861 if( pOp->p5 & SQLITE_NULLEQ ){
81866 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
81868 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
81881 if( pOp->p5 & SQLITE_STOREP2 ){
81882 pOut = &aMem[pOp->p2];
81886 REGISTER_TRACE(pOp->p2, pOut);
81889 if( pOp->p5 & SQLITE_JUMPIFNULL ){
81897 affinity = pOp->p5 & SQLITE_AFF_MASK;
81902 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
81903 flags3 = pIn3->flags;
81911 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
81912 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
81913 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
81919 testcase( pIn1->flags & MEM_Int );
81920 testcase( pIn1->flags & MEM_Real );
81922 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
81923 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
81927 testcase( pIn3->flags & MEM_Int );
81928 testcase( pIn3->flags & MEM_Real );
81930 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
81931 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
81934 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
81935 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
81948 res2 = aLTb[pOp->opcode - OP_Ne];
81951 res2 = aEQb[pOp->opcode - OP_Ne];
81954 res2 = aGTb[pOp->opcode - OP_Ne];
81958 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
81959 pIn1->flags = flags1;
81960 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
81961 pIn3->flags = flags3;
81963 if( pOp->p5 & SQLITE_STOREP2 ){
81964 pOut = &aMem[pOp->p2];
81966 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
81974 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
81976 testcase( res2==0 && pOp->opcode==OP_Eq );
81977 testcase( res2==1 && pOp->opcode==OP_Eq );
81978 testcase( res2==0 && pOp->opcode==OP_Ne );
81979 testcase( res2==1 && pOp->opcode==OP_Ne );
81980 if( (pOp->opcode==OP_Eq)==res2 ) break;
81984 pOut->u.i = res2;
81985 REGISTER_TRACE(pOp->p2, pOut);
81987 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
82005 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
82006 assert( pOp[-1].p5 & SQLITE_STOREP2 );
82026 assert( pOp->p4type==P4_INTARRAY );
82027 assert( pOp->p4.ai );
82034 ** Synopsis: r[P1@P3] <-> r[P2@P3]
82036 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
82037 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
82064 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
82068 assert( pOp[-1].opcode==OP_Permutation );
82069 assert( pOp[-1].p4type==P4_INTARRAY );
82070 aPermute = pOp[-1].p4.ai + 1;
82073 n = pOp->p3;
82074 pKeyInfo = pOp->p4.pKeyInfo;
82077 p1 = pOp->p1;
82078 p2 = pOp->p2;
82083 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
82084 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
82086 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
82087 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
82096 assert( i<pKeyInfo->nKeyField );
82097 pColl = pKeyInfo->aColl[i];
82098 bRev = pKeyInfo->aSortOrder[i];
82101 if( bRev ) iCompare = -iCompare;
82116 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
82118 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
82120 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
82150 pIn1 = &aMem[pOp->p1];
82151 if( pIn1->flags & MEM_Null ){
82156 pIn2 = &aMem[pOp->p2];
82157 if( pIn2->flags & MEM_Null ){
82162 if( pOp->opcode==OP_And ){
82169 pOut = &aMem[pOp->p3];
82173 pOut->u.i = v1;
82187 pIn1 = &aMem[pOp->p1];
82188 pOut = &aMem[pOp->p2];
82190 if( (pIn1->flags & MEM_Null)==0 ){
82191 pOut->flags = MEM_Int;
82192 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
82201 ** ones-complement of the P1 value into register P2. If P1 holds
82205 pIn1 = &aMem[pOp->p1];
82206 pOut = &aMem[pOp->p2];
82208 if( (pIn1->flags & MEM_Null)==0 ){
82209 pOut->flags = MEM_Int;
82210 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
82218 ** encountered on each invocation of the byte-code program. Jump to P2
82221 ** Top-level programs determine first invocation by comparing the P1
82229 ** because the self-altering code trick does not work for recursive
82234 assert( p->aOp[0].opcode==OP_Init );
82235 if( p->pFrame ){
82236 iAddr = (int)(pOp - p->aOp);
82237 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
82241 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
82243 if( p->aOp[0].p1==pOp->p1 ){
82249 pOp->p1 = p->aOp[0].p1;
82256 ** is considered true if it is numeric and non-zero. If the value
82257 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
82263 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
82268 pIn1 = &aMem[pOp->p1];
82269 if( pIn1->flags & MEM_Null ){
82270 c = pOp->p3;
82277 if( pOp->opcode==OP_IfNot ) c = !c;
82292 pIn1 = &aMem[pOp->p1];
82293 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
82294 if( (pIn1->flags & MEM_Null)!=0 ){
82306 pIn1 = &aMem[pOp->p1];
82307 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
82308 if( (pIn1->flags & MEM_Null)==0 ){
82323 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82324 assert( p->apCsr[pOp->p1]!=0 );
82325 if( p->apCsr[pOp->p1]->nullRow ){
82326 sqlite3VdbeMemSetNull(aMem + pOp->p3);
82346 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
82350 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82351 pC = p->apCsr[pOp->p1];
82352 pOut = &p->aMem[pOp->p3];
82353 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
82356 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
82367 ** information about the format of the data.) Extract the P2-th column
82377 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
82379 ** The first OP_Column against a pseudo-table after the value of the content
82391 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
82399 u64 offset64; /* 64-bit offset */
82403 pC = p->apCsr[pOp->p1];
82404 p2 = pOp->p2;
82407 ** the correct row) then bring it up-to-date by doing the necessary
82408 ** B-Tree seek. */
82412 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
82413 pDest = &aMem[pOp->p3];
82415 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82417 assert( p2<pC->nField );
82418 aOffset = pC->aOffset;
82419 assert( pC->eCurType!=CURTYPE_VTAB );
82420 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
82421 assert( pC->eCurType!=CURTYPE_SORTER );
82423 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
82424 if( pC->nullRow ){
82425 if( pC->eCurType==CURTYPE_PSEUDO ){
82426 /* For the special case of as pseudo-cursor, the seekResult field
82428 assert( pC->seekResult>0 );
82429 pReg = &aMem[pC->seekResult];
82430 assert( pReg->flags & MEM_Blob );
82432 pC->payloadSize = pC->szRow = pReg->n;
82433 pC->aRow = (u8*)pReg->z;
82439 pCrsr = pC->uc.pCursor;
82440 assert( pC->eCurType==CURTYPE_BTREE );
82443 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
82444 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
82445 assert( pC->szRow<=pC->payloadSize );
82446 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
82447 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
82451 pC->cacheStatus = p->cacheCtr;
82452 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
82453 pC->nHdrParsed = 0;
82456 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
82457 /* pC->aRow does not have to hold the entire row, but it does at least
82458 ** need to cover the header of the record. If pC->aRow does not contain
82461 pC->aRow = 0;
82462 pC->szRow = 0;
82470 ** 3-byte type for each of the maximum of 32768 columns plus three
82473 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
82478 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
82490 zData = pC->aRow;
82491 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
82498 ** parsed and valid information is in aOffset[] and pC->aType[].
82500 if( pC->nHdrParsed<=p2 ){
82502 ** to extract additional fields up through the p2+1-th field
82504 if( pC->iHdrOffset<aOffset[0] ){
82506 if( pC->aRow==0 ){
82508 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
82512 zData = pC->aRow;
82515 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
82517 i = pC->nHdrParsed;
82519 zHdr = zData + pC->iHdrOffset;
82530 pC->aType[i++] = t;
82539 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
82540 || (offset64 > pC->payloadSize)
82546 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
82551 pC->nHdrParsed = i;
82552 pC->iHdrOffset = (u32)(zHdr - zData);
82553 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
82562 if( pC->nHdrParsed<=p2 ){
82563 if( pOp->p4type==P4_MEM ){
82564 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
82571 t = pC->aType[p2];
82574 /* Extract the content for the p2+1-th column. Control can only
82575 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
82578 assert( p2<pC->nHdrParsed );
82584 assert( t==pC->aType[p2] );
82585 if( pC->szRow>=aOffset[p2+1] ){
82587 ** page - where the content is not on an overflow page */
82588 zData = pC->aRow + aOffset[p2];
82593 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
82597 pDest->n = len = (t-12)/2;
82598 pDest->enc = encoding;
82599 if( pDest->szMalloc < len+2 ){
82600 pDest->flags = MEM_Null;
82603 pDest->z = pDest->zMalloc;
82605 memcpy(pDest->z, zData, len);
82606 pDest->z[len] = 0;
82607 pDest->z[len+1] = 0;
82608 pDest->flags = aFlag[t&1];
82611 pDest->enc = encoding;
82613 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
82614 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
82631 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
82633 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
82634 pDest->flags &= ~MEM_Ephem;
82640 REGISTER_TRACE(pOp->p3, pDest);
82645 pOp = &aOp[aOp[0].p3-1];
82658 ** P4 is a string that is P2 characters long. The N-th character of the
82659 ** string indicates the column affinity that should be used for the N-th
82665 zAffinity = pOp->p4.z;
82667 assert( pOp->p2>0 );
82668 assert( zAffinity[pOp->p2]==0 );
82669 pIn1 = &aMem[pOp->p1];
82671 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
82686 ** P4 may be a string that is P2 characters long. The N-th character of the
82687 ** string indicates the column affinity that should be used for the N-th
82716 ** ------------------------------------------------------------------------
82717 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
82718 ** ------------------------------------------------------------------------
82725 ** hdr-size field is also a varint which is the offset from the beginning
82731 nField = pOp->p1;
82732 zAffinity = pOp->p4.z;
82733 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
82735 nField = pOp->p2;
82736 pLast = &pData0[nField-1];
82737 file_format = p->minWriteFileFormat;
82740 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
82741 pOut = &aMem[pOp->p3];
82758 ** have a non-NULL default value. Also, the record must be left with
82760 ** index of the right-most column with a non-NULL default value */
82761 if( pOp->p5 ){
82762 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
82763 pLast--;
82764 nField--;
82776 if( pRec->flags & MEM_Zero ){
82781 ** Give such values a special internal-use-only serial-type of 10
82784 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
82789 nZero += pRec->u.nZero;
82790 len -= pRec->u.nZero;
82797 pRec->uTemp = serial_type;
82799 pRec--;
82802 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
82818 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
82823 ** the new record. The output register (pOp->p3) is not allowed to
82830 zNewRecord = (u8 *)pOut->z;
82838 serial_type = pRec->uTemp;
82839 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
82842 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
82849 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
82850 pOut->n = (int)nByte;
82851 pOut->flags = MEM_Blob;
82853 pOut->u.nZero = nZero;
82854 pOut->flags |= MEM_Zero;
82856 REGISTER_TRACE(pOp->p3, pOut);
82872 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
82873 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
82879 pOut->u.i = nEntry;
82900 p1 = pOp->p1;
82901 zName = pOp->p4.z;
82906 assert( db->pSavepoint==0 || db->autoCommit==0 );
82908 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
82910 assert( p->bIsReader );
82913 if( db->nVdbeWrite>0 ){
82917 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
82926 ** that the db->aVTrans[] array is empty. */
82927 assert( db->autoCommit==0 || db->nVTrans==0 );
82929 db->nStatement+db->nSavepoint);
82936 pNew->zName = (char *)&pNew[1];
82937 memcpy(pNew->zName, zName, nName+1);
82941 if( db->autoCommit ){
82942 db->autoCommit = 0;
82943 db->isTransactionSavepoint = 1;
82945 db->nSavepoint++;
82949 pNew->pNext = db->pSavepoint;
82950 db->pSavepoint = pNew;
82951 pNew->nDeferredCons = db->nDeferredCons;
82952 pNew->nDeferredImmCons = db->nDeferredImmCons;
82961 pSavepoint = db->pSavepoint;
82962 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
82963 pSavepoint = pSavepoint->pNext
82970 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
82974 sqlite3VdbeError(p, "cannot release savepoint - "
82983 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
82988 db->autoCommit = 1;
82990 p->pc = (int)(pOp - aOp);
82991 db->autoCommit = 0;
82992 p->rc = rc = SQLITE_BUSY;
82995 db->isTransactionSavepoint = 0;
82996 rc = p->rc;
82999 iSavepoint = db->nSavepoint - iSavepoint - 1;
83001 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
83002 for(ii=0; ii<db->nDb; ii++){
83003 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
83011 for(ii=0; ii<db->nDb; ii++){
83012 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
83020 db->mDbFlags |= DBFLAG_SchemaChange;
83026 while( db->pSavepoint!=pSavepoint ){
83027 pTmp = db->pSavepoint;
83028 db->pSavepoint = pTmp->pNext;
83030 db->nSavepoint--;
83038 assert( pSavepoint==db->pSavepoint );
83039 db->pSavepoint = pSavepoint->pNext;
83042 db->nSavepoint--;
83045 db->nDeferredCons = pSavepoint->nDeferredCons;
83046 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
83062 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
83073 desiredAutoCommit = pOp->p1;
83074 iRollback = pOp->p2;
83077 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
83078 assert( p->bIsReader );
83080 if( desiredAutoCommit!=db->autoCommit ){
83084 db->autoCommit = 1;
83085 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
83089 sqlite3VdbeError(p, "cannot commit transaction - "
83096 db->autoCommit = (u8)desiredAutoCommit;
83099 p->pc = (int)(pOp - aOp);
83100 db->autoCommit = (u8)(1-desiredAutoCommit);
83101 p->rc = rc = SQLITE_BUSY;
83104 assert( db->nStatement==0 );
83106 if( p->rc==SQLITE_OK ){
83115 (iRollback)?"cannot rollback - no transaction is active":
83116 "cannot commit - no transaction is active"));
83128 ** If P2 is non-zero, then a write-transaction is started, or if a
83129 ** read-transaction is already active, it is upgraded to a write-transaction.
83130 ** If P2 is zero, then a read-transaction is started.
83137 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
83163 assert( p->bIsReader );
83164 assert( p->readOnly==0 || pOp->p2==0 );
83165 assert( pOp->p1>=0 && pOp->p1<db->nDb );
83166 assert( DbMaskTest(p->btreeMask, pOp->p1) );
83167 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
83171 pBt = db->aDb[pOp->p1].pBt;
83174 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
83179 p->pc = (int)(pOp - aOp);
83180 p->rc = rc;
83186 if( pOp->p2 && p->usesStmtJournal
83187 && (db->autoCommit==0 || db->nVdbeRead>1)
83190 if( p->iStatement==0 ){
83191 assert( db->nStatement>=0 && db->nSavepoint>=0 );
83192 db->nStatement++;
83193 p->iStatement = db->nSavepoint + db->nStatement;
83196 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
83198 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
83204 p->nStmtDefCons = db->nDeferredCons;
83205 p->nStmtDefImmCons = db->nDeferredImmCons;
83209 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
83214 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
83218 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
83219 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
83220 sqlite3DbFree(db, p->zErrMsg);
83221 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
83222 /* If the schema-cookie from the database file matches the cookie
83223 ** stored with the in-memory representation of the schema, do
83226 ** If virtual-tables are in use, this is not just an optimization.
83227 ** Often, v-tables store their data in other SQLite tables, which
83228 ** are queried from within xNext() and other v-table methods using
83229 ** prepared queries. If such a query is out-of-date, we do not want to
83231 ** v-table would have to be ready for the sqlite3_vtab structure itself
83233 ** a v-table method.
83235 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
83236 sqlite3ResetOneSchema(db, pOp->p1);
83238 p->expired = 1;
83253 ** There must be a read-lock on the database (either a transaction
83262 assert( p->bIsReader );
83263 iDb = pOp->p1;
83264 iCookie = pOp->p3;
83265 assert( pOp->p3<SQLITE_N_BTREE_META );
83266 assert( iDb>=0 && iDb<db->nDb );
83267 assert( db->aDb[iDb].pBt!=0 );
83268 assert( DbMaskTest(p->btreeMask, iDb) );
83270 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
83272 pOut->u.i = iMeta;
83288 assert( pOp->p2<SQLITE_N_BTREE_META );
83289 assert( pOp->p1>=0 && pOp->p1<db->nDb );
83290 assert( DbMaskTest(p->btreeMask, pOp->p1) );
83291 assert( p->readOnly==0 );
83292 pDb = &db->aDb[pOp->p1];
83293 assert( pDb->pBt!=0 );
83294 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
83296 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
83297 if( pOp->p2==BTREE_SCHEMA_VERSION ){
83299 pDb->pSchema->schema_cookie = pOp->p3;
83300 db->mDbFlags |= DBFLAG_SchemaChange;
83301 }else if( pOp->p2==BTREE_FILE_FORMAT ){
83303 pDb->pSchema->file_format = pOp->p3;
83305 if( pOp->p1==1 ){
83309 p->expired = 0;
83318 ** Open a read-only cursor for the database table whose root page is
83351 ** number of P2 and if it is this opcode becomes a no-op. In other words,
83375 ** in read/write mode. For a given table, there can be one or more read-only
83390 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
83391 assert( pOp->p4type==P4_KEYINFO );
83392 pCur = p->apCsr[pOp->p1];
83393 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
83394 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
83402 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
83403 assert( p->bIsReader );
83404 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
83405 || p->readOnly==0 );
83407 if( p->expired ){
83414 p2 = pOp->p2;
83415 iDb = pOp->p3;
83416 assert( iDb>=0 && iDb<db->nDb );
83417 assert( DbMaskTest(p->btreeMask, iDb) );
83418 pDb = &db->aDb[iDb];
83419 pX = pDb->pBt;
83421 if( pOp->opcode==OP_OpenWrite ){
83423 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
83425 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
83426 p->minWriteFileFormat = pDb->pSchema->file_format;
83431 if( pOp->p5 & OPFLAG_P2ISREG ){
83433 assert( p2<=(p->nMem+1 - p->nCursor) );
83436 assert( (pIn2->flags & MEM_Int)!=0 );
83438 p2 = (int)pIn2->u.i;
83445 if( pOp->p4type==P4_KEYINFO ){
83446 pKeyInfo = pOp->p4.pKeyInfo;
83447 assert( pKeyInfo->enc==ENC(db) );
83448 assert( pKeyInfo->db==db );
83449 nField = pKeyInfo->nAllField;
83450 }else if( pOp->p4type==P4_INT32 ){
83451 nField = pOp->p4.i;
83453 assert( pOp->p1>=0 );
83456 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
83458 pCur->nullRow = 1;
83459 pCur->isOrdered = 1;
83460 pCur->pgnoRoot = p2;
83462 pCur->wrFlag = wrFlag;
83464 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
83465 pCur->pKeyInfo = pKeyInfo;
83467 ** SQLite used to check if the root-page flags were sane at this point
83470 pCur->isTable = pOp->p4type!=P4_KEYINFO;
83475 testcase( pOp->p5 & OPFLAG_BULKCSR );
83477 testcase( pOp->p2 & OPFLAG_SEEKEQ );
83479 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
83480 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
83491 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
83497 pOrig = p->apCsr[pOp->p2];
83498 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
83500 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
83502 pCx->nullRow = 1;
83503 pCx->isEphemeral = 1;
83504 pCx->pKeyInfo = pOrig->pKeyInfo;
83505 pCx->isTable = pOrig->isTable;
83506 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
83507 pCx->pKeyInfo, pCx->uc.pCursor);
83521 ** the main database is read-only. The ephemeral
83553 assert( pOp->p1>=0 );
83554 assert( pOp->p2>=0 );
83555 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
83557 pCx->nullRow = 1;
83558 pCx->isEphemeral = 1;
83559 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
83560 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
83562 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
83568 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
83570 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
83572 assert( pOp->p4type==P4_KEYINFO );
83573 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
83576 assert( pKeyInfo->db==db );
83577 assert( pKeyInfo->enc==ENC(db) );
83578 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
83579 pKeyInfo, pCx->uc.pCursor);
83581 pCx->isTable = 0;
83583 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
83584 0, pCx->uc.pCursor);
83585 pCx->isTable = 1;
83589 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
83597 ** tables using an external merge-sort algorithm.
83599 ** If argument P3 is non-zero, then it indicates that the sorter may
83606 assert( pOp->p1>=0 );
83607 assert( pOp->p2>=0 );
83608 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
83610 pCx->pKeyInfo = pOp->p4.pKeyInfo;
83611 assert( pCx->pKeyInfo->db==db );
83612 assert( pCx->pKeyInfo->enc==ENC(db) );
83613 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
83627 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83628 pC = p->apCsr[pOp->p1];
83630 if( (pC->seqCount++)==0 ){
83644 ** A pseudo-table created by this opcode is used to hold a single
83647 ** is the only cursor opcode that works with a pseudo-table.
83650 ** the pseudo-table.
83655 assert( pOp->p1>=0 );
83656 assert( pOp->p3>=0 );
83657 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
83659 pCx->nullRow = 1;
83660 pCx->seekResult = pOp->p2;
83661 pCx->isTable = 1;
83662 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
83664 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
83666 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
83667 assert( pOp->p5==0 );
83674 ** currently open, this instruction is a no-op.
83677 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83678 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
83679 p->apCsr[pOp->p1] = 0;
83688 ** table or index for cursor P1 are used. P4 is a 64-bit integer
83691 ** by the cursor. The high-order bit is set if any column after
83696 pC = p->apCsr[pOp->p1];
83697 assert( pC->eCurType==CURTYPE_BTREE );
83698 pC->maskUsed = *(u64*)pOp->p4.pI64;
83706 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
83731 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
83749 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
83767 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
83801 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83802 assert( pOp->p2!=0 );
83803 pC = p->apCsr[pOp->p1];
83805 assert( pC->eCurType==CURTYPE_BTREE );
83809 assert( pC->isOrdered );
83810 assert( pC->uc.pCursor!=0 );
83811 oc = pOp->opcode;
83813 pC->nullRow = 0;
83815 pC->seekOp = pOp->opcode;
83818 if( pC->isTable ){
83820 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
83826 pIn3 = &aMem[pOp->p3];
83827 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
83834 if( (pIn3->flags & MEM_Int)==0 ){
83835 if( (pIn3->flags & MEM_Real)==0 ){
83846 ** (x > 4.9) -> (x >= 5)
83847 ** (x <= 4.9) -> (x < 5)
83849 if( pIn3->u.r<(double)iKey ){
83850 assert( OP_SeekGE==(OP_SeekGT-1) );
83851 assert( OP_SeekLT==(OP_SeekLE-1) );
83853 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
83858 else if( pIn3->u.r>(double)iKey ){
83865 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
83866 pC->movetoTarget = iKey; /* Used by OP_Delete */
83875 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
83877 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
83885 nField = pOp->p4.i;
83886 assert( pOp->p4type==P4_INT32 );
83888 r.pKeyInfo = pC->pKeyInfo;
83893 ** r.default_rc = -1;
83898 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
83899 assert( oc!=OP_SeekGT || r.default_rc==-1 );
83900 assert( oc!=OP_SeekLE || r.default_rc==-1 );
83904 r.aMem = &aMem[pOp->p3];
83909 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
83918 pC->deferredMoveto = 0;
83919 pC->cacheStatus = CACHE_STALE;
83926 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
83942 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
83955 res = sqlite3BtreeEof(pC->uc.pCursor);
83959 assert( pOp->p2>0 );
84015 ** record are not-NULL then a check is done to determine if any row in the
84042 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
84045 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84046 assert( pOp->p4type==P4_INT32 );
84047 pC = p->apCsr[pOp->p1];
84050 pC->seekOp = pOp->opcode;
84052 pIn3 = &aMem[pOp->p3];
84053 assert( pC->eCurType==CURTYPE_BTREE );
84054 assert( pC->uc.pCursor!=0 );
84055 assert( pC->isTable==0 );
84056 if( pOp->p4.i>0 ){
84057 r.pKeyInfo = pC->pKeyInfo;
84058 r.nField = (u16)pOp->p4.i;
84064 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
84070 assert( pIn3->flags & MEM_Blob );
84074 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
84076 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
84078 pIdxKey->default_rc = 0;
84080 if( pOp->opcode==OP_NoConflict ){
84084 for(ii=0; ii<pIdxKey->nField; ii++){
84085 if( pIdxKey->aMem[ii].flags & MEM_Null ){
84091 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
84096 pC->seekResult = res;
84098 pC->nullRow = 1-alreadyExists;
84099 pC->deferredMoveto = 0;
84100 pC->cacheStatus = CACHE_STALE;
84101 if( pOp->opcode==OP_Found ){
84127 ** (with arbitrary multi-value keys).
84146 ** P3 register to contain a non-integer value, in which case the jump is
84150 ** (with arbitrary multi-value keys).
84164 pIn3 = &aMem[pOp->p3];
84165 if( (pIn3->flags & MEM_Int)==0 ){
84167 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
84171 pIn3 = &aMem[pOp->p3];
84172 assert( pIn3->flags & MEM_Int );
84173 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84174 pC = p->apCsr[pOp->p1];
84177 pC->seekOp = 0;
84179 assert( pC->isTable );
84180 assert( pC->eCurType==CURTYPE_BTREE );
84181 pCrsr = pC->uc.pCursor;
84184 iKey = pIn3->u.i;
84187 pC->movetoTarget = iKey; /* Used by OP_Delete */
84188 pC->nullRow = 0;
84189 pC->cacheStatus = CACHE_STALE;
84190 pC->deferredMoveto = 0;
84192 pC->seekResult = res;
84195 if( pOp->p2==0 ){
84214 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84215 assert( p->apCsr[pOp->p1]!=0 );
84216 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
84218 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
84249 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84250 pC = p->apCsr[pOp->p1];
84252 assert( pC->eCurType==CURTYPE_BTREE );
84253 assert( pC->uc.pCursor!=0 );
84256 ** thing) is obtained in a two-step algorithm.
84268 assert( pC->isTable );
84280 if( !pC->useRandomRowid ){
84281 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
84286 v = 1; /* IMP: R-61914-48074 */
84288 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
84289 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
84291 pC->useRandomRowid = 1;
84293 v++; /* IMP: R-29538-34987 */
84299 if( pOp->p3 ){
84301 assert( pOp->p3>0 );
84302 if( p->pFrame ){
84303 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
84305 assert( pOp->p3<=pFrame->nMem );
84306 pMem = &pFrame->aMem[pOp->p3];
84309 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
84310 pMem = &aMem[pOp->p3];
84315 REGISTER_TRACE(pOp->p3, pMem);
84317 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
84318 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
84319 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
84322 if( v<pMem->u.i+1 ){
84323 v = pMem->u.i + 1;
84325 pMem->u.i = v;
84328 if( pC->useRandomRowid ){
84329 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
84333 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
84339 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
84345 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
84348 assert( v>0 ); /* EV: R-40812-03570 */
84350 pC->deferredMoveto = 0;
84351 pC->cacheStatus = CACHE_STALE;
84353 pOut->u.i = v;
84382 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
84385 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
84386 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
84406 const char *zDb; /* database name - used by the update hook */
84407 Table *pTab; /* Table structure - used by update and pre-update hooks */
84410 pData = &aMem[pOp->p2];
84411 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84413 pC = p->apCsr[pOp->p1];
84415 assert( pC->eCurType==CURTYPE_BTREE );
84416 assert( pC->uc.pCursor!=0 );
84417 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
84418 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
84419 REGISTER_TRACE(pOp->p2, pData);
84421 if( pOp->opcode==OP_Insert ){
84422 pKey = &aMem[pOp->p3];
84423 assert( pKey->flags & MEM_Int );
84425 REGISTER_TRACE(pOp->p3, pKey);
84426 x.nKey = pKey->u.i;
84428 assert( pOp->opcode==OP_InsertInt );
84429 x.nKey = pOp->p3;
84432 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
84433 assert( pC->iDb>=0 );
84434 zDb = db->aDb[pC->iDb].zDbSName;
84435 pTab = pOp->p4.pTab;
84436 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
84443 /* Invoke the pre-update hook, if any */
84445 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
84446 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
84448 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
84449 /* Prevent post-update hook from running in cases when it should not */
84453 if( pOp->p5 & OPFLAG_ISNOOP ) break;
84456 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
84457 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
84458 assert( pData->flags & (MEM_Blob|MEM_Str) );
84459 x.pData = pData->z;
84460 x.nData = pData->n;
84461 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
84462 if( pData->flags & MEM_Zero ){
84463 x.nZero = pData->u.nZero;
84468 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
84469 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
84471 pC->deferredMoveto = 0;
84472 pC->cacheStatus = CACHE_STALE;
84474 /* Invoke the update-hook if required. */
84477 assert( db->xUpdateCallback!=0 );
84478 assert( pTab->aCol!=0 );
84479 db->xUpdateCallback(db->pUpdateArg,
84480 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
84481 zDb, pTab->zName, x.nKey);
84493 ** the next Next instruction will be a no-op. As a result, in this case
84507 ** P1 must not be pseudo-table. It has to be a real table with
84511 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
84513 ** this case. Specifically, if one is configured, the pre-update hook is
84514 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
84527 opflags = pOp->p2;
84528 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84529 pC = p->apCsr[pOp->p1];
84531 assert( pC->eCurType==CURTYPE_BTREE );
84532 assert( pC->uc.pCursor!=0 );
84533 assert( pC->deferredMoveto==0 );
84536 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
84538 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
84540 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
84541 assert( pC->movetoTarget==iKey );
84545 /* If the update-hook or pre-update-hook will be invoked, set zDb to
84550 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
84551 assert( pC->iDb>=0 );
84552 assert( pOp->p4.pTab!=0 );
84553 zDb = db->aDb[pC->iDb].zDbSName;
84554 pTab = pOp->p4.pTab;
84555 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
84556 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
84564 /* Invoke the pre-update-hook if required. */
84565 if( db->xPreUpdateCallback && pOp->p4.pTab ){
84568 || (aMem[pOp->p3].flags & MEM_Int)
84572 zDb, pTab, pC->movetoTarget,
84573 pOp->p3
84580 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
84585 if( p->pFrame==0 ){
84586 if( pC->isEphemeral==0
84587 && (pOp->p5 & OPFLAG_AUXDELETE)==0
84588 && (pC->wrFlag & OPFLAG_FORDELETE)==0
84592 if( pOp->p2 & OPFLAG_NCHANGE ){
84593 nExtraDelete--;
84598 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
84599 pC->cacheStatus = CACHE_STALE;
84600 pC->seekResult = 0;
84603 /* Invoke the update-hook if required. */
84605 p->nChange++;
84606 if( db->xUpdateCallback && HasRowid(pTab) ){
84607 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
84608 pC->movetoTarget);
84609 assert( pC->iDb>=0 );
84623 sqlite3VdbeSetChanges(db, p->nChange);
84624 p->nChange = 0;
84648 pC = p->apCsr[pOp->p1];
84650 assert( pOp->p4type==P4_INT32 );
84651 pIn3 = &aMem[pOp->p3];
84652 nKeyCol = pOp->p4.i;
84668 ** a register that is the source for a pseudo-table cursor created using
84669 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
84676 pOut = &aMem[pOp->p2];
84677 pC = p->apCsr[pOp->p1];
84680 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
84681 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84683 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
84700 ** of a real table, not a pseudo-table.
84704 ** register will be invalidated as soon as the cursor moves - including
84722 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84723 pC = p->apCsr[pOp->p1];
84725 assert( pC->eCurType==CURTYPE_BTREE );
84727 assert( pC->nullRow==0 );
84728 assert( pC->uc.pCursor!=0 );
84729 pCrsr = pC->uc.pCursor;
84739 assert( pC->deferredMoveto==0 );
84747 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
84753 if( !pOp->p3 ) Deephemeralize(pOut);
84755 REGISTER_TRACE(pOp->p2, pOut);
84776 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84777 pC = p->apCsr[pOp->p1];
84779 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
84780 if( pC->nullRow ){
84781 pOut->flags = MEM_Null;
84783 }else if( pC->deferredMoveto ){
84784 v = pC->movetoTarget;
84786 }else if( pC->eCurType==CURTYPE_VTAB ){
84787 assert( pC->uc.pVCur!=0 );
84788 pVtab = pC->uc.pVCur->pVtab;
84789 pModule = pVtab->pModule;
84790 assert( pModule->xRowid );
84791 rc = pModule->xRowid(pC->uc.pVCur, &v);
84796 assert( pC->eCurType==CURTYPE_BTREE );
84797 assert( pC->uc.pCursor!=0 );
84800 if( pC->nullRow ){
84801 pOut->flags = MEM_Null;
84804 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
84806 pOut->u.i = v;
84819 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84820 pC = p->apCsr[pOp->p1];
84822 pC->nullRow = 1;
84823 pC->cacheStatus = CACHE_STALE;
84824 if( pC->eCurType==CURTYPE_BTREE ){
84825 assert( pC->uc.pCursor!=0 );
84826 sqlite3BtreeClearCursor(pC->uc.pCursor);
84859 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84860 pC = p->apCsr[pOp->p1];
84862 assert( pC->eCurType==CURTYPE_BTREE );
84863 pCrsr = pC->uc.pCursor;
84867 pC->seekOp = pOp->opcode;
84869 if( pOp->opcode==OP_SeekEnd ){
84870 assert( pOp->p2==0 );
84871 pC->seekResult = -1;
84877 pC->nullRow = (u8)res;
84878 pC->deferredMoveto = 0;
84879 pC->cacheStatus = CACHE_STALE;
84881 if( pOp->p2>0 ){
84899 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84900 pC = p->apCsr[pOp->p1];
84902 pCrsr = pC->uc.pCursor;
84908 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
84941 sqlite3_search_count--;
84943 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
84963 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84964 pC = p->apCsr[pOp->p1];
84966 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
84969 pC->seekOp = OP_Rewind;
84974 assert( pC->eCurType==CURTYPE_BTREE );
84975 pCrsr = pC->uc.pCursor;
84978 pC->deferredMoveto = 0;
84979 pC->cacheStatus = CACHE_STALE;
84982 pC->nullRow = (u8)res;
84983 assert( pOp->p2>0 && pOp->p2<p->nOp );
85000 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
85012 ** number P5-1 in the prepared statement is incremented.
85019 ** open it behaves a no-op.
85033 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
85045 ** number P5-1 in the prepared statement is incremented.
85050 ** open it behaves a no-op.
85062 pC = p->apCsr[pOp->p1];
85068 if( p->apCsr[pOp->p1]==0 ) break;
85072 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85073 assert( pOp->p5<ArraySize(p->aCounter) );
85074 pC = p->apCsr[pOp->p1];
85076 assert( pC->deferredMoveto==0 );
85077 assert( pC->eCurType==CURTYPE_BTREE );
85078 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
85079 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
85080 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
85081 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
85085 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
85086 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
85087 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
85088 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
85089 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
85090 || pC->seekOp==OP_Last );
85092 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
85094 pC->cacheStatus = CACHE_STALE;
85097 pC->nullRow = 0;
85098 p->aCounter[pOp->p5]++;
85106 pC->nullRow = 1;
85122 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
85150 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85151 pC = p->apCsr[pOp->p1];
85153 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
85154 pIn2 = &aMem[pOp->p2];
85155 assert( pIn2->flags & MEM_Blob );
85156 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
85157 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
85158 assert( pC->isTable==0 );
85161 if( pOp->opcode==OP_SorterInsert ){
85164 x.nKey = pIn2->n;
85165 x.pKey = pIn2->z;
85166 x.aMem = aMem + pOp->p3;
85167 x.nMem = (u16)pOp->p4.i;
85168 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
85169 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
85170 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
85172 assert( pC->deferredMoveto==0 );
85173 pC->cacheStatus = CACHE_STALE;
85192 assert( pOp->p3>0 );
85193 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
85194 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85195 pC = p->apCsr[pOp->p1];
85197 assert( pC->eCurType==CURTYPE_BTREE );
85198 pCrsr = pC->uc.pCursor;
85200 assert( pOp->p5==0 );
85201 r.pKeyInfo = pC->pKeyInfo;
85202 r.nField = (u16)pOp->p3;
85204 r.aMem = &aMem[pOp->p2];
85211 assert( pC->deferredMoveto==0 );
85212 pC->cacheStatus = CACHE_STALE;
85213 pC->seekResult = 0;
85230 ** is non-zero, then reading column a(i)-1 from cursor P3 is
85251 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85252 pC = p->apCsr[pOp->p1];
85254 assert( pC->eCurType==CURTYPE_BTREE );
85255 assert( pC->uc.pCursor!=0 );
85256 assert( pC->isTable==0 );
85257 assert( pC->deferredMoveto==0 );
85258 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
85269 if( !pC->nullRow ){
85271 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
85275 if( pOp->opcode==OP_DeferredSeek ){
85276 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
85277 pTabCur = p->apCsr[pOp->p3];
85279 assert( pTabCur->eCurType==CURTYPE_BTREE );
85280 assert( pTabCur->uc.pCursor!=0 );
85281 assert( pTabCur->isTable );
85282 pTabCur->nullRow = 0;
85283 pTabCur->movetoTarget = rowid;
85284 pTabCur->deferredMoveto = 1;
85285 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
85286 pTabCur->aAltMap = pOp->p4.ai;
85287 pTabCur->pAltCursor = pC;
85290 pOut->u.i = rowid;
85293 assert( pOp->opcode==OP_IdxRowid );
85294 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
85351 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85352 pC = p->apCsr[pOp->p1];
85354 assert( pC->isOrdered );
85355 assert( pC->eCurType==CURTYPE_BTREE );
85356 assert( pC->uc.pCursor!=0);
85357 assert( pC->deferredMoveto==0 );
85358 assert( pOp->p5==0 || pOp->p5==1 );
85359 assert( pOp->p4type==P4_INT32 );
85360 r.pKeyInfo = pC->pKeyInfo;
85361 r.nField = (u16)pOp->p4.i;
85362 if( pOp->opcode<OP_IdxLT ){
85363 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
85364 r.default_rc = -1;
85366 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
85369 r.aMem = &aMem[pOp->p3];
85376 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
85377 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
85378 res = -res;
85380 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
85401 ** value of the root page that moved - its value before the move occurred -
85412 ** and non-autovacuum modes.
85420 assert( p->readOnly==0 );
85421 assert( pOp->p1>1 );
85423 pOut->flags = MEM_Null;
85424 if( db->nVdbeRead > db->nVDestroy+1 ){
85426 p->errorAction = OE_Abort;
85429 iDb = pOp->p3;
85430 assert( DbMaskTest(p->btreeMask, iDb) );
85432 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
85433 pOut->flags = MEM_Int;
85434 pOut->u.i = iMoved;
85438 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
85458 ** If the P3 value is non-zero, then the table referred to must be an
85470 assert( p->readOnly==0 );
85471 assert( DbMaskTest(p->btreeMask, pOp->p2) );
85473 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
85475 if( pOp->p3 ){
85476 p->nChange += nChange;
85477 if( pOp->p3>0 ){
85478 assert( memIsValid(&aMem[pOp->p3]) );
85479 memAboutToChange(p, &aMem[pOp->p3]);
85480 aMem[pOp->p3].u.i += nChange;
85498 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85499 pC = p->apCsr[pOp->p1];
85502 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
85504 assert( pC->eCurType==CURTYPE_BTREE );
85505 assert( pC->isEphemeral );
85506 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
85515 ** Allocate a new b-tree in the main database file if P1==0 or in the
85519 ** The root page number of the new b-tree is stored in register P2.
85527 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
85528 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85529 assert( DbMaskTest(p->btreeMask, pOp->p1) );
85530 assert( p->readOnly==0 );
85531 pDb = &db->aDb[pOp->p1];
85532 assert( pDb->pBt!=0 );
85533 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
85535 pOut->u.i = pgno;
85544 db->nSqlExec++;
85545 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
85546 db->nSqlExec--;
85557 ** then runs the new virtual machine. It is thus a re-entrant opcode.
85570 for(iDb=0; iDb<db->nDb; iDb++){
85571 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
85575 iDb = pOp->p1;
85576 assert( iDb>=0 && iDb<db->nDb );
85581 initData.iDb = pOp->p1;
85582 initData.pzErrMsg = &p->zErrMsg;
85585 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
85589 assert( db->init.busy==0 );
85590 db->init.busy = 1;
85592 assert( !db->mallocFailed );
85596 db->init.busy = 0;
85617 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85618 rc = sqlite3AnalysisLoad(db, pOp->p1);
85626 ** Remove the internal (in-memory) data structures that describe
85633 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
85639 ** Remove the internal (in-memory) data structures that describe
85646 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
85652 ** Remove the internal (in-memory) data structures that describe
85659 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
85691 assert( p->bIsReader );
85692 nRoot = pOp->p2;
85693 aRoot = pOp->p4.ai;
85696 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85697 pnErr = &aMem[pOp->p3];
85698 assert( (pnErr->flags & MEM_Int)!=0 );
85699 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
85700 pIn1 = &aMem[pOp->p1];
85701 assert( pOp->p5<db->nDb );
85702 assert( DbMaskTest(p->btreeMask, pOp->p5) );
85703 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
85704 (int)pnErr->u.i+1, &nErr);
85711 pnErr->u.i -= nErr-1;
85712 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
85729 pIn1 = &aMem[pOp->p1];
85730 pIn2 = &aMem[pOp->p2];
85731 assert( (pIn2->flags & MEM_Int)!=0 );
85732 if( (pIn1->flags & MEM_RowSet)==0 ){
85734 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
85736 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
85751 pIn1 = &aMem[pOp->p1];
85752 if( (pIn1->flags & MEM_RowSet)==0
85753 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
85762 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
85770 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
85779 ** must have P4==0, the final set must have P4==-1, and for all other sets
85784 ** (b) when P4==-1 there is no need to insert the value, as it will
85794 pIn1 = &aMem[pOp->p1];
85795 pIn3 = &aMem[pOp->p3];
85796 iSet = pOp->p4.i;
85797 assert( pIn3->flags&MEM_Int );
85802 if( (pIn1->flags & MEM_RowSet)==0 ){
85804 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
85807 assert( pOp->p4type==P4_INT32 );
85808 assert( iSet==-1 || iSet>=0 );
85810 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
85815 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
85828 ** cell in an array of values used as arguments to the sub-program. P2
85829 ** contains the address to jump to if the sub-program throws an IGNORE
85832 ** memory required by the sub-vdbe at runtime.
85836 ** If P5 is non-zero, then recursive program invocation is enabled.
85839 int nMem; /* Number of memory registers for sub-program */
85840 int nByte; /* Bytes of runtime space required for sub-program */
85845 SubProgram *pProgram; /* Sub-program to execute */
85848 pProgram = pOp->p4.pProgram;
85849 pRt = &aMem[pOp->p3];
85850 assert( pProgram->nOp>0 );
85853 ** disabled for backwards compatibility (p5 is set if this sub-program
85863 if( pOp->p5 ){
85864 t = pProgram->token;
85865 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
85869 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
85879 if( (pRt->flags&MEM_Frame)==0 ){
85885 nMem = pProgram->nMem + pProgram->nCsr;
85887 if( pProgram->nCsr==0 ) nMem++;
85890 + pProgram->nCsr * sizeof(VdbeCursor*)
85891 + (pProgram->nOp + 7)/8;
85897 pRt->flags = MEM_Frame;
85898 pRt->u.pFrame = pFrame;
85900 pFrame->v = p;
85901 pFrame->nChildMem = nMem;
85902 pFrame->nChildCsr = pProgram->nCsr;
85903 pFrame->pc = (int)(pOp - aOp);
85904 pFrame->aMem = p->aMem;
85905 pFrame->nMem = p->nMem;
85906 pFrame->apCsr = p->apCsr;
85907 pFrame->nCursor = p->nCursor;
85908 pFrame->aOp = p->aOp;
85909 pFrame->nOp = p->nOp;
85910 pFrame->token = pProgram->token;
85912 pFrame->anExec = p->anExec;
85915 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
85917 pMem->flags = MEM_Undefined;
85918 pMem->db = db;
85921 pFrame = pRt->u.pFrame;
85922 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
85923 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
85924 assert( pProgram->nCsr==pFrame->nChildCsr );
85925 assert( (int)(pOp - aOp)==pFrame->pc );
85928 p->nFrame++;
85929 pFrame->pParent = p->pFrame;
85930 pFrame->lastRowid = db->lastRowid;
85931 pFrame->nChange = p->nChange;
85932 pFrame->nDbChange = p->db->nChange;
85933 assert( pFrame->pAuxData==0 );
85934 pFrame->pAuxData = p->pAuxData;
85935 p->pAuxData = 0;
85936 p->nChange = 0;
85937 p->pFrame = pFrame;
85938 p->aMem = aMem = VdbeFrameMem(pFrame);
85939 p->nMem = pFrame->nChildMem;
85940 p->nCursor = (u16)pFrame->nChildCsr;
85941 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
85942 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
85943 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
85944 p->aOp = aOp = pProgram->aOp;
85945 p->nOp = pProgram->nOp;
85947 p->anExec = 0;
85949 pOp = &aOp[-1];
85956 ** This opcode is only ever present in sub-programs called via the
85970 pFrame = p->pFrame;
85971 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
85983 ** If P1 is non-zero, the database constraint counter is incremented
85988 if( db->flags & SQLITE_DeferFKs ){
85989 db->nDeferredImmCons += pOp->p2;
85990 }else if( pOp->p1 ){
85991 db->nDeferredCons += pOp->p2;
85993 p->nFkConstraint += pOp->p2;
86001 ** This opcode tests if a foreign key constraint-counter is currently zero.
86005 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
86007 ** zero, the jump is taken if the statement constraint-counter is zero
86011 if( pOp->p1 ){
86012 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
86013 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
86015 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
86016 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
86028 ** within a sub-program). Set the value of register P1 to the maximum of
86036 if( p->pFrame ){
86037 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
86038 pIn1 = &pFrame->aMem[pOp->p1];
86040 pIn1 = &aMem[pOp->p1];
86044 pIn2 = &aMem[pOp->p2];
86046 if( pIn1->u.i<pIn2->u.i){
86047 pIn1->u.i = pIn2->u.i;
86054 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
86064 pIn1 = &aMem[pOp->p1];
86065 assert( pIn1->flags&MEM_Int );
86066 VdbeBranchTaken( pIn1->u.i>0, 2);
86067 if( pIn1->u.i>0 ){
86068 pIn1->u.i -= pOp->p3;
86075 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
86088 ** and r[P2] is set to -1.
86094 pIn1 = &aMem[pOp->p1];
86095 pIn3 = &aMem[pOp->p3];
86097 assert( pIn1->flags & MEM_Int );
86098 assert( pIn3->flags & MEM_Int );
86099 x = pIn1->u.i;
86100 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
86108 pOut->u.i = -1;
86110 pOut->u.i = x;
86116 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
86120 ** If it is non-zero (negative or positive) and then also jump to P2.
86124 pIn1 = &aMem[pOp->p1];
86125 assert( pIn1->flags&MEM_Int );
86126 VdbeBranchTaken(pIn1->u.i<0, 2);
86127 if( pIn1->u.i ){
86128 if( pIn1->u.i>0 ) pIn1->u.i--;
86135 ** Synopsis: if (--r[P1])==0 goto P2
86141 pIn1 = &aMem[pOp->p1];
86142 assert( pIn1->flags&MEM_Int );
86143 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
86144 VdbeBranchTaken(pIn1->u.i==0, 2);
86145 if( pIn1->u.i==0 ) goto jump_to_p2;
86182 assert( pOp->p4type==P4_FUNCDEF );
86183 n = pOp->p5;
86184 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
86185 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
86186 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
86187 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
86189 pCtx->pMem = 0;
86190 pCtx->pFunc = pOp->p4.pFunc;
86191 pCtx->iOp = (int)(pOp - aOp);
86192 pCtx->pVdbe = p;
86193 pCtx->argc = n;
86194 pOp->p4type = P4_FUNCCTX;
86195 pOp->p4.pCtx = pCtx;
86196 pOp->opcode = OP_AggStep;
86205 assert( pOp->p4type==P4_FUNCCTX );
86206 pCtx = pOp->p4.pCtx;
86207 pMem = &aMem[pOp->p3];
86213 if( pCtx->pMem != pMem ){
86214 pCtx->pMem = pMem;
86215 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
86219 for(i=0; i<pCtx->argc; i++){
86220 assert( memIsValid(pCtx->argv[i]) );
86221 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
86225 pMem->n++;
86227 pCtx->pOut = &t;
86228 pCtx->fErrorOrAux = 0;
86229 pCtx->skipFlag = 0;
86230 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
86231 if( pCtx->fErrorOrAux ){
86232 if( pCtx->isError ){
86234 rc = pCtx->isError;
86241 if( pCtx->skipFlag ){
86242 assert( pOp[-1].opcode==OP_CollSeq );
86243 i = pOp[-1].p1;
86264 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86265 pMem = &aMem[pOp->p1];
86266 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
86267 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
86283 ** Checkpoint database P1. This is a no-op if P1 is not currently in
86290 ** mem[P3+2] are initialized to -1.
86297 assert( p->readOnly==0 );
86299 aRes[1] = aRes[2] = -1;
86300 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
86301 || pOp->p2==SQLITE_CHECKPOINT_FULL
86302 || pOp->p2==SQLITE_CHECKPOINT_RESTART
86303 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
86305 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
86311 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
86328 ** Write a string containing the final journal-mode to register P2.
86340 eNew = pOp->p3;
86349 assert( pOp->p1>=0 && pOp->p1<db->nDb );
86350 assert( p->readOnly==0 );
86352 pBt = db->aDb[pOp->p1].pBt;
86366 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
86374 if( !db->autoCommit || db->nVdbeRead>1 ){
86385 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
86413 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
86414 pOut->z = (char *)sqlite3JournalModename(eNew);
86415 pOut->n = sqlite3Strlen30(pOut->z);
86416 pOut->enc = SQLITE_UTF8;
86430 assert( p->readOnly==0 );
86431 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
86447 assert( pOp->p1>=0 && pOp->p1<db->nDb );
86448 assert( DbMaskTest(p->btreeMask, pOp->p1) );
86449 assert( p->readOnly==0 );
86450 pBt = db->aDb[pOp->p1].pBt;
86469 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
86473 if( !pOp->p1 ){
86476 p->expired = 1;
86486 ** the shared-cache feature is enabled.
86492 ** P2 contains the root-page of the table to lock.
86498 u8 isWriteLock = (u8)pOp->p3;
86499 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
86500 int p1 = pOp->p1;
86501 assert( p1>=0 && p1<db->nDb );
86502 assert( DbMaskTest(p->btreeMask, p1) );
86504 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
86507 const char *z = pOp->p4.z;
86529 pVTab = pOp->p4.pVtab;
86531 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
86551 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
86552 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
86553 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
86556 assert( zTab || db->mallocFailed );
86558 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
86573 db->nVDestroy++;
86574 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
86575 db->nVDestroy--;
86594 assert( p->bIsReader );
86597 pVtab = pOp->p4.pVtab->pVtab;
86598 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
86602 pModule = pVtab->pModule;
86603 rc = pModule->xOpen(pVtab, &pVCur);
86608 pVCur->pVtab = pVtab;
86611 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
86613 pCur->uc.pVCur = pVCur;
86614 pVtab->nRef++;
86616 assert( db->mallocFailed );
86617 pModule->xClose(pVCur);
86657 pQuery = &aMem[pOp->p3];
86659 pCur = p->apCsr[pOp->p1];
86661 REGISTER_TRACE(pOp->p3, pQuery);
86662 assert( pCur->eCurType==CURTYPE_VTAB );
86663 pVCur = pCur->uc.pVCur;
86664 pVtab = pVCur->pVtab;
86665 pModule = pVtab->pModule;
86668 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
86669 nArg = (int)pArgc->u.i;
86670 iQuery = (int)pQuery->u.i;
86674 apArg = p->apArg;
86678 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
86681 res = pModule->xEof(pVCur);
86682 pCur->nullRow = 0;
86693 ** Store in register P3 the value of the P2-th column of
86694 ** the current row of the virtual-table of cursor P1.
86700 ** by virtual table implementations to return special "no-change"
86709 VdbeCursor *pCur = p->apCsr[pOp->p1];
86710 assert( pCur->eCurType==CURTYPE_VTAB );
86711 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
86712 pDest = &aMem[pOp->p3];
86714 if( pCur->nullRow ){
86718 pVtab = pCur->uc.pVCur->pVtab;
86719 pModule = pVtab->pModule;
86720 assert( pModule->xColumn );
86723 if( pOp->p5 ){
86725 pDest->flags = MEM_Null|MEM_Zero;
86726 pDest->u.nZero = 0;
86730 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
86736 REGISTER_TRACE(pOp->p3, pDest);
86761 pCur = p->apCsr[pOp->p1];
86762 assert( pCur->eCurType==CURTYPE_VTAB );
86763 if( pCur->nullRow ){
86766 pVtab = pCur->uc.pVCur->pVtab;
86767 pModule = pVtab->pModule;
86768 assert( pModule->xNext );
86776 rc = pModule->xNext(pCur->uc.pVCur);
86779 res = pModule->xEof(pCur->uc.pVCur);
86800 pVtab = pOp->p4.pVtab->pVtab;
86801 pName = &aMem[pOp->p1];
86802 assert( pVtab->pModule->xRename );
86804 assert( p->readOnly==0 );
86805 REGISTER_TRACE(pOp->p1, pName);
86806 assert( pName->flags & MEM_Str );
86807 testcase( pName->enc==SQLITE_UTF8 );
86808 testcase( pName->enc==SQLITE_UTF16BE );
86809 testcase( pName->enc==SQLITE_UTF16LE );
86812 rc = pVtab->pModule->xRename(pVtab, pName->z);
86814 p->expired = 0;
86827 ** invocation. The value in register (P3+P2-1) corresponds to the
86857 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
86858 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
86860 assert( p->readOnly==0 );
86861 pVtab = pOp->p4.pVtab->pVtab;
86862 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
86866 pModule = pVtab->pModule;
86867 nArg = pOp->p2;
86868 assert( pOp->p4type==P4_VTAB );
86869 if( ALWAYS(pModule->xUpdate) ){
86870 u8 vtabOnConflict = db->vtabOnConflict;
86871 apArg = p->apArg;
86872 pX = &aMem[pOp->p3];
86879 db->vtabOnConflict = pOp->p5;
86880 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
86881 db->vtabOnConflict = vtabOnConflict;
86883 if( rc==SQLITE_OK && pOp->p1 ){
86884 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
86885 db->lastRowid = rowid;
86887 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
86888 if( pOp->p5==OE_Ignore ){
86891 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
86894 p->nChange++;
86909 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
86929 pBt = db->aDb[pOp->p1].pBt;
86931 if( pOp->p3 ){
86933 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
86935 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
86948 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
86965 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
86986 assert( pOp->p4type==P4_FUNCDEF );
86987 n = pOp->p5;
86988 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
86989 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
86990 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
86991 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
86993 pCtx->pOut = 0;
86994 pCtx->pFunc = pOp->p4.pFunc;
86995 pCtx->iOp = (int)(pOp - aOp);
86996 pCtx->pVdbe = p;
86997 pCtx->argc = n;
86998 pOp->p4type = P4_FUNCCTX;
86999 pOp->p4.pCtx = pCtx;
87002 pOp->opcode += 2;
87010 assert( pOp->p4type==P4_FUNCCTX );
87011 pCtx = pOp->p4.pCtx;
87017 pOut = &aMem[pOp->p3];
87018 if( pCtx->pOut != pOut ){
87019 pCtx->pOut = pOut;
87020 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
87025 for(i=0; i<pCtx->argc; i++){
87026 assert( memIsValid(pCtx->argv[i]) );
87027 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
87031 pCtx->fErrorOrAux = 0;
87032 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
87035 if( pCtx->fErrorOrAux ){
87036 if( pCtx->isError ){
87038 rc = pCtx->isError;
87040 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
87045 if( pOut->flags & (MEM_Str|MEM_Blob) ){
87050 REGISTER_TRACE(pOp->p3, pOut);
87069 ** the UTF-8 string contained in P4 is emitted on the trace callback.
87086 ** The "--" string is broken up to prevent false-positives with srcck1.c.
87089 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
87091 ** using the X argument when X begins with "--" and invoking
87094 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
87097 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
87100 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
87101 && !p->doingRerun
87102 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
87105 if( db->mTrace & SQLITE_TRACE_LEGACY ){
87106 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
87108 x(db->pTraceArg, z);
87112 if( db->nVdbeExec>1 ){
87113 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
87114 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
87117 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
87121 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
87124 for(j=0; j<db->nDb; j++){
87125 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
87126 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
87131 if( (db->flags & SQLITE_SqlTrace)!=0
87132 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
87134 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
87138 assert( pOp->p2>0 );
87139 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
87140 if( pOp->opcode==OP_Trace ) break;
87141 for(i=1; i<p->nOp; i++){
87142 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
87144 pOp->p1 = 0;
87146 pOp->p1++;
87147 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
87157 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
87162 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87163 assert( pOp->p4type==P4_EXPR );
87164 pC = p->apCsr[pOp->p1];
87166 assert( pC->eCurType==CURTYPE_BTREE );
87167 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
87168 pOp->p4.pExpr, aMem);
87183 ** the same as a no-op. This opcodesnever appears in a real VM program.
87186 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
87192 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
87201 if( endTime>start ) pOrigOp->cycles += endTime - start;
87202 pOrigOp->cnt++;
87212 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
87215 if( db->flags & SQLITE_VdbeTrace ){
87216 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
87219 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
87222 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
87233 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
87235 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
87238 p->rc = rc;
87242 (int)(pOp - aOp), p->zSql, p->zErrMsg);
87247 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
87255 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
87258 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
87282 assert( db->u1.isInterrupted );
87283 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
87284 p->rc = rc;
87330 ** the b-tree cursor associated with blob handle p to point to row iRow.
87341 ** If an error does occur, then the b-tree cursor is closed. All subsequent
87348 Vdbe *v = (Vdbe *)p->pStmt;
87353 v->aMem[1].flags = MEM_Int;
87354 v->aMem[1].u.i = iRow;
87360 if( v->pc>4 ){
87361 v->pc = 4;
87362 assert( v->aOp[v->pc].opcode==OP_NotExists );
87365 rc = sqlite3_step(p->pStmt);
87368 VdbeCursor *pC = v->apCsr[0];
87369 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
87370 testcase( pC->nHdrParsed==p->iCol );
87371 testcase( pC->nHdrParsed==p->iCol+1 );
87373 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
87377 sqlite3_finalize(p->pStmt);
87378 p->pStmt = 0;
87380 p->iOffset = pC->aType[p->iCol + pC->nField];
87381 p->nByte = sqlite3VdbeSerialTypeLen(type);
87382 p->pCsr = pC->uc.pCursor;
87383 sqlite3BtreeIncrblobCursor(p->pCsr);
87389 }else if( p->pStmt ){
87390 rc = sqlite3_finalize(p->pStmt);
87391 p->pStmt = 0;
87393 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
87396 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
87416 int wrFlag, /* True -> read/write access, false -> read-only */
87420 int iCol; /* Index of zColumn in row-record */
87440 sqlite3_mutex_enter(db->mutex);
87461 if( pTab && pTab->pSelect ){
87476 pBlob->pTab = pTab;
87477 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
87480 for(iCol=0; iCol<pTab->nCol; iCol++) {
87481 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
87485 if( iCol==pTab->nCol ){
87500 if( db->flags&SQLITE_ForeignKeys ){
87506 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
87508 for(j=0; j<pFKey->nCol; j++){
87509 if( pFKey->aCol[j].iFrom==iCol ){
87516 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
87518 for(j=0; j<pIdx->nKeyCol; j++){
87520 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
87534 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
87535 assert( pBlob->pStmt || db->mallocFailed );
87536 if( pBlob->pStmt ){
87540 ** of writing code to use the b-tree layer directly is that the
87545 ** Code external to the Vdbe then "borrows" the b-tree cursor and
87550 ** which closes the b-tree cursor and (possibly) commits the
87563 Vdbe *v = (Vdbe *)pBlob->pStmt;
87564 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
87568 pTab->pSchema->schema_cookie,
87569 pTab->pSchema->iGeneration);
87571 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
87577 if( db->mallocFailed==0 ){
87584 aOp[0].p2 = pTab->tnum;
87586 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
87588 if( db->mallocFailed==0 ){
87592 ** parameter of the other to pTab->tnum. */
87594 aOp[1].p2 = pTab->tnum;
87605 aOp[1].p4.i = pTab->nCol+1;
87606 aOp[3].p2 = pTab->nCol;
87615 pBlob->iCol = iCol;
87616 pBlob->db = db;
87618 if( db->mallocFailed ){
87625 if( rc==SQLITE_OK && db->mallocFailed==0 ){
87628 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
87635 sqlite3_mutex_leave(db->mutex);
87649 db = p->db;
87650 sqlite3_mutex_enter(db->mutex);
87651 rc = sqlite3_finalize(p->pStmt);
87653 sqlite3_mutex_leave(db->mutex);
87676 db = p->db;
87677 sqlite3_mutex_enter(db->mutex);
87678 v = (Vdbe*)p->pStmt;
87680 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
87684 /* If there is no statement handle, then the blob-handle has
87690 ** returned, clean-up the statement handle.
87692 assert( db == v->db );
87693 sqlite3BtreeEnterCursor(p->pCsr);
87696 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
87697 /* If a pre-update hook is registered and this is a write cursor,
87700 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
87707 ** using the incremental-blob API, this works. For the sessions module
87711 iKey = sqlite3BtreeIntegerKey(p->pCsr);
87713 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
87718 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
87719 sqlite3BtreeLeaveCursor(p->pCsr);
87722 p->pStmt = 0;
87724 v->rc = rc;
87729 sqlite3_mutex_leave(db->mutex);
87755 return (p && p->pStmt) ? p->nByte : 0;
87774 db = p->db;
87775 sqlite3_mutex_enter(db->mutex);
87777 if( p->pStmt==0 ){
87778 /* If there is no statement handle, then the blob-handle has
87793 assert( rc==SQLITE_OK || p->pStmt==0 );
87794 sqlite3_mutex_leave(db->mutex);
87803 ** 2011-07-09
87818 ** The VdbeSorter object implements a multi-threaded external merge sort
87822 ** Here is the (internal, non-API) interface between this module and the
87875 ** an in-memory merge sort. In this case, no temporary files are required
87882 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
87883 ** of PMAs may be created by merging existing PMAs together - for example
87884 ** merging two or more level-0 PMAs together creates a level-1 PMA.
87888 ** page-cache of the main database. Specifically, the threshold is set to
87892 ** If the sorter is running in single-threaded mode, then all PMAs generated
87894 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
87901 ** The sorter is running in multi-threaded mode if (a) the library was built
87902 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
87911 ** sorter is running in single-threaded mode, then these PMAs are merged
87916 ** Or, if running in multi-threaded mode, then a background thread is
87926 ** Rewind() is called, then a hierarchy of incremental-merges is used.
87932 ** If running in multi-threaded mode and there are more than
87945 ** characteristics of the sorter in multi-threaded mode.
87952 ** Hard-coded maximum amount of data to accumulate in memory before flushing
87965 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
87967 typedef struct SorterList SorterList; /* In-memory list of records */
87980 ** An in-memory list of objects to be sorted.
87989 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
88012 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
88026 ** aReadr[0] -> Banana
88027 ** aReadr[1] -> Feijoa
88028 ** aReadr[2] -> Elderberry
88029 ** aReadr[3] -> Currant
88030 ** aReadr[4] -> Grapefruit
88031 ** aReadr[5] -> Apple
88032 ** aReadr[6] -> Durian
88033 ** aReadr[7] -> EOF
88042 ** aReadr[5] -> Eggplant
88047 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
88070 ** single-threaded operation, there is exactly one instance of this object
88071 ** and for multi-threaded operation there are two or more instances.
88082 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
88099 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
88104 SorterFile file; /* Temp file for level-0 PMAs */
88128 SorterList list; /* List of in-memory records */
88171 ** There are two types of IncrMerger object - single (bUseThread==0) and
88172 ** multi-threaded (bUseThread==1).
88174 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
88189 ** A single-threaded IncrMerger does not open any temporary files of its
88191 ** at offset iStartOff of file pTask->file2. And instead of using a
88193 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
88211 ** size. But I/O is more efficient if it occurs in page-sized blocks where
88213 ** the PMA so that aligned, page-size blocks are written.
88216 int eFWErr; /* Non-zero if in an error state */
88230 ** by this module. If using a separate allocation for each in-memory record
88238 ** has finished passing records to the sorter, or when the in-memory buffer
88271 sqlite3_free(pReadr->aAlloc);
88272 sqlite3_free(pReadr->aBuffer);
88273 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
88274 vdbeIncrFree(pReadr->pIncr);
88295 if( p->aMap ){
88296 *ppOut = &p->aMap[p->iReadOff];
88297 p->iReadOff += nByte;
88301 assert( p->aBuffer );
88304 ** p->nBuffer bytes of data from the file into it. Or, if there are less
88305 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
88306 iBuf = p->iReadOff % p->nBuffer;
88312 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
88313 nRead = p->nBuffer;
88315 nRead = (int)(p->iEof - p->iReadOff);
88320 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
88324 nAvail = p->nBuffer - iBuf;
88327 /* The requested data is available in the in-memory buffer. In this
88330 *ppOut = &p->aBuffer[iBuf];
88331 p->iReadOff += nByte;
88333 /* The requested data is not all available in the in-memory buffer.
88334 ** In this case, allocate space at p->aAlloc[] to copy the requested
88335 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
88338 /* Extend the p->aAlloc[] allocation if required. */
88339 if( p->nAlloc<nByte ){
88341 int nNew = MAX(128, p->nAlloc*2);
88343 aNew = sqlite3Realloc(p->aAlloc, nNew);
88345 p->nAlloc = nNew;
88346 p->aAlloc = aNew;
88350 ** p->aAlloc[]. */
88351 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
88352 p->iReadOff += nAvail;
88353 nRem = nByte - nAvail;
88355 /* The following loop copies up to p->nBuffer bytes per iteration into
88356 ** the p->aAlloc[] buffer. */
88363 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
88366 assert( aNext!=p->aAlloc );
88367 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
88368 nRem -= nCopy;
88371 *ppOut = p->aAlloc;
88384 if( p->aMap ){
88385 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
88387 iBuf = p->iReadOff % p->nBuffer;
88388 if( iBuf && (p->nBuffer-iBuf)>=9 ){
88389 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
88416 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
88417 sqlite3_file *pFd = pFile->pFd;
88418 if( pFd->pMethods->iVersion>=3 ){
88419 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
88439 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
88442 if( pReadr->aMap ){
88443 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
88444 pReadr->aMap = 0;
88446 pReadr->iReadOff = iOff;
88447 pReadr->iEof = pFile->iEof;
88448 pReadr->pFd = pFile->pFd;
88450 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
88451 if( rc==SQLITE_OK && pReadr->aMap==0 ){
88452 int pgsz = pTask->pSorter->pgsz;
88453 int iBuf = pReadr->iReadOff % pgsz;
88454 if( pReadr->aBuffer==0 ){
88455 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
88456 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
88457 pReadr->nBuffer = pgsz;
88460 int nRead = pgsz - iBuf;
88461 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
88462 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
88465 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
88483 if( pReadr->iReadOff>=pReadr->iEof ){
88484 IncrMerger *pIncr = pReadr->pIncr;
88488 if( rc==SQLITE_OK && pIncr->bEof==0 ){
88490 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
88508 pReadr->nKey = (int)nRec;
88509 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
88518 ** starting at offset iStart and ending at offset iEof-1. This function
88534 assert( pFile->iEof>iStart );
88535 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
88536 assert( pReadr->aBuffer==0 );
88537 assert( pReadr->aMap==0 );
88543 pReadr->iEof = pReadr->iReadOff + nByte;
88560 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
88564 UnpackedRecord *r2 = pTask->pUnpacked;
88566 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
88574 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
88578 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
88579 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
88582 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
88587 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
88591 UnpackedRecord *r2 = pTask->pUnpacked;
88593 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
88606 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
88621 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
88623 res = n1 - n2;
88627 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
88633 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
88634 res = res * -1;
88647 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
88669 if( (res = v1[i] - v2[i])!=0 ){
88671 res = v1[0] & 0x80 ? -1 : +1;
88677 res = s1 - s2;
88682 res = -1;
88684 res = s1 - s2;
88689 if( *v1 & 0x80 ) res = -1;
88696 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
88701 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
88702 res = res * -1;
88711 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
88714 ** is non-zero and the sorter is able to guarantee a stable sort, nField
88722 ** The sorter can guarantee a stable sort when running in single-threaded
88723 ** mode, but not in multi-threaded mode.
88735 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
88736 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
88750 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
88758 nWorker = SORTER_MAX_MERGE_COUNT-1;
88762 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
88763 assert( pCsr->eCurType==CURTYPE_SORTER );
88764 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
88768 pCsr->uc.pSorter = pSorter;
88772 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
88773 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
88774 pKeyInfo->db = 0;
88776 pKeyInfo->nKeyField = nField;
88778 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
88779 pSorter->nTask = nWorker + 1;
88780 pSorter->iPrev = (u8)(nWorker - 1);
88781 pSorter->bUseThreads = (pSorter->nTask>1);
88782 pSorter->db = db;
88783 for(i=0; i<pSorter->nTask; i++){
88784 SortSubtask *pTask = &pSorter->aTask[i];
88785 pTask->pSorter = pSorter;
88791 pSorter->mnPmaSize = szPma * pgsz;
88793 mxCache = db->aDb[0].pSchema->cache_size;
88795 /* A negative cache-size value C indicates that the cache is abs(C)
88797 mxCache = mxCache * -1024;
88802 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
88807 assert( pSorter->iMemory==0 );
88808 pSorter->nMemory = pgsz;
88809 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
88810 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
88814 if( pKeyInfo->nAllField<13
88815 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
88817 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
88832 pNext = p->u.pNext;
88842 sqlite3DbFree(db, pTask->pUnpacked);
88844 /* pTask->list.aMemory can only be non-zero if it was handed memory
88846 if( pTask->list.aMemory ){
88847 sqlite3_free(pTask->list.aMemory);
88851 assert( pTask->list.aMemory==0 );
88852 vdbeSorterRecordFree(0, pTask->list.pList);
88854 if( pTask->file.pFd ){
88855 sqlite3OsCloseFree(pTask->file.pFd);
88857 if( pTask->file2.pFd ){
88858 sqlite3OsCloseFree(pTask->file2.pFd);
88866 int iTask = (pTask - pTask->pSorter->aTask);
88867 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
88880 int iTask = (pTask - pTask->pSorter->aTask);
88881 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
88891 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
88904 ** Join thread pTask->thread.
88908 if( pTask->pThread ){
88910 int bDone = pTask->bDone;
88914 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
88917 assert( pTask->bDone==1 );
88918 pTask->bDone = 0;
88919 pTask->pThread = 0;
88932 assert( pTask->pThread==0 && pTask->bDone==0 );
88933 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
88938 ** level-0 PMAs.
88947 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
88950 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
88951 for(i=pSorter->nTask-1; i>=0; i--){
88952 SortSubtask *pTask = &pSorter->aTask[i];
88982 pNew->nTree = N;
88983 pNew->pTask = 0;
88984 pNew->aReadr = (PmaReader*)&pNew[1];
88985 pNew->aTree = (int*)&pNew->aReadr[N];
88996 for(i=0; i<pMerger->nTree; i++){
88997 vdbePmaReaderClear(&pMerger->aReadr[i]);
89010 if( pIncr->bUseThread ){
89011 vdbeSorterJoinThread(pIncr->pTask);
89012 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
89013 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
89016 vdbeMergeEngineFree(pIncr->pMerger);
89027 assert( pSorter->bUseThreads || pSorter->pReader==0 );
89029 if( pSorter->pReader ){
89030 vdbePmaReaderClear(pSorter->pReader);
89031 sqlite3DbFree(db, pSorter->pReader);
89032 pSorter->pReader = 0;
89035 vdbeMergeEngineFree(pSorter->pMerger);
89036 pSorter->pMerger = 0;
89037 for(i=0; i<pSorter->nTask; i++){
89038 SortSubtask *pTask = &pSorter->aTask[i];
89040 pTask->pSorter = pSorter;
89042 if( pSorter->list.aMemory==0 ){
89043 vdbeSorterRecordFree(0, pSorter->list.pList);
89045 pSorter->list.pList = 0;
89046 pSorter->list.szPMA = 0;
89047 pSorter->bUsePMA = 0;
89048 pSorter->iMemory = 0;
89049 pSorter->mxKeysize = 0;
89050 sqlite3DbFree(db, pSorter->pUnpacked);
89051 pSorter->pUnpacked = 0;
89059 assert( pCsr->eCurType==CURTYPE_SORTER );
89060 pSorter = pCsr->uc.pSorter;
89063 sqlite3_free(pSorter->list.aMemory);
89065 pCsr->uc.pSorter = 0;
89071 ** The first argument is a file-handle open on a temporary file. The file
89080 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
89094 ** Allocate space for a file-handle and open a temporary file. If successful,
89095 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
89105 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
89122 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
89126 if( pTask->pUnpacked==0 ){
89127 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
89128 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
89129 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
89130 pTask->pUnpacked->errCode = 0;
89151 res = pTask->xCompare(
89152 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
89157 pp = &p1->u.pNext;
89158 p1 = p1->u.pNext;
89165 pp = &p2->u.pNext;
89166 p2 = p2->u.pNext;
89182 if( p->typeMask==SORTER_TYPE_INTEGER ){
89184 }else if( p->typeMask==SORTER_TYPE_TEXT ){
89191 ** Sort the linked list of records headed at pTask->pList. Return
89204 p = pList->pList;
89205 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
89214 if( pList->aMemory ){
89215 if( (u8*)p==pList->aMemory ){
89218 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
89219 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
89222 pNext = p->u.pNext;
89225 p->u.pNext = 0;
89239 pList->pList = p;
89242 assert( pTask->pUnpacked->errCode==SQLITE_OK
89243 || pTask->pUnpacked->errCode==SQLITE_NOMEM
89245 return pTask->pUnpacked->errCode;
89249 ** Initialize a PMA-writer object.
89258 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
89259 if( !p->aBuffer ){
89260 p->eFWErr = SQLITE_NOMEM_BKPT;
89262 p->iBufEnd = p->iBufStart = (iStart % nBuf);
89263 p->iWriteOff = iStart - p->iBufStart;
89264 p->nBuffer = nBuf;
89265 p->pFd = pFd;
89275 while( nRem>0 && p->eFWErr==0 ){
89277 if( nCopy>(p->nBuffer - p->iBufEnd) ){
89278 nCopy = p->nBuffer - p->iBufEnd;
89281 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
89282 p->iBufEnd += nCopy;
89283 if( p->iBufEnd==p->nBuffer ){
89284 p->eFWErr = sqlite3OsWrite(p->pFd,
89285 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
89286 p->iWriteOff + p->iBufStart
89288 p->iBufStart = p->iBufEnd = 0;
89289 p->iWriteOff += p->nBuffer;
89291 assert( p->iBufEnd<p->nBuffer );
89293 nRem -= nCopy;
89298 ** Flush any buffered data to disk and clean up the PMA-writer object.
89299 ** The results of using the PMA-writer after this call are undefined.
89308 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
89309 p->eFWErr = sqlite3OsWrite(p->pFd,
89310 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
89311 p->iWriteOff + p->iBufStart
89314 *piEof = (p->iWriteOff + p->iBufEnd);
89315 sqlite3_free(p->aBuffer);
89316 rc = p->eFWErr;
89333 ** Write the current contents of in-memory linked-list pList to a level-0
89334 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
89342 ** * One or more records packed end-to-end in order of ascending keys.
89347 sqlite3 *db = pTask->pSorter->db;
89352 /* Set iSz to the expected size of file pTask->file after writing the PMA.
89354 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
89359 assert( pList->szPMA>0 );
89362 if( pTask->file.pFd==0 ){
89363 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
89364 assert( rc!=SQLITE_OK || pTask->file.pFd );
89365 assert( pTask->file.iEof==0 );
89366 assert( pTask->nPMA==0 );
89371 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
89383 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
89384 pTask->file.iEof);
89385 pTask->nPMA++;
89386 vdbePmaWriteVarint(&writer, pList->szPMA);
89387 for(p=pList->pList; p; p=pNext){
89388 pNext = p->u.pNext;
89389 vdbePmaWriteVarint(&writer, p->nVal);
89390 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
89391 if( pList->aMemory==0 ) sqlite3_free(p);
89393 pList->pList = p;
89394 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
89398 assert( rc!=SQLITE_OK || pList->pList==0 );
89399 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
89415 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
89416 SortSubtask *pTask = pMerger->pTask;
89419 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
89430 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
89431 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
89433 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
89436 if( pReadr1->pFd==0 ){
89438 }else if( pReadr2->pFd==0 ){
89439 iRes = -1;
89441 iRes = pTask->xCompare(pTask, &bCached,
89442 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
89448 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
89453 ** was actually called above, then pTask->pUnpacked now contains
89462 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
89463 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
89466 if( pReadr1->pFd ) bCached = 0;
89467 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
89468 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
89471 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
89474 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
89479 ** The main routine for background threads that write level-0 PMAs.
89484 assert( pTask->bDone==0 );
89485 rc = vdbeSorterListToPMA(pTask, &pTask->list);
89486 pTask->bDone = 1;
89497 pSorter->bUsePMA = 1;
89498 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
89503 int nWorker = (pSorter->nTask-1);
89507 pSorter->bUsePMA = 1;
89509 /* Select a sub-task to sort and flush the current list of in-memory
89510 ** records to disk. If the sorter is running in multi-threaded mode,
89511 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
89512 ** the background thread from a sub-tasks previous turn is still running,
89513 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
89514 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
89515 ** sub-tasks are prefered as they use background threads - the final
89516 ** sub-task uses the main thread. */
89518 int iTest = (pSorter->iPrev + i + 1) % nWorker;
89519 pTask = &pSorter->aTask[iTest];
89520 if( pTask->bDone ){
89523 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
89529 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
89532 u8 *aMem = pTask->list.aMemory;
89535 assert( pTask->pThread==0 && pTask->bDone==0 );
89536 assert( pTask->list.pList==0 );
89537 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
89539 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
89540 pTask->list = pSorter->list;
89541 pSorter->list.pList = 0;
89542 pSorter->list.szPMA = 0;
89544 pSorter->list.aMemory = aMem;
89545 pSorter->nMemory = sqlite3MallocSize(aMem);
89546 }else if( pSorter->list.aMemory ){
89547 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
89548 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
89574 assert( pCsr->eCurType==CURTYPE_SORTER );
89575 pSorter = pCsr->uc.pSorter;
89576 getVarint32((const u8*)&pVal->z[1], t);
89578 pSorter->typeMask &= SORTER_TYPE_INTEGER;
89580 pSorter->typeMask &= SORTER_TYPE_TEXT;
89582 pSorter->typeMask = 0;
89590 ** If using the single large allocation mode (pSorter->aMemory!=0), then
89597 ** * The total memory allocated for the in-memory list is greater
89598 ** than (page-size * cache-size), or
89600 ** * The total memory allocated for the in-memory list is greater
89601 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
89603 nReq = pVal->n + sizeof(SorterRecord);
89604 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
89605 if( pSorter->mxPmaSize ){
89606 if( pSorter->list.aMemory ){
89607 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
89610 (pSorter->list.szPMA > pSorter->mxPmaSize)
89611 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
89616 pSorter->list.szPMA = 0;
89617 pSorter->iMemory = 0;
89618 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
89622 pSorter->list.szPMA += nPMA;
89623 if( nPMA>pSorter->mxKeysize ){
89624 pSorter->mxKeysize = nPMA;
89627 if( pSorter->list.aMemory ){
89628 int nMin = pSorter->iMemory + nReq;
89630 if( nMin>pSorter->nMemory ){
89632 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
89633 int nNew = pSorter->nMemory * 2;
89635 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
89638 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
89640 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
89641 pSorter->list.aMemory = aNew;
89642 pSorter->nMemory = nNew;
89645 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
89646 pSorter->iMemory += ROUND8(nReq);
89647 if( pSorter->list.pList ){
89648 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
89655 pNew->u.pNext = pSorter->list.pList;
89658 memcpy(SRVAL(pNew), pVal->z, pVal->n);
89659 pNew->nVal = pVal->n;
89660 pSorter->list.pList = pNew;
89666 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
89668 ** except that the number-of-bytes varint is omitted from the start.
89673 i64 iStart = pIncr->iStartOff;
89674 SorterFile *pOut = &pIncr->aFile[1];
89675 SortSubtask *pTask = pIncr->pTask;
89676 MergeEngine *pMerger = pIncr->pMerger;
89678 assert( pIncr->bEof==0 );
89682 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
89685 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
89686 int nKey = pReader->nKey;
89691 if( pReader->pFd==0 ) break;
89692 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
89696 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
89697 assert( pIncr->pMerger->pTask==pTask );
89698 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
89701 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
89710 ** multi-threaded IncrMerger objects.
89715 pIncr->pTask->bDone = 1;
89724 assert( pIncr->bUseThread );
89725 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
89735 ** For single-threaded objects, this is accomplished by literally reading
89736 ** keys from pIncr->pMerger and repopulating aFile[0].
89738 ** For multi-threaded objects, all that is required is to wait until the
89750 if( pIncr->bUseThread ){
89751 rc = vdbeSorterJoinThread(pIncr->pTask);
89754 SorterFile f0 = pIncr->aFile[0];
89755 pIncr->aFile[0] = pIncr->aFile[1];
89756 pIncr->aFile[1] = f0;
89760 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
89761 pIncr->bEof = 1;
89770 pIncr->aFile[0] = pIncr->aFile[1];
89771 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
89772 pIncr->bEof = 1;
89794 pIncr->pMerger = pMerger;
89795 pIncr->pTask = pTask;
89796 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
89797 pTask->file2.iEof += pIncr->mxSz;
89807 ** Set the "use-threads" flag on object pIncr.
89810 pIncr->bUseThread = 1;
89811 pIncr->pTask->file2.iEof -= pIncr->mxSz;
89818 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
89824 int iOut /* Store the result in pMerger->aTree[iOut] */
89832 assert( iOut<pMerger->nTree && iOut>0 );
89834 if( iOut>=(pMerger->nTree/2) ){
89835 i1 = (iOut - pMerger->nTree/2) * 2;
89838 i1 = pMerger->aTree[iOut*2];
89839 i2 = pMerger->aTree[iOut*2+1];
89842 p1 = &pMerger->aReadr[i1];
89843 p2 = &pMerger->aReadr[i2];
89845 if( p1->pFd==0 ){
89847 }else if( p2->pFd==0 ){
89850 SortSubtask *pTask = pMerger->pTask;
89853 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
89854 res = pTask->xCompare(
89855 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
89864 pMerger->aTree[iOut] = iRes;
89871 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
89911 int nTree = pMerger->nTree;
89913 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
89917 assert( pMerger->pTask==0 );
89918 pMerger->pTask = pTask;
89924 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
89927 ** on this PmaReader before any of the multi-threaded PmaReaders takes
89928 ** better advantage of multi-processor hardware. */
89929 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
89931 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
89936 for(i=pMerger->nTree-1; i>0; i--){
89939 return pTask->pUnpacked->errCode;
89944 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
89946 ** object at (pReadr->pIncr).
89949 ** in the sub-tree headed by pReadr are also initialized. Data is then
89954 ** to be a multi-threaded PmaReader and this function is being called in a
89955 ** background thread. In this case all PmaReaders in the sub-tree are
89963 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
89964 ** this entire function is being run by thread (pTask->thread), that will
89968 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
89969 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
89977 IncrMerger *pIncr = pReadr->pIncr;
89978 SortSubtask *pTask = pIncr->pTask;
89979 sqlite3 *db = pTask->pSorter->db;
89981 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
89984 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
89986 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
89987 ** requires two temp files to itself, whereas a single-threaded object
89988 ** only requires a region of pTask->file2. */
89990 int mxSz = pIncr->mxSz;
89992 if( pIncr->bUseThread ){
89993 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
89995 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
89999 /*if( !pIncr->bUseThread )*/{
90000 if( pTask->file2.pFd==0 ){
90001 assert( pTask->file2.iEof>0 );
90002 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
90003 pTask->file2.iEof = 0;
90006 pIncr->aFile[1].pFd = pTask->file2.pFd;
90007 pIncr->iStartOff = pTask->file2.iEof;
90008 pTask->file2.iEof += mxSz;
90014 if( rc==SQLITE_OK && pIncr->bUseThread ){
90016 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
90018 ** pIncr->pTask->thread.
90047 pReader->pIncr->pTask->bDone = 1;
90053 ** If the PmaReader passed as the first argument is not an incremental-reader
90054 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
90058 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
90064 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
90068 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
90069 if( pIncr->bUseThread ){
90071 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
90082 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
90083 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
90088 ** first PMA to read from pTask->file. Assuming no error occurs, it is
90096 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
90097 MergeEngine **ppOut /* OUT: New merge-engine */
90109 PmaReader *pReadr = &pNew->aReadr[i];
90110 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
90111 iOff = pReadr->iEof;
90128 ** nPMA<=16 -> TreeDepth() == 0
90129 ** nPMA<=256 -> TreeDepth() == 1
90130 ** nPMA<=65536 -> TreeDepth() == 2
90143 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
90171 PmaReader *pReadr = &p->aReadr[iIter];
90173 if( pReadr->pIncr==0 ){
90178 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
90182 p = pReadr->pIncr->pMerger;
90188 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
90197 ** that has already written two or more level-0 PMAs to one or more temp
90215 /* If the sorter uses more than one task, then create the top-level
90217 ** one PmaReader per sub-task. */
90218 assert( pSorter->bUseThreads || pSorter->nTask==1 );
90219 if( pSorter->nTask>1 ){
90220 pMain = vdbeMergeEngineNew(pSorter->nTask);
90225 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
90226 SortSubtask *pTask = &pSorter->aTask[iTask];
90227 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
90228 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
90230 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
90233 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
90234 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
90240 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
90241 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
90242 int nReader; /* Number of level-0 PMAs to merge */
90244 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
90255 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
90280 ** (for multi-threaded sorters) so that it can be used to iterate through
90287 SortSubtask *pTask0 = &pSorter->aTask[0];
90290 sqlite3 *db = pTask0->pSorter->db;
90293 for(i=0; i<pSorter->nTask; i++){
90294 pSorter->aTask[i].xCompare = xCompare;
90301 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
90302 if( pSorter->bUseThreads ){
90305 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
90309 pSorter->pReader = pReadr;
90313 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
90315 vdbeIncrMergerSetThreads(pReadr->pIncr);
90316 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
90318 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
90320 assert( pIncr->pTask!=pLast );
90323 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
90328 ** b) If it is using task (nTask-1), it is configured to run
90329 ** in single-threaded mode. This is important, as the
90333 PmaReader *p = &pMain->aReadr[iTask];
90334 assert( p->pIncr==0 || (
90335 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
90336 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
90350 pSorter->pMerger = pMain;
90371 assert( pCsr->eCurType==CURTYPE_SORTER );
90372 pSorter = pCsr->uc.pSorter;
90377 ** from the in-memory list. */
90378 if( pSorter->bUsePMA==0 ){
90379 if( pSorter->list.pList ){
90381 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
90388 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
90392 assert( pSorter->list.pList );
90402 assert( pSorter->pReader==0 );
90423 assert( pCsr->eCurType==CURTYPE_SORTER );
90424 pSorter = pCsr->uc.pSorter;
90425 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
90426 if( pSorter->bUsePMA ){
90427 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
90428 assert( pSorter->bUseThreads==0 || pSorter->pReader );
90429 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
90431 if( pSorter->bUseThreads ){
90432 rc = vdbePmaReaderNext(pSorter->pReader);
90433 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
90436 /*if( !pSorter->bUseThreads )*/ {
90438 assert( pSorter->pMerger!=0 );
90439 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
90440 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
90444 SorterRecord *pFree = pSorter->list.pList;
90445 pSorter->list.pList = pFree->u.pNext;
90446 pFree->u.pNext = 0;
90447 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
90448 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
90462 if( pSorter->bUsePMA ){
90465 if( pSorter->bUseThreads ){
90466 pReader = pSorter->pReader;
90469 /*if( !pSorter->bUseThreads )*/{
90470 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
90472 *pnKey = pReader->nKey;
90473 pKey = pReader->aKey;
90475 *pnKey = pSorter->list.pList->nVal;
90476 pKey = SRVAL(pSorter->list.pList);
90488 assert( pCsr->eCurType==CURTYPE_SORTER );
90489 pSorter = pCsr->uc.pSorter;
90494 pOut->n = nKey;
90496 memcpy(pOut->z, pKey, nKey);
90529 assert( pCsr->eCurType==CURTYPE_SORTER );
90530 pSorter = pCsr->uc.pSorter;
90531 r2 = pSorter->pUnpacked;
90532 pKeyInfo = pCsr->pKeyInfo;
90534 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
90536 r2->nField = nKeyCol;
90538 assert( r2->nField==nKeyCol );
90543 if( r2->aMem[i].flags & MEM_Null ){
90544 *pRes = -1;
90549 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
90567 ** This file contains code use to implement an in-memory rollback journal.
90568 ** The in-memory rollback journal is used to journal transactions for
90571 ** Update: The in-memory journal is also used to temporarily cache
90572 ** smaller journals that are not critical for power-loss recovery.
90590 ** The zChunk array is always at least 8 bytes in size - usually much more.
90607 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
90619 ** This structure is a subclass of sqlite3_file. Each open memory-journal
90624 int nChunkSize; /* In-memory chunk-size */
90628 FileChunk *pFirst; /* Head of in-memory chunk-list */
90638 ** Read data from the in-memory journal file. This is the implementation
90655 if( (iAmt+iOfst)>p->endpoint.iOffset ){
90660 assert( (iAmt+iOfst)<=p->endpoint.iOffset );
90661 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
90662 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
90664 for(pChunk=p->pFirst;
90665 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
90666 pChunk=pChunk->pNext
90668 iOff += p->nChunkSize;
90671 pChunk = p->readpoint.pChunk;
90675 iChunkOffset = (int)(iOfst%p->nChunkSize);
90677 int iSpace = p->nChunkSize - iChunkOffset;
90678 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
90679 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
90681 nRead -= iSpace;
90683 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
90684 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
90685 p->readpoint.pChunk = pChunk;
90696 for(pIter=p->pFirst; pIter; pIter=pNext){
90697 pNext = pIter->pNext;
90700 p->pFirst = 0;
90717 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
90719 nChunk = copy.endpoint.iOffset - iOff;
90721 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
90726 /* No error has occurred. Free the in-memory buffers. */
90732 ** the original before returning. This way, SQLite uses the in-memory
90733 ** journal data to roll back changes made to the internal page-cache
90757 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
90767 /* An in-memory journal file should only ever be appended to. Random
90769 ** the in-memory journal is being used by a connection using the
90770 ** atomic-write optimization. In this case the first 28 bytes of the
90772 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
90775 if( iOfst==0 && p->pFirst ){
90776 assert( p->nChunkSize>iAmt );
90777 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
90780 assert( iOfst>0 || p->pFirst==0 );
90784 FileChunk *pChunk = p->endpoint.pChunk;
90785 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
90786 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
90790 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
90794 pNew->pNext = 0;
90796 assert( p->pFirst );
90797 pChunk->pNext = pNew;
90799 assert( !p->pFirst );
90800 p->pFirst = pNew;
90802 p->endpoint.pChunk = pNew;
90805 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
90807 nWrite -= iSpace;
90808 p->endpoint.iOffset += iSpace;
90810 p->nSize = iAmt + iOfst;
90828 p->nSize = 0;
90829 p->endpoint.pChunk = 0;
90830 p->endpoint.iOffset = 0;
90831 p->readpoint.pChunk = 0;
90832 p->readpoint.iOffset = 0;
90850 ** syncing an in-memory journal is a no-op.
90862 *pSize = (sqlite_int64) p->endpoint.iOffset;
90897 ** all content is always stored in main-memory. Finally, if nSpill is a
90898 ** positive value, then the journal file is initially created in-memory
90912 /* Zero the file-handle object. If nSpill was passed zero, initialize
90915 ** made on the journal file-handle. */
90922 p->nChunkSize = nSpill;
90924 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
90925 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
90928 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
90929 p->nSpill = nSpill;
90930 p->flags = flags;
90931 p->zJournal = zName;
90932 p->pVfs = pVfs;
90937 ** Open an in-memory journal file.
90940 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
90947 ** in-memory-only journal file (i.e. is one that was opened with a +ve
90954 if( p->pMethod==&MemJournalMethods && (
90956 p->nSpill>0
90961 NEVER(p->nSpill>0)
90964 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
90974 ** The file-handle passed as the only argument is open on a journal file.
90979 return p->pMethods==&MemJournalMethods;
90984 ** pVfs to create the underlying on-disk files.
90987 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
91025 ** return from the top-level walk call.
91035 rc = pWalker->xExprCallback(pWalker, pExpr);
91038 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
91039 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
91040 if( pExpr->pRight ){
91041 pExpr = pExpr->pRight;
91044 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
91045 }else if( pExpr->x.pList ){
91046 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
91065 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
91066 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
91079 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
91080 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
91081 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
91082 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
91083 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
91084 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
91100 pSrc = p->pSrc;
91102 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
91103 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
91106 if( pItem->fg.isTabFunc
91107 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
91118 ** on the compound select chain, p->pPrior.
91123 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
91130 ** is a no-op returning WRC_Continue.
91135 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
91137 rc = pWalker->xSelectCallback(pWalker, p);
91144 if( pWalker->xSelectCallback2 ){
91145 pWalker->xSelectCallback2(pWalker, p);
91147 p = p->pPrior;
91179 ** is a helper function - a callback for the tree walker.
91182 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
91196 ** Turn the pExpr expression into an alias for the iCol-th column of the
91217 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
91222 Expr *pOrig; /* The iCol-th column of the result set */
91226 assert( iCol>=0 && iCol<pEList->nExpr );
91227 pOrig = pEList->a[iCol].pExpr;
91229 db = pParse->db;
91233 if( pExpr->op==TK_COLLATE ){
91234 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
91241 ** The pExpr->u.zToken might point into memory that will be freed by the
91248 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
91249 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
91250 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
91251 pExpr->flags |= EP_MemToken;
91266 for(k=0; k<pUsing->nId; k++){
91267 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
91309 ** pExpr->iDb Set the index in db->aDb[] of the database X
91311 ** pExpr->iTable Set to the cursor number for the table obtained
91313 ** pExpr->pTab Points to the Table structure of X.Y (even if
91315 ** pExpr->iColumn Set to the column number within the table.
91316 ** pExpr->op Set to TK_COLUMN.
91317 ** pExpr->pLeft Any expression this points to is deleted
91318 ** pExpr->pRight Any expression this points to is deleted.
91342 sqlite3 *db = pParse->db; /* The database connection */
91355 /* Initialize the node to no-match */
91356 pExpr->iTable = -1;
91357 pExpr->pTab = 0;
91365 testcase( pNC->ncFlags & NC_PartIdx );
91366 testcase( pNC->ncFlags & NC_IsCheck );
91367 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
91374 for(i=0; i<db->nDb; i++){
91375 assert( db->aDb[i].zDbSName );
91376 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
91377 pSchema = db->aDb[i].pSchema;
91384 /* Start at the inner-most context and move outward until a match is found */
91388 SrcList *pSrcList = pNC->pSrcList;
91391 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
91392 pTab = pItem->pTab;
91393 assert( pTab!=0 && pTab->zName!=0 );
91394 assert( pTab->nCol>0 );
91395 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
91397 pEList = pItem->pSelect->pEList;
91398 for(j=0; j<pEList->nExpr; j++){
91399 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
91403 pExpr->iColumn = j;
91409 if( zDb && pTab->pSchema!=pSchema ){
91413 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
91422 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
91423 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
91425 ** is for the right-hand table of a NATURAL JOIN or is in a
91429 if( pItem->fg.jointype & JT_NATURAL ) continue;
91430 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
91434 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
91435 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
91441 pExpr->iTable = pMatch->iCursor;
91442 pExpr->pTab = pMatch->pTab;
91444 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
91445 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
91448 pSchema = pExpr->pTab->pSchema;
91456 if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
91457 int op = pParse->eTriggerOp;
91460 pExpr->iTable = 1;
91461 pTab = pParse->pTriggerTab;
91463 pExpr->iTable = 0;
91464 pTab = pParse->pTriggerTab;
91471 pSchema = pTab->pSchema;
91473 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
91474 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
91475 if( iCol==pTab->iPKey ){
91476 iCol = -1;
91481 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
91482 /* IMP: R-51414-32910 */
91483 iCol = -1;
91485 if( iCol<pTab->nCol ){
91488 pExpr->affinity = SQLITE_AFF_INTEGER;
91489 }else if( pExpr->iTable==0 ){
91492 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
91496 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
91498 pExpr->iColumn = (i16)iCol;
91499 pExpr->pTab = pTab;
91512 && (pNC->ncFlags & NC_IdxExpr)==0
91514 && VisibleRowid(pMatch->pTab)
91517 pExpr->iColumn = -1;
91518 pExpr->affinity = SQLITE_AFF_INTEGER;
91523 ** might refer to an result-set alias. This happens, for example, when
91533 ** The ability to use an output result-set column in the WHERE, GROUP BY,
91539 if( (pEList = pNC->pEList)!=0
91543 for(j=0; j<pEList->nExpr; j++){
91544 char *zAs = pEList->a[j].zName;
91547 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
91548 assert( pExpr->x.pList==0 );
91549 assert( pExpr->x.pSelect==0 );
91550 pOrig = pEList->a[j].pExpr;
91551 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
91572 pNC = pNC->pNext;
91579 ** supplied) and the value of Z is enclosed in double-quotes, then
91584 ** Because no reference was made to outer contexts, the pNC->nRef
91588 pExpr->op = TK_STRING;
91589 pExpr->pTab = 0;
91607 pParse->checkSchema = 1;
91608 pTopNC->nErr++;
91615 ** then set the high-order bit of the bitmask.
91617 if( pExpr->iColumn>=0 && pMatch!=0 ){
91618 int n = pExpr->iColumn;
91619 testcase( n==BMS-1 );
91621 n = BMS-1;
91623 assert( pMatch->iCursor==pExpr->iTable );
91624 pMatch->colUsed |= ((Bitmask)1)<<n;
91629 sqlite3ExprDelete(db, pExpr->pLeft);
91630 pExpr->pLeft = 0;
91631 sqlite3ExprDelete(db, pExpr->pRight);
91632 pExpr->pRight = 0;
91633 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
91639 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
91645 pTopNC->nRef++;
91647 pTopNC = pTopNC->pNext;
91662 struct SrcList_item *pItem = &pSrc->a[iSrc];
91663 p->pTab = pItem->pTab;
91664 p->iTable = pItem->iCursor;
91665 if( p->pTab->iPKey==iCol ){
91666 p->iColumn = -1;
91668 p->iColumn = (ynVar)iCol;
91670 testcase( iCol==BMS-1 );
91671 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
91679 ** pNC->ncFlags values determined by validMask.
91688 if( (pNC->ncFlags & validMask)!=0 ){
91690 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
91692 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
91700 ** Return 1024 times this value. Or return -1 if p is not a floating point
91704 double r = -1.0;
91705 if( p->op!=TK_FLOAT ) return -1;
91706 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
91708 if( r>1.0 ) return -1;
91727 pNC = pWalker->u.pNC;
91729 pParse = pNC->pParse;
91730 assert( pParse==pWalker->pParse );
91733 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
91734 SrcList *pSrcList = pNC->pSrcList;
91736 for(i=0; i<pNC->pSrcList->nSrc; i++){
91737 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
91741 switch( pExpr->op ){
91749 SrcList *pSrcList = pNC->pSrcList;
91751 assert( pSrcList && pSrcList->nSrc==1 );
91752 pItem = pSrcList->a;
91753 assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
91754 pExpr->op = TK_COLUMN;
91755 pExpr->pTab = pItem->pTab;
91756 pExpr->iTable = pItem->iCursor;
91757 pExpr->iColumn = -1;
91758 pExpr->affinity = SQLITE_AFF_INTEGER;
91769 ** be one call to lookupName(). Then the compiler will in-line
91779 if( pExpr->op==TK_ID ){
91782 zColumn = pExpr->u.zToken;
91785 pRight = pExpr->pRight;
91786 if( pRight->op==TK_ID ){
91788 zTable = pExpr->pLeft->u.zToken;
91789 zColumn = pRight->u.zToken;
91791 assert( pRight->op==TK_DOT );
91792 zDb = pExpr->pLeft->u.zToken;
91793 zTable = pRight->pLeft->u.zToken;
91794 zColumn = pRight->pRight->u.zToken;
91803 ExprList *pList = pExpr->x.pList; /* The argument list */
91804 int n = pList ? pList->nExpr : 0; /* Number of arguments */
91811 u8 enc = ENC(pParse->db); /* The database encoding */
91814 zId = pExpr->u.zToken;
91816 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
91818 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
91825 is_agg = pDef->xFinalize!=0;
91826 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
91829 pExpr->iTable = exprProbability(pList->a[1].pExpr);
91830 if( pExpr->iTable<0 ){
91834 pNC->nErr++;
91837 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
91839 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
91840 ** short-hand for likelihood(X,0.0625).
91841 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
91843 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
91846 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
91851 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
91855 pDef->zName);
91856 pNC->nErr++;
91858 pExpr->op = TK_NULL;
91863 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
91869 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
91873 notValid(pParse, pNC, "non-deterministic functions",
91877 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
91879 pNC->nErr++;
91881 }else if( no_such_func && pParse->db->init.busy==0
91883 && pParse->explain==0
91887 pNC->nErr++;
91891 pNC->nErr++;
91893 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
91897 pExpr->op = TK_AGG_FUNCTION;
91898 pExpr->op2 = 0;
91899 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
91900 pExpr->op2++;
91901 pNC2 = pNC2->pNext;
91906 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
91907 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
91910 pNC->ncFlags |= NC_AllowAgg;
91912 /* FIX ME: Compute pExpr->affinity based on the expected return
91919 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
91922 testcase( pExpr->op==TK_IN );
91924 int nRef = pNC->nRef;
91926 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
91927 assert( pNC->nRef>=nRef );
91928 if( nRef!=pNC->nRef ){
91930 pNC->ncFlags |= NC_VarSelect;
91949 if( pParse->db->mallocFailed ) break;
91950 assert( pExpr->pLeft!=0 );
91951 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
91952 if( pExpr->op==TK_BETWEEN ){
91953 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
91955 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
91958 assert( pExpr->pRight!=0 );
91959 nRight = sqlite3ExprVectorSize(pExpr->pRight);
91962 testcase( pExpr->op==TK_EQ );
91963 testcase( pExpr->op==TK_NE );
91964 testcase( pExpr->op==TK_LT );
91965 testcase( pExpr->op==TK_LE );
91966 testcase( pExpr->op==TK_GT );
91967 testcase( pExpr->op==TK_GE );
91968 testcase( pExpr->op==TK_IS );
91969 testcase( pExpr->op==TK_ISNOT );
91970 testcase( pExpr->op==TK_BETWEEN );
91976 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
91983 ** to the AS-name of one of the terms of the expression list. If it is,
92000 if( pE->op==TK_ID ){
92001 char *zCol = pE->u.zToken;
92002 for(i=0; i<pEList->nExpr; i++){
92003 char *zAs = pEList->a[i].zName;
92021 ** Attempt to match pE against result set columns in the left-most
92023 ** as an indication to the caller that it should sort by the i-th column.
92024 ** The left-most column is 1. In other words, the value returned is the
92028 ** If there is no match, return 0. Return -1 if an error occurs.
92040 u8 savedSuppErr; /* Saved value of db->suppressErr */
92043 pEList = pSelect->pEList;
92049 nc.pSrcList = pSelect->pSrc;
92053 db = pParse->db;
92054 savedSuppErr = db->suppressErr;
92055 db->suppressErr = 1;
92057 db->suppressErr = savedSuppErr;
92061 ** in the result set. Return an 1-based index of the matching
92062 ** result-set entry.
92064 for(i=0; i<pEList->nExpr; i++){
92065 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
92075 ** Generate an ORDER BY or GROUP BY term out-of-range error.
92080 int i, /* The index (1-based) of the term out of range */
92084 "%r %s BY term out of range - should be "
92097 ** beginning with the left-most SELECT and working toward the right.
92113 pOrderBy = pSelect->pOrderBy;
92115 db = pParse->db;
92116 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
92120 for(i=0; i<pOrderBy->nExpr; i++){
92121 pOrderBy->a[i].done = 0;
92123 pSelect->pNext = 0;
92124 while( pSelect->pPrior ){
92125 pSelect->pPrior->pNext = pSelect;
92126 pSelect = pSelect->pPrior;
92131 pEList = pSelect->pEList;
92133 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
92134 int iCol = -1;
92136 if( pItem->done ) continue;
92137 pE = sqlite3ExprSkipCollate(pItem->pExpr);
92139 if( iCol<=0 || iCol>pEList->nExpr ){
92140 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
92147 if( !db->mallocFailed ){
92159 pNew->flags |= EP_IntValue;
92160 pNew->u.iValue = iCol;
92161 if( pItem->pExpr==pE ){
92162 pItem->pExpr = pNew;
92164 Expr *pParent = pItem->pExpr;
92165 assert( pParent->op==TK_COLLATE );
92166 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
92167 assert( pParent->pLeft==pE );
92168 pParent->pLeft = pNew;
92171 pItem->u.x.iOrderByCol = (u16)iCol;
92172 pItem->done = 1;
92177 pSelect = pSelect->pNext;
92179 for(i=0; i<pOrderBy->nExpr; i++){
92180 if( pOrderBy->a[i].done==0 ){
92197 ** return non-zero. Return zero if no errors are seen.
92206 sqlite3 *db = pParse->db;
92210 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
92211 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
92215 pEList = pSelect->pEList;
92217 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
92218 if( pItem->u.x.iOrderByCol ){
92219 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
92220 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
92223 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
92236 ** If the order-by term is an integer I between 1 and N (where N is the
92238 ** in the resolution is a copy of the I-th result-set expression. If
92239 ** the order-by term is an identifier that corresponds to the AS-name of
92240 ** a result-set expression, then the term resolves to a copy of the
92241 ** result-set expression. Otherwise, the expression is resolved in
92242 ** the usual way - using sqlite3ResolveExprNames().
92261 nResult = pSelect->pEList->nExpr;
92262 pParse = pNC->pParse;
92263 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
92264 Expr *pE = pItem->pExpr;
92267 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
92269 /* If an AS-name match is found, mark this ORDER BY column as being
92270 ** a copy of the iCol-th result-set column. The subsequent call to
92272 ** copy of the iCol-th result-set expression. */
92273 pItem->u.x.iOrderByCol = (u16)iCol;
92280 ** order-by term to a copy of the result-set expression */
92285 pItem->u.x.iOrderByCol = (u16)iCol;
92290 pItem->u.x.iOrderByCol = 0;
92294 for(j=0; j<pSelect->pEList->nExpr; j++){
92295 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
92296 pItem->u.x.iOrderByCol = j+1;
92314 Select *pLeftmost; /* Left-most of SELECT of a compound */
92319 if( p->selFlags & SF_Resolved ){
92322 pOuterNC = pWalker->u.pNC;
92323 pParse = pWalker->pParse;
92324 db = pParse->db;
92334 if( (p->selFlags & SF_Expanded)==0 ){
92336 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
92339 isCompound = p->pPrior!=0;
92343 assert( (p->selFlags & SF_Expanded)!=0 );
92344 assert( (p->selFlags & SF_Resolved)==0 );
92345 p->selFlags |= SF_Resolved;
92352 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
92358 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
92359 ** as if it were part of the sub-query, not the parent. This block
92360 ** moves the pOrderBy down to the sub-query. It will be moved back
92362 if( p->selFlags & SF_Converted ){
92363 Select *pSub = p->pSrc->a[0].pSelect;
92364 assert( p->pSrc->nSrc==1 && p->pOrderBy );
92365 assert( pSub->pPrior && pSub->pOrderBy==0 );
92366 pSub->pOrderBy = p->pOrderBy;
92367 p->pOrderBy = 0;
92372 for(i=0; i<p->pSrc->nSrc; i++){
92373 struct SrcList_item *pItem = &p->pSrc->a[i];
92374 if( pItem->pSelect ){
92377 const char *zSavedContext = pParse->zAuthContext;
92381 ** pItem->pSelect, check if this value has changed. If so, then
92382 ** SELECT statement pItem->pSelect must be correlated. Set the
92383 ** pItem->fg.isCorrelated flag if this is the case. */
92384 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
92386 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
92387 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
92388 pParse->zAuthContext = zSavedContext;
92389 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
92391 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
92392 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
92393 pItem->fg.isCorrelated = (nRef!=0);
92397 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
92398 ** resolve the result-set expression list.
92401 sNC.pSrcList = p->pSrc;
92405 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
92407 /* If there are no aggregate functions in the result-set, and no GROUP BY
92410 assert( (p->selFlags & SF_Aggregate)==0 );
92411 pGroupBy = p->pGroupBy;
92414 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
92421 if( p->pHaving && !pGroupBy ){
92426 /* Add the output column list to the name-context before parsing the
92432 ** re-evaluated for each reference to it.
92434 sNC.pEList = p->pEList;
92435 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
92436 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
92438 /* Resolve names in table-valued-function arguments */
92439 for(i=0; i<p->pSrc->nSrc; i++){
92440 struct SrcList_item *pItem = &p->pSrc->a[i];
92441 if( pItem->fg.isTabFunc
92442 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
92455 ** the sub-query back to the parent query. At this point each term
92459 if( p->selFlags & SF_Converted ){
92460 Select *pSub = p->pSrc->a[0].pSelect;
92461 p->pOrderBy = pSub->pOrderBy;
92462 pSub->pOrderBy = 0;
92467 ** below, after all of the result-sets for all of the elements of
92470 ** If there is an ORDER BY clause on a term of a compound-select other
92471 ** than the right-most term, then that is a syntax error. But the error
92475 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
92476 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
92480 if( db->mallocFailed ){
92490 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
92493 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
92494 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
92504 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
92505 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
92511 p = p->pPrior;
92527 ** table columns and result-set columns. At the same time, do error
92535 ** the symbolic name assigned to an ATTACH-ed database.
92546 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
92550 ** To resolve result-set references, look for expression nodes of the
92551 ** form Z (with no X and Y prefix) where the Z matches the right-hand
92552 ** size of an AS clause in the result-set of a SELECT. The Z expression
92553 ** is replaced by a copy of the left-hand side of the result-set expression.
92554 ** Table-name and function resolution occurs on the substituted expression
92581 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
92582 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
92583 w.pParse = pNC->pParse;
92589 w.pParse->nHeight += pExpr->nHeight;
92590 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
92596 w.pParse->nHeight -= pExpr->nHeight;
92598 if( pNC->ncFlags & NC_HasAgg ){
92601 pNC->ncFlags |= savedHasAgg;
92602 return pNC->nErr>0 || w.pParse->nErr>0;
92616 for(i=0; i<pList->nExpr; i++){
92617 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
92625 ** decendents of the SELECT, including compounds off of p->pPrior,
92658 ** is set to -1 and the Expr.iColumn value is set to the column number.
92669 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
92670 NameContext sNC; /* Name context for pParse->pNewTable */
92676 sSrc.a[0].zName = pTab->zName;
92678 sSrc.a[0].iCursor = -1;
92712 assert( iCol<pTab->nCol );
92713 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
92720 ** or a sub-select with a column as the return value, then the
92735 if( pExpr->flags & EP_Generic ) return 0;
92736 op = pExpr->op;
92738 assert( pExpr->flags&EP_xIsSelect );
92739 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
92741 if( op==TK_REGISTER ) op = pExpr->op2;
92745 return sqlite3AffinityType(pExpr->u.zToken, 0);
92748 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
92749 return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
92752 assert( pExpr->pLeft->flags&EP_xIsSelect );
92754 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
92757 return pExpr->affinity;
92765 ** If a memory allocation error occurs, that fact is recorded in pParse->db
92774 if( pCollName->n>0 ){
92775 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
92777 pNew->pLeft = pExpr;
92778 pNew->flags |= EP_Collate|EP_Skip;
92799 assert( pExpr->x.pList->nExpr>0 );
92800 assert( pExpr->op==TK_FUNCTION );
92801 pExpr = pExpr->x.pList->a[0].pExpr;
92803 assert( pExpr->op==TK_COLLATE );
92804 pExpr = pExpr->pLeft;
92825 sqlite3 *db = pParse->db;
92829 int op = p->op;
92830 if( p->flags & EP_Generic ) break;
92832 p = p->pLeft;
92835 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
92836 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
92841 && p->pTab!=0
92843 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
92845 int j = p->iColumn;
92847 const char *zColl = p->pTab->aCol[j].zColl;
92852 if( p->flags & EP_Collate ){
92853 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
92854 p = p->pLeft;
92856 Expr *pNext = p->pRight;
92858 assert( p->x.pList==0 || p->pRight==0 );
92859 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
92860 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
92862 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
92864 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
92865 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
92866 pNext = p->x.pList->a[i].pExpr;
92895 if( p==0 ) p = pParse->db->pDfltColl;
92906 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
92943 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
92944 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
92945 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
92946 assert( pExpr->pLeft );
92947 aff = sqlite3ExprAffinity(pExpr->pLeft);
92948 if( pExpr->pRight ){
92949 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
92951 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
93005 if( pLeft->flags & EP_Collate ){
93007 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
93036 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
93038 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
93058 ** is a sub-select, return the number of columns in the sub-select. For
93062 u8 op = pExpr->op;
93063 if( op==TK_REGISTER ) op = pExpr->op2;
93065 return pExpr->x.pList->nExpr;
93067 return pExpr->x.pSelect->pEList->nExpr;
93074 ** Return a pointer to a subexpression of pVector that is the i-th
93084 ** just the expression for the i-th term of the result set, and may
93091 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
93092 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
93093 return pVector->x.pSelect->pEList->a[i].pExpr;
93095 return pVector->x.pList->a[i].pExpr;
93104 ** the iField-th column of the vector expression pVector.
93124 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
93128 if( pVector->op==TK_SELECT ){
93129 assert( pVector->flags & EP_xIsSelect );
93136 ** pLeft->iTable: First in an array of register holding result, or 0
93148 pRet->iColumn = iField;
93149 pRet->pLeft = pVector;
93151 assert( pRet==0 || pRet->iTable==0 );
93153 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
93154 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
93162 ** sub-select returns more than one column, the first in an array
93170 if( pExpr->op==TK_SELECT ){
93178 ** Argument pVector points to a vector expression - either a TK_VECTOR
93186 ** containing the results of the sub-select.
93203 u8 op = pVector->op;
93207 return pVector->iTable+iField;
93210 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
93213 *ppExpr = pVector->x.pList->a[iField].pExpr;
93224 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
93225 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
93226 ** otherwise: op==pExpr->op and p5==0
93235 Vdbe *v = pParse->pVdbe;
93236 Expr *pLeft = pExpr->pLeft;
93237 Expr *pRight = pExpr->pRight;
93249 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
93250 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
93251 || pExpr->op==TK_LT || pExpr->op==TK_GT
93252 || pExpr->op==TK_LE || pExpr->op==TK_GE
93254 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
93255 || (pExpr->op==TK_ISNOT && op==TK_NE) );
93256 assert( p5==0 || pExpr->op!=op );
93257 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
93284 if( i==nLeft-1 ){
93300 if( i==nLeft-2 ) opx = op;
93314 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
93335 if( p->nHeight>*pnHeight ){
93336 *pnHeight = p->nHeight;
93343 for(i=0; i<p->nExpr; i++){
93344 heightOfExpr(p->a[i].pExpr, pnHeight);
93350 for(p=pSelect; p; p=p->pPrior){
93351 heightOfExpr(p->pWhere, pnHeight);
93352 heightOfExpr(p->pHaving, pnHeight);
93353 heightOfExpr(p->pLimit, pnHeight);
93354 heightOfExprList(p->pEList, pnHeight);
93355 heightOfExprList(p->pGroupBy, pnHeight);
93356 heightOfExprList(p->pOrderBy, pnHeight);
93372 heightOfExpr(p->pLeft, &nHeight);
93373 heightOfExpr(p->pRight, &nHeight);
93375 heightOfSelect(p->x.pSelect, &nHeight);
93376 }else if( p->x.pList ){
93377 heightOfExprList(p->x.pList, &nHeight);
93378 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
93380 p->nHeight = nHeight + 1;
93392 if( pParse->nErr ) return;
93394 sqlite3ExprCheckHeight(pParse, p->nHeight);
93412 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
93413 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
93430 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
93434 ** can be translated into a 32-bit integer, then the token is not
93451 if( op!=TK_INTEGER || pToken->z==0
93452 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
93453 nExtra = pToken->n+1;
93460 pNew->op = (u8)op;
93461 pNew->iAgg = -1;
93464 pNew->flags |= EP_IntValue|EP_Leaf;
93465 pNew->u.iValue = iValue;
93467 pNew->u.zToken = (char*)&pNew[1];
93468 assert( pToken->z!=0 || pToken->n==0 );
93469 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
93470 pNew->u.zToken[pToken->n] = 0;
93471 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
93472 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
93473 sqlite3Dequote(pNew->u.zToken);
93478 pNew->nHeight = 1;
93485 ** Allocate a new expression node from a zero-terminated token that has
93512 assert( db->mallocFailed );
93517 pRoot->pRight = pRight;
93518 pRoot->flags |= EP_Propagate & pRight->flags;
93521 pRoot->pLeft = pLeft;
93522 pRoot->flags |= EP_Propagate & pLeft->flags;
93532 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
93542 if( op==TK_AND && pParse->nErr==0 ){
93543 /* Take advantage of short-circuit false optimization for AND */
93544 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
93546 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
93549 p->op = op & TKFLG_MASK;
93550 p->iAgg = -1;
93552 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
93555 sqlite3ExprCheckHeight(pParse, p->nHeight);
93566 pExpr->x.pSelect = pSelect;
93570 assert( pParse->db->mallocFailed );
93571 sqlite3SelectDelete(pParse->db, pSelect);
93579 ** expression at compile-time return 0.
93587 ** LEFT JOIN, then we cannot determine at compile-time whether or not
93633 sqlite3 *db = pParse->db;
93640 pNew->x.pList = pList;
93664 sqlite3 *db = pParse->db;
93670 z = pExpr->u.zToken;
93677 x = (ynVar)(++pParse->nVar);
93685 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
93686 i = z[1]-'0'; /* The common case of ?N for a single digit N */
93689 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
93693 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
93694 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
93695 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
93697 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
93701 if( x>pParse->nVar ){
93702 pParse->nVar = (int)x;
93704 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
93712 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
93714 x = (ynVar)(++pParse->nVar);
93719 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
93722 pExpr->iColumn = x;
93723 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
93733 /* Sanity check: Assert that the IntValue is non-negative if it exists */
93734 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
93737 assert( p->pLeft==0 );
93738 assert( p->pRight==0 );
93739 assert( p->x.pSelect==0 );
93744 assert( p->x.pList==0 || p->pRight==0 );
93745 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
93746 if( p->pRight ){
93747 sqlite3ExprDeleteNN(db, p->pRight);
93749 sqlite3SelectDelete(db, p->x.pSelect);
93751 sqlite3ExprListDelete(db, p->x.pList);
93754 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
93785 ** The dupedExprStructSize() function returns two values OR-ed together:
93798 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
93813 if( 0==flags || p->op==TK_SELECT_COLUMN ){
93820 if( p->pLeft || p->x.pList ){
93823 assert( p->pRight==0 );
93837 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
93838 nByte += sqlite3Strlen30(p->u.zToken)+1;
93861 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
93870 ** to store the copy of expression p, the copies of p->u.zToken
93871 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
93899 ** by the copy of the p->u.zToken string (if any).
93904 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
93905 nToken = sqlite3Strlen30(p->u.zToken) + 1;
93916 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
93921 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
93922 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
93923 pNew->flags |= staticFlag;
93925 /* Copy the p->u.zToken string, if any. */
93927 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
93928 memcpy(zToken, p->u.zToken, nToken);
93931 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
93932 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
93934 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
93936 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
93940 /* Fill in pNew->pLeft and pNew->pRight. */
93944 pNew->pLeft = p->pLeft ?
93945 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
93946 pNew->pRight = p->pRight ?
93947 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
93954 if( pNew->op==TK_SELECT_COLUMN ){
93955 pNew->pLeft = p->pLeft;
93956 assert( p->iColumn==0 || p->pRight==0 );
93957 assert( p->pRight==0 || p->pRight==p->pLeft );
93959 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
93961 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
93971 ** and the db->mallocFailed flag set.
93977 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
93981 pRet->nCte = p->nCte;
93982 for(i=0; i<p->nCte; i++){
93983 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
93984 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
93985 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
94010 ** part of the in-memory representation of the database schema.
94025 pNew->nExpr = p->nExpr;
94026 pItem = pNew->a;
94027 pOldItem = p->a;
94028 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
94029 Expr *pOldExpr = pOldItem->pExpr;
94031 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
94033 && pOldExpr->op==TK_SELECT_COLUMN
94034 && (pNewExpr = pItem->pExpr)!=0
94036 assert( pNewExpr->iColumn==0 || i>0 );
94037 if( pNewExpr->iColumn==0 ){
94038 assert( pOldExpr->pLeft==pOldExpr->pRight );
94039 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
94042 assert( pItem[-1].pExpr!=0 );
94043 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
94044 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
94045 pNewExpr->pLeft = pPriorSelectCol;
94048 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
94049 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
94050 pItem->sortOrder = pOldItem->sortOrder;
94051 pItem->done = 0;
94052 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
94053 pItem->u = pOldItem->u;
94072 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
94075 pNew->nSrc = pNew->nAlloc = p->nSrc;
94076 for(i=0; i<p->nSrc; i++){
94077 struct SrcList_item *pNewItem = &pNew->a[i];
94078 struct SrcList_item *pOldItem = &p->a[i];
94080 pNewItem->pSchema = pOldItem->pSchema;
94081 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
94082 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
94083 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
94084 pNewItem->fg = pOldItem->fg;
94085 pNewItem->iCursor = pOldItem->iCursor;
94086 pNewItem->addrFillSub = pOldItem->addrFillSub;
94087 pNewItem->regReturn = pOldItem->regReturn;
94088 if( pNewItem->fg.isIndexedBy ){
94089 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
94091 pNewItem->pIBIndex = pOldItem->pIBIndex;
94092 if( pNewItem->fg.isTabFunc ){
94093 pNewItem->u1.pFuncArg =
94094 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
94096 pTab = pNewItem->pTab = pOldItem->pTab;
94098 pTab->nTabRef++;
94100 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
94101 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
94102 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
94103 pNewItem->colUsed = pOldItem->colUsed;
94114 pNew->nId = p->nId;
94115 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
94116 if( pNew->a==0 ){
94120 /* Note that because the size of the allocation for p->a[] is not
94123 for(i=0; i<p->nId; i++){
94124 struct IdList_item *pNewItem = &pNew->a[i];
94125 struct IdList_item *pOldItem = &p->a[i];
94126 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
94127 pNewItem->idx = pOldItem->idx;
94138 for(p=pDup; p; p=p->pPrior){
94141 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
94142 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
94143 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
94144 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
94145 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
94146 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
94147 pNew->op = p->op;
94148 pNew->pNext = pNext;
94149 pNew->pPrior = 0;
94150 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
94151 pNew->iLimit = 0;
94152 pNew->iOffset = 0;
94153 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
94154 pNew->addrOpenEphm[0] = -1;
94155 pNew->addrOpenEphm[1] = -1;
94156 pNew->nSelectRow = p->nSelectRow;
94157 pNew->pWith = withDup(db, p->pWith);
94158 sqlite3SelectSetName(pNew, p->zSelName);
94160 pp = &pNew->pPrior;
94181 ** Reason: This routine assumes that the number of slots in pList->a[]
94186 ** NULL is returned. If non-NULL is returned, then it is guaranteed
94195 sqlite3 *db = pParse->db;
94202 pList->nExpr = 0;
94203 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
94206 sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
94212 pItem = &pList->a[pList->nExpr++];
94213 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
94215 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
94216 pItem->pExpr = pExpr;
94243 sqlite3 *db = pParse->db;
94246 int iFirst = pList ? pList->nExpr : 0;
94257 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
94259 pColumns->nId, n);
94263 for(i=0; i<pColumns->nId; i++){
94267 assert( pList->nExpr==iFirst+i+1 );
94268 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
94269 pColumns->a[i].zName = 0;
94273 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
94274 Expr *pFirst = pList->a[iFirst].pExpr;
94276 assert( pFirst->op==TK_SELECT_COLUMN );
94280 pFirst->pRight = pExpr;
94285 pFirst->iTable = pColumns->nId;
94300 assert( p->nExpr>0 );
94302 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
94305 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
94313 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
94322 assert( pList!=0 || pParse->db->mallocFailed!=0 );
94325 assert( pList->nExpr>0 );
94326 pItem = &pList->a[pList->nExpr-1];
94327 assert( pItem->zName==0 );
94328 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
94329 if( dequote ) sqlite3Dequote(pItem->zName);
94338 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
94347 sqlite3 *db = pParse->db;
94348 assert( pList!=0 || db->mallocFailed!=0 );
94350 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
94351 assert( pList->nExpr>0 );
94352 sqlite3DbFree(db, pItem->zSpan);
94353 pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
94366 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
94367 testcase( pEList && pEList->nExpr==mx );
94368 testcase( pEList && pEList->nExpr==mx+1 );
94369 if( pEList && pEList->nExpr>mx ){
94378 int i = pList->nExpr;
94379 struct ExprList_item *pItem = pList->a;
94380 assert( pList->nExpr>0 );
94382 sqlite3ExprDelete(db, pItem->pExpr);
94383 sqlite3DbFree(db, pItem->zName);
94384 sqlite3DbFree(db, pItem->zSpan);
94386 }while( --i>0 );
94394 ** Return the bitwise-OR of all Expr.flags fields in the given
94401 for(i=0; i<pList->nExpr; i++){
94402 Expr *pExpr = pList->a[i].pExpr;
94404 m |= pExpr->flags;
94410 ** This is a SELECT-node callback for the expression walker that
94412 ** pWalker->eCode to zero and abort.
94418 pWalker->eCode = 0;
94430 ** sqlite3ExprIsConstant() pWalker->eCode==1
94431 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
94432 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
94433 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
94449 /* If pWalker->eCode is 2 then any term of the expression that comes from
94452 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
94453 pWalker->eCode = 0;
94457 switch( pExpr->op ){
94459 ** and either pWalker->eCode==4 or 5 or the function has the
94462 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
94465 pWalker->eCode = 0;
94472 testcase( pExpr->op==TK_ID );
94473 testcase( pExpr->op==TK_COLUMN );
94474 testcase( pExpr->op==TK_AGG_FUNCTION );
94475 testcase( pExpr->op==TK_AGG_COLUMN );
94476 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
94481 testcase( pExpr->op==TK_IF_NULL_ROW );
94482 pWalker->eCode = 0;
94485 if( pWalker->eCode==5 ){
94489 pExpr->op = TK_NULL;
94490 }else if( pWalker->eCode==4 ){
94493 pWalker->eCode = 0;
94498 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail will disallow */
94499 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail will disallow */
94517 ** Walk an expression tree. Return non-zero if the expression is constant
94520 ** For the purposes of this function, a double-quoted string (ex: "abc")
94521 ** is considered a variable but a single-quoted string (ex: 'abc') is
94529 ** Walk an expression tree. Return non-zero if the expression is constant
94539 ** Walk an expression tree. Return non-zero if the expression is constant
94541 ** expression must not refer to any non-deterministic function nor any
94553 ExprList *pGroupBy = pWalker->u.pGroupBy;
94558 for(i=0; i<pGroupBy->nExpr; i++){
94559 Expr *p = pGroupBy->a[i].pExpr;
94560 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
94561 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
94562 if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){
94568 /* Check if pExpr is a sub-select. If so, consider it variable. */
94570 pWalker->eCode = 0;
94578 ** Walk the expression tree passed as the first argument. Return non-zero
94586 ** assumes that no other collating sequence will have a finer-grained
94608 ** Walk an expression tree. Return non-zero if the expression is constant
94612 ** For the purposes of this function, a double-quoted string (ex: "abc")
94613 ** is considered a variable but a single-quoted string (ex: 'abc') is
94641 ** to fit in a 32-bit integer, return 1 and put the value of the integer
94643 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
94649 /* If an expression is an integer literal that fits in a signed 32-bit
94651 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
94652 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
94654 if( p->flags & EP_IntValue ){
94655 *pValue = p->u.iValue;
94658 switch( p->op ){
94660 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
94665 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
94666 assert( v!=(-2147483647-1) );
94667 *pValue = -v;
94693 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
94694 op = p->op;
94695 if( op==TK_REGISTER ) op = p->op2;
94704 p->pTab==0 || /* Reference to column of index on expression */
94705 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
94724 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
94725 op = p->op;
94726 if( op==TK_REGISTER ) op = p->op2;
94741 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
94742 return p->iColumn<0
94752 ** Return TRUE if the given string is a row-id column name.
94777 p = pX->x.pSelect;
94778 if( p->pPrior ) return 0; /* Not a compound SELECT */
94779 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
94780 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
94781 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
94784 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
94785 if( p->pLimit ) return 0; /* Has no LIMIT clause */
94786 if( p->pWhere ) return 0; /* Has no WHERE clause */
94787 pSrc = p->pSrc;
94789 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
94790 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
94791 pTab = pSrc->a[0].pTab;
94793 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
94795 pEList = p->pEList;
94798 for(i=0; i<pEList->nExpr; i++){
94799 Expr *pRes = pEList->a[i].pExpr;
94800 if( pRes->op!=TK_COLUMN ) return 0;
94801 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
94809 ** Generate code that checks the left-most column of index table iCur to see if
94811 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
94829 ** right-hand side. Return TRUE if that list is constant.
94835 pLHS = pIn->pLeft;
94836 pIn->pLeft = 0;
94838 pIn->pLeft = pLHS;
94848 ** The job of this routine is to find or create a b-tree object that can
94852 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
94853 ** and pX->iTable is set to the index of that cursor.
94855 ** The returned value of this function indicates the b-tree type, as follows:
94857 ** IN_INDEX_ROWID - The cursor was opened on a database table.
94858 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
94859 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
94860 ** IN_INDEX_EPH - The cursor was opened on a specially created and
94862 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
94865 ** An existing b-tree might be used if the RHS expression pX is a simple
94872 ** pX->iTable made to point to the ephemeral table instead of an
94881 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
94882 ** through the set members) then the b-tree must not contain duplicates.
94884 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
94887 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
94890 ** index can be found with the specified <columns> as its left-most.
94894 ** routine might decide that creating an ephemeral b-tree for membership
94899 ** When the b-tree is being used for membership tests, the calling function
94908 ** the value in that register will be NULL if the b-tree contains one or more
94909 ** NULL values, and it will be some non-NULL value if the b-tree contains no
94926 Expr *pX, /* The right-hand side (RHS) of the IN operator */
94933 int iTab = pParse->nTab++; /* Cursor of the RHS table */
94937 assert( pX->op==TK_IN );
94945 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
94947 ExprList *pEList = pX->x.pSelect->pEList;
94948 for(i=0; i<pEList->nExpr; i++){
94949 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
94951 if( i==pEList->nExpr ){
94959 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
94960 sqlite3 *db = pParse->db; /* Database connection */
94963 ExprList *pEList = p->pEList;
94964 int nExpr = pEList->nExpr;
94966 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
94967 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
94968 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
94969 pTab = p->pSrc->a[0].pTab;
94972 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
94974 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
94977 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
94996 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
94997 int iCol = pEList->a[i].pExpr->iColumn;
95019 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
95022 if( pIdx->nColumn<nExpr ) continue;
95023 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
95025 testcase( pIdx->nColumn==BMS-2 );
95026 testcase( pIdx->nColumn==BMS-1 );
95027 if( pIdx->nColumn>=BMS-1 ) continue;
95029 if( pIdx->nKeyCol>nExpr
95030 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
95038 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
95039 Expr *pRhs = pEList->a[i].pExpr;
95043 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
95045 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
95046 assert( pIdx->azColl[j] );
95047 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
95059 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
95060 if( colUsed==(MASKBIT(nExpr)-1) ){
95065 sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
95068 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
95070 VdbeComment((v, "%s", pIdx->zName));
95072 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
95076 i64 mask = (1<<nExpr)-1;
95080 *prRhsHasNull = ++pParse->nMem;
95102 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
95108 /* Could not find an existing table or index to use as the RHS b-tree.
95111 u32 savedNQueryLoop = pParse->nQueryLoop;
95115 pParse->nQueryLoop = 0;
95116 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
95120 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
95123 pParse->nQueryLoop = savedNQueryLoop;
95125 pX->iTable = iTab;
95130 n = sqlite3ExprVectorSize(pX->pLeft);
95140 ** function allocates and returns a nul-terminated string containing
95147 Expr *pLeft = pExpr->pLeft;
95149 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
95152 assert( pExpr->op==TK_IN );
95153 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
95160 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
95176 ** "sub-select returns N columns - expected M"
95179 const char *zFmt = "sub-select returns %d columns - expected %d";
95186 ** it is not permitted. If pExpr is a sub-select vector, this routine
95189 ** "sub-select returns N columns - expected 1"
95197 if( pExpr->flags & EP_xIsSelect ){
95198 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
95210 ** (SELECT a FROM b) -- subquery
95211 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
95212 ** x IN (4,5,11) -- IN operator with list on right-hand side
95213 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
95218 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
95220 ** to some integer key column of a table B-Tree. In this case, use an
95221 ** intkey B-Tree to store the set of IN(...) values instead of the usual
95222 ** (slower) variable length keys B-Tree.
95224 ** If rMayHaveNull is non-zero, that means that the operation is an IN
95228 ** value to non-NULL if the RHS is NULL-free.
95231 ** result. For a multi-column SELECT, the result is stored in a contiguous
95232 ** array of registers and the return value is the register of the left-most
95242 int jmpIfDynamic = -1; /* One-time test address */
95251 ** * The right-hand side is a correlated subquery
95252 ** * The right-hand side is an expression list containing variables
95263 if( pParse->explain==2 ){
95264 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
95266 pExpr->op==TK_IN?"LIST":"SCALAR",
95267 pParse->iNextSelectId
95269 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
95273 switch( pExpr->op ){
95276 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
95296 pExpr->iTable = pParse->nTab++;
95298 pExpr->iTable, (isRowid?0:nVal));
95299 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
95307 Select *pSelect = pExpr->x.pSelect;
95308 ExprList *pEList = pSelect->pEList;
95313 if( ALWAYS(pEList->nExpr==nVal) ){
95316 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
95318 pSelect->iLimit = 0;
95319 testcase( pSelect->selFlags & SF_Distinct );
95322 sqlite3DbFree(pParse->db, dest.zAffSdst);
95326 sqlite3DbFree(pParse->db, dest.zAffSdst);
95329 assert( pEList->nExpr>0 );
95333 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
95334 pParse, p, pEList->a[i].pExpr
95338 }else if( ALWAYS(pExpr->x.pList!=0) ){
95348 ExprList *pList = pExpr->x.pList;
95358 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
95365 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
95366 Expr *pE2 = pItem->pExpr;
95371 ** this code only executes once. Because for a non-constant
95376 jmpIfDynamic = -1;
95381 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
95388 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
95392 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
95426 testcase( pExpr->op==TK_EXISTS );
95427 testcase( pExpr->op==TK_SELECT );
95428 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
95431 pSel = pExpr->x.pSelect;
95432 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
95433 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
95434 pParse->nMem += nReg;
95435 if( pExpr->op==TK_SELECT ){
95439 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
95446 pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
95447 if( pSel->pLimit ){
95448 sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
95449 pSel->pLimit->pLeft = pLimit;
95451 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
95453 pSel->iLimit = 0;
95464 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
95479 ** sub-select on the RHS of the IN() operator has the same number of
95481 ** a sub-query, that the LHS is a vector of size 1.
95484 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
95485 if( (pIn->flags & EP_xIsSelect) ){
95486 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
95487 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
95491 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
95505 ** The left-hand side (LHS) is a scalar or vector expression. The
95506 ** right-hand side (RHS) is an array of zero or more scalar values, or a
95521 ** See the separate in-operator.md documentation file in the canonical
95545 int addrTop; /* Top of the step-6 loop */
95547 pLeft = pExpr->pLeft;
95550 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
95552 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
95554 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
95557 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
95560 v = pParse->pVdbe;
95567 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
95572 ** nVector-1. */
95607 ** This is step (1) in the in-operator.md optimized algorithm.
95610 ExprList *pList = pExpr->x.pList;
95611 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
95621 for(ii=0; ii<pList->nExpr; ii++){
95622 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
95623 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
95626 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
95629 VdbeCoverageIf(v, ii<pList->nExpr-1);
95630 VdbeCoverageIf(v, ii==pList->nExpr-1);
95659 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
95666 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
95671 /* In this case, the RHS is the ROWID of table b-tree and so we also
95672 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
95674 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
95681 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
95686 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
95690 /* Step 4. If the RHS is known to be non-NULL and we did not find
95711 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
95726 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
95735 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
95751 sqlite3DbFree(pParse->db, aiMap);
95752 sqlite3DbFree(pParse->db, zAff);
95759 ** value described by z[0..n-1] into register iMem.
95761 ** The z[] string will probably not be zero-terminated. But the
95770 if( negateFlag ) value = -value;
95779 ** text z[0..n-1] into register iMem.
95781 ** Expr.u.zToken is always UTF8 and zero-terminated.
95784 Vdbe *v = pParse->pVdbe;
95785 if( pExpr->flags & EP_IntValue ){
95786 int i = pExpr->u.iValue;
95788 if( negFlag ) i = -i;
95793 const char *z = pExpr->u.zToken;
95798 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
95802 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
95810 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
95817 ** Erase column-cache entry number i
95820 if( pParse->aColCache[i].tempReg ){
95821 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
95822 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
95825 pParse->nColCache--;
95826 if( i<pParse->nColCache ){
95827 pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
95843 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
95844 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
95847 ** for testing only - to verify that SQLite always gets the same answer
95850 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
95858 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
95859 assert( p->iTable!=iTab || p->iColumn!=iCol );
95864 if( pParse->nColCache>=SQLITE_N_COLCACHE ){
95866 idxLru = -1;
95867 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
95868 if( p->lru<minLru ){
95870 minLru = p->lru;
95873 p = &pParse->aColCache[idxLru];
95875 p = &pParse->aColCache[pParse->nColCache++];
95879 p->iLevel = pParse->iCacheLevel;
95880 p->iTable = iTab;
95881 p->iColumn = iCol;
95882 p->iReg = iReg;
95883 p->tempReg = 0;
95884 p->lru = pParse->iCacheCnt++;
95888 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
95893 while( i<pParse->nColCache ){
95894 struct yColCache *p = &pParse->aColCache[i];
95895 if( p->iReg >= iReg && p->iReg < iReg+nReg ){
95909 pParse->iCacheLevel++;
95911 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
95912 printf("PUSH to %d\n", pParse->iCacheLevel);
95924 assert( pParse->iCacheLevel>=1 );
95925 pParse->iCacheLevel--;
95927 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
95928 printf("POP to %d\n", pParse->iCacheLevel);
95931 while( i<pParse->nColCache ){
95932 if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
95949 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
95950 if( p->iReg==iReg ){
95951 p->tempReg = 0;
95957 ** appropriate for the iIdxCol-th column of index pIdx.
95966 i16 iTabCol = pIdx->aiColumn[iIdxCol];
95968 assert( pIdx->aColExpr );
95969 assert( pIdx->aColExpr->nExpr>iIdxCol );
95970 pParse->iSelfTab = iTabCur + 1;
95971 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
95972 pParse->iSelfTab = 0;
95974 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
95980 ** Generate code to extract the value of the iCol-th column of a table.
95993 if( iCol<0 || iCol==pTab->iPKey ){
96009 ** Generate code that will extract the iColumn-th column from
96013 ** is not garanteeed for GetColumn() - the result can be stored in
96028 Vdbe *v = pParse->pVdbe;
96032 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
96033 if( p->iTable==iTable && p->iColumn==iColumn ){
96034 p->lru = pParse->iCacheCnt++;
96035 sqlite3ExprCachePinRegister(pParse, p->iReg);
96036 return p->iReg;
96056 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
96067 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
96071 for(i=0; i<pParse->nColCache; i++){
96072 if( pParse->aColCache[i].tempReg
96073 && pParse->nTempReg<ArraySize(pParse->aTempReg)
96075 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
96078 pParse->nColCache = 0;
96090 ** Generate code to move content from registers iFrom...iFrom+nReg-1
96091 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
96095 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
96110 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
96111 int r = p->iReg;
96125 p->op2 = p->op;
96126 p->op = TK_REGISTER;
96127 p->iTable = iReg;
96148 if( p->op==TK_SELECT ){
96156 iResult = pParse->nMem+1;
96157 pParse->nMem += nResult;
96159 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
96179 Vdbe *v = pParse->pVdbe; /* The VM under construction */
96182 int regFree1 = 0; /* If non-zero free this temporary register */
96183 int regFree2 = 0; /* If non-zero free this temporary register */
96188 assert( target>0 && target<=pParse->nMem );
96190 assert( pParse->db->mallocFailed );
96197 op = pExpr->op;
96201 AggInfo *pAggInfo = pExpr->pAggInfo;
96202 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
96203 if( !pAggInfo->directMode ){
96204 assert( pCol->iMem>0 );
96205 return pCol->iMem;
96206 }else if( pAggInfo->useSortingIdx ){
96207 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
96208 pCol->iSorterColumn, target);
96214 int iTab = pExpr->iTable;
96216 if( pParse->iSelfTab<0 ){
96218 return pExpr->iColumn - pParse->iSelfTab;
96222 iTab = pParse->iSelfTab - 1;
96225 return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
96226 pExpr->iColumn, iTab, target,
96227 pExpr->op2);
96236 codeReal(v, pExpr->u.zToken, 0, target);
96242 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
96255 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
96256 assert( pExpr->u.zToken[1]=='\'' );
96257 z = &pExpr->u.zToken[2];
96258 n = sqlite3Strlen30(z) - 1;
96267 assert( pExpr->u.zToken!=0 );
96268 assert( pExpr->u.zToken[0]!=0 );
96269 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
96270 if( pExpr->u.zToken[1]!=0 ){
96271 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
96272 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
96273 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
96279 return pExpr->iTable;
96284 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
96290 sqlite3AffinityType(pExpr->u.zToken, 0));
96300 /* fall-through */
96307 Expr *pLeft = pExpr->pLeft;
96312 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
96313 codeCompare(pParse, pLeft, pExpr->pRight, op,
96349 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
96350 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
96357 Expr *pLeft = pExpr->pLeft;
96359 if( pLeft->op==TK_INTEGER ){
96363 }else if( pLeft->op==TK_FLOAT ){
96365 codeReal(v, pLeft->u.zToken, 1, target);
96373 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
96383 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
96394 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
96404 AggInfo *pInfo = pExpr->pAggInfo;
96407 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
96409 return pInfo->aFunc[pExpr->iAgg].iMem;
96420 sqlite3 *db = pParse->db; /* The database connection */
96427 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
96433 pFarg = pExpr->x.pList;
96435 nFarg = pFarg ? pFarg->nExpr : 0;
96437 zId = pExpr->u.zToken;
96440 if( pDef==0 && pParse->explain ){
96444 if( pDef==0 || pDef->xFinalize!=0 ){
96449 /* Attempt a direct implementation of the built-in COALESCE() and
96451 ** arguments past the first non-NULL argument.
96453 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
96456 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
96462 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
96469 /* The UNLIKELY() function is a no-op. The result is the value
96472 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
96474 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
96482 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
96486 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
96488 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
96494 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
96498 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
96499 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
96504 r1 = pParse->nMem+1;
96505 pParse->nMem += nFarg;
96515 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
96518 assert( pFarg->a[0].pExpr!=0 );
96519 exprOp = pFarg->a[0].pExpr->op;
96523 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
96524 pFarg->a[0].pExpr->op2 =
96525 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
96549 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
96550 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
96552 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
96555 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
96556 if( !pColl ) pColl = db->pDfltColl;
96560 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
96561 Expr *pArg = pFarg->a[0].pExpr;
96562 if( pArg->op==TK_COLUMN ){
96563 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
96570 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
96585 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
96594 if( pExpr->pLeft->iTable==0 ){
96595 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
96597 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
96598 if( pExpr->iTable
96599 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
96602 pExpr->iTable, n);
96604 return pExpr->pLeft->iTable + pExpr->iColumn;
96627 ** X is stored in pExpr->pLeft.
96628 ** Y is stored in pExpr->pList->a[0].pExpr.
96629 ** Z is stored in pExpr->pList->a[1].pExpr.
96638 return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
96643 ** to a column in the new.* or old.* pseudo-tables available to
96645 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
96646 ** is set to the column of the pseudo-table to read, or to -1 to
96651 ** to reference another column of the old.* pseudo-table, where
96653 ** set to (n+1), where n is the number of columns in each pseudo-table.
96654 ** For a reference to any other column in the new.* pseudo-table, p1
96663 ** p1==0 -> old.rowid p1==3 -> new.rowid
96664 ** p1==1 -> old.a p1==4 -> new.a
96665 ** p1==2 -> old.b p1==5 -> new.b
96667 Table *pTab = pExpr->pTab;
96668 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
96670 assert( pExpr->iTable==0 || pExpr->iTable==1 );
96671 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
96672 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
96673 assert( p1>=0 && p1<(pTab->nCol*2+2) );
96676 VdbeComment((v, "%s.%s -> $%d",
96677 (pExpr->iTable ? "new" : "old"),
96678 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
96686 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
96688 if( pExpr->iColumn>=0
96689 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
96704 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
96706 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
96724 ** X (if it exists) is in pExpr->pLeft.
96725 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
96728 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
96744 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
96746 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
96747 assert(pExpr->x.pList->nExpr > 0);
96748 pEList = pExpr->x.pList;
96749 aListelem = pEList->a;
96750 nExpr = pEList->nExpr;
96752 if( (pX = pExpr->pLeft)!=0 ){
96754 testcase( pX->op==TK_COLUMN );
96762 ** The value in regFree1 might get SCopy-ed into the file result.
96767 for(i=0; i<nExpr-1; i=i+2){
96776 testcase( pTest->op==TK_COLUMN );
96778 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
96786 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
96791 assert( pParse->db->mallocFailed || pParse->nErr>0
96792 || pParse->iCacheLevel==iCacheLevel );
96798 assert( pExpr->affinity==OE_Rollback
96799 || pExpr->affinity==OE_Abort
96800 || pExpr->affinity==OE_Fail
96801 || pExpr->affinity==OE_Ignore
96803 if( !pParse->pTriggerTab ){
96805 "RAISE() may only be used within a trigger-program");
96808 if( pExpr->affinity==OE_Abort ){
96812 if( pExpr->affinity==OE_Ignore ){
96814 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
96818 pExpr->affinity, pExpr->u.zToken, 0, 0);
96846 p = pParse->pConstExpr;
96850 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
96851 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
96852 return pItem->u.iConstExprReg;
96856 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
96859 struct ExprList_item *pItem = &p->a[p->nExpr-1];
96860 pItem->reusable = regDest<0;
96861 if( regDest<0 ) regDest = ++pParse->nMem;
96862 pItem->u.iConstExprReg = regDest;
96864 pParse->pConstExpr = p;
96885 && pExpr->op!=TK_REGISTER
96889 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
96911 assert( target>0 && target<=pParse->nMem );
96912 if( pExpr && pExpr->op==TK_REGISTER ){
96913 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
96916 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
96917 if( inReg!=target && pParse->pVdbe ){
96918 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
96929 sqlite3 *db = pParse->db;
96931 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
96942 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
96962 Vdbe *v = pParse->pVdbe;
96966 assert( pExpr->op!=TK_REGISTER );
96968 iMem = ++pParse->nMem;
96978 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
97003 Vdbe *v = pParse->pVdbe;
97006 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
97007 n = pList->nExpr;
97009 for(pItem=pList->a, i=0; i<n; i++, pItem++){
97010 Expr *pExpr = pItem->pExpr;
97011 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
97013 i--;
97014 n--;
97016 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
97025 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
97026 && pOp->p1+pOp->p3+1==inReg
97027 && pOp->p2+pOp->p3+1==target+i
97029 pOp->p3++;
97078 exprX = *pExpr->pLeft;
97084 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
97087 compRight.pRight = pExpr->x.pList->a[1].pExpr;
97129 Vdbe *v = pParse->pVdbe;
97138 op = pExpr->op;
97143 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
97145 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
97152 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
97154 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
97160 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
97176 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
97178 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
97179 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
97180 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
97200 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
97227 /* No-op */
97252 Vdbe *v = pParse->pVdbe;
97262 /* The value of pExpr->op and op are related as follows:
97264 ** pExpr->op op
97265 ** --------- ----------
97275 ** For other values of pExpr->op, op is undefined and unused.
97280 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
97284 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
97285 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
97286 assert( pExpr->op!=TK_NE || op==OP_Eq );
97287 assert( pExpr->op!=TK_EQ || op==OP_Ne );
97288 assert( pExpr->op!=TK_LT || op==OP_Ge );
97289 assert( pExpr->op!=TK_LE || op==OP_Gt );
97290 assert( pExpr->op!=TK_GT || op==OP_Le );
97291 assert( pExpr->op!=TK_GE || op==OP_Lt );
97293 switch( pExpr->op ){
97296 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
97298 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
97305 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
97307 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
97314 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
97319 testcase( pExpr->op==TK_IS );
97320 testcase( pExpr->op==TK_ISNOT );
97321 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
97330 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
97332 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
97333 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
97334 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
97352 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
97381 /* no-op */
97402 sqlite3 *db = pParse->db;
97404 if( db->mallocFailed==0 ){
97414 ** If pExpr is a simple SQL value - an integer, real, string, blob
97415 ** or NULL value - then the VDBE currently being prepared is configured
97416 ** to re-prepare each time a new value is bound to variable pVar.
97419 ** same as that currently bound to variable pVar, non-zero is returned.
97428 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
97430 iVar = pVar->iColumn;
97431 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
97432 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
97435 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
97450 ** other than the top-level COLLATE operator.
97452 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
97464 ** this routine is used, it does not hurt to get an extra 2 - that
97469 ** pParse->pReprepare can be matched against literals in pB. The
97470 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
97480 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
97483 combinedFlags = pA->flags | pB->flags;
97485 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
97490 if( pA->op!=pB->op ){
97491 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
97494 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
97499 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
97500 if( pA->op==TK_FUNCTION ){
97501 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
97502 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
97503 return pA->op==TK_COLLATE ? 1 : 2;
97506 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
97509 if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
97510 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
97511 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
97512 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
97513 if( pA->iColumn!=pB->iColumn ) return 2;
97514 if( pA->iTable!=pB->iTable
97515 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
97523 ** non-zero if they differ in any way.
97525 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
97528 ** This routine might return non-zero for equivalent ExprLists. The
97534 ** always differs from a non-NULL pointer.
97540 if( pA->nExpr!=pB->nExpr ) return 1;
97541 for(i=0; i<pA->nExpr; i++){
97542 Expr *pExprA = pA->a[i].pExpr;
97543 Expr *pExprB = pB->a[i].pExpr;
97544 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
97551 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
97578 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
97590 if( pE2->op==TK_OR
97591 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
97592 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
97596 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
97597 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
97598 testcase( pX!=pE1->pLeft );
97599 if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
97618 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
97619 ** pWalker->u.pIdxCover->pIdx.
97622 if( pExpr->op==TK_COLUMN
97623 && pExpr->iTable==pWalker->u.pIdxCover->iCur
97624 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
97626 pWalker->eCode = 1;
97680 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
97682 struct SrcCount *p = pWalker->u.pSrcCount;
97683 SrcList *pSrc = p->pSrc;
97684 int nSrc = pSrc ? pSrc->nSrc : 0;
97686 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
97689 p->nThis++;
97691 p->nOther++;
97706 assert( pExpr->op==TK_AGG_FUNCTION );
97713 sqlite3WalkExprList(&w, pExpr->x.pList);
97718 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
97723 pInfo->aCol = sqlite3ArrayAllocate(
97725 pInfo->aCol,
97726 sizeof(pInfo->aCol[0]),
97727 &pInfo->nColumn,
97734 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
97739 pInfo->aFunc = sqlite3ArrayAllocate(
97741 pInfo->aFunc,
97742 sizeof(pInfo->aFunc[0]),
97743 &pInfo->nFunc,
97756 NameContext *pNC = pWalker->u.pNC;
97757 Parse *pParse = pNC->pParse;
97758 SrcList *pSrcList = pNC->pSrcList;
97759 AggInfo *pAggInfo = pNC->pAggInfo;
97761 switch( pExpr->op ){
97764 testcase( pExpr->op==TK_AGG_COLUMN );
97765 testcase( pExpr->op==TK_COLUMN );
97769 struct SrcList_item *pItem = pSrcList->a;
97770 for(i=0; i<pSrcList->nSrc; i++, pItem++){
97773 if( pExpr->iTable==pItem->iCursor ){
97777 ** Make an entry for the column in pAggInfo->aCol[] if there
97781 pCol = pAggInfo->aCol;
97782 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
97783 if( pCol->iTable==pExpr->iTable &&
97784 pCol->iColumn==pExpr->iColumn ){
97788 if( (k>=pAggInfo->nColumn)
97789 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
97791 pCol = &pAggInfo->aCol[k];
97792 pCol->pTab = pExpr->pTab;
97793 pCol->iTable = pExpr->iTable;
97794 pCol->iColumn = pExpr->iColumn;
97795 pCol->iMem = ++pParse->nMem;
97796 pCol->iSorterColumn = -1;
97797 pCol->pExpr = pExpr;
97798 if( pAggInfo->pGroupBy ){
97800 ExprList *pGB = pAggInfo->pGroupBy;
97801 struct ExprList_item *pTerm = pGB->a;
97802 n = pGB->nExpr;
97804 Expr *pE = pTerm->pExpr;
97805 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
97806 pE->iColumn==pExpr->iColumn ){
97807 pCol->iSorterColumn = j;
97812 if( pCol->iSorterColumn<0 ){
97813 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
97816 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
97819 ** pAggInfo->aCol[] entry.
97822 pExpr->pAggInfo = pAggInfo;
97823 pExpr->op = TK_AGG_COLUMN;
97824 pExpr->iAgg = (i16)k;
97826 } /* endif pExpr->iTable==pItem->iCursor */
97832 if( (pNC->ncFlags & NC_InAggFunc)==0
97833 && pWalker->walkerDepth==pExpr->op2
97838 struct AggInfo_func *pItem = pAggInfo->aFunc;
97839 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
97840 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
97844 if( i>=pAggInfo->nFunc ){
97845 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
97847 u8 enc = ENC(pParse->db);
97848 i = addAggInfoFunc(pParse->db, pAggInfo);
97851 pItem = &pAggInfo->aFunc[i];
97852 pItem->pExpr = pExpr;
97853 pItem->iMem = ++pParse->nMem;
97855 pItem->pFunc = sqlite3FindFunction(pParse->db,
97856 pExpr->u.zToken,
97857 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
97858 if( pExpr->flags & EP_Distinct ){
97859 pItem->iDistinct = pParse->nTab++;
97861 pItem->iDistinct = -1;
97865 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
97869 pExpr->iAgg = (i16)i;
97870 pExpr->pAggInfo = pAggInfo;
97881 pWalker->walkerDepth++;
97886 pWalker->walkerDepth--;
97891 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
97905 assert( pNC->pSrcList!=0 );
97919 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
97920 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
97929 if( pParse->nTempReg==0 ){
97930 return ++pParse->nMem;
97932 return pParse->aTempReg[--pParse->nTempReg];
97944 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
97947 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
97948 if( p->iReg==iReg ){
97949 p->tempReg = 1;
97953 pParse->aTempReg[pParse->nTempReg++] = iReg;
97963 i = pParse->iRangeReg;
97964 n = pParse->nRangeReg;
97966 assert( !usedAsColumnCache(pParse, i, i+n-1) );
97967 pParse->iRangeReg += nReg;
97968 pParse->nRangeReg -= nReg;
97970 i = pParse->nMem+1;
97971 pParse->nMem += nReg;
97981 if( nReg>pParse->nRangeReg ){
97982 pParse->nRangeReg = nReg;
97983 pParse->iRangeReg = iReg;
97991 pParse->nTempReg = 0;
97992 pParse->nRangeReg = 0;
98003 if( pParse->nRangeReg>0
98004 && pParse->iRangeReg+pParse->nRangeReg > iFirst
98005 && pParse->iRangeReg <= iLast
98009 for(i=0; i<pParse->nTempReg; i++){
98010 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
98051 ** -> 'CREATE TABLE def(a, b, c)'
98054 ** -> 'CREATE INDEX i ON def(a, b, c)'
98075 ** statement is that the table name is the first non-space token that
98099 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
98101 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
98118 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
98154 (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
98165 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
98237 /* Variable tname now contains the token that is the old table-name
98240 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
98242 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
98248 ** Register built-in functions used to help implement ALTER TABLE
98300 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
98301 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
98316 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
98318 /* If the table is not located in the temp-db (in which case NULL is
98320 ** that is not part of the temp-db schema, add a clause to the WHERE
98323 if( pTab->pSchema!=pTempSchema ){
98324 sqlite3 *db = pParse->db;
98325 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
98326 if( pTrig->pSchema==pTempSchema ){
98327 zWhere = whereOrName(db, zWhere, pTrig->zName);
98332 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
98333 sqlite3DbFree(pParse->db, zWhere);
98344 ** pTab->zName if this function is being called to code part of an
98357 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
98358 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
98363 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
98364 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
98366 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
98371 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
98374 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
98392 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
98416 char *zName = 0; /* NULL-terminated version of pName */
98417 sqlite3 *db = pParse->db; /* Database connection */
98418 int nTabName; /* Number of UTF-8 characters in zTabName */
98424 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
98425 u32 savedDbFlags; /* Saved value of db->mDbFlags */
98427 savedDbFlags = db->mDbFlags;
98428 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
98429 assert( pSrc->nSrc==1 );
98430 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
98432 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
98434 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
98435 zDb = db->aDb[iDb].zDbSName;
98436 db->mDbFlags |= DBFLAG_PreferBuiltin;
98454 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
98462 if( pTab->pSelect ){
98463 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
98470 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
98481 if( pVTab->pVtab->pModule->xRename==0 ){
98501 ** of any resources used by the v-table implementation (including other
98506 int i = ++pParse->nMem;
98513 /* figure out how many UTF-8 characters are in zName */
98514 zTabName = pTab->zName;
98515 nTabName = sqlite3Utf8CharLen(zTabName, -1);
98518 if( db->flags&SQLITE_ForeignKeys ){
98519 /* If foreign-key support is enabled, rewrite the CREATE TABLE
98564 zDb, zName, pTab->zName);
98584 if( db->flags&SQLITE_ForeignKeys ){
98586 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
98587 Table *pFrom = p->pFrom;
98589 reloadTableSchema(pParse, p->pFrom, pFrom->zName);
98601 db->mDbFlags = savedDbFlags;
98609 ** The Table structure pParse->pNewTable was extended to include
98613 Table *pNew; /* Copy of pParse->pNewTable */
98618 char *zCol; /* Null-terminated column definition */
98622 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
98625 db = pParse->db;
98626 if( pParse->nErr || db->mallocFailed ) return;
98628 pNew = pParse->pNewTable;
98632 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
98633 zDb = db->aDb[iDb].zDbSName;
98634 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
98635 pCol = &pNew->aCol[pNew->nCol-1];
98636 pDflt = pCol->pDflt;
98642 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
98651 assert( pDflt==0 || pDflt->op==TK_SPAN );
98652 if( pDflt && pDflt->pLeft->op==TK_NULL ){
98660 if( pCol->colFlags & COLFLAG_PRIMKEY ){
98664 if( pNew->pIndex ){
98668 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
98670 "Cannot add a REFERENCES column with non-NULL default value");
98673 if( pCol->notNull && !pDflt ){
98688 assert( db->mallocFailed == 1 );
98692 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
98699 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
98701 char *zEnd = &zCol[pColDef->n-1];
98702 u32 savedDbFlags = db->mDbFlags;
98704 *zEnd-- = '\0';
98706 db->mDbFlags |= DBFLAG_PreferBuiltin;
98711 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
98715 db->mDbFlags = savedDbFlags;
98725 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
98732 reloadTableSchema(pParse, pTab, pTab->zName);
98736 ** This function is called by the parser after the table-name in
98737 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
98738 ** pSrc is the full-name of the table being altered.
98757 sqlite3 *db = pParse->db;
98760 assert( pParse->pNewTable==0 );
98762 if( db->mallocFailed ) goto exit_begin_add_column;
98763 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
98774 if( pTab->pSelect ){
98778 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
98782 assert( pTab->addColOffset>0 );
98783 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
98794 pParse->pNewTable = pNew;
98795 pNew->nTabRef = 1;
98796 pNew->nCol = pTab->nCol;
98797 assert( pNew->nCol>0 );
98798 nAlloc = (((pNew->nCol-1)/8)*8)+8;
98799 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
98800 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
98801 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
98802 if( !pNew->aCol || !pNew->zName ){
98803 assert( db->mallocFailed );
98806 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
98807 for(i=0; i<pNew->nCol; i++){
98808 Column *pCol = &pNew->aCol[i];
98809 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
98810 pCol->zColl = 0;
98811 pCol->pDflt = 0;
98813 pNew->pSchema = db->aDb[iDb].pSchema;
98814 pNew->addColOffset = pTab->addColOffset;
98815 pNew->nTabRef = 1;
98832 ** 2005-07-08
98882 ** columns. The N-th integer (for N>1) is the average number of rows in
98883 ** the index which have the same value for the first N-1 columns. For
98884 ** a K-column index, there will be K+1 integers in the stat column. If
98908 ** inclusive are samples of the left-most key value in the index taken at
98915 ** For i between 0 and S-1. Conceptually, the index space is divided into
98942 ** of entries in the index whose left-most column exactly matches
98943 ** the left-most column of the sample. The second integer in nEq
98949 ** left-most column is less than the left-most column of the sample.
98950 ** The K-th integer in the nLt entry is the number of index entries
98965 ** looks at the left-most column of the index. The sqlite_stat3.sample
98966 ** column contains the actual value of the left-most column instead
98993 ** appropriate compile-time options are provided.
99026 sqlite3 *db = pParse->db;
99035 pDb = &db->aDb[iDb];
99043 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
99046 ** side-effect of the CREATE TABLE statement is to leave the rootpage
99047 ** of the new table in register pParse->regRoot. This is important
99050 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
99052 aRoot[i] = pParse->regRoot;
99059 aRoot[i] = pStat->tnum;
99065 pDb->zDbSName, zTab, zWhereType, zWhere
99068 }else if( db->xPreUpdateCallback ){
99069 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
99095 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
99123 u32 iPrn; /* Pseudo-random number used for sampling */
99138 if( p->nRowid ){
99139 sqlite3DbFree(db, p->u.aRowid);
99140 p->nRowid = 0;
99150 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
99151 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
99152 if( p->u.aRowid ){
99153 p->nRowid = n;
99154 memcpy(p->u.aRowid, pData, n);
99156 p->nRowid = 0;
99166 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
99167 p->nRowid = 0;
99168 p->u.iRowid = iRowid;
99178 pTo->isPSample = pFrom->isPSample;
99179 pTo->iCol = pFrom->iCol;
99180 pTo->iHash = pFrom->iHash;
99181 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
99182 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
99183 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
99184 if( pFrom->nRowid ){
99185 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
99187 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
99199 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
99200 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
99201 sampleClear(p->db, &p->current);
99203 sqlite3DbFree(p->db, p);
99270 p->db = db;
99271 p->nRow = 0;
99272 p->nCol = nCol;
99273 p->nKeyCol = nKeyCol;
99274 p->current.anDLt = (tRowcnt*)&p[1];
99275 p->current.anEq = &p->current.anDLt[nColUp];
99280 int i; /* Used to iterate through p->aSample[] */
99282 p->iGet = -1;
99283 p->mxSample = mxSample;
99284 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
99285 p->current.anLt = &p->current.anEq[nColUp];
99286 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
99289 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
99290 p->aBest = &p->a[mxSample];
99291 pSpace = (u8*)(&p->a[mxSample+nCol]);
99293 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
99294 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
99295 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
99297 assert( (pSpace - (u8*)p)==n );
99300 p->aBest[i].iCol = i;
99324 ** pNew and pOld are both candidate non-periodic samples selected for
99325 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
99332 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
99339 int nCol = pAccum->nCol;
99341 assert( pNew->iCol==pOld->iCol );
99342 for(i=pNew->iCol+1; i<nCol; i++){
99343 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
99344 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
99346 if( pNew->iHash>pOld->iHash ) return 1;
99356 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
99363 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
99364 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
99366 assert( pOld->isPSample==0 && pNew->isPSample==0 );
99367 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
99372 if( pNew->iCol<pOld->iCol ) return 1;
99373 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
99377 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
99382 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
99383 ** remove the least desirable sample from p->a[] to make room.
99396 if( nEqZero>p->nMaxEqZero ){
99397 p->nMaxEqZero = nEqZero;
99399 if( pNew->isPSample==0 ){
99401 assert( pNew->anEq[pNew->iCol]>0 );
99408 for(i=p->nSample-1; i>=0; i--){
99409 Stat4Sample *pOld = &p->a[i];
99410 if( pOld->anEq[pNew->iCol]==0 ){
99411 if( pOld->isPSample ) return;
99412 assert( pOld->iCol>pNew->iCol );
99420 pUpgrade->iCol = pNew->iCol;
99421 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
99428 if( p->nSample>=p->mxSample ){
99429 Stat4Sample *pMin = &p->a[p->iMin];
99430 tRowcnt *anEq = pMin->anEq;
99431 tRowcnt *anLt = pMin->anLt;
99432 tRowcnt *anDLt = pMin->anDLt;
99433 sampleClear(p->db, pMin);
99434 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
99435 pSample = &p->a[p->nSample-1];
99436 pSample->nRowid = 0;
99437 pSample->anEq = anEq;
99438 pSample->anDLt = anDLt;
99439 pSample->anLt = anLt;
99440 p->nSample = p->mxSample-1;
99443 /* The "rows less-than" for the rowid column must be greater than that
99444 ** for the last sample in the p->a[] array. Otherwise, the samples would
99447 assert( p->nSample==0
99448 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
99452 pSample = &p->a[p->nSample];
99454 p->nSample++;
99457 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
99462 if( p->nSample>=p->mxSample ){
99463 int iMin = -1;
99464 for(i=0; i<p->mxSample; i++){
99465 if( p->a[i].isPSample ) continue;
99466 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
99471 p->iMin = iMin;
99478 ** p->current contains a sample that reflects the previous row of the
99488 for(i=(p->nCol-2); i>=iChng; i--){
99489 Stat4Sample *pBest = &p->aBest[i];
99490 pBest->anEq[i] = p->current.anEq[i];
99491 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
99497 ** p->nMaxEqZero or greater set to zero. */
99498 for(i=p->nSample-1; i>=0; i--){
99500 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
99504 if( iChng<p->nMaxEqZero ){
99505 for(i=p->nSample-1; i>=0; i--){
99507 for(j=iChng; j<p->nCol; j++){
99508 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
99511 p->nMaxEqZero = iChng;
99517 tRowcnt nLt = p->current.anLt[0];
99518 tRowcnt nEq = p->current.anEq[0];
99521 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
99522 p->current.isPSample = 1;
99523 sampleInsert(p, &p->current, 0);
99524 p->current.isPSample = 0;
99527 /* Or if it is a non-periodic sample. Add it in this case too. */
99528 if( p->nSample<p->mxSample
99529 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
99531 sampleInsert(p, &p->current, 0);
99547 ** C Index of left-most column to differ from previous row
99571 assert( p->nCol>0 );
99572 assert( iChng<p->nCol );
99574 if( p->nRow==0 ){
99576 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
99584 p->current.anEq[i]++;
99586 for(i=iChng; i<p->nCol; i++){
99587 p->current.anDLt[i]++;
99589 p->current.anLt[i] += p->current.anEq[i];
99591 p->current.anEq[i] = 1;
99594 p->nRow++;
99597 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
99599 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
99602 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
99607 tRowcnt nLt = p->current.anLt[p->nCol-1];
99610 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
99611 p->current.isPSample = 1;
99612 p->current.iCol = 0;
99613 sampleInsert(p, &p->current, p->nCol-1);
99614 p->current.isPSample = 0;
99618 for(i=0; i<(p->nCol-1); i++){
99619 p->current.iCol = i;
99620 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
99621 sampleCopy(p, &p->aBest[i], &p->current);
99660 ** a one-parameter function, stat_get(P), that always returns the
99701 ** I = (K+D-1)/D
99706 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
99712 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
99714 for(i=0; i<p->nKeyCol; i++){
99715 u64 nDistinct = p->current.anDLt[i] + 1;
99716 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
99719 assert( p->current.anEq[i] );
99723 sqlite3_result_text(context, zRet, -1, sqlite3_free);
99727 if( p->iGet<0 ){
99729 p->iGet = 0;
99731 if( p->iGet<p->nSample ){
99732 Stat4Sample *pS = p->a + p->iGet;
99733 if( pS->nRowid==0 ){
99734 sqlite3_result_int64(context, pS->u.iRowid);
99736 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
99743 assert( p->iGet<p->nSample );
99745 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
99746 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
99748 aCnt = p->a[p->iGet].anDLt;
99749 p->iGet++;
99757 char *zRet = sqlite3MallocZero(p->nCol * 25);
99763 for(i=0; i<p->nCol; i++){
99768 z[-1] = '\0';
99769 sqlite3_result_text(context, zRet, -1, sqlite3_free);
99815 sqlite3 *db = pParse->db; /* Database handle */
99821 int jZeroRows = -1; /* Jump from here if number of rows is zero */
99839 pParse->nMem = MAX(pParse->nMem, iMem);
99844 if( pTab->tnum==0 ){
99848 if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
99853 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
99857 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
99858 db->aDb[iDb].zDbSName ) ){
99864 if( db->xPreUpdateCallback ){
99867 pStat1->zName = (char*)&pStat1[1];
99868 memcpy(pStat1->zName, "sqlite_stat1", 13);
99869 pStat1->nCol = 3;
99870 pStat1->iPKey = -1;
99871 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
99875 /* Establish a read-lock on the table at the shared-cache level.
99876 ** Open a read-only cursor on the table. Also allocate a cursor number
99879 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
99882 pParse->nTab = MAX(pParse->nTab, iTab);
99884 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
99886 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
99894 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
99896 nCol = pIdx->nKeyCol;
99897 zIdxName = pTab->zName;
99898 nColTest = nCol - 1;
99900 nCol = pIdx->nColumn;
99901 zIdxName = pIdx->zName;
99902 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
99907 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
99910 ** Pseudo-code for loop that calls stat_push():
99945 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
99947 /* Open a read-only cursor on the index being analyzed. */
99948 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
99949 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
99951 VdbeComment((v, "%s", pIdx->zName));
99967 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
100003 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
100004 /* For a single-column UNIQUE index, once we have found a non-NULL
100011 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
100030 sqlite3VdbeJumpHere(v, addrNextRow-1);
100051 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
100053 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
100054 for(j=0; j<pPk->nKeyCol; j++){
100055 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
100056 assert( k>=0 && k<pIdx->nColumn );
100058 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
100060 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
100061 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
100077 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
100094 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
100105 ** the previous loop. Thus the not-found jump of seekOp will never
100119 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
100133 VdbeComment((v, "%s", pTab->zName));
100143 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
100165 sqlite3 *db = pParse->db;
100166 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
100173 iStatCur = pParse->nTab;
100174 pParse->nTab += 3;
100176 iMem = pParse->nMem+1;
100177 iTab = pParse->nTab;
100179 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
100196 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
100197 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
100199 iStatCur = pParse->nTab;
100200 pParse->nTab += 3;
100202 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
100204 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
100206 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
100214 ** ANALYZE -- 1
100215 ** ANALYZE <database> -- 2
100216 ** ANALYZE ?<database>.?<tablename> -- 3
100223 sqlite3 *db = pParse->db;
100234 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
100242 for(i=0; i<db->nDb; i++){
100246 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
100253 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
100257 analyzeTable(pParse, pIdx->pTable, pIdx);
100265 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
100281 ** The first argument points to a nul-terminated string containing a
100305 v = v*10 + c - '0';
100324 pIndex->bUnordered = 0;
100325 pIndex->noSkipScan = 0;
100328 pIndex->bUnordered = 1;
100329 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
100330 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
100332 pIndex->noSkipScan = 1;
100335 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
100336 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
100351 ** argv[2] = results of analysis - on integer for each column
100368 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
100377 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
100383 int nCol = pIndex->nKeyCol+1;
100388 if( pIndex->aiRowEst==0 ){
100389 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
100390 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
100392 aiRowEst = pIndex->aiRowEst;
100394 pIndex->bUnordered = 0;
100395 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
100396 pIndex->hasStat1 = 1;
100397 if( pIndex->pPartIdxWhere==0 ){
100398 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
100399 pTable->tabFlags |= TF_HasStat1;
100403 fakeIdx.szIdxRow = pTable->szTabRow;
100407 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
100408 pTable->szTabRow = fakeIdx.szIdxRow;
100409 pTable->tabFlags |= TF_HasStat1;
100421 if( pIdx->aSample ){
100423 for(j=0; j<pIdx->nSample; j++){
100424 IndexSample *p = &pIdx->aSample[j];
100425 sqlite3DbFree(db, p->p);
100427 sqlite3DbFree(db, pIdx->aSample);
100429 if( db && db->pnBytesFreed==0 ){
100430 pIdx->nSample = 0;
100431 pIdx->aSample = 0;
100441 ** Populate the pIdx->aAvgEq[] array based on the samples currently
100442 ** stored in pIdx->aSample[].
100446 IndexSample *aSample = pIdx->aSample;
100447 IndexSample *pFinal = &aSample[pIdx->nSample-1];
100450 if( pIdx->nSampleCol>1 ){
100455 nCol = pIdx->nSampleCol-1;
100456 pIdx->aAvgEq[nCol] = 1;
100459 int nSample = pIdx->nSample;
100467 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
100468 nRow = pFinal->anLt[iCol];
100469 nDist100 = (i64)100 * pFinal->anDLt[iCol];
100470 nSample--;
100472 nRow = pIdx->aiRowEst[0];
100473 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
100475 pIdx->nRowEst0 = nRow;
100482 if( i==(pIdx->nSample-1)
100491 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
100494 pIdx->aAvgEq[iCol] = avgEq;
100540 IndexSample *pSample; /* A slot in pIdx->aSample[] */
100542 assert( db->lookaside.bDisable );
100547 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
100565 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
100566 /* Index.nSample is non-zero at this point if data has already been
100568 if( pIdx==0 || pIdx->nSample ) continue;
100570 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
100571 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
100572 nIdxCol = pIdx->nKeyCol;
100574 nIdxCol = pIdx->nColumn;
100577 pIdx->nSampleCol = nIdxCol;
100582 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
100583 if( pIdx->aSample==0 ){
100587 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
100588 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
100590 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
100591 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
100592 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
100594 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
100603 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
100618 nCol = pIdx->nSampleCol;
100624 pSample = &pIdx->aSample[pIdx->nSample];
100625 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
100626 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
100627 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
100635 pSample->n = sqlite3_column_bytes(pStmt, 4);
100636 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
100637 if( pSample->p==0 ){
100641 if( pSample->n ){
100642 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
100644 pIdx->nSample++;
100658 assert( db->lookaside.bDisable );
100695 ** If an OOM error occurs, this function always sets db->mallocFailed.
100704 Schema *pSchema = db->aDb[iDb].pSchema;
100706 assert( iDb>=0 && iDb<db->nDb );
100707 assert( db->aDb[iDb].pBt!=0 );
100711 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
100713 pTab->tabFlags &= ~TF_HasStat1;
100715 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
100717 pIdx->hasStat1 = 0;
100720 pIdx->aSample = 0;
100726 sInfo.zDatabase = db->aDb[iDb].zDbSName;
100740 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
100742 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
100748 db->lookaside.bDisable++;
100750 db->lookaside.bDisable--;
100752 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
100754 sqlite3_free(pIdx->aiRowEst);
100755 pIdx->aiRowEst = 0;
100808 if( pExpr->op!=TK_ID ){
100811 pExpr->op = TK_STRING;
100818 ** An SQL user-function registered to do the work of an ATTACH statement. The
100859 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100860 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
100861 db->aLimit[SQLITE_LIMIT_ATTACHED]
100865 for(i=0; i<db->nDb; i++){
100866 char *z = db->aDb[i].zDbSName;
100874 /* Allocate the new entry in the db->aDb[] array and initialize the schema
100877 if( db->aDb==db->aDbStatic ){
100878 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100880 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100882 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100885 db->aDb = aNew;
100886 pNew = &db->aDb[db->nDb];
100893 flags = db->openFlags;
100894 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100897 sqlite3_result_error(context, zErr, -1);
100903 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100905 db->nDb++;
100906 db->skipBtreeMutex = 0;
100912 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
100913 if( !pNew->pSchema ){
100915 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
100920 sqlite3BtreeEnter(pNew->pBt);
100921 pPager = sqlite3BtreePager(pNew->pBt);
100922 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
100923 sqlite3BtreeSecureDelete(pNew->pBt,
100924 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
100926 sqlite3BtreeSetPagerFlags(pNew->pBt,
100927 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100929 sqlite3BtreeLeave(pNew->pBt);
100931 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100932 pNew->zDbSName = sqlite3DbStrDup(db, zName);
100933 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100956 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
100962 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
100963 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
100972 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100984 if( newAuth<db->auth.authLevel ){
100990 int iDb = db->nDb - 1;
100992 if( db->aDb[iDb].pBt ){
100993 sqlite3BtreeClose(db->aDb[iDb].pBt);
100994 db->aDb[iDb].pBt = 0;
100995 db->aDb[iDb].pSchema = 0;
100998 db->nDb = iDb;
101014 sqlite3_result_error(context, zErrDyn, -1);
101021 ** An SQL user-function registered to do the work of an DETACH statement. The
101042 for(i=0; i<db->nDb; i++){
101043 pDb = &db->aDb[i];
101044 if( pDb->pBt==0 ) continue;
101045 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
101048 if( i>=db->nDb ){
101056 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
101061 sqlite3BtreeClose(pDb->pBt);
101062 pDb->pBt = 0;
101063 pDb->pSchema = 0;
101068 sqlite3_result_error(context, zErr, -1);
101087 sqlite3* db = pParse->db;
101090 if( pParse->nErr ) goto attach_end;
101105 if( pAuthArg->op==TK_STRING ){
101106 zAuthArg = pAuthArg->u.zToken;
101124 assert( v || db->mallocFailed );
101126 sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
101128 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
101129 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
101196 db = pParse->db;
101197 assert( db->nDb>iDb );
101198 pFix->pParse = pParse;
101199 pFix->zDb = db->aDb[iDb].zDbSName;
101200 pFix->pSchema = db->aDb[iDb].pSchema;
101201 pFix->zType = zType;
101202 pFix->pName = pName;
101203 pFix->bVarOnly = (iDb==1);
101217 ** pParse->zErrMsg and these routines return non-zero. If everything
101229 zDb = pFix->zDb;
101230 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
101231 if( pFix->bVarOnly==0 ){
101232 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
101233 sqlite3ErrorMsg(pFix->pParse,
101235 pFix->zType, pFix->pName, pItem->zDatabase);
101238 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
101239 pItem->zDatabase = 0;
101240 pItem->pSchema = pFix->pSchema;
101243 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
101244 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
101255 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
101258 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
101261 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
101264 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
101267 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
101270 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101273 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101276 pSelect = pSelect->pPrior;
101285 if( pExpr->op==TK_VARIABLE ){
101286 if( pFix->pParse->db->init.busy ){
101287 pExpr->op = TK_NULL;
101289 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
101295 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
101297 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
101299 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
101302 pExpr = pExpr->pLeft;
101313 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
101314 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
101328 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
101331 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
101334 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
101337 pStep = pStep->pNext;
101359 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
101406 ** means that the SQL statement will never-run - the sqlite3_exec() call
101422 sqlite3_mutex_enter(db->mutex);
101423 db->xAuth = (sqlite3_xauth)xAuth;
101424 db->pAuthArg = pArg;
101426 sqlite3_mutex_leave(db->mutex);
101431 ** Write an error message into pParse->zErrMsg that explains that the
101432 ** user-supplied authorization function returned an illegal value.
101436 pParse->rc = SQLITE_ERROR;
101454 sqlite3 *db = pParse->db; /* Database handle */
101455 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
101458 if( db->init.busy ) return SQLITE_OK;
101459 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
101461 ,db->auth.zAuthUser
101466 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
101468 pParse->rc = SQLITE_AUTH;
101490 sqlite3 *db = pParse->db;
101493 int iSrc; /* Index in pTabList->a[] of table being read */
101497 if( db->xAuth==0 ) return;
101498 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
101505 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
101506 if( pExpr->op==TK_TRIGGER ){
101507 pTab = pParse->pTriggerTab;
101510 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
101511 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
101512 pTab = pTabList->a[iSrc].pTab;
101517 iCol = pExpr->iColumn;
101521 assert( iCol<pTab->nCol );
101522 zCol = pTab->aCol[iCol].zName;
101523 }else if( pTab->iPKey>=0 ){
101524 assert( pTab->iPKey<pTab->nCol );
101525 zCol = pTab->aCol[pTab->iPKey].zName;
101529 assert( iDb>=0 && iDb<db->nDb );
101530 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
101531 pExpr->op = TK_NULL;
101548 sqlite3 *db = pParse->db;
101554 if( db->init.busy || IN_DECLARE_VTAB ){
101558 if( db->xAuth==0 ){
101562 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
101563 ** callback are either NULL pointers or zero-terminated strings that
101571 testcase( pParse->zAuthContext==0 );
101573 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
101575 ,db->auth.zAuthUser
101580 pParse->rc = SQLITE_AUTH;
101591 ** popped. Or if pParse==0, this routine is a no-op.
101599 pContext->pParse = pParse;
101600 pContext->zAuthContext = pParse->zAuthContext;
101601 pParse->zAuthContext = zContext;
101609 if( pContext->pParse ){
101610 pContext->pParse->zAuthContext = pContext->zAuthContext;
101611 pContext->pParse = 0;
101658 ** Record the fact that we want to lock a table at run-time.
101681 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
101682 for(i=0; i<pToplevel->nTableLock; i++){
101683 p = &pToplevel->aTableLock[i];
101684 if( p->iDb==iDb && p->iTab==iTab ){
101685 p->isWriteLock = (p->isWriteLock || isWriteLock);
101690 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
101691 pToplevel->aTableLock =
101692 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
101693 if( pToplevel->aTableLock ){
101694 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
101695 p->iDb = iDb;
101696 p->iTab = iTab;
101697 p->isWriteLock = isWriteLock;
101698 p->zLockName = zName;
101700 pToplevel->nTableLock = 0;
101701 sqlite3OomFault(pToplevel->db);
101716 for(i=0; i<pParse->nTableLock; i++){
101717 TableLock *p = &pParse->aTableLock[i];
101718 int p1 = p->iDb;
101719 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
101720 p->zLockName, P4_STATIC);
101728 ** Return TRUE if the given yDbMask object is empty - if it contains no
101754 assert( pParse->pToplevel==0 );
101755 db = pParse->db;
101756 if( pParse->nested ) return;
101757 if( db->mallocFailed || pParse->nErr ){
101758 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
101766 assert( !pParse->isMultiWrite
101767 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
101772 if( pParse->nTableLock>0 && db->init.busy==0 ){
101774 if( db->auth.authLevel<UAUTH_User ){
101776 pParse->rc = SQLITE_AUTH_USER;
101788 if( db->mallocFailed==0
101789 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
101792 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
101794 for(iDb=0; iDb<db->nDb; iDb++){
101796 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
101798 pSchema = db->aDb[iDb].pSchema;
101802 DbMaskTest(pParse->writeMask,iDb), /* P2 */
101803 pSchema->schema_cookie, /* P3 */
101804 pSchema->iGeneration /* P4 */
101806 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
101808 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
101811 for(i=0; i<pParse->nVtabLock; i++){
101812 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
101815 pParse->nVtabLock = 0;
101819 ** obtain the required table-locks. This is a no-op unless the
101820 ** shared-cache feature is enabled.
101829 if( pParse->pConstExpr ){
101830 ExprList *pEL = pParse->pConstExpr;
101831 pParse->okConstFactor = 0;
101832 for(i=0; i<pEL->nExpr; i++){
101833 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
101845 if( v && pParse->nErr==0 && !db->mallocFailed ){
101846 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
101849 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
101851 pParse->rc = SQLITE_DONE;
101853 pParse->rc = SQLITE_ERROR;
101873 sqlite3 *db = pParse->db;
101876 if( pParse->nErr ) return;
101877 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
101884 pParse->nested++;
101891 pParse->nested--;
101905 ** Locate the in-memory structure that describes a particular database
101925 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
101930 for(i=OMIT_TEMPDB; i<db->nDb; i++){
101932 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
101934 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
101941 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
101948 ** Locate the in-memory structure that describes a particular database
101951 ** error message in pParse->zErrMsg.
101954 ** routine leaves an error message in pParse->zErrMsg where
101971 p = sqlite3FindTable(pParse->db, zName, zDbase);
101975 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
101979 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
101981 pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
101984 return pMod->pEpoTab;
101994 pParse->checkSchema = 1;
102006 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
102007 ** non-NULL if it is part of a view or trigger program definition. See
102016 assert( p->pSchema==0 || p->zDatabase==0 );
102017 if( p->pSchema ){
102018 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
102019 zDb = pParse->db->aDb[iDb].zDbSName;
102021 zDb = p->zDatabase;
102023 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
102027 ** Locate the in-memory structure that describes
102043 for(i=OMIT_TEMPDB; i<db->nDb; i++){
102045 Schema *pSchema = db->aDb[j].pSchema;
102047 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
102049 p = sqlite3HashFind(&pSchema->idxHash, zName);
102062 sqlite3ExprDelete(db, p->pPartIdxWhere);
102063 sqlite3ExprListDelete(db, p->aColExpr);
102064 sqlite3DbFree(db, p->zColAff);
102065 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
102067 sqlite3_free(p->aiRowEst);
102083 pHash = &db->aDb[iDb].pSchema->idxHash;
102086 if( pIndex->pTable->pIndex==pIndex ){
102087 pIndex->pTable->pIndex = pIndex->pNext;
102092 p = pIndex->pTable->pIndex;
102093 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
102094 if( ALWAYS(p && p->pNext==pIndex) ){
102095 p->pNext = pIndex->pNext;
102100 db->mDbFlags |= DBFLAG_SchemaChange;
102104 ** Look through the list of open database files in db->aDb[] and if
102106 ** db->aDb[] structure to a smaller size, if possible.
102113 for(i=j=2; i<db->nDb; i++){
102114 struct Db *pDb = &db->aDb[i];
102115 if( pDb->pBt==0 ){
102116 sqlite3DbFree(db, pDb->zDbSName);
102117 pDb->zDbSName = 0;
102121 db->aDb[j] = db->aDb[i];
102125 db->nDb = j;
102126 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
102127 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
102128 sqlite3DbFree(db, db->aDb);
102129 db->aDb = db->aDbStatic;
102135 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
102140 assert( iDb<db->nDb );
102148 if( db->nSchemaLock==0 ){
102149 for(i=0; i<db->nDb; i++){
102151 sqlite3SchemaClear(db->aDb[i].pSchema);
102164 assert( db->nSchemaLock==0 );
102165 for(i=0; i<db->nDb; i++){
102166 Db *pDb = &db->aDb[i];
102167 if( pDb->pSchema ){
102168 sqlite3SchemaClear(pDb->pSchema);
102171 db->mDbFlags &= ~DBFLAG_SchemaChange;
102181 db->mDbFlags &= ~DBFLAG_SchemaChange;
102192 if( (pCol = pTable->aCol)!=0 ){
102193 for(i=0; i<pTable->nCol; i++, pCol++){
102194 sqlite3DbFree(db, pCol->zName);
102195 sqlite3ExprDelete(db, pCol->pDflt);
102196 sqlite3DbFree(db, pCol->zColl);
102198 sqlite3DbFree(db, pTable->aCol);
102214 ** db parameter can be used with db->pnBytesFreed to measure the memory
102225 if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
102231 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
102232 pNext = pIndex->pNext;
102233 assert( pIndex->pSchema==pTable->pSchema
102234 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
102235 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
102236 char *zName = pIndex->zName;
102238 &pIndex->pSchema->idxHash, zName, 0
102240 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
102252 sqlite3DbFree(db, pTable->zName);
102253 sqlite3DbFree(db, pTable->zColAff);
102254 sqlite3SelectDelete(db, pTable->pSelect);
102255 sqlite3ExprListDelete(db, pTable->pCheck);
102267 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
102281 assert( iDb>=0 && iDb<db->nDb );
102284 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
102285 pDb = &db->aDb[iDb];
102286 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
102288 db->mDbFlags |= DBFLAG_SchemaChange;
102307 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
102323 if( p->nTab==0 ){
102324 p->nTab = 1;
102329 ** Parameter zName points to a nul-terminated buffer containing the name
102331 ** function returns the index of the named database in db->aDb[], or
102332 ** -1 if the named db cannot be found.
102335 int i = -1; /* Database number */
102338 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
102339 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
102351 ** index of the named database in db->aDb[], or -1 if the named db
102386 sqlite3 *db = pParse->db;
102389 if( pName2->n>0 ){
102390 if( db->init.busy ) {
102392 return -1;
102398 return -1;
102401 assert( db->init.iDb==0 || db->init.busy
102402 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
102403 iDb = db->init.iDb;
102410 ** This routine is used to check if the UTF-8 string zName is a legal
102417 if( !pParse->db->init.busy && pParse->nested==0
102418 && (pParse->db->flags & SQLITE_WriteSchema)==0
102431 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
102437 ** column iCol. Return -1 if not found.
102441 for(i=0; i<pIdx->nColumn; i++){
102442 if( iCol==pIdx->aiColumn[i] ) return i;
102444 return -1;
102457 ** The new table record is initialized and put in pParse->pNewTable.
102474 sqlite3 *db = pParse->db;
102479 if( db->init.busy && db->init.newTnum==1 ){
102481 iDb = db->init.iDb;
102488 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
102497 pParse->sNameToken = *pName;
102502 if( db->init.iDb==1 ) isTemp = 1;
102513 char *zDb = db->aDb[iDb].zDbSName;
102532 char *zDb = db->aDb[iDb].zDbSName;
102541 assert( !db->init.busy || CORRUPT_DB );
102554 assert( db->mallocFailed );
102555 pParse->rc = SQLITE_NOMEM_BKPT;
102556 pParse->nErr++;
102559 pTable->zName = zName;
102560 pTable->iPKey = -1;
102561 pTable->pSchema = db->aDb[iDb].pSchema;
102562 pTable->nTabRef = 1;
102564 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
102566 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
102568 assert( pParse->pNewTable==0 );
102569 pParse->pNewTable = pTable;
102576 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
102578 pTable->pSchema->pSeqTab = pTable;
102590 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
102607 reg1 = pParse->regRowid = ++pParse->nMem;
102608 reg2 = pParse->regRoot = ++pParse->nMem;
102609 reg3 = ++pParse->nMem;
102613 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
102619 /* This just creates a place-holder record in the sqlite_master table.
102623 ** The rowid for the new entry is left in register pParse->regRowid.
102624 ** The root page number of the new table is left in reg pParse->regRoot.
102634 pParse->addrCrTab =
102645 /* Normal (non-error) return. */
102659 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
102660 pCol->colFlags |= COLFLAG_HIDDEN;
102661 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
102662 pTab->tabFlags |= TF_OOOHidden;
102682 sqlite3 *db = pParse->db;
102683 if( (p = pParse->pNewTable)==0 ) return;
102684 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
102685 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
102688 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
102690 memcpy(z, pName->z, pName->n);
102691 z[pName->n] = 0;
102693 for(i=0; i<p->nCol; i++){
102694 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
102700 if( (p->nCol & 0x7)==0 ){
102702 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
102707 p->aCol = aNew;
102709 pCol = &p->aCol[p->nCol];
102710 memset(pCol, 0, sizeof(p->aCol[0]));
102711 pCol->zName = z;
102714 if( pType->n==0 ){
102717 pCol->affinity = SQLITE_AFF_BLOB;
102718 pCol->szEst = 1;
102721 memcpy(zType, pType->z, pType->n);
102722 zType[pType->n] = 0;
102724 pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst);
102725 pCol->colFlags |= COLFLAG_HASTYPE;
102727 p->nCol++;
102728 pParse->constraintName.n = 0;
102739 p = pParse->pNewTable;
102740 if( p==0 || NEVER(p->nCol<1) ) return;
102741 p->aCol[p->nCol-1].notNull = (u8)onError;
102742 p->tabFlags |= TF_HasNotNull;
102749 ** This routine does a case-independent search of zType for the
102757 ** --------------------------------
102819 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
102825 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
102850 sqlite3 *db = pParse->db;
102851 p = pParse->pNewTable;
102853 pCol = &(p->aCol[p->nCol-1]);
102854 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
102856 pCol->zName);
102862 sqlite3ExprDelete(db, pCol->pDflt);
102868 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
102892 if( p->op==TK_STRING ){
102893 p->op = TK_ID;
102894 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
102895 p->pLeft->op = TK_ID;
102911 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
102924 Table *pTab = pParse->pNewTable;
102926 int iCol = -1, i;
102929 if( pTab->tabFlags & TF_HasPrimaryKey ){
102931 "table \"%s\" has more than one primary key", pTab->zName);
102934 pTab->tabFlags |= TF_HasPrimaryKey;
102936 iCol = pTab->nCol - 1;
102937 pCol = &pTab->aCol[iCol];
102938 pCol->colFlags |= COLFLAG_PRIMKEY;
102941 nTerm = pList->nExpr;
102943 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
102946 if( pCExpr->op==TK_ID ){
102947 const char *zCName = pCExpr->u.zToken;
102948 for(iCol=0; iCol<pTab->nCol; iCol++){
102949 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
102950 pCol = &pTab->aCol[iCol];
102951 pCol->colFlags |= COLFLAG_PRIMKEY;
102963 pTab->iPKey = iCol;
102964 pTab->keyConf = (u8)onError;
102966 pTab->tabFlags |= autoInc*TF_Autoincrement;
102967 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
102980 sqlite3ExprListDelete(pParse->db, pList);
102992 Table *pTab = pParse->pNewTable;
102993 sqlite3 *db = pParse->db;
102995 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
102997 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
102998 if( pParse->constraintName.n ){
102999 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
103004 sqlite3ExprDelete(pParse->db, pCheckExpr);
103018 if( (p = pParse->pNewTable)==0 ) return;
103019 i = p->nCol-1;
103020 db = pParse->db;
103026 sqlite3DbFree(db, p->aCol[i].zColl);
103027 p->aCol[i].zColl = zColl;
103033 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
103034 assert( pIdx->nKeyCol==1 );
103035 if( pIdx->aiColumn[0]==i ){
103036 pIdx->azColl[0] = p->aCol[i].zColl;
103065 sqlite3 *db = pParse->db;
103067 u8 initbusy = db->init.busy;
103071 if( !initbusy && (!pColl || !pColl->xCmp) ){
103089 ** This plan is not completely bullet-proof. It is possible for
103095 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
103096 ** the schema-version whenever the schema changes.
103099 sqlite3 *db = pParse->db;
103100 Vdbe *v = pParse->pVdbe;
103103 db->aDb[iDb].pSchema->schema_cookie+1);
103126 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
103130 ** If the string zSignedIdent consists entirely of alpha-numeric
103133 ** it is quoted using double-quotes.
103169 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
103170 n += identLength(pCol->zName) + 5;
103172 n += identLength(p->zName);
103182 n += 35 + 6*p->nCol;
103190 identPut(zStmt, &k, p->zName);
103192 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
103203 sqlite3_snprintf(n-k, &zStmt[k], zSep);
103206 identPut(zStmt, &k, pCol->zName);
103207 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
103208 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
103209 testcase( pCol->affinity==SQLITE_AFF_BLOB );
103210 testcase( pCol->affinity==SQLITE_AFF_TEXT );
103211 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
103212 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
103213 testcase( pCol->affinity==SQLITE_AFF_REAL );
103215 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
103217 assert( pCol->affinity==SQLITE_AFF_BLOB
103218 || pCol->affinity==sqlite3AffinityType(zType, 0) );
103223 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
103234 if( pIdx->nColumn>=N ) return SQLITE_OK;
103235 assert( pIdx->isResized==0 );
103239 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
103240 pIdx->azColl = (const char**)zExtra;
103242 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
103243 pIdx->aiColumn = (i16*)zExtra;
103245 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
103246 pIdx->aSortOrder = (u8*)zExtra;
103247 pIdx->nColumn = N;
103248 pIdx->isResized = 1;
103259 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
103260 wTable += pTabCol->szEst;
103262 if( pTab->iPKey<0 ) wTable++;
103263 pTab->szTabRow = sqlite3LogEst(wTable*4);
103272 const Column *aCol = pIdx->pTable->aCol;
103273 for(i=0; i<pIdx->nColumn; i++){
103274 i16 x = pIdx->aiColumn[i];
103275 assert( x<pIdx->pTable->nCol );
103276 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
103278 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
103284 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
103317 sqlite3 *db = pParse->db;
103318 Vdbe *v = pParse->pVdbe;
103322 if( !db->init.imposterTable ){
103323 for(i=0; i<pTab->nCol; i++){
103324 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
103325 pTab->aCol[i].notNull = OE_Abort;
103330 /* The remaining transformations only apply to b-tree tables, not to
103337 if( pParse->addrCrTab ){
103339 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
103345 if( pTab->iPKey>=0 ){
103348 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
103352 pList->a[0].sortOrder = pParse->iPkSortOrder;
103353 assert( pParse->pNewTable==pTab );
103354 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
103356 if( db->mallocFailed ) return;
103358 pTab->iPKey = -1;
103367 for(i=j=1; i<pPk->nKeyCol; i++){
103368 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
103369 pPk->nColumn--;
103371 pPk->aiColumn[j++] = pPk->aiColumn[i];
103374 pPk->nKeyCol = j;
103377 pPk->isCovering = 1;
103378 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
103379 nPk = pPk->nKeyCol;
103385 if( v && pPk->tnum>0 ){
103386 assert( db->init.busy==0 );
103387 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
103391 pPk->tnum = pTab->tnum;
103393 /* Update the in-memory representation of all UNIQUE indices by converting
103396 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103400 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
103404 pIdx->nColumn = pIdx->nKeyCol;
103407 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
103408 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
103409 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
103410 pIdx->aiColumn[j] = pPk->aiColumn[i];
103411 pIdx->azColl[j] = pPk->azColl[i];
103415 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
103416 assert( pIdx->nColumn>=j );
103421 if( nPk<pTab->nCol ){
103422 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
103423 for(i=0, j=nPk; i<pTab->nCol; i++){
103424 if( !hasColumn(pPk->aiColumn, j, i) ){
103425 assert( j<pPk->nColumn );
103426 pPk->aiColumn[j] = i;
103427 pPk->azColl[j] = sqlite3StrBINARY;
103431 assert( pPk->nColumn==j );
103432 assert( pTab->nCol==j );
103434 pPk->nColumn = pTab->nCol;
103447 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
103466 sqlite3 *db = pParse->db; /* The database connection */
103473 assert( !db->mallocFailed );
103474 p = pParse->pNewTable;
103477 assert( !db->init.busy || !pSelect );
103479 /* If the db->init.busy is 1 it means we are reading the SQL off the
103482 ** for the table from the db->init.newTnum field. (The page number
103486 ** table itself. So mark it read-only.
103488 if( db->init.busy ){
103489 p->tnum = db->init.newTnum;
103490 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103495 if( (p->tabFlags & TF_Autoincrement) ){
103500 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
103501 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
103503 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
103508 iDb = sqlite3SchemaToIndex(db, p->pSchema);
103513 if( p->pCheck ){
103514 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
103520 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
103530 if( !db->init.busy ){
103545 if( p->pSelect==0 ){
103558 ** statement to populate the new table. The root-page number for the
103559 ** new table is in register pParse->regRoot.
103565 ** A shared-cache write-lock is not required to write to the new table,
103566 ** as a schema-lock must have already been obtained to create it. Since
103567 ** a schema-lock excludes all other database users, the write-lock would
103572 int regYield; /* Register holding co-routine entry-point */
103573 int addrTop; /* Top of the co-routine */
103579 regYield = ++pParse->nMem;
103580 regRec = ++pParse->nMem;
103581 regRowid = ++pParse->nMem;
103582 assert(pParse->nTab==1);
103584 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
103586 pParse->nTab = 2;
103589 if( pParse->nErr ) return;
103592 assert( p->aCol==0 );
103593 p->nCol = pSelTab->nCol;
103594 p->aCol = pSelTab->aCol;
103595 pSelTab->nCol = 0;
103596 pSelTab->aCol = 0;
103600 if( pParse->nErr ) return;
103602 sqlite3VdbeJumpHere(v, addrTop - 1);
103618 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
103619 n = (int)(pEnd2->z - pParse->sNameToken.z);
103620 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
103622 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
103634 db->aDb[iDb].zDbSName, MASTER_NAME,
103636 p->zName,
103637 p->zName,
103638 pParse->regRoot,
103640 pParse->regRowid
103649 if( (p->tabFlags & TF_Autoincrement)!=0 ){
103650 Db *pDb = &db->aDb[iDb];
103652 if( pDb->pSchema->pSeqTab==0 ){
103655 pDb->zDbSName
103663 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
103667 /* Add the table to the in-memory representation of the database.
103669 if( db->init.busy ){
103671 Schema *pSchema = p->pSchema;
103673 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
103679 pParse->pNewTable = 0;
103680 db->mDbFlags |= DBFLAG_SchemaChange;
103683 if( !p->pSelect ){
103684 const char *zName = (const char *)pParse->sNameToken.z;
103687 if( pCons->z==0 ){
103690 nName = (int)((const char *)pCons->z - zName);
103691 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
103718 sqlite3 *db = pParse->db;
103720 if( pParse->nVar>0 ){
103725 p = pParse->pNewTable;
103726 if( p==0 || pParse->nErr ) goto create_view_fail;
103728 iDb = sqlite3SchemaToIndex(db, p->pSchema);
103734 ** allocated rather than point to the input string - which means that
103737 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
103738 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
103739 if( db->mallocFailed ) goto create_view_fail;
103744 sEnd = pParse->sLastToken;
103750 n = (int)(sEnd.z - pBegin->z);
103752 z = pBegin->z;
103753 while( sqlite3Isspace(z[n-1]) ){ n--; }
103754 sEnd.z = &z[n-1];
103771 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
103778 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
103789 db->nSchemaLock++;
103791 db->nSchemaLock--;
103802 if( pTable->nCol>0 ) return 0;
103819 if( pTable->nCol<0 ){
103820 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
103823 assert( pTable->nCol>=0 );
103832 assert( pTable->pSelect );
103833 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
103835 n = pParse->nTab;
103836 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
103837 pTable->nCol = -1;
103838 db->lookaside.bDisable++;
103840 xAuth = db->xAuth;
103841 db->xAuth = 0;
103843 db->xAuth = xAuth;
103847 pParse->nTab = n;
103848 if( pTable->pCheck ){
103851 ** arglist which is stored in pTable->pCheck. The pCheck field
103855 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
103856 &pTable->nCol, &pTable->aCol);
103857 if( db->mallocFailed==0
103858 && pParse->nErr==0
103859 && pTable->nCol==pSel->pEList->nExpr
103867 assert( pTable->aCol==0 );
103868 pTable->nCol = pSelTab->nCol;
103869 pTable->aCol = pSelTab->aCol;
103870 pSelTab->nCol = 0;
103871 pSelTab->aCol = 0;
103872 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
103874 pTable->nCol = 0;
103879 db->lookaside.bDisable--;
103883 pTable->pSchema->schemaFlags |= DB_UnresetViews;
103897 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
103899 if( pTab->pSelect ){
103901 pTab->aCol = 0;
103902 pTab->nCol = 0;
103914 ** root-page of a table or index in database iDb has changed from iFrom
103935 pDb = &db->aDb[iDb];
103936 pHash = &pDb->pSchema->tblHash;
103939 if( pTab->tnum==iFrom ){
103940 pTab->tnum = iTo;
103943 pHash = &pDb->pSchema->idxHash;
103946 if( pIdx->tnum==iFrom ){
103947 pIdx->tnum = iTo;
103954 ** Write code to erase the table with root-page iTable from database iDb.
103956 ** if a root-page of another table is moved by the btree-layer whilst
103957 ** erasing iTable (this can happen with an auto-vacuum database).
103967 ** is non-zero, then it is the root page number of a table moved to
103977 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
103985 ** in case a root-page belonging to another table is moved by the btree layer
103986 ** is also added (this can happen with an auto-vacuum database).
103989 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
103991 ** table and index root-pages in order, starting with the numerically
103992 ** largest root-page number. This guarantees that none of the root-pages
104000 ** and root page 5 happened to be the largest root-page number in the
104003 ** a free-list page.
104005 int iTab = pTab->tnum;
104015 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
104016 int iIdx = pIdx->tnum;
104017 assert( pIdx->pSchema==pTab->pSchema );
104025 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104026 assert( iDb>=0 && iDb<pParse->db->nDb );
104044 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
104048 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
104062 sqlite3 *db = pParse->db;
104064 Db *pDb = &db->aDb[iDb];
104082 assert( pTrigger->pSchema==pTab->pSchema ||
104083 pTrigger->pSchema==db->aDb[1].pSchema );
104085 pTrigger = pTrigger->pNext;
104092 ** move as a result of the drop (can happen in auto-vacuum mode).
104094 if( pTab->tabFlags & TF_Autoincrement ){
104097 pDb->zDbSName, pTab->zName
104111 pDb->zDbSName, MASTER_NAME, pTab->zName);
104120 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
104122 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
104134 sqlite3 *db = pParse->db;
104137 if( db->mallocFailed ){
104140 assert( pParse->nErr==0 );
104141 assert( pName->nSrc==1 );
104143 if( noErr ) db->suppressErr++;
104145 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
104146 if( noErr ) db->suppressErr--;
104149 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
104152 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
104153 assert( iDb>=0 && iDb<db->nDb );
104165 const char *zDb = db->aDb[iDb].zDbSName;
104179 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
104188 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
104191 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
104196 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
104197 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
104198 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
104206 if( isView && pTab->pSelect==0 ){
104207 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
104210 if( !isView && pTab->pSelect ){
104211 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
104222 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
104242 ** under construction in the pParse->pNewTable field.
104254 sqlite3 *db = pParse->db;
104258 Table *p = pParse->pNewTable;
104267 int iCol = p->nCol-1;
104269 if( pToCol && pToCol->nExpr!=1 ){
104272 p->aCol[iCol].zName, pTo);
104276 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
104282 nCol = pFromCol->nExpr;
104284 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
104286 for(i=0; i<pToCol->nExpr; i++){
104287 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
104294 pFKey->pFrom = p;
104295 pFKey->pNextFrom = p->pFKey;
104296 z = (char*)&pFKey->aCol[nCol];
104297 pFKey->zTo = z;
104298 memcpy(z, pTo->z, pTo->n);
104299 z[pTo->n] = 0;
104301 z += pTo->n+1;
104302 pFKey->nCol = nCol;
104304 pFKey->aCol[0].iFrom = p->nCol-1;
104308 for(j=0; j<p->nCol; j++){
104309 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
104310 pFKey->aCol[i].iFrom = j;
104314 if( j>=p->nCol ){
104317 pFromCol->a[i].zName);
104324 int n = sqlite3Strlen30(pToCol->a[i].zName);
104325 pFKey->aCol[i].zCol = z;
104326 memcpy(z, pToCol->a[i].zName, n);
104331 pFKey->isDeferred = 0;
104332 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
104333 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
104335 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
104336 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
104337 pFKey->zTo, (void *)pFKey
104344 assert( pNextTo->pPrevTo==0 );
104345 pFKey->pNextTo = pNextTo;
104346 pNextTo->pPrevTo = pFKey;
104351 p->pFKey = pFKey;
104372 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
104373 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
104374 pFKey->isDeferred = (u8)isDeferred;
104387 ** the root page number of the index is taken from pIndex->tnum.
104390 Table *pTab = pIndex->pTable; /* The table that is indexed */
104391 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
104392 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
104401 sqlite3 *db = pParse->db; /* The database connection */
104402 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
104405 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
104406 db->aDb[iDb].zDbSName ) ){
104411 /* Require a write-lock on the table to perform this operation */
104412 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
104419 tnum = pIndex->tnum;
104422 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
104425 iSorter = pParse->nTab++;
104426 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
104451 pIndex->nKeyCol); VdbeCoverage(v);
104473 ** of 8-byte aligned space after the Index object and return a
104493 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
104494 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
104495 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
104496 p->aSortOrder = (u8*)pExtra;
104497 p->nColumn = nCol;
104498 p->nKeyCol = nCol - 1;
104508 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
104509 ** as the table to be indexed. pParse->pNewTable is a table that is
104513 ** is a primary key or unique-constraint on the most recent column added
104520 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
104536 sqlite3 *db = pParse->db;
104546 if( db->mallocFailed || pParse->nErr>0 ){
104561 /* Use the two-part index name to determine the database
104568 assert( pName && pName->z );
104575 if( !db->init.busy ){
104577 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
104589 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
104590 assert( db->mallocFailed==0 || pTab==0 );
104592 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
104594 "cannot create a TEMP index on non-TEMP table \"%s\"",
104595 pTab->zName);
104602 pTab = pParse->pNewTable;
104604 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
104606 pDb = &db->aDb[iDb];
104609 assert( pParse->nErr==0 );
104610 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
104611 && db->init.busy==0
104613 && sqlite3UserAuthTable(pTab->zName)==0
104615 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
104616 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
104620 if( pTab->pSelect ){
104648 assert( pName->z!=0 );
104652 if( !db->init.busy ){
104658 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
104662 assert( !db->init.busy );
104670 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
104671 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
104688 const char *zDb = pDb->zDbSName;
104694 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
104706 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
104710 assert( pList->nExpr==1 );
104719 for(i=0; i<pList->nExpr; i++){
104720 Expr *pExpr = pList->a[i].pExpr;
104722 if( pExpr->op==TK_COLLATE ){
104723 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
104731 nExtraCol = pPk ? pPk->nKeyCol : 1;
104732 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
104734 if( db->mallocFailed ){
104737 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
104738 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
104739 pIndex->zName = zExtra;
104741 memcpy(pIndex->zName, zName, nName+1);
104742 pIndex->pTable = pTab;
104743 pIndex->onError = (u8)onError;
104744 pIndex->uniqNotNull = onError!=OE_None;
104745 pIndex->idxType = idxType;
104746 pIndex->pSchema = db->aDb[iDb].pSchema;
104747 pIndex->nKeyCol = pList->nExpr;
104750 pIndex->pPartIdxWhere = pPIWhere;
104757 if( pDb->pSchema->file_format>=4 ){
104758 sortOrderMask = -1; /* Honor DESC */
104766 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
104772 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
104773 Expr *pCExpr; /* The i-th index expression */
104774 int requestedSortOrder; /* ASC or DESC on the i-th expression */
104777 sqlite3StringToId(pListItem->pExpr);
104778 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
104779 if( pParse->nErr ) goto exit_create_index;
104780 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
104781 if( pCExpr->op!=TK_COLUMN ){
104782 if( pTab==pParse->pNewTable ){
104787 if( pIndex->aColExpr==0 ){
104789 pIndex->aColExpr = pCopy;
104790 if( !db->mallocFailed ){
104792 pListItem = &pCopy->a[i];
104796 pIndex->aiColumn[i] = XN_EXPR;
104797 pIndex->uniqNotNull = 0;
104799 j = pCExpr->iColumn;
104802 j = pTab->iPKey;
104803 }else if( pTab->aCol[j].notNull==0 ){
104804 pIndex->uniqNotNull = 0;
104806 pIndex->aiColumn[i] = (i16)j;
104809 if( pListItem->pExpr->op==TK_COLLATE ){
104811 zColl = pListItem->pExpr->u.zToken;
104817 nExtra -= nColl;
104819 zColl = pTab->aCol[j].zColl;
104822 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
104825 pIndex->azColl[i] = zColl;
104826 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
104827 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
104835 for(j=0; j<pPk->nKeyCol; j++){
104836 int x = pPk->aiColumn[j];
104838 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
104839 pIndex->nColumn--;
104841 pIndex->aiColumn[i] = x;
104842 pIndex->azColl[i] = pPk->azColl[j];
104843 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
104847 assert( i==pIndex->nColumn );
104849 pIndex->aiColumn[i] = XN_ROWID;
104850 pIndex->azColl[i] = sqlite3StrBINARY;
104853 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
104858 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
104859 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
104860 pIndex->isCovering = 1;
104861 for(j=0; j<pTab->nCol; j++){
104862 if( j==pTab->iPKey ) continue;
104864 pIndex->isCovering = 0;
104869 if( pTab==pParse->pNewTable ){
104892 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
104895 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
104898 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
104899 for(k=0; k<pIdx->nKeyCol; k++){
104902 assert( pIdx->aiColumn[k]>=0 );
104903 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
104904 z1 = pIdx->azColl[k];
104905 z2 = pIndex->azColl[k];
104908 if( k==pIdx->nKeyCol ){
104909 if( pIdx->onError!=pIndex->onError ){
104917 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
104921 if( pIdx->onError==OE_Default ){
104922 pIdx->onError = pIndex->onError;
104925 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
104932 ** in-memory database structures.
104934 assert( pParse->nErr==0 );
104935 if( db->init.busy ){
104938 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
104939 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
104940 pIndex->zName, pIndex);
104946 db->mDbFlags |= DBFLAG_SchemaChange;
104948 pIndex->tnum = db->init.newTnum;
104968 int iMem = ++pParse->nMem;
104981 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
104988 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
104989 if( pName->z[n-1]==';' ) n--;
104992 onError==OE_None ? "" : " UNIQUE", n, pName->z);
105003 db->aDb[iDb].zDbSName, MASTER_NAME,
105004 pIndex->zName,
105005 pTab->zName,
105012 ** to invalidate all pre-compiled statements.
105018 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
105022 sqlite3VdbeJumpHere(v, pIndex->tnum);
105031 if( db->init.busy || pTblName==0 ){
105032 if( onError!=OE_Replace || pTab->pIndex==0
105033 || pTab->pIndex->onError==OE_Replace){
105034 pIndex->pNext = pTab->pIndex;
105035 pTab->pIndex = pIndex;
105037 Index *pOther = pTab->pIndex;
105038 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
105039 pOther = pOther->pNext;
105041 pIndex->pNext = pOther->pNext;
105042 pOther->pNext = pIndex;
105057 ** Fill the Index.aiRowEst[] array with default information - information
105067 ** aiRowEst[N]<=aiRowEst[N-1]
105077 LogEst *a = pIdx->aiRowLogEst;
105078 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
105082 assert( !pIdx->hasStat1 );
105087 a[0] = pIdx->pTable->nRowLogEst;
105088 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
105094 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
105099 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
105109 sqlite3 *db = pParse->db;
105112 assert( pParse->nErr==0 ); /* Never called with prior errors */
105113 if( db->mallocFailed ){
105116 assert( pName->nSrc==1 );
105120 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
105125 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
105127 pParse->checkSchema = 1;
105130 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
105135 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
105139 Table *pTab = pIndex->pTable;
105140 const char *zDb = db->aDb[iDb].zDbSName;
105146 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
105158 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
105160 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
105162 destroyRootPage(pParse, pIndex->tnum, iDb);
105163 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
105176 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
105184 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
105196 if( (n & (n-1))==0 ){
105200 *pIdx = -1;
105224 pList->a = sqlite3ArrayAllocate(
105226 pList->a,
105227 sizeof(pList->a[0]),
105228 &pList->nId,
105235 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
105245 for(i=0; i<pList->nId; i++){
105246 sqlite3DbFree(db, pList->a[i].zName);
105248 sqlite3DbFree(db, pList->a);
105253 ** Return the index in pList of the identifier named zId. Return -1
105258 if( pList==0 ) return -1;
105259 for(i=0; i<pList->nId; i++){
105260 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
105262 return -1;
105282 ** db->mallocFailed flag will be set to true.
105287 int nExtra, /* Number of new slots to add to pSrc->a[] */
105288 int iStart /* Index in pSrc->a[] of first new slot */
105296 assert( iStart<=pSrc->nSrc );
105299 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
105301 int nAlloc = pSrc->nSrc*2+nExtra;
105304 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
105306 assert( db->mallocFailed );
105310 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
105311 pSrc->nAlloc = nGot;
105316 for(i=pSrc->nSrc-1; i>=iStart; i--){
105317 pSrc->a[i+nExtra] = pSrc->a[i];
105319 pSrc->nSrc += nExtra;
105322 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
105324 pSrc->a[i].iCursor = -1;
105378 pList->nAlloc = 1;
105379 pList->nSrc = 1;
105380 memset(&pList->a[0], 0, sizeof(pList->a[0]));
105381 pList->a[0].iCursor = -1;
105383 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
105385 if( db->mallocFailed ){
105389 pItem = &pList->a[pList->nSrc-1];
105390 if( pDatabase && pDatabase->z==0 ){
105394 pItem->zName = sqlite3NameFromToken(db, pDatabase);
105395 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
105397 pItem->zName = sqlite3NameFromToken(db, pTable);
105398 pItem->zDatabase = 0;
105409 assert(pList || pParse->db->mallocFailed );
105411 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
105412 if( pItem->iCursor>=0 ) break;
105413 pItem->iCursor = pParse->nTab++;
105414 if( pItem->pSelect ){
105415 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
105428 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
105429 sqlite3DbFree(db, pItem->zDatabase);
105430 sqlite3DbFree(db, pItem->zName);
105431 sqlite3DbFree(db, pItem->zAlias);
105432 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
105433 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
105434 sqlite3DeleteTable(db, pItem->pTab);
105435 sqlite3SelectDelete(db, pItem->pSelect);
105436 sqlite3ExprDelete(db, pItem->pOn);
105437 sqlite3IdListDelete(db, pItem->pUsing);
105448 ** pDatabase is NULL if the database name qualifier is missing - the
105463 Token *pAlias, /* The right-hand side of the AS subexpression */
105469 sqlite3 *db = pParse->db;
105480 assert( p->nSrc>0 );
105481 pItem = &p->a[p->nSrc-1];
105483 if( pAlias->n ){
105484 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
105486 pItem->pSelect = pSubquery;
105487 pItem->pOn = pOn;
105488 pItem->pUsing = pUsing;
105501 ** element of the source-list passed as the second argument.
105505 if( p && pIndexedBy->n>0 ){
105507 assert( p->nSrc>0 );
105508 pItem = &p->a[p->nSrc-1];
105509 assert( pItem->fg.notIndexed==0 );
105510 assert( pItem->fg.isIndexedBy==0 );
105511 assert( pItem->fg.isTabFunc==0 );
105512 if( pIndexedBy->n==1 && !pIndexedBy->z ){
105515 pItem->fg.notIndexed = 1;
105517 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
105518 pItem->fg.isIndexedBy = 1;
105525 ** table-valued-function.
105529 struct SrcList_item *pItem = &p->a[p->nSrc-1];
105530 assert( pItem->fg.notIndexed==0 );
105531 assert( pItem->fg.isIndexedBy==0 );
105532 assert( pItem->fg.isTabFunc==0 );
105533 pItem->u1.pFuncArg = pList;
105534 pItem->fg.isTabFunc = 1;
105536 sqlite3ExprListDelete(pParse->db, pList);
105552 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
105558 for(i=p->nSrc-1; i>0; i--){
105559 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
105561 p->a[0].fg.jointype = 0;
105574 db = pParse->db;
105582 for(i=0; i<db->nDb; i++){
105600 assert( pParse->db!=0 );
105618 char *zName = sqlite3NameFromToken(pParse->db, pName);
105626 sqlite3DbFree(pParse->db, zName);
105638 sqlite3 *db = pParse->db;
105639 if( db->aDb[1].pBt==0 && !pParse->explain ){
105649 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
105653 pParse->rc = rc;
105656 db->aDb[1].pBt = pBt;
105657 assert( db->aDb[1].pSchema );
105658 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
105669 ** will occur at the end of the top-level VDBE and will be generated
105675 assert( iDb>=0 && iDb<pParse->db->nDb );
105676 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
105678 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
105679 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
105680 DbMaskSet(pToplevel->cookieMask, iDb);
105692 sqlite3 *db = pParse->db;
105694 for(i=0; i<db->nDb; i++){
105695 Db *pDb = &db->aDb[i];
105696 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
105718 DbMaskSet(pToplevel->writeMask, iDb);
105719 pToplevel->isMultiWrite |= setStatement;
105731 pToplevel->isMultiWrite = 1;
105752 pToplevel->mayAbort = 1;
105788 Table *pTab = pIdx->pTable;
105790 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
105791 if( pIdx->aColExpr ){
105792 sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
105794 for(j=0; j<pIdx->nKeyCol; j++){
105796 assert( pIdx->aiColumn[j]>=0 );
105797 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
105799 sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
105813 ** Code an OP_Halt due to non-unique rowid.
105818 Table *pTab /* The table with the non-unique rowid */
105822 if( pTab->iPKey>=0 ){
105823 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
105824 pTab->aCol[pTab->iPKey].zName);
105827 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
105842 for(i=0; i<pIndex->nColumn; i++){
105843 const char *z = pIndex->azColl[i];
105844 assert( z!=0 || pIndex->aiColumn[i]<0 );
105845 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
105861 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
105863 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
105865 sqlite3RefillIndex(pParse, pIndex, -1);
105880 sqlite3 *db = pParse->db; /* The database connection */
105885 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
105887 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
105898 ** REINDEX -- 1
105899 ** REINDEX <collation> -- 2
105900 ** REINDEX ?<database>.?<tablename> -- 3
105901 ** REINDEX ?<database>.?<indexname> -- 4
105916 sqlite3 *db = pParse->db; /* The database connection */
105928 }else if( NEVER(pName2==0) || pName2->z==0 ){
105930 assert( pName1->z );
105931 zColl = sqlite3NameFromToken(pParse->db, pName1);
105945 zDb = db->aDb[iDb].zDbSName;
105956 sqlite3RefillIndex(pParse, pIndex, -1);
105971 int nCol = pIdx->nColumn;
105972 int nKey = pIdx->nKeyCol;
105974 if( pParse->nErr ) return 0;
105975 if( pIdx->uniqNotNull ){
105976 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
105978 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
105983 const char *zColl = pIdx->azColl[i];
105984 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
105986 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
105988 if( pParse->nErr ){
105989 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
105990 if( pIdx->bNoQuery==0 ){
105995 ** the missing index using the collation-needed callback. For
105998 pIdx->bNoQuery = 1;
105999 pParse->rc = SQLITE_ERROR_RETRY;
106016 Token *pName, /* Name of the common-table */
106020 sqlite3 *db = pParse->db;
106026 zName = sqlite3NameFromToken(pParse->db, pName);
106029 for(i=0; i<pWith->nCte; i++){
106030 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
106037 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
106042 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
106044 if( db->mallocFailed ){
106050 pNew->a[pNew->nCte].pSelect = pQuery;
106051 pNew->a[pNew->nCte].pCols = pArglist;
106052 pNew->a[pNew->nCte].zName = zName;
106053 pNew->a[pNew->nCte].zCteErr = 0;
106054 pNew->nCte++;
106066 for(i=0; i<pWith->nCte; i++){
106067 struct Cte *pCte = &pWith->a[i];
106068 sqlite3ExprListDelete(db, pCte->pCols);
106069 sqlite3SelectDelete(db, pCte->pSelect);
106070 sqlite3DbFree(db, pCte->zName);
106102 assert( !db->xCollNeeded || !db->xCollNeeded16 );
106103 if( db->xCollNeeded ){
106106 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
106110 if( db->xCollNeeded16 ){
106113 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
106116 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
106127 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
106132 char *z = pColl->zName;
106137 if( pColl2->xCmp!=0 ){
106139 pColl->xDel = 0; /* Do not copy the destructor */
106167 sqlite3 *db = pParse->db;
106173 if( !p || !p->xCmp ){
106180 if( p && !p->xCmp && synthCollSeq(db, p) ){
106183 assert( !p || p->xCmp );
106186 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
106203 if( pColl && pColl->xCmp==0 ){
106204 const char *zName = pColl->zName;
106205 sqlite3 *db = pParse->db;
106224 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
106236 pColl = sqlite3HashFind(&db->aCollSeq, zName);
106250 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
106268 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
106292 pColl = db->pDfltColl;
106296 if( pColl ) pColl += enc-1;
106306 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
106307 ** is also -1. In other words, we are searching for a function that
106310 ** If nArg is -2 that means that we are searching for any function
106320 ** 4: UTF8/16 conversion required - argument count matches exactly
106321 ** 5: UTF16 byte order conversion required - argument count matches exactly
106324 ** If nArg==(-2) then any function with a non-null xSFunc is
106326 ** a non-match.
106331 int nArg, /* Desired number of arguments. (-1)==any */
106336 /* nArg of -2 is a special case */
106337 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
106340 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
106344 if( p->nArg==nArg ){
106351 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
106353 }else if( (enc & p->funcFlags & 2)!=0 ){
106369 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
106370 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
106393 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
106394 aDef[i].pNext = pOther->pNext;
106395 pOther->pNext = &aDef[i];
106408 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
106416 ** If nArg is -2, then the first valid function found is returned. A
106417 ** function is valid if xSFunc is non-zero. The nArg==(-2)
106419 ** of arguments. If nArg is -2, then createFlag must be 0.
106427 const char *zName, /* Name of the function. zero-terminated */
106428 int nArg, /* Number of arguments. -1 means any number */
106438 assert( nArg>=(-2) );
106439 assert( nArg>=(-1) || createFlag==0 );
106442 /* First search for a match amongst the application-defined functions.
106444 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
106451 p = p->pNext;
106454 /* If no match is found, search the built-in functions.
106456 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
106457 ** functions even if a prior app-defined function was found. And give
106458 ** priority to built-in functions.
106463 ** new function. But the FuncDefs for built-in functions are read-only.
106464 ** So we must not search for built-ins when creating a new function.
106466 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
106476 p = p->pNext;
106487 pBest->zName = (const char*)&pBest[1];
106488 pBest->nArg = (u16)nArg;
106489 pBest->funcFlags = enc;
106491 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
106497 pBest->pNext = pOther;
106501 if( pBest && (pBest->xSFunc || createFlag) ){
106521 temp1 = pSchema->tblHash;
106522 temp2 = pSchema->trigHash;
106523 sqlite3HashInit(&pSchema->trigHash);
106524 sqlite3HashClear(&pSchema->idxHash);
106529 sqlite3HashInit(&pSchema->tblHash);
106535 sqlite3HashClear(&pSchema->fkeyHash);
106536 pSchema->pSeqTab = 0;
106537 if( pSchema->schemaFlags & DB_SchemaLoaded ){
106538 pSchema->iGeneration++;
106540 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
106556 }else if ( 0==p->file_format ){
106557 sqlite3HashInit(&p->tblHash);
106558 sqlite3HashInit(&p->idxHash);
106559 sqlite3HashInit(&p->trigHash);
106560 sqlite3HashInit(&p->fkeyHash);
106561 p->enc = SQLITE_UTF8;
106594 ** pSrc->a[0].pTab Pointer to the Table object
106595 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
106599 struct SrcList_item *pItem = pSrc->a;
106601 assert( pItem && pSrc->nSrc==1 );
106603 sqlite3DeleteTable(pParse->db, pItem->pTab);
106604 pItem->pTab = pTab;
106606 pTab->nTabRef++;
106628 ** In either case leave an error message in pParse and return non-zero.
106631 && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
106632 || ( (pTab->tabFlags & TF_Readonly)!=0
106633 && (pParse->db->flags & SQLITE_WriteSchema)==0
106634 && pParse->nested==0 )
106636 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
106641 if( !viewOk && pTab->pSelect ){
106642 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
106667 sqlite3 *db = pParse->db;
106668 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
106672 assert( pFrom->nSrc==1 );
106673 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
106674 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
106675 assert( pFrom->a[0].pOn==0 );
106676 assert( pFrom->a[0].pUsing==0 );
106697 SrcList *pSrc, /* the FROM clause -- which tables to scan */
106703 sqlite3 *db = pParse->db;
106715 sqlite3ExprDelete(pParse->db, pWhere);
106716 sqlite3ExprListDelete(pParse->db, pOrderBy);
106736 pTab = pSrc->a[0].pTab;
106744 if( pPk->nKeyCol==1 ){
106745 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
106750 for(i=0; i<pPk->nKeyCol; i++){
106751 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
106756 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
106763 pSrc->a[0].pTab = 0;
106764 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
106765 pSrc->a[0].pTab = pTab;
106766 pSrc->a[0].pIBIndex = 0;
106808 int memCnt = -1; /* Memory cell used for change counting */
106832 db = pParse->db;
106833 if( pParse->nErr || db->mallocFailed ){
106836 assert( pTabList->nSrc==1 );
106852 isView = pTab->pSelect!=0;
106882 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106883 assert( iDb<db->nDb );
106884 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
106885 db->aDb[iDb].zDbSName);
106894 assert( pTabList->nSrc==1 );
106895 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
106896 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
106897 pParse->nTab++;
106903 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
106912 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
106941 if( db->flags & SQLITE_CountRows ){
106942 memCnt = ++pParse->nMem;
106953 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
106963 && db->xPreUpdateCallback==0
106967 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
106969 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
106970 pTab->zName, P4_STATIC);
106972 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106973 assert( pIdx->pSchema==pTab->pSchema );
106974 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
106986 iRowSet = ++pParse->nMem;
106993 nPk = pPk->nKeyCol;
106994 iPk = pParse->nMem+1;
106995 pParse->nMem += nPk;
106996 iEphCur = pParse->nTab++;
107005 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
107006 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
107007 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
107016 if( db->flags & SQLITE_CountRows ){
107023 assert( pPk->aiColumn[i]>=0 );
107025 pPk->aiColumn[i], iPk+i);
107029 iKey = pParse->nMem + 1;
107030 iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
107031 if( iKey>pParse->nMem ) pParse->nMem = iKey;
107035 /* For ONEPASS, no need to store the rowid/primary-key. There is only
107046 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
107047 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
107052 iKey = ++pParse->nMem;
107055 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
107090 /* Set up a loop over the rowids/primary-keys that were found in the
107091 ** where-clause loop above.
107095 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
107096 assert( pPk!=0 || pTab->pSelect!=0 );
107124 pParse->isMultiWrite = 0;
107129 int count = (pParse->nested==0); /* True to count changes */
107134 /* End of the loop over all rowids/primary-keys. */
107145 } /* End non-truncate path */
107151 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
107159 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
107199 ** cursor number iIdxCur+i for the i-th index.
107235 u8 count, /* If non-zero, increment the row change counter */
107240 Vdbe *v = pParse->pVdbe; /* Vdbe */
107274 iOld = pParse->nMem+1;
107275 pParse->nMem += (1 + pTab->nCol);
107277 /* Populate the OLD.* pseudo-table register array. These values will be
107280 for(iCol=0; iCol<pTab->nCol; iCol++){
107307 iIdxNoSeek = -1;
107320 ** If variable 'count' is non-zero, then this OP_Delete instruction should
107321 ** invoke the update-hook. The pre-update-hook, on the other hand should
107323 ** the update-hook is not invoked for rows removed by REPLACE, but the
107324 ** pre-update-hook is.
107326 if( pTab->pSelect==0 ){
107330 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
107372 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
107373 ** index is the 0-th index.)
107387 int r1 = -1; /* Register holding an index key */
107394 v = pParse->pVdbe;
107396 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
107401 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
107405 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
107452 Vdbe *v = pParse->pVdbe;
107458 if( pIdx->pPartIdxWhere ){
107460 pParse->iSelfTab = iDataCur + 1;
107462 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
107464 pParse->iSelfTab = 0;
107469 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
107471 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
107474 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
107475 && pPrior->aiColumn[j]!=XN_EXPR
107491 if( pIdx->pTable->pSelect ){
107492 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
107493 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
107501 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
107507 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
107525 ** This file contains the C-language implementations for many of the SQL
107539 assert( context->pVdbe!=0 );
107540 pOp = &context->pVdbe->aOp[context->iOp-1];
107541 assert( pOp->opcode==OP_CollSeq );
107542 assert( pOp->p4type==P4_COLLSEQ );
107543 return pOp->p4.pColl;
107551 context->skipFlag = 1;
107555 ** Implementation of the non-aggregate min() and max() functions
107568 mask = sqlite3_user_data(context)==0 ? 0 : -1;
107571 assert( mask==-1 || mask==0 );
107593 int i = sqlite3_value_type(argv[0]) - 1;
107601 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
107605 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
107649 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
107660 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
107662 ** equivalent positive 64-bit two complement value. */
107663 sqlite3_result_error(context, "integer overflow", -1);
107666 iVal = -iVal;
107672 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
107679 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
107683 if( rVal<0 ) rVal = -rVal;
107734 nHaystack--;
107758 x.nArg = argc-1;
107761 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
107774 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
107775 ** of x. If x is text, then we actually count UTF-8 characters.
107819 ** as substr(X,1,N) - it returns the first N characters of X. This
107820 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
107821 ** from 2009-02-02 for compatibility of applications that exploited the
107828 p2 = -p2;
107832 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
107842 p1--;
107844 p2--;
107847 p1 -= p2;
107857 p1--;
107859 for(z2=z; *z2 && p2; p2--){
107862 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
107866 p2 = len-p1;
107890 /* If Y==0 and X will fit in a 64-bit int,
107894 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
107896 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
107897 r = -(double)((sqlite_int64)((-r)+0.5));
107922 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
107923 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
107924 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
107986 #define noopFunc versionFunc /* Substitute function - never called */
108001 ** (or -9223372036854775808) since when you do abs() of that
108006 ** therefore be no less than -9223372036854775807.
108008 r = -(r & LARGEST_INT64);
108048 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
108057 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
108082 /* IMP: R-52756-41993 This function is a wrapper around the
108088 ** A structure defining how to do GLOB-style comparisons.
108111 /* The correct SQL-92 behavior is for the LIKE operator to ignore
108126 ** Compare two UTF-8 strings for equality where the first string is
108146 ** range of characters can be specified using '-'. Example:
108147 ** "[a-z]" matches any single lower-case letter. To match a '-', make
108170 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
108171 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
108172 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
108188 if( pInfo->matchSet==0 ){
108194 assert( matchOther<0x80 ); /* '[' is a single-byte character */
108196 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
108209 ** For a case-insensitive search, set variable cx to be the same as
108242 if( pInfo->matchSet==0 ){
108262 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
108293 ** non-zero if there is no match.
108300 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
108301 ** a miss - like strcmp().
108319 ** the build-in LIKE operator. The first argument to the function is the
108358 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
108359 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
108360 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
108361 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
108367 /* The escape character string must consist of a single UTF-8 character.
108372 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
108374 "ESCAPE expression must be a single character", -1);
108379 escape = pInfo->matchSet;
108417 /* IMP: R-48699-48617 This function is an SQL wrapper around the
108418 ** sqlite3_libversion() C-interface. */
108419 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
108433 /* IMP: R-24470-31136 This function is an SQL wrapper around the
108435 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
108441 ** its side-effects.
108467 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
108491 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
108495 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
108499 /* Array for converting from half-bytes (nybbles) into ASCII hex
108511 ** single-quote escapes.
108526 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
108549 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
108586 ** The unicode() function. Return the integer unicode code-point value
108638 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
108671 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
108684 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
108721 || sqlite3_context_db_handle(context)->mallocFailed );
108741 loopLimit = nStr - nPattern;
108748 nOut += nRep - nPattern;
108749 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108750 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108751 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108765 i += nPattern-1;
108768 assert( j+nStr-i+1==nOut );
108769 memcpy(&zOut[j], &zStr[i], nStr-i);
108770 j += nStr - i;
108824 aLen[nChar] = (u8)(z - azChar[nChar]);
108839 nIn -= len;
108847 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
108850 nIn -= len;
108865 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
108866 ** When the "sqlite3" command-line shell is built using this functionality,
108868 ** involving application-defined functions to be examined in a generic
108876 /* no-op */
108881 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
108882 ** is only available if the SQLITE_SOUNDEX compile-time option is used
108889 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
108934 /* IMP: R-64894-50321 The string "?000" is returned if the argument
108943 ** A function that loads a shared-library extension then returns NULL.
108954 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
108955 sqlite3_result_error(context, "not authorized", -1);
108965 sqlite3_result_error(context, zErrMsg, -1);
108982 u8 approx; /* True if non-integer value was input to the sum */
109003 p->cnt++;
109006 p->rSum += v;
109007 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
109008 p->overflow = 1;
109011 p->rSum += sqlite3_value_double(argv[0]);
109012 p->approx = 1;
109019 if( p && p->cnt>0 ){
109020 if( p->overflow ){
109021 sqlite3_result_error(context,"integer overflow",-1);
109022 }else if( p->approx ){
109023 sqlite3_result_double(context, p->rSum);
109025 sqlite3_result_int64(context, p->iSum);
109032 if( p && p->cnt>0 ){
109033 sqlite3_result_double(context, p->rSum/(double)p->cnt);
109040 sqlite3_result_double(context, p ? p->rSum : (double)0);
109059 p->n++;
109066 ** expressed as a 32-bit integer. */
109067 assert( argc==1 || p==0 || p->n>0x7fffffff
109068 || p->n==sqlite3_aggregate_count(context) );
109074 sqlite3_result_int64(context, p ? p->n : 0);
109093 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
109094 }else if( pBest->flags ){
109101 ** sqlite3_user_data() function returns (void *)-1. For min() it
109114 pBest->db = sqlite3_context_db_handle(context);
109122 if( pRes->flags ){
109147 int firstTerm = pAccum->mxAlloc==0;
109148 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
109168 if( pAccum->accError==STRACCUM_TOOBIG ){
109170 }else if( pAccum->accError==STRACCUM_NOMEM ){
109173 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
109180 ** This routine does per-connection function registration. Most
109181 ** of the built-in functions above are part of the global function set.
109193 ** Set the LIKEOPT flag on the 2-argument function with the given name.
109199 pDef->funcFlags |= flagVal;
109204 ** Register the built-in LIKE and GLOB functions. The caseSensitive
109229 ** LIKE-style function then return FALSE.
109244 if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
109248 nExpr = pExpr->x.pList->nExpr;
109249 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
109250 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
109256 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
109258 if( pEscape->op!=TK_STRING ) return 0;
109259 zEscape = pEscape->u.zToken;
109268 memcpy(aWc, pDef->pUserData, 3);
109272 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
109278 ** to the global function hash table. This occurs at start-time (as
109289 ** FuncDef.pHash elements at start-time. The elements of this array
109290 ** are read-only after initialization is complete.
109325 FUNCTION(min, -1, 0, 1, minmaxFunc ),
109329 FUNCTION(max, -1, 1, 1, minmaxFunc ),
109336 FUNCTION(printf, -1, 0, 0, printfFunc ),
109338 FUNCTION(char, -1, 0, 0, charFunc ),
109380 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
109384 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
109395 #if 0 /* Enable to print out how the built-in functions are hashed */
109400 printf("FUNC-HASH %02d:", i);
109401 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
109402 int n = sqlite3Strlen30(p->zName);
109403 int h = p->zName[0] + n;
109404 printf(" %s(%d)", p->zName, h);
109434 ** --------------------------
109516 ** ---------------
109518 ** Before coding an UPDATE or DELETE row operation, the code-generator
109523 ** accessed). No information is required by the code-generator before
109527 ** sqlite3FkRequired() - Test to see if FK processing is required.
109528 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
109532 ** --------------------------------------
109534 ** sqlite3FkCheck() - Check for foreign key violations.
109535 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
109536 ** sqlite3FkDelete() - Delete an FKey structure.
109541 ** -----------------------
109571 ** constraint to the parent table column stored in the left-most column
109573 ** child table column that corresponds to the second left-most column of
109592 ** then non-zero is returned, and a "foreign key mismatch" error loaded
109593 ** into pParse. If an OOM error occurs, non-zero is returned and the
109594 ** pParse->db->mallocFailed flag is set.
109605 int nCol = pFKey->nCol; /* Number of columns in parent key */
109606 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
109613 /* If this is a non-composite (single column) foreign key, check if it
109619 ** Non-composite foreign keys do not require the aiCol array.
109629 if( pParent->iPKey>=0 ){
109631 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
109635 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
109640 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
109641 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
109653 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
109658 /* If zKey is non-NULL, then this foreign key was declared to
109664 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
109673 zDfltColl = pParent->aCol[iCol].zColl;
109675 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
109677 zIdxCol = pParent->aCol[iCol].zName;
109679 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
109680 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
109692 if( !pParse->disableTriggers ){
109694 "foreign key mismatch - \"%w\" referencing \"%w\"",
109695 pFKey->pFrom->zName, pFKey->zTo);
109697 sqlite3DbFree(pParse->db, aiCol);
109709 ** affected - once to "delete" the old row, and then again to "insert" the
109719 ** --------------------------------------------------------------------------
109744 int iCur = pParse->nTab - 1; /* Cursor number to use */
109755 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
109758 for(i=0; i<pFKey->nCol; i++){
109780 ** to increment the constraint-counter (i.e. this is an INSERT operation),
109782 ** increment the constraint-counter. */
109783 if( pTab==pFKey->pFrom && nIncr==1 ){
109791 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
109795 int nCol = pFKey->nCol;
109799 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
109806 ** to increment the constraint-counter (i.e. this is an INSERT operation),
109808 ** increment the constraint-counter.
109810 ** If any of the parent-key values are NULL, then the row cannot match
109812 ** of the parent-key values are NULL (at this point it is known that
109815 if( pTab==pFKey->pFrom && nIncr==1 ){
109819 int iParent = pIdx->aiColumn[i]+1+regData;
109820 assert( pIdx->aiColumn[i]>=0 );
109821 assert( aiCol[i]!=pTab->iPKey );
109822 if( pIdx->aiColumn[i]==pTab->iPKey ){
109833 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
109841 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
109842 && !pParse->pToplevel
109843 && !pParse->isMultiWrite
109853 if( nIncr>0 && pFKey->isDeferred==0 ){
109856 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
109881 sqlite3 *db = pParse->db;
109885 if( iCol>=0 && iCol!=pTab->iPKey ){
109886 pCol = &pTab->aCol[iCol];
109887 pExpr->iTable = regBase + iCol + 1;
109888 pExpr->affinity = pCol->affinity;
109889 zColl = pCol->zColl;
109890 if( zColl==0 ) zColl = db->pDfltColl->zName;
109893 pExpr->iTable = regBase;
109894 pExpr->affinity = SQLITE_AFF_INTEGER;
109912 pExpr->pTab = pTab;
109913 pExpr->iTable = iCursor;
109914 pExpr->iColumn = iCol;
109923 ** code for an SQL UPDATE operation, this function may be called twice -
109926 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
109935 ** --------------------------------------------------------------------------
109961 sqlite3 *db = pParse->db; /* Database handle */
109969 assert( pIdx==0 || pIdx->pTable==pTab );
109970 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
109971 assert( pIdx!=0 || pFKey->nCol==1 );
109975 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
109981 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
109987 for(i=0; i<pFKey->nCol; i++){
109994 iCol = pIdx ? pIdx->aiColumn[i] : -1;
109996 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
109998 zCol = pFKey->pFrom->aCol[iCol].zName;
110015 if( pTab==pFKey->pFrom && nIncr>0 ){
110020 pLeft = exprTableRegister(pParse, pTab, regData, -1);
110021 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
110027 for(i=0; i<pPk->nKeyCol; i++){
110028 i16 iCol = pIdx->aiColumn[i];
110031 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
110049 if( pParse->nErr==0 ){
110051 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
110079 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
110085 ** and all of its sub-components.
110087 ** The Trigger structure or any of its sub-components may be allocated from
110092 TriggerStep *pStep = p->step_list;
110093 sqlite3ExprDelete(dbMem, pStep->pWhere);
110094 sqlite3ExprListDelete(dbMem, pStep->pExprList);
110095 sqlite3SelectDelete(dbMem, pStep->pSelect);
110096 sqlite3ExprDelete(dbMem, p->pWhen);
110119 sqlite3 *db = pParse->db;
110120 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
110132 for(p=pTab->pFKey; p; p=p->pNextFrom){
110133 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
110140 pParse->disableTriggers = 1;
110142 pParse->disableTriggers = 0;
110153 if( (db->flags & SQLITE_DeferFKs)==0 ){
110173 ** is set to -1). If the rowid column is modified by the UPDATE statement
110174 ** the bChngRowid argument is non-zero.
110186 for(i=0; i<p->nCol; i++){
110187 int iChildKey = p->aCol[i].iFrom;
110189 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
110200 ** is set to -1). If the rowid column is modified by the UPDATE statement
110201 ** the bChngRowid argument is non-zero.
110213 for(i=0; i<p->nCol; i++){
110214 char *zKey = p->aCol[i].zCol;
110216 for(iKey=0; iKey<pTab->nCol; iKey++){
110217 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
110218 Column *pCol = &pTab->aCol[iKey];
110220 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
110221 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
110237 if( pTop->pTriggerPrg ){
110238 Trigger *p = pTop->pTriggerPrg->pTrigger;
110239 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
110240 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
110254 ** first register in an array of (pTab->nCol+1) registers containing the
110260 ** first register of an array of (pTab->nCol+1) registers containing the new
110276 sqlite3 *db = pParse->db; /* Database handle */
110280 int isIgnoreErrors = pParse->disableTriggers;
110282 /* Exactly one of regOld and regNew should be non-zero. */
110285 /* If foreign-keys are disabled, this function is a no-op. */
110286 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
110288 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110289 zDb = db->aDb[iDb].zDbSName;
110293 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
110303 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
110313 if( pParse->disableTriggers ){
110314 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
110316 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
110320 if( !isIgnoreErrors || db->mallocFailed ) return;
110327 ** FK counter for each row of the current table with non-NULL keys.
110330 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
110331 for(i=0; i<pFKey->nCol; i++){
110332 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
110335 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
110339 assert( pFKey->nCol==1 || (aiFree && pIdx) );
110344 iCol = pFKey->aCol[0].iFrom;
110347 for(i=0; i<pFKey->nCol; i++){
110348 if( aiCol[i]==pTab->iPKey ){
110349 aiCol[i] = -1;
110351 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
110356 if( db->xAuth ){
110358 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
110359 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
110365 /* Take a shared-cache advisory read-lock on the parent table. Allocate
110368 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
110369 pParse->nTab++;
110375 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
110394 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
110403 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
110404 && !pParse->pToplevel && !pParse->isMultiWrite
110413 if( !isIgnoreErrors || db->mallocFailed ) return;
110416 assert( aiCol || pFKey->nCol==1 );
110422 struct SrcList_item *pItem = pSrc->a;
110423 pItem->pTab = pFKey->pFrom;
110424 pItem->zName = pFKey->pFrom->zName;
110425 pItem->pTab->nTabRef++;
110426 pItem->iCursor = pParse->nTab++;
110429 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
110432 int eAction = pFKey->aAction[aChange!=0];
110437 ** So do not set the "may-abort" flag in this case.
110440 ** may-abort flag will eventually be set on this statement anyway
110451 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
110455 pItem->zName = 0;
110473 if( pParse->db->flags&SQLITE_ForeignKeys ){
110476 for(p=pTab->pFKey; p; p=p->pNextFrom){
110477 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
110479 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
110483 for(i=0; i<pIdx->nKeyCol; i++){
110484 assert( pIdx->aiColumn[i]>=0 );
110485 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
110500 ** entry in the aChange[] array is set to -1. If the column is modified,
110505 ** non-zero. If there is no foreign key related processing, this function
110519 int *aChange, /* Non-NULL for UPDATE operations */
110523 if( pParse->db->flags&SQLITE_ForeignKeys ){
110528 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
110535 for(p=pTab->pFKey; p; p=p->pNextFrom){
110536 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
110543 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
110545 if( p->aAction[1]!=OE_None ) return 2;
110556 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
110565 ** sub-system, code for them is created by fkScanChildren()).
110587 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
110589 sqlite3 *db = pParse->db; /* Database handle */
110594 action = pFKey->aAction[iAction];
110595 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
110598 pTrigger = pFKey->apTrigger[iAction];
110604 int *aiCol = 0; /* child table cols -> parent key cols */
110613 assert( aiCol || pFKey->nCol==1 );
110615 for(i=0; i<pFKey->nCol; i++){
110623 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
110625 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
110626 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
110628 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
110629 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
110667 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
110682 zFrom = pFKey->pFrom->zName;
110693 pRaise->affinity = OE_Abort;
110705 db->lookaside.bDisable++;
110710 nFrom + 1 /* Space for pStep->zTarget */
110713 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
110714 pStep->zTarget = (char *)&pStep[1];
110715 memcpy((char *)pStep->zTarget, zFrom, nFrom);
110717 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
110718 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
110719 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
110722 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
110726 /* Re-enable the lookaside buffer, if it was disabled earlier. */
110727 db->lookaside.bDisable--;
110733 if( db->mallocFailed==1 ){
110741 pStep->op = TK_SELECT;
110745 pStep->op = TK_DELETE;
110749 pStep->op = TK_UPDATE;
110751 pStep->pTrig = pTrigger;
110752 pTrigger->pSchema = pTab->pSchema;
110753 pTrigger->pTabSchema = pTab->pSchema;
110754 pFKey->apTrigger[iAction] = pTrigger;
110755 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
110768 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
110773 /* If foreign-key support is enabled, iterate through all FKs that
110776 ** trigger sub-program. */
110777 if( pParse->db->flags&SQLITE_ForeignKeys ){
110779 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
110799 FKey *pNext; /* Copy of pFKey->pNextFrom */
110802 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
110803 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
110806 if( !db || db->pnBytesFreed==0 ){
110807 if( pFKey->pPrevTo ){
110808 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
110810 void *p = (void *)pFKey->pNextTo;
110811 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
110812 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
110814 if( pFKey->pNextTo ){
110815 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
110819 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
110822 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
110826 fkTriggerDelete(db, pFKey->apTrigger[0]);
110827 fkTriggerDelete(db, pFKey->apTrigger[1]);
110830 pNext = pFKey->pNextFrom;
110874 sqlite3TableLock(pParse, iDb, pTab->tnum,
110875 (opcode==OP_OpenWrite)?1:0, pTab->zName);
110877 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
110878 VdbeComment((v, "%s", pTab->zName));
110882 assert( pPk->tnum==pTab->tnum );
110883 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
110885 VdbeComment((v, "%s", pTab->zName));
110895 ** ------------------------------
110910 if( !pIdx->zColAff ){
110920 Table *pTab = pIdx->pTable;
110921 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
110922 if( !pIdx->zColAff ){
110926 for(n=0; n<pIdx->nColumn; n++){
110927 i16 x = pIdx->aiColumn[n];
110929 pIdx->zColAff[n] = pTab->aCol[x].affinity;
110931 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
110935 assert( pIdx->aColExpr!=0 );
110936 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
110938 pIdx->zColAff[n] = aff;
110941 pIdx->zColAff[n] = 0;
110944 return pIdx->zColAff;
110960 ** ------------------------------
110969 char *zColAff = pTab->zColAff;
110972 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
110978 for(i=0; i<pTab->nCol; i++){
110979 zColAff[i] = pTab->aCol[i].affinity;
110982 zColAff[i--] = 0;
110984 pTab->zColAff = zColAff;
110991 sqlite3VdbeChangeP4(v, -1, zColAff, i);
110997 ** Return non-zero if the table pTab in database iDb or any of its indices
111007 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
111013 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
111015 int tnum = pOp->p2;
111016 if( tnum==pTab->tnum ){
111019 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
111020 if( tnum==pIndex->tnum ){
111026 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
111027 assert( pOp->p4.pVtab!=0 );
111028 assert( pOp->p4type==P4_VTAB );
111065 if( (pTab->tabFlags & TF_Autoincrement)!=0
111066 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
111071 pInfo = pToplevel->pAinc;
111072 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
111074 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
111076 pInfo->pNext = pToplevel->pAinc;
111077 pToplevel->pAinc = pInfo;
111078 pInfo->pTab = pTab;
111079 pInfo->iDb = iDb;
111080 pToplevel->nMem++; /* Register to hold name of table */
111081 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
111082 pToplevel->nMem++; /* Rowid in sqlite_sequence */
111084 memId = pInfo->regCtr;
111095 sqlite3 *db = pParse->db; /* The database connection */
111098 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
111100 /* This routine is never called during trigger-generation. It is
111101 ** only called from the top-level */
111102 assert( pParse->pTriggerTab==0 );
111106 for(p = pParse->pAinc; p; p = p->pNext){
111121 pDb = &db->aDb[p->iDb];
111122 memId = p->regCtr;
111123 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
111124 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
111125 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
111131 aOp[3].p1 = memId-1;
111150 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
111163 Vdbe *v = pParse->pVdbe;
111164 sqlite3 *db = pParse->db;
111167 for(p = pParse->pAinc; p; p = p->pNext){
111177 Db *pDb = &db->aDb[p->iDb];
111179 int memId = p->regCtr;
111182 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
111183 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
111188 aOp[2].p1 = memId-1;
111197 if( pParse->pAinc ) autoIncrementEnd(pParse);
111202 ** above are all no-ops
111226 ** then a list of all (non-hidden) columns for the table is substituted.
111231 ** first two forms shown above. A VALUES clause is really just short-hand
111237 ** insert with data coming from a single-row VALUES clause, the code executes
111238 ** once straight down through. Pseudo-code follows (we call this
111250 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
111274 ** X <- A
111282 ** end-coroutine X
111295 ** X <- A
111303 ** end co-routine R
111331 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
111359 db = pParse->db;
111360 if( pParse->nErr || db->mallocFailed ){
111369 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
111370 pList = pSelect->pEList;
111371 pSelect->pEList = 0;
111378 assert( pTabList->nSrc==1 );
111383 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
111384 assert( iDb<db->nDb );
111385 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
111386 db->aDb[iDb].zDbSName) ){
111396 isView = pTab->pSelect!=0;
111409 ** ViewGetColumnNames() is a no-op if pTab is not a view.
111415 /* Cannot insert into a read-only table.
111425 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
111453 regRowid = regIns = pParse->nMem+1;
111454 pParse->nMem += pTab->nCol + 1;
111457 pParse->nMem++;
111470 ** PRIMARY KEY in the original table is pTab->iPKey.)
111472 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
111474 for(i=0; i<pColumn->nId; i++){
111475 pColumn->a[i].idx = -1;
111477 for(i=0; i<pColumn->nId; i++){
111478 for(j=0; j<pTab->nCol; j++){
111479 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
111480 pColumn->a[i].idx = j;
111482 if( j==pTab->iPKey ){
111488 if( j>=pTab->nCol ){
111489 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
111494 pTabList, 0, pColumn->a[i].zName);
111495 pParse->checkSchema = 1;
111503 ** is coming from a SELECT statement, then generate a co-routine that
111505 ** co-routine is the common header to the 3rd and 4th templates.
111508 /* Data is coming from a SELECT or from a multi-row VALUES clause.
111509 ** Generate a co-routine to run the SELECT. */
111510 int regYield; /* Register holding co-routine entry-point */
111511 int addrTop; /* Top of the co-routine */
111514 regYield = ++pParse->nMem;
111519 dest.nSdst = pTab->nCol;
111522 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
111524 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
111525 assert( pSelect->pEList );
111526 nColumn = pSelect->pEList->nExpr;
111556 srcTab = pParse->nTab++;
111571 ** single-row VALUES clause
111576 srcTab = -1;
111579 nColumn = pList->nExpr;
111593 ipkColumn = pTab->iPKey;
111599 for(i=0; i<pTab->nCol; i++){
111600 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
111602 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
111605 pTabList, 0, pTab->nCol-nHidden, nColumn);
111608 if( pColumn!=0 && nColumn!=pColumn->nId ){
111609 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
111615 if( db->flags & SQLITE_CountRows ){
111616 regRowCount = ++pParse->nMem;
111623 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
111629 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
111631 aRegIdx[i] = ++pParse->nMem;
111632 pParse->nMem += pIdx->nColumn;
111666 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
111672 ** not happened yet) so we substitute a rowid of -1
111675 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
111683 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
111686 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
111698 for(i=j=0; i<pTab->nCol; i++){
111700 for(j=0; j<pColumn->nId; j++){
111701 if( pColumn->a[j].idx==i ) break;
111704 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
111705 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
111706 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
111711 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
111713 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
111727 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
111729 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
111747 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
111748 pOp = sqlite3VdbeGetOp(v, -1);
111750 if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
111752 pOp->opcode = OP_NewRowid;
111753 pOp->p1 = iDataCur;
111754 pOp->p2 = regRowid;
111755 pOp->p3 = regAutoinc;
111785 for(i=0; i<pTab->nCol; i++){
111787 if( i==pTab->iPKey ){
111792 ** As there may be shallow copies of this value, make it a soft-NULL */
111797 if( IsHiddenColumn(&pTab->aCol[i]) ){
111798 j = -1;
111801 j = i - nHidden;
111804 for(j=0; j<pColumn->nId; j++){
111805 if( pColumn->a[j].idx==i ) break;
111808 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
111809 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
111817 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
111828 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
111850 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
111860 if( (db->flags & SQLITE_CountRows)!=0 ){
111867 pTab, regData-2-pTab->nCol, onError, endOfLoop);
111888 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
111897 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
111925 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
111931 ** bit 0x01 of pWalker->eCode if
111932 ** pWalker->eCode to 0 if this expression node references any of the
111936 if( pExpr->op==TK_COLUMN ){
111937 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
111938 if( pExpr->iColumn>=0 ){
111939 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
111940 pWalker->eCode |= CKCNSTRNT_COLUMN;
111943 pWalker->eCode |= CKCNSTRNT_ROWID;
111950 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
111983 ** pTab->nCol+1 registers in this range. The first register (the one
112010 ** at pTab->pIndex.
112017 ** for the first index in the pTab->pIndex list. Cursors for other indices
112018 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
112026 ** --------------- ---------- ----------------------------------------
112056 ** Or if overrideError==OE_Default, then the pParse->onError parameter
112057 ** is used. Or if pParse->onError==OE_Default then the onError value
112068 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
112091 db = pParse->db;
112094 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
112095 nCol = pTab->nCol;
112106 nPkField = pPk->nKeyCol;
112116 if( i==pTab->iPKey ){
112123 onError = pTab->aCol[i].notNull;
112130 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
112141 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
112142 pTab->aCol[i].zName);
112159 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
112169 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
112170 ExprList *pCheck = pTab->pCheck;
112171 pParse->iSelfTab = -(regNewData+1);
112173 for(i=0; i<pCheck->nExpr; i++){
112175 Expr *pExpr = pCheck->a[i].pExpr;
112182 char *zName = pCheck->a[i].zName;
112183 if( zName==0 ) zName = pTab->zName;
112184 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
112191 pParse->iSelfTab = 0;
112202 onError = pTab->keyConf;
112224 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112225 if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
112251 ** recursive-triggers flag is set, call GenerateRowDelete() to
112253 ** the triggers and remove both the table and index b-tree entries.
112255 ** Otherwise, if there are no triggers or the recursive-triggers
112257 ** GenerateRowIndexDelete(). This removes the index b-tree entries
112258 ** only. The table b-tree entry will be replaced by the new entry
112273 if( db->flags&SQLITE_RecTriggers ){
112279 regNewData, 1, 0, OE_Replace, 1, -1);
112283 /* This OP_Delete opcode fires the pre-update-hook only. It does
112284 ** not modify the b-tree. It is more efficient to let the coming
112291 if( pTab->pIndex ){
112293 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
112319 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
112334 if( pIdx->pPartIdxWhere ){
112336 pParse->iSelfTab = -(regNewData+1);
112337 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
112339 pParse->iSelfTab = 0;
112346 for(i=0; i<pIdx->nColumn; i++){
112347 int iField = pIdx->aiColumn[i];
112350 pParse->iSelfTab = -(regNewData+1);
112351 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
112352 pParse->iSelfTab = 0;
112353 VdbeComment((v, "%s column %d", pIdx->zName, i));
112355 if( iField==XN_ROWID || iField==pTab->iPKey ){
112361 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
112364 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
112365 VdbeComment((v, "for %s", pIdx->zName));
112367 if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
112380 onError = pIdx->onError;
112398 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
112401 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
112403 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
112404 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
112413 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
112421 ** is different from old-rowid */
112430 ** store it in registers regR..regR+nPk-1 */
112432 for(i=0; i<pPk->nKeyCol; i++){
112433 assert( pPk->aiColumn[i]>=0 );
112434 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
112436 VdbeComment((v, "%s.%s", pTab->zName,
112437 pTab->aCol[pPk->aiColumn[i]].zName));
112448 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
112452 for(i=0; i<pPk->nKeyCol; i++){
112453 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
112454 x = pPk->aiColumn[i];
112456 if( i==(pPk->nKeyCol-1) ){
112489 if( db->flags&SQLITE_RecTriggers ){
112515 ** to be the number of columns in table pTab that must not be NULL-trimmed.
112517 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
112523 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
112524 if( pTab->pSchema->file_format<2 ) return;
112526 for(i=pTab->nCol-1; i>0; i--){
112527 if( pTab->aCol[i].pDflt!=0 ) break;
112528 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
112569 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
112570 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
112573 if( pIdx->pPartIdxWhere ){
112579 assert( pParse->nested==0 );
112593 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
112599 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
112603 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
112605 if( pParse->nested ){
112618 if( !pParse->nested ){
112634 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
112640 ** pTab->pIndex list.
112642 ** If pTab is a virtual table, then this routine is a no-op and the
112664 /* This routine is a no-op for virtual tables. Leave the output
112669 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
112672 if( iBase<0 ) iBase = pParse->nTab;
112678 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
112681 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
112683 assert( pIdx->pSchema==pTab->pSchema );
112689 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
112692 VdbeComment((v, "%s", pIdx->zName));
112695 if( iBase>pParse->nTab ) pParse->nTab = iBase;
112704 ** purposes only - to make sure the transfer optimization really
112726 assert( pDest->pTable!=pSrc->pTable );
112727 if( pDest->nKeyCol!=pSrc->nKeyCol ){
112730 if( pDest->onError!=pSrc->onError ){
112733 for(i=0; i<pSrc->nKeyCol; i++){
112734 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
112737 if( pSrc->aiColumn[i]==XN_EXPR ){
112738 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
112739 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
112740 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
112744 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
112747 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
112751 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
112769 ** There are lots of rules for determining compatibility - see comments
112774 ** is empty - a factor that can only be determined at run-time. In that
112791 sqlite3 *db = pParse->db;
112795 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
112810 if( pParse->pWith || pSelect->pWith ){
112825 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
112828 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
112829 if( pSelect->pSrc->nSrc!=1 ){
112832 if( pSelect->pSrc->a[0].pSelect ){
112835 if( pSelect->pWhere ){
112838 if( pSelect->pOrderBy ){
112843 if( pSelect->pGroupBy ){
112846 if( pSelect->pLimit ){
112849 if( pSelect->pPrior ){
112852 if( pSelect->selFlags & SF_Distinct ){
112855 pEList = pSelect->pEList;
112857 if( pEList->nExpr!=1 ){
112860 assert( pEList->a[0].pExpr );
112861 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
112869 pItem = pSelect->pSrc->a;
112885 if( pSrc->pSelect ){
112888 if( pDest->nCol!=pSrc->nCol ){
112891 if( pDest->iPKey!=pSrc->iPKey ){
112894 for(i=0; i<pDest->nCol; i++){
112895 Column *pDestCol = &pDest->aCol[i];
112896 Column *pSrcCol = &pSrc->aCol[i];
112898 if( (db->mDbFlags & DBFLAG_Vacuum)==0
112899 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
112904 if( pDestCol->affinity!=pSrcCol->affinity ){
112907 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
112910 if( pDestCol->notNull && !pSrcCol->notNull ){
112915 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
112916 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
112917 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
112918 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
112919 pSrcCol->pDflt->u.zToken)!=0)
112925 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
112929 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
112937 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
112949 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
112953 if( (db->flags & SQLITE_CountRows)!=0 ){
112964 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
112967 iSrc = pParse->nTab++;
112968 iDest = pParse->nTab++;
112974 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
112975 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
113004 if( pDest->iPKey>=0 ){
113011 }else if( pDest->pIndex==0 ){
113015 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
113018 if( db->mDbFlags & DBFLAG_Vacuum ){
113032 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
113033 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
113035 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
113037 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
113041 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
113043 VdbeComment((v, "%s", pSrcIdx->zName));
113044 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
113047 VdbeComment((v, "%s", pDestIdx->zName));
113050 if( db->mDbFlags & DBFLAG_Vacuum ){
113055 ** order. In this case, instead of seeking within the b-tree as part
113057 ** OP_IdxInsert to seek to the point within the b-tree where each key
113065 for(i=0; i<pSrcIdx->nColumn; i++){
113066 const char *zColl = pSrcIdx->azColl[i];
113069 if( i==pSrcIdx->nColumn ){
113074 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
113146 sqlite3_mutex_enter(db->mutex);
113153 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
113159 /* this happens for a comment or white-space */
113174 && db->flags&SQLITE_NullCallback)) ){
113200 /* EVIDENCE-OF: R-38229-40159 If the callback function to
113201 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
113239 assert( (rc&db->errMask)==rc );
113240 sqlite3_mutex_leave(db->mutex);
113580 ** (part of the main SQLite library - not an extension) so that
113587 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
113589 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
113591 #define sqlite3_bind_blob sqlite3_api->bind_blob
113592 #define sqlite3_bind_double sqlite3_api->bind_double
113593 #define sqlite3_bind_int sqlite3_api->bind_int
113594 #define sqlite3_bind_int64 sqlite3_api->bind_int64
113595 #define sqlite3_bind_null sqlite3_api->bind_null
113596 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
113597 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
113598 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
113599 #define sqlite3_bind_text sqlite3_api->bind_text
113600 #define sqlite3_bind_text16 sqlite3_api->bind_text16
113601 #define sqlite3_bind_value sqlite3_api->bind_value
113602 #define sqlite3_busy_handler sqlite3_api->busy_handler
113603 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
113604 #define sqlite3_changes sqlite3_api->changes
113605 #define sqlite3_close sqlite3_api->close
113606 #define sqlite3_collation_needed sqlite3_api->collation_needed
113607 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
113608 #define sqlite3_column_blob sqlite3_api->column_blob
113609 #define sqlite3_column_bytes sqlite3_api->column_bytes
113610 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
113611 #define sqlite3_column_count sqlite3_api->column_count
113612 #define sqlite3_column_database_name sqlite3_api->column_database_name
113613 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
113614 #define sqlite3_column_decltype sqlite3_api->column_decltype
113615 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
113616 #define sqlite3_column_double sqlite3_api->column_double
113617 #define sqlite3_column_int sqlite3_api->column_int
113618 #define sqlite3_column_int64 sqlite3_api->column_int64
113619 #define sqlite3_column_name sqlite3_api->column_name
113620 #define sqlite3_column_name16 sqlite3_api->column_name16
113621 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
113622 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
113623 #define sqlite3_column_table_name sqlite3_api->column_table_name
113624 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
113625 #define sqlite3_column_text sqlite3_api->column_text
113626 #define sqlite3_column_text16 sqlite3_api->column_text16
113627 #define sqlite3_column_type sqlite3_api->column_type
113628 #define sqlite3_column_value sqlite3_api->column_value
113629 #define sqlite3_commit_hook sqlite3_api->commit_hook
113630 #define sqlite3_complete sqlite3_api->complete
113631 #define sqlite3_complete16 sqlite3_api->complete16
113632 #define sqlite3_create_collation sqlite3_api->create_collation
113633 #define sqlite3_create_collation16 sqlite3_api->create_collation16
113634 #define sqlite3_create_function sqlite3_api->create_function
113635 #define sqlite3_create_function16 sqlite3_api->create_function16
113636 #define sqlite3_create_module sqlite3_api->create_module
113637 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
113638 #define sqlite3_data_count sqlite3_api->data_count
113639 #define sqlite3_db_handle sqlite3_api->db_handle
113640 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
113641 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
113642 #define sqlite3_errcode sqlite3_api->errcode
113643 #define sqlite3_errmsg sqlite3_api->errmsg
113644 #define sqlite3_errmsg16 sqlite3_api->errmsg16
113645 #define sqlite3_exec sqlite3_api->exec
113647 #define sqlite3_expired sqlite3_api->expired
113649 #define sqlite3_finalize sqlite3_api->finalize
113650 #define sqlite3_free sqlite3_api->free
113651 #define sqlite3_free_table sqlite3_api->free_table
113652 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
113653 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
113654 #define sqlite3_get_table sqlite3_api->get_table
113656 #define sqlite3_global_recover sqlite3_api->global_recover
113658 #define sqlite3_interrupt sqlite3_api->interruptx
113659 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
113660 #define sqlite3_libversion sqlite3_api->libversion
113661 #define sqlite3_libversion_number sqlite3_api->libversion_number
113662 #define sqlite3_malloc sqlite3_api->malloc
113663 #define sqlite3_mprintf sqlite3_api->mprintf
113664 #define sqlite3_open sqlite3_api->open
113665 #define sqlite3_open16 sqlite3_api->open16
113666 #define sqlite3_prepare sqlite3_api->prepare
113667 #define sqlite3_prepare16 sqlite3_api->prepare16
113668 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
113669 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
113670 #define sqlite3_profile sqlite3_api->profile
113671 #define sqlite3_progress_handler sqlite3_api->progress_handler
113672 #define sqlite3_realloc sqlite3_api->realloc
113673 #define sqlite3_reset sqlite3_api->reset
113674 #define sqlite3_result_blob sqlite3_api->result_blob
113675 #define sqlite3_result_double sqlite3_api->result_double
113676 #define sqlite3_result_error sqlite3_api->result_error
113677 #define sqlite3_result_error16 sqlite3_api->result_error16
113678 #define sqlite3_result_int sqlite3_api->result_int
113679 #define sqlite3_result_int64 sqlite3_api->result_int64
113680 #define sqlite3_result_null sqlite3_api->result_null
113681 #define sqlite3_result_text sqlite3_api->result_text
113682 #define sqlite3_result_text16 sqlite3_api->result_text16
113683 #define sqlite3_result_text16be sqlite3_api->result_text16be
113684 #define sqlite3_result_text16le sqlite3_api->result_text16le
113685 #define sqlite3_result_value sqlite3_api->result_value
113686 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
113687 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
113688 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
113689 #define sqlite3_snprintf sqlite3_api->xsnprintf
113690 #define sqlite3_step sqlite3_api->step
113691 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
113692 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
113693 #define sqlite3_total_changes sqlite3_api->total_changes
113694 #define sqlite3_trace sqlite3_api->trace
113696 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
113698 #define sqlite3_update_hook sqlite3_api->update_hook
113699 #define sqlite3_user_data sqlite3_api->user_data
113700 #define sqlite3_value_blob sqlite3_api->value_blob
113701 #define sqlite3_value_bytes sqlite3_api->value_bytes
113702 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
113703 #define sqlite3_value_double sqlite3_api->value_double
113704 #define sqlite3_value_int sqlite3_api->value_int
113705 #define sqlite3_value_int64 sqlite3_api->value_int64
113706 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
113707 #define sqlite3_value_text sqlite3_api->value_text
113708 #define sqlite3_value_text16 sqlite3_api->value_text16
113709 #define sqlite3_value_text16be sqlite3_api->value_text16be
113710 #define sqlite3_value_text16le sqlite3_api->value_text16le
113711 #define sqlite3_value_type sqlite3_api->value_type
113712 #define sqlite3_vmprintf sqlite3_api->vmprintf
113713 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
113714 #define sqlite3_overload_function sqlite3_api->overload_function
113715 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
113716 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
113717 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
113718 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
113719 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
113720 #define sqlite3_blob_close sqlite3_api->blob_close
113721 #define sqlite3_blob_open sqlite3_api->blob_open
113722 #define sqlite3_blob_read sqlite3_api->blob_read
113723 #define sqlite3_blob_write sqlite3_api->blob_write
113724 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
113725 #define sqlite3_file_control sqlite3_api->file_control
113726 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
113727 #define sqlite3_memory_used sqlite3_api->memory_used
113728 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
113729 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
113730 #define sqlite3_mutex_free sqlite3_api->mutex_free
113731 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
113732 #define sqlite3_mutex_try sqlite3_api->mutex_try
113733 #define sqlite3_open_v2 sqlite3_api->open_v2
113734 #define sqlite3_release_memory sqlite3_api->release_memory
113735 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
113736 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
113737 #define sqlite3_sleep sqlite3_api->sleep
113738 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
113739 #define sqlite3_vfs_find sqlite3_api->vfs_find
113740 #define sqlite3_vfs_register sqlite3_api->vfs_register
113741 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
113742 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
113743 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
113744 #define sqlite3_result_error_code sqlite3_api->result_error_code
113745 #define sqlite3_test_control sqlite3_api->test_control
113746 #define sqlite3_randomness sqlite3_api->randomness
113747 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
113748 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
113749 #define sqlite3_limit sqlite3_api->limit
113750 #define sqlite3_next_stmt sqlite3_api->next_stmt
113751 #define sqlite3_sql sqlite3_api->sql
113752 #define sqlite3_status sqlite3_api->status
113753 #define sqlite3_backup_finish sqlite3_api->backup_finish
113754 #define sqlite3_backup_init sqlite3_api->backup_init
113755 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
113756 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
113757 #define sqlite3_backup_step sqlite3_api->backup_step
113758 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
113759 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
113760 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
113761 #define sqlite3_db_config sqlite3_api->db_config
113762 #define sqlite3_db_mutex sqlite3_api->db_mutex
113763 #define sqlite3_db_status sqlite3_api->db_status
113764 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
113765 #define sqlite3_log sqlite3_api->log
113766 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
113767 #define sqlite3_sourceid sqlite3_api->sourceid
113768 #define sqlite3_stmt_status sqlite3_api->stmt_status
113769 #define sqlite3_strnicmp sqlite3_api->strnicmp
113770 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
113771 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
113772 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
113773 #define sqlite3_wal_hook sqlite3_api->wal_hook
113774 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
113775 #define sqlite3_vtab_config sqlite3_api->vtab_config
113776 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
113778 #define sqlite3_close_v2 sqlite3_api->close_v2
113779 #define sqlite3_db_filename sqlite3_api->db_filename
113780 #define sqlite3_db_readonly sqlite3_api->db_readonly
113781 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
113782 #define sqlite3_errstr sqlite3_api->errstr
113783 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
113784 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
113785 #define sqlite3_stricmp sqlite3_api->stricmp
113786 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
113787 #define sqlite3_uri_int64 sqlite3_api->uri_int64
113788 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
113789 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
113790 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
113792 #define sqlite3_auto_extension sqlite3_api->auto_extension
113793 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
113794 #define sqlite3_bind_text64 sqlite3_api->bind_text64
113795 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
113796 #define sqlite3_load_extension sqlite3_api->load_extension
113797 #define sqlite3_malloc64 sqlite3_api->malloc64
113798 #define sqlite3_msize sqlite3_api->msize
113799 #define sqlite3_realloc64 sqlite3_api->realloc64
113800 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
113801 #define sqlite3_result_blob64 sqlite3_api->result_blob64
113802 #define sqlite3_result_text64 sqlite3_api->result_text64
113803 #define sqlite3_strglob sqlite3_api->strglob
113805 #define sqlite3_value_dup sqlite3_api->value_dup
113806 #define sqlite3_value_free sqlite3_api->value_free
113807 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
113808 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
113810 #define sqlite3_value_subtype sqlite3_api->value_subtype
113811 #define sqlite3_result_subtype sqlite3_api->result_subtype
113813 #define sqlite3_status64 sqlite3_api->status64
113814 #define sqlite3_strlike sqlite3_api->strlike
113815 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
113817 #define sqlite3_system_errno sqlite3_api->system_errno
113819 #define sqlite3_trace_v2 sqlite3_api->trace_v2
113820 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
113822 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
113824 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
113825 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
113826 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
113827 #define sqlite3_result_pointer sqlite3_api->result_pointer
113828 #define sqlite3_value_pointer sqlite3_api->value_pointer
113830 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
113831 #define sqlite3_value_nochange sqltie3_api->value_nochange
113832 #define sqlite3_vtab_collation sqltie3_api->vtab_collation
113845 # define SQLITE_EXTENSION_INIT1 /*no-op*/
113847 # define SQLITE_EXTENSION_INIT3 /*no-op*/
114292 sqlite3_vfs *pVfs = db->pVfs;
114324 if( (db->flags & SQLITE_LoadExtension)==0 ){
114350 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
114377 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
114396 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
114415 /* Append the new shared library handle to the db->aExtension array. */
114416 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
114420 if( db->nExtension>0 ){
114421 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
114423 sqlite3DbFree(db, db->aExtension);
114424 db->aExtension = aHandle;
114426 db->aExtension[db->nExtension++] = handle;
114436 sqlite3_mutex_enter(db->mutex);
114439 sqlite3_mutex_leave(db->mutex);
114449 assert( sqlite3_mutex_held(db->mutex) );
114450 for(i=0; i<db->nExtension; i++){
114451 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
114453 sqlite3DbFree(db, db->aExtension);
114461 sqlite3_mutex_enter(db->mutex);
114463 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
114465 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
114467 sqlite3_mutex_leave(db->mutex);
114488 ** we have to locate the state vector at run-time. In the more common
114548 ** routine is a no-op.
114563 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
114565 wsdAutoext.nExt--;
114732 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
114736 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
114738 /* Names of columns for pragmas that return multi-column result
114739 ** or that return single-column results where the name of the
114797 /* Definitions of all built-in pragmas */
115385 ** Interpret the given string as an auto-vacuum mode value.
115403 ** backed temporary databases, 2 for the Red-Black tree in memory database
115404 ** and 0 to use the compile-time default.
115408 return z[0] - '0';
115425 sqlite3 *db = pParse->db;
115426 if( db->aDb[1].pBt!=0 ){
115427 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
115432 sqlite3BtreeClose(db->aDb[1].pBt);
115433 db->aDb[1].pBt = 0;
115448 sqlite3 *db = pParse->db;
115449 if( db->temp_store==ts ) return SQLITE_OK;
115453 db->temp_store = (u8)ts;
115465 u8 n = pPragma->nPragCName;
115468 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
115471 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
115505 if( db->autoCommit ){
115506 Db *pDb = db->aDb;
115507 int n = db->nDb;
115513 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
115514 while( (n--) > 0 ){
115515 if( pDb->pBt ){
115516 sqlite3BtreeSetPagerFlags(pDb->pBt,
115517 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
115524 # define setAllPagerFlags(X) /* no-op */
115529 ** Return a human-readable name for a constraint resolution action.
115550 ** journal-mode name.
115577 upr = ArraySize(aPragmaName)-1;
115583 upr = mid - 1;
115594 ** Generate code to output a single-column result row with a value of the
115627 int minusFlag /* True if a '-' sign preceded <value> */
115629 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
115630 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
115636 sqlite3 *db = pParse->db; /* The database connection */
115643 pParse->nMem = 2;
115649 pDb = &db->aDb[iDb];
115661 zRight = sqlite3MPrintf(db, "-%T", pValue);
115667 zDb = pId2->n>0 ? pDb->zDbSName : 0;
115672 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
115674 ** handled the pragma and generate a no-op prepared statement.
115676 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
115681 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
115691 db->busyHandler.nBusy = 0;
115705 pParse->nErr++;
115706 pParse->rc = rc;
115715 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
115720 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
115721 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
115727 switch( pPragma->ePragTyp ){
115762 pParse->nMem += 2;
115774 pDb->pSchema->cache_size = size;
115775 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
115792 Btree *pBt = pDb->pBt;
115798 /* Malloc may fail when setting the page-size, as there is an internal
115801 db->nextPagesize = sqlite3Atoi(zRight);
115802 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
115818 Btree *pBt = pDb->pBt;
115819 int b = -1;
115828 if( pId2->n==0 && b>=0 ){
115830 for(ii=0; ii<db->nDb; ii++){
115831 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
115859 iReg = ++pParse->nMem;
115878 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
115881 ** the locking-mode of the main database).
115883 eMode = db->dfltLockMode;
115886 if( pId2->n==0 ){
115888 ** of the PRAGMA command. In this case the locking-mode must be
115896 assert(pDb==&db->aDb[0]);
115897 for(ii=2; ii<db->nDb; ii++){
115898 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
115901 db->dfltLockMode = (u8)eMode;
115903 pPager = sqlite3BtreePager(pDb->pBt);
115941 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
115944 pId2->n = 1;
115946 for(ii=db->nDb-1; ii>=0; ii--){
115947 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
115963 Pager *pPager = sqlite3BtreePager(pDb->pBt);
115964 i64 iLimit = -2;
115967 if( iLimit<-1 ) iLimit = -1;
115980 ** Get or set the value of the database 'auto-vacuum' parameter.
115985 Btree *pBt = pDb->pBt;
115992 db->nextAutovac = (u8)eAuto;
115994 ** incr-vacuum flags. This is required in case this connection
115996 ** as an auto-vacuum capable db.
116003 ** that this really is an auto-vacuum capable database.
116022 aOp[4].p3 = eAuto - 1;
116045 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
116061 ** number of pages is adjusted so that the cache uses -N kibibytes
116067 returnSingleInt(v, pDb->pSchema->cache_size);
116070 pDb->pSchema->cache_size = size;
116071 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
116088 ** number of pages is adjusted so that the cache uses -N kibibytes
116102 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
116103 sqlite3BtreeSetSpillSize(pDb->pBt,0));
116107 sqlite3BtreeSetSpillSize(pDb->pBt, size);
116110 db->flags |= SQLITE_CacheSpill;
116112 db->flags &= ~SQLITE_CacheSpill;
116141 if( pId2->n==0 ) db->szMmap = sz;
116142 for(ii=db->nDb-1; ii>=0; ii--){
116143 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
116144 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
116148 sz = -1;
116157 pParse->nErr++;
116158 pParse->rc = rc;
116171 ** Note that it is possible for the library compile-time options to
116176 returnSingleInt(v, db->temp_store);
116200 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
116207 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
116208 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
116244 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
116273 Pager *pPager = sqlite3BtreePager(pDb->pBt);
116280 Pager *pPager = sqlite3BtreePager(pDb->pBt);
116310 returnSingleInt(v, pDb->safety_level-1);
116312 if( !db->autoCommit ){
116318 pDb->safety_level = iLevel;
116319 pDb->bSyncSet = 1;
116331 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
116333 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
116334 if( db->autoCommit==0 ){
116336 ** in auto-commit mode. */
116340 if( db->auth.authLevel==UAUTH_User ){
116341 /* Do not allow non-admin users to modify the schema arbitrarily */
116347 db->flags |= mask;
116349 db->flags &= ~mask;
116350 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
116353 /* Many of the flag-pragmas modify the code generated by the SQL
116376 ** pk: Non-zero for PK fields.
116386 pParse->nMem = 6;
116389 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
116394 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
116399 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
116401 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
116403 i-nHidden,
116404 pCol->zName,
116406 pCol->notNull ? 1 : 0,
116407 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
116418 pParse->nMem = 5;
116420 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
116423 pTab->zName,
116425 pTab->szTabRow,
116426 pTab->nRowLogEst,
116427 pTab->tabFlags);
116428 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116430 pIdx->zName,
116431 pIdx->szIdxRow,
116432 pIdx->aiRowLogEst[0],
116433 pIdx->hasStat1);
116448 if( pPragma->iArg ){
116450 mx = pIdx->nColumn;
116451 pParse->nMem = 6;
116454 mx = pIdx->nKeyCol;
116455 pParse->nMem = 3;
116457 pTab = pIdx->pTable;
116459 assert( pParse->nMem<=pPragma->nPragCName );
116461 i16 cnum = pIdx->aiColumn[i];
116463 cnum<0 ? 0 : pTab->aCol[cnum].zName);
116464 if( pPragma->iArg ){
116466 pIdx->aSortOrder[i],
116467 pIdx->azColl[i],
116468 i<pIdx->nKeyCol);
116470 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
116482 pParse->nMem = 5;
116484 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
116488 pIdx->zName,
116490 azOrigin[pIdx->idxType],
116491 pIdx->pPartIdxWhere!=0);
116499 pParse->nMem = 3;
116500 for(i=0; i<db->nDb; i++){
116501 if( db->aDb[i].pBt==0 ) continue;
116502 assert( db->aDb[i].zDbSName!=0 );
116505 db->aDb[i].zDbSName,
116506 sqlite3BtreeGetFilename(db->aDb[i].pBt));
116514 pParse->nMem = 2;
116515 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
116517 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
116527 pParse->nMem = 2;
116529 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
116530 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
116533 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
116535 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
116543 pParse->nMem = 1;
116544 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
116546 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
116569 pFK = pTab->pFKey;
116572 pParse->nMem = 8;
116576 for(j=0; j<pFK->nCol; j++){
116580 pFK->zTo,
116581 pTab->aCol[pFK->aCol[j].iFrom].zName,
116582 pFK->aCol[j].zCol,
116583 actionName(pFK->aAction[1]), /* ON UPDATE */
116584 actionName(pFK->aAction[0]), /* ON DELETE */
116588 pFK = pFK->pNextFrom;
116614 regResult = pParse->nMem+1;
116615 pParse->nMem += 4;
116616 regKey = ++pParse->nMem;
116617 regRow = ++pParse->nMem;
116619 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
116628 if( pTab==0 || pTab->pFKey==0 ) continue;
116629 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
116630 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
116632 sqlite3VdbeLoadString(v, regResult, pTab->zName);
116633 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
116634 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
116637 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
116643 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
116651 assert( pParse->nErr>0 || pFK==0 );
116653 if( pParse->nTab<i ) pParse->nTab = i;
116655 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
116656 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
116669 for(j=0; j<pFK->nCol; j++){
116670 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
116678 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
116679 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
116686 assert( pFK->nCol==1 );
116695 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
116745 ** without the overhead of cross-checking indexes. Quick_check
116760 ** to -1 here, to indicate that the VDBE should verify the integrity
116763 assert( iDb==0 || pId2->z );
116764 if( pId2->z==0 ) iDb = -1;
116767 pParse->nMem = 6;
116777 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
116780 for(i=0; i<db->nDb; i++){
116792 /* Do an integrity check of the B-Tree
116798 pTbls = &db->aDb[i].pSchema->tblHash;
116804 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
116812 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
116813 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116814 aRoot[++cnt] = pIdx->tnum;
116820 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
116823 /* Do the b-tree integrity checks */
116828 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
116842 int r1 = -1;
116844 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
116850 ** reg[8+i] counts the number of entries in the i-th index
116853 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
116856 assert( pParse->nMem>=8+j );
116861 for(j=0; j<pTab->nCol; j++){
116864 if( j==pTab->iPKey ) continue;
116865 if( pTab->aCol[j].notNull==0 ) continue;
116869 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
116870 pTab->aCol[j].zName);
116876 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116877 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
116878 if( db->mallocFailed==0 ){
116883 pParse->iSelfTab = iDataCur + 1;
116885 for(k=pCheck->nExpr-1; k>0; k--){
116886 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
116888 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
116891 pParse->iSelfTab = 0;
116893 pTab->zName);
116903 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
116906 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
116916 pIdx->nColumn); VdbeCoverage(v);
116921 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
116932 for(kk=0; kk<pIdx->nKeyCol; kk++){
116933 int iCol = pIdx->aiColumn[kk];
116934 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
116935 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
116943 pIdx->nKeyCol); VdbeCoverage(v);
116944 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
116953 sqlite3VdbeJumpHere(v, loopTop-1);
116957 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
116962 sqlite3VdbeLoadString(v, 4, pIdx->zName);
116986 aOp[0].p2 = 1-mxErr;
116992 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
117001 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
117006 ** The second form of this pragma is a no-op if the main database file
117027 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
117028 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
117029 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
117032 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
117042 returnSingleText(v, encnames[ENC(pParse->db)].zName);
117053 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
117054 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
117056 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
117060 if( !pEnc->zName ){
117085 ** the value of the schema-version and user-version, respectively. Both
117086 ** the schema-version and the user-version are 32-bit signed integers
117089 ** The schema-cookie is usually only manipulated internally by SQLite. It
117096 ** the schema-version is potentially dangerous and may lead to program
117099 ** The user-version is not used internally by SQLite. It may be used by
117103 int iCookie = pPragma->iArg; /* Which cookie to read or write */
117105 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
117143 ** Return the names of all compile-time options used in this build,
117149 pParse->nMem = 1;
117166 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
117177 pParse->nMem = 3;
117196 db->xWalCallback==sqlite3WalDefaultHook ?
117197 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
117205 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
117247 ** ever added that should be off by default, those off-by-default
117257 ** (2) The query planner used sqlite_stat1-style statistics for one or
117285 iTabCur = pParse->nTab++;
117286 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
117289 pSchema = db->aDb[iDb].pSchema;
117290 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
117296 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
117299 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
117300 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117301 if( !pIdx->hasStat1 ){
117313 db->aDb[iDb].zDbSName, pTab->zName);
117337 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
117341 returnSingleInt(v, db->busyTimeout);
117349 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
117351 ** specified and is a non-negative integer.
117352 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
117354 ** sqlite3_soft_heap_limit64(-1) C-language function.
117361 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
117380 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
117393 pParse->nMem = 2;
117394 for(i=0; i<db->nDb; i++){
117398 if( db->aDb[i].zDbSName==0 ) continue;
117399 pBt = db->aDb[i].pBt;
117402 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
117406 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
117442 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
117447 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
117457 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
117462 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
117513 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
117518 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
117523 if( pPragma->mPragFlg & PragFlg_Result1 ){
117527 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
117533 assert( strlen(zBuf) < sizeof(zBuf)-1 );
117541 pTab->pName = pPragma;
117542 pTab->db = db;
117543 pTab->iHidden = i;
117544 pTab->nHidden = j;
117576 pIdxInfo->estimatedCost = (double)1;
117577 if( pTab->nHidden==0 ){ return SQLITE_OK; }
117578 pConstraint = pIdxInfo->aConstraint;
117581 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
117582 if( pConstraint->usable==0 ) continue;
117583 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
117584 if( pConstraint->iColumn < pTab->iHidden ) continue;
117585 j = pConstraint->iColumn - pTab->iHidden;
117590 pIdxInfo->estimatedCost = (double)2147483647;
117591 pIdxInfo->estimatedRows = 2147483647;
117594 j = seen[0]-1;
117595 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
117596 pIdxInfo->aConstraintUsage[j].omit = 1;
117598 pIdxInfo->estimatedCost = (double)20;
117599 pIdxInfo->estimatedRows = 20;
117600 j = seen[1]-1;
117601 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
117602 pIdxInfo->aConstraintUsage[j].omit = 1;
117612 pCsr->base.pVtab = pVtab;
117613 *ppCursor = &pCsr->base;
117620 sqlite3_finalize(pCsr->pPragma);
117621 pCsr->pPragma = 0;
117622 for(i=0; i<ArraySize(pCsr->azArg); i++){
117623 sqlite3_free(pCsr->azArg[i]);
117624 pCsr->azArg[i] = 0;
117642 pCsr->iRowid++;
117643 assert( pCsr->pPragma );
117644 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
117645 rc = sqlite3_finalize(pCsr->pPragma);
117646 pCsr->pPragma = 0;
117661 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
117670 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
117673 assert( j<ArraySize(pCsr->azArg) );
117674 assert( pCsr->azArg[j]==0 );
117676 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
117677 if( pCsr->azArg[j]==0 ){
117682 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
117684 if( pCsr->azArg[1] ){
117685 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
117687 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
117688 if( pCsr->azArg[0] ){
117689 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
117693 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
117696 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
117707 return (pCsr->pPragma==0);
117719 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
117720 if( i<pTab->iHidden ){
117721 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
117723 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
117733 *p = pCsr->iRowid;
117740 0, /* xCreate - create a table */
117741 pragmaVtabConnect, /* xConnect - connect to an existing table */
117742 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
117743 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
117744 0, /* xDestroy - Drop a table */
117745 pragmaVtabOpen, /* xOpen - open a cursor */
117746 pragmaVtabClose, /* xClose - close a cursor */
117747 pragmaVtabFilter, /* xFilter - configure scan constraints */
117748 pragmaVtabNext, /* xNext - advance a cursor */
117750 pragmaVtabColumn, /* xColumn - read data */
117751 pragmaVtabRowid, /* xRowid - read data */
117752 0, /* xUpdate - write data */
117753 0, /* xBegin - begin transaction */
117754 0, /* xSync - sync transaction */
117755 0, /* xCommit - commit transaction */
117756 0, /* xRollback - rollback transaction */
117757 0, /* xFindFunction - function overloading */
117758 0, /* xRename - rename the table */
117774 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
117775 assert( sqlite3HashFind(&db->aModule, zName)==0 );
117811 sqlite3 *db = pData->db;
117812 if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117816 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117817 sqlite3DbFree(db, *pData->pzErrMsg);
117818 *pData->pzErrMsg = z;
117820 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117837 sqlite3 *db = pData->db;
117838 int iDb = pData->iDb;
117842 assert( sqlite3_mutex_held(db->mutex) );
117844 if( db->mallocFailed ){
117849 assert( iDb>=0 && iDb<db->nDb );
117855 ** But because db->init.busy is set to 1, no VDBE code is generated
117860 u8 saved_iDb = db->init.iDb;
117864 assert( db->init.busy );
117865 db->init.iDb = iDb;
117866 db->init.newTnum = sqlite3Atoi(argv[1]);
117867 db->init.orphanTrigger = 0;
117868 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
117869 rc = db->errCode;
117871 db->init.iDb = saved_iDb;
117872 assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 );
117874 if( db->init.orphanTrigger ){
117877 pData->rc = rc;
117896 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
117904 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
117932 assert( iDb>=0 && iDb<db->nDb );
117933 assert( db->aDb[iDb].pSchema );
117934 assert( sqlite3_mutex_held(db->mutex) );
117935 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
117937 db->init.busy = 1;
117939 /* Construct the in-memory representation schema tables (sqlite_master or
117943 ** the schema table as read-only. */
117961 pDb = &db->aDb[iDb];
117962 if( pDb->pBt==0 ){
117969 /* If there is not already a read-only (or read-write) transaction opened
117970 ** on the b-tree database, open one now. If a transaction is opened, it
117972 sqlite3BtreeEnter(pDb->pBt);
117973 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
117974 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
117989 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
118000 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
118002 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
118004 /* If opening a non-empty database, check the text encoding. For the
118009 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
118014 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
118022 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
118032 pDb->pSchema->enc = ENC(db);
118034 if( pDb->pSchema->cache_size==0 ){
118036 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
118038 pDb->pSchema->cache_size = size;
118040 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
118042 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
118048 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
118051 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
118052 if( pDb->pSchema->file_format==0 ){
118053 pDb->pSchema->file_format = 1;
118055 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
118066 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
118067 db->flags &= ~SQLITE_LegacyFileFmt;
118072 assert( db->init.busy );
118077 db->aDb[iDb].zDbSName, zMasterName);
118081 xAuth = db->xAuth;
118082 db->xAuth = 0;
118086 db->xAuth = xAuth;
118097 if( db->mallocFailed ){
118101 if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
118120 sqlite3BtreeCommit(pDb->pBt);
118122 sqlite3BtreeLeave(pDb->pBt);
118131 db->init.busy = 0;
118136 ** Initialize all database files - the main database file, the file
118143 ** file was of zero-length, then the DB_Empty flag is also set.
118147 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
118149 assert( sqlite3_mutex_held(db->mutex) );
118150 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
118151 assert( db->init.busy==0 );
118153 assert( db->nDb>0 );
118160 for(i=db->nDb-1; i>0; i--){
118173 ** This routine is a no-op if the database schema is already initialized.
118178 sqlite3 *db = pParse->db;
118179 assert( sqlite3_mutex_held(db->mutex) );
118180 if( !db->init.busy ){
118181 rc = sqlite3Init(db, &pParse->zErrMsg);
118184 pParse->rc = rc;
118185 pParse->nErr++;
118193 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
118194 ** make no changes to pParse->rc.
118197 sqlite3 *db = pParse->db;
118202 assert( pParse->checkSchema );
118203 assert( sqlite3_mutex_held(db->mutex) );
118204 for(iDb=0; iDb<db->nDb; iDb++){
118206 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
118209 /* If there is not already a read-only (or read-write) transaction opened
118210 ** on the b-tree database, open one now. If a transaction is opened, it
118211 ** will be closed immediately after reading the meta-value. */
118222 ** value stored as part of the in-memory schema representation,
118226 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
118228 pParse->rc = SQLITE_SCHEMA;
118240 ** which database file in db->aDb[] the schema refers to.
118246 int i = -1000000;
118248 /* If pSchema is NULL, then return -1000000. This happens when code in
118250 ** created by a sub-select). In this case the return value of this
118253 ** We return -1000000 instead of the more usual -1 simply because using
118254 ** -1000000 as the incorrect index into db->aDb[] is much
118255 ** more likely to cause a segfault than -1 (of course there are assert()
118258 assert( sqlite3_mutex_held(db->mutex) );
118261 assert( i<db->nDb );
118262 if( db->aDb[i].pSchema==pSchema ){
118266 assert( i>=0 && i<db->nDb );
118275 sqlite3 *db = pParse->db;
118276 sqlite3DbFree(db, pParse->aLabel);
118277 sqlite3ExprListDelete(db, pParse->pConstExpr);
118279 assert( db->lookaside.bDisable >= pParse->disableLookaside );
118280 db->lookaside.bDisable -= pParse->disableLookaside;
118282 pParse->disableLookaside = 0;
118286 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
118290 const char *zSql, /* UTF-8 encoded SQL statement. */
118306 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
118307 assert( sqlite3_mutex_held(db->mutex) );
118309 /* For a long-term use prepared statement avoid the use of
118314 db->lookaside.bDisable++;
118319 ** some other database connection is holding a write-lock, which in
118340 for(i=0; i<db->nDb; i++) {
118341 Btree *pBt = db->aDb[i].pBt;
118346 const char *zDb = db->aDb[i].zDbSName;
118348 testcase( db->flags & SQLITE_ReadUncommit );
118357 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
118359 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
118370 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
118384 if( db->mallocFailed ){
118409 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
118415 if( db->init.busy==0 ){
118416 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
118418 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
118435 sParse.pTriggerPrg = pT->pNext;
118446 const char *zSql, /* UTF-8 encoded SQL statement. */
118463 sqlite3_mutex_enter(db->mutex);
118472 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
118475 assert( (rc&db->errMask)==rc );
118476 sqlite3_mutex_leave(db->mutex);
118495 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
118499 assert( sqlite3_mutex_held(db->mutex) );
118501 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
118529 const char *zSql, /* UTF-8 encoded SQL statement. */
118541 const char *zSql, /* UTF-8 encoded SQL statement. */
118547 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
118559 const char *zSql, /* UTF-8 encoded SQL statement. */
118566 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
118583 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
118587 const void *zSql, /* UTF-16 encoded SQL statement. */
118593 /* This function currently works by first transforming the UTF-16
118594 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
118614 sqlite3_mutex_enter(db->mutex);
118617 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
118622 ** equivalent pointer into the UTF-16 string by counting the unicode
118624 ** the same number of characters into the UTF-16 string.
118626 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
118631 sqlite3_mutex_leave(db->mutex);
118645 const void *zSql, /* UTF-16 encoded SQL statement. */
118657 const void *zSql, /* UTF-16 encoded SQL statement. */
118669 const void *zSql, /* UTF-16 encoded SQL statement. */
118710 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
118711 (S)->zSelName,(S)),\
118740 int regReturn; /* Register holding block-output return address */
118741 int labelBkOut; /* Start label for the block-output subroutine */
118755 Select *pPrior = p->pPrior;
118756 sqlite3ExprListDelete(db, p->pEList);
118757 sqlite3SrcListDelete(db, p->pSrc);
118758 sqlite3ExprDelete(db, p->pWhere);
118759 sqlite3ExprListDelete(db, p->pGroupBy);
118760 sqlite3ExprDelete(db, p->pHaving);
118761 sqlite3ExprListDelete(db, p->pOrderBy);
118762 sqlite3ExprDelete(db, p->pLimit);
118763 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
118774 pDest->eDest = (u8)eDest;
118775 pDest->iSDParm = iParm;
118776 pDest->zAffSdst = 0;
118777 pDest->iSdst = 0;
118778 pDest->nSdst = 0;
118789 SrcList *pSrc, /* the FROM clause -- which tables to scan */
118799 pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
118801 assert( pParse->db->mallocFailed );
118806 sqlite3Expr(pParse->db,TK_ASTERISK,0));
118808 pNew->pEList = pEList;
118809 pNew->op = TK_SELECT;
118810 pNew->selFlags = selFlags;
118811 pNew->iLimit = 0;
118812 pNew->iOffset = 0;
118814 pNew->zSelName[0] = 0;
118816 pNew->addrOpenEphm[0] = -1;
118817 pNew->addrOpenEphm[1] = -1;
118818 pNew->nSelectRow = 0;
118819 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
118820 pNew->pSrc = pSrc;
118821 pNew->pWhere = pWhere;
118822 pNew->pGroupBy = pGroupBy;
118823 pNew->pHaving = pHaving;
118824 pNew->pOrderBy = pOrderBy;
118825 pNew->pPrior = 0;
118826 pNew->pNext = 0;
118827 pNew->pLimit = pLimit;
118828 pNew->pWith = 0;
118829 if( pParse->db->mallocFailed ) {
118830 clearSelect(pParse->db, pNew, pNew!=&standin);
118833 assert( pNew->pSrc!=0 || pParse->nErr>0 );
118845 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
118859 ** Return a pointer to the right-most SELECT statement in a compound.
118862 while( p->pNext ) p = p->pNext;
118909 if( p->n==aKeyword[j].nChar
118910 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
118941 ** Return the index of a column in a table. Return -1 if the column
118946 for(i=0; i<pTab->nCol; i++){
118947 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
118949 return -1;
118963 int N, /* Number of tables in pSrc->a[] to search */
118965 int *piTab, /* Write index of pSrc->a[] here */
118966 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
118973 iCol = columnIndex(pSrc->a[i].pTab, zCol);
119006 sqlite3 *db = pParse->db;
119012 assert( pSrc->nSrc>iRight );
119013 assert( pSrc->a[iLeft].pTab );
119014 assert( pSrc->a[iRight].pTab );
119024 pEq->iRightJoinTable = (i16)pE2->iTable;
119060 p->iRightJoinTable = (i16)iTable;
119061 if( p->op==TK_FUNCTION && p->x.pList ){
119063 for(i=0; i<p->x.pList->nExpr; i++){
119064 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
119067 setJoinExpr(p->pLeft, iTable);
119068 p = p->pRight;
119078 ** The left most table is the first entry in Select.pSrc. The right-most
119092 pSrc = p->pSrc;
119093 pLeft = &pSrc->a[0];
119095 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
119096 Table *pRightTab = pRight->pTab;
119099 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
119100 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
119105 if( pRight->fg.jointype & JT_NATURAL ){
119106 if( pRight->pOn || pRight->pUsing ){
119111 for(j=0; j<pRightTab->nCol; j++){
119116 zName = pRightTab->aCol[j].zName;
119119 isOuter, &p->pWhere);
119126 if( pRight->pOn && pRight->pUsing ){
119135 if( pRight->pOn ){
119136 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
119137 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
119138 pRight->pOn = 0;
119148 if( pRight->pUsing ){
119149 IdList *pList = pRight->pUsing;
119150 for(j=0; j<pList->nId; j++){
119156 zName = pList->a[j].zName;
119161 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
119166 isOuter, &p->pWhere);
119183 ** through regData+nData-1 onto the sorter.
119194 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
119195 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
119196 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
119199 int regRecord = ++pParse->nMem; /* Assembled sorter record */
119200 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
119208 regBase = regData - nExpr - bSeq;
119210 regBase = pParse->nMem + 1;
119211 pParse->nMem += nBase;
119213 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
119214 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
119215 pSort->labelDone = sqlite3VdbeMakeLabel(v);
119216 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
119219 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
119224 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
119233 regPrevKey = pParse->nMem+1;
119234 pParse->nMem += pSort->nOBSat;
119235 nKey = nExpr - pSort->nOBSat + bSeq;
119239 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
119242 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
119243 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
119244 if( pParse->db->mallocFailed ) return;
119245 pOp->p2 = nKey + nData;
119246 pKI = pOp->p4.pKeyInfo;
119247 memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
119248 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
119249 testcase( pKI->nAllField > pKI->nKeyField+2 );
119250 pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
119251 pKI->nAllField-pKI->nKeyField-1);
119254 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
119255 pSort->regReturn = ++pParse->nMem;
119256 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
119257 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
119259 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
119263 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
119266 if( pSort->sortFlags & SORTFLAG_UseSorter ){
119271 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
119272 regBase+nOBSat, nBase-nOBSat);
119281 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
119282 if( pSort->bOrderedInnerLoop ){
119283 r1 = ++pParse->nMem;
119284 sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
119287 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
119288 if( pSort->bOrderedInnerLoop ){
119336 v = pParse->pVdbe;
119349 ** If srcTab is negative, then the p->pEList expressions
119351 ** zero or more, then data is pulled from srcTab and p->pEList is used only
119357 int srcTab, /* Pull data from this table if non-negative */
119364 Vdbe *v = pParse->pVdbe;
119367 int eDest = pDest->eDest; /* How to dispose of results */
119368 int iParm = pDest->iSDParm; /* First argument to disposal method */
119375 ** values for any expressions that are also part of the sort-key are omitted
119381 assert( p->pEList!=0 );
119382 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
119383 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
119386 codeOffset(v, p->iOffset, iContinue);
119391 nResultCol = p->pEList->nExpr;
119393 if( pDest->iSdst==0 ){
119395 nPrefixReg = pSort->pOrderBy->nExpr;
119396 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
119397 pParse->nMem += nPrefixReg;
119399 pDest->iSdst = pParse->nMem+1;
119400 pParse->nMem += nResultCol;
119401 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
119403 ** on the right-hand side of an INSERT contains more result columns than
119407 pParse->nMem += nResultCol;
119409 pDest->nSdst = nResultCol;
119410 regOrig = regResult = pDest->iSdst;
119414 VdbeComment((v, "%s", p->pEList->a[i].zName));
119427 /* For each expression in p->pEList that is a copy of an expression in
119428 ** the ORDER BY clause (pSort->pOrderBy), set the associated
119430 ** expression within the sort-key that pushOntoSorter() will generate.
119431 ** This allows the p->pEList field to be omitted from the sorted record,
119434 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
119436 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
119437 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
119444 nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
119453 switch( pDistinct->eTnctType ){
119460 regPrev = pParse->nMem+1;
119461 pParse->nMem += nResultCol;
119469 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
119470 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
119471 pOp->opcode = OP_Null;
119472 pOp->p1 = 1;
119473 pOp->p2 = regPrev;
119477 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
119478 if( i<nResultCol-1 ){
119485 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
119488 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
119489 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
119494 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
119499 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
119500 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
119506 codeOffset(v, p->iOffset, iContinue);
119588 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
119590 r1, pDest->zAffSdst, nResultCol);
119612 assert( nResultCol<=pDest->nSdst );
119616 assert( nResultCol==pDest->nSdst );
119624 case SRT_Coroutine: /* Send data to a co-routine */
119632 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
119642 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
119643 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
119644 ** pSO->nExpr columns, then make sure all keys are unique by adding a
119653 pSO = pDest->pOrderBy;
119655 nKey = pSO->nExpr;
119674 regResult + pSO->a[i].u.x.iOrderByCol - 1,
119692 ** user-defined functions that have side effects. We do not care
119706 if( pSort==0 && p->iLimit ){
119707 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
119716 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
119719 p->aSortOrder = (u8*)&p->aColl[N+X];
119720 p->nKeyField = (u16)N;
119721 p->nAllField = (u16)(N+X);
119722 p->enc = ENC(db);
119723 p->db = db;
119724 p->nRef = 1;
119737 assert( p->nRef>0 );
119738 p->nRef--;
119739 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
119748 assert( p->nRef>0 );
119749 p->nRef++;
119761 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
119787 sqlite3 *db = pParse->db;
119790 nExpr = pList->nExpr;
119791 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
119794 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
119795 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
119796 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
119819 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
119822 ** "USE TEMP B-TREE FOR xxx"
119828 if( pParse->explain==2 ){
119829 Vdbe *v = pParse->pVdbe;
119830 char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
119831 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
119836 ** Assign expression b to lvalue a. A second, no-op, version of this macro
119845 /* No-op versions of the explainXXX() functions and macros. */
119853 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
119857 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
119873 if( pParse->explain==2 ){
119874 Vdbe *v = pParse->pVdbe;
119876 pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
119877 bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
119879 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
119883 /* No-op versions of the explainXXX() functions and macros. */
119888 ** If the inner loop was generated using a non-null pOrderBy argument,
119900 Vdbe *v = pParse->pVdbe; /* The prepared statement */
119901 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
119906 ExprList *pOrderBy = pSort->pOrderBy;
119907 int eDest = pDest->eDest;
119908 int iParm = pDest->iSDParm;
119917 struct ExprList_item *aOutEx = p->pEList->a;
119920 if( pSort->labelBkOut ){
119921 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
119923 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
119925 iTab = pSort->iECursor;
119928 regRow = pDest->iSdst;
119935 nKey = pOrderBy->nExpr - pSort->nOBSat;
119936 if( pSort->sortFlags & SORTFLAG_UseSorter ){
119937 int regSortOut = ++pParse->nMem;
119938 iSortTab = pParse->nTab++;
119939 if( pSort->labelBkOut ){
119946 codeOffset(v, p->iOffset, addrContinue);
119951 codeOffset(v, p->iOffset, addrContinue);
119958 iRead = aOutEx[i].u.x.iOrderByCol-1;
119975 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
119977 pDest->zAffSdst, nColumn);
119992 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
119993 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
119995 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
120011 if( pSort->sortFlags & SORTFLAG_UseSorter ){
120016 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
120031 ** result-set expression in all of the following SELECT statements is
120042 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
120069 assert( pNC->pSrcList!=0 );
120070 assert( pExpr->op!=TK_AGG_COLUMN ); /* This routine runes before aggregates
120072 switch( pExpr->op ){
120080 int iCol = pExpr->iColumn; /* Index of column in pTab */
120082 SrcList *pTabList = pNC->pSrcList;
120083 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
120084 if( j<pTabList->nSrc ){
120085 pTab = pTabList->a[j].pTab;
120086 pS = pTabList->a[j].pSelect;
120088 pNC = pNC->pNext;
120103 ** sub-select. In this case, set the column type to NULL, even
120113 assert( pTab && pExpr->pTab==pTab );
120115 /* The "table" is actually a sub-select or a view in the FROM clause
120117 ** data for the result-set column of the sub-select.
120119 if( iCol>=0 && iCol<pS->pEList->nExpr ){
120121 ** rowid of the sub-select or view. This expression is legal (see
120122 ** test case misc2.2.2) - it always evaluates to NULL.
120125 Expr *p = pS->pEList->a[iCol].pExpr;
120126 sNC.pSrcList = pS->pSrc;
120128 sNC.pParse = pNC->pParse;
120135 if( iCol<0 ) iCol = pTab->iPKey;
120136 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
120141 zOrigCol = pTab->aCol[iCol].zName;
120142 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
120144 zOrigTab = pTab->zName;
120145 if( pNC->pParse && pTab->pSchema ){
120146 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
120147 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
120150 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
120154 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
120162 /* The expression is a sub-select. Return the declaration type and
120167 Select *pS = pExpr->x.pSelect;
120168 Expr *p = pS->pEList->a[0].pExpr;
120170 sNC.pSrcList = pS->pSrc;
120172 sNC.pParse = pNC->pParse;
120200 Vdbe *v = pParse->pVdbe;
120206 for(i=0; i<pEList->nExpr; i++){
120207 Expr *p = pEList->a[i].pExpr;
120215 /* The vdbe must make its own copy of the column-type and other
120265 Vdbe *v = pParse->pVdbe;
120270 sqlite3 *db = pParse->db;
120276 if( pParse->explain ){
120281 if( pParse->colNamesSet || db->mallocFailed ) return;
120282 /* Column names are determined by the left-most term of a compound select */
120283 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
120285 pTabList = pSelect->pSrc;
120286 pEList = pSelect->pEList;
120289 pParse->colNamesSet = 1;
120290 fullName = (db->flags & SQLITE_FullColNames)!=0;
120291 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
120292 sqlite3VdbeSetNumCols(v, pEList->nExpr);
120293 for(i=0; i<pEList->nExpr; i++){
120294 Expr *p = pEList->a[i].pExpr;
120297 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
120298 assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
120299 if( pEList->a[i].zName ){
120301 char *zName = pEList->a[i].zName;
120303 }else if( srcName && p->op==TK_COLUMN ){
120305 int iCol = p->iColumn;
120306 pTab = p->pTab;
120308 if( iCol<0 ) iCol = pTab->iPKey;
120309 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
120313 zCol = pTab->aCol[iCol].zName;
120317 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
120323 const char *z = pEList->a[i].zSpan;
120359 sqlite3 *db = pParse->db; /* Database connection */
120370 nCol = pEList->nExpr;
120382 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
120385 if( (zName = pEList->a[i].zName)!=0 ){
120388 Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
120389 while( pColExpr->op==TK_DOT ){
120390 pColExpr = pColExpr->pRight;
120393 assert( pColExpr->op!=TK_AGG_COLUMN );
120394 if( pColExpr->op==TK_COLUMN ){
120396 int iCol = pColExpr->iColumn;
120397 Table *pTab = pColExpr->pTab;
120399 if( iCol<0 ) iCol = pTab->iPKey;
120400 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
120401 }else if( pColExpr->op==TK_ID ){
120403 zName = pColExpr->u.zToken;
120406 zName = pEList->a[i].zSpan;
120422 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
120428 pCol->zName = zName;
120435 if( db->mallocFailed ){
120463 sqlite3 *db = pParse->db;
120472 assert( (pSelect->selFlags & SF_Resolved)!=0 );
120473 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
120474 if( db->mallocFailed ) return;
120476 sNC.pSrcList = pSelect->pSrc;
120477 a = pSelect->pEList->a;
120478 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
120483 /* pCol->szEst = ... // Column size est for SELECT tables never used */
120484 pCol->affinity = sqlite3ExprAffinity(p);
120487 n = sqlite3Strlen30(pCol->zName);
120488 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
120489 if( pCol->zName ){
120490 memcpy(&pCol->zName[n+1], zType, m+1);
120491 pCol->colFlags |= COLFLAG_HASTYPE;
120494 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
120496 if( pColl && pCol->zColl==0 ){
120497 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
120500 pTab->szTabRow = 1; /* Any non-zero value works */
120509 sqlite3 *db = pParse->db;
120512 savedFlags = db->flags;
120513 db->flags &= ~SQLITE_FullColNames;
120514 db->flags |= SQLITE_ShortColNames;
120516 if( pParse->nErr ) return 0;
120517 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
120518 db->flags = savedFlags;
120525 assert( db->lookaside.bDisable );
120526 pTab->nTabRef = 1;
120527 pTab->zName = 0;
120528 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
120529 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
120531 pTab->iPKey = -1;
120532 if( db->mallocFailed ){
120544 if( pParse->pVdbe ){
120545 return pParse->pVdbe;
120547 if( pParse->pToplevel==0
120548 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
120550 pParse->okConstFactor = 1;
120558 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
120566 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
120574 ** Only if pLimit->pLeft!=0 do the limit registers get
120584 Expr *pLimit = p->pLimit;
120586 if( p->iLimit ) return;
120589 ** "LIMIT -1" always shows all rows. There is some
120596 assert( pLimit->op==TK_LIMIT );
120597 assert( pLimit->pLeft!=0 );
120598 p->iLimit = iLimit = ++pParse->nMem;
120601 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
120606 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
120607 p->nSelectRow = sqlite3LogEst((u64)n);
120608 p->selFlags |= SF_FixedLimit;
120611 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
120616 if( pLimit->pRight ){
120617 p->iOffset = iOffset = ++pParse->nMem;
120618 pParse->nMem++; /* Allocate an extra register for limit+offset */
120619 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
120630 ** Return the appropriate collating sequence for the iCol-th column of
120631 ** the result set for the compound-select statement "p". Return NULL if
120635 ** left-most term of the select that has a collating sequence.
120639 if( p->pPrior ){
120640 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
120645 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
120648 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
120649 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
120664 ExprList *pOrderBy = p->pOrderBy;
120665 int nOrderBy = p->pOrderBy->nExpr;
120666 sqlite3 *db = pParse->db;
120671 struct ExprList_item *pItem = &pOrderBy->a[i];
120672 Expr *pTerm = pItem->pExpr;
120675 if( pTerm->flags & EP_Collate ){
120678 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
120679 if( pColl==0 ) pColl = db->pDfltColl;
120680 pOrderBy->a[i].pExpr =
120681 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
120684 pRet->aColl[i] = pColl;
120685 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
120697 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
120699 ** p->pPrior p
120702 ** There is exactly one reference to the recursive-table in the FROM clause
120703 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
120705 ** The setup-query runs once to generate an initial set of rows that go
120709 ** recursive-table for a recursive-query run. The output of the recursive-query
120734 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
120735 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
120736 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
120737 Select *pSetup = p->pPrior; /* The setup query */
120757 p->nSelectRow = 320; /* 4 billion rows */
120759 pLimit = p->pLimit;
120760 regLimit = p->iLimit;
120761 regOffset = p->iOffset;
120762 p->pLimit = 0;
120763 p->iLimit = p->iOffset = 0;
120764 pOrderBy = p->pOrderBy;
120767 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
120768 if( pSrc->a[i].fg.isRecursive ){
120769 iCurrent = pSrc->a[i].iCursor;
120777 iQueue = pParse->nTab++;
120778 if( p->op==TK_UNION ){
120780 iDistinct = pParse->nTab++;
120787 regCurrent = ++pParse->nMem;
120791 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
120799 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
120800 p->selFlags |= SF_UsesEphemeral;
120804 p->pOrderBy = 0;
120806 /* Store the results of the setup-query in Queue. */
120807 pSetup->pNext = 0;
120809 pSetup->pNext = p;
120818 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
120836 ** the value for the recursive-table. Store the results in the Queue.
120838 if( p->selFlags & SF_Aggregate ){
120841 p->pPrior = 0;
120843 assert( p->pPrior==0 );
120844 p->pPrior = pSetup;
120852 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
120853 p->pOrderBy = pOrderBy;
120854 p->pLimit = pLimit;
120862 Select *p, /* The right-most of SELECTs to be coded */
120867 ** Handle the special case of a compound-select that originates from a
120880 ** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
120884 Select *p, /* The right-most of SELECTs to be coded */
120891 assert( p->selFlags & SF_MultiValue );
120893 assert( p->selFlags & SF_Values );
120894 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
120895 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
120896 if( p->pPrior==0 ) break;
120897 assert( p->pPrior->pNext==p );
120898 p = p->pPrior;
120902 pPrior = p->pPrior;
120903 p->pPrior = 0;
120905 p->pPrior = pPrior;
120906 if( rc || pRightmost->pLimit ) break;
120907 p->nSelectRow = nRow;
120908 p = p->pNext;
120918 ** "p" points to the right-most of the two queries. the query on the
120919 ** left is p->pPrior. The left query could also be a compound query
120925 ** Example 1: Consider a three-way compound SQL statement.
120933 ** `-----> SELECT b FROM t2
120935 ** `------> SELECT a FROM t1
120939 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
120946 Select *p, /* The right-most of SELECTs to be coded */
120956 int iSub1 = 0; /* EQP id of left-hand query */
120957 int iSub2 = 0; /* EQP id of right-hand query */
120961 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
120963 assert( p && p->pPrior ); /* Calling function guarantees this much */
120964 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
120965 db = pParse->db;
120966 pPrior = p->pPrior;
120968 if( pPrior->pOrderBy || pPrior->pLimit ){
120970 pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
120981 assert( p->pEList );
120982 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
120986 /* Special handling for a compound-select that originates as a VALUES clause.
120988 if( p->selFlags & SF_MultiValue ){
120996 assert( p->pEList && pPrior->pEList );
120997 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
121000 if( p->selFlags & SF_Recursive ){
121007 if( p->pOrderBy ){
121013 switch( p->op ){
121017 assert( !pPrior->pLimit );
121018 pPrior->iLimit = p->iLimit;
121019 pPrior->iOffset = p->iOffset;
121020 pPrior->pLimit = p->pLimit;
121021 explainSetInteger(iSub1, pParse->iNextSelectId);
121023 p->pLimit = 0;
121027 p->pPrior = 0;
121028 p->iLimit = pPrior->iLimit;
121029 p->iOffset = pPrior->iOffset;
121030 if( p->iLimit ){
121031 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
121033 if( p->iOffset ){
121035 p->iLimit, p->iOffset+1, p->iOffset);
121038 explainSetInteger(iSub2, pParse->iNextSelectId);
121041 pDelete = p->pPrior;
121042 p->pPrior = pPrior;
121043 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
121044 if( pPrior->pLimit
121045 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
121046 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
121048 p->nSelectRow = sqlite3LogEst((u64)nLimit);
121060 Expr *pLimit; /* Saved values of p->nLimit */
121064 testcase( p->op==TK_EXCEPT );
121065 testcase( p->op==TK_UNION );
121071 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
121077 unionTab = pParse->nTab++;
121078 assert( p->pOrderBy==0 );
121080 assert( p->addrOpenEphm[0] == -1 );
121081 p->addrOpenEphm[0] = addr;
121082 findRightmost(p)->selFlags |= SF_UsesEphemeral;
121083 assert( p->pEList );
121088 assert( !pPrior->pOrderBy );
121090 explainSetInteger(iSub1, pParse->iNextSelectId);
121098 if( p->op==TK_EXCEPT ){
121101 assert( p->op==TK_UNION );
121104 p->pPrior = 0;
121105 pLimit = p->pLimit;
121106 p->pLimit = 0;
121108 explainSetInteger(iSub2, pParse->iNextSelectId);
121111 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
121112 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
121113 sqlite3ExprListDelete(db, p->pOrderBy);
121114 pDelete = p->pPrior;
121115 p->pPrior = pPrior;
121116 p->pOrderBy = 0;
121117 if( p->op==TK_UNION ){
121118 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
121120 sqlite3ExprDelete(db, p->pLimit);
121121 p->pLimit = pLimit;
121122 p->iLimit = 0;
121123 p->iOffset = 0;
121131 assert( p->pEList );
121146 default: assert( p->op==TK_INTERSECT ); {
121158 tab1 = pParse->nTab++;
121159 tab2 = pParse->nTab++;
121160 assert( p->pOrderBy==0 );
121163 assert( p->addrOpenEphm[0] == -1 );
121164 p->addrOpenEphm[0] = addr;
121165 findRightmost(p)->selFlags |= SF_UsesEphemeral;
121166 assert( p->pEList );
121171 explainSetInteger(iSub1, pParse->iNextSelectId);
121180 assert( p->addrOpenEphm[1] == -1 );
121181 p->addrOpenEphm[1] = addr;
121182 p->pPrior = 0;
121183 pLimit = p->pLimit;
121184 p->pLimit = 0;
121186 explainSetInteger(iSub2, pParse->iNextSelectId);
121189 pDelete = p->pPrior;
121190 p->pPrior = pPrior;
121191 if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
121192 sqlite3ExprDelete(db, p->pLimit);
121193 p->pLimit = pLimit;
121198 assert( p->pEList );
121218 explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
121224 ** This section is run by the right-most SELECT statement only.
121225 ** SELECT statements to the left always skip this part. The right-most
121229 if( p->selFlags & SF_UsesEphemeral ){
121233 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
121236 assert( p->pNext==0 );
121237 nCol = p->pEList->nExpr;
121243 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
121246 *apColl = db->pDfltColl;
121250 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
121252 int addr = pLoop->addrOpenEphm[i];
121256 assert( pLoop->addrOpenEphm[1]<0 );
121262 pLoop->addrOpenEphm[i] = -1;
121269 pDest->iSdst = dest.iSdst;
121270 pDest->nSdst = dest.nSdst;
121281 if( p->selFlags & SF_Values ){
121285 " do not have the same number of result columns", selectOpName(p->op));
121293 ** The data to be output is contained in pIn->iSdst. There are
121294 ** pIn->nSdst columns to be output. pDest is where the output should
121306 ** If the LIMIT found in p->iLimit is reached, jump immediately to
121319 Vdbe *v = pParse->pVdbe;
121331 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
121335 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
121338 if( pParse->db->mallocFailed ) return 0;
121342 codeOffset(v, p->iOffset, iContinue);
121344 assert( pDest->eDest!=SRT_Exists );
121345 assert( pDest->eDest!=SRT_Table );
121346 switch( pDest->eDest ){
121352 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
121353 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
121354 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
121366 testcase( pIn->nSdst>1 );
121368 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
121369 r1, pDest->zAffSdst, pIn->nSdst);
121370 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
121371 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
121372 pIn->iSdst, pIn->nSdst);
121382 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
121383 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
121390 ** starting at pDest->iSdst. Then the co-routine yields.
121393 if( pDest->iSdst==0 ){
121394 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
121395 pDest->nSdst = pIn->nSdst;
121397 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
121398 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
121411 assert( pDest->eDest==SRT_Output );
121412 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
121413 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
121420 if( p->iLimit ){
121421 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
121442 ** co-routines. Then run the co-routines in parallel and merge the results
121469 ** ------------- ----------------- -------------- -----------------
121520 Select *p, /* The right-most of SELECTs to be coded */
121528 int regAddrA; /* Address register for select-A coroutine */
121529 int regAddrB; /* Address register for select-B coroutine */
121530 int addrSelectA; /* Address of the select-A coroutine */
121531 int addrSelectB; /* Address of the select-B coroutine */
121532 int regOutA; /* Address register for the output-A subroutine */
121533 int regOutB; /* Address register for the output-B subroutine */
121534 int addrOutA; /* Address of the output-A subroutine */
121535 int addrOutB = 0; /* Address of the output-B subroutine */
121536 int addrEofA; /* Address of the select-A-exhausted subroutine */
121538 int addrEofB; /* Address of the select-B-exhausted subroutine */
121542 int regLimitA; /* Limit register for select-A */
121543 int regLimitB; /* Limit register for select-A */
121545 int savedLimit; /* Saved value of p->iLimit */
121546 int savedOffset; /* Saved value of p->iOffset */
121558 int iSub1; /* EQP id of left-hand query */
121559 int iSub2; /* EQP id of right-hand query */
121562 assert( p->pOrderBy!=0 );
121564 db = pParse->db;
121565 v = pParse->pVdbe;
121573 op = p->op;
121574 pPrior = p->pPrior;
121575 assert( pPrior->pOrderBy==0 );
121576 pOrderBy = p->pOrderBy;
121578 nOrderBy = pOrderBy->nExpr;
121585 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
121587 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
121588 assert( pItem->u.x.iOrderByCol>0 );
121589 if( pItem->u.x.iOrderByCol==i ) break;
121594 pNew->flags |= EP_IntValue;
121595 pNew->u.iValue = i;
121596 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
121597 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
121613 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
121614 assert( pItem->u.x.iOrderByCol>0 );
121615 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
121616 aPermute[i] = pItem->u.x.iOrderByCol - 1;
121625 p->pOrderBy = pOrderBy;
121626 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
121635 int nExpr = p->pEList->nExpr;
121636 assert( nOrderBy>=nExpr || db->mallocFailed );
121637 regPrev = pParse->nMem+1;
121638 pParse->nMem += nExpr+1;
121644 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
121645 pKeyDup->aSortOrder[i] = 0;
121652 p->pPrior = 0;
121653 pPrior->pNext = 0;
121654 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
121655 if( pPrior->pPrior==0 ){
121656 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
121661 if( p->iLimit && op==TK_ALL ){
121662 regLimitA = ++pParse->nMem;
121663 regLimitB = ++pParse->nMem;
121664 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
121670 sqlite3ExprDelete(db, p->pLimit);
121671 p->pLimit = 0;
121673 regAddrA = ++pParse->nMem;
121674 regAddrB = ++pParse->nMem;
121675 regOutA = ++pParse->nMem;
121676 regOutB = ++pParse->nMem;
121681 ** left of the compound operator - the "A" select.
121686 pPrior->iLimit = regLimitA;
121687 explainSetInteger(iSub1, pParse->iNextSelectId);
121693 ** the right - the "B" select
121698 savedLimit = p->iLimit;
121699 savedOffset = p->iOffset;
121700 p->iLimit = regLimitB;
121701 p->iOffset = 0;
121702 explainSetInteger(iSub2, pParse->iNextSelectId);
121704 p->iLimit = savedLimit;
121705 p->iOffset = savedOffset;
121733 VdbeNoopComment((v, "eof-A subroutine"));
121738 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
121746 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
121748 VdbeNoopComment((v, "eof-B subroutine"));
121756 VdbeNoopComment((v, "A-lt-B subroutine"));
121769 VdbeNoopComment((v, "A-eq-B subroutine"));
121777 VdbeNoopComment((v, "A-gt-B subroutine"));
121806 if( p->pPrior ){
121807 sqlite3SelectDelete(db, p->pPrior);
121809 p->pPrior = pPrior;
121810 pPrior->pNext = p;
121814 explainComposite(pParse, p->op, iSub1, iSub2, 0);
121815 return pParse->nErr!=0;
121841 ** a column in table number iTable with a copy of the iColumn-th
121858 && pExpr->iRightJoinTable==pSubst->iTable
121860 pExpr->iRightJoinTable = pSubst->iNewTable;
121862 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
121863 if( pExpr->iColumn<0 ){
121864 pExpr->op = TK_NULL;
121867 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
121869 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
121870 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
121872 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
121874 sqlite3 *db = pSubst->pParse->db;
121875 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
121879 ifNullRow.iTable = pSubst->iNewTable;
121883 if( pNew && pSubst->isLeftJoin ){
121887 pNew->iRightJoinTable = pExpr->iRightJoinTable;
121895 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
121896 pExpr->iTable = pSubst->iNewTable;
121898 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
121899 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
121901 substSelect(pSubst, pExpr->x.pSelect, 1);
121903 substExprList(pSubst, pExpr->x.pList);
121914 for(i=0; i<pList->nExpr; i++){
121915 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
121921 int doPrior /* Do substitutes on p->pPrior too */
121928 substExprList(pSubst, p->pEList);
121929 substExprList(pSubst, p->pGroupBy);
121930 substExprList(pSubst, p->pOrderBy);
121931 p->pHaving = substExpr(pSubst, p->pHaving);
121932 p->pWhere = substExpr(pSubst, p->pWhere);
121933 pSrc = p->pSrc;
121935 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
121936 substSelect(pSubst, pItem->pSelect, 1);
121937 if( pItem->fg.isTabFunc ){
121938 substExprList(pSubst, pItem->u1.pFuncArg);
121941 }while( doPrior && (p = p->pPrior)!=0 );
121981 ** other than the one FROM-clause subquery that is a candidate
121983 ** from 2015-02-09.)
121994 ** sub-queries that were excluded from this optimization. Restriction
122009 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
122010 ** accidently carried the comment forward until 2014-09-15. Original
122041 ** The parent and sub-query may contain WHERE clauses. Subject to
122048 ** Also, each component of the sub-query must return the same number
122051 ** such (illegal) sub-query is flattened. The caller will detect the
122054 ** (18) If the sub-query is a compound select, then all terms of the
122056 ** columns of the sub-query.
122061 ** (20) If the sub-query is a compound select, then it must not use
122073 ** a recursive CTE, then the sub-query may not be a compound query.
122079 ** The subquery may not be an aggregate that uses the built-in min() or
122086 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
122089 ** If flattening is not attempted, this routine is a no-op and returns 0.
122098 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
122101 const char *zSavedAuthContext = pParse->zAuthContext;
122104 Select *pSub1; /* Pointer to the rightmost select in sub-query */
122108 int iNewParent = -1;/* Replacement table for iParent */
122113 sqlite3 *db = pParse->db;
122118 assert( p->pPrior==0 );
122120 pSrc = p->pSrc;
122121 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
122122 pSubitem = &pSrc->a[iFrom];
122123 iParent = pSubitem->iCursor;
122124 pSub = pSubitem->pSelect;
122127 pSubSrc = pSub->pSrc;
122131 ** because they could be computed at compile-time. But when LIMIT and OFFSET
122134 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
122135 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
122136 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
122139 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
122140 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
122141 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
122144 if( p->pOrderBy && pSub->pOrderBy ){
122147 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
122148 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
122149 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
122152 if( pSub->selFlags & (SF_Recursive) ){
122171 ** aggregates are processed - there is no mechanism to determine if
122172 ** the LEFT JOIN table should be all-NULL.
122176 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
122178 if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
122185 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
122187 ** though they are not necessary. This will stress-test the OP_IfNullRow
122189 isLeftJoin = -1;
122193 /* Restriction (17): If the sub-query is a compound SELECT, then it must
122198 if( pSub->pPrior ){
122199 if( pSub->pOrderBy ){
122202 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
122205 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
122206 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
122207 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
122208 assert( pSub->pSrc!=0 );
122209 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
122210 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
122211 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
122212 || pSub1->pSrc->nSrc<1 /* (17c) */
122216 testcase( pSub1->pSrc->nSrc>1 );
122220 if( p->pOrderBy ){
122222 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
122223 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
122228 /* Ex-restriction (23):
122234 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
122238 pSub->zSelName, pSub, iFrom));
122241 pParse->zAuthContext = pSubitem->zName;
122244 pParse->zAuthContext = zSavedAuthContext;
122246 /* If the sub-query is a compound SELECT statement, then (by restrictions
122250 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
122253 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
122254 ** OFFSET clauses and joins them to the left-hand-side of the original
122256 ** select statements in the compound sub-query.
122277 ** We call this the "compound-subquery flattening".
122279 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
122281 ExprList *pOrderBy = p->pOrderBy;
122282 Expr *pLimit = p->pLimit;
122283 Select *pPrior = p->pPrior;
122284 p->pOrderBy = 0;
122285 p->pSrc = 0;
122286 p->pPrior = 0;
122287 p->pLimit = 0;
122289 sqlite3SelectSetName(pNew, pSub->zSelName);
122290 p->pLimit = pLimit;
122291 p->pOrderBy = pOrderBy;
122292 p->pSrc = pSrc;
122293 p->op = TK_ALL;
122295 p->pPrior = pPrior;
122297 pNew->pPrior = pPrior;
122298 if( pPrior ) pPrior->pNext = pNew;
122299 pNew->pNext = p;
122300 p->pPrior = pNew;
122302 ("compound-subquery flattener creates %s.%p as peer\n",
122303 pNew->zSelName, pNew));
122305 if( db->mallocFailed ) return 1;
122308 /* Begin flattening the iFrom-th entry of the FROM clause
122311 pSub = pSub1 = pSubitem->pSelect;
122316 sqlite3DbFree(db, pSubitem->zDatabase);
122317 sqlite3DbFree(db, pSubitem->zName);
122318 sqlite3DbFree(db, pSubitem->zAlias);
122319 pSubitem->zDatabase = 0;
122320 pSubitem->zName = 0;
122321 pSubitem->zAlias = 0;
122322 pSubitem->pSelect = 0;
122329 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
122331 if( ALWAYS(pSubitem->pTab!=0) ){
122332 Table *pTabToDel = pSubitem->pTab;
122333 if( pTabToDel->nTabRef==1 ){
122335 pTabToDel->pNextZombie = pToplevel->pZombieTab;
122336 pToplevel->pZombieTab = pTabToDel;
122338 pTabToDel->nTabRef--;
122340 pSubitem->pTab = 0;
122343 /* The following loop runs once for each term in a compound-subquery
122345 ** of flattening - a flattening other than a compound-subquery flattening -
122356 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
122359 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
122360 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
122361 pSrc = pParent->pSrc; /* FROM clause of the outer query */
122365 jointype = pSubitem->fg.jointype;
122368 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
122370 assert( db->mallocFailed );
122391 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
122392 if( db->mallocFailed ){
122401 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
122402 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
122403 pSrc->a[i+iFrom] = pSubSrc->a[i];
122404 iNewParent = pSubSrc->a[i].iCursor;
122405 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
122407 pSrc->a[iFrom].fg.jointype = jointype;
122421 if( pSub->pOrderBy ){
122422 /* At this point, any non-zero iOrderByCol values indicate that the
122429 ** function attempts to flatten a compound sub-query into pParent
122430 ** (the only way this can happen is if the compound sub-query is
122431 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
122432 ExprList *pOrderBy = pSub->pOrderBy;
122433 for(i=0; i<pOrderBy->nExpr; i++){
122434 pOrderBy->a[i].u.x.iOrderByCol = 0;
122436 assert( pParent->pOrderBy==0 );
122437 assert( pSub->pPrior==0 );
122438 pParent->pOrderBy = pOrderBy;
122439 pSub->pOrderBy = 0;
122441 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
122445 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
122446 if( db->mallocFailed==0 ){
122452 x.pEList = pSub->pEList;
122459 pParent->selFlags |= pSub->selFlags & SF_Distinct;
122467 if( pSub->pLimit ){
122468 pParent->pLimit = pSub->pLimit;
122469 pSub->pLimit = 0;
122496 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
122500 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
122508 ** (1) (** This restriction was removed on 2017-09-29. We used to
122528 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
122540 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
122549 for(pX=pSubq; pX; pX=pX->pPrior){
122550 assert( (pX->selFlags & (SF_Recursive))==0 );
122555 if( pSubq->pLimit!=0 ){
122558 while( pWhere->op==TK_AND ){
122559 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
122560 pWhere = pWhere->pLeft;
122567 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
122572 x.pEList = pSubq->pEList;
122574 if( pSubq->selFlags & SF_Aggregate ){
122575 pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
122577 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
122579 pSubq = pSubq->pPrior;
122604 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
122610 assert( pFunc->op==TK_AGG_FUNCTION );
122611 if( pEList==0 || pEList->nExpr!=1 ) return eRet;
122612 zFunc = pFunc->u.zToken;
122623 assert( pOrderBy!=0 || db->mallocFailed );
122624 if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
122630 ** The second argument is the associated aggregate-info object. This
122635 ** where table is a database table, not a sub-select or view. If the query
122643 assert( !p->pGroupBy );
122645 if( p->pWhere || p->pEList->nExpr!=1
122646 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
122650 pTab = p->pSrc->a[0].pTab;
122651 pExpr = p->pEList->a[0].pExpr;
122652 assert( pTab && !pTab->pSelect && pExpr );
122655 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
122656 if( NEVER(pAggInfo->nFunc==0) ) return 0;
122657 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
122658 if( pExpr->flags&EP_Distinct ) return 0;
122664 ** If the source-list item passed as an argument was augmented with an
122668 ** pFrom->pIndex and return SQLITE_OK.
122671 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
122672 Table *pTab = pFrom->pTab;
122673 char *zIndexedBy = pFrom->u1.zIndexedBy;
122675 for(pIdx=pTab->pIndex;
122676 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
122677 pIdx=pIdx->pNext
122681 pParse->checkSchema = 1;
122684 pFrom->pIBIndex = pIdx;
122719 if( p->pPrior==0 ) return WRC_Continue;
122720 if( p->pOrderBy==0 ) return WRC_Continue;
122721 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
122723 a = p->pOrderBy->a;
122724 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
122725 if( a[i].pExpr->flags & EP_Collate ) break;
122731 pParse = pWalker->pParse;
122732 db = pParse->db;
122739 p->pSrc = pNewSrc;
122740 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
122741 p->op = TK_SELECT;
122742 p->pWhere = 0;
122743 pNew->pGroupBy = 0;
122744 pNew->pHaving = 0;
122745 pNew->pOrderBy = 0;
122746 p->pPrior = 0;
122747 p->pNext = 0;
122748 p->pWith = 0;
122749 p->selFlags &= ~SF_Compound;
122750 assert( (p->selFlags & SF_Converted)==0 );
122751 p->selFlags |= SF_Converted;
122752 assert( pNew->pPrior!=0 );
122753 pNew->pPrior->pNext = pNew;
122754 pNew->pLimit = 0;
122759 ** Check to see if the FROM clause term pFrom has table-valued function
122761 ** non-zero, since pFrom is not allowed to be a table-valued function.
122764 if( pFrom->fg.isTabFunc ){
122765 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
122775 ** FROM clause element pItem is really a common-table-expression (CTE)
122779 ** If a non-NULL value is returned, set *ppContext to point to the With
122788 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
122790 for(p=pWith; p; p=p->pOuter){
122792 for(i=0; i<p->nCte; i++){
122793 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
122795 return &p->a[i];
122804 ** with the inner-most WITH clause being at the top of the stack.
122814 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
122816 assert( pParse->pWith!=pWith );
122817 pWith->pOuter = pParse->pWith;
122818 pParse->pWith = pWith;
122819 if( bFree ) pParse->pWithToFree = pWith;
122829 ** If pFrom falls into either of the two categories above, pFrom->pTab
122831 ** (pFrom->pTab!=0) to determine whether or not a successful match
122842 Parse *pParse = pWalker->pParse;
122843 sqlite3 *db = pParse->db;
122847 assert( pFrom->pTab==0 );
122849 pCte = searchWith(pParse->pWith, pFrom, &pWith);
122854 Select *pLeft; /* Left-most SELECT statement */
122856 With *pSavedWith; /* Initial value of pParse->pWith */
122858 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
122860 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
122862 if( pCte->zCteErr ){
122863 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
122868 assert( pFrom->pTab==0 );
122869 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
122871 pTab->nTabRef = 1;
122872 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
122873 pTab->iPKey = -1;
122874 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
122875 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
122876 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
122877 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
122878 assert( pFrom->pSelect );
122881 pSel = pFrom->pSelect;
122882 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
122885 SrcList *pSrc = pFrom->pSelect->pSrc;
122886 for(i=0; i<pSrc->nSrc; i++){
122887 struct SrcList_item *pItem = &pSrc->a[i];
122888 if( pItem->zDatabase==0
122889 && pItem->zName!=0
122890 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
122892 pItem->pTab = pTab;
122893 pItem->fg.isRecursive = 1;
122894 pTab->nTabRef++;
122895 pSel->selFlags |= SF_Recursive;
122901 if( pTab->nTabRef>2 ){
122903 pParse, "multiple references to recursive table: %s", pCte->zName
122907 assert( pTab->nTabRef==1 ||
122908 ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
122910 pCte->zCteErr = "circular reference: %s";
122911 pSavedWith = pParse->pWith;
122912 pParse->pWith = pWith;
122914 Select *pPrior = pSel->pPrior;
122915 assert( pPrior->pWith==0 );
122916 pPrior->pWith = pSel->pWith;
122918 pPrior->pWith = 0;
122922 pParse->pWith = pWith;
122924 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
122925 pEList = pLeft->pEList;
122926 if( pCte->pCols ){
122927 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
122929 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
122931 pParse->pWith = pSavedWith;
122934 pEList = pCte->pCols;
122937 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
122939 if( pSel->selFlags & SF_Recursive ){
122940 pCte->zCteErr = "multiple recursive references: %s";
122942 pCte->zCteErr = "recursive reference in a subquery: %s";
122946 pCte->zCteErr = 0;
122947 pParse->pWith = pSavedWith;
122964 Parse *pParse = pWalker->pParse;
122965 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
122966 With *pWith = findRightmost(p)->pWith;
122968 assert( pParse->pWith==pWith );
122969 pParse->pWith = pWith->pOuter;
122984 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
122986 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
123002 Parse *pParse = pWalker->pParse;
123007 sqlite3 *db = pParse->db;
123009 u16 selFlags = p->selFlags;
123012 p->selFlags |= SF_Expanded;
123013 if( db->mallocFailed ){
123016 assert( p->pSrc!=0 );
123020 pTabList = p->pSrc;
123021 pEList = p->pEList;
123022 if( OK_IF_ALWAYS_TRUE(p->pWith) ){
123023 sqlite3WithPush(pParse, p->pWith, 0);
123035 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
123037 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
123038 if( pFrom->fg.isRecursive ) continue;
123039 assert( pFrom->pTab==0 );
123042 if( pFrom->pTab ) {} else
123044 if( pFrom->zName==0 ){
123046 Select *pSel = pFrom->pSelect;
123047 /* A sub-query in the FROM clause of a SELECT */
123049 assert( pFrom->pTab==0 );
123051 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
123053 pTab->nTabRef = 1;
123054 if( pFrom->zAlias ){
123055 pTab->zName = sqlite3DbStrDup(db, pFrom->zAlias);
123057 pTab->zName = sqlite3MPrintf(db, "subquery_%p", (void*)pTab);
123059 while( pSel->pPrior ){ pSel = pSel->pPrior; }
123060 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
123061 pTab->iPKey = -1;
123062 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
123063 pTab->tabFlags |= TF_Ephemeral;
123067 assert( pFrom->pTab==0 );
123068 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
123070 if( pTab->nTabRef>=0xffff ){
123072 pTab->zName);
123073 pFrom->pTab = 0;
123076 pTab->nTabRef++;
123081 if( IsVirtual(pTab) || pTab->pSelect ){
123084 assert( pFrom->pSelect==0 );
123085 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
123086 sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
123087 nCol = pTab->nCol;
123088 pTab->nCol = -1;
123089 sqlite3WalkSelect(pWalker, pFrom->pSelect);
123090 pTab->nCol = nCol;
123103 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
123118 for(k=0; k<pEList->nExpr; k++){
123119 pE = pEList->a[k].pExpr;
123120 if( pE->op==TK_ASTERISK ) break;
123121 assert( pE->op!=TK_DOT || pE->pRight!=0 );
123122 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
123123 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
123124 elistFlags |= pE->flags;
123126 if( k<pEList->nExpr ){
123132 struct ExprList_item *a = pEList->a;
123134 int flags = pParse->db->flags;
123138 for(k=0; k<pEList->nExpr; k++){
123140 elistFlags |= pE->flags;
123141 pRight = pE->pRight;
123142 assert( pE->op!=TK_DOT || pRight!=0 );
123143 if( pE->op!=TK_ASTERISK
123144 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
123150 pNew->a[pNew->nExpr-1].zName = a[k].zName;
123151 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
123161 if( pE->op==TK_DOT ){
123162 assert( pE->pLeft!=0 );
123163 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
123164 zTName = pE->pLeft->u.zToken;
123166 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
123167 Table *pTab = pFrom->pTab;
123168 Select *pSub = pFrom->pSelect;
123169 char *zTabName = pFrom->zAlias;
123173 zTabName = pTab->zName;
123175 if( db->mallocFailed ) break;
123176 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
123181 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123182 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
123184 for(j=0; j<pTab->nCol; j++){
123185 char *zName = pTab->aCol[j].zName;
123192 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
123198 ** result-set list unless the SELECT has the SF_IncludeHidden
123201 if( (p->selFlags & SF_IncludeHidden)==0
123202 && IsHiddenColumn(&pTab->aCol[j])
123209 if( (pFrom->fg.jointype & JT_NATURAL)!=0
123216 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
123225 if( longNames || pTabList->nSrc>1 ){
123243 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
123244 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
123246 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
123247 testcase( pX->zSpan==0 );
123249 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
123251 testcase( pX->zSpan==0 );
123253 pX->bSpanIsTab = 1;
123268 p->pEList = pNew;
123270 if( p->pEList ){
123271 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
123276 p->selFlags |= SF_ComplexResult;
123283 ** No-op routine for the parse-tree walker.
123297 ** No-op routine for the parse-tree walker for SELECT statements.
123325 ** The calling function can detect the problem by looking at pParse->nErr
123326 ** and/or pParse->db->mallocFailed.
123332 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
123348 ** For each FROM-clause subquery, add Column.zType and Column.zColl
123363 assert( p->selFlags & SF_Resolved );
123364 assert( (p->selFlags & SF_HasTypeInfo)==0 );
123365 p->selFlags |= SF_HasTypeInfo;
123366 pParse = pWalker->pParse;
123367 pTabList = p->pSrc;
123368 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
123369 Table *pTab = pFrom->pTab;
123371 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
123372 /* A sub-query in the FROM clause of a SELECT */
123373 Select *pSel = pFrom->pSelect;
123375 while( pSel->pPrior ) pSel = pSel->pPrior;
123386 ** the Table structures of all FROM-clause subqueries in a
123407 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
123408 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
123420 assert( p!=0 || pParse->db->mallocFailed );
123421 if( pParse->db->mallocFailed ) return;
123422 if( p->selFlags & SF_HasTypeInfo ) return;
123424 if( pParse->nErr || pParse->db->mallocFailed ) return;
123426 if( pParse->nErr || pParse->db->mallocFailed ) return;
123439 Vdbe *v = pParse->pVdbe;
123442 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
123447 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
123448 for(i=0; i<pAggInfo->nColumn; i++){
123449 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
123450 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
123452 for(i=0; i<pAggInfo->nFunc; i++){
123453 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
123454 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
123457 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
123458 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
123459 if( pFunc->iDistinct>=0 ){
123460 Expr *pE = pFunc->pExpr;
123462 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
123465 pFunc->iDistinct = -1;
123467 KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
123468 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
123480 Vdbe *v = pParse->pVdbe;
123483 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
123484 ExprList *pList = pF->pExpr->x.pList;
123485 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
123486 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
123487 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
123496 Vdbe *v = pParse->pVdbe;
123503 pAggInfo->directMode = 1;
123504 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
123508 ExprList *pList = pF->pExpr->x.pList;
123509 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
123511 nArg = pList->nExpr;
123518 if( pF->iDistinct>=0 ){
123522 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
123524 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
123528 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
123529 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
123530 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
123533 pColl = pParse->db->pDfltColl;
123535 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
123538 sqlite3VdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem);
123539 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
123552 ** to pC->iMem. But by the time the value is used, the original register
123563 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
123564 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
123566 pAggInfo->directMode = 0;
123583 if( pParse->explain==2 ){
123585 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
123586 pTab->zName,
123588 bCover ? pIdx->zName : ""
123591 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
123614 ** sub-expression matches the criteria for being moved to the WHERE
123615 ** clause. If so, add it to the WHERE clause and replace the sub-expression
123619 if( pExpr->op!=TK_AND ){
123620 struct HavingToWhereCtx *p = pWalker->u.pHavingCtx;
123621 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, p->pGroupBy) ){
123622 sqlite3 *db = pWalker->pParse->db;
123625 Expr *pWhere = *(p->ppWhere);
123628 *(p->ppWhere) = pNew;
123671 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
123676 SrcList *pTabList, /* Search for self-joins in this FROM clause */
123680 for(pItem = pTabList->a; pItem<pThis; pItem++){
123681 if( pItem->pSelect==0 ) continue;
123682 if( pItem->fg.viaCoroutine ) continue;
123683 if( pItem->zName==0 ) continue;
123684 if( sqlite3_stricmp(pItem->zDatabase, pThis->zDatabase)!=0 ) continue;
123685 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
123687 pThis->pSelect->pWhere, pItem->pSelect->pWhere, -1)
123721 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
123722 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
123723 pExpr = p->pEList->a[0].pExpr;
123724 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
123725 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
123726 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
123727 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
123728 pSub = p->pSrc->a[0].pSelect;
123730 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
123732 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
123733 if( pSub->pWhere ) return 0; /* No WHERE clause */
123734 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
123735 pSub = pSub->pPrior; /* Repeat over compound */
123740 db = pParse->db;
123743 pSub = p->pSrc->a[0].pSelect;
123744 p->pSrc->a[0].pSelect = 0;
123745 sqlite3SrcListDelete(db, p->pSrc);
123746 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
123749 pPrior = pSub->pPrior;
123750 pSub->pPrior = 0;
123751 pSub->pNext = 0;
123752 pSub->selFlags |= SF_Aggregate;
123753 pSub->selFlags &= ~SF_Compound;
123754 pSub->nSelectRow = 0;
123755 sqlite3ExprListDelete(db, pSub->pEList);
123757 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
123767 p->pEList->a[0].pExpr = pExpr;
123768 p->selFlags &= ~SF_Aggregate;
123772 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
123788 ** pParse->zErrMsg.
123817 int iRestoreSelectId = pParse->iSelectId;
123818 pParse->iSelectId = pParse->iNextSelectId++;
123821 db = pParse->db;
123822 if( p==0 || db->mallocFailed || pParse->nErr ){
123828 pParse->nSelectIndent++;
123835 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
123836 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
123837 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
123838 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
123840 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
123841 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
123842 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
123843 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
123846 sqlite3ExprListDelete(db, p->pOrderBy);
123847 p->pOrderBy = 0;
123848 p->selFlags &= ~SF_Distinct;
123852 sSort.pOrderBy = p->pOrderBy;
123853 pTabList = p->pSrc;
123854 if( pParse->nErr || db->mallocFailed ){
123857 assert( p->pEList!=0 );
123858 isAgg = (p->selFlags & SF_Aggregate)!=0;
123870 if( pDest->eDest==SRT_Output ){
123877 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
123878 struct SrcList_item *pItem = &pTabList->a[i];
123879 Select *pSub = pItem->pSelect;
123880 Table *pTab = pItem->pTab;
123885 if( pTab->nCol!=pSub->pEList->nExpr ){
123887 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
123895 ** will be implemented as a co-routine and there is no advantage to
123898 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
123899 assert( pSub->pGroupBy==0 );
123904 ** it will be implemented as a co-routine, then do not flatten. This
123919 if( pSub->pOrderBy!=0
123921 && (p->selFlags & SF_ComplexResult)!=0
123922 && (pTabList->nSrc==1
123923 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
123930 i = -1;
123932 pTabList = p->pSrc;
123933 if( db->mallocFailed ) goto select_end;
123935 sSort.pOrderBy = p->pOrderBy;
123944 if( p->pPrior ){
123946 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
123948 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
123949 pParse->nSelectIndent--;
123957 ** (2) Generate code for all sub-queries
123959 for(i=0; i<pTabList->nSrc; i++){
123960 struct SrcList_item *pItem = &pTabList->a[i];
123972 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
123973 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
123980 ** assume the column name is non-NULL and segfault. The use of an empty
123983 if( pItem->colUsed==0 ){
123984 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
123988 /* Generate code for all sub-queries in the FROM clause
123990 pSub = pItem->pSelect;
123996 ** a view or the co-routine to implement a view. The first instance
123999 if( pItem->addrFillSub ){
124000 if( pItem->fg.viaCoroutine==0 ){
124001 /* The subroutine that manifests the view might be a one-time routine,
124004 testcase( sqlite3VdbeGetOp(v, pItem->addrFillSub)->opcode==OP_Once );
124005 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
124013 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
124017 pParse->nHeight += sqlite3SelectExprHeight(p);
124019 /* Make copies of constant WHERE-clause terms in the outer query down
124022 if( (pItem->fg.jointype & JT_OUTER)==0
124023 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
124027 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
124033 zSavedAuthContext = pParse->zAuthContext;
124034 pParse->zAuthContext = pItem->zName;
124038 ** The subquery is implemented as a co-routine if the subquery is
124042 ** TODO: Are there other reasons beside (1) to use a co-routine
124046 && (pTabList->nSrc==1
124047 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
124049 /* Implement a co-routine that will return a single row of the result
124054 pItem->regReturn = ++pParse->nMem;
124055 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
124056 VdbeComment((v, "%s", pItem->pTab->zName));
124057 pItem->addrFillSub = addrTop;
124058 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
124059 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
124061 pItem->pTab->nRowLogEst = pSub->nSelectRow;
124062 pItem->fg.viaCoroutine = 1;
124063 pItem->regResult = dest.iSdst;
124064 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
124065 sqlite3VdbeJumpHere(v, addrTop-1);
124069 ** the content of this subquery. pItem->addrFillSub will point
124070 ** to the address of the generated subroutine. pItem->regReturn
124078 assert( pItem->addrFillSub==0 );
124079 pItem->regReturn = ++pParse->nMem;
124080 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
124081 pItem->addrFillSub = topAddr+1;
124082 if( pItem->fg.isCorrelated==0 ){
124087 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
124089 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
124093 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
124094 explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
124095 assert( pPrior->pSelect!=0 );
124096 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
124098 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
124099 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
124102 pItem->pTab->nRowLogEst = pSub->nSelectRow;
124104 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
124105 VdbeComment((v, "end %s", pItem->pTab->zName));
124109 if( db->mallocFailed ) goto select_end;
124110 pParse->nHeight -= sqlite3SelectExprHeight(p);
124111 pParse->zAuthContext = zSavedAuthContext;
124117 pEList = p->pEList;
124118 pWhere = p->pWhere;
124119 pGroupBy = p->pGroupBy;
124120 pHaving = p->pHaving;
124121 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
124125 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
124134 if( db->mallocFailed ) goto select_end;
124135 pEList = p->pEList;
124136 pTabList = p->pSrc;
124141 ** if the select-list is the same as the ORDER BY list, then this query
124150 ** The second form is preferred as a single index (or temp-table) may be
124152 ** written the query must use a temp-table for at least one of the ORDER
124153 ** BY and DISTINCT, and an index or separate temp-table for the other.
124155 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
124156 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
124158 p->selFlags &= ~SF_Distinct;
124159 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
124160 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
124175 ** being unused if the data can be extracted in pre-sorted order.
124183 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
124184 sSort.iECursor = pParse->nTab++;
124187 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
124191 sSort.addrSortIndex = -1;
124196 if( pDest->eDest==SRT_EphemTab ){
124197 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
124203 if( (p->selFlags & SF_FixedLimit)==0 ){
124204 p->nSelectRow = 320; /* 4 billion rows */
124207 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
124214 if( p->selFlags & SF_Distinct ){
124215 sDistinct.tabTnct = pParse->nTab++;
124218 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
124230 wctrlFlags |= p->selFlags & SF_FixedLimit;
124234 p->pEList, wctrlFlags, p->nSelectRow);
124236 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
124237 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
124245 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
124259 assert( p->pEList==pEList );
124260 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
124290 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
124291 pItem->u.x.iAlias = 0;
124293 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
124294 pItem->u.x.iAlias = 0;
124297 if( p->nSelectRow>66 ) p->nSelectRow = 66;
124300 p->nSelectRow = 0;
124306 ** in the correct order. It also may not - the GROUP BY might use a
124311 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
124326 sAggInfo.mnReg = pParse->nMem+1;
124327 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
124333 assert( pWhere==p->pWhere );
124334 havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
124335 pWhere = p->pWhere;
124340 if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
124348 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
124351 sAggInfo.mxReg = pParse->nMem;
124352 if( db->mallocFailed ) goto select_end;
124359 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
124364 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
124377 int addr1; /* A-vs-B comparision jump */
124391 sAggInfo.sortingIdx = pParse->nTab++;
124399 iUseFlag = ++pParse->nMem;
124400 iAbortFlag = ++pParse->nMem;
124401 regOutputRow = ++pParse->nMem;
124403 regReset = ++pParse->nMem;
124405 iAMem = pParse->nMem + 1;
124406 pParse->nMem += pGroupBy->nExpr;
124407 iBMem = pParse->nMem + 1;
124408 pParse->nMem += pGroupBy->nExpr;
124413 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
124425 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
124443 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
124447 nGroupBy = pGroupBy->nExpr;
124462 if( pCol->iSorterColumn>=j ){
124465 pCol->pTab, pCol->iColumn, pCol->iTable, r1);
124475 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
124489 ** This is an optimization - the correct answer should result regardless.
124510 for(j=0; j<pGroupBy->nExpr; j++){
124515 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
124518 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
124532 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
124569 ** is less than or equal to zero, the subroutine is a no-op. If
124586 selectInnerLoop(pParse, p, -1, &sSort,
124592 /* Generate a subroutine that will reset the group-by accumulator
124616 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124617 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
124621 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
124624 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
124628 ** (2011-04-15) Do not do a full scan of an unordered index.
124630 ** (2013-10-03) Do not count the entries in a partial index.
124636 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124637 if( pIdx->bUnordered==0
124638 && pIdx->szIdxRow<pTab->szTabRow
124639 && pIdx->pPartIdxWhere==0
124640 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
124646 iRoot = pBest->tnum;
124650 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
124653 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
124665 assert( p->pGroupBy==0 );
124674 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
124693 selectInnerLoop(pParse, p, -1, 0, 0,
124710 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
124719 rc = (pParse->nErr>0);
124725 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
124731 pParse->nSelectIndent--;
124781 int need; /* Slots needed in p->azResult[] */
124785 /* Make sure there is enough space in p->azResult to hold everything
124788 if( p->nRow==0 && argv!=0 ){
124793 if( p->nData + need > p->nAlloc ){
124795 p->nAlloc = p->nAlloc*2 + need;
124796 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
124798 p->azResult = azNew;
124804 if( p->nRow==0 ){
124805 p->nColumn = nCol;
124809 p->azResult[p->nData++] = z;
124811 }else if( (int)p->nColumn!=nCol ){
124812 sqlite3_free(p->zErrMsg);
124813 p->zErrMsg = sqlite3_mprintf(
124816 p->rc = SQLITE_ERROR;
124832 p->azResult[p->nData++] = z;
124834 p->nRow++;
124839 p->rc = SQLITE_NOMEM_BKPT;
124879 db->errCode = SQLITE_NOMEM;
124895 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
124908 db->errCode = SQLITE_NOMEM;
124927 azResult--;
124960 pTriggerStep = pTriggerStep->pNext;
124962 sqlite3ExprDelete(db, pTmp->pWhere);
124963 sqlite3ExprListDelete(db, pTmp->pExprList);
124964 sqlite3SelectDelete(db, pTmp->pSelect);
124965 sqlite3IdListDelete(db, pTmp->pIdList);
124966 sqlite3DbFree(db, pTmp->zSpan);
124977 ** are already attached to pTab->pTrigger. But there might be additional
124979 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
124984 ** pTab as well as the triggers lised in pTab->pTrigger.
124987 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
124990 if( pParse->disableTriggers ){
124994 if( pTmpSchema!=pTab->pSchema ){
124996 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
124997 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
124999 if( pTrig->pTabSchema==pTab->pSchema
125000 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
125002 pTrig->pNext = (pList ? pList : pTab->pTrigger);
125008 return (pList ? pList : pTab->pTrigger);
125015 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
125034 sqlite3 *db = pParse->db; /* The database connection */
125039 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
125045 if( pName2->n>0 ){
125058 if( !pTableName || db->mallocFailed ){
125062 /* A long-standing parser bug is that this syntax was allowed:
125070 if( db->init.busy && iDb!=1 ){
125071 sqlite3DbFree(db, pTableName->a[0].zDatabase);
125072 pTableName->a[0].zDatabase = 0;
125081 if( db->init.busy==0 && pName2->n==0 && pTab
125082 && pTab->pSchema==db->aDb[1].pSchema ){
125087 if( db->mallocFailed ) goto trigger_cleanup;
125088 assert( pTableName->nSrc==1 );
125096 if( db->init.iDb==1 ){
125099 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
125103 ** "orphaned trigger" - a trigger whose associated table is missing.
125105 db->init.orphanTrigger = 1;
125121 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
125125 assert( !db->init.busy );
125132 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
125140 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
125145 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
125153 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125155 const char *zDb = db->aDb[iTabDb].zDbSName;
125156 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
125158 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
125179 pTrigger->zName = zName;
125181 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
125182 pTrigger->pSchema = db->aDb[iDb].pSchema;
125183 pTrigger->pTabSchema = pTab->pSchema;
125184 pTrigger->op = (u8)op;
125185 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
125186 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
125187 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
125188 assert( pParse->pNewTrigger==0 );
125189 pParse->pNewTrigger = pTrigger;
125196 if( !pParse->pNewTrigger ){
125199 assert( pParse->pNewTrigger==pTrigger );
125212 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
125214 sqlite3 *db = pParse->db; /* The database */
125219 pParse->pNewTrigger = 0;
125220 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
125221 zName = pTrig->zName;
125222 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
125223 pTrig->step_list = pStepList;
125225 pStepList->pTrig = pTrig;
125226 pStepList = pStepList->pNext;
125228 sqlite3TokenInit(&nameToken, pTrig->zName);
125230 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
125231 || sqlite3FixExpr(&sFix, pTrig->pWhen)
125239 if( !db->init.busy ){
125247 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
125251 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
125252 pTrig->table, z);
125259 if( db->init.busy ){
125261 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
125266 }else if( pLink->pSchema==pLink->pTabSchema ){
125268 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
125270 pLink->pNext = pTab->pTrigger;
125271 pTab->pTrigger = pLink;
125277 assert( !pParse->pNewTrigger );
125310 pTriggerStep->op = TK_SELECT;
125311 pTriggerStep->pSelect = pSelect;
125312 pTriggerStep->orconf = OE_Default;
125313 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
125321 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
125332 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
125335 memcpy(z, pName->z, pName->n);
125337 pTriggerStep->zTarget = z;
125338 pTriggerStep->op = op;
125339 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
125362 assert(pSelect != 0 || db->mallocFailed);
125366 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
125367 pTriggerStep->pIdList = pColumn;
125368 pTriggerStep->orconf = orconf;
125395 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
125396 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
125397 pTriggerStep->orconf = orconf;
125420 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
125421 pTriggerStep->orconf = OE_Default;
125432 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
125433 sqlite3DbFree(db, pTrigger->zName);
125434 sqlite3DbFree(db, pTrigger->table);
125435 sqlite3ExprDelete(db, pTrigger->pWhen);
125436 sqlite3IdListDelete(db, pTrigger->pColumns);
125453 sqlite3 *db = pParse->db;
125455 if( db->mallocFailed ) goto drop_trigger_cleanup;
125460 assert( pName->nSrc==1 );
125461 zDb = pName->a[0].zDatabase;
125462 zName = pName->a[0].zName;
125464 for(i=OMIT_TEMPDB; i<db->nDb; i++){
125466 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
125468 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
125477 pParse->checkSchema = 1;
125491 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
125501 sqlite3 *db = pParse->db;
125504 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
125505 assert( iDb>=0 && iDb<db->nDb );
125508 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
125512 const char *zDb = db->aDb[iDb].zDbSName;
125515 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
125528 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
125531 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
125543 pHash = &(db->aDb[iDb].pSchema->trigHash);
125546 if( pTrigger->pSchema==pTrigger->pTabSchema ){
125549 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
125550 *pp = (*pp)->pNext;
125553 db->mDbFlags |= DBFLAG_SchemaChange;
125569 for(e=0; e<pEList->nExpr; e++){
125570 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
125592 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
125596 for(p=pList; p; p=p->pNext){
125597 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
125598 mask |= p->tr_tm;
125608 ** Convert the pStep->zTarget string into a SrcList and return a pointer
125621 sqlite3 *db = pParse->db;
125627 assert( pSrc->nSrc>0 );
125628 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
125629 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
125632 assert( iDb<db->nDb );
125633 zDb = db->aDb[iDb].zDbSName;
125634 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
125650 Vdbe *v = pParse->pVdbe;
125651 sqlite3 *db = pParse->db;
125653 assert( pParse->pTriggerTab && pParse->pToplevel );
125656 for(pStep=pStepList; pStep; pStep=pStep->pNext){
125667 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
125668 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
125670 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
125671 assert( pParse->okConstFactor==0 );
125674 if( pStep->zSpan ){
125676 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
125681 switch( pStep->op ){
125685 sqlite3ExprListDup(db, pStep->pExprList, 0),
125686 sqlite3ExprDup(db, pStep->pWhere, 0),
125687 pParse->eOrconf, 0, 0
125694 sqlite3SelectDup(db, pStep->pSelect, 0),
125695 sqlite3IdListDup(db, pStep->pIdList),
125696 pParse->eOrconf
125703 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
125707 default: assert( pStep->op==TK_SELECT ); {
125709 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
125716 if( pStep->op!=TK_SELECT ){
125743 ** Parse context structure pFrom has just been used to create a sub-vdbe
125748 assert( pFrom->zErrMsg==0 || pFrom->nErr );
125749 assert( pTo->zErrMsg==0 || pTo->nErr );
125750 if( pTo->nErr==0 ){
125751 pTo->zErrMsg = pFrom->zErrMsg;
125752 pTo->nErr = pFrom->nErr;
125753 pTo->rc = pFrom->rc;
125755 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
125760 ** Create and populate a new TriggerPrg object with a sub-program
125770 sqlite3 *db = pParse->db; /* Database handle */
125774 NameContext sNC; /* Name context for sub-vdbe */
125775 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
125776 Parse *pSubParse; /* Parse context for sub-vdbe */
125779 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
125780 assert( pTop->pVdbe );
125784 ** list of the top-level Parse object sooner rather than later. */
125787 pPrg->pNext = pTop->pTriggerPrg;
125788 pTop->pTriggerPrg = pPrg;
125789 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
125791 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
125792 pPrg->pTrigger = pTrigger;
125793 pPrg->orconf = orconf;
125794 pPrg->aColmask[0] = 0xffffffff;
125795 pPrg->aColmask[1] = 0xffffffff;
125798 ** trigger sub-program. */
125803 pSubParse->db = db;
125804 pSubParse->pTriggerTab = pTab;
125805 pSubParse->pToplevel = pTop;
125806 pSubParse->zAuthContext = pTrigger->zName;
125807 pSubParse->eTriggerOp = pTrigger->op;
125808 pSubParse->nQueryLoop = pParse->nQueryLoop;
125813 pTrigger->zName, onErrorText(orconf),
125814 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
125815 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
125816 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
125817 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
125818 pTab->zName
125821 if( pTrigger->zName ){
125822 sqlite3VdbeChangeP4(v, -1,
125823 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
125829 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
125831 if( pTrigger->pWhen ){
125832 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
125834 && db->mallocFailed==0
125842 /* Code the trigger program into the sub-vdbe. */
125843 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
125845 /* Insert an OP_Halt at the end of the sub-program. */
125850 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
125853 if( db->mallocFailed==0 && pParse->nErr==0 ){
125854 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
125856 pProgram->nMem = pSubParse->nMem;
125857 pProgram->nCsr = pSubParse->nTab;
125858 pProgram->token = (void *)pTrigger;
125859 pPrg->aColmask[0] = pSubParse->oldmask;
125860 pPrg->aColmask[1] = pSubParse->newmask;
125864 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
125865 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
125873 ** Return a pointer to a TriggerPrg object containing the sub-program for
125887 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
125893 for(pPrg=pRoot->pTriggerPrg;
125894 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
125895 pPrg=pPrg->pNext
125923 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
125926 ** is a pointer to the sub-vdbe containing the trigger program. */
125928 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
125930 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
125931 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
125933 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
125935 /* Set the P5 operand of the OP_Program instruction to non-zero if
125937 ** invocation is disallowed if (a) the sub-program is really a trigger,
125952 ** operation on pTab, this function is a no-op.
125957 ** (a copy of pTab->nCol), then registers are populated as follows:
125960 ** ------------------------------------------------------
125962 ** reg+1 OLD.* value of left-most column of pTab
125964 ** reg+N OLD.* value of right-most column of pTab
125966 ** reg+N+2 OLD.* value of left-most column of pTab
125968 ** reg+N+N+1 NEW.* value of right-most column of pTab
126001 for(p=pTrigger; p; p=p->pNext){
126006 assert( p->pSchema!=0 );
126007 assert( p->pTabSchema!=0 );
126008 assert( p->pSchema==p->pTabSchema
126009 || p->pSchema==pParse->db->aDb[1].pSchema );
126012 if( p->op==op
126013 && p->tr_tm==tr_tm
126014 && checkColumnOverlap(p->pColumns, pChanges)
126022 ** Triggers may access values stored in the old.* or new.* pseudo-table.
126023 ** This function returns a 32-bit bitmask indicating which columns of the
126028 ** Bit 0 of the returned mask is set if the left-most column of the
126060 for(p=pTrigger; p; p=p->pNext){
126061 if( p->op==op && (tr_tm&p->tr_tm)
126062 && checkColumnOverlap(p->pColumns,pChanges)
126067 mask |= pPrg->aColmask[isNew];
126111 ** i-th column of table pTab. This routine sets the P4 parameter of the
126117 ** command. If the latter, then the row-records in the table btree on disk
126120 ** If the former, then all row-records are guaranteed to include a value
126136 ** stored in place of an 8-byte floating point value in order to save
126141 if( !pTab->pSelect ){
126144 Column *pCol = &pTab->aCol[i];
126145 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
126146 assert( i<pTab->nCol );
126147 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
126148 pCol->affinity, &pValue);
126154 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
126189 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
126190 ** an expression for the i-th column of the table.
126191 ** aXRef[i]==-1 if the i-th column is not changed. */
126198 NameContext sNC; /* The name-context to resolve expressions in */
126230 db = pParse->db;
126231 if( pParse->nErr || db->mallocFailed ){
126234 assert( pTabList->nSrc==1 );
126240 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126247 isView = pTab->pSelect!=0;
126281 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
126284 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
126286 iDataCur = pParse->nTab;
126287 pTabList->a[0].iCursor = iDataCur;
126289 pParse->nTab++;
126295 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
126297 aRegIdx = aXRef+pTab->nCol;
126301 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
126303 /* Initialize the name-context */
126315 for(i=0; i<pChanges->nExpr; i++){
126316 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
126319 for(j=0; j<pTab->nCol; j++){
126320 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
126321 if( j==pTab->iPKey ){
126323 pRowidExpr = pChanges->a[i].pExpr;
126324 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
126331 if( j>=pTab->nCol ){
126332 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
126333 j = -1;
126335 pRowidExpr = pChanges->a[i].pExpr;
126337 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
126338 pParse->checkSchema = 1;
126345 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
126346 j<0 ? "ROWID" : pTab->aCol[j].zName,
126347 db->aDb[iDb].zDbSName);
126351 aXRef[j] = -1;
126366 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
126376 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
126378 if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
126379 reg = ++pParse->nMem;
126380 pParse->nMem += pIdx->nColumn;
126383 for(i=0; i<pIdx->nKeyCol; i++){
126384 i16 iIdxCol = pIdx->aiColumn[i];
126386 reg = ++pParse->nMem;
126387 pParse->nMem += pIdx->nColumn;
126389 || (onError==OE_Default && pIdx->onError==OE_Replace)
126409 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
126414 regRowSet = ++pParse->nMem;
126415 regOldRowid = regNewRowid = ++pParse->nMem;
126417 regOld = pParse->nMem + 1;
126418 pParse->nMem += pTab->nCol;
126421 regNewRowid = ++pParse->nMem;
126423 regNew = pParse->nMem + 1;
126424 pParse->nMem += pTab->nCol;
126429 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
126462 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
126463 regRowCount = ++pParse->nMem;
126471 nPk = pPk->nKeyCol;
126472 iPk = pParse->nMem+1;
126473 pParse->nMem += nPk;
126474 regKey = ++pParse->nMem;
126475 iEph = pParse->nTab++;
126484 ** Do not consider a single-pass strategy for a multi-row update if
126488 ** or index, causing a single-pass approach to malfunction. */
126490 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
126496 /* A one-pass strategy that might update more than one row may not
126509 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
126517 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
126530 assert( pPk->aiColumn[i]>=0 );
126531 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
126554 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
126555 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
126558 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
126605 /* Compute the old pre-UPDATE content of the row being changed, if that
126612 for(i=0; i<pTab->nCol; i++){
126615 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
126644 for(i=0; i<pTab->nCol; i++){
126645 if( i==pTab->iPKey ){
126650 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
126674 /* The row-trigger may have deleted the row being updated. In this
126676 ** required. This behavior - what happens when the row being updated
126677 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
126688 /* If it did not delete it, the row-trigger may still have modified
126693 for(i=0; i<pTab->nCol; i++){
126694 if( aXRef[i]<0 && i!=pTab->iPKey ){
126723 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
126727 ** to invoke the pre-update hook.
126730 ** pre-update hook. If the caller invokes preupdate_new(), the returned
126744 if( !pParse->nested ){
126777 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
126788 /* Nothing to do at end-of-loop for a single-pass */
126804 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
126813 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
126845 ** There are two possible strategies - the default and the special
126873 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
126876 sqlite3 *db = pParse->db; /* Database connection */
126879 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
126883 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
126892 ephemTab = pParse->nTab++;
126894 regArg = pParse->nMem + 1;
126895 pParse->nMem += nArg;
126896 regRec = ++pParse->nMem;
126897 regRowid = ++pParse->nMem;
126904 for(i=0; i<pTab->nCol; i++){
126906 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
126924 assert( pPk->nKeyCol==1 );
126925 iPk = pPk->aiColumn[0];
126933 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
126934 ** above. Also, if this is a top-level parse (not a trigger), clear the
126935 ** multi-write flag so that the VM does not open a statement journal */
126938 pParse->isMultiWrite = 0;
126946 ** accept no-change records with serial_type 10 */
127022 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127096 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
127097 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
127100 iDb = sqlite3FindDb(pParse->db, pNm);
127118 u16 saved_mDbFlags; /* Saved value of db->mDbFlags */
127119 u32 saved_flags; /* Saved value of db->flags */
127120 int saved_nChange; /* Saved value of db->nChange */
127121 int saved_nTotalChange; /* Saved value of db->nTotalChange */
127129 if( !db->autoCommit ){
127133 if( db->nVdbeActive>1 ){
127134 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
127139 ** restored before returning. Then set the writable-schema flag, and
127141 saved_flags = db->flags;
127142 saved_mDbFlags = db->mDbFlags;
127143 saved_nChange = db->nChange;
127144 saved_nTotalChange = db->nTotalChange;
127145 saved_mTrace = db->mTrace;
127146 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
127147 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
127148 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
127149 db->mTrace = 0;
127151 zDbMain = db->aDb[iDb].zDbSName;
127152 pMain = db->aDb[iDb].pBt;
127161 ** An optimisation would be to use a non-journaled pager.
127169 nDb = db->nDb;
127172 assert( (db->nDb-1)==nDb );
127173 pDb = &db->aDb[nDb];
127174 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
127175 pTemp = pDb->pBt;
127187 if( db->nextPagesize ){
127192 if( nKey ) db->nextPagesize = 0;
127196 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
127202 ** to ensure that we do not try to change the page-size on a WAL database.
127212 db->nextPagesize = 0;
127216 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
127217 || NEVER(db->mallocFailed)
127224 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
127231 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
127245 db->init.iDb = 0;
127258 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
127259 db->mDbFlags &= ~DBFLAG_Vacuum;
127278 ** both transactions are closed by this block - the main database
127325 /* Restore the original value of db->flags */
127326 db->init.iDb = 0;
127327 db->mDbFlags = saved_mDbFlags;
127328 db->flags = saved_flags;
127329 db->nChange = saved_nChange;
127330 db->nTotalChange = saved_nTotalChange;
127331 db->mTrace = saved_mTrace;
127332 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
127341 db->autoCommit = 1;
127344 sqlite3BtreeClose(pDb->pBt);
127345 pDb->pBt = 0;
127346 pDb->pSchema = 0;
127349 /* This both clears the schemas and reduces the size of the db->aDb[]
127411 pMod->zName = zCopy;
127412 pMod->pModule = pModule;
127413 pMod->pAux = pAux;
127414 pMod->xDestroy = xDestroy;
127415 pMod->pEpoTab = 0;
127416 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
127441 sqlite3_mutex_enter(db->mutex);
127442 if( sqlite3HashFind(&db->aModule, zName) ){
127449 sqlite3_mutex_leave(db->mutex);
127455 ** External API function used to create a new virtual-table module.
127470 ** External API function used to create a new virtual-table module.
127494 pVTab->nRef++;
127499 ** pTab is a pointer to a Table structure representing a virtual-table.
127501 ** this virtual-table, if one has been created, or NULL otherwise.
127506 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
127511 ** Decrement the ref-count on a virtual table object. When the ref-count
127515 sqlite3 *db = pVTab->db;
127518 assert( pVTab->nRef>0 );
127519 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
127521 pVTab->nRef--;
127522 if( pVTab->nRef==0 ){
127523 sqlite3_vtab *p = pVTab->pVtab;
127525 p->pModule->xDisconnect(p);
127533 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
127536 ** connection db is left in the p->pVTable list.
127540 VTable *pVTable = p->pVTable;
127541 p->pVTable = 0;
127547 ** database connection that may have an entry in the p->pVTable list.
127549 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
127552 sqlite3 *db2 = pVTable->db;
127553 VTable *pNext = pVTable->pNext;
127557 p->pVTable = pRet;
127558 pRet->pNext = 0;
127560 pVTable->pNext = db2->pDisconnect;
127561 db2->pDisconnect = pVTable;
127573 ** list in p->pVTab. It also decrements the VTable ref count. This is
127576 ** be being used by other shared-cache connections).
127583 assert( sqlite3_mutex_held(db->mutex) );
127585 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
127586 if( (*ppVTab)->db==db ){
127588 *ppVTab = pVTab->pNext;
127600 ** shared b-tree databases opened using connection db are held by the
127610 ** or, if the virtual table is stored in a non-sharable database, then
127614 ** by multiple threads. It is thread-safe.
127617 VTable *p = db->pDisconnect;
127618 db->pDisconnect = 0;
127621 assert( sqlite3_mutex_held(db->mutex) );
127626 VTable *pNext = p->pNext;
127634 ** Clear any and all virtual-table information from the Table record.
127638 ** Since it is a virtual-table, the Table structure contains a pointer
127648 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
127649 if( p->azModuleArg ){
127651 for(i=0; i<p->nModuleArg; i++){
127652 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
127654 sqlite3DbFree(db, p->azModuleArg);
127659 ** Add a new module argument to pTable->azModuleArg[].
127660 ** The string is not copied - the pointer is stored. The
127665 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
127667 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
127671 int i = pTable->nModuleArg++;
127674 pTable->azModuleArg = azModuleArg;
127695 pTable = pParse->pNewTable;
127697 assert( 0==pTable->pIndex );
127699 db = pParse->db;
127700 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
127703 assert( pTable->nModuleArg==0 );
127706 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
127707 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
127708 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
127710 pParse->sNameToken.n = (int)(
127711 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
127720 if( pTable->azModuleArg ){
127721 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
127722 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
127729 ** in pParse->zArg[] and appends it to the list of arguments on the
127730 ** virtual table currently under construction in pParse->pTable.
127733 if( pParse->sArg.z && pParse->pNewTable ){
127734 const char *z = (const char*)pParse->sArg.z;
127735 int n = pParse->sArg.n;
127736 sqlite3 *db = pParse->db;
127737 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
127746 Table *pTab = pParse->pNewTable; /* The table being constructed */
127747 sqlite3 *db = pParse->db; /* The database connection */
127751 pParse->sArg.z = 0;
127752 if( pTab->nModuleArg<1 ) return;
127760 if( !db->init.busy ){
127769 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
127771 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
127777 ** The VM register number pParse->regRowid holds the rowid of an
127781 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
127786 db->aDb[iDb].zDbSName, MASTER_NAME,
127787 pTab->zName,
127788 pTab->zName,
127790 pParse->regRowid
127797 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
127800 iReg = ++pParse->nMem;
127801 sqlite3VdbeLoadString(v, iReg, pTab->zName);
127805 /* If we are rereading the sqlite_master table create the in-memory
127812 Schema *pSchema = pTab->pSchema;
127813 const char *zName = pTab->zName;
127815 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
127821 pParse->pNewTable = 0;
127831 pParse->sArg.z = 0;
127832 pParse->sArg.n = 0;
127840 Token *pArg = &pParse->sArg;
127841 if( pArg->z==0 ){
127842 pArg->z = p->z;
127843 pArg->n = p->n;
127845 assert(pArg->z <= p->z);
127846 pArg->n = (int)(&p->z[p->n] - pArg->z);
127865 const char *const*azArg = (const char *const*)pTab->azModuleArg;
127866 int nArg = pTab->nModuleArg;
127872 /* Check that the virtual-table is not already being initialized */
127873 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
127874 if( pCtx->pTab==pTab ){
127876 "vtable constructor called recursively: %s", pTab->zName
127882 zModuleName = sqlite3DbStrDup(db, pTab->zName);
127893 pVTable->db = db;
127894 pVTable->pMod = pMod;
127896 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
127897 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
127900 assert( &db->pVtabCtx );
127904 sCtx.pPrior = db->pVtabCtx;
127906 db->pVtabCtx = &sCtx;
127907 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
127908 db->pVtabCtx = sCtx.pPrior;
127920 }else if( ALWAYS(pVTable->pVtab) ){
127923 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
127924 pVTable->pVtab->pModule = pMod->pModule;
127925 pVTable->nRef = 1;
127928 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
127935 ** into the linked list headed by pTab->pVTable. Then loop through the
127939 pVTable->pNext = pTab->pVTable;
127940 pTab->pVTable = pVTable;
127942 for(iCol=0; iCol<pTab->nCol; iCol++){
127943 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
127949 && (i==0 || zType[i-1]==' ')
127962 assert(zType[i-1]==' ');
127963 zType[i-1] = '\0';
127965 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
127968 pTab->tabFlags |= oooHidden;
127983 ** This call is a no-op if table pTab is not a virtual table.
127986 sqlite3 *db = pParse->db;
127997 zMod = pTab->azModuleArg[0];
127998 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
128001 const char *zModule = pTab->azModuleArg[0];
128006 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
128009 pParse->rc = rc;
128017 ** Grow the db->aVTrans[] array so that there is room for at least one
128018 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
128024 if( (db->nVTrans%ARRAY_INCR)==0 ){
128026 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
128027 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
128031 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
128032 db->aVTrans = aVTrans;
128044 db->aVTrans[db->nVTrans++] = pVTab;
128062 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
128063 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
128066 zMod = pTab->azModuleArg[0];
128067 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
128073 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
128077 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
128109 sqlite3_mutex_enter(db->mutex);
128110 pCtx = db->pVtabCtx;
128111 if( !pCtx || pCtx->bDeclared ){
128113 sqlite3_mutex_leave(db->mutex);
128116 pTab = pCtx->pTab;
128125 && !db->mallocFailed
128126 && !sParse.pNewTable->pSelect
128129 if( !pTab->aCol ){
128132 pTab->aCol = pNew->aCol;
128133 pTab->nCol = pNew->nCol;
128134 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
128135 pNew->nCol = 0;
128136 pNew->aCol = 0;
128137 assert( pTab->pIndex==0 );
128140 && pCtx->pVTable->pMod->pModule->xUpdate!=0
128141 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
128143 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
128144 ** or else must have a single-column PRIMARY KEY */
128147 pIdx = pNew->pIndex;
128149 assert( pIdx->pNext==0 );
128150 pTab->pIndex = pIdx;
128151 pNew->pIndex = 0;
128152 pIdx->pTable = pTab;
128155 pCtx->bDeclared = 1;
128171 sqlite3_mutex_leave(db->mutex);
128180 ** This call is a no-op if zTab is not a virtual table.
128186 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
128187 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
128190 for(p=pTab->pVTable; p; p=p->pNext){
128191 assert( p->pVtab );
128192 if( p->pVtab->nRef>0 ){
128197 xDestroy = p->pMod->pModule->xDestroy;
128199 rc = xDestroy(p->pVtab);
128202 assert( pTab->pVTable==p && p->pNext==0 );
128203 p->pVtab = 0;
128204 pTab->pVTable = 0;
128222 if( db->aVTrans ){
128223 VTable **aVTrans = db->aVTrans;
128224 db->aVTrans = 0;
128225 for(i=0; i<db->nVTrans; i++){
128227 sqlite3_vtab *p = pVTab->pVtab;
128230 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
128233 pVTab->iSavepoint = 0;
128237 db->nVTrans = 0;
128246 ** If an error message is available, leave it in p->zErrMsg.
128251 VTable **aVTrans = db->aVTrans;
128253 db->aVTrans = 0;
128254 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
128256 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
128257 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
128262 db->aVTrans = aVTrans;
128296 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
128307 pModule = pVTab->pVtab->pModule;
128309 if( pModule->xBegin ){
128313 for(i=0; i<db->nVTrans; i++){
128314 if( db->aVTrans[i]==pVTab ){
128323 rc = pModule->xBegin(pVTab->pVtab);
128325 int iSvpt = db->nStatement + db->nSavepoint;
128327 if( iSvpt && pModule->xSavepoint ){
128328 pVTab->iSavepoint = iSvpt;
128329 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
128356 assert( iSavepoint>=-1 );
128357 if( db->aVTrans ){
128359 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
128360 VTable *pVTab = db->aVTrans[i];
128361 const sqlite3_module *pMod = pVTab->pMod->pModule;
128362 if( pVTab->pVtab && pMod->iVersion>=2 ){
128366 xMethod = pMod->xSavepoint;
128367 pVTab->iSavepoint = iSavepoint+1;
128370 xMethod = pMod->xRollbackTo;
128373 xMethod = pMod->xRelease;
128376 if( xMethod && pVTab->iSavepoint>iSavepoint ){
128377 rc = xMethod(pVTab->pVtab, iSavepoint);
128417 if( pExpr->op!=TK_COLUMN ) return pDef;
128418 pTab = pExpr->pTab;
128421 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
128423 assert( pVtab->pModule!=0 );
128424 pMod = (sqlite3_module *)pVtab->pModule;
128425 if( pMod->xFindFunction==0 ) return pDef;
128430 zLowerName = sqlite3DbStrDup(db, pDef->zName);
128435 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
128445 + sqlite3Strlen30(pDef->zName) + 1);
128450 pNew->zName = (const char*)&pNew[1];
128451 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
128452 pNew->xSFunc = xSFunc;
128453 pNew->pUserData = pArg;
128454 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
128459 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
128462 ** is a no-op.
128470 for(i=0; i<pToplevel->nVtabLock; i++){
128471 if( pTab==pToplevel->apVtabLock[i] ) return;
128473 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
128474 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
128476 pToplevel->apVtabLock = apVtabLock;
128477 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
128479 sqlite3OomFault(pToplevel->db);
128486 ** exist. Return non-zero if the eponymous virtual table instance exists
128492 ** instances always exist. They cannot be DROP-ed.
128498 const sqlite3_module *pModule = pMod->pModule;
128502 sqlite3 *db = pParse->db;
128503 if( pMod->pEpoTab ) return 1;
128504 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
128507 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
128508 if( pTab->zName==0 ){
128512 pMod->pEpoTab = pTab;
128513 pTab->nTabRef = 1;
128514 pTab->pSchema = db->aDb[0].pSchema;
128515 assert( pTab->nModuleArg==0 );
128516 pTab->iPKey = -1;
128517 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
128519 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
128520 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
128535 Table *pTab = pMod->pEpoTab;
128540 pTab->tabFlags |= TF_Ephemeral;
128542 pMod->pEpoTab = 0;
128562 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
128563 return (int)aMap[db->vtabOnConflict-1];
128578 sqlite3_mutex_enter(db->mutex);
128582 VtabCtx *p = db->pVtabCtx;
128586 assert( p->pTab==0 || IsVirtual(p->pTab) );
128587 p->pVTable->bConstraint = (u8)va_arg(ap, int);
128598 sqlite3_mutex_leave(db->mutex);
128607 ** 2015-06-06
128620 ** This file was split off from where.c on 2015-06-06 in order to reduce the
128629 ** 2013-11-12
128687 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
128695 int addrSkip; /* Jump here for next iteration of skip-scan */
128706 union { /* Information that depends on pWLoop->wsFlags */
128714 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
128728 ** prevent a query solution - which is an error) and many terms of the
128730 ** potential way of implementing that FROM-clause term, together with
128746 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
128787 ** correspond to the subquery(s) of OR-clause processing. Only the
128803 ** vector, not a scalar, and because dependencies are many-to-one, not
128804 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
128820 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
128833 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
128864 ** spread out over the non-negative integers. For example, the cursor
128883 int iParent; /* Disable pWC->a[iParent] when this term disabled */
128891 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
128904 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
128914 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
128929 int k; /* Resume scanning at this->pWC->a[this->k] */
128931 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
128989 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
128990 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
128997 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
128998 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
129012 #define initMaskSet(P) (P)->n=0
129055 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
129063 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
129074 ** Private interfaces - callable only by other where.c routines.
129116 int iLevel, /* Which level of pWInfo->a[] should be coded */
129136 ** OR-ed combination of these values can be used when searching for
129148 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
129149 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
129150 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
129151 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
129155 #define WO_OR 0x0200 /* Two or more OR-connected terms */
129156 #define WO_AND 0x0400 /* Two or more AND-connected terms */
129161 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
129176 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
129184 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
129194 ** Return the name of the i-th column of the pIdx index.
129197 i = pIdx->aiColumn[i];
129200 return pIdx->pTable->aCol[i].zName;
129215 int iTerm, /* Zero-based index of first term. */
129216 int bAnd, /* Non-zero to append " AND " */
129256 Index *pIndex = pLoop->u.btree.pIndex;
129257 u16 nEq = pLoop->u.btree.nEq;
129258 u16 nSkip = pLoop->nSkip;
129261 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
129270 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
129271 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
129274 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
129275 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
129281 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
129283 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
129299 if( sqlite3ParseToplevel(pParse)->explain==2 )
129302 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
129303 Vdbe *v = pParse->pVdbe; /* VM being constructed */
129304 sqlite3 *db = pParse->db; /* Database handle */
129305 int iId = pParse->iSelectId; /* Select id (left-most output column) */
129313 pLoop = pLevel->pWLoop;
129314 flags = pLoop->wsFlags;
129318 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
129323 if( pItem->pSelect ){
129324 sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
129326 sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
129329 if( pItem->zAlias ){
129330 sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
129336 assert( pLoop->u.btree.pIndex!=0 );
129337 pIdx = pLoop->u.btree.pIndex;
129339 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
129354 sqlite3XPrintf(&str, zFmt, pIdx->zName);
129374 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
129378 if( pLoop->nOut>=10 ){
129379 sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
129408 WhereLoop *pLoop = pLvl->pWLoop;
129409 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
129410 zObj = pLoop->u.btree.pIndex->zName;
129412 zObj = pSrclist->a[pLvl->iFrom].zName;
129415 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
129461 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
129466 while( (pTerm->wtFlags & TERM_CODED)==0
129467 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
129468 && (pLevel->notReady & pTerm->prereqAll)==0
129470 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
129471 pTerm->wtFlags |= TERM_LIKECOND;
129473 pTerm->wtFlags |= TERM_CODED;
129475 if( pTerm->iParent<0 ) break;
129476 pTerm = &pTerm->pWC->a[pTerm->iParent];
129478 pTerm->nChild--;
129479 if( pTerm->nChild!=0 ) break;
129488 ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
129496 Vdbe *v = pParse->pVdbe;
129498 assert( pParse->db->mallocFailed );
129507 n--;
129511 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
129512 n--;
129584 sqlite3 *db = pParse->db;
129586 if( db->mallocFailed==0 ){
129587 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
129588 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
129594 for(i=iEq; i<pLoop->nLTerm; i++){
129595 if( pLoop->aLTerm[i]->pExpr==pX ){
129596 int iField = pLoop->aLTerm[i]->iField - 1;
129597 assert( pOrigRhs->a[iField].pExpr!=0 );
129598 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
129599 pOrigRhs->a[iField].pExpr = 0;
129600 assert( pOrigLhs->a[iField].pExpr!=0 );
129601 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
129602 pOrigLhs->a[iField].pExpr = 0;
129607 pNew->pLeft->x.pList = pLhs;
129608 pNew->x.pSelect->pEList = pRhs;
129609 if( pLhs && pLhs->nExpr==1 ){
129613 Expr *p = pLhs->a[0].pExpr;
129614 pLhs->a[0].pExpr = 0;
129615 sqlite3ExprDelete(db, pNew->pLeft);
129616 pNew->pLeft = p;
129618 pSelect = pNew->x.pSelect;
129619 if( pSelect->pOrderBy ){
129621 ** iOrderByCol variables. These are set to non-zero when an
129623 ** result-set. Since the result-set of the SELECT statement may
129627 ExprList *pOrderBy = pSelect->pOrderBy;
129628 for(i=0; i<pOrderBy->nExpr; i++){
129629 pOrderBy->a[i].u.x.iOrderByCol = 0;
129656 ** straight-line code. For constraints of the form X IN (...)
129664 int bRev, /* True for reverse-order IN operations */
129667 Expr *pX = pTerm->pExpr;
129668 Vdbe *v = pParse->pVdbe;
129671 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
129673 if( pX->op==TK_EQ || pX->op==TK_IS ){
129674 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
129675 }else if( pX->op==TK_ISNULL ){
129683 WhereLoop *pLoop = pLevel->pWLoop;
129688 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
129689 && pLoop->u.btree.pIndex!=0
129690 && pLoop->u.btree.pIndex->aSortOrder[iEq]
129696 assert( pX->op==TK_IN );
129700 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
129705 for(i=iEq;i<pLoop->nLTerm; i++){
129706 assert( pLoop->aLTerm[i]!=0 );
129707 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
129710 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
129713 sqlite3 *db = pParse->db;
129716 if( !db->mallocFailed ){
129717 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
129719 pTerm->pExpr->iTable = pX->iTable;
129722 pX = pTerm->pExpr;
129729 iTab = pX->iTable;
129733 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
129735 pLoop->wsFlags |= WHERE_IN_ABLE;
129736 if( pLevel->u.in.nIn==0 ){
129737 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
129740 i = pLevel->u.in.nIn;
129741 pLevel->u.in.nIn += nEq;
129742 pLevel->u.in.aInLoop =
129743 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
129744 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
129745 pIn = pLevel->u.in.aInLoop;
129749 for(i=iEq;i<pLoop->nLTerm; i++){
129750 if( pLoop->aLTerm[i]->pExpr==pX ){
129751 int iOut = iReg + i - iEq;
129754 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
129757 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
129761 pIn->iCur = iTab;
129762 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
129764 pIn->eEndLoopOp = OP_Noop;
129770 pLevel->u.in.nIn = 0;
129772 sqlite3DbFree(pParse->db, aiMap);
129792 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
129793 ** The only thing it does is allocate the pLevel->iMem memory cell and
129832 u16 nSkip; /* Number of left-most columns to skip */
129833 Vdbe *v = pParse->pVdbe; /* The vm under construction */
129843 pLoop = pLevel->pWLoop;
129844 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
129845 nEq = pLoop->u.btree.nEq;
129846 nSkip = pLoop->nSkip;
129847 pIdx = pLoop->u.btree.pIndex;
129852 regBase = pParse->nMem + 1;
129853 nReg = pLoop->u.btree.nEq + nExtraReg;
129854 pParse->nMem += nReg;
129856 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
129857 assert( zAff!=0 || pParse->db->mallocFailed );
129860 int iIdxCur = pLevel->iIdxCur;
129864 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
129866 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
129873 testcase( pIdx->aiColumn[j]==XN_EXPR );
129883 pTerm = pLoop->aLTerm[j];
129887 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
129888 testcase( pTerm->wtFlags & TERM_VIRTUAL );
129898 if( pTerm->eOperator & WO_IN ){
129899 if( pTerm->pExpr->flags & EP_xIsSelect ){
129906 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
129907 Expr *pRight = pTerm->pExpr->pRight;
129908 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
129909 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
129942 ** becomes a no-op.
129949 if( pTerm->wtFlags & TERM_LIKEOPT ){
129951 assert( pLevel->iLikeRepCntr>0 );
129952 pOp = sqlite3VdbeGetOp(v, -1);
129954 assert( pOp->opcode==OP_String8
129955 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
129956 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
129957 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
129980 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
129983 struct CCurHint *pHint = pWalker->u.pCCurHint;
129984 assert( pHint->pIdx!=0 );
129985 if( pExpr->op==TK_COLUMN
129986 && pExpr->iTable==pHint->iTabCur
129987 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
129989 pWalker->eCode = 1;
129996 ** should be included in the cursor-hint for a table that is on the rhs
129997 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
130010 if( pExpr->op==TK_IS
130011 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
130012 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
130014 pWalker->eCode = 1;
130015 }else if( pExpr->op==TK_FUNCTION ){
130018 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
130019 pWalker->eCode = 1;
130046 struct CCurHint *pHint = pWalker->u.pCCurHint;
130047 if( pExpr->op==TK_COLUMN ){
130048 if( pExpr->iTable!=pHint->iTabCur ){
130049 Vdbe *v = pWalker->pParse->pVdbe;
130050 int reg = ++pWalker->pParse->nMem; /* Register for column value */
130052 v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg
130054 pExpr->op = TK_REGISTER;
130055 pExpr->iTable = reg;
130056 }else if( pHint->pIdx!=0 ){
130057 pExpr->iTable = pHint->iIdxCur;
130058 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
130059 assert( pExpr->iColumn>=0 );
130061 }else if( pExpr->op==TK_AGG_FUNCTION ){
130063 ** be a correlated sub-query, and expression pExpr is an aggregate from
130081 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
130083 Parse *pParse = pWInfo->pParse;
130084 sqlite3 *db = pParse->db;
130085 Vdbe *v = pParse->pVdbe;
130087 WhereLoop *pLoop = pLevel->pWLoop;
130096 iCur = pLevel->iTabCur;
130097 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
130099 sHint.iIdxCur = pLevel->iIdxCur;
130100 sHint.pIdx = pLoop->u.btree.pIndex;
130104 pWC = &pWInfo->sWC;
130105 for(i=0; i<pWC->nTerm; i++){
130106 pTerm = &pWC->a[i];
130107 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
130108 if( pTerm->prereqAll & pLevel->notReady ) continue;
130112 ** from the cursor-hint.
130132 if( pTabItem->fg.jointype & JT_LEFT ){
130133 Expr *pExpr = pTerm->pExpr;
130135 || pExpr->iRightJoinTable!=pTabItem->iCursor
130139 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
130143 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
130146 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
130149 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
130150 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
130151 if( j<pLoop->nLTerm ) continue;
130154 /* No subqueries or non-deterministic functions allowed */
130155 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
130162 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
130167 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
130178 # define codeCursorHint(A,B,C,D) /* No-op */
130182 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
130191 ** However, if the scan currently being coded is a branch of an OR-loop and
130202 int iCur, /* Cursor for IPK b-tree */
130205 Parse *pParse = pWInfo->pParse; /* Parse context */
130206 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
130209 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
130212 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
130213 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
130216 Table *pTab = pIdx->pTable;
130217 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
130219 ai[0] = pTab->nCol;
130220 for(i=0; i<pIdx->nColumn-1; i++){
130221 assert( pIdx->aiColumn[i]<pTab->nCol );
130222 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
130224 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
130242 if( (p->flags & EP_xIsSelect) ){
130243 Vdbe *v = pParse->pVdbe;
130245 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
130250 ExprList *pList = p->x.pList;
130251 assert( nReg<=pList->nExpr );
130253 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
130280 IdxExprTrans *pX = p->u.pIdxTrans;
130281 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
130282 pExpr->op = TK_COLUMN;
130283 pExpr->iTable = pX->iIdxCur;
130284 pExpr->iColumn = pX->iIdxCol;
130285 pExpr->pTab = 0;
130307 aColExpr = pIdx->aColExpr;
130314 for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
130315 if( pIdx->aiColumn[iIdxCol]!=XN_EXPR ) continue;
130316 assert( aColExpr->a[iIdxCol].pExpr!=0 );
130318 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
130319 sqlite3WalkExpr(&w, pWInfo->pWhere);
130320 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
130321 sqlite3WalkExprList(&w, pWInfo->pResultSet);
130326 ** Generate code for the start of the iLevel-th loop in the WHERE clause
130331 int iLevel, /* Which level of pWInfo->a[] should be coded */
130355 pParse = pWInfo->pParse;
130356 v = pParse->pVdbe;
130357 pWC = &pWInfo->sWC;
130358 db = pParse->db;
130359 pLevel = &pWInfo->a[iLevel];
130360 pLoop = pLevel->pWLoop;
130361 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
130362 iCur = pTabItem->iCursor;
130363 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
130364 bRev = (pWInfo->revMask>>iLevel)&1;
130365 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
130366 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
130367 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
130379 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
130380 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
130386 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
130387 pLevel->iLeftJoin = ++pParse->nMem;
130388 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
130389 VdbeComment((v, "init LEFT JOIN no-match flag"));
130394 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
130395 addrHalt = pWInfo->a[j].addrBrk;
130397 /* Special case of a FROM clause subquery implemented as a co-routine */
130398 if( pTabItem->fg.viaCoroutine ){
130399 int regYield = pTabItem->regReturn;
130400 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
130401 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
130403 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
130404 pLevel->op = OP_Goto;
130408 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
130409 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
130414 int nConstraint = pLoop->nLTerm;
130419 addrNotFound = pLevel->addrBrk;
130422 pTerm = pLoop->aLTerm[j];
130424 if( pTerm->eOperator & WO_IN ){
130426 addrNotFound = pLevel->addrNxt;
130428 Expr *pRight = pTerm->pExpr->pRight;
130432 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
130435 pLoop->u.vtab.idxStr,
130436 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
130438 pLoop->u.vtab.needFree = 0;
130439 pLevel->p1 = iCur;
130440 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
130441 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
130442 iIn = pLevel->u.in.nIn;
130443 for(j=nConstraint-1; j>=0; j--){
130444 pTerm = pLoop->aLTerm[j];
130445 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
130447 }else if( (pTerm->eOperator & WO_IN)!=0 ){
130456 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
130457 if( !db->mallocFailed ){
130459 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
130460 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
130461 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
130462 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
130463 testcase( pOp->opcode==OP_Rowid );
130464 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
130470 assert( pCompare!=0 || db->mallocFailed );
130472 pCompare->pLeft = pTerm->pExpr->pLeft;
130473 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
130475 pRight->iTable = iReg+j+2;
130476 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
130478 pCompare->pLeft = 0;
130485 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
130494 if( (pLoop->wsFlags & WHERE_IPK)!=0
130495 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
130502 assert( pLoop->u.btree.nEq==1 );
130503 pTerm = pLoop->aLTerm[0];
130505 assert( pTerm->pExpr!=0 );
130507 testcase( pTerm->wtFlags & TERM_VIRTUAL );
130508 iReleaseReg = ++pParse->nMem;
130511 addrNxt = pLevel->addrNxt;
130515 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
130517 pLevel->op = OP_Noop;
130518 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
130519 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
130531 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
130532 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
130558 assert( (pStart->wtFlags & TERM_VNULL)==0 );
130559 testcase( pStart->wtFlags & TERM_VIRTUAL );
130560 pX = pStart->pExpr;
130562 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130563 if( sqlite3ExprIsVector(pX->pRight) ){
130565 codeExprOrVector(pParse, pX->pRight, r1, 1);
130566 op = aMoveOp[(pX->op - TK_GT) | 0x0001];
130568 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130570 op = aMoveOp[(pX->op - TK_GT)];
130574 VdbeCoverageIf(v, pX->op==TK_GT);
130575 VdbeCoverageIf(v, pX->op==TK_LE);
130576 VdbeCoverageIf(v, pX->op==TK_LT);
130577 VdbeCoverageIf(v, pX->op==TK_GE);
130587 pX = pEnd->pExpr;
130589 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
130590 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
130591 testcase( pEnd->wtFlags & TERM_VIRTUAL );
130592 memEndValue = ++pParse->nMem;
130593 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
130594 if( 0==sqlite3ExprIsVector(pX->pRight)
130595 && (pX->op==TK_LT || pX->op==TK_GT)
130601 if( 0==sqlite3ExprIsVector(pX->pRight) ){
130606 pLevel->op = bRev ? OP_Prev : OP_Next;
130607 pLevel->p1 = iCur;
130608 pLevel->p2 = start;
130609 assert( pLevel->p5==0 );
130611 iRowidReg = ++pParse->nMem;
130613 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
130621 }else if( pLoop->wsFlags & WHERE_INDEXED ){
130626 ** left-most columns of the index. It may also contain
130629 ** the right-most column can be an inequality - the rest must
130669 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
130670 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
130671 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
130687 pIdx = pLoop->u.btree.pIndex;
130688 iIdxCur = pLevel->iIdxCur;
130689 assert( nEq>=pLoop->nSkip );
130699 assert( pWInfo->pOrderBy==0
130700 || pWInfo->pOrderBy->nExpr==1
130701 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
130702 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
130703 && pWInfo->nOBSat>0
130704 && (pIdx->nKeyCol>nEq)
130706 assert( pLoop->nSkip==0 );
130715 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
130716 pRangeStart = pLoop->aLTerm[j++];
130717 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
130719 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
130720 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
130722 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
130723 pRangeEnd = pLoop->aLTerm[j++];
130724 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
130726 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
130728 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
130729 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
130730 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
130732 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
130736 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
130738 pLevel->iLikeRepCntr <<=1;
130739 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
130743 j = pIdx->aiColumn[nEq];
130744 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
130749 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
130755 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
130756 || (bRev && pIdx->nKeyCol==nEq)
130773 addrNxt = pLevel->addrNxt;
130775 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
130776 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
130777 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
130778 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
130779 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
130780 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
130786 Expr *pRight = pRangeStart->pExpr->pRight;
130789 if( (pRangeStart->wtFlags & TERM_VNULL)==0
130799 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
130812 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
130813 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
130814 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
130835 Expr *pRight = pRangeEnd->pExpr->pRight;
130839 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
130849 assert( pParse->db->mallocFailed );
130852 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
130869 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
130884 }else if( HasRowid(pIdx->pTable) ){
130885 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
130886 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
130887 && (pWInfo->eOnePass==ONEPASS_SINGLE)
130889 iRowidReg = ++pParse->nMem;
130891 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
130898 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
130899 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
130900 for(j=0; j<pPk->nKeyCol; j++){
130901 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
130905 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
130916 if( pLoop->wsFlags & WHERE_ONEROW ){
130917 pLevel->op = OP_Noop;
130919 pLevel->op = OP_Prev;
130921 pLevel->op = OP_Next;
130923 pLevel->p1 = iIdxCur;
130924 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
130925 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
130926 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
130928 assert( pLevel->p5==0 );
130934 if( pLoop->wsFlags & WHERE_MULTI_OR ){
130973 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
130978 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
130979 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
130981 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
130983 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
130990 u16 wctrlFlags; /* Flags for sub-WHERE clause */
130992 Table *pTab = pTabItem->pTab;
130994 pTerm = pLoop->aLTerm[0];
130996 assert( pTerm->eOperator & WO_OR );
130997 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
130998 pOrWc = &pTerm->u.pOrInfo->wc;
130999 pLevel->op = OP_Return;
131000 pLevel->p1 = regReturn;
131006 if( pWInfo->nLevel>1 ){
131009 nNotReady = pWInfo->nLevel - iLevel - 1;
131011 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
131013 pOrTab->nAlloc = (u8)(nNotReady + 1);
131014 pOrTab->nSrc = pOrTab->nAlloc;
131015 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
131016 origSrc = pWInfo->pTabList->a;
131018 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
131021 pOrTab = pWInfo->pTabList;
131032 ** correct response for the end-of-loop code (the OP_Return) is to
131036 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
131038 regRowset = ++pParse->nMem;
131042 regRowset = pParse->nTab++;
131043 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
131046 regRowid = ++pParse->nMem;
131056 ** the "interesting" terms of z - terms that did not originate in the
131064 if( pWC->nTerm>1 ){
131066 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
131067 Expr *pExpr = pWC->a[iTerm].pExpr;
131068 if( &pWC->a[iTerm] == pTerm ) continue;
131070 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
131071 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
131072 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
131073 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
131074 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
131085 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
131087 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
131088 for(ii=0; ii<pOrWc->nTerm; ii++){
131089 WhereTerm *pOrTerm = &pOrWc->a[ii];
131090 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
131091 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
131092 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
131095 pAndExpr->pLeft = pOrExpr;
131099 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
131102 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
131106 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
131108 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
131110 /* This is the sub-WHERE clause body. First skip over
131111 ** duplicate rows from prior sub-WHERE clauses, and record the
131113 ** row will be skipped in subsequent sub-WHERE clauses.
131115 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
131117 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
131119 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
131125 int nPk = pPk->nKeyCol;
131131 int iCol = pPk->aiColumn[iPk];
131143 ** the temp table. And if iSet is -1, assume that there is no
131166 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
131169 /* The pSubWInfo->untestedTerms flag means that this OR term
131174 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
131176 /* If all of the OR-connected terms are optimized using the same
131182 ** uses an index, and this is either the first OR-connected term
131188 pSubLoop = pSubWInfo->a[0].pWLoop;
131189 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
131190 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
131191 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
131192 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
131194 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
131195 pCov = pSubLoop->u.btree.pIndex;
131205 pLevel->u.pCovidx = pCov;
131206 if( pCov ) pLevel->iIdxCur = iCovCur;
131208 pAndExpr->pLeft = 0;
131212 sqlite3VdbeGoto(v, pLevel->addrBrk);
131215 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
131227 if( pTabItem->fg.isRecursive ){
131229 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
131230 pLevel->op = OP_Noop;
131233 pLevel->op = aStep[bRev];
131234 pLevel->p1 = iCur;
131235 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
131238 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
131243 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
131255 ** sub-queries.
131263 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
131266 testcase( pTerm->wtFlags & TERM_VIRTUAL );
131267 testcase( pTerm->wtFlags & TERM_CODED );
131268 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
131269 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
131270 testcase( pWInfo->untestedTerms==0
131271 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
131272 pWInfo->untestedTerms = 1;
131275 pE = pTerm->pExpr;
131277 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
131281 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
131285 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
131290 if( pTerm->wtFlags & TERM_LIKECOND ){
131299 u32 x = pLevel->iLikeRepCntr;
131308 pWC->nTerm-j, pTerm, iLoop));
131313 pTerm->wtFlags |= TERM_CODED;
131326 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
131329 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
131330 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
131331 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
131332 if( pTerm->leftCursor!=iCur ) continue;
131333 if( pLevel->iLeftJoin ) continue;
131334 pE = pTerm->pExpr;
131336 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131337 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131340 if( pAlt->wtFlags & (TERM_CODED) ) continue;
131341 testcase( pAlt->eOperator & WO_EQ );
131342 testcase( pAlt->eOperator & WO_IS );
131343 testcase( pAlt->eOperator & WO_IN );
131345 sEAlt = *pAlt->pExpr;
131346 sEAlt.pLeft = pE->pLeft;
131353 if( pLevel->iLeftJoin ){
131354 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
131355 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
131358 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
131359 testcase( pTerm->wtFlags & TERM_VIRTUAL );
131360 testcase( pTerm->wtFlags & TERM_CODED );
131361 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
131362 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
131363 assert( pWInfo->untestedTerms );
131366 assert( pTerm->pExpr );
131367 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
131368 pTerm->wtFlags |= TERM_CODED;
131372 return pLevel->notReady;
131378 ** 2015-06-08
131405 sqlite3WhereClauseClear(&p->wc);
131413 sqlite3WhereClauseClear(&p->wc);
131420 ** The index in pWC->a[] of the new WhereTerm is returned on success.
131423 ** the db->mallocFailed flag so that higher-level functions can detect it.
131425 ** This routine will increase the size of the pWC->a[] array as necessary.
131434 ** the pWC->a[] array.
131440 if( pWC->nTerm>=pWC->nSlot ){
131441 WhereTerm *pOld = pWC->a;
131442 sqlite3 *db = pWC->pWInfo->pParse->db;
131443 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
131444 if( pWC->a==0 ){
131448 pWC->a = pOld;
131451 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
131452 if( pOld!=pWC->aStatic ){
131455 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
131457 pTerm = &pWC->a[idx = pWC->nTerm++];
131459 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
131461 pTerm->truthProb = 1;
131463 pTerm->pExpr = sqlite3ExprSkipCollate(p);
131464 pTerm->wtFlags = wtFlags;
131465 pTerm->pWC = pWC;
131466 pTerm->iParent = -1;
131467 memset(&pTerm->eOperator, 0,
131468 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
131498 u16 expRight = (pExpr->pRight->flags & EP_Collate);
131499 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
131500 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
131506 pExpr->pRight->flags &= ~EP_Collate;
131507 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
131508 /* Neither X nor Y have COLLATE operators, but X has a non-default
131511 pExpr->pLeft->flags |= EP_Collate;
131514 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
131515 if( pExpr->op>=TK_GT ){
131520 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
131521 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
131538 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
131539 c = (u16)(WO_EQ<<(op-TK_EQ));
131577 int cnt; /* Number of non-wildcard prefix characters */
131579 sqlite3 *db = pParse->db; /* Database connection */
131590 pList = pExpr->x.pList;
131591 pLeft = pList->a[1].pExpr;
131593 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
131594 op = pRight->op;
131595 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
131596 Vdbe *pReprepare = pParse->pReprepare;
131597 int iCol = pRight->iColumn;
131602 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
131603 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
131605 z = (u8*)pRight->u.zToken;
131613 ** with a digit or '-', then "lhs LIKE rhs" will always be false if
131616 if( sqlite3Isdigit(z[0]) || z[0]=='-' ){
131617 if( pLeft->op!=TK_COLUMN
131619 || IsVirtual(pLeft->pTab) /* Value might be numeric */
131634 ** with a wildcard and if (2) the non-wildcard prefix does not end with
131639 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
131649 char *zNew = pPrefix->u.zToken;
131662 Vdbe *v = pParse->pVdbe;
131663 sqlite3VdbeSetVarmask(v, pRight->iColumn);
131664 if( *pisComplete && pRight->u.zToken[1] ){
131673 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
131695 ** ----------------------- ---------------------------------
131722 if( pExpr->op==TK_FUNCTION ){
131736 pList = pExpr->x.pList;
131737 if( pList==0 || pList->nExpr!=2 ){
131740 pCol = pList->a[1].pExpr;
131741 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
131745 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
131747 *ppRight = pList->a[0].pExpr;
131752 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
131754 Expr *pLeft = pExpr->pLeft;
131755 Expr *pRight = pExpr->pRight;
131756 if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
131759 if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
131765 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
131766 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
131767 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
131780 pDerived->flags |= pBase->flags & EP_FromJoin;
131781 pDerived->iRightJoinTable = pBase->iRightJoinTable;
131789 pWC->a[iChild].iParent = iParent;
131790 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
131791 pWC->a[iParent].nChild++;
131795 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
131800 if( pTerm->eOperator!=WO_AND ){
131803 if( N<pTerm->u.pAndInfo->wc.nTerm ){
131804 return &pTerm->u.pAndInfo->wc.a[N];
131811 ** two subterms are in disjunction - they are OR-ed together.
131821 ** x<y OR x=y --> x<=y
131822 ** x=y OR x=y --> x=y
131823 ** x<=y OR x<y --> x<=y
131827 ** x<y OR x>y --> x!=y
131835 u16 eOp = pOne->eOperator | pTwo->eOperator;
131841 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
131842 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
131845 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
131846 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
131847 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
131848 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
131850 if( (eOp & (eOp-1))!=0 ){
131858 db = pWC->pWInfo->pParse->db;
131859 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
131861 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
131862 pNew->op = op;
131869 ** Analyze a term that consists of two or more OR-connected
131882 ** The term being analyzed must have two or more of OR-connected subterms.
131883 ** A single subterm might be a set of AND-connected sub-subterms.
131921 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
131958 int idxTerm /* Index of the OR-term to be analyzed */
131960 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
131961 Parse *pParse = pWInfo->pParse; /* Parser context */
131962 sqlite3 *db = pParse->db; /* Database connection */
131963 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
131964 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
131967 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
131977 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
131978 assert( pExpr->op==TK_OR );
131979 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
131981 pTerm->wtFlags |= TERM_ORINFO;
131982 pOrWc = &pOrInfo->wc;
131983 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
131987 if( db->mallocFailed ) return;
131988 assert( pOrWc->nTerm>=2 );
131995 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
131996 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
131998 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
132006 pOrTerm->u.pAndInfo = pAndInfo;
132007 pOrTerm->wtFlags |= TERM_ANDINFO;
132008 pOrTerm->eOperator = WO_AND;
132009 pAndWC = &pAndInfo->wc;
132010 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
132011 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
132012 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
132014 pAndWC->pOuter = pWC;
132015 if( !db->mallocFailed ){
132016 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
132017 assert( pAndTerm->pExpr );
132018 if( allowedOp(pAndTerm->pExpr->op)
132019 || pAndTerm->eOperator==WO_AUX
132021 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
132027 }else if( pOrTerm->wtFlags & TERM_COPIED ){
132032 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
132033 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
132034 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
132035 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
132038 if( (pOrTerm->eOperator & WO_EQ)==0 ){
132050 pOrInfo->indexable = indexable;
132051 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
132053 /* For a two-way OR, attempt to implementation case 2.
132055 if( indexable && pOrWc->nTerm==2 ){
132058 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
132061 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
132072 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
132075 ** something other than == on a column in the single table. The 1-bit
132079 ** sure the same column is used on all terms. The 2-bit case is when
132090 int iColumn = -1; /* Column index on lhs of IN operator */
132091 int iCursor = -1; /* Table cursor common to all terms */
132101 pOrTerm = pOrWc->a;
132102 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
132103 assert( pOrTerm->eOperator & WO_EQ );
132104 pOrTerm->wtFlags &= ~TERM_OR_OK;
132105 if( pOrTerm->leftCursor==iCursor ){
132106 /* This is the 2-bit case and we are on the second iteration and
132111 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
132112 pOrTerm->leftCursor))==0 ){
132117 testcase( pOrTerm->wtFlags & TERM_COPIED );
132118 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
132119 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
132122 iColumn = pOrTerm->u.leftColumn;
132123 iCursor = pOrTerm->leftCursor;
132131 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
132139 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
132140 assert( pOrTerm->eOperator & WO_EQ );
132141 if( pOrTerm->leftCursor!=iCursor ){
132142 pOrTerm->wtFlags &= ~TERM_OR_OK;
132143 }else if( pOrTerm->u.leftColumn!=iColumn ){
132147 /* If the right-hand side is also a column, then the affinities
132151 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
132152 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
132156 pOrTerm->wtFlags |= TERM_OR_OK;
132172 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
132173 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
132174 assert( pOrTerm->eOperator & WO_EQ );
132175 assert( pOrTerm->leftCursor==iCursor );
132176 assert( pOrTerm->u.leftColumn==iColumn );
132177 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
132178 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
132179 pLeft = pOrTerm->pExpr->pLeft;
132188 pNew->x.pList = pList;
132192 pTerm = &pWC->a[idxTerm];
132197 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
132221 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
132222 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
132224 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
132225 aff2 = sqlite3ExprAffinity(pExpr->pRight);
132231 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
132232 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
132233 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
132244 SrcList *pSrc = pS->pSrc;
132245 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
132246 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
132247 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
132248 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
132249 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
132252 for(i=0; i<pSrc->nSrc; i++){
132253 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132254 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
132257 pS = pS->pPrior;
132268 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
132285 iCur = pFrom->a[i].iCursor;
132286 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
132287 if( pIdx->aColExpr==0 ) continue;
132288 for(i=0; i<pIdx->nKeyCol; i++){
132289 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
132290 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
132312 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
132313 pExpr = pExpr->x.pList->a[0].pExpr;
132316 if( pExpr->op==TK_COLUMN ){
132317 aiCurCol[0] = pExpr->iTable;
132318 aiCurCol[1] = pExpr->iColumn;
132322 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
132349 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
132353 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
132359 int op; /* Top-level operator. pExpr->op */
132360 Parse *pParse = pWInfo->pParse; /* Parsing context */
132361 sqlite3 *db = pParse->db; /* Database connection */
132365 if( db->mallocFailed ){
132368 pTerm = &pWC->a[idxTerm];
132369 pMaskSet = &pWInfo->sMaskSet;
132370 pExpr = pTerm->pExpr;
132371 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
132372 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
132373 op = pExpr->op;
132375 assert( pExpr->pRight==0 );
132378 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
132380 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
132383 pTerm->prereqRight = 0;
132385 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
132387 pMaskSet->bVarSelect = 0;
132389 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
132391 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
132393 extraRight = x-1; /* ON clause terms may not be used with an index
132400 pTerm->prereqAll = prereqAll;
132401 pTerm->leftCursor = -1;
132402 pTerm->iParent = -1;
132403 pTerm->eOperator = 0;
132406 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
132407 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
132408 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
132410 if( pTerm->iField>0 ){
132412 assert( pLeft->op==TK_VECTOR );
132413 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
132417 pTerm->leftCursor = aiCurCol[0];
132418 pTerm->u.leftColumn = aiCurCol[1];
132419 pTerm->eOperator = operatorMask(op) & opMask;
132421 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
132423 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
132427 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
132428 assert( pTerm->iField==0 );
132429 if( pTerm->leftCursor>=0 ){
132432 if( db->mallocFailed ){
132438 pNew = &pWC->a[idxNew];
132440 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
132441 pTerm = &pWC->a[idxTerm];
132442 pTerm->wtFlags |= TERM_COPIED;
132445 pTerm->eOperator |= WO_EQUIV;
132453 pNew->leftCursor = aiCurCol[0];
132454 pNew->u.leftColumn = aiCurCol[1];
132456 pNew->prereqRight = prereqLeft | extraRight;
132457 pNew->prereqAll = prereqAll;
132458 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
132478 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
132479 ExprList *pList = pExpr->x.pList;
132483 assert( pList->nExpr==2 );
132488 sqlite3ExprDup(db, pExpr->pLeft, 0),
132489 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
132494 pTerm = &pWC->a[idxTerm];
132504 else if( pExpr->op==TK_OR ){
132505 assert( pWC->op==TK_AND );
132507 pTerm = &pWC->a[idxTerm];
132521 ** for LIKE) then the lower-bound is made all uppercase and the upper-
132525 if( pWC->op==TK_AND
132529 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
132537 pLeft = pExpr->x.pList->a[1].pExpr;
132540 /* Convert the lower bound to upper-case and the upper bound to
132541 ** lower-case (upper-case is less than lower-case in ASCII) so that
132544 if( noCase && !pParse->db->mallocFailed ){
132547 pTerm->wtFlags |= TERM_LIKE;
132548 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
132549 pStr1->u.zToken[i] = sqlite3Toupper(c);
132550 pStr2->u.zToken[i] = sqlite3Tolower(c);
132554 if( !db->mallocFailed ){
132556 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
132565 if( c=='A'-1 ) isComplete = 0;
132587 pTerm = &pWC->a[idxTerm];
132605 if( pWC->op==TK_AND ){
132608 while( res-- > 0 ){
132624 pNewTerm = &pWC->a[idxNew];
132625 pNewTerm->prereqRight = prereqExpr;
132626 pNewTerm->leftCursor = pLeft->iTable;
132627 pNewTerm->u.leftColumn = pLeft->iColumn;
132628 pNewTerm->eOperator = WO_AUX;
132629 pNewTerm->eMatchOp = eOp2;
132631 pTerm = &pWC->a[idxTerm];
132632 pTerm->wtFlags |= TERM_COPIED;
132633 pNewTerm->prereqAll = pTerm->prereqAll;
132640 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
132641 ** new terms for each component comparison - "a = ?" and "b = ?". The
132646 ** is not a sub-select. */
132647 if( pWC->op==TK_AND
132648 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
132649 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
132650 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
132651 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
132652 || (pExpr->pRight->flags & EP_xIsSelect)==0)
132658 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
132659 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
132661 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
132666 pTerm = &pWC->a[idxTerm];
132667 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */
132668 pTerm->eOperator = 0;
132671 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132679 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
132680 && pExpr->pLeft->op==TK_VECTOR
132681 && pExpr->x.pSelect->pPrior==0
132684 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
132687 pWC->a[idxNew].iField = i+1;
132701 if( pExpr->op==TK_NOTNULL
132702 && pExpr->pLeft->op==TK_COLUMN
132703 && pExpr->pLeft->iColumn>=0
132707 Expr *pLeft = pExpr->pLeft;
132718 pNewTerm = &pWC->a[idxNew];
132719 pNewTerm->prereqRight = 0;
132720 pNewTerm->leftCursor = pLeft->iTable;
132721 pNewTerm->u.leftColumn = pLeft->iColumn;
132722 pNewTerm->eOperator = WO_GT;
132724 pTerm = &pWC->a[idxTerm];
132725 pTerm->wtFlags |= TERM_COPIED;
132726 pNewTerm->prereqAll = pTerm->prereqAll;
132734 testcase( pTerm!=&pWC->a[idxTerm] );
132735 pTerm = &pWC->a[idxTerm];
132736 pTerm->prereqRight |= extraRight;
132763 pWC->op = op;
132765 if( pE2->op!=op ){
132768 sqlite3WhereSplit(pWC, pE2->pLeft, op);
132769 sqlite3WhereSplit(pWC, pE2->pRight, op);
132780 pWC->pWInfo = pWInfo;
132781 pWC->pOuter = 0;
132782 pWC->nTerm = 0;
132783 pWC->nSlot = ArraySize(pWC->aStatic);
132784 pWC->a = pWC->aStatic;
132795 sqlite3 *db = pWC->pWInfo->pParse->db;
132796 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
132797 if( a->wtFlags & TERM_DYNAMIC ){
132798 sqlite3ExprDelete(db, a->pExpr);
132800 if( a->wtFlags & TERM_ORINFO ){
132801 whereOrInfoDelete(db, a->u.pOrInfo);
132802 }else if( a->wtFlags & TERM_ANDINFO ){
132803 whereAndInfoDelete(db, a->u.pAndInfo);
132806 if( pWC->a!=pWC->aStatic ){
132807 sqlite3DbFree(db, pWC->a);
132820 if( p->op==TK_COLUMN ){
132821 return sqlite3WhereGetMask(pMaskSet, p->iTable);
132823 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
132825 if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
132826 if( p->pRight ){
132827 mask |= sqlite3WhereExprUsage(pMaskSet, p->pRight);
132828 assert( p->x.pList==0 );
132830 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
132831 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
132832 }else if( p->x.pList ){
132833 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
132841 for(i=0; i<pList->nExpr; i++){
132842 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
132862 for(i=pWC->nTerm-1; i>=0; i--){
132868 ** For table-valued-functions, transform the function arguments into
132884 if( pItem->fg.isTabFunc==0 ) return;
132885 pTab = pItem->pTab;
132887 pArgs = pItem->u1.pFuncArg;
132889 for(j=k=0; j<pArgs->nExpr; j++){
132890 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
132891 if( k>=pTab->nCol ){
132892 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
132893 pTab->zName, j);
132896 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
132898 pColRef->iTable = pItem->iCursor;
132899 pColRef->iColumn = k++;
132900 pColRef->pTab = pTab;
132902 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
132958 return pWInfo->nRowOut;
132966 return pWInfo->eDistinct;
132974 return pWInfo->nOBSat;
132986 return pWInfo->bOrderedInnerLoop;
132994 assert( pWInfo->iContinue!=0 );
132995 return pWInfo->iContinue;
133003 return pWInfo->iBreak;
133010 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
133017 ** Either value may be -1, indicating that cursor is not used.
133020 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
133024 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
133026 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
133028 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
133032 return pWInfo->eOnePass;
133039 pDest->n = pSrc->n;
133040 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
133053 LogEst rRun, /* Run-cost of the new entry */
133058 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
133059 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
133062 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
133066 if( pSet->n<N_OR_COST ){
133067 p = &pSet->a[pSet->n++];
133068 p->nOut = nOut;
133070 p = pSet->a;
133071 for(i=1; i<pSet->n; i++){
133072 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
133074 if( p->rRun<=rRun ) return 0;
133077 p->prereq = prereq;
133078 p->rRun = rRun;
133079 if( p->nOut>nOut ) p->nOut = nOut;
133089 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
133090 for(i=0; i<pMaskSet->n; i++){
133091 if( pMaskSet->ix[i]==iCursor ){
133103 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
133107 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
133108 pMaskSet->ix[pMaskSet->n++] = iCursor;
133118 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
133120 WhereClause *pWC; /* Shorthand for pScan->pWC */
133122 int k = pScan->k; /* Where to start scanning */
133124 assert( pScan->iEquiv<=pScan->nEquiv );
133125 pWC = pScan->pWC;
133127 iColumn = pScan->aiColumn[pScan->iEquiv-1];
133128 iCur = pScan->aiCur[pScan->iEquiv-1];
133131 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
133132 if( pTerm->leftCursor==iCur
133133 && pTerm->u.leftColumn==iColumn
133135 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
133136 pScan->pIdxExpr,iCur)==0)
133137 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
133139 if( (pTerm->eOperator & WO_EQUIV)!=0
133140 && pScan->nEquiv<ArraySize(pScan->aiCur)
133141 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
133144 for(j=0; j<pScan->nEquiv; j++){
133145 if( pScan->aiCur[j]==pX->iTable
133146 && pScan->aiColumn[j]==pX->iColumn ){
133150 if( j==pScan->nEquiv ){
133151 pScan->aiCur[j] = pX->iTable;
133152 pScan->aiColumn[j] = pX->iColumn;
133153 pScan->nEquiv++;
133156 if( (pTerm->eOperator & pScan->opMask)!=0 ){
133158 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
133160 Parse *pParse = pWC->pWInfo->pParse;
133161 pX = pTerm->pExpr;
133162 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
133165 assert(pX->pLeft);
133167 pX->pLeft, pX->pRight);
133168 if( pColl==0 ) pColl = pParse->db->pDfltColl;
133169 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
133173 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
133174 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
133175 && pX->iTable==pScan->aiCur[0]
133176 && pX->iColumn==pScan->aiColumn[0]
133178 testcase( pTerm->eOperator & WO_IS );
133181 pScan->pWC = pWC;
133182 pScan->k = k+1;
133187 pWC = pWC->pOuter;
133190 if( pScan->iEquiv>=pScan->nEquiv ) break;
133191 pWC = pScan->pOrigWC;
133193 pScan->iEquiv++;
133225 pScan->pOrigWC = pWC;
133226 pScan->pWC = pWC;
133227 pScan->pIdxExpr = 0;
133228 pScan->idxaff = 0;
133229 pScan->zCollName = 0;
133232 iColumn = pIdx->aiColumn[j];
133234 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
133235 pScan->zCollName = pIdx->azColl[j];
133236 }else if( iColumn==pIdx->pTable->iPKey ){
133239 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
133240 pScan->zCollName = pIdx->azColl[j];
133245 pScan->opMask = opMask;
133246 pScan->k = 0;
133247 pScan->aiCur[0] = iCur;
133248 pScan->aiColumn[0] = iColumn;
133249 pScan->nEquiv = 1;
133250 pScan->iEquiv = 1;
133261 ** Search for terms matching the iColumn-th column of pIdx
133262 ** rather than the iColumn-th column of table iCur.
133266 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
133273 ** then try for the one with no dependencies on <expr> - in other words where
133276 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
133294 if( (p->prereqRight & notReady)==0 ){
133295 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
133296 testcase( p->eOperator & WO_IS );
133307 ** This function searches pList for an entry that matches the iCol-th column
133310 ** If such an expression is found, its index in pList->a[] is returned. If
133311 ** no expression is found, -1 is returned.
133321 const char *zColl = pIdx->azColl[iCol];
133323 for(i=0; i<pList->nExpr; i++){
133324 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
133325 if( p->op==TK_COLUMN
133326 && p->iColumn==pIdx->aiColumn[iCol]
133327 && p->iTable==iBase
133329 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
133330 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
133336 return -1;
133340 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
133345 assert( iCol>=0 && iCol<pIdx->nColumn );
133346 j = pIdx->aiColumn[iCol];
133348 return pIdx->pTable->aCol[j].notNull;
133349 }else if( j==(-1) ){
133352 assert( j==(-2) );
133359 ** Return true if the DISTINCT expression-list passed as the third argument
133363 ** DISTINCT list are collectively unique and individually non-null.
133376 /* If there is more than one table or sub-select in the FROM clause of
133379 if( pTabList->nSrc!=1 ) return 0;
133380 iBase = pTabList->a[0].iCursor;
133381 pTab = pTabList->a[0].pTab;
133384 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
133385 ** current SELECT is a correlated sub-query.
133387 for(i=0; i<pDistinct->nExpr; i++){
133388 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
133389 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
133400 ** comparison and select-list expressions must match those of the index.
133405 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133407 for(i=0; i<pIdx->nKeyCol; i++){
133413 if( i==pIdx->nKeyCol ){
133427 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
133434 ** opcodes into OP_Copy when the table is being accessed via co-routine
133438 ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
133447 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */
133449 Vdbe *v = pParse->pVdbe;
133452 if( pParse->db->mallocFailed ) return;
133454 if( pOp->p1!=iTabCur ) continue;
133455 if( pOp->opcode==OP_Column ){
133456 pOp->opcode = OP_Copy;
133457 pOp->p1 = pOp->p2 + iRegister;
133458 pOp->p2 = pOp->p3;
133459 pOp->p3 = 0;
133460 }else if( pOp->opcode==OP_Rowid ){
133463 pOp->opcode = OP_AddImm;
133464 pOp->p1 = pOp->p2;
133465 pOp->p2 = 1;
133467 pOp->opcode = OP_Null;
133468 pOp->p1 = 0;
133469 pOp->p3 = 0;
133479 ** are no-ops.
133485 for(i=0; i<p->nConstraint; i++){
133488 p->aConstraint[i].iColumn,
133489 p->aConstraint[i].iTermOffset,
133490 p->aConstraint[i].op,
133491 p->aConstraint[i].usable);
133493 for(i=0; i<p->nOrderBy; i++){
133496 p->aOrderBy[i].iColumn,
133497 p->aOrderBy[i].desc);
133503 for(i=0; i<p->nConstraint; i++){
133506 p->aConstraintUsage[i].argvIndex,
133507 p->aConstraintUsage[i].omit);
133509 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
133510 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
133511 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
133512 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
133513 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
133532 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
133533 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
133534 if( (pSrc->fg.jointype & JT_LEFT)
133535 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
133536 && (pTerm->eOperator & WO_IS)
133543 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
133544 if( pTerm->u.leftColumn<0 ) return 0;
133545 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
133546 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
133547 testcase( pTerm->pExpr->op==TK_IS );
133568 WhereTerm *pWCEnd; /* End of pWC->a[] */
133577 int mxBitCol; /* Maximum column in pSrc->colUsed */
133592 v = pParse->pVdbe;
133599 pTable = pSrc->pTab;
133600 pWCEnd = &pWC->a[pWC->nTerm];
133601 pLoop = pLevel->pWLoop;
133603 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
133604 Expr *pExpr = pTerm->pExpr;
133605 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
133606 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
133607 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
133608 if( pLoop->prereq==0
133609 && (pTerm->wtFlags & TERM_VIRTUAL)==0
133611 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
133612 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
133613 sqlite3ExprDup(pParse->db, pExpr, 0));
133616 int iCol = pTerm->u.leftColumn;
133617 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
133619 testcase( iCol==BMS-1 );
133622 "automatic index on %s(%s)", pTable->zName,
133623 pTable->aCol[iCol].zName);
133627 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
133630 pLoop->aLTerm[nKeyCol++] = pTerm;
133636 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
133637 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
133648 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
133649 mxBitCol = MIN(BMS-1,pTable->nCol);
133650 testcase( pTable->nCol==BMS-1 );
133651 testcase( pTable->nCol==BMS-2 );
133655 if( pSrc->colUsed & MASKBIT(BMS-1) ){
133656 nKeyCol += pTable->nCol - BMS + 1;
133660 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
133662 pLoop->u.btree.pIndex = pIdx;
133663 pIdx->zName = "auto-index";
133664 pIdx->pTable = pTable;
133667 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
133669 int iCol = pTerm->u.leftColumn;
133670 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
133671 testcase( iCol==BMS-1 );
133674 Expr *pX = pTerm->pExpr;
133676 pIdx->aiColumn[n] = pTerm->u.leftColumn;
133677 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
133678 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
133683 assert( (u32)n==pLoop->u.btree.nEq );
133689 pIdx->aiColumn[n] = i;
133690 pIdx->azColl[n] = sqlite3StrBINARY;
133694 if( pSrc->colUsed & MASKBIT(BMS-1) ){
133695 for(i=BMS-1; i<pTable->nCol; i++){
133696 pIdx->aiColumn[n] = i;
133697 pIdx->azColl[n] = sqlite3StrBINARY;
133702 pIdx->aiColumn[n] = XN_ROWID;
133703 pIdx->azColl[n] = sqlite3StrBINARY;
133706 assert( pLevel->iIdxCur>=0 );
133707 pLevel->iIdxCur = pParse->nTab++;
133708 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
133710 VdbeComment((v, "for %s", pTable->zName));
133714 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
133715 if( pTabItem->fg.viaCoroutine ){
133716 int regYield = pTabItem->regReturn;
133718 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
133721 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
133723 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
133728 pLoop->wsFlags |= WHERE_PARTIALIDX;
133732 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
133734 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
133737 if( pTabItem->fg.viaCoroutine ){
133739 testcase( pParse->db->mallocFailed );
133740 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
133741 pTabItem->regResult, 1);
133743 pTabItem->fg.viaCoroutine = 0;
133745 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
133756 sqlite3ExprDelete(pParse->db, pPartial);
133787 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
133788 if( pTerm->leftCursor != pSrc->iCursor ) continue;
133789 if( pTerm->prereqRight & mUnusable ) continue;
133790 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
133791 testcase( pTerm->eOperator & WO_IN );
133792 testcase( pTerm->eOperator & WO_ISNULL );
133793 testcase( pTerm->eOperator & WO_IS );
133794 testcase( pTerm->eOperator & WO_ALL );
133795 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
133796 if( pTerm->wtFlags & TERM_VNULL ) continue;
133797 assert( pTerm->u.leftColumn>=(-1) );
133807 int n = pOrderBy->nExpr;
133809 Expr *pExpr = pOrderBy->a[i].pExpr;
133810 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
133819 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
133836 *(int*)&pIdxInfo->nConstraint = nTerm;
133837 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
133838 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
133839 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
133840 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
133843 pHidden->pWC = pWC;
133844 pHidden->pParse = pParse;
133845 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
133847 if( pTerm->leftCursor != pSrc->iCursor ) continue;
133848 if( pTerm->prereqRight & mUnusable ) continue;
133849 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
133850 testcase( pTerm->eOperator & WO_IN );
133851 testcase( pTerm->eOperator & WO_IS );
133852 testcase( pTerm->eOperator & WO_ISNULL );
133853 testcase( pTerm->eOperator & WO_ALL );
133854 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
133855 if( pTerm->wtFlags & TERM_VNULL ) continue;
133856 assert( pTerm->u.leftColumn>=(-1) );
133857 pIdxCons[j].iColumn = pTerm->u.leftColumn;
133859 op = pTerm->eOperator & WO_ALL;
133862 pIdxCons[j].op = pTerm->eMatchOp;
133879 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
133882 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
133893 Expr *pExpr = pOrderBy->a[i].pExpr;
133894 pIdxOrderBy[i].iColumn = pExpr->iColumn;
133895 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
133909 ** non-zero value is returned. Otherwise, 0 is returned and the output
133913 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
133917 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
133921 rc = pVtab->pModule->xBestIndex(pVtab, p);
133926 sqlite3OomFault(pParse->db);
133927 }else if( !pVtab->zErrMsg ){
133930 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
133933 sqlite3_free(pVtab->zErrMsg);
133934 pVtab->zErrMsg = 0;
133939 for(i=0; i<p->nConstraint; i++){
133940 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
133942 "table %s: xBestIndex returned an invalid plan", pTab->zName);
133947 return pParse->nErr;
133961 ** into the aSample[] array - it is an index into a virtual set of samples
133972 IndexSample *aSample = pIdx->aSample;
133986 assert( pIdx->nSample>0 );
133987 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
134033 nField = pRec->nField;
134035 iSample = pIdx->nSample * nField;
134047 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
134053 pRec->nField = n;
134056 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
134059 iLower = aSample[iSamp].anLt[n-1];
134061 res = -1;
134064 iCol = n-1;
134073 if( pParse->db->mallocFailed==0 ){
134076 assert( i<pIdx->nSample );
134077 assert( iCol==nField-1 );
134078 pRec->nField = nField;
134080 || pParse->db->mallocFailed
134083 /* Unless i==pIdx->nSample, indicating that pRec is larger than
134086 assert( i<=pIdx->nSample && i>=0 );
134087 pRec->nField = iCol+1;
134088 assert( i==pIdx->nSample
134090 || pParse->db->mallocFailed );
134095 ** If (i>0), then pRec must also be greater than sample (i-1). */
134097 pRec->nField = iCol;
134099 || pParse->db->mallocFailed );
134102 pRec->nField = nField;
134103 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
134104 || pParse->db->mallocFailed );
134112 assert( iCol==nField-1 );
134117 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
134120 if( i>=pIdx->nSample ){
134121 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
134129 iGap = iUpper - iLower;
134137 aStat[1] = pIdx->aAvgEq[nField-1];
134140 /* Restore the pRec->nField value before returning. */
134141 pRec->nField = nField;
134160 if( pTerm->truthProb<=0 ){
134161 nRet += pTerm->truthProb;
134162 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
134163 nRet -= 20; assert( 20==sqlite3LogEst(4) );
134175 assert( iCol>=0 && iCol<pIdx->nColumn );
134176 if( !pIdx->zColAff ){
134179 return pIdx->zColAff[iCol];
134187 ** range-scan on a skip-scan index. For example:
134192 ** Value pLoop->nOut is currently set to the estimated number of rows
134203 ** N is the total number of samples, the pLoop->nOut value is adjusted
134206 ** nOut = nOut * ( min(U - L, 1) / N )
134227 Index *p = pLoop->u.btree.pIndex;
134228 int nEq = pLoop->u.btree.nEq;
134229 sqlite3 *db = pParse->db;
134230 int nLower = -1;
134231 int nUpper = p->nSample+1;
134240 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
134242 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
134246 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
134247 nUpper = p2 ? 0 : p->nSample;
134253 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
134254 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
134264 nDiff = (nUpper - nLower);
134273 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
134274 pLoop->nOut -= nAdjust;
134276 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
134277 nLower, nUpper, nAdjust*-1, pLoop->nOut));
134307 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
134315 ** left-most column of the index). Or, if the query is:
134340 int nOut = pLoop->nOut;
134344 Index *p = pLoop->u.btree.pIndex;
134345 int nEq = pLoop->u.btree.nEq;
134347 if( p->nSample>0 && nEq<p->nSampleCol ){
134348 if( nEq==pBuilder->nRecValid ){
134349 UnpackedRecord *pRec = pBuilder->pRec;
134351 int nBtm = pLoop->u.btree.nBtm;
134352 int nTop = pLoop->u.btree.nTop;
134357 ** key-prefix formed by the nEq values matched against the nEq left-most
134371 ** The number of rows between the two bounds is then just iUpper-iLower.
134375 int iLwrIdx = -2; /* aSample[] for the lower bound */
134376 int iUprIdx = -1; /* aSample[] for the upper bound */
134379 testcase( pRec->nField!=pBuilder->nRecValid );
134380 pRec->nField = pBuilder->nRecValid;
134385 iUpper = p->nRowEst0;
134387 /* Note: this call could be optimized away - since the same values must
134394 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
134395 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
134396 assert( p->aSortOrder!=0 );
134397 if( p->aSortOrder[nEq] ){
134406 Expr *pExpr = pLower->pExpr->pRight;
134413 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
134415 nOut--;
134423 Expr *pExpr = pUpper->pExpr->pRight;
134430 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
134432 nOut--;
134437 pBuilder->pRec = pRec;
134440 nNew = sqlite3LogEst(iUpper - iLower);
134445 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
134466 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
134471 ** has an application-defined likelihood(), assume the range is
134472 ** reduced by an additional 75%. This means that, by default, an open-ended
134476 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
134477 nNew -= 20;
134480 nOut -= (pLower!=0) + (pUpper!=0);
134484 if( pLoop->nOut>nOut ){
134486 pLoop->nOut, nOut));
134489 pLoop->nOut = (LogEst)nOut;
134497 ** the histogram data. This only works when x is the left-most
134504 ** non-zero.
134517 Index *p = pBuilder->pNew->u.btree.pIndex;
134518 int nEq = pBuilder->pNew->u.btree.nEq;
134519 UnpackedRecord *pRec = pBuilder->pRec;
134525 assert( nEq<=p->nColumn );
134526 assert( p->aSample!=0 );
134527 assert( p->nSample>0 );
134528 assert( pBuilder->nRecValid<nEq );
134532 if( pBuilder->nRecValid<(nEq-1) ){
134538 if( nEq>=p->nColumn ){
134543 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
134544 pBuilder->pRec = pRec;
134547 pBuilder->nRecValid = nEq;
134551 p->zName, nEq-1, (int)a[1]));
134561 ** an IN constraint where the right-hand side of the IN operator
134568 ** non-zero.
134581 Index *p = pBuilder->pNew->u.btree.pIndex;
134582 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
134583 int nRecValid = pBuilder->nRecValid;
134589 assert( p->aSample!=0 );
134590 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
134592 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
134594 pBuilder->nRecValid = nRecValid;
134602 assert( pBuilder->nRecValid==nRecValid );
134614 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
134619 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
134620 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
134621 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
134622 if( pTerm->eOperator & WO_SINGLE ){
134624 pTerm->leftCursor, pTerm->u.leftColumn);
134625 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
134627 pTerm->u.pOrInfo->indexable);
134629 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
134632 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
134633 iTerm, pTerm, zType, zLeft, pTerm->truthProb,
134634 pTerm->eOperator, pTerm->wtFlags);
134635 if( pTerm->iField ){
134636 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
134640 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
134651 for(i=0; i<pWC->nTerm; i++){
134652 whereTermPrint(&pWC->a[i], i);
134662 WhereInfo *pWInfo = pWC->pWInfo;
134663 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
134664 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
134665 Table *pTab = pItem->pTab;
134666 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
134667 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
134668 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
134670 pItem->zAlias ? pItem->zAlias : pTab->zName);
134671 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
134673 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
134675 int i = sqlite3Strlen30(zName) - 1;
134676 while( zName[i]!='_' ) i--;
134679 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
134685 if( p->u.vtab.idxStr ){
134687 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
134689 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
134691 sqlite3DebugPrintf(" %-19s", z);
134694 if( p->wsFlags & WHERE_SKIPSCAN ){
134695 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
134697 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
134699 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
134700 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
134702 for(i=0; i<p->nLTerm; i++){
134703 whereTermPrint(p->aLTerm[i], i);
134714 p->aLTerm = p->aLTermSpace;
134715 p->nLTerm = 0;
134716 p->nLSlot = ArraySize(p->aLTermSpace);
134717 p->wsFlags = 0;
134724 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
134725 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
134726 sqlite3_free(p->u.vtab.idxStr);
134727 p->u.vtab.needFree = 0;
134728 p->u.vtab.idxStr = 0;
134729 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
134730 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
134731 sqlite3DbFreeNN(db, p->u.btree.pIndex);
134732 p->u.btree.pIndex = 0;
134741 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
134747 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
134751 if( p->nLSlot>=n ) return SQLITE_OK;
134753 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
134755 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
134756 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
134757 p->aLTerm = paNew;
134758 p->nLSlot = n;
134767 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
134768 memset(&pTo->u, 0, sizeof(pTo->u));
134772 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
134773 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
134774 pFrom->u.vtab.needFree = 0;
134775 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
134776 pFrom->u.btree.pIndex = 0;
134795 for(i=0; i<pWInfo->nLevel; i++){
134796 WhereLevel *pLevel = &pWInfo->a[i];
134797 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
134798 sqlite3DbFree(db, pLevel->u.in.aInLoop);
134801 sqlite3WhereClauseClear(&pWInfo->sWC);
134802 while( pWInfo->pLoops ){
134803 WhereLoop *p = pWInfo->pLoops;
134804 pWInfo->pLoops = p->pNextLoop;
134823 ** was added because if X uses skip-scan less than Y it still might
134826 ** than a non-covering index even if it is a proper subset.
134833 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
134836 if( pY->nSkip > pX->nSkip ) return 0;
134837 if( pX->rRun >= pY->rRun ){
134838 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
134839 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
134841 for(i=pX->nLTerm-1; i>=0; i--){
134842 if( pX->aLTerm[i]==0 ) continue;
134843 for(j=pY->nLTerm-1; j>=0; j--){
134844 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
134848 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
134849 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
134870 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
134871 for(; p; p=p->pNextLoop){
134872 if( p->iTab!=pTemplate->iTab ) continue;
134873 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
134878 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
134879 pTemplate->rRun = p->rRun;
134880 pTemplate->nOut = p->nOut - 1;
134885 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
134886 pTemplate->rRun = p->rRun;
134887 pTemplate->nOut = p->nOut + 1;
134911 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
134912 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
134921 assert( p->rSetup==0 || pTemplate->rSetup==0
134922 || p->rSetup==pTemplate->rSetup );
134926 ** rSetup. Call this SETUP-INVARIANT */
134927 assert( p->rSetup>=pTemplate->rSetup );
134929 /* Any loop using an appliation-defined index (or PRIMARY KEY or
134931 ** than an automatic index. Unless it is a skip-scan. */
134932 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
134933 && (pTemplate->nSkip)==0
134934 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
134935 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
134936 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
134946 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
134947 && p->rSetup<=pTemplate->rSetup /* (2a) */
134948 && p->rRun<=pTemplate->rRun /* (2b) */
134949 && p->nOut<=pTemplate->nOut /* (2c) */
134959 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
134960 && p->rRun>=pTemplate->rRun /* (2a) */
134961 && p->nOut>=pTemplate->nOut /* (2b) */
134963 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
134979 ** If pBuilder->pOrSet is not NULL then we care about only the
134981 ** information is gathered in the pBuilder->pOrSet object. This special
134984 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
134996 WhereInfo *pWInfo = pBuilder->pWInfo;
134997 sqlite3 *db = pWInfo->pParse->db;
135000 /* If pBuilder->pOrSet is defined, then only keep track of the costs
135003 if( pBuilder->pOrSet!=0 ){
135004 if( pTemplate->nLTerm ){
135006 u16 n = pBuilder->pOrSet->n;
135009 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
135010 pTemplate->nOut);
135013 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
135014 whereLoopPrint(pTemplate, pBuilder->pWC);
135023 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
135024 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
135032 whereLoopPrint(pTemplate, pBuilder->pWC);
135048 whereLoopPrint(p, pBuilder->pWC);
135053 whereLoopPrint(pTemplate, pBuilder->pWC);
135061 p->pNextLoop = 0;
135066 WhereLoop **ppTail = &p->pNextLoop;
135073 *ppTail = pToDel->pNextLoop;
135077 whereLoopPrint(pToDel, pBuilder->pWC);
135084 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
135085 Index *pIndex = p->u.btree.pIndex;
135086 if( pIndex && pIndex->tnum==0 ){
135087 p->u.btree.pIndex = 0;
135106 ** TODO --> Perhaps this is something that could be improved by better
135110 ** value corresponds to -1 in LogEst notation, so this means decrement
135117 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
135118 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
135128 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
135130 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
135132 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
135133 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
135134 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
135135 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
135136 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
135137 for(j=pLoop->nLTerm-1; j>=0; j--){
135138 pX = pLoop->aLTerm[j];
135141 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
135144 if( pTerm->truthProb<=0 ){
135147 pLoop->nOut += pTerm->truthProb;
135151 pLoop->nOut--;
135152 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
135153 Expr *pRight = pTerm->pExpr->pRight;
135154 testcase( pTerm->pExpr->op==TK_IS );
135155 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
135165 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
135192 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
135195 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
135202 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
135203 Expr *pRhs = pTerm->pExpr->pRight;
135204 if( pRhs->flags & EP_xIsSelect ){
135205 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
135207 pRhs = pRhs->x.pList->a[i].pExpr;
135214 if( pLhs->op!=TK_COLUMN
135215 || pLhs->iTable!=iCur
135216 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
135217 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
135222 testcase( pLhs->iColumn==XN_ROWID );
135224 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
135229 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
135236 ** compiled with -DSQLITE_ENABLE_COSTMULT
135245 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
135248 ** When this function is called, pBuilder->pNew->nOut contains the
135253 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
135262 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
135263 Parse *pParse = pWInfo->pParse; /* Parsing context */
135264 sqlite3 *db = pParse->db; /* Database connection malloc context */
135269 Bitmask saved_prereq; /* Original value of pNew->prereq */
135270 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
135271 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
135272 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
135273 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
135274 u16 saved_nSkip; /* Original value of pNew->nSkip */
135275 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
135276 LogEst saved_nOut; /* Original value of pNew->nOut */
135282 pNew = pBuilder->pNew;
135283 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
135285 pProbe->zName, pNew->u.btree.nEq));
135287 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
135288 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
135289 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
135292 assert( pNew->u.btree.nBtm==0 );
135295 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
135297 assert( pNew->u.btree.nEq<pProbe->nColumn );
135299 saved_nEq = pNew->u.btree.nEq;
135300 saved_nBtm = pNew->u.btree.nBtm;
135301 saved_nTop = pNew->u.btree.nTop;
135302 saved_nSkip = pNew->nSkip;
135303 saved_nLTerm = pNew->nLTerm;
135304 saved_wsFlags = pNew->wsFlags;
135305 saved_prereq = pNew->prereq;
135306 saved_nOut = pNew->nOut;
135307 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
135309 pNew->rSetup = 0;
135310 rSize = pProbe->aiRowLogEst[0];
135313 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
135318 int nRecValid = pBuilder->nRecValid;
135320 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
135325 if( pTerm->prereqRight & pNew->maskSelf ) continue;
135329 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
135334 if( (pSrc->fg.jointype & JT_LEFT)!=0
135335 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
135343 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
135344 pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
135346 pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
135348 pNew->wsFlags = saved_wsFlags;
135349 pNew->u.btree.nEq = saved_nEq;
135350 pNew->u.btree.nBtm = saved_nBtm;
135351 pNew->u.btree.nTop = saved_nTop;
135352 pNew->nLTerm = saved_nLTerm;
135353 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
135354 pNew->aLTerm[pNew->nLTerm++] = pTerm;
135355 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
135358 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
135359 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
135360 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
135364 Expr *pExpr = pTerm->pExpr;
135365 pNew->wsFlags |= WHERE_COLUMN_IN;
135376 for(i=0; i<pNew->nLTerm-1; i++){
135377 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
135379 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
135381 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
135386 int iCol = pProbe->aiColumn[saved_nEq];
135387 pNew->wsFlags |= WHERE_COLUMN_EQ;
135388 assert( saved_nEq==pNew->u.btree.nEq );
135390 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135392 if( iCol>=0 && pProbe->uniqNotNull==0 ){
135393 pNew->wsFlags |= WHERE_UNQ_WANTED;
135395 pNew->wsFlags |= WHERE_ONEROW;
135399 pNew->wsFlags |= WHERE_COLUMN_NULL;
135403 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
135404 pNew->u.btree.nBtm = whereRangeVectorLen(
135405 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
135409 if( pTerm->wtFlags & TERM_LIKEOPT ){
135413 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
135414 assert( pTop->wtFlags & TERM_LIKEOPT );
135415 assert( pTop->eOperator==WO_LT );
135416 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
135417 pNew->aLTerm[pNew->nLTerm++] = pTop;
135418 pNew->wsFlags |= WHERE_TOP_LIMIT;
135419 pNew->u.btree.nTop = 1;
135425 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
135426 pNew->u.btree.nTop = whereRangeVectorLen(
135427 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
135430 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
135431 pNew->aLTerm[pNew->nLTerm-2] : 0;
135434 /* At this point pNew->nOut is set to the number of rows expected to
135438 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
135439 assert( pNew->nOut==saved_nOut );
135440 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
135445 int nEq = ++pNew->u.btree.nEq;
135448 assert( pNew->nOut==saved_nOut );
135449 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
135452 pNew->nOut += pTerm->truthProb;
135453 pNew->nOut -= nIn;
135458 && pProbe->nSample
135459 && pNew->u.btree.nEq<=pProbe->nSampleCol
135460 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
135462 Expr *pExpr = pTerm->pExpr;
135467 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
135469 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
135474 pNew->nOut = sqlite3LogEst(nOut);
135475 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
135476 pNew->nOut -= nIn;
135482 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
135487 pNew->nOut += 10;
135494 ** it to pNew->rRun, which is currently set to the cost of the index
135495 ** seek only. Then, if this is a non-covering index, add the cost of
135497 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
135498 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
135499 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
135500 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
135502 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
135504 nOutUnadjusted = pNew->nOut;
135505 pNew->rRun += nInMul + nIn;
135506 pNew->nOut += nInMul + nIn;
135507 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
135510 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
135511 pNew->nOut = saved_nOut;
135513 pNew->nOut = nOutUnadjusted;
135516 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
135517 && pNew->u.btree.nEq<pProbe->nColumn
135521 pNew->nOut = saved_nOut;
135523 pBuilder->nRecValid = nRecValid;
135526 pNew->prereq = saved_prereq;
135527 pNew->u.btree.nEq = saved_nEq;
135528 pNew->u.btree.nBtm = saved_nBtm;
135529 pNew->u.btree.nTop = saved_nTop;
135530 pNew->nSkip = saved_nSkip;
135531 pNew->wsFlags = saved_wsFlags;
135532 pNew->nOut = saved_nOut;
135533 pNew->nLTerm = saved_nLTerm;
135535 /* Consider using a skip-scan if there are no WHERE clause constraints
135536 ** available for the left-most terms of the index, and if the average
135537 ** number of repeats in the left-most terms is at least 18.
135547 && saved_nEq+1<pProbe->nKeyCol
135548 && pProbe->noSkipScan==0
135549 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
135550 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
135553 pNew->u.btree.nEq++;
135554 pNew->nSkip++;
135555 pNew->aLTerm[pNew->nLTerm++] = 0;
135556 pNew->wsFlags |= WHERE_SKIPSCAN;
135557 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
135558 pNew->nOut -= nIter;
135559 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
135560 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
135563 pNew->nOut = saved_nOut;
135564 pNew->u.btree.nEq = saved_nEq;
135565 pNew->nSkip = saved_nSkip;
135566 pNew->wsFlags = saved_wsFlags;
135570 pProbe->zName, saved_nEq, rc));
135591 if( pIndex->bUnordered ) return 0;
135592 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
135593 for(ii=0; ii<pOB->nExpr; ii++){
135594 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
135595 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
135596 if( pExpr->iColumn<0 ) return 1;
135597 for(jj=0; jj<pIndex->nKeyCol; jj++){
135598 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
135600 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
135601 for(jj=0; jj<pIndex->nKeyCol; jj++){
135602 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
135603 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
135619 for(j=pIdx->nColumn-1; j>=0; j--){
135620 int x = pIdx->aiColumn[j];
135622 testcase( x==BMS-1 );
135623 testcase( x==BMS-2 );
135624 if( x<BMS-1 ) m |= MASKBIT(x);
135636 Parse *pParse = pWC->pWInfo->pParse;
135637 while( pWhere->op==TK_AND ){
135638 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
135639 pWhere = pWhere->pRight;
135641 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
135642 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
135643 Expr *pExpr = pTerm->pExpr;
135644 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
135655 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
135656 ** a b-tree table, not a virtual table.
135658 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
135663 ** cost = nRow * 3.0 // full-table scan
135665 ** cost = nRow * (K+3.0) // scan of non-covering index
135672 ** the index b-tree:
135675 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
135679 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
135684 ** log(nRow) factor is omitted from a non-covering index scan in order to
135685 ** bias the scoring in favor of using an index, since the worst-case
135686 ** performance of using an index is far better than the worst-case performance
135697 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
135709 pNew = pBuilder->pNew;
135710 pWInfo = pBuilder->pWInfo;
135711 pTabList = pWInfo->pTabList;
135712 pSrc = pTabList->a + pNew->iTab;
135713 pTab = pSrc->pTab;
135714 pWC = pBuilder->pWC;
135715 assert( !IsVirtual(pSrc->pTab) );
135717 if( pSrc->pIBIndex ){
135719 pProbe = pSrc->pIBIndex;
135721 pProbe = pTab->pIndex;
135735 sPk.szIdxRow = pTab->szTabRow;
135736 aiRowEstPk[0] = pTab->nRowLogEst;
135738 pFirst = pSrc->pTab->pIndex;
135739 if( pSrc->fg.notIndexed==0 ){
135746 rSize = pTab->nRowLogEst;
135751 if( !pBuilder->pOrSet /* Not part of an OR optimization */
135752 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
135753 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
135754 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
135755 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
135757 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
135758 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
135760 /* Generate auto-index WhereLoops */
135762 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
135763 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
135764 if( pTerm->prereqRight & pNew->maskSelf ) continue;
135766 pNew->u.btree.nEq = 1;
135767 pNew->nSkip = 0;
135768 pNew->u.btree.pIndex = 0;
135769 pNew->nLTerm = 1;
135770 pNew->aLTerm[0] = pTerm;
135771 /* TUNING: One-time cost for computing the automatic index is
135779 pNew->rSetup = rLogSize + rSize + 4;
135780 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
135781 pNew->rSetup += 24;
135783 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
135784 if( pNew->rSetup<0 ) pNew->rSetup = 0;
135789 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
135790 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
135791 pNew->wsFlags = WHERE_AUTO_INDEX;
135792 pNew->prereq = mPrereq | pTerm->prereqRight;
135802 pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
135804 if( pProbe->pPartIdxWhere!=0
135805 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
135806 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
135809 if( pProbe->bNoQuery ) continue;
135810 rSize = pProbe->aiRowLogEst[0];
135811 pNew->u.btree.nEq = 0;
135812 pNew->u.btree.nBtm = 0;
135813 pNew->u.btree.nTop = 0;
135814 pNew->nSkip = 0;
135815 pNew->nLTerm = 0;
135816 pNew->iSortIdx = 0;
135817 pNew->rSetup = 0;
135818 pNew->prereq = mPrereq;
135819 pNew->nOut = rSize;
135820 pNew->u.btree.pIndex = pProbe;
135821 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
135823 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
135824 if( pProbe->tnum<=0 ){
135826 pNew->wsFlags = WHERE_IPK;
135829 pNew->iSortIdx = b ? iSortIdx : 0;
135831 pNew->rRun = rSize + 16;
135832 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
135835 pNew->nOut = rSize;
135839 if( pProbe->isCovering ){
135840 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
135843 m = pSrc->colUsed & ~columnsInIndex(pProbe);
135844 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
135850 || pProbe->pPartIdxWhere!=0
135852 && pProbe->bUnordered==0
135853 && (pProbe->szIdxRow<pTab->szTabRow)
135854 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
135856 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
135859 pNew->iSortIdx = b ? iSortIdx : 0;
135864 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
135866 /* If this is a non-covering index scan, add in the cost of
135873 int iCur = pSrc->iCursor;
135874 WhereClause *pWC2 = &pWInfo->sWC;
135875 for(ii=0; ii<pWC2->nTerm; ii++){
135876 WhereTerm *pTerm = &pWC2->a[ii];
135877 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
135882 if( pTerm->truthProb<=0 ){
135883 nLookup += pTerm->truthProb;
135885 nLookup--;
135886 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
135890 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
135892 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
135895 pNew->nOut = rSize;
135900 pBuilder->bldFlags = 0;
135902 if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
135903 /* If a non-unique index is used, or if a prefix of the key for
135904 ** unique index is used (making the index functionally non-unique)
135907 pTab->tabFlags |= TF_StatsUsed;
135910 sqlite3Stat4ProbeFree(pBuilder->pRec);
135911 pBuilder->nRecValid = 0;
135912 pBuilder->pRec = 0;
135922 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
135949 WhereClause *pWC = pBuilder->pWC;
135951 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
135955 WhereLoop *pNew = pBuilder->pNew;
135956 Parse *pParse = pBuilder->pWInfo->pParse;
135957 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
135958 int nConstraint = pIdxInfo->nConstraint;
135962 pNew->prereq = mPrereq;
135966 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
135968 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
135969 pIdxCons->usable = 0;
135970 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
135971 && (pTerm->eOperator & mExclude)==0
135973 pIdxCons->usable = 1;
135979 assert( pIdxInfo->needToFreeIdxStr==0 );
135980 pIdxInfo->idxStr = 0;
135981 pIdxInfo->idxNum = 0;
135982 pIdxInfo->orderByConsumed = 0;
135983 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
135984 pIdxInfo->estimatedRows = 25;
135985 pIdxInfo->idxFlags = 0;
135986 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
135989 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
135992 mxTerm = -1;
135993 assert( pNew->nLSlot>=nConstraint );
135994 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
135995 pNew->u.vtab.omitMask = 0;
135996 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
135999 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
136001 int j = pIdxCons->iTermOffset;
136004 || j>=pWC->nTerm
136005 || pNew->aLTerm[iTerm]!=0
136006 || pIdxCons->usable==0
136009 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
136012 testcase( iTerm==nConstraint-1 );
136014 testcase( j==pWC->nTerm-1 );
136015 pTerm = &pWC->a[j];
136016 pNew->prereq |= pTerm->prereqRight;
136017 assert( iTerm<pNew->nLSlot );
136018 pNew->aLTerm[iTerm] = pTerm;
136022 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
136023 if( (pTerm->eOperator & WO_IN)!=0 ){
136029 pIdxInfo->orderByConsumed = 0;
136030 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
136035 pNew->u.vtab.omitMask &= ~mNoOmit;
136037 pNew->nLTerm = mxTerm+1;
136038 assert( pNew->nLTerm<=pNew->nLSlot );
136039 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
136040 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
136041 pIdxInfo->needToFreeIdxStr = 0;
136042 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
136043 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
136044 pIdxInfo->nOrderBy : 0);
136045 pNew->rSetup = 0;
136046 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
136047 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
136051 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
136052 pNew->wsFlags |= WHERE_ONEROW;
136054 pNew->wsFlags &= ~WHERE_ONEROW;
136057 if( pNew->u.vtab.needFree ){
136058 sqlite3_free(pNew->u.vtab.idxStr);
136059 pNew->u.vtab.needFree = 0;
136063 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
136078 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
136080 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
136081 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
136082 if( pX->pLeft ){
136083 pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
136085 zRet = (pC ? pC->zName : "BINARY");
136092 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
136113 ** mUnusable should always be configured as "not-usable" for xBestIndex.
136133 pWInfo = pBuilder->pWInfo;
136134 pParse = pWInfo->pParse;
136135 pWC = pBuilder->pWC;
136136 pNew = pBuilder->pNew;
136137 pSrc = &pWInfo->pTabList->a[pNew->iTab];
136138 assert( IsVirtual(pSrc->pTab) );
136139 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
136142 pNew->rSetup = 0;
136143 pNew->wsFlags = WHERE_VIRTUALTABLE;
136144 pNew->nLTerm = 0;
136145 pNew->u.vtab.needFree = 0;
136146 nConstraint = p->nConstraint;
136147 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
136148 sqlite3DbFree(pParse->db, p);
136161 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
136174 mBestNoIn = pNew->prereq & ~mPrereq;
136189 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
136200 if( pNew->prereq==mPrereq ){
136226 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
136227 sqlite3DbFreeNN(pParse->db, p);
136241 WhereInfo *pWInfo = pBuilder->pWInfo;
136252 pWC = pBuilder->pWC;
136253 pWCEnd = pWC->a + pWC->nTerm;
136254 pNew = pBuilder->pNew;
136256 pItem = pWInfo->pTabList->a + pNew->iTab;
136257 iCur = pItem->iCursor;
136259 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
136260 if( (pTerm->eOperator & WO_OR)!=0
136261 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
136263 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
136264 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
136273 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
136274 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
136275 if( (pOrTerm->eOperator & WO_AND)!=0 ){
136276 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
136277 }else if( pOrTerm->leftCursor==iCur ){
136278 tempWC.pWInfo = pWC->pWInfo;
136289 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
136290 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
136296 if( IsVirtual(pItem->pTab) ){
136326 pNew->nLTerm = 1;
136327 pNew->aLTerm[0] = pTerm;
136328 pNew->wsFlags = WHERE_MULTI_OR;
136329 pNew->rSetup = 0;
136330 pNew->iSortIdx = 0;
136331 memset(&pNew->u, 0, sizeof(pNew->u));
136334 ** of all sub-scans required by the OR-scan. However, due to rounding
136335 ** errors, it may be that the cost of the OR-scan is equal to its
136336 ** most expensive sub-scan. Add the smallest possible penalty
136343 ** the planner may elect to "OR" together a full-table scan and an
136345 pNew->rRun = sSum.a[i].rRun + 1;
136346 pNew->nOut = sSum.a[i].nOut;
136347 pNew->prereq = sSum.a[i].prereq;
136350 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
136360 WhereInfo *pWInfo = pBuilder->pWInfo;
136364 SrcList *pTabList = pWInfo->pTabList;
136366 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
136367 sqlite3 *db = pWInfo->pParse->db;
136373 pNew = pBuilder->pNew;
136375 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
136377 pNew->iTab = iTab;
136378 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
136379 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
136381 ** right-hand-side of a LEFT or CROSS JOIN. */
136384 priorJointype = pItem->fg.jointype;
136386 if( IsVirtual(pItem->pTab) ){
136389 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
136390 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
136402 mPrior |= pNew->maskSelf;
136403 if( rc || db->mallocFailed ) break;
136425 ** pOrderBy terms must be matched in strict left-to-right order.
136432 u16 nLoop, /* Number of entries in pPath->aLoop[] */
136433 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
136439 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
136455 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
136458 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
136462 ** We say the WhereLoop is "one-row" if it generates no more than one
136463 ** row of output. A WhereLoop is one-row if all of the following are true:
136466 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
136467 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
136469 ** We say the WhereLoop is "order-distinct" if the set of columns from
136471 ** row of the WhereLoop. Every one-row WhereLoop is automatically
136472 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
136473 ** is not order-distinct. To be order-distinct is not quite the same as being
136475 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
136476 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
136480 ** automatically order-distinct.
136486 nOrderBy = pOrderBy->nExpr;
136487 testcase( nOrderBy==BMS-1 );
136488 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
136490 obDone = MASKBIT(nOrderBy)-1;
136496 if( iLoop>0 ) ready |= pLoop->maskSelf;
136498 pLoop = pPath->aLoop[iLoop];
136503 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
136504 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
136507 pLoop->u.btree.nIdxCol = 0;
136509 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
136518 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
136519 if( pOBExpr->op!=TK_COLUMN ) continue;
136520 if( pOBExpr->iTable!=iCur ) continue;
136521 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
136524 if( pTerm->eOperator==WO_IN ){
136529 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
136530 if( j>=pLoop->nLTerm ) continue;
136532 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
136533 if( sqlite3ExprCollSeqMatch(pWInfo->pParse,
136534 pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
136537 testcase( pTerm->pExpr->op==TK_IS );
136542 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
136543 if( pLoop->wsFlags & WHERE_IPK ){
136547 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
136550 nKeyCol = pIndex->nKeyCol;
136551 nColumn = pIndex->nColumn;
136552 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
136553 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
136554 || !HasRowid(pIndex->pTable));
136566 assert( j>=pLoop->u.btree.nEq
136567 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
136569 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
136570 u16 eOp = pLoop->aLTerm[j]->eOperator;
136588 ** j<pLoop->u.btree.nEq constraint above. Any equality other
136591 Expr *pX = pLoop->aLTerm[j]->pExpr;
136592 for(i=j+1; i<pLoop->u.btree.nEq; i++){
136593 if( pLoop->aLTerm[i]->pExpr==pX ){
136594 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
136603 ** (revIdx) for the j-th column of the index.
136606 iColumn = pIndex->aiColumn[j];
136607 revIdx = pIndex->aSortOrder[j];
136608 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
136615 ** WhereLoop is not well-ordered
136619 && j>=pLoop->u.btree.nEq
136620 && pIndex->pTable->aCol[iColumn].notNull==0
136625 /* Find the ORDER BY term that corresponds to the j-th column
136631 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
136636 if( pOBExpr->op!=TK_COLUMN ) continue;
136637 if( pOBExpr->iTable!=iCur ) continue;
136638 if( pOBExpr->iColumn!=iColumn ) continue;
136640 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
136646 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
136647 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
136649 pLoop->u.btree.nIdxCol = j+1;
136657 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
136659 rev = revIdx ^ pOrderBy->a[i].sortOrder;
136683 } /* end-if not one-row */
136687 orderDistinctMask |= pLoop->maskSelf;
136692 p = pOrderBy->a[i].pExpr;
136693 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
136700 } /* End the loop over all WhereLoops from outer-most down to inner-most */
136703 for(i=nOrderBy-1; i>0; i--){
136704 Bitmask m = MASKBIT(i) - 1;
136709 return -1;
136716 ** BY clause - and so any order that groups rows as required satisfies the
136733 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
136734 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
136737 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
136738 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
136739 return pWInfo->sorted;
136747 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
136748 if( pLast ) zName[i++] = pLast->cId;
136770 ** Or, if the order-by clause has X terms but only the last Y
136771 ** terms are out of order, then block-sorting will reduce the
136780 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
136785 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
136786 nRow = pWInfo->iLimit;
136793 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
136795 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
136826 pParse = pWInfo->pParse;
136827 db = pParse->db;
136828 nLoop = pWInfo->nLevel;
136830 ** For 2-way joins, the 5 best paths are followed.
136833 assert( nLoop<=pWInfo->pTabList->nSrc );
136834 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
136841 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
136844 nOrderBy = pWInfo->pOrderBy->nExpr;
136856 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
136857 pFrom->aLoop = pX;
136862 ** is either zero - meaning it has not yet been initialized - or the
136877 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
136885 ** -1, indicating that the result set may or may not be ordered,
136887 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
136896 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
136900 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
136902 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
136904 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
136905 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
136906 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
136914 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
136915 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
136916 nOut = pFrom->nRow + pWLoop->nOut;
136917 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
136920 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
136923 revMask = pFrom->revLoop;
136934 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
136935 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
136939 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
136943 ** mxChoice best-so-far paths.
136945 ** First look for an existing path among best-so-far paths
136949 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
136950 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
136951 ** of legal values for isOrdered, -1..64.
136954 if( pTo->maskLoop==maskNew
136955 && ((pTo->isOrdered^isOrdered)&0x80)==0
136957 testcase( jj==nTo-1 );
136962 /* None of the existing best-so-far paths match the candidate. */
136967 ** paths currently in the best-so-far buffer. So discard
136971 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
136979 ** needs to be added to the set of best-so-far paths. */
136990 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
136996 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
137002 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
137004 if( pTo->rCost<rCost
137005 || (pTo->rCost==rCost
137006 && (pTo->nRow<nOut
137007 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
137014 "Skip %s cost=%-3d,%3d,%3d order=%c",
137017 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
137018 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
137019 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
137023 testcase( pTo->rCost==rCost );
137026 testcase( pTo->rCost==rCost+1 );
137032 "Update %s cost=%-3d,%3d,%3d order=%c",
137035 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
137036 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
137037 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
137042 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
137043 pTo->revLoop = revMask;
137044 pTo->nRow = nOut;
137045 pTo->rCost = rCost;
137046 pTo->rUnsorted = rUnsorted;
137047 pTo->isOrdered = isOrdered;
137048 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
137049 pTo->aLoop[iLoop] = pWLoop;
137055 if( pTo->rCost>mxCost
137056 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
137058 mxCost = pTo->rCost;
137059 mxUnsorted = pTo->rUnsorted;
137069 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
137071 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
137072 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
137073 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
137074 if( pTo->isOrdered>0 ){
137075 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
137099 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
137101 assert( pWInfo->nLevel==nLoop );
137104 WhereLevel *pLevel = pWInfo->a + iLoop;
137105 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
137106 pLevel->iFrom = pWLoop->iTab;
137107 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
137109 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
137110 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
137111 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
137115 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
137116 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
137117 if( rc==pWInfo->pResultSet->nExpr ){
137118 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
137121 if( pWInfo->pOrderBy ){
137122 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
137123 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
137124 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
137127 pWInfo->nOBSat = pFrom->isOrdered;
137128 pWInfo->revMask = pFrom->revLoop;
137129 if( pWInfo->nOBSat<=0 ){
137130 pWInfo->nOBSat = 0;
137132 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
137137 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
137138 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
137141 if( rc==pWInfo->pOrderBy->nExpr ){
137142 pWInfo->bOrderedInnerLoop = 1;
137143 pWInfo->revMask = m;
137149 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
137150 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
137153 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
137154 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
137156 assert( pWInfo->sorted==0 );
137157 if( nOrder==pWInfo->pOrderBy->nExpr ){
137158 pWInfo->sorted = 1;
137159 pWInfo->revMask = revMask;
137165 pWInfo->nRowOut = pFrom->nRow;
137176 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
137179 ** Return non-zero on success, if this query can be handled by this
137180 ** no-frills query planner. Return zero if this query needs the
137181 ** general-purpose query planner.
137194 pWInfo = pBuilder->pWInfo;
137195 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
137196 assert( pWInfo->pTabList->nSrc>=1 );
137197 pItem = pWInfo->pTabList->a;
137198 pTab = pItem->pTab;
137200 if( pItem->fg.isIndexedBy ) return 0;
137201 iCur = pItem->iCursor;
137202 pWC = &pWInfo->sWC;
137203 pLoop = pBuilder->pNew;
137204 pLoop->wsFlags = 0;
137205 pLoop->nSkip = 0;
137206 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
137208 testcase( pTerm->eOperator & WO_IS );
137209 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
137210 pLoop->aLTerm[0] = pTerm;
137211 pLoop->nLTerm = 1;
137212 pLoop->u.btree.nEq = 1;
137214 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
137216 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
137218 assert( pLoop->aLTermSpace==pLoop->aLTerm );
137220 || pIdx->pPartIdxWhere!=0
137221 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
137223 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
137224 for(j=0; j<pIdx->nKeyCol; j++){
137227 testcase( pTerm->eOperator & WO_IS );
137228 pLoop->aLTerm[j] = pTerm;
137230 if( j!=pIdx->nKeyCol ) continue;
137231 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
137232 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
137233 pLoop->wsFlags |= WHERE_IDX_ONLY;
137235 pLoop->nLTerm = j;
137236 pLoop->u.btree.nEq = j;
137237 pLoop->u.btree.pIndex = pIdx;
137239 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
137243 if( pLoop->wsFlags ){
137244 pLoop->nOut = (LogEst)1;
137245 pWInfo->a[0].pWLoop = pLoop;
137246 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
137247 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
137248 pWInfo->a[0].iTabCur = iCur;
137249 pWInfo->nRowOut = 1;
137250 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
137251 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
137252 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
137255 pLoop->cId = '0';
137266 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
137267 pWalker->eCode = 0;
137274 ** Return true if the expression contains no non-deterministic SQL
137275 ** functions. Do not consider non-deterministic SQL functions that are
137276 ** part of sub-select statements.
137307 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
137311 ** end |-- by sqlite3WhereEnd()
137318 ** scanning through all values on the right-hand side of the IN.
137321 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
137331 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
137341 ** inner loops (or around the "..." if the test occurs within the inner-
137389 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
137393 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
137410 db = pParse->db;
137414 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
137415 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
137427 testcase( pTabList->nSrc==BMS );
137428 if( pTabList->nSrc>BMS ){
137438 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
137443 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
137444 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
137447 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
137449 if( db->mallocFailed ){
137454 pWInfo->pParse = pParse;
137455 pWInfo->pTabList = pTabList;
137456 pWInfo->pOrderBy = pOrderBy;
137457 pWInfo->pWhere = pWhere;
137458 pWInfo->pResultSet = pResultSet;
137459 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
137460 pWInfo->nLevel = nTabList;
137461 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
137462 pWInfo->wctrlFlags = wctrlFlags;
137463 pWInfo->iLimit = iAuxArg;
137464 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
137465 memset(&pWInfo->nOBSat, 0,
137466 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
137467 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
137468 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
137469 pMaskSet = &pWInfo->sMaskSet;
137471 sWLB.pWC = &pWInfo->sWC;
137476 sWLB.pNew->cId = '*';
137483 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
137484 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
137489 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
137491 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
137496 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
137499 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
137503 ** Note that bitmasks are created for all pTabList->nSrc tables in
137505 ** equal to pTabList->nSrc but might be shortened to 1 if the
137510 createMask(pMaskSet, pTabList->a[ii].iCursor);
137511 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
137512 }while( (++ii)<pTabList->nSrc );
137516 for(ii=0; ii<pTabList->nSrc; ii++){
137517 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
137526 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
137527 if( db->mallocFailed ) goto whereBeginError;
137533 ** Do not do this if the expression contains non-deterministic functions
137534 ** that are not within a sub-select. This is not strictly required, but
137537 ** FROM ... WHERE random()>0; -- eval random() once per row
137538 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
137540 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137541 WhereTerm *pT = &sWLB.pWC->a[ii];
137542 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137543 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137544 pT->wtFlags |= TERM_CODED;
137549 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
137551 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
137554 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
137555 pWInfo->pOrderBy = pResultSet;
137583 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
137584 p->cId = zLabel[i%(sizeof(zLabel)-1)];
137591 if( db->mallocFailed ) goto whereBeginError;
137592 if( pWInfo->pOrderBy ){
137593 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
137594 if( db->mallocFailed ) goto whereBeginError;
137597 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
137598 pWInfo->revMask = ALLBITS;
137600 if( pParse->nErr || NEVER(db->mallocFailed) ){
137605 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
137606 if( pWInfo->nOBSat>0 ){
137607 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
137609 switch( pWInfo->eDistinct ){
137624 for(ii=0; ii<pWInfo->nLevel; ii++){
137625 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
137660 if( pWInfo->nLevel>=2
137669 for(i=pWInfo->nLevel-1; i>=1; i--){
137672 pLoop = pWInfo->a[i].pWLoop;
137673 pItem = &pWInfo->pTabList->a[pLoop->iTab];
137674 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
137676 && (pLoop->wsFlags & WHERE_ONEROW)==0
137680 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
137681 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
137682 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
137683 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
137684 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
137685 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
137692 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
137693 notReady &= ~pLoop->maskSelf;
137694 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
137695 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
137696 pTerm->wtFlags |= TERM_CODED;
137699 if( i!=pWInfo->nLevel-1 ){
137700 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
137701 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
137703 pWInfo->nLevel--;
137704 nTabList--;
137708 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
137711 ** to use a one-pass algorithm, determine if this is appropriate.
137713 ** A one-pass approach can be used if the caller has requested one
137717 ** * the caller has indicated that a one-pass approach can be used
137726 ** use a one-pass approach, and this is not set accurately for scans
137729 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
137731 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
137738 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
137739 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
137743 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
137751 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
137756 pTabItem = &pTabList->a[pLevel->iFrom];
137757 pTab = pTabItem->pTab;
137758 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
137759 pLoop = pLevel->pWLoop;
137760 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
137764 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
137766 int iCur = pTabItem->iCursor;
137772 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
137775 if( pWInfo->eOnePass!=ONEPASS_OFF ){
137777 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
137779 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
137780 assert( pTabItem->iCursor==pLevel->iTabCur );
137781 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
137782 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
137783 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
137784 Bitmask b = pTabItem->colUsed;
137787 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
137788 assert( n<=pTab->nCol );
137791 if( pLoop->u.btree.pIndex!=0 ){
137799 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
137800 (const u8*)&pTabItem->colUsed, P4_INT64);
137803 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
137805 if( pLoop->wsFlags & WHERE_INDEXED ){
137806 Index *pIx = pLoop->u.btree.pIndex;
137810 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
137814 /* This is one term of an OR-optimization using the PRIMARY KEY of a
137816 iIndexCur = pLevel->iTabCur;
137818 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
137819 Index *pJ = pTabItem->pTab->pIndex;
137824 pJ = pJ->pNext;
137827 pWInfo->aiCurOnePass[1] = iIndexCur;
137832 iIndexCur = pParse->nTab++;
137834 pLevel->iIdxCur = iIndexCur;
137835 assert( pIx->pSchema==pTab->pSchema );
137838 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
137840 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
137841 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
137842 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
137843 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
137847 VdbeComment((v, "%s", pIx->zName));
137852 for(ii=0; ii<pIx->nColumn; ii++){
137853 jj = pIx->aiColumn[ii];
137856 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
137867 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
137868 if( db->mallocFailed ) goto whereBeginError;
137877 pLevel = &pWInfo->a[ii];
137878 wsFlags = pLevel->pWLoop->wsFlags;
137880 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
137881 constructAutomaticIndex(pParse, &pWInfo->sWC,
137882 &pTabList->a[pLevel->iFrom], notReady, pLevel);
137883 if( db->mallocFailed ) goto whereBeginError;
137887 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
137889 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
137891 pWInfo->iContinue = pLevel->addrCont;
137898 VdbeModuleComment((v, "Begin WHERE-core"));
137904 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
137915 Parse *pParse = pWInfo->pParse;
137916 Vdbe *v = pParse->pVdbe;
137920 SrcList *pTabList = pWInfo->pTabList;
137921 sqlite3 *db = pParse->db;
137925 VdbeModuleComment((v, "End WHERE-core"));
137927 for(i=pWInfo->nLevel-1; i>=0; i--){
137929 pLevel = &pWInfo->a[i];
137930 pLoop = pLevel->pWLoop;
137931 if( pLevel->op!=OP_Noop ){
137936 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
137937 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
137938 && (pLoop->wsFlags & WHERE_INDEXED)!=0
137939 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
137940 && (n = pLoop->u.btree.nIdxCol)>0
137941 && pIdx->aiRowLogEst[n]>=36
137943 int r1 = pParse->nMem+1;
137946 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
137948 pParse->nMem += n+1;
137949 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
137950 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
137953 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
137957 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
137958 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
137959 sqlite3VdbeChangeP5(v, pLevel->p5);
137961 VdbeCoverageIf(v, pLevel->op==OP_Next);
137962 VdbeCoverageIf(v, pLevel->op==OP_Prev);
137963 VdbeCoverageIf(v, pLevel->op==OP_VNext);
137968 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
137970 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
137973 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
137974 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
137975 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
137976 if( pIn->eEndLoopOp!=OP_Noop ){
137977 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
137979 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
137980 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
137982 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
137985 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
137986 if( pLevel->addrSkip ){
137987 sqlite3VdbeGoto(v, pLevel->addrSkip);
137988 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
137989 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
137990 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
137993 if( pLevel->addrLikeRep ){
137994 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
137995 pLevel->addrLikeRep);
137999 if( pLevel->iLeftJoin ){
138000 int ws = pLoop->wsFlags;
138001 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
138004 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
138005 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
138008 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
138010 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
138012 if( pLevel->op==OP_Return ){
138013 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
138015 sqlite3VdbeGoto(v, pLevel->addrFirst);
138019 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
138020 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
138026 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
138028 assert( pWInfo->nLevel<=pTabList->nSrc );
138029 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
138033 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
138034 Table *pTab = pTabItem->pTab;
138036 pLoop = pLevel->pWLoop;
138038 /* For a co-routine, change all OP_Column references to the table of
138039 ** the co-routine into OP_Copy of result contained in a register.
138042 if( pTabItem->fg.viaCoroutine ){
138043 testcase( pParse->db->mallocFailed );
138044 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
138045 pTabItem->regResult, 0);
138060 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
138061 pIdx = pLoop->u.btree.pIndex;
138062 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
138063 pIdx = pLevel->u.pCovidx;
138066 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
138067 && !db->mallocFailed
138070 k = pLevel->addrBody;
138073 if( pOp->p1!=pLevel->iTabCur ) continue;
138074 if( pOp->opcode==OP_Column
138076 || pOp->opcode==OP_Offset
138079 int x = pOp->p2;
138080 assert( pIdx->pTable==pTab );
138083 x = pPk->aiColumn[x];
138088 pOp->p2 = x;
138089 pOp->p1 = pLevel->iIdxCur;
138091 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
138092 || pWInfo->eOnePass );
138093 }else if( pOp->opcode==OP_Rowid ){
138094 pOp->p1 = pLevel->iIdxCur;
138095 pOp->opcode = OP_IdxRowid;
138096 }else if( pOp->opcode==OP_IfNullRow ){
138097 pOp->p1 = pLevel->iIdxCur;
138105 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
138113 ** 2000-05-29
138127 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
138128 ** interstitial "-" characters) contained in this template is changed into
138142 ** Disable all error recovery processing in the parser push-down
138193 pParse->disableLookaside++;
138194 pParse->db->lookaside.bDisable++;
138199 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
138204 if( p->pPrior ){
138207 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
138208 pLoop->pNext = pNext;
138209 pLoop->selFlags |= SF_Compound;
138211 if( (p->selFlags & SF_MultiValue)==0 &&
138212 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
138226 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
138229 p->op = (u8)op;
138230 p->flags = EP_Leaf;
138231 p->iAgg = -1;
138232 p->u.zToken = (char*)&p[1];
138233 memcpy(p->u.zToken, t.z, t.n);
138234 p->u.zToken[t.n] = 0;
138235 if( sqlite3Isquote(p->u.zToken[0]) ){
138236 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
138237 sqlite3Dequote(p->u.zToken);
138240 p->nHeight = 1;
138249 sqlite3 *db = pParse->db;
138250 if( pA && pY && pY->op==TK_NULL ){
138251 pA->op = (u8)op;
138252 sqlite3ExprDelete(db, pA->pRight);
138253 pA->pRight = 0;
138271 && pParse->db->init.busy==0
138274 pIdToken->n, pIdToken->z);
138282 ** "lemon" is run with the "-m" command-line option.
138289 ** that represent terminal and non-terminal symbols.
138295 ** (also known as: "terminal symbols") have fall-back
138299 ** YYACTIONTYPE is the data type used for "action codes" - numbers
138304 ** value associated with a terminal or non-terminal
138307 ** Each non-terminal can have a different minor type.
138327 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
138328 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
138331 ** YY_NO_ACTION The yy_action[] code for no-op
138364 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
138365 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
138380 /* Define the yytestcase() macro to be a no-op if is not already defined
138405 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
138414 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
138427 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
138439 ** shifting non-terminals after a reduce.
138825 #define YY_REDUCE_MIN (-217)
138828 /* 0 */ -144, -139, -134, -136, -141, 64, 114, 116, -158, -148,
138829 /* 10 */ -217, 96, 819, 871, 878, 219, 270, 886, 272, -110,
138830 /* 20 */ 413, 918, 972, 228, -214, -214, -214, -214, -214, -214,
138831 /* 30 */ -214, -214, -214, -214, -214, -214, -214, -214, -214, -214,
138832 /* 40 */ -214, -214, -214, -214, -214, -214, -214, -214, -214, -214,
138833 /* 50 */ -214, -214, -214, -214, -214, -214, 62, 323, 377, 536,
138838 /* 100 */ 1187, 1192, -214, -214, -214, -214, -214, -214, -214, -214,
138839 /* 110 */ -214, 132, -45, 97, 8, 164, 379, 175, 255, -214,
138840 /* 120 */ 178, -214, -214, -214, -214, -168, -168, -168, 124, 329,
138841 /* 130 */ 399, 401, -129, 347, 347, 331, 133, 451, 452, 498,
138843 /* 150 */ 547, -126, 588, 290, 27, 572, 501, 597, 537, 582,
138844 /* 160 */ 183, 599, 600, 601, 649, 650, 653, 508, 538, -29,
138845 /* 170 */ -156, -152, -137, -79, 135, 74, 130, 242, 338, 378,
138912 /********** End of lemon-generated parsing tables *****************************/
139033 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
139036 YYMINORTYPE minor; /* The user-supplied minor token value. This
139046 int yyhwm; /* High-water mark of the stack */
139702 newSize = p->yystksz*2 + 100;
139703 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
139704 if( p->yystack==&p->yystk0 ){
139706 if( pNew ) pNew[0] = p->yystk0;
139708 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
139711 p->yystack = pNew;
139712 p->yytos = &p->yystack[idx];
139716 yyTracePrompt, p->yystksz, newSize);
139719 p->yystksz = newSize;
139739 pParser->yyhwm = 0;
139742 pParser->yytos = NULL;
139743 pParser->yystack = NULL;
139744 pParser->yystksz = 0;
139746 pParser->yystack = &pParser->yystk0;
139747 pParser->yystksz = 1;
139751 pParser->yyerrcnt = -1;
139753 pParser->yytos = pParser->yystack;
139754 pParser->yystack[0].stateno = 0;
139755 pParser->yystack[0].major = 0;
139757 pParser->yystackEnd = &pParser->yystack[YYSTACKDEPTH-1];
139798 ** terminal or non-terminal is destroyed. This can happen
139813 sqlite3SelectDelete(pParse->db, (yypminor->yy387));
139826 sqlite3ExprDelete(pParse->db, (yypminor->yy314));
139842 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
139850 sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
139856 sqlite3WithDelete(pParse->db, (yypminor->yy451));
139863 sqlite3IdListDelete(pParse->db, (yypminor->yy384));
139869 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
139874 sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
139890 assert( pParser->yytos!=0 );
139891 assert( pParser->yytos > pParser->yystack );
139892 yytos = pParser->yytos--;
139897 yyTokenName[yytos->major]);
139900 yy_destructor(pParser, yytos->major, &yytos->minor);
139908 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
139910 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
139941 return pParser->yyhwm;
139947 ** is in state X and has a lookahead token Y. In a well-tested
139984 ** look-ahead token iLookAhead.
139988 YYCODETYPE iLookAhead /* The look-ahead token */
139991 int stateno = pParser->yytos->stateno;
140022 int j = i - iLookAhead + YYWILDCARD;
140051 ** Find the appropriate action for a parser given the non-terminal
140052 ** look-ahead token iLookAhead.
140056 YYCODETYPE iLookAhead /* The look-ahead token */
140090 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
140108 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
140112 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
140113 yyNewState - YY_MIN_REDUCE);
140131 yypParser->yytos++;
140133 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
140134 yypParser->yyhwm++;
140135 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
140139 if( yypParser->yytos>yypParser->yystackEnd ){
140140 yypParser->yytos--;
140145 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
140147 yypParser->yytos--;
140154 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
140156 yytos = yypParser->yytos;
140157 yytos->stateno = (YYACTIONTYPE)yyNewState;
140158 yytos->major = (YYCODETYPE)yyMajor;
140159 yytos->minor.yy0 = yyMinor;
140167 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
140170 { 147, -1 }, /* (0) explain ::= EXPLAIN */
140171 { 147, -3 }, /* (1) explain ::= EXPLAIN QUERY PLAN */
140172 { 148, -1 }, /* (2) cmdx ::= cmd */
140173 { 149, -3 }, /* (3) cmd ::= BEGIN transtype trans_opt */
140175 { 150, -1 }, /* (5) transtype ::= DEFERRED */
140176 { 150, -1 }, /* (6) transtype ::= IMMEDIATE */
140177 { 150, -1 }, /* (7) transtype ::= EXCLUSIVE */
140178 { 149, -2 }, /* (8) cmd ::= COMMIT|END trans_opt */
140179 { 149, -2 }, /* (9) cmd ::= ROLLBACK trans_opt */
140180 { 149, -2 }, /* (10) cmd ::= SAVEPOINT nm */
140181 { 149, -3 }, /* (11) cmd ::= RELEASE savepoint_opt nm */
140182 { 149, -5 }, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
140183 { 154, -6 }, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
140184 { 156, -1 }, /* (14) createkw ::= CREATE */
140186 { 158, -3 }, /* (16) ifnotexists ::= IF NOT EXISTS */
140187 { 157, -1 }, /* (17) temp ::= TEMP */
140189 { 155, -5 }, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
140190 { 155, -2 }, /* (20) create_table_args ::= AS select */
140192 { 162, -2 }, /* (22) table_options ::= WITHOUT nm */
140193 { 164, -2 }, /* (23) columnname ::= nm typetoken */
140195 { 166, -4 }, /* (25) typetoken ::= typename LP signed RP */
140196 { 166, -6 }, /* (26) typetoken ::= typename LP signed COMMA signed RP */
140197 { 167, -2 }, /* (27) typename ::= typename ID|STRING */
140199 { 172, -2 }, /* (29) ccons ::= CONSTRAINT nm */
140200 { 172, -4 }, /* (30) ccons ::= DEFAULT scanpt term scanpt */
140201 { 172, -4 }, /* (31) ccons ::= DEFAULT LP expr RP */
140202 { 172, -4 }, /* (32) ccons ::= DEFAULT PLUS term scanpt */
140203 { 172, -4 }, /* (33) ccons ::= DEFAULT MINUS term scanpt */
140204 { 172, -3 }, /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
140205 { 172, -3 }, /* (35) ccons ::= NOT NULL onconf */
140206 { 172, -5 }, /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
140207 { 172, -2 }, /* (37) ccons ::= UNIQUE onconf */
140208 { 172, -4 }, /* (38) ccons ::= CHECK LP expr RP */
140209 { 172, -4 }, /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
140210 { 172, -1 }, /* (40) ccons ::= defer_subclause */
140211 { 172, -2 }, /* (41) ccons ::= COLLATE ID|STRING */
140213 { 177, -1 }, /* (43) autoinc ::= AUTOINCR */
140215 { 179, -2 }, /* (45) refargs ::= refargs refarg */
140216 { 181, -2 }, /* (46) refarg ::= MATCH nm */
140217 { 181, -3 }, /* (47) refarg ::= ON INSERT refact */
140218 { 181, -3 }, /* (48) refarg ::= ON DELETE refact */
140219 { 181, -3 }, /* (49) refarg ::= ON UPDATE refact */
140220 { 182, -2 }, /* (50) refact ::= SET NULL */
140221 { 182, -2 }, /* (51) refact ::= SET DEFAULT */
140222 { 182, -1 }, /* (52) refact ::= CASCADE */
140223 { 182, -1 }, /* (53) refact ::= RESTRICT */
140224 { 182, -2 }, /* (54) refact ::= NO ACTION */
140225 { 180, -3 }, /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
140226 { 180, -2 }, /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
140228 { 183, -2 }, /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
140229 { 183, -2 }, /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
140231 { 185, -1 }, /* (61) tconscomma ::= COMMA */
140232 { 186, -2 }, /* (62) tcons ::= CONSTRAINT nm */
140233 { 186, -7 }, /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
140234 { 186, -5 }, /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
140235 { 186, -5 }, /* (65) tcons ::= CHECK LP expr RP onconf */
140236 …{ 186, -10 }, /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defe…
140239 { 175, -3 }, /* (69) onconf ::= ON CONFLICT resolvetype */
140241 { 190, -2 }, /* (71) orconf ::= OR resolvetype */
140242 { 191, -1 }, /* (72) resolvetype ::= IGNORE */
140243 { 191, -1 }, /* (73) resolvetype ::= REPLACE */
140244 { 149, -4 }, /* (74) cmd ::= DROP TABLE ifexists fullname */
140245 { 193, -2 }, /* (75) ifexists ::= IF EXISTS */
140247 { 149, -9 }, /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
140248 { 149, -4 }, /* (78) cmd ::= DROP VIEW ifexists fullname */
140249 { 149, -1 }, /* (79) cmd ::= select */
140250 { 163, -2 }, /* (80) select ::= with selectnowith */
140251 { 195, -3 }, /* (81) selectnowith ::= selectnowith multiselect_op oneselect */
140252 { 198, -1 }, /* (82) multiselect_op ::= UNION */
140253 { 198, -2 }, /* (83) multiselect_op ::= UNION ALL */
140254 { 198, -1 }, /* (84) multiselect_op ::= EXCEPT|INTERSECT */
140255 …{ 196, -9 }, /* (85) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having…
140256 { 207, -4 }, /* (86) values ::= VALUES LP nexprlist RP */
140257 { 207, -5 }, /* (87) values ::= values COMMA LP exprlist RP */
140258 { 199, -1 }, /* (88) distinct ::= DISTINCT */
140259 { 199, -1 }, /* (89) distinct ::= ALL */
140262 { 200, -5 }, /* (92) selcollist ::= sclp scanpt expr scanpt as */
140263 { 200, -3 }, /* (93) selcollist ::= sclp scanpt STAR */
140264 { 200, -5 }, /* (94) selcollist ::= sclp scanpt nm DOT STAR */
140265 { 211, -2 }, /* (95) as ::= AS nm */
140268 { 201, -2 }, /* (98) from ::= FROM seltablist */
140269 { 213, -2 }, /* (99) stl_prefix ::= seltablist joinop */
140271 { 212, -7 }, /* (101) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
140272 { 212, -9 }, /* (102) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
140273 { 212, -7 }, /* (103) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
140274 { 212, -7 }, /* (104) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
140276 { 159, -2 }, /* (106) dbnm ::= DOT nm */
140277 { 194, -2 }, /* (107) fullname ::= nm dbnm */
140278 { 214, -1 }, /* (108) joinop ::= COMMA|JOIN */
140279 { 214, -2 }, /* (109) joinop ::= JOIN_KW JOIN */
140280 { 214, -3 }, /* (110) joinop ::= JOIN_KW nm JOIN */
140281 { 214, -4 }, /* (111) joinop ::= JOIN_KW nm nm JOIN */
140282 { 216, -2 }, /* (112) on_opt ::= ON expr */
140285 { 215, -3 }, /* (115) indexed_opt ::= INDEXED BY nm */
140286 { 215, -2 }, /* (116) indexed_opt ::= NOT INDEXED */
140287 { 217, -4 }, /* (117) using_opt ::= USING LP idlist RP */
140290 { 205, -3 }, /* (120) orderby_opt ::= ORDER BY sortlist */
140291 { 187, -4 }, /* (121) sortlist ::= sortlist COMMA expr sortorder */
140292 { 187, -2 }, /* (122) sortlist ::= expr sortorder */
140293 { 176, -1 }, /* (123) sortorder ::= ASC */
140294 { 176, -1 }, /* (124) sortorder ::= DESC */
140297 { 203, -3 }, /* (127) groupby_opt ::= GROUP BY nexprlist */
140299 { 204, -2 }, /* (129) having_opt ::= HAVING expr */
140301 { 206, -2 }, /* (131) limit_opt ::= LIMIT expr */
140302 { 206, -4 }, /* (132) limit_opt ::= LIMIT expr OFFSET expr */
140303 { 206, -4 }, /* (133) limit_opt ::= LIMIT expr COMMA expr */
140304 { 149, -6 }, /* (134) cmd ::= with DELETE FROM fullname indexed_opt where_opt */
140306 { 202, -2 }, /* (136) where_opt ::= WHERE expr */
140307 { 149, -8 }, /* (137) cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
140308 { 219, -5 }, /* (138) setlist ::= setlist COMMA nm EQ expr */
140309 { 219, -7 }, /* (139) setlist ::= setlist COMMA LP idlist RP EQ expr */
140310 { 219, -3 }, /* (140) setlist ::= nm EQ expr */
140311 { 219, -5 }, /* (141) setlist ::= LP idlist RP EQ expr */
140312 { 149, -6 }, /* (142) cmd ::= with insert_cmd INTO fullname idlist_opt select */
140313 { 149, -7 }, /* (143) cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES */
140314 { 220, -2 }, /* (144) insert_cmd ::= INSERT orconf */
140315 { 220, -1 }, /* (145) insert_cmd ::= REPLACE */
140317 { 221, -3 }, /* (147) idlist_opt ::= LP idlist RP */
140318 { 218, -3 }, /* (148) idlist ::= idlist COMMA nm */
140319 { 218, -1 }, /* (149) idlist ::= nm */
140320 { 174, -3 }, /* (150) expr ::= LP expr RP */
140321 { 174, -1 }, /* (151) expr ::= ID|INDEXED */
140322 { 174, -1 }, /* (152) expr ::= JOIN_KW */
140323 { 174, -3 }, /* (153) expr ::= nm DOT nm */
140324 { 174, -5 }, /* (154) expr ::= nm DOT nm DOT nm */
140325 { 173, -1 }, /* (155) term ::= NULL|FLOAT|BLOB */
140326 { 173, -1 }, /* (156) term ::= STRING */
140327 { 173, -1 }, /* (157) term ::= INTEGER */
140328 { 174, -1 }, /* (158) expr ::= VARIABLE */
140329 { 174, -3 }, /* (159) expr ::= expr COLLATE ID|STRING */
140330 { 174, -6 }, /* (160) expr ::= CAST LP expr AS typetoken RP */
140331 { 174, -5 }, /* (161) expr ::= ID|INDEXED LP distinct exprlist RP */
140332 { 174, -4 }, /* (162) expr ::= ID|INDEXED LP STAR RP */
140333 { 173, -1 }, /* (163) term ::= CTIME_KW */
140334 { 174, -5 }, /* (164) expr ::= LP nexprlist COMMA expr RP */
140335 { 174, -3 }, /* (165) expr ::= expr AND expr */
140336 { 174, -3 }, /* (166) expr ::= expr OR expr */
140337 { 174, -3 }, /* (167) expr ::= expr LT|GT|GE|LE expr */
140338 { 174, -3 }, /* (168) expr ::= expr EQ|NE expr */
140339 { 174, -3 }, /* (169) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
140340 { 174, -3 }, /* (170) expr ::= expr PLUS|MINUS expr */
140341 { 174, -3 }, /* (171) expr ::= expr STAR|SLASH|REM expr */
140342 { 174, -3 }, /* (172) expr ::= expr CONCAT expr */
140343 { 222, -2 }, /* (173) likeop ::= NOT LIKE_KW|MATCH */
140344 { 174, -3 }, /* (174) expr ::= expr likeop expr */
140345 { 174, -5 }, /* (175) expr ::= expr likeop expr ESCAPE expr */
140346 { 174, -2 }, /* (176) expr ::= expr ISNULL|NOTNULL */
140347 { 174, -3 }, /* (177) expr ::= expr NOT NULL */
140348 { 174, -3 }, /* (178) expr ::= expr IS expr */
140349 { 174, -4 }, /* (179) expr ::= expr IS NOT expr */
140350 { 174, -2 }, /* (180) expr ::= NOT expr */
140351 { 174, -2 }, /* (181) expr ::= BITNOT expr */
140352 { 174, -2 }, /* (182) expr ::= MINUS expr */
140353 { 174, -2 }, /* (183) expr ::= PLUS expr */
140354 { 223, -1 }, /* (184) between_op ::= BETWEEN */
140355 { 223, -2 }, /* (185) between_op ::= NOT BETWEEN */
140356 { 174, -5 }, /* (186) expr ::= expr between_op expr AND expr */
140357 { 224, -1 }, /* (187) in_op ::= IN */
140358 { 224, -2 }, /* (188) in_op ::= NOT IN */
140359 { 174, -5 }, /* (189) expr ::= expr in_op LP exprlist RP */
140360 { 174, -3 }, /* (190) expr ::= LP select RP */
140361 { 174, -5 }, /* (191) expr ::= expr in_op LP select RP */
140362 { 174, -5 }, /* (192) expr ::= expr in_op nm dbnm paren_exprlist */
140363 { 174, -4 }, /* (193) expr ::= EXISTS LP select RP */
140364 { 174, -5 }, /* (194) expr ::= CASE case_operand case_exprlist case_else END */
140365 { 227, -5 }, /* (195) case_exprlist ::= case_exprlist WHEN expr THEN expr */
140366 { 227, -4 }, /* (196) case_exprlist ::= WHEN expr THEN expr */
140367 { 228, -2 }, /* (197) case_else ::= ELSE expr */
140369 { 226, -1 }, /* (199) case_operand ::= expr */
140372 { 208, -3 }, /* (202) nexprlist ::= nexprlist COMMA expr */
140373 { 208, -1 }, /* (203) nexprlist ::= expr */
140375 { 225, -3 }, /* (205) paren_exprlist ::= LP exprlist RP */
140376 …{ 149, -12 }, /* (206) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist R…
140377 { 229, -1 }, /* (207) uniqueflag ::= UNIQUE */
140380 { 178, -3 }, /* (210) eidlist_opt ::= LP eidlist RP */
140381 { 188, -5 }, /* (211) eidlist ::= eidlist COMMA nm collate sortorder */
140382 { 188, -3 }, /* (212) eidlist ::= nm collate sortorder */
140384 { 230, -2 }, /* (214) collate ::= COLLATE ID|STRING */
140385 { 149, -4 }, /* (215) cmd ::= DROP INDEX ifexists fullname */
140386 { 149, -1 }, /* (216) cmd ::= VACUUM */
140387 { 149, -2 }, /* (217) cmd ::= VACUUM nm */
140388 { 149, -3 }, /* (218) cmd ::= PRAGMA nm dbnm */
140389 { 149, -5 }, /* (219) cmd ::= PRAGMA nm dbnm EQ nmnum */
140390 { 149, -6 }, /* (220) cmd ::= PRAGMA nm dbnm LP nmnum RP */
140391 { 149, -5 }, /* (221) cmd ::= PRAGMA nm dbnm EQ minus_num */
140392 { 149, -6 }, /* (222) cmd ::= PRAGMA nm dbnm LP minus_num RP */
140393 { 169, -2 }, /* (223) plus_num ::= PLUS INTEGER|FLOAT */
140394 { 170, -2 }, /* (224) minus_num ::= MINUS INTEGER|FLOAT */
140395 { 149, -5 }, /* (225) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
140396 …{ 232, -11 }, /* (226) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_ev…
140397 { 234, -1 }, /* (227) trigger_time ::= BEFORE|AFTER */
140398 { 234, -2 }, /* (228) trigger_time ::= INSTEAD OF */
140400 { 235, -1 }, /* (230) trigger_event ::= DELETE|INSERT */
140401 { 235, -1 }, /* (231) trigger_event ::= UPDATE */
140402 { 235, -3 }, /* (232) trigger_event ::= UPDATE OF idlist */
140404 { 237, -2 }, /* (234) when_clause ::= WHEN expr */
140405 { 233, -3 }, /* (235) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
140406 { 233, -2 }, /* (236) trigger_cmd_list ::= trigger_cmd SEMI */
140407 { 239, -3 }, /* (237) trnm ::= nm DOT nm */
140408 { 240, -3 }, /* (238) tridxby ::= INDEXED BY nm */
140409 { 240, -2 }, /* (239) tridxby ::= NOT INDEXED */
140410 …{ 238, -8 }, /* (240) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
140411 { 238, -7 }, /* (241) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select scanpt */
140412 { 238, -6 }, /* (242) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
140413 { 238, -3 }, /* (243) trigger_cmd ::= scanpt select scanpt */
140414 { 174, -4 }, /* (244) expr ::= RAISE LP IGNORE RP */
140415 { 174, -6 }, /* (245) expr ::= RAISE LP raisetype COMMA nm RP */
140416 { 192, -1 }, /* (246) raisetype ::= ROLLBACK */
140417 { 192, -1 }, /* (247) raisetype ::= ABORT */
140418 { 192, -1 }, /* (248) raisetype ::= FAIL */
140419 { 149, -4 }, /* (249) cmd ::= DROP TRIGGER ifexists fullname */
140420 { 149, -6 }, /* (250) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
140421 { 149, -3 }, /* (251) cmd ::= DETACH database_kw_opt expr */
140423 { 242, -2 }, /* (253) key_opt ::= KEY expr */
140424 { 149, -1 }, /* (254) cmd ::= REINDEX */
140425 { 149, -3 }, /* (255) cmd ::= REINDEX nm dbnm */
140426 { 149, -1 }, /* (256) cmd ::= ANALYZE */
140427 { 149, -3 }, /* (257) cmd ::= ANALYZE nm dbnm */
140428 { 149, -6 }, /* (258) cmd ::= ALTER TABLE fullname RENAME TO nm */
140429 …{ 149, -7 }, /* (259) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname cargl…
140430 { 243, -1 }, /* (260) add_column_fullname ::= fullname */
140431 { 149, -1 }, /* (261) cmd ::= create_vtab */
140432 { 149, -4 }, /* (262) cmd ::= create_vtab LP vtabarglist RP */
140433 { 245, -8 }, /* (263) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
140435 { 248, -1 }, /* (265) vtabargtoken ::= ANY */
140436 { 248, -3 }, /* (266) vtabargtoken ::= lp anylist RP */
140437 { 249, -1 }, /* (267) lp ::= LP */
140439 { 197, -2 }, /* (269) with ::= WITH wqlist */
140440 { 197, -3 }, /* (270) with ::= WITH RECURSIVE wqlist */
140441 { 251, -6 }, /* (271) wqlist ::= nm eidlist_opt AS LP select RP */
140442 { 251, -8 }, /* (272) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
140443 { 144, -1 }, /* (273) input ::= cmdlist */
140444 { 145, -2 }, /* (274) cmdlist ::= cmdlist ecmd */
140445 { 145, -1 }, /* (275) cmdlist ::= ecmd */
140446 { 146, -1 }, /* (276) ecmd ::= SEMI */
140447 { 146, -3 }, /* (277) ecmd ::= explain cmdx SEMI */
140450 { 151, -1 }, /* (280) trans_opt ::= TRANSACTION */
140451 { 151, -2 }, /* (281) trans_opt ::= TRANSACTION nm */
140452 { 153, -1 }, /* (282) savepoint_opt ::= SAVEPOINT */
140454 { 149, -2 }, /* (284) cmd ::= create_table create_table_args */
140455 { 160, -4 }, /* (285) columnlist ::= columnlist COMMA columnname carglist */
140456 { 160, -2 }, /* (286) columnlist ::= columnname carglist */
140457 { 152, -1 }, /* (287) nm ::= ID|INDEXED */
140458 { 152, -1 }, /* (288) nm ::= STRING */
140459 { 152, -1 }, /* (289) nm ::= JOIN_KW */
140460 { 166, -1 }, /* (290) typetoken ::= typename */
140461 { 167, -1 }, /* (291) typename ::= ID|STRING */
140462 { 168, -1 }, /* (292) signed ::= plus_num */
140463 { 168, -1 }, /* (293) signed ::= minus_num */
140464 { 165, -2 }, /* (294) carglist ::= carglist ccons */
140466 { 172, -2 }, /* (296) ccons ::= NULL onconf */
140467 { 161, -2 }, /* (297) conslist_opt ::= COMMA conslist */
140468 { 184, -3 }, /* (298) conslist ::= conslist tconscomma tcons */
140469 { 184, -1 }, /* (299) conslist ::= tcons */
140471 { 189, -1 }, /* (301) defer_subclause_opt ::= defer_subclause */
140472 { 191, -1 }, /* (302) resolvetype ::= raisetype */
140473 { 195, -1 }, /* (303) selectnowith ::= oneselect */
140474 { 196, -1 }, /* (304) oneselect ::= values */
140475 { 210, -2 }, /* (305) sclp ::= selcollist COMMA */
140476 { 211, -1 }, /* (306) as ::= ID|STRING */
140477 { 174, -1 }, /* (307) expr ::= term */
140478 { 222, -1 }, /* (308) likeop ::= LIKE_KW|MATCH */
140479 { 209, -1 }, /* (309) exprlist ::= nexprlist */
140480 { 231, -1 }, /* (310) nmnum ::= plus_num */
140481 { 231, -1 }, /* (311) nmnum ::= nm */
140482 { 231, -1 }, /* (312) nmnum ::= ON */
140483 { 231, -1 }, /* (313) nmnum ::= DELETE */
140484 { 231, -1 }, /* (314) nmnum ::= DEFAULT */
140485 { 169, -1 }, /* (315) plus_num ::= INTEGER|FLOAT */
140487 { 236, -3 }, /* (317) foreach_clause ::= FOR EACH ROW */
140488 { 239, -1 }, /* (318) trnm ::= nm */
140490 { 241, -1 }, /* (320) database_kw_opt ::= DATABASE */
140493 { 244, -1 }, /* (323) kwcolumn_opt ::= COLUMNKW */
140494 { 246, -1 }, /* (324) vtabarglist ::= vtabarg */
140495 { 246, -3 }, /* (325) vtabarglist ::= vtabarglist COMMA vtabarg */
140496 { 247, -2 }, /* (326) vtabarg ::= vtabarg vtabargtoken */
140498 { 250, -4 }, /* (328) anylist ::= anylist LP anylist RP */
140499 { 250, -2 }, /* (329) anylist ::= anylist ANY */
140511 ** only called from one place, optimizing compilers will in-line it, which
140527 yymsp = yypParser->yytos;
140547 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
140548 yypParser->yyhwm++;
140549 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
140553 if( yypParser->yytos>=yypParser->yystackEnd ){
140558 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
140563 yymsp = yypParser->yytos;
140580 { pParse->explain = 1; }
140583 { pParse->explain = 2; }
140589 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
140597 {yymsp[0].minor.yy4 = yymsp[0].major; /*A-overwrites-X*/}
140601 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
140620 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2…
140638 {yymsp[-2].minor.yy4 = 1;}
140646 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy4,0);
140652 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
140658 yymsp[-1].minor.yy4 = TF_WithoutRowid | TF_NoVisibleRowid;
140660 yymsp[-1].minor.yy4 = 0;
140666 {sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
140675 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
140680 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
140684 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
140694 {pParse->constraintName = yymsp[0].minor.yy0;}
140697 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy314,yymsp[-2].minor.yy336,yymsp[0].minor.yy336);}
140700 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy314,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
140703 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy314,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy336);}
140707 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy314, 0);
140708 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy336);
140721 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
140728 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy314);}
140731 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
140740 { yymsp[1].minor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
140743 { yymsp[-1].minor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.v…
140746 { yymsp[-1].minor.yy215.value = 0; yymsp[-1].minor.yy215.mask = 0x000000; }
140749 { yymsp[-2].minor.yy215.value = 0; yymsp[-2].minor.yy215.mask = 0x000000; }
140752 { yymsp[-2].minor.yy215.value = yymsp[0].minor.yy4; yymsp[-2].minor.yy215.mask = 0x0000ff; }
140755 { yymsp[-2].minor.yy215.value = yymsp[0].minor.yy4<<8; yymsp[-2].minor.yy215.mask = 0x00ff00; }
140758 { yymsp[-1].minor.yy4 = OE_SetNull; /* EV: R-33326-45252 */}
140761 { yymsp[-1].minor.yy4 = OE_SetDflt; /* EV: R-33326-45252 */}
140764 { yymsp[0].minor.yy4 = OE_Cascade; /* EV: R-33326-45252 */}
140767 { yymsp[0].minor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
140770 { yymsp[-1].minor.yy4 = OE_None; /* EV: R-33326-45252 */}
140773 {yymsp[-2].minor.yy4 = 0;}
140778 {yymsp[-1].minor.yy4 = yymsp[0].minor.yy4;}
140785 {yymsp[-1].minor.yy4 = 1;}
140788 {yymsp[-1].minor.yy4 = 0;}
140791 {pParse->constraintName.n = 0;}
140794 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
140797 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0,
140801 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy314);}
140805 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
140814 {yymsp[-2].minor.yy4 = yymsp[0].minor.yy4;}
140825 sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
140830 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
140835 sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
140842 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
140849 p->pWith = yymsp[-1].minor.yy451;
140852 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy451);
140854 yymsp[-1].minor.yy387 = p; /*A-overwrites-W*/
140860 Select *pLhs = yymsp[-2].minor.yy387;
140861 if( pRhs && pRhs->pPrior ){
140870 pRhs->op = (u8)yymsp[-1].minor.yy4;
140871 pRhs->pPrior = pLhs;
140872 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
140873 pRhs->selFlags &= ~SF_MultiValue;
140874 if( yymsp[-1].minor.yy4!=TK_ALL ) pParse->hasCompound = 1;
140876 sqlite3SelectDelete(pParse->db, pLhs);
140878 yymsp[-2].minor.yy387 = pRhs;
140883 {yymsp[0].minor.yy4 = yymsp[0].major; /*A-overwrites-OP*/}
140886 {yymsp[-1].minor.yy4 = TK_ALL;}
140891 Token s = yymsp[-8].minor.yy0; /*A-overwrites-S*/
140893 …-8].minor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy259,yymsp[-4].mi…
140899 ** If the SELECT keyword is immediately followed by a C-style comment
140904 if( yymsp[-8].minor.yy387!=0 ){
140907 … sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "#%d",
140908 ++pParse->nSelect);
140914 …sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "%.*s",…
140922 yymsp[-3].minor.yy387 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
140927 Select *pRight, *pLeft = yymsp[-4].minor.yy387;
140928 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
140929 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
140931 pRight->op = TK_ALL;
140932 pRight->pPrior = pLeft;
140933 yymsp[-4].minor.yy387 = pRight;
140935 yymsp[-4].minor.yy387 = pLeft;
140955 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy314…
140956 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
140957 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy336,yymsp[-1].minor.yy336);
140962 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
140963 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
140969 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
140971 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
140978 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
140981 {yymsp[1].minor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy259));}
140985 yymsp[-1].minor.yy259 = yymsp[0].minor.yy259;
140986 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy259);
140991 …if( ALWAYS(yymsp[-1].minor.yy259 && yymsp[-1].minor.yy259->nSrc>0) ) yymsp[-1].minor.yy259->a[yyms…
140999 …p[-6].minor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,&yymsp[-5].minor.yy0…
141000 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy259, &yymsp[-2].minor.yy0);
141005 …p[-8].minor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy259,&yymsp[-7].minor.yy0…
141006 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy259, yymsp[-4].minor.yy322);
141011 …yymsp[-6].minor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].m…
141016 …if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].m…
141017 yymsp[-6].minor.yy259 = yymsp[-4].minor.yy259;
141018 }else if( yymsp[-4].minor.yy259->nSrc==1 ){
141019 …yymsp[-6].minor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].m…
141020 if( yymsp[-6].minor.yy259 ){
141021 struct SrcList_item *pNew = &yymsp[-6].minor.yy259->a[yymsp[-6].minor.yy259->nSrc-1];
141022 struct SrcList_item *pOld = yymsp[-4].minor.yy259->a;
141023 pNew->zName = pOld->zName;
141024 pNew->zDatabase = pOld->zDatabase;
141025 pNew->pSelect = pOld->pSelect;
141026 pOld->zName = pOld->zDatabase = 0;
141027 pOld->pSelect = 0;
141029 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy259);
141032 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
141033 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,SF_NestedFrom,0);
141034 …yymsp[-6].minor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].m…
141043 {yymsp[-1].minor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0…
141049 {yymsp[-1].minor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
141052 {yymsp[-2].minor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-ove…
141055 {yymsp[-3].minor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].…
141061 {yymsp[-1].minor.yy314 = yymsp[0].minor.yy314;}
141072 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
141075 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
141078 {yymsp[-3].minor.yy384 = yymsp[-1].minor.yy384;}
141086 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
141090 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
141091 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy322,yymsp[0].minor.yy4);
141096 yymsp[-1].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314); /*A-overwrites-Y*/
141097 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy322,yymsp[0].minor.yy4);
141110 {yymsp[-1].minor.yy314 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy314,0);}
141113 {yymsp[-3].minor.yy314 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy314,yymsp[0].minor.yy314);}
141116 {yymsp[-3].minor.yy314 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy314,yymsp[-2].minor.yy314);}
141120 sqlite3WithPush(pParse, yymsp[-5].minor.yy451, 1);
141121 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
141122 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314,0,0);
141127 sqlite3WithPush(pParse, yymsp[-7].minor.yy451, 1);
141128 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
141129 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list");
141130 …sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].mi…
141135 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy314);
141136 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
141141 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
141147 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
141149 yymsp[-2].minor.yy322 = yylhsminor.yy322;
141153 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy384, yymsp[0].min…
141158 sqlite3WithPush(pParse, yymsp[-5].minor.yy451, 1);
141159 …sqlite3Insert(pParse, yymsp[-2].minor.yy259, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4…
141164 sqlite3WithPush(pParse, yymsp[-6].minor.yy451, 1);
141165 sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy4);
141169 {yymsp[-2].minor.yy384 = yymsp[-1].minor.yy384;}
141172 {yymsp[-2].minor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
141175 {yymsp[0].minor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
141178 {yymsp[-2].minor.yy314 = yymsp[-1].minor.yy314;}
141182 {yymsp[0].minor.yy314=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
141186 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
141187 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
141190 yymsp[-2].minor.yy314 = yylhsminor.yy314;
141194 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
141195 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
141196 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
141200 yymsp[-4].minor.yy314 = yylhsminor.yy314;
141204 {yymsp[0].minor.yy314=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
141208 yylhsminor.yy314 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
141221 ** in the virtual machine. #N is the N-th register. */
141222 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
141224 if( pParse->nested==0 ){
141229 if( yymsp[0].minor.yy314 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy314->iTable);
141236 …yymsp[-2].minor.yy314 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy314, &yymsp[0].minor.…
141241 yymsp[-5].minor.yy314 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
141242 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy314, yymsp[-3].minor.yy314, 0);
141247 …if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION…
141248 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
141250 yylhsminor.yy314 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
141251 if( yymsp[-2].minor.yy4==SF_Distinct && yylhsminor.yy314 ){
141252 yylhsminor.yy314->flags |= EP_Distinct;
141255 yymsp[-4].minor.yy314 = yylhsminor.yy314;
141259 yylhsminor.yy314 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
141261 yymsp[-3].minor.yy314 = yylhsminor.yy314;
141271 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy314);
141272 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
141273 if( yymsp[-4].minor.yy314 ){
141274 yymsp[-4].minor.yy314->x.pList = pList;
141276 sqlite3ExprListDelete(pParse->db, pList);
141288 {yymsp[-2].minor.yy314=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy314,yymsp[0].minor.yy3…
141291 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
141296 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
141297 yymsp[-1].minor.yy0.n &= 0x7fffffff;
141299 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy314);
141300 yymsp[-2].minor.yy314 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0);
141301 if( bNot ) yymsp[-2].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy314, 0);
141302 if( yymsp[-2].minor.yy314 ) yymsp[-2].minor.yy314->flags |= EP_InfixFunc;
141308 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
141309 yymsp[-3].minor.yy0.n &= 0x7fffffff;
141310 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy314);
141311 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy314);
141313 yymsp[-4].minor.yy314 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0);
141314 if( bNot ) yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy314, 0);
141315 if( yymsp[-4].minor.yy314 ) yymsp[-4].minor.yy314->flags |= EP_InfixFunc;
141319 {yymsp[-1].minor.yy314 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy314,0);}
141322 {yymsp[-2].minor.yy314 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy314,0);}
141326 yymsp[-2].minor.yy314 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy314,yymsp[0].minor.yy314);
141327 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy314, yymsp[-2].minor.yy314, TK_ISNULL);
141332 yymsp[-3].minor.yy314 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy314,yymsp[0].minor.yy314);
141333 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy314, yymsp[-3].minor.yy314, TK_NOTNULL);
141338 {yymsp[-1].minor.yy314 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy314, 0);/*A-overwri…
141341 {yymsp[-1].minor.yy314 = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy314, 0);}
141344 {yymsp[-1].minor.yy314 = sqlite3PExpr(pParse, TK_UPLUS, yymsp[0].minor.yy314, 0);}
141352 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy314);
141354 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy314, 0);
141355 if( yymsp[-4].minor.yy314 ){
141356 yymsp[-4].minor.yy314->x.pList = pList;
141358 sqlite3ExprListDelete(pParse->db, pList);
141360 …if( yymsp[-3].minor.yy4 ) yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy3…
141365 if( yymsp[-1].minor.yy322==0 ){
141374 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy314);
141375 …yymsp[-4].minor.yy314 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.…
141376 }else if( yymsp[-1].minor.yy322->nExpr==1 ){
141393 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
141394 yymsp[-1].minor.yy322->a[0].pExpr = 0;
141395 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
141399 pRHS->flags &= ~EP_Collate;
141400 pRHS->flags |= EP_Generic;
141402 …yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, yymsp[-3].minor.yy4 ? TK_NE : TK_EQ, yymsp[-4].minor.…
141404 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy314, 0);
141405 if( yymsp[-4].minor.yy314 ){
141406 yymsp[-4].minor.yy314->x.pList = yymsp[-1].minor.yy322;
141407 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy314);
141409 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
141411 …if( yymsp[-3].minor.yy4 ) yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy3…
141417 yymsp[-2].minor.yy314 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
141418 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy314, yymsp[-1].minor.yy387);
141423 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy314, 0);
141424 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy314, yymsp[-1].minor.yy387);
141425 …if( yymsp[-3].minor.yy4 ) yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy3…
141430 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
141433 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy314, 0);
141434 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy314, pSelect);
141435 …if( yymsp[-3].minor.yy4 ) yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy3…
141441 p = yymsp[-3].minor.yy314 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
141442 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy387);
141447 yymsp[-4].minor.yy314 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, 0);
141448 if( yymsp[-4].minor.yy314 ){
141449 …yymsp[-4].minor.yy314->x.pList = yymsp[-1].minor.yy314 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
141450 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy314);
141452 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
141453 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy314);
141459 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy314);
141460 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy314);
141465 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy314);
141466 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy314);
141470 {yymsp[0].minor.yy314 = yymsp[0].minor.yy314; /*A-overwrites-X*/}
141473 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy314);}
141476 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy314); /*A-overwrites-Y*/}
141480 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
141484 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
141485 …sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor…
141486 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy314, SQLITE_SO_ASC, yymsp[-8].minor.yy4, SQLITE_IDXTYPE_AP…
141498 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
141503 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
141507 {sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
141516 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
141519 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
141522 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
141525 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
141528 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
141533 all.z = yymsp[-3].minor.yy0.z;
141534 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
141535 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
141540 …-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.…
141541 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
141545 { yymsp[0].minor.yy4 = yymsp[0].major; /*A-overwrites-X*/ }
141548 { yymsp[-1].minor.yy4 = TK_INSTEAD;}
141555 {yymsp[0].minor.yy90.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy90.b = 0;}
141558 {yymsp[-2].minor.yy90.a = TK_UPDATE; yymsp[-2].minor.yy90.b = yymsp[0].minor.yy384;}
141566 { yymsp[-1].minor.yy314 = yymsp[0].minor.yy314; }
141570 assert( yymsp[-2].minor.yy203!=0 );
141571 yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
141572 yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
141577 assert( yymsp[-1].minor.yy203!=0 );
141578 yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
141583 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
141604 …3TriggerUpdateStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy314,…
141605 yymsp[-7].minor.yy203 = yylhsminor.yy203;
141608 …->db,&yymsp[-3].minor.yy0,yymsp[-2].minor.yy384,yymsp[-1].minor.yy387,yymsp[-5].minor.yy4,yymsp[-6…
141609 yymsp[-6].minor.yy203 = yylhsminor.yy203;
141612 …hsminor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy314, …
141613 yymsp[-5].minor.yy203 = yylhsminor.yy203;
141616 …gerSelectStep(pParse->db, yymsp[-1].minor.yy387, yymsp[-2].minor.yy336, yymsp[0].minor.yy336); /*y…
141617 yymsp[-2].minor.yy203 = yylhsminor.yy203;
141621 yymsp[-3].minor.yy314 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
141622 if( yymsp[-3].minor.yy314 ){
141623 yymsp[-3].minor.yy314->affinity = OE_Ignore;
141629 yymsp[-5].minor.yy314 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
141630 if( yymsp[-5].minor.yy314 ) {
141631 yymsp[-5].minor.yy314->affinity = (char)yymsp[-3].minor.yy4;
141643 sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
141648 sqlite3Attach(pParse, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, yymsp[0].minor.yy314);
141660 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
141666 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
141670 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
141675 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
141676 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
141693 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
141708 { yymsp[-1].minor.yy451 = yymsp[0].minor.yy451; }
141711 { yymsp[-2].minor.yy451 = yymsp[0].minor.yy451; }
141715 …yymsp[-5].minor.yy451 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yym…
141720 …yymsp[-7].minor.yy451 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy451, &yymsp[-5].minor.yy0, yymsp[…
141797 yypParser->yytos = yymsp;
141798 yymsp->stateno = (YYACTIONTYPE)yyact;
141799 yymsp->major = (YYCODETYPE)yygoto;
141816 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
141860 yypParser->yyerrcnt = -1;
141862 assert( yypParser->yytos==yypParser->yystack );
141883 ** <li> An option argument of a grammar-specified type.
141906 assert( yypParser->yytos!=0 );
141914 int stateno = yypParser->yytos->stateno;
141920 yyTracePrompt,yyTokenName[yymajor],stateno-YY_MIN_REDUCE);
141928 yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor);
141932 yypParser->yyerrcnt--;
141936 yypParser->yytos--;
141970 if( yypParser->yyerrcnt<0 ){
141973 yymx = yypParser->yytos->major;
141984 while( yypParser->yytos >= yypParser->yystack
141987 yypParser->yytos->stateno,
141992 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
141996 yypParser->yyerrcnt = -1;
142003 yypParser->yyerrcnt = 3;
142027 if( yypParser->yyerrcnt<=0 ){
142030 yypParser->yyerrcnt = 3;
142035 yypParser->yyerrcnt = -1;
142041 }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack );
142047 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
142048 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
142073 ** individual tokens and sends those tokens one-by-one over to the
142098 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
142103 #define CC_SLASH 16 /* '/'. / or c-style comment */
142159 ** lower-case ASCII equivalent. On ASCII machines, this is just
142160 ** an upper-to-lower case map. On EBCDIC machines we also need
142213 ** might be implemented more directly using a hand-written hash table.
142263 /* aKWHash[i] is the hash value for the i-th keyword */
142277 ** then the i-th keyword has no more hash collisions. Otherwise,
142278 ** the next keyword with the same hash is aKWHash[i]-1. */
142291 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
142305 ** the text for the i-th keyword. */
142318 /* aKWCode[i] is the parser symbol code for the i-th keyword */
142346 /* Check to see if z[0..n-1] is a keyword. If it is, write the
142353 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
142354 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
142510 ** For ASCII, any character with the high-order bit set is
142511 ** allowed in an identifier. For 7-bit characters,
142541 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
142556 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
142570 if( z[1]=='-' ){
142572 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
142605 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
142730 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
142836 void *pEngine; /* The LEMON-generated LALR(1) parser */
142839 int lastTokenParsed = -1; /* type of the previous token */
142840 sqlite3 *db = pParse->db; /* The database connection */
142843 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
142847 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
142848 if( db->nVdbeActive==0 ){
142849 db->u1.isInterrupted = 0;
142851 pParse->rc = SQLITE_OK;
142852 pParse->zTail = zSql;
142865 assert( pParse->pNewTable==0 );
142866 assert( pParse->pNewTrigger==0 );
142867 assert( pParse->nVar==0 );
142868 assert( pParse->pVList==0 );
142872 mxSqlLen -= n;
142874 pParse->rc = SQLITE_TOOBIG;
142891 if( db->u1.isInterrupted ){
142892 pParse->rc = SQLITE_INTERRUPT;
142901 pParse->sLastToken.z = zSql;
142902 pParse->sLastToken.n = n;
142903 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
142906 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
142910 pParse->zTail = zSql;
142923 if( db->mallocFailed ){
142924 pParse->rc = SQLITE_NOMEM_BKPT;
142926 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
142927 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
142930 if( pParse->zErrMsg ){
142931 *pzErrMsg = pParse->zErrMsg;
142932 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
142933 pParse->zErrMsg = 0;
142936 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
142937 sqlite3VdbeDelete(pParse->pVdbe);
142938 pParse->pVdbe = 0;
142941 if( pParse->nested==0 ){
142942 sqlite3DbFree(db, pParse->aTableLock);
142943 pParse->aTableLock = 0;
142944 pParse->nTableLock = 0;
142948 sqlite3_free(pParse->apVtabLock);
142952 /* If the pParse->declareVtab flag is set, do not delete any table
142953 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
142956 sqlite3DeleteTable(db, pParse->pNewTable);
142959 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
142960 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
142961 sqlite3DbFree(db, pParse->pVList);
142962 while( pParse->pAinc ){
142963 AutoincInfo *p = pParse->pAinc;
142964 pParse->pAinc = p->pNext;
142967 while( pParse->pZombieTab ){
142968 Table *p = pParse->pZombieTab;
142969 pParse->pZombieTab = p->pNextZombie;
142972 assert( nErr==0 || pParse->rc!=SQLITE_OK );
143008 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
143037 ** (0) INVALID We have not yet seen a non-whitespace character.
143135 case '/': { /* C-style comments */
143147 case '-': { /* SQL-style comments from "--" to end of line */
143148 if( zSql[1]!='-' ){
143157 case '[': { /* Microsoft-style identifiers in [...] */
143164 case '`': /* Grave-accent quoted symbols used by MySQL */
143165 case '"': /* single- and double-quoted strings */
143226 zSql += nId-1;
143243 ** above, except that the parameter is required to be UTF-16 encoded, not
143244 ** UTF-8.
143256 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
143404 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
143410 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
143415 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
143423 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
143428 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
143430 ** the SQLITE_THREADSAFE compile-time option being set to 0.
143436 ** this variable being set to non-zero will cause OSTRACE macros to emit
143483 ** This routine is a no-op except on its very first call for the process,
143499 ** * Calls to this routine from Y must block until the outer-most
143520 ** combination, the work-around is to set the correct pointer
143521 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
143525 ** to sqlite3_initialize() should be a no-op. But the initialization
143545 ** malloc subsystem - this implies that the allocation of a static
143582 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
143623 sqlite3GlobalConfig.nRefInitMutex--;
143640 u64 x = (((u64)1)<<63)-1;
143651 ** compile-time option.
143669 ** when this routine is invoked, then this routine is a harmless no-op.
143718 ** the SQLite library at run-time.
143739 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
143741 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
143742 ** Single-thread. */
143748 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
143750 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
143751 ** Multi-thread. */
143757 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
143759 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
143766 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
143773 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
143782 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
143785 ** low-level memory allocation routines to be used in place of the memory
143791 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
143800 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
143811 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
143812 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
143821 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
143833 /* no-op */
143843 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
143851 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
143862 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
143867 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
143868 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
143883 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
143893 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
143928 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
143929 ** can be changed at start-time using the
143934 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
143935 ** argument of type int. If non-zero, then URI handling is globally
143943 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
143961 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
143967 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
143968 ** negative, then that argument is changed to its compile-time default.
143970 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
143972 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
143973 ** compile-time option.
143985 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
143987 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
144036 if( db->lookaside.bMalloced ){
144037 sqlite3_free(db->lookaside.pStart);
144042 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
144050 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
144056 db->lookaside.pStart = pStart;
144057 db->lookaside.pInit = 0;
144058 db->lookaside.pFree = 0;
144059 db->lookaside.sz = (u16)sz;
144064 db->lookaside.nSlot = cnt;
144066 for(i=cnt-1; i>=0; i--){
144067 p->pNext = db->lookaside.pInit;
144068 db->lookaside.pInit = p;
144071 db->lookaside.pEnd = p;
144072 db->lookaside.bDisable = 0;
144073 db->lookaside.bMalloced = pBuf==0 ?1:0;
144075 db->lookaside.pStart = db;
144076 db->lookaside.pEnd = db;
144077 db->lookaside.bDisable = 1;
144078 db->lookaside.bMalloced = 0;
144079 db->lookaside.nSlot = 0;
144095 return db->mutex;
144108 sqlite3_mutex_enter(db->mutex);
144110 for(i=0; i<db->nDb; i++){
144111 Btree *pBt = db->aDb[i].pBt;
144118 sqlite3_mutex_leave(db->mutex);
144123 ** Flush any dirty pages in the pager-cache for any attached database
144134 sqlite3_mutex_enter(db->mutex);
144136 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
144137 Btree *pBt = db->aDb[i].pBt;
144148 sqlite3_mutex_leave(db->mutex);
144161 /* IMP: R-06824-28531 */
144162 /* IMP: R-36257-52125 */
144163 db->aDb[0].zDbSName = va_arg(ap,char*);
144168 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
144169 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
144170 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
144188 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
144193 u32 oldFlags = db->flags;
144195 db->flags |= aFlagOp[i].mask;
144197 db->flags &= ~aFlagOp[i].mask;
144199 if( oldFlags!=db->flags ){
144203 *pRes = (db->flags & aFlagOp[i].mask)!=0;
144218 ** Return true if the buffer z[0..n-1] contains all spaces.
144221 while( n>0 && z[n-1]==' ' ){ n--; }
144239 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
144246 && allSpaces(((char*)pKey1)+n, nKey1-n)
144247 && allSpaces(((char*)pKey2)+n, nKey2-n)
144249 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
144255 rc = nKey1 - nKey2;
144262 ** Another built-in collating sequence: NOCASE.
144268 ** At the moment there is only a UTF-8 implementation.
144279 r = nKey1-nKey2;
144294 return db->lastRowid;
144307 sqlite3_mutex_enter(db->mutex);
144308 db->lastRowid = iRowid;
144309 sqlite3_mutex_leave(db->mutex);
144322 return db->nChange;
144335 return db->nTotalChange;
144341 ** at the b-tree/pager level.
144344 while( db->pSavepoint ){
144345 Savepoint *pTmp = db->pSavepoint;
144346 db->pSavepoint = pTmp->pNext;
144349 db->nSavepoint = 0;
144350 db->nStatement = 0;
144351 db->isTransactionSavepoint = 0;
144361 FuncDestructor *pDestructor = p->u.pDestructor;
144363 pDestructor->nRef--;
144364 if( pDestructor->nRef==0 ){
144365 pDestructor->xDestroy(pDestructor->pUserData);
144380 for(i=0; i<db->nDb; i++){
144381 Schema *pSchema = db->aDb[i].pSchema;
144382 if( db->aDb[i].pSchema ){
144383 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
144389 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
144391 if( pMod->pEpoTab ){
144392 sqlite3VtabDisconnect(db, pMod->pEpoTab);
144408 assert( sqlite3_mutex_held(db->mutex) );
144409 if( db->pVdbe ) return 1;
144410 for(j=0; j<db->nDb; j++){
144411 Btree *pBt = db->aDb[j].pBt;
144422 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
144423 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
144429 sqlite3_mutex_enter(db->mutex);
144430 if( db->mTrace & SQLITE_TRACE_CLOSE ){
144431 db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
144439 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
144441 ** SQL statements below, as the v-table implementation may be storing
144452 sqlite3_mutex_leave(db->mutex);
144465 db->magic = SQLITE_MAGIC_ZOMBIE;
144499 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
144500 sqlite3_mutex_leave(db->mutex);
144512 ** they are reset. And that the required b-tree mutex is held to make
144520 for(j=0; j<db->nDb; j++){
144521 struct Db *pDb = &db->aDb[j];
144522 if( pDb->pBt ){
144523 sqlite3BtreeClose(pDb->pBt);
144524 pDb->pBt = 0;
144526 pDb->pSchema = 0;
144531 if( db->aDb[1].pSchema ){
144532 sqlite3SchemaClear(db->aDb[1].pSchema);
144538 assert( db->nDb<=2 );
144539 assert( db->aDb==db->aDbStatic );
144542 ** locks and does not require any further unlock-notify callbacks.
144546 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
144551 pNext = p->pNext;
144556 sqlite3HashClear(&db->aFunc);
144557 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
144567 sqlite3HashClear(&db->aCollSeq);
144569 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
144571 if( pMod->xDestroy ){
144572 pMod->xDestroy(pMod->pAux);
144577 sqlite3HashClear(&db->aModule);
144581 sqlite3ValueFree(db->pErr);
144584 sqlite3_free(db->auth.zAuthUser);
144585 sqlite3_free(db->auth.zAuthPW);
144588 db->magic = SQLITE_MAGIC_ERROR;
144590 /* The temp-database schema is allocated differently from the other schema
144596 sqlite3DbFree(db, db->aDb[1].pSchema);
144597 sqlite3_mutex_leave(db->mutex);
144598 db->magic = SQLITE_MAGIC_CLOSED;
144599 sqlite3_mutex_free(db->mutex);
144601 if( db->lookaside.bMalloced ){
144602 sqlite3_free(db->lookaside.pStart);
144609 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
144618 assert( sqlite3_mutex_held(db->mutex) );
144621 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
144623 ** modified the database schema. If the b-tree mutexes are not taken
144624 ** here, then another shared-cache connection might sneak in between
144628 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
144630 for(i=0; i<db->nDb; i++){
144631 Btree *p = db->aDb[i].pBt;
144642 if( (db->mDbFlags&DBFLAG_SchemaChange)!=0 && db->init.busy==0 ){
144649 db->nDeferredCons = 0;
144650 db->nDeferredImmCons = 0;
144651 db->flags &= ~SQLITE_DeferFKs;
144653 /* If one has been configured, invoke the rollback-hook callback */
144654 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
144655 db->xRollbackCallback(db->pRollbackArg);
144841 int timeout = db->busyTimeout;
144849 delay = delays[NDELAY-1];
144850 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
144853 delay = timeout - prior;
144856 sqlite3OsSleep(db->pVfs, delay*1000);
144860 int timeout = ((sqlite3 *)ptr)->busyTimeout;
144864 sqlite3OsSleep(db->pVfs, 1000000);
144873 ** If this routine returns non-zero, the lock is retried. If it
144878 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
144879 rc = p->xFunc(p->pArg, p->nBusy);
144881 p->nBusy = -1;
144883 p->nBusy++;
144900 sqlite3_mutex_enter(db->mutex);
144901 db->busyHandler.xFunc = xBusy;
144902 db->busyHandler.pArg = pArg;
144903 db->busyHandler.nBusy = 0;
144904 db->busyTimeout = 0;
144905 sqlite3_mutex_leave(db->mutex);
144927 sqlite3_mutex_enter(db->mutex);
144929 db->xProgress = xProgress;
144930 db->nProgressOps = (unsigned)nOps;
144931 db->pProgressArg = pArg;
144933 db->xProgress = 0;
144934 db->nProgressOps = 0;
144935 db->pProgressArg = 0;
144937 sqlite3_mutex_leave(db->mutex);
144952 db->busyTimeout = ms;
144964 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
144969 db->u1.isInterrupted = 1;
144994 assert( sqlite3_mutex_held(db->mutex) );
144999 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
145041 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
145042 if( db->nVdbeActive ){
145044 "unable to delete/modify user-function due to active statements");
145045 assert( !db->mallocFailed );
145053 assert(p || db->mallocFailed);
145063 pDestructor->nRef++;
145065 p->u.pDestructor = pDestructor;
145066 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
145067 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
145068 p->xSFunc = xSFunc ? xSFunc : xStep;
145069 p->xFinalize = xFinal;
145070 p->pUserData = pUserData;
145071 p->nArg = (u16)nArg;
145111 sqlite3_mutex_enter(db->mutex);
145118 pArg->xDestroy = xDestroy;
145119 pArg->pUserData = p;
145122 if( pArg && pArg->nRef==0 ){
145130 sqlite3_mutex_leave(db->mutex);
145151 sqlite3_mutex_enter(db->mutex);
145152 assert( !db->mallocFailed );
145153 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
145157 sqlite3_mutex_leave(db->mutex);
145167 ** this routine is a no-op. If the function does not exist, then create
145168 ** a new one that always throws a run-time error.
145183 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
145187 sqlite3_mutex_enter(db->mutex);
145193 sqlite3_mutex_leave(db->mutex);
145202 ** A NULL trace function means that no tracing is executes. A non-NULL
145216 sqlite3_mutex_enter(db->mutex);
145217 pOld = db->pTraceArg;
145218 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
145219 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
145220 db->pTraceArg = pArg;
145221 sqlite3_mutex_leave(db->mutex);
145226 /* Register a trace callback using the version-2 interface.
145239 sqlite3_mutex_enter(db->mutex);
145242 db->mTrace = mTrace;
145243 db->xTrace = xTrace;
145244 db->pTraceArg = pArg;
145245 sqlite3_mutex_leave(db->mutex);
145254 ** A NULL profile function means that no profiling is executes. A non-NULL
145271 sqlite3_mutex_enter(db->mutex);
145272 pOld = db->pProfileArg;
145273 db->xProfile = xProfile;
145274 db->pProfileArg = pArg;
145275 sqlite3_mutex_leave(db->mutex);
145283 ** If the invoked function returns non-zero, then the commit becomes a
145299 sqlite3_mutex_enter(db->mutex);
145300 pOld = db->pCommitArg;
145301 db->xCommitCallback = xCallback;
145302 db->pCommitArg = pArg;
145303 sqlite3_mutex_leave(db->mutex);
145324 sqlite3_mutex_enter(db->mutex);
145325 pRet = db->pUpdateArg;
145326 db->xUpdateCallback = xCallback;
145327 db->pUpdateArg = pArg;
145328 sqlite3_mutex_leave(db->mutex);
145349 sqlite3_mutex_enter(db->mutex);
145350 pRet = db->pRollbackArg;
145351 db->xRollbackCallback = xCallback;
145352 db->pRollbackArg = pArg;
145353 sqlite3_mutex_leave(db->mutex);
145369 sqlite3_mutex_enter(db->mutex);
145370 pRet = db->pPreUpdateArg;
145371 db->xPreUpdateCallback = xCallback;
145372 db->pPreUpdateArg = pArg;
145373 sqlite3_mutex_leave(db->mutex);
145430 ** into the write-ahead-log by this database connection.
145445 sqlite3_mutex_enter(db->mutex);
145446 pRet = db->pWalArg;
145447 db->xWalCallback = xCallback;
145448 db->pWalArg = pArg;
145449 sqlite3_mutex_leave(db->mutex);
145476 /* Initialize the output variables to -1 in case an error occurs. */
145477 if( pnLog ) *pnLog = -1;
145478 if( pnCkpt ) *pnCkpt = -1;
145485 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
145490 sqlite3_mutex_enter(db->mutex);
145498 db->busyHandler.nBusy = 0;
145506 if( db->nVdbeActive==0 ){
145507 db->u1.isInterrupted = 0;
145510 sqlite3_mutex_leave(db->mutex);
145518 ** to contains a zero-length string, all attached databases are
145522 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
145529 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
145538 ** associated with the specific b-tree being checkpointed is taken by
145542 ** checkpointed. If an error is encountered it is returned immediately -
145553 assert( sqlite3_mutex_held(db->mutex) );
145554 assert( !pnLog || *pnLog==-1 );
145555 assert( !pnCkpt || *pnCkpt==-1 );
145557 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
145559 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
145574 ** This function returns true if main-memory should be used instead of
145576 ** The value returned depends on the value of db->temp_store (runtime
145581 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
145582 ** ----------------- -------------- ------------------------------
145594 return ( db->temp_store==2 );
145597 return ( db->temp_store!=1 );
145610 ** Return UTF-8 encoded English language explanation of the most recent
145621 sqlite3_mutex_enter(db->mutex);
145622 if( db->mallocFailed ){
145625 testcase( db->pErr==0 );
145626 z = (char*)sqlite3_value_text(db->pErr);
145627 assert( !db->mallocFailed );
145629 z = sqlite3ErrStr(db->errCode);
145632 sqlite3_mutex_leave(db->mutex);
145638 ** Return UTF-16 encoded English language explanation of the most recent
145658 sqlite3_mutex_enter(db->mutex);
145659 if( db->mallocFailed ){
145662 z = sqlite3_value_text16(db->pErr);
145664 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
145665 z = sqlite3_value_text16(db->pErr);
145668 ** above. If this is the case, then the db->mallocFailed flag needs to
145674 sqlite3_mutex_leave(db->mutex);
145687 if( !db || db->mallocFailed ){
145690 return db->errCode & db->errMask;
145696 if( !db || db->mallocFailed ){
145699 return db->errCode;
145702 return db ? db->iSysErrno : 0;
145729 assert( sqlite3_mutex_held(db->mutex) );
145747 ** are no active VMs, invalidate any pre-compiled statements.
145750 if( pColl && pColl->xCmp ){
145751 if( db->nVdbeActive ){
145761 ** Also, collation destructor - CollSeq.xDel() - function may need
145764 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
145765 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
145769 if( p->enc==pColl->enc ){
145770 if( p->xDel ){
145771 p->xDel(p->pUser);
145773 p->xCmp = 0;
145781 pColl->xCmp = xCompare;
145782 pColl->pUser = pCtx;
145783 pColl->xDel = xDel;
145784 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
145805 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
145850 ** If an invalid limit index is supplied, report -1.
145864 return -1;
145868 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
145869 ** there is a hard upper bound set at compile-time by a C preprocessor
145886 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
145890 return -1;
145892 oldLimit = db->aLimit[limitId];
145893 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
145895 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
145897 db->aLimit[limitId] = newLimit;
145899 return oldLimit; /* IMP: R-53341-35419 */
145903 ** This function is used to parse both URIs and non-URI filenames passed by the
145909 ** query parameter. The second argument contains the URI (or non-URI filename)
145928 const char *zUri, /* Nul-terminated URI to parse */
145943 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
145944 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
145945 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
145954 ** method that there may be extra parameters following the file-name. */
145981 iIn-7, &zUri[7]);
145994 ** 0: Parsing file-name.
146032 if( zFile[iOut-1]==0 ){
146034 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
146160 ** is UTF-8 encoded.
146163 const char *zFilename, /* Database filename UTF-8 encoded */
146230 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
146231 if( db->mutex==0 ){
146237 sqlite3MutexWarnOnContention(db->mutex);
146240 sqlite3_mutex_enter(db->mutex);
146241 db->errMask = 0xff;
146242 db->nDb = 2;
146243 db->magic = SQLITE_MAGIC_BUSY;
146244 db->aDb = db->aDbStatic;
146246 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
146247 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
146248 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
146249 db->autoCommit = 1;
146250 db->nextAutovac = -1;
146251 db->szMmap = sqlite3GlobalConfig.szMmap;
146252 db->nextPagesize = 0;
146253 db->nMaxSorterMmap = 0x7FFFFFFF;
146254 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
146286 sqlite3HashInit(&db->aCollSeq);
146288 sqlite3HashInit(&db->aModule);
146291 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
146292 ** and UTF-16, so add a version for each to avoid any unnecessary
146295 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
146303 if( db->mallocFailed ){
146306 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
146309 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
146310 assert( db->pDfltColl!=0 );
146315 ** Throw an error if any non-sense combination is used. If we
146324 db->openFlags = flags;
146332 rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
146334 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
146344 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
146353 sqlite3BtreeEnter(db->aDb[0].pBt);
146354 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
146355 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
146356 sqlite3BtreeLeave(db->aDb[0].pBt);
146357 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
146362 db->aDb[0].zDbSName = "main";
146363 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
146364 db->aDb[1].zDbSName = "temp";
146365 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
146367 db->magic = SQLITE_MAGIC_OPEN;
146368 if( db->mallocFailed ){
146372 /* Register all built-in functions, but do not attempt to read the
146381 /* Register any built-in FTS5 module before loading the automatic
146384 if( !db->mallocFailed && rc==SQLITE_OK ){
146389 /* Load automatic extensions - extensions that have been registered
146401 if( !db->mallocFailed ){
146408 if( !db->mallocFailed && rc==SQLITE_OK ){
146415 if( !db->mallocFailed && rc==SQLITE_OK ){
146421 if( !db->mallocFailed && rc==SQLITE_OK ){
146427 if( !db->mallocFailed && rc==SQLITE_OK){
146433 if( !db->mallocFailed && rc==SQLITE_OK){
146439 if( !db->mallocFailed && rc==SQLITE_OK){
146445 if( !db->mallocFailed && rc==SQLITE_OK){
146451 if( !db->mallocFailed && rc==SQLITE_OK){
146456 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
146457 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
146461 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
146462 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
146468 /* Enable the lookaside-malloc subsystem */
146476 assert( db->mutex!=0 || isThreadsafe==0
146478 sqlite3_mutex_leave(db->mutex);
146486 db->magic = SQLITE_MAGIC_SICK;
146528 const char *filename, /* Database filename (UTF-8) */
146544 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
146558 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
146605 sqlite3_mutex_enter(db->mutex);
146606 assert( !db->mallocFailed );
146609 sqlite3_mutex_leave(db->mutex);
146630 sqlite3_mutex_enter(db->mutex);
146631 assert( !db->mallocFailed );
146632 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
146638 sqlite3_mutex_leave(db->mutex);
146655 sqlite3_mutex_enter(db->mutex);
146656 db->xCollNeeded = xCollNeeded;
146657 db->xCollNeeded16 = 0;
146658 db->pCollNeededArg = pCollNeededArg;
146659 sqlite3_mutex_leave(db->mutex);
146676 sqlite3_mutex_enter(db->mutex);
146677 db->xCollNeeded = 0;
146678 db->xCollNeeded16 = xCollNeeded16;
146679 db->pCollNeededArg = pCollNeededArg;
146680 sqlite3_mutex_leave(db->mutex);
146708 return db->autoCommit;
146720 ** a low-level error is first detected.
146758 ** This is a convenience routine that makes sure that all thread-specific
146761 ** SQLite no longer uses thread-specific data so this routine is now a
146762 ** no-op. It is retained for historical compatibility.
146781 int *pAutoinc /* OUTPUT: True if column is auto-increment */
146802 sqlite3_mutex_enter(db->mutex);
146811 if( !pTab || pTab->pSelect ){
146820 for(iCol=0; iCol<pTab->nCol; iCol++){
146821 pCol = &pTab->aCol[iCol];
146822 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
146826 if( iCol==pTab->nCol ){
146828 iCol = pTab->iPKey;
146829 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
146849 zCollSeq = pCol->zColl;
146850 notnull = pCol->notNull!=0;
146851 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
146852 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
146883 sqlite3_mutex_leave(db->mutex);
146910 sqlite3_mutex_enter(db->mutex);
146911 db->errMask = onoff ? 0xffffffff : 0xff;
146912 sqlite3_mutex_leave(db->mutex);
146926 sqlite3_mutex_enter(db->mutex);
146945 }else if( fd->pMethods ){
146952 sqlite3_mutex_leave(db->mutex);
147000 ** is an array of integers that defines the test. Return -1 on a
147001 ** memory allocation error, 0 on success, or non-zero for an error.
147075 ** This action provides a run-time test to see whether or not
147076 ** assert() was enabled at compile-time. If X is true and assert()
147085 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
147094 ** This action provides a run-time test to see how the ALWAYS and
147095 ** NEVER macros were defined at compile-time.
147100 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
147102 ** hard-coded to true or else it asserts if its argument is false.
147103 ** The first behavior (hard-coded to true) is the case if
147108 ** The run-time test procedure might look something like this:
147111 ** // ALWAYS() and NEVER() are no-op pass-through macros
147127 ** The integer returned reveals the byte-order of the computer on which
147130 ** 1 big-endian, determined at run-time
147131 ** 10 little-endian, determined at run-time
147132 ** 432101 big-endian, determined at compile-time
147133 ** 123410 little-endian, determined at compile-time
147148 sqlite3_mutex_enter(db->mutex);
147149 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
147150 sqlite3_mutex_leave(db->mutex);
147165 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
147189 ** If parameter onoff is non-zero, configure the wrappers so that all
147200 ** Set or clear a flag that indicates that the database file is always well-
147204 ** that demonstrat invariants on well-formed database files.
147238 db->nMaxSorterMmap = va_arg(ap, int);
147257 ** or off. "tnum" is the root page of the b-tree to which the imposter
147270 sqlite3_mutex_enter(db->mutex);
147271 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
147272 db->init.busy = db->init.imposterTable = va_arg(ap,int);
147273 db->init.newTnum = va_arg(ap,int);
147274 if( db->init.busy==0 && db->init.newTnum>0 ){
147277 sqlite3_mutex_leave(db->mutex);
147285 ** -DYYCOVERAGE) writes a report onto "out" that shows all
147335 ** Return a 64-bit integer value for a query parameter.
147355 return iDb<0 ? 0 : db->aDb[iDb].pBt;
147375 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
147383 return -1;
147387 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
147408 sqlite3_mutex_enter(db->mutex);
147410 if( db->autoCommit==0 ){
147413 Btree *pBt = db->aDb[iDb].pBt;
147423 sqlite3_mutex_leave(db->mutex);
147429 ** Open a read-transaction on the snapshot idendified by pSnapshot.
147444 sqlite3_mutex_enter(db->mutex);
147445 if( db->autoCommit==0 ){
147449 Btree *pBt = db->aDb[iDb].pBt;
147460 sqlite3_mutex_leave(db->mutex);
147480 sqlite3_mutex_enter(db->mutex);
147483 Btree *pBt = db->aDb[iDb].pBt;
147492 sqlite3_mutex_leave(db->mutex);
147507 ** Given the name of a compile-time option, return true if that option
147543 ** Return the N-th compile-time option string. If N is out of range,
147605 ** 1) Each entry in the list has a non-NULL value for either
147617 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
147622 assert( p->pUnlockConnection || p->pBlockingConnection );
147625 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
147626 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
147627 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
147628 assert( db==0 || p->pUnlockConnection!=db );
147629 assert( db==0 || p->pBlockingConnection!=db );
147639 ** db is not currently a part of the list, this function is a no-op.
147644 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
147646 *pp = (*pp)->pNextBlocked;
147661 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
147662 pp=&(*pp)->pNextBlocked
147664 db->pNextBlocked = *pp;
147686 ** Register an unlock-notify callback.
147691 ** cache. pOther is found by looking at db->pBlockingConnection.
147697 ** a deadlock.
147713 sqlite3_mutex_enter(db->mutex);
147718 db->pBlockingConnection = 0;
147719 db->pUnlockConnection = 0;
147720 db->xUnlockNotify = 0;
147721 db->pUnlockArg = 0;
147722 }else if( 0==db->pBlockingConnection ){
147731 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
147733 rc = SQLITE_LOCKED; /* Deadlock detected. */
147735 db->pUnlockConnection = db->pBlockingConnection;
147736 db->xUnlockNotify = xNotify;
147737 db->pUnlockArg = pArg;
147744 assert( !db->mallocFailed );
147746 sqlite3_mutex_leave(db->mutex);
147758 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
147761 db->pBlockingConnection = pBlocker;
147777 ** set to db, then invoke the configured unlock-notify callback and
147785 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
147795 /* This loop runs once for each entry in the blocked-connections list. */
147796 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
147800 if( p->pBlockingConnection==db ){
147801 p->pBlockingConnection = 0;
147805 if( p->pUnlockConnection==db ){
147806 assert( p->xUnlockNotify );
147807 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
147826 ** be passed to the unlock-notify callback is larger than the
147833 ** closed and the unlock-notify callbacks on blocked connections
147835 ** indefinitely for an unlock-notify callback that will never
147838 ** Instead, invoke the unlock-notify callback with the context
147841 ** requiring any dynamic allocation. This is sub-optimal because
147855 aArg[nArg++] = p->pUnlockArg;
147856 xUnlockNotify = p->xUnlockNotify;
147857 p->pUnlockConnection = 0;
147858 p->xUnlockNotify = 0;
147859 p->pUnlockArg = 0;
147863 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
147865 *pp = p->pNextBlocked;
147866 p->pNextBlocked = 0;
147868 pp = &p->pNextBlocked;
147906 ** This is an SQLite module implementing full-text search.
147919 /* The full-text index is stored in a series of b+tree (-like)
147928 ** The basic unit of encoding is a variable-length integer called a
147929 ** varint. We encode variable-length integers in little-endian order
147936 ** 7 bits - A
147937 ** 14 bits - BA
147938 ** 21 bits - BBA
147942 ** the encoding is not the same. SQLite varints are big-endian
147944 ** little-endian and can be up to 10 bytes in length (in theory).
147954 ** A doclist (document list) holds a docid-sorted list of hits for a
147960 ** FTS3 used to optionally store character offsets using a compile-time
147993 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
147996 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
147998 ** (72-2) and then terminates with the 0 at K.
148000 ** A "position-list" is the list of positions for multiple columns for
148001 ** a single docid. A "column-list" is the set of positions for a single
148002 ** column. Hence, a position-list consists of one or more column-lists,
148003 ** a document record consists of a docid followed by a position-list and
148007 ** array of varint-encoded docids.
148022 ** (further terms are delta-encoded)
148068 ** (further terms are delta-encoded)
148098 ** The segment directory in table %_segdir stores meta-information for
148110 ** The meta-information in the segment directory is:
148111 ** level - segment level (see below)
148112 ** idx - index within level
148113 ** - (level,idx uniquely identify a segment)
148114 ** start_block - first leaf node
148115 ** leaves_end_block - last leaf node
148116 ** end_block - last block (including interior nodes)
148117 ** root - contents of root node
148146 ** a tiny bit slower (perhaps due to more overhead in merge-time
148162 ** costs), and infrequent and non-existent terms still seem to be fast
148165 ** TODO(shess) That said, it would be nice to have a better query-side
148173 ** Since we're using a segmented structure, with no docid-oriented
148238 ** Defines the interface to tokenizers used by fulltext-search. There
148296 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
148343 ** normalized version of the token (i.e. after any case-folding and/or
148357 ** nul-terminated. This should either be fixed, or pInput/nBytes
148409 ** This is the header file for the generic hash-table implementation
148411 ** hash table implementation for the full-text indexing module.
148422 ** The internals of this structure are intended to be opaque -- client
148442 ** structure. All elements are stored on a single doubly-linked list.
148457 ** (including the null-terminator, if any). Case
148498 #define fts3HashFirst(H) ((H)->first)
148499 #define fts3HashNext(E) ((E)->next)
148500 #define fts3HashData(E) ((E)->data)
148501 #define fts3HashKey(E) ((E)->pKey)
148502 #define fts3HashKeysize(E) ((E)->nKey)
148507 #define fts3HashCount(H) ((H)->count)
148546 ** a collision when building an amalgamation with built-in FTS3.
148565 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
148567 ** as one or more b+-trees in the %_segments and %_segdir tables.
148569 ** It is possible to determine which index a b+-tree belongs to based on the
148571 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
148577 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
148584 ** make it a no-op.
148591 ** Terminator values for position-lists and column-lists.
148593 #define POS_COLUMN (1) /* Column-list terminator */
148594 #define POS_END (0) /* Position-list terminator */
148622 typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
148623 typedef short int i16; /* 2-byte (or larger) signed integer */
148624 typedef unsigned int u32; /* 4-byte unsigned integer */
148625 typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
148626 typedef sqlite3_int64 i64; /* 8-byte signed integer */
148717 ** time must share a common language-id (see the FTS4 langid= feature).
148720 ** A single FTS4 table may have multiple full-text indexes. For each index
148754 ** by special insert command 'test-no-incr-doclist'. */
148777 char *aDoclist; /* List of docids for full-text queries */
148796 ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
148810 #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
148813 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
148815 ** above. The upper 16-bits contain a combination of the following
148816 ** bits, used to describe extra constraints on full-text searches.
148836 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
148849 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
148895 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
148963 #define FTS3_SEGCURSOR_PENDING -1
148964 #define FTS3_SEGCURSOR_ALL -2
148993 int nAdvance; /* How many seg-readers to advance */
149121 ** Write a 64-bit variable-length integer to memory starting at p[0].
149132 q[-1] &= 0x7f; /* turn off high bit in final byte */
149133 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
149134 return (int) (q - (unsigned char *)p);
149145 ** Read a 64-bit variable-length integer from memory starting at p[0].
149168 return (int)(p - pStart);
149173 ** a non-negative 32-bit integer before it is returned.
149209 ** Convert an SQL-style quoted string into a normal string by removing
149210 ** the quote characters. The conversion is done in-place. If the
149212 ** is a no-op.
149230 /* If the first byte was a '[', then the close-quote character is a ']' */
149259 ** varint that is part of a doclist (or position-list, or any other list
149276 ** clear on character p[-1]. */
149277 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
149292 assert( p->nPendingData==0 );
149293 assert( p->pSegments==0 );
149296 sqlite3_finalize(p->pSeekStmt);
149297 for(i=0; i<SizeofArray(p->aStmt); i++){
149298 sqlite3_finalize(p->aStmt[i]);
149300 sqlite3_free(p->zSegmentsTbl);
149301 sqlite3_free(p->zReadExprlist);
149302 sqlite3_free(p->zWriteExprlist);
149303 sqlite3_free(p->zContentTbl);
149304 sqlite3_free(p->zLanguageid);
149307 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
149329 ** If *pRc is initially non-zero then this routine is a no-op.
149357 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
149358 sqlite3 *db = p->db; /* Database handle */
149361 if( p->zContentTbl==0 ){
149362 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
149364 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
149365 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
149366 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
149367 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
149382 ** If *pRc is non-zero when this function is called, it is a no-op.
149394 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
149395 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
149398 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
149399 for(i=1; zCols && i<p->nColumn; i++){
149400 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
149406 zCols, p->zName, zLanguageid
149411 rc = sqlite3_declare_vtab(p->db, zSql);
149424 fts3DbExec(pRc, p->db,
149427 p->zDb, p->zName
149429 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
149437 ** If the p->bHasDocsize boolean is true (indicating that this is an
149444 sqlite3 *db = p->db; /* The database connection */
149446 if( p->zContentTbl==0 ){
149447 const char *zLanguageid = p->zLanguageid;
149452 for(i=0; zContentCols && i<p->nColumn; i++){
149453 char *z = p->azColumn[i];
149464 p->zDb, p->zName, zContentCols
149472 p->zDb, p->zName
149484 p->zDb, p->zName
149486 if( p->bHasDocsize ){
149489 p->zDb, p->zName
149492 assert( p->bHasStat==p->bFts4 );
149493 if( p->bHasStat ){
149500 ** Store the current database page-size in bytes in p->nPgsz.
149502 ** If *pRc is non-zero when this function is called, it is a no-op.
149512 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
149516 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
149519 p->nPgsz = sqlite3_column_int(pStmt, 0);
149522 p->nPgsz = 1024;
149526 assert( p->nPgsz>0 || rc!=SQLITE_OK );
149553 *pnKey = (int)(zCsr-z);
149589 ** Return a copy of input string zInput enclosed in double-quotes (") and
149592 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
149627 ** table has the three user-defined columns "a", "b", and "c", the following
149635 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
149646 if( p->zContentTbl==0 ){
149653 for(i=0; i<p->nColumn; i++){
149654 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
149656 if( p->zLanguageid ){
149662 for(i=0; i<p->nColumn; i++){
149663 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
149665 if( p->zLanguageid ){
149666 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
149670 p->zDb,
149671 (p->zContentTbl ? p->zContentTbl : p->zName),
149672 (p->zContentTbl ? "" : "_content")
149680 ** user-defined text column).
149685 ** user-defined text columns, the following string is returned:
149692 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
149709 for(i=0; i<p->nColumn; i++){
149712 if( p->zLanguageid ){
149720 ** This function interprets the string at (*pp) as a non-negative integer
149738 nInt = nInt * 10 + (p[0] - '0');
149798 nIndex--;
149799 i--;
149824 ** of each columns name, including the nul-terminator.
149853 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
149868 ** nul-terminator byte. */
149906 ** argv[0] -> module name ("fts3" or "fts4")
149907 ** argv[1] -> database name
149908 ** argv[2] -> table name
149909 ** argv[...] -> "column name" and other module argument fields.
149957 nByte = sizeof(const char *) * (argc-2);
150002 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
150003 { "prefix", 6 }, /* 1 -> PREFIX */
150004 { "compress", 8 }, /* 2 -> COMPRESS */
150005 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
150006 { "order", 5 }, /* 4 -> ORDER */
150007 { "content", 7 }, /* 5 -> CONTENT */
150008 { "languageid", 10 }, /* 6 -> LANGUAGEID */
150009 { "notindexed", 10 } /* 7 -> NOTINDEXED */
150018 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
150119 nCol--;
150162 p->db = db;
150163 p->nColumn = nCol;
150164 p->nPendingData = 0;
150165 p->azColumn = (char **)&p[1];
150166 p->pTokenizer = pTokenizer;
150167 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
150168 p->bHasDocsize = (isFts4 && bNoDocsize==0);
150169 p->bHasStat = (u8)isFts4;
150170 p->bFts4 = (u8)isFts4;
150171 p->bDescIdx = (u8)bDescIdx;
150172 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
150173 p->zContentTbl = zContent;
150174 p->zLanguageid = zLanguageid;
150177 TESTONLY( p->inTransaction = -1 );
150178 TESTONLY( p->mxSavepoint = -1 );
150180 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
150181 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
150182 p->nIndex = nIndex;
150184 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
150186 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
150189 zCsr = (char *)&p->abNotindexed[nCol];
150190 p->zName = zCsr;
150193 p->zDb = zCsr;
150207 p->azColumn[iCol] = zCsr;
150214 int n = (int)strlen(p->azColumn[iCol]);
150218 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
150220 p->abNotindexed[iCol] = 1;
150238 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
150239 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
150253 p->bHasStat = 2;
150256 /* Figure out the page-size for the database. This is required in order to
150259 p->nNodeSize = p->nPgsz-35;
150278 pTokenizer->pModule->xDestroy(pTokenizer);
150281 assert( p->pSegments==0 );
150282 *ppVTab = &p->base;
150313 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
150315 ** support estimatedRows. In that case this function is a no-op.
150320 pIdxInfo->estimatedRows = nRow;
150326 ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
150328 ** support index-info flags. In that case this function is a no-op.
150333 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
150343 ** 2. Full-text search using a MATCH operator on a non-docid column.
150349 int iCons = -1; /* Index of constraint to use */
150351 int iLangidCons = -1; /* Index of langid=x constraint, if present */
150352 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
150353 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
150360 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
150361 pInfo->estimatedCost = 5000000;
150362 for(i=0; i<pInfo->nConstraint; i++){
150364 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
150365 if( pCons->usable==0 ){
150366 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
150372 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
150373 pInfo->estimatedCost = 1e50;
150380 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
150383 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
150384 pInfo->idxNum = FTS3_DOCID_SEARCH;
150385 pInfo->estimatedCost = 1.0;
150389 /* A MATCH constraint. Use a full-text search.
150398 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
150399 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
150401 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
150402 pInfo->estimatedCost = 2.0;
150407 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
150408 && pCons->iColumn==p->nColumn + 2
150414 switch( pCons->op ){
150429 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
150433 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
150434 pInfo->aConstraintUsage[iCons].omit = 1;
150437 pInfo->idxNum |= FTS3_HAVE_LANGID;
150438 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
150441 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
150442 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
150445 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
150446 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
150452 if( pInfo->nOrderBy==1 ){
150453 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
150454 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
150455 if( pOrder->desc ){
150456 pInfo->idxStr = "DESC";
150458 pInfo->idxStr = "ASC";
150460 pInfo->orderByConsumed = 1;
150464 assert( p->pSegments==0 );
150489 ** Finalize the statement handle at pCsr->pStmt.
150496 if( pCsr->bSeekStmt ){
150497 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
150498 if( p->pSeekStmt==0 ){
150499 p->pSeekStmt = pCsr->pStmt;
150500 sqlite3_reset(pCsr->pStmt);
150501 pCsr->pStmt = 0;
150503 pCsr->bSeekStmt = 0;
150505 sqlite3_finalize(pCsr->pStmt);
150515 sqlite3_free(pCsr->aDoclist);
150516 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
150517 sqlite3Fts3ExprFree(pCsr->pExpr);
150518 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
150527 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
150529 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
150535 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
150540 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
150545 if( pCsr->pStmt==0 ){
150546 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
150548 if( p->pSeekStmt ){
150549 pCsr->pStmt = p->pSeekStmt;
150550 p->pSeekStmt = 0;
150552 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
150554 rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
150557 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
150563 ** Position the pCsr->pStmt statement so that it is on the row
150569 if( pCsr->isRequireSeek ){
150572 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
150573 pCsr->isRequireSeek = 0;
150574 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
150577 rc = sqlite3_reset(pCsr->pStmt);
150578 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
150580 ** table is missing a row that is present in the full-text index.
150583 pCsr->isEof = 1;
150597 ** a b-tree for a term or term prefix. The node data is passed to this
150602 ** of the child node that heads the sub-tree that may contain the term.
150604 ** If piLast is not NULL, then *piLast is set to the right-most child node
150605 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
150627 ** interior node. Then load the blockid of the left-child of the b-tree
150660 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){
150681 ** to the term from the interior node, then all terms on the sub-tree
150713 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
150714 ** contains a term. This function searches the sub-tree headed by the zNode
150719 ** left-most leaf node in the tree that may contain the specified term.
150721 ** right-most leaf node that may contain a term for which the specified
150777 ** This function is used to create delta-encoded serialized lists of FTS3
150785 assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
150786 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
150792 ** start of a position-list. After it returns, *ppPoslist points to the
150793 ** first byte after the position-list.
150811 ** of some other, multi-byte, value.
150813 ** The following while-loop moves pEnd to point to the first byte that is not
150816 ** last byte in the position-list.
150825 int n = (int)(pEnd - *ppPoslist);
150836 ** start of a column-list. After it returns, *ppPoslist points to the
150837 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
150839 ** A column-list is list of delta-encoded positions for a single column
150842 ** The column-list is terminated either by a POS_COLUMN varint (1) or
150844 ** the POS_COLUMN or POS_END that terminates the column-list.
150846 ** If pp is not NULL, then the contents of the column-list are copied
150855 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
150856 ** not part of a multi-byte varint.
150863 int n = (int)(pEnd - *ppPoslist);
150873 ** Value used to signify the end of an position-list. This is safe because
150879 ** This function is used to help parse position-lists. When this function is
150880 ** called, *pp may point to the start of the next varint in the position-list
150881 ** being parsed, or it may point to 1 byte past the end of the position-list
150885 ** If *pp points past the end of the current position-list, set *pi to
150892 ** in the position-list. Because positions are delta-encoded, the value
150897 char **pp, /* IN/OUT: Pointer into position-list buffer */
150898 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
150902 *pi -= 2;
150964 /* At this point, both p1 and p2 point to the start of column-lists
150966 ** A column-list is a list of non-negative delta-encoded varints, each
150977 iPrev -= 2;
151004 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
151019 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
151066 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
151067 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
151075 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
151081 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
151084 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
151107 ** column-number in the position list.
151135 ** Merge two position-lists as required by the NEAR operator. The argument
151141 ** Position list *pp1 corresponds to the left-hand side of the NEAR
151146 ** The output position list - written to *pp - is a copy of *pp2 with those
151219 *pVal -= iVal;
151234 ** If *pbFirst is non-zero when this function is called, then the value
151235 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
151236 ** (if bDescIdx is non-zero).
151250 iWrite = iVal - *piPrev;
151252 iWrite = *piPrev - iVal;
151264 ** arguments are 64-bit docid values. If the value of the stack variable
151265 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
151266 ** Otherwise, (i2-i1).
151271 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
151278 ** passed a non-zero value.
151311 ** current and previous docid (a positive number - since the list is in
151336 aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
151362 *pnOut = (int)(p-aOut);
151363 assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
151369 ** the output contains a copy of each position from the right-hand input
151370 ** doclist for which there is a position in the left-hand input doclist
151375 ** order, it should be passed a non-zero value.
151377 ** The right-hand input doclist is overwritten by this function.
151433 *pnRight = (int)(p - aOut);
151510 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
151511 if( pTS->aaOutput[i] ){
151513 aOut = pTS->aaOutput[i];
151514 nOut = pTS->anOutput[i];
151515 pTS->aaOutput[i] = 0;
151520 int rc = fts3DoclistOrMerge(p->bDescIdx,
151521 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
151528 sqlite3_free(pTS->aaOutput[i]);
151530 pTS->aaOutput[i] = 0;
151537 pTS->aaOutput[0] = aOut;
151538 pTS->anOutput[0] = nOut;
151550 ** doclists to merge, the merging is done pair-wise using the TermSelect
151562 if( pTS->aaOutput[0]==0 ){
151572 ** not true for order=DESC. For example, a doclist containing (1, -1)
151573 ** may be smaller than (-1), as in the first example the -1 may be stored
151574 ** as a single-byte delta, whereas in the second it must be stored as a
151579 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
151580 pTS->anOutput[0] = nDoclist;
151581 if( pTS->aaOutput[0] ){
151582 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
151591 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
151592 if( pTS->aaOutput[iOut]==0 ){
151594 pTS->aaOutput[iOut] = aMerge;
151595 pTS->anOutput[iOut] = nMerge;
151601 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
151602 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
151610 sqlite3_free(pTS->aaOutput[iOut]);
151611 pTS->aaOutput[iOut] = 0;
151615 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
151616 pTS->aaOutput[iOut] = aMerge;
151617 pTS->anOutput[iOut] = nMerge;
151626 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
151632 if( (pCsr->nSegment%16)==0 ){
151634 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
151635 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
151640 pCsr->apSegment = apNew;
151642 pCsr->apSegment[pCsr->nSegment++] = pNew;
151647 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
151656 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
151668 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
151669 ** for the pending-terms. If this is a scan, then this call must be being
151674 if( iLevel<0 && p->aIndex ){
151706 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
151724 ** Set up a cursor object for iterating through a full-text index or a
151729 int iLangid, /* Language-id to search */
151730 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
151738 assert( iIndex>=0 && iIndex<p->nIndex );
151789 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
151798 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
151801 for(i=1; bFound==0 && i<p->nIndex; i++){
151802 if( p->aIndex[i].nPrefix==nTerm ){
151804 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
151807 pSegcsr->bLookup = 1;
151811 for(i=1; bFound==0 && i<p->nIndex; i++){
151812 if( p->aIndex[i].nPrefix==nTerm+1 ){
151814 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
151819 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
151827 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
151830 pSegcsr->bLookup = !isPrefix;
151853 int iColumn, /* Column to query (or -ve for all columns) */
151858 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
151859 TermSelect tsc; /* Object for pair-wise doclist merging */
151862 pSegcsr = pTok->pSegcsr;
151866 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
151867 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
151868 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
151870 filter.zTerm = pTok->z;
151871 filter.nTerm = pTok->n;
151877 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
151894 pTok->pSegcsr = 0;
151903 ** contains a position-list following each docid. Otherwise, it is assumed
151925 ** the next row that matches. For a full-table scan, this will be
151930 ** even if we reach end-of-file. The fts3EofMethod() will be called
151936 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
151937 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
151938 pCsr->isEof = 1;
151939 rc = sqlite3_reset(pCsr->pStmt);
151941 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
151947 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
151954 ** Constants for the largest and smallest possible 64-bit signed integers.
151955 ** These macros are designed to work correctly on both 32-bit and 64-bit
151960 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
151965 ** converted to a 64-bit signed integer. Otherwise, return a copy of
151990 ** column on the left-hand side of the MATCH operator is column
151991 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
152004 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
152017 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
152018 assert( p->pSegments==0 );
152032 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
152033 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
152036 pCsr->bDesc = (idxStr[0]=='D');
152038 pCsr->bDesc = p->bDescIdx;
152040 pCsr->eSearch = (i16)eSearch;
152043 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
152050 pCsr->iLangid = 0;
152051 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
152053 assert( p->base.zErrMsg==0 );
152054 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
152055 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
152056 &p->base.zErrMsg
152065 pCsr->pNextId = pCsr->aDoclist;
152066 pCsr->iPrevId = 0;
152069 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
152071 ** full-text query or docid lookup, the statement retrieves a single
152078 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
152079 (pCsr->bDesc ? "DESC" : "ASC")
152083 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
152087 rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
152095 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
152109 if( pCsr->isEof ){
152111 pCsr->isEof = 1;
152113 return pCsr->isEof;
152124 *pRowid = pCsr->iPrevId;
152134 ** (iCol < p->nColumn) -> The value of the iCol'th user column.
152135 ** (iCol == p->nColumn) -> Magic column with the same name as the table.
152136 ** (iCol == p->nColumn+1) -> Docid column
152137 ** (iCol == p->nColumn+2) -> Langid column
152146 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
152149 assert( iCol>=0 && iCol<=p->nColumn+2 );
152151 switch( iCol-p->nColumn ){
152153 /* The special 'table-name' column */
152159 sqlite3_result_int64(pCtx, pCsr->iPrevId);
152163 if( pCsr->pExpr ){
152164 sqlite3_result_int64(pCtx, pCsr->iLangid);
152166 }else if( p->zLanguageid==0 ){
152170 iCol = p->nColumn;
152171 /* fall-through */
152175 /* A user column. Or, if this is a full-table scan, possibly the
152176 ** language-id column. Seek the cursor. */
152178 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
152179 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
152184 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
152203 ** Implementation of xSync() method. Flush the contents of the pending-terms
152204 ** hash-table to the database.
152208 /* Following an incremental-merge operation, assuming that the input
152213 ** if the height of the input segment b-trees is N, and input segments
152215 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
152216 ** small - often between 0 and 2. So the overhead of the incremental
152224 ** as it would also be required by a crisis-merge that used the same input
152227 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
152231 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
152235 && p->nLeafAdd>(nMinMerge/16)
152236 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
152239 int A; /* Incr-merge parameter A */
152243 A = p->nLeafAdd * mxLevel;
152245 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
152248 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
152254 ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
152260 if( p->bHasStat==2 ){
152261 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
152263 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
152265 p->bHasStat = (res==SQLITE_OK);
152279 assert( p->pSegments==0 );
152280 assert( p->nPendingData==0 );
152281 assert( p->inTransaction!=1 );
152282 TESTONLY( p->inTransaction = 1 );
152283 TESTONLY( p->mxSavepoint = -1; );
152284 p->nLeafAdd = 0;
152289 ** Implementation of xCommit() method. This is a no-op. The contents of
152290 ** the pending-terms hash-table have already been flushed into the database
152296 assert( p->nPendingData==0 );
152297 assert( p->inTransaction!=0 );
152298 assert( p->pSegments==0 );
152299 TESTONLY( p->inTransaction = 0 );
152300 TESTONLY( p->mxSavepoint = -1; );
152305 ** Implementation of xRollback(). Discard the contents of the pending-terms
152306 ** hash-table. Any changes made to the database are reverted by SQLite.
152311 assert( p->inTransaction!=0 );
152312 TESTONLY( p->inTransaction = 0 );
152313 TESTONLY( p->mxSavepoint = -1; );
152319 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
152324 char *p = &(*ppPoslist)[-2];
152328 while( p>pStart && (c=*p--)==0 );
152334 c = *p--;
152378 sqlite3_result_error(pContext, zErr, -1);
152397 int iCol = -1;
152401 ** the non-overloaded version would have been called instead of this one).
152407 "wrong number of arguments to function snippet()", -1);
152422 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
152470 p = (Fts3Table *)pCursor->base.pVtab;
152477 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
152480 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
152552 sqlite3 *db = p->db; /* Database connection */
152562 ** flushes the pending terms table. But leave the (no-op) call to
152565 assert( p->nPendingData==0 );
152570 if( p->zContentTbl==0 ){
152573 p->zDb, p->zName, zName
152577 if( p->bHasDocsize ){
152580 p->zDb, p->zName, zName
152583 if( p->bHasStat ){
152586 p->zDb, p->zName, zName
152591 p->zDb, p->zName, zName
152595 p->zDb, p->zName, zName
152603 ** Flush the contents of the pending-terms table to disk.
152608 assert( ((Fts3Table *)pVtab)->inTransaction );
152609 assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
152610 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
152611 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
152620 ** This is a no-op.
152626 assert( p->inTransaction );
152627 assert( p->mxSavepoint >= iSavepoint );
152628 TESTONLY( p->mxSavepoint = iSavepoint-1 );
152640 assert( p->inTransaction );
152641 assert( p->mxSavepoint >= iSavepoint );
152642 TESTONLY( p->mxSavepoint = iSavepoint );
152685 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
152738 /* Allocate and initialize the hash-table used to store tokenizers. */
152746 /* Load the built-in tokenizers into the hash table */
152768 /* Create the virtual table wrapper around the hash-table and overload
152774 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
152809 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
152811 ** within the union of all segments of a b-tree. Hence the name.
152814 ** segment b-tree (if the term is not a prefix or it is a prefix for which
152815 ** there exists prefix b-tree of the right length) then it may be traversed
152816 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
152827 if( pExpr->eType==FTSQUERY_PHRASE ){
152829 int nToken = pExpr->pPhrase->nToken;
152832 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
152834 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
152841 assert( pExpr->pPhrase->iDoclistToken==0 );
152842 pExpr->pPhrase->iDoclistToken = -1;
152844 *pnOr += (pExpr->eType==FTSQUERY_OR);
152845 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
152846 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
152853 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
152869 assert( iToken!=p->iDoclistToken );
152872 sqlite3_free(p->doclist.aAll);
152873 p->doclist.aAll = 0;
152874 p->doclist.nAll = 0;
152877 else if( p->iDoclistToken<0 ){
152878 p->doclist.aAll = pList;
152879 p->doclist.nAll = nList;
152882 else if( p->doclist.aAll==0 ){
152893 if( p->iDoclistToken<iToken ){
152894 pLeft = p->doclist.aAll;
152895 nLeft = p->doclist.nAll;
152898 nDiff = iToken - p->iDoclistToken;
152900 pRight = p->doclist.aAll;
152901 nRight = p->doclist.nAll;
152904 nDiff = p->iDoclistToken - iToken;
152908 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
152911 p->doclist.aAll = pRight;
152912 p->doclist.nAll = nRight;
152915 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
152920 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
152929 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
152933 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
152934 Fts3PhraseToken *pToken = &p->aToken[iToken];
152935 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
152937 if( pToken->pSegcsr ){
152940 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
152945 assert( pToken->pSegcsr==0 );
152965 int iPrev = -1; /* Token number of previous deferred token */
152967 assert( pPhrase->doclist.bFreeList==0 );
152969 for(iToken=0; iToken<pPhrase->nToken; iToken++){
152970 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
152971 Fts3DeferredToken *pDeferred = pToken->pDeferred;
152981 pPhrase->doclist.pList = 0;
152982 pPhrase->doclist.nList = 0;
152995 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
152998 nPoslist = (int)(aOut - aPoslist);
153001 pPhrase->doclist.pList = 0;
153002 pPhrase->doclist.nList = 0;
153011 int nMaxUndeferred = pPhrase->iDoclistToken;
153013 pPhrase->doclist.pList = aPoslist;
153014 pPhrase->doclist.nList = nPoslist;
153015 pPhrase->doclist.iDocid = pCsr->iPrevId;
153016 pPhrase->doclist.bFreeList = 1;
153025 p2 = pPhrase->doclist.pList;
153026 nDistance = nMaxUndeferred - iPrev;
153028 p1 = pPhrase->doclist.pList;
153030 nDistance = iPrev - nMaxUndeferred;
153039 pPhrase->doclist.pList = aOut;
153041 pPhrase->doclist.bFreeList = 1;
153042 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
153045 pPhrase->doclist.pList = 0;
153046 pPhrase->doclist.nList = 0;
153062 ** This function is called for each Fts3Phrase in a full-text query
153074 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
153082 ** tokens or prefix tokens that cannot use a prefix-index. */
153085 && pCsr->bDesc==pTab->bDescIdx
153086 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
153088 && pTab->bNoIncrDoclist==0
153091 for(i=0; bIncrOk==1 && i<p->nToken; i++){
153092 Fts3PhraseToken *pToken = &p->aToken[i];
153093 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
153096 if( pToken->pSegcsr ) bHaveIncr = 1;
153101 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
153102 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
153103 Fts3PhraseToken *pToken = &p->aToken[i];
153104 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
153106 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
153109 p->bIncr = 1;
153113 p->bIncr = 0;
153116 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
153124 ** pending-terms lists when writing to databases with "order=desc".
153137 u8 *pbEof /* OUT: End-of-file flag */
153160 iMul = (bDescIdx ? -1 : 1);
153163 *pnList = (int)(pEnd - pNext);
153167 int iMul = (bDescIdx ? -1 : 1);
153170 *piDocid -= (iMul * iDelta);
153177 *pnList = (int)(pSave - p);
153192 u8 *pbEof /* OUT: End-of-file flag */
153212 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
153220 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
153229 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
153231 if( pDL->pNextDocid ){
153232 pIter = pDL->pNextDocid;
153234 pIter = pDL->aAll;
153243 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
153244 pDL->iDocid += iDelta;
153246 pDL->iDocid -= iDelta;
153248 pDL->pList = pIter;
153250 pDL->nList = (int)(pIter - pDL->pList);
153252 /* pIter now points just past the 0x00 that terminates the position-
153253 ** list for document pDL->iDocid. However, if this position-list was
153256 ** with this case by advancing pIter past the zero-padding added by
153260 pDL->pNextDocid = pIter;
153261 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
153279 ** multi-token phrase. Advance it to the next matching document in the
153295 if( pPhrase->iDoclistToken==iToken ){
153296 assert( p->bIgnore==0 );
153297 assert( pPhrase->aToken[iToken].pSegcsr==0 );
153298 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
153299 p->pList = pPhrase->doclist.pList;
153300 p->nList = pPhrase->doclist.nList;
153301 p->iDocid = pPhrase->doclist.iDocid;
153303 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
153304 assert( pToken->pDeferred==0 );
153305 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
153306 if( pToken->pSegcsr ){
153307 assert( p->bIgnore==0 );
153309 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
153311 if( p->pList==0 ) *pbEof = 1;
153313 p->bIgnore = 1;
153344 Fts3Doclist *pDL = &p->doclist;
153345 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
153350 assert( p->bIncr==1 );
153352 if( p->nToken==1 ){
153353 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
153354 &pDL->iDocid, &pDL->pList, &pDL->nList
153356 if( pDL->pList==0 ) bEof = 1;
153358 int bDescDoclist = pCsr->bDesc;
153362 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
153363 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
153371 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
153378 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
153382 for(i=0; i<p->nToken; i++){
153397 int nByte = a[p->nToken-1].nList;
153400 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
153402 for(i=0; i<(p->nToken-1); i++){
153407 int nDist = p->nToken-1-i;
153410 nList = (int)(pOut - aDoclist);
153413 if( i==(p->nToken-1) ){
153414 pDL->iDocid = iMax;
153415 pDL->pList = aDoclist;
153416 pDL->nList = nList;
153417 pDL->bFreeList = 1;
153444 Fts3Doclist *pDL = &p->doclist;
153445 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
153447 if( p->bIncr ){
153449 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
153450 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
153451 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
153453 pDL->pList = pDL->pNextDocid;
153463 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
153471 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
153483 if( pExpr->eType==FTSQUERY_PHRASE ){
153484 int nToken = pExpr->pPhrase->nToken;
153488 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
153490 pExpr->bDeferred = (i==nToken);
153492 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
153494 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
153495 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
153496 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
153526 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
153539 if( pExpr->eType==FTSQUERY_PHRASE ){
153540 Fts3Phrase *pPhrase = pExpr->pPhrase;
153542 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
153544 pTC->pPhrase = pPhrase;
153545 pTC->iToken = i;
153546 pTC->pRoot = pRoot;
153547 pTC->pToken = &pPhrase->aToken[i];
153548 pTC->iCol = pPhrase->iColumn;
153549 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
153551 }else if( pExpr->eType!=FTSQUERY_NOT ){
153552 assert( pExpr->eType==FTSQUERY_OR
153553 || pExpr->eType==FTSQUERY_AND
153554 || pExpr->eType==FTSQUERY_NEAR
153556 assert( pExpr->pLeft && pExpr->pRight );
153557 if( pExpr->eType==FTSQUERY_OR ){
153558 pRoot = pExpr->pLeft;
153562 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
153563 if( pExpr->eType==FTSQUERY_OR ){
153564 pRoot = pExpr->pRight;
153568 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
153586 if( pCsr->nRowAvg==0 ){
153598 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
153620 pCsr->nDoc = nDoc;
153621 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
153622 assert( pCsr->nRowAvg>0 );
153626 *pnPage = pCsr->nRowAvg;
153650 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
153665 if( pTab->zContentTbl ){
153687 ** to retrieve the entire doclist for the token from the full-text index.
153693 ** b. Part of a multi-token phrase.
153705 ** (nMinEst + 4^nOther - 1) / (4^nOther)
153714 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
153721 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
153726 Fts3PhraseToken *pToken = pTC->pToken;
153727 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
153728 fts3SegReaderCursorFree(pToken->pSegcsr);
153729 pToken->pSegcsr = 0;
153732 ** for-loop. Except, limit the value to 2^24 to prevent it from
153733 ** overflowing the 32-bit integer it is stored in. */
153736 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
153738 ** part of a multi-token phrase. Either way, the entire doclist will
153740 Fts3PhraseToken *pToken = pTC->pToken;
153743 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
153747 pTab, pTC->pPhrase, pTC->iToken,pList,nList
153753 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
153759 pTC->pToken = 0;
153767 ** the full-text query currently stored in pCsr->pExpr. To iterate through
153773 ** if( pCsr->bEof ) break;
153774 ** ... return row pCsr->iPrevId to the caller ...
153778 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
153784 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
153788 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
153804 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
153805 nToken = (int)(pTC-aTC);
153806 nOr = (int)(ppOr-apOr);
153820 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
153828 if( pPhrase->doclist.bFreeList ){
153829 sqlite3_free(pPhrase->doclist.pList);
153831 pPhrase->doclist.pList = 0;
153832 pPhrase->doclist.nList = 0;
153833 pPhrase->doclist.bFreeList = 0;
153852 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
153865 int nParam1 = nNear + pPhrase->nToken;
153872 assert( pPhrase->doclist.pList );
153874 p2 = pOut = pPhrase->doclist.pList;
153879 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
153880 assert( pPhrase->doclist.pList[nNew]=='\0' );
153881 assert( nNew<=pPhrase->doclist.nList && nNew>0 );
153882 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
153883 pPhrase->doclist.nList = nNew;
153884 *paPoslist = pPhrase->doclist.pList;
153885 *pnToken = pPhrase->nToken;
153892 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
153896 ** or descending if it is non-zero.
153901 ** Fts3Expr.bEof (non-zero if EOF - there is no next row)
153908 ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
153909 ** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
153916 ** the db. In this case the position-list is not populated at all.
153919 ** more non-deferred tokens, then the expression is advanced to the
153920 ** next possible match, considering only non-deferred tokens. In other
153938 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
153939 assert( pExpr->bEof==0 );
153940 pExpr->bStart = 1;
153942 switch( pExpr->eType ){
153945 Fts3Expr *pLeft = pExpr->pLeft;
153946 Fts3Expr *pRight = pExpr->pRight;
153947 assert( !pLeft->bDeferred || !pRight->bDeferred );
153949 if( pLeft->bDeferred ){
153953 pExpr->iDocid = pRight->iDocid;
153954 pExpr->bEof = pRight->bEof;
153955 }else if( pRight->bDeferred ){
153959 pExpr->iDocid = pLeft->iDocid;
153960 pExpr->bEof = pLeft->bEof;
153965 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
153966 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
153974 pExpr->iDocid = pLeft->iDocid;
153975 pExpr->bEof = (pLeft->bEof || pRight->bEof);
153976 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
153977 assert( pRight->eType==FTSQUERY_PHRASE );
153978 if( pRight->pPhrase->doclist.aAll ){
153979 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
153980 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
153981 memset(pDl->pList, 0, pDl->nList);
153985 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
153986 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
153987 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
153988 memset(pDl->pList, 0, pDl->nList);
153998 Fts3Expr *pLeft = pExpr->pLeft;
153999 Fts3Expr *pRight = pExpr->pRight;
154000 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
154002 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
154003 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
154005 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
154007 }else if( pLeft->bEof || iCmp>0 ){
154014 pExpr->bEof = (pLeft->bEof && pRight->bEof);
154015 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
154016 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
154017 pExpr->iDocid = pLeft->iDocid;
154019 pExpr->iDocid = pRight->iDocid;
154026 Fts3Expr *pLeft = pExpr->pLeft;
154027 Fts3Expr *pRight = pExpr->pRight;
154029 if( pRight->bStart==0 ){
154031 assert( *pRc!=SQLITE_OK || pRight->bStart );
154035 if( pLeft->bEof==0 ){
154037 && !pRight->bEof
154038 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
154043 pExpr->iDocid = pLeft->iDocid;
154044 pExpr->bEof = pLeft->bEof;
154049 Fts3Phrase *pPhrase = pExpr->pPhrase;
154051 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
154052 pExpr->iDocid = pPhrase->doclist.iDocid;
154065 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
154085 ** +--NEAR--+ <-- root of NEAR query
154087 ** +--NEAR--+ "z"
154089 ** +--NEAR--+ "y"
154093 ** The right-hand child of a NEAR node is always a phrase. The
154094 ** left-hand child may be either a phrase or a NEAR node. There are
154095 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
154098 && pExpr->eType==FTSQUERY_NEAR
154099 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
154106 for(p=pExpr; p->pLeft; p=p->pLeft){
154107 assert( p->pRight->pPhrase->doclist.nList>0 );
154108 nTmp += p->pRight->pPhrase->doclist.nList;
154110 nTmp += p->pPhrase->doclist.nList;
154116 char *aPoslist = p->pPhrase->doclist.pList;
154117 int nToken = p->pPhrase->nToken;
154119 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
154120 Fts3Phrase *pPhrase = p->pRight->pPhrase;
154121 int nNear = p->nNear;
154125 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
154126 nToken = pExpr->pRight->pPhrase->nToken;
154127 for(p=pExpr->pLeft; p && res; p=p->pLeft){
154130 assert( p->pParent && p->pParent->pLeft==p );
154131 nNear = p->pParent->nNear;
154133 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
154147 ** Assuming no error occurs or has occurred, It returns non-zero if the
154151 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
154163 switch( pExpr->eType ){
154167 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
154168 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
154185 ** (as "b" is part of a non-matching NEAR clause).
154188 && pExpr->eType==FTSQUERY_NEAR
154189 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
154192 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
154193 if( p->pRight->iDocid==pCsr->iPrevId ){
154194 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
154197 if( p->iDocid==pCsr->iPrevId ){
154198 fts3EvalInvalidatePoslist(p->pPhrase);
154205 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
154206 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
154213 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
154214 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
154220 if( pCsr->pDeferred
154221 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
154223 Fts3Phrase *pPhrase = pExpr->pPhrase;
154224 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
154225 if( pExpr->bDeferred ){
154229 bHit = (pPhrase->doclist.pList!=0);
154230 pExpr->iDocid = pCsr->iPrevId;
154234 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
154245 ** iterating through the results of a full-text query. At this point the
154278 if( pCsr->pDeferred ){
154284 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
154286 /* Free the position-lists accumulated for each deferred token above. */
154299 Fts3Expr *pExpr = pCsr->pExpr;
154300 assert( pCsr->isEof==0 );
154302 pCsr->isEof = 1;
154305 if( pCsr->isRequireSeek==0 ){
154306 sqlite3_reset(pCsr->pStmt);
154308 assert( sqlite3_data_count(pCsr->pStmt)==0 );
154310 pCsr->isEof = pExpr->bEof;
154311 pCsr->isRequireSeek = 1;
154312 pCsr->isMatchinfoNeeded = 1;
154313 pCsr->iPrevId = pExpr->iDocid;
154314 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
154320 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
154321 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
154323 pCsr->isEof = 1;
154335 ** a no-op. If an error occurs within this function, *pRc is set to an
154344 Fts3Phrase *pPhrase = pExpr->pPhrase;
154348 if( pPhrase->bIncr ){
154350 for(i=0; i<pPhrase->nToken; i++){
154351 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
154352 assert( pToken->pDeferred==0 );
154353 if( pToken->pSegcsr ){
154354 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
154359 pPhrase->doclist.pNextDocid = 0;
154360 pPhrase->doclist.iDocid = 0;
154361 pPhrase->pOrPoslist = 0;
154364 pExpr->iDocid = 0;
154365 pExpr->bEof = 0;
154366 pExpr->bStart = 0;
154368 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
154369 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
154377 ** the values in Fts3Expr.aMI[] according to the position-list currently
154378 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
154383 Fts3Phrase *pPhrase = pExpr->pPhrase;
154384 if( pPhrase && pPhrase->doclist.pList ){
154386 char *p = pPhrase->doclist.pList;
154400 pExpr->aMI[iCol*3 + 1] += iCnt;
154401 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
154408 fts3EvalUpdateCounts(pExpr->pLeft);
154409 fts3EvalUpdateCounts(pExpr->pRight);
154430 assert( pExpr->eType==FTSQUERY_PHRASE );
154431 if( pExpr->aMI==0 ){
154432 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
154436 sqlite3_int64 iPrevId = pCsr->iPrevId;
154442 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
154443 pRoot = pRoot->pParent;
154445 iDocid = pRoot->iDocid;
154446 bEof = pRoot->bEof;
154447 assert( pRoot->bStart );
154450 for(p=pRoot; p; p=p->pLeft){
154451 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
154452 assert( pE->aMI==0 );
154453 pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
154454 if( !pE->aMI ) return SQLITE_NOMEM;
154455 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
154460 while( pCsr->isEof==0 && rc==SQLITE_OK ){
154464 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
154465 assert( sqlite3_data_count(pCsr->pStmt)==0 );
154469 pCsr->isEof = pRoot->bEof;
154470 pCsr->isRequireSeek = 1;
154471 pCsr->isMatchinfoNeeded = 1;
154472 pCsr->iPrevId = pRoot->iDocid;
154473 }while( pCsr->isEof==0
154474 && pRoot->eType==FTSQUERY_NEAR
154478 if( rc==SQLITE_OK && pCsr->isEof==0 ){
154483 pCsr->isEof = 0;
154484 pCsr->iPrevId = iPrevId;
154487 pRoot->bEof = bEof;
154493 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
154498 assert( pRoot->bEof==0 );
154499 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
154528 ** * If a phrase contains some deferred tokens (and some non-deferred
154530 ** the non-deferred tokens instead of actual phrase occurrences.
154540 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
154544 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
154545 assert( pCsr->nDoc>0 );
154546 for(iCol=0; iCol<pTab->nColumn; iCol++){
154547 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
154548 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
154553 assert( pExpr->aMI );
154554 for(iCol=0; iCol<pTab->nColumn; iCol++){
154555 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
154556 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
154569 ** a position-list indicating the occurrences of the phrase in column iCol
154575 ** if the requested column contains "a b X c d X X" and the position-list
154589 Fts3Phrase *pPhrase = pExpr->pPhrase;
154590 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
154598 assert( iCol>=0 && iCol<pTab->nColumn );
154599 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
154603 iDocid = pExpr->iDocid;
154604 pIter = pPhrase->doclist.pList;
154605 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
154607 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
154616 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
154620 for(p=pExpr->pParent; p; p=p->pParent){
154621 if( p->eType==FTSQUERY_OR ) bOr = 1;
154622 if( p->eType==FTSQUERY_NEAR ) pNear = p;
154623 if( p->bEof ) bTreeEof = 1;
154630 if( pPhrase->bIncr ){
154631 int bEofSave = pNear->bEof;
154633 while( rc==SQLITE_OK && !pNear->bEof ){
154635 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
154637 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
154640 while( rc==SQLITE_OK && !pNear->bEof ){
154647 for(p=pNear; p; p=p->pLeft){
154651 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
154652 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
154653 assert( pTest->eType==FTSQUERY_PHRASE );
154654 pPh = pTest->pPhrase;
154656 pIter = pPh->pOrPoslist;
154657 iDocid = pPh->iOrDocid;
154658 if( pCsr->bDesc==bDescDoclist ){
154659 bEof = !pPh->doclist.nAll ||
154660 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
154661 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
154663 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
154668 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
154669 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
154672 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
154677 pPh->pOrPoslist = pIter;
154678 pPh->iOrDocid = iDocid;
154679 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
154683 pIter = pPhrase->pOrPoslist;
154714 ** * the contents of pPhrase->doclist, and
154720 sqlite3_free(pPhrase->doclist.aAll);
154722 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
154723 for(i=0; i<pPhrase->nToken; i++){
154724 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
154725 pPhrase->aToken[i].pSegcsr = 0;
154793 int nStop; /* Byte-length of string zStop */
154837 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
154838 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
154865 p->pFts3Tab = (Fts3Table *)&p[1];
154866 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
154867 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
154868 p->pFts3Tab->db = db;
154869 p->pFts3Tab->nIndex = 1;
154871 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
154872 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
154873 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
154890 Fts3Table *pFts3 = p->pFts3Tab;
154894 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
154895 sqlite3_finalize(pFts3->aStmt[i]);
154897 sqlite3_free(pFts3->zSegmentsTbl);
154907 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
154914 int iEq = -1;
154915 int iGe = -1;
154916 int iLe = -1;
154917 int iLangid = -1;
154923 if( pInfo->nOrderBy==1
154924 && pInfo->aOrderBy[0].iColumn==0
154925 && pInfo->aOrderBy[0].desc==0
154927 pInfo->orderByConsumed = 1;
154932 for(i=0; i<pInfo->nConstraint; i++){
154933 if( pInfo->aConstraint[i].usable ){
154934 int op = pInfo->aConstraint[i].op;
154935 int iCol = pInfo->aConstraint[i].iColumn;
154951 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
154952 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
154953 pInfo->estimatedCost = 5;
154955 pInfo->idxNum = 0;
154956 pInfo->estimatedCost = 20000;
154958 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
154959 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
154960 pInfo->estimatedCost /= 2;
154963 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
154964 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
154965 pInfo->estimatedCost /= 2;
154969 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
154970 pInfo->estimatedCost--;
154977 ** xOpen - Open a cursor.
154993 ** xClose - Close a cursor.
154996 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
155000 sqlite3Fts3SegReaderFinish(&pCsr->csr);
155001 sqlite3_free((void *)pCsr->filter.zTerm);
155002 sqlite3_free(pCsr->zStop);
155003 sqlite3_free(pCsr->aStat);
155009 if( nSize>pCsr->nStat ){
155011 aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
155015 memset(&aNew[pCsr->nStat], 0,
155016 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
155018 pCsr->aStat = aNew;
155019 pCsr->nStat = nSize;
155025 ** xNext - Advance the cursor to the next row, if any.
155029 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
155033 pCsr->iRowid++;
155035 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
155036 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
155039 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
155042 int nDoclist = pCsr->csr.nDoclist;
155043 char *aDoclist = pCsr->csr.aDoclist;
155048 if( pCsr->zStop ){
155049 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
155050 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
155051 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
155052 pCsr->isEof = 1;
155058 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
155068 pCsr->aStat[0].nDoc++;
155084 pCsr->aStat[1].nDoc++;
155095 pCsr->aStat[iCol+1].nOcc++;
155096 pCsr->aStat[0].nOcc++;
155104 pCsr->aStat[iCol+1].nDoc++;
155110 pCsr->iCol = 0;
155113 pCsr->isEof = 1;
155119 ** xFilter - Initialize a cursor to point at the start of its data.
155129 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
155134 int iEq = -1; /* Index of term=? value in apVal */
155135 int iGe = -1; /* Index of term>=? value in apVal */
155136 int iLe = -1; /* Index of term<=? value in apVal */
155137 int iLangid = -1; /* Index of languageid=? value in apVal */
155165 testcase(pCsr->filter.zTerm);
155166 sqlite3Fts3SegReaderFinish(&pCsr->csr);
155167 sqlite3_free((void *)pCsr->filter.zTerm);
155168 sqlite3_free(pCsr->aStat);
155169 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
155171 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
155172 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
155176 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
155178 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
155179 pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
155180 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
155185 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
155186 pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
155187 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
155200 pCsr->iLangid = iLangVal;
155203 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
155206 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
155214 ** xEof - Return true if the cursor is at EOF, or false otherwise.
155218 return pCsr->isEof;
155222 ** xColumn - Return a column value.
155231 assert( p->isEof==0 );
155234 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
155238 if( p->iCol ){
155239 sqlite3_result_int(pCtx, p->iCol-1);
155241 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
155246 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
155250 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
155255 sqlite3_result_int(pCtx, p->iLangid);
155263 ** xRowid - Return the current rowid for the cursor.
155270 *pRowid = pCsr->iRowid;
155327 ** (the right-hand argument to the MATCH operator). Because the supported
155329 ** hand-coded.
155344 ** c) The old syntax supports the "-" token qualifier. This is not
155354 ** non-zero the new syntax is activated. This is so both syntaxes can
155366 ** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
155401 ** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
155402 ** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
155410 int bFts4; /* True to allow FTS4-only syntax */
155413 int isNot; /* True if getNextNode() sees a unary - */
155451 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
155455 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
155458 pCsr->pTokenizer = pTokenizer;
155459 if( pModule->iVersion>=1 ){
155460 rc = pModule->xLanguageid(pCsr, iLangid);
155462 pModule->xClose(pCsr);
155496 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
155497 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
155510 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
155516 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
155523 pRet->eType = FTSQUERY_PHRASE;
155524 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
155525 pRet->pPhrase->nToken = 1;
155526 pRet->pPhrase->iColumn = iCol;
155527 pRet->pPhrase->aToken[0].n = nToken;
155528 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
155529 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
155532 pRet->pPhrase->aToken[0].isPrefix = 1;
155538 && iStart>0 && z[iStart-1]=='-'
155540 pParse->isNot = 1;
155541 iStart--;
155542 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
155543 pRet->pPhrase->aToken[0].bFirst = 1;
155544 iStart--;
155556 pModule->xClose(pCursor);
155565 ** Enlarge a memory allocation. If an out-of-memory allocation occurs,
155593 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
155594 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
155624 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
155630 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
155641 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
155647 pToken->n = nByte;
155648 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
155649 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
155654 pModule->xClose(pCursor);
155664 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
155665 p->eType = FTSQUERY_PHRASE;
155666 p->pPhrase = (Fts3Phrase *)&p[1];
155667 p->pPhrase->iColumn = pParse->iDefaultCol;
155668 p->pPhrase->nToken = nToken;
155670 zBuf = (char *)&p->pPhrase->aToken[nToken];
155678 for(jj=0; jj<p->pPhrase->nToken; jj++){
155679 p->pPhrase->aToken[jj].z = zBuf;
155680 zBuf += p->pPhrase->aToken[jj].n;
155690 pModule->xClose(pCursor);
155732 pParse->isNot = 0;
155738 nInput--;
155749 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
155753 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
155755 int nKey = pKey->n;
155759 if( pKey->eType==FTSQUERY_NEAR ){
155764 nNear = nNear * 10 + (zInput[nKey] - '0');
155781 pRet->eType = pKey->eType;
155782 pRet->nNear = nNear;
155784 *pnConsumed = (int)((zInput - z) + nKey);
155801 *pnConsumed = (int)((zInput - z) + ii + 1);
155805 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
155811 pParse->nNest++;
155812 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
155814 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
155817 pParse->nNest--;
155818 *pnConsumed = (int)((zInput - z) + 1);
155835 iCol = pParse->iDefaultCol;
155837 for(ii=0; ii<pParse->nCol; ii++){
155838 const char *zStr = pParse->azCol[ii];
155844 iColLen = (int)((zInput - z) + nStr + 1);
155848 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
155873 assert( p->eType!=FTSQUERY_PHRASE );
155875 return p->eType;
155876 }else if( p->eType==FTSQUERY_NEAR ){
155878 }else if( p->eType==FTSQUERY_OR ){
155881 assert( p->eType==FTSQUERY_AND );
155899 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
155900 pSplit = pSplit->pParent;
155903 if( pSplit->pParent ){
155904 assert( pSplit->pParent->pRight==pSplit );
155905 pSplit->pParent->pRight = pNew;
155906 pNew->pParent = pSplit->pParent;
155910 pNew->pLeft = pSplit;
155911 pSplit->pParent = pNew;
155917 ** closing bracket - ')' - is encountered.
155949 && p->eType==FTSQUERY_PHRASE && pParse->isNot
155958 pNot->eType = FTSQUERY_NOT;
155959 pNot->pRight = p;
155960 p->pParent = pNot;
155962 pNot->pLeft = pNotBranch;
155963 pNotBranch->pParent = pNot;
155968 int eType = p->eType;
155969 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
155992 pAnd->eType = FTSQUERY_AND;
156007 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
156008 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
156017 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
156018 pPrev->pRight = p;
156019 p->pParent = pPrev;
156033 nIn -= nByte;
156048 while( pIter->pLeft ){
156049 pIter = pIter->pLeft;
156051 pIter->pLeft = pRet;
156052 pRet->pParent = pIter;
156057 *pnConsumed = n - nIn;
156079 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
156081 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
156094 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
156103 int eType = pRoot->eType; /* Type of node in this tree */
156123 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
156124 for(p=pRoot; p->eType==eType; p=p->pLeft){
156125 assert( p->pParent==0 || p->pParent->pLeft==p );
156126 assert( p->pLeft && p->pRight );
156132 Fts3Expr *pParent = p->pParent; /* Current parent of p */
156134 assert( pParent==0 || pParent->pLeft==p );
156135 p->pParent = 0;
156137 pParent->pLeft = 0;
156141 rc = fts3ExprBalance(&p, nMaxDepth-1);
156150 pFree->pLeft = apLeaf[iLvl];
156151 pFree->pRight = p;
156152 pFree->pLeft->pParent = pFree;
156153 pFree->pRight->pParent = pFree;
156156 pFree = pFree->pParent;
156157 p->pParent = 0;
156171 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
156174 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
156175 pParent->pRight->pParent = pParent->pParent;
156176 if( pParent->pParent ){
156177 pParent->pParent->pLeft = pParent->pRight;
156180 pRoot = pParent->pRight;
156185 pParent->pParent = pFree;
156195 p->pParent = 0;
156198 pFree->pRight = p;
156199 pFree->pLeft = apLeaf[i];
156200 pFree->pLeft->pParent = pFree;
156201 pFree->pRight->pParent = pFree;
156204 pFree = pFree->pParent;
156205 p->pParent = 0;
156219 pFree = pDel->pParent;
156228 Fts3Expr *pLeft = pRoot->pLeft;
156229 Fts3Expr *pRight = pRoot->pRight;
156231 pRoot->pLeft = 0;
156232 pRoot->pRight = 0;
156233 pLeft->pParent = 0;
156234 pRight->pParent = 0;
156236 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
156238 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
156246 pRoot->pLeft = pLeft;
156247 pLeft->pParent = pRoot;
156248 pRoot->pRight = pRight;
156249 pRight->pParent = pRoot;
156277 int bFts4, /* True to allow FTS4-only syntax */
156318 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
156322 ** nul-terminated string and the length is determined using strlen().
156328 ** Column names must be nul-terminated strings.
156331 ** that appears on the left-hand-side of the MATCH operator (the default
156333 ** specified as part of the query string), or -1 if tokens may by default
156340 int bFts4, /* True to allow FTS4-only syntax */
156381 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
156382 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
156383 sqlite3_free(p->aMI);
156396 assert( pDel==0 || pDel->pParent==0 );
156397 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
156398 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
156401 Fts3Expr *pParent = p->pParent;
156403 if( pParent && p==pParent->pLeft && pParent->pRight ){
156404 p = pParent->pRight;
156405 while( p && (p->pLeft || p->pRight) ){
156406 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
156407 p = (p->pLeft ? p->pLeft : p->pRight);
156425 ** Function to query the hash-table of tokenizers (see README.tokenizers).
156437 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
156442 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
156466 switch( pExpr->eType ){
156468 Fts3Phrase *pPhrase = pExpr->pPhrase;
156471 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
156472 for(i=0; zBuf && i<pPhrase->nToken; i++){
156474 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
156475 (pPhrase->aToken[i].isPrefix?"+":"")
156482 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
156496 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
156499 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
156538 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
156549 sqlite3_result_error(context, "No such tokenizer module", -1);
156553 rc = pModule->xCreate(0, 0, &pTokenizer);
156559 pTokenizer->pModule = pModule;
156563 nCol = argc-2;
156588 sqlite3_result_error(context, "Error parsing expression", -1);
156592 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
156600 rc = pModule->xDestroy(pTokenizer);
156611 db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
156615 -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
156637 ** This is the implementation of generic hash-tables used in SQLite.
156639 ** implementation for the full-text indexing module.
156687 pNew->keyClass = keyClass;
156688 pNew->copyKey = copyKey;
156689 pNew->first = 0;
156690 pNew->count = 0;
156691 pNew->htsize = 0;
156692 pNew->ht = 0;
156703 elem = pH->first;
156704 pH->first = 0;
156705 fts3HashFree(pH->ht);
156706 pH->ht = 0;
156707 pH->htsize = 0;
156709 Fts3HashElem *next_elem = elem->next;
156710 if( pH->copyKey && elem->pKey ){
156711 fts3HashFree(elem->pKey);
156716 pH->count = 0;
156728 nKey--;
156743 while( nKey-- > 0 ){
156797 pHead = pEntry->chain;
156799 pNew->next = pHead;
156800 pNew->prev = pHead->prev;
156801 if( pHead->prev ){ pHead->prev->next = pNew; }
156802 else { pH->first = pNew; }
156803 pHead->prev = pNew;
156805 pNew->next = pH->first;
156806 if( pH->first ){ pH->first->prev = pNew; }
156807 pNew->prev = 0;
156808 pH->first = pNew;
156810 pEntry->count++;
156811 pEntry->chain = pNew;
156819 ** Return non-zero if a memory allocation error occurs.
156826 assert( (new_size & (new_size-1))==0 );
156829 fts3HashFree(pH->ht);
156830 pH->ht = new_ht;
156831 pH->htsize = new_size;
156832 xHash = ftsHashFunction(pH->keyClass);
156833 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
156834 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
156835 next_elem = elem->next;
156855 if( pH->ht ){
156856 struct _fts3ht *pEntry = &pH->ht[h];
156857 elem = pEntry->chain;
156858 count = pEntry->count;
156859 xCompare = ftsCompareFunction(pH->keyClass);
156860 while( count-- && elem ){
156861 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
156864 elem = elem->next;
156879 if( elem->prev ){
156880 elem->prev->next = elem->next;
156882 pH->first = elem->next;
156884 if( elem->next ){
156885 elem->next->prev = elem->prev;
156887 pEntry = &pH->ht[h];
156888 if( pEntry->chain==elem ){
156889 pEntry->chain = elem->next;
156891 pEntry->count--;
156892 if( pEntry->count<=0 ){
156893 pEntry->chain = 0;
156895 if( pH->copyKey && elem->pKey ){
156896 fts3HashFree(elem->pKey);
156899 pH->count--;
156900 if( pH->count<=0 ){
156901 assert( pH->first==0 );
156902 assert( pH->count==0 );
156915 if( pH==0 || pH->ht==0 ) return 0;
156916 xHash = ftsHashFunction(pH->keyClass);
156919 assert( (pH->htsize & (pH->htsize-1))==0 );
156920 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
156932 return pElem ? pElem->data : 0;
156963 xHash = ftsHashFunction(pH->keyClass);
156966 assert( (pH->htsize & (pH->htsize-1))==0 );
156967 h = hraw & (pH->htsize-1);
156970 void *old_data = elem->data;
156974 elem->data = data;
156979 if( (pH->htsize==0 && fts3Rehash(pH,8))
156980 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
156982 pH->count = 0;
156985 assert( pH->htsize>0 );
156988 if( pH->copyKey && pKey!=0 ){
156989 new_elem->pKey = fts3HashMalloc( nKey );
156990 if( new_elem->pKey==0 ){
156994 memcpy((void*)new_elem->pKey, pKey, nKey);
156996 new_elem->pKey = (void*)pKey;
156998 new_elem->nKey = nKey;
156999 pH->count++;
157000 assert( pH->htsize>0 );
157001 assert( (pH->htsize & (pH->htsize-1))==0 );
157002 h = hraw & (pH->htsize-1);
157003 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
157004 new_elem->data = data;
157023 ** Implementation of the full-text-search tokenizer that implements
157082 *ppTokenizer = &t->base;
157096 ** string to be tokenized is zInput[0..nInput-1]. A cursor
157112 c->zInput = zInput;
157114 c->nInput = 0;
157116 c->nInput = (int)strlen(zInput);
157118 c->nInput = nInput;
157120 c->iOffset = 0; /* start tokenizing at the beginning */
157121 c->iToken = 0;
157122 c->zToken = NULL; /* no space allocated, yet. */
157123 c->nAllocated = 0;
157125 *ppCursor = &c->base;
157135 sqlite3_free(c->zToken);
157166 j = cType[x-'a'];
157175 j = cType[x-'a'];
157176 if( j<2 ) return 1-j;
157188 ** vowel-consonant pairs followed by an optional vowel. "m" is the
157192 ** Return true if the m-value for z is 1 or more. In other words,
157235 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
157254 ** are consonant-vowel-consonent and where the final consonant
157291 *(--z) = *(zTo++);
157300 ** US-ASCII case folding. If the input word is too long (more
157311 zOut[i] = c - 'A' + 'a';
157319 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
157330 ** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
157334 ** Any upper-case characters in the US-ASCII character set ([A-Z])
157335 ** are converted to lower case. Upper-case UTF characters are
157342 ** are taken from each end. US-ASCII case folding still applies.
157345 ** in [a-zA-Z] then no stemming is attempted and this routine just
157346 ** copies the input into the input into the output with US-ASCII
157356 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
157362 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
157365 zReverse[j] = c + 'a' - 'A';
157369 /* The use of a character not in [a-zA-Z] means that we fallback
157375 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
157405 *(--z) = 'e';
157581 zOut[--i] = *(z++);
157599 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
157614 const char *z = c->zInput;
157616 while( c->iOffset<c->nInput ){
157620 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
157621 c->iOffset++;
157624 /* Count non-delimiter characters. */
157625 iStartOffset = c->iOffset;
157626 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
157627 c->iOffset++;
157630 if( c->iOffset>iStartOffset ){
157631 int n = c->iOffset-iStartOffset;
157632 if( n>c->nAllocated ){
157634 c->nAllocated = n+20;
157635 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
157637 c->zToken = pNew;
157639 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
157640 *pzToken = c->zToken;
157642 *piEndOffset = c->iOffset;
157643 *piPosition = c->iToken++;
157651 ** The set of routines that implement the porter-stemmer tokenizer
157689 ** This is part of an SQLite module implementing full-text search.
157709 ** Return true if the two-argument version of fts3_tokenizer()
157716 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
157724 ** SELECT <function-name>(<key-name>);
157725 ** SELECT <function-name>(<key-name>, <pointer>);
157727 ** where <function-name> is the name passed as the second argument
157732 ** to the string <key-name>. If <pointer> is not specified, then
157733 ** the string <key-name> must already exist in the has table. Otherwise,
157738 ** to string <key-name> (after the hash-table is updated, if applicable).
157762 sqlite3_result_error(context, "argument type mismatch", -1);
157768 sqlite3_result_error(context, "out of memory", -1);
157771 sqlite3_result_error(context, "fts3tokenize disabled", -1);
157780 sqlite3_result_error(context, zErr, -1);
157835 *pn = (int)(z2-z1);
157849 char *zEnd; /* Pointer to nul-term of zCopy */
157886 rc = m->xCreate(iArg, aArg, ppTok);
157891 (*ppTok)->pModule = m;
157915 ** SELECT <function-name>(<key-name>, ..., <input-string>);
157917 ** where <function-name> is the name passed as the second argument
157922 ** list. For each token in the <input-string>, three elements are
157925 ** substring of <input-string> associated with the token. For example,
157926 ** using the built-in "simple" tokenizer:
157964 sqlite3_result_error(context, "insufficient arguments", -1);
157970 nInput = sqlite3_value_bytes(argv[argc-1]);
157971 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
157978 sqlite3_result_error(context, zErr2, -1);
157986 for(i=1; i<argc-1; i++){
157987 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
157990 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
157994 pTokenizer->pModule = p;
158000 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
158004 nToken = iEnd-iStart;
158008 if( SQLITE_OK!=p->xClose(pCsr) ){
158012 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
158019 sqlite3_result_error(context, zErr, -1);
158021 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
158036 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
158041 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
158060 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
158065 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
158083 ** can be used as designed by the C-code in the queryTokenizer and
158127 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
158177 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
158208 ** Implementation of the "simple" full-text-search tokenizer.
158247 return c<0x80 && t->delim[c];
158267 ** else we need to reindex. One solution would be a meta-table to
158275 /* We explicitly don't support UTF-8 delimiters for now. */
158280 t->delim[ch] = 1;
158283 /* Mark non-alphanumeric ASCII characters as delimiters */
158286 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
158290 *ppTokenizer = &t->base;
158304 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
158320 c->pInput = pInput;
158322 c->nBytes = 0;
158324 c->nBytes = (int)strlen(pInput);
158326 c->nBytes = nBytes;
158328 c->iOffset = 0; /* start tokenizing at the beginning */
158329 c->iToken = 0;
158330 c->pToken = NULL; /* no space allocated, yet. */
158331 c->nTokenAllocated = 0;
158333 *ppCursor = &c->base;
158343 sqlite3_free(c->pToken);
158361 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
158362 unsigned char *p = (unsigned char *)c->pInput;
158364 while( c->iOffset<c->nBytes ){
158368 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
158369 c->iOffset++;
158372 /* Count non-delimiter characters. */
158373 iStartOffset = c->iOffset;
158374 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
158375 c->iOffset++;
158378 if( c->iOffset>iStartOffset ){
158379 int i, n = c->iOffset-iStartOffset;
158380 if( n>c->nTokenAllocated ){
158382 c->nTokenAllocated = n+20;
158383 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
158385 c->pToken = pNew;
158388 /* TODO(shess) This needs expansion to handle UTF-8
158389 ** case-insensitivity.
158392 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
158394 *ppToken = c->pToken;
158397 *piEndOffset = c->iOffset;
158398 *piPosition = c->iToken++;
158449 ** <tokenizer-name>, <arg-1>, ...
158529 ** The second argument, argv[], is an array of pointers to nul-terminated
158608 nDequote = argc-3;
158624 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
158636 pTab->pMod = pMod;
158637 pTab->pTok = pTok;
158638 *ppVtab = &pTab->base;
158641 pMod->xDestroy(pTok);
158657 pTab->pMod->xDestroy(pTab->pTok);
158663 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
158672 for(i=0; i<pInfo->nConstraint; i++){
158673 if( pInfo->aConstraint[i].usable
158674 && pInfo->aConstraint[i].iColumn==0
158675 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
158677 pInfo->idxNum = 1;
158678 pInfo->aConstraintUsage[i].argvIndex = 1;
158679 pInfo->aConstraintUsage[i].omit = 1;
158680 pInfo->estimatedCost = 1;
158685 pInfo->idxNum = 0;
158686 assert( pInfo->estimatedCost>1000000.0 );
158692 ** xOpen - Open a cursor.
158713 if( pCsr->pCsr ){
158714 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
158715 pTab->pMod->xClose(pCsr->pCsr);
158716 pCsr->pCsr = 0;
158718 sqlite3_free(pCsr->zInput);
158719 pCsr->zInput = 0;
158720 pCsr->zToken = 0;
158721 pCsr->nToken = 0;
158722 pCsr->iStart = 0;
158723 pCsr->iEnd = 0;
158724 pCsr->iPos = 0;
158725 pCsr->iRowid = 0;
158729 ** xClose - Close a cursor.
158740 ** xNext - Advance the cursor to the next row, if any.
158744 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
158747 pCsr->iRowid++;
158748 rc = pTab->pMod->xNext(pCsr->pCsr,
158749 &pCsr->zToken, &pCsr->nToken,
158750 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
158762 ** xFilter - Initialize a cursor to point at the start of its data.
158773 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
158781 pCsr->zInput = sqlite3_malloc(nByte+1);
158782 if( pCsr->zInput==0 ){
158785 memcpy(pCsr->zInput, zByte, nByte);
158786 pCsr->zInput[nByte] = 0;
158787 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
158789 pCsr->pCsr->pTokenizer = pTab->pTok;
158799 ** xEof - Return true if the cursor is at EOF, or false otherwise.
158803 return (pCsr->zToken==0);
158807 ** xColumn - Return a column value.
158819 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
158822 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
158825 sqlite3_result_int(pCtx, pCsr->iStart);
158828 sqlite3_result_int(pCtx, pCsr->iEnd);
158832 sqlite3_result_int(pCtx, pCsr->iPos);
158839 ** xRowid - Return the current rowid for the cursor.
158846 *pRowid = (sqlite3_int64)pCsr->iRowid;
158904 ** tables. It also contains code to merge FTS3 b-tree segments. Some
158905 ** of the sub-routines used to merge segments are also used by the query
158920 ** When full-text index nodes are loaded from disk, the buffer that they
158922 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
158932 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
158938 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
158940 ** The code is written so that the hard lower-limit for each of these values
158974 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
159011 ** a contiguous set of segment b-tree leaf nodes. Although the details of
159014 ** terms when querying the full-text index. See functions:
159029 u8 rootOnly; /* True for a root-only reader */
159058 int nOffsetList; /* For descending pending seg-readers only */
159062 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
159063 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
159066 ** An instance of this structure is used to create a segment b-tree in the
159090 ** the interior part of the segment b+-tree structures (everything except
159099 ** or the pending-terms table being flushed), leaves are written into the
159102 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
159108 SegmentNode *pRight; /* Pointer to right-sibling */
159109 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
159232 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
159275 /* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
159277 /* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
159283 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
159286 pStmt = p->aStmt[eStmt];
159290 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
159292 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
159294 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
159299 rc = sqlite3_prepare_v3(p->db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
159303 p->aStmt[eStmt] = pStmt;
159400 ** shared-cache table-lock on the %_segdir table. This is required before
159415 if( p->nPendingData==0 ){
159429 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
159436 ** level within the index) are encoded in 64-bit integer values stored
159438 ** separate component values into the single 64-bit integer value that
159441 ** Specifically, each language-id/index combination is allocated 1024
159442 ** 64-bit integer level values ("absolute levels"). The main terms index
159443 ** for language-id 0 is allocate values 0-1023. The first prefix index
159444 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
159448 ** absolute levels that corresponds to language-id iLangid and index
159454 int iIndex, /* Index in p->aIndex[] */
159459 assert( p->nIndex>0 );
159460 assert( iIndex>=0 && iIndex<p->nIndex );
159462 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
159486 int iIndex, /* Index for p->aIndex[] */
159495 assert( iIndex>=0 && iIndex<p->nIndex );
159503 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
159542 p->nSpace = 100;
159543 p->aData = (char *)&p[1];
159544 p->nData = 0;
159546 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
159547 int nNew = p->nSpace * 2;
159554 p->nSpace = nNew;
159555 p->aData = (char *)&p[1];
159559 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
159560 p->aData[p->nData] = '\0';
159566 ** Add a docid/column/position entry to a PendingList structure. Non-zero
159584 assert( !p || p->iLastDocid<=iDocid );
159586 if( !p || p->iLastDocid!=iDocid ){
159587 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
159589 assert( p->nData<p->nSpace );
159590 assert( p->aData[p->nData]==0 );
159591 p->nData++;
159596 p->iLastCol = -1;
159597 p->iLastPos = 0;
159598 p->iLastDocid = iDocid;
159600 if( iCol>0 && p->iLastCol!=iCol ){
159606 p->iLastCol = iCol;
159607 p->iLastPos = 0;
159610 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
159611 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
159613 p->iLastPos = iPos;
159634 ** Add an entry to one of the pending-terms hash tables.
159649 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
159651 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
159662 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
159668 ** Tokenize the nul-terminated string zText and add all tokens to the
159669 ** pending-terms hash-table. The docid used is that currently stored in
159670 ** p->iPrevDocid, and the column is specified by argument iCol.
159690 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
159691 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
159706 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
159711 xNext = pModule->xNext;
159718 /* Positions cannot be negative; we use -1 as a terminator internally.
159719 ** Tokens must have a non-zero length.
159728 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
159733 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
159734 struct Fts3Index *pIndex = &p->aIndex[i];
159735 if( nToken<pIndex->nPrefix ) continue;
159737 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
159742 pModule->xClose(pCsr);
159749 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
159753 Fts3Table *p, /* Full-text table handle */
159762 ** forced-flush would provide better performance. I suspect that if
159767 if( iDocid<p->iPrevDocid
159768 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
159769 || p->iPrevLangid!=iLangid
159770 || p->nPendingData>p->nMaxPendingData
159775 p->iPrevDocid = iDocid;
159776 p->iPrevLangid = iLangid;
159777 p->bPrevDelete = bDelete;
159782 ** Discard the contents of the pending-terms hash tables.
159786 for(i=0; i<p->nIndex; i++){
159788 Fts3Hash *pHash = &p->aIndex[i].hPending;
159795 p->nPendingData = 0;
159813 for(i=2; i<p->nColumn+2; i++){
159814 int iCol = i-2;
159815 if( p->abNotindexed[iCol]==0 ){
159821 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
159834 ** apVal[2] Left-most user-defined column
159836 ** apVal[p->nColumn+1] Right-most user-defined column
159837 ** apVal[p->nColumn+2] Hidden column with same name as table
159838 ** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
159839 ** apVal[p->nColumn+4] Hidden languageid column
159842 Fts3Table *p, /* Full-text table */
159849 if( p->zContentTbl ){
159850 sqlite3_value *pRowid = apVal[p->nColumn+3];
159870 if( rc==SQLITE_OK && p->zLanguageid ){
159872 pContentInsert, p->nColumn+2,
159873 sqlite3_value_int(apVal[p->nColumn+4])
159885 ** In FTS3, this is an error. It is an error to specify non-NULL values
159888 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
159895 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
159905 *piDocid = sqlite3_last_insert_rowid(p->db);
159918 /* Discard the contents of the pending-terms hash table. */
159923 assert( p->zContentTbl==0 || bContent==0 );
159927 if( p->bHasDocsize ){
159930 if( p->bHasStat ){
159941 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
159948 ** full-text index.
159969 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
159970 int iCol = i-1;
159971 if( p->abNotindexed[iCol]==0 ){
159973 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
159974 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
160015 int iIndex, /* Index for p->aIndex */
160024 assert( p->nIndex>=1 );
160070 ** paBlob is non-NULL, then it is the responsibility of the caller to
160092 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
160095 if( p->pSegments ){
160096 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
160098 if( 0==p->zSegmentsTbl ){
160099 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
160100 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
160103 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
160108 int nByte = sqlite3_blob_bytes(p->pSegments);
160119 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
160134 ** Close the blob handle at p->pSegments, if it is open. See comments above
160138 sqlite3_blob_close(p->pSegments);
160139 p->pSegments = 0;
160146 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
160148 pReader->pBlob,
160149 &pReader->aNode[pReader->nPopulate],
160151 pReader->nPopulate
160155 pReader->nPopulate += nRead;
160156 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
160157 if( pReader->nPopulate==pReader->nNode ){
160158 sqlite3_blob_close(pReader->pBlob);
160159 pReader->pBlob = 0;
160160 pReader->nPopulate = 0;
160168 assert( !pReader->pBlob
160169 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
160171 while( pReader->pBlob && rc==SQLITE_OK
160172 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
160184 sqlite3_free(pSeg->aNode);
160185 sqlite3_blob_close(pSeg->pBlob);
160186 pSeg->pBlob = 0;
160188 pSeg->aNode = 0;
160201 int rc; /* Return code of various sub-routines */
160206 if( !pReader->aDoclist ){
160207 pNext = pReader->aNode;
160209 pNext = &pReader->aDoclist[pReader->nDoclist];
160212 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
160215 Fts3HashElem *pElem = *(pReader->ppNextElem);
160216 sqlite3_free(pReader->aNode);
160217 pReader->aNode = 0;
160221 int nCopy = pList->nData+1;
160222 pReader->zTerm = (char *)fts3HashKey(pElem);
160223 pReader->nTerm = fts3HashKeysize(pElem);
160226 memcpy(aCopy, pList->aData, nCopy);
160227 pReader->nNode = pReader->nDoclist = nCopy;
160228 pReader->aNode = pReader->aDoclist = aCopy;
160229 pReader->ppNextElem++;
160230 assert( pReader->aNode );
160239 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
160240 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
160245 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
160246 (bIncr ? &pReader->nPopulate : 0)
160249 assert( pReader->pBlob==0 );
160250 if( bIncr && pReader->nPopulate<pReader->nNode ){
160251 pReader->pBlob = p->pSegments;
160252 p->pSegments = 0;
160254 pNext = pReader->aNode;
160267 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
160268 || nPrefix>pReader->nTermAlloc
160275 ** overflow - hence the (i64) casts. */
160276 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
160278 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
160282 pReader->zTerm = zNew;
160283 pReader->nTermAlloc = nNew;
160289 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
160290 pReader->nTerm = nPrefix+nSuffix;
160292 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
160293 pReader->aDoclist = pNext;
160294 pReader->pOffsetList = 0;
160297 ** b-tree node. And that the final byte of the doclist is 0x00. If either
160300 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
160301 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
160314 assert( pReader->aDoclist );
160315 assert( !pReader->pOffsetList );
160316 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
160318 pReader->iDocid = 0;
160319 pReader->nOffsetList = 0;
160321 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
160322 &pReader->iDocid, &pReader->nOffsetList, &bEof
160325 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
160327 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
160328 pReader->pOffsetList = &pReader->aDoclist[n];
160339 ** *ppOffsetList is set to point to the first column-offset list
160341 ** *pnOffsetList is set to the length of the set of column-offset
160342 ** lists, not including the nul-terminator byte. For example:
160347 char **ppOffsetList, /* OUT: Pointer to current position-list */
160351 char *p = pReader->pOffsetList;
160356 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
160357 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
160358 ** Pending-terms doclists are always built up in ascending order, so
160362 *ppOffsetList = pReader->pOffsetList;
160363 *pnOffsetList = pReader->nOffsetList - 1;
160366 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
160367 &pReader->nOffsetList, &bEof
160370 pReader->pOffsetList = 0;
160372 pReader->pOffsetList = p;
160375 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
160386 ** the populated part of pReader->aNode[].
160391 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
160398 ** size of the previous offset-list.
160401 *ppOffsetList = pReader->pOffsetList;
160402 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
160414 pReader->pOffsetList = 0;
160419 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
160420 if( pTab->bDescIdx ){
160421 pReader->iDocid -= iDelta;
160423 pReader->iDocid += iDelta;
160438 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
160442 int pgsz = p->nPgsz;
160444 assert( p->bFts4 );
160447 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
160448 Fts3SegReader *pReader = pMsr->apSegment[ii];
160453 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
160474 sqlite3_free(pReader->zTerm);
160477 sqlite3_free(pReader->aNode);
160479 sqlite3_blob_close(pReader->pBlob);
160510 pReader->iIdx = iAge;
160511 pReader->bLookup = bLookup!=0;
160512 pReader->iStartBlock = iStartLeaf;
160513 pReader->iLeafEndBlock = iEndLeaf;
160514 pReader->iEndBlock = iEndBlock;
160518 pReader->aNode = (char *)&pReader[1];
160519 pReader->rootOnly = 1;
160520 pReader->nNode = nRoot;
160521 memcpy(pReader->aNode, zRoot, nRoot);
160522 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
160524 pReader->iCurrentBlock = iStartLeaf-1;
160533 ** the contents of the pending-terms hash table to the database.
160547 c = n1 - n2;
160557 ** through each term in the pending-terms table. Or, if isPrefixIter is
160558 ** non-zero, it iterates through each term and its prefixes. For example, if
160573 int iIndex, /* Index for p->aIndex */
160577 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
160586 pHash = &p->aIndex[iIndex].hPending;
160622 ** the index. All that is required is a straight hash-lookup.
160642 pReader->iIdx = 0x7FFFFFFF;
160643 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
160644 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
160669 if( pLhs->aNode && pRhs->aNode ){
160670 int rc2 = pLhs->nTerm - pRhs->nTerm;
160672 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
160674 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
160680 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
160683 rc = pRhs->iIdx - pLhs->iIdx;
160701 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
160703 if( pLhs->iDocid==pRhs->iDocid ){
160704 rc = pRhs->iIdx - pLhs->iIdx;
160706 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
160709 assert( pLhs->aNode && pRhs->aNode );
160713 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
160715 if( pLhs->iDocid==pRhs->iDocid ){
160716 rc = pRhs->iIdx - pLhs->iIdx;
160718 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
160721 assert( pLhs->aNode && pRhs->aNode );
160730 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
160739 if( pSeg->aNode ){
160740 if( pSeg->nTerm>nTerm ){
160741 res = memcmp(pSeg->zTerm, zTerm, nTerm);
160743 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
160746 res = pSeg->nTerm-nTerm;
160754 ** the final (nSegment-nSuspect) members are already in sorted order
160768 if( nSuspect==nSegment ) nSuspect--;
160769 for(i=nSuspect-1; i>=0; i--){
160771 for(j=i; j<(nSegment-1); j++){
160782 for(i=0; i<(nSuspect-1); i++){
160855 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160903 int nData = pTree->nData; /* Current size of node in bytes */
160908 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
160909 nSuffix = nTerm-nPrefix;
160912 if( nReq<=p->nNodeSize || !pTree->zTerm ){
160914 if( nReq>p->nNodeSize ){
160916 ** and the static node buffer (p->nNodeSize bytes) is not large
160918 ** p->nNodeSize bytes, but since this scenario only comes about when
160922 assert( pTree->aData==(char *)&pTree[1] );
160923 pTree->aData = (char *)sqlite3_malloc(nReq);
160924 if( !pTree->aData ){
160929 if( pTree->zTerm ){
160930 /* There is no prefix-length field for first term in a node */
160931 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
160934 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
160935 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
160936 pTree->nData = nData + nSuffix;
160937 pTree->nEntry++;
160940 if( pTree->nMalloc<nTerm ){
160941 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
160945 pTree->nMalloc = nTerm*2;
160946 pTree->zMalloc = zNew;
160948 pTree->zTerm = pTree->zMalloc;
160949 memcpy(pTree->zTerm, zTerm, nTerm);
160950 pTree->nTerm = nTerm;
160952 pTree->zTerm = (char *)zTerm;
160953 pTree->nTerm = nTerm;
160960 ** current node. Create a new node (a right-sibling of the current node).
160967 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
160972 pNew->nData = 1 + FTS3_VARINT_MAX;
160973 pNew->aData = (char *)&pNew[1];
160976 SegmentNode *pParent = pTree->pParent;
160978 if( pTree->pParent==0 ){
160979 pTree->pParent = pParent;
160981 pTree->pRight = pNew;
160982 pNew->pLeftmost = pTree->pLeftmost;
160983 pNew->pParent = pParent;
160984 pNew->zMalloc = pTree->zMalloc;
160985 pNew->nMalloc = pTree->nMalloc;
160986 pTree->zMalloc = 0;
160988 pNew->pLeftmost = pNew;
161006 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
161007 pTree->aData[nStart] = (char)iHeight;
161008 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
161037 if( !pTree->pParent ){
161040 *piLast = iFree-1;
161041 *pnRoot = pTree->nData - nStart;
161042 *paRoot = &pTree->aData[nStart];
161047 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
161049 int nWrite = pIter->nData - nStart;
161051 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
161053 iNextLeaf += (pIter->nEntry+1);
161058 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
161071 SegmentNode *p = pTree->pLeftmost;
161072 fts3NodeFree(p->pParent);
161074 SegmentNode *pRight = p->pRight;
161075 if( p->aData!=(char *)&p[1] ){
161076 sqlite3_free(p->aData);
161078 assert( pRight==0 || p->zMalloc==0 );
161079 sqlite3_free(p->zMalloc);
161120 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
161121 if( !pWriter->aData ) return SQLITE_NOMEM;
161122 pWriter->nSize = p->nNodeSize;
161128 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
161129 pWriter->iFirst = pWriter->iFree;
161134 nData = pWriter->nData;
161136 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
161137 nSuffix = nTerm-nPrefix;
161146 if( nData>0 && nData+nReq>p->nNodeSize ){
161150 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
161152 p->nLeafAdd++;
161158 ** to the database (still available in pWriter->zTerm), and
161164 ** the common prefix (if any) of zTerm and pWriter->zTerm.
161167 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
161171 pWriter->nTerm = 0;
161183 pWriter->nLeafData += nReq;
161188 if( nReq>pWriter->nSize ){
161189 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
161191 pWriter->aData = aNew;
161192 pWriter->nSize = nReq;
161194 assert( nData+nReq<=pWriter->nSize );
161196 /* Append the prefix-compressed term and doclist to the buffer. */
161197 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
161198 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
161199 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
161201 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
161202 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
161203 pWriter->nData = nData + nDoclist;
161205 /* Save the current term so that it can be used to prefix-compress the next.
161211 if( nTerm>pWriter->nMalloc ){
161212 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
161216 pWriter->nMalloc = nTerm*2;
161217 pWriter->zMalloc = zNew;
161218 pWriter->zTerm = zNew;
161220 assert( pWriter->zTerm==pWriter->zMalloc );
161221 memcpy(pWriter->zTerm, zTerm, nTerm);
161223 pWriter->zTerm = (char *)zTerm;
161225 pWriter->nTerm = nTerm;
161243 if( pWriter->pTree ){
161249 iLastLeaf = pWriter->iFree;
161250 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
161252 rc = fts3NodeWrite(p, pWriter->pTree, 1,
161253 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
161257 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
161262 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
161264 p->nLeafAdd++;
161274 sqlite3_free(pWriter->aData);
161275 sqlite3_free(pWriter->zMalloc);
161276 fts3NodeFree(pWriter->pTree);
161294 if( p->zContentTbl ){
161326 assert( iIndex>=0 && iIndex<p->nIndex );
161338 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
161379 ** opened with seg-reader pSeg. This function does not affect the contents
161387 if( pSeg->iStartBlock ){
161391 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
161392 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
161402 ** segment to delete the old, now redundant, segment b-trees. Specifically,
161417 int iIndex, /* Index for p->aIndex */
161439 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
161468 ** If parameter bZero is non-zero, then any part of the input list following
161489 nList = (int)(p - pList);
161493 nList -= (int)(p - pList);
161503 memset(&pList[nList], 0, pEnd - &pList[nList]);
161517 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
161521 if( nList>pMsr->nBuffer ){
161523 pMsr->nBuffer = nList*2;
161524 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
161526 pMsr->aBuffer = pNew;
161529 memcpy(pMsr->aBuffer, pList, nList);
161535 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
161540 int nMerge = pMsr->nAdvance;
161541 Fts3SegReader **apSegment = pMsr->apSegment;
161543 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
161553 pSeg = pMsr->apSegment[0];
161555 if( pSeg->pOffsetList==0 ){
161563 sqlite3_int64 iDocid = apSegment[0]->iDocid;
161569 && apSegment[j]->pOffsetList
161570 && apSegment[j]->iDocid==iDocid
161576 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
161581 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
161582 pList = pMsr->aBuffer;
161585 if( pMsr->iColFilter>=0 ){
161586 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
161608 int nSeg = pCsr->nSegment;
161614 ** b-tree leaf nodes contain more than one term.
161616 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
161618 Fts3SegReader *pSeg = pCsr->apSegment[i];
161624 if( pSeg->bLookup && res!=0 ){
161628 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
161638 pCsr->pFilter = pFilter;
161639 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
161651 int nSegment = pCsr->nSegment;
161653 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
161656 assert( pCsr->pFilter==0 );
161665 Fts3SegReader *pSeg = pCsr->apSegment[i];
161666 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
161670 pCsr->nAdvance = i;
161673 for(i=0; i<pCsr->nAdvance; i++){
161674 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
161677 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
161679 assert( iCol<0 || iCol<p->nColumn );
161680 pCsr->iColFilter = iCol;
161698 int i; /* Used to iterate through segment-readers */
161700 assert( pCsr->zTerm==0 );
161701 assert( pCsr->nTerm==0 );
161702 assert( pCsr->aDoclist==0 );
161703 assert( pCsr->nDoclist==0 );
161705 pCsr->nAdvance = 0;
161706 pCsr->bRestart = 1;
161707 for(i=0; i<pCsr->nSegment; i++){
161708 pCsr->apSegment[i]->pOffsetList = 0;
161709 pCsr->apSegment[i]->nOffsetList = 0;
161710 pCsr->apSegment[i]->iDocid = 0;
161723 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
161724 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
161725 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
161726 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
161727 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
161728 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
161730 Fts3SegReader **apSegment = pCsr->apSegment;
161731 int nSegment = pCsr->nSegment;
161732 Fts3SegFilter *pFilter = pCsr->pFilter;
161734 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
161737 if( pCsr->nSegment==0 ) return SQLITE_OK;
161743 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
161746 for(i=0; i<pCsr->nAdvance; i++){
161748 if( pSeg->bLookup ){
161755 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
161756 pCsr->nAdvance = 0;
161758 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
161760 if( apSegment[0]->aNode==0 ) break;
161762 pCsr->nTerm = apSegment[0]->nTerm;
161763 pCsr->zTerm = apSegment[0]->zTerm;
161765 /* If this is a prefix-search, and if the term that apSegment[0] points
161766 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
161772 if( pFilter->zTerm && !isScan ){
161773 if( pCsr->nTerm<pFilter->nTerm
161774 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
161775 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
161783 && apSegment[nMerge]->aNode
161784 && apSegment[nMerge]->nTerm==pCsr->nTerm
161785 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
161794 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
161796 pCsr->nDoclist = apSegment[0]->nDoclist;
161798 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
161799 pCsr->aDoclist = pCsr->aBuffer;
161801 pCsr->aDoclist = apSegment[0]->aDoclist;
161816 while( apSegment[0]->pOffsetList ){
161821 sqlite3_int64 iDocid = apSegment[0]->iDocid;
161825 && apSegment[j]->pOffsetList
161826 && apSegment[j]->iDocid==iDocid
161833 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
161841 if( p->bDescIdx && nDoclist>0 ){
161842 iDelta = iPrev - iDocid;
161844 iDelta = iDocid - iPrev;
161850 if( nDoclist+nByte>pCsr->nBuffer ){
161852 pCsr->nBuffer = (nDoclist+nByte)*2;
161853 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
161857 pCsr->aBuffer = aNew;
161861 char *a = &pCsr->aBuffer[nDoclist];
161870 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
161873 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
161875 pCsr->aBuffer[nDoclist++] = '\0';
161883 pCsr->aDoclist = pCsr->aBuffer;
161884 pCsr->nDoclist = nDoclist;
161888 pCsr->nAdvance = nMerge;
161900 for(i=0; i<pCsr->nSegment; i++){
161901 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
161903 sqlite3_free(pCsr->apSegment);
161904 sqlite3_free(pCsr->aBuffer);
161906 pCsr->nSegment = 0;
161907 pCsr->apSegment = 0;
161908 pCsr->aBuffer = 0;
161917 ** containing the text representation of two non-negative integers separated
161934 iVal = iVal*10 + (zText[i] - '0');
161939 if( zText[i]=='-' ){
161941 iMul = -1;
161943 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
161944 iVal = iVal*10 + (zText[i] - '0');
161967 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
162009 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
162012 ** In other words, move all segments being promoted to level -1,
162014 ** order. The contents of level -1 (which is never used, except
162033 /* Move level -1 to level iAbsLevel */
162060 int iIndex, /* Index in p->aIndex[] to merge */
162077 assert( iIndex>=0 && iIndex<p->nIndex );
162104 assert( FTS3_SEGCURSOR_PENDING==-1 );
162139 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
162158 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
162159 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
162164 /* Determine the auto-incr-merge setting if unknown. If enabled,
162167 if( rc==SQLITE_OK && p->bHasStat
162168 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
162176 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
162177 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
162179 p->nAutoincrmerge = 0;
162224 ** with docid equal to p->iPrevDocid. The sizes are encoded as
162238 pBlob = sqlite3_malloc( 10*p->nColumn );
162243 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
162250 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
162285 const int nStat = p->nColumn+2;
162314 if( nChng<0 && a[0]<(u32)(-nChng) ){
162319 for(i=0; i<p->nColumn+1; i++){
162324 x = x + aSzIns[i] - aSzDel[i];
162354 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
162355 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
162359 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
162399 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
162403 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
162408 int nByte = sizeof(u32) * (p->nColumn+1)*3;
162414 aSzIns = &aSz[p->nColumn+1];
162415 aSzDel = &aSzIns[p->nColumn+1];
162423 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
162424 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
162425 if( p->abNotindexed[iCol]==0 ){
162428 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
162431 if( p->bHasDocsize ){
162439 for(iCol=0; iCol<=p->nColumn; iCol++){
162444 if( p->bFts4 ){
162464 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
162475 int nByte; /* Bytes allocated at pCsr->apSegment[] */
162480 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
162482 if( pCsr->apSegment==0 ){
162485 memset(pCsr->apSegment, 0, nByte);
162492 assert( pCsr->nSegment==0 );
162500 &pCsr->apSegment[i]
162502 pCsr->nSegment++;
162529 ** This structure is used to build up buffers containing segment b-tree
162540 ** to an appendable b-tree segment.
162575 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
162576 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
162579 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
162580 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
162581 ** to reflect the new size of the pBlob->a[] buffer.
162584 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
162586 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
162588 pBlob->nAlloc = nAlloc;
162589 pBlob->a = a;
162597 ** Attempt to advance the node-reader object passed as the first argument to
162602 ** (e.g. because the current entry is the last) set NodeReader->aNode to
162607 int bFirst = (p->term.n==0); /* True for first term on the node */
162612 assert( p->aNode );
162613 if( p->iChild && bFirst==0 ) p->iChild++;
162614 if( p->iOff>=p->nNode ){
162616 p->aNode = 0;
162619 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
162621 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
162623 if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
162626 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
162628 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
162629 p->term.n = nPrefix+nSuffix;
162630 p->iOff += nSuffix;
162631 if( p->iChild==0 ){
162632 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
162633 if( (p->nNode-p->iOff)<p->nDoclist ){
162636 p->aDoclist = &p->aNode[p->iOff];
162637 p->iOff += p->nDoclist;
162642 assert( p->iOff<=p->nNode );
162647 ** Release all dynamic resources held by node-reader object *p.
162650 sqlite3_free(p->term.a);
162654 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
162662 p->aNode = aNode;
162663 p->nNode = nNode;
162666 if( p->aNode[0] ){
162668 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
162670 p->iOff = 1;
162684 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
162692 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
162698 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
162708 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
162709 nSuffix = nTerm - nPrefix;
162713 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
162718 Blob *pBlk = &pNode->block;
162719 if( pBlk->n==0 ){
162720 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
162722 pBlk->a[0] = (char)iLayer;
162723 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
162726 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
162727 blobGrowBuffer(&pNode->key, nTerm, &rc);
162730 if( pNode->key.n ){
162731 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
162733 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
162734 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
162735 pBlk->n += nSuffix;
162737 memcpy(pNode->key.a, zTerm, nTerm);
162738 pNode->key.n = nTerm;
162744 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
162746 assert( pNode->block.nAlloc>=p->nNodeSize );
162747 pNode->block.a[0] = (char)iLayer;
162748 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
162750 iNextPtr = pNode->iBlock;
162751 pNode->iBlock++;
162752 pNode->key.n = 0;
162769 ** followed by the left-hand-child varint for an internal node.
162796 int bFirst = (pPrev->n==0); /* True if this is the first term written */
162802 assert( pNode->n>0 );
162803 assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
162808 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
162809 nSuffix = nTerm - nPrefix;
162810 memcpy(pPrev->a, zTerm, nTerm);
162811 pPrev->n = nTerm;
162814 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
162816 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
162817 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
162818 pNode->n += nSuffix;
162821 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
162822 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
162823 pNode->n += nDoclist;
162826 assert( pNode->n<=pNode->nAlloc );
162833 ** appendable b-tree segment opened for writing by pWriter.
162842 const char *zTerm = pCsr->zTerm;
162843 int nTerm = pCsr->nTerm;
162844 const char *aDoclist = pCsr->aDoclist;
162845 int nDoclist = pCsr->nDoclist;
162849 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
162852 pLeaf = &pWriter->aNodeWriter[0];
162853 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
162854 nSuffix = nTerm - nPrefix;
162863 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
162864 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
162865 pWriter->nWork++;
162871 ** to the database (still available in pLeaf->key), and
162877 ** the common prefix (if any) of zTerm and pWriter->zTerm.
162884 pLeaf->iBlock++;
162885 pLeaf->key.n = 0;
162886 pLeaf->block.n = 0;
162894 pWriter->nLeafData += nSpace;
162895 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
162897 if( pLeaf->block.n==0 ){
162898 pLeaf->block.n = 1;
162899 pLeaf->block.a[0] = '\0';
162902 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
162911 ** merge-writer object pWriter, and if no error has occurred, to flush
162924 IncrmergeWriter *pWriter, /* Merge-writer object */
162927 int i; /* Used to iterate through non-root layers */
162928 int iRoot; /* Index of root in pWriter->aNodeWriter */
162932 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
162936 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
162937 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
162938 if( pNode->block.n>0 ) break;
162939 assert( *pRc || pNode->block.nAlloc==0 );
162940 assert( *pRc || pNode->key.nAlloc==0 );
162941 sqlite3_free(pNode->block.a);
162942 sqlite3_free(pNode->key.a);
162945 /* Empty output segment. This is a no-op. */
162964 Blob *pBlock = &pWriter->aNodeWriter[1].block;
162967 pBlock->a[0] = 0x01;
162968 pBlock->n = 1 + sqlite3Fts3PutVarint(
162969 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
162974 pRoot = &pWriter->aNodeWriter[iRoot];
162978 NodeWriter *pNode = &pWriter->aNodeWriter[i];
162979 if( pNode->block.n>0 && rc==SQLITE_OK ){
162980 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
162982 sqlite3_free(pNode->block.a);
162983 sqlite3_free(pNode->key.a);
162989 pWriter->iAbsLevel+1, /* level */
162990 pWriter->iIdx, /* idx */
162991 pWriter->iStart, /* start_block */
162992 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
162993 pWriter->iEnd, /* end_block */
162994 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
162995 pRoot->block.a, pRoot->block.n /* root */
162998 sqlite3_free(pRoot->block.a);
162999 sqlite3_free(pRoot->key.a);
163009 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
163020 if( res==0 ) res = nLhs - nRhs;
163055 ** This function is called when initializing an incremental-merge operation.
163058 ** merge-writer object *pWriter is initialized to write to it.
163063 ** space pre-allocated), and
163096 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
163097 if( pWriter->nLeafData<0 ){
163098 pWriter->nLeafData = pWriter->nLeafData * -1;
163100 pWriter->bNoLeafData = (pWriter->nLeafData==0);
163107 /* Check for the zero-length marker in the %_segments table */
163139 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
163140 pWriter->iStart = iStart;
163141 pWriter->iEnd = iEnd;
163142 pWriter->iAbsLevel = iAbsLevel;
163143 pWriter->iIdx = iIdx;
163146 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
163149 pNode = &pWriter->aNodeWriter[nHeight];
163150 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
163151 blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
163153 memcpy(pNode->block.a, aRoot, nRoot);
163154 pNode->block.n = nRoot;
163157 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
163159 pNode = &pWriter->aNodeWriter[i];
163161 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
163163 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
163165 memcpy(pNode->key.a, reader.term.a, reader.term.n);
163166 pNode->key.n = reader.term.n;
163170 pNode = &pWriter->aNodeWriter[i-1];
163171 pNode->iBlock = reader.iChild;
163173 blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
163175 memcpy(pNode->block.a, aBlock, nBlock);
163176 pNode->block.n = nBlock;
163238 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
163241 ** (start_block + (1 + end_block - start_block) / 16). And so on.
163244 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
163263 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
163275 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
163276 pWriter->iEnd = pWriter->iStart - 1;
163277 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
163286 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
163289 pWriter->iAbsLevel = iAbsLevel;
163290 pWriter->nLeafEst = nLeafEst;
163291 pWriter->iIdx = iIdx;
163295 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
163305 ** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
163375 assert( p->bIgnoreSavepoint==0 );
163376 p->bIgnoreSavepoint = 1;
163385 p->bIgnoreSavepoint = 0;
163392 pNode->a[0] = (char)iHeight;
163394 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
163395 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
163397 assert( pNode->nAlloc>=1 );
163398 pNode->n = 1;
163403 ** The first two arguments are a pointer to and the size of a segment b-tree
163426 pNew->n = 0;
163433 if( pNew->n==0 ){
163445 if( pNew->n==0 ){
163449 assert( pNew->n<=pNew->nAlloc );
163516 sqlite3_bind_int64(pDel, 2, iNewStart-1);
163541 ** This function is called after an incrmental-merge operation has run to
163560 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
163565 ** somewhere in the pCsr->apSegment[] array. */
163566 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
163567 pSeg = pCsr->apSegment[j];
163568 if( pSeg->iIdx==i ) break;
163570 assert( j<pCsr->nSegment && pSeg->iIdx==i );
163572 if( pSeg->aNode==0 ){
163573 /* Seg-reader is at EOF. Remove the entire input segment. */
163576 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
163583 const char *zTerm = pSeg->zTerm;
163584 int nTerm = pSeg->nTerm;
163585 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
163590 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
163599 ** Store an incr-merge hint in the database.
163608 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163617 ** Load an incr-merge hint from the database. The incr-merge hint, if one
163628 pHint->n = 0;
163639 memcpy(pHint->a, aHint, nHint);
163640 pHint->n = nHint;
163652 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
163666 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
163668 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
163669 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
163682 const int nHint = pHint->n;
163685 i = pHint->n-2;
163686 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
163687 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
163689 pHint->n = i;
163690 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
163691 i += fts3GetVarint32(&pHint->a[i], pnInput);
163727 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
163736 ** set nSeg to -1.
163745 nSeg = -1;
163766 /* This undoes the effect of the HintPop() above - so that no entry
163781 ** done on iAbsLevel - fall through to the next iteration of the loop
163784 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
163793 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
163801 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
163806 const char *zKey = pCsr->zTerm;
163807 int nKey = pCsr->nTerm;
163808 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
163813 if( rc==SQLITE_OK && pWriter->nLeafEst ){
163818 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
163823 nRem -= (1 + pWriter->nWork);
163833 pWriter->nLeafData = pWriter->nLeafData * -1;
163836 if( nSeg==0 && pWriter->bNoLeafData==0 ){
163837 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
163844 /* Write the hint values into the %_stat table for the next incr-merger */
163865 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
163881 const char *zParam /* Nul-terminated string containing "A,B" */
163902 if( !p->bHasStat ){
163903 assert( p->bFts4==0 );
163924 const char *zParam /* Nul-terminated string containing boolean */
163928 p->nAutoincrmerge = fts3Getint(&zParam);
163929 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
163930 p->nAutoincrmerge = 8;
163932 if( !p->bHasStat ){
163933 assert( p->bFts4==0 );
163940 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
163947 ** Return a 64-bit checksum for the FTS index entry specified by the
163954 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
163983 int iIndex, /* Index to cksum (0..p->nIndex-1) */
164029 iPos += (iVal - 2);
164058 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
164064 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
164065 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
164069 for(i=0; i<p->nIndex; i++){
164079 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
164083 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
164087 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
164096 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
164097 if( p->abNotindexed[iCol]==0 ){
164102 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
164109 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
164115 for(i=1; i<p->nIndex; i++){
164116 if( p->aIndex[i].nPrefix<=nToken ){
164118 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
164124 if( pT ) pModule->xClose(pT);
164138 ** Run the integrity-check. If no error occurs and the current contents of
164145 ** The integrity-check works as follows. For each token and indexed token
164146 ** prefix in the document set, a 64-bit checksum is calculated (by code
164152 ** + The language-id of the row it appears in,
164160 ** The integrity-check code calculates the same checksum in two ways:
164165 ** If the two checksums are identical, the integrity-check is deemed to have
164197 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
164205 p->nNodeSize = atoi(&zVal[9]);
164208 p->nMaxPendingData = atoi(&zVal[11]);
164210 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
164211 p->bNoIncrDoclist = atoi(&zVal[21]);
164228 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
164229 fts3PendingListDelete(pDef->pList);
164230 pDef->pList = 0;
164235 ** Free all entries in the pCsr->pDeffered list. Entries are added to
164241 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
164242 pNext = pDef->pNext;
164243 fts3PendingListDelete(pDef->pList);
164246 pCsr->pDeferred = 0;
164250 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
164253 ** A deferred-doclist is like any other doclist with position information
164259 if( pCsr->pDeferred ){
164264 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
164265 sqlite3_tokenizer *pT = p->pTokenizer;
164266 sqlite3_tokenizer_module const *pModule = pT->pModule;
164268 assert( pCsr->isRequireSeek==0 );
164269 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
164271 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
164272 if( p->abNotindexed[i]==0 ){
164273 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
164276 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
164283 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
164284 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
164285 Fts3PhraseToken *pPT = pDef->pToken;
164286 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
164287 && (pPT->bFirst==0 || iPos==0)
164288 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
164289 && (0==memcmp(zToken, pPT->z, pPT->n))
164291 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
164295 if( pTC ) pModule->xClose(pTC);
164300 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
164301 if( pDef->pList ){
164302 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
164322 if( p->pList==0 ){
164326 pRet = (char *)sqlite3_malloc(p->pList->nData);
164329 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
164330 *pnData = p->pList->nData - nSkip;
164333 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
164338 ** Add an entry for token pToken to the pCsr->pDeferred list.
164343 int iCol /* Column that token must appear in (or -1) */
164351 pDeferred->pToken = pToken;
164352 pDeferred->pNext = pCsr->pDeferred;
164353 pDeferred->iCol = iCol;
164354 pCsr->pDeferred = pDeferred;
164356 assert( pToken->pDeferred==0 );
164357 pToken->pDeferred = pDeferred;
164388 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
164390 *pnChng = *pnChng - 1;
164391 if( p->zContentTbl==0 ){
164394 if( p->bHasDocsize ){
164433 assert( p->bHasStat==0 || p->bHasStat==1 );
164435 assert( p->pSegments==0 );
164438 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
164447 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
164449 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
164453 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
164459 aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
164464 aSzIns = &aSzDel[p->nColumn+1];
164465 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
164473 ** If the on-conflict mode is REPLACE, this means that the existing row
164475 ** if the on-conflict mode is other than REPLACE, then this method must
164479 if( nArg>1 && p->zContentTbl==0 ){
164481 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
164507 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
164528 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
164531 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164535 if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
164539 assert( p->iPrevDocid==*pRowid );
164542 if( p->bHasDocsize ){
164548 if( p->bFts4 ){
164559 ** Flush any data in the pending-terms hash table to disk. If successful,
164565 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
164569 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
164572 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
164573 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
164677 u32 *aMatchinfo; /* Pre-allocated buffer */
164702 int n; /* Length of z in bytes (excl. nul-term) */
164712 ** Allocate a two-slot MatchinfoBuffer object.
164722 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
164723 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
164724 pRet->nElem = nElem;
164725 pRet->zMatchinfo = ((char*)pRet) + nByte;
164726 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
164727 pRet->aRef[0] = 1;
164734 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
164736 assert( (u32*)p==&pBuf->aMatchinfo[1]
164737 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
164739 if( (u32*)p==&pBuf->aMatchinfo[1] ){
164740 pBuf->aRef[1] = 0;
164742 pBuf->aRef[2] = 0;
164745 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
164754 if( p->aRef[1]==0 ){
164755 p->aRef[1] = 1;
164756 aOut = &p->aMatchinfo[1];
164759 else if( p->aRef[2]==0 ){
164760 p->aRef[2] = 1;
164761 aOut = &p->aMatchinfo[p->nElem+2];
164764 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
164767 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
164776 p->bGlobal = 1;
164777 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
164785 assert( p->aRef[0]==1 );
164786 p->aRef[0] = 0;
164787 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
164799 ** This function is used to help iterate through a position-list. A position
164803 ** two. For example, to store the position-list:
164821 *piPos += (iVal-2);
164834 int eType = pExpr->eType; /* Type of expression node pExpr */
164837 assert( pExpr->pLeft && pExpr->pRight );
164838 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
164840 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
164851 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
164876 Fts3Phrase *pPhrase = pExpr->pPhrase;
164881 p->nPhrase++;
164882 p->nToken += pPhrase->nToken;
164893 ** indirectly descended from the right-hand-side of a NOT operator). If
164905 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
164913 pExpr->iPhrase = iPhrase;
164934 iIter = -1;
164952 if( pIter->iCurrent<0 ){
164957 pIter->iCurrent = 0;
164962 for(i=0; i<pIter->nPhrase; i++){
164963 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
164964 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
164970 for(i=0; i<pIter->nPhrase; i++){
164971 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
164972 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
164973 iEnd = pPhrase->iHead;
164980 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
164981 for(i=0; i<pIter->nPhrase; i++){
164982 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
164983 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
164984 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
165003 int iStart = pIter->iCurrent; /* First token of snippet */
165009 for(i=0; i<pIter->nPhrase; i++){
165010 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
165011 if( pPhrase->pTail ){
165012 char *pCsr = pPhrase->pTail;
165013 int iCsr = pPhrase->iTail;
165015 while( iCsr<(iStart+pIter->nSnippet) ){
165018 u64 mPos = (u64)1 << (iCsr - iStart);
165027 for(j=0; j<pPhrase->nToken; j++){
165050 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
165054 pPhrase->nToken = pExpr->pPhrase->nToken;
165055 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
165059 pPhrase->pList = pCsr;
165062 pPhrase->pHead = pCsr;
165063 pPhrase->pTail = pCsr;
165064 pPhrase->iHead = iFirst;
165065 pPhrase->iTail = iFirst;
165068 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
165103 int iBestScore = -1; /* Best snippet score found so far */
165133 sIter.iCurrent = -1;
165134 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
165147 pFragment->iCol = iCol;
165156 pFragment->iPos = iPos;
165157 pFragment->hlmask = mHighlite;
165158 pFragment->covered = mCover;
165171 ** Append a string to the string-buffer passed as the first argument.
165179 int nAppend /* Size of zAppend in bytes (or -1) */
165189 if( pStr->n+nAppend+1>=pStr->nAlloc ){
165190 int nAlloc = pStr->nAlloc+nAppend+100;
165191 char *zNew = sqlite3_realloc(pStr->z, nAlloc);
165195 pStr->z = zNew;
165196 pStr->nAlloc = nAlloc;
165198 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
165201 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
165202 pStr->n += nAppend;
165203 pStr->z[pStr->n] = '\0';
165212 ** and '.' is a non-highlighted term, BestSnippet() may select:
165218 ** non-highlighted terms to the right of the final highlighted term as there
165237 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
165245 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
165246 nDesired = (nLeft-nRight)/2;
165261 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
165266 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
165272 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
165274 pMod->xClose(pC);
165277 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
165303 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165310 int iPos = pFragment->iPos; /* First token of snippet */
165311 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
165312 int iCol = pFragment->iCol+1; /* Query column to extract text from */
165316 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
165318 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
165323 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
165326 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
165327 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
165334 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
165350 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
165353 /* Special case - the last token of the snippet is also the last token
165357 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
165364 int n = nDoc - iBegin;
165366 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
165376 rc = fts3StringAppend(pOut, zEllipsis, -1);
165386 rc = fts3StringAppend(pOut, zEllipsis, -1);
165392 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
165394 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
165395 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
165396 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
165397 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
165402 pMod->xClose(pC);
165408 ** This function is used to count the entries in a column-list (a
165409 ** delta-encoded list of term offsets within a single column of a single
165411 ** beginning of the first varint in the column-list (the varint that
165414 ** the last varint in the column-list (either the 0x00 signifying the end
165415 ** of the position-list, or the 0x01 that precedes the column number of
165416 ** the next column in the position-list).
165418 ** The number of elements in the column-list is returned.
165425 /* A column-list is terminated by either a 0x01 or 0x00. */
165442 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
165444 Fts3Phrase *pPhrase = pExpr->pPhrase;
165445 char *pIter = pPhrase->doclist.pList;
165448 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
165449 if( p->flag==FTS3_MATCHINFO_LHITS ){
165450 iStart = pExpr->iPhrase * p->nCol;
165452 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
165457 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
165458 if( p->flag==FTS3_MATCHINFO_LHITS ){
165459 p->aMatchinfo[iStart + iCol] = (u32)nHit;
165461 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
165478 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
165479 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
165480 if( pExpr->pLeft ){
165481 fts3ExprLHitGather(pExpr->pLeft, p);
165482 fts3ExprLHitGather(pExpr->pRight, p);
165512 ** file system. This is done because the full-text index doclist is required
165513 ** to calculate these values properly, and the full-text index doclist is
165523 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
165539 int iStart = iPhrase * p->nCol * 3;
165542 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
165544 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
165546 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
165548 p->aMatchinfo[iStart+i*3] = 0;
165562 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
165563 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
165564 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
165589 nVal = pInfo->nCol;
165593 nVal = pInfo->nCol * pInfo->nPhrase;
165597 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
165602 nVal = pInfo->nCol * pInfo->nPhrase * 3;
165637 ** iterating through a multi-column position-list corresponding to the
165671 char *pRead = pIter->pRead;
165680 pIter->iPos += (int)(iRead-2);
165683 pIter->pRead = pRead;
165690 ** If the call is successful, the longest-common-substring lengths for each
165691 ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
165695 ** data written to the first nCol elements of pInfo->aMatchinfo[] is
165707 aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
165709 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
165710 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
165712 for(i=0; i<pInfo->nPhrase; i++){
165714 nToken -= pIter->pExpr->pPhrase->nToken;
165715 pIter->iPosOffset = nToken;
165718 for(iCol=0; iCol<pInfo->nCol; iCol++){
165722 for(i=0; i<pInfo->nPhrase; i++){
165725 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
165727 if( pIt->pRead ){
165728 pIt->iPos = pIt->iPosOffset;
165738 for(i=0; i<pInfo->nPhrase; i++){
165740 if( pIter->pRead==0 ){
165744 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
165747 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
165755 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
165758 pInfo->aMatchinfo[iCol] = nLcs;
165766 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
165770 ** string has already been validated and the pInfo->aMatchinfo[] array
165780 ** pInfo->aMatchinfo[] buffer is left in is undefined.
165790 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165794 pInfo->flag = zArg[i];
165797 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
165801 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
165808 pInfo->aMatchinfo[0] = (u32)nDoc;
165820 for(iCol=0; iCol<pInfo->nCol; iCol++){
165825 pInfo->aMatchinfo[iCol] = iVal;
165833 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
165837 for(iCol=0; iCol<pInfo->nCol; iCol++){
165840 pInfo->aMatchinfo[iCol] = (u32)nToken;
165857 memset(pInfo->aMatchinfo, 0, nZero);
165858 fts3ExprLHitGather(pCsr->pExpr, pInfo);
165865 pExpr = pCsr->pExpr;
165869 if( pCsr->pDeferred ){
165870 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
165882 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
165891 ** Populate pCsr->aMatchinfo[] with data for the current row. The
165892 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
165900 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165909 sInfo.nCol = pTab->nColumn;
165914 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
165915 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
165916 pCsr->pMIBuffer = 0;
165924 if( pCsr->pMIBuffer==0 ){
165925 int nMatchinfo = 0; /* Number of u32 elements in match-info */
165929 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
165930 sInfo.nPhrase = pCsr->nPhrase;
165936 sqlite3_result_error(pCtx, zErr, -1);
165944 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
165945 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
165947 pCsr->isMatchinfoNeeded = 1;
165952 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
165960 sInfo.nPhrase = pCsr->nPhrase;
165963 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
165971 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
165982 const char *zStart, /* Snippet start text - "<b>" */
165983 const char *zEnd, /* Snippet end text - "</b>" */
165984 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
165988 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166003 int nFToken = -1; /* Number of tokens in each fragment */
166005 if( !pCsr->pExpr ){
166012 int iSnip; /* Loop counter 0..nSnippet-1 */
166017 nFToken = (nToken+nSnippet-1) / nSnippet;
166019 nFToken = -1 * nToken;
166023 int iBestScore = -1; /* Best score of columns checked so far */
166033 for(iRead=0; iRead<pTab->nColumn; iRead++){
166049 mCovered |= pFragment->covered;
166063 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
166073 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
166082 char *pList; /* Position-list */
166103 int iPos = 0; /* First position in position-list */
166107 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
166108 nTerm = pExpr->pPhrase->nToken;
166115 TermOffset *pT = &p->aTerm[p->iTerm++];
166116 pT->iOff = nTerm-iTerm-1;
166117 pT->pList = pList;
166118 pT->iPos = iPos;
166131 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166132 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
166139 if( !pCsr->pExpr ){
166145 assert( pCsr->isRequireSeek==0 );
166157 sCtx.iDocid = pCsr->iPrevId;
166161 ** string-buffer res for each column.
166163 for(iCol=0; iCol<pTab->nColumn; iCol++){
166179 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
166184 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
166187 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
166188 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
166190 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
166198 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
166203 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
166211 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
166212 iMinPos = pT->iPos-pT->iOff;
166222 if( 0==(0xFE&*pTerm->pList) ){
166223 pTerm->pList = 0;
166225 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
166228 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
166233 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
166235 rc = fts3StringAppend(&res, aBuffer, -1);
166236 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
166245 pMod->xClose(pC);
166257 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
166270 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166279 if( !pCsr->pExpr ){
166305 ** Implementation of the "unicode" full-text-search tokenizer.
166321 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
166341 c = sqlite3Utf8Trans1[c-0xc0]; \
166399 sqlite3_free(p->aiException);
166450 aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
166452 nNew = p->nException;
166462 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
166467 p->aiException = aNew;
166468 p->nException = nNew;
166475 ** Return true if the p->aiException[] array contains the value iCode.
166478 if( p->nException>0 ){
166479 int *a = p->aiException;
166481 int iHi = p->nException-1;
166490 iHi = iTest-1;
166522 pNew->bRemoveDiacritic = 1;
166529 pNew->bRemoveDiacritic = 1;
166532 pNew->bRemoveDiacritic = 0;
166535 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
166538 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
166556 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
166574 pCsr->aInput = (const unsigned char *)aInput;
166576 pCsr->nInput = 0;
166578 pCsr->nInput = (int)strlen(aInput);
166580 pCsr->nInput = nInput;
166583 *pp = &pCsr->base;
166594 sqlite3_free(pCsr->zToken);
166612 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
166615 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
166618 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
166630 zOut = pCsr->zToken;
166635 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
166636 char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
166638 zOut = &zNew[zOut - pCsr->zToken];
166639 pCsr->zToken = zNew;
166640 pCsr->nAlloc += 64;
166645 iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
166658 pCsr->iOff = (int)(z - pCsr->aInput);
166659 *paToken = pCsr->zToken;
166660 *pnToken = (int)(zOut - pCsr->zToken);
166661 *piStart = (int)(zStart - pCsr->aInput);
166662 *piEnd = (int)(zEnd - pCsr->aInput);
166663 *piPos = pCsr->iToken++;
166723 ** The most significant 22 bits in each 32-bit value contain the first
166823 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
166831 iHi = iTest-1;
166845 ** of the ASCII letter only. For example, if passed 235 - "LATIN
166846 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
166880 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
166888 iHi = iTest-1;
166905 (mask0 & (1 << (c-768))) :
166906 (mask1 & (1 << (c-768-32)));
167016 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
167019 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
167021 int iRes = -1;
167026 int cmp = (c - aEntry[iTest].iCode);
167031 iHi = iTest-1;
167037 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
167038 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
167067 ** This file contains code for implementations of the r-tree and r*-tree
167072 ** Database Format of R-Tree Tables
167073 ** --------------------------------
167075 ** The data structure for a single virtual r-tree table is stored in three
167077 ** in the table name is replaced with the user-supplied name of the r-tree
167084 ** The data for each node of the r-tree structure is stored in the %_node
167085 ** table. For each node that is not the root node of the r-tree, there is
167091 ** The root node of an r-tree always exists, even if the r-tree table is
167097 ** of the node contain the tree depth as a big-endian integer.
167098 ** For non-root nodes, the first 2 bytes are left unused.
167104 ** consists of a single 8-byte integer followed by an even number
167105 ** of 4-byte coordinates. For leaf nodes the integer is the rowid
167161 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
167169 ** An rtree virtual-table object.
167180 int iDepth; /* Current depth of the r-tree structure */
167181 char *zDb; /* Name of database containing r-tree table */
167182 char *zName; /* Name of r-tree table */
167188 ** linked together via the pointer normally used for hash chains -
167189 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
167193 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
167212 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
167235 ** When doing a search of an r-tree, instances of the following structure
167238 ** The id is always a node-id. For iLevel>=1 the id is the node-id of
167241 ** represents is the iCell-th entry in the parent node.
167257 ** If an R*-tree "Reinsert" operation is required, the same number of
167260 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
167265 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
167267 ** Therefore all non-root nodes must contain at least 3 entries. Since
167268 ** 3^40 is greater than 2^64, an r-tree structure always has a depth of
167301 #define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
167305 ** coordinates within a single R-Tree are always of the same time.
167310 u32 u; /* Unsigned for byte-order conversions */
167323 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
167349 #define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
167350 #define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
167366 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
167384 ** xGeom and xQueryFunc fields is non-NULL, depending on whether the
167401 ** sqlite3_rtree_query_callback() create, and is read as the right-hand
167402 ** operand to the MATCH operator of an R-Tree.
167433 ** it is not, make it a no-op.
167441 ** and whether or not that determination is run-time or compile-time.
167443 ** For best performance, an attempt is made to guess at the byte-order
167444 ** using C-preprocessor macros. If that is unsuccessful, or if
167445 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
167446 ** at run-time.
167457 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
167479 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
167481 pCoord->u = _byteswap_ulong(*(u32*)p);
167483 pCoord->u = __builtin_bswap32(*(u32*)p);
167485 pCoord->u = *(u32*)p;
167487 pCoord->u = (
167533 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
167537 i = __builtin_bswap32(pCoord->u);
167540 i = _byteswap_ulong(pCoord->u);
167543 i = pCoord->u;
167546 i = pCoord->u;
167581 p->nRef++;
167589 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
167590 p->isDirty = 1;
167607 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
167616 assert( pNode->pNext==0 );
167617 iHash = nodeHash(pNode->iNode);
167618 pNode->pNext = pRtree->aHash[iHash];
167619 pRtree->aHash[iHash] = pNode;
167627 if( pNode->iNode!=0 ){
167628 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
167629 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
167630 *pp = pNode->pNext;
167631 pNode->pNext = 0;
167636 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
167643 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
167645 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
167646 pNode->zData = (u8 *)&pNode[1];
167647 pNode->nRef = 1;
167648 pNode->pParent = pParent;
167649 pNode->isDirty = 1;
167659 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
167660 sqlite3_blob *pBlob = pRtree->pNodeBlob;
167661 pRtree->pNodeBlob = 0;
167667 ** Obtain a reference to an r-tree node.
167670 Rtree *pRtree, /* R-tree structure */
167682 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
167683 if( pParent && !pNode->pParent ){
167685 pNode->pParent = pParent;
167687 pNode->nRef++;
167692 if( pRtree->pNodeBlob ){
167693 sqlite3_blob *pBlob = pRtree->pNodeBlob;
167694 pRtree->pNodeBlob = 0;
167696 pRtree->pNodeBlob = pBlob;
167702 if( pRtree->pNodeBlob==0 ){
167703 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
167705 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
167706 &pRtree->pNodeBlob);
167715 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
167716 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
167720 pNode->pParent = pParent;
167721 pNode->zData = (u8 *)&pNode[1];
167722 pNode->nRef = 1;
167723 pNode->iNode = iNode;
167724 pNode->isDirty = 0;
167725 pNode->pNext = 0;
167726 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
167727 pRtree->iNodeSize, 0);
167732 /* If the root node was just loaded, set pRtree->iDepth to the height
167733 ** of the r-tree structure. A height of zero means all data is stored on
167736 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
167739 pRtree->iDepth = readInt16(pNode->zData);
167740 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
167750 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
167774 Rtree *pRtree, /* The overall R-Tree */
167780 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
167781 p += writeInt64(p, pCell->iRowid);
167782 for(ii=0; ii<pRtree->nDim2; ii++){
167783 p += writeCoord(p, &pCell->aCoord[ii]);
167785 pNode->isDirty = 1;
167792 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
167793 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
167794 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
167796 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
167797 pNode->isDirty = 1;
167807 Rtree *pRtree, /* The overall R-Tree */
167814 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
167820 writeInt16(&pNode->zData[2], nCell+1);
167821 pNode->isDirty = 1;
167832 if( pNode->isDirty ){
167833 sqlite3_stmt *p = pRtree->pWriteNode;
167834 if( pNode->iNode ){
167835 sqlite3_bind_int64(p, 1, pNode->iNode);
167839 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167841 pNode->isDirty = 0;
167843 if( pNode->iNode==0 && rc==SQLITE_OK ){
167844 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167858 assert( pNode->nRef>0 );
167859 pNode->nRef--;
167860 if( pNode->nRef==0 ){
167861 if( pNode->iNode==1 ){
167862 pRtree->iDepth = -1;
167864 if( pNode->pParent ){
167865 rc = nodeRelease(pRtree, pNode->pParent);
167878 ** Return the 64-bit integer value associated with cell iCell of
167880 ** an internal node, then the 64-bit integer is a child page number.
167883 Rtree *pRtree, /* The overall R-Tree */
167888 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
167895 Rtree *pRtree, /* The overall R-Tree */
167901 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
167909 Rtree *pRtree, /* The overall R-Tree */
167917 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
167918 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
167919 pCoord = pCell->aCoord;
167925 }while( ii<pRtree->nDim2 );
167963 ** Increment the r-tree reference count.
167966 pRtree->nBusy++;
167970 ** Decrement the r-tree reference count. When the reference count reaches
167974 pRtree->nBusy--;
167975 if( pRtree->nBusy==0 ){
167976 pRtree->inWrTrans = 0;
167977 pRtree->nCursor = 0;
167979 sqlite3_finalize(pRtree->pWriteNode);
167980 sqlite3_finalize(pRtree->pDeleteNode);
167981 sqlite3_finalize(pRtree->pReadRowid);
167982 sqlite3_finalize(pRtree->pWriteRowid);
167983 sqlite3_finalize(pRtree->pDeleteRowid);
167984 sqlite3_finalize(pRtree->pReadParent);
167985 sqlite3_finalize(pRtree->pWriteParent);
167986 sqlite3_finalize(pRtree->pDeleteParent);
168009 pRtree->zDb, pRtree->zName,
168010 pRtree->zDb, pRtree->zName,
168011 pRtree->zDb, pRtree->zName
168017 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
168038 pCsr->base.pVtab = pVTab;
168040 pRtree->nCursor++;
168052 if( pCsr->aConstraint ){
168054 for(i=0; i<pCsr->nConstraint; i++){
168055 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
168057 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
168061 sqlite3_free(pCsr->aConstraint);
168062 pCsr->aConstraint = 0;
168070 Rtree *pRtree = (Rtree *)(cur->pVtab);
168073 assert( pRtree->nCursor>0 );
168075 sqlite3_free(pCsr->aPoint);
168076 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
168078 pRtree->nCursor--;
168086 ** Return non-zero if the cursor does not currently point to a valid
168091 return pCsr->atEOF;
168095 ** Convert raw bits from the on-disk RTree record into a coordinate value.
168096 ** The on-disk format is big-endian and needs to be converted for little-
168097 ** endian platforms. The on-disk record stores integer coordinates if
168098 ** eInt is true and it stores 32-bit floating point records if eInt is
168099 ** false. a[] is the four bytes of the on-disk record to be decoded.
168103 ** other four are various architectures-specific optimizations.
168152 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
168153 int nCoord = pInfo->nCoord; /* No. of coordinates */
168158 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
168161 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
168162 pInfo->iRowid = readInt64(pCellData);
168195 if( pConstraint->op==RTREE_MATCH ){
168197 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
168202 pInfo->aCoord = aCoord;
168203 pInfo->iLevel = pSearch->iLevel - 1;
168204 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
168205 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
168206 rc = pConstraint->u.xQueryFunc(pInfo);
168207 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
168208 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
168209 *prScore = pInfo->rScore;
168228 /* p->iCoord might point to either a lower or upper bound coordinate
168231 pCellData += 8 + 4*(p->iCoord&0xfe);
168233 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
168234 || p->op==RTREE_GT || p->op==RTREE_EQ );
168235 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
168236 switch( p->op ){
168242 if( p->u.rValue>=val ) return;
168243 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
168250 if( p->u.rValue<=val ) return;
168262 ** The op is given by p->op. The xN is p->iCoord-th coordinate in
168263 ** pCellData. $val is given by p->u.rValue.
168273 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
168274 || p->op==RTREE_GT || p->op==RTREE_EQ );
168275 pCellData += 8 + p->iCoord*4;
168276 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
168278 switch( p->op ){
168279 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
168280 case RTREE_LT: if( xN < p->u.rValue ) return; break;
168281 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
168282 case RTREE_GT: if( xN > p->u.rValue ) return; break;
168283 default: if( xN == p->u.rValue ) return; break;
168289 ** One of the cells in node pNode is guaranteed to have a 64-bit
168312 ** in its parent. If pNode is the root node, return -1.
168315 RtreeNode *pParent = pNode->pParent;
168317 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
168319 *piIndex = -1;
168331 ** is a depth-first search, which is the desired default behavior.
168337 if( pA->rScore<pB->rScore ) return -1;
168338 if( pA->rScore>pB->rScore ) return +1;
168339 if( pA->iLevel<pB->iLevel ) return -1;
168340 if( pA->iLevel>pB->iLevel ) return +1;
168348 RtreeSearchPoint t = p->aPoint[i];
168350 p->aPoint[i] = p->aPoint[j];
168351 p->aPoint[j] = t;
168355 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
168356 p->aNode[i] = 0;
168358 RtreeNode *pTemp = p->aNode[i];
168359 p->aNode[i] = p->aNode[j];
168360 p->aNode[j] = pTemp;
168369 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
168377 int ii = 1 - pCur->bPoint;
168379 assert( pCur->bPoint || pCur->nPoint );
168380 if( pCur->aNode[ii]==0 ){
168382 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
168383 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
168385 return pCur->aNode[ii];
168398 if( pCur->nPoint>=pCur->nPointAlloc ){
168399 int nNew = pCur->nPointAlloc*2 + 8;
168400 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
168402 pCur->aPoint = pNew;
168403 pCur->nPointAlloc = nNew;
168405 i = pCur->nPoint++;
168406 pNew = pCur->aPoint + i;
168407 pNew->rScore = rScore;
168408 pNew->iLevel = iLevel;
168412 j = (i-1)/2;
168413 pParent = pCur->aPoint + j;
168433 pCur->anQueue[iLevel]++;
168435 || pFirst->rScore>rScore
168436 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
168438 if( pCur->bPoint ){
168442 ii = (int)(pNew - pCur->aPoint) + 1;
168444 assert( pCur->aNode[ii]==0 );
168445 pCur->aNode[ii] = pCur->aNode[0];
168447 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
168449 pCur->aNode[0] = 0;
168450 *pNew = pCur->sPoint;
168452 pCur->sPoint.rScore = rScore;
168453 pCur->sPoint.iLevel = iLevel;
168454 pCur->bPoint = 1;
168455 return &pCur->sPoint;
168466 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
168470 printf(" %p\n", pCur->aNode[idx]);
168478 if( pCur->bPoint ){
168479 tracePoint(&pCur->sPoint, -1, pCur);
168481 for(ii=0; ii<pCur->nPoint; ii++){
168482 if( ii>0 || pCur->bPoint ) printf(" ");
168483 tracePoint(&pCur->aPoint[ii], ii, pCur);
168488 # define RTREE_QUEUE_TRACE(A,B) /* no-op */
168495 i = 1 - p->bPoint;
168497 if( p->aNode[i] ){
168498 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
168499 p->aNode[i] = 0;
168501 if( p->bPoint ){
168502 p->anQueue[p->sPoint.iLevel]--;
168503 p->bPoint = 0;
168504 }else if( p->nPoint ){
168505 p->anQueue[p->aPoint[0].iLevel]--;
168506 n = --p->nPoint;
168507 p->aPoint[0] = p->aPoint[n];
168508 if( n<RTREE_CACHE_SZ-1 ){
168509 p->aNode[1] = p->aNode[n+1];
168510 p->aNode[n+1] = 0;
168515 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
168516 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
168523 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
168537 ** contains an entry suitable for returning as a result-set row,
168548 int nConstraint = pCur->nConstraint;
168553 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
168554 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
168559 while( p->iCell<nCell ){
168560 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
168561 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
168564 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
168565 if( pConstraint->op>=RTREE_MATCH ){
168569 }else if( p->iLevel==1 ){
168576 p->iCell++;
168578 x.iLevel = p->iLevel - 1;
168583 x.id = p->id;
168584 x.iCell = p->iCell - 1;
168586 if( p->iCell>=nCell ){
168587 RTREE_QUEUE_TRACE(pCur, "POP-S:");
168593 p->eWithin = (u8)eWithin;
168594 p->id = x.id;
168595 p->iCell = x.iCell;
168596 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
168599 if( p->iCell>=nCell ){
168600 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
168604 pCur->atEOF = p==0;
168616 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
168631 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
168640 Rtree *pRtree = (Rtree *)cur->pVtab;
168650 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
168652 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
168654 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
168659 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
168677 sqlite3_int64 *piNode /* Write the node-id here */
168681 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
168682 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
168683 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
168686 sqlite3_reset(pRtree->pReadRowid);
168688 rc = sqlite3_reset(pRtree->pReadRowid);
168696 ** first argument to this function is the right-hand operand to the MATCH
168706 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
168710 memcpy(pBlob, pSrc, pSrc->iSize);
168711 pInfo->pContext = pBlob->cb.pContext;
168712 pInfo->nParam = pBlob->nParam;
168713 pInfo->aParam = pBlob->aParam;
168714 pInfo->apSqlParam = pBlob->apSqlParam;
168716 if( pBlob->cb.xGeom ){
168717 pCons->u.xGeom = pBlob->cb.xGeom;
168719 pCons->op = RTREE_QUERY;
168720 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
168722 pCons->pInfo = pInfo;
168734 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
168745 sqlite3_free(pCsr->aPoint);
168747 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
168749 pCsr->iStrategy = idxNum;
168751 /* Special case - lookup by rowid. */
168759 assert( p!=0 ); /* Always returns pCsr->sPoint */
168760 pCsr->aNode[0] = pLeaf;
168761 p->id = iNode;
168762 p->eWithin = PARTLY_WITHIN;
168764 p->iCell = (u8)iCell;
168765 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
168767 pCsr->atEOF = 1;
168770 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
168775 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
168776 pCsr->nConstraint = argc;
168777 if( !pCsr->aConstraint ){
168780 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
168781 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
168785 RtreeConstraint *p = &pCsr->aConstraint[ii];
168786 p->op = idxStr[ii*2];
168787 p->iCoord = idxStr[ii*2+1]-'0';
168788 if( p->op>=RTREE_MATCH ){
168789 /* A MATCH operator. The right-hand-side must be a blob that
168797 p->pInfo->nCoord = pRtree->nDim2;
168798 p->pInfo->anQueue = pCsr->anQueue;
168799 p->pInfo->mxLevel = pRtree->iDepth + 1;
168802 p->u.rValue = sqlite3_value_int64(argv[ii]);
168804 p->u.rValue = sqlite3_value_double(argv[ii]);
168812 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
168814 pNew->id = 1;
168815 pNew->iCell = 0;
168816 pNew->eWithin = PARTLY_WITHIN;
168817 assert( pCsr->bPoint==1 );
168818 pCsr->aNode[0] = pRoot;
168820 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
168836 ** ------------------------------------------------
168838 ** 2 See below R-tree query or full-table scan.
168839 ** ------------------------------------------------
168851 ** ----------------------
168858 ** ----------------------
168875 /* Check if there exists a MATCH constraint - even an unusable one. If there
168876 ** is, do not consider the lookup-by-rowid plan as using such a plan would
168879 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
168880 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
168885 assert( pIdxInfo->idxStr==0 );
168886 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
168887 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
168889 if( bMatch==0 && p->usable
168890 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
168895 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
168896 pIdxInfo->aConstraintUsage[jj].omit = 0;
168898 pIdxInfo->idxNum = 1;
168899 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
168900 pIdxInfo->aConstraintUsage[jj].omit = 1;
168902 /* This strategy involves a two rowid lookups on an B-Tree structures
168903 ** and then a linear search of an R-Tree node. This should be
168908 pIdxInfo->estimatedCost = 30.0;
168909 pIdxInfo->estimatedRows = 1;
168913 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
168915 switch( p->op ){
168922 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
168927 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
168928 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
168929 pIdxInfo->aConstraintUsage[ii].omit = 1;
168933 pIdxInfo->idxNum = 2;
168934 pIdxInfo->needToFreeIdxStr = 1;
168935 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
168939 nRow = pRtree->nRowEst >> (iIdx/2);
168940 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
168941 pIdxInfo->estimatedRows = nRow;
168947 ** Return the N-dimensional volumn of the cell stored in *p.
168951 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
168953 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
168954 switch( pRtree->nDim ){
168955 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
168956 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
168957 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
168958 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
168959 default: area *= p->aCoord[1].f - p->aCoord[0].f;
168964 switch( pRtree->nDim ){
168965 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
168966 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
168967 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
168968 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
168969 default: area *= p->aCoord[1].i - p->aCoord[0].i;
168981 int ii = pRtree->nDim2 - 2;
168983 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
168984 ii -= 2;
168994 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
168996 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
168997 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
168999 }while( ii<pRtree->nDim2 );
169002 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
169003 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
169005 }while( ii<pRtree->nDim2 );
169015 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
169016 for(ii=0; ii<pRtree->nDim2; ii+=2){
169017 RtreeCoord *a1 = &p1->aCoord[ii];
169018 RtreeCoord *a2 = &p2->aCoord[ii];
169037 return (cellArea(pRtree, &cell)-area);
169051 for(jj=0; jj<pRtree->nDim2; jj+=2){
169053 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
169054 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
169059 o = o * (x2-x1);
169075 int iHeight, /* Height of sub-tree rooted at pCell */
169083 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
169138 while( p->pParent ){
169139 RtreeNode *pParent = p->pParent;
169159 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
169162 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
169163 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
169164 sqlite3_step(pRtree->pWriteRowid);
169165 return sqlite3_reset(pRtree->pWriteRowid);
169169 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
169172 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
169173 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
169174 sqlite3_step(pRtree->pWriteParent);
169175 return sqlite3_reset(pRtree->pWriteParent);
169184 ** (nIdx-1) in no particular order. This function sorts the values
169209 int nRight = nIdx-nLeft;
169244 RtreeDValue left = aDistance[aIdx[jj-1]];
169256 ** (nIdx-1) in no particular order. This function sorts the values
169278 int nRight = nIdx-nLeft;
169309 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
169310 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
169321 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
169340 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
169347 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
169349 for(ii=0; ii<pRtree->nDim; ii++){
169351 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
169358 for(ii=0; ii<pRtree->nDim; ii++){
169367 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
169377 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
169378 for(kk=1; kk<(nCell-1); kk++){
169432 nodeRelease(pRtree, pChild->pParent);
169434 pChild->pParent = pNode;
169437 return xSetMapping(pRtree, iRowid, pNode->iNode);
169477 if( pNode->iNode==1 ){
169480 pRtree->iDepth++;
169481 pNode->isDirty = 1;
169482 writeInt16(pNode->zData, pRtree->iDepth);
169485 pRight = nodeNew(pRtree, pLeft->pParent);
169494 memset(pLeft->zData, 0, pRtree->iNodeSize);
169495 memset(pRight->zData, 0, pRtree->iNodeSize);
169509 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
169514 rightbbox.iRowid = pRight->iNode;
169515 leftbbox.iRowid = pLeft->iNode;
169517 if( pNode->iNode==1 ){
169518 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
169523 RtreeNode *pParent = pLeft->pParent;
169534 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
169541 if( iRowid==pCell->iRowid ){
169548 if( pNode->iNode==1 ){
169557 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
169577 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
169579 ** the pLeaf->pParent chain all the way up to the root node.
169581 ** This operation is required when a row is deleted (or updated - an update
169590 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
169592 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
169593 rc = sqlite3_step(pRtree->pReadParent);
169598 /* Before setting pChild->pParent, test that we are not creating a
169603 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
169604 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
169606 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
169609 rc = sqlite3_reset(pRtree->pReadParent);
169611 if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
169612 pChild = pChild->pParent;
169625 assert( pNode->nRef==1 );
169630 pParent = pNode->pParent;
169631 pNode->pParent = 0;
169643 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
169644 sqlite3_step(pRtree->pDeleteNode);
169645 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
169650 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
169651 sqlite3_step(pRtree->pDeleteParent);
169652 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
169656 /* Remove the node from the in-memory hash table and link it into
169657 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
169660 pNode->iNode = iHeight;
169661 pNode->pNext = pRtree->pDeleted;
169662 pNode->nRef++;
169663 pRtree->pDeleted = pNode;
169669 RtreeNode *pParent = pNode->pParent;
169681 box.iRowid = pNode->iNode;
169693 ** cell, adjust the r-tree data structure if required.
169704 ** the in-memory node image, so it cannot fail.
169713 pParent = pNode->pParent;
169714 assert( pParent || pNode->iNode==1 );
169765 if( ii==(nCell-1) ){
169771 for(iDim=0; iDim<pRtree->nDim; iDim++){
169776 for(iDim=0; iDim<pRtree->nDim; iDim++){
169782 for(iDim=0; iDim<pRtree->nDim; iDim++){
169783 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
169785 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
169792 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
169795 if( p->iRowid==pCell->iRowid ){
169797 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
169799 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
169807 /* Find a node to store this cell in. pNode->iNode currently contains
169808 ** the height of the sub-tree headed by the cell.
169839 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
169841 nodeRelease(pRtree, pChild->pParent);
169843 pChild->pParent = pNode;
169847 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
169850 pRtree->iReinsertHeight = iHeight;
169857 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
169859 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
169876 /* Find a node to store this cell in. pNode->iNode currently contains
169877 ** the height of the sub-tree headed by the cell.
169879 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
169882 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
169893 ** Select a currently unused rowid for a new r-tree record.
169897 sqlite3_bind_null(pRtree->pWriteRowid, 1);
169898 sqlite3_bind_null(pRtree->pWriteRowid, 2);
169899 sqlite3_step(pRtree->pWriteRowid);
169900 rc = sqlite3_reset(pRtree->pWriteRowid);
169901 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
169906 ** Remove the entry with rowid=iDelete from the r-tree structure.
169940 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
169941 sqlite3_step(pRtree->pDeleteRowid);
169942 rc = sqlite3_reset(pRtree->pDeleteRowid);
169953 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
169959 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
169964 pRtree->iDepth--;
169965 writeInt16(pRoot->zData, pRtree->iDepth);
169966 pRoot->isDirty = 1;
169970 /* Re-insert the contents of any underfull nodes removed from the tree. */
169971 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
169975 pRtree->pDeleted = pLeaf->pNext;
169990 ** Rounding constants for float->double conversion.
169992 #define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
170021 ** (at pRtree->base.zErrMsg) to an appropriate value and returns
170037 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
170039 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
170048 pRtree->base.zErrMsg = sqlite3_mprintf(
170049 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
170054 pRtree->base.zErrMsg = sqlite3_mprintf(
170055 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
170085 /* Constraint handling. A write operation on an r-tree table may return
170091 ** In the first case, if the conflict-handling mode is REPLACE, then
170094 ** conflict-handling mode specified by the user.
170101 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
170107 assert( nData<=(pRtree->nDim2 + 3) );
170110 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
170111 for(ii=0; ii<nData-4; ii+=2){
170122 for(ii=0; ii<nData-4; ii+=2){
170140 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
170141 steprc = sqlite3_step(pRtree->pReadRowid);
170142 rc = sqlite3_reset(pRtree->pReadRowid);
170144 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
170157 ** record to delete from the r-tree table. The following block does
170165 ** (azData[2]..azData[argc-1]) contain a new record to insert into
170166 ** the r-tree structure.
170169 /* Insert the new record into the r-tree */
170183 pRtree->iReinsertHeight = -1;
170202 assert( pRtree->inWrTrans==0 );
170203 pRtree->inWrTrans++;
170213 pRtree->inWrTrans = 0;
170228 , pRtree->zDb, pRtree->zName, zNewName
170229 , pRtree->zDb, pRtree->zName, zNewName
170230 , pRtree->zDb, pRtree->zName, zNewName
170234 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
170245 ** DROP TABLE command - which fortunately always opens a savepoint - cannot
170251 ** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
170256 int iwt = pRtree->inWrTrans;
170258 pRtree->inWrTrans = 0;
170260 pRtree->inWrTrans = iwt;
170265 ** This function populates the pRtree->nRowEst variable with an estimate
170277 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
170280 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
170283 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
170287 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
170297 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
170299 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
170310 rtreeCreate, /* xCreate - create a table */
170311 rtreeConnect, /* xConnect - connect to an existing table */
170312 rtreeBestIndex, /* xBestIndex - Determine search strategy */
170313 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
170314 rtreeDestroy, /* xDestroy - Drop a table */
170315 rtreeOpen, /* xOpen - open a cursor */
170316 rtreeClose, /* xClose - close a cursor */
170317 rtreeFilter, /* xFilter - configure scan constraints */
170318 rtreeNext, /* xNext - advance a cursor */
170320 rtreeColumn, /* xColumn - read data */
170321 rtreeRowid, /* xRowid - read data */
170322 rtreeUpdate, /* xUpdate - write data */
170323 rtreeBeginTransaction, /* xBegin - begin transaction */
170324 rtreeEndTransaction, /* xSync - sync transaction */
170325 rtreeEndTransaction, /* xCommit - commit transaction */
170326 rtreeEndTransaction, /* xRollback - rollback transaction */
170327 0, /* xFindFunction - function overloading */
170328 rtreeRename, /* xRename - rename the table */
170362 pRtree->db = db;
170371 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
170383 appStmt[0] = &pRtree->pWriteNode;
170384 appStmt[1] = &pRtree->pDeleteNode;
170385 appStmt[2] = &pRtree->pReadRowid;
170386 appStmt[3] = &pRtree->pWriteRowid;
170387 appStmt[4] = &pRtree->pDeleteRowid;
170388 appStmt[5] = &pRtree->pReadParent;
170389 appStmt[6] = &pRtree->pWriteParent;
170390 appStmt[7] = &pRtree->pDeleteParent;
170396 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
170418 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
170431 ** determine the node-size used by the rtree table being created or connected
170432 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
170436 ** table already exists. In this case the node-size is determined by inspecting
170439 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
170441 ** database page-size is so large that more than RTREE_MAXCELLS entries
170442 ** would fit in a single node, use a smaller node-size.
170454 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
170457 pRtree->iNodeSize = iPageSize-64;
170458 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
170459 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
170467 pRtree->zDb, pRtree->zName
170469 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
170472 }else if( pRtree->iNodeSize<(512-64) ){
170475 pRtree->zName);
170485 ** methods of the r-tree virtual table.
170487 ** argv[0] -> module name
170488 ** argv[1] -> database name
170489 ** argv[2] -> table name
170490 ** argv[...] -> column names...
170529 pRtree->nBusy = 1;
170530 pRtree->base.pModule = &rtreeModule;
170531 pRtree->zDb = (char *)&pRtree[1];
170532 pRtree->zName = &pRtree->zDb[nDb+1];
170533 pRtree->nDim = (u8)((argc-4)/2);
170534 pRtree->nDim2 = pRtree->nDim*2;
170535 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
170536 pRtree->eCoordType = (u8)eCoordType;
170537 memcpy(pRtree->zDb, argv[1], nDb);
170538 memcpy(pRtree->zName, argv[2], nName);
170545 ** the r-tree table schema.
170577 assert( pRtree->nBusy==1 );
170586 ** Implementation of a scalar function that decodes r-tree nodes to
170591 ** an r-tree node. For a two-dimensional r-tree structure called "rt", to
170597 ** entry for each cell in the r-tree node. Each entry is itself a
170598 ** list, containing the 8-byte rowid/pageno followed by the
170599 ** <num-dimension>*2 coordinates.
170622 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
170626 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
170629 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
170644 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
170649 ** from the front of a blob that is an r-tree node. For example:
170662 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
170671 ** implementation of integrity-check function rtreecheck().
170683 int nNonLeaf; /* Number of non-leaf cells in table */
170697 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
170719 if( pCheck->rc==SQLITE_OK ){
170721 pCheck->rc = SQLITE_NOMEM;
170723 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
170740 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
170743 pCheck->rc = SQLITE_NOMEM;
170745 pCheck->zReport = sqlite3_mprintf("%z%s%z",
170746 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
170748 if( pCheck->zReport==0 ){
170749 pCheck->rc = SQLITE_NOMEM;
170752 pCheck->nErr++;
170758 ** This function is a no-op if there is already an error code stored
170774 assert( pCheck->rc==SQLITE_OK );
170775 if( pCheck->pGetNode==0 ){
170776 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
170778 pCheck->zDb, pCheck->zTab
170782 if( pCheck->rc==SQLITE_OK ){
170783 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
170784 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
170785 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
170786 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
170789 pCheck->rc = SQLITE_NOMEM;
170795 rtreeCheckReset(pCheck, pCheck->pGetNode);
170796 if( pCheck->rc==SQLITE_OK && pRet==0 ){
170830 if( pCheck->aCheckMapping[bLeaf]==0 ){
170831 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
170832 azSql[bLeaf], pCheck->zDb, pCheck->zTab
170835 if( pCheck->rc!=SQLITE_OK ) return;
170837 pStmt = pCheck->aCheckMapping[bLeaf];
170841 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
170848 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
170879 for(i=0; i<pCheck->nDim; i++){
170884 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
170894 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
170895 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
170908 ** the r-tree structure. Argument aParent points to the array of coordinates
170924 assert( pCheck->nDim>0 );
170944 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
170951 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
170957 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
170958 pCheck->nNonLeaf++;
170961 pCheck->nLeaf++;
170978 if( pCheck->rc==SQLITE_OK ){
170981 pCheck->zDb, pCheck->zTab, zTbl
170988 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
170992 pCheck->rc = sqlite3_finalize(pCount);
170998 ** This function does the bulk of the work for the rtree integrity-check.
171018 ** to ensure that the queries run as part of this integrity-check operate
171029 check.nDim = (sqlite3_column_count(pStmt) - 1) / 2;
171039 /* Do the actual integrity-check */
171048 /* Finalize SQL statements used by the integrity-check */
171065 ** rtreecheck(<rtree-table>);
171066 ** rtreecheck(<database>, <rtree-table>);
171068 ** Invoking this SQL function runs an integrity-check on the named rtree
171069 ** table. The integrity-check verifies the following:
171071 ** 1. For each cell in the r-tree structure (%_node table), that:
171082 ** d) for cells on non-leaf nodes, that there is an entry in the
171087 ** as there are leaf cells in the r-tree structure, and that there
171091 ** as there are non-leaf cells in the r-tree structure, and that
171092 ** there is a non-leaf cell that corresponds to each entry in the
171102 "wrong number of arguments to function rtreecheck()", -1
171117 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
171127 ** Register the r-tree module with database handle db. This creates the
171142 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
171169 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
171179 for(i=0; i<p->nParam; i++){
171180 sqlite3_value_free(p->apSqlParam[i]);
171191 ** contains the geometry-checking callback routines and a list of
171195 ** The R-Tree MATCH operator will read the returned BLOB, deserialize
171197 ** out which elements of the R-Tree should be returned by the query.
171205 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
171212 pBlob->iSize = nBlob;
171213 pBlob->cb = pGeomCtx[0];
171214 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
171215 pBlob->nParam = nArg;
171217 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
171218 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
171220 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
171222 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
171235 ** Register a new geometry function for use with the r-tree MATCH operator.
171243 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
171248 pGeomCtx->xGeom = xGeom;
171249 pGeomCtx->xQueryFunc = 0;
171250 pGeomCtx->xDestructor = 0;
171251 pGeomCtx->pContext = pContext;
171252 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
171258 ** Register a new 2nd-generation geometry function for use with the
171259 ** r-tree MATCH operator.
171268 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
171273 pGeomCtx->xGeom = 0;
171274 pGeomCtx->xQueryFunc = xQueryFunc;
171275 pGeomCtx->xDestructor = xDestructor;
171276 pGeomCtx->pContext = pContext;
171277 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
171314 ** ("International Components for Unicode", an open-source library
171327 ** provide case-independent matching.
171364 sqlite3_result_error(pCtx, zBuf, -1);
171386 ** a multi-byte UTF8 character. It is copied here from SQLite source
171403 c = icuUtf8Trans1[c-0xc0]; \
171417 ** Compare two UTF-8 strings for equality where the first string is
171423 const uint8_t *zString, /* The UTF-8 string to compare against */
171440 ** 1. uPattern is an unescaped match-all character "%",
171441 ** 2. uPattern is an unescaped match-one character "_",
171498 ** the build-in LIKE operator. The first argument to the function is the
171522 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
171528 /* The escape character string must consist of a single UTF-8 character.
171538 "ESCAPE expression must be a single character", -1);
171597 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
171609 uregex_setText(pExpr, zString, -1, &status);
171634 ** Implementations of scalar functions for case mapping - upper() and
171635 ** lower(). Function upper() converts its input to upper-case (ABC).
171636 ** Function lower() converts to lower-case (abc).
171645 ** upper('ABC') -> 'abc'
171646 ** lower('abc') -> 'ABC'
171654 ** lower('I', 'en_us') -> 'i'
171655 ** lower('I', 'tr_tr') -> '\u131' (small dotless i)
171657 ** http://www.icu-project.org/userguide/posix.html#case_mappings
171662 int nInput; /* Size of utf-16 input string in bytes */
171739 case UCOL_LESS: return -1;
171754 ** SELECT icu_load_collation(<locale>, <collation-name>);
171757 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
171767 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
171793 sqlite3_result_error(p, "Error registering collation function", -1);
171829 db, p->zName, p->nArg, p->enc,
171830 p->iContext ? (void*)db : (void*)0,
171831 p->xFunc, 0, 0
171893 UBreakIterator *pIter; /* ICU break-iterator object */
171895 UChar *aChar; /* Copy of input using utf-16 encoding */
171896 int *aOffset; /* Offsets of each character in utf-8 input */
171925 p->zLocale = (char *)&p[1];
171926 memcpy(p->zLocale, argv[0], n);
171945 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
171984 pCsr->aChar = (UChar *)&pCsr[1];
171985 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
171987 pCsr->aOffset[iOut] = iInput;
171992 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
171997 pCsr->aOffset[iOut] = iInput;
172006 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
172011 pCsr->nChar = iOut;
172013 ubrk_first(pCsr->pIter);
172023 ubrk_close(pCsr->pIter);
172024 sqlite3_free(pCsr->zBuffer);
172049 iStart = ubrk_current(pCsr->pIter);
172050 iEnd = ubrk_next(pCsr->pIter);
172057 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
172070 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
172074 pCsr->zBuffer = zNew;
172075 pCsr->nBuffer = nByte;
172079 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
172080 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
172083 } while( nByte>pCsr->nBuffer );
172085 *ppToken = pCsr->zBuffer;
172087 *piStartOffset = pCsr->aOffset[iStart];
172088 *piEndOffset = pCsr->aOffset[iEnd];
172089 *piPosition = pCsr->iToken++;
172146 ** to a *-oal file. A *-oal file is just like a *-wal file, except
172147 ** that it is named "<database>-oal" instead of "<database>-wal".
172149 ** "<database>-oal", they go on using the original database in
172150 ** rollback mode while the *-oal file is being generated.
172155 ** to update each b-tree individually. All updates required by each
172156 ** b-tree are completed before moving on to the next, and all
172159 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
172165 ** detect the new *-wal file and read the database in wal mode. At
172166 ** this point they see the new version of the database - including
172169 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
172172 ** handle is closed before the entire *-wal file is checkpointed,
172182 ** When state is saved, any commit to the *-oal file and the commit to
172192 ** by delete the RBU update database and *-oal file and have the device
172231 ** b-tree indexes that are collectively larger than the available cache
172234 ** The problem is that in order to update a b-tree, the leaf page (at least)
172244 ** by index key before applying them to the b-tree. This leads to an IO
172245 ** pattern that resembles a single linear scan through the index b-tree,
172252 ** large transaction to be broken down into sub-transactions performed
172257 ** has committed one or more sub-transactions, other database clients continue
172276 ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
172297 ** target table, and one more - "rbu_control". The data_% table should
172316 ** of zero or more numeric characters (0-9). This can be significant because
172326 ** key column - "rowid". Virtual tables for which the "rowid" column does
172337 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
172339 ** hidden columns are optional - they are updated by RBU if present in
172375 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
172390 ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
172391 ** the target table column. rbu_delta() is invoked with two arguments - the
172405 ** to be a "fossil delta" - a patch to be applied to a blob value in the
172406 ** format used by the fossil source-code management system. In this case
172443 ** b-tree operation, so thousands of calls may be required to apply
172504 ** Or, if zState is non-NULL, it must be a path to a database file in which
172518 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
172530 ** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
172544 ** name of the state database is "<database>-vacuum", where <database>
172546 ** state database is not already present in the file-system, it is created
172595 ** database handle, or non-zero to access the rbu update database handle.
172630 ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
172657 ** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
172668 ** Return the total number of key-value operations (inserts, deletes or
172685 ** The update is visible to non-RBU clients during stage 2. During stage 1
172686 ** non-RBU reader clients may see the original database.
172710 ** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
172722 ** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
172723 ** may either add further data to the *-oal file, or compute data that will
172727 ** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
172728 ** will move the *-oal file to the equivalent *-wal path. If the current
172735 ** sqlite3rbu_step() will copy a page of data from the *-wal file into
172755 ** Create an RBU VFS named zName that accesses the underlying file-system
172757 ** then the new RBU VFS uses the default system VFS to access the file-system.
172758 ** The new object is registered as a non-default VFS with SQLite before
172768 ** (for example multiplexor) to access the file-system. For example,
172777 ** // access the file-system via the system default VFS, bypassing the
172792 ** non-RBU users. There is no harm in an application accessing the
172793 ** file-system via "rbu" all the time, even if it only uses RBU functionality
172824 /* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
172845 ** 1: the *-rbu file is currently under construction.
172846 ** 2: the *-rbu file has been constructed, but not yet moved
172847 ** to the *-wal path.
172869 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
172870 ** header created by recovering the *-wal file. This is used to detect
172871 ** cases when another client appends frames to the *-wal file in the
172876 ** Valid if STAGE==1. The current change-counter cookie value in the
172880 ** Valid if STAGE==1. The size in bytes of the *-oal file.
172970 int *aiSrcOrder; /* src table col -> target table col */
172974 int eType; /* Table type - an RBU_PK_XXX value */
172993 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
173019 #define RBU_INSERT 1 /* Insert on a main table b-tree */
173020 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
173022 #define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
173023 #define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
173025 #define RBU_UPDATE 6 /* Update a row in a main table b-tree */
173028 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
173041 ** a running estimate of the number of b-tree operations required to
173042 ** finish populating the *-oal file. This allows the sqlite3_bp_progress()
173043 ** API to calculate the permyriadage progress of populating the *-oal file
173061 ** no b-tree operations are required on index b-trees. Or if the
173063 ** required (one delete and one insert on each index b-tree).
173067 ** b-trees.
173117 ** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
173147 u8 iWriteVer; /* "write-version" value for main db files */
173151 char **apShm; /* Array of mmap'd *-shm regions */
173163 #define rbuIsVacuum(p) ((p)->zTarget==0)
173173 ** are lifted from the fossil source code (http://fossil-scm.org). They
173185 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173186 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173187 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173188 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
173189 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
173190 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
173191 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
173192 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
173201 z--;
173202 *pLen -= z - zStart;
173209 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
173223 N -= 16;
173231 N -= 4;
173251 ** The delta string should be null-terminated. But the delta string
173259 ** then this routine returns -1.
173280 return -1;
173282 zDelta++; lenDelta--;
173288 zDelta++; lenDelta--;
173292 return -1;
173294 zDelta++; lenDelta--;
173298 return -1;
173302 return -1;
173309 zDelta++; lenDelta--;
173313 return -1;
173317 return -1;
173322 lenDelta -= cnt;
173326 zDelta++; lenDelta--;
173331 return -1;
173336 return -1;
173342 return -1;
173347 return -1;
173355 return -1;
173396 sqlite3_result_error(context, "corrupt fossil delta", -1);
173406 sqlite3_result_error(context, "corrupt fossil delta", -1);
173430 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
173493 for(i=0; i<pIter->nTblCol; i++){
173494 sqlite3_free(pIter->azTblCol[i]);
173495 sqlite3_free(pIter->azTblType[i]);
173497 sqlite3_free(pIter->azTblCol);
173498 pIter->azTblCol = 0;
173499 pIter->azTblType = 0;
173500 pIter->aiSrcOrder = 0;
173501 pIter->abTblPk = 0;
173502 pIter->abNotNull = 0;
173503 pIter->nTblCol = 0;
173504 pIter->eType = 0; /* Invalid value */
173514 sqlite3_finalize(pIter->pSelect);
173515 sqlite3_finalize(pIter->pInsert);
173516 sqlite3_finalize(pIter->pDelete);
173517 sqlite3_finalize(pIter->pTmpInsert);
173518 pUp = pIter->pRbuUpdate;
173520 RbuUpdateStmt *pTmp = pUp->pNext;
173521 sqlite3_finalize(pUp->pUpdate);
173526 pIter->pSelect = 0;
173527 pIter->pInsert = 0;
173528 pIter->pDelete = 0;
173529 pIter->pRbuUpdate = 0;
173530 pIter->pTmpInsert = 0;
173531 pIter->nCol = 0;
173540 sqlite3_finalize(pIter->pTblIter);
173541 sqlite3_finalize(pIter->pIdxIter);
173555 int rc = p->rc;
173560 if( pIter->zIdx==0 ){
173561 rc = sqlite3_exec(p->dbMain,
173566 , 0, 0, &p->zErrmsg
173571 if( pIter->bCleanup ){
173573 pIter->bCleanup = 0;
173574 rc = sqlite3_step(pIter->pTblIter);
173576 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
173577 pIter->zTbl = 0;
173579 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
173580 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
173581 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
173584 if( pIter->zIdx==0 ){
173585 sqlite3_stmt *pIdx = pIter->pIdxIter;
173586 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
173589 rc = sqlite3_step(pIter->pIdxIter);
173591 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
173592 pIter->bCleanup = 1;
173593 pIter->zIdx = 0;
173595 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
173596 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
173597 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
173598 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
173607 p->rc = rc;
173615 ** accepts one or two arguments. The first argument is the name of a table -
173619 ** For a non-vacuum RBU handle, if the table name matches the pattern:
173621 ** data[0-9]_<name>
173627 ** "data_t1" -> "t1"
173628 ** "data0123_t2" -> "t2"
173629 ** "dataAB_t3" -> NULL
173647 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
173654 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
173673 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
173683 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
173690 pIter->bCleanup = 1;
173691 p->rc = rc;
173699 ** If an error has already occurred (p->rc is already set to something other
173709 if( p->rc==SQLITE_OK ){
173710 if( zSql==0 ) p->rc = SQLITE_NOMEM;
173727 ** called, it is a no-op.
173734 if( p->rc==SQLITE_OK ){
173736 p->rc = SQLITE_NOMEM;
173738 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
173743 return p->rc;
173758 if( p->rc==SQLITE_OK ){
173762 p->rc = SQLITE_NOMEM;
173772 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
173782 pIter->azTblCol = azNew;
173783 pIter->azTblType = &azNew[nCol];
173784 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
173785 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
173786 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
173787 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
173792 ** The first argument must be a nul-terminated string. This function
173828 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
173829 p->rc = rc;
173830 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
173891 assert( p->rc==SQLITE_OK );
173892 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
173898 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
173908 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
173911 if( p->rc ) goto rbuTableType_end;
173916 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
173920 if( p->rc==SQLITE_OK ){
173932 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
173935 if( p->rc==SQLITE_OK ){
173955 ** the pIter->abIndexed[] array.
173961 if( p->rc==SQLITE_OK ){
173962 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
173963 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
173964 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
173968 pIter->nIndex = 0;
173969 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
173973 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
173976 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
173978 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
173982 pIter->nIndex++;
173985 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
173986 /* "PRAGMA index_list" includes the main PK b-tree */
173987 pIter->nIndex--;
173991 if( bIndex==0 ) pIter->abIndexed = 0;
173996 ** If they are not already populated, populate the pIter->azTblCol[],
173997 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
174005 if( pIter->azTblCol==0 ){
174014 assert( pIter->eType==0 );
174015 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
174016 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
174017 p->rc = SQLITE_ERROR;
174018 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
174020 if( p->rc ) return p->rc;
174021 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
174023 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
174024 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
174025 || pIter->eType==RBU_PK_VTAB
174031 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
174032 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
174034 if( p->rc==SQLITE_OK ){
174038 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
174041 char *zCopy = rbuStrndup(zName, &p->rc);
174042 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
174043 pIter->azTblCol[pIter->nTblCol++] = zCopy;
174052 if( p->rc==SQLITE_OK
174054 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
174056 p->rc = SQLITE_ERROR;
174057 p->zErrmsg = sqlite3_mprintf(
174058 "table %q %s rbu_rowid column", pIter->zDataTbl,
174063 /* Check that all non-HIDDEN columns in the destination table are also
174066 if( p->rc==SQLITE_OK ){
174067 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
174068 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
174071 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
174073 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
174074 for(i=iOrder; i<pIter->nTblCol; i++){
174075 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
174077 if( i==pIter->nTblCol ){
174078 p->rc = SQLITE_ERROR;
174079 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
174080 pIter->zDataTbl, zName
174088 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
174089 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
174092 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
174093 pIter->abTblPk[iOrder] = (iPk!=0);
174094 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
174101 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
174102 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
174105 return p->rc;
174109 ** This function constructs and returns a pointer to a nul-terminated
174111 ** column names currently stored in the pIter->azTblCol[] array.
174120 for(i=0; i<pIter->nTblCol; i++){
174121 const char *z = pIter->azTblCol[i];
174160 int rc = p->rc; /* Error code */
174172 assert( p->zErrmsg==0 );
174173 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
174174 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
174188 if( pIter->eType==RBU_PK_IPK ){
174190 for(i=0; pIter->abTblPk[i]==0; i++);
174191 assert( i<pIter->nTblCol );
174192 zCol = pIter->azTblCol[i];
174200 zCol = pIter->azTblCol[iCid];
174201 zType = pIter->azTblType[iCid];
174205 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
174235 p->rc = rc;
174253 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
174262 if( p->rc==SQLITE_OK && pIter->abIndexed ){
174265 for(i=0; i<pIter->nTblCol; i++){
174266 if( pIter->abIndexed[i] ){
174267 const char *zCol = pIter->azTblCol[i];
174274 p->rc = SQLITE_NOMEM;
174280 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
174302 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
174303 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
174304 }else if( pIter->eType==RBU_PK_EXTERNAL ){
174307 for(i=0; i<pIter->nTblCol; i++){
174308 if( pIter->abTblPk[i] ){
174320 for(i=0; i<pIter->nTblCol; i++){
174321 if( pIter->abTblPk[i] ){
174322 const char *zCol = pIter->azTblCol[i];
174333 ** (p->objiter.pSelect) currently points to a valid row. However, there
174335 ** stored in the (p->nCol+1)'th column. Set the error code and error message
174339 p->rc = SQLITE_ERROR;
174340 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
174345 ** Return a nul-terminated string containing the comma separated list of
174367 if( p->rc==SQLITE_OK ){
174370 if( (int)strlen(zMask)!=pIter->nTblCol ){
174374 for(i=0; i<pIter->nTblCol; i++){
174375 char c = zMask[pIter->aiSrcOrder[i]];
174378 zList, zSep, pIter->azTblCol[i], i+1
174384 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
174390 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
174401 ** Return a nul-terminated string consisting of nByte comma separated
174444 assert( pIter->zIdx==0 );
174445 if( p->rc==SQLITE_OK ){
174447 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
174448 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
174450 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
174451 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
174453 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
174458 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
174467 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
174484 ** a table b-tree where the table has an external primary key. If the
174487 ** no-op.
174502 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
174503 int tnum = pIter->iPkTnum; /* Root page of PK index */
174513 ** zIdx to point to a nul-terminated string containing this name. */
174514 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
174517 if( p->rc==SQLITE_OK ){
174524 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
174530 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
174537 iCid, pIter->azTblType[iCid], zCollate
174546 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
174547 rbuMPrintfExec(p, p->dbMain,
174551 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
174565 ** table b-tree of the table before returning. Non-zero is returned if
174576 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
174577 int tnum = pIter->iTnum;
174581 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
174583 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
174585 const char *zCol = pIter->azTblCol[iCol];
174588 p->rc = sqlite3_table_column_metadata(
174589 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
174592 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
174598 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
174599 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
174604 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
174611 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
174612 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
174613 pIter->zTbl, zSql,
174614 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
174616 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
174637 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
174638 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
174640 assert( pIter->pTmpInsert==0 );
174641 p->rc = prepareFreeAndCollectError(
174642 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
174644 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
174659 || p->objiter.eType==RBU_PK_EXTERNAL
174660 || p->objiter.eType==RBU_PK_NONE
174663 p->nPhaseOneStep += p->objiter.nIndex;
174667 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
174670 sqlite3_step(p->objiter.pTmpInsert);
174671 rc = sqlite3_reset(p->objiter.pTmpInsert);
174687 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
174689 assert( pIter->bCleanup==0 );
174690 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
174691 const int tnum = pIter->iTnum;
174693 char **pz = &p->zErrmsg;
174694 const char *zIdx = pIter->zIdx;
174698 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
174699 if( !zLimit ) p->rc = SQLITE_NOMEM;
174703 const char *zTbl = pIter->zTbl;
174710 assert( pIter->eType!=RBU_PK_VTAB );
174717 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
174718 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
174719 rbuMPrintfExec(p, p->dbMain,
174723 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
174726 pIter->nCol = nBind;
174727 if( p->rc==SQLITE_OK ){
174728 p->rc = prepareFreeAndCollectError(
174729 p->dbMain, &pIter->pInsert, &p->zErrmsg,
174735 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
174736 p->rc = prepareFreeAndCollectError(
174737 p->dbMain, &pIter->pDelete, &p->zErrmsg,
174743 if( p->rc==SQLITE_OK ){
174749 pIter->zDataTbl,
174754 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
174757 zCollist, p->zStateDb, pIter->zDataTbl,
174767 zCollist, p->zStateDb, pIter->zDataTbl,
174768 zCollist, pIter->zDataTbl,
174772 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
174780 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
174781 ||(pIter->eType==RBU_PK_NONE)
174782 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
174783 const char *zTbl = pIter->zTbl; /* Table this step applies to */
174786 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
174792 pIter->nCol = pIter->nTblCol;
174797 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
174799 /* Create the INSERT statement to write to the target PK b-tree */
174800 if( p->rc==SQLITE_OK ){
174801 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
174809 /* Create the DELETE statement to write to the target PK b-tree.
174812 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
174813 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
174820 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
174822 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
174827 rbuMPrintfExec(p, p->dbRbu,
174830 , p->zStateDb, pIter->zDataTbl
174831 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
174832 , pIter->zDataTbl
174835 rbuMPrintfExec(p, p->dbMain,
174855 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
174856 rbuMPrintfExec(p, p->dbMain,
174869 if( p->rc==SQLITE_OK ){
174874 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
174880 pIter->zDataTbl, zLimit
174894 return p->rc;
174899 ** be used to update the imposter table for the main table b-tree of the
174923 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
174925 if( strcmp(pUp->zMask, zMask)==0 ){
174926 *pp = pUp->pNext;
174927 pUp->pNext = pIter->pRbuUpdate;
174928 pIter->pRbuUpdate = pUp;
174929 *ppStmt = pUp->pUpdate;
174934 assert( pUp==0 || pUp->pNext==0 );
174937 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
174939 sqlite3_finalize(pUp->pUpdate);
174940 pUp->pUpdate = 0;
174942 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
174950 pUp->zMask = (char*)&pUp[1];
174951 memcpy(pUp->zMask, zMask, pIter->nTblCol);
174952 pUp->pNext = pIter->pRbuUpdate;
174953 pIter->pRbuUpdate = pUp;
174958 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
174960 zPrefix, pIter->zTbl, zSet, zWhere
174962 p->rc = prepareFreeAndCollectError(
174963 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
174965 *ppStmt = pUp->pUpdate;
174971 return p->rc;
174980 if( p->rc==SQLITE_OK ){
174982 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
174983 if( p->rc ){
174984 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
174997 sqlite3_free(p->zTbl);
174998 sqlite3_free(p->zIdx);
175021 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
175022 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
175027 pRet->eStage = sqlite3_column_int(pStmt, 1);
175028 if( pRet->eStage!=RBU_STAGE_OAL
175029 && pRet->eStage!=RBU_STAGE_MOVE
175030 && pRet->eStage!=RBU_STAGE_CKPT
175032 p->rc = SQLITE_CORRUPT;
175037 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
175041 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
175045 pRet->nRow = sqlite3_column_int(pStmt, 1);
175049 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
175053 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
175057 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
175061 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
175065 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
175076 p->rc = rc;
175086 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
175087 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
175090 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
175092 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
175093 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
175094 if( p->zState==0 ){
175095 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
175096 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
175102 if( p->zState ){
175103 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
175104 memcpy(p->zStateDb, "stat", 4);
175106 memcpy(p->zStateDb, "main", 4);
175110 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
175111 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
175116 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
175120 if( p->rc==SQLITE_OK ){
175124 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
175127 if( p->rc==SQLITE_OK
175134 if( p->rc==SQLITE_OK ) p->rc = rc2;
175136 if( p->rc==SQLITE_OK && bOk==0 ){
175137 p->rc = SQLITE_ERROR;
175138 p->zErrmsg = sqlite3_mprintf("invalid state database");
175141 if( p->rc==SQLITE_OK ){
175142 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
175148 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
175151 p->nRbu = 0;
175152 p->pRbuFd = 0;
175153 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
175154 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
175155 if( p->eStage>=RBU_STAGE_MOVE ){
175160 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
175164 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
175167 p->eStage = 0;
175168 if( p->rc==SQLITE_OK && p->dbMain==0 ){
175170 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
175171 }else if( p->pRbuFd->pWalFd ){
175173 p->pRbuFd->bNolock = 0;
175174 sqlite3_close(p->dbRbu);
175175 sqlite3_close(p->dbMain);
175176 p->dbMain = 0;
175177 p->dbRbu = 0;
175181 p->rc = SQLITE_ERROR;
175182 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
175186 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
175187 zExtra = &p->zRbu[5];
175194 zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
175195 sqlite3_db_filename(p->dbRbu, "main"),
175200 p->rc = SQLITE_NOMEM;
175203 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
175208 if( p->rc==SQLITE_OK ){
175209 p->rc = sqlite3_create_function(p->dbMain,
175210 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
175214 if( p->rc==SQLITE_OK ){
175215 p->rc = sqlite3_create_function(p->dbMain,
175220 if( p->rc==SQLITE_OK ){
175221 p->rc = sqlite3_create_function(p->dbRbu,
175222 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
175226 if( p->rc==SQLITE_OK ){
175227 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
175229 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
175234 if( p->rc==SQLITE_OK ){
175235 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
175238 if( p->rc==SQLITE_NOTFOUND ){
175239 p->rc = SQLITE_ERROR;
175240 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
175246 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
175248 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
175254 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
175259 ** test.db-journal => test.nal
175260 ** test.db-wal => test.wal
175261 ** test.db-shm => test.shm
175262 ** test.db-mj7f3319fa => test.9fa
175272 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
175273 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
175279 ** Return the current wal-index header checksum for the target database
175280 ** as a 64-bit integer.
175282 ** The checksum is store in the first page of xShmMap memory as an 8-byte
175287 if( p->rc==SQLITE_OK ){
175288 sqlite3_file *pDb = p->pTargetFd->pReal;
175290 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
175291 if( p->rc==SQLITE_OK ){
175303 ** (wal frame -> db page) copy operations required to checkpoint the
175305 ** perform the copy operations directly on the file-system.
175308 ** being resumed. In this case, if the checksum of the wal-index-header
175317 ** recovered. Running a read-statement here to ensure that doing so
175320 p->eStage = 0;
175321 if( p->rc==SQLITE_OK ){
175322 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
175334 ** * Attempts to read from the *-wal file or write to the database file
175341 ** no-ops. These locks will not be released until the connection
175349 ** array populated with a set of (frame -> page) mappings. Because the
175354 if( p->rc==SQLITE_OK ){
175356 p->eStage = RBU_STAGE_CAPTURE;
175357 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
175358 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
175361 if( p->rc==SQLITE_OK && p->nFrame>0 ){
175362 p->eStage = RBU_STAGE_CKPT;
175363 p->nStep = (pState ? pState->nRow : 0);
175364 p->aBuf = rbuMalloc(p, p->pgsz);
175365 p->iWalCksum = rbuShmChecksum(p);
175368 if( p->rc==SQLITE_OK ){
175369 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
175370 p->rc = SQLITE_DONE;
175371 p->eStage = RBU_STAGE_DONE;
175374 sqlite3_file *pDb = p->pTargetFd->pReal;
175375 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
175376 assert( p->nPagePerSector==0 );
175377 nSectorSize = pDb->pMethods->xSectorSize(pDb);
175378 if( nSectorSize>p->pgsz ){
175379 p->nPagePerSector = nSectorSize / p->pgsz;
175381 p->nPagePerSector = 1;
175388 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
175402 if( pRbu->mLock!=mReq ){
175403 pRbu->rc = SQLITE_BUSY;
175407 pRbu->pgsz = iAmt;
175408 if( pRbu->nFrame==pRbu->nFrameAlloc ){
175409 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
175411 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
175413 pRbu->aFrame = aNew;
175414 pRbu->nFrameAlloc = nNew;
175417 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
175418 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
175419 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
175420 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
175421 pRbu->nFrame++;
175431 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
175441 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
175442 sqlite3_file *pDb = p->pTargetFd->pReal;
175445 assert( p->rc==SQLITE_OK );
175446 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
175447 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
175448 if( p->rc ) return;
175450 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
175451 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
175459 sqlite3_file *pReal = p->pTargetFd->pReal;
175460 assert( p->rc==SQLITE_OK );
175461 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
175462 if( p->rc==SQLITE_OK ){
175463 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
175472 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
175481 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
175493 ** on the database file. This proc moves the *-oal file to the *-wal path,
175494 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
175499 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
175505 zMove = sqlite3_db_filename(p->dbRbu, "main");
175507 zOal = sqlite3_mprintf("%s-oal", zMove);
175508 zWal = sqlite3_mprintf("%s-wal", zMove);
175510 assert( p->eStage==RBU_STAGE_MOVE );
175511 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
175513 p->rc = SQLITE_NOMEM;
175515 /* Move the *-oal file to *-wal. At this point connection p->db is
175520 ** lock is obtained here before the *-oal is moved to *-wal.
175523 if( p->rc==SQLITE_OK ){
175527 /* Re-open the databases. */
175528 rbuObjIterFinalize(&p->objiter);
175529 sqlite3_close(p->dbRbu);
175530 sqlite3_close(p->dbMain);
175531 p->dbMain = 0;
175532 p->dbRbu = 0;
175544 p->rc = SQLITE_OK;
175546 p->rc = SQLITE_IOERR;
175550 p->rc = SQLITE_IOERR_NOMEM;
175554 p->rc = SQLITE_IOERR_NOMEM;
175558 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
175561 if( p->rc==SQLITE_OK ){
175574 ** (p->objiter.pSelect) currently points to a valid row. This function
175590 int iCol = p->objiter.nCol; /* Index of rbu_control column */
175593 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
175595 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
175607 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
175609 p->rc = SQLITE_NOMEM;
175646 RbuObjIter *pIter = &p->objiter;
175651 assert( p->rc==SQLITE_OK );
175652 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
175662 p->nPhaseOneStep -= p->objiter.nIndex;
175666 pWriter = pIter->pDelete;
175668 pWriter = pIter->pInsert;
175671 for(i=0; i<pIter->nCol; i++){
175672 /* If this is an INSERT into a table b-tree and the table has an
175676 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
175677 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
175679 p->rc = SQLITE_MISMATCH;
175680 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
175684 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
175688 pVal = sqlite3_column_value(pIter->pSelect, i);
175689 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
175690 if( p->rc ) return;
175692 if( pIter->zIdx==0 ){
175693 if( pIter->eType==RBU_PK_VTAB
175694 || pIter->eType==RBU_PK_NONE
175695 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
175702 ** Hence column_value(pIter->nCol+1).
175704 assertColumnName(pIter->pSelect, pIter->nCol+1,
175707 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
175708 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
175711 if( p->rc==SQLITE_OK ){
175713 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
175720 ** The object-iterator (p->objiter) currently points to a valid object,
175721 ** and the input cursor (p->objiter.pSelect) currently points to a valid
175729 RbuObjIter *pIter = &p->objiter;
175738 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
175740 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
175744 if( pIter->zIdx==0 ){
175745 p->nPhaseOneStep += p->objiter.nIndex;
175748 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
175757 p->nPhaseOneStep -= p->objiter.nIndex;
175761 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
175762 char c = zMask[pIter->aiSrcOrder[i]];
175763 pVal = sqlite3_column_value(pIter->pSelect, i);
175764 if( pIter->abTblPk[i] || c!='.' ){
175765 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
175768 if( p->rc==SQLITE_OK
175769 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
175772 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
175773 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
175774 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
175776 if( p->rc==SQLITE_OK ){
175778 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
175783 return p->rc;
175787 ** Increment the schema cookie of the main database opened by p->dbMain.
175790 ** opened by p->dbMain to one more than the schema cookie of the main
175791 ** db opened by p->dbRbu.
175794 if( p->rc==SQLITE_OK ){
175795 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
175799 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
175802 if( p->rc==SQLITE_OK ){
175813 if( p->rc==SQLITE_OK ){
175814 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
175825 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
175827 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
175830 assert( p->zErrmsg==0 );
175831 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
175843 p->zStateDb,
175845 RBU_STATE_TBL, p->objiter.zTbl,
175846 RBU_STATE_IDX, p->objiter.zIdx,
175847 RBU_STATE_ROW, p->nStep,
175848 RBU_STATE_PROGRESS, p->nProgress,
175849 RBU_STATE_CKPT, p->iWalCksum,
175850 RBU_STATE_COOKIE, (i64)pFd->iCookie,
175851 RBU_STATE_OALSZ, p->iOalSz,
175852 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep
175861 if( rc!=SQLITE_OK ) p->rc = rc;
175868 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
175884 if( p->rc==SQLITE_OK ){
175886 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
175889 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
175890 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
175908 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
175909 if( p->rc==SQLITE_OK ){
175910 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
175917 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
175919 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
175922 if( p->rc!=SQLITE_OK ) return;
175924 if( p->rc==SQLITE_OK ){
175925 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
175930 if( p->rc==SQLITE_OK ){
175931 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
175936 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
175942 p->rc = sqlite3_reset(pInsert);
175944 if( p->rc==SQLITE_OK ){
175945 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
175957 switch( p->eStage ){
175959 RbuObjIter *pIter = &p->objiter;
175963 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
175969 while( p->rc==SQLITE_OK && pIter->zTbl ){
175971 if( pIter->bCleanup ){
175975 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
175976 rbuMPrintfExec(p, p->dbRbu,
175977 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
175984 if( p->rc==SQLITE_OK ){
175985 int rc = sqlite3_step(pIter->pSelect);
175987 p->nProgress++;
175988 p->nStep++;
175991 p->rc = sqlite3_reset(pIter->pSelect);
175992 p->nStep = 0;
175999 if( p->rc==SQLITE_OK ){
176000 assert( pIter->zTbl==0 );
176003 if( p->rc==SQLITE_OK ){
176004 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
176006 if( p->rc==SQLITE_OK ){
176007 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
176009 p->eStage = RBU_STAGE_MOVE;
176015 if( p->rc==SQLITE_OK ){
176017 p->nProgress++;
176023 if( p->rc==SQLITE_OK ){
176024 if( p->nStep>=p->nFrame ){
176025 sqlite3_file *pDb = p->pTargetFd->pReal;
176028 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
176031 if( p->rc==SQLITE_OK ){
176033 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
176034 if( p->rc==SQLITE_OK ){
176035 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
176039 if( p->rc==SQLITE_OK ){
176040 p->eStage = RBU_STAGE_DONE;
176041 p->rc = SQLITE_DONE;
176048 ** However, if the sector-size is larger than the page-size, and the
176056 RbuFrame *pFrame = &p->aFrame[p->nStep];
176057 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
176059 p->nStep++;
176060 }while( p->nStep<p->nFrame
176061 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
176062 && p->rc==SQLITE_OK
176065 p->nProgress++;
176073 return p->rc;
176080 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
176093 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
176101 assert( p->rc==SQLITE_OK );
176102 if( pState->zTbl ){
176103 RbuObjIter *pIter = &p->objiter;
176106 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
176107 || rbuStrCompare(pIter->zIdx, pState->zIdx)
176108 || rbuStrCompare(pIter->zTbl, pState->zTbl)
176113 if( rc==SQLITE_OK && !pIter->zTbl ){
176115 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
176119 p->nStep = pState->nRow;
176120 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
176123 p->rc = rc;
176128 ** If there is a "*-oal" file in the file-system corresponding to the
176129 ** target database in the file-system, delete it. If an error occurs,
176133 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
176136 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
176137 pVfs->xDelete(pVfs, zOal, 0);
176152 assert( p->rc==SQLITE_OK );
176155 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
176156 if( p->rc==SQLITE_OK ){
176159 p->zVfsName = pVfs->zName;
176160 ((rbu_vfs*)pVfs)->pRbu = p;
176169 if( p->zVfsName ){
176170 sqlite3rbu_destroy_vfs(p->zVfsName);
176171 p->zVfsName = 0;
176176 ** This user-defined SQL function is invoked with a single argument - the
176192 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
176197 sqlite3_result_error(pCtx, zErrmsg, -1);
176207 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
176230 if( p->rc==SQLITE_OK ){
176234 p->nPhaseOneStep = -1;
176236 p->rc = sqlite3_create_function(p->dbRbu,
176241 ** occurs, nPhaseOneStep will be left set to -1. */
176242 if( p->rc==SQLITE_OK ){
176243 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
176247 if( p->rc==SQLITE_OK ){
176251 p->rc = sqlite3_finalize(pStmt);
176254 if( p->rc==SQLITE_OK && bExists ){
176255 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
176259 if( p->rc==SQLITE_OK ){
176261 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
176263 p->rc = sqlite3_finalize(pStmt);
176289 if( p->rc==SQLITE_OK ){
176293 p->zTarget = pCsr;
176294 memcpy(p->zTarget, zTarget, nTarget+1);
176297 p->zRbu = pCsr;
176298 memcpy(p->zRbu, zRbu, nRbu+1);
176301 p->zState = rbuMPrintf(p, "%s", zState);
176306 ** to be a wal-mode db. But, this may have happened due to an earlier
176317 if( p->rc==SQLITE_OK ){
176319 assert( pState || p->rc!=SQLITE_OK );
176320 if( p->rc==SQLITE_OK ){
176322 if( pState->eStage==0 ){
176325 p->eStage = RBU_STAGE_OAL;
176327 p->eStage = pState->eStage;
176328 p->nPhaseOneStep = pState->nPhaseOneStep;
176330 p->nProgress = pState->nProgress;
176331 p->iOalSz = pState->iOalSz;
176334 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
176336 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
176337 if( p->eStage==RBU_STAGE_OAL ){
176338 p->rc = SQLITE_ERROR;
176339 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
176340 }else if( p->eStage==RBU_STAGE_MOVE ){
176341 p->eStage = RBU_STAGE_CKPT;
176342 p->nStep = 0;
176346 if( p->rc==SQLITE_OK
176347 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
176348 && pState->eStage!=0
176350 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
176351 if( pFd->iCookie!=pState->iCookie ){
176352 /* At this point (pTargetFd->iCookie) contains the value of the
176353 ** change-counter cookie (the thing that gets incremented when a
176356 p->rc = SQLITE_BUSY;
176357 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
176363 if( p->rc==SQLITE_OK ){
176364 if( p->eStage==RBU_STAGE_OAL ){
176365 sqlite3 *db = p->dbMain;
176366 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
176369 if( p->rc==SQLITE_OK ){
176370 p->rc = rbuObjIterFirst(p, &p->objiter);
176375 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
176376 p->rc = SQLITE_DONE;
176377 p->eStage = RBU_STAGE_DONE;
176379 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
176384 /* Open transactions both databases. The *-oal file is opened or
176386 if( p->rc==SQLITE_OK ){
176387 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
176393 if( p->rc==SQLITE_OK ){
176396 p->rc = sqlite3_exec(
176397 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
176401 if( p->rc==SQLITE_OK ){
176405 }else if( p->eStage==RBU_STAGE_MOVE ){
176406 /* no-op */
176407 }else if( p->eStage==RBU_STAGE_CKPT ){
176409 }else if( p->eStage==RBU_STAGE_DONE ){
176410 p->rc = SQLITE_DONE;
176412 p->rc = SQLITE_CORRUPT;
176431 pRet->rc = SQLITE_MISUSE;
176445 /* TODO: Check that zTarget and zRbu are non-NULL */
176457 /* TODO: Check that both arguments are non-NULL */
176467 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
176476 ** the pattern "rbu_imp_[0-9]*".
176479 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
176481 size_t nErrmsg = strlen(p->zErrmsg);
176482 for(i=0; i<(nErrmsg-8); i++){
176483 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
176485 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
176486 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
176487 nErrmsg -= nDel;
176500 /* Commit the transaction to the *-oal file. */
176501 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
176502 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
176506 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
176507 sqlite3_file *pDb = p->pTargetFd->pReal;
176508 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
176511 rbuSaveState(p, p->eStage);
176513 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
176514 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
176518 rbuObjIterFinalize(&p->objiter);
176525 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
176526 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
176527 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
176531 sqlite3_close(p->dbRbu);
176532 sqlite3_close(p->dbMain);
176533 assert( p->szTemp==0 );
176535 sqlite3_free(p->aBuf);
176536 sqlite3_free(p->aFrame);
176539 rc = p->rc;
176541 *pzErrmsg = p->zErrmsg;
176543 sqlite3_free(p->zErrmsg);
176545 sqlite3_free(p->zState);
176555 ** Return the total number of key-value operations (inserts, deletes or
176560 return pRbu->nProgress;
176569 switch( p->eStage ){
176571 if( p->nPhaseOneStep>0 ){
176572 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
176574 *pnOne = -1;
176586 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
176617 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
176620 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
176621 assert( p->eStage==RBU_STAGE_OAL
176622 || p->eStage==RBU_STAGE_MOVE
176623 || p->eStage==RBU_STAGE_CKPT
176624 || p->eStage==RBU_STAGE_DONE
176626 return aRes[p->eStage];
176631 int rc = p->rc;
176634 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
176635 if( p->eStage==RBU_STAGE_OAL ){
176637 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
176641 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
176642 sqlite3_file *pDb = p->pTargetFd->pReal;
176643 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
176646 p->rc = rc;
176647 rbuSaveState(p, p->eStage);
176648 rc = p->rc;
176650 if( p->eStage==RBU_STAGE_OAL ){
176652 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
176653 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
176654 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
176657 p->rc = rc;
176666 ** written, the value of the change-counter cookie is stored in
176667 ** rbu_file.iCookie. Similarly, the value of the "write-version"
176677 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
176681 ** 3a. If xAccess() is called to check if there exists a *-wal file
176683 ** stage (preparing the *-oal file), the following special handling
176686 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
176689 ** * if the *-wal file does not exist, set the output parameter to
176690 ** non-zero (to tell SQLite that it does exist) anyway.
176692 ** Then, when xOpen() is called to open the *-wal file associated with
176693 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
176694 ** file, the rbu vfs opens the corresponding *-oal file instead.
176696 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
176698 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
176699 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
176706 ** from automatically checkpointing a *-wal (or *-oal) file from within
176715 ** locks are no-ops (so that once obtained, these locks are never
176721 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
176722 if( p->pRbu ){
176723 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
176726 if( (1<<i) & p->pRbu->mLock ){
176727 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
176730 p->pRbu->mLock = 0;
176737 sqlite3rbu *pRbu = pFd->pRbu;
176738 i64 nDiff = nNew - pFd->sz;
176739 pRbu->szTemp += nDiff;
176740 pFd->sz = nNew;
176741 assert( pRbu->szTemp>=0 );
176742 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
176747 ** Add an item to the main-db lists, if it is not already present.
176749 ** There are two main-db lists. One for all file descriptors, and one
176755 rbu_vfs *pRbuVfs = p->pRbuVfs;
176757 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
176758 sqlite3_mutex_enter(pRbuVfs->mutex);
176759 if( p->pRbu==0 ){
176760 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
176761 p->pMainNext = pRbuVfs->pMain;
176762 pRbuVfs->pMain = p;
176764 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
176766 p->pMainRbuNext = pRbuVfs->pMainRbu;
176767 pRbuVfs->pMainRbu = p;
176770 sqlite3_mutex_leave(pRbuVfs->mutex);
176774 ** Remove an item from the main-db lists.
176778 sqlite3_mutex_enter(p->pRbuVfs->mutex);
176779 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
176780 if( *pp ) *pp = p->pMainNext;
176781 p->pMainNext = 0;
176782 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
176783 if( *pp ) *pp = p->pMainRbuNext;
176784 p->pMainRbuNext = 0;
176785 sqlite3_mutex_leave(p->pRbuVfs->mutex);
176790 ** either the xOpen() or xAccess() VFS method, search the main-db list for
176791 ** a file-handle opened by the same database connection on the corresponding
176794 ** If parameter bRbu is true, only search for file-descriptors with
176799 sqlite3_mutex_enter(pRbuVfs->mutex);
176801 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
176803 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
176805 sqlite3_mutex_leave(pRbuVfs->mutex);
176818 for(i=0; i<p->nShm; i++){
176819 sqlite3_free(p->apShm[i]);
176821 sqlite3_free(p->apShm);
176822 p->apShm = 0;
176823 sqlite3_free(p->zDel);
176825 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
176828 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
176830 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
176833 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
176836 rc = p->pReal->pMethods->xClose(p->pReal);
176842 ** Read and return an unsigned 32-bit big-endian integer from the buffer
176853 ** Write an unsigned 32-bit value in big-endian format to the supplied
176869 ** Read data from an rbuVfs-file.
176878 sqlite3rbu *pRbu = p->pRbu;
176881 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
176882 assert( p->openFlags & SQLITE_OPEN_WAL );
176883 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
176885 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
176886 && (p->openFlags & SQLITE_OPEN_WAL)
176887 && iOfst>=pRbu->iOalSz
176892 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
176897 ** SQLite will not check for a *-wal file. */
176900 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
176901 && pRbu->rc==SQLITE_OK
176903 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
176904 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
176912 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
176915 memset(&aBuf[100], 0, iAmt-100);
176923 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
176927 p->iCookie = rbuGetU32(&pBuf[24]);
176928 p->iWriteVer = pBuf[19];
176935 ** Write data to an rbuVfs-file.
176944 sqlite3rbu *pRbu = p->pRbu;
176947 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
176948 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
176949 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
176952 if( pRbu->eStage==RBU_STAGE_OAL
176953 && (p->openFlags & SQLITE_OPEN_WAL)
176954 && iOfst>=pRbu->iOalSz
176956 pRbu->iOalSz = iAmt + iOfst;
176957 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
176959 if( szNew>p->sz ){
176965 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
176966 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
176970 p->iCookie = rbuGetU32(&pBuf[24]);
176971 p->iWriteVer = pBuf[19];
176978 ** Truncate an rbuVfs-file.
176982 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
176986 return p->pReal->pMethods->xTruncate(p->pReal, size);
176990 ** Sync an rbuVfs-file.
176994 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
176995 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
177000 return p->pReal->pMethods->xSync(p->pReal, flags);
177004 ** Return the current file-size of an rbuVfs-file.
177009 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
177013 ** check for the existance of a *-wal file. rbuVfsRead() contains
177016 && p->pRbu && rbuIsVacuum(p->pRbu)
177017 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
177025 ** Lock an rbuVfs-file.
177029 sqlite3rbu *pRbu = p->pRbu;
177032 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
177034 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
177040 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
177047 ** Unlock an rbuVfs-file.
177051 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
177055 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
177059 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
177063 ** File control method. For custom operations on an rbuVfs-file.
177067 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
177070 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
177071 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
177077 ** one is found, this vfs will operate in pass-through mode. The lower
177079 rc = xControl(p->pReal, op, pArg);
177085 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
177088 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
177090 pRbu->pTargetFd = p;
177091 p->pRbu = pRbu;
177092 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
177095 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
177103 pRbu->nRbu++;
177104 pRbu->pRbuFd = p;
177105 p->bNolock = 1;
177108 rc = xControl(p->pReal, op, pArg);
177110 rbu_vfs *pRbuVfs = p->pRbuVfs;
177112 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
177121 ** Return the sector-size in bytes for an rbuVfs-file.
177125 return p->pReal->pMethods->xSectorSize(p->pReal);
177129 ** Return the device characteristic flags supported by an rbuVfs-file.
177133 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
177137 ** Take or release a shared-memory lock.
177141 sqlite3rbu *pRbu = p->pRbu;
177148 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
177149 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
177158 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
177165 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
177167 pRbu->mLock |= (1 << ofst);
177176 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
177187 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
177190 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
177192 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
177194 if( iRegion<=p->nShm ){
177196 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
177200 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
177201 p->apShm = apNew;
177202 p->nShm = iRegion+1;
177206 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
177212 p->apShm[iRegion] = pNew;
177217 *pp = p->apShm[iRegion];
177222 assert( p->apShm==0 );
177223 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
177234 p->pReal->pMethods->xShmBarrier(p->pReal);
177243 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
177245 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
177247 /* no-op */
177251 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
177259 ** the name of the *-wal file this db connection will use. SQLite
177261 ** or xOpen() to operate on the *-wal file.
177270 odd = 1 - odd;
177314 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
177321 pFd->pReal = (sqlite3_file*)&pFd[1];
177322 pFd->pRbuVfs = pRbuVfs;
177323 pFd->openFlags = flags;
177327 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
177328 ** the name of the *-wal file this db connection will use. SQLite
177330 ** or xOpen() to operate on the *-wal file. */
177331 pFd->zWal = rbuMainToWal(zName, flags);
177336 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
177337 /* This call is to open a *-wal file. Intead, open the *-oal. This
177344 if( rbuIsVacuum(pDb->pRbu) ){
177345 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
177352 zCopy[nCopy-3] = 'o';
177355 zOpen = (const char*)(pFd->zDel = zCopy);
177359 pFd->pRbu = pDb->pRbu;
177361 pDb->pWalFd = pFd;
177365 pFd->pRbu = pRbuVfs->pRbu;
177378 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
177380 if( pFd->pReal->pMethods ){
177384 pFile->pMethods = &rbuvfs_io_methods;
177389 sqlite3_free(pFd->zDel);
177399 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177400 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
177414 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
177417 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
177419 /* If this call is to check if a *-wal file associated with an RBU target
177423 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
177428 ** b) if the *-wal file does not exist, claim that it does anyway,
177430 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
177435 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
177440 rc = rbuVfsFileSize(&pDb->base, &sz);
177460 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177461 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
177469 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177470 return pRealVfs->xDlOpen(pRealVfs, zPath);
177475 ** utf-8 string describing the most recent error encountered associated
177479 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177480 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
177491 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177492 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
177499 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177500 pRealVfs->xDlClose(pRealVfs, pHandle);
177509 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177510 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
177518 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177519 return pRealVfs->xSleep(pRealVfs, nMicro);
177526 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
177527 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
177531 ** No-op.
177543 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
177544 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
177551 ** Create an RBU VFS named zName that accesses the underlying file-system
177552 ** via existing VFS zParent. The new object is registered as a non-default
177605 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
177606 pNew->base.mxPathname = pParent->mxPathname;
177607 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
177608 pNew->pRealVfs = pParent;
177609 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
177613 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
177614 if( pNew->mutex==0 ){
177617 rc = sqlite3_vfs_register(&pNew->base, 0);
177622 sqlite3_mutex_free(pNew->mutex);
177635 pRbu->szTempLimit = n;
177637 return pRbu->szTempLimit;
177641 return pRbu->szTemp;
177665 ** The dbstat virtual table is used to extract low-level formatting
177682 ** root-node of the b-tree structure to each page. The value of the
177683 ** root-node path is '/'.
177685 ** The value of the path for the left-most child page of the root of
177686 ** a b-tree is '/000/'. (Btrees store content ordered from left to right
177688 ** The next to left-most child of the root page is
177689 ** '/001', and so on, each sibling page identified by a 3-digit hex
177690 ** value. The children of the 451st left-most sibling have paths such
177694 ** six-digit hexadecimal value to the path to the cell they are linked
177696 ** the left-most cell of the 450th child of the root page are identified
177705 ** sort-order than its child page:
177707 ** '/1c2/000/' // Left-most child of 451st child of root
177751 u32 iRightChildPg; /* Right-child page number (or 0) */
177821 pTab->db = db;
177822 pTab->iDb = iDb;
177838 ** There is no "best-index". This virtual table always does a linear
177847 pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */
177854 for(i=0; i<pIdxInfo->nConstraint; i++){
177855 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
177856 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
177857 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
177858 pIdxInfo->idxNum = 1;
177859 pIdxInfo->estimatedCost = 1.0;
177860 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
177861 pIdxInfo->aConstraintUsage[i].omit = 1;
177870 if( ( pIdxInfo->nOrderBy==1
177871 && pIdxInfo->aOrderBy[0].iColumn==0
177872 && pIdxInfo->aOrderBy[0].desc==0
177874 ( pIdxInfo->nOrderBy==2
177875 && pIdxInfo->aOrderBy[0].iColumn==0
177876 && pIdxInfo->aOrderBy[0].desc==0
177877 && pIdxInfo->aOrderBy[1].iColumn==1
177878 && pIdxInfo->aOrderBy[1].desc==0
177881 pIdxInfo->orderByConsumed = 1;
177899 pCsr->base.pVtab = pVTab;
177900 pCsr->iDb = pTab->iDb;
177909 if( p->aCell ){
177910 for(i=0; i<p->nCell; i++){
177911 sqlite3_free(p->aCell[i].aOvfl);
177913 sqlite3_free(p->aCell);
177915 sqlite3PagerUnref(p->pPg);
177916 sqlite3_free(p->zPath);
177922 sqlite3_reset(pCsr->pStmt);
177923 for(i=0; i<ArraySize(pCsr->aPage); i++){
177924 statClearPage(&pCsr->aPage[i]);
177926 pCsr->iPage = 0;
177927 sqlite3_free(pCsr->zPath);
177928 pCsr->zPath = 0;
177929 pCsr->isEof = 0;
177938 sqlite3_finalize(pCsr->pStmt);
177954 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
177955 nMaxLocal = nUsable - 35;
177957 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
177958 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
177961 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
177973 u8 *aData = sqlite3PagerGetData(p->pPg);
177974 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
177976 p->flags = aHdr[0];
177977 p->nCell = get2byte(&aHdr[3]);
177978 p->nMxPayload = 0;
177980 isLeaf = (p->flags==0x0A || p->flags==0x0D);
177981 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
177983 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
177990 p->nUnused = nUnused;
177991 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
177994 if( p->nCell ){
177999 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
178001 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
178002 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
178003 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
178005 for(i=0; i<p->nCell; i++){
178006 StatCell *pCell = &p->aCell[i];
178010 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
178013 if( p->flags==0x05 ){
178019 if( p->flags==0x0D ){
178023 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
178024 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
178025 pCell->nLocal = nLocal;
178028 assert( nLocal<=(nUsable-35) );
178031 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
178032 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
178033 pCell->nOvfl = nOvfl;
178034 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
178035 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
178036 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
178039 u32 iPrev = pCell->aOvfl[j-1];
178046 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
178058 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
178059 ** the current value of pCsr->iPageno.
178062 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
178063 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
178069 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
178070 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
178076 x[0] = pCsr->iPageno;
178077 if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
178078 pCsr->iOffset = x[0];
178079 pCsr->szPage = (int)x[1];
178091 StatTable *pTab = (StatTable *)pCursor->pVtab;
178092 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
178095 sqlite3_free(pCsr->zPath);
178096 pCsr->zPath = 0;
178099 if( pCsr->aPage[0].pPg==0 ){
178100 rc = sqlite3_step(pCsr->pStmt);
178103 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
178106 pCsr->isEof = 1;
178107 return sqlite3_reset(pCsr->pStmt);
178109 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
178110 pCsr->aPage[0].iPgno = iRoot;
178111 pCsr->aPage[0].iCell = 0;
178112 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
178113 pCsr->iPage = 0;
178116 pCsr->isEof = 1;
178117 return sqlite3_reset(pCsr->pStmt);
178122 StatPage *p = &pCsr->aPage[pCsr->iPage];
178124 while( p->iCell<p->nCell ){
178125 StatCell *pCell = &p->aCell[p->iCell];
178126 if( pCell->iOvfl<pCell->nOvfl ){
178129 nUsable = sqlite3BtreeGetPageSize(pBt) -
178132 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
178133 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
178134 pCsr->zPagetype = "overflow";
178135 pCsr->nCell = 0;
178136 pCsr->nMxPayload = 0;
178137 pCsr->zPath = z = sqlite3_mprintf(
178138 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
178140 if( pCell->iOvfl<pCell->nOvfl-1 ){
178141 pCsr->nUnused = 0;
178142 pCsr->nPayload = nUsable - 4;
178144 pCsr->nPayload = pCell->nLastOvfl;
178145 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
178147 pCell->iOvfl++;
178151 if( p->iRightChildPg ) break;
178152 p->iCell++;
178155 if( !p->iRightChildPg || p->iCell>p->nCell ){
178157 if( pCsr->iPage==0 ) return statNext(pCursor);
178158 pCsr->iPage--;
178161 pCsr->iPage++;
178162 assert( p==&pCsr->aPage[pCsr->iPage-1] );
178164 if( p->iCell==p->nCell ){
178165 p[1].iPgno = p->iRightChildPg;
178167 p[1].iPgno = p->aCell[p->iCell].iChildPg;
178171 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
178172 p->iCell++;
178182 StatPage *p = &pCsr->aPage[pCsr->iPage];
178183 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
178184 pCsr->iPageno = p->iPgno;
178190 switch( p->flags ){
178193 pCsr->zPagetype = "internal";
178197 pCsr->zPagetype = "leaf";
178200 pCsr->zPagetype = "corrupted";
178203 pCsr->nCell = p->nCell;
178204 pCsr->nUnused = p->nUnused;
178205 pCsr->nMxPayload = p->nMxPayload;
178206 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
178209 for(i=0; i<p->nCell; i++){
178210 nPayload += p->aCell[i].nLocal;
178212 pCsr->nPayload = nPayload;
178221 return pCsr->isEof;
178230 StatTable *pTab = (StatTable*)(pCursor->pVtab);
178237 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
178238 if( pCsr->iDb<0 ){
178239 sqlite3_free(pCursor->pVtab->zErrMsg);
178240 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
178241 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
178244 pCsr->iDb = pTab->iDb;
178247 sqlite3_finalize(pCsr->pStmt);
178248 pCsr->pStmt = 0;
178249 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
178255 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster);
178259 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
178277 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
178280 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
178283 sqlite3_result_int64(ctx, pCsr->iPageno);
178286 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
178289 sqlite3_result_int(ctx, pCsr->nCell);
178292 sqlite3_result_int(ctx, pCsr->nPayload);
178295 sqlite3_result_int(ctx, pCsr->nUnused);
178298 sqlite3_result_int(ctx, pCsr->nMxPayload);
178301 sqlite3_result_int64(ctx, pCsr->iOffset);
178304 sqlite3_result_int(ctx, pCsr->szPage);
178308 int iDb = pCsr->iDb;
178309 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
178318 *pRowid = pCsr->iPageno;
178333 statOpen, /* xOpen - open a cursor */
178334 statClose, /* xClose - close a cursor */
178335 statFilter, /* xFilter - configure scan constraints */
178336 statNext, /* xNext - advance a cursor */
178337 statEof, /* xEof - check for end of scan */
178338 statColumn, /* xColumn - read data */
178339 statRowid, /* xRowid - read data */
178360 ** 2017-10-11
178444 pTab->db = db;
178475 for(i=0; i<pIdxInfo->nConstraint; i++){
178476 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
178477 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
178478 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
178479 if( !p->usable ){
178481 pIdxInfo->estimatedRows = 0x7fffffff;
178485 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
178486 pIdxInfo->aConstraintUsage[i].omit = 1;
178494 pIdxInfo->estimatedCost = 1.0e6;
178497 for(i=0; i<pIdxInfo->nConstraint; i++){
178498 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
178499 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
178500 pIdxInfo->estimatedRows = 1;
178501 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
178502 pIdxInfo->estimatedCost = 1.0;
178503 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
178504 pIdxInfo->aConstraintUsage[i].omit = 1;
178509 pIdxInfo->idxNum = iPlan;
178511 if( pIdxInfo->nOrderBy>=1
178512 && pIdxInfo->aOrderBy[0].iColumn<=0
178513 && pIdxInfo->aOrderBy[0].desc==0
178515 pIdxInfo->orderByConsumed = 1;
178531 pCsr->base.pVtab = pVTab;
178532 pCsr->pgno = -1;
178544 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
178555 pCsr->pgno++;
178561 return pCsr->pgno > pCsr->mxPgno;
178580 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
178582 sqlite3 *db = pTab->db;
178586 pCsr->pgno = 1;
178587 pCsr->mxPgno = 0;
178593 pCsr->iDb = sqlite3FindDbName(db, zSchema);
178594 if( pCsr->iDb<0 ) return SQLITE_OK;
178596 pCsr->iDb = 0;
178598 pBt = db->aDb[pCsr->iDb].pBt;
178600 pCsr->pPager = sqlite3BtreePager(pBt);
178601 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
178602 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
178605 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
178606 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
178607 pCsr->pgno = 1;
178608 pCsr->mxPgno = 0;
178610 pCsr->mxPgno = pCsr->pgno;
178613 assert( pCsr->pgno==1 );
178615 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
178616 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
178629 sqlite3_result_int(ctx, pCsr->pgno);
178634 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
178636 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
178644 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
178653 *pRowid = pCsr->pgno;
178684 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
178689 pBt = pTab->db->aDb[iDb].pBt;
178715 sqlite3_free(pVtab->zErrMsg);
178716 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
178726 sqlite3 *db = pTab->db;
178728 for(i=0; i<db->nDb; i++){
178729 Btree *pBt = db->aDb[i].pBt;
178747 dbpageOpen, /* xOpen - open a cursor */
178748 dbpageClose, /* xClose - close a cursor */
178749 dbpageFilter, /* xFilter - configure scan constraints */
178750 dbpageNext, /* xNext - advance a cursor */
178751 dbpageEof, /* xEof - check for end of scan */
178752 dbpageColumn, /* xColumn - read data */
178753 dbpageRowid, /* xRowid - read data */
178817 int bAutoAttach; /* True to auto-attach tables */
178818 int rc; /* Non-zero if an error has occurred */
178881 ** The data associated with each hash-table entry is a structure containing
178902 ** change-set binary format, and so must be architecture independent.
178904 ** Unlike the SQLite database record format, each field is self-contained -
178922 ** UTF-8). Followed by a buffer containing the UTF-8 representation
178930 ** An 8-byte big-endian integer value.
178933 ** An 8-byte big-endian IEEE 754-2008 real value.
178950 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
178955 ** 1 byte: The "indirect-change" flag.
178992 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
178997 ** 1 byte: The "indirect-change" flag.
179029 SessionChange *pNext; /* For hash-table collisions */
179055 /* Load an unaligned and unsigned 32-bit integer */
179059 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
179070 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
179087 ** If it is non-NULL, the serialized form of the value is written to
179093 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
179097 u8 *aBuf, /* If non-NULL, write serialized value here */
179117 /* TODO: SQLite does something special to deal with mixed-endian
179171 ** as a series of unsigned integers. In order to calculate a hash-key value
179186 ** Append the hash of the 64-bit integer passed as the second argument to the
179187 ** hash-key value passed as the first. Return the new hash-key value.
179196 ** the hash-key value passed as the first. Return the new hash-key value.
179206 ** hash-key value passed as the first. Return the new hash-key value.
179213 ** This function may only be called from within a pre-update callback.
179234 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
179235 for(i=0; i<pTab->nCol; i++){
179236 if( pTab->abPK[i] ){
179242 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
179244 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
179273 assert( pTab->bStat1==0 || i!=1 );
179279 *piHash = (h % pTab->nChange);
179303 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
179304 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
179316 for(i=0; i<pTab->nCol; i++){
179318 int isPK = pTab->abPK[i];
179366 for(iCol=0; iCol<pTab->nCol; iCol++){
179367 if( pTab->abPK[iCol] ){
179371 if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
179434 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
179445 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
179446 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
179501 for(i=0; i<pTab->nCol; i++){
179509 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
179510 if( pTab->abPK[i]==0 ) bRequired = 1;
179526 for(i=0; i<pTab->nCol; i++){
179535 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
179549 ** This function is only called from within a pre-update-hook callback.
179550 ** It determines if the current pre-update-hook change affects the same row
179552 ** if the pre-update-hook does not affect the same row as pChange, it returns
179559 int op /* Current pre-update operation */
179562 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
179565 for(iCol=0; iCol<pTab->nCol; iCol++){
179566 if( !pTab->abPK[iCol] ){
179579 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
179580 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
179582 /* assert( db->pPreUpdate->pUnpacked ); */
179583 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
179629 ** It is possible that a non-fatal OOM error occurs in this function. In
179630 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
179635 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
179638 int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
179642 if( pTab->nChange==0 ){
179649 for(i=0; i<pTab->nChange; i++){
179652 for(p=pTab->apChange[i]; p; p=pNext){
179653 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
179654 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
179655 pNext = p->pNext;
179656 p->pNext = apNew[iHash];
179661 sqlite3_free(pTab->apChange);
179662 pTab->nChange = nNew;
179663 pTab->apChange = apNew;
179675 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
179677 ** NULL) is set to point to an array of booleans - true if the corresponding
179701 u8 **pabPK /* OUT: Array of booleans - true for PK col */
179736 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
179799 ** This function is only called from within a pre-update handler for a
179805 ** non-zero returned. Or, if no error occurs but the table has no primary
179806 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
179811 if( pTab->nCol==0 ){
179813 assert( pTab->azCol==0 || pTab->abPK==0 );
179814 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
179815 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
179817 if( pSession->rc==SQLITE_OK ){
179819 for(i=0; i<pTab->nCol; i++){
179821 pTab->abPK = abPK;
179825 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
179826 pTab->bStat1 = 1;
179830 return (pSession->rc || pTab->abPK==0);
179835 ** sqlite_stat1 table. The purpose of this is to substitute a zero-length
179847 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
179849 pVal = p->pSession->pZeroBlob;
179857 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
179859 pVal = p->pSession->pZeroBlob;
179866 return p->hook.xCount(p->hook.pCtx);
179870 return p->hook.xDepth(p->hook.pCtx);
179875 ** This function is only called from with a pre-update-hook reporting a
179880 ** to the changed-rows hash table associated with table pTab.
179892 if( pSession->rc ) return;
179899 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
179900 pSession->rc = SQLITE_SCHEMA;
179906 pSession->rc = SQLITE_NOMEM;
179910 if( pTab->bStat1 ){
179911 stat1.hook = pSession->hook;
179913 pSession->hook.pCtx = (void*)&stat1;
179914 pSession->hook.xNew = sessionStat1New;
179915 pSession->hook.xOld = sessionStat1Old;
179916 pSession->hook.xCount = sessionStat1Count;
179917 pSession->hook.xDepth = sessionStat1Depth;
179918 if( pSession->pZeroBlob==0 ){
179925 pSession->pZeroBlob = p;
179929 /* Calculate the hash-key for this change. If the primary key of the row
179938 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
179951 pTab->nEntry++;
179955 for(i=0; i<pTab->nCol; i++){
179958 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
179960 }else if( pTab->abPK[i] ){
179961 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
179965 /* This may fail if SQLite value p contains a utf-16 string that must
179966 ** be converted to utf-8 and an OOM error occurs while doing so. */
179978 pChange->aRecord = (u8 *)&pChange[1];
179986 for(i=0; i<pTab->nCol; i++){
179989 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
179990 }else if( pTab->abPK[i] ){
179991 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
179993 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
179996 /* Add the change to the hash-table */
179997 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
179998 pChange->bIndirect = 1;
180000 pChange->nRecord = nByte;
180001 pChange->op = op;
180002 pChange->pNext = pTab->apChange[iHash];
180003 pTab->apChange[iHash] = pChange;
180005 }else if( pC->bIndirect ){
180008 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
180009 && pSession->bIndirect==0
180011 pC->bIndirect = 0;
180018 if( pTab->bStat1 ){
180019 pSession->hook = stat1.hook;
180022 pSession->rc = rc;
180036 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
180037 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
180040 if( pRet==0 && pSession->bAutoAttach ){
180041 /* If there is a table-filter configured, invoke it. If it returns 0,
180043 if( pSession->xTableFilter==0
180044 || pSession->xTableFilter(pSession->pFilterCtx, zName)
180048 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
180049 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
180060 ** The 'pre-update' hook registered by this module with SQLite databases.
180074 assert( sqlite3_mutex_held(db->mutex) );
180076 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
180082 if( pSession->bEnable==0 ) continue;
180083 if( pSession->rc ) continue;
180084 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
180086 pSession->rc = sessionFindTable(pSession, zName, &pTab);
180088 assert( pSession->rc==SQLITE_OK );
180098 ** The pre-update hook implementations.
180114 ** Install the pre-update hooks on the session object passed as the only
180120 pSession->hook.pCtx = (void*)pSession->db;
180121 pSession->hook.xOld = sessionPreupdateOld;
180122 pSession->hook.xNew = sessionPreupdateNew;
180123 pSession->hook.xCount = sessionPreupdateCount;
180124 pSession->hook.xDepth = sessionPreupdateDepth;
180138 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
180143 *ppVal = sqlite3_column_value(p->pStmt, iVal);
180148 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
180162 pSession->hook.pCtx = (void*)pDiffCtx;
180163 pSession->hook.xOld = sessionDiffOld;
180164 pSession->hook.xNew = sessionDiffNew;
180165 pSession->hook.xCount = sessionDiffCount;
180166 pSession->hook.xDepth = sessionDiffDepth;
180248 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
180254 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
180256 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
180257 pDiffCtx->pStmt = pStmt;
180258 pDiffCtx->nOldOff = 0;
180278 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
180279 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
180286 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
180292 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
180295 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
180296 pDiffCtx->pStmt = pStmt;
180297 pDiffCtx->nOldOff = pTab->nCol;
180316 const char *zDb = pSession->zDb;
180317 int rc = pSession->rc;
180323 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
180327 sqlite3 *db = pSession->db;
180334 rc = pSession->rc;
180347 if( pTo->nCol!=nCol ){
180352 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
180353 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
180370 zExpr = sessionExprComparePK(pTo->nCol,
180371 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
180395 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
180419 pNew->db = db;
180420 pNew->zDb = (char *)&pNew[1];
180421 pNew->bEnable = 1;
180422 memcpy(pNew->zDb, zDb, nDb+1);
180430 pNew->pNext = pOld;
180439 ** of the changed-rows hash tables are also deleted.
180447 pNext = pTab->pNext;
180448 for(i=0; i<pTab->nChange; i++){
180451 for(p=pTab->apChange[i]; p; p=pNextChange){
180452 pNextChange = p->pNext;
180456 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
180457 sqlite3_free(pTab->apChange);
180466 sqlite3 *db = pSession->db;
180474 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
180476 *pp = (*pp)->pNext;
180482 sqlite3ValueFree(pSession->pZeroBlob);
180485 ** associated hash-tables. */
180486 sessionDeleteTable(pSession->pTable);
180500 pSession->bAutoAttach = 1;
180501 pSession->pFilterCtx = pCtx;
180502 pSession->xTableFilter = xFilter;
180518 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
180521 pSession->bAutoAttach = 1;
180527 ** a no-op. Return early. */
180529 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
180530 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
180546 pTab->zName = (char *)&pTab[1];
180547 memcpy(pTab->zName, zName, nName+1);
180548 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
180554 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
180563 ** set *pRc to SQLITE_NOMEM and return non-zero.
180566 if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
180568 int nNew = p->nAlloc ? p->nAlloc : 128;
180571 }while( nNew<(p->nBuf+nByte) );
180573 aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
180577 p->aBuf = aNew;
180578 p->nAlloc = nNew;
180588 ** This function is a no-op if *pRc is non-zero when it is called.
180599 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
180600 p->nBuf += nByte;
180608 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180616 p->aBuf[p->nBuf++] = v;
180621 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180629 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
180634 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180647 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
180648 p->nBuf += nBlob;
180653 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180655 ** up to (but not including) the nul-terminator are written to the buffer.
180667 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
180668 p->nBuf += nStr;
180673 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180675 ** to the buffer. No nul-terminator is written.
180686 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
180691 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180694 ** nul-terminator byte is written.
180706 char *zOut = (char *)&p->aBuf[p->nBuf];
180714 p->nBuf = (int)((u8 *)zOut - p->aBuf);
180719 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
180791 u8 *abPK /* Boolean array - true for PK columns */
180796 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
180798 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
180801 sessionAppendByte(pBuf, p->bIndirect, &rc);
180846 /* If at least one field has been modified, this is not a no-op. */
180871 pBuf->nBuf = nRewind;
180890 u8 *abPK /* Boolean array - true for PK columns */
180895 sessionAppendByte(pBuf, p->bIndirect, &rc);
180898 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
180901 u8 *a = p->aRecord;
180925 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
180928 assert( (a - p->aRecord)==p->nRecord );
180951 int nSql = -1;
181004 u8 *a = pChange->aRecord;
181062 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
181075 sessionAppendVarint(pBuf, pTab->nCol, pRc);
181076 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
181077 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
181082 ** (if it is non-zero) based on the current contents of the session object
181098 sqlite3 *db = pSession->db; /* Source database handle */
181107 ** this call will be a no-op. */
181113 if( pSession->rc ) return pSession->rc;
181114 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
181119 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
181120 if( pTab->nEntry ){
181121 const char *zName = pTab->zName;
181131 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
181132 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
181142 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
181146 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
181149 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
181153 if( p->op==SQLITE_INSERT ){
181156 sessionAppendByte(&buf, p->bIndirect, &rc);
181163 }else if( p->op!=SQLITE_INSERT ){
181178 nNoop = -1;
181266 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
181268 pSession->bEnable = bEnable;
181270 ret = pSession->bEnable;
181271 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
181280 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
181282 pSession->bIndirect = bIndirect;
181284 ret = pSession->bIndirect;
181285 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
181297 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
181298 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
181299 ret = (pTab->nEntry>0);
181301 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
181329 pRet->in.aData = (u8 *)pChangeset;
181330 pRet->in.nData = nChangeset;
181331 pRet->in.xInput = xInput;
181332 pRet->in.pIn = pIn;
181333 pRet->in.bEof = (xInput ? 0 : 1);
181367 if( pIn->bEof && pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
181368 int nMove = pIn->buf.nBuf - pIn->iNext;
181371 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
181373 pIn->buf.nBuf -= pIn->iNext;
181374 pIn->iNext = 0;
181375 pIn->nData = pIn->buf.nBuf;
181388 if( pIn->xInput ){
181389 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
181392 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
181393 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
181394 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
181396 pIn->bEof = 1;
181398 pIn->buf.nBuf += nNew;
181402 pIn->aData = pIn->buf.aBuf;
181403 pIn->nData = pIn->buf.nBuf;
181495 eType = pIn->aData[pIn->iNext++];
181505 u8 *aVal = &pIn->aData[pIn->iNext];
181508 pIn->iNext += sessionVarintGet(aVal, &nByte);
181512 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
181514 pIn->iNext += nByte;
181525 pIn->iNext += 8;
181534 ** The input pointer currently points to the second byte of a table-header.
181553 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181559 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181562 if( (pIn->iNext + nRead)<pIn->nData ) break;
181590 eType = pIn->aData[pIn->iNext + nByte++];
181593 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
181606 ** The input pointer currently points to the second byte of a table-header.
181613 ** This function decodes the table-header and populates the p->nCol,
181614 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
181615 ** also allocated or resized according to the new value of p->nCol. The
181625 assert( p->rc==SQLITE_OK );
181627 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181631 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181632 nCopy -= nVarint;
181633 p->in.iNext += nVarint;
181634 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181635 p->tblhdr.nBuf = 0;
181636 sessionBufferGrow(&p->tblhdr, nByte, &rc);
181640 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181641 memset(p->tblhdr.aBuf, 0, iPK);
181642 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
181643 p->in.iNext += nCopy;
181646 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
181647 p->abPK = (u8*)&p->apValue[p->nCol*2];
181648 p->zTab = (char*)&p->abPK[p->nCol];
181649 return (p->rc = rc);
181670 u8 **paRec, /* If non-NULL, store record pointer here */
181671 int *pnRec /* If non-NULL, store size of record here */
181678 /* If the iterator is in the error-state, return immediately. */
181679 if( p->rc!=SQLITE_OK ) return p->rc;
181681 /* Free the current contents of p->apValue[], if any. */
181682 if( p->apValue ){
181683 for(i=0; i<p->nCol*2; i++){
181684 sqlite3ValueFree(p->apValue[i]);
181686 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
181693 p->rc = sessionInputBuffer(&p->in, 2);
181694 if( p->rc!=SQLITE_OK ) return p->rc;
181697 if( p->in.iNext>=p->in.nData ){
181701 sessionDiscardData(&p->in);
181702 p->in.iCurrent = p->in.iNext;
181704 op = p->in.aData[p->in.iNext++];
181706 p->bPatchset = (op=='P');
181707 if( sessionChangesetReadTblhdr(p) ) return p->rc;
181708 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181709 p->in.iCurrent = p->in.iNext;
181710 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181711 op = p->in.aData[p->in.iNext++];
181714 p->op = op;
181715 p->bIndirect = p->in.aData[p->in.iNext++];
181716 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181717 return (p->rc = SQLITE_CORRUPT_BKPT);
181722 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
181723 nVal = p->nCol * 2;
181724 }else if( p->bPatchset && op==SQLITE_DELETE ){
181726 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
181728 nVal = p->nCol;
181730 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
181731 if( p->rc!=SQLITE_OK ) return p->rc;
181732 *paRec = &p->in.aData[p->in.iNext];
181733 p->in.iNext += *pnRec;
181737 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
181738 u8 *abPK = p->bPatchset ? p->abPK : 0;
181739 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
181740 if( p->rc!=SQLITE_OK ) return p->rc;
181744 if( p->op!=SQLITE_DELETE ){
181745 p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
181746 if( p->rc!=SQLITE_OK ) return p->rc;
181749 if( p->bPatchset && p->op==SQLITE_UPDATE ){
181754 for(i=0; i<p->nCol; i++){
181755 assert( p->apValue[i]==0 );
181756 assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181757 if( p->abPK[i] ){
181758 p->apValue[i] = p->apValue[i+p->nCol];
181759 p->apValue[i+p->nCol] = 0;
181792 *pOp = pIter->op;
181793 *pnCol = pIter->nCol;
181794 *pzTab = pIter->zTab;
181795 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
181807 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
181810 *pabPK = pIter->abPK;
181811 if( pnCol ) *pnCol = pIter->nCol;
181825 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
181833 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
181836 if( iVal<0 || iVal>=pIter->nCol ){
181839 *ppValue = pIter->apValue[iVal];
181853 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
181861 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
181864 if( iVal<0 || iVal>=pIter->nCol ){
181867 *ppValue = pIter->apValue[pIter->nCol+iVal];
181876 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
181877 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
181882 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
181887 ** If value iVal is out-of-range or some other error occurs, an SQLite error
181895 if( !pIter->pConflict ){
181898 if( iVal<0 || iVal>=pIter->nCol ){
181901 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
181917 if( pIter->pConflict || pIter->apValue ){
181920 *pnOut = pIter->nCol;
181934 int i; /* Used to iterate through p->apValue[] */
181935 rc = p->rc;
181936 if( p->apValue ){
181937 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
181939 sqlite3_free(p->tblhdr.aBuf);
181940 sqlite3_free(p->in.buf.aBuf);
181974 if( pInput->iNext>=pInput->nData ) break;
181975 eType = pInput->aData[pInput->iNext];
181984 ** * A nul-terminated table name.
181988 pInput->iNext++;
181992 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
181994 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
181996 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
181999 pInput->iNext += nByte;
182009 int bIndirect = pInput->aData[pInput->iNext+1];
182011 pInput->iNext += 2;
182016 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
182017 pInput->iNext += nByte;
182036 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
182039 pInput->iNext += 2;
182152 u8 *abPK; /* Boolean array - true if column is in PK */
182178 SessionApplyCtx *p /* Session changeset-apply context */
182190 for(i=0; i<p->nCol; i++){
182191 if( p->abPK[i] ){
182194 sessionAppendIdent(&buf, p->azCol[i], &rc);
182201 if( nPk<p->nCol ){
182203 sessionAppendInteger(&buf, p->nCol+1, &rc);
182207 for(i=0; i<p->nCol; i++){
182208 if( !p->abPK[i] ){
182210 sessionAppendIdent(&buf, p->azCol[i], &rc);
182220 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
182251 ** a row even if the non-PK values do not match. This is required if the
182252 ** conflict-handler is invoked with CHANGESET_DATA and returns
182261 SessionApplyCtx *p /* Session changeset-apply context */
182274 for(i=0; i<p->nCol; i++){
182276 sessionAppendIdent(&buf, p->azCol[i], &rc);
182282 sessionAppendIdent(&buf, p->azCol[i], &rc);
182289 for(i=0; i<p->nCol; i++){
182290 if( p->abPK[i] ){
182291 sessionAppendIdent(&buf, p->azCol[i], &rc);
182298 /* Append the non-PK part of the WHERE clause */
182300 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
182302 for(i=0; i<p->nCol; i++){
182303 if( !p->abPK[i] ){
182307 sessionAppendIdent(&buf, p->azCol[i], &rc);
182316 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
182340 SessionApplyCtx *p /* Session changeset-apply context */
182343 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
182358 SessionApplyCtx *p /* Session changeset-apply context */
182367 for(i=0; i<p->nCol; i++){
182369 sessionAppendIdent(&buf, p->azCol[i], &rc);
182373 for(i=1; i<p->nCol; i++){
182379 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
182386 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
182398 rc = sessionPrepare(db, &p->pInsert,
182405 rc = sessionPrepare(db, &p->pUpdate,
182416 rc = sessionPrepare(db, &p->pDelete,
182436 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
182438 ** the (pVal->z) variable remains as it was or the type of the value is
182440 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
182443 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
182455 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
182540 ** -------------------------------------------------
182551 ** ----------------------------------------------------------------
182559 ** is set to non-zero before returning SQLITE_OK.
182588 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
182595 pIter->pConflict = p->pSelect;
182597 pIter->pConflict = 0;
182598 rc = sqlite3_reset(p->pSelect);
182600 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
182603 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
182604 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
182605 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
182674 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
182675 assert( p->azCol && p->abPK );
182693 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
182694 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
182695 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
182696 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
182700 sqlite3_step(p->pDelete);
182701 rc = sqlite3_reset(p->pDelete);
182702 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
182720 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
182722 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
182725 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
182729 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
182735 sqlite3_step(p->pUpdate);
182736 rc = sqlite3_reset(p->pUpdate);
182738 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
182756 if( p->bStat1 ){
182760 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
182763 sqlite3_reset(p->pSelect);
182768 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
182771 sqlite3_step(p->pInsert);
182772 rc = sqlite3_reset(p->pInsert);
182791 ** function handles the case where the conflict-handler is invoked and
182792 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
182817 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
182827 assert( pIter->op==SQLITE_INSERT );
182831 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
182832 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
182835 sqlite3_step(pApply->pDelete);
182836 rc = sqlite3_reset(pApply->pDelete);
182850 ** Retry the changes accumulated in the pApply->constraints buffer.
182862 while( pApply->constraints.nBuf ){
182864 SessionBuffer cons = pApply->constraints;
182865 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
182869 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
182871 pIter2->bPatchset = bPatchset;
182872 pIter2->zTab = (char*)zTab;
182873 pIter2->nCol = pApply->nCol;
182874 pIter2->abPK = pApply->abPK;
182875 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
182876 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
182877 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
182886 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
182890 if( pApply->constraints.nBuf>=cons.nBuf ){
182892 pApply->bDeferConstraints = 0;
182929 pIter->in.bNoDiscard = 1;
182947 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
182962 ** non-zero, proceed. */
183034 bPatchset = pIter->bPatchset;
183166 pNew->op = op2;
183167 pNew->bIndirect = bIndirect;
183168 pNew->nRecord = nRec;
183169 pNew->aRecord = (u8*)&pNew[1];
183170 memcpy(pNew->aRecord, aRec, nRec);
183172 int op1 = pExist->op;
183175 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
183176 ** op1=INSERT, op2=UPDATE -> INSERT.
183177 ** op1=INSERT, op2=DELETE -> (none)
183179 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
183180 ** op1=UPDATE, op2=UPDATE -> UPDATE.
183181 ** op1=UPDATE, op2=DELETE -> DELETE.
183183 ** op1=DELETE, op2=INSERT -> UPDATE.
183184 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
183185 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
183197 u8 *aExist = pExist->aRecord;
183204 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
183211 pNew->bIndirect = (bIndirect && pExist->bIndirect);
183212 aCsr = pNew->aRecord = (u8 *)&pNew[1];
183217 pNew->op = SQLITE_INSERT;
183218 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
183219 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
183222 pNew->op = SQLITE_UPDATE;
183237 sessionSkipRecord(&a1, pTab->nCol);
183238 sessionSkipRecord(&a2, pTab->nCol);
183240 pNew->op = SQLITE_UPDATE;
183247 pNew->op = SQLITE_DELETE;
183252 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
183257 pNew->nRecord = (int)(aCsr - pNew->aRecord);
183291 if( pGrp->pList==0 ){
183292 pGrp->bPatch = pIter->bPatchset;
183293 }else if( pIter->bPatchset!=pGrp->bPatch ){
183299 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
183305 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
183306 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
183317 pTab->nCol = nCol;
183318 pTab->abPK = (u8*)&pTab[1];
183319 memcpy(pTab->abPK, abPK, nCol);
183320 pTab->zName = (char*)&pTab->abPK[nCol];
183321 memcpy(pTab->zName, zNew, nNew+1);
183327 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
183329 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
183335 if( sessionGrowHash(pIter->bPatchset, pTab) ){
183340 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
183346 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
183349 if( pIter->bPatchset ){
183350 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
183353 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
183355 *pp = (*pp)->pNext;
183356 pTab->nEntry--;
183362 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
183366 pChange->pNext = pTab->apChange[iHash];
183367 pTab->apChange[iHash] = pChange;
183368 pTab->nEntry++;
183372 if( rc==SQLITE_OK ) rc = pIter->rc;
183407 ** hash tables attached to the SessionTable objects in list p->pList.
183409 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
183411 if( pTab->nEntry==0 ) continue;
183413 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
183414 for(i=0; i<pTab->nChange; i++){
183416 for(p=pTab->apChange[i]; p; p=p->pNext){
183417 sessionAppendByte(&buf, p->op, &rc);
183418 sessionAppendByte(&buf, p->bIndirect, &rc);
183419 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
183461 ** to changeset-group p.
183522 sessionDeleteTable(pGrp->pList);
183590 ** 2015-08-12
183630 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
183654 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
183695 sqlite3_context *pCtx; /* Function context - put error messages here */
183741 u32 n; /* Bytes of content, or number of sub-nodes */
183781 p->zBuf = p->zSpace;
183782 p->nAlloc = sizeof(p->zSpace);
183783 p->nUsed = 0;
183784 p->bStatic = 1;
183790 p->pCtx = pCtx;
183791 p->bErr = 0;
183800 if( !p->bStatic ) sqlite3_free(p->zBuf);
183805 /* Report an out-of-memory (OOM) condition
183808 p->bErr = 1;
183809 sqlite3_result_error_nomem(p->pCtx);
183813 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
183814 ** Return zero on success. Return non-zero on an OOM error
183817 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
183819 if( p->bStatic ){
183820 if( p->bErr ) return 1;
183826 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
183827 p->zBuf = zNew;
183828 p->bStatic = 0;
183830 zNew = sqlite3_realloc64(p->zBuf, nTotal);
183835 p->zBuf = zNew;
183837 p->nAlloc = nTotal;
183844 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
183845 memcpy(p->zBuf+p->nUsed, zIn, N);
183846 p->nUsed += N;
183853 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
183855 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
183857 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
183863 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
183864 p->zBuf[p->nUsed++] = c;
183872 if( p->nUsed==0 ) return;
183873 c = p->zBuf[p->nUsed-1];
183877 /* Append the N-byte string in zIn to the end of the JsonString string
183879 ** any double-quotes or backslash characters contained within the
183884 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
183885 p->zBuf[p->nUsed++] = '"';
183890 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
183891 p->zBuf[p->nUsed++] = '\\';
183907 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
183908 p->zBuf[p->nUsed++] = '\\';
183909 p->zBuf[p->nUsed++] = 'u';
183910 p->zBuf[p->nUsed++] = '0';
183911 p->zBuf[p->nUsed++] = '0';
183912 p->zBuf[p->nUsed++] = '0' + (c>>4);
183915 p->zBuf[p->nUsed++] = c;
183917 p->zBuf[p->nUsed++] = '"';
183918 assert( p->nUsed<p->nAlloc );
183952 if( p->bErr==0 ){
183953 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
183954 p->bErr = 2;
183966 if( p->bErr==0 ){
183967 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
183968 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
183972 assert( p->bStatic );
183989 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
183997 sqlite3_free(pParse->aNode);
183998 pParse->aNode = 0;
183999 pParse->nNode = 0;
184000 pParse->nAlloc = 0;
184001 sqlite3_free(pParse->aUp);
184002 pParse->aUp = 0;
184023 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
184024 if( pNode->jnFlags & JNODE_REPLACE ){
184025 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
184028 pNode = pNode->u.pPatch;
184030 switch( pNode->eType ){
184032 assert( pNode->eType==JSON_NULL );
184045 if( pNode->jnFlags & JNODE_RAW ){
184046 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
184053 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
184060 while( j<=pNode->n ){
184067 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
184068 pNode = &pNode[pNode->u.iAppend];
184078 while( j<=pNode->n ){
184087 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
184088 pNode = &pNode[pNode->u.iAppend];
184120 switch( pNode->eType ){
184122 assert( pNode->eType==JSON_NULL );
184136 const char *z = pNode->u.zJContent;
184137 if( z[0]=='-' ){ z++; }
184139 unsigned v = *(z++) - '0';
184145 if( pNode->u.zJContent[0]=='-' ){
184155 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
184164 const char *z = pNode->u.zJContent;
184167 r = strtod(pNode->u.zJContent, 0);
184176 if( pNode->jnFlags & JNODE_RAW ){
184177 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
184181 assert( (pNode->jnFlags & JNODE_RAW)==0 );
184182 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
184183 /* JSON formatted without any backslash-escapes */
184184 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
184189 u32 n = pNode->n;
184190 const char *z = pNode->u.zJContent;
184198 for(i=1, j=0; i<n-1; i++){
184207 assert( i<n-2 );
184210 if( c<='9' ) v = v*16 + c - '0';
184211 else if( c<='F' ) v = v*16 + c - 'A' + 10;
184212 else v = v*16 + c - 'a' + 10;
184273 u32 n, /* Content size or sub-node count */
184278 assert( pParse->nNode>=pParse->nAlloc );
184279 if( pParse->oom ) return -1;
184280 nNew = pParse->nAlloc*2 + 10;
184281 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
184283 pParse->oom = 1;
184284 return -1;
184286 pParse->nAlloc = nNew;
184287 pParse->aNode = pNew;
184288 assert( pParse->nNode<pParse->nAlloc );
184294 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
184295 ** new node, or -1 if a memory allocation fails.
184300 u32 n, /* Content size or sub-node count */
184304 if( pParse->nNode>=pParse->nAlloc ){
184307 p = &pParse->aNode[pParse->nNode];
184308 p->eType = (u8)eType;
184309 p->jnFlags = 0;
184310 p->n = n;
184311 p->u.zJContent = zContent;
184312 return pParse->nNode++;
184325 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
184328 ** Return negative for a syntax error. Special cases: return -2 if the
184329 ** first non-whitespace character is '}' and return -3 if the first
184330 ** non-whitespace character is ']'.
184338 const char *z = pParse->zJson;
184343 if( iThis<0 ) return -1;
184346 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
184349 pParse->iDepth--;
184350 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
184351 return -1;
184353 if( pParse->oom ) return -1;
184354 pNode = &pParse->aNode[pParse->nNode-1];
184355 if( pNode->eType!=JSON_STRING ) return -1;
184356 pNode->jnFlags |= JNODE_LABEL;
184359 if( z[j]!=':' ) return -1;
184362 pParse->iDepth--;
184363 if( x<0 ) return -1;
184368 if( c!='}' ) return -1;
184371 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
184376 if( iThis<0 ) return -1;
184379 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
184381 pParse->iDepth--;
184383 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
184384 return -1;
184390 if( c!=']' ) return -1;
184393 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
184403 return -1;
184412 return -1;
184419 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
184420 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
184437 }else if( c=='-' || (c>='0' && c<='9') ){
184441 assert( '-' < '0' );
184443 j = c=='-' ? i+1 : i;
184444 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
184451 if( z[j-1]=='-' ) return -1;
184452 if( seenDP ) return -1;
184457 if( z[j-1]<'0' ) return -1;
184458 if( seenE ) return -1;
184461 if( c=='+' || c=='-' ){
184465 if( c<'0' || c>'9' ) return -1;
184470 if( z[j-1]<'0' ) return -1;
184472 j - i, &z[i]);
184475 return -2; /* End of {...} */
184477 return -3; /* End of [...] */
184481 return -1; /* Syntax error */
184486 ** Parse a complete JSON string. Return 0 on success or non-zero if there
184500 pParse->zJson = zJson;
184502 if( pParse->oom ) i = -1;
184504 assert( pParse->iDepth==0 );
184506 if( zJson[i] ) i = -1;
184510 if( pParse->oom ){
184513 sqlite3_result_error(pCtx, "malformed JSON", -1);
184526 JsonNode *pNode = &pParse->aNode[i];
184528 pParse->aUp[i] = iParent;
184529 switch( pNode->eType ){
184531 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
184537 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
184538 pParse->aUp[i+j] = i;
184554 assert( pParse->aUp==0 );
184555 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
184557 pParse->oom = 1;
184567 #define JSON_CACHE_ID (-429938)
184586 if( p && p->nJson==nJson && memcmp(p->zJson,zJson,nJson)==0 ){
184587 p->nErr = 0;
184596 p->zJson = (char*)&p[1];
184597 memcpy((char*)p->zJson, zJson, nJson+1);
184598 if( jsonParse(p, pCtx, p->zJson) ){
184602 p->nJson = nJson;
184612 if( pNode->jnFlags & JNODE_RAW ){
184613 if( pNode->n!=nKey ) return 0;
184614 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
184616 if( pNode->n!=nKey+2 ) return 0;
184617 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
184642 JsonNode *pRoot = &pParse->aNode[iRoot];
184645 if( pRoot->eType!=JSON_OBJECT ) return 0;
184650 nKey = i-1;
184668 while( j<=pRoot->n ){
184675 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
184676 iRoot += pRoot->u.iAppend;
184677 pRoot = &pParse->aNode[iRoot];
184687 if( pParse->oom ) return 0;
184689 pRoot = &pParse->aNode[iRoot];
184690 pRoot->u.iAppend = iStart - iRoot;
184691 pRoot->jnFlags |= JNODE_APPEND;
184692 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
184697 if( pRoot->eType!=JSON_ARRAY ) return 0;
184701 i = i*10 + zPath[j] - '0';
184711 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
184712 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
184715 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
184716 iRoot += pRoot->u.iAppend;
184717 pRoot = &pParse->aNode[iRoot];
184720 if( j<=pRoot->n ){
184728 if( pParse->oom ) return 0;
184730 pRoot = &pParse->aNode[iRoot];
184731 pRoot->u.iAppend = iStart - iRoot;
184732 pRoot->jnFlags |= JNODE_APPEND;
184755 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
184764 if( pParse->oom ) return 0;
184765 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
184781 ** pParse->nErr counter.
184806 pParse->nErr++;
184810 sqlite3_result_error(pCtx, zMsg, -1);
184829 sqlite3_result_error(pCtx, zMsg, -1);
184838 assert( pNode->eType==JSON_OBJECT );
184839 n = pNode->n;
184861 ** well-formed.
184868 JsonString s; /* Output string - not real JSON */
184884 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
184918 ** double-quotes around strings and returning the unquoted string "null"
184964 ** Return the number of elements in the top-level JSON array.
184965 ** Return 0 if the input is not a well-formed JSON array.
184979 assert( p->nNode );
184984 pNode = p->aNode;
184989 if( pNode->eType==JSON_ARRAY ){
184990 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
184991 for(i=1; i<=pNode->n; n++){
185025 if( p->nErr ) break;
185055 if( pPatch->eType!=JSON_OBJECT ){
185058 assert( iTarget>=0 && iTarget<pParse->nNode );
185059 pTarget = &pParse->aNode[iTarget];
185060 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
185061 if( pTarget->eType!=JSON_OBJECT ){
185066 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
185074 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
185085 pTarget = &pParse->aNode[iTarget];
185094 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
185099 if( pParse->oom ) return 0;
185101 pTarget = &pParse->aNode[iTarget];
185102 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
185103 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
185105 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
185106 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
185161 "of arguments", -1);
185168 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
185209 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
185222 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
185246 pNode->jnFlags |= (u8)JNODE_REPLACE;
185247 pNode->u.iReplace = i + 1;
185269 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
185300 pNode->jnFlags |= (u8)JNODE_REPLACE;
185301 pNode->u.iReplace = i + 1;
185317 ** Return the top-level "type" of a JSON string. Throw an error if
185318 ** either the JSON or PATH inputs are not well-formed.
185338 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
185346 ** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
185383 if( pStr->zBuf==0 ){
185388 pStr->pCtx = ctx;
185397 pStr->pCtx = ctx;
185399 if( pStr->bErr ){
185400 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
185401 assert( pStr->bStatic );
185403 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
185404 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
185405 pStr->bStatic = 1;
185429 if( pStr->zBuf==0 ){
185434 pStr->pCtx = ctx;
185448 if( pStr->bErr ){
185449 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
185450 assert( pStr->bStatic );
185452 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
185453 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
185454 pStr->bStatic = 1;
185469 sqlite3_vtab_cursor base; /* Base class - must be first */
185474 u8 eType; /* Type of top-level element */
185533 *ppCursor = &pCur->base;
185542 pCur->bRecursive = 1;
185550 sqlite3_free(p->zJson);
185551 sqlite3_free(p->zRoot);
185552 jsonParseReset(&p->sParse);
185553 p->iRowid = 0;
185554 p->i = 0;
185555 p->iEnd = 0;
185556 p->eType = 0;
185557 p->zJson = 0;
185558 p->zRoot = 0;
185573 return p->i >= p->iEnd;
185579 if( p->bRecursive ){
185580 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
185581 p->i++;
185582 p->iRowid++;
185583 if( p->i<p->iEnd ){
185584 u32 iUp = p->sParse.aUp[p->i];
185585 JsonNode *pUp = &p->sParse.aNode[iUp];
185586 p->eType = pUp->eType;
185587 if( pUp->eType==JSON_ARRAY ){
185588 if( iUp==p->i-1 ){
185589 pUp->u.iKey = 0;
185591 pUp->u.iKey++;
185596 switch( p->eType ){
185598 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
185599 p->iRowid++;
185603 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
185604 p->iRowid++;
185608 p->i = p->iEnd;
185629 iUp = p->sParse.aUp[i];
185631 pNode = &p->sParse.aNode[i];
185632 pUp = &p->sParse.aNode[iUp];
185633 if( pUp->eType==JSON_ARRAY ){
185634 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
185636 assert( pUp->eType==JSON_OBJECT );
185637 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
185638 assert( pNode->eType==JSON_STRING );
185639 assert( pNode->jnFlags & JNODE_LABEL );
185640 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
185651 JsonNode *pThis = &p->sParse.aNode[p->i];
185654 if( p->i==0 ) break;
185655 if( p->eType==JSON_OBJECT ){
185657 }else if( p->eType==JSON_ARRAY ){
185659 if( p->bRecursive ){
185660 if( p->iRowid==0 ) break;
185661 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
185663 iKey = p->iRowid;
185670 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
185675 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
185676 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
185680 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
185681 if( pThis->eType>=JSON_ARRAY ) break;
185687 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
185691 if( p->i>p->iBegin && p->bRecursive ){
185692 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
185699 if( p->bRecursive ){
185700 jsonEachComputePath(p, &x, p->i);
185702 if( p->zRoot ){
185703 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
185707 if( p->eType==JSON_ARRAY ){
185708 jsonPrintf(30, &x, "[%d]", p->iRowid);
185710 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
185717 if( p->bRecursive ){
185720 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
185728 const char *zRoot = p->zRoot;
185730 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
185735 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
185745 *pRowid = p->iRowid;
185759 int jsonIdx = -1;
185760 int rootIdx = -1;
185764 pConstraint = pIdxInfo->aConstraint;
185765 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
185766 if( pConstraint->usable==0 ) continue;
185767 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
185768 switch( pConstraint->iColumn ){
185771 default: /* no-op */ break;
185775 pIdxInfo->idxNum = 0;
185776 pIdxInfo->estimatedCost = 1e99;
185778 pIdxInfo->estimatedCost = 1.0;
185779 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
185780 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
185782 pIdxInfo->idxNum = 1;
185784 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
185785 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
185786 pIdxInfo->idxNum = 3;
185810 p->zJson = sqlite3_malloc64( n+1 );
185811 if( p->zJson==0 ) return SQLITE_NOMEM;
185812 memcpy(p->zJson, z, (size_t)n+1);
185813 if( jsonParse(&p->sParse, 0, p->zJson) ){
185815 if( p->sParse.oom==0 ){
185816 sqlite3_free(cur->pVtab->zErrMsg);
185817 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
185818 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
185822 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
185832 p->zRoot = sqlite3_malloc64( n+1 );
185833 if( p->zRoot==0 ) return SQLITE_NOMEM;
185834 memcpy(p->zRoot, zRoot, (size_t)n+1);
185838 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
185841 sqlite3_free(cur->pVtab->zErrMsg);
185842 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
185844 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
185849 pNode = p->sParse.aNode;
185851 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
185852 p->eType = pNode->eType;
185853 if( p->eType>=JSON_ARRAY ){
185854 pNode->u.iKey = 0;
185855 p->iEnd = p->i + pNode->n + 1;
185856 if( p->bRecursive ){
185857 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
185858 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
185859 p->i--;
185862 p->i++;
185865 p->iEnd = p->i+1;
185879 jsonEachOpenEach, /* xOpen - open a cursor */
185880 jsonEachClose, /* xClose - close a cursor */
185881 jsonEachFilter, /* xFilter - configure scan constraints */
185882 jsonEachNext, /* xNext - advance a cursor */
185883 jsonEachEof, /* xEof - check for end of scan */
185884 jsonEachColumn, /* xColumn - read data */
185885 jsonEachRowid, /* xRowid - read data */
185906 jsonEachOpenTree, /* xOpen - open a cursor */
185907 jsonEachClose, /* xClose - close a cursor */
185908 jsonEachFilter, /* xFilter - configure scan constraints */
185909 jsonEachNext, /* xNext - advance a cursor */
185910 jsonEachEof, /* xEof - check for end of scan */
185911 jsonEachColumn, /* xColumn - read data */
185912 jsonEachRowid, /* xRowid - read data */
185942 { "json_array", -1, 0, jsonArrayFunc },
185945 { "json_extract", -1, 0, jsonExtractFunc },
185946 { "json_insert", -1, 0, jsonSetFunc },
185947 { "json_object", -1, 0, jsonObjectFunc },
185950 { "json_remove", -1, 0, jsonRemoveFunc },
185951 { "json_replace", -1, 0, jsonReplaceFunc },
185952 { "json_set", -1, 1, jsonSetFunc },
186093 ** non-negative but less than the number of columns in the table, return
186108 ** non-negative but less than the number of columns in the table, set
186122 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
186154 ** set to -1.
186224 ** If the bClear argument is non-zero, then the auxiliary data is cleared
186246 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
186248 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
186254 ** modify this structure directly - it should only be used as shown above
186262 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
186276 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
186278 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
186287 ** xPhraseFirstColumn() set iCol to -1).
186356 ** The second and third arguments are an array of nul-terminated strings
186374 ** by argument pText. pText may or may not be nul-terminated. The first
186382 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
186387 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
186391 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
186396 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
186429 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
186463 ** still appears to contain just two phrases - "(first OR 1st)"
186474 ** when tokenizing query text (it should not - to do would be
186521 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
186654 ** Constants for the largest and smallest possible 64-bit signed integers.
186657 # define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
186662 ** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
186739 ** The minimum number of segments that an auto-merge operation should
186741 ** compile time default. Zero disables auto-merge altogether.
186747 ** the string "rowid" otherwise. This text is not quoted - if it is
186754 ** decent error message if it encounters a file-format version it does
186764 ** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
186791 int nHashSize; /* Bytes of memory for in-memory hash */
186795 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
186799 int bPrefixIndex; /* True to use prefix-indexes */
186877 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
186878 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
186881 /* Write and decode big-endian 32-bit integer values */
186928 /* Bucket of terms object used by the integrity-check in offsets=0 mode. */
186953 #define sqlite3Fts5IterEof(x) ((x)->bEof)
186988 ** Argument p points to a buffer containing utf-8 text that is n bytes in
187041 int iCol, /* Column token appears in (-ve -> delete) */
187057 ** Flush any data stored in the in-memory hash tables to the database.
187063 ** Discard any data stored in the in-memory hash tables. Do not write it
187065 ** table may have changed on disk. So any in-memory caches of %_data
187077 ** Functions called by the storage module as part of integrity-check.
187120 iOff--; \
187164 int iCol, /* Column token appears in (-ve -> delete) */
187189 const char **pzTerm, /* OUT: term (nul-terminated) */
187307 ** The fts5_expr.c API above this point is used by the other hand-written
187422 ** 2000-05-29
187436 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
187437 ** interstitial "-" characters) contained in this template is changed into
187452 ** Disable all error recovery processing in the parser push-down
187477 ** "lemon" is run with the "-m" command-line option.
187484 ** that represent terminal and non-terminal symbols.
187490 ** (also known as: "terminal symbols") have fall-back
187494 ** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
187499 ** value associated with a terminal or non-terminal
187502 ** Each non-terminal can have a different minor type.
187522 ** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
187523 ** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
187526 ** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
187552 #define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse = fts5yypParser->pParse
187553 #define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse = pParse
187567 /* Define the fts5yytestcase() macro to be a no-op if is not already defined
187592 ** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
187601 ** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
187614 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
187626 ** shifting non-terminals after a reduce.
187669 #define fts5YY_REDUCE_MIN (-18)
187672 /* 0 */ -17, -9, -1, 7, 15, 23, 42, -18, -18, 55,
187673 /* 10 */ 72, -4, -4, 4, -4, 10, 25, 29,
187681 /********** End of lemon-generated parsing tables *****************************/
187719 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
187722 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
187732 int fts5yyhwm; /* High-water mark of the stack */
187862 newSize = p->fts5yystksz*2 + 100;
187863 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
187864 if( p->fts5yystack==&p->fts5yystk0 ){
187866 if( pNew ) pNew[0] = p->fts5yystk0;
187868 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
187871 p->fts5yystack = pNew;
187872 p->fts5yytos = &p->fts5yystack[idx];
187876 fts5yyTracePrompt, p->fts5yystksz, newSize);
187879 p->fts5yystksz = newSize;
187899 pParser->fts5yyhwm = 0;
187902 pParser->fts5yytos = NULL;
187903 pParser->fts5yystack = NULL;
187904 pParser->fts5yystksz = 0;
187906 pParser->fts5yystack = &pParser->fts5yystk0;
187907 pParser->fts5yystksz = 1;
187911 pParser->fts5yyerrcnt = -1;
187913 pParser->fts5yytos = pParser->fts5yystack;
187914 pParser->fts5yystack[0].stateno = 0;
187915 pParser->fts5yystack[0].major = 0;
187917 pParser->fts5yystackEnd = &pParser->fts5yystack[fts5YYSTACKDEPTH-1];
187958 ** terminal or non-terminal is destroyed. This can happen
187977 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy54));
187983 sqlite3_free((fts5yypminor->fts5yy43));
187989 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy14));
187994 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy11));
188010 assert( pParser->fts5yytos!=0 );
188011 assert( pParser->fts5yytos > pParser->fts5yystack );
188012 fts5yytos = pParser->fts5yytos--;
188017 fts5yyTokenName[fts5yytos->major]);
188020 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
188028 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
188030 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
188061 return pParser->fts5yyhwm;
188067 ** is in state X and has a lookahead token Y. In a well-tested
188104 ** look-ahead token iLookAhead.
188108 fts5YYCODETYPE iLookAhead /* The look-ahead token */
188111 int stateno = pParser->fts5yytos->stateno;
188142 int j = i - iLookAhead + fts5YYWILDCARD;
188171 ** Find the appropriate action for a parser given the non-terminal
188172 ** look-ahead token iLookAhead.
188176 fts5YYCODETYPE iLookAhead /* The look-ahead token */
188210 …while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser…
188228 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
188232 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
188233 fts5yyNewState - fts5YY_MIN_REDUCE);
188251 fts5yypParser->fts5yytos++;
188253 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
188254 fts5yypParser->fts5yyhwm++;
188255 …assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
188259 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
188260 fts5yypParser->fts5yytos--;
188265 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
188267 fts5yypParser->fts5yytos--;
188274 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
188276 fts5yytos = fts5yypParser->fts5yytos;
188277 fts5yytos->stateno = (fts5YYACTIONTYPE)fts5yyNewState;
188278 fts5yytos->major = (fts5YYCODETYPE)fts5yyMajor;
188279 fts5yytos->minor.fts5yy0 = fts5yyMinor;
188287 fts5YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
188290 { 17, -1 }, /* (0) input ::= expr */
188291 { 21, -4 }, /* (1) colset ::= MINUS LCP colsetlist RCP */
188292 { 21, -3 }, /* (2) colset ::= LCP colsetlist RCP */
188293 { 21, -1 }, /* (3) colset ::= STRING */
188294 { 21, -2 }, /* (4) colset ::= MINUS STRING */
188295 { 22, -2 }, /* (5) colsetlist ::= colsetlist STRING */
188296 { 22, -1 }, /* (6) colsetlist ::= STRING */
188297 { 18, -3 }, /* (7) expr ::= expr AND expr */
188298 { 18, -3 }, /* (8) expr ::= expr OR expr */
188299 { 18, -3 }, /* (9) expr ::= expr NOT expr */
188300 { 18, -5 }, /* (10) expr ::= colset COLON LP expr RP */
188301 { 18, -3 }, /* (11) expr ::= LP expr RP */
188302 { 18, -1 }, /* (12) expr ::= exprlist */
188303 { 20, -1 }, /* (13) exprlist ::= cnearset */
188304 { 20, -2 }, /* (14) exprlist ::= exprlist cnearset */
188305 { 19, -1 }, /* (15) cnearset ::= nearset */
188306 { 19, -3 }, /* (16) cnearset ::= colset COLON nearset */
188307 { 23, -1 }, /* (17) nearset ::= phrase */
188308 { 23, -2 }, /* (18) nearset ::= CARET phrase */
188309 { 23, -5 }, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
188310 { 24, -1 }, /* (20) nearphrases ::= phrase */
188311 { 24, -2 }, /* (21) nearphrases ::= nearphrases phrase */
188313 { 26, -2 }, /* (23) neardist_opt ::= COMMA STRING */
188314 { 25, -4 }, /* (24) phrase ::= phrase PLUS STRING star_opt */
188315 { 25, -2 }, /* (25) phrase ::= STRING star_opt */
188316 { 27, -1 }, /* (26) star_opt ::= STAR */
188329 ** only called from one place, optimizing compilers will in-line it, which
188345 fts5yymsp = fts5yypParser->fts5yytos;
188365 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
188366 fts5yypParser->fts5yyhwm++;
188367 … assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
188371 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
188376 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
188381 fts5yymsp = fts5yypParser->fts5yytos;
188402 … fts5yymsp[-3].minor.fts5yy43 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy43);
188406 { fts5yymsp[-2].minor.fts5yy43 = fts5yymsp[-1].minor.fts5yy43; }
188416 fts5yymsp[-1].minor.fts5yy43 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
188417 fts5yymsp[-1].minor.fts5yy43 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy43);
188422 …fts5yylhsminor.fts5yy43 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy43, &fts5yymsp[…
188423 fts5yymsp[-1].minor.fts5yy43 = fts5yylhsminor.fts5yy43;
188433 …fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy54, fts…
188435 fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188439 …fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy54, fts5…
188441 fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188445 …fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy54, fts…
188447 fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188451 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy54, fts5yymsp[-4].minor.fts5yy43);
188452 fts5yylhsminor.fts5yy54 = fts5yymsp[-1].minor.fts5yy54;
188454 fts5yymsp[-4].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188457 {fts5yymsp[-2].minor.fts5yy54 = fts5yymsp[-1].minor.fts5yy54;}
188466 …fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy54, fts5yy…
188468 fts5yymsp[-1].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188479 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy54, fts5yymsp[-2].minor.fts5yy43);
188481 fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
188490 fts5yymsp[-1].minor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy11);
188495 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
188496 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy14, &fts5yymsp[-1].minor.fts5yy0);
188497 fts5yylhsminor.fts5yy14 = fts5yymsp[-2].minor.fts5yy14;
188499 fts5yymsp[-4].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
188509 …fts5yylhsminor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy14, fts5yymsp[…
188511 fts5yymsp[-1].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
188517 { fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
188521 …fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy11, &fts5yymsp[-1…
188523 fts5yymsp[-3].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
188527 …fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[…
188529 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
188554 fts5yypParser->fts5yytos = fts5yymsp;
188555 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
188556 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
188573 …while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser…
188615 fts5yypParser->fts5yyerrcnt = -1;
188617 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
188638 ** <li> An option argument of a grammar-specified type.
188661 assert( fts5yypParser->fts5yytos!=0 );
188669 int stateno = fts5yypParser->fts5yytos->stateno;
188675 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],stateno-fts5YY_MIN_REDUCE);
188683 fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,fts5yyminor);
188687 fts5yypParser->fts5yyerrcnt--;
188691 fts5yypParser->fts5yytos--;
188725 if( fts5yypParser->fts5yyerrcnt<0 ){
188728 fts5yymx = fts5yypParser->fts5yytos->major;
188739 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
188742 fts5yypParser->fts5yytos->stateno,
188747 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
188751 fts5yypParser->fts5yyerrcnt = -1;
188758 fts5yypParser->fts5yyerrcnt = 3;
188782 if( fts5yypParser->fts5yyerrcnt<=0 ){
188785 fts5yypParser->fts5yyerrcnt = 3;
188790 fts5yypParser->fts5yyerrcnt = -1;
188796 }while( fts5yymajor!=fts5YYNOCODE && fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
188802 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
188803 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
188867 pIter->iStart = -1;
188868 pIter->iEnd = -1;
188870 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
188872 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
188874 if( ic==pIter->iCol ){
188875 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
188876 if( pIter->iStart<0 ){
188877 pIter->iStart = io;
188878 pIter->iEnd = iEnd;
188879 }else if( io<=pIter->iEnd ){
188880 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
188885 pIter->iInst++;
188905 pIter->pApi = pApi;
188906 pIter->pFts = pFts;
188907 pIter->iCol = iCol;
188908 rc = pApi->xInstCount(pFts, &pIter->nInst);
188927 int iRangeEnd; /* If non-zero, last token to include */
188937 ** Append text to the HighlightContext output string - p->zOut. Argument