Lines Matching +full:clang +full:- +full:5

7 ** of 5% or more are commonly seen when SQLite is compiled as a single
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__,
800 ** NO_TEST - The branches on this line are not
805 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
809 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
813 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
818 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
892 ** 2015-03-02
930 ** large file support, or if the OS is windows, these should be no-ops.
936 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
945 ** on 2008-11-28.) These days, all Linux kernels support large files, so
962 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
966 ** version numbers in clang are for "marketing" only and are inconsistent
967 ** and unreliable. Fortunately, all versions of clang also recognize the
969 ** so the GCC_VERSION macro will be set to a correct non-zero value even
970 ** when compiling with clang.
1001 ** some MinGW-specific macros). When compiling for MinGW, either the
1031 ** 2001-09-15
1042 ** presents to client programs. If a C-function, structure, datatype,
1053 ** The official C-language API documentation for SQLite is derived
1100 ** These no-op macros are used in front of interfaces to mark those
1102 ** should not use deprecated interfaces - they are supported for backwards
1126 ** CAPI3REF: Compile-Time Library Version Numbers
1142 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
1144 ** a string which identifies a particular check-in of SQLite
1146 ** string contains the date and time of the check-in (UTC) and a SHA1
1147 ** or SHA3-256 hash of the entire source tree.
1155 #define SQLITE_SOURCE_ID "2017-08-01 13:24:15 9501e22dfeebdcefa783575e47c60b514d7c2e0cad73b2a4…
1158 ** CAPI3REF: Run-Time Library Version Numbers
1193 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1202 ** returning the N-th compile time option string. ^If N is out of range,
1224 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
1242 ** This interface only reports on the compile-time mutex setting
1248 ** sqlite3_threadsafe() function shows only the compile-time setting of
1249 ** thread safety, not any run-time changes to that setting made by
1274 ** CAPI3REF: 64-Bit Integer Types
1277 ** Because there is no cross-platform way to specify 64-bit integer types
1278 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
1285 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1308 ** substitute integer for floating-point.
1354 ** argument is a harmless no-op.
1367 ** CAPI3REF: One-Step Query Execution Interface
1375 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1376 ** semicolon-separate SQL statements passed into its 2nd argument,
1388 ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
1390 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
1392 ** on error message strings returned through the 5th parameter of
1394 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
1395 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
1398 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1448 /* beginning-of-error-codes */
1453 #define SQLITE_BUSY 5 /* The database file is locked */
1479 /* end-of-error-codes */
1487 ** these result codes are too coarse-grained. They do not provide as
1502 #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
1543 #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1610 ** read-only media and cannot be changed even by processes with
1662 ** (Third-party VFS implementations might also make the distinction
1697 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1730 ** locking strategy (for example to use dot-file locks), to inquire
1776 ** fails to zero-fill short reads might seem to work. However,
1777 ** failure to zero-fill short reads will eventually lead to
1823 ** compile-time option is used.
1837 ** point to an integer (type int) containing the new chunk-size to use
1839 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1850 ** the [rollback journal] or the [write-ahead log]) for a particular database
1863 ** this file-control is NULL. However, if the database file is being synced
1864 ** as part of a multi-database commit, the argument points to a nul-terminated
1865 ** string containing the transactions master-journal file name. VFSes that
1882 ** anti-virus programs. By default, the windows VFS will retry file read,
1907 ** WAL mode. If the integer is -1, then it is overwritten with the current
1912 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1916 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1917 ** mode. If the integer is -1, then it is overwritten with the current
1918 ** zero-damage mode setting.
1929 ** final bottom-level VFS are written into memory obtained from
1933 ** all file-control actions, there is no guarantee that this will actually
1935 ** pointer in case this file-control is not implemented. This file-control
1939 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1943 ** to a pointer to the top-level VFS.)^
1945 ** upper-most shim only.
1962 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1964 ** of the result string if the string is non-NULL.
1970 ** it is able to override built-in [PRAGMA] statements.
1974 ** file-control may be invoked by SQLite on the database file handle
1976 ** to the connections busy-handler callback. The argument is of type (void **)
1977 ** - an array of two (void *) values. The first (void *) actually points
1979 ** busy-handler, this function should be invoked with the second (void *) in
1980 ** the array as the only argument. If it returns non-zero, then the operation
1985 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1995 ** maximum number of bytes that will be used for memory-mapped I/O.
2001 ** file-control is used internally to implement [PRAGMA mmap_size].
2007 ** The argument is a zero-terminated string. Higher layers in the
2009 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2034 ** Applications should <em>not</em> use this file-control.
2050 #define SQLITE_FCNTL_SIZE_HINT 5
2126 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
2144 ** consist of a single "-" character followed by no more than
2145 ** 11 alphanumeric and/or "-" characters.
2160 ** If xOpen() opens a file read-only then it sets *pOutFlags to
2180 ** the open of a journal file a no-op. Writes to this journal would
2181 ** also be no-ops, and any attempt to read the journal would return
2183 ** file will be doing page-aligned sector reads and writes in a random
2235 ** of good-quality randomness into zOut. The return value is
2243 ** a 24-hour day).
2269 void *pAppData; /* Pointer to application-specific data */
2379 ** are harmless no-ops.)^
2384 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2407 ** compile-time option, then the automatic calls to sqlite3_initialize()
2416 ** The sqlite3_os_init() routine does operating-system specific
2432 ** (using the [SQLITE_OS_OTHER=1] compile-time
2434 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2463 ** implementation of an application-defined [sqlite3_os_init()].
2473 ** then this routine returns a non-zero [error code].
2487 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2500 ** and low-level memory allocation routines.
2512 ** Note that SQLite comes with several [built-in memory allocators]
2517 ** memory allocator that simulates memory out-of-memory conditions in
2582 ** non-zero [error code] if a discontinued or unsupported configuration option
2588 ** [threading mode] to Single-thread. In other words, it disables
2591 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2593 ** value of Single-thread and so [sqlite3_config()] will return
2599 ** [threading mode] to Multi-thread. In other words, it disables
2603 ** are enabled so that SQLite will be safe to use in a multi-threaded
2606 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2607 ** it is not possible to set the Multi-thread [threading mode] and
2622 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2631 ** alternative low-level memory allocation routines to be used in place of
2649 ** disabled, the following SQLite interfaces become non-operational:
2664 ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
2668 ** The first argument must be a pointer to an 8-byte aligned buffer
2680 ** fragmentation in low-memory embedded systems.
2687 ** This configuration option is a no-op if an application-define page
2690 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2698 ** argument must be either a NULL pointer or a pointer to an 8-byte
2705 ** ^If pMem is NULL and N is non-zero, then each database connection
2708 ** of -1024*N bytes if N is negative, . ^If additional
2722 ** An 8-byte aligned pointer to the memory,
2729 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2732 ** for the minimum allocation size are 2**5 through 2**8.</dd>
2737 ** The argument specifies alternative low-level mutex routines to be used
2741 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2754 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2787 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2789 ** passed through as the first parameter to the application-defined logger
2797 ** In a multi-threaded application, the application-defined logger
2802 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2819 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2820 ** if that compile-time option is omitted.
2830 ** They are retained for backwards compatibility but are now no-ops.
2836 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2852 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2859 ** compile-time maximum mmap size set by the
2860 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2862 ** changed to its compile-time default.
2867 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2868 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2884 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2893 ** becomes the [statement journal] spill-to-disk threshold.
2896 ** Or if the threshold is -1, statement journals are always held
2902 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2909 #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
2918 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2919 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2942 ** non-zero [error code] if a discontinued or unsupported configuration option
2957 ** must be aligned to an 8-byte boundary. ^If the second argument to
2989 ** <dd> ^This option is used to enable or disable the two-argument
2991 ** [FTS3] full-text search engine extension.
3005 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3007 ** When the first argument to this interface is 1, then only the C-API is
3009 ** this interface is 0, then both the C-API and the SQL function are disabled.
3010 ** If the first argument is -1, then no changes are made to state of either the
3011 ** C-API or the SQL function.
3033 ** is an integer - non-zero to disable checkpoints-on-close, or zero (the
3035 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
3036 ** have been disabled - 0 if they are not disabled, 1 if they are.
3077 ** has a unique 64-bit signed
3155 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3177 ** any changes performed by sub-triggers, as the sqlite3_changes()
3178 ** value will be saved and restored after each sub-trigger has run.)^
3222 ** CAPI3REF: Interrupt A Long-Running Query
3228 ** or Ctrl-C where the user wants a long query operation to halt
3253 ** SQL statements is a no-op and has no effect on SQL statements
3261 ** These routines are useful during command-line input to determine if the
3267 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3282 ** then the return value from sqlite3_complete16() will be non-zero
3285 ** The input to [sqlite3_complete()] must be a zero-terminated
3286 ** UTF-8 string.
3288 ** The input to [sqlite3_complete16()] must be a zero-terminated
3289 ** UTF-16 string in native byte order.
3296 ** KEYWORDS: {busy-handler callback} {busy handler}
3318 ** ^If the callback returns non-zero, then another attempt
3394 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3396 ** to zero-terminated strings that contain the names of the columns.
3398 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3410 ** -----------------------
3426 ** azResult&#91;5] = "28";
3432 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3464 ** These routines are work-alikes of the "printf()" family of functions
3467 ** plus some additional non-standard formats, detailed below.
3469 ** C-library standards are omitted from this implementation.
3492 ** guarantees that the buffer is always zero-terminated. ^The first
3495 ** written will be n-1 characters.
3504 ** ^(The %q option works like %s in that it substitutes a nul-terminated
3556 ** be contained within double-quotes instead of single quotes, and it
3557 ** escapes the double-quote character instead of the single-quote
3575 ** does not include operating-system specific VFS implementation. The
3586 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3587 ** of a signed 32-bit integer.
3592 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3597 ** might result if sqlite3_free() is called with a non-NULL pointer that
3617 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3618 ** of a 32-bit signed integer.
3633 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3637 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
3639 ** is no longer provided. Only built-in memory allocators can be used.
3643 ** filenames between the UTF-8 encoding used by SQLite
3670 ** routines, which form the built-in memory allocation subsystem.
3675 ** value of [sqlite3_memory_used()] since the high-water mark
3682 ** ^The memory high-water mark is reset to the current value of
3685 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3692 ** CAPI3REF: Pseudo-Random Number Generator
3694 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3697 ** the build-in random() and randomblob() SQL functions. This interface allows
3708 ** non-NULL P then the pseudo-randomness is generated
3715 ** CAPI3REF: Compile-Time Authorization Callbacks
3746 ** to the callback are either NULL pointers or zero-terminated strings
3774 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3793 ** statement might be re-prepared during [sqlite3_step()] due to a
3836 ** codes is used as the second parameter. ^(The 5th parameter to the
3839 ** is the name of the inner-most trigger or view that is responsible for
3841 ** top-level SQL code.
3848 #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
3891 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3895 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3897 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3902 ** the original statement text and an estimate of wall-clock time
3922 ** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
3944 ** interface by using the X argument when X begins with "--" and invoking
3951 ** X argument points to a 64-bit integer which is the estimated of
3981 ** M argument should be the bitwise OR-ed combination of
4031 ** ^If the progress callback returns non-zero, the operation is
4048 ** filename argument. ^The filename argument is interpreted as UTF-8 for
4049 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4060 ** ^The default encoding will be UTF-8 for databases created using
4062 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
4078 ** <dd>The database is opened in read-only mode. If the database does not
4098 ** opens in the multi-thread [threading mode] as long as the single-thread
4099 ** mode has not been set at compile-time or start-time. ^If the
4101 ** in the serialized [threading mode] unless single-thread was
4102 ** previously selected at compile-time or start-time.
4114 ** ^If the filename is ":memory:", then a private, temporary in-memory database
4115 ** is created for the connection. ^This in-memory database will vanish when
4123 ** on-disk database will be created. ^This private database will be
4133 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4156 ** SQLite and its built-in [VFSes] interpret the
4171 ** ^If "ro" is specified, then the database is opened for read-only
4174 ** "rw", then the database is opened for read-write (but not create)
4178 ** set to "memory" then a pure [in-memory database] that never reads
4205 ** read-only media. ^When immutable is set, SQLite assumes that the
4207 ** privilege, and so the database is opened read-only and all locking
4222 ** <table border="1" align=center cellpadding=5>
4232 ** <tr><td style="white-space:nowrap">
4236 ** necessary - space characters can be used literally
4239 ** Open file "data.db" in the current directory for read-only access.
4240 ** Regardless of whether or not shared-cache mode is enabled by
4242 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4243 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4244 ** that uses dot-files in place of posix advisory locking.
4251 ** percent sign - "%" - followed by exactly two hexadecimal digits
4253 ** URI filename are interpreted, they are encoded using UTF-8 and all
4255 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4259 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4261 ** characters must be converted to UTF-8 prior to passing them into
4271 const char *filename, /* Database filename (UTF-8) */
4275 const void *filename, /* Database filename (UTF-16) */
4279 const char *filename, /* Database filename (UTF-8) */
4306 ** case or if the value begins with a non-zero number. The
4314 ** 64-bit signed integer and returns that integer, or D if P does not
4344 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4345 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4351 ** ^The sqlite3_errstr() interface returns the English-language text
4352 ** that describes the [result code], as UTF-8.
4388 ** The life-cycle of a prepared statement object usually goes like this:
4403 ** CAPI3REF: Run-time Limits
4416 ** set at compile-time by a C preprocessor macro called
4425 ** simply invoke this interface with the third parameter set to -1.
4427 ** Run-time limits are intended for use in applications that manage
4440 ** New run-time limit categories may be added in future releases.
4445 ** CAPI3REF: Run-Time Limit Categories
4449 ** that can be lowered at run-time using [sqlite3_limit()].
4505 #define SQLITE_LIMIT_VDBE_OP 5
4543 ** To execute an SQL statement, it must first be compiled into a byte-code
4552 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4553 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4554 ** as a convenience. The UTF-16 interfaces work by converting the
4555 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4562 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4564 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4565 ** and sqlite3_prepare16_v3() use UTF-16.
4571 ** If the caller knows that the supplied string is nul-terminated, then
4574 ** the nul-terminator.
4624 ** ^The specific value of WHERE-clause [parameter] might influence the
4625 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4627 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4639 const char *zSql, /* SQL statement, UTF-8 encoded */
4646 const char *zSql, /* SQL statement, UTF-8 encoded */
4653 const char *zSql, /* SQL statement, UTF-8 encoded */
4661 const void *zSql, /* SQL statement, UTF-16 encoded */
4668 const void *zSql, /* SQL statement, UTF-16 encoded */
4675 const void *zSql, /* SQL statement, UTF-16 encoded */
4686 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4690 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4704 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4705 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4721 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4725 ** Note that [application-defined SQL functions] or
4729 ** change the database file through side-effects:
4757 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4794 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4805 ** implementation of [application-defined SQL functions] are protected.
4820 ** is always first parameter to [application-defined SQL functions].
4821 ** The application-defined SQL function implementation will pass this
4879 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4913 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4915 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4987 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
4998 ** ^If the value N is out of range or if the N-th parameter is
5000 ** always in UTF-8 encoding even if the named parameter was
5001 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5018 ** name must be given in UTF-8 even if the original statement
5019 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5060 ** interface returns a pointer to a zero-terminated UTF-8 string
5061 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5062 ** UTF-16 string. ^The first parameter is the [prepared statement]
5073 ** (for example during a conversion from UTF-8 to UTF-16) then a
5092 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5101 ** ^The names returned are the original un-aliased names of the
5107 ** ^The left-most column is column 0 for these routines.
5116 ** UTF-16 encoded strings and the other functions return UTF-8.
5119 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5147 ** ^The returned string is always UTF-8 encoded.
5160 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5210 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5236 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5266 ** will return non-zero if previous call to [sqlite3_step](P) returned
5268 ** where it always returns zero since each step of that multi-step
5282 ** <li> 64-bit signed integer
5283 ** <li> 64-bit IEEE floating point number
5299 #define SQLITE_NULL 5
5316 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5317 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5318 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5319 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5324 ** or a UTF-8 TEXT result in bytes
5326 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5378 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5380 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5381 ** the string to UTF-8 and then returns the number of bytes.
5383 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5387 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5389 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5390 ** the string to UTF-16 and then returns the number of bytes.
5392 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5403 ** even empty strings, are always zero-terminated. ^The return
5404 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5416 ** [application-defined SQL functions] or [virtual tables], not within
5417 ** top-level application code.
5435 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5456 ** sqlite3_column_text16() is called. A zero-terminator might
5458 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5460 ** to UTF-16.</li>
5461 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5463 ** to UTF-8.</li>
5466 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5531 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5546 ** object back to its initial state, ready to be re-executed.
5571 ** KEYWORDS: {application-defined SQL function}
5572 ** KEYWORDS: {application-defined SQL functions}
5585 ** connection then application-defined SQL functions must be added
5589 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5590 ** representation, exclusive of the zero-terminator. ^Note that the name
5591 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5597 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5600 ** parameter is less than -1 or greater than 127 then the behavior is
5619 ** deterministic. The built-in [random()] SQL function is an example of a
5628 ** pointers to C-language functions that implement the SQL function or
5650 ** SQL function is used. ^A function implementation with a non-negative
5659 ** ^Built-in functions may be overloaded by new application-defined functions.
5661 ** ^An application-defined function is permitted to call other
5704 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5705 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5706 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5708 #define SQLITE_ANY 5 /* Deprecated */
5749 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5750 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5752 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5753 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5755 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5756 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5759 ** or a UTF-8 TEXT in bytes
5761 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5774 ** [application-defined SQL functions] and [virtual tables].
5784 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5785 ** in the native byte-order of the host machine. ^The
5787 ** extract UTF-16 strings as big-endian and little-endian respectively.
5843 ** an [application-defined SQL function] argument V. The subtype
5862 ** then sqlite3_value_free(V) is a harmless no-op.
5917 ** the pointer that was the pUserData parameter (the 5th parameter)
5923 ** the application-defined function is running.
5943 ** These functions may be used by (non-aggregate) SQL functions to
5947 ** of where this might be useful is in a regular-expression matching
5956 ** value to the application-defined function. ^N is zero for the left-most
5961 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
5962 ** argument of the application-defined function. ^Subsequent
5986 ** function parameters that are compile-time constants, including literal
5989 ** The value of the N parameter to these interfaces should be non-negative.
6016 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6032 ** an application-defined function to be the BLOB whose content is pointed
6037 ** interfaces set the result of the application-defined function to be
6041 ** an application-defined function to be a floating point value specified
6049 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6050 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
6055 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6073 ** of the application-defined function to be the 32-bit signed integer
6076 ** of the application-defined function to be the 64-bit signed integer
6080 ** of the application-defined function to be NULL.
6084 ** set the return value of the application-defined function to be
6085 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6086 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6088 ** application-defined function to be a text string in an encoding
6097 ** is non-negative, then as many bytes (not characters) of the text
6098 ** pointed to by the 2nd parameter are taken as the application-defined
6099 ** function result. If the 3rd parameter is non-negative, then it
6106 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6120 ** the application-defined function to be a copy of the
6131 ** also associates the host-language pointer P or type T with that
6133 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6141 ** than the one containing the application-defined function that received
6173 ** the result from the [application-defined SQL function] with
6189 ** ^The name of the collation is a UTF-8 string
6191 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6253 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6296 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6297 ** the names are passed as UTF-16 in machine native byte order.
6298 ** ^A call to either function replaces the existing collation-needed callback.
6402 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6444 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6445 ** &nbsp; TemporaryFolder->Path->Data();
6448 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6461 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6493 ** CAPI3REF: Test For Auto-Commit Mode
6497 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6501 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6503 ** If certain kinds of errors occur on a statement within a multi-statement
6536 ** connection D, or if database N is a temporary or in-memory database, then
6547 ** CAPI3REF: Determine if a database is read-only
6551 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6585 ** ^If the callback on a commit hook function returns non-zero,
6607 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6609 ** hook returning non-zero, just as it would be with any other rollback.
6704 ** shared cache mode should be enabled per-database connection via
6708 ** 32-bit integer is atomic.
6710 ** See Also: [SQLite Shared-Cache Mode]
6718 ** of heap memory by deallocating non-essential memory allocations
6720 ** pages to improve performance is an example of non-essential memory.
6723 ** ^The sqlite3_release_memory() routine is a no-op returning zero
6737 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6772 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
6773 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
6784 ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
6816 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
6820 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
6837 ** ^Metadata is returned by writing to the memory locations passed as the 5th
6845 ** <tr><td> 5th <td> const char* <td> Data type
6887 int *pAutoinc /* OUTPUT: True if column is auto-increment */
6899 ** with various operating-system specific extensions added.
6908 ** X is consists of the lower-case equivalent of all ASCII alphabetic
6947 ** [extension loading] while evaluating user-entered SQL, the following API
6955 ** ^This interface enables or disables both the C-API
6958 ** to enable or disable only the C-API.)^
6998 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7027 ** The interface to the virtual-table mechanism is currently considered
7031 ** When the virtual-table mechanism stabilizes, we will declare the
7102 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7114 ** expr on the right-hand side can be evaluated (and thus the constraint
7129 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7135 ** non-zero.
7139 ** the right-hand side of the corresponding aConstraint[] is evaluated
7140 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7163 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7194 int iColumn; /* Column constrained. -1 for ROWID */
7197 int iTermOffset; /* Used internally - xBestIndex should ignore */
7362 ** The interface to the virtual-table mechanism defined above (back up
7367 ** When the virtual-table mechanism stabilizes, we will declare the
7404 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7406 ** read-only access.
7442 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7458 ** and the built-in [zeroblob] SQL function may be used to create a
7459 ** zero-filled blob to read or write using the incremental-blob interface.
7489 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7511 ** ^If the blob handle being closed was opened for read-write access, and if
7512 ** the database is in auto-commit mode and there are no other open read-write
7520 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7547 ** caller-supplied buffer. N bytes of data are copied into buffer Z
7576 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
7625 ** ^Names are zero-terminated UTF-8 strings.
7656 ** is selected automatically at compile-time. The following
7667 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7672 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7709 ** cases where it really needs one. If a faster non-recursive mutex
7756 ** behave as no-ops.
7769 ** An instance of this structure defines the low-level routines
7819 ** xMutexInit() must be no-ops.
7865 ** the routine should return 1. This seems counter-intuitive since
7869 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
7885 ** next. Applications that override the built-in mutex logic must be
7894 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
7901 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
7912 ** ^If the [threading mode] is Single-thread or Multi-thread then this
7918 ** CAPI3REF: Low-Level Control Of Database Files
7937 ** case is a short-circuit path which does not actually invoke the
7982 #define SQLITE_TESTCTRL_FIRST 5
7983 #define SQLITE_TESTCTRL_PRNG_SAVE 5
8025 ** SQLITE_OK on success and a non-zero [error code] on failure.
8028 ** be represented by a 32-bit integer, then the values returned by
8046 ** These integer constants designate various run-time status parameters
8055 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
8130 #define SQLITE_STATUS_MALLOC_SIZE 5
8155 ** non-zero [error code] on failure.
8171 ** The [sqlite3_db_status()] interface will return a non-zero error code
8181 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8189 ** Only the high-water value is meaningful;
8197 ** Only the high-water value is meaningful;
8220 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8268 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
8321 ** A non-zero value in this counter may indicate an opportunity to
8327 ** A non-zero value in this counter may indicate an opportunity to
8363 #define SQLITE_STMTSTATUS_REPREPARE 5
8414 ** The built-in page cache is recommended for most uses.
8429 ** built-in default page cache is used instead of the application defined
8461 ** false if it is used for an in-memory database. The cache implementation
8472 ** suggested maximum cache-size (number of pages stored by) the cache
8518 ** as its second argument. If the third parameter, discard, is non-zero,
8552 ** is not obligated to free any memory, but well-behaved implementations should
8611 ** for copying in-memory databases to or from persistent files.
8617 ** ^The source database is read-locked only while it is being read;
8651 ** there is already a read or read-write transaction open on the
8683 ** <li> the destination database was opened read-only, or
8684 ** <li> the destination database is using write-ahead-log journaling
8686 ** <li> the destination database is an in-memory database and the
8690 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
8691 ** the [sqlite3_busy_handler | busy-handler function]
8693 ** busy-handler returns non-zero before the lock is available, then
8714 ** sqlite3_backup_step(), the source database may be modified mid-way
8731 ** active write-transaction on the destination database is rolled back.
8738 ** ^If an out-of-memory condition or IO error occurred during any prior
8807 ** ^When running in shared-cache mode, a database operation may fail with
8808 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
8809 ** individual tables within the shared-cache cannot be obtained. See
8810 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
8814 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
8818 ** ^Shared-cache locks are released when a database connection concludes
8822 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
8832 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
8838 ** ^If the blocked connection is attempting to obtain a write-lock on a
8839 ** shared-cache table, and more than one other connection currently holds
8840 ** a read-lock on the same table, then SQLite arbitrarily selects one of
8843 ** ^(There may be at most one unlock-notify callback registered by a
8845 ** blocked connection already has a registered unlock-notify callback,
8848 ** unlock-notify callback is canceled. ^The blocked connections
8849 ** unlock-notify callback may also be canceled by closing the blocked
8852 ** The unlock-notify callback is not reentrant. If an application invokes
8853 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
8861 ** When an unlock-notify callback is registered, the application provides a
8865 ** an unlock-notify callback is a pointer to an array of void* pointers,
8869 ** more than one blocked connection that has registered for an unlock-notify
8879 ** Assuming that after registering for an unlock-notify callback a
8890 ** unlock-notify callback is registered. The system is said to be in
8891 ** a deadlocked state if connection A has registered for an unlock-notify
8893 ** B has itself registered for an unlock-notify callback when connection
8896 ** registered for an unlock-notify callback on the conclusion of connection
8908 ** sqlite3_unlock_notify() results in the unlock-notify callback being
8909 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
8929 ** and extensions to compare the contents of two buffers containing UTF-8
8930 ** strings in a case-independent fashion, using the same definition of "case
8946 ** Note that this routine returns zero on a match and non-zero if the strings
8963 ** insensitive - equivalent upper and lower case ASCII characters match
8969 ** Note that this routine returns zero on a match and non-zero if the strings
8993 ** a fixed-length buffer on the stack. If the log message is longer than
9000 ** CAPI3REF: Write-Ahead Log Commit Hook
9007 ** the associated write-lock on the database released)^, so the implementation
9013 ** ^The third parameter is the name of the database that was written to -
9014 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9015 ** is the number of pages currently in the write-ahead log file,
9026 ** A single database handle may have at most a single write-ahead log callback
9028 ** previously registered write-ahead log callback. ^Note that the
9040 ** CAPI3REF: Configure an auto-checkpoint
9047 ** more frames in the [write-ahead log] file. ^Passing zero or
9062 ** ^Every new [database connection] defaults to having the auto-checkpoint
9078 ** [write-ahead log] for database X on [database connection] D to be
9079 ** transferred into the database file and for the write-ahead log to
9105 ** in the log were checkpointed. ^The [busy-handler callback]
9112 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9121 ** [busy-handler callback])
9134 ** the log file or to -1 if the checkpoint could not run because
9138 ** was called) or to -1 if the checkpoint could not run due to an error or
9146 ** busy-handler configured, it will not be invoked in this case.
9150 ** obtained immediately, and a busy-handler is configured, it is invoked and
9151 ** the writer lock retried until either the busy-handler returns 0 or the lock
9152 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9153 ** database readers as described above. ^If the busy-handler returns 0 before
9156 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9172 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9241 ** If X is non-zero, then the virtual table implementation guarantees
9291 #define SQLITE_REPLACE 5
9308 ** set to the total number of times that the X-th loop has run.</dd>
9312 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
9317 ** iteration of the X-th loop. If the query planner's estimates was accurate,
9324 ** to a zero-terminated UTF-8 string containing the name of the index or table
9325 ** used for the X-th loop.
9329 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9330 ** description for the X-th loop.
9334 ** "select-id" for the X-th loop. The select-id identifies which query or
9335 ** subquery the loop is part of. The main query has a select-id of zero.
9336 ** The select-id is the same value as is output in the first column
9345 #define SQLITE_SCANSTAT_SELECTID 5
9358 ** compile-time option.
9366 ** Loops are numbered starting from zero. ^If idx is out of range - less than
9368 ** the statement - a non-zero value is returned and the variable that pOut
9373 ** as if the loop did not exist - it returns non-zero and leave the variable
9386 ** CAPI3REF: Zero Scan-Status Counters
9391 ** This API is only available if the library is built with pre-processor
9397 ** CAPI3REF: Flush caches to disk mid-transaction
9399 ** ^If a write-transaction is open on [database connection] D when the
9401 ** pages in the pager-cache that are not currently in use are written out
9405 ** interface flushes caches for all schemas - "main", "temp", and
9410 ** immediately and there is a busy-handler callback configured, it is invoked
9418 ** example an IO error or out-of-memory condition), then processing is
9429 ** CAPI3REF: The pre-update hook.
9432 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9504 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
9505 ** triggers; or 2 for changes resulting from triggers called by top-level
9531 ** CAPI3REF: Low-level system error code
9535 ** The return value is OS-dependent. For example, on unix systems, after
9581 ** If there is not already a read-transaction open on schema S when
9613 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9651 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9668 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9759 /* The double-precision datatype used by RTree depends on the
9760 ** SQLITE_RTREE_INT_ONLY compile-time option.
9770 ** R-Tree geometry query as follows:
9795 ** Register a 2nd-generation geometry callback named zScore that can be
9796 ** used as part of an R-Tree geometry query as follows:
9814 ** Note that the first 5 fields of this structure are identical to
9895 ** is not possible for an application to register a pre-update hook on a
9898 ** which a pre-update hook is already defined. The results of attempting
9932 ** disabled - it does not. A newly created session object is enabled.
9939 ** no-op, and may be used to query the current state of the session.
10041 ** database row along with the updated values for each updated non-primary-key
10050 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10052 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
10065 ** are sorted in the same order in which they were attached (or auto-attached)
10079 ** recorded once - the first time a row with said primary key is inserted,
10086 ** The session object therefore accumulates two types of records - those
10104 ** found and one or more of the non-primary key fields have been
10121 ** This may appear to have some counter-intuitive effects if a single row
10142 ** does not have a primary key, this function is a no-op (but does not return
10162 ** used to update the table in database zFrom (call this the "from-table")
10164 ** object (call this the "to-table"). Specifically:
10167 ** <li> For each row (primary key) that exists in the to-table but not in
10168 ** the from-table, an INSERT record is added to the session object.
10170 ** <li> For each row (primary key) that exists in the to-table but not in
10171 ** the from-table, a DELETE record is added to the session object.
10174 ** different non-PK values in each, an UPDATE record is added to the
10218 ** Because the non-primary key "old.*" fields are omitted, no
10237 ** Return non-zero if no changes to attached tables have been recorded by
10245 ** are restored. However, if this function returns non-zero, then it is
10296 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10319 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10325 ** nul-terminated utf-8 encoded string containing the name of the table
10328 ** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
10376 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
10384 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10414 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10447 ** conflict-handler callback by [sqlite3changeset_apply()] with either
10458 ** "conflicting row" associated with the current conflict-handler callback
10494 ** function with an iterator passed to a conflict-handler by
10502 ** returned. This is to allow the following pattern (pseudo-code):
10646 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
10647 ** <tr><th style="white-space:pre">Existing Change </th>
10648 ** <th style="white-space:pre">New Change </th>
10693 ** returned. Or, if an out-of-memory condition occurs during processing, this
10749 ** the table. Otherwise, if the return value is non-zero or the xFilter
10786 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
10790 ** returned by each invocation of the conflict-handler function. Refer to
10799 ** stored in all non-primary key columns also match the values stored in
10803 ** the non-primary key fields contains a value different from the original
10804 ** row value stored in the changeset, the conflict-handler function is
10807 ** only the values of those non-primary key fields are compared against
10808 ** the current database contents - any trailing database table columns
10812 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10817 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
10836 ** This includes the case where the INSERT operation is re-attempted because
10844 ** stored in all modified non-primary key columns also match the values
10848 ** the modified non-primary key fields contains a value different from an
10849 ** original row value stored in the changeset, the conflict-handler function
10851 ** UPDATE changes only contain values for non-primary key fields that are
10853 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
10856 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
10860 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
10897 ** Values that may be passed as the second argument to a conflict-handler.
10904 ** (non primary-key) fields modified by the update do not contain the
10953 #define SQLITE_CHANGESET_FOREIGN_KEY 5
10994 ** corresponding non-streaming API functions:
10996 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
10997 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
11006 ** Non-streaming functions that accept changesets (or patchsets) as input
11011 ** low-memory environment is required to handle very large changesets, the
11206 ** non-negative but less than the number of columns in the table, return
11221 ** non-negative but less than the number of columns in the table, set
11235 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
11267 ** set to -1.
11337 ** If the bClear argument is non-zero, then the auxiliary data is cleared
11359 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
11361 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
11367 ** modify this structure directly - it should only be used as shown above
11375 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
11389 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
11391 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
11400 ** xPhraseFirstColumn() set iCol to -1).
11469 ** The second and third arguments are an array of nul-terminated strings
11487 ** by argument pText. pText may or may not be nul-terminated. The first
11495 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
11500 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
11504 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
11509 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
11519 ** size of the token in bytes. The 4th and 5th arguments are the byte offsets
11542 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
11576 ** still appears to contain just two phrases - "(first OR 1st)"
11587 ** when tokenizing query text (it should not - to do would be
11597 ** synonyms would call xToken() 5 times, as follows:
11601 ** xToken(pCtx, 0, "won", 3, 2, 5);
11602 ** xToken(pCtx, 0, "first", 5, 6, 11);
11604 ** xToken(pCtx, 0, "place", 5, 12, 17);
11634 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
11730 ** autoconf-based build
11758 ** The hard limit is the ability of a 32-bit signed integer
11759 ** to count the size: 2^31-1 or 2147483647.
11777 ** tell you that in a well-normalized database, you usually should
11839 ** The suggested maximum number of in-memory pages to use for
11842 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
11844 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
11845 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
11848 # define SQLITE_DEFAULT_CACHE_SIZE -2000
11862 ** counted using a signed 8-bit integer which has a maximum value of 127
11878 ** imposed by the use of 16-bit offsets within each page.
11884 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
11885 ** compiled with the default page-size limit will not be able to rollback
11908 ** device characteristics (sector-size and atomic write() support),
11925 ** This value can be lowered (or raised) at run-time using that the
11956 #pragma warn -rch /* unreachable code */
11957 #pragma warn -ccc /* Condition is always true or false */
11958 #pragma warn -aus /* Assigned value is never used */
11959 #pragma warn -csu /* Comparing signed and unsigned */
11960 #pragma warn -spa /* Suspicious pointer arithmetic */
11984 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
11994 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
11998 #else /* Generates a warning - but it always works */
12038 ** level of threadsafety. 2 means the library is multithreaded - multiple
12046 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
12054 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
12060 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
12067 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
12096 # error "Two or more of the following compile-time configuration options\
12134 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
12202 ** of SQLite to unexpected behavior - to make the code "self-healing"
12208 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
12272 ** Return true (non-zero) if the input is an integer that is too large
12273 ** to fit in 32-bits. This macro is used inside of various testcase()
12274 ** macros to verify that we have tested SQLite for large-file support.
12301 ** This is the header file for the generic hash-table implementation
12312 ** The internals of this structure are intended to be opaque -- client
12319 ** All elements of the hash table are on a single doubly-linked list.
12323 ** the global doubly-linked list. The contents of the bucket are the
12324 ** element pointed to plus the next _ht.count-1 elements in the list.
12343 ** structure. All elements are stored on a single doubly-linked list.
12374 #define sqliteHashFirst(H) ((H)->first)
12375 #define sqliteHashNext(E) ((E)->next)
12376 #define sqliteHashData(E) ((E)->data)
12377 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
12378 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
12383 /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
12395 #define TK_BEGIN 5
12580 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
12585 ** substitute integer for floating-point
12616 ** the VDBE-level file format changes. The following macros define the
12627 ** changed at run-time using a pragma.
12635 ** on the command-line
12665 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
12667 ** The default value of "20" was choosen to minimize the run-time of the
12668 ** speedtest1 test program with options: --shrink-memory --reprepare
12679 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
12710 ** types can be conveniently redefined at compile-type. Like this:
12712 ** cc '-DUINTPTR_TYPE=long long int' ...
12752 typedef sqlite_int64 i64; /* 8-byte signed integer */
12753 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
12754 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
12755 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
12756 typedef INT16_TYPE i16; /* 2-byte signed integer */
12757 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
12758 typedef INT8_TYPE i8; /* 1-byte signed integer */
12766 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
12771 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
12772 ** can be used at compile-time if desired.
12775 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
12777 typedef u32 tRowcnt; /* 32-bit is the default */
12781 ** Estimated quantities used for query planning are stored as 16-bit
12783 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
12792 ** 1 -> 0 20 -> 43 10000 -> 132
12793 ** 2 -> 10 25 -> 46 25000 -> 146
12794 ** 3 -> 16 100 -> 66 1000000 -> 199
12795 ** 4 -> 20 1000 -> 99 1048576 -> 200
12796 ** 10 -> 33 1024 -> 100 4294967296 -> 320
12801 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
12842 ** and whether or not that determination is run-time or compile-time.
12844 ** For best performance, an attempt is made to guess at the byte-order
12845 ** using C-preprocessor macros. If that is unsuccessful, or if
12846 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
12847 ** at run-time.
12881 ** Constants for the largest and smallest possible 64-bit signed integers.
12882 ** These macros are designed to work correctly on both 32-bit and 64-bit
12886 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
12890 ** to force 8-byte alignment on 64-bit architectures.
12900 ** Assert that the pointer X is aligned to an 8-byte boundary. This
12905 ** underlying malloc() implementation might return us 4-byte aligned
12906 ** pointers. In that case, only verify 4-byte alignment.
12909 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
12911 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
12923 ** Default maximum size of memory used by memory-mapped I/O in the VFS
12943 ** default MMAP_SIZE is specified at compile-time, make sure that it does
12979 ** An instance of the following structure is used to store the busy-handler
12984 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
13003 ** The root-page of the master database table.
13021 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
13040 ** directly, we use its constant as a key to lookup the run-time allocated
13042 ** for the run-time allocated buffer.
13045 ** macros become no-ops and have zero performance impact.
13155 ** This header file defines the interface that the sqlite B-Tree file
13168 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
13175 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
13176 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
13189 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
13204 #define BTREE_MEMORY 2 /* This is an in-memory DB */
13205 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
13251 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
13254 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
13257 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
13258 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
13278 ** For example, the free-page-count field is located at byte offset 36 of
13279 ** the database file header. The incr-vacuum-flag field is located at
13283 ** It is a read-only number computed by the pager. But we merge it with
13292 #define BTREE_TEXT_ENCODING 5
13296 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
13310 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
13311 ** column of the b-tree of the cursor. The Expr tree will not contain
13312 ** any function calls nor subqueries nor references to b-trees other than
13315 ** The design of the _RANGE hint is aid b-tree implementations that try
13316 ** to prefetch content from remote machines - to provide those
13341 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
13346 ** For read-only cursors the wrFlag argument is always zero. For read-write
13357 ** by this, the native b-tree engine of SQLite, but it is available to
13359 ** b-tree system. For alternative storage engines in which a delete of
13363 ** and DELETE operations as no-ops, and any READ operation against a
13366 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
13372 int wrFlag, /* 1 for writing. 0 for read-only */
13476 ** Enter and Leave procedures no-ops.
13563 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
13595 int iSrcLine; /* Source-code line that generated this opcode */
13602 ** A sub-routine used to implement a trigger program.
13605 VdbeOp *aOp; /* Array of opcodes for sub-program */
13611 SubProgram *pNext; /* Next sub-program already visited */
13631 #define P4_STATIC (-1) /* Pointer to a static string */
13632 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
13633 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
13634 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
13635 #define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
13636 #define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
13638 #define P4_FREE_IF_LE (-7)
13639 #define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
13640 #define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
13641 #define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
13642 #define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
13643 #define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
13644 #define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
13645 #define P4_REAL (-13) /* P4 is a 64-bit floating point value */
13646 #define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
13647 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
13648 #define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
13657 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
13666 # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
13681 #define ADDR(X) (-1-(X))
13696 #define OP_NextIfOpen 5
13737 #define OP_IfPos 46 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
13738 #define OP_IfNotZero 47 /* synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
13739 #define OP_DecrJumpZero 48 /* synopsis: if (--r[P1])==0 goto P2 */
13780 #define OP_Subtract 89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
13785 #define OP_Compare 94 /* synopsis: r[P1@P3] <-> r[P2@P3] */
13838 #define OP_OffsetLimit 147 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
13906 ** Additional non-public SQLITE_PREPARE_* flags
13996 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
14034 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
14035 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
14090 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14113 ** is devoted to storing a master journal name - there are no more pages to
14117 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14125 #define PAGER_MEMORY 0x0002 /* In-memory database */
14130 #define PAGER_LOCKINGMODE_QUERY -1
14141 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14146 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14147 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14153 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14175 ** that make up the Pager sub-system API. See source code comments for
14389 /* Modify the page-size after the cache has been created. */
14411 /* Change a page number. Used by incr-vacuum. */
14453 /* Set and get the suggested cache-size for the specified pager-cache.
14456 ** the total number of pages cached by purgeable pager-caches to the sum
14457 ** of the suggested cache-sizes.
14464 /* Set or get the suggested spill-size for the specified pager-cache.
14466 ** The spill-size is the minimum number of pages in cache before the cache
14510 ** This header file (together with is companion C source-code file
14514 ** This header file is #include-ed by sqliteInt.h and thus ends up
14522 ** necessary pre-processor macros for it.
14538 ** This file contains pre-processor directives related to operating system
14588 ** a no-op
14608 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
14610 ** 2006-10-31: The default prefix used to be "sqlite_". But then
14611 ** Mcafee started using SQLite in their anti-virus product and it
14657 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
14763 ** sqlite3_malloc() to obtain space for the file-handle structure.
14802 ** at start-time.
14804 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
14807 ** start-time.
14809 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
14811 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
14828 ** If this is a no-op implementation, implement everything as macros.
14848 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
14868 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
14869 ** In other words, the zero-based numbers are used for all external interfaces
14870 ** and the one-based values are used internally.
14898 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
14927 ** Db.pSchema->flags field.
14929 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
14930 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
14931 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
14932 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
14935 ** Allowed values for the DB.pSchema->flags field.
14955 ** Lookaside malloc is a set of fixed-size buffers that can be used
14990 ** A hash table for built-in function definitions. (Application-defined
15068 u8 autoCommit; /* The auto-commit flag. */
15072 u8 dfltLockMode; /* Default locking-mode for attached dbs */
15078 u8 skipBtreeMutex; /* True if no shared-cache backends */
15115 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
15152 int nSavepoint; /* Number of non-transaction savepoints */
15153 int nStatement; /* Number of nested statement-transactions */
15182 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
15183 #define ENC(db) ((db)->enc)
15206 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
15223 #define SQLITE_PreferBuiltin 0x02000000 /* Preference to built-in funcs */
15252 #define SQLITE_CountOfView 0x1000 /* The count-of-view optimization */
15259 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
15260 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
15266 #define ConstFactorOk(P) ((P)->okConstFactor)
15282 ** structure. For global built-in functions (ex: substr(), max(), count())
15284 ** For per-connection application-defined functions, a pointer to this
15285 ** structure is held in the db->aHash hash table.
15287 ** The u.pHash field is used by the global built-ins. The u.pDestructor
15288 ** field is used by per-connection app-def functions.
15291 i8 nArg; /* Number of arguments. -1 means unlimited */
15295 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
15305 ** This structure encapsulates a user-function destructor callback (as
15339 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
15342 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
15343 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
15344 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
15345 #define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
15346 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
15350 ** single query - might change over time */
15351 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
15361 ** as the user-data (sqlite3_user_data()) for the function. If
15371 ** a single query. The iArg is ignored. The user-data is always set
15377 ** ignored and the user-data for these functions is set to an
15378 ** arbitrary non-NULL pointer. The bNC parameter is not used.
15390 ** available as the function user-data (sqlite3_user_data()). The
15429 char *zName; /* Savepoint name (nul-terminated) */
15487 char *zName; /* Name of the collating sequence, UTF-8 encoded */
15499 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
15553 ** database connections, even when the rest of the in-memory database
15563 ** database schema are initially stored in a linked-list pointed to by
15570 ** When an in-memory Table object is deleted (for example when the
15614 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
15637 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
15638 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
15642 #define TF_Readonly 0x0001 /* Read-only system table */
15648 #define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */
15649 #define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */
15660 # define IsVirtual(X) ((X)->nModuleArg)
15667 ** only works for non-virtual tables (ordinary tables and views) and is
15672 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15673 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15675 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
15684 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
15685 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
15700 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
15703 ** from-table == child-table
15704 ** to-table == parent-table
15707 ** which is attached to the from-table. The to-table need not exist when
15708 ** the from-table is created. The existence of the to-table is not checked.
15722 /* EV: R-30323-21917 */
15762 #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
15783 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
15805 ** pKeyInfo->nField.
15809 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
15813 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
15814 ** multiple entries in the b-tree with the same key (when only looking
15815 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
15820 ** get and equal results when comparing this structure to a b-tree record.
15824 ** b-tree.
15827 KeyInfo *pKeyInfo; /* Collation and sort-order information */
15861 ** algorithm to employ whenever an attempt is made to insert a non-unique
15869 ** number (it cannot - the database page is not allocated until the VDBE
15894 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
15900 IndexSample *aSample; /* Samples of the left-most key */
15901 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
15902 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
15914 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
15917 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
15922 #define XN_ROWID (-1) /* Indexed column is the rowid */
15923 #define XN_EXPR (-2) /* Indexed column is an expression */
15947 const char *z; /* Text of the token. Not NULL-terminated! */
15970 int sortingIdxPTab; /* Cursor number of pseudo-table */
15996 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
15997 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
15998 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
16003 ** the option is available (at compile-time).
16018 ** to represent the greater-than-or-equal-to operator in the expression
16032 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
16051 ** subquery gives a constant result, then iTable is -1. If the subquery
16056 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
16080 int iValue; /* Non-negative integer value if EP_IntValue */
16105 ** TK_TRIGGER: 1 -> new, 0 -> old
16108 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
16111 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
16157 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
16158 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
16159 #define ExprSetProperty(E,P) (E)->flags|=(P)
16160 #define ExprClearProperty(E,P) (E)->flags&=~(P)
16164 ** processes but is a no-op for delivery.
16167 # define ExprSetVVAProperty(E,P) (E)->flags|=(P)
16185 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
16195 ** By default the Expr.zSpan field holds a human-readable description of
16248 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
16261 ** Changing this from a 64-bit to a 32-bit type limits the number of
16281 #define ALLBITS ((Bitmask)-1)
16299 ** In the colUsed field, the high-order bit (bit 63) is set if the table
16300 ** contains more than 63 columns and the 64-th or later column is used.
16314 int regResult; /* Registers holding results of a co-routine */
16319 unsigned isTabFunc :1; /* True if table-valued-function syntax */
16320 unsigned isCorrelated :1; /* True if sub-query is correlated */
16321 unsigned viaCoroutine :1; /* Implemented as a co-routine */
16325 u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
16333 ExprList *pFuncArg; /* Arguments to table-valued-function */
16358 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
16361 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
16364 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
16393 ** NameContexts can be nested. When resolving names, the inner-most
16408 ExprList *pEList; /* Optional list of result-set columns */
16437 ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
16510 ** identified by pDest->iSDParm.
16512 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
16514 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
16519 ** the side-effects of functions.
16529 ** in register pDest->iSDParm then abandon the rest
16533 ** row of result as the key in table pDest->iSDParm.
16534 ** Apply the affinity pDest->affSdst before storing
16537 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
16543 ** SRT_Coroutine Generate a co-routine that returns a new row of
16545 ** of the co-routine is stored in register pDest->iSDParm
16546 ** and the result row is stored in pDest->nDest registers
16547 ** starting with pDest->iSdst.
16549 ** SRT_Table Store results in temporary table pDest->iSDParm.
16555 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
16556 ** But also use temporary table pDest->iSDParm+1 as
16560 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
16564 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
16566 ** index at pDest->iSDParm+1 hold all prior stores.
16572 #define SRT_Fifo 5 /* Store result as data with an automatic rowid */
16578 #define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
16604 ** the code generator needs. We have to keep per-table autoincrement
16630 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
16678 ** The nTableLock and aTableLock variables are only used if the shared-cache
16679 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
16680 ** used to store the set of table-locks required by the statement being
16707 ** of the base register during check-constraint eval */
16718 int nMaxArg; /* Max args passed to user function by sub-program */
16725 TableLock *aTableLock; /* Required table locks for shared-cache mode */
16771 int nHeight; /* Expression tree height of current sub-select */
16781 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
16797 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
16806 #define IN_DECLARE_VTAB (pParse->declareVtab)
16830 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
16833 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
16837 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
16869 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
16870 the <column-list> is stored here */
16889 * that is a part of a trigger-program.
16894 * the first step of the trigger-program.
16901 * orconf -> stores the ON CONFLICT algorithm
16902 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
16904 * zTarget -> Dequoted name of the table to insert into.
16905 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
16907 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
16908 * statement, then this stores the column-names to be
16912 * zTarget -> Dequoted name of the table to delete from.
16913 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
16917 * zTarget -> Dequoted name of the table to update.
16918 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
16920 * pExprList -> A list of the columns to update and the expressions to update
16934 TriggerStep *pNext; /* Next in the link-list */
16935 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
16949 const char *zType; /* Type of the container - used for error messages */
16950 const Token *pName; /* Name of the container - used for error messages */
16969 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
16973 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
16997 int bUseCis; /* Use covering indices for full-scans */
16999 int neverCorrupt; /* Database is always well-formed */
17002 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
17003 sqlite3_mem_methods m; /* Low-level memory allocation interface */
17004 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
17005 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
17018 int sharedCacheEnabled; /* true if shared-cache mode enabled */
17020 /* The above might be initialized to non-zero. The following need to always
17051 ** the assert is only valid on a well-formed database. Instead of:
17063 ** things that are always true for well-formed databases.
17068 ** Context pointer passed down through the tree-walk.
17105 ** Return code from the parse-tree walking primitives and their
17130 ** data structures on sqlite3DebugPrintf() using a tree-like view.
17139 ** Assuming zIn points to the first byte of a UTF-8 character,
17140 ** advance zIn to point to the first byte of the next UTF-8 character.
17151 ** routines that report the line-number on which the error originated
17192 ** The ctype.h header is needed for non-ASCII systems. It is also
17402 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
17637 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
17638 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
17698 ** Routines to read and write variable-length integers. These used to
17865 ** The interface to the LEMON-generated parser
17923 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
17961 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
17963 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
17965 ** provided (enforcement of FK constraints requires the triggers sub-system).
18014 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
18017 #define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
18063 ** sqlite3IoTrace is a pointer to a printf-like routine used to
18096 ** passed back to non-lookaside free() routines. Asserts such as the
18097 ** example above are placed on the non-lookaside free() routines to verify
18100 ** All of this is no-op for a production build. It only comes into
18101 ** play when the SQLITE_MEMDEBUG compile-time option is used.
18108 # define sqlite3MemdebugSetType(X,Y) /* no-op */
18159 /* An array to map all upper-case characters into their corresponding
18160 ** lower-case character.
18162 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
18168 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18185 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
18190 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
18205 ** The following 256 byte lookup table is used to support SQLites built-in
18218 ** case. i.e. if the character is a lower-case ASCII character.
18219 ** If x is a lower-case ASCII character, then its upper-case equivalent
18220 ** is (x - 0x20). Therefore toupper() can be implemented as:
18227 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
18229 ** non-ASCII UTF character. Hence the test for whether or not a character is
18239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
18246 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
18272 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
18276 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
18277 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
18279 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
18294 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
18295 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
18296 ** that compile-time option is omitted.
18312 ** before 3.12.0). -1 means always keep the entire statement journal in
18322 ** The default lookaside-configuration, the format "SZ,N". SZ is the
18324 ** and N is the number of slots. The lookaside-configuration can be
18325 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
18326 ** or at run-time for an individual database connection using
18391 ** Hash table for global functions - functions common to all
18393 ** read-only.
18408 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
18528 ** * A b-tree cursor
18529 ** - In the main database or in an ephemeral database
18530 ** - On either an index or a table
18533 ** * A one-row "pseudotable" stored in a single register
18538 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
18547 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
18600 ** When a sub-program is executed (OP_Program), a structure of this type
18603 ** values stored in the Vdbe struct. When the sub-program is finished,
18605 ** restoring the state of the VM to as it was before the sub-program
18639 int nDbChange; /* Value of db->nChange */
18669 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
18725 /* Return TRUE if Mem X contains dynamically allocated content - anything
18729 (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
18735 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
18742 #define memIsValid(M) ((M)->flags & MEM_Undefined)==0
18781 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
18796 int iSelectID; /* The "Select-ID" for this loop */
18839 i64 startTime; /* Time when query started - used for profiling */
18850 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
18852 bft changeCntOn:1; /* True to update the change-counter */
18857 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
18866 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
19012 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
19057 ** we have to locate the state vector at run-time. In the more common
19087 ** The value of N is added to the current status value and the high-water
19091 ** mark is unchanged. N must be non-negative for StatusDown().
19111 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
19185 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
19186 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
19194 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
19197 *pCurrent = db->lookaside.nOut; in sqlite3_db_status()
19198 *pHighwater = db->lookaside.mxOut; in sqlite3_db_status()
19200 db->lookaside.mxOut = db->lookaside.nOut; in sqlite3_db_status()
19211 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
19212 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
19214 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
19216 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
19231 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19232 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
19258 db->pnBytesFreed = &nByte; in sqlite3_db_status()
19259 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19260 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
19265 pSchema->tblHash.count in sqlite3_db_status()
19266 + pSchema->trigHash.count in sqlite3_db_status()
19267 + pSchema->idxHash.count in sqlite3_db_status()
19268 + pSchema->fkeyHash.count in sqlite3_db_status()
19270 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
19271 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
19272 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
19273 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
19275 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
19278 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
19283 db->pnBytesFreed = 0; in sqlite3_db_status()
19300 db->pnBytesFreed = &nByte; in sqlite3_db_status()
19301 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ in sqlite3_db_status()
19305 db->pnBytesFreed = 0; in sqlite3_db_status()
19307 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
19326 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
19327 if( db->aDb[i].pBt ){ in sqlite3_db_status()
19328 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
19332 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
19333 /* IMP: R-54100-20147 */ in sqlite3_db_status()
19334 /* IMP: R-29431-39229 */ in sqlite3_db_status()
19339 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
19344 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
19345 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
19353 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
19373 ** There is only one exported symbol in this file - the function
19382 ** 1970-01-01 00:00:00 is JD 2440587.5
19383 ** 2000-01-01 00:00:00 is JD 2451544.5
19385 ** This implementation requires years to be expressed as a 4-digit number
19386 ** which means that only dates between 0000-01-01 and 9999-12-31 can
19392 ** use the julian calendar for dates prior to 1582-10-15 and for some
19400 ** ISBM 0-943396-61-1
19401 ** Willmann-Bell, Inc
19447 ** of a four-character format specifiers ABCD is:
19461 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
19462 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
19463 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
19464 ** the 2-digit day which is the last integer in the set.
19477 char N = zFormat[0] - '0'; in getDigits()
19478 char min = zFormat[1] - '0'; in getDigits()
19483 max = aMx[zFormat[2] - 'a']; in getDigits()
19486 while( N-- ){ in getDigits()
19490 val = val*10 + *zDate - '0'; in getDigits()
19507 ** Parse a timezone extension on the end of a date-time.
19510 ** (+/-)HH:MM
19517 ** of change in p->tz and return 0. If a parser error occurs,
19518 ** return non-zero.
19527 p->tz = 0; in parseTimezone()
19529 if( c=='-' ){ in parseTimezone()
19530 sgn = -1; in parseTimezone()
19543 zDate += 5; in parseTimezone()
19544 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
19547 p->tzSet = 1; in parseTimezone()
19564 zDate += 5; in parseHhMmSs()
19575 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
19584 p->validJD = 0; in parseHhMmSs()
19585 p->rawS = 0; in parseHhMmSs()
19586 p->validHMS = 1; in parseHhMmSs()
19587 p->h = h; in parseHhMmSs()
19588 p->m = m; in parseHhMmSs()
19589 p->s = s + ms; in parseHhMmSs()
19591 p->validTZ = (p->tz!=0)?1:0; in parseHhMmSs()
19600 p->isError = 1; in datetimeError()
19604 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
19605 ** that the YYYY-MM-DD is according to the Gregorian calendar.
19612 if( p->validJD ) return; in computeJD()
19613 if( p->validYMD ){ in computeJD()
19614 Y = p->Y; in computeJD()
19615 M = p->M; in computeJD()
19616 D = p->D; in computeJD()
19618 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
19622 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
19627 Y--; in computeJD()
19631 B = 2 - A + (A/4); in computeJD()
19634 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
19635 p->validJD = 1; in computeJD()
19636 if( p->validHMS ){ in computeJD()
19637 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); in computeJD()
19638 if( p->validTZ ){ in computeJD()
19639 p->iJD -= p->tz*60000; in computeJD()
19640 p->validYMD = 0; in computeJD()
19641 p->validHMS = 0; in computeJD()
19642 p->validTZ = 0; in computeJD()
19650 ** YYYY-MM-DD HH:MM:SS.FFF
19651 ** YYYY-MM-DD HH:MM:SS
19652 ** YYYY-MM-DD HH:MM
19653 ** YYYY-MM-DD
19656 ** on success and 1 if the input string is not a well-formed
19662 if( zDate[0]=='-' ){ in parseYyyyMmDd()
19668 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
19676 p->validHMS = 0; in parseYyyyMmDd()
19680 p->validJD = 0; in parseYyyyMmDd()
19681 p->validYMD = 1; in parseYyyyMmDd()
19682 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
19683 p->M = M; in parseYyyyMmDd()
19684 p->D = D; in parseYyyyMmDd()
19685 if( p->validTZ ){ in parseYyyyMmDd()
19697 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
19698 if( p->iJD>0 ){ in setDateTimeToCurrent()
19699 p->validJD = 1; in setDateTimeToCurrent()
19710 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
19713 p->s = r; in setRawDateNumber()
19714 p->rawS = 1; in setRawDateNumber()
19716 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
19717 p->validJD = 1; in setRawDateNumber()
19727 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
19731 ** In the first form, the +/-HH:MM is always optional. The fractional
19756 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
19779 if( p->validYMD ) return; in computeYMD()
19780 if( !p->validJD ){ in computeYMD()
19781 p->Y = 2000; in computeYMD()
19782 p->M = 1; in computeYMD()
19783 p->D = 1; in computeYMD()
19784 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
19788 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
19789 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
19790 A = Z + 1 + A - (A/4); in computeYMD()
19792 C = (int)((B - 122.1)/365.25); in computeYMD()
19794 E = (int)((B-D)/30.6001); in computeYMD()
19796 p->D = B - D - X1; in computeYMD()
19797 p->M = E<14 ? E-1 : E-13; in computeYMD()
19798 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
19800 p->validYMD = 1; in computeYMD()
19808 if( p->validHMS ) return; in computeHMS()
19810 s = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
19811 p->s = s/1000.0; in computeHMS()
19812 s = (int)p->s; in computeHMS()
19813 p->s -= s; in computeHMS()
19814 p->h = s/3600; in computeHMS()
19815 s -= p->h*3600; in computeHMS()
19816 p->m = s/60; in computeHMS()
19817 p->s += s - p->m*60; in computeHMS()
19818 p->rawS = 0; in computeHMS()
19819 p->validHMS = 1; in computeHMS()
19834 p->validYMD = 0; in clearYMD_HMS_TZ()
19835 p->validHMS = 0; in clearYMD_HMS_TZ()
19836 p->validTZ = 0; in clearYMD_HMS_TZ()
19846 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
19860 ** using whatever operating-system specific localtime facility that
19862 ** non-zero on any kind of error.
19867 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
19925 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in localtimeOffset()
19943 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in localtimeOffset()
19945 sqlite3_result_error(pCtx, "local time unavailable", -1); in localtimeOffset()
19963 return y.iJD - x.iJD; in localtimeOffset()
19972 ** Where NNN is an arbitrary floating-point number and "days" can be one
19986 { 1, 5, "month", 176546.0, 30.0*86400000.0 },
19991 ** Process a modifier to a date-time stamp. The modifiers are
20032 p->iJD += localtimeOffset(p, pCtx, &rc); in parseModifier()
20042 ** Treat the current value of p->s as the number of in parseModifier()
20045 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
20046 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
20049 p->iJD = (sqlite3_int64)r; in parseModifier()
20050 p->validJD = 1; in parseModifier()
20051 p->rawS = 0; in parseModifier()
20057 if( p->tzSet==0 ){ in parseModifier()
20062 p->iJD -= c1; in parseModifier()
20064 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); in parseModifier()
20066 p->tzSet = 1; in parseModifier()
20080 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
20087 p->validTZ = 0; in parseModifier()
20088 p->validJD = 0; in parseModifier()
20090 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
20091 if( Z>n ) Z -= 7; in parseModifier()
20092 p->iJD += (n - Z)*86400000; in parseModifier()
20106 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
20109 p->validHMS = 1; in parseModifier()
20110 p->h = p->m = 0; in parseModifier()
20111 p->s = 0.0; in parseModifier()
20112 p->rawS = 0; in parseModifier()
20113 p->validTZ = 0; in parseModifier()
20114 p->validJD = 0; in parseModifier()
20116 p->D = 1; in parseModifier()
20119 p->M = 1; in parseModifier()
20120 p->D = 1; in parseModifier()
20128 case '-': in parseModifier()
20134 case '5': in parseModifier()
20147 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
20159 tx.iJD -= 43200000; in parseModifier()
20161 tx.iJD -= day*86400000; in parseModifier()
20162 if( z[0]=='-' ) tx.iJD = -tx.iJD; in parseModifier()
20165 p->iJD += tx.iJD; in parseModifier()
20176 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
20179 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
20183 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
20189 p->M += (int)r; in parseModifier()
20190 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
20191 p->Y += x; in parseModifier()
20192 p->M -= x*12; in parseModifier()
20193 p->validJD = 0; in parseModifier()
20194 r -= (int)r; in parseModifier()
20200 p->Y += y; in parseModifier()
20201 p->validJD = 0; in parseModifier()
20202 r -= (int)r; in parseModifier()
20207 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); in parseModifier()
20223 ** Process time function arguments. argv[0] is a date-time stamp.
20259 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
20289 ** Return YYYY-MM-DD HH:MM:SS
20300 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d", in datetimeFunc()
20302 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in datetimeFunc()
20321 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in timeFunc()
20328 ** Return YYYY-MM-DD
20339 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); in dateFunc()
20340 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in dateFunc()
20351 ** %H hour 00-24
20352 ** %j day of year 000-366
20354 ** %m month 01-12
20355 ** %M minute 00-59
20356 ** %s seconds since 1970-01-01
20357 ** %S seconds 00-59
20358 ** %w day of week 0-6 sunday==0
20359 ** %W week of year 00-53
20360 ** %Y year 0000-9999
20377 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
20412 testcase( n==sizeof(zBuf)-1 ); in strftimeFunc()
20414 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); in strftimeFunc()
20415 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); in strftimeFunc()
20418 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ in strftimeFunc()
20453 nDay = (int)((x.iJD-y.iJD+43200000)/86400000); in strftimeFunc()
20457 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); in strftimeFunc()
20474 (i64)(x.iJD/1000 - 21086676*(i64)10000)); in strftimeFunc()
20484 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]); in strftimeFunc()
20492 sqlite3_result_text(context, z, -1, in strftimeFunc()
20541 ** If the library is compiled to omit the full-scale date and time
20547 ** This function uses the C-library functions time(), gmtime()
20549 ** as the user-data for the function.
20568 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
20579 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
20592 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
20593 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
20594 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
20595 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
20596 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
20602 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
20603 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
20635 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
20653 ** from within OsOpen()), but some third-party implementations may.
20693 if( pId->pMethods ){ in sqlite3OsClose()
20694 pId->pMethods->xClose(pId); in sqlite3OsClose()
20695 pId->pMethods = 0; in sqlite3OsClose()
20700 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
20704 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
20707 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
20711 return id->pMethods->xSync(id, flags); in sqlite3OsSync()
20715 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
20719 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
20722 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
20726 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
20743 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM in sqlite3OsFileControl()
20753 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
20756 (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
20760 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
20764 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
20767 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
20770 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
20773 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
20783 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
20790 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
20793 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
20796 /* No-op stubs to use when memory-mapped I/O is disabled */
20823 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut); in sqlite3OsOpen()
20824 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
20830 return pVfs->xDelete(pVfs, zPath, dirSync); in sqlite3OsDelete()
20839 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
20849 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
20853 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
20856 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
20859 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
20862 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
20866 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
20869 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
20872 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
20876 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
20882 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
20883 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
20886 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
20901 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
20956 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
20958 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
20970 /* No-op */ in vfsUnlink()
20972 vfsList = pVfs->pNext; in vfsUnlink()
20975 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
20976 p = p->pNext; in vfsUnlink()
20978 if( p->pNext==pVfs ){ in vfsUnlink()
20979 p->pNext = pVfs->pNext; in vfsUnlink()
21003 pVfs->pNext = vfsList; in sqlite3_vfs_register()
21006 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
21007 vfsList->pNext = pVfs; in sqlite3_vfs_register()
21046 ** Most malloc failures are non-benign. After they occur, SQLite
21070 ** we have to locate the state vector at run-time. In the more common
21100 ** indicates that subsequent malloc failures are non-benign.
21131 ** This file contains a no-op memory allocation drivers for use when
21141 ** used when no other memory allocator is specified using compile-time
21147 ** No-op versions of all memory allocation routines
21160 ** Populate the low-level memory allocation function pointers in
21193 ** This file contains low-level memory allocation drivers for when
21194 ** SQLite will use the standard C-library malloc/realloc/free interface
21197 ** This file contains implementations of the low-level memory allocation
21205 ** C-preprocessor macro summary:
21212 ** a different name, using a separate -D
21228 ** used when no other memory allocator is specified using compile-time
21248 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
21253 ** Use standard C library malloc and free on non-Apple systems.
21270 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
21304 ** For this low-level routine, we are guaranteed that nByte>0 because
21338 ** For this low-level routine, we already know that pPrior!=0 since
21340 ** by higher-level routines.
21348 p--; in sqlite3MemFree()
21365 p--; in sqlite3MemSize()
21374 ** For this low-level interface, we know that pPrior!=0. Cases where
21375 ** pPrior==0 while have been intercepted by higher-level routine and
21377 ** cases where nByte<=0 will have been intercepted by higher-level
21393 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
21394 p--; in sqlite3MemRealloc()
21454 ** Populate the low-level memory allocation function pointers in
21487 ** This file contains low-level memory allocation drivers for when
21488 ** SQLite will use the standard C-library malloc/realloc/free interface
21493 ** This file contains implementations of the low-level memory allocation
21519 ** ------------------------------------------------------------------------
21521 ** ------------------------------------------------------------------------
21605 if( i>NCSIZE-1 ){ in adjustStats()
21606 i = NCSIZE - 1; in adjustStats()
21615 mem.nCurrent[i]--; in adjustStats()
21633 p--; in sqlite3MemsysGetHeader()
21634 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
21635 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
21643 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
21656 return (int)pHdr->iSize; in sqlite3MemSize()
21689 ** Fill a buffer with pseudo-random bytes. This is used to preset
21698 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
21703 nByte -= 4; in randomFill()
21705 while( nByte-- > 0 ){ in randomFill()
21706 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
21734 pHdr->pNext = 0; in sqlite3MemMalloc()
21735 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
21737 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
21742 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
21743 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
21744 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
21745 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
21748 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
21749 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
21752 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
21755 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
21760 pHdr->iSize = nByte; in sqlite3MemMalloc()
21765 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
21783 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
21785 if( pHdr->pPrev ){ in sqlite3MemFree()
21786 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
21787 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
21790 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
21792 if( pHdr->pNext ){ in sqlite3MemFree()
21793 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
21794 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
21797 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
21800 z -= pHdr->nTitle; in sqlite3MemFree()
21801 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
21802 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
21803 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
21821 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
21825 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
21826 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
21827 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
21835 ** Populate the low-level memory allocation function pointers in
21859 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
21860 pHdr->eType = eType; in sqlite3MemdebugSetType()
21878 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
21879 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
21900 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
21901 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
21930 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
21939 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
21941 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
21942 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
21961 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
21963 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
21965 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
21966 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
21969 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
21970 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
21975 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
21977 fprintf(out, " %5d: %10d %10d %10d\n", in sqlite3MemdebugDump()
21981 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
21982 fprintf(out, " %5d: %10d %10d %10d\n", in sqlite3MemdebugDump()
21983 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
21984 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
22036 ** mean that the library will use a memory-pool by default, just that
22077 ** two fields form a double-linked list of chunks of related sizes.
22114 ** True if we are evaluating an out-of-memory callback.
22142 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
22175 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
22177 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
22178 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
22181 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
22210 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
22211 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
22212 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
22215 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
22254 ** size parameters for check-out and return a pointer to the
22261 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
22262 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
22263 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
22264 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
22265 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
22266 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
22278 if( nBlock>=mem3.szMaster-1 ){ in memsys3FromMaster()
22288 newi = mem3.iMaster + mem3.szMaster - nBlock; in memsys3FromMaster()
22290 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock; in memsys3FromMaster()
22291 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2; in memsys3FromMaster()
22292 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromMaster()
22293 mem3.szMaster -= nBlock; in memsys3FromMaster()
22294 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster; in memsys3FromMaster()
22295 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FromMaster()
22296 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FromMaster()
22326 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
22330 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
22331 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
22336 size = i + size/4 - prev; in memsys3Merge()
22337 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
22338 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
22339 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
22379 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
22381 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
22387 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
22420 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
22447 i = p - mem3.aPool; in memsys3FreeUnsafe()
22448 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
22449 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
22451 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
22452 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
22453 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
22458 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
22459 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize; in memsys3FreeUnsafe()
22460 mem3.iMaster -= size; in memsys3FreeUnsafe()
22463 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
22464 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FreeUnsafe()
22465 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; in memsys3FreeUnsafe()
22467 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
22468 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
22470 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
22471 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; in memsys3FreeUnsafe()
22472 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; in memsys3FreeUnsafe()
22479 ** size returned omits the 8-byte header overhead. This only
22486 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
22487 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
22497 return ((n+11)&~7) - 4; in memsys3Roundup()
22537 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
22566 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
22612 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
22618 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
22623 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
22629 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
22631 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
22635 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
22640 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
22649 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
22654 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8); in sqlite3Memsys3Dump()
22655 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8); in sqlite3Memsys3Dump()
22671 ** Populate the low-level memory allocation function pointers in
22732 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
22741 ** N >= M*(1 + log2(n)/2) - n + 1
22770 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
22805 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
22836 ** structures, return a pointer to the idx-th such link.
22850 next = MEM5LINK(i)->next; in memsys5Unlink()
22851 prev = MEM5LINK(i)->prev; in memsys5Unlink()
22855 MEM5LINK(prev)->next = next; in memsys5Unlink()
22858 MEM5LINK(next)->prev = prev; in memsys5Unlink()
22873 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
22874 MEM5LINK(i)->prev = -1; in memsys5Link()
22877 MEM5LINK(x)->prev = i; in memsys5Link()
22899 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
22954 iBin--; in memsys5MallocUnsafe()
22965 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
22992 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
22994 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
22996 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
23001 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
23004 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
23009 mem5.currentCount--; in memsys5FreeUnsafe()
23010 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
23019 iBuddy = iBlock - size; in memsys5FreeUnsafe()
23081 ** memsys5Round(). Hence nBytes is always a non-negative power
23090 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
23113 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
23126 ** Examples: memsys5Log(1) -> 0
23127 ** memsys5Log(2) -> 1
23128 ** memsys5Log(4) -> 2
23129 ** memsys5Log(5) -> 3
23130 ** memsys5Log(8) -> 3
23131 ** memsys5Log(9) -> 4
23135 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
23160 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
23178 mem5.aiFreelist[ii] = -1; in memsys5Init()
23182 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
23232 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
23322 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
23323 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
23324 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
23325 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
23326 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
23327 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
23328 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
23329 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
23331 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
23419 ** this function is a no-op.
23463 ** here are place-holders. Applications can substitute working
23464 ** mutex routines at start-time using the
23537 return p==0 || p->cnt>0; in debugMutexHeld()
23541 return p==0 || p->cnt==0; in debugMutexNotheld()
23556 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
23563 pNew->id = id; in debugMutexAlloc()
23564 pNew->cnt = 0; in debugMutexAlloc()
23570 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
23575 pNew = &aStatic[id-2]; in debugMutexAlloc()
23576 pNew->id = id; in debugMutexAlloc()
23588 assert( p->cnt==0 ); in debugMutexFree()
23589 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
23611 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
23612 p->cnt++; in debugMutexEnter()
23616 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
23617 p->cnt++; in debugMutexTry()
23630 p->cnt--; in debugMutexLeave()
23631 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
23653 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
23654 ** is used regardless of the run-time threadsafety setting.
23694 ** home-grown mutexes. Encapsulate these conditions into a single #define.
23736 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
23742 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
23745 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
23798 ** cases where it really needs one. If a faster non-recursive mutex
23839 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
23845 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
23854 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
23860 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
23865 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
23870 if( p ) p->id = iType; in pthreadMutexAlloc()
23882 assert( p->nRef==0 ); in pthreadMutexFree()
23884 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
23887 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
23909 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
23914 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
23915 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
23917 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
23924 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
23925 p->nRef++; in pthreadMutexEnter()
23927 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
23928 assert( p->nRef==0 ); in pthreadMutexEnter()
23929 p->owner = self; in pthreadMutexEnter()
23930 p->nRef = 1; in pthreadMutexEnter()
23934 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
23936 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
23938 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
23939 p->owner = pthread_self(); in pthreadMutexEnter()
23940 p->nRef++; in pthreadMutexEnter()
23945 if( p->trace ){ in pthreadMutexEnter()
23946 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
23952 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
23957 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
23958 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
23960 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
23967 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
23968 p->nRef++; in pthreadMutexTry()
23970 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
23971 assert( p->nRef==0 ); in pthreadMutexTry()
23972 p->owner = self; in pthreadMutexTry()
23973 p->nRef = 1; in pthreadMutexTry()
23980 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
23982 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
23984 p->owner = pthread_self(); in pthreadMutexTry()
23985 p->nRef++; in pthreadMutexTry()
23994 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
23995 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
24010 p->nRef--; in pthreadMutexLeave()
24011 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
24013 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
24016 if( p->nRef==0 ){ in pthreadMutexLeave()
24017 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
24020 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
24024 if( p->trace ){ in pthreadMutexLeave()
24025 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
24089 ** all of the platform-specific files (os_*.c) and is #included into those
24101 ** switch. The following code should catch this problem at compile-time.
24115 ** high-performance timing routines.
24131 ** This file contains inline asm code for retrieving "high-performance"
24138 ** The following routine only works on pentium-class (or newer) processors.
24140 ** processor and returns that value. This can be used for high-res
24211 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
24235 || sqlite3_io_error_pending-- == 1 ) \
24250 sqlite3_diskfull_pending--; \
24328 ** Determine if we are dealing with Windows CE - which has a much reduced
24356 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
24411 return p->nRef!=0 && p->owner==GetCurrentThreadId();
24415 return p->nRef==0 || p->owner!=tid;
24460 static int winMutex_isNt = -1; /* <0 means "need to query" */
24538 ** cases where it really needs one. If a faster non-recursive mutex
24564 p->id = iType;
24567 p->trace = 1;
24571 InitializeCriticalSectionEx(&p->mutex, 0, 0);
24573 InitializeCriticalSection(&p->mutex);
24580 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
24585 p = &winMutex_staticMutexes[iType-2];
24586 p->id = iType;
24589 p->trace = 1;
24606 assert( p->nRef==0 && p->owner==0 );
24607 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
24608 DeleteCriticalSection(&p->mutex);
24634 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
24639 EnterCriticalSection(&p->mutex);
24641 assert( p->nRef>0 || p->owner==0 );
24642 p->owner = tid;
24643 p->nRef++;
24644 if( p->trace ){
24645 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
24646 tid, p->id, p, p->trace, p->nRef));
24657 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
24671 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
24676 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
24678 p->owner = tid;
24679 p->nRef++;
24687 if( p->trace ){
24688 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
24689 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
24707 assert( p->nRef>0 );
24708 assert( p->owner==tid );
24709 p->nRef--;
24710 if( p->nRef==0 ) p->owner = 0;
24711 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
24714 LeaveCriticalSection(&p->mutex);
24716 if( p->trace ){
24717 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
24718 tid, p->id, p, p->trace, p->nRef));
24765 ** Attempt to release up to n bytes of non-essential memory currently
24766 ** held by SQLite. An example of non-essential memory is memory used to
24773 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
24774 ** is a no-op returning zero if SQLite is not compiled with
24826 ** no-op.
24841 ** Set the soft heap-size limit for the library. Passing a zero or
24850 if( rc ) return -1;
24862 excess = sqlite3_memory_used() - n;
24891 for(i=0; i<n-1; i++){
24892 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
24893 pSlot = pSlot->pNext;
24895 pSlot->pNext = 0;
24914 ** Return true if the heap is currently under memory pressure - in other
24972 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
24989 if( nUsed >= mem0.alarmThreshold - nFull ){
25031 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
25055 ** xScratchMalloc(). We verify this constraint in the single-threaded
25067 ** for situations where the memory might be held long-term. This
25080 mem0.pScratchFree = mem0.pScratchFree->pNext;
25081 mem0.nScratchFree--;
25098 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
25101 ** This can only be checked in single-threaded mode.
25115 ** single-threaded case since checking in the multi-threaded case
25118 scratchAllocOut--;
25126 pSlot->pNext = mem0.pScratchFree;
25157 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
25185 assert( sqlite3_mutex_held(db->mutex) );
25186 return db->lookaside.sz;
25199 if( p==0 ) return; /* IMP: R-49053-54554 */
25215 ** *db->pnBytesFreed.
25218 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
25223 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
25224 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
25227 assert( db==0 || sqlite3_mutex_held(db->mutex) );
25230 if( db->pnBytesFreed ){
25238 memset(p, 0xaa, db->lookaside.sz);
25240 pBuf->pNext = db->lookaside.pFree;
25241 db->lookaside.pFree = pBuf;
25242 db->lookaside.nOut--;
25253 assert( db==0 || sqlite3_mutex_held(db->mutex) );
25266 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
25269 sqlite3_free(pOld); /* IMP: R-26507-47431 */
25277 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
25286 nDiff = nNew - nOld;
25288 mem0.alarmThreshold-nDiff ){
25298 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
25304 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
25316 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
25360 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
25369 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
25397 assert( sqlite3_mutex_held(db->mutex) );
25398 assert( db->pnBytesFreed==0 );
25399 if( db->lookaside.bDisable==0 ){
25400 assert( db->mallocFailed==0 );
25401 if( n>db->lookaside.sz ){
25402 db->lookaside.anStat[1]++;
25403 }else if( (pBuf = db->lookaside.pFree)==0 ){
25404 db->lookaside.anStat[2]++;
25406 db->lookaside.pFree = pBuf->pNext;
25407 db->lookaside.nOut++;
25408 db->lookaside.anStat[0]++;
25409 if( db->lookaside.nOut>db->lookaside.mxOut ){
25410 db->lookaside.mxOut = db->lookaside.nOut;
25414 }else if( db->mallocFailed ){
25419 assert( sqlite3_mutex_held(db->mutex) );
25420 assert( db->pnBytesFreed==0 );
25421 if( db->mallocFailed ){
25438 assert( sqlite3_mutex_held(db->mutex) );
25439 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
25446 if( db->mallocFailed==0 ){
25450 memcpy(pNew, p, db->lookaside.sz);
25462 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
25525 ** Call this routine to record the fact that an OOM (out-of-memory) error
25526 ** has happened. This routine will set db->mallocFailed, and also
25531 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
25532 db->mallocFailed = 1;
25533 if( db->nVdbeExec>0 ){
25534 db->u1.isInterrupted = 1;
25536 db->lookaside.bDisable++;
25542 ** db->mallocFailed flag as necessary.
25548 if( db->mallocFailed && db->nVdbeExec==0 ){
25549 db->mallocFailed = 0;
25550 db->u1.isInterrupted = 0;
25551 assert( db->lookaside.bDisable>0 );
25552 db->lookaside.bDisable--;
25574 ** If an OOM as occurred, then the connection error-code (the value
25579 ** Otherwise the read (and possible write) of db->mallocFailed
25583 assert( sqlite3_mutex_held(db->mutex) );
25584 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
25587 return rc & db->errMask;
25598 ** This file contains code for a set of "printf"-like routines. These
25609 #define etRADIX 0 /* non-decimal integer types. %x %o */
25614 #define etSTRING 5 /* Strings. %s */
25625 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
25626 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
25633 ** An "etByte" is an 8-bit unsigned value.
25662 static const char aPrefix[] = "-x0\000X0";
25708 ** 16 (the number of significant digits in a 64-bit float) '0' is
25715 (*cnt)--;
25719 *val = (*val - d)*10.0;
25729 p->accError = eError;
25730 p->nAlloc = 0;
25737 if( p->nArg<=p->nUsed ) return 0;
25738 return sqlite3_value_int64(p->apArg[p->nUsed++]);
25741 if( p->nArg<=p->nUsed ) return 0.0;
25742 return sqlite3_value_double(p->apArg[p->nUsed++]);
25745 if( p->nArg<=p->nUsed ) return 0;
25746 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
25773 etByte flag_leftjustify; /* True if "-" flag is present */
25783 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
25801 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
25815 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
25828 case '-': flag_leftjustify = 1; break;
25847 width = width >= -2147483647 ? -width : 0;
25853 wx = wx*10 + c - '0';
25877 precision = precision >= -2147483647 ? -precision : -1;
25882 px = px*10 + c - '0';
25889 precision = -1;
25891 assert( precision>=(-1) );
25916 xtype = infop->type;
25927 ** flag_leftjustify TRUE if a '-' is present or if the
25932 ** always non-negative. Zero is the default.
25934 ** is -1.
25948 if( infop->flags & FLAG_SIGNED ){
25965 longvalue = -v;
25967 prefix = '-';
25987 if( flag_zeropad && precision<width-(prefix!=0) ){
25988 precision = width-(prefix!=0);
25990 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
26002 bufpt = &zOut[nOut-1];
26009 *(--bufpt) = zOrd[x*2+1];
26010 *(--bufpt) = zOrd[x*2];
26013 const char *cset = &aDigits[infop->charset];
26014 u8 base = infop->base;
26016 *(--bufpt) = cset[longvalue%base];
26020 length = (int)(&zOut[nOut-1]-bufpt);
26022 *(--bufpt) = '0'; /* Zero pad */
26026 int nn = (length - 1)/3; /* Number of "," to insert */
26027 int ix = (length - 1)%3 + 1;
26028 bufpt -= nn;
26031 ix--;
26034 nn--;
26039 if( prefix ) *(--bufpt) = prefix; /* Add sign */
26040 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
26043 pre = &aPrefix[infop->prefix];
26044 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
26046 length = (int)(&zOut[nOut-1]-bufpt);
26061 realvalue = -realvalue;
26062 prefix = '-';
26066 if( xtype==etGENERIC && precision>0 ) precision--;
26068 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
26083 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
26084 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
26104 if( exp<-4 || exp>precision ){
26107 precision = precision - exp;
26118 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
26137 for(; e2>=0; e2--){
26147 for(e2++; e2<0; precision--, e2++){
26152 while( (precision--)>0 ){
26157 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
26159 if( bufpt[-1]=='.' ){
26163 *(--bufpt) = 0;
26169 *(bufpt++) = aDigits[infop->charset];
26171 *(bufpt++) = '-'; exp = -exp;
26187 length = (int)(bufpt-zOut);
26194 int nPad = width - length;
26195 for(i=width; i>=nPad; i--){
26196 bufpt[i] = bufpt[i-nPad];
26199 while( nPad-- ) bufpt[i++] = '0';
26206 *(va_arg(ap,int*)) = pAccum->nChar;
26223 width -= precision-1;
26225 sqlite3AppendChar(pAccum, width-1, ' ');
26228 sqlite3AppendChar(pAccum, precision-1, c);
26270 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
26301 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26304 if( pToken && pToken->n ){
26305 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
26314 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26317 pItem = &pSrc->a[k];
26319 assert( k>=0 && k<pSrc->nSrc );
26320 if( pItem->zDatabase ){
26321 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
26324 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
26338 width -= length;
26348 sqlite3DbFree(pAccum->db, zExtra);
26363 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
26364 if( p->accError ){
26365 testcase(p->accError==STRACCUM_TOOBIG);
26366 testcase(p->accError==STRACCUM_NOMEM);
26369 if( p->mxAlloc==0 ){
26370 N = p->nAlloc - p->nChar - 1;
26374 char *zOld = isMalloced(p) ? p->zText : 0;
26375 i64 szNew = p->nChar;
26376 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
26378 if( szNew+p->nChar<=p->mxAlloc ){
26381 szNew += p->nChar;
26383 if( szNew > p->mxAlloc ){
26388 p->nAlloc = (int)szNew;
26390 if( p->db ){
26391 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
26393 zNew = sqlite3_realloc64(zOld, p->nAlloc);
26396 assert( p->zText!=0 || p->nChar==0 );
26397 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
26398 p->zText = zNew;
26399 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
26400 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
26414 testcase( p->nChar + (i64)N > 0x7fffffff );
26415 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
26418 assert( (p->zText==p->zBase)==!isMalloced(p) );
26419 while( (N--)>0 ) p->zText[p->nChar++] = c;
26426 ** This is a helper routine to sqlite3StrAccumAppend() that does special-case
26433 memcpy(&p->zText[p->nChar], z, N);
26434 p->nChar += N;
26436 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
26445 assert( p->zText!=0 || p->nChar==0 || p->accError );
26447 assert( p->accError==0 || p->nAlloc==0 );
26448 if( p->nChar+N >= p->nAlloc ){
26451 assert( p->zText );
26452 p->nChar += N;
26453 memcpy(&p->zText[p->nChar-N], z, N);
26458 ** Append the complete text of zero-terminated string z[] to the p string.
26466 ** Finish off a string by making sure it is zero-terminated.
26471 assert( p->mxAlloc>0 && !isMalloced(p) );
26472 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
26473 if( p->zText ){
26474 memcpy(p->zText, p->zBase, p->nChar+1);
26475 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
26479 return p->zText;
26482 if( p->zText ){
26483 assert( (p->zText==p->zBase)==!isMalloced(p) );
26484 p->zText[p->nChar] = 0;
26485 if( p->mxAlloc>0 && !isMalloced(p) ){
26489 return p->zText;
26496 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
26498 sqlite3DbFree(p->db, p->zText);
26499 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
26501 p->zText = 0;
26509 ** memory is used if not NULL. db->mallocFailed is set appropriately
26519 p->zText = p->zBase = zBase;
26520 p->db = db;
26521 p->nChar = 0;
26522 p->nAlloc = n;
26523 p->mxAlloc = mx;
26524 p->accError = 0;
26525 p->printfFlags = 0;
26530 ** %-conversion extensions.
26538 db->aLimit[SQLITE_LIMIT_LENGTH]);
26550 ** %-conversion extensions.
26563 ** %-conversion extensions.
26587 ** %-conversion extensions.
26641 ** stack space on small-stack systems when logging is disabled.
26696 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
26709 ** 2015-06-08
26740 p->iLevel++;
26743 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
26752 p->iLevel--;
26753 if( p->iLevel<0 ) sqlite3_free(p);
26767 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
26768 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
26770 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
26776 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
26791 ** Generate a human-readable description of a WITH clause.
26796 if( pWith->nCte==0 ) return;
26797 if( pWith->pOuter ){
26798 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
26802 if( pWith->nCte>0 ){
26804 for(i=0; i<pWith->nCte; i++){
26807 const struct Cte *pCte = &pWith->a[i];
26809 sqlite3XPrintf(&x, "%s", pCte->zName);
26810 if( pCte->pCols && pCte->pCols->nExpr>0 ){
26813 for(j=0; j<pCte->pCols->nExpr; j++){
26814 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
26821 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
26822 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
26831 ** Generate a human-readable description of a Select object.
26837 sqlite3TreeViewLine(pView, "nil-SELECT");
26841 if( p->pWith ){
26842 sqlite3TreeViewWith(pView, p->pWith, 1);
26848 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
26849 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
26850 (int)p->nSelectRow
26853 if( p->pPrior ){
26857 if( p->pSrc && p->pSrc->nSrc ) n++;
26858 if( p->pWhere ) n++;
26859 if( p->pGroupBy ) n++;
26860 if( p->pHaving ) n++;
26861 if( p->pOrderBy ) n++;
26862 if( p->pLimit ) n++;
26863 if( p->pOffset ) n++;
26865 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
26866 if( p->pSrc && p->pSrc->nSrc ){
26868 pView = sqlite3TreeViewPush(pView, (n--)>0);
26870 for(i=0; i<p->pSrc->nSrc; i++){
26871 struct SrcList_item *pItem = &p->pSrc->a[i];
26875 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
26876 if( pItem->zDatabase ){
26877 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
26878 }else if( pItem->zName ){
26879 sqlite3XPrintf(&x, " %s", pItem->zName);
26881 if( pItem->pTab ){
26882 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
26884 if( pItem->zAlias ){
26885 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
26887 if( pItem->fg.jointype & JT_LEFT ){
26888 sqlite3XPrintf(&x, " LEFT-JOIN");
26891 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
26892 if( pItem->pSelect ){
26893 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
26895 if( pItem->fg.isTabFunc ){
26896 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
26902 if( p->pWhere ){
26903 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
26904 sqlite3TreeViewExpr(pView, p->pWhere, 0);
26907 if( p->pGroupBy ){
26908 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
26910 if( p->pHaving ){
26911 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
26912 sqlite3TreeViewExpr(pView, p->pHaving, 0);
26915 if( p->pOrderBy ){
26916 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
26918 if( p->pLimit ){
26919 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
26920 sqlite3TreeViewExpr(pView, p->pLimit, 0);
26923 if( p->pOffset ){
26924 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
26925 sqlite3TreeViewExpr(pView, p->pOffset, 0);
26928 if( p->pPrior ){
26930 switch( p->op ){
26937 p = p->pPrior;
26943 ** Generate a human-readable explanation of an expression tree.
26955 if( pExpr->flags ){
26958 pExpr->flags, pExpr->iRightJoinTable);
26960 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
26965 switch( pExpr->op ){
26968 pExpr->iTable, pExpr->iColumn, zFlgs);
26972 if( pExpr->iTable<0 ){
26974 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
26977 pExpr->iTable, pExpr->iColumn, zFlgs);
26982 if( pExpr->flags & EP_IntValue ){
26983 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
26985 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
26991 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
26996 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
27005 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
27011 pExpr->u.zToken, pExpr->iColumn);
27015 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
27019 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
27025 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
27026 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27060 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
27061 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27066 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
27067 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27077 pFarg = pExpr->x.pList;
27079 if( pExpr->op==TK_AGG_FUNCTION ){
27081 pExpr->op2, pExpr->u.zToken);
27083 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
27092 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
27093 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27097 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
27098 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27102 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
27103 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27105 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
27107 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
27120 ** X is stored in pExpr->pLeft.
27121 ** Y is stored in pExpr->pList->a[0].pExpr.
27122 ** Z is stored in pExpr->pList->a[1].pExpr.
27125 Expr *pX = pExpr->pLeft;
27126 Expr *pY = pExpr->x.pList->a[0].pExpr;
27127 Expr *pZ = pExpr->x.pList->a[1].pExpr;
27136 ** to a column in the new.* or old.* pseudo-tables available to
27138 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
27139 ** is set to the column of the pseudo-table to read, or to -1 to
27143 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
27148 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27149 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
27155 switch( pExpr->affinity ){
27161 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
27167 pExpr->iTable, pExpr->iColumn, zFlgs);
27168 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
27172 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
27176 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
27177 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
27181 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
27182 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27186 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
27192 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
27193 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
27196 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27203 ** Generate a human-readable explanation of an expression list.
27216 for(i=0; i<pList->nExpr; i++){
27217 int j = pList->a[i].u.x.iOrderByCol;
27222 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
27253 ** This file contains code to implement a pseudo-random number
27278 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
27280 ** we have to locate the state vector at run-time. In the more common
27316 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
27346 }while( --N );
27391 ** This file presents a simple cross-platform threading interface for
27404 ** single-threaded if desired.
27416 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
27439 /* This routine is never used in single-threaded mode */
27446 p->xTask = xTask;
27447 p->pIn = pIn;
27455 rc = pthread_create(&p->tid, 0, xTask, pIn);
27458 p->done = 1;
27459 p->pOut = xTask(pIn);
27471 if( p->done ){
27472 *ppOut = p->pOut;
27475 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
27488 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
27514 assert( p->id==GetCurrentThreadId() );
27516 assert( p->xTask!=0 );
27517 p->pResult = p->xTask(p->pIn);
27544 p->xTask = xTask;
27545 p->pIn = pIn;
27546 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
27547 if( p->tid==0 ){
27551 if( p->xTask==0 ){
27552 p->id = GetCurrentThreadId();
27553 p->pResult = xTask(pIn);
27568 if( p->xTask==0 ){
27569 /* assert( p->id==GetCurrentThreadId() ); */
27571 assert( p->tid==0 );
27573 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
27574 rc = sqlite3Win32Wait((HANDLE)p->tid);
27576 bRc = CloseHandle((HANDLE)p->tid);
27579 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
27588 /********************************* Single-Threaded **************************/
27617 p->xTask = xTask;
27618 p->pIn = pIn;
27620 p->xTask = 0;
27621 p->pResult = xTask(pIn);
27632 if( p->xTask ){
27633 *ppOut = p->xTask(p->pIn);
27635 *ppOut = p->pResult;
27651 /****************************** End Single-Threaded *************************/
27667 ** This file contains routines used to translate between UTF-8,
27668 ** UTF-16, UTF-16BE, and UTF-16LE.
27670 ** Notes on UTF-8:
27672 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
27679 ** Notes on UTF-16: (with wwww+1==uuuuu)
27681 ** Word-0 Word-1 Value
27687 ** 0xff 0xfe little-endian utf-16 follows
27688 ** 0xfe 0xff big-endian utf-16 follows
27705 ** a multi-byte UTF8 character.
27744 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
27745 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
27756 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
27757 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
27784 ** Translate a single UTF-8 character. Return the unicode value.
27791 ** Notes On Invalid UTF-8:
27793 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
27794 ** be encoded as a multi-byte character. Any multi-byte character that
27798 ** If a multi-byte character attempts to encode a value between
27802 ** byte of a character are interpreted as single-byte characters
27806 ** * This routine accepts over-length UTF8 encodings
27807 ** for unicode values 0x80 and greater. It does not change over-length
27813 c = sqlite3Utf8Trans1[c-0xc0]; \
27827 ** For this routine, we assume the UTF8 string is always zero-terminated.
27831 c = sqlite3Utf8Trans1[c-0xc0];
27865 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
27866 assert( pMem->flags&MEM_Str );
27867 assert( pMem->enc!=desiredEnc );
27868 assert( pMem->enc!=0 );
27869 assert( pMem->n>=0 );
27879 /* If the translation is between UTF-16 little and big endian, then
27883 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
27891 zIn = (u8*)pMem->z;
27892 zTerm = &zIn[pMem->n&~1];
27899 pMem->enc = desiredEnc;
27905 /* When converting from UTF-16, the maximum growth results from
27906 ** translating a 2-byte character to a 4-byte UTF-8 character.
27908 ** nul-terminator.
27910 pMem->n &= ~1;
27911 len = pMem->n * 2 + 1;
27913 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
27914 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
27916 ** nul-terminator.
27918 len = pMem->n * 2 + 2;
27927 zIn = (u8*)pMem->z;
27928 zTerm = &zIn[pMem->n];
27929 zOut = sqlite3DbMallocRaw(pMem->db, len);
27935 if( pMem->enc==SQLITE_UTF8 ){
27937 /* UTF-8 -> UTF-16 Little-endian */
27944 /* UTF-8 -> UTF-16 Big-endian */
27950 pMem->n = (int)(z - zOut);
27954 if( pMem->enc==SQLITE_UTF16LE ){
27955 /* UTF-16 Little-endian -> UTF-8 */
27961 /* UTF-16 Big-endian -> UTF-8 */
27967 pMem->n = (int)(z - zOut);
27970 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
27972 c = pMem->flags;
27974 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
27975 pMem->enc = desiredEnc;
27976 pMem->z = (char*)zOut;
27977 pMem->zMalloc = pMem->z;
27978 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
27994 ** This routine checks for a byte-order mark at the beginning of the
27995 ** UTF-16 string stored in *pMem. If one is present, it is removed and
27997 ** byte-swapping, it just sets Mem.enc appropriately.
28006 assert( pMem->n>=0 );
28007 if( pMem->n>1 ){
28008 u8 b1 = *(u8 *)pMem->z;
28009 u8 b2 = *(((u8 *)pMem->z) + 1);
28021 pMem->n -= 2;
28022 memmove(pMem->z, &pMem->z[2], pMem->n);
28023 pMem->z[pMem->n] = '\0';
28024 pMem->z[pMem->n+1] = '\0';
28025 pMem->flags |= MEM_Term;
28026 pMem->enc = bom;
28034 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
28047 zTerm = (const u8*)(-1);
28057 /* This test function is not currently used by the automated test-suite.
28062 ** Translate UTF-8 to UTF-8.
28064 ** This has the effect of making sure that the string is well-formed
28065 ** UTF-8. Miscoded characters are removed.
28067 ** The translation is done in-place and aborted if the output
28082 return (int)(zOut - zStart);
28088 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
28089 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
28100 if( db->mallocFailed ){
28104 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
28105 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
28106 assert( m.z || db->mallocFailed );
28111 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
28113 ** in pZ. nChar must be non-negative.
28131 return (int)(z-(unsigned char const *)zIn);
28150 n = (int)(z-zBuf);
28159 assert( (z-zBuf)==n );
28165 n = (int)(z-zBuf);
28171 assert( (z-zBuf)==n );
28177 n = (int)(z-zBuf);
28183 assert( (z-zBuf)==n );
28254 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
28258 ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
28259 ** On the other hand, the use of -ffast-math comes with the following
28262 ** This option [-ffast-math] should never be turned on by any
28263 ** -O option since it can result in incorrect output for programs
28267 ** Under MSVC, this NaN test may fail if compiled with a floating-
28276 # error SQLite will not work correctly with the -ffast-math option of GCC.
28291 ** lower 30 bits of a 32-bit signed integer.
28306 ** The column type is an extra string stored after the zero-terminator on
28310 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
28311 return pCol->zName + strlen(pCol->zName) + 1;
28315 ** Helper function for sqlite3Error() - called rarely. Broken out into
28320 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
28331 db->errCode = err_code;
28332 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
28343 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
28362 ** encoded in UTF-8.
28370 db->errCode = err_code;
28374 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
28380 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
28385 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
28404 sqlite3 *db = pParse->db;
28408 if( db->suppressErr ){
28411 pParse->nErr++;
28412 sqlite3DbFree(db, pParse->zErrMsg);
28413 pParse->zErrMsg = zMsg;
28414 pParse->rc = SQLITE_ERROR;
28419 ** Convert an SQL-style quoted string into a normal string by removing
28420 ** the quote characters. The conversion is done in-place. If the
28422 ** is a no-op.
28424 ** The input string must be zero-terminated. A new zero-terminator
28427 ** The return value is -1 if no dequoting occurs or the length of the
28431 ** 2002-Feb-14: This routine is extended to remove MS-Access style
28432 ** brackets from around identifiers. For example: "[a-b-c]" becomes
28433 ** "a-b-c".
28462 p->z = z;
28463 p->n = sqlite3Strlen30(z);
28466 /* Convenient short-hand */
28473 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
28475 ** the contents of two buffers containing UTF-8 strings in a
28476 ** case-independent fashion, using the same definition of "case
28481 return zRight ? -1 : 0;
28493 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
28503 return zRight ? -1 : 0;
28509 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
28510 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
28518 ** uses the encoding enc. The string is not necessarily zero-terminated.
28524 ** [+-]digits[E[+-]digits]
28525 ** [+-]digits.[digits][E[+-]digits]
28526 ** [+-].digits[E[+-]digits]
28545 int eValid = 1; /* True exponent is either not used or is well-formed */
28548 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
28559 for(i=3-enc; i<length && z[i]==0; i+=2){}
28570 if( *z=='-' ){
28571 sign = -1;
28578 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
28579 s = s*10 + (*z - '0');
28583 /* skip non-significant significand digits
28594 if( s<((LARGEST_INT64-9)/10) ){
28595 s = s*10 + (*z - '0');
28596 d--;
28611 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
28614 if( *z=='-' ){
28615 esign = -1;
28622 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
28635 esign = -1;
28636 e *= -1;
28643 result = sign<0 ? -(double)0 : (double)0;
28651 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
28653 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
28656 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
28659 e--;
28663 s = sign<0 ? -s : s;
28665 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
28670 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
28671 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
28672 while( e%308 ) { scale *= 1.0e+1; e -= 1; }
28690 while( e%22 ) { scale *= 1.0e+1; e -= 1; }
28691 while( e>0 ) { scale *= 1.0e+22; e -= 22; }
28704 /* return true if number and no extra non-whitespace chracters after */
28712 ** Compare the 19-character string zNum against the text representation
28723 ** will return -8.
28731 c = (zNum[i*incr]-pow63[i])*10;
28734 c = zNum[18*incr] - '8';
28735 testcase( c==(-1) );
28743 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
28746 ** If the zNum value is representable as a 64-bit twos-complement
28751 ** signed 64-bit integer, its negative -9223372036854775808 can be.
28753 ** If zNum is too big for a 64-bit integer and is not
28754 ** 9223372036854775808 or if zNum contains any non-numeric text,
28758 ** The string is not necessarily zero-terminated. The encoding is
28767 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
28776 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
28783 if( *zNum=='-' ){
28793 u = u*10 + c - '0';
28798 *pNum = -(i64)u;
28808 || nonNum /* UTF16 with high-order bytes non-zero */
28810 /* zNum is empty or contains non-numeric text or is longer
28818 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
28830 assert( u-1==LARGEST_INT64 );
28837 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
28838 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
28843 ** 0 Successful transformation. Fits in a 64-bit signed integer.
28844 ** 1 Integer too large for a 64-bit signed integer or is malformed
28859 return (z[k]==0 && k-i<=16) ? 0 : 1;
28868 ** If zNum represents an integer that will fit in 32-bits, then set
28873 ** Any non-numeric characters that following zNum are ignored.
28875 ** input number to be zero-terminated.
28881 if( zNum[0]=='-' ){
28908 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
28915 ** 2^31 -> 2147483648
28921 testcase( v-neg==2147483647 );
28922 if( v-neg>2147483647 ){
28926 v = -v;
28933 ** Return a 32-bit integer value extracted from a string. If the
28943 ** The variable-length integer encoding is as follows:
28950 ** 7 bits - A
28951 ** 14 bits - BA
28952 ** 21 bits - BBA
28953 ** 28 bits - BBBA
28954 ** 35 bits - BBBBA
28955 ** 42 bits - BBBBBA
28956 ** 49 bits - BBBBBBA
28957 ** 56 bits - BBBBBBBA
28958 ** 64 bits - BBBBBBBBC
28962 ** Write a 64-bit variable-length integer to memory starting at p[0].
28966 ** A variable-length integer consists of the lower 7 bits of each byte
28977 for(i=7; i>=0; i--){
28990 for(i=0, j=n-1; j>=0; j--, i++){
29022 ** Read a 64-bit variable-length integer from memory starting at p[0].
29106 return 5;
29175 b = p[-4];
29186 ** Read a 32-bit variable-length integer from memory starting at p[0].
29189 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
29193 ** single-byte case. All code should use the MACRO version as
29194 ** this function assumes the single-byte case has already been handled.
29199 /* The 1-byte case. Overwhelmingly the most common. Handled inline
29212 /* The 2-byte case */
29225 /* The 3-byte case */
29240 /* A 32-bit varint is used to store size information in btrees.
29241 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
29242 ** A 3-byte varint is sufficient, for example, to record the size
29243 ** of a 1048569-byte BLOB or string.
29245 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
29246 ** rare larger cases can be handled by the slower 64-bit varint
29254 p -= 2;
29267 ** unrolling for the 3- and 4-byte varint cases. This code is
29295 return 5;
29300 ** slow) general-purpose sqlite3GetVarint() routine to extract the
29306 p -= 4;
29308 assert( n>5 && n<=9 );
29317 ** 64-bit integer.
29327 ** Read or write a four-byte big-endian integer value.
29394 n--;
29437 magic = db->magic;
29450 magic = db->magic;
29463 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
29464 ** the other 64-bit signed integer at *pA and store the result in *pA.
29474 testcase( iB==-1 ); testcase( iB==0 );
29476 testcase( iA>0 && LARGEST_INT64 - iA == iB );
29477 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
29478 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
29480 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
29481 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
29482 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
29494 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
29496 *pA -= iB;
29499 return sqlite3AddInt64(pA, -iB);
29517 if( -iA>LARGEST_INT64/-iB ) return 1;
29526 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
29527 ** if the integer has a value of -2147483648, return +2147483647
29532 return -x;
29537 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
29543 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
29548 ** test.db-journal => test.nal
29549 ** test.db-wal => test.wal
29550 ** test.db-shm => test.shm
29551 ** test.db-mj7f3319fa => test.9fa
29560 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
29561 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
29576 8, 8, /* 4,5 */
29579 5, 5, 5, /* 12-14 */
29580 4, 4, 4, 4, /* 15-18 */
29581 3, 3, 3, 3, 3, 3, /* 19-24 */
29582 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
29587 return a+x[a-b];
29591 return b+x[b-a];
29600 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
29604 while( x<8 ){ y -= 10; x <<= 1; }
29606 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
29609 return a[x&7] + y - 10;
29624 e = (a>>52) - 1022;
29636 ** non-standard compile-time options is enabled.
29642 if( n>=5 ) n -= 2;
29643 else if( n>=1 ) n -= 1;
29652 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
29660 ** db->mallocFailed flag is set.
29673 ** is always zero-terminated.
29772 ** This is the implementation of generic hash-tables
29785 pNew->first = 0;
29786 pNew->count = 0;
29787 pNew->htsize = 0;
29788 pNew->ht = 0;
29799 elem = pH->first;
29800 pH->first = 0;
29801 sqlite3_free(pH->ht);
29802 pH->ht = 0;
29803 pH->htsize = 0;
29805 HashElem *next_elem = elem->next;
29809 pH->count = 0;
29818 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
29821 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
29839 pHead = pEntry->count ? pEntry->chain : 0;
29840 pEntry->count++;
29841 pEntry->chain = pNew;
29846 pNew->next = pHead;
29847 pNew->prev = pHead->prev;
29848 if( pHead->prev ){ pHead->prev->next = pNew; }
29849 else { pH->first = pNew; }
29850 pHead->prev = pNew;
29852 pNew->next = pH->first;
29853 if( pH->first ){ pH->first->prev = pNew; }
29854 pNew->prev = 0;
29855 pH->first = pNew;
29874 if( new_size==pH->htsize ) return 0;
29890 sqlite3_free(pH->ht);
29891 pH->ht = new_ht;
29892 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
29894 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
29895 unsigned int h = strHash(elem->pKey) % new_size;
29896 next_elem = elem->next;
29917 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
29919 h = strHash(pKey) % pH->htsize;
29920 pEntry = &pH->ht[h];
29921 elem = pEntry->chain;
29922 count = pEntry->count;
29925 elem = pH->first;
29926 count = pH->count;
29929 while( count-- ){
29931 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
29934 elem = elem->next;
29948 if( elem->prev ){
29949 elem->prev->next = elem->next;
29951 pH->first = elem->next;
29953 if( elem->next ){
29954 elem->next->prev = elem->prev;
29956 if( pH->ht ){
29957 pEntry = &pH->ht[h];
29958 if( pEntry->chain==elem ){
29959 pEntry->chain = elem->next;
29961 pEntry->count--;
29962 assert( pEntry->count>=0 );
29965 pH->count--;
29966 if( pH->count==0 ){
29967 assert( pH->first==0 );
29968 assert( pH->count==0 );
29980 return findElementWithHash(pH, pKey, 0)->data;
30005 if( elem->data ){
30006 void *old_data = elem->data;
30010 elem->data = data;
30011 elem->pKey = pKey;
30018 new_elem->pKey = pKey;
30019 new_elem->data = data;
30020 pH->count++;
30021 if( pH->count>=10 && pH->count > 2*pH->htsize ){
30022 if( rehash(pH, pH->count*2) ){
30023 assert( pH->htsize>0 );
30024 h = strHash(pKey) % pH->htsize;
30027 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
30050 /* 5 */ "NextIfOpen" OpHelp(""),
30091 /* 46 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
30092 /* 47 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
30093 /* 48 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
30134 /* 89 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
30139 /* 94 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
30192 /* 147 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30232 ** This file contains the VFS implementation for unix-like operating systems
30238 ** use flock(), dot-files, various proprietary locking schemas, or simply
30248 ** * General-purpose declarations and utility functions.
30252 ** + for no-op locks
30253 ** + for dot-file locks
30261 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
30274 ** 3. Dot-file locking,
30276 ** 5. AFP locking (OSX only),
30280 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
30357 ** If we are to be thread-safe, include the pthreads header and define
30380 ** Maximum supported path-length.
30402 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
30431 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
30457 ** transaction counter in bytes 24-27 of database files are updated
30517 ** all of the platform-specific files (os_*.c) and is #included into those
30529 ** switch. The following code should catch this problem at compile-time.
30543 ** high-performance timing routines.
30559 ** This file contains inline asm code for retrieving "high-performance"
30566 ** The following routine only works on pentium-class (or newer) processors.
30568 ** processor and returns that value. This can be used for high-res
30639 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
30663 || sqlite3_io_error_pending-- == 1 ) \
30678 sqlite3_diskfull_pending--; \
30720 ** The threadid macro resolves to the thread-id or to 0. Used for
30741 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
30742 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
30754 ** which always has the same well-defined interface.
30765 ** Many system calls are accessed through pointer-to-functions so that
30794 ** DJGPP. But it is DOS - what did you expect?
30801 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
30925 ** log if they come from non-root processes. So avoid calling fchown() if
31005 int i = -1;
31009 for(i=0; i<ArraySize(aSyscall)-1; i++){
31037 ** The m parameter will be non-zero only when creating -wal, -journal,
31038 ** and -shm files. We want those files to have *exactly* the same
31040 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
31062 fd = -1;
31113 ** integer lock-type.
31132 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
31133 ** command-line option on the compiler. This code is normally
31149 if( p->l_type==F_RDLCK ){
31151 }else if( p->l_type==F_WRLCK ){
31153 }else if( p->l_type==F_UNLCK ){
31158 assert( p->l_whence==SEEK_SET );
31162 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
31163 (int)p->l_pid, s);
31164 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
31177 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
31197 /* On Android, ftruncate() always uses 32-bit offsets, even if
31258 ** a performance-critical path, so it is sufficient to put these
31283 ** Changes are made in-place. Return the new name length.
31285 ** The original filename is in z[0..n-1]. Return the number of
31290 while( n>1 && z[n-1]=='/' ){ n--; }
31299 while( j>0 && z[j-1]!='/' ){ j--; }
31300 if( j>0 ){ j--; }
31331 pNew->zCanonicalName = (char*)&pNew[1];
31332 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
31333 n = vxworksSimplifyName(pNew->zCanonicalName, n);
31340 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
31341 if( pCandidate->nName==n
31342 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
31345 pCandidate->nRef++;
31352 pNew->nRef = 1;
31353 pNew->nName = n;
31354 pNew->pNext = vxworksFileList;
31366 assert( pId->nRef>0 );
31367 pId->nRef--;
31368 if( pId->nRef==0 ){
31370 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
31372 *pp = pId->pNext;
31430 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
31458 ** on linux - with NPTL a lock created by thread A can override locks
31459 ** in thread B. But there is no way to know at compile-time which
31461 ** compile-time whether or not thread A can override locks on thread B.
31462 ** One has to do a run-time check to discover the behavior of the
31482 ** sizes ino_t at only 32-bits instead of 64-bits. (See
31483 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
31484 ** To work around this, always allocate 64-bits for the inode number.
31485 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
31528 ** This function - unixLogErrorAtLine(), is only ever called via the macro
31533 ** errno and, if possible, the human-readable equivalent from strerror() or
31539 ** failed (e.g. "unlink", "open") and the associated file-system path,
31553 ** the strerror() function to obtain the human-readable error message
31576 strerror_r(iErrno, aErr, sizeof(aErr)-1);
31582 /* Non-threadsafe build, use strerror(). */
31588 "os_unix.c:%d: (%d) %s(%s) - %s",
31611 pFile ? pFile->zPath : 0, lineno);
31616 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
31620 pFile->lastErrno = error;
31624 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
31627 unixInodeInfo *pInode = pFile->pInode;
31630 for(p=pInode->pUnused; p; p=pNext){
31631 pNext = p->pNext;
31632 robust_close(pFile, p->fd, __LINE__);
31635 pInode->pUnused = 0;
31645 unixInodeInfo *pInode = pFile->pInode;
31648 pInode->nRef--;
31649 if( pInode->nRef==0 ){
31650 assert( pInode->pShmNode==0 );
31652 if( pInode->pPrev ){
31653 assert( pInode->pPrev->pNext==pInode );
31654 pInode->pPrev->pNext = pInode->pNext;
31657 inodeList = pInode->pNext;
31659 if( pInode->pNext ){
31660 assert( pInode->pNext->pPrev==pInode );
31661 pInode->pNext->pPrev = pInode->pPrev;
31685 struct stat statbuf; /* Low-level file information */
31690 /* Get low-level information about the file that we can used to
31693 fd = pFile->h;
31698 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
31705 ** incorrectly for zero-size files. See ticket #3260. To work
31714 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
31731 fileId.pId = pFile->pId;
31736 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
31737 pInode = pInode->pNext;
31745 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
31746 pInode->nRef = 1;
31747 pInode->pNext = inodeList;
31748 pInode->pPrev = 0;
31749 if( inodeList ) inodeList->pPrev = pInode;
31752 pInode->nRef++;
31763 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
31766 return pFile->pInode!=0 &&
31767 (osStat(pFile->zPath, &buf)!=0
31768 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
31787 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
31789 rc = osFstat(pFile->h, &buf);
31791 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
31795 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
31799 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
31803 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
31812 ** to a non-zero value otherwise *pResOut is set to zero. The return value
31823 assert( pFile->eFileLock<=SHARED_LOCK );
31824 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
31827 if( pFile->pInode->eFileLock>SHARED_LOCK ){
31834 if( !reserved && !pFile->pInode->bProcessLock ){
31840 if( osFcntl(pFile->h, F_GETLK, &lock) ){
31850 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
31857 ** Attempt to set a system-lock on the file pFile. The lock is
31860 ** If the pFile was opened read/write from unix-excl, then the only lock
31863 ** operations become no-ops. Locking operations still happen internally,
31868 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
31869 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
31870 ** and is read-only.
31872 ** Zero is returned if the call completes successfully, or -1 if a call
31877 unixInodeInfo *pInode = pFile->pInode;
31880 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
31881 if( pInode->bProcessLock==0 ){
31883 assert( pInode->nLock==0 );
31888 rc = osFcntl(pFile->h, F_SETLK, &lock);
31890 pInode->bProcessLock = 1;
31891 pInode->nLock++;
31896 rc = osFcntl(pFile->h, F_SETLK, pLock);
31902 ** Lock the file with the lock specified by parameter eFileLock - one
31916 ** UNLOCKED -> SHARED
31917 ** SHARED -> RESERVED
31918 ** SHARED -> (PENDING) -> EXCLUSIVE
31919 ** RESERVED -> (PENDING) -> EXCLUSIVE
31920 ** PENDING -> EXCLUSIVE
31928 ** lock primitives (called read-locks and write-locks below, to avoid
31937 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
31938 ** byte'. If this is successful, 'shared byte range' is read-locked
31941 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
31943 ** Windows95 is now pretty much extinct, but this work-around for the
31944 ** lack of shared-locks on Windows95 lives on, for backwards
31948 ** A RESERVED lock is implemented by grabbing a write-lock on the
31952 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
31960 ** implemented by obtaining a write-lock on the entire 'shared byte
31961 ** range'. Since all other locks require a read-lock on one of the bytes
31972 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
31973 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
31974 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
31981 if( pFile->eFileLock>=eFileLock ){
31982 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
31992 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
31994 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
31996 /* This mutex is needed because pFile->pInode is shared across threads
31999 pInode = pFile->pInode;
32004 if( (pFile->eFileLock!=pInode->eFileLock &&
32005 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
32016 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
32018 assert( pFile->eFileLock==0 );
32019 assert( pInode->nShared>0 );
32020 pFile->eFileLock = SHARED_LOCK;
32021 pInode->nShared++;
32022 pInode->nLock++;
32034 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
32053 assert( pInode->nShared==0 );
32054 assert( pInode->eFileLock==0 );
32057 /* Now get the read-lock */
32081 pFile->eFileLock = SHARED_LOCK;
32082 pInode->nLock++;
32083 pInode->nShared = 1;
32085 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
32094 assert( 0!=pFile->eFileLock );
32117 /* Set up the transaction-counter change checking flags when
32123 && pFile->eFileLock<=SHARED_LOCK
32126 pFile->transCntrChng = 0;
32127 pFile->dbUpdate = 0;
32128 pFile->inNormalWrite = 1;
32134 pFile->eFileLock = eFileLock;
32135 pInode->eFileLock = eFileLock;
32137 pFile->eFileLock = PENDING_LOCK;
32138 pInode->eFileLock = PENDING_LOCK;
32143 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
32153 unixInodeInfo *pInode = pFile->pInode;
32154 UnixUnusedFd *p = pFile->pUnused;
32155 p->pNext = pInode->pUnused;
32156 pInode->pUnused = p;
32157 pFile->h = -1;
32158 pFile->pUnused = 0;
32166 ** the requested locking level, this routine is a no-op.
32181 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
32182 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
32186 if( pFile->eFileLock<=eFileLock ){
32190 pInode = pFile->pInode;
32191 assert( pInode->nShared!=0 );
32192 if( pFile->eFileLock>SHARED_LOCK ){
32193 assert( pInode->eFileLock==pFile->eFileLock );
32204 pFile->inNormalWrite = 0;
32208 ** before establishing the readlock - to avoid a race condition we downgrade
32224 off_t divSize = SHARED_SIZE - 1;
32230 if( unixFileLock(pFile, &lock)==(-1) ){
32240 if( unixFileLock(pFile, &lock)==(-1) ){
32251 lock.l_len = SHARED_SIZE-divSize;
32252 if( unixFileLock(pFile, &lock)==(-1) ){
32283 pInode->eFileLock = SHARED_LOCK;
32295 pInode->nShared--;
32296 if( pInode->nShared==0 ){
32301 pInode->eFileLock = NO_LOCK;
32305 pInode->eFileLock = NO_LOCK;
32306 pFile->eFileLock = NO_LOCK;
32314 pInode->nLock--;
32315 assert( pInode->nLock>=0 );
32316 if( pInode->nLock==0 ){
32323 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
32332 ** the requested locking level, this routine is a no-op.
32336 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
32361 if( pFile->h>=0 ){
32362 robust_close(pFile, pFile->h, __LINE__);
32363 pFile->h = -1;
32366 if( pFile->pId ){
32367 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
32368 osUnlink(pFile->pId->zCanonicalName);
32370 vxworksReleaseFileId(pFile->pId);
32371 pFile->pId = 0;
32375 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
32376 osUnlink(pFile->zPath);
32377 sqlite3_free(*(char**)&pFile->zPath);
32378 pFile->zPath = 0;
32381 OSTRACE(("CLOSE %-3d\n", pFile->h));
32382 OpenCounter(-1);
32383 sqlite3_free(pFile->pUnused);
32401 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
32402 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
32405 ** descriptor to pInode->pUnused list. It will be automatically closed
32420 ****************************** No-op Locking **********************************
32426 ** This locking mode is appropriate for use on read-only databases
32427 ** (ex: databases that are burned into CD-ROM, for example.) It can
32457 /******************* End of the no-op lock implementation *********************
32461 ************************* Begin dot-file Locking ******************************
32491 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32506 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
32507 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
32513 ** Lock the file with the lock specified by parameter eFileLock - one
32527 ** UNLOCKED -> SHARED
32528 ** SHARED -> RESERVED
32529 ** SHARED -> (PENDING) -> EXCLUSIVE
32530 ** RESERVED -> (PENDING) -> EXCLUSIVE
32531 ** PENDING -> EXCLUSIVE
32541 char *zLockFile = (char *)pFile->lockingContext;
32548 if( pFile->eFileLock > NO_LOCK ){
32549 pFile->eFileLock = eFileLock;
32576 pFile->eFileLock = eFileLock;
32585 ** the requested locking level, this routine is a no-op.
32591 char *zLockFile = (char *)pFile->lockingContext;
32595 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
32596 pFile->eFileLock, osGetpid(0)));
32599 /* no-op if possible */
32600 if( pFile->eFileLock==eFileLock ){
32608 pFile->eFileLock = SHARED_LOCK;
32625 pFile->eFileLock = NO_LOCK;
32636 sqlite3_free(pFile->lockingContext);
32639 /****************** End of the dot-file lock implementation *******************
32647 ** flock() locking is like dot-file locking in that the various
32648 ** fine-grain locking levels supported by SQLite are collapsed into
32675 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32688 if( pFile->eFileLock>SHARED_LOCK ){
32695 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
32698 lrc = robust_flock(pFile->h, LOCK_UN);
32717 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
32730 ** Lock the file with the lock specified by parameter eFileLock - one
32744 ** UNLOCKED -> SHARED
32745 ** SHARED -> RESERVED
32746 ** SHARED -> (PENDING) -> EXCLUSIVE
32747 ** RESERVED -> (PENDING) -> EXCLUSIVE
32748 ** PENDING -> EXCLUSIVE
32766 if (pFile->eFileLock > NO_LOCK) {
32767 pFile->eFileLock = eFileLock;
32773 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
32782 pFile->eFileLock = eFileLock;
32784 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
32800 ** the requested locking level, this routine is a no-op.
32806 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
32807 pFile->eFileLock, osGetpid(0)));
32810 /* no-op if possible */
32811 if( pFile->eFileLock==eFileLock ){
32817 pFile->eFileLock = eFileLock;
32822 if( robust_flock(pFile->h, LOCK_UN) ){
32828 pFile->eFileLock = NO_LOCK;
32852 ** Semaphore locking is like dot-lock and flock in that it really only
32862 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32875 if( pFile->eFileLock>SHARED_LOCK ){
32881 sem_t *pSem = pFile->pInode->pSem;
32883 if( sem_trywait(pSem)==-1 ){
32890 reserved = (pFile->eFileLock < SHARED_LOCK);
32897 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
32904 ** Lock the file with the lock specified by parameter eFileLock - one
32918 ** UNLOCKED -> SHARED
32919 ** SHARED -> RESERVED
32920 ** SHARED -> (PENDING) -> EXCLUSIVE
32921 ** RESERVED -> (PENDING) -> EXCLUSIVE
32922 ** PENDING -> EXCLUSIVE
32934 sem_t *pSem = pFile->pInode->pSem;
32939 if (pFile->eFileLock > NO_LOCK) {
32940 pFile->eFileLock = eFileLock;
32946 if( sem_trywait(pSem)==-1 ){
32952 pFile->eFileLock = eFileLock;
32963 ** the requested locking level, this routine is a no-op.
32967 sem_t *pSem = pFile->pInode->pSem;
32971 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
32972 pFile->eFileLock, osGetpid(0)));
32975 /* no-op if possible */
32976 if( pFile->eFileLock==eFileLock ){
32982 pFile->eFileLock = eFileLock;
32987 if ( sem_post(pSem)==-1 ) {
32995 pFile->eFileLock = NO_LOCK;
33027 ** on Apple Macintosh computers - both OS9 and OSX.
33029 ** Third-party implementations of AFP are available. But this code here
33056 ** This is a utility for setting or clearing a bit-range lock on an
33075 pb.fd = pFile->h;
33078 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
33081 if ( err==-1 ) {
33104 ** to a non-zero value otherwise *pResOut is set to zero. The return value
33116 context = (afpLockingContext *) pFile->lockingContext;
33117 if( context->reserved ){
33121 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
33124 if( pFile->pInode->eFileLock>SHARED_LOCK ){
33132 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
33136 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
33147 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
33154 ** Lock the file with the lock specified by parameter eFileLock - one
33168 ** UNLOCKED -> SHARED
33169 ** SHARED -> RESERVED
33170 ** SHARED -> (PENDING) -> EXCLUSIVE
33171 ** RESERVED -> (PENDING) -> EXCLUSIVE
33172 ** PENDING -> EXCLUSIVE
33180 unixInodeInfo *pInode = pFile->pInode;
33181 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
33184 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
33185 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
33186 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
33192 if( pFile->eFileLock>=eFileLock ){
33193 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
33203 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
33205 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
33207 /* This mutex is needed because pFile->pInode is shared across threads
33210 pInode = pFile->pInode;
33215 if( (pFile->eFileLock!=pInode->eFileLock &&
33216 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
33227 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
33229 assert( pFile->eFileLock==0 );
33230 assert( pInode->nShared>0 );
33231 pFile->eFileLock = SHARED_LOCK;
33232 pInode->nShared++;
33233 pInode->nLock++;
33242 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
33245 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
33259 assert( pInode->nShared==0 );
33260 assert( pInode->eFileLock==0 );
33263 /* Now get the read-lock SHARED_LOCK */
33266 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
33267 lrc1 = afpSetLock(context->dbPath, pFile,
33268 SHARED_FIRST+pInode->sharedByte, 1, 1);
33270 lrc1Errno = pFile->lastErrno;
33273 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
33285 pFile->eFileLock = SHARED_LOCK;
33286 pInode->nLock++;
33287 pInode->nShared = 1;
33289 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
33299 assert( 0!=pFile->eFileLock );
33300 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
33302 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
33304 context->reserved = 1;
33313 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
33314 pInode->sharedByte, 1, 0)) ){
33317 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
33319 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
33320 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
33338 pFile->eFileLock = eFileLock;
33339 pInode->eFileLock = eFileLock;
33341 pFile->eFileLock = PENDING_LOCK;
33342 pInode->eFileLock = PENDING_LOCK;
33347 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
33357 ** the requested locking level, this routine is a no-op.
33363 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
33366 int h = pFile->h;
33370 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
33371 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
33375 if( pFile->eFileLock<=eFileLock ){
33379 pInode = pFile->pInode;
33380 assert( pInode->nShared!=0 );
33381 if( pFile->eFileLock>SHARED_LOCK ){
33382 assert( pInode->eFileLock==pFile->eFileLock );
33384 SimulateIOError( h=(-1) )
33396 assert( pFile->inNormalWrite==0
33397 || pFile->dbUpdate==0
33398 || pFile->transCntrChng==1 );
33399 pFile->inNormalWrite = 0;
33402 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
33403 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
33404 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
33405 /* only re-establish the shared lock if necessary */
33406 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
33407 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
33412 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
33413 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
33415 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
33416 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
33418 context->reserved = 0;
33421 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
33422 pInode->eFileLock = SHARED_LOCK;
33431 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
33432 pInode->nShared--;
33433 if( pInode->nShared==0 ){
33435 SimulateIOError( h=(-1) )
33438 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
33441 pInode->eFileLock = NO_LOCK;
33442 pFile->eFileLock = NO_LOCK;
33446 pInode->nLock--;
33447 assert( pInode->nLock>=0 );
33448 if( pInode->nLock==0 ){
33455 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
33468 if( pFile->pInode && pFile->pInode->nLock ){
33471 ** descriptor to pInode->aPending. It will be automatically closed when
33477 sqlite3_free(pFile->lockingContext);
33487 ** is available. If you don't compile for a mac, then the "unix-afp"
33502 ** the requested locking level, this routine is a no-op.
33518 **************** Non-locking sqlite3_file methods *****************************
33548 assert( id->h>2 );
33551 got = osPread(id->h, pBuf, cnt, offset);
33552 SimulateIOError( got = -1 );
33554 got = osPread64(id->h, pBuf, cnt, offset);
33555 SimulateIOError( got = -1 );
33557 newOffset = lseek(id->h, offset, SEEK_SET);
33558 SimulateIOError( newOffset = -1 );
33561 return -1;
33563 got = osRead(id->h, pBuf, cnt);
33572 cnt -= got;
33579 OSTRACE(("READ %-3d %5d %7lld %llu\n",
33580 id->h, got+prior, offset-prior, TIMER_ELAPSED));
33601 /* If this is a database file (not a journal, master-journal or temp
33604 assert( pFile->pUnused==0
33613 if( offset<pFile->mmapSize ){
33614 if( offset+amt <= pFile->mmapSize ){
33615 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
33618 int nCopy = pFile->mmapSize - offset;
33619 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
33621 amt -= nCopy;
33635 /* Unread parts of the buffer must be zero-filled */
33636 memset(&((char*)pBuf)[got], 0, amt-got);
33642 ** Attempt to seek the file-descriptor passed as the first argument to
33644 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
33670 SimulateIOError( iSeek = -1 );
33672 rc = -1;
33680 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
33688 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
33695 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
33714 /* If this is a database file (not a journal, master-journal or temp
33717 assert( pFile->pUnused==0
33725 ** doing a hot-journal rollback or a write to some file other than a
33730 if( pFile->inNormalWrite ){
33731 pFile->dbUpdate = 1; /* The database has been modified */
33738 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
33739 pFile->transCntrChng = 1; /* The transaction counter has changed */
33748 if( offset<pFile->mmapSize ){
33749 if( offset+amt <= pFile->mmapSize ){
33750 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
33753 int nCopy = pFile->mmapSize - offset;
33754 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
33756 amt -= nCopy;
33763 amt -= wrote;
33767 SimulateIOError(( wrote=(-1), amt=1 ));
33771 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
33796 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
33866 ** no-op. But go ahead and call fstat() to validate the file
33899 if( rc==-1 && errno==ENOTSUP ){
33905 if( OS_VXWORKS && rc!= -1 ){
33918 ** The directory file descriptor is used for only one thing - to
33927 ** replace this routine with a harmless no-op. To make this routine
33928 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
33936 int fd = -1;
33940 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
33949 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
33964 ** has been created by fsync-ing the directory that contains the file.
33969 ** will not roll back - possibly leading to database corruption.
33989 OSTRACE(("SYNC %-3d\n", pFile->h));
33990 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
33994 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
33998 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
34001 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
34003 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
34005 rc = osOpenDirectory(pFile->zPath, &dirfd);
34013 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
34027 /* If the user has configured a chunk-size for this file, truncate the
34032 if( pFile->szChunk>0 ){
34033 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
34036 rc = robust_ftruncate(pFile->h, nByte);
34039 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
34043 ** doing a hot-journal rollback or a write to some file other than a
34046 ** when restoring a database using the backup API from a zero-length
34049 if( pFile->inNormalWrite && nByte==0 ){
34050 pFile->transCntrChng = 1;
34059 if( nByte<pFile->mmapSize ){
34060 pFile->mmapSize = nByte;
34075 rc = osFstat(((unixFile*)id)->h, &buf);
34083 /* When opening a zero-size database, the findInodeInfo() procedure
34085 ** in the OS-X msdos filesystem. In order to avoid problems with upper
34097 ** Handler for proxy-locking file-control verbs. Defined below in the
34105 ** file-control operation. Enlarge the database to nBytes in size
34106 ** (rounded up to the next chunk-size). If the database is already
34107 ** nBytes or larger, this routine is a no-op.
34110 if( pFile->szChunk>0 ){
34114 if( osFstat(pFile->h, &buf) ){
34118 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
34127 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
34134 ** at offset (nSize-1), to set the size of the file correctly.
34138 int nBlk = buf.st_blksize; /* File-system block size */
34142 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
34145 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
34146 if( iWrite>=nSize ) iWrite = nSize - 1;
34155 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
34157 if( pFile->szChunk<=0 ){
34158 if( robust_ftruncate(pFile->h, nByte) ){
34160 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
34174 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34176 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34180 *pArg = (pFile->ctrlFlags & mask)!=0;
34182 pFile->ctrlFlags &= ~mask;
34184 pFile->ctrlFlags |= mask;
34198 *(int*)pArg = pFile->eFileLock;
34202 *(int*)pArg = pFile->lastErrno;
34206 pFile->szChunk = *(int *)pArg;
34225 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
34229 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
34231 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
34247 *(i64*)pArg = pFile->mmapSizeMax;
34248 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
34249 pFile->mmapSizeMax = newLimit;
34250 if( pFile->mmapSize>0 ){
34252 rc = unixMapfile(pFile, -1);
34265 ((unixFile*)id)->dbUpdate = 0;
34285 ** if two files are created in the same file-system directory (i.e.
34304 if( pFile->sectorSize == 0 ){
34307 /* Set defaults for non-supported filesystems */
34308 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
34309 pFile->deviceCharacteristics = 0;
34310 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
34311 return pFile->sectorSize;
34315 pFile->sectorSize = fsInfo.f_bsize;
34316 pFile->deviceCharacteristics =
34324 pFile->sectorSize = fsInfo.f_bsize;
34325 pFile->deviceCharacteristics =
34327 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
34334 pFile->sectorSize = fsInfo.f_bsize;
34335 pFile->deviceCharacteristics =
34343 pFile->sectorSize = fsInfo.f_bsize;
34344 pFile->deviceCharacteristics =
34346 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
34351 pFile->sectorSize = fsInfo.f_bsize;
34352 pFile->deviceCharacteristics =
34354 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
34359 pFile->deviceCharacteristics =
34368 if( pFile->sectorSize % 512 != 0 ){
34369 pFile->deviceCharacteristics = 0;
34370 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
34372 return pFile->sectorSize;
34382 ** words, after a power-loss event, parts of the file that were never
34383 ** written might end up being altered.) However, non-PSOW behavior is very,
34386 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
34393 if( p->sectorSize==0 ) unixSectorSize(id);
34394 rc = p->deviceCharacteristics;
34396 if( p->ctrlFlags & UNIXFILE_PSOW ){
34427 ** When multiple threads all reference the same wal-index, each thread
34429 ** of this unixShmNode object. In other words, each wal-index is opened
34444 ** The following fields are read-only after the object is created:
34458 int szRegion; /* Size of shared-memory regions */
34460 u8 isReadonly; /* True if read-only */
34461 char **apRegion; /* Array of mapped shared-memory regions */
34476 ** are read-only thereafter:
34481 ** All other fields are read/write. The unixShm.pFile->mutex must be held
34500 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
34502 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
34511 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
34516 pShmNode = pFile->pInode->pShmNode;
34517 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
34525 if( pShmNode->h>=0 ){
34533 rc = osFcntl(pShmNode->h, F_SETLK, &f);
34534 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
34540 OSTRACE(("SHM-LOCK "));
34541 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
34545 pShmNode->exclMask &= ~mask;
34546 pShmNode->sharedMask &= ~mask;
34548 OSTRACE(("read-lock %d ok", ofst));
34549 pShmNode->exclMask &= ~mask;
34550 pShmNode->sharedMask |= mask;
34553 OSTRACE(("write-lock %d ok", ofst));
34554 pShmNode->exclMask |= mask;
34555 pShmNode->sharedMask &= ~mask;
34561 OSTRACE(("read-lock failed"));
34564 OSTRACE(("write-lock %d failed", ofst));
34567 OSTRACE((" - afterwards %03x,%03x\n",
34568 pShmNode->sharedMask, pShmNode->exclMask));
34578 ** current system page-size.
34581 ** to use 64KB pages - in this case each mapping must cover at least two
34587 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
34595 ** This is not a VFS shared-memory method; it is a utility function called
34596 ** by VFS shared-memory methods.
34599 unixShmNode *p = pFd->pInode->pShmNode;
34601 if( p && ALWAYS(p->nRef==0) ){
34604 assert( p->pInode==pFd->pInode );
34605 sqlite3_mutex_free(p->mutex);
34606 for(i=0; i<p->nRegion; i+=nShmPerMap){
34607 if( p->h>=0 ){
34608 osMunmap(p->apRegion[i], p->szRegion);
34610 sqlite3_free(p->apRegion[i]);
34613 sqlite3_free(p->apRegion);
34614 if( p->h>=0 ){
34615 robust_close(pFd, p->h, __LINE__);
34616 p->h = -1;
34618 p->pInode->pShmNode = 0;
34624 ** Open a shared-memory area associated with open database file pDbFd.
34627 ** The file used to implement shared-memory is in the same directory
34629 ** file with the "-shm" suffix added. For example, if the database file
34631 ** for shared memory will be called "/home/user1/config.db-shm".
34637 ** database to end up using different files for shared memory -
34638 ** meaning that their memory would not really be shared - resulting
34640 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
34641 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
34645 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
34648 ** When opening a new shared-memory file, if no other instances of that
34652 ** If the original database file (pDbFd) is using the "unix-excl" VFS
34670 assert( pDbFd->pShm==0 );
34676 pInode = pDbFd->pInode;
34677 pShmNode = pInode->pShmNode;
34681 const char *zBasePath = pDbFd->zPath;
34685 ** a new *-shm file is created, an attempt will be made to create it
34688 if( osFstat(pDbFd->h, &sStat) ){
34704 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
34707 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
34710 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
34711 sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
34713 pShmNode->h = -1;
34714 pDbFd->pInode->pShmNode = pShmNode;
34715 pShmNode->pInode = pDbFd->pInode;
34717 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34718 if( pShmNode->mutex==0 ){
34724 if( pInode->bProcessLock==0 ){
34726 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
34728 pShmNode->isReadonly = 1;
34730 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
34731 if( pShmNode->h<0 ){
34740 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
34742 /* Check to see if another process is holding the dead-man switch.
34747 if( robust_ftruncate(pShmNode->h, 0) ){
34759 p->pShmNode = pShmNode;
34761 p->id = pShmNode->nextShmId++;
34763 pShmNode->nRef++;
34764 pDbFd->pShm = p;
34771 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
34774 sqlite3_mutex_enter(pShmNode->mutex);
34775 p->pNext = pShmNode->pFirst;
34776 pShmNode->pFirst = p;
34777 sqlite3_mutex_leave(pShmNode->mutex);
34790 ** shared-memory associated with the database file fd. Shared-memory regions
34791 ** are numbered starting from zero. Each shared-memory region is szRegion
34796 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
34799 ** bExtend is non-zero and the requested shared-memory region has not yet
34802 ** If the shared-memory region has already been allocated or is allocated by
34821 /* If the shared-memory file has not yet been opened, open it now. */
34822 if( pDbFd->pShm==0 ){
34827 p = pDbFd->pShm;
34828 pShmNode = p->pShmNode;
34829 sqlite3_mutex_enter(pShmNode->mutex);
34830 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
34831 assert( pShmNode->pInode==pDbFd->pInode );
34832 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
34833 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
34838 if( pShmNode->nRegion<nReqRegion ){
34843 pShmNode->szRegion = szRegion;
34845 if( pShmNode->h>=0 ){
34847 ** Check to see if it has been allocated (i.e. if the wal-index file is
34850 if( osFstat(pShmNode->h, &sStat) ){
34878 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
34879 const char *zFile = pShmNode->zFilename;
34890 pShmNode->apRegion, nReqRegion*sizeof(char *)
34896 pShmNode->apRegion = apNew;
34897 while( pShmNode->nRegion<nReqRegion ){
34901 if( pShmNode->h>=0 ){
34903 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
34904 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
34907 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
34920 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
34922 pShmNode->nRegion += nShmPerMap;
34927 if( pShmNode->nRegion>iRegion ){
34928 *pp = pShmNode->apRegion[iRegion];
34932 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
34933 sqlite3_mutex_leave(pShmNode->mutex);
34938 ** Change the lock state for a shared-memory segment.
34952 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
34954 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
34958 assert( pShmNode==pDbFd->pInode->pShmNode );
34959 assert( pShmNode->pInode==pDbFd->pInode );
34967 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
34968 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
34970 mask = (1<<(ofst+n)) - (1<<ofst);
34972 sqlite3_mutex_enter(pShmNode->mutex);
34977 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34979 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
34980 allMask |= pX->sharedMask;
34983 /* Unlock the system-level locks */
34992 p->exclMask &= ~mask;
34993 p->sharedMask &= ~mask;
35002 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
35003 if( (pX->exclMask & mask)!=0 ){
35007 allShared |= pX->sharedMask;
35021 p->sharedMask |= mask;
35027 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
35028 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
35040 assert( (p->sharedMask & mask)==0 );
35041 p->exclMask |= mask;
35045 sqlite3_mutex_leave(pShmNode->mutex);
35046 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
35047 p->id, osGetpid(0), p->sharedMask, p->exclMask));
35061 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
35067 ** Close a connection to shared-memory. Delete the underlying
35071 ** routine is a harmless no-op.
35075 int deleteFlag /* Delete shared-memory if true */
35078 unixShmNode *pShmNode; /* The underlying shared-memory file */
35083 p = pDbFd->pShm;
35085 pShmNode = p->pShmNode;
35087 assert( pShmNode==pDbFd->pInode->pShmNode );
35088 assert( pShmNode->pInode==pDbFd->pInode );
35092 sqlite3_mutex_enter(pShmNode->mutex);
35093 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
35094 *pp = p->pNext;
35098 pDbFd->pShm = 0;
35099 sqlite3_mutex_leave(pShmNode->mutex);
35101 /* If pShmNode->nRef has reached 0, then close the underlying
35102 ** shared-memory file, too */
35104 assert( pShmNode->nRef>0 );
35105 pShmNode->nRef--;
35106 if( pShmNode->nRef==0 ){
35107 if( deleteFlag && pShmNode->h>=0 ){
35108 osUnlink(pShmNode->zFilename);
35130 assert( pFd->nFetchOut==0 );
35131 if( pFd->pMapRegion ){
35132 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
35133 pFd->pMapRegion = 0;
35134 pFd->mmapSize = 0;
35135 pFd->mmapSizeActual = 0;
35159 int h = pFd->h; /* File descriptor open on db file */
35160 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
35161 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
35165 assert( pFd->nFetchOut==0 );
35166 assert( nNew>pFd->mmapSize );
35167 assert( nNew<=pFd->mmapSizeMax );
35169 assert( pFd->mmapSizeActual>=pFd->mmapSize );
35173 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
35178 i64 nReuse = pFd->mmapSize;
35181 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
35187 osMunmap(pReq, nOrig-nReuse);
35194 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
35197 osMunmap(pNew, nNew - nReuse);
35219 unixLogError(SQLITE_OK, zErr, pFd->zPath);
35224 pFd->mmapSizeMax = 0;
35226 pFd->pMapRegion = (void *)pNew;
35227 pFd->mmapSize = pFd->mmapSizeActual = nNew;
35231 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35234 ** outstanding xFetch() references to it, this function is a no-op.
35236 ** If parameter nByte is non-negative, then it is the requested size of
35247 assert( nMap>=0 || pFd->nFetchOut==0 );
35248 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
35249 if( pFd->nFetchOut>0 ) return SQLITE_OK;
35252 struct stat statbuf; /* Low-level file information */
35253 if( osFstat(pFd->h, &statbuf) ){
35258 if( nMap>pFd->mmapSizeMax ){
35259 nMap = pFd->mmapSizeMax;
35262 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
35263 if( nMap!=pFd->mmapSize ){
35290 if( pFd->mmapSizeMax>0 ){
35291 if( pFd->pMapRegion==0 ){
35292 int rc = unixMapfile(pFd, -1);
35295 if( pFd->mmapSize >= iOff+nAmt ){
35296 *pp = &((u8 *)pFd->pMapRegion)[iOff];
35297 pFd->nFetchOut++;
35305 ** If the third argument is non-NULL, then this function releases a
35322 assert( (p==0)==(pFd->nFetchOut==0) );
35325 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
35328 pFd->nFetchOut--;
35333 assert( pFd->nFetchOut>=0 );
35351 ** of "finder" functions. A finder-function is used to locate the appropriate
35354 ** the correct finder-function for that VFS.
35357 ** object. The only interesting finder-function is autolockIoFinder, which
35361 ** For finder-function F, two objects are created:
35363 ** (1) The real finder-function named "FImpt()".
35370 ** directly at the finder-function since C90 rules prevent a void*
35487 ** The proxy locking method is a "super-method" in the sense that it
35489 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
35558 if( statfs(filePath, &fsInfo) != -1 ){
35570 ** Test byte-range lock using fcntl(). If the call succeeds,
35571 ** assume that the file-system supports POSIX style locks.
35577 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
35617 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
35655 assert( pNew->pInode==NULL );
35663 || pVfs->pAppData==(void*)&autolockIoFinder );
35671 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
35672 pNew->h = h;
35673 pNew->pVfs = pVfs;
35674 pNew->zPath = zFilename;
35675 pNew->ctrlFlags = (u8)ctrlFlags;
35677 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
35681 pNew->ctrlFlags |= UNIXFILE_PSOW;
35683 if( strcmp(pVfs->zName,"unix-excl")==0 ){
35684 pNew->ctrlFlags |= UNIXFILE_EXCL;
35688 pNew->pId = vxworksFindFileId(zFilename);
35689 if( pNew->pId==0 ){
35698 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
35703 pNew->lockingContext = (void*)zFilename;
35713 rc = findInodeInfo(pNew, &pNew->pInode);
35726 ** handle h - as it is guaranteed that no posix locks will be released
35734 h = -1;
35745 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
35752 pCtx->dbPath = zFilename;
35753 pCtx->reserved = 0;
35756 rc = findInodeInfo(pNew, &pNew->pInode);
35758 sqlite3_free(pNew->lockingContext);
35760 h = -1;
35781 pNew->lockingContext = zLockFile;
35790 rc = findInodeInfo(pNew, &pNew->pInode);
35791 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
35792 char *zSemName = pNew->pInode->aSemName;
35795 pNew->pId->zCanonicalName);
35798 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
35799 if( pNew->pInode->pSem == SEM_FAILED ){
35801 pNew->pInode->aSemName[0] = '\0';
35812 h = -1;
35814 pNew->ctrlFlags |= UNIXFILE_DELETE;
35820 pNew->pMethod = pLockingStyle;
35863 ** pVfs->mxPathname bytes.
35869 /* It's odd to simulate an io-error here, but really this is just
35870 ** using the io-error infrastructure to test that SQLite handles this
35882 zBuf[nBuf-2] = 0;
35885 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
35892 ** Routine to transform a unixFile into a proxy-locking unixFile.
35893 ** Implementation in the proxy-lock division, but used by unixOpen()
35901 ** file (not a journal or master-journal file) identified by pathname
35907 ** other file descriptor open on the same file is holding a file-lock.
35913 ** such file descriptor is located, -1 is returned.
35929 ** ignored and -1 is returned. The caller will try to open a new file
35939 while( pInode && (pInode->fileId.dev!=sStat.st_dev
35940 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
35941 pInode = pInode->pNext;
35945 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
35948 *pp = pUnused->pNext;
35989 ** this function queries the file-system for the permissions on the
36018 ** "<path to db>-journal"
36019 ** "<path to db>-wal"
36020 ** "<path to db>-journalNN"
36021 ** "<path to db>-walNN"
36026 nDb = sqlite3Strlen30(zPath) - 1;
36027 while( zPath[nDb]!='-' ){
36030 ** is guaranteed to contain a '-' character. */
36035 ** a '-' character. So check for that case and return early. */
36038 nDb--;
36071 ** ReadWrite() -> (READWRITE | CREATE)
36072 ** ReadOnly() -> (READONLY)
36073 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
36075 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
36089 int fd = -1; /* File descriptor returned by open() */
36108 /* If creating a master or main-file journal, this function will open
36109 ** a file-descriptor on the directory too. The first time unixSync()
36143 /* Assert that the upper layer has set one of the "file-type" flags. */
36166 fd = pUnused->fd;
36173 p->pUnused = pUnused;
36175 /* Database filenames are double-zero terminated if they are not
36183 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
36189 /* Generated temporary filenames are always double-zero terminated
36210 assert( !p->pUnused );
36215 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
36218 /* Failed to open the file for read/write access. Try read-only. */
36244 if( p->pUnused ){
36245 p->pUnused->fd = fd;
36246 p->pUnused->flags = flags;
36264 p->openFlags = openFlags;
36269 if( fstatfs(fd, &fsInfo) == -1 ){
36274 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
36275 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
36277 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
36278 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
36294 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
36299 ** never use proxy, NULL means use proxy for non-local files only. */
36312 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
36327 sqlite3_free(p->pUnused);
36345 if( osUnlink(zPath)==(-1) ){
36419 if( osGetcwd(zOut, nOut-2)==0 ){
36426 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
36431 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
36437 ** is stored as a nul-terminated string in the buffer pointed to by
36441 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
36459 assert( pVfs->mxPathname==MAX_PATHNAME );
36462 /* It's odd to simulate an io-error here, but really this is just
36463 ** using the io-error infrastructure to test that SQLite handles this
36492 nByte = osReadlink(zIn, zDel, nOut-1);
36498 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
36558 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
36561 ** use dlsym() with -pedantic-errors?
36569 ** This work-around is unlikely to work correctly on any system where
36598 ** errors. The reports issued by valgrind are incorrect - we would
36600 ** uninitialized space in zBuf - but valgrind errors tend to worry
36662 ** The following variable, if set to a non-zero value, is interpreted as
36726 ** low-level error message when operating-system problems come up
36745 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
36747 ** meta-layer over top of the primitive locking implemented above. For
36750 ** been defined - so that the primitive locking methods are available
36755 ** The default locking schemes in SQLite use byte-range locks on the
36760 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
36761 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
36768 ** SHARED_RANGE 0x40000002 -> 0x40000200
36772 ** the read cache when byte-range locks are present. Enabling the read
36775 ** close-to-open semantics for ensuring cache coherency
36779 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
36788 ** -----------------
36820 ** -----------------------
36830 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
36831 ** by taking an sqlite-style shared lock on the conch file, reading the
36835 ** is patterned after the database file name as ".<databasename>-conch".
36843 ** The proxy file - a single-byte file used for all advisory file locks
36859 ** ---------------------
36863 ** Database files accessed on non-local file systems are
36908 int conchHeld; /* 1 if the conch is held, -1 if lockless */
36941 if( lPath[len-1]!='/' ){
36970 if( lockPath[i] == '/' && (i - start > 0) ){
36972 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
36973 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
37005 int fd = -1;
37014 ** 2. if that fails, and this is a lock file (not-conch), try creating
37016 ** 3. if that fails, try to open the file read-only
37021 fd = pUnused->fd;
37062 pNew->openFlags = openFlags;
37066 pUnused->fd = fd;
37067 pUnused->flags = openFlags;
37068 pNew->pUnused = pUnused;
37126 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
37138 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37139 unixFile *conchFile = pCtx->conchFile;
37142 char *cPath = pCtx->conchFilePath;
37146 int fd = -1;
37147 int rc = -1;
37150 /* create a new path by replace the trailing '-conch' with '-break' */
37153 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
37158 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
37179 robust_close(pFile, conchFile->h, __LINE__);
37180 conchFile->h = fd;
37181 conchFile->openFlags = O_RDWR | O_CREAT;
37198 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37199 unixFile *conchFile = pCtx->conchFile;
37206 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
37216 if( osFstat(conchFile->h, &buf) ){
37235 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
37257 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
37260 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
37270 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
37276 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37278 if( pCtx->conchHeld!=0 ){
37281 unixFile *conchFile = pCtx->conchFile;
37294 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
37295 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
37311 storeLastErrno(pFile, conchFile->lastErrno);
37323 ** retry with a new auto-generated path
37331 if( !pCtx->lockProxyPath ){
37332 /* for auto-named local lock file, just check the host ID and we'll
37336 size_t pathLen = (readLen - PROXY_PATHINDEX);
37339 pathLen=MAXPATHLEN-1;
37349 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
37350 readLen-PROXY_PATHINDEX)
37358 if( (conchFile->openFlags&O_RDWR) == 0 ){
37364 if( !pCtx->lockProxyPath ){
37365 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
37374 futimes(conchFile->h, NULL);
37376 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
37392 if( pCtx->lockProxyPath!=NULL ){
37393 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
37399 robust_ftruncate(conchFile->h, writeSize);
37401 full_fsync(conchFile->h,0,0);
37407 int err = osFstat(pFile->h, &buf);
37413 osFchmod(conchFile->h, cmode);
37416 rc = osFchmod(conchFile->h, cmode);
37417 }while( rc==(-1) && errno==EINTR );
37433 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
37436 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
37437 if( rc==SQLITE_OK && pFile->openFlags ){
37439 if( pFile->h>=0 ){
37440 robust_close(pFile, pFile->h, __LINE__);
37442 pFile->h = -1;
37443 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
37446 pFile->h = fd;
37452 if( rc==SQLITE_OK && !pCtx->lockProxy ){
37453 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
37454 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
37457 ** so try again via auto-naming
37469 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
37470 if( !pCtx->lockProxyPath ){
37476 pCtx->conchHeld = 1;
37478 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
37480 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
37481 afpCtx->dbPath = pCtx->lockProxyPath;
37484 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
37486 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
37489 } while (1); /* in case we need to retry the :auto: lock file -
37502 pCtx = (proxyLockingContext *)pFile->lockingContext;
37503 conchFile = pCtx->conchFile;
37504 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
37505 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
37507 if( pCtx->conchHeld>0 ){
37508 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
37510 pCtx->conchHeld = 0;
37511 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
37529 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
37541 for( i=(len-1); i>=0; i-- ){
37553 /* append the "-conch" suffix to the file */
37554 memcpy(&conchPath[i+1], "-conch", 7);
37561 /* Takes a fully configured proxy locking-style unix file and switches
37565 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
37566 char *oldPath = pCtx->lockProxyPath;
37569 if( pFile->eFileLock!=NO_LOCK ){
37578 unixFile *lockProxy = pCtx->lockProxy;
37579 pCtx->lockProxy=NULL;
37580 pCtx->conchHeld = 0;
37582 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
37587 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
37602 if( pFile->pMethod == &afpIoMethods ){
37605 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
37606 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
37610 if( pFile->pMethod == &dotlockIoMethods ){
37613 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
37614 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
37617 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
37618 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
37628 ** ->lockingContext
37629 ** ->pMethod
37637 if( pFile->eFileLock!=NO_LOCK ){
37647 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
37656 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
37658 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
37659 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
37661 ** (c) the file system is read-only, then enable no-locking access.
37669 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
37671 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
37676 pCtx->conchHeld = -1; /* read only FS/ lockless */
37682 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
37686 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
37687 if( pCtx->dbPath==NULL ){
37695 pCtx->oldLockingContext = pFile->lockingContext;
37696 pFile->lockingContext = pCtx;
37697 pCtx->pOldMethod = pFile->pMethod;
37698 pFile->pMethod = &proxyIoMethods;
37700 if( pCtx->conchFile ){
37701 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
37702 sqlite3_free(pCtx->conchFile);
37704 sqlite3DbFree(0, pCtx->lockProxyPath);
37705 sqlite3_free(pCtx->conchFilePath);
37708 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
37722 if( pFile->pMethod == &proxyIoMethods ){
37723 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
37725 if( pCtx->lockProxyPath ){
37726 *(const char **)pArg = pCtx->lockProxyPath;
37738 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
37741 /* turn off proxy locking - not supported. If support is added for
37747 /* turn off proxy locking - already off - NOOP */
37754 (proxyLockingContext*)pFile->lockingContext;
37756 || (pCtx->lockProxyPath &&
37757 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
37780 ** above this point are all utilities. The lock-related methods of the
37781 ** proxy-locking sqlite3_io_method object follow.
37788 ** to a non-zero value otherwise *pResOut is set to zero. The return value
37795 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37796 if( pCtx->conchHeld>0 ){
37797 unixFile *proxy = pCtx->lockProxy;
37798 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
37807 ** Lock the file with the lock specified by parameter eFileLock - one
37821 ** UNLOCKED -> SHARED
37822 ** SHARED -> RESERVED
37823 ** SHARED -> (PENDING) -> EXCLUSIVE
37824 ** RESERVED -> (PENDING) -> EXCLUSIVE
37825 ** PENDING -> EXCLUSIVE
37834 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37835 if( pCtx->conchHeld>0 ){
37836 unixFile *proxy = pCtx->lockProxy;
37837 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
37838 pFile->eFileLock = proxy->eFileLock;
37852 ** the requested locking level, this routine is a no-op.
37858 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37859 if( pCtx->conchHeld>0 ){
37860 unixFile *proxy = pCtx->lockProxy;
37861 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
37862 pFile->eFileLock = proxy->eFileLock;
37876 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
37877 unixFile *lockProxy = pCtx->lockProxy;
37878 unixFile *conchFile = pCtx->conchFile;
37882 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
37884 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
37887 pCtx->lockProxy = 0;
37890 if( pCtx->conchHeld ){
37894 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
37898 sqlite3DbFree(0, pCtx->lockProxyPath);
37899 sqlite3_free(pCtx->conchFilePath);
37900 sqlite3DbFree(0, pCtx->dbPath);
37902 pFile->lockingContext = pCtx->oldLockingContext;
37903 pFile->pMethod = pCtx->pOldMethod;
37905 return pFile->pMethod->xClose(id);
37925 ** This routine registers all VFS implementations for unix-like operating
37942 ** when compiling with -pedantic-errors on GCC.)
37945 ** finder-function. The finder-function returns a pointer to the
37948 ** macro for addition information on finder-functions.
37996 UNIXVFS("unix-none", nolockIoFinder ),
37997 UNIXVFS("unix-dotfile", dotlockIoFinder ),
37998 UNIXVFS("unix-excl", posixIoFinder ),
38000 UNIXVFS("unix-namedsem", semIoFinder ),
38003 UNIXVFS("unix-posix", posixIoFinder ),
38006 UNIXVFS("unix-flock", flockIoFinder ),
38009 UNIXVFS("unix-afp", afpIoFinder ),
38010 UNIXVFS("unix-nfs", nfsIoFinder ),
38011 UNIXVFS("unix-proxy", proxyIoFinder ),
38016 /* Double-check that the aSyscall[] array has been constructed
38032 ** This routine is a no-op for unix.
38077 ** all of the platform-specific files (os_*.c) and is #included into those
38089 ** switch. The following code should catch this problem at compile-time.
38103 ** high-performance timing routines.
38119 ** This file contains inline asm code for retrieving "high-performance"
38126 ** The following routine only works on pentium-class (or newer) processors.
38128 ** processor and returns that value. This can be used for high-res
38199 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
38223 || sqlite3_io_error_pending-- == 1 ) \
38238 sqlite3_diskfull_pending--; \
38283 ** based on the sub-platform)?
38291 ** based on the sub-platform)?
38379 ** characters, so we allocate 4 bytes per character assuming worst-case of
38380 ** 4-bytes-per-character for UTF8.
38403 ** Returns non-zero if the character should be treated as a directory
38467 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
38480 typedef struct winShm winShm; /* A connection to shared-memory */
38481 typedef struct winShmNode winShmNode; /* A region of shared-memory */
38540 BOOL bNoLock; /* Non-zero if locking is disabled. */
38554 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
38580 * If this is non-zero, an isolated heap will be created by the native Win32
38586 * WARNING: It is important to note that when this setting is non-zero and the
38598 * This is the maximum possible initial size of the Win32-specific heap, in
38606 * This is the extra space for the initial size of the Win32-specific heap,
38619 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
38626 * Win32-specific heap. It cannot be negative.
38632 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
38638 * initial size of the Win32-specific heap to exceed the maximum amount
38647 * The initial size of the Win32-specific heap. This value may be zero.
38656 * The maximum size of the Win32-specific heap. This value may be zero.
38672 ** The winMemData structure stores information required by the Win32-specific
38756 ** Many system calls are accessed through pointer-to-functions so that
38812 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
39373 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
39485 int i = -1;
39489 for(i=0; i<ArraySize(aSyscall)-1; i++){
39504 ** "pnLargest" argument, if non-zero, will be used to return the size of the
39594 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
39595 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
39596 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
39662 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
39690 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
39715 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
39810 if( n==(SIZE_T)-1 ){
39832 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
39833 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
39836 if( !pWinMemData->hHeap ){
39844 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
39846 if( !pWinMemData->hHeap ){
39853 pWinMemData->bOwned = TRUE;
39854 assert( pWinMemData->bOwned );
39857 pWinMemData->hHeap = osGetProcessHeap();
39858 if( !pWinMemData->hHeap ){
39863 pWinMemData->bOwned = FALSE;
39864 assert( !pWinMemData->bOwned );
39866 assert( pWinMemData->hHeap!=0 );
39867 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
39869 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
39881 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
39882 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
39884 if( pWinMemData->hHeap ){
39885 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
39887 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
39889 if( pWinMemData->bOwned ){
39890 if( !osHeapDestroy(pWinMemData->hHeap) ){
39892 osGetLastError(), (void*)pWinMemData->hHeap);
39894 pWinMemData->bOwned = FALSE;
39896 pWinMemData->hHeap = NULL;
39901 ** Populate the low-level memory allocation function pointers in
39928 ** Convert a UTF-8 string to Microsoft Unicode.
39936 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
39944 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
39954 ** Convert a Microsoft Unicode string to UTF-8.
39962 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
39970 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
39990 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
39999 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
40009 ** Convert a Microsoft Unicode string to a multi-byte character string,
40019 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
40027 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
40037 ** Convert a multi-byte character string to UTF-8.
40055 ** Convert a UTF-8 string to a multi-byte character string.
40207 ** is zero if the error message fits in the buffer, or non-zero
40287 ** This function - winLogErrorAtLine() - is only ever called via the macro
40292 ** error code and, if possible, the human-readable equivalent from
40298 ** failed and the associated file-system path, if any.
40318 "os_win.c:%d: (%lu) %s(%s) - %s",
40327 ** will be retried following a locking error - probably caused by
40344 ** non-zero if the error code is transient in nature and the operation
40432 y.tm_year = pTm.wYear - 1900;
40433 y.tm_mon = pTm.wMonth - 1;
40447 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
40481 memset(&pFile->local, 0, sizeof(pFile->local));
40491 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
40492 if (!pFile->hMutex){
40493 pFile->lastErrno = osGetLastError();
40495 return winLogError(SQLITE_IOERR, pFile->lastErrno,
40500 winceMutexAcquire(pFile->hMutex);
40503 ** case-sensitive, take advantage of that by uppercasing the mutex name
40507 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
40512 ** must be zero-initialized */
40521 if( pFile->hShared ){
40522 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
40525 if( !pFile->shared ){
40526 pFile->lastErrno = osGetLastError();
40527 winLogError(SQLITE_IOERR, pFile->lastErrno,
40530 osCloseHandle(pFile->hShared);
40531 pFile->hShared = NULL;
40536 if( pFile->hShared==NULL ){
40538 pFile->lastErrno = lastErrno;
40539 winLogError(SQLITE_IOERR, pFile->lastErrno,
40543 winceMutexRelease(pFile->hMutex);
40544 osCloseHandle(pFile->hMutex);
40545 pFile->hMutex = NULL;
40551 memset(pFile->shared, 0, sizeof(winceLock));
40554 winceMutexRelease(pFile->hMutex);
40562 if (pFile->hMutex){
40564 winceMutexAcquire(pFile->hMutex);
40568 if (pFile->local.nReaders){
40569 pFile->shared->nReaders --;
40571 if (pFile->local.bReserved){
40572 pFile->shared->bReserved = FALSE;
40574 if (pFile->local.bPending){
40575 pFile->shared->bPending = FALSE;
40577 if (pFile->local.bExclusive){
40578 pFile->shared->bExclusive = FALSE;
40581 /* De-reference and close our copy of the shared memory handle */
40582 osUnmapViewOfFile(pFile->shared);
40583 osCloseHandle(pFile->hShared);
40586 winceMutexRelease(pFile->hMutex);
40587 osCloseHandle(pFile->hMutex);
40588 pFile->hMutex = NULL;
40608 if (!pFile->hMutex) return TRUE;
40609 winceMutexAcquire(pFile->hMutex);
40614 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
40615 pFile->shared->bExclusive = TRUE;
40616 pFile->local.bExclusive = TRUE;
40621 /* Want a read-only lock? */
40624 if (pFile->shared->bExclusive == 0){
40625 pFile->local.nReaders ++;
40626 if (pFile->local.nReaders == 1){
40627 pFile->shared->nReaders ++;
40637 if (pFile->shared->bPending == 0) {
40638 pFile->shared->bPending = TRUE;
40639 pFile->local.bPending = TRUE;
40647 if (pFile->shared->bReserved == 0) {
40648 pFile->shared->bReserved = TRUE;
40649 pFile->local.bReserved = TRUE;
40654 winceMutexRelease(pFile->hMutex);
40674 if (!pFile->hMutex) return TRUE;
40675 winceMutexAcquire(pFile->hMutex);
40680 if (pFile->local.bExclusive){
40682 pFile->local.bExclusive = FALSE;
40683 pFile->shared->bExclusive = FALSE;
40688 else if (pFile->local.nReaders){
40691 pFile->local.nReaders --;
40692 if (pFile->local.nReaders == 0)
40694 pFile->shared->nReaders --;
40703 if (pFile->local.bPending){
40704 pFile->local.bPending = FALSE;
40705 pFile->shared->bPending = FALSE;
40712 if (pFile->local.bReserved) {
40713 pFile->local.bReserved = FALSE;
40714 pFile->shared->bReserved = FALSE;
40719 winceMutexRelease(pFile->hMutex);
40799 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
40805 ** Otherwise, set pFile->lastErrno and return non-zero.
40814 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
40820 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
40826 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
40830 pFile->lastErrno = lastErrno;
40831 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
40832 "winSeekFile", pFile->zPath);
40833 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
40837 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
40848 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
40851 pFile->lastErrno = osGetLastError();
40852 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
40853 "winSeekFile", pFile->zPath);
40854 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
40858 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
40886 assert( pFile->pShm==0 );
40888 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
40890 osGetCurrentProcessId(), pFile, pFile->h));
40897 rc = osCloseHandle(pFile->h);
40903 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
40904 if( pAppData==NULL || !pAppData->bNoLock ){
40908 if( pFile->zDeleteOnClose ){
40911 osDeleteFileW(pFile->zDeleteOnClose)==0
40912 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
40917 sqlite3_free(pFile->zDeleteOnClose);
40921 pFile->h = NULL;
40923 OpenCounter(-1);
40925 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
40928 "winClose", pFile->zPath);
40955 pFile->h, pBuf, amt, offset, pFile->locktype));
40960 if( offset<pFile->mmapSize ){
40961 if( offset+amt <= pFile->mmapSize ){
40962 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
40963 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
40964 osGetCurrentProcessId(), pFile, pFile->h));
40967 int nCopy = (int)(pFile->mmapSize - offset);
40968 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
40970 amt -= nCopy;
40979 osGetCurrentProcessId(), pFile, pFile->h));
40982 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
40987 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
40992 pFile->lastErrno = lastErrno;
40994 osGetCurrentProcessId(), pFile, pFile->h));
40995 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
40996 "winRead", pFile->zPath);
41000 /* Unread parts of the buffer must be zero-filled */
41001 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
41003 osGetCurrentProcessId(), pFile, pFile->h));
41008 osGetCurrentProcessId(), pFile, pFile->h));
41033 pFile->h, pBuf, amt, offset, pFile->locktype));
41038 if( offset<pFile->mmapSize ){
41039 if( offset+amt <= pFile->mmapSize ){
41040 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41041 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
41042 osGetCurrentProcessId(), pFile, pFile->h));
41045 int nCopy = (int)(pFile->mmapSize - offset);
41046 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41048 amt -= nCopy;
41076 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
41078 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
41094 nRem -= nWrite;
41097 pFile->lastErrno = lastErrno;
41103 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
41104 || ( pFile->lastErrno==ERROR_DISK_FULL )){
41106 osGetCurrentProcessId(), pFile, pFile->h));
41107 return winLogError(SQLITE_FULL, pFile->lastErrno,
41108 "winWrite1", pFile->zPath);
41111 osGetCurrentProcessId(), pFile, pFile->h));
41112 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
41113 "winWrite2", pFile->zPath);
41118 osGetCurrentProcessId(), pFile, pFile->h));
41133 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
41135 /* If the user has configured a chunk-size for this file, truncate the
41140 if( pFile->szChunk>0 ){
41141 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
41144 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
41146 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
41147 "winTruncate1", pFile->zPath);
41148 }else if( 0==osSetEndOfFile(pFile->h) &&
41150 pFile->lastErrno = lastErrno;
41151 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
41152 "winTruncate2", pFile->zPath);
41160 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
41161 pFile->mmapSize = nByte;
41166 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
41212 osGetCurrentProcessId(), pFile, pFile->h, flags,
41213 pFile->locktype));
41225 ** no-op
41228 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
41229 osGetCurrentProcessId(), pFile, pFile->h));
41233 if( pFile->pMapRegion ){
41234 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
41235 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
41237 pFile, pFile->pMapRegion));
41239 pFile->lastErrno = osGetLastError();
41240 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
41242 pFile, pFile->pMapRegion));
41243 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
41244 "winSync1", pFile->zPath);
41248 rc = osFlushFileBuffers(pFile->h);
41252 osGetCurrentProcessId(), pFile, pFile->h));
41255 pFile->lastErrno = osGetLastError();
41257 osGetCurrentProcessId(), pFile, pFile->h));
41258 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
41259 "winSync2", pFile->zPath);
41274 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
41279 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
41283 pFile->lastErrno = osGetLastError();
41284 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
41285 "winFileSize", pFile->zPath);
41294 lowerBits = osGetFileSize(pFile->h, &upperBits);
41298 pFile->lastErrno = lastErrno;
41299 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
41300 "winFileSize", pFile->zPath);
41305 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
41347 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
41354 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
41356 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
41364 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
41365 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
41366 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
41370 pFile->lastErrno = osGetLastError();
41373 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
41383 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
41385 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
41389 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
41393 pFile->lastErrno = lastErrno;
41394 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
41395 "winUnlockReadLock", pFile->zPath);
41397 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
41402 ** Lock the file with the lock specified by parameter locktype - one
41416 ** UNLOCKED -> SHARED
41417 ** SHARED -> RESERVED
41418 ** SHARED -> (PENDING) -> EXCLUSIVE
41419 ** RESERVED -> (PENDING) -> EXCLUSIVE
41420 ** PENDING -> EXCLUSIVE
41430 int newLocktype; /* Set pFile->locktype to this value before exiting */
41437 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
41443 if( pFile->locktype>=locktype ){
41444 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
41448 /* Do not allow any kind of write-lock on a read-only database
41450 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
41456 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
41458 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
41464 newLocktype = pFile->locktype;
41465 if( pFile->locktype==NO_LOCK
41466 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
41469 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
41472 ** around problems caused by indexing and/or anti-virus software on
41478 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
41479 pFile->h, cnt, res));
41481 pFile->lastErrno = lastErrno;
41483 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
41484 pFile->h, cnt, sqlite3ErrName(rc)));
41498 assert( pFile->locktype==NO_LOCK );
41510 assert( pFile->locktype==SHARED_LOCK );
41511 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
41529 assert( pFile->locktype>=SHARED_LOCK );
41531 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
41545 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
41554 pFile->lastErrno = lastErrno;
41556 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
41557 pFile->h, locktype, newLocktype));
41559 pFile->locktype = (u8)newLocktype;
41561 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
41568 ** non-zero, otherwise zero.
41575 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
41578 if( pFile->locktype>=RESERVED_LOCK ){
41580 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
41582 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
41584 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
41587 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
41590 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
41591 pFile->h, pResOut, *pResOut));
41600 ** the requested locking level, this routine is a no-op.
41613 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
41614 type = pFile->locktype;
41616 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
41621 "winUnlock", pFile->zPath);
41625 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
41631 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
41633 pFile->locktype = (u8)locktype;
41635 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
41640 ****************************** No-op Locking **********************************
41646 ** This locking mode is appropriate for use on read-only databases
41647 ** (ex: databases that are burned into CD-ROM, for example.) It can
41674 /******************* End of the no-op lock implementation *********************
41679 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
41681 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
41685 *pArg = (pFile->ctrlFlags & mask)!=0;
41687 pFile->ctrlFlags &= ~mask;
41689 pFile->ctrlFlags |= mask;
41703 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
41706 *(int*)pArg = pFile->locktype;
41707 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41711 *(int*)pArg = (int)pFile->lastErrno;
41712 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41716 pFile->szChunk = *(int *)pArg;
41717 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41721 if( pFile->szChunk>0 ){
41732 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
41735 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41740 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41745 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41749 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
41750 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41765 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41770 *phFile = pFile->h;
41771 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
41777 HANDLE hOldFile = pFile->h;
41778 pFile->h = *phFile;
41781 hOldFile, pFile->h));
41787 int rc = winGetTempname(pFile->pVfs, &zTFile);
41791 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
41801 *(i64*)pArg = pFile->mmapSizeMax;
41802 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
41803 pFile->mmapSizeMax = newLimit;
41804 if( pFile->mmapSize>0 ){
41806 rc = winMapfile(pFile, -1);
41809 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
41814 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
41824 ** if two files are created in the same file-system directory (i.e.
41839 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
41880 ** log-summary, each thread has its own winFile object, but they all
41882 ** log-summary is opened only once per process.
41890 ** The following fields are read-only after the object is created:
41905 int szRegion; /* Size of shared-memory regions */
41933 ** are read-only thereafter:
41938 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
41965 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
41973 assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
41975 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
41976 pFile->hFile.h, lockType, ofst, nByte));
41978 /* Release/Acquire the system-level lock */
41980 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
41985 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
41991 pFile->lastErrno = osGetLastError();
41995 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
41996 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
41997 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
42009 ** This is not a VFS shared-memory method; it is a utility function called
42010 ** by VFS shared-memory methods.
42016 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
42020 if( p->nRef==0 ){
42022 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
42023 for(i=0; i<p->nRegion; i++){
42024 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
42025 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
42028 bRc = osCloseHandle(p->aRegion[i].hMap);
42029 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
42033 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
42035 winClose((sqlite3_file *)&p->hFile);
42041 winDelete(pVfs, p->zFilename, 0);
42045 *pp = p->pNext;
42046 sqlite3_free(p->aRegion);
42049 pp = &p->pNext;
42055 ** Open the shared-memory area associated with database file pDbFd.
42057 ** When opening a new shared-memory file, if no other instances of that
42068 assert( pDbFd->pShm==0 ); /* Not previously opened */
42075 nName = sqlite3Strlen30(pDbFd->zPath);
42081 pNew->zFilename = (char*)&pNew[1];
42082 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
42083 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
42089 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
42093 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
42100 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
42101 pShmNode->pNext = winShmNodeList;
42105 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
42106 if( pShmNode->mutex==0 ){
42112 rc = winOpen(pDbFd->pVfs,
42113 pShmNode->zFilename, /* Name of the file (UTF-8) */
42114 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
42121 /* Check to see if another process is holding the dead-man switch.
42125 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
42128 "winOpenShm", pDbFd->zPath);
42139 p->pShmNode = pShmNode;
42141 p->id = pShmNode->nextShmId++;
42143 pShmNode->nRef++;
42144 pDbFd->pShm = p;
42151 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
42154 sqlite3_mutex_enter(pShmNode->mutex);
42155 p->pNext = pShmNode->pFirst;
42156 pShmNode->pFirst = p;
42157 sqlite3_mutex_leave(pShmNode->mutex);
42163 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
42171 ** Close a connection to shared-memory. Delete the underlying
42178 winFile *pDbFd; /* Database holding shared-memory */
42180 winShmNode *pShmNode; /* The underlying shared-memory file */
42184 p = pDbFd->pShm;
42186 pShmNode = p->pShmNode;
42190 sqlite3_mutex_enter(pShmNode->mutex);
42191 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
42192 *pp = p->pNext;
42196 pDbFd->pShm = 0;
42197 sqlite3_mutex_leave(pShmNode->mutex);
42199 /* If pShmNode->nRef has reached 0, then close the underlying
42200 ** shared-memory file, too */
42202 assert( pShmNode->nRef>0 );
42203 pShmNode->nRef--;
42204 if( pShmNode->nRef==0 ){
42205 winShmPurge(pDbFd->pVfs, deleteFlag);
42213 ** Change the lock state for a shared-memory segment.
42222 winShm *p = pDbFd->pShm; /* The shared memory being locked */
42224 winShmNode *pShmNode = p->pShmNode;
42236 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
42238 sqlite3_mutex_enter(pShmNode->mutex);
42243 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42245 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
42246 allMask |= pX->sharedMask;
42249 /* Unlock the system-level locks */
42258 p->exclMask &= ~mask;
42259 p->sharedMask &= ~mask;
42268 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42269 if( (pX->exclMask & mask)!=0 ){
42273 allShared |= pX->sharedMask;
42287 p->sharedMask |= mask;
42293 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42294 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
42306 assert( (p->sharedMask & mask)==0 );
42307 p->exclMask |= mask;
42311 sqlite3_mutex_leave(pShmNode->mutex);
42312 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
42313 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
42328 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42335 ** shared-memory associated with the database file fd. Shared-memory regions
42336 ** are numbered starting from zero. Each shared-memory region is szRegion
42341 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
42344 ** isWrite is non-zero and the requested shared-memory region has not yet
42347 ** If the shared-memory region has already been allocated or is allocated by
42360 winShm *pShm = pDbFd->pShm;
42367 pShm = pDbFd->pShm;
42369 pShmNode = pShm->pShmNode;
42371 sqlite3_mutex_enter(pShmNode->mutex);
42372 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
42374 if( pShmNode->nRegion<=iRegion ){
42377 sqlite3_int64 sz; /* Current size of wal-index file */
42379 pShmNode->szRegion = szRegion;
42382 ** Check to see if it has been allocated (i.e. if the wal-index file is
42385 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
42388 "winShmMap1", pDbFd->zPath);
42396 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
42400 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
42403 "winShmMap2", pDbFd->zPath);
42410 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
42416 pShmNode->aRegion = apNew;
42418 while( pShmNode->nRegion<=iRegion ){
42419 HANDLE hMap = NULL; /* file-mapping handle */
42423 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
42427 hMap = osCreateFileMappingW(pShmNode->hFile.h,
42431 hMap = osCreateFileMappingA(pShmNode->hFile.h,
42435 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
42436 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
42439 int iOffset = pShmNode->nRegion*szRegion;
42443 iOffset - iOffsetShift, szRegion + iOffsetShift
42447 0, iOffset - iOffsetShift, szRegion + iOffsetShift
42450 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
42451 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
42455 pShmNode->lastErrno = osGetLastError();
42456 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
42457 "winShmMap3", pDbFd->zPath);
42462 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
42463 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
42464 pShmNode->nRegion++;
42469 if( pShmNode->nRegion>iRegion ){
42472 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
42477 sqlite3_mutex_leave(pShmNode->mutex);
42494 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
42496 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
42497 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
42498 if( pFile->pMapRegion ){
42499 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
42500 pFile->lastErrno = osGetLastError();
42501 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
42503 pFile->pMapRegion));
42504 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
42505 "winUnmapfile1", pFile->zPath);
42507 pFile->pMapRegion = 0;
42508 pFile->mmapSize = 0;
42509 pFile->mmapSizeActual = 0;
42511 if( pFile->hMap!=NULL ){
42512 if( !osCloseHandle(pFile->hMap) ){
42513 pFile->lastErrno = osGetLastError();
42514 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
42515 osGetCurrentProcessId(), pFile, pFile->hMap));
42516 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
42517 "winUnmapfile2", pFile->zPath);
42519 pFile->hMap = NULL;
42521 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
42527 ** Memory map or remap the file opened by file-descriptor pFd (if the file
42530 ** outstanding xFetch() references to it, this function is a no-op.
42532 ** If parameter nByte is non-negative, then it is the requested size of
42546 assert( nMap>=0 || pFd->nFetchOut==0 );
42547 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
42550 if( pFd->nFetchOut>0 ) return SQLITE_OK;
42555 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
42560 if( nMap>pFd->mmapSizeMax ){
42561 nMap = pFd->mmapSizeMax;
42563 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
42565 if( nMap==0 && pFd->mmapSize>0 ){
42568 if( nMap!=pFd->mmapSize ){
42575 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
42581 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
42583 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
42587 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
42591 if( pFd->hMap==NULL ){
42592 pFd->lastErrno = osGetLastError();
42593 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
42594 "winMapfile1", pFd->zPath);
42596 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
42603 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
42605 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
42608 osCloseHandle(pFd->hMap);
42609 pFd->hMap = NULL;
42610 pFd->lastErrno = osGetLastError();
42611 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
42612 "winMapfile2", pFd->zPath);
42614 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
42618 pFd->pMapRegion = pNew;
42619 pFd->mmapSize = nMap;
42620 pFd->mmapSizeActual = nMap;
42623 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
42651 if( pFd->mmapSizeMax>0 ){
42652 if( pFd->pMapRegion==0 ){
42653 int rc = winMapfile(pFd, -1);
42660 if( pFd->mmapSize >= iOff+nAmt ){
42661 *pp = &((u8 *)pFd->pMapRegion)[iOff];
42662 pFd->nFetchOut++;
42673 ** If the third argument is non-NULL, then this function releases a
42689 assert( (p==0)==(pFd->nFetchOut==0) );
42692 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
42698 pFd->nFetchOut--;
42702 ** is unnecessary can be omitted - potentially improving
42707 assert( pFd->nFetchOut>=0 );
42795 ** supports for filenames into UTF-8. Space to hold the result is
42814 ** Convert a UTF-8 filename into whatever form the underlying
42834 ** This function returns non-zero if the specified UTF-8 string buffer
42842 if( winIsDirSep(zBuf[nLen-1]) ){
42868 /* It's odd to simulate an io-error here, but really this is just
42869 ** using the io-error infrastructure to test that SQLite handles this
42877 nMax = pVfs->mxPathname; nBuf = nMax + 2;
42880 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
42888 nDir = nMax - (nPre + 15);
42893 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
42898 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
42938 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
42951 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
42959 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
42966 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
42972 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
42990 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
42996 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
43008 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43018 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43023 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
43033 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
43047 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
43065 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
43069 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
43074 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
43080 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
43118 const char *zName, /* Name of the file (UTF-8) */
43135 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
43184 /* Assert that the upper layer has set one of the "file-type" flags. */
43193 pFile->h = INVALID_HANDLE_VALUE;
43215 /* Database filenames are double-zero terminated if they are not
43328 pFile->lastErrno = lastErrno;
43329 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
43354 pAppData = (winVfsAppData*)pVfs->pAppData;
43359 && ((pAppData==NULL) || !pAppData->bNoLock)
43365 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
43370 pFile->zDeleteOnClose = zConverted;
43378 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
43379 pFile->pVfs = pVfs;
43380 pFile->h = h;
43382 pFile->ctrlFlags |= WINFILE_RDONLY;
43385 pFile->ctrlFlags |= WINFILE_PSOW;
43387 pFile->lastErrno = NO_ERROR;
43388 pFile->zPath = zName;
43390 pFile->hMap = NULL;
43391 pFile->pMapRegion = 0;
43392 pFile->mmapSize = 0;
43393 pFile->mmapSizeActual = 0;
43394 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
43550 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
43596 ** Returns non-zero if the specified path name starts with a drive letter
43606 ** Returns non-zero if the specified path name should be used verbatim. If
43607 ** non-zero is returned from this function, the calling function must simply
43608 ** use the provided path name verbatim -OR- resolve it into a full path name
43644 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
43669 assert( nFull>=pVfs->mxPathname );
43677 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
43683 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
43693 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
43699 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
43705 zRelative, zOut, pVfs->mxPathname+1)<0 ){
43715 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
43734 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
43737 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
43743 /* It's odd to simulate an io-error here, but really this is just
43744 ** using the io-error infrastructure to test that SQLite handles this
43756 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
43817 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
43834 int nFull = pVfs->mxPathname+1;
43909 for(j=0, k=p->i; j<sz; j++){
43910 p->a[k++] ^= x[j];
43911 if( k>=p->na ) k = 0;
43913 p->i = k;
43914 p->nXor += sz;
43988 ** The following variable, if set to a non-zero value, is interpreted as
44007 /* FILETIME structure is a 64-bit value representing the number of
44008 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
44015 /* 2^32 - to avoid use of LL and warnings in gcc */
44065 ** buffer with a nul-terminated UTF-8 encoded error message
44071 ** is zero if the error message fits in the buffer, or non-zero
44072 ** otherwise (if the message was truncated). If non-zero is returned,
44073 ** then it is not necessary to include the nul-terminator character
44130 "win32-longpath", /* zName */
44155 "win32-none", /* zName */
44180 "win32-longpath-none", /* zName */
44201 /* Double-check that the aSyscall[] array has been constructed
44255 ** This file implements an object that represents a fixed-length
44259 ** journalled during a transaction, or which pages have the "dont-write"
44274 ** 5 and 500 set operations per Bitvec object, though the number of sets can
44288 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
44305 ** sub-dividing and re-hashing. */
44310 ** no fewer collisions than the no-op *1. */
44330 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
44339 u32 nSet; /* Number of bits that are set - only valid for aHash
44363 p->iSize = iSize;
44369 ** Check to see if the i-th bit is set. Return true or false.
44375 i--;
44376 if( i>=p->iSize ) return 0;
44377 while( p->iDivisor ){
44378 u32 bin = i/p->iDivisor;
44379 i = i%p->iDivisor;
44380 p = p->u.apSub[bin];
44385 if( p->iSize<=BITVEC_NBIT ){
44386 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
44389 while( p->u.aHash[h] ){
44390 if( p->u.aHash[h]==i ) return 1;
44401 ** Set the i-th bit. Return 0 on success and an error code if
44404 ** This routine might cause sub-bitmaps to be allocated. Failing
44405 ** to get the memory needed to hold the sub-bitmap is the only
44416 assert( i<=p->iSize );
44417 i--;
44418 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
44419 u32 bin = i/p->iDivisor;
44420 i = i%p->iDivisor;
44421 if( p->u.apSub[bin]==0 ){
44422 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
44423 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
44425 p = p->u.apSub[bin];
44427 if( p->iSize<=BITVEC_NBIT ){
44428 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
44434 /* worring about sub-dividing and re-hashing. */
44435 if( !p->u.aHash[h] ){
44436 if (p->nSet<(BITVEC_NINT-1)) {
44445 if( p->u.aHash[h]==i ) return SQLITE_OK;
44448 } while( p->u.aHash[h] );
44453 if( p->nSet>=BITVEC_MXHASH ){
44456 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
44460 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
44461 memset(p->u.apSub, 0, sizeof(p->u.apSub));
44462 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
44472 p->nSet++;
44473 p->u.aHash[h] = i;
44478 ** Clear the i-th bit.
44486 i--;
44487 while( p->iDivisor ){
44488 u32 bin = i/p->iDivisor;
44489 i = i%p->iDivisor;
44490 p = p->u.apSub[bin];
44495 if( p->iSize<=BITVEC_NBIT ){
44496 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
44500 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
44501 memset(p->u.aHash, 0, sizeof(p->u.aHash));
44502 p->nSet = 0;
44505 u32 h = BITVEC_HASH(aiValues[j]-1);
44506 p->nSet++;
44507 while( p->u.aHash[h] ){
44511 p->u.aHash[h] = aiValues[j];
44522 if( p->iDivisor ){
44525 sqlite3BitvecDestroy(p->u.apSub[i]);
44536 return p->iSize;
44558 ** There are 6 opcodes numbered from 0 through 5. 0 is the
44566 ** 5 N S X Set N bits from S increment X in array only, not in bitvec
44570 ** Opcode 5 works on the linear array only, not on the Bitvec.
44571 ** Opcode 5 is used to deliberately induce a fault in order to
44578 ** If a memory allocation error occurs, return -1.
44583 int rc = -1;
44604 case 5: {
44606 i = aOp[pc+2] - 1;
44618 if( (--aOp[pc+1]) > 0 ) nx = 0;
44623 if( op!=5 ){
44634 ** match (rc==0). Change rc to non-zero if a discrepancy
44639 + (sqlite3BitvecSize(pBitvec) - sz);
44685 ** such that p was added to the list more recently than p->pDirtyNext.
44690 ** page to eject from the cache mid-transaction. It is better to eject
44734 if( pCache->pCache==0 ) return;
44738 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
44740 pPg = (PgHdr*)pLower->pExtra;
44741 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
44742 a = (unsigned char *)pLower->pBuf;
44745 if( pPg->pPage==0 ){
44746 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
44768 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
44769 pCache = pPg->pCache;
44771 if( pPg->flags & PGHDR_CLEAN ){
44772 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
44773 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
44774 assert( pCache->pDirtyTail!=pPg );
44777 if( pPg->flags & PGHDR_WRITEABLE ){
44778 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
44816 PCache *p = pPage->pCache;
44820 pPage->pgno));
44822 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
44823 assert( pPage->pDirtyPrev || pPage==p->pDirty );
44826 if( p->pSynced==pPage ){
44827 p->pSynced = pPage->pDirtyPrev;
44830 if( pPage->pDirtyNext ){
44831 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
44833 assert( pPage==p->pDirtyTail );
44834 p->pDirtyTail = pPage->pDirtyPrev;
44836 if( pPage->pDirtyPrev ){
44837 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
44843 assert( pPage==p->pDirty );
44844 p->pDirty = pPage->pDirtyNext;
44845 assert( p->bPurgeable || p->eCreate==2 );
44846 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
44847 assert( p->bPurgeable==0 || p->eCreate==1 );
44848 p->eCreate = 2;
44851 pPage->pDirtyNext = 0;
44852 pPage->pDirtyPrev = 0;
44855 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
44857 pPage->pDirtyNext = p->pDirty;
44858 if( pPage->pDirtyNext ){
44859 assert( pPage->pDirtyNext->pDirtyPrev==0 );
44860 pPage->pDirtyNext->pDirtyPrev = pPage;
44862 p->pDirtyTail = pPage;
44863 if( p->bPurgeable ){
44864 assert( p->eCreate==2 );
44865 p->eCreate = 1;
44868 p->pDirty = pPage;
44874 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
44875 if( !p->pSynced
44876 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
44878 p->pSynced = pPage;
44886 ** being used for an in-memory database, this function is a no-op.
44889 if( p->pCache->bPurgeable ){
44890 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
44891 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
44892 pcacheDump(p->pCache);
44897 ** Compute the number of pages of cache requested. p->szCache is the
44901 if( p->szCache>=0 ){
44902 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
44904 return p->szCache;
44906 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
44909 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
44920 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
44921 ** built-in default page cache is used instead of the application defined
44929 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
44960 p->szPage = 1;
44961 p->szExtra = szExtra;
44963 p->bPurgeable = bPurgeable;
44964 p->eCreate = 2;
44965 p->xStress = xStress;
44966 p->pStress = pStress;
44967 p->szCache = 100;
44968 p->szSpill = 1;
44978 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
44979 if( pCache->szPage ){
44982 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
44983 pCache->bPurgeable
44987 if( pCache->pCache ){
44988 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
44990 pCache->pCache = pNew;
44991 pCache->szPage = szPage;
45030 assert( pCache->pCache!=0 );
45032 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
45041 eCreate = createFlag & pCache->eCreate;
45043 assert( createFlag==0 || pCache->eCreate==eCreate );
45044 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
45045 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
45068 if( pCache->eCreate==2 ) return 0;
45070 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
45071 /* Find a dirty page to write-out and recycle. First try to find a
45072 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
45080 for(pPg=pCache->pSynced;
45081 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
45082 pPg=pPg->pDirtyPrev
45084 pCache->pSynced = pPg;
45086 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
45092 "spill page %d making room for %d - cache used: %d/%d",
45093 pPg->pgno, pgno,
45094 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
45097 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
45098 rc = pCache->xStress(pCache->pStress, pPg);
45105 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
45125 pPgHdr = (PgHdr*)pPage->pExtra;
45126 assert( pPgHdr->pPage==0 );
45127 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
45128 pPgHdr->pPage = pPage;
45129 pPgHdr->pData = pPage->pBuf;
45130 pPgHdr->pExtra = (void *)&pPgHdr[1];
45131 memset(pPgHdr->pExtra, 0, 8);
45132 pPgHdr->pCache = pCache;
45133 pPgHdr->pgno = pgno;
45134 pPgHdr->flags = PGHDR_CLEAN;
45152 pPgHdr = (PgHdr *)pPage->pExtra;
45154 if( !pPgHdr->pPage ){
45157 pCache->nRefSum++;
45158 pPgHdr->nRef++;
45168 assert( p->nRef>0 );
45169 p->pCache->nRefSum--;
45170 if( (--p->nRef)==0 ){
45171 if( p->flags&PGHDR_CLEAN ){
45173 }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
45174 /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
45176 ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
45187 assert(p->nRef>0);
45189 p->nRef++;
45190 p->pCache->nRefSum++;
45199 assert( p->nRef==1 );
45201 if( p->flags&PGHDR_DIRTY ){
45204 p->pCache->nRefSum--;
45205 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
45213 assert( p->nRef>0 );
45215 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
45216 p->flags &= ~PGHDR_DONT_WRITE;
45217 if( p->flags & PGHDR_CLEAN ){
45218 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
45219 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
45220 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
45233 if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
45234 assert( (p->flags & PGHDR_CLEAN)==0 );
45236 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
45237 p->flags |= PGHDR_CLEAN;
45238 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
45240 if( p->nRef==0 ){
45251 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
45252 while( (p = pCache->pDirty)!=0 ){
45262 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
45263 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45264 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
45266 pCache->pSynced = pCache->pDirtyTail;
45274 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45275 p->flags &= ~PGHDR_NEED_SYNC;
45277 pCache->pSynced = pCache->pDirtyTail;
45284 PCache *pCache = p->pCache;
45285 assert( p->nRef>0 );
45288 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
45289 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
45290 p->pgno = newPgno;
45291 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
45306 if( pCache->pCache ){
45310 for(p=pCache->pDirty; p; p=pNext){
45311 pNext = p->pDirtyNext;
45316 assert( p->pgno>0 );
45317 if( p->pgno>pgno ){
45318 assert( p->flags&PGHDR_DIRTY );
45322 if( pgno==0 && pCache->nRefSum ){
45324 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
45326 ** pCache->nRefSum>0 */
45327 memset(pPage1->pBuf, 0, pCache->szPage);
45331 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
45339 assert( pCache->pCache!=0 );
45341 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
45360 if( pA->pgno<pB->pgno ){
45361 pTail->pDirty = pA;
45363 pA = pA->pDirty;
45365 pTail->pDirty = pB;
45369 pTail->pDirty = pB;
45371 pB = pB->pDirty;
45373 pTail->pDirty = pA;
45398 pIn = p->pDirty;
45399 p->pDirty = 0;
45400 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
45409 if( NEVER(i==N_SORT_BUCKET-1) ){
45429 for(p=pCache->pDirty; p; p=p->pDirtyNext){
45430 p->pDirty = p->pDirtyNext;
45432 return pcacheSortDirtyList(pCache->pDirty);
45442 return pCache->nRefSum;
45449 return p->nRef;
45456 assert( pCache->pCache!=0 );
45457 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
45462 ** Get the suggested cache-size value.
45470 ** Set the suggested cache-size value.
45473 assert( pCache->pCache!=0 );
45474 pCache->szCache = mxPage;
45475 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
45480 ** Set the suggested cache-spill value. Make no changes if if the
45481 ** argument is zero. Return the effective cache-spill size, which will
45486 assert( p->pCache!=0 );
45489 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
45491 p->szSpill = mxPage;
45494 if( res<p->szSpill ) res = p->szSpill;
45502 assert( pCache->pCache!=0 );
45503 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
45508 ** in the page-cache hierarchy.
45520 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
45532 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
45560 ** -------------------------------------------------------------
45562 ** -------------------------------------------------------------
45578 ** size can vary according to architecture, compile-time options, and
45586 ** power of two, and leaving the rounded-up space unused.
45598 ** (1) The general-purpose memory allocator - sqlite3Malloc()
45599 ** (2) Global page-cache memory provided using sqlite3_config() with
45601 ** (3) PCache-local bulk allocation.
45611 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
45615 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
45619 ** show that method (3) with N==100 provides about a 5% performance boost for
45632 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
45673 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
45679 ** open database file (including each in-memory database and each
45709 PgHdr1 *pFree; /* List of unused pcache-local pages */
45710 void *pBulk; /* Bulk memory used by pcache-local */
45744 ** reading because (1) most platforms read a 32-bit integer atomically and
45761 # define pcache1EnterMutex(X) assert((X)->mutex==0)
45762 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
45765 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
45766 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
45776 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
45794 while( n-- ){
45796 p->pNext = pcache1.pFree;
45805 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
45806 ** true if pCache->pFree ends up containing one or more free pages.
45813 if( pCache->nMax<3 ) return 0;
45816 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
45818 szBulk = -1024 * (i64)pcache1.nInitPage;
45820 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
45821 szBulk = pCache->szAlloc*(i64)pCache->nMax;
45823 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
45826 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
45828 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
45829 pX->page.pBuf = zBulk;
45830 pX->page.pExtra = &pX[1];
45831 pX->isBulkLocal = 1;
45832 pX->isAnchor = 0;
45833 pX->pNext = pCache->pFree;
45834 pCache->pFree = pX;
45835 zBulk += pCache->szAlloc;
45836 }while( --nBulk );
45838 return pCache->pFree!=0;
45857 pcache1.pFree = pcache1.pFree->pNext;
45858 pcache1.nFreeSlot--;
45895 pSlot->pNext = pcache1.pFree;
45942 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
45943 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
45944 p = pCache->pFree;
45945 pCache->pFree = p->pNext;
45946 p->pNext = 0;
45953 assert( pCache->pGroup==&pcache1.grp );
45954 pcache1LeaveMutex(pCache->pGroup);
45958 pPg = pcache1Alloc(pCache->szPage);
45959 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
45966 pPg = pcache1Alloc(pCache->szAlloc);
45967 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
45971 pcache1EnterMutex(pCache->pGroup);
45974 p->page.pBuf = pPg;
45975 p->page.pExtra = &p[1];
45976 p->isBulkLocal = 0;
45977 p->isAnchor = 0;
45979 if( pCache->bPurgeable ){
45980 pCache->pGroup->nCurrentPage++;
45991 pCache = p->pCache;
45992 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
45993 if( p->isBulkLocal ){
45994 p->pNext = pCache->pFree;
45995 pCache->pFree = p;
45997 pcache1Free(p->page.pBuf);
46002 if( pCache->bPurgeable ){
46003 pCache->pGroup->nCurrentPage--;
46041 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
46062 assert( sqlite3_mutex_held(p->pGroup->mutex) );
46064 nNew = p->nHash*2;
46069 pcache1LeaveMutex(p->pGroup);
46070 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
46072 if( p->nHash ){ sqlite3EndBenignMalloc(); }
46073 pcache1EnterMutex(p->pGroup);
46075 for(i=0; i<p->nHash; i++){
46077 PgHdr1 *pNext = p->apHash[i];
46079 unsigned int h = pPage->iKey % nNew;
46080 pNext = pPage->pNext;
46081 pPage->pNext = apNew[h];
46085 sqlite3_free(p->apHash);
46086 p->apHash = apNew;
46087 p->nHash = nNew;
46094 ** LRU list, then this function is a no-op.
46102 assert( pPage->isPinned==0 );
46103 pCache = pPage->pCache;
46104 assert( pPage->pLruNext );
46105 assert( pPage->pLruPrev );
46106 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46107 pPage->pLruPrev->pLruNext = pPage->pLruNext;
46108 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
46109 pPage->pLruNext = 0;
46110 pPage->pLruPrev = 0;
46111 pPage->isPinned = 1;
46112 assert( pPage->isAnchor==0 );
46113 assert( pCache->pGroup->lru.isAnchor==1 );
46114 pCache->nRecyclable--;
46128 PCache1 *pCache = pPage->pCache;
46131 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46132 h = pPage->iKey % pCache->nHash;
46133 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
46134 *pp = (*pp)->pNext;
46136 pCache->nPage--;
46145 PGroup *pGroup = pCache->pGroup;
46147 assert( sqlite3_mutex_held(pGroup->mutex) );
46148 while( pGroup->nCurrentPage>pGroup->nMaxPage
46149 && (p=pGroup->lru.pLruPrev)->isAnchor==0
46151 assert( p->pCache->pGroup==pGroup );
46152 assert( p->isPinned==0 );
46156 if( pCache->nPage==0 && pCache->pBulk ){
46157 sqlite3_free(pCache->pBulk);
46158 pCache->pBulk = pCache->pFree = 0;
46173 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
46175 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
46176 assert( pCache->iMaxKey >= iLimit );
46177 assert( pCache->nHash > 0 );
46178 if( pCache->iMaxKey - iLimit < pCache->nHash ){
46183 h = iLimit % pCache->nHash;
46184 iStop = pCache->iMaxKey % pCache->nHash;
46185 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
46189 h = pCache->nHash/2;
46190 iStop = h - 1;
46195 assert( h<pCache->nHash );
46196 pp = &pCache->apHash[h];
46198 if( pPage->iKey>=iLimit ){
46199 pCache->nPage--;
46200 *pp = pPage->pNext;
46201 if( !pPage->isPinned ) pcache1PinPage(pPage);
46204 pp = &pPage->pNext;
46209 h = (h+1) % pCache->nHash;
46211 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
46228 ** private PGroup (mode-1). pcache1.separateCache is false if the single
46229 ** PGroup in pcache1.grp is used for all page caches (mode-2).
46231 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
46233 ** * Use a unified cache in single-threaded applications that have
46234 ** configured a start-time buffer for use as page-cache memory using
46235 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
46238 ** * Otherwise use separate caches (mode-1)
46292 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
46300 pGroup->mxPinned = 10;
46304 if( pGroup->lru.isAnchor==0 ){
46305 pGroup->lru.isAnchor = 1;
46306 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
46308 pCache->pGroup = pGroup;
46309 pCache->szPage = szPage;
46310 pCache->szExtra = szExtra;
46311 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
46312 pCache->bPurgeable = (bPurgeable ? 1 : 0);
46316 pCache->nMin = 10;
46317 pGroup->nMinPage += pCache->nMin;
46318 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
46321 if( pCache->nHash==0 ){
46336 if( pCache->bPurgeable ){
46337 PGroup *pGroup = pCache->pGroup;
46339 pGroup->nMaxPage += (nMax - pCache->nMax);
46340 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
46341 pCache->nMax = nMax;
46342 pCache->n90pct = pCache->nMax*9/10;
46355 if( pCache->bPurgeable ){
46356 PGroup *pGroup = pCache->pGroup;
46359 savedMaxPage = pGroup->nMaxPage;
46360 pGroup->nMaxPage = 0;
46362 pGroup->nMaxPage = savedMaxPage;
46373 pcache1EnterMutex(pCache->pGroup);
46374 n = pCache->nPage;
46375 pcache1LeaveMutex(pCache->pGroup);
46381 ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
46394 PGroup *pGroup = pCache->pGroup;
46398 assert( pCache->nPage >= pCache->nRecyclable );
46399 nPinned = pCache->nPage - pCache->nRecyclable;
46400 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
46401 assert( pCache->n90pct == pCache->nMax*9/10 );
46403 nPinned>=pGroup->mxPinned
46404 || nPinned>=pCache->n90pct
46405 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
46410 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
46411 assert( pCache->nHash>0 && pCache->apHash );
46414 if( pCache->bPurgeable
46415 && !pGroup->lru.pLruPrev->isAnchor
46416 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
46419 pPage = pGroup->lru.pLruPrev;
46420 assert( pPage->isPinned==0 );
46423 pOther = pPage->pCache;
46424 if( pOther->szAlloc != pCache->szAlloc ){
46428 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
46432 /* Step 5. If a usable page buffer has still not been found,
46440 unsigned int h = iKey % pCache->nHash;
46441 pCache->nPage++;
46442 pPage->iKey = iKey;
46443 pPage->pNext = pCache->apHash[h];
46444 pPage->pCache = pCache;
46445 pPage->pLruPrev = 0;
46446 pPage->pLruNext = 0;
46447 pPage->isPinned = 1;
46448 *(void **)pPage->page.pExtra = 0;
46449 pCache->apHash[h] = pPage;
46450 if( iKey>pCache->iMaxKey ){
46451 pCache->iMaxKey = iKey;
46467 ** For a non-purgeable cache (a cache used as the storage for an in-memory
46470 ** a non-purgeable cache.
46507 ** proceed to step 5.
46509 ** 5. Otherwise, allocate and return a new page buffer.
46513 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
46525 pPage = pCache->apHash[iKey % pCache->nHash];
46526 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
46533 if( !pPage->isPinned ){
46539 /* Steps 3, 4, and 5 implemented by this subroutine */
46554 pcache1EnterMutex(pCache->pGroup);
46556 assert( pPage==0 || pCache->iMaxKey>=iKey );
46557 pcache1LeaveMutex(pCache->pGroup);
46571 assert( pCache->bPurgeable || createFlag!=1 );
46572 assert( pCache->bPurgeable || pCache->nMin==0 );
46573 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
46574 assert( pCache->nMin==0 || pCache->bPurgeable );
46575 assert( pCache->nHash>0 );
46577 if( pCache->pGroup->mutex ){
46599 PGroup *pGroup = pCache->pGroup;
46601 assert( pPage->pCache==pCache );
46607 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
46608 assert( pPage->isPinned==1 );
46610 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
46614 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
46615 pPage->pLruPrev = &pGroup->lru;
46616 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
46618 pCache->nRecyclable++;
46619 pPage->isPinned = 0;
46622 pcache1LeaveMutex(pCache->pGroup);
46638 assert( pPage->iKey==iOld );
46639 assert( pPage->pCache==pCache );
46641 pcache1EnterMutex(pCache->pGroup);
46643 h = iOld%pCache->nHash;
46644 pp = &pCache->apHash[h];
46646 pp = &(*pp)->pNext;
46648 *pp = pPage->pNext;
46650 h = iNew%pCache->nHash;
46651 pPage->iKey = iNew;
46652 pPage->pNext = pCache->apHash[h];
46653 pCache->apHash[h] = pPage;
46654 if( iNew>pCache->iMaxKey ){
46655 pCache->iMaxKey = iNew;
46658 pcache1LeaveMutex(pCache->pGroup);
46670 pcache1EnterMutex(pCache->pGroup);
46671 if( iLimit<=pCache->iMaxKey ){
46673 pCache->iMaxKey = iLimit-1;
46675 pcache1LeaveMutex(pCache->pGroup);
46685 PGroup *pGroup = pCache->pGroup;
46686 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
46688 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
46689 assert( pGroup->nMaxPage >= pCache->nMax );
46690 pGroup->nMaxPage -= pCache->nMax;
46691 assert( pGroup->nMinPage >= pCache->nMin );
46692 pGroup->nMinPage -= pCache->nMin;
46693 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
46696 sqlite3_free(pCache->pBulk);
46697 sqlite3_free(pCache->apHash);
46757 && p->isAnchor==0
46759 nFree += pcache1MemSize(p->page.pBuf);
46763 assert( p->isPinned==0 );
46786 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
46787 assert( p->isPinned==0 );
46846 ** a non-zero batch number, it will see all prior INSERTs.
46874 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
46940 p->pChunk = 0;
46941 p->db = db;
46942 p->pEntry = 0;
46943 p->pLast = 0;
46944 p->pForest = 0;
46945 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
46946 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
46947 p->rsFlags = ROWSET_SORTED;
46948 p->iBatch = 0;
46959 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
46960 pNextChunk = pChunk->pNextChunk;
46961 sqlite3DbFree(p->db, pChunk);
46963 p->pChunk = 0;
46964 p->nFresh = 0;
46965 p->pEntry = 0;
46966 p->pLast = 0;
46967 p->pForest = 0;
46968 p->rsFlags = ROWSET_SORTED;
46976 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
46981 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
46985 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
46989 pNew->pNextChunk = p->pChunk;
46990 p->pChunk = pNew;
46991 p->pFresh = pNew->aEntry;
46992 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
46994 p->nFresh--;
46995 return p->pFresh++;
47009 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
47013 pEntry->v = rowid;
47014 pEntry->pRight = 0;
47015 pLast = p->pLast;
47017 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
47020 p->rsFlags &= ~ROWSET_SORTED;
47022 pLast->pRight = pEntry;
47024 p->pEntry = pEntry;
47026 p->pLast = pEntry;
47045 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
47046 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
47047 if( pA->v<=pB->v ){
47048 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
47049 pA = pA->pRight;
47051 pTail->pRight = pB;
47055 pTail = pTail->pRight = pB;
47056 pB = pB->pRight;
47058 pTail->pRight = pA;
47076 pNext = pIn->pRight;
47077 pIn->pRight = 0;
47105 if( pIn->pLeft ){
47107 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
47108 p->pRight = pIn;
47112 if( pIn->pRight ){
47113 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
47117 assert( (*ppLast)->pRight==0 );
47140 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
47144 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
47148 pLeft = rowSetNDeepTree(ppList, iDepth-1);
47150 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
47155 p->pLeft = pLeft;
47156 *ppList = p->pRight;
47157 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
47160 *ppList = p->pRight;
47161 p->pLeft = p->pRight = 0;
47177 pList = p->pRight;
47178 p->pLeft = p->pRight = 0;
47182 pList = p->pRight;
47183 p->pLeft = pLeft;
47184 p->pRight = rowSetNDeepTree(&pList, iDepth);
47204 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
47207 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47208 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47209 p->pEntry = rowSetEntrySort(p->pEntry);
47211 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
47215 if( p->pEntry ){
47216 *pRowid = p->pEntry->v;
47217 p->pEntry = p->pEntry->pRight;
47218 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
47233 ** on pRowSet->pEntry, then sort those entries into the forest at
47234 ** pRowSet->pForest so that they can be tested.
47240 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
47245 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
47246 p = pRowSet->pEntry;
47248 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
47249 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
47253 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
47254 ppPrevTree = &pTree->pRight;
47255 if( pTree->pLeft==0 ){
47256 pTree->pLeft = rowSetListToTree(p);
47260 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
47261 pTree->pLeft = 0;
47268 pTree->v = 0;
47269 pTree->pRight = 0;
47270 pTree->pLeft = rowSetListToTree(p);
47273 pRowSet->pEntry = 0;
47274 pRowSet->pLast = 0;
47275 pRowSet->rsFlags |= ROWSET_SORTED;
47277 pRowSet->iBatch = iBatch;
47283 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
47284 p = pTree->pLeft;
47286 if( p->v<iRowid ){
47287 p = p->pRight;
47288 }else if( p->v>iRowid ){
47289 p = p->pLeft;
47335 ** This header file defines the interface to the write-ahead logging
47375 /* Connection to a write-ahead log (WAL) file.
47380 /* Open and close a connection to a write-ahead log. */
47388 ** snapshot is like a read-transaction. It is the state of the database
47397 /* Read a page from the write-ahead log, if it is present. */
47424 Wal *pWal, /* Write-ahead log connection */
47448 /* Return true if the argument is non-NULL and the WAL module is using
47449 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
47450 ** WAL module is using shared-memory, return false.
47462 ** stored in each frame (i.e. the db page-size when the WAL was created).
47521 ** (5) All writes to the database file are synced prior to the rollback journal
47541 ** method is a no-op, but that does not change the fact the SQLite will
47552 ** (11) A database file is well-formed at the beginning and at the conclusion
47576 ** to print out file-descriptors.
47579 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
47582 #define PAGERID(p) ((int)(p->fd))
47590 ** OPEN <------+------+
47593 ** +---------> READER-------+ |
47596 ** |<-------WRITER_LOCKED------> ERROR
47599 ** |<------WRITER_CACHEMOD-------->|
47602 ** |<-------WRITER_DBMOD---------->|
47605 ** +<------WRITER_FINISHED-------->+
47610 ** OPEN -> READER [sqlite3PagerSharedLock]
47611 ** READER -> OPEN [pager_unlock]
47613 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
47614 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
47615 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
47616 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
47617 ** WRITER_*** -> READER [pager_end_transaction]
47619 ** WRITER_*** -> ERROR [pager_error]
47620 ** ERROR -> OPEN [pager_unlock]
47626 ** state - the file may or may not be locked and the database size is
47636 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
47637 ** was) in exclusive-locking mode, a user-level read transaction is
47641 ** it opens a read-transaction on the database and returns to state
47642 ** OPEN after the read-transaction is completed. However a connection
47644 ** this state even after the read-transaction is closed. The only way
47648 ** * A read transaction may be active (but a write-transaction cannot).
47650 ** * The dbSize variable may be trusted (even if a user-level read
47654 ** * Even if a read-transaction is not open, it is guaranteed that
47655 ** there is no hot-journal in the file-system.
47659 ** The pager moves to this state from READER when a write-transaction
47661 ** required to start a write-transaction are held, but no actual
47676 ** * If the connection is open in rollback-mode, a RESERVED or greater
47678 ** * If the connection is open in WAL-mode, a WAL write transaction
47717 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
47733 ** The ERROR state is entered when an IO or disk-full error (including
47735 ** difficult to be sure that the in-memory pager state (cache contents,
47736 ** db size etc.) are consistent with the contents of the file-system.
47738 ** Temporary pager files may enter the ERROR state, but in-memory pagers
47742 ** the contents of the page-cache may be left in an inconsistent state.
47754 ** page-cache and any other in-memory state at the same time. Everything
47755 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
47756 ** when a read-transaction is next opened on the pager (transitioning
47772 ** In other cases, the error is returned to the b-tree layer. The b-tree
47776 ** Condition (3) is necessary because it can be triggered by a read-only
47778 ** code were simply returned to the user, the b-tree layer would not
47780 ** read-only statement cannot leave the pager in an internally inconsistent
47786 ** * The pager is not an in-memory pager.
47796 ** state. There are two exceptions: immediately after exclusive-mode has
47807 #define PAGER_WRITER_FINISHED 5
47812 ** following locking-states, according to the lock currently held on
47820 ** pagerUnlockDb() take a conservative approach - eLock is always updated
47831 ** from ERROR to OPEN state. At this point there may be a hot-journal file
47832 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
47836 ** of hot-journal detection.
47842 ** a hot-journal may be mistaken for a journal being created by an active
47849 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
47850 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
47851 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
47865 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
47867 if( P->xCodec==0 ){ O=(char*)D; }else \
47868 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
47870 # define CODEC1(P,D,N,X,E) /* NO-OP */
47890 ** set to 0. If a journal-header is written into the main journal while
47893 ** journal before the journal-header. This is required during savepoint
47902 Pgno iSubRec; /* Index of first record in sub-journal */
47926 ** For a real on-disk database, the current lock held on the database file -
47929 ** For a temporary or in-memory database (neither of which require any
47941 ** This boolean variable is used to make sure that the change-counter
47942 ** (the 4-byte header field at byte offset 24 of the database file) is
47945 ** It is set to true when the change-counter field is updated, which
47950 ** updating the change-counter is omitted for the current transaction.
47953 ** need only update the change-counter once, for the first transaction
47959 ** (or may not) specify a master-journal name to be written into the
47962 ** Whether or not a journal file contains a master-journal pointer affects
47965 ** If a journal file does not contain a master-journal pointer, it is
47967 ** it does contain a master-journal pointer the journal file is finalized
47972 ** simply by overwriting the first journal-header with zeroes, as the
47973 ** master journal pointer could interfere with hot-journal rollback of any
47983 ** This variables control the behavior of cache-spills (calls made by
47985 ** to the file-system in order to free up memory).
47997 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
47998 ** is larger than the database page-size in order to prevent a journal sync
48003 ** This is a boolean variable. If true, then any required sub-journal
48004 ** is opened as an in-memory journal file. If false, then in-memory
48005 ** sub-journals are only used for in-memory pager files.
48008 ** write-transaction is opened.
48019 ** is not an integer multiple of the page-size, the value stored in
48020 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
48022 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
48025 ** During a write-transaction, if pages with page-numbers greater than
48036 ** Throughout a write-transaction, dbFileSize contains the size of
48038 ** write-transaction is first opened, and updated when VFS calls are made
48058 ** write-transaction is opened (at the same time as dbFileSize and
48061 ** size-hint passed to the method call. See pager_write_pagelist() for
48069 ** sub-codes.
48084 u8 readOnly; /* True for a read-only database */
48098 u8 changeCountDone; /* Set after incrementing the change-counter */
48099 u8 setMaster; /* True if a m-j name has been written to jrnl */
48100 u8 doNotSpill; /* Do not spill the cache when non-zero */
48101 u8 subjInMemory; /* True to use in-memory sub-journals */
48109 int nRec; /* Pages journalled since last j-header written */
48110 u32 cksumInit; /* Quasi-random value added to every checksum */
48111 u32 nSubRec; /* Number of records written to sub-journal */
48115 sqlite3_file *sjfd; /* File descriptor for sub-journal */
48128 ** End of the routinely-changing class members
48131 u16 nExtra; /* Add this many bytes to each in-memory page */
48157 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
48158 char *zWal; /* File name for write-ahead log */
48174 ** a non-testing build. These variables are not thread-safe.
48193 ** written, semi-random garbage data might appear in the journal
48200 ** of a 32-bit checksum on each page of data. The checksum covers both
48201 ** the page number and the pPager->pageSize bytes of data for the page.
48202 ** This cksum is initialized to a 32-bit random value that appears in the
48218 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
48224 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
48227 ** The macro MEMDB is true if we are dealing with an in-memory database.
48235 # define MEMDB pPager->memDb
48240 ** interfaces to access the database using memory-mapped I/O.
48243 # define USEFETCH(x) ((x)->bUseFetch)
48249 ** The maximum legal page number is (2^31 - 1).
48255 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
48259 ** if( isOpen(pPager->jfd) ){ ...
48263 ** if( pPager->jfd->pMethods ){ ...
48265 #define isOpen(pFd) ((pFd)->pMethods!=0)
48268 ** Return true if this pager uses a write-ahead log to read page pgno.
48275 if( pPager->pWal==0 ) return 0;
48276 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
48281 # define pagerUseWal(x) ((x)->pWal!=0)
48303 assert( p->eState==PAGER_OPEN
48304 || p->eState==PAGER_READER
48305 || p->eState==PAGER_WRITER_LOCKED
48306 || p->eState==PAGER_WRITER_CACHEMOD
48307 || p->eState==PAGER_WRITER_DBMOD
48308 || p->eState==PAGER_WRITER_FINISHED
48309 || p->eState==PAGER_ERROR
48312 /* Regardless of the current state, a temp-file connection always behaves
48314 ** the change-counter field, so the changeCountDone flag is always set.
48316 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
48317 assert( p->tempFile==0 || pPager->changeCountDone );
48319 /* If the useJournal flag is clear, the journal-mode must be "OFF".
48320 ** And if the journal-mode is "OFF", the journal file must not be open.
48322 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
48323 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
48325 /* Check that MEMDB implies noSync. And an in-memory journal. Since
48326 ** this means an in-memory pager performs no IO at all, it cannot encounter
48328 ** a journal file. (although the in-memory journal implementation may
48330 ** is therefore not possible for an in-memory pager to enter the ERROR
48334 assert( !isOpen(p->fd) );
48335 assert( p->noSync );
48336 assert( p->journalMode==PAGER_JOURNALMODE_OFF
48337 || p->journalMode==PAGER_JOURNALMODE_MEMORY
48339 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
48346 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
48347 assert( p->eLock!=PENDING_LOCK );
48349 switch( p->eState ){
48352 assert( pPager->errCode==SQLITE_OK );
48353 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
48357 assert( pPager->errCode==SQLITE_OK );
48358 assert( p->eLock!=UNKNOWN_LOCK );
48359 assert( p->eLock>=SHARED_LOCK );
48363 assert( p->eLock!=UNKNOWN_LOCK );
48364 assert( pPager->errCode==SQLITE_OK );
48366 assert( p->eLock>=RESERVED_LOCK );
48368 assert( pPager->dbSize==pPager->dbOrigSize );
48369 assert( pPager->dbOrigSize==pPager->dbFileSize );
48370 assert( pPager->dbOrigSize==pPager->dbHintSize );
48371 assert( pPager->setMaster==0 );
48375 assert( p->eLock!=UNKNOWN_LOCK );
48376 assert( pPager->errCode==SQLITE_OK );
48383 assert( p->eLock>=RESERVED_LOCK );
48384 assert( isOpen(p->jfd)
48385 || p->journalMode==PAGER_JOURNALMODE_OFF
48386 || p->journalMode==PAGER_JOURNALMODE_WAL
48389 assert( pPager->dbOrigSize==pPager->dbFileSize );
48390 assert( pPager->dbOrigSize==pPager->dbHintSize );
48394 assert( p->eLock==EXCLUSIVE_LOCK );
48395 assert( pPager->errCode==SQLITE_OK );
48397 assert( p->eLock>=EXCLUSIVE_LOCK );
48398 assert( isOpen(p->jfd)
48399 || p->journalMode==PAGER_JOURNALMODE_OFF
48400 || p->journalMode==PAGER_JOURNALMODE_WAL
48402 assert( pPager->dbOrigSize<=pPager->dbHintSize );
48406 assert( p->eLock==EXCLUSIVE_LOCK );
48407 assert( pPager->errCode==SQLITE_OK );
48409 assert( isOpen(p->jfd)
48410 || p->journalMode==PAGER_JOURNALMODE_OFF
48411 || p->journalMode==PAGER_JOURNALMODE_WAL
48420 assert( pPager->errCode!=SQLITE_OK );
48421 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
48450 , p->zFilename
48451 , p->eState==PAGER_OPEN ? "OPEN" :
48452 p->eState==PAGER_READER ? "READER" :
48453 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
48454 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
48455 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
48456 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
48457 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
48458 , (int)p->errCode
48459 , p->eLock==NO_LOCK ? "NO_LOCK" :
48460 p->eLock==RESERVED_LOCK ? "RESERVED" :
48461 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
48462 p->eLock==SHARED_LOCK ? "SHARED" :
48463 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
48464 , p->exclusiveMode ? "exclusive" : "normal"
48465 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
48466 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
48467 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
48468 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
48469 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
48470 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
48471 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
48472 , p->journalOff, p->journalHdr
48473 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
48492 if( pPager->errCode ){
48493 pPager->xGet = getPageError;
48497 && pPager->xCodec==0
48500 pPager->xGet = getPageMMap;
48503 pPager->xGet = getPageNormal;
48508 ** Return true if it is necessary to write page *pPg into the sub-journal.
48509 ** A page needs to be written into the sub-journal if there exists one
48512 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
48513 ** * The bit corresponding to the page-number is not set in
48517 Pager *pPager = pPg->pPager;
48519 Pgno pgno = pPg->pgno;
48521 for(i=0; i<pPager->nSavepoint; i++){
48522 p = &pPager->aSavepoint[i];
48523 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
48535 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
48540 ** Read a 32-bit integer from the given file descriptor. Store the integer
48544 ** All values are stored on disk as big-endian.
48556 ** Write a 32-bit integer into a string buffer in big-endian byte order.
48562 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
48583 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
48586 if( isOpen(pPager->fd) ){
48587 assert( pPager->eLock>=eLock );
48588 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
48589 if( pPager->eLock!=UNKNOWN_LOCK ){
48590 pPager->eLock = (u8)eLock;
48611 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
48612 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
48613 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
48614 pPager->eLock = (u8)eLock;
48622 ** This function determines whether or not the atomic-write optimization
48631 ** an error to call this function if pPager is opened on an in-memory
48641 if( !pPager->tempFile ){
48646 assert( isOpen(pPager->fd) );
48647 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
48648 nSector = pPager->sectorSize;
48649 szPage = pPager->pageSize;
48671 ** Return a 32-bit hash of the page data for pPage.
48682 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
48685 pPage->pageHash = pager_pagehash(pPage);
48691 ** that the page is either dirty or still matches the calculated page-hash.
48695 Pager *pPager = pPg->pPager;
48696 assert( pPager->eState!=PAGER_ERROR );
48697 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
48718 ** nul-terminator), then this is handled as if no master journal name
48723 ** nul-terminator byte is appended to the buffer following the master
48743 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
48746 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
48747 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
48749 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
48756 cksum -= zMaster[u];
48762 ** master-journal filename.
48773 ** following the value in pPager->journalOff, assuming a sector
48774 ** size of pPager->sectorSize bytes.
48779 ** ---------------------------------------
48788 i64 c = pPager->journalOff;
48790 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
48794 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
48801 ** This function is a no-op if the journal file has not been written to
48804 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
48806 ** zero the 28-byte header at the start of the journal file. In either case,
48807 ** if the pager is not in no-sync mode, sync the journal file immediately
48810 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
48821 assert( isOpen(pPager->jfd) );
48822 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
48823 if( pPager->journalOff ){
48824 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
48828 rc = sqlite3OsTruncate(pPager->jfd, 0);
48831 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
48833 if( rc==SQLITE_OK && !pPager->noSync ){
48834 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
48845 rc = sqlite3OsFileSize(pPager->jfd, &sz);
48847 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
48860 ** - 8 bytes: Magic identifying journal format.
48861 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
48862 ** - 4 bytes: Random number used for page hash.
48863 ** - 4 bytes: Initial database page count.
48864 ** - 4 bytes: Sector size used by the process that wrote this journal.
48865 ** - 4 bytes: Database page size.
48867 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
48871 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
48872 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
48876 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
48886 for(ii=0; ii<pPager->nSavepoint; ii++){
48887 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
48888 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
48892 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
48895 ** Write the nRec Field - the number of page records that follow this
48898 ** if in full-sync mode), the zero is overwritten with the true number
48908 ** * When the pager is in no-sync mode. Corruption can follow a
48914 assert( isOpen(pPager->fd) || pPager->noSync );
48915 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
48916 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
48924 /* The random check-hash initializer */
48925 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
48926 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
48928 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
48930 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
48933 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
48941 nHeader-(sizeof(aJournalMagic)+20));
48952 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
48955 ** The loop is required here in case the sector-size is larger than the
48961 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
48962 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
48963 assert( pPager->journalHdr <= pPager->journalOff );
48964 pPager->journalOff += nHeader;
48974 ** pPager->journalOff. See comments above function writeJournalHdr() for
48979 ** database before the transaction began, in pages. Also, pPager->cksumInit
48998 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
49004 pPager->journalOff = journalHdrOffset(pPager);
49005 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
49008 iHdrOff = pPager->journalOff;
49015 if( isHot || iHdrOff!=pPager->journalHdr ){
49016 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
49025 /* Read the first three 32-bit fields of the journal header: The nRec
49026 ** field, the checksum-initializer and the database size at the start
49029 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
49030 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
49031 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
49036 if( pPager->journalOff==0 ){
49037 u32 iPageSize; /* Page-size field of journal header */
49038 u32 iSectorSize; /* Sector-size field of journal header */
49040 /* Read the page-size and sector-size journal header fields. */
49041 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
49042 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
49047 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
49052 iPageSize = pPager->pageSize;
49055 /* Check that the values read from the page-size and sector-size fields
49062 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
49064 /* If the either the page-size or sector-size in the journal-header is
49065 ** invalid, then the process that wrote the journal-header must have
49072 /* Update the page-size to match the value read from the journal.
49076 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
49079 /* Update the assumed sector-size to match the value used by
49085 pPager->sectorSize = iSectorSize;
49088 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
49096 ** thing written to a journal file. If the pager is in full-sync mode, the
49101 ** + N bytes: Master journal filename in utf-8.
49102 ** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
49107 ** journal name, where each byte is interpreted as a signed 8-bit integer.
49110 ** this call is a no-op.
49119 assert( pPager->setMaster==0 );
49123 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
49124 || !isOpen(pPager->jfd)
49128 pPager->setMaster = 1;
49129 assert( pPager->journalHdr <= pPager->journalOff );
49136 /* If in full-sync mode, advance to the next disk sector before writing
49140 if( pPager->fullSync ){
49141 pPager->journalOff = journalHdrOffset(pPager);
49143 iHdrOff = pPager->journalOff;
49148 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
49149 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
49150 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
49151 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
49152 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
49157 pPager->journalOff += (nMaster+20);
49159 /* If the pager is in peristent-journal mode, then the physical
49160 ** journal-file may extend past the end of the master-journal name
49162 ** dangerous because the code to rollback a hot-journal file
49163 ** will not be able to find the master-journal name to determine
49169 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
49170 && jrnlSize>pPager->journalOff
49172 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
49178 ** Discard the entire contents of the in-memory page-cache.
49181 pPager->iDataVersion++;
49182 sqlite3BackupRestart(pPager->pBackup);
49183 sqlite3PcacheClear(pPager->pPCache);
49187 ** Return the pPager->iDataVersion value
49190 assert( pPager->eState>PAGER_OPEN );
49191 return pPager->iDataVersion;
49196 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
49201 for(ii=0; ii<pPager->nSavepoint; ii++){
49202 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
49204 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
49205 sqlite3OsClose(pPager->sjfd);
49207 sqlite3_free(pPager->aSavepoint);
49208 pPager->aSavepoint = 0;
49209 pPager->nSavepoint = 0;
49210 pPager->nSubRec = 0;
49222 for(ii=0; ii<pPager->nSavepoint; ii++){
49223 PagerSavepoint *p = &pPager->aSavepoint[ii];
49224 if( pgno<=p->nOrig ){
49225 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
49234 ** This function is a no-op if the pager is in exclusive mode and not
49238 ** If the pager is not in exclusive-access mode, the database file is
49239 ** completely unlocked. If the file is unlocked and the file-system does
49245 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
49246 ** or not, any journal file left in the file-system will be treated
49247 ** as a hot-journal and rolled back the next time a read-transaction
49252 assert( pPager->eState==PAGER_READER
49253 || pPager->eState==PAGER_OPEN
49254 || pPager->eState==PAGER_ERROR
49257 sqlite3BitvecDestroy(pPager->pInJournal);
49258 pPager->pInJournal = 0;
49262 assert( !isOpen(pPager->jfd) );
49263 sqlite3WalEndReadTransaction(pPager->pWal);
49264 pPager->eState = PAGER_OPEN;
49265 }else if( !pPager->exclusiveMode ){
49267 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
49274 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
49275 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
49276 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
49277 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
49278 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
49279 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
49281 || 1!=(pPager->journalMode & 5)
49283 sqlite3OsClose(pPager->jfd);
49292 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
49293 pPager->eLock = UNKNOWN_LOCK;
49297 ** without clearing the error code. This is intentional - the error
49300 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
49301 pPager->changeCountDone = 0;
49302 pPager->eState = PAGER_OPEN;
49308 ** normal and exclusive-locking mode.
49310 assert( pPager->errCode==SQLITE_OK || !MEMDB );
49311 if( pPager->errCode ){
49312 if( pPager->tempFile==0 ){
49314 pPager->changeCountDone = 0;
49315 pPager->eState = PAGER_OPEN;
49317 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
49319 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
49320 pPager->errCode = SQLITE_OK;
49324 pPager->journalOff = 0;
49325 pPager->journalHdr = 0;
49326 pPager->setMaster = 0;
49333 ** the error-code about to be returned by a pager API function. The
49337 ** IOERR sub-codes, the pager enters the ERROR state and the error code
49341 ** The ERROR state indicates that the contents of the pager-cache
49343 ** the contents of the pager-cache. If a transaction was active when
49346 ** it were a hot-journal).
49352 pPager->errCode==SQLITE_FULL ||
49353 pPager->errCode==SQLITE_OK ||
49354 (pPager->errCode & 0xff)==SQLITE_IOERR
49357 pPager->errCode = rc;
49358 pPager->eState = PAGER_ERROR;
49374 ** * For non-TEMP databases, always sync to disk. This is necessary
49383 if( pPager->tempFile==0 ) return 1;
49385 if( !isOpen(pPager->fd) ) return 0;
49386 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
49392 ** after rollback of a hot-journal, or if an error occurs while opening
49393 ** the journal file or writing the very first journal-header of a
49398 ** exclusive than a RESERVED lock, it is a no-op.
49404 ** transaction. Nor will it be considered to be a hot-journal by this
49407 ** the current journal-mode (Pager.journalMode value), as follows:
49411 ** in-memory journal.
49430 ** If running in non-exclusive rollback mode, the lock on the file is
49448 ** is no write-transaction active but a RESERVED or greater lock is
49451 ** 1. After a successful hot-journal rollback, it is called with
49456 ** read-transaction, this function is called with eState==PAGER_READER
49457 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
49460 assert( pPager->eState!=PAGER_ERROR );
49461 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
49466 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
49467 if( isOpen(pPager->jfd) ){
49471 if( sqlite3JournalIsInMemory(pPager->jfd) ){
49472 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
49473 sqlite3OsClose(pPager->jfd);
49474 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
49475 if( pPager->journalOff==0 ){
49478 rc = sqlite3OsTruncate(pPager->jfd, 0);
49479 if( rc==SQLITE_OK && pPager->fullSync ){
49485 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
49488 pPager->journalOff = 0;
49489 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
49490 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
49492 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
49493 pPager->journalOff = 0;
49496 ** a hot-journal was just rolled back. In this case the journal
49498 ** the database file, it will do so using an in-memory journal.
49500 int bDelete = !pPager->tempFile;
49501 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
49502 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
49503 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
49504 || pPager->journalMode==PAGER_JOURNALMODE_WAL
49506 sqlite3OsClose(pPager->jfd);
49508 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
49514 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
49515 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
49518 p->pageHash = 0;
49524 sqlite3BitvecDestroy(pPager->pInJournal);
49525 pPager->pInJournal = 0;
49526 pPager->nRec = 0;
49529 sqlite3PcacheCleanAll(pPager->pPCache);
49531 sqlite3PcacheClearWritable(pPager->pPCache);
49533 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
49537 /* Drop the WAL write-lock, if any. Also, if the connection was in
49541 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
49543 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
49544 /* This branch is taken when committing a transaction in rollback-journal
49550 assert( pPager->eLock==EXCLUSIVE_LOCK );
49551 rc = pager_truncate(pPager, pPager->dbSize);
49554 if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
49555 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
49559 if( !pPager->exclusiveMode
49560 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
49563 pPager->changeCountDone = 0;
49565 pPager->eState = PAGER_READER;
49566 pPager->setMaster = 0;
49577 ** call to pager_unlock() will discard all in-memory pages, unlock
49579 ** means that there is a hot-journal left in the file-system, the next
49589 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
49591 if( pPager->eState>=PAGER_WRITER_LOCKED ){
49595 }else if( !pPager->exclusiveMode ){
49596 assert( pPager->eState==PAGER_READER );
49604 ** Parameter aData must point to a buffer of pPager->pageSize bytes
49606 ** page of data and the current value of pPager->cksumInit.
49609 ** random initial value (pPager->cksumInit) and every 200th byte
49610 ** of the page data, starting with byte offset (pPager->pageSize%200).
49611 ** Each byte is interpreted as an 8-bit unsigned integer.
49623 u32 cksum = pPager->cksumInit; /* Checksum value to return */
49624 int i = pPager->pageSize-200; /* Loop counter */
49627 i -= 200;
49638 if( pPager->xCodecSizeChng ){
49639 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
49640 (int)pPager->nReserve);
49644 # define pagerReportSize(X) /* No-op if we do not support a codec */
49654 if( pDest->nReserve!=pSrc->nReserve ){
49655 pDest->nReserve = pSrc->nReserve;
49663 ** from the sub-journal (if isMainJrnl==0) and playback that page.
49667 ** The main rollback journal uses checksums - the statement journal does
49670 ** If the page number of the page record read from the (sub-)journal file
49680 ** If the page record is successfully read from the (sub-)journal file
49682 ** while reading the record from the (sub-)journal file or while writing
49684 ** is successfully read from the (sub-)journal file but appears to be
49688 ** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
49702 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
49714 ** the codec. It is false for pure in-memory journals. */
49715 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
49720 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
49721 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
49723 aData = pPager->pTmpSpace;
49729 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
49730 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
49731 ** only reads from the main journal, not the sub-journal.
49733 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
49734 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
49736 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
49738 /* Read the page number and page data from the journal or sub-journal
49741 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
49744 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
49746 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
49757 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
49761 rc = read32bits(jfd, (*pOffset)-4, &cksum);
49777 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
49778 pPager->nReserve = ((u8*)aData)[20];
49786 ** An exception to the above rule: If the database is in no-sync mode
49796 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
49813 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
49823 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
49825 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
49826 (isMainJrnl?"main-journal":"sub-journal")
49829 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
49831 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
49833 if( isOpen(pPager->fd)
49834 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
49837 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
49838 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
49842 ** This is usually safe even for an encrypted database - as the data
49844 ** is if the data was just read from an in-memory sub-journal. In that
49850 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
49854 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
49856 if( pgno>pPager->dbFileSize ){
49857 pPager->dbFileSize = pgno;
49859 if( pPager->pBackup ){
49863 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
49867 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
49871 ** the database and the page is not in-memory, there is a potential
49872 ** problem. When the page is next fetched by the b-tree layer, it
49878 ** if the page is on the free-list at the start of the transaction, then
49881 ** The solution is to add an in-memory page to the cache containing
49882 ** the data just read from the sub-journal. Mark the page as dirty
49883 ** and if the pager requires a journal-sync, then mark the page as
49884 ** requiring a journal-sync before it is written.
49887 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
49888 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
49890 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
49891 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
49903 pData = pPg->pData;
49904 memcpy(pData, (u8*)aData, pPager->pageSize);
49905 pPager->xReiniter(pPg);
49915 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
49920 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
49937 ** of all of its child journals, one after another, formatted as utf-8
49939 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
49942 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
49947 ** This function reads the contents of the master-journal file into
49958 ** the file-system using sqlite3OsDelete().
49967 ** a couple of kilobytes or so - potentially larger than the page
49971 sqlite3_vfs *pVfs = pPager->pVfs;
49973 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
49974 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
49984 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
49985 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
49997 ** journal files extracted from regular rollback-journals.
50001 nMasterPtr = pVfs->mxPathname+1;
50013 while( (zJournal-zMasterJournal)<nMasterJournal ){
50062 ** file in the file-system. This only happens when committing a transaction,
50063 ** or rolling back a transaction (including rolling back a hot-journal).
50066 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
50067 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
50082 assert( pPager->eState!=PAGER_ERROR );
50083 assert( pPager->eState!=PAGER_READER );
50085 if( isOpen(pPager->fd)
50086 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
50089 int szPage = pPager->pageSize;
50090 assert( pPager->eLock==EXCLUSIVE_LOCK );
50092 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
50096 rc = sqlite3OsTruncate(pPager->fd, newSize);
50098 char *pTmp = pPager->pTmpSpace;
50100 testcase( (newSize-szPage) == currentSize );
50101 testcase( (newSize-szPage) > currentSize );
50102 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
50105 pPager->dbFileSize = nPage;
50113 ** Return a sanitized version of the sector-size of OS file pFile. The
50136 ** Otherwise, for non-temporary files, the effective sector size is
50143 ** pPager->sectorSize is to define the "blast radius" of bytes that
50151 assert( isOpen(pPager->fd) || pPager->tempFile );
50153 if( pPager->tempFile
50154 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
50160 pPager->sectorSize = 512;
50162 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
50173 ** (2) 4 byte big-endian integer which is the number of valid page records
50176 ** (3) 4 byte big-endian integer which is the initial value for the
50180 ** (5) 4 byte big-endian integer which is the sector size. The header
50182 ** (6) 4 byte big-endian integer which is the page size.
50186 ** + pPager->pageSize bytes of data.
50203 ** no-sync option for the journal. A power failure could lead to corruption
50207 ** If the file opened as the journal file is not a well-formed
50213 ** If an I/O or malloc() error occurs, the journal-file is not deleted
50224 sqlite3_vfs *pVfs = pPager->pVfs;
50238 assert( isOpen(pPager->jfd) );
50239 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
50251 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
50255 zMaster = pPager->pTmpSpace;
50256 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
50264 pPager->journalOff = 0;
50286 ** working in no-sync mode. This means that the rest of the journal
50291 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
50292 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
50310 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
50311 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
50317 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
50322 pPager->dbSize = mxPg;
50333 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
50338 pPager->journalOff = szJ;
50365 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
50369 if( pPager->fd->pMethods ){
50370 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
50375 ** malloc error that occurred after the change-counter was updated but
50376 ** before the transaction was committed, then the change-counter
50379 ** update the change-counter at all. This may lead to cache inconsistency
50383 pPager->changeCountDone = pPager->tempFile;
50386 zMaster = pPager->pTmpSpace;
50387 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
50391 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
50408 nPlayback, pPager->zJournal);
50422 ** pPg->pData. A shared lock or greater must be held on the database
50432 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
50433 Pgno pgno = pPg->pgno; /* Page number to read */
50435 int pgsz = pPager->pageSize; /* Number of bytes to read */
50437 assert( pPager->eState>=PAGER_READER && !MEMDB );
50438 assert( isOpen(pPager->fd) );
50442 /* Try to pull the page from the write-ahead log. */
50443 rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
50447 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
50448 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
50461 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
50468 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
50470 u8 *dbFileVers = &((u8*)pPg->pData)[24];
50471 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
50474 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT);
50477 PAGER_INCR(pPager->nRead);
50486 ** Update the value of the change-counter at offsets 24 and 92 in
50490 ** routine which only updates the change-counter if the update is actually
50491 ** needed, as determined by the pPager->changeCountDone state variable.
50497 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
50498 put32bits(((char*)pPg->pData)+24, change_counter);
50503 put32bits(((char*)pPg->pData)+92, change_counter);
50504 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
50532 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
50537 pPager->xReiniter(pPg);
50551 sqlite3BackupRestart(pPager->pBackup);
50570 pPager->dbSize = pPager->dbOrigSize;
50571 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
50572 pList = sqlite3PcacheDirtyList(pPager->pPCache);
50574 PgHdr *pNext = pList->pDirty;
50575 rc = pagerUndoCallback((void *)pPager, pList->pgno);
50601 assert( pPager->pWal );
50605 for(p=pList; p && p->pDirty; p=p->pDirty){
50606 assert( p->pgno < p->pDirty->pgno );
50610 assert( pList->pDirty==0 || isCommit );
50618 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
50619 if( p->pgno<=nTruncate ){
50620 ppNext = &p->pDirty;
50628 pPager->aStat[PAGER_STAT_WRITE] += nList;
50630 if( pList->pgno==1 ) pager_write_changecounter(pList);
50631 rc = sqlite3WalFrames(pPager->pWal,
50632 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
50634 if( rc==SQLITE_OK && pPager->pBackup ){
50635 for(p=pList; p; p=p->pDirty){
50636 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
50641 pList = sqlite3PcacheDirtyList(pPager->pPCache);
50642 for(p=pList; p; p=p->pDirty){
50663 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
50670 sqlite3WalEndReadTransaction(pPager->pWal);
50672 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
50675 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
50694 /* Query the WAL sub-system for the database size. The WalDbsize()
50697 ** available from the WAL sub-system if the log file is empty or
50700 assert( pPager->eState==PAGER_OPEN );
50701 assert( pPager->eLock>=SHARED_LOCK );
50702 assert( isOpen(pPager->fd) );
50703 assert( pPager->tempFile==0 );
50704 nPage = sqlite3WalDbsize(pPager->pWal);
50707 ** WAL sub-system, determine the page count based on the size of
50709 ** integer multiple of the page-size, round up the result.
50711 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
50713 int rc = sqlite3OsFileSize(pPager->fd, &n);
50717 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
50724 if( nPage>pPager->mxPgno ){
50725 pPager->mxPgno = (Pgno)nPage;
50734 ** Check if the *-wal file that corresponds to the database opened by pPager
50735 ** exists if the database is not empy, or verify that the *-wal file does
50738 ** If the database is not empty and the *-wal file exists, open the pager
50739 ** in WAL mode. If the database is empty or if no *-wal file exists and
50747 ** a WAL on a none-empty database, this ensures there is no race condition
50753 assert( pPager->eState==PAGER_OPEN );
50754 assert( pPager->eLock>=SHARED_LOCK );
50756 if( !pPager->tempFile ){
50759 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
50768 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
50770 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
50773 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
50774 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
50788 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
50801 ** * Pages are then played back from the sub-journal file, starting
50815 ** (or transaction). No page with a page-number greater than this value
50820 i64 iHdrOff; /* End of first segment of main-journal records */
50824 assert( pPager->eState!=PAGER_ERROR );
50825 assert( pPager->eState>=PAGER_WRITER_LOCKED );
50829 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
50838 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
50839 pPager->changeCountDone = pPager->tempFile;
50845 /* Use pPager->journalOff as the effective size of the main rollback
50848 ** past pPager->journalOff is off-limits to us.
50850 szJ = pPager->journalOff;
50856 ** greater than the current database size (pPager->dbSize) but those
50861 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
50862 pPager->journalOff = pSavepoint->iOffset;
50863 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
50864 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
50868 pPager->journalOff = 0;
50873 ** of the main journal file. Continue to skip out-of-range pages and
50876 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
50884 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
50889 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
50891 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
50893 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
50894 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
50898 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
50900 /* Finally, rollback pages from the sub-journal. Page that were
50902 ** will be skipped. Out-of-range pages are also skipped.
50906 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
50909 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
50911 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
50912 assert( offset==(i64)ii*(4+pPager->pageSize) );
50920 pPager->journalOff = szJ;
50927 ** Change the maximum number of in-memory pages that are allowed
50931 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
50935 ** Change the maximum number of in-memory pages that are allowed
50939 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
50947 sqlite3_file *fd = pPager->fd;
50948 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
50950 sz = pPager->szMmap;
50951 pPager->bUseFetch = (sz>0);
50953 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
50962 pPager->szMmap = szMmap;
50970 sqlite3PcacheShrink(pPager->pPCache);
50992 ** database (with some additional information - the nRec field
50993 ** of the journal header - being written in between the two
51003 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
51014 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
51030 if( pPager->tempFile ){
51031 pPager->noSync = 1;
51032 pPager->fullSync = 0;
51033 pPager->extraSync = 0;
51035 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
51036 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
51037 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
51039 if( pPager->noSync ){
51040 pPager->syncFlags = 0;
51041 pPager->ckptSyncFlags = 0;
51043 pPager->syncFlags = SQLITE_SYNC_FULL;
51044 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
51046 pPager->syncFlags = SQLITE_SYNC_NORMAL;
51047 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
51049 pPager->syncFlags = SQLITE_SYNC_NORMAL;
51050 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
51052 pPager->walSyncFlags = pPager->syncFlags;
51053 if( pPager->fullSync ){
51054 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
51057 pPager->doNotSpill &= ~SPILLFLAG_OFF;
51059 pPager->doNotSpill |= SPILLFLAG_OFF;
51101 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
51109 ** The pager invokes the busy-handler if sqlite3OsLock() returns
51110 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
51114 ** (which occurs during hot-journal rollback). Summary:
51117 ** --------------------------------------------------------
51118 ** NO_LOCK -> SHARED_LOCK | Yes
51119 ** SHARED_LOCK -> RESERVED_LOCK | No
51120 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
51121 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
51123 ** If the busy-handler callback returns non-zero, the lock is
51129 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
51132 pPager->xBusyHandler = xBusyHandler;
51133 pPager->pBusyHandlerArg = pBusyHandlerArg;
51135 if( isOpen(pPager->fd) ){
51136 void **ap = (void **)&pPager->xBusyHandler;
51139 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
51148 ** is a no-op. The value returned is the error state error code (i.e.
51149 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
51158 ** * the database is either not an in-memory database or it is
51159 ** an in-memory database that currently consists of zero pages.
51183 ** is a no-op for that case anyhow.
51188 if( (pPager->memDb==0 || pPager->dbSize==0)
51189 && sqlite3PcacheRefCount(pPager->pPCache)==0
51190 && pageSize && pageSize!=(u32)pPager->pageSize
51195 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
51196 rc = sqlite3OsFileSize(pPager->fd, &nByte);
51205 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
51208 sqlite3PageFree(pPager->pTmpSpace);
51209 pPager->pTmpSpace = pNew;
51210 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
51211 pPager->pageSize = pageSize;
51217 *pPageSize = pPager->pageSize;
51219 if( nReserve<0 ) nReserve = pPager->nReserve;
51221 pPager->nReserve = (i16)nReserve;
51237 return pPager->pTmpSpace;
51249 pPager->mxPgno = mxPage;
51251 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
51252 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
51253 return pPager->mxPgno;
51261 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
51270 sqlite3_io_error_pending = -1;
51297 assert( isOpen(pPager->fd) || pPager->tempFile );
51305 if( isOpen(pPager->fd) ){
51307 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
51316 ** This function may only be called when a read-transaction is open on
51319 ** However, if the file is between 1 and <page-size> bytes in size, then
51323 assert( pPager->eState>=PAGER_READER );
51324 assert( pPager->eState!=PAGER_WRITER_FINISHED );
51325 *pnPage = (int)pPager->dbSize;
51331 ** a similar or greater lock is already held, this function is a no-op
51346 /* Check that this is either a no-op (because the requested lock is
51347 ** already held), or one of the transitions that the busy-handler
51351 assert( (pPager->eLock>=locktype)
51352 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
51353 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
51358 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
51364 ** following is true for all dirty pages currently in the page-cache:
51370 ** be necessary to write the current content out to the sub-journal
51380 ** sub-journal rolled back the content could not be restored and the
51386 assert( pPg->flags&PGHDR_DIRTY );
51387 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
51390 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
51397 ** Truncate the in-memory database file image to nPage pages. This
51408 assert( pPager->dbSize>=nPage );
51409 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
51410 pPager->dbSize = nPage;
51425 ** This function is called before attempting a hot-journal rollback. It
51426 ** syncs the journal file to disk, then sets pPager->journalHdr to the
51430 ** Syncing a hot-journal to disk before attempting to roll it back ensures
51431 ** that if a power-failure occurs during the rollback, the process that
51440 if( !pPager->noSync ){
51441 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
51444 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
51468 if( pPager->pMmapFreelist ){
51469 *ppPage = p = pPager->pMmapFreelist;
51470 pPager->pMmapFreelist = p->pDirty;
51471 p->pDirty = 0;
51472 assert( pPager->nExtra>=8 );
51473 memset(p->pExtra, 0, 8);
51475 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
51477 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
51480 p->pExtra = (void *)&p[1];
51481 p->flags = PGHDR_MMAP;
51482 p->nRef = 1;
51483 p->pPager = pPager;
51486 assert( p->pExtra==(void *)&p[1] );
51487 assert( p->pPage==0 );
51488 assert( p->flags==PGHDR_MMAP );
51489 assert( p->pPager==pPager );
51490 assert( p->nRef==1 );
51492 p->pgno = pgno;
51493 p->pData = pData;
51494 pPager->nMmapOut++;
51505 Pager *pPager = pPg->pPager;
51506 pPager->nMmapOut--;
51507 pPg->pDirty = pPager->pMmapFreelist;
51508 pPager->pMmapFreelist = pPg;
51510 assert( pPager->fd->pMethods->iVersion>=3 );
51511 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
51520 for(p=pPager->pMmapFreelist; p; p=pNext){
51521 pNext = p->pDirty;
51542 u8 *pTmp = (u8 *)pPager->pTmpSpace;
51549 /* pPager->errCode = 0; */
51550 pPager->exclusiveMode = 0;
51552 assert( db || pPager->pWal==0 );
51553 sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags, pPager->pageSize,
51554 (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
51556 pPager->pWal = 0;
51570 ** back or finalize it. The next database user will have to do hot-journal
51573 if( isOpen(pPager->jfd) ){
51582 sqlite3OsClose(pPager->jfd);
51583 sqlite3OsClose(pPager->fd);
51585 sqlite3PcacheClose(pPager->pPCache);
51588 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
51591 assert( !pPager->aSavepoint && !pPager->pInJournal );
51592 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
51603 return pPg->pgno;
51617 ** disk and can be restored in the event of a hot-journal rollback.
51619 ** If the Pager.noSync flag is set, then this function is a no-op.
51620 ** Otherwise, the actions required depend on the journal-mode and the
51621 ** device characteristics of the file-system, as follows:
51623 ** * If the journal file is an in-memory journal file, no action need
51629 ** been written following it. If the pager is operating in full-sync
51635 ** Or, in pseudo-code:
51637 ** if( NOT <in-memory journal> ){
51639 ** if( <full-sync mode> ) xSync(<journal file>);
51652 assert( pPager->eState==PAGER_WRITER_CACHEMOD
51653 || pPager->eState==PAGER_WRITER_DBMOD
51661 if( !pPager->noSync ){
51662 assert( !pPager->tempFile );
51663 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
51664 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
51665 assert( isOpen(pPager->jfd) );
51669 ** that wrote to this database was operating in persistent-journal
51672 ** file happens to be a journal-header (written as part of the
51673 ** previous connection's transaction), and a crash or power-failure
51677 ** hot-journal rollback following recovery. It may roll back all
51679 ** out-of-date data that follows it. Database corruption.
51695 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
51698 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
51701 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
51708 ** full-synchronous mode, sync the journal first. This ensures that
51718 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
51721 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
51724 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
51726 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
51733 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
51734 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
51739 pPager->journalHdr = pPager->journalOff;
51741 pPager->nRec = 0;
51746 pPager->journalHdr = pPager->journalOff;
51754 sqlite3PcacheClearSyncFlags(pPager->pPCache);
51755 pPager->eState = PAGER_WRITER_DBMOD;
51763 ** in-memory pages in the list to the database file. The argument may
51765 ** a no-op.
51772 ** If the pager is a temp-file pager and the actual file-system file
51797 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
51798 assert( pPager->eLock==EXCLUSIVE_LOCK );
51799 assert( isOpen(pPager->fd) || pList->pDirty==0 );
51801 /* If the file is a temp-file has not yet been opened, open it now. It
51803 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
51805 if( !isOpen(pPager->fd) ){
51806 assert( pPager->tempFile && rc==SQLITE_OK );
51807 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
51813 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
51815 && pPager->dbHintSize<pPager->dbSize
51816 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
51818 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
51819 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
51820 pPager->dbHintSize = pPager->dbSize;
51824 Pgno pgno = pList->pgno;
51828 ** make the file smaller (presumably by auto-vacuum code). Do not write
51834 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
51835 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
51838 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
51839 if( pList->pgno==1 ) pager_write_changecounter(pList);
51842 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
51845 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
51852 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
51854 if( pgno>pPager->dbFileSize ){
51855 pPager->dbFileSize = pgno;
51857 pPager->aStat[PAGER_STAT_WRITE]++;
51860 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
51870 pList = pList->pDirty;
51877 ** Ensure that the sub-journal file is open. If it is already open, this
51878 ** function is a no-op.
51886 if( !isOpen(pPager->sjfd) ){
51891 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
51892 nStmtSpill = -1;
51894 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
51900 ** Append a record of the current state of page pPg to the sub-journal.
51902 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
51906 ** error code if the attempt to write to the sub-journal fails, or
51912 Pager *pPager = pPg->pPager;
51913 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
51915 /* Open the sub-journal, if it has not already been opened */
51916 assert( pPager->useJournal );
51917 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
51918 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
51921 || pPg->pgno>pPager->dbOrigSize
51925 /* If the sub-journal was opened successfully (or was already open),
51928 void *pData = pPg->pData;
51929 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
51933 if( !pPager->subjInMemory ){
51934 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
51938 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
51939 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
51941 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
51946 pPager->nSubRec++;
51947 assert( pPager->nSavepoint>0 );
51948 rc = addToSavepointBitvecs(pPager, pPg->pgno);
51963 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
51983 assert( pPg->pPager==pPager );
51984 assert( pPg->flags&PGHDR_DIRTY );
52002 if( NEVER(pPager->errCode) ) return SQLITE_OK;
52003 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
52004 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
52005 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
52006 if( pPager->doNotSpill
52007 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
52008 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
52013 pPg->pDirty = 0;
52023 if( pPg->flags&PGHDR_NEED_SYNC
52024 || pPager->eState==PAGER_WRITER_CACHEMOD
52031 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
52038 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
52049 int rc = pPager->errCode;
52051 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
52054 PgHdr *pNext = pList->pDirty;
52055 if( pList->nRef==0 ){
52071 ** If zFilename is NULL then a randomly-named temporary file is created
52075 ** This can be used to implement an in-memory database.
52101 int nExtra, /* Extra bytes append to each in-memory page */
52109 int tempFile = 0; /* True for temp files (incl. in-memory files) */
52110 int memDb = 0; /* True if this is an in-memory file */
52111 int readOnly = 0; /* True if this is a read-only file */
52121 /* Figure out how much space is required for each journal file-handle
52122 ** (there are two of them, the main journal and the sub-journal). */
52146 nPathname = pVfs->mxPathname+1;
52159 nUri = (int)(&z[1] - zUri);
52161 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
52163 ** the database being opened will be more than pVfs->mxPathname
52166 ** check for a hot-journal before reading.
52182 ** Database file handle (pVfs->szOsFile bytes)
52183 ** Sub-journal file handle (journalFileSize bytes)
52191 ROUND8(pVfs->szOsFile) + /* The main db file */
52205 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
52206 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
52207 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
52208 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
52209 pPager->zFilename = (char*)(pPtr += journalFileSize);
52210 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
52215 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
52216 memcpy(pPager->zFilename, zPathname, nPathname);
52217 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
52218 memcpy(pPager->zJournal, zPathname, nPathname);
52219 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
52220 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
52222 pPager->zWal = &pPager->zJournal[nPathname+8+1];
52223 memcpy(pPager->zWal, zPathname, nPathname);
52224 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
52225 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
52229 pPager->pVfs = pVfs;
52230 pPager->vfsFlags = vfsFlags;
52236 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
52249 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
52253 if( szPageDflt<pPager->sectorSize ){
52254 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
52257 szPageDflt = (u32)pPager->sectorSize;
52274 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
52286 ** This branch is also run for an in-memory database. An in-memory
52287 ** database is the same as a temp-file that is never written out to
52288 ** disk and uses an in-memory rollback journal.
52294 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
52295 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
52296 pPager->noLock = 1; /* Do no locking */
52304 assert( pPager->memDb==0 );
52305 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
52314 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
52320 sqlite3OsClose(pPager->fd);
52321 sqlite3PageFree(pPager->pTmpSpace);
52326 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
52327 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
52329 pPager->useJournal = (u8)useJournal;
52330 /* pPager->stmtOpen = 0; */
52331 /* pPager->stmtInUse = 0; */
52332 /* pPager->nRef = 0; */
52333 /* pPager->stmtSize = 0; */
52334 /* pPager->stmtJSize = 0; */
52335 /* pPager->nPage = 0; */
52336 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
52337 /* pPager->state = PAGER_UNLOCK; */
52338 /* pPager->errMask = 0; */
52339 pPager->tempFile = (u8)tempFile;
52343 pPager->exclusiveMode = (u8)tempFile;
52344 pPager->changeCountDone = pPager->tempFile;
52345 pPager->memDb = (u8)memDb;
52346 pPager->readOnly = (u8)readOnly;
52347 assert( useJournal || pPager->tempFile );
52348 pPager->noSync = pPager->tempFile;
52349 if( pPager->noSync ){
52350 assert( pPager->fullSync==0 );
52351 assert( pPager->extraSync==0 );
52352 assert( pPager->syncFlags==0 );
52353 assert( pPager->walSyncFlags==0 );
52354 assert( pPager->ckptSyncFlags==0 );
52356 pPager->fullSync = 1;
52357 pPager->extraSync = 0;
52358 pPager->syncFlags = SQLITE_SYNC_NORMAL;
52359 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
52360 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
52362 /* pPager->pFirst = 0; */
52363 /* pPager->pFirstSynced = 0; */
52364 /* pPager->pLast = 0; */
52365 pPager->nExtra = (u16)nExtra;
52366 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
52367 assert( isOpen(pPager->fd) || tempFile );
52370 pPager->journalMode = PAGER_JOURNALMODE_OFF;
52372 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
52374 /* pPager->xBusyHandler = 0; */
52375 /* pPager->pBusyHandlerArg = 0; */
52376 pPager->xReiniter = xReinit;
52378 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
52379 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
52388 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52395 if( pPager->tempFile ) return SQLITE_OK;
52396 if( pPager->dbSize==0 ) return SQLITE_OK;
52397 assert( pPager->zFilename && pPager->zFilename[0] );
52398 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52400 /* If the HAS_MOVED file-control is unimplemented, assume that the file
52414 ** the file-system for the given pager. A hot journal is one that
52415 ** needs to be played back. According to this function, a hot-journal
52432 ** case this routine will return a false-positive. The pager_playback()
52436 ** If a hot-journal file is found to exist, *pExists is set to 1 and
52437 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
52439 ** to determine whether or not a hot-journal file exists, the IO error
52443 sqlite3_vfs * const pVfs = pPager->pVfs;
52446 int jrnlOpen = !!isOpen(pPager->jfd);
52448 assert( pPager->useJournal );
52449 assert( isOpen(pPager->fd) );
52450 assert( pPager->eState==PAGER_OPEN );
52452 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
52458 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
52468 ** in fact there is none. This results in a false-positive which will
52471 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
52475 assert( pPager->tempFile==0 );
52489 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
52490 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
52496 ** at least one non-zero bytes at the start of the journal file.
52502 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
52506 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
52511 sqlite3OsClose(pPager->jfd);
52538 ** has been successfully called. If a shared-lock is already held when
52539 ** this function is called, it is a no-op.
52546 ** the SHARED lock, the file-system is checked for a hot-journal,
52547 ** which is played back if present. Following any hot-journal
52549 ** the 'change-counter' field of the database file header and
52552 ** 2) If the pager is running in exclusive-mode, and there are currently
52559 ** occurs while locking the database, checking for a hot-journal file or
52565 /* This routine is only called from b-tree and only when there are no
52569 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
52571 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
52572 assert( pPager->errCode==SQLITE_OK );
52574 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
52575 int bHotJournal = 1; /* True if there exists a hot journal-file */
52578 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
52582 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
52589 if( pPager->eLock<=SHARED_LOCK ){
52596 if( pPager->readOnly ){
52606 ** hot-journal back.
52623 ** in exclusive-access mode the file descriptor will be kept open
52624 ** and possibly used for a transaction later on. Also, write-access
52631 ** may mean that the pager was in the error-state when this
52634 if( !isOpen(pPager->jfd) ){
52635 sqlite3_vfs * const pVfs = pPager->pVfs;
52638 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
52642 assert( !pPager->tempFile );
52643 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
52644 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
52647 sqlite3OsClose(pPager->jfd);
52654 ** playing back the hot-journal so that we don't end up with
52660 if( isOpen(pPager->jfd) ){
52664 rc = pager_playback(pPager, !pPager->tempFile);
52665 pPager->eState = PAGER_OPEN;
52667 }else if( !pPager->exclusiveMode ){
52673 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
52692 assert( pPager->eState==PAGER_OPEN );
52693 assert( (pPager->eLock==SHARED_LOCK)
52694 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
52698 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
52699 /* The shared-lock has just been acquired then check to
52703 ** single unnecessary sqlite3OsRead() call at the start-up.
52707 ** a 32-bit counter that is incremented with each change. The
52715 char dbFileVers[sizeof(pPager->dbFileVers)];
52718 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
52726 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
52736 sqlite3OsUnfetch(pPager->fd, 0, 0);
52741 /* If there is a WAL file in the file-system, open this database in WAL
52742 ** mode. Otherwise, the following function call is a no-op.
52746 assert( pPager->pWal==0 || rc==SQLITE_OK );
52755 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
52756 rc = pagerPagecount(pPager, &pPager->dbSize);
52763 assert( pPager->eState==PAGER_OPEN );
52765 pPager->eState = PAGER_READER;
52766 pPager->hasHeldSharedLock = 1;
52777 ** nothing to rollback, so this routine is a no-op.
52780 if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
52793 ** getPageNormal() -- The normal getter
52794 ** getPageError() -- Used if the pager is in an error state
52795 ** getPageMmap() -- Used if memory-mapped I/O is enabled
52817 ** a) When reading a free-list leaf page from the database, and
52835 ** to find a page in the in-memory cache first. If the page is not already
52837 ** just returns 0. This routine acquires a read-lock the first time it
52853 assert( pPager->errCode==SQLITE_OK );
52854 assert( pPager->eState>=PAGER_READER );
52856 assert( pPager->hasHeldSharedLock==1 );
52859 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
52862 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
52869 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
52871 assert( pPg->pgno==pgno );
52872 assert( pPg->pPager==pPager || pPg->pPager==0 );
52875 if( pPg->pPager && !noContent ){
52879 pPager->aStat[PAGER_STAT_HIT]++;
52894 pPg->pPager = pPager;
52896 assert( !isOpen(pPager->fd) || !MEMDB );
52897 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
52898 if( pgno>pPager->mxPgno ){
52903 /* Failure to set the bits in the InJournal bit-vectors is benign.
52910 if( pgno<=pPager->dbOrigSize ){
52911 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
52918 memset(pPg->pData, 0, pPager->pageSize);
52923 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
52926 assert( pPg->pPager==pPager );
52927 pPager->aStat[PAGER_STAT_MISS]++;
52948 /* The page getter for when memory-mapped I/O is enabled */
52959 /* It is acceptable to use a read-only (mmap) page for any page except
52960 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
52962 ** temporary or in-memory database. */
52964 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
52969 assert( pPager->xCodec==0 );
52979 assert( pPager->eState>=PAGER_READER );
52981 assert( pPager->hasHeldSharedLock==1 );
52982 assert( pPager->errCode==SQLITE_OK );
52985 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
52993 rc = sqlite3OsFetch(pPager->fd,
52994 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
52997 if( pPager->eState>PAGER_READER || pPager->tempFile ){
53003 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
53029 assert( pPager->errCode!=SQLITE_OK );
53031 return pPager->errCode;
53043 return pPager->xGet(pPager, pgno, ppPage, flags);
53047 ** Acquire a page if it is already in the in-memory cache. Do
53061 assert( pPager->pPCache!=0 );
53062 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
53063 assert( pPage==0 || pPager->hasHeldSharedLock );
53065 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
53079 pPager = pPg->pPager;
53080 if( pPg->flags & PGHDR_MMAP ){
53097 ** to the start of it. If there are active savepoints, open the sub-journal
53115 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
53117 assert( pPager->eState==PAGER_WRITER_LOCKED );
53119 assert( pPager->pInJournal==0 );
53121 /* If already in the error state, this function is a no-op. But on
53124 if( NEVER(pPager->errCode) ) return pPager->errCode;
53126 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
53127 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
53128 if( pPager->pInJournal==0 ){
53133 if( !isOpen(pPager->jfd) ){
53134 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
53135 sqlite3MemJournalOpen(pPager->jfd);
53140 if( pPager->tempFile ){
53153 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
53157 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
53162 ** the sub-journal if necessary.
53166 pPager->nRec = 0;
53167 pPager->journalOff = 0;
53168 pPager->setMaster = 0;
53169 pPager->journalHdr = 0;
53175 sqlite3BitvecDestroy(pPager->pInJournal);
53176 pPager->pInJournal = 0;
53178 assert( pPager->eState==PAGER_WRITER_LOCKED );
53179 pPager->eState = PAGER_WRITER_CACHEMOD;
53186 ** Begin a write-transaction on the specified pager object. If a
53187 ** write-transaction has already been opened, this function is a no-op.
53194 ** If the subjInMemory argument is non-zero, then any sub-journal opened
53195 ** within this transaction will be opened as an in-memory file. This
53196 ** has no effect if the sub-journal is already opened (as it may be when
53198 ** sub-journal. If the subjInMemory argument is zero, then any required
53199 ** sub-journal is implemented in-memory if pPager is an in-memory database,
53205 if( pPager->errCode ) return pPager->errCode;
53206 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
53207 pPager->subjInMemory = (u8)subjInMemory;
53209 if( ALWAYS(pPager->eState==PAGER_READER) ){
53210 assert( pPager->pInJournal==0 );
53216 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
53221 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
53226 ** The busy-handler is not invoked if another connection already
53227 ** holds the write-lock. If possible, the upper layer will call it.
53229 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
53233 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
53248 ** transactions may copy data from the sub-journal into the database
53252 pPager->eState = PAGER_WRITER_LOCKED;
53253 pPager->dbHintSize = pPager->dbSize;
53254 pPager->dbFileSize = pPager->dbSize;
53255 pPager->dbOrigSize = pPager->dbSize;
53256 pPager->journalOff = 0;
53259 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
53260 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
53272 Pager *pPager = pPg->pPager;
53276 i64 iOff = pPager->journalOff;
53281 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
53283 assert( pPager->journalHdr<=pPager->journalOff );
53284 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
53288 ** page in the block above, set the need-sync flag for the page.
53294 pPg->flags |= PGHDR_NEED_SYNC;
53296 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
53298 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
53300 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
53303 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
53304 pPager->journalOff, pPager->pageSize));
53307 PAGERID(pPager), pPg->pgno,
53308 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
53310 pPager->journalOff += 8 + pPager->pageSize;
53311 pPager->nRec++;
53312 assert( pPager->pInJournal!=0 );
53313 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
53316 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
53323 ** main journal or sub-journal as required. If the page is written into
53329 Pager *pPager = pPg->pPager;
53332 /* This routine is not called unless a write-transaction has already
53336 assert( pPager->eState==PAGER_WRITER_LOCKED
53337 || pPager->eState==PAGER_WRITER_CACHEMOD
53338 || pPager->eState==PAGER_WRITER_DBMOD
53341 assert( pPager->errCode==0 );
53342 assert( pPager->readOnly==0 );
53346 ** obtained the necessary locks to begin the write-transaction, but the
53354 if( pPager->eState==PAGER_WRITER_LOCKED ){
53358 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
53368 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
53369 if( pPager->pInJournal!=0
53370 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
53373 if( pPg->pgno<=pPager->dbOrigSize ){
53379 if( pPager->eState!=PAGER_WRITER_DBMOD ){
53380 pPg->flags |= PGHDR_NEED_SYNC;
53383 PAGERID(pPager), pPg->pgno,
53384 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
53388 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
53393 pPg->flags |= PGHDR_WRITEABLE;
53398 if( pPager->nSavepoint>0 ){
53403 if( pPager->dbSize<pPg->pgno ){
53404 pPager->dbSize = pPg->pgno;
53427 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
53428 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
53435 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
53436 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
53438 /* This trick assumes that both the page-size and sector-size are
53442 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
53444 nPageCount = pPager->dbSize;
53445 if( pPg->pgno>nPageCount ){
53446 nPage = (pPg->pgno - pg1)+1;
53447 }else if( (pg1+nPagePerSector-1)>nPageCount ){
53448 nPage = nPageCount+1-pg1;
53453 assert(pg1<=pPg->pgno);
53454 assert((pg1+nPage)>pPg->pgno);
53459 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
53464 if( pPage->flags&PGHDR_NEED_SYNC ){
53471 if( pPage->flags&PGHDR_NEED_SYNC ){
53489 pPage->flags |= PGHDR_NEED_SYNC;
53495 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
53496 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
53508 ** fit on a single disk sector. In this case all co-resident pages
53515 Pager *pPager = pPg->pPager;
53516 assert( (pPg->flags & PGHDR_MMAP)==0 );
53517 assert( pPager->eState>=PAGER_WRITER_LOCKED );
53519 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
53520 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
53522 }else if( pPager->errCode ){
53523 return pPager->errCode;
53524 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
53525 assert( pPager->tempFile==0 );
53539 return pPg->flags & PGHDR_WRITEABLE;
53557 ** This optimization cannot be used with a temp-file, as the page may
53564 Pager *pPager = pPg->pPager;
53565 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
53566 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
53567 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
53568 pPg->flags |= PGHDR_DONT_WRITE;
53569 pPg->flags &= ~PGHDR_WRITEABLE;
53570 testcase( pPg->flags & PGHDR_NEED_SYNC );
53577 ** change-counter, stored as a 4-byte big-endian integer starting at
53581 ** But this only happens if the pPager->changeCountDone flag is false.
53591 ** The isDirectMode flag may only be non-zero if the library was compiled
53593 ** if isDirect is non-zero, then the database file is updated directly
53600 assert( pPager->eState==PAGER_WRITER_CACHEMOD
53601 || pPager->eState==PAGER_WRITER_DBMOD
53606 ** atomic-write optimization is enabled in this build, then isDirect
53610 ** The idea is that if the atomic-write optimization is not
53623 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
53626 assert( !pPager->tempFile && isOpen(pPager->fd) );
53633 ** operating in direct-mode, make page 1 writable. When not in
53635 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
53648 assert( pPager->dbFileSize>0 );
53649 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
53651 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
53652 pPager->aStat[PAGER_STAT_WRITE]++;
53655 /* Update the pager's copy of the change-counter. Otherwise, the
53657 ** flushed (as the change-counter values will not match). */
53659 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
53660 pPager->changeCountDone = 1;
53663 pPager->changeCountDone = 1;
53674 ** Sync the database file to disk. This is a no-op for in-memory databases
53677 ** If successful, or if called on a pager for which it is a no-op, this
53683 if( isOpen(pPager->fd) ){
53685 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
53688 if( rc==SQLITE_OK && !pPager->noSync ){
53690 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
53696 ** This function may only be called while a write-transaction is active in
53697 ** rollback. If the connection is in WAL mode, this call is a no-op.
53707 int rc = pPager->errCode;
53710 assert( pPager->eState==PAGER_WRITER_CACHEMOD
53711 || pPager->eState==PAGER_WRITER_DBMOD
53712 || pPager->eState==PAGER_WRITER_LOCKED
53730 ** * The database file change-counter is updated,
53731 ** * the journal is synced (unless the atomic-write optimization is used),
53743 ** If the final parameter - noSync - is true, then the database file itself
53755 assert( pPager->eState==PAGER_WRITER_LOCKED
53756 || pPager->eState==PAGER_WRITER_CACHEMOD
53757 || pPager->eState==PAGER_WRITER_DBMOD
53758 || pPager->eState==PAGER_ERROR
53763 if( NEVER(pPager->errCode) ) return pPager->errCode;
53769 pPager->zFilename, zMaster, pPager->dbSize));
53772 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
53774 assert( MEMDB==0 || pPager->tempFile );
53775 assert( isOpen(pPager->fd) || pPager->tempFile );
53777 /* If this is an in-memory db, or no pages have been written to, or this
53778 ** function has already been called, it is mostly a no-op. However, any
53780 sqlite3BackupRestart(pPager->pBackup);
53783 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
53787 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
53790 pList->pDirty = 0;
53794 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
53798 sqlite3PcacheCleanAll(pPager->pPCache);
53801 /* The following block updates the change-counter. Exactly how it
53802 ** does this depends on whether or not the atomic-update optimization
53806 ** * The file-system supports the atomic-write property for
53807 ** blocks of size page-size, and
53808 ** * This commit is not part of a multi-file transaction, and
53813 ** counter in 'indirect-mode'. If the optimization is compiled in but
53816 ** pager_incr_changecounter() to update the change-counter in indirect
53820 ** then call pager_incr_changecounter() to update the change-counter
53826 assert( isOpen(pPager->jfd)
53827 || pPager->journalMode==PAGER_JOURNALMODE_OFF
53828 || pPager->journalMode==PAGER_JOURNALMODE_WAL
53830 if( !zMaster && isOpen(pPager->jfd)
53831 && pPager->journalOff==jrnlBufferSize(pPager)
53832 && pPager->dbSize>=pPager->dbOrigSize
53833 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
53835 /* Update the db file change counter via the direct-write method. The
53836 ** following call will modify the in-memory representation of page 1
53838 ** directly to the database file. Because of the atomic-write
53839 ** property of the host file-system, this is safe.
53843 rc = sqlite3JournalCreate(pPager->jfd);
53855 ** or if zMaster is NULL (no master journal), then this call is a no-op.
53861 ** If the atomic-update optimization is being used, this sync will not
53864 ** Because the change-counter page was just modified, unless the
53865 ** atomic-update optimization is used it is almost certain that the
53869 ** xSync() call will be changed to a no-op by the OS anyhow.
53874 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
53879 sqlite3PcacheCleanAll(pPager->pPCache);
53884 ** last page in the db image moved to the free-list. In this case the
53887 if( pPager->dbSize>pPager->dbFileSize ){
53888 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
53889 assert( pPager->eState==PAGER_WRITER_DBMOD );
53904 pPager->eState = PAGER_WRITER_FINISHED;
53913 ** synced to disk. The journal file still exists in the file-system
53915 ** be used as a hot-journal and the current transaction rolled back.
53919 ** for hot-journal rollback. Once this is done the transaction is
53931 if( NEVER(pPager->errCode) ) return pPager->errCode;
53933 assert( pPager->eState==PAGER_WRITER_LOCKED
53934 || pPager->eState==PAGER_WRITER_FINISHED
53935 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
53940 ** this transaction, the pager is running in exclusive-mode and is
53941 ** using persistent journals, then this function is a no-op.
53945 ** a hot-journal during hot-journal rollback, 0 changes will be made
53950 if( pPager->eState==PAGER_WRITER_LOCKED
53951 && pPager->exclusiveMode
53952 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
53954 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
53955 pPager->eState = PAGER_READER;
53960 pPager->iDataVersion++;
53961 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
53967 ** transaction are reverted and the current write-transaction is closed.
53977 ** in-memory cache pages to the state they were in when the transaction
53986 ** In WAL mode, all cache-entries containing data modified within the
53988 ** their pre-transaction state by re-reading data from the database or
53995 /* PagerRollback() is a no-op if called in READER or OPEN state. If
54000 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
54001 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
54005 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
54006 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
54008 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
54009 int eState = pPager->eState;
54016 pPager->errCode = SQLITE_ABORT;
54017 pPager->eState = PAGER_ERROR;
54025 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
54038 ** Return TRUE if the database file is opened read-only. Return FALSE
54042 return pPager->readOnly;
54050 return sqlite3PcacheRefCount(pPager->pPCache);
54059 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
54060 + 5*sizeof(void*);
54061 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
54063 + pPager->pageSize;
54079 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
54080 a[1] = sqlite3PcachePagecount(pPager->pPCache);
54081 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
54082 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
54083 a[4] = pPager->eState;
54084 a[5] = pPager->errCode;
54085 a[6] = pPager->aStat[PAGER_STAT_HIT];
54086 a[7] = pPager->aStat[PAGER_STAT_MISS];
54087 a[8] = 0; /* Used to be pPager->nOvfl */
54088 a[9] = pPager->nRead;
54089 a[10] = pPager->aStat[PAGER_STAT_WRITE];
54098 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
54112 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
54114 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
54119 ** Return true if this is an in-memory or temp-file backed pager.
54122 return pPager->tempFile;
54129 ** equal to nSavepoint, then this function is a no-op.
54132 ** occurs while opening the sub-journal file, then an IO error code is
54137 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
54141 assert( pPager->eState>=PAGER_WRITER_LOCKED );
54143 assert( nSavepoint>nCurrent && pPager->useJournal );
54150 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
54155 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
54156 pPager->aSavepoint = aNew;
54160 aNew[ii].nOrig = pPager->dbSize;
54161 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
54162 aNew[ii].iOffset = pPager->journalOff;
54166 aNew[ii].iSubRec = pPager->nSubRec;
54167 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
54172 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
54174 pPager->nSavepoint = ii+1;
54176 assert( pPager->nSavepoint==nSavepoint );
54181 assert( pPager->eState>=PAGER_WRITER_LOCKED );
54184 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
54204 ** (the first created). A value of (Pager.nSavepoint-1) means operate
54206 ** (Pager.nSavepoint-1), then this function is a no-op.
54223 int rc = pPager->errCode;
54232 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
54241 for(ii=nNew; ii<pPager->nSavepoint; ii++){
54242 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
54244 pPager->nSavepoint = nNew;
54247 ** the sub-journal to zero bytes in size. */
54249 if( nNew==0 && isOpen(pPager->sjfd) ){
54250 /* Only truncate if it is an in-memory sub-journal. */
54251 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
54252 rc = sqlite3OsTruncate(pPager->sjfd, 0);
54255 pPager->nSubRec = 0;
54259 ** If this is a temp-file, it is possible that the journal file has
54263 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
54264 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
54275 pPager->journalMode==PAGER_JOURNALMODE_OFF
54276 && pPager->eState>=PAGER_WRITER_CACHEMOD
54278 pPager->errCode = SQLITE_ABORT;
54279 pPager->eState = PAGER_ERROR;
54291 ** Except, if the pager is in-memory only, then return an empty string if
54295 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
54296 ** participate in shared-cache.
54299 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
54306 return pPager->pVfs;
54315 return pPager->fd;
54324 return pPager->jfd;
54326 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
54334 return pPager->zJournal;
54348 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
54349 pPager->xCodec = pPager->memDb ? 0 : xCodec;
54350 pPager->xCodecSizeChng = xCodecSizeChng;
54351 pPager->xCodecFree = xCodecFree;
54352 pPager->pCodec = pCodec;
54357 return pPager->pCodec;
54369 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
54377 return pPager->eState;
54391 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
54399 ** If the fourth argument, isCommit, is non-zero, then this page is being
54409 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
54413 assert( pPg->nRef>0 );
54414 assert( pPager->eState==PAGER_WRITER_CACHEMOD
54415 || pPager->eState==PAGER_WRITER_DBMOD
54419 /* In order to be able to rollback, an in-memory database must journal
54422 assert( pPager->tempFile || !MEMDB );
54423 if( pPager->tempFile ){
54430 ** sub-journal now. This is required to handle the following scenario:
54438 ** If page X were not written to the sub-journal here, it would not
54442 ** subjournalPage() may need to allocate space to store pPg->pgno into
54446 if( (pPg->flags & PGHDR_DIRTY)!=0
54453 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
54454 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
54456 /* If the journal needs to be sync()ed before page pPg->pgno can
54457 ** be written to, store pPg->pgno in local variable needSyncPgno.
54460 ** the journal needs to be sync()ed before database page pPg->pgno
54463 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
54464 needSyncPgno = pPg->pgno;
54465 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
54466 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
54467 assert( pPg->flags&PGHDR_DIRTY );
54470 /* If the cache contains a page with page-number pgno, remove it
54475 pPg->flags &= ~PGHDR_NEED_SYNC;
54477 assert( !pPgOld || pPgOld->nRef==1 );
54479 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
54480 if( pPager->tempFile ){
54481 /* Do not discard pages from an in-memory database since we might
54483 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
54489 origPgno = pPg->pgno;
54493 /* For an in-memory database, make sure the original page continues
54497 if( pPager->tempFile && pPgOld ){
54503 /* If needSyncPgno is non-zero, then the journal file needs to be
54505 ** Currently, no such page exists in the page-cache and the
54507 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
54510 ** If the attempt to load the page into the page-cache fails, (due
54520 if( needSyncPgno<=pPager->dbOrigSize ){
54521 assert( pPager->pTmpSpace!=0 );
54522 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
54526 pPgHdr->flags |= PGHDR_NEED_SYNC;
54542 assert( pPg->pgno!=iNew );
54543 pPg->flags = flags;
54551 assert( pPg->nRef>0 || pPg->pPager->memDb );
54552 return pPg->pData;
54560 return pPg->pExtra;
54564 ** Get/set the locking-mode for this pager. Parameter eMode must be one
54567 ** the locking-mode is set to the value specified.
54571 ** locking-mode.
54579 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
54580 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
54581 pPager->exclusiveMode = (u8)eMode;
54583 return (int)pPager->exclusiveMode;
54587 ** Set the journal-mode for this pager. Parameter eMode must be one of:
54599 ** * An in-memory database can only have its journal_mode set to _OFF
54604 ** The returned indicate the current (possibly updated) journal-mode.
54607 u8 eOld = pPager->journalMode; /* Prior journalmode */
54628 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
54630 /* Do allow the journalmode of an in-memory database to be set to
54643 assert( pPager->eState!=PAGER_ERROR );
54644 pPager->journalMode = (u8)eMode;
54650 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
54651 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
54652 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
54653 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
54654 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
54655 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
54657 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
54658 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
54668 sqlite3OsClose(pPager->jfd);
54669 if( pPager->eLock>=RESERVED_LOCK ){
54670 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
54673 int state = pPager->eState;
54678 if( pPager->eState==PAGER_READER ){
54683 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
54690 assert( state==pPager->eState );
54693 sqlite3OsClose(pPager->jfd);
54698 return (int)pPager->journalMode;
54705 return (int)pPager->journalMode;
54715 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
54716 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
54721 ** Get/set the size-limit used for persistent journal files.
54723 ** Setting the size limit to -1 means no limit is enforced.
54724 ** An attempt to set a limit smaller than -1 is a no-op.
54727 if( iLimit>=-1 ){
54728 pPager->journalSizeLimit = iLimit;
54729 sqlite3WalLimit(pPager->pWal, iLimit);
54731 return pPager->journalSizeLimit;
54735 ** Return a pointer to the pPager->pBackup variable. The backup module
54741 return &pPager->pBackup;
54746 ** Unless this is an in-memory or temporary database, clear the pager cache.
54749 assert( MEMDB==0 || pPager->tempFile );
54750 if( pPager->tempFile==0 ) pager_reset(pPager);
54771 if( pPager->pWal ){
54772 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
54773 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
54774 pPager->pBusyHandlerArg,
54775 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
54783 return sqlite3WalCallback(pPager->pWal);
54788 ** primitives necessary for write-ahead logging.
54791 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
54792 if( pPager->noLock ) return 0;
54793 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
54803 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
54816 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
54817 ** lock on the database file and use heap-memory to store the wal-index
54818 ** in. Otherwise, use the normal shared-memory.
54823 assert( pPager->pWal==0 && pPager->tempFile==0 );
54824 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
54826 /* If the pager is already in exclusive-mode, the WAL module will use
54827 ** heap-memory for the wal-index instead of the VFS shared-memory
54831 if( pPager->exclusiveMode ){
54839 rc = sqlite3WalOpen(pPager->pVfs,
54840 pPager->fd, pPager->zWal, pPager->exclusiveMode,
54841 pPager->journalSizeLimit, &pPager->pWal
54855 ** file (not a temp file or an in-memory database), and the WAL file
54861 ** If the pager is open on a temp-file (or in-memory database), or if
54867 int *pbOpen /* OUT: Set to true if call is a no-op */
54872 assert( pPager->eState==PAGER_OPEN || pbOpen );
54873 assert( pPager->eState==PAGER_READER || !pbOpen );
54875 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
54877 if( !pPager->tempFile && !pPager->pWal ){
54881 sqlite3OsClose(pPager->jfd);
54885 pPager->journalMode = PAGER_JOURNALMODE_WAL;
54886 pPager->eState = PAGER_OPEN;
54907 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
54909 /* If the log file is not already open, but does exist in the file-system,
54913 if( !pPager->pWal ){
54918 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
54927 ** the database file, the log and log-summary files will be deleted.
54929 if( rc==SQLITE_OK && pPager->pWal ){
54932 rc = sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags,
54933 pPager->pageSize, (u8*)pPager->pTmpSpace);
54934 pPager->pWal = 0;
54936 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
54949 if( pPager->pWal ){
54950 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
54962 if( pPager->pWal ){
54963 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
54976 if( pPager->pWal ){
54977 rc = sqlite3WalSnapshotRecover(pPager->pWal);
54988 ** A read-lock must be held on the pager when this function is called. If
54995 assert( pPager->eState>=PAGER_READER );
54996 return sqlite3WalFramesize(pPager->pWal);
55016 ** This file contains the implementation of a write-ahead log (WAL) used in
55019 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
55038 ** big-endian 32-bit unsigned integer values:
55044 ** 16: Salt-1, random integer incremented with each checkpoint
55045 ** 20: Salt-2, a different random integer changing with each ckpt
55046 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
55047 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
55049 ** Immediately following the wal-header are zero or more frames. Each
55050 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
55051 ** of page data. The frame-header is six big-endian 32-bit unsigned
55057 ** 8: Salt-1 (copied from the header)
55058 ** 12: Salt-2 (copied from the header)
55059 ** 16: Checksum-1.
55060 ** 20: Checksum-2.
55065 ** (1) The salt-1 and salt-2 values in the frame-header match
55066 ** salt values in the wal-header
55068 ** (2) The checksum values in the final 8 bytes of the frame-header
55073 ** The checksum is computed using 32-bit big-endian integers if the
55075 ** is computed using little-endian if the magic number is 0x377f0682.
55077 ** big-endian format regardless of which byte order is used to compute
55079 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
55082 ** for i from 0 to n-1 step 2:
55089 ** of the sequence being summed.) The s1 value spans all 32-bit
55092 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
55093 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
55094 ** The VFS.xSync operations serve as write barriers - all writes launched
55098 ** After each checkpoint, the salt-1 value is incremented and the salt-2
55127 ** data structure called the wal-index is maintained to expedite the
55130 ** WAL-INDEX FORMAT
55132 ** Conceptually, the wal-index is shared memory, though VFS implementations
55133 ** might choose to implement the wal-index using a mmapped file. Because
55134 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
55138 ** The wal-index is transient. After a crash, the wal-index can (and should
55140 ** to either truncate or zero the header of the wal-index when the last
55141 ** connection to it closes. Because the wal-index is transient, it can
55142 ** use an architecture-specific format; it does not have to be cross-platform.
55144 ** as big endian, the wal-index can store multi-byte values in the native
55147 ** The purpose of the wal-index is to answer this question quickly: Given
55152 ** The wal-index consists of a header region, followed by an one or
55155 ** The wal-index header contains the total number of frames within the WAL
55161 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
55163 ** wal-index.
55165 ** Each index block contains two sections, a page-mapping that contains the
55166 ** database page number associated with each wal frame, and a hash-table
55168 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
55169 ** for the first index block) 32-bit page numbers. The first entry in the
55170 ** first index-block contains the database page number corresponding to the
55175 ** The last index block in a wal-index usually contains less than the full
55176 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
55178 ** allocated size of the page-mapping array - the page-mapping array merely
55182 ** can be found by scanning the page-mapping sections of each index block
55188 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
55193 ** 1-based index of an entry in the mapping section of the same
55194 ** index block. Let K be the 1-based index of the largest entry in
55209 ** wrap-around.) Because the hash table is never more than half full,
55214 ** current index block. Otherwise the iMax-th mapping entry of the
55232 ** and to the wal-index) might be using a different value K1, where K1>K0.
55238 ** in the first place - which is what reader one wants. Meanwhile, the
55261 ** The maximum (and only) versions of the wal and wal-index formats
55265 ** values in the wal-header are correct and (b) the version field is not
55268 ** Similarly, if a client successfully reads a wal-index header (i.e. the
55270 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
55279 ** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
55286 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
55296 ** The following object holds a copy of the wal-index header content.
55298 ** The actual header in the wal-index consists of two copies of this
55305 ** Or it can be 1 to represent a 65536-byte page. The latter case was
55309 u32 iVersion; /* Wal-index version */
55313 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
55323 ** A copy of the following object occurs in the wal-index immediately
55346 ** holds read-lock K, then the value in aReadMark[K] is no greater than
55349 ** a special case; its value is never used and it exists as a place-holder
55378 ** We assume that 32-bit loads are atomic and so no locks are needed in
55393 ** only support mandatory file-locks, we do not read or write data
55407 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
55408 ** big-endian format in the first 4 bytes of a WAL file.
55411 ** file are calculated by treating all data as an array of 32-bit
55412 ** big-endian words. Otherwise, they are calculated by interpreting
55413 ** all data as 32-bit little-endian words.
55418 ** Return the offset of frame iFrame in the write-ahead log file,
55420 ** is to the start of the write-ahead log frame-header.
55423 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
55427 ** An open write-ahead log file is represented by an instance of the
55438 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
55440 i16 readLock; /* Which read lock is being held. -1 for none */
55442 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
55449 WalIndexHdr hdr; /* Wal-index header for current transaction */
55453 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
55477 ** Each page of the wal-index mapping contains a hash-table made up of
55491 ** walIteratorInit() - Create a new iterator,
55492 ** walIteratorNext() - Step an iterator,
55493 ** walIteratorFree() - Free an iterator.
55506 } aSegment[1]; /* One for every 32KB page in the wal-index */
55510 ** Define the parameters of the hash tables in the wal-index file. There
55511 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
55512 ** wal-index.
55514 ** Changing any of these constants will alter the wal-index format and
55522 ** The block of page numbers associated with the first hash-table in a
55523 ** wal-index is smaller than usual. This is so that there is a complete
55524 ** hash-table on each aligned 32KB page of the wal-index.
55526 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
55528 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
55534 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
55535 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
55538 ** If this call is successful, *ppPage is set to point to the wal-index
55545 /* Enlarge the pWal->apWiData[] array if required */
55546 if( pWal->nWiData<=iPage ){
55549 apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
55554 memset((void*)&apNew[pWal->nWiData], 0,
55555 sizeof(u32*)*(iPage+1-pWal->nWiData));
55556 pWal->apWiData = apNew;
55557 pWal->nWiData = iPage+1;
55561 if( pWal->apWiData[iPage]==0 ){
55562 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
55563 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
55564 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
55566 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
55567 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
55570 pWal->readOnly |= WAL_SHM_RDONLY;
55576 *ppPage = pWal->apWiData[iPage];
55582 ** Return a pointer to the WalCkptInfo structure in the wal-index.
55585 assert( pWal->nWiData>0 && pWal->apWiData[0] );
55586 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
55590 ** Return a pointer to the WalIndexHdr structure in the wal-index.
55593 assert( pWal->nWiData>0 && pWal->apWiData[0] );
55594 return (volatile WalIndexHdr*)pWal->apWiData[0];
55598 ** The argument to this macro must be of type u32. On a little-endian
55600 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
55602 ** of the input value as a little-endian integer.
55619 int nativeCksum, /* True for native byte-order, false for non-native */
55657 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
55658 sqlite3OsShmBarrier(pWal->pDbFd);
55663 ** Write the header information in pWal->hdr into the wal-index.
55665 ** The checksum on pWal->hdr is updated before it is written.
55671 assert( pWal->writeLock );
55672 pWal->hdr.isInit = 1;
55673 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
55674 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
55675 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
55677 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
55682 ** supplied by the caller. A frame-header is made up of a series of
55683 ** 4-byte big-endian integers, as follows:
55688 ** 8: Salt-1 (copied from the wal-header)
55689 ** 12: Salt-2 (copied from the wal-header)
55690 ** 16: Checksum-1.
55691 ** 20: Checksum-2.
55694 Wal *pWal, /* The write-ahead log */
55696 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
55700 int nativeCksum; /* True for native byte-order checksums */
55701 u32 *aCksum = pWal->hdr.aFrameCksum;
55705 if( pWal->iReCksum==0 ){
55706 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
55708 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
55710 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
55725 Wal *pWal, /* The write-ahead log */
55731 int nativeCksum; /* True for native byte-order checksums */
55732 u32 *aCksum = pWal->hdr.aFrameCksum;
55736 /* A frame is only valid if the salt values in the frame-header
55737 ** match the salt values in the wal-header.
55739 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
55751 ** all prior frams, the first 16 bytes of this frame-header,
55752 ** and the frame-data matches the checksum in the last 8
55753 ** bytes of this frame-header.
55755 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
55757 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
55781 return "WRITE-LOCK";
55783 return "CKPT-LOCK";
55785 return "RECOVER-LOCK";
55788 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
55789 lockIdx-WAL_READ_LOCK(0));
55798 ** A lock cannot be moved directly between shared and exclusive - it must go
55801 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
55805 if( pWal->exclusiveMode ) return SQLITE_OK;
55806 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
55808 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
55810 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
55814 if( pWal->exclusiveMode ) return;
55815 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
55817 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
55821 if( pWal->exclusiveMode ) return SQLITE_OK;
55822 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
55824 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
55826 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
55830 if( pWal->exclusiveMode ) return;
55831 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
55833 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
55839 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
55844 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
55845 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
55848 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
55853 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
55857 ** in the wal-index file. Set *piZero to one less than the frame
55887 iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
55890 *paPgno = &aPgno[-1];
55898 ** Return the number of the wal-index page that contains the hash-table
55899 ** and page-number array that contain entries corresponding to WAL frame
55900 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
55904 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
55920 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
55922 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
55927 ** than pWal->hdr.mxFrame.
55929 ** This function is called whenever pWal->hdr.mxFrame is decreased due
55932 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
55934 ** pWal->hdr.mxFrame advances to the point where those hash tables are
55945 assert( pWal->writeLock );
55946 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
55947 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
55948 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
55950 if( pWal->hdr.mxFrame==0 ) return;
55952 /* Obtain pointers to the hash-table and page-number array containing
55953 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
55954 ** that the page said hash-table and array reside on is already mapped.
55956 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
55957 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
55958 walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
55960 /* Zero all hash-table entries that correspond to frame numbers greater
55961 ** than pWal->hdr.mxFrame.
55963 iLimit = pWal->hdr.mxFrame - iZero;
55972 ** frame numbers greater than pWal->hdr.mxFrame.
55974 nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
55996 ** Set an entry in the wal-index that will map database page number
56007 /* Assuming the wal-index file was successfully mapped, populate the
56012 int idx; /* Value to write to hash-table slot */
56015 idx = iFrame - iZero;
56018 /* If this is the first entry to be added to this hash-table, zero the
56022 int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
56030 ** the hash-table before writing any new entries.
56037 /* Write the aPgno[] array entry and the hash-table slot. */
56040 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
56058 ** thing to check, so only do this occasionally - not on every
56079 ** Recover the wal-index by reading the write-ahead log file.
56082 ** wal-index to prevent other threads/processes from doing anything
56083 ** with the WAL or wal-index while recovery is running. The
56101 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
56104 assert( pWal->writeLock );
56105 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
56106 nLock = SQLITE_SHM_NLOCK - iLock;
56113 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
56115 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
56133 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
56146 || szPage&(szPage-1)
56152 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
56153 pWal->szPage = szPage;
56154 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
56155 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
56158 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
56159 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
56161 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
56162 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
56192 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
56199 /* If nTruncate is non-zero, this is a commit record. */
56201 pWal->hdr.mxFrame = iFrame;
56202 pWal->hdr.nPage = nTruncate;
56203 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
56206 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
56207 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
56218 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
56219 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
56222 /* Reset the checkpoint-header. This is safe because this thread is
56227 pInfo->nBackfill = 0;
56228 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
56229 pInfo->aReadMark[0] = 0;
56230 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
56231 if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
56238 if( pWal->hdr.nPage ){
56241 pWal->hdr.mxFrame, pWal->zWalName
56253 ** Close an open wal-index.
56256 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
56258 for(i=0; i<pWal->nWiData; i++){
56259 sqlite3_free((void *)pWal->apWiData[i]);
56260 pWal->apWiData[i] = 0;
56263 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
56274 ** client from unlinking the WAL or wal-index file. If another process
56283 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
56286 int bNoShm, /* True to run in heap-memory mode */
56315 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
56320 pRet->pVfs = pVfs;
56321 pRet->pWalFd = (sqlite3_file *)&pRet[1];
56322 pRet->pDbFd = pDbFd;
56323 pRet->readLock = -1;
56324 pRet->mxWalSize = mxWalSize;
56325 pRet->zWalName = zWalName;
56326 pRet->syncHeader = 1;
56327 pRet->padToSectorBoundary = 1;
56328 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
56330 /* Open file handle on the write-ahead log file. */
56332 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
56334 pRet->readOnly = WAL_RDONLY;
56339 sqlite3OsClose(pRet->pWalFd);
56343 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
56345 pRet->padToSectorBoundary = 0;
56357 if( pWal ) pWal->mxWalSize = iLimit;
56379 iMin = p->iPrior;
56381 for(i=p->nSegment-1; i>=0; i--){
56382 struct WalSegment *pSegment = &p->aSegment[i];
56383 while( pSegment->iNext<pSegment->nEntry ){
56384 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
56388 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
56392 pSegment->iNext++;
56396 *piPage = p->iPrior = iRet;
56424 const u32 *aContent, /* Pages in wal - keys for the sort */
56488 ht_slot *aList; /* Pointer to sub-list content */
56496 struct Sublist aSub[13]; /* Array of sub-lists */
56500 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
56509 assert( p->aList && p->nList<=(1<<iSub) );
56510 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
56511 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
56522 assert( p->nList<=(1<<iSub) );
56523 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
56524 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
56534 assert( aContent[aList[i]] > aContent[aList[i-1]] );
56565 ht_slot *aTmp; /* Temp space used by merge-sort */
56571 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
56572 iLast = pWal->hdr.mxFrame;
56577 + (nSegment-1)*sizeof(struct WalSegment)
56584 p->nSegment = nSegment;
56586 /* Allocate temporary space used by the merge-sort routine. This block
56609 nEntry = (int)(iLast - iZero);
56611 nEntry = (int)((u32*)aHash - (u32*)aPgno);
56613 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
56620 p->aSegment[i].iZero = iZero;
56621 p->aSegment[i].nEntry = nEntry;
56622 p->aSegment[i].aIndex = aIndex;
56623 p->aSegment[i].aPgno = (u32 *)aPgno;
56638 ** busy-handler function. Invoke it and retry the lock until either the
56639 ** lock is successfully obtained or the busy-handler returns 0.
56656 ** The cache of the wal-index header must be valid to call this function.
56657 ** Return the page-size in bytes used by the database.
56660 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
56669 ** file - there are no readers that may attempt to read a frame from
56672 ** This function updates the shared-memory structures so that the next
56677 ** new wal-index header. It should be passed a pseudo-random value (i.e.
56683 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
56684 pWal->nCkpt++;
56685 pWal->hdr.mxFrame = 0;
56687 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
56689 pInfo->nBackfill = 0;
56690 pInfo->nBackfillAttempted = 0;
56691 pInfo->aReadMark[1] = 0;
56692 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
56693 assert( pInfo->aReadMark[0]==0 );
56705 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
56711 ** in the WAL and can be recovered following a power-loss or hard reset.
56718 ** This routine uses and updates the nBackfill field of the wal-index header.
56737 int szPage; /* Database page-size */
56750 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
56759 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
56768 mxSafeFrame = pWal->hdr.mxFrame;
56769 mxPage = pWal->hdr.nPage;
56771 /* Thread-sanitizer reports that the following is an unsafe read,
56779 u32 y = pInfo->aReadMark[i];
56781 assert( y<=pWal->hdr.mxFrame );
56784 pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
56795 if( pInfo->nBackfill<mxSafeFrame
56799 u32 nBackfill = pInfo->nBackfill;
56801 pInfo->nBackfillAttempted = mxSafeFrame;
56805 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
56813 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
56815 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
56824 if( db->u1.isInterrupted ){
56825 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
56833 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
56835 iOffset = (iDbpage-1)*(i64)szPage;
56837 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
56843 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
56844 i64 szDb = pWal->hdr.nPage*(i64)szPage;
56846 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
56848 rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
56852 pInfo->nBackfill = mxSafeFrame;
56873 assert( pWal->writeLock );
56874 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
56879 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
56880 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
56883 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
56889 ** wal-index header in shared memory, as all subsequent reader or
56893 ** the wal-index header do not match the contents of the
56894 ** file-system. To avoid this, update the wal-index header to
56897 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
56899 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
56917 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
56919 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
56923 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
56939 int isDelete = 0; /* True to unlink wal and wal-index files */
56942 ** ordinary, rollback-mode locking methods, this guarantees that the
56945 ** the wal and wal-index files.
56950 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
56952 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
56953 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
56959 int bPersist = -1;
56961 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
56965 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
56968 }else if( pWal->mxWalSize>=0 ){
56972 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
56981 sqlite3OsClose(pWal->pWalFd);
56984 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
56988 sqlite3_free((void *)pWal->apWiData);
56995 ** Try to read the wal-index header. Return 0 on success and 1 if
56998 ** The wal-index is in shared memory. Another thread or process might
57005 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
57008 ** If the checksum cannot be verified return non-zero. If the header
57016 /* The first page of the wal-index must be mapped at this point. */
57017 assert( pWal->nWiData>0 && pWal->apWiData[0] );
57022 ** from the file. If this happens, return non-zero.
57024 ** There are two copies of the header at the beginning of the wal-index.
57038 return 1; /* Malformed header - probably all zeros */
57040 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
57045 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
57047 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
57048 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
57049 testcase( pWal->szPage<=32768 );
57050 testcase( pWal->szPage>=65536 );
57058 ** Read the wal-index header from the wal-index and into pWal->hdr.
57059 ** If the wal-header appears to be corrupt, try to reconstruct the
57060 ** wal-index from the WAL before returning.
57062 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
57063 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
57066 ** If the wal-index header is successfully read, return SQLITE_OK.
57072 volatile u32 *page0; /* Chunk of wal-index containing header */
57074 /* Ensure that page 0 of the wal-index (the page that contains the
57075 ** wal-index header) is mapped. Return early if an error occurs here.
57082 assert( page0 || pWal->writeLock==0 );
57084 /* If the first page of the wal-index has been mapped, try to read the
57085 ** wal-index header immediately, without holding any lock. This usually
57086 ** works, but may fail if the wal-index header is corrupt or currently
57094 assert( badHdr==0 || pWal->writeLock==0 );
57096 if( pWal->readOnly & WAL_SHM_RDONLY ){
57102 pWal->writeLock = 1;
57106 /* If the wal-index header is still malformed even while holding
57114 pWal->writeLock = 0;
57120 ** sure the wal-index was not constructed with some future format that
57123 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
57134 #define WAL_RETRY (-1)
57148 ** to make a copy of the wal-index header into pWal->hdr. If the
57149 ** wal-index header has changed, *pChanged is set to 1 (as an indication
57151 ** flushed.) When useWal==1, the wal-index header is assumed to already
57162 ** bad luck when there is lots of contention for the wal-index, but that
57166 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
57167 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
57170 ** including frame number aReadMark[pWal->readLock]. The reader will
57171 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
57172 ** Or if pWal->readLock==0, then the reader will ignore the WAL
57175 ** this routine will always set pWal->readLock>0 on success.
57177 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
57187 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
57194 assert( pWal->readLock<0 ); /* Not currently locked */
57202 ** paged out or take a page-fault that is time-consuming to resolve,
57206 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
57213 if( cnt>5 ){
57216 VVA_ONLY( pWal->lockError = 1; )
57219 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
57220 sqlite3OsSleep(pWal->pVfs, nDelay);
57234 if( pWal->apWiData[0]==0 ){
57239 ** code that determines whether or not the shared-memory region
57256 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
57258 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0
57259 || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr)))
57268 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
57285 pWal->readLock = 0;
57295 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
57299 mxFrame = pWal->hdr.mxFrame;
57301 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
57302 mxFrame = pWal->pSnapshot->mxFrame;
57306 u32 thisMark = pInfo->aReadMark[i];
57313 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
57319 mxReadMark = pInfo->aReadMark[i] = mxFrame;
57329 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
57337 /* Now that the read-lock has been obtained, check that neither the
57338 ** value in the aReadMark[] array or the contents of the wal-index
57341 ** It is necessary to check that the wal-index header did not change
57342 ** between the time it was read and when the shared-lock was obtained
57345 ** that occur later in the log than pWal->hdr.mxFrame may have been
57348 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
57351 ** Before checking that the live wal-index header has not changed
57354 ** to read any frames earlier than minFrame from the wal file - they
57358 ** nBackfill and checking that the wal-header in shared-memory still
57359 ** matches the one cached in pWal->hdr, it is guaranteed that the
57360 ** checkpointer that set nBackfill was not working with a wal-index
57361 ** header newer than that cached in pWal->hdr. If it were, that could
57363 ** a version of page X that lies before pWal->minFrame (call that version
57366 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
57369 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
57371 pWal->minFrame = pInfo->nBackfill+1;
57373 if( pInfo->aReadMark[mxI]!=mxReadMark
57374 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
57379 assert( mxReadMark<=pWal->hdr.mxFrame );
57380 pWal->readLock = (i16)mxI;
57394 ** This is only really safe if the file-system is such that any page
57408 assert( pWal->readLock>=0 );
57412 int szPage = (int)pWal->szPage;
57415 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
57422 u32 i = pInfo->nBackfillAttempted;
57423 for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
57433 pgno = aPgno[i-iZero];
57434 iDbOff = (i64)(pgno-1) * szPage;
57438 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
57441 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
57449 pInfo->nBackfillAttempted = i-1;
57467 ** it takes a snapshot of the state of the WAL and wal-index for the current
57469 ** Other threads might append new content to the WAL and wal-index but
57483 WalIndexHdr *pSnapshot = pWal->pSnapshot;
57484 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
57499 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
57501 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
57502 ** is populated with the wal-index header corresponding to the head
57510 ** pSnapshot->mxFrame into the database file. Note that the
57515 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
57516 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
57522 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
57525 ** before checking pInfo->nBackfillAttempted.
57535 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
57536 ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr
57539 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
57540 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
57542 assert( pWal->readLock>0 );
57543 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
57565 ** read-lock.
57569 if( pWal->readLock>=0 ){
57570 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
57571 pWal->readLock = -1;
57589 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
57594 assert( pWal->readLock>=0 || pWal->lockError );
57596 /* If the "last page" field of the wal-index header snapshot is 0, then
57598 ** in this case as an optimization. Likewise, if pWal->readLock==0,
57602 if( iLast==0 || pWal->readLock==0 ){
57612 ** that adds entries to the wal-index (and possibly to this hash
57616 ** read transaction was opened may have been written incorrectly -
57623 ** if we had exclusive access to the hash-table:
57626 ** This condition filters out normal hash-table collisions.
57630 ** table after the current read-transaction had started.
57632 iMinHash = walFramePage(pWal->minFrame);
57633 for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
57648 if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){
57652 if( (nCollide--)==0 ){
57660 ** of the wal-index file content. Make sure the results agree with the
57665 assert( pWal->minFrame>0 );
57666 for(iTest=iLast; iTest>=pWal->minFrame; iTest--){
57693 sz = pWal->hdr.szPage;
57699 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
57706 if( pWal && ALWAYS(pWal->readLock>=0) ){
57707 return pWal->hdr.nPage;
57731 assert( pWal->readLock>=0 );
57732 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
57734 if( pWal->readOnly ){
57745 pWal->writeLock = 1;
57751 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
57753 pWal->writeLock = 0;
57765 if( pWal->writeLock ){
57767 pWal->writeLock = 0;
57768 pWal->iReCksum = 0;
57769 pWal->truncateOnCommit = 0;
57776 ** function moves the write-pointer back to the start of the transaction.
57788 if( ALWAYS(pWal->writeLock) ){
57789 Pgno iMax = pWal->hdr.mxFrame;
57792 /* Restore the clients cache of the wal-index header to the state it
57795 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
57797 for(iFrame=pWal->hdr.mxFrame+1;
57803 ** (b) has an outstanding reference, then xUndo is either a no-op
57815 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
57827 assert( pWal->writeLock );
57828 aWalData[0] = pWal->hdr.mxFrame;
57829 aWalData[1] = pWal->hdr.aFrameCksum[0];
57830 aWalData[2] = pWal->hdr.aFrameCksum[1];
57831 aWalData[3] = pWal->nCkpt;
57843 assert( pWal->writeLock );
57844 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
57846 if( aWalData[3]!=pWal->nCkpt ){
57847 /* This savepoint was opened immediately after the write-transaction
57852 aWalData[3] = pWal->nCkpt;
57855 if( aWalData[0]<pWal->hdr.mxFrame ){
57856 pWal->hdr.mxFrame = aWalData[0];
57857 pWal->hdr.aFrameCksum[0] = aWalData[1];
57858 pWal->hdr.aFrameCksum[1] = aWalData[2];
57870 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
57874 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
57881 if( pWal->readLock==0 ){
57883 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
57884 if( pInfo->nBackfill>0 ){
57887 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
57892 ** wal-index header to reflect this.
57895 ** at this point. But updating the actual wal-index header is also
57899 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
57905 pWal->readLock = -1;
57921 ** the next fsync should occur - passed from sqlite3WalFrames() into
57934 ** Do a sync when crossing the p->iSyncPoint boundary.
57947 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
57948 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
57949 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
57952 iAmt -= iFirstAmt;
57954 assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
57955 rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
57958 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
57973 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
57977 pData = pPage->pData;
57979 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
57983 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
57996 const int szPage = pWal->szPage;/* Database page size */
57999 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
58009 ** wal-file header. Otherwise, read them from the frame header of the
58011 assert( pWal->iReCksum>0 );
58012 if( pWal->iReCksum==1 ){
58015 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
58017 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
58018 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
58019 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
58021 iRead = pWal->iReCksum;
58022 pWal->iReCksum = 0;
58025 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
58032 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
58041 ** Write a set of frames to the log. The caller must hold the write-lock
58046 int szPage, /* Database page-size in bytes */
58064 assert( pWal->writeLock );
58071 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
58073 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
58078 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
58079 iFirst = pLive->mxFrame+1;
58083 ** log file, instead of appending to it at pWal->hdr.mxFrame.
58093 iFrame = pWal->hdr.mxFrame;
58095 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
58096 u32 aCksum[2]; /* Checksum for wal-header */
58101 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
58102 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
58103 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
58104 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
58108 pWal->szPage = szPage;
58109 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
58110 pWal->hdr.aFrameCksum[0] = aCksum[0];
58111 pWal->hdr.aFrameCksum[1] = aCksum[1];
58112 pWal->truncateOnCommit = 1;
58114 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
58115 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
58122 ** an out-of-order write following a WAL restart could result in
58127 if( pWal->syncHeader && sync_flags ){
58128 rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
58132 assert( (int)pWal->szPage==szPage );
58136 w.pFd = pWal->pWalFd;
58144 for(p=pList; p; p=p->pDirty){
58149 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
58151 if( iFirst && (p->pDirty || isCommit==0) ){
58153 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
58158 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
58159 pWal->iReCksum = iWrite;
58164 pData = p->pData;
58166 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
58168 p->flags &= ~PGHDR_WAL_APPEND;
58175 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
58180 p->flags |= PGHDR_WAL_APPEND;
58184 if( isCommit && pWal->iReCksum ){
58205 if( pWal->padToSectorBoundary ){
58206 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
58207 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
58227 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
58228 i64 sz = pWal->mxWalSize;
58229 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
58233 pWal->truncateOnCommit = 0;
58236 /* Append data to the wal-index. It is not necessary to lock the
58237 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
58241 iFrame = pWal->hdr.mxFrame;
58242 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
58243 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
58245 rc = walIndexAppend(pWal, iFrame, p->pgno);
58249 nExtra--;
58250 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
58255 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
58258 pWal->hdr.mxFrame = iFrame;
58260 pWal->hdr.iChange++;
58261 pWal->hdr.nPage = nTruncate;
58263 /* If this is a commit, update the wal-index header too. */
58266 pWal->iCallback = iFrame;
58281 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
58297 int isChanged = 0; /* True if a new wal-index header is loaded */
58301 assert( pWal->ckptLock==0 );
58302 assert( pWal->writeLock==0 );
58304 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
58308 if( pWal->readOnly ) return SQLITE_READONLY;
58311 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
58315 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
58318 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
58325 pWal->ckptLock = 1;
58327 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
58331 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
58332 ** immediately, and a busy-handler is configured, it is invoked and the
58333 ** writer lock retried until either the busy-handler returns 0 or the
58339 pWal->writeLock = 1;
58347 /* Read the wal-index header. */
58350 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
58351 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
58358 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
58366 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
58367 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
58372 /* If a new wal-index header was loaded before the checkpoint was
58373 ** performed, then the pager-cache associated with pWal is now
58374 ** out of date. So zero the cached wal-index header to ensure that
58378 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
58384 pWal->ckptLock = 0;
58397 ret = pWal->iCallback;
58398 pWal->iCallback = 0;
58409 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
58411 ** transition out of exclusive-mode is successful, return 1. This
58416 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
58418 ** WAL is already in exclusive-locking mode - meaning that this
58419 ** routine is a no-op. The pager must already hold the exclusive lock
58422 ** If op is negative, then do a dry-run of the op==1 case but do
58429 assert( pWal->writeLock==0 );
58430 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
58432 /* pWal->readLock is usually set, but might be -1 if there was a
58433 ** prior error while attempting to acquire are read-lock. This cannot
58436 ** upgrade to exclusive-mode following such an error.
58438 assert( pWal->readLock>=0 || pWal->lockError );
58439 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
58442 if( pWal->exclusiveMode ){
58443 pWal->exclusiveMode = 0;
58444 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
58445 pWal->exclusiveMode = 1;
58447 rc = pWal->exclusiveMode==0;
58453 assert( pWal->exclusiveMode==0 );
58454 assert( pWal->readLock>=0 );
58455 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
58456 pWal->exclusiveMode = 1;
58459 rc = pWal->exclusiveMode==0;
58465 ** Return true if the argument is non-NULL and the WAL module is using
58466 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
58467 ** WAL module is using shared-memory, return false.
58470 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
58483 assert( pWal->readLock>=0 && pWal->writeLock==0 );
58485 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
58493 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
58500 /* Try to open on pSnapshot when the next read-transaction starts
58503 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
58507 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
58516 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
58517 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
58518 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
58519 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
58527 ** read-lock. This function returns the database page-size if it is known,
58531 assert( pWal==0 || pWal->readLock>=0 );
58532 return (pWal ? pWal->szPage : 0);
58539 return pWal->pWalFd;
58576 ** This file implements an external (disk-based) database using BTrees.
58580 ** "Sorting And Searching", pages 473-480. Addison-Wesley
58586 ** ----------------------------------------------------------------
58587 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
58588 ** ----------------------------------------------------------------
58593 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
58615 ** page, or a pointer-map page.
58634 ** 40 60 15 4-byte meta values passed to higher layers
58639 ** 52 4 Largest root-page (auto/incr_vacuum)
58640 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
58643 ** 68 4 Application-ID
58645 ** 92 4 The version-valid-for number
58648 ** All of the integer values are big-endian (most significant byte first).
58656 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
58671 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
58674 ** |----------------|
58676 ** |----------------|
58678 ** |----------------|
58682 ** |----------------|
58685 ** |----------------| ^ Grows upwards
58688 ** |----------------|
58696 ** 5 2 first byte of the cell content area
58707 ** The cell pointer array contains zero or more 2-byte numbers which are
58738 ** appears first. A variable-length integer may not be more than 9 bytes long.
58740 ** allows a 64-bit integer to be encoded in 9 bytes.
58763 ** filled with data (pagesize - 4 bytes). The last page can have as little
58786 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
58793 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
58804 ** You can change this value at compile-time by specifying a
58805 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
58806 ** header must be exactly 16 bytes including the zero-terminator so
58818 ** first byte of on-disk image of every BTree page.
58828 ** is derived from the raw on-disk page content.
58835 ** stored in MemPage.pBt->mutex.
58840 u8 intKey; /* True if table b-trees. False for index b-trees */
58856 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
58857 ** non-overflow cell */
58917 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
58928 ** If the shared-data extension is enabled, there may be multiple users
58955 ** If a BtShared client fails to obtain a write-lock on a database
58956 ** table (because there exists one or more read-locks on the table),
58957 ** the shared-cache enters 'pending-lock' state and isPending is
58960 ** The shared-cache leaves the 'pending lock' state when either of
58966 ** while in the 'pending-lock' state, no connection may start a new
58969 ** This feature is included to help prevent writer-starvation.
58978 u8 autoVacuum; /* True if auto-vacuum is enabled */
58979 u8 incrVacuum; /* True if incr-vacuum is enabled */
58983 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
58988 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
58989 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
58998 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
58999 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
59003 BtLock *pLock; /* List of locks held on this shared-btree struct */
59018 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
59020 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
59032 u16 nSize; /* Size of the cell content on the main b-tree page */
59036 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
59039 ** root-node and 3 for all other internal nodes.
59048 ** b-tree within a database file.
59058 ** found at self->pBt->mutex.
59061 ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op.
59062 ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op.
59085 u8 curIntKey; /* Value of apPage[0]->intKey */
59087 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
59114 ** Cursor is valid except that the Cursor.skipNext field is non-zero
59116 ** operation should be a no-op.
59144 ** These macros define the location of the pointer-map entry for a
59149 ** PTRMAP_PAGENO returns the database page number of the pointer-map
59153 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
59155 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
59159 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
59176 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
59179 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
59187 ** overflow pages. The page-number identifies the previous
59190 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
59197 #define PTRMAP_BTREE 5
59203 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
59204 assert( p->pBt->inTransaction>=p->inTrans );
59209 ** if the database supports auto-vacuum or not. Because it is used
59215 #define ISAUTOVACUUM (pBt->autoVacuum)
59226 ** the database. As the integrity-check proceeds, for each page used in
59227 ** the database the corresponding bit is set. This allows integrity-check to
59234 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
59243 u32 *heap; /* Min-heap used for analyzing cell coverage */
59247 ** Routines to read or write a two- and four-byte big-endian integer values.
59256 ** two-byte aligned address. get2bytea() is only used for accessing the
59275 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
59277 ** p->locked boolean to true.
59280 assert( p->locked==0 );
59281 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
59282 assert( sqlite3_mutex_held(p->db->mutex) );
59284 sqlite3_mutex_enter(p->pBt->mutex);
59285 p->pBt->db = p->db;
59286 p->locked = 1;
59290 ** Release the BtShared mutex associated with B-Tree handle p and
59291 ** clear the p->locked boolean.
59294 BtShared *pBt = p->pBt;
59295 assert( p->locked==1 );
59296 assert( sqlite3_mutex_held(pBt->mutex) );
59297 assert( sqlite3_mutex_held(p->db->mutex) );
59298 assert( p->db==pBt->db );
59300 sqlite3_mutex_leave(pBt->mutex);
59301 p->locked = 0;
59311 ** and this routine is a no-op. The underlying mutex is non-recursive.
59316 ** by all database connections. The p->pNext is a list of other
59319 ** p, then first unlock all of the others on p->pNext, then wait
59328 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
59329 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
59330 assert( p->pNext==0 || p->pNext->db==p->db );
59331 assert( p->pPrev==0 || p->pPrev->db==p->db );
59332 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
59335 assert( !p->locked || p->wantToLock>0 );
59336 assert( p->sharable || p->wantToLock==0 );
59339 assert( sqlite3_mutex_held(p->db->mutex) );
59343 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
59345 if( !p->sharable ) return;
59346 p->wantToLock++;
59347 if( p->locked ) return;
59364 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
59365 p->pBt->db = p->db;
59366 p->locked = 1;
59375 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
59376 assert( pLater->sharable );
59377 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
59378 assert( !pLater->locked || pLater->wantToLock>0 );
59379 if( pLater->locked ){
59384 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
59385 if( pLater->wantToLock ){
59396 assert( sqlite3_mutex_held(p->db->mutex) );
59397 if( p->sharable ){
59398 assert( p->wantToLock>0 );
59399 p->wantToLock--;
59400 if( p->wantToLock==0 ){
59409 ** B-Tree is not marked as sharable.
59414 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
59415 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
59416 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
59417 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
59419 return (p->sharable==0 || p->locked);
59431 ** There is a corresponding leave-all procedures.
59442 assert( sqlite3_mutex_held(db->mutex) );
59443 for(i=0; i<db->nDb; i++){
59444 p = db->aDb[i].pBt;
59445 if( p && p->sharable ){
59450 db->skipBtreeMutex = skipOk;
59453 if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
59458 assert( sqlite3_mutex_held(db->mutex) );
59459 for(i=0; i<db->nDb; i++){
59460 p = db->aDb[i].pBt;
59465 if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
59477 if( !sqlite3_mutex_held(db->mutex) ){
59480 for(i=0; i<db->nDb; i++){
59482 p = db->aDb[i].pBt;
59483 if( p && p->sharable &&
59484 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
59495 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
59499 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
59508 assert( iDb>=0 && iDb<db->nDb );
59509 if( !sqlite3_mutex_held(db->mutex) ) return 0;
59511 p = db->aDb[iDb].pBt;
59513 return p->sharable==0 || p->locked==1;
59521 ** these two routines, all mutex operations are no-ops in that case and
59525 ** the ones below, are no-ops and are null #defines in btree.h.
59529 p->pBt->db = p->db;
59533 for(i=0; i<db->nDb; i++){
59534 Btree *p = db->aDb[i].pBt;
59536 p->pBt->db = p->db;
59551 sqlite3BtreeEnter(pCur->pBtree);
59555 sqlite3BtreeLeave(pCur->pBtree);
59575 ** This file implements an external (disk-based) database using BTrees.
59599 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
59604 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
59607 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
59610 ** Values passed as the 5th argument to allocateBtreePage()
59620 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
59665 ** shared-cache table level locks. If the library is compiled with the
59666 ** shared-cache feature disabled, then there is only ever one user
59668 ** So define the lock related functions as no-ops.
59687 ** For example, when writing to a table with root-page iRoot via
59700 ** hold a write-lock on the schema table (root page 1). This is also
59705 Pgno iRoot, /* Root page of b-tree */
59706 int isIndex, /* True if iRoot is the root of an index b-tree */
59709 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
59714 ** and has the read-uncommitted flag set, then no lock is required.
59717 if( (pBtree->sharable==0)
59718 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
59725 ** the correct locks are held. So do not bother - just return true.
59728 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
59732 /* Figure out the root-page that the lock should be held on. For table
59733 ** b-trees, this is just the root page of the b-tree being read or
59734 ** written. For index b-trees, it is the root page of the associated
59738 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
59740 if( pIdx->tnum==(int)iRoot ){
59747 iTab = pIdx->pTable->tnum;
59754 /* Search for the required lock. Either a write-lock on root-page iTab, a
59755 ** write-lock on the schema table, or (if the client is reading) a
59756 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
59757 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
59758 if( pLock->pBtree==pBtree
59759 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
59760 && pLock->eLock>=eLockType
59782 ** read-uncommitted flag set, then it is OK for the other object to
59792 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
59793 if( p->pgnoRoot==iRoot
59794 && p->pBtree!=pBtree
59795 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
59806 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
59811 BtShared *pBt = p->pBt;
59816 assert( p->db!=0 );
59817 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
59819 /* If requesting a write-lock, then the Btree must have an open write
59823 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
59824 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
59826 /* This routine is a no-op if the shared-cache is not enabled */
59827 if( !p->sharable ){
59834 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
59835 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
59839 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
59840 /* The condition (pIter->eLock!=eLock) in the following if(...)
59843 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
59849 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
59850 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
59851 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
59852 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
59854 assert( p==pBt->pWriter );
59855 pBt->btsFlags |= BTS_PENDING;
59866 ** Add a lock on the table with root-page iTable to the shared-btree used
59883 BtShared *pBt = p->pBt;
59889 assert( p->db!=0 );
59891 /* A connection with the read-uncommitted flag set will never try to
59892 ** obtain a read-lock using this function. The only read-lock obtained
59893 ** by a connection in read-uncommitted mode is on the sqlite_master
59895 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
59897 /* This function should only be called on a sharable b-tree after it
59898 ** has been determined that no other b-tree holds a conflicting lock. */
59899 assert( p->sharable );
59903 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
59904 if( pIter->iTable==iTable && pIter->pBtree==p ){
59918 pLock->iTable = iTable;
59919 pLock->pBtree = p;
59920 pLock->pNext = pBt->pLock;
59921 pBt->pLock = pLock;
59925 ** and the requested lock. This means if a write-lock was already held
59926 ** and a read-lock requested, we don't incorrectly downgrade the lock.
59929 if( eLock>pLock->eLock ){
59930 pLock->eLock = eLock;
59947 BtShared *pBt = p->pBt;
59948 BtLock **ppIter = &pBt->pLock;
59951 assert( p->sharable || 0==*ppIter );
59952 assert( p->inTrans>0 );
59956 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
59957 assert( pLock->pBtree->inTrans>=pLock->eLock );
59958 if( pLock->pBtree==p ){
59959 *ppIter = pLock->pNext;
59960 assert( pLock->iTable!=1 || pLock==&p->lock );
59961 if( pLock->iTable!=1 ){
59965 ppIter = &pLock->pNext;
59969 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
59970 if( pBt->pWriter==p ){
59971 pBt->pWriter = 0;
59972 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
59973 }else if( pBt->nTransaction==2 ){
59983 pBt->btsFlags &= ~BTS_PENDING;
59988 ** This function changes all write-locks held by Btree p into read-locks.
59991 BtShared *pBt = p->pBt;
59992 if( pBt->pWriter==p ){
59994 pBt->pWriter = 0;
59995 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
59996 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
59997 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
59998 pLock->eLock = READ_LOCK;
60014 return sqlite3_mutex_held(p->pBt->mutex);
60018 ** database connetion. This is important in shared-cache mode. If the database
60019 ** connection pointers get out-of-sync, it is possible for routines like
60022 ** statements only and for the purpose of double-checking that the btree code
60023 ** does keep the database connection pointers up-to-date.
60027 return (p->pBtree->db==p->pBt->db);
60035 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
60038 ** Invalidate the overflow page-list cache for all cursors opened
60043 assert( sqlite3_mutex_held(pBt->mutex) );
60044 for(p=pBt->pCursor; p; p=p->pNext){
60057 ** cursors open on any row within the table with root-page pgnoRoot.
60070 if( pBtree->hasIncrblobCur==0 ) return;
60072 pBtree->hasIncrblobCur = 0;
60073 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
60074 if( (p->curFlags & BTCF_Incrblob)!=0 ){
60075 pBtree->hasIncrblobCur = 1;
60076 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
60077 p->eState = CURSOR_INVALID;
60090 ** when a page that previously contained data becomes a free-list leaf
60095 ** free-list leaf pages:
60098 ** a free-list leaf page, the page is not written to the database
60099 ** (as free-list leaf pages contain no meaningful data). Sometimes
60103 ** 2) When a free-list leaf page is reused, its content is not read
60109 ** a page is moved to the free-list and then reused within the same
60111 ** it is moved to the free-list and it is also not journalled when it
60112 ** is extracted from the free-list and reused, then the original data
60117 ** moved to become a free-list leaf page, the corresponding bit is
60118 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
60125 if( !pBt->pHasContent ){
60126 assert( pgno<=pBt->nPage );
60127 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
60128 if( !pBt->pHasContent ){
60132 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
60133 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
60141 ** This function is called when a free-list leaf page is removed from the
60142 ** free-list for reuse. It returns false if it is safe to retrieve the
60143 ** page from the pager layer with the 'no-content' flag set. True otherwise.
60146 Bitvec *p = pBt->pHasContent;
60152 ** invoked at the conclusion of each write-transaction.
60155 sqlite3BitvecDestroy(pBt->pHasContent);
60156 pBt->pHasContent = 0;
60164 for(i=0; i<=pCur->iPage; i++){
60165 releasePage(pCur->apPage[i]);
60166 pCur->apPage[i] = 0;
60168 pCur->iPage = -1;
60174 ** function saves the current cursor key in variables pCur->nKey and
60175 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
60179 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
60180 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
60181 ** set to point to a malloced buffer pCur->nKey bytes in size containing
60186 assert( CURSOR_VALID==pCur->eState );
60187 assert( 0==pCur->pKey );
60190 if( pCur->curIntKey ){
60192 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
60196 pCur->nKey = sqlite3BtreePayloadSize(pCur);
60197 pKey = sqlite3Malloc( pCur->nKey );
60199 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
60201 pCur->pKey = pKey;
60209 assert( !pCur->curIntKey || !pCur->pKey );
60223 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
60224 assert( 0==pCur->pKey );
60227 if( pCur->eState==CURSOR_SKIPNEXT ){
60228 pCur->eState = CURSOR_VALID;
60230 pCur->skipNext = 0;
60236 pCur->eState = CURSOR_REQUIRESEEK;
60239 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
60248 ** the table with root-page iRoot. "Saving the cursor position" means that
60259 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
60269 assert( sqlite3_mutex_held(pBt->mutex) );
60270 assert( pExcept==0 || pExcept->pBt==pBt );
60271 for(p=pBt->pCursor; p; p=p->pNext){
60272 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
60275 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
60290 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
60291 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
60297 testcase( p->iPage>0 );
60301 p = p->pNext;
60311 sqlite3_free(pCur->pKey);
60312 pCur->pKey = 0;
60313 pCur->eState = CURSOR_INVALID;
60333 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
60335 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
60336 if( pIdxKey->nField==0 ){
60337 rc = SQLITE_CORRUPT_PGNO(pCur->apPage[pCur->iPage]->pgno);
60346 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
60362 assert( pCur->eState>=CURSOR_REQUIRESEEK );
60363 if( pCur->eState==CURSOR_FAULT ){
60364 return pCur->skipNext;
60366 pCur->eState = CURSOR_INVALID;
60367 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
60369 sqlite3_free(pCur->pKey);
60370 pCur->pKey = 0;
60371 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
60372 pCur->skipNext |= skipNext;
60373 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
60374 pCur->eState = CURSOR_SKIPNEXT;
60381 (p->eState>=CURSOR_REQUIRESEEK ? \
60398 return pCur->eState!=CURSOR_VALID;
60418 assert( pCur->eState!=CURSOR_VALID );
60424 if( pCur->eState!=CURSOR_VALID ){
60427 assert( pCur->skipNext==0 );
60449 pCur->hints = x;
60456 ** number for the pointer-map page that contains the entry for the
60466 assert( sqlite3_mutex_held(pBt->mutex) );
60468 nPagesPerMapPage = (pBt->usableSize/5)+1;
60469 iPtrMap = (pgno-2)/nPagesPerMapPage;
60483 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
60484 ** a no-op. If an error occurs, the appropriate error code is written
60496 assert( sqlite3_mutex_held(pBt->mutex) );
60497 /* The master-journal page number must never be used as a pointer map page */
60500 assert( pBt->autoVacuum );
60506 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
60516 assert( offset <= (int)pBt->usableSize-5 );
60520 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
60546 assert( sqlite3_mutex_held(pBt->mutex) );
60549 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
60560 assert( offset <= (int)pBt->usableSize-5 );
60566 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
60582 ** 4-byte child pointer found on interior pages, if there is one.
60587 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
60589 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
60595 ** on a single B-tree page. Make necessary adjustments to the CellInfo
60616 minLocal = pPage->minLocal;
60617 maxLocal = pPage->maxLocal;
60618 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
60622 pInfo->nLocal = (u16)surplus;
60624 pInfo->nLocal = (u16)minLocal;
60626 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
60648 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60649 assert( pPage->leaf==0 );
60650 assert( pPage->childPtrSize==4 );
60654 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
60655 pInfo->nPayload = 0;
60656 pInfo->nLocal = 0;
60657 pInfo->pPayload = 0;
60669 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60670 assert( pPage->leaf==0 || pPage->leaf==1 );
60671 assert( pPage->intKeyLeaf );
60672 assert( pPage->childPtrSize==0 );
60693 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
60712 pInfo->nKey = *(i64*)&iKey;
60713 pInfo->nPayload = nPayload;
60714 pInfo->pPayload = pIter;
60715 testcase( nPayload==pPage->maxLocal );
60716 testcase( nPayload==pPage->maxLocal+1 );
60717 if( nPayload<=pPage->maxLocal ){
60721 pInfo->nSize = nPayload + (u16)(pIter - pCell);
60722 if( pInfo->nSize<4 ) pInfo->nSize = 4;
60723 pInfo->nLocal = (u16)nPayload;
60736 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60737 assert( pPage->leaf==0 || pPage->leaf==1 );
60738 assert( pPage->intKeyLeaf==0 );
60739 pIter = pCell + pPage->childPtrSize;
60749 pInfo->nKey = nPayload;
60750 pInfo->nPayload = nPayload;
60751 pInfo->pPayload = pIter;
60752 testcase( nPayload==pPage->maxLocal );
60753 testcase( nPayload==pPage->maxLocal+1 );
60754 if( nPayload<=pPage->maxLocal ){
60758 pInfo->nSize = nPayload + (u16)(pIter - pCell);
60759 if( pInfo->nSize<4 ) pInfo->nSize = 4;
60760 pInfo->nLocal = (u16)nPayload;
60770 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
60778 ** data area of the btree-page. The return number includes the cell
60786 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
60796 pPage->xParseCell(pPage, pCell, &debuginfo);
60808 if( pPage->intKey ){
60809 /* pIter now points at the 64-bit integer key value, a variable length
60815 testcase( nSize==pPage->maxLocal );
60816 testcase( nSize==pPage->maxLocal+1 );
60817 if( nSize<=pPage->maxLocal ){
60818 nSize += (u32)(pIter - pCell);
60821 int minLocal = pPage->minLocal;
60822 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
60823 testcase( nSize==pPage->maxLocal );
60824 testcase( nSize==pPage->maxLocal+1 );
60825 if( nSize>pPage->maxLocal ){
60828 nSize += 4 + (u16)(pIter - pCell);
60843 pPage->xParseCell(pPage, pCell, &debuginfo);
60848 assert( pPage->childPtrSize==4 );
60851 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
60852 return (u16)(pIter - pCell);
60860 return pPage->xCellSize(pPage, findCell(pPage, iCell));
60867 ** to an overflow page, insert an entry into the pointer-map
60874 pPage->xParseCell(pPage, pCell, &info);
60876 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
60877 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
60885 ** page so that there are no free-blocks on the free-block list.
60890 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
60891 ** b-tree page so that there are no freeblocks or fragment bytes, all
60897 int pc; /* Address of the i-th cell */
60910 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60911 assert( pPage->pBt!=0 );
60912 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
60913 assert( pPage->nOverflow==0 );
60914 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60916 src = data = pPage->aData;
60917 hdr = pPage->hdrOffset;
60918 cellOffset = pPage->cellOffset;
60919 nCell = pPage->nCell;
60922 usableSize = pPage->pBt->usableSize;
60927 ** offsets to each pointer in the cell-pointer array than it is to
60949 int top = get2byte(&data[hdr+5]);
60951 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
60953 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
60954 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
60958 assert( cbrk+(iFree-top) <= usableSize );
60959 memmove(&data[cbrk], &data[top], iFree-top);
60971 iCellLast = usableSize - 4;
60973 u8 *pAddr; /* The i-th cell pointer */
60982 return SQLITE_CORRUPT_PGNO(pPage->pgno);
60985 size = pPage->xCellSize(pPage, &src[pc]);
60986 cbrk -= size;
60988 return SQLITE_CORRUPT_PGNO(pPage->pgno);
60997 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
60998 x = get2byte(&data[hdr+5]);
60999 memcpy(&temp[x], &data[x], (cbrk+size) - x);
61007 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
61008 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61011 put2byte(&data[hdr+5], cbrk);
61014 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
61015 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61020 ** Search the free-list on page pPg for space to store a cell nByte bytes in
61022 ** from the free-list.
61024 ** If no suitable space can be found on the free-list, return NULL.
61034 const int hdr = pPg->hdrOffset;
61035 u8 * const aData = pPg->aData;
61039 int usableSize = pPg->pBt->usableSize;
61044 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
61046 if( pc>usableSize-4 || pc<iAddr+4 ){
61047 *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
61050 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
61051 ** freeblock form a big-endian integer which is the size of the freeblock
61052 ** in bytes, including the 4-byte header. */
61054 if( (x = size - nByte)>=0 ){
61057 if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
61058 *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
61061 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
61065 /* Remove the slot from the free-list. Update the number of
61070 /* The slot remains on the free-list. Reduce its size to account
61084 ** Allocate nByte bytes of space from within the B-Tree page passed
61085 ** as the first argument. Write into *pIdx the index into pPage->aData[]
61097 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
61098 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
61103 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61104 assert( pPage->pBt );
61105 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61107 assert( pPage->nFree>=nByte );
61108 assert( pPage->nOverflow==0 );
61109 assert( nByte < (int)(pPage->pBt->usableSize-8) );
61111 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
61112 gap = pPage->cellOffset + 2*pPage->nCell;
61114 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
61117 ** However, that integer is too large to be stored in a 2-byte unsigned
61119 top = get2byte(&data[hdr+5]);
61120 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
61122 if( top==0 && pPage->pBt->usableSize==65536 ){
61125 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61139 assert( pSpace>=data && (pSpace - data)<65536 );
61140 *pIdx = (int)(pSpace - data);
61152 assert( pPage->nCell>0 || CORRUPT_DB );
61153 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
61155 top = get2byteNotZero(&data[hdr+5]);
61166 top -= nByte;
61167 put2byte(&data[hdr+5], top);
61168 assert( top+nByte <= (int)pPage->pBt->usableSize );
61174 ** Return a section of the pPage->aData to the freelist.
61175 ** The first byte of the new free block is pPage->aData[iStart]
61192 u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
61194 unsigned char *data = pPage->aData; /* Page content */
61196 assert( pPage->pBt!=0 );
61197 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61198 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
61199 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
61200 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61206 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
61213 hdr = pPage->hdrOffset;
61221 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61225 if( iFreeBlk>iLast ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61235 nFrag = iFreeBlk - iEnd;
61236 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61238 if( iEnd > pPage->pBt->usableSize ){
61239 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61241 iSize = iEnd - iStart;
61252 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61253 nFrag += iStart - iPtrEnd;
61254 iSize = iEnd - iPtr;
61258 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61259 data[hdr+7] -= nFrag;
61261 if( iStart==get2byte(&data[hdr+5]) ){
61265 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61267 put2byte(&data[hdr+5], iEnd);
61274 pPage->nFree += iOrigSize;
61291 BtShared *pBt; /* A copy of pPage->pBt */
61293 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
61294 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61295 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
61297 pPage->childPtrSize = 4-4*pPage->leaf;
61298 pPage->xCellSize = cellSizePtr;
61299 pBt = pPage->pBt;
61301 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
61302 ** interior table b-tree page. */
61303 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
61304 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
61305 ** leaf table b-tree page. */
61307 pPage->intKey = 1;
61308 if( pPage->leaf ){
61309 pPage->intKeyLeaf = 1;
61310 pPage->xParseCell = btreeParseCellPtr;
61312 pPage->intKeyLeaf = 0;
61313 pPage->xCellSize = cellSizePtrNoPayload;
61314 pPage->xParseCell = btreeParseCellPtrNoPayload;
61316 pPage->maxLocal = pBt->maxLeaf;
61317 pPage->minLocal = pBt->minLeaf;
61319 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
61320 ** interior index b-tree page. */
61322 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
61323 ** leaf index b-tree page. */
61325 pPage->intKey = 0;
61326 pPage->intKeyLeaf = 0;
61327 pPage->xParseCell = btreeParseCellPtrIndex;
61328 pPage->maxLocal = pBt->maxLocal;
61329 pPage->minLocal = pBt->minLocal;
61331 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
61333 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61335 pPage->max1bytePayload = pBt->max1bytePayload;
61343 ** not contain a well-formed database page, then return
61345 ** guarantee that the page is well-formed. It only shows that
61349 int pc; /* Address of a freeblock within pPage->aData[] */
61351 u8 *data; /* Equal to pPage->aData */
61360 assert( pPage->pBt!=0 );
61361 assert( pPage->pBt->db!=0 );
61362 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61363 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
61364 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
61365 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
61366 assert( pPage->isInit==0 );
61368 pBt = pPage->pBt;
61369 hdr = pPage->hdrOffset;
61370 data = pPage->aData;
61371 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
61372 ** the b-tree page type. */
61374 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61376 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
61377 pPage->maskPage = (u16)(pBt->pageSize - 1);
61378 pPage->nOverflow = 0;
61379 usableSize = pBt->usableSize;
61380 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
61381 pPage->aDataEnd = &data[usableSize];
61382 pPage->aCellIdx = &data[cellOffset];
61383 pPage->aDataOfst = &data[pPage->childPtrSize];
61384 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
61387 top = get2byteNotZero(&data[hdr+5]);
61388 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
61390 pPage->nCell = get2byte(&data[hdr+3]);
61391 if( pPage->nCell>MX_CELL(pBt) ){
61393 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61395 testcase( pPage->nCell==MX_CELL(pBt) );
61396 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
61400 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
61409 iCellFirst = cellOffset + 2*pPage->nCell;
61410 iCellLast = usableSize - 4;
61411 if( pBt->db->flags & SQLITE_CellSizeCk ){
61415 if( !pPage->leaf ) iCellLast--;
61416 for(i=0; i<pPage->nCell; i++){
61421 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61423 sz = pPage->xCellSize(pPage, &data[pc]);
61426 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61429 if( !pPage->leaf ) iCellLast++;
61433 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
61437 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
61441 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
61444 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61449 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61459 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61463 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61468 ** of the cell-content area plus the number of free bytes within
61469 ** the cell-content area. If this is greater than the usable-size
61471 ** serves to verify that the offset to the start of the cell-content
61475 return SQLITE_CORRUPT_PGNO(pPage->pgno);
61477 pPage->nFree = (u16)(nFree - iCellFirst);
61478 pPage->isInit = 1;
61487 unsigned char *data = pPage->aData;
61488 BtShared *pBt = pPage->pBt;
61489 u8 hdr = pPage->hdrOffset;
61492 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
61493 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
61494 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
61495 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
61496 assert( sqlite3_mutex_held(pBt->mutex) );
61497 if( pBt->btsFlags & BTS_FAST_SECURE ){
61498 memset(&data[hdr], 0, pBt->usableSize - hdr);
61504 put2byte(&data[hdr+5], pBt->usableSize);
61505 pPage->nFree = (u16)(pBt->usableSize - first);
61507 pPage->cellOffset = first;
61508 pPage->aDataEnd = &data[pBt->usableSize];
61509 pPage->aCellIdx = &data[first];
61510 pPage->aDataOfst = &data[pPage->childPtrSize];
61511 pPage->nOverflow = 0;
61512 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
61513 pPage->maskPage = (u16)(pBt->pageSize - 1);
61514 pPage->nCell = 0;
61515 pPage->isInit = 1;
61525 if( pgno!=pPage->pgno ){
61526 pPage->aData = sqlite3PagerGetData(pDbPage);
61527 pPage->pDbPage = pDbPage;
61528 pPage->pBt = pBt;
61529 pPage->pgno = pgno;
61530 pPage->hdrOffset = pgno==1 ? 100 : 0;
61532 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
61557 assert( sqlite3_mutex_held(pBt->mutex) );
61558 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
61571 assert( sqlite3_mutex_held(pBt->mutex) );
61572 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
61581 ** error, return ((unsigned int)-1).
61584 return pBt->nPage;
61588 assert( ((p->pBt->nPage)&0x8000000)==0 );
61589 return btreePagecount(p->pBt);
61597 ** And if the fetch fails, this routine must decrement pCur->iPage.
61599 ** The page is fetched as read-write unless pCur is not NULL and is
61600 ** a read-only cursor.
61610 int bReadOnly /* True for a read-only page */
61614 assert( sqlite3_mutex_held(pBt->mutex) );
61615 assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
61616 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
61617 assert( pCur==0 || pCur->iPage>0 );
61623 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
61628 if( (*ppPage)->isInit==0 ){
61636 assert( (*ppPage)->pgno==pgno );
61637 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
61641 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
61649 if( pCur ) pCur->iPage--;
61660 assert( pPage->aData );
61661 assert( pPage->pBt );
61662 assert( pPage->pDbPage!=0 );
61663 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
61664 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
61665 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61666 sqlite3PagerUnrefNotNull(pPage->pDbPage);
61689 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
61694 (*ppPage)->isInit = 0;
61714 if( pPage->isInit ){
61715 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61716 pPage->isInit = 0;
61723 ** the call for every page that comes in for re-initing. */
61734 assert( pBt->db );
61735 assert( sqlite3_mutex_held(pBt->db->mutex) );
61736 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
61744 ** be exclusively in memory, or it might use a disk-based memory cache.
61748 ** If zFilename is ":memory:" then an in-memory database is created
61761 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
61778 /* Set the variable isMemdb to true for an in-memory database, or
61779 ** false for a file-based database.
61791 assert( sqlite3_mutex_held(db->mutex) );
61810 p->inTrans = TRANS_NONE;
61811 p->db = db;
61813 p->lock.pBtree = p;
61814 p->lock.iTable = 1;
61825 int nFullPathname = pVfs->mxPathname+1;
61829 p->sharable = 1;
61851 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
61852 assert( pBt->nRef>0 );
61853 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
61854 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
61856 for(iDb=db->nDb-1; iDb>=0; iDb--){
61857 Btree *pExisting = db->aDb[iDb].pBt;
61858 if( pExisting && pExisting->pBt==pBt ){
61866 p->pBt = pBt;
61867 pBt->nRef++;
61881 p->sharable = 1;
61903 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
61906 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
61907 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
61912 pBt->openFlags = (u8)flags;
61913 pBt->db = db;
61914 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
61915 p->pBt = pBt;
61917 pBt->pCursor = 0;
61918 pBt->pPage1 = 0;
61919 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
61921 pBt->btsFlags |= BTS_SECURE_DELETE;
61923 pBt->btsFlags |= BTS_OVERWRITE;
61925 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
61926 ** determined by the 2-byte integer located at an offset of 16 bytes from
61928 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
61929 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
61930 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
61931 pBt->pageSize = 0;
61933 /* If the magic name ":memory:" will create an in-memory database, then
61934 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
61937 ** regular file-name. In this case the auto-vacuum applies as per normal.
61940 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
61941 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
61946 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
61947 ** determined by the one-byte unsigned integer found at an offset of 20
61950 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
61952 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
61953 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
61956 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
61958 pBt->usableSize = pBt->pageSize - nReserve;
61959 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
61964 pBt->nRef = 1;
61965 if( p->sharable ){
61969 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
61970 if( pBt->mutex==0 ){
61976 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
61988 if( p->sharable ){
61991 for(i=0; i<db->nDb; i++){
61992 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
61993 while( pSib->pPrev ){ pSib = pSib->pPrev; }
61994 if( (uptr)p->pBt<(uptr)pSib->pBt ){
61995 p->pNext = pSib;
61996 p->pPrev = 0;
61997 pSib->pPrev = p;
61999 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
62000 pSib = pSib->pNext;
62002 p->pNext = pSib->pNext;
62003 p->pPrev = pSib;
62004 if( p->pNext ){
62005 p->pNext->pPrev = p;
62007 pSib->pNext = p;
62018 if( pBt && pBt->pPager ){
62019 sqlite3PagerClose(pBt->pPager, 0);
62027 /* If the B-Tree was successfully opened, set the pager-cache size to the
62028 ** default value. Except, when opening on an existing shared pager-cache,
62029 ** do not change the pager-cache size.
62032 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
62035 pFile = sqlite3PagerFile(pBt->pPager);
62036 if( pFile->pMethods ){
62037 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
62060 assert( sqlite3_mutex_notheld(pBt->mutex) );
62063 pBt->nRef--;
62064 if( pBt->nRef<=0 ){
62066 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
62069 while( ALWAYS(pList) && pList->pNext!=pBt ){
62070 pList=pList->pNext;
62073 pList->pNext = pBt->pNext;
62077 sqlite3_mutex_free(pBt->mutex);
62089 ** Make sure pBt->pTmpSpace points to an allocation of
62090 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
62094 if( !pBt->pTmpSpace ){
62095 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
62097 /* One of the uses of pBt->pTmpSpace is to format cells before
62110 ** left-child pointer to the beginning of a cell.
62112 if( pBt->pTmpSpace ){
62113 memset(pBt->pTmpSpace, 0, 8);
62114 pBt->pTmpSpace += 4;
62120 ** Free the pBt->pTmpSpace allocation
62123 if( pBt->pTmpSpace ){
62124 pBt->pTmpSpace -= 4;
62125 sqlite3PageFree(pBt->pTmpSpace);
62126 pBt->pTmpSpace = 0;
62134 BtShared *pBt = p->pBt;
62138 assert( sqlite3_mutex_held(p->db->mutex) );
62140 pCur = pBt->pCursor;
62143 pCur = pCur->pNext;
62144 if( pTmp->pBtree==p ){
62150 ** The call to sqlite3BtreeRollback() drops any table-locks held by
62156 /* If there are still other outstanding references to the shared-btree
62158 ** up the shared-btree.
62160 assert( p->wantToLock==0 && p->locked==0 );
62161 if( !p->sharable || removeFromSharingList(pBt) ){
62167 assert( !pBt->pCursor );
62168 sqlite3PagerClose(pBt->pPager, p->db);
62169 if( pBt->xFreeSchema && pBt->pSchema ){
62170 pBt->xFreeSchema(pBt->pSchema);
62172 sqlite3DbFree(0, pBt->pSchema);
62178 assert( p->wantToLock==0 );
62179 assert( p->locked==0 );
62180 if( p->pPrev ) p->pPrev->pNext = p->pNext;
62181 if( p->pNext ) p->pNext->pPrev = p->pPrev;
62196 BtShared *pBt = p->pBt;
62197 assert( sqlite3_mutex_held(p->db->mutex) );
62199 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
62215 BtShared *pBt = p->pBt;
62217 assert( sqlite3_mutex_held(p->db->mutex) );
62219 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
62230 BtShared *pBt = p->pBt;
62231 assert( sqlite3_mutex_held(p->db->mutex) );
62233 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
62244 ** is a very low but non-zero probability of damage. Level 3 reduces the
62252 BtShared *pBt = p->pBt;
62253 assert( sqlite3_mutex_held(p->db->mutex) );
62255 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
62283 BtShared *pBt = p->pBt;
62284 assert( nReserve>=-1 && nReserve<=255 );
62287 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
62289 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
62294 nReserve = pBt->pageSize - pBt->usableSize;
62298 ((pageSize-1)&pageSize)==0 ){
62300 assert( !pBt->pCursor );
62301 pBt->pageSize = (u32)pageSize;
62304 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
62305 pBt->usableSize = pBt->pageSize - (u16)nReserve;
62306 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
62315 return p->pBt->pageSize;
62320 ** may only be called if it is guaranteed that the b-tree mutex is already
62324 ** known that the shared b-tree mutex is held, but the mutex on the
62331 assert( sqlite3_mutex_held(p->pBt->mutex) );
62332 n = p->pBt->pageSize - p->pBt->usableSize;
62350 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
62365 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
62376 ** newFlag==(-1) No changes
62381 ** freelist leaf pages are not written back to the database. Thus in-page
62395 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
62396 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
62398 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
62404 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
62405 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
62406 ** is disabled. The default value for the auto-vacuum property is
62413 BtShared *pBt = p->pBt;
62418 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
62421 pBt->autoVacuum = av ?1:0;
62422 pBt->incrVacuum = av==2 ?1:0;
62430 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
62440 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
62441 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
62450 ** If the user has not set the safety-level for this database connection
62451 ** using "PRAGMA synchronous", and if the safety-level is not already
62459 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
62460 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
62461 if( pDb->bSyncSet==0
62462 && pDb->safety_level!=safety_level
62463 && pDb!=&db->aDb[1]
62465 pDb->safety_level = safety_level;
62466 sqlite3PagerSetFlags(pBt->pPager,
62467 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
62480 ** well-formed database file, then SQLITE_CORRUPT is returned.
62491 assert( sqlite3_mutex_held(pBt->mutex) );
62492 assert( pBt->pPage1==0 );
62493 rc = sqlite3PagerSharedLock(pBt->pPager);
62501 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
62502 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
62503 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
62509 u8 *page1 = pPage1->aData;
62511 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
62520 pBt->btsFlags |= BTS_READ_ONLY;
62527 pBt->btsFlags |= BTS_READ_ONLY;
62538 ** may not be the latest version - there may be a newer one in the log
62541 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
62543 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
62559 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
62568 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
62569 ** determined by the 2-byte integer located at an offset of 16 bytes from
62572 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
62574 if( ((pageSize-1)&pageSize)!=0
62581 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
62585 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
62586 ** determined by the one-byte unsigned integer found at an offset of 20
62588 usableSize = pageSize - page1[20];
62589 if( (u32)pageSize!=pBt->pageSize ){
62591 ** of BtShared.pageSize, we have discovered that the page-size is
62592 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
62594 ** again with the correct page-size.
62597 pBt->usableSize = usableSize;
62598 pBt->pageSize = pageSize;
62600 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
62601 pageSize-usableSize);
62604 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
62608 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
62614 pBt->pageSize = pageSize;
62615 pBt->usableSize = usableSize;
62617 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
62618 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
62624 ** cells can will fit on one page. We assume a 10-byte page header.
62626 ** 2-byte pointer to the cell
62627 ** 4-byte child pointer
62628 ** 9-byte nKey value
62629 ** 4-byte nData value
62630 ** 4-byte overflow page pointer
62631 ** So a cell consists of a 2-byte pointer, a header which is as much as
62635 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
62636 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
62637 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
62638 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
62639 if( pBt->maxLocal>127 ){
62640 pBt->max1bytePayload = 127;
62642 pBt->max1bytePayload = (u8)pBt->maxLocal;
62644 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
62645 pBt->pPage1 = pPage1;
62646 pBt->nPage = nPage;
62651 pBt->pPage1 = 0;
62671 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
62672 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
62673 && pCur->eState!=CURSOR_FAULT ) r++;
62685 ** If there is a transaction in progress, this routine is a no-op.
62688 assert( sqlite3_mutex_held(pBt->mutex) );
62689 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
62690 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
62691 MemPage *pPage1 = pBt->pPage1;
62692 assert( pPage1->aData );
62693 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
62694 pBt->pPage1 = 0;
62709 assert( sqlite3_mutex_held(pBt->mutex) );
62710 if( pBt->nPage>0 ){
62713 pP1 = pBt->pPage1;
62715 data = pP1->aData;
62716 rc = sqlite3PagerWrite(pP1->pDbPage);
62720 data[16] = (u8)((pBt->pageSize>>8)&0xff);
62721 data[17] = (u8)((pBt->pageSize>>16)&0xff);
62724 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
62725 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
62729 memset(&data[24], 0, 100-24);
62731 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
62733 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
62734 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
62735 put4byte(&data[36 + 4*4], pBt->autoVacuum);
62736 put4byte(&data[36 + 7*4], pBt->incrVacuum);
62738 pBt->nPage = 1;
62751 p->pBt->nPage = 0;
62752 rc = newDatabase(p->pBt);
62758 ** Attempt to start a new transaction. A write-transaction
62759 ** is started if the second argument is nonzero, otherwise a read-
62763 ** upgraded to exclusive by calling this routine a second time - the
62766 ** A write-transaction must be started before attempting any
62780 ** if there is one. But if there was previously a read-lock, do not
62781 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
62782 ** returned when there is already a read-lock in order to avoid a deadlock.
62793 BtShared *pBt = p->pBt;
62799 /* If the btree is already in a write-transaction, or it
62800 ** is already in a read-transaction and a read-transaction
62801 ** is requested, this is a no-op.
62803 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
62806 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
62808 /* Write transactions are not possible on a read-only database */
62809 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
62818 ** on this shared-btree structure and a second write transaction is
62821 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
62822 || (pBt->btsFlags & BTS_PENDING)!=0
62824 pBlock = pBt->pWriter->db;
62827 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
62828 if( pIter->pBtree!=p ){
62829 pBlock = pIter->pBtree->db;
62835 sqlite3ConnectionBlocked(p->db, pBlock);
62842 /* Any read-only or read-write transaction implies a read-lock on
62843 ** page 1. So if some other shared-cache client already has a write-lock
62848 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
62849 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
62851 /* Call lockBtree() until either pBt->pPage1 is populated or
62853 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
62854 ** reading page 1 it discovers that the page-size of the database
62855 ** file is not pBt->pageSize. In this case lockBtree() will update
62856 ** pBt->pageSize to the page-size of the file on disk.
62858 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
62861 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
62864 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
62874 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
62878 if( p->inTrans==TRANS_NONE ){
62879 pBt->nTransaction++;
62881 if( p->sharable ){
62882 assert( p->lock.pBtree==p && p->lock.iTable==1 );
62883 p->lock.eLock = READ_LOCK;
62884 p->lock.pNext = pBt->pLock;
62885 pBt->pLock = &p->lock;
62889 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
62890 if( p->inTrans>pBt->inTransaction ){
62891 pBt->inTransaction = p->inTrans;
62894 MemPage *pPage1 = pBt->pPage1;
62896 assert( !pBt->pWriter );
62897 pBt->pWriter = p;
62898 pBt->btsFlags &= ~BTS_EXCLUSIVE;
62899 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
62902 /* If the db-size header field is incorrect (as it may be if an old
62905 ** re-read the database size from page 1 if a savepoint or transaction
62908 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
62909 rc = sqlite3PagerWrite(pPage1->pDbPage);
62911 put4byte(&pPage1->aData[28], pBt->nPage);
62922 ** the sub-journal is not already open, then it will be opened here.
62924 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
62935 ** Set the pointer-map entries for all children of page pPage. Also, if
62943 BtShared *pBt = pPage->pBt;
62944 Pgno pgno = pPage->pgno;
62946 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62947 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
62949 nCell = pPage->nCell;
62956 if( !pPage->leaf ){
62962 if( !pPage->leaf ){
62963 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
62975 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
62978 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
62981 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
62985 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
62986 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
62989 if( get4byte(pPage->aData)!=iFrom ){
62990 return SQLITE_CORRUPT_PGNO(pPage->pgno);
62992 put4byte(pPage->aData, iTo);
62998 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
63000 nCell = pPage->nCell;
63006 pPage->xParseCell(pPage, pCell, &info);
63008 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
63009 return SQLITE_CORRUPT_PGNO(pPage->pgno);
63011 if( iFrom==get4byte(pCell+info.nSize-4) ){
63012 put4byte(pCell+info.nSize-4, iTo);
63026 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
63027 return SQLITE_CORRUPT_PGNO(pPage->pgno);
63029 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
63041 ** the journal needs to be sync()ed before database page pDbPage->pgno
63049 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
63054 Pgno iDbPage = pDbPage->pgno;
63055 Pager *pPager = pBt->pPager;
63060 assert( sqlite3_mutex_held(pBt->mutex) );
63061 assert( pDbPage->pBt==pBt );
63066 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
63070 pDbPage->pgno = iFreePage;
63072 /* If pDbPage was a btree-page, then it may have child pages and/or cells
63086 Pgno nextOvfl = get4byte(pDbPage->aData);
63104 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
63122 ** Perform a single step of an incremental-vacuum. If successful, return
63127 ** More specifically, this function attempts to re-organize the database so
63133 ** If the bCommit parameter is non-zero, this function assumes that the
63135 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
63139 Pgno nFreeList; /* Number of pages still on the free-list */
63142 assert( sqlite3_mutex_held(pBt->mutex) );
63149 nFreeList = get4byte(&pBt->pPage1->aData[36]);
63164 /* Remove the page from the files free-list. This is not required
63165 ** if bCommit is non-zero. In that case, the free-list will be
63193 ** looping until a free-page located within the first nFin pages
63221 iLastPg--;
63223 pBt->bDoTruncate = 1;
63224 pBt->nPage = iLastPg;
63230 ** The database opened by the first argument is an auto-vacuum database
63232 ** size of the database in pages following an auto-vacuum operation.
63239 nEntry = pBt->usableSize/5;
63240 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
63241 nFin = nOrig - nFree - nPtrmap;
63243 nFin--;
63246 nFin--;
63253 ** A write-transaction must be opened before calling this function.
63262 BtShared *pBt = p->pBt;
63265 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
63266 if( !pBt->autoVacuum ){
63270 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
63282 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
63283 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
63295 ** is committed for an auto-vacuum database.
63304 Pager *pPager = pBt->pPager;
63307 assert( sqlite3_mutex_held(pBt->mutex) );
63309 assert(pBt->autoVacuum);
63310 if( !pBt->incrVacuum ){
63319 ** is either a pointer-map page or the pending-byte page. If one
63325 nFree = get4byte(&pBt->pPage1->aData[36]);
63331 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
63335 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
63336 put4byte(&pBt->pPage1->aData[32], 0);
63337 put4byte(&pBt->pPage1->aData[36], 0);
63338 put4byte(&pBt->pPage1->aData[28], nFin);
63339 pBt->bDoTruncate = 1;
63340 pBt->nPage = nFin;
63356 ** This routine does the first phase of a two-phase commit. This routine
63368 ** This call is a no-op if no write-transaction is currently active on pBt.
63379 ** the write-transaction for this database file is to delete the journal.
63383 if( p->inTrans==TRANS_WRITE ){
63384 BtShared *pBt = p->pBt;
63387 if( pBt->autoVacuum ){
63394 if( pBt->bDoTruncate ){
63395 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
63398 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
63409 BtShared *pBt = p->pBt;
63410 sqlite3 *db = p->db;
63414 pBt->bDoTruncate = 0;
63416 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
63418 ** handle, downgrade to a read-only transaction. The other statements
63421 p->inTrans = TRANS_READ;
63427 if( p->inTrans!=TRANS_NONE ){
63429 pBt->nTransaction--;
63430 if( 0==pBt->nTransaction ){
63431 pBt->inTransaction = TRANS_NONE;
63437 p->inTrans = TRANS_NONE;
63447 ** This routine implements the second phase of a 2-phase commit. The
63459 ** is non-zero then this b-tree transaction is part of a multi-file
63463 ** reset the b-tree objects internal state to indicate that the write
63472 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
63476 /* If the handle has a write-transaction open, commit the shared-btrees
63479 if( p->inTrans==TRANS_WRITE ){
63481 BtShared *pBt = p->pBt;
63482 assert( pBt->inTransaction==TRANS_WRITE );
63483 assert( pBt->nTransaction>0 );
63484 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
63489 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
63490 pBt->inTransaction = TRANS_READ;
63524 ** flag is true, then only write-cursors need be tripped - read-only
63528 ** rolled back modified the database schema. In this case b-tree root
63533 ** saving the current position of a read-only cursor, all cursors,
63534 ** including all read-cursors are tripped.
63546 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63548 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
63549 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
63558 p->eState = CURSOR_FAULT;
63559 p->skipNext = errCode;
63561 for(i=0; i<=p->iPage; i++){
63562 releasePage(p->apPage[i]);
63563 p->apPage[i] = 0;
63584 BtShared *pBt = p->pBt;
63603 if( p->inTrans==TRANS_WRITE ){
63606 assert( TRANS_WRITE==pBt->inTransaction );
63607 rc2 = sqlite3PagerRollback(pBt->pPager);
63612 /* The rollback may have destroyed the pPage1->aData value. So
63614 ** sure pPage1->aData is set correctly. */
63616 int nPage = get4byte(28+(u8*)pPage1->aData);
63618 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
63619 testcase( pBt->nPage!=nPage );
63620 pBt->nPage = nPage;
63624 pBt->inTransaction = TRANS_READ;
63644 ** A statement sub-transaction is implemented as an anonymous savepoint. The
63646 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
63647 ** are no active savepoints and no other statement-transactions open,
63653 BtShared *pBt = p->pBt;
63655 assert( p->inTrans==TRANS_WRITE );
63656 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
63658 assert( iStatement>p->db->nSavepoint );
63659 assert( pBt->inTransaction==TRANS_WRITE );
63665 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
63677 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
63684 if( p && p->inTrans==TRANS_WRITE ){
63685 BtShared *pBt = p->pBt;
63687 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
63693 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
63696 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
63697 pBt->nPage = 0;
63700 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
63705 assert( pBt->nPage>0 );
63714 ** iTable. If a read-only cursor is requested, it is assumed that
63715 ** the caller already has at least a read-only transaction open
63716 ** on the database already. If a write-cursor is requested, then
63733 ** 3: The database must be writable (not on read-only media)
63744 ** operations on this cursor can be no-ops and all READ operations can
63745 ** return a null row (2-bytes: 0x01 0x00).
63748 ** root page of a b-tree. If it is not, then the cursor acquired
63757 int wrFlag, /* 1 to write. 0 read-only */
63761 BtShared *pBt = p->pBt; /* Shared b-tree handle */
63771 ** b-tree database, the connection is holding the required table locks,
63778 assert( p->inTrans>TRANS_NONE );
63779 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
63780 assert( pBt->pPage1 && pBt->pPage1->aData );
63781 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
63785 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
63794 pCur->pgnoRoot = (Pgno)iTable;
63795 pCur->iPage = -1;
63796 pCur->pKeyInfo = pKeyInfo;
63797 pCur->pBtree = p;
63798 pCur->pBt = pBt;
63799 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
63800 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
63803 for(pX=pBt->pCursor; pX; pX=pX->pNext){
63804 if( pX->pgnoRoot==(Pgno)iTable ){
63805 pX->curFlags |= BTCF_Multiple;
63806 pCur->curFlags |= BTCF_Multiple;
63809 pCur->pNext = pBt->pCursor;
63810 pBt->pCursor = pCur;
63811 pCur->eState = CURSOR_INVALID;
63817 int wrFlag, /* 1 to write. 0 read-only */
63837 ** to users so they cannot do the sizeof() themselves - they must call
63850 ** of run-time by skipping the initialization of those elements.
63861 Btree *pBtree = pCur->pBtree;
63864 BtShared *pBt = pCur->pBt;
63867 assert( pBt->pCursor!=0 );
63868 if( pBt->pCursor==pCur ){
63869 pBt->pCursor = pCur->pNext;
63871 BtCursor *pPrev = pBt->pCursor;
63873 if( pPrev->pNext==pCur ){
63874 pPrev->pNext = pCur->pNext;
63877 pPrev = pPrev->pNext;
63880 for(i=0; i<=pCur->iPage; i++){
63881 releasePage(pCur->apPage[i]);
63884 sqlite3_free(pCur->aOverflow);
63902 int iPage = pCur->iPage;
63904 btreeParseCell(pCur->apPage[iPage], pCur->ix, &info);
63905 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
63911 if( pCur->info.nSize==0 ){
63912 int iPage = pCur->iPage;
63913 pCur->curFlags |= BTCF_ValidNKey;
63914 btreeParseCell(pCur->apPage[iPage],pCur->ix,&pCur->info);
63923 ** that is currently pointing to a row in a (non-empty) table.
63927 return pCur && pCur->eState==CURSOR_VALID;
63932 return pCur->eState==CURSOR_VALID;
63943 assert( pCur->eState==CURSOR_VALID );
63944 assert( pCur->curIntKey );
63946 return pCur->info.nKey;
63954 ** The caller must guarantee that the cursor is pointing to a non-NULL
63960 assert( pCur->eState==CURSOR_VALID );
63962 return pCur->info.nPayload;
63968 ** linked list of overflow pages. If possible, it uses the auto-vacuum
63969 ** pointer-map data instead of reading the content of page ovfl to do so.
63981 ** the pointer-map was used to obtain the value for *pPgnoNext), then
63994 assert( sqlite3_mutex_held(pBt->mutex) );
63999 ** autovacuum pointer-map pages. Guess that the next page in
64004 if( pBt->autoVacuum ){
64028 next = get4byte(pPage->aData);
64056 int eOp, /* 0 -> copy from page, 1 -> copy to page */
64089 ** the overflow page-list cache array (BtCursor.aOverflow).
64093 ** Once an overflow page-list cache has been allocated, it must be
64095 ** the cursor is moved to a different row. Additionally, in auto-vacuum
64096 ** mode, the following events may invalidate an overflow page-list cache.
64107 int eOp /* zero to read. non-zero to write. */
64112 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
64113 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
64120 assert( pCur->eState==CURSOR_VALID );
64121 assert( pCur->ix<pPage->nCell );
64125 aPayload = pCur->info.pPayload;
64126 assert( offset+amt <= pCur->info.nPayload );
64128 assert( aPayload > pPage->aData );
64129 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
64132 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
64135 return SQLITE_CORRUPT_PGNO(pPage->pgno);
64139 if( offset<pCur->info.nLocal ){
64141 if( a+offset>pCur->info.nLocal ){
64142 a = pCur->info.nLocal - offset;
64144 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
64147 amt -= a;
64149 offset -= pCur->info.nLocal;
64154 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
64157 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
64166 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
64167 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
64168 if( nOvfl>pCur->nOvflAlloc ){
64170 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
64175 pCur->nOvflAlloc = nOvfl*2;
64176 pCur->aOverflow = aNew;
64179 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
64180 pCur->curFlags |= BTCF_ValidOvfl;
64182 /* If the overflow page-list cache has been allocated and the
64186 if( pCur->aOverflow[offset/ovflSize] ){
64188 nextPage = pCur->aOverflow[iIdx];
64195 /* If required, populate the overflow page-list cache. */
64196 assert( pCur->aOverflow[iIdx]==0
64197 || pCur->aOverflow[iIdx]==nextPage
64199 pCur->aOverflow[iIdx] = nextPage;
64205 ** page-list cache, if any, then fall back to the getOverflowPage()
64208 assert( pCur->curFlags & BTCF_ValidOvfl );
64209 assert( pCur->pBtree->db==pBt->db );
64210 if( pCur->aOverflow[iIdx+1] ){
64211 nextPage = pCur->aOverflow[iIdx+1];
64215 offset -= ovflSize;
64225 a = ovflSize - offset;
64233 ** 3) there is no open write-transaction, and
64234 ** 4) the database is file-backed, and
64235 ** 5) the page is not in the WAL file
64239 ** output buffer, bypassing the page-cache altogether. This speeds
64244 && pBt->inTransaction==TRANS_READ /* (3) */
64245 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
64246 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
64247 && &pBuf[-4]>=pBufStart /* (6) */
64250 u8 *aWrite = &pBuf[-4];
64253 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
64261 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
64272 amt -= a;
64283 return SQLITE_CORRUPT_PGNO(pPage->pgno);
64293 ** pCur can be pointing to either a table or an index b-tree.
64295 ** pCur is pointing to an index b-tree then the key section is read.
64307 assert( pCur->eState==CURSOR_VALID );
64308 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
64309 assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
64326 if ( pCur->eState==CURSOR_INVALID ){
64334 if( pCur->eState==CURSOR_VALID ){
64346 ** the key if index btrees (pPage->intKey==0) and is the data for
64347 ** table btrees (pPage->intKey==1). The number of bytes of available
64367 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
64368 assert( pCur->eState==CURSOR_VALID );
64369 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64371 assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
64372 assert( pCur->info.nSize>0 );
64373 assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
64374 assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
64375 amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
64376 if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
64378 return (void*)pCur->info.pPayload;
64385 ** b-tree page. Write the number of available bytes into *pAmt.
64405 ** This function returns SQLITE_CORRUPT if the page-header flags field of
64407 ** if an intkey page appears to be the parent of a non-intkey page, or
64408 ** vice-versa).
64411 BtShared *pBt = pCur->pBt;
64414 assert( pCur->eState==CURSOR_VALID );
64415 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
64416 assert( pCur->iPage>=0 );
64417 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
64420 pCur->info.nSize = 0;
64421 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
64422 pCur->aiIdx[pCur->iPage++] = pCur->ix;
64423 pCur->ix = 0;
64424 return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
64425 pCur, pCur->curPagerFlags);
64430 ** Page pParent is an internal (non-leaf) tree page. This function
64431 ** asserts that page number iChild is the left-child if the iIdx'th
64433 ** cells in pParent, that page number iChild is the right-child of
64439 assert( iIdx<=pParent->nCell );
64440 if( iIdx==pParent->nCell ){
64441 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
64453 ** pCur->idx is set to the cell index that contains the pointer
64455 ** right-most child page then pCur->idx is set to one more than
64460 assert( pCur->eState==CURSOR_VALID );
64461 assert( pCur->iPage>0 );
64462 assert( pCur->apPage[pCur->iPage] );
64464 pCur->apPage[pCur->iPage-1],
64465 pCur->aiIdx[pCur->iPage-1],
64466 pCur->apPage[pCur->iPage]->pgno
64468 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
64469 pCur->info.nSize = 0;
64470 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
64471 pCur->ix = pCur->aiIdx[pCur->iPage-1];
64472 releasePageNotNull(pCur->apPage[pCur->iPage--]);
64476 ** Move the cursor to point to the root page of its b-tree structure.
64483 ** If the b-tree structure is empty, the cursor state is set to
64489 ** page-header flags indicate that the [virtual] root-page is the expected
64490 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
64492 ** indicating a table b-tree, or if the caller did specify a KeyInfo
64494 ** b-tree).
64504 if( pCur->eState>=CURSOR_REQUIRESEEK ){
64505 if( pCur->eState==CURSOR_FAULT ){
64506 assert( pCur->skipNext!=SQLITE_OK );
64507 return pCur->skipNext;
64512 if( pCur->iPage>=0 ){
64513 if( pCur->iPage ){
64515 assert( pCur->apPage[pCur->iPage]!=0 );
64516 releasePageNotNull(pCur->apPage[pCur->iPage--]);
64517 }while( pCur->iPage);
64520 }else if( pCur->pgnoRoot==0 ){
64521 pCur->eState = CURSOR_INVALID;
64524 assert( pCur->iPage==(-1) );
64525 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
64526 0, pCur->curPagerFlags);
64528 pCur->eState = CURSOR_INVALID;
64531 pCur->iPage = 0;
64532 pCur->curIntKey = pCur->apPage[0]->intKey;
64534 pRoot = pCur->apPage[0];
64535 assert( pRoot->pgno==pCur->pgnoRoot );
64537 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
64538 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
64539 ** NULL, the caller expects a table b-tree. If this is not the case,
64544 ** if pCur->iPage>=0). But this is not so if the database is corrupted
64545 ** in such a way that page pRoot is linked into a second b-tree table
64547 assert( pRoot->intKey==1 || pRoot->intKey==0 );
64548 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
64549 return SQLITE_CORRUPT_PGNO(pCur->apPage[pCur->iPage]->pgno);
64553 pCur->ix = 0;
64554 pCur->info.nSize = 0;
64555 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
64557 pRoot = pCur->apPage[0];
64558 if( pRoot->nCell>0 ){
64559 pCur->eState = CURSOR_VALID;
64560 }else if( !pRoot->leaf ){
64562 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
64563 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
64564 pCur->eState = CURSOR_VALID;
64567 pCur->eState = CURSOR_INVALID;
64573 ** Move the cursor down to the left-most leaf entry beneath the
64576 ** The left-most leaf is the one with the smallest key - the first
64585 assert( pCur->eState==CURSOR_VALID );
64586 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
64587 assert( pCur->ix<pPage->nCell );
64588 pgno = get4byte(findCell(pPage, pCur->ix));
64595 ** Move the cursor down to the right-most leaf entry beneath the
64598 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
64599 ** finds the right-most entry beneath the *page*.
64601 ** The right-most entry is the one with the largest key - the last
64610 assert( pCur->eState==CURSOR_VALID );
64611 while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
64612 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
64613 pCur->ix = pPage->nCell;
64617 pCur->ix = pPage->nCell-1;
64618 assert( pCur->info.nSize==0 );
64619 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
64631 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64634 if( pCur->eState==CURSOR_INVALID ){
64635 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
64638 assert( pCur->apPage[pCur->iPage]->nCell>0 );
64654 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64656 /* If the cursor already points to the last entry, this is a no-op. */
64657 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
64660 ** to the last entry in the b-tree. */
64662 for(ii=0; ii<pCur->iPage; ii++){
64663 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
64665 assert( pCur->ix==pCur->apPage[pCur->iPage]->nCell-1 );
64666 assert( pCur->apPage[pCur->iPage]->leaf );
64673 if( CURSOR_INVALID==pCur->eState ){
64674 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
64677 assert( pCur->eState==CURSOR_VALID );
64681 pCur->curFlags |= BTCF_AtLast;
64683 pCur->curFlags &= ~BTCF_AtLast;
64718 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
64732 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64734 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
64735 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
64740 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
64742 if( pCur->info.nKey==intKey ){
64746 if( pCur->info.nKey<intKey ){
64747 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
64748 *pRes = -1;
64755 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
64760 if( pCur->info.nKey==intKey ){
64774 pIdxKey->errCode = 0;
64775 assert( pIdxKey->default_rc==1
64776 || pIdxKey->default_rc==0
64777 || pIdxKey->default_rc==-1
64787 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
64788 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
64789 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
64790 if( pCur->eState==CURSOR_INVALID ){
64791 *pRes = -1;
64792 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
64795 assert( pCur->apPage[0]->intKey==pCur->curIntKey );
64796 assert( pCur->curIntKey || pIdxKey );
64800 MemPage *pPage = pCur->apPage[pCur->iPage];
64803 /* pPage->nCell must be greater than zero. If this is the root-page
64805 ** not run. If this is not the root-page, then the moveToChild() routine
64807 ** be the right kind (index or table) of b-tree page. Otherwise
64809 assert( pPage->nCell>0 );
64810 assert( pPage->intKey==(pIdxKey==0) );
64812 upr = pPage->nCell-1;
64814 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
64815 pCur->ix = (u16)idx;
64820 if( pPage->intKeyLeaf ){
64822 if( pCell>=pPage->aDataEnd ){
64823 return SQLITE_CORRUPT_PGNO(pPage->pgno);
64830 if( lwr>upr ){ c = -1; break; }
64832 upr = idx-1;
64836 pCur->ix = (u16)idx;
64837 if( !pPage->leaf ){
64841 pCur->curFlags |= BTCF_ValidNKey;
64842 pCur->info.nKey = nCellKey;
64843 pCur->info.nSize = 0;
64856 /* The maximum supported page-size is 65536 bytes. This means that
64857 ** the maximum number of record bytes stored on an index B-Tree
64858 ** page is less than 16384 bytes and may be stored as a 2-byte
64861 ** stored entirely within the b-tree page by inspecting the first
64865 if( nCell<=pPage->max1bytePayload ){
64866 /* This branch runs if the record-size field of the cell is a
64868 ** b-tree page. */
64869 testcase( pCell+nCell+1==pPage->aDataEnd );
64872 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
64874 /* The record-size field is a 2 byte varint and the record
64875 ** fits entirely on the main b-tree page. */
64876 testcase( pCell+nCell+2==pPage->aDataEnd );
64889 u8 * const pCellBody = pCell - pPage->childPtrSize;
64890 pPage->xParseCell(pPage, pCellBody, &pCur->info);
64891 nCell = (int)pCur->info.nKey;
64897 rc = SQLITE_CORRUPT_PGNO(pPage->pgno);
64905 pCur->ix = (u16)idx;
64907 pCur->curFlags &= ~BTCF_ValidOvfl;
64916 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
64917 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
64922 upr = idx-1;
64927 pCur->ix = (u16)idx;
64928 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
64936 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
64937 assert( pPage->isInit );
64938 if( pPage->leaf ){
64939 assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
64940 pCur->ix = (u16)idx;
64946 if( lwr>=pPage->nCell ){
64947 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
64951 pCur->ix = (u16)lwr;
64956 pCur->info.nSize = 0;
64957 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
64974 return (CURSOR_VALID!=pCur->eState);
64987 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64992 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
64993 if( NEVER(pCur->apPage[pCur->iPage]->leaf==0) ) return -1;
64995 for(n=1, i=0; i<=pCur->iPage; i++){
64996 n *= pCur->apPage[i]->nCell;
65027 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
65028 if( pCur->eState!=CURSOR_VALID ){
65029 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
65034 if( CURSOR_INVALID==pCur->eState ){
65037 if( pCur->skipNext ){
65038 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
65039 pCur->eState = CURSOR_VALID;
65040 if( pCur->skipNext>0 ){
65041 pCur->skipNext = 0;
65044 pCur->skipNext = 0;
65048 pPage = pCur->apPage[pCur->iPage];
65049 idx = ++pCur->ix;
65050 assert( pPage->isInit );
65056 ** page into more than one b-tree structure. */
65057 testcase( idx>pPage->nCell );
65059 if( idx>=pPage->nCell ){
65060 if( !pPage->leaf ){
65061 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
65066 if( pCur->iPage==0 ){
65067 pCur->eState = CURSOR_INVALID;
65071 pPage = pCur->apPage[pCur->iPage];
65072 }while( pCur->ix>=pPage->nCell );
65073 if( pPage->intKey ){
65079 if( pPage->leaf ){
65090 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
65091 pCur->info.nSize = 0;
65092 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
65093 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
65094 pPage = pCur->apPage[pCur->iPage];
65095 if( (++pCur->ix)>=pPage->nCell ){
65096 pCur->ix--;
65099 if( pPage->leaf ){
65131 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
65132 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
65133 assert( pCur->info.nSize==0 );
65134 if( pCur->eState!=CURSOR_VALID ){
65139 if( CURSOR_INVALID==pCur->eState ){
65142 if( pCur->skipNext ){
65143 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
65144 pCur->eState = CURSOR_VALID;
65145 if( pCur->skipNext<0 ){
65146 pCur->skipNext = 0;
65149 pCur->skipNext = 0;
65153 pPage = pCur->apPage[pCur->iPage];
65154 assert( pPage->isInit );
65155 if( !pPage->leaf ){
65156 int idx = pCur->ix;
65161 while( pCur->ix==0 ){
65162 if( pCur->iPage==0 ){
65163 pCur->eState = CURSOR_INVALID;
65168 assert( pCur->info.nSize==0 );
65169 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
65171 pCur->ix--;
65172 pPage = pCur->apPage[pCur->iPage];
65173 if( pPage->intKey && !pPage->leaf ){
65184 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
65186 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
65187 pCur->info.nSize = 0;
65188 if( pCur->eState!=CURSOR_VALID
65189 || pCur->ix==0
65190 || pCur->apPage[pCur->iPage]->leaf==0
65194 pCur->ix--;
65215 ** anywhere on the free-list, then it is guaranteed to be returned. If
65235 assert( sqlite3_mutex_held(pBt->mutex) );
65236 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
65237 pPage1 = pBt->pPage1;
65239 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
65241 n = get4byte(&pPage1->aData[36]);
65242 testcase( n==mxPage-1 );
65249 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
65252 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
65253 ** shows that the page 'nearby' is somewhere on the free-list, then
65254 ** the entire-list will be searched for that page.
65261 assert( pBt->autoVacuum );
65273 /* Decrement the free-list count by 1. Set iTrunk to the index of the
65274 ** first free-list trunk page. iPrevTrunk is initially 1.
65276 rc = sqlite3PagerWrite(pPage1->pDbPage);
65278 put4byte(&pPage1->aData[36], n-1);
65281 ** is not true. Otherwise, it runs once for each trunk-page on the
65282 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
65288 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
65291 iTrunk = get4byte(&pPrevTrunk->aData[0]);
65293 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
65296 iTrunk = get4byte(&pPage1->aData[32]);
65300 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
65309 assert( pTrunk->aData!=0 );
65310 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
65312 k = get4byte(&pTrunk->aData[4]);
65318 rc = sqlite3PagerWrite(pTrunk->pDbPage);
65323 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
65326 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
65327 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
65341 rc = sqlite3PagerWrite(pTrunk->pDbPage);
65347 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
65349 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
65353 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
65357 ** pointers to free-list leaves. The first leaf becomes a trunk
65361 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
65371 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
65376 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
65377 put4byte(&pNewTrunk->aData[4], k-1);
65378 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
65381 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
65382 put4byte(&pPage1->aData[32], iNewTrunk);
65384 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
65388 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
65392 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
65398 unsigned char *aData = pTrunk->aData;
65412 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
65414 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
65439 *pPgno, closest+1, k, pTrunk->pgno, n-1));
65440 rc = sqlite3PagerWrite(pTrunk->pDbPage);
65442 if( closest<k-1 ){
65445 put4byte(&aData[4], k-1);
65449 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
65466 ** pager layer with the 'no-content' flag set. This prevents the pager
65468 ** current transaction has already run one or more incremental-vacuum
65471 ** not set the no-content flag. This causes the pager to load and journal
65476 ** file on disk. So the effects of disabling the no-content optimization
65480 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
65482 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
65484 pBt->nPage++;
65485 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
65488 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
65489 /* If *pPgno refers to a pointer-map page, allocate two new pages
65491 ** becomes a new pointer-map page, the second is used by the caller.
65494 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
65495 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
65496 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
65498 rc = sqlite3PagerWrite(pPg->pDbPage);
65502 pBt->nPage++;
65503 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
65506 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
65507 *pPgno = pBt->nPage;
65512 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
65525 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
65526 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
65531 ** This function is used to add page iPage to the database file free-list.
65532 ** It is assumed that the page is not already a part of the free-list.
65543 MemPage *pTrunk = 0; /* Free-list trunk page */
65544 Pgno iTrunk = 0; /* Page number of free-list trunk page */
65545 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
65548 int nFree; /* Initial number of pages on free-list */
65550 assert( sqlite3_mutex_held(pBt->mutex) );
65552 assert( !pMemPage || pMemPage->pgno==iPage );
65557 sqlite3PagerRef(pPage->pDbPage);
65563 rc = sqlite3PagerWrite(pPage1->pDbPage);
65565 nFree = get4byte(&pPage1->aData[36]);
65566 put4byte(&pPage1->aData[36], nFree+1);
65568 if( pBt->btsFlags & BTS_SECURE_DELETE ){
65573 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
65577 memset(pPage->aData, 0, pPage->pBt->pageSize);
65580 /* If the database supports auto-vacuum, write an entry in the pointer-map
65588 /* Now manipulate the actual database free-list structure. There are two
65589 ** possibilities. If the free-list is currently empty, or if the first
65590 ** trunk page in the free-list is full, then this page will become a
65591 ** new free-list trunk page. Otherwise, it will become a leaf of the
65592 ** first trunk page in the current free-list. This block tests if it
65593 ** is possible to add the page as a new free-list leaf.
65598 iTrunk = get4byte(&pPage1->aData[32]);
65604 nLeaf = get4byte(&pTrunk->aData[4]);
65605 assert( pBt->usableSize>32 );
65606 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
65610 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
65615 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
65618 ** usableSize/4 - 8 entries will be reported as corrupt. In order
65620 ** we will continue to restrict the number of entries to usableSize/4 - 8
65623 ** to read "usableSize/4-2" instead of "usableSize/4-8".
65625 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
65630 rc = sqlite3PagerWrite(pTrunk->pDbPage);
65632 put4byte(&pTrunk->aData[4], nLeaf+1);
65633 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
65634 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
65635 sqlite3PagerDontWrite(pPage->pDbPage);
65639 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
65645 ** the page being freed as a leaf page of the first trunk in the free-list.
65646 ** Possibly because the free-list is empty, or possibly because the
65647 ** first trunk in the free-list is full. Either way, the page being freed
65648 ** will become the new first trunk page in the free-list.
65653 rc = sqlite3PagerWrite(pPage->pDbPage);
65657 put4byte(pPage->aData, iTrunk);
65658 put4byte(&pPage->aData[4], 0);
65659 put4byte(&pPage1->aData[32], iPage);
65660 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
65664 pPage->isInit = 0;
65672 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
65686 BtShared *pBt = pPage->pBt;
65692 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65693 pPage->xParseCell(pPage, pCell, pInfo);
65694 if( pInfo->nLocal==pInfo->nPayload ){
65697 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
65699 return SQLITE_CORRUPT_PGNO(pPage->pgno);
65701 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
65702 assert( pBt->usableSize > 4 );
65703 ovflPageSize = pBt->usableSize - 4;
65704 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
65706 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
65708 while( nOvfl-- ){
65723 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
65730 ** freePage2() may zero the page contents if secure-delete mode is
65741 sqlite3PagerUnref(pOvfl->pDbPage);
65756 ** Note that pCell does not necessary need to point to the pPage->aData
65758 ** be constructed in this temporary area then copied into pPage->aData
65775 BtShared *pBt = pPage->pBt;
65779 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65783 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
65784 || sqlite3PagerIswriteable(pPage->pDbPage) );
65787 nHeader = pPage->childPtrSize;
65788 if( pPage->intKey ){
65789 nPayload = pX->nData + pX->nZero;
65790 pSrc = pX->pData;
65791 nSrc = pX->nData;
65792 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
65794 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
65796 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
65797 nSrc = nPayload = (int)pX->nKey;
65798 pSrc = pX->pKey;
65803 if( nPayload<=pPage->maxLocal ){
65812 int mn = pPage->minLocal;
65813 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
65814 testcase( n==pPage->maxLocal );
65815 testcase( n==pPage->maxLocal+1 );
65816 if( n > pPage->maxLocal ) n = mn;
65838 pPage->xParseCell(pPage, pCell, &info);
65839 assert( nHeader==(int)(info.pPayload - pCell) );
65840 assert( info.nKey==pX->nKey );
65850 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
65851 if( pBt->autoVacuum ){
65861 /* If the database supports auto-vacuum, and the second or subsequent
65862 ** overflow page is being allocated, add an entry to the pointer-map
65866 ** to the pointer-map. If we write nothing to this pointer-map slot,
65871 if( pBt->autoVacuum && rc==SQLITE_OK ){
65886 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
65890 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
65891 || sqlite3PagerIswriteable(pPage->pDbPage) );
65896 pPrior = pOvfl->aData;
65898 pPayload = &pOvfl->aData[4];
65899 spaceLeft = pBt->usableSize - 4;
65906 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
65910 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
65911 || sqlite3PagerIswriteable(pPage->pDbPage) );
65920 nPayload -= n;
65923 nSrc -= n;
65924 spaceLeft -= n;
65931 ** Remove the i-th cell from pPage. This routine effects pPage only.
65940 u8 *data; /* pPage->aData */
65946 assert( idx>=0 && idx<pPage->nCell );
65948 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
65949 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65950 data = pPage->aData;
65951 ptr = &pPage->aCellIdx[2*idx];
65953 hdr = pPage->hdrOffset;
65954 testcase( pc==get2byte(&data[hdr+5]) );
65955 testcase( pc+sz==pPage->pBt->usableSize );
65956 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
65965 pPage->nCell--;
65966 if( pPage->nCell==0 ){
65969 put2byte(&data[hdr+5], pPage->pBt->usableSize);
65970 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
65971 - pPage->childPtrSize - 8;
65973 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
65974 put2byte(&data[hdr+3], pPage->nCell);
65975 pPage->nFree += 2;
65986 ** in pPage->apOvfl[] and make it point to the cell content (either
65988 ** Allocating a new entry in pPage->aCell[] implies that
65989 ** pPage->nOverflow is incremented.
65995 int i, /* New cell becomes the i-th cell of the page */
65999 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
66005 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
66008 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
66009 assert( MX_CELL(pPage->pBt)<=10921 );
66010 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
66011 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
66012 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
66013 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66017 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
66019 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
66020 if( pPage->nOverflow || sz+2>pPage->nFree ){
66028 j = pPage->nOverflow++;
66029 /* Comparison against ArraySize-1 since we hold back one extra slot
66032 assert( j < ArraySize(pPage->apOvfl)-1 );
66033 pPage->apOvfl[j] = pCell;
66034 pPage->aiOvfl[j] = (u16)i;
66041 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
66042 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
66044 int rc = sqlite3PagerWrite(pPage->pDbPage);
66049 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66050 data = pPage->aData;
66051 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
66057 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
66058 assert( idx+sz <= (int)pPage->pBt->usableSize );
66059 pPage->nFree -= (u16)(2 + sz);
66064 pIns = pPage->aCellIdx + i*2;
66065 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
66067 pPage->nCell++;
66069 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
66070 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
66072 if( pPage->pBt->autoVacuum ){
66095 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
66099 assert( idx>=0 && idx+N<=p->nCell );
66101 assert( p->apCell[idx]!=0 );
66102 if( p->szCell[idx]==0 ){
66103 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
66106 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
66109 N--;
66117 assert( N>=0 && N<p->nCell );
66118 assert( p->szCell[N]==0 );
66119 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
66120 return p->szCell[N];
66123 assert( N>=0 && N<p->nCell );
66124 if( p->szCell[N] ) return p->szCell[N];
66129 ** Array apCell[] contains pointers to nCell b-tree page cells. The
66147 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
66148 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
66149 const int usableSize = pPg->pBt->usableSize;
66152 u8 *pCellptr = pPg->aCellIdx;
66153 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
66156 i = get2byte(&aData[hdr+5]);
66157 memcpy(&pTmp[i], &aData[i], usableSize - i);
66163 pCell = &pTmp[pCell - aData];
66165 pData -= szCell[i];
66166 put2byte(pCellptr, (pData - aData));
66170 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
66171 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
66174 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
66175 pPg->nCell = nCell;
66176 pPg->nOverflow = 0;
66179 put2byte(&aData[hdr+3], pPg->nCell);
66180 put2byte(&aData[hdr+5], pData - aData);
66186 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
66189 ** the page needs to be defragmented before the cells will fit), non-zero
66193 ** Argument pCellptr points to the first entry in the cell-pointer array
66195 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
66197 ** that it is safe to overwrite this part of the cell-pointer array.
66205 ** end of the space required by this page for the cell-pointer area (for
66206 ** all cells - not just those inserted by the current call). If the content
66208 ** cells in apCell[], then the cells do not fit and non-zero is returned.
66212 u8 *pBegin, /* End of cell-pointer array */
66213 u8 **ppData, /* IN/OUT: Page content -area pointer */
66214 u8 *pCellptr, /* Pointer to cell-pointer area */
66220 u8 *aData = pPg->aData;
66223 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
66229 if( (pData - pBegin)<sz ) return 1;
66230 pData -= sz;
66233 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
66236 assert( (pSlot+sz)<=pCArray->apCell[i]
66237 || pSlot>=(pCArray->apCell[i]+sz)
66239 memmove(pSlot, pCArray->apCell[i], sz);
66240 put2byte(pCellptr, (pSlot - aData));
66248 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
66251 ** within the body of pPg to the pPg free-list. The cell-pointers and other
66254 ** This function returns the total number of cells added to the free-list.
66262 u8 * const aData = pPg->aData;
66263 u8 * const pEnd = &aData[pPg->pBt->usableSize];
66264 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
66272 u8 *pCell = pCArray->apCell[i];
66278 sz = pCArray->szCell[i]; assert( sz>0 );
66281 assert( pFree>aData && (pFree - aData)<65536 );
66282 freeSpace(pPg, (u16)(pFree - aData), szFree);
66295 assert( pFree>aData && (pFree - aData)<65536 );
66296 freeSpace(pPg, (u16)(pFree - aData), szFree);
66303 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting
66310 ** The pPg->nFree field is invalid when this function returns. It is the
66320 u8 * const aData = pPg->aData;
66321 const int hdr = pPg->hdrOffset;
66322 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
66323 int nCell = pPg->nCell; /* Cells stored on pPg */
66327 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
66331 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
66332 memcpy(pTmp, aData, pPg->pBt->usableSize);
66337 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
66338 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
66339 nCell -= nShift;
66342 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
66345 pData = &aData[get2byteNotZero(&aData[hdr+5])];
66350 int nAdd = MIN(nNew,iOld-iNew);
66351 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
66352 pCellptr = pPg->aCellIdx;
66362 for(i=0; i<pPg->nOverflow; i++){
66363 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
66365 pCellptr = &pPg->aCellIdx[iCell * 2];
66366 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
66376 pCellptr = &pPg->aCellIdx[nCell*2];
66379 iNew+nCell, nNew-nCell, pCArray
66382 pPg->nCell = nNew;
66383 pPg->nOverflow = 0;
66385 put2byte(&aData[hdr+3], pPg->nCell);
66386 put2byte(&aData[hdr+5], pData - aData);
66390 u8 *pCell = pCArray->apCell[i+iNew];
66391 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
66392 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
66393 pCell = &pTmp[pCell - aData];
66396 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
66404 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
66426 ** a new entry is being inserted on the extreme right-end of the
66430 ** Instead of trying to balance the 3 right-most leaf pages, just add
66431 ** a new page to the right-hand side and put the one new entry in
66437 ** pPage is the leaf page which is the right-most page in the tree.
66439 ** which is also the right-most entry on the page.
66448 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
66453 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66454 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
66455 assert( pPage->nOverflow==1 );
66458 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
66460 /* Allocate a new page. This page will become the right-sibling of
66469 u8 *pCell = pPage->apOvfl[0];
66470 u16 szCell = pPage->xCellSize(pPage, pCell);
66473 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
66474 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
66478 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
66480 /* If this is an auto-vacuum database, update the pointer map
66490 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
66491 if( szCell>pNew->minLocal ){
66497 ** consists of a 4-byte page number (the page number of pPage) and
66501 ** To find the largest key value on pPage, first find the right-most
66503 ** record-length (a variable length integer at most 32-bits in size)
66505 ** The first of the while(...) loops below skips over the record-length
66509 pCell = findCell(pPage, pPage->nCell-1);
66517 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
66518 0, pPage->pgno, &rc);
66521 /* Set the right-child pointer of pParent to point to the new page. */
66522 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
66536 ** for setting pointer-map entries.
66544 BtShared *pBt = pPage->pBt;
66545 assert( pPage->isInit );
66547 for(j=0; j<pPage->nCell; j++){
66552 pPage->xParseCell(pPage, z, &info);
66554 Pgno ovfl = get4byte(&z[info.nSize-4]);
66556 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
66558 if( !pPage->leaf ){
66561 assert( n==pPage->pgno && e==PTRMAP_BTREE );
66564 if( !pPage->leaf ){
66565 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
66567 assert( n==pPage->pgno && e==PTRMAP_BTREE );
66575 ** This function is used to copy the contents of the b-tree node stored
66577 ** the pointer-map entries for each child page are updated so that the
66593 BtShared * const pBt = pFrom->pBt;
66594 u8 * const aFrom = pFrom->aData;
66595 u8 * const aTo = pTo->aData;
66596 int const iFromHdr = pFrom->hdrOffset;
66597 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
66602 assert( pFrom->isInit );
66603 assert( pFrom->nFree>=iToHdr );
66604 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
66606 /* Copy the b-tree node content from page pFrom to page pTo. */
66607 iData = get2byte(&aFrom[iFromHdr+5]);
66608 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
66609 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
66616 pTo->isInit = 0;
66623 /* If this is an auto-vacuum database, update the pointer-map entries
66624 ** for any b-tree or overflow pages that pTo now contains the pointers to.
66666 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
66675 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
66676 int isRoot, /* True if pParent is a root-page */
66684 int nxDiv; /* Next divider slot in pParent->aCell[] */
66689 int pageFlags; /* Value of pPage->aData[0] */
66695 u8 *pRight; /* Location in parent of right-sibling pointer */
66696 u8 *apDiv[NB-1]; /* Divider cells in pParent */
66697 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
66699 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
66711 pBt = pParent->pBt;
66712 assert( sqlite3_mutex_held(pBt->mutex) );
66713 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
66716 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
66724 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
66725 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
66742 i = pParent->nOverflow + pParent->nCell;
66750 nxDiv = i-2+bBulk;
66752 nxDiv = iParentIdx-1;
66754 i = 2-bBulk;
66757 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
66758 pRight = &pParent->aData[pParent->hdrOffset+8];
66760 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
66769 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
66770 if( (i--)==0 ) break;
66772 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
66773 apDiv[i] = pParent->apOvfl[0];
66775 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
66776 pParent->nOverflow = 0;
66778 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
66780 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
66789 ** But not if we are in secure-delete mode. In secure-delete mode,
66794 if( pBt->btsFlags & BTS_FAST_SECURE ){
66797 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
66798 if( (iOff+szNew[i])>(int)pBt->usableSize ){
66804 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
66807 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
66811 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
66821 + pBt->pageSize; /* aSpace1 */
66823 /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
66825 assert( szScratch<=6*(int)pBt->pageSize );
66852 leafCorrection = b.pRef->leaf*4;
66853 leafData = b.pRef->intKeyLeaf;
66856 int limit = pOld->nCell;
66857 u8 *aData = pOld->aData;
66858 u16 maskPage = pOld->maskPage;
66859 u8 *piCell = aData + pOld->cellOffset;
66862 /* Verify that all sibling pages are of the same "type" (table-leaf,
66863 ** table-interior, index-leaf, or index-interior).
66865 if( pOld->aData[0]!=apOld[0]->aData[0] ){
66887 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
66888 if( pOld->nOverflow>0 ){
66889 limit = pOld->aiOvfl[0];
66895 for(k=0; k<pOld->nOverflow; k++){
66896 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
66897 b.apCell[b.nCell] = pOld->apOvfl[k];
66901 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
66910 if( i<nOld-1 && !leafData){
66917 assert( sz<=pBt->maxLocal+23 );
66918 assert( iSpace1 <= (int)pBt->pageSize );
66922 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
66923 if( !pOld->leaf ){
66925 assert( pOld->hdrOffset==0 );
66928 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
66935 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
66947 ** size of all cells on the i-th page and cntNew[] which is the index
66954 ** szNew[i]: Spaced used on the i-th sibling page.
66956 ** the right of the i-th sibling page.
66960 usableSpace = pBt->usableSize - 12 + leafCorrection;
66963 szNew[i] = usableSpace - p->nFree;
66964 for(j=0; j<p->nOverflow; j++){
66965 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
66976 szNew[k-1] = 0;
66977 cntNew[k-1] = b.nCell;
66979 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
66980 szNew[i] -= sz;
66989 cntNew[i]--;
67003 szNew[i+1] -= sz;
67007 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
67016 ** always nearly full, while the right-most sibling might be nearly empty.
67021 ** be so out of balance as to be illegal. For example, the right-most
67024 for(i=k-1; i>0; i--){
67026 int szLeft = szNew[i-1]; /* Size of sibling on the left */
67027 int r; /* Index of right-most cell in left sibling */
67030 r = cntNew[i-1] - 1;
67031 d = r + 1 - leafData;
67038 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
67042 szLeft -= b.szCell[r] + 2;
67043 cntNew[i-1] = r;
67044 r--;
67045 d--;
67048 szNew[i-1] = szLeft;
67049 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
67055 /* Sanity check: For a non-corrupt database file one of the follwing
67062 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
67064 apOld[0]->pgno, apOld[0]->nCell,
67065 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
67066 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
67072 pageFlags = apOld[0]->aData[0];
67078 rc = sqlite3PagerWrite(pNew->pDbPage);
67090 /* Set the pointer-map entry for the new sibling page. */
67092 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
67113 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
67114 aPgFlags[i] = apNew[i]->pDbPage->flags;
67138 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
67140 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
67141 apNew[i]->pgno = pgno;
67147 apNew[0]->pgno, szNew[0], cntNew[0],
67148 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
67149 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
67150 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
67151 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
67152 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
67153 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
67154 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
67155 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
67158 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
67159 put4byte(pRight, apNew[nNew-1]->pgno);
67161 /* If the sibling pages are not leaves, ensure that the right-child pointer
67162 ** of the right-most new sibling page is set to the value that was
67163 ** originally in the same field of the right-most old sibling page. */
67165 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
67166 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
67181 ** associated with the right-child of each sibling may also need to be
67187 u8 *aOld = pNew->aData;
67188 int cntOldNext = pNew->nCell + pNew->nOverflow;
67189 int usableSize = pBt->usableSize;
67197 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
67198 aOld = pOld->aData;
67212 || pNew->pgno!=aPgno[iOld]
67216 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
67218 if( cachedCellSize(&b,i)>pNew->minLocal ){
67227 for(i=0; i<nNew-1; i++){
67239 if( !pNew->leaf ){
67240 memcpy(&pNew->aData[8], pCell, 4);
67242 /* If the tree is a leaf-data tree, and the siblings are leaves,
67244 ** cell consists of the integer key for the right-most cell of
67245 ** the sibling-page assembled above only.
67248 j--;
67249 pNew->xParseCell(pNew, b.apCell[j], &info);
67254 pCell -= 4;
67255 /* Obscure case for non-leaf-data trees: If the cell at pCell was
67262 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
67268 sz = pParent->xCellSize(pParent, pCell);
67272 assert( sz<=pBt->maxLocal+23 );
67273 assert( iOvflSpace <= (int)pBt->pageSize );
67274 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
67276 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
67283 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
67285 ** the left-hand sibling apNew[iPg-1] has been updated.
67289 ** the right-hand sibling apNew[iPg+1] has been updated.
67293 ** The iPg value in the following loop starts at nNew-1 goes down
67294 ** to 0, then back up to nNew-1 again, thus making two passes over
67301 for(i=1-nNew; i<nNew; i++){
67302 int iPg = i<0 ? -i : i;
67306 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
67313 ** only after iPg-1 has already been updated. */
67314 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
67324 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
67325 iNew = cntNew[iPg-1] + !leafData;
67326 nNewCell = cntNew[iPg] - iNew;
67332 apNew[iPg]->nFree = usableSpace-szNew[iPg];
67333 assert( apNew[iPg]->nOverflow==0 );
67334 assert( apNew[iPg]->nCell==nNewCell );
67344 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
67345 /* The root page of the b-tree now contains no cells. The only sibling
67346 ** page is the right-child of the parent. Copy the contents of the
67348 ** b-tree structure by one. This is described as the "balance-shallower"
67349 ** sub-algorithm in some documentation.
67351 ** If this is an auto-vacuum database, the call to copyNodeContent()
67352 ** sets all pointer-map entries corresponding to database image pages
67361 rc = defragmentPage(apNew[0], -1);
67363 assert( apNew[0]->nFree ==
67364 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
67370 /* Fix the pointer map entries associated with the right-child of each
67374 u32 key = get4byte(&apNew[i]->aData[8]);
67375 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
67379 assert( pParent->isInit );
67390 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
67417 ** This function is called when the root page of a b-tree structure is
67422 ** page is then overwritten to make it an empty page with the right-child
67425 ** Before returning, all pointer-map entries corresponding to pages
67426 ** that the new child-page now contains pointers to are updated. The
67427 ** entry corresponding to the new right-child pointer of the root
67439 BtShared *pBt = pRoot->pBt; /* The BTree */
67441 assert( pRoot->nOverflow>0 );
67442 assert( sqlite3_mutex_held(pBt->mutex) );
67444 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
67445 ** page that will become the new right-child of pPage. Copy the contents
67448 rc = sqlite3PagerWrite(pRoot->pDbPage);
67450 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
67453 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
67461 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
67462 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
67463 assert( pChild->nCell==pRoot->nCell );
67465 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
67468 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
67469 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
67470 memcpy(pChild->apOvfl, pRoot->apOvfl,
67471 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
67472 pChild->nOverflow = pRoot->nOverflow;
67474 /* Zero the contents of pRoot. Then install pChild as the right-child. */
67475 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
67476 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
67494 const int nMin = pCur->pBt->usableSize * 2 / 3;
67502 int iPage = pCur->iPage;
67503 MemPage *pPage = pCur->apPage[iPage];
67506 if( pPage->nOverflow ){
67507 /* The root page of the b-tree is overfull. In this case call the
67508 ** balance_deeper() function to create a new child for the root-page
67509 ** and copy the current contents of the root-page to it. The
67510 ** next iteration of the do-loop will balance the child page.
67514 rc = balance_deeper(pPage, &pCur->apPage[1]);
67516 pCur->iPage = 1;
67517 pCur->ix = 0;
67518 pCur->aiIdx[0] = 0;
67519 assert( pCur->apPage[1]->nOverflow );
67524 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
67527 MemPage * const pParent = pCur->apPage[iPage-1];
67528 int const iIdx = pCur->aiIdx[iPage-1];
67530 rc = sqlite3PagerWrite(pParent->pDbPage);
67533 if( pPage->intKeyLeaf
67534 && pPage->nOverflow==1
67535 && pPage->aiOvfl[0]==pPage->nCell
67536 && pParent->pgno!=1
67537 && pParent->nCell==iIdx
67542 ** happens, the next iteration of the do-loop will balance pParent
67561 ** become overfull or underfull. The next iteration of the do-loop
67566 ** A subsequent iteration of the do-loop will deal with this by
67568 ** but it doesn't deal with overflow cells - just moves them to a
67575 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
67577 pCur->hints&BTREE_BULKLOAD);
67593 pPage->nOverflow = 0;
67595 /* The next iteration of the do-loop balances the parent page. */
67597 pCur->iPage--;
67598 assert( pCur->iPage>=0 );
67624 ** If the seekResult parameter is non-zero, then a successful call to
67635 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
67636 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
67646 int loc = seekResult; /* -1: before desired location +1: after */
67650 Btree *p = pCur->pBtree;
67651 BtShared *pBt = p->pBt;
67657 if( pCur->eState==CURSOR_FAULT ){
67658 assert( pCur->skipNext!=SQLITE_OK );
67659 return pCur->skipNext;
67663 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
67664 && pBt->inTransaction==TRANS_WRITE
67665 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
67666 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
67669 ** expecting an index b-tree, then the caller should be inserting blob
67673 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
67677 ** In some cases, the call to btreeMoveto() below is a no-op. For
67678 ** example, when inserting data into a table with auto-generated integer
67681 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
67686 if( pCur->curFlags & BTCF_Multiple ){
67687 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
67691 if( pCur->pKeyInfo==0 ){
67692 assert( pX->pKey==0 );
67693 /* If this is an insert into a table b-tree, invalidate any incrblob
67695 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
67700 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
67705 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
67708 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
67712 if( pX->nMem ){
67714 r.pKeyInfo = pCur->pKeyInfo;
67715 r.aMem = pX->aMem;
67716 r.nField = pX->nMem;
67724 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
67728 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
67730 pPage = pCur->apPage[pCur->iPage];
67731 assert( pPage->intKey || pX->nKey>=0 );
67732 assert( pPage->leaf || !pPage->intKey );
67735 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
67737 assert( pPage->isInit );
67738 newCell = pBt->pTmpSpace;
67742 assert( szNew==pPage->xCellSize(pPage, newCell) );
67744 idx = pCur->ix;
67747 assert( idx<pPage->nCell );
67748 rc = sqlite3PagerWrite(pPage->pDbPage);
67753 if( !pPage->leaf ){
67758 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
67768 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
67770 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
67776 }else if( loc<0 && pPage->nCell>0 ){
67777 assert( pPage->leaf );
67778 idx = ++pCur->ix;
67779 pCur->curFlags &= ~BTCF_ValidNKey;
67781 assert( pPage->leaf );
67784 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
67785 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
67799 ** multiple records into an intkey b-tree using a single cursor (as can
67802 ** the b-tree if possible. If the cursor is left pointing to the last
67807 pCur->info.nSize = 0;
67808 if( pPage->nOverflow ){
67810 pCur->curFlags &= ~(BTCF_ValidNKey);
67817 pCur->apPage[pCur->iPage]->nOverflow = 0;
67818 pCur->eState = CURSOR_INVALID;
67821 if( pCur->pKeyInfo ){
67822 assert( pCur->pKey==0 );
67823 pCur->pKey = sqlite3Malloc( pX->nKey );
67824 if( pCur->pKey==0 ){
67827 memcpy(pCur->pKey, pX->pKey, pX->nKey);
67830 pCur->eState = CURSOR_REQUIRESEEK;
67831 pCur->nKey = pX->nKey;
67834 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
67853 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
67858 Btree *p = pCur->pBtree;
67859 BtShared *pBt = p->pBt;
67870 assert( pBt->inTransaction==TRANS_WRITE );
67871 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
67872 assert( pCur->curFlags & BTCF_WriteFlag );
67873 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
67874 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
67875 assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
67876 assert( pCur->eState==CURSOR_VALID );
67879 iCellDepth = pCur->iPage;
67880 iCellIdx = pCur->ix;
67881 pPage = pCur->apPage[iCellDepth];
67886 ** will cause a b-tree rebalance, then this is done by saving the cursor
67894 if( !pPage->leaf
67895 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
67897 /* A b-tree rebalance will be required after deleting this entry.
67911 ** sub-tree headed by the child page of the cell being deleted. This makes
67913 if( !pPage->leaf ){
67921 if( pCur->curFlags & BTCF_Multiple ){
67922 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
67926 /* If this is a delete operation to remove a row from a table b-tree,
67928 if( pCur->pKeyInfo==0 ){
67929 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
67935 rc = sqlite3PagerWrite(pPage->pDbPage);
67942 ** is currently pointing to the largest entry in the sub-tree headed
67943 ** by the child-page of the cell that was just deleted from an internal
67946 if( !pPage->leaf ){
67947 MemPage *pLeaf = pCur->apPage[pCur->iPage];
67949 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
67952 pCell = findCell(pLeaf, pLeaf->nCell-1);
67953 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
67954 nCell = pLeaf->xCellSize(pLeaf, pCell);
67956 pTmp = pBt->pTmpSpace;
67958 rc = sqlite3PagerWrite(pLeaf->pDbPage);
67960 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
67962 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
67982 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
67983 while( pCur->iPage>iCellDepth ){
67984 releasePage(pCur->apPage[pCur->iPage--]);
67991 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
67992 assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
67993 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
67994 pCur->eState = CURSOR_SKIPNEXT;
67995 if( iCellIdx>=pPage->nCell ){
67996 pCur->skipNext = -1;
67997 pCur->ix = pPage->nCell-1;
67999 pCur->skipNext = 1;
68004 pCur->eState = CURSOR_REQUIRESEEK;
68023 BtShared *pBt = p->pBt;
68027 int ptfFlags; /* Page-type flage for the root page of new table */
68030 assert( pBt->inTransaction==TRANS_WRITE );
68031 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
68039 if( pBt->autoVacuum ){
68040 Pgno pgnoMove; /* Move a page here to make room for the root-page */
68045 ** out to be an overflow page, delete all overflow page-map caches
68051 ** root page of the new table should go. meta[3] is the largest root-page
68052 ** created so far, so the new root-page is (meta[3]+1).
68057 /* The new root-page may not be allocated on a pointer-map page, or the
68077 /* pgnoRoot is the page that will be used for the root-page of
68121 rc = sqlite3PagerWrite(pRoot->pDbPage);
68130 /* Update the pointer-map and meta-data with the new root-page number. */
68141 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
68153 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
68160 sqlite3PagerUnref(pRoot->pDbPage);
68161 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
68190 assert( sqlite3_mutex_held(pBt->mutex) );
68196 if( pPage->bBusy ){
68200 pPage->bBusy = 1;
68201 hdr = pPage->hdrOffset;
68202 for(i=0; i<pPage->nCell; i++){
68204 if( !pPage->leaf ){
68211 if( !pPage->leaf ){
68212 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
68215 assert( pPage->intKey || CORRUPT_DB );
68216 testcase( !pPage->intKey );
68217 *pnChange += pPage->nCell;
68221 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
68222 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
68226 pPage->bBusy = 0;
68246 BtShared *pBt = p->pBt;
68248 assert( p->inTrans==TRANS_WRITE );
68254 ** is the root of a table b-tree - if it is not, the following call is
68255 ** a no-op). */
68269 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
68295 BtShared *pBt = p->pBt;
68298 assert( p->inTrans==TRANS_WRITE );
68315 if( pBt->autoVacuum ){
68320 /* If the table being dropped is the table with the largest root-page
68329 /* The table being dropped does not have the largest root-page
68331 ** gap left by the deleted root-page.
68354 /* Set the new 'max-root-page' value in the database header. This
68356 ** be a root-page number, less one again if that is the
68359 maxRootPgno--;
68362 maxRootPgno--;
68384 ** This function may only be called if the b-tree connection already
68387 ** Read the meta-information out of a database file. Meta[0]
68390 ** is read-only, the others are read/write.
68404 BtShared *pBt = p->pBt;
68407 assert( p->inTrans>TRANS_NONE );
68409 assert( pBt->pPage1 );
68413 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
68415 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
68418 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
68419 ** database, mark the database as read-only. */
68422 pBt->btsFlags |= BTS_READ_ONLY;
68430 ** Write meta-information back into the database. Meta[0] is
68431 ** read-only and may not be written.
68434 BtShared *pBt = p->pBt;
68439 assert( p->inTrans==TRANS_WRITE );
68440 assert( pBt->pPage1!=0 );
68441 pP1 = pBt->pPage1->aData;
68442 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68447 assert( pBt->autoVacuum || iMeta==0 );
68449 pBt->incrVacuum = (u8)iMeta;
68459 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
68460 ** number of entries in the b-tree and write the result to *pnEntry.
68470 if( pCur->pgnoRoot==0 ){
68477 ** page in the B-Tree structure (not including overflow pages).
68481 MemPage *pPage; /* Current page of the b-tree */
68483 /* If this is a leaf page or the tree is not an int-key tree, then
68487 pPage = pCur->apPage[pCur->iPage];
68488 if( pPage->leaf || !pPage->intKey ){
68489 nEntry += pPage->nCell;
68495 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
68497 ** to visit is the right-child of its parent.
68502 if( pPage->leaf ){
68504 if( pCur->iPage==0 ){
68505 /* All pages of the b-tree have been visited. Return successfully. */
68510 }while ( pCur->ix>=pCur->apPage[pCur->iPage]->nCell );
68512 pCur->ix++;
68513 pPage = pCur->apPage[pCur->iPage];
68517 ** points at. This is the right-child if (iIdx==pPage->nCell).
68519 iIdx = pCur->ix;
68520 if( iIdx==pPage->nCell ){
68521 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
68537 return p->pBt->pPager;
68550 if( !pCheck->mxErr ) return;
68551 pCheck->mxErr--;
68552 pCheck->nErr++;
68554 if( pCheck->errMsg.nChar ){
68555 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
68557 if( pCheck->zPfx ){
68558 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
68560 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
68562 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
68563 pCheck->mallocFailed = 1;
68571 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
68575 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
68576 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
68583 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
68584 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
68590 ** reference to the page, add an error message to pCheck->zErrMsg.
68598 if( iPage>pCheck->nPage ){
68612 ** Check that the entry in the pointer-map for page iChild maps to
68626 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
68628 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
68654 while( N-- > 0 && pCheck->mxErr ){
68664 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
68672 if( pCheck->pBt->autoVacuum ){
68676 if( n>(int)pCheck->pBt->usableSize/4-2 ){
68679 N--;
68684 if( pCheck->pBt->autoVacuum ){
68690 N -= n;
68695 /* If this database supports auto-vacuum and iPage is not the last
68696 ** page in this overflow list, check that the pointer-map entry for
68699 if( pCheck->pBt->autoVacuum && N>0 ){
68709 checkAppendMsg(pCheck, "free-page count in header is too small");
68716 ** An implementation of a min-heap.
68726 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
68753 aHeap[0]--;
68779 ** 5. Verify that the depth of all children is the same.
68790 int depth = -1, d2; /* Depth of a subtree */
68806 u32 *heap = 0; /* Min-heap used for checking cell coverage */
68807 u32 x, prev = 0; /* Next and previous entry on the min-heap */
68808 const char *saved_zPfx = pCheck->zPfx;
68809 int saved_v1 = pCheck->v1;
68810 int saved_v2 = pCheck->v2;
68815 pBt = pCheck->pBt;
68816 usableSize = pBt->usableSize;
68819 pCheck->zPfx = "Page %d: ";
68820 pCheck->v1 = iPage;
68829 savedIsInit = pPage->isInit;
68830 pPage->isInit = 0;
68837 data = pPage->aData;
68838 hdr = pPage->hdrOffset;
68841 pCheck->zPfx = "On tree page %d cell %d: ";
68842 contentOffset = get2byteNotZero(&data[hdr+5]);
68845 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68848 assert( pPage->nCell==nCell );
68850 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
68851 ** immediately follows the b-tree page header. */
68852 cellStart = hdr + 12 - 4*pPage->leaf;
68853 assert( pPage->aCellIdx==&data[cellStart] );
68854 pCellIdx = &data[cellStart + 2*(nCell-1)];
68856 if( !pPage->leaf ){
68857 /* Analyze the right-child page of internal pages */
68860 if( pBt->autoVacuum ){
68861 pCheck->zPfx = "On page %d at right child: ";
68870 heap = pCheck->heap;
68874 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
68876 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
68880 pCheck->v2 = i;
68883 pCellIdx -= 2;
68884 if( pc<contentOffset || pc>usableSize-4 ){
68886 pc, contentOffset, usableSize-4);
68891 pPage->xParseCell(pPage, pCell, &info);
68899 if( pPage->intKey ){
68911 assert( pc + info.nSize - 4 <= usableSize );
68912 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
68913 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
68915 if( pBt->autoVacuum ){
68922 if( !pPage->leaf ){
68926 if( pBt->autoVacuum ){
68937 /* Populate the coverage-checking heap for leaf pages */
68938 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
68945 pCheck->zPfx = 0;
68946 if( doCoverageCheck && pCheck->mxErr>0 ){
68947 /* For leaf pages, the min-heap has already been initialized and the
68950 if( !pPage->leaf ){
68951 heap = pCheck->heap;
68953 for(i=nCell-1; i>=0; i--){
68956 size = pPage->xCellSize(pPage, &data[pc]);
68957 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
68960 /* Add the freeblocks to the min-heap
68962 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
68969 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
68972 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
68973 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
68974 ** big-endian integer which is the offset in the b-tree page of the next
68978 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
68981 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
68984 /* Analyze the min-heap looking for overlap between cells and/or
68987 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
68992 ** The loop below pulls entries from the min-heap in order and compares
68998 prev = contentOffset - 1; /* Implied first min-heap entry */
69005 nFrag += (x>>16) - (prev&0xffff) - 1;
69009 nFrag += usableSize - (prev&0xffff) - 1;
69010 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
69011 ** is stored in the fifth field of the b-tree page header.
69012 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
69023 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
69025 pCheck->zPfx = saved_zPfx;
69026 pCheck->v1 = saved_v1;
69027 pCheck->v2 = saved_v2;
69038 ** A read-only or read-write transaction must be opened before calling
69043 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
69055 BtShared *pBt = p->pBt;
69056 int savedDbFlags = pBt->db->flags;
69061 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
69062 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
69065 sCheck.pPager = pBt->pPager;
69086 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
69098 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
69099 get4byte(&pBt->pPage1->aData[36]));
69104 testcase( pBt->db->flags & SQLITE_CellSizeCk );
69105 pBt->db->flags &= ~SQLITE_CellSizeCk;
69110 if( pBt->autoVacuum && aRoot[i]>1 ){
69116 pBt->db->flags = savedDbFlags;
69126 /* If the database supports auto-vacuum, make sure no tables contain
69127 ** references to pointer-map pages.
69130 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
69134 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
69152 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
69160 ** an empty string if the database is in-memory or a TEMP database.
69166 assert( p->pBt->pPager!=0 );
69167 return sqlite3PagerFilename(p->pBt->pPager, 1);
69179 assert( p->pBt->pPager!=0 );
69180 return sqlite3PagerJournalname(p->pBt->pPager);
69184 ** Return non-zero if a transaction is active.
69187 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
69188 return (p && (p->inTrans==TRANS_WRITE));
69196 ** transaction on the shared-cache the argument Btree is connected to.
69203 BtShared *pBt = p->pBt;
69205 if( pBt->inTransaction!=TRANS_NONE ){
69208 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
69217 ** Return non-zero if a read (or write) transaction is active.
69221 assert( sqlite3_mutex_held(p->db->mutex) );
69222 return p->inTrans!=TRANS_NONE;
69227 assert( sqlite3_mutex_held(p->db->mutex) );
69228 return p->nBackup!=0;
69233 ** a single shared-btree. The memory is used by client code for its own
69234 ** purposes (for example, to store a high-level schema associated with
69235 ** the shared-btree). The btree layer manages reference counting issues.
69237 ** The first time this is called on a shared-btree, nBytes bytes of memory
69246 ** Just before the shared-btree is closed, the function passed as the
69252 BtShared *pBt = p->pBt;
69254 if( !pBt->pSchema && nBytes ){
69255 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
69256 pBt->xFreeSchema = xFree;
69259 return pBt->pSchema;
69269 assert( sqlite3_mutex_held(p->db->mutex) );
69286 assert( p->inTrans!=TRANS_NONE );
69287 if( p->sharable ){
69317 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
69318 assert( pCsr->curFlags & BTCF_Incrblob );
69324 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
69325 if( pCsr->eState!=CURSOR_VALID ){
69331 ** version of the b-tree page modified by the accessPayload call below.
69337 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
69343 ** (c) the connection holds a write-lock on the table (if required),
69344 ** (d) there are no conflicting read-locks, and
69347 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
69350 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
69351 && pCsr->pBt->inTransaction==TRANS_WRITE );
69352 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
69353 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
69354 assert( pCsr->apPage[pCsr->iPage]->intKey );
69363 pCur->curFlags |= BTCF_Incrblob;
69364 pCur->pBtree->hasIncrblobCur = 1;
69374 BtShared *pBt = pBtree->pBt;
69382 pBt->btsFlags &= ~BTS_NO_WAL;
69383 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
69387 u8 *aData = pBt->pPage1->aData;
69391 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
69400 pBt->btsFlags &= ~BTS_NO_WAL;
69409 return (pCsr->hints & mask)!=0;
69413 ** Return true if the given Btree is read-only.
69416 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
69429 return p->sharable;
69438 testcase( p->sharable );
69439 return p->pBt->nRef;
69467 Btree *pDest; /* Destination b-tree file */
69469 int bDestLocked; /* True once a write-transaction is open on pDest */
69473 Btree *pSrc; /* Source b-tree file */
69491 ** structure may be accessed via two groups of thread-safe entry points:
69505 ** backup_pagecount() are not thread-safe functions. If they are called
69513 ** Non-sharable Btrees (in-memory databases for example), do not have
69550 return pDb->aDb[i].pBt;
69559 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
69564 ** Check that there is no open read-transaction on the b-tree passed as the
69566 ** is an open read-transaction, return SQLITE_ERROR and leave an error
69608 sqlite3_mutex_enter(pSrcDb->mutex);
69609 sqlite3_mutex_enter(pDestDb->mutex);
69618 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
69629 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
69630 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
69631 p->pDestDb = pDestDb;
69632 p->pSrcDb = pSrcDb;
69633 p->iNext = 1;
69634 p->isAttached = 0;
69636 if( 0==p->pSrc || 0==p->pDest
69637 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
69649 p->pSrc->nBackup++;
69652 sqlite3_mutex_leave(pDestDb->mutex);
69653 sqlite3_mutex_leave(pSrcDb->mutex);
69677 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
69678 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
69679 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
69683 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
69684 ** guaranteed that the shared-mutex is held by this thread, handle
69685 ** p->pSrc may not actually be the owner. */
69686 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
69687 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
69692 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
69693 assert( p->bDestLocked );
69694 assert( !isFatalError(p->rc) );
69695 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
69698 /* Catch the case where the destination is an in-memory database and the
69729 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
69732 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
69750 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
69762 ** this function is a no-op.
69782 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
69783 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
69784 p->pNext = *pp;
69786 p->isAttached = 1;
69790 ** Copy nPage pages from the source b-tree to the destination.
69801 sqlite3_mutex_enter(p->pSrcDb->mutex);
69802 sqlite3BtreeEnter(p->pSrc);
69803 if( p->pDestDb ){
69804 sqlite3_mutex_enter(p->pDestDb->mutex);
69807 rc = p->rc;
69809 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
69810 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
69812 int nSrcPage = -1; /* Size of source db in pages */
69815 /* If the source pager is currently in a write-transaction, return
69818 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
69824 /* If there is no open read-transaction on the source database, open
69828 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
69829 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
69839 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
69844 if( SQLITE_OK==rc && p->bDestLocked==0
69845 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
69847 p->bDestLocked = 1;
69848 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
69853 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
69854 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
69855 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
69860 /* Now that there is a read-lock on the source database, query the
69863 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
69865 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
69866 const Pgno iSrcPg = p->iNext; /* Source page number */
69867 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
69875 p->iNext++;
69878 p->nPagecount = nSrcPage;
69879 p->nRemaining = nSrcPage+1-p->iNext;
69880 if( p->iNext>(Pgno)nSrcPage ){
69882 }else if( !p->isAttached ){
69888 ** is to make sure that the schema-version really does change in
69894 rc = sqlite3BtreeNewDb(p->pDest);
69898 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
69901 if( p->pDestDb ){
69902 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
69905 rc = sqlite3BtreeSetVersion(p->pDest, 2);
69922 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
69923 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
69926 nDestTruncate = (nSrcPage+ratio-1)/ratio;
69927 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
69928 nDestTruncate--;
69936 /* If the source page-size is smaller than the destination page-size,
69942 ** pending-byte page in the source database may need to be
69955 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
69967 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
70011 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
70021 ** "committing" a read-only transaction cannot fail.
70025 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
70026 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
70033 p->rc = rc;
70035 if( p->pDestDb ){
70036 sqlite3_mutex_leave(p->pDestDb->mutex);
70038 sqlite3BtreeLeave(p->pSrc);
70039 sqlite3_mutex_leave(p->pSrcDb->mutex);
70053 pSrcDb = p->pSrcDb;
70054 sqlite3_mutex_enter(pSrcDb->mutex);
70055 sqlite3BtreeEnter(p->pSrc);
70056 if( p->pDestDb ){
70057 sqlite3_mutex_enter(p->pDestDb->mutex);
70061 if( p->pDestDb ){
70062 p->pSrc->nBackup--;
70064 if( p->isAttached ){
70065 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
70067 pp = &(*pp)->pNext;
70069 *pp = p->pNext;
70073 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
70076 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
70077 if( p->pDestDb ){
70078 sqlite3Error(p->pDestDb, rc);
70081 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
70083 sqlite3BtreeLeave(p->pSrc);
70084 if( p->pDestDb ){
70085 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
70105 return p->nRemaining;
70119 return p->nPagecount;
70141 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
70142 if( !isFatalError(p->rc) && iPage<p->iNext ){
70148 assert( p->pDestDb );
70149 sqlite3_mutex_enter(p->pDestDb->mutex);
70151 sqlite3_mutex_leave(p->pDestDb->mutex);
70154 p->rc = rc;
70157 }while( (p = p->pNext)!=0 );
70176 for(p=pBackup; p; p=p->pNext){
70177 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
70178 p->iNext = 1;
70200 if( pFd->pMethods ){
70213 b.pSrcDb = pFrom->db;
70226 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
70233 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
70279 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
70285 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
70288 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
70290 if( p->flags & MEM_Null ){
70292 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob
70300 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
70303 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
70304 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
70305 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
70308 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
70316 assert( (p->flags & MEM_Cleared)==0 );
70320 assert( p->szMalloc==0
70321 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
70331 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
70333 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
70334 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
70335 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
70336 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
70351 ** routine is a no-op.
70361 assert( (pMem->flags&MEM_RowSet)==0 );
70364 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
70367 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70377 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
70378 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
70384 ** Make sure pMem->z points to a writable allocation of at least
70388 ** pMem->z into the new allocation. pMem must be either a string or
70390 ** in pMem->z is discarded.
70394 assert( (pMem->flags&MEM_RowSet)==0 );
70395 testcase( pMem->db==0 );
70399 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
70400 testcase( bPreserve && pMem->z==0 );
70402 assert( pMem->szMalloc==0
70403 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
70405 if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
70406 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
70409 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
70410 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
70412 if( pMem->zMalloc==0 ){
70414 pMem->z = 0;
70415 pMem->szMalloc = 0;
70418 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
70421 if( bPreserve && pMem->z && ALWAYS(pMem->z!=pMem->zMalloc) ){
70422 memcpy(pMem->zMalloc, pMem->z, pMem->n);
70424 if( (pMem->flags&MEM_Dyn)!=0 ){
70425 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
70426 pMem->xDel((void *)(pMem->z));
70429 pMem->z = pMem->zMalloc;
70430 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
70435 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
70436 ** If pMem->zMalloc already meets or exceeds the requested size, this
70437 ** routine is a no-op.
70440 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
70449 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
70450 if( pMem->szMalloc<szNew ){
70453 assert( (pMem->flags & MEM_Dyn)==0 );
70454 pMem->z = pMem->zMalloc;
70455 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
70466 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70467 assert( (pMem->flags&MEM_RowSet)==0 );
70468 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
70470 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
70471 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
70474 pMem->z[pMem->n] = 0;
70475 pMem->z[pMem->n+1] = 0;
70476 pMem->flags |= MEM_Term;
70479 pMem->flags &= ~MEM_Ephem;
70481 pMem->pScopyFrom = 0;
70488 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
70494 assert( pMem->flags & MEM_Zero );
70495 assert( pMem->flags&MEM_Blob );
70496 assert( (pMem->flags&MEM_RowSet)==0 );
70497 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70500 nByte = pMem->n + pMem->u.nZero;
70508 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
70509 pMem->n += pMem->u.nZero;
70510 pMem->flags &= ~(MEM_Zero|MEM_Term);
70520 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
70523 pMem->z[pMem->n] = 0;
70524 pMem->z[pMem->n+1] = 0;
70525 pMem->flags |= MEM_Term;
70533 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70534 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
70535 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
70536 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
70546 ** is a no-op.
70558 int fg = pMem->flags;
70561 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70565 assert( (pMem->flags&MEM_RowSet)==0 );
70570 pMem->enc = 0;
70574 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
70576 ** is UTF-16le or UTF-16be do a translation.
70578 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
70581 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
70584 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
70586 pMem->n = sqlite3Strlen30(pMem->z);
70587 pMem->enc = SQLITE_UTF8;
70588 pMem->flags |= MEM_Str|MEM_Term;
70589 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
70604 if( ALWAYS(pFunc && pFunc->xFinalize) ){
70607 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
70608 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70612 t.db = pMem->db;
70616 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
70617 assert( (pMem->flags & MEM_Dyn)==0 );
70618 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
70635 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
70637 if( p->flags&MEM_Agg ){
70638 sqlite3VdbeMemFinalize(p, p->u.pDef);
70639 assert( (p->flags & MEM_Agg)==0 );
70640 testcase( p->flags & MEM_Dyn );
70642 if( p->flags&MEM_Dyn ){
70643 assert( (p->flags&MEM_RowSet)==0 );
70644 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
70645 p->xDel((void *)p->z);
70646 }else if( p->flags&MEM_RowSet ){
70647 sqlite3RowSetClear(p->u.pRowSet);
70648 }else if( p->flags&MEM_Frame ){
70649 VdbeFrame *pFrame = p->u.pFrame;
70650 pFrame->pParent = pFrame->v->pDelFrame;
70651 pFrame->v->pDelFrame = pFrame;
70653 p->flags = MEM_Null;
70658 ** by p->xDel and memory in p->zMalloc.
70668 if( p->szMalloc ){
70669 sqlite3DbFreeNN(p->db, p->zMalloc);
70670 p->szMalloc = 0;
70672 p->z = 0;
70687 if( VdbeMemDynamic(p) || p->szMalloc ){
70693 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
70694 ** If the double is out of range of a 64-bit signed integer then
70695 ** return the closest available 64-bit signed integer.
70699 /* When floating-point is omitted, double and int64 are the same thing */
70704 ** minimum and maximum 64-bit integers, or they define them
70707 ** larger than a 32-bit integer constant.
70726 ** a floating-point then the value returned is the integer part.
70729 ** an SQL-NULL value, return 0.
70735 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
70740 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70742 flags = pMem->flags;
70744 return pMem->u.i;
70746 return doubleToInt64(pMem->u.r);
70748 assert( pMem->z || pMem->n==0 );
70764 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
70768 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70770 if( pMem->flags & MEM_Real ){
70771 return pMem->u.r;
70772 }else if( pMem->flags & MEM_Int ){
70773 return (double)pMem->u.i;
70774 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
70788 assert( pMem->flags & MEM_Real );
70789 assert( (pMem->flags & MEM_RowSet)==0 );
70790 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70793 ix = doubleToInt64(pMem->u.r);
70797 ** (1) the round-trip conversion real->int->real is a no-op, and
70805 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
70806 pMem->u.i = ix;
70815 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70816 assert( (pMem->flags & MEM_RowSet)==0 );
70819 pMem->u.i = sqlite3VdbeIntValue(pMem);
70829 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70832 pMem->u.r = sqlite3VdbeRealValue(pMem);
70846 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
70847 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
70848 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
70849 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
70852 pMem->u.r = sqlite3VdbeRealValue(pMem);
70857 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
70858 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
70870 if( pMem->flags & MEM_Null ) return;
70873 if( (pMem->flags & MEM_Blob)==0 ){
70875 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
70876 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
70878 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
70897 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
70899 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
70900 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
70913 pMem->flags = flags;
70914 pMem->db = db;
70915 pMem->szMalloc = 0;
70935 pMem->flags = MEM_Null;
70948 pMem->flags = MEM_Blob|MEM_Zero;
70949 pMem->n = 0;
70951 pMem->u.nZero = n;
70952 pMem->enc = SQLITE_UTF8;
70953 pMem->z = 0;
70959 ** a 64-bit integer.
70963 pMem->u.i = val;
70964 pMem->flags = MEM_Int;
70975 pMem->u.i = val;
70976 pMem->flags = MEM_Int;
70980 /* A no-op destructor */
70993 assert( pMem->flags==MEM_Null );
70994 pMem->u.zPType = zPType ? zPType : "";
70995 pMem->z = pPtr;
70996 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
70997 pMem->eSubtype = 'p';
70998 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
71009 pMem->u.r = val;
71010 pMem->flags = MEM_Real;
71020 sqlite3 *db = pMem->db;
71022 assert( (pMem->flags & MEM_RowSet)==0 );
71024 pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
71025 if( db->mallocFailed ){
71026 pMem->flags = MEM_Null;
71027 pMem->szMalloc = 0;
71029 assert( pMem->zMalloc );
71030 pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
71031 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
71032 assert( pMem->u.pRowSet!=0 );
71033 pMem->flags = MEM_RowSet;
71039 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
71042 assert( p->db!=0 );
71043 if( p->flags & (MEM_Str|MEM_Blob) ){
71044 int n = p->n;
71045 if( p->flags & MEM_Zero ){
71046 n += p->u.nZero;
71048 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
71059 ** This is used for testing and debugging only - to make sure shallow
71065 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
71066 if( pX->pScopyFrom==pMem ){
71067 pX->flags |= MEM_Undefined;
71068 pX->pScopyFrom = 0;
71071 pMem->pScopyFrom = 0;
71078 ** pTo are freed. The pFrom->z field is not duplicated. If
71079 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
71088 assert( (pFrom->flags & MEM_RowSet)==0 );
71089 assert( pTo->db==pFrom->db );
71092 if( (pFrom->flags&MEM_Static)==0 ){
71093 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
71095 pTo->flags |= srcType;
71106 assert( (pFrom->flags & MEM_RowSet)==0 );
71109 pTo->flags &= ~MEM_Dyn;
71110 if( pTo->flags&(MEM_Str|MEM_Blob) ){
71111 if( 0==(pFrom->flags&MEM_Static) ){
71112 pTo->flags |= MEM_Ephem;
71127 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
71128 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
71129 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
71133 pFrom->flags = MEM_Null;
71134 pFrom->szMalloc = 0;
71159 int nByte = n; /* New value for pMem->n */
71161 u16 flags = 0; /* New value for pMem->flags */
71163 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71164 assert( (pMem->flags & MEM_RowSet)==0 );
71172 if( pMem->db ){
71173 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
71207 memcpy(pMem->z, z, nAlloc);
71210 pMem->zMalloc = pMem->z = (char *)z;
71211 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
71214 pMem->z = (char *)z;
71215 pMem->xDel = xDel;
71219 pMem->n = nByte;
71220 pMem->flags = flags;
71221 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
71224 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
71243 ** pMem->zMalloc to hold the content from the btree, if possible. New
71244 ** pMem->zMalloc space will be allocated if necessary. The calling routine
71258 pMem->flags = MEM_Null;
71260 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
71262 pMem->z[amt] = 0;
71263 pMem->z[amt+1] = 0;
71264 pMem->flags = MEM_Blob|MEM_Term;
71265 pMem->n = (int)amt;
71287 assert( (pMem->flags & MEM_RowSet)==0 );
71292 pMem->z = &zData[offset];
71293 pMem->flags = MEM_Blob|MEM_Ephem;
71294 pMem->n = (int)amt;
71305 ** to a zero-terminated version of that string.
71309 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
71311 assert( (pVal->flags & MEM_RowSet)==0 );
71312 assert( (pVal->flags & (MEM_Null))==0 );
71313 if( pVal->flags & (MEM_Blob|MEM_Str) ){
71315 pVal->flags |= MEM_Str;
71316 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
71319 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
71320 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
71325 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
71328 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
71330 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
71331 || pVal->db->mallocFailed );
71332 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
71333 return pVal->z;
71345 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
71351 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
71353 assert( (pVal->flags & MEM_RowSet)==0 );
71354 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
71355 return pVal->z;
71357 if( pVal->flags&MEM_Null ){
71369 p->flags = MEM_Null;
71370 p->db = db;
71391 ** Otherwise, if the second argument is non-zero, then this function is
71400 UnpackedRecord *pRec = p->ppRec[0];
71403 Index *pIdx = p->pIdx; /* Index being probed */
71406 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
71411 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
71412 if( pRec->pKeyInfo ){
71413 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
71414 assert( pRec->pKeyInfo->enc==ENC(db) );
71415 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
71417 pRec->aMem[i].flags = MEM_Null;
71418 pRec->aMem[i].db = db;
71426 p->ppRec[0] = pRec;
71429 pRec->nField = p->iVal+1;
71430 return &pRec->aMem[p->iVal];
71477 assert( (p->flags & EP_TokenOnly)==0 );
71478 pList = p->x.pList;
71479 if( pList ) nVal = pList->nExpr;
71480 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
71482 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
71483 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
71495 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
71506 assert( pCtx->pParse->rc==SQLITE_OK );
71510 pFunc->xSFunc(&ctx, nVal, apVal);
71513 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
71520 pCtx->pParse->nErr++;
71523 pCtx->pParse->rc = rc;
71569 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
71570 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
71576 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
71579 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
71580 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
71590 ** case when the value is -9223372036854775808.
71593 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
71594 pExpr = pExpr->pLeft;
71595 op = pExpr->op;
71596 negInt = -1;
71597 zNeg = "-";
71604 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
71606 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
71608 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
71615 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
71620 /* This branch happens for multiple negative signs. Ex: -(-5) */
71621 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
71625 if( pVal->flags & MEM_Real ){
71626 pVal->u.r = -pVal->u.r;
71627 }else if( pVal->u.i==SMALLEST_INT64 ){
71628 pVal->u.r = -(double)SMALLEST_INT64;
71631 pVal->u.i = -pVal->u.i;
71643 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
71644 assert( pExpr->u.zToken[1]=='\'' );
71647 zVal = &pExpr->u.zToken[2];
71648 nVal = sqlite3Strlen30(zVal)-1;
71680 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
71737 ** Register built-in functions used to help read ANALYZE data.
71773 sqlite3 *db = pParse->db;
71778 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
71784 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
71786 int iBindVar = pExpr->iColumn;
71787 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
71788 if( (v = pParse->pReprepare)!=0 ){
71791 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
71793 pVal->db = pParse->db;
71800 assert( pVal==0 || pVal->db==db );
71851 if( pExpr==0 || pExpr->op!=TK_SELECT ){
71862 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
71894 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
71936 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
71937 pMem->enc = ENC(db);
71949 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
71950 Mem *aMem = pRec->aMem;
71955 sqlite3KeyInfoUnref(pRec->pKeyInfo);
71980 sqlite3DbFreeNN(((Mem*)v)->db, v);
71989 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
71993 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
71994 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
71995 return p->n;
71997 if( (p->flags & MEM_Blob)!=0 ){
71998 if( p->flags & MEM_Zero ){
71999 return p->n + p->u.nZero;
72001 return p->n;
72004 if( p->flags & MEM_Null ) return 0;
72031 sqlite3 *db = pParse->db;
72035 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
72036 p->db = db;
72037 if( db->pVdbe ){
72038 db->pVdbe->pPrev = p;
72040 p->pNext = db->pVdbe;
72041 p->pPrev = 0;
72042 db->pVdbe = p;
72043 p->magic = VDBE_MAGIC_INIT;
72044 p->pParse = pParse;
72045 assert( pParse->aLabel==0 );
72046 assert( pParse->nLabel==0 );
72047 assert( pParse->nOpAlloc==0 );
72048 assert( pParse->szOpAlloc==0 );
72057 sqlite3DbFree(p->db, p->zErrMsg);
72059 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
72068 p->prepFlags = prepFlags;
72070 p->expmask = 0;
72072 assert( p->zSql==0 );
72073 p->zSql = sqlite3DbStrNDup(p->db, z, n);
72082 assert( pA->db==pB->db );
72086 pTmp = pA->pNext;
72087 pA->pNext = pB->pNext;
72088 pB->pNext = pTmp;
72089 pTmp = pA->pPrev;
72090 pA->pPrev = pB->pPrev;
72091 pB->pPrev = pTmp;
72092 zTmp = pA->zSql;
72093 pA->zSql = pB->zSql;
72094 pB->zSql = zTmp;
72095 pB->expmask = pA->expmask;
72096 pB->prepFlags = pA->prepFlags;
72097 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
72098 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
72106 ** If an out-of-memory error occurs while resizing the array, return
72113 Parse *p = v->pParse;
72115 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
72123 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
72125 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
72130 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
72131 sqlite3OomFault(p->db);
72136 assert( nNew>=(p->nOpAlloc+nOp) );
72137 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
72139 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
72140 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
72141 v->aOp = pNew;
72174 assert( p->pParse->nOpAlloc<=p->nOp );
72176 assert( p->pParse->nOpAlloc>p->nOp );
72183 i = p->nOp;
72184 assert( p->magic==VDBE_MAGIC_INIT );
72186 if( p->pParse->nOpAlloc<=i ){
72189 p->nOp++;
72190 pOp = &p->aOp[i];
72191 pOp->opcode = (u8)op;
72192 pOp->p5 = 0;
72193 pOp->p1 = p1;
72194 pOp->p2 = p2;
72195 pOp->p3 = p3;
72196 pOp->p4.p = 0;
72197 pOp->p4type = P4_NOTUSED;
72199 pOp->zComment = 0;
72202 if( p->db->flags & SQLITE_VdbeAddopTrace ){
72204 Parse *pParse = p->pParse;
72205 for(jj=kk=0; jj<pParse->nColCache; jj++){
72206 struct yColCache *x = pParse->aColCache + jj;
72207 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
72211 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
72216 pOp->cycles = 0;
72217 pOp->cnt = 0;
72220 pOp->iSrcLine = 0;
72324 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
72339 if( p->db->mallocFailed==0 ){
72340 VdbeOp *pOp = &p->aOp[addr];
72341 pOp->p4type = P4_INT32;
72342 pOp->p4.i = p4;
72347 /* Insert the end of a co-routine
72353 ** co-routine has its own independent set of registers, because co-routines
72355 ** that could cause problems if two or more co-routines are using the same
72358 v->pParse->nTempReg = 0;
72359 v->pParse->nRangeReg = 0;
72371 ** always negative and P2 values are suppose to be non-negative.
72377 Parse *p = v->pParse;
72378 int i = p->nLabel++;
72379 assert( v->magic==VDBE_MAGIC_INIT );
72380 if( (i & (i-1))==0 ){
72381 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
72382 (i*2+1)*sizeof(p->aLabel[0]));
72384 if( p->aLabel ){
72385 p->aLabel[i] = -1;
72396 Parse *p = v->pParse;
72398 assert( v->magic==VDBE_MAGIC_INIT );
72399 assert( j<p->nLabel );
72401 if( p->aLabel ){
72402 p->aLabel[j] = v->nOp;
72410 p->runOnlyOnce = 1;
72417 p->runOnlyOnce = 0;
72424 ** in a Vdbe main program and each of the sub-programs (triggers) it may
72435 ** sqlite3DbFree(v->db, sIter.apSub);
72444 int iSub; /* 0 = main program, 1 = first sub-program etc. */
72447 Vdbe *v = p->v;
72452 if( p->iSub<=p->nSub ){
72454 if( p->iSub==0 ){
72455 aOp = v->aOp;
72456 nOp = v->nOp;
72458 aOp = p->apSub[p->iSub-1]->aOp;
72459 nOp = p->apSub[p->iSub-1]->nOp;
72461 assert( p->iAddr<nOp );
72463 pRet = &aOp[p->iAddr];
72464 p->iAddr++;
72465 if( p->iAddr==nOp ){
72466 p->iSub++;
72467 p->iAddr = 0;
72470 if( pRet->p4type==P4_SUBPROGRAM ){
72471 int nByte = (p->nSub+1)*sizeof(SubProgram*);
72473 for(j=0; j<p->nSub; j++){
72474 if( p->apSub[j]==pRet->p4.pProgram ) break;
72476 if( j==p->nSub ){
72477 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
72478 if( !p->apSub ){
72481 p->apSub[p->nSub++] = pRet->p4.pProgram;
72494 ** sub-programs contains any of the following:
72509 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
72522 int opcode = pOp->opcode;
72525 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
72533 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
72538 sqlite3DbFree(v->db, sIter.apSub);
72545 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
72548 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
72565 ** (5) Reclaim the memory allocated for storing labels.
72574 Parse *pParse = p->pParse;
72575 int *aLabel = pParse->aLabel;
72576 p->readOnly = 1;
72577 p->bIsReader = 0;
72578 pOp = &p->aOp[p->nOp-1];
72587 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
72590 switch( pOp->opcode ){
72592 if( pOp->p2!=0 ) p->readOnly = 0;
72597 p->bIsReader = 1;
72605 p->readOnly = 0;
72606 p->bIsReader = 1;
72611 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
72616 assert( (pOp - p->aOp) >= 3 );
72617 assert( pOp[-1].opcode==OP_Integer );
72618 n = pOp[-1].p1;
72626 pOp->p4.xAdvance = sqlite3BtreeNext;
72627 pOp->p4type = P4_ADVANCE;
72632 pOp->p4.xAdvance = sqlite3BtreePrevious;
72633 pOp->p4type = P4_ADVANCE;
72637 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){
72638 assert( ADDR(pOp->p2)<pParse->nLabel );
72639 pOp->p2 = aLabel[ADDR(pOp->p2)];
72642 if( pOp==p->aOp ) break;
72643 pOp--;
72645 sqlite3DbFree(p->db, pParse->aLabel);
72646 pParse->aLabel = 0;
72647 pParse->nLabel = 0;
72649 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
72656 assert( p->magic==VDBE_MAGIC_INIT );
72657 return p->nOp;
72666 ** sqlite3VdbeAddOpList() will always be non-NULL.
72670 assert( p->nOp + N <= p->pParse->nOpAlloc );
72684 for(i=0; i<p->nOp; i++){
72685 assert( p->aOp[i].opcode!=OP_ResultRow );
72702 VdbeOp *aOp = p->aOp;
72703 assert( aOp && !p->db->mallocFailed );
72706 assert( DbMaskAllZero(p->btreeMask) );
72709 *pnOp = p->nOp;
72710 p->aOp = 0;
72718 ** Non-zero P2 arguments to jump instructions are automatically adjusted
72725 int iLineno /* Source-file line number of first opcode */
72730 assert( p->magic==VDBE_MAGIC_INIT );
72731 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
72734 pFirst = pOut = &p->aOp[p->nOp];
72736 pOut->opcode = aOp->opcode;
72737 pOut->p1 = aOp->p1;
72738 pOut->p2 = aOp->p2;
72739 assert( aOp->p2>=0 );
72740 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
72741 pOut->p2 += p->nOp;
72743 pOut->p3 = aOp->p3;
72744 pOut->p4type = P4_NOTUSED;
72745 pOut->p4.p = 0;
72746 pOut->p5 = 0;
72748 pOut->zComment = 0;
72751 pOut->iSrcLine = iLineno+i;
72756 if( p->db->flags & SQLITE_VdbeAddopTrace ){
72757 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
72761 p->nOp += nOp;
72777 int nByte = (p->nScan+1) * sizeof(ScanStatus);
72779 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
72781 ScanStatus *pNew = &aNew[p->nScan++];
72782 pNew->addrExplain = addrExplain;
72783 pNew->addrLoop = addrLoop;
72784 pNew->addrVisit = addrVisit;
72785 pNew->nEst = nEst;
72786 pNew->zName = sqlite3DbStrDup(p->db, zName);
72787 p->aScan = aNew;
72798 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
72801 sqlite3VdbeGetOp(p,addr)->p1 = val;
72804 sqlite3VdbeGetOp(p,addr)->p2 = val;
72807 sqlite3VdbeGetOp(p,addr)->p3 = val;
72810 assert( p->nOp>0 || p->db->mallocFailed );
72811 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
72819 sqlite3VdbeChangeP2(p, addr, p->nOp);
72828 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
72839 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
72843 freeEphemeralFunction(db, p->pFunc);
72861 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
72875 if( db->pnBytesFreed==0 ){
72883 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
72897 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
72898 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
72900 sqlite3DbFree(db, pOp->zComment);
72909 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
72913 p->pNext = pVdbe->pProgram;
72914 pVdbe->pProgram = p;
72922 if( p->db->mallocFailed ) return 0;
72923 assert( addr>=0 && addr<p->nOp );
72924 pOp = &p->aOp[addr];
72925 freeP4(p->db, pOp->p4type, pOp->p4.p);
72926 pOp->p4type = P4_NOTUSED;
72927 pOp->p4.z = 0;
72928 pOp->opcode = OP_Noop;
72937 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
72938 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
72967 if( pOp->p4type ){
72968 freeP4(p->db, pOp->p4type, pOp->p4.p);
72969 pOp->p4type = 0;
72970 pOp->p4.p = 0;
72973 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
72976 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
72977 pOp->p4type = P4_DYNAMIC;
72984 db = p->db;
72985 assert( p->magic==VDBE_MAGIC_INIT );
72986 assert( p->aOp!=0 || db->mallocFailed );
72987 if( db->mallocFailed ){
72991 assert( p->nOp>0 );
72992 assert( addr<p->nOp );
72994 addr = p->nOp - 1;
72996 pOp = &p->aOp[addr];
72997 if( n>=0 || pOp->p4type ){
73004 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
73005 pOp->p4type = P4_INT32;
73008 pOp->p4.p = (void*)zP4;
73009 pOp->p4type = (signed char)n;
73016 ** to the value defined by the arguments. This is a high-speed
73027 if( p->db->mallocFailed ){
73028 freeP4(p->db, n, pP4);
73031 assert( p->nOp>0 );
73032 pOp = &p->aOp[p->nOp-1];
73033 assert( pOp->p4type==P4_NOTUSED );
73034 pOp->p4type = n;
73035 pOp->p4.p = pP4;
73044 Vdbe *v = pParse->pVdbe;
73055 ** insert a No-op and add the comment to that new instruction. This
73060 assert( p->nOp>0 || p->aOp==0 );
73061 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
73062 if( p->nOp ){
73063 assert( p->aOp );
73064 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
73065 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
73092 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
73097 ** Return the opcode for a given address. If the address is -1, then
73113 assert( p->magic==VDBE_MAGIC_INIT );
73115 addr = p->nOp - 1;
73117 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
73118 if( p->db->mallocFailed ){
73121 return &p->aOp[addr];
73131 if( c=='1' ) return pOp->p1;
73132 if( c=='2' ) return pOp->p2;
73133 if( c=='3' ) return pOp->p3;
73134 if( c=='4' ) return pOp->p4.i;
73135 return pOp->p5;
73146 ** "PX" -> "r[X]"
73147 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
73148 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
73149 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
73162 zOpName = sqlite3OpcodeName(pOp->opcode);
73169 if( pOp->p5 & SQLITE_STOREP2 ){
73176 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
73180 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
73182 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
73187 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
73197 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
73199 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
73208 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
73209 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
73213 }else if( pOp->zComment ){
73214 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
73231 switch( pExpr->op ){
73233 sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
73236 sqlite3XPrintf(p, "%d", pExpr->u.iValue);
73242 sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
73246 if( pExpr->iColumn<0 ){
73249 sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
73287 displayP4Expr(p, pExpr->pLeft);
73288 if( pExpr->pRight ){
73290 displayP4Expr(p, pExpr->pRight);
73308 switch( pOp->p4type ){
73311 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
73312 assert( pKeyInfo->aSortOrder!=0 );
73313 sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
73314 for(j=0; j<pKeyInfo->nField; j++){
73315 CollSeq *pColl = pKeyInfo->aColl[j];
73316 const char *zColl = pColl ? pColl->zName : "";
73318 sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
73325 displayP4Expr(&x, pOp->p4.pExpr);
73330 CollSeq *pColl = pOp->p4.pColl;
73331 sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
73335 FuncDef *pDef = pOp->p4.pFunc;
73336 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
73341 FuncDef *pDef = pOp->p4.pCtx->pFunc;
73342 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
73347 sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
73351 sqlite3XPrintf(&x, "%d", pOp->p4.i);
73355 sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
73359 Mem *pMem = pOp->p4.pMem;
73360 if( pMem->flags & MEM_Str ){
73361 zP4 = pMem->z;
73362 }else if( pMem->flags & MEM_Int ){
73363 sqlite3XPrintf(&x, "%lld", pMem->u.i);
73364 }else if( pMem->flags & MEM_Real ){
73365 sqlite3XPrintf(&x, "%.16g", pMem->u.r);
73366 }else if( pMem->flags & MEM_Null ){
73369 assert( pMem->flags & MEM_Blob );
73376 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
73383 int *ai = pOp->p4.ai;
73402 sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
73406 zP4 = pOp->p4.z;
73420 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
73424 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
73425 ** p->btreeMask of databases that will require a lock.
73428 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
73429 assert( i<(int)sizeof(p->btreeMask)*8 );
73430 DbMaskSet(p->btreeMask, i);
73431 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
73432 DbMaskSet(p->lockMask, i);
73438 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
73442 ** that the correct busy-handler callback is invoked if required.
73444 ** If SQLite is not threadsafe but does support shared-cache mode, then
73449 ** If SQLite is not threadsafe and does not support shared-cache mode, this
73450 ** function is a no-op.
73452 ** The p->btreeMask field is a bitmask of all btrees that the prepared
73453 ** statement p will ever use. Let N be the number of bits in p->btreeMask
73463 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
73464 db = p->db;
73465 aDb = db->aDb;
73466 nDb = db->nDb;
73468 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
73484 db = p->db;
73485 aDb = db->aDb;
73486 nDb = db->nDb;
73488 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
73494 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
73507 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
73519 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
73530 while( (N--)>0 ){
73531 p->db = db;
73532 p->flags = flags;
73533 p->szMalloc = 0;
73535 p->pScopyFrom = 0;
73547 sqlite3 *db = p->db;
73548 if( db->pnBytesFreed ){
73550 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
73564 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
73565 ** sqlite3MemRelease() were called from here. With -O2, this jumps
73570 testcase( p->flags & MEM_Agg );
73571 testcase( p->flags & MEM_Dyn );
73572 testcase( p->flags & MEM_Frame );
73573 testcase( p->flags & MEM_RowSet );
73574 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
73576 }else if( p->szMalloc ){
73577 sqlite3DbFreeNN(db, p->zMalloc);
73578 p->szMalloc = 0;
73581 p->flags = MEM_Undefined;
73593 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
73594 for(i=0; i<p->nChildCsr; i++){
73595 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
73597 releaseMemArray(aMem, p->nChildMem);
73598 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
73599 sqlite3DbFree(p->v->db, p);
73610 ** When p->explain==1, each instruction is listed. When
73611 ** p->explain==2, only OP_Explain instructions are listed and these
73612 ** are shown in a different format. p->explain==2 is used to implement
73615 ** When p->explain==1, first the main program is listed, then each of
73622 int nSub = 0; /* Number of sub-vdbes seen so far */
73623 SubProgram **apSub = 0; /* Array of sub-vdbes */
73625 sqlite3 *db = p->db; /* The database connection */
73628 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
73630 assert( p->explain );
73631 assert( p->magic==VDBE_MAGIC_RUN );
73632 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
73636 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
73639 p->pResultSet = 0;
73641 if( p->rc==SQLITE_NOMEM_BKPT ){
73653 ** encountered, but p->pc will eventually catch up to nRow.
73655 nRow = p->nOp;
73656 if( p->explain==1 ){
73661 assert( p->nMem>9 );
73662 pSub = &p->aMem[9];
73663 if( pSub->flags&MEM_Blob ){
73666 nSub = pSub->n/sizeof(Vdbe*);
73667 apSub = (SubProgram **)pSub->z;
73670 nRow += apSub[i]->nOp;
73675 i = p->pc++;
73676 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
73678 p->rc = SQLITE_OK;
73680 }else if( db->u1.isInterrupted ){
73681 p->rc = SQLITE_INTERRUPT;
73683 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
73687 if( i<p->nOp ){
73690 pOp = &p->aOp[i];
73695 i -= p->nOp;
73696 for(j=0; i>=apSub[j]->nOp; j++){
73697 i -= apSub[j]->nOp;
73699 pOp = &apSub[j]->aOp[i];
73701 if( p->explain==1 ){
73702 pMem->flags = MEM_Int;
73703 pMem->u.i = i; /* Program counter */
73706 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
73707 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
73708 assert( pMem->z!=0 );
73709 pMem->n = sqlite3Strlen30(pMem->z);
73710 pMem->enc = SQLITE_UTF8;
73715 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
73718 if( pOp->p4type==P4_SUBPROGRAM ){
73722 if( apSub[j]==pOp->p4.pProgram ) break;
73725 apSub = (SubProgram **)pSub->z;
73726 apSub[nSub++] = pOp->p4.pProgram;
73727 pSub->flags |= MEM_Blob;
73728 pSub->n = nSub*sizeof(SubProgram*);
73733 pMem->flags = MEM_Int;
73734 pMem->u.i = pOp->p1; /* P1 */
73737 pMem->flags = MEM_Int;
73738 pMem->u.i = pOp->p2; /* P2 */
73741 pMem->flags = MEM_Int;
73742 pMem->u.i = pOp->p3; /* P3 */
73746 assert( p->db->mallocFailed );
73749 pMem->flags = MEM_Str|MEM_Term;
73750 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
73751 if( zP4!=pMem->z ){
73752 pMem->n = 0;
73753 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
73755 assert( pMem->z!=0 );
73756 pMem->n = sqlite3Strlen30(pMem->z);
73757 pMem->enc = SQLITE_UTF8;
73761 if( p->explain==1 ){
73763 assert( p->db->mallocFailed );
73766 pMem->flags = MEM_Str|MEM_Term;
73767 pMem->n = 2;
73768 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
73769 pMem->enc = SQLITE_UTF8;
73774 assert( p->db->mallocFailed );
73777 pMem->flags = MEM_Str|MEM_Term;
73778 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
73779 pMem->enc = SQLITE_UTF8;
73781 pMem->flags = MEM_Null; /* Comment */
73785 p->nResColumn = 8 - 4*(p->explain-1);
73786 p->pResultSet = &p->aMem[1];
73787 p->rc = SQLITE_OK;
73800 if( p->zSql ){
73801 z = p->zSql;
73802 }else if( p->nOp>=1 ){
73803 const VdbeOp *pOp = &p->aOp[0];
73804 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
73805 z = pOp->p4.z;
73818 int nOp = p->nOp;
73822 pOp = &p->aOp[0];
73823 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
73826 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
73830 if( z[i-1]!=' ' ){
73853 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
73872 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
73875 if( nByte <= p->nFree ){
73876 p->nFree -= nByte;
73877 pBuf = &p->pSpace[p->nFree];
73879 p->nNeeded += nByte;
73895 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
73899 assert( p->nOp>0 );
73902 p->magic = VDBE_MAGIC_RUN;
73905 for(i=0; i<p->nMem; i++){
73906 assert( p->aMem[i].db==p->db );
73909 p->pc = -1;
73910 p->rc = SQLITE_OK;
73911 p->errorAction = OE_Abort;
73912 p->nChange = 0;
73913 p->cacheCtr = 1;
73914 p->minWriteFileFormat = 255;
73915 p->iStatement = 0;
73916 p->nFkConstraint = 0;
73918 for(i=0; i<p->nOp; i++){
73919 p->aOp[i].cnt = 0;
73920 p->aOp[i].cycles = 0;
73956 assert( p->nOp>0 );
73958 assert( p->magic==VDBE_MAGIC_INIT );
73959 assert( pParse==p->pParse );
73960 db = p->db;
73961 assert( db->mallocFailed==0 );
73962 nVar = pParse->nVar;
73963 nMem = pParse->nMem;
73964 nCursor = pParse->nTab;
73965 nArg = pParse->nMaxArg;
73979 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
73980 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
73982 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
73987 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
73988 if( pParse->explain && nMem<10 ){
73991 p->expired = 0;
73999 ** This two-pass approach that reuses as much memory as possible from
74005 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
74006 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
74007 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
74008 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
74010 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
74013 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
74015 }while( !db->mallocFailed );
74017 p->pVList = pParse->pVList;
74018 pParse->pVList = 0;
74019 p->explain = pParse->explain;
74020 if( db->mallocFailed ){
74021 p->nVar = 0;
74022 p->nCursor = 0;
74023 p->nMem = 0;
74025 p->nCursor = nCursor;
74026 p->nVar = (ynVar)nVar;
74027 initMemArray(p->aVar, nVar, db, MEM_Null);
74028 p->nMem = nMem;
74029 initMemArray(p->aMem, nMem, db, MEM_Undefined);
74030 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
74032 memset(p->anExec, 0, p->nOp*sizeof(i64));
74046 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
74047 switch( pCx->eCurType ){
74049 sqlite3VdbeSorterClose(p->db, pCx);
74053 if( pCx->isEphemeral ){
74054 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
74055 /* The pCx->pCursor will be close automatically, if it exists, by
74058 assert( pCx->uc.pCursor!=0 );
74059 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
74065 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
74066 const sqlite3_module *pModule = pVCur->pVtab->pModule;
74067 assert( pVCur->pVtab->nRef>0 );
74068 pVCur->pVtab->nRef--;
74069 pModule->xClose(pVCur);
74080 if( p->apCsr ){
74082 for(i=0; i<p->nCursor; i++){
74083 VdbeCursor *pC = p->apCsr[i];
74086 p->apCsr[i] = 0;
74094 ** is used, for example, when a trigger sub-program is halted to restore
74098 Vdbe *v = pFrame->v;
74101 v->anExec = pFrame->anExec;
74103 v->aOp = pFrame->aOp;
74104 v->nOp = pFrame->nOp;
74105 v->aMem = pFrame->aMem;
74106 v->nMem = pFrame->nMem;
74107 v->apCsr = pFrame->apCsr;
74108 v->nCursor = pFrame->nCursor;
74109 v->db->lastRowid = pFrame->lastRowid;
74110 v->nChange = pFrame->nChange;
74111 v->db->nChange = pFrame->nDbChange;
74112 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
74113 v->pAuxData = pFrame->pAuxData;
74114 pFrame->pAuxData = 0;
74115 return pFrame->pc;
74127 if( p->pFrame ){
74129 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
74131 p->pFrame = 0;
74132 p->nFrame = 0;
74134 assert( p->nFrame==0 );
74136 if( p->aMem ){
74137 releaseMemArray(p->aMem, p->nMem);
74139 while( p->pDelFrame ){
74140 VdbeFrame *pDel = p->pDelFrame;
74141 p->pDelFrame = pDel->pParent;
74146 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
74147 assert( p->pAuxData==0 );
74154 sqlite3 *db = p->db;
74160 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
74161 if( p->aMem ){
74162 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
74166 sqlite3DbFree(db, p->zErrMsg);
74167 p->zErrMsg = 0;
74168 p->pResultSet = 0;
74179 sqlite3 *db = p->db;
74181 if( p->nResColumn ){
74182 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
74183 sqlite3DbFree(db, p->aColName);
74186 p->nResColumn = (u16)nResColumn;
74187 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
74188 if( p->aColName==0 ) return;
74189 initMemArray(p->aColName, n, db, MEM_Null);
74211 assert( idx<p->nResColumn );
74213 if( p->db->mallocFailed ){
74217 assert( p->aColName!=0 );
74218 pColName = &(p->aColName[idx+var*p->nResColumn]);
74219 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
74220 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
74227 ** write-transaction spanning more than one database file, this routine
74232 int nTrans = 0; /* Number of databases with an active write-transaction
74233 ** that are candidates for a two-phase commit using a
74234 ** master-journal */
74259 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74260 Btree *pBt = db->aDb[i].pBt;
74277 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
74291 /* If there are any write-transactions at all, invoke the commit hook */
74292 if( needXcommit && db->xCommitCallback ){
74293 rc = db->xCommitCallback(db->pCommitArg);
74299 /* The simple case - no more than one database file (not counting the
74301 ** master-journal.
74305 ** that case we do not support atomic multi-file commits, so use the
74308 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
74311 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74312 Btree *pBt = db->aDb[i].pBt;
74323 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74324 Btree *pBt = db->aDb[i].pBt;
74334 /* The complex case - There is a multi-file write-transaction active.
74340 sqlite3_vfs *pVfs = db->pVfs;
74341 char *zMaster = 0; /* File-name for the master journal */
74342 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
74351 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
74366 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
74370 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
74392 for(i=0; i<db->nDb; i++){
74393 Btree *pBt = db->aDb[i].pBt;
74433 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74434 Btree *pBt = db->aDb[i].pBt;
74466 for(i=0; i<db->nDb; i++){
74467 Btree *pBt = db->aDb[i].pBt;
74486 ** This is an internal self-check only - it is not an essential processing
74489 ** This is a no-op if NDEBUG is defined.
74497 p = db->pVdbe;
74501 if( p->readOnly==0 ) nWrite++;
74502 if( p->bIsReader ) nRead++;
74504 p = p->pNext;
74506 assert( cnt==db->nVdbeActive );
74507 assert( nWrite==db->nVdbeWrite );
74508 assert( nRead==db->nVdbeRead );
74515 ** If the Vdbe passed as the first argument opened a statement-transaction,
74525 sqlite3 *const db = p->db;
74528 const int iSavepoint = p->iStatement-1;
74531 assert( db->nStatement>0 );
74532 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
74534 for(i=0; i<db->nDb; i++){
74536 Btree *pBt = db->aDb[i].pBt;
74549 db->nStatement--;
74550 p->iStatement = 0;
74565 db->nDeferredCons = p->nStmtDefCons;
74566 db->nDeferredImmCons = p->nStmtDefImmCons;
74571 if( p->db->nStatement && p->iStatement ){
74590 sqlite3 *db = p->db;
74591 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
74592 || (!deferred && p->nFkConstraint>0)
74594 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
74595 p->errorAction = OE_Abort;
74618 sqlite3 *db = p->db;
74636 if( p->magic!=VDBE_MAGIC_RUN ){
74639 if( db->mallocFailed ){
74640 p->rc = SQLITE_NOMEM_BKPT;
74647 if( p->pc>=0 && p->bIsReader ){
74648 int mrc; /* Primary error code from p->rc */
74656 mrc = p->rc & 0xff;
74660 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
74665 ** Even if the statement is read-only, it is important to perform
74667 ** occurred while writing to the journal, sub-journal or database
74672 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
74673 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
74681 db->autoCommit = 1;
74682 p->nChange = 0;
74688 if( p->rc==SQLITE_OK ){
74692 /* If the auto-commit flag is set and this is the only active writer
74699 && db->autoCommit
74700 && db->nVdbeWrite==(p->readOnly==0)
74702 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
74705 if( NEVER(p->readOnly) ){
74711 /* The auto-commit flag is true, the vdbe program was successful
74717 if( rc==SQLITE_BUSY && p->readOnly ){
74721 p->rc = rc;
74723 p->nChange = 0;
74725 db->nDeferredCons = 0;
74726 db->nDeferredImmCons = 0;
74727 db->flags &= ~SQLITE_DeferFKs;
74732 p->nChange = 0;
74734 db->nStatement = 0;
74736 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
74738 }else if( p->errorAction==OE_Abort ){
74743 db->autoCommit = 1;
74744 p->nChange = 0;
74748 /* If eStatementOp is non-zero, then a statement transaction needs to
74757 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
74758 p->rc = rc;
74759 sqlite3DbFree(db, p->zErrMsg);
74760 p->zErrMsg = 0;
74764 db->autoCommit = 1;
74765 p->nChange = 0;
74770 ** has been rolled back, update the database connection change-counter.
74772 if( p->changeCntOn ){
74774 sqlite3VdbeSetChanges(db, p->nChange);
74778 p->nChange = 0;
74786 if( p->pc>=0 ){
74787 db->nVdbeActive--;
74788 if( !p->readOnly ) db->nVdbeWrite--;
74789 if( p->bIsReader ) db->nVdbeRead--;
74790 assert( db->nVdbeActive>=db->nVdbeRead );
74791 assert( db->nVdbeRead>=db->nVdbeWrite );
74792 assert( db->nVdbeWrite>=0 );
74794 p->magic = VDBE_MAGIC_HALT;
74796 if( db->mallocFailed ){
74797 p->rc = SQLITE_NOMEM_BKPT;
74800 /* If the auto-commit flag is set to true, then any locks that were held
74802 ** to invoke any required unlock-notify callbacks.
74804 if( db->autoCommit ){
74808 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
74809 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
74815 ** in p->rc. This routine sets that result back to SQLITE_OK.
74818 p->rc = SQLITE_OK;
74830 sqlite3 *db = p->db;
74831 int rc = p->rc;
74832 if( p->zErrMsg ){
74833 db->bBenignMalloc++;
74835 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
74836 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
74838 db->bBenignMalloc--;
74839 }else if( db->pErr ){
74840 sqlite3ValueSetNull(db->pErr);
74842 db->errCode = rc;
74852 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
74853 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
74854 assert( v->db->init.busy==0 );
74857 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
74859 sqlite3DbFree(v->db, zExpanded);
74880 db = p->db;
74893 if( p->pc>=0 ){
74896 sqlite3DbFree(db, p->zErrMsg);
74897 p->zErrMsg = 0;
74898 if( p->runOnlyOnce ) p->expired = 1;
74899 }else if( p->rc && p->expired ){
74904 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
74905 sqlite3DbFree(db, p->zErrMsg);
74906 p->zErrMsg = 0;
74920 fprintf(out, "---- ");
74921 for(i=0; i<p->nOp; i++){
74922 fprintf(out, "%02x", p->aOp[i].opcode);
74925 if( p->zSql ){
74927 fprintf(out, "-- ");
74928 for(i=0; (c = p->zSql[i])!=0; i++){
74929 if( pc=='\n' ) fprintf(out, "-- ");
74935 for(i=0; i<p->nOp; i++){
74938 p->aOp[i].cnt,
74939 p->aOp[i].cycles,
74940 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
74943 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
74949 p->magic = VDBE_MAGIC_RESET;
74950 return p->rc & db->errMask;
74959 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
74961 assert( (rc & p->db->errMask)==rc );
74987 || (pAux->iAuxOp==iOp
74988 && pAux->iAuxArg>=0
74989 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
74991 testcase( pAux->iAuxArg==31 );
74992 if( pAux->xDeleteAux ){
74993 pAux->xDeleteAux(pAux->pAux);
74995 *pp = pAux->pNextAux;
74998 pp= &pAux->pNextAux;
75013 assert( p->db==0 || p->db==db );
75014 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
75015 for(pSub=p->pProgram; pSub; pSub=pNext){
75016 pNext = pSub->pNext;
75017 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
75020 if( p->magic!=VDBE_MAGIC_INIT ){
75021 releaseMemArray(p->aVar, p->nVar);
75022 sqlite3DbFree(db, p->pVList);
75023 sqlite3DbFree(db, p->pFree);
75025 vdbeFreeOpArray(db, p->aOp, p->nOp);
75026 sqlite3DbFree(db, p->aColName);
75027 sqlite3DbFree(db, p->zSql);
75031 for(i=0; i<p->nScan; i++){
75032 sqlite3DbFree(db, p->aScan[i].zName);
75034 sqlite3DbFree(db, p->aScan);
75046 db = p->db;
75047 assert( sqlite3_mutex_held(db->mutex) );
75049 if( p->pPrev ){
75050 p->pPrev->pNext = p->pNext;
75052 assert( db->pVdbe==p );
75053 db->pVdbe = p->pNext;
75055 if( p->pNext ){
75056 p->pNext->pPrev = p->pPrev;
75058 p->magic = VDBE_MAGIC_DEAD;
75059 p->db = 0;
75073 assert( p->deferredMoveto );
75074 assert( p->isTable );
75075 assert( p->eCurType==CURTYPE_BTREE );
75076 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
75082 p->deferredMoveto = 0;
75083 p->cacheStatus = CACHE_STALE;
75096 assert( p->eCurType==CURTYPE_BTREE );
75097 assert( p->uc.pCursor!=0 );
75098 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
75099 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
75100 p->cacheStatus = CACHE_STALE;
75101 if( isDifferentRow ) p->nullRow = 1;
75110 assert( p->eCurType==CURTYPE_BTREE );
75111 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
75128 ** not been deleted out from under the cursor, then this routine is a no-op.
75132 if( p->eCurType==CURTYPE_BTREE ){
75133 if( p->deferredMoveto ){
75135 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
75136 *pp = p->pAltCursor;
75137 *piCol = iMap - 1;
75142 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
75160 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
75167 ** serial-type and data blob separately.
75172 ** -------------- --------------- ---------------
75178 ** 5 6 signed integer
75184 ** N>=12 and even (N-12)/2 BLOB
75185 ** N>=13 and odd (N-13)/2 text
75192 ** Return the serial-type for the value stored in pMem.
75195 int flags = pMem->flags;
75205 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
75206 i64 i = pMem->u.i;
75225 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
75233 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
75234 assert( pMem->n>=0 );
75235 n = (u32)pMem->n;
75237 n += pMem->u.nZero;
75247 /* 0 1 2 3 4 5 6 7 8 9 */
75250 /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
75264 ** Return the length of the data corresponding to the supplied serial-type.
75268 return (serial_type-12)/2;
75271 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
75281 ** If we are on an architecture with mixed-endian floating
75285 ** For most architectures, this is a no-op.
75287 ** (later): It is reported to me that the mixed-endian problem
75289 ** that early versions of GCC stored the two words of a 64-bit
75297 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
75301 ** (2007-08-30) Frank van Vugt has studied this problem closely
75304 ** emulation that uses only 32-bit mantissas instead of a full
75305 ** 48-bits as required by the IEEE standard. (This is the
75308 ** the necessary byte swapping is carried out using a 64-bit integer
75309 ** rather than a 64-bit float. Frank assures us that the code here
75340 ** of the pMem->u.nZero bytes for a MEM_Zero value.
75343 ** of bytes in the zero-filled tail is included in the return value only
75354 assert( sizeof(v)==sizeof(pMem->u.r) );
75355 memcpy(&v, &pMem->u.r, sizeof(v));
75358 v = pMem->u.i;
75363 buf[--i] = (u8)(v&0xFF);
75371 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
75373 len = pMem->n;
75374 if( len>0 ) memcpy(buf, pMem->z, len);
75383 ** big-endian integer. Return the equivalent native integer
75409 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
75410 ** twos-complement integer. */
75411 pMem->u.i = *(i64*)&x;
75412 pMem->flags = MEM_Int;
75413 testcase( pMem->u.i<0 );
75415 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
75420 ** defined that 64-bit floating point values really are mixed
75429 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
75431 memcpy(&pMem->u.r, &x, sizeof(x));
75432 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
75445 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
75446 pMem->flags = MEM_Null;
75450 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
75452 pMem->u.i = ONE_BYTE_INT(buf);
75453 pMem->flags = MEM_Int;
75454 testcase( pMem->u.i<0 );
75457 case 2: { /* 2-byte signed integer */
75458 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
75459 ** twos-complement integer. */
75460 pMem->u.i = TWO_BYTE_INT(buf);
75461 pMem->flags = MEM_Int;
75462 testcase( pMem->u.i<0 );
75465 case 3: { /* 3-byte signed integer */
75466 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
75467 ** twos-complement integer. */
75468 pMem->u.i = THREE_BYTE_INT(buf);
75469 pMem->flags = MEM_Int;
75470 testcase( pMem->u.i<0 );
75473 case 4: { /* 4-byte signed integer */
75474 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
75475 ** twos-complement integer. */
75476 pMem->u.i = FOUR_BYTE_INT(buf);
75478 /* Work around a sign-extension bug in the HP compiler for HP/UX */
75479 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
75481 pMem->flags = MEM_Int;
75482 testcase( pMem->u.i<0 );
75485 case 5: { /* 6-byte signed integer */
75486 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
75487 ** twos-complement integer. */
75488 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
75489 pMem->flags = MEM_Int;
75490 testcase( pMem->u.i<0 );
75493 case 6: /* 8-byte signed integer */
75501 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
75502 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
75503 pMem->u.i = serial_type-8;
75504 pMem->flags = MEM_Int;
75508 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
75510 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
75511 ** (N-13)/2 bytes in length. */
75513 pMem->z = (char *)buf;
75514 pMem->n = (serial_type-12)/2;
75515 pMem->flags = aFlag[serial_type&1];
75516 return pMem->n;
75540 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
75541 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
75543 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
75544 assert( pKeyInfo->aSortOrder!=0 );
75545 p->pKeyInfo = pKeyInfo;
75546 p->nField = pKeyInfo->nField + 1;
75551 ** Given the nKey-byte encoding of a record in pKey[], populate the
75566 Mem *pMem = p->aMem;
75568 p->default_rc = 0;
75577 pMem->enc = pKeyInfo->enc;
75578 pMem->db = pKeyInfo->db;
75579 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
75580 pMem->szMalloc = 0;
75581 pMem->z = 0;
75584 if( (++u)>=p->nField ) break;
75586 assert( u<=pKeyInfo->nField + 1 );
75587 p->nField = u;
75616 pKeyInfo = pPKey2->pKeyInfo;
75617 if( pKeyInfo->db==0 ) return 1;
75618 mem1.enc = pKeyInfo->enc;
75619 mem1.db = pKeyInfo->db;
75635 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
75636 assert( pKeyInfo->aSortOrder!=0 );
75637 assert( pKeyInfo->nField>0 );
75663 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
75666 if( pKeyInfo->aSortOrder[i] ){
75667 rc = -rc; /* Invert the result for DESC sort order. */
75672 }while( idx1<szHdr1 && i<pPKey2->nField );
75683 rc = pPKey2->default_rc;
75690 if( pKeyInfo->db->mallocFailed ) return 1;
75699 ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
75701 ** If this constraint is not satisfied, it means that the high-speed
75725 assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
75735 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
75743 if( pMem1->enc==pColl->enc ){
75746 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
75752 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
75753 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
75756 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
75757 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
75762 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
75772 ** with MEM_Zero. Return true if it could be a zero-blob.
75789 int n1 = pB1->n;
75790 int n2 = pB2->n;
75792 /* It is possible to have a Blob value that has some non-zero content
75796 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
75797 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
75799 if( (pB1->flags|pB2->flags) & MEM_Zero ){
75800 if( pB1->flags & pB2->flags & MEM_Zero ){
75801 return pB1->u.nZero - pB2->u.nZero;
75802 }else if( pB1->flags & MEM_Zero ){
75803 if( !isAllZero(pB2->z, pB2->n) ) return -1;
75804 return pB1->u.nZero - n2;
75806 if( !isAllZero(pB1->z, pB1->n) ) return +1;
75807 return n1 - pB2->u.nZero;
75810 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
75812 return n1 - n2;
75816 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
75823 if( x<r ) return -1;
75829 if( r<-9223372036854775808.0 ) return +1;
75830 if( r>9223372036854775807.0 ) return -1;
75832 if( i<y ) return -1;
75834 if( y==SMALLEST_INT64 && r>0.0 ) return -1;
75838 if( s<r ) return -1;
75857 f1 = pMem1->flags;
75858 f2 = pMem2->flags;
75866 return (f2&MEM_Null) - (f1&MEM_Null);
75873 if( pMem1->u.i < pMem2->u.i ) return -1;
75874 if( pMem1->u.i > pMem2->u.i ) return +1;
75878 if( pMem1->u.r < pMem2->u.r ) return -1;
75879 if( pMem1->u.r > pMem2->u.r ) return +1;
75884 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
75886 return -1;
75891 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
75893 return -1;
75907 return -1;
75910 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
75911 assert( pMem1->enc==SQLITE_UTF8 ||
75912 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
75918 assert( !pColl || pColl->xCmp );
75933 ** The first argument passed to this function is a serial-type that
75934 ** corresponds to an integer - all values between 1 and 9 inclusive
75958 case 5: {
75970 return (serial_type - 8);
75982 ** If argument bSkip is non-zero, it is assumed that the caller has already
75986 ** fields that appear in both keys are equal, then pPKey2->default_rc is
75989 ** If database corruption is discovered, set pPKey2->errCode to
75991 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
75992 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
76004 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
76005 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
76023 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
76030 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
76032 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
76033 assert( pPKey2->pKeyInfo->nField>0 );
76039 if( pRhs->flags & MEM_Int ){
76045 rc = -1;
76048 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
76051 i64 rhs = pRhs->u.i;
76053 rc = -1;
76061 else if( pRhs->flags & MEM_Real ){
76070 rc = -1;
76074 if( mem1.u.r<pRhs->u.r ){
76075 rc = -1;
76076 }else if( mem1.u.r>pRhs->u.r ){
76080 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
76086 else if( pRhs->flags & MEM_Str ){
76090 rc = -1;
76094 mem1.n = (serial_type - 12) / 2;
76098 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
76100 }else if( pKeyInfo->aColl[i] ){
76101 mem1.enc = pKeyInfo->enc;
76102 mem1.db = pKeyInfo->db;
76106 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
76109 int nCmp = MIN(mem1.n, pRhs->n);
76110 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
76111 if( rc==0 ) rc = mem1.n - pRhs->n;
76117 else if( pRhs->flags & MEM_Blob ){
76118 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
76122 rc = -1;
76124 int nStr = (serial_type - 12) / 2;
76128 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
76130 }else if( pRhs->flags & MEM_Zero ){
76134 rc = nStr - pRhs->u.nZero;
76137 int nCmp = MIN(nStr, pRhs->n);
76138 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
76139 if( rc==0 ) rc = nStr - pRhs->n;
76151 if( pKeyInfo->aSortOrder[i] ){
76152 rc = -rc;
76163 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
76174 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
76175 || pKeyInfo->db->mallocFailed
76177 pPKey2->eqSeen = 1;
76178 return pPKey2->default_rc;
76191 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
76209 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
76212 case 1: { /* 1-byte signed integer */
76217 case 2: { /* 2-byte signed integer */
76222 case 3: { /* 3-byte signed integer */
76227 case 4: { /* 4-byte signed integer */
76233 case 5: { /* 6-byte signed integer */
76238 case 6: { /* 8-byte signed integer */
76265 v = pPKey2->aMem[0].u.i;
76267 res = pPKey2->r1;
76269 res = pPKey2->r2;
76270 }else if( pPKey2->nField>1 ){
76276 ** fields. Return pPKey2->default_rc in this case. */
76277 res = pPKey2->default_rc;
76278 pPKey2->eqSeen = 1;
76288 ** uses the collation sequence BINARY and (c) that the size-of-header varint
76299 assert( pPKey2->aMem[0].flags & MEM_Str );
76300 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
76303 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
76305 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
76311 nStr = (serial_type-12) / 2;
76313 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
76316 nCmp = MIN( pPKey2->aMem[0].n, nStr );
76317 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
76320 res = nStr - pPKey2->aMem[0].n;
76322 if( pPKey2->nField>1 ){
76325 res = pPKey2->default_rc;
76326 pPKey2->eqSeen = 1;
76329 res = pPKey2->r2;
76331 res = pPKey2->r1;
76334 res = pPKey2->r2;
76336 res = pPKey2->r1;
76342 || pPKey2->pKeyInfo->db->mallocFailed
76354 ** that the size-of-header varint that occurs at the start of each record
76365 ** header size is (12*5 + 1 + 1) bytes. */
76366 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
76367 int flags = p->aMem[0].flags;
76368 if( p->pKeyInfo->aSortOrder[0] ){
76369 p->r1 = 1;
76370 p->r2 = -1;
76372 p->r1 = -1;
76373 p->r2 = 1;
76381 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
76407 ** than 2GiB are support - anything large must be database corruption.
76409 ** this code can safely assume that nCellKey is 32-bits
76430 /* The last field of the index should be an integer - the ROWID.
76432 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
76437 testcase( typeRowid==5 );
76451 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
76486 assert( pC->eCurType==CURTYPE_BTREE );
76487 pCur = pC->uc.pCursor;
76511 assert( sqlite3_mutex_held(db->mutex) );
76512 db->nChange = nChange;
76513 db->nTotalChange += nChange;
76521 v->changeCntOn = 1;
76530 ** programs obsolete. Removing user-defined functions or collating
76536 for(p = db->pVdbe; p; p=p->pNext){
76537 p->expired = 1;
76545 return v->db;
76552 return v->prepFlags;
76566 Mem *pMem = &v->aVar[iVar-1];
76567 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
76568 if( 0==(pMem->flags & MEM_Null) ){
76569 sqlite3_value *pRet = sqlite3ValueNew(v->db);
76582 ** to sqlite3_reoptimize() that re-preparing the statement may result
76587 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
76589 v->expmask |= 0x80000000;
76591 v->expmask |= ((u32)1 << (iVar-1));
76600 ** throw an error if it is given inputs that would make it non-deterministic.
76601 ** This routine is invoked by date/time functions that use non-deterministic
76606 if( pCtx->pVdbe==0 ) return 1;
76608 if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
76610 "non-deterministic function in index expression or CHECK constraint",
76611 -1);
76624 if( pVtab->zErrMsg ){
76625 sqlite3 *db = p->db;
76626 sqlite3DbFree(db, p->zErrMsg);
76627 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
76628 sqlite3_free(pVtab->zErrMsg);
76629 pVtab->zErrMsg = 0;
76638 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
76648 Mem *pMem = &p->aMem[i];
76649 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
76658 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
76664 Vdbe *v, /* Vdbe pre-update hook is invoked by */
76672 sqlite3 *db = v->db;
76675 const char *zTbl = pTab->zName;
76678 assert( db->pPreUpdate==0 );
76685 iKey2 = v->aMem[iReg].u.i;
76691 assert( pCsr->nField==pTab->nCol
76692 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
76701 preupdate.keyinfo.nField = pTab->nCol;
76707 db->pPreUpdate = &preupdate;
76708 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
76709 db->pPreUpdate = 0;
76715 for(i=0; i<pCsr->nField; i++){
76745 ** Return TRUE (non-zero) of the statement supplied as an argument needs
76754 return p==0 || p->expired;
76764 if( p->db==0 ){
76788 assert( p->startTime>0 );
76789 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
76790 assert( db->init.busy==0 );
76791 assert( p->zSql!=0 );
76792 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
76793 iElapse = (iNow - p->startTime)*1000000;
76794 if( db->xProfile ){
76795 db->xProfile(db->pProfileArg, p->zSql, iElapse);
76797 if( db->mTrace & SQLITE_TRACE_PROFILE ){
76798 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
76800 p->startTime = 0;
76807 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
76809 # define checkProfileCallback(DB,P) /*no-op*/
76824 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
76825 ** pointer is a harmless no-op. */
76829 sqlite3 *db = v->db;
76831 sqlite3_mutex_enter(db->mutex);
76854 sqlite3 *db = v->db;
76855 sqlite3_mutex_enter(db->mutex);
76859 assert( (rc & (db->errMask))==rc );
76861 sqlite3_mutex_leave(db->mutex);
76874 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
76877 for(i=0; i<p->nVar; i++){
76878 sqlite3VdbeMemRelease(&p->aVar[i]);
76879 p->aVar[i].flags = MEM_Null;
76881 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
76882 if( p->expmask ){
76883 p->expired = 1;
76896 if( p->flags & (MEM_Blob|MEM_Str) ){
76898 assert( p->flags==MEM_Null && p->z==0 );
76901 p->flags |= MEM_Blob;
76902 return p->n ? p->z : 0;
76924 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
76928 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
76931 && p->eSubtype=='p'
76932 && strcmp(p->u.zPType, zPType)==0
76934 return (void*)p->z;
76953 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
76954 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
76992 return aType[pVal->flags&MEM_AffMask];
77004 pNew->flags &= ~MEM_Dyn;
77005 pNew->db = 0;
77006 if( pNew->flags&(MEM_Str|MEM_Blob) ){
77007 pNew->flags &= ~(MEM_Static|MEM_Dyn);
77008 pNew->flags |= MEM_Ephem;
77026 ** The following routines are used by user-defined functions to specify
77043 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
77070 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77079 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77088 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77089 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
77092 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77093 pCtx->isError = SQLITE_ERROR;
77094 pCtx->fErrorOrAux = 1;
77095 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
77099 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77100 pCtx->isError = SQLITE_ERROR;
77101 pCtx->fErrorOrAux = 1;
77102 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
77106 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77107 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
77110 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77111 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
77114 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77115 sqlite3VdbeMemSetNull(pCtx->pOut);
77123 Mem *pOut = pCtx->pOut;
77124 assert( sqlite3_mutex_held(pOut->db->mutex) );
77129 Mem *pOut = pCtx->pOut;
77130 assert( sqlite3_mutex_held(pOut->db->mutex) );
77131 pOut->eSubtype = eSubtype & 0xff;
77132 pOut->flags |= MEM_Subtype;
77140 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77150 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77166 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77175 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77184 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77189 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77190 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
77193 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77194 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
77197 Mem *pOut = pCtx->pOut;
77198 assert( sqlite3_mutex_held(pOut->db->mutex) );
77199 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
77202 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
77206 pCtx->isError = errCode;
77207 pCtx->fErrorOrAux = 1;
77209 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
77211 if( pCtx->pOut->flags & MEM_Null ){
77212 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
77219 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77220 pCtx->isError = SQLITE_TOOBIG;
77221 pCtx->fErrorOrAux = 1;
77222 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
77228 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77229 sqlite3VdbeMemSetNull(pCtx->pOut);
77230 pCtx->isError = SQLITE_NOMEM_BKPT;
77231 pCtx->fErrorOrAux = 1;
77232 sqlite3OomFault(pCtx->pOut->db);
77243 for(i=0; i<db->nDb; i++){
77244 Btree *pBt = db->aDb[i].pBt;
77250 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
77251 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
77274 if( p->magic!=VDBE_MAGIC_RUN ){
77279 ** This "automatic-reset" change is not technically an incompatibility,
77285 ** returns, and those were broken by the automatic-reset change. As a
77286 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
77292 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
77303 db = p->db;
77304 if( db->mallocFailed ){
77305 p->rc = SQLITE_NOMEM;
77309 if( p->pc<=0 && p->expired ){
77310 p->rc = SQLITE_SCHEMA;
77314 if( p->pc<0 ){
77319 if( db->nVdbeActive==0 ){
77320 db->u1.isInterrupted = 0;
77323 assert( db->nVdbeWrite>0 || db->autoCommit==0
77324 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
77328 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
77329 && !db->init.busy && p->zSql ){
77330 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
77332 assert( p->startTime==0 );
77336 db->nVdbeActive++;
77337 if( p->readOnly==0 ) db->nVdbeWrite++;
77338 if( p->bIsReader ) db->nVdbeRead++;
77339 p->pc = 0;
77342 p->rcApp = SQLITE_OK;
77345 if( p->explain ){
77350 db->nVdbeExec++;
77352 db->nVdbeExec--;
77361 assert( p->rc==SQLITE_OK );
77362 p->rc = doWalCallbacks(db);
77363 if( p->rc!=SQLITE_OK ){
77368 db->errCode = rc;
77369 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
77370 p->rc = SQLITE_NOMEM_BKPT;
77376 ** be one of the values in the first assert() below. Variable p->rc
77383 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
77384 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
77389 ** error has occurred, then return the error code in p->rc to the
77394 return (rc&db->errMask);
77398 ** This is the top-level implementation of sqlite3_step(). Call
77412 db = v->db;
77413 sqlite3_mutex_enter(db->mutex);
77414 v->doingRerun = 0;
77417 int savedPc = v->pc;
77421 if( savedPc>=0 ) v->doingRerun = 1;
77422 assert( v->expired==0 );
77433 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
77434 sqlite3DbFree(db, v->zErrMsg);
77435 if( !db->mallocFailed ){
77436 v->zErrMsg = sqlite3DbStrDup(db, zErr);
77437 v->rc = rc2;
77439 v->zErrMsg = 0;
77440 v->rc = rc = SQLITE_NOMEM_BKPT;
77444 sqlite3_mutex_leave(db->mutex);
77454 assert( p && p->pFunc );
77455 return p->pFunc->pUserData;
77462 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
77469 assert( p && p->pOut );
77470 return p->pOut->db;
77483 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
77484 assert( p->pVdbe!=0 );
77487 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
77490 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
77509 const char *zName = context->pFunc->zName;
77514 sqlite3_result_error(context, zErr, -1);
77520 ** its pMem->z element.
77523 Mem *pMem = p->pMem;
77524 assert( (pMem->flags & MEM_Agg)==0 );
77527 pMem->z = 0;
77530 pMem->flags = MEM_Agg;
77531 pMem->u.pDef = p->pFunc;
77532 if( pMem->z ){
77533 memset(pMem->z, 0, nByte);
77536 return (void*)pMem->z;
77545 assert( p && p->pFunc && p->pFunc->xFinalize );
77546 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
77548 if( (p->pMem->flags & MEM_Agg)==0 ){
77551 return (void*)p->pMem->z;
77557 ** the user-function defined by pCtx.
77559 ** The left-most argument is 0.
77568 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77570 if( pCtx->pVdbe==0 ) return 0;
77572 assert( pCtx->pVdbe!=0 );
77574 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
77575 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
77576 return pAuxData->pAux;
77584 ** argument to the user-function defined by pCtx. Any previous value is
77587 ** The left-most argument is 0.
77600 Vdbe *pVdbe = pCtx->pVdbe;
77602 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
77609 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
77610 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
77615 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
77617 pAuxData->iAuxOp = pCtx->iOp;
77618 pAuxData->iAuxArg = iArg;
77619 pAuxData->pNextAux = pVdbe->pAuxData;
77620 pVdbe->pAuxData = pAuxData;
77621 if( pCtx->fErrorOrAux==0 ){
77622 pCtx->isError = 0;
77623 pCtx->fErrorOrAux = 1;
77625 }else if( pAuxData->xDeleteAux ){
77626 pAuxData->xDeleteAux(pAuxData->pAux);
77629 pAuxData->pAux = pAux;
77630 pAuxData->xDeleteAux = xDelete;
77650 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
77651 return p->pMem->n;
77660 return pVm ? pVm->nResColumn : 0;
77669 if( pVm==0 || pVm->pResultSet==0 ) return 0;
77670 return pVm->nResColumn;
77679 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
77680 ** instead of an 8-byte one. This all works fine, except that when
77682 ** that a Mem structure is located on an 8-byte boundary. To prevent
77684 ** using gcc, we force nullMem to be 8-byte aligned using the magical
77722 assert( pVm->db );
77723 sqlite3_mutex_enter(pVm->db->mutex);
77724 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
77725 pOut = &pVm->pResultSet[i];
77727 sqlite3Error(pVm->db, SQLITE_RANGE);
77760 assert( p->db!=0 );
77761 assert( sqlite3_mutex_held(p->db->mutex) );
77762 p->rc = sqlite3ApiExit(p->db, p->rc);
77763 sqlite3_mutex_leave(p->db->mutex);
77813 if( pOut->flags&MEM_Static ){
77814 pOut->flags &= ~MEM_Static;
77815 pOut->flags |= MEM_Ephem;
77834 ** Convert the N-th element of pStmt->pColName[] into a string using
77837 ** There are up to 5 names for each column. useType determines which
77867 db = p->db;
77872 sqlite3_mutex_enter(db->mutex);
77873 assert( db->mallocFailed==0 );
77874 ret = xFunc(&p->aColName[N]);
77878 if( db->mallocFailed ){
77882 sqlite3_mutex_leave(db->mutex);
77991 ** The error code stored in database p->db is overwritten with the return
77999 sqlite3_mutex_enter(p->db->mutex);
78000 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
78001 sqlite3Error(p->db, SQLITE_MISUSE);
78002 sqlite3_mutex_leave(p->db->mutex);
78004 "bind on a busy prepared statement: [%s]", p->zSql);
78007 if( i<1 || i>p->nVar ){
78008 sqlite3Error(p->db, SQLITE_RANGE);
78009 sqlite3_mutex_leave(p->db->mutex);
78012 i--;
78013 pVar = &p->aVar[i];
78015 pVar->flags = MEM_Null;
78016 sqlite3Error(p->db, SQLITE_OK);
78021 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
78027 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
78028 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
78029 p->expired = 1;
78052 pVar = &p->aVar[i-1];
78055 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
78058 sqlite3Error(p->db, rc);
78059 rc = sqlite3ApiExit(p->db, rc);
78062 sqlite3_mutex_leave(p->db->mutex);
78104 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
78105 sqlite3_mutex_leave(p->db->mutex);
78117 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
78118 sqlite3_mutex_leave(p->db->mutex);
78127 sqlite3_mutex_leave(p->db->mutex);
78142 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
78143 sqlite3_mutex_leave(p->db->mutex);
78189 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
78193 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
78197 if( pValue->flags & MEM_Zero ){
78198 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
78200 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
78205 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
78206 pValue->enc);
78221 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
78222 sqlite3_mutex_leave(p->db->mutex);
78229 sqlite3_mutex_enter(p->db->mutex);
78230 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
78236 rc = sqlite3ApiExit(p->db, rc);
78237 sqlite3_mutex_leave(p->db->mutex);
78247 return p ? p->nVar : 0;
78254 ** The result is always UTF-8.
78259 return sqlite3VListNumToName(p->pVList, i);
78269 return sqlite3VListNameToNum(p->pVList, zName, nName);
78282 assert( pTo->db==pFrom->db );
78283 assert( pTo->nVar==pFrom->nVar );
78284 sqlite3_mutex_enter(pTo->db->mutex);
78285 for(i=0; i<pFrom->nVar; i++){
78286 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
78288 sqlite3_mutex_leave(pTo->db->mutex);
78308 if( pFrom->nVar!=pTo->nVar ){
78311 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
78312 if( pTo->expmask ){
78313 pTo->expired = 1;
78315 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
78316 if( pFrom->expmask ){
78317 pFrom->expired = 1;
78330 return pStmt ? ((Vdbe*)pStmt)->db : 0;
78338 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
78346 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
78363 sqlite3_mutex_enter(pDb->mutex);
78365 pNext = (sqlite3_stmt*)pDb->pVdbe;
78367 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
78369 sqlite3_mutex_leave(pDb->mutex);
78386 sqlite3 *db = pVdbe->db;
78387 sqlite3_mutex_enter(db->mutex);
78389 db->pnBytesFreed = (int*)&v;
78392 db->pnBytesFreed = 0;
78393 sqlite3_mutex_leave(db->mutex);
78395 v = pVdbe->aCounter[op];
78396 if( resetFlag ) pVdbe->aCounter[op] = 0;
78406 return p ? p->zSql : 0;
78426 sqlite3_mutex_enter(p->db->mutex);
78428 sqlite3_mutex_leave(p->db->mutex);
78449 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
78456 ** This function is called from within a pre-update callback to retrieve
78460 PreUpdate *p = db->pPreUpdate;
78465 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
78466 if( !p || p->op==SQLITE_INSERT ){
78470 if( p->pPk ){
78471 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
78473 if( iIdx>=p->pCsr->nField || iIdx<0 ){
78479 if( p->pUnpacked==0 ){
78483 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
78486 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
78488 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
78489 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
78495 p->aRecord = aRec;
78498 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
78499 if( iIdx==p->pTab->iPKey ){
78500 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
78501 }else if( iIdx>=p->pUnpacked->nField ){
78503 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
78504 if( pMem->flags & MEM_Int ){
78517 ** This function is called from within a pre-update callback to retrieve
78521 PreUpdate *p = db->pPreUpdate;
78522 return (p ? p->keyinfo.nField : 0);
78528 ** This function is designed to be called from within a pre-update callback
78532 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
78533 ** top-level trigger etc.).
78539 PreUpdate *p = db->pPreUpdate;
78540 return (p ? p->v->nFrame : 0);
78546 ** This function is called from within a pre-update callback to retrieve
78550 PreUpdate *p = db->pPreUpdate;
78554 if( !p || p->op==SQLITE_DELETE ){
78558 if( p->pPk && p->op!=SQLITE_UPDATE ){
78559 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
78561 if( iIdx>=p->pCsr->nField || iIdx<0 ){
78566 if( p->op==SQLITE_INSERT ){
78567 /* For an INSERT, memory cell p->iNewReg contains the serialized record
78569 UnpackedRecord *pUnpack = p->pNewUnpacked;
78571 Mem *pData = &p->v->aMem[p->iNewReg];
78574 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
78579 p->pNewUnpacked = pUnpack;
78581 pMem = &pUnpack->aMem[iIdx];
78582 if( iIdx==p->pTab->iPKey ){
78583 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
78584 }else if( iIdx>=pUnpack->nField ){
78588 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
78593 assert( p->op==SQLITE_UPDATE );
78594 if( !p->aNew ){
78595 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
78596 if( !p->aNew ){
78601 assert( iIdx>=0 && iIdx<p->pCsr->nField );
78602 pMem = &p->aNew[iIdx];
78603 if( pMem->flags==0 ){
78604 if( iIdx==p->pTab->iPKey ){
78605 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
78607 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
78632 if( idx<0 || idx>=p->nScan ) return 1;
78633 pScan = &p->aScan[idx];
78636 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
78640 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
78645 LogEst x = pScan->nEst;
78654 *(const char**)pOut = pScan->zName;
78658 if( pScan->addrExplain ){
78659 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
78666 if( pScan->addrExplain ){
78667 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
78669 *(int*)pOut = -1;
78685 memset(p->anExec, 0, p->nOp * sizeof(i64));
78706 ** The Vdbe parse-tree explainer is also found here.
78714 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
78739 ** This function returns a pointer to a nul-terminated string in memory
78743 ** then the returned string holds a copy of zRawSql with "-- " prepended
78749 ** with large (multi-megabyte) strings and blobs.
78759 ** parameter index is known, locate the value in p->aVar[]. Then render
78779 db = p->db;
78781 db->aLimit[SQLITE_LIMIT_LENGTH]);
78782 if( db->nVdbeExec>1 ){
78786 sqlite3StrAccumAppend(&out, "-- ", 3);
78787 assert( (zRawSql - zStart) > 0 );
78788 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
78790 }else if( p->nVar==0 ){
78819 assert( idx>0 && idx<=p->nVar );
78820 pVar = &p->aVar[idx-1];
78821 if( pVar->flags & MEM_Null ){
78823 }else if( pVar->flags & MEM_Int ){
78824 sqlite3XPrintf(&out, "%lld", pVar->u.i);
78825 }else if( pVar->flags & MEM_Real ){
78826 sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
78827 }else if( pVar->flags & MEM_Str ){
78834 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
78842 nOut = pVar->n;
78846 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
78849 sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
78851 if( nOut<pVar->n ){
78852 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
78858 }else if( pVar->flags & MEM_Zero ){
78859 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
78862 assert( pVar->flags & MEM_Blob );
78864 nOut = pVar->n;
78869 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
78873 if( nOut<pVar->n ){
78874 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
78963 ** use this information to make sure that the zero-blob functionality
78970 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
78971 sqlite3_max_blobsize = p->n;
78981 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
78983 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
79026 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
79043 ** already. Return non-zero if a malloc() fails.
79046 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
79061 if( ((P)->flags&MEM_Ephem)!=0 \
79065 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
79075 int iDb, /* Database the cursor belongs to, or -1 */
79093 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
79094 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
79096 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
79104 assert( iCur>=0 && iCur<p->nCursor );
79105 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
79106 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
79107 p->apCsr[iCur] = 0;
79110 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
79112 pCx->eCurType = eCurType;
79113 pCx->iDb = iDb;
79114 pCx->nField = nField;
79115 pCx->aOffset = &pCx->aType[nField];
79117 pCx->uc.pCursor = (BtCursor*)
79118 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
79119 sqlite3BtreeCursorZero(pCx->uc.pCursor);
79143 u8 enc = pRec->enc;
79144 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
79145 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
79146 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
79147 pRec->u.i = iValue;
79148 pRec->flags |= MEM_Int;
79150 pRec->u.r = rValue;
79151 pRec->flags |= MEM_Real;
79163 ** floating-point representation if an integer representation
79172 ** No-op. pRec is unchanged.
79182 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
79183 if( (pRec->flags & MEM_Real)==0 ){
79184 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
79195 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
79196 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
79200 pRec->flags &= ~(MEM_Real|MEM_Int);
79235 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
79239 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
79240 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
79241 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
79244 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
79254 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
79255 ** But it does set pMem->u.r and pMem->u.i appropriately.
79258 if( pMem->flags & (MEM_Int|MEM_Real) ){
79259 return pMem->flags & (MEM_Int|MEM_Real);
79261 if( pMem->flags & (MEM_Str|MEM_Blob) ){
79274 int f = pMem->flags;
79294 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
79296 for(i=0; i<16 && i<pMem->n; i++){
79297 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
79300 for(i=0; i<16 && i<pMem->n; i++){
79301 char z = pMem->z[i];
79307 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
79327 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
79330 for(j=0; j<15 && j<pMem->n; j++){
79331 u8 c = pMem->z[j];
79339 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
79351 if( p->flags & MEM_Undefined ){
79353 }else if( p->flags & MEM_Null ){
79355 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
79356 printf(" si:%lld", p->u.i);
79357 }else if( p->flags & MEM_Int ){
79358 printf(" i:%lld", p->u.i);
79360 }else if( p->flags & MEM_Real ){
79361 printf(" r:%g", p->u.r);
79363 }else if( p->flags & MEM_RowSet ){
79370 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
79381 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
79391 ** high-performance timing routines.
79407 ** This file contains inline asm code for retrieving "high-performance"
79414 ** The following routine only works on pentium-class (or newer) processors.
79416 ** processor and returns that value. This can be used for high-res
79490 ** the number of non-transaction savepoints currently in the
79500 for(p=db->pSavepoint; p; p=p->pNext) n++;
79501 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
79507 ** Return the register of pOp->p2 after first preparing it to be
79512 pOut->flags = MEM_Int;
79517 assert( pOp->p2>0 );
79518 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
79519 pOut = &p->aMem[pOp->p2];
79521 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
79524 pOut->flags = MEM_Int;
79537 Op *aOp = p->aOp; /* Copy of p->aOp */
79546 sqlite3 *db = p->db; /* The database */
79554 Mem *aMem = p->aMem; /* Copy of p->aMem */
79564 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
79566 if( p->rc==SQLITE_NOMEM ){
79571 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
79572 assert( p->bIsReader || p->readOnly!=0 );
79573 p->iCurrentTime = 0;
79574 assert( p->explain==0 );
79575 p->pResultSet = 0;
79576 db->busyHandler.nBusy = 0;
79577 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
79580 if( db->xProgress ){
79581 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
79582 assert( 0 < db->nProgressOps );
79583 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
79590 if( p->pc==0
79591 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
79596 if( p->db->flags & SQLITE_VdbeListing ){
79598 for(i=0; i<p->nOp; i++){
79602 if( p->db->flags & SQLITE_VdbeEQP ){
79603 for(i=0; i<p->nOp; i++){
79611 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
79615 for(pOp=&aOp[p->pc]; 1; pOp++){
79620 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
79626 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
79632 if( db->flags & SQLITE_VdbeTrace ){
79633 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
79643 sqlite3_interrupt_count--;
79653 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
79655 assert( pOp->p1>0 );
79656 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
79657 assert( memIsValid(&aMem[pOp->p1]) );
79658 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
79659 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
79662 assert( pOp->p2>0 );
79663 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
79664 assert( memIsValid(&aMem[pOp->p2]) );
79665 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
79666 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
79669 assert( pOp->p3>0 );
79670 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
79671 assert( memIsValid(&aMem[pOp->p3]) );
79672 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
79673 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
79676 assert( pOp->p2>0 );
79677 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
79678 memAboutToChange(p, &aMem[pOp->p2]);
79681 assert( pOp->p3>0 );
79682 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
79683 memAboutToChange(p, &aMem[pOp->p3]);
79691 switch( pOp->opcode ){
79698 ** the switch statement will break with convention and be flush-left. Another
79736 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
79742 pOp = &aOp[pOp->p2 - 1];
79753 ** faster according to "valgrind --tool=cachegrind" */
79755 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
79760 ** If the progress callback returns non-zero, exit the virtual machine with
79763 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
79764 assert( db->nProgressOps!=0 );
79765 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
79766 if( db->xProgress(db->pProgressArg) ){
79782 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
79783 pIn1 = &aMem[pOp->p1];
79786 pIn1->flags = MEM_Int;
79787 pIn1->u.i = (int)(pOp-aOp);
79788 REGISTER_TRACE(pOp->p1, pIn1);
79793 pOp = &aOp[pOp->p2 - 1];
79803 pIn1 = &aMem[pOp->p1];
79804 assert( pIn1->flags==MEM_Int );
79805 pOp = &aOp[pIn1->u.i];
79806 pIn1->flags = MEM_Undefined;
79822 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
79823 assert( pOp->p2>=0 && pOp->p2<p->nOp );
79824 assert( pOp->p3>=0 && pOp->p3<p->nOp );
79825 pOut = &aMem[pOp->p1];
79827 pOut->u.i = pOp->p3 - 1;
79828 pOut->flags = MEM_Int;
79829 if( pOp->p2 ) goto jump_to_p2;
79843 pIn1 = &aMem[pOp->p1];
79844 assert( pIn1->flags==MEM_Int );
79845 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
79846 pCaller = &aOp[pIn1->u.i];
79847 assert( pCaller->opcode==OP_Yield );
79848 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
79849 pOp = &aOp[pCaller->p2 - 1];
79850 pIn1->flags = MEM_Undefined;
79869 pIn1 = &aMem[pOp->p1];
79871 pIn1->flags = MEM_Int;
79872 pcDest = (int)pIn1->u.i;
79873 pIn1->u.i = (int)(pOp - aOp);
79874 REGISTER_TRACE(pOp->p1, pIn1);
79884 ** value in register P3 is not NULL, then this routine is a no-op.
79888 pIn3 = &aMem[pOp->p3];
79889 if( (pIn3->flags & MEM_Null)==0 ) break;
79927 pcx = (int)(pOp - aOp);
79928 if( pOp->p1==SQLITE_OK && p->pFrame ){
79929 /* Halt the sub-program. Return control to the parent frame. */
79930 pFrame = p->pFrame;
79931 p->pFrame = pFrame->pParent;
79932 p->nFrame--;
79933 sqlite3VdbeSetChanges(db, p->nChange);
79935 if( pOp->p2==OE_Ignore ){
79936 /* Instruction pcx is the OP_Program that invoked the sub-program
79938 ** instruction is set to OE_Ignore, then the sub-program is throwing
79941 pcx = p->aOp[pcx].p2-1;
79943 aOp = p->aOp;
79944 aMem = p->aMem;
79948 p->rc = pOp->p1;
79949 p->errorAction = (u8)pOp->p2;
79950 p->pc = pcx;
79951 assert( pOp->p5<=4 );
79952 if( p->rc ){
79953 if( pOp->p5 ){
79956 testcase( pOp->p5==1 );
79957 testcase( pOp->p5==2 );
79958 testcase( pOp->p5==3 );
79959 testcase( pOp->p5==4 );
79960 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
79961 if( pOp->p4.z ){
79962 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
79965 sqlite3VdbeError(p, "%s", pOp->p4.z);
79967 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
79972 p->rc = SQLITE_BUSY;
79974 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
79975 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
79976 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
79984 ** The 32-bit integer value P1 is written into register P2.
79988 pOut->u.i = pOp->p1;
79995 ** P4 is a pointer to a 64-bit integer value.
80000 assert( pOp->p4.pI64!=0 );
80001 pOut->u.i = *pOp->p4.pI64;
80009 ** P4 is a pointer to a 64-bit floating point value.
80014 pOut->flags = MEM_Real;
80015 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
80016 pOut->u.r = *pOp->p4.pReal;
80024 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
80030 assert( pOp->p4.z!=0 );
80032 pOp->opcode = OP_String;
80033 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
80037 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
80040 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
80042 pOut->szMalloc = 0;
80043 pOut->flags |= MEM_Static;
80044 if( pOp->p4type==P4_DYNAMIC ){
80045 sqlite3DbFree(db, pOp->p4.z);
80047 pOp->p4type = P4_DYNAMIC;
80048 pOp->p4.z = pOut->z;
80049 pOp->p1 = pOut->n;
80053 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
80073 assert( pOp->p4.z!=0 );
80075 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
80076 pOut->z = pOp->p4.z;
80077 pOut->n = pOp->p1;
80078 pOut->enc = encoding;
80081 if( pOp->p3>0 ){
80082 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
80083 pIn3 = &aMem[pOp->p3];
80084 assert( pIn3->flags & MEM_Int );
80085 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
80099 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
80107 cnt = pOp->p3-pOp->p2;
80108 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
80109 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
80110 pOut->n = 0;
80115 pOut->flags = nullFlag;
80116 pOut->n = 0;
80117 cnt--;
80131 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
80132 pOut = &aMem[pOp->p1];
80133 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
80144 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
80146 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
80147 pOut->enc = encoding;
80163 assert( pOp->p1>0 && pOp->p1<=p->nVar );
80164 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
80165 pVar = &p->aVar[pOp->p1 - 1];
80169 pOut = &aMem[pOp->p2];
80178 ** Move the P3 values in register P1..P1+P3-1 over into
80179 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
80181 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
80189 n = pOp->p3;
80190 p1 = pOp->p1;
80191 p2 = pOp->p2;
80198 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
80199 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
80204 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
80205 pOut->pScopyFrom += pOp->p2 - p1;
80212 }while( --n );
80227 n = pOp->p3;
80228 pIn1 = &aMem[pOp->p1];
80229 pOut = &aMem[pOp->p2];
80235 pOut->pScopyFrom = 0;
80237 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
80238 if( (n--)==0 ) break;
80259 pIn1 = &aMem[pOp->p1];
80260 pOut = &aMem[pOp->p2];
80264 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
80278 pIn1 = &aMem[pOp->p1];
80279 assert( (pIn1->flags & MEM_Int)!=0 );
80280 pOut = &aMem[pOp->p2];
80281 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
80288 ** The registers P1 through P1+P2-1 contain a single row of
80291 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
80297 assert( p->nResColumn==pOp->p2 );
80298 assert( pOp->p1>0 );
80299 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
80304 if( db->xProgress!=0
80306 && db->xProgress(db->pProgressArg)!=0
80317 assert( db->flags&SQLITE_CountRows );
80318 assert( p->usesStmtJournal );
80329 ** ensure that statement-transactions are always nested, not overlapping.
80330 ** If the open statement-transaction is not closed here, then the user
80334 ** The statement transaction is never a top-level transaction. Hence
80337 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
80342 p->cacheCtr = (p->cacheCtr + 2)|1;
80345 ** and have an assigned type. The results are de-ephemeralized as
80348 pMem = p->pResultSet = &aMem[pOp->p1];
80349 for(i=0; i<pOp->p2; i++){
80355 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
80357 if( db->mallocFailed ) goto no_mem;
80359 if( db->mTrace & SQLITE_TRACE_ROW ){
80360 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
80365 p->pc = (int)(pOp - aOp) + 1;
80386 pIn1 = &aMem[pOp->p1];
80387 pIn2 = &aMem[pOp->p2];
80388 pOut = &aMem[pOp->p3];
80390 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
80397 nByte = pIn1->n + pIn2->n;
80398 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
80406 memcpy(pOut->z, pIn2->z, pIn2->n);
80408 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
80409 pOut->z[nByte]=0;
80410 pOut->z[nByte+1] = 0;
80411 pOut->flags |= MEM_Term;
80412 pOut->n = (int)nByte;
80413 pOut->enc = encoding;
80434 ** Synopsis: r[P3]=r[P2]-r[P1]
80470 pIn1 = &aMem[pOp->p1];
80472 pIn2 = &aMem[pOp->p2];
80474 pOut = &aMem[pOp->p3];
80475 flags = pIn1->flags | pIn2->flags;
80477 iA = pIn1->u.i;
80478 iB = pIn2->u.i;
80480 switch( pOp->opcode ){
80486 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
80492 if( iA==-1 ) iA = 1;
80497 pOut->u.i = iB;
80506 switch( pOp->opcode ){
80508 case OP_Subtract: rB -= rA; break;
80520 if( iA==-1 ) iA = 1;
80526 pOut->u.i = rB;
80532 pOut->u.r = rB;
80550 ** be returned. This is used by the built-in min(), max() and nullif()
80559 ** publicly. Only built-in functions have access to this feature.
80562 assert( pOp->p4type==P4_COLLSEQ );
80563 if( pOp->p1 ){
80564 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
80572 ** Take the bit-wise AND of the values in register P1 and P2 and
80579 ** Take the bit-wise OR of the values in register P1 and P2 and
80608 pIn1 = &aMem[pOp->p1];
80609 pIn2 = &aMem[pOp->p2];
80610 pOut = &aMem[pOp->p3];
80611 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
80617 op = pOp->opcode;
80628 op = 2*OP_ShiftLeft + 1 - op;
80629 iB = iB>(-64) ? -iB : 64;
80633 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
80640 /* Sign-extend on a right shift of a negative number */
80641 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
80646 pOut->u.i = iA;
80660 pIn1 = &aMem[pOp->p1];
80663 pIn1->u.i += pOp->p2;
80675 pIn1 = &aMem[pOp->p1];
80676 if( (pIn1->flags & MEM_Int)==0 ){
80678 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
80679 if( (pIn1->flags & MEM_Int)==0 ){
80680 if( pOp->p2==0 ){
80703 pIn1 = &aMem[pOp->p1];
80704 if( pIn1->flags & MEM_Int ){
80728 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
80729 testcase( pOp->p2==SQLITE_AFF_TEXT );
80730 testcase( pOp->p2==SQLITE_AFF_BLOB );
80731 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
80732 testcase( pOp->p2==SQLITE_AFF_INTEGER );
80733 testcase( pOp->p2==SQLITE_AFF_REAL );
80734 pIn1 = &aMem[pOp->p1];
80737 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
80751 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
80801 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
80848 u16 flags1; /* Copy of initial value of pIn1->flags */
80849 u16 flags3; /* Copy of initial value of pIn3->flags */
80851 pIn1 = &aMem[pOp->p1];
80852 pIn3 = &aMem[pOp->p3];
80853 flags1 = pIn1->flags;
80854 flags3 = pIn3->flags;
80857 if( pOp->p5 & SQLITE_NULLEQ ){
80862 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
80864 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
80877 if( pOp->p5 & SQLITE_STOREP2 ){
80878 pOut = &aMem[pOp->p2];
80882 REGISTER_TRACE(pOp->p2, pOut);
80885 if( pOp->p5 & SQLITE_JUMPIFNULL ){
80893 affinity = pOp->p5 & SQLITE_AFF_MASK;
80898 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
80899 flags3 = pIn3->flags;
80907 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
80908 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
80909 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
80915 testcase( pIn1->flags & MEM_Int );
80916 testcase( pIn1->flags & MEM_Real );
80918 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
80919 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
80923 testcase( pIn3->flags & MEM_Int );
80924 testcase( pIn3->flags & MEM_Real );
80926 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
80927 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
80930 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
80931 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
80934 switch( pOp->opcode ){
80944 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
80945 pIn1->flags = flags1;
80946 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
80947 pIn3->flags = flags3;
80949 if( pOp->p5 & SQLITE_STOREP2 ){
80950 pOut = &aMem[pOp->p2];
80953 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
80961 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
80963 testcase( res2==0 && pOp->opcode==OP_Eq );
80964 testcase( res2==1 && pOp->opcode==OP_Eq );
80965 testcase( res2==0 && pOp->opcode==OP_Ne );
80966 testcase( res2==1 && pOp->opcode==OP_Ne );
80967 if( (pOp->opcode==OP_Eq)==res2 ) break;
80971 pOut->u.i = res2;
80972 REGISTER_TRACE(pOp->p2, pOut);
80974 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
80992 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
80993 assert( pOp[-1].p5 & SQLITE_STOREP2 );
81013 assert( pOp->p4type==P4_INTARRAY );
81014 assert( pOp->p4.ai );
81021 ** Synopsis: r[P1@P3] <-> r[P2@P3]
81023 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
81024 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
81051 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
81055 assert( pOp[-1].opcode==OP_Permutation );
81056 assert( pOp[-1].p4type==P4_INTARRAY );
81057 aPermute = pOp[-1].p4.ai + 1;
81060 n = pOp->p3;
81061 pKeyInfo = pOp->p4.pKeyInfo;
81064 p1 = pOp->p1;
81065 p2 = pOp->p2;
81070 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
81071 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
81073 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
81074 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
81083 assert( i<pKeyInfo->nField );
81084 pColl = pKeyInfo->aColl[i];
81085 bRev = pKeyInfo->aSortOrder[i];
81088 if( bRev ) iCompare = -iCompare;
81103 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
81105 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
81107 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
81137 pIn1 = &aMem[pOp->p1];
81138 if( pIn1->flags & MEM_Null ){
81143 pIn2 = &aMem[pOp->p2];
81144 if( pIn2->flags & MEM_Null ){
81149 if( pOp->opcode==OP_And ){
81156 pOut = &aMem[pOp->p3];
81160 pOut->u.i = v1;
81174 pIn1 = &aMem[pOp->p1];
81175 pOut = &aMem[pOp->p2];
81177 if( (pIn1->flags & MEM_Null)==0 ){
81178 pOut->flags = MEM_Int;
81179 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
81188 ** ones-complement of the P1 value into register P2. If P1 holds
81192 pIn1 = &aMem[pOp->p1];
81193 pOut = &aMem[pOp->p2];
81195 if( (pIn1->flags & MEM_Null)==0 ){
81196 pOut->flags = MEM_Int;
81197 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
81205 ** encountered on each invocation of the byte-code program. Jump to P2
81208 ** Top-level programs determine first invocation by comparing the P1
81216 ** because the self-altering code trick does not work for recursive
81221 assert( p->aOp[0].opcode==OP_Init );
81222 if( p->pFrame ){
81223 iAddr = (int)(pOp - p->aOp);
81224 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
81228 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
81230 if( p->aOp[0].p1==pOp->p1 ){
81236 pOp->p1 = p->aOp[0].p1;
81243 ** is considered true if it is numeric and non-zero. If the value
81244 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
81250 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
81255 pIn1 = &aMem[pOp->p1];
81256 if( pIn1->flags & MEM_Null ){
81257 c = pOp->p3;
81264 if( pOp->opcode==OP_IfNot ) c = !c;
81279 pIn1 = &aMem[pOp->p1];
81280 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
81281 if( (pIn1->flags & MEM_Null)!=0 ){
81293 pIn1 = &aMem[pOp->p1];
81294 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
81295 if( (pIn1->flags & MEM_Null)==0 ){
81310 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
81311 assert( p->apCsr[pOp->p1]!=0 );
81312 if( p->apCsr[pOp->p1]->nullRow ){
81313 sqlite3VdbeMemSetNull(aMem + pOp->p3);
81324 ** information about the format of the data.) Extract the P2-th column
81334 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
81336 ** The first OP_Column against a pseudo-table after the value of the content
81348 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
81357 u64 offset64; /* 64-bit offset */
81362 pC = p->apCsr[pOp->p1];
81363 p2 = pOp->p2;
81366 ** the correct row) then bring it up-to-date by doing the necessary
81367 ** B-Tree seek. */
81371 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
81372 pDest = &aMem[pOp->p3];
81374 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
81376 assert( p2<pC->nField );
81377 aOffset = pC->aOffset;
81378 assert( pC->eCurType!=CURTYPE_VTAB );
81379 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
81380 assert( pC->eCurType!=CURTYPE_SORTER );
81382 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
81383 if( pC->nullRow ){
81384 if( pC->eCurType==CURTYPE_PSEUDO ){
81385 assert( pC->uc.pseudoTableReg>0 );
81386 pReg = &aMem[pC->uc.pseudoTableReg];
81387 assert( pReg->flags & MEM_Blob );
81389 pC->payloadSize = pC->szRow = avail = pReg->n;
81390 pC->aRow = (u8*)pReg->z;
81396 pCrsr = pC->uc.pCursor;
81397 assert( pC->eCurType==CURTYPE_BTREE );
81400 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
81401 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
81403 if( pC->payloadSize <= (u32)avail ){
81404 pC->szRow = pC->payloadSize;
81405 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
81408 pC->szRow = avail;
81411 pC->cacheStatus = p->cacheCtr;
81412 pC->iHdrOffset = getVarint32(pC->aRow, offset);
81413 pC->nHdrParsed = 0;
81417 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/
81418 /* pC->aRow does not have to hold the entire row, but it does at least
81419 ** need to cover the header of the record. If pC->aRow does not contain
81422 pC->aRow = 0;
81423 pC->szRow = 0;
81428 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
81431 ** 3-byte type for each of the maximum of 32768 columns plus three
81434 if( offset > 98307 || offset > pC->payloadSize ){
81438 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
81443 zData = pC->aRow;
81444 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
81450 ** parsed and valid information is in aOffset[] and pC->aType[].
81452 if( pC->nHdrParsed<=p2 ){
81454 ** to extract additional fields up through the p2+1-th field
81456 if( pC->iHdrOffset<aOffset[0] ){
81458 if( pC->aRow==0 ){
81460 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
81464 zData = pC->aRow;
81467 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
81469 i = pC->nHdrParsed;
81471 zHdr = zData + pC->iHdrOffset;
81481 pC->aType[i++] = t;
81490 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
81491 || (offset64 > pC->payloadSize)
81493 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
81498 pC->nHdrParsed = i;
81499 pC->iHdrOffset = (u32)(zHdr - zData);
81500 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
81509 if( pC->nHdrParsed<=p2 ){
81510 if( pOp->p4type==P4_MEM ){
81511 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
81518 t = pC->aType[p2];
81521 /* Extract the content for the p2+1-th column. Control can only
81522 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
81525 assert( p2<pC->nHdrParsed );
81531 assert( t==pC->aType[p2] );
81532 if( pC->szRow>=aOffset[p2+1] ){
81534 ** page - where the content is not on an overflow page */
81535 zData = pC->aRow + aOffset[p2];
81540 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
81544 pDest->n = len = (t-12)/2;
81545 pDest->enc = encoding;
81546 if( pDest->szMalloc < len+2 ){
81547 pDest->flags = MEM_Null;
81550 pDest->z = pDest->zMalloc;
81552 memcpy(pDest->z, zData, len);
81553 pDest->z[len] = 0;
81554 pDest->z[len+1] = 0;
81555 pDest->flags = aFlag[t&1];
81558 pDest->enc = encoding;
81560 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
81561 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
81578 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
81580 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
81581 pDest->flags &= ~MEM_Ephem;
81587 REGISTER_TRACE(pOp->p3, pDest);
81596 ** P4 is a string that is P2 characters long. The N-th character of the
81597 ** string indicates the column affinity that should be used for the N-th
81603 zAffinity = pOp->p4.z;
81605 assert( pOp->p2>0 );
81606 assert( zAffinity[pOp->p2]==0 );
81607 pIn1 = &aMem[pOp->p1];
81609 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
81624 ** P4 may be a string that is P2 characters long. The N-th character of the
81625 ** string indicates the column affinity that should be used for the N-th
81654 ** ------------------------------------------------------------------------
81655 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
81656 ** ------------------------------------------------------------------------
81663 ** hdr-size field is also a varint which is the offset from the beginning
81669 nField = pOp->p1;
81670 zAffinity = pOp->p4.z;
81671 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
81673 nField = pOp->p2;
81674 pLast = &pData0[nField-1];
81675 file_format = p->minWriteFileFormat;
81678 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
81679 pOut = &aMem[pOp->p3];
81696 ** have a non-NULL default value. Also, the record must be left with
81698 ** index of the right-most column with a non-NULL default value */
81699 if( pOp->p5 ){
81700 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
81701 pLast--;
81702 nField--;
81713 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
81714 if( pRec->flags & MEM_Zero ){
81718 nZero += pRec->u.nZero;
81719 len -= pRec->u.nZero;
81727 pRec--;
81730 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
81746 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
81751 ** the new record. The output register (pOp->p3) is not allowed to
81758 zNewRecord = (u8 *)pOut->z;
81766 serial_type = pRec->uTemp;
81767 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
81770 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
81777 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
81778 pOut->n = (int)nByte;
81779 pOut->flags = MEM_Blob;
81781 pOut->u.nZero = nZero;
81782 pOut->flags |= MEM_Zero;
81784 REGISTER_TRACE(pOp->p3, pOut);
81800 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
81801 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
81807 pOut->u.i = nEntry;
81828 p1 = pOp->p1;
81829 zName = pOp->p4.z;
81834 assert( db->pSavepoint==0 || db->autoCommit==0 );
81836 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
81838 assert( p->bIsReader );
81841 if( db->nVdbeWrite>0 ){
81845 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
81854 ** that the db->aVTrans[] array is empty. */
81855 assert( db->autoCommit==0 || db->nVTrans==0 );
81857 db->nStatement+db->nSavepoint);
81864 pNew->zName = (char *)&pNew[1];
81865 memcpy(pNew->zName, zName, nName+1);
81869 if( db->autoCommit ){
81870 db->autoCommit = 0;
81871 db->isTransactionSavepoint = 1;
81873 db->nSavepoint++;
81877 pNew->pNext = db->pSavepoint;
81878 db->pSavepoint = pNew;
81879 pNew->nDeferredCons = db->nDeferredCons;
81880 pNew->nDeferredImmCons = db->nDeferredImmCons;
81889 pSavepoint = db->pSavepoint;
81890 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
81891 pSavepoint = pSavepoint->pNext
81898 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
81902 sqlite3VdbeError(p, "cannot release savepoint - "
81911 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
81916 db->autoCommit = 1;
81918 p->pc = (int)(pOp - aOp);
81919 db->autoCommit = 0;
81920 p->rc = rc = SQLITE_BUSY;
81923 db->isTransactionSavepoint = 0;
81924 rc = p->rc;
81927 iSavepoint = db->nSavepoint - iSavepoint - 1;
81929 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
81930 for(ii=0; ii<db->nDb; ii++){
81931 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
81939 for(ii=0; ii<db->nDb; ii++){
81940 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
81948 db->flags = (db->flags | SQLITE_InternChanges);
81954 while( db->pSavepoint!=pSavepoint ){
81955 pTmp = db->pSavepoint;
81956 db->pSavepoint = pTmp->pNext;
81958 db->nSavepoint--;
81966 assert( pSavepoint==db->pSavepoint );
81967 db->pSavepoint = pSavepoint->pNext;
81970 db->nSavepoint--;
81973 db->nDeferredCons = pSavepoint->nDeferredCons;
81974 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
81990 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
82001 desiredAutoCommit = pOp->p1;
82002 iRollback = pOp->p2;
82005 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
82006 assert( p->bIsReader );
82008 if( desiredAutoCommit!=db->autoCommit ){
82012 db->autoCommit = 1;
82013 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
82017 sqlite3VdbeError(p, "cannot commit transaction - "
82024 db->autoCommit = (u8)desiredAutoCommit;
82027 p->pc = (int)(pOp - aOp);
82028 db->autoCommit = (u8)(1-desiredAutoCommit);
82029 p->rc = rc = SQLITE_BUSY;
82032 assert( db->nStatement==0 );
82034 if( p->rc==SQLITE_OK ){
82043 (iRollback)?"cannot rollback - no transaction is active":
82044 "cannot commit - no transaction is active"));
82056 ** If P2 is non-zero, then a write-transaction is started, or if a
82057 ** read-transaction is already active, it is upgraded to a write-transaction.
82058 ** If P2 is zero, then a read-transaction is started.
82065 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
82091 assert( p->bIsReader );
82092 assert( p->readOnly==0 || pOp->p2==0 );
82093 assert( pOp->p1>=0 && pOp->p1<db->nDb );
82094 assert( DbMaskTest(p->btreeMask, pOp->p1) );
82095 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
82099 pBt = db->aDb[pOp->p1].pBt;
82102 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
82107 p->pc = (int)(pOp - aOp);
82108 p->rc = rc;
82114 if( pOp->p2 && p->usesStmtJournal
82115 && (db->autoCommit==0 || db->nVdbeRead>1)
82118 if( p->iStatement==0 ){
82119 assert( db->nStatement>=0 && db->nSavepoint>=0 );
82120 db->nStatement++;
82121 p->iStatement = db->nSavepoint + db->nStatement;
82124 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
82126 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
82132 p->nStmtDefCons = db->nDeferredCons;
82133 p->nStmtDefImmCons = db->nDeferredImmCons;
82137 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
82142 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
82146 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
82147 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
82148 sqlite3DbFree(db, p->zErrMsg);
82149 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
82150 /* If the schema-cookie from the database file matches the cookie
82151 ** stored with the in-memory representation of the schema, do
82154 ** If virtual-tables are in use, this is not just an optimization.
82155 ** Often, v-tables store their data in other SQLite tables, which
82156 ** are queried from within xNext() and other v-table methods using
82157 ** prepared queries. If such a query is out-of-date, we do not want to
82159 ** v-table would have to be ready for the sqlite3_vtab structure itself
82161 ** a v-table method.
82163 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
82164 sqlite3ResetOneSchema(db, pOp->p1);
82166 p->expired = 1;
82181 ** There must be a read-lock on the database (either a transaction
82190 assert( p->bIsReader );
82191 iDb = pOp->p1;
82192 iCookie = pOp->p3;
82193 assert( pOp->p3<SQLITE_N_BTREE_META );
82194 assert( iDb>=0 && iDb<db->nDb );
82195 assert( db->aDb[iDb].pBt!=0 );
82196 assert( DbMaskTest(p->btreeMask, iDb) );
82198 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
82200 pOut->u.i = iMeta;
82216 assert( pOp->p2<SQLITE_N_BTREE_META );
82217 assert( pOp->p1>=0 && pOp->p1<db->nDb );
82218 assert( DbMaskTest(p->btreeMask, pOp->p1) );
82219 assert( p->readOnly==0 );
82220 pDb = &db->aDb[pOp->p1];
82221 assert( pDb->pBt!=0 );
82222 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
82224 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
82225 if( pOp->p2==BTREE_SCHEMA_VERSION ){
82227 pDb->pSchema->schema_cookie = pOp->p3;
82228 db->flags |= SQLITE_InternChanges;
82229 }else if( pOp->p2==BTREE_FILE_FORMAT ){
82231 pDb->pSchema->file_format = pOp->p3;
82233 if( pOp->p1==1 ){
82237 p->expired = 0;
82246 ** Open a read-only cursor for the database table whose root page is
82279 ** number of P2 and if it is this opcode becomes a no-op. In other words,
82303 ** in read/write mode. For a given table, there can be one or more read-only
82318 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
82319 assert( pOp->p4type==P4_KEYINFO );
82320 pCur = p->apCsr[pOp->p1];
82321 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
82322 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
82330 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
82331 assert( p->bIsReader );
82332 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
82333 || p->readOnly==0 );
82335 if( p->expired ){
82342 p2 = pOp->p2;
82343 iDb = pOp->p3;
82344 assert( iDb>=0 && iDb<db->nDb );
82345 assert( DbMaskTest(p->btreeMask, iDb) );
82346 pDb = &db->aDb[iDb];
82347 pX = pDb->pBt;
82349 if( pOp->opcode==OP_OpenWrite ){
82351 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
82353 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
82354 p->minWriteFileFormat = pDb->pSchema->file_format;
82359 if( pOp->p5 & OPFLAG_P2ISREG ){
82361 assert( p2<=(p->nMem+1 - p->nCursor) );
82364 assert( (pIn2->flags & MEM_Int)!=0 );
82366 p2 = (int)pIn2->u.i;
82373 if( pOp->p4type==P4_KEYINFO ){
82374 pKeyInfo = pOp->p4.pKeyInfo;
82375 assert( pKeyInfo->enc==ENC(db) );
82376 assert( pKeyInfo->db==db );
82377 nField = pKeyInfo->nField+pKeyInfo->nXField;
82378 }else if( pOp->p4type==P4_INT32 ){
82379 nField = pOp->p4.i;
82381 assert( pOp->p1>=0 );
82384 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
82386 pCur->nullRow = 1;
82387 pCur->isOrdered = 1;
82388 pCur->pgnoRoot = p2;
82390 pCur->wrFlag = wrFlag;
82392 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
82393 pCur->pKeyInfo = pKeyInfo;
82395 ** SQLite used to check if the root-page flags were sane at this point
82398 pCur->isTable = pOp->p4type!=P4_KEYINFO;
82403 testcase( pOp->p5 & OPFLAG_BULKCSR );
82405 testcase( pOp->p2 & OPFLAG_SEEKEQ );
82407 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
82408 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
82419 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
82425 pOrig = p->apCsr[pOp->p2];
82426 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
82428 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
82430 pCx->nullRow = 1;
82431 pCx->isEphemeral = 1;
82432 pCx->pKeyInfo = pOrig->pKeyInfo;
82433 pCx->isTable = pOrig->isTable;
82434 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
82435 pCx->pKeyInfo, pCx->uc.pCursor);
82449 ** the main database is read-only. The ephemeral
82481 assert( pOp->p1>=0 );
82482 assert( pOp->p2>=0 );
82483 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
82485 pCx->nullRow = 1;
82486 pCx->isEphemeral = 1;
82487 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
82488 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
82490 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
82496 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
82498 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
82500 assert( pOp->p4type==P4_KEYINFO );
82501 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
82504 assert( pKeyInfo->db==db );
82505 assert( pKeyInfo->enc==ENC(db) );
82506 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
82507 pKeyInfo, pCx->uc.pCursor);
82509 pCx->isTable = 0;
82511 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
82512 0, pCx->uc.pCursor);
82513 pCx->isTable = 1;
82517 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
82525 ** tables using an external merge-sort algorithm.
82527 ** If argument P3 is non-zero, then it indicates that the sorter may
82534 assert( pOp->p1>=0 );
82535 assert( pOp->p2>=0 );
82536 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
82538 pCx->pKeyInfo = pOp->p4.pKeyInfo;
82539 assert( pCx->pKeyInfo->db==db );
82540 assert( pCx->pKeyInfo->enc==ENC(db) );
82541 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
82555 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82556 pC = p->apCsr[pOp->p1];
82558 if( (pC->seqCount++)==0 ){
82572 ** A pseudo-table created by this opcode is used to hold a single
82575 ** is the only cursor opcode that works with a pseudo-table.
82578 ** the pseudo-table.
82583 assert( pOp->p1>=0 );
82584 assert( pOp->p3>=0 );
82585 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
82587 pCx->nullRow = 1;
82588 pCx->uc.pseudoTableReg = pOp->p2;
82589 pCx->isTable = 1;
82590 assert( pOp->p5==0 );
82597 ** currently open, this instruction is a no-op.
82600 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82601 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
82602 p->apCsr[pOp->p1] = 0;
82611 ** table or index for cursor P1 are used. P4 is a 64-bit integer
82614 ** by the cursor. The high-order bit is set if any column after
82619 pC = p->apCsr[pOp->p1];
82620 assert( pC->eCurType==CURTYPE_BTREE );
82621 pC->maskUsed = *(u64*)pOp->p4.pI64;
82629 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82654 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82672 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82690 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82724 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82725 assert( pOp->p2!=0 );
82726 pC = p->apCsr[pOp->p1];
82728 assert( pC->eCurType==CURTYPE_BTREE );
82732 assert( pC->isOrdered );
82733 assert( pC->uc.pCursor!=0 );
82734 oc = pOp->opcode;
82736 pC->nullRow = 0;
82738 pC->seekOp = pOp->opcode;
82741 if( pC->isTable ){
82743 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
82749 pIn3 = &aMem[pOp->p3];
82750 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
82757 if( (pIn3->flags & MEM_Int)==0 ){
82758 if( (pIn3->flags & MEM_Real)==0 ){
82767 ** is 4.9 and the integer approximation 5:
82769 ** (x > 4.9) -> (x >= 5)
82770 ** (x <= 4.9) -> (x < 5)
82772 if( pIn3->u.r<(double)iKey ){
82773 assert( OP_SeekGE==(OP_SeekGT-1) );
82774 assert( OP_SeekLT==(OP_SeekLE-1) );
82776 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
82781 else if( pIn3->u.r>(double)iKey ){
82788 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
82789 pC->movetoTarget = iKey; /* Used by OP_Delete */
82798 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
82800 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
82808 nField = pOp->p4.i;
82809 assert( pOp->p4type==P4_INT32 );
82811 r.pKeyInfo = pC->pKeyInfo;
82816 ** r.default_rc = -1;
82821 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
82822 assert( oc!=OP_SeekGT || r.default_rc==-1 );
82823 assert( oc!=OP_SeekLE || r.default_rc==-1 );
82827 r.aMem = &aMem[pOp->p3];
82832 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
82841 pC->deferredMoveto = 0;
82842 pC->cacheStatus = CACHE_STALE;
82849 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
82865 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
82878 res = sqlite3BtreeEof(pC->uc.pCursor);
82882 assert( pOp->p2>0 );
82938 ** record are not-NULL then a check is done to determine if any row in the
82965 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
82968 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82969 assert( pOp->p4type==P4_INT32 );
82970 pC = p->apCsr[pOp->p1];
82973 pC->seekOp = pOp->opcode;
82975 pIn3 = &aMem[pOp->p3];
82976 assert( pC->eCurType==CURTYPE_BTREE );
82977 assert( pC->uc.pCursor!=0 );
82978 assert( pC->isTable==0 );
82979 if( pOp->p4.i>0 ){
82980 r.pKeyInfo = pC->pKeyInfo;
82981 r.nField = (u16)pOp->p4.i;
82987 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
82993 assert( pIn3->flags & MEM_Blob );
82997 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
82999 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
83001 pIdxKey->default_rc = 0;
83003 if( pOp->opcode==OP_NoConflict ){
83007 for(ii=0; ii<pIdxKey->nField; ii++){
83008 if( pIdxKey->aMem[ii].flags & MEM_Null ){
83014 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
83019 pC->seekResult = res;
83021 pC->nullRow = 1-alreadyExists;
83022 pC->deferredMoveto = 0;
83023 pC->cacheStatus = CACHE_STALE;
83024 if( pOp->opcode==OP_Found ){
83050 ** (with arbitrary multi-value keys).
83069 ** P3 register to contain a non-integer value, in which case the jump is
83073 ** (with arbitrary multi-value keys).
83087 pIn3 = &aMem[pOp->p3];
83088 if( (pIn3->flags & MEM_Int)==0 ){
83090 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
83094 pIn3 = &aMem[pOp->p3];
83095 assert( pIn3->flags & MEM_Int );
83096 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83097 pC = p->apCsr[pOp->p1];
83100 pC->seekOp = 0;
83102 assert( pC->isTable );
83103 assert( pC->eCurType==CURTYPE_BTREE );
83104 pCrsr = pC->uc.pCursor;
83107 iKey = pIn3->u.i;
83110 pC->movetoTarget = iKey; /* Used by OP_Delete */
83111 pC->nullRow = 0;
83112 pC->cacheStatus = CACHE_STALE;
83113 pC->deferredMoveto = 0;
83115 pC->seekResult = res;
83118 if( pOp->p2==0 ){
83137 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83138 assert( p->apCsr[pOp->p1]!=0 );
83139 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
83141 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
83172 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83173 pC = p->apCsr[pOp->p1];
83175 assert( pC->eCurType==CURTYPE_BTREE );
83176 assert( pC->uc.pCursor!=0 );
83179 ** thing) is obtained in a two-step algorithm.
83191 assert( pC->isTable );
83203 if( !pC->useRandomRowid ){
83204 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
83209 v = 1; /* IMP: R-61914-48074 */
83211 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
83212 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
83214 pC->useRandomRowid = 1;
83216 v++; /* IMP: R-29538-34987 */
83222 if( pOp->p3 ){
83224 assert( pOp->p3>0 );
83225 if( p->pFrame ){
83226 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
83228 assert( pOp->p3<=pFrame->nMem );
83229 pMem = &pFrame->aMem[pOp->p3];
83232 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83233 pMem = &aMem[pOp->p3];
83238 REGISTER_TRACE(pOp->p3, pMem);
83240 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
83241 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
83242 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
83245 if( v<pMem->u.i+1 ){
83246 v = pMem->u.i + 1;
83248 pMem->u.i = v;
83251 if( pC->useRandomRowid ){
83252 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
83256 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
83262 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
83268 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
83271 assert( v>0 ); /* EV: R-40812-03570 */
83273 pC->deferredMoveto = 0;
83274 pC->cacheStatus = CACHE_STALE;
83276 pOut->u.i = v;
83305 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
83308 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
83309 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
83329 const char *zDb; /* database name - used by the update hook */
83330 Table *pTab; /* Table structure - used by update and pre-update hooks */
83335 pData = &aMem[pOp->p2];
83336 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83338 pC = p->apCsr[pOp->p1];
83340 assert( pC->eCurType==CURTYPE_BTREE );
83341 assert( pC->uc.pCursor!=0 );
83342 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
83343 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
83344 REGISTER_TRACE(pOp->p2, pData);
83346 if( pOp->opcode==OP_Insert ){
83347 pKey = &aMem[pOp->p3];
83348 assert( pKey->flags & MEM_Int );
83350 REGISTER_TRACE(pOp->p3, pKey);
83351 x.nKey = pKey->u.i;
83353 assert( pOp->opcode==OP_InsertInt );
83354 x.nKey = pOp->p3;
83357 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
83358 assert( pC->iDb>=0 );
83359 zDb = db->aDb[pC->iDb].zDbSName;
83360 pTab = pOp->p4.pTab;
83361 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
83362 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
83369 /* Invoke the pre-update hook, if any */
83370 if( db->xPreUpdateCallback
83371 && pOp->p4type==P4_TABLE
83372 && !(pOp->p5 & OPFLAG_ISUPDATE)
83374 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
83376 if( pOp->p5 & OPFLAG_ISNOOP ) break;
83379 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
83380 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
83381 if( pData->flags & MEM_Null ){
83385 assert( pData->flags & (MEM_Blob|MEM_Str) );
83386 x.pData = pData->z;
83387 x.nData = pData->n;
83389 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
83390 if( pData->flags & MEM_Zero ){
83391 x.nZero = pData->u.nZero;
83396 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
83397 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
83399 pC->deferredMoveto = 0;
83400 pC->cacheStatus = CACHE_STALE;
83402 /* Invoke the update-hook if required. */
83404 if( db->xUpdateCallback && op ){
83405 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
83417 ** the next Next instruction will be a no-op. As a result, in this case
83431 ** P1 must not be pseudo-table. It has to be a real table with
83435 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
83437 ** this case. Specifically, if one is configured, the pre-update hook is
83438 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
83451 opflags = pOp->p2;
83452 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83453 pC = p->apCsr[pOp->p1];
83455 assert( pC->eCurType==CURTYPE_BTREE );
83456 assert( pC->uc.pCursor!=0 );
83457 assert( pC->deferredMoveto==0 );
83460 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
83462 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
83464 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
83465 assert( pC->movetoTarget==iKey );
83469 /* If the update-hook or pre-update-hook will be invoked, set zDb to
83474 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
83475 assert( pC->iDb>=0 );
83476 assert( pOp->p4.pTab!=0 );
83477 zDb = db->aDb[pC->iDb].zDbSName;
83478 pTab = pOp->p4.pTab;
83479 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
83480 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
83488 /* Invoke the pre-update-hook if required. */
83489 if( db->xPreUpdateCallback && pOp->p4.pTab ){
83492 || (aMem[pOp->p3].flags & MEM_Int)
83496 zDb, pTab, pC->movetoTarget,
83497 pOp->p3
83504 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
83509 if( p->pFrame==0 ){
83510 if( pC->isEphemeral==0
83511 && (pOp->p5 & OPFLAG_AUXDELETE)==0
83512 && (pC->wrFlag & OPFLAG_FORDELETE)==0
83516 if( pOp->p2 & OPFLAG_NCHANGE ){
83517 nExtraDelete--;
83522 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
83523 pC->cacheStatus = CACHE_STALE;
83524 pC->seekResult = 0;
83527 /* Invoke the update-hook if required. */
83529 p->nChange++;
83530 if( db->xUpdateCallback && HasRowid(pTab) ){
83531 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
83532 pC->movetoTarget);
83533 assert( pC->iDb>=0 );
83547 sqlite3VdbeSetChanges(db, p->nChange);
83548 p->nChange = 0;
83572 pC = p->apCsr[pOp->p1];
83574 assert( pOp->p4type==P4_INT32 );
83575 pIn3 = &aMem[pOp->p3];
83576 nKeyCol = pOp->p4.i;
83592 ** a register that is the source for a pseudo-table cursor created using
83593 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
83600 pOut = &aMem[pOp->p2];
83601 pC = p->apCsr[pOp->p1];
83604 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
83605 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83607 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
83624 ** of a real table, not a pseudo-table.
83628 ** register will be invalidated as soon as the cursor moves - including
83646 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83647 pC = p->apCsr[pOp->p1];
83649 assert( pC->eCurType==CURTYPE_BTREE );
83651 assert( pC->nullRow==0 );
83652 assert( pC->uc.pCursor!=0 );
83653 pCrsr = pC->uc.pCursor;
83663 assert( pC->deferredMoveto==0 );
83671 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
83677 if( !pOp->p3 ) Deephemeralize(pOut);
83679 REGISTER_TRACE(pOp->p2, pOut);
83700 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83701 pC = p->apCsr[pOp->p1];
83703 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
83704 if( pC->nullRow ){
83705 pOut->flags = MEM_Null;
83707 }else if( pC->deferredMoveto ){
83708 v = pC->movetoTarget;
83710 }else if( pC->eCurType==CURTYPE_VTAB ){
83711 assert( pC->uc.pVCur!=0 );
83712 pVtab = pC->uc.pVCur->pVtab;
83713 pModule = pVtab->pModule;
83714 assert( pModule->xRowid );
83715 rc = pModule->xRowid(pC->uc.pVCur, &v);
83720 assert( pC->eCurType==CURTYPE_BTREE );
83721 assert( pC->uc.pCursor!=0 );
83724 if( pC->nullRow ){
83725 pOut->flags = MEM_Null;
83728 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
83730 pOut->u.i = v;
83743 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83744 pC = p->apCsr[pOp->p1];
83746 pC->nullRow = 1;
83747 pC->cacheStatus = CACHE_STALE;
83748 if( pC->eCurType==CURTYPE_BTREE ){
83749 assert( pC->uc.pCursor!=0 );
83750 sqlite3BtreeClearCursor(pC->uc.pCursor);
83767 ** If P3 is -1, then the cursor is positioned at the end of the btree
83779 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83780 pC = p->apCsr[pOp->p1];
83782 assert( pC->eCurType==CURTYPE_BTREE );
83783 pCrsr = pC->uc.pCursor;
83786 pC->seekResult = pOp->p3;
83788 pC->seekOp = OP_Last;
83790 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){
83792 pC->nullRow = (u8)res;
83793 pC->deferredMoveto = 0;
83794 pC->cacheStatus = CACHE_STALE;
83796 if( pOp->p2>0 ){
83801 assert( pOp->p2==0 );
83817 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83818 pC = p->apCsr[pOp->p1];
83820 pCrsr = pC->uc.pCursor;
83826 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
83859 sqlite3_search_count--;
83861 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
83881 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83882 pC = p->apCsr[pOp->p1];
83884 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
83887 pC->seekOp = OP_Rewind;
83892 assert( pC->eCurType==CURTYPE_BTREE );
83893 pCrsr = pC->uc.pCursor;
83896 pC->deferredMoveto = 0;
83897 pC->cacheStatus = CACHE_STALE;
83900 pC->nullRow = (u8)res;
83901 assert( pOp->p2>0 && pOp->p2<p->nOp );
83918 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
83930 ** number P5-1 in the prepared statement is incremented.
83937 ** open it behaves a no-op.
83951 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
83963 ** number P5-1 in the prepared statement is incremented.
83968 ** open it behaves a no-op.
83980 pC = p->apCsr[pOp->p1];
83986 if( p->apCsr[pOp->p1]==0 ) break;
83990 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
83991 assert( pOp->p5<ArraySize(p->aCounter) );
83992 pC = p->apCsr[pOp->p1];
83994 assert( pC->deferredMoveto==0 );
83995 assert( pC->eCurType==CURTYPE_BTREE );
83996 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
83997 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
83998 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
83999 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
84003 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
84004 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
84005 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
84006 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
84007 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
84008 || pC->seekOp==OP_Last );
84010 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
84012 pC->cacheStatus = CACHE_STALE;
84015 pC->nullRow = 0;
84016 p->aCounter[pOp->p5]++;
84024 pC->nullRow = 1;
84040 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
84068 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84069 pC = p->apCsr[pOp->p1];
84071 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
84072 pIn2 = &aMem[pOp->p2];
84073 assert( pIn2->flags & MEM_Blob );
84074 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
84075 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
84076 assert( pC->isTable==0 );
84079 if( pOp->opcode==OP_SorterInsert ){
84082 x.nKey = pIn2->n;
84083 x.pKey = pIn2->z;
84084 x.aMem = aMem + pOp->p3;
84085 x.nMem = (u16)pOp->p4.i;
84086 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
84087 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
84088 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
84090 assert( pC->deferredMoveto==0 );
84091 pC->cacheStatus = CACHE_STALE;
84110 assert( pOp->p3>0 );
84111 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
84112 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84113 pC = p->apCsr[pOp->p1];
84115 assert( pC->eCurType==CURTYPE_BTREE );
84116 pCrsr = pC->uc.pCursor;
84118 assert( pOp->p5==0 );
84119 r.pKeyInfo = pC->pKeyInfo;
84120 r.nField = (u16)pOp->p3;
84122 r.aMem = &aMem[pOp->p2];
84129 assert( pC->deferredMoveto==0 );
84130 pC->cacheStatus = CACHE_STALE;
84131 pC->seekResult = 0;
84148 ** is non-zero, then reading column a(i)-1 from cursor P3 is
84169 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84170 pC = p->apCsr[pOp->p1];
84172 assert( pC->eCurType==CURTYPE_BTREE );
84173 assert( pC->uc.pCursor!=0 );
84174 assert( pC->isTable==0 );
84175 assert( pC->deferredMoveto==0 );
84176 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
84187 if( !pC->nullRow ){
84189 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
84193 if( pOp->opcode==OP_DeferredSeek ){
84194 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
84195 pTabCur = p->apCsr[pOp->p3];
84197 assert( pTabCur->eCurType==CURTYPE_BTREE );
84198 assert( pTabCur->uc.pCursor!=0 );
84199 assert( pTabCur->isTable );
84200 pTabCur->nullRow = 0;
84201 pTabCur->movetoTarget = rowid;
84202 pTabCur->deferredMoveto = 1;
84203 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
84204 pTabCur->aAltMap = pOp->p4.ai;
84205 pTabCur->pAltCursor = pC;
84208 pOut->u.i = rowid;
84211 assert( pOp->opcode==OP_IdxRowid );
84212 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
84269 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84270 pC = p->apCsr[pOp->p1];
84272 assert( pC->isOrdered );
84273 assert( pC->eCurType==CURTYPE_BTREE );
84274 assert( pC->uc.pCursor!=0);
84275 assert( pC->deferredMoveto==0 );
84276 assert( pOp->p5==0 || pOp->p5==1 );
84277 assert( pOp->p4type==P4_INT32 );
84278 r.pKeyInfo = pC->pKeyInfo;
84279 r.nField = (u16)pOp->p4.i;
84280 if( pOp->opcode<OP_IdxLT ){
84281 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
84282 r.default_rc = -1;
84284 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
84287 r.aMem = &aMem[pOp->p3];
84294 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
84295 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
84296 res = -res;
84298 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
84319 ** value of the root page that moved - its value before the move occurred -
84330 ** and non-autovacuum modes.
84338 assert( p->readOnly==0 );
84339 assert( pOp->p1>1 );
84341 pOut->flags = MEM_Null;
84342 if( db->nVdbeRead > db->nVDestroy+1 ){
84344 p->errorAction = OE_Abort;
84347 iDb = pOp->p3;
84348 assert( DbMaskTest(p->btreeMask, iDb) );
84350 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
84351 pOut->flags = MEM_Int;
84352 pOut->u.i = iMoved;
84356 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
84376 ** If the P3 value is non-zero, then the table referred to must be an
84388 assert( p->readOnly==0 );
84389 assert( DbMaskTest(p->btreeMask, pOp->p2) );
84391 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
84393 if( pOp->p3 ){
84394 p->nChange += nChange;
84395 if( pOp->p3>0 ){
84396 assert( memIsValid(&aMem[pOp->p3]) );
84397 memAboutToChange(p, &aMem[pOp->p3]);
84398 aMem[pOp->p3].u.i += nChange;
84416 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84417 pC = p->apCsr[pOp->p1];
84420 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
84422 assert( pC->eCurType==CURTYPE_BTREE );
84423 assert( pC->isEphemeral );
84424 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
84439 ** have a 4-byte integer key and can have arbitrary data. An index
84462 assert( pOp->p1>=0 && pOp->p1<db->nDb );
84463 assert( DbMaskTest(p->btreeMask, pOp->p1) );
84464 assert( p->readOnly==0 );
84465 pDb = &db->aDb[pOp->p1];
84466 assert( pDb->pBt!=0 );
84467 if( pOp->opcode==OP_CreateTable ){
84473 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
84475 pOut->u.i = pgno;
84484 db->nSqlExec++;
84485 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
84486 db->nSqlExec--;
84497 ** then runs the new virtual machine. It is thus a re-entrant opcode.
84510 for(iDb=0; iDb<db->nDb; iDb++){
84511 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
84515 iDb = pOp->p1;
84516 assert( iDb>=0 && iDb<db->nDb );
84521 initData.iDb = pOp->p1;
84522 initData.pzErrMsg = &p->zErrMsg;
84525 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
84529 assert( db->init.busy==0 );
84530 db->init.busy = 1;
84532 assert( !db->mallocFailed );
84536 db->init.busy = 0;
84557 assert( pOp->p1>=0 && pOp->p1<db->nDb );
84558 rc = sqlite3AnalysisLoad(db, pOp->p1);
84566 ** Remove the internal (in-memory) data structures that describe
84573 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
84579 ** Remove the internal (in-memory) data structures that describe
84586 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
84592 ** Remove the internal (in-memory) data structures that describe
84599 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
84631 assert( p->bIsReader );
84632 nRoot = pOp->p2;
84633 aRoot = pOp->p4.ai;
84636 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
84637 pnErr = &aMem[pOp->p3];
84638 assert( (pnErr->flags & MEM_Int)!=0 );
84639 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
84640 pIn1 = &aMem[pOp->p1];
84641 assert( pOp->p5<db->nDb );
84642 assert( DbMaskTest(p->btreeMask, pOp->p5) );
84643 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
84644 (int)pnErr->u.i+1, &nErr);
84651 pnErr->u.i -= nErr-1;
84652 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
84669 pIn1 = &aMem[pOp->p1];
84670 pIn2 = &aMem[pOp->p2];
84671 assert( (pIn2->flags & MEM_Int)!=0 );
84672 if( (pIn1->flags & MEM_RowSet)==0 ){
84674 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
84676 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
84691 pIn1 = &aMem[pOp->p1];
84692 if( (pIn1->flags & MEM_RowSet)==0
84693 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
84702 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
84710 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
84719 ** must have P4==0, the final set must have P4==-1, and for all other sets
84724 ** (b) when P4==-1 there is no need to insert the value, as it will
84734 pIn1 = &aMem[pOp->p1];
84735 pIn3 = &aMem[pOp->p3];
84736 iSet = pOp->p4.i;
84737 assert( pIn3->flags&MEM_Int );
84742 if( (pIn1->flags & MEM_RowSet)==0 ){
84744 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
84747 assert( pOp->p4type==P4_INT32 );
84748 assert( iSet==-1 || iSet>=0 );
84750 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
84755 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
84768 ** cell in an array of values used as arguments to the sub-program. P2
84769 ** contains the address to jump to if the sub-program throws an IGNORE
84772 ** memory required by the sub-vdbe at runtime.
84776 ** If P5 is non-zero, then recursive program invocation is enabled.
84779 int nMem; /* Number of memory registers for sub-program */
84780 int nByte; /* Bytes of runtime space required for sub-program */
84785 SubProgram *pProgram; /* Sub-program to execute */
84788 pProgram = pOp->p4.pProgram;
84789 pRt = &aMem[pOp->p3];
84790 assert( pProgram->nOp>0 );
84793 ** disabled for backwards compatibility (p5 is set if this sub-program
84803 if( pOp->p5 ){
84804 t = pProgram->token;
84805 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
84809 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
84819 if( (pRt->flags&MEM_Frame)==0 ){
84825 nMem = pProgram->nMem + pProgram->nCsr;
84827 if( pProgram->nCsr==0 ) nMem++;
84830 + pProgram->nCsr * sizeof(VdbeCursor*)
84831 + (pProgram->nOp + 7)/8;
84837 pRt->flags = MEM_Frame;
84838 pRt->u.pFrame = pFrame;
84840 pFrame->v = p;
84841 pFrame->nChildMem = nMem;
84842 pFrame->nChildCsr = pProgram->nCsr;
84843 pFrame->pc = (int)(pOp - aOp);
84844 pFrame->aMem = p->aMem;
84845 pFrame->nMem = p->nMem;
84846 pFrame->apCsr = p->apCsr;
84847 pFrame->nCursor = p->nCursor;
84848 pFrame->aOp = p->aOp;
84849 pFrame->nOp = p->nOp;
84850 pFrame->token = pProgram->token;
84852 pFrame->anExec = p->anExec;
84855 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
84857 pMem->flags = MEM_Undefined;
84858 pMem->db = db;
84861 pFrame = pRt->u.pFrame;
84862 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
84863 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
84864 assert( pProgram->nCsr==pFrame->nChildCsr );
84865 assert( (int)(pOp - aOp)==pFrame->pc );
84868 p->nFrame++;
84869 pFrame->pParent = p->pFrame;
84870 pFrame->lastRowid = db->lastRowid;
84871 pFrame->nChange = p->nChange;
84872 pFrame->nDbChange = p->db->nChange;
84873 assert( pFrame->pAuxData==0 );
84874 pFrame->pAuxData = p->pAuxData;
84875 p->pAuxData = 0;
84876 p->nChange = 0;
84877 p->pFrame = pFrame;
84878 p->aMem = aMem = VdbeFrameMem(pFrame);
84879 p->nMem = pFrame->nChildMem;
84880 p->nCursor = (u16)pFrame->nChildCsr;
84881 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
84882 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
84883 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
84884 p->aOp = aOp = pProgram->aOp;
84885 p->nOp = pProgram->nOp;
84887 p->anExec = 0;
84889 pOp = &aOp[-1];
84896 ** This opcode is only ever present in sub-programs called via the
84910 pFrame = p->pFrame;
84911 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
84923 ** If P1 is non-zero, the database constraint counter is incremented
84928 if( db->flags & SQLITE_DeferFKs ){
84929 db->nDeferredImmCons += pOp->p2;
84930 }else if( pOp->p1 ){
84931 db->nDeferredCons += pOp->p2;
84933 p->nFkConstraint += pOp->p2;
84941 ** This opcode tests if a foreign key constraint-counter is currently zero.
84945 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
84947 ** zero, the jump is taken if the statement constraint-counter is zero
84951 if( pOp->p1 ){
84952 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
84953 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
84955 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
84956 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
84968 ** within a sub-program). Set the value of register P1 to the maximum of
84976 if( p->pFrame ){
84977 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
84978 pIn1 = &pFrame->aMem[pOp->p1];
84980 pIn1 = &aMem[pOp->p1];
84984 pIn2 = &aMem[pOp->p2];
84986 if( pIn1->u.i<pIn2->u.i){
84987 pIn1->u.i = pIn2->u.i;
84994 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
85004 pIn1 = &aMem[pOp->p1];
85005 assert( pIn1->flags&MEM_Int );
85006 VdbeBranchTaken( pIn1->u.i>0, 2);
85007 if( pIn1->u.i>0 ){
85008 pIn1->u.i -= pOp->p3;
85015 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
85028 ** and r[P2] is set to -1.
85034 pIn1 = &aMem[pOp->p1];
85035 pIn3 = &aMem[pOp->p3];
85037 assert( pIn1->flags & MEM_Int );
85038 assert( pIn3->flags & MEM_Int );
85039 x = pIn1->u.i;
85040 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
85048 pOut->u.i = -1;
85050 pOut->u.i = x;
85056 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
85060 ** If it is non-zero (negative or positive) and then also jump to P2.
85064 pIn1 = &aMem[pOp->p1];
85065 assert( pIn1->flags&MEM_Int );
85066 VdbeBranchTaken(pIn1->u.i<0, 2);
85067 if( pIn1->u.i ){
85068 if( pIn1->u.i>0 ) pIn1->u.i--;
85075 ** Synopsis: if (--r[P1])==0 goto P2
85081 pIn1 = &aMem[pOp->p1];
85082 assert( pIn1->flags&MEM_Int );
85083 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
85084 VdbeBranchTaken(pIn1->u.i==0, 2);
85085 if( pIn1->u.i==0 ) goto jump_to_p2;
85122 assert( pOp->p4type==P4_FUNCDEF );
85123 n = pOp->p5;
85124 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85125 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
85126 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
85127 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
85129 pCtx->pMem = 0;
85130 pCtx->pFunc = pOp->p4.pFunc;
85131 pCtx->iOp = (int)(pOp - aOp);
85132 pCtx->pVdbe = p;
85133 pCtx->argc = n;
85134 pOp->p4type = P4_FUNCCTX;
85135 pOp->p4.pCtx = pCtx;
85136 pOp->opcode = OP_AggStep;
85145 assert( pOp->p4type==P4_FUNCCTX );
85146 pCtx = pOp->p4.pCtx;
85147 pMem = &aMem[pOp->p3];
85153 if( pCtx->pMem != pMem ){
85154 pCtx->pMem = pMem;
85155 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
85159 for(i=0; i<pCtx->argc; i++){
85160 assert( memIsValid(pCtx->argv[i]) );
85161 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
85165 pMem->n++;
85167 pCtx->pOut = &t;
85168 pCtx->fErrorOrAux = 0;
85169 pCtx->skipFlag = 0;
85170 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
85171 if( pCtx->fErrorOrAux ){
85172 if( pCtx->isError ){
85174 rc = pCtx->isError;
85181 if( pCtx->skipFlag ){
85182 assert( pOp[-1].opcode==OP_CollSeq );
85183 i = pOp[-1].p1;
85204 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
85205 pMem = &aMem[pOp->p1];
85206 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
85207 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
85223 ** Checkpoint database P1. This is a no-op if P1 is not currently in
85230 ** mem[P3+2] are initialized to -1.
85237 assert( p->readOnly==0 );
85239 aRes[1] = aRes[2] = -1;
85240 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
85241 || pOp->p2==SQLITE_CHECKPOINT_FULL
85242 || pOp->p2==SQLITE_CHECKPOINT_RESTART
85243 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
85245 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
85251 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
85268 ** Write a string containing the final journal-mode to register P2.
85280 eNew = pOp->p3;
85289 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85290 assert( p->readOnly==0 );
85292 pBt = db->aDb[pOp->p1].pBt;
85306 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
85314 if( !db->autoCommit || db->nVdbeRead>1 ){
85325 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
85353 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
85354 pOut->z = (char *)sqlite3JournalModename(eNew);
85355 pOut->n = sqlite3Strlen30(pOut->z);
85356 pOut->enc = SQLITE_UTF8;
85370 assert( p->readOnly==0 );
85371 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
85387 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85388 assert( DbMaskTest(p->btreeMask, pOp->p1) );
85389 assert( p->readOnly==0 );
85390 pBt = db->aDb[pOp->p1].pBt;
85409 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
85413 if( !pOp->p1 ){
85416 p->expired = 1;
85426 ** the shared-cache feature is enabled.
85432 ** P2 contains the root-page of the table to lock.
85438 u8 isWriteLock = (u8)pOp->p3;
85439 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
85440 int p1 = pOp->p1;
85441 assert( p1>=0 && p1<db->nDb );
85442 assert( DbMaskTest(p->btreeMask, p1) );
85444 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
85447 const char *z = pOp->p4.z;
85469 pVTab = pOp->p4.pVtab;
85471 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
85491 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
85492 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
85493 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
85496 assert( zTab || db->mallocFailed );
85498 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
85513 db->nVDestroy++;
85514 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
85515 db->nVDestroy--;
85534 assert( p->bIsReader );
85537 pVtab = pOp->p4.pVtab->pVtab;
85538 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
85542 pModule = pVtab->pModule;
85543 rc = pModule->xOpen(pVtab, &pVCur);
85548 pVCur->pVtab = pVtab;
85551 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
85553 pCur->uc.pVCur = pVCur;
85554 pVtab->nRef++;
85556 assert( db->mallocFailed );
85557 pModule->xClose(pVCur);
85597 pQuery = &aMem[pOp->p3];
85599 pCur = p->apCsr[pOp->p1];
85601 REGISTER_TRACE(pOp->p3, pQuery);
85602 assert( pCur->eCurType==CURTYPE_VTAB );
85603 pVCur = pCur->uc.pVCur;
85604 pVtab = pVCur->pVtab;
85605 pModule = pVtab->pModule;
85608 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
85609 nArg = (int)pArgc->u.i;
85610 iQuery = (int)pQuery->u.i;
85614 apArg = p->apArg;
85618 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
85621 res = pModule->xEof(pVCur);
85622 pCur->nullRow = 0;
85633 ** Store the value of the P2-th column of
85634 ** the row of the virtual-table that the
85643 VdbeCursor *pCur = p->apCsr[pOp->p1];
85644 assert( pCur->eCurType==CURTYPE_VTAB );
85645 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85646 pDest = &aMem[pOp->p3];
85648 if( pCur->nullRow ){
85652 pVtab = pCur->uc.pVCur->pVtab;
85653 pModule = pVtab->pModule;
85654 assert( pModule->xColumn );
85658 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
85664 REGISTER_TRACE(pOp->p3, pDest);
85689 pCur = p->apCsr[pOp->p1];
85690 assert( pCur->eCurType==CURTYPE_VTAB );
85691 if( pCur->nullRow ){
85694 pVtab = pCur->uc.pVCur->pVtab;
85695 pModule = pVtab->pModule;
85696 assert( pModule->xNext );
85704 rc = pModule->xNext(pCur->uc.pVCur);
85707 res = pModule->xEof(pCur->uc.pVCur);
85728 pVtab = pOp->p4.pVtab->pVtab;
85729 pName = &aMem[pOp->p1];
85730 assert( pVtab->pModule->xRename );
85732 assert( p->readOnly==0 );
85733 REGISTER_TRACE(pOp->p1, pName);
85734 assert( pName->flags & MEM_Str );
85735 testcase( pName->enc==SQLITE_UTF8 );
85736 testcase( pName->enc==SQLITE_UTF16BE );
85737 testcase( pName->enc==SQLITE_UTF16LE );
85740 rc = pVtab->pModule->xRename(pVtab, pName->z);
85742 p->expired = 0;
85755 ** invocation. The value in register (P3+P2-1) corresponds to the
85785 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
85786 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
85788 assert( p->readOnly==0 );
85789 pVtab = pOp->p4.pVtab->pVtab;
85790 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
85794 pModule = pVtab->pModule;
85795 nArg = pOp->p2;
85796 assert( pOp->p4type==P4_VTAB );
85797 if( ALWAYS(pModule->xUpdate) ){
85798 u8 vtabOnConflict = db->vtabOnConflict;
85799 apArg = p->apArg;
85800 pX = &aMem[pOp->p3];
85807 db->vtabOnConflict = pOp->p5;
85808 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
85809 db->vtabOnConflict = vtabOnConflict;
85811 if( rc==SQLITE_OK && pOp->p1 ){
85812 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
85813 db->lastRowid = rowid;
85815 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
85816 if( pOp->p5==OE_Ignore ){
85819 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
85822 p->nChange++;
85837 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
85857 pBt = db->aDb[pOp->p1].pBt;
85859 if( pOp->p3 ){
85861 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
85863 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
85876 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
85893 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
85914 assert( pOp->p4type==P4_FUNCDEF );
85915 n = pOp->p5;
85916 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85917 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
85918 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
85919 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
85921 pCtx->pOut = 0;
85922 pCtx->pFunc = pOp->p4.pFunc;
85923 pCtx->iOp = (int)(pOp - aOp);
85924 pCtx->pVdbe = p;
85925 pCtx->argc = n;
85926 pOp->p4type = P4_FUNCCTX;
85927 pOp->p4.pCtx = pCtx;
85930 pOp->opcode += 2;
85938 assert( pOp->p4type==P4_FUNCCTX );
85939 pCtx = pOp->p4.pCtx;
85945 pOut = &aMem[pOp->p3];
85946 if( pCtx->pOut != pOut ){
85947 pCtx->pOut = pOut;
85948 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
85953 for(i=0; i<pCtx->argc; i++){
85954 assert( memIsValid(pCtx->argv[i]) );
85955 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
85959 pCtx->fErrorOrAux = 0;
85960 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
85963 if( pCtx->fErrorOrAux ){
85964 if( pCtx->isError ){
85966 rc = pCtx->isError;
85968 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
85973 if( pOut->flags & (MEM_Str|MEM_Blob) ){
85978 REGISTER_TRACE(pOp->p3, pOut);
85991 ** the UTF-8 string contained in P4 is emitted on the trace callback.
86004 ** The "--" string is broken up to prevent false-positives with srcck1.c.
86007 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
86009 ** using the X argument when X begins with "--" and invoking
86012 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
86013 assert( pOp==p->aOp ); /* Always instruction 0 */
86016 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
86017 && !p->doingRerun
86018 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
86021 if( db->mTrace & SQLITE_TRACE_LEGACY ){
86022 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
86024 x(db->pTraceArg, z);
86028 if( db->nVdbeExec>1 ){
86029 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
86030 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
86033 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
86037 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
86040 for(j=0; j<db->nDb; j++){
86041 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
86042 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
86047 if( (db->flags & SQLITE_SqlTrace)!=0
86048 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
86050 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
86054 assert( pOp->p2>0 );
86055 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
86056 for(i=1; i<p->nOp; i++){
86057 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
86059 pOp->p1 = 0;
86061 pOp->p1++;
86062 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
86072 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
86077 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86078 assert( pOp->p4type==P4_EXPR );
86079 pC = p->apCsr[pOp->p1];
86081 assert( pC->eCurType==CURTYPE_BTREE );
86082 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
86083 pOp->p4.pExpr, aMem);
86098 ** the same as a no-op. This opcodesnever appears in a real VM program.
86101 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
86107 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
86116 if( endTime>start ) pOrigOp->cycles += endTime - start;
86117 pOrigOp->cnt++;
86127 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
86130 if( db->flags & SQLITE_VdbeTrace ){
86131 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
86134 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
86137 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
86148 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
86150 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
86153 p->rc = rc;
86157 (int)(pOp - aOp), p->zSql, p->zErrMsg);
86162 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
86170 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
86173 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
86197 assert( db->u1.isInterrupted );
86198 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
86199 p->rc = rc;
86245 ** the b-tree cursor associated with blob handle p to point to row iRow.
86256 ** If an error does occur, then the b-tree cursor is closed. All subsequent
86263 Vdbe *v = (Vdbe *)p->pStmt;
86268 v->aMem[1].flags = MEM_Int;
86269 v->aMem[1].u.i = iRow;
86275 if( v->pc>3 ){
86276 v->pc = 3;
86279 rc = sqlite3_step(p->pStmt);
86282 VdbeCursor *pC = v->apCsr[0];
86283 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
86284 testcase( pC->nHdrParsed==p->iCol );
86285 testcase( pC->nHdrParsed==p->iCol+1 );
86287 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
86291 sqlite3_finalize(p->pStmt);
86292 p->pStmt = 0;
86294 p->iOffset = pC->aType[p->iCol + pC->nField];
86295 p->nByte = sqlite3VdbeSerialTypeLen(type);
86296 p->pCsr = pC->uc.pCursor;
86297 sqlite3BtreeIncrblobCursor(p->pCsr);
86303 }else if( p->pStmt ){
86304 rc = sqlite3_finalize(p->pStmt);
86305 p->pStmt = 0;
86307 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
86310 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
86330 int wrFlag, /* True -> read/write access, false -> read-only */
86334 int iCol; /* Index of zColumn in row-record */
86354 sqlite3_mutex_enter(db->mutex);
86363 pParse->db = db;
86378 if( pTab && pTab->pSelect ){
86384 if( pParse->zErrMsg ){
86386 zErr = pParse->zErrMsg;
86387 pParse->zErrMsg = 0;
86393 pBlob->pTab = pTab;
86394 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
86397 for(iCol=0; iCol<pTab->nCol; iCol++) {
86398 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
86402 if( iCol==pTab->nCol ){
86417 if( db->flags&SQLITE_ForeignKeys ){
86423 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
86425 for(j=0; j<pFKey->nCol; j++){
86426 if( pFKey->aCol[j].iFrom==iCol ){
86433 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86435 for(j=0; j<pIdx->nKeyCol; j++){
86437 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
86451 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
86452 assert( pBlob->pStmt || db->mallocFailed );
86453 if( pBlob->pStmt ){
86457 ** of writing code to use the b-tree layer directly is that the
86462 ** Code external to the Vdbe then "borrows" the b-tree cursor and
86467 ** which closes the b-tree cursor and (possibly) commits the
86475 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
86478 {OP_Halt, 0, 0, 0}, /* 5 */
86480 Vdbe *v = (Vdbe *)pBlob->pStmt;
86481 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
86485 pTab->pSchema->schema_cookie,
86486 pTab->pSchema->iGeneration);
86493 if( db->mallocFailed==0 ){
86500 aOp[0].p2 = pTab->tnum;
86502 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
86504 if( db->mallocFailed==0 ){
86508 ** parameter of the other to pTab->tnum. */
86510 aOp[1].p2 = pTab->tnum;
86521 aOp[1].p4.i = pTab->nCol+1;
86522 aOp[3].p2 = pTab->nCol;
86524 pParse->nVar = 0;
86525 pParse->nMem = 1;
86526 pParse->nTab = 1;
86531 pBlob->iCol = iCol;
86532 pBlob->db = db;
86534 if( db->mallocFailed ){
86541 if( rc==SQLITE_OK && db->mallocFailed==0 ){
86544 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
86552 sqlite3_mutex_leave(db->mutex);
86566 db = p->db;
86567 sqlite3_mutex_enter(db->mutex);
86568 rc = sqlite3_finalize(p->pStmt);
86570 sqlite3_mutex_leave(db->mutex);
86593 db = p->db;
86594 sqlite3_mutex_enter(db->mutex);
86595 v = (Vdbe*)p->pStmt;
86597 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
86601 /* If there is no statement handle, then the blob-handle has
86607 ** returned, clean-up the statement handle.
86609 assert( db == v->db );
86610 sqlite3BtreeEnterCursor(p->pCsr);
86613 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
86614 /* If a pre-update hook is registered and this is a write cursor,
86617 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
86624 ** using the incremental-blob API, this works. For the sessions module
86628 iKey = sqlite3BtreeIntegerKey(p->pCsr);
86630 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
86635 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
86636 sqlite3BtreeLeaveCursor(p->pCsr);
86639 p->pStmt = 0;
86641 v->rc = rc;
86646 sqlite3_mutex_leave(db->mutex);
86672 return (p && p->pStmt) ? p->nByte : 0;
86691 db = p->db;
86692 sqlite3_mutex_enter(db->mutex);
86694 if( p->pStmt==0 ){
86695 /* If there is no statement handle, then the blob-handle has
86710 assert( rc==SQLITE_OK || p->pStmt==0 );
86711 sqlite3_mutex_leave(db->mutex);
86720 ** 2011-07-09
86735 ** The VdbeSorter object implements a multi-threaded external merge sort
86739 ** Here is the (internal, non-API) interface between this module and the
86792 ** an in-memory merge sort. In this case, no temporary files are required
86799 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
86800 ** of PMAs may be created by merging existing PMAs together - for example
86801 ** merging two or more level-0 PMAs together creates a level-1 PMA.
86805 ** page-cache of the main database. Specifically, the threshold is set to
86809 ** If the sorter is running in single-threaded mode, then all PMAs generated
86811 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
86818 ** The sorter is running in multi-threaded mode if (a) the library was built
86819 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
86828 ** sorter is running in single-threaded mode, then these PMAs are merged
86833 ** Or, if running in multi-threaded mode, then a background thread is
86843 ** Rewind() is called, then a hierarchy of incremental-merges is used.
86849 ** If running in multi-threaded mode and there are more than
86862 ** characteristics of the sorter in multi-threaded mode.
86869 ** Hard-coded maximum amount of data to accumulate in memory before flushing
86882 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
86884 typedef struct SorterList SorterList; /* In-memory list of records */
86897 ** An in-memory list of objects to be sorted.
86906 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
86929 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
86943 ** aReadr[0] -> Banana
86944 ** aReadr[1] -> Feijoa
86945 ** aReadr[2] -> Elderberry
86946 ** aReadr[3] -> Currant
86947 ** aReadr[4] -> Grapefruit
86948 ** aReadr[5] -> Apple
86949 ** aReadr[6] -> Durian
86950 ** aReadr[7] -> EOF
86952 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
86955 ** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
86959 ** aReadr[5] -> Eggplant
86962 ** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
86963 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
86964 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
86965 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
86968 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
86987 ** single-threaded operation, there is exactly one instance of this object
86988 ** and for multi-threaded operation there are two or more instances.
86999 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
87016 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
87021 SorterFile file; /* Temp file for level-0 PMAs */
87045 SorterList list; /* List of in-memory records */
87088 ** There are two types of IncrMerger object - single (bUseThread==0) and
87089 ** multi-threaded (bUseThread==1).
87091 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
87106 ** A single-threaded IncrMerger does not open any temporary files of its
87108 ** at offset iStartOff of file pTask->file2. And instead of using a
87110 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
87128 ** size. But I/O is more efficient if it occurs in page-sized blocks where
87130 ** the PMA so that aligned, page-size blocks are written.
87133 int eFWErr; /* Non-zero if in an error state */
87147 ** by this module. If using a separate allocation for each in-memory record
87155 ** has finished passing records to the sorter, or when the in-memory buffer
87188 sqlite3_free(pReadr->aAlloc);
87189 sqlite3_free(pReadr->aBuffer);
87190 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
87191 vdbeIncrFree(pReadr->pIncr);
87212 if( p->aMap ){
87213 *ppOut = &p->aMap[p->iReadOff];
87214 p->iReadOff += nByte;
87218 assert( p->aBuffer );
87221 ** p->nBuffer bytes of data from the file into it. Or, if there are less
87222 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
87223 iBuf = p->iReadOff % p->nBuffer;
87229 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
87230 nRead = p->nBuffer;
87232 nRead = (int)(p->iEof - p->iReadOff);
87237 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
87241 nAvail = p->nBuffer - iBuf;
87244 /* The requested data is available in the in-memory buffer. In this
87247 *ppOut = &p->aBuffer[iBuf];
87248 p->iReadOff += nByte;
87250 /* The requested data is not all available in the in-memory buffer.
87251 ** In this case, allocate space at p->aAlloc[] to copy the requested
87252 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
87255 /* Extend the p->aAlloc[] allocation if required. */
87256 if( p->nAlloc<nByte ){
87258 int nNew = MAX(128, p->nAlloc*2);
87260 aNew = sqlite3Realloc(p->aAlloc, nNew);
87262 p->nAlloc = nNew;
87263 p->aAlloc = aNew;
87267 ** p->aAlloc[]. */
87268 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
87269 p->iReadOff += nAvail;
87270 nRem = nByte - nAvail;
87272 /* The following loop copies up to p->nBuffer bytes per iteration into
87273 ** the p->aAlloc[] buffer. */
87280 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
87283 assert( aNext!=p->aAlloc );
87284 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
87285 nRem -= nCopy;
87288 *ppOut = p->aAlloc;
87301 if( p->aMap ){
87302 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
87304 iBuf = p->iReadOff % p->nBuffer;
87305 if( iBuf && (p->nBuffer-iBuf)>=9 ){
87306 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
87333 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
87334 sqlite3_file *pFd = pFile->pFd;
87335 if( pFd->pMethods->iVersion>=3 ){
87336 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
87356 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
87359 if( pReadr->aMap ){
87360 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
87361 pReadr->aMap = 0;
87363 pReadr->iReadOff = iOff;
87364 pReadr->iEof = pFile->iEof;
87365 pReadr->pFd = pFile->pFd;
87367 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
87368 if( rc==SQLITE_OK && pReadr->aMap==0 ){
87369 int pgsz = pTask->pSorter->pgsz;
87370 int iBuf = pReadr->iReadOff % pgsz;
87371 if( pReadr->aBuffer==0 ){
87372 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
87373 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
87374 pReadr->nBuffer = pgsz;
87377 int nRead = pgsz - iBuf;
87378 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
87379 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
87382 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
87400 if( pReadr->iReadOff>=pReadr->iEof ){
87401 IncrMerger *pIncr = pReadr->pIncr;
87405 if( rc==SQLITE_OK && pIncr->bEof==0 ){
87407 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
87425 pReadr->nKey = (int)nRec;
87426 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
87435 ** starting at offset iStart and ending at offset iEof-1. This function
87451 assert( pFile->iEof>iStart );
87452 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
87453 assert( pReadr->aBuffer==0 );
87454 assert( pReadr->aMap==0 );
87460 pReadr->iEof = pReadr->iReadOff + nByte;
87477 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
87481 UnpackedRecord *r2 = pTask->pUnpacked;
87483 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
87491 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
87495 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
87496 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
87499 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
87504 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
87508 UnpackedRecord *r2 = pTask->pUnpacked;
87510 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
87523 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
87538 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
87540 res = n1 - n2;
87544 if( pTask->pSorter->pKeyInfo->nField>1 ){
87550 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
87551 res = res * -1;
87564 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
87586 if( (res = v1[i] - v2[i])!=0 ){
87588 res = v1[0] & 0x80 ? -1 : +1;
87594 res = s1 - s2;
87599 res = -1;
87601 res = s1 - s2;
87606 if( *v1 & 0x80 ) res = -1;
87613 if( pTask->pSorter->pKeyInfo->nField>1 ){
87618 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
87619 res = res * -1;
87628 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
87631 ** is non-zero and the sorter is able to guarantee a stable sort, nField
87639 ** The sorter can guarantee a stable sort when running in single-threaded
87640 ** mode, but not in multi-threaded mode.
87652 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
87653 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
87667 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
87675 nWorker = SORTER_MAX_MERGE_COUNT-1;
87679 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
87680 assert( pCsr->eCurType==CURTYPE_SORTER );
87681 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
87685 pCsr->uc.pSorter = pSorter;
87689 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
87690 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
87691 pKeyInfo->db = 0;
87693 pKeyInfo->nXField += (pKeyInfo->nField - nField);
87694 pKeyInfo->nField = nField;
87696 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
87697 pSorter->nTask = nWorker + 1;
87698 pSorter->iPrev = (u8)(nWorker - 1);
87699 pSorter->bUseThreads = (pSorter->nTask>1);
87700 pSorter->db = db;
87701 for(i=0; i<pSorter->nTask; i++){
87702 SortSubtask *pTask = &pSorter->aTask[i];
87703 pTask->pSorter = pSorter;
87709 pSorter->mnPmaSize = szPma * pgsz;
87711 mxCache = db->aDb[0].pSchema->cache_size;
87713 /* A negative cache-size value C indicates that the cache is abs(C)
87715 mxCache = mxCache * -1024;
87720 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
87722 /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
87727 assert( pSorter->iMemory==0 );
87728 pSorter->nMemory = pgsz;
87729 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
87730 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
87734 if( (pKeyInfo->nField+pKeyInfo->nXField)<13
87735 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
87737 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
87752 pNext = p->u.pNext;
87762 sqlite3DbFree(db, pTask->pUnpacked);
87764 /* pTask->list.aMemory can only be non-zero if it was handed memory
87766 if( pTask->list.aMemory ){
87767 sqlite3_free(pTask->list.aMemory);
87771 assert( pTask->list.aMemory==0 );
87772 vdbeSorterRecordFree(0, pTask->list.pList);
87774 if( pTask->file.pFd ){
87775 sqlite3OsCloseFree(pTask->file.pFd);
87777 if( pTask->file2.pFd ){
87778 sqlite3OsCloseFree(pTask->file2.pFd);
87786 int iTask = (pTask - pTask->pSorter->aTask);
87787 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
87800 int iTask = (pTask - pTask->pSorter->aTask);
87801 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
87811 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
87824 ** Join thread pTask->thread.
87828 if( pTask->pThread ){
87830 int bDone = pTask->bDone;
87834 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
87837 assert( pTask->bDone==1 );
87838 pTask->bDone = 0;
87839 pTask->pThread = 0;
87852 assert( pTask->pThread==0 && pTask->bDone==0 );
87853 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
87858 ** level-0 PMAs.
87867 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
87870 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
87871 for(i=pSorter->nTask-1; i>=0; i--){
87872 SortSubtask *pTask = &pSorter->aTask[i];
87902 pNew->nTree = N;
87903 pNew->pTask = 0;
87904 pNew->aReadr = (PmaReader*)&pNew[1];
87905 pNew->aTree = (int*)&pNew->aReadr[N];
87916 for(i=0; i<pMerger->nTree; i++){
87917 vdbePmaReaderClear(&pMerger->aReadr[i]);
87930 if( pIncr->bUseThread ){
87931 vdbeSorterJoinThread(pIncr->pTask);
87932 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
87933 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
87936 vdbeMergeEngineFree(pIncr->pMerger);
87947 assert( pSorter->bUseThreads || pSorter->pReader==0 );
87949 if( pSorter->pReader ){
87950 vdbePmaReaderClear(pSorter->pReader);
87951 sqlite3DbFree(db, pSorter->pReader);
87952 pSorter->pReader = 0;
87955 vdbeMergeEngineFree(pSorter->pMerger);
87956 pSorter->pMerger = 0;
87957 for(i=0; i<pSorter->nTask; i++){
87958 SortSubtask *pTask = &pSorter->aTask[i];
87960 pTask->pSorter = pSorter;
87962 if( pSorter->list.aMemory==0 ){
87963 vdbeSorterRecordFree(0, pSorter->list.pList);
87965 pSorter->list.pList = 0;
87966 pSorter->list.szPMA = 0;
87967 pSorter->bUsePMA = 0;
87968 pSorter->iMemory = 0;
87969 pSorter->mxKeysize = 0;
87970 sqlite3DbFree(db, pSorter->pUnpacked);
87971 pSorter->pUnpacked = 0;
87979 assert( pCsr->eCurType==CURTYPE_SORTER );
87980 pSorter = pCsr->uc.pSorter;
87983 sqlite3_free(pSorter->list.aMemory);
87985 pCsr->uc.pSorter = 0;
87991 ** The first argument is a file-handle open on a temporary file. The file
88000 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
88014 ** Allocate space for a file-handle and open a temporary file. If successful,
88015 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
88025 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
88042 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
88046 if( pTask->pUnpacked==0 ){
88047 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
88048 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
88049 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
88050 pTask->pUnpacked->errCode = 0;
88071 res = pTask->xCompare(
88072 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
88077 pp = &p1->u.pNext;
88078 p1 = p1->u.pNext;
88085 pp = &p2->u.pNext;
88086 p2 = p2->u.pNext;
88102 if( p->typeMask==SORTER_TYPE_INTEGER ){
88104 }else if( p->typeMask==SORTER_TYPE_TEXT ){
88111 ** Sort the linked list of records headed at pTask->pList. Return
88124 p = pList->pList;
88125 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
88134 if( pList->aMemory ){
88135 if( (u8*)p==pList->aMemory ){
88138 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
88139 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
88142 pNext = p->u.pNext;
88145 p->u.pNext = 0;
88159 pList->pList = p;
88162 assert( pTask->pUnpacked->errCode==SQLITE_OK
88163 || pTask->pUnpacked->errCode==SQLITE_NOMEM
88165 return pTask->pUnpacked->errCode;
88169 ** Initialize a PMA-writer object.
88178 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
88179 if( !p->aBuffer ){
88180 p->eFWErr = SQLITE_NOMEM_BKPT;
88182 p->iBufEnd = p->iBufStart = (iStart % nBuf);
88183 p->iWriteOff = iStart - p->iBufStart;
88184 p->nBuffer = nBuf;
88185 p->pFd = pFd;
88195 while( nRem>0 && p->eFWErr==0 ){
88197 if( nCopy>(p->nBuffer - p->iBufEnd) ){
88198 nCopy = p->nBuffer - p->iBufEnd;
88201 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
88202 p->iBufEnd += nCopy;
88203 if( p->iBufEnd==p->nBuffer ){
88204 p->eFWErr = sqlite3OsWrite(p->pFd,
88205 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
88206 p->iWriteOff + p->iBufStart
88208 p->iBufStart = p->iBufEnd = 0;
88209 p->iWriteOff += p->nBuffer;
88211 assert( p->iBufEnd<p->nBuffer );
88213 nRem -= nCopy;
88218 ** Flush any buffered data to disk and clean up the PMA-writer object.
88219 ** The results of using the PMA-writer after this call are undefined.
88228 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
88229 p->eFWErr = sqlite3OsWrite(p->pFd,
88230 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
88231 p->iWriteOff + p->iBufStart
88234 *piEof = (p->iWriteOff + p->iBufEnd);
88235 sqlite3_free(p->aBuffer);
88236 rc = p->eFWErr;
88253 ** Write the current contents of in-memory linked-list pList to a level-0
88254 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
88262 ** * One or more records packed end-to-end in order of ascending keys.
88267 sqlite3 *db = pTask->pSorter->db;
88272 /* Set iSz to the expected size of file pTask->file after writing the PMA.
88274 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
88279 assert( pList->szPMA>0 );
88282 if( pTask->file.pFd==0 ){
88283 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
88284 assert( rc!=SQLITE_OK || pTask->file.pFd );
88285 assert( pTask->file.iEof==0 );
88286 assert( pTask->nPMA==0 );
88291 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
88303 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
88304 pTask->file.iEof);
88305 pTask->nPMA++;
88306 vdbePmaWriteVarint(&writer, pList->szPMA);
88307 for(p=pList->pList; p; p=pNext){
88308 pNext = p->u.pNext;
88309 vdbePmaWriteVarint(&writer, p->nVal);
88310 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
88311 if( pList->aMemory==0 ) sqlite3_free(p);
88313 pList->pList = p;
88314 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
88318 assert( rc!=SQLITE_OK || pList->pList==0 );
88319 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
88335 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
88336 SortSubtask *pTask = pMerger->pTask;
88339 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
88350 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
88351 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
88353 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
88356 if( pReadr1->pFd==0 ){
88358 }else if( pReadr2->pFd==0 ){
88359 iRes = -1;
88361 iRes = pTask->xCompare(pTask, &bCached,
88362 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
88368 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
88373 ** was actually called above, then pTask->pUnpacked now contains
88382 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
88383 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
88386 if( pReadr1->pFd ) bCached = 0;
88387 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
88388 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
88391 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
88394 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
88399 ** The main routine for background threads that write level-0 PMAs.
88404 assert( pTask->bDone==0 );
88405 rc = vdbeSorterListToPMA(pTask, &pTask->list);
88406 pTask->bDone = 1;
88417 pSorter->bUsePMA = 1;
88418 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
88423 int nWorker = (pSorter->nTask-1);
88427 pSorter->bUsePMA = 1;
88429 /* Select a sub-task to sort and flush the current list of in-memory
88430 ** records to disk. If the sorter is running in multi-threaded mode,
88431 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
88432 ** the background thread from a sub-tasks previous turn is still running,
88433 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
88434 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
88435 ** sub-tasks are prefered as they use background threads - the final
88436 ** sub-task uses the main thread. */
88438 int iTest = (pSorter->iPrev + i + 1) % nWorker;
88439 pTask = &pSorter->aTask[iTest];
88440 if( pTask->bDone ){
88443 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
88449 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
88452 u8 *aMem = pTask->list.aMemory;
88455 assert( pTask->pThread==0 && pTask->bDone==0 );
88456 assert( pTask->list.pList==0 );
88457 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
88459 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
88460 pTask->list = pSorter->list;
88461 pSorter->list.pList = 0;
88462 pSorter->list.szPMA = 0;
88464 pSorter->list.aMemory = aMem;
88465 pSorter->nMemory = sqlite3MallocSize(aMem);
88466 }else if( pSorter->list.aMemory ){
88467 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
88468 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
88494 assert( pCsr->eCurType==CURTYPE_SORTER );
88495 pSorter = pCsr->uc.pSorter;
88496 getVarint32((const u8*)&pVal->z[1], t);
88498 pSorter->typeMask &= SORTER_TYPE_INTEGER;
88500 pSorter->typeMask &= SORTER_TYPE_TEXT;
88502 pSorter->typeMask = 0;
88510 ** If using the single large allocation mode (pSorter->aMemory!=0), then
88517 ** * The total memory allocated for the in-memory list is greater
88518 ** than (page-size * cache-size), or
88520 ** * The total memory allocated for the in-memory list is greater
88521 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
88523 nReq = pVal->n + sizeof(SorterRecord);
88524 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
88525 if( pSorter->mxPmaSize ){
88526 if( pSorter->list.aMemory ){
88527 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
88530 (pSorter->list.szPMA > pSorter->mxPmaSize)
88531 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
88536 pSorter->list.szPMA = 0;
88537 pSorter->iMemory = 0;
88538 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
88542 pSorter->list.szPMA += nPMA;
88543 if( nPMA>pSorter->mxKeysize ){
88544 pSorter->mxKeysize = nPMA;
88547 if( pSorter->list.aMemory ){
88548 int nMin = pSorter->iMemory + nReq;
88550 if( nMin>pSorter->nMemory ){
88552 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
88553 int nNew = pSorter->nMemory * 2;
88555 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
88558 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
88560 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
88561 pSorter->list.aMemory = aNew;
88562 pSorter->nMemory = nNew;
88565 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
88566 pSorter->iMemory += ROUND8(nReq);
88567 if( pSorter->list.pList ){
88568 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
88575 pNew->u.pNext = pSorter->list.pList;
88578 memcpy(SRVAL(pNew), pVal->z, pVal->n);
88579 pNew->nVal = pVal->n;
88580 pSorter->list.pList = pNew;
88586 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
88588 ** except that the number-of-bytes varint is omitted from the start.
88593 i64 iStart = pIncr->iStartOff;
88594 SorterFile *pOut = &pIncr->aFile[1];
88595 SortSubtask *pTask = pIncr->pTask;
88596 MergeEngine *pMerger = pIncr->pMerger;
88598 assert( pIncr->bEof==0 );
88602 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
88605 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
88606 int nKey = pReader->nKey;
88611 if( pReader->pFd==0 ) break;
88612 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
88616 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
88617 assert( pIncr->pMerger->pTask==pTask );
88618 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
88621 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
88630 ** multi-threaded IncrMerger objects.
88635 pIncr->pTask->bDone = 1;
88644 assert( pIncr->bUseThread );
88645 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
88655 ** For single-threaded objects, this is accomplished by literally reading
88656 ** keys from pIncr->pMerger and repopulating aFile[0].
88658 ** For multi-threaded objects, all that is required is to wait until the
88670 if( pIncr->bUseThread ){
88671 rc = vdbeSorterJoinThread(pIncr->pTask);
88674 SorterFile f0 = pIncr->aFile[0];
88675 pIncr->aFile[0] = pIncr->aFile[1];
88676 pIncr->aFile[1] = f0;
88680 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
88681 pIncr->bEof = 1;
88690 pIncr->aFile[0] = pIncr->aFile[1];
88691 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
88692 pIncr->bEof = 1;
88714 pIncr->pMerger = pMerger;
88715 pIncr->pTask = pTask;
88716 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
88717 pTask->file2.iEof += pIncr->mxSz;
88727 ** Set the "use-threads" flag on object pIncr.
88730 pIncr->bUseThread = 1;
88731 pIncr->pTask->file2.iEof -= pIncr->mxSz;
88738 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
88744 int iOut /* Store the result in pMerger->aTree[iOut] */
88752 assert( iOut<pMerger->nTree && iOut>0 );
88754 if( iOut>=(pMerger->nTree/2) ){
88755 i1 = (iOut - pMerger->nTree/2) * 2;
88758 i1 = pMerger->aTree[iOut*2];
88759 i2 = pMerger->aTree[iOut*2+1];
88762 p1 = &pMerger->aReadr[i1];
88763 p2 = &pMerger->aReadr[i2];
88765 if( p1->pFd==0 ){
88767 }else if( p2->pFd==0 ){
88770 SortSubtask *pTask = pMerger->pTask;
88773 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
88774 res = pTask->xCompare(
88775 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
88784 pMerger->aTree[iOut] = iRes;
88791 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
88831 int nTree = pMerger->nTree;
88833 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
88837 assert( pMerger->pTask==0 );
88838 pMerger->pTask = pTask;
88844 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
88847 ** on this PmaReader before any of the multi-threaded PmaReaders takes
88848 ** better advantage of multi-processor hardware. */
88849 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
88851 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
88856 for(i=pMerger->nTree-1; i>0; i--){
88859 return pTask->pUnpacked->errCode;
88864 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
88866 ** object at (pReadr->pIncr).
88869 ** in the sub-tree headed by pReadr are also initialized. Data is then
88874 ** to be a multi-threaded PmaReader and this function is being called in a
88875 ** background thread. In this case all PmaReaders in the sub-tree are
88883 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
88884 ** this entire function is being run by thread (pTask->thread), that will
88888 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
88889 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
88897 IncrMerger *pIncr = pReadr->pIncr;
88898 SortSubtask *pTask = pIncr->pTask;
88899 sqlite3 *db = pTask->pSorter->db;
88901 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
88904 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
88906 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
88907 ** requires two temp files to itself, whereas a single-threaded object
88908 ** only requires a region of pTask->file2. */
88910 int mxSz = pIncr->mxSz;
88912 if( pIncr->bUseThread ){
88913 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
88915 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
88919 /*if( !pIncr->bUseThread )*/{
88920 if( pTask->file2.pFd==0 ){
88921 assert( pTask->file2.iEof>0 );
88922 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
88923 pTask->file2.iEof = 0;
88926 pIncr->aFile[1].pFd = pTask->file2.pFd;
88927 pIncr->iStartOff = pTask->file2.iEof;
88928 pTask->file2.iEof += mxSz;
88934 if( rc==SQLITE_OK && pIncr->bUseThread ){
88936 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
88938 ** pIncr->pTask->thread.
88967 pReader->pIncr->pTask->bDone = 1;
88973 ** If the PmaReader passed as the first argument is not an incremental-reader
88974 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
88978 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
88984 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
88988 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
88989 if( pIncr->bUseThread ){
88991 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
89002 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
89003 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
89008 ** first PMA to read from pTask->file. Assuming no error occurs, it is
89016 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
89017 MergeEngine **ppOut /* OUT: New merge-engine */
89029 PmaReader *pReadr = &pNew->aReadr[i];
89030 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
89031 iOff = pReadr->iEof;
89048 ** nPMA<=16 -> TreeDepth() == 0
89049 ** nPMA<=256 -> TreeDepth() == 1
89050 ** nPMA<=65536 -> TreeDepth() == 2
89063 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
89091 PmaReader *pReadr = &p->aReadr[iIter];
89093 if( pReadr->pIncr==0 ){
89098 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
89102 p = pReadr->pIncr->pMerger;
89108 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
89117 ** that has already written two or more level-0 PMAs to one or more temp
89135 /* If the sorter uses more than one task, then create the top-level
89137 ** one PmaReader per sub-task. */
89138 assert( pSorter->bUseThreads || pSorter->nTask==1 );
89139 if( pSorter->nTask>1 ){
89140 pMain = vdbeMergeEngineNew(pSorter->nTask);
89145 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
89146 SortSubtask *pTask = &pSorter->aTask[iTask];
89147 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
89148 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
89150 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
89153 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
89154 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
89160 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
89161 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
89162 int nReader; /* Number of level-0 PMAs to merge */
89164 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
89175 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
89200 ** (for multi-threaded sorters) so that it can be used to iterate through
89207 SortSubtask *pTask0 = &pSorter->aTask[0];
89210 sqlite3 *db = pTask0->pSorter->db;
89213 for(i=0; i<pSorter->nTask; i++){
89214 pSorter->aTask[i].xCompare = xCompare;
89221 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
89222 if( pSorter->bUseThreads ){
89225 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
89229 pSorter->pReader = pReadr;
89233 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
89235 vdbeIncrMergerSetThreads(pReadr->pIncr);
89236 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
89238 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
89240 assert( pIncr->pTask!=pLast );
89243 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
89248 ** b) If it is using task (nTask-1), it is configured to run
89249 ** in single-threaded mode. This is important, as the
89253 PmaReader *p = &pMain->aReadr[iTask];
89254 assert( p->pIncr==0 || (
89255 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
89256 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
89270 pSorter->pMerger = pMain;
89291 assert( pCsr->eCurType==CURTYPE_SORTER );
89292 pSorter = pCsr->uc.pSorter;
89297 ** from the in-memory list. */
89298 if( pSorter->bUsePMA==0 ){
89299 if( pSorter->list.pList ){
89301 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
89308 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
89312 assert( pSorter->list.pList );
89322 assert( pSorter->pReader==0 );
89343 assert( pCsr->eCurType==CURTYPE_SORTER );
89344 pSorter = pCsr->uc.pSorter;
89345 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
89346 if( pSorter->bUsePMA ){
89347 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
89348 assert( pSorter->bUseThreads==0 || pSorter->pReader );
89349 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
89351 if( pSorter->bUseThreads ){
89352 rc = vdbePmaReaderNext(pSorter->pReader);
89353 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
89356 /*if( !pSorter->bUseThreads )*/ {
89358 assert( pSorter->pMerger!=0 );
89359 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
89360 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
89364 SorterRecord *pFree = pSorter->list.pList;
89365 pSorter->list.pList = pFree->u.pNext;
89366 pFree->u.pNext = 0;
89367 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
89368 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
89382 if( pSorter->bUsePMA ){
89385 if( pSorter->bUseThreads ){
89386 pReader = pSorter->pReader;
89389 /*if( !pSorter->bUseThreads )*/{
89390 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
89392 *pnKey = pReader->nKey;
89393 pKey = pReader->aKey;
89395 *pnKey = pSorter->list.pList->nVal;
89396 pKey = SRVAL(pSorter->list.pList);
89408 assert( pCsr->eCurType==CURTYPE_SORTER );
89409 pSorter = pCsr->uc.pSorter;
89414 pOut->n = nKey;
89416 memcpy(pOut->z, pKey, nKey);
89449 assert( pCsr->eCurType==CURTYPE_SORTER );
89450 pSorter = pCsr->uc.pSorter;
89451 r2 = pSorter->pUnpacked;
89452 pKeyInfo = pCsr->pKeyInfo;
89454 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
89456 r2->nField = nKeyCol;
89458 assert( r2->nField==nKeyCol );
89463 if( r2->aMem[i].flags & MEM_Null ){
89464 *pRes = -1;
89469 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
89487 ** This file contains code use to implement an in-memory rollback journal.
89488 ** The in-memory rollback journal is used to journal transactions for
89491 ** Update: The in-memory journal is also used to temporarily cache
89492 ** smaller journals that are not critical for power-loss recovery.
89510 ** The zChunk array is always at least 8 bytes in size - usually much more.
89527 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
89539 ** This structure is a subclass of sqlite3_file. Each open memory-journal
89544 int nChunkSize; /* In-memory chunk-size */
89548 FileChunk *pFirst; /* Head of in-memory chunk-list */
89558 ** Read data from the in-memory journal file. This is the implementation
89574 if( (iAmt+iOfst)>p->endpoint.iOffset ){
89579 assert( (iAmt+iOfst)<=p->endpoint.iOffset );
89580 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
89581 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
89583 for(pChunk=p->pFirst;
89584 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
89585 pChunk=pChunk->pNext
89587 iOff += p->nChunkSize;
89590 pChunk = p->readpoint.pChunk;
89594 iChunkOffset = (int)(iOfst%p->nChunkSize);
89596 int iSpace = p->nChunkSize - iChunkOffset;
89597 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
89598 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
89600 nRead -= iSpace;
89602 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
89603 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
89604 p->readpoint.pChunk = pChunk;
89615 for(pIter=p->pFirst; pIter; pIter=pNext){
89616 pNext = pIter->pNext;
89619 p->pFirst = 0;
89636 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
89638 nChunk = copy.endpoint.iOffset - iOff;
89640 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
89645 /* No error has occurred. Free the in-memory buffers. */
89651 ** the original before returning. This way, SQLite uses the in-memory
89652 ** journal data to roll back changes made to the internal page-cache
89676 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
89686 /* An in-memory journal file should only ever be appended to. Random
89688 ** the in-memory journal is being used by a connection using the
89689 ** atomic-write optimization. In this case the first 28 bytes of the
89691 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
89693 if( iOfst==0 && p->pFirst ){
89694 assert( p->nChunkSize>iAmt );
89695 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
89698 assert( iOfst>0 || p->pFirst==0 );
89702 FileChunk *pChunk = p->endpoint.pChunk;
89703 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
89704 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
89708 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
89712 pNew->pNext = 0;
89714 assert( p->pFirst );
89715 pChunk->pNext = pNew;
89717 assert( !p->pFirst );
89718 p->pFirst = pNew;
89720 p->endpoint.pChunk = pNew;
89723 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
89725 nWrite -= iSpace;
89726 p->endpoint.iOffset += iSpace;
89728 p->nSize = iAmt + iOfst;
89746 p->nSize = 0;
89747 p->endpoint.pChunk = 0;
89748 p->endpoint.iOffset = 0;
89749 p->readpoint.pChunk = 0;
89750 p->readpoint.iOffset = 0;
89768 ** syncing an in-memory journal is a no-op.
89780 *pSize = (sqlite_int64) p->endpoint.iOffset;
89815 ** all content is always stored in main-memory. Finally, if nSpill is a
89816 ** positive value, then the journal file is initially created in-memory
89830 /* Zero the file-handle object. If nSpill was passed zero, initialize
89833 ** made on the journal file-handle. */
89840 p->nChunkSize = nSpill;
89842 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
89843 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
89846 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
89847 p->nSpill = nSpill;
89848 p->flags = flags;
89849 p->zJournal = zName;
89850 p->pVfs = pVfs;
89855 ** Open an in-memory journal file.
89858 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
89864 ** in-memory-only journal file (i.e. is one that was opened with a +ve
89870 if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){
89878 ** The file-handle passed as the only argument is open on a journal file.
89883 return p->pMethods==&MemJournalMethods;
89888 ** pVfs to create the underlying on-disk files.
89891 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
89929 ** return from the top-level walk call.
89938 rc = pWalker->xExprCallback(pWalker, pExpr);
89941 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
89942 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
89943 if( pExpr->pRight ){
89944 if( walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
89946 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
89947 }else if( pExpr->x.pList ){
89948 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
89965 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
89966 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
89979 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
89980 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
89981 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
89982 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
89983 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
89984 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
89985 if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
90001 pSrc = p->pSrc;
90003 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
90004 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
90007 if( pItem->fg.isTabFunc
90008 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
90020 ** on the compound select chain, p->pPrior.
90025 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
90032 ** is a no-op returning WRC_Continue.
90037 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
90039 rc = pWalker->xSelectCallback(pWalker, p);
90046 if( pWalker->xSelectCallback2 ){
90047 pWalker->xSelectCallback2(pWalker, p);
90049 p = p->pPrior;
90081 ** is a helper function - a callback for the tree walker.
90084 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
90098 ** Turn the pExpr expression into an alias for the iCol-th column of the
90119 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
90124 Expr *pOrig; /* The iCol-th column of the result set */
90128 assert( iCol>=0 && iCol<pEList->nExpr );
90129 pOrig = pEList->a[iCol].pExpr;
90131 db = pParse->db;
90135 if( pExpr->op==TK_COLLATE ){
90136 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
90143 ** The pExpr->u.zToken might point into memory that will be freed by the
90150 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
90151 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
90152 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
90153 pExpr->flags |= EP_MemToken;
90168 for(k=0; k<pUsing->nId; k++){
90169 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
90211 ** pExpr->iDb Set the index in db->aDb[] of the database X
90213 ** pExpr->iTable Set to the cursor number for the table obtained
90215 ** pExpr->pTab Points to the Table structure of X.Y (even if
90217 ** pExpr->iColumn Set to the column number within the table.
90218 ** pExpr->op Set to TK_COLUMN.
90219 ** pExpr->pLeft Any expression this points to is deleted
90220 ** pExpr->pRight Any expression this points to is deleted.
90244 sqlite3 *db = pParse->db; /* The database connection */
90257 /* Initialize the node to no-match */
90258 pExpr->iTable = -1;
90259 pExpr->pTab = 0;
90267 testcase( pNC->ncFlags & NC_PartIdx );
90268 testcase( pNC->ncFlags & NC_IsCheck );
90269 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
90276 for(i=0; i<db->nDb; i++){
90277 assert( db->aDb[i].zDbSName );
90278 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
90279 pSchema = db->aDb[i].pSchema;
90286 /* Start at the inner-most context and move outward until a match is found */
90290 SrcList *pSrcList = pNC->pSrcList;
90293 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
90294 pTab = pItem->pTab;
90295 assert( pTab!=0 && pTab->zName!=0 );
90296 assert( pTab->nCol>0 );
90297 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
90299 pEList = pItem->pSelect->pEList;
90300 for(j=0; j<pEList->nExpr; j++){
90301 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
90305 pExpr->iColumn = j;
90311 if( zDb && pTab->pSchema!=pSchema ){
90315 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
90324 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
90325 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
90327 ** is for the right-hand table of a NATURAL JOIN or is in a
90331 if( pItem->fg.jointype & JT_NATURAL ) continue;
90332 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
90336 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
90337 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
90343 pExpr->iTable = pMatch->iCursor;
90344 pExpr->pTab = pMatch->pTab;
90346 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
90347 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
90350 pSchema = pExpr->pTab->pSchema;
90358 if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
90359 int op = pParse->eTriggerOp;
90362 pExpr->iTable = 1;
90363 pTab = pParse->pTriggerTab;
90365 pExpr->iTable = 0;
90366 pTab = pParse->pTriggerTab;
90373 pSchema = pTab->pSchema;
90375 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
90376 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
90377 if( iCol==pTab->iPKey ){
90378 iCol = -1;
90383 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
90384 /* IMP: R-51414-32910 */
90385 iCol = -1;
90387 if( iCol<pTab->nCol ){
90390 pExpr->affinity = SQLITE_AFF_INTEGER;
90391 }else if( pExpr->iTable==0 ){
90394 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
90398 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
90400 pExpr->iColumn = (i16)iCol;
90401 pExpr->pTab = pTab;
90414 && (pNC->ncFlags & NC_IdxExpr)==0
90416 && VisibleRowid(pMatch->pTab)
90419 pExpr->iColumn = -1;
90420 pExpr->affinity = SQLITE_AFF_INTEGER;
90425 ** might refer to an result-set alias. This happens, for example, when
90435 ** The ability to use an output result-set column in the WHERE, GROUP BY,
90441 if( (pEList = pNC->pEList)!=0
90445 for(j=0; j<pEList->nExpr; j++){
90446 char *zAs = pEList->a[j].zName;
90449 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
90450 assert( pExpr->x.pList==0 );
90451 assert( pExpr->x.pSelect==0 );
90452 pOrig = pEList->a[j].pExpr;
90453 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
90474 pNC = pNC->pNext;
90481 ** supplied) and the value of Z is enclosed in double-quotes, then
90486 ** Because no reference was made to outer contexts, the pNC->nRef
90490 pExpr->op = TK_STRING;
90491 pExpr->pTab = 0;
90509 pParse->checkSchema = 1;
90510 pTopNC->nErr++;
90517 ** then set the high-order bit of the bitmask.
90519 if( pExpr->iColumn>=0 && pMatch!=0 ){
90520 int n = pExpr->iColumn;
90521 testcase( n==BMS-1 );
90523 n = BMS-1;
90525 assert( pMatch->iCursor==pExpr->iTable );
90526 pMatch->colUsed |= ((Bitmask)1)<<n;
90531 sqlite3ExprDelete(db, pExpr->pLeft);
90532 pExpr->pLeft = 0;
90533 sqlite3ExprDelete(db, pExpr->pRight);
90534 pExpr->pRight = 0;
90535 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
90541 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
90547 pTopNC->nRef++;
90549 pTopNC = pTopNC->pNext;
90564 struct SrcList_item *pItem = &pSrc->a[iSrc];
90565 p->pTab = pItem->pTab;
90566 p->iTable = pItem->iCursor;
90567 if( p->pTab->iPKey==iCol ){
90568 p->iColumn = -1;
90570 p->iColumn = (ynVar)iCol;
90572 testcase( iCol==BMS-1 );
90573 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
90581 ** pNC->ncFlags values determined by validMask.
90590 if( (pNC->ncFlags & validMask)!=0 ){
90592 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
90594 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
90602 ** Return 1024 times this value. Or return -1 if p is not a floating point
90606 double r = -1.0;
90607 if( p->op!=TK_FLOAT ) return -1;
90608 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
90610 if( r>1.0 ) return -1;
90629 pNC = pWalker->u.pNC;
90631 pParse = pNC->pParse;
90632 assert( pParse==pWalker->pParse );
90635 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
90636 SrcList *pSrcList = pNC->pSrcList;
90638 for(i=0; i<pNC->pSrcList->nSrc; i++){
90639 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
90643 switch( pExpr->op ){
90651 SrcList *pSrcList = pNC->pSrcList;
90653 assert( pSrcList && pSrcList->nSrc==1 );
90654 pItem = pSrcList->a;
90655 pExpr->op = TK_COLUMN;
90656 pExpr->pTab = pItem->pTab;
90657 pExpr->iTable = pItem->iCursor;
90658 pExpr->iColumn = -1;
90659 pExpr->affinity = SQLITE_AFF_INTEGER;
90670 ** be one call to lookupName(). Then the compiler will in-line
90680 if( pExpr->op==TK_ID ){
90683 zColumn = pExpr->u.zToken;
90686 pRight = pExpr->pRight;
90687 if( pRight->op==TK_ID ){
90689 zTable = pExpr->pLeft->u.zToken;
90690 zColumn = pRight->u.zToken;
90692 assert( pRight->op==TK_DOT );
90693 zDb = pExpr->pLeft->u.zToken;
90694 zTable = pRight->pLeft->u.zToken;
90695 zColumn = pRight->pRight->u.zToken;
90704 ExprList *pList = pExpr->x.pList; /* The argument list */
90705 int n = pList ? pList->nExpr : 0; /* Number of arguments */
90712 u8 enc = ENC(pParse->db); /* The database encoding */
90715 zId = pExpr->u.zToken;
90717 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
90719 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
90726 is_agg = pDef->xFinalize!=0;
90727 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
90730 pExpr->iTable = exprProbability(pList->a[1].pExpr);
90731 if( pExpr->iTable<0 ){
90735 pNC->nErr++;
90738 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
90740 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
90741 ** short-hand for likelihood(X,0.0625).
90742 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
90744 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
90747 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
90752 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
90756 pDef->zName);
90757 pNC->nErr++;
90759 pExpr->op = TK_NULL;
90764 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
90770 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
90774 notValid(pParse, pNC, "non-deterministic functions",
90778 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
90780 pNC->nErr++;
90782 }else if( no_such_func && pParse->db->init.busy==0
90784 && pParse->explain==0
90788 pNC->nErr++;
90792 pNC->nErr++;
90794 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
90798 pExpr->op = TK_AGG_FUNCTION;
90799 pExpr->op2 = 0;
90800 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
90801 pExpr->op2++;
90802 pNC2 = pNC2->pNext;
90807 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
90808 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
90811 pNC->ncFlags |= NC_AllowAgg;
90813 /* FIX ME: Compute pExpr->affinity based on the expected return
90820 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
90823 testcase( pExpr->op==TK_IN );
90825 int nRef = pNC->nRef;
90827 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
90828 assert( pNC->nRef>=nRef );
90829 if( nRef!=pNC->nRef ){
90831 pNC->ncFlags |= NC_VarSelect;
90850 if( pParse->db->mallocFailed ) break;
90851 assert( pExpr->pLeft!=0 );
90852 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
90853 if( pExpr->op==TK_BETWEEN ){
90854 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
90856 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
90859 assert( pExpr->pRight!=0 );
90860 nRight = sqlite3ExprVectorSize(pExpr->pRight);
90863 testcase( pExpr->op==TK_EQ );
90864 testcase( pExpr->op==TK_NE );
90865 testcase( pExpr->op==TK_LT );
90866 testcase( pExpr->op==TK_LE );
90867 testcase( pExpr->op==TK_GT );
90868 testcase( pExpr->op==TK_GE );
90869 testcase( pExpr->op==TK_IS );
90870 testcase( pExpr->op==TK_ISNOT );
90871 testcase( pExpr->op==TK_BETWEEN );
90877 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
90884 ** to the AS-name of one of the terms of the expression list. If it is,
90901 if( pE->op==TK_ID ){
90902 char *zCol = pE->u.zToken;
90903 for(i=0; i<pEList->nExpr; i++){
90904 char *zAs = pEList->a[i].zName;
90922 ** Attempt to match pE against result set columns in the left-most
90924 ** as an indication to the caller that it should sort by the i-th column.
90925 ** The left-most column is 1. In other words, the value returned is the
90929 ** If there is no match, return 0. Return -1 if an error occurs.
90941 u8 savedSuppErr; /* Saved value of db->suppressErr */
90944 pEList = pSelect->pEList;
90950 nc.pSrcList = pSelect->pSrc;
90954 db = pParse->db;
90955 savedSuppErr = db->suppressErr;
90956 db->suppressErr = 1;
90958 db->suppressErr = savedSuppErr;
90962 ** in the result set. Return an 1-based index of the matching
90963 ** result-set entry.
90965 for(i=0; i<pEList->nExpr; i++){
90966 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
90976 ** Generate an ORDER BY or GROUP BY term out-of-range error.
90981 int i, /* The index (1-based) of the term out of range */
90985 "%r %s BY term out of range - should be "
90998 ** beginning with the left-most SELECT and working toward the right.
91014 pOrderBy = pSelect->pOrderBy;
91016 db = pParse->db;
91018 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
91023 for(i=0; i<pOrderBy->nExpr; i++){
91024 pOrderBy->a[i].done = 0;
91026 pSelect->pNext = 0;
91027 while( pSelect->pPrior ){
91028 pSelect->pPrior->pNext = pSelect;
91029 pSelect = pSelect->pPrior;
91034 pEList = pSelect->pEList;
91036 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
91037 int iCol = -1;
91039 if( pItem->done ) continue;
91040 pE = sqlite3ExprSkipCollate(pItem->pExpr);
91042 if( iCol<=0 || iCol>pEList->nExpr ){
91043 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
91050 if( !db->mallocFailed ){
91062 pNew->flags |= EP_IntValue;
91063 pNew->u.iValue = iCol;
91064 if( pItem->pExpr==pE ){
91065 pItem->pExpr = pNew;
91067 Expr *pParent = pItem->pExpr;
91068 assert( pParent->op==TK_COLLATE );
91069 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
91070 assert( pParent->pLeft==pE );
91071 pParent->pLeft = pNew;
91074 pItem->u.x.iOrderByCol = (u16)iCol;
91075 pItem->done = 1;
91080 pSelect = pSelect->pNext;
91082 for(i=0; i<pOrderBy->nExpr; i++){
91083 if( pOrderBy->a[i].done==0 ){
91100 ** return non-zero. Return zero if no errors are seen.
91109 sqlite3 *db = pParse->db;
91113 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
91115 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
91120 pEList = pSelect->pEList;
91122 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
91123 if( pItem->u.x.iOrderByCol ){
91124 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
91125 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
91128 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
91141 ** If the order-by term is an integer I between 1 and N (where N is the
91143 ** in the resolution is a copy of the I-th result-set expression. If
91144 ** the order-by term is an identifier that corresponds to the AS-name of
91145 ** a result-set expression, then the term resolves to a copy of the
91146 ** result-set expression. Otherwise, the expression is resolved in
91147 ** the usual way - using sqlite3ResolveExprNames().
91166 nResult = pSelect->pEList->nExpr;
91167 pParse = pNC->pParse;
91168 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
91169 Expr *pE = pItem->pExpr;
91172 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
91174 /* If an AS-name match is found, mark this ORDER BY column as being
91175 ** a copy of the iCol-th result-set column. The subsequent call to
91177 ** copy of the iCol-th result-set expression. */
91178 pItem->u.x.iOrderByCol = (u16)iCol;
91185 ** order-by term to a copy of the result-set expression */
91190 pItem->u.x.iOrderByCol = (u16)iCol;
91195 pItem->u.x.iOrderByCol = 0;
91199 for(j=0; j<pSelect->pEList->nExpr; j++){
91200 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
91201 pItem->u.x.iOrderByCol = j+1;
91219 Select *pLeftmost; /* Left-most of SELECT of a compound */
91224 if( p->selFlags & SF_Resolved ){
91227 pOuterNC = pWalker->u.pNC;
91228 pParse = pWalker->pParse;
91229 db = pParse->db;
91239 if( (p->selFlags & SF_Expanded)==0 ){
91241 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
91244 isCompound = p->pPrior!=0;
91248 assert( (p->selFlags & SF_Expanded)!=0 );
91249 assert( (p->selFlags & SF_Resolved)==0 );
91250 p->selFlags |= SF_Resolved;
91257 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
91258 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
91264 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
91265 ** as if it were part of the sub-query, not the parent. This block
91266 ** moves the pOrderBy down to the sub-query. It will be moved back
91268 if( p->selFlags & SF_Converted ){
91269 Select *pSub = p->pSrc->a[0].pSelect;
91270 assert( p->pSrc->nSrc==1 && p->pOrderBy );
91271 assert( pSub->pPrior && pSub->pOrderBy==0 );
91272 pSub->pOrderBy = p->pOrderBy;
91273 p->pOrderBy = 0;
91278 for(i=0; i<p->pSrc->nSrc; i++){
91279 struct SrcList_item *pItem = &p->pSrc->a[i];
91280 if( pItem->pSelect ){
91283 const char *zSavedContext = pParse->zAuthContext;
91287 ** pItem->pSelect, check if this value has changed. If so, then
91288 ** SELECT statement pItem->pSelect must be correlated. Set the
91289 ** pItem->fg.isCorrelated flag if this is the case. */
91290 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
91292 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
91293 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
91294 pParse->zAuthContext = zSavedContext;
91295 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
91297 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
91298 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
91299 pItem->fg.isCorrelated = (nRef!=0);
91303 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
91304 ** resolve the result-set expression list.
91307 sNC.pSrcList = p->pSrc;
91311 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
91313 /* If there are no aggregate functions in the result-set, and no GROUP BY
91316 assert( (p->selFlags & SF_Aggregate)==0 );
91317 pGroupBy = p->pGroupBy;
91320 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
91327 if( p->pHaving && !pGroupBy ){
91332 /* Add the output column list to the name-context before parsing the
91338 ** re-evaluated for each reference to it.
91340 sNC.pEList = p->pEList;
91341 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
91342 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
91344 /* Resolve names in table-valued-function arguments */
91345 for(i=0; i<p->pSrc->nSrc; i++){
91346 struct SrcList_item *pItem = &p->pSrc->a[i];
91347 if( pItem->fg.isTabFunc
91348 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
91361 ** the sub-query back to the parent query. At this point each term
91365 if( p->selFlags & SF_Converted ){
91366 Select *pSub = p->pSrc->a[0].pSelect;
91367 p->pOrderBy = pSub->pOrderBy;
91368 pSub->pOrderBy = 0;
91373 ** below, after all of the result-sets for all of the elements of
91376 ** If there is an ORDER BY clause on a term of a compound-select other
91377 ** than the right-most term, then that is a syntax error. But the error
91381 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
91382 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
91386 if( db->mallocFailed ){
91396 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
91399 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
91400 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
91410 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
91411 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
91417 p = p->pPrior;
91433 ** table columns and result-set columns. At the same time, do error
91441 ** the symbolic name assigned to an ATTACH-ed database.
91452 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
91456 ** To resolve result-set references, look for expression nodes of the
91457 ** form Z (with no X and Y prefix) where the Z matches the right-hand
91458 ** size of an AS clause in the result-set of a SELECT. The Z expression
91459 ** is replaced by a copy of the left-hand side of the result-set expression.
91460 ** Table-name and function resolution occurs on the substituted expression
91487 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
91488 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
91489 w.pParse = pNC->pParse;
91495 w.pParse->nHeight += pExpr->nHeight;
91496 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
91502 w.pParse->nHeight -= pExpr->nHeight;
91504 if( pNC->ncFlags & NC_HasAgg ){
91507 pNC->ncFlags |= savedHasAgg;
91508 return pNC->nErr>0 || w.pParse->nErr>0;
91522 for(i=0; i<pList->nExpr; i++){
91523 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
91531 ** decendents of the SELECT, including compounds off of p->pPrior,
91564 ** is set to -1 and the Expr.iColumn value is set to the column number.
91575 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
91576 NameContext sNC; /* Name context for pParse->pNewTable */
91582 sSrc.a[0].zName = pTab->zName;
91584 sSrc.a[0].iCursor = -1;
91618 assert( iCol<pTab->nCol );
91619 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
91626 ** or a sub-select with a column as the return value, then the
91641 if( pExpr->flags & EP_Generic ) return 0;
91642 op = pExpr->op;
91644 assert( pExpr->flags&EP_xIsSelect );
91645 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
91647 if( op==TK_REGISTER ) op = pExpr->op2;
91651 return sqlite3AffinityType(pExpr->u.zToken, 0);
91654 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
91655 return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
91658 assert( pExpr->pLeft->flags&EP_xIsSelect );
91660 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
91663 return pExpr->affinity;
91671 ** If a memory allocation error occurs, that fact is recorded in pParse->db
91680 if( pCollName->n>0 ){
91681 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
91683 pNew->pLeft = pExpr;
91684 pNew->flags |= EP_Collate|EP_Skip;
91705 assert( pExpr->x.pList->nExpr>0 );
91706 assert( pExpr->op==TK_FUNCTION );
91707 pExpr = pExpr->x.pList->a[0].pExpr;
91709 assert( pExpr->op==TK_COLLATE );
91710 pExpr = pExpr->pLeft;
91726 sqlite3 *db = pParse->db;
91730 int op = p->op;
91731 if( p->flags & EP_Generic ) break;
91733 p = p->pLeft;
91736 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
91737 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
91742 && p->pTab!=0
91744 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
91746 int j = p->iColumn;
91748 const char *zColl = p->pTab->aCol[j].zColl;
91753 if( p->flags & EP_Collate ){
91754 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
91755 p = p->pLeft;
91757 Expr *pNext = p->pRight;
91759 assert( p->x.pList==0 || p->pRight==0 );
91760 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
91761 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
91763 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
91765 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
91766 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
91767 pNext = p->x.pList->a[i].pExpr;
91818 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
91819 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
91820 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
91821 assert( pExpr->pLeft );
91822 aff = sqlite3ExprAffinity(pExpr->pLeft);
91823 if( pExpr->pRight ){
91824 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
91826 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
91880 if( pLeft->flags & EP_Collate ){
91882 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
91911 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
91913 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
91933 ** is a sub-select, return the number of columns in the sub-select. For
91937 u8 op = pExpr->op;
91938 if( op==TK_REGISTER ) op = pExpr->op2;
91940 return pExpr->x.pList->nExpr;
91942 return pExpr->x.pSelect->pEList->nExpr;
91949 ** Return a pointer to a subexpression of pVector that is the i-th
91959 ** just the expression for the i-th term of the result set, and may
91966 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
91967 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
91968 return pVector->x.pSelect->pEList->a[i].pExpr;
91970 return pVector->x.pList->a[i].pExpr;
91979 ** the iField-th column of the vector expression pVector.
91999 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
92003 if( pVector->op==TK_SELECT ){
92004 assert( pVector->flags & EP_xIsSelect );
92011 ** pLeft->iTable: First in an array of register holding result, or 0
92023 pRet->iColumn = iField;
92024 pRet->pLeft = pVector;
92026 assert( pRet==0 || pRet->iTable==0 );
92028 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
92029 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
92037 ** sub-select returns more than one column, the first in an array
92045 if( pExpr->op==TK_SELECT ){
92053 ** Argument pVector points to a vector expression - either a TK_VECTOR
92061 ** containing the results of the sub-select.
92078 u8 op = pVector->op;
92082 return pVector->iTable+iField;
92085 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
92088 *ppExpr = pVector->x.pList->a[iField].pExpr;
92099 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
92100 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
92101 ** otherwise: op==pExpr->op and p5==0
92110 Vdbe *v = pParse->pVdbe;
92111 Expr *pLeft = pExpr->pLeft;
92112 Expr *pRight = pExpr->pRight;
92124 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
92125 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
92126 || pExpr->op==TK_LT || pExpr->op==TK_GT
92127 || pExpr->op==TK_LE || pExpr->op==TK_GE
92129 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
92130 || (pExpr->op==TK_ISNOT && op==TK_NE) );
92131 assert( p5==0 || pExpr->op!=op );
92132 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
92159 if( i==nLeft-1 ){
92175 if( i==nLeft-2 ) opx = op;
92189 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
92210 if( p->nHeight>*pnHeight ){
92211 *pnHeight = p->nHeight;
92218 for(i=0; i<p->nExpr; i++){
92219 heightOfExpr(p->a[i].pExpr, pnHeight);
92225 heightOfExpr(p->pWhere, pnHeight);
92226 heightOfExpr(p->pHaving, pnHeight);
92227 heightOfExpr(p->pLimit, pnHeight);
92228 heightOfExpr(p->pOffset, pnHeight);
92229 heightOfExprList(p->pEList, pnHeight);
92230 heightOfExprList(p->pGroupBy, pnHeight);
92231 heightOfExprList(p->pOrderBy, pnHeight);
92232 heightOfSelect(p->pPrior, pnHeight);
92248 heightOfExpr(p->pLeft, &nHeight);
92249 heightOfExpr(p->pRight, &nHeight);
92251 heightOfSelect(p->x.pSelect, &nHeight);
92252 }else if( p->x.pList ){
92253 heightOfExprList(p->x.pList, &nHeight);
92254 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
92256 p->nHeight = nHeight + 1;
92268 if( pParse->nErr ) return;
92270 sqlite3ExprCheckHeight(pParse, p->nHeight);
92288 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
92289 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
92306 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
92310 ** can be translated into a 32-bit integer, then the token is not
92327 if( op!=TK_INTEGER || pToken->z==0
92328 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
92329 nExtra = pToken->n+1;
92336 pNew->op = (u8)op;
92337 pNew->iAgg = -1;
92340 pNew->flags |= EP_IntValue|EP_Leaf;
92341 pNew->u.iValue = iValue;
92343 pNew->u.zToken = (char*)&pNew[1];
92344 assert( pToken->z!=0 || pToken->n==0 );
92345 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
92346 pNew->u.zToken[pToken->n] = 0;
92347 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
92348 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
92349 sqlite3Dequote(pNew->u.zToken);
92354 pNew->nHeight = 1;
92361 ** Allocate a new expression node from a zero-terminated token that has
92388 assert( db->mallocFailed );
92393 pRoot->pRight = pRight;
92394 pRoot->flags |= EP_Propagate & pRight->flags;
92397 pRoot->pLeft = pLeft;
92398 pRoot->flags |= EP_Propagate & pLeft->flags;
92408 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
92418 if( op==TK_AND && pParse->nErr==0 ){
92419 /* Take advantage of short-circuit false optimization for AND */
92420 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
92422 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
92425 p->op = op & TKFLG_MASK;
92426 p->iAgg = -1;
92428 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
92431 sqlite3ExprCheckHeight(pParse, p->nHeight);
92442 pExpr->x.pSelect = pSelect;
92446 assert( pParse->db->mallocFailed );
92447 sqlite3SelectDelete(pParse->db, pSelect);
92455 ** expression at compile-time return 0.
92463 ** LEFT JOIN, then we cannot determine at compile-time whether or not
92509 sqlite3 *db = pParse->db;
92516 pNew->x.pList = pList;
92539 sqlite3 *db = pParse->db;
92545 z = pExpr->u.zToken;
92552 x = (ynVar)(++pParse->nVar);
92560 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
92561 i = z[1]-'0'; /* The common case of ?N for a single digit N */
92564 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
92568 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
92569 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
92570 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
92572 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
92576 if( x>pParse->nVar ){
92577 pParse->nVar = (int)x;
92579 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
92587 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
92589 x = (ynVar)(++pParse->nVar);
92594 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
92597 pExpr->iColumn = x;
92598 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
92608 /* Sanity check: Assert that the IntValue is non-negative if it exists */
92609 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
92612 assert( p->pLeft==0 );
92613 assert( p->pRight==0 );
92614 assert( p->x.pSelect==0 );
92619 assert( p->x.pList==0 || p->pRight==0 );
92620 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
92621 if( p->pRight ){
92622 sqlite3ExprDeleteNN(db, p->pRight);
92624 sqlite3SelectDelete(db, p->x.pSelect);
92626 sqlite3ExprListDelete(db, p->x.pList);
92629 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
92660 ** The dupedExprStructSize() function returns two values OR-ed together:
92673 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
92688 if( 0==flags || p->op==TK_SELECT_COLUMN ){
92695 if( p->pLeft || p->x.pList ){
92698 assert( p->pRight==0 );
92712 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
92713 nByte += sqlite3Strlen30(p->u.zToken)+1;
92736 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
92745 ** to store the copy of expression p, the copies of p->u.zToken
92746 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
92774 ** by the copy of the p->u.zToken string (if any).
92779 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
92780 nToken = sqlite3Strlen30(p->u.zToken) + 1;
92791 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
92796 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
92797 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
92798 pNew->flags |= staticFlag;
92800 /* Copy the p->u.zToken string, if any. */
92802 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
92803 memcpy(zToken, p->u.zToken, nToken);
92806 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
92807 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
92809 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
92811 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
92815 /* Fill in pNew->pLeft and pNew->pRight. */
92819 pNew->pLeft = p->pLeft ?
92820 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
92821 pNew->pRight = p->pRight ?
92822 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
92829 if( pNew->op==TK_SELECT_COLUMN ){
92830 pNew->pLeft = p->pLeft;
92831 assert( p->iColumn==0 || p->pRight==0 );
92832 assert( p->pRight==0 || p->pRight==p->pLeft );
92834 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
92836 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
92846 ** and the db->mallocFailed flag set.
92852 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
92856 pRet->nCte = p->nCte;
92857 for(i=0; i<p->nCte; i++){
92858 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
92859 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
92860 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
92885 ** part of the in-memory representation of the database schema.
92899 sizeof(*pNew)+sizeof(pNew->a[0])*(p->nExpr-1) );
92901 pNew->nAlloc = pNew->nExpr = p->nExpr;
92902 pItem = pNew->a;
92903 pOldItem = p->a;
92904 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
92905 Expr *pOldExpr = pOldItem->pExpr;
92907 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
92909 && pOldExpr->op==TK_SELECT_COLUMN
92910 && (pNewExpr = pItem->pExpr)!=0
92912 assert( pNewExpr->iColumn==0 || i>0 );
92913 if( pNewExpr->iColumn==0 ){
92914 assert( pOldExpr->pLeft==pOldExpr->pRight );
92915 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
92918 assert( pItem[-1].pExpr!=0 );
92919 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
92920 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
92921 pNewExpr->pLeft = pPriorSelectCol;
92924 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
92925 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
92926 pItem->sortOrder = pOldItem->sortOrder;
92927 pItem->done = 0;
92928 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
92929 pItem->u = pOldItem->u;
92948 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
92951 pNew->nSrc = pNew->nAlloc = p->nSrc;
92952 for(i=0; i<p->nSrc; i++){
92953 struct SrcList_item *pNewItem = &pNew->a[i];
92954 struct SrcList_item *pOldItem = &p->a[i];
92956 pNewItem->pSchema = pOldItem->pSchema;
92957 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
92958 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
92959 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
92960 pNewItem->fg = pOldItem->fg;
92961 pNewItem->iCursor = pOldItem->iCursor;
92962 pNewItem->addrFillSub = pOldItem->addrFillSub;
92963 pNewItem->regReturn = pOldItem->regReturn;
92964 if( pNewItem->fg.isIndexedBy ){
92965 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
92967 pNewItem->pIBIndex = pOldItem->pIBIndex;
92968 if( pNewItem->fg.isTabFunc ){
92969 pNewItem->u1.pFuncArg =
92970 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
92972 pTab = pNewItem->pTab = pOldItem->pTab;
92974 pTab->nTabRef++;
92976 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
92977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
92978 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
92979 pNewItem->colUsed = pOldItem->colUsed;
92990 pNew->nId = p->nId;
92991 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
92992 if( pNew->a==0 ){
92996 /* Note that because the size of the allocation for p->a[] is not
92999 for(i=0; i<p->nId; i++){
93000 struct IdList_item *pNewItem = &pNew->a[i];
93001 struct IdList_item *pOldItem = &p->a[i];
93002 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
93003 pNewItem->idx = pOldItem->idx;
93014 for(p=pDup; p; p=p->pPrior){
93017 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
93018 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
93019 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
93020 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
93021 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
93022 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
93023 pNew->op = p->op;
93024 pNew->pNext = pNext;
93025 pNew->pPrior = 0;
93026 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
93027 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
93028 pNew->iLimit = 0;
93029 pNew->iOffset = 0;
93030 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
93031 pNew->addrOpenEphm[0] = -1;
93032 pNew->addrOpenEphm[1] = -1;
93033 pNew->nSelectRow = p->nSelectRow;
93034 pNew->pWith = withDup(db, p->pWith);
93035 sqlite3SelectSetName(pNew, p->zSelName);
93037 pp = &pNew->pPrior;
93056 ** NULL is returned. If non-NULL is returned, then it is guaranteed
93065 sqlite3 *db = pParse->db;
93072 pList->nExpr = 0;
93073 pList->nAlloc = 1;
93074 }else if( pList->nExpr==pList->nAlloc ){
93077 sizeof(*pList)+(2*pList->nAlloc - 1)*sizeof(pList->a[0]));
93082 pList->nAlloc *= 2;
93084 pItem = &pList->a[pList->nExpr++];
93085 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
93087 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
93088 pItem->pExpr = pExpr;
93115 sqlite3 *db = pParse->db;
93118 int iFirst = pList ? pList->nExpr : 0;
93129 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
93131 pColumns->nId, n);
93135 for(i=0; i<pColumns->nId; i++){
93139 assert( pList->nExpr==iFirst+i+1 );
93140 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
93141 pColumns->a[i].zName = 0;
93145 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
93146 Expr *pFirst = pList->a[iFirst].pExpr;
93148 assert( pFirst->op==TK_SELECT_COLUMN );
93152 pFirst->pRight = pExpr;
93157 pFirst->iTable = pColumns->nId;
93172 assert( p->nExpr>0 );
93174 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
93177 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
93185 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
93194 assert( pList!=0 || pParse->db->mallocFailed!=0 );
93197 assert( pList->nExpr>0 );
93198 pItem = &pList->a[pList->nExpr-1];
93199 assert( pItem->zName==0 );
93200 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
93201 if( dequote ) sqlite3Dequote(pItem->zName);
93210 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
93218 sqlite3 *db = pParse->db;
93219 assert( pList!=0 || db->mallocFailed!=0 );
93221 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
93222 assert( pList->nExpr>0 );
93223 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
93224 sqlite3DbFree(db, pItem->zSpan);
93225 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
93226 (int)(pSpan->zEnd - pSpan->zStart));
93239 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
93240 testcase( pEList && pEList->nExpr==mx );
93241 testcase( pEList && pEList->nExpr==mx+1 );
93242 if( pEList && pEList->nExpr>mx ){
93251 int i = pList->nExpr;
93252 struct ExprList_item *pItem = pList->a;
93253 assert( pList->nExpr>0 );
93255 sqlite3ExprDelete(db, pItem->pExpr);
93256 sqlite3DbFree(db, pItem->zName);
93257 sqlite3DbFree(db, pItem->zSpan);
93259 }while( --i>0 );
93267 ** Return the bitwise-OR of all Expr.flags fields in the given
93274 for(i=0; i<pList->nExpr; i++){
93275 Expr *pExpr = pList->a[i].pExpr;
93277 m |= pExpr->flags;
93291 ** sqlite3ExprIsConstant() pWalker->eCode==1
93292 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
93293 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
93294 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
93300 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
93310 /* If pWalker->eCode is 2 then any term of the expression that comes from
93313 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
93314 pWalker->eCode = 0;
93318 switch( pExpr->op ){
93320 ** and either pWalker->eCode==4 or 5 or the function has the
93323 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
93326 pWalker->eCode = 0;
93333 testcase( pExpr->op==TK_ID );
93334 testcase( pExpr->op==TK_COLUMN );
93335 testcase( pExpr->op==TK_AGG_FUNCTION );
93336 testcase( pExpr->op==TK_AGG_COLUMN );
93337 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
93342 testcase( pExpr->op==TK_IF_NULL_ROW );
93343 pWalker->eCode = 0;
93346 if( pWalker->eCode==5 ){
93350 pExpr->op = TK_NULL;
93351 }else if( pWalker->eCode==4 ){
93354 pWalker->eCode = 0;
93359 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
93360 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
93366 pWalker->eCode = 0;
93383 ** Walk an expression tree. Return non-zero if the expression is constant
93386 ** For the purposes of this function, a double-quoted string (ex: "abc")
93387 ** is considered a variable but a single-quoted string (ex: 'abc') is
93395 ** Walk an expression tree. Return non-zero if the expression is constant
93405 ** Walk an expression tree. Return non-zero if the expression is constant
93407 ** expression must not refer to any non-deterministic function nor any
93419 ExprList *pGroupBy = pWalker->u.pGroupBy;
93424 for(i=0; i<pGroupBy->nExpr; i++){
93425 Expr *p = pGroupBy->a[i].pExpr;
93426 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
93427 CollSeq *pColl = sqlite3ExprCollSeq(pWalker->pParse, p);
93428 if( pColl==0 || sqlite3_stricmp("BINARY", pColl->zName)==0 ){
93434 /* Check if pExpr is a sub-select. If so, consider it variable. */
93436 pWalker->eCode = 0;
93444 ** Walk the expression tree passed as the first argument. Return non-zero
93452 ** assumes that no other collating sequence will have a finer-grained
93474 ** Walk an expression tree. Return non-zero if the expression is constant
93478 ** For the purposes of this function, a double-quoted string (ex: "abc")
93479 ** is considered a variable but a single-quoted string (ex: 'abc') is
93507 ** to fit in a 32-bit integer, return 1 and put the value of the integer
93509 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
93515 /* If an expression is an integer literal that fits in a signed 32-bit
93517 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
93518 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
93520 if( p->flags & EP_IntValue ){
93521 *pValue = p->u.iValue;
93524 switch( p->op ){
93526 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
93531 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
93532 assert( v!=(-2147483647-1) );
93533 *pValue = -v;
93559 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
93560 op = p->op;
93561 if( op==TK_REGISTER ) op = p->op2;
93569 assert( p->pTab!=0 );
93571 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
93590 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
93591 op = p->op;
93592 if( op==TK_REGISTER ) op = p->op2;
93607 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
93608 return p->iColumn<0
93618 ** Return TRUE if the given string is a row-id column name.
93643 p = pX->x.pSelect;
93644 if( p->pPrior ) return 0; /* Not a compound SELECT */
93645 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
93646 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
93647 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
93650 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
93651 if( p->pLimit ) return 0; /* Has no LIMIT clause */
93652 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
93653 if( p->pWhere ) return 0; /* Has no WHERE clause */
93654 pSrc = p->pSrc;
93656 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
93657 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
93658 pTab = pSrc->a[0].pTab;
93660 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
93662 pEList = p->pEList;
93665 for(i=0; i<pEList->nExpr; i++){
93666 Expr *pRes = pEList->a[i].pExpr;
93667 if( pRes->op!=TK_COLUMN ) return 0;
93668 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
93676 ** Generate code that checks the left-most column of index table iCur to see if
93678 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
93696 ** right-hand side. Return TRUE if that list is constant.
93702 pLHS = pIn->pLeft;
93703 pIn->pLeft = 0;
93705 pIn->pLeft = pLHS;
93715 ** The job of this routine is to find or create a b-tree object that can
93719 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
93720 ** and pX->iTable is set to the index of that cursor.
93722 ** The returned value of this function indicates the b-tree type, as follows:
93724 ** IN_INDEX_ROWID - The cursor was opened on a database table.
93725 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
93726 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
93727 ** IN_INDEX_EPH - The cursor was opened on a specially created and
93729 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
93732 ** An existing b-tree might be used if the RHS expression pX is a simple
93739 ** pX->iTable made to point to the ephemeral table instead of an
93749 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
93750 ** through the set members) then the b-tree must not contain duplicates.
93752 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
93755 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
93758 ** index can be found with the specified <columns> as its left-most.
93762 ** routine might decide that creating an ephemeral b-tree for membership
93767 ** When the b-tree is being used for membership tests, the calling function
93776 ** the value in that register will be NULL if the b-tree contains one or more
93777 ** NULL values, and it will be some non-NULL value if the b-tree contains no
93794 Expr *pX, /* The right-hand side (RHS) of the IN operator */
93801 int iTab = pParse->nTab++; /* Cursor of the RHS table */
93805 assert( pX->op==TK_IN );
93813 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
93815 ExprList *pEList = pX->x.pSelect->pEList;
93816 for(i=0; i<pEList->nExpr; i++){
93817 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
93819 if( i==pEList->nExpr ){
93827 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
93828 sqlite3 *db = pParse->db; /* Database connection */
93831 ExprList *pEList = p->pEList;
93832 int nExpr = pEList->nExpr;
93834 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
93835 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
93836 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
93837 pTab = p->pSrc->a[0].pTab;
93840 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
93842 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
93845 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
93864 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
93865 int iCol = pEList->a[i].pExpr->iColumn;
93887 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
93890 if( pIdx->nColumn<nExpr ) continue;
93891 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
93893 testcase( pIdx->nColumn==BMS-2 );
93894 testcase( pIdx->nColumn==BMS-1 );
93895 if( pIdx->nColumn>=BMS-1 ) continue;
93897 if( pIdx->nKeyCol>nExpr
93898 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
93906 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
93907 Expr *pRhs = pEList->a[i].pExpr;
93911 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
93913 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
93914 assert( pIdx->azColl[j] );
93915 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
93927 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
93928 if( colUsed==(MASKBIT(nExpr)-1) ){
93933 sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
93936 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
93938 VdbeComment((v, "%s", pIdx->zName));
93940 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
93944 i64 mask = (1<<nExpr)-1;
93948 *prRhsHasNull = ++pParse->nMem;
93970 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
93976 /* Could not find an existing table or index to use as the RHS b-tree.
93979 u32 savedNQueryLoop = pParse->nQueryLoop;
93983 pParse->nQueryLoop = 0;
93984 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
93988 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
93991 pParse->nQueryLoop = savedNQueryLoop;
93993 pX->iTable = iTab;
93998 n = sqlite3ExprVectorSize(pX->pLeft);
94008 ** function allocates and returns a nul-terminated string containing
94015 Expr *pLeft = pExpr->pLeft;
94017 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
94020 assert( pExpr->op==TK_IN );
94021 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
94028 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
94044 ** "sub-select returns N columns - expected M"
94047 const char *zFmt = "sub-select returns %d columns - expected %d";
94054 ** it is not permitted. If pExpr is a sub-select vector, this routine
94057 ** "sub-select returns N columns - expected 1"
94065 if( pExpr->flags & EP_xIsSelect ){
94066 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
94078 ** (SELECT a FROM b) -- subquery
94079 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
94080 ** x IN (4,5,11) -- IN operator with list on right-hand side
94081 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
94086 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
94088 ** to some integer key column of a table B-Tree. In this case, use an
94089 ** intkey B-Tree to store the set of IN(...) values instead of the usual
94090 ** (slower) variable length keys B-Tree.
94092 ** If rMayHaveNull is non-zero, that means that the operation is an IN
94096 ** value to non-NULL if the RHS is NULL-free.
94099 ** result. For a multi-column SELECT, the result is stored in a contiguous
94100 ** array of registers and the return value is the register of the left-most
94110 int jmpIfDynamic = -1; /* One-time test address */
94119 ** * The right-hand side is a correlated subquery
94120 ** * The right-hand side is an expression list containing variables
94131 if( pParse->explain==2 ){
94132 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
94134 pExpr->op==TK_IN?"LIST":"SCALAR",
94135 pParse->iNextSelectId
94137 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
94141 switch( pExpr->op ){
94144 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
94164 pExpr->iTable = pParse->nTab++;
94166 pExpr->iTable, (isRowid?0:nVal));
94167 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
94175 Select *pSelect = pExpr->x.pSelect;
94176 ExprList *pEList = pSelect->pEList;
94181 if( ALWAYS(pEList->nExpr==nVal) ){
94184 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
94186 pSelect->iLimit = 0;
94187 testcase( pSelect->selFlags & SF_Distinct );
94190 sqlite3DbFree(pParse->db, dest.zAffSdst);
94194 sqlite3DbFree(pParse->db, dest.zAffSdst);
94197 assert( pEList->nExpr>0 );
94201 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
94202 pParse, p, pEList->a[i].pExpr
94206 }else if( ALWAYS(pExpr->x.pList!=0) ){
94216 ExprList *pList = pExpr->x.pList;
94226 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
94233 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
94234 Expr *pE2 = pItem->pExpr;
94239 ** this code only executes once. Because for a non-constant
94244 jmpIfDynamic = -1;
94249 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
94256 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
94260 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
94293 testcase( pExpr->op==TK_EXISTS );
94294 testcase( pExpr->op==TK_SELECT );
94295 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
94298 pSel = pExpr->x.pSelect;
94299 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
94300 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
94301 pParse->nMem += nReg;
94302 if( pExpr->op==TK_SELECT ){
94306 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
94313 sqlite3ExprDelete(pParse->db, pSel->pLimit);
94314 pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,
94316 pSel->iLimit = 0;
94317 pSel->selFlags &= ~SF_MultiValue;
94328 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
94343 ** sub-select on the RHS of the IN() operator has the same number of
94345 ** a sub-query, that the LHS is a vector of size 1.
94348 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
94349 if( (pIn->flags & EP_xIsSelect) ){
94350 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
94351 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
94355 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
94369 ** The left-hand side (LHS) is a scalar or vector expression. The
94370 ** right-hand side (RHS) is an array of zero or more scalar values, or a
94385 ** See the separate in-operator.md documentation file in the canonical
94409 int addrTop; /* Top of the step-6 loop */
94411 pLeft = pExpr->pLeft;
94414 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
94416 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
94418 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
94421 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
94424 v = pParse->pVdbe;
94431 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
94436 ** nVector-1. */
94471 ** This is step (1) in the in-operator.md optimized algorithm.
94474 ExprList *pList = pExpr->x.pList;
94475 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
94485 for(ii=0; ii<pList->nExpr; ii++){
94486 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
94487 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
94490 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
94493 VdbeCoverageIf(v, ii<pList->nExpr-1);
94494 VdbeCoverageIf(v, ii==pList->nExpr-1);
94523 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
94530 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
94535 /* In this case, the RHS is the ROWID of table b-tree and so we also
94536 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
94538 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
94544 /* Combine Step 3 and Step 5 into a single opcode */
94545 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
94550 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
94554 /* Step 4. If the RHS is known to be non-NULL and we did not find
94562 /* Step 5. If we do not care about the difference between NULL and
94575 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
94590 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
94599 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
94615 sqlite3DbFree(pParse->db, aiMap);
94616 sqlite3DbFree(pParse->db, zAff);
94623 ** value described by z[0..n-1] into register iMem.
94625 ** The z[] string will probably not be zero-terminated. But the
94634 if( negateFlag ) value = -value;
94643 ** text z[0..n-1] into register iMem.
94645 ** Expr.u.zToken is always UTF8 and zero-terminated.
94648 Vdbe *v = pParse->pVdbe;
94649 if( pExpr->flags & EP_IntValue ){
94650 int i = pExpr->u.iValue;
94652 if( negFlag ) i = -i;
94657 const char *z = pExpr->u.zToken;
94662 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
94666 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
94674 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
94681 ** Erase column-cache entry number i
94684 if( pParse->aColCache[i].tempReg ){
94685 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
94686 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
94689 pParse->nColCache--;
94690 if( i<pParse->nColCache ){
94691 pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
94707 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
94708 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
94711 ** for testing only - to verify that SQLite always gets the same answer
94714 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
94722 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94723 assert( p->iTable!=iTab || p->iColumn!=iCol );
94728 if( pParse->nColCache>=SQLITE_N_COLCACHE ){
94730 idxLru = -1;
94731 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
94732 if( p->lru<minLru ){
94734 minLru = p->lru;
94737 p = &pParse->aColCache[idxLru];
94739 p = &pParse->aColCache[pParse->nColCache++];
94743 p->iLevel = pParse->iCacheLevel;
94744 p->iTable = iTab;
94745 p->iColumn = iCol;
94746 p->iReg = iReg;
94747 p->tempReg = 0;
94748 p->lru = pParse->iCacheCnt++;
94752 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
94757 while( i<pParse->nColCache ){
94758 struct yColCache *p = &pParse->aColCache[i];
94759 if( p->iReg >= iReg && p->iReg < iReg+nReg ){
94773 pParse->iCacheLevel++;
94775 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
94776 printf("PUSH to %d\n", pParse->iCacheLevel);
94788 assert( pParse->iCacheLevel>=1 );
94789 pParse->iCacheLevel--;
94791 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
94792 printf("POP to %d\n", pParse->iCacheLevel);
94795 while( i<pParse->nColCache ){
94796 if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
94813 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94814 if( p->iReg==iReg ){
94815 p->tempReg = 0;
94821 ** appropriate for the iIdxCol-th column of index pIdx.
94830 i16 iTabCol = pIdx->aiColumn[iIdxCol];
94832 assert( pIdx->aColExpr );
94833 assert( pIdx->aColExpr->nExpr>iIdxCol );
94834 pParse->iSelfTab = iTabCur + 1;
94835 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
94836 pParse->iSelfTab = 0;
94838 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
94844 ** Generate code to extract the value of the iCol-th column of a table.
94857 if( iCol<0 || iCol==pTab->iPKey ){
94873 ** Generate code that will extract the iColumn-th column from
94877 ** is not garanteeed for GetColumn() - the result can be stored in
94892 Vdbe *v = pParse->pVdbe;
94896 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94897 if( p->iTable==iTable && p->iColumn==iColumn ){
94898 p->lru = pParse->iCacheCnt++;
94899 sqlite3ExprCachePinRegister(pParse, p->iReg);
94900 return p->iReg;
94920 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
94931 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
94935 for(i=0; i<pParse->nColCache; i++){
94936 if( pParse->aColCache[i].tempReg
94937 && pParse->nTempReg<ArraySize(pParse->aTempReg)
94939 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
94942 pParse->nColCache = 0;
94954 ** Generate code to move content from registers iFrom...iFrom+nReg-1
94955 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
94959 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
94974 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94975 int r = p->iReg;
94989 p->op2 = p->op;
94990 p->op = TK_REGISTER;
94991 p->iTable = iReg;
95012 if( p->op==TK_SELECT ){
95020 iResult = pParse->nMem+1;
95021 pParse->nMem += nResult;
95023 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
95043 Vdbe *v = pParse->pVdbe; /* The VM under construction */
95046 int regFree1 = 0; /* If non-zero free this temporary register */
95047 int regFree2 = 0; /* If non-zero free this temporary register */
95052 assert( target>0 && target<=pParse->nMem );
95054 assert( pParse->db->mallocFailed );
95061 op = pExpr->op;
95065 AggInfo *pAggInfo = pExpr->pAggInfo;
95066 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
95067 if( !pAggInfo->directMode ){
95068 assert( pCol->iMem>0 );
95069 return pCol->iMem;
95070 }else if( pAggInfo->useSortingIdx ){
95071 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
95072 pCol->iSorterColumn, target);
95078 int iTab = pExpr->iTable;
95080 if( pParse->iSelfTab<0 ){
95082 return pExpr->iColumn - pParse->iSelfTab;
95086 iTab = pParse->iSelfTab - 1;
95089 return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
95090 pExpr->iColumn, iTab, target,
95091 pExpr->op2);
95100 codeReal(v, pExpr->u.zToken, 0, target);
95106 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
95119 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
95120 assert( pExpr->u.zToken[1]=='\'' );
95121 z = &pExpr->u.zToken[2];
95122 n = sqlite3Strlen30(z) - 1;
95131 assert( pExpr->u.zToken!=0 );
95132 assert( pExpr->u.zToken[0]!=0 );
95133 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
95134 if( pExpr->u.zToken[1]!=0 ){
95135 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
95136 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
95137 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
95143 return pExpr->iTable;
95148 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
95154 sqlite3AffinityType(pExpr->u.zToken, 0));
95164 /* fall-through */
95171 Expr *pLeft = pExpr->pLeft;
95176 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
95177 codeCompare(pParse, pLeft, pExpr->pRight, op,
95213 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
95214 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
95221 Expr *pLeft = pExpr->pLeft;
95223 if( pLeft->op==TK_INTEGER ){
95227 }else if( pLeft->op==TK_FLOAT ){
95229 codeReal(v, pLeft->u.zToken, 1, target);
95237 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
95247 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
95258 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
95268 AggInfo *pInfo = pExpr->pAggInfo;
95271 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
95273 return pInfo->aFunc[pExpr->iAgg].iMem;
95284 sqlite3 *db = pParse->db; /* The database connection */
95291 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
95297 pFarg = pExpr->x.pList;
95299 nFarg = pFarg ? pFarg->nExpr : 0;
95301 zId = pExpr->u.zToken;
95304 if( pDef==0 && pParse->explain ){
95308 if( pDef==0 || pDef->xFinalize!=0 ){
95313 /* Attempt a direct implementation of the built-in COALESCE() and
95315 ** arguments past the first non-NULL argument.
95317 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
95320 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
95326 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
95333 /* The UNLIKELY() function is a no-op. The result is the value
95336 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
95338 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
95346 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
95350 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
95352 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
95358 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
95362 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
95363 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
95368 r1 = pParse->nMem+1;
95369 pParse->nMem += nFarg;
95379 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
95382 assert( pFarg->a[0].pExpr!=0 );
95383 exprOp = pFarg->a[0].pExpr->op;
95387 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
95388 pFarg->a[0].pExpr->op2 =
95389 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
95413 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
95414 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
95416 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
95419 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
95420 if( !pColl ) pColl = db->pDfltColl;
95423 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
95437 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
95446 if( pExpr->pLeft->iTable==0 ){
95447 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
95449 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
95450 if( pExpr->iTable
95451 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
95454 pExpr->iTable, n);
95456 return pExpr->pLeft->iTable + pExpr->iColumn;
95479 ** X is stored in pExpr->pLeft.
95480 ** Y is stored in pExpr->pList->a[0].pExpr.
95481 ** Z is stored in pExpr->pList->a[1].pExpr.
95490 return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
95495 ** to a column in the new.* or old.* pseudo-tables available to
95497 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
95498 ** is set to the column of the pseudo-table to read, or to -1 to
95503 ** to reference another column of the old.* pseudo-table, where
95505 ** set to (n+1), where n is the number of columns in each pseudo-table.
95506 ** For a reference to any other column in the new.* pseudo-table, p1
95515 ** p1==0 -> old.rowid p1==3 -> new.rowid
95516 ** p1==1 -> old.a p1==4 -> new.a
95517 ** p1==2 -> old.b p1==5 -> new.b
95519 Table *pTab = pExpr->pTab;
95520 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
95522 assert( pExpr->iTable==0 || pExpr->iTable==1 );
95523 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
95524 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
95525 assert( p1>=0 && p1<(pTab->nCol*2+2) );
95528 VdbeComment((v, "%s.%s -> $%d",
95529 (pExpr->iTable ? "new" : "old"),
95530 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
95538 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
95540 if( pExpr->iColumn>=0
95541 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
95556 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
95558 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
95576 ** X (if it exists) is in pExpr->pLeft.
95577 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
95580 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
95596 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
95598 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
95599 assert(pExpr->x.pList->nExpr > 0);
95600 pEList = pExpr->x.pList;
95601 aListelem = pEList->a;
95602 nExpr = pEList->nExpr;
95604 if( (pX = pExpr->pLeft)!=0 ){
95606 testcase( pX->op==TK_COLUMN );
95614 ** The value in regFree1 might get SCopy-ed into the file result.
95619 for(i=0; i<nExpr-1; i=i+2){
95628 testcase( pTest->op==TK_COLUMN );
95630 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
95638 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
95643 assert( pParse->db->mallocFailed || pParse->nErr>0
95644 || pParse->iCacheLevel==iCacheLevel );
95650 assert( pExpr->affinity==OE_Rollback
95651 || pExpr->affinity==OE_Abort
95652 || pExpr->affinity==OE_Fail
95653 || pExpr->affinity==OE_Ignore
95655 if( !pParse->pTriggerTab ){
95657 "RAISE() may only be used within a trigger-program");
95660 if( pExpr->affinity==OE_Abort ){
95664 if( pExpr->affinity==OE_Ignore ){
95666 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
95670 pExpr->affinity, pExpr->u.zToken, 0, 0);
95698 p = pParse->pConstExpr;
95702 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
95703 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
95704 return pItem->u.iConstExprReg;
95708 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
95711 struct ExprList_item *pItem = &p->a[p->nExpr-1];
95712 pItem->reusable = regDest<0;
95713 if( regDest<0 ) regDest = ++pParse->nMem;
95714 pItem->u.iConstExprReg = regDest;
95716 pParse->pConstExpr = p;
95737 && pExpr->op!=TK_REGISTER
95741 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
95763 assert( target>0 && target<=pParse->nMem );
95764 if( pExpr && pExpr->op==TK_REGISTER ){
95765 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
95768 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
95769 if( inReg!=target && pParse->pVdbe ){
95770 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
95781 sqlite3 *db = pParse->db;
95783 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
95794 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
95814 Vdbe *v = pParse->pVdbe;
95818 assert( pExpr->op!=TK_REGISTER );
95820 iMem = ++pParse->nMem;
95851 Vdbe *v = pParse->pVdbe;
95854 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
95855 n = pList->nExpr;
95857 for(pItem=pList->a, i=0; i<n; i++, pItem++){
95858 Expr *pExpr = pItem->pExpr;
95859 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
95861 i--;
95862 n--;
95864 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
95873 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
95874 && pOp->p1+pOp->p3+1==inReg
95875 && pOp->p2+pOp->p3+1==target+i
95877 pOp->p3++;
95926 exprX = *pExpr->pLeft;
95932 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
95935 compRight.pRight = pExpr->x.pList->a[1].pExpr;
95977 Vdbe *v = pParse->pVdbe;
95986 op = pExpr->op;
95991 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
95993 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
96000 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
96002 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
96008 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
96024 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
96026 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
96027 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
96028 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
96048 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
96075 /* No-op */
96100 Vdbe *v = pParse->pVdbe;
96110 /* The value of pExpr->op and op are related as follows:
96112 ** pExpr->op op
96113 ** --------- ----------
96123 ** For other values of pExpr->op, op is undefined and unused.
96128 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
96132 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
96133 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
96134 assert( pExpr->op!=TK_NE || op==OP_Eq );
96135 assert( pExpr->op!=TK_EQ || op==OP_Ne );
96136 assert( pExpr->op!=TK_LT || op==OP_Ge );
96137 assert( pExpr->op!=TK_LE || op==OP_Gt );
96138 assert( pExpr->op!=TK_GT || op==OP_Le );
96139 assert( pExpr->op!=TK_GE || op==OP_Lt );
96141 switch( pExpr->op ){
96144 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
96146 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
96153 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
96155 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
96162 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
96167 testcase( pExpr->op==TK_IS );
96168 testcase( pExpr->op==TK_ISNOT );
96169 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
96178 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
96180 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
96181 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
96182 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
96200 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
96229 /* no-op */
96250 sqlite3 *db = pParse->db;
96252 if( db->mallocFailed==0 ){
96262 ** If pExpr is a simple SQL value - an integer, real, string, blob
96263 ** or NULL value - then the VDBE currently being prepared is configured
96264 ** to re-prepare each time a new value is bound to variable pVar.
96267 ** same as that currently bound to variable pVar, non-zero is returned.
96276 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
96278 iVar = pVar->iColumn;
96279 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
96280 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
96283 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
96298 ** other than the top-level COLLATE operator.
96300 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
96312 ** this routine is used, it does not hurt to get an extra 2 - that
96317 ** pParse->pReprepare can be matched against literals in pB. The
96318 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
96328 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
96331 combinedFlags = pA->flags | pB->flags;
96333 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
96338 if( pA->op!=pB->op ){
96339 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
96342 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
96347 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
96348 if( pA->op==TK_FUNCTION ){
96349 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
96350 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
96351 return pA->op==TK_COLLATE ? 1 : 2;
96354 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
96357 if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
96358 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
96359 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
96360 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
96361 if( pA->iColumn!=pB->iColumn ) return 2;
96362 if( pA->iTable!=pB->iTable
96363 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
96371 ** non-zero if they differ in any way.
96373 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
96376 ** This routine might return non-zero for equivalent ExprLists. The
96382 ** always differs from a non-NULL pointer.
96388 if( pA->nExpr!=pB->nExpr ) return 1;
96389 for(i=0; i<pA->nExpr; i++){
96390 Expr *pExprA = pA->a[i].pExpr;
96391 Expr *pExprB = pB->a[i].pExpr;
96392 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
96399 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
96414 ** pE1: x==5 pE2: x==5 Result: true
96415 ** pE1: x>0 pE2: x==5 Result: false
96426 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
96438 if( pE2->op==TK_OR
96439 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
96440 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
96444 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
96445 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
96446 testcase( pX!=pE1->pLeft );
96447 if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
96466 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
96467 ** pWalker->u.pIdxCover->pIdx.
96470 if( pExpr->op==TK_COLUMN
96471 && pExpr->iTable==pWalker->u.pIdxCover->iCur
96472 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
96474 pWalker->eCode = 1;
96528 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
96530 struct SrcCount *p = pWalker->u.pSrcCount;
96531 SrcList *pSrc = p->pSrc;
96532 int nSrc = pSrc ? pSrc->nSrc : 0;
96534 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
96537 p->nThis++;
96539 p->nOther++;
96554 assert( pExpr->op==TK_AGG_FUNCTION );
96561 sqlite3WalkExprList(&w, pExpr->x.pList);
96566 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
96571 pInfo->aCol = sqlite3ArrayAllocate(
96573 pInfo->aCol,
96574 sizeof(pInfo->aCol[0]),
96575 &pInfo->nColumn,
96582 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
96587 pInfo->aFunc = sqlite3ArrayAllocate(
96589 pInfo->aFunc,
96590 sizeof(pInfo->aFunc[0]),
96591 &pInfo->nFunc,
96604 NameContext *pNC = pWalker->u.pNC;
96605 Parse *pParse = pNC->pParse;
96606 SrcList *pSrcList = pNC->pSrcList;
96607 AggInfo *pAggInfo = pNC->pAggInfo;
96609 switch( pExpr->op ){
96612 testcase( pExpr->op==TK_AGG_COLUMN );
96613 testcase( pExpr->op==TK_COLUMN );
96617 struct SrcList_item *pItem = pSrcList->a;
96618 for(i=0; i<pSrcList->nSrc; i++, pItem++){
96621 if( pExpr->iTable==pItem->iCursor ){
96625 ** Make an entry for the column in pAggInfo->aCol[] if there
96629 pCol = pAggInfo->aCol;
96630 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
96631 if( pCol->iTable==pExpr->iTable &&
96632 pCol->iColumn==pExpr->iColumn ){
96636 if( (k>=pAggInfo->nColumn)
96637 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
96639 pCol = &pAggInfo->aCol[k];
96640 pCol->pTab = pExpr->pTab;
96641 pCol->iTable = pExpr->iTable;
96642 pCol->iColumn = pExpr->iColumn;
96643 pCol->iMem = ++pParse->nMem;
96644 pCol->iSorterColumn = -1;
96645 pCol->pExpr = pExpr;
96646 if( pAggInfo->pGroupBy ){
96648 ExprList *pGB = pAggInfo->pGroupBy;
96649 struct ExprList_item *pTerm = pGB->a;
96650 n = pGB->nExpr;
96652 Expr *pE = pTerm->pExpr;
96653 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
96654 pE->iColumn==pExpr->iColumn ){
96655 pCol->iSorterColumn = j;
96660 if( pCol->iSorterColumn<0 ){
96661 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
96664 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
96667 ** pAggInfo->aCol[] entry.
96670 pExpr->pAggInfo = pAggInfo;
96671 pExpr->op = TK_AGG_COLUMN;
96672 pExpr->iAgg = (i16)k;
96674 } /* endif pExpr->iTable==pItem->iCursor */
96680 if( (pNC->ncFlags & NC_InAggFunc)==0
96681 && pWalker->walkerDepth==pExpr->op2
96686 struct AggInfo_func *pItem = pAggInfo->aFunc;
96687 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
96688 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
96692 if( i>=pAggInfo->nFunc ){
96693 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
96695 u8 enc = ENC(pParse->db);
96696 i = addAggInfoFunc(pParse->db, pAggInfo);
96699 pItem = &pAggInfo->aFunc[i];
96700 pItem->pExpr = pExpr;
96701 pItem->iMem = ++pParse->nMem;
96703 pItem->pFunc = sqlite3FindFunction(pParse->db,
96704 pExpr->u.zToken,
96705 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
96706 if( pExpr->flags & EP_Distinct ){
96707 pItem->iDistinct = pParse->nTab++;
96709 pItem->iDistinct = -1;
96713 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
96717 pExpr->iAgg = (i16)i;
96718 pExpr->pAggInfo = pAggInfo;
96729 pWalker->walkerDepth++;
96734 pWalker->walkerDepth--;
96739 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
96753 assert( pNC->pSrcList!=0 );
96767 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
96768 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
96777 if( pParse->nTempReg==0 ){
96778 return ++pParse->nMem;
96780 return pParse->aTempReg[--pParse->nTempReg];
96792 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
96795 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
96796 if( p->iReg==iReg ){
96797 p->tempReg = 1;
96801 pParse->aTempReg[pParse->nTempReg++] = iReg;
96811 i = pParse->iRangeReg;
96812 n = pParse->nRangeReg;
96814 assert( !usedAsColumnCache(pParse, i, i+n-1) );
96815 pParse->iRangeReg += nReg;
96816 pParse->nRangeReg -= nReg;
96818 i = pParse->nMem+1;
96819 pParse->nMem += nReg;
96829 if( nReg>pParse->nRangeReg ){
96830 pParse->nRangeReg = nReg;
96831 pParse->iRangeReg = iReg;
96839 pParse->nTempReg = 0;
96840 pParse->nRangeReg = 0;
96851 if( pParse->nRangeReg>0
96852 && pParse->iRangeReg+pParse->nRangeReg > iFirst
96853 && pParse->iRangeReg <= iLast
96857 for(i=0; i<pParse->nTempReg; i++){
96858 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
96899 ** -> 'CREATE TABLE def(a, b, c)'
96902 ** -> 'CREATE INDEX i ON def(a, b, c)'
96923 ** statement is that the table name is the first non-space token that
96947 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
96949 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
96966 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
97002 (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
97013 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
97085 /* Variable tname now contains the token that is the old table-name
97088 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
97090 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
97096 ** Register built-in functions used to help implement ALTER TABLE
97148 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
97149 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
97164 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
97166 /* If the table is not located in the temp-db (in which case NULL is
97168 ** that is not part of the temp-db schema, add a clause to the WHERE
97171 if( pTab->pSchema!=pTempSchema ){
97172 sqlite3 *db = pParse->db;
97173 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
97174 if( pTrig->pSchema==pTempSchema ){
97175 zWhere = whereOrName(db, zWhere, pTrig->zName);
97180 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
97181 sqlite3DbFree(pParse->db, zWhere);
97192 ** pTab->zName if this function is being called to code part of an
97205 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
97206 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
97211 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
97212 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
97214 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
97219 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
97222 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
97240 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
97264 char *zName = 0; /* NULL-terminated version of pName */
97265 sqlite3 *db = pParse->db; /* Database connection */
97266 int nTabName; /* Number of UTF-8 characters in zTabName */
97272 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
97273 int savedDbFlags; /* Saved value of db->flags */
97275 savedDbFlags = db->flags;
97276 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
97277 assert( pSrc->nSrc==1 );
97278 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
97280 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
97282 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
97283 zDb = db->aDb[iDb].zDbSName;
97284 db->flags |= SQLITE_PreferBuiltin;
97302 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
97310 if( pTab->pSelect ){
97311 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
97318 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
97329 if( pVTab->pVtab->pModule->xRename==0 ){
97349 ** of any resources used by the v-table implementation (including other
97354 int i = ++pParse->nMem;
97361 /* figure out how many UTF-8 characters are in zName */
97362 zTabName = pTab->zName;
97363 nTabName = sqlite3Utf8CharLen(zTabName, -1);
97366 if( db->flags&SQLITE_ForeignKeys ){
97367 /* If foreign-key support is enabled, rewrite the CREATE TABLE
97412 zDb, zName, pTab->zName);
97432 if( db->flags&SQLITE_ForeignKeys ){
97434 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
97435 Table *pFrom = p->pFrom;
97437 reloadTableSchema(pParse, p->pFrom, pFrom->zName);
97449 db->flags = savedDbFlags;
97457 ** The Table structure pParse->pNewTable was extended to include
97461 Table *pNew; /* Copy of pParse->pNewTable */
97466 char *zCol; /* Null-terminated column definition */
97470 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
97473 db = pParse->db;
97474 if( pParse->nErr || db->mallocFailed ) return;
97476 pNew = pParse->pNewTable;
97480 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
97481 zDb = db->aDb[iDb].zDbSName;
97482 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
97483 pCol = &pNew->aCol[pNew->nCol-1];
97484 pDflt = pCol->pDflt;
97490 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
97499 assert( pDflt==0 || pDflt->op==TK_SPAN );
97500 if( pDflt && pDflt->pLeft->op==TK_NULL ){
97508 if( pCol->colFlags & COLFLAG_PRIMKEY ){
97512 if( pNew->pIndex ){
97516 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
97518 "Cannot add a REFERENCES column with non-NULL default value");
97521 if( pCol->notNull && !pDflt ){
97536 assert( db->mallocFailed == 1 );
97540 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
97547 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
97549 char *zEnd = &zCol[pColDef->n-1];
97550 int savedDbFlags = db->flags;
97552 *zEnd-- = '\0';
97554 db->flags |= SQLITE_PreferBuiltin;
97559 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
97563 db->flags = savedDbFlags;
97573 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
97580 reloadTableSchema(pParse, pTab, pTab->zName);
97584 ** This function is called by the parser after the table-name in
97585 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
97586 ** pSrc is the full-name of the table being altered.
97605 sqlite3 *db = pParse->db;
97608 assert( pParse->pNewTable==0 );
97610 if( db->mallocFailed ) goto exit_begin_add_column;
97611 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
97622 if( pTab->pSelect ){
97626 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
97630 assert( pTab->addColOffset>0 );
97631 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97642 pParse->pNewTable = pNew;
97643 pNew->nTabRef = 1;
97644 pNew->nCol = pTab->nCol;
97645 assert( pNew->nCol>0 );
97646 nAlloc = (((pNew->nCol-1)/8)*8)+8;
97647 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
97648 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
97649 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
97650 if( !pNew->aCol || !pNew->zName ){
97651 assert( db->mallocFailed );
97654 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
97655 for(i=0; i<pNew->nCol; i++){
97656 Column *pCol = &pNew->aCol[i];
97657 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
97658 pCol->zColl = 0;
97659 pCol->pDflt = 0;
97661 pNew->pSchema = db->aDb[iDb].pSchema;
97662 pNew->addColOffset = pTab->addColOffset;
97663 pNew->nTabRef = 1;
97680 ** 2005-07-08
97730 ** columns. The N-th integer (for N>1) is the average number of rows in
97731 ** the index which have the same value for the first N-1 columns. For
97732 ** a K-column index, there will be K+1 integers in the stat column. If
97756 ** inclusive are samples of the left-most key value in the index taken at
97763 ** For i between 0 and S-1. Conceptually, the index space is divided into
97790 ** of entries in the index whose left-most column exactly matches
97791 ** the left-most column of the sample. The second integer in nEq
97797 ** left-most column is less than the left-most column of the sample.
97798 ** The K-th integer in the nLt entry is the number of index entries
97813 ** looks at the left-most column of the index. The sqlite_stat3.sample
97814 ** column contains the actual value of the left-most column instead
97841 ** appropriate compile-time options are provided.
97874 sqlite3 *db = pParse->db;
97883 pDb = &db->aDb[iDb];
97891 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
97894 ** side-effect of the CREATE TABLE statement is to leave the rootpage
97895 ** of the new table in register pParse->regRoot. This is important
97898 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
97900 aRoot[i] = pParse->regRoot;
97907 aRoot[i] = pStat->tnum;
97913 pDb->zDbSName, zTab, zWhereType, zWhere
97939 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
97967 u32 iPrn; /* Pseudo-random number used for sampling */
97982 if( p->nRowid ){
97983 sqlite3DbFree(db, p->u.aRowid);
97984 p->nRowid = 0;
97994 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
97995 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
97996 if( p->u.aRowid ){
97997 p->nRowid = n;
97998 memcpy(p->u.aRowid, pData, n);
98000 p->nRowid = 0;
98010 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
98011 p->nRowid = 0;
98012 p->u.iRowid = iRowid;
98022 pTo->isPSample = pFrom->isPSample;
98023 pTo->iCol = pFrom->iCol;
98024 pTo->iHash = pFrom->iHash;
98025 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
98026 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
98027 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
98028 if( pFrom->nRowid ){
98029 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
98031 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
98043 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
98044 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
98045 sampleClear(p->db, &p->current);
98047 sqlite3DbFree(p->db, p);
98114 p->db = db;
98115 p->nRow = 0;
98116 p->nCol = nCol;
98117 p->nKeyCol = nKeyCol;
98118 p->current.anDLt = (tRowcnt*)&p[1];
98119 p->current.anEq = &p->current.anDLt[nColUp];
98124 int i; /* Used to iterate through p->aSample[] */
98126 p->iGet = -1;
98127 p->mxSample = mxSample;
98128 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
98129 p->current.anLt = &p->current.anEq[nColUp];
98130 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
98133 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
98134 p->aBest = &p->a[mxSample];
98135 pSpace = (u8*)(&p->a[mxSample+nCol]);
98137 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
98138 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
98139 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
98141 assert( (pSpace - (u8*)p)==n );
98144 p->aBest[i].iCol = i;
98168 ** pNew and pOld are both candidate non-periodic samples selected for
98169 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
98176 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
98183 int nCol = pAccum->nCol;
98185 assert( pNew->iCol==pOld->iCol );
98186 for(i=pNew->iCol+1; i<nCol; i++){
98187 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
98188 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
98190 if( pNew->iHash>pOld->iHash ) return 1;
98200 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
98207 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
98208 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
98210 assert( pOld->isPSample==0 && pNew->isPSample==0 );
98211 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
98216 if( pNew->iCol<pOld->iCol ) return 1;
98217 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
98221 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
98226 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
98227 ** remove the least desirable sample from p->a[] to make room.
98240 if( nEqZero>p->nMaxEqZero ){
98241 p->nMaxEqZero = nEqZero;
98243 if( pNew->isPSample==0 ){
98245 assert( pNew->anEq[pNew->iCol]>0 );
98252 for(i=p->nSample-1; i>=0; i--){
98253 Stat4Sample *pOld = &p->a[i];
98254 if( pOld->anEq[pNew->iCol]==0 ){
98255 if( pOld->isPSample ) return;
98256 assert( pOld->iCol>pNew->iCol );
98264 pUpgrade->iCol = pNew->iCol;
98265 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
98272 if( p->nSample>=p->mxSample ){
98273 Stat4Sample *pMin = &p->a[p->iMin];
98274 tRowcnt *anEq = pMin->anEq;
98275 tRowcnt *anLt = pMin->anLt;
98276 tRowcnt *anDLt = pMin->anDLt;
98277 sampleClear(p->db, pMin);
98278 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
98279 pSample = &p->a[p->nSample-1];
98280 pSample->nRowid = 0;
98281 pSample->anEq = anEq;
98282 pSample->anDLt = anDLt;
98283 pSample->anLt = anLt;
98284 p->nSample = p->mxSample-1;
98287 /* The "rows less-than" for the rowid column must be greater than that
98288 ** for the last sample in the p->a[] array. Otherwise, the samples would
98291 assert( p->nSample==0
98292 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
98296 pSample = &p->a[p->nSample];
98298 p->nSample++;
98301 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
98306 if( p->nSample>=p->mxSample ){
98307 int iMin = -1;
98308 for(i=0; i<p->mxSample; i++){
98309 if( p->a[i].isPSample ) continue;
98310 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
98315 p->iMin = iMin;
98322 ** p->current contains a sample that reflects the previous row of the
98332 for(i=(p->nCol-2); i>=iChng; i--){
98333 Stat4Sample *pBest = &p->aBest[i];
98334 pBest->anEq[i] = p->current.anEq[i];
98335 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
98341 ** p->nMaxEqZero or greater set to zero. */
98342 for(i=p->nSample-1; i>=0; i--){
98344 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
98348 if( iChng<p->nMaxEqZero ){
98349 for(i=p->nSample-1; i>=0; i--){
98351 for(j=iChng; j<p->nCol; j++){
98352 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
98355 p->nMaxEqZero = iChng;
98361 tRowcnt nLt = p->current.anLt[0];
98362 tRowcnt nEq = p->current.anEq[0];
98365 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
98366 p->current.isPSample = 1;
98367 sampleInsert(p, &p->current, 0);
98368 p->current.isPSample = 0;
98371 /* Or if it is a non-periodic sample. Add it in this case too. */
98372 if( p->nSample<p->mxSample
98373 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
98375 sampleInsert(p, &p->current, 0);
98391 ** C Index of left-most column to differ from previous row
98415 assert( p->nCol>0 );
98416 assert( iChng<p->nCol );
98418 if( p->nRow==0 ){
98420 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
98428 p->current.anEq[i]++;
98430 for(i=iChng; i<p->nCol; i++){
98431 p->current.anDLt[i]++;
98433 p->current.anLt[i] += p->current.anEq[i];
98435 p->current.anEq[i] = 1;
98438 p->nRow++;
98441 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
98443 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
98446 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
98451 tRowcnt nLt = p->current.anLt[p->nCol-1];
98454 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
98455 p->current.isPSample = 1;
98456 p->current.iCol = 0;
98457 sampleInsert(p, &p->current, p->nCol-1);
98458 p->current.isPSample = 0;
98462 for(i=0; i<(p->nCol-1); i++){
98463 p->current.iCol = i;
98464 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
98465 sampleCopy(p, &p->aBest[i], &p->current);
98504 ** a one-parameter function, stat_get(P), that always returns the
98545 ** I = (K+D-1)/D
98550 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
98556 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
98558 for(i=0; i<p->nKeyCol; i++){
98559 u64 nDistinct = p->current.anDLt[i] + 1;
98560 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
98563 assert( p->current.anEq[i] );
98567 sqlite3_result_text(context, zRet, -1, sqlite3_free);
98571 if( p->iGet<0 ){
98573 p->iGet = 0;
98575 if( p->iGet<p->nSample ){
98576 Stat4Sample *pS = p->a + p->iGet;
98577 if( pS->nRowid==0 ){
98578 sqlite3_result_int64(context, pS->u.iRowid);
98580 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
98587 assert( p->iGet<p->nSample );
98589 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
98590 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
98592 aCnt = p->a[p->iGet].anDLt;
98593 p->iGet++;
98601 char *zRet = sqlite3MallocZero(p->nCol * 25);
98607 for(i=0; i<p->nCol; i++){
98612 z[-1] = '\0';
98613 sqlite3_result_text(context, zRet, -1, sqlite3_free);
98659 sqlite3 *db = pParse->db; /* Database handle */
98665 int jZeroRows = -1; /* Jump from here if number of rows is zero */
98680 pParse->nMem = MAX(pParse->nMem, iMem);
98685 if( pTab->tnum==0 ){
98689 if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
98694 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
98698 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
98699 db->aDb[iDb].zDbSName ) ){
98704 /* Establish a read-lock on the table at the shared-cache level.
98705 ** Open a read-only cursor on the table. Also allocate a cursor number
98708 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
98711 pParse->nTab = MAX(pParse->nTab, iTab);
98713 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
98715 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98723 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
98725 nCol = pIdx->nKeyCol;
98726 zIdxName = pTab->zName;
98727 nColTest = nCol - 1;
98729 nCol = pIdx->nColumn;
98730 zIdxName = pIdx->zName;
98731 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
98736 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
98739 ** Pseudo-code for loop that calls stat_push():
98774 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
98776 /* Open a read-only cursor on the index being analyzed. */
98777 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
98778 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
98780 VdbeComment((v, "%s", pIdx->zName));
98796 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
98832 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
98833 /* For a single-column UNIQUE index, once we have found a non-NULL
98840 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
98859 sqlite3VdbeJumpHere(v, addrNextRow-1);
98880 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
98882 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
98883 for(j=0; j<pPk->nKeyCol; j++){
98884 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
98885 assert( k>=0 && k<pIdx->nColumn );
98887 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
98889 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
98890 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
98920 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
98931 ** the previous loop. Thus the not-found jump of seekOp will never
98945 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
98959 VdbeComment((v, "%s", pTab->zName));
98988 sqlite3 *db = pParse->db;
98989 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
98996 iStatCur = pParse->nTab;
98997 pParse->nTab += 3;
98999 iMem = pParse->nMem+1;
99000 iTab = pParse->nTab;
99002 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
99019 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
99020 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
99022 iStatCur = pParse->nTab;
99023 pParse->nTab += 3;
99025 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
99027 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
99029 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
99037 ** ANALYZE -- 1
99038 ** ANALYZE <database> -- 2
99039 ** ANALYZE ?<database>.?<tablename> -- 3
99046 sqlite3 *db = pParse->db;
99057 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
99065 for(i=0; i<db->nDb; i++){
99069 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
99076 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
99080 analyzeTable(pParse, pIdx->pTable, pIdx);
99088 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
99104 ** The first argument points to a nul-terminated string containing a
99128 v = v*10 + c - '0';
99147 pIndex->bUnordered = 0;
99148 pIndex->noSkipScan = 0;
99151 pIndex->bUnordered = 1;
99152 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
99153 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
99155 pIndex->noSkipScan = 1;
99158 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
99159 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
99174 ** argv[2] = results of analysis - on integer for each column
99191 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
99200 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
99206 int nCol = pIndex->nKeyCol+1;
99211 if( pIndex->aiRowEst==0 ){
99212 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
99213 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
99215 aiRowEst = pIndex->aiRowEst;
99217 pIndex->bUnordered = 0;
99218 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
99219 pIndex->hasStat1 = 1;
99220 if( pIndex->pPartIdxWhere==0 ){
99221 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
99222 pTable->tabFlags |= TF_HasStat1;
99226 fakeIdx.szIdxRow = pTable->szTabRow;
99230 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
99231 pTable->szTabRow = fakeIdx.szIdxRow;
99232 pTable->tabFlags |= TF_HasStat1;
99244 if( pIdx->aSample ){
99246 for(j=0; j<pIdx->nSample; j++){
99247 IndexSample *p = &pIdx->aSample[j];
99248 sqlite3DbFree(db, p->p);
99250 sqlite3DbFree(db, pIdx->aSample);
99252 if( db && db->pnBytesFreed==0 ){
99253 pIdx->nSample = 0;
99254 pIdx->aSample = 0;
99264 ** Populate the pIdx->aAvgEq[] array based on the samples currently
99265 ** stored in pIdx->aSample[].
99269 IndexSample *aSample = pIdx->aSample;
99270 IndexSample *pFinal = &aSample[pIdx->nSample-1];
99273 if( pIdx->nSampleCol>1 ){
99278 nCol = pIdx->nSampleCol-1;
99279 pIdx->aAvgEq[nCol] = 1;
99282 int nSample = pIdx->nSample;
99290 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
99291 nRow = pFinal->anLt[iCol];
99292 nDist100 = (i64)100 * pFinal->anDLt[iCol];
99293 nSample--;
99295 nRow = pIdx->aiRowEst[0];
99296 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
99298 pIdx->nRowEst0 = nRow;
99305 if( i==(pIdx->nSample-1)
99314 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
99317 pIdx->aAvgEq[iCol] = avgEq;
99363 IndexSample *pSample; /* A slot in pIdx->aSample[] */
99365 assert( db->lookaside.bDisable );
99370 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
99388 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
99389 /* Index.nSample is non-zero at this point if data has already been
99391 if( pIdx==0 || pIdx->nSample ) continue;
99393 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
99394 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
99395 nIdxCol = pIdx->nKeyCol;
99397 nIdxCol = pIdx->nColumn;
99400 pIdx->nSampleCol = nIdxCol;
99405 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
99406 if( pIdx->aSample==0 ){
99410 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
99411 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
99413 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
99414 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
99415 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
99417 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
99426 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
99441 nCol = pIdx->nSampleCol;
99447 pSample = &pIdx->aSample[pIdx->nSample];
99448 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
99449 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
99450 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
99458 pSample->n = sqlite3_column_bytes(pStmt, 4);
99459 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
99460 if( pSample->p==0 ){
99464 if( pSample->n ){
99465 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
99467 pIdx->nSample++;
99481 assert( db->lookaside.bDisable );
99518 ** If an OOM error occurs, this function always sets db->mallocFailed.
99527 Schema *pSchema = db->aDb[iDb].pSchema;
99529 assert( iDb>=0 && iDb<db->nDb );
99530 assert( db->aDb[iDb].pBt!=0 );
99534 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
99536 pTab->tabFlags &= ~TF_HasStat1;
99538 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
99540 pIdx->hasStat1 = 0;
99543 pIdx->aSample = 0;
99549 sInfo.zDatabase = db->aDb[iDb].zDbSName;
99563 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
99565 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
99571 db->lookaside.bDisable++;
99573 db->lookaside.bDisable--;
99575 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
99577 sqlite3_free(pIdx->aiRowEst);
99578 pIdx->aiRowEst = 0;
99631 if( pExpr->op!=TK_ID ){
99634 pExpr->op = TK_STRING;
99641 ** An SQL user-function registered to do the work of an ATTACH statement. The
99682 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
99683 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
99684 db->aLimit[SQLITE_LIMIT_ATTACHED]
99688 if( !db->autoCommit ){
99692 for(i=0; i<db->nDb; i++){
99693 char *z = db->aDb[i].zDbSName;
99701 /* Allocate the new entry in the db->aDb[] array and initialize the schema
99704 if( db->aDb==db->aDbStatic ){
99705 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
99707 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
99709 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
99712 db->aDb = aNew;
99713 pNew = &db->aDb[db->nDb];
99720 flags = db->openFlags;
99721 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
99724 sqlite3_result_error(context, zErr, -1);
99730 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
99732 db->nDb++;
99733 db->skipBtreeMutex = 0;
99739 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
99740 if( !pNew->pSchema ){
99742 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
99747 sqlite3BtreeEnter(pNew->pBt);
99748 pPager = sqlite3BtreePager(pNew->pBt);
99749 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
99750 sqlite3BtreeSecureDelete(pNew->pBt,
99751 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
99753 sqlite3BtreeSetPagerFlags(pNew->pBt,
99754 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
99756 sqlite3BtreeLeave(pNew->pBt);
99758 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
99759 pNew->zDbSName = sqlite3DbStrDup(db, zName);
99760 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
99783 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
99789 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
99790 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
99799 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
99811 if( newAuth<db->auth.authLevel ){
99817 int iDb = db->nDb - 1;
99819 if( db->aDb[iDb].pBt ){
99820 sqlite3BtreeClose(db->aDb[iDb].pBt);
99821 db->aDb[iDb].pBt = 0;
99822 db->aDb[iDb].pSchema = 0;
99825 db->nDb = iDb;
99841 sqlite3_result_error(context, zErrDyn, -1);
99848 ** An SQL user-function registered to do the work of an DETACH statement. The
99869 for(i=0; i<db->nDb; i++){
99870 pDb = &db->aDb[i];
99871 if( pDb->pBt==0 ) continue;
99872 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
99875 if( i>=db->nDb ){
99883 if( !db->autoCommit ){
99888 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
99893 sqlite3BtreeClose(pDb->pBt);
99894 pDb->pBt = 0;
99895 pDb->pSchema = 0;
99900 sqlite3_result_error(context, zErr, -1);
99919 sqlite3* db = pParse->db;
99922 if( pParse->nErr ) goto attach_end;
99937 if( pAuthArg->op==TK_STRING ){
99938 zAuthArg = pAuthArg->u.zToken;
99956 assert( v || db->mallocFailed );
99958 sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
99960 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
99961 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
100028 db = pParse->db;
100029 assert( db->nDb>iDb );
100030 pFix->pParse = pParse;
100031 pFix->zDb = db->aDb[iDb].zDbSName;
100032 pFix->pSchema = db->aDb[iDb].pSchema;
100033 pFix->zType = zType;
100034 pFix->pName = pName;
100035 pFix->bVarOnly = (iDb==1);
100049 ** pParse->zErrMsg and these routines return non-zero. If everything
100061 zDb = pFix->zDb;
100062 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
100063 if( pFix->bVarOnly==0 ){
100064 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
100065 sqlite3ErrorMsg(pFix->pParse,
100067 pFix->zType, pFix->pName, pItem->zDatabase);
100070 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
100071 pItem->zDatabase = 0;
100072 pItem->pSchema = pFix->pSchema;
100075 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
100076 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
100087 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
100090 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
100093 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
100096 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
100099 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
100102 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
100105 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
100108 if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
100111 pSelect = pSelect->pPrior;
100120 if( pExpr->op==TK_VARIABLE ){
100121 if( pFix->pParse->db->init.busy ){
100122 pExpr->op = TK_NULL;
100124 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
100130 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
100132 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
100134 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
100137 pExpr = pExpr->pLeft;
100148 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
100149 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
100163 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
100166 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
100169 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
100172 pStep = pStep->pNext;
100194 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
100241 ** means that the SQL statement will never-run - the sqlite3_exec() call
100257 sqlite3_mutex_enter(db->mutex);
100258 db->xAuth = (sqlite3_xauth)xAuth;
100259 db->pAuthArg = pArg;
100261 sqlite3_mutex_leave(db->mutex);
100266 ** Write an error message into pParse->zErrMsg that explains that the
100267 ** user-supplied authorization function returned an illegal value.
100271 pParse->rc = SQLITE_ERROR;
100289 sqlite3 *db = pParse->db; /* Database handle */
100290 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
100293 if( db->init.busy ) return SQLITE_OK;
100294 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
100296 ,db->auth.zAuthUser
100300 if( db->nDb>2 || iDb!=0 ){
100305 pParse->rc = SQLITE_AUTH;
100327 sqlite3 *db = pParse->db;
100330 int iSrc; /* Index in pTabList->a[] of table being read */
100334 if( db->xAuth==0 ) return;
100335 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
100342 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
100343 if( pExpr->op==TK_TRIGGER ){
100344 pTab = pParse->pTriggerTab;
100347 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
100348 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
100349 pTab = pTabList->a[iSrc].pTab;
100354 iCol = pExpr->iColumn;
100358 assert( iCol<pTab->nCol );
100359 zCol = pTab->aCol[iCol].zName;
100360 }else if( pTab->iPKey>=0 ){
100361 assert( pTab->iPKey<pTab->nCol );
100362 zCol = pTab->aCol[pTab->iPKey].zName;
100366 assert( iDb>=0 && iDb<db->nDb );
100367 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
100368 pExpr->op = TK_NULL;
100385 sqlite3 *db = pParse->db;
100391 if( db->init.busy || IN_DECLARE_VTAB ){
100395 if( db->xAuth==0 ){
100399 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
100400 ** callback are either NULL pointers or zero-terminated strings that
100408 testcase( pParse->zAuthContext==0 );
100410 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
100412 ,db->auth.zAuthUser
100417 pParse->rc = SQLITE_AUTH;
100428 ** popped. Or if pParse==0, this routine is a no-op.
100436 pContext->pParse = pParse;
100437 pContext->zAuthContext = pParse->zAuthContext;
100438 pParse->zAuthContext = zContext;
100446 if( pContext->pParse ){
100447 pContext->pParse->zAuthContext = pContext->zAuthContext;
100448 pContext->pParse = 0;
100495 ** Record the fact that we want to lock a table at run-time.
100518 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
100519 for(i=0; i<pToplevel->nTableLock; i++){
100520 p = &pToplevel->aTableLock[i];
100521 if( p->iDb==iDb && p->iTab==iTab ){
100522 p->isWriteLock = (p->isWriteLock || isWriteLock);
100527 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
100528 pToplevel->aTableLock =
100529 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
100530 if( pToplevel->aTableLock ){
100531 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
100532 p->iDb = iDb;
100533 p->iTab = iTab;
100534 p->isWriteLock = isWriteLock;
100535 p->zLockName = zName;
100537 pToplevel->nTableLock = 0;
100538 sqlite3OomFault(pToplevel->db);
100553 for(i=0; i<pParse->nTableLock; i++){
100554 TableLock *p = &pParse->aTableLock[i];
100555 int p1 = p->iDb;
100556 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
100557 p->zLockName, P4_STATIC);
100565 ** Return TRUE if the given yDbMask object is empty - if it contains no
100591 assert( pParse->pToplevel==0 );
100592 db = pParse->db;
100593 if( pParse->nested ) return;
100594 if( db->mallocFailed || pParse->nErr ){
100595 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
100603 assert( !pParse->isMultiWrite
100604 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
100609 if( pParse->nTableLock>0 && db->init.busy==0 ){
100611 if( db->auth.authLevel<UAUTH_User ){
100613 pParse->rc = SQLITE_AUTH_USER;
100625 if( db->mallocFailed==0
100626 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
100629 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
100631 for(iDb=0; iDb<db->nDb; iDb++){
100633 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
100635 pSchema = db->aDb[iDb].pSchema;
100639 DbMaskTest(pParse->writeMask,iDb), /* P2 */
100640 pSchema->schema_cookie, /* P3 */
100641 pSchema->iGeneration /* P4 */
100643 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
100645 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
100648 for(i=0; i<pParse->nVtabLock; i++){
100649 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
100652 pParse->nVtabLock = 0;
100656 ** obtain the required table-locks. This is a no-op unless the
100657 ** shared-cache feature is enabled.
100666 if( pParse->pConstExpr ){
100667 ExprList *pEL = pParse->pConstExpr;
100668 pParse->okConstFactor = 0;
100669 for(i=0; i<pEL->nExpr; i++){
100670 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
100682 if( v && pParse->nErr==0 && !db->mallocFailed ){
100683 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
100686 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
100688 pParse->rc = SQLITE_DONE;
100690 pParse->rc = SQLITE_ERROR;
100710 sqlite3 *db = pParse->db;
100713 if( pParse->nErr ) return;
100714 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
100721 pParse->nested++;
100728 pParse->nested--;
100742 ** Locate the in-memory structure that describes a particular database
100762 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
100767 for(i=OMIT_TEMPDB; i<db->nDb; i++){
100769 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
100771 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
100778 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
100785 ** Locate the in-memory structure that describes a particular database
100788 ** error message in pParse->zErrMsg.
100791 ** routine leaves an error message in pParse->zErrMsg where
100808 p = sqlite3FindTable(pParse->db, zName, zDbase);
100812 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
100816 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
100818 pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
100821 return pMod->pEpoTab;
100831 pParse->checkSchema = 1;
100843 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
100844 ** non-NULL if it is part of a view or trigger program definition. See
100853 assert( p->pSchema==0 || p->zDatabase==0 );
100854 if( p->pSchema ){
100855 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
100856 zDb = pParse->db->aDb[iDb].zDbSName;
100858 zDb = p->zDatabase;
100860 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
100864 ** Locate the in-memory structure that describes
100880 for(i=OMIT_TEMPDB; i<db->nDb; i++){
100882 Schema *pSchema = db->aDb[j].pSchema;
100884 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
100886 p = sqlite3HashFind(&pSchema->idxHash, zName);
100899 sqlite3ExprDelete(db, p->pPartIdxWhere);
100900 sqlite3ExprListDelete(db, p->aColExpr);
100901 sqlite3DbFree(db, p->zColAff);
100902 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
100904 sqlite3_free(p->aiRowEst);
100920 pHash = &db->aDb[iDb].pSchema->idxHash;
100923 if( pIndex->pTable->pIndex==pIndex ){
100924 pIndex->pTable->pIndex = pIndex->pNext;
100929 p = pIndex->pTable->pIndex;
100930 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
100931 if( ALWAYS(p && p->pNext==pIndex) ){
100932 p->pNext = pIndex->pNext;
100937 db->flags |= SQLITE_InternChanges;
100941 ** Look through the list of open database files in db->aDb[] and if
100943 ** db->aDb[] structure to a smaller size, if possible.
100950 for(i=j=2; i<db->nDb; i++){
100951 struct Db *pDb = &db->aDb[i];
100952 if( pDb->pBt==0 ){
100953 sqlite3DbFree(db, pDb->zDbSName);
100954 pDb->zDbSName = 0;
100958 db->aDb[j] = db->aDb[i];
100962 db->nDb = j;
100963 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
100964 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
100965 sqlite3DbFree(db, db->aDb);
100966 db->aDb = db->aDbStatic;
100976 assert( iDb<db->nDb );
100979 pDb = &db->aDb[iDb];
100981 assert( pDb->pSchema!=0 );
100982 sqlite3SchemaClear(pDb->pSchema);
100989 pDb = &db->aDb[1];
100990 assert( pDb->pSchema!=0 );
100991 sqlite3SchemaClear(pDb->pSchema);
101003 for(i=0; i<db->nDb; i++){
101004 Db *pDb = &db->aDb[i];
101005 if( pDb->pSchema ){
101006 sqlite3SchemaClear(pDb->pSchema);
101009 db->flags &= ~SQLITE_InternChanges;
101019 db->flags &= ~SQLITE_InternChanges;
101030 if( (pCol = pTable->aCol)!=0 ){
101031 for(i=0; i<pTable->nCol; i++, pCol++){
101032 sqlite3DbFree(db, pCol->zName);
101033 sqlite3ExprDelete(db, pCol->pDflt);
101034 sqlite3DbFree(db, pCol->zColl);
101036 sqlite3DbFree(db, pTable->aCol);
101052 ** db parameter can be used with db->pnBytesFreed to measure the memory
101062 TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
101063 db->lookaside.nOut : 0 );
101066 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
101067 pNext = pIndex->pNext;
101068 assert( pIndex->pSchema==pTable->pSchema
101069 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
101070 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
101071 char *zName = pIndex->zName;
101073 &pIndex->pSchema->idxHash, zName, 0
101075 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
101087 sqlite3DbFree(db, pTable->zName);
101088 sqlite3DbFree(db, pTable->zColAff);
101089 sqlite3SelectDelete(db, pTable->pSelect);
101090 sqlite3ExprListDelete(db, pTable->pCheck);
101097 assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
101102 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
101116 assert( iDb>=0 && iDb<db->nDb );
101119 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
101120 pDb = &db->aDb[iDb];
101121 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
101123 db->flags |= SQLITE_InternChanges;
101142 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
101157 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
101158 if( p->nTab==0 ){
101159 p->nTab = 1;
101164 ** Parameter zName points to a nul-terminated buffer containing the name
101166 ** function returns the index of the named database in db->aDb[], or
101167 ** -1 if the named db cannot be found.
101170 int i = -1; /* Database number */
101173 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
101174 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
101186 ** index of the named database in db->aDb[], or -1 if the named db
101221 sqlite3 *db = pParse->db;
101224 if( pName2->n>0 ){
101225 if( db->init.busy ) {
101227 return -1;
101233 return -1;
101236 assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0);
101237 iDb = db->init.iDb;
101244 ** This routine is used to check if the UTF-8 string zName is a legal
101251 if( !pParse->db->init.busy && pParse->nested==0
101252 && (pParse->db->flags & SQLITE_WriteSchema)==0
101265 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
101271 ** column iCol. Return -1 if not found.
101275 for(i=0; i<pIdx->nColumn; i++){
101276 if( iCol==pIdx->aiColumn[i] ) return i;
101278 return -1;
101291 ** The new table record is initialized and put in pParse->pNewTable.
101308 sqlite3 *db = pParse->db;
101313 if( db->init.busy && db->init.newTnum==1 ){
101315 iDb = db->init.iDb;
101322 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
101331 pParse->sNameToken = *pName;
101336 if( db->init.iDb==1 ) isTemp = 1;
101347 char *zDb = db->aDb[iDb].zDbSName;
101366 char *zDb = db->aDb[iDb].zDbSName;
101375 assert( !db->init.busy || CORRUPT_DB );
101388 assert( db->mallocFailed );
101389 pParse->rc = SQLITE_NOMEM_BKPT;
101390 pParse->nErr++;
101393 pTable->zName = zName;
101394 pTable->iPKey = -1;
101395 pTable->pSchema = db->aDb[iDb].pSchema;
101396 pTable->nTabRef = 1;
101398 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
101400 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
101402 assert( pParse->pNewTable==0 );
101403 pParse->pNewTable = pTable;
101410 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
101412 pTable->pSchema->pSeqTab = pTable;
101424 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
101428 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
101441 reg1 = pParse->regRowid = ++pParse->nMem;
101442 reg2 = pParse->regRoot = ++pParse->nMem;
101443 reg3 = ++pParse->nMem;
101447 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
101453 /* This just creates a place-holder record in the sqlite_master table.
101457 ** The rowid for the new entry is left in register pParse->regRowid.
101458 ** The root page number of the new table is left in reg pParse->regRoot.
101468 pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
101478 /* Normal (non-error) return. */
101492 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
101493 pCol->colFlags |= COLFLAG_HIDDEN;
101494 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
101495 pTab->tabFlags |= TF_OOOHidden;
101515 sqlite3 *db = pParse->db;
101516 if( (p = pParse->pNewTable)==0 ) return;
101518 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
101519 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
101523 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
101525 memcpy(z, pName->z, pName->n);
101526 z[pName->n] = 0;
101528 for(i=0; i<p->nCol; i++){
101529 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
101535 if( (p->nCol & 0x7)==0 ){
101537 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
101542 p->aCol = aNew;
101544 pCol = &p->aCol[p->nCol];
101545 memset(pCol, 0, sizeof(p->aCol[0]));
101546 pCol->zName = z;
101549 if( pType->n==0 ){
101552 pCol->affinity = SQLITE_AFF_BLOB;
101553 pCol->szEst = 1;
101556 memcpy(zType, pType->z, pType->n);
101557 zType[pType->n] = 0;
101559 pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst);
101560 pCol->colFlags |= COLFLAG_HASTYPE;
101562 p->nCol++;
101563 pParse->constraintName.n = 0;
101574 p = pParse->pNewTable;
101575 if( p==0 || NEVER(p->nCol<1) ) return;
101576 p->aCol[p->nCol-1].notNull = (u8)onError;
101577 p->tabFlags |= TF_HasNotNull;
101584 ** This routine does a case-independent search of zType for the
101592 ** --------------------------------
101654 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
101660 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
101680 sqlite3 *db = pParse->db;
101681 p = pParse->pNewTable;
101683 pCol = &(p->aCol[p->nCol-1]);
101684 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
101686 pCol->zName);
101693 sqlite3ExprDelete(db, pCol->pDflt);
101696 x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
101697 (int)(pSpan->zEnd - pSpan->zStart));
101698 x.pLeft = pSpan->pExpr;
101700 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
101704 sqlite3ExprDelete(db, pSpan->pExpr);
101724 if( p->op==TK_STRING ){
101725 p->op = TK_ID;
101726 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
101727 p->pLeft->op = TK_ID;
101743 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
101756 Table *pTab = pParse->pNewTable;
101758 int iCol = -1, i;
101761 if( pTab->tabFlags & TF_HasPrimaryKey ){
101763 "table \"%s\" has more than one primary key", pTab->zName);
101766 pTab->tabFlags |= TF_HasPrimaryKey;
101768 iCol = pTab->nCol - 1;
101769 pCol = &pTab->aCol[iCol];
101770 pCol->colFlags |= COLFLAG_PRIMKEY;
101773 nTerm = pList->nExpr;
101775 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
101778 if( pCExpr->op==TK_ID ){
101779 const char *zCName = pCExpr->u.zToken;
101780 for(iCol=0; iCol<pTab->nCol; iCol++){
101781 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
101782 pCol = &pTab->aCol[iCol];
101783 pCol->colFlags |= COLFLAG_PRIMKEY;
101795 pTab->iPKey = iCol;
101796 pTab->keyConf = (u8)onError;
101798 pTab->tabFlags |= autoInc*TF_Autoincrement;
101799 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
101812 sqlite3ExprListDelete(pParse->db, pList);
101824 Table *pTab = pParse->pNewTable;
101825 sqlite3 *db = pParse->db;
101827 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
101829 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
101830 if( pParse->constraintName.n ){
101831 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
101836 sqlite3ExprDelete(pParse->db, pCheckExpr);
101850 if( (p = pParse->pNewTable)==0 ) return;
101851 i = p->nCol-1;
101852 db = pParse->db;
101858 sqlite3DbFree(db, p->aCol[i].zColl);
101859 p->aCol[i].zColl = zColl;
101865 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
101866 assert( pIdx->nKeyCol==1 );
101867 if( pIdx->aiColumn[0]==i ){
101868 pIdx->azColl[0] = p->aCol[i].zColl;
101897 sqlite3 *db = pParse->db;
101899 u8 initbusy = db->init.busy;
101903 if( !initbusy && (!pColl || !pColl->xCmp) ){
101921 ** This plan is not completely bullet-proof. It is possible for
101927 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
101928 ** the schema-version whenever the schema changes.
101931 sqlite3 *db = pParse->db;
101932 Vdbe *v = pParse->pVdbe;
101935 db->aDb[iDb].pSchema->schema_cookie+1);
101958 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
101962 ** If the string zSignedIdent consists entirely of alpha-numeric
101965 ** it is quoted using double-quotes.
102001 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
102002 n += identLength(pCol->zName) + 5;
102004 n += identLength(p->zName);
102014 n += 35 + 6*p->nCol;
102022 identPut(zStmt, &k, p->zName);
102024 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
102035 sqlite3_snprintf(n-k, &zStmt[k], zSep);
102038 identPut(zStmt, &k, pCol->zName);
102039 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
102040 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
102041 testcase( pCol->affinity==SQLITE_AFF_BLOB );
102042 testcase( pCol->affinity==SQLITE_AFF_TEXT );
102043 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
102044 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
102045 testcase( pCol->affinity==SQLITE_AFF_REAL );
102047 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
102049 assert( pCol->affinity==SQLITE_AFF_BLOB
102050 || pCol->affinity==sqlite3AffinityType(zType, 0) );
102055 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
102066 if( pIdx->nColumn>=N ) return SQLITE_OK;
102067 assert( pIdx->isResized==0 );
102071 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
102072 pIdx->azColl = (const char**)zExtra;
102074 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
102075 pIdx->aiColumn = (i16*)zExtra;
102077 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
102078 pIdx->aSortOrder = (u8*)zExtra;
102079 pIdx->nColumn = N;
102080 pIdx->isResized = 1;
102091 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
102092 wTable += pTabCol->szEst;
102094 if( pTab->iPKey<0 ) wTable++;
102095 pTab->szTabRow = sqlite3LogEst(wTable*4);
102104 const Column *aCol = pIdx->pTable->aCol;
102105 for(i=0; i<pIdx->nColumn; i++){
102106 i16 x = pIdx->aiColumn[i];
102107 assert( x<pIdx->pTable->nCol );
102108 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
102110 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
102116 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
102136 ** (5) Add all table columns to the PRIMARY KEY Index object
102150 sqlite3 *db = pParse->db;
102151 Vdbe *v = pParse->pVdbe;
102155 if( !db->init.imposterTable ){
102156 for(i=0; i<pTab->nCol; i++){
102157 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
102158 pTab->aCol[i].notNull = OE_Abort;
102163 /* The remaining transformations only apply to b-tree tables, not to
102168 ** root-page for the table into an OP_CreateIndex opcode. The index
102171 if( pParse->addrCrTab ){
102173 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex);
102179 if( pTab->iPKey>=0 ){
102182 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
102186 pList->a[0].sortOrder = pParse->iPkSortOrder;
102187 assert( pParse->pNewTable==pTab );
102188 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
102190 if( db->mallocFailed ) return;
102192 pTab->iPKey = -1;
102201 for(i=j=1; i<pPk->nKeyCol; i++){
102202 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
102203 pPk->nColumn--;
102205 pPk->aiColumn[j++] = pPk->aiColumn[i];
102208 pPk->nKeyCol = j;
102211 pPk->isCovering = 1;
102212 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
102213 nPk = pPk->nKeyCol;
102219 if( v && pPk->tnum>0 ){
102220 assert( db->init.busy==0 );
102221 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
102225 pPk->tnum = pTab->tnum;
102227 /* Update the in-memory representation of all UNIQUE indices by converting
102230 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102234 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
102238 pIdx->nColumn = pIdx->nKeyCol;
102241 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
102242 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
102243 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
102244 pIdx->aiColumn[j] = pPk->aiColumn[i];
102245 pIdx->azColl[j] = pPk->azColl[i];
102249 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
102250 assert( pIdx->nColumn>=j );
102255 if( nPk<pTab->nCol ){
102256 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
102257 for(i=0, j=nPk; i<pTab->nCol; i++){
102258 if( !hasColumn(pPk->aiColumn, j, i) ){
102259 assert( j<pPk->nColumn );
102260 pPk->aiColumn[j] = i;
102261 pPk->azColl[j] = sqlite3StrBINARY;
102265 assert( pPk->nColumn==j );
102266 assert( pTab->nCol==j );
102268 pPk->nColumn = pTab->nCol;
102281 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
102300 sqlite3 *db = pParse->db; /* The database connection */
102307 assert( !db->mallocFailed );
102308 p = pParse->pNewTable;
102311 assert( !db->init.busy || !pSelect );
102313 /* If the db->init.busy is 1 it means we are reading the SQL off the
102316 ** for the table from the db->init.newTnum field. (The page number
102320 ** table itself. So mark it read-only.
102322 if( db->init.busy ){
102323 p->tnum = db->init.newTnum;
102324 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
102329 if( (p->tabFlags & TF_Autoincrement) ){
102334 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
102335 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
102337 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
102342 iDb = sqlite3SchemaToIndex(db, p->pSchema);
102347 if( p->pCheck ){
102348 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
102354 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
102364 if( !db->init.busy ){
102379 if( p->pSelect==0 ){
102392 ** statement to populate the new table. The root-page number for the
102393 ** new table is in register pParse->regRoot.
102399 ** A shared-cache write-lock is not required to write to the new table,
102400 ** as a schema-lock must have already been obtained to create it. Since
102401 ** a schema-lock excludes all other database users, the write-lock would
102406 int regYield; /* Register holding co-routine entry-point */
102407 int addrTop; /* Top of the co-routine */
102413 regYield = ++pParse->nMem;
102414 regRec = ++pParse->nMem;
102415 regRowid = ++pParse->nMem;
102416 assert(pParse->nTab==1);
102418 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
102420 pParse->nTab = 2;
102426 sqlite3VdbeJumpHere(v, addrTop - 1);
102427 if( pParse->nErr ) return;
102430 assert( p->aCol==0 );
102431 p->nCol = pSelTab->nCol;
102432 p->aCol = pSelTab->aCol;
102433 pSelTab->nCol = 0;
102434 pSelTab->aCol = 0;
102451 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
102452 n = (int)(pEnd2->z - pParse->sNameToken.z);
102453 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
102455 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
102467 db->aDb[iDb].zDbSName, MASTER_NAME,
102469 p->zName,
102470 p->zName,
102471 pParse->regRoot,
102473 pParse->regRowid
102482 if( (p->tabFlags & TF_Autoincrement)!=0 ){
102483 Db *pDb = &db->aDb[iDb];
102485 if( pDb->pSchema->pSeqTab==0 ){
102488 pDb->zDbSName
102496 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
102500 /* Add the table to the in-memory representation of the database.
102502 if( db->init.busy ){
102504 Schema *pSchema = p->pSchema;
102506 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
102512 pParse->pNewTable = 0;
102513 db->flags |= SQLITE_InternChanges;
102516 if( !p->pSelect ){
102517 const char *zName = (const char *)pParse->sNameToken.z;
102520 if( pCons->z==0 ){
102523 nName = (int)((const char *)pCons->z - zName);
102524 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
102551 sqlite3 *db = pParse->db;
102553 if( pParse->nVar>0 ){
102558 p = pParse->pNewTable;
102559 if( p==0 || pParse->nErr ) goto create_view_fail;
102561 iDb = sqlite3SchemaToIndex(db, p->pSchema);
102567 ** allocated rather than point to the input string - which means that
102570 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
102571 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
102572 if( db->mallocFailed ) goto create_view_fail;
102577 sEnd = pParse->sLastToken;
102583 n = (int)(sEnd.z - pBegin->z);
102585 z = pBegin->z;
102586 while( sqlite3Isspace(z[n-1]) ){ n--; }
102587 sEnd.z = &z[n-1];
102604 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
102611 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
102629 if( pTable->nCol>0 ) return 0;
102646 if( pTable->nCol<0 ){
102647 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
102650 assert( pTable->nCol>=0 );
102659 assert( pTable->pSelect );
102660 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
102662 n = pParse->nTab;
102663 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
102664 pTable->nCol = -1;
102665 db->lookaside.bDisable++;
102667 xAuth = db->xAuth;
102668 db->xAuth = 0;
102670 db->xAuth = xAuth;
102674 pParse->nTab = n;
102675 if( pTable->pCheck ){
102678 ** arglist which is stored in pTable->pCheck. The pCheck field
102682 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
102683 &pTable->nCol, &pTable->aCol);
102684 if( db->mallocFailed==0
102685 && pParse->nErr==0
102686 && pTable->nCol==pSel->pEList->nExpr
102694 assert( pTable->aCol==0 );
102695 pTable->nCol = pSelTab->nCol;
102696 pTable->aCol = pSelTab->aCol;
102697 pSelTab->nCol = 0;
102698 pSelTab->aCol = 0;
102699 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
102701 pTable->nCol = 0;
102706 db->lookaside.bDisable--;
102710 pTable->pSchema->schemaFlags |= DB_UnresetViews;
102724 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
102726 if( pTab->pSelect ){
102728 pTab->aCol = 0;
102729 pTab->nCol = 0;
102741 ** root-page of a table or index in database iDb has changed from iFrom
102762 pDb = &db->aDb[iDb];
102763 pHash = &pDb->pSchema->tblHash;
102766 if( pTab->tnum==iFrom ){
102767 pTab->tnum = iTo;
102770 pHash = &pDb->pSchema->idxHash;
102773 if( pIdx->tnum==iFrom ){
102774 pIdx->tnum = iTo;
102781 ** Write code to erase the table with root-page iTable from database iDb.
102783 ** if a root-page of another table is moved by the btree-layer whilst
102784 ** erasing iTable (this can happen with an auto-vacuum database).
102794 ** is non-zero, then it is the root page number of a table moved to
102804 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
102812 ** in case a root-page belonging to another table is moved by the btree layer
102813 ** is also added (this can happen with an auto-vacuum database).
102818 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102819 destroyRootPage(pParse, pTab->tnum, iDb);
102820 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102821 destroyRootPage(pParse, pIdx->tnum, iDb);
102824 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
102826 ** table and index root-pages in order, starting with the numerically
102827 ** largest root-page number. This guarantees that none of the root-pages
102833 ** OP_Destroy 5 0
102835 ** and root page 5 happened to be the largest root-page number in the
102836 ** database, then root page 5 would be moved to page 4 by the
102837 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
102838 ** a free-list page.
102840 int iTab = pTab->tnum;
102850 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102851 int iIdx = pIdx->tnum;
102852 assert( pIdx->pSchema==pTab->pSchema );
102860 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102861 assert( iDb>=0 && iDb<pParse->db->nDb );
102880 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
102884 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
102898 sqlite3 *db = pParse->db;
102900 Db *pDb = &db->aDb[iDb];
102918 assert( pTrigger->pSchema==pTab->pSchema ||
102919 pTrigger->pSchema==db->aDb[1].pSchema );
102921 pTrigger = pTrigger->pNext;
102928 ** move as a result of the drop (can happen in auto-vacuum mode).
102930 if( pTab->tabFlags & TF_Autoincrement ){
102933 pDb->zDbSName, pTab->zName
102947 pDb->zDbSName, MASTER_NAME, pTab->zName);
102956 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
102958 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
102970 sqlite3 *db = pParse->db;
102973 if( db->mallocFailed ){
102976 assert( pParse->nErr==0 );
102977 assert( pName->nSrc==1 );
102979 if( noErr ) db->suppressErr++;
102981 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
102982 if( noErr ) db->suppressErr--;
102985 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
102988 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102989 assert( iDb>=0 && iDb<db->nDb );
103001 const char *zDb = db->aDb[iDb].zDbSName;
103015 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
103024 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
103027 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
103032 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
103033 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
103034 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
103042 if( isView && pTab->pSelect==0 ){
103043 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
103046 if( !isView && pTab->pSelect ){
103047 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
103058 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
103078 ** under construction in the pParse->pNewTable field.
103090 sqlite3 *db = pParse->db;
103094 Table *p = pParse->pNewTable;
103103 int iCol = p->nCol-1;
103105 if( pToCol && pToCol->nExpr!=1 ){
103108 p->aCol[iCol].zName, pTo);
103112 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
103118 nCol = pFromCol->nExpr;
103120 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
103122 for(i=0; i<pToCol->nExpr; i++){
103123 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
103130 pFKey->pFrom = p;
103131 pFKey->pNextFrom = p->pFKey;
103132 z = (char*)&pFKey->aCol[nCol];
103133 pFKey->zTo = z;
103134 memcpy(z, pTo->z, pTo->n);
103135 z[pTo->n] = 0;
103137 z += pTo->n+1;
103138 pFKey->nCol = nCol;
103140 pFKey->aCol[0].iFrom = p->nCol-1;
103144 for(j=0; j<p->nCol; j++){
103145 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
103146 pFKey->aCol[i].iFrom = j;
103150 if( j>=p->nCol ){
103153 pFromCol->a[i].zName);
103160 int n = sqlite3Strlen30(pToCol->a[i].zName);
103161 pFKey->aCol[i].zCol = z;
103162 memcpy(z, pToCol->a[i].zName, n);
103167 pFKey->isDeferred = 0;
103168 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
103169 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
103171 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
103172 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
103173 pFKey->zTo, (void *)pFKey
103180 assert( pNextTo->pPrevTo==0 );
103181 pFKey->pNextTo = pNextTo;
103182 pNextTo->pPrevTo = pFKey;
103187 p->pFKey = pFKey;
103208 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
103209 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
103210 pFKey->isDeferred = (u8)isDeferred;
103223 ** the root page number of the index is taken from pIndex->tnum.
103226 Table *pTab = pIndex->pTable; /* The table that is indexed */
103227 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
103228 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
103237 sqlite3 *db = pParse->db; /* The database connection */
103238 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
103241 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
103242 db->aDb[iDb].zDbSName ) ){
103247 /* Require a write-lock on the table to perform this operation */
103248 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
103255 tnum = pIndex->tnum;
103258 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
103261 iSorter = pParse->nTab++;
103262 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
103287 pIndex->nKeyCol); VdbeCoverage(v);
103293 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
103309 ** of 8-byte aligned space after the Index object and return a
103329 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
103330 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
103331 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
103332 p->aSortOrder = (u8*)pExtra;
103333 p->nColumn = nCol;
103334 p->nKeyCol = nCol - 1;
103344 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
103345 ** as the table to be indexed. pParse->pNewTable is a table that is
103349 ** is a primary key or unique-constraint on the most recent column added
103356 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
103372 sqlite3 *db = pParse->db;
103382 if( db->mallocFailed || pParse->nErr>0 ){
103397 /* Use the two-part index name to determine the database
103404 assert( pName && pName->z );
103411 if( !db->init.busy ){
103413 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
103425 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
103426 assert( db->mallocFailed==0 || pTab==0 );
103428 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
103430 "cannot create a TEMP index on non-TEMP table \"%s\"",
103431 pTab->zName);
103438 pTab = pParse->pNewTable;
103440 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103442 pDb = &db->aDb[iDb];
103445 assert( pParse->nErr==0 );
103446 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
103447 && db->init.busy==0
103449 && sqlite3UserAuthTable(pTab->zName)==0
103451 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
103452 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
103456 if( pTab->pSelect ){
103484 assert( pName->z!=0 );
103488 if( !db->init.busy ){
103494 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
103498 assert( !db->init.busy );
103506 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
103507 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
103524 const char *zDb = pDb->zDbSName;
103530 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
103542 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
103546 assert( pList->nExpr==1 );
103555 for(i=0; i<pList->nExpr; i++){
103556 Expr *pExpr = pList->a[i].pExpr;
103558 if( pExpr->op==TK_COLLATE ){
103559 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
103567 nExtraCol = pPk ? pPk->nKeyCol : 1;
103568 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
103570 if( db->mallocFailed ){
103573 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
103574 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
103575 pIndex->zName = zExtra;
103577 memcpy(pIndex->zName, zName, nName+1);
103578 pIndex->pTable = pTab;
103579 pIndex->onError = (u8)onError;
103580 pIndex->uniqNotNull = onError!=OE_None;
103581 pIndex->idxType = idxType;
103582 pIndex->pSchema = db->aDb[iDb].pSchema;
103583 pIndex->nKeyCol = pList->nExpr;
103586 pIndex->pPartIdxWhere = pPIWhere;
103593 if( pDb->pSchema->file_format>=4 ){
103594 sortOrderMask = -1; /* Honor DESC */
103602 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
103608 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
103609 Expr *pCExpr; /* The i-th index expression */
103610 int requestedSortOrder; /* ASC or DESC on the i-th expression */
103613 sqlite3StringToId(pListItem->pExpr);
103614 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
103615 if( pParse->nErr ) goto exit_create_index;
103616 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
103617 if( pCExpr->op!=TK_COLUMN ){
103618 if( pTab==pParse->pNewTable ){
103623 if( pIndex->aColExpr==0 ){
103625 pIndex->aColExpr = pCopy;
103626 if( !db->mallocFailed ){
103628 pListItem = &pCopy->a[i];
103632 pIndex->aiColumn[i] = XN_EXPR;
103633 pIndex->uniqNotNull = 0;
103635 j = pCExpr->iColumn;
103638 j = pTab->iPKey;
103639 }else if( pTab->aCol[j].notNull==0 ){
103640 pIndex->uniqNotNull = 0;
103642 pIndex->aiColumn[i] = (i16)j;
103645 if( pListItem->pExpr->op==TK_COLLATE ){
103647 zColl = pListItem->pExpr->u.zToken;
103653 nExtra -= nColl;
103655 zColl = pTab->aCol[j].zColl;
103658 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
103661 pIndex->azColl[i] = zColl;
103662 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
103663 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
103671 for(j=0; j<pPk->nKeyCol; j++){
103672 int x = pPk->aiColumn[j];
103674 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
103675 pIndex->nColumn--;
103677 pIndex->aiColumn[i] = x;
103678 pIndex->azColl[i] = pPk->azColl[j];
103679 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
103683 assert( i==pIndex->nColumn );
103685 pIndex->aiColumn[i] = XN_ROWID;
103686 pIndex->azColl[i] = sqlite3StrBINARY;
103689 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
103694 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
103695 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
103696 pIndex->isCovering = 1;
103697 for(j=0; j<pTab->nCol; j++){
103698 if( j==pTab->iPKey ) continue;
103700 pIndex->isCovering = 0;
103705 if( pTab==pParse->pNewTable ){
103728 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103731 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
103734 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
103735 for(k=0; k<pIdx->nKeyCol; k++){
103738 assert( pIdx->aiColumn[k]>=0 );
103739 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
103740 z1 = pIdx->azColl[k];
103741 z2 = pIndex->azColl[k];
103744 if( k==pIdx->nKeyCol ){
103745 if( pIdx->onError!=pIndex->onError ){
103753 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
103757 if( pIdx->onError==OE_Default ){
103758 pIdx->onError = pIndex->onError;
103761 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
103768 ** in-memory database structures.
103770 assert( pParse->nErr==0 );
103771 if( db->init.busy ){
103774 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
103775 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
103776 pIndex->zName, pIndex);
103782 db->flags |= SQLITE_InternChanges;
103784 pIndex->tnum = db->init.newTnum;
103804 int iMem = ++pParse->nMem;
103817 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
103824 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
103825 if( pName->z[n-1]==';' ) n--;
103828 onError==OE_None ? "" : " UNIQUE", n, pName->z);
103839 db->aDb[iDb].zDbSName, MASTER_NAME,
103840 pIndex->zName,
103841 pTab->zName,
103848 ** to invalidate all pre-compiled statements.
103854 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
103858 sqlite3VdbeJumpHere(v, pIndex->tnum);
103867 if( db->init.busy || pTblName==0 ){
103868 if( onError!=OE_Replace || pTab->pIndex==0
103869 || pTab->pIndex->onError==OE_Replace){
103870 pIndex->pNext = pTab->pIndex;
103871 pTab->pIndex = pIndex;
103873 Index *pOther = pTab->pIndex;
103874 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
103875 pOther = pOther->pNext;
103877 pIndex->pNext = pOther->pNext;
103878 pOther->pNext = pIndex;
103893 ** Fill the Index.aiRowEst[] array with default information - information
103903 ** aiRowEst[N]<=aiRowEst[N-1]
103913 LogEst *a = pIdx->aiRowLogEst;
103914 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
103918 assert( !pIdx->hasStat1 );
103923 a[0] = pIdx->pTable->nRowLogEst;
103924 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
103927 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
103928 ** 6 and each subsequent value (if any) is 5. */
103930 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
103931 a[i] = 23; assert( 23==sqlite3LogEst(5) );
103935 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
103945 sqlite3 *db = pParse->db;
103948 assert( pParse->nErr==0 ); /* Never called with prior errors */
103949 if( db->mallocFailed ){
103952 assert( pName->nSrc==1 );
103956 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
103961 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
103963 pParse->checkSchema = 1;
103966 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
103971 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
103975 Table *pTab = pIndex->pTable;
103976 const char *zDb = db->aDb[iDb].zDbSName;
103982 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
103994 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
103996 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
103998 destroyRootPage(pParse, pIndex->tnum, iDb);
103999 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
104012 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
104020 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
104032 if( (n & (n-1))==0 ){
104036 *pIdx = -1;
104060 pList->a = sqlite3ArrayAllocate(
104062 pList->a,
104063 sizeof(pList->a[0]),
104064 &pList->nId,
104071 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
104081 for(i=0; i<pList->nId; i++){
104082 sqlite3DbFree(db, pList->a[i].zName);
104084 sqlite3DbFree(db, pList->a);
104089 ** Return the index in pList of the identifier named zId. Return -1
104094 if( pList==0 ) return -1;
104095 for(i=0; i<pList->nId; i++){
104096 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
104098 return -1;
104118 ** db->mallocFailed flag will be set to true.
104123 int nExtra, /* Number of new slots to add to pSrc->a[] */
104124 int iStart /* Index in pSrc->a[] of first new slot */
104132 assert( iStart<=pSrc->nSrc );
104135 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
104137 int nAlloc = pSrc->nSrc*2+nExtra;
104140 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
104142 assert( db->mallocFailed );
104146 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
104147 pSrc->nAlloc = nGot;
104152 for(i=pSrc->nSrc-1; i>=iStart; i--){
104153 pSrc->a[i+nExtra] = pSrc->a[i];
104155 pSrc->nSrc += nExtra;
104158 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
104160 pSrc->a[i].iCursor = -1;
104214 pList->nAlloc = 1;
104215 pList->nSrc = 1;
104216 memset(&pList->a[0], 0, sizeof(pList->a[0]));
104217 pList->a[0].iCursor = -1;
104219 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
104221 if( db->mallocFailed ){
104225 pItem = &pList->a[pList->nSrc-1];
104226 if( pDatabase && pDatabase->z==0 ){
104230 pItem->zName = sqlite3NameFromToken(db, pDatabase);
104231 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
104233 pItem->zName = sqlite3NameFromToken(db, pTable);
104234 pItem->zDatabase = 0;
104245 assert(pList || pParse->db->mallocFailed );
104247 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
104248 if( pItem->iCursor>=0 ) break;
104249 pItem->iCursor = pParse->nTab++;
104250 if( pItem->pSelect ){
104251 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
104264 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
104265 sqlite3DbFree(db, pItem->zDatabase);
104266 sqlite3DbFree(db, pItem->zName);
104267 sqlite3DbFree(db, pItem->zAlias);
104268 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
104269 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
104270 sqlite3DeleteTable(db, pItem->pTab);
104271 sqlite3SelectDelete(db, pItem->pSelect);
104272 sqlite3ExprDelete(db, pItem->pOn);
104273 sqlite3IdListDelete(db, pItem->pUsing);
104284 ** pDatabase is NULL if the database name qualifier is missing - the
104299 Token *pAlias, /* The right-hand side of the AS subexpression */
104305 sqlite3 *db = pParse->db;
104313 if( p==0 || NEVER(p->nSrc==0) ){
104316 pItem = &p->a[p->nSrc-1];
104318 if( pAlias->n ){
104319 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
104321 pItem->pSelect = pSubquery;
104322 pItem->pOn = pOn;
104323 pItem->pUsing = pUsing;
104336 ** element of the source-list passed as the second argument.
104340 if( p && ALWAYS(p->nSrc>0) ){
104341 struct SrcList_item *pItem = &p->a[p->nSrc-1];
104342 assert( pItem->fg.notIndexed==0 );
104343 assert( pItem->fg.isIndexedBy==0 );
104344 assert( pItem->fg.isTabFunc==0 );
104345 if( pIndexedBy->n==1 && !pIndexedBy->z ){
104348 pItem->fg.notIndexed = 1;
104350 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
104351 pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0);
104358 ** table-valued-function.
104362 struct SrcList_item *pItem = &p->a[p->nSrc-1];
104363 assert( pItem->fg.notIndexed==0 );
104364 assert( pItem->fg.isIndexedBy==0 );
104365 assert( pItem->fg.isTabFunc==0 );
104366 pItem->u1.pFuncArg = pList;
104367 pItem->fg.isTabFunc = 1;
104369 sqlite3ExprListDelete(pParse->db, pList);
104385 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
104391 for(i=p->nSrc-1; i>0; i--){
104392 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
104394 p->a[0].fg.jointype = 0;
104407 db = pParse->db;
104415 for(i=0; i<db->nDb; i++){
104433 assert( pParse->db!=0 );
104451 char *zName = sqlite3NameFromToken(pParse->db, pName);
104459 sqlite3DbFree(pParse->db, zName);
104471 sqlite3 *db = pParse->db;
104472 if( db->aDb[1].pBt==0 && !pParse->explain ){
104482 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
104486 pParse->rc = rc;
104489 db->aDb[1].pBt = pBt;
104490 assert( db->aDb[1].pSchema );
104491 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
104502 ** will occur at the end of the top-level VDBE and will be generated
104508 assert( iDb>=0 && iDb<pParse->db->nDb );
104509 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
104511 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
104512 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
104513 DbMaskSet(pToplevel->cookieMask, iDb);
104525 sqlite3 *db = pParse->db;
104527 for(i=0; i<db->nDb; i++){
104528 Db *pDb = &db->aDb[i];
104529 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
104551 DbMaskSet(pToplevel->writeMask, iDb);
104552 pToplevel->isMultiWrite |= setStatement;
104564 pToplevel->isMultiWrite = 1;
104585 pToplevel->mayAbort = 1;
104621 Table *pTab = pIdx->pTable;
104623 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
104624 if( pIdx->aColExpr ){
104625 sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
104627 for(j=0; j<pIdx->nKeyCol; j++){
104629 assert( pIdx->aiColumn[j]>=0 );
104630 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
104632 sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
104646 ** Code an OP_Halt due to non-unique rowid.
104651 Table *pTab /* The table with the non-unique rowid */
104655 if( pTab->iPKey>=0 ){
104656 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
104657 pTab->aCol[pTab->iPKey].zName);
104660 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
104675 for(i=0; i<pIndex->nColumn; i++){
104676 const char *z = pIndex->azColl[i];
104677 assert( z!=0 || pIndex->aiColumn[i]<0 );
104678 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
104694 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
104696 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104698 sqlite3RefillIndex(pParse, pIndex, -1);
104713 sqlite3 *db = pParse->db; /* The database connection */
104718 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
104720 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
104731 ** REINDEX -- 1
104732 ** REINDEX <collation> -- 2
104733 ** REINDEX ?<database>.?<tablename> -- 3
104734 ** REINDEX ?<database>.?<indexname> -- 4
104749 sqlite3 *db = pParse->db; /* The database connection */
104761 }else if( NEVER(pName2==0) || pName2->z==0 ){
104763 assert( pName1->z );
104764 zColl = sqlite3NameFromToken(pParse->db, pName1);
104778 zDb = db->aDb[iDb].zDbSName;
104789 sqlite3RefillIndex(pParse, pIndex, -1);
104804 int nCol = pIdx->nColumn;
104805 int nKey = pIdx->nKeyCol;
104807 if( pParse->nErr ) return 0;
104808 if( pIdx->uniqNotNull ){
104809 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
104811 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
104816 const char *zColl = pIdx->azColl[i];
104817 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
104819 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
104821 if( pParse->nErr ){
104837 Token *pName, /* Name of the common-table */
104841 sqlite3 *db = pParse->db;
104847 zName = sqlite3NameFromToken(pParse->db, pName);
104850 for(i=0; i<pWith->nCte; i++){
104851 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
104858 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
104863 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
104865 if( db->mallocFailed ){
104871 pNew->a[pNew->nCte].pSelect = pQuery;
104872 pNew->a[pNew->nCte].pCols = pArglist;
104873 pNew->a[pNew->nCte].zName = zName;
104874 pNew->a[pNew->nCte].zCteErr = 0;
104875 pNew->nCte++;
104887 for(i=0; i<pWith->nCte; i++){
104888 struct Cte *pCte = &pWith->a[i];
104889 sqlite3ExprListDelete(db, pCte->pCols);
104890 sqlite3SelectDelete(db, pCte->pSelect);
104891 sqlite3DbFree(db, pCte->zName);
104923 assert( !db->xCollNeeded || !db->xCollNeeded16 );
104924 if( db->xCollNeeded ){
104927 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
104931 if( db->xCollNeeded16 ){
104934 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
104937 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
104948 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
104953 char *z = pColl->zName;
104958 if( pColl2->xCmp!=0 ){
104960 pColl->xDel = 0; /* Do not copy the destructor */
104988 sqlite3 *db = pParse->db;
104994 if( !p || !p->xCmp ){
105001 if( p && !p->xCmp && synthCollSeq(db, p) ){
105004 assert( !p || p->xCmp );
105023 if( pColl && pColl->xCmp==0 ){
105024 const char *zName = pColl->zName;
105025 sqlite3 *db = pParse->db;
105044 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
105056 pColl = sqlite3HashFind(&db->aCollSeq, zName);
105070 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
105088 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
105112 pColl = db->pDfltColl;
105116 if( pColl ) pColl += enc-1;
105126 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
105127 ** is also -1. In other words, we are searching for a function that
105130 ** If nArg is -2 that means that we are searching for any function
105140 ** 4: UTF8/16 conversion required - argument count matches exactly
105141 ** 5: UTF16 byte order conversion required - argument count matches exactly
105144 ** If nArg==(-2) then any function with a non-null xSFunc is
105146 ** a non-match.
105151 int nArg, /* Desired number of arguments. (-1)==any */
105156 /* nArg of -2 is a special case */
105157 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
105160 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
105164 if( p->nArg==nArg ){
105171 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
105173 }else if( (enc & p->funcFlags & 2)!=0 ){
105189 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
105190 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
105213 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
105214 aDef[i].pNext = pOther->pNext;
105215 pOther->pNext = &aDef[i];
105228 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
105236 ** If nArg is -2, then the first valid function found is returned. A
105237 ** function is valid if xSFunc is non-zero. The nArg==(-2)
105239 ** of arguments. If nArg is -2, then createFlag must be 0.
105247 const char *zName, /* Name of the function. zero-terminated */
105248 int nArg, /* Number of arguments. -1 means any number */
105258 assert( nArg>=(-2) );
105259 assert( nArg>=(-1) || createFlag==0 );
105262 /* First search for a match amongst the application-defined functions.
105264 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
105271 p = p->pNext;
105274 /* If no match is found, search the built-in functions.
105276 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
105277 ** functions even if a prior app-defined function was found. And give
105278 ** priority to built-in functions.
105283 ** new function. But the FuncDefs for built-in functions are read-only.
105284 ** So we must not search for built-ins when creating a new function.
105286 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
105296 p = p->pNext;
105307 pBest->zName = (const char*)&pBest[1];
105308 pBest->nArg = (u16)nArg;
105309 pBest->funcFlags = enc;
105311 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
105317 pBest->pNext = pOther;
105321 if( pBest && (pBest->xSFunc || createFlag) ){
105341 temp1 = pSchema->tblHash;
105342 temp2 = pSchema->trigHash;
105343 sqlite3HashInit(&pSchema->trigHash);
105344 sqlite3HashClear(&pSchema->idxHash);
105349 sqlite3HashInit(&pSchema->tblHash);
105355 sqlite3HashClear(&pSchema->fkeyHash);
105356 pSchema->pSeqTab = 0;
105357 if( pSchema->schemaFlags & DB_SchemaLoaded ){
105358 pSchema->iGeneration++;
105359 pSchema->schemaFlags &= ~DB_SchemaLoaded;
105376 }else if ( 0==p->file_format ){
105377 sqlite3HashInit(&p->tblHash);
105378 sqlite3HashInit(&p->idxHash);
105379 sqlite3HashInit(&p->trigHash);
105380 sqlite3HashInit(&p->fkeyHash);
105381 p->enc = SQLITE_UTF8;
105414 ** pSrc->a[0].pTab Pointer to the Table object
105415 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
105419 struct SrcList_item *pItem = pSrc->a;
105421 assert( pItem && pSrc->nSrc==1 );
105423 sqlite3DeleteTable(pParse->db, pItem->pTab);
105424 pItem->pTab = pTab;
105426 pTab->nTabRef++;
105448 ** In either case leave an error message in pParse and return non-zero.
105451 && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
105452 || ( (pTab->tabFlags & TF_Readonly)!=0
105453 && (pParse->db->flags & SQLITE_WriteSchema)==0
105454 && pParse->nested==0 )
105456 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
105461 if( !viewOk && pTab->pSelect ){
105462 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
105485 sqlite3 *db = pParse->db;
105486 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
105490 assert( pFrom->nSrc==1 );
105491 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
105492 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
105493 assert( pFrom->a[0].pOn==0 );
105494 assert( pFrom->a[0].pUsing==0 );
105509 ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
105515 SrcList *pSrc, /* the FROM clause -- which tables to scan */
105561 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
105563 sqlite3ExprListDelete(pParse->db, pEList);
105579 sqlite3ExprDelete(pParse->db, pWhere);
105580 sqlite3ExprListDelete(pParse->db, pOrderBy);
105581 sqlite3ExprDelete(pParse->db, pLimit);
105582 sqlite3ExprDelete(pParse->db, pOffset);
105591 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
105613 int memCnt = -1; /* Memory cell used for change counting */
105637 db = pParse->db;
105638 if( pParse->nErr || db->mallocFailed ){
105641 assert( pTabList->nSrc==1 );
105656 isView = pTab->pSelect!=0;
105676 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105677 assert( iDb<db->nDb );
105678 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
105679 db->aDb[iDb].zDbSName);
105688 assert( pTabList->nSrc==1 );
105689 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
105690 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
105691 pParse->nTab++;
105697 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
105706 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
105731 if( db->flags & SQLITE_CountRows ){
105732 memCnt = ++pParse->nMem;
105738 ** It is easier just to erase the whole table. Prior to version 3.6.5,
105743 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
105753 && db->xPreUpdateCallback==0
105757 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
105759 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
105760 pTab->zName, P4_STATIC);
105762 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
105763 assert( pIdx->pSchema==pTab->pSchema );
105764 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
105776 iRowSet = ++pParse->nMem;
105783 nPk = pPk->nKeyCol;
105784 iPk = pParse->nMem+1;
105785 pParse->nMem += nPk;
105786 iEphCur = pParse->nTab++;
105795 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
105796 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
105797 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
105806 if( db->flags & SQLITE_CountRows ){
105813 assert( pPk->aiColumn[i]>=0 );
105815 pPk->aiColumn[i], iPk+i);
105819 iKey = pParse->nMem + 1;
105820 iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
105821 if( iKey>pParse->nMem ) pParse->nMem = iKey;
105825 /* For ONEPASS, no need to store the rowid/primary-key. There is only
105836 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
105837 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
105842 iKey = ++pParse->nMem;
105845 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
105880 /* Set up a loop over the rowids/primary-keys that were found in the
105881 ** where-clause loop above.
105885 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
105886 assert( pPk!=0 || pTab->pSelect!=0 );
105910 pParse->isMultiWrite = 0;
105915 int count = (pParse->nested==0); /* True to count changes */
105920 /* End of the loop over all rowids/primary-keys. */
105931 } /* End non-truncate path */
105937 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
105945 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
105981 ** cursor number iIdxCur+i for the i-th index.
106017 u8 count, /* If non-zero, increment the row change counter */
106022 Vdbe *v = pParse->pVdbe; /* Vdbe */
106056 iOld = pParse->nMem+1;
106057 pParse->nMem += (1 + pTab->nCol);
106059 /* Populate the OLD.* pseudo-table register array. These values will be
106062 for(iCol=0; iCol<pTab->nCol; iCol++){
106089 iIdxNoSeek = -1;
106102 ** If variable 'count' is non-zero, then this OP_Delete instruction should
106103 ** invoke the update-hook. The pre-update-hook, on the other hand should
106105 ** the update-hook is not invoked for rows removed by REPLACE, but the
106106 ** pre-update-hook is.
106108 if( pTab->pSelect==0 ){
106112 if( pParse->nested==0 ){
106154 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
106155 ** index is the 0-th index.)
106169 int r1 = -1; /* Register holding an index key */
106176 v = pParse->pVdbe;
106178 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
106183 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
106187 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
106234 Vdbe *v = pParse->pVdbe;
106240 if( pIdx->pPartIdxWhere ){
106242 pParse->iSelfTab = iDataCur + 1;
106244 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
106246 pParse->iSelfTab = 0;
106251 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
106253 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
106256 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
106257 && pPrior->aiColumn[j]!=XN_EXPR
106273 if( pIdx->pTable->pSelect ){
106274 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
106275 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
106283 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
106289 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
106307 ** This file contains the C-language implementations for many of the SQL
106321 assert( context->pVdbe!=0 );
106322 pOp = &context->pVdbe->aOp[context->iOp-1];
106323 assert( pOp->opcode==OP_CollSeq );
106324 assert( pOp->p4type==P4_COLLSEQ );
106325 return pOp->p4.pColl;
106333 context->skipFlag = 1;
106337 ** Implementation of the non-aggregate min() and max() functions
106350 mask = sqlite3_user_data(context)==0 ? 0 : -1;
106353 assert( mask==-1 || mask==0 );
106375 int i = sqlite3_value_type(argv[0]) - 1;
106382 assert( SQLITE_NULL==5 );
106383 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
106387 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
106431 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
106442 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
106444 ** equivalent positive 64-bit two complement value. */
106445 sqlite3_result_error(context, "integer overflow", -1);
106448 iVal = -iVal;
106454 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
106461 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
106465 if( rVal<0 ) rVal = -rVal;
106516 nHaystack--;
106540 x.nArg = argc-1;
106543 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
106556 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
106557 ** of x. If x is text, then we actually count UTF-8 characters.
106601 ** as substr(X,1,N) - it returns the first N characters of X. This
106602 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
106603 ** from 2009-02-02 for compatibility of applications that exploited the
106610 p2 = -p2;
106614 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
106624 p1--;
106626 p2--;
106629 p1 -= p2;
106639 p1--;
106641 for(z2=z; *z2 && p2; p2--){
106644 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
106648 p2 = len-p1;
106672 /* If Y==0 and X will fit in a 64-bit int,
106676 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
106678 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
106679 r = -(double)((sqlite_int64)((-r)+0.5));
106704 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
106705 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
106706 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
106768 #define noopFunc versionFunc /* Substitute function - never called */
106783 ** (or -9223372036854775808) since when you do abs() of that
106788 ** therefore be no less than -9223372036854775807.
106790 r = -(r & LARGEST_INT64);
106830 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
106839 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
106864 /* IMP: R-52756-41993 This function is a wrapper around the
106870 ** A structure defining how to do GLOB-style comparisons.
106893 /* The correct SQL-92 behavior is for the LIKE operator to ignore
106908 ** Compare two UTF-8 strings for equality where the first string is
106928 ** range of characters can be specified using '-'. Example:
106929 ** "[a-z]" matches any single lower-case letter. To match a '-', make
106952 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
106953 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
106954 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
106970 if( pInfo->matchSet==0 ){
106976 assert( matchOther<0x80 ); /* '[' is a single-byte character */
106978 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
106991 ** For a case-insensitive search, set variable cx to be the same as
107020 if( pInfo->matchSet==0 ){
107040 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
107071 ** non-zero if there is no match.
107078 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
107079 ** a miss - like strcmp().
107097 ** the build-in LIKE operator. The first argument to the function is the
107136 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
107137 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
107138 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
107139 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
107145 /* The escape character string must consist of a single UTF-8 character.
107150 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
107152 "ESCAPE expression must be a single character", -1);
107157 escape = pInfo->matchSet;
107194 /* IMP: R-48699-48617 This function is an SQL wrapper around the
107195 ** sqlite3_libversion() C-interface. */
107196 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
107210 /* IMP: R-24470-31136 This function is an SQL wrapper around the
107212 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
107218 ** its side-effects.
107244 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
107268 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
107272 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
107276 /* Array for converting from half-bytes (nybbles) into ASCII hex
107279 '0', '1', '2', '3', '4', '5', '6', '7',
107288 ** single-quote escapes.
107303 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
107326 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
107363 ** The unicode() function. Return the integer unicode code-point value
107415 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
107448 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
107461 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
107498 || sqlite3_context_db_handle(context)->mallocFailed );
107518 loopLimit = nStr - nPattern;
107525 nOut += nRep - nPattern;
107526 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
107527 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
107528 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
107542 i += nPattern-1;
107545 assert( j+nStr-i+1==nOut );
107546 memcpy(&zOut[j], &zStr[i], nStr-i);
107547 j += nStr - i;
107601 aLen[nChar] = (u8)(z - azChar[nChar]);
107616 nIn -= len;
107624 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
107627 nIn -= len;
107642 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
107643 ** When the "sqlite3" command-line shell is built using this functionality,
107645 ** involving application-defined functions to be examined in a generic
107653 /* no-op */
107658 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
107659 ** is only available if the SQLITE_SOUNDEX compile-time option is used
107666 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
107682 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
107684 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
107711 /* IMP: R-64894-50321 The string "?000" is returned if the argument
107720 ** A function that loads a shared-library extension then returns NULL.
107731 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
107732 sqlite3_result_error(context, "not authorized", -1);
107742 sqlite3_result_error(context, zErrMsg, -1);
107759 u8 approx; /* True if non-integer value was input to the sum */
107780 p->cnt++;
107783 p->rSum += v;
107784 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
107785 p->overflow = 1;
107788 p->rSum += sqlite3_value_double(argv[0]);
107789 p->approx = 1;
107796 if( p && p->cnt>0 ){
107797 if( p->overflow ){
107798 sqlite3_result_error(context,"integer overflow",-1);
107799 }else if( p->approx ){
107800 sqlite3_result_double(context, p->rSum);
107802 sqlite3_result_int64(context, p->iSum);
107809 if( p && p->cnt>0 ){
107810 sqlite3_result_double(context, p->rSum/(double)p->cnt);
107817 sqlite3_result_double(context, p ? p->rSum : (double)0);
107836 p->n++;
107843 ** expressed as a 32-bit integer. */
107844 assert( argc==1 || p==0 || p->n>0x7fffffff
107845 || p->n==sqlite3_aggregate_count(context) );
107851 sqlite3_result_int64(context, p ? p->n : 0);
107870 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
107871 }else if( pBest->flags ){
107878 ** sqlite3_user_data() function returns (void *)-1. For min() it
107891 pBest->db = sqlite3_context_db_handle(context);
107899 if( pRes->flags ){
107924 int firstTerm = pAccum->mxAlloc==0;
107925 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
107945 if( pAccum->accError==STRACCUM_TOOBIG ){
107947 }else if( pAccum->accError==STRACCUM_NOMEM ){
107950 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
107957 ** This routine does per-connection function registration. Most
107958 ** of the built-in functions above are part of the global function set.
107970 ** Set the LIKEOPT flag on the 2-argument function with the given name.
107976 pDef->funcFlags |= flagVal;
107981 ** Register the built-in LIKE and GLOB functions. The caseSensitive
108005 ** return TRUE. If the function is not a LIKE-style function then
108015 if( pExpr->op!=TK_FUNCTION
108016 || !pExpr->x.pList
108017 || pExpr->x.pList->nExpr!=2
108022 pDef = sqlite3FindFunction(db, pExpr->u.zToken, 2, SQLITE_UTF8, 0);
108023 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
108031 memcpy(aWc, pDef->pUserData, 3);
108035 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
108041 ** to the global function hash table. This occurs at start-time (as
108052 ** FuncDef.pHash elements at start-time. The elements of this array
108053 ** are read-only after initialization is complete.
108084 FUNCTION(min, -1, 0, 1, minmaxFunc ),
108088 FUNCTION(max, -1, 1, 1, minmaxFunc ),
108095 FUNCTION(printf, -1, 0, 0, printfFunc ),
108097 FUNCTION(char, -1, 0, 0, charFunc ),
108139 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
108143 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
108154 #if 0 /* Enable to print out how the built-in functions are hashed */
108159 printf("FUNC-HASH %02d:", i);
108160 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
108161 int n = sqlite3Strlen30(p->zName);
108162 int h = p->zName[0] + n;
108163 printf(" %s(%d)", p->zName, h);
108193 ** --------------------------
108275 ** ---------------
108277 ** Before coding an UPDATE or DELETE row operation, the code-generator
108282 ** accessed). No information is required by the code-generator before
108286 ** sqlite3FkRequired() - Test to see if FK processing is required.
108287 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
108291 ** --------------------------------------
108293 ** sqlite3FkCheck() - Check for foreign key violations.
108294 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
108295 ** sqlite3FkDelete() - Delete an FKey structure.
108300 ** -----------------------
108330 ** constraint to the parent table column stored in the left-most column
108332 ** child table column that corresponds to the second left-most column of
108351 ** then non-zero is returned, and a "foreign key mismatch" error loaded
108352 ** into pParse. If an OOM error occurs, non-zero is returned and the
108353 ** pParse->db->mallocFailed flag is set.
108364 int nCol = pFKey->nCol; /* Number of columns in parent key */
108365 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
108372 /* If this is a non-composite (single column) foreign key, check if it
108378 ** Non-composite foreign keys do not require the aiCol array.
108388 if( pParent->iPKey>=0 ){
108390 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
108394 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
108399 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
108400 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
108412 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
108417 /* If zKey is non-NULL, then this foreign key was declared to
108423 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
108432 zDfltColl = pParent->aCol[iCol].zColl;
108434 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
108436 zIdxCol = pParent->aCol[iCol].zName;
108438 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
108439 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
108451 if( !pParse->disableTriggers ){
108453 "foreign key mismatch - \"%w\" referencing \"%w\"",
108454 pFKey->pFrom->zName, pFKey->zTo);
108456 sqlite3DbFree(pParse->db, aiCol);
108468 ** affected - once to "delete" the old row, and then again to "insert" the
108478 ** --------------------------------------------------------------------------
108503 int iCur = pParse->nTab - 1; /* Cursor number to use */
108514 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
108517 for(i=0; i<pFKey->nCol; i++){
108539 ** to increment the constraint-counter (i.e. this is an INSERT operation),
108541 ** increment the constraint-counter. */
108542 if( pTab==pFKey->pFrom && nIncr==1 ){
108550 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
108554 int nCol = pFKey->nCol;
108558 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
108565 ** to increment the constraint-counter (i.e. this is an INSERT operation),
108567 ** increment the constraint-counter.
108569 ** If any of the parent-key values are NULL, then the row cannot match
108571 ** of the parent-key values are NULL (at this point it is known that
108574 if( pTab==pFKey->pFrom && nIncr==1 ){
108578 int iParent = pIdx->aiColumn[i]+1+regData;
108579 assert( pIdx->aiColumn[i]>=0 );
108580 assert( aiCol[i]!=pTab->iPKey );
108581 if( pIdx->aiColumn[i]==pTab->iPKey ){
108592 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
108600 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
108601 && !pParse->pToplevel
108602 && !pParse->isMultiWrite
108612 if( nIncr>0 && pFKey->isDeferred==0 ){
108615 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
108640 sqlite3 *db = pParse->db;
108644 if( iCol>=0 && iCol!=pTab->iPKey ){
108645 pCol = &pTab->aCol[iCol];
108646 pExpr->iTable = regBase + iCol + 1;
108647 pExpr->affinity = pCol->affinity;
108648 zColl = pCol->zColl;
108649 if( zColl==0 ) zColl = db->pDfltColl->zName;
108652 pExpr->iTable = regBase;
108653 pExpr->affinity = SQLITE_AFF_INTEGER;
108671 pExpr->pTab = pTab;
108672 pExpr->iTable = iCursor;
108673 pExpr->iColumn = iCol;
108682 ** code for an SQL UPDATE operation, this function may be called twice -
108685 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
108694 ** --------------------------------------------------------------------------
108720 sqlite3 *db = pParse->db; /* Database handle */
108728 assert( pIdx==0 || pIdx->pTable==pTab );
108729 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
108730 assert( pIdx!=0 || pFKey->nCol==1 );
108734 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
108740 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
108746 for(i=0; i<pFKey->nCol; i++){
108753 iCol = pIdx ? pIdx->aiColumn[i] : -1;
108755 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
108757 zCol = pFKey->pFrom->aCol[iCol].zName;
108774 if( pTab==pFKey->pFrom && nIncr>0 ){
108779 pLeft = exprTableRegister(pParse, pTab, regData, -1);
108780 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
108786 for(i=0; i<pPk->nKeyCol; i++){
108787 i16 iCol = pIdx->aiColumn[i];
108790 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
108808 if( pParse->nErr==0 ){
108810 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
108838 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
108844 ** and all of its sub-components.
108846 ** The Trigger structure or any of its sub-components may be allocated from
108851 TriggerStep *pStep = p->step_list;
108852 sqlite3ExprDelete(dbMem, pStep->pWhere);
108853 sqlite3ExprListDelete(dbMem, pStep->pExprList);
108854 sqlite3SelectDelete(dbMem, pStep->pSelect);
108855 sqlite3ExprDelete(dbMem, p->pWhen);
108878 sqlite3 *db = pParse->db;
108879 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
108891 for(p=pTab->pFKey; p; p=p->pNextFrom){
108892 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
108899 pParse->disableTriggers = 1;
108901 pParse->disableTriggers = 0;
108912 if( (db->flags & SQLITE_DeferFKs)==0 ){
108932 ** is set to -1). If the rowid column is modified by the UPDATE statement
108933 ** the bChngRowid argument is non-zero.
108945 for(i=0; i<p->nCol; i++){
108946 int iChildKey = p->aCol[i].iFrom;
108948 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
108959 ** is set to -1). If the rowid column is modified by the UPDATE statement
108960 ** the bChngRowid argument is non-zero.
108972 for(i=0; i<p->nCol; i++){
108973 char *zKey = p->aCol[i].zCol;
108975 for(iKey=0; iKey<pTab->nCol; iKey++){
108976 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
108977 Column *pCol = &pTab->aCol[iKey];
108979 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
108980 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
108996 if( pTop->pTriggerPrg ){
108997 Trigger *p = pTop->pTriggerPrg->pTrigger;
108998 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
108999 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
109013 ** first register in an array of (pTab->nCol+1) registers containing the
109019 ** first register of an array of (pTab->nCol+1) registers containing the new
109035 sqlite3 *db = pParse->db; /* Database handle */
109039 int isIgnoreErrors = pParse->disableTriggers;
109041 /* Exactly one of regOld and regNew should be non-zero. */
109044 /* If foreign-keys are disabled, this function is a no-op. */
109045 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
109047 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
109048 zDb = db->aDb[iDb].zDbSName;
109052 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
109062 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
109072 if( pParse->disableTriggers ){
109073 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
109075 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
109079 if( !isIgnoreErrors || db->mallocFailed ) return;
109086 ** FK counter for each row of the current table with non-NULL keys.
109089 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
109090 for(i=0; i<pFKey->nCol; i++){
109091 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
109094 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
109098 assert( pFKey->nCol==1 || (aiFree && pIdx) );
109103 iCol = pFKey->aCol[0].iFrom;
109106 for(i=0; i<pFKey->nCol; i++){
109107 if( aiCol[i]==pTab->iPKey ){
109108 aiCol[i] = -1;
109110 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
109115 if( db->xAuth ){
109117 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
109118 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
109124 /* Take a shared-cache advisory read-lock on the parent table. Allocate
109127 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
109128 pParse->nTab++;
109134 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
109153 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
109162 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
109163 && !pParse->pToplevel && !pParse->isMultiWrite
109172 if( !isIgnoreErrors || db->mallocFailed ) return;
109175 assert( aiCol || pFKey->nCol==1 );
109181 struct SrcList_item *pItem = pSrc->a;
109182 pItem->pTab = pFKey->pFrom;
109183 pItem->zName = pFKey->pFrom->zName;
109184 pItem->pTab->nTabRef++;
109185 pItem->iCursor = pParse->nTab++;
109188 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
109191 int eAction = pFKey->aAction[aChange!=0];
109196 ** So do not set the "may-abort" flag in this case.
109199 ** may-abort flag will eventually be set on this statement anyway
109210 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
109214 pItem->zName = 0;
109232 if( pParse->db->flags&SQLITE_ForeignKeys ){
109235 for(p=pTab->pFKey; p; p=p->pNextFrom){
109236 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
109238 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
109242 for(i=0; i<pIdx->nKeyCol; i++){
109243 assert( pIdx->aiColumn[i]>=0 );
109244 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
109259 ** entry in the aChange[] array is set to -1. If the column is modified,
109264 ** non-zero. If there is no foreign key related processing, this function
109278 int *aChange, /* Non-NULL for UPDATE operations */
109282 if( pParse->db->flags&SQLITE_ForeignKeys ){
109287 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
109294 for(p=pTab->pFKey; p; p=p->pNextFrom){
109295 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
109302 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
109304 if( p->aAction[1]!=OE_None ) return 2;
109315 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
109324 ** sub-system, code for them is created by fkScanChildren()).
109346 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
109348 sqlite3 *db = pParse->db; /* Database handle */
109353 action = pFKey->aAction[iAction];
109354 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
109357 pTrigger = pFKey->apTrigger[iAction];
109363 int *aiCol = 0; /* child table cols -> parent key cols */
109372 assert( aiCol || pFKey->nCol==1 );
109374 for(i=0; i<pFKey->nCol; i++){
109382 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
109384 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
109385 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
109387 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
109388 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
109426 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
109441 zFrom = pFKey->pFrom->zName;
109452 pRaise->affinity = OE_Abort;
109464 db->lookaside.bDisable++;
109469 nFrom + 1 /* Space for pStep->zTarget */
109472 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
109473 pStep->zTarget = (char *)&pStep[1];
109474 memcpy((char *)pStep->zTarget, zFrom, nFrom);
109476 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
109477 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
109478 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
109481 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
109485 /* Re-enable the lookaside buffer, if it was disabled earlier. */
109486 db->lookaside.bDisable--;
109492 if( db->mallocFailed==1 ){
109500 pStep->op = TK_SELECT;
109504 pStep->op = TK_DELETE;
109508 pStep->op = TK_UPDATE;
109510 pStep->pTrig = pTrigger;
109511 pTrigger->pSchema = pTab->pSchema;
109512 pTrigger->pTabSchema = pTab->pSchema;
109513 pFKey->apTrigger[iAction] = pTrigger;
109514 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
109527 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
109532 /* If foreign-key support is enabled, iterate through all FKs that
109535 ** trigger sub-program. */
109536 if( pParse->db->flags&SQLITE_ForeignKeys ){
109538 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
109558 FKey *pNext; /* Copy of pFKey->pNextFrom */
109561 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
109562 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
109565 if( !db || db->pnBytesFreed==0 ){
109566 if( pFKey->pPrevTo ){
109567 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
109569 void *p = (void *)pFKey->pNextTo;
109570 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
109571 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
109573 if( pFKey->pNextTo ){
109574 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
109578 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
109581 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
109585 fkTriggerDelete(db, pFKey->apTrigger[0]);
109586 fkTriggerDelete(db, pFKey->apTrigger[1]);
109589 pNext = pFKey->pNextFrom;
109633 sqlite3TableLock(pParse, iDb, pTab->tnum,
109634 (opcode==OP_OpenWrite)?1:0, pTab->zName);
109636 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
109637 VdbeComment((v, "%s", pTab->zName));
109641 assert( pPk->tnum==pTab->tnum );
109642 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
109644 VdbeComment((v, "%s", pTab->zName));
109654 ** ------------------------------
109669 if( !pIdx->zColAff ){
109679 Table *pTab = pIdx->pTable;
109680 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
109681 if( !pIdx->zColAff ){
109685 for(n=0; n<pIdx->nColumn; n++){
109686 i16 x = pIdx->aiColumn[n];
109688 pIdx->zColAff[n] = pTab->aCol[x].affinity;
109690 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
109694 assert( pIdx->aColExpr!=0 );
109695 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
109697 pIdx->zColAff[n] = aff;
109700 pIdx->zColAff[n] = 0;
109703 return pIdx->zColAff;
109719 ** ------------------------------
109728 char *zColAff = pTab->zColAff;
109731 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
109737 for(i=0; i<pTab->nCol; i++){
109738 zColAff[i] = pTab->aCol[i].affinity;
109741 zColAff[i--] = 0;
109743 pTab->zColAff = zColAff;
109750 sqlite3VdbeChangeP4(v, -1, zColAff, i);
109756 ** Return non-zero if the table pTab in database iDb or any of its indices
109766 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
109772 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
109774 int tnum = pOp->p2;
109775 if( tnum==pTab->tnum ){
109778 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
109779 if( tnum==pIndex->tnum ){
109785 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
109786 assert( pOp->p4.pVtab!=0 );
109787 assert( pOp->p4type==P4_VTAB );
109824 if( (pTab->tabFlags & TF_Autoincrement)!=0
109825 && (pParse->db->flags & SQLITE_Vacuum)==0
109830 pInfo = pToplevel->pAinc;
109831 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
109833 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
109835 pInfo->pNext = pToplevel->pAinc;
109836 pToplevel->pAinc = pInfo;
109837 pInfo->pTab = pTab;
109838 pInfo->iDb = iDb;
109839 pToplevel->nMem++; /* Register to hold name of table */
109840 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
109841 pToplevel->nMem++; /* Rowid in sqlite_sequence */
109843 memId = pInfo->regCtr;
109854 sqlite3 *db = pParse->db; /* The database connection */
109857 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
109859 /* This routine is never called during trigger-generation. It is
109860 ** only called from the top-level */
109861 assert( pParse->pTriggerTab==0 );
109865 for(p = pParse->pAinc; p; p = p->pNext){
109873 /* 5 */ {OP_Column, 0, 1, 0},
109880 pDb = &db->aDb[p->iDb];
109881 memId = p->regCtr;
109882 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
109883 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
109884 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
109890 aOp[3].p1 = memId-1;
109894 aOp[5].p3 = memId;
109909 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
109922 Vdbe *v = pParse->pVdbe;
109923 sqlite3 *db = pParse->db;
109926 for(p = pParse->pAinc; p; p = p->pNext){
109936 Db *pDb = &db->aDb[p->iDb];
109938 int memId = p->regCtr;
109941 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
109942 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
109947 aOp[2].p1 = memId-1;
109956 if( pParse->pAinc ) autoIncrementEnd(pParse);
109961 ** above are all no-ops
109985 ** then a list of all (non-hidden) columns for the table is substituted.
109990 ** first two forms shown above. A VALUES clause is really just short-hand
109996 ** insert with data coming from a single-row VALUES clause, the code executes
109997 ** once straight down through. Pseudo-code follows (we call this
110009 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
110033 ** X <- A
110041 ** end-coroutine X
110054 ** X <- A
110062 ** end co-routine R
110091 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
110119 db = pParse->db;
110120 if( pParse->nErr || db->mallocFailed ){
110129 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
110130 pList = pSelect->pEList;
110131 pSelect->pEList = 0;
110138 assert( pTabList->nSrc==1 );
110139 zTab = pTabList->a[0].zName;
110145 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110146 assert( iDb<db->nDb );
110147 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
110148 db->aDb[iDb].zDbSName) ){
110158 isView = pTab->pSelect!=0;
110171 ** ViewGetColumnNames() is a no-op if pTab is not a view.
110177 /* Cannot insert into a read-only table.
110187 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
110215 regRowid = regIns = pParse->nMem+1;
110216 pParse->nMem += pTab->nCol + 1;
110219 pParse->nMem++;
110232 ** PRIMARY KEY in the original table is pTab->iPKey.)
110234 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
110236 for(i=0; i<pColumn->nId; i++){
110237 pColumn->a[i].idx = -1;
110239 for(i=0; i<pColumn->nId; i++){
110240 for(j=0; j<pTab->nCol; j++){
110241 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
110242 pColumn->a[i].idx = j;
110244 if( j==pTab->iPKey ){
110250 if( j>=pTab->nCol ){
110251 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
110256 pTabList, 0, pColumn->a[i].zName);
110257 pParse->checkSchema = 1;
110265 ** is coming from a SELECT statement, then generate a co-routine that
110267 ** co-routine is the common header to the 3rd and 4th templates.
110270 /* Data is coming from a SELECT or from a multi-row VALUES clause.
110271 ** Generate a co-routine to run the SELECT. */
110272 int regYield; /* Register holding co-routine entry-point */
110273 int addrTop; /* Top of the co-routine */
110276 regYield = ++pParse->nMem;
110281 dest.nSdst = pTab->nCol;
110284 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
110286 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
110287 assert( pSelect->pEList );
110288 nColumn = pSelect->pEList->nExpr;
110318 srcTab = pParse->nTab++;
110333 ** single-row VALUES clause
110338 srcTab = -1;
110341 nColumn = pList->nExpr;
110355 ipkColumn = pTab->iPKey;
110361 for(i=0; i<pTab->nCol; i++){
110362 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
110364 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
110367 pTabList, 0, pTab->nCol-nHidden, nColumn);
110370 if( pColumn!=0 && nColumn!=pColumn->nId ){
110371 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
110377 if( db->flags & SQLITE_CountRows ){
110378 regRowCount = ++pParse->nMem;
110385 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
110391 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
110393 aRegIdx[i] = ++pParse->nMem;
110394 pParse->nMem += pIdx->nColumn;
110428 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
110434 ** not happened yet) so we substitute a rowid of -1
110437 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
110445 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
110448 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
110460 for(i=j=0; i<pTab->nCol; i++){
110462 for(j=0; j<pColumn->nId; j++){
110463 if( pColumn->a[j].idx==i ) break;
110466 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
110467 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
110468 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
110473 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
110475 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
110489 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
110491 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
110509 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
110510 pOp = sqlite3VdbeGetOp(v, -1);
110511 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
110513 pOp->opcode = OP_NewRowid;
110514 pOp->p1 = iDataCur;
110515 pOp->p2 = regRowid;
110516 pOp->p3 = regAutoinc;
110546 for(i=0; i<pTab->nCol; i++){
110548 if( i==pTab->iPKey ){
110553 ** As there may be shallow copies of this value, make it a soft-NULL */
110558 if( IsHiddenColumn(&pTab->aCol[i]) ){
110559 j = -1;
110562 j = i - nHidden;
110565 for(j=0; j<pColumn->nId; j++){
110566 if( pColumn->a[j].idx==i ) break;
110569 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
110570 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
110578 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
110589 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
110611 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
110621 if( (db->flags & SQLITE_CountRows)!=0 ){
110628 pTab, regData-2-pTab->nCol, onError, endOfLoop);
110649 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
110658 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
110686 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
110692 ** bit 0x01 of pWalker->eCode if
110693 ** pWalker->eCode to 0 if this expression node references any of the
110697 if( pExpr->op==TK_COLUMN ){
110698 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
110699 if( pExpr->iColumn>=0 ){
110700 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
110701 pWalker->eCode |= CKCNSTRNT_COLUMN;
110704 pWalker->eCode |= CKCNSTRNT_ROWID;
110711 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
110744 ** pTab->nCol+1 registers in this range. The first register (the one
110771 ** at pTab->pIndex.
110778 ** for the first index in the pTab->pIndex list. Cursors for other indices
110779 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
110787 ** --------------- ---------- ----------------------------------------
110817 ** Or if overrideError==OE_Default, then the pParse->onError parameter
110818 ** is used. Or if pParse->onError==OE_Default then the onError value
110829 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
110852 db = pParse->db;
110855 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
110856 nCol = pTab->nCol;
110867 nPkField = pPk->nKeyCol;
110877 if( i==pTab->iPKey ){
110884 onError = pTab->aCol[i].notNull;
110891 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
110902 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
110903 pTab->aCol[i].zName);
110920 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
110930 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
110931 ExprList *pCheck = pTab->pCheck;
110932 pParse->iSelfTab = -(regNewData+1);
110934 for(i=0; i<pCheck->nExpr; i++){
110936 Expr *pExpr = pCheck->a[i].pExpr;
110943 char *zName = pCheck->a[i].zName;
110944 if( zName==0 ) zName = pTab->zName;
110945 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
110952 pParse->iSelfTab = 0;
110963 onError = pTab->keyConf;
110985 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110986 if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
111012 ** recursive-triggers flag is set, call GenerateRowDelete() to
111014 ** the triggers and remove both the table and index b-tree entries.
111016 ** Otherwise, if there are no triggers or the recursive-triggers
111018 ** GenerateRowIndexDelete(). This removes the index b-tree entries
111019 ** only. The table b-tree entry will be replaced by the new entry
111034 if( db->flags&SQLITE_RecTriggers ){
111040 regNewData, 1, 0, OE_Replace, 1, -1);
111044 /* This OP_Delete opcode fires the pre-update-hook only. It does
111045 ** not modify the b-tree. It is more efficient to let the coming
111052 if( pTab->pIndex ){
111054 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
111080 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
111095 if( pIdx->pPartIdxWhere ){
111097 pParse->iSelfTab = -(regNewData+1);
111098 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
111100 pParse->iSelfTab = 0;
111107 for(i=0; i<pIdx->nColumn; i++){
111108 int iField = pIdx->aiColumn[i];
111111 pParse->iSelfTab = -(regNewData+1);
111112 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
111113 pParse->iSelfTab = 0;
111114 VdbeComment((v, "%s column %d", pIdx->zName, i));
111116 if( iField==XN_ROWID || iField==pTab->iPKey ){
111122 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
111125 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
111126 VdbeComment((v, "for %s", pIdx->zName));
111128 if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
111141 onError = pIdx->onError;
111157 ** (5) No FK constraint counters need to be updated if a conflict occurs.
111159 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
111162 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
111164 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
111165 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
111173 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
111181 ** is different from old-rowid */
111190 ** store it in registers regR..regR+nPk-1 */
111192 for(i=0; i<pPk->nKeyCol; i++){
111193 assert( pPk->aiColumn[i]>=0 );
111194 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
111196 VdbeComment((v, "%s.%s", pTab->zName,
111197 pTab->aCol[pPk->aiColumn[i]].zName));
111208 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
111212 for(i=0; i<pPk->nKeyCol; i++){
111213 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
111214 x = pPk->aiColumn[i];
111216 if( i==(pPk->nKeyCol-1) ){
111249 if( db->flags&SQLITE_RecTriggers ){
111274 ** to be the number of columns in table pTab that must not be NULL-trimmed.
111276 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
111282 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
111283 if( pTab->pSchema->file_format<2 ) return;
111285 for(i=pTab->nCol-1; i>0; i--){
111286 if( pTab->aCol[i].pDflt!=0 ) break;
111287 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
111328 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
111329 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
111332 if( pIdx->pPartIdxWhere ){
111338 assert( pParse->nested==0 );
111352 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
111358 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
111362 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
111364 if( pParse->nested ){
111377 if( !pParse->nested ){
111393 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
111399 ** pTab->pIndex list.
111401 ** If pTab is a virtual table, then this routine is a no-op and the
111423 /* This routine is a no-op for virtual tables. Leave the output
111428 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
111431 if( iBase<0 ) iBase = pParse->nTab;
111437 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
111440 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
111442 assert( pIdx->pSchema==pTab->pSchema );
111448 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
111451 VdbeComment((v, "%s", pIdx->zName));
111454 if( iBase>pParse->nTab ) pParse->nTab = iBase;
111463 ** purposes only - to make sure the transfer optimization really
111485 assert( pDest->pTable!=pSrc->pTable );
111486 if( pDest->nKeyCol!=pSrc->nKeyCol ){
111489 if( pDest->onError!=pSrc->onError ){
111492 for(i=0; i<pSrc->nKeyCol; i++){
111493 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
111496 if( pSrc->aiColumn[i]==XN_EXPR ){
111497 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
111498 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
111499 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
111503 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
111506 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
111510 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
111528 ** There are lots of rules for determining compatibility - see comments
111533 ** is empty - a factor that can only be determined at run-time. In that
111550 sqlite3 *db = pParse->db;
111554 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
111569 if( pParse->pWith || pSelect->pWith ){
111584 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
111587 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
111588 if( pSelect->pSrc->nSrc!=1 ){
111591 if( pSelect->pSrc->a[0].pSelect ){
111594 if( pSelect->pWhere ){
111597 if( pSelect->pOrderBy ){
111602 if( pSelect->pGroupBy ){
111605 if( pSelect->pLimit ){
111608 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
111609 if( pSelect->pPrior ){
111612 if( pSelect->selFlags & SF_Distinct ){
111615 pEList = pSelect->pEList;
111617 if( pEList->nExpr!=1 ){
111620 assert( pEList->a[0].pExpr );
111621 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
111629 pItem = pSelect->pSrc->a;
111645 if( pSrc->pSelect ){
111648 if( pDest->nCol!=pSrc->nCol ){
111651 if( pDest->iPKey!=pSrc->iPKey ){
111654 for(i=0; i<pDest->nCol; i++){
111655 Column *pDestCol = &pDest->aCol[i];
111656 Column *pSrcCol = &pSrc->aCol[i];
111658 if( (db->flags & SQLITE_Vacuum)==0
111659 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
111664 if( pDestCol->affinity!=pSrcCol->affinity ){
111667 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
111670 if( pDestCol->notNull && !pSrcCol->notNull ){
111675 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
111676 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
111677 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
111678 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
111679 pSrcCol->pDflt->u.zToken)!=0)
111685 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
111689 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
111697 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
111709 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
111713 if( (db->flags & SQLITE_CountRows)!=0 ){
111724 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
111727 iSrc = pParse->nTab++;
111728 iDest = pParse->nTab++;
111734 if( (db->flags & SQLITE_Vacuum)==0 && (
111735 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
111764 if( pDest->iPKey>=0 ){
111771 }else if( pDest->pIndex==0 ){
111775 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
111778 if( db->flags & SQLITE_Vacuum ){
111779 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
111792 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
111793 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
111795 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
111797 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
111801 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
111803 VdbeComment((v, "%s", pSrcIdx->zName));
111804 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
111807 VdbeComment((v, "%s", pDestIdx->zName));
111810 if( db->flags & SQLITE_Vacuum ){
111815 ** order. In this case, instead of seeking within the b-tree as part
111817 ** OP_IdxInsert to seek to the point within the b-tree where each key
111825 for(i=0; i<pSrcIdx->nColumn; i++){
111826 const char *zColl = pSrcIdx->azColl[i];
111829 if( i==pSrcIdx->nColumn ){
111831 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
111834 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
111906 sqlite3_mutex_enter(db->mutex);
111913 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
111919 /* this happens for a comment or white-space */
111934 && db->flags&SQLITE_NullCallback)) ){
111960 /* EVIDENCE-OF: R-38229-40159 If the callback function to
111961 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
111999 assert( (rc&db->errMask)==rc );
112000 sqlite3_mutex_leave(db->mutex);
112337 ** (part of the main SQLite library - not an extension) so that
112344 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
112346 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
112348 #define sqlite3_bind_blob sqlite3_api->bind_blob
112349 #define sqlite3_bind_double sqlite3_api->bind_double
112350 #define sqlite3_bind_int sqlite3_api->bind_int
112351 #define sqlite3_bind_int64 sqlite3_api->bind_int64
112352 #define sqlite3_bind_null sqlite3_api->bind_null
112353 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
112354 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
112355 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
112356 #define sqlite3_bind_text sqlite3_api->bind_text
112357 #define sqlite3_bind_text16 sqlite3_api->bind_text16
112358 #define sqlite3_bind_value sqlite3_api->bind_value
112359 #define sqlite3_busy_handler sqlite3_api->busy_handler
112360 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
112361 #define sqlite3_changes sqlite3_api->changes
112362 #define sqlite3_close sqlite3_api->close
112363 #define sqlite3_collation_needed sqlite3_api->collation_needed
112364 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
112365 #define sqlite3_column_blob sqlite3_api->column_blob
112366 #define sqlite3_column_bytes sqlite3_api->column_bytes
112367 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
112368 #define sqlite3_column_count sqlite3_api->column_count
112369 #define sqlite3_column_database_name sqlite3_api->column_database_name
112370 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
112371 #define sqlite3_column_decltype sqlite3_api->column_decltype
112372 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
112373 #define sqlite3_column_double sqlite3_api->column_double
112374 #define sqlite3_column_int sqlite3_api->column_int
112375 #define sqlite3_column_int64 sqlite3_api->column_int64
112376 #define sqlite3_column_name sqlite3_api->column_name
112377 #define sqlite3_column_name16 sqlite3_api->column_name16
112378 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
112379 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
112380 #define sqlite3_column_table_name sqlite3_api->column_table_name
112381 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
112382 #define sqlite3_column_text sqlite3_api->column_text
112383 #define sqlite3_column_text16 sqlite3_api->column_text16
112384 #define sqlite3_column_type sqlite3_api->column_type
112385 #define sqlite3_column_value sqlite3_api->column_value
112386 #define sqlite3_commit_hook sqlite3_api->commit_hook
112387 #define sqlite3_complete sqlite3_api->complete
112388 #define sqlite3_complete16 sqlite3_api->complete16
112389 #define sqlite3_create_collation sqlite3_api->create_collation
112390 #define sqlite3_create_collation16 sqlite3_api->create_collation16
112391 #define sqlite3_create_function sqlite3_api->create_function
112392 #define sqlite3_create_function16 sqlite3_api->create_function16
112393 #define sqlite3_create_module sqlite3_api->create_module
112394 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
112395 #define sqlite3_data_count sqlite3_api->data_count
112396 #define sqlite3_db_handle sqlite3_api->db_handle
112397 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
112398 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
112399 #define sqlite3_errcode sqlite3_api->errcode
112400 #define sqlite3_errmsg sqlite3_api->errmsg
112401 #define sqlite3_errmsg16 sqlite3_api->errmsg16
112402 #define sqlite3_exec sqlite3_api->exec
112404 #define sqlite3_expired sqlite3_api->expired
112406 #define sqlite3_finalize sqlite3_api->finalize
112407 #define sqlite3_free sqlite3_api->free
112408 #define sqlite3_free_table sqlite3_api->free_table
112409 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
112410 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
112411 #define sqlite3_get_table sqlite3_api->get_table
112413 #define sqlite3_global_recover sqlite3_api->global_recover
112415 #define sqlite3_interrupt sqlite3_api->interruptx
112416 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
112417 #define sqlite3_libversion sqlite3_api->libversion
112418 #define sqlite3_libversion_number sqlite3_api->libversion_number
112419 #define sqlite3_malloc sqlite3_api->malloc
112420 #define sqlite3_mprintf sqlite3_api->mprintf
112421 #define sqlite3_open sqlite3_api->open
112422 #define sqlite3_open16 sqlite3_api->open16
112423 #define sqlite3_prepare sqlite3_api->prepare
112424 #define sqlite3_prepare16 sqlite3_api->prepare16
112425 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
112426 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
112427 #define sqlite3_profile sqlite3_api->profile
112428 #define sqlite3_progress_handler sqlite3_api->progress_handler
112429 #define sqlite3_realloc sqlite3_api->realloc
112430 #define sqlite3_reset sqlite3_api->reset
112431 #define sqlite3_result_blob sqlite3_api->result_blob
112432 #define sqlite3_result_double sqlite3_api->result_double
112433 #define sqlite3_result_error sqlite3_api->result_error
112434 #define sqlite3_result_error16 sqlite3_api->result_error16
112435 #define sqlite3_result_int sqlite3_api->result_int
112436 #define sqlite3_result_int64 sqlite3_api->result_int64
112437 #define sqlite3_result_null sqlite3_api->result_null
112438 #define sqlite3_result_text sqlite3_api->result_text
112439 #define sqlite3_result_text16 sqlite3_api->result_text16
112440 #define sqlite3_result_text16be sqlite3_api->result_text16be
112441 #define sqlite3_result_text16le sqlite3_api->result_text16le
112442 #define sqlite3_result_value sqlite3_api->result_value
112443 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
112444 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
112445 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
112446 #define sqlite3_snprintf sqlite3_api->snprintf
112447 #define sqlite3_step sqlite3_api->step
112448 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
112449 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
112450 #define sqlite3_total_changes sqlite3_api->total_changes
112451 #define sqlite3_trace sqlite3_api->trace
112453 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
112455 #define sqlite3_update_hook sqlite3_api->update_hook
112456 #define sqlite3_user_data sqlite3_api->user_data
112457 #define sqlite3_value_blob sqlite3_api->value_blob
112458 #define sqlite3_value_bytes sqlite3_api->value_bytes
112459 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
112460 #define sqlite3_value_double sqlite3_api->value_double
112461 #define sqlite3_value_int sqlite3_api->value_int
112462 #define sqlite3_value_int64 sqlite3_api->value_int64
112463 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
112464 #define sqlite3_value_text sqlite3_api->value_text
112465 #define sqlite3_value_text16 sqlite3_api->value_text16
112466 #define sqlite3_value_text16be sqlite3_api->value_text16be
112467 #define sqlite3_value_text16le sqlite3_api->value_text16le
112468 #define sqlite3_value_type sqlite3_api->value_type
112469 #define sqlite3_vmprintf sqlite3_api->vmprintf
112470 #define sqlite3_vsnprintf sqlite3_api->vsnprintf
112471 #define sqlite3_overload_function sqlite3_api->overload_function
112472 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
112473 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
112474 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
112475 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
112476 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
112477 #define sqlite3_blob_close sqlite3_api->blob_close
112478 #define sqlite3_blob_open sqlite3_api->blob_open
112479 #define sqlite3_blob_read sqlite3_api->blob_read
112480 #define sqlite3_blob_write sqlite3_api->blob_write
112481 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
112482 #define sqlite3_file_control sqlite3_api->file_control
112483 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
112484 #define sqlite3_memory_used sqlite3_api->memory_used
112485 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
112486 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
112487 #define sqlite3_mutex_free sqlite3_api->mutex_free
112488 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
112489 #define sqlite3_mutex_try sqlite3_api->mutex_try
112490 #define sqlite3_open_v2 sqlite3_api->open_v2
112491 #define sqlite3_release_memory sqlite3_api->release_memory
112492 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
112493 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
112494 #define sqlite3_sleep sqlite3_api->sleep
112495 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
112496 #define sqlite3_vfs_find sqlite3_api->vfs_find
112497 #define sqlite3_vfs_register sqlite3_api->vfs_register
112498 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
112499 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
112500 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
112501 #define sqlite3_result_error_code sqlite3_api->result_error_code
112502 #define sqlite3_test_control sqlite3_api->test_control
112503 #define sqlite3_randomness sqlite3_api->randomness
112504 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
112505 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
112506 #define sqlite3_limit sqlite3_api->limit
112507 #define sqlite3_next_stmt sqlite3_api->next_stmt
112508 #define sqlite3_sql sqlite3_api->sql
112509 #define sqlite3_status sqlite3_api->status
112510 #define sqlite3_backup_finish sqlite3_api->backup_finish
112511 #define sqlite3_backup_init sqlite3_api->backup_init
112512 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
112513 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
112514 #define sqlite3_backup_step sqlite3_api->backup_step
112515 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
112516 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
112517 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
112518 #define sqlite3_db_config sqlite3_api->db_config
112519 #define sqlite3_db_mutex sqlite3_api->db_mutex
112520 #define sqlite3_db_status sqlite3_api->db_status
112521 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
112522 #define sqlite3_log sqlite3_api->log
112523 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
112524 #define sqlite3_sourceid sqlite3_api->sourceid
112525 #define sqlite3_stmt_status sqlite3_api->stmt_status
112526 #define sqlite3_strnicmp sqlite3_api->strnicmp
112527 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
112528 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
112529 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
112530 #define sqlite3_wal_hook sqlite3_api->wal_hook
112531 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
112532 #define sqlite3_vtab_config sqlite3_api->vtab_config
112533 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
112535 #define sqlite3_close_v2 sqlite3_api->close_v2
112536 #define sqlite3_db_filename sqlite3_api->db_filename
112537 #define sqlite3_db_readonly sqlite3_api->db_readonly
112538 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
112539 #define sqlite3_errstr sqlite3_api->errstr
112540 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
112541 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
112542 #define sqlite3_stricmp sqlite3_api->stricmp
112543 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
112544 #define sqlite3_uri_int64 sqlite3_api->uri_int64
112545 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
112546 #define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
112547 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
112549 #define sqlite3_auto_extension sqlite3_api->auto_extension
112550 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
112551 #define sqlite3_bind_text64 sqlite3_api->bind_text64
112552 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
112553 #define sqlite3_load_extension sqlite3_api->load_extension
112554 #define sqlite3_malloc64 sqlite3_api->malloc64
112555 #define sqlite3_msize sqlite3_api->msize
112556 #define sqlite3_realloc64 sqlite3_api->realloc64
112557 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
112558 #define sqlite3_result_blob64 sqlite3_api->result_blob64
112559 #define sqlite3_result_text64 sqlite3_api->result_text64
112560 #define sqlite3_strglob sqlite3_api->strglob
112562 #define sqlite3_value_dup sqlite3_api->value_dup
112563 #define sqlite3_value_free sqlite3_api->value_free
112564 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
112565 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
112567 #define sqlite3_value_subtype sqlite3_api->value_subtype
112568 #define sqlite3_result_subtype sqlite3_api->result_subtype
112570 #define sqlite3_status64 sqlite3_api->status64
112571 #define sqlite3_strlike sqlite3_api->strlike
112572 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
112574 #define sqlite3_system_errno sqlite3_api->system_errno
112576 #define sqlite3_trace_v2 sqlite3_api->trace_v2
112577 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
112579 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
112581 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
112582 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
112583 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
112584 #define sqlite3_result_pointer sqlite3_api->result_pointer
112585 #define sqlite3_value_pointer sqlite3_api->value_pointer
112598 # define SQLITE_EXTENSION_INIT1 /*no-op*/
112600 # define SQLITE_EXTENSION_INIT3 /*no-op*/
113041 sqlite3_vfs *pVfs = db->pVfs;
113073 if( (db->flags & SQLITE_LoadExtension)==0 ){
113097 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
113124 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
113143 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
113162 /* Append the new shared library handle to the db->aExtension array. */
113163 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
113167 if( db->nExtension>0 ){
113168 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
113170 sqlite3DbFree(db, db->aExtension);
113171 db->aExtension = aHandle;
113173 db->aExtension[db->nExtension++] = handle;
113183 sqlite3_mutex_enter(db->mutex);
113186 sqlite3_mutex_leave(db->mutex);
113196 assert( sqlite3_mutex_held(db->mutex) );
113197 for(i=0; i<db->nExtension; i++){
113198 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
113200 sqlite3DbFree(db, db->aExtension);
113208 sqlite3_mutex_enter(db->mutex);
113210 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
113212 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
113214 sqlite3_mutex_leave(db->mutex);
113235 ** we have to locate the state vector at run-time. In the more common
113295 ** routine is a no-op.
113310 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
113312 wsdAutoext.nExt--;
113432 #define PragTyp_CACHE_SPILL 5
113479 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
113483 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
113485 /* Names of columns for pragmas that return multi-column result
113486 ** or that return single-column results where the name of the
113495 /* 5 */ "dflt_value",
113544 /* Definitions of all built-in pragmas */
113780 /* ColNames: */ 21, 5,
113978 /* ColNames: */ 7, 5,
114088 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
114132 ** Interpret the given string as an auto-vacuum mode value.
114150 ** backed temporary databases, 2 for the Red-Black tree in memory database
114151 ** and 0 to use the compile-time default.
114155 return z[0] - '0';
114172 sqlite3 *db = pParse->db;
114173 if( db->aDb[1].pBt!=0 ){
114174 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
114179 sqlite3BtreeClose(db->aDb[1].pBt);
114180 db->aDb[1].pBt = 0;
114195 sqlite3 *db = pParse->db;
114196 if( db->temp_store==ts ) return SQLITE_OK;
114200 db->temp_store = (u8)ts;
114212 u8 n = pPragma->nPragCName;
114215 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
114218 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
114252 if( db->autoCommit ){
114253 Db *pDb = db->aDb;
114254 int n = db->nDb;
114260 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
114261 while( (n--) > 0 ){
114262 if( pDb->pBt ){
114263 sqlite3BtreeSetPagerFlags(pDb->pBt,
114264 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
114271 # define setAllPagerFlags(X) /* no-op */
114276 ** Return a human-readable name for a constraint resolution action.
114297 ** journal-mode name.
114311 assert( PAGER_JOURNALMODE_WAL==5 );
114324 upr = ArraySize(aPragmaName)-1;
114330 upr = mid - 1;
114341 ** Generate code to output a single-column result row with the result
114374 int minusFlag /* True if a '-' sign preceded <value> */
114376 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
114377 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
114383 sqlite3 *db = pParse->db; /* The database connection */
114390 pParse->nMem = 2;
114396 pDb = &db->aDb[iDb];
114408 zRight = sqlite3MPrintf(db, "-%T", pValue);
114414 zDb = pId2->n>0 ? pDb->zDbSName : 0;
114419 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
114421 ** handled the pragma and generate a no-op prepared statement.
114423 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
114428 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
114438 db->busyHandler.nBusy = 0;
114452 pParse->nErr++;
114453 pParse->rc = rc;
114462 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
114467 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
114468 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
114474 switch( pPragma->ePragTyp ){
114509 pParse->nMem += 2;
114521 pDb->pSchema->cache_size = size;
114522 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
114539 Btree *pBt = pDb->pBt;
114545 /* Malloc may fail when setting the page-size, as there is an internal
114548 db->nextPagesize = sqlite3Atoi(zRight);
114549 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
114565 Btree *pBt = pDb->pBt;
114566 int b = -1;
114575 if( pId2->n==0 && b>=0 ){
114577 for(ii=0; ii<db->nDb; ii++){
114578 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
114606 iReg = ++pParse->nMem;
114625 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
114628 ** the locking-mode of the main database).
114630 eMode = db->dfltLockMode;
114633 if( pId2->n==0 ){
114635 ** of the PRAGMA command. In this case the locking-mode must be
114643 assert(pDb==&db->aDb[0]);
114644 for(ii=2; ii<db->nDb; ii++){
114645 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
114648 db->dfltLockMode = (u8)eMode;
114650 pPager = sqlite3BtreePager(pDb->pBt);
114688 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
114691 pId2->n = 1;
114693 for(ii=db->nDb-1; ii>=0; ii--){
114694 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
114710 Pager *pPager = sqlite3BtreePager(pDb->pBt);
114711 i64 iLimit = -2;
114714 if( iLimit<-1 ) iLimit = -1;
114727 ** Get or set the value of the database 'auto-vacuum' parameter.
114732 Btree *pBt = pDb->pBt;
114739 db->nextAutovac = (u8)eAuto;
114741 ** incr-vacuum flags. This is required in case this connection
114743 ** as an auto-vacuum capable db.
114750 ** that this really is an auto-vacuum capable database.
114769 aOp[4].p3 = eAuto - 1;
114792 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
114808 ** number of pages is adjusted so that the cache uses -N kibibytes
114814 returnSingleInt(v, pDb->pSchema->cache_size);
114817 pDb->pSchema->cache_size = size;
114818 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
114835 ** number of pages is adjusted so that the cache uses -N kibibytes
114849 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
114850 sqlite3BtreeSetSpillSize(pDb->pBt,0));
114854 sqlite3BtreeSetSpillSize(pDb->pBt, size);
114857 db->flags |= SQLITE_CacheSpill;
114859 db->flags &= ~SQLITE_CacheSpill;
114888 if( pId2->n==0 ) db->szMmap = sz;
114889 for(ii=db->nDb-1; ii>=0; ii--){
114890 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
114891 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
114895 sz = -1;
114904 pParse->nErr++;
114905 pParse->rc = rc;
114918 ** Note that it is possible for the library compile-time options to
114923 returnSingleInt(v, db->temp_store);
114947 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
114954 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
114955 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
114991 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
115020 Pager *pPager = sqlite3BtreePager(pDb->pBt);
115027 Pager *pPager = sqlite3BtreePager(pDb->pBt);
115057 returnSingleInt(v, pDb->safety_level-1);
115059 if( !db->autoCommit ){
115065 pDb->safety_level = iLevel;
115066 pDb->bSyncSet = 1;
115078 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
115080 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
115081 if( db->autoCommit==0 ){
115083 ** in auto-commit mode. */
115087 if( db->auth.authLevel==UAUTH_User ){
115088 /* Do not allow non-admin users to modify the schema arbitrarily */
115094 db->flags |= mask;
115096 db->flags &= ~mask;
115097 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
115100 /* Many of the flag-pragmas modify the code generated by the SQL
115132 pParse->nMem = 6;
115135 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
115140 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
115145 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
115147 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
115149 i-nHidden,
115150 pCol->zName,
115152 pCol->notNull ? 1 : 0,
115153 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
115164 pParse->nMem = 5;
115166 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
115169 pTab->zName,
115171 pTab->szTabRow,
115172 pTab->nRowLogEst,
115173 pTab->tabFlags);
115174 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115176 pIdx->zName,
115177 pIdx->szIdxRow,
115178 pIdx->aiRowLogEst[0],
115179 pIdx->hasStat1);
115180 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
115194 if( pPragma->iArg ){
115196 mx = pIdx->nColumn;
115197 pParse->nMem = 6;
115200 mx = pIdx->nKeyCol;
115201 pParse->nMem = 3;
115203 pTab = pIdx->pTable;
115205 assert( pParse->nMem<=pPragma->nPragCName );
115207 i16 cnum = pIdx->aiColumn[i];
115209 cnum<0 ? 0 : pTab->aCol[cnum].zName);
115210 if( pPragma->iArg ){
115212 pIdx->aSortOrder[i],
115213 pIdx->azColl[i],
115214 i<pIdx->nKeyCol);
115216 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
115228 pParse->nMem = 5;
115230 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
115234 pIdx->zName,
115236 azOrigin[pIdx->idxType],
115237 pIdx->pPartIdxWhere!=0);
115245 pParse->nMem = 3;
115246 for(i=0; i<db->nDb; i++){
115247 if( db->aDb[i].pBt==0 ) continue;
115248 assert( db->aDb[i].zDbSName!=0 );
115251 db->aDb[i].zDbSName,
115252 sqlite3BtreeGetFilename(db->aDb[i].pBt));
115260 pParse->nMem = 2;
115261 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
115263 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
115273 pParse->nMem = 2;
115275 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
115276 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
115280 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
115282 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
115291 pParse->nMem = 1;
115292 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
115294 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
115319 pFK = pTab->pFKey;
115322 pParse->nMem = 8;
115326 for(j=0; j<pFK->nCol; j++){
115330 pFK->zTo,
115331 pTab->aCol[pFK->aCol[j].iFrom].zName,
115332 pFK->aCol[j].zCol,
115333 actionName(pFK->aAction[1]), /* ON UPDATE */
115334 actionName(pFK->aAction[0]), /* ON DELETE */
115338 pFK = pFK->pNextFrom;
115364 regResult = pParse->nMem+1;
115365 pParse->nMem += 4;
115366 regKey = ++pParse->nMem;
115367 regRow = ++pParse->nMem;
115369 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
115378 if( pTab==0 || pTab->pFKey==0 ) continue;
115379 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
115380 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
115382 sqlite3VdbeLoadString(v, regResult, pTab->zName);
115383 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
115384 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
115387 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
115393 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
115401 assert( pParse->nErr>0 || pFK==0 );
115403 if( pParse->nTab<i ) pParse->nTab = i;
115405 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
115406 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
115419 for(j=0; j<pFK->nCol; j++){
115420 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
115428 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
115429 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
115436 assert( pFK->nCol==1 );
115445 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
115495 ** without the overhead of cross-checking indexes. Quick_check
115510 ** to -1 here, to indicate that the VDBE should verify the integrity
115513 assert( iDb==0 || pId2->z );
115514 if( pId2->z==0 ) iDb = -1;
115517 pParse->nMem = 6;
115527 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
115530 for(i=0; i<db->nDb; i++){
115543 /* Do an integrity check of the B-Tree
115549 pTbls = &db->aDb[i].pSchema->tblHash;
115554 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
115562 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
115563 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115564 aRoot[cnt++] = pIdx->tnum;
115570 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
115573 /* Do the b-tree integrity checks */
115578 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
115593 int r1 = -1;
115595 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
115596 if( pTab->pCheck==0
115597 && (pTab->tabFlags & TF_HasNotNull)==0
115598 && (pTab->pIndex==0 || isQuick)
115607 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
115610 assert( pParse->nMem>=8+j );
115615 for(j=0; j<pTab->nCol; j++){
115618 if( j==pTab->iPKey ) continue;
115619 if( pTab->aCol[j].notNull==0 ) continue;
115623 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
115624 pTab->aCol[j].zName);
115630 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
115631 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
115632 if( db->mallocFailed==0 ){
115637 pParse->iSelfTab = iDataCur + 1;
115639 for(k=pCheck->nExpr-1; k>0; k--){
115640 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
115642 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
115645 pParse->iSelfTab = 0;
115647 pTab->zName);
115656 for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
115666 pIdx->nColumn); VdbeCoverage(v);
115671 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
115682 for(kk=0; kk<pIdx->nKeyCol; kk++){
115683 int iCol = pIdx->aiColumn[kk];
115684 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
115685 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
115693 pIdx->nKeyCol); VdbeCoverage(v);
115694 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
115702 sqlite3VdbeJumpHere(v, loopTop-1);
115706 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
115711 sqlite3VdbeLoadString(v, 3, pIdx->zName);
115732 aOp[0].p2 = 1-mxErr;
115744 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
115749 ** The second form of this pragma is a no-op if the main database file
115770 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
115771 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
115772 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
115775 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
115785 returnSingleText(v, encnames[ENC(pParse->db)].zName);
115796 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
115797 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
115799 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
115803 if( !pEnc->zName ){
115828 ** the value of the schema-version and user-version, respectively. Both
115829 ** the schema-version and the user-version are 32-bit signed integers
115832 ** The schema-cookie is usually only manipulated internally by SQLite. It
115839 ** the schema-version is potentially dangerous and may lead to program
115842 ** The user-version is not used internally by SQLite. It may be used by
115846 int iCookie = pPragma->iArg; /* Which cookie to read or write */
115848 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
115886 ** Return the names of all compile-time options used in this build,
115892 pParse->nMem = 1;
115909 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
115920 pParse->nMem = 3;
115939 db->xWalCallback==sqlite3WalDefaultHook ?
115940 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
115948 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
115990 ** ever added that should be off by default, those off-by-default
116000 ** (2) The query planner used sqlite_stat1-style statistics for one or
116028 iTabCur = pParse->nTab++;
116029 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
116032 pSchema = db->aDb[iDb].pSchema;
116033 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
116039 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
116042 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
116043 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116044 if( !pIdx->hasStat1 ){
116056 db->aDb[iDb].zDbSName, pTab->zName);
116080 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
116084 returnSingleInt(v, db->busyTimeout);
116092 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
116094 ** specified and is a non-negative integer.
116095 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
116097 ** sqlite3_soft_heap_limit64(-1) C-language function.
116104 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
116123 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
116136 pParse->nMem = 2;
116137 for(i=0; i<db->nDb; i++){
116141 if( db->aDb[i].zDbSName==0 ) continue;
116142 pBt = db->aDb[i].pBt;
116145 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
116149 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
116185 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
116190 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
116200 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
116205 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
116256 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
116261 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
116266 if( pPragma->mPragFlg & PragFlg_Result1 ){
116270 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
116276 assert( strlen(zBuf) < sizeof(zBuf)-1 );
116284 pTab->pName = pPragma;
116285 pTab->db = db;
116286 pTab->iHidden = i;
116287 pTab->nHidden = j;
116319 pIdxInfo->estimatedCost = (double)1;
116320 if( pTab->nHidden==0 ){ return SQLITE_OK; }
116321 pConstraint = pIdxInfo->aConstraint;
116324 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
116325 if( pConstraint->usable==0 ) continue;
116326 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
116327 if( pConstraint->iColumn < pTab->iHidden ) continue;
116328 j = pConstraint->iColumn - pTab->iHidden;
116333 pIdxInfo->estimatedCost = (double)2147483647;
116334 pIdxInfo->estimatedRows = 2147483647;
116337 j = seen[0]-1;
116338 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
116339 pIdxInfo->aConstraintUsage[j].omit = 1;
116341 pIdxInfo->estimatedCost = (double)20;
116342 pIdxInfo->estimatedRows = 20;
116343 j = seen[1]-1;
116344 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
116345 pIdxInfo->aConstraintUsage[j].omit = 1;
116355 pCsr->base.pVtab = pVtab;
116356 *ppCursor = &pCsr->base;
116363 sqlite3_finalize(pCsr->pPragma);
116364 pCsr->pPragma = 0;
116365 for(i=0; i<ArraySize(pCsr->azArg); i++){
116366 sqlite3_free(pCsr->azArg[i]);
116367 pCsr->azArg[i] = 0;
116385 pCsr->iRowid++;
116386 assert( pCsr->pPragma );
116387 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
116388 rc = sqlite3_finalize(pCsr->pPragma);
116389 pCsr->pPragma = 0;
116404 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
116413 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
116416 assert( j<ArraySize(pCsr->azArg) );
116417 assert( pCsr->azArg[j]==0 );
116419 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
116420 if( pCsr->azArg[j]==0 ){
116425 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
116427 if( pCsr->azArg[1] ){
116428 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
116430 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
116431 if( pCsr->azArg[0] ){
116432 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
116436 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
116439 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
116450 return (pCsr->pPragma==0);
116462 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
116463 if( i<pTab->iHidden ){
116464 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
116466 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
116476 *p = pCsr->iRowid;
116483 0, /* xCreate - create a table */
116484 pragmaVtabConnect, /* xConnect - connect to an existing table */
116485 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
116486 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
116487 0, /* xDestroy - Drop a table */
116488 pragmaVtabOpen, /* xOpen - open a cursor */
116489 pragmaVtabClose, /* xClose - close a cursor */
116490 pragmaVtabFilter, /* xFilter - configure scan constraints */
116491 pragmaVtabNext, /* xNext - advance a cursor */
116493 pragmaVtabColumn, /* xColumn - read data */
116494 pragmaVtabRowid, /* xRowid - read data */
116495 0, /* xUpdate - write data */
116496 0, /* xBegin - begin transaction */
116497 0, /* xSync - sync transaction */
116498 0, /* xCommit - commit transaction */
116499 0, /* xRollback - rollback transaction */
116500 0, /* xFindFunction - function overloading */
116501 0, /* xRename - rename the table */
116517 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
116518 assert( sqlite3HashFind(&db->aModule, zName)==0 );
116554 sqlite3 *db = pData->db;
116555 if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
116559 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
116560 sqlite3DbFree(db, *pData->pzErrMsg);
116561 *pData->pzErrMsg = z;
116563 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
116580 sqlite3 *db = pData->db;
116581 int iDb = pData->iDb;
116585 assert( sqlite3_mutex_held(db->mutex) );
116587 if( db->mallocFailed ){
116592 assert( iDb>=0 && iDb<db->nDb );
116598 ** But because db->init.busy is set to 1, no VDBE code is generated
116603 u8 saved_iDb = db->init.iDb;
116607 assert( db->init.busy );
116608 db->init.iDb = iDb;
116609 db->init.newTnum = sqlite3Atoi(argv[1]);
116610 db->init.orphanTrigger = 0;
116611 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
116612 rc = db->errCode;
116614 db->init.iDb = saved_iDb;
116615 assert( saved_iDb==0 || (db->flags & SQLITE_Vacuum)!=0 );
116617 if( db->init.orphanTrigger ){
116620 pData->rc = rc;
116639 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
116647 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
116670 int meta[5];
116675 assert( iDb>=0 && iDb<db->nDb );
116676 assert( db->aDb[iDb].pSchema );
116677 assert( sqlite3_mutex_held(db->mutex) );
116678 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
116680 /* Construct the in-memory representation schema tables (sqlite_master or
116684 ** the schema table as read-only. */
116702 pDb = &db->aDb[iDb];
116703 if( pDb->pBt==0 ){
116710 /* If there is not already a read-only (or read-write) transaction opened
116711 ** on the b-tree database, open one now. If a transaction is opened, it
116713 sqlite3BtreeEnter(pDb->pBt);
116714 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
116715 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
116730 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
116731 ** meta[5] User version
116741 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
116743 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
116745 /* If opening a non-empty database, check the text encoding. For the
116750 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
116755 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
116763 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
116773 pDb->pSchema->enc = ENC(db);
116775 if( pDb->pSchema->cache_size==0 ){
116777 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
116779 pDb->pSchema->cache_size = size;
116781 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
116783 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
116789 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
116792 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
116793 if( pDb->pSchema->file_format==0 ){
116794 pDb->pSchema->file_format = 1;
116796 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
116807 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
116808 db->flags &= ~SQLITE_LegacyFileFmt;
116813 assert( db->init.busy );
116818 db->aDb[iDb].zDbSName, zMasterName);
116822 xAuth = db->xAuth;
116823 db->xAuth = 0;
116827 db->xAuth = xAuth;
116838 if( db->mallocFailed ){
116842 if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
116861 sqlite3BtreeCommit(pDb->pBt);
116863 sqlite3BtreeLeave(pDb->pBt);
116873 ** Initialize all database files - the main database file, the file
116880 ** file was of zero-length, then the DB_Empty flag is also set.
116884 int commit_internal = !(db->flags&SQLITE_InternChanges);
116886 assert( sqlite3_mutex_held(db->mutex) );
116887 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
116888 assert( db->init.busy==0 );
116890 db->init.busy = 1;
116892 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
116905 assert( db->nDb>1 );
116914 db->init.busy = 0;
116923 ** This routine is a no-op if the database schema is already initialized.
116928 sqlite3 *db = pParse->db;
116929 assert( sqlite3_mutex_held(db->mutex) );
116930 if( !db->init.busy ){
116931 rc = sqlite3Init(db, &pParse->zErrMsg);
116934 pParse->rc = rc;
116935 pParse->nErr++;
116943 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
116944 ** make no changes to pParse->rc.
116947 sqlite3 *db = pParse->db;
116952 assert( pParse->checkSchema );
116953 assert( sqlite3_mutex_held(db->mutex) );
116954 for(iDb=0; iDb<db->nDb; iDb++){
116956 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
116959 /* If there is not already a read-only (or read-write) transaction opened
116960 ** on the b-tree database, open one now. If a transaction is opened, it
116961 ** will be closed immediately after reading the meta-value. */
116972 ** value stored as part of the in-memory schema representation,
116976 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
116978 pParse->rc = SQLITE_SCHEMA;
116990 ** which database file in db->aDb[] the schema refers to.
116996 int i = -1000000;
116998 /* If pSchema is NULL, then return -1000000. This happens when code in
117000 ** created by a sub-select). In this case the return value of this
117003 ** We return -1000000 instead of the more usual -1 simply because using
117004 ** -1000000 as the incorrect index into db->aDb[] is much
117005 ** more likely to cause a segfault than -1 (of course there are assert()
117008 assert( sqlite3_mutex_held(db->mutex) );
117010 for(i=0; ALWAYS(i<db->nDb); i++){
117011 if( db->aDb[i].pSchema==pSchema ){
117015 assert( i>=0 && i<db->nDb );
117025 sqlite3 *db = pParse->db;
117026 sqlite3DbFree(db, pParse->aLabel);
117027 sqlite3ExprListDelete(db, pParse->pConstExpr);
117029 assert( db->lookaside.bDisable >= pParse->disableLookaside );
117030 db->lookaside.bDisable -= pParse->disableLookaside;
117032 pParse->disableLookaside = 0;
117037 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
117041 const char *zSql, /* UTF-8 encoded SQL statement. */
117057 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
117058 assert( sqlite3_mutex_held(db->mutex) );
117060 /* For a long-term use prepared statement avoid the use of
117065 db->lookaside.bDisable++;
117070 ** some other database connection is holding a write-lock, which in
117091 for(i=0; i<db->nDb; i++) {
117092 Btree *pBt = db->aDb[i].pBt;
117097 const char *zDb = db->aDb[i].zDbSName;
117099 testcase( db->flags & SQLITE_ReadUncommit );
117108 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
117110 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
117121 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
117135 if( db->mallocFailed ){
117160 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
117166 if( db->init.busy==0 ){
117167 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
117169 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
117186 sParse.pTriggerPrg = pT->pNext;
117194 assert( (rc&db->errMask)==rc );
117199 const char *zSql, /* UTF-8 encoded SQL statement. */
117215 sqlite3_mutex_enter(db->mutex);
117223 sqlite3_mutex_leave(db->mutex);
117243 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
117247 assert( sqlite3_mutex_held(db->mutex) );
117249 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
117277 const char *zSql, /* UTF-8 encoded SQL statement. */
117289 const char *zSql, /* UTF-8 encoded SQL statement. */
117295 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
117299 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
117307 const char *zSql, /* UTF-8 encoded SQL statement. */
117314 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
117331 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
117335 const void *zSql, /* UTF-16 encoded SQL statement. */
117341 /* This function currently works by first transforming the UTF-16
117342 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
117362 sqlite3_mutex_enter(db->mutex);
117365 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
117370 ** equivalent pointer into the UTF-16 string by counting the unicode
117372 ** the same number of characters into the UTF-16 string.
117374 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
117379 sqlite3_mutex_leave(db->mutex);
117393 const void *zSql, /* UTF-16 encoded SQL statement. */
117405 const void *zSql, /* UTF-16 encoded SQL statement. */
117417 const void *zSql, /* UTF-16 encoded SQL statement. */
117458 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
117459 (S)->zSelName,(S)),\
117488 int regReturn; /* Register holding block-output return address */
117489 int labelBkOut; /* Start label for the block-output subroutine */
117503 Select *pPrior = p->pPrior;
117504 sqlite3ExprListDelete(db, p->pEList);
117505 sqlite3SrcListDelete(db, p->pSrc);
117506 sqlite3ExprDelete(db, p->pWhere);
117507 sqlite3ExprListDelete(db, p->pGroupBy);
117508 sqlite3ExprDelete(db, p->pHaving);
117509 sqlite3ExprListDelete(db, p->pOrderBy);
117510 sqlite3ExprDelete(db, p->pLimit);
117511 sqlite3ExprDelete(db, p->pOffset);
117512 if( p->pWith ) sqlite3WithDelete(db, p->pWith);
117523 pDest->eDest = (u8)eDest;
117524 pDest->iSDParm = iParm;
117525 pDest->zAffSdst = 0;
117526 pDest->iSdst = 0;
117527 pDest->nSdst = 0;
117538 SrcList *pSrc, /* the FROM clause -- which tables to scan */
117549 pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
117551 assert( pParse->db->mallocFailed );
117555 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(pParse->db,TK_ASTERISK,0));
117557 pNew->pEList = pEList;
117558 pNew->op = TK_SELECT;
117559 pNew->selFlags = selFlags;
117560 pNew->iLimit = 0;
117561 pNew->iOffset = 0;
117563 pNew->zSelName[0] = 0;
117565 pNew->addrOpenEphm[0] = -1;
117566 pNew->addrOpenEphm[1] = -1;
117567 pNew->nSelectRow = 0;
117568 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
117569 pNew->pSrc = pSrc;
117570 pNew->pWhere = pWhere;
117571 pNew->pGroupBy = pGroupBy;
117572 pNew->pHaving = pHaving;
117573 pNew->pOrderBy = pOrderBy;
117574 pNew->pPrior = 0;
117575 pNew->pNext = 0;
117576 pNew->pLimit = pLimit;
117577 pNew->pOffset = pOffset;
117578 pNew->pWith = 0;
117579 assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || pParse->db->mallocFailed!=0 );
117580 if( pParse->db->mallocFailed ) {
117581 clearSelect(pParse->db, pNew, pNew!=&standin);
117584 assert( pNew->pSrc!=0 || pParse->nErr>0 );
117596 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
117610 ** Return a pointer to the right-most SELECT statement in a compound.
117613 while( p->pNext ) p = p->pNext;
117647 /* outer */ { 10, 5, JT_OUTER },
117648 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
117650 /* inner */ { 23, 5, JT_INNER },
117651 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
117660 if( p->n==aKeyword[j].nChar
117661 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
117666 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
117692 ** Return the index of a column in a table. Return -1 if the column
117697 for(i=0; i<pTab->nCol; i++){
117698 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
117700 return -1;
117714 int N, /* Number of tables in pSrc->a[] to search */
117716 int *piTab, /* Write index of pSrc->a[] here */
117717 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
117724 iCol = columnIndex(pSrc->a[i].pTab, zCol);
117757 sqlite3 *db = pParse->db;
117763 assert( pSrc->nSrc>iRight );
117764 assert( pSrc->a[iLeft].pTab );
117765 assert( pSrc->a[iRight].pTab );
117775 pEq->iRightJoinTable = (i16)pE2->iTable;
117797 ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
117799 ** The where clause needs to defer the handling of the t1.x=5
117801 ** NULL t2 row will be inserted whenever t1.x!=5. If we do not
117802 ** defer the handling of t1.x=5, it will be processed immediately
117803 ** after the t1 loop and rows with t1.x!=5 will never appear in
117811 p->iRightJoinTable = (i16)iTable;
117812 if( p->op==TK_FUNCTION && p->x.pList ){
117814 for(i=0; i<p->x.pList->nExpr; i++){
117815 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
117818 setJoinExpr(p->pLeft, iTable);
117819 p = p->pRight;
117829 ** The left most table is the first entry in Select.pSrc. The right-most
117843 pSrc = p->pSrc;
117844 pLeft = &pSrc->a[0];
117846 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
117847 Table *pLeftTab = pLeft->pTab;
117848 Table *pRightTab = pRight->pTab;
117852 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
117857 if( pRight->fg.jointype & JT_NATURAL ){
117858 if( pRight->pOn || pRight->pUsing ){
117863 for(j=0; j<pRightTab->nCol; j++){
117868 zName = pRightTab->aCol[j].zName;
117871 isOuter, &p->pWhere);
117878 if( pRight->pOn && pRight->pUsing ){
117887 if( pRight->pOn ){
117888 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
117889 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
117890 pRight->pOn = 0;
117900 if( pRight->pUsing ){
117901 IdList *pList = pRight->pUsing;
117902 for(j=0; j<pList->nId; j++){
117908 zName = pList->a[j].zName;
117913 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
117918 isOuter, &p->pWhere);
117935 ** through regData+nData-1 onto the sorter.
117946 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
117947 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
117948 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
117951 int regRecord = ++pParse->nMem; /* Assembled sorter record */
117952 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
117960 regBase = regData - nExpr - bSeq;
117962 regBase = pParse->nMem + 1;
117963 pParse->nMem += nBase;
117965 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
117966 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
117967 pSort->labelDone = sqlite3VdbeMakeLabel(v);
117968 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
117971 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
117976 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
117985 regPrevKey = pParse->nMem+1;
117986 pParse->nMem += pSort->nOBSat;
117987 nKey = nExpr - pSort->nOBSat + bSeq;
117991 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
117994 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
117995 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
117996 if( pParse->db->mallocFailed ) return;
117997 pOp->p2 = nKey + nData;
117998 pKI = pOp->p4.pKeyInfo;
117999 memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
118000 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
118001 testcase( pKI->nXField>2 );
118002 pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
118003 pKI->nXField-1);
118006 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
118007 pSort->regReturn = ++pParse->nMem;
118008 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
118009 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
118011 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
118015 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
118018 if( pSort->sortFlags & SORTFLAG_UseSorter ){
118023 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
118024 regBase+nOBSat, nBase-nOBSat);
118033 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
118034 if( pSort->bOrderedInnerLoop ){
118035 r1 = ++pParse->nMem;
118036 sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
118039 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
118040 if( pSort->bOrderedInnerLoop ){
118088 v = pParse->pVdbe;
118117 Vdbe *v = pParse->pVdbe;
118120 int eDest = pDest->eDest; /* How to dispose of results */
118121 int iParm = pDest->iSDParm; /* First argument to disposal method */
118128 ** values for any expressions that are also part of the sort-key are omitted
118135 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
118136 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
118139 codeOffset(v, p->iOffset, iContinue);
118144 nResultCol = pEList->nExpr;
118146 if( pDest->iSdst==0 ){
118148 nPrefixReg = pSort->pOrderBy->nExpr;
118149 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
118150 pParse->nMem += nPrefixReg;
118152 pDest->iSdst = pParse->nMem+1;
118153 pParse->nMem += nResultCol;
118154 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
118156 ** on the right-hand side of an INSERT contains more result columns than
118160 pParse->nMem += nResultCol;
118162 pDest->nSdst = nResultCol;
118163 regOrig = regResult = pDest->iSdst;
118167 VdbeComment((v, "%s", pEList->a[i].zName));
118181 ** the ORDER BY clause (pSort->pOrderBy), set the associated
118183 ** expression within the sort-key that pushOntoSorter() will generate.
118187 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
118189 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
118190 pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
118205 switch( pDistinct->eTnctType ){
118212 regPrev = pParse->nMem+1;
118213 pParse->nMem += nResultCol;
118221 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
118222 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
118223 pOp->opcode = OP_Null;
118224 pOp->p1 = 1;
118225 pOp->p2 = regPrev;
118229 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
118230 if( i<nResultCol-1 ){
118237 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
118240 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
118241 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
118246 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
118251 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
118252 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
118258 codeOffset(v, p->iOffset, iContinue);
118340 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
118342 r1, pDest->zAffSdst, nResultCol);
118364 assert( nResultCol<=pDest->nSdst );
118368 assert( nResultCol==pDest->nSdst );
118376 case SRT_Coroutine: /* Send data to a co-routine */
118384 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
118394 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
118395 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
118396 ** pSO->nExpr columns, then make sure all keys are unique by adding a
118405 pSO = pDest->pOrderBy;
118407 nKey = pSO->nExpr;
118426 regResult + pSO->a[i].u.x.iOrderByCol - 1,
118444 ** user-defined functions that have side effects. We do not care
118458 if( pSort==0 && p->iLimit ){
118459 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
118468 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
118471 p->aSortOrder = (u8*)&p->aColl[N+X];
118472 p->nField = (u16)N;
118473 p->nXField = (u16)X;
118474 p->enc = ENC(db);
118475 p->db = db;
118476 p->nRef = 1;
118489 assert( p->nRef>0 );
118490 p->nRef--;
118491 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
118500 assert( p->nRef>0 );
118501 p->nRef++;
118513 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
118539 sqlite3 *db = pParse->db;
118542 nExpr = pList->nExpr;
118543 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
118546 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
118548 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
118549 if( !pColl ) pColl = db->pDfltColl;
118550 pInfo->aColl[i-iStart] = pColl;
118551 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
118574 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
118577 ** "USE TEMP B-TREE FOR xxx"
118583 if( pParse->explain==2 ){
118584 Vdbe *v = pParse->pVdbe;
118585 char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
118586 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
118591 ** Assign expression b to lvalue a. A second, no-op, version of this macro
118600 /* No-op versions of the explainXXX() functions and macros. */
118608 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
118612 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
118628 if( pParse->explain==2 ){
118629 Vdbe *v = pParse->pVdbe;
118631 pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
118632 bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
118634 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
118638 /* No-op versions of the explainXXX() functions and macros. */
118643 ** If the inner loop was generated using a non-null pOrderBy argument,
118655 Vdbe *v = pParse->pVdbe; /* The prepared statement */
118656 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
118661 ExprList *pOrderBy = pSort->pOrderBy;
118662 int eDest = pDest->eDest;
118663 int iParm = pDest->iSDParm;
118672 struct ExprList_item *aOutEx = p->pEList->a;
118675 if( pSort->labelBkOut ){
118676 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
118678 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
118680 iTab = pSort->iECursor;
118683 regRow = pDest->iSdst;
118690 nKey = pOrderBy->nExpr - pSort->nOBSat;
118691 if( pSort->sortFlags & SORTFLAG_UseSorter ){
118692 int regSortOut = ++pParse->nMem;
118693 iSortTab = pParse->nTab++;
118694 if( pSort->labelBkOut ){
118701 codeOffset(v, p->iOffset, addrContinue);
118706 codeOffset(v, p->iOffset, addrContinue);
118713 iRead = aOutEx[i].u.x.iOrderByCol-1;
118730 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
118732 pDest->zAffSdst, nColumn);
118747 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
118748 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
118750 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
118766 if( pSort->sortFlags & SORTFLAG_UseSorter ){
118771 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
118786 ** result-set expression in all of the following SELECT statements is
118797 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
118824 assert( pNC->pSrcList!=0 );
118825 switch( pExpr->op ){
118834 int iCol = pExpr->iColumn; /* Index of column in pTab */
118835 testcase( pExpr->op==TK_AGG_COLUMN );
118836 testcase( pExpr->op==TK_COLUMN );
118838 SrcList *pTabList = pNC->pSrcList;
118839 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
118840 if( j<pTabList->nSrc ){
118841 pTab = pTabList->a[j].pTab;
118842 pS = pTabList->a[j].pSelect;
118844 pNC = pNC->pNext;
118859 ** sub-select. In this case, set the column type to NULL, even
118869 assert( pTab && pExpr->pTab==pTab );
118871 /* The "table" is actually a sub-select or a view in the FROM clause
118873 ** data for the result-set column of the sub-select.
118875 if( iCol>=0 && iCol<pS->pEList->nExpr ){
118877 ** rowid of the sub-select or view. This expression is legal (see
118878 ** test case misc2.2.2) - it always evaluates to NULL.
118881 Expr *p = pS->pEList->a[iCol].pExpr;
118882 sNC.pSrcList = pS->pSrc;
118884 sNC.pParse = pNC->pParse;
118887 }else if( pTab->pSchema ){
118890 if( iCol<0 ) iCol = pTab->iPKey;
118891 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
118897 zOrigCol = pTab->aCol[iCol].zName;
118898 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
118899 estWidth = pTab->aCol[iCol].szEst;
118901 zOrigTab = pTab->zName;
118902 if( pNC->pParse ){
118903 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
118904 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
118910 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
118911 estWidth = pTab->aCol[iCol].szEst;
118919 /* The expression is a sub-select. Return the declaration type and
118924 Select *pS = pExpr->x.pSelect;
118925 Expr *p = pS->pEList->a[0].pExpr;
118927 sNC.pSrcList = pS->pSrc;
118929 sNC.pParse = pNC->pParse;
118958 Vdbe *v = pParse->pVdbe;
118964 for(i=0; i<pEList->nExpr; i++){
118965 Expr *p = pEList->a[i].pExpr;
118973 /* The vdbe must make its own copy of the column-type and other
119023 Vdbe *v = pParse->pVdbe;
119028 sqlite3 *db = pParse->db;
119034 if( pParse->explain ){
119039 if( pParse->colNamesSet || db->mallocFailed ) return;
119040 /* Column names are determined by the left-most term of a compound select */
119041 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
119042 pTabList = pSelect->pSrc;
119043 pEList = pSelect->pEList;
119046 pParse->colNamesSet = 1;
119047 fullName = (db->flags & SQLITE_FullColNames)!=0;
119048 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
119049 sqlite3VdbeSetNumCols(v, pEList->nExpr);
119050 for(i=0; i<pEList->nExpr; i++){
119051 Expr *p = pEList->a[i].pExpr;
119054 if( pEList->a[i].zName ){
119056 char *zName = pEList->a[i].zName;
119058 }else if( srcName && p->op==TK_COLUMN ){
119060 int iCol = p->iColumn;
119061 pTab = p->pTab;
119063 if( iCol<0 ) iCol = pTab->iPKey;
119064 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
119068 zCol = pTab->aCol[iCol].zName;
119072 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
119078 const char *z = pEList->a[i].zSpan;
119114 sqlite3 *db = pParse->db; /* Database connection */
119125 nCol = pEList->nExpr;
119136 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
119139 if( (zName = pEList->a[i].zName)!=0 ){
119142 Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
119143 while( pColExpr->op==TK_DOT ){
119144 pColExpr = pColExpr->pRight;
119147 if( pColExpr->op==TK_COLUMN && pColExpr->pTab!=0 ){
119149 int iCol = pColExpr->iColumn;
119150 Table *pTab = pColExpr->pTab;
119151 if( iCol<0 ) iCol = pTab->iPKey;
119152 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
119153 }else if( pColExpr->op==TK_ID ){
119155 zName = pColExpr->u.zToken;
119158 zName = pEList->a[i].zSpan;
119174 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
119180 pCol->zName = zName;
119187 if( db->mallocFailed ){
119215 sqlite3 *db = pParse->db;
119225 assert( (pSelect->selFlags & SF_Resolved)!=0 );
119226 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
119227 if( db->mallocFailed ) return;
119229 sNC.pSrcList = pSelect->pSrc;
119230 a = pSelect->pEList->a;
119231 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
119235 zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst);
119236 szAll += pCol->szEst;
119237 pCol->affinity = sqlite3ExprAffinity(p);
119239 n = sqlite3Strlen30(pCol->zName);
119240 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
119241 if( pCol->zName ){
119242 memcpy(&pCol->zName[n+1], zType, m+1);
119243 pCol->colFlags |= COLFLAG_HASTYPE;
119246 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
119248 if( pColl && pCol->zColl==0 ){
119249 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
119252 pTab->szTabRow = sqlite3LogEst(szAll*4);
119261 sqlite3 *db = pParse->db;
119264 savedFlags = db->flags;
119265 db->flags &= ~SQLITE_FullColNames;
119266 db->flags |= SQLITE_ShortColNames;
119268 if( pParse->nErr ) return 0;
119269 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
119270 db->flags = savedFlags;
119277 assert( db->lookaside.bDisable );
119278 pTab->nTabRef = 1;
119279 pTab->zName = 0;
119280 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
119281 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
119283 pTab->iPKey = -1;
119284 if( db->mallocFailed ){
119296 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
119298 if( pParse->pToplevel==0
119299 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
119301 pParse->okConstFactor = 1;
119306 Vdbe *v = pParse->pVdbe;
119339 if( p->iLimit ) return;
119342 ** "LIMIT -1" always shows all rows. There is some
119348 assert( p->pOffset==0 || p->pLimit!=0 );
119349 if( p->pLimit ){
119350 p->iLimit = iLimit = ++pParse->nMem;
119353 if( sqlite3ExprIsInteger(p->pLimit, &n) ){
119358 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
119359 p->nSelectRow = sqlite3LogEst((u64)n);
119360 p->selFlags |= SF_FixedLimit;
119363 sqlite3ExprCode(pParse, p->pLimit, iLimit);
119368 if( p->pOffset ){
119369 p->iOffset = iOffset = ++pParse->nMem;
119370 pParse->nMem++; /* Allocate an extra register for limit+offset */
119371 sqlite3ExprCode(pParse, p->pOffset, iOffset);
119382 ** Return the appropriate collating sequence for the iCol-th column of
119383 ** the result set for the compound-select statement "p". Return NULL if
119387 ** left-most term of the select that has a collating sequence.
119391 if( p->pPrior ){
119392 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
119397 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
119400 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
119401 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
119416 ExprList *pOrderBy = p->pOrderBy;
119417 int nOrderBy = p->pOrderBy->nExpr;
119418 sqlite3 *db = pParse->db;
119423 struct ExprList_item *pItem = &pOrderBy->a[i];
119424 Expr *pTerm = pItem->pExpr;
119427 if( pTerm->flags & EP_Collate ){
119430 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
119431 if( pColl==0 ) pColl = db->pDfltColl;
119432 pOrderBy->a[i].pExpr =
119433 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
119436 pRet->aColl[i] = pColl;
119437 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
119449 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
119451 ** p->pPrior p
119454 ** There is exactly one reference to the recursive-table in the FROM clause
119455 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
119457 ** The setup-query runs once to generate an initial set of rows that go
119461 ** recursive-table for a recursive-query run. The output of the recursive-query
119486 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
119487 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
119488 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
119489 Select *pSetup = p->pPrior; /* The setup query */
119509 p->nSelectRow = 320; /* 4 billion rows */
119511 pLimit = p->pLimit;
119512 pOffset = p->pOffset;
119513 regLimit = p->iLimit;
119514 regOffset = p->iOffset;
119515 p->pLimit = p->pOffset = 0;
119516 p->iLimit = p->iOffset = 0;
119517 pOrderBy = p->pOrderBy;
119520 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
119521 if( pSrc->a[i].fg.isRecursive ){
119522 iCurrent = pSrc->a[i].iCursor;
119530 iQueue = pParse->nTab++;
119531 if( p->op==TK_UNION ){
119533 iDistinct = pParse->nTab++;
119540 regCurrent = ++pParse->nMem;
119544 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
119552 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
119553 p->selFlags |= SF_UsesEphemeral;
119557 p->pOrderBy = 0;
119559 /* Store the results of the setup-query in Queue. */
119560 pSetup->pNext = 0;
119562 pSetup->pNext = p;
119571 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
119580 selectInnerLoop(pParse, p, p->pEList, iCurrent,
119589 ** the value for the recursive-table. Store the results in the Queue.
119591 if( p->selFlags & SF_Aggregate ){
119594 p->pPrior = 0;
119596 assert( p->pPrior==0 );
119597 p->pPrior = pSetup;
119605 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
119606 p->pOrderBy = pOrderBy;
119607 p->pLimit = pLimit;
119608 p->pOffset = pOffset;
119616 Select *p, /* The right-most of SELECTs to be coded */
119621 ** Handle the special case of a compound-select that originates from a
119633 Select *p, /* The right-most of SELECTs to be coded */
119639 assert( p->selFlags & SF_MultiValue );
119641 assert( p->selFlags & SF_Values );
119642 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
119643 assert( p->pLimit==0 );
119644 assert( p->pOffset==0 );
119645 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
119646 if( p->pPrior==0 ) break;
119647 assert( p->pPrior->pNext==p );
119648 p = p->pPrior;
119652 pPrior = p->pPrior;
119653 p->pPrior = 0;
119655 p->pPrior = pPrior;
119657 p->nSelectRow = nRow;
119658 p = p->pNext;
119668 ** "p" points to the right-most of the two queries. the query on the
119669 ** left is p->pPrior. The left query could also be a compound query
119675 ** Example 1: Consider a three-way compound SQL statement.
119683 ** `-----> SELECT b FROM t2
119685 ** `------> SELECT a FROM t1
119689 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
119696 Select *p, /* The right-most of SELECTs to be coded */
119706 int iSub1 = 0; /* EQP id of left-hand query */
119707 int iSub2 = 0; /* EQP id of right-hand query */
119711 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
119713 assert( p && p->pPrior ); /* Calling function guarantees this much */
119714 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
119715 db = pParse->db;
119716 pPrior = p->pPrior;
119718 if( pPrior->pOrderBy ){
119720 selectOpName(p->op));
119724 if( pPrior->pLimit ){
119726 selectOpName(p->op));
119737 assert( p->pEList );
119738 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
119742 /* Special handling for a compound-select that originates as a VALUES clause.
119744 if( p->selFlags & SF_MultiValue ){
119752 assert( p->pEList && pPrior->pEList );
119753 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
119756 if( p->selFlags & SF_Recursive ){
119763 if( p->pOrderBy ){
119769 switch( p->op ){
119773 assert( !pPrior->pLimit );
119774 pPrior->iLimit = p->iLimit;
119775 pPrior->iOffset = p->iOffset;
119776 pPrior->pLimit = p->pLimit;
119777 pPrior->pOffset = p->pOffset;
119778 explainSetInteger(iSub1, pParse->iNextSelectId);
119780 p->pLimit = 0;
119781 p->pOffset = 0;
119785 p->pPrior = 0;
119786 p->iLimit = pPrior->iLimit;
119787 p->iOffset = pPrior->iOffset;
119788 if( p->iLimit ){
119789 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
119791 if( p->iOffset ){
119793 p->iLimit, p->iOffset+1, p->iOffset);
119796 explainSetInteger(iSub2, pParse->iNextSelectId);
119799 pDelete = p->pPrior;
119800 p->pPrior = pPrior;
119801 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
119802 if( pPrior->pLimit
119803 && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
119804 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
119806 p->nSelectRow = sqlite3LogEst((u64)nLimit);
119818 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
119822 testcase( p->op==TK_EXCEPT );
119823 testcase( p->op==TK_UNION );
119829 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
119830 assert( p->pOffset==0 ); /* Not allowed on leftward elements */
119836 unionTab = pParse->nTab++;
119837 assert( p->pOrderBy==0 );
119839 assert( p->addrOpenEphm[0] == -1 );
119840 p->addrOpenEphm[0] = addr;
119841 findRightmost(p)->selFlags |= SF_UsesEphemeral;
119842 assert( p->pEList );
119847 assert( !pPrior->pOrderBy );
119849 explainSetInteger(iSub1, pParse->iNextSelectId);
119857 if( p->op==TK_EXCEPT ){
119860 assert( p->op==TK_UNION );
119863 p->pPrior = 0;
119864 pLimit = p->pLimit;
119865 p->pLimit = 0;
119866 pOffset = p->pOffset;
119867 p->pOffset = 0;
119869 explainSetInteger(iSub2, pParse->iNextSelectId);
119872 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
119873 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
119874 sqlite3ExprListDelete(db, p->pOrderBy);
119875 pDelete = p->pPrior;
119876 p->pPrior = pPrior;
119877 p->pOrderBy = 0;
119878 if( p->op==TK_UNION ){
119879 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
119881 sqlite3ExprDelete(db, p->pLimit);
119882 p->pLimit = pLimit;
119883 p->pOffset = pOffset;
119884 p->iLimit = 0;
119885 p->iOffset = 0;
119893 assert( p->pEList );
119899 selectInnerLoop(pParse, p, p->pEList, unionTab,
119908 default: assert( p->op==TK_INTERSECT ); {
119920 tab1 = pParse->nTab++;
119921 tab2 = pParse->nTab++;
119922 assert( p->pOrderBy==0 );
119925 assert( p->addrOpenEphm[0] == -1 );
119926 p->addrOpenEphm[0] = addr;
119927 findRightmost(p)->selFlags |= SF_UsesEphemeral;
119928 assert( p->pEList );
119933 explainSetInteger(iSub1, pParse->iNextSelectId);
119942 assert( p->addrOpenEphm[1] == -1 );
119943 p->addrOpenEphm[1] = addr;
119944 p->pPrior = 0;
119945 pLimit = p->pLimit;
119946 p->pLimit = 0;
119947 pOffset = p->pOffset;
119948 p->pOffset = 0;
119950 explainSetInteger(iSub2, pParse->iNextSelectId);
119953 pDelete = p->pPrior;
119954 p->pPrior = pPrior;
119955 if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
119956 sqlite3ExprDelete(db, p->pLimit);
119957 p->pLimit = pLimit;
119958 p->pOffset = pOffset;
119963 assert( p->pEList );
119972 selectInnerLoop(pParse, p, p->pEList, tab1,
119983 explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
119989 ** This section is run by the right-most SELECT statement only.
119990 ** SELECT statements to the left always skip this part. The right-most
119994 if( p->selFlags & SF_UsesEphemeral ){
119998 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
120001 assert( p->pNext==0 );
120002 nCol = p->pEList->nExpr;
120008 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
120011 *apColl = db->pDfltColl;
120015 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
120017 int addr = pLoop->addrOpenEphm[i];
120021 assert( pLoop->addrOpenEphm[1]<0 );
120027 pLoop->addrOpenEphm[i] = -1;
120034 pDest->iSdst = dest.iSdst;
120035 pDest->nSdst = dest.nSdst;
120046 if( p->selFlags & SF_Values ){
120050 " do not have the same number of result columns", selectOpName(p->op));
120058 ** The data to be output is contained in pIn->iSdst. There are
120059 ** pIn->nSdst columns to be output. pDest is where the output should
120071 ** If the LIMIT found in p->iLimit is reached, jump immediately to
120084 Vdbe *v = pParse->pVdbe;
120096 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
120100 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
120103 if( pParse->db->mallocFailed ) return 0;
120107 codeOffset(v, p->iOffset, iContinue);
120109 assert( pDest->eDest!=SRT_Exists );
120110 assert( pDest->eDest!=SRT_Table );
120111 switch( pDest->eDest ){
120117 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
120118 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
120119 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
120131 testcase( pIn->nSdst>1 );
120133 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
120134 r1, pDest->zAffSdst, pIn->nSdst);
120135 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
120136 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
120137 pIn->iSdst, pIn->nSdst);
120147 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
120148 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
120155 ** starting at pDest->iSdst. Then the co-routine yields.
120158 if( pDest->iSdst==0 ){
120159 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
120160 pDest->nSdst = pIn->nSdst;
120162 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
120163 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
120176 assert( pDest->eDest==SRT_Output );
120177 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
120178 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
120185 if( p->iLimit ){
120186 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
120207 ** co-routines. Then run the co-routines in parallel and merge the results
120234 ** ------------- ----------------- -------------- -----------------
120285 Select *p, /* The right-most of SELECTs to be coded */
120293 int regAddrA; /* Address register for select-A coroutine */
120294 int regAddrB; /* Address register for select-B coroutine */
120295 int addrSelectA; /* Address of the select-A coroutine */
120296 int addrSelectB; /* Address of the select-B coroutine */
120297 int regOutA; /* Address register for the output-A subroutine */
120298 int regOutB; /* Address register for the output-B subroutine */
120299 int addrOutA; /* Address of the output-A subroutine */
120300 int addrOutB = 0; /* Address of the output-B subroutine */
120301 int addrEofA; /* Address of the select-A-exhausted subroutine */
120303 int addrEofB; /* Address of the select-B-exhausted subroutine */
120307 int regLimitA; /* Limit register for select-A */
120308 int regLimitB; /* Limit register for select-A */
120310 int savedLimit; /* Saved value of p->iLimit */
120311 int savedOffset; /* Saved value of p->iOffset */
120323 int iSub1; /* EQP id of left-hand query */
120324 int iSub2; /* EQP id of right-hand query */
120327 assert( p->pOrderBy!=0 );
120329 db = pParse->db;
120330 v = pParse->pVdbe;
120338 op = p->op;
120339 pPrior = p->pPrior;
120340 assert( pPrior->pOrderBy==0 );
120341 pOrderBy = p->pOrderBy;
120343 nOrderBy = pOrderBy->nExpr;
120350 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
120352 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
120353 assert( pItem->u.x.iOrderByCol>0 );
120354 if( pItem->u.x.iOrderByCol==i ) break;
120359 pNew->flags |= EP_IntValue;
120360 pNew->u.iValue = i;
120361 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
120362 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
120378 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
120379 assert( pItem->u.x.iOrderByCol>0 );
120380 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
120381 aPermute[i] = pItem->u.x.iOrderByCol - 1;
120390 p->pOrderBy = pOrderBy;
120391 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
120400 int nExpr = p->pEList->nExpr;
120401 assert( nOrderBy>=nExpr || db->mallocFailed );
120402 regPrev = pParse->nMem+1;
120403 pParse->nMem += nExpr+1;
120409 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
120410 pKeyDup->aSortOrder[i] = 0;
120417 p->pPrior = 0;
120418 pPrior->pNext = 0;
120419 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
120420 if( pPrior->pPrior==0 ){
120421 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
120426 if( p->iLimit && op==TK_ALL ){
120427 regLimitA = ++pParse->nMem;
120428 regLimitB = ++pParse->nMem;
120429 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
120435 sqlite3ExprDelete(db, p->pLimit);
120436 p->pLimit = 0;
120437 sqlite3ExprDelete(db, p->pOffset);
120438 p->pOffset = 0;
120440 regAddrA = ++pParse->nMem;
120441 regAddrB = ++pParse->nMem;
120442 regOutA = ++pParse->nMem;
120443 regOutB = ++pParse->nMem;
120448 ** left of the compound operator - the "A" select.
120453 pPrior->iLimit = regLimitA;
120454 explainSetInteger(iSub1, pParse->iNextSelectId);
120460 ** the right - the "B" select
120465 savedLimit = p->iLimit;
120466 savedOffset = p->iOffset;
120467 p->iLimit = regLimitB;
120468 p->iOffset = 0;
120469 explainSetInteger(iSub2, pParse->iNextSelectId);
120471 p->iLimit = savedLimit;
120472 p->iOffset = savedOffset;
120500 VdbeNoopComment((v, "eof-A subroutine"));
120505 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120513 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
120515 VdbeNoopComment((v, "eof-B subroutine"));
120523 VdbeNoopComment((v, "A-lt-B subroutine"));
120536 VdbeNoopComment((v, "A-eq-B subroutine"));
120544 VdbeNoopComment((v, "A-gt-B subroutine"));
120573 if( p->pPrior ){
120574 sqlite3SelectDelete(db, p->pPrior);
120576 p->pPrior = pPrior;
120577 pPrior->pNext = p;
120581 explainComposite(pParse, p->op, iSub1, iSub2, 0);
120582 return pParse->nErr!=0;
120608 ** a column in table number iTable with a copy of the iColumn-th
120624 if( ExprHasProperty(pExpr, EP_FromJoin) && pExpr->iRightJoinTable==pSubst->iTable ){
120625 pExpr->iRightJoinTable = pSubst->iNewTable;
120627 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
120628 if( pExpr->iColumn<0 ){
120629 pExpr->op = TK_NULL;
120632 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
120634 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
120635 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
120637 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
120639 sqlite3 *db = pSubst->pParse->db;
120640 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
120644 ifNullRow.iTable = pSubst->iNewTable;
120648 if( pNew && pSubst->isLeftJoin ){
120652 pNew->iRightJoinTable = pExpr->iRightJoinTable;
120660 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
120661 pExpr->iTable = pSubst->iNewTable;
120663 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
120664 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
120666 substSelect(pSubst, pExpr->x.pSelect, 1);
120668 substExprList(pSubst, pExpr->x.pList);
120679 for(i=0; i<pList->nExpr; i++){
120680 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
120686 int doPrior /* Do substitutes on p->pPrior too */
120693 substExprList(pSubst, p->pEList);
120694 substExprList(pSubst, p->pGroupBy);
120695 substExprList(pSubst, p->pOrderBy);
120696 p->pHaving = substExpr(pSubst, p->pHaving);
120697 p->pWhere = substExpr(pSubst, p->pWhere);
120698 pSrc = p->pSrc;
120700 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
120701 substSelect(pSubst, pItem->pSelect, 1);
120702 if( pItem->fg.isTabFunc ){
120703 substExprList(pSubst, pItem->u1.pFuncArg);
120706 }while( doPrior && (p = p->pPrior)!=0 );
120718 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
120730 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
120743 ** FROM-clause subquery that is a candidate for flattening. (2b is
120744 ** due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
120753 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
120754 ** sub-queries that were excluded from this optimization. Restriction
120770 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
120771 ** accidently carried the comment forward until 2014-09-15. Original
120792 ** (17) The sub-query is not a compound select, or it is a UNION ALL
120793 ** compound clause made up entirely of non-aggregate queries, and
120800 ** The parent and sub-query may contain WHERE clauses. Subject to
120807 ** Also, each component of the sub-query must return the same number
120810 ** such (illegal) sub-query is flattened. The caller will detect the
120813 ** (18) If the sub-query is a compound select, then all terms of the
120815 ** columns of the sub-query.
120820 ** (20) If the sub-query is a compound select, then it must not use
120831 ** (23) The parent is not a recursive CTE, or the sub-query is not a
120836 ** (24) The subquery is not an aggregate that uses the built-in min() or
120843 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
120846 ** If flattening is not attempted, this routine is a no-op and returns 0.
120855 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
120859 const char *zSavedAuthContext = pParse->zAuthContext;
120862 Select *pSub1; /* Pointer to the rightmost select in sub-query */
120866 int iNewParent = -1;/* Replacement table for iParent */
120871 sqlite3 *db = pParse->db;
120876 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
120878 pSrc = p->pSrc;
120879 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
120880 pSubitem = &pSrc->a[iFrom];
120881 iParent = pSubitem->iCursor;
120882 pSub = pSubitem->pSelect;
120886 if( pSrc->nSrc>1 ) return 0; /* Restriction (2a) */
120887 if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
120888 || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
120889 || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
120895 pSubSrc = pSub->pSrc;
120899 ** because they could be computed at compile-time. But when LIMIT and OFFSET
120902 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
120903 if( pSub->pOffset ) return 0; /* Restriction (14) */
120904 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
120907 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
120908 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
120909 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
120912 if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
120915 if( p->pOrderBy && pSub->pOrderBy ){
120918 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
120919 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
120920 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
120923 testcase( pSub->selFlags & SF_Recursive );
120924 testcase( pSub->selFlags & SF_MinMaxAgg );
120925 if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
120928 if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
120946 ** are processed - there is no mechanism to determine if the LEFT JOIN
120947 ** table should be all-NULL.
120951 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
120953 if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
120959 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
120961 ** they are not necessary. This will stress-test the OP_IfNullRow opcode. */
120962 isLeftJoin = -1;
120966 /* Restriction 17: If the sub-query is a compound SELECT, then it must
120971 if( pSub->pPrior ){
120972 if( pSub->pOrderBy ){
120975 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
120978 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
120979 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
120980 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
120981 assert( pSub->pSrc!=0 );
120982 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
120983 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
120984 || (pSub1->pPrior && pSub1->op!=TK_ALL)
120985 || pSub1->pSrc->nSrc<1
120989 testcase( pSub1->pSrc->nSrc>1 );
120993 if( p->pOrderBy ){
120995 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
120996 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
121003 pSub->zSelName, pSub, iFrom));
121006 pParse->zAuthContext = pSubitem->zName;
121009 pParse->zAuthContext = zSavedAuthContext;
121011 /* If the sub-query is a compound SELECT statement, then (by restrictions
121015 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
121018 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
121019 ** OFFSET clauses and joins them to the left-hand-side of the original
121021 ** select statements in the compound sub-query.
121031 ** ) WHERE a!=5 ORDER BY 1
121035 ** SELECT x+1 FROM tab WHERE x+1!=5
121037 ** SELECT y+1 FROM tab WHERE y+1!=5
121039 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
121042 ** We call this the "compound-subquery flattening".
121044 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
121046 ExprList *pOrderBy = p->pOrderBy;
121047 Expr *pLimit = p->pLimit;
121048 Expr *pOffset = p->pOffset;
121049 Select *pPrior = p->pPrior;
121050 p->pOrderBy = 0;
121051 p->pSrc = 0;
121052 p->pPrior = 0;
121053 p->pLimit = 0;
121054 p->pOffset = 0;
121056 sqlite3SelectSetName(pNew, pSub->zSelName);
121057 p->pOffset = pOffset;
121058 p->pLimit = pLimit;
121059 p->pOrderBy = pOrderBy;
121060 p->pSrc = pSrc;
121061 p->op = TK_ALL;
121063 p->pPrior = pPrior;
121065 pNew->pPrior = pPrior;
121066 if( pPrior ) pPrior->pNext = pNew;
121067 pNew->pNext = p;
121068 p->pPrior = pNew;
121070 ("compound-subquery flattener creates %s.%p as peer\n",
121071 pNew->zSelName, pNew));
121073 if( db->mallocFailed ) return 1;
121076 /* Begin flattening the iFrom-th entry of the FROM clause
121079 pSub = pSub1 = pSubitem->pSelect;
121084 sqlite3DbFree(db, pSubitem->zDatabase);
121085 sqlite3DbFree(db, pSubitem->zName);
121086 sqlite3DbFree(db, pSubitem->zAlias);
121087 pSubitem->zDatabase = 0;
121088 pSubitem->zName = 0;
121089 pSubitem->zAlias = 0;
121090 pSubitem->pSelect = 0;
121097 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
121099 if( ALWAYS(pSubitem->pTab!=0) ){
121100 Table *pTabToDel = pSubitem->pTab;
121101 if( pTabToDel->nTabRef==1 ){
121103 pTabToDel->pNextZombie = pToplevel->pZombieTab;
121104 pToplevel->pZombieTab = pTabToDel;
121106 pTabToDel->nTabRef--;
121108 pSubitem->pTab = 0;
121111 /* The following loop runs once for each term in a compound-subquery
121113 ** of flattening - a flattening other than a compound-subquery flattening -
121124 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
121127 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
121128 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
121129 pSrc = pParent->pSrc; /* FROM clause of the outer query */
121133 jointype = pSubitem->fg.jointype;
121136 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
121138 assert( db->mallocFailed );
121159 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
121160 if( db->mallocFailed ){
121169 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
121170 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
121171 pSrc->a[i+iFrom] = pSubSrc->a[i];
121172 iNewParent = pSubSrc->a[i].iCursor;
121173 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
121175 pSrc->a[iFrom].fg.jointype = jointype;
121182 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
121189 if( pSub->pOrderBy ){
121190 /* At this point, any non-zero iOrderByCol values indicate that the
121197 ** function attempts to flatten a compound sub-query into pParent
121198 ** (the only way this can happen is if the compound sub-query is
121199 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
121200 ExprList *pOrderBy = pSub->pOrderBy;
121201 for(i=0; i<pOrderBy->nExpr; i++){
121202 pOrderBy->a[i].u.x.iOrderByCol = 0;
121204 assert( pParent->pOrderBy==0 );
121205 assert( pSub->pPrior==0 );
121206 pParent->pOrderBy = pOrderBy;
121207 pSub->pOrderBy = 0;
121209 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
121214 assert( pParent->pHaving==0 );
121215 pParent->pHaving = pParent->pWhere;
121216 pParent->pWhere = pWhere;
121217 pParent->pHaving = sqlite3ExprAnd(db,
121218 sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
121220 assert( pParent->pGroupBy==0 );
121221 pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
121223 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
121225 if( db->mallocFailed==0 ){
121231 x.pEList = pSub->pEList;
121238 pParent->selFlags |= pSub->selFlags & SF_Distinct;
121246 if( pSub->pLimit ){
121247 pParent->pLimit = pSub->pLimit;
121248 pSub->pLimit = 0;
121275 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
121279 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
121280 ** WHERE x=5 AND y=10;
121288 ** to copy the outer WHERE-clause terms onto the HAVING clause of the
121300 ** (5) The WHERE clause expression originates in the ON or USING clause
121303 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
121316 for(pX=pSubq; pX; pX=pX->pPrior){
121317 if( (pX->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
121318 testcase( pX->selFlags & SF_Aggregate );
121319 testcase( pX->selFlags & SF_Recursive );
121324 if( pSubq->pLimit!=0 ){
121327 while( pWhere->op==TK_AND ){
121328 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
121329 pWhere = pWhere->pLeft;
121331 if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */
121336 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
121341 x.pEList = pSubq->pEList;
121343 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
121344 pSubq = pSubq->pPrior;
121370 if( pAggInfo->nFunc==1 ){
121371 Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
121372 ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */
121374 assert( pExpr->op==TK_AGG_FUNCTION );
121375 if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
121376 const char *zFunc = pExpr->u.zToken;
121387 assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
121393 ** The second argument is the associated aggregate-info object. This
121398 ** where table is a database table, not a sub-select or view. If the query
121406 assert( !p->pGroupBy );
121408 if( p->pWhere || p->pEList->nExpr!=1
121409 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
121413 pTab = p->pSrc->a[0].pTab;
121414 pExpr = p->pEList->a[0].pExpr;
121415 assert( pTab && !pTab->pSelect && pExpr );
121418 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
121419 if( NEVER(pAggInfo->nFunc==0) ) return 0;
121420 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
121421 if( pExpr->flags&EP_Distinct ) return 0;
121427 ** If the source-list item passed as an argument was augmented with an
121431 ** pFrom->pIndex and return SQLITE_OK.
121434 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
121435 Table *pTab = pFrom->pTab;
121436 char *zIndexedBy = pFrom->u1.zIndexedBy;
121438 for(pIdx=pTab->pIndex;
121439 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
121440 pIdx=pIdx->pNext
121444 pParse->checkSchema = 1;
121447 pFrom->pIBIndex = pIdx;
121482 if( p->pPrior==0 ) return WRC_Continue;
121483 if( p->pOrderBy==0 ) return WRC_Continue;
121484 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
121486 a = p->pOrderBy->a;
121487 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
121488 if( a[i].pExpr->flags & EP_Collate ) break;
121494 pParse = pWalker->pParse;
121495 db = pParse->db;
121502 p->pSrc = pNewSrc;
121503 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
121504 p->op = TK_SELECT;
121505 p->pWhere = 0;
121506 pNew->pGroupBy = 0;
121507 pNew->pHaving = 0;
121508 pNew->pOrderBy = 0;
121509 p->pPrior = 0;
121510 p->pNext = 0;
121511 p->pWith = 0;
121512 p->selFlags &= ~SF_Compound;
121513 assert( (p->selFlags & SF_Converted)==0 );
121514 p->selFlags |= SF_Converted;
121515 assert( pNew->pPrior!=0 );
121516 pNew->pPrior->pNext = pNew;
121517 pNew->pLimit = 0;
121518 pNew->pOffset = 0;
121523 ** Check to see if the FROM clause term pFrom has table-valued function
121525 ** non-zero, since pFrom is not allowed to be a table-valued function.
121528 if( pFrom->fg.isTabFunc ){
121529 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
121539 ** FROM clause element pItem is really a common-table-expression (CTE)
121543 ** If a non-NULL value is returned, set *ppContext to point to the With
121552 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
121554 for(p=pWith; p; p=p->pOuter){
121556 for(i=0; i<p->nCte; i++){
121557 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
121559 return &p->a[i];
121568 ** with the inner-most WITH clause being at the top of the stack.
121578 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
121580 assert( pParse->pWith!=pWith );
121581 pWith->pOuter = pParse->pWith;
121582 pParse->pWith = pWith;
121583 if( bFree ) pParse->pWithToFree = pWith;
121593 ** If pFrom falls into either of the two categories above, pFrom->pTab
121595 ** (pFrom->pTab!=0) to determine whether or not a successful match
121606 Parse *pParse = pWalker->pParse;
121607 sqlite3 *db = pParse->db;
121611 assert( pFrom->pTab==0 );
121613 pCte = searchWith(pParse->pWith, pFrom, &pWith);
121618 Select *pLeft; /* Left-most SELECT statement */
121620 With *pSavedWith; /* Initial value of pParse->pWith */
121622 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
121624 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
121626 if( pCte->zCteErr ){
121627 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
121632 assert( pFrom->pTab==0 );
121633 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
121635 pTab->nTabRef = 1;
121636 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
121637 pTab->iPKey = -1;
121638 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
121639 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
121640 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
121641 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
121642 assert( pFrom->pSelect );
121645 pSel = pFrom->pSelect;
121646 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
121649 SrcList *pSrc = pFrom->pSelect->pSrc;
121650 for(i=0; i<pSrc->nSrc; i++){
121651 struct SrcList_item *pItem = &pSrc->a[i];
121652 if( pItem->zDatabase==0
121653 && pItem->zName!=0
121654 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
121656 pItem->pTab = pTab;
121657 pItem->fg.isRecursive = 1;
121658 pTab->nTabRef++;
121659 pSel->selFlags |= SF_Recursive;
121665 if( pTab->nTabRef>2 ){
121667 pParse, "multiple references to recursive table: %s", pCte->zName
121671 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
121673 pCte->zCteErr = "circular reference: %s";
121674 pSavedWith = pParse->pWith;
121675 pParse->pWith = pWith;
121677 Select *pPrior = pSel->pPrior;
121678 assert( pPrior->pWith==0 );
121679 pPrior->pWith = pSel->pWith;
121681 pPrior->pWith = 0;
121685 pParse->pWith = pWith;
121687 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
121688 pEList = pLeft->pEList;
121689 if( pCte->pCols ){
121690 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
121692 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
121694 pParse->pWith = pSavedWith;
121697 pEList = pCte->pCols;
121700 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
121702 if( pSel->selFlags & SF_Recursive ){
121703 pCte->zCteErr = "multiple recursive references: %s";
121705 pCte->zCteErr = "recursive reference in a subquery: %s";
121709 pCte->zCteErr = 0;
121710 pParse->pWith = pSavedWith;
121727 Parse *pParse = pWalker->pParse;
121728 if( pParse->pWith && p->pPrior==0 ){
121729 With *pWith = findRightmost(p)->pWith;
121731 assert( pParse->pWith==pWith );
121732 pParse->pWith = pWith->pOuter;
121747 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
121749 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
121765 Parse *pParse = pWalker->pParse;
121770 sqlite3 *db = pParse->db;
121772 u16 selFlags = p->selFlags;
121774 p->selFlags |= SF_Expanded;
121775 if( db->mallocFailed ){
121778 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
121781 pTabList = p->pSrc;
121782 pEList = p->pEList;
121783 if( p->pWith ){
121784 sqlite3WithPush(pParse, p->pWith, 0);
121796 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
121798 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
121799 if( pFrom->fg.isRecursive ) continue;
121800 assert( pFrom->pTab==0 );
121803 if( pFrom->pTab ) {} else
121805 if( pFrom->zName==0 ){
121807 Select *pSel = pFrom->pSelect;
121808 /* A sub-query in the FROM clause of a SELECT */
121810 assert( pFrom->pTab==0 );
121812 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
121814 pTab->nTabRef = 1;
121815 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
121816 while( pSel->pPrior ){ pSel = pSel->pPrior; }
121817 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
121818 pTab->iPKey = -1;
121819 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
121820 pTab->tabFlags |= TF_Ephemeral;
121824 assert( pFrom->pTab==0 );
121825 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
121827 if( pTab->nTabRef>=0xffff ){
121829 pTab->zName);
121830 pFrom->pTab = 0;
121833 pTab->nTabRef++;
121838 if( IsVirtual(pTab) || pTab->pSelect ){
121841 assert( pFrom->pSelect==0 );
121842 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
121843 sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
121844 nCol = pTab->nCol;
121845 pTab->nCol = -1;
121846 sqlite3WalkSelect(pWalker, pFrom->pSelect);
121847 pTab->nCol = nCol;
121860 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
121875 for(k=0; k<pEList->nExpr; k++){
121876 pE = pEList->a[k].pExpr;
121877 if( pE->op==TK_ASTERISK ) break;
121878 assert( pE->op!=TK_DOT || pE->pRight!=0 );
121879 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
121880 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
121882 if( k<pEList->nExpr ){
121888 struct ExprList_item *a = pEList->a;
121890 int flags = pParse->db->flags;
121894 for(k=0; k<pEList->nExpr; k++){
121896 pRight = pE->pRight;
121897 assert( pE->op!=TK_DOT || pRight!=0 );
121898 if( pE->op!=TK_ASTERISK
121899 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
121905 pNew->a[pNew->nExpr-1].zName = a[k].zName;
121906 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
121916 if( pE->op==TK_DOT ){
121917 assert( pE->pLeft!=0 );
121918 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
121919 zTName = pE->pLeft->u.zToken;
121921 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
121922 Table *pTab = pFrom->pTab;
121923 Select *pSub = pFrom->pSelect;
121924 char *zTabName = pFrom->zAlias;
121928 zTabName = pTab->zName;
121930 if( db->mallocFailed ) break;
121931 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
121936 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121937 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
121939 for(j=0; j<pTab->nCol; j++){
121940 char *zName = pTab->aCol[j].zName;
121947 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
121953 ** result-set list unless the SELECT has the SF_IncludeHidden
121956 if( (p->selFlags & SF_IncludeHidden)==0
121957 && IsHiddenColumn(&pTab->aCol[j])
121964 if( (pFrom->fg.jointype & JT_NATURAL)!=0
121971 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
121980 if( longNames || pTabList->nSrc>1 ){
121998 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
121999 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
122001 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
122002 testcase( pX->zSpan==0 );
122004 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
122006 testcase( pX->zSpan==0 );
122008 pX->bSpanIsTab = 1;
122023 p->pEList = pNew;
122026 if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
122035 ** No-op routine for the parse-tree walker.
122049 ** No-op routine for the parse-tree walker for SELECT statements.
122077 ** The calling function can detect the problem by looking at pParse->nErr
122078 ** and/or pParse->db->mallocFailed.
122084 if( pParse->hasCompound ){
122100 ** For each FROM-clause subquery, add Column.zType and Column.zColl
122115 assert( p->selFlags & SF_Resolved );
122116 assert( (p->selFlags & SF_HasTypeInfo)==0 );
122117 p->selFlags |= SF_HasTypeInfo;
122118 pParse = pWalker->pParse;
122119 pTabList = p->pSrc;
122120 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
122121 Table *pTab = pFrom->pTab;
122123 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
122124 /* A sub-query in the FROM clause of a SELECT */
122125 Select *pSel = pFrom->pSelect;
122127 while( pSel->pPrior ) pSel = pSel->pPrior;
122138 ** the Table structures of all FROM-clause subqueries in a
122159 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
122160 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
122174 db = pParse->db;
122175 if( db->mallocFailed ) return;
122176 if( p->selFlags & SF_HasTypeInfo ) return;
122178 if( pParse->nErr || db->mallocFailed ) return;
122180 if( pParse->nErr || db->mallocFailed ) return;
122193 Vdbe *v = pParse->pVdbe;
122196 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
122201 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
122202 for(i=0; i<pAggInfo->nColumn; i++){
122203 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
122204 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
122206 for(i=0; i<pAggInfo->nFunc; i++){
122207 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
122208 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
122211 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
122212 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
122213 if( pFunc->iDistinct>=0 ){
122214 Expr *pE = pFunc->pExpr;
122216 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
122219 pFunc->iDistinct = -1;
122221 KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
122222 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
122234 Vdbe *v = pParse->pVdbe;
122237 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
122238 ExprList *pList = pF->pExpr->x.pList;
122239 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
122240 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
122241 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
122250 Vdbe *v = pParse->pVdbe;
122257 pAggInfo->directMode = 1;
122258 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
122262 ExprList *pList = pF->pExpr->x.pList;
122263 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
122265 nArg = pList->nExpr;
122272 if( pF->iDistinct>=0 ){
122276 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
122278 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
122282 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
122283 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
122284 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
122287 pColl = pParse->db->pDfltColl;
122289 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
122292 sqlite3VdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem);
122293 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
122306 ** to pC->iMem. But by the time the value is used, the original register
122317 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
122318 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
122320 pAggInfo->directMode = 0;
122337 if( pParse->explain==2 ){
122339 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
122340 pTab->zName,
122342 bCover ? pIdx->zName : ""
122345 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
122368 ** sub-expression matches the criteria for being moved to the WHERE
122369 ** clause. If so, add it to the WHERE clause and replace the sub-expression
122373 if( pExpr->op!=TK_AND ){
122374 struct HavingToWhereCtx *p = pWalker->u.pHavingCtx;
122375 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, p->pGroupBy) ){
122376 sqlite3 *db = pWalker->pParse->db;
122379 Expr *pWhere = *(p->ppWhere);
122382 *(p->ppWhere) = pNew;
122425 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
122430 SrcList *pTabList, /* Search for self-joins in this FROM clause */
122434 for(pItem = pTabList->a; pItem<pThis; pItem++){
122435 if( pItem->pSelect==0 ) continue;
122436 if( pItem->fg.viaCoroutine ) continue;
122437 if( pItem->zName==0 ) continue;
122438 if( sqlite3_stricmp(pItem->zDatabase, pThis->zDatabase)!=0 ) continue;
122439 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
122441 pThis->pSelect->pWhere, pItem->pSelect->pWhere, -1)
122475 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate query */
122476 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
122477 pExpr = p->pEList->a[0].pExpr;
122478 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
122479 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Must be count() */
122480 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
122481 if( p->pSrc->nSrc!=1 ) return 0; /* One table in the FROM clause */
122482 pSub = p->pSrc->a[0].pSelect;
122484 if( pSub->pPrior==0 ) return 0; /* Must be a compound subquery */
122486 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
122487 if( pSub->pWhere ) return 0; /* No WHERE clause */
122488 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
122489 pSub = pSub->pPrior; /* Repeat over compound terms */
122494 db = pParse->db;
122497 pSub = p->pSrc->a[0].pSelect;
122498 p->pSrc->a[0].pSelect = 0;
122499 sqlite3SrcListDelete(db, p->pSrc);
122500 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
122503 pPrior = pSub->pPrior;
122504 pSub->pPrior = 0;
122505 pSub->pNext = 0;
122506 pSub->selFlags |= SF_Aggregate;
122507 pSub->selFlags &= ~SF_Compound;
122508 pSub->nSelectRow = 0;
122509 sqlite3ExprListDelete(db, pSub->pEList);
122511 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
122521 p->pEList->a[0].pExpr = pExpr;
122522 p->selFlags &= ~SF_Aggregate;
122526 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
122542 ** pParse->zErrMsg.
122569 int iRestoreSelectId = pParse->iSelectId;
122570 pParse->iSelectId = pParse->iNextSelectId++;
122573 db = pParse->db;
122574 if( p==0 || db->mallocFailed || pParse->nErr ){
122580 pParse->nSelectIndent++;
122587 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
122588 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
122589 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
122590 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
122592 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
122593 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
122594 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
122595 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
122598 sqlite3ExprListDelete(db, p->pOrderBy);
122599 p->pOrderBy = 0;
122600 p->selFlags &= ~SF_Distinct;
122604 sSort.pOrderBy = p->pOrderBy;
122605 pTabList = p->pSrc;
122606 if( pParse->nErr || db->mallocFailed ){
122609 assert( p->pEList!=0 );
122610 isAgg = (p->selFlags & SF_Aggregate)!=0;
122622 if( pDest->eDest==SRT_Output ){
122629 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
122630 struct SrcList_item *pItem = &pTabList->a[i];
122631 Select *pSub = pItem->pSelect;
122633 Table *pTab = pItem->pTab;
122638 if( pTab->nCol!=pSub->pEList->nExpr ){
122640 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
122644 isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
122649 p->selFlags |= SF_Aggregate;
122651 i = -1;
122653 pTabList = p->pSrc;
122654 if( db->mallocFailed ) goto select_end;
122656 sSort.pOrderBy = p->pOrderBy;
122665 if( p->pPrior ){
122667 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
122669 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
122670 pParse->nSelectIndent--;
122678 ** (2) Generate code for all sub-queries
122680 for(i=0; i<pTabList->nSrc; i++){
122681 struct SrcList_item *pItem = &pTabList->a[i];
122689 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
122690 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
122697 ** assume the column name is non-NULL and segfault. The use of an empty string
122700 if( pItem->colUsed==0 ){
122701 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
122705 /* Generate code for all sub-queries in the FROM clause
122707 pSub = pItem->pSelect;
122713 ** a view or the co-routine to implement a view. The first instance
122716 if( pItem->addrFillSub ){
122717 if( pItem->fg.viaCoroutine==0 ){
122718 /* The subroutine that manifests the view might be a one-time routine,
122721 testcase( sqlite3VdbeGetOp(v, pItem->addrFillSub)->opcode==OP_Once );
122722 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
122730 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
122734 pParse->nHeight += sqlite3SelectExprHeight(p);
122736 /* Make copies of constant WHERE-clause terms in the outer query down
122739 if( (pItem->fg.jointype & JT_OUTER)==0
122740 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
122744 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
122752 ** The subquery is implemented as a co-routine if all of these are true:
122757 ** the use of co-routines.)
122758 ** (3) Co-routines are not disabled using sqlite3_test_control()
122761 ** TODO: Are there other reasons beside (1) to use a co-routine
122765 && (pTabList->nSrc==1
122766 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
122767 && (p->selFlags & SF_All)==0 /* (2) */
122770 /* Implement a co-routine that will return a single row of the result
122774 pItem->regReturn = ++pParse->nMem;
122775 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
122776 VdbeComment((v, "%s", pItem->pTab->zName));
122777 pItem->addrFillSub = addrTop;
122778 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
122779 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
122781 pItem->pTab->nRowLogEst = pSub->nSelectRow;
122782 pItem->fg.viaCoroutine = 1;
122783 pItem->regResult = dest.iSdst;
122784 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
122785 sqlite3VdbeJumpHere(v, addrTop-1);
122789 ** the content of this subquery. pItem->addrFillSub will point
122790 ** to the address of the generated subroutine. pItem->regReturn
122798 assert( pItem->addrFillSub==0 );
122799 pItem->regReturn = ++pParse->nMem;
122800 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
122801 pItem->addrFillSub = topAddr+1;
122802 if( pItem->fg.isCorrelated==0 ){
122807 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
122809 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
122813 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
122814 explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
122815 assert( pPrior->pSelect!=0 );
122816 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
122818 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
122819 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
122822 pItem->pTab->nRowLogEst = pSub->nSelectRow;
122824 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
122825 VdbeComment((v, "end %s", pItem->pTab->zName));
122829 if( db->mallocFailed ) goto select_end;
122830 pParse->nHeight -= sqlite3SelectExprHeight(p);
122836 pEList = p->pEList;
122837 pWhere = p->pWhere;
122838 pGroupBy = p->pGroupBy;
122839 pHaving = p->pHaving;
122840 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
122844 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
122853 if( db->mallocFailed ) goto select_end;
122854 pEList = p->pEList;
122855 pTabList = p->pSrc;
122860 ** if the select-list is the same as the ORDER BY list, then this query
122869 ** The second form is preferred as a single index (or temp-table) may be
122871 ** written the query must use a temp-table for at least one of the ORDER
122872 ** BY and DISTINCT, and an index or separate temp-table for the other.
122874 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
122875 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
122877 p->selFlags &= ~SF_Distinct;
122878 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
122879 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
122894 ** being unused if the data can be extracted in pre-sorted order.
122902 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
122903 sSort.iECursor = pParse->nTab++;
122906 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
122910 sSort.addrSortIndex = -1;
122915 if( pDest->eDest==SRT_EphemTab ){
122916 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
122922 if( (p->selFlags & SF_FixedLimit)==0 ){
122923 p->nSelectRow = 320; /* 4 billion rows */
122926 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
122933 if( p->selFlags & SF_Distinct ){
122934 sDistinct.tabTnct = pParse->nTab++;
122937 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
122949 wctrlFlags |= p->selFlags & SF_FixedLimit;
122953 p->pEList, wctrlFlags, p->nSelectRow);
122955 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
122956 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
122964 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
122978 selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
123008 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
123009 pItem->u.x.iAlias = 0;
123011 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
123012 pItem->u.x.iAlias = 0;
123015 if( p->nSelectRow>66 ) p->nSelectRow = 66;
123018 p->nSelectRow = 0;
123024 ** in the correct order. It also may not - the GROUP BY might use a
123029 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
123044 sAggInfo.mnReg = pParse->nMem+1;
123045 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
123051 assert( pWhere==p->pWhere );
123052 havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
123053 pWhere = p->pWhere;
123061 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
123064 sAggInfo.mxReg = pParse->nMem;
123065 if( db->mallocFailed ) goto select_end;
123072 int addr1; /* A-vs-B comparision jump */
123086 sAggInfo.sortingIdx = pParse->nTab++;
123094 iUseFlag = ++pParse->nMem;
123095 iAbortFlag = ++pParse->nMem;
123096 regOutputRow = ++pParse->nMem;
123098 regReset = ++pParse->nMem;
123100 iAMem = pParse->nMem + 1;
123101 pParse->nMem += pGroupBy->nExpr;
123102 iBMem = pParse->nMem + 1;
123103 pParse->nMem += pGroupBy->nExpr;
123108 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
123120 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
123138 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
123142 nGroupBy = pGroupBy->nExpr;
123157 if( pCol->iSorterColumn>=j ){
123160 pCol->pTab, pCol->iColumn, pCol->iTable, r1);
123170 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
123184 ** This is an optimization - the correct answer should result regardless.
123205 for(j=0; j<pGroupBy->nExpr; j++){
123210 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
123213 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
123227 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
123264 ** is less than or equal to zero, the subroutine is a no-op. If
123281 selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
123287 /* Generate a subroutine that will reset the group-by accumulator
123312 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
123313 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
123317 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
123320 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
123324 ** (2011-04-15) Do not do a full scan of an unordered index.
123326 ** (2013-10-03) Do not count the entries in a partial index.
123332 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123333 if( pIdx->bUnordered==0
123334 && pIdx->szIdxRow<pTab->szTabRow
123335 && pIdx->pPartIdxWhere==0
123336 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
123342 iRoot = pBest->tnum;
123346 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
123349 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
123385 assert( p->pGroupBy==0 );
123387 if( p->pHaving==0 ){
123390 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
123395 assert( db->mallocFailed || pMinMax!=0 );
123396 if( !db->mallocFailed ){
123397 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
123398 pMinMax->a[0].pExpr->op = TK_COLUMN;
123413 assert( pMinMax==0 || pMinMax->nExpr==1 );
123425 selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
123443 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
123452 rc = (pParse->nErr>0);
123458 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
123464 pParse->nSelectIndent--;
123514 int need; /* Slots needed in p->azResult[] */
123518 /* Make sure there is enough space in p->azResult to hold everything
123521 if( p->nRow==0 && argv!=0 ){
123526 if( p->nData + need > p->nAlloc ){
123528 p->nAlloc = p->nAlloc*2 + need;
123529 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
123531 p->azResult = azNew;
123537 if( p->nRow==0 ){
123538 p->nColumn = nCol;
123542 p->azResult[p->nData++] = z;
123544 }else if( (int)p->nColumn!=nCol ){
123545 sqlite3_free(p->zErrMsg);
123546 p->zErrMsg = sqlite3_mprintf(
123549 p->rc = SQLITE_ERROR;
123565 p->azResult[p->nData++] = z;
123567 p->nRow++;
123572 p->rc = SQLITE_NOMEM_BKPT;
123612 db->errCode = SQLITE_NOMEM;
123628 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
123641 db->errCode = SQLITE_NOMEM;
123660 azResult--;
123693 pTriggerStep = pTriggerStep->pNext;
123695 sqlite3ExprDelete(db, pTmp->pWhere);
123696 sqlite3ExprListDelete(db, pTmp->pExprList);
123697 sqlite3SelectDelete(db, pTmp->pSelect);
123698 sqlite3IdListDelete(db, pTmp->pIdList);
123709 ** are already attached to pTab->pTrigger. But there might be additional
123711 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
123716 ** pTab as well as the triggers lised in pTab->pTrigger.
123719 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
123722 if( pParse->disableTriggers ){
123726 if( pTmpSchema!=pTab->pSchema ){
123728 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
123729 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
123731 if( pTrig->pTabSchema==pTab->pSchema
123732 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
123734 pTrig->pNext = (pList ? pList : pTab->pTrigger);
123740 return (pList ? pList : pTab->pTrigger);
123747 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
123766 sqlite3 *db = pParse->db; /* The database connection */
123771 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
123777 if( pName2->n>0 ){
123790 if( !pTableName || db->mallocFailed ){
123794 /* A long-standing parser bug is that this syntax was allowed:
123802 if( db->init.busy && iDb!=1 ){
123803 sqlite3DbFree(db, pTableName->a[0].zDatabase);
123804 pTableName->a[0].zDatabase = 0;
123813 if( db->init.busy==0 && pName2->n==0 && pTab
123814 && pTab->pSchema==db->aDb[1].pSchema ){
123819 if( db->mallocFailed ) goto trigger_cleanup;
123820 assert( pTableName->nSrc==1 );
123828 if( db->init.iDb==1 ){
123831 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
123835 ** "orphaned trigger" - a trigger whose associated table is missing.
123837 db->init.orphanTrigger = 1;
123853 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
123857 assert( !db->init.busy );
123864 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
123872 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
123877 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
123885 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123887 const char *zDb = db->aDb[iTabDb].zDbSName;
123888 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
123890 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
123911 pTrigger->zName = zName;
123913 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
123914 pTrigger->pSchema = db->aDb[iDb].pSchema;
123915 pTrigger->pTabSchema = pTab->pSchema;
123916 pTrigger->op = (u8)op;
123917 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
123918 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
123919 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
123920 assert( pParse->pNewTrigger==0 );
123921 pParse->pNewTrigger = pTrigger;
123928 if( !pParse->pNewTrigger ){
123931 assert( pParse->pNewTrigger==pTrigger );
123944 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
123946 sqlite3 *db = pParse->db; /* The database */
123951 pParse->pNewTrigger = 0;
123952 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
123953 zName = pTrig->zName;
123954 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
123955 pTrig->step_list = pStepList;
123957 pStepList->pTrig = pTrig;
123958 pStepList = pStepList->pNext;
123960 sqlite3TokenInit(&nameToken, pTrig->zName);
123962 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
123963 || sqlite3FixExpr(&sFix, pTrig->pWhen)
123971 if( !db->init.busy ){
123979 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
123983 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
123984 pTrig->table, z);
123991 if( db->init.busy ){
123993 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
123998 }else if( pLink->pSchema==pLink->pTabSchema ){
124000 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
124002 pLink->pNext = pTab->pTrigger;
124003 pTab->pTrigger = pLink;
124009 assert( !pParse->pNewTrigger );
124026 pTriggerStep->op = TK_SELECT;
124027 pTriggerStep->pSelect = pSelect;
124028 pTriggerStep->orconf = OE_Default;
124036 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
124045 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
124048 memcpy(z, pName->z, pName->n);
124050 pTriggerStep->zTarget = z;
124051 pTriggerStep->op = op;
124072 assert(pSelect != 0 || db->mallocFailed);
124076 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
124077 pTriggerStep->pIdList = pColumn;
124078 pTriggerStep->orconf = orconf;
124103 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
124104 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124105 pTriggerStep->orconf = orconf;
124126 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124127 pTriggerStep->orconf = OE_Default;
124138 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
124139 sqlite3DbFree(db, pTrigger->zName);
124140 sqlite3DbFree(db, pTrigger->table);
124141 sqlite3ExprDelete(db, pTrigger->pWhen);
124142 sqlite3IdListDelete(db, pTrigger->pColumns);
124159 sqlite3 *db = pParse->db;
124161 if( db->mallocFailed ) goto drop_trigger_cleanup;
124166 assert( pName->nSrc==1 );
124167 zDb = pName->a[0].zDatabase;
124168 zName = pName->a[0].zName;
124170 for(i=OMIT_TEMPDB; i<db->nDb; i++){
124172 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
124174 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
124183 pParse->checkSchema = 1;
124197 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
124207 sqlite3 *db = pParse->db;
124210 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
124211 assert( iDb>=0 && iDb<db->nDb );
124214 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
124218 const char *zDb = db->aDb[iDb].zDbSName;
124221 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
124234 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
124237 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
124249 pHash = &(db->aDb[iDb].pSchema->trigHash);
124252 if( pTrigger->pSchema==pTrigger->pTabSchema ){
124255 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
124256 *pp = (*pp)->pNext;
124259 db->flags |= SQLITE_InternChanges;
124275 for(e=0; e<pEList->nExpr; e++){
124276 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
124298 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
124302 for(p=pList; p; p=p->pNext){
124303 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
124304 mask |= p->tr_tm;
124314 ** Convert the pStep->zTarget string into a SrcList and return a pointer
124327 sqlite3 *db = pParse->db;
124333 assert( pSrc->nSrc>0 );
124334 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
124335 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
124338 assert( iDb<db->nDb );
124339 zDb = db->aDb[iDb].zDbSName;
124340 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
124356 Vdbe *v = pParse->pVdbe;
124357 sqlite3 *db = pParse->db;
124359 assert( pParse->pTriggerTab && pParse->pToplevel );
124362 for(pStep=pStepList; pStep; pStep=pStep->pNext){
124373 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
124374 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
124376 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
124377 assert( pParse->okConstFactor==0 );
124379 switch( pStep->op ){
124383 sqlite3ExprListDup(db, pStep->pExprList, 0),
124384 sqlite3ExprDup(db, pStep->pWhere, 0),
124385 pParse->eOrconf
124392 sqlite3SelectDup(db, pStep->pSelect, 0),
124393 sqlite3IdListDup(db, pStep->pIdList),
124394 pParse->eOrconf
124401 sqlite3ExprDup(db, pStep->pWhere, 0)
124405 default: assert( pStep->op==TK_SELECT ); {
124407 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
124414 if( pStep->op!=TK_SELECT ){
124441 ** Parse context structure pFrom has just been used to create a sub-vdbe
124446 assert( pFrom->zErrMsg==0 || pFrom->nErr );
124447 assert( pTo->zErrMsg==0 || pTo->nErr );
124448 if( pTo->nErr==0 ){
124449 pTo->zErrMsg = pFrom->zErrMsg;
124450 pTo->nErr = pFrom->nErr;
124451 pTo->rc = pFrom->rc;
124453 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
124458 ** Create and populate a new TriggerPrg object with a sub-program
124468 sqlite3 *db = pParse->db; /* Database handle */
124472 NameContext sNC; /* Name context for sub-vdbe */
124473 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
124474 Parse *pSubParse; /* Parse context for sub-vdbe */
124477 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
124478 assert( pTop->pVdbe );
124482 ** list of the top-level Parse object sooner rather than later. */
124485 pPrg->pNext = pTop->pTriggerPrg;
124486 pTop->pTriggerPrg = pPrg;
124487 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
124489 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
124490 pPrg->pTrigger = pTrigger;
124491 pPrg->orconf = orconf;
124492 pPrg->aColmask[0] = 0xffffffff;
124493 pPrg->aColmask[1] = 0xffffffff;
124496 ** trigger sub-program. */
124501 pSubParse->db = db;
124502 pSubParse->pTriggerTab = pTab;
124503 pSubParse->pToplevel = pTop;
124504 pSubParse->zAuthContext = pTrigger->zName;
124505 pSubParse->eTriggerOp = pTrigger->op;
124506 pSubParse->nQueryLoop = pParse->nQueryLoop;
124511 pTrigger->zName, onErrorText(orconf),
124512 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
124513 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
124514 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
124515 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
124516 pTab->zName
124519 sqlite3VdbeChangeP4(v, -1,
124520 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
124525 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
124527 if( pTrigger->pWhen ){
124528 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
124530 && db->mallocFailed==0
124538 /* Code the trigger program into the sub-vdbe. */
124539 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
124541 /* Insert an OP_Halt at the end of the sub-program. */
124546 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
124549 if( db->mallocFailed==0 ){
124550 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
124552 pProgram->nMem = pSubParse->nMem;
124553 pProgram->nCsr = pSubParse->nTab;
124554 pProgram->token = (void *)pTrigger;
124555 pPrg->aColmask[0] = pSubParse->oldmask;
124556 pPrg->aColmask[1] = pSubParse->newmask;
124560 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
124561 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
124569 ** Return a pointer to a TriggerPrg object containing the sub-program for
124583 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
124589 for(pPrg=pRoot->pTriggerPrg;
124590 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
124591 pPrg=pPrg->pNext
124619 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
124622 ** is a pointer to the sub-vdbe containing the trigger program. */
124624 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
124626 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
124627 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
124629 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
124631 /* Set the P5 operand of the OP_Program instruction to non-zero if
124633 ** invocation is disallowed if (a) the sub-program is really a trigger,
124648 ** operation on pTab, this function is a no-op.
124653 ** (a copy of pTab->nCol), then registers are populated as follows:
124656 ** ------------------------------------------------------
124658 ** reg+1 OLD.* value of left-most column of pTab
124660 ** reg+N OLD.* value of right-most column of pTab
124662 ** reg+N+2 OLD.* value of left-most column of pTab
124664 ** reg+N+N+1 NEW.* value of right-most column of pTab
124697 for(p=pTrigger; p; p=p->pNext){
124702 assert( p->pSchema!=0 );
124703 assert( p->pTabSchema!=0 );
124704 assert( p->pSchema==p->pTabSchema
124705 || p->pSchema==pParse->db->aDb[1].pSchema );
124708 if( p->op==op
124709 && p->tr_tm==tr_tm
124710 && checkColumnOverlap(p->pColumns, pChanges)
124718 ** Triggers may access values stored in the old.* or new.* pseudo-table.
124719 ** This function returns a 32-bit bitmask indicating which columns of the
124724 ** Bit 0 of the returned mask is set if the left-most column of the
124756 for(p=pTrigger; p; p=p->pNext){
124757 if( p->op==op && (tr_tm&p->tr_tm)
124758 && checkColumnOverlap(p->pColumns,pChanges)
124763 mask |= pPrg->aColmask[isNew];
124807 ** i-th column of table pTab. This routine sets the P4 parameter of the
124813 ** command. If the latter, then the row-records in the table btree on disk
124816 ** If the former, then all row-records are guaranteed to include a value
124832 ** stored in place of an 8-byte floating point value in order to save
124837 if( !pTab->pSelect ){
124840 Column *pCol = &pTab->aCol[i];
124841 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
124842 assert( i<pTab->nCol );
124843 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
124844 pCol->affinity, &pValue);
124850 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
124859 ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
124883 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
124884 ** an expression for the i-th column of the table.
124885 ** aXRef[i]==-1 if the i-th column is not changed. */
124892 NameContext sNC; /* The name-context to resolve expressions in */
124924 db = pParse->db;
124925 if( pParse->nErr || db->mallocFailed ){
124928 assert( pTabList->nSrc==1 );
124934 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124941 isView = pTab->pSelect!=0;
124965 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
124968 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
124970 iDataCur = pParse->nTab;
124971 pTabList->a[0].iCursor = iDataCur;
124973 pParse->nTab++;
124979 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
124981 aRegIdx = aXRef+pTab->nCol;
124985 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
124987 /* Initialize the name-context */
124999 for(i=0; i<pChanges->nExpr; i++){
125000 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
125003 for(j=0; j<pTab->nCol; j++){
125004 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
125005 if( j==pTab->iPKey ){
125007 pRowidExpr = pChanges->a[i].pExpr;
125008 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
125015 if( j>=pTab->nCol ){
125016 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
125017 j = -1;
125019 pRowidExpr = pChanges->a[i].pExpr;
125021 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
125022 pParse->checkSchema = 1;
125029 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
125030 j<0 ? "ROWID" : pTab->aCol[j].zName,
125031 db->aDb[iDb].zDbSName);
125035 aXRef[j] = -1;
125050 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
125060 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
125062 if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
125063 reg = ++pParse->nMem;
125064 pParse->nMem += pIdx->nColumn;
125067 for(i=0; i<pIdx->nKeyCol; i++){
125068 i16 iIdxCol = pIdx->aiColumn[i];
125070 reg = ++pParse->nMem;
125071 pParse->nMem += pIdx->nColumn;
125073 || (onError==OE_Default && pIdx->onError==OE_Replace)
125093 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
125098 regRowSet = ++pParse->nMem;
125099 regOldRowid = regNewRowid = ++pParse->nMem;
125101 regOld = pParse->nMem + 1;
125102 pParse->nMem += pTab->nCol;
125105 regNewRowid = ++pParse->nMem;
125107 regNew = pParse->nMem + 1;
125108 pParse->nMem += pTab->nCol;
125113 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
125142 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
125143 regRowCount = ++pParse->nMem;
125151 nPk = pPk->nKeyCol;
125152 iPk = pParse->nMem+1;
125153 pParse->nMem += nPk;
125154 regKey = ++pParse->nMem;
125155 iEph = pParse->nTab++;
125164 ** Do not consider a single-pass strategy for a multi-row update if
125168 ** or index, causing a single-pass approach to malfunction. */
125170 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
125176 /* A one-pass strategy that might update more than one row may not
125189 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
125197 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
125210 assert( pPk->aiColumn[i]>=0 );
125211 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
125234 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
125235 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
125238 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
125285 /* Compute the old pre-UPDATE content of the row being changed, if that
125292 for(i=0; i<pTab->nCol; i++){
125295 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
125324 for(i=0; i<pTab->nCol; i++){
125325 if( i==pTab->iPKey ){
125330 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
125354 /* The row-trigger may have deleted the row being updated. In this
125356 ** required. This behavior - what happens when the row being updated
125357 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
125368 /* If it did not delete it, the row-trigger may still have modified
125373 for(i=0; i<pTab->nCol; i++){
125374 if( aXRef[i]<0 && i!=pTab->iPKey ){
125403 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
125407 ** to invoke the pre-update hook.
125410 ** pre-update hook. If the caller invokes preupdate_new(), the returned
125424 if( !pParse->nested ){
125457 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
125468 /* Nothing to do at end-of-loop for a single-pass */
125484 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
125493 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
125521 ** There are two possible strategies - the default and the special
125549 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
125552 sqlite3 *db = pParse->db; /* Database connection */
125555 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
125559 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
125568 ephemTab = pParse->nTab++;
125570 regArg = pParse->nMem + 1;
125571 pParse->nMem += nArg;
125572 regRec = ++pParse->nMem;
125573 regRowid = ++pParse->nMem;
125586 for(i=0; i<pTab->nCol; i++){
125588 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
125597 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
125598 ** above. Also, if this is a top-level parse (not a trigger), clear the
125599 ** multi-write flag so that the VM does not open a statement journal */
125602 pParse->isMultiWrite = 0;
125681 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
125755 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
125756 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
125759 iDb = sqlite3FindDb(pParse->db, pNm);
125777 int saved_flags; /* Saved value of the db->flags */
125778 int saved_nChange; /* Saved value of db->nChange */
125779 int saved_nTotalChange; /* Saved value of db->nTotalChange */
125787 if( !db->autoCommit ){
125791 if( db->nVdbeActive>1 ){
125792 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
125797 ** restored before returning. Then set the writable-schema flag, and
125799 saved_flags = db->flags;
125800 saved_nChange = db->nChange;
125801 saved_nTotalChange = db->nTotalChange;
125802 saved_mTrace = db->mTrace;
125803 db->flags |= (SQLITE_WriteSchema | SQLITE_IgnoreChecks
125805 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
125806 db->mTrace = 0;
125808 zDbMain = db->aDb[iDb].zDbSName;
125809 pMain = db->aDb[iDb].pBt;
125818 ** An optimisation would be to use a non-journaled pager.
125826 nDb = db->nDb;
125829 assert( (db->nDb-1)==nDb );
125830 pDb = &db->aDb[nDb];
125831 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
125832 pTemp = pDb->pBt;
125844 if( db->nextPagesize ){
125849 if( nKey ) db->nextPagesize = 0;
125853 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
125859 ** to ensure that we do not try to change the page-size on a WAL database.
125869 db->nextPagesize = 0;
125873 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
125874 || NEVER(db->mallocFailed)
125881 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
125888 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
125902 db->init.iDb = 0;
125915 assert( (db->flags & SQLITE_Vacuum)!=0 );
125916 db->flags &= ~SQLITE_Vacuum;
125935 ** both transactions are closed by this block - the main database
125982 /* Restore the original value of db->flags */
125983 db->init.iDb = 0;
125984 db->flags = saved_flags;
125985 db->nChange = saved_nChange;
125986 db->nTotalChange = saved_nTotalChange;
125987 db->mTrace = saved_mTrace;
125988 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
125997 db->autoCommit = 1;
126000 sqlite3BtreeClose(pDb->pBt);
126001 pDb->pBt = 0;
126002 pDb->pSchema = 0;
126005 /* This both clears the schemas and reduces the size of the db->aDb[]
126065 pMod->zName = zCopy;
126066 pMod->pModule = pModule;
126067 pMod->pAux = pAux;
126068 pMod->xDestroy = xDestroy;
126069 pMod->pEpoTab = 0;
126070 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
126095 sqlite3_mutex_enter(db->mutex);
126096 if( sqlite3HashFind(&db->aModule, zName) ){
126103 sqlite3_mutex_leave(db->mutex);
126109 ** External API function used to create a new virtual-table module.
126124 ** External API function used to create a new virtual-table module.
126148 pVTab->nRef++;
126153 ** pTab is a pointer to a Table structure representing a virtual-table.
126155 ** this virtual-table, if one has been created, or NULL otherwise.
126160 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
126165 ** Decrement the ref-count on a virtual table object. When the ref-count
126169 sqlite3 *db = pVTab->db;
126172 assert( pVTab->nRef>0 );
126173 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
126175 pVTab->nRef--;
126176 if( pVTab->nRef==0 ){
126177 sqlite3_vtab *p = pVTab->pVtab;
126179 p->pModule->xDisconnect(p);
126187 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
126190 ** connection db is left in the p->pVTable list.
126194 VTable *pVTable = p->pVTable;
126195 p->pVTable = 0;
126201 ** database connection that may have an entry in the p->pVTable list.
126203 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
126206 sqlite3 *db2 = pVTable->db;
126207 VTable *pNext = pVTable->pNext;
126211 p->pVTable = pRet;
126212 pRet->pNext = 0;
126214 pVTable->pNext = db2->pDisconnect;
126215 db2->pDisconnect = pVTable;
126227 ** list in p->pVTab. It also decrements the VTable ref count. This is
126230 ** be being used by other shared-cache connections).
126237 assert( sqlite3_mutex_held(db->mutex) );
126239 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
126240 if( (*ppVTab)->db==db ){
126242 *ppVTab = pVTab->pNext;
126254 ** shared b-tree databases opened using connection db are held by the
126264 ** or, if the virtual table is stored in a non-sharable database, then
126268 ** by multiple threads. It is thread-safe.
126271 VTable *p = db->pDisconnect;
126272 db->pDisconnect = 0;
126275 assert( sqlite3_mutex_held(db->mutex) );
126280 VTable *pNext = p->pNext;
126288 ** Clear any and all virtual-table information from the Table record.
126292 ** Since it is a virtual-table, the Table structure contains a pointer
126302 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
126303 if( p->azModuleArg ){
126305 for(i=0; i<p->nModuleArg; i++){
126306 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
126308 sqlite3DbFree(db, p->azModuleArg);
126313 ** Add a new module argument to pTable->azModuleArg[].
126314 ** The string is not copied - the pointer is stored. The
126319 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
126321 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
126325 int i = pTable->nModuleArg++;
126328 pTable->azModuleArg = azModuleArg;
126349 pTable = pParse->pNewTable;
126351 assert( 0==pTable->pIndex );
126353 db = pParse->db;
126354 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
126357 assert( pTable->nModuleArg==0 );
126360 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
126361 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
126362 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
126364 pParse->sNameToken.n = (int)(
126365 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
126374 if( pTable->azModuleArg ){
126375 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
126376 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
126383 ** in pParse->zArg[] and appends it to the list of arguments on the
126384 ** virtual table currently under construction in pParse->pTable.
126387 if( pParse->sArg.z && pParse->pNewTable ){
126388 const char *z = (const char*)pParse->sArg.z;
126389 int n = pParse->sArg.n;
126390 sqlite3 *db = pParse->db;
126391 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
126400 Table *pTab = pParse->pNewTable; /* The table being constructed */
126401 sqlite3 *db = pParse->db; /* The database connection */
126405 pParse->sArg.z = 0;
126406 if( pTab->nModuleArg<1 ) return;
126414 if( !db->init.busy ){
126423 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
126425 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
126431 ** The VM register number pParse->regRowid holds the rowid of an
126435 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126440 db->aDb[iDb].zDbSName, MASTER_NAME,
126441 pTab->zName,
126442 pTab->zName,
126444 pParse->regRowid
126451 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
126454 iReg = ++pParse->nMem;
126455 sqlite3VdbeLoadString(v, iReg, pTab->zName);
126459 /* If we are rereading the sqlite_master table create the in-memory
126466 Schema *pSchema = pTab->pSchema;
126467 const char *zName = pTab->zName;
126469 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
126475 pParse->pNewTable = 0;
126485 pParse->sArg.z = 0;
126486 pParse->sArg.n = 0;
126494 Token *pArg = &pParse->sArg;
126495 if( pArg->z==0 ){
126496 pArg->z = p->z;
126497 pArg->n = p->n;
126499 assert(pArg->z <= p->z);
126500 pArg->n = (int)(&p->z[p->n] - pArg->z);
126519 const char *const*azArg = (const char *const*)pTab->azModuleArg;
126520 int nArg = pTab->nModuleArg;
126526 /* Check that the virtual-table is not already being initialized */
126527 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
126528 if( pCtx->pTab==pTab ){
126530 "vtable constructor called recursively: %s", pTab->zName
126536 zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
126546 pVTable->db = db;
126547 pVTable->pMod = pMod;
126549 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126550 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
126553 assert( &db->pVtabCtx );
126557 sCtx.pPrior = db->pVtabCtx;
126559 db->pVtabCtx = &sCtx;
126560 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
126561 db->pVtabCtx = sCtx.pPrior;
126573 }else if( ALWAYS(pVTable->pVtab) ){
126576 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
126577 pVTable->pVtab->pModule = pMod->pModule;
126578 pVTable->nRef = 1;
126581 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
126588 ** into the linked list headed by pTab->pVTable. Then loop through the
126592 pVTable->pNext = pTab->pVTable;
126593 pTab->pVTable = pVTable;
126595 for(iCol=0; iCol<pTab->nCol; iCol++){
126596 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
126602 && (i==0 || zType[i-1]==' ')
126615 assert(zType[i-1]==' ');
126616 zType[i-1] = '\0';
126618 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
126621 pTab->tabFlags |= oooHidden;
126636 ** This call is a no-op if table pTab is not a virtual table.
126639 sqlite3 *db = pParse->db;
126650 zMod = pTab->azModuleArg[0];
126651 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
126654 const char *zModule = pTab->azModuleArg[0];
126659 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
126669 ** Grow the db->aVTrans[] array so that there is room for at least one
126670 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
126673 const int ARRAY_INCR = 5;
126676 if( (db->nVTrans%ARRAY_INCR)==0 ){
126678 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
126679 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
126683 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
126684 db->aVTrans = aVTrans;
126696 db->aVTrans[db->nVTrans++] = pVTab;
126714 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
126715 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
126718 zMod = pTab->azModuleArg[0];
126719 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
126725 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
126729 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
126761 sqlite3_mutex_enter(db->mutex);
126762 pCtx = db->pVtabCtx;
126763 if( !pCtx || pCtx->bDeclared ){
126765 sqlite3_mutex_leave(db->mutex);
126768 pTab = pCtx->pTab;
126775 pParse->declareVtab = 1;
126776 pParse->db = db;
126777 pParse->nQueryLoop = 1;
126780 && pParse->pNewTable
126781 && !db->mallocFailed
126782 && !pParse->pNewTable->pSelect
126783 && !IsVirtual(pParse->pNewTable)
126785 if( !pTab->aCol ){
126786 Table *pNew = pParse->pNewTable;
126788 pTab->aCol = pNew->aCol;
126789 pTab->nCol = pNew->nCol;
126790 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
126791 pNew->nCol = 0;
126792 pNew->aCol = 0;
126793 assert( pTab->pIndex==0 );
126794 if( !HasRowid(pNew) && pCtx->pVTable->pMod->pModule->xUpdate!=0 ){
126797 pIdx = pNew->pIndex;
126799 assert( pIdx->pNext==0 );
126800 pTab->pIndex = pIdx;
126801 pNew->pIndex = 0;
126802 pIdx->pTable = pTab;
126805 pCtx->bDeclared = 1;
126811 pParse->declareVtab = 0;
126813 if( pParse->pVdbe ){
126814 sqlite3VdbeFinalize(pParse->pVdbe);
126816 sqlite3DeleteTable(db, pParse->pNewTable);
126823 sqlite3_mutex_leave(db->mutex);
126832 ** This call is a no-op if zTab is not a virtual table.
126838 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
126839 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
126842 for(p=pTab->pVTable; p; p=p->pNext){
126843 assert( p->pVtab );
126844 if( p->pVtab->nRef>0 ){
126849 xDestroy = p->pMod->pModule->xDestroy;
126851 rc = xDestroy(p->pVtab);
126854 assert( pTab->pVTable==p && p->pNext==0 );
126855 p->pVtab = 0;
126856 pTab->pVTable = 0;
126874 if( db->aVTrans ){
126875 VTable **aVTrans = db->aVTrans;
126876 db->aVTrans = 0;
126877 for(i=0; i<db->nVTrans; i++){
126879 sqlite3_vtab *p = pVTab->pVtab;
126882 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
126885 pVTab->iSavepoint = 0;
126889 db->nVTrans = 0;
126898 ** If an error message is available, leave it in p->zErrMsg.
126903 VTable **aVTrans = db->aVTrans;
126905 db->aVTrans = 0;
126906 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
126908 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
126909 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
126914 db->aVTrans = aVTrans;
126948 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
126959 pModule = pVTab->pVtab->pModule;
126961 if( pModule->xBegin ){
126965 for(i=0; i<db->nVTrans; i++){
126966 if( db->aVTrans[i]==pVTab ){
126975 rc = pModule->xBegin(pVTab->pVtab);
126977 int iSvpt = db->nStatement + db->nSavepoint;
126979 if( iSvpt && pModule->xSavepoint ){
126980 pVTab->iSavepoint = iSvpt;
126981 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
127008 assert( iSavepoint>=-1 );
127009 if( db->aVTrans ){
127011 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
127012 VTable *pVTab = db->aVTrans[i];
127013 const sqlite3_module *pMod = pVTab->pMod->pModule;
127014 if( pVTab->pVtab && pMod->iVersion>=2 ){
127018 xMethod = pMod->xSavepoint;
127019 pVTab->iSavepoint = iSavepoint+1;
127022 xMethod = pMod->xRollbackTo;
127025 xMethod = pMod->xRelease;
127028 if( xMethod && pVTab->iSavepoint>iSavepoint ){
127029 rc = xMethod(pVTab->pVtab, iSavepoint);
127069 if( pExpr->op!=TK_COLUMN ) return pDef;
127070 pTab = pExpr->pTab;
127073 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
127075 assert( pVtab->pModule!=0 );
127076 pMod = (sqlite3_module *)pVtab->pModule;
127077 if( pMod->xFindFunction==0 ) return pDef;
127082 zLowerName = sqlite3DbStrDup(db, pDef->zName);
127087 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
127097 + sqlite3Strlen30(pDef->zName) + 1);
127102 pNew->zName = (const char*)&pNew[1];
127103 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
127104 pNew->xSFunc = xSFunc;
127105 pNew->pUserData = pArg;
127106 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
127111 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
127114 ** is a no-op.
127122 for(i=0; i<pToplevel->nVtabLock; i++){
127123 if( pTab==pToplevel->apVtabLock[i] ) return;
127125 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
127126 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
127128 pToplevel->apVtabLock = apVtabLock;
127129 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
127131 sqlite3OomFault(pToplevel->db);
127138 ** exist. Return non-zero if the eponymous virtual table instance exists
127144 ** instances always exist. They cannot be DROP-ed.
127150 const sqlite3_module *pModule = pMod->pModule;
127154 sqlite3 *db = pParse->db;
127155 if( pMod->pEpoTab ) return 1;
127156 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
127159 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
127160 if( pTab->zName==0 ){
127164 pMod->pEpoTab = pTab;
127165 pTab->nTabRef = 1;
127166 pTab->pSchema = db->aDb[0].pSchema;
127167 assert( pTab->nModuleArg==0 );
127168 pTab->iPKey = -1;
127169 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
127171 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
127172 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
127187 Table *pTab = pMod->pEpoTab;
127192 pTab->tabFlags |= TF_Ephemeral;
127194 pMod->pEpoTab = 0;
127213 assert( OE_Ignore==4 && OE_Replace==5 );
127214 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
127215 return (int)aMap[db->vtabOnConflict-1];
127230 sqlite3_mutex_enter(db->mutex);
127234 VtabCtx *p = db->pVtabCtx;
127238 assert( p->pTab==0 || IsVirtual(p->pTab) );
127239 p->pVTable->bConstraint = (u8)va_arg(ap, int);
127250 sqlite3_mutex_leave(db->mutex);
127259 ** 2015-06-06
127272 ** This file was split off from where.c on 2015-06-06 in order to reduce the
127281 ** 2013-11-12
127339 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
127347 int addrSkip; /* Jump here for next iteration of skip-scan */
127358 union { /* Information that depends on pWLoop->wsFlags */
127366 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
127380 ** prevent a query solution - which is an error) and many terms of the
127382 ** potential way of implementing that FROM-clause term, together with
127398 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
127439 ** correspond to the subquery(s) of OR-clause processing. Only the
127455 ** vector, not a scalar, and because dependencies are many-to-one, not
127456 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
127472 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
127485 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
127516 ** spread out over the non-negative integers. For example, the cursor
127535 int iParent; /* Disable pWC->a[iParent] when this term disabled */
127543 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
127556 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
127566 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
127581 int k; /* Resume scanning at this->pWC->a[this->k] */
127583 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
127641 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
127642 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
127645 ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
127646 ** would map those cursor numbers into bits 0 through 5.
127649 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
127650 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
127664 #define initMaskSet(P) (P)->n=0
127707 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
127715 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
127726 ** Private interfaces - callable only by other where.c routines.
127768 int iLevel, /* Which level of pWInfo->a[] should be coded */
127788 ** OR-ed combination of these values can be used when searching for
127801 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
127802 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
127803 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
127804 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
127808 #define WO_OR 0x0200 /* Two or more OR-connected terms */
127809 #define WO_AND 0x0400 /* Two or more AND-connected terms */
127814 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
127829 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
127837 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
127847 ** Return the name of the i-th column of the pIdx index.
127850 i = pIdx->aiColumn[i];
127853 return pIdx->pTable->aCol[i].zName;
127868 int iTerm, /* Zero-based index of first term. */
127869 int bAnd, /* Non-zero to append " AND " */
127875 if( bAnd ) sqlite3StrAccumAppend(pStr, " AND ", 5);
127909 Index *pIndex = pLoop->u.btree.pIndex;
127910 u16 nEq = pLoop->u.btree.nEq;
127911 u16 nSkip = pLoop->nSkip;
127914 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
127918 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
127923 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
127924 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
127927 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
127928 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
127934 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
127936 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
127952 if( pParse->explain==2 )
127955 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
127956 Vdbe *v = pParse->pVdbe; /* VM being constructed */
127957 sqlite3 *db = pParse->db; /* Database handle */
127958 int iId = pParse->iSelectId; /* Select id (left-most output column) */
127966 pLoop = pLevel->pWLoop;
127967 flags = pLoop->wsFlags;
127971 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
127976 if( pItem->pSelect ){
127977 sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
127979 sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
127982 if( pItem->zAlias ){
127983 sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
127989 assert( pLoop->u.btree.pIndex!=0 );
127990 pIdx = pLoop->u.btree.pIndex;
127992 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
128007 sqlite3XPrintf(&str, zFmt, pIdx->zName);
128027 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
128031 if( pLoop->nOut>=10 ){
128032 sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
128061 WhereLoop *pLoop = pLvl->pWLoop;
128062 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
128063 zObj = pLoop->u.btree.pIndex->zName;
128065 zObj = pSrclist->a[pLvl->iFrom].zName;
128068 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
128114 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
128119 && (pTerm->wtFlags & TERM_CODED)==0
128120 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
128121 && (pLevel->notReady & pTerm->prereqAll)==0
128123 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
128124 pTerm->wtFlags |= TERM_LIKECOND;
128126 pTerm->wtFlags |= TERM_CODED;
128128 if( pTerm->iParent<0 ) break;
128129 pTerm = &pTerm->pWC->a[pTerm->iParent];
128130 pTerm->nChild--;
128131 if( pTerm->nChild!=0 ) break;
128140 ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
128148 Vdbe *v = pParse->pVdbe;
128150 assert( pParse->db->mallocFailed );
128159 n--;
128163 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
128164 n--;
128212 ** straight-line code. For constraints of the form X IN (...)
128220 int bRev, /* True for reverse-order IN operations */
128223 Expr *pX = pTerm->pExpr;
128224 Vdbe *v = pParse->pVdbe;
128227 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
128229 if( pX->op==TK_EQ || pX->op==TK_IS ){
128230 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
128231 }else if( pX->op==TK_ISNULL ){
128239 WhereLoop *pLoop = pLevel->pWLoop;
128244 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
128245 && pLoop->u.btree.pIndex!=0
128246 && pLoop->u.btree.pIndex->aSortOrder[iEq]
128252 assert( pX->op==TK_IN );
128256 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
128261 for(i=iEq;i<pLoop->nLTerm; i++){
128262 if( ALWAYS(pLoop->aLTerm[i]) && pLoop->aLTerm[i]->pExpr==pX ) nEq++;
128265 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
128268 Select *pSelect = pX->x.pSelect;
128269 sqlite3 *db = pParse->db;
128270 u16 savedDbOptFlags = db->dbOptFlags;
128271 ExprList *pOrigRhs = pSelect->pEList;
128272 ExprList *pOrigLhs = pX->pLeft->x.pList;
128274 ExprList *pLhs = 0; /* New pX->pLeft vector */
128276 for(i=iEq;i<pLoop->nLTerm; i++){
128277 if( pLoop->aLTerm[i]->pExpr==pX ){
128278 int iField = pLoop->aLTerm[i]->iField - 1;
128279 Expr *pNewRhs = sqlite3ExprDup(db, pOrigRhs->a[iField].pExpr, 0);
128280 Expr *pNewLhs = sqlite3ExprDup(db, pOrigLhs->a[iField].pExpr, 0);
128286 if( !db->mallocFailed ){
128287 Expr *pLeft = pX->pLeft;
128289 if( pSelect->pOrderBy ){
128291 ** iOrderByCol variables. These are set to non-zero when an
128293 ** result-set. Since the result-set of the SELECT statement may
128297 ExprList *pOrderBy = pSelect->pOrderBy;
128298 for(i=0; i<pOrderBy->nExpr; i++){
128299 pOrderBy->a[i].u.x.iOrderByCol = 0;
128306 if( pLhs->nExpr==1 ){
128307 pX->pLeft = pLhs->a[0].pExpr;
128309 pLeft->x.pList = pLhs;
128310 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq);
128313 pSelect->pEList = pRhs;
128314 db->dbOptFlags |= SQLITE_QueryFlattener;
128316 db->dbOptFlags = savedDbOptFlags;
128318 pSelect->pEList = pOrigRhs;
128319 pLeft->x.pList = pOrigLhs;
128320 pX->pLeft = pLeft;
128322 sqlite3ExprListDelete(pParse->db, pLhs);
128323 sqlite3ExprListDelete(pParse->db, pRhs);
128330 iTab = pX->iTable;
128334 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
128336 pLoop->wsFlags |= WHERE_IN_ABLE;
128337 if( pLevel->u.in.nIn==0 ){
128338 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
128341 i = pLevel->u.in.nIn;
128342 pLevel->u.in.nIn += nEq;
128343 pLevel->u.in.aInLoop =
128344 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
128345 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
128346 pIn = pLevel->u.in.aInLoop;
128350 for(i=iEq;i<pLoop->nLTerm; i++){
128351 if( pLoop->aLTerm[i]->pExpr==pX ){
128352 int iOut = iReg + i - iEq;
128355 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
128358 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
128362 pIn->iCur = iTab;
128363 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
128365 pIn->eEndLoopOp = OP_Noop;
128371 pLevel->u.in.nIn = 0;
128373 sqlite3DbFree(pParse->db, aiMap);
128385 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
128389 ** a==5 and b IN (1,2,3). The current values for a and b will be stored
128393 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
128394 ** The only thing it does is allocate the pLevel->iMem memory cell and
128399 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
128433 u16 nSkip; /* Number of left-most columns to skip */
128434 Vdbe *v = pParse->pVdbe; /* The vm under construction */
128444 pLoop = pLevel->pWLoop;
128445 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
128446 nEq = pLoop->u.btree.nEq;
128447 nSkip = pLoop->nSkip;
128448 pIdx = pLoop->u.btree.pIndex;
128453 regBase = pParse->nMem + 1;
128454 nReg = pLoop->u.btree.nEq + nExtraReg;
128455 pParse->nMem += nReg;
128457 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
128458 assert( zAff!=0 || pParse->db->mallocFailed );
128461 int iIdxCur = pLevel->iIdxCur;
128465 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
128467 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
128474 testcase( pIdx->aiColumn[j]==XN_EXPR );
128484 pTerm = pLoop->aLTerm[j];
128488 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
128489 testcase( pTerm->wtFlags & TERM_VIRTUAL );
128499 if( pTerm->eOperator & WO_IN ){
128500 if( pTerm->pExpr->flags & EP_xIsSelect ){
128507 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
128508 Expr *pRight = pTerm->pExpr->pRight;
128509 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
128510 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
128543 ** becomes a no-op.
128550 if( pTerm->wtFlags & TERM_LIKEOPT ){
128552 assert( pLevel->iLikeRepCntr>0 );
128553 pOp = sqlite3VdbeGetOp(v, -1);
128555 assert( pOp->opcode==OP_String8
128556 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
128557 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
128558 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
128581 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
128584 struct CCurHint *pHint = pWalker->u.pCCurHint;
128585 assert( pHint->pIdx!=0 );
128586 if( pExpr->op==TK_COLUMN
128587 && pExpr->iTable==pHint->iTabCur
128588 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
128590 pWalker->eCode = 1;
128597 ** should be included in the cursor-hint for a table that is on the rhs
128598 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
128611 if( pExpr->op==TK_IS
128612 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
128613 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
128615 pWalker->eCode = 1;
128616 }else if( pExpr->op==TK_FUNCTION ){
128619 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
128620 pWalker->eCode = 1;
128647 struct CCurHint *pHint = pWalker->u.pCCurHint;
128648 if( pExpr->op==TK_COLUMN ){
128649 if( pExpr->iTable!=pHint->iTabCur ){
128650 Vdbe *v = pWalker->pParse->pVdbe;
128651 int reg = ++pWalker->pParse->nMem; /* Register for column value */
128653 v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg
128655 pExpr->op = TK_REGISTER;
128656 pExpr->iTable = reg;
128657 }else if( pHint->pIdx!=0 ){
128658 pExpr->iTable = pHint->iIdxCur;
128659 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
128660 assert( pExpr->iColumn>=0 );
128662 }else if( pExpr->op==TK_AGG_FUNCTION ){
128664 ** be a correlated sub-query, and expression pExpr is an aggregate from
128682 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
128684 Parse *pParse = pWInfo->pParse;
128685 sqlite3 *db = pParse->db;
128686 Vdbe *v = pParse->pVdbe;
128688 WhereLoop *pLoop = pLevel->pWLoop;
128697 iCur = pLevel->iTabCur;
128698 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
128700 sHint.iIdxCur = pLevel->iIdxCur;
128701 sHint.pIdx = pLoop->u.btree.pIndex;
128705 pWC = &pWInfo->sWC;
128706 for(i=0; i<pWC->nTerm; i++){
128707 pTerm = &pWC->a[i];
128708 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
128709 if( pTerm->prereqAll & pLevel->notReady ) continue;
128713 ** from the cursor-hint.
128733 if( pTabItem->fg.jointype & JT_LEFT ){
128734 Expr *pExpr = pTerm->pExpr;
128736 || pExpr->iRightJoinTable!=pTabItem->iCursor
128740 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
128744 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
128747 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
128750 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
128751 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
128752 if( j<pLoop->nLTerm ) continue;
128755 /* No subqueries or non-deterministic functions allowed */
128756 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
128763 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
128768 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
128779 # define codeCursorHint(A,B,C,D) /* No-op */
128783 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
128792 ** However, if the scan currently being coded is a branch of an OR-loop and
128803 int iCur, /* Cursor for IPK b-tree */
128806 Parse *pParse = pWInfo->pParse; /* Parse context */
128807 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
128810 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
128813 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
128814 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
128817 Table *pTab = pIdx->pTable;
128818 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
128820 ai[0] = pTab->nCol;
128821 for(i=0; i<pIdx->nColumn-1; i++){
128822 assert( pIdx->aiColumn[i]<pTab->nCol );
128823 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
128825 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
128843 if( (p->flags & EP_xIsSelect) ){
128844 Vdbe *v = pParse->pVdbe;
128846 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
128851 ExprList *pList = p->x.pList;
128852 assert( nReg<=pList->nExpr );
128854 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
128881 IdxExprTrans *pX = p->u.pIdxTrans;
128882 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
128883 pExpr->op = TK_COLUMN;
128884 pExpr->iTable = pX->iIdxCur;
128885 pExpr->iColumn = pX->iIdxCol;
128886 pExpr->pTab = 0;
128908 aColExpr = pIdx->aColExpr;
128915 for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
128916 if( pIdx->aiColumn[iIdxCol]!=XN_EXPR ) continue;
128917 assert( aColExpr->a[iIdxCol].pExpr!=0 );
128919 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
128920 sqlite3WalkExpr(&w, pWInfo->pWhere);
128921 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
128922 sqlite3WalkExprList(&w, pWInfo->pResultSet);
128927 ** Generate code for the start of the iLevel-th loop in the WHERE clause
128932 int iLevel, /* Which level of pWInfo->a[] should be coded */
128956 pParse = pWInfo->pParse;
128957 v = pParse->pVdbe;
128958 pWC = &pWInfo->sWC;
128959 db = pParse->db;
128960 pLevel = &pWInfo->a[iLevel];
128961 pLoop = pLevel->pWLoop;
128962 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
128963 iCur = pTabItem->iCursor;
128964 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
128965 bRev = (pWInfo->revMask>>iLevel)&1;
128966 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
128967 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
128968 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
128980 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
128981 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
128987 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
128988 pLevel->iLeftJoin = ++pParse->nMem;
128989 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
128990 VdbeComment((v, "init LEFT JOIN no-match flag"));
128995 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
128996 addrHalt = pWInfo->a[j].addrBrk;
128998 /* Special case of a FROM clause subquery implemented as a co-routine */
128999 if( pTabItem->fg.viaCoroutine ){
129000 int regYield = pTabItem->regReturn;
129001 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
129002 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
129004 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
129005 pLevel->op = OP_Goto;
129009 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
129010 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
129015 int nConstraint = pLoop->nLTerm;
129020 addrNotFound = pLevel->addrBrk;
129023 pTerm = pLoop->aLTerm[j];
129025 if( pTerm->eOperator & WO_IN ){
129027 addrNotFound = pLevel->addrNxt;
129029 Expr *pRight = pTerm->pExpr->pRight;
129033 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
129036 pLoop->u.vtab.idxStr,
129037 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
129039 pLoop->u.vtab.needFree = 0;
129040 pLevel->p1 = iCur;
129041 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
129042 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
129043 iIn = pLevel->u.in.nIn;
129044 for(j=nConstraint-1; j>=0; j--){
129045 pTerm = pLoop->aLTerm[j];
129046 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
129048 }else if( (pTerm->eOperator & WO_IN)!=0 ){
129057 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
129058 if( !db->mallocFailed ){
129060 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
129061 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
129062 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
129063 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
129064 testcase( pOp->opcode==OP_Rowid );
129065 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
129071 assert( pCompare!=0 || db->mallocFailed );
129073 pCompare->pLeft = pTerm->pExpr->pLeft;
129074 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
129076 pRight->iTable = iReg+j+2;
129077 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
129079 pCompare->pLeft = 0;
129086 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
129095 if( (pLoop->wsFlags & WHERE_IPK)!=0
129096 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
129103 assert( pLoop->u.btree.nEq==1 );
129104 pTerm = pLoop->aLTerm[0];
129106 assert( pTerm->pExpr!=0 );
129108 testcase( pTerm->wtFlags & TERM_VIRTUAL );
129109 iReleaseReg = ++pParse->nMem;
129112 addrNxt = pLevel->addrNxt;
129116 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
129118 pLevel->op = OP_Noop;
129119 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
129120 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
129132 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
129133 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
129159 assert( (pStart->wtFlags & TERM_VNULL)==0 );
129160 testcase( pStart->wtFlags & TERM_VIRTUAL );
129161 pX = pStart->pExpr;
129163 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
129164 if( sqlite3ExprIsVector(pX->pRight) ){
129166 codeExprOrVector(pParse, pX->pRight, r1, 1);
129167 op = aMoveOp[(pX->op - TK_GT) | 0x0001];
129169 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
129171 op = aMoveOp[(pX->op - TK_GT)];
129175 VdbeCoverageIf(v, pX->op==TK_GT);
129176 VdbeCoverageIf(v, pX->op==TK_LE);
129177 VdbeCoverageIf(v, pX->op==TK_LT);
129178 VdbeCoverageIf(v, pX->op==TK_GE);
129188 pX = pEnd->pExpr;
129190 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
129191 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
129192 testcase( pEnd->wtFlags & TERM_VIRTUAL );
129193 memEndValue = ++pParse->nMem;
129194 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
129195 if( 0==sqlite3ExprIsVector(pX->pRight)
129196 && (pX->op==TK_LT || pX->op==TK_GT)
129202 if( 0==sqlite3ExprIsVector(pX->pRight) ){
129207 pLevel->op = bRev ? OP_Prev : OP_Next;
129208 pLevel->p1 = iCur;
129209 pLevel->p2 = start;
129210 assert( pLevel->p5==0 );
129212 iRowidReg = ++pParse->nMem;
129214 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
129222 }else if( pLoop->wsFlags & WHERE_INDEXED ){
129227 ** left-most columns of the index. It may also contain
129230 ** the right-most column can be an inequality - the rest must
129235 ** x=5
129236 ** x=5 AND y=10
129237 ** x=5 AND y<10
129238 ** x=5 AND y>5 AND y<10
129239 ** x=5 AND y=5 AND z<=10
129242 ** the x=5 term:
129244 ** x=5 AND z<10
129260 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
129270 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
129271 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
129272 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
129288 pIdx = pLoop->u.btree.pIndex;
129289 iIdxCur = pLevel->iIdxCur;
129290 assert( nEq>=pLoop->nSkip );
129300 assert( pWInfo->pOrderBy==0
129301 || pWInfo->pOrderBy->nExpr==1
129302 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
129303 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
129304 && pWInfo->nOBSat>0
129305 && (pIdx->nKeyCol>nEq)
129307 assert( pLoop->nSkip==0 );
129316 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
129317 pRangeStart = pLoop->aLTerm[j++];
129318 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
129320 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
129321 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
129323 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
129324 pRangeEnd = pLoop->aLTerm[j++];
129325 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
129327 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
129329 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
129330 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
129331 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
129333 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
129337 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
129339 pLevel->iLikeRepCntr <<=1;
129340 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
129344 j = pIdx->aiColumn[nEq];
129345 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
129350 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
129356 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
129357 || (bRev && pIdx->nKeyCol==nEq)
129374 addrNxt = pLevel->addrNxt;
129376 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
129377 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
129378 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
129379 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
129380 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
129381 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
129387 Expr *pRight = pRangeStart->pExpr->pRight;
129390 if( (pRangeStart->wtFlags & TERM_VNULL)==0
129400 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
129413 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
129414 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
129415 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
129436 Expr *pRight = pRangeEnd->pExpr->pRight;
129440 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
129450 assert( pParse->db->mallocFailed );
129453 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
129469 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
129484 }else if( HasRowid(pIdx->pTable) ){
129485 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
129486 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
129487 && (pWInfo->eOnePass==ONEPASS_SINGLE)
129489 iRowidReg = ++pParse->nMem;
129491 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
129498 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
129499 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
129500 for(j=0; j<pPk->nKeyCol; j++){
129501 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
129505 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
129516 if( pLoop->wsFlags & WHERE_ONEROW ){
129517 pLevel->op = OP_Noop;
129519 pLevel->op = OP_Prev;
129521 pLevel->op = OP_Next;
129523 pLevel->p1 = iIdxCur;
129524 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
129525 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
129526 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
129528 assert( pLevel->p5==0 );
129534 if( pLoop->wsFlags & WHERE_MULTI_OR ){
129535 /* Case 5: Two or more separately indexed terms connected by OR
129544 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
129573 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
129578 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
129579 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
129581 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
129583 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
129590 u16 wctrlFlags; /* Flags for sub-WHERE clause */
129592 Table *pTab = pTabItem->pTab;
129594 pTerm = pLoop->aLTerm[0];
129596 assert( pTerm->eOperator & WO_OR );
129597 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
129598 pOrWc = &pTerm->u.pOrInfo->wc;
129599 pLevel->op = OP_Return;
129600 pLevel->p1 = regReturn;
129606 if( pWInfo->nLevel>1 ){
129609 nNotReady = pWInfo->nLevel - iLevel - 1;
129611 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
129613 pOrTab->nAlloc = (u8)(nNotReady + 1);
129614 pOrTab->nSrc = pOrTab->nAlloc;
129615 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
129616 origSrc = pWInfo->pTabList->a;
129618 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
129621 pOrTab = pWInfo->pTabList;
129632 ** correct response for the end-of-loop code (the OP_Return) is to
129636 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
129638 regRowset = ++pParse->nMem;
129642 regRowset = pParse->nTab++;
129643 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
129646 regRowid = ++pParse->nMem;
129656 ** the "interesting" terms of z - terms that did not originate in the
129664 if( pWC->nTerm>1 ){
129666 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
129667 Expr *pExpr = pWC->a[iTerm].pExpr;
129668 if( &pWC->a[iTerm] == pTerm ) continue;
129670 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
129671 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
129672 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
129673 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
129674 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
129685 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
129687 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
129688 for(ii=0; ii<pOrWc->nTerm; ii++){
129689 WhereTerm *pOrTerm = &pOrWc->a[ii];
129690 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
129691 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
129692 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
129695 pAndExpr->pLeft = pOrExpr;
129699 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
129702 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
129706 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
129708 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
129710 /* This is the sub-WHERE clause body. First skip over
129711 ** duplicate rows from prior sub-WHERE clauses, and record the
129713 ** row will be skipped in subsequent sub-WHERE clauses.
129715 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
129717 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
129719 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
129725 int nPk = pPk->nKeyCol;
129731 int iCol = pPk->aiColumn[iPk];
129743 ** the temp table. And if iSet is -1, assume that there is no
129766 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
129769 /* The pSubWInfo->untestedTerms flag means that this OR term
129774 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
129776 /* If all of the OR-connected terms are optimized using the same
129782 ** uses an index, and this is either the first OR-connected term
129788 pSubLoop = pSubWInfo->a[0].pWLoop;
129789 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
129790 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
129791 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
129792 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
129794 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
129795 pCov = pSubLoop->u.btree.pIndex;
129805 pLevel->u.pCovidx = pCov;
129806 if( pCov ) pLevel->iIdxCur = iCovCur;
129808 pAndExpr->pLeft = 0;
129812 sqlite3VdbeGoto(v, pLevel->addrBrk);
129815 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
129827 if( pTabItem->fg.isRecursive ){
129829 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
129830 pLevel->op = OP_Noop;
129833 pLevel->op = aStep[bRev];
129834 pLevel->p1 = iCur;
129835 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
129838 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
129843 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
129855 ** sub-queries.
129863 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
129866 testcase( pTerm->wtFlags & TERM_VIRTUAL );
129867 testcase( pTerm->wtFlags & TERM_CODED );
129868 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
129869 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
129870 testcase( pWInfo->untestedTerms==0
129871 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
129872 pWInfo->untestedTerms = 1;
129875 pE = pTerm->pExpr;
129877 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
129881 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
129885 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
129890 if( pTerm->wtFlags & TERM_LIKECOND ){
129899 u32 x = pLevel->iLikeRepCntr;
129908 pWC->nTerm-j, pTerm, iLoop));
129913 pTerm->wtFlags |= TERM_CODED;
129926 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
129929 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
129930 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
129931 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
129932 if( pTerm->leftCursor!=iCur ) continue;
129933 if( pLevel->iLeftJoin ) continue;
129934 pE = pTerm->pExpr;
129936 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
129937 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
129940 if( pAlt->wtFlags & (TERM_CODED) ) continue;
129941 testcase( pAlt->eOperator & WO_EQ );
129942 testcase( pAlt->eOperator & WO_IS );
129943 testcase( pAlt->eOperator & WO_IN );
129945 sEAlt = *pAlt->pExpr;
129946 sEAlt.pLeft = pE->pLeft;
129953 if( pLevel->iLeftJoin ){
129954 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
129955 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
129958 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
129959 testcase( pTerm->wtFlags & TERM_VIRTUAL );
129960 testcase( pTerm->wtFlags & TERM_CODED );
129961 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
129962 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
129963 assert( pWInfo->untestedTerms );
129966 assert( pTerm->pExpr );
129967 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
129968 pTerm->wtFlags |= TERM_CODED;
129972 return pLevel->notReady;
129978 ** 2015-06-08
130005 sqlite3WhereClauseClear(&p->wc);
130013 sqlite3WhereClauseClear(&p->wc);
130020 ** The index in pWC->a[] of the new WhereTerm is returned on success.
130023 ** the db->mallocFailed flag so that higher-level functions can detect it.
130025 ** This routine will increase the size of the pWC->a[] array as necessary.
130034 ** the pWC->a[] array.
130040 if( pWC->nTerm>=pWC->nSlot ){
130041 WhereTerm *pOld = pWC->a;
130042 sqlite3 *db = pWC->pWInfo->pParse->db;
130043 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
130044 if( pWC->a==0 ){
130048 pWC->a = pOld;
130051 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
130052 if( pOld!=pWC->aStatic ){
130055 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
130057 pTerm = &pWC->a[idx = pWC->nTerm++];
130059 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
130061 pTerm->truthProb = 1;
130063 pTerm->pExpr = sqlite3ExprSkipCollate(p);
130064 pTerm->wtFlags = wtFlags;
130065 pTerm->pWC = pWC;
130066 pTerm->iParent = -1;
130067 memset(&pTerm->eOperator, 0,
130068 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
130098 u16 expRight = (pExpr->pRight->flags & EP_Collate);
130099 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
130100 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
130106 pExpr->pRight->flags &= ~EP_Collate;
130107 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
130108 /* Neither X nor Y have COLLATE operators, but X has a non-default
130111 pExpr->pLeft->flags |= EP_Collate;
130114 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
130115 if( pExpr->op>=TK_GT ){
130120 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
130121 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
130138 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
130139 c = (u16)(WO_EQ<<(op-TK_EQ));
130177 int cnt; /* Number of non-wildcard prefix characters */
130179 sqlite3 *db = pParse->db; /* Database connection */
130190 pList = pExpr->x.pList;
130191 pLeft = pList->a[1].pExpr;
130193 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
130194 op = pRight->op;
130195 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
130196 Vdbe *pReprepare = pParse->pReprepare;
130197 int iCol = pRight->iColumn;
130202 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
130203 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
130205 z = pRight->u.zToken;
130213 ** with a digit or '-', then "lhs LIKE rhs" will always be false if
130216 if( sqlite3Isdigit(z[0]) || z[0]=='-' ){
130217 if( pLeft->op!=TK_COLUMN
130219 || IsVirtual(pLeft->pTab) /* Value might be numeric */
130229 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
130233 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
130236 Vdbe *v = pParse->pVdbe;
130237 sqlite3VdbeSetVarmask(v, pRight->iColumn);
130238 if( *pisComplete && pRight->u.zToken[1] ){
130247 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
130291 if( pExpr->op!=TK_FUNCTION ){
130294 pList = pExpr->x.pList;
130295 if( pList==0 || pList->nExpr!=2 ){
130298 pCol = pList->a[1].pExpr;
130299 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
130303 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
130318 pDerived->flags |= pBase->flags & EP_FromJoin;
130319 pDerived->iRightJoinTable = pBase->iRightJoinTable;
130327 pWC->a[iChild].iParent = iParent;
130328 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
130329 pWC->a[iParent].nChild++;
130333 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
130338 if( pTerm->eOperator!=WO_AND ){
130341 if( N<pTerm->u.pAndInfo->wc.nTerm ){
130342 return &pTerm->u.pAndInfo->wc.a[N];
130349 ** two subterms are in disjunction - they are OR-ed together.
130359 ** x<y OR x=y --> x<=y
130360 ** x=y OR x=y --> x=y
130361 ** x<=y OR x<y --> x<=y
130365 ** x<y OR x>y --> x!=y
130373 u16 eOp = pOne->eOperator | pTwo->eOperator;
130379 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
130380 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
130383 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
130384 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
130385 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
130386 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
130388 if( (eOp & (eOp-1))!=0 ){
130396 db = pWC->pWInfo->pParse->db;
130397 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
130399 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
130400 pNew->op = op;
130407 ** Analyze a term that consists of two or more OR-connected
130410 ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
130420 ** The term being analyzed must have two or more of OR-connected subterms.
130421 ** A single subterm might be a set of AND-connected sub-subterms.
130424 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
130428 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
130459 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
130496 int idxTerm /* Index of the OR-term to be analyzed */
130498 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
130499 Parse *pParse = pWInfo->pParse; /* Parser context */
130500 sqlite3 *db = pParse->db; /* Database connection */
130501 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
130502 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
130505 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
130515 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
130516 assert( pExpr->op==TK_OR );
130517 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
130519 pTerm->wtFlags |= TERM_ORINFO;
130520 pOrWc = &pOrInfo->wc;
130521 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
130525 if( db->mallocFailed ) return;
130526 assert( pOrWc->nTerm>=2 );
130533 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
130534 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
130536 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
130544 pOrTerm->u.pAndInfo = pAndInfo;
130545 pOrTerm->wtFlags |= TERM_ANDINFO;
130546 pOrTerm->eOperator = WO_AND;
130547 pAndWC = &pAndInfo->wc;
130548 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
130549 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
130550 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
130552 pAndWC->pOuter = pWC;
130553 if( !db->mallocFailed ){
130554 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
130555 assert( pAndTerm->pExpr );
130556 if( allowedOp(pAndTerm->pExpr->op)
130557 || pAndTerm->eOperator==WO_MATCH
130559 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
130565 }else if( pOrTerm->wtFlags & TERM_COPIED ){
130570 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
130571 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
130572 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
130573 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
130576 if( (pOrTerm->eOperator & WO_EQ)==0 ){
130588 pOrInfo->indexable = indexable;
130589 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
130591 /* For a two-way OR, attempt to implementation case 2.
130593 if( indexable && pOrWc->nTerm==2 ){
130596 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
130599 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
130610 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
130613 ** something other than == on a column in the single table. The 1-bit
130617 ** sure the same column is used on all terms. The 2-bit case is when
130628 int iColumn = -1; /* Column index on lhs of IN operator */
130629 int iCursor = -1; /* Table cursor common to all terms */
130639 pOrTerm = pOrWc->a;
130640 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
130641 assert( pOrTerm->eOperator & WO_EQ );
130642 pOrTerm->wtFlags &= ~TERM_OR_OK;
130643 if( pOrTerm->leftCursor==iCursor ){
130644 /* This is the 2-bit case and we are on the second iteration and
130649 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
130650 pOrTerm->leftCursor))==0 ){
130655 testcase( pOrTerm->wtFlags & TERM_COPIED );
130656 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
130657 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
130660 iColumn = pOrTerm->u.leftColumn;
130661 iCursor = pOrTerm->leftCursor;
130669 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
130677 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
130678 assert( pOrTerm->eOperator & WO_EQ );
130679 if( pOrTerm->leftCursor!=iCursor ){
130680 pOrTerm->wtFlags &= ~TERM_OR_OK;
130681 }else if( pOrTerm->u.leftColumn!=iColumn ){
130685 /* If the right-hand side is also a column, then the affinities
130689 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
130690 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
130694 pOrTerm->wtFlags |= TERM_OR_OK;
130710 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
130711 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
130712 assert( pOrTerm->eOperator & WO_EQ );
130713 assert( pOrTerm->leftCursor==iCursor );
130714 assert( pOrTerm->u.leftColumn==iColumn );
130715 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
130716 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
130717 pLeft = pOrTerm->pExpr->pLeft;
130726 pNew->x.pList = pList;
130730 pTerm = &pWC->a[idxTerm];
130735 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
130749 ** 5a. Both operands use the same collating sequence OR
130750 ** 5b. The overall collating sequence is BINARY
130760 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
130761 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
130763 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
130764 aff2 = sqlite3ExprAffinity(pExpr->pRight);
130770 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
130771 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
130772 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
130773 zColl1 = pColl ? pColl->zName : 0;
130774 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
130775 zColl2 = pColl ? pColl->zName : 0;
130787 SrcList *pSrc = pS->pSrc;
130788 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
130789 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
130790 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
130791 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
130792 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
130795 for(i=0; i<pSrc->nSrc; i++){
130796 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
130797 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
130800 pS = pS->pPrior;
130811 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
130828 iCur = pFrom->a[i].iCursor;
130829 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
130830 if( pIdx->aColExpr==0 ) continue;
130831 for(i=0; i<pIdx->nKeyCol; i++){
130832 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
130833 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
130855 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
130856 pExpr = pExpr->x.pList->a[0].pExpr;
130859 if( pExpr->op==TK_COLUMN ){
130860 aiCurCol[0] = pExpr->iTable;
130861 aiCurCol[1] = pExpr->iColumn;
130865 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
130892 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
130896 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
130902 int op; /* Top-level operator. pExpr->op */
130903 Parse *pParse = pWInfo->pParse; /* Parsing context */
130904 sqlite3 *db = pParse->db; /* Database connection */
130908 if( db->mallocFailed ){
130911 pTerm = &pWC->a[idxTerm];
130912 pMaskSet = &pWInfo->sMaskSet;
130913 pExpr = pTerm->pExpr;
130914 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
130915 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
130916 op = pExpr->op;
130918 assert( pExpr->pRight==0 );
130921 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
130923 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
130926 pTerm->prereqRight = 0;
130928 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
130930 pMaskSet->bVarSelect = 0;
130932 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
130934 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
130936 extraRight = x-1; /* ON clause terms may not be used with an index
130943 pTerm->prereqAll = prereqAll;
130944 pTerm->leftCursor = -1;
130945 pTerm->iParent = -1;
130946 pTerm->eOperator = 0;
130949 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
130950 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
130951 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
130953 if( pTerm->iField>0 ){
130955 assert( pLeft->op==TK_VECTOR );
130956 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
130960 pTerm->leftCursor = aiCurCol[0];
130961 pTerm->u.leftColumn = aiCurCol[1];
130962 pTerm->eOperator = operatorMask(op) & opMask;
130964 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
130966 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
130970 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
130971 assert( pTerm->iField==0 );
130972 if( pTerm->leftCursor>=0 ){
130975 if( db->mallocFailed ){
130981 pNew = &pWC->a[idxNew];
130983 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
130984 pTerm = &pWC->a[idxTerm];
130985 pTerm->wtFlags |= TERM_COPIED;
130988 pTerm->eOperator |= WO_EQUIV;
130996 pNew->leftCursor = aiCurCol[0];
130997 pNew->u.leftColumn = aiCurCol[1];
130999 pNew->prereqRight = prereqLeft | extraRight;
131000 pNew->prereqAll = prereqAll;
131001 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
131021 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
131022 ExprList *pList = pExpr->x.pList;
131026 assert( pList->nExpr==2 );
131031 sqlite3ExprDup(db, pExpr->pLeft, 0),
131032 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
131037 pTerm = &pWC->a[idxTerm];
131047 else if( pExpr->op==TK_OR ){
131048 assert( pWC->op==TK_AND );
131050 pTerm = &pWC->a[idxTerm];
131064 ** for LIKE) then the lower-bound is made all uppercase and the upper-
131068 if( pWC->op==TK_AND
131072 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
131080 pLeft = pExpr->x.pList->a[1].pExpr;
131083 /* Convert the lower bound to upper-case and the upper bound to
131084 ** lower-case (upper-case is less than lower-case in ASCII) so that
131087 if( noCase && !pParse->db->mallocFailed ){
131090 pTerm->wtFlags |= TERM_LIKE;
131091 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
131092 pStr1->u.zToken[i] = sqlite3Toupper(c);
131093 pStr2->u.zToken[i] = sqlite3Tolower(c);
131097 if( !db->mallocFailed ){
131099 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
131108 if( c=='A'-1 ) isComplete = 0;
131130 pTerm = &pWC->a[idxTerm];
131145 if( pWC->op==TK_AND && isMatchOfColumn(pExpr, &eOp2) ){
131151 pRight = pExpr->x.pList->a[0].pExpr;
131152 pLeft = pExpr->x.pList->a[1].pExpr;
131164 pNewTerm = &pWC->a[idxNew];
131165 pNewTerm->prereqRight = prereqExpr;
131166 pNewTerm->leftCursor = pLeft->iTable;
131167 pNewTerm->u.leftColumn = pLeft->iColumn;
131168 pNewTerm->eOperator = WO_MATCH;
131169 pNewTerm->eMatchOp = eOp2;
131171 pTerm = &pWC->a[idxTerm];
131172 pTerm->wtFlags |= TERM_COPIED;
131173 pNewTerm->prereqAll = pTerm->prereqAll;
131178 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
131179 ** new terms for each component comparison - "a = ?" and "b = ?". The
131184 ** is not a sub-select. */
131185 if( pWC->op==TK_AND
131186 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
131187 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
131188 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
131189 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
131190 || (pExpr->pRight->flags & EP_xIsSelect)==0)
131196 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
131197 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
131199 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
131204 pTerm = &pWC->a[idxTerm];
131205 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */
131206 pTerm->eOperator = 0;
131209 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
131217 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
131218 && pExpr->pLeft->op==TK_VECTOR
131219 && pExpr->x.pSelect->pPrior==0
131222 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
131225 pWC->a[idxNew].iField = i+1;
131239 if( pExpr->op==TK_NOTNULL
131240 && pExpr->pLeft->op==TK_COLUMN
131241 && pExpr->pLeft->iColumn>=0
131245 Expr *pLeft = pExpr->pLeft;
131256 pNewTerm = &pWC->a[idxNew];
131257 pNewTerm->prereqRight = 0;
131258 pNewTerm->leftCursor = pLeft->iTable;
131259 pNewTerm->u.leftColumn = pLeft->iColumn;
131260 pNewTerm->eOperator = WO_GT;
131262 pTerm = &pWC->a[idxTerm];
131263 pTerm->wtFlags |= TERM_COPIED;
131264 pNewTerm->prereqAll = pTerm->prereqAll;
131272 testcase( pTerm!=&pWC->a[idxTerm] );
131273 pTerm = &pWC->a[idxTerm];
131274 pTerm->prereqRight |= extraRight;
131301 pWC->op = op;
131303 if( pE2->op!=op ){
131306 sqlite3WhereSplit(pWC, pE2->pLeft, op);
131307 sqlite3WhereSplit(pWC, pE2->pRight, op);
131318 pWC->pWInfo = pWInfo;
131319 pWC->pOuter = 0;
131320 pWC->nTerm = 0;
131321 pWC->nSlot = ArraySize(pWC->aStatic);
131322 pWC->a = pWC->aStatic;
131333 sqlite3 *db = pWC->pWInfo->pParse->db;
131334 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
131335 if( a->wtFlags & TERM_DYNAMIC ){
131336 sqlite3ExprDelete(db, a->pExpr);
131338 if( a->wtFlags & TERM_ORINFO ){
131339 whereOrInfoDelete(db, a->u.pOrInfo);
131340 }else if( a->wtFlags & TERM_ANDINFO ){
131341 whereAndInfoDelete(db, a->u.pAndInfo);
131344 if( pWC->a!=pWC->aStatic ){
131345 sqlite3DbFree(db, pWC->a);
131358 if( p->op==TK_COLUMN ){
131359 return sqlite3WhereGetMask(pMaskSet, p->iTable);
131361 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
131363 if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
131364 if( p->pRight ){
131365 mask |= sqlite3WhereExprUsage(pMaskSet, p->pRight);
131366 assert( p->x.pList==0 );
131368 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
131369 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
131370 }else if( p->x.pList ){
131371 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
131379 for(i=0; i<pList->nExpr; i++){
131380 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
131400 for(i=pWC->nTerm-1; i>=0; i--){
131406 ** For table-valued-functions, transform the function arguments into
131422 if( pItem->fg.isTabFunc==0 ) return;
131423 pTab = pItem->pTab;
131425 pArgs = pItem->u1.pFuncArg;
131427 for(j=k=0; j<pArgs->nExpr; j++){
131428 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
131429 if( k>=pTab->nCol ){
131430 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
131431 pTab->zName, j);
131434 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
131436 pColRef->iTable = pItem->iCursor;
131437 pColRef->iColumn = k++;
131438 pColRef->pTab = pTab;
131440 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
131481 return pWInfo->nRowOut;
131489 return pWInfo->eDistinct;
131497 return pWInfo->nOBSat;
131509 return pWInfo->bOrderedInnerLoop;
131517 assert( pWInfo->iContinue!=0 );
131518 return pWInfo->iContinue;
131526 return pWInfo->iBreak;
131533 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
131540 ** Either value may be -1, indicating that cursor is not used.
131543 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
131547 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
131549 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
131551 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
131555 return pWInfo->eOnePass;
131562 pDest->n = pSrc->n;
131563 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
131576 LogEst rRun, /* Run-cost of the new entry */
131581 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
131582 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
131585 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
131589 if( pSet->n<N_OR_COST ){
131590 p = &pSet->a[pSet->n++];
131591 p->nOut = nOut;
131593 p = pSet->a;
131594 for(i=1; i<pSet->n; i++){
131595 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
131597 if( p->rRun<=rRun ) return 0;
131600 p->prereq = prereq;
131601 p->rRun = rRun;
131602 if( p->nOut>nOut ) p->nOut = nOut;
131612 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
131613 for(i=0; i<pMaskSet->n; i++){
131614 if( pMaskSet->ix[i]==iCursor ){
131626 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
131630 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
131631 pMaskSet->ix[pMaskSet->n++] = iCursor;
131641 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
131643 WhereClause *pWC; /* Shorthand for pScan->pWC */
131645 int k = pScan->k; /* Where to start scanning */
131647 assert( pScan->iEquiv<=pScan->nEquiv );
131648 pWC = pScan->pWC;
131650 iColumn = pScan->aiColumn[pScan->iEquiv-1];
131651 iCur = pScan->aiCur[pScan->iEquiv-1];
131654 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
131655 if( pTerm->leftCursor==iCur
131656 && pTerm->u.leftColumn==iColumn
131658 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
131659 pScan->pIdxExpr,iCur)==0)
131660 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
131662 if( (pTerm->eOperator & WO_EQUIV)!=0
131663 && pScan->nEquiv<ArraySize(pScan->aiCur)
131664 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
131667 for(j=0; j<pScan->nEquiv; j++){
131668 if( pScan->aiCur[j]==pX->iTable
131669 && pScan->aiColumn[j]==pX->iColumn ){
131673 if( j==pScan->nEquiv ){
131674 pScan->aiCur[j] = pX->iTable;
131675 pScan->aiColumn[j] = pX->iColumn;
131676 pScan->nEquiv++;
131679 if( (pTerm->eOperator & pScan->opMask)!=0 ){
131681 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
131683 Parse *pParse = pWC->pWInfo->pParse;
131684 pX = pTerm->pExpr;
131685 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
131688 assert(pX->pLeft);
131690 pX->pLeft, pX->pRight);
131691 if( pColl==0 ) pColl = pParse->db->pDfltColl;
131692 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
131696 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
131697 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
131698 && pX->iTable==pScan->aiCur[0]
131699 && pX->iColumn==pScan->aiColumn[0]
131701 testcase( pTerm->eOperator & WO_IS );
131704 pScan->pWC = pWC;
131705 pScan->k = k+1;
131710 pWC = pWC->pOuter;
131713 if( pScan->iEquiv>=pScan->nEquiv ) break;
131714 pWC = pScan->pOrigWC;
131716 pScan->iEquiv++;
131748 pScan->pOrigWC = pWC;
131749 pScan->pWC = pWC;
131750 pScan->pIdxExpr = 0;
131751 pScan->idxaff = 0;
131752 pScan->zCollName = 0;
131755 iColumn = pIdx->aiColumn[j];
131757 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
131758 pScan->zCollName = pIdx->azColl[j];
131759 }else if( iColumn==pIdx->pTable->iPKey ){
131762 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
131763 pScan->zCollName = pIdx->azColl[j];
131768 pScan->opMask = opMask;
131769 pScan->k = 0;
131770 pScan->aiCur[0] = iCur;
131771 pScan->aiColumn[0] = iColumn;
131772 pScan->nEquiv = 1;
131773 pScan->iEquiv = 1;
131784 ** Search for terms matching the iColumn-th column of pIdx
131785 ** rather than the iColumn-th column of table iCur.
131789 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
131796 ** then try for the one with no dependencies on <expr> - in other words where
131799 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
131817 if( (p->prereqRight & notReady)==0 ){
131818 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
131819 testcase( p->eOperator & WO_IS );
131830 ** This function searches pList for an entry that matches the iCol-th column
131833 ** If such an expression is found, its index in pList->a[] is returned. If
131834 ** no expression is found, -1 is returned.
131844 const char *zColl = pIdx->azColl[iCol];
131846 for(i=0; i<pList->nExpr; i++){
131847 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
131848 if( p->op==TK_COLUMN
131849 && p->iColumn==pIdx->aiColumn[iCol]
131850 && p->iTable==iBase
131852 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
131853 if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
131859 return -1;
131863 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
131868 assert( iCol>=0 && iCol<pIdx->nColumn );
131869 j = pIdx->aiColumn[iCol];
131871 return pIdx->pTable->aCol[j].notNull;
131872 }else if( j==(-1) ){
131875 assert( j==(-2) );
131882 ** Return true if the DISTINCT expression-list passed as the third argument
131886 ** DISTINCT list are collectively unique and individually non-null.
131899 /* If there is more than one table or sub-select in the FROM clause of
131902 if( pTabList->nSrc!=1 ) return 0;
131903 iBase = pTabList->a[0].iCursor;
131904 pTab = pTabList->a[0].pTab;
131907 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
131908 ** current SELECT is a correlated sub-query.
131910 for(i=0; i<pDistinct->nExpr; i++){
131911 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
131912 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
131923 ** comparison and select-list expressions must match those of the index.
131928 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131930 for(i=0; i<pIdx->nKeyCol; i++){
131936 if( i==pIdx->nKeyCol ){
131950 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
131957 ** opcodes into OP_Copy when the table is being accessed via co-routine
131961 ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
131970 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */
131972 Vdbe *v = pParse->pVdbe;
131975 if( pParse->db->mallocFailed ) return;
131977 if( pOp->p1!=iTabCur ) continue;
131978 if( pOp->opcode==OP_Column ){
131979 pOp->opcode = OP_Copy;
131980 pOp->p1 = pOp->p2 + iRegister;
131981 pOp->p2 = pOp->p3;
131982 pOp->p3 = 0;
131983 }else if( pOp->opcode==OP_Rowid ){
131986 pOp->opcode = OP_AddImm;
131987 pOp->p1 = pOp->p2;
131988 pOp->p2 = 1;
131990 pOp->opcode = OP_Null;
131991 pOp->p1 = 0;
131992 pOp->p3 = 0;
132002 ** are no-ops.
132008 for(i=0; i<p->nConstraint; i++){
132011 p->aConstraint[i].iColumn,
132012 p->aConstraint[i].iTermOffset,
132013 p->aConstraint[i].op,
132014 p->aConstraint[i].usable);
132016 for(i=0; i<p->nOrderBy; i++){
132019 p->aOrderBy[i].iColumn,
132020 p->aOrderBy[i].desc);
132026 for(i=0; i<p->nConstraint; i++){
132029 p->aConstraintUsage[i].argvIndex,
132030 p->aConstraintUsage[i].omit);
132032 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
132033 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
132034 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
132035 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
132036 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
132055 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
132056 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
132057 if( (pSrc->fg.jointype & JT_LEFT)
132058 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
132059 && (pTerm->eOperator & WO_IS)
132066 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
132067 if( pTerm->u.leftColumn<0 ) return 0;
132068 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
132069 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
132070 testcase( pTerm->pExpr->op==TK_IS );
132091 WhereTerm *pWCEnd; /* End of pWC->a[] */
132100 int mxBitCol; /* Maximum column in pSrc->colUsed */
132115 v = pParse->pVdbe;
132122 pTable = pSrc->pTab;
132123 pWCEnd = &pWC->a[pWC->nTerm];
132124 pLoop = pLevel->pWLoop;
132126 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
132127 Expr *pExpr = pTerm->pExpr;
132128 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
132129 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
132130 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
132131 if( pLoop->prereq==0
132132 && (pTerm->wtFlags & TERM_VIRTUAL)==0
132134 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
132135 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
132136 sqlite3ExprDup(pParse->db, pExpr, 0));
132139 int iCol = pTerm->u.leftColumn;
132140 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
132142 testcase( iCol==BMS-1 );
132145 "automatic index on %s(%s)", pTable->zName,
132146 pTable->aCol[iCol].zName);
132150 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
132153 pLoop->aLTerm[nKeyCol++] = pTerm;
132159 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
132160 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
132171 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
132172 mxBitCol = MIN(BMS-1,pTable->nCol);
132173 testcase( pTable->nCol==BMS-1 );
132174 testcase( pTable->nCol==BMS-2 );
132178 if( pSrc->colUsed & MASKBIT(BMS-1) ){
132179 nKeyCol += pTable->nCol - BMS + 1;
132183 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
132185 pLoop->u.btree.pIndex = pIdx;
132186 pIdx->zName = "auto-index";
132187 pIdx->pTable = pTable;
132190 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
132192 int iCol = pTerm->u.leftColumn;
132193 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
132194 testcase( iCol==BMS-1 );
132197 Expr *pX = pTerm->pExpr;
132199 pIdx->aiColumn[n] = pTerm->u.leftColumn;
132200 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
132201 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
132206 assert( (u32)n==pLoop->u.btree.nEq );
132212 pIdx->aiColumn[n] = i;
132213 pIdx->azColl[n] = sqlite3StrBINARY;
132217 if( pSrc->colUsed & MASKBIT(BMS-1) ){
132218 for(i=BMS-1; i<pTable->nCol; i++){
132219 pIdx->aiColumn[n] = i;
132220 pIdx->azColl[n] = sqlite3StrBINARY;
132225 pIdx->aiColumn[n] = XN_ROWID;
132226 pIdx->azColl[n] = sqlite3StrBINARY;
132229 assert( pLevel->iIdxCur>=0 );
132230 pLevel->iIdxCur = pParse->nTab++;
132231 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
132233 VdbeComment((v, "for %s", pTable->zName));
132237 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
132238 if( pTabItem->fg.viaCoroutine ){
132239 int regYield = pTabItem->regReturn;
132241 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
132244 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
132246 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
132251 pLoop->wsFlags |= WHERE_PARTIALIDX;
132255 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
132257 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
132260 if( pTabItem->fg.viaCoroutine ){
132262 testcase( pParse->db->mallocFailed );
132263 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
132264 pTabItem->regResult, 1);
132266 pTabItem->fg.viaCoroutine = 0;
132268 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
132279 sqlite3ExprDelete(pParse->db, pPartial);
132309 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
132310 if( pTerm->leftCursor != pSrc->iCursor ) continue;
132311 if( pTerm->prereqRight & mUnusable ) continue;
132312 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
132313 testcase( pTerm->eOperator & WO_IN );
132314 testcase( pTerm->eOperator & WO_ISNULL );
132315 testcase( pTerm->eOperator & WO_IS );
132316 testcase( pTerm->eOperator & WO_ALL );
132317 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
132318 if( pTerm->wtFlags & TERM_VNULL ) continue;
132319 assert( pTerm->u.leftColumn>=(-1) );
132329 int n = pOrderBy->nExpr;
132331 Expr *pExpr = pOrderBy->a[i].pExpr;
132332 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
132341 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
132357 *(int*)&pIdxInfo->nConstraint = nTerm;
132358 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
132359 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
132360 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
132361 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
132364 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
132366 if( pTerm->leftCursor != pSrc->iCursor ) continue;
132367 if( pTerm->prereqRight & mUnusable ) continue;
132368 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
132369 testcase( pTerm->eOperator & WO_IN );
132370 testcase( pTerm->eOperator & WO_IS );
132371 testcase( pTerm->eOperator & WO_ISNULL );
132372 testcase( pTerm->eOperator & WO_ALL );
132373 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
132374 if( pTerm->wtFlags & TERM_VNULL ) continue;
132375 assert( pTerm->u.leftColumn>=(-1) );
132376 pIdxCons[j].iColumn = pTerm->u.leftColumn;
132378 op = (u8)pTerm->eOperator & WO_ALL;
132381 op = pTerm->eMatchOp;
132393 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
132396 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
132406 Expr *pExpr = pOrderBy->a[i].pExpr;
132407 pIdxOrderBy[i].iColumn = pExpr->iColumn;
132408 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
132422 ** non-zero value is returned. Otherwise, 0 is returned and the output
132426 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
132430 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
132434 rc = pVtab->pModule->xBestIndex(pVtab, p);
132439 sqlite3OomFault(pParse->db);
132440 }else if( !pVtab->zErrMsg ){
132443 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
132446 sqlite3_free(pVtab->zErrMsg);
132447 pVtab->zErrMsg = 0;
132452 for(i=0; i<p->nConstraint; i++){
132453 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
132455 "table %s: xBestIndex returned an invalid plan", pTab->zName);
132460 return pParse->nErr;
132474 ** into the aSample[] array - it is an index into a virtual set of samples
132485 IndexSample *aSample = pIdx->aSample;
132499 assert( pIdx->nSample>0 );
132500 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
132512 ** aSample[0] = (a, 5)
132514 ** aSample[2] = (b, 5)
132523 ** 1: (a, 5)
132527 ** 5: (b, 5)
132546 nField = pRec->nField;
132548 iSample = pIdx->nSample * nField;
132560 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
132566 pRec->nField = n;
132569 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
132572 iLower = aSample[iSamp].anLt[n-1];
132574 res = -1;
132577 iCol = n-1;
132586 if( pParse->db->mallocFailed==0 ){
132589 assert( i<pIdx->nSample );
132590 assert( iCol==nField-1 );
132591 pRec->nField = nField;
132593 || pParse->db->mallocFailed
132596 /* Unless i==pIdx->nSample, indicating that pRec is larger than
132599 assert( i<=pIdx->nSample && i>=0 );
132600 pRec->nField = iCol+1;
132601 assert( i==pIdx->nSample
132603 || pParse->db->mallocFailed );
132608 ** If (i>0), then pRec must also be greater than sample (i-1). */
132610 pRec->nField = iCol;
132612 || pParse->db->mallocFailed );
132615 pRec->nField = nField;
132616 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
132617 || pParse->db->mallocFailed );
132625 assert( iCol==nField-1 );
132630 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
132633 if( i>=pIdx->nSample ){
132634 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
132642 iGap = iUpper - iLower;
132650 aStat[1] = pIdx->aAvgEq[nField-1];
132653 /* Restore the pRec->nField value before returning. */
132654 pRec->nField = nField;
132673 if( pTerm->truthProb<=0 ){
132674 nRet += pTerm->truthProb;
132675 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
132676 nRet -= 20; assert( 20==sqlite3LogEst(4) );
132688 assert( iCol>=0 && iCol<pIdx->nColumn );
132689 if( !pIdx->zColAff ){
132692 return pIdx->zColAff[iCol];
132700 ** range-scan on a skip-scan index. For example:
132705 ** Value pLoop->nOut is currently set to the estimated number of rows
132716 ** N is the total number of samples, the pLoop->nOut value is adjusted
132719 ** nOut = nOut * ( min(U - L, 1) / N )
132740 Index *p = pLoop->u.btree.pIndex;
132741 int nEq = pLoop->u.btree.nEq;
132742 sqlite3 *db = pParse->db;
132743 int nLower = -1;
132744 int nUpper = p->nSample+1;
132753 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
132755 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
132759 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
132760 nUpper = p2 ? 0 : p->nSample;
132766 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
132767 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
132777 nDiff = (nUpper - nLower);
132786 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
132787 pLoop->nOut -= nAdjust;
132789 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
132790 nLower, nUpper, nAdjust*-1, pLoop->nOut));
132820 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
132828 ** left-most column of the index). Or, if the query is:
132853 int nOut = pLoop->nOut;
132857 Index *p = pLoop->u.btree.pIndex;
132858 int nEq = pLoop->u.btree.nEq;
132860 if( p->nSample>0 && nEq<p->nSampleCol ){
132861 if( nEq==pBuilder->nRecValid ){
132862 UnpackedRecord *pRec = pBuilder->pRec;
132864 int nBtm = pLoop->u.btree.nBtm;
132865 int nTop = pLoop->u.btree.nTop;
132870 ** key-prefix formed by the nEq values matched against the nEq left-most
132884 ** The number of rows between the two bounds is then just iUpper-iLower.
132888 int iLwrIdx = -2; /* aSample[] for the lower bound */
132889 int iUprIdx = -1; /* aSample[] for the upper bound */
132892 testcase( pRec->nField!=pBuilder->nRecValid );
132893 pRec->nField = pBuilder->nRecValid;
132898 iUpper = p->nRowEst0;
132900 /* Note: this call could be optimized away - since the same values must
132907 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
132908 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
132909 assert( p->aSortOrder!=0 );
132910 if( p->aSortOrder[nEq] ){
132919 Expr *pExpr = pLower->pExpr->pRight;
132926 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
132928 nOut--;
132936 Expr *pExpr = pUpper->pExpr->pRight;
132943 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
132945 nOut--;
132950 pBuilder->pRec = pRec;
132953 nNew = sqlite3LogEst(iUpper - iLower);
132958 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
132979 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
132984 ** has an application-defined likelihood(), assume the range is
132985 ** reduced by an additional 75%. This means that, by default, an open-ended
132989 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
132990 nNew -= 20;
132993 nOut -= (pLower!=0) + (pUpper!=0);
132997 if( pLoop->nOut>nOut ){
132999 pLoop->nOut, nOut));
133002 pLoop->nOut = (LogEst)nOut;
133010 ** the histogram data. This only works when x is the left-most
133017 ** non-zero.
133030 Index *p = pBuilder->pNew->u.btree.pIndex;
133031 int nEq = pBuilder->pNew->u.btree.nEq;
133032 UnpackedRecord *pRec = pBuilder->pRec;
133038 assert( nEq<=p->nColumn );
133039 assert( p->aSample!=0 );
133040 assert( p->nSample>0 );
133041 assert( pBuilder->nRecValid<nEq );
133045 if( pBuilder->nRecValid<(nEq-1) ){
133051 if( nEq>=p->nColumn ){
133056 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
133057 pBuilder->pRec = pRec;
133060 pBuilder->nRecValid = nEq;
133064 p->zName, nEq-1, (int)a[1]));
133074 ** an IN constraint where the right-hand side of the IN operator
133081 ** non-zero.
133094 Index *p = pBuilder->pNew->u.btree.pIndex;
133095 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
133096 int nRecValid = pBuilder->nRecValid;
133102 assert( p->aSample!=0 );
133103 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
133105 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
133107 pBuilder->nRecValid = nRecValid;
133115 assert( pBuilder->nRecValid==nRecValid );
133127 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
133132 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
133133 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
133134 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
133135 if( pTerm->eOperator & WO_SINGLE ){
133137 pTerm->leftCursor, pTerm->u.leftColumn);
133138 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
133140 pTerm->u.pOrInfo->indexable);
133142 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
133145 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
133146 iTerm, pTerm, zType, zLeft, pTerm->truthProb,
133147 pTerm->eOperator, pTerm->wtFlags);
133148 if( pTerm->iField ){
133149 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
133153 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
133164 for(i=0; i<pWC->nTerm; i++){
133165 whereTermPrint(&pWC->a[i], i);
133175 WhereInfo *pWInfo = pWC->pWInfo;
133176 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
133177 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
133178 Table *pTab = pItem->pTab;
133179 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
133180 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
133181 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
133183 pItem->zAlias ? pItem->zAlias : pTab->zName);
133184 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
133186 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
133188 int i = sqlite3Strlen30(zName) - 1;
133189 while( zName[i]!='_' ) i--;
133192 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
133198 if( p->u.vtab.idxStr ){
133200 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
133202 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
133204 sqlite3DebugPrintf(" %-19s", z);
133207 if( p->wsFlags & WHERE_SKIPSCAN ){
133208 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
133210 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
133212 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
133213 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
133215 for(i=0; i<p->nLTerm; i++){
133216 whereTermPrint(p->aLTerm[i], i);
133227 p->aLTerm = p->aLTermSpace;
133228 p->nLTerm = 0;
133229 p->nLSlot = ArraySize(p->aLTermSpace);
133230 p->wsFlags = 0;
133237 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
133238 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
133239 sqlite3_free(p->u.vtab.idxStr);
133240 p->u.vtab.needFree = 0;
133241 p->u.vtab.idxStr = 0;
133242 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
133243 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
133244 sqlite3DbFreeNN(db, p->u.btree.pIndex);
133245 p->u.btree.pIndex = 0;
133254 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
133260 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
133264 if( p->nLSlot>=n ) return SQLITE_OK;
133266 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
133268 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
133269 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
133270 p->aLTerm = paNew;
133271 p->nLSlot = n;
133280 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
133281 memset(&pTo->u, 0, sizeof(pTo->u));
133285 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
133286 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
133287 pFrom->u.vtab.needFree = 0;
133288 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
133289 pFrom->u.btree.pIndex = 0;
133308 for(i=0; i<pWInfo->nLevel; i++){
133309 WhereLevel *pLevel = &pWInfo->a[i];
133310 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
133311 sqlite3DbFree(db, pLevel->u.in.aInLoop);
133314 sqlite3WhereClauseClear(&pWInfo->sWC);
133315 while( pWInfo->pLoops ){
133316 WhereLoop *p = pWInfo->pLoops;
133317 pWInfo->pLoops = p->pNextLoop;
133338 ** was added because if X uses skip-scan less than Y it still might
133346 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
133349 if( pY->nSkip > pX->nSkip ) return 0;
133350 if( pX->rRun >= pY->rRun ){
133351 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
133352 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
133354 for(i=pX->nLTerm-1; i>=0; i--){
133355 if( pX->aLTerm[i]==0 ) continue;
133356 for(j=pY->nLTerm-1; j>=0; j--){
133357 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
133379 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
133380 for(; p; p=p->pNextLoop){
133381 if( p->iTab!=pTemplate->iTab ) continue;
133382 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
133387 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
133388 pTemplate->rRun = p->rRun;
133389 pTemplate->nOut = p->nOut - 1;
133394 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
133395 pTemplate->rRun = p->rRun;
133396 pTemplate->nOut = p->nOut + 1;
133420 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
133421 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
133430 assert( p->rSetup==0 || pTemplate->rSetup==0
133431 || p->rSetup==pTemplate->rSetup );
133435 ** rSetup. Call this SETUP-INVARIANT */
133436 assert( p->rSetup>=pTemplate->rSetup );
133438 /* Any loop using an appliation-defined index (or PRIMARY KEY or
133440 ** than an automatic index. Unless it is a skip-scan. */
133441 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
133442 && (pTemplate->nSkip)==0
133443 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
133444 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
133445 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
133455 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
133456 && p->rSetup<=pTemplate->rSetup /* (2a) */
133457 && p->rRun<=pTemplate->rRun /* (2b) */
133458 && p->nOut<=pTemplate->nOut /* (2c) */
133468 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
133469 && p->rRun>=pTemplate->rRun /* (2a) */
133470 && p->nOut>=pTemplate->nOut /* (2b) */
133472 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
133488 ** If pBuilder->pOrSet is not NULL then we care about only the
133490 ** information is gathered in the pBuilder->pOrSet object. This special
133493 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
133505 WhereInfo *pWInfo = pBuilder->pWInfo;
133506 sqlite3 *db = pWInfo->pParse->db;
133509 /* If pBuilder->pOrSet is defined, then only keep track of the costs
133512 if( pBuilder->pOrSet!=0 ){
133513 if( pTemplate->nLTerm ){
133515 u16 n = pBuilder->pOrSet->n;
133518 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
133519 pTemplate->nOut);
133522 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
133523 whereLoopPrint(pTemplate, pBuilder->pWC);
133532 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
133533 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
133541 whereLoopPrint(pTemplate, pBuilder->pWC);
133557 whereLoopPrint(p, pBuilder->pWC);
133562 whereLoopPrint(pTemplate, pBuilder->pWC);
133570 p->pNextLoop = 0;
133575 WhereLoop **ppTail = &p->pNextLoop;
133582 *ppTail = pToDel->pNextLoop;
133586 whereLoopPrint(pToDel, pBuilder->pWC);
133593 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
133594 Index *pIndex = p->u.btree.pIndex;
133595 if( pIndex && pIndex->tnum==0 ){
133596 p->u.btree.pIndex = 0;
133615 ** TODO --> Perhaps this is something that could be improved by better
133619 ** value corresponds to -1 in LogEst notation, so this means decrement
133626 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
133627 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
133637 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
133639 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
133641 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
133642 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
133643 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
133644 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
133645 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
133646 for(j=pLoop->nLTerm-1; j>=0; j--){
133647 pX = pLoop->aLTerm[j];
133650 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
133653 if( pTerm->truthProb<=0 ){
133656 pLoop->nOut += pTerm->truthProb;
133660 pLoop->nOut--;
133661 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
133662 Expr *pRight = pTerm->pExpr->pRight;
133663 testcase( pTerm->pExpr->op==TK_IS );
133664 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
133674 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
133701 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
133704 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
133711 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
133712 Expr *pRhs = pTerm->pExpr->pRight;
133713 if( pRhs->flags & EP_xIsSelect ){
133714 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
133716 pRhs = pRhs->x.pList->a[i].pExpr;
133723 if( pLhs->op!=TK_COLUMN
133724 || pLhs->iTable!=iCur
133725 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
133726 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
133731 testcase( pLhs->iColumn==XN_ROWID );
133733 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
133738 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
133745 ** compiled with -DSQLITE_ENABLE_COSTMULT
133754 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
133757 ** When this function is called, pBuilder->pNew->nOut contains the
133762 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
133771 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
133772 Parse *pParse = pWInfo->pParse; /* Parsing context */
133773 sqlite3 *db = pParse->db; /* Database connection malloc context */
133778 Bitmask saved_prereq; /* Original value of pNew->prereq */
133779 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
133780 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
133781 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
133782 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
133783 u16 saved_nSkip; /* Original value of pNew->nSkip */
133784 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
133785 LogEst saved_nOut; /* Original value of pNew->nOut */
133791 pNew = pBuilder->pNew;
133792 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
133794 pProbe->zName, pNew->u.btree.nEq));
133796 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
133797 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
133798 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
133801 assert( pNew->u.btree.nBtm==0 );
133804 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
133806 assert( pNew->u.btree.nEq<pProbe->nColumn );
133808 saved_nEq = pNew->u.btree.nEq;
133809 saved_nBtm = pNew->u.btree.nBtm;
133810 saved_nTop = pNew->u.btree.nTop;
133811 saved_nSkip = pNew->nSkip;
133812 saved_nLTerm = pNew->nLTerm;
133813 saved_wsFlags = pNew->wsFlags;
133814 saved_prereq = pNew->prereq;
133815 saved_nOut = pNew->nOut;
133816 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
133818 pNew->rSetup = 0;
133819 rSize = pProbe->aiRowLogEst[0];
133822 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
133827 int nRecValid = pBuilder->nRecValid;
133829 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
133834 if( pTerm->prereqRight & pNew->maskSelf ) continue;
133838 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
133843 if( (pSrc->fg.jointype & JT_LEFT)!=0
133844 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
133852 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
133853 pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
133855 pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
133857 pNew->wsFlags = saved_wsFlags;
133858 pNew->u.btree.nEq = saved_nEq;
133859 pNew->u.btree.nBtm = saved_nBtm;
133860 pNew->u.btree.nTop = saved_nTop;
133861 pNew->nLTerm = saved_nLTerm;
133862 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
133863 pNew->aLTerm[pNew->nLTerm++] = pTerm;
133864 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
133867 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
133868 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
133869 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
133873 Expr *pExpr = pTerm->pExpr;
133874 pNew->wsFlags |= WHERE_COLUMN_IN;
133885 for(i=0; i<pNew->nLTerm-1; i++){
133886 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
133888 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
133890 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
133895 int iCol = pProbe->aiColumn[saved_nEq];
133896 pNew->wsFlags |= WHERE_COLUMN_EQ;
133897 assert( saved_nEq==pNew->u.btree.nEq );
133899 || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
133901 if( iCol>=0 && pProbe->uniqNotNull==0 ){
133902 pNew->wsFlags |= WHERE_UNQ_WANTED;
133904 pNew->wsFlags |= WHERE_ONEROW;
133908 pNew->wsFlags |= WHERE_COLUMN_NULL;
133912 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
133913 pNew->u.btree.nBtm = whereRangeVectorLen(
133914 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
133918 if( pTerm->wtFlags & TERM_LIKEOPT ){
133922 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
133923 assert( pTop->wtFlags & TERM_LIKEOPT );
133924 assert( pTop->eOperator==WO_LT );
133925 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
133926 pNew->aLTerm[pNew->nLTerm++] = pTop;
133927 pNew->wsFlags |= WHERE_TOP_LIMIT;
133928 pNew->u.btree.nTop = 1;
133934 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
133935 pNew->u.btree.nTop = whereRangeVectorLen(
133936 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
133939 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
133940 pNew->aLTerm[pNew->nLTerm-2] : 0;
133943 /* At this point pNew->nOut is set to the number of rows expected to
133947 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
133948 assert( pNew->nOut==saved_nOut );
133949 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
133954 int nEq = ++pNew->u.btree.nEq;
133957 assert( pNew->nOut==saved_nOut );
133958 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
133961 pNew->nOut += pTerm->truthProb;
133962 pNew->nOut -= nIn;
133967 && pProbe->nSample
133968 && pNew->u.btree.nEq<=pProbe->nSampleCol
133969 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
133971 Expr *pExpr = pTerm->pExpr;
133976 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
133978 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
133983 pNew->nOut = sqlite3LogEst(nOut);
133984 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
133985 pNew->nOut -= nIn;
133991 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
133996 pNew->nOut += 10;
134003 ** it to pNew->rRun, which is currently set to the cost of the index
134004 ** seek only. Then, if this is a non-covering index, add the cost of
134006 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
134007 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
134008 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
134009 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
134011 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
134013 nOutUnadjusted = pNew->nOut;
134014 pNew->rRun += nInMul + nIn;
134015 pNew->nOut += nInMul + nIn;
134016 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
134019 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
134020 pNew->nOut = saved_nOut;
134022 pNew->nOut = nOutUnadjusted;
134025 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
134026 && pNew->u.btree.nEq<pProbe->nColumn
134030 pNew->nOut = saved_nOut;
134032 pBuilder->nRecValid = nRecValid;
134035 pNew->prereq = saved_prereq;
134036 pNew->u.btree.nEq = saved_nEq;
134037 pNew->u.btree.nBtm = saved_nBtm;
134038 pNew->u.btree.nTop = saved_nTop;
134039 pNew->nSkip = saved_nSkip;
134040 pNew->wsFlags = saved_wsFlags;
134041 pNew->nOut = saved_nOut;
134042 pNew->nLTerm = saved_nLTerm;
134044 /* Consider using a skip-scan if there are no WHERE clause constraints
134045 ** available for the left-most terms of the index, and if the average
134046 ** number of repeats in the left-most terms is at least 18.
134056 && saved_nEq+1<pProbe->nKeyCol
134057 && pProbe->noSkipScan==0
134058 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
134059 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
134062 pNew->u.btree.nEq++;
134063 pNew->nSkip++;
134064 pNew->aLTerm[pNew->nLTerm++] = 0;
134065 pNew->wsFlags |= WHERE_SKIPSCAN;
134066 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
134067 pNew->nOut -= nIter;
134068 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
134069 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
134070 nIter += 5;
134072 pNew->nOut = saved_nOut;
134073 pNew->u.btree.nEq = saved_nEq;
134074 pNew->nSkip = saved_nSkip;
134075 pNew->wsFlags = saved_wsFlags;
134079 pProbe->zName, saved_nEq, rc));
134100 if( pIndex->bUnordered ) return 0;
134101 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
134102 for(ii=0; ii<pOB->nExpr; ii++){
134103 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
134104 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
134105 if( pExpr->iColumn<0 ) return 1;
134106 for(jj=0; jj<pIndex->nKeyCol; jj++){
134107 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
134109 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
134110 for(jj=0; jj<pIndex->nKeyCol; jj++){
134111 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
134112 if( sqlite3ExprCompare(0, pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
134128 for(j=pIdx->nColumn-1; j>=0; j--){
134129 int x = pIdx->aiColumn[j];
134131 testcase( x==BMS-1 );
134132 testcase( x==BMS-2 );
134133 if( x<BMS-1 ) m |= MASKBIT(x);
134145 Parse *pParse = pWC->pWInfo->pParse;
134146 while( pWhere->op==TK_AND ){
134147 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
134148 pWhere = pWhere->pRight;
134150 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
134151 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
134152 Expr *pExpr = pTerm->pExpr;
134153 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
134164 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
134165 ** a b-tree table, not a virtual table.
134167 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
134172 ** cost = nRow * 3.0 // full-table scan
134174 ** cost = nRow * (K+3.0) // scan of non-covering index
134181 ** the index b-tree:
134184 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
134188 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
134193 ** log(nRow) factor is omitted from a non-covering index scan in order to
134194 ** bias the scoring in favor of using an index, since the worst-case
134195 ** performance of using an index is far better than the worst-case performance
134206 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
134218 pNew = pBuilder->pNew;
134219 pWInfo = pBuilder->pWInfo;
134220 pTabList = pWInfo->pTabList;
134221 pSrc = pTabList->a + pNew->iTab;
134222 pTab = pSrc->pTab;
134223 pWC = pBuilder->pWC;
134224 assert( !IsVirtual(pSrc->pTab) );
134226 if( pSrc->pIBIndex ){
134228 pProbe = pSrc->pIBIndex;
134230 pProbe = pTab->pIndex;
134244 sPk.szIdxRow = pTab->szTabRow;
134245 aiRowEstPk[0] = pTab->nRowLogEst;
134247 pFirst = pSrc->pTab->pIndex;
134248 if( pSrc->fg.notIndexed==0 ){
134255 rSize = pTab->nRowLogEst;
134260 if( !pBuilder->pOrSet /* Not part of an OR optimization */
134261 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
134262 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
134263 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
134264 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
134266 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
134267 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
134269 /* Generate auto-index WhereLoops */
134271 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
134272 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
134273 if( pTerm->prereqRight & pNew->maskSelf ) continue;
134275 pNew->u.btree.nEq = 1;
134276 pNew->nSkip = 0;
134277 pNew->u.btree.pIndex = 0;
134278 pNew->nLTerm = 1;
134279 pNew->aLTerm[0] = pTerm;
134280 /* TUNING: One-time cost for computing the automatic index is
134288 pNew->rSetup = rLogSize + rSize + 4;
134289 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
134290 pNew->rSetup += 24;
134292 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
134293 if( pNew->rSetup<0 ) pNew->rSetup = 0;
134298 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
134299 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
134300 pNew->wsFlags = WHERE_AUTO_INDEX;
134301 pNew->prereq = mPrereq | pTerm->prereqRight;
134310 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
134311 if( pProbe->pPartIdxWhere!=0
134312 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
134313 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
134316 rSize = pProbe->aiRowLogEst[0];
134317 pNew->u.btree.nEq = 0;
134318 pNew->u.btree.nBtm = 0;
134319 pNew->u.btree.nTop = 0;
134320 pNew->nSkip = 0;
134321 pNew->nLTerm = 0;
134322 pNew->iSortIdx = 0;
134323 pNew->rSetup = 0;
134324 pNew->prereq = mPrereq;
134325 pNew->nOut = rSize;
134326 pNew->u.btree.pIndex = pProbe;
134327 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
134329 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
134330 if( pProbe->tnum<=0 ){
134332 pNew->wsFlags = WHERE_IPK;
134335 pNew->iSortIdx = b ? iSortIdx : 0;
134337 pNew->rRun = rSize + 16;
134338 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
134341 pNew->nOut = rSize;
134345 if( pProbe->isCovering ){
134346 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
134349 m = pSrc->colUsed & ~columnsInIndex(pProbe);
134350 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
134356 || pProbe->pPartIdxWhere!=0
134358 && pProbe->bUnordered==0
134359 && (pProbe->szIdxRow<pTab->szTabRow)
134360 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
134362 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
134365 pNew->iSortIdx = b ? iSortIdx : 0;
134370 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
134372 /* If this is a non-covering index scan, add in the cost of
134379 int iCur = pSrc->iCursor;
134380 WhereClause *pWC2 = &pWInfo->sWC;
134381 for(ii=0; ii<pWC2->nTerm; ii++){
134382 WhereTerm *pTerm = &pWC2->a[ii];
134383 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
134388 if( pTerm->truthProb<=0 ){
134389 nLookup += pTerm->truthProb;
134391 nLookup--;
134392 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
134396 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
134398 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
134401 pNew->nOut = rSize;
134406 pBuilder->bldFlags = 0;
134408 if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
134409 /* If a non-unique index is used, or if a prefix of the key for
134410 ** unique index is used (making the index functionally non-unique)
134413 pTab->tabFlags |= TF_StatsUsed;
134416 sqlite3Stat4ProbeFree(pBuilder->pRec);
134417 pBuilder->nRecValid = 0;
134418 pBuilder->pRec = 0;
134423 if( pSrc->pIBIndex ) break;
134432 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
134459 WhereClause *pWC = pBuilder->pWC;
134461 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
134465 WhereLoop *pNew = pBuilder->pNew;
134466 Parse *pParse = pBuilder->pWInfo->pParse;
134467 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
134468 int nConstraint = pIdxInfo->nConstraint;
134472 pNew->prereq = mPrereq;
134476 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
134478 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
134479 pIdxCons->usable = 0;
134480 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
134481 && (pTerm->eOperator & mExclude)==0
134483 pIdxCons->usable = 1;
134489 assert( pIdxInfo->needToFreeIdxStr==0 );
134490 pIdxInfo->idxStr = 0;
134491 pIdxInfo->idxNum = 0;
134492 pIdxInfo->orderByConsumed = 0;
134493 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
134494 pIdxInfo->estimatedRows = 25;
134495 pIdxInfo->idxFlags = 0;
134496 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
134499 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
134502 mxTerm = -1;
134503 assert( pNew->nLSlot>=nConstraint );
134504 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
134505 pNew->u.vtab.omitMask = 0;
134506 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
134509 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
134511 int j = pIdxCons->iTermOffset;
134514 || j>=pWC->nTerm
134515 || pNew->aLTerm[iTerm]!=0
134516 || pIdxCons->usable==0
134519 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
134522 testcase( iTerm==nConstraint-1 );
134524 testcase( j==pWC->nTerm-1 );
134525 pTerm = &pWC->a[j];
134526 pNew->prereq |= pTerm->prereqRight;
134527 assert( iTerm<pNew->nLSlot );
134528 pNew->aLTerm[iTerm] = pTerm;
134532 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
134533 if( (pTerm->eOperator & WO_IN)!=0 ){
134539 pIdxInfo->orderByConsumed = 0;
134540 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
134545 pNew->u.vtab.omitMask &= ~mNoOmit;
134547 pNew->nLTerm = mxTerm+1;
134548 assert( pNew->nLTerm<=pNew->nLSlot );
134549 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
134550 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
134551 pIdxInfo->needToFreeIdxStr = 0;
134552 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
134553 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
134554 pIdxInfo->nOrderBy : 0);
134555 pNew->rSetup = 0;
134556 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
134557 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
134561 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
134562 pNew->wsFlags |= WHERE_ONEROW;
134564 pNew->wsFlags &= ~WHERE_ONEROW;
134567 if( pNew->u.vtab.needFree ){
134568 sqlite3_free(pNew->u.vtab.idxStr);
134569 pNew->u.vtab.needFree = 0;
134573 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
134581 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
134602 ** mUnusable should always be configured as "not-usable" for xBestIndex.
134622 pWInfo = pBuilder->pWInfo;
134623 pParse = pWInfo->pParse;
134624 pWC = pBuilder->pWC;
134625 pNew = pBuilder->pNew;
134626 pSrc = &pWInfo->pTabList->a[pNew->iTab];
134627 assert( IsVirtual(pSrc->pTab) );
134628 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
134631 pNew->rSetup = 0;
134632 pNew->wsFlags = WHERE_VIRTUALTABLE;
134633 pNew->nLTerm = 0;
134634 pNew->u.vtab.needFree = 0;
134635 nConstraint = p->nConstraint;
134636 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
134637 sqlite3DbFree(pParse->db, p);
134650 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
134663 mBestNoIn = pNew->prereq & ~mPrereq;
134678 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
134689 if( pNew->prereq==mPrereq ){
134715 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
134716 sqlite3DbFreeNN(pParse->db, p);
134730 WhereInfo *pWInfo = pBuilder->pWInfo;
134741 pWC = pBuilder->pWC;
134742 pWCEnd = pWC->a + pWC->nTerm;
134743 pNew = pBuilder->pNew;
134745 pItem = pWInfo->pTabList->a + pNew->iTab;
134746 iCur = pItem->iCursor;
134748 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
134749 if( (pTerm->eOperator & WO_OR)!=0
134750 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
134752 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
134753 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
134762 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
134763 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
134764 if( (pOrTerm->eOperator & WO_AND)!=0 ){
134765 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
134766 }else if( pOrTerm->leftCursor==iCur ){
134767 tempWC.pWInfo = pWC->pWInfo;
134778 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
134779 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
134785 if( IsVirtual(pItem->pTab) ){
134815 pNew->nLTerm = 1;
134816 pNew->aLTerm[0] = pTerm;
134817 pNew->wsFlags = WHERE_MULTI_OR;
134818 pNew->rSetup = 0;
134819 pNew->iSortIdx = 0;
134820 memset(&pNew->u, 0, sizeof(pNew->u));
134823 ** of all sub-scans required by the OR-scan. However, due to rounding
134824 ** errors, it may be that the cost of the OR-scan is equal to its
134825 ** most expensive sub-scan. Add the smallest possible penalty
134832 ** the planner may elect to "OR" together a full-table scan and an
134834 pNew->rRun = sSum.a[i].rRun + 1;
134835 pNew->nOut = sSum.a[i].nOut;
134836 pNew->prereq = sSum.a[i].prereq;
134839 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
134849 WhereInfo *pWInfo = pBuilder->pWInfo;
134853 SrcList *pTabList = pWInfo->pTabList;
134855 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
134856 sqlite3 *db = pWInfo->pParse->db;
134862 pNew = pBuilder->pNew;
134864 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
134866 pNew->iTab = iTab;
134867 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
134868 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
134870 ** right-hand-side of a LEFT or CROSS JOIN. */
134873 priorJointype = pItem->fg.jointype;
134875 if( IsVirtual(pItem->pTab) ){
134878 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
134879 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
134891 mPrior |= pNew->maskSelf;
134892 if( rc || db->mallocFailed ) break;
134914 ** pOrderBy terms must be matched in strict left-to-right order.
134921 u16 nLoop, /* Number of entries in pPath->aLoop[] */
134922 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
134928 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
134944 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
134947 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
134951 ** We say the WhereLoop is "one-row" if it generates no more than one
134952 ** row of output. A WhereLoop is one-row if all of the following are true:
134955 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
134956 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
134958 ** We say the WhereLoop is "order-distinct" if the set of columns from
134960 ** row of the WhereLoop. Every one-row WhereLoop is automatically
134961 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
134962 ** is not order-distinct. To be order-distinct is not quite the same as being
134964 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
134965 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
134969 ** automatically order-distinct.
134975 nOrderBy = pOrderBy->nExpr;
134976 testcase( nOrderBy==BMS-1 );
134977 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
134979 obDone = MASKBIT(nOrderBy)-1;
134985 if( iLoop>0 ) ready |= pLoop->maskSelf;
134987 pLoop = pPath->aLoop[iLoop];
134992 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
134993 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
134996 pLoop->u.btree.nIdxCol = 0;
134998 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
135007 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
135008 if( pOBExpr->op!=TK_COLUMN ) continue;
135009 if( pOBExpr->iTable!=iCur ) continue;
135010 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
135013 if( pTerm->eOperator==WO_IN ){
135018 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
135019 if( j>=pLoop->nLTerm ) continue;
135021 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
135023 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
135024 if( !pColl ) pColl = db->pDfltColl;
135025 z1 = pColl->zName;
135026 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
135027 if( !pColl ) pColl = db->pDfltColl;
135028 z2 = pColl->zName;
135030 testcase( pTerm->pExpr->op==TK_IS );
135035 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
135036 if( pLoop->wsFlags & WHERE_IPK ){
135040 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
135043 nKeyCol = pIndex->nKeyCol;
135044 nColumn = pIndex->nColumn;
135045 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
135046 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
135047 || !HasRowid(pIndex->pTable));
135059 assert( j>=pLoop->u.btree.nEq
135060 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
135062 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
135063 u16 eOp = pLoop->aLTerm[j]->eOperator;
135081 ** j<pLoop->u.btree.nEq constraint above. Any equality other
135084 Expr *pX = pLoop->aLTerm[j]->pExpr;
135085 for(i=j+1; i<pLoop->u.btree.nEq; i++){
135086 if( pLoop->aLTerm[i]->pExpr==pX ){
135087 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
135096 ** (revIdx) for the j-th column of the index.
135099 iColumn = pIndex->aiColumn[j];
135100 revIdx = pIndex->aSortOrder[j];
135101 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
135108 ** WhereLoop is not well-ordered
135112 && j>=pLoop->u.btree.nEq
135113 && pIndex->pTable->aCol[iColumn].notNull==0
135118 /* Find the ORDER BY term that corresponds to the j-th column
135124 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
135128 if( iColumn>=(-1) ){
135129 if( pOBExpr->op!=TK_COLUMN ) continue;
135130 if( pOBExpr->iTable!=iCur ) continue;
135131 if( pOBExpr->iColumn!=iColumn ) continue;
135134 pOBExpr,pIndex->aColExpr->a[j].pExpr,iCur) ){
135139 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
135140 if( !pColl ) pColl = db->pDfltColl;
135141 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
135143 pLoop->u.btree.nIdxCol = j+1;
135151 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
135153 rev = revIdx ^ pOrderBy->a[i].sortOrder;
135177 } /* end-if not one-row */
135181 orderDistinctMask |= pLoop->maskSelf;
135186 p = pOrderBy->a[i].pExpr;
135187 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
135194 } /* End the loop over all WhereLoops from outer-most down to inner-most */
135197 for(i=nOrderBy-1; i>0; i--){
135198 Bitmask m = MASKBIT(i) - 1;
135203 return -1;
135210 ** BY clause - and so any order that groups rows as required satisfies the
135227 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
135228 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
135231 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
135232 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
135233 return pWInfo->sorted;
135241 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
135242 if( pLast ) zName[i++] = pLast->cId;
135264 ** Or, if the order-by clause has X terms but only the last Y
135265 ** terms are out of order, then block-sorting will reduce the
135274 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
135279 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
135280 nRow = pWInfo->iLimit;
135287 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
135289 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
135320 pParse = pWInfo->pParse;
135321 db = pParse->db;
135322 nLoop = pWInfo->nLevel;
135324 ** For 2-way joins, the 5 best paths are followed.
135326 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
135327 assert( nLoop<=pWInfo->pTabList->nSrc );
135328 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
135335 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
135338 nOrderBy = pWInfo->pOrderBy->nExpr;
135350 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
135351 pFrom->aLoop = pX;
135356 ** is either zero - meaning it has not yet been initialized - or the
135371 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
135379 ** -1, indicating that the result set may or may not be ordered,
135381 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
135390 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
135394 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
135396 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
135398 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
135399 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
135400 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
135408 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
135409 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
135410 nOut = pFrom->nRow + pWLoop->nOut;
135411 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
135414 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
135417 revMask = pFrom->revLoop;
135428 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
135429 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
135433 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
135437 ** mxChoice best-so-far paths.
135439 ** First look for an existing path among best-so-far paths
135443 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
135444 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
135445 ** of legal values for isOrdered, -1..64.
135448 if( pTo->maskLoop==maskNew
135449 && ((pTo->isOrdered^isOrdered)&0x80)==0
135451 testcase( jj==nTo-1 );
135456 /* None of the existing best-so-far paths match the candidate. */
135461 ** paths currently in the best-so-far buffer. So discard
135465 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
135473 ** needs to be added to the set of best-so-far paths. */
135484 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
135490 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
135496 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
135498 if( pTo->rCost<rCost
135499 || (pTo->rCost==rCost
135500 && (pTo->nRow<nOut
135501 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
135508 "Skip %s cost=%-3d,%3d,%3d order=%c",
135511 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
135512 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
135513 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
135517 testcase( pTo->rCost==rCost );
135520 testcase( pTo->rCost==rCost+1 );
135526 "Update %s cost=%-3d,%3d,%3d order=%c",
135529 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
135530 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
135531 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
135536 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
135537 pTo->revLoop = revMask;
135538 pTo->nRow = nOut;
135539 pTo->rCost = rCost;
135540 pTo->rUnsorted = rUnsorted;
135541 pTo->isOrdered = isOrdered;
135542 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
135543 pTo->aLoop[iLoop] = pWLoop;
135549 if( pTo->rCost>mxCost
135550 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
135552 mxCost = pTo->rCost;
135553 mxUnsorted = pTo->rUnsorted;
135563 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
135565 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
135566 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
135567 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
135568 if( pTo->isOrdered>0 ){
135569 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
135593 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
135595 assert( pWInfo->nLevel==nLoop );
135598 WhereLevel *pLevel = pWInfo->a + iLoop;
135599 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
135600 pLevel->iFrom = pWLoop->iTab;
135601 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
135603 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
135604 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
135605 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
135609 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
135610 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
135611 if( rc==pWInfo->pResultSet->nExpr ){
135612 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
135615 if( pWInfo->pOrderBy ){
135616 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
135617 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
135618 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
135621 pWInfo->nOBSat = pFrom->isOrdered;
135622 pWInfo->revMask = pFrom->revLoop;
135623 if( pWInfo->nOBSat<=0 ){
135624 pWInfo->nOBSat = 0;
135626 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
135631 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
135632 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
135635 if( rc==pWInfo->pOrderBy->nExpr ){
135636 pWInfo->bOrderedInnerLoop = 1;
135637 pWInfo->revMask = m;
135643 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
135644 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
135647 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
135648 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
135650 assert( pWInfo->sorted==0 );
135651 if( nOrder==pWInfo->pOrderBy->nExpr ){
135652 pWInfo->sorted = 1;
135653 pWInfo->revMask = revMask;
135659 pWInfo->nRowOut = pFrom->nRow;
135670 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
135673 ** Return non-zero on success, if this query can be handled by this
135674 ** no-frills query planner. Return zero if this query needs the
135675 ** general-purpose query planner.
135688 pWInfo = pBuilder->pWInfo;
135689 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
135690 assert( pWInfo->pTabList->nSrc>=1 );
135691 pItem = pWInfo->pTabList->a;
135692 pTab = pItem->pTab;
135694 if( pItem->fg.isIndexedBy ) return 0;
135695 iCur = pItem->iCursor;
135696 pWC = &pWInfo->sWC;
135697 pLoop = pBuilder->pNew;
135698 pLoop->wsFlags = 0;
135699 pLoop->nSkip = 0;
135700 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
135702 testcase( pTerm->eOperator & WO_IS );
135703 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
135704 pLoop->aLTerm[0] = pTerm;
135705 pLoop->nLTerm = 1;
135706 pLoop->u.btree.nEq = 1;
135708 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
135710 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
135712 assert( pLoop->aLTermSpace==pLoop->aLTerm );
135714 || pIdx->pPartIdxWhere!=0
135715 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
135717 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
135718 for(j=0; j<pIdx->nKeyCol; j++){
135721 testcase( pTerm->eOperator & WO_IS );
135722 pLoop->aLTerm[j] = pTerm;
135724 if( j!=pIdx->nKeyCol ) continue;
135725 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
135726 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
135727 pLoop->wsFlags |= WHERE_IDX_ONLY;
135729 pLoop->nLTerm = j;
135730 pLoop->u.btree.nEq = j;
135731 pLoop->u.btree.pIndex = pIdx;
135733 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
135737 if( pLoop->wsFlags ){
135738 pLoop->nOut = (LogEst)1;
135739 pWInfo->a[0].pWLoop = pLoop;
135740 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
135741 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
135742 pWInfo->a[0].iTabCur = iCur;
135743 pWInfo->nRowOut = 1;
135744 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
135745 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
135746 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
135749 pLoop->cId = '0';
135760 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
135761 pWalker->eCode = 0;
135768 ** Return true if the expression contains no non-deterministic SQL
135769 ** functions. Do not consider non-deterministic SQL functions that are
135770 ** part of sub-select statements.
135800 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
135804 ** end |-- by sqlite3WhereEnd()
135811 ** scanning through all values on the right-hand side of the IN.
135814 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
135824 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
135834 ** inner loops (or around the "..." if the test occurs within the inner-
135882 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
135886 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
135903 db = pParse->db;
135907 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
135908 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
135920 testcase( pTabList->nSrc==BMS );
135921 if( pTabList->nSrc>BMS ){
135931 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
135936 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
135937 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
135940 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
135942 if( db->mallocFailed ){
135947 pWInfo->pParse = pParse;
135948 pWInfo->pTabList = pTabList;
135949 pWInfo->pOrderBy = pOrderBy;
135950 pWInfo->pWhere = pWhere;
135951 pWInfo->pResultSet = pResultSet;
135952 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
135953 pWInfo->nLevel = nTabList;
135954 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
135955 pWInfo->wctrlFlags = wctrlFlags;
135956 pWInfo->iLimit = iAuxArg;
135957 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
135958 memset(&pWInfo->nOBSat, 0,
135959 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
135960 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
135961 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
135962 pMaskSet = &pWInfo->sMaskSet;
135964 sWLB.pWC = &pWInfo->sWC;
135969 sWLB.pNew->cId = '*';
135976 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
135977 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
135982 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
135984 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
135990 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
135993 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
135997 ** Note that bitmasks are created for all pTabList->nSrc tables in
135999 ** equal to pTabList->nSrc but might be shortened to 1 if the
136002 for(ii=0; ii<pTabList->nSrc; ii++){
136003 createMask(pMaskSet, pTabList->a[ii].iCursor);
136004 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
136009 for(ii=0; ii<pTabList->nSrc; ii++){
136010 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
136018 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
136019 if( db->mallocFailed ) goto whereBeginError;
136025 ** Do not do this if the expression contains non-deterministic functions
136026 ** that are not within a sub-select. This is not strictly required, but
136029 ** FROM ... WHERE random()>0; -- eval random() once per row
136030 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
136032 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
136033 WhereTerm *pT = &sWLB.pWC->a[ii];
136034 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
136035 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
136036 pT->wtFlags |= TERM_CODED;
136041 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
136043 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
136046 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
136047 pWInfo->pOrderBy = pResultSet;
136075 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
136076 p->cId = zLabel[i%(sizeof(zLabel)-1)];
136083 if( db->mallocFailed ) goto whereBeginError;
136084 if( pWInfo->pOrderBy ){
136085 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
136086 if( db->mallocFailed ) goto whereBeginError;
136089 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
136090 pWInfo->revMask = ALLBITS;
136092 if( pParse->nErr || NEVER(db->mallocFailed) ){
136097 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
136098 if( pWInfo->nOBSat>0 ){
136099 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
136101 switch( pWInfo->eDistinct ){
136116 for(ii=0; ii<pWInfo->nLevel; ii++){
136117 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
136122 if( pWInfo->nLevel>=2
136130 while( pWInfo->nLevel>=2 ){
136132 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
136133 if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break;
136135 && (pLoop->wsFlags & WHERE_ONEROW)==0
136139 if( (tabUsed & pLoop->maskSelf)!=0 ) break;
136140 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
136141 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
136142 if( (pTerm->prereqAll & pLoop->maskSelf)!=0
136143 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
136149 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
136150 pWInfo->nLevel--;
136151 nTabList--;
136155 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
136158 ** to use a one-pass algorithm, determine if this is appropriate.
136160 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
136162 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
136168 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
136169 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
136173 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
136181 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
136186 pTabItem = &pTabList->a[pLevel->iFrom];
136187 pTab = pTabItem->pTab;
136188 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136189 pLoop = pLevel->pWLoop;
136190 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
136194 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
136196 int iCur = pTabItem->iCursor;
136202 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
136205 if( pWInfo->eOnePass!=ONEPASS_OFF ){
136207 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
136209 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
136210 assert( pTabItem->iCursor==pLevel->iTabCur );
136211 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
136212 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
136213 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
136214 Bitmask b = pTabItem->colUsed;
136217 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
136218 assert( n<=pTab->nCol );
136221 if( pLoop->u.btree.pIndex!=0 ){
136229 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
136230 (const u8*)&pTabItem->colUsed, P4_INT64);
136233 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
136235 if( pLoop->wsFlags & WHERE_INDEXED ){
136236 Index *pIx = pLoop->u.btree.pIndex;
136240 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
136244 /* This is one term of an OR-optimization using the PRIMARY KEY of a
136246 iIndexCur = pLevel->iTabCur;
136248 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
136249 Index *pJ = pTabItem->pTab->pIndex;
136254 pJ = pJ->pNext;
136257 pWInfo->aiCurOnePass[1] = iIndexCur;
136262 iIndexCur = pParse->nTab++;
136264 pLevel->iIdxCur = iIndexCur;
136265 assert( pIx->pSchema==pTab->pSchema );
136268 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
136270 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
136271 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
136272 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
136273 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
136277 VdbeComment((v, "%s", pIx->zName));
136282 for(ii=0; ii<pIx->nColumn; ii++){
136283 jj = pIx->aiColumn[ii];
136286 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
136297 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
136298 if( db->mallocFailed ) goto whereBeginError;
136308 pLevel = &pWInfo->a[ii];
136309 wsFlags = pLevel->pWLoop->wsFlags;
136311 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
136312 constructAutomaticIndex(pParse, &pWInfo->sWC,
136313 &pTabList->a[pLevel->iFrom], notReady, pLevel);
136314 if( db->mallocFailed ) goto whereBeginError;
136318 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
136320 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
136322 pWInfo->iContinue = pLevel->addrCont;
136329 VdbeModuleComment((v, "Begin WHERE-core"));
136335 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
136346 Parse *pParse = pWInfo->pParse;
136347 Vdbe *v = pParse->pVdbe;
136351 SrcList *pTabList = pWInfo->pTabList;
136352 sqlite3 *db = pParse->db;
136356 VdbeModuleComment((v, "End WHERE-core"));
136358 for(i=pWInfo->nLevel-1; i>=0; i--){
136360 pLevel = &pWInfo->a[i];
136361 pLoop = pLevel->pWLoop;
136362 if( pLevel->op!=OP_Noop ){
136367 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
136368 && (pLoop->wsFlags & WHERE_INDEXED)!=0
136369 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
136370 && (n = pLoop->u.btree.nIdxCol)>0
136371 && pIdx->aiRowLogEst[n]>=36
136373 int r1 = pParse->nMem+1;
136376 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
136378 pParse->nMem += n+1;
136379 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
136380 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
136383 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
136387 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
136388 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
136389 sqlite3VdbeChangeP5(v, pLevel->p5);
136391 VdbeCoverageIf(v, pLevel->op==OP_Next);
136392 VdbeCoverageIf(v, pLevel->op==OP_Prev);
136393 VdbeCoverageIf(v, pLevel->op==OP_VNext);
136398 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
136400 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
136403 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
136404 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
136405 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
136406 if( pIn->eEndLoopOp!=OP_Noop ){
136407 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
136409 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
136410 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
136412 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
136415 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
136416 if( pLevel->addrSkip ){
136417 sqlite3VdbeGoto(v, pLevel->addrSkip);
136418 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
136419 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
136420 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
136423 if( pLevel->addrLikeRep ){
136424 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
136425 pLevel->addrLikeRep);
136429 if( pLevel->iLeftJoin ){
136430 int ws = pLoop->wsFlags;
136431 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
136434 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
136437 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
136439 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
136441 if( pLevel->op==OP_Return ){
136442 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
136444 sqlite3VdbeGoto(v, pLevel->addrFirst);
136448 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
136449 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
136455 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
136457 assert( pWInfo->nLevel<=pTabList->nSrc );
136458 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
136462 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
136463 Table *pTab = pTabItem->pTab;
136465 pLoop = pLevel->pWLoop;
136467 /* For a co-routine, change all OP_Column references to the table of
136468 ** the co-routine into OP_Copy of result contained in a register.
136471 if( pTabItem->fg.viaCoroutine ){
136472 testcase( pParse->db->mallocFailed );
136473 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
136474 pTabItem->regResult, 0);
136489 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
136490 pIdx = pLoop->u.btree.pIndex;
136491 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
136492 pIdx = pLevel->u.pCovidx;
136495 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
136496 && !db->mallocFailed
136499 k = pLevel->addrBody;
136502 if( pOp->p1!=pLevel->iTabCur ) continue;
136503 if( pOp->opcode==OP_Column ){
136504 int x = pOp->p2;
136505 assert( pIdx->pTable==pTab );
136508 x = pPk->aiColumn[x];
136513 pOp->p2 = x;
136514 pOp->p1 = pLevel->iIdxCur;
136516 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
136517 || pWInfo->eOnePass );
136518 }else if( pOp->opcode==OP_Rowid ){
136519 pOp->p1 = pLevel->iIdxCur;
136520 pOp->opcode = OP_IdxRowid;
136521 }else if( pOp->opcode==OP_IfNullRow ){
136522 pOp->p1 = pLevel->iIdxCur;
136530 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
136538 ** 2000-05-29
136552 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
136553 ** interstitial "-" characters) contained in this template is changed into
136567 ** Disable all error recovery processing in the parser push-down
136627 pParse->disableLookaside++;
136628 pParse->db->lookaside.bDisable++;
136633 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
136638 if( p->pPrior ){
136641 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
136642 pLoop->pNext = pNext;
136643 pLoop->selFlags |= SF_Compound;
136645 if( (p->selFlags & SF_MultiValue)==0 &&
136646 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
136659 pOut->zStart = pStart->z;
136660 pOut->zEnd = &pEnd->z[pEnd->n];
136668 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
136671 p->op = (u8)op;
136672 p->flags = EP_Leaf;
136673 p->iAgg = -1;
136674 p->u.zToken = (char*)&p[1];
136675 memcpy(p->u.zToken, t.z, t.n);
136676 p->u.zToken[t.n] = 0;
136677 if( sqlite3Isquote(p->u.zToken[0]) ){
136678 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
136679 sqlite3Dequote(p->u.zToken);
136682 p->nHeight = 1;
136685 pOut->pExpr = p;
136686 pOut->zStart = t.z;
136687 pOut->zEnd = &t.z[t.n];
136699 pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr);
136700 pLeft->zEnd = pRight->zEnd;
136703 /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
136708 pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0);
136720 pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
136721 pOperand->zEnd = &pPostOp->z[pPostOp->n];
136727 sqlite3 *db = pParse->db;
136728 if( pA && pY && pY->op==TK_NULL ){
136729 pA->op = (u8)op;
136730 sqlite3ExprDelete(db, pA->pRight);
136731 pA->pRight = 0;
136744 pOut->zStart = pPreOp->z;
136745 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
136746 pOut->zEnd = pOperand->zEnd;
136763 && pParse->db->init.busy==0
136766 pIdToken->n, pIdToken->z);
136774 ** "lemon" is run with the "-m" command-line option.
136781 ** that represent terminal and non-terminal symbols.
136787 ** (also known as: "terminal symbols") have fall-back
136791 ** YYACTIONTYPE is the data type used for "action codes" - numbers
136796 ** value associated with a terminal or non-terminal
136799 ** Each non-terminal can have a different minor type.
136818 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
136819 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
136823 ** YY_NO_ACTION The yy_action[] code for no-op
136855 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
136856 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
136870 /* Define the yytestcase() macro to be a no-op if is not already defined
136895 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
136897 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
136922 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
136935 ** shifting non-terminals after a reduce.
137000 /* 590 */ 10, 10, 5, 301, 203, 449, 177, 969, 253, 419,
137231 /* 1310 */ 201, 37, 95, 96, 97, 98, 192, 5, 101, 192,
137248 /* 1480 */ 78, 78, 111, 96, 122, 35, 1, 5, 22, 107,
137260 #define YY_SHIFT_MIN (-114)
137263 /* 0 */ 5, 1117, 1312, 1128, 1274, 1274, 1274, 1274, 61, -19,
137265 /* 20 */ 66, 66, 201, -29, 331, 318, 133, 259, 335, 411,
137273 /* 100 */ 1274, 1274, 1274, 1274, -70, -47, -47, -47, -47, -47,
137275 /* 120 */ 2, -30, 1565, 1565, 1565, -17, -17, -17, 145, 145,
137285 /* 220 */ 596, 596, 596, 575, -114, 971, 740, 454, 503, 503,
137310 #define YY_REDUCE_USE_DFLT (-174)
137312 #define YY_REDUCE_MIN (-173)
137315 /* 0 */ -119, 1014, 131, 1031, -12, 225, 228, 300, -40, -45,
137317 /* 20 */ 16, 137, 367, 323, -38, 391, -173, -173, -173, -173,
137318 /* 30 */ -173, -173, -173, -173, -173, -173, -173, -173, -173, -173,
137319 /* 40 */ -173, -173, -173, -173, -173, -173, -173, -173, -173, -173,
137320 /* 50 */ -173, -173, -173, -173, -173, -173, -173, -173, 374, 437,
137325 /* 100 */ 989, 1008, 1016, 1018, -173, -173, -173, -173, -173, -173,
137326 /* 110 */ -173, -173, -173, 544, -37, 274, 299, 501, 161, -173,
137327 /* 120 */ 193, -173, -173, -173, -173, 22, 22, 22, 64, 141,
137329 /* 140 */ 750, 794, 796, -58, 32, 383, 660, 737, 386, 787,
137397 /********** End of lemon-generated parsing tables *****************************/
137504 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
137507 YYMINORTYPE minor; /* The user-supplied minor token value. This
137517 int yyhwm; /* High-water mark of the stack */
137645 /* 5 */ "transtype ::= DEFERRED",
137983 newSize = p->yystksz*2 + 100;
137984 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
137985 if( p->yystack==&p->yystk0 ){
137987 if( pNew ) pNew[0] = p->yystk0;
137989 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
137992 p->yystack = pNew;
137993 p->yytos = &p->yystack[idx];
137997 yyTracePrompt, p->yystksz, newSize);
138000 p->yystksz = newSize;
138020 pParser->yyhwm = 0;
138023 pParser->yytos = NULL;
138024 pParser->yystack = NULL;
138025 pParser->yystksz = 0;
138027 pParser->yystack = &pParser->yystk0;
138028 pParser->yystksz = 1;
138032 pParser->yyerrcnt = -1;
138034 pParser->yytos = pParser->yystack;
138035 pParser->yystack[0].stateno = 0;
138036 pParser->yystack[0].major = 0;
138038 pParser->yystackEnd = &pParser->yystack[YYSTACKDEPTH-1];
138079 ** terminal or non-terminal is destroyed. This can happen
138094 sqlite3SelectDelete(pParse->db, (yypminor->yy243));
138100 sqlite3ExprDelete(pParse->db, (yypminor->yy190).pExpr);
138116 sqlite3ExprListDelete(pParse->db, (yypminor->yy148));
138124 sqlite3SrcListDelete(pParse->db, (yypminor->yy185));
138130 sqlite3WithDelete(pParse->db, (yypminor->yy285));
138141 sqlite3ExprDelete(pParse->db, (yypminor->yy72));
138148 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
138154 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy145));
138159 sqlite3IdListDelete(pParse->db, (yypminor->yy332).b);
138175 assert( pParser->yytos!=0 );
138176 assert( pParser->yytos > pParser->yystack );
138177 yytos = pParser->yytos--;
138182 yyTokenName[yytos->major]);
138185 yy_destructor(pParser, yytos->major, &yytos->minor);
138193 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
138195 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
138226 return pParser->yyhwm;
138232 ** look-ahead token iLookAhead.
138236 YYCODETYPE iLookAhead /* The look-ahead token */
138239 int stateno = pParser->yytos->stateno;
138265 int j = i - iLookAhead + YYWILDCARD;
138294 ** Find the appropriate action for a parser given the non-terminal
138295 ** look-ahead token iLookAhead.
138299 YYCODETYPE iLookAhead /* The look-ahead token */
138334 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
138352 yyTracePrompt,yyTokenName[yypParser->yytos->major],
138356 yyTracePrompt,yyTokenName[yypParser->yytos->major]);
138374 yypParser->yytos++;
138376 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
138377 yypParser->yyhwm++;
138378 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
138382 if( yypParser->yytos>yypParser->yystackEnd ){
138383 yypParser->yytos--;
138388 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
138390 yypParser->yytos--;
138397 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
138399 yytos = yypParser->yytos;
138400 yytos->stateno = (YYACTIONTYPE)yyNewState;
138401 yytos->major = (YYCODETYPE)yyMajor;
138402 yytos->minor.yy0 = yyMinor;
138410 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
138413 { 147, -1 },
138414 { 147, -3 },
138415 { 148, -1 },
138416 { 149, -3 },
138418 { 150, -1 },
138419 { 150, -1 },
138420 { 150, -1 },
138421 { 149, -2 },
138422 { 149, -2 },
138423 { 149, -2 },
138424 { 149, -3 },
138425 { 149, -5 },
138426 { 154, -6 },
138427 { 156, -1 },
138429 { 158, -3 },
138430 { 157, -1 },
138432 { 155, -5 },
138433 { 155, -2 },
138435 { 162, -2 },
138436 { 164, -2 },
138438 { 166, -4 },
138439 { 166, -6 },
138440 { 167, -2 },
138441 { 171, -2 },
138442 { 171, -2 },
138443 { 171, -4 },
138444 { 171, -3 },
138445 { 171, -3 },
138446 { 171, -2 },
138447 { 171, -3 },
138448 { 171, -5 },
138449 { 171, -2 },
138450 { 171, -4 },
138451 { 171, -4 },
138452 { 171, -1 },
138453 { 171, -2 },
138455 { 176, -1 },
138457 { 178, -2 },
138458 { 180, -2 },
138459 { 180, -3 },
138460 { 180, -3 },
138461 { 180, -3 },
138462 { 181, -2 },
138463 { 181, -2 },
138464 { 181, -1 },
138465 { 181, -1 },
138466 { 181, -2 },
138467 { 179, -3 },
138468 { 179, -2 },
138470 { 182, -2 },
138471 { 182, -2 },
138473 { 184, -1 },
138474 { 185, -2 },
138475 { 185, -7 },
138476 { 185, -5 },
138477 { 185, -5 },
138478 { 185, -10 },
138481 { 174, -3 },
138483 { 189, -2 },
138484 { 190, -1 },
138485 { 190, -1 },
138486 { 149, -4 },
138487 { 192, -2 },
138489 { 149, -9 },
138490 { 149, -4 },
138491 { 149, -1 },
138492 { 163, -2 },
138493 { 194, -3 },
138494 { 197, -1 },
138495 { 197, -2 },
138496 { 197, -1 },
138497 { 195, -9 },
138498 { 206, -4 },
138499 { 206, -5 },
138500 { 198, -1 },
138501 { 198, -1 },
138504 { 199, -3 },
138505 { 199, -2 },
138506 { 199, -4 },
138507 { 210, -2 },
138510 { 200, -2 },
138511 { 212, -2 },
138513 { 211, -7 },
138514 { 211, -9 },
138515 { 211, -7 },
138516 { 211, -7 },
138518 { 159, -2 },
138519 { 193, -2 },
138520 { 213, -1 },
138521 { 213, -2 },
138522 { 213, -3 },
138523 { 213, -4 },
138524 { 215, -2 },
138527 { 214, -3 },
138528 { 214, -2 },
138529 { 216, -4 },
138532 { 204, -3 },
138533 { 186, -4 },
138534 { 186, -2 },
138535 { 175, -1 },
138536 { 175, -1 },
138539 { 202, -3 },
138541 { 203, -2 },
138543 { 205, -2 },
138544 { 205, -4 },
138545 { 205, -4 },
138546 { 149, -6 },
138548 { 201, -2 },
138549 { 149, -8 },
138550 { 218, -5 },
138551 { 218, -7 },
138552 { 218, -3 },
138553 { 218, -5 },
138554 { 149, -6 },
138555 { 149, -7 },
138556 { 219, -2 },
138557 { 219, -1 },
138559 { 220, -3 },
138560 { 217, -3 },
138561 { 217, -1 },
138562 { 173, -3 },
138563 { 173, -1 },
138564 { 173, -1 },
138565 { 173, -3 },
138566 { 173, -5 },
138567 { 172, -1 },
138568 { 172, -1 },
138569 { 172, -1 },
138570 { 173, -1 },
138571 { 173, -3 },
138572 { 173, -6 },
138573 { 173, -5 },
138574 { 173, -4 },
138575 { 172, -1 },
138576 { 173, -5 },
138577 { 173, -3 },
138578 { 173, -3 },
138579 { 173, -3 },
138580 { 173, -3 },
138581 { 173, -3 },
138582 { 173, -3 },
138583 { 173, -3 },
138584 { 173, -3 },
138585 { 221, -2 },
138586 { 173, -3 },
138587 { 173, -5 },
138588 { 173, -2 },
138589 { 173, -3 },
138590 { 173, -3 },
138591 { 173, -4 },
138592 { 173, -2 },
138593 { 173, -2 },
138594 { 173, -2 },
138595 { 173, -2 },
138596 { 222, -1 },
138597 { 222, -2 },
138598 { 173, -5 },
138599 { 223, -1 },
138600 { 223, -2 },
138601 { 173, -5 },
138602 { 173, -3 },
138603 { 173, -5 },
138604 { 173, -5 },
138605 { 173, -4 },
138606 { 173, -5 },
138607 { 226, -5 },
138608 { 226, -4 },
138609 { 227, -2 },
138611 { 225, -1 },
138614 { 207, -3 },
138615 { 207, -1 },
138617 { 224, -3 },
138618 { 149, -12 },
138619 { 228, -1 },
138622 { 177, -3 },
138623 { 187, -5 },
138624 { 187, -3 },
138626 { 229, -2 },
138627 { 149, -4 },
138628 { 149, -1 },
138629 { 149, -2 },
138630 { 149, -3 },
138631 { 149, -5 },
138632 { 149, -6 },
138633 { 149, -5 },
138634 { 149, -6 },
138635 { 169, -2 },
138636 { 170, -2 },
138637 { 149, -5 },
138638 { 231, -11 },
138639 { 233, -1 },
138640 { 233, -2 },
138642 { 234, -1 },
138643 { 234, -1 },
138644 { 234, -3 },
138646 { 236, -2 },
138647 { 232, -3 },
138648 { 232, -2 },
138649 { 238, -3 },
138650 { 239, -3 },
138651 { 239, -2 },
138652 { 237, -7 },
138653 { 237, -5 },
138654 { 237, -5 },
138655 { 237, -1 },
138656 { 173, -4 },
138657 { 173, -6 },
138658 { 191, -1 },
138659 { 191, -1 },
138660 { 191, -1 },
138661 { 149, -4 },
138662 { 149, -6 },
138663 { 149, -3 },
138665 { 241, -2 },
138666 { 149, -1 },
138667 { 149, -3 },
138668 { 149, -1 },
138669 { 149, -3 },
138670 { 149, -6 },
138671 { 149, -7 },
138672 { 242, -1 },
138673 { 149, -1 },
138674 { 149, -4 },
138675 { 244, -8 },
138677 { 247, -1 },
138678 { 247, -3 },
138679 { 248, -1 },
138681 { 196, -2 },
138682 { 196, -3 },
138683 { 250, -6 },
138684 { 250, -8 },
138685 { 144, -1 },
138686 { 145, -2 },
138687 { 145, -1 },
138688 { 146, -1 },
138689 { 146, -3 },
138692 { 151, -1 },
138693 { 151, -2 },
138694 { 153, -1 },
138696 { 149, -2 },
138697 { 160, -4 },
138698 { 160, -2 },
138699 { 152, -1 },
138700 { 152, -1 },
138701 { 152, -1 },
138702 { 166, -1 },
138703 { 167, -1 },
138704 { 168, -1 },
138705 { 168, -1 },
138706 { 165, -2 },
138708 { 171, -2 },
138709 { 161, -2 },
138710 { 183, -3 },
138711 { 183, -1 },
138713 { 188, -1 },
138714 { 190, -1 },
138715 { 194, -1 },
138716 { 195, -1 },
138717 { 209, -2 },
138718 { 210, -1 },
138719 { 173, -1 },
138720 { 221, -1 },
138721 { 208, -1 },
138722 { 230, -1 },
138723 { 230, -1 },
138724 { 230, -1 },
138725 { 230, -1 },
138726 { 230, -1 },
138727 { 169, -1 },
138729 { 235, -3 },
138730 { 238, -1 },
138732 { 240, -1 },
138735 { 243, -1 },
138736 { 245, -1 },
138737 { 245, -3 },
138738 { 246, -2 },
138740 { 249, -4 },
138741 { 249, -2 },
138759 yymsp = yypParser->yytos;
138773 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
138774 yypParser->yyhwm++;
138775 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
138779 if( yypParser->yytos>=yypParser->yystackEnd ){
138784 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
138789 yymsp = yypParser->yytos;
138806 { pParse->explain = 1; }
138809 { pParse->explain = 2; }
138815 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy194);}
138820 case 5: /* transtype ::= DEFERRED */
138823 {yymsp[0].minor.yy194 = yymsp[0].major; /*A-overwrites-X*/}
138827 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
138846 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy194,0,0,yymsp[
138864 {yymsp[-2].minor.yy194 = 1;}
138872 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy194,0);
138878 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy243);
138883 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
138884 yymsp[-1].minor.yy194 = TF_WithoutRowid | TF_NoVisibleRowid;
138886 yymsp[-1].minor.yy194 = 0;
138892 {sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
138901 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
138906 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
138910 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
138914 {pParse->constraintName = yymsp[0].minor.yy0;}
138921 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy190);}
138927 v.zStart = yymsp[-1].minor.yy0.z;
138943 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy194,yymsp[0].minor.yy194,yymsp[-2].minor.yy194);}
138950 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy190.pExpr);}
138953 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy148,yymsp[0].minor.yy194);}
138962 { yymsp[1].minor.yy194 = OE_None*0x0101; /* EV: R-19803-45884 */}
138965 { yymsp[-1].minor.yy194 = (yymsp[-1].minor.yy194 & ~yymsp[0].minor.yy497.mask) | yymsp[0].minor.yy4…
138968 { yymsp[-1].minor.yy497.value = 0; yymsp[-1].minor.yy497.mask = 0x000000; }
138971 { yymsp[-2].minor.yy497.value = 0; yymsp[-2].minor.yy497.mask = 0x000000; }
138974 { yymsp[-2].minor.yy497.value = yymsp[0].minor.yy194; yymsp[-2].minor.yy497.mask = 0x0000ff; }
138977 { yymsp[-2].minor.yy497.value = yymsp[0].minor.yy194<<8; yymsp[-2].minor.yy497.mask = 0x00ff00; }
138980 { yymsp[-1].minor.yy194 = OE_SetNull; /* EV: R-33326-45252 */}
138983 { yymsp[-1].minor.yy194 = OE_SetDflt; /* EV: R-33326-45252 */}
138986 { yymsp[0].minor.yy194 = OE_Cascade; /* EV: R-33326-45252 */}
138989 { yymsp[0].minor.yy194 = OE_Restrict; /* EV: R-33326-45252 */}
138992 { yymsp[-1].minor.yy194 = OE_None; /* EV: R-33326-45252 */}
138995 {yymsp[-2].minor.yy194 = 0;}
139000 {yymsp[-1].minor.yy194 = yymsp[0].minor.yy194;}
139007 {yymsp[-1].minor.yy194 = 1;}
139010 {yymsp[-1].minor.yy194 = 0;}
139013 {pParse->constraintName.n = 0;}
139016 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy148,yymsp[0].minor.yy194,yymsp[-2].minor.yy194,0);}
139019 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy148,yymsp[0].minor.yy194,0,0,0,0,
139023 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy190.pExpr);}
139027 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy148, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy148…
139036 {yymsp[-2].minor.yy194 = yymsp[0].minor.yy194;}
139047 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 0, yymsp[-1].minor.yy194);
139052 …, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy148, yymsp[0…
139057 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 1, yymsp[-1].minor.yy194);
139064 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy243);
139071 p->pWith = yymsp[-1].minor.yy285;
139074 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy285);
139076 yymsp[-1].minor.yy243 = p; /*A-overwrites-W*/
139082 Select *pLhs = yymsp[-2].minor.yy243;
139083 if( pRhs && pRhs->pPrior ){
139092 pRhs->op = (u8)yymsp[-1].minor.yy194;
139093 pRhs->pPrior = pLhs;
139094 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
139095 pRhs->selFlags &= ~SF_MultiValue;
139096 if( yymsp[-1].minor.yy194!=TK_ALL ) pParse->hasCompound = 1;
139098 sqlite3SelectDelete(pParse->db, pLhs);
139100 yymsp[-2].minor.yy243 = pRhs;
139105 {yymsp[0].minor.yy194 = yymsp[0].major; /*A-overwrites-OP*/}
139108 {yymsp[-1].minor.yy194 = TK_ALL;}
139113 Token s = yymsp[-8].minor.yy0; /*A-overwrites-S*/
139115-8].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].mi…
139121 ** If the SELECT keyword is immediately followed by a C-style comment
139126 if( yymsp[-8].minor.yy243!=0 ){
139129 … sqlite3_snprintf(sizeof(yymsp[-8].minor.yy243->zSelName), yymsp[-8].minor.yy243->zSelName, "#%d",
139130 ++pParse->nSelect);
139136 …sqlite3_snprintf(sizeof(yymsp[-8].minor.yy243->zSelName), yymsp[-8].minor.yy243->zSelName, "%.*s",…
139144 yymsp[-3].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values,0,0);
139149 Select *pRight, *pLeft = yymsp[-4].minor.yy243;
139150 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
139151 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
139153 pRight->op = TK_ALL;
139154 pRight->pPrior = pLeft;
139155 yymsp[-4].minor.yy243 = pRight;
139157 yymsp[-4].minor.yy243 = pLeft;
139177 …yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148, yymsp[-1].minor.yy190…
139178 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-2].minor.yy148, &yymsp[0].minor…
139179 sqlite3ExprListSetSpan(pParse,yymsp[-2].minor.yy148,&yymsp[-1].minor.yy190);
139184 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
139185 yymsp[-1].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy148, p);
139191 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
139193 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, pDot);
139200 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
139203 {yymsp[1].minor.yy185 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy185));}
139207 yymsp[-1].minor.yy185 = yymsp[0].minor.yy185;
139208 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy185);
139213 …if( ALWAYS(yymsp[-1].minor.yy185 && yymsp[-1].minor.yy185->nSrc>0) ) yymsp[-1].minor.yy185->a[yyms…
139221 …p[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,&yymsp[-5].minor.yy0…
139222 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy185, &yymsp[-2].minor.yy0);
139227 …p[-8].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy185,&yymsp[-7].minor.yy0…
139228 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy185, yymsp[-4].minor.yy148);
139233 …yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].m…
139238 …if( yymsp[-6].minor.yy185==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy72==0 && yymsp[0].mi…
139239 yymsp[-6].minor.yy185 = yymsp[-4].minor.yy185;
139240 }else if( yymsp[-4].minor.yy185->nSrc==1 ){
139241 …yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].m…
139242 if( yymsp[-6].minor.yy185 ){
139243 struct SrcList_item *pNew = &yymsp[-6].minor.yy185->a[yymsp[-6].minor.yy185->nSrc-1];
139244 struct SrcList_item *pOld = yymsp[-4].minor.yy185->a;
139245 pNew->zName = pOld->zName;
139246 pNew->zDatabase = pOld->zDatabase;
139247 pNew->pSelect = pOld->pSelect;
139248 pOld->zName = pOld->zDatabase = 0;
139249 pOld->pSelect = 0;
139251 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy185);
139254 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185);
139255 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,SF_NestedFrom,0,0);
139256 …yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].m…
139265 {yymsp[-1].minor.yy185 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0…
139271 {yymsp[-1].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
139274 {yymsp[-2].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
139277 {yymsp[-3].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
139283 {yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr;}
139293 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
139296 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
139299 {yymsp[-3].minor.yy254 = yymsp[-1].minor.yy254;}
139307 {yymsp[-2].minor.yy148 = yymsp[0].minor.yy148;}
139311 …yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148,yymsp[-1].minor.yy190.p…
139312 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy148,yymsp[0].minor.yy194);
139317 …yymsp[-1].minor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy190.pExpr); /*A-overwrite…
139318 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy148,yymsp[0].minor.yy194);
139334 {yymsp[-1].minor.yy354.pLimit = yymsp[0].minor.yy190.pExpr; yymsp[-1].minor.yy354.pOffset = 0;}
139337 {yymsp[-3].minor.yy354.pLimit = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy354.pOffset = yymsp[…
139340 {yymsp[-3].minor.yy354.pOffset = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy354.pLimit = yymsp[…
139344 sqlite3WithPush(pParse, yymsp[-5].minor.yy285, 1);
139345 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy185, &yymsp[-1].minor.yy0);
139346 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy185,yymsp[0].minor.yy72);
139351 sqlite3WithPush(pParse, yymsp[-7].minor.yy285, 1);
139352 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy185, &yymsp[-3].minor.yy0);
139353 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy148,"set list");
139354 …sqlite3Update(pParse,yymsp[-4].minor.yy185,yymsp[-1].minor.yy148,yymsp[0].minor.yy72,yymsp[-5].min…
139359 …yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy148, yymsp[0].minor.yy190.…
139360 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy148, &yymsp[-2].minor.yy0, 1);
139365 …yymsp[-6].minor.yy148 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy148, yymsp[-3].minor…
139371 sqlite3ExprListSetName(pParse, yylhsminor.yy148, &yymsp[-2].minor.yy0, 1);
139373 yymsp[-2].minor.yy148 = yylhsminor.yy148;
139377 …yymsp[-4].minor.yy148 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
139382 sqlite3WithPush(pParse, yymsp[-5].minor.yy285, 1);
139383 …sqlite3Insert(pParse, yymsp[-2].minor.yy185, yymsp[0].minor.yy243, yymsp[-1].minor.yy254, yymsp[-4…
139388 sqlite3WithPush(pParse, yymsp[-6].minor.yy285, 1);
139389 sqlite3Insert(pParse, yymsp[-3].minor.yy185, 0, yymsp[-2].minor.yy254, yymsp[-5].minor.yy194);
139393 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
139396 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
139399 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
139402 {spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/ yyms…
139406 {spanExpr(&yymsp[0].minor.yy190,pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
139410 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
139411 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
139412 spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/
139413 yymsp[-2].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
139418 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
139419 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
139420 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
139422 spanSet(&yymsp[-4].minor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/
139423 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
139428 {spanExpr(&yymsp[0].minor.yy190,pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
139432 yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
139447 ** in the virtual machine. #N is the N-th register. */
139448 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
139451 if( pParse->nested==0 ){
139456 … if( yymsp[0].minor.yy190.pExpr ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy190.pExpr->iTable);
139463 …yymsp[-2].minor.yy190.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy190.pExpr, &yym…
139464 yymsp[-2].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
139469 spanSet(&yymsp[-5].minor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/
139470 yymsp[-5].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
139471 …sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy190.pExpr, yymsp[-3].minor.yy190.pExpr, 0);
139476 …if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION…
139477 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
139479 yylhsminor.yy190.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy148, &yymsp[-4].minor.yy0);
139480 spanSet(&yylhsminor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
139481 if( yymsp[-2].minor.yy194==SF_Distinct && yylhsminor.yy190.pExpr ){
139482 yylhsminor.yy190.pExpr->flags |= EP_Distinct;
139485 yymsp[-4].minor.yy190 = yylhsminor.yy190;
139489 yylhsminor.yy190.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
139490 spanSet(&yylhsminor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
139492 yymsp[-3].minor.yy190 = yylhsminor.yy190;
139503 …ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy148, yymsp[-1].minor.yy190.pExpr…
139506 yylhsminor.yy190.pExpr->x.pList = pList;
139507 spanSet(&yylhsminor.yy190, &yymsp[-4].minor.yy0, &yymsp[0].minor.yy0);
139509 sqlite3ExprListDelete(pParse->db, pList);
139512 yymsp[-4].minor.yy190 = yylhsminor.yy190;
139522 {spanBinaryExpr(pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);}
139525 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
139530 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
139531 yymsp[-1].minor.yy0.n &= 0x7fffffff;
139533 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy190.pExpr);
139534 yymsp[-2].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0);
139535 exprNot(pParse, bNot, &yymsp[-2].minor.yy190);
139536 yymsp[-2].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
139537 if( yymsp[-2].minor.yy190.pExpr ) yymsp[-2].minor.yy190.pExpr->flags |= EP_InfixFunc;
139543 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
139544 yymsp[-3].minor.yy0.n &= 0x7fffffff;
139545 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
139546 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy190.pExpr);
139548 yymsp[-4].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0);
139549 exprNot(pParse, bNot, &yymsp[-4].minor.yy190);
139550 yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
139551 if( yymsp[-4].minor.yy190.pExpr ) yymsp[-4].minor.yy190.pExpr->flags |= EP_InfixFunc;
139555 {spanUnaryPostfix(pParse,yymsp[0].major,&yymsp[-1].minor.yy190,&yymsp[0].minor.yy0);}
139558 {spanUnaryPostfix(pParse,TK_NOTNULL,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy0);}
139562 spanBinaryExpr(pParse,TK_IS,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);
139563 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-2].minor.yy190.pExpr, TK_ISNULL);
139568 spanBinaryExpr(pParse,TK_ISNOT,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy190);
139569 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-3].minor.yy190.pExpr, TK_NOTNULL);
139574 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.yy190,&yymsp[-1].min…
139577 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0…
139580 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UPLUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0)…
139588 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
139590 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy190.pExpr, 0);
139591 if( yymsp[-4].minor.yy190.pExpr ){
139592 yymsp[-4].minor.yy190.pExpr->x.pList = pList;
139594 sqlite3ExprListDelete(pParse->db, pList);
139596 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
139597 yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
139602 if( yymsp[-1].minor.yy148==0 ){
139611 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy190.pExpr);
139612 …yymsp[-4].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].…
139613 }else if( yymsp[-1].minor.yy148->nExpr==1 ){
139630 Expr *pRHS = yymsp[-1].minor.yy148->a[0].pExpr;
139631 yymsp[-1].minor.yy148->a[0].pExpr = 0;
139632 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy148);
139636 pRHS->flags &= ~EP_Collate;
139637 pRHS->flags |= EP_Generic;
139639 …yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy194 ? TK_NE : TK_EQ, yymsp[-4…
139641 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
139642 if( yymsp[-4].minor.yy190.pExpr ){
139643 yymsp[-4].minor.yy190.pExpr->x.pList = yymsp[-1].minor.yy148;
139644 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy190.pExpr);
139646 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy148);
139648 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
139650 yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
139655 spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/
139656 yymsp[-2].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
139657 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy190.pExpr, yymsp[-1].minor.yy243);
139662 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
139663 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, yymsp[-1].minor.yy243);
139664 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
139665 yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
139670 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
139673 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
139674 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, pSelect);
139675 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
139676 …yymsp[-4].minor.yy190.zEnd = yymsp[-1].minor.yy0.z ? &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]…
139682 spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/
139683 p = yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
139684 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy243);
139689 spanSet(&yymsp[-4].minor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-C*/
139690 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy72, 0);
139691 if( yymsp[-4].minor.yy190.pExpr ){
139692 …yymsp[-4].minor.yy190.pExpr->x.pList = yymsp[-1].minor.yy72 ? sqlite3ExprListAppend(pParse,yymsp[-
139693 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy190.pExpr);
139695 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy148);
139696 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy72);
139702 …yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yymsp[-2].minor.yy190.…
139703 …yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yymsp[0].minor.yy190.p…
139708 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
139709 …yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, yymsp[0].minor.yy190.p…
139713 {yymsp[0].minor.yy72 = yymsp[0].minor.yy190.pExpr; /*A-overwrites-X*/}
139716 {yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[0].minor.yy190.pE…
139719 {yymsp[0].minor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExpr); /*A-overwrites-
139723 {yymsp[-2].minor.yy148 = yymsp[-1].minor.yy148;}
139727 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
139728 …sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy148, yymsp[-10].minor…
139729 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy72, SQLITE_SO_ASC, yymsp[-8].minor.yy194, SQLITE_IDXTYPE_A…
139741 …yymsp[-4].minor.yy148 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy148, &yymsp[-2].minor.yy…
139746 …yymsp[-2].minor.yy148 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
139750 {sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);}
139759 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
139762 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
139765 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
139768 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
139771 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
139776 all.z = yymsp[-3].minor.yy0.z;
139777 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
139778 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy145, &all);
139783-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].min…
139784 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
139788 { yymsp[0].minor.yy194 = yymsp[0].major; /*A-overwrites-X*/ }
139791 { yymsp[-1].minor.yy194 = TK_INSTEAD;}
139798 {yymsp[0].minor.yy332.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy332.b = 0;}
139801 {yymsp[-2].minor.yy332.a = TK_UPDATE; yymsp[-2].minor.yy332.b = yymsp[0].minor.yy254;}
139809 { yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr; }
139813 assert( yymsp[-2].minor.yy145!=0 );
139814 yymsp[-2].minor.yy145->pLast->pNext = yymsp[-1].minor.yy145;
139815 yymsp[-2].minor.yy145->pLast = yymsp[-1].minor.yy145;
139820 assert( yymsp[-1].minor.yy145!=0 );
139821 yymsp[-1].minor.yy145->pLast = yymsp[-1].minor.yy145;
139826 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
139847 {yymsp[-6].minor.yy145 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor…
139850-4].minor.yy145 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy254…
139853 {yymsp[-4].minor.yy145 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.…
139856 {yymsp[0].minor.yy145 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy243); /*A-overwrites-
139860 spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/
139861 yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
139862 if( yymsp[-3].minor.yy190.pExpr ){
139863 yymsp[-3].minor.yy190.pExpr->affinity = OE_Ignore;
139869 spanSet(&yymsp[-5].minor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/
139870 yymsp[-5].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
139871 if( yymsp[-5].minor.yy190.pExpr ) {
139872 yymsp[-5].minor.yy190.pExpr->affinity = (char)yymsp[-3].minor.yy194;
139884 sqlite3DropTrigger(pParse,yymsp[0].minor.yy185,yymsp[-1].minor.yy194);
139889 …sqlite3Attach(pParse, yymsp[-3].minor.yy190.pExpr, yymsp[-1].minor.yy190.pExpr, yymsp[0].minor.yy7…
139901 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
139907 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
139911 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy185,&yymsp[0].minor.yy0);
139916 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
139917 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
139934 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
139949 { yymsp[-1].minor.yy285 = yymsp[0].minor.yy285; }
139952 { yymsp[-2].minor.yy285 = yymsp[0].minor.yy285; }
139956 …yymsp[-5].minor.yy285 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy148, yym…
139961 …yymsp[-7].minor.yy285 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy285, &yymsp[-5].minor.yy0, yymsp[
140038 yypParser->yytos += yysize;
140042 yypParser->yytos = yymsp;
140043 yymsp->stateno = (YYACTIONTYPE)yyact;
140044 yymsp->major = (YYCODETYPE)yygoto;
140062 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
140103 yypParser->yyerrcnt = -1;
140105 assert( yypParser->yytos==yypParser->yystack );
140126 ** <li> An option argument of a grammar-specified type.
140149 assert( yypParser->yytos!=0 );
140166 yypParser->yyerrcnt--;
140170 yy_reduce(yypParser,yyact-YY_MIN_REDUCE);
140202 if( yypParser->yyerrcnt<0 ){
140205 yymx = yypParser->yytos->major;
140216 while( yypParser->yytos >= yypParser->yystack
140219 yypParser->yytos->stateno,
140224 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
140228 yypParser->yyerrcnt = -1;
140235 yypParser->yyerrcnt = 3;
140259 if( yypParser->yyerrcnt<=0 ){
140262 yypParser->yyerrcnt = 3;
140267 yypParser->yyerrcnt = -1;
140273 }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack );
140279 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
140280 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
140305 ** individual tokens and sends those tokens one-by-one over to the
140324 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
140330 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
140335 #define CC_SLASH 16 /* '/'. / or c-style comment */
140353 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
140354 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
140355 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
140356 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
140375 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
140377 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
140391 ** lower-case ASCII equivalent. On ASCII machines, this is just
140392 ** an upper-to-lower case map. On EBCDIC machines we also need
140404 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
140410 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
140445 ** might be implemented more directly using a hand-written hash table.
140495 /* aKWHash[i] is the hash value for the i-th keyword */
140501 0, 103, 24, 0, 17, 0, 122, 53, 23, 0, 5, 110, 25,
140509 ** then the i-th keyword has no more hash collisions. Otherwise,
140510 ** the next keyword with the same hash is aKWHash[i]-1. */
140523 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
140525 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
140526 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6,
140527 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10,
140528 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7,
140529 6, 6, 5, 6, 5, 5, 9, 7, 7, 3, 2, 4, 4,
140530 7, 3, 6, 4, 7, 6, 12, 6, 9, 4, 6, 5, 4,
140531 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7,
140532 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8,
140533 2, 4, 4, 4, 4, 4, 2, 2, 6, 5, 8, 5, 8,
140534 3, 5, 5, 6, 4, 9, 3,
140537 ** the text for the i-th keyword. */
140550 /* aKWCode[i] is the parser symbol code for the i-th keyword */
140578 /* Check to see if z[0..n-1] is a keyword. If it is, write the
140585 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
140586 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
140602 testcase( i==5 ); /* EACH */
140742 ** For ASCII, any character with the high-order bit set is
140743 ** allowed in an identifier. For 7-bit characters,
140761 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
140773 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
140788 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
140802 if( z[1]=='-' ){
140804 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
140837 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
140943 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
140962 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
141068 void *pEngine; /* The LEMON-generated LALR(1) parser */
141071 int lastTokenParsed = -1; /* type of the previous token */
141072 sqlite3 *db = pParse->db; /* The database connection */
141075 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
141079 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
141080 if( db->nVdbeActive==0 ){
141081 db->u1.isInterrupted = 0;
141083 pParse->rc = SQLITE_OK;
141084 pParse->zTail = zSql;
141097 assert( pParse->pNewTable==0 );
141098 assert( pParse->pNewTrigger==0 );
141099 assert( pParse->nVar==0 );
141100 assert( pParse->pVList==0 );
141104 mxSqlLen -= n;
141106 pParse->rc = SQLITE_TOOBIG;
141119 zSql -= n;
141123 if( db->u1.isInterrupted ){
141124 pParse->rc = SQLITE_INTERRUPT;
141133 pParse->sLastToken.z = zSql;
141134 pParse->sLastToken.n = n;
141135 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
141138 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
141142 pParse->zTail = zSql;
141155 if( db->mallocFailed ){
141156 pParse->rc = SQLITE_NOMEM_BKPT;
141158 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
141159 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
141162 if( pParse->zErrMsg ){
141163 *pzErrMsg = pParse->zErrMsg;
141164 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
141165 pParse->zErrMsg = 0;
141168 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
141169 sqlite3VdbeDelete(pParse->pVdbe);
141170 pParse->pVdbe = 0;
141173 if( pParse->nested==0 ){
141174 sqlite3DbFree(db, pParse->aTableLock);
141175 pParse->aTableLock = 0;
141176 pParse->nTableLock = 0;
141180 sqlite3_free(pParse->apVtabLock);
141184 /* If the pParse->declareVtab flag is set, do not delete any table
141185 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
141188 sqlite3DeleteTable(db, pParse->pNewTable);
141191 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
141192 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
141193 sqlite3DbFree(db, pParse->pVList);
141194 while( pParse->pAinc ){
141195 AutoincInfo *p = pParse->pAinc;
141196 pParse->pAinc = p->pNext;
141199 while( pParse->pZombieTab ){
141200 Table *p = pParse->pZombieTab;
141201 pParse->pZombieTab = p->pNextZombie;
141204 assert( nErr==0 || pParse->rc!=SQLITE_OK );
141240 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
141255 #define tkTEMP 5
141269 ** (0) INVALID We have not yet seen a non-whitespace character.
141285 ** (5) TRIGGER We are in the middle of a trigger definition that must be
141302 ** (5) tkTEMP The "temp" or "temporary" keyword.
141328 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
141329 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
141330 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
141331 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
141367 case '/': { /* C-style comments */
141379 case '-': { /* SQL-style comments from "--" to end of line */
141380 if( zSql[1]!='-' ){
141389 case '[': { /* Microsoft-style identifiers in [...] */
141396 case '`': /* Grave-accent quoted symbols used by MySQL */
141397 case '"': /* single- and double-quoted strings */
141458 zSql += nId-1;
141475 ** above, except that the parameter is required to be UTF-16 encoded, not
141476 ** UTF-8.
141488 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
141632 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
141638 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
141643 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
141649 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
141654 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
141656 ** the SQLITE_THREADSAFE compile-time option being set to 0.
141662 ** this variable being set to non-zero will cause OSTRACE macros to emit
141709 ** This routine is a no-op except on its very first call for the process,
141725 ** * Calls to this routine from Y must block until the outer-most
141746 ** combination, the work-around is to set the correct pointer
141747 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
141751 ** to sqlite3_initialize() should be a no-op. But the initialization
141771 ** malloc subsystem - this implies that the allocation of a static
141808 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
141849 sqlite3GlobalConfig.nRefInitMutex--;
141866 u64 x = (((u64)1)<<63)-1;
141877 ** compile-time option.
141895 ** when this routine is invoked, then this routine is a harmless no-op.
141944 ** the SQLite library at run-time.
141965 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
141967 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
141968 ** Single-thread. */
141974 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
141976 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
141977 ** Multi-thread. */
141983 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
141985 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
141992 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
141999 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
142008 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
142011 ** low-level memory allocation routines to be used in place of the memory
142017 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
142026 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
142033 /* EVIDENCE-OF: R-08404-60887 There are three arguments to
142034 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
142043 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
142044 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
142053 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
142065 /* no-op */
142075 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
142083 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
142094 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
142099 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
142100 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
142115 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
142125 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
142160 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
142161 ** can be changed at start-time using the
142166 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
142167 ** argument of type int. If non-zero, then URI handling is globally
142175 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
142193 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
142199 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
142200 ** negative, then that argument is changed to its compile-time default.
142202 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
142204 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
142205 ** compile-time option.
142217 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
142219 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
142260 if( db->lookaside.nOut ){
142267 if( db->lookaside.bMalloced ){
142268 sqlite3_free(db->lookaside.pStart);
142273 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
142281 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
142287 db->lookaside.pStart = pStart;
142288 db->lookaside.pFree = 0;
142289 db->lookaside.sz = (u16)sz;
142295 for(i=cnt-1; i>=0; i--){
142296 p->pNext = db->lookaside.pFree;
142297 db->lookaside.pFree = p;
142300 db->lookaside.pEnd = p;
142301 db->lookaside.bDisable = 0;
142302 db->lookaside.bMalloced = pBuf==0 ?1:0;
142304 db->lookaside.pStart = db;
142305 db->lookaside.pEnd = db;
142306 db->lookaside.bDisable = 1;
142307 db->lookaside.bMalloced = 0;
142323 return db->mutex;
142336 sqlite3_mutex_enter(db->mutex);
142338 for(i=0; i<db->nDb; i++){
142339 Btree *pBt = db->aDb[i].pBt;
142346 sqlite3_mutex_leave(db->mutex);
142351 ** Flush any dirty pages in the pager-cache for any attached database
142362 sqlite3_mutex_enter(db->mutex);
142364 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
142365 Btree *pBt = db->aDb[i].pBt;
142376 sqlite3_mutex_leave(db->mutex);
142389 /* IMP: R-06824-28531 */
142390 /* IMP: R-36257-52125 */
142391 db->aDb[0].zDbSName = va_arg(ap,char*);
142396 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
142397 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
142398 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
142415 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
142420 int oldFlags = db->flags;
142422 db->flags |= aFlagOp[i].mask;
142424 db->flags &= ~aFlagOp[i].mask;
142426 if( oldFlags!=db->flags ){
142430 *pRes = (db->flags & aFlagOp[i].mask)!=0;
142445 ** Return true if the buffer z[0..n-1] contains all spaces.
142448 while( n>0 && z[n-1]==' ' ){ n--; }
142466 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
142473 && allSpaces(((char*)pKey1)+n, nKey1-n)
142474 && allSpaces(((char*)pKey2)+n, nKey2-n)
142476 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
142482 rc = nKey1 - nKey2;
142489 ** Another built-in collating sequence: NOCASE.
142495 ** At the moment there is only a UTF-8 implementation.
142506 r = nKey1-nKey2;
142521 return db->lastRowid;
142534 sqlite3_mutex_enter(db->mutex);
142535 db->lastRowid = iRowid;
142536 sqlite3_mutex_leave(db->mutex);
142549 return db->nChange;
142562 return db->nTotalChange;
142568 ** at the b-tree/pager level.
142571 while( db->pSavepoint ){
142572 Savepoint *pTmp = db->pSavepoint;
142573 db->pSavepoint = pTmp->pNext;
142576 db->nSavepoint = 0;
142577 db->nStatement = 0;
142578 db->isTransactionSavepoint = 0;
142588 FuncDestructor *pDestructor = p->u.pDestructor;
142590 pDestructor->nRef--;
142591 if( pDestructor->nRef==0 ){
142592 pDestructor->xDestroy(pDestructor->pUserData);
142607 for(i=0; i<db->nDb; i++){
142608 Schema *pSchema = db->aDb[i].pSchema;
142609 if( db->aDb[i].pSchema ){
142610 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
142616 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
142618 if( pMod->pEpoTab ){
142619 sqlite3VtabDisconnect(db, pMod->pEpoTab);
142635 assert( sqlite3_mutex_held(db->mutex) );
142636 if( db->pVdbe ) return 1;
142637 for(j=0; j<db->nDb; j++){
142638 Btree *pBt = db->aDb[j].pBt;
142649 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
142650 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
142656 sqlite3_mutex_enter(db->mutex);
142657 if( db->mTrace & SQLITE_TRACE_CLOSE ){
142658 db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
142666 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
142668 ** SQL statements below, as the v-table implementation may be storing
142679 sqlite3_mutex_leave(db->mutex);
142692 db->magic = SQLITE_MAGIC_ZOMBIE;
142726 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
142727 sqlite3_mutex_leave(db->mutex);
142739 ** they are reset. And that the required b-tree mutex is held to make
142747 for(j=0; j<db->nDb; j++){
142748 struct Db *pDb = &db->aDb[j];
142749 if( pDb->pBt ){
142750 sqlite3BtreeClose(pDb->pBt);
142751 pDb->pBt = 0;
142753 pDb->pSchema = 0;
142758 if( db->aDb[1].pSchema ){
142759 sqlite3SchemaClear(db->aDb[1].pSchema);
142765 assert( db->nDb<=2 );
142766 assert( db->aDb==db->aDbStatic );
142769 ** locks and does not require any further unlock-notify callbacks.
142773 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
142778 pNext = p->pNext;
142783 sqlite3HashClear(&db->aFunc);
142784 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
142794 sqlite3HashClear(&db->aCollSeq);
142796 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
142798 if( pMod->xDestroy ){
142799 pMod->xDestroy(pMod->pAux);
142804 sqlite3HashClear(&db->aModule);
142808 sqlite3ValueFree(db->pErr);
142811 sqlite3_free(db->auth.zAuthUser);
142812 sqlite3_free(db->auth.zAuthPW);
142815 db->magic = SQLITE_MAGIC_ERROR;
142817 /* The temp-database schema is allocated differently from the other schema
142823 sqlite3DbFree(db, db->aDb[1].pSchema);
142824 sqlite3_mutex_leave(db->mutex);
142825 db->magic = SQLITE_MAGIC_CLOSED;
142826 sqlite3_mutex_free(db->mutex);
142827 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
142828 if( db->lookaside.bMalloced ){
142829 sqlite3_free(db->lookaside.pStart);
142836 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
142845 assert( sqlite3_mutex_held(db->mutex) );
142848 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
142850 ** modified the database schema. If the b-tree mutexes are not taken
142851 ** here, then another shared-cache connection might sneak in between
142855 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
142857 for(i=0; i<db->nDb; i++){
142858 Btree *p = db->aDb[i].pBt;
142869 if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
142876 db->nDeferredCons = 0;
142877 db->nDeferredImmCons = 0;
142878 db->flags &= ~SQLITE_DeferFKs;
142880 /* If one has been configured, invoke the rollback-hook callback */
142881 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
142882 db->xRollbackCallback(db->pRollbackArg);
143062 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
143067 int timeout = db->busyTimeout;
143075 delay = delays[NDELAY-1];
143076 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
143079 delay = timeout - prior;
143082 sqlite3OsSleep(db->pVfs, delay*1000);
143086 int timeout = ((sqlite3 *)ptr)->busyTimeout;
143090 sqlite3OsSleep(db->pVfs, 1000000);
143099 ** If this routine returns non-zero, the lock is retried. If it
143104 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
143105 rc = p->xFunc(p->pArg, p->nBusy);
143107 p->nBusy = -1;
143109 p->nBusy++;
143126 sqlite3_mutex_enter(db->mutex);
143127 db->busyHandler.xFunc = xBusy;
143128 db->busyHandler.pArg = pArg;
143129 db->busyHandler.nBusy = 0;
143130 db->busyTimeout = 0;
143131 sqlite3_mutex_leave(db->mutex);
143153 sqlite3_mutex_enter(db->mutex);
143155 db->xProgress = xProgress;
143156 db->nProgressOps = (unsigned)nOps;
143157 db->pProgressArg = pArg;
143159 db->xProgress = 0;
143160 db->nProgressOps = 0;
143161 db->pProgressArg = 0;
143163 sqlite3_mutex_leave(db->mutex);
143178 db->busyTimeout = ms;
143190 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
143195 db->u1.isInterrupted = 1;
143220 assert( sqlite3_mutex_held(db->mutex) );
143225 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
143267 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
143268 if( db->nVdbeActive ){
143270 "unable to delete/modify user-function due to active statements");
143271 assert( !db->mallocFailed );
143279 assert(p || db->mallocFailed);
143289 pDestructor->nRef++;
143291 p->u.pDestructor = pDestructor;
143292 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
143293 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
143294 p->xSFunc = xSFunc ? xSFunc : xStep;
143295 p->xFinalize = xFinal;
143296 p->pUserData = pUserData;
143297 p->nArg = (u16)nArg;
143337 sqlite3_mutex_enter(db->mutex);
143344 pArg->xDestroy = xDestroy;
143345 pArg->pUserData = p;
143348 if( pArg && pArg->nRef==0 ){
143356 sqlite3_mutex_leave(db->mutex);
143377 sqlite3_mutex_enter(db->mutex);
143378 assert( !db->mallocFailed );
143379 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
143383 sqlite3_mutex_leave(db->mutex);
143393 ** this routine is a no-op. If the function does not exist, then create
143394 ** a new one that always throws a run-time error.
143409 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
143413 sqlite3_mutex_enter(db->mutex);
143419 sqlite3_mutex_leave(db->mutex);
143428 ** A NULL trace function means that no tracing is executes. A non-NULL
143442 sqlite3_mutex_enter(db->mutex);
143443 pOld = db->pTraceArg;
143444 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
143445 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
143446 db->pTraceArg = pArg;
143447 sqlite3_mutex_leave(db->mutex);
143452 /* Register a trace callback using the version-2 interface.
143465 sqlite3_mutex_enter(db->mutex);
143468 db->mTrace = mTrace;
143469 db->xTrace = xTrace;
143470 db->pTraceArg = pArg;
143471 sqlite3_mutex_leave(db->mutex);
143480 ** A NULL profile function means that no profiling is executes. A non-NULL
143497 sqlite3_mutex_enter(db->mutex);
143498 pOld = db->pProfileArg;
143499 db->xProfile = xProfile;
143500 db->pProfileArg = pArg;
143501 sqlite3_mutex_leave(db->mutex);
143509 ** If the invoked function returns non-zero, then the commit becomes a
143525 sqlite3_mutex_enter(db->mutex);
143526 pOld = db->pCommitArg;
143527 db->xCommitCallback = xCallback;
143528 db->pCommitArg = pArg;
143529 sqlite3_mutex_leave(db->mutex);
143550 sqlite3_mutex_enter(db->mutex);
143551 pRet = db->pUpdateArg;
143552 db->xUpdateCallback = xCallback;
143553 db->pUpdateArg = pArg;
143554 sqlite3_mutex_leave(db->mutex);
143575 sqlite3_mutex_enter(db->mutex);
143576 pRet = db->pRollbackArg;
143577 db->xRollbackCallback = xCallback;
143578 db->pRollbackArg = pArg;
143579 sqlite3_mutex_leave(db->mutex);
143595 sqlite3_mutex_enter(db->mutex);
143596 pRet = db->pPreUpdateArg;
143597 db->xPreUpdateCallback = xCallback;
143598 db->pPreUpdateArg = pArg;
143599 sqlite3_mutex_leave(db->mutex);
143656 ** into the write-ahead-log by this database connection.
143671 sqlite3_mutex_enter(db->mutex);
143672 pRet = db->pWalArg;
143673 db->xWalCallback = xCallback;
143674 db->pWalArg = pArg;
143675 sqlite3_mutex_leave(db->mutex);
143702 /* Initialize the output variables to -1 in case an error occurs. */
143703 if( pnLog ) *pnLog = -1;
143704 if( pnCkpt ) *pnCkpt = -1;
143711 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
143716 sqlite3_mutex_enter(db->mutex);
143724 db->busyHandler.nBusy = 0;
143732 if( db->nVdbeActive==0 ){
143733 db->u1.isInterrupted = 0;
143736 sqlite3_mutex_leave(db->mutex);
143744 ** to contains a zero-length string, all attached databases are
143748 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
143755 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
143764 ** associated with the specific b-tree being checkpointed is taken by
143768 ** checkpointed. If an error is encountered it is returned immediately -
143778 assert( sqlite3_mutex_held(db->mutex) );
143779 assert( !pnLog || *pnLog==-1 );
143780 assert( !pnCkpt || *pnCkpt==-1 );
143782 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
143784 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
143799 ** This function returns true if main-memory should be used instead of
143801 ** The value returned depends on the value of db->temp_store (runtime
143806 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
143807 ** ----------------- -------------- ------------------------------
143819 return ( db->temp_store==2 );
143822 return ( db->temp_store!=1 );
143835 ** Return UTF-8 encoded English language explanation of the most recent
143846 sqlite3_mutex_enter(db->mutex);
143847 if( db->mallocFailed ){
143850 testcase( db->pErr==0 );
143851 z = (char*)sqlite3_value_text(db->pErr);
143852 assert( !db->mallocFailed );
143854 z = sqlite3ErrStr(db->errCode);
143857 sqlite3_mutex_leave(db->mutex);
143863 ** Return UTF-16 encoded English language explanation of the most recent
143883 sqlite3_mutex_enter(db->mutex);
143884 if( db->mallocFailed ){
143887 z = sqlite3_value_text16(db->pErr);
143889 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
143890 z = sqlite3_value_text16(db->pErr);
143893 ** above. If this is the case, then the db->mallocFailed flag needs to
143899 sqlite3_mutex_leave(db->mutex);
143912 if( !db || db->mallocFailed ){
143915 return db->errCode & db->errMask;
143921 if( !db || db->mallocFailed ){
143924 return db->errCode;
143927 return db ? db->iSysErrno : 0;
143954 assert( sqlite3_mutex_held(db->mutex) );
143972 ** are no active VMs, invalidate any pre-compiled statements.
143975 if( pColl && pColl->xCmp ){
143976 if( db->nVdbeActive ){
143986 ** Also, collation destructor - CollSeq.xDel() - function may need
143989 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
143990 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
143994 if( p->enc==pColl->enc ){
143995 if( p->xDel ){
143996 p->xDel(p->pUser);
143998 p->xCmp = 0;
144006 pColl->xCmp = xCompare;
144007 pColl->pUser = pCtx;
144008 pColl->xDel = xDel;
144009 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
144030 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
144075 ** If an invalid limit index is supplied, report -1.
144089 return -1;
144093 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
144094 ** there is a hard upper bound set at compile-time by a C preprocessor
144111 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
144115 return -1;
144117 oldLimit = db->aLimit[limitId];
144118 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
144120 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
144122 db->aLimit[limitId] = newLimit;
144124 return oldLimit; /* IMP: R-53341-35419 */
144128 ** This function is used to parse both URIs and non-URI filenames passed by the
144134 ** query parameter. The second argument contains the URI (or non-URI filename)
144153 const char *zUri, /* Nul-terminated URI to parse */
144168 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
144169 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
144170 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
144179 ** method that there may be extra parameters following the file-name. */
144186 iIn = 5;
144188 if( strncmp(zUri+5, "///", 3)==0 ){
144193 ** file://host/path or file:////host/path. But 5 leading slashes is a
144196 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
144201 if( zUri[5]=='/' && zUri[6]=='/' ){
144206 iIn-7, &zUri[7]);
144219 ** 0: Parsing file-name.
144257 if( zFile[iOut-1]==0 ){
144259 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
144299 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
144385 ** is UTF-8 encoded.
144388 const char *zFilename, /* Database filename UTF-8 encoded */
144450 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
144451 if( db->mutex==0 ){
144457 sqlite3_mutex_enter(db->mutex);
144458 db->errMask = 0xff;
144459 db->nDb = 2;
144460 db->magic = SQLITE_MAGIC_BUSY;
144461 db->aDb = db->aDbStatic;
144463 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
144464 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
144465 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
144466 db->autoCommit = 1;
144467 db->nextAutovac = -1;
144468 db->szMmap = sqlite3GlobalConfig.szMmap;
144469 db->nextPagesize = 0;
144470 db->nMaxSorterMmap = 0x7FFFFFFF;
144471 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
144503 sqlite3HashInit(&db->aCollSeq);
144505 sqlite3HashInit(&db->aModule);
144508 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
144509 ** and UTF-16, so add a version for each to avoid any unnecessary
144512 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
144520 if( db->mallocFailed ){
144523 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
144526 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
144527 assert( db->pDfltColl!=0 );
144532 ** Throw an error if any non-sense combination is used. If we
144541 db->openFlags = flags;
144549 rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
144551 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
144561 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
144570 sqlite3BtreeEnter(db->aDb[0].pBt);
144571 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
144572 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
144573 sqlite3BtreeLeave(db->aDb[0].pBt);
144574 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
144579 db->aDb[0].zDbSName = "main";
144580 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
144581 db->aDb[1].zDbSName = "temp";
144582 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
144584 db->magic = SQLITE_MAGIC_OPEN;
144585 if( db->mallocFailed ){
144589 /* Register all built-in functions, but do not attempt to read the
144598 /* Register any built-in FTS5 module before loading the automatic
144601 if( !db->mallocFailed && rc==SQLITE_OK ){
144606 /* Load automatic extensions - extensions that have been registered
144618 if( !db->mallocFailed ){
144625 if( !db->mallocFailed && rc==SQLITE_OK ){
144632 if( !db->mallocFailed && rc==SQLITE_OK ){
144638 if( !db->mallocFailed && rc==SQLITE_OK ){
144644 if( !db->mallocFailed && rc==SQLITE_OK){
144650 if( !db->mallocFailed && rc==SQLITE_OK){
144656 if( !db->mallocFailed && rc==SQLITE_OK){
144662 if( !db->mallocFailed && rc==SQLITE_OK){
144667 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
144668 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
144672 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
144673 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
144679 /* Enable the lookaside-malloc subsystem */
144687 assert( db->mutex!=0 || isThreadsafe==0
144689 sqlite3_mutex_leave(db->mutex);
144697 db->magic = SQLITE_MAGIC_SICK;
144739 const char *filename, /* Database filename (UTF-8) */
144755 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
144769 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
144816 sqlite3_mutex_enter(db->mutex);
144817 assert( !db->mallocFailed );
144820 sqlite3_mutex_leave(db->mutex);
144841 sqlite3_mutex_enter(db->mutex);
144842 assert( !db->mallocFailed );
144843 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
144849 sqlite3_mutex_leave(db->mutex);
144866 sqlite3_mutex_enter(db->mutex);
144867 db->xCollNeeded = xCollNeeded;
144868 db->xCollNeeded16 = 0;
144869 db->pCollNeededArg = pCollNeededArg;
144870 sqlite3_mutex_leave(db->mutex);
144887 sqlite3_mutex_enter(db->mutex);
144888 db->xCollNeeded = 0;
144889 db->xCollNeeded16 = xCollNeeded16;
144890 db->pCollNeededArg = pCollNeededArg;
144891 sqlite3_mutex_leave(db->mutex);
144919 return db->autoCommit;
144931 ** a low-level error is first detected.
144969 ** This is a convenience routine that makes sure that all thread-specific
144972 ** SQLite no longer uses thread-specific data so this routine is now a
144973 ** no-op. It is retained for historical compatibility.
144992 int *pAutoinc /* OUTPUT: True if column is auto-increment */
145013 sqlite3_mutex_enter(db->mutex);
145022 if( !pTab || pTab->pSelect ){
145031 for(iCol=0; iCol<pTab->nCol; iCol++){
145032 pCol = &pTab->aCol[iCol];
145033 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
145037 if( iCol==pTab->nCol ){
145039 iCol = pTab->iPKey;
145040 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
145060 zCollSeq = pCol->zColl;
145061 notnull = pCol->notNull!=0;
145062 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
145063 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
145094 sqlite3_mutex_leave(db->mutex);
145121 sqlite3_mutex_enter(db->mutex);
145122 db->errMask = onoff ? 0xffffffff : 0xff;
145123 sqlite3_mutex_leave(db->mutex);
145137 sqlite3_mutex_enter(db->mutex);
145156 }else if( fd->pMethods ){
145163 sqlite3_mutex_leave(db->mutex);
145211 ** is an array of integers that defines the test. Return -1 on a
145212 ** memory allocation error, 0 on success, or non-zero for an error.
145286 ** This action provides a run-time test to see whether or not
145287 ** assert() was enabled at compile-time. If X is true and assert()
145296 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
145305 ** This action provides a run-time test to see how the ALWAYS and
145306 ** NEVER macros were defined at compile-time.
145311 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
145313 ** hard-coded to true or else it asserts if its argument is false.
145314 ** The first behavior (hard-coded to true) is the case if
145319 ** The run-time test procedure might look something like this:
145322 ** // ALWAYS() and NEVER() are no-op pass-through macros
145338 ** The integer returned reveals the byte-order of the computer on which
145341 ** 1 big-endian, determined at run-time
145342 ** 10 little-endian, determined at run-time
145343 ** 432101 big-endian, determined at compile-time
145344 ** 123410 little-endian, determined at compile-time
145359 sqlite3_mutex_enter(db->mutex);
145360 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
145361 sqlite3_mutex_leave(db->mutex);
145376 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
145416 ** If parameter onoff is non-zero, configure the wrappers so that all
145427 ** Set or clear a flag that indicates that the database file is always well-
145431 ** that demonstrat invariants on well-formed database files.
145465 db->nMaxSorterMmap = va_arg(ap, int);
145484 ** or off. "tnum" is the root page of the b-tree to which the imposter
145497 sqlite3_mutex_enter(db->mutex);
145498 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
145499 db->init.busy = db->init.imposterTable = va_arg(ap,int);
145500 db->init.newTnum = va_arg(ap,int);
145501 if( db->init.busy==0 && db->init.newTnum>0 ){
145504 sqlite3_mutex_leave(db->mutex);
145546 ** Return a 64-bit integer value for a query parameter.
145566 return iDb<0 ? 0 : db->aDb[iDb].pBt;
145586 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
145594 return -1;
145598 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
145619 sqlite3_mutex_enter(db->mutex);
145621 if( db->autoCommit==0 ){
145624 Btree *pBt = db->aDb[iDb].pBt;
145634 sqlite3_mutex_leave(db->mutex);
145640 ** Open a read-transaction on the snapshot idendified by pSnapshot.
145655 sqlite3_mutex_enter(db->mutex);
145656 if( db->autoCommit==0 ){
145660 Btree *pBt = db->aDb[iDb].pBt;
145671 sqlite3_mutex_leave(db->mutex);
145691 sqlite3_mutex_enter(db->mutex);
145694 Btree *pBt = db->aDb[iDb].pBt;
145703 sqlite3_mutex_leave(db->mutex);
145718 ** Given the name of a compile-time option, return true if that option
145754 ** Return the N-th compile-time option string. If N is out of range,
145816 ** 1) Each entry in the list has a non-NULL value for either
145828 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
145833 assert( p->pUnlockConnection || p->pBlockingConnection );
145836 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
145837 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
145838 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
145839 assert( db==0 || p->pUnlockConnection!=db );
145840 assert( db==0 || p->pBlockingConnection!=db );
145850 ** db is not currently a part of the list, this function is a no-op.
145855 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
145857 *pp = (*pp)->pNextBlocked;
145872 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
145873 pp=&(*pp)->pNextBlocked
145875 db->pNextBlocked = *pp;
145897 ** Register an unlock-notify callback.
145902 ** cache. pOther is found by looking at db->pBlockingConnection.
145924 sqlite3_mutex_enter(db->mutex);
145929 db->pBlockingConnection = 0;
145930 db->pUnlockConnection = 0;
145931 db->xUnlockNotify = 0;
145932 db->pUnlockArg = 0;
145933 }else if( 0==db->pBlockingConnection ){
145942 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
145946 db->pUnlockConnection = db->pBlockingConnection;
145947 db->xUnlockNotify = xNotify;
145948 db->pUnlockArg = pArg;
145955 assert( !db->mallocFailed );
145957 sqlite3_mutex_leave(db->mutex);
145969 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
145972 db->pBlockingConnection = pBlocker;
145988 ** set to db, then invoke the configured unlock-notify callback and
145996 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
146006 /* This loop runs once for each entry in the blocked-connections list. */
146007 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
146011 if( p->pBlockingConnection==db ){
146012 p->pBlockingConnection = 0;
146016 if( p->pUnlockConnection==db ){
146017 assert( p->xUnlockNotify );
146018 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
146037 ** be passed to the unlock-notify callback is larger than the
146044 ** closed and the unlock-notify callbacks on blocked connections
146046 ** indefinitely for an unlock-notify callback that will never
146049 ** Instead, invoke the unlock-notify callback with the context
146052 ** requiring any dynamic allocation. This is sub-optimal because
146066 aArg[nArg++] = p->pUnlockArg;
146067 xUnlockNotify = p->xUnlockNotify;
146068 p->pUnlockConnection = 0;
146069 p->xUnlockNotify = 0;
146070 p->pUnlockArg = 0;
146074 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
146076 *pp = p->pNextBlocked;
146077 p->pNextBlocked = 0;
146079 pp = &p->pNextBlocked;
146117 ** This is an SQLite module implementing full-text search.
146130 /* The full-text index is stored in a series of b+tree (-like)
146139 ** The basic unit of encoding is a variable-length integer called a
146140 ** varint. We encode variable-length integers in little-endian order
146147 ** 7 bits - A
146148 ** 14 bits - BA
146149 ** 21 bits - BBA
146153 ** the encoding is not the same. SQLite varints are big-endian
146155 ** little-endian and can be up to 10 bytes in length (in theory).
146165 ** A doclist (document list) holds a docid-sorted list of hits for a
146171 ** FTS3 used to optionally store character offsets using a compile-time
146201 ** value: 123 5 9 1 1 14 35 0 234 72 0
146204 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
146207 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
146209 ** (72-2) and then terminates with the 0 at K.
146211 ** A "position-list" is the list of positions for multiple columns for
146212 ** a single docid. A "column-list" is the set of positions for a single
146213 ** column. Hence, a position-list consists of one or more column-lists,
146214 ** a document record consists of a docid followed by a position-list and
146218 ** array of varint-encoded docids.
146233 ** (further terms are delta-encoded)
146260 ** node rather than splitting into 2k and .5k nodes. My intuition is
146279 ** (further terms are delta-encoded)
146309 ** The segment directory in table %_segdir stores meta-information for
146321 ** The meta-information in the segment directory is:
146322 ** level - segment level (see below)
146323 ** idx - index within level
146324 ** - (level,idx uniquely identify a segment)
146325 ** start_block - first leaf node
146326 ** leaves_end_block - last leaf node
146327 ** end_block - last block (including interior nodes)
146328 ** root - contents of root node
146357 ** a tiny bit slower (perhaps due to more overhead in merge-time
146373 ** costs), and infrequent and non-existent terms still seem to be fast
146376 ** TODO(shess) That said, it would be nice to have a better query-side
146384 ** Since we're using a segmented structure, with no docid-oriented
146449 ** Defines the interface to tokenizers used by fulltext-search. There
146507 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
146554 ** normalized version of the token (i.e. after any case-folding and/or
146568 ** nul-terminated. This should either be fixed, or pInput/nBytes
146620 ** This is the header file for the generic hash-table implementation
146622 ** hash table implementation for the full-text indexing module.
146633 ** The internals of this structure are intended to be opaque -- client
146653 ** structure. All elements are stored on a single doubly-linked list.
146668 ** (including the null-terminator, if any). Case
146709 #define fts3HashFirst(H) ((H)->first)
146710 #define fts3HashNext(E) ((E)->next)
146711 #define fts3HashData(E) ((E)->data)
146712 #define fts3HashKey(E) ((E)->pKey)
146713 #define fts3HashKeysize(E) ((E)->nKey)
146718 #define fts3HashCount(H) ((H)->count)
146757 ** a collision when building an amalgamation with built-in FTS3.
146776 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
146778 ** as one or more b+-trees in the %_segments and %_segdir tables.
146780 ** It is possible to determine which index a b+-tree belongs to based on the
146782 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
146788 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
146795 ** make it a no-op.
146802 ** Terminator values for position-lists and column-lists.
146804 #define POS_COLUMN (1) /* Column-list terminator */
146805 #define POS_END (0) /* Position-list terminator */
146833 typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
146834 typedef short int i16; /* 2-byte (or larger) signed integer */
146835 typedef unsigned int u32; /* 4-byte unsigned integer */
146836 typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
146837 typedef sqlite3_int64 i64; /* 8-byte signed integer */
146928 ** time must share a common language-id (see the FTS4 langid= feature).
146931 ** A single FTS4 table may have multiple full-text indexes. For each index
146965 ** by special insert command 'test-no-incr-doclist'. */
146988 char *aDoclist; /* List of docids for full-text queries */
147007 ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
147021 #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
147024 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
147026 ** above. The upper 16-bits contain a combination of the following
147027 ** bits, used to describe extra constraints on full-text searches.
147047 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
147060 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
147106 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
147137 #define FTSQUERY_PHRASE 5
147174 #define FTS3_SEGCURSOR_PENDING -1
147175 #define FTS3_SEGCURSOR_ALL -2
147204 int nAdvance; /* How many seg-readers to advance */
147332 ** Write a 64-bit variable-length integer to memory starting at p[0].
147343 q[-1] &= 0x7f; /* turn off high bit in final byte */
147344 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
147345 return (int) (q - (unsigned char *)p);
147356 ** Read a 64-bit variable-length integer from memory starting at p[0].
147379 return (int)(p - pStart);
147384 ** a non-negative 32-bit integer before it is returned.
147403 return 5;
147419 ** Convert an SQL-style quoted string into a normal string by removing
147420 ** the quote characters. The conversion is done in-place. If the
147422 ** is a no-op.
147440 /* If the first byte was a '[', then the close-quote character is a ']' */
147469 ** varint that is part of a doclist (or position-list, or any other list
147486 ** clear on character p[-1]. */
147487 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
147502 assert( p->nPendingData==0 );
147503 assert( p->pSegments==0 );
147506 sqlite3_finalize(p->pSeekStmt);
147507 for(i=0; i<SizeofArray(p->aStmt); i++){
147508 sqlite3_finalize(p->aStmt[i]);
147510 sqlite3_free(p->zSegmentsTbl);
147511 sqlite3_free(p->zReadExprlist);
147512 sqlite3_free(p->zWriteExprlist);
147513 sqlite3_free(p->zContentTbl);
147514 sqlite3_free(p->zLanguageid);
147517 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
147539 ** If *pRc is initially non-zero then this routine is a no-op.
147567 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
147568 sqlite3 *db = p->db; /* Database handle */
147571 if( p->zContentTbl==0 ){
147572 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
147574 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
147575 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
147576 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
147577 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
147592 ** If *pRc is non-zero when this function is called, it is a no-op.
147604 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
147605 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
147608 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
147609 for(i=1; zCols && i<p->nColumn; i++){
147610 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
147616 zCols, p->zName, zLanguageid
147621 rc = sqlite3_declare_vtab(p->db, zSql);
147634 fts3DbExec(pRc, p->db,
147637 p->zDb, p->zName
147639 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
147647 ** If the p->bHasDocsize boolean is true (indicating that this is an
147654 sqlite3 *db = p->db; /* The database connection */
147656 if( p->zContentTbl==0 ){
147657 const char *zLanguageid = p->zLanguageid;
147662 for(i=0; zContentCols && i<p->nColumn; i++){
147663 char *z = p->azColumn[i];
147674 p->zDb, p->zName, zContentCols
147682 p->zDb, p->zName
147694 p->zDb, p->zName
147696 if( p->bHasDocsize ){
147699 p->zDb, p->zName
147702 assert( p->bHasStat==p->bFts4 );
147703 if( p->bHasStat ){
147710 ** Store the current database page-size in bytes in p->nPgsz.
147712 ** If *pRc is non-zero when this function is called, it is a no-op.
147722 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
147726 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
147729 p->nPgsz = sqlite3_column_int(pStmt, 0);
147732 p->nPgsz = 1024;
147736 assert( p->nPgsz>0 || rc!=SQLITE_OK );
147763 *pnKey = (int)(zCsr-z);
147799 ** Return a copy of input string zInput enclosed in double-quotes (") and
147802 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
147837 ** table has the three user-defined columns "a", "b", and "c", the following
147845 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
147856 if( p->zContentTbl==0 ){
147863 for(i=0; i<p->nColumn; i++){
147864 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
147866 if( p->zLanguageid ){
147872 for(i=0; i<p->nColumn; i++){
147873 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
147875 if( p->zLanguageid ){
147876 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
147880 p->zDb,
147881 (p->zContentTbl ? p->zContentTbl : p->zName),
147882 (p->zContentTbl ? "" : "_content")
147890 ** user-defined text column).
147895 ** user-defined text columns, the following string is returned:
147902 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
147919 for(i=0; i<p->nColumn; i++){
147922 if( p->zLanguageid ){
147930 ** This function interprets the string at (*pp) as a non-negative integer
147948 nInt = nInt * 10 + (p[0] - '0');
148008 nIndex--;
148009 i--;
148034 ** of each columns name, including the nul-terminator.
148063 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
148078 ** nul-terminator byte. */
148116 ** argv[0] -> module name ("fts3" or "fts4")
148117 ** argv[1] -> database name
148118 ** argv[2] -> table name
148119 ** argv[...] -> "column name" and other module argument fields.
148167 nByte = sizeof(const char *) * (argc-2);
148212 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
148213 { "prefix", 6 }, /* 1 -> PREFIX */
148214 { "compress", 8 }, /* 2 -> COMPRESS */
148215 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
148216 { "order", 5 }, /* 4 -> ORDER */
148217 { "content", 7 }, /* 5 -> CONTENT */
148218 { "languageid", 10 }, /* 6 -> LANGUAGEID */
148219 { "notindexed", 10 } /* 7 -> NOTINDEXED */
148228 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
148269 case 5: /* CONTENT */
148329 nCol--;
148372 p->db = db;
148373 p->nColumn = nCol;
148374 p->nPendingData = 0;
148375 p->azColumn = (char **)&p[1];
148376 p->pTokenizer = pTokenizer;
148377 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
148378 p->bHasDocsize = (isFts4 && bNoDocsize==0);
148379 p->bHasStat = (u8)isFts4;
148380 p->bFts4 = (u8)isFts4;
148381 p->bDescIdx = (u8)bDescIdx;
148382 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
148383 p->zContentTbl = zContent;
148384 p->zLanguageid = zLanguageid;
148387 TESTONLY( p->inTransaction = -1 );
148388 TESTONLY( p->mxSavepoint = -1 );
148390 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
148391 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
148392 p->nIndex = nIndex;
148394 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
148396 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
148399 zCsr = (char *)&p->abNotindexed[nCol];
148400 p->zName = zCsr;
148403 p->zDb = zCsr;
148417 p->azColumn[iCol] = zCsr;
148424 int n = (int)strlen(p->azColumn[iCol]);
148428 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
148430 p->abNotindexed[iCol] = 1;
148448 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
148449 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
148463 p->bHasStat = 2;
148466 /* Figure out the page-size for the database. This is required in order to
148469 p->nNodeSize = p->nPgsz-35;
148488 pTokenizer->pModule->xDestroy(pTokenizer);
148491 assert( p->pSegments==0 );
148492 *ppVTab = &p->base;
148523 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
148525 ** support estimatedRows. In that case this function is a no-op.
148530 pIdxInfo->estimatedRows = nRow;
148536 ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
148538 ** support index-info flags. In that case this function is a no-op.
148543 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
148553 ** 2. Full-text search using a MATCH operator on a non-docid column.
148559 int iCons = -1; /* Index of constraint to use */
148561 int iLangidCons = -1; /* Index of langid=x constraint, if present */
148562 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
148563 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
148570 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
148571 pInfo->estimatedCost = 5000000;
148572 for(i=0; i<pInfo->nConstraint; i++){
148574 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
148575 if( pCons->usable==0 ){
148576 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
148582 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
148583 pInfo->estimatedCost = 1e50;
148590 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
148593 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
148594 pInfo->idxNum = FTS3_DOCID_SEARCH;
148595 pInfo->estimatedCost = 1.0;
148599 /* A MATCH constraint. Use a full-text search.
148608 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
148609 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
148611 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
148612 pInfo->estimatedCost = 2.0;
148617 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
148618 && pCons->iColumn==p->nColumn + 2
148624 switch( pCons->op ){
148639 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
148643 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
148644 pInfo->aConstraintUsage[iCons].omit = 1;
148647 pInfo->idxNum |= FTS3_HAVE_LANGID;
148648 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
148651 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
148652 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
148655 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
148656 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
148662 if( pInfo->nOrderBy==1 ){
148663 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
148664 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
148665 if( pOrder->desc ){
148666 pInfo->idxStr = "DESC";
148668 pInfo->idxStr = "ASC";
148670 pInfo->orderByConsumed = 1;
148674 assert( p->pSegments==0 );
148699 ** Finalize the statement handle at pCsr->pStmt.
148706 if( pCsr->bSeekStmt ){
148707 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
148708 if( p->pSeekStmt==0 ){
148709 p->pSeekStmt = pCsr->pStmt;
148710 sqlite3_reset(pCsr->pStmt);
148711 pCsr->pStmt = 0;
148713 pCsr->bSeekStmt = 0;
148715 sqlite3_finalize(pCsr->pStmt);
148725 sqlite3_free(pCsr->aDoclist);
148726 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
148727 sqlite3Fts3ExprFree(pCsr->pExpr);
148728 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
148737 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
148739 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
148745 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
148750 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
148755 if( pCsr->pStmt==0 ){
148756 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
148758 if( p->pSeekStmt ){
148759 pCsr->pStmt = p->pSeekStmt;
148760 p->pSeekStmt = 0;
148762 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
148764 rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
148767 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
148773 ** Position the pCsr->pStmt statement so that it is on the row
148779 if( pCsr->isRequireSeek ){
148782 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
148783 pCsr->isRequireSeek = 0;
148784 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
148787 rc = sqlite3_reset(pCsr->pStmt);
148788 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
148790 ** table is missing a row that is present in the full-text index.
148793 pCsr->isEof = 1;
148807 ** a b-tree for a term or term prefix. The node data is passed to this
148812 ** of the child node that heads the sub-tree that may contain the term.
148814 ** If piLast is not NULL, then *piLast is set to the right-most child node
148815 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
148837 ** interior node. Then load the blockid of the left-child of the b-tree
148891 ** to the term from the interior node, then all terms on the sub-tree
148923 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
148924 ** contains a term. This function searches the sub-tree headed by the zNode
148929 ** left-most leaf node in the tree that may contain the specified term.
148931 ** right-most leaf node that may contain a term for which the specified
148987 ** This function is used to create delta-encoded serialized lists of FTS3
148995 assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
148996 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
149002 ** start of a position-list. After it returns, *ppPoslist points to the
149003 ** first byte after the position-list.
149021 ** of some other, multi-byte, value.
149023 ** The following while-loop moves pEnd to point to the first byte that is not
149026 ** last byte in the position-list.
149035 int n = (int)(pEnd - *ppPoslist);
149046 ** start of a column-list. After it returns, *ppPoslist points to the
149047 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
149049 ** A column-list is list of delta-encoded positions for a single column
149052 ** The column-list is terminated either by a POS_COLUMN varint (1) or
149054 ** the POS_COLUMN or POS_END that terminates the column-list.
149056 ** If pp is not NULL, then the contents of the column-list are copied
149065 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
149066 ** not part of a multi-byte varint.
149073 int n = (int)(pEnd - *ppPoslist);
149083 ** Value used to signify the end of an position-list. This is safe because
149089 ** This function is used to help parse position-lists. When this function is
149090 ** called, *pp may point to the start of the next varint in the position-list
149091 ** being parsed, or it may point to 1 byte past the end of the position-list
149095 ** If *pp points past the end of the current position-list, set *pi to
149102 ** in the position-list. Because positions are delta-encoded, the value
149107 char **pp, /* IN/OUT: Pointer into position-list buffer */
149108 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
149112 *pi -= 2;
149174 /* At this point, both p1 and p2 point to the start of column-lists
149176 ** A column-list is a list of non-negative delta-encoded varints, each
149187 iPrev -= 2;
149214 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
149229 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
149276 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
149277 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
149285 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
149291 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
149294 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
149317 ** column-number in the position list.
149345 ** Merge two position-lists as required by the NEAR operator. The argument
149351 ** Position list *pp1 corresponds to the left-hand side of the NEAR
149356 ** The output position list - written to *pp - is a copy of *pp2 with those
149429 *pVal -= iVal;
149444 ** If *pbFirst is non-zero when this function is called, then the value
149445 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
149446 ** (if bDescIdx is non-zero).
149460 iWrite = iVal - *piPrev;
149462 iWrite = *piPrev - iVal;
149474 ** arguments are 64-bit docid values. If the value of the stack variable
149475 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
149476 ** Otherwise, (i2-i1).
149481 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
149488 ** passed a non-zero value.
149521 ** current and previous docid (a positive number - since the list is in
149546 aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
149572 *pnOut = (int)(p-aOut);
149573 assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
149579 ** the output contains a copy of each position from the right-hand input
149580 ** doclist for which there is a position in the left-hand input doclist
149585 ** order, it should be passed a non-zero value.
149587 ** The right-hand input doclist is overwritten by this function.
149643 *pnRight = (int)(p - aOut);
149720 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
149721 if( pTS->aaOutput[i] ){
149723 aOut = pTS->aaOutput[i];
149724 nOut = pTS->anOutput[i];
149725 pTS->aaOutput[i] = 0;
149730 int rc = fts3DoclistOrMerge(p->bDescIdx,
149731 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
149738 sqlite3_free(pTS->aaOutput[i]);
149740 pTS->aaOutput[i] = 0;
149747 pTS->aaOutput[0] = aOut;
149748 pTS->anOutput[0] = nOut;
149760 ** doclists to merge, the merging is done pair-wise using the TermSelect
149772 if( pTS->aaOutput[0]==0 ){
149782 ** not true for order=DESC. For example, a doclist containing (1, -1)
149783 ** may be smaller than (-1), as in the first example the -1 may be stored
149784 ** as a single-byte delta, whereas in the second it must be stored as a
149789 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
149790 pTS->anOutput[0] = nDoclist;
149791 if( pTS->aaOutput[0] ){
149792 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
149801 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
149802 if( pTS->aaOutput[iOut]==0 ){
149804 pTS->aaOutput[iOut] = aMerge;
149805 pTS->anOutput[iOut] = nMerge;
149811 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
149812 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
149820 sqlite3_free(pTS->aaOutput[iOut]);
149821 pTS->aaOutput[iOut] = 0;
149825 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
149826 pTS->aaOutput[iOut] = aMerge;
149827 pTS->anOutput[iOut] = nMerge;
149836 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
149842 if( (pCsr->nSegment%16)==0 ){
149844 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
149845 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
149850 pCsr->apSegment = apNew;
149852 pCsr->apSegment[pCsr->nSegment++] = pNew;
149857 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
149866 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
149878 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
149879 ** for the pending-terms. If this is a scan, then this call must be being
149884 if( iLevel<0 && p->aIndex ){
149916 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
149934 ** Set up a cursor object for iterating through a full-text index or a
149939 int iLangid, /* Language-id to search */
149940 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
149948 assert( iIndex>=0 && iIndex<p->nIndex );
149999 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
150008 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
150011 for(i=1; bFound==0 && i<p->nIndex; i++){
150012 if( p->aIndex[i].nPrefix==nTerm ){
150014 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
150017 pSegcsr->bLookup = 1;
150021 for(i=1; bFound==0 && i<p->nIndex; i++){
150022 if( p->aIndex[i].nPrefix==nTerm+1 ){
150024 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
150029 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
150037 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
150040 pSegcsr->bLookup = !isPrefix;
150063 int iColumn, /* Column to query (or -ve for all columns) */
150068 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
150069 TermSelect tsc; /* Object for pair-wise doclist merging */
150072 pSegcsr = pTok->pSegcsr;
150076 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
150077 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
150078 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
150080 filter.zTerm = pTok->z;
150081 filter.nTerm = pTok->n;
150087 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
150104 pTok->pSegcsr = 0;
150113 ** contains a position-list following each docid. Otherwise, it is assumed
150135 ** the next row that matches. For a full-table scan, this will be
150140 ** even if we reach end-of-file. The fts3EofMethod() will be called
150146 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
150147 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
150148 pCsr->isEof = 1;
150149 rc = sqlite3_reset(pCsr->pStmt);
150151 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
150157 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
150164 ** Constants for the largest and smallest possible 64-bit signed integers.
150165 ** These macros are designed to work correctly on both 32-bit and 64-bit
150170 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
150175 ** converted to a 64-bit signed integer. Otherwise, return a copy of
150200 ** column on the left-hand side of the MATCH operator is column
150201 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
150214 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
150227 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
150228 assert( p->pSegments==0 );
150242 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
150243 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
150246 pCsr->bDesc = (idxStr[0]=='D');
150248 pCsr->bDesc = p->bDescIdx;
150250 pCsr->eSearch = (i16)eSearch;
150253 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
150260 pCsr->iLangid = 0;
150261 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
150263 assert( p->base.zErrMsg==0 );
150264 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
150265 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
150266 &p->base.zErrMsg
150275 pCsr->pNextId = pCsr->aDoclist;
150276 pCsr->iPrevId = 0;
150279 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
150281 ** full-text query or docid lookup, the statement retrieves a single
150288 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
150289 (pCsr->bDesc ? "DESC" : "ASC")
150293 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
150297 rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
150305 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
150319 if( pCsr->isEof ){
150321 pCsr->isEof = 1;
150323 return pCsr->isEof;
150334 *pRowid = pCsr->iPrevId;
150344 ** (iCol < p->nColumn) -> The value of the iCol'th user column.
150345 ** (iCol == p->nColumn) -> Magic column with the same name as the table.
150346 ** (iCol == p->nColumn+1) -> Docid column
150347 ** (iCol == p->nColumn+2) -> Langid column
150356 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
150359 assert( iCol>=0 && iCol<=p->nColumn+2 );
150361 switch( iCol-p->nColumn ){
150363 /* The special 'table-name' column */
150369 sqlite3_result_int64(pCtx, pCsr->iPrevId);
150373 if( pCsr->pExpr ){
150374 sqlite3_result_int64(pCtx, pCsr->iLangid);
150376 }else if( p->zLanguageid==0 ){
150380 iCol = p->nColumn;
150381 /* fall-through */
150385 /* A user column. Or, if this is a full-table scan, possibly the
150386 ** language-id column. Seek the cursor. */
150388 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
150389 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
150394 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
150413 ** Implementation of xSync() method. Flush the contents of the pending-terms
150414 ** hash-table to the database.
150418 /* Following an incremental-merge operation, assuming that the input
150423 ** if the height of the input segment b-trees is N, and input segments
150425 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
150426 ** small - often between 0 and 2. So the overhead of the incremental
150434 ** as it would also be required by a crisis-merge that used the same input
150437 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
150441 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
150445 && p->nLeafAdd>(nMinMerge/16)
150446 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
150449 int A; /* Incr-merge parameter A */
150453 A = p->nLeafAdd * mxLevel;
150455 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
150458 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
150464 ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
150470 if( p->bHasStat==2 ){
150471 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
150473 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
150475 p->bHasStat = (res==SQLITE_OK);
150489 assert( p->pSegments==0 );
150490 assert( p->nPendingData==0 );
150491 assert( p->inTransaction!=1 );
150492 TESTONLY( p->inTransaction = 1 );
150493 TESTONLY( p->mxSavepoint = -1; );
150494 p->nLeafAdd = 0;
150499 ** Implementation of xCommit() method. This is a no-op. The contents of
150500 ** the pending-terms hash-table have already been flushed into the database
150506 assert( p->nPendingData==0 );
150507 assert( p->inTransaction!=0 );
150508 assert( p->pSegments==0 );
150509 TESTONLY( p->inTransaction = 0 );
150510 TESTONLY( p->mxSavepoint = -1; );
150515 ** Implementation of xRollback(). Discard the contents of the pending-terms
150516 ** hash-table. Any changes made to the database are reverted by SQLite.
150521 assert( p->inTransaction!=0 );
150522 TESTONLY( p->inTransaction = 0 );
150523 TESTONLY( p->mxSavepoint = -1; );
150529 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
150534 char *p = &(*ppPoslist)[-2];
150538 while( p>pStart && (c=*p--)==0 );
150544 c = *p--;
150588 sqlite3_result_error(pContext, zErr, -1);
150607 int iCol = -1;
150611 ** the non-overloaded version would have been called instead of this one).
150617 "wrong number of arguments to function snippet()", -1);
150623 case 6: nToken = sqlite3_value_int(apVal[5]);
150624 case 5: iCol = sqlite3_value_int(apVal[4]);
150632 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
150680 p = (Fts3Table *)pCursor->base.pVtab;
150687 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
150690 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
150762 sqlite3 *db = p->db; /* Database connection */
150772 ** flushes the pending terms table. But leave the (no-op) call to
150775 assert( p->nPendingData==0 );
150780 if( p->zContentTbl==0 ){
150783 p->zDb, p->zName, zName
150787 if( p->bHasDocsize ){
150790 p->zDb, p->zName, zName
150793 if( p->bHasStat ){
150796 p->zDb, p->zName, zName
150801 p->zDb, p->zName, zName
150805 p->zDb, p->zName, zName
150813 ** Flush the contents of the pending-terms table to disk.
150818 assert( ((Fts3Table *)pVtab)->inTransaction );
150819 assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
150820 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
150821 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
150830 ** This is a no-op.
150836 assert( p->inTransaction );
150837 assert( p->mxSavepoint >= iSavepoint );
150838 TESTONLY( p->mxSavepoint = iSavepoint-1 );
150850 assert( p->inTransaction );
150851 assert( p->mxSavepoint >= iSavepoint );
150852 TESTONLY( p->mxSavepoint = iSavepoint );
150895 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
150948 /* Allocate and initialize the hash-table used to store tokenizers. */
150956 /* Load the built-in tokenizers into the hash table */
150978 /* Create the virtual table wrapper around the hash-table and overload
150984 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
151019 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
151021 ** within the union of all segments of a b-tree. Hence the name.
151024 ** segment b-tree (if the term is not a prefix or it is a prefix for which
151025 ** there exists prefix b-tree of the right length) then it may be traversed
151026 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
151037 if( pExpr->eType==FTSQUERY_PHRASE ){
151039 int nToken = pExpr->pPhrase->nToken;
151042 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
151044 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
151051 assert( pExpr->pPhrase->iDoclistToken==0 );
151052 pExpr->pPhrase->iDoclistToken = -1;
151054 *pnOr += (pExpr->eType==FTSQUERY_OR);
151055 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
151056 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
151063 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
151079 assert( iToken!=p->iDoclistToken );
151082 sqlite3_free(p->doclist.aAll);
151083 p->doclist.aAll = 0;
151084 p->doclist.nAll = 0;
151087 else if( p->iDoclistToken<0 ){
151088 p->doclist.aAll = pList;
151089 p->doclist.nAll = nList;
151092 else if( p->doclist.aAll==0 ){
151103 if( p->iDoclistToken<iToken ){
151104 pLeft = p->doclist.aAll;
151105 nLeft = p->doclist.nAll;
151108 nDiff = iToken - p->iDoclistToken;
151110 pRight = p->doclist.aAll;
151111 nRight = p->doclist.nAll;
151114 nDiff = p->iDoclistToken - iToken;
151118 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
151121 p->doclist.aAll = pRight;
151122 p->doclist.nAll = nRight;
151125 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
151130 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
151139 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151143 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
151144 Fts3PhraseToken *pToken = &p->aToken[iToken];
151145 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
151147 if( pToken->pSegcsr ){
151150 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
151155 assert( pToken->pSegcsr==0 );
151175 int iPrev = -1; /* Token number of previous deferred token */
151177 assert( pPhrase->doclist.bFreeList==0 );
151179 for(iToken=0; iToken<pPhrase->nToken; iToken++){
151180 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
151181 Fts3DeferredToken *pDeferred = pToken->pDeferred;
151191 pPhrase->doclist.pList = 0;
151192 pPhrase->doclist.nList = 0;
151205 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
151208 nPoslist = (int)(aOut - aPoslist);
151211 pPhrase->doclist.pList = 0;
151212 pPhrase->doclist.nList = 0;
151221 int nMaxUndeferred = pPhrase->iDoclistToken;
151223 pPhrase->doclist.pList = aPoslist;
151224 pPhrase->doclist.nList = nPoslist;
151225 pPhrase->doclist.iDocid = pCsr->iPrevId;
151226 pPhrase->doclist.bFreeList = 1;
151235 p2 = pPhrase->doclist.pList;
151236 nDistance = nMaxUndeferred - iPrev;
151238 p1 = pPhrase->doclist.pList;
151240 nDistance = iPrev - nMaxUndeferred;
151249 pPhrase->doclist.pList = aOut;
151251 pPhrase->doclist.bFreeList = 1;
151252 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
151255 pPhrase->doclist.pList = 0;
151256 pPhrase->doclist.nList = 0;
151272 ** This function is called for each Fts3Phrase in a full-text query
151284 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151292 ** tokens or prefix tokens that cannot use a prefix-index. */
151295 && pCsr->bDesc==pTab->bDescIdx
151296 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
151298 && pTab->bNoIncrDoclist==0
151301 for(i=0; bIncrOk==1 && i<p->nToken; i++){
151302 Fts3PhraseToken *pToken = &p->aToken[i];
151303 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
151306 if( pToken->pSegcsr ) bHaveIncr = 1;
151311 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
151312 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
151313 Fts3PhraseToken *pToken = &p->aToken[i];
151314 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
151316 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
151319 p->bIncr = 1;
151323 p->bIncr = 0;
151326 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
151334 ** pending-terms lists when writing to databases with "order=desc".
151347 u8 *pbEof /* OUT: End-of-file flag */
151370 iMul = (bDescIdx ? -1 : 1);
151373 *pnList = (int)(pEnd - pNext);
151377 int iMul = (bDescIdx ? -1 : 1);
151380 *piDocid -= (iMul * iDelta);
151387 *pnList = (int)(pSave - p);
151402 u8 *pbEof /* OUT: End-of-file flag */
151422 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
151430 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
151439 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
151441 if( pDL->pNextDocid ){
151442 pIter = pDL->pNextDocid;
151444 pIter = pDL->aAll;
151453 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
151454 pDL->iDocid += iDelta;
151456 pDL->iDocid -= iDelta;
151458 pDL->pList = pIter;
151460 pDL->nList = (int)(pIter - pDL->pList);
151462 /* pIter now points just past the 0x00 that terminates the position-
151463 ** list for document pDL->iDocid. However, if this position-list was
151466 ** with this case by advancing pIter past the zero-padding added by
151470 pDL->pNextDocid = pIter;
151471 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
151489 ** multi-token phrase. Advance it to the next matching document in the
151505 if( pPhrase->iDoclistToken==iToken ){
151506 assert( p->bIgnore==0 );
151507 assert( pPhrase->aToken[iToken].pSegcsr==0 );
151508 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
151509 p->pList = pPhrase->doclist.pList;
151510 p->nList = pPhrase->doclist.nList;
151511 p->iDocid = pPhrase->doclist.iDocid;
151513 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
151514 assert( pToken->pDeferred==0 );
151515 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
151516 if( pToken->pSegcsr ){
151517 assert( p->bIgnore==0 );
151519 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
151521 if( p->pList==0 ) *pbEof = 1;
151523 p->bIgnore = 1;
151554 Fts3Doclist *pDL = &p->doclist;
151555 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151560 assert( p->bIncr==1 );
151562 if( p->nToken==1 ){
151563 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
151564 &pDL->iDocid, &pDL->pList, &pDL->nList
151566 if( pDL->pList==0 ) bEof = 1;
151568 int bDescDoclist = pCsr->bDesc;
151572 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
151573 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
151581 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
151588 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
151592 for(i=0; i<p->nToken; i++){
151607 int nByte = a[p->nToken-1].nList;
151610 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
151612 for(i=0; i<(p->nToken-1); i++){
151617 int nDist = p->nToken-1-i;
151620 nList = (int)(pOut - aDoclist);
151623 if( i==(p->nToken-1) ){
151624 pDL->iDocid = iMax;
151625 pDL->pList = aDoclist;
151626 pDL->nList = nList;
151627 pDL->bFreeList = 1;
151654 Fts3Doclist *pDL = &p->doclist;
151655 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151657 if( p->bIncr ){
151659 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
151660 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
151661 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
151663 pDL->pList = pDL->pNextDocid;
151673 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
151681 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
151693 if( pExpr->eType==FTSQUERY_PHRASE ){
151694 int nToken = pExpr->pPhrase->nToken;
151698 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
151700 pExpr->bDeferred = (i==nToken);
151702 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
151704 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
151705 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
151706 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
151736 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
151749 if( pExpr->eType==FTSQUERY_PHRASE ){
151750 Fts3Phrase *pPhrase = pExpr->pPhrase;
151752 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
151754 pTC->pPhrase = pPhrase;
151755 pTC->iToken = i;
151756 pTC->pRoot = pRoot;
151757 pTC->pToken = &pPhrase->aToken[i];
151758 pTC->iCol = pPhrase->iColumn;
151759 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
151761 }else if( pExpr->eType!=FTSQUERY_NOT ){
151762 assert( pExpr->eType==FTSQUERY_OR
151763 || pExpr->eType==FTSQUERY_AND
151764 || pExpr->eType==FTSQUERY_NEAR
151766 assert( pExpr->pLeft && pExpr->pRight );
151767 if( pExpr->eType==FTSQUERY_OR ){
151768 pRoot = pExpr->pLeft;
151772 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
151773 if( pExpr->eType==FTSQUERY_OR ){
151774 pRoot = pExpr->pRight;
151778 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
151796 if( pCsr->nRowAvg==0 ){
151808 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
151830 pCsr->nDoc = nDoc;
151831 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
151832 assert( pCsr->nRowAvg>0 );
151836 *pnPage = pCsr->nRowAvg;
151860 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151875 if( pTab->zContentTbl ){
151897 ** to retrieve the entire doclist for the token from the full-text index.
151903 ** b. Part of a multi-token phrase.
151915 ** (nMinEst + 4^nOther - 1) / (4^nOther)
151924 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
151931 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
151936 Fts3PhraseToken *pToken = pTC->pToken;
151937 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
151938 fts3SegReaderCursorFree(pToken->pSegcsr);
151939 pToken->pSegcsr = 0;
151942 ** for-loop. Except, limit the value to 2^24 to prevent it from
151943 ** overflowing the 32-bit integer it is stored in. */
151946 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
151948 ** part of a multi-token phrase. Either way, the entire doclist will
151950 Fts3PhraseToken *pToken = pTC->pToken;
151953 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
151957 pTab, pTC->pPhrase, pTC->iToken,pList,nList
151963 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
151969 pTC->pToken = 0;
151977 ** the full-text query currently stored in pCsr->pExpr. To iterate through
151983 ** if( pCsr->bEof ) break;
151984 ** ... return row pCsr->iPrevId to the caller ...
151988 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151994 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
151998 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
152014 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
152015 nToken = (int)(pTC-aTC);
152016 nOr = (int)(ppOr-apOr);
152030 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
152038 if( pPhrase->doclist.bFreeList ){
152039 sqlite3_free(pPhrase->doclist.pList);
152041 pPhrase->doclist.pList = 0;
152042 pPhrase->doclist.nList = 0;
152043 pPhrase->doclist.bFreeList = 0;
152051 ** abc NEAR/5 "def ghi"
152053 ** Parameter nNear is passed the NEAR distance of the expression (5 in
152062 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
152075 int nParam1 = nNear + pPhrase->nToken;
152082 assert( pPhrase->doclist.pList );
152084 p2 = pOut = pPhrase->doclist.pList;
152089 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
152090 assert( pPhrase->doclist.pList[nNew]=='\0' );
152091 assert( nNew<=pPhrase->doclist.nList && nNew>0 );
152092 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
152093 pPhrase->doclist.nList = nNew;
152094 *paPoslist = pPhrase->doclist.pList;
152095 *pnToken = pPhrase->nToken;
152102 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
152106 ** or descending if it is non-zero.
152111 ** Fts3Expr.bEof (non-zero if EOF - there is no next row)
152118 ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
152119 ** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
152126 ** the db. In this case the position-list is not populated at all.
152129 ** more non-deferred tokens, then the expression is advanced to the
152130 ** next possible match, considering only non-deferred tokens. In other
152148 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
152149 assert( pExpr->bEof==0 );
152150 pExpr->bStart = 1;
152152 switch( pExpr->eType ){
152155 Fts3Expr *pLeft = pExpr->pLeft;
152156 Fts3Expr *pRight = pExpr->pRight;
152157 assert( !pLeft->bDeferred || !pRight->bDeferred );
152159 if( pLeft->bDeferred ){
152163 pExpr->iDocid = pRight->iDocid;
152164 pExpr->bEof = pRight->bEof;
152165 }else if( pRight->bDeferred ){
152169 pExpr->iDocid = pLeft->iDocid;
152170 pExpr->bEof = pLeft->bEof;
152175 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
152176 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
152184 pExpr->iDocid = pLeft->iDocid;
152185 pExpr->bEof = (pLeft->bEof || pRight->bEof);
152186 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
152187 assert( pRight->eType==FTSQUERY_PHRASE );
152188 if( pRight->pPhrase->doclist.aAll ){
152189 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
152190 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
152191 memset(pDl->pList, 0, pDl->nList);
152195 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
152196 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
152197 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
152198 memset(pDl->pList, 0, pDl->nList);
152208 Fts3Expr *pLeft = pExpr->pLeft;
152209 Fts3Expr *pRight = pExpr->pRight;
152210 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
152212 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
152213 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
152215 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
152217 }else if( pLeft->bEof || iCmp>0 ){
152224 pExpr->bEof = (pLeft->bEof && pRight->bEof);
152225 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
152226 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
152227 pExpr->iDocid = pLeft->iDocid;
152229 pExpr->iDocid = pRight->iDocid;
152236 Fts3Expr *pLeft = pExpr->pLeft;
152237 Fts3Expr *pRight = pExpr->pRight;
152239 if( pRight->bStart==0 ){
152241 assert( *pRc!=SQLITE_OK || pRight->bStart );
152245 if( pLeft->bEof==0 ){
152247 && !pRight->bEof
152248 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
152253 pExpr->iDocid = pLeft->iDocid;
152254 pExpr->bEof = pLeft->bEof;
152259 Fts3Phrase *pPhrase = pExpr->pPhrase;
152261 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
152262 pExpr->iDocid = pPhrase->doclist.iDocid;
152275 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
152295 ** +--NEAR--+ <-- root of NEAR query
152297 ** +--NEAR--+ "z"
152299 ** +--NEAR--+ "y"
152303 ** The right-hand child of a NEAR node is always a phrase. The
152304 ** left-hand child may be either a phrase or a NEAR node. There are
152305 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
152308 && pExpr->eType==FTSQUERY_NEAR
152309 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
152316 for(p=pExpr; p->pLeft; p=p->pLeft){
152317 assert( p->pRight->pPhrase->doclist.nList>0 );
152318 nTmp += p->pRight->pPhrase->doclist.nList;
152320 nTmp += p->pPhrase->doclist.nList;
152326 char *aPoslist = p->pPhrase->doclist.pList;
152327 int nToken = p->pPhrase->nToken;
152329 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
152330 Fts3Phrase *pPhrase = p->pRight->pPhrase;
152331 int nNear = p->nNear;
152335 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
152336 nToken = pExpr->pRight->pPhrase->nToken;
152337 for(p=pExpr->pLeft; p && res; p=p->pLeft){
152340 assert( p->pParent && p->pParent->pLeft==p );
152341 nNear = p->pParent->nNear;
152343 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
152357 ** Assuming no error occurs or has occurred, It returns non-zero if the
152361 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
152373 switch( pExpr->eType ){
152377 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
152378 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
152395 ** (as "b" is part of a non-matching NEAR clause).
152398 && pExpr->eType==FTSQUERY_NEAR
152399 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
152402 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
152403 if( p->pRight->iDocid==pCsr->iPrevId ){
152404 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
152407 if( p->iDocid==pCsr->iPrevId ){
152408 fts3EvalInvalidatePoslist(p->pPhrase);
152415 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
152416 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
152423 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
152424 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
152430 if( pCsr->pDeferred
152431 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
152433 Fts3Phrase *pPhrase = pExpr->pPhrase;
152434 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
152435 if( pExpr->bDeferred ){
152439 bHit = (pPhrase->doclist.pList!=0);
152440 pExpr->iDocid = pCsr->iPrevId;
152444 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
152455 ** iterating through the results of a full-text query. At this point the
152488 if( pCsr->pDeferred ){
152494 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
152496 /* Free the position-lists accumulated for each deferred token above. */
152509 Fts3Expr *pExpr = pCsr->pExpr;
152510 assert( pCsr->isEof==0 );
152512 pCsr->isEof = 1;
152515 if( pCsr->isRequireSeek==0 ){
152516 sqlite3_reset(pCsr->pStmt);
152518 assert( sqlite3_data_count(pCsr->pStmt)==0 );
152520 pCsr->isEof = pExpr->bEof;
152521 pCsr->isRequireSeek = 1;
152522 pCsr->isMatchinfoNeeded = 1;
152523 pCsr->iPrevId = pExpr->iDocid;
152524 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
152530 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
152531 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
152533 pCsr->isEof = 1;
152545 ** a no-op. If an error occurs within this function, *pRc is set to an
152554 Fts3Phrase *pPhrase = pExpr->pPhrase;
152558 if( pPhrase->bIncr ){
152560 for(i=0; i<pPhrase->nToken; i++){
152561 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
152562 assert( pToken->pDeferred==0 );
152563 if( pToken->pSegcsr ){
152564 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
152569 pPhrase->doclist.pNextDocid = 0;
152570 pPhrase->doclist.iDocid = 0;
152571 pPhrase->pOrPoslist = 0;
152574 pExpr->iDocid = 0;
152575 pExpr->bEof = 0;
152576 pExpr->bStart = 0;
152578 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
152579 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
152587 ** the values in Fts3Expr.aMI[] according to the position-list currently
152588 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
152593 Fts3Phrase *pPhrase = pExpr->pPhrase;
152594 if( pPhrase && pPhrase->doclist.pList ){
152596 char *p = pPhrase->doclist.pList;
152610 pExpr->aMI[iCol*3 + 1] += iCnt;
152611 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
152618 fts3EvalUpdateCounts(pExpr->pLeft);
152619 fts3EvalUpdateCounts(pExpr->pRight);
152640 assert( pExpr->eType==FTSQUERY_PHRASE );
152641 if( pExpr->aMI==0 ){
152642 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
152646 sqlite3_int64 iPrevId = pCsr->iPrevId;
152652 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
152653 pRoot = pRoot->pParent;
152655 iDocid = pRoot->iDocid;
152656 bEof = pRoot->bEof;
152657 assert( pRoot->bStart );
152660 for(p=pRoot; p; p=p->pLeft){
152661 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
152662 assert( pE->aMI==0 );
152663 pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
152664 if( !pE->aMI ) return SQLITE_NOMEM;
152665 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
152670 while( pCsr->isEof==0 && rc==SQLITE_OK ){
152674 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
152675 assert( sqlite3_data_count(pCsr->pStmt)==0 );
152679 pCsr->isEof = pRoot->bEof;
152680 pCsr->isRequireSeek = 1;
152681 pCsr->isMatchinfoNeeded = 1;
152682 pCsr->iPrevId = pRoot->iDocid;
152683 }while( pCsr->isEof==0
152684 && pRoot->eType==FTSQUERY_NEAR
152688 if( rc==SQLITE_OK && pCsr->isEof==0 ){
152693 pCsr->isEof = 0;
152694 pCsr->iPrevId = iPrevId;
152697 pRoot->bEof = bEof;
152703 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
152708 assert( pRoot->bEof==0 );
152709 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
152738 ** * If a phrase contains some deferred tokens (and some non-deferred
152740 ** the non-deferred tokens instead of actual phrase occurrences.
152750 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
152754 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
152755 assert( pCsr->nDoc>0 );
152756 for(iCol=0; iCol<pTab->nColumn; iCol++){
152757 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
152758 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
152763 assert( pExpr->aMI );
152764 for(iCol=0; iCol<pTab->nColumn; iCol++){
152765 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
152766 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
152779 ** a position-list indicating the occurrences of the phrase in column iCol
152785 ** if the requested column contains "a b X c d X X" and the position-list
152799 Fts3Phrase *pPhrase = pExpr->pPhrase;
152800 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
152808 assert( iCol>=0 && iCol<pTab->nColumn );
152809 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
152813 iDocid = pExpr->iDocid;
152814 pIter = pPhrase->doclist.pList;
152815 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
152817 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
152826 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
152830 for(p=pExpr->pParent; p; p=p->pParent){
152831 if( p->eType==FTSQUERY_OR ) bOr = 1;
152832 if( p->eType==FTSQUERY_NEAR ) pNear = p;
152833 if( p->bEof ) bTreeEof = 1;
152840 if( pPhrase->bIncr ){
152841 int bEofSave = pNear->bEof;
152843 while( rc==SQLITE_OK && !pNear->bEof ){
152845 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
152847 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
152850 while( rc==SQLITE_OK && !pNear->bEof ){
152857 for(p=pNear; p; p=p->pLeft){
152861 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
152862 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
152863 assert( pTest->eType==FTSQUERY_PHRASE );
152864 pPh = pTest->pPhrase;
152866 pIter = pPh->pOrPoslist;
152867 iDocid = pPh->iOrDocid;
152868 if( pCsr->bDesc==bDescDoclist ){
152869 bEof = !pPh->doclist.nAll ||
152870 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
152871 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
152873 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
152878 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
152879 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
152882 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
152887 pPh->pOrPoslist = pIter;
152888 pPh->iOrDocid = iDocid;
152889 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
152893 pIter = pPhrase->pOrPoslist;
152924 ** * the contents of pPhrase->doclist, and
152930 sqlite3_free(pPhrase->doclist.aAll);
152932 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
152933 for(i=0; i<pPhrase->nToken; i++){
152934 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
152935 pPhrase->aToken[i].pSegcsr = 0;
153003 int nStop; /* Byte-length of string zStop */
153047 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
153048 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
153050 if( argc!=4 && argc!=5 ) goto bad_args;
153054 if( argc==5 ){
153075 p->pFts3Tab = (Fts3Table *)&p[1];
153076 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
153077 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
153078 p->pFts3Tab->db = db;
153079 p->pFts3Tab->nIndex = 1;
153081 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
153082 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
153083 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
153100 Fts3Table *pFts3 = p->pFts3Tab;
153104 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
153105 sqlite3_finalize(pFts3->aStmt[i]);
153107 sqlite3_free(pFts3->zSegmentsTbl);
153117 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
153124 int iEq = -1;
153125 int iGe = -1;
153126 int iLe = -1;
153127 int iLangid = -1;
153133 if( pInfo->nOrderBy==1
153134 && pInfo->aOrderBy[0].iColumn==0
153135 && pInfo->aOrderBy[0].desc==0
153137 pInfo->orderByConsumed = 1;
153142 for(i=0; i<pInfo->nConstraint; i++){
153143 if( pInfo->aConstraint[i].usable ){
153144 int op = pInfo->aConstraint[i].op;
153145 int iCol = pInfo->aConstraint[i].iColumn;
153161 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
153162 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
153163 pInfo->estimatedCost = 5;
153165 pInfo->idxNum = 0;
153166 pInfo->estimatedCost = 20000;
153168 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
153169 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
153170 pInfo->estimatedCost /= 2;
153173 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
153174 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
153175 pInfo->estimatedCost /= 2;
153179 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
153180 pInfo->estimatedCost--;
153187 ** xOpen - Open a cursor.
153203 ** xClose - Close a cursor.
153206 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
153210 sqlite3Fts3SegReaderFinish(&pCsr->csr);
153211 sqlite3_free((void *)pCsr->filter.zTerm);
153212 sqlite3_free(pCsr->zStop);
153213 sqlite3_free(pCsr->aStat);
153219 if( nSize>pCsr->nStat ){
153221 aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
153225 memset(&aNew[pCsr->nStat], 0,
153226 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
153228 pCsr->aStat = aNew;
153229 pCsr->nStat = nSize;
153235 ** xNext - Advance the cursor to the next row, if any.
153239 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
153243 pCsr->iRowid++;
153245 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
153246 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
153249 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
153252 int nDoclist = pCsr->csr.nDoclist;
153253 char *aDoclist = pCsr->csr.aDoclist;
153258 if( pCsr->zStop ){
153259 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
153260 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
153261 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
153262 pCsr->isEof = 1;
153268 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
153278 pCsr->aStat[0].nDoc++;
153294 pCsr->aStat[1].nDoc++;
153305 pCsr->aStat[iCol+1].nOcc++;
153306 pCsr->aStat[0].nOcc++;
153314 pCsr->aStat[iCol+1].nDoc++;
153320 pCsr->iCol = 0;
153323 pCsr->isEof = 1;
153329 ** xFilter - Initialize a cursor to point at the start of its data.
153339 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
153344 int iEq = -1; /* Index of term=? value in apVal */
153345 int iGe = -1; /* Index of term>=? value in apVal */
153346 int iLe = -1; /* Index of term<=? value in apVal */
153347 int iLangid = -1; /* Index of languageid=? value in apVal */
153375 testcase(pCsr->filter.zTerm);
153376 sqlite3Fts3SegReaderFinish(&pCsr->csr);
153377 sqlite3_free((void *)pCsr->filter.zTerm);
153378 sqlite3_free(pCsr->aStat);
153379 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
153381 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
153382 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
153386 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
153388 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
153389 pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
153390 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
153395 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
153396 pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
153397 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
153410 pCsr->iLangid = iLangVal;
153413 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
153416 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
153424 ** xEof - Return true if the cursor is at EOF, or false otherwise.
153428 return pCsr->isEof;
153432 ** xColumn - Return a column value.
153441 assert( p->isEof==0 );
153444 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
153448 if( p->iCol ){
153449 sqlite3_result_int(pCtx, p->iCol-1);
153451 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
153456 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
153460 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
153465 sqlite3_result_int(pCtx, p->iLangid);
153473 ** xRowid - Return the current rowid for the cursor.
153480 *pRowid = pCsr->iRowid;
153537 ** (the right-hand argument to the MATCH operator). Because the supported
153539 ** hand-coded.
153554 ** c) The old syntax supports the "-" token qualifier. This is not
153564 ** non-zero the new syntax is activated. This is so both syntaxes can
153576 ** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
153611 ** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
153612 ** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
153620 int bFts4; /* True to allow FTS4-only syntax */
153623 int isNot; /* True if getNextNode() sees a unary - */
153661 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
153665 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
153668 pCsr->pTokenizer = pTokenizer;
153669 if( pModule->iVersion>=1 ){
153670 rc = pModule->xLanguageid(pCsr, iLangid);
153672 pModule->xClose(pCsr);
153706 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
153707 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
153720 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
153726 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
153733 pRet->eType = FTSQUERY_PHRASE;
153734 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
153735 pRet->pPhrase->nToken = 1;
153736 pRet->pPhrase->iColumn = iCol;
153737 pRet->pPhrase->aToken[0].n = nToken;
153738 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
153739 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
153742 pRet->pPhrase->aToken[0].isPrefix = 1;
153748 && iStart>0 && z[iStart-1]=='-'
153750 pParse->isNot = 1;
153751 iStart--;
153752 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
153753 pRet->pPhrase->aToken[0].bFirst = 1;
153754 iStart--;
153766 pModule->xClose(pCursor);
153775 ** Enlarge a memory allocation. If an out-of-memory allocation occurs,
153803 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
153804 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
153834 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
153840 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
153851 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
153857 pToken->n = nByte;
153858 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
153859 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
153864 pModule->xClose(pCursor);
153874 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
153875 p->eType = FTSQUERY_PHRASE;
153876 p->pPhrase = (Fts3Phrase *)&p[1];
153877 p->pPhrase->iColumn = pParse->iDefaultCol;
153878 p->pPhrase->nToken = nToken;
153880 zBuf = (char *)&p->pPhrase->aToken[nToken];
153888 for(jj=0; jj<p->pPhrase->nToken; jj++){
153889 p->pPhrase->aToken[jj].z = zBuf;
153890 zBuf += p->pPhrase->aToken[jj].n;
153900 pModule->xClose(pCursor);
153942 pParse->isNot = 0;
153948 nInput--;
153959 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
153963 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
153965 int nKey = pKey->n;
153969 if( pKey->eType==FTSQUERY_NEAR ){
153971 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
153973 for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
153974 nNear = nNear * 10 + (zInput[nKey] - '0');
153991 pRet->eType = pKey->eType;
153992 pRet->nNear = nNear;
153994 *pnConsumed = (int)((zInput - z) + nKey);
154011 *pnConsumed = (int)((zInput - z) + ii + 1);
154015 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
154021 pParse->nNest++;
154022 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
154024 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
154027 pParse->nNest--;
154028 *pnConsumed = (int)((zInput - z) + 1);
154045 iCol = pParse->iDefaultCol;
154047 for(ii=0; ii<pParse->nCol; ii++){
154048 const char *zStr = pParse->azCol[ii];
154054 iColLen = (int)((zInput - z) + nStr + 1);
154058 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
154083 assert( p->eType!=FTSQUERY_PHRASE );
154085 return p->eType;
154086 }else if( p->eType==FTSQUERY_NEAR ){
154088 }else if( p->eType==FTSQUERY_OR ){
154091 assert( p->eType==FTSQUERY_AND );
154109 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
154110 pSplit = pSplit->pParent;
154113 if( pSplit->pParent ){
154114 assert( pSplit->pParent->pRight==pSplit );
154115 pSplit->pParent->pRight = pNew;
154116 pNew->pParent = pSplit->pParent;
154120 pNew->pLeft = pSplit;
154121 pSplit->pParent = pNew;
154127 ** closing bracket - ')' - is encountered.
154159 && p->eType==FTSQUERY_PHRASE && pParse->isNot
154168 pNot->eType = FTSQUERY_NOT;
154169 pNot->pRight = p;
154170 p->pParent = pNot;
154172 pNot->pLeft = pNotBranch;
154173 pNotBranch->pParent = pNot;
154178 int eType = p->eType;
154179 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
154202 pAnd->eType = FTSQUERY_AND;
154217 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
154218 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
154227 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
154228 pPrev->pRight = p;
154229 p->pParent = pPrev;
154243 nIn -= nByte;
154258 while( pIter->pLeft ){
154259 pIter = pIter->pLeft;
154261 pIter->pLeft = pRet;
154262 pRet->pParent = pIter;
154267 *pnConsumed = n - nIn;
154289 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
154291 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
154304 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
154313 int eType = pRoot->eType; /* Type of node in this tree */
154333 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
154334 for(p=pRoot; p->eType==eType; p=p->pLeft){
154335 assert( p->pParent==0 || p->pParent->pLeft==p );
154336 assert( p->pLeft && p->pRight );
154342 Fts3Expr *pParent = p->pParent; /* Current parent of p */
154344 assert( pParent==0 || pParent->pLeft==p );
154345 p->pParent = 0;
154347 pParent->pLeft = 0;
154351 rc = fts3ExprBalance(&p, nMaxDepth-1);
154360 pFree->pLeft = apLeaf[iLvl];
154361 pFree->pRight = p;
154362 pFree->pLeft->pParent = pFree;
154363 pFree->pRight->pParent = pFree;
154366 pFree = pFree->pParent;
154367 p->pParent = 0;
154381 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
154384 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
154385 pParent->pRight->pParent = pParent->pParent;
154386 if( pParent->pParent ){
154387 pParent->pParent->pLeft = pParent->pRight;
154390 pRoot = pParent->pRight;
154395 pParent->pParent = pFree;
154405 p->pParent = 0;
154408 pFree->pRight = p;
154409 pFree->pLeft = apLeaf[i];
154410 pFree->pLeft->pParent = pFree;
154411 pFree->pRight->pParent = pFree;
154414 pFree = pFree->pParent;
154415 p->pParent = 0;
154429 pFree = pDel->pParent;
154438 Fts3Expr *pLeft = pRoot->pLeft;
154439 Fts3Expr *pRight = pRoot->pRight;
154441 pRoot->pLeft = 0;
154442 pRoot->pRight = 0;
154443 pLeft->pParent = 0;
154444 pRight->pParent = 0;
154446 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
154448 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
154456 pRoot->pLeft = pLeft;
154457 pLeft->pParent = pRoot;
154458 pRoot->pRight = pRight;
154459 pRight->pParent = pRoot;
154487 int bFts4, /* True to allow FTS4-only syntax */
154528 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
154532 ** nul-terminated string and the length is determined using strlen().
154538 ** Column names must be nul-terminated strings.
154541 ** that appears on the left-hand-side of the MATCH operator (the default
154543 ** specified as part of the query string), or -1 if tokens may by default
154550 int bFts4, /* True to allow FTS4-only syntax */
154591 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
154592 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
154593 sqlite3_free(p->aMI);
154606 assert( pDel==0 || pDel->pParent==0 );
154607 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
154608 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
154611 Fts3Expr *pParent = p->pParent;
154613 if( pParent && p==pParent->pLeft && pParent->pRight ){
154614 p = pParent->pRight;
154615 while( p && (p->pLeft || p->pRight) ){
154616 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
154617 p = (p->pLeft ? p->pLeft : p->pRight);
154635 ** Function to query the hash-table of tokenizers (see README.tokenizers).
154647 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
154652 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
154676 switch( pExpr->eType ){
154678 Fts3Phrase *pPhrase = pExpr->pPhrase;
154681 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
154682 for(i=0; zBuf && i<pPhrase->nToken; i++){
154684 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
154685 (pPhrase->aToken[i].isPrefix?"+":"")
154692 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
154706 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
154709 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
154748 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
154759 sqlite3_result_error(context, "No such tokenizer module", -1);
154763 rc = pModule->xCreate(0, 0, &pTokenizer);
154769 pTokenizer->pModule = pModule;
154773 nCol = argc-2;
154798 sqlite3_result_error(context, "Error parsing expression", -1);
154802 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
154810 rc = pModule->xDestroy(pTokenizer);
154821 db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
154825 -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
154847 ** This is the implementation of generic hash-tables used in SQLite.
154849 ** implementation for the full-text indexing module.
154897 pNew->keyClass = keyClass;
154898 pNew->copyKey = copyKey;
154899 pNew->first = 0;
154900 pNew->count = 0;
154901 pNew->htsize = 0;
154902 pNew->ht = 0;
154913 elem = pH->first;
154914 pH->first = 0;
154915 fts3HashFree(pH->ht);
154916 pH->ht = 0;
154917 pH->htsize = 0;
154919 Fts3HashElem *next_elem = elem->next;
154920 if( pH->copyKey && elem->pKey ){
154921 fts3HashFree(elem->pKey);
154926 pH->count = 0;
154938 nKey--;
154953 while( nKey-- > 0 ){
155007 pHead = pEntry->chain;
155009 pNew->next = pHead;
155010 pNew->prev = pHead->prev;
155011 if( pHead->prev ){ pHead->prev->next = pNew; }
155012 else { pH->first = pNew; }
155013 pHead->prev = pNew;
155015 pNew->next = pH->first;
155016 if( pH->first ){ pH->first->prev = pNew; }
155017 pNew->prev = 0;
155018 pH->first = pNew;
155020 pEntry->count++;
155021 pEntry->chain = pNew;
155029 ** Return non-zero if a memory allocation error occurs.
155036 assert( (new_size & (new_size-1))==0 );
155039 fts3HashFree(pH->ht);
155040 pH->ht = new_ht;
155041 pH->htsize = new_size;
155042 xHash = ftsHashFunction(pH->keyClass);
155043 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
155044 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
155045 next_elem = elem->next;
155065 if( pH->ht ){
155066 struct _fts3ht *pEntry = &pH->ht[h];
155067 elem = pEntry->chain;
155068 count = pEntry->count;
155069 xCompare = ftsCompareFunction(pH->keyClass);
155070 while( count-- && elem ){
155071 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
155074 elem = elem->next;
155089 if( elem->prev ){
155090 elem->prev->next = elem->next;
155092 pH->first = elem->next;
155094 if( elem->next ){
155095 elem->next->prev = elem->prev;
155097 pEntry = &pH->ht[h];
155098 if( pEntry->chain==elem ){
155099 pEntry->chain = elem->next;
155101 pEntry->count--;
155102 if( pEntry->count<=0 ){
155103 pEntry->chain = 0;
155105 if( pH->copyKey && elem->pKey ){
155106 fts3HashFree(elem->pKey);
155109 pH->count--;
155110 if( pH->count<=0 ){
155111 assert( pH->first==0 );
155112 assert( pH->count==0 );
155125 if( pH==0 || pH->ht==0 ) return 0;
155126 xHash = ftsHashFunction(pH->keyClass);
155129 assert( (pH->htsize & (pH->htsize-1))==0 );
155130 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
155142 return pElem ? pElem->data : 0;
155173 xHash = ftsHashFunction(pH->keyClass);
155176 assert( (pH->htsize & (pH->htsize-1))==0 );
155177 h = hraw & (pH->htsize-1);
155180 void *old_data = elem->data;
155184 elem->data = data;
155189 if( (pH->htsize==0 && fts3Rehash(pH,8))
155190 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
155192 pH->count = 0;
155195 assert( pH->htsize>0 );
155198 if( pH->copyKey && pKey!=0 ){
155199 new_elem->pKey = fts3HashMalloc( nKey );
155200 if( new_elem->pKey==0 ){
155204 memcpy((void*)new_elem->pKey, pKey, nKey);
155206 new_elem->pKey = (void*)pKey;
155208 new_elem->nKey = nKey;
155209 pH->count++;
155210 assert( pH->htsize>0 );
155211 assert( (pH->htsize & (pH->htsize-1))==0 );
155212 h = hraw & (pH->htsize-1);
155213 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
155214 new_elem->data = data;
155233 ** Implementation of the full-text-search tokenizer that implements
155292 *ppTokenizer = &t->base;
155306 ** string to be tokenized is zInput[0..nInput-1]. A cursor
155322 c->zInput = zInput;
155324 c->nInput = 0;
155326 c->nInput = (int)strlen(zInput);
155328 c->nInput = nInput;
155330 c->iOffset = 0; /* start tokenizing at the beginning */
155331 c->iToken = 0;
155332 c->zToken = NULL; /* no space allocated, yet. */
155333 c->nAllocated = 0;
155335 *ppCursor = &c->base;
155345 sqlite3_free(c->zToken);
155376 j = cType[x-'a'];
155385 j = cType[x-'a'];
155386 if( j<2 ) return 1-j;
155398 ** vowel-consonant pairs followed by an optional vowel. "m" is the
155402 ** Return true if the m-value for z is 1 or more. In other words,
155445 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
155464 ** are consonant-vowel-consonent and where the final consonant
155501 *(--z) = *(zTo++);
155510 ** US-ASCII case folding. If the input word is too long (more
155521 zOut[i] = c - 'A' + 'a';
155529 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
155540 ** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
155544 ** Any upper-case characters in the US-ASCII character set ([A-Z])
155545 ** are converted to lower case. Upper-case UTF characters are
155552 ** are taken from each end. US-ASCII case folding still applies.
155555 ** in [a-zA-Z] then no stemming is attempted and this routine just
155556 ** copies the input into the input into the output with US-ASCII
155566 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
155572 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
155575 zReverse[j] = c + 'a' - 'A';
155579 /* The use of a character not in [a-zA-Z] means that we fallback
155585 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
155615 *(--z) = 'e';
155771 /* Step 5a */
155780 /* Step 5b */
155791 zOut[--i] = *(z++);
155805 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
155809 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
155824 const char *z = c->zInput;
155826 while( c->iOffset<c->nInput ){
155830 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
155831 c->iOffset++;
155834 /* Count non-delimiter characters. */
155835 iStartOffset = c->iOffset;
155836 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
155837 c->iOffset++;
155840 if( c->iOffset>iStartOffset ){
155841 int n = c->iOffset-iStartOffset;
155842 if( n>c->nAllocated ){
155844 c->nAllocated = n+20;
155845 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
155847 c->zToken = pNew;
155849 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
155850 *pzToken = c->zToken;
155852 *piEndOffset = c->iOffset;
155853 *piPosition = c->iToken++;
155861 ** The set of routines that implement the porter-stemmer tokenizer
155899 ** This is part of an SQLite module implementing full-text search.
155919 ** Return true if the two-argument version of fts3_tokenizer()
155926 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
155934 ** SELECT <function-name>(<key-name>);
155935 ** SELECT <function-name>(<key-name>, <pointer>);
155937 ** where <function-name> is the name passed as the second argument
155942 ** to the string <key-name>. If <pointer> is not specified, then
155943 ** the string <key-name> must already exist in the has table. Otherwise,
155948 ** to string <key-name> (after the hash-table is updated, if applicable).
155972 sqlite3_result_error(context, "argument type mismatch", -1);
155978 sqlite3_result_error(context, "out of memory", -1);
155981 sqlite3_result_error(context, "fts3tokenize disabled", -1);
155990 sqlite3_result_error(context, zErr, -1);
156005 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
156045 *pn = (int)(z2-z1);
156059 char *zEnd; /* Pointer to nul-term of zCopy */
156096 rc = m->xCreate(iArg, aArg, ppTok);
156101 (*ppTok)->pModule = m;
156125 ** SELECT <function-name>(<key-name>, ..., <input-string>);
156127 ** where <function-name> is the name passed as the second argument
156132 ** list. For each token in the <input-string>, three elements are
156135 ** substring of <input-string> associated with the token. For example,
156136 ** using the built-in "simple" tokenizer:
156174 sqlite3_result_error(context, "insufficient arguments", -1);
156180 nInput = sqlite3_value_bytes(argv[argc-1]);
156181 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
156188 sqlite3_result_error(context, zErr2, -1);
156196 for(i=1; i<argc-1; i++){
156197 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
156200 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
156204 pTokenizer->pModule = p;
156210 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
156214 nToken = iEnd-iStart;
156218 if( SQLITE_OK!=p->xClose(pCsr) ){
156222 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
156229 sqlite3_result_error(context, zErr, -1);
156231 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
156246 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
156251 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
156270 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
156275 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
156293 ** can be used as designed by the C-code in the queryTokenizer and
156337 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
156387 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
156418 ** Implementation of the "simple" full-text-search tokenizer.
156457 return c<0x80 && t->delim[c];
156477 ** else we need to reindex. One solution would be a meta-table to
156485 /* We explicitly don't support UTF-8 delimiters for now. */
156490 t->delim[ch] = 1;
156493 /* Mark non-alphanumeric ASCII characters as delimiters */
156496 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
156500 *ppTokenizer = &t->base;
156514 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
156530 c->pInput = pInput;
156532 c->nBytes = 0;
156534 c->nBytes = (int)strlen(pInput);
156536 c->nBytes = nBytes;
156538 c->iOffset = 0; /* start tokenizing at the beginning */
156539 c->iToken = 0;
156540 c->pToken = NULL; /* no space allocated, yet. */
156541 c->nTokenAllocated = 0;
156543 *ppCursor = &c->base;
156553 sqlite3_free(c->pToken);
156571 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
156572 unsigned char *p = (unsigned char *)c->pInput;
156574 while( c->iOffset<c->nBytes ){
156578 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
156579 c->iOffset++;
156582 /* Count non-delimiter characters. */
156583 iStartOffset = c->iOffset;
156584 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
156585 c->iOffset++;
156588 if( c->iOffset>iStartOffset ){
156589 int i, n = c->iOffset-iStartOffset;
156590 if( n>c->nTokenAllocated ){
156592 c->nTokenAllocated = n+20;
156593 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
156595 c->pToken = pNew;
156598 /* TODO(shess) This needs expansion to handle UTF-8
156599 ** case-insensitivity.
156602 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
156604 *ppToken = c->pToken;
156607 *piEndOffset = c->iOffset;
156608 *piPosition = c->iToken++;
156659 ** <tokenizer-name>, <arg-1>, ...
156739 ** The second argument, argv[], is an array of pointers to nul-terminated
156818 nDequote = argc-3;
156834 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
156846 pTab->pMod = pMod;
156847 pTab->pTok = pTok;
156848 *ppVtab = &pTab->base;
156851 pMod->xDestroy(pTok);
156867 pTab->pMod->xDestroy(pTab->pTok);
156873 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
156882 for(i=0; i<pInfo->nConstraint; i++){
156883 if( pInfo->aConstraint[i].usable
156884 && pInfo->aConstraint[i].iColumn==0
156885 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
156887 pInfo->idxNum = 1;
156888 pInfo->aConstraintUsage[i].argvIndex = 1;
156889 pInfo->aConstraintUsage[i].omit = 1;
156890 pInfo->estimatedCost = 1;
156895 pInfo->idxNum = 0;
156896 assert( pInfo->estimatedCost>1000000.0 );
156902 ** xOpen - Open a cursor.
156923 if( pCsr->pCsr ){
156924 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
156925 pTab->pMod->xClose(pCsr->pCsr);
156926 pCsr->pCsr = 0;
156928 sqlite3_free(pCsr->zInput);
156929 pCsr->zInput = 0;
156930 pCsr->zToken = 0;
156931 pCsr->nToken = 0;
156932 pCsr->iStart = 0;
156933 pCsr->iEnd = 0;
156934 pCsr->iPos = 0;
156935 pCsr->iRowid = 0;
156939 ** xClose - Close a cursor.
156950 ** xNext - Advance the cursor to the next row, if any.
156954 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
156957 pCsr->iRowid++;
156958 rc = pTab->pMod->xNext(pCsr->pCsr,
156959 &pCsr->zToken, &pCsr->nToken,
156960 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
156972 ** xFilter - Initialize a cursor to point at the start of its data.
156983 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
156991 pCsr->zInput = sqlite3_malloc(nByte+1);
156992 if( pCsr->zInput==0 ){
156995 memcpy(pCsr->zInput, zByte, nByte);
156996 pCsr->zInput[nByte] = 0;
156997 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
156999 pCsr->pCsr->pTokenizer = pTab->pTok;
157009 ** xEof - Return true if the cursor is at EOF, or false otherwise.
157013 return (pCsr->zToken==0);
157017 ** xColumn - Return a column value.
157029 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
157032 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
157035 sqlite3_result_int(pCtx, pCsr->iStart);
157038 sqlite3_result_int(pCtx, pCsr->iEnd);
157042 sqlite3_result_int(pCtx, pCsr->iPos);
157049 ** xRowid - Return the current rowid for the cursor.
157056 *pRowid = (sqlite3_int64)pCsr->iRowid;
157114 ** tables. It also contains code to merge FTS3 b-tree segments. Some
157115 ** of the sub-routines used to merge segments are also used by the query
157130 ** When full-text index nodes are loaded from disk, the buffer that they
157132 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
157142 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
157148 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
157150 ** The code is written so that the hard lower-limit for each of these values
157184 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
157221 ** a contiguous set of segment b-tree leaf nodes. Although the details of
157224 ** terms when querying the full-text index. See functions:
157239 u8 rootOnly; /* True for a root-only reader */
157268 int nOffsetList; /* For descending pending seg-readers only */
157272 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
157273 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
157276 ** An instance of this structure is used to create a segment b-tree in the
157300 ** the interior part of the segment b+-tree structures (everything except
157309 ** or the pending-terms table being flushed), leaves are written into the
157312 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
157318 SegmentNode *pRight; /* Pointer to right-sibling */
157319 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
157337 #define SQL_DELETE_ALL_DOCSIZE 5
157398 /* 5 */ "DELETE FROM %Q.'%q_docsize'",
157442 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
157485 /* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
157487 /* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
157493 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
157496 pStmt = p->aStmt[eStmt];
157500 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
157502 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
157504 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
157509 rc = sqlite3_prepare_v3(p->db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
157513 p->aStmt[eStmt] = pStmt;
157610 ** shared-cache table-lock on the %_segdir table. This is required before
157625 if( p->nPendingData==0 ){
157639 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
157646 ** level within the index) are encoded in 64-bit integer values stored
157648 ** separate component values into the single 64-bit integer value that
157651 ** Specifically, each language-id/index combination is allocated 1024
157652 ** 64-bit integer level values ("absolute levels"). The main terms index
157653 ** for language-id 0 is allocate values 0-1023. The first prefix index
157654 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
157658 ** absolute levels that corresponds to language-id iLangid and index
157664 int iIndex, /* Index in p->aIndex[] */
157669 assert( p->nIndex>0 );
157670 assert( iIndex>=0 && iIndex<p->nIndex );
157672 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
157696 int iIndex, /* Index for p->aIndex[] */
157705 assert( iIndex>=0 && iIndex<p->nIndex );
157713 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
157752 p->nSpace = 100;
157753 p->aData = (char *)&p[1];
157754 p->nData = 0;
157756 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
157757 int nNew = p->nSpace * 2;
157764 p->nSpace = nNew;
157765 p->aData = (char *)&p[1];
157769 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
157770 p->aData[p->nData] = '\0';
157776 ** Add a docid/column/position entry to a PendingList structure. Non-zero
157794 assert( !p || p->iLastDocid<=iDocid );
157796 if( !p || p->iLastDocid!=iDocid ){
157797 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
157799 assert( p->nData<p->nSpace );
157800 assert( p->aData[p->nData]==0 );
157801 p->nData++;
157806 p->iLastCol = -1;
157807 p->iLastPos = 0;
157808 p->iLastDocid = iDocid;
157810 if( iCol>0 && p->iLastCol!=iCol ){
157816 p->iLastCol = iCol;
157817 p->iLastPos = 0;
157820 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
157821 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
157823 p->iLastPos = iPos;
157844 ** Add an entry to one of the pending-terms hash tables.
157859 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
157861 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
157872 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
157878 ** Tokenize the nul-terminated string zText and add all tokens to the
157879 ** pending-terms hash-table. The docid used is that currently stored in
157880 ** p->iPrevDocid, and the column is specified by argument iCol.
157900 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
157901 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
157916 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
157921 xNext = pModule->xNext;
157928 /* Positions cannot be negative; we use -1 as a terminator internally.
157929 ** Tokens must have a non-zero length.
157938 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
157943 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
157944 struct Fts3Index *pIndex = &p->aIndex[i];
157945 if( nToken<pIndex->nPrefix ) continue;
157947 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
157952 pModule->xClose(pCsr);
157959 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
157963 Fts3Table *p, /* Full-text table handle */
157972 ** forced-flush would provide better performance. I suspect that if
157977 if( iDocid<p->iPrevDocid
157978 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
157979 || p->iPrevLangid!=iLangid
157980 || p->nPendingData>p->nMaxPendingData
157985 p->iPrevDocid = iDocid;
157986 p->iPrevLangid = iLangid;
157987 p->bPrevDelete = bDelete;
157992 ** Discard the contents of the pending-terms hash tables.
157996 for(i=0; i<p->nIndex; i++){
157998 Fts3Hash *pHash = &p->aIndex[i].hPending;
158005 p->nPendingData = 0;
158023 for(i=2; i<p->nColumn+2; i++){
158024 int iCol = i-2;
158025 if( p->abNotindexed[iCol]==0 ){
158031 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
158044 ** apVal[2] Left-most user-defined column
158046 ** apVal[p->nColumn+1] Right-most user-defined column
158047 ** apVal[p->nColumn+2] Hidden column with same name as table
158048 ** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
158049 ** apVal[p->nColumn+4] Hidden languageid column
158052 Fts3Table *p, /* Full-text table */
158059 if( p->zContentTbl ){
158060 sqlite3_value *pRowid = apVal[p->nColumn+3];
158080 if( rc==SQLITE_OK && p->zLanguageid ){
158082 pContentInsert, p->nColumn+2,
158083 sqlite3_value_int(apVal[p->nColumn+4])
158095 ** In FTS3, this is an error. It is an error to specify non-NULL values
158098 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
158105 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
158115 *piDocid = sqlite3_last_insert_rowid(p->db);
158128 /* Discard the contents of the pending-terms hash table. */
158133 assert( p->zContentTbl==0 || bContent==0 );
158137 if( p->bHasDocsize ){
158140 if( p->bHasStat ){
158151 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
158158 ** full-text index.
158179 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
158180 int iCol = i-1;
158181 if( p->abNotindexed[iCol]==0 ){
158183 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
158184 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
158225 int iIndex, /* Index for p->aIndex */
158234 assert( p->nIndex>=1 );
158280 ** paBlob is non-NULL, then it is the responsibility of the caller to
158302 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
158305 if( p->pSegments ){
158306 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
158308 if( 0==p->zSegmentsTbl ){
158309 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
158310 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
158313 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
158318 int nByte = sqlite3_blob_bytes(p->pSegments);
158329 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
158344 ** Close the blob handle at p->pSegments, if it is open. See comments above
158348 sqlite3_blob_close(p->pSegments);
158349 p->pSegments = 0;
158356 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
158358 pReader->pBlob,
158359 &pReader->aNode[pReader->nPopulate],
158361 pReader->nPopulate
158365 pReader->nPopulate += nRead;
158366 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
158367 if( pReader->nPopulate==pReader->nNode ){
158368 sqlite3_blob_close(pReader->pBlob);
158369 pReader->pBlob = 0;
158370 pReader->nPopulate = 0;
158378 assert( !pReader->pBlob
158379 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
158381 while( pReader->pBlob && rc==SQLITE_OK
158382 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
158394 sqlite3_free(pSeg->aNode);
158395 sqlite3_blob_close(pSeg->pBlob);
158396 pSeg->pBlob = 0;
158398 pSeg->aNode = 0;
158411 int rc; /* Return code of various sub-routines */
158416 if( !pReader->aDoclist ){
158417 pNext = pReader->aNode;
158419 pNext = &pReader->aDoclist[pReader->nDoclist];
158422 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
158425 Fts3HashElem *pElem = *(pReader->ppNextElem);
158426 sqlite3_free(pReader->aNode);
158427 pReader->aNode = 0;
158431 int nCopy = pList->nData+1;
158432 pReader->zTerm = (char *)fts3HashKey(pElem);
158433 pReader->nTerm = fts3HashKeysize(pElem);
158436 memcpy(aCopy, pList->aData, nCopy);
158437 pReader->nNode = pReader->nDoclist = nCopy;
158438 pReader->aNode = pReader->aDoclist = aCopy;
158439 pReader->ppNextElem++;
158440 assert( pReader->aNode );
158449 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
158450 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
158455 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
158456 (bIncr ? &pReader->nPopulate : 0)
158459 assert( pReader->pBlob==0 );
158460 if( bIncr && pReader->nPopulate<pReader->nNode ){
158461 pReader->pBlob = p->pSegments;
158462 p->pSegments = 0;
158464 pNext = pReader->aNode;
158477 || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
158482 if( nPrefix+nSuffix>pReader->nTermAlloc ){
158484 char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
158488 pReader->zTerm = zNew;
158489 pReader->nTermAlloc = nNew;
158495 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
158496 pReader->nTerm = nPrefix+nSuffix;
158498 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
158499 pReader->aDoclist = pNext;
158500 pReader->pOffsetList = 0;
158503 ** b-tree node. And that the final byte of the doclist is 0x00. If either
158506 if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
158507 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
158520 assert( pReader->aDoclist );
158521 assert( !pReader->pOffsetList );
158522 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
158524 pReader->iDocid = 0;
158525 pReader->nOffsetList = 0;
158527 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
158528 &pReader->iDocid, &pReader->nOffsetList, &bEof
158531 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
158533 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
158534 pReader->pOffsetList = &pReader->aDoclist[n];
158545 ** *ppOffsetList is set to point to the first column-offset list
158547 ** *pnOffsetList is set to the length of the set of column-offset
158548 ** lists, not including the nul-terminator byte. For example:
158553 char **ppOffsetList, /* OUT: Pointer to current position-list */
158557 char *p = pReader->pOffsetList;
158562 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
158563 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
158564 ** Pending-terms doclists are always built up in ascending order, so
158568 *ppOffsetList = pReader->pOffsetList;
158569 *pnOffsetList = pReader->nOffsetList - 1;
158572 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
158573 &pReader->nOffsetList, &bEof
158576 pReader->pOffsetList = 0;
158578 pReader->pOffsetList = p;
158581 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
158592 ** the populated part of pReader->aNode[].
158597 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
158604 ** size of the previous offset-list.
158607 *ppOffsetList = pReader->pOffsetList;
158608 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
158620 pReader->pOffsetList = 0;
158625 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
158626 if( pTab->bDescIdx ){
158627 pReader->iDocid -= iDelta;
158629 pReader->iDocid += iDelta;
158644 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
158648 int pgsz = p->nPgsz;
158650 assert( p->bFts4 );
158653 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
158654 Fts3SegReader *pReader = pMsr->apSegment[ii];
158659 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
158680 sqlite3_free(pReader->zTerm);
158683 sqlite3_free(pReader->aNode);
158685 sqlite3_blob_close(pReader->pBlob);
158716 pReader->iIdx = iAge;
158717 pReader->bLookup = bLookup!=0;
158718 pReader->iStartBlock = iStartLeaf;
158719 pReader->iLeafEndBlock = iEndLeaf;
158720 pReader->iEndBlock = iEndBlock;
158724 pReader->aNode = (char *)&pReader[1];
158725 pReader->rootOnly = 1;
158726 pReader->nNode = nRoot;
158727 memcpy(pReader->aNode, zRoot, nRoot);
158728 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
158730 pReader->iCurrentBlock = iStartLeaf-1;
158739 ** the contents of the pending-terms hash table to the database.
158753 c = n1 - n2;
158763 ** through each term in the pending-terms table. Or, if isPrefixIter is
158764 ** non-zero, it iterates through each term and its prefixes. For example, if
158779 int iIndex, /* Index for p->aIndex */
158783 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
158792 pHash = &p->aIndex[iIndex].hPending;
158828 ** the index. All that is required is a straight hash-lookup.
158848 pReader->iIdx = 0x7FFFFFFF;
158849 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
158850 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
158875 if( pLhs->aNode && pRhs->aNode ){
158876 int rc2 = pLhs->nTerm - pRhs->nTerm;
158878 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
158880 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
158886 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
158889 rc = pRhs->iIdx - pLhs->iIdx;
158907 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
158909 if( pLhs->iDocid==pRhs->iDocid ){
158910 rc = pRhs->iIdx - pLhs->iIdx;
158912 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
158915 assert( pLhs->aNode && pRhs->aNode );
158919 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
158921 if( pLhs->iDocid==pRhs->iDocid ){
158922 rc = pRhs->iIdx - pLhs->iIdx;
158924 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
158927 assert( pLhs->aNode && pRhs->aNode );
158936 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
158945 if( pSeg->aNode ){
158946 if( pSeg->nTerm>nTerm ){
158947 res = memcmp(pSeg->zTerm, zTerm, nTerm);
158949 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
158952 res = pSeg->nTerm-nTerm;
158960 ** the final (nSegment-nSuspect) members are already in sorted order
158974 if( nSuspect==nSegment ) nSuspect--;
158975 for(i=nSuspect-1; i>=0; i--){
158977 for(j=i; j<(nSegment-1); j++){
158988 for(i=0; i<(nSuspect-1); i++){
159057 sqlite3_bind_int64(pStmt, 5, iEndBlock);
159061 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
159109 int nData = pTree->nData; /* Current size of node in bytes */
159114 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
159115 nSuffix = nTerm-nPrefix;
159118 if( nReq<=p->nNodeSize || !pTree->zTerm ){
159120 if( nReq>p->nNodeSize ){
159122 ** and the static node buffer (p->nNodeSize bytes) is not large
159124 ** p->nNodeSize bytes, but since this scenario only comes about when
159128 assert( pTree->aData==(char *)&pTree[1] );
159129 pTree->aData = (char *)sqlite3_malloc(nReq);
159130 if( !pTree->aData ){
159135 if( pTree->zTerm ){
159136 /* There is no prefix-length field for first term in a node */
159137 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
159140 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
159141 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
159142 pTree->nData = nData + nSuffix;
159143 pTree->nEntry++;
159146 if( pTree->nMalloc<nTerm ){
159147 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
159151 pTree->nMalloc = nTerm*2;
159152 pTree->zMalloc = zNew;
159154 pTree->zTerm = pTree->zMalloc;
159155 memcpy(pTree->zTerm, zTerm, nTerm);
159156 pTree->nTerm = nTerm;
159158 pTree->zTerm = (char *)zTerm;
159159 pTree->nTerm = nTerm;
159166 ** current node. Create a new node (a right-sibling of the current node).
159173 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
159178 pNew->nData = 1 + FTS3_VARINT_MAX;
159179 pNew->aData = (char *)&pNew[1];
159182 SegmentNode *pParent = pTree->pParent;
159184 if( pTree->pParent==0 ){
159185 pTree->pParent = pParent;
159187 pTree->pRight = pNew;
159188 pNew->pLeftmost = pTree->pLeftmost;
159189 pNew->pParent = pParent;
159190 pNew->zMalloc = pTree->zMalloc;
159191 pNew->nMalloc = pTree->nMalloc;
159192 pTree->zMalloc = 0;
159194 pNew->pLeftmost = pNew;
159212 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
159213 pTree->aData[nStart] = (char)iHeight;
159214 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
159243 if( !pTree->pParent ){
159246 *piLast = iFree-1;
159247 *pnRoot = pTree->nData - nStart;
159248 *paRoot = &pTree->aData[nStart];
159253 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
159255 int nWrite = pIter->nData - nStart;
159257 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
159259 iNextLeaf += (pIter->nEntry+1);
159264 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
159277 SegmentNode *p = pTree->pLeftmost;
159278 fts3NodeFree(p->pParent);
159280 SegmentNode *pRight = p->pRight;
159281 if( p->aData!=(char *)&p[1] ){
159282 sqlite3_free(p->aData);
159284 assert( pRight==0 || p->zMalloc==0 );
159285 sqlite3_free(p->zMalloc);
159326 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
159327 if( !pWriter->aData ) return SQLITE_NOMEM;
159328 pWriter->nSize = p->nNodeSize;
159334 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
159335 pWriter->iFirst = pWriter->iFree;
159340 nData = pWriter->nData;
159342 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
159343 nSuffix = nTerm-nPrefix;
159352 if( nData>0 && nData+nReq>p->nNodeSize ){
159356 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
159358 p->nLeafAdd++;
159364 ** to the database (still available in pWriter->zTerm), and
159370 ** the common prefix (if any) of zTerm and pWriter->zTerm.
159373 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
159377 pWriter->nTerm = 0;
159389 pWriter->nLeafData += nReq;
159394 if( nReq>pWriter->nSize ){
159395 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
159397 pWriter->aData = aNew;
159398 pWriter->nSize = nReq;
159400 assert( nData+nReq<=pWriter->nSize );
159402 /* Append the prefix-compressed term and doclist to the buffer. */
159403 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
159404 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
159405 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
159407 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
159408 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
159409 pWriter->nData = nData + nDoclist;
159411 /* Save the current term so that it can be used to prefix-compress the next.
159417 if( nTerm>pWriter->nMalloc ){
159418 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
159422 pWriter->nMalloc = nTerm*2;
159423 pWriter->zMalloc = zNew;
159424 pWriter->zTerm = zNew;
159426 assert( pWriter->zTerm==pWriter->zMalloc );
159427 memcpy(pWriter->zTerm, zTerm, nTerm);
159429 pWriter->zTerm = (char *)zTerm;
159431 pWriter->nTerm = nTerm;
159449 if( pWriter->pTree ){
159455 iLastLeaf = pWriter->iFree;
159456 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
159458 rc = fts3NodeWrite(p, pWriter->pTree, 1,
159459 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
159463 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
159468 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
159470 p->nLeafAdd++;
159480 sqlite3_free(pWriter->aData);
159481 sqlite3_free(pWriter->zMalloc);
159482 fts3NodeFree(pWriter->pTree);
159500 if( p->zContentTbl ){
159532 assert( iIndex>=0 && iIndex<p->nIndex );
159544 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
159585 ** opened with seg-reader pSeg. This function does not affect the contents
159593 if( pSeg->iStartBlock ){
159597 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
159598 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
159608 ** segment to delete the old, now redundant, segment b-trees. Specifically,
159623 int iIndex, /* Index for p->aIndex */
159645 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
159674 ** If parameter bZero is non-zero, then any part of the input list following
159695 nList = (int)(p - pList);
159699 nList -= (int)(p - pList);
159709 memset(&pList[nList], 0, pEnd - &pList[nList]);
159723 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
159727 if( nList>pMsr->nBuffer ){
159729 pMsr->nBuffer = nList*2;
159730 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
159732 pMsr->aBuffer = pNew;
159735 memcpy(pMsr->aBuffer, pList, nList);
159741 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
159746 int nMerge = pMsr->nAdvance;
159747 Fts3SegReader **apSegment = pMsr->apSegment;
159749 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
159759 pSeg = pMsr->apSegment[0];
159761 if( pSeg->pOffsetList==0 ){
159769 sqlite3_int64 iDocid = apSegment[0]->iDocid;
159775 && apSegment[j]->pOffsetList
159776 && apSegment[j]->iDocid==iDocid
159782 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
159787 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
159788 pList = pMsr->aBuffer;
159791 if( pMsr->iColFilter>=0 ){
159792 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
159814 int nSeg = pCsr->nSegment;
159820 ** b-tree leaf nodes contain more than one term.
159822 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
159824 Fts3SegReader *pSeg = pCsr->apSegment[i];
159830 if( pSeg->bLookup && res!=0 ){
159834 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
159844 pCsr->pFilter = pFilter;
159845 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
159857 int nSegment = pCsr->nSegment;
159859 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
159862 assert( pCsr->pFilter==0 );
159871 Fts3SegReader *pSeg = pCsr->apSegment[i];
159872 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
159876 pCsr->nAdvance = i;
159879 for(i=0; i<pCsr->nAdvance; i++){
159880 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
159883 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
159885 assert( iCol<0 || iCol<p->nColumn );
159886 pCsr->iColFilter = iCol;
159904 int i; /* Used to iterate through segment-readers */
159906 assert( pCsr->zTerm==0 );
159907 assert( pCsr->nTerm==0 );
159908 assert( pCsr->aDoclist==0 );
159909 assert( pCsr->nDoclist==0 );
159911 pCsr->nAdvance = 0;
159912 pCsr->bRestart = 1;
159913 for(i=0; i<pCsr->nSegment; i++){
159914 pCsr->apSegment[i]->pOffsetList = 0;
159915 pCsr->apSegment[i]->nOffsetList = 0;
159916 pCsr->apSegment[i]->iDocid = 0;
159929 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
159930 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
159931 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
159932 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
159933 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
159934 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
159936 Fts3SegReader **apSegment = pCsr->apSegment;
159937 int nSegment = pCsr->nSegment;
159938 Fts3SegFilter *pFilter = pCsr->pFilter;
159940 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
159943 if( pCsr->nSegment==0 ) return SQLITE_OK;
159949 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
159952 for(i=0; i<pCsr->nAdvance; i++){
159954 if( pSeg->bLookup ){
159961 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
159962 pCsr->nAdvance = 0;
159964 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
159966 if( apSegment[0]->aNode==0 ) break;
159968 pCsr->nTerm = apSegment[0]->nTerm;
159969 pCsr->zTerm = apSegment[0]->zTerm;
159971 /* If this is a prefix-search, and if the term that apSegment[0] points
159972 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
159978 if( pFilter->zTerm && !isScan ){
159979 if( pCsr->nTerm<pFilter->nTerm
159980 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
159981 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
159989 && apSegment[nMerge]->aNode
159990 && apSegment[nMerge]->nTerm==pCsr->nTerm
159991 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
160000 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
160002 pCsr->nDoclist = apSegment[0]->nDoclist;
160004 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
160005 pCsr->aDoclist = pCsr->aBuffer;
160007 pCsr->aDoclist = apSegment[0]->aDoclist;
160022 while( apSegment[0]->pOffsetList ){
160027 sqlite3_int64 iDocid = apSegment[0]->iDocid;
160031 && apSegment[j]->pOffsetList
160032 && apSegment[j]->iDocid==iDocid
160039 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
160047 if( p->bDescIdx && nDoclist>0 ){
160048 iDelta = iPrev - iDocid;
160050 iDelta = iDocid - iPrev;
160056 if( nDoclist+nByte>pCsr->nBuffer ){
160058 pCsr->nBuffer = (nDoclist+nByte)*2;
160059 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
160063 pCsr->aBuffer = aNew;
160067 char *a = &pCsr->aBuffer[nDoclist];
160076 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
160079 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
160081 pCsr->aBuffer[nDoclist++] = '\0';
160089 pCsr->aDoclist = pCsr->aBuffer;
160090 pCsr->nDoclist = nDoclist;
160094 pCsr->nAdvance = nMerge;
160106 for(i=0; i<pCsr->nSegment; i++){
160107 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
160109 sqlite3_free(pCsr->apSegment);
160110 sqlite3_free(pCsr->aBuffer);
160112 pCsr->nSegment = 0;
160113 pCsr->apSegment = 0;
160114 pCsr->aBuffer = 0;
160123 ** containing the text representation of two non-negative integers separated
160140 iVal = iVal*10 + (zText[i] - '0');
160145 if( zText[i]=='-' ){
160147 iMul = -1;
160149 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
160150 iVal = iVal*10 + (zText[i] - '0');
160173 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
160215 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
160218 ** In other words, move all segments being promoted to level -1,
160220 ** order. The contents of level -1 (which is never used, except
160239 /* Move level -1 to level iAbsLevel */
160266 int iIndex, /* Index in p->aIndex[] to merge */
160283 assert( iIndex>=0 && iIndex<p->nIndex );
160310 assert( FTS3_SEGCURSOR_PENDING==-1 );
160345 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
160364 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
160365 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
160370 /* Determine the auto-incr-merge setting if unknown. If enabled,
160373 if( rc==SQLITE_OK && p->bHasStat
160374 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
160382 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
160383 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
160385 p->nAutoincrmerge = 0;
160430 ** with docid equal to p->iPrevDocid. The sizes are encoded as
160444 pBlob = sqlite3_malloc( 10*p->nColumn );
160449 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
160456 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
160491 const int nStat = p->nColumn+2;
160520 if( nChng<0 && a[0]<(u32)(-nChng) ){
160525 for(i=0; i<p->nColumn+1; i++){
160530 x = x + aSzIns[i] - aSzDel[i];
160560 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
160561 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
160565 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
160605 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
160609 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
160614 int nByte = sizeof(u32) * (p->nColumn+1)*3;
160620 aSzIns = &aSz[p->nColumn+1];
160621 aSzDel = &aSzIns[p->nColumn+1];
160629 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
160630 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
160631 if( p->abNotindexed[iCol]==0 ){
160634 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
160637 if( p->bHasDocsize ){
160645 for(iCol=0; iCol<=p->nColumn; iCol++){
160650 if( p->bFts4 ){
160670 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
160681 int nByte; /* Bytes allocated at pCsr->apSegment[] */
160686 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
160688 if( pCsr->apSegment==0 ){
160691 memset(pCsr->apSegment, 0, nByte);
160698 assert( pCsr->nSegment==0 );
160706 &pCsr->apSegment[i]
160708 pCsr->nSegment++;
160735 ** This structure is used to build up buffers containing segment b-tree
160746 ** to an appendable b-tree segment.
160781 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
160782 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
160785 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
160786 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
160787 ** to reflect the new size of the pBlob->a[] buffer.
160790 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
160792 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
160794 pBlob->nAlloc = nAlloc;
160795 pBlob->a = a;
160803 ** Attempt to advance the node-reader object passed as the first argument to
160808 ** (e.g. because the current entry is the last) set NodeReader->aNode to
160813 int bFirst = (p->term.n==0); /* True for first term on the node */
160818 assert( p->aNode );
160819 if( p->iChild && bFirst==0 ) p->iChild++;
160820 if( p->iOff>=p->nNode ){
160822 p->aNode = 0;
160825 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
160827 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
160829 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
160831 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
160832 p->term.n = nPrefix+nSuffix;
160833 p->iOff += nSuffix;
160834 if( p->iChild==0 ){
160835 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
160836 p->aDoclist = &p->aNode[p->iOff];
160837 p->iOff += p->nDoclist;
160842 assert( p->iOff<=p->nNode );
160848 ** Release all dynamic resources held by node-reader object *p.
160851 sqlite3_free(p->term.a);
160855 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
160863 p->aNode = aNode;
160864 p->nNode = nNode;
160867 if( p->aNode[0] ){
160869 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
160871 p->iOff = 1;
160885 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
160893 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
160899 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
160909 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
160910 nSuffix = nTerm - nPrefix;
160914 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
160919 Blob *pBlk = &pNode->block;
160920 if( pBlk->n==0 ){
160921 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
160923 pBlk->a[0] = (char)iLayer;
160924 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
160927 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
160928 blobGrowBuffer(&pNode->key, nTerm, &rc);
160931 if( pNode->key.n ){
160932 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
160934 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
160935 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
160936 pBlk->n += nSuffix;
160938 memcpy(pNode->key.a, zTerm, nTerm);
160939 pNode->key.n = nTerm;
160945 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
160947 assert( pNode->block.nAlloc>=p->nNodeSize );
160948 pNode->block.a[0] = (char)iLayer;
160949 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
160951 iNextPtr = pNode->iBlock;
160952 pNode->iBlock++;
160953 pNode->key.n = 0;
160970 ** followed by the left-hand-child varint for an internal node.
160997 int bFirst = (pPrev->n==0); /* True if this is the first term written */
161003 assert( pNode->n>0 );
161004 assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
161009 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
161010 nSuffix = nTerm - nPrefix;
161011 memcpy(pPrev->a, zTerm, nTerm);
161012 pPrev->n = nTerm;
161015 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
161017 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
161018 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
161019 pNode->n += nSuffix;
161022 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
161023 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
161024 pNode->n += nDoclist;
161027 assert( pNode->n<=pNode->nAlloc );
161034 ** appendable b-tree segment opened for writing by pWriter.
161043 const char *zTerm = pCsr->zTerm;
161044 int nTerm = pCsr->nTerm;
161045 const char *aDoclist = pCsr->aDoclist;
161046 int nDoclist = pCsr->nDoclist;
161050 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
161053 pLeaf = &pWriter->aNodeWriter[0];
161054 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
161055 nSuffix = nTerm - nPrefix;
161064 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
161065 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
161066 pWriter->nWork++;
161072 ** to the database (still available in pLeaf->key), and
161078 ** the common prefix (if any) of zTerm and pWriter->zTerm.
161085 pLeaf->iBlock++;
161086 pLeaf->key.n = 0;
161087 pLeaf->block.n = 0;
161095 pWriter->nLeafData += nSpace;
161096 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
161098 if( pLeaf->block.n==0 ){
161099 pLeaf->block.n = 1;
161100 pLeaf->block.a[0] = '\0';
161103 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
161112 ** merge-writer object pWriter, and if no error has occurred, to flush
161125 IncrmergeWriter *pWriter, /* Merge-writer object */
161128 int i; /* Used to iterate through non-root layers */
161129 int iRoot; /* Index of root in pWriter->aNodeWriter */
161133 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
161137 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
161138 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
161139 if( pNode->block.n>0 ) break;
161140 assert( *pRc || pNode->block.nAlloc==0 );
161141 assert( *pRc || pNode->key.nAlloc==0 );
161142 sqlite3_free(pNode->block.a);
161143 sqlite3_free(pNode->key.a);
161146 /* Empty output segment. This is a no-op. */
161165 Blob *pBlock = &pWriter->aNodeWriter[1].block;
161168 pBlock->a[0] = 0x01;
161169 pBlock->n = 1 + sqlite3Fts3PutVarint(
161170 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
161175 pRoot = &pWriter->aNodeWriter[iRoot];
161179 NodeWriter *pNode = &pWriter->aNodeWriter[i];
161180 if( pNode->block.n>0 && rc==SQLITE_OK ){
161181 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
161183 sqlite3_free(pNode->block.a);
161184 sqlite3_free(pNode->key.a);
161190 pWriter->iAbsLevel+1, /* level */
161191 pWriter->iIdx, /* idx */
161192 pWriter->iStart, /* start_block */
161193 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
161194 pWriter->iEnd, /* end_block */
161195 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
161196 pRoot->block.a, pRoot->block.n /* root */
161199 sqlite3_free(pRoot->block.a);
161200 sqlite3_free(pRoot->key.a);
161210 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
161221 if( res==0 ) res = nLhs - nRhs;
161256 ** This function is called when initializing an incremental-merge operation.
161259 ** merge-writer object *pWriter is initialized to write to it.
161264 ** space pre-allocated), and
161297 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
161298 if( pWriter->nLeafData<0 ){
161299 pWriter->nLeafData = pWriter->nLeafData * -1;
161301 pWriter->bNoLeafData = (pWriter->nLeafData==0);
161308 /* Check for the zero-length marker in the %_segments table */
161340 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
161341 pWriter->iStart = iStart;
161342 pWriter->iEnd = iEnd;
161343 pWriter->iAbsLevel = iAbsLevel;
161344 pWriter->iIdx = iIdx;
161347 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
161350 pNode = &pWriter->aNodeWriter[nHeight];
161351 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
161352 blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
161354 memcpy(pNode->block.a, aRoot, nRoot);
161355 pNode->block.n = nRoot;
161358 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
161360 pNode = &pWriter->aNodeWriter[i];
161362 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
161364 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
161366 memcpy(pNode->key.a, reader.term.a, reader.term.n);
161367 pNode->key.n = reader.term.n;
161371 pNode = &pWriter->aNodeWriter[i-1];
161372 pNode->iBlock = reader.iChild;
161374 blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
161376 memcpy(pNode->block.a, aBlock, nBlock);
161377 pNode->block.n = nBlock;
161439 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
161442 ** (start_block + (1 + end_block - start_block) / 16). And so on.
161445 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
161464 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
161476 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
161477 pWriter->iEnd = pWriter->iStart - 1;
161478 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
161487 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
161490 pWriter->iAbsLevel = iAbsLevel;
161491 pWriter->nLeafEst = nLeafEst;
161492 pWriter->iIdx = iIdx;
161496 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
161506 ** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
161576 assert( p->bIgnoreSavepoint==0 );
161577 p->bIgnoreSavepoint = 1;
161586 p->bIgnoreSavepoint = 0;
161593 pNode->a[0] = (char)iHeight;
161595 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
161596 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
161598 assert( pNode->nAlloc>=1 );
161599 pNode->n = 1;
161604 ** The first two arguments are a pointer to and the size of a segment b-tree
161627 pNew->n = 0;
161634 if( pNew->n==0 ){
161646 if( pNew->n==0 ){
161650 assert( pNew->n<=pNew->nAlloc );
161717 sqlite3_bind_int64(pDel, 2, iNewStart-1);
161742 ** This function is called after an incrmental-merge operation has run to
161761 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
161766 ** somewhere in the pCsr->apSegment[] array. */
161767 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
161768 pSeg = pCsr->apSegment[j];
161769 if( pSeg->iIdx==i ) break;
161771 assert( j<pCsr->nSegment && pSeg->iIdx==i );
161773 if( pSeg->aNode==0 ){
161774 /* Seg-reader is at EOF. Remove the entire input segment. */
161777 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
161784 const char *zTerm = pSeg->zTerm;
161785 int nTerm = pSeg->nTerm;
161786 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
161791 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
161800 ** Store an incr-merge hint in the database.
161809 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
161818 ** Load an incr-merge hint from the database. The incr-merge hint, if one
161829 pHint->n = 0;
161840 memcpy(pHint->a, aHint, nHint);
161841 pHint->n = nHint;
161853 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
161867 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
161869 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
161870 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
161883 const int nHint = pHint->n;
161886 i = pHint->n-2;
161887 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
161888 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
161890 pHint->n = i;
161891 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
161892 i += fts3GetVarint32(&pHint->a[i], pnInput);
161928 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
161937 ** set nSeg to -1.
161946 nSeg = -1;
161967 /* This undoes the effect of the HintPop() above - so that no entry
161982 ** done on iAbsLevel - fall through to the next iteration of the loop
161985 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
161994 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
162002 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
162007 const char *zKey = pCsr->zTerm;
162008 int nKey = pCsr->nTerm;
162009 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
162014 if( rc==SQLITE_OK && pWriter->nLeafEst ){
162019 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
162024 nRem -= (1 + pWriter->nWork);
162034 pWriter->nLeafData = pWriter->nLeafData * -1;
162037 if( nSeg==0 && pWriter->bNoLeafData==0 ){
162038 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
162045 /* Write the hint values into the %_stat table for the next incr-merger */
162066 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
162082 const char *zParam /* Nul-terminated string containing "A,B" */
162103 if( !p->bHasStat ){
162104 assert( p->bFts4==0 );
162125 const char *zParam /* Nul-terminated string containing boolean */
162129 p->nAutoincrmerge = fts3Getint(&zParam);
162130 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
162131 p->nAutoincrmerge = 8;
162133 if( !p->bHasStat ){
162134 assert( p->bFts4==0 );
162141 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
162148 ** Return a 64-bit checksum for the FTS index entry specified by the
162155 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
162184 int iIndex, /* Index to cksum (0..p->nIndex-1) */
162230 iPos += (iVal - 2);
162259 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
162265 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
162266 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
162270 for(i=0; i<p->nIndex; i++){
162280 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
162284 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
162288 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
162297 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
162298 if( p->abNotindexed[iCol]==0 ){
162303 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
162310 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
162316 for(i=1; i<p->nIndex; i++){
162317 if( p->aIndex[i].nPrefix<=nToken ){
162319 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
162325 if( pT ) pModule->xClose(pT);
162339 ** Run the integrity-check. If no error occurs and the current contents of
162346 ** The integrity-check works as follows. For each token and indexed token
162347 ** prefix in the document set, a 64-bit checksum is calculated (by code
162353 ** + The language-id of the row it appears in,
162361 ** The integrity-check code calculates the same checksum in two ways:
162366 ** If the two checksums are identical, the integrity-check is deemed to have
162398 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
162406 p->nNodeSize = atoi(&zVal[9]);
162409 p->nMaxPendingData = atoi(&zVal[11]);
162411 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
162412 p->bNoIncrDoclist = atoi(&zVal[21]);
162429 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
162430 fts3PendingListDelete(pDef->pList);
162431 pDef->pList = 0;
162436 ** Free all entries in the pCsr->pDeffered list. Entries are added to
162442 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
162443 pNext = pDef->pNext;
162444 fts3PendingListDelete(pDef->pList);
162447 pCsr->pDeferred = 0;
162451 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
162454 ** A deferred-doclist is like any other doclist with position information
162460 if( pCsr->pDeferred ){
162465 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
162466 sqlite3_tokenizer *pT = p->pTokenizer;
162467 sqlite3_tokenizer_module const *pModule = pT->pModule;
162469 assert( pCsr->isRequireSeek==0 );
162470 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
162472 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
162473 if( p->abNotindexed[i]==0 ){
162474 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
162477 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
162484 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
162485 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
162486 Fts3PhraseToken *pPT = pDef->pToken;
162487 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
162488 && (pPT->bFirst==0 || iPos==0)
162489 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
162490 && (0==memcmp(zToken, pPT->z, pPT->n))
162492 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
162496 if( pTC ) pModule->xClose(pTC);
162501 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
162502 if( pDef->pList ){
162503 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
162523 if( p->pList==0 ){
162527 pRet = (char *)sqlite3_malloc(p->pList->nData);
162530 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
162531 *pnData = p->pList->nData - nSkip;
162534 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
162539 ** Add an entry for token pToken to the pCsr->pDeferred list.
162544 int iCol /* Column that token must appear in (or -1) */
162552 pDeferred->pToken = pToken;
162553 pDeferred->pNext = pCsr->pDeferred;
162554 pDeferred->iCol = iCol;
162555 pCsr->pDeferred = pDeferred;
162557 assert( pToken->pDeferred==0 );
162558 pToken->pDeferred = pDeferred;
162589 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
162591 *pnChng = *pnChng - 1;
162592 if( p->zContentTbl==0 ){
162595 if( p->bHasDocsize ){
162634 assert( p->bHasStat==0 || p->bHasStat==1 );
162636 assert( p->pSegments==0 );
162639 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
162648 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
162650 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
162654 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
162660 aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
162665 aSzIns = &aSzDel[p->nColumn+1];
162666 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
162674 ** If the on-conflict mode is REPLACE, this means that the existing row
162676 ** if the on-conflict mode is other than REPLACE, then this method must
162680 if( nArg>1 && p->zContentTbl==0 ){
162682 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
162708 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
162729 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
162732 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
162736 if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
162740 assert( p->iPrevDocid==*pRowid );
162743 if( p->bHasDocsize ){
162749 if( p->bFts4 ){
162760 ** Flush any data in the pending-terms hash table to disk. If successful,
162766 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
162770 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
162773 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
162774 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
162878 u32 *aMatchinfo; /* Pre-allocated buffer */
162903 int n; /* Length of z in bytes (excl. nul-term) */
162913 ** Allocate a two-slot MatchinfoBuffer object.
162923 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
162924 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
162925 pRet->nElem = nElem;
162926 pRet->zMatchinfo = ((char*)pRet) + nByte;
162927 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
162928 pRet->aRef[0] = 1;
162935 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
162937 assert( (u32*)p==&pBuf->aMatchinfo[1]
162938 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
162940 if( (u32*)p==&pBuf->aMatchinfo[1] ){
162941 pBuf->aRef[1] = 0;
162943 pBuf->aRef[2] = 0;
162946 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
162955 if( p->aRef[1]==0 ){
162956 p->aRef[1] = 1;
162957 aOut = &p->aMatchinfo[1];
162960 else if( p->aRef[2]==0 ){
162961 p->aRef[2] = 1;
162962 aOut = &p->aMatchinfo[p->nElem+2];
162965 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
162968 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
162977 p->bGlobal = 1;
162978 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
162986 assert( p->aRef[0]==1 );
162987 p->aRef[0] = 0;
162988 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
163000 ** This function is used to help iterate through a position-list. A position
163004 ** two. For example, to store the position-list:
163022 *piPos += (iVal-2);
163035 int eType = pExpr->eType; /* Type of expression node pExpr */
163038 assert( pExpr->pLeft && pExpr->pRight );
163039 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
163041 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
163052 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
163077 Fts3Phrase *pPhrase = pExpr->pPhrase;
163082 p->nPhrase++;
163083 p->nToken += pPhrase->nToken;
163094 ** indirectly descended from the right-hand-side of a NOT operator). If
163106 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
163114 pExpr->iPhrase = iPhrase;
163135 iIter = -1;
163153 if( pIter->iCurrent<0 ){
163158 pIter->iCurrent = 0;
163163 for(i=0; i<pIter->nPhrase; i++){
163164 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
163165 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
163171 for(i=0; i<pIter->nPhrase; i++){
163172 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
163173 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
163174 iEnd = pPhrase->iHead;
163181 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
163182 for(i=0; i<pIter->nPhrase; i++){
163183 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
163184 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
163185 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
163204 int iStart = pIter->iCurrent; /* First token of snippet */
163210 for(i=0; i<pIter->nPhrase; i++){
163211 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
163212 if( pPhrase->pTail ){
163213 char *pCsr = pPhrase->pTail;
163214 int iCsr = pPhrase->iTail;
163216 while( iCsr<(iStart+pIter->nSnippet) ){
163219 u64 mPos = (u64)1 << (iCsr - iStart);
163228 for(j=0; j<pPhrase->nToken; j++){
163251 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
163255 pPhrase->nToken = pExpr->pPhrase->nToken;
163256 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
163260 pPhrase->pList = pCsr;
163263 pPhrase->pHead = pCsr;
163264 pPhrase->pTail = pCsr;
163265 pPhrase->iHead = iFirst;
163266 pPhrase->iTail = iFirst;
163269 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
163304 int iBestScore = -1; /* Best snippet score found so far */
163334 sIter.iCurrent = -1;
163335 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
163348 pFragment->iCol = iCol;
163357 pFragment->iPos = iPos;
163358 pFragment->hlmask = mHighlite;
163359 pFragment->covered = mCover;
163372 ** Append a string to the string-buffer passed as the first argument.
163380 int nAppend /* Size of zAppend in bytes (or -1) */
163390 if( pStr->n+nAppend+1>=pStr->nAlloc ){
163391 int nAlloc = pStr->nAlloc+nAppend+100;
163392 char *zNew = sqlite3_realloc(pStr->z, nAlloc);
163396 pStr->z = zNew;
163397 pStr->nAlloc = nAlloc;
163399 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
163402 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
163403 pStr->n += nAppend;
163404 pStr->z[pStr->n] = '\0';
163413 ** and '.' is a non-highlighted term, BestSnippet() may select:
163419 ** non-highlighted terms to the right of the final highlighted term as there
163438 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
163446 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
163447 nDesired = (nLeft-nRight)/2;
163462 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
163467 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
163473 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
163475 pMod->xClose(pC);
163478 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
163504 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163511 int iPos = pFragment->iPos; /* First token of snippet */
163512 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
163513 int iCol = pFragment->iCol+1; /* Query column to extract text from */
163517 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
163519 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
163524 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
163527 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
163528 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
163535 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
163551 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
163554 /* Special case - the last token of the snippet is also the last token
163558 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
163565 int n = nDoc - iBegin;
163567 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
163577 rc = fts3StringAppend(pOut, zEllipsis, -1);
163587 rc = fts3StringAppend(pOut, zEllipsis, -1);
163593 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
163595 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
163596 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
163597 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
163598 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
163603 pMod->xClose(pC);
163609 ** This function is used to count the entries in a column-list (a
163610 ** delta-encoded list of term offsets within a single column of a single
163612 ** beginning of the first varint in the column-list (the varint that
163615 ** the last varint in the column-list (either the 0x00 signifying the end
163616 ** of the position-list, or the 0x01 that precedes the column number of
163617 ** the next column in the position-list).
163619 ** The number of elements in the column-list is returned.
163626 /* A column-list is terminated by either a 0x01 or 0x00. */
163643 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
163645 Fts3Phrase *pPhrase = pExpr->pPhrase;
163646 char *pIter = pPhrase->doclist.pList;
163649 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
163650 if( p->flag==FTS3_MATCHINFO_LHITS ){
163651 iStart = pExpr->iPhrase * p->nCol;
163653 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
163658 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
163659 if( p->flag==FTS3_MATCHINFO_LHITS ){
163660 p->aMatchinfo[iStart + iCol] = (u32)nHit;
163662 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
163679 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
163680 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
163681 if( pExpr->pLeft ){
163682 fts3ExprLHitGather(pExpr->pLeft, p);
163683 fts3ExprLHitGather(pExpr->pRight, p);
163713 ** file system. This is done because the full-text index doclist is required
163714 ** to calculate these values properly, and the full-text index doclist is
163724 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
163740 int iStart = iPhrase * p->nCol * 3;
163743 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
163745 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
163747 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
163749 p->aMatchinfo[iStart+i*3] = 0;
163763 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
163764 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
163765 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
163790 nVal = pInfo->nCol;
163794 nVal = pInfo->nCol * pInfo->nPhrase;
163798 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
163803 nVal = pInfo->nCol * pInfo->nPhrase * 3;
163838 ** iterating through a multi-column position-list corresponding to the
163872 char *pRead = pIter->pRead;
163881 pIter->iPos += (int)(iRead-2);
163884 pIter->pRead = pRead;
163891 ** If the call is successful, the longest-common-substring lengths for each
163892 ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
163896 ** data written to the first nCol elements of pInfo->aMatchinfo[] is
163908 aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
163910 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
163911 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
163913 for(i=0; i<pInfo->nPhrase; i++){
163915 nToken -= pIter->pExpr->pPhrase->nToken;
163916 pIter->iPosOffset = nToken;
163919 for(iCol=0; iCol<pInfo->nCol; iCol++){
163923 for(i=0; i<pInfo->nPhrase; i++){
163926 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
163928 if( pIt->pRead ){
163929 pIt->iPos = pIt->iPosOffset;
163939 for(i=0; i<pInfo->nPhrase; i++){
163941 if( pIter->pRead==0 ){
163945 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
163948 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
163956 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
163959 pInfo->aMatchinfo[iCol] = nLcs;
163967 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
163971 ** string has already been validated and the pInfo->aMatchinfo[] array
163981 ** pInfo->aMatchinfo[] buffer is left in is undefined.
163991 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163995 pInfo->flag = zArg[i];
163998 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
164002 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
164009 pInfo->aMatchinfo[0] = (u32)nDoc;
164021 for(iCol=0; iCol<pInfo->nCol; iCol++){
164026 pInfo->aMatchinfo[iCol] = iVal;
164034 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
164038 for(iCol=0; iCol<pInfo->nCol; iCol++){
164041 pInfo->aMatchinfo[iCol] = (u32)nToken;
164058 memset(pInfo->aMatchinfo, 0, nZero);
164059 fts3ExprLHitGather(pCsr->pExpr, pInfo);
164066 pExpr = pCsr->pExpr;
164070 if( pCsr->pDeferred ){
164071 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
164083 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
164092 ** Populate pCsr->aMatchinfo[] with data for the current row. The
164093 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
164101 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164110 sInfo.nCol = pTab->nColumn;
164115 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
164116 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
164117 pCsr->pMIBuffer = 0;
164125 if( pCsr->pMIBuffer==0 ){
164126 int nMatchinfo = 0; /* Number of u32 elements in match-info */
164130 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
164131 sInfo.nPhrase = pCsr->nPhrase;
164137 sqlite3_result_error(pCtx, zErr, -1);
164145 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
164146 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
164148 pCsr->isMatchinfoNeeded = 1;
164153 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
164161 sInfo.nPhrase = pCsr->nPhrase;
164164 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
164172 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
164183 const char *zStart, /* Snippet start text - "<b>" */
164184 const char *zEnd, /* Snippet end text - "</b>" */
164185 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
164189 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164204 int nFToken = -1; /* Number of tokens in each fragment */
164206 if( !pCsr->pExpr ){
164213 int iSnip; /* Loop counter 0..nSnippet-1 */
164218 nFToken = (nToken+nSnippet-1) / nSnippet;
164220 nFToken = -1 * nToken;
164224 int iBestScore = -1; /* Best score of columns checked so far */
164234 for(iRead=0; iRead<pTab->nColumn; iRead++){
164250 mCovered |= pFragment->covered;
164264 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
164274 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
164283 char *pList; /* Position-list */
164304 int iPos = 0; /* First position in position-list */
164308 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
164309 nTerm = pExpr->pPhrase->nToken;
164316 TermOffset *pT = &p->aTerm[p->iTerm++];
164317 pT->iOff = nTerm-iTerm-1;
164318 pT->pList = pList;
164319 pT->iPos = iPos;
164332 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164333 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
164340 if( !pCsr->pExpr ){
164346 assert( pCsr->isRequireSeek==0 );
164358 sCtx.iDocid = pCsr->iPrevId;
164362 ** string-buffer res for each column.
164364 for(iCol=0; iCol<pTab->nColumn; iCol++){
164380 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
164385 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
164388 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
164389 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
164391 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
164399 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
164404 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
164412 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
164413 iMinPos = pT->iPos-pT->iOff;
164423 if( 0==(0xFE&*pTerm->pList) ){
164424 pTerm->pList = 0;
164426 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
164429 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
164434 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
164436 rc = fts3StringAppend(&res, aBuffer, -1);
164437 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
164446 pMod->xClose(pC);
164458 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
164471 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164480 if( !pCsr->pExpr ){
164506 ** Implementation of the "unicode" full-text-search tokenizer.
164522 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
164542 c = sqlite3Utf8Trans1[c-0xc0]; \
164600 sqlite3_free(p->aiException);
164651 aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
164653 nNew = p->nException;
164663 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
164668 p->aiException = aNew;
164669 p->nException = nNew;
164676 ** Return true if the p->aiException[] array contains the value iCode.
164679 if( p->nException>0 ){
164680 int *a = p->aiException;
164682 int iHi = p->nException-1;
164691 iHi = iTest-1;
164723 pNew->bRemoveDiacritic = 1;
164730 pNew->bRemoveDiacritic = 1;
164733 pNew->bRemoveDiacritic = 0;
164736 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
164739 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
164757 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
164775 pCsr->aInput = (const unsigned char *)aInput;
164777 pCsr->nInput = 0;
164779 pCsr->nInput = (int)strlen(aInput);
164781 pCsr->nInput = nInput;
164784 *pp = &pCsr->base;
164795 sqlite3_free(pCsr->zToken);
164813 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
164816 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
164819 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
164831 zOut = pCsr->zToken;
164836 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
164837 char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
164839 zOut = &zNew[zOut - pCsr->zToken];
164840 pCsr->zToken = zNew;
164841 pCsr->nAlloc += 64;
164846 iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
164859 pCsr->iOff = (int)(z - pCsr->aInput);
164860 *paToken = pCsr->zToken;
164861 *pnToken = (int)(zOut - pCsr->zToken);
164862 *piStart = (int)(zStart - pCsr->aInput);
164863 *piEnd = (int)(zEnd - pCsr->aInput);
164864 *piPos = pCsr->iToken++;
164924 ** The most significant 22 bits in each 32-bit value contain the first
165020 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
165024 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
165032 iHi = iTest-1;
165046 ** of the ASCII letter only. For example, if passed 235 - "LATIN
165047 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
165081 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
165089 iHi = iTest-1;
165106 (mask0 & (1 << (c-768))) :
165107 (mask1 & (1 << (c-768-32)));
165217 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
165220 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
165222 int iRes = -1;
165227 int cmp = (c - aEntry[iTest].iCode);
165232 iHi = iTest-1;
165238 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
165239 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
165268 ** This file contains code for implementations of the r-tree and r*-tree
165273 ** Database Format of R-Tree Tables
165274 ** --------------------------------
165276 ** The data structure for a single virtual r-tree table is stored in three
165278 ** in the table name is replaced with the user-supplied name of the r-tree
165285 ** The data for each node of the r-tree structure is stored in the %_node
165286 ** table. For each node that is not the root node of the r-tree, there is
165292 ** The root node of an r-tree always exists, even if the r-tree table is
165298 ** of the node contain the tree depth as a big-endian integer.
165299 ** For non-root nodes, the first 2 bytes are left unused.
165305 ** consists of a single 8-byte integer followed by an even number
165306 ** of 4-byte coordinates. For leaf nodes the integer is the rowid
165350 #define RTREE_MAX_DIMENSIONS 5
165361 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
165369 ** An rtree virtual-table object.
165380 int iDepth; /* Current depth of the r-tree structure */
165381 char *zDb; /* Name of database containing r-tree table */
165382 char *zName; /* Name of r-tree table */
165388 ** linked together via the pointer normally used for hash chains -
165389 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
165393 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
165412 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
165435 ** When doing a search of an r-tree, instances of the following structure
165438 ** The id is always a node-id. For iLevel>=1 the id is the node-id of
165441 ** represents is the iCell-th entry in the parent node.
165457 ** If an R*-tree "Reinsert" operation is required, the same number of
165460 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
165465 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
165467 ** Therefore all non-root nodes must contain at least 3 entries. Since
165468 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
165479 #define RTREE_CACHE_SZ 5
165501 #define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
165505 ** coordinates within a single R-Tree are always of the same time.
165510 u32 u; /* Unsigned for byte-order conversions */
165523 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
165549 #define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
165550 #define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
165566 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
165584 ** xGeom and xQueryFunc fields is non-NULL, depending on whether the
165601 ** sqlite3_rtree_query_callback() create, and is read as the right-hand
165602 ** operand to the MATCH operator of an R-Tree.
165621 ** clang, since clang works hard to be gcc compatible. So the gcc
165622 ** optimizations will also work when compiling with clang.
165633 ** it is not, make it a no-op.
165641 ** and whether or not that determination is run-time or compile-time.
165643 ** For best performance, an attempt is made to guess at the byte-order
165644 ** using C-preprocessor macros. If that is unsuccessful, or if
165645 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
165646 ** at run-time.
165657 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
165679 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
165681 pCoord->u = _byteswap_ulong(*(u32*)p);
165683 pCoord->u = __builtin_bswap32(*(u32*)p);
165685 pCoord->u = *(u32*)p;
165687 pCoord->u = (
165715 (((u64)p[5]) << 16) +
165733 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
165737 i = __builtin_bswap32(pCoord->u);
165740 i = _byteswap_ulong(pCoord->u);
165743 i = pCoord->u;
165746 i = pCoord->u;
165769 p[5] = (i>>16)&0xFF;
165781 p->nRef++;
165789 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
165790 p->isDirty = 1;
165807 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
165816 assert( pNode->pNext==0 );
165817 iHash = nodeHash(pNode->iNode);
165818 pNode->pNext = pRtree->aHash[iHash];
165819 pRtree->aHash[iHash] = pNode;
165827 if( pNode->iNode!=0 ){
165828 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
165829 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
165830 *pp = pNode->pNext;
165831 pNode->pNext = 0;
165836 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
165843 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
165845 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
165846 pNode->zData = (u8 *)&pNode[1];
165847 pNode->nRef = 1;
165848 pNode->pParent = pParent;
165849 pNode->isDirty = 1;
165859 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
165860 sqlite3_blob *pBlob = pRtree->pNodeBlob;
165861 pRtree->pNodeBlob = 0;
165867 ** Obtain a reference to an r-tree node.
165870 Rtree *pRtree, /* R-tree structure */
165882 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
165883 if( pParent && !pNode->pParent ){
165885 pNode->pParent = pParent;
165887 pNode->nRef++;
165892 if( pRtree->pNodeBlob ){
165893 sqlite3_blob *pBlob = pRtree->pNodeBlob;
165894 pRtree->pNodeBlob = 0;
165896 pRtree->pNodeBlob = pBlob;
165902 if( pRtree->pNodeBlob==0 ){
165903 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
165905 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
165906 &pRtree->pNodeBlob);
165915 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
165916 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
165920 pNode->pParent = pParent;
165921 pNode->zData = (u8 *)&pNode[1];
165922 pNode->nRef = 1;
165923 pNode->iNode = iNode;
165924 pNode->isDirty = 0;
165925 pNode->pNext = 0;
165926 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
165927 pRtree->iNodeSize, 0);
165932 /* If the root node was just loaded, set pRtree->iDepth to the height
165933 ** of the r-tree structure. A height of zero means all data is stored on
165936 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
165939 pRtree->iDepth = readInt16(pNode->zData);
165940 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
165950 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
165974 Rtree *pRtree, /* The overall R-Tree */
165980 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
165981 p += writeInt64(p, pCell->iRowid);
165982 for(ii=0; ii<pRtree->nDim2; ii++){
165983 p += writeCoord(p, &pCell->aCoord[ii]);
165985 pNode->isDirty = 1;
165992 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
165993 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
165994 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
165996 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
165997 pNode->isDirty = 1;
166007 Rtree *pRtree, /* The overall R-Tree */
166014 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
166020 writeInt16(&pNode->zData[2], nCell+1);
166021 pNode->isDirty = 1;
166032 if( pNode->isDirty ){
166033 sqlite3_stmt *p = pRtree->pWriteNode;
166034 if( pNode->iNode ){
166035 sqlite3_bind_int64(p, 1, pNode->iNode);
166039 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
166041 pNode->isDirty = 0;
166043 if( pNode->iNode==0 && rc==SQLITE_OK ){
166044 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
166058 assert( pNode->nRef>0 );
166059 pNode->nRef--;
166060 if( pNode->nRef==0 ){
166061 if( pNode->iNode==1 ){
166062 pRtree->iDepth = -1;
166064 if( pNode->pParent ){
166065 rc = nodeRelease(pRtree, pNode->pParent);
166078 ** Return the 64-bit integer value associated with cell iCell of
166080 ** an internal node, then the 64-bit integer is a child page number.
166083 Rtree *pRtree, /* The overall R-Tree */
166088 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
166095 Rtree *pRtree, /* The overall R-Tree */
166101 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
166109 Rtree *pRtree, /* The overall R-Tree */
166117 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
166118 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
166119 pCoord = pCell->aCoord;
166125 }while( ii<pRtree->nDim2 );
166163 ** Increment the r-tree reference count.
166166 pRtree->nBusy++;
166170 ** Decrement the r-tree reference count. When the reference count reaches
166174 pRtree->nBusy--;
166175 if( pRtree->nBusy==0 ){
166176 pRtree->inWrTrans = 0;
166177 pRtree->nCursor = 0;
166179 sqlite3_finalize(pRtree->pWriteNode);
166180 sqlite3_finalize(pRtree->pDeleteNode);
166181 sqlite3_finalize(pRtree->pReadRowid);
166182 sqlite3_finalize(pRtree->pWriteRowid);
166183 sqlite3_finalize(pRtree->pDeleteRowid);
166184 sqlite3_finalize(pRtree->pReadParent);
166185 sqlite3_finalize(pRtree->pWriteParent);
166186 sqlite3_finalize(pRtree->pDeleteParent);
166209 pRtree->zDb, pRtree->zName,
166210 pRtree->zDb, pRtree->zName,
166211 pRtree->zDb, pRtree->zName
166217 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
166238 pCsr->base.pVtab = pVTab;
166240 pRtree->nCursor++;
166252 if( pCsr->aConstraint ){
166254 for(i=0; i<pCsr->nConstraint; i++){
166255 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
166257 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
166261 sqlite3_free(pCsr->aConstraint);
166262 pCsr->aConstraint = 0;
166270 Rtree *pRtree = (Rtree *)(cur->pVtab);
166273 assert( pRtree->nCursor>0 );
166275 sqlite3_free(pCsr->aPoint);
166276 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
166278 pRtree->nCursor--;
166286 ** Return non-zero if the cursor does not currently point to a valid
166291 return pCsr->atEOF;
166295 ** Convert raw bits from the on-disk RTree record into a coordinate value.
166296 ** The on-disk format is big-endian and needs to be converted for little-
166297 ** endian platforms. The on-disk record stores integer coordinates if
166298 ** eInt is true and it stores 32-bit floating point records if eInt is
166299 ** false. a[] is the four bytes of the on-disk record to be decoded.
166303 ** other four are various architectures-specific optimizations.
166352 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
166353 int nCoord = pInfo->nCoord; /* No. of coordinates */
166358 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
166361 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
166362 pInfo->iRowid = readInt64(pCellData);
166372 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
166387 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
166395 if( pConstraint->op==RTREE_MATCH ){
166397 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
166402 pInfo->aCoord = aCoord;
166403 pInfo->iLevel = pSearch->iLevel - 1;
166404 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
166405 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
166406 rc = pConstraint->u.xQueryFunc(pInfo);
166407 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
166408 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
166409 *prScore = pInfo->rScore;
166428 /* p->iCoord might point to either a lower or upper bound coordinate
166431 pCellData += 8 + 4*(p->iCoord&0xfe);
166433 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
166434 || p->op==RTREE_GT || p->op==RTREE_EQ );
166435 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
166436 switch( p->op ){
166442 if( p->u.rValue>=val ) return;
166443 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
166450 if( p->u.rValue<=val ) return;
166462 ** The op is given by p->op. The xN is p->iCoord-th coordinate in
166463 ** pCellData. $val is given by p->u.rValue.
166473 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
166474 || p->op==RTREE_GT || p->op==RTREE_EQ );
166475 pCellData += 8 + p->iCoord*4;
166476 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
166478 switch( p->op ){
166479 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
166480 case RTREE_LT: if( xN < p->u.rValue ) return; break;
166481 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
166482 case RTREE_GT: if( xN > p->u.rValue ) return; break;
166483 default: if( xN == p->u.rValue ) return; break;
166489 ** One of the cells in node pNode is guaranteed to have a 64-bit
166512 ** in its parent. If pNode is the root node, return -1.
166515 RtreeNode *pParent = pNode->pParent;
166517 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
166519 *piIndex = -1;
166531 ** is a depth-first search, which is the desired default behavior.
166537 if( pA->rScore<pB->rScore ) return -1;
166538 if( pA->rScore>pB->rScore ) return +1;
166539 if( pA->iLevel<pB->iLevel ) return -1;
166540 if( pA->iLevel>pB->iLevel ) return +1;
166548 RtreeSearchPoint t = p->aPoint[i];
166550 p->aPoint[i] = p->aPoint[j];
166551 p->aPoint[j] = t;
166555 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
166556 p->aNode[i] = 0;
166558 RtreeNode *pTemp = p->aNode[i];
166559 p->aNode[i] = p->aNode[j];
166560 p->aNode[j] = pTemp;
166569 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
166577 int ii = 1 - pCur->bPoint;
166579 assert( pCur->bPoint || pCur->nPoint );
166580 if( pCur->aNode[ii]==0 ){
166582 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
166583 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
166585 return pCur->aNode[ii];
166598 if( pCur->nPoint>=pCur->nPointAlloc ){
166599 int nNew = pCur->nPointAlloc*2 + 8;
166600 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
166602 pCur->aPoint = pNew;
166603 pCur->nPointAlloc = nNew;
166605 i = pCur->nPoint++;
166606 pNew = pCur->aPoint + i;
166607 pNew->rScore = rScore;
166608 pNew->iLevel = iLevel;
166612 j = (i-1)/2;
166613 pParent = pCur->aPoint + j;
166633 pCur->anQueue[iLevel]++;
166635 || pFirst->rScore>rScore
166636 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
166638 if( pCur->bPoint ){
166642 ii = (int)(pNew - pCur->aPoint) + 1;
166644 assert( pCur->aNode[ii]==0 );
166645 pCur->aNode[ii] = pCur->aNode[0];
166647 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
166649 pCur->aNode[0] = 0;
166650 *pNew = pCur->sPoint;
166652 pCur->sPoint.rScore = rScore;
166653 pCur->sPoint.iLevel = iLevel;
166654 pCur->bPoint = 1;
166655 return &pCur->sPoint;
166666 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
166670 printf(" %p\n", pCur->aNode[idx]);
166678 if( pCur->bPoint ){
166679 tracePoint(&pCur->sPoint, -1, pCur);
166681 for(ii=0; ii<pCur->nPoint; ii++){
166682 if( ii>0 || pCur->bPoint ) printf(" ");
166683 tracePoint(&pCur->aPoint[ii], ii, pCur);
166688 # define RTREE_QUEUE_TRACE(A,B) /* no-op */
166695 i = 1 - p->bPoint;
166697 if( p->aNode[i] ){
166698 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
166699 p->aNode[i] = 0;
166701 if( p->bPoint ){
166702 p->anQueue[p->sPoint.iLevel]--;
166703 p->bPoint = 0;
166704 }else if( p->nPoint ){
166705 p->anQueue[p->aPoint[0].iLevel]--;
166706 n = --p->nPoint;
166707 p->aPoint[0] = p->aPoint[n];
166708 if( n<RTREE_CACHE_SZ-1 ){
166709 p->aNode[1] = p->aNode[n+1];
166710 p->aNode[n+1] = 0;
166715 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
166716 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
166723 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
166737 ** contains an entry suitable for returning as a result-set row,
166748 int nConstraint = pCur->nConstraint;
166753 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
166754 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
166759 while( p->iCell<nCell ){
166760 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
166761 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
166764 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
166765 if( pConstraint->op>=RTREE_MATCH ){
166769 }else if( p->iLevel==1 ){
166776 p->iCell++;
166778 x.iLevel = p->iLevel - 1;
166783 x.id = p->id;
166784 x.iCell = p->iCell - 1;
166786 if( p->iCell>=nCell ){
166787 RTREE_QUEUE_TRACE(pCur, "POP-S:");
166793 p->eWithin = (u8)eWithin;
166794 p->id = x.id;
166795 p->iCell = x.iCell;
166796 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
166799 if( p->iCell>=nCell ){
166800 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
166804 pCur->atEOF = p==0;
166816 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
166831 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
166840 Rtree *pRtree = (Rtree *)cur->pVtab;
166850 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
166852 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
166854 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
166859 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
166877 sqlite3_int64 *piNode /* Write the node-id here */
166881 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
166882 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
166883 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
166886 sqlite3_reset(pRtree->pReadRowid);
166888 rc = sqlite3_reset(pRtree->pReadRowid);
166896 ** first argument to this function is the right-hand operand to the MATCH
166906 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
166910 memcpy(pBlob, pSrc, pSrc->iSize);
166911 pInfo->pContext = pBlob->cb.pContext;
166912 pInfo->nParam = pBlob->nParam;
166913 pInfo->aParam = pBlob->aParam;
166914 pInfo->apSqlParam = pBlob->apSqlParam;
166916 if( pBlob->cb.xGeom ){
166917 pCons->u.xGeom = pBlob->cb.xGeom;
166919 pCons->op = RTREE_QUERY;
166920 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
166922 pCons->pInfo = pInfo;
166934 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
166945 sqlite3_free(pCsr->aPoint);
166947 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
166949 pCsr->iStrategy = idxNum;
166951 /* Special case - lookup by rowid. */
166959 assert( p!=0 ); /* Always returns pCsr->sPoint */
166960 pCsr->aNode[0] = pLeaf;
166961 p->id = iNode;
166962 p->eWithin = PARTLY_WITHIN;
166964 p->iCell = (u8)iCell;
166965 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
166967 pCsr->atEOF = 1;
166970 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
166975 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
166976 pCsr->nConstraint = argc;
166977 if( !pCsr->aConstraint ){
166980 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
166981 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
166985 RtreeConstraint *p = &pCsr->aConstraint[ii];
166986 p->op = idxStr[ii*2];
166987 p->iCoord = idxStr[ii*2+1]-'0';
166988 if( p->op>=RTREE_MATCH ){
166989 /* A MATCH operator. The right-hand-side must be a blob that
166997 p->pInfo->nCoord = pRtree->nDim2;
166998 p->pInfo->anQueue = pCsr->anQueue;
166999 p->pInfo->mxLevel = pRtree->iDepth + 1;
167002 p->u.rValue = sqlite3_value_int64(argv[ii]);
167004 p->u.rValue = sqlite3_value_double(argv[ii]);
167012 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
167014 pNew->id = 1;
167015 pNew->iCell = 0;
167016 pNew->eWithin = PARTLY_WITHIN;
167017 assert( pCsr->bPoint==1 );
167018 pCsr->aNode[0] = pRoot;
167020 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
167036 ** ------------------------------------------------
167038 ** 2 See below R-tree query or full-table scan.
167039 ** ------------------------------------------------
167051 ** ----------------------
167058 ** ----------------------
167075 /* Check if there exists a MATCH constraint - even an unusable one. If there
167076 ** is, do not consider the lookup-by-rowid plan as using such a plan would
167079 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
167080 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
167085 assert( pIdxInfo->idxStr==0 );
167086 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
167087 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
167089 if( bMatch==0 && p->usable
167090 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
167095 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
167096 pIdxInfo->aConstraintUsage[jj].omit = 0;
167098 pIdxInfo->idxNum = 1;
167099 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
167100 pIdxInfo->aConstraintUsage[jj].omit = 1;
167102 /* This strategy involves a two rowid lookups on an B-Tree structures
167103 ** and then a linear search of an R-Tree node. This should be
167108 pIdxInfo->estimatedCost = 30.0;
167109 pIdxInfo->estimatedRows = 1;
167113 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
167115 switch( p->op ){
167122 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
167127 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
167128 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
167129 pIdxInfo->aConstraintUsage[ii].omit = 1;
167133 pIdxInfo->idxNum = 2;
167134 pIdxInfo->needToFreeIdxStr = 1;
167135 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
167139 nRow = pRtree->nRowEst >> (iIdx/2);
167140 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
167141 pIdxInfo->estimatedRows = nRow;
167147 ** Return the N-dimensional volumn of the cell stored in *p.
167151 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
167153 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
167154 switch( pRtree->nDim ){
167155 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
167156 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
167157 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
167158 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
167159 default: area *= p->aCoord[1].f - p->aCoord[0].f;
167164 switch( pRtree->nDim ){
167165 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
167166 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
167167 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
167168 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
167169 default: area *= p->aCoord[1].i - p->aCoord[0].i;
167181 int ii = pRtree->nDim2 - 2;
167183 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
167184 ii -= 2;
167194 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
167196 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
167197 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
167199 }while( ii<pRtree->nDim2 );
167202 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
167203 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
167205 }while( ii<pRtree->nDim2 );
167215 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
167216 for(ii=0; ii<pRtree->nDim2; ii+=2){
167217 RtreeCoord *a1 = &p1->aCoord[ii];
167218 RtreeCoord *a2 = &p2->aCoord[ii];
167237 return (cellArea(pRtree, &cell)-area);
167251 for(jj=0; jj<pRtree->nDim2; jj+=2){
167253 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
167254 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
167259 o = o * (x2-x1);
167275 int iHeight, /* Height of sub-tree rooted at pCell */
167283 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
167338 while( p->pParent ){
167339 RtreeNode *pParent = p->pParent;
167359 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
167362 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
167363 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
167364 sqlite3_step(pRtree->pWriteRowid);
167365 return sqlite3_reset(pRtree->pWriteRowid);
167369 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
167372 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
167373 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
167374 sqlite3_step(pRtree->pWriteParent);
167375 return sqlite3_reset(pRtree->pWriteParent);
167384 ** (nIdx-1) in no particular order. This function sorts the values
167409 int nRight = nIdx-nLeft;
167444 RtreeDValue left = aDistance[aIdx[jj-1]];
167456 ** (nIdx-1) in no particular order. This function sorts the values
167478 int nRight = nIdx-nLeft;
167509 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
167510 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
167521 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
167540 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
167547 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
167549 for(ii=0; ii<pRtree->nDim; ii++){
167551 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
167558 for(ii=0; ii<pRtree->nDim; ii++){
167567 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
167577 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
167578 for(kk=1; kk<(nCell-1); kk++){
167632 nodeRelease(pRtree, pChild->pParent);
167634 pChild->pParent = pNode;
167637 return xSetMapping(pRtree, iRowid, pNode->iNode);
167677 if( pNode->iNode==1 ){
167680 pRtree->iDepth++;
167681 pNode->isDirty = 1;
167682 writeInt16(pNode->zData, pRtree->iDepth);
167685 pRight = nodeNew(pRtree, pLeft->pParent);
167694 memset(pLeft->zData, 0, pRtree->iNodeSize);
167695 memset(pRight->zData, 0, pRtree->iNodeSize);
167709 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
167714 rightbbox.iRowid = pRight->iNode;
167715 leftbbox.iRowid = pLeft->iNode;
167717 if( pNode->iNode==1 ){
167718 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
167723 RtreeNode *pParent = pLeft->pParent;
167734 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
167741 if( iRowid==pCell->iRowid ){
167748 if( pNode->iNode==1 ){
167757 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
167777 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
167779 ** the pLeaf->pParent chain all the way up to the root node.
167781 ** This operation is required when a row is deleted (or updated - an update
167790 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
167792 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
167793 rc = sqlite3_step(pRtree->pReadParent);
167798 /* Before setting pChild->pParent, test that we are not creating a
167803 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
167804 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
167806 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
167809 rc = sqlite3_reset(pRtree->pReadParent);
167811 if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
167812 pChild = pChild->pParent;
167825 assert( pNode->nRef==1 );
167830 pParent = pNode->pParent;
167831 pNode->pParent = 0;
167843 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
167844 sqlite3_step(pRtree->pDeleteNode);
167845 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
167850 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
167851 sqlite3_step(pRtree->pDeleteParent);
167852 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
167856 /* Remove the node from the in-memory hash table and link it into
167857 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
167860 pNode->iNode = iHeight;
167861 pNode->pNext = pRtree->pDeleted;
167862 pNode->nRef++;
167863 pRtree->pDeleted = pNode;
167869 RtreeNode *pParent = pNode->pParent;
167881 box.iRowid = pNode->iNode;
167893 ** cell, adjust the r-tree data structure if required.
167904 ** the in-memory node image, so it cannot fail.
167913 pParent = pNode->pParent;
167914 assert( pParent || pNode->iNode==1 );
167965 if( ii==(nCell-1) ){
167971 for(iDim=0; iDim<pRtree->nDim; iDim++){
167976 for(iDim=0; iDim<pRtree->nDim; iDim++){
167982 for(iDim=0; iDim<pRtree->nDim; iDim++){
167983 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
167985 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
167992 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
167995 if( p->iRowid==pCell->iRowid ){
167997 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
167999 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
168007 /* Find a node to store this cell in. pNode->iNode currently contains
168008 ** the height of the sub-tree headed by the cell.
168039 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
168041 nodeRelease(pRtree, pChild->pParent);
168043 pChild->pParent = pNode;
168047 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
168050 pRtree->iReinsertHeight = iHeight;
168057 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
168059 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
168076 /* Find a node to store this cell in. pNode->iNode currently contains
168077 ** the height of the sub-tree headed by the cell.
168079 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
168082 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
168093 ** Select a currently unused rowid for a new r-tree record.
168097 sqlite3_bind_null(pRtree->pWriteRowid, 1);
168098 sqlite3_bind_null(pRtree->pWriteRowid, 2);
168099 sqlite3_step(pRtree->pWriteRowid);
168100 rc = sqlite3_reset(pRtree->pWriteRowid);
168101 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
168106 ** Remove the entry with rowid=iDelete from the r-tree structure.
168140 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
168141 sqlite3_step(pRtree->pDeleteRowid);
168142 rc = sqlite3_reset(pRtree->pDeleteRowid);
168153 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
168159 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
168164 pRtree->iDepth--;
168165 writeInt16(pRoot->zData, pRtree->iDepth);
168166 pRoot->isDirty = 1;
168170 /* Re-insert the contents of any underfull nodes removed from the tree. */
168171 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
168175 pRtree->pDeleted = pLeaf->pNext;
168190 ** Rounding constants for float->double conversion.
168192 #define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
168221 ** (at pRtree->base.zErrMsg) to an appropriate value and returns
168237 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
168239 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
168248 pRtree->base.zErrMsg = sqlite3_mprintf(
168249 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
168254 pRtree->base.zErrMsg = sqlite3_mprintf(
168255 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
168285 /* Constraint handling. A write operation on an r-tree table may return
168291 ** In the first case, if the conflict-handling mode is REPLACE, then
168294 ** conflict-handling mode specified by the user.
168301 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
168303 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
168307 assert( nData<=(pRtree->nDim2 + 3) );
168310 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
168311 for(ii=0; ii<nData-4; ii+=2){
168322 for(ii=0; ii<nData-4; ii+=2){
168340 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
168341 steprc = sqlite3_step(pRtree->pReadRowid);
168342 rc = sqlite3_reset(pRtree->pReadRowid);
168344 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
168357 ** record to delete from the r-tree table. The following block does
168365 ** (azData[2]..azData[argc-1]) contain a new record to insert into
168366 ** the r-tree structure.
168369 /* Insert the new record into the r-tree */
168383 pRtree->iReinsertHeight = -1;
168402 assert( pRtree->inWrTrans==0 );
168403 pRtree->inWrTrans++;
168413 pRtree->inWrTrans = 0;
168428 , pRtree->zDb, pRtree->zName, zNewName
168429 , pRtree->zDb, pRtree->zName, zNewName
168430 , pRtree->zDb, pRtree->zName, zNewName
168434 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
168445 ** DROP TABLE command - which fortunately always opens a savepoint - cannot
168451 ** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
168456 int iwt = pRtree->inWrTrans;
168458 pRtree->inWrTrans = 0;
168460 pRtree->inWrTrans = iwt;
168465 ** This function populates the pRtree->nRowEst variable with an estimate
168477 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
168480 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
168483 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
168487 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
168497 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
168499 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
168510 rtreeCreate, /* xCreate - create a table */
168511 rtreeConnect, /* xConnect - connect to an existing table */
168512 rtreeBestIndex, /* xBestIndex - Determine search strategy */
168513 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
168514 rtreeDestroy, /* xDestroy - Drop a table */
168515 rtreeOpen, /* xOpen - open a cursor */
168516 rtreeClose, /* xClose - close a cursor */
168517 rtreeFilter, /* xFilter - configure scan constraints */
168518 rtreeNext, /* xNext - advance a cursor */
168520 rtreeColumn, /* xColumn - read data */
168521 rtreeRowid, /* xRowid - read data */
168522 rtreeUpdate, /* xUpdate - write data */
168523 rtreeBeginTransaction, /* xBegin - begin transaction */
168524 rtreeEndTransaction, /* xSync - sync transaction */
168525 rtreeEndTransaction, /* xCommit - commit transaction */
168526 rtreeEndTransaction, /* xRollback - rollback transaction */
168527 0, /* xFindFunction - function overloading */
168528 rtreeRename, /* xRename - rename the table */
168562 pRtree->db = db;
168571 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
168583 appStmt[0] = &pRtree->pWriteNode;
168584 appStmt[1] = &pRtree->pDeleteNode;
168585 appStmt[2] = &pRtree->pReadRowid;
168586 appStmt[3] = &pRtree->pWriteRowid;
168587 appStmt[4] = &pRtree->pDeleteRowid;
168588 appStmt[5] = &pRtree->pReadParent;
168589 appStmt[6] = &pRtree->pWriteParent;
168590 appStmt[7] = &pRtree->pDeleteParent;
168596 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
168618 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
168631 ** determine the node-size used by the rtree table being created or connected
168632 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
168636 ** table already exists. In this case the node-size is determined by inspecting
168639 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
168641 ** database page-size is so large that more than RTREE_MAXCELLS entries
168642 ** would fit in a single node, use a smaller node-size.
168654 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
168657 pRtree->iNodeSize = iPageSize-64;
168658 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
168659 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
168667 pRtree->zDb, pRtree->zName
168669 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
168672 }else if( pRtree->iNodeSize<(512-64) ){
168675 pRtree->zName);
168685 ** methods of the r-tree virtual table.
168687 ** argv[0] -> module name
168688 ** argv[1] -> database name
168689 ** argv[2] -> table name
168690 ** argv[...] -> column names...
168729 pRtree->nBusy = 1;
168730 pRtree->base.pModule = &rtreeModule;
168731 pRtree->zDb = (char *)&pRtree[1];
168732 pRtree->zName = &pRtree->zDb[nDb+1];
168733 pRtree->nDim = (u8)((argc-4)/2);
168734 pRtree->nDim2 = pRtree->nDim*2;
168735 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
168736 pRtree->eCoordType = (u8)eCoordType;
168737 memcpy(pRtree->zDb, argv[1], nDb);
168738 memcpy(pRtree->zName, argv[2], nName);
168745 ** the r-tree table schema.
168777 assert( pRtree->nBusy==1 );
168785 ** Implementation of a scalar function that decodes r-tree nodes to
168789 ** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
168790 ** an r-tree node. For a two-dimensional r-tree structure called "rt", to
168796 ** entry for each cell in the r-tree node. Each entry is itself a
168797 ** list, containing the 8-byte rowid/pageno followed by the
168798 ** <num-dimension>*2 coordinates.
168821 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
168825 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
168828 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
168843 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
168847 ** from the front of a blob that is an r-tree node. For example:
168860 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
168868 ** Register the r-tree module with database handle db. This creates the
168905 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
168915 for(i=0; i<p->nParam; i++){
168916 sqlite3_value_free(p->apSqlParam[i]);
168927 ** contains the geometry-checking callback routines and a list of
168931 ** The R-Tree MATCH operator will read the returned BLOB, deserialize
168933 ** out which elements of the R-Tree should be returned by the query.
168941 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
168948 pBlob->iSize = nBlob;
168949 pBlob->cb = pGeomCtx[0];
168950 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
168951 pBlob->nParam = nArg;
168953 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
168954 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
168956 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
168958 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
168971 ** Register a new geometry function for use with the r-tree MATCH operator.
168979 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
168984 pGeomCtx->xGeom = xGeom;
168985 pGeomCtx->xQueryFunc = 0;
168986 pGeomCtx->xDestructor = 0;
168987 pGeomCtx->pContext = pContext;
168988 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
168994 ** Register a new 2nd-generation geometry function for use with the
168995 ** r-tree MATCH operator.
169004 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
169009 pGeomCtx->xGeom = 0;
169010 pGeomCtx->xQueryFunc = xQueryFunc;
169011 pGeomCtx->xDestructor = xDestructor;
169012 pGeomCtx->pContext = pContext;
169013 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
169050 ** ("International Components for Unicode", an open-source library
169063 ** provide case-independent matching.
169100 ** a multi-byte UTF8 character. It is copied here from SQLite source
169117 c = icuUtf8Trans1[c-0xc0]; \
169131 ** Compare two UTF-8 strings for equality where the first string is
169137 const uint8_t *zString, /* The UTF-8 string to compare against */
169154 ** 1. uPattern is an unescaped match-all character "%",
169155 ** 2. uPattern is an unescaped match-one character "_",
169212 ** the build-in LIKE operator. The first argument to the function is the
169236 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
169242 /* The escape character string must consist of a single UTF-8 character.
169252 "ESCAPE expression must be a single character", -1);
169277 sqlite3_result_error(pCtx, zBuf, -1);
169329 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
169341 uregex_setText(pExpr, zString, -1, &status);
169366 ** Implementations of scalar functions for case mapping - upper() and
169367 ** lower(). Function upper() converts its input to upper-case (ABC).
169368 ** Function lower() converts to lower-case (abc).
169377 ** upper('ABC') -> 'abc'
169378 ** lower('abc') -> 'ABC'
169386 ** lower('I', 'en_us') -> 'i'
169387 ** lower('I', 'tr_tr') -> '\u131' (small dotless i)
169389 ** http://www.icu-project.org/userguide/posix.html#case_mappings
169394 int nInput; /* Size of utf-16 input string in bytes */
169469 case UCOL_LESS: return -1;
169484 ** SELECT icu_load_collation(<locale>, <collation-name>);
169487 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
169497 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
169523 sqlite3_result_error(p, "Error registering collation function", -1);
169558 db, p->zName, p->nArg, p->enc,
169559 p->iContext ? (void*)db : (void*)0,
169560 p->xFunc, 0, 0
169622 UBreakIterator *pIter; /* ICU break-iterator object */
169624 UChar *aChar; /* Copy of input using utf-16 encoding */
169625 int *aOffset; /* Offsets of each character in utf-8 input */
169654 p->zLocale = (char *)&p[1];
169655 memcpy(p->zLocale, argv[0], n);
169674 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
169713 pCsr->aChar = (UChar *)&pCsr[1];
169714 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
169716 pCsr->aOffset[iOut] = iInput;
169721 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
169726 pCsr->aOffset[iOut] = iInput;
169735 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
169740 pCsr->nChar = iOut;
169742 ubrk_first(pCsr->pIter);
169752 ubrk_close(pCsr->pIter);
169753 sqlite3_free(pCsr->zBuffer);
169778 iStart = ubrk_current(pCsr->pIter);
169779 iEnd = ubrk_next(pCsr->pIter);
169786 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
169799 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
169803 pCsr->zBuffer = zNew;
169804 pCsr->nBuffer = nByte;
169808 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
169809 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
169812 } while( nByte>pCsr->nBuffer );
169814 *ppToken = pCsr->zBuffer;
169816 *piStartOffset = pCsr->aOffset[iStart];
169817 *piEndOffset = pCsr->aOffset[iEnd];
169818 *piPosition = pCsr->iToken++;
169875 ** to a *-oal file. A *-oal file is just like a *-wal file, except
169876 ** that it is named "<database>-oal" instead of "<database>-wal".
169878 ** "<database>-oal", they go on using the original database in
169879 ** rollback mode while the *-oal file is being generated.
169884 ** to update each b-tree individually. All updates required by each
169885 ** b-tree are completed before moving on to the next, and all
169888 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
169894 ** detect the new *-wal file and read the database in wal mode. At
169895 ** this point they see the new version of the database - including
169898 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
169901 ** handle is closed before the entire *-wal file is checkpointed,
169911 ** When state is saved, any commit to the *-oal file and the commit to
169921 ** by delete the RBU update database and *-oal file and have the device
169960 ** b-tree indexes that are collectively larger than the available cache
169963 ** The problem is that in order to update a b-tree, the leaf page (at least)
169973 ** by index key before applying them to the b-tree. This leads to an IO
169974 ** pattern that resembles a single linear scan through the index b-tree,
169981 ** large transaction to be broken down into sub-transactions performed
169986 ** has committed one or more sub-transactions, other database clients continue
170005 ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
170026 ** target table, and one more - "rbu_control". The data_% table should
170045 ** of zero or more numeric characters (0-9). This can be significant because
170055 ** key column - "rowid". Virtual tables for which the "rowid" column does
170066 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
170068 ** hidden columns are optional - they are updated by RBU if present in
170104 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
170119 ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
170120 ** the target table column. rbu_delta() is invoked with two arguments - the
170134 ** to be a "fossil delta" - a patch to be applied to a blob value in the
170135 ** format used by the fossil source-code management system. In this case
170172 ** b-tree operation, so thousands of calls may be required to apply
170233 ** Or, if zState is non-NULL, it must be a path to a database file in which
170247 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
170259 ** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
170273 ** name of the state database is "<database>-vacuum", where <database>
170275 ** state database is not already present in the file-system, it is created
170302 ** database handle, or non-zero to access the rbu update database handle.
170337 ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
170364 ** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
170375 ** Return the total number of key-value operations (inserts, deletes or
170392 ** The update is visible to non-RBU clients during stage 2. During stage 1
170393 ** non-RBU reader clients may see the original database.
170417 ** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
170429 ** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
170430 ** may either add further data to the *-oal file, or compute data that will
170434 ** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
170435 ** will move the *-oal file to the equivalent *-wal path. If the current
170442 ** sqlite3rbu_step() will copy a page of data from the *-wal file into
170457 #define SQLITE_RBU_STATE_ERROR 5
170462 ** Create an RBU VFS named zName that accesses the underlying file-system
170464 ** then the new RBU VFS uses the default system VFS to access the file-system.
170465 ** The new object is registered as a non-default VFS with SQLite before
170475 ** (for example multiplexor) to access the file-system. For example,
170484 ** // access the file-system via the system default VFS, bypassing the
170499 ** non-RBU users. There is no harm in an application accessing the
170500 ** file-system via "rbu" all the time, even if it only uses RBU functionality
170544 ** May be set to integer values 1, 2, 4 or 5. As follows:
170545 ** 1: the *-rbu file is currently under construction.
170546 ** 2: the *-rbu file has been constructed, but not yet moved
170547 ** to the *-wal path.
170549 ** 5: the rbu update has been checkpointed.
170569 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
170570 ** header created by recovering the *-wal file. This is used to detect
170571 ** cases when another client appends frames to the *-wal file in the
170576 ** Valid if STAGE==1. The current change-counter cookie value in the
170580 ** Valid if STAGE==1. The size in bytes of the *-oal file.
170586 #define RBU_STATE_PROGRESS 5
170596 #define RBU_STAGE_DONE 5
170670 int *aiSrcOrder; /* src table col -> target table col */
170674 int eType; /* Table type - an RBU_PK_XXX value */
170693 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
170705 ** 5: Table is a virtual table.
170712 #define RBU_PK_VTAB 5
170719 #define RBU_INSERT 1 /* Insert on a main table b-tree */
170720 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
170722 #define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
170723 #define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
170725 #define RBU_UPDATE 6 /* Update a row in a main table b-tree */
170728 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
170741 ** a running estimate of the number of b-tree operations required to
170742 ** finish populating the *-oal file. This allows the sqlite3_bp_progress()
170743 ** API to calculate the permyriadage progress of populating the *-oal file
170761 ** no b-tree operations are required on index b-trees. Or if the
170763 ** required (one delete and one insert on each index b-tree).
170767 ** b-trees.
170783 char zStateDb[5]; /* Db name for state ("stat" or "main") */
170834 u8 iWriteVer; /* "write-version" value for main db files */
170838 char **apShm; /* Array of mmap'd *-shm regions */
170849 #define rbuIsVacuum(p) ((p)->zTarget==0)
170859 ** are lifted from the fossil source code (http://fossil-scm.org). They
170871 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170872 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170873 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170874 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
170875 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
170876 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
170877 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
170878 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
170887 z--;
170888 *pLen -= z - zStart;
170894 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
170904 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
170908 N -= 16;
170916 N -= 4;
170935 ** The delta string should be null-terminated. But the delta string
170943 ** then this routine returns -1.
170964 return -1;
170966 zDelta++; lenDelta--;
170972 zDelta++; lenDelta--;
170976 return -1;
170978 zDelta++; lenDelta--;
170982 return -1;
170986 return -1;
170993 zDelta++; lenDelta--;
170997 return -1;
171001 return -1;
171006 lenDelta -= cnt;
171010 zDelta++; lenDelta--;
171015 return -1;
171020 return -1;
171026 return -1;
171031 return -1;
171039 return -1;
171080 sqlite3_result_error(context, "corrupt fossil delta", -1);
171090 sqlite3_result_error(context, "corrupt fossil delta", -1);
171114 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
171177 for(i=0; i<pIter->nTblCol; i++){
171178 sqlite3_free(pIter->azTblCol[i]);
171179 sqlite3_free(pIter->azTblType[i]);
171181 sqlite3_free(pIter->azTblCol);
171182 pIter->azTblCol = 0;
171183 pIter->azTblType = 0;
171184 pIter->aiSrcOrder = 0;
171185 pIter->abTblPk = 0;
171186 pIter->abNotNull = 0;
171187 pIter->nTblCol = 0;
171188 pIter->eType = 0; /* Invalid value */
171198 sqlite3_finalize(pIter->pSelect);
171199 sqlite3_finalize(pIter->pInsert);
171200 sqlite3_finalize(pIter->pDelete);
171201 sqlite3_finalize(pIter->pTmpInsert);
171202 pUp = pIter->pRbuUpdate;
171204 RbuUpdateStmt *pTmp = pUp->pNext;
171205 sqlite3_finalize(pUp->pUpdate);
171210 pIter->pSelect = 0;
171211 pIter->pInsert = 0;
171212 pIter->pDelete = 0;
171213 pIter->pRbuUpdate = 0;
171214 pIter->pTmpInsert = 0;
171215 pIter->nCol = 0;
171224 sqlite3_finalize(pIter->pTblIter);
171225 sqlite3_finalize(pIter->pIdxIter);
171239 int rc = p->rc;
171244 if( pIter->zIdx==0 ){
171245 rc = sqlite3_exec(p->dbMain,
171250 , 0, 0, &p->zErrmsg
171255 if( pIter->bCleanup ){
171257 pIter->bCleanup = 0;
171258 rc = sqlite3_step(pIter->pTblIter);
171260 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
171261 pIter->zTbl = 0;
171263 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
171264 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
171265 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
171268 if( pIter->zIdx==0 ){
171269 sqlite3_stmt *pIdx = pIter->pIdxIter;
171270 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
171273 rc = sqlite3_step(pIter->pIdxIter);
171275 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
171276 pIter->bCleanup = 1;
171277 pIter->zIdx = 0;
171279 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
171280 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
171281 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
171282 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
171291 p->rc = rc;
171299 ** accepts one or two arguments. The first argument is the name of a table -
171303 ** For a non-vacuum RBU handle, if the table name matches the pattern:
171305 ** data[0-9]_<name>
171311 ** "data_t1" -> "t1"
171312 ** "data0123_t2" -> "t2"
171313 ** "dataAB_t3" -> NULL
171331 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
171338 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
171357 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
171367 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
171374 pIter->bCleanup = 1;
171375 p->rc = rc;
171383 ** If an error has already occurred (p->rc is already set to something other
171393 if( p->rc==SQLITE_OK ){
171394 if( zSql==0 ) p->rc = SQLITE_NOMEM;
171411 ** called, it is a no-op.
171418 if( p->rc==SQLITE_OK ){
171420 p->rc = SQLITE_NOMEM;
171422 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
171427 return p->rc;
171442 if( p->rc==SQLITE_OK ){
171446 p->rc = SQLITE_NOMEM;
171456 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
171466 pIter->azTblCol = azNew;
171467 pIter->azTblType = &azNew[nCol];
171468 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
171469 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
171470 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
171471 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
171476 ** The first argument must be a nul-terminated string. This function
171512 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
171513 p->rc = rc;
171514 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
171575 assert( p->rc==SQLITE_OK );
171576 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
171582 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
171592 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
171595 if( p->rc ) goto rbuTableType_end;
171600 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
171604 if( p->rc==SQLITE_OK ){
171616 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
171619 if( p->rc==SQLITE_OK ){
171621 if( sqlite3_column_int(aStmt[3],5)>0 ){
171639 ** the pIter->abIndexed[] array.
171645 if( p->rc==SQLITE_OK ){
171646 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
171647 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
171648 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
171652 pIter->nIndex = 0;
171653 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
171657 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
171660 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
171662 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
171666 pIter->nIndex++;
171669 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
171670 /* "PRAGMA index_list" includes the main PK b-tree */
171671 pIter->nIndex--;
171675 if( bIndex==0 ) pIter->abIndexed = 0;
171680 ** If they are not already populated, populate the pIter->azTblCol[],
171681 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
171689 if( pIter->azTblCol==0 ){
171698 assert( pIter->eType==0 );
171699 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
171700 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
171701 p->rc = SQLITE_ERROR;
171702 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
171704 if( p->rc ) return p->rc;
171705 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
171707 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
171708 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
171709 || pIter->eType==RBU_PK_VTAB
171715 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
171716 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
171718 if( p->rc==SQLITE_OK ){
171722 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
171725 char *zCopy = rbuStrndup(zName, &p->rc);
171726 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
171727 pIter->azTblCol[pIter->nTblCol++] = zCopy;
171736 if( p->rc==SQLITE_OK
171738 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
171740 p->rc = SQLITE_ERROR;
171741 p->zErrmsg = sqlite3_mprintf(
171742 "table %q %s rbu_rowid column", pIter->zDataTbl,
171747 /* Check that all non-HIDDEN columns in the destination table are also
171750 if( p->rc==SQLITE_OK ){
171751 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
171752 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
171755 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
171757 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
171758 for(i=iOrder; i<pIter->nTblCol; i++){
171759 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
171761 if( i==pIter->nTblCol ){
171762 p->rc = SQLITE_ERROR;
171763 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
171764 pIter->zDataTbl, zName
171767 int iPk = sqlite3_column_int(pStmt, 5);
171772 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
171773 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
171776 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
171777 pIter->abTblPk[iOrder] = (iPk!=0);
171778 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
171785 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
171786 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
171789 return p->rc;
171793 ** This function constructs and returns a pointer to a nul-terminated
171795 ** column names currently stored in the pIter->azTblCol[] array.
171804 for(i=0; i<pIter->nTblCol; i++){
171805 const char *z = pIter->azTblCol[i];
171844 int rc = p->rc; /* Error code */
171856 assert( p->zErrmsg==0 );
171857 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
171858 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
171872 if( pIter->eType==RBU_PK_IPK ){
171874 for(i=0; pIter->abTblPk[i]==0; i++);
171875 assert( i<pIter->nTblCol );
171876 zCol = pIter->azTblCol[i];
171884 zCol = pIter->azTblCol[iCid];
171885 zType = pIter->azTblType[iCid];
171889 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
171919 p->rc = rc;
171937 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
171946 if( p->rc==SQLITE_OK && pIter->abIndexed ){
171949 for(i=0; i<pIter->nTblCol; i++){
171950 if( pIter->abIndexed[i] ){
171951 const char *zCol = pIter->azTblCol[i];
171958 p->rc = SQLITE_NOMEM;
171964 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
171986 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
171987 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
171988 }else if( pIter->eType==RBU_PK_EXTERNAL ){
171991 for(i=0; i<pIter->nTblCol; i++){
171992 if( pIter->abTblPk[i] ){
172004 for(i=0; i<pIter->nTblCol; i++){
172005 if( pIter->abTblPk[i] ){
172006 const char *zCol = pIter->azTblCol[i];
172017 ** (p->objiter.pSelect) currently points to a valid row. However, there
172019 ** stored in the (p->nCol+1)'th column. Set the error code and error message
172023 p->rc = SQLITE_ERROR;
172024 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
172029 ** Return a nul-terminated string containing the comma separated list of
172051 if( p->rc==SQLITE_OK ){
172054 if( (int)strlen(zMask)!=pIter->nTblCol ){
172058 for(i=0; i<pIter->nTblCol; i++){
172059 char c = zMask[pIter->aiSrcOrder[i]];
172062 zList, zSep, pIter->azTblCol[i], i+1
172068 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
172074 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
172085 ** Return a nul-terminated string consisting of nByte comma separated
172128 assert( pIter->zIdx==0 );
172129 if( p->rc==SQLITE_OK ){
172131 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
172132 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
172134 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
172135 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
172137 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
172142 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
172151 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
172152 if( sqlite3_column_int(pXInfo, 5) ){
172168 ** a table b-tree where the table has an external primary key. If the
172171 ** no-op.
172186 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
172187 int tnum = pIter->iPkTnum; /* Root page of PK index */
172197 ** zIdx to point to a nul-terminated string containing this name. */
172198 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
172201 if( p->rc==SQLITE_OK ){
172208 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
172214 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
172215 int bKey = sqlite3_column_int(pXInfo, 5);
172221 iCid, pIter->azTblType[iCid], zCollate
172230 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
172231 rbuMPrintfExec(p, p->dbMain,
172235 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
172249 ** table b-tree of the table before returning. Non-zero is returned if
172260 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
172261 int tnum = pIter->iTnum;
172265 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
172267 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
172269 const char *zCol = pIter->azTblCol[iCol];
172272 p->rc = sqlite3_table_column_metadata(
172273 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
172276 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
172282 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
172283 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
172288 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
172295 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
172296 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
172297 pIter->zTbl, zSql,
172298 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
172300 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
172321 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
172322 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
172324 assert( pIter->pTmpInsert==0 );
172325 p->rc = prepareFreeAndCollectError(
172326 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
172328 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
172343 || p->objiter.eType==RBU_PK_EXTERNAL
172344 || p->objiter.eType==RBU_PK_NONE
172347 p->nPhaseOneStep += p->objiter.nIndex;
172351 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
172354 sqlite3_step(p->objiter.pTmpInsert);
172355 rc = sqlite3_reset(p->objiter.pTmpInsert);
172371 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
172373 assert( pIter->bCleanup==0 );
172374 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
172375 const int tnum = pIter->iTnum;
172377 char **pz = &p->zErrmsg;
172378 const char *zIdx = pIter->zIdx;
172382 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
172383 if( !zLimit ) p->rc = SQLITE_NOMEM;
172387 const char *zTbl = pIter->zTbl;
172394 assert( pIter->eType!=RBU_PK_VTAB );
172401 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
172402 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
172403 rbuMPrintfExec(p, p->dbMain,
172407 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
172410 pIter->nCol = nBind;
172411 if( p->rc==SQLITE_OK ){
172412 p->rc = prepareFreeAndCollectError(
172413 p->dbMain, &pIter->pInsert, &p->zErrmsg,
172419 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
172420 p->rc = prepareFreeAndCollectError(
172421 p->dbMain, &pIter->pDelete, &p->zErrmsg,
172427 if( p->rc==SQLITE_OK ){
172433 pIter->zDataTbl,
172438 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
172441 zCollist, p->zStateDb, pIter->zDataTbl,
172451 zCollist, p->zStateDb, pIter->zDataTbl,
172452 zCollist, pIter->zDataTbl,
172456 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
172464 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
172465 ||(pIter->eType==RBU_PK_NONE)
172466 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
172467 const char *zTbl = pIter->zTbl; /* Table this step applies to */
172470 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
172476 pIter->nCol = pIter->nTblCol;
172481 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
172483 /* Create the INSERT statement to write to the target PK b-tree */
172484 if( p->rc==SQLITE_OK ){
172485 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
172493 /* Create the DELETE statement to write to the target PK b-tree.
172496 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
172497 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
172504 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
172506 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
172511 rbuMPrintfExec(p, p->dbRbu,
172514 , p->zStateDb, pIter->zDataTbl
172515 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
172516 , pIter->zDataTbl
172519 rbuMPrintfExec(p, p->dbMain,
172539 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
172540 rbuMPrintfExec(p, p->dbMain,
172553 if( p->rc==SQLITE_OK ){
172558 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
172564 pIter->zDataTbl, zLimit
172578 return p->rc;
172583 ** be used to update the imposter table for the main table b-tree of the
172607 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
172609 if( strcmp(pUp->zMask, zMask)==0 ){
172610 *pp = pUp->pNext;
172611 pUp->pNext = pIter->pRbuUpdate;
172612 pIter->pRbuUpdate = pUp;
172613 *ppStmt = pUp->pUpdate;
172618 assert( pUp==0 || pUp->pNext==0 );
172621 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
172623 sqlite3_finalize(pUp->pUpdate);
172624 pUp->pUpdate = 0;
172626 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
172634 pUp->zMask = (char*)&pUp[1];
172635 memcpy(pUp->zMask, zMask, pIter->nTblCol);
172636 pUp->pNext = pIter->pRbuUpdate;
172637 pIter->pRbuUpdate = pUp;
172642 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
172644 zPrefix, pIter->zTbl, zSet, zWhere
172646 p->rc = prepareFreeAndCollectError(
172647 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
172649 *ppStmt = pUp->pUpdate;
172655 return p->rc;
172664 if( p->rc==SQLITE_OK ){
172666 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
172667 if( p->rc ){
172668 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
172681 sqlite3_free(p->zTbl);
172682 sqlite3_free(p->zIdx);
172705 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
172706 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
172711 pRet->eStage = sqlite3_column_int(pStmt, 1);
172712 if( pRet->eStage!=RBU_STAGE_OAL
172713 && pRet->eStage!=RBU_STAGE_MOVE
172714 && pRet->eStage!=RBU_STAGE_CKPT
172716 p->rc = SQLITE_CORRUPT;
172721 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
172725 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
172729 pRet->nRow = sqlite3_column_int(pStmt, 1);
172733 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
172737 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
172741 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
172745 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
172749 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
172760 p->rc = rc;
172770 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
172771 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
172774 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
172776 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
172777 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
172778 if( p->zState==0 ){
172779 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
172780 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
172786 if( p->zState ){
172787 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
172788 memcpy(p->zStateDb, "stat", 4);
172790 memcpy(p->zStateDb, "main", 4);
172794 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
172795 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
172800 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
172804 if( p->rc==SQLITE_OK ){
172808 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
172811 if( p->rc==SQLITE_OK
172818 if( p->rc==SQLITE_OK ) p->rc = rc2;
172820 if( p->rc==SQLITE_OK && bOk==0 ){
172821 p->rc = SQLITE_ERROR;
172822 p->zErrmsg = sqlite3_mprintf("invalid state database");
172825 if( p->rc==SQLITE_OK ){
172826 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
172832 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
172835 p->nRbu = 0;
172836 p->pRbuFd = 0;
172837 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
172838 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
172839 if( p->eStage>=RBU_STAGE_MOVE ){
172844 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
172848 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
172851 p->eStage = 0;
172852 if( p->rc==SQLITE_OK && p->dbMain==0 ){
172854 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
172855 }else if( p->pRbuFd->pWalFd ){
172857 p->pRbuFd->bNolock = 0;
172858 sqlite3_close(p->dbRbu);
172859 sqlite3_close(p->dbMain);
172860 p->dbMain = 0;
172861 p->dbRbu = 0;
172865 p->rc = SQLITE_ERROR;
172866 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
172870 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
172871 zExtra = &p->zRbu[5];
172878 zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
172879 sqlite3_db_filename(p->dbRbu, "main"),
172884 p->rc = SQLITE_NOMEM;
172887 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
172892 if( p->rc==SQLITE_OK ){
172893 p->rc = sqlite3_create_function(p->dbMain,
172894 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
172898 if( p->rc==SQLITE_OK ){
172899 p->rc = sqlite3_create_function(p->dbMain,
172904 if( p->rc==SQLITE_OK ){
172905 p->rc = sqlite3_create_function(p->dbRbu,
172906 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
172910 if( p->rc==SQLITE_OK ){
172911 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
172913 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
172918 if( p->rc==SQLITE_OK ){
172919 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
172922 if( p->rc==SQLITE_NOTFOUND ){
172923 p->rc = SQLITE_ERROR;
172924 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
172930 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
172932 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
172938 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
172943 ** test.db-journal => test.nal
172944 ** test.db-wal => test.wal
172945 ** test.db-shm => test.shm
172946 ** test.db-mj7f3319fa => test.9fa
172956 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
172957 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
172963 ** Return the current wal-index header checksum for the target database
172964 ** as a 64-bit integer.
172966 ** The checksum is store in the first page of xShmMap memory as an 8-byte
172971 if( p->rc==SQLITE_OK ){
172972 sqlite3_file *pDb = p->pTargetFd->pReal;
172974 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
172975 if( p->rc==SQLITE_OK ){
172987 ** (wal frame -> db page) copy operations required to checkpoint the
172989 ** perform the copy operations directly on the file-system.
172992 ** being resumed. In this case, if the checksum of the wal-index-header
173001 ** recovered. Running a read-statement here to ensure that doing so
173004 p->eStage = 0;
173005 if( p->rc==SQLITE_OK ){
173006 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
173018 ** * Attempts to read from the *-wal file or write to the database file
173025 ** no-ops. These locks will not be released until the connection
173033 ** array populated with a set of (frame -> page) mappings. Because the
173038 if( p->rc==SQLITE_OK ){
173040 p->eStage = RBU_STAGE_CAPTURE;
173041 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
173042 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
173045 if( p->rc==SQLITE_OK && p->nFrame>0 ){
173046 p->eStage = RBU_STAGE_CKPT;
173047 p->nStep = (pState ? pState->nRow : 0);
173048 p->aBuf = rbuMalloc(p, p->pgsz);
173049 p->iWalCksum = rbuShmChecksum(p);
173052 if( p->rc==SQLITE_OK ){
173053 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
173054 p->rc = SQLITE_DONE;
173055 p->eStage = RBU_STAGE_DONE;
173058 sqlite3_file *pDb = p->pTargetFd->pReal;
173059 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
173060 assert( p->nPagePerSector==0 );
173061 nSectorSize = pDb->pMethods->xSectorSize(pDb);
173062 if( nSectorSize>p->pgsz ){
173063 p->nPagePerSector = nSectorSize / p->pgsz;
173065 p->nPagePerSector = 1;
173072 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
173086 if( pRbu->mLock!=mReq ){
173087 pRbu->rc = SQLITE_BUSY;
173091 pRbu->pgsz = iAmt;
173092 if( pRbu->nFrame==pRbu->nFrameAlloc ){
173093 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
173095 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
173097 pRbu->aFrame = aNew;
173098 pRbu->nFrameAlloc = nNew;
173101 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
173102 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
173103 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
173104 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
173105 pRbu->nFrame++;
173115 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
173125 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
173126 sqlite3_file *pDb = p->pTargetFd->pReal;
173129 assert( p->rc==SQLITE_OK );
173130 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
173131 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
173132 if( p->rc ) return;
173134 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
173135 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
173143 sqlite3_file *pReal = p->pTargetFd->pReal;
173144 assert( p->rc==SQLITE_OK );
173145 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
173146 if( p->rc==SQLITE_OK ){
173147 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
173156 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
173165 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
173177 ** on the database file. This proc moves the *-oal file to the *-wal path,
173178 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
173183 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
173189 zMove = sqlite3_db_filename(p->dbRbu, "main");
173191 zOal = sqlite3_mprintf("%s-oal", zMove);
173192 zWal = sqlite3_mprintf("%s-wal", zMove);
173194 assert( p->eStage==RBU_STAGE_MOVE );
173195 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
173197 p->rc = SQLITE_NOMEM;
173199 /* Move the *-oal file to *-wal. At this point connection p->db is
173204 ** lock is obtained here before the *-oal is moved to *-wal.
173207 if( p->rc==SQLITE_OK ){
173211 /* Re-open the databases. */
173212 rbuObjIterFinalize(&p->objiter);
173213 sqlite3_close(p->dbRbu);
173214 sqlite3_close(p->dbMain);
173215 p->dbMain = 0;
173216 p->dbRbu = 0;
173228 p->rc = SQLITE_OK;
173230 p->rc = SQLITE_IOERR;
173234 p->rc = SQLITE_IOERR_NOMEM;
173238 p->rc = SQLITE_IOERR_NOMEM;
173242 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
173245 if( p->rc==SQLITE_OK ){
173258 ** (p->objiter.pSelect) currently points to a valid row. This function
173274 int iCol = p->objiter.nCol; /* Index of rbu_control column */
173277 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
173279 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
173291 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
173293 p->rc = SQLITE_NOMEM;
173330 RbuObjIter *pIter = &p->objiter;
173335 assert( p->rc==SQLITE_OK );
173336 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
173346 p->nPhaseOneStep -= p->objiter.nIndex;
173350 pWriter = pIter->pDelete;
173352 pWriter = pIter->pInsert;
173355 for(i=0; i<pIter->nCol; i++){
173356 /* If this is an INSERT into a table b-tree and the table has an
173360 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
173361 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
173363 p->rc = SQLITE_MISMATCH;
173364 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
173368 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
173372 pVal = sqlite3_column_value(pIter->pSelect, i);
173373 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
173374 if( p->rc ) return;
173376 if( pIter->zIdx==0 ){
173377 if( pIter->eType==RBU_PK_VTAB
173378 || pIter->eType==RBU_PK_NONE
173379 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
173386 ** Hence column_value(pIter->nCol+1).
173388 assertColumnName(pIter->pSelect, pIter->nCol+1,
173391 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
173392 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
173395 if( p->rc==SQLITE_OK ){
173397 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
173404 ** The object-iterator (p->objiter) currently points to a valid object,
173405 ** and the input cursor (p->objiter.pSelect) currently points to a valid
173413 RbuObjIter *pIter = &p->objiter;
173422 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
173424 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
173428 if( pIter->zIdx==0 ){
173429 p->nPhaseOneStep += p->objiter.nIndex;
173432 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
173441 p->nPhaseOneStep -= p->objiter.nIndex;
173445 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
173446 char c = zMask[pIter->aiSrcOrder[i]];
173447 pVal = sqlite3_column_value(pIter->pSelect, i);
173448 if( pIter->abTblPk[i] || c!='.' ){
173449 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
173452 if( p->rc==SQLITE_OK
173453 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
173456 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
173457 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
173458 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
173460 if( p->rc==SQLITE_OK ){
173462 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
173467 return p->rc;
173471 ** Increment the schema cookie of the main database opened by p->dbMain.
173474 ** opened by p->dbMain to one more than the schema cookie of the main
173475 ** db opened by p->dbRbu.
173478 if( p->rc==SQLITE_OK ){
173479 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
173483 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
173486 if( p->rc==SQLITE_OK ){
173497 if( p->rc==SQLITE_OK ){
173498 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
173509 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
173511 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
173514 assert( p->zErrmsg==0 );
173515 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
173527 p->zStateDb,
173529 RBU_STATE_TBL, p->objiter.zTbl,
173530 RBU_STATE_IDX, p->objiter.zIdx,
173531 RBU_STATE_ROW, p->nStep,
173532 RBU_STATE_PROGRESS, p->nProgress,
173533 RBU_STATE_CKPT, p->iWalCksum,
173534 RBU_STATE_COOKIE, (i64)pFd->iCookie,
173535 RBU_STATE_OALSZ, p->iOalSz,
173536 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep
173545 if( rc!=SQLITE_OK ) p->rc = rc;
173552 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
173568 if( p->rc==SQLITE_OK ){
173570 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
173573 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
173574 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
173592 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
173593 if( p->rc==SQLITE_OK ){
173594 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
173601 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
173603 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
173606 if( p->rc!=SQLITE_OK ) return;
173608 if( p->rc==SQLITE_OK ){
173609 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
173614 if( p->rc==SQLITE_OK ){
173615 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
173620 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
173622 for(i=0; i<5; i++){
173626 p->rc = sqlite3_reset(pInsert);
173628 if( p->rc==SQLITE_OK ){
173629 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
173641 switch( p->eStage ){
173643 RbuObjIter *pIter = &p->objiter;
173647 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
173653 while( p->rc==SQLITE_OK && pIter->zTbl ){
173655 if( pIter->bCleanup ){
173659 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
173660 rbuMPrintfExec(p, p->dbRbu,
173661 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
173668 if( p->rc==SQLITE_OK ){
173669 int rc = sqlite3_step(pIter->pSelect);
173671 p->nProgress++;
173672 p->nStep++;
173675 p->rc = sqlite3_reset(pIter->pSelect);
173676 p->nStep = 0;
173683 if( p->rc==SQLITE_OK ){
173684 assert( pIter->zTbl==0 );
173687 if( p->rc==SQLITE_OK ){
173688 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
173690 if( p->rc==SQLITE_OK ){
173691 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
173693 p->eStage = RBU_STAGE_MOVE;
173699 if( p->rc==SQLITE_OK ){
173701 p->nProgress++;
173707 if( p->rc==SQLITE_OK ){
173708 if( p->nStep>=p->nFrame ){
173709 sqlite3_file *pDb = p->pTargetFd->pReal;
173712 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
173715 if( p->rc==SQLITE_OK ){
173717 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
173718 if( p->rc==SQLITE_OK ){
173719 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
173723 if( p->rc==SQLITE_OK ){
173724 p->eStage = RBU_STAGE_DONE;
173725 p->rc = SQLITE_DONE;
173732 ** However, if the sector-size is larger than the page-size, and the
173740 RbuFrame *pFrame = &p->aFrame[p->nStep];
173741 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
173743 p->nStep++;
173744 }while( p->nStep<p->nFrame
173745 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
173746 && p->rc==SQLITE_OK
173749 p->nProgress++;
173757 return p->rc;
173764 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
173777 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
173785 assert( p->rc==SQLITE_OK );
173786 if( pState->zTbl ){
173787 RbuObjIter *pIter = &p->objiter;
173790 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
173791 || rbuStrCompare(pIter->zIdx, pState->zIdx)
173792 || rbuStrCompare(pIter->zTbl, pState->zTbl)
173797 if( rc==SQLITE_OK && !pIter->zTbl ){
173799 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
173803 p->nStep = pState->nRow;
173804 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
173807 p->rc = rc;
173812 ** If there is a "*-oal" file in the file-system corresponding to the
173813 ** target database in the file-system, delete it. If an error occurs,
173817 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
173820 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
173821 pVfs->xDelete(pVfs, zOal, 0);
173836 assert( p->rc==SQLITE_OK );
173839 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
173840 if( p->rc==SQLITE_OK ){
173843 p->zVfsName = pVfs->zName;
173852 if( p->zVfsName ){
173853 sqlite3rbu_destroy_vfs(p->zVfsName);
173854 p->zVfsName = 0;
173859 ** This user-defined SQL function is invoked with a single argument - the
173875 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
173880 sqlite3_result_error(pCtx, zErrmsg, -1);
173890 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
173913 if( p->rc==SQLITE_OK ){
173917 p->nPhaseOneStep = -1;
173919 p->rc = sqlite3_create_function(p->dbRbu,
173924 ** occurs, nPhaseOneStep will be left set to -1. */
173925 if( p->rc==SQLITE_OK ){
173926 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
173930 if( p->rc==SQLITE_OK ){
173934 p->rc = sqlite3_finalize(pStmt);
173937 if( p->rc==SQLITE_OK && bExists ){
173938 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
173942 if( p->rc==SQLITE_OK ){
173944 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
173946 p->rc = sqlite3_finalize(pStmt);
173972 if( p->rc==SQLITE_OK ){
173976 p->zTarget = pCsr;
173977 memcpy(p->zTarget, zTarget, nTarget+1);
173980 p->zRbu = pCsr;
173981 memcpy(p->zRbu, zRbu, nRbu+1);
173984 p->zState = rbuMPrintf(p, "%s", zState);
173989 ** to be a wal-mode db. But, this may have happened due to an earlier
174000 if( p->rc==SQLITE_OK ){
174002 assert( pState || p->rc!=SQLITE_OK );
174003 if( p->rc==SQLITE_OK ){
174005 if( pState->eStage==0 ){
174008 p->eStage = RBU_STAGE_OAL;
174010 p->eStage = pState->eStage;
174011 p->nPhaseOneStep = pState->nPhaseOneStep;
174013 p->nProgress = pState->nProgress;
174014 p->iOalSz = pState->iOalSz;
174017 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
174019 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
174020 if( p->eStage==RBU_STAGE_OAL ){
174021 p->rc = SQLITE_ERROR;
174022 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
174023 }else if( p->eStage==RBU_STAGE_MOVE ){
174024 p->eStage = RBU_STAGE_CKPT;
174025 p->nStep = 0;
174029 if( p->rc==SQLITE_OK
174030 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
174031 && pState->eStage!=0
174033 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
174034 if( pFd->iCookie!=pState->iCookie ){
174035 /* At this point (pTargetFd->iCookie) contains the value of the
174036 ** change-counter cookie (the thing that gets incremented when a
174039 p->rc = SQLITE_BUSY;
174040 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
174046 if( p->rc==SQLITE_OK ){
174047 if( p->eStage==RBU_STAGE_OAL ){
174048 sqlite3 *db = p->dbMain;
174049 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
174052 if( p->rc==SQLITE_OK ){
174053 p->rc = rbuObjIterFirst(p, &p->objiter);
174058 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
174059 p->rc = SQLITE_DONE;
174060 p->eStage = RBU_STAGE_DONE;
174062 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
174067 /* Open transactions both databases. The *-oal file is opened or
174069 if( p->rc==SQLITE_OK ){
174070 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
174076 if( p->rc==SQLITE_OK ){
174079 p->rc = sqlite3_exec(
174080 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
174084 if( p->rc==SQLITE_OK ){
174088 }else if( p->eStage==RBU_STAGE_MOVE ){
174089 /* no-op */
174090 }else if( p->eStage==RBU_STAGE_CKPT ){
174092 }else if( p->eStage==RBU_STAGE_DONE ){
174093 p->rc = SQLITE_DONE;
174095 p->rc = SQLITE_CORRUPT;
174114 pRet->rc = SQLITE_MISUSE;
174128 /* TODO: Check that zTarget and zRbu are non-NULL */
174140 /* TODO: Check that both arguments are non-NULL */
174150 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
174159 ** the pattern "rbu_imp_[0-9]*".
174162 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
174164 size_t nErrmsg = strlen(p->zErrmsg);
174165 for(i=0; i<(nErrmsg-8); i++){
174166 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
174168 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
174169 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
174170 nErrmsg -= nDel;
174183 /* Commit the transaction to the *-oal file. */
174184 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
174185 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
174189 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
174190 sqlite3_file *pDb = p->pTargetFd->pReal;
174191 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
174194 rbuSaveState(p, p->eStage);
174196 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
174197 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
174201 rbuObjIterFinalize(&p->objiter);
174208 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
174209 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
174210 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
174214 sqlite3_close(p->dbRbu);
174215 sqlite3_close(p->dbMain);
174217 sqlite3_free(p->aBuf);
174218 sqlite3_free(p->aFrame);
174221 rc = p->rc;
174223 *pzErrmsg = p->zErrmsg;
174225 sqlite3_free(p->zErrmsg);
174227 sqlite3_free(p->zState);
174237 ** Return the total number of key-value operations (inserts, deletes or
174242 return pRbu->nProgress;
174251 switch( p->eStage ){
174253 if( p->nPhaseOneStep>0 ){
174254 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
174256 *pnOne = -1;
174268 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
174293 assert( RBU_STAGE_DONE==5 );
174299 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
174302 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
174303 assert( p->eStage==RBU_STAGE_OAL
174304 || p->eStage==RBU_STAGE_MOVE
174305 || p->eStage==RBU_STAGE_CKPT
174306 || p->eStage==RBU_STAGE_DONE
174308 return aRes[p->eStage];
174313 int rc = p->rc;
174316 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
174317 if( p->eStage==RBU_STAGE_OAL ){
174319 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
174323 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
174324 sqlite3_file *pDb = p->pTargetFd->pReal;
174325 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
174328 p->rc = rc;
174329 rbuSaveState(p, p->eStage);
174330 rc = p->rc;
174332 if( p->eStage==RBU_STAGE_OAL ){
174334 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
174335 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
174336 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
174339 p->rc = rc;
174348 ** written, the value of the change-counter cookie is stored in
174349 ** rbu_file.iCookie. Similarly, the value of the "write-version"
174359 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
174363 ** 3a. If xAccess() is called to check if there exists a *-wal file
174365 ** stage (preparing the *-oal file), the following special handling
174368 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
174371 ** * if the *-wal file does not exist, set the output parameter to
174372 ** non-zero (to tell SQLite that it does exist) anyway.
174374 ** Then, when xOpen() is called to open the *-wal file associated with
174375 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
174376 ** file, the rbu vfs opens the corresponding *-oal file instead.
174378 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
174380 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
174381 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
174388 ** from automatically checkpointing a *-wal (or *-oal) file from within
174397 ** locks are no-ops (so that once obtained, these locks are never
174403 if( p->pRbu ){
174404 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
174407 if( (1<<i) & p->pRbu->mLock ){
174408 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
174411 p->pRbu->mLock = 0;
174424 for(i=0; i<p->nShm; i++){
174425 sqlite3_free(p->apShm[i]);
174427 sqlite3_free(p->apShm);
174428 p->apShm = 0;
174429 sqlite3_free(p->zDel);
174431 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
174433 sqlite3_mutex_enter(p->pRbuVfs->mutex);
174434 for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
174435 *pp = p->pMainNext;
174436 sqlite3_mutex_leave(p->pRbuVfs->mutex);
174438 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
174442 rc = p->pReal->pMethods->xClose(p->pReal);
174448 ** Read and return an unsigned 32-bit big-endian integer from the buffer
174459 ** Write an unsigned 32-bit value in big-endian format to the supplied
174475 ** Read data from an rbuVfs-file.
174484 sqlite3rbu *pRbu = p->pRbu;
174487 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
174488 assert( p->openFlags & SQLITE_OPEN_WAL );
174489 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
174491 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
174492 && (p->openFlags & SQLITE_OPEN_WAL)
174493 && iOfst>=pRbu->iOalSz
174498 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
174503 ** SQLite will not check for a *-wal file. */
174506 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
174507 && pRbu->rc==SQLITE_OK
174509 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
174510 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
174518 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
174521 memset(&aBuf[100], 0, iAmt-100);
174529 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
174533 p->iCookie = rbuGetU32(&pBuf[24]);
174534 p->iWriteVer = pBuf[19];
174541 ** Write data to an rbuVfs-file.
174550 sqlite3rbu *pRbu = p->pRbu;
174553 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
174554 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
174555 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
174557 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
174558 && (p->openFlags & SQLITE_OPEN_WAL)
174559 && iOfst>=pRbu->iOalSz
174561 pRbu->iOalSz = iAmt + iOfst;
174563 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
174564 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
174568 p->iCookie = rbuGetU32(&pBuf[24]);
174569 p->iWriteVer = pBuf[19];
174576 ** Truncate an rbuVfs-file.
174580 return p->pReal->pMethods->xTruncate(p->pReal, size);
174584 ** Sync an rbuVfs-file.
174588 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
174589 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
174594 return p->pReal->pMethods->xSync(p->pReal, flags);
174598 ** Return the current file-size of an rbuVfs-file.
174603 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
174607 ** check for the existance of a *-wal file. rbuVfsRead() contains
174610 && p->pRbu && rbuIsVacuum(p->pRbu)
174611 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
174619 ** Lock an rbuVfs-file.
174623 sqlite3rbu *pRbu = p->pRbu;
174626 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
174628 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
174634 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
174641 ** Unlock an rbuVfs-file.
174645 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
174649 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
174653 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
174657 ** File control method. For custom operations on an rbuVfs-file.
174661 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
174664 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
174665 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
174671 ** one is found, this vfs will operate in pass-through mode. The lower
174673 rc = xControl(p->pReal, op, pArg);
174679 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
174682 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
174684 pRbu->pTargetFd = p;
174685 p->pRbu = pRbu;
174686 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
174694 pRbu->nRbu++;
174695 pRbu->pRbuFd = p;
174696 p->bNolock = 1;
174699 rc = xControl(p->pReal, op, pArg);
174701 rbu_vfs *pRbuVfs = p->pRbuVfs;
174703 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
174712 ** Return the sector-size in bytes for an rbuVfs-file.
174716 return p->pReal->pMethods->xSectorSize(p->pReal);
174720 ** Return the device characteristic flags supported by an rbuVfs-file.
174724 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
174728 ** Take or release a shared-memory lock.
174732 sqlite3rbu *pRbu = p->pRbu;
174739 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
174740 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
174749 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
174756 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
174758 pRbu->mLock |= (1 << ofst);
174767 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
174778 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
174781 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
174783 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
174785 if( iRegion<=p->nShm ){
174787 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
174791 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
174792 p->apShm = apNew;
174793 p->nShm = iRegion+1;
174797 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
174803 p->apShm[iRegion] = pNew;
174808 *pp = p->apShm[iRegion];
174813 assert( p->apShm==0 );
174814 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
174825 p->pReal->pMethods->xShmBarrier(p->pReal);
174834 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
174836 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
174838 /* no-op */
174842 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
174850 ** file-handle opened by the same database connection on the corresponding
174855 sqlite3_mutex_enter(pRbuVfs->mutex);
174856 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
174857 sqlite3_mutex_leave(pRbuVfs->mutex);
174864 ** the name of the *-wal file this db connection will use. SQLite
174866 ** or xOpen() to operate on the *-wal file.
174875 odd = 1 - odd;
174919 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
174926 pFd->pReal = (sqlite3_file*)&pFd[1];
174927 pFd->pRbuVfs = pRbuVfs;
174928 pFd->openFlags = flags;
174932 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
174933 ** the name of the *-wal file this db connection will use. SQLite
174935 ** or xOpen() to operate on the *-wal file. */
174936 pFd->zWal = rbuMainToWal(zName, flags);
174941 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
174942 /* This call is to open a *-wal file. Intead, open the *-oal. This
174949 if( rbuIsVacuum(pDb->pRbu) ){
174950 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
174957 zCopy[nCopy-3] = 'o';
174960 zOpen = (const char*)(pFd->zDel = zCopy);
174964 pFd->pRbu = pDb->pRbu;
174966 pDb->pWalFd = pFd;
174981 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
174983 if( pFd->pReal->pMethods ){
174987 pFile->pMethods = &rbuvfs_io_methods;
174989 sqlite3_mutex_enter(pRbuVfs->mutex);
174990 pFd->pMainNext = pRbuVfs->pMain;
174991 pRbuVfs->pMain = pFd;
174992 sqlite3_mutex_leave(pRbuVfs->mutex);
174995 sqlite3_free(pFd->zDel);
175005 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175006 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
175020 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
175023 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
175025 /* If this call is to check if a *-wal file associated with an RBU target
175029 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
175034 ** b) if the *-wal file does not exist, claim that it does anyway,
175036 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
175041 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
175064 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175065 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
175073 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175074 return pRealVfs->xDlOpen(pRealVfs, zPath);
175079 ** utf-8 string describing the most recent error encountered associated
175083 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175084 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
175095 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175096 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
175103 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175104 pRealVfs->xDlClose(pRealVfs, pHandle);
175113 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175114 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
175122 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175123 return pRealVfs->xSleep(pRealVfs, nMicro);
175130 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
175131 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
175135 ** No-op.
175147 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
175148 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
175155 ** Create an RBU VFS named zName that accesses the underlying file-system
175156 ** via existing VFS zParent. The new object is registered as a non-default
175209 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
175210 pNew->base.mxPathname = pParent->mxPathname;
175211 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
175212 pNew->pRealVfs = pParent;
175213 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
175217 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
175218 if( pNew->mutex==0 ){
175221 rc = sqlite3_vfs_register(&pNew->base, 0);
175226 sqlite3_mutex_free(pNew->mutex);
175255 ** The dbstat virtual table is used to extract low-level formatting
175272 ** root-node of the b-tree structure to each page. The value of the
175273 ** root-node path is '/'.
175275 ** The value of the path for the left-most child page of the root of
175276 ** a b-tree is '/000/'. (Btrees store content ordered from left to right
175278 ** The next to left-most child of the root page is
175279 ** '/001', and so on, each sibling page identified by a 3-digit hex
175280 ** value. The children of the 451st left-most sibling have paths such
175284 ** six-digit hexadecimal value to the path to the cell they are linked
175286 ** the left-most cell of the 450th child of the root page are identified
175295 ** sort-order than its child page:
175297 ** '/1c2/000/' // Left-most child of 451st child of root
175341 u32 iRightChildPg; /* Right-child page number (or 0) */
175411 pTab->db = db;
175412 pTab->iDb = iDb;
175428 ** There is no "best-index". This virtual table always does a linear
175437 pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */
175444 for(i=0; i<pIdxInfo->nConstraint; i++){
175445 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
175446 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
175447 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
175448 pIdxInfo->idxNum = 1;
175449 pIdxInfo->estimatedCost = 1.0;
175450 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
175451 pIdxInfo->aConstraintUsage[i].omit = 1;
175460 if( ( pIdxInfo->nOrderBy==1
175461 && pIdxInfo->aOrderBy[0].iColumn==0
175462 && pIdxInfo->aOrderBy[0].desc==0
175464 ( pIdxInfo->nOrderBy==2
175465 && pIdxInfo->aOrderBy[0].iColumn==0
175466 && pIdxInfo->aOrderBy[0].desc==0
175467 && pIdxInfo->aOrderBy[1].iColumn==1
175468 && pIdxInfo->aOrderBy[1].desc==0
175471 pIdxInfo->orderByConsumed = 1;
175489 pCsr->base.pVtab = pVTab;
175490 pCsr->iDb = pTab->iDb;
175499 if( p->aCell ){
175500 for(i=0; i<p->nCell; i++){
175501 sqlite3_free(p->aCell[i].aOvfl);
175503 sqlite3_free(p->aCell);
175505 sqlite3PagerUnref(p->pPg);
175506 sqlite3_free(p->zPath);
175512 sqlite3_reset(pCsr->pStmt);
175513 for(i=0; i<ArraySize(pCsr->aPage); i++){
175514 statClearPage(&pCsr->aPage[i]);
175516 pCsr->iPage = 0;
175517 sqlite3_free(pCsr->zPath);
175518 pCsr->zPath = 0;
175519 pCsr->isEof = 0;
175528 sqlite3_finalize(pCsr->pStmt);
175544 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
175545 nMaxLocal = nUsable - 35;
175547 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
175548 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
175551 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
175563 u8 *aData = sqlite3PagerGetData(p->pPg);
175564 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
175566 p->flags = aHdr[0];
175567 p->nCell = get2byte(&aHdr[3]);
175568 p->nMxPayload = 0;
175570 isLeaf = (p->flags==0x0A || p->flags==0x0D);
175571 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
175573 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
175580 p->nUnused = nUnused;
175581 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
175584 if( p->nCell ){
175589 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
175591 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
175592 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
175593 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
175595 for(i=0; i<p->nCell; i++){
175596 StatCell *pCell = &p->aCell[i];
175600 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
175603 if( p->flags==0x05 ){
175609 if( p->flags==0x0D ){
175613 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
175614 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
175615 pCell->nLocal = nLocal;
175618 assert( nLocal<=(nUsable-35) );
175621 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
175622 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
175623 pCell->nOvfl = nOvfl;
175624 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
175625 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
175626 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
175629 u32 iPrev = pCell->aOvfl[j-1];
175636 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
175648 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
175649 ** the current value of pCsr->iPageno.
175652 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
175653 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
175659 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
175660 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
175666 x[0] = pCsr->iPageno;
175667 if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
175668 pCsr->iOffset = x[0];
175669 pCsr->szPage = (int)x[1];
175681 StatTable *pTab = (StatTable *)pCursor->pVtab;
175682 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
175685 sqlite3_free(pCsr->zPath);
175686 pCsr->zPath = 0;
175689 if( pCsr->aPage[0].pPg==0 ){
175690 rc = sqlite3_step(pCsr->pStmt);
175693 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
175696 pCsr->isEof = 1;
175697 return sqlite3_reset(pCsr->pStmt);
175699 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
175700 pCsr->aPage[0].iPgno = iRoot;
175701 pCsr->aPage[0].iCell = 0;
175702 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
175703 pCsr->iPage = 0;
175706 pCsr->isEof = 1;
175707 return sqlite3_reset(pCsr->pStmt);
175712 StatPage *p = &pCsr->aPage[pCsr->iPage];
175714 while( p->iCell<p->nCell ){
175715 StatCell *pCell = &p->aCell[p->iCell];
175716 if( pCell->iOvfl<pCell->nOvfl ){
175719 nUsable = sqlite3BtreeGetPageSize(pBt) -
175722 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
175723 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
175724 pCsr->zPagetype = "overflow";
175725 pCsr->nCell = 0;
175726 pCsr->nMxPayload = 0;
175727 pCsr->zPath = z = sqlite3_mprintf(
175728 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
175730 if( pCell->iOvfl<pCell->nOvfl-1 ){
175731 pCsr->nUnused = 0;
175732 pCsr->nPayload = nUsable - 4;
175734 pCsr->nPayload = pCell->nLastOvfl;
175735 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
175737 pCell->iOvfl++;
175741 if( p->iRightChildPg ) break;
175742 p->iCell++;
175745 if( !p->iRightChildPg || p->iCell>p->nCell ){
175747 if( pCsr->iPage==0 ) return statNext(pCursor);
175748 pCsr->iPage--;
175751 pCsr->iPage++;
175752 assert( p==&pCsr->aPage[pCsr->iPage-1] );
175754 if( p->iCell==p->nCell ){
175755 p[1].iPgno = p->iRightChildPg;
175757 p[1].iPgno = p->aCell[p->iCell].iChildPg;
175761 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
175762 p->iCell++;
175772 StatPage *p = &pCsr->aPage[pCsr->iPage];
175773 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
175774 pCsr->iPageno = p->iPgno;
175780 switch( p->flags ){
175783 pCsr->zPagetype = "internal";
175787 pCsr->zPagetype = "leaf";
175790 pCsr->zPagetype = "corrupted";
175793 pCsr->nCell = p->nCell;
175794 pCsr->nUnused = p->nUnused;
175795 pCsr->nMxPayload = p->nMxPayload;
175796 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
175799 for(i=0; i<p->nCell; i++){
175800 nPayload += p->aCell[i].nLocal;
175802 pCsr->nPayload = nPayload;
175811 return pCsr->isEof;
175820 StatTable *pTab = (StatTable*)(pCursor->pVtab);
175827 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
175828 if( pCsr->iDb<0 ){
175829 sqlite3_free(pCursor->pVtab->zErrMsg);
175830 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
175831 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
175834 pCsr->iDb = pTab->iDb;
175837 sqlite3_finalize(pCsr->pStmt);
175838 pCsr->pStmt = 0;
175839 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
175845 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster);
175849 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
175867 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
175870 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
175873 sqlite3_result_int64(ctx, pCsr->iPageno);
175876 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
175879 sqlite3_result_int(ctx, pCsr->nCell);
175881 case 5: /* payload */
175882 sqlite3_result_int(ctx, pCsr->nPayload);
175885 sqlite3_result_int(ctx, pCsr->nUnused);
175888 sqlite3_result_int(ctx, pCsr->nMxPayload);
175891 sqlite3_result_int64(ctx, pCsr->iOffset);
175894 sqlite3_result_int(ctx, pCsr->szPage);
175898 int iDb = pCsr->iDb;
175899 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
175908 *pRowid = pCsr->iPageno;
175923 statOpen, /* xOpen - open a cursor */
175924 statClose, /* xClose - close a cursor */
175925 statFilter, /* xFilter - configure scan constraints */
175926 statNext, /* xNext - advance a cursor */
175927 statEof, /* xEof - check for end of scan */
175928 statColumn, /* xColumn - read data */
175929 statRowid, /* xRowid - read data */
175993 int bAutoAttach; /* True to auto-attach tables */
175994 int rc; /* Non-zero if an error has occurred */
176056 ** The data associated with each hash-table entry is a structure containing
176076 ** change-set binary format, and so must be architecture independent.
176078 ** Unlike the SQLite database record format, each field is self-contained -
176096 ** UTF-8). Followed by a buffer containing the UTF-8 representation
176104 ** An 8-byte big-endian integer value.
176107 ** An 8-byte big-endian IEEE 754-2008 real value.
176124 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
176129 ** 1 byte: The "indirect-change" flag.
176166 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
176171 ** 1 byte: The "indirect-change" flag.
176203 SessionChange *pNext; /* For hash-table collisions */
176229 /* Load an unaligned and unsigned 32-bit integer */
176233 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
176244 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
176252 aBuf[5] = (i>>16) & 0xFF;
176261 ** If it is non-NULL, the serialized form of the value is written to
176267 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
176271 u8 *aBuf, /* If non-NULL, write serialized value here */
176291 /* TODO: SQLite does something special to deal with mixed-endian
176345 ** as a series of unsigned integers. In order to calculate a hash-key value
176360 ** Append the hash of the 64-bit integer passed as the second argument to the
176361 ** hash-key value passed as the first. Return the new hash-key value.
176370 ** the hash-key value passed as the first. Return the new hash-key value.
176380 ** hash-key value passed as the first. Return the new hash-key value.
176387 ** This function may only be called from within a pre-update callback.
176408 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
176409 for(i=0; i<pTab->nCol; i++){
176410 if( pTab->abPK[i] ){
176416 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
176418 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
176452 *piHash = (h % pTab->nChange);
176476 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
176477 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
176489 for(i=0; i<pTab->nCol; i++){
176491 int isPK = pTab->abPK[i];
176539 for(iCol=0; iCol<pTab->nCol; iCol++){
176540 if( pTab->abPK[iCol] ){
176544 if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
176607 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
176618 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
176619 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
176674 for(i=0; i<pTab->nCol; i++){
176682 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
176683 if( pTab->abPK[i]==0 ) bRequired = 1;
176699 for(i=0; i<pTab->nCol; i++){
176708 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
176722 ** This function is only called from within a pre-update-hook callback.
176723 ** It determines if the current pre-update-hook change affects the same row
176725 ** if the pre-update-hook does not affect the same row as pChange, it returns
176732 int op /* Current pre-update operation */
176735 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
176738 for(iCol=0; iCol<pTab->nCol; iCol++){
176739 if( !pTab->abPK[iCol] ){
176752 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
176753 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
176755 /* assert( db->pPreUpdate->pUnpacked ); */
176756 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
176803 ** It is possible that a non-fatal OOM error occurs in this function. In
176804 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
176809 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
176812 int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
176816 if( pTab->nChange==0 ){
176823 for(i=0; i<pTab->nChange; i++){
176826 for(p=pTab->apChange[i]; p; p=pNext){
176827 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
176828 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
176829 pNext = p->pNext;
176830 p->pNext = apNew[iHash];
176835 sqlite3_free(pTab->apChange);
176836 pTab->nChange = nNew;
176837 pTab->apChange = apNew;
176851 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
176853 ** NULL) is set to point to an array of booleans - true if the corresponding
176879 u8 **pabPK /* OUT: Array of booleans - true for PK col */
176898 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
176935 abPK[i] = sqlite3_column_int(pStmt, 5);
176961 ** This function is only called from within a pre-update handler for a
176967 ** non-zero returned. Or, if no error occurs but the table has no primary
176968 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
176973 if( pTab->nCol==0 ){
176975 assert( pTab->azCol==0 || pTab->abPK==0 );
176976 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
176977 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
176979 if( pSession->rc==SQLITE_OK ){
176981 for(i=0; i<pTab->nCol; i++){
176983 pTab->abPK = abPK;
176989 return (pSession->rc || pTab->abPK==0);
176993 ** This function is only called from with a pre-update-hook reporting a
176998 ** to the changed-rows hash table associated with table pTab.
177009 if( pSession->rc ) return;
177016 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
177017 pSession->rc = SQLITE_SCHEMA;
177023 pSession->rc = SQLITE_NOMEM;
177027 /* Calculate the hash-key for this change. If the primary key of the row
177036 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
177049 pTab->nEntry++;
177053 for(i=0; i<pTab->nCol; i++){
177056 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
177058 }else if( pTab->abPK[i] ){
177059 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
177063 /* This may fail if SQLite value p contains a utf-16 string that must
177064 ** be converted to utf-8 and an OOM error occurs while doing so. */
177076 pChange->aRecord = (u8 *)&pChange[1];
177084 for(i=0; i<pTab->nCol; i++){
177087 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
177088 }else if( pTab->abPK[i] ){
177089 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
177091 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
177094 /* Add the change to the hash-table */
177095 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
177096 pChange->bIndirect = 1;
177098 pChange->nRecord = nByte;
177099 pChange->op = op;
177100 pChange->pNext = pTab->apChange[iHash];
177101 pTab->apChange[iHash] = pChange;
177103 }else if( pC->bIndirect ){
177106 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
177107 && pSession->bIndirect==0
177109 pC->bIndirect = 0;
177117 pSession->rc = rc;
177131 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
177132 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
177135 if( pRet==0 && pSession->bAutoAttach ){
177136 /* If there is a table-filter configured, invoke it. If it returns 0,
177138 if( pSession->xTableFilter==0
177139 || pSession->xTableFilter(pSession->pFilterCtx, zName)
177143 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
177144 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
177155 ** The 'pre-update' hook registered by this module with SQLite databases.
177169 assert( sqlite3_mutex_held(db->mutex) );
177171 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
177177 if( pSession->bEnable==0 ) continue;
177178 if( pSession->rc ) continue;
177179 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
177181 pSession->rc = sessionFindTable(pSession, zName, &pTab);
177183 assert( pSession->rc==SQLITE_OK );
177193 ** The pre-update hook implementations.
177209 ** Install the pre-update hooks on the session object passed as the only
177215 pSession->hook.pCtx = (void*)pSession->db;
177216 pSession->hook.xOld = sessionPreupdateOld;
177217 pSession->hook.xNew = sessionPreupdateNew;
177218 pSession->hook.xCount = sessionPreupdateCount;
177219 pSession->hook.xDepth = sessionPreupdateDepth;
177233 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
177238 *ppVal = sqlite3_column_value(p->pStmt, iVal);
177243 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
177257 pSession->hook.pCtx = (void*)pDiffCtx;
177258 pSession->hook.xOld = sessionDiffOld;
177259 pSession->hook.xNew = sessionDiffNew;
177260 pSession->hook.xCount = sessionDiffCount;
177261 pSession->hook.xDepth = sessionDiffDepth;
177343 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
177349 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
177351 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
177352 pDiffCtx->pStmt = pStmt;
177353 pDiffCtx->nOldOff = 0;
177373 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
177374 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
177381 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
177387 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
177390 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
177391 pDiffCtx->pStmt = pStmt;
177392 pDiffCtx->nOldOff = pTab->nCol;
177411 const char *zDb = pSession->zDb;
177412 int rc = pSession->rc;
177418 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
177422 sqlite3 *db = pSession->db;
177429 rc = pSession->rc;
177442 if( pTo->nCol!=nCol ){
177447 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
177448 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
177466 zExpr = sessionExprComparePK(pTo->nCol,
177467 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
177491 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
177515 pNew->db = db;
177516 pNew->zDb = (char *)&pNew[1];
177517 pNew->bEnable = 1;
177518 memcpy(pNew->zDb, zDb, nDb+1);
177526 pNew->pNext = pOld;
177535 ** of the changed-rows hash tables are also deleted.
177543 pNext = pTab->pNext;
177544 for(i=0; i<pTab->nChange; i++){
177547 for(p=pTab->apChange[i]; p; p=pNextChange){
177548 pNextChange = p->pNext;
177552 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
177553 sqlite3_free(pTab->apChange);
177562 sqlite3 *db = pSession->db;
177570 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
177572 *pp = (*pp)->pNext;
177580 ** associated hash-tables. */
177581 sessionDeleteTable(pSession->pTable);
177595 pSession->bAutoAttach = 1;
177596 pSession->pFilterCtx = pCtx;
177597 pSession->xTableFilter = xFilter;
177613 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
177616 pSession->bAutoAttach = 1;
177622 ** a no-op. Return early. */
177624 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
177625 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
177641 pTab->zName = (char *)&pTab[1];
177642 memcpy(pTab->zName, zName, nName+1);
177643 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
177649 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
177658 ** set *pRc to SQLITE_NOMEM and return non-zero.
177661 if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
177663 int nNew = p->nAlloc ? p->nAlloc : 128;
177666 }while( nNew<(p->nBuf+nByte) );
177668 aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
177672 p->aBuf = aNew;
177673 p->nAlloc = nNew;
177683 ** This function is a no-op if *pRc is non-zero when it is called.
177694 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
177695 p->nBuf += nByte;
177703 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177711 p->aBuf[p->nBuf++] = v;
177716 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177724 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
177729 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177742 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
177743 p->nBuf += nBlob;
177748 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177750 ** up to (but not including) the nul-terminator are written to the buffer.
177762 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
177763 p->nBuf += nStr;
177768 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177770 ** to the buffer. No nul-terminator is written.
177781 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
177786 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177789 ** nul-terminator byte is written.
177801 char *zOut = (char *)&p->aBuf[p->nBuf];
177809 p->nBuf = (int)((u8 *)zOut - p->aBuf);
177814 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
177886 u8 *abPK /* Boolean array - true for PK columns */
177891 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
177893 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
177896 sessionAppendByte(pBuf, p->bIndirect, &rc);
177941 /* If at least one field has been modified, this is not a no-op. */
177966 pBuf->nBuf = nRewind;
177985 u8 *abPK /* Boolean array - true for PK columns */
177990 sessionAppendByte(pBuf, p->bIndirect, &rc);
177993 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
177996 u8 *a = p->aRecord;
178020 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
178023 assert( (a - p->aRecord)==p->nRecord );
178086 u8 *a = pChange->aRecord;
178144 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
178157 sessionAppendVarint(pBuf, pTab->nCol, pRc);
178158 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
178159 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
178164 ** (if it is non-zero) based on the current contents of the session object
178180 sqlite3 *db = pSession->db; /* Source database handle */
178189 ** this call will be a no-op. */
178195 if( pSession->rc ) return pSession->rc;
178196 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
178201 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
178202 if( pTab->nEntry ){
178203 const char *zName = pTab->zName;
178213 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
178214 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
178224 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
178228 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
178231 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
178235 if( p->op==SQLITE_INSERT ){
178238 sessionAppendByte(&buf, p->bIndirect, &rc);
178245 }else if( p->op!=SQLITE_INSERT ){
178260 nNoop = -1;
178348 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
178350 pSession->bEnable = bEnable;
178352 ret = pSession->bEnable;
178353 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
178362 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
178364 pSession->bIndirect = bIndirect;
178366 ret = pSession->bIndirect;
178367 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
178379 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
178380 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
178381 ret = (pTab->nEntry>0);
178383 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
178411 pRet->in.aData = (u8 *)pChangeset;
178412 pRet->in.nData = nChangeset;
178413 pRet->in.xInput = xInput;
178414 pRet->in.pIn = pIn;
178415 pRet->in.bEof = (xInput ? 0 : 1);
178449 if( pIn->bEof && pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
178450 int nMove = pIn->buf.nBuf - pIn->iNext;
178453 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
178455 pIn->buf.nBuf -= pIn->iNext;
178456 pIn->iNext = 0;
178457 pIn->nData = pIn->buf.nBuf;
178470 if( pIn->xInput ){
178471 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
178474 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
178475 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
178476 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
178478 pIn->bEof = 1;
178480 pIn->buf.nBuf += nNew;
178484 pIn->aData = pIn->buf.aBuf;
178485 pIn->nData = pIn->buf.nBuf;
178577 eType = pIn->aData[pIn->iNext++];
178587 u8 *aVal = &pIn->aData[pIn->iNext];
178590 pIn->iNext += sessionVarintGet(aVal, &nByte);
178594 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
178596 pIn->iNext += nByte;
178607 pIn->iNext += 8;
178616 ** The input pointer currently points to the second byte of a table-header.
178635 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
178641 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
178644 if( (pIn->iNext + nRead)<pIn->nData ) break;
178672 eType = pIn->aData[pIn->iNext + nByte++];
178675 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
178688 ** The input pointer currently points to the second byte of a table-header.
178695 ** This function decodes the table-header and populates the p->nCol,
178696 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
178697 ** also allocated or resized according to the new value of p->nCol. The
178707 assert( p->rc==SQLITE_OK );
178709 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
178713 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
178714 nCopy -= nVarint;
178715 p->in.iNext += nVarint;
178716 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
178717 p->tblhdr.nBuf = 0;
178718 sessionBufferGrow(&p->tblhdr, nByte, &rc);
178722 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
178723 memset(p->tblhdr.aBuf, 0, iPK);
178724 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
178725 p->in.iNext += nCopy;
178728 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
178729 p->abPK = (u8*)&p->apValue[p->nCol*2];
178730 p->zTab = (char*)&p->abPK[p->nCol];
178731 return (p->rc = rc);
178752 u8 **paRec, /* If non-NULL, store record pointer here */
178753 int *pnRec /* If non-NULL, store size of record here */
178760 /* If the iterator is in the error-state, return immediately. */
178761 if( p->rc!=SQLITE_OK ) return p->rc;
178763 /* Free the current contents of p->apValue[], if any. */
178764 if( p->apValue ){
178765 for(i=0; i<p->nCol*2; i++){
178766 sqlite3ValueFree(p->apValue[i]);
178768 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
178775 p->rc = sessionInputBuffer(&p->in, 2);
178776 if( p->rc!=SQLITE_OK ) return p->rc;
178779 if( p->in.iNext>=p->in.nData ){
178783 sessionDiscardData(&p->in);
178784 p->in.iCurrent = p->in.iNext;
178786 op = p->in.aData[p->in.iNext++];
178788 p->bPatchset = (op=='P');
178789 if( sessionChangesetReadTblhdr(p) ) return p->rc;
178790 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
178791 p->in.iCurrent = p->in.iNext;
178792 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
178793 op = p->in.aData[p->in.iNext++];
178796 p->op = op;
178797 p->bIndirect = p->in.aData[p->in.iNext++];
178798 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
178799 return (p->rc = SQLITE_CORRUPT_BKPT);
178804 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
178805 nVal = p->nCol * 2;
178806 }else if( p->bPatchset && op==SQLITE_DELETE ){
178808 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
178810 nVal = p->nCol;
178812 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
178813 if( p->rc!=SQLITE_OK ) return p->rc;
178814 *paRec = &p->in.aData[p->in.iNext];
178815 p->in.iNext += *pnRec;
178819 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
178820 u8 *abPK = p->bPatchset ? p->abPK : 0;
178821 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
178822 if( p->rc!=SQLITE_OK ) return p->rc;
178826 if( p->op!=SQLITE_DELETE ){
178827 p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
178828 if( p->rc!=SQLITE_OK ) return p->rc;
178831 if( p->bPatchset && p->op==SQLITE_UPDATE ){
178836 for(i=0; i<p->nCol; i++){
178837 assert( p->apValue[i]==0 );
178838 assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
178839 if( p->abPK[i] ){
178840 p->apValue[i] = p->apValue[i+p->nCol];
178841 p->apValue[i+p->nCol] = 0;
178874 *pOp = pIter->op;
178875 *pnCol = pIter->nCol;
178876 *pzTab = pIter->zTab;
178877 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
178889 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
178892 *pabPK = pIter->abPK;
178893 if( pnCol ) *pnCol = pIter->nCol;
178907 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
178915 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
178918 if( iVal<0 || iVal>=pIter->nCol ){
178921 *ppValue = pIter->apValue[iVal];
178935 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
178943 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
178946 if( iVal<0 || iVal>=pIter->nCol ){
178949 *ppValue = pIter->apValue[pIter->nCol+iVal];
178958 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
178959 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
178964 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
178969 ** If value iVal is out-of-range or some other error occurs, an SQLite error
178977 if( !pIter->pConflict ){
178980 if( iVal<0 || iVal>=pIter->nCol ){
178983 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
178999 if( pIter->pConflict || pIter->apValue ){
179002 *pnOut = pIter->nCol;
179016 int i; /* Used to iterate through p->apValue[] */
179017 rc = p->rc;
179018 if( p->apValue ){
179019 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
179021 sqlite3_free(p->tblhdr.aBuf);
179022 sqlite3_free(p->in.buf.aBuf);
179056 if( pInput->iNext>=pInput->nData ) break;
179057 eType = pInput->aData[pInput->iNext];
179066 ** * A nul-terminated table name.
179070 pInput->iNext++;
179074 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
179076 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
179078 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
179081 pInput->iNext += nByte;
179091 int bIndirect = pInput->aData[pInput->iNext+1];
179093 pInput->iNext += 2;
179098 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
179099 pInput->iNext += nByte;
179118 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
179121 pInput->iNext += 2;
179234 u8 *abPK; /* Boolean array - true if column is in PK */
179248 ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
179250 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
179260 SessionApplyCtx *p /* Session changeset-apply context */
179272 for(i=0; i<p->nCol; i++){
179273 if( p->abPK[i] ){
179276 sessionAppendIdent(&buf, p->azCol[i], &rc);
179283 if( nPk<p->nCol ){
179285 sessionAppendInteger(&buf, p->nCol+1, &rc);
179289 for(i=0; i<p->nCol; i++){
179290 if( !p->abPK[i] ){
179292 sessionAppendIdent(&buf, p->azCol[i], &rc);
179302 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
179319 ** b = CASE WHEN ?5 THEN ?6 ELSE b END,
179323 ** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
179333 ** a row even if the non-PK values do not match. This is required if the
179334 ** conflict-handler is invoked with CHANGESET_DATA and returns
179343 SessionApplyCtx *p /* Session changeset-apply context */
179356 for(i=0; i<p->nCol; i++){
179358 sessionAppendIdent(&buf, p->azCol[i], &rc);
179364 sessionAppendIdent(&buf, p->azCol[i], &rc);
179371 for(i=0; i<p->nCol; i++){
179372 if( p->abPK[i] ){
179373 sessionAppendIdent(&buf, p->azCol[i], &rc);
179380 /* Append the non-PK part of the WHERE clause */
179382 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
179384 for(i=0; i<p->nCol; i++){
179385 if( !p->abPK[i] ){
179389 sessionAppendIdent(&buf, p->azCol[i], &rc);
179398 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
179421 SessionApplyCtx *p /* Session changeset-apply context */
179424 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
179439 SessionApplyCtx *p /* Session changeset-apply context */
179448 for(i=0; i<p->nCol; i++){
179450 sessionAppendIdent(&buf, p->azCol[i], &rc);
179454 for(i=1; i<p->nCol; i++){
179460 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
179476 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
179478 ** the (pVal->z) variable remains as it was or the type of the value is
179480 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
179483 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
179495 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
179580 ** -------------------------------------------------
179591 ** ----------------------------------------------------------------
179599 ** is set to non-zero before returning SQLITE_OK.
179628 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
179635 pIter->pConflict = p->pSelect;
179637 pIter->pConflict = 0;
179638 rc = sqlite3_reset(p->pSelect);
179640 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
179643 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
179644 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
179645 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
179714 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
179715 assert( p->azCol && p->abPK );
179733 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
179734 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
179735 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
179736 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
179740 sqlite3_step(p->pDelete);
179741 rc = sqlite3_reset(p->pDelete);
179742 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
179760 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
179762 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
179765 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
179769 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
179775 sqlite3_step(p->pUpdate);
179776 rc = sqlite3_reset(p->pUpdate);
179778 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
179796 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
179799 sqlite3_step(p->pInsert);
179800 rc = sqlite3_reset(p->pInsert);
179817 ** function handles the case where the conflict-handler is invoked and
179818 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
179843 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
179853 assert( pIter->op==SQLITE_INSERT );
179857 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
179858 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
179861 sqlite3_step(pApply->pDelete);
179862 rc = sqlite3_reset(pApply->pDelete);
179876 ** Retry the changes accumulated in the pApply->constraints buffer.
179888 while( pApply->constraints.nBuf ){
179890 SessionBuffer cons = pApply->constraints;
179891 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
179895 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
179897 pIter2->bPatchset = bPatchset;
179898 pIter2->zTab = (char*)zTab;
179899 pIter2->nCol = pApply->nCol;
179900 pIter2->abPK = pApply->abPK;
179901 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
179902 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
179903 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
179912 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
179916 if( pApply->constraints.nBuf>=cons.nBuf ){
179918 pApply->bDeferConstraints = 0;
179955 pIter->in.bNoDiscard = 1;
179973 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
179988 ** non-zero, proceed. */
180052 bPatchset = pIter->bPatchset;
180184 pNew->op = op2;
180185 pNew->bIndirect = bIndirect;
180186 pNew->nRecord = nRec;
180187 pNew->aRecord = (u8*)&pNew[1];
180188 memcpy(pNew->aRecord, aRec, nRec);
180190 int op1 = pExist->op;
180193 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
180194 ** op1=INSERT, op2=UPDATE -> INSERT.
180195 ** op1=INSERT, op2=DELETE -> (none)
180197 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
180198 ** op1=UPDATE, op2=UPDATE -> UPDATE.
180199 ** op1=UPDATE, op2=DELETE -> DELETE.
180201 ** op1=DELETE, op2=INSERT -> UPDATE.
180202 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
180203 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
180215 u8 *aExist = pExist->aRecord;
180222 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
180229 pNew->bIndirect = (bIndirect && pExist->bIndirect);
180230 aCsr = pNew->aRecord = (u8 *)&pNew[1];
180235 pNew->op = SQLITE_INSERT;
180236 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
180237 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
180240 pNew->op = SQLITE_UPDATE;
180255 sessionSkipRecord(&a1, pTab->nCol);
180256 sessionSkipRecord(&a2, pTab->nCol);
180258 pNew->op = SQLITE_UPDATE;
180265 pNew->op = SQLITE_DELETE;
180270 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
180275 pNew->nRecord = (int)(aCsr - pNew->aRecord);
180309 if( pGrp->pList==0 ){
180310 pGrp->bPatch = pIter->bPatchset;
180311 }else if( pIter->bPatchset!=pGrp->bPatch ){
180317 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
180323 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
180324 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
180335 pTab->nCol = nCol;
180336 pTab->abPK = (u8*)&pTab[1];
180337 memcpy(pTab->abPK, abPK, nCol);
180338 pTab->zName = (char*)&pTab->abPK[nCol];
180339 memcpy(pTab->zName, zNew, nNew+1);
180345 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
180347 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
180353 if( sessionGrowHash(pIter->bPatchset, pTab) ){
180358 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
180364 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
180367 if( pIter->bPatchset ){
180368 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
180371 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
180373 *pp = (*pp)->pNext;
180374 pTab->nEntry--;
180380 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
180384 pChange->pNext = pTab->apChange[iHash];
180385 pTab->apChange[iHash] = pChange;
180386 pTab->nEntry++;
180390 if( rc==SQLITE_OK ) rc = pIter->rc;
180425 ** hash tables attached to the SessionTable objects in list p->pList.
180427 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
180429 if( pTab->nEntry==0 ) continue;
180431 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
180432 for(i=0; i<pTab->nChange; i++){
180434 for(p=pTab->apChange[i]; p; p=p->pNext){
180435 sessionAppendByte(&buf, p->op, &rc);
180436 sessionAppendByte(&buf, p->bIndirect, &rc);
180437 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
180479 ** to changeset-group p.
180540 sessionDeleteTable(pGrp->pList);
180608 ** 2015-08-12
180648 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
180672 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
180713 sqlite3_context *pCtx; /* Function context - put error messages here */
180729 #define JSON_STRING 5
180759 u32 n; /* Bytes of content, or number of sub-nodes */
180799 p->zBuf = p->zSpace;
180800 p->nAlloc = sizeof(p->zSpace);
180801 p->nUsed = 0;
180802 p->bStatic = 1;
180808 p->pCtx = pCtx;
180809 p->bErr = 0;
180818 if( !p->bStatic ) sqlite3_free(p->zBuf);
180823 /* Report an out-of-memory (OOM) condition
180826 p->bErr = 1;
180827 sqlite3_result_error_nomem(p->pCtx);
180831 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
180832 ** Return zero on success. Return non-zero on an OOM error
180835 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
180837 if( p->bStatic ){
180838 if( p->bErr ) return 1;
180844 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
180845 p->zBuf = zNew;
180846 p->bStatic = 0;
180848 zNew = sqlite3_realloc64(p->zBuf, nTotal);
180853 p->zBuf = zNew;
180855 p->nAlloc = nTotal;
180862 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
180863 memcpy(p->zBuf+p->nUsed, zIn, N);
180864 p->nUsed += N;
180871 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
180873 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
180875 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
180881 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
180882 p->zBuf[p->nUsed++] = c;
180890 if( p->nUsed==0 ) return;
180891 c = p->zBuf[p->nUsed-1];
180895 /* Append the N-byte string in zIn to the end of the JsonString string
180897 ** any double-quotes or backslash characters contained within the
180902 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
180903 p->zBuf[p->nUsed++] = '"';
180908 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
180909 p->zBuf[p->nUsed++] = '\\';
180925 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
180926 p->zBuf[p->nUsed++] = '\\';
180927 p->zBuf[p->nUsed++] = 'u';
180928 p->zBuf[p->nUsed++] = '0';
180929 p->zBuf[p->nUsed++] = '0';
180930 p->zBuf[p->nUsed++] = '0' + (c>>4);
180933 p->zBuf[p->nUsed++] = c;
180935 p->zBuf[p->nUsed++] = '"';
180936 assert( p->nUsed<p->nAlloc );
180970 if( p->bErr==0 ){
180971 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
180972 p->bErr = 2;
180984 if( p->bErr==0 ){
180985 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
180986 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
180990 assert( p->bStatic );
181007 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
181015 sqlite3_free(pParse->aNode);
181016 pParse->aNode = 0;
181017 pParse->nNode = 0;
181018 pParse->nAlloc = 0;
181019 sqlite3_free(pParse->aUp);
181020 pParse->aUp = 0;
181041 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
181042 if( pNode->jnFlags & JNODE_REPLACE ){
181043 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
181046 pNode = pNode->u.pPatch;
181048 switch( pNode->eType ){
181050 assert( pNode->eType==JSON_NULL );
181059 jsonAppendRaw(pOut, "false", 5);
181063 if( pNode->jnFlags & JNODE_RAW ){
181064 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
181071 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
181078 while( j<=pNode->n ){
181085 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
181086 pNode = &pNode[pNode->u.iAppend];
181096 while( j<=pNode->n ){
181105 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
181106 pNode = &pNode[pNode->u.iAppend];
181138 switch( pNode->eType ){
181140 assert( pNode->eType==JSON_NULL );
181154 const char *z = pNode->u.zJContent;
181155 if( z[0]=='-' ){ z++; }
181157 unsigned v = *(z++) - '0';
181163 if( pNode->u.zJContent[0]=='-' ){
181173 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
181182 const char *z = pNode->u.zJContent;
181185 r = strtod(pNode->u.zJContent, 0);
181194 if( pNode->jnFlags & JNODE_RAW ){
181195 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
181199 assert( (pNode->jnFlags & JNODE_RAW)==0 );
181200 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
181201 /* JSON formatted without any backslash-escapes */
181202 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
181207 u32 n = pNode->n;
181208 const char *z = pNode->u.zJContent;
181216 for(i=1, j=0; i<n-1; i++){
181225 assert( i<n-2 );
181228 if( c<='9' ) v = v*16 + c - '0';
181229 else if( c<='F' ) v = v*16 + c - 'A' + 10;
181230 else v = v*16 + c - 'a' + 10;
181291 u32 n, /* Content size or sub-node count */
181296 assert( pParse->nNode>=pParse->nAlloc );
181297 if( pParse->oom ) return -1;
181298 nNew = pParse->nAlloc*2 + 10;
181299 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
181301 pParse->oom = 1;
181302 return -1;
181304 pParse->nAlloc = nNew;
181305 pParse->aNode = pNew;
181306 assert( pParse->nNode<pParse->nAlloc );
181312 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
181313 ** new node, or -1 if a memory allocation fails.
181318 u32 n, /* Content size or sub-node count */
181322 if( pParse->nNode>=pParse->nAlloc ){
181325 p = &pParse->aNode[pParse->nNode];
181326 p->eType = (u8)eType;
181327 p->jnFlags = 0;
181328 p->n = n;
181329 p->u.zJContent = zContent;
181330 return pParse->nNode++;
181343 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
181346 ** Return negative for a syntax error. Special cases: return -2 if the
181347 ** first non-whitespace character is '}' and return -3 if the first
181348 ** non-whitespace character is ']'.
181356 const char *z = pParse->zJson;
181361 if( iThis<0 ) return -1;
181364 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
181367 pParse->iDepth--;
181368 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
181369 return -1;
181371 if( pParse->oom ) return -1;
181372 pNode = &pParse->aNode[pParse->nNode-1];
181373 if( pNode->eType!=JSON_STRING ) return -1;
181374 pNode->jnFlags |= JNODE_LABEL;
181377 if( z[j]!=':' ) return -1;
181380 pParse->iDepth--;
181381 if( x<0 ) return -1;
181386 if( c!='}' ) return -1;
181389 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
181394 if( iThis<0 ) return -1;
181397 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
181399 pParse->iDepth--;
181401 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
181402 return -1;
181408 if( c!=']' ) return -1;
181411 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
181421 return -1;
181430 return -1;
181437 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
181438 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
181451 && strncmp(z+i,"false",5)==0
181452 && !safe_isalnum(z[i+5]) ){
181454 return i+5;
181455 }else if( c=='-' || (c>='0' && c<='9') ){
181459 assert( '-' < '0' );
181461 j = c=='-' ? i+1 : i;
181462 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
181469 if( z[j-1]=='-' ) return -1;
181470 if( seenDP ) return -1;
181475 if( z[j-1]<'0' ) return -1;
181476 if( seenE ) return -1;
181479 if( c=='+' || c=='-' ){
181483 if( c<'0' || c>'9' ) return -1;
181488 if( z[j-1]<'0' ) return -1;
181490 j - i, &z[i]);
181493 return -2; /* End of {...} */
181495 return -3; /* End of [...] */
181499 return -1; /* Syntax error */
181504 ** Parse a complete JSON string. Return 0 on success or non-zero if there
181518 pParse->zJson = zJson;
181520 if( pParse->oom ) i = -1;
181522 assert( pParse->iDepth==0 );
181524 if( zJson[i] ) i = -1;
181528 if( pParse->oom ){
181531 sqlite3_result_error(pCtx, "malformed JSON", -1);
181544 JsonNode *pNode = &pParse->aNode[i];
181546 pParse->aUp[i] = iParent;
181547 switch( pNode->eType ){
181549 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
181555 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
181556 pParse->aUp[i+j] = i;
181572 assert( pParse->aUp==0 );
181573 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
181575 pParse->oom = 1;
181585 #define JSON_CACHE_ID (-429938)
181604 if( p && p->nJson==nJson && memcmp(p->zJson,zJson,nJson)==0 ){
181605 p->nErr = 0;
181614 p->zJson = (char*)&p[1];
181615 memcpy((char*)p->zJson, zJson, nJson+1);
181616 if( jsonParse(p, pCtx, p->zJson) ){
181620 p->nJson = nJson;
181630 if( pNode->jnFlags & JNODE_RAW ){
181631 if( pNode->n!=nKey ) return 0;
181632 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
181634 if( pNode->n!=nKey+2 ) return 0;
181635 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
181660 JsonNode *pRoot = &pParse->aNode[iRoot];
181663 if( pRoot->eType!=JSON_OBJECT ) return 0;
181668 nKey = i-1;
181686 while( j<=pRoot->n ){
181693 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
181694 iRoot += pRoot->u.iAppend;
181695 pRoot = &pParse->aNode[iRoot];
181705 if( pParse->oom ) return 0;
181707 pRoot = &pParse->aNode[iRoot];
181708 pRoot->u.iAppend = iStart - iRoot;
181709 pRoot->jnFlags |= JNODE_APPEND;
181710 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
181715 if( pRoot->eType!=JSON_ARRAY ) return 0;
181719 i = i*10 + zPath[j] - '0';
181729 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
181730 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
181733 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
181734 iRoot += pRoot->u.iAppend;
181735 pRoot = &pParse->aNode[iRoot];
181738 if( j<=pRoot->n ){
181746 if( pParse->oom ) return 0;
181748 pRoot = &pParse->aNode[iRoot];
181749 pRoot->u.iAppend = iStart - iRoot;
181750 pRoot->jnFlags |= JNODE_APPEND;
181773 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
181782 if( pParse->oom ) return 0;
181783 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
181799 ** pParse->nErr counter.
181824 pParse->nErr++;
181828 sqlite3_result_error(pCtx, zMsg, -1);
181847 sqlite3_result_error(pCtx, zMsg, -1);
181856 assert( pNode->eType==JSON_OBJECT );
181857 n = pNode->n;
181879 ** well-formed.
181886 JsonString s; /* Output string - not real JSON */
181902 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
181936 ** double-quotes around strings and returning the unquoted string "null"
181982 ** Return the number of elements in the top-level JSON array.
181983 ** Return 0 if the input is not a well-formed JSON array.
181997 assert( p->nNode );
182002 pNode = p->aNode;
182007 if( pNode->eType==JSON_ARRAY ){
182008 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
182009 for(i=1; i<=pNode->n; n++){
182043 if( p->nErr ) break;
182073 if( pPatch->eType!=JSON_OBJECT ){
182076 assert( iTarget>=0 && iTarget<pParse->nNode );
182077 pTarget = &pParse->aNode[iTarget];
182078 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
182079 if( pTarget->eType!=JSON_OBJECT ){
182084 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
182092 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
182103 pTarget = &pParse->aNode[iTarget];
182112 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
182117 if( pParse->oom ) return 0;
182119 pTarget = &pParse->aNode[iTarget];
182120 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
182121 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
182123 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
182124 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
182179 "of arguments", -1);
182186 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
182227 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
182240 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
182264 pNode->jnFlags |= (u8)JNODE_REPLACE;
182265 pNode->u.iReplace = i + 1;
182287 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
182318 pNode->jnFlags |= (u8)JNODE_REPLACE;
182319 pNode->u.iReplace = i + 1;
182335 ** Return the top-level "type" of a JSON string. Throw an error if
182336 ** either the JSON or PATH inputs are not well-formed.
182356 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
182364 ** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
182401 if( pStr->zBuf==0 ){
182406 pStr->pCtx = ctx;
182415 pStr->pCtx = ctx;
182417 if( pStr->bErr ){
182418 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
182419 assert( pStr->bStatic );
182421 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
182422 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
182423 pStr->bStatic = 1;
182447 if( pStr->zBuf==0 ){
182452 pStr->pCtx = ctx;
182466 if( pStr->bErr ){
182467 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
182468 assert( pStr->bStatic );
182470 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
182471 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
182472 pStr->bStatic = 1;
182487 sqlite3_vtab_cursor base; /* Base class - must be first */
182492 u8 eType; /* Type of top-level element */
182516 #define JEACH_PARENT 5
182551 *ppCursor = &pCur->base;
182560 pCur->bRecursive = 1;
182568 sqlite3_free(p->zJson);
182569 sqlite3_free(p->zRoot);
182570 jsonParseReset(&p->sParse);
182571 p->iRowid = 0;
182572 p->i = 0;
182573 p->iEnd = 0;
182574 p->eType = 0;
182575 p->zJson = 0;
182576 p->zRoot = 0;
182591 return p->i >= p->iEnd;
182597 if( p->bRecursive ){
182598 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
182599 p->i++;
182600 p->iRowid++;
182601 if( p->i<p->iEnd ){
182602 u32 iUp = p->sParse.aUp[p->i];
182603 JsonNode *pUp = &p->sParse.aNode[iUp];
182604 p->eType = pUp->eType;
182605 if( pUp->eType==JSON_ARRAY ){
182606 if( iUp==p->i-1 ){
182607 pUp->u.iKey = 0;
182609 pUp->u.iKey++;
182614 switch( p->eType ){
182616 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
182617 p->iRowid++;
182621 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
182622 p->iRowid++;
182626 p->i = p->iEnd;
182647 iUp = p->sParse.aUp[i];
182649 pNode = &p->sParse.aNode[i];
182650 pUp = &p->sParse.aNode[iUp];
182651 if( pUp->eType==JSON_ARRAY ){
182652 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
182654 assert( pUp->eType==JSON_OBJECT );
182655 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
182656 assert( pNode->eType==JSON_STRING );
182657 assert( pNode->jnFlags & JNODE_LABEL );
182658 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
182669 JsonNode *pThis = &p->sParse.aNode[p->i];
182672 if( p->i==0 ) break;
182673 if( p->eType==JSON_OBJECT ){
182675 }else if( p->eType==JSON_ARRAY ){
182677 if( p->bRecursive ){
182678 if( p->iRowid==0 ) break;
182679 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
182681 iKey = p->iRowid;
182688 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
182693 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
182694 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
182698 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
182699 if( pThis->eType>=JSON_ARRAY ) break;
182705 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
182709 if( p->i>p->iBegin && p->bRecursive ){
182710 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
182717 if( p->bRecursive ){
182718 jsonEachComputePath(p, &x, p->i);
182720 if( p->zRoot ){
182721 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
182725 if( p->eType==JSON_ARRAY ){
182726 jsonPrintf(30, &x, "[%d]", p->iRowid);
182728 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
182735 if( p->bRecursive ){
182738 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
182746 const char *zRoot = p->zRoot;
182748 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
182753 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
182763 *pRowid = p->iRowid;
182777 int jsonIdx = -1;
182778 int rootIdx = -1;
182782 pConstraint = pIdxInfo->aConstraint;
182783 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
182784 if( pConstraint->usable==0 ) continue;
182785 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
182786 switch( pConstraint->iColumn ){
182789 default: /* no-op */ break;
182793 pIdxInfo->idxNum = 0;
182794 pIdxInfo->estimatedCost = 1e99;
182796 pIdxInfo->estimatedCost = 1.0;
182797 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
182798 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
182800 pIdxInfo->idxNum = 1;
182802 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
182803 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
182804 pIdxInfo->idxNum = 3;
182828 p->zJson = sqlite3_malloc64( n+1 );
182829 if( p->zJson==0 ) return SQLITE_NOMEM;
182830 memcpy(p->zJson, z, (size_t)n+1);
182831 if( jsonParse(&p->sParse, 0, p->zJson) ){
182833 if( p->sParse.oom==0 ){
182834 sqlite3_free(cur->pVtab->zErrMsg);
182835 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
182836 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
182840 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
182850 p->zRoot = sqlite3_malloc64( n+1 );
182851 if( p->zRoot==0 ) return SQLITE_NOMEM;
182852 memcpy(p->zRoot, zRoot, (size_t)n+1);
182856 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
182859 sqlite3_free(cur->pVtab->zErrMsg);
182860 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
182862 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
182867 pNode = p->sParse.aNode;
182869 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
182870 p->eType = pNode->eType;
182871 if( p->eType>=JSON_ARRAY ){
182872 pNode->u.iKey = 0;
182873 p->iEnd = p->i + pNode->n + 1;
182874 if( p->bRecursive ){
182875 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
182876 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
182877 p->i--;
182880 p->i++;
182883 p->iEnd = p->i+1;
182897 jsonEachOpenEach, /* xOpen - open a cursor */
182898 jsonEachClose, /* xClose - close a cursor */
182899 jsonEachFilter, /* xFilter - configure scan constraints */
182900 jsonEachNext, /* xNext - advance a cursor */
182901 jsonEachEof, /* xEof - check for end of scan */
182902 jsonEachColumn, /* xColumn - read data */
182903 jsonEachRowid, /* xRowid - read data */
182924 jsonEachOpenTree, /* xOpen - open a cursor */
182925 jsonEachClose, /* xClose - close a cursor */
182926 jsonEachFilter, /* xFilter - configure scan constraints */
182927 jsonEachNext, /* xNext - advance a cursor */
182928 jsonEachEof, /* xEof - check for end of scan */
182929 jsonEachColumn, /* xColumn - read data */
182930 jsonEachRowid, /* xRowid - read data */
182960 { "json_array", -1, 0, jsonArrayFunc },
182963 { "json_extract", -1, 0, jsonExtractFunc },
182964 { "json_insert", -1, 0, jsonSetFunc },
182965 { "json_object", -1, 0, jsonObjectFunc },
182968 { "json_remove", -1, 0, jsonRemoveFunc },
182969 { "json_replace", -1, 0, jsonReplaceFunc },
182970 { "json_set", -1, 1, jsonSetFunc },
183111 ** non-negative but less than the number of columns in the table, return
183126 ** non-negative but less than the number of columns in the table, set
183140 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
183172 ** set to -1.
183242 ** If the bClear argument is non-zero, then the auxiliary data is cleared
183264 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
183266 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
183272 ** modify this structure directly - it should only be used as shown above
183280 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
183294 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
183296 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
183305 ** xPhraseFirstColumn() set iCol to -1).
183374 ** The second and third arguments are an array of nul-terminated strings
183392 ** by argument pText. pText may or may not be nul-terminated. The first
183400 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
183405 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
183409 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
183414 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
183424 ** size of the token in bytes. The 4th and 5th arguments are the byte offsets
183447 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
183481 ** still appears to contain just two phrases - "(first OR 1st)"
183492 ** when tokenizing query text (it should not - to do would be
183502 ** synonyms would call xToken() 5 times, as follows:
183506 ** xToken(pCtx, 0, "won", 3, 2, 5);
183507 ** xToken(pCtx, 0, "first", 5, 6, 11);
183509 ** xToken(pCtx, 0, "place", 5, 12, 17);
183539 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
183672 ** Constants for the largest and smallest possible 64-bit signed integers.
183675 # define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
183680 ** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
183757 ** The minimum number of segments that an auto-merge operation should
183759 ** compile time default. Zero disables auto-merge altogether.
183765 ** the string "rowid" otherwise. This text is not quoted - if it is
183772 ** decent error message if it encounters a file-format version it does
183782 ** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
183809 int nHashSize; /* Bytes of memory for in-memory hash */
183813 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
183817 int bPrefixIndex; /* True to use prefix-indexes */
183895 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
183896 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
183899 /* Write and decode big-endian 32-bit integer values */
183946 /* Bucket of terms object used by the integrity-check in offsets=0 mode. */
183971 #define sqlite3Fts5IterEof(x) ((x)->bEof)
184006 ** Argument p points to a buffer containing utf-8 text that is n bytes in
184059 int iCol, /* Column token appears in (-ve -> delete) */
184075 ** Flush any data stored in the in-memory hash tables to the database.
184081 ** Discard any data stored in the in-memory hash tables. Do not write it
184083 ** table may have changed on disk. So any in-memory caches of %_data
184095 ** Functions called by the storage module as part of integrity-check.
184138 iOff--; \
184182 int iCol, /* Column token appears in (-ve -> delete) */
184207 const char **pzTerm, /* OUT: term (nul-terminated) */
184325 ** The fts5_expr.c API above this point is used by the other hand-written
184425 #define FTS5_COLON 5
184437 ** 2000-05-29
184451 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
184452 ** interstitial "-" characters) contained in this template is changed into
184467 ** Disable all error recovery processing in the parser push-down
184492 ** "lemon" is run with the "-m" command-line option.
184499 ** that represent terminal and non-terminal symbols.
184505 ** (also known as: "terminal symbols") have fall-back
184509 ** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
184514 ** value associated with a terminal or non-terminal
184517 ** Each non-terminal can have a different minor type.
184536 ** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
184537 ** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
184541 ** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
184565 #define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse = fts5yypParser->pParse
184566 #define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse = pParse
184579 /* Define the fts5yytestcase() macro to be a no-op if is not already defined
184604 ** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
184606 ** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
184631 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
184644 ** shifting non-terminals after a reduce.
184656 /* 60 */ 7, 24, 3, 4, 5, 3, 4, 5, 3, 77,
184657 /* 70 */ 4, 5, 3, 61, 23, 15, 60, 11, 80, 12,
184659 /* 90 */ 8, 28, 5, 3, 51, 55, 72, 14,
184667 /* 50 */ 6, 7, 13, 9, 10, 18, 21, 20, 5, 22,
184668 /* 60 */ 5, 24, 3, 1, 2, 3, 1, 2, 3, 0,
184669 /* 70 */ 1, 2, 3, 11, 6, 7, 11, 9, 5, 9,
184683 #define fts5YY_REDUCE_USE_DFLT (-18)
184685 #define fts5YY_REDUCE_MIN (-17)
184688 /* 0 */ -16, -8, 0, 9, 17, 25, 37, -17, 64, -17,
184697 /********** End of lemon-generated parsing tables *****************************/
184735 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
184738 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
184748 int fts5yyhwm; /* High-water mark of the stack */
184820 /* 5 */ "colsetlist ::= colsetlist STRING",
184856 newSize = p->fts5yystksz*2 + 100;
184857 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
184858 if( p->fts5yystack==&p->fts5yystk0 ){
184860 if( pNew ) pNew[0] = p->fts5yystk0;
184862 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
184865 p->fts5yystack = pNew;
184866 p->fts5yytos = &p->fts5yystack[idx];
184870 fts5yyTracePrompt, p->fts5yystksz, newSize);
184873 p->fts5yystksz = newSize;
184893 pParser->fts5yyhwm = 0;
184896 pParser->fts5yytos = NULL;
184897 pParser->fts5yystack = NULL;
184898 pParser->fts5yystksz = 0;
184900 pParser->fts5yystack = &pParser->fts5yystk0;
184901 pParser->fts5yystksz = 1;
184905 pParser->fts5yyerrcnt = -1;
184907 pParser->fts5yytos = pParser->fts5yystack;
184908 pParser->fts5yystack[0].stateno = 0;
184909 pParser->fts5yystack[0].major = 0;
184911 pParser->fts5yystackEnd = &pParser->fts5yystack[fts5YYSTACKDEPTH-1];
184952 ** terminal or non-terminal is destroyed. This can happen
184971 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
184977 sqlite3_free((fts5yypminor->fts5yy11));
184983 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
184988 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
185004 assert( pParser->fts5yytos!=0 );
185005 assert( pParser->fts5yytos > pParser->fts5yystack );
185006 fts5yytos = pParser->fts5yytos--;
185011 fts5yyTokenName[fts5yytos->major]);
185014 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
185022 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
185024 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
185055 return pParser->fts5yyhwm;
185061 ** look-ahead token iLookAhead.
185065 fts5YYCODETYPE iLookAhead /* The look-ahead token */
185068 int stateno = pParser->fts5yytos->stateno;
185094 int j = i - iLookAhead + fts5YYWILDCARD;
185123 ** Find the appropriate action for a parser given the non-terminal
185124 ** look-ahead token iLookAhead.
185128 fts5YYCODETYPE iLookAhead /* The look-ahead token */
185163 …while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser…
185181 fts5yyTracePrompt,fts5yyTokenName[fts5yypParser->fts5yytos->major],
185185 fts5yyTracePrompt,fts5yyTokenName[fts5yypParser->fts5yytos->major]);
185203 fts5yypParser->fts5yytos++;
185205 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
185206 fts5yypParser->fts5yyhwm++;
185207 …assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
185211 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
185212 fts5yypParser->fts5yytos--;
185217 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
185219 fts5yypParser->fts5yytos--;
185226 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
185228 fts5yytos = fts5yypParser->fts5yytos;
185229 fts5yytos->stateno = (fts5YYACTIONTYPE)fts5yyNewState;
185230 fts5yytos->major = (fts5YYCODETYPE)fts5yyMajor;
185231 fts5yytos->minor.fts5yy0 = fts5yyMinor;
185239 fts5YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
185242 { 16, -1 },
185243 { 20, -4 },
185244 { 20, -3 },
185245 { 20, -1 },
185246 { 20, -2 },
185247 { 21, -2 },
185248 { 21, -1 },
185249 { 17, -3 },
185250 { 17, -3 },
185251 { 17, -3 },
185252 { 17, -5 },
185253 { 17, -3 },
185254 { 17, -1 },
185255 { 19, -1 },
185256 { 19, -2 },
185257 { 18, -1 },
185258 { 18, -3 },
185259 { 22, -1 },
185260 { 22, -5 },
185261 { 23, -1 },
185262 { 23, -2 },
185264 { 25, -2 },
185265 { 24, -4 },
185266 { 24, -2 },
185267 { 26, -1 },
185286 fts5yymsp = fts5yypParser->fts5yytos;
185300 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
185301 fts5yypParser->fts5yyhwm++;
185302 … assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
185306 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
185311 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
185316 fts5yymsp = fts5yypParser->fts5yytos;
185337 … fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
185341 { fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
185351 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
185352 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
185355 case 5: /* colsetlist ::= colsetlist STRING */
185357 …fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[…
185358 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
185368 …fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts…
185370 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185374 …fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5…
185376 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185380 …fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts…
185382 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185386 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
185387 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
185389 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185392 {fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
185401 …fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yy…
185403 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185414 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
185416 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
185424 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
185425 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
185426 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
185428 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
185438 …fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[…
185440 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
185446 { fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
185450 …fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1…
185452 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
185456 …fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[…
185458 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
185483 fts5yypParser->fts5yytos += fts5yysize;
185487 fts5yypParser->fts5yytos = fts5yymsp;
185488 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
185489 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
185507 …while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser…
185549 fts5yypParser->fts5yyerrcnt = -1;
185551 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
185572 ** <li> An option argument of a grammar-specified type.
185595 assert( fts5yypParser->fts5yytos!=0 );
185612 fts5yypParser->fts5yyerrcnt--;
185616 fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE);
185648 if( fts5yypParser->fts5yyerrcnt<0 ){
185651 fts5yymx = fts5yypParser->fts5yytos->major;
185662 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
185665 fts5yypParser->fts5yytos->stateno,
185670 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
185674 fts5yypParser->fts5yyerrcnt = -1;
185681 fts5yypParser->fts5yyerrcnt = 3;
185705 if( fts5yypParser->fts5yyerrcnt<=0 ){
185708 fts5yypParser->fts5yyerrcnt = 3;
185713 fts5yypParser->fts5yyerrcnt = -1;
185719 }while( fts5yymajor!=fts5YYNOCODE && fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
185725 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
185726 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
185790 pIter->iStart = -1;
185791 pIter->iEnd = -1;
185793 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
185795 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
185797 if( ic==pIter->iCol ){
185798 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
185799 if( pIter->iStart<0 ){
185800 pIter->iStart = io;
185801 pIter->iEnd = iEnd;
185802 }else if( io<=pIter->iEnd ){
185803 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
185808 pIter->iInst++;
185828 pIter->pApi = pApi;
185829 pIter->pFts = pFts;
185830 pIter->iCol = iCol;
185831 rc = pApi->xInstCount(pFts, &pIter->nInst);
185850 int iRangeEnd; /* If non-zero, last token to include */
185860 ** Append text to the HighlightContext output string - p->zOut. Argument
185865 ** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
185875 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
185876 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
185898 iPos = p->iPos++;
185900 if( p->iRangeEnd>0 ){
185901 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
185902 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
185905 if( iPos==p->iter.iStart ){
185906 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
185907 fts5HighlightAppend(&rc, p, p->zOpen, -1);
185908 p->iOff = iStartOff;
185911 if( iPos==p->iter.iEnd ){
185912 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
185913 fts5HighlightAppend(&rc, p, p->zOpen, -1);
185915 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
185916 fts5HighlightAppend(&rc, p, p->zClose, -1);
185917 p->iOff = iEndOff;
185919 rc = fts5CInstIterNext(&p->iter);
185923 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
185924 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
185925 p->iOff = iEndOff;
185926 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
185927 fts5HighlightAppend(&rc, p, p->zClose, -1);
185950 sqlite3_result_error(pCtx, zErr, -1);
185958 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
185966 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
185968 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
185971 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
186001 if( p->nFirstAlloc==p->nFirst ){
186002 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
186005 aNew = (int*)sqlite3_realloc(p->aFirst, nNew*sizeof(int));
186007 p->aFirst = aNew;
186008 p->nFirstAlloc = nNew;
186010 p->aFirst[p->nFirst++] = iAdd;
186034 if( p->iPos>0 ){
186037 for(i=iStartOff-1; i>=0; i--){
186038 c = p->zDoc[i];
186041 if( i!=iStartOff-1 && (c=='.' || c==':') ){
186042 rc = fts5SentenceFinderAdd(p, p->iPos);
186047 p->iPos++;
186068 int iFirst = -1;
186073 rc = pApi->xInstCount(pFts, &nInst);
186075 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
186080 iLast = iOff + pApi->xPhraseSize(pFts, ip);
186086 int iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
186087 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
186109 int nToken; /* 5th argument to snippet() */
186121 if( nVal!=5 ){
186123 sqlite3_result_error(pCtx, zErr, -1);
186127 nCol = pApi->xColumnCount(pFts);
186136 nPhrase = pApi->xPhraseCount(pFts);
186142 rc = pApi->xInstCount(pFts, &nInst);
186153 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);