Lines Matching +full:- +full:a
4 ** single large file, the entire code can be compiled as a single translation
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
13 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
30 ** a legal notice, here is a blessing:
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.
62 ** This array looks large, but in a typical installation actually uses
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__,
782 ** a legal notice, here is a blessing:
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
850 ** a legal notice, here is a blessing:
892 ** 2015-03-02
895 ** a legal notice, here is a blessing:
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
938 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
945 ** on 2008-11-28.) These days, all Linux kernels support large files, so
960 ** conditionally include optimizations for each of these compilers. A
962 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
965 ** There was once also a CLANG_VERSION macro. However, we learn that the
969 ** so the GCC_VERSION macro will be set to a correct non-zero value even
1001 ** some MinGW-specific macros). When compiling for MinGW, either the
1031 ** 2001-09-15
1034 ** a legal notice, here is a blessing:
1042 ** presents to client programs. If a C-function, structure, datatype,
1044 ** not a published API of SQLite, is subject to change without
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
1108 ** compiler magic ended up generating such a flurry of bug reports
1126 ** CAPI3REF: Compile-Time Library Version Numbers
1129 ** evaluates to a string literal that is the SQLite version in the
1142 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
1143 ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
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
1176 ** macro. ^The sqlite3_libversion() function returns a pointer to the
1182 ** a pointer to a string constant whose value is the same as the
1193 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1202 ** returning the N-th compile time option string. ^If N is out of range,
1203 ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
1224 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
1233 ** Enabling mutexes incurs a measurable performance penalty.
1242 ** This interface only reports on the compile-time mutex setting
1245 ** can be fully or partially disabled using a call to [sqlite3_config()]
1248 ** sqlite3_threadsafe() function shows only the compile-time setting of
1249 ** thread safety, not any run-time changes to that setting made by
1261 ** Each open SQLite database is represented by a pointer to an instance of
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
1307 ** If compiling for a processor that lacks floating point support,
1308 ** substitute integer for floating-point.
1315 ** CAPI3REF: Closing A Database Connection
1339 ** sqlite3_close_v2() is called on a [database connection] that still has
1345 ** ^If an [sqlite3] object is destroyed while a transaction is open,
1349 ** must be either a NULL
1353 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
1354 ** argument is a harmless no-op.
1360 ** The type for a callback function.
1367 ** CAPI3REF: One-Step Query Execution Interface
1370 ** The sqlite3_exec() interface is a convenience wrapper around
1373 ** without having to use a lot of C code.
1375 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1376 ** semicolon-separate SQL statements passed into its 2nd argument,
1398 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1405 ** [sqlite3_column_text()], one for each column. ^If an element of a
1407 ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
1412 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
1413 ** to an empty string, or a pointer that contains only whitespace and/or
1421 ** is a valid and open [database connection].
1448 /* beginning-of-error-codes */
1454 #define SQLITE_LOCKED 6 /* A table in the database is locked */
1455 #define SQLITE_NOMEM 7 /* A malloc() failed */
1456 #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
1474 #define SQLITE_NOTADB 26 /* File opened that is not a database file */
1479 /* end-of-error-codes */
1487 ** these result codes are too coarse-grained. They do not provide as
1493 ** on a per database connection basis using the
1589 ** object returns an integer which is a vector of these
1599 ** that when data is appended to a file, the data is appended
1604 ** after reboot following a crash or power loss, the only bytes in a
1608 ** flag indicates that a file cannot be deleted when open. The
1610 ** read-only media and cannot be changed even by processes with
1645 ** [sqlite3_io_methods] object it uses a combination of
1661 ** only make a difference on Mac OSX for the default SQLite code.
1662 ** (Third-party VFS implementations might also make the distinction
1678 ** for their own use. The pMethods entry is a pointer to an
1691 ** [sqlite3_file] object (or, more commonly, a subclass of the
1692 ** [sqlite3_file] object) with a pointer to an instance of this object.
1697 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1699 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1705 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1719 ** either in this process or in some other process, is holding a RESERVED,
1721 ** if such a lock exists and false otherwise.
1723 ** The xFileControl() method is a generic interface that allows custom
1726 ** integer opcode. The third argument is a generic pointer intended to
1727 ** point to a structure that may contain arguments or space in which to
1730 ** locking strategy (for example to use dot-file locks), to inquire
1731 ** about the status of a lock, or to break stale locks. The SQLite
1733 ** A [file control opcodes | list of opcodes] less than 100 is available.
1734 ** Applications that define a custom xFileControl method should use opcodes
1743 ** method returns a bit vector describing behaviors of the
1768 ** that when data is appended to a file, the data is appended
1775 ** in the unread portions of the buffer with zeros. A VFS that
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.
1827 ** layer a hint of how large the database file will grow to be during the
1835 ** extends and truncates the database file in chunks of a size specified
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
1843 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1844 ** to the [sqlite3_file] object associated with a particular database
1848 ** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1850 ** the [rollback journal] or the [write-ahead log]) for a particular database
1858 ** sent to the VFS immediately before the xSync method is invoked on a
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
1872 ** and sent to the VFS after a transaction has been committed immediately
1882 ** anti-virus programs. By default, the windows VFS will retry file read,
1883 ** file write, and file delete operations up to 10 times, with a delay
1888 ** within the same process. The argument is a pointer to an array of two
1905 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
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
1915 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
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.
1922 ** a write transaction to indicate that, unless it is rolled back for some
1929 ** final bottom-level VFS are written into memory obtained from
1933 ** all file-control actions, there is no guarantee that this will actually
1934 ** do anything. Callers should initialize the char* variable to a NULL
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.
1948 ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1956 ** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1962 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1963 ** prepared statement if result string is NULL, or that returns a copy
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
1975 ** shortly after it is opened in order to provide a custom VFS with access
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
1978 ** to a function of type (int (*)(void *)). In order to invoke the connections
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
1986 ** to have SQLite generate a
1989 ** argument should be a char** which will be filled with the filename
1991 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
1995 ** maximum number of bytes that will be used for memory-mapped I/O.
1996 ** The argument is a pointer to a value of type sqlite3_int64 that
2000 ** can be queried by passing in a pointer to a negative number. This
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.
2012 ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2013 ** pointer to an integer and it writes a boolean into that integer depending
2019 ** underlying native file handle associated with a file handle. This file
2020 ** control interprets its argument as a pointer to a native file handle and
2030 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2033 ** circumstances in order to fix a problem with priority inversion.
2034 ** Applications should <em>not</em> use this file-control.
2086 ** abstract type for a mutex object. The SQLite core never looks
2097 ** A pointer to the opaque sqlite3_api_routines structure is passed as
2121 ** a pathname in this VFS.
2123 ** Registered sqlite3_vfs objects are kept on a linked list formed by
2126 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
2132 ** or modify this field while holding a particular static mutex.
2141 ** is either a NULL pointer or string obtained
2143 ** ^If a suffix is added to the zFilename parameter, it will
2144 ** consist of a single "-" character followed by no more than
2145 ** 11 alphanumeric and/or "-" characters.
2149 ** the [sqlite3_file] can safely store a pointer to the
2151 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
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
2182 ** SQLITE_IOERR. Or the implementation might recognize that a database
2183 ** file will be doing page-aligned sector reads and writes in a random
2212 ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
2219 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2220 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2221 ** to test whether a file is at least readable. The file can be a
2226 ** is also passed as a parameter to both methods. If the output buffer
2228 ** handled as a fatal error by SQLite, vfs implementations should endeavor
2229 ** to prevent this by setting mxPathname to a sufficiently large value.
2232 ** interfaces are not strictly a part of the filesystem, but they are
2235 ** of good-quality randomness into zOut. The return value is
2239 ** method returns a Julian Day Number for the current date and time as
2240 ** a floating point value.
2243 ** a 24-hour day).
2252 ** system calls with functions under its control, a test program can
2269 void *pAppData; /* Pointer to application-specific data */
2315 ** [temp_store_directory pragma], though this could change in a future
2319 ** currently unused, though it might be used in a future release of
2358 ** The SQLite core will never attempt to acquire or release a
2374 ** A call to sqlite3_initialize() is an "effective" call if it is
2377 ** following a call to sqlite3_shutdown(). ^(Only an effective call
2379 ** are harmless no-ops.)^
2381 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
2384 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2387 ** is not. The sqlite3_shutdown() interface must only be called from a
2398 ** the library (perhaps it is unable to allocate a needed resource such
2399 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
2407 ** compile-time option, then the automatic calls to sqlite3_initialize()
2416 ** The sqlite3_os_init() routine does operating-system specific
2421 ** setting up a default [sqlite3_vfs] module, or setting up
2422 ** a default configuration using [sqlite3_config()].
2432 ** (using the [SQLITE_OS_OTHER=1] compile-time
2433 ** option) the application must supply a suitable implementation for
2434 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2463 ** implementation of an application-defined [sqlite3_os_init()].
2471 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2473 ** then this routine returns a non-zero [error code].
2482 ** changes to a [database connection]. The interface is similar to
2483 ** [sqlite3_config()] except that the changes apply to a single
2487 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2500 ** and low-level memory allocation routines.
2503 ** A pointer to an instance of this object is the argument to
2512 ** Note that SQLite comes with several [built-in memory allocators]
2514 ** and that this object is only useful to a tiny minority of applications
2517 ** memory allocator that simulates memory out-of-memory conditions in
2524 ** xRealloc is always a value returned by a prior call to xRoundup.
2526 ** xSize should return the allocated size of a memory allocation
2531 ** a memory allocation given a particular requested size. Most memory
2533 ** of 8. Some allocators round up to a larger multiple or to a power of 2.
2562 void (*xFree)(void*); /* Free a prior allocation */
2581 ** the call worked. The [sqlite3_config()] interface will return a
2582 ** non-zero [error code] if a discontinued or unsupported configuration option
2588 ** [threading mode] to Single-thread. In other words, it disables
2589 ** all mutexing and puts SQLite into a mode where it can only be used
2590 ** by a single thread. ^If SQLite is compiled with
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
2628 ** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2629 ** a pointer to an instance of the [sqlite3_mem_methods] structure.
2631 ** alternative low-level memory allocation routines to be used in place of
2637 ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2638 ** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2642 ** routines with a wrapper that simulations memory allocation failure or
2647 ** interpreted as a boolean, which enables or disables the collection of
2649 ** disabled, the following SQLite interfaces become non-operational:
2662 ** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
2664 ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
2668 ** The first argument must be a pointer to an 8-byte aligned buffer
2671 ** ^SQLite will never request a scratch buffer that is more than 6
2680 ** fragmentation in low-memory embedded systems.
2684 ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2687 ** This configuration option is a no-op if an application-define page
2689 ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2690 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2693 ** (a power of two between 512 and 65536) plus some extra bytes for each
2698 ** argument must be either a NULL pointer or a pointer to an 8-byte
2703 ** a page cache line is larger than sz bytes or if all of the pMem buffer
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
2714 ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2722 ** An 8-byte aligned pointer to the memory,
2729 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2735 ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2737 ** The argument specifies alternative low-level mutex routines to be used
2738 ** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2741 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2747 ** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2748 ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2752 ** routines with a wrapper used to track mutex usage for performance
2754 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2770 ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2771 ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2772 ** the interface to a custom page cache implementation.)^
2773 ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2776 ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2777 ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2783 ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2784 ** function with a call signature of void(*)(void*,int,const char*),
2785 ** and a pointer to void. ^If the function pointer is not NULL, it is
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
2791 ** the logger function is a copy of the first parameter to the corresponding
2792 ** [sqlite3_log()] call and is intended to be a [result code] or an
2797 ** In a multi-threaded application, the application-defined logger
2801 ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2802 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2815 ** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2816 ** argument which is interpreted as a boolean in order to enable or disable
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
2837 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2842 ** points to a buffer containing the name of the main database file. If the
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
2873 ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2874 ** is a pointer to an integer and writes into that integer the number of extra
2881 ** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2884 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2892 ** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2893 ** becomes the [statement journal] spill-to-disk threshold.
2896 ** Or if the threshold is -1, statement journals are always held
2899 ** threshold to a value such as 64KiB can greatly reduce the amount of
2902 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2918 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2919 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2941 ** the call worked. ^The [sqlite3_db_config()] interface will return a
2942 ** non-zero [error code] if a discontinued or unsupported configuration option
2949 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2950 ** pointer to a memory buffer to use for lookaside memory.
2957 ** must be aligned to an 8-byte boundary. ^If the second argument to
2958 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2960 ** configuration for a database connection can only be changed when that
2973 ** unchanged. The second parameter is a pointer to an integer into which
2975 ** following this call. The second parameter may be a NULL pointer, in
2983 ** The second parameter is a pointer to an integer into which
2985 ** following this call. The second parameter may be a NULL pointer, in
2989 ** <dd> ^This option is used to enable or disable the two-argument
2991 ** [FTS3] full-text search engine extension.
2996 ** The second parameter is a pointer to an integer into which
2998 ** following this call. The second parameter may be a NULL pointer, in
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.
3012 ** The second parameter is a pointer to an integer into which
3015 ** be a NULL pointer, in which case the new setting is not reported back.
3020 ** schema. ^The sole argument is a pointer to a constant UTF8 string
3022 ** does not make a copy of the new main schema name string, so the application
3028 ** <dd> Usually, when a database in wal mode is closed or detached from a
3030 ** connections at all to the database. If so, it performs a checkpoint
3033 ** is an integer - non-zero to disable checkpoints-on-close, or zero (the
3034 ** default) to enable them. The second parameter is a pointer to an integer
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.
3042 ** a single SQL query statement will always use the same algorithm regardless
3077 ** has a unique 64-bit signed
3081 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
3085 ** the most recent successful [INSERT] into a rowid table or [virtual table]
3096 ** part of committing a transaction (e.g. to flush data accumulated in memory
3104 ** ^(If an [INSERT] occurs within a trigger then this routine will
3109 ** ^An [INSERT] that fails due to a constraint violation is not a
3114 ** encounters a constraint violation, it does not fail. The
3125 ** If a separate thread performs a new [INSERT] on the same
3140 ** without inserting a row into the database.
3155 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3158 ** Changes to a view that are intercepted by
3161 ** DELETE statement run on a view is always zero. Only changes made to real
3165 ** executed while a trigger program is running. This may happen if the
3170 ** <li> ^(Before entering a trigger program the value returned by
3174 ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3177 ** any changes performed by sub-triggers, as the sqlite3_changes()
3178 ** value will be saved and restored after each sub-trigger has run.)^
3182 ** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3184 ** ^If it is used by the second or subsequent such statement within a trigger
3191 ** If a separate thread makes changes on the same database connection
3209 ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3215 ** If a separate thread makes changes on the same database connection
3222 ** CAPI3REF: Interrupt A Long-Running Query
3227 ** called in response to a user action such as pressing "Cancel"
3228 ** or Ctrl-C where the user wants a long query operation to halt
3231 ** ^It is safe to call this routine from a thread different from the
3233 ** is not safe to call this routine with a [database connection] that
3252 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
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
3262 ** currently entered text seems to form a complete SQL statement or
3265 ** appears to be a complete SQL statement. ^A statement is judged to be
3266 ** complete if it ends with a semicolon token and is not a prefix of a
3267 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3270 ** embedded) and thus do not count as a statement terminator. ^Whitespace
3273 ** ^These routines return 0 if the statement is incomplete. ^If a
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.
3295 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3296 ** KEYWORDS: {busy-handler callback} {busy handler}
3299 ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3301 ** an attempt is made to access a database table associated with
3311 ** ^The first argument to the busy handler is a copy of the void* pointer which
3318 ** ^If the callback returns non-zero, then another attempt
3321 ** The presence of a busy handler does not guarantee that it will be invoked
3323 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3326 ** Consider a scenario where one process is holding a read lock that
3327 ** it is trying to promote to a reserved lock and
3328 ** a second process is holding a reserved lock that it is trying
3339 ** ^(There can only be a single busy handler defined for each
3340 ** [database connection]. Setting a new busy handler clears any
3350 ** A busy handler must not close the database connection
3356 ** CAPI3REF: Set A Busy Timeout
3359 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3360 ** for a specified amount of time when a table is locked. ^The handler
3369 ** ^(There can only be a single busy handler for a particular
3382 ** This is a legacy interface that is preserved for backwards compatibility.
3385 ** Definition: A <b>result table</b> is memory data structure created by the
3386 ** [sqlite3_get_table()] interface. A result table records the
3389 ** The table conceptually has a number of rows and columns. But
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
3401 ** A result table might consist of one or more memory allocations.
3402 ** It is not safe to pass a result table directly to [sqlite3_free()].
3403 ** A result table should be deallocated using [sqlite3_free_table()].
3405 ** ^(As an example of the result table format, suppose a query result
3410 ** -----------------------
3432 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3433 ** string of its 2nd parameter and returns a result table to the
3443 ** The sqlite3_get_table() interface is implemented as a wrapper around
3446 ** interface defined here. As a consequence, errors that occur in the
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.
3474 ** released by [sqlite3_free()]. ^Both routines return a
3485 ** returns a pointer to its buffer instead of the number of
3487 ** the number of characters written would be a more useful return
3492 ** guarantees that the buffer is always zero-terminated. ^The first
3495 ** written will be n-1 characters.
3497 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3504 ** ^(The %q option works like %s in that it substitutes a nul-terminated
3506 ** %q is designed for use inside a string literal.)^ By doubling each '\''
3513 ** char *zText = "It's a happy day!";
3528 ** INSERT INTO table1 VALUES('It''s a happy day!')
3535 ** INSERT INTO table1 VALUES('It's a happy day!');
3538 ** This second example is an SQL syntax error. As a general rule you should
3539 ** always use %q instead of %s when inserting text into a string literal.
3543 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
3552 ** The code above will render a correct SQL statement in the zSQL
3553 ** variable even if the zText variable is a NULL pointer.
3556 ** be contained within double-quotes instead of single quotes, and it
3557 ** escapes the double-quote character instead of the single-quote
3559 ** table and column names into a constructed SQL statement.
3575 ** does not include operating-system specific VFS implementation. The
3578 ** ^The sqlite3_malloc() routine returns a pointer to a block
3581 ** memory, it returns a NULL pointer. ^If the parameter N to
3583 ** a NULL pointer.
3586 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3587 ** of a signed 32-bit integer.
3589 ** ^Calling sqlite3_free() with a pointer previously returned
3592 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3595 ** memory might result in a segmentation fault or other severe error.
3596 ** Memory corruption, a segmentation fault, or other severe error
3597 ** might result if sqlite3_free() is called with a non-NULL pointer that
3600 ** ^The sqlite3_realloc(X,N) interface attempts to resize a
3603 ** is a NULL pointer then its behavior is identical to calling
3608 ** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3617 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3618 ** of a 32-bit signed integer.
3620 ** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3624 ** of bytes requested when X was allocated. ^If X is a NULL pointer then
3626 ** the beginning of memory allocation, or if it points to a formerly
3632 ** is always aligned to at least an 8 byte boundary, or to a
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
3650 ** must be either NULL or else pointers obtained from a prior
3655 ** a block of memory after it has been released using
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
3695 ** select random [ROWID | ROWIDs] when inserting new records into a table that
3697 ** the build-in random() and randomblob() SQL functions. This interface allows
3700 ** ^A call to this routine stores N bytes of randomness into buffer P.
3701 ** ^The P parameter can be a NULL pointer.
3704 ** call had N less than one or a NULL pointer for P, then the PRNG is
3707 ** ^If the previous call to this routine had an N of 1 or more and a
3708 ** non-NULL P then the pseudo-randomness is generated
3715 ** CAPI3REF: Compile-Time Authorization Callbacks
3719 ** ^This routine registers an authorizer callback with a particular
3742 ** ^The first parameter to the authorizer callback is a copy of the third
3746 ** to the callback are either NULL pointers or zero-terminated strings
3748 ** Applications must always be prepared to encounter a NULL pointer in any
3754 ** a NULL value in place of the table column that would have
3757 ** columns of a table.
3758 ** ^When a table is referenced by a [SELECT] but no column values are
3759 ** extracted from that table (for example in a query like
3761 ** is invoked once for that table with a column name that is an empty string.
3770 ** example, an application may allow a user to enter arbitrary
3771 ** SQL queries for evaluation by a database. But the application does
3774 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3782 ** ^(Only a single authorizer can be in place on a database connection
3783 ** at a time. Each call to sqlite3_set_authorizer overrides the
3784 ** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3792 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3793 ** statement might be re-prepared during [sqlite3_step()] due to a
3801 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3818 ** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3827 ** The [sqlite3_set_authorizer()] interface registers a callback function
3839 ** is the name of the inner-most trigger or view that is responsible for
3841 ** top-level SQL code.
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
3928 ** ^A trace callback has four arguments: xCallback(T,C,P,X).
3930 ** ^The C argument is a copy of the context pointer passed in as the
3936 ** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3939 ** trigger subprogram. ^The P argument is a pointer to the
3940 ** [prepared statement]. ^The X argument is a pointer to a string which
3942 ** that indicates the invocation of a trigger. ^The callback can compute
3944 ** interface by using the X argument when X begins with "--" and invoking
3950 ** ^The P argument is a pointer to the [prepared statement] and the
3951 ** X argument points to a 64-bit integer which is the estimated of
3956 ** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3957 ** statement generates a single row of result.
3958 ** ^The P argument is a pointer to the [prepared statement] and the
3962 ** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3964 ** ^The P argument is a pointer to the [database connection] object
3977 ** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3981 ** M argument should be the bitwise OR-ed combination of
3992 ** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3995 ** ^The C argument is a copy of the context pointer.
4017 ** interface is to keep a GUI updated during a large query.
4025 ** ^Only a single progress handler may be defined at one time per
4026 ** [database connection]; setting a new progress handler cancels the
4028 ** ^The progress handler is also disabled by setting N to a value less
4031 ** ^If the progress callback returns non-zero, the operation is
4032 ** interrupted. This feature can be used to implement a
4033 ** "Cancel" button on a GUI progress dialog box.
4044 ** CAPI3REF: Opening A New Database Connection
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
4050 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
4053 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4057 ** an English language description of the error following a failure of any
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.
4112 ** a NULL pointer then the default [sqlite3_vfs] object is used.
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
4118 ** It is recommended that when a database filename actually does begin with
4119 ** a ":" character you should prefix the filename with a pathname such as
4122 ** ^If the filename is an empty string, then a private, temporary
4123 ** on-disk database will be created. ^This private database will be
4129 ** begins with "file:", then the filename is interpreted as a URI. ^URI
4133 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4142 ** error is returned to the caller. ^The fragment component of a URI, if
4146 ** which contains the database. ^If the path begins with a '/' character,
4148 ** with a '/' (meaning that the authority section is omitted from the URI)
4149 ** then the path is interpreted as a relative path.
4151 ** is a drive specification (e.g. "C:").)^
4154 ** The query component of a URI may contain parameters that are interpreted
4155 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
4156 ** SQLite and its built-in [VFSes] interpret the
4161 ** a VFS object that provides the operating system interface that should
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
4179 ** or writes from disk is used. ^It is an error to specify a value for
4189 ** a URI filename, its value overrides any behavior requested by setting
4196 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4198 ** is useful for accessing a database on a filesystem that does not
4203 ** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4205 ** read-only media. ^When immutable is set, SQLite assumes that the
4206 ** database file cannot be changed, even by a process with higher
4207 ** privilege, and so the database is opened read-only and all locking
4209 ** property on a database file that does in fact change can result
4215 ** ^Specifying an unknown parameter in the query component of a URI is not an
4231 ** An error. "darkstar" is not a recognized authority.
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
4241 ** default, use a private cache.
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.
4246 ** An error. "readonly" is not a valid option for the "mode" parameter.
4250 ** query components of a URI. A hexadecimal escape sequence consists of a
4251 ** percent sign - "%" - followed by exactly two hexadecimal digits
4252 ** specifying an octet value. ^Before the path or query components of a
4253 ** URI filename are interpreted, they are encoded using UTF-8 and all
4254 ** hexadecimal escape sequences replaced by a single byte containing the
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) */
4289 ** to see if a database file was a URI that contained a specific query
4293 ** a VFS implementation when the flags parameter to xOpen() has one or
4297 ** parameter if it exists or a NULL pointer if P does not appear as a
4298 ** query parameter on F. If P is a query parameter of F
4300 ** a pointer to an empty string.
4302 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4306 ** case or if the value begins with a non-zero number. The
4309 ** if the value begins with a numeric zero. If P is not a query
4313 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4314 ** 64-bit signed integer and returns that integer, or D if P does not
4318 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4319 ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4320 ** is not a database file pathname pointer that SQLite passed into the xOpen
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.
4357 ** case that a second error occurs on a separate thread in between
4380 ** An instance of this object represents a single SQL statement that
4383 ** Think of each SQL statement as a separate computer program. The
4384 ** original SQL text is source code. A prepared statement object
4385 ** is the compiled object code. All SQL must be converted into a
4388 ** The life-cycle of a prepared statement object usually goes like this:
4403 ** CAPI3REF: Run-time Limits
4407 ** on a connection by connection basis. The first parameter is the
4409 ** second parameter is one of the [limit categories] that define a
4413 ** ^If the new limit is a negative number, the limit is unchanged.
4414 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4416 ** set at compile-time by a C preprocessor macro called
4419 ** ^Attempts to increase a limit above its hard upper bound are
4424 ** ^Hence, to find the current value of a limit without changing it,
4425 ** simply invoke this interface with the third parameter set to -1.
4427 ** Run-time limits are intended for use in applications that manage
4429 ** by untrusted external sources. An example application might be a
4434 ** be given much smaller limits designed to prevent a denial of service
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()].
4461 ** <dd>The maximum number of columns in a table definition or in the
4462 ** result set of a [SELECT] or the maximum number of columns in an index
4469 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4472 ** <dd>The maximum number of instructions in a virtual machine program
4475 ** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4478 ** <dd>The maximum number of arguments on a function.</dd>)^
4496 ** <dd>The maximum number of auxiliary worker threads that a single
4524 ** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4525 ** that the prepared statement will be retained for a long time and
4528 ** be used just once or at most a few times and then destroyed using
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.
4557 ** The first argument, "db", is a [database connection] obtained from a
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
4572 ** there is a small performance advantage to passing an nByte parameter that
4574 ** the nul-terminator.
4581 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4584 ** string or a comment) then *ppStmt is set to NULL.
4597 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
4612 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4613 ** and the application would have to make a second call to [sqlite3_reset()]
4620 ** WHERE clause might influence the choice of query plan for a statement,
4622 ** a schema change, on the first [sqlite3_step()] call following any change
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.
4631 ** the extra prepFlags parameter, which is a bit array consisting of zero or
4634 ** sqlite3_prepare_v3() with a zero prepFlags parameter.
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
4694 ** ^(For example, if a prepared statement is created using the SQL
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
4726 ** [virtual tables] might change the database indirectly as a side effect.
4727 ** ^(For example, if an application defines a function "eval()" that
4729 ** change the database file through side-effects:
4744 ** change the configuration of a database connection, they do not make
4754 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4757 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4762 ** interface returns false if S is a NULL pointer. If S is not a
4763 ** NULL pointer and is not a pointer to a valid [prepared statement]
4767 ** to locate all prepared statements associated with a database
4770 ** statements that are holding a transaction open.
4779 ** that can be stored in a database table. SQLite uses dynamic typing
4784 ** Some interfaces require a protected sqlite3_value. Other interfaces
4785 ** will accept either a protected or an unprotected sqlite3_value.
4787 ** whether or not it requires a protected sqlite3_value. The
4788 ** [sqlite3_value_dup()] interface can be used to construct a new
4792 ** a mutex is held. An internal mutex is held for a protected
4794 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4805 ** implementation of [application-defined SQL functions] are protected.
4819 ** sqlite3_context object. ^A pointer to an sqlite3_context object
4820 ** is always first parameter to [application-defined SQL functions].
4821 ** The application-defined SQL function implementation will pass this
4836 ** literals may be replaced by a [parameter] that matches one of following
4853 ** a pointer to the [sqlite3_stmt] object returned from
4868 ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4871 ** ^(In those routines that have a fourth argument, its value is the
4879 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4889 ** is a destructor used to dispose of the BLOB or
4907 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4908 ** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
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
4917 ** associated with the pointer P of type T. ^D is either a NULL pointer or
4918 ** a pointer to a destructor function for P. ^SQLite will invoke the
4919 ** destructor D with a single argument of P when it is finished using
4920 ** P. The T parameter should be a static string, preferably a string
4924 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
4925 ** for the [prepared statement] or with a prepared statement for which
4928 ** routine is passed a [prepared statement] that has been finalized, the
4936 ** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
4966 ** in a [prepared statement]. SQL parameters are tokens of the
4969 ** to the parameters at a later time.
4983 ** CAPI3REF: Name Of A Host Parameter
4987 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
4989 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
4993 ** ^Parameters of the form "?" without a following integer have no name
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()],
5011 ** CAPI3REF: Index Of A Parameter With A Given Name
5016 ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
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
5029 ** CAPI3REF: Reset All Bindings On A Prepared Statement
5033 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5039 ** CAPI3REF: Number Of Columns In A Result Set
5045 ** ^However, just because this routine returns a positive number does not
5046 ** mean that one or more rows of data will be returned. ^A SELECT statement
5047 ** will always have a positive sqlite3_column_count() but depending on the
5055 ** CAPI3REF: Column Names In A Result Set
5058 ** ^These routines return the name assigned to a particular column
5059 ** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
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]
5068 ** reprepared by the first call to [sqlite3_step()] for a particular run
5073 ** (for example during a conversion from UTF-8 to UTF-16) then a
5076 ** ^The name of a result column is the value of the "AS" clause for
5085 ** CAPI3REF: Source Of Data In A Query Result
5088 ** ^These routines provide a means to determine the database, table, and
5089 ** table column that is the origin of a particular result column in
5092 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5097 ** reprepared by the first call to [sqlite3_step()] for a particular run
5099 ** again in a different encoding.
5101 ** ^The names returned are the original un-aliased names of the
5104 ** ^The first argument to these interfaces is a [prepared statement].
5107 ** ^The left-most column is column 0 for these routines.
5110 ** subquery and is not a column value, then all of these functions return
5111 ** NULL. ^These routine might also return NULL if a memory allocation error
5116 ** UTF-16 encoded strings and the other functions return UTF-8.
5119 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5138 ** CAPI3REF: Declared Datatype Of A Query Result
5141 ** ^(The first parameter is a [prepared statement].
5142 ** If this statement is a [SELECT] statement and the Nth column of the
5143 ** returned result set of that [SELECT] is a table column (not an
5146 ** expression or subquery, then a NULL pointer is returned.
5147 ** ^The returned string is always UTF-8 encoded.
5158 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
5160 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5161 ** is declared to contain a particular type does not mean that the
5174 ** After a [prepared statement] has been prepared using any of
5194 ** database locks it needs to do its job. ^If the statement is a [COMMIT]
5196 ** statement. If the statement is not a [COMMIT] and occurs within an
5206 ** is returned each time a new row of data is ready for processing by the
5210 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5213 ** ^With the legacy interface, a more specific error code (for example,
5220 ** Perhaps it was called on a [prepared statement] that has
5226 ** For all versions of SQLite up to and including 3.6.23.1, a call to
5234 ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5236 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5240 ** API always returns a generic error code, [SQLITE_ERROR], following any
5244 ** We admit that this is a goofy design. The problem has been fixed
5255 ** CAPI3REF: Number of columns in a result set
5263 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
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
5292 ** for a completely different meaning. Software that links against both
5308 ** CAPI3REF: Result Values From A Query
5316 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5317 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5318 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5319 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5323 ** <tr><td><b>sqlite3_column_bytes</b><td>→<td>Size of a BLOB
5324 ** or a UTF-8 TEXT result in bytes
5326 ** <td>→ <td>Size of UTF-16
5334 ** ^These routines return information about a single column of the current
5335 ** result row of a query. ^In every case the first argument is a pointer
5343 ** If the SQL statement does not currently point to a valid row, or if the
5352 ** are called from a different thread while any of these routines
5356 ** each return the value of a result column in a specific data format. If
5369 ** After a type conversion, the result of calling sqlite3_column_type()
5372 ** following a type conversion.
5374 ** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
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.
5382 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
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.
5391 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
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.
5407 ** [unprotected sqlite3_value] object. In a multithreaded environment,
5416 ** [application-defined SQL functions] or [virtual tables], not within
5417 ** top-level application code.
5420 ** ^For example, if the internal representation is FLOAT and a text result
5431 ** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5432 ** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5435 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5444 ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5455 ** <li> The initial content is a BLOB and sqlite3_column_text() or
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
5467 ** not invalidate a prior pointer, though of course the content of the buffer
5489 ** ^The pointers returned are valid until a type conversion occurs as
5496 ** ^(If a memory allocation error occurs during the evaluation of any
5497 ** of these routines, a default value is returned. The default value
5498 ** is either the integer 0, the floating point number 0.0, or a NULL
5514 ** CAPI3REF: Destroy A Prepared Statement Object
5517 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5531 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5534 ** resource leaks. It is a grievous error for the application to try to use
5535 ** a prepared statement after it has been finalized. Any use of a prepared
5542 ** CAPI3REF: Reset A Prepared Statement Object
5545 ** The sqlite3_reset() function is called to reset a [prepared statement]
5546 ** object back to its initial state, ready to be re-executed.
5571 ** KEYWORDS: {application-defined SQL function}
5572 ** KEYWORDS: {application-defined SQL functions}
5580 ** and the presence or absence of a destructor callback for
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.
5592 ** ^Any attempt to create a function with a longer name
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
5618 ** the same inputs within a single SQL statement. Most SQL functions are
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
5629 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5643 ** is passed a single argument which is a copy of the application data
5650 ** SQL function is used. ^A function implementation with a non-negative
5651 ** nArg parameter is a better match than a function implementation with
5652 ** a negative nArg. ^A function where the preferred text encoding
5653 ** matches the database encoding is a better
5654 ** match than a function where the encoding is different.
5655 ** ^A function where the encoding difference is between UTF16le and UTF16be
5656 ** is a closer match than a function where the encoding difference is
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 */
5749 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5750 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5752 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5753 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5755 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5756 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5758 ** <tr><td><b>sqlite3_value_bytes</b><td>→<td>Size of a BLOB
5759 ** or a UTF-8 TEXT in bytes
5761 ** <td>→ <td>Size of UTF-16
5774 ** [application-defined SQL functions] and [virtual tables].
5781 ** except that these routines take a single [protected sqlite3_value] object
5782 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
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.
5793 ** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5802 ** sqlite3_value_text(V) is called to extract a text value for that
5804 ** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
5810 ** such a conversion is possible without loss of information (in other
5811 ** words, if the value is a string that looks like a number)
5817 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
5843 ** an [application-defined SQL function] argument V. The subtype
5844 ** information can be used to pass a limited amount of context from
5854 ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
5855 ** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
5856 ** is a [protected sqlite3_value] object even if the input is not.
5857 ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
5861 ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
5862 ** then sqlite3_value_free(V) is a harmless no-op.
5875 ** for a particular aggregate function, SQLite
5876 ** allocates N of memory, zeroes out that memory, and returns a pointer
5887 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5888 ** when first called if N is less than or equal to zero or if a memory
5902 ** The first parameter must be a copy of the
5916 ** ^The sqlite3_user_data() interface returns a copy of
5923 ** the application-defined function is running.
5931 ** ^The sqlite3_context_db_handle() interface returns a copy of
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
5954 ** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
5956 ** value to the application-defined function. ^N is zero for the left-most
5959 ** returns a NULL pointer.
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
5975 ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
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
6038 ** a BLOB containing all zero bytes and N bytes in size.
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
6058 ** routines make a private copy of the error message text before
6062 ** returned by SQLite as a result of an error in a function. ^By default,
6063 ** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6067 ** error indicating that a string or BLOB is too long to represent.
6070 ** error indicating that a memory allocation failed.
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
6102 ** in the string at a byte offset that is less than the value of the 3rd
6106 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6112 ** copy the content of the parameter nor call a destructor on the content
6116 ** then SQLite makes a copy of the result into space obtained
6120 ** the application-defined function to be a copy of the
6122 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6125 ** ^A [protected sqlite3_value] object may always be used where an
6131 ** also associates the host-language pointer P or type T with that
6133 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6134 ** ^If the D parameter is not NULL, then it is a pointer to a destructor
6136 ** when SQLite is finished with P. The T parameter should be a static
6137 ** string and preferably a string literal. The sqlite3_result_pointer()
6141 ** than the one containing the application-defined function that received
6173 ** the result from the [application-defined SQL function] with
6186 ** ^These functions add, remove, or modify a [collation] associated
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().
6213 ** ^The fifth argument, xCallback, is a pointer to the collating function.
6221 ** ^The collating function callback is invoked with a copy of the pArg
6226 ** respectively. A collating function must always return the same answer
6231 ** strings A, B, and C:
6234 ** <li> If A==B then B==A.
6235 ** <li> If A==B and B==C then A==C.
6236 ** <li> If A<B THEN B>A.
6237 ** <li> If A<B and B<C then A<C.
6240 ** If a collating function fails any of the above constraints and that
6253 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6289 ** ^To avoid having to register all collation sequences before a database
6290 ** can be used, a single callback function may be registered with the
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.
6300 ** ^(When the callback is invoked, the first argument passed is a copy
6360 ** Specify the activation key for a SEE database. Unless
6370 ** Specify the activation key for a CEROD database. Unless
6379 ** CAPI3REF: Suspend Execution For A Short Time
6382 ** for at least a number of milliseconds specified in its parameter.
6400 ** ^(If this global variable is made to point to a string which is
6401 ** the name of a folder (a.k.a. directory), then all temporary files
6402 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6404 ** is a NULL pointer, then SQLite performs a search for an appropriate
6408 ** It is required to set a temporary folder on Windows Runtime (WinRT).
6410 ** neither read nor write this variable. This global variable is a relic
6415 ** thread at a time. It is not safe to read or modify this variable
6416 ** if a [database connection] is being used at the same time in a separate
6444 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6445 ** TemporaryFolder->Path->Data();
6448 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6458 ** ^(If this global variable is made to point to a string which is
6459 ** the name of a folder (a.k.a. directory), then all database files
6460 ** specified with a relative pathname and created or accessed by
6461 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6462 ** to be relative to that directory.)^ ^If this variable is a NULL
6464 ** with a relative pathname are relative to the current directory
6468 ** Changing the value of this variable while a database connection is
6469 ** open can result in a corrupt database.
6472 ** thread at a time. It is not safe to read or modify this variable
6473 ** if a [database connection] is being used at the same time in a separate
6493 ** CAPI3REF: Test For Auto-Commit Mode
6497 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6500 ** ^Autocommit mode is disabled by a [BEGIN] statement.
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
6517 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
6521 ** to which a [prepared statement] belongs. ^The [database connection]
6530 ** CAPI3REF: Return The Filename For A Database Connection
6533 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
6536 ** connection D, or if database N is a temporary or in-memory database, then
6537 ** a NULL pointer is returned.
6542 ** to open the database originally was a URI or relative pathname.
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
6552 ** the name of a database on connection D.
6560 ** ^This interface returns a pointer to the next [prepared statement] after
6562 ** then this interface returns a pointer to the first prepared statement
6566 ** The [database connection] pointer D in a call to
6568 ** connection and in particular must not be a NULL pointer.
6576 ** ^The sqlite3_commit_hook() interface registers a callback
6577 ** function to be invoked whenever a transaction is [COMMIT | committed].
6578 ** ^Any callback set by a previous call to sqlite3_commit_hook()
6580 ** ^The sqlite3_rollback_hook() interface registers a callback
6581 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6582 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
6585 ** ^If the callback on a commit hook function returns non-zero,
6586 ** then the commit is converted into a rollback.
6603 ** ^Registering a NULL function disables the callback.
6607 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6608 ** ^The rollback hook is invoked on a rollback that results from a commit
6609 ** hook returning non-zero, just as it would be with any other rollback.
6611 ** ^For the purposes of this API, a transaction is said to have been
6614 ** ^The rollback callback is not invoked if a transaction is
6626 ** ^The sqlite3_update_hook() interface registers a callback function
6628 ** to be invoked whenever a row is updated, inserted or deleted in
6629 ** a [rowid table].
6630 ** ^Any callback set by a previous call to this function
6633 ** ^The second argument is a pointer to the function to invoke when a
6634 ** row is updated, inserted or deleted in a rowid table.
6635 ** ^The first argument to the callback is a copy of the third argument
6653 ** The exceptions defined in this paragraph might change in a future
6686 ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6704 ** shared cache mode should be enabled per-database connection via
6707 ** This interface is threadsafe on processors where writing a
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
6731 ** CAPI3REF: Free Memory Used By A Database Connection
6737 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6745 ** CAPI3REF: Impose A Limit On Heap Size
6762 ** sqlite3_soft_heap_limit64() with a negative argument.
6771 ** <li> Memory accounting is disabled using a combination of the
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],
6801 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
6810 ** CAPI3REF: Extract Metadata About A Column Of A Table
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
6823 ** does not. If the table name parameter T in a call to
6847 ** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
6857 ** ^If the specified table is actually a view, an [error code] is returned.
6860 ** is not a [WITHOUT ROWID] table and an
6887 int *pAutoinc /* OUTPUT: True if column is auto-increment */
6899 ** with various operating-system specific extensions added.
6907 ** If that does not work, it constructs a name "sqlite3_X_init" where the
6908 ** X is consists of the lower-case equivalent of all ASCII alphabetic
6946 ** unprepared to deal with [extension loading], and as a means of disabling
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.)^
6973 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
6998 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7010 ** initialization routine X that was registered using a prior call to
7027 ** The interface to the virtual-table mechanism is currently considered
7029 ** If this is a problem for you, do not use the interface at this time.
7031 ** When the virtual-table mechanism stabilizes, we will declare the
7047 ** This structure, sometimes called a "virtual table module",
7048 ** defines the implementation of a [virtual tables].
7051 ** ^A virtual table module is created by filling in a persistent
7052 ** instance of this structure and passing a pointer to that instance
7054 ** ^The registration remains valid until it is replaced by a different
7101 ** method of a [virtual table module]. The fields under **Inputs** are 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
7124 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
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
7153 ** ^The estimatedCost value is an estimate of the cost of a particular
7154 ** strategy. A cost of N indicates that the cost of the strategy is similar
7155 ** to a linear scan of an SQLite table with N rows. A cost of log(N)
7156 ** indicates that the expense of the operation is similar to that of a
7157 ** binary search on a unique indexed field of an SQLite table with N rows.
7162 ** The xBestIndex method may optionally populate the idxFlags field with a
7163 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7168 ** SQLite also assumes that if a call to the xUpdate() method is made as
7169 ** part of the same statement to delete or update a virtual table row and the
7179 ** If a virtual table extension is
7183 ** therefore only be used if [sqlite3_libversion_number()] returns a
7187 ** sqlite3_libversion_number() returns a value greater than or equal to
7194 int iColumn; /* Column constrained. -1 for ROWID */
7197 int iTermOffset; /* Used internally - xBestIndex should ignore */
7207 unsigned char omit; /* Do not code a test for this constraint */
7232 ** an operator that is part of a constraint term in the wHERE clause of
7233 ** a query that uses a [virtual table].
7246 ** CAPI3REF: Register A Virtual Table Implementation
7249 ** ^These routines are used to register a new [virtual table module] name.
7251 ** creating a new [virtual table] using the module and before using a
7256 ** second parameter. ^The third parameter is a pointer to
7260 ** when a new virtual table is be being created or reinitialized.
7262 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7263 ** is a pointer to a destructor for the pClientData. ^SQLite will
7268 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
7289 ** Every [virtual table module] implementation uses a subclass
7290 ** of this object to describe a particular instance
7296 ** ^Virtual tables methods can set an error message by assigning a
7298 ** take care that any prior string is freed by a call to [sqlite3_free()]
7299 ** prior to assigning a new string to zErrMsg. ^After the error message
7314 ** Every [virtual table module] implementation uses a subclass of the
7322 ** the content of a cursor structure to suit its own needs.
7333 ** CAPI3REF: Declare The Schema Of A Virtual Table
7335 ** ^The [xCreate] and [xConnect] methods of a
7343 ** CAPI3REF: Overload A Function For A Virtual Table
7351 ** ^(This API makes sure a global version of a function with a particular
7353 ** before this API is called, a new function is created.)^ ^The implementation
7356 ** purpose is to be a placeholder function that can be overloaded
7357 ** by a [virtual table].
7362 ** The interface to the virtual-table mechanism defined above (back up
7363 ** to a comment remarkably similar to this one) is currently considered
7365 ** If this is a problem for you, do not use the interface at this time.
7367 ** When the virtual-table mechanism stabilizes, we will declare the
7372 ** CAPI3REF: A Handle To An Open BLOB
7386 ** CAPI3REF: Open A BLOB For Incremental I/O
7390 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7404 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7406 ** read-only access.
7418 ** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7421 ** <li> ^(The specified column of row iRow contains a value that is not
7422 ** a TEXT or BLOB value)^,
7426 ** column zColumn is part of a [child key] definition and the blob is
7434 ** A BLOB referenced by sqlite3_blob_open() may be read using the
7436 ** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7438 ** interface. However, the column, table, or database of a [BLOB handle]
7441 ** ^(If the row that a BLOB handle points to is modified by an
7442 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7444 ** This is true if any column of the row is changed, even a column
7447 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7448 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
7453 ** the opened blob. ^The size of a blob may not be changed by this
7454 ** interface. Use the [UPDATE] SQL command to change the size of a
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.
7461 ** To avoid a resource leak, every open [BLOB handle] should eventually
7462 ** be released by a call to [sqlite3_blob_close()].
7479 ** CAPI3REF: Move a BLOB Handle to a New Row
7483 ** to a different row of the same database table. ^The new row is identified
7486 ** remain the same. Moving an existing [BLOB handle] to a new row is
7487 ** faster than closing the existing handle and opening a new one.
7489 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7490 ** it must exist and there must be either a blob or text value stored in
7492 ** it does not contain a blob or text value, or if another error occurs, an
7504 ** CAPI3REF: Close A BLOB Handle
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
7517 ** Calling this function with an argument that is not a NULL pointer or an
7519 ** with a null pointer (such as would be returned by a failed call to
7520 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7521 ** is passed a valid open blob handle, the values returned by the
7533 ** blob content; they cannot change the size of a blob.
7535 ** This routine only works on a [BLOB handle] which has been created
7536 ** by a prior successful call to [sqlite3_blob_open()] and which has not
7543 ** CAPI3REF: Read Data From A BLOB Incrementally
7546 ** ^(This function is used to read data from an open [BLOB handle] into a
7547 ** caller-supplied buffer. N bytes of data are copied into buffer Z
7562 ** This routine only works on a [BLOB handle] which has been created
7563 ** by a prior successful call to [sqlite3_blob_open()] and which has not
7572 ** CAPI3REF: Write Data Into A BLOB Incrementally
7575 ** ^(This function is used to write data into an open [BLOB handle] from a
7576 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
7590 ** not possible to increase the size of a BLOB using this API.
7604 ** This routine only works on a [BLOB handle] which has been created
7605 ** by a prior successful call to [sqlite3_blob_open()] and which has not
7616 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7618 ** with the underlying operating system. Most SQLite builds come with a
7623 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7625 ** ^Names are zero-terminated UTF-8 strings.
7626 ** ^If there is no match, a NULL pointer is returned.
7634 ** same name are registered, the behavior is undefined. If a
7635 ** VFS is registered with a name that is NULL or an empty string,
7638 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7656 ** is selected automatically at compile-time. The following
7665 ** The SQLITE_MUTEX_NOOP implementation is a set of routines
7667 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7672 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7674 ** application must supply a custom mutex implementation using the
7679 ** ^The sqlite3_mutex_alloc() routine allocates a new
7680 ** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7704 ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
7706 ** The mutex implementation does not need to make a distinction
7708 ** not want to. SQLite will only request a recursive mutex in
7709 ** cases where it really needs one. If a faster non-recursive mutex
7711 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
7715 ** a pointer to a static preexisting mutex. ^Nine static mutexes are
7724 ** returns a different mutex on every call. ^For the static
7728 ** ^The sqlite3_mutex_free() routine deallocates a previously
7729 ** allocated dynamic mutex. Attempting to deallocate a static
7733 ** to enter a mutex. ^If another thread is already within the mutex,
7749 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
7755 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
7756 ** behave as no-ops.
7769 ** An instance of this structure defines the low-level routines
7773 ** sufficient, however the application has the option of substituting a custom
7775 ** does not provide a suitable implementation. In this case, the application
7809 ** above silently ignore any invocations that pass a NULL pointer instead
7810 ** of a valid mutex handle. The implementations of the methods defined
7812 ** of passing a NULL pointer instead of a valid mutex handle are undefined
7814 ** it is passed a NULL pointer).
7819 ** xMutexInit() must be no-ops.
7823 ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
7824 ** memory allocation for a fast or recursive mutex.
7864 ** If the argument to sqlite3_mutex_held() is a NULL pointer then
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
7871 ** interface should also return 1 when given a NULL pointer.
7881 ** The [sqlite3_mutex_alloc()] interface takes a single argument
7885 ** next. Applications that override the built-in mutex logic must be
7901 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
7906 ** CAPI3REF: Retrieve the mutex for a database connection
7909 ** ^This interface returns a pointer the [sqlite3_mutex] object that
7912 ** ^If the [threading mode] is Single-thread or Multi-thread then this
7913 ** routine returns a NULL pointer.
7918 ** CAPI3REF: Low-Level Control Of Database Files
7921 ** ^The [sqlite3_file_control()] interface makes a direct call to the
7923 ** with a particular database identified by the second argument. ^The
7927 ** ^A NULL pointer can be used in place of "main" to refer to the
7935 ** a pointer to the underlying [sqlite3_file] object to be written into
7937 ** case is a short-circuit path which does not actually invoke the
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
8097 ** in bytes. Since a single thread may only have one scratch allocation
8141 ** about a single [database connection]. ^The first argument is the
8154 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8155 ** non-zero [error code] on failure.
8171 ** The [sqlite3_db_status()] interface will return a non-zero error code
8172 ** if a discontinued or unsupported verb is invoked.
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;
8207 ** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8220 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8251 ** If an IO or other error occurs while writing a page to disk, the effect
8288 ** that the prepared statement is using a full table scan rather than
8292 ** a [prepared statement]. The first argument is the prepared statement
8294 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8315 ** a table as part of a full table scan. Large numbers for this counter
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
8335 ** used as a proxy for the total work done by the prepared statement.
8346 ** been run. A single "run" for the purposes of this counter is one
8347 ** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8354 ** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8383 ** The sqlite3_pcache_page object represents a single page in the
8405 ** By implementing a
8409 ** determine exactly which parts of a database file are cached and for
8414 ** The built-in page cache is recommended for most uses.
8425 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8429 ** built-in default page cache is used instead of the application defined
8448 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
8452 ** be allocated by the cache. ^szPage will always a power of two. ^The
8453 ** second parameter szExtra is a number of bytes of extra storage
8455 ** a number less than 250. SQLite will use the
8460 ** created will be used to cache database pages of a file stored on disk, or
8461 ** false if it is used for an in-memory database. The cache implementation
8463 ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
8464 ** never invoke xUnpin() except to deliberately delete a page.
8465 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8467 ** ^Hence, a cache created with bPurgeable false will
8472 ** suggested maximum cache-size (number of pages stored by) the cache
8483 ** The xFetch() method locates a page in the cache and returns a pointer to
8484 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8485 ** The pBuf element of the returned sqlite3_pcache_page object will be a
8486 ** pointer to a buffer of szPage bytes used to store the content of a
8488 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
8496 ** implementation must return a pointer to the page buffer with its content
8503 ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
8504 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
8506 ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
8507 ** NULL if allocating a new page is effectively impossible.
8510 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
8511 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
8517 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
8518 ** as its second argument. If the third parameter, discard, is non-zero,
8525 ** The cache must not perform any reference counting. A single
8543 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
8552 ** is not obligated to free any memory, but well-behaved implementations should
8599 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
8611 ** for copying in-memory databases to or from persistent files.
8615 ** ^SQLite holds a write transaction open on the destination database file
8617 ** ^The source database is read-locked only while it is being read;
8619 ** ^Thus, the backup may be performed on a live source database without
8623 ** ^(To perform a backup operation:
8650 ** ^A call to sqlite3_backup_init() will fail, returning NULL, if
8651 ** there is already a read or read-write transaction open on the
8660 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
8677 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
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
8711 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
8714 ** sqlite3_backup_step(), the source database may be modified mid-way
8716 ** external process or via a database connection other than the one being
8731 ** active write-transaction on the destination database is rolled back.
8733 ** and may not be used following a call to sqlite3_backup_finish().
8738 ** ^If an out-of-memory condition or IO error occurred during any prior
8742 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
8743 ** is not a permanent error and does not affect the return value of
8755 ** sqlite3_backup_step(). If the source database is modified in a way that
8764 ** purposes while a backup operation is underway or being initialized.
8775 ** nevertheless. Use of the destination database connection while a
8776 ** backup is in progress might also also cause a mutex deadlock.
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.
8811 ** ^This API may be used to register a callback that SQLite will invoke
8814 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
8818 ** ^Shared-cache locks are released when a database connection concludes
8821 ** ^When a connection (known as the blocked connection) fails to obtain a
8822 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
8827 ** the first argument to register for a callback that will be invoked
8832 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
8833 ** there is a chance that the blocking connection will have already
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,
8847 ** called with a NULL pointer as its second argument, then any existing
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,
8868 ** When a blocking connections transaction is concluded, there may be
8869 ** more than one blocked connection that has registered for an unlock-notify
8879 ** Assuming that after registering for an unlock-notify callback a
8881 ** action (a reasonable assumption), then using this API may cause the
8888 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
8889 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
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
8894 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
8896 ** registered for an unlock-notify callback on the conclusion of connection
8897 ** C's transaction, where connection C is waiting on connection A. ^Any
8902 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
8904 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
8908 ** sqlite3_unlock_notify() results in the unlock-notify callback being
8909 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
8913 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
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
8979 ** ^The [sqlite3_log()] interface writes a message into the [error log]
8993 ** a fixed-length buffer on the stack. If the log message is longer than
8994 ** a few hundred characters, it will be truncated to the length of the
9000 ** CAPI3REF: Write-Ahead Log Commit Hook
9003 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
9004 ** is invoked each time data is committed to a database in wal mode.
9007 ** the associated write-lock on the database released)^, so the implementation
9011 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
9012 ** registering the callback. ^The second is a copy of the database handle.
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,
9022 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
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
9043 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9046 ** after committing a transaction if there are N or
9047 ** more frames in the [write-ahead log] file. ^Passing zero or
9048 ** a negative value as the nFrame parameter disables automatic
9052 ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9062 ** ^Every new [database connection] defaults to having the auto-checkpoint
9063 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9066 ** for a particular application.
9071 ** CAPI3REF: Checkpoint a database
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
9083 ** This interface used to be the only way to cause a checkpoint to
9086 ** compatibility and as a convenience for applications that need to manually
9087 ** start a callback but which do not need the full power (and corresponding
9093 ** CAPI3REF: Checkpoint a database
9096 ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9099 ** ^(The M parameter must be a valid [checkpoint mode]:)^
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])
9130 ** to a successful return.
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
9144 ** any other process is running a checkpoint operation at the same time, the
9145 ** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
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
9159 ** ^If parameter zDb is NULL or points to a zero length string, then the
9172 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9173 ** zDb is not NULL (or a zero length string) and is not the name of any
9210 ** of a [virtual table] implementation to configure
9236 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9241 ** If X is non-zero, then the virtual table implementation guarantees
9245 ** is able to roll back a statement or database transaction, and abandon
9252 ** must do so within the [xUpdate] method. If a call to the
9266 ** This function may only be called from within a call to the [xUpdate] method
9267 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9280 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
9283 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
9285 ** [SQLITE_ABORT] is also a [result code].
9298 ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
9301 ** When the value returned to V is a string, space to hold that string is
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
9358 ** compile-time option.
9363 ** ^The requested measurement is written into a variable pointed to by
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
9402 ** to disk. A dirty page may be in use if a database cursor created by an
9403 ** active SQL statement is reading from it, or if it is page 1 of a database
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.
9434 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
9436 ** on a database table.
9437 ** ^At most one preupdate hook may be registered at a time on a single
9441 ** with a NULL pointer as the second parameter.
9449 ** ^The second parameter to the preupdate callback is a pointer to
9462 ** For an UPDATE or DELETE operation on a [rowid table], the sixth
9464 ** row being modified or deleted. For an INSERT operation on a rowid table,
9465 ** or any operation on a WITHOUT ROWID table, the value of the sixth
9466 ** parameter is undefined. For an INSERT or UPDATE on a rowid table the
9474 ** provide additional information about a preupdate event. These routines
9475 ** may only be called from within a preupdate callback. Invoking any of
9476 ** these routines from outside of a preupdate callback or with a
9484 ** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
9485 ** a [protected sqlite3_value] that contains the value of the Nth column of
9493 ** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
9494 ** a [protected sqlite3_value] that contains the value of the Nth column of
9503 ** callback was invoked as a result of a direct insert, update, or delete
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
9520 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
9531 ** CAPI3REF: Low-level system error code
9534 ** number that caused the most recent I/O error or failure to open a file.
9535 ** The return value is OS-dependent. For example, on unix systems, after
9547 ** An instance of the snapshot object records the state of a [WAL mode]
9551 ** same database file can each be reading a different historical version
9552 ** of the database file. When a [database connection] begins a read
9556 ** by the reader until a new read transaction is started.
9559 ** version of the database file so that it is possible to later open a new read
9564 ** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer
9573 ** CAPI3REF: Record A Database Snapshot
9576 ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
9579 ** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
9581 ** If there is not already a read-transaction open on schema S when
9592 ** <li> Schema S of [database connection] D must be a [WAL mode] database.
9594 ** <li> There must not be a write transaction open on schema S of database
9599 ** that a snapshot cannot be taken on a wal mode database with no wal
9606 ** whether or not a read transaction is opened on schema S is undefined.
9608 ** The [sqlite3_snapshot] object returned from a successful call to
9610 ** to avoid a memory leak.
9613 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9622 ** CAPI3REF: Start a read transaction on an historical snapshot
9625 ** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a
9633 ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
9637 ** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the
9639 ** ^A [snapshot] will fail to open if it has been overwritten by a
9641 ** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
9643 ** schema S is in [WAL mode]. A database connection might not know
9647 ** (Hint: Run "[PRAGMA application_id]" against a newly opened
9651 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9660 ** CAPI3REF: Destroy a snapshot
9665 ** using this routine to avoid a memory leak.
9668 ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
9690 ** Otherwise, this API returns a negative value if P1 refers to an older
9692 ** snapshot, and a positive value if P1 is a newer snapshot than P2.
9700 ** CAPI3REF: Recover snapshots from a wal file
9703 ** If all connections disconnect from a database file but do not perform
9704 ** a checkpoint, the existing wal file is opened along with the database
9713 ** sqlite3_snapshot_open(). It is an error if there is already a read
9714 ** transaction open on the database, or if the database is not a wal mode
9739 ** a legal notice, here is a blessing:
9759 /* The double-precision datatype used by RTree depends on the
9760 ** SQLITE_RTREE_INT_ONLY compile-time option.
9769 ** Register a geometry callback named zGeom that can be used as part of an
9770 ** R-Tree geometry query as follows:
9783 ** A pointer to a structure of the following type is passed as the first
9795 ** Register a 2nd-generation geometry callback named zScore that can be
9796 ** used as part of an R-Tree geometry query as follows:
9810 ** A pointer to a structure of the following type is passed as the
9815 ** sqlite3_rtree_geometry. This structure is a subclass of
9877 ** CAPI3REF: Create A New Session Object
9879 ** Create a new session object attached to database handle db. If successful,
9880 ** a pointer to the new object is written to *ppSession and SQLITE_OK is
9884 ** It is possible to create multiple session objects attached to a single
9895 ** is not possible for an application to register a pre-update hook on a
9897 ** it possible to create a session object attached to a database handle for
9898 ** which a pre-update hook is already defined. The results of attempting
9913 ** CAPI3REF: Delete A Session Object
9915 ** Delete a session object previously allocated using
9916 ** [sqlite3session_create()]. Once a session object has been deleted, the
9928 ** CAPI3REF: Enable Or Disable A Session Object
9930 ** Enable or disable the recording of changes by a session object. When
9931 ** enabled, a session object records changes made to the database. When
9932 ** disabled - it does not. A newly created session object is enabled.
9934 ** details regarding how enabling and disabling a session object affects
9937 ** Passing zero to this function disables the session. Passing a value
9938 ** greater than zero enables it. Passing a value less than zero is a
9939 ** no-op, and may be used to query the current state of the session.
9949 ** Each change recorded by a session object is marked as either direct or
9950 ** indirect. A change is marked as indirect if either:
9956 ** instead of directly as a result of a users SQL statement.
9959 ** If a single row is affected by more than one operation within a session,
9966 ** is set. Passing a value less than zero does not modify the current value
9976 ** CAPI3REF: Attach A Table To A Session Object
9978 ** If argument zTab is not NULL, then it is the name of a table to attach
9988 ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
9991 ** KEY may consist of a single column, or may be a composite key.
9994 ** is it an error if the named table does not have a PRIMARY KEY. However,
10009 ** CAPI3REF: Set a table filter on a Session Object.
10014 ** If xFilter returns 0, changes is not tracked. Note that once a table is
10027 ** CAPI3REF: Generate A Changeset From A Session Object
10029 ** Obtain a changeset containing changes to the tables attached to the
10031 ** set *ppChangeset to point to a buffer containing the changeset
10036 ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10037 ** each representing a change to a single row of an attached table. An INSERT
10038 ** change contains the values of each field of a new database row. A DELETE
10039 ** contains the original values of each field of a deleted database row. An
10041 ** database row along with the updated values for each updated non-primary-key
10042 ** column. It is not possible for an UPDATE change to represent a change that
10043 ** modifies the values of primary key columns. If such a change is made, it
10044 ** is represented in a changeset as a DELETE followed by an INSERT.
10047 ** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
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
10053 ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10056 ** The contents of a changeset may be traversed using an iterator created
10057 ** using the [sqlite3changeset_start()] API. A changeset may be applied to
10058 ** a database with a compatible schema using the [sqlite3changeset_apply()]
10061 ** Within a changeset generated by this function, all changes related to a
10063 ** a changeset or when applying a changeset to a database, all changes related
10064 ** to a single table are processed before moving on to the next table. Tables
10065 ** are sorted in the same order in which they were attached (or auto-attached)
10067 ** a single table are stored is undefined.
10069 ** Following a successful call to this function, it is the responsibility of
10075 ** Once a table has been attached to a session object, the session object
10079 ** recorded once - the first time a row with said primary key is inserted,
10082 ** There is one exception to the previous paragraph: when a row is inserted,
10083 ** updated or deleted, if one or more of its primary key columns contain a
10086 ** The session object therefore accumulates two types of records - those
10088 ** a new record) and those that consist of the primary key values and the
10090 ** or updates a record).
10098 ** for a row with a matching primary key. If one is found, an INSERT
10103 ** queried for a row with a matching primary key. If such a row is
10104 ** found and one or more of the non-primary key fields have been
10106 ** the changeset. Or, if no such row is found in the table, a DELETE
10107 ** change is added to the changeset. If there is a row with a matching
10112 ** This means, amongst other things, that if a row is inserted and then later
10113 ** deleted while a session object is active, neither the insert nor the delete
10114 ** will be present in the changeset. Or if a row is deleted and then later a
10115 ** row with the same primary key values inserted while a session object is
10117 ** a DELETE and an INSERT.
10119 ** When a session object is disabled (see the [sqlite3session_enable()] API),
10121 ** This may appear to have some counter-intuitive effects if a single row
10122 ** is written to more than once during a session. For example, if a row
10123 ** is inserted while a session object is enabled, then later deleted while
10126 ** Or, if one field of a row is updated while a session is disabled, and
10137 ** CAPI3REF: Load The Difference Between Tables Into A Session
10142 ** does not have a primary key, this function is a no-op (but does not return
10145 ** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10147 ** a table compatible with the table attached to the session by this function.
10148 ** A table is considered compatible if it:
10161 ** This function adds a set of changes to the session object that could be
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
10178 ** To clarify, if this function is called and then a changeset constructed
10188 ** may be set to point to a buffer containing an English language error
10201 ** CAPI3REF: Generate A Patchset From A Session Object
10203 ** The differences between a patchset and a changeset are that:
10212 ** A patchset blob may be used with up to date versions of all
10214 ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
10215 ** attempting to use a patchset blob with old versions of the
10218 ** Because the non-primary key "old.*" fields are omitted, no
10219 ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
10223 ** Changes within a patchset are ordered in the same way as for changesets
10225 ** a single table are grouped together, tables appear in the order in which
10235 ** CAPI3REF: Test if a changeset has recorded any changes.
10237 ** Return non-zero if no changes to attached tables have been recorded by
10242 ** [sqlite3session_changeset()] on the session handle may still return a
10243 ** changeset that contains no changes. This can happen when a row in
10245 ** are restored. However, if this function returns non-zero, then it is
10246 ** guaranteed that a call to sqlite3session_changeset() will return a
10252 ** CAPI3REF: Create An Iterator To Traverse A Changeset
10254 ** Create an iterator used to iterate through the contents of a changeset.
10259 ** The following functions can be used to advance and query a changeset
10277 ** that apply to a single table are grouped together. This means that when
10278 ** an application iterates through a changeset using an iterator created by
10279 ** this function, all changes that relate to a single table are visited
10280 ** consecutively. There is no chance that the iterator will visit a change
10292 ** CAPI3REF: Advance A Changeset Iterator
10296 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10304 ** no error occurs and the iterator points to a valid change after a call
10316 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
10319 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10324 ** If argument pzTab is not NULL, then *pzTab is set to point to a
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
10332 ** [sqlite3session_indirect()] for a description of direct and indirect
10350 ** CAPI3REF: Obtain The Primary Key Definition Of A Table
10352 ** For each modified table, a changeset includes the following:
10369 ** If this function is called when the iterator does not point to a valid
10376 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
10381 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
10384 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10395 ** If successful, this function sets *ppValue to point to a protected
10411 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
10414 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10425 ** If successful, this function sets *ppValue to point to a protected
10429 ** a new value for the requested column, *ppValue is set to NULL and
10444 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
10446 ** This function should only be used with iterator objects passed to a
10447 ** conflict-handler callback by [sqlite3changeset_apply()] with either
10456 ** If successful, this function sets *ppValue to point to a protected
10458 ** "conflicting row" associated with the current conflict-handler callback
10487 ** CAPI3REF: Finalize A Changeset Iterator
10494 ** function with an iterator passed to a conflict-handler by
10498 ** If an error was encountered within a call to an sqlite3changeset_xxx()
10502 ** returned. This is to allow the following pattern (pseudo-code):
10516 ** CAPI3REF: Invert A Changeset
10518 ** This function is used to "invert" a changeset object. Applying an inverted
10519 ** changeset to a database reverses the effects of applying the uninverted
10524 ** <li> Each INSERT change is changed to a DELETE, and
10531 ** If successful, a pointer to a buffer containing the inverted changeset
10537 ** on the *ppOut pointer to free the buffer allocation following a successful
10540 ** WARNING/TODO: This function currently assumes that the input is a valid
10551 ** This function is used to concatenate two changesets, A and B, into a
10552 ** single changeset. The result is a changeset equivalent to applying
10553 ** changeset A followed by changeset B.
10574 void *pA, /* Pointer to buffer containing changeset A */
10588 ** CAPI3REF: Create A New Changegroup Object
10591 ** (or patchsets) into a single changeset (or patchset). A single changegroup
10596 ** a pointer to a new sqlite3_changegroup object before returning. The caller
10597 ** should eventually free the returned object using a call to
10604 ** <li> It is created using a call to sqlite3changegroup_new().
10610 ** by the application via a call to sqlite3changegroup_output().
10612 ** <li> The object is deleted using a call to sqlite3changegroup_delete().
10625 ** CAPI3REF: Add A Changeset To A Changegroup
10630 ** If the buffer contains a patchset, then all prior calls to this function
10632 ** the buffer contains a changeset, so must have the earlier calls to this
10637 ** their PRIMARY KEY columns. A change in the changeset is considered to
10638 ** apply to the same row as a change already present in the changegroup if
10643 ** contain changes that apply to a single row, the final contents of the
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>
10688 ** If the new changeset contains changes to a table that is already present
10693 ** returned. Or, if an out-of-memory condition occurs during processing, this
10702 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
10704 ** Obtain a buffer containing a changeset (or patchset) representing the
10706 ** were themselves changesets, the output is a changeset. Or, if the
10707 ** inputs were patchsets, the output is also a patchset.
10710 ** sqlite3session_patchset() functions, all changes related to a single
10720 ** is returned and the output variables are set to the size of and a
10722 ** responsibility of the caller to eventually free the buffer using a
10732 ** CAPI3REF: Delete A Changegroup Object
10737 ** CAPI3REF: Apply A Changeset To A Database
10739 ** Apply a changeset to a database. This function attempts to update the
10746 ** the table name as the second argument, and a copy of the context pointer
10749 ** the table. Otherwise, if the return value is non-zero or the xFilter
10754 ** tests that the target database contains a compatible table. A table is
10767 ** changes associated with the table are applied. A warning message is issued
10771 ** For each change for which there is a compatible table, an attempt is made
10773 ** change. If a change cannot be applied cleanly, the conflict handler
10775 ** invoked. A description of exactly when the conflict handler is invoked for
10779 ** of passing anything other than a valid function pointer as the xConflict
10786 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
10790 ** returned by each invocation of the conflict-handler function. Refer to
10797 ** contains a row with the same primary key value (or values) as the
10799 ** stored in all non-primary key columns also match the values stored in
10802 ** If a row with matching primary key values is found, but one or more of
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]
10816 ** (which can only happen if a foreign key constraint is violated), the
10817 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
10829 ** contains a row with the same primary key values, the conflict handler
10836 ** This includes the case where the INSERT operation is re-attempted because
10842 ** contains a row with the same primary key value (or values) as the
10844 ** stored in all modified non-primary key columns also match the values
10847 ** If a row with matching primary key values is found, but one or more of
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
10872 ** All changes made by this function are enclosed in a savepoint transaction.
10873 ** If any other error (aside from a constraint failure when attempting to
10897 ** Values that may be passed as the second argument to a conflict-handler.
10902 ** when processing a DELETE or UPDATE change if a row with the required
10904 ** (non primary-key) fields modified by the update do not contain the
10912 ** argument when processing a DELETE or UPDATE change if a row with the
10927 ** If foreign key handling is enabled, and applying a changeset leaves the
10928 ** database in a state containing foreign key violations, the conflict
10940 ** If any other constraint violation occurs while applying a change (i.e.
10941 ** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
10958 ** A conflict handler callback must return one of the following three values.
10962 ** If a conflict handler returns this value no special action is taken. The
10977 ** handler, then the conflicting row is removed from the database and a
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
11007 ** require that the entire changeset be stored in a single buffer in memory.
11008 ** Similarly, those that return a changeset or patchset do so by returning
11009 ** a pointer to a single large buffer allocated using sqlite3_malloc().
11010 ** Normally this is convenient. However, if an application running in a
11011 ** low-memory environment is required to handle very large changesets, the
11014 ** In order to avoid this problem, instead of a single large buffer, input
11015 ** is passed to a streaming API functions by way of a callback function that
11017 ** required. In all cases, a pair of API function parameters such as
11032 ** argument passed is a copy of the supplied pIn context pointer. The second
11033 ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
11040 ** returns a copy of the error code to the caller.
11049 ** return them in chunks by way of a callback function instead of via a
11050 ** pointer to a single large buffer. In this case, a pair of parameters such
11066 ** the application. The first parameter passed to each call is a copy of the
11068 ** points to a buffer nData bytes in size containing the chunk of output
11072 ** is immediately abandoned and the streaming API function returns a copy
11076 ** parameter set to a value less than or equal to zero. Other than this,
11148 ** a legal notice, here is a blessing:
11192 const unsigned char *a; member
11200 ** Return a copy of the context pointer the extension function was
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
11234 ** current document. If successful, (*pz) is set to point to a buffer
11235 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
11255 ** (i.e. if it is a contentless table), then this API always returns 0.
11267 ** set to -1.
11283 ** of the current query. Specifically, a query equivalent to:
11287 ** with $p set to a phrase equivalent to the phrase iPhrase of the
11293 ** Invoking Api.xUserData() returns a copy of the pointer passed as
11313 ** Each extension function is allocated a single auxiliary data slot for
11315 ** more than once for a single FTS query, then all invocations share a
11337 ** If the bClear argument is non-zero, then the auxiliary data is cleared
11351 ** method, to iterate through all instances of a single query phrase within
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
11374 ** (i.e. if it is a contentless table), then this API always iterates
11375 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
11383 ** of iterating through all instances of a phrase in the current row, these
11385 ** that contain one or more instances of a specified phrase. For example:
11389 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
11391 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
11398 ** "detail=none" "content=" option (i.e. if it is a contentless table),
11400 ** xPhraseFirstColumn() set iCol to -1).
11456 ** Applications may also register custom tokenizer types. A tokenizer
11457 ** is registered by providing fts5 with a populated instance of the
11463 ** This function is used to allocate and initialize a tokenizer instance.
11464 ** A tokenizer instance is required to actually tokenize text.
11466 ** The first argument passed to this function is a copy of the (void*)
11469 ** The second and third arguments are an array of nul-terminated strings
11481 ** This function is invoked to delete a tokenizer handle previously
11487 ** by argument pText. pText may or may not be nul-terminated. The first
11488 ** argument passed to this function is a pointer to an Fts5Tokenizer object
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
11502 ** a bareword or quoted string specified as part of the query.
11504 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
11506 ** followed by a "*" character, indicating that the last token
11507 ** returned by the tokenizer will be treated as a token prefix.
11509 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
11512 ** on a columnsize=0 database.
11516 ** be invoked. The first argument to it should be a copy of the pointer
11518 ** arguments are a pointer to a buffer containing the token text, and the
11532 ** immediately return a copy of the xToken() return value. Or, if the
11540 ** Custom tokenizers may also support synonyms. Consider a case in which a
11541 ** user wishes to query for a phrase such as "first place". Using the
11542 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
11550 ** <ol><li> By mapping all synonyms to a single token. In this case, the
11559 ** <li> By adding multiple synonyms for a single term to the FTS index.
11561 ** provide multiple synonyms for a single term within the document.
11569 ** first token in the MATCH query and FTS5 effectively runs a query
11576 ** still appears to contain just two phrases - "(first OR 1st)"
11577 ** being treated as a single phrase.
11579 ** <li> By adding multiple synonyms for a single term to the FTS index.
11581 ** provides multiple synonyms for each token. So that when a
11587 ** when tokenizing query text (it should not - to do would be
11594 ** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
11595 ** is considered to supply a synonym for the previous token. For example,
11596 ** when parsing the document "I won first place", a tokenizer that supports
11608 ** xToken() is called. Multiple synonyms may be specified for a single token
11610 ** There is no limit to the number of synonyms that may be provided for a
11631 ** Method (2) offers a midpoint between (1) and (3). Using this method,
11632 ** a query such as '1s*' will match documents that contain the literal
11634 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
11686 /* Create a new tokenizer */
11703 /* Create a new auxiliary function */
11730 ** autoconf-based build
11743 ** a legal notice, here is a blessing:
11755 ** The maximum length of a TEXT or BLOB in bytes. This also
11756 ** limits the size of a row in a table or index.
11758 ** The hard limit is the ability of a 32-bit signed integer
11759 ** to count the size: 2^31-1 or 2147483647.
11768 ** * Columns in a table
11770 ** * Columns in a view
11772 ** * Terms in the result set of a SELECT statement
11773 ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
11777 ** tell you that in a well-normalized database, you usually should
11778 ** not have more than a dozen or so columns in any table. And if
11779 ** that is the case, there is no point in having more than a few
11787 ** The maximum length of a single SQL statement in bytes.
11803 ** A value of 0 used to mean that the limit was not enforced.
11812 ** The maximum number of terms in a compound SELECT statement.
11814 ** level of recursion for each term. A stack overflow can result
11816 ** never has more than 3 or 4 terms. Use a value of 0 to disable
11817 ** any limit on the number of terms in a compount SELECT.
11824 ** The maximum number of opcodes in a VDBE program.
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
11871 ** The maximum value of a ?nnn wildcard that the parser will accept.
11877 /* Maximum page size. The upper bound on this value is 65536. This a limit
11878 ** imposed by the use of 16-bit offsets within each page.
11882 ** a library that is technically incompatible with an SQLite library
11883 ** compiled with a different limit. If a process operating on a database
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
11895 ** The default size of a database page.
11908 ** device characteristics (sector-size and atomic write() support),
11909 ** SQLite may choose a larger value. This constant is the maximum value
11925 ** This value can be lowered (or raised) at run-time using that the
11933 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
11943 ** A value of 1 means that a trigger program will not be able to itself
11944 ** fire any triggers. A value of 0 means that no trigger programs at all
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 */
11977 ** to generate appropriate macros for a wide range of compilers.
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 */
12004 ** A macro to hint to the compiler that a function should not be
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
12081 ** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
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
12184 ** Sometimes we need a small amount of code such as a variable initialization
12185 ** to setup for a later assert() statement. We do not want this code to
12201 ** are included in a few cases in order to enhance the resilience
12202 ** of SQLite to unexpected behavior - to make the code "self-healing"
12208 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
12227 ** during a normal build. The following macro can be used to disable tests
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.
12279 ** The macro unlikely() is a hint that surrounds a boolean
12281 ** a boolean expression that is usually true. These hints could,
12294 ** a legal notice, here is a blessing:
12301 ** This is the header file for the generic hash-table implementation
12311 /* A complete hash table is an instance of the following structure.
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.
12322 ** There are Hash.htsize buckets. Each bucket points to a spot in
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.
12327 ** by a linear search of the global list. For small tables, the
12329 ** in the table, it is faster to do a linear search than to manage
12343 ** structure. All elements are stored on a single doubly-linked list.
12355 ** Access routines. To delete, insert a NULL pointer.
12363 ** Macros for looping over all elements of a hash table. The idiom is
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 */
12381 ** Number of entries in a hash table
12383 /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
12559 /* Flags that can be added to a token code when it is not
12560 ** being stored in a u8: */
12572 ** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
12575 ** should not be used in production. This is a performance measurement
12580 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
12584 ** If compiling for a processor that lacks floating point support,
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.
12634 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
12635 ** on the command-line
12663 ** pagecaches for each database connection. A positive number is the
12664 ** number of pages. A negative number N translations means that a buffer
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))
12686 # define MIN(A,B) ((A)<(B)?(A):(B)) argument
12689 # define MAX(A,B) ((A)>(B)?(A):(B)) argument
12695 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} argument
12701 #if 'A' == '\301'
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 */
12761 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
12762 ** that can be stored in a u32 without loss of data. The value
12766 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
12769 ** The datatype used to store estimates of the number of rows in a
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.
12785 ** For example, quantities 16 and 17 are both represented by a LogEst
12787 ** not exact values, this imprecision is not a problem.
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
12806 ** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
12819 /* The uptr type is an unsigned integer large enough to hold a pointer
12833 ** In other words, S is a buffer and E is a pointer to the first byte after
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)
12889 ** Round up a number to the next larger multiple of 8. This is used
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
12942 ** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
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
12980 ** callback for a given sqlite handle.
12984 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
12996 ** is a special table that holds the names and attributes of all
13003 ** The root-page of the master database table.
13013 ** A convenience macro that returns the number of elements in
13019 ** Determine if the argument is a power of two
13021 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
13024 ** The following value as a destructor means to use sqlite3DbFree().
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.
13061 ** make it clear to human readers when a function parameter is deliberately
13062 ** left unused within the body of a function. This usually happens when
13063 ** a function is called via a function pointer. For example the
13068 ** When a function parameter is not used at all within the body of a function,
13129 /* A VList object records a mapping between parameters/variables/wildcards
13132 ** on the sqlite3VListAdd() routine for more information. A VList is really
13139 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
13148 ** a legal notice, here is a blessing:
13155 ** This header file defines the interface that the sqlite B-Tree file
13156 ** subsystem. See comments in the source code for a detailed description
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 */
13203 #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
13204 #define BTREE_MEMORY 2 /* This is an in-memory DB */
13205 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
13206 #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
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
13282 ** The BTREE_DATA_VERSION value is not really a value stored in the header.
13283 ** It is a read-only number computed by the pager. But we merge it with
13285 ** Call it a "virtual meta value".
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
13335 ** OP_SeekLE opcodes for a range search, but where the range of entries
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
13362 ** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
13363 ** and DELETE operations as no-ops, and any READ operation against a
13364 ** FORDELETE cursor may return a null row: 0x01 0x00.
13366 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
13372 int wrFlag, /* 1 for writing. 0 for read-only */
13400 /* An instance of the BtreePayload object describes the content of a single
13416 ** run a little faster by using fewer registers for parameter passing.
13476 ** Enter and Leave procedures no-ops.
13524 ** a legal notice, here is a blessing:
13534 ** or VDBE. The VDBE implements an abstract machine that runs a
13542 ** A single VDBE is an opaque structure named "Vdbe". Only routines
13556 ** A single instruction of the virtual machine has an opcode
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 */
13615 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
13630 #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
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 */
13676 ** The following macro converts a relative address in the p2 field
13677 ** of a VdbeOp structure into a negative number so that
13681 #define ADDR(X) (-1-(X))
13685 ** header file that defines a number for each opcode used by the VDBE.
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
13913 ** for a description of what each of these routines does.
13931 # define sqlite3VdbeVerifyNoMallocRequired(A,B) argument
13932 # define sqlite3VdbeVerifyNoResultRow(A) argument
13996 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
14017 ** The VdbeCoverage macros are used to set a coverage testing point
14020 ** only works with an amalagmation build. That's ok since a VDBE branch
14025 ** // as a branch
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()
14056 # define sqlite3VdbeScanStatus(a,b,c,d,e) argument
14069 ** a legal notice, here is a blessing:
14077 ** subsystem. The page cache subsystem reads and writes a file a page
14078 ** at a time and provides a journal for rollback.
14085 ** Default maximum size for persistent journal files. A negative
14090 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14094 ** The type used to represent a page number. The first page in a file
14095 ** is called page 1. 0 is used to represent "not a page".
14100 ** Each open file is managed by a separate instance of the "Pager" structure.
14111 ** reserved for working around a windows/posix incompatibility). It is
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))
14124 #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
14125 #define PAGER_MEMORY 0x0002 /* In-memory database */
14130 #define PAGER_LOCKINGMODE_QUERY -1
14139 ** therefore cannot be changed without a compatibility break.
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
14176 ** a detailed description of each routine.
14179 /* Open and close a Pager connection. */
14192 /* Functions used to configure a Pager object. */
14313 ** a legal notice, here is a blessing:
14376 /* Create a new pager cache.
14389 /* Modify the page-size after the cache has been created. */
14392 /* Return the size in bytes of a PCache object. Used to preallocate
14407 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
14411 /* Change a page number. Used by incr-vacuum. */
14417 /* Get a list of all dirty pages in the cache, sorted by page number */
14449 /* Check invariants on a PgHdr object */
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
14489 /* Number of dirty pages as a percentage of the configured cache size */
14502 ** a legal notice, here is a blessing:
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.
14530 ** a legal notice, here is a blessing:
14538 ** This file contains pre-processor directives related to operating system
14588 ** a no-op
14595 ** The default size of a disk sector
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
14613 ** This annoyed many windows users. Those users would then do a
14630 ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
14631 ** RESERVED: A single process may hold a RESERVED lock on a file at
14633 ** PENDING: A single process may hold a PENDING lock on a file at
14638 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
14639 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
14640 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
14657 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
14658 ** byte out of a specific range of bytes. The lock byte is obtained at
14662 ** There can only be one writer. A RESERVED_LOCK is obtained by locking
14663 ** a single byte of the file that is designated as the reserved lock byte.
14664 ** A PENDING_LOCK is obtained by locking a designated byte different from
14672 ** But a single Win95 reader will lock out all WinNT readers and a single
14677 ** a random byte is selected for a shared lock. The pool of bytes for
14691 ** that all locks will fit on a single page even at the minimum page size.
14697 ** Changing the value of PENDING_BYTE results in a subtly incompatible
14699 ** the incompatibility right away, even running a full regression test.
14763 ** sqlite3_malloc() to obtain space for the file-handle structure.
14778 ** a legal notice, here is a blessing:
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.
14895 ** An instance of the following structure stores a database schema.
14897 ** Most Schema objects are associated with a Btree. The exception is
14898 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
14899 ** In shared cache mode, a single Schema object can be shared by multiple
14906 ** A thread must be holding a mutex on the corresponding Btree in order
14908 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
14909 ** For a TEMP Schema, only the connection mutex is required.
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
14957 ** associated with a particular database connection. The use of
14958 ** lookaside malloc provides a significant performance enhancement
14964 ** the lookaside subsystem is stored on a linked list of LookasideSlot
14968 ** with a particular database connection. Hence, schema information cannot
14990 ** A hash table for built-in function definitions. (Application-defined
14991 ** functions use a regular table table from hash.h.)
14993 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
14998 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ member
15017 #define UAUTH_User 2 /* Authenticated as a normal user */
15068 u8 autoCommit; /* The auto-commit flag. */
15070 u8 mallocFailed; /* True if we have seen a malloc failure */
15072 u8 dfltLockMode; /* Default locking-mode for attached dbs */
15076 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
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 */
15180 ** A macro to discover the encoding of a database.
15182 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
15183 #define ENC(db) ((db)->enc)
15202 /* the count using a callback. */
15206 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
15220 ** could be factored out into a separate bit vector of the sqlite3 object. */
15223 #define SQLITE_PreferBuiltin 0x02000000 /* Preference to built-in funcs */
15224 #define SQLITE_Vacuum 0x04000000 /* Currently in a VACUUM */
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)
15264 ** code. The argument is a Parse object for the code generator.
15266 #define ConstFactorOk(P) ((P)->okConstFactor)
15282 ** structure. For global built-in functions (ex: substr(), max(), count())
15283 ** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
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 */
15299 FuncDef *pHash; /* Next with a different name but the same hash */
15305 ** This structure encapsulates a user-function destructor callback (as
15306 ** configured using create_function_v2()) and a reference counter. When
15307 ** create_function_v2() is called to create a function with a destructor,
15308 ** a single object of this type is allocated. FuncDestructor.nRef is set to
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 */
15347 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
15349 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
15350 ** single query - might change over time */
15351 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
15358 ** Used to create a scalar function definition of a function zName
15360 ** value passed as iArg is cast to a (void*) and made available
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
15372 ** to a NULL pointer. The bNC parameter is not used.
15377 ** ignored and the user-data for these functions is set to an
15378 ** arbitrary non-NULL pointer. The bNC parameter is not used.
15387 ** Used to create a scalar function definition of a function zName
15388 ** that accepts nArg arguments and is implemented by a call to C
15389 ** function likeFunc. Argument pArg is cast to a (void *) and made
15390 ** available as the function user-data (sqlite3_user_data()). The
15423 ** All current savepoints are stored in a linked list starting at
15429 char *zName; /* Savepoint name (nul-terminated) */
15465 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
15474 #define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
15478 ** A "Collating Sequence" is defined by an instance of the following
15479 ** structure. Conceptually, a collating sequence consists of a name and
15480 ** a comparison routine that defines the order of that sequence.
15487 char *zName; /* Name of the collating sequence, UTF-8 encoded */
15495 ** A sort order can be either ASC or DESC.
15499 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
15505 ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
15506 ** the speed a little by numbering the values consecutively.
15508 ** But rather than start with 0 or 1, we begin with 'A'. That way,
15509 ** when multiple affinity types are concatenated into a string and
15513 ** for a numeric type is a single comparison. And the BLOB type is first.
15515 #define SQLITE_AFF_BLOB 'A'
15533 ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
15534 ** It causes an assert() to fire if either operand to a comparison
15553 ** database connections, even when the rest of the in-memory database
15562 ** All VTable objects that correspond to a single table in a shared
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
15576 ** next time a statement is prepared using said sqlite3*. This is done
15604 Select *pSelect; /* NULL for tables. Points to definition if a view. */
15608 /* ... also used as column name list in a VIEW */
15614 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
15621 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
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 */
15644 #define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
15648 #define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */
15649 #define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */
15655 ** Test to see whether or not a table is a virtual table. This is
15656 ** done as a macro so that it will be optimized out when virtual
15660 # define IsVirtual(X) ((X)->nModuleArg)
15666 ** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
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)
15683 /* Does the table have a rowid */
15684 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
15685 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
15690 ** A foreign key is associated with two tables. The "from" table is
15696 ** a INTEGER PRIMARY KEY,
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.
15712 ** A list of all children for a table named Z (which might not even exist)
15713 ** is held in Schema.fkeyHash with a hash key of Z.
15722 /* EV: R-30323-21917 */
15733 ** SQLite supports many different ways to resolve a constraint
15734 ** error. ROLLBACK processing means that a constraint violation
15744 ** a UNIQUE constraint violation are removed so that the new insert or
15750 ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
15783 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
15792 ** This object holds a record which has been parsed out into individual
15793 ** fields, for the purposes of doing a comparison.
15795 ** A record is an object that contains one or more fields of data.
15796 ** Records are used to store the content of a table row and to store
15797 ** the key of an index. A blob encoding of a record is created by
15801 ** An instance of this object serves as a "key" for doing a search on
15804 ** just a prefix of the key. The number of fields is given by
15805 ** pKeyInfo->nField.
15808 ** or greater than a key in the btree, respectively. These are normally
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 */
15859 ** it means this is not a unique index. Otherwise it is a unique index
15861 ** algorithm to employ whenever an attempt is made to insert a non-unique
15864 ** While parsing a CREATE TABLE or CREATE INDEX statement in order to
15868 ** used to store the address of a VDBE instruction, not a database page
15869 ** number (it cannot - the database page is not allocated until the VDBE
15893 unsigned isCovering:1; /* True if this is a covering index */
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 */
15910 #define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
15913 /* Return true if index X is a PRIMARY KEY index */
15914 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
15916 /* Return true if index X is a UNIQUE index */
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 */
15927 ** using a structure of this type. See documentation at the top of the
15947 const char *z; /* Text of the token. Not NULL-terminated! */
15953 ** code for a SELECT that contains aggregate functions.
15955 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
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
15999 ** it uses less memory in the Expr object, which is a big memory user
16003 ** the option is available (at compile-time).
16018 ** to represent the greater-than-or-equal-to operator in the expression
16023 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
16027 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
16030 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
16031 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
16032 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
16037 ** An expression of the form ID or ID.ID refers to a column in a table.
16039 ** the integer cursor number of a VDBE cursor pointing to that table and
16041 ** expression is used as a result in an aggregate SELECT, then the
16045 ** If the expression is an unbound variable marker (a question mark
16049 ** If the expression is a subquery then Expr.iColumn holds an integer
16051 ** subquery gives a constant result, then iTable is -1. If the subquery
16052 ** gives a different answer at different times during statement processing
16053 ** then iTable is the address of a subroutine that computes the subquery.
16056 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
16061 ** Expr objects can use a lot of memory space in database schema. To
16064 ** two or more Expr objects will be stored in a single memory allocation,
16076 char affinity; /* The affinity of the column or 0 if not a column */
16080 int iValue; /* Non-negative integer value if EP_IntValue */
16085 ** access them will result in a segfault or malfunction.
16097 ** access them will result in a segfault or malfunction.
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[] */
16131 #define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
16142 #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
16144 #define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
16145 #define EP_Alias 0x400000 /* Is an alias for a result set column */
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)
16173 ** Macros to determine the number of bytes required by a normal Expr
16185 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
16188 ** A list of expressions. Each expression may optionally have a
16190 ** as the list of "expr AS ID" fields following a "SELECT" or in the
16191 ** list of "ID = expr" items in an UPDATE. A list of expressions can
16192 ** also be used as the argument to a function, in which case the a.zName
16195 ** By default the Expr.zSpan field holds a human-readable description of
16197 ** column labels. In this case, Expr.zSpan is typically the text of a
16198 ** column expression as it exists in a SELECT statement. However, if
16205 int nAlloc; /* Number of a[] slots allocated */
16211 unsigned done :1; /* A flag to indicate when processing is finished */
16221 } a[1]; /* One slot for each expression in the list */ member
16236 ** An instance of this structure can hold a simple list of identifiers,
16237 ** such as the list "a,b,c" in the following statements:
16239 ** INSERT INTO t(a,b,c) VALUES ...;
16240 ** CREATE INDEX idx ON t(a,b,c);
16241 ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
16243 ** The IdList.a.idx field is used when the IdList represents the list of
16244 ** column names after a table name in an INSERT statement. In the statement
16246 ** INSERT INTO t(a,b,c) ...
16248 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
16253 int idx; /* Index in some Table.aCol[] of a column named zName */
16254 } *a; member
16261 ** Changing this from a 64-bit to a 32-bit type limits the number of
16262 ** tables in a join to 32 instead of 64. But it also reduces the size
16272 ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
16277 ** A bit in a Bitmask
16281 #define ALLBITS ((Bitmask)-1)
16284 ** The following structure describes the FROM clause of a SELECT statement.
16285 ** Each table or subquery in the FROM clause is a separate element of
16286 ** the SrcList.a[] array.
16289 ** can also be used to describe a particular table such as the table that
16291 ** such a table must be a simple name: ID. But in SQLite, the table can
16292 ** now be identified by a database name, a dot, then the table name: ID.ID.
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.
16304 u32 nAlloc; /* Number of entries allocated in a[] below */
16309 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
16311 Select *pSelect; /* A SELECT statement used in place of a table name */
16312 int addrFillSub; /* Address of subroutine to manifest a subquery */
16314 int regResult; /* Registers holding results of a co-routine */
16317 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
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 */
16328 Expr *pOn; /* The ON clause of a join */
16329 IdList *pUsing; /* The USING clause of a join */
16333 ExprList *pFuncArg; /* Arguments to table-valued-function */
16336 } a[1]; /* One entry for each identifier on the list */ member
16340 ** Permitted values of the SrcList.a.jointype field
16344 #define JT_NATURAL 0x0004 /* True for a "natural" join */
16358 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
16361 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
16363 #define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
16364 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
16366 #define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
16367 #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
16385 ** A NameContext defines a context in which to resolve table and column
16386 ** names. The context consists of a list of tables (the pSrcList) field and
16387 ** a list of named expression (pEList). The named expression list may
16388 ** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
16390 ** pEList corresponds to the result set of a SELECT and is NULL for
16393 ** NameContexts can be nested. When resolving names, the inner-most
16396 ** is checked. This process continues until either a match is found
16397 ** or all contexts are check. When a match is found, the nRef member of
16400 ** Each subquery gets a new NameContext. The pNext field points to the
16403 ** subqueries looking for a match.
16408 ExprList *pEList; /* Optional list of result-set columns */
16425 #define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */
16426 #define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */
16430 #define NC_VarSelect 0x0040 /* A correlated subquery has been seen */
16435 ** needed to generate code for a single SELECT statement.
16437 ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
16438 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
16468 Select *pPrior; /* Prior select in a compound select statement */
16469 Select *pNext; /* Next select to the left in a compound */
16487 #define SF_Aggregate 0x00008 /* Contains agg functions or a GROUP BY */
16492 #define SF_Compound 0x00100 /* Part of a compound query */
16495 #define SF_NestedFrom 0x00800 /* Part of a parenthesized FROM clause */
16497 #define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
16498 #define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
16505 ** The results of a SELECT can be distributed in several ways, as defined
16509 ** SRT_Union Store results as a key in a temporary index
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.
16524 ** SRT_Output Generate a row of output (using the OP_ResultRow
16527 ** SRT_Mem Only valid if the result is a single column.
16529 ** in register pDest->iSDParm then abandon the rest
16532 ** SRT_Set The result must be a single column. Store each
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
16557 ** a record of all prior results and ignore any duplicate
16560 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
16561 ** an index). Append a sequence number so that all entries
16564 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
16566 ** index at pDest->iSDParm+1 hold all prior stores.
16569 #define SRT_Except 2 /* Remove result from a UNION index */
16578 #define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
16581 #define SRT_Mem 10 /* Store result in a memory cell */
16584 #define SRT_Coroutine 13 /* Generate a single row of result */
16589 ** a SELECT statement.
16593 int iSDParm; /* A parameter used by the eDest disposal method */
16604 ** the code generator needs. We have to keep per-table autoincrement
16610 AutoincInfo *pNext; /* Next info block in a list of them all */
16630 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
16631 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
16636 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
16637 ** accessed (or set to 0 for triggers fired as a result of INSERT
16639 ** a mask of new.* columns used by the program.
16669 ** An SQL parser context. A copy of this structure is passed through
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 */
16714 yDbMask writeMask; /* Start a write transaction on these databases */
16718 int nMaxArg; /* Max args passed to user function by sub-program */
16725 TableLock *aTableLock; /* Required table locks for shared-cache mode */
16731 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
16748 u8 tempReg; /* iReg is a temp register that needs to be freed */
16771 int nHeight; /* Expression tree height of current sub-select */
16779 Table *pNewTable; /* A table being constructed by CREATE TABLE */
16780 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
16781 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
16783 Token sArg; /* Complete text of a module argument */
16797 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
16806 #define IN_DECLARE_VTAB (pParse->declareVtab)
16810 ** An instance of the following structure can be declared on a stack and used
16830 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
16833 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
16836 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
16837 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
16843 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
16846 #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
16855 * 2. All triggers associated with a single table form a linked list, using the
16856 * pNext member of struct Trigger. A pointer to the first element of the
16860 * The "step_list" member points to the first element of a linked list
16869 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
16870 the <column-list> is stored here */
16878 ** A trigger is either a BEFORE or an AFTER trigger. The following constants
16888 * An instance of struct TriggerStep is used to store a single SQL statement
16889 * that is a part of a trigger-program.
16891 * Instances of struct TriggerStep are stored in a singly linked list (linked
16894 * the first step of the trigger-program.
16896 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
16901 * orconf -> stores the ON CONFLICT algorithm
16902 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
16903 * this stores a pointer to the SELECT statement. Otherwise NULL.
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
16928 Trigger *pTrig; /* The trigger that this step is a part of */
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 */
16954 ** An objected used to accumulate the text of a string where we
16959 char *zBase; /* A base allocation. Not from malloc. */
16969 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
16973 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
16977 ** A pointer to this structure is used to communicate information
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:
17061 ** For most test cases, CORRUPT_DB is set to false using a special
17063 ** things that are always true for well-formed databases.
17068 ** Context pointer passed down through the tree-walk.
17076 u8 eCode; /* A small processing code */
17079 int n; /* A counter */
17080 int iCur; /* A cursor number */
17105 ** Return code from the parse-tree walking primitives and their
17113 ** An instance of this structure represents a set of one or more CTEs
17114 ** (common table expressions) created by a single WITH clause.
17124 } a[1]; member
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
17152 ** using sqlite3_log(). The routines also provide a convenient place
17153 ** to set a debugger breakpoint.
17192 ** The ctype.h header is needed for non-ASCII systems. It is also
17402 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
17443 # define sqlite3ViewGetColumnNames(A,B) 0 argument
17501 #define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
17583 # define sqlite3SetMakeRecordP5(A,B) argument
17601 # define sqlite3SelectSetName(A,B) argument
17637 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
17638 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
17641 # define sqlite3DeleteTrigger(A,B) argument
17642 # define sqlite3DropTriggerPtr(A,B) argument
17643 # define sqlite3UnlinkAndDeleteTrigger(A,B,C) argument
17644 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I) argument
17645 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F) argument
17649 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0 argument
17662 # define sqlite3AuthRead(a,b,c,d) argument
17663 # define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK argument
17664 # define sqlite3AuthContextPush(a,b,c) argument
17665 # define sqlite3AuthContextPop(a) ((void)(a)) argument
17698 ** Routines to read and write variable-length integers. These used to
17708 ** The common case is for a varint to be a single byte. They following
17709 ** macros handle the common case without a procedure call, but then call
17712 #define getVarint32(A,B) \ argument
17713 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
17714 #define putVarint32(A,B) \ argument
17715 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
17716 sqlite3PutVarint((A),(B)))
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).
17975 #define sqlite3FkActions(a,b,c,d,e,f) argument
17976 #define sqlite3FkCheck(a,b,c,d,e,f) argument
17977 #define sqlite3FkDropTable(a,b,c) argument
17978 #define sqlite3FkOldmask(a,b) 0 argument
17979 #define sqlite3FkRequired(a,b,c,d) 0 argument
17980 #define sqlite3FkReferences(a) 0 argument
17986 #define sqlite3FkDelete(a,b) argument
17987 #define sqlite3FkLocateIndex(a,b,c,d,e) argument
18014 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
18023 #define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
18063 ** sqlite3IoTrace is a pointer to a printf-like routine used to
18067 # define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; } argument
18071 # define IOTRACE(A) argument
18081 ** the MEMTYPE_* macros defined below. The type must be a bitmask with
18082 ** a single bit set.
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 */
18147 ** a legal notice, here is a blessing:
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
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 ()*+,-./ */
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
18323 ** number of bytes in each lookaside slot (should be a multiple of 8)
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
18414 ** a different position in the file. This allows code that has to
18448 ** a legal notice, here is a blessing:
18466 ** a legal notice, here is a blessing:
18483 ** The maximum number of times that a statement will try to reparse
18502 ** SQL is translated into a sequence of instructions to be
18503 ** executed by a virtual machine. Each instruction is an instance
18526 ** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
18528 ** * A b-tree cursor
18529 ** - In the main database or in an ephemeral database
18530 ** - On either an index or a table
18531 ** * A sorter
18532 ** * A virtual table
18533 ** * A one-row "pseudotable" stored in a single register
18538 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
18539 u8 nullRow; /* True if pointing to a row with no data */
18540 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
18547 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
18563 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
18595 ** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
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
18608 ** The memory for a VdbeFrame object is allocated and managed by a memory
18639 int nDbChange; /* Value of db->nChange */
18669 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
18671 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
18672 void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */
18686 ** For a pointer type created using sqlite3_bind_pointer() or
18689 ** If the MEM_Str flag is set then Mem.z points at a string representation.
18695 #define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
18696 #define MEM_Str 0x0002 /* Value is a string */
18698 #define MEM_Real 0x0008 /* Value is a real number */
18699 #define MEM_Blob 0x0010 /* Value is a BLOB */
18701 #define MEM_RowSet 0x0020 /* Value is a RowSet object */
18702 #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
18708 /* Whenever Mem contains a valid string or blob representation, one of
18715 #define MEM_Static 0x0800 /* Mem.z points to a static string */
18725 /* Return TRUE if Mem X contains dynamically allocated content - anything
18726 ** that needs to be deallocated to avoid a leak.
18729 (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
18732 ** Clear any existing type flags from a Mem and replace them with f
18735 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
18738 ** Return true if a memory cell is not marked as invalid. This macro
18742 #define memIsValid(M) ((M)->flags & MEM_Undefined)==0
18746 ** Each auxiliary data pointer stored by a user defined function
18748 ** of this structure. All such structures associated with a single VM
18749 ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
18761 ** The "context" argument for an installable function. A pointer to an
18765 ** There is a typedef for this structure in sqlite.h. So all routines,
18766 ** even the public interface to SQLite, can use a pointer to this structure.
18781 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
18786 /* A bitfield type for use inside of structures. Always follow with :N where
18796 int iSelectID; /* The "Select-ID" for this loop */
18806 ** is really a pointer to an instance of this structure.
18826 /* When allocating a new Vdbe object, all of the fields below should be
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 */
18854 bft usesStmtJournal:1; /* True if uses a statement journal */
18857 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
18858 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
18866 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
18878 #define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */
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
19070 ** Return the current value of a status parameter. The caller must
19083 ** Add N to the value of a status record. The caller must hold the
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()
19179 ** Query status information for a single database connection
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()
19363 ** a legal notice, here is a blessing:
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
19387 ** be represented, even though julian day numbers allow a much wider
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
19412 ** The MSVC CRT on Windows CE may not have a localtime() function.
19413 ** So declare a substitute. The substitute function itself is
19422 ** A structure for holding a single date and time.
19447 ** of a four-character format specifiers ABCD is:
19449 ** A: number of digits to convert. Always "2" or "4".
19452 ** a: 12
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.
19470 ** spec into a max size: a b c d e f */ in getDigits()
19477 char N = zFormat[0] - '0'; in getDigits()
19478 char min = zFormat[1] - '0'; in getDigits()
19482 assert( zFormat[2]>='a' && zFormat[2]<='f' ); 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.
19520 ** A missing specifier is not considered an error.
19527 p->tz = 0; in parseTimezone()
19529 if( c=='-' ){ in parseTimezone()
19530 sgn = -1; in parseTimezone()
19544 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
19547 p->tzSet = 1; in parseTimezone()
19556 ** Return 1 if there is a parsing error and 0 on success.
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.
19610 int Y, M, D, A, B, X1, X2; in computeJD() local
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()
19630 A = Y/100; 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()
19707 ** Input "r" is a numeric quantity which might be a julian day number,
19709 ** range of a julian day number, install it as such and set validJD.
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()
19722 ** Attempt to parse the given string into a julian day number. Return
19727 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
19731 ** In the first form, the +/-HH:MM is always optional. The fractional
19734 ** as there is a time string. The time string can be omitted as long
19735 ** as there is a year and date.
19756 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
19761 ** such a large integer literal, so we have to encode it.
19778 int Z, A, B, C, D, E, X1; in computeYMD() local
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()
19791 B = A + 1524; 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
19904 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
19919 /* Initialize the contents of sLocal to avoid a compiler warning. */ in localtimeOffset()
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
19991 ** Process a modifier to a date-time stamp. The modifiers are
20010 ** is in a system call (i.e. localtime()), then an error message is written
20027 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to in parseModifier()
20032 p->iJD += localtimeOffset(p, pCtx, &rc); in parseModifier()
20042 ** Treat the current value of p->s as the number of in parseModifier()
20043 ** seconds since 1970. Convert to a real julian day number. 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()
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.
20229 ** then assume a default value of "now" for argv[0].
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()
20347 ** Return a string described by FORMAT. Conversions as follows:
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()
20407 return; /* ERROR. return a NULL */ 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()
20492 sqlite3_result_text(context, z, -1, in strftimeFunc()
20541 ** If the library is compiled to omit the full-scale date and time
20542 ** handling (to get a smaller binary), the following minimal version
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()
20615 ** a legal notice, here is a blessing:
20630 ** of code will give us the ability to simulate a disk I/O error. This
20635 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
20644 ** When testing, also keep a count of the number of open files.
20652 ** memory (actually, os_unix.c allocates a small amount of memory
20653 ** from within OsOpen()), but some third-party implementations may.
20654 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
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()
20734 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
20741 ** is using a regular VFS, it is called after the corresponding in sqlite3OsFileControl()
20742 ** transaction has been committed. Injecting a fault at this point in sqlite3OsFileControl()
20743 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM in sqlite3OsFileControl()
20747 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably 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 */
20819 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed in sqlite3OsOpen()
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()
20921 ** This function is a wrapper around the OS specific implementation of
20923 ** ability to simulate a malloc failure, so that the handling of an
20940 ** Locate a VFS by name. If no name is given, simply return the
20956 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
20958 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
20965 ** Unlink a VFS from the linked list
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()
20985 ** Register a VFS with the system. It is harmless to register the same
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()
21015 ** Unregister a VFS so that it is no longer accessible.
21033 ** a legal notice, here is a blessing:
21043 ** sqlite3_mem_methods structure fails to allocate a block of memory
21046 ** Most malloc failures are non-benign. After they occur, SQLite
21048 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
21049 ** fatal. For example, if a malloc fails while resizing a hash table, this
21051 ** hash table will continue to function normally. So a malloc failure
21052 ** during a hash table resize is a benign fault.
21070 ** we have to locate the state vector at run-time. In the more common
21099 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
21100 ** indicates that subsequent malloc failures are non-benign.
21123 ** a legal notice, here is a blessing:
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
21185 ** a legal notice, here is a blessing:
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()
21354 ** Report the allocated size of a prior return from xMalloc()
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()
21410 ** Round up a request size to the next valid allocation size.
21454 ** Populate the low-level memory allocation function pointers in
21479 ** a legal notice, here is a blessing:
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
21511 # define backtrace(A,B) 1 argument
21512 # define backtrace_symbols_fd(A,B,C) argument
21519 ** ------------------------------------------------------------------------
21521 ** ------------------------------------------------------------------------
21523 ** The application code sees only a pointer to the allocation. We have
21526 ** backtrace pointers. There is also a guard word at the end of the
21552 ** into a single structure named "mem". This is to keep the
21564 ** Head and tail of a linked list of all outstanding allocations
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()
21682 ** Round up a request size to the next valid allocation size.
21689 ** Fill a buffer with pseudo-random bytes. This is used to preset
21690 ** the content of a new memory allocation to unpredictable values and
21691 ** to clear the content of a freed allocation to unpredictable values.
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()
21811 ** For this debugging implementation, we *always* make a copy of the
21812 ** allocation into a new place in memory. In this way, if the
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()
21910 ** A value of zero turns off backtracing. The number is always rounded
21911 ** up to a multiple of 2.
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()
21947 ** Open the file indicated and write a log of all unfreed memory
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()
21981 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
21983 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
21984 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
22010 ** a legal notice, here is a blessing:
22017 ** This file contains the C functions that implement a memory
22021 ** use of malloc(). The SQLite user supplies a block of memory
22036 ** mean that the library will use a memory-pool by default, just that
22043 ** Maximum size (in Mem3Blocks) of a "small" chunk.
22054 ** A memory allocation (also called a "chunk") consists of two or
22056 ** a header that is not returned to the user.
22058 ** A chunk is two or more blocks that is either checked out or
22070 ** We often identify a chunk by its index in mem3.aPool[]. When
22073 ** A chunk index of 0 means "no such chunk" and is the equivalent
22074 ** of a NULL pointer.
22077 ** two fields form a double-linked list of chunks of related sizes.
22081 ** The second block of a chunk is user data if the chunk is checked
22082 ** out. If a chunk is checked out, the user data may extend into
22101 ** into a single structure named "mem3". This is to keep the
22114 ** True if we are evaluating an out-of-memory callback.
22139 ** for smaller chunks, or a hash on the block size for larger
22142 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
22150 ** on. *pRoot is the list that i is a member of.
22170 ** whatever list is currently a member of.
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()
22253 ** Chunk i is a free chunk that has been unlinked. Adjust its
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()
22271 ** Carve a piece off of the end of the mem3.iMaster free chunk.
22272 ** Return a pointer to the new allocation. Or, if the master chunk
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()
22305 ** *pRoot is the head of a list of free chunks of the same size
22312 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
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()
22353 ** Return a block of memory of at least nBytes in size.
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()
22395 ** Try to satisfy the allocation by carving a piece off of the end in memsys3MallocUnsafe()
22406 ** Then try again to satisfy the allocation by carving a piece off 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()
22491 ** Round up a request size to the next valid allocation size.
22497 return ((n+11)&~7) - 4; in memsys3Roundup()
22537 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
22563 /* Store a pointer to the memory block in global structure mem3. */ in memsys3Init()
22566 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
22591 ** Open the file indicated and write a log of all unfreed memory
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
22700 ** a legal notice, here is a blessing:
22707 ** This file contains the C functions that implement a memory
22711 ** use of malloc(). The application gives SQLite a block of memory
22723 ** 1. All memory allocation sizes are rounded up to a power of 2.
22725 ** 2. If two adjacent free blocks are the halves of a larger block,
22732 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
22735 ** allocation size (after rounding all sizes up to a power of 2.) Let M
22741 ** N >= M*(1 + log2(n)/2) - n + 1
22755 ** A minimum allocation is an instance of the following structure.
22757 ** size of the array is a power of 2.
22759 ** The size of this object must be a power of two. That fact is
22770 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
22783 ** into a single structure named "mem5". This is to keep the
22805 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
22815 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
22830 ** Access the static variable through a macro for SQLITE_OMIT_WSD.
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()
22906 ** Return a block of memory of at least nBytes in size.
22911 ** The caller has obtained a mutex prior to invoking this
22916 int i; /* Index of a mem5.aPool[] slot */ in memsys5MallocUnsafe()
22921 /* nByte must be a positive */ in memsys5MallocUnsafe()
22940 ** block. If not, then split a block of the next larger power of in memsys5MallocUnsafe()
22941 ** two in order to create a new free block of size iLogsize. in memsys5MallocUnsafe()
22954 iBin--; in memsys5MallocUnsafe()
22965 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
22974 ** or retains a value from a previous allocation */ in memsys5MallocUnsafe()
22978 /* Return a pointer to the allocated memory. */ 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()
23080 ** nBytes is always a value obtained from a prior call to
23081 ** memsys5Round(). Hence nBytes is always a non-negative power
23090 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
23108 ** Round up a request size to the next valid allocation size. If
23112 ** All allocations must be a power of two and must be expressed by a
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()
23142 ** This routine is not threadsafe. The caller must be holding a mutex
23157 /* The size of a Mem5Link object must be a power of two. Verify that in memsys5Init()
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()
23192 /* If a mutex is required for normal operation, allocate one */ in memsys5Init()
23211 ** Open the file indicated and write a log of all unfreed memory
23232 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
23254 ** linkage. It returns a pointer to a static sqlite3_mem_methods
23279 ** a legal notice, here is a blessing:
23296 ** allocate a mutex while the system is uninitialized.
23310 ** install a mutex implementation via sqlite3_config() prior to in sqlite3MutexInit()
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()
23361 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
23382 ** Free a dynamic mutex.
23416 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
23418 ** is not currently entered. If a NULL pointer is passed as an argument
23419 ** this function is a no-op.
23451 ** a legal notice, here is a blessing:
23462 ** that use SQLite in a single thread. The routines defined
23463 ** here are place-holders. Applications can substitute working
23464 ** mutex routines at start-time using the
23528 int cnt; /* Number of entries without a matching leave */
23537 return p==0 || p->cnt>0; in debugMutexHeld()
23541 return p==0 || p->cnt==0; in debugMutexNotheld()
23551 ** The sqlite3_mutex_alloc() routine allocates a new
23552 ** mutex and returns a pointer to it. If it returns NULL
23553 ** that means that a mutex could not be allocated.
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()
23584 ** This routine deallocates a previously allocated mutex.
23588 assert( p->cnt==0 ); in debugMutexFree()
23589 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
23600 ** to enter a mutex. If another thread is already within the mutex,
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()
23622 ** The sqlite3_mutex_leave() routine exits a mutex that was
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.
23669 ** a legal notice, here is a blessing:
23684 ** Note that this implementation requires a version of pthreads that
23694 ** home-grown mutexes. Encapsulate these conditions into a single #define.
23730 ** incorrect results. On most platforms, pthread_equal() is a
23732 ** told that HPUX is not such a platform. If so, then these routines
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()
23750 ** Try to provide a memory barrier operation, needed for initialization
23769 ** The sqlite3_mutex_alloc() routine allocates a new
23770 ** mutex and returns a pointer to it. If it returns NULL
23771 ** that means that a mutex could not be allocated. SQLite
23793 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
23795 ** The mutex implementation does not need to make a distinction
23797 ** not want to. But SQLite will only request a recursive mutex in
23798 ** cases where it really needs one. If a faster non-recursive mutex
23800 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
23803 ** a pointer to a static preexisting mutex. Six static mutexes are
23812 ** returns a different mutex on every call. But for the static
23839 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
23841 /* Use a recursive mutex if it is available */ 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()
23877 ** This routine deallocates a previously
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()
23899 ** to enter a mutex. If another thread is already within the mutex,
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()
24002 ** The sqlite3_mutex_leave() routine exits a mutex that was
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()
24059 ** a legal notice, here is a blessing:
24080 ** a legal notice, here is a blessing:
24088 ** This file contains macros and a little bit of code that is common to
24089 ** all of the platform-specific files (os_*.c) and is #included into those
24092 ** This file should be #included by the os_*.c files only. It is not a
24101 ** switch. The following code should catch this problem at compile-time.
24115 ** high-performance timing routines.
24123 ** a legal notice, here is a blessing:
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
24150 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); in sqlite3Hwtime()
24169 __asm__ __volatile__ ("rdtsc" : "=A" (val));
24211 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
24221 ** of code will give us the ability to simulate a disk I/O error. This
24235 || sqlite3_io_error_pending-- == 1 ) \
24250 sqlite3_diskfull_pending--; \
24255 #define SimulateIOError(A)
24256 #define SimulateDiskfullError(A)
24260 ** When testing, keep a count of the number of open files.
24283 ** a legal notice, here is a blessing:
24328 ** Determine if we are dealing with Windows CE - which has a much reduced
24338 ** Determine if we are dealing with WinRT, which provides only a subset of
24356 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
24374 ** on a Win32 system.
24392 ** These are the initializer values used when declaring a "static" mutex
24411 return p->nRef!=0 && p->owner==GetCurrentThreadId();
24415 return p->nRef==0 || p->owner!=tid;
24425 ** Try to provide a memory barrier operation, needed for initialization
24460 static int winMutex_isNt = -1; /* <0 means "need to query" */
24509 ** The sqlite3_mutex_alloc() routine allocates a new
24510 ** mutex and returns a pointer to it. If it returns NULL
24511 ** that means that a mutex could not be allocated. SQLite
24533 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
24535 ** The mutex implementation does not need to make a distinction
24537 ** not want to. But SQLite will only request a recursive mutex in
24538 ** cases where it really needs one. If a faster non-recursive mutex
24540 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
24543 ** a pointer to a static preexisting mutex. Six static mutexes are
24552 ** returns a different mutex on every call. But for the static
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;
24600 ** This routine deallocates a previously
24606 assert( p->nRef==0 && p->owner==0 );
24607 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
24608 DeleteCriticalSection(&p->mutex);
24619 ** to enter a mutex. If another thread is already within the 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)));
24696 ** The sqlite3_mutex_leave() routine exits a mutex that was
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));
24751 ** a legal notice, here is a blessing:
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
24798 ** (so that a range test can be used to determine if an allocation
24799 ** being freed came from pScratch) and a pointer to the list of
24825 ** that was invoked when memory usage grew too large. Now it is a
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
24963 ** Do a memory allocation with statistics and alarms. Assume the
24972 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
24975 ** or else a crash results. Hence, do not attempt to optimize out the
24989 if( nUsed >= mem0.alarmThreshold - nFull ){
25018 /* A memory allocation of a number of bytes which is near the maximum
25031 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
25054 ** Each thread may only have a single outstanding allocation from
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;
25153 ** TRUE if p is a lookaside memory allocation from db
25157 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
25160 #define isLookaside(A,B) 0
25164 ** Return the size of a memory allocation previously obtained from
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);
25222 ** Free memory that might be associated with a particular database
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
25278 ** argument to xRealloc is always a value returned by a prior call to
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
25371 ** Hence for a particular database connection, once malloc starts
25376 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
25378 ** if( b ) a[10] = 9;
25380 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
25381 ** that all prior mallocs (ex: "a") worked too.
25384 ** not a NULL pointer.
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));
25482 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
25517 ** Free any prior content in *pz and replace it with a copy of zNew.
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--;
25570 ** The returned value is normally a copy of the second argument to this
25571 ** function. However, if a malloc() failure has occurred since the previous
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 */
25622 #define etTOKEN 11 /* a pointer to a Token structure */
25623 #define etSRCLIST 12 /* a pointer to a SrcList */
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";
25699 ** "*val" is a double such that 0.1 <= *val < 10.0
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;
25734 ** Extra argument values from a PrintfArguments object
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++]);
25751 ** On machines with a small stack size, you can redefine the
25760 ** Render a string given by "fmt" into the StrAccum object.
25771 int idx; /* A general purpose loop counter */
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;
25924 ** flag_alternateform TRUE if a '#' is present.
25925 ** flag_altform2 TRUE if a '!' is present.
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);
26355 ** Enlarge the memory allocation on a StrAccum object so that it is
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.
26467 ** Return a pointer to the resulting string. Return a NULL
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;
26505 ** Initialize a string accumulator.
26508 ** db: Pointer to a database connection. May be NULL. Lookaside
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.
26604 ** are not able to use a "," as the decimal point in place of "." as
26640 ** We house it in a separate routine from sqlite3_log() to avoid using
26641 ** stack space on small-stack systems when logging is disabled.
26643 ** sqlite3_log() must render into a static buffer. It cannot dynamically
26663 ** Format and write a message to the log if logging is enabled.
26676 ** A version of printf() that understands %lld. Used for debugging.
26678 ** and segfaults if you give it a long long int.
26696 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
26709 ** 2015-06-08
26712 ** a legal notice, here is a blessing:
26721 ** These routines print a parse tree to standard output for debugging and
26731 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
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);
26757 ** Generate a single line of output for the tree, with a prefix that contains
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);
26783 ** Shorthand for starting a new tree item that consists of a single label
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;
27135 /* If the opcode is TK_TRIGGER, then the expression is a reference
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);
27246 ** a legal notice, here is a blessing:
27253 ** This file contains code to implement a pseudo-random number
27262 /* All threads share a single random number generator.
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
27312 ** not need to contain a lot of randomness since we are not
27316 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
27346 }while( --N );
27353 ** PRNG and restore the PRNG to its saved state at a later time, or
27383 ** a legal notice, here is a blessing:
27391 ** This file presents a simple cross-platform threading interface for
27394 ** A "thread" can be created using sqlite3ThreadCreate(). This thread
27404 ** single-threaded if desired.
27416 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
27419 /* A running thread */
27428 /* Create a new thread */
27431 void *(*xTask)(void*), /* Routine to run in a separate thread */
27439 /* This routine is never used in single-threaded mode */
27446 p->xTask = xTask;
27447 p->pIn = pIn;
27448 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
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 */
27491 /* A running thread */
27495 void *(*xTask)(void*); /* The routine to run as a thread */
27514 assert( p->id==GetCurrentThreadId() );
27516 assert( p->xTask!=0 );
27517 p->pResult = p->xTask(p->pIn);
27523 /* Create a new thread */
27526 void *(*xTask)(void*), /* Routine to run in a separate thread */
27536 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
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 **************************/
27591 ** This implementation does not actually create a new thread. It does the
27596 /* A running thread */
27598 void *(*xTask)(void*); /* The routine to run as a thread */
27603 /* Create a new thread */
27606 void *(*xTask)(void*), /* Routine to run in a separate thread */
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 *************************/
27660 ** a legal notice, here is a blessing:
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.
27787 ** is a 0x00.
27789 ** Write a pointer to the next unread byte back into *pzNext.
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
27795 ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
27797 ** * This routine never allows a UTF16 surrogate value to be encoded.
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];
27855 ** encoding, or if *pMem does not contain a string value.
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.
27907 ** A single byte is required for the output string
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 );
28195 ** a legal notice, here is a blessing:
28225 ** Give a callback to the test harness that can be used to simulate faults
28244 ** Return true if the floating point value is Not a Number (NaN).
28254 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
28255 ** found that many systems do not have a working isnan() function so
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.
28290 ** Compute a string length that is limited to what can be stored in
28291 ** lower 30 bits of a 32-bit signed integer.
28303 ** Return the declared type of a column. Or return zDflt if the column
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
28316 ** a separate routine to avoid unnecessary register saves on entry to
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);
28355 ** %s Insert a string
28356 ** %z A string that should be freed after use
28358 ** %T Insert a token
28359 ** %S Insert the first element of a SrcList
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.
28388 ** %s Insert a string
28389 ** %z A string that should be freed after use
28391 ** %T Insert a token
28392 ** %S Insert the first element of a SrcList
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
28421 ** input does not begin with a quote character, then this routine
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".
28459 ** Generate a Token object from a string
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;
28488 unsigned char *a, *b;
28490 a = (unsigned char *)zLeft;
28493 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
28494 if( c || *a==0 ) break;
28495 a++;
28501 register unsigned char *a, *b;
28503 return zRight ? -1 : 0;
28507 a = (unsigned char *)zLeft;
28509 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
28510 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
28514 ** The string z[] is an text representation of a real number.
28515 ** Convert this string to a double and write it into *pResult.
28518 ** uses the encoding enc. The string is not necessarily zero-terminated.
28520 ** Return TRUE if the result is a valid real number (or integer) and FALSE
28524 ** [+-]digits[E[+-]digits]
28525 ** [+-]digits.[digits][E[+-]digits]
28526 ** [+-].digits[E[+-]digits]
28531 ** If some prefix of the input string is a valid number, this routine
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--;
28608 /* This branch is needed to avoid a (harmless) buffer overread. The
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;
28649 ** comments, as a hint to the mutation tester.
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
28750 ** case is broken out because while 9223372036854775808 cannot be a
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++){
28912 /* The longest decimal representation of a 32 bit integer is 10 digits:
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:
28946 ** A = 0xxxxxxx 7 bits of data and one flag bit
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++){
29013 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
29015 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
29022 ** Read a 64-bit variable-length integer from memory starting at p[0].
29026 u32 a,b,s;
29028 a = *p;
29029 /* a: p0 (unmasked) */
29030 if (!(a&0x80))
29032 *v = a;
29041 a &= 0x7f;
29042 a = a<<7;
29043 a |= b;
29044 *v = a;
29053 a = a<<14;
29054 a |= *p;
29055 /* a: p0<<14 | p2 (unmasked) */
29056 if (!(a&0x80))
29058 a &= SLOT_2_0;
29061 a |= b;
29062 *v = a;
29067 a &= SLOT_2_0;
29076 /* a &= (0x7f<<14)|(0x7f); */
29077 a = a<<7;
29078 a |= b;
29079 *v = a;
29083 /* a: p0<<14 | p2 (masked) */
29087 /* a &= (0x7f<<14)|(0x7f); */
29089 s = a;
29093 a = a<<14;
29094 a |= *p;
29095 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
29096 if (!(a&0x80))
29100 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
29103 a |= b;
29105 *v = ((u64)s)<<32 | a;
29122 a &= SLOT_2_0;
29123 a = a<<7;
29124 a |= b;
29126 *v = ((u64)s)<<32 | a;
29131 a = a<<14;
29132 a |= *p;
29133 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
29134 if (!(a&0x80))
29136 a &= SLOT_4_2_0;
29139 a |= b;
29141 *v = ((u64)s)<<32 | a;
29146 a &= SLOT_2_0;
29155 /* a &= (0x7f<<14)|(0x7f); */
29156 a = a<<7;
29157 a |= b;
29159 *v = ((u64)s)<<32 | a;
29164 a = a<<15;
29165 a |= *p;
29166 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
29169 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
29172 a |= b;
29175 b = p[-4];
29180 *v = ((u64)s)<<32 | a;
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
29192 ** A MACRO version, getVarint32, is provided which inlines the
29193 ** single-byte case. All code should use the MACRO version as
29194 ** this function assumes the single-byte case has already been handled.
29197 u32 a,b;
29199 /* The 1-byte case. Overwhelmingly the most common. Handled inline
29201 a = *p;
29202 /* a: p0 (unmasked) */
29204 if (!(a&0x80))
29207 *v = a;
29212 /* The 2-byte case */
29219 a &= 0x7f;
29220 a = a<<7;
29221 *v = a | b;
29225 /* The 3-byte case */
29227 a = a<<14;
29228 a |= *p;
29229 /* a: p0<<14 | p2 (unmasked) */
29230 if (!(a&0x80))
29233 a &= (0x7f<<14)|(0x7f);
29236 *v = a | b;
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
29278 a &= (0x7f<<14)|(0x7f);
29279 a = a<<7;
29280 *v = a | b;
29285 a = a<<14;
29286 a |= *p;
29287 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
29288 if (!(a&0x80))
29291 a &= SLOT_4_2_0;
29294 *v = a | b;
29298 /* We can only reach this point when reading a corrupt database
29300 ** slow) general-purpose sqlite3GetVarint() routine to extract the
29306 p -= 4;
29317 ** 64-bit integer.
29327 ** Read or write a four-byte big-endian integer value.
29367 ** Translate a single byte of Hex into an integer.
29368 ** This routine only works if h really is a valid hexadecimal
29369 ** character: 0..9a..fA..F
29372 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
29384 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
29385 ** value. Return a pointer to its binary value. Space to hold the
29394 n--;
29406 ** Log an error that is an API call on a connection pointer that should
29408 ** argument. The zType is a word like "NULL" or "closed" or "invalid".
29418 ** Check to make sure we have a valid db pointer. This test is not
29427 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
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
29538 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
29539 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
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);
29568 ** not a simple "+" operator because LogEst is stored as a logarithmic
29572 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
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 */
29584 if( a>=b ){
29585 if( a>b+49 ) return a;
29586 if( a>b+31 ) return a+1;
29587 return a+x[a-b];
29589 if( b>a+49 ) return b;
29590 if( b>a+31 ) return b+1;
29591 return b+x[b-a];
29596 ** Convert an integer into a LogEst. In other words, compute an
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;
29614 ** Convert a double into a LogEst
29618 u64 a;
29620 assert( sizeof(x)==8 && sizeof(a)==8 );
29623 memcpy(&a, &x, 8);
29624 e = (a>>52) - 1022;
29633 ** Convert a LogEst into an integer.
29636 ** non-standard compile-time options is enabled.
29642 if( n>=5 ) n -= 2;
29643 else if( n>=1 ) n -= 1;
29649 ** possible to this routine is 310, resulting in a maximum x of 31 */
29652 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
29657 ** Add a new name/number pair to a VList. This might require that the
29660 ** db->mallocFailed flag is set.
29662 ** A VList is really just an array of integers. To destroy a VList,
29673 ** is always zero-terminated.
29684 ** } a[0];
29726 ** Return a pointer to the name of a variable in the given VList that
29727 ** has the value iVal. Or return a NULL if there is no such variable in
29765 ** a legal notice, here is a blessing:
29772 ** This is the implementation of generic hash-tables
29778 /* Turn bulk memory into a hash table object by initializing the
29781 ** "pNew" is a pointer to the hash table that is to be initialized.
29785 pNew->first = 0;
29786 pNew->count = 0;
29787 pNew->htsize = 0;
29788 pNew->ht = 0;
29791 /* Remove all entries from a hash table. Reclaim all memory.
29792 ** Call this routine to delete a hash table or to reset a hash table
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;
29877 /* The inability to allocates space for a larger hash table is
29878 ** a performance hit but it is not a fatal error. So mark the
29879 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
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;
29904 ** a pointer to a static null element with HashElem.data==0 is returned.
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;
29939 /* Remove a single entry from the hash table given a pointer to that
29940 ** element and a hash on the element's key.
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 );
29973 /* Attempt to locate an element of the hash table pH with a key
29980 return findElementWithHash(pH, pKey, 0)->data;
29986 ** If no element exists with a matching key, then a new
29991 ** The key is not copied in this instance. If a malloc fails, then
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);
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)"),
30224 ** a legal notice, here is a blessing:
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,
30357 ** If we are to be thread-safe, include the pthreads header and define
30366 ** Default permissions when creating a new file
30380 ** Maximum supported path-length.
30394 ** Only set the lastErrno if the error code is a real error and not
30395 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
30402 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
30406 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
30431 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
30457 ** transaction counter in bytes 24-27 of database files are updated
30459 ** occur if a file is updated without also updating the transaction
30465 unsigned char inNormalWrite; /* True if in a normal write operation */
30470 /* In test mode, increase the size of this structure a bit so that
30478 ** method was called. If xOpen() is called from a different process id,
30479 ** indicating that a fork() has occurred, the PRNG will be reset.
30508 ** a legal notice, here is a blessing:
30516 ** This file contains macros and a little bit of code that is common to
30517 ** all of the platform-specific files (os_*.c) and is #included into those
30520 ** This file should be #included by the os_*.c files only. It is not a
30529 ** switch. The following code should catch this problem at compile-time.
30543 ** high-performance timing routines.
30551 ** a legal notice, here is a blessing:
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
30578 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
30597 __asm__ __volatile__ ("rdtsc" : "=A" (val));
30639 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
30649 ** of code will give us the ability to simulate a disk I/O error. This
30663 || sqlite3_io_error_pending-- == 1 ) \
30678 sqlite3_diskfull_pending--; \
30683 #define SimulateIOError(A)
30684 #define SimulateDiskfullError(A)
30688 ** When testing, keep a count of the number of open files.
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.
30751 ** The difference is important when using a pointer to the function.
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?
30798 #define osFstat(a,b,c) 0
30924 ** On some systems, calls to fchown() will trigger a message in a security
30925 ** log if they come from non-root processes. So avoid calling fchown() if
30981 ** Return the value of a system call. Return NULL if zName is not a
31001 ** is the last system call or if zName is not the name of a valid
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",
31192 ** could lead to a corrupt database.
31197 /* On Android, ftruncate() always uses 32-bit offsets, even if
31199 ** truncate a file to any size larger than 2GiB. Silently ignore any
31210 ** This routine translates a standard POSIX errno code into something
31212 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
31213 ** and a variety of "please close the file descriptor NOW" errors into
31247 ** On most versions of unix, we can get a unique ID for a file by concatenating
31249 ** On VxWorks, a unique file id must be based on the canonical filename.
31251 ** A pointer to an instance of the following structure can be used as a
31253 ** a copy of the canonical filename. There is also a reference count.
31258 ** a performance-critical path, so it is sufficient to put these
31259 ** structures on a linked list.
31262 struct vxworksFileId *pNext; /* Next in a list of them all */
31270 ** All unique filenames are held on a linked list headed by this
31276 ** Simplify a filename into its canonical form
31281 ** * convert /A/../ where A is any simple name into just /
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--; }
31312 ** Find a unique file ID for the given absolute pathname. Return
31313 ** a pointer to the vxworksFileId object. This pointer is the unique
31317 ** the object is returned. A new vxworksFileId object is created
31320 ** If a memory allocation error occurs, return NULL.
31331 pNew->zCanonicalName = (char*)&pNew[1];
31332 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
31333 n = vxworksSimplifyName(pNew->zCanonicalName, n);
31336 ** If found, increment the reference count and return a pointer to
31340 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
31341 if( pCandidate->nName==n
31342 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
31345 pCandidate->nRef++;
31351 /* No match was found. We will make a new file ID */
31352 pNew->nRef = 1;
31353 pNew->nName = n;
31354 pNew->pNext = vxworksFileList;
31361 ** Decrement the reference count on a vxworksFileId object. Free
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;
31386 ** section 6.5.2.2 lines 483 through 490 specify that when a process
31387 ** sets or clears a lock, that operation overrides any prior locks set
31389 ** that it overrides locks set by the same process using a different
31396 ** one is a hard or symbolic link to the other) then if you set
31399 ** fail since there was already a lock on the file due to fd1.
31410 ** on its own. Whenever a new database is opened, we have to find the
31415 ** locks to see if another thread has previously set a lock on that same
31423 ** descriptor. It is now a structure that holds the integer file
31424 ** descriptor and a pointer to a structure that describes the internal
31428 ** a reference count (so we will know when to delete it) and a "cnt"
31430 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
31433 ** Any attempt to lock or unlock a file first checks the locking
31434 ** structure. The fcntl() system call is only invoked to set a
31436 ** a locked and an unlocked state.
31440 ** If you close a file descriptor that points to a file that has locks,
31443 ** maintains a count of the number of pending locks on tha inode.
31447 ** The unixInodeInfo structure keeps a list of file descriptors that need to
31454 ** not posix compliant. Under LinuxThreads, a lock created by thread
31455 ** A cannot be modified or overridden by a different thread B.
31456 ** Only thread A can modify the lock. Locking behavior is correct
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
31474 ** to locate a particular unixInodeInfo object.
31481 /* We are told that some versions of Android contain a bug that
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,
31486 ** but that should not be a big deal. */
31497 ** A single inode can have multiple file descriptors, so each unixFile
31498 ** structure contains a pointer to an instance of this object and this
31499 ** object keeps a count of the number of unixFile pointing to it.
31522 ** A lists of all unixInodeInfo objects.
31528 ** This function - unixLogErrorAtLine(), is only ever called via the macro
31532 ** set. It logs a message using sqlite3_log() containing the current value of
31533 ** errno and, if possible, the human-readable equivalent from strerror() or
31539 ** failed (e.g. "unlink", "open") and the associated file-system path,
31542 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
31552 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
31553 ** the strerror() function to obtain the human-readable error message
31563 ** returns a pointer to a buffer containing the error message. That pointer
31569 ** available, the error message will often be an empty string. Not a
31571 ** could lead to a segfault though.
31576 strerror_r(iErrno, aErr, sizeof(aErr)-1);
31579 /* This is a threadsafe build, but strerror_r() is not available. */
31582 /* Non-threadsafe build, use strerror(). */
31588 "os_unix.c:%d: (%d) %s(%s) - %s",
31596 ** Close a file descriptor.
31598 ** We assume that close() almost always works, since it is only in a
31599 ** very sick application or on a very sick platform that it might fail.
31611 pFile ? pFile->zPath : 0, lineno);
31616 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
31617 ** a convenient place to set a breakpoint.
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;
31639 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
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;
31669 ** Given a file descriptor, locate the unixInodeInfo object that
31670 ** describes that file descriptor. Create a new one if necessary. The
31685 struct stat statbuf; /* Low-level file information */
31690 /* Get low-level information about the file that we can used to
31691 ** create a unique name for the file.
31693 fd = pFile->h;
31698 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
31705 ** incorrectly for zero-size files. See ticket #3260. To work
31706 ** around this problem (we consider it a bug in OS X, not SQLite)
31707 ** we always increase the file size to 1 by writing a single byte
31711 ** is a race condition such that another thread has already populated
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);
31774 ** Check a unixFile that is a database. Verify the following:
31777 ** (2) The file is not a symbolic link
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);
31810 ** This routine checks if there is a RESERVED lock held on the specified
31811 ** file by this or any other process. If such a lock is held, set *pResOut
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 */
31826 /* Check if a thread in this process holds such a lock */
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
31922 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
31923 ** routine to lower a locking level.
31928 ** lock primitives (called read-locks and write-locks below, to avoid
31935 ** range', a range of 510 bytes at a well known offset.
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
31947 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
31948 ** A RESERVED lock is implemented by grabbing a write-lock on the
31951 ** A process may only obtain a PENDING lock after it has obtained a
31952 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
31954 ** obtained, but existing SHARED locks are allowed to persist. A process
31955 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
31956 ** This property is used by the algorithm for rolling back a journal file
31957 ** after a crash.
31959 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
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,
31977 /* If there is already a lock of this type or more restrictive on the
31981 if( pFile->eFileLock>=eFileLock ){
31982 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
31989 ** (2) SQLite never explicitly requests a pendig lock.
31990 ** (3) A shared lock is always held when a reserve lock is requested.
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;
32001 /* If some thread using this PID has a lock via a different unixFile*
32004 if( (pFile->eFileLock!=pInode->eFileLock &&
32005 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
32011 /* If a SHARED lock is requested, and some thread using this PID already
32012 ** has a SHARED or RESERVED lock, then increment reference counts and
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++;
32027 /* A PENDING lock is needed before acquiring a SHARED lock and before
32034 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
32053 assert( pInode->nShared==0 );
32054 assert( pInode->eFileLock==0 );
32057 /* Now get the read-lock */
32070 /* This could happen with a network mount */
32081 pFile->eFileLock = SHARED_LOCK;
32082 pInode->nLock++;
32083 pInode->nShared = 1;
32085 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
32087 ** same process is still holding a shared lock. */
32090 /* The request was for a RESERVED or EXCLUSIVE lock. It is
32091 ** assumed that there is a SHARED or greater lock on the file
32094 assert( 0!=pFile->eFileLock );
32117 /* Set up the transaction-counter change checking flags when
32118 ** transitioning from a SHARED to a RESERVED lock. The change
32119 ** from SHARED to RESERVED marks the beginning of a normal
32120 ** write operation (not a hot journal rollback).
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.
32170 ** set to a read lock, then the other part is simply unlocked. This works
32171 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
32172 ** remove the write lock on a region when a read lock is set.
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 );
32196 /* When reducing a lock such that other processes can start
32202 ** cache. The use of a stale cache can lead to database corruption.
32204 pFile->inNormalWrite = 0;
32207 /* downgrading to a shared lock on NFS involves clearing the write lock
32208 ** before establishing the readlock - to avoid a race condition we downgrade
32209 ** the lock in 2 blocks, so that part of the range will be covered by a
32210 ** write lock until the rest is covered by a read lock:
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 );
32353 ** even on VxWorks. A mutex will be acquired on VxWorks by the
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);
32389 ** Close a file.
32398 /* unixFile.pInode is always valid here. Otherwise, a different close
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
32430 ** database connections. But there is a serious risk of database
32457 /******************* End of the no-op lock implementation *********************
32461 ************************* Begin dot-file Locking ******************************
32464 ** files (really a directory) to control access to the database. This works
32467 ** (1) There is zero concurrency. A single reader blocks all other
32473 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
32476 ** Dotfile locking works by creating a subdirectory in the same directory as
32477 ** the database and with the same name but with a ".lock" extension added.
32478 ** The existence of a lock directory implies an EXCLUSIVE lock. All other
32489 ** This routine checks if there is a RESERVED lock held on the specified
32490 ** file by this or any other process. If such a lock is held, set *pResOut
32491 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32494 ** In dotfile locking, either a lock exists or it does not. So in this
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
32533 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
32534 ** routine to lower a locking level.
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;
32630 ** Close a file. Make sure the lock has been released before closing.
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
32649 ** a single exclusive lock. In other words, SHARED, RESERVED, and
32652 ** only a single process can be reading the database at a time.
32668 # define robust_flock(a,b) flock(a,b)
32673 ** This routine checks if there is a RESERVED lock held on the specified
32674 ** file by this or any other process. If such a lock is held, set *pResOut
32675 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32687 /* Check if a thread in this process holds such a lock */
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
32755 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
32756 ** routine to lower a locking level.
32764 /* if we already have a lock, it is 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;
32834 ** Close a file.
32852 ** Semaphore locking is like dot-lock and flock in that it really only
32853 ** supports EXCLUSIVE locking. Only a single process can read or write
32854 ** the database file at a time. This reduces potential concurrency, but
32860 ** This routine checks if there is a RESERVED lock held on the specified
32861 ** file by this or any other process. If such a lock is held, set *pResOut
32862 ** to a non-zero value otherwise *pResOut is set to zero. The return value
32874 /* Check if a thread in this process holds such a lock */
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
32929 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
32930 ** routine to lower a locking level.
32934 sem_t *pSem = pFile->pInode->pSem;
32937 /* if we already have a lock, it is exclusive.
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;
33000 ** Close a file.
33026 ** AFP is the Apple Filing Protocol. AFP is a network filesystem found
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 ) {
33102 ** This routine checks if there is a RESERVED lock held on the specified
33103 ** file by this or any other process. If such a lock is held, set *pResOut
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 */
33123 /* Check if a thread in this process holds such a lock */
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
33174 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
33175 ** routine to lower a locking level.
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)));
33188 /* If there is already a lock of this type or more restrictive on the
33192 if( pFile->eFileLock>=eFileLock ){
33193 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
33200 ** (2) SQLite never explicitly requests a pendig lock.
33201 ** (3) A shared lock is always held when a reserve lock is requested.
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;
33212 /* If some thread using this PID has a lock via a different unixFile*
33215 if( (pFile->eFileLock!=pInode->eFileLock &&
33216 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
33222 /* If a SHARED lock is requested, and some thread using this PID already
33223 ** has a SHARED or RESERVED lock, then increment reference counts and
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++;
33237 /* A PENDING lock is needed before acquiring a SHARED lock and before
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 ){
33291 ** same process is still holding a shared lock. */
33294 /* The request was for a RESERVED or EXCLUSIVE lock. It is
33295 ** assumed that there is a SHARED or greater lock on the file
33299 assert( 0!=pFile->eFileLock );
33300 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
33301 /* Acquire a 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)) ){
33322 ** a critical I/O error
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) )
33388 /* When reducing a lock such that other processes can start
33394 ** cache. The use of a stale cache can lead to database corruption.
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;
33460 ** Close a file & cleanup AFP specific locking context
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 *****************************
33537 ** To avoid stomping the errno value on a failed read the lastErrno value
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));
33585 ** Read data from a file into a buffer. Return SQLITE_OK if all
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;
33634 storeLastErrno(pFile, 0); /* not a system error */
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.
33691 ** To avoid stomping the errno value on a failed write the lastErrno value
33695 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
33700 ** Write data from a buffer into a file. Return SQLITE_OK on success
33714 /* If this is a database file (not a journal, master-journal or temp
33717 assert( pFile->pUnused==0
33724 /* If we are doing a normal write to a database file (as opposed to
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 ){
33775 storeLastErrno(pFile, 0); /* not a system error */
33793 ** We do not trust systems to provide a working fdatasync(). Some do.
33796 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
33856 /* Record the number of times that we do a normal fsync() and
33865 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33866 ** no-op. But go ahead and call fstat() to validate the file
33867 ** descriptor as we need a method to provoke a failure during
33885 ** and (for now) ignore the overhead of a superfluous fcntl call.
33899 if( rc==-1 && errno==ENOTSUP ){
33905 if( OS_VXWORKS && rc!= -1 ){
33912 ** Open a file descriptor to the directory containing file zFilename.
33918 ** The directory file descriptor is used for only one thing - to
33919 ** fsync() a directory to make sure file creation and deletion events
33925 ** chromium sandbox. Opening a directory is a security risk (we are
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
33929 ** *pFd set to a negative number.
33936 int fd = -1;
33940 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
33949 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
33957 ** Make sure all writes to a particular file are committed to disk.
33964 ** has been created by fsync-ing the directory that contains the file.
33965 ** If we do not do this and we encounter a power failure, the directory
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)
33999 ** are unable to fsync a directory, so ignore errors on the fsync.
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;
34019 ** Truncate an open file to a specified size
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);
34042 /* If we are doing a normal write to a database file (as opposed to
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;
34055 /* If the file was just truncated to a size smaller than the currently
34059 if( nByte<pFile->mmapSize ){
34060 pFile->mmapSize = nByte;
34069 ** Determine the current size of a file in bytes
34075 rc = osFstat(((unixFile*)id)->h, &buf);
34083 /* When opening a zero-size database, the findInodeInfo() procedure
34084 ** writes a single byte into that file in order to work around a bug
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);
34131 /* If the OS does not have posix_fallocate(), fake it. Write a
34133 ** within the extended region. Then, if required, a single byte
34134 ** at offset (nSize-1), to set the size of the file correctly.
34135 ** This is a similar technique to that used by glibc on systems
34136 ** that do not have a real fallocate() call.
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);
34173 ** If *pArg is initially negative then this is a query. Set *pArg to
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);
34260 ** a rollback and that the database is therefore unchanged and
34265 ((unixFile*)id)->dbUpdate = 0;
34285 ** if two files are created in the same file-system directory (i.e.
34286 ** a database and its journal file) that the sector size will be the
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 =
34366 /* Last chance verification. If the sector size isn't a multiple of 512
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,
34384 ** very rare. And asserting PSOW makes a large reduction in the amount
34385 ** of required I/O for journaling, since a lot of padding is eliminated.
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
34428 ** has its own unixShm object, but they all point to a single instance
34429 ** of this unixShmNode object. In other words, each wal-index is opened
34432 ** Each unixShmNode object is connected to a single unixInodeInfo object.
34436 ** the unixInodeInfo object contains a pointer to this unixShmNode object
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));
34577 ** a time, assuming that each mapping must be an integer multiple of the
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".
34634 ** some other tmpfs mount. But if a file in a different directory
34636 ** or a chroot() might cause two different processes on the same
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
34646 ** "unsupported" and may go away in a future SQLite release.
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 );
34672 /* Check to see if a unixShmNode object already exists. Reuse an existing
34673 ** one if present. Create a new one if necessary.
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) ){
34758 /* Make the new connection a child of the unixShmNode */
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);
34789 ** This function is called to obtain a pointer to region iRegion of the
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
34797 ** region has not been allocated (by any client, including one running in a
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) ){
34864 ** writing a single byte to the end of each (OS) page being
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.
34940 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
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));
35052 ** Implement a memory barrier or memory fence on shared memory.
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
35233 ** there already exists a mapping for this file, and there are still
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
35243 ** recreated as a result of outstanding references) or an SQLite error
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 ){
35272 ** If possible, return a pointer to a mapping of file fd starting at offset
35275 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
35280 ** If this function does return a pointer, the caller must eventually
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
35352 ** sqlite3_io_methods object for a particular database file. The pAppData
35354 ** the correct finder-function for that VFS.
35356 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
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()".
35365 ** (2) A constant pointer to this function named just "F".
35368 ** A pointer to the F pointer is used as the pAppData value for VFS
35370 ** directly at the finder-function since C90 rules prevent a void*
35371 ** from be cast into a function pointer.
35376 ** * A constant sqlite3_io_methods object call METHOD that has locking
35379 ** * An I/O method finder function called FINDER that returns a pointer
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
35512 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
35554 /* If filePath==NULL that means we are dealing with a transient file
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 ) {
35605 /* If filePath==NULL that means we are dealing with a transient file
35617 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
35629 ** An abstract type for a pointer to an IO method finder function:
35655 assert( pNew->pInode==NULL );
35657 /* Usually the path zFilename should not be a relative pathname. The
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);
35719 ** (a) A call to fstat() failed.
35720 ** (b) A malloc failed.
35726 ** handle h - as it is guaranteed that no posix locks will be released
35729 ** If scenario (a) caused the error then things are not so safe. The
35731 ** such bad shape that dropping a lock or two doesn't matter much.
35734 h = -1;
35745 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
35750 ** according to requirement F11141. So we do not need to make a
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;
35828 ** Return the name of a directory in which to put temporary files.
35861 ** Create a temporary file name in zBuf. zBuf must be allocated
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
35905 ** Such a file descriptor may exist if a database connection was closed
35907 ** other file descriptor open on the same file is holding a file-lock.
35912 ** If a suitable file descriptor is found, then it is returned. If no
35913 ** such file descriptor is located, -1 is returned.
35926 /* A stat() call may fail for various reasons. If this happens, it is
35929 ** ignored and -1 is returned. The caller will try to open a new file
35932 ** Even if a subsequent open() call does succeed, the consequences of
35933 ** not searching for a reusable file descriptor are not dire. */
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;
35981 ** and a value suitable for passing as the third argument to open(2) is
35988 ** But if the file being opened is a WAL or regular journal file, then
35989 ** this function queries the file-system for the permissions on the
36014 /* zPath is a path to a WAL or journal file. The following block derives
36018 ** "<path to db>-journal"
36019 ** "<path to db>-wal"
36020 ** "<path to db>-journalNN"
36021 ** "<path to db>-walNN"
36023 ** where NN is a decimal number. The NN naming schemes are
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--;
36047 /* If this is a main database file and the file was opened using a URI
36049 ** its value as a filename and try to copy the mode, uid and gid from
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()
36118 /* If argument zPath is a NULL pointer, this function is required to open
36119 ** a temporary file. Use this buffer to store the file name in.
36126 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
36143 /* Assert that the upper layer has set one of the "file-type" flags. */
36150 /* Detect a pid change and reset the PRNG. There is a race condition
36166 fd = pUnused->fd;
36173 p->pUnused = pUnused;
36175 /* Database filenames are double-zero terminated if they are not
36181 /* If zName is NULL, the upper layer is requesting a temp file. */
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. */
36231 /* If this process is running as root and if creating a new rollback
36244 if( p->pUnused ){
36245 p->pUnused->fd = fd;
36246 p->pUnused->flags = flags;
36264 p->openFlags = openFlags;
36269 if( fstatfs(fd, &fsInfo) == -1 ){
36275 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
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);
36436 ** Turn a relative pathname into a full pathname. The relative path
36437 ** is stored as a nul-terminated string in the buffer pointed to by
36440 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
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
36471 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
36492 nByte = osReadlink(zIn, zDel, nOut-1);
36498 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
36530 ** Interfaces for opening a shared library, finding entry points
36540 ** SQLite calls this function immediately after a call to unixDlSym() or
36541 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
36543 ** is available, zBufOut is left unmodified and SQLite uses a default
36558 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
36559 ** cast into a pointer to a function. And yet the library dlsym() routine
36560 ** returns a void* which is really a pointer to a function. So how do we
36561 ** use dlsym() with -pedantic-errors?
36563 ** Variable x below is defined to be a pointer to a function taking
36564 ** parameters void* and const char* and returning a pointer to a function.
36565 ** We initialize x by assigning it a pointer to the dlsym() function.
36566 ** (That assignment requires a cast.) Then we call the function that
36569 ** This work-around is unlikely to work correctly on any system where
36570 ** you really cannot cast a function pointer into void*. But then, on the
36571 ** other hand, dlsym() will not work on such a system either, so we have
36598 ** errors. The reports issued by valgrind are incorrect - we would
36600 ** uninitialized space in zBuf - but valgrind errors tend to worry
36633 ** Sleep for a little while. Return the amount of time slept.
36636 ** requested from the underlying operating system, a number which
36662 ** The following variable, if set to a non-zero value, is interpreted as
36672 ** the current time and date as a Julian Day number times 86_400_000. In
36709 ** current time and date as a Julian Day number into *prNow and
36725 ** The xGetLastError() method is designed to return a better
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
36746 ** other locking methods on secondary lock files. Proxy locking is a
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
36762 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
36763 ** address in the shared range is taken for a SHARED lock, the entire
36768 ** SHARED_RANGE 0x40000002 -> 0x40000200
36770 ** This works well on the local file system, but shows a nearly 100x
36772 ** the read cache when byte-range locks are present. Enabling the read
36773 ** cache exposes a cache coherency problem that is present on all OS X
36775 ** close-to-open semantics for ensuring cache coherency
36779 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
36782 ** changes the way database access is controlled by limiting access to a
36783 ** single host at a time and moving file locks off of the database file
36784 ** and onto a proxy file on the local file system.
36788 ** -----------------
36803 ** Specifying ":auto:" means that if there is a conch file with a matching
36805 ** a proxy path based on the user's temp dir
36813 ** Once a lock proxy is configured for a database connection, it can not
36814 ** be removed, however it may be switched to a different proxy path via
36820 ** -----------------------
36824 ** * conch file to limit access to the database file to a single host
36825 ** at a time
36827 ** * proxy file to act as a proxy for the advisory locks normally
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".
36839 ** path and the lock is downgraded to a shared lock again. If the conch
36840 ** is held by another process (with a shared lock), the exclusive lock
36843 ** The proxy file - a single-byte file used for all advisory file locks
36851 ** opening a connection to a database file does not take a lock on it.
36859 ** ---------------------
36863 ** Database files accessed on non-local file systems are
36880 ** Permissions to use when creating a directory for storing the
36908 int conchHeld; /* 1 if the conch is held, -1 if lockless */
36916 ** which must point to valid, writable memory large enough for a maxLen length
36941 if( lPath[len-1]!='/' ){
36945 /* transform the db path to a unique cache name */
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] != '.') ){
36994 ** Create a new VFS file descriptor (stored in memory obtained from
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 */
37132 ** Takes an open conch file, copies the contents to a new path and then moves
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;
37194 /* Take the requested lock on the conch file and break a stale lock if the
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);
37246 /* don't break the lock on short read or a version mismatch */
37257 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
37260 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
37269 /* Takes the conch by taking a shared lock and read the contents conch, if
37270 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
37272 ** host IDs match, or a new lock path will be generated automatically
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);
37316 /* a short read or version format mismatch means we need to create a new
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;
37345 /* create a copy of the lock path if the conch is taken */
37349 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
37350 readLen-PROXY_PATHINDEX)
37358 if( (conchFile->openFlags&O_RDWR) == 0 ){
37363 /* either the conch didn't match or we need to create a new one */
37364 if( !pCtx->lockProxyPath ){
37365 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
37367 /* create a copy of the lock path _only_ if the conch is taken */
37371 ** has a shared lock already), if the host id matches, use the big
37374 futimes(conchFile->h, NULL);
37376 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
37378 ** same process is still holding a shared lock. */
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);
37402 /* If we created a new conch file (not just updated the contents of a
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
37465 /* Need to make a copy of path if we extracted the value
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 -
37495 ** If pFile holds a lock on a conch file, then release that lock.
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,
37517 ** Given the name of a database file, compute the name of its conch file.
37525 ** *pConchPath is set to NULL if a memory allocation error occurs.
37529 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
37540 /* now insert a "." before the last / character */
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);
37594 ** pFile is a file that has been opened by a prior xOpen call. dbPath
37595 ** is a string buffer at least MAXPATHLEN+1 characters in size.
37602 if( pFile->pMethod == &afpIoMethods ){
37603 /* afp style keeps a reference to the db path in the filePath field
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) ){
37660 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
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.
37786 ** This routine checks if there is a RESERVED lock held on the specified
37787 ** file by this or any other process. If such a lock is held, set *pResOut
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
37827 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
37828 ** routine to lower a locking level.
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;
37871 ** Close a file that uses proxy locks.
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
37930 ** This routine is called once during SQLite initialization and by a
37938 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
37939 ** to the "finder" function. (pAppData is a pointer to a pointer because
37940 ** silly C90 rules prohibit a void* from being cast to a function pointer
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.
37950 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
37951 ** object. But the "autolockIoFinder" available on MacOSX does a little
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.
38046 ** a legal notice, here is a blessing:
38068 ** a legal notice, here is a blessing:
38076 ** This file contains macros and a little bit of code that is common to
38077 ** all of the platform-specific files (os_*.c) and is #included into those
38080 ** This file should be #included by the os_*.c files only. It is not a
38089 ** switch. The following code should catch this problem at compile-time.
38103 ** high-performance timing routines.
38111 ** a legal notice, here is a blessing:
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
38138 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
38157 __asm__ __volatile__ ("rdtsc" : "=A" (val));
38199 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
38209 ** of code will give us the ability to simulate a disk I/O error. This
38223 || sqlite3_io_error_pending-- == 1 ) \
38238 sqlite3_diskfull_pending--; \
38243 #define SimulateIOError(A)
38244 #define SimulateDiskfullError(A)
38248 ** When testing, keep a count of the number of open files.
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
38407 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
38411 ** This macro is used when a local variable is set to a value that is
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 */
38491 BOOL bPending; /* Indicates a pending lock has been obtained */
38492 BOOL bReserved; /* Indicates a reserved lock has been obtained */
38498 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
38507 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
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
38734 ** In order to facilitate testing on a WinNT system, the test fixture
38756 ** Many system calls are accessed through pointer-to-functions so that
39373 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
39374 ** is really just a macro that uses a compiler intrinsic (e.g. x64).
39375 ** So do not try to make this is into a redefinable interface.
39461 ** Return the value of a system call. Return NULL if zName is not a
39481 ** is the last system call or if zName is not the name of a valid
39485 int i = -1;
39489 for(i=0; i<ArraySize(aSyscall)-1; i++){
39501 ** If a Win32 native heap has been configured, this function will attempt to
39504 ** "pnLargest" argument, if non-zero, will be used to return the size of the
39542 ** If a Win32 native heap has been configured, this function will attempt to
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,
39667 ** API as long as we don't call it when running Win95/98/ME. A call to
39684 ** This function determines if the machine is running a version of Windows
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 ){
39819 ** Round up a request size to the next valid allocation size.
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.
40073 ** This is a public wrapper for the winUtf8ToUnicode() function.
40089 ** This is a public wrapper for the winUnicodeToUtf8() function.
40105 ** This is a public wrapper for the winMbcsToUtf8() function.
40121 ** This is a public wrapper for the winMbcsToUtf8() function.
40137 ** This is a public wrapper for the winUtf8ToMbcs() function.
40153 ** This is a public wrapper for the winUtf8ToMbcs() function.
40207 ** is zero if the error message fits in the buffer, or non-zero
40242 /* allocate a buffer and convert to UTF8 */
40265 /* allocate a buffer and convert to UTF8 */
40277 /* copy a maximum of nBuf chars to output buffer */
40287 ** This function - winLogErrorAtLine() - is only ever called via the macro
40291 ** It logs a message using sqlite3_log() containing the current value of
40292 ** error code and, if possible, the human-readable equivalent from
40298 ** failed and the associated file-system path, if any.
40300 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
40318 "os_win.c:%d: (%lu) %s(%s) - %s",
40326 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
40327 ** will be retried following a locking error - probably caused by
40341 ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
40344 ** non-zero if the error code is transient in nature and the operation
40346 ** retried. The argument to this macro should be a variable.
40348 ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
40357 #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
40358 ((a)==ERROR_SHARING_VIOLATION) || \
40359 ((a)==ERROR_LOCK_VIOLATION) || \
40360 ((a)==ERROR_DEV_NOT_EXIST) || \
40361 ((a)==ERROR_NETNAME_DELETED) || \
40362 ((a)==ERROR_SEM_TIMEOUT) || \
40363 ((a)==ERROR_NETWORK_UNREACHABLE))
40367 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
40398 ** Log a I/O error retry episode.
40416 ** The MSVC CRT on Windows CE may not have a localtime() function.
40417 ** So define a substitute.
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)]
40450 ** Acquire a lock on the handle h
40459 ** Release a lock acquired by winceMutexAcquire()
40481 memset(&pFile->local, 0, sizeof(pFile->local));
40484 ** to derive a mutex name. */
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,
40511 /* Set a flag that indicates we're the first to create the memory so it
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 ++;
40633 /* Want a pending lock? */
40637 if (pFile->shared->bPending == 0) {
40638 pFile->shared->bPending = TRUE;
40639 pFile->local.bPending = TRUE;
40644 /* Want a reserved lock? */
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);
40677 /* Releasing a reader lock or an exclusive lock */
40680 if (pFile->local.bExclusive){
40682 pFile->local.bExclusive = FALSE;
40683 pFile->shared->bExclusive = FALSE;
40687 /* Did we just have a reader lock? */
40688 else if (pFile->local.nReaders){
40691 pFile->local.nReaders --;
40692 if (pFile->local.nReaders == 0)
40694 pFile->shared->nReaders --;
40700 /* Releasing a pending lock */
40703 if (pFile->local.bPending){
40704 pFile->local.bPending = FALSE;
40705 pFile->shared->bPending = FALSE;
40709 /* Releasing a reserved lock */
40712 if (pFile->local.bReserved) {
40713 pFile->local.bReserved = FALSE;
40714 pFile->shared->bReserved = FALSE;
40719 winceMutexRelease(pFile->hMutex);
40728 ** Lock a file region.
40760 ** Unlock a file region.
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));
40819 /* API oddity: If successful, SetFilePointer() returns a dword
40820 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
40822 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
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));
40870 ** Close a file.
40872 ** It is reported that an attempt to close a handle might sometimes
40873 ** fail. This is a very unreasonable result, but Windows is notorious
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
40915 sqlite3_win32_sleep(100); /* Wait a little before trying again */
40917 sqlite3_free(pFile->zDeleteOnClose);
40921 pFile->h = NULL;
40923 OpenCounter(-1);
40925 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
40928 "winClose", pFile->zPath);
40932 ** Read data from a file into a buffer. Return SQLITE_OK if all
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));
41013 ** Write data from a buffer into a file. Return SQLITE_OK on success
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));
41123 ** Truncate an open file to a specified size
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);
41156 /* If the file was truncated to a size smaller than the currently
41160 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
41161 pFile->mmapSize = nByte;
41166 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
41180 ** Make sure all writes to a particular file are committed to disk.
41212 osGetCurrentProcessId(), pFile, pFile->h, flags,
41213 pFile->locktype));
41224 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
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);
41265 ** Determine the current size of a file in bytes
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)));
41341 ** Acquire a reader lock.
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();
41371 /* No need to log a failure to lock */
41373 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
41378 ** Undo a readlock
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
41422 ** This routine will only increase a lock. The winUnlock() routine
41424 ** It is not possible to lower the locking level one step at a time. You
41429 int res = 1; /* Result of a Windows lock call */
41430 int newLocktype; /* Set pFile->locktype to this value before exiting */
41431 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
41437 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
41439 /* If there is already a lock of this type or more restrictive on the
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 );
41460 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
41461 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
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
41474 ** If you are using this code as a model for alternative VFSes, do not
41475 ** copy this retry logic. It is a hack intended for Windows only.
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)));
41495 /* Acquire a shared lock
41498 assert( pFile->locktype==NO_LOCK );
41507 /* Acquire a RESERVED lock
41510 assert( pFile->locktype==SHARED_LOCK );
41511 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
41519 /* Acquire a PENDING lock
41529 assert( pFile->locktype>=SHARED_LOCK );
41531 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
41541 /* If we are holding a PENDING lock that ought to be released, then
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)));
41566 ** This routine checks if there is a RESERVED lock held on the specified
41567 ** file by this or any other process. If such a lock is held, return
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
41650 ** database connections. But there is a serious risk of database
41674 /******************* End of the no-op lock implementation *********************
41678 ** If *pArg is initially negative then this is a query. Set *pArg to
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));
41754 int *a = (int*)pArg;
41755 if( a[0]>0 ){
41756 winIoerrRetry = a[0];
41758 a[0] = winIoerrRetry;
41760 if( a[1]>0 ){
41761 winIoerrRetryDelay = a[1];
41763 a[1] = winIoerrRetryDelay;
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.
41825 ** a database and its journal file) that the sector size will be the
41834 ** Return a vector of device characteristics.
41839 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
41845 ** During sqlite3_os_init() we do a GetSystemInfo()
41878 ** Object used to represent a single file opened and mmapped to provide
41880 ** log-summary, each thread has its own winFile object, but they all
41881 ** point to a single instance of this object. In other words, each
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 */
41922 ** A global array of all winShmNode objects.
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 */
42071 ** allocate space for a new winShmNode and filename.
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);
42086 ** If no matching winShmNode currently exists, create a new one.
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);
42138 /* Make the new connection a child of the winShmNode */
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,
42319 ** Implement a memory barrier or memory fence on shared memory.
42328 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42334 ** This function is called to obtain a pointer to region iRegion of the
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
42342 ** region has not been allocated (by any client, including one running in a
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
42529 ** there already exists a mapping for this file, and there are still
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
42539 ** recreated as a result of outstanding references) or an SQLite error
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",
42630 ** If possible, return a pointer to a mapping of file fd starting at offset
42633 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
42638 ** If this function does return a pointer, the caller must eventually
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--;
42700 /* FIXME: If Windows truly always prevents truncating or deleting a
42701 ** file while a mapping is held, then the following winUnmapfile() call
42702 ** is unnecessary can be omitted - potentially improving
42707 assert( pFd->nFetchOut>=0 );
42794 ** Convert a filename from whatever the underlying operating system
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
42835 ** ends with a directory separator character or one was successfully
42842 if( winIsDirSep(zBuf[nLen-1]) ){
42855 ** Create a temporary file name and store the resulting pointer into pzBuf.
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
42874 /* Allocate a temporary buffer to store the fully qualified file
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"));
42929 /* If the path starts with a drive letter followed by the colon
42930 ** character, assume it is already a native Win32 path; otherwise,
42931 ** it must be converted to a native Win32 path via the Cygwin API
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));
43085 ** Return TRUE if the named file is really a directory. Return false if
43086 ** it is something other than a directory, or if there is any kind of memory
43114 ** Open a file.
43118 const char *zName, /* Name of the file (UTF-8) */
43135 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
43138 /* If argument zPath is a NULL pointer, this function is required to open
43139 ** a temporary file. Use this buffer to store the file name in.
43167 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
43184 /* Assert that the upper layer has set one of the "file-type" flags. */
43193 pFile->h = INVALID_HANDLE_VALUE;
43202 /* If the second argument to this function is NULL, generate a
43215 /* Database filenames are double-zero terminated if they are not
43243 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
43248 /* Creates a new file, only if it does not already exist. */
43255 /* Opens a file, only if it exists. */
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;
43404 ** Note that Windows does not allow a file to be deleted if some other
43405 ** process has it open. Sometimes a virus scanner or indexing program
43406 ** will open a journal file shortly after it is created in order to do
43519 ** Check the existence and status of a file.
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
43597 ** followed by a colon character.
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
43615 ** If the path name starts with a forward slash or a backslash, it is either
43616 ** a legal UNC name, a volume relative path, or an absolute path name in the
43626 ** If the path name starts with a letter and a colon it is either a volume
43628 ** attempt to treat it as a relative path name (i.e. they should simply use
43636 ** If we get to this point, the path name should almost certainly be a purely
43637 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
43643 ** Turn a relative pathname into a full pathname. Write the full
43644 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
43669 assert( nFull>=pVfs->mxPathname );
43672 ** NOTE: We are dealing with a relative path name and the data
43675 ** one by prepending the data directory and a slash.
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);
43725 /* WinCE has no concept of a relative pathname, or so I am told. */
43726 /* WinRT has no way to convert a relative path to an absolute one. */
43729 ** NOTE: We are dealing with a relative path name and the data
43732 ** one by prepending the data directory and a backslash.
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
43751 ** NOTE: We are dealing with a relative path name and the data
43754 ** one by prepending the data directory and a backslash.
43756 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
43817 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
43828 ** Interfaces for opening a shared library, finding entry points
43834 int nFull = pVfs->mxPathname+1;
43899 unsigned char *a; /* Gather entropy into this buffer */
43900 int na; /* Size of a[] in bytes */
43901 int i; /* XOR next input into a[i] */
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;
43933 e.a = (unsigned char*)zBuf;
43979 ** Sleep for a little while. Return the amount of time slept.
43988 ** The following variable, if set to a non-zero value, is interpreted as
43998 ** the current time and date as a Julian Day number times 86_400_000. In
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 */
44046 ** current time and date as a Julian Day number into *prNow and
44060 ** The idea is that this function works like a combination of
44064 ** a buffer of nBuf bytes. The OS layer should populate the
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
44248 ** a legal notice, here is a blessing:
44255 ** This file implements an object that represents a fixed-length
44258 ** A bitmap is used to record which pages of a database file have been
44259 ** journalled during a transaction, or which pages have the "dont-write"
44260 ** property. Usually only a few pages are meet either condition.
44262 ** But sometimes (for example when during a DROP of a large table) most
44263 ** or all of the pages in a database can get journalled. In those cases,
44270 ** may be set or cleared one at a time.
44277 ** start of a transaction, and is thus usually less than a few thousand,
44278 ** but can be as large as 2 billion for a really big database.
44288 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
44291 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
44297 /* Number of elements in a bitmap array. */
44305 ** sub-dividing and re-hashing. */
44310 ** no fewer collisions than the no-op *1. */
44317 ** A bitmap is an instance of the following structure.
44323 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
44327 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
44330 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
44339 u32 nSet; /* Number of bits that are set - only valid for aHash
44345 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
44354 ** Create a new bitmap object able to handle bits between 0 and iSize,
44355 ** inclusive. Return a pointer to the new object. Return NULL if
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
44408 ** The calling function must ensure that p is a valid Bitvec object
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));
44432 /* if there wasn't a hash collision, and this doesn't */
44434 /* worring about sub-dividing and re-hashing. */
44435 if( !p->u.aHash[h] ){
44436 if (p->nSet<(BITVEC_NINT-1)) {
44442 /* there was a collision, check to see if it's already */
44443 /* in hash, if not, try to find a spot for it */
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.
44480 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
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];
44518 ** Destroy a bitmap object. Reclaim all memory used.
44522 if( p->iDivisor ){
44525 sqlite3BitvecDestroy(p->u.apSub[i]);
44536 return p->iSize;
44553 ** The input is an array of integers that acts as a program
44569 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
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;
44587 /* Allocate the Bitvec to be tested and a linear array of
44606 i = aOp[pc+2] - 1;
44618 if( (--aOp[pc+1]) > 0 ) nx = 0;
44634 ** match (rc==0). Change rc to non-zero if a discrepancy
44639 + (sqlite3BitvecSize(pBitvec) - sz);
44662 ** a legal notice, here is a blessing:
44674 ** A complete page cache is an instance of this structure. Every
44675 ** entry in the cache holds a single page of the database file. The
44678 ** A page cache entry is "clean" if it exactly matches what is currently
44679 ** on disk. A page is "dirty" if it has been modified and needs to be
44685 ** such that p was added to the list more recently than p->pDirtyNext.
44689 ** The PCache.pSynced variable is used to optimize searching for a dirty
44690 ** page to eject from the cache mid-transaction. It is better to eject
44691 ** a page that does not require a journal sync than one that does.
44693 ** to either the oldest page in the pDirty/pDirtyTail list that has a
44694 ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
44708 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
44719 ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
44720 ** is displayed for many operations, resulting in a lot of output.
44731 unsigned char *a;
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;
44743 for(j=0; j<12; j++) printf("%02x", a[j]);
44745 if( pPg->pPage==0 ){
44746 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
44756 ** Check invariants on a PgHdr entry. Return true if everything is OK.
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 */
44787 ** syncing the rollback journal, which might cause corruption on a power
44791 ** disk sector size. When any page of a sector is journalled, all pages
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;
44841 ** searching for a dirty page to eject from the cache when it might
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;
44870 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
44872 ** optimization, as if pSynced points to a page with the NEED_SYNC
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. */
44935 ** Return the size in bytes of a PCache object.
44940 ** Create a new PCache object. Storage space to hold the object
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;
44998 ** Try to obtain a page from the cache.
45000 ** This routine returns a pointer to an sqlite3_pcache_page object if
45001 ** such an object is already in cache, or if a new one is created.
45002 ** This routine returns a NULL pointer if the object was not in cache
45006 ** be 3 (not 1, but 3) to try to create a new page.
45009 ** is not already in the cache. If createFlag is 1, then a new page
45014 ** initialize the sqlite3_pcache_page object and convert it into a
45018 ** the stack on entry and pop them back off on exit, which saves a
45030 assert( pCache->pCache!=0 );
45032 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
45035 ** 0 Do not allocate a new page. (createFlag==0)
45036 ** 1 Allocate a new page if doing so is inexpensive.
45038 ** 2 Allocate a new page even it doing so is difficult.
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);
45052 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
45055 ** try harder to allocate a page. This routine might invoke the stress
45057 ** allocate the new page and will only fail to allocate a new page on
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
45076 ** If the LRU page in the dirty list that has a clear 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);
45110 ** This is a helper routine for sqlite3PcacheFetchFinish()
45114 ** This routine is broken out into a separate function since it
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;
45141 ** must be called after sqlite3PcacheFetch() in order to get a usable
45152 pPgHdr = (PgHdr *)pPage->pExtra;
45154 if( !pPgHdr->pPage ){
45157 pCache->nRefSum++;
45158 pPgHdr->nRef++;
45164 ** Decrement the reference count on a page. If the page is clean and the
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
45184 ** Increase the reference count of a supplied page by 1.
45187 assert(p->nRef>0);
45189 p->nRef++;
45190 p->pCache->nRefSum++;
45194 ** Drop a page from the cache. There must be exactly one reference to the
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) ){
45299 ** other than page 1 with a page number greater than pgno.
45301 ** If there is a reference to page 1 and the pgno parameter passed to this
45306 if( pCache->pCache ){
45310 for(p=pCache->pDirty; p; p=pNext){
45311 pNext = p->pDirtyNext;
45312 /* This routine never gets call with a positive pgno except right
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);
45336 ** Close a cache.
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;
45386 ** Since there cannot be more than 2^31 distinct pages in a database,
45393 PgHdr *a[N_SORT_BUCKET], *p;
45395 memset(a, 0, sizeof(a));
45398 pIn = p->pDirty;
45399 p->pDirty = 0;
45400 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
45401 if( a[i]==0 ){
45402 a[i] = p;
45405 p = pcacheMergeDirtyList(a[i], p);
45406 a[i] = 0;
45409 if( NEVER(i==N_SORT_BUCKET-1) ){
45413 a[i] = pcacheMergeDirtyList(a[i], p);
45416 p = a[0];
45418 if( a[i]==0 ) continue;
45419 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
45425 ** Return a list of all dirty pages in the cache, sorted by page number.
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.
45513 ** Return the number of dirty pages currently in the cache, as a percentage
45520 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
45532 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
45544 ** a legal notice, here is a blessing:
45558 ** A Page cache line looks like this:
45560 ** -------------------------------------------------------------
45562 ** -------------------------------------------------------------
45570 ** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
45571 ** PgHdr1 contains information needed to look up a page by its page number.
45578 ** size can vary according to architecture, compile-time options, and
45582 ** using a separate memory allocation from the database page content. This
45584 ** fragmentation" in academic literature) of allocating a few bytes more
45585 ** than a power of two with the memory allocator rounding up to the next
45586 ** power of two, and leaving the rounded-up space unused.
45596 ** Memory for a page might come from any of three sources:
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.
45603 ** The third case is a chunk of heap memory (defaulting to 100 pages worth)
45609 ** If N is positive, then N pages worth of memory are allocated using a single
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
45631 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
45632 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
45647 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
45649 ** pages when they are under memory pressure. A PGroup is an instance of
45657 ** (2) There is a single global PGroup that all PCaches are a member
45661 ** unused pages from other PCaches) but it also operates without a mutex,
45662 ** and is therefore often faster. Mode 2 requires a mutex in order to be
45665 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
45673 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
45679 ** open database file (including each in-memory database and each
45680 ** temporary or transient database) has a single page cache which
45689 ** modified at any time by a call to the pcache1Cachesize() method.
45709 PgHdr1 *pFree; /* List of unused pcache-local pages */
45710 void *pBulk; /* Bulk memory used by pcache-local */
45733 int separateCache; /* Use a new PGroup for each PCache */
45743 /* The following value requires a mutex to change. We skip the mutex on
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)
45775 ** This function is called during initialization if a static buffer is
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.
45812 /* Do not bother with a bulk allocation if the cache size very small */
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;
45919 ** Return the size of a pcache allocation
45936 ** Allocate a new page object initially associated with cache pCache.
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++;
45986 ** Free a page object allocated by pcache1AllocPage().
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--;
46025 ** Return true if it desirable to avoid allocating a new page cache
46030 ** it is desirable to avoid allocating a new page cache entry because
46037 ** allocating a new page cache entry in order to avoid stressing
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;
46163 ** Discard all pages from cache pCache with a page number (key value)
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)
46285 ** Allocate a new cache.
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 ){
46332 ** Configure the cache_size limit for a cache.
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);
46384 ** This steps are broken out into a separate procedure because they are
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 );
46413 /* Step 4. Try to recycle a page. */
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,
46433 ** attempt to allocate a new one.
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;
46460 ** Fetch a page by key value.
46462 ** Whether or not a new page may be allocated by this function depends on
46463 ** the value of the createFlag argument. 0 means do not allocate a new
46464 ** page. 1 means allocate a new page if space is easily available. 2
46465 ** means to try really hard to allocate a new page.
46467 ** For a non-purgeable cache (a cache used as the storage for an in-memory
46469 ** the calling function (pcache.c) will never have a createFlag of 1 on
46470 ** a non-purgeable cache.
46472 ** There are three different approaches to obtaining space for a page,
46475 ** 1. Regardless of the value of createFlag, the cache is searched for a
46482 ** return NULL (do not allocate a new page) if any of the following
46485 ** (a) the number of pages pinned by the cache is greater than
46495 ** (a) The number of pages allocated for the cache is already
46505 ** then attempt to recycle a page from the LRU list. If it is the right
46509 ** 5. Otherwise, allocate and return a new page buffer.
46512 ** the general case. pcache1FetchNoMutex() is a faster implementation for
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 ){
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 ){
46590 ** Mark a page as unpinned (eligible for asynchronous recycling).
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);
46664 ** Discard all unpinned pages in the cache with a page number equal to
46665 ** or greater than parameter iLimit. Any pinned pages with a page number
46670 pcache1EnterMutex(pCache->pGroup);
46671 if( iLimit<=pCache->iMaxKey ){
46673 pCache->iMaxKey = iLimit-1;
46675 pcache1LeaveMutex(pCache->pGroup);
46681 ** Destroy a cache allocated using pcache1Create().
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 );
46803 ** a legal notice, here is a blessing:
46811 ** This module implements an object we call a "RowSet".
46813 ** The RowSet object is a collection of rowids. Rowids
46815 ** can be intermixed with tests to see if a given rowid has been
46822 ** Hence, the primitive operations for a RowSet are:
46831 ** obviously. The INSERT primitive adds a new element to the RowSet.
46840 ** The TEST primitive includes a "batch" number. The TEST primitive
46846 ** a non-zero batch number, it will see all prior INSERTs.
46848 ** No INSERTs may occurs after a SMALLEST. An assertion will fail if
46852 ** has to be allocated on an INSERT.) The cost of a TEST with a new
46854 ** The cost of a TEST using the same batch number is O(logN). The cost
46874 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
46877 ** Each entry in a RowSet is an instance of the following object.
46879 ** This same object is reused to store a linked list of trees of RowSetEntry
46893 ** chunks are kept on a linked list so that they can be deallocated
46902 ** A RowSet in an instance of the following structure.
46904 ** A typedef of this structure if found in sqliteInt.h.
46925 ** Turn bulk memory into a RowSet object. N bytes of memory
46926 ** are available at pSpace. The db pointer is used as a memory context
46928 ** Return a pointer to the new RowSet object.
46930 ** It must be the case that N is sufficient to make a Rowset. If not
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;
46953 ** Deallocate all chunks from a RowSet. This frees all memory that
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;
46972 ** Allocate a new RowSetEntry object that is associated with the
46973 ** given RowSet. Return a pointer to the new and completely uninitialized
46976 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
46981 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
46982 /* We could allocate a fresh RowSetEntry each time one is needed, but it
46983 ** is more efficient to pull a preallocated entry from the pool */
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++;
46999 ** Insert a new value into a RowSet.
47001 ** The mallocFailed flag of the database connection is set if a
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;
47095 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
47096 ** Convert this tree into a linked list connected by the pRight pointers
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 );
47122 ** Convert a sorted list of elements (connected by pRight) into a binary
47123 ** tree with depth of iDepth. A depth of 1 means the tree contains a single
47124 ** node taken from the head of *ppList. A depth of 2 means a tree with
47132 ** Return a pointer to the root of the constructed binary tree.
47140 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
47144 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
47145 /* This branch causes a *balanced* tree to be generated. A valid tree
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;
47167 ** Convert a sorted list of elements into a binary tree. Make the tree
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() */
47206 /* Merge the forest into a single sorted list on first call */
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*/
47232 ** If this is the first test of a new batch and if there exist entries
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 );
47242 /* Sort entries into the forest on the first test of a new batch.
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;
47304 ** a legal notice, here is a blessing:
47313 ** The pager is used to access a database disk file. It implements
47314 ** atomic commit and rollback through the use of a journal file that
47328 ** a legal notice, here is a blessing:
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. */
47384 /* Set the limiting size of a WAL file. */
47387 /* Used by readers to open (lock) and close (unlock) a snapshot. A
47388 ** snapshot is like a read-transaction. It is the state of the database
47389 ** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
47397 /* Read a page from the write-ahead log, if it is present. */
47416 ** response to a ROLLBACK TO command. */
47419 /* Write a frame or frames to the log. */
47424 Wal *pWal, /* Write-ahead log connection */
47436 /* Return the value to pass to a sqlite3_wal_hook callback, the
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).
47479 ** This comment block describes invariants that hold when using a rollback
47483 ** Within this comment block, a page is deemed to have been synced
47488 ** Definition: A page of the database file is said to be "overwriteable" if
47491 ** (a) The original content of the page as it was at the beginning of
47495 ** (b) The page was a freelist leaf page at the start of the transaction.
47500 ** (1) A page of the database file is never overwritten unless one of the
47503 ** (a) The page and all other pages on the same sector are overwriteable.
47507 ** number consists of a single page change.
47509 ** (2) The content of a page written into the rollback journal exactly matches
47515 ** in length and are aligned on a page boundary.
47517 ** (4) Reads from the database file are either aligned on a page boundary and
47524 ** (6) If a master journal file is used, then all writes to the database file
47534 ** of the unsynced changes to a rollback journal are removed and the
47538 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
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
47558 ** (13) A SHARED lock is held on the database file while reading any
47576 ** to print out file-descriptors.
47578 ** PAGERID() takes a pointer to a Pager struct as its argument. The
47579 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
47582 #define PAGERID(p) ((int)(p->fd))
47586 ** The Pager.eState variable stores the current 'state' of a pager. A
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
47640 ** A connection running with locking_mode=normal enters this state when
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
47645 ** a locking_mode=exclusive connection can transition from READER to OPEN
47648 ** * A read transaction may be active (but a write-transaction cannot).
47649 ** * A SHARED or greater lock is held on the database file.
47650 ** * The dbSize variable may be trusted (even if a user-level read
47653 ** * If the database is a WAL database, then the WAL connection is open.
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
47664 ** In rollback mode, a RESERVED or (if the transaction was opened with
47675 ** * A write transaction is active.
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
47688 ** A pager moves from WRITER_LOCKED state to this state when a page is
47690 ** is opened (if it is not already open) and a header written to the
47693 ** * A write transaction is active.
47694 ** * A RESERVED or greater lock is held on the database file.
47706 ** * A write transaction is active.
47715 ** It is not possible for a WAL connection to enter this state.
47717 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
47724 ** * A write transaction is active.
47733 ** The ERROR state is entered when an IO or disk-full error (including
47734 ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
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
47741 ** For example, if an IO error occurs while performing a rollback,
47742 ** the contents of the page-cache may be left in an inconsistent state.
47744 ** (as usually happens after a rollback). Any subsequent readers might
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
47762 ** 1. An error occurs while attempting a rollback. This happens in
47765 ** 2. An error occurs while attempting to finalize a journal file
47766 ** following a commit in function sqlite3PagerCommitPhaseTwo().
47772 ** In other cases, the error is returned to the b-tree layer. The b-tree
47773 ** layer then attempts a rollback operation. If the error condition
47776 ** Condition (3) is necessary because it can be triggered by a read-only
47777 ** statement executed within a transaction. In this case, if the error
47778 ** code were simply returned to the user, the b-tree layer would not
47779 ** automatically attempt a rollback, as it assumes that an error in a
47780 ** read-only statement cannot leave the pager in an internally inconsistent
47786 ** * The pager is not an in-memory pager.
47791 ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
47792 ** connection is open in WAL mode. A WAL connection is always in one
47795 ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
47796 ** state. There are two exceptions: immediately after exclusive-mode has
47812 ** following locking-states, according to the lock currently held on
47820 ** pagerUnlockDb() take a conservative approach - eLock is always updated
47823 ** to a less exclusive (lower) value than the lock that is actually held
47824 ** at the system level, but it is never set to a more exclusive value.
47827 ** be a few redundant xLock() calls or a lock may be held for longer than
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.
47838 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
47841 ** doesn't know it because of a previous error in xUnlock). If this happens
47842 ** a hot-journal may be mistaken for a journal being created by an active
47846 ** To work around this, if a call to xUnlock() fails when unlocking the
47848 ** is only changed back to a real locking state after a successful call
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
47861 ** A macro used for invoking the codec if there is one
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 */
47876 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
47877 ** This could conceivably cause corruption following a power failure on
47878 ** such a system. This is currently an undocumented limit.
47889 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
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 */
47916 ** An open page cache is an instance of struct Pager. A description of
47922 ** diagram above for a description of the pager state.
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
47948 ** relinquished on the database file. Each time a transaction is committed,
47950 ** updating the change-counter is omitted for the current transaction.
47952 ** This mechanism means that when running in exclusive mode, a connection
47953 ** need only update the change-counter once, for the first transaction
47958 ** When PagerCommitPhaseOne() is called to commit a transaction, it may
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).
47989 ** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
47991 ** to allocate a new page to prevent the journal file from being written
47993 ** case is a user preference.
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.
48007 ** This variable is updated by the upper layer each time a new
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
48031 ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
48036 ** Throughout a write-transaction, dbFileSize contains the size of
48037 ** the file on disk in pages. It is set to a copy of dbSize when the
48038 ** write-transaction is first opened, and updated when VFS calls are made
48042 ** unnecessary calls to xTruncate() after committing a transaction. If,
48043 ** when a transaction is committed, the dbFileSize variable indicates
48047 ** dbFileSize is not used when rolling back a transaction. In this case
48049 ** a call to xFilesize() that is not strictly required). In either case,
48057 ** dbHintSize is set to a copy of the dbSize variable when a
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.
48075 u8 useJournal; /* Use a rollback journal on this file */
48082 u8 tempFile; /* zFilename is a temporary or immutable file */
48084 u8 readOnly; /* True for a read-only database */
48090 ** when the pager is first created or else only change when there is a
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 */
48103 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
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 */
48135 int pageSize; /* Number of bytes in a page */
48147 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
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.
48189 ** was obtained from /dev/random. It is used only as a sanity check.
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
48204 ** because garbage data that appears at the end of a journal is likely
48218 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
48222 ** size as a single disk sector. See also setSectorSize().
48224 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
48227 ** The macro MEMDB is true if we are dealing with an in-memory database.
48228 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
48229 ** the value of MEMDB will be a constant and the compiler will optimize
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).
48254 ** The argument to this macro is a file descriptor (type sqlite3_file*).
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 );
48343 /* If changeCountDone is set, a RESERVED lock or greater must be held
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 );
48380 ** a rollback transaction that switches from journal_mode=off
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 );
48431 ** Return a pointer to a human readable string in a static buffer
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.
48558 #define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
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
48625 ** (a) the value returned by OsDeviceCharacteristics() indicates that
48626 ** a database page may be written atomically, and
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;
48666 ** on the cache using a hash function. This is used for testing
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);
48689 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
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) );
48709 ** This function attempts to read a master journal file name from the
48712 ** used to store a master journal file name at the end of a journal file.
48714 ** zMaster must point to a buffer of at least nMaster bytes allocated by
48717 ** name in the journal is longer than nMaster bytes (including a
48718 ** nul-terminator), then this is handled as if no master journal name
48721 ** If a master journal file name is present at the end of the journal
48722 ** file, then it is copied into the buffer pointed to by zMaster. A
48723 ** nul-terminator byte is appended to the buffer following the master
48738 unsigned char aMagic[8]; /* A buffer to hold the magic header */
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];
48761 ** definitely roll back, so just return SQLITE_OK and report a (nul)
48762 ** master-journal filename.
48773 ** following the value in pPager->journalOff, assuming a sector
48774 ** size of pPager->sectorSize bytes.
48776 ** i.e for a sector size of 512:
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);
48838 ** is still held on the file. If there is a size limit configured for
48845 rc = sqlite3OsFileSize(pPager->jfd, &sz);
48847 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
48855 ** The journal file must be open when this routine is called. A journal
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
48901 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
48904 ** is dangerous, as if a failure occurred whilst writing to the journal
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));
48946 ** record is written to the following sector (leaving a gap in the file
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;
48971 ** The journal file must be open when this is called. A journal header file
48974 ** pPager->journalOff. See comments above function writeJournalHdr() for
48975 ** a description of the journal header format.
48979 ** database before the transaction began, in pages. Also, pPager->cksumInit
48995 unsigned char aMagic[8]; /* A buffer to hold the magic header */
48998 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
49001 ** journal file is too small for there to be a header stored at this
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
49056 ** are within range. To be 'in range', both values need to be a power
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.
49073 ** Use a testcase() macro to make sure that malloc failure within
49076 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
49079 /* Update the assumed sector-size to match the value used by
49081 ** created by a process other than this one, then this routine
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.
49109 ** If zMaster is a NULL pointer (occurs for a single database transaction),
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;
49216 ** or SQLITE_NOMEM if a malloc failure occurs.
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;
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;
49332 ** The first argument is a pointer to the pager structure, the second
49333 ** the error-code about to be returned by a pager API function. The
49334 ** value returned is a copy of the second argument to this function.
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
49377 ** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
49378 ** file has been created already (via a spill on pagerStress()) and
49383 if( pPager->tempFile==0 ) return 1;
49385 if( !isOpen(pPager->fd) ) return 0;
49386 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
49390 ** This routine ends a transaction. A transaction is usually ended by
49391 ** either a COMMIT or a ROLLBACK operation. This routine may be called
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.
49402 ** If the journal file is open, then it is "finalized". Once a journal
49403 ** file has been finalized it is not possible to use it to roll back a
49404 ** transaction. Nor will it be considered to be a hot-journal by this
49405 ** or any other database connection. Exactly how a journal is finalized
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
49431 ** downgraded to a SHARED_LOCK.
49447 ** or at least a RESERVED lock. This function may be called when there
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
49454 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
49455 ** lock switches back to locking_mode=normal and then executes a
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 ){
49481 ** Otherwise the journal might resurrect following a power loss and
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;
49572 ** Execute a rollback if a transaction is active and unlock the
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
49580 ** connection to obtain a shared lock on the pager (which may be this one)
49584 ** malloc error occurs during a rollback, then this will itself cause
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
49605 ** of data. Compute and return a checksum based ont the contents of the
49606 ** page of data and the current value of pPager->cksumInit.
49608 ** This is not a real checksum. It is really just the sum of the
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.
49616 ** If journal corruption occurs due to a power failure, the most likely
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 */
49650 ** pager as it is in the source. This comes up when a VACUUM changes the
49654 if( pDest->nReserve!=pSrc->nReserve ){
49655 pDest->nReserve = pSrc->nReserve;
49662 ** Read a single page from either the journal file (if isMainJrnl==1) or
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
49674 ** If pDone is not NULL, then it is a record of pages that have already
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
49692 ** Neither of these two scenarios are possible during a savepoint rollback.
49694 ** If this is a savepoint rollback, then memory may have to be dynamically
49702 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
49703 int isSavepnt /* True for a savepoint rollback */
49707 Pgno pgno; /* The page number of a page in 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;
49727 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
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;
49749 ** thought. If a power failure occurs while the journal is being written,
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];
49782 /* If the pager is in CACHEMOD state, then there must be a copy of this
49786 ** An exception to the above rule: If the database is in no-sync mode
49787 ** and a page is moved during an incremental vacuum then the page may
49788 ** not be in the pager cache. Later: if a malloc() or IO error occurs
49789 ** during a Movepage() call, then the page may not be in the cache
49796 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
49801 ** This occurs when a page is changed prior to the start of a statement
49802 ** then changed again within the statement. When rolling back such a
49805 ** journal. Otherwise, a power loss might leave modified data in the
49813 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
49814 ** is possible to fail a statement on a database that does not yet exist.
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);
49870 /* If this is a rollback of a savepoint and data was not written to
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
49876 ** There are a couple of different ways this can happen. All are quite
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;
49898 ** database active. However such a page may be rolled back as a result
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); }
49928 ** Parameter zMaster is the name of a master journal file. A single journal
49936 ** When a master journal file is created, it is populated with the names
49937 ** of all of its child journals, one after another, formatted as utf-8
49938 ** encoded text. The end of each child journal file is marked with a
49939 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
49940 ** file for a transaction involving two databases might be:
49942 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
49944 ** A master journal file may only be deleted once all of its child
49947 ** This function reads the contents of the master-journal file into
49952 ** * if the child journal contains a reference to master journal
49955 ** If a child journal can be found that matches both of the criteria
49958 ** the file-system using sqlite3OsDelete().
49965 ** TODO: This function allocates a single block of memory to load
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 */
49978 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
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 ){
50039 /* We have a match. Do not delete the master journal file. */
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).
50073 ** you try to truncate a file to some size that is larger than it
50074 ** currently is, so detect this case and write a single zero byte to
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, ¤tSize);
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
50144 ** might change if a crash occurs while writing to a single byte in
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);
50172 ** (1) 8 byte prefix. A copy of aJournalMagic[].
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
50179 ** database to during a rollback.
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.
50194 ** value of nRec from the size of the journal file. But if a power
50197 ** the extra entries had not yet made it safely to disk. In such a case,
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
50216 ** The isHot parameter indicates that we are trying to rollback a journal
50217 ** that might be a hot journal. Or, it could be that the journal is
50224 sqlite3_vfs *pVfs = pPager->pVfs;
50229 int rc; /* Result code of a subroutine */
50238 assert( isOpen(pPager->jfd) );
50239 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
50245 ** If a master journal file name is specified, but the file is not
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;
50267 /* This loop terminates either when a readJournalHdr() or
50273 ** not enough bytes left in the journal file for a complete header, or
50274 ** it is corrupted, then a process must have failed while writing it.
50285 /* If nRec is 0xffffffff, then this journal was created by a process
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));
50295 /* If nRec is 0 and this rollback is of a transaction created by this
50302 ** When rolling back a hot journal, nRec==0 always means that the next
50304 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
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;
50343 ** not completely written and synced prior to a crash. In that
50363 /* Following a rollback, the database file should be back in its original
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);
50374 /* If this playback is happening automatically as a result of an IO or
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)
50400 /* If there was a master journal and this routine will return success,
50408 nPlayback, pPager->zJournal);
50412 ** back a journal created by a process with a different sector size
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);
50457 ** that will never be a valid file version. dbFileVers[] is a copy
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);
50510 ** written into the log file when a WAL transaction is rolled back.
50512 ** is actually a pointer to the Pager structure.
50532 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
50537 pPager->xReiniter(pPg);
50543 /* Normally, if a transaction is rolled back, any backup processes are
50545 ** database. This is not generally possible with a WAL database, as
50551 sqlite3BackupRestart(pPager->pBackup);
50557 ** This function is called to rollback a transaction on a WAL database.
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);
50583 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
50595 int isCommit /* True if this is a commit */
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 );
50612 /* If a WAL transaction is being committed, there is no point in writing
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){
50651 ** Begin a read transaction on the WAL.
50654 ** makes a snapshot of the database at the current point in time and preserves
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
50745 ** The caller must hold a SHARED lock on the database file to call this
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;
50785 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
50788 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
50801 ** * Pages are then played back from the sub-journal file, starting
50805 ** Throughout the rollback process, each time a page is rolled back, the
50806 ** corresponding bit is set in a bitvec structure (variable pDone in the
50807 ** implementation below). This is used to ensure that a page is only
50811 ** journal file. There is no need for a bitvec in this case.
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 );
50827 /* Allocate a bitvec to use to store the set of pages rolled back */
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;
50855 ** There might be records in the main journal that have a page number
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);
50988 ** in a state which would cause damage to the database
50992 ** database (with some additional information - the nRec field
50993 ** of the journal header - being written in between the two
50994 ** syncs). If we assume that writing a
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;
51066 ** attempts to open a temporary file. This information is used for
51074 ** Open a temporary file.
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,
51111 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
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).
51153 ** * the new page size (value of *pPageSize) is valid (a power
51158 ** * the database is either not an in-memory database or it is
51159 ** an in-memory database that currently consists of zero pages.
51164 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
51176 /* It is not possible to do a full assert_pager_state() here, as this
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;
51229 ** Return a pointer to the "temporary page" buffer held internally
51230 ** by the pager. This is a buffer that is big enough to hold the
51231 ** entire content of a database page. This buffer is used internally
51232 ** during rollback and will be overwritten whenever a rollback
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;
51284 ** If the pager was opened on a transient file (zFilename==""), or
51285 ** opened on a file less than N bytes in size, the output buffer is
51287 ** function is used to read database headers, and a new transient or
51288 ** zero sized database has a header than consists entirely of zeroes.
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
51320 ** this is considered a 1 page file.
51323 assert( pPager->eState>=PAGER_READER );
51324 assert( pPager->eState!=PAGER_WRITER_FINISHED );
51325 *pnPage = (int)pPager->dbSize;
51330 ** Try to obtain a lock of type locktype on the database file. If
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:
51366 ** a) The page number is less than or equal to the size of the
51370 ** be necessary to write the current content out to the sub-journal
51376 ** the database file. If a savepoint transaction were rolled back after
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
51402 ** This function is only called right before committing a transaction.
51408 assert( pPager->dbSize>=nPage );
51409 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
51410 pPager->dbSize = nPage;
51417 ** called right before committing a transaction. So although the
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);
51451 ** Obtain a reference to a memory mapped page object for page number pgno.
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++;
51501 ** Release a reference to page pPg. pPg must have been returned by an
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;
51530 ** If a transaction was in progress when this routine is called, that
51532 ** and their memory is freed. Any attempt to use a page associated
51534 ** result in a coredump.
51536 ** This function always succeeds. If a transaction is active an attempt
51538 ** a hot journal may be left in the filesystem but no error is returned
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;
51564 ** file may be played back into the database. If a power failure occurs
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.
51682 ** a valid header following Pager.journalOff, then write a 0x00
51687 ** as a temporary buffer to inspect the first couple of bytes of
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
51710 ** it as a candidate for rollback.
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;
51761 ** The argument is the first in a linked list of dirty pages connected
51763 ** in-memory pages in the list to the database file. The argument may
51765 ** a no-op.
51767 ** The pager must hold at least a RESERVED lock when this function
51772 ** If the pager is a temp-file pager and the actual file-system file
51778 ** a page is skipped if it meets either of the following criteria:
51783 ** If writing out a page causes the database file to grow, Pager.dbFileSize
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);
51810 /* Before the first write, give the VFS a hint of what the final
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.
51881 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
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
51907 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
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);
51962 ** soft memory limit. The first argument is a pointer to a Pager object
51963 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
51964 ** database). The second argument is a reference to a page that is
51983 assert( pPg->pPager==pPager );
51984 assert( pPg->flags&PGHDR_DIRTY );
51986 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
51987 ** journal (and adding a new header) is not allowed. This occurs
51992 ** regardless of whether or not a sync is required. This is set during
51993 ** a rollback or by user request, respectively.
51999 ** be called in the error state. Nevertheless, we include a NEVER()
52000 ** test for the error state as a safeguard against future changes.
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;
52015 /* Write a single frame for this page to the log. */
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 ){
52066 ** Allocate and initialize a new Pager object and put a pointer to it
52071 ** If zFilename is NULL then a randomly-named temporary file is created
52075 ** This can be used to implement an in-memory database.
52079 ** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
52087 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
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). */
52141 ** to by zPathname, length nPathname. Or, if this is a temporary file,
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);
52241 ** choose a default page size in case we have to create the
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);
52282 /* If a temporary file is requested, it is not opened immediately.
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
52413 ** PAGER_SHARED state. It tests if there is a hot journal present in
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
52419 ** * No process holds a RESERVED or greater lock on the database file, and
52423 ** If the current size of the database file is 0 but a journal file
52424 ** exists, that is probably an old journal left over from a prior
52429 ** This routine does not check if there is a master journal filename
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;
52445 int exists = 1; /* True if a journal file is present */
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);
52461 int locked = 0; /* True if some process holds a RESERVED lock */
52464 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
52467 ** is the case, this routine might think there is a hot journal when
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 );
52479 ** journal is a remnant from a prior database with the same name where
52481 ** transaction that populates a new database is being rolled back.
52489 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
52490 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
52494 /* The journal file exists and no other connection has a reserved
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);
52516 ** it has a zero header, that might be due to an I/O error, or
52519 ** This might be a false positive. But if it is, then the
52536 ** This function is called to obtain a shared lock on the database file.
52538 ** has been successfully called. If a shared-lock is already held when
52539 ** this function is called, it is a no-op.
52544 ** on the database file), then an attempt is made to obtain a
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
52560 ** rolling back a journal file, the IO error code is returned.
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 );
52586 /* If a journal file exists, and there is no RESERVED lock on the
52589 if( pPager->eLock<=SHARED_LOCK ){
52596 if( pPager->readOnly ){
52602 ** important that a RESERVED lock is not obtained on the way to the
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
52680 ** PAGER_ERROR now. This is not actually counted as a transition
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
52702 ** occurring on the very first access to a file, in order to save a
52703 ** single unnecessary sqlite3OsRead() call at the start-up.
52707 ** a 32-bit counter that is incremented with each change. The
52709 ** a codec is in use.
52711 ** There is a vanishingly small chance that a change will not be
52715 char dbFileVers[sizeof(pPager->dbFileVers)];
52718 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
52726 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
52731 ** to its original size while this process was not holding a lock.
52732 ** In this case there may exist a Pager.pMap mapping that appears
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) ){
52786 ** The page getter methods each try to acquire a reference to a
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
52798 ** Otherwise, a new page object is allocated and populated with data
52800 ** choose not to allocate a new page object and may reuse an existing
52803 ** The extra data appended to a page is always initialized to zeros the
52804 ** first time a page is loaded into memory. If the page requested is
52817 ** a) When reading a free-list leaf page from the database, and
52819 ** b) When a savepoint is being rolled back and we need to load
52820 ** a new page into the cache to be filled with the data read
52828 ** point in the future, using a call to sqlite3PagerWrite(), its contents
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
52845 DbPage **ppPage, /* Write a pointer to the page here */
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]++;
52883 /* The pager cache has created a new page. Its content needs to
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.
52904 ** It merely means that we might do some extra work to journal a
52906 ** to test the case where a malloc error occurs while trying to set
52907 ** a bit in a bit vector.
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 */
52952 DbPage **ppPage, /* Write a pointer to the page here */
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
52961 ** flag was specified by the caller. And so long as the db is not a
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);
53024 DbPage **ppPage, /* Write a pointer to the page here */
53029 assert( pPager->errCode!=SQLITE_OK );
53031 return pPager->errCode;
53040 DbPage **ppPage, /* Write a pointer to the page here */
53043 return pPager->xGet(pPager, pgno, ppPage, flags);
53047 ** Acquire a page if it is already in the in-memory cache. Do
53048 ** not read the page from disk. Return a pointer to the page,
53054 ** returns NULL if the page is not in cache or if a disk I/O error
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);
53069 ** Release a page reference.
53073 ** are released, a rollback occurs and the lock on the database is
53079 pPager = pPg->pPager;
53080 if( pPg->flags & PGHDR_MMAP ){
53093 ** There must already be a RESERVED or EXCLUSIVE lock on the database
53096 ** Open the journal file for pager pPager and write a journal header
53097 ** to the start of it. If there are active savepoints, open the sub-journal
53099 ** opened to write a rollback log for a transaction. It is not used
53100 ** when opening a hot journal file to roll it back.
53103 ** then this function just writes a journal header to the start of the
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.
53189 ** If the exFlag argument is false, then acquire at least a RESERVED
53191 ** an EXCLUSIVE lock. If such a lock is already held, no locking
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
53197 ** running in exclusive mode) or if the transaction does not require a
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,
53200 ** or using a temporary file otherwise.
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);
53231 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
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);
53322 ** Mark a single data page as writeable. The page is written into the
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 );
53364 /* If a rollback journal is in use, them make sure the page that is about
53365 ** to change is in the rollback journal, or if the page is a new page off
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;
53410 ** This is a variant of sqlite3PagerWrite() that runs when the sector size
53412 ** all bytes of a sector are written together by hardware. Hence, all bytes of
53413 ** a sector need to be journalled in case of a power loss in the middle of
53414 ** a write.
53427 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
53428 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
53431 ** a journal header to be written between the pages journaled by
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;
53501 ** Mark a data page as writeable. This routine must be called before
53502 ** making changes to a page. The caller must check the return value
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;
53544 ** A call to this routine tells the pager that it is not necessary to
53547 ** the page has been added as a leaf of the freelist and so its
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
53594 ** by writing an updated version of page 1 using a call to the
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
53656 ** next time a read transaction is opened the cache will be
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
53724 ** of a master journal file that should be written into the individual
53726 ** journal (a single database transaction).
53730 ** * The database file change-counter is updated,
53731 ** * the journal is synced (unless the atomic-write optimization is used),
53740 ** Note that if zMaster==NULL, this does not overwrite a previous value
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
53762 /* If a prior error occurred, report that error again. */
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);
53853 /* Write the master journal name into the journal file. If a master
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
53866 ** journal requires a sync here. However, in locking_mode=exclusive
53867 ** on a system under memory pressure it is just possible that this is
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
53914 ** though, and if a failure occurs at this point it will eventually
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
53928 /* This routine should not be called if a prior error has occurred.
53929 ** But if (due to a coding error elsewhere in the system) it does get
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.
53943 ** The start of the journal file currently contains a single journal
53944 ** header with the nRec field set to 0. If such a journal is used as
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);
53966 ** If a write transaction is open, then all changes made within the
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 );
54031 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
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)
54061 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
54063 + pPager->pageSize;
54078 static int a[11];
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];
54090 return a;
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.
54131 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
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 );
54146 ** if the allocation fails. Otherwise, zero the new portion in case a
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 ){
54193 ** This function is called to rollback or release (commit) a savepoint.
54203 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
54204 ** (the first created). A value of (Pager.nSavepoint-1) means operate
54206 ** (Pager.nSavepoint-1), then this function is a no-op.
54208 ** If a negative value is passed to this function, then the current
54215 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
54218 ** This function may return SQLITE_NOMEM if a memory allocation fails,
54219 ** or an IO error code if an IO error occurs while rolling back a
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;
54246 /* If this is a release of the outermost savepoint, truncate
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;
54258 /* Else this is a rollback operation, playback the specified savepoint.
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;
54364 ** This function returns a pointer to a buffer containing the encrypted
54365 ** page content. If a malloc fails, this function may return NULL.
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
54394 ** A transaction must be active when this routine is called. It used to be
54395 ** required that a statement transaction was not active, but this restriction
54396 ** has been removed (CREATE INDEX needs to move a page when a statement
54399 ** If the fourth argument, isCommit, is non-zero, then this page is being
54400 ** moved as part of a database reorganization just before the transaction
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
54511 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
54515 ** the journal file twice, but that is not a problem.
54520 if( needSyncPgno<=pPager->dbOrigSize ){
54521 assert( pPager->pTmpSpace!=0 );
54522 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
54526 pPgHdr->flags |= PGHDR_NEED_SYNC;
54536 ** The page handle passed as the first argument refers to a dirty page
54537 ** with a page number other than iNew. This function changes the page's
54542 assert( pPg->pgno!=iNew );
54543 pPg->flags = flags;
54548 ** Return a pointer to the data for the specified page.
54551 assert( pPg->nRef>0 || pPg->pPager->memDb );
54552 return pPg->pData;
54556 ** Return a pointer to the Pager.nExtra bytes of "extra" space
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 */
54611 ** only. We invoke it once here to suppress a compiler warning. */
54625 ** the logic there will never allow a temporary file to be changed
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;
54657 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
54658 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
54661 ** not possible, then that is not a problem. Deleting the journal file
54664 ** Before deleting the journal file, obtain a RESERVED lock on the
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;
54709 ** Return TRUE if the pager is in a state where it is OK to change the
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);
54797 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
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
54851 ** The caller must be holding a SHARED lock on the database file to call
54854 ** If the pager passed as the first argument is open on a real database
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);
54944 ** If this is a WAL database, obtain a snapshot handle for the snapshot
54949 if( pPager->pWal ){
54950 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
54956 ** If this is a WAL database, store a pointer to pSnapshot. Next time a
54958 ** identifies. If this is not a WAL database, return an error.
54962 if( pPager->pWal ){
54963 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
54971 ** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
54972 ** is not a WAL database, return an error.
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
54991 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
54995 assert( pPager->eState>=PAGER_READER );
54996 return sqlite3WalFramesize(pPager->pWal);
55008 ** a legal notice, here is a blessing:
55016 ** This file contains the implementation of a write-ahead log (WAL) used in
55019 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
55021 ** A WAL file consists of a header followed by zero or more "frames".
55022 ** Each frame records the revised content of a single page from the
55024 ** frames into the WAL. Transactions commit when a frame is written that
55025 ** contains a commit marker. A single WAL can and usually does record
55027 ** transferred back into the database file in an operation called a
55030 ** A single WAL file can be used multiple times. In other words, the
55032 ** frames can overwrite the old ones. A WAL always grows from beginning
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.
55062 ** A frame is considered valid if and only if the following conditions are
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.
55076 ** The checksum values are always stored in the frame header in a
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
55101 ** following a crash.
55105 ** To read a page from the database (call it page number P), a reader
55107 ** last valid instance of page P that is a followed by a commit frame
55108 ** or is a commit frame itself becomes the value read. If the WAL
55109 ** contains no copies of page P that are valid and which are a commit
55110 ** frame or are followed by a commit frame, then page P is read from
55113 ** To start a read transaction, the reader records the index of the last
55117 ** and ignores the newly appended content, it will see a consistent snapshot
55118 ** of the database from a single point in time. This technique allows
55126 ** and read performance suffers. To overcome this problem, a separate
55127 ** data structure called the wal-index is maintained to expedite the
55128 ** search for frames of a particular page.
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
55135 ** on a network filesystem. All users of the database must be able to
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
55148 ** a page number P and a maximum frame index M, return the index of the
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
55167 ** that allows readers to query an index block for a specific page number.
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.
55192 ** prior to finding a match is 1. Each entry of the hash table is an
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
55198 ** contain a value of 0.
55200 ** To look for page P in the hash table, first compute a hash iKey on
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
55218 ** A hash search begins with the last index block and moves toward the
55224 ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
55225 ** comparisons (on average) suffice to either locate a frame in the
55230 ** reader might be using some value K0 and a second reader that started
55231 ** at a later time (after additional transactions were added to the WAL
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
55242 ** When a rollback occurs, the value of K is decreased. Hash table entries
55261 ** The maximum (and only) versions of the wal and wal-index formats
55264 ** If a client begins recovering a WAL file and finds that (a) the checksum
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
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
55331 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
55332 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
55335 ** nBackfillAttempted is the largest value of nBackfill that a checkpoint
55338 ** nBackfill is only set after all backfilling completes. So if a checkpoint
55342 ** The aLock[] field is a set of bytes used for locking. These bytes should
55345 ** There is one entry in aReadMark[] for each reader lock. If a reader
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
55354 ** The value of aReadMark[K] may only be changed by a thread that
55356 ** aReadMark[K] cannot changed while there is a reader is using that mark
55357 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
55361 ** in use (that is, every aReadMark[j] for which there is a corresponding
55378 ** We assume that 32-bit loads are atomic and so no locks are needed in
55391 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
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,
55419 ** assuming a database page size of szPage bytes. The offset returned
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 */
55443 u8 writeLock; /* True if in a write transaction */
55444 u8 ckptLock; /* True if holding a checkpoint lock */
55449 WalIndexHdr hdr; /* Wal-index header for current transaction */
55453 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
55455 u8 lockError; /* True if a locking error has occurred */
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
55519 #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
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;
55560 /* Request a pointer to the required page from the VFS */
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.
55616 ** nByte must be a positive multiple of 8.
55619 int nativeCksum, /* True for native byte-order, false for non-native */
55620 u8 *a, /* Content to be checksummed */
55621 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
55626 u32 *aData = (u32 *)a;
55627 u32 *aEnd = (u32 *)&a[nByte];
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));
55681 ** This function encodes a single frame header and writes it to a buffer
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);
55721 ** in aData[] is valid. If it is a valid frame, fill *piPage and
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 ){
55743 /* A frame is only valid if the page number is creater than zero.
55750 /* A frame is only valid if a checksum of the WAL header,
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);
55777 ** a part of an ordinary build.
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,
55838 ** Compute a hash on a page number. The resulting hash value must land
55839 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
55840 ** the hash to the next value in the event of a collision.
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
55858 ** number of the first frame indexed by this hash table. If a
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
55930 ** to a rollback or savepoint.
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]);
56027 ** must have exited unexpectedly in the middle of a transaction (after
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;
56057 ** via the hash table. This turns out to be a really, really expensive
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);
56138 /* If the database page size is not a power of two, or is greater than
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])
56175 /* Malloc a buffer to read frames into. */
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);
56268 ** Open a connection to the WAL file zWalName. The database file must
56272 ** A SHARED lock should be held on the database file when this function
56274 ** client from unlinking the WAL or wal-index file. If another process
56279 ** *ppWal is set to point to a new WAL handle. If an error occurs,
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;
56367 ** Return 0 on success. If there are no pages in the WAL with a page
56376 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
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;
56401 ** This function merges two sorted lists into a single sorted list.
56410 ** This routine overwrites aRight[] with a new (probably longer) sequence
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]] );
56548 ** Construct a WalInterator object that can be used to loop over all
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;
56637 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
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);
56666 ** a) the WRITER lock is held,
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 );
56701 ** by active readers. This routine will never overwrite a database page
56702 ** that a concurrent reader might be using.
56704 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
56705 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
56706 ** checkpoints are always run by a background thread or background
56707 ** process, foreground threads will never block on a lengthy fsync call.
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.
56720 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
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,
56777 ** that would never be written by a real client, things are still
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);
56808 /* If the database may grow as a result of this checkpoint, hint
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;
56832 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
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;
56861 /* Reset the return code so as not to report a checkpoint failure
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
56885 ** truncates the log file to zero bytes just prior to a
56889 ** wal-index header in shared memory, as all subsequent reader or
56892 ** as it would leave the system in a state where the contents of
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);
56928 ** Close a connection to a log file.
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 ){
56971 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
56972 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
56974 ** leave a corrupt WAL file on disk. */
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
56996 ** there is a problem.
56998 ** The wal-index is in shared memory. Another thread or process might
57000 ** read it, which might result in inconsistency. A dirty read is detected
57002 ** a checksum on the header.
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] );
57019 /* Read the header. This might happen concurrently with a write to the
57020 ** same area of shared memory on a different CPU in a SMP,
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.
57071 int badHdr; /* True if a header read failed */
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
57091 /* If the first attempt failed, it might have been due to a race
57092 ** with a writer. So get a WRITE lock and try again.
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
57107 ** a WRITE lock, it can only mean that the header is corrupted and
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)
57137 ** Attempt to start a read transaction. This might fail due to a race or
57141 ** On success return SQLITE_OK. On a permanent failure (such an
57143 ** recovery) return a positive error code.
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
57160 ** and is not honoring the locking protocol. There is a vanishingly small
57161 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
57162 ** bad luck when there is lots of contention for the wal-index, but that
57165 ** On success, this routine obtains a read lock on
57166 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
57167 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
57169 ** access any database page that is modified by a WAL frame up to and
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.
57180 ** to select a particular WAL_READ_LOCK() that strives to let the
57187 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
57194 assert( pWal->readLock<0 ); /* Not currently locked */
57196 /* Take steps to avoid spinning forever if there is a protocol error.
57198 ** Circumstances that cause a RETRY should only last for the briefest
57201 ** if we are unlucky, another process that is holding a lock might get
57202 ** paged out or take a page-fault that is time-consuming to resolve,
57207 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
57208 ** is more of a scheduler yield than an actual delay. But on the 10th
57216 VVA_ONLY( pWal->lockError = 1; )
57219 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
57220 sqlite3OsSleep(pWal->pVfs, nDelay);
57226 /* If there is not a recovery running in another thread or process
57228 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
57234 if( pWal->apWiData[0]==0 ){
57236 ** We assume this is a transient condition, so return WAL_RETRY. The
57238 ** modules may return SQLITE_BUSY due to a race condition in the
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)) ){
57272 ** which implies that the database file contains a trustworthy
57273 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
57278 ** is obtained, that is not necessarily true. A checkpointer may
57280 ** it finished. Leaving a corrupt image in the database file.
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
57344 ** that the log file may have been wrapped by a writer, or that frames
57345 ** that occur later in the log than pWal->hdr.mxFrame may have been
57346 ** copied into the database by a checkpointer. If either of these things
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
57357 ** Because a ShmBarrier() call is made between taking the copy of
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
57362 ** cause a problem. The checkpointer could omit to checkpoint
57363 ** a version of page X that lies before pWal->minFrame (call that version
57364 ** A) on the basis that there is a newer version (version B) of the same
57366 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
57367 ** that it can read version A from the database file. However, since
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
57397 ** may be left set to a value larger than expected, if a wal frame
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;
57464 ** Begin a read transaction on the database.
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 ){
57500 /* At this point the client has a lock on an aReadMark[] slot holding
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
57509 ** (2) A checkpoint as been attempted that wrote frames past
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 );
57518 /* It is possible that there is a checkpointer thread running
57522 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
57524 ** there is no checkpointer process by taking a shared CKPT lock
57525 ** before checking pInfo->nBackfillAttempted.
57527 ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing
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));
57564 ** Finish with a read transaction. All this does is release the
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 */
57593 /* This routine is only be called from within a read transaction. */
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 ){
57659 /* If expensive assert() statements are available, do a linear search
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;
57698 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
57699 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
57706 if( pWal && ALWAYS(pWal->readLock>=0) ){
57707 return pWal->hdr.nPage;
57714 ** This function starts a write transaction on the WAL.
57716 ** A read transaction must have already been started by a prior call
57721 ** thread to write as doing so would cause a fork. So this routine
57724 ** There can only be a single writer active at a time.
57729 /* Cannot start a write transaction without first holding a read
57731 assert( pWal->readLock>=0 );
57732 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
57734 if( pWal->readOnly ){
57738 /* Only one writer allowed at a time. Get the write lock. Return
57745 pWal->writeLock = 1;
57751 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
57753 pWal->writeLock = 0;
57761 ** End a write transaction. The commit has already been done. This
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;
57802 ** is passed as the second argument is (a) in the cache and
57803 ** (b) has an outstanding reference, then xUndo is either a no-op
57804 ** (if (a) is false) or simply expels the page from the cache (if (b)
57807 ** If the upper layer is doing a rollback, it is guaranteed that there
57810 ** committed. As a result, the call to xUndo may not fail.
57815 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
57824 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
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;
57838 ** by a call to WalSavepoint().
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];
57866 ** This function is called just before writing a set of frames to the log
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);
57963 ** Write out a single frame of the WAL
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));
57988 ** This function is called as part of committing a transaction within which
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 */
58049 int isCommit, /* True if this is a commit */
58057 int szFrame; /* The size of a single frame */
58064 assert( pWal->writeLock );
58066 /* If this frame set completes a transaction, then nTruncate>0. If
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.
58091 ** this source file for a description of the WAL header format.
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 ){
58189 /* If this is the end of a transaction, then we might need to pad
58192 ** Padding and syncing only occur if this set of frames complete a
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;
58278 ** Obtain a CHECKPOINT lock and then backfill as much information as
58281 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
58282 ** callback. In this case this function runs a blocking checkpoint.
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
58375 ** next time the pager opens a snapshot on this database it knows that
58378 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
58384 pWal->ckptLock = 0;
58389 /* Return the value to pass to a sqlite3_wal_hook callback, the
58397 ret = pWal->iCallback;
58398 pWal->iCallback = 0;
58408 ** into locking_mode=NORMAL. This means that we must acquire a lock
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 );
58474 /* Create a snapshot object. The content of a snapshot is opaque to
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
58514 /* aSalt[0] is a copy of the value stored in the wal file header. It
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;
58526 ** If the argument is not NULL, it points to a Wal object that holds a
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;
58550 ** a legal notice, here is a blessing:
58561 ** a good breakout.
58569 ** a legal notice, here is a blessing:
58576 ** This file implements an external (disk-based) database using BTrees.
58577 ** For a detailed discussion of BTrees, refer to
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
58596 ** Finding a particular key requires reading O(log(M)) pages from the
58599 ** In this implementation, a single file can hold one or more separate
58601 ** key and data for any entry are combined to form the "payload". A
58605 ** and the preceding pointer are combined to form a "Cell". Each
58606 ** page has a small header which contains the Ptr(N) pointer and other
58612 ** the second is page 2, and so forth. A page number of zero indicates
58614 ** Each page can be either a btree page, a freelist page, an overflow
58615 ** page, or a pointer-map page.
58617 ** The first page is always a btree page. The first 100 bytes of the first
58618 ** page contain a special header (the "file header") that describes the file.
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).
58655 ** space in a page that can be consumed by a single cell for standard
58656 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
58660 ** If the payload for a cell is larger than the max payload, then extra
58666 ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
58667 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
58671 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
58674 ** |----------------|
58676 ** |----------------|
58678 ** |----------------|
58682 ** |----------------|
58685 ** |----------------| ^ Grows upwards
58688 ** |----------------|
58707 ** The cell pointer array contains zero or more 2-byte numbers which are
58716 ** Unused space within the cell content area is collected into a linked list of
58719 ** increasing order. Because a freeblock must be at least 4 bytes in size,
58721 ** exist on the freeblock chain. A group of 3 or fewer free bytes is called
58722 ** a fragment. The total number of bytes in all fragments is recorded.
58734 ** Cell content makes use of variable length integers. A variable
58738 ** appears first. A variable-length integer may not be more than 9 bytes long.
58739 ** As a special case, all 8 bytes of the 9th byte are used as data. This
58740 ** allows a 64-bit integer to be encoded in 9 bytes.
58751 ** bytes of key and data in a btree cell.
58753 ** The content of a cell looks like this:
58762 ** Overflow pages form a linked list. Each page except the last is completely
58763 ** filled with data (pagesize - 4 bytes). The last page can have as little
58771 ** file header points to the first in a linked list of trunk page. Each trunk
58772 ** page points to multiple leaf pages. The content of a leaf page is
58773 ** unspecified. A trunk page looks like this:
58783 /* The following value is the maximum cell size assuming a maximum page
58786 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
58788 /* The maximum number of cells on a single page of the database. This
58789 ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
58793 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
58801 ** This is a magic string that appears at the beginning of every
58802 ** SQLite database in order to identify the file as a real database.
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.
58826 ** An instance of this object stores information about each a single database
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 */
58843 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
58845 u8 leaf; /* True if a leaf page */
58856 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
58857 ** non-overflow cell */
58870 ** A linked list of the following structures is stored at BtShared.pLock.
58871 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
58873 ** from this list when a transaction is committed or rolled back, or when
58874 ** a btree handle is closed.
58887 /* A Btree handle
58889 ** A database connection contains a pointer to an instance of
58917 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
58928 ** If the shared-data extension is enabled, there may be multiple users
58929 ** of the Btree structure. At most one of these may open a write transaction,
58937 ** An instance of this object represents a single database file.
58939 ** A single database file can be in use at the same time by two
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.
58974 BtCursor *pCursor; /* A list of all open cursors */
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 */
58990 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
58991 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
58992 u32 pageSize; /* Total number of bytes on a page */
58998 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
58999 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
59002 BtShared *pNext; /* Next on a list of sharable BtShared structs */
59003 BtLock *pLock; /* List of locks held on this shared-btree struct */
59006 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
59018 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
59020 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
59024 ** about a cell. The parseCellPtr() function fills in this structure
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
59037 ** this will be declared corrupt. This value is calculated based on a
59038 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
59039 ** root-node and 3 for all other internal nodes.
59041 ** If a tree that appears to be taller than this is encountered, it is
59047 ** A cursor is a pointer to a particular entry within a particular
59048 ** b-tree within a database file.
59053 ** A single database file can be shared by two more database connections,
59054 ** but cursors cannot be shared. Each cursor is associated with a
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.
59068 BtCursor *pNext; /* Forms a linked list of all cursors */
59070 CellInfo info; /* A parse of the cell we are pointing at */
59085 u8 curIntKey; /* Value of apPage[0]->intKey */
59087 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
59095 #define BTCF_WriteFlag 0x01 /* True if a write cursor */
59106 ** Cursor does not point to a valid entry. This can happen (for example)
59111 ** Cursor points to a valid entry. getPayload() etc. may be called.
59114 ** Cursor is valid except that the Cursor.skipNext field is non-zero
59116 ** operation should be a no-op.
59121 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
59126 ** An unrecoverable error (an I/O error or a malloc failure) has occurred
59127 ** on a different connection that shares the BtShared cache with this
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))
59163 ** The pointer map is a lookup table that identifies the parent page for
59165 ** contains a pointer to the child. Every page in the database contains
59168 ** entry consists of a single byte 'type' and a 4 byte parent page number.
59172 ** position in the file to another as part of autovacuum. When a page
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
59182 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
59184 ** contains the cell with a pointer to this overflow page.
59186 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
59187 ** overflow pages. The page-number identifies the previous
59190 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
59199 /* A bunch of assert() statements to check the transaction state variables
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 */
59239 int mallocFailed; /* A memory allocation error has occurred */
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.
59255 ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
59256 ** two-byte aligned address. get2bytea() is only used for accessing the
59257 ** cell addresses in a btree header.
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;
59308 ** Enter a mutex on the given BTree object.
59311 ** and this routine is a no-op. The underlying mutex is non-recursive.
59312 ** But we keep a reference count in Btree.wantToLock so the behavior
59316 ** by all database connections. The p->pNext is a list of other
59318 ** which need to be locked after p. If we cannot get a lock on
59319 ** p, then first unlock all of the others on p->pNext, then wait
59321 ** subsequent Btrees that desire a lock.
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 );
59338 /* We should already hold a lock on the database connection */
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;
59351 /* This is a helper function for sqlite3BtreeLock(). By moving
59364 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
59365 p->pBt->db = p->db;
59366 p->locked = 1;
59370 /* To avoid deadlock, first release all locks with a larger
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 ){
59393 ** Exit the recursive mutex on a Btree.
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);
59425 ** Enter the mutex on every Btree associated with a database
59427 ** a statement since we will be comparing table and column names
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;
59544 ** Enter a mutex on a Btree given a cursor owned by that Btree.
59551 sqlite3BtreeEnter(pCur->pBtree);
59555 sqlite3BtreeLeave(pCur->pBtree);
59568 ** a legal notice, here is a blessing:
59575 ** This file implements an external (disk-based) database using BTrees.
59577 ** Including a description of file format and an overview of operation.
59599 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
59603 ** from the header of a btree page. If the page size is 65536 and the page
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)
59620 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
59630 ** A list of BtShared objects that are eligible for participation
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.
59670 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
59671 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
59672 #define clearAllSharedCacheTableLocks(a)
59673 #define downgradeAllSharedCacheTableLocks(a)
59674 #define hasSharedCacheTableLock(a,b,c,d) 1
59675 #define hasReadConflicts(a, b) 0
59687 ** For example, when writing to a table with root-page iRoot via
59692 ** When writing to an index that resides in a sharable database, the
59693 ** caller should have first obtained a lock specifying the root page of
59694 ** the corresponding table. This makes things a bit more complicated,
59695 ** as this module treats each table as a separate structure. To determine
59699 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
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
59783 ** have a read cursor.
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)
59805 ** Query to see if Btree handle p may obtain a lock of type eLock
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(...)
59841 ** statement is a simplification of:
59843 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
59846 ** may hold a WRITE_LOCK on any table in this file (since there can
59847 ** only be a single writer).
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
59872 ** (a) The specified Btree object p is connected to a sharable
59875 ** (b) No other Btree objects hold a lock that conflicts
59880 ** is returned if a malloc attempt fails.
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 ){
59910 /* If the above search did not find a BtLock struct associating Btree 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 ){
59975 ** transaction. If there currently exists a writer, and p is not
59980 ** If there is not currently a writer, then BTS_PENDING must
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
60020 ** btreeInitPage() to reference an stale connection pointer that references a
60021 ** a connection that has already closed. This routine is used inside assert()
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){
60051 ** This function is called before modifying the contents of a table
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:
60097 ** 1) When all data is deleted from a page and the page becomes
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
60100 ** such a page is not even journalled (as it will not be modified,
60103 ** 2) When a free-list leaf page is reused, its content is not read
60107 ** By themselves, these optimizations work fine and provide a handy
60109 ** a page is moved to the free-list and then reused within the same
60110 ** transaction, a problem comes up. If the page is not journalled when
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
60113 ** may be lost. In the event of a rollback, it may not be possible
60116 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
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;
60160 ** Release all of the apPage[] pages for a cursor.
60164 for(i=0; i<=pCur->iPage; i++){
60165 releasePage(pCur->apPage[i]);
60166 pCur->apPage[i] = 0;
60168 pCur->iPage = -1;
60172 ** The cursor passed as the only argument must point to a valid entry
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 ){
60191 /* Only the rowid is required for a table btree */
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
60249 ** the location in the btree is remembered in such a way that it can be
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;
60280 ** the cursors if and when a cursor is found that actually requires saving.
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;
60317 ** In this version of BtreeMoveto, pKey is a packed index record
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 ? \
60386 ** Determine whether or not a cursor has moved from the position where
60389 ** from under them, for example. Cursor might also move if a btree
60392 ** Calling this routine with a NULL cursor pointer returns false.
60394 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
60398 return pCur->eState!=CURSOR_VALID;
60402 ** This routine restores a cursor back to its original position after it
60403 ** has been moved by some outside activity (such as a btree rebalance or
60404 ** a row having been deleted out from under the cursor).
60411 ** This routine should only be called for a cursor that just returned
60418 assert( pCur->eState!=CURSOR_VALID );
60424 if( pCur->eState!=CURSOR_VALID ){
60427 assert( pCur->skipNext==0 );
60449 pCur->hints = x;
60455 ** Given a page number of a regular database page, return the page
60456 ** number for the pointer-map page that contains the entry for the
60459 ** Return 0 (not a valid page) for pgno==1 since there is
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 );
60577 ** Given a btree page and a cell index (0 means the first cell on
60578 ** the page, 1 means the second cell, and so forth) return a pointer
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;
60633 ** Parse a cell content block and fill in the CellInfo structure.
60639 ** There is also a wrapper function btreeParseCell() that works for
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 );
60679 ** The code is inlined to avoid a function call.
60693 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
60695 ** The code is inlined to avoid a function call.
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);
60777 ** Compute the total number of bytes that a Cell needs in the cell
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 */
60787 u8 *pEnd; /* End mark for a varint */
60792 ** the (CellInfo.nSize) value found by doing a full parse of the
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);
60835 u8 *pEnd; /* End mark for a varint */
60839 ** the (CellInfo.nSize) value found by doing a full parse of the
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));
60866 ** If the cell pCell, part of page pPage contains a pointer
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 */
60899 int size; /* Size of a cell */
60900 int usableSize; /* Number of usable bytes on a page */
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
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);
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);
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
61021 ** size. If one can be found, return a pointer to the space and remove it
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[]
61093 ** allocation is being made in order to insert a new cell, so we will
61094 ** also end up needing a new cell pointer.
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
61118 ** integer, so a value of 0 is used in its place. */
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);
61131 ** freelist looking for a free slot big enough to satisfy the request.
61139 assert( pSpace>=data && (pSpace - data)<65536 );
61140 *pIdx = (int)(pSpace - data);
61147 /* The request could not be fulfilled using a freelist slot. Check
61152 assert( pPage->nCell>0 || CORRUPT_DB );
61153 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
61166 top -= nByte;
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);
61230 ** iPtr: The address of a pointer to iFreeBlk
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;
61265 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
61274 pPage->nFree += iOrigSize;
61279 ** Decode the flags byte (the first byte of the header) for a page
61283 ** indicates a corrupt database files:
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. */
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;
61340 ** Initialize the auxiliary information for a disk block.
61343 ** not contain a well-formed database page, then return
61344 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
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
61385 ** the start of the cell content area. A zero value for this integer is
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) ){
61392 /* To many cells for a single page. The page must be corrupt */
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
61397 ** possible for a root page of a table that contains no rows) then the
61400 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
61402 /* A malformed database page might cause us to read past the end
61403 ** of page when parsing a cell.
61405 ** The following block of code checks early to see if a cell extends
61406 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
61409 iCellFirst = cellOffset + 2*pPage->nCell;
61410 iCellLast = usableSize - 4;
61411 if( pBt->db->flags & SQLITE_CellSizeCk ){
61413 int sz; /* Size of a cell */
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;
61483 ** Set up a raw page so that it looks like a database page holding
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;
61520 ** Convert a DbPage obtained from the pager into a MemPage used by
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) );
61537 ** Get a page from the pager. Initialize the MemPage.pBt and
61557 assert( sqlite3_mutex_held(pBt->mutex) );
61558 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
61565 ** Retrieve a page from the pager cache. If the requested page is not
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);
61593 ** Get a page from the pager and initialize it.
61595 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
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) );
61639 /* If obtaining a child page for a cursor, we must verify that the page is
61641 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
61649 if( pCur ) pCur->iPage--;
61656 ** Release a MemPage. This should be called once for each prior
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;
61703 ** During a rollback, when the pager reloads information into the cache
61714 if( pPage->isInit ){
61715 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
61716 pPage->isInit = 0;
61718 /* pPage might not be a btree page; it might be an overflow page
61719 ** or ptrmap page or a free page. In those cases, the following
61723 ** the call for every page that comes in for re-initing. */
61730 ** Invoke the busy handler for a btree.
61734 assert( pBt->db );
61735 assert( sqlite3_mutex_held(pBt->db->mutex) );
61736 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
61740 ** Open a database file.
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
61751 ** The "flags" parameter is a bitmask that might contain bits like
61761 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
61770 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
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) );
61794 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
61797 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
61810 p->inTrans = TRANS_NONE;
61811 p->db = db;
61813 p->lock.pBtree = p;
61814 p->lock.iTable = 1;
61819 ** If this Btree is a candidate for shared cache, try to find an
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;
61890 ** when compiling on a different architecture.
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
61936 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
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);
61984 /* If the new Btree uses a sharable pBtShared, then link the new
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
62098 ** inserting them into a leaf page (function fillInCell()). If
62099 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
62103 ** it into a database page. This is not actually a problem, but it
62104 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
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);
62206 ** If the number of pages exceeds this limit during a write transaction,
62212 ** using mxPage of 0 is a way to query the current spill size.
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);
62243 ** there is a high probability of damage) Level 2 is the default. There
62244 ** is a very low but non-zero probability of damage. Level 3 reduces the
62245 ** probability of damage to near zero but with a write performance reduction.
62252 BtShared *pBt = p->pBt;
62253 assert( sqlite3_mutex_held(p->db->mutex) );
62255 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
62266 ** The page size must be a power of 2 between 512 and 65536. If the page
62270 ** Page sizes are constrained to be a power of two so that the region
62273 ** at the beginning of a page.
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;
62358 ** Set the maximum page count for a database if mxPage is positive.
62365 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
62376 ** newFlag==(-1) No changes
62378 ** This routine acts as a query if newFlag is less than zero
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));
62476 ** Get a reference to pPage1 of the database file. This will
62477 ** also acquire a readlock on that file.
62479 ** SQLITE_OK is returned on success. If the file is not a
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);
62499 ** a valid database file.
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 ){
62590 /* After reading the first page of the database assuming a page size
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);
62623 ** a cell. Make sure it is small enough so that at least minFanout
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;
62664 ** For the purposes of this routine, a cursor is any cursor that
62671 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
62672 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
62673 && pCur->eState!=CURSOR_FAULT ) r++;
62681 ** of a transaction but there is a read lock on the database, then
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;
62701 ** into a new empty database by initializing the first page of
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;
62744 ** Initialize the first page of the database file (creating a database
62745 ** consisting of a single page and no schema objects). Return SQLITE_OK
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-
62762 ** to access the database. A preexisting transaction may not be
62763 ** upgraded to exclusive by calling this routine a second time - the
62764 ** exclusivity flag only works for a new transaction.
62766 ** A write-transaction must be started before attempting any
62768 ** will work unless a transaction is started first:
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.
62784 ** Suppose there are two processes A and B. A has a read lock and B has
62785 ** a reserved lock. B tries to promote to exclusive but is blocked because
62786 ** of A's read lock. A tries to promote to reserved but is blocked by B.
62789 ** when A already has a read lock, we encourage A to give up and let B
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 ){
62817 /* If another database handle has already opened a write transaction
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]);
62971 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
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 */
63053 MemPage *pPtrPage; /* The page that contains a pointer to 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
63076 ** If pDbPage is an overflow page, then the first 4 bytes may store a
63077 ** pointer to a subsequent overflow page. If this is the case, then
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.
63254 ** It performs a single unit of work towards an incremental vacuum.
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);
63294 ** This routine is called prior to sqlite3PagerCommit when a transaction
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 ){
63318 /* It is not possible to create a database for which the final page
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
63357 ** causes a rollback journal to be created (if it does not already exist)
63358 ** and populated with enough information so that if a power loss occurs
63368 ** This call is a no-op if no write-transaction is currently active on pBt.
63371 ** the name of a master journal file that should be written into the
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);
63406 ** at the conclusion of a transaction.
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
63458 ** the upper layer will attempt a rollback. However, if the second argument
63459 ** is non-zero then this b-tree transaction is part of a multi-file
63461 ** (by deleting a master journal file) and the caller will ignore this
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;
63500 ** Do both phases of a commit.
63519 ** Every cursor is a candidate to be tripped, including cursors
63523 ** This routine gets called when a rollback occurs. If the writeOnly
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.
63537 ** saving a cursor position, an SQLite error code.
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;
63577 ** a tripped cursor will result in an error.
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;
63634 ** Start a statement subtransaction. The subtransaction can be rolled
63635 ** back independently of the main transaction. You must start a transaction
63636 ** before starting a subtransaction. The subtransaction is ended automatically
63640 ** that are contained within a BEGIN...COMMIT block. If a constraint
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 );
63660 /* At the pager level, a statement transaction is a savepoint with
63665 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
63677 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
63679 ** from a normal transaction rollback, as no locks are released and 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 );
63713 ** Create a new cursor for the BTree whose root is on the page
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)
63738 ** is set. If FORDELETE is set, that is a hint to the implementation that
63740 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
63741 ** this implementation. But in a hypothetical alternative storage engine
63743 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
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 */
63770 /* The following assert statements verify that if this is a sharable
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 */
63833 ** Return the size of a BtCursor object in bytes.
63836 ** sufficient storage to hold a cursor. The BtCursor object is opaque
63837 ** to users so they cannot do the sizeof() themselves - they must call
63845 ** Initialize memory that will be converted into a BtCursor object.
63849 ** do not need to be zeroed and they are large, so we can save a lot
63850 ** of run-time by skipping the initialization of those elements.
63857 ** Close a cursor. The read lock on the database file is released
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);
63892 ** Make sure the BtCursor* given in the argument has a valid
63896 ** BtCursor.info is a cache of the information in the current cell.
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);
63922 ** Return true if the given BtCursor is valid. A valid cursor is one
63923 ** that is currently pointing to a row in a (non-empty) table.
63924 ** This is a verification routine is used only within assert() statements.
63927 return pCur && pCur->eState==CURSOR_VALID;
63932 return pCur->eState==CURSOR_VALID;
63936 ** Return the value of the integer key or "rowid" for a table btree.
63937 ** This routine is only valid for a cursor that is pointing into a
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.
63977 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
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);
64042 ** Copy data from a buffer to a page, or from a page to a buffer.
64044 ** pPayload is a pointer to data stored on database page pDbPage.
64056 int eOp, /* 0 -> copy from page, 1 -> copy to page */
64060 /* Copy data from buffer to page (a write operation) */
64067 /* Copy data from page to buffer (a read operation) */
64078 ** 0: The operation is a read. Populate the overflow cache.
64079 ** 1: The operation is a write. Populate the overflow cache.
64081 ** A total of "amt" bytes are read or written beginning at "offset".
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.
64099 ** * A commit in auto_vacuum="full" mode,
64100 ** * Creating a table (may require moving an overflow page).
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 ){
64140 int a = amt;
64141 if( a+offset>pCur->info.nLocal ){
64142 a = pCur->info.nLocal - offset;
64144 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
64146 pBuf += a;
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]);
64163 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
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;
64223 int a = amt;
64224 if( a + offset > ovflSize ){
64225 a = ovflSize - offset;
64231 ** 1) this is a read operation, and
64233 ** 3) there is no open write-transaction, and
64234 ** 4) the database is file-backed, and
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,
64267 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
64272 amt -= a;
64274 pBuf += a;
64283 return SQLITE_CORRUPT_PGNO(pPage->pgno);
64293 ** pCur can be pointing to either a table or an index b-tree.
64294 ** If pointing to a table btree, then the content section is read. If
64295 ** pCur is pointing to an index b-tree then the key section is read.
64298 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
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 ){
64344 ** Return a pointer to payload information from the entry that the
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
64349 ** returned will not be a valid pointer.
64354 ** key and data without making a copy. If the key and/or data spills
64356 ** the key/data and copy it into a preallocated buffer.
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.
64390 ** Hence, a mutex on the BtShared should be held prior to calling
64402 ** Move the cursor down to a new child page. The newPgno argument is the
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
64438 ** in a corrupt database */
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.
64478 ** If the table has a virtual root page, then the cursor is moved to point
64479 ** to the virtual root page instead of the actual root page. A table has a
64480 ** virtual root page when the actual root page contains no cells and a
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
64491 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
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;
64692 ** specified by pIdxKey or intKey. Return a success code.
64699 ** left pointing at a leaf page which would hold the entry if it
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;
64752 ** try to get there using sqlite3BtreeNext() rather than a full
64754 ** is still obtained without this case, only a little more slowely */
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
64808 ** a moveToChild() or moveToRoot() call would have detected corruption. */
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 );
64880 ** this case the whole cell needs to be parsed, a buffer allocated
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 );
64965 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
64974 return (CURSOR_VALID!=pCur->eState);
64979 ** pointing to. Return a negative number if no estimate is currently
64987 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
64991 ** will always point to a leaf node. */
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;
65012 ** routine is called when it is necessary to move to a different page or
65017 ** skipped if the SQL index had been a unique index. The F argument
65018 ** is a hint to the implement. SQLite btree implementation does not use
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 );
65053 ** to be invalid here. This can only occur if a second cursor modifies
65054 ** the page while cursor pCur is holding a reference to it. Which can
65055 ** only happen if the database is corrupt in such a way as to link the
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 ){
65117 ** helper routine is called when it is necessary to move to a different page
65122 ** skipped if the SQL index had been a unique index. The F argument is a
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--;
65199 ** Allocate a new page from the database file.
65210 ** locate a page close to the page number "nearby". This can be used in an
65215 ** anywhere on the free-list, then it is guaranteed to be returned. If
65224 Pgno nearby, /* Search for a page near this one */
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)
65283 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
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));
65395 /* Extract a leaf from the trunk */
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);
65462 /* There are no pages on the freelist, so append a new page to the
65466 ** pager layer with the 'no-content' flag set. This prevents the pager
65468 ** current transaction has already run one or more incremental-vacuum
65470 ** that is required in the event of a rollback. In this case, do
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.
65535 ** If the caller happens to have a pointer to the MemPage object
65539 ** If a pointer to a MemPage object is passed as the second argument,
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 ){
65612 ** being freed as a new leaf.
65615 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
65616 ** coded. But due to a coding error in versions of SQLite prior to
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-- ){
65712 /* 0 is not a legal page number and page 1 cannot be an
65723 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
65726 ** to an overflow page belonging to a cell that is being deleted/updated.
65730 ** freePage2() may zero the page contents if secure-delete mode is
65731 ** enabled. If this 'overflow' page happens to be a page that the
65741 sqlite3PagerUnref(pOvfl->pDbPage);
65750 ** Create the byte sequence used to represent a cell on page pPage
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;
65832 ** Use a call to btreeParseCellPtr() to verify that the values above
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
65865 ** If this is the first overflow page, then write a partial entry
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;
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;
65980 ** Insert a new cell on pPage at cell index "i". pCell points to the
65984 ** will not fit, then make a copy of the cell content into pTemp if
65985 ** pTemp is not null. Regardless of pTemp, allocate a new entry
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) );
66014 /* The cell should normally be sized correctly. However, when moving a
66015 ** malformed cell from a leaf page to an interior page, if the cell size
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
66030 ** as a contingency. In other words, never need more than 3 overflow
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 ){
66073 /* The cell may contain a pointer to an overflow page. If so, write
66083 ** A CellArray object contains a cache of pointers and sizes for a
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
66135 ** function works around problems caused by this by making a copy of any
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);
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
66234 ** database. But they might for a corrupt database. Hence use memmove()
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);
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]);
66409 ** in a balancing operation. NN is the number of neighbors on either side
66415 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
66416 ** in exchange for a larger degradation in INSERT and UPDATE performance.
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.
66438 ** pParent is its parent. pPage must have a single overflow entry
66439 ** which is also the right-most entry on the page.
66441 ** The pSpace buffer is used to store a temporary copy of the divider
66442 ** cell that will be inserted into pParent. Such a cell consists of a 4
66443 ** byte page number followed by a variable length integer. In other
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
66486 ** be marked as dirty. Returning an error code will cause a
66490 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
66491 if( szCell>pNew->minLocal ){
66496 /* Create a divider cell to insert into pParent. The divider cell
66497 ** consists of a 4-byte page number (the page number of pPage) and
66498 ** a variable length key value (which must be the same value as the
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)
66504 ** and the key value (a variable length integer, may have any value).
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
66576 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
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. */
66608 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
66609 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
66613 ** fairly obscure circumstances, even though it is a copy of initialized
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.
66635 ** same amount of free space. Usually a single sibling on either side of the
66639 ** is a root page or a child of a root page) then all available siblings
66657 ** in a corrupted state. So if this routine fails, the database should
66660 ** The third argument to this function, aOvflSpace, is a pointer to a
66664 ** a maximum of four divider cells into the parent page, and the maximum
66665 ** size of a cell stored within an internal node is always less than 1/4
66666 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
66669 ** If aOvflSpace is set to a null pointer, this function returns
66675 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
66676 int isRoot, /* True if pParent is a root-page */
66677 int bBulk /* True if this call is part of a bulk load */
66684 int nxDiv; /* Next divider slot in pParent->aCell[] */
66686 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
66687 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
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 */
66701 Pgno pgno; /* Temp var to store a page number in */
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]);
66784 ** This is safe because dropping a cell only overwrites the first
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 );
66848 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
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] ){
66877 ** cells into a parent on a prior balance, and divider cells are always
66884 ** long be able to find the cells if a pointer to each cell is not saved
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);
66932 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
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.
67018 ** get a better balance.
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
67058 ** (2) pPage is a virtual root page. A virtual root page is when
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);
67102 ** This helps to keep entries in the disk file in order so that a scan
67103 ** of the table is closer to a linear scan through the file. That in turn
67107 ** than (NB+2) (a small constant), that should not be a problem.
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);
67174 ** a) if the cell contains a reference to an overflow chain, the
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;
67209 ** pCell really was a part of sibling page iOld (not a divider or
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
67256 ** previously stored on a leaf node, and its reported size was 4
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 ){
67393 ** debugging. This is usually disabled because a corrupt database may
67417 ** This function is called when the root page of a b-tree structure is
67420 ** A new child page is allocated and the contents of the current root
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
67430 ** If successful, *ppChild is set to contain a reference to the child
67437 MemPage *pChild = 0; /* Pointer to a new child page */
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
67539 /* Call balance_quick() to create a new sibling of pPage on which
67540 ** to store the overflow cell. balance_quick() inserts a new cell
67542 ** happens, the next iteration of the do-loop will balance pParent
67547 ** The purpose of the following assert() is to check that only a
67549 ** function. If this were not verified, a subtle bug involving reuse
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
67572 ** copied either into the body of a database page or into the new
67575 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
67577 pCur->hints&BTREE_BULKLOAD);
67580 ** by a previous call to balance_nonroot(). Its contents are
67593 pPage->nOverflow = 0;
67595 /* The next iteration of the do-loop balances the parent page. */
67597 pCur->iPage--;
67598 assert( pCur->iPage>=0 );
67610 ** Insert a new record into the BTree. The content of the new record
67613 ** pointing at a random location.
67615 ** For a table btree (used for rowid tables), only the pX.nKey value of
67624 ** If the seekResult parameter is non-zero, then a successful call to
67627 ** is currently pointing to a cell that will be adjacent to the cell
67628 ** to be inserted. If seekResult<0 then pCur points to a cell that is
67629 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
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
67671 ** intkey table, the caller should be inserting integer keys with a
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);
67698 ** to a row with the same key as the new entry being inserted. */
67700 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
67702 /* If the cursor is currently on the last row and we are appending a
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 );
67798 ** There is a subtle but important optimization here too. When inserting
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
67805 ** row without seeking the cursor. This can be a big performance boost.
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 );
67845 ** But if that bit is set, then the cursor is left in a state such that
67850 ** associated with a single table entry and its indexes. Only one of those
67852 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
67853 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
67854 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
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
67890 ** Or, if the current delete will not cause a rebalance, then 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.
67906 /* If the page containing the entry to delete is not a leaf page, move
67910 ** of the 'next' entry, as the previous entry is always a part of the
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);
67941 /* If the cell deleted was not located on a leaf page, then the cursor
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);
67966 /* Balance the tree. If the entry deleted was located on a leaf page,
67972 ** pCur is pointing to the leaf page from which a cell was removed to
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;
68012 ** Create a new BTree table. Write into *piTable the page
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 */
68043 /* Creating a new table may probably require moving an existing database
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
68067 /* Allocate a page. The page that currently resides at pgnoRoot will
68077 /* pgnoRoot is the page that will be used for the root-page of
68087 ** case they are holding a reference to an xFetch reference
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;
68232 ** Delete all information from a single table in the database. iTable is
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);
68273 ** Erase all information in a table and add the root of the table to
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
68385 ** has a read or write transaction open on the database.
68387 ** Read the meta-information out of a database file. Meta[0]
68390 ** is read-only, the others are read/write.
68396 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
68399 ** database file. It is a number computed by the pager. But its access
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;
68492 /* pPage is a leaf node. This loop navigates the cursor so that it
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]));
68533 ** Return the pager associated with a BTree. This routine is used for
68537 return p->pBt->pPager;
68542 ** Append a message to the error message string.
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;
68647 int isFreeList, /* True for a freelist. False for overflow page list */
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.
68723 ** of its daughter nodes. A consequence of the heap property is that the
68726 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
68733 ** entry represents the span of a cell or freeblock on a btree page.
68734 ** The upper 16 bits are the index of the first byte of a range and the
68753 aHeap[0]--;
68768 ** Do various sanity checks on a single page of a tree. Return
68790 int depth = -1, d2; /* Depth of a subtree */
68803 u32 pc; /* Address of a cell */
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: ";
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
68994 ** overlap, that means bytes are used multiple times. If there is a gap,
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;
69034 ** This routine does a complete check of the given BTree file. aRoot[] is
69036 ** a table. nRoot is the number of entries in aRoot.
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
69044 ** returned. If a memory allocation error occurs, NULL is returned.
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));
69193 ** Run a checkpoint on the Btree passed as the first argument.
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;
69232 ** This function returns a pointer to a blob of memory associated with
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
69239 ** call the nBytes parameter is ignored and a pointer to the same blob
69243 ** allocated, a null pointer is returned. If the blob has already been
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) );
69280 ** Obtain a lock on the table whose root page is iTab. The
69281 ** lock is a write lock if isWritelock is true or a read lock
69286 assert( p->inTrans!=TRANS_NONE );
69287 if( p->sharable ){
69305 ** Argument pCsr must be a cursor opened for writing on an
69306 ** INTKEY table currently pointing at a valid table entry.
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.
69333 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
69334 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
69337 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
69341 ** (a) the cursor is open for writing,
69342 ** (b) there is a read/write transaction open,
69343 ** (c) the connection holds a write-lock on the table (if required),
69344 ** (d) there are no conflicting read-locks, and
69345 ** (e) the cursor points at a valid row of an intKey table.
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;
69405 ** Return true if the cursor has a hint specified. This routine is
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;
69449 ** a legal notice, here is a blessing:
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 */
69490 ** Once it has been created using backup_init(), a single sqlite3_backup
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
69507 ** the values returned may be invalid. There is no way for a call to
69513 ** Non-sharable Btrees (in-memory databases for example), do not have
69518 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
69519 ** in connection handle pDb. If such a database cannot be found, return
69520 ** a NULL pointer and write an error message to pErrorDb.
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
69580 ** a pointer to the new sqlite3_backup object.
69605 ** database connection while a backup is in progress may cause
69606 ** a malfunction or a deadlock.
69608 sqlite3_mutex_enter(pSrcDb->mutex);
69609 sqlite3_mutex_enter(pDestDb->mutex);
69617 /* Allocate space for a new sqlite3_backup object...
69618 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
69619 ** call to sqlite3_backup_init() and is destroyed by a call to
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
69640 ** error was hit. Or there is a transaction open on the destination
69649 p->pSrc->nBackup++;
69652 sqlite3_mutex_leave(pDestDb->mutex);
69653 sqlite3_mutex_leave(pSrcDb->mutex);
69659 ** considered fatal if encountered during a backup operation. All errors
69667 ** Parameter zSrcData points to a buffer containing the data for
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
69707 ** and a codec is in use.
69714 ** between source and destination. If there is a difference, try to
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
69825 ** one now. If a transaction is opened here, then it will be closed
69828 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
69829 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
69837 ** not possible to create a database file that uses one page size by
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)
69962 ** the database file in any way, knowing that if a power failure
69967 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
70011 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
70018 /* If bCloseTrans is true, then this function opened a read transaction
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;
70072 /* If a transaction is still open on the Btree, roll it back. */
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
70086 ** call to sqlite3_backup_init() and is destroyed by a call to
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 ){
70144 ** has been modified by a transaction on the source pager. Copy
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;
70184 ** Copy the complete content of pBtFrom into pBtTo. A transaction
70200 if( pFd->pMethods ){
70213 b.pSrcDb = pFrom->db;
70222 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
70225 ** within a single call (unless an error occurs). The assert() statement
70226 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
70233 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
70252 ** a legal notice, here is a blessing:
70260 ** This file contains code use to manipulate "Mem" structure. A "Mem"
70261 ** stores a single value in the VDBE. Mem is an opaque structure visible
70262 ** only within the VDBE. Interface routines refer to a Mem using the
70270 ** Check invariants on a Mem object.
70279 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
70284 ** That saves a few cycles in inner loops. */
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
70295 /* If MEM_Null is set, then either the value is a pure NULL (the usual
70296 ** case) or it is a pointer set using sqlite3_bind_pointer() or
70297 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
70300 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
70301 /* This is a pointer type. There may be a flag to indicate what to
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
70311 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
70316 assert( (p->flags & MEM_Cleared)==0 );
70320 assert( p->szMalloc==0
70321 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
70323 /* If p holds a string or blob, the Mem.z must point to exactly
70329 ** (4) A static string or blob
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
70345 ** If pMem is an object with a valid string representation, this routine
70349 ** If pMem is not a string object, or the encoding of the string
70351 ** routine is a no-op.
70354 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
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 );
70398 ** contain a valid string or blob value. */
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 ){
70545 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
70546 ** is a no-op.
70551 ** A MEM_Null value will never be passed to this function. This function is
70554 ** keys are strings. In the former case a NULL pointer is returned the
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);
70626 ** If the memory cell contains a value that must be freed by
70630 ** This is a helper routine for sqlite3VdbeMemSetNull() and
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.
70660 ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
70668 if( p->szMalloc ){
70669 sqlite3DbFreeNN(p->db, p->zMalloc);
70670 p->szMalloc = 0;
70672 p->z = 0;
70679 ** Use this routine prior to clean up prior to abandoning a Mem, or to
70680 ** reset a Mem back to its minimum memory utilization.
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.
70727 ** If pMem is a string or blob, then we make an attempt to convert
70729 ** an SQL-NULL value, return 0.
70731 ** If pMem represents a string value, its encoding might be changed.
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 );
70756 ** Return the best representation of pMem that we can get into a
70757 ** double. If pMem is already a double or an integer, return its
70758 ** value. If it is a string or blob, try to convert it to a double.
70759 ** If it is a NULL, return 0.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) ){
70783 ** The MEM structure is already a MEM_Real. Try to also make it a
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);
70842 ** is a string that does not look completely like a number. Convert
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);
70864 ** "aff". Casting is different from applying affinity in that a cast
70870 if( pMem->flags & MEM_Null ) return;
70872 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
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);
70907 ** Initialize bulk memory to be a consistent Mem object.
70913 pMem->flags = flags;
70914 pMem->db = db;
70915 pMem->szMalloc = 0;
70927 ** Use this routine to reset the Mem prior to insert a new value.
70935 pMem->flags = MEM_Null;
70943 ** Delete any previous value and set the value to be a BLOB of length
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;
70958 ** to a value change. So invoke the destructor, then set the value to
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 */
70984 ** Set the value stored in *pMem should already be a NULL.
70985 ** Also store a pointer to go with it.
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;
71038 ** Return true if the Mem object contains a TEXT or BLOB that is
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];
71055 ** This routine prepares a memory cell for modification by breaking
71056 ** its link to a shallow copy and by marking any current shallow
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;
71100 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
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;
71122 ** freed. If pFrom contains ephemeral data, a copy is made.
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;
71138 ** Change the value of a Mem to be a string or a BLOB.
71142 ** string is copied into a (possibly existing) buffer managed by the
71148 ** stored without allocating memory, then it is. If a memory allocation
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 );
71166 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
71172 if( pMem->db ){
71173 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
71190 ** also sets a flag in local variable "flags" to indicate the memory
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) ){
71237 ** Move data out of a btree key or data field and into a Mem structure.
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;
71303 ** The pVal argument is known to be a value other than NULL.
71304 ** Convert it into a string with encoding enc and return a pointer
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;
71340 ** external API. It works in a similar way to sqlite3_value_text(),
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 ){
71364 ** Create a new sqlite3_value object.
71369 p->flags = MEM_Null;
71370 p->db = db;
71387 ** Allocate and return a pointer to a new sqlite3_value object. If
71391 ** Otherwise, if the second argument is non-zero, then this function is
71394 ** that function will return to its caller here. Then return a pointer to
71395 ** an sqlite3_value within the UnpackedRecord.a[] array.
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];
71440 ** to be a scalar SQL function. If
71447 ** error occurs, output parameter (*ppVal) is set to point to a value
71451 ** If the result is a text value, the sqlite3_value object uses encoding
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;
71540 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
71544 ** Extract a value from the supplied expression in the manner described
71569 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
71570 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
71573 ** on a table column definition, and hence only when pCtx==0. This
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);
71589 /* Handle negative integers in a single step. This is needed in the
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;
71677 ** Create a new sqlite3_value object, containing the value of pExpr.
71680 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
71681 ** be converted directly into a value, then the value is allocated and
71682 ** a pointer written to *ppVal. The caller is responsible for deallocating
71684 ** cannot be converted to a value, then *ppVal is set to NULL.
71699 ** a single argument of any type. The return value is a formatted database
71700 ** record (a blob) containing the argument value.
71737 ** Register built-in functions used to help read ANALYZE data.
71747 ** Attempt to extract a value from pExpr and use it to construct *ppVal.
71753 ** A value is extracted in the following cases:
71757 ** * The expression is a bound variable, and this is a reprepare, or
71759 ** * The expression is a literal value.
71766 Expr *pExpr, /* The expression to extract a value from */
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 );
71810 ** A single call to this function populates zero or more fields of the
71812 ** right starting with 0). A single field is populated if:
71816 ** * The expression is a bound variable, and this is a reprepare, or
71818 ** * The sqlite3ValueFromExpr() function is able to extract a value
71819 ** from the expression (i.e. the expression is a literal value).
71821 ** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
71832 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
71836 ** error if a value cannot be extracted from pExpr. If an error does
71843 Expr *pExpr, /* The expression to extract a value from */
71851 if( pExpr==0 || pExpr->op!=TK_SELECT ){
71862 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
71875 ** Attempt to extract a value from expression pExpr using the methods
71878 ** If successful, set *ppVal to point to a new value object and return
71886 Expr *pExpr, /* The expression to extract a value from */
71894 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
71895 ** the column value into *ppVal. If *ppVal is initially NULL then a new
71908 u32 t; /* a column type code */
71914 u8 *a = (u8*)pRec; /* Typecast byte array */
71918 iHdr = getVarint32(a, nHdr);
71922 iHdr += getVarint32(&a[iHdr], t);
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);
71986 ** The valueBytes() routine is a helper function.
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;
72014 ** a legal notice, here is a blessing:
72022 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
72028 ** Create a new virtual database engine.
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);
72064 ** Remember the SQL string for a prepared statement.
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)));
72129 /* Ensure that the size of a VDBE does not grow too large */
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;
72147 /* This routine is just a convenient place to set a breakpoint that will
72158 ** Add a new instruction to the list of instructions current in the
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;
72251 ** "s" character in zTypes[], the register is a string if the argument is
72252 ** not NULL, or OP_Null if the value is a null pointer. For each "i" character
72279 ** Add an opcode that includes the p4 value as a pointer.
72296 ** Add an opcode that includes the p4 value with a P4_INT64 or
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;
72363 ** Create a new symbolic label for an instruction that has yet to be
72364 ** coded. The symbolic label is really just a negative number. The
72366 ** the label is resolved to a specific address, the VDBE will scan
72370 ** The VDBE knows that a P2 value is a label because labels are
72371 ** always negative and P2 values are suppose to be non-negative.
72372 ** Hence, a negative P2 value is a label that has yet to be resolved.
72374 ** Zero is returned if a malloc() fails.
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;
72393 ** a prior call to sqlite3VdbeMakeLabel().
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);
72540 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
72545 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
72548 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
72554 ** (1) For each jump instruction with a negative P2 value (a label)
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];
72585 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
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;
72665 ** fail due to a OOM fault and hence that the return value from
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 );
72691 ** This function returns a pointer to the array of opcodes associated with
72702 VdbeOp *aOp = p->aOp;
72703 assert( aOp && !p->db->mallocFailed );
72706 assert( DbMaskAllZero(p->btreeMask) );
72709 *pnOp = p->nOp;
72710 p->aOp = 0;
72715 ** Add a whole list of operations to the operation stack. Return a
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;
72795 ** for a specific instruction.
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 ){
72836 ** Delete a P4 value if necessary.
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;
72933 ** If the last opcode is "op" and it is not a jump destination,
72937 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
72938 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
72945 ** Change the value of the P4 operand for a specific instruction.
72946 ** This routine is useful when a large program is loaded from a
72947 ** static array using sqlite3VdbeAddOpList but we want to make a
72950 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
72952 ** A value of n==0 means copy bytes of zP4 up to and including the
72956 ** to a string or structure that is guaranteed to exist for the lifetime of
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 ){
73003 ** that was cast to a (const char *). */
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
73057 ** in a production build.
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
73100 ** If a memory allocation error has occurred prior to the calling of this
73101 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
73102 ** is readable but not writable, though it is cast to a writable value.
73103 ** The return of a dummy opcode allows the call to continue functioning
73105 ** this routine is a valid pointer. But because the dummy.opcode is 0,
73111 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
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;
73139 ** Compute a string for the "comment" field of a VDBE opcode listing.
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);
73300 ** Compute a string that describes the P4 parameter for an opcode.
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.
73423 ** attached databases that will be use. A mask of these databases
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
73456 ** be a problem.
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) ){
73477 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
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 */
73501 ** Print a single opcode. This routine is used for debugging only.
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);
73563 ** callgrind, this causes a certain test case to hit the CPU 4.7
73564 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
73565 ** sqlite3MemRelease() were called from here. With -O2, this jumps
73566 ** to 6.6 percent. The test case is inserting 1000 rows into a table
73567 ** with no indexes using a single prepared INSERT statement, bind()
73568 ** and reset(). Inserts are grouped into a transaction.
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;
73587 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
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);
73604 ** Give a listing of the program in the virtual machine.
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 ){
73642 /* This happens if a malloc() inside a call to sqlite3_column_text() or
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 ){
73664 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
73665 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
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;
73714 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
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;
73796 ** Print the SQL that was used to generate a VDBE program.
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]!=' ' ){
73844 ** by subcomponents of a prepared statement. Space is allocated out
73845 ** of a ReusableSpace object by the allocSpace() routine below.
73853 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
73854 ** from the ReusableSpace object. Return a pointer to the allocated
73860 ** been allocated by a prior call to this routine, so just return a copy
73869 void *pBuf, /* Pointer to a prior allocation */
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;
73926 ** Prepare a virtual machine for execution for the first time after
73940 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
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;
73967 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
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;
73997 ** pass will fill in the remainder using a fresh memory allocation.
73999 ** This two-pass approach that reuses as much memory as possible from
74001 ** reduce the amount of memory held by a prepared statement.
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));
74039 ** Close a VDBE cursor and release all the resources that cursor
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 );
74151 ** Clean up the VM after a single run.
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);
74194 ** zName must be a pointer to a nul terminated string.
74196 ** This call must be made after a call to sqlite3VdbeSetNumCols().
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 );
74225 ** A read or write transaction may or may not be active on database handle
74226 ** db. If a transaction is active, commit it. If there is a
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 */
74247 ** be done before determining whether a master journal file is
74253 /* This loop determines (a) if the commit hook should be invoked and
74256 ** one database file has an open write transaction, a master journal
74259 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74260 Btree *pBt = db->aDb[i].pBt;
74262 /* Whether or not a database might need a master journal depends upon
74264 ** journal modes use a master journal and which do not */
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
74300 ** TEMP database) has a transaction active. There is no need for the
74301 ** master-journal.
74303 ** If the return value of sqlite3BtreeGetFilename() is a zero length
74304 ** string, it means the main database is :memory: or a temp file. In
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;
74320 ** IO error while deleting or truncating a journal file. It is unlikely,
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.
74335 ** This requires a master journal file to ensure the transaction is
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);
74349 /* Select a master journal file name */
74351 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
74366 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
74370 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
74390 ** back independently if a failure occurs.
74392 for(i=0; i<db->nDb; i++){
74393 Btree *pBt = db->aDb[i].pBt;
74428 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
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 ){
74579 ** This function is called when a transaction opened by the database
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;
74604 ** This routine is called the when a VDBE tries to halt. If the VDBE
74606 ** changes. If a rollback is needed, then do the rollback.
74608 ** This routine is the only way to move the state of a VM from
74610 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
74618 sqlite3 *db = p->db;
74620 /* This function contains the logic that determines if a statement or
74621 ** transaction will be committed or rolled back as a result of the
74636 if( p->magic!=VDBE_MAGIC_RUN ){
74639 if( db->mallocFailed ){
74640 p->rc = SQLITE_NOMEM_BKPT;
74646 ** SQL statement does not read or write a database file. */
74647 if( p->pc>=0 && p->bIsReader ){
74648 int mrc; /* Primary error code from p->rc */
74650 int isSpecialError; /* Set to true if a 'special' error */
74656 mrc = p->rc & 0xff;
74660 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
74661 ** no rollback is necessary. Otherwise, at least a savepoint
74662 ** transaction must be rolled back to restore the database to a
74665 ** Even if the statement is read-only, it is important to perform
74666 ** a statement or transaction rollback operation. If the error
74667 ** occurred while writing to the journal, sub-journal or database
74670 ** the pager to a consistent state.
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
74693 ** VM, then we do either a commit or rollback of the current transaction.
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
74713 ** key constraints to hold up the transaction. This means a commit
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);
74868 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
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;
74917 FILE *out = fopen("vdbe_profile.out", "a");
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;
74954 ** Clean up and delete a VDBE after execution. Return an integer which is
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;
75064 ** The cursor "p" has a pending seek operation that has not yet been
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;
75092 ** cursor, set the cursor to point to a NULL row.
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) ){
75122 ** If a MoveTo operation is pending on the given cursor, then do that
75125 ** a NULL row.
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) ){
75159 ** data and index records. Each serialized value consists of a
75160 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
75161 ** integer, stored as a varint.
75164 ** the blob of data that it corresponds to. In a table record, all serial
75167 ** serial-type and data blob separately.
75172 ** -------------- --------------- ---------------
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;
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;
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
75292 ** GCC might have just copying the problem from a prior compiler.
75293 ** I am also told that newer versions of GCC that follow a different
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);
75382 /* Input "x" is a sequence of unsigned characters that represent a
75383 ** big-endian integer. Return the equivalent native integer
75396 ** The few cases that require local variables are broken out into a separate
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 */
75495 /* These use local variables, so do them in a separate routine
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;
75524 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
75528 ** stack space). If the former, then *ppFree is set to a pointer that should
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;
75600 ** Return false if there is a disagreement.
75616 pKeyInfo = pPKey2->pKeyInfo;
75617 if( pKeyInfo->db==0 ) return 1;
75618 mem1.enc = pKeyInfo->enc;
75619 mem1.db = pKeyInfo->db;
75626 ** the unnecessary initialization has a measurable negative performance
75627 ** impact, since this routine is a very high runner. And so, we choose
75635 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
75636 assert( pKeyInfo->aSortOrder!=0 );
75637 assert( pKeyInfo->nField>0 );
75646 ** a buffer overread. The "d1+serial_type1+2" subexpression will
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 );
75675 ** the following assert(). If the assert() fails, it indicates a
75676 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
75683 rc = pPKey2->default_rc;
75690 if( pKeyInfo->db->mallocFailed ) return 1;
75697 ** Count the number of fields (a.k.a. columns) in the record given by
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 );
75728 # define vdbeAssertFieldCountWithinLimits(A,B,C)
75733 ** using the collation sequence pColl. As usual, return a negative , zero
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);
75771 ** The input pBlob is guaranteed to be a Blob that is not marked
75772 ** with MEM_Zero. Return true if it could be a zero-blob.
75785 ** If one blob is a prefix of the other, then the shorter is the lessor.
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);
75869 /* At least one of the two values is a number
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;
75899 /* If one value is a string and the other is a blob, the string is less.
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 );
75923 /* If a NULL pointer was passed as the collate function, fall through
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
75935 ** except 7. The second points to a buffer containing an integer value
75970 return (serial_type - 8);
75975 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
75977 ** greater than key2. The {nKey1, pKey1} key must be a blob
75979 ** key must be a parsed key such as obtained from
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);
76085 /* RHS is a string */
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;
76116 /* RHS is a blob */
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 );
76166 ** the following assert(). If the assert() fails, it indicates a
76167 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
76174 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
76175 || pKeyInfo->db->mallocFailed
76177 pPKey2->eqSeen = 1;
76178 return pPKey2->default_rc;
76190 ** that (a) the first field of pPKey2 is an integer, and (b) the
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 */
76253 ** this code. Including it causes gcc to generate a faster switch
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;
76287 ** that (a) the first field of pPKey2 is a string, that (b) the first field
76288 ** uses the collation sequence BINARY and (c) that the size-of-header varint
76289 ** at the start of (pKey1/nKey1) fits in a single byte.
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
76348 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
76354 ** that the size-of-header varint that occurs at the start of each record
76355 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
76356 ** also assumes that it is safe to overread a buffer by at least the
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 ){
76395 ** pCur might be pointing to text obtained from a corrupt database file.
76407 ** than 2GiB are support - anything large must be database corruption.
76409 ** this code can safely assume that nCellKey is 32-bits
76422 /* The index entry must begin with a header size */
76430 /* The last field of the index should be an integer - the ROWID.
76432 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
76451 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
76466 ** the key string in pUnpacked. Write into *pRes a number
76470 ** pUnpacked is either created without a rowid or is truncated so that it
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;
76517 ** Set a flag in the vdbe to update the change counter when it is finalised
76521 v->changeCntOn = 1;
76525 ** Mark every prepared statement associated with a database connection
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;
76549 ** Return the SQLITE_PREPARE flags for a Vdbe.
76552 return v->prepFlags;
76556 ** Return a pointer to an sqlite3_value structure containing the value bound
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);
76581 ** Configure SQL variable iVar so that binding a new value to it signals
76582 ** to sqlite3_reoptimize() that re-preparing the statement may result
76583 ** in a better query plan.
76587 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
76589 v->expmask |= 0x80000000;
76591 v->expmask |= ((u32)1 << (iVar-1));
76596 ** Cause a function to throw an error if it was call from OP_PureFunc
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);
76620 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
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++){
76729 ** a legal notice, here is a blessing:
76745 ** Return TRUE (non-zero) of the statement supplied as an argument needs
76746 ** to be recompiled. A statement needs to be recompiled whenever the
76747 ** execution environment changes in a way that would alter the program
76754 return p==0 || p->expired;
76759 ** Check on a Vdbe to make sure it has not been finalized. Log
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;
76803 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
76807 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
76809 # define checkProfileCallback(DB,P) /*no-op*/
76813 ** The following routine destroys a virtual machine that is created by
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);
76842 ** back to its starting state so that it can be reused. A success code from
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;
76891 ** The following routines extract information from a Mem or sqlite3_value
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];
76995 /* Make a copy of an sqlite3_value object
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
77030 ** result as a string or blob but if the string or blob is too large, it
77043 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
77050 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
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);
77236 ** This function is called after a transaction has been committed. It
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);
77261 ** Execute the statement pStmt, either until a row of data is ready, the
77265 ** API. The only thing omitted is the automatic recompile if a
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
77288 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
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 ){
77316 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
77317 ** from interrupting a statement that has not yet started.
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
77399 ** sqlite3Step() to do most of the work. If a schema error occurs,
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);
77450 ** Extract the user data from a sqlite3_context structure and return a
77454 assert( p && p->pFunc );
77455 return p->pFunc->pUserData;
77459 ** Extract the user data from a sqlite3_context structure and return a
77462 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
77463 ** returns a copy of the pointer to the database connection (the 1st
77469 assert( p && p->pOut );
77470 return p->pOut->db;
77474 ** Return the current time for a statement. If the current time
77475 ** is requested more than once within the same run of a single prepared
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);
77519 ** Create a new aggregate context for p and return a pointer to
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;
77540 ** Allocate or return the aggregate context for a user function. A new
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.
77561 ** Undocumented behavior: If iArg is negative then access a cache of
77562 ** auxiliary data pointers that is available to all functions within a
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;
77674 ** Return a pointer to static memory containing an SQL NULL value.
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
77712 ** it is, return a pointer to the Mem for the value of that column.
77713 ** If iCol is not valid, return a pointer to a Mem which has a value
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);
77734 ** This function is called after invoking an sqlite3_value_XXX function on a
77735 ** column value (i.e. a value returned by evaluating an SQL expression in the
77736 ** select list of a SELECT statement) that may cause a malloc() failure. If
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);
77775 ** need to call malloc() to expand the result of a zeroblob()
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
77846 ** If the result is not a simple column reference (if it is an expression
77847 ** or a constant) then useTypes 2, 3, and 4 return NULL.
77867 db = p->db;
77872 sqlite3_mutex_enter(db->mutex);
77873 assert( db->mallocFailed==0 );
77874 ret = xFunc(&p->aColName[N]);
77875 /* A malloc may have failed inside of the xFunc() call. If this
77878 if( db->mallocFailed ){
77882 sqlite3_mutex_leave(db->mutex);
77930 ** Return the name of the database from which a result column derives.
77932 ** anything else which is not an unambiguous reference to a database column.
77946 ** Return the name of the table from which a result column derives.
77948 ** anything else which is not an unambiguous reference to a database column.
77962 ** Return the name of the table column from which a result column derives.
77964 ** anything else which is not an unambiguous reference to a database column.
77981 ** Routines used to attach values to wildcards in a compiled SQL statement.
77985 ** the same as binding a NULL value to the column. If the "i" parameter is
77988 ** A successful evaluation of this routine acquires the mutex on p.
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);
78019 ** binding a new value to this variable invalidates the current query plan.
78021 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
78023 ** for a statement, then the statement will be automatically recompiled,
78024 ** as if there had been a schema change, on the first sqlite3_step() call
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;
78035 ** Bind a text or BLOB value.
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);
78071 ** Bind a blob value to an SQL statement variable.
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;
78251 ** Return the name of a wildcard parameter. Return NULL if the index
78254 ** The result is always UTF-8.
78259 return sqlite3VListNumToName(p->pVList, i);
78263 ** Given a wildcard parameter name, return the index of the variable
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);
78298 ** database connections. But as this is a deprecated interface, we
78301 ** If the two statements contain a different number of bindings, then
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;
78350 ** Return a pointer to the next prepared statement after pStmt associated
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);
78374 ** Return the value of a status counter for a prepared statement
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;
78402 ** Return the SQL associated with a prepared statement
78406 return p ? p->zSql : 0;
78410 ** Return the SQL associated with a prepared statement with
78426 sqlite3_mutex_enter(p->db->mutex);
78428 sqlite3_mutex_leave(p->db->mutex);
78437 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
78438 ** if successful, or a NULL pointer if an OOM error is encountered.
78449 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
78456 ** This function is called from within a pre-update callback to retrieve
78457 ** a field of the row currently being updated or deleted.
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
78530 ** immediately by a user SQL statement. Or, if the change was made by a
78532 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
78533 ** top-level trigger etc.).
78535 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
78536 ** or SET DEFAULT action is considered a trigger.
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
78547 ** a field of the row currently being updated or inserted.
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
78589 ** value. Make a copy of the cell contents and return a pointer to it.
78590 ** It is not safe to return a pointer to the memory cell itself as the
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]);
78622 ** Return status data for a single loop within query pStmt.
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));
78695 ** a legal notice, here is a blessing:
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
78716 ** a host parameter. If the text contains no host parameters, return
78739 ** This function returns a pointer to a nul-terminated string in memory
78741 ** string contains a copy of zRawSql but with host parameters expanded to
78743 ** then the returned string holds a copy of zRawSql with "-- " prepended
78749 ** with large (multi-megabyte) strings and blobs.
78755 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
78759 ** parameter index is known, locate the value in p->aVar[]. Then render
78760 ** the value as a literal in place of the host parameter name.
78767 int idx = 0; /* Index of a host parameter */
78769 int n; /* Length of a token prefix */
78772 Mem *pVar; /* Value of a host parameter */
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);
78892 ** a legal notice, here is a blessing:
78900 ** bytecode of a prepared statement.
78914 ** not misused. A shallow copy of a string or blob just copies a
78927 ** The following global variable is incremented every time a cursor
78962 ** or MEM_Str that has been used by a VDBE opcode. The test procedures
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)
78998 ** Test a register to see if it exceeds the current maximum blob size.
79019 ** source code is in a single file (the amalgamation). Special values 1
79026 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
79042 ** Convert the given register into a string if it isn't one
79043 ** already. Return non-zero if a malloc() fails.
79046 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
79051 ** a pointer to a dynamically allocated string where some other entity
79056 ** This routine converts an ephemeral string into a dynamically allocated
79058 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
79061 if( ((P)->flags&MEM_Ephem)!=0 \
79065 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
79068 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
79075 int iDb, /* Database the cursor belongs to, or -1 */
79079 ** required for this VdbeCursor structure. It is convenient to use a
79080 ** vdbe memory cell to manage the memory allocation required for a
79083 ** * Sometimes cursor numbers are used for a couple of different
79084 ** purposes in a vdbe program. The different uses might require
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);
79126 ** Try to convert a value into a numeric representation if we can
79128 ** looks like a number, convert it into a number. If it does not
79129 ** look like a number, leave it alone.
79134 ** will have a MEM_Int representation when bTryForInt is true.
79136 ** If bTryForInt is false, then if the input string contains a decimal
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;
79162 ** Try to convert pRec to an integer representation or a
79163 ** floating-point representation if an integer representation
79169 ** Convert pRec to a text 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);
79193 ** there is already a string rep, but it is pointless to waste those
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);
79205 ** Try to convert the type of a function argument or a result column
79206 ** into a numeric representation. Use either INTEGER or REAL whichever
79233 ** pMem currently only holds a string type (or maybe a BLOB that we can
79234 ** interpret as a string if we want to). Compute its corresponding
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) ){
79269 ** Write a nice string representation of the contents of cell pMem
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]);
79348 ** Print the value of a register for tracing purposes:
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.
79399 ** a legal notice, here is a blessing:
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
79426 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
79445 __asm__ __volatile__ ("rdtsc" : "=A" (val));
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;
79531 ** Execute as much of a VDBE program as we can.
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 ){
79567 /* This happens if a malloc() inside a call to sqlite3_column_text() or
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);
79639 ** if we have a special test build.
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 ){
79694 ** What follows is a massive switch statement where each case implements a
79697 ** that is a lot of wasted space on the left margin. So the code within
79698 ** the switch statement will break with convention and be flush-left. Another
79708 ** case statement is followed by a comment of the form "/# same as ... #/"
79736 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
79737 ** that this Goto is the bottom of a loop and that the lines from P2 down
79742 pOp = &aOp[pOp->p2 - 1];
79744 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
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
79761 ** a return code SQLITE_ABORT.
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);
79790 /* Most jump operations do a goto to this spot in order to update
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;
79835 ** The instruction at the address in register P1 is a Yield.
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;
79857 ** has the effect of yielding to a coroutine.
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);
79883 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
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;
79899 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
79908 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
79920 ** every program. So a jump past the last instruction of the program
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
80025 ** into a String opcode before it is executed for the first time. During
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] ){
80067 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
80068 ** of a string, as if it had been CAST. In other words:
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;
80094 ** Write a NULL into registers P2. If P3 greater than P2, then also write
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--;
80127 ** the register, so that if the value was a string or blob that was
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;
80140 ** P4 points to a blob of data P1 bytes long. Store this
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
80180 ** left holding a NULL. It is an error for register ranges
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 );
80219 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
80221 ** This instruction makes a deep copy of the value. A duplicate
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;
80248 ** Make a shallow copy of register P1 into register P2.
80250 ** This instruction makes a shallow copy of the value. If the value
80251 ** is a string or blob, then the copy is only a pointer to the
80255 ** during the lifetime of the copy. Use OP_Copy to make a complete
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 );
80324 ** modified to the user. This is the only way that a VM that
80325 ** opens a statement transaction may invoke this opcode.
80327 ** In case this is such a statement, close any statement transaction
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
80346 ** a side effect.
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;
80381 ** to avoid a memcpy().
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;
80548 ** P4 is a pointer to a CollSeq object. If the next call to a user function
80550 ** be returned. This is used by the built-in min(), max() and nullif()
80553 ** If P1 is not zero, then it is a register that a subsequent min() or
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;
80625 /* If shifting by a negative amount, shift in the other direction */
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 ){
80695 ** If register P1 holds an integer convert it to a real value.
80697 ** This opcode is used when extracting information from a column that
80700 ** to have only a real value.
80703 pIn1 = &aMem[pOp->p1];
80704 if( pIn1->flags & MEM_Int ){
80718 ** <li> P2=='A' → BLOB
80725 ** A NULL value is not changed by this routine. It remains NULL.
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 -
80765 ** numeric, then a numeric comparison is used. If the two values
80777 ** In other words, a prior r[P2] value will not be overwritten by 1 (true).
80788 ** In other words, a prior r[P2] value will not be overwritten by 0 (false).
80801 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
80815 ** numeric, then a numeric comparison is used. If the two values
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 ){
80892 /* Neither operand is NULL. Do a comparison. */
80893 affinity = pOp->p5 & SQLITE_AFF_MASK;
80898 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
80899 flags3 = pIn3->flags;
80906 ** optimization, to avoid a call to sqlite3MemCompare() */
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 ){
80954 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
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
81032 ** P4 is a KeyInfo structure that defines collating sequences and sort
81036 ** The comparison is a sort comparison, so NULLs compare equal,
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];
81119 ** the other input is NULL. A NULL and true or two NULLs give
81120 ** a NULL output.
81129 ** even if the other input is NULL. A NULL and false or two NULLs
81130 ** give a NULL output.
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;
81169 ** Interpret the value in register P1 as a boolean value. Store the
81171 ** NULL, then a NULL is stored in P2.
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
81189 ** a NULL then store a NULL in P2.
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
81214 ** For subprograms, there is a bitmask in the VdbeFrame that determines
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.
81249 ** is considered false if it has a numeric value of zero. If the value
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 ){
81304 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
81306 ** If P1 is not on a NULL row, then fall through without making any
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);
81322 ** Interpret the data that cursor P1 points to as a structure built using
81324 ** information about the format of the data.) Extract the P2-th column
81326 ** values in the record, extract a NULL.
81330 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
81331 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
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
81340 ** the result is guaranteed to only be used as the argument of a length()
81348 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
81357 u64 offset64; /* 64-bit offset */
81359 u32 t; /* A type code from the record header */
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;
81425 /* Make sure a corrupt database has not given us an oversize header.
81430 ** them, respectively. So the maximum header length results from a
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);
81507 ** columns. So the result will be either the default value or a NULL.
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];
81539 /* If the column value is a string, we need a persistent value, not
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))
81566 ** 2. the length(X) function if X is a blob, and
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);
81594 ** Apply affinities to a range of P2 registers starting with P1.
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)] );
81621 ** use as a data record in a database table or as a key
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
81634 u8 *zNewRecord; /* A buffer to hold the data for the new record */
81640 int nVarint; /* Number of bytes in a varint */
81649 u32 len; /* Length of a field */
81654 ** ------------------------------------------------------------------------
81655 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
81656 ** ------------------------------------------------------------------------
81661 ** Each type field is a varint representing the serial type of the
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
81740 /* Rare case of a really large header */
81746 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
81750 /* Make sure the output register has a buffer large enough to store
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;
81803 nEntry = 0; /* Not needed. Only used to silence a warning. */
81807 pOut->u.i = nEntry;
81815 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
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 ){
81842 /* A new savepoint cannot be created if there are active write
81845 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
81851 /* This call is Ok even if this savepoint is actually a transaction
81853 ** If this is a transaction savepoint being opened, it is guaranteed
81854 ** that the db->aVTrans[] array is empty. */
81855 assert( db->autoCommit==0 || db->nVTrans==0 );
81857 db->nStatement+db->nSavepoint);
81861 /* Create a new savepoint structure. */
81864 pNew->zName = (char *)&pNew[1];
81865 memcpy(pNew->zName, zName, nName+1);
81867 /* If there is no open transaction, then mark this as a special
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 ){
81899 /* It is not possible to release (commit) a savepoint if there are
81902 sqlite3VdbeError(p, "cannot release savepoint - "
81907 /* Determine whether or not this is a transaction savepoint. If so,
81908 ** and this is a RELEASE command, then the current transaction
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);
81952 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
81954 while( db->pSavepoint!=pSavepoint ){
81955 pTmp = db->pSavepoint;
81956 db->pSavepoint = pTmp->pNext;
81958 db->nSavepoint--;
81961 /* If it is a RELEASE, then destroy the savepoint being operated on
81962 ** too. If it is a ROLLBACK TO, then set the number of deferred
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
81992 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
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 ){
82014 /* If this instruction implements a COMMIT and other VMs are writing
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 ){
82042 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
82043 (iRollback)?"cannot rollback - no transaction is active":
82044 "cannot commit - no transaction is active"));
82054 ** Begin a transaction on database P1 if a transaction is not already
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
82067 ** throw an ABORT exception), a statement transaction may also be opened.
82068 ** More specifically, a statement transaction is opened iff the database
82070 ** active statements. A statement transaction allows the changes made by this
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;
82212 ** A transaction must be started before executing this opcode.
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
82247 ** P2 in a database file. The database file is determined by P3.
82257 ** There will be a read lock on the database whenever there is an
82259 ** then a read lock is acquired as part of this instruction. A read
82263 ** to get a read lock but fails, the script terminates with an
82266 ** The P4 value may be either an integer (P4_INT32) or a pointer to
82267 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
82278 ** checks to see if the cursor on P1 is already open with a root page
82279 ** number of P2 and if it is this opcode becomes a no-op. In other words,
82283 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
82291 ** Open a read/write cursor named P1 on the table or index whose root
82295 ** The P4 value may be either an integer (P4_INT32) or a pointer to
82296 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
82303 ** in read/write mode. For a given table, there can be one or more read-only
82304 ** cursors or a single read/write cursor but not both.
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 */
82325 /* If the cursor is not currently open or is open on a different
82326 ** index, then fall through into OP_OpenRead to force a reopen */
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;
82367 /* The p2 value always comes from a prior OP_CreateTable opcode and
82369 ** If there were a failure, the prepared statement would have halted
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)));
82415 ** Open a new cursor P1 that points to the same ephemeral table as
82416 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
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);
82437 ** opened for a database. Since there is already an open cursor when this
82447 ** Open a new cursor P1 to a transient table.
82449 ** the main database is read-only. The ephemeral
82453 ** The cursor points to a BTree table if P4==0 and to a BTree index
82454 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
82457 ** The P5 parameter can be a mask of the BTREE_* flags defined
82465 ** This opcode works the same as OP_OpenEphemeral. It has a
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);
82493 /* If a transient index is required, create it by calling
82495 ** opening it. If a transient table is required, just use the
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);
82524 ** a transient index that is specifically designed to sort large
82525 ** tables using an external merge-sort algorithm.
82527 ** If argument P3 is non-zero, then it indicates that the sorter may
82528 ** assume that a stable sort considering the first P3 fields of each
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);
82549 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
82555 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82556 pC = p->apCsr[pOp->p1];
82558 if( (pC->seqCount++)==0 ){
82567 ** Open a new cursor that points to a fake table that contains a single
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 );
82596 ** Close a cursor previously opened as P1. If P1 is not
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),
82639 ** opcode will always land on a record that equally equals the key, or
82654 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82655 ** use the value in register P3 as a key. If cursor P1 refers
82672 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82673 ** use the value in register P3 as a key. If cursor P1 refers
82690 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
82691 ** use the value in register P3 as a key. If cursor P1 refers
82704 ** opcode will always land on a record that equally equals the key, or
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 ){
82759 /* If the P3 value cannot be converted into any kind of a number,
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 */
82794 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
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 );
82896 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
82901 ** is a prefix of any entry in P1 then a jump is made to P2 and
82904 ** This operation leaves the cursor in a state where it can be
82913 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
82918 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
82923 ** This operation leaves the cursor in a state where it cannot be
82932 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
82938 ** record are not-NULL then a check is done to determine if any row in the
82939 ** P1 index btree has a matching key prefix. If there are no matches, jump
82940 ** immediately to P2. If there is a match, fall through and leave the P1
82946 ** This operation leaves the cursor in a state where it cannot be
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 ){
83005 ** input fields are NULL, since any key with a NULL will not
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 ){
83037 ** P1 is the index of a cursor open on an SQL table btree (with integer
83039 ** contain a record with rowid P3 then jump immediately to P2.
83041 ** a record with rowid P3 then
83050 ** (with arbitrary multi-value keys).
83052 ** This opcode leaves the cursor in a state where it cannot be advanced
83061 ** P1 is the index of a cursor open on an SQL table btree (with integer
83062 ** keys). P3 is an integer rowid. If P1 does not contain a record with
83064 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
83069 ** P3 register to contain a non-integer value, in which case the jump is
83073 ** (with arbitrary multi-value keys).
83075 ** This opcode leaves the cursor in a state where it cannot be advanced
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++;
83149 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
83150 ** The record number is not previously used as a key in the database
83154 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
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.
83186 ** The second algorithm is to select a rowid at random and see if
83188 ** succeeded. If the random rowid does exist, we select a new one
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 ){
83223 /* Assert that P3 is a valid memory cell. */
83224 assert( pOp->p3>0 );
83225 if( p->pFrame ){
83226 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
83227 /* Assert that P3 is a valid memory cell. */
83228 assert( pOp->p3<=pFrame->nMem );
83229 pMem = &pFrame->aMem[pOp->p3];
83231 /* Assert that P3 is a valid memory cell. */
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;
83283 ** Write an entry into the table of cursor P1. A new entry is
83287 ** be a MEM_Int.
83297 ** seeks on the cursor or if the most recent seek used a key equal to P3.
83304 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
83305 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
83306 ** following a successful insert.
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);
83364 pTab = 0; /* Not needed. Silence a compiler warning. */
83365 zDb = 0; /* Not needed. Silence a compiler warning. */
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
83418 ** it is ok to delete a record from within a Next loop. If
83423 ** delete one of several associated with deleting a table row and all its
83431 ** P1 must not be pseudo-table. It has to be a real table with
83434 ** If P4 is not NULL then it points to a Table object. In this case either
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
83470 ** the name of the db to pass as to it. Also set local pTab to a copy
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);
83483 zDb = 0; /* Not needed. Silence a compiler warning. */
83484 pTab = 0; /* Not needed. Silence a compiler warning. */
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;
83555 ** P1 is a sorter cursor. This instruction compares a prefix of the
83556 ** record blob in register P3 against a prefix of the entry that
83560 ** If either P3 or the sorter contains a NULL in one of their significant
83572 pC = p->apCsr[pOp->p1];
83574 assert( pOp->p4type==P4_INT32 );
83575 pIn3 = &aMem[pOp->p3];
83576 nKeyCol = pOp->p4.i;
83591 ** This opcode is normally use to move a record out of the sorter and into
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
83595 ** us from having to issue a separate NullRow instruction to clear that cache.
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;
83621 ** If cursor P2 is a table, then the content extracted is the data.
83623 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
83624 ** of a real table, not a pseudo-table.
83628 ** register will be invalidated as soon as the cursor moves - including
83631 ** then a copy of the data is made into memory. P3!=0 is faster, but
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;
83660 ** generator) then the fix would be to insert a call to
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);
83689 ** P1 can be either an ordinary table or a virtual table. There used to
83690 ** be a separate OP_VRowid opcode for use with virtual tables, but this
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;
83736 ** Move the cursor P1 to a null row. Any OP_Column operations
83738 ** write a NULL.
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
83768 ** for the purpose of appending a new entry onto the btree. In that
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;
83848 ** Sorting is accomplished by writing records into a sorting index,
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
83921 ** The P3 value is a hint to the btree implementation. If P3==1, that
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
83954 ** The P3 value is a hint to the btree implementation. If P3==1, that
83963 ** number P5-1 in the prepared statement is incremented.
83968 ** open it behaves a no-op.
83972 ** This opcode works just like OP_Next except that P1 must be a
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
84050 ** seeks on the cursor or if the most recent seek used a key equivalent
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;
84138 ** P1 is an open index cursor and P3 is a cursor on the corresponding
84139 ** table. This opcode does a deferred seek of the P3 table cursor
84142 ** This is a deferred seek. Nothing actually happens until
84143 ** the cursor is used to read a record. That way, if no reads
84147 ** one entry for each column in the P3 table. If array entry a(i)
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 ){
84188 rowid = 0; /* Not needed. Only used to silence a warning. */
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];
84291 res = 0; /* Not needed. Only used to silence a warning. */
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 -
84321 ** table being dropped was already the last one in the database) then a
84322 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
84327 ** updating existing cursors when a root page is moved in an AUTOVACUUM
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) );
84349 iMoved = 0; /* Not needed. Only to silence a warning. */
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);
84433 ** Allocate a new table in the main database file if P1==0 or in the
84438 ** The difference between a table and an index is this: A table must
84439 ** have a 4-byte integer key and can have arbitrary data. An index
84447 ** Allocate a new index in the main database file if P1==0 or in the
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--;
84496 ** This opcode invokes the parser to create a new virtual machine,
84497 ** then runs the new virtual machine. It is thus a re-entrant opcode.
84506 ** on every btree. This is a prerequisite for invoking
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 );
84518 /* Used to be a conditional */ {
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
84567 ** the table named P4 in database P1. This is called after a table
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
84593 ** the trigger named P4 in database P1. This is called after a trigger
84599 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
84609 ** If no problems are found, store a NULL in register P1.
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);
84663 ** Insert the integer value held by register P2 into a RowSet object
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
84700 /* A value was pulled from the index */
84702 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
84710 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
84711 ** contains a RowSet object and that RowSet object contains
84718 ** Each set is identified by a unique P4 value. The first set
84719 ** must have P4==0, the final set must have P4==-1, and for all other sets
84722 ** This allows optimizations: (a) when P4==0 there is no need to test
84724 ** (b) when P4==-1 there is no need to insert the value, as it will
84725 ** never be tested for, and (c) when a value that is part of set X is
84734 pIn1 = &aMem[pOp->p1];
84735 pIn3 = &aMem[pOp->p3];
84736 iSet = pOp->p4.i;
84737 assert( pIn3->flags&MEM_Int );
84739 /* If there is anything other than a rowset object in memory cell P1,
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
84771 ** of a memory cell in this (the parent) VM that is used to allocate the
84772 ** memory required by the sub-vdbe at runtime.
84774 ** P4 is a pointer to the VM containing the trigger program.
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
84794 ** is really a trigger, not a foreign key action, and the flag set
84798 ** disabled. In some cases a single trigger may generate more than one
84800 ** ON CONFLICT algorithm). SubProgram structures associated with a
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
84897 ** OP_Program instruction. Copy a value currently stored in a memory
84910 pFrame = p->pFrame;
84911 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
84922 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
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;
84966 ** P1 is a register in the root frame of this VM (the root frame is
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)
85017 ** This opcode performs a commonly used computation associated with
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) ){
85047 ** looping forever is a reasonable approximation. */
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;
85094 ** function has P5 arguments. P4 is a pointer to the FuncDef
85105 ** function has P5 arguments. P4 is a pointer to an sqlite3_context
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];
85149 /* If this function is inside of a trigger, the register array in aMem[]
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;
85196 ** P4 is a pointer to the FuncDef for this function. The P2
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++){
85263 ** modes (delete, truncate, persist, off and memory), this is a simple
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;
85301 /* Do not allow a transition to journal_mode=WAL for a database
85306 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
85314 if( !db->autoCommit || db->nVdbeRead>1 ){
85317 "cannot change %s wal mode from within a transaction",
85325 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
85327 ** after a successful return.
85339 /* Open a transaction on the database file. Regardless of the journal
85340 ** mode, this transaction always uses a rollback journal.
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);
85380 ** Perform a single step of the incremental vacuum procedure on
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;
85425 ** Obtain a lock on a particular table. This instruction is only used when
85426 ** the shared-cache feature is enabled.
85429 ** on which the lock is acquired. A readlock is obtained if P3==0 or
85430 ** a write lock if P3==1.
85432 ** P2 contains the root-page of the table to lock.
85434 ** P4 contains a pointer to the name of the table being locked. This is only
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;
85460 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
85464 ** within a callback to a virtual table xSync() method. If it is, the error
85469 pVTab = pOp->p4.pVtab;
85471 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
85480 ** P2 is a register that holds the name of a virtual table in database
85489 /* Because P2 is always a static string, it is impossible for the
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);
85509 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
85513 db->nVDestroy++;
85514 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
85515 db->nVDestroy--;
85524 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
85525 ** P1 is a cursor number. This opcode opens a cursor to the virtual
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);
85568 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
85571 ** P4 is either NULL or a string that was generated by the xBestIndex
85582 ** A jump is made to P2 if the result set after filtering would be empty.
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);
85720 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
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;
85752 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
85755 ** invocation. The value in register (P3+P2-1) corresponds to the
85758 ** The xUpdate method will do a DELETE or an INSERT or both.
85760 ** is the rowid of a row to delete. If argv[0] is NULL then no
85767 ** a row to delete.
85769 ** P1 is a boolean flag. If it is set to true and the xUpdate call
85774 ** apply in the case of a constraint failure on an insert or update.
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);
85871 ** Invoke a user function (P4 is a pointer to a FuncDef object that
85876 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
85879 ** whether meta data associated with a user function argument using the
85888 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
85889 ** contains a pointer to the function to be run) with P5 arguments taken
85893 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
85896 ** whether meta data associated with a user function argument using the
85901 ** to a FuncDef object. But on first evaluation, the P4 operand is
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;
85941 /* If this function is inside of a trigger, the register array in aMem[]
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);
85987 ** Programs contain a single instance of this opcode as the very first
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]++;
86069 ** Provide a hint to cursor P1 that it only needs to return rows that
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);
86091 ** Do nothing. This instruction is often useful as a jump
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
86177 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
86185 /* Jump to here if a malloc() fails.
86197 assert( db->u1.isInterrupted );
86198 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
86199 p->rc = rc;
86211 ** a legal notice, here is a blessing:
86245 ** the b-tree cursor associated with blob handle p to point to row iRow.
86250 ** contain a value of type TEXT or BLOB in the column nominated when the
86252 ** be set to point to a buffer containing an error message. It is the
86256 ** If an error does occur, then the b-tree cursor is closed. All subsequent
86263 Vdbe *v = (Vdbe *)p->pStmt;
86266 ** This is done directly as a performance optimization
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));
86322 ** Open a blob handle.
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 ){
86411 ** column is not indexed, and that it is not part of a foreign key.
86417 if( db->flags&SQLITE_ForeignKeys ){
86419 ** is not necessary to check if it is part of a parent key, as parent
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 ){
86455 /* This VDBE program seeks a btree cursor to the identified
86456 ** db/table/row entry. The reason for using a vdbe program instead
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
86472 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
86473 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
86480 Vdbe *v = (Vdbe *)pBlob->pStmt;
86481 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
86485 pTab->pSchema->schema_cookie,
86486 pTab->pSchema->iGeneration);
86490 /* Make sure a mutex is held on the table to be accessed */
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);
86557 ** Close a blob handle that was previously created using
86566 db = p->db;
86567 sqlite3_mutex_enter(db->mutex);
86568 rc = sqlite3_finalize(p->pStmt);
86570 sqlite3_mutex_leave(db->mutex);
86578 ** Perform a read or write operation on a blob
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 ){
86598 /* Request is out of range. Return a transient error. */
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
86623 ** slightly more efficient). Since you cannot write to a PK column
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);
86651 ** Read data from a blob handle.
86658 ** Write data to a blob handle.
86665 ** Query a blob handle for the size of the data.
86672 return (p && p->pStmt) ? p->nByte : 0;
86676 ** Move an existing blob handle to point to a different row of the same
86680 ** contain a blob or text value, then an error code is returned and the
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
86723 ** a legal notice, here is a blessing:
86731 ** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
86735 ** The VdbeSorter object implements a multi-threaded external merge sort
86739 ** Here is the (internal, non-API) interface between this module and the
86742 ** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
86744 ** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
86745 ** object. The row is a binary blob in the
86748 ** in the case of a SELECT w/ ORDER BY, or
86750 ** in the case of a CREATE INDEX.
86766 ** Used to enforce uniqueness in a
86776 ** The interfaces above must be called in a particular order. Write() can
86791 ** a threshold, when Rewind() is called the set of records is sorted using
86792 ** an in-memory merge sort. In this case, no temporary files are required
86798 ** written to a temporary file in "Packed Memory Array" (PMA) format.
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.
86804 ** records to a PMA is roughly the same as the limit configured for the
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
86810 ** are appended to a single temporary file. Or, if the sorter is running in
86811 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
86813 ** sorting the records and writing the PMA to a temporary file itself, the
86814 ** calling thread usually launches a worker thread to do so. Except, if
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
86823 ** When Rewind() is called, any data remaining in memory is flushed to a
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
86835 ** merged T bytes of data into a single sorted PMA, the main thread
86840 ** by Write() above to determine when to create a new PMA.
86843 ** Rewind() is called, then a hierarchy of incremental-merges is used.
86847 ** PMAs at a time. This done is to improve locality.
86849 ** If running in multi-threaded mode and there are more than
86853 ** thread to merge the output of each of the others to a single PMA for
86862 ** characteristics of the sorter in multi-threaded mode.
86869 ** Hard-coded maximum amount of data to accumulate in memory before flushing
86870 ** to a level 0 PMA. The purpose of this limit is to prevent various integer
86881 typedef struct SorterRecord SorterRecord; /* A record being sorted */
86882 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
86884 typedef struct SorterList SorterList; /* In-memory list of records */
86888 ** A container for a temp file handle and the current amount of data
86897 ** An in-memory list of objects to be sorted.
86906 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
86912 ** one big PMA using a merge operation. Separate PMAs all need to be
86916 ** The aReadr[] array contains a PmaReader object for each of the PMAs being
86917 ** merged. An aReadr[] object either points to a valid key or else is at EOF.
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
86959 ** aReadr[5] -> Eggplant
86964 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
86982 ** This object represents a single thread of control in a sort operation.
86987 ** single-threaded operation, there is exactly one instance of this object
86988 ** and for multi-threaded operation there are two or more instances.
86991 ** structure for which each thread requires a separate instance. For example,
86995 ** Before a background thread is launched, variable bDone is set to 0. Then,
86999 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
87000 ** attempt to select a SortSubtask for which there is not already an
87004 ** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
87016 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
87017 UnpackedRecord *pUnpacked; /* Space to unpack a record */
87018 SorterList list; /* List for thread to write to a PMA */
87021 SorterFile file; /* Temp file for level-0 PMAs */
87027 ** Main sorter structure. A single instance of this is allocated for each
87045 SorterList list; /* List of in-memory records */
87060 ** An instance of the following object is used to read records out of a
87063 ** contain a contiguous representation of the key, then aAlloc is allocated
87083 ** Normally, a PmaReader object iterates through an existing PMA stored
87084 ** within a temp file. However, if the PmaReader.pIncr variable points to
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]
87096 ** a background thread to populate aFile[1] with the next mxSz bytes of
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
87109 ** background thread to prepare data for the PmaReader, with a single
87110 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
87120 int bUseThread; /* True to use a bg thread for this object */
87125 ** An instance of this object is used for writing a PMA.
87127 ** The PMA is written one record at a time. Each record is of an arbitrary
87128 ** size. But I/O is more efficient if it occurs in page-sized blocks where
87129 ** each block is aligned on a page boundary. This object caches writes to
87130 ** the PMA so that aligned, page-size blocks are written.
87133 int eFWErr; /* Non-zero if in an error state */
87143 ** This object is the header on a single record while that record is being
87144 ** held in memory and prior to being written out as part of a PMA.
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
87169 /* Return a pointer to the buffer containing the record data for SorterRecord
87177 /* Maximum number of PMAs that a single MergeEngine can merge */
87188 sqlite3_free(pReadr->aAlloc);
87189 sqlite3_free(pReadr->aBuffer);
87190 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
87191 vdbeIncrFree(pReadr->pIncr);
87197 ** If successful, set *ppOut to point to a buffer containing the data
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
87245 ** case there is no need to make a copy of the data, just return a
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;
87295 ** Read a varint from the stream of data accessed by p. Set *pnOut to
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);
87308 u8 aVarint[16], *a;
87311 rc = vdbePmaReadBlob(p, 1, &a);
87313 aVarint[(i++)&0xf] = a[0];
87314 }while( (a[0]&0x80)!=0 );
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
87440 ** contains a single PMA, and that that PMA omits the initial length varint.
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;
87471 ** A version of vdbeSorterCompare() that assumes that it has already been
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);
87517 ** A specially optimized version of vdbeSorterCompare() that assumes that
87518 ** the first field of each key is a TEXT value and that the collation
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;
87559 ** A specially optimized version of vdbeSorterCompare() that assumes that
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;
87626 ** Initialize the temporary index cursor just opened as a sorter cursor.
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
87632 ** is used instead. This is used when sorting records for a CREATE INDEX
87636 ** any reason to compare PK fields and they can be ignored for a small
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;
87845 ** Launch a background thread to run xTask(pIn).
87849 void *(*xTask)(void*), /* Routine to run in a separate thread */
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
87868 ** is currently attempt to join one of the other threads. To avoid a race
87870 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
87871 for(i=pSorter->nTask-1; i>=0; i--){
87872 SortSubtask *pTask = &pSorter->aTask[i];
87884 ** Allocate a new MergeEngine object capable of handling up to
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);
87942 ** Reset a sorting cursor back to its original empty state.
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;
88057 ** Merge the two sorted lists p1 and p2 into a single list.
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;
88242 ** Write value iVal encoded as a varint to the PMA. Return
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
88257 ** The format of a PMA is:
88259 ** * A varint. This varint contains the total number of bytes of content
88262 ** * One or more records packed end-to-end in order of ascending keys.
88263 ** Each record consists of a varint followed by a blob of data (the
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
88374 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
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;
88412 ** Flush the current contents of VdbeSorter.list to a new PMA, possibly
88413 ** using a background thread.
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);
88451 /* Launch a background thread for this operation */
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;
88480 ** Add a record to the sorter.
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;
88508 ** flushed to a PMA before continuing. If so, do so.
88510 ** If using the single large allocation mode (pSorter->aMemory!=0), then
88511 ** flush the contents of memory to a new PMA if (a) at least one value is
88515 ** of memory to a PMA if either of the following are true:
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;
88640 ** Launch a background thread to populate aFile[1] of pIncr.
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
88661 ** been exhausted, this function also launches a new background thread
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;
88700 ** Allocate and return a new IncrMerger object to read data from pMerger.
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
88802 ** building a merge tree.
88831 int nTree = pMerger->nTree;
88833 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
88836 /* Verify that the MergeEngine is assigned to a single thread */
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
88878 ** to its first key. A call to vdbePmaReaderNext() is still required to do
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),
88979 ** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
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;
89043 ** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
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;
89116 ** This function is called as part of a SorterRewind() operation on a sorter
89117 ** that has already written two or more level-0 PMAs to one or more temp
89118 ** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
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);
89198 ** on a sorter that has written two or more PMAs to temporary files. It sets
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++){
89246 ** a) The incremental merge object is configured to use the
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()
89310 ** creates a new list consisting of a single key immediately afterwards.
89312 assert( pSorter->list.pList );
89320 /* Assuming no errors have occurred, set up a merger structure to
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;
89374 ** Return a pointer to a buffer owned by the sorter that contains the
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);
89430 ** Otherwise, set *pRes to a negative, zero or positive value if the
89435 ** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
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);
89479 ** a legal notice, here is a blessing:
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.
89508 ** The rollback journal is composed of a linked list of these structures.
89510 ** The zChunk array is always at least 8 bytes in size - usually much more.
89527 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
89530 ** An instance of this object serves as a cursor into the rollback journal.
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;
89623 ** Flush the contents of memory to a real file on disk.
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;
89810 ** Open a journal file.
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
89832 ** case none of the code in this module is executed as a result of calls
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);
89863 ** If the argument p points to a MemJournal structure that is not an
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;
89887 ** Return the number of bytes required to store a JournalFile that uses vfs
89888 ** pVfs to create the underlying on-disk files.
89891 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
89900 ** a legal notice, here is a blessing:
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;
90060 ** a legal notice, here is a blessing:
90069 ** resolve all identifiers by associating them with a particular
90077 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
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
90101 ** If the reference is followed by a COLLATE operator, then make sure
90104 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
90108 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
90112 ** zero but it might be more if the alias is contained within a subquery
90118 ExprList *pEList, /* A result set */
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
90145 ** make a copy of the token before doing the sqlite3DbFree().
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;
90177 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
90206 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
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 */
90249 int isTrigger = 0; /* True if resolved to a trigger column */
90251 Column *pCol; /* A column of pTab */
90257 /* Initialize the node to no-match */
90258 pExpr->iTable = -1;
90259 pExpr->pTab = 0;
90262 /* Translate the schema name in zDb into a pointer to the corresponding
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;
90356 ** it is a new.* or old.* trigger argument reference
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;
90409 ** Perhaps the name is a reference to the ROWID
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
90428 ** SELECT a+b AS x FROM table WHERE x<10;
90430 ** In cases like this, replace pExpr with a copy of the expression that
90431 ** forms the result set entry ("a+b" in the example) and return immediately.
90435 ** The ability to use an output result-set column in the WHERE, GROUP BY,
90436 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
90437 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
90438 ** is supported for backwards compatibility only. Hence, we issue a warning
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) ){
90471 ** we have a match (cnt>0) or when we run out of name contexts.
90474 pNC = pNC->pNext;
90481 ** supplied) and the value of Z is enclosed in double-quotes, then
90482 ** Z is a string literal if it doesn't match any column names. In that
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++;
90513 /* If a column from a table in pSrcList is referenced, then record
90514 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
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;
90558 ** Allocate and return a pointer to an expression to load the column iCol
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";
90601 ** Expression p should encode a floating point value between 1.0 and 0.0.
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;
90665 /* A column name: ID
90667 ** Or a database, table and column: ID.ID.ID
90670 ** be one call to lookupName(). Then the compiler will in-line
90671 ** lookupName() for a size reduction and performance increase.
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 ){
90733 "second argument to likelihood() must be a "
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;
90881 ** pEList is a list of expressions which are really the result set of the
90882 ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
90883 ** This routine checks to see if pE is a simple identifier which corresponds
90884 ** to the AS-name of one of the terms of the expression list. If it is,
90887 ** no match, or if pE is not a simple identifier, then this routine
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;
90914 ** pE is a pointer to an expression which is a single term in the
90915 ** ORDER BY of a compound SELECT. The expression has not been
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 "
90990 ** Analyze the ORDER BY clause in a compound SELECT statement. Modify
90991 ** each term of the ORDER BY clause is a constant integer between 1
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 ){
91094 ** the SELECT statement pSelect. If any term is reference to a
91095 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
91096 ** field) then convert that term into a copy of the corresponding result set
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().
91161 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
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
91176 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
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;
91214 int isCompound; /* True if p is a compound select */
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;
91232 ** already expanded this SELECT. However, if this is a subquery within
91233 ** an expression, sqlite3ResolveExprNames() will be called without a
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);
91325 /* If a HAVING clause is present, then there must be a GROUP BY clause.
91327 if( p->pHaving && !pGroupBy ){
91328 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
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)
91360 /* If this is a converted compound query, move the ORDER BY clause from
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) ){
91408 /* If this is part of a compound SELECT, check that it has the right
91410 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
91411 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
91417 p = p->pPrior;
91421 /* Resolve the ORDER BY on a compound SELECT after all terms of
91433 ** table columns and result-set columns. At the same time, do error
91434 ** checking on function usage and set a flag if any aggregate functions
91440 ** X: The name of a database. Ex: "main" or "temp" or
91441 ** the symbolic name assigned to an ATTACH-ed database.
91443 ** Y: The name of a table in a FROM clause. Or in a trigger
91446 ** Z: The name of a column in table Y.
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
91463 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
91465 ** The "x" term of the order by is replaced by "a+b" to render:
91467 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
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;
91514 ** list rather than a single expression.
91522 for(i=0; i<pList->nExpr; i++){
91523 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
91530 ** Resolve all names in all expressions of a SELECT and in all
91531 ** decendents of the SELECT, including compounds off of p->pPrior,
91535 ** See sqlite3ResolveExprNames() for a description of the kinds of
91558 ** Resolve names in expressions that can only reference a single table:
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;
91583 sSrc.a[0].pTab = pTab;
91584 sSrc.a[0].iCursor = -1;
91598 ** a legal notice, here is a blessing:
91615 ** Return the affinity character for a single column of a table.
91618 assert( iCol<pTab->nCol );
91619 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
91625 ** If pExpr is a column, a reference to a column via an 'AS' alias,
91626 ** or a sub-select with a column as the return value, then the
91633 ** CREATE TABLE t1(a);
91634 ** SELECT * FROM t1 WHERE a;
91635 ** SELECT a AS b FROM t1 WHERE b;
91636 ** SELECT * FROM t1 WHERE (select a from t1);
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;
91668 ** sequence named by pToken. Return a pointer to a new Expr node that
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;
91720 ** The collating sequence might be determined by a COLLATE operator
91721 ** or by the presence of a column with a defined collating sequence.
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
91745 ** a TK_COLUMN but was previously evaluated and cached in a register */
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;
91785 ** pExpr is an operand of a comparison operator. aff2 is the
91801 /* Neither side of the comparison is a column. Compare the
91806 /* One side is a column, the other is not. Use the columns affinity. */
91813 ** pExpr is a comparison operator. Return the type affinity that should
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);
91834 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
91852 ** Return the P5 value that should be used for a binary comparison
91862 ** Return a pointer to the collation sequence that should be used by
91863 ** a binary comparison operator comparing pLeft and pRight.
91865 ** If the left hand expression has a collating sequence type, then it is
91867 ** is used, or the default (BINARY) if neither expression has a collating
91870 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
91880 if( pLeft->flags & EP_Collate ){
91882 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
91894 ** Generate code for a comparison operator.
91911 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
91913 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
91918 ** Return true if expression pExpr is a vector, or false otherwise.
91920 ** A vector is defined as any expression that results in two or more
91922 ** parser will not generate a TK_VECTOR with fewer than two entries.
91923 ** But a TK_SELECT might be either a vector or a scalar. It is only
91924 ** considered a vector if it has two or more result columns.
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
91953 ** If pVector is really a scalar (and "scalar" here includes subqueries
91954 ** that return a single column!) then return pVector unmodified.
91958 ** If the vector is a (SELECT ...) then the expression returned is
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;
91977 ** Compute and return a new Expr object which when passed to
91979 ** the iField-th column of the vector expression pVector.
91981 ** It is ok for pVector to be a scalar (as long as iField==0).
91987 ** The caller retains ownership of pVector. If pVector is a TK_SELECT,
91989 ** valid for the life of the returned object. If pVector is a TK_VECTOR
91990 ** or a scalar expression, then it can be deleted as soon as this routine
91993 ** A trick to cause a TK_SELECT pVector to be deleted together with
91999 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
92003 if( pVector->op==TK_SELECT ){
92004 assert( pVector->flags & EP_xIsSelect );
92009 ** iColumn: Index of a column in pVector
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
92040 ** If pExpr is not a TK_SELECT expression, return 0.
92045 if( pExpr->op==TK_SELECT ){
92053 ** Argument pVector points to a vector expression - either a TK_VECTOR
92055 ** the register number of a register that contains the value of
92058 ** If pVector is a TK_SELECT expression, then code for it must have
92061 ** containing the results of the sub-select.
92065 ** a temporary register to be freed by the caller before returning.
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;
92093 ** Expression pExpr is a comparison between two vector values. Compute
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);
92239 ** Expr.pSelect member has a height of 1. Any other expression
92240 ** has a height equal to the maximum height of any other
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);
92298 ** Construct a new expression node and return a pointer to it. Memory
92299 ** for this node and for the pToken argument is a single allocation
92306 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
92309 ** Special case: If op==TK_INTEGER and pToken points to a string that
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
92378 ** If pRoot==NULL that means that a memory allocation error has occurred.
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;
92407 ** One or both of the subtrees can be NULL. Return a pointer to the new
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);
92438 ** do a memory allocation failure) then delete the pSelect object.
92442 pExpr->x.pSelect = pSelect;
92446 assert( pParse->db->mallocFailed );
92447 sqlite3SelectDelete(pParse->db, pSelect);
92455 ** expression at compile-time return 0.
92458 ** the expression really is always false or false (a false negative).
92459 ** But it is a bug to return 1 if the expression might have different
92460 ** boolean values in different circumstances (a false positive.)
92462 ** Note that if the expression is part of conditional for a
92463 ** LEFT JOIN, then we cannot determine at compile-time whether or not
92484 ** of returning an AND expression, just return a constant expression with
92485 ** a value of false.
92504 ** Construct a new expression node for a function with multiple
92509 sqlite3 *db = pParse->db;
92516 pNew->x.pList = pList;
92523 ** Assign a variable number to an expression that encodes a wildcard
92526 ** Wildcards consisting of a single "?" are assigned the next sequential
92530 ** sure "nnn" is not too big to avoid a denial of service attack when
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);
92651 ** to store a copy of an expression or expression tree. They differ in
92660 ** The dupedExprStructSize() function returns two values OR-ed together:
92661 ** (1) the space required for a copy of the Expr structure only and
92673 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
92678 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
92679 ** to reduce a pristine expression tree from the parser. The implementation
92688 if( 0==flags || p->op==TK_SELECT_COLUMN ){
92695 if( p->pLeft || p->x.pList ){
92698 assert( p->pRight==0 );
92707 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
92712 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
92713 nByte += sqlite3Strlen30(p->u.zToken)+1;
92719 ** Return the number of bytes required to create a duplicate of the
92720 ** expression passed as the first argument. The second argument is a
92723 ** The value returned includes space to create a copy of the Expr struct
92736 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
92744 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
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);
92844 ** Create and return a deep copy of the object passed as the second
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);
92882 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
92883 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
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;
92938 ** called with a NULL argument.
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
92997 ** necessarily a power of two, sqlite3IdListAppend() may not be called
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;
93052 ** Add a new element to the end of an expression list. If pList is
93053 ** initially NULL, then create a new expression list.
93055 ** If a memory allocation error occurs, the entire list is freed and
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;
93099 ** pColumns and pExpr form a vector assignment which is part of the SET
93102 ** (a,b,c) = (expr1,expr2,expr3)
93103 ** Or: (a,b,c) = (SELECT x,y,z FROM ....)
93106 ** expression list pList. In the case of a subquery on the RHS, append
93115 sqlite3 *db = pParse->db;
93118 int iFirst = pList ? pList->nExpr : 0;
93124 /* If the RHS is a vector, then we can immediately check to see that
93125 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
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;
93181 ** Set the ExprList.a[].zName element of the most recently added item
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);
93206 ** Set the ExprList.a[].zSpan element of the most recently added item
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
93297 ** is found to not be a constant.
93300 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
93301 ** an existing schema and 4 when processing a new statement. A bound
93304 ** contain a bound parameter because they were generated by older versions
93305 ** of SQLite to be parsed by newer versions of SQLite without raising a
93310 /* If pWalker->eCode is 2 then any term of the expression that comes from
93311 ** the ON or USING clauses of a left join disqualifies the expression
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 ){
93348 ** statements into a NULL when parsing the CREATE statement text out
93350 pExpr->op = TK_NULL;
93351 }else if( pWalker->eCode==4 ){
93352 /* A bound parameter in a CREATE statement that originates from
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
93388 ** a constant.
93395 ** Walk an expression tree. Return non-zero if the expression is constant
93396 ** that does no originate from the ON or USING clauses of a join.
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
93448 ** This routine is used to determine if a term of the HAVING clause can
93449 ** be promoted into the WHERE clause. In order for such a promotion to work,
93451 ** a "group". The requirement that the GROUP BY term must be BINARY
93452 ** assumes that no other collating sequence will have a finer-grained
93453 ** grouping than binary. In other words (A=B COLLATE binary) implies
93454 ** A=B in every other collating sequence. The requirement that the
93474 ** Walk an expression tree. Return non-zero if the expression is constant
93475 ** or a function call with constant arguments. Return and 0 if there
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
93480 ** a constant.
93489 ** Walk an expression tree. Return 1 if the expression contains a
93506 ** If the expression p codes a constant integer that is small enough
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;
93550 ** when we know that a value cannot be NULL. Hence, a false positive
93552 ** be a small performance hit but is otherwise harmless. On the other
93553 ** hand, a false negative (returning FALSE when the result could be NULL)
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);
93578 ** Return TRUE if the given expression is a constant which would be
93583 ** can be omitted. When in doubt return FALSE. A false negative
93584 ** is harmless. A false positive, however, can result in the wrong
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.
93628 ** pX is the RHS of an IN operator. If pX is a SELECT statement
93629 ** that can be simplified to a direct table access, then return
93630 ** a pointer to the SELECT statement. If pX is not a SELECT statement,
93631 ** or if the SELECT statement needs to be manifested into a transient
93641 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
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 */
93661 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
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
93695 ** The argument is an IN operator with a list (not a subquery) on the
93696 ** right-hand side. Return TRUE if that list is constant.
93702 pLHS = pIn->pLeft;
93703 pIn->pLeft = 0;
93705 pIn->pLeft = pLHS;
93713 ** might be either a list of expressions or a subquery.
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
93730 ** implemented as a sequence of comparisons.
93732 ** An existing b-tree might be used if the RHS expression pX is a simple
93737 ** If the RHS of the IN operator is a list or a more complex subquery, then
93739 ** pX->iTable made to point to the ephemeral table instead of an
93744 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
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
93753 ** a UNIQUE constraint or index.
93755 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
93757 ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
93758 ** index can be found with the specified <columns> as its left-most.
93761 ** if the RHS of the IN operator is a list (not a subquery) then this
93762 ** routine might decide that creating an ephemeral b-tree for membership
93764 ** calling routine should implement the IN operator using a sequence
93767 ** When the b-tree is being used for membership tests, the calling function
93769 ** contains a NULL. If prRhsHasNull is not a NULL pointer and
93770 ** if there is any chance that the (...) might contain a NULL value at
93771 ** runtime, then a register is allocated and the register number written
93772 ** to *prRhsHasNull. If there is no chance that the (...) contains a
93775 ** If a register is allocated and its location stored in *prRhsHasNull, then
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
93786 ** (?,?,?) IN (SELECT a, b, c FROM t1)
93787 ** CREATE INDEX i1 ON t1(b, c, a);
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 );
93808 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
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 ){
93825 ** satisfy the query. This is preferable to generating a new
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;
93962 ** and the RHS of the IN operator is a list, not a subquery
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);
94026 char a = sqlite3ExprAffinity(pA);
94028 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
94030 zRet[i] = a;
94044 ** "sub-select returns N columns - expected M"
94047 const char *zFmt = "sub-select returns %d columns - expected %d";
94053 ** Expression pExpr is a vector that has been used in a context where
94054 ** it is not permitted. If pExpr is a sub-select vector, this routine
94055 ** loads the Parse object with a message of the form:
94057 ** "sub-select returns N columns - expected 1"
94059 ** Or, if it is a regular scalar vector:
94065 if( pExpr->flags & EP_xIsSelect ){
94066 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
94075 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
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
94087 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
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
94093 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
94096 ** value to non-NULL if the RHS is NULL-free.
94098 ** For a SELECT or EXISTS operator, return the register that holds the
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
94108 int isRowid /* If true, LHS of IN operator is a rowid */
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
94121 ** * We are inside a trigger
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 */
94156 ** If the 'x' expression is a column value, or the SELECT...
94157 ** statement returns a column value, then the affinity of that
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) ){
94210 ** store it in the temporary table. If <expr> is a column, then use
94212 ** a column, use numeric affinity.
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);
94279 ** For a SELECT, generate code to put the values for all columns of
94284 ** into a register and return that register number.
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
94371 ** subquery. If the RHS is a subquery, the number of result columns must
94373 ** a list of values, the LHS must be a scalar.
94385 ** See the separate in-operator.md documentation file in the canonical
94408 int destNotNull; /* Jump here if a comparison is not true in step 6 */
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. */
94444 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
94450 ** aiMap[] array contains a mapping from the original LHS field order to
94468 ** suitable for evaluating the IN operator, then evaluate using a
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, ®ToFree);
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
94531 ** of the RHS using the LHS as a probe. If found, the result is
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
94537 ** into a single opcode. */
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
94571 ** For a scalar LHS, it is sufficient to check just the first row
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];
94697 ** Record in the column cache that a particular column from a
94698 ** particular table is stored in a particular register.
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 ){
94805 ** When a cached column is reused, make sure that its register is
94806 ** no longer available as a temp register. ticket #3879: that same
94813 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94814 if( p->iReg==iReg ){
94815 p->tempReg = 0;
94820 /* Generate code that will load into register regOut a value that is
94821 ** appropriate for the iIdxCol-th column of index pIdx.
94826 int iTabCur, /* Cursor pointing to a table row */
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
94874 ** table pTab and store the column value in a register.
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);
94969 ** and does not appear in a normal build.
94974 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
94975 int r = p->iReg;
94984 ** Convert a scalar expression node to a TK_REGISTER referencing
94989 p->op2 = p->op;
94990 p->op = TK_REGISTER;
94991 p->iTable = iReg;
94996 ** Evaluate an expression (either a vector or a scalar expression) and store
95000 ** If the returned result register is a temporary scalar, then also write
95002 ** is not a temporary or if the expression is a vector set *piFreeable
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, ®Free2);
95177 codeCompare(pParse, pLeft, pExpr->pRight, op,
95213 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
95214 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
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, ®Free2);
95247 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
95258 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
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 */
95286 CollSeq *pColl = 0; /* A collating sequence */
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);
95342 /* The AFFINITY() function evaluates to a string that describes
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;
95374 /* For length() and typeof() functions with a column argument,
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);
95402 ** a virtual table column.
95406 ** see if it is a column in a virtual table. This is done because
95409 ** function. The expression "A glob B" is equivalent to
95410 ** "glob(B,A). We want to use the A in "A glob B" to test
95411 ** for function overloading. But we use the B term in "glob(B,A)".
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);
95494 /* If the opcode is TK_TRIGGER, then the expression is a reference
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
95504 ** i is the index of the column. For a new.rowid reference, p1 is
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
95511 ** CREATE TABLE t1(a, b);
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);
95566 ** Form A:
95572 ** Form A is can be transformed into the equivalent form B as follows:
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].
95595 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
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;
95722 ** into a register. Return the register number where the results
95725 ** If the register is a temporary register that can be deallocated,
95727 ** a temporary, then set *pReg to zero.
95729 ** If pExpr is a constant, then this routine might generate this
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);
95776 ** Make a transient copy of expression pExpr and then code it using
95781 sqlite3 *db = pParse->db;
95783 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
95794 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
95805 ** Also make a copy of the expression results into another "cache" register
95807 ** the result is a copy of the cache register.
95814 Vdbe *v = pParse->pVdbe;
95818 assert( pExpr->op!=TK_REGISTER );
95820 iMem = ++pParse->nMem;
95827 ** expression list into a sequence of registers beginning at target.
95838 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
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++;
95888 ** Generate code for a BETWEEN operator.
95926 exprX = *pExpr->pLeft;
95932 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
95935 compRight.pRight = pExpr->x.pList->a[1].pExpr;
95940 /* Mark the expression is being from the ON or USING clause of a join
95942 ** it into the Parse.pConstExpr list. We should use a new bit for this,
95963 ** Generate code for a boolean expression such that a jump is made
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, ®Free1);
96027 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
96028 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
96048 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
96075 /* No-op */
96091 ** Generate code for a boolean expression such that a jump is made
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, ®Free1);
96181 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
96182 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
96200 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
96229 /* no-op */
96245 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
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.
96266 ** Additionally, if pExpr is a simple SQL value and the value is the
96267 ** same as that currently bound to variable pVar, non-zero is returned.
96268 ** Otherwise, if the values are not the same or if pExpr is not a simple
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 */
96295 ** Do a deep comparison of two expression trees. Return 0 if the two
96297 ** by a COLLATE operator at the top level. Return 2 if there are differences
96298 ** other than the top-level COLLATE operator.
96300 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
96310 ** expressions are the same. But if you get a 0 or 1 return, then you
96312 ** this routine is used, it does not hurt to get an extra 2 - that
96314 ** an incorrect 0 or 1 could lead to a malfunction.
96317 ** pParse->pReprepare can be matched against literals in pB. The
96318 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
96321 ** pB causes a return value of 2.
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
96379 ** a malfunction will result.
96381 ** Two NULL pointers are considered to be the same. But a NULL pointer
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
96423 ** Expr.iTable<0 then assume a table number given by iTab.
96426 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
96430 ** When in doubt, return false. Returning true might give a performance
96431 ** improvement. Returning false might cause a performance reduction, but
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;
96455 ** index only, without having to do a search for the corresponding
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;
96514 SrcList *pSrc; /* One particular FROM clause in a nested query */
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
96567 ** the new element. Return a negative number if malloc fails.
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
96583 ** the new element. Return a negative number if malloc fails.
96587 pInfo->aFunc = sqlite3ArrayAllocate(
96589 pInfo->aFunc,
96590 sizeof(pInfo->aFunc[0]),
96591 &pInfo->nFunc,
96598 ** This is the xExprCallback for a tree walker. It is used to
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 ){
96622 /* If we reach this point, it means that pExpr refers to a table
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
96666 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
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
96683 /* Check to see if pExpr is a duplicate of another aggregate
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);
96774 ** Allocate a single new register for use to hold some intermediate result.
96777 if( pParse->nTempReg==0 ){
96778 return ++pParse->nMem;
96780 return pParse->aTempReg[--pParse->nTempReg];
96784 ** Deallocate a register, making available for reuse for some other
96787 ** If a register is currently being used by the column cache, then
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;
96806 ** Allocate or deallocate a block of nReg consecutive registers.
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 ){
96872 ** a legal notice, here is a blessing:
96893 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
96894 ** CREATE INDEX command. The second is a table name. The table name in
96898 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
96899 ** -> 'CREATE TABLE def(a, b, c)'
96901 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
96902 ** -> 'CREATE INDEX i ON def(a, b, c)'
96923 ** statement is that the table name is the first non-space token that
96924 ** is immediately followed by a TK_LP or TK_USING token.
96947 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
96949 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
96965 ** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
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);
97020 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
97021 ** statement. The second is a table name. The table name in the CREATE
97071 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
97075 ** Note that ON cannot be a database, table or column name, so
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
97116 ** If argument zWhere is NULL, then a pointer string containing the text
97140 ** Generate the text of a WHERE expression which can be used to select all
97148 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
97149 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
97156 ** Generate the text of a WHERE expression which can be used to select all
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);
97237 ** Parameter zName is the name of a table that is about to be altered
97239 ** If the table is a system table, this function leaves an error message
97240 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
97242 ** Or, if zName is not a system table, zero is returned.
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;
97286 /* Get a NULL terminated version of the new table name. */
97290 /* Check that a table or index named 'zName' does not already exist
97299 /* Make sure it is not a system table being altered, or a reserved name
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 ){
97335 /* Begin a transaction for database iDb.
97337 ** schema). Open a statement transaction if the table is a virtual
97347 /* If this is a virtual table, invoke the xRename() function if
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) ){
97495 /* If the default value for the new column was specified with a
97499 assert( pDflt==0 || pDflt->op==TK_SPAN );
97500 if( pDflt && pDflt->pLeft->op==TK_NULL ){
97505 ** If there is a NOT NULL constraint, then the default value for the
97508 if( pCol->colFlags & COLFLAG_PRIMKEY ){
97509 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
97512 if( pNew->pIndex ){
97513 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
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 ){
97523 "Cannot add a NOT NULL column with default value NULL");
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.
97588 ** This routine makes a (partial) copy of the Table structure
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]);
97621 /* Make sure this is not an attempt to ALTER a view. */
97622 if( pTab->pSelect ){
97623 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
97626 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
97630 assert( pTab->addColOffset>0 );
97631 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97633 /* Put a copy of the Table struct in Parse.pNewTable for the
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;
97665 /* Begin a transaction and increment the schema cookie. */
97680 ** 2005-07-08
97683 ** a legal notice, here is a blessing:
97710 ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
97724 ** a string consisting of a list of integers. The first integer in this
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
97737 ** must be separated from the last integer by a single space. If the
97739 ** the index is unordered and will not use the index for a range query.
97742 ** column contains a single integer which is the (estimated) number of
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
97773 ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
97787 ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
97788 ** binary encoding of a key from the index. The nEq column is a
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
97815 ** of a blob encoding of the complete index key as is found in
97817 ** all contain just a single integer which is the same as the first
97841 ** appropriate compile-time options are provided.
97845 ** Argument zWhere may be a pointer to a buffer containing a table name,
97846 ** or it may be a NULL pointer. If it is not NULL, then all entries in
97874 sqlite3 *db = pParse->db;
97883 pDb = &db->aDb[iDb];
97891 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
97893 /* The sqlite_statN table does not exist. Create it. Note that a
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() -
97955 u8 isPSample; /* True if a periodic sample */
97962 tRowcnt nPSample; /* How often to do a periodic sample */
97966 Stat4Sample current; /* Current row as a Stat4Sample */
97967 u32 iPrn; /* Pseudo-random number used for sampling */
97969 int iMin; /* Index in a[] of entry with minimum score */
97971 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
97973 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
97977 /* Reclaim memory used by a Stat4Sample
97982 if( p->nRowid ){
97983 sqlite3DbFree(db, p->u.aRowid);
97984 p->nRowid = 0;
97989 /* Initialize the BLOB value of a ROWID
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;
98005 /* Initialize the INTEGER value of a ROWID.
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);
98037 ** Reclaim all memory of a Stat4Accum structure.
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);
98057 ** Note 1: In the special case of the covering index that implements a
98066 ** original WITHOUT ROWID table as N==K as a special case.
98069 ** value is a pointer to the Stat4Accum object. The datatype of the
98070 ** return value is BLOB, but it is really just a pointer to the Stat4Accum
98103 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
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]);
98132 /* Set up the Stat4Accum.a[] and aBest[] arrays */
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;
98149 /* Return a pointer to the allocated object to the caller. Note that
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.
98237 ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
98240 if( nEqZero>p->nMaxEqZero ){
98241 p->nMaxEqZero = nEqZero;
98243 if( pNew->isPSample==0 ){
98245 assert( pNew->anEq[pNew->iCol]>0 );
98249 ** added a sample that shares this prefix, there is no need to add
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
98331 ** into IndexSample.a[] at this point. */
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];
98364 /* Check if this is to be a periodic sample. If so, add it. */
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
98392 ** R Rowid for the current row. Might be a key record for
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];
98453 /* Check if this is to be a periodic sample. If so, add it. */
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);
98492 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
98497 ** inserted as part of a manually constructed bytecode program. (See
98499 ** parameter will always be a poiner to a Stat4Accum object, never a
98504 ** a one-parameter function, stat_get(P), that always returns the
98514 /* STAT3 and STAT4 have a parameter on this routine. */
98529 ** The value is a string composed of a list of integers describing
98533 ** the number of rows matched by a stabbing query on the index using
98534 ** a key with the corresponding number of fields. In other words,
98535 ** if the index is on columns (a,b) and the sqlite_stat1 value is
98539 ** * "WHERE a=?" matches 10 rows, and
98540 ** * "WHERE a=? AND b=?" matches 2 rows.
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);
98649 ** a single table.
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():
98771 ** the regPrev array and a trailing rowid (the rowid slot is required
98772 ** when building a record to insert into the sample column of
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));
98785 ** (or for a WITHOUT ROWID table, the number of PK columns),
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 */
98955 /* Create a single sqlite_stat1 entry containing NULL as the index
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)){
99010 ** Generate code that will do an analysis of a single table in
99011 ** a database. If pOnlyIdx is not NULL then it is a single index
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;
99210 ** the old data with the new instead of allocating a new array. */
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;
99323 ** Look up an index by name. Or, if the name of a WITHOUT ROWID table
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);
99452 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
99456 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
99457 ** a buffer overread. */
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;
99597 ** a legal notice, here is a blessing:
99611 ** is slightly different from resolving a normal SQL expression, because simple
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
99849 ** three arguments to the function come directly from a detach statement:
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);
99904 ** This procedure generates VDBE code for a single invocation of either the
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));
99977 ** Called by the parser to compile a DETACH statement.
100016 ** Initialize a DbFixer structure. This routine must be called prior
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);
100040 ** a specific database to all table references where the database name
100042 ** must have been initialized by a prior call to sqlite3FixInit().
100045 ** view in one database does not refer to objects in a different database.
100047 ** allowed to refer to anything.) If a reference is explicitly made
100048 ** to an object in a different database, an error message is added to
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;
100184 ** a legal notice, here is a blessing:
100194 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
100199 ** All of the code in this file may be omitted by defining a single
100210 ** is a copy of the 3rd argument to this routine. The second argument
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;
100313 ** The pExpr should be a TK_COLUMN expression. The table referred to
100314 ** is in pTabList or else it is the NEW or OLD table of a trigger.
100318 ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
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);
100337 /* An attempt to read a column out of a subquery or other
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
100404 ** parameters can be either NULL or a string. */
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;
100460 ** a legal notice, here is a blessing:
100490 u8 isWriteLock; /* True for write lock. False for a read lock */
100495 ** Record the fact that we want to lock a table at run-time.
100498 ** A read or a write lock can be taken depending on isWritelock.
100501 ** code to make the lock occur is generated by a later call to
100508 u8 isWriteLock, /* True for a write lock */
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
100578 ** This routine is called after a single SQL statement has been
100579 ** parsed and a VDBE program to execute that statement has been
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;
100621 ** set for each database that is used. Generate code to start a
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 */
100684 /* A minimum of one cursor is required if autoincrement is used
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 */
100719 return; /* A malloc must have failed */
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 ){
100813 /* If zName is the not the name of a table in the schema created using
100816 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
100818 pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
100821 return pMod->pEpoTab;
100831 pParse->checkSchema = 1;
100841 ** This is a wrapper around sqlite3LocateTable(). The difference between
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
100865 ** a particular index given the name of that index
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);
100998 ** "main" and "temp") for a single database connection.
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;
101016 ** This routine is called when a commit occurs.
101019 db->flags &= ~SQLITE_InternChanges;
101023 ** Delete memory allocated for the column names of a table or view (the
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;
101127 ** Given a token, return a string that consists of the text of that
101142 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
101158 if( p->nTab==0 ){
101159 p->nTab = 1;
101164 ** Parameter zName points to a nul-terminated buffer containing the name
101165 ** of a database ("main", "temp" or the name of an attached db). This
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;
101184 ** The token *pName contains the name of a database (either "main" or
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
101245 ** unqualified name for a new schema object (table, index, view or
101251 if( !pParse->db->init.busy && pParse->nested==0
101252 && (pParse->db->flags & SQLITE_WriteSchema)==0
101261 ** Return the PRIMARY KEY index of a table
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;
101282 ** Begin constructing a new table representation in memory. This is
101284 ** to a CREATE TABLE statement. In particular, this routine is called
101291 ** The new table record is initialized and put in pParse->pNewTable.
101301 int isTemp, /* True if this is a TEMP table */
101302 int isView, /* True if this is a VIEW */
101303 int isVirtual, /* True if this is a VIRTUAL table */
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 ){
101323 /* If creating a temp table, the name may not be qualified. Unless
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;
101406 ** then record a pointer to this table in the main database structure
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. */
101487 /* Set properties of a table column based on the (magical)
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;
101502 ** Add a new column to the table currently being constructed.
101505 ** in a CREATE TABLE statement. sqlite3StartTable() gets called
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;
101568 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
101569 ** been seen on a column. This routine sets the notNull flag on
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 ** --------------------------------
101614 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
101626 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
101629 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
101654 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
101660 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
101675 ** parsing a CREATE TABLE statement.
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);
101688 /* A copy of pExpr is used instead of the original, as pExpr contains
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);
101713 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
101718 ** the expression given in its argument from a TK_STRING into a TK_ID
101719 ** if the expression is just a TK_STRING with an optional COLLATE clause.
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;
101732 ** Designate the PRIMARY KEY for the table. pList is a list of names
101736 ** A table can have at most one primary key. If the table already has
101737 ** a primary key (and this is the second primary key) then create an
101740 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
101743 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
101746 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
101752 int onError, /* What to do with a uniqueness conflict */
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);
101817 ** Add a new CHECK constraint to the table currently under construction.
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;
101882 ** request it. If the collation factory does not supply such a sequence,
101890 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
101897 sqlite3 *db = pParse->db;
101899 u8 initbusy = db->init.busy;
101903 if( !initbusy && (!pColl || !pColl->xCmp) ){
101916 ** changes. When a process first reads the schema it records the
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);
101955 ** The first parameter is a pointer to an output buffer. The second
101956 ** parameter is a pointer to an integer that contains the offset at
101958 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
101962 ** If the string zSignedIdent consists entirely of alpha-numeric
101963 ** characters, does not begin with a digit and is not an SQL keyword,
101965 ** it is quoted using double-quotes.
101991 ** Generate a CREATE TABLE statement appropriate for the given
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;
102085 ** Estimate the total row width for a table.
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);
102099 ** Estimate the average size of a row for an index.
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;
102121 ** This routine runs at the end of parsing a CREATE TABLE statement that
102122 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
102124 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
102129 ** no rowid btree for a WITHOUT ROWID. Instead, the canonical
102130 ** data storage is a covering index btree.
102137 ** so that the PRIMARY KEY is a covering index. The surplus
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);
102177 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
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;
102198 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
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;
102217 ** code for a CREATE TABLE (not when parsing one as part of reading
102218 ** a database schema). */
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++;
102237 /* This index is a superset of the primary key */
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;
102274 ** a CREATE TABLE statement.
102281 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
102288 ** was called to create a table generated from a
102297 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
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){
102358 /* If not initializing, then create a record for the new table
102361 ** If this is a TEMPORARY table, write the entry into the auxiliary
102364 if( !db->init.busy ){
102379 if( p->pSelect==0 ){
102380 /* A regular table */
102385 /* A view */
102391 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
102392 ** statement to populate the new table. The root-page number for the
102393 ** new table is in register pParse->regRoot.
102395 ** Once the SELECT has been coded by sqlite3Select(), it is in a
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 */
102408 int regRec; /* A record to be insert into the new table */
102411 Table *pSelTab; /* A table that describes the SELECT results */
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
102459 /* A slot for the record has already been allocated in the
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);
102532 ** The parser calls this routine in order to create a new VIEW
102540 Select *pSelect, /* A SELECT statement that will become the new view */
102541 int isTemp, /* TRUE for a TEMPORARY view */
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);
102565 /* Make a copy of the entire SELECT statement that defines the view.
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];
102602 ** The Table structure pTable is really a VIEW. Fill in the names of
102604 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
102607 Table *pSelTab; /* A fake table from which we get the result set */
102611 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
102626 /* A positive nCol means the columns names for this view are
102629 if( pTable->nCol>0 ) return 0;
102631 /* A negative nCol is a special marker meaning that we are currently
102633 ** a negative nCol, it means two or more views form a loop, like this:
102642 ** CREATE TABLE main.ex1(a);
102643 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
102646 if( pTable->nCol<0 ){
102647 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
102650 assert( pTable->nCol>=0 );
102656 ** to be permanent. So the computation is done on a copy of the SELECT
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
102680 ** a VIEW it holds the list of column names.
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;
102735 # define sqliteViewResetAll(A,B)
102740 ** used by SQLite when the btree layer moves a table root page. The
102741 ** root-page of a table or index in database iDb has changed from iFrom
102752 ** rootpage==iFrom have been converted to have a rootpage of iTo
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
102798 ** The "#NNN" in the SQL is a special constant that means whatever value
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
102835 ** and root page 5 happened to be the largest root-page number in the
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 );
102871 ** after a DROP INDEX or DROP TABLE command.
102880 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
102884 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
102894 ** Generate code to drop a table.
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
102940 ** every row that refers to a table of the same name as the one being
102941 ** dropped. Triggers are handled separately because a trigger can be
102942 ** created in the temp database that refers to a table in another
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);
102964 ** This routine is called to do the work of a DROP TABLE statement.
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 );
102991 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
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);
103039 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
103040 ** on a table.
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);
103068 ** This routine is called to create a new foreign key on the table
103072 ** the table referred to (a.k.a the "parent" table). pToCol is a list
103078 ** under construction in the pParse->pNewTable field.
103080 ** The foreign key is set for IMMEDIATE processing. A subsequent call
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;
103199 ** clause is seen as part of a foreign key definition. The isDeferred
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;
103216 ** used to initialize a newly created index or to recompute the
103217 ** content of an index in response to a REINDEX command.
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 */
103233 int iPartIdxLabel; /* Jump to this label to skip a row */
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;
103341 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
103343 ** be NULL for a primary key or an index that is created to satisfy a
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
103346 ** currently being constructed by a CREATE TABLE statement.
103348 ** pList is a list of columns to be indexed. pList will be NULL if this
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 */
103357 ExprList *pList, /* A list of columns to be indexed */
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 );
103408 ** is a temp table. If so, set the database to 1. Do not do this
103409 ** if initialising a database schema.
103411 if( !db->init.busy ){
103413 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
103421 /* Because the parser constructs pTblName from a single identifier,
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 ){
103474 ** one of the index names collides with the name of a temporary table or
103478 ** dealing with a primary key or UNIQUE constraint. We have to invent our
103484 assert( pName->z!=0 );
103488 if( !db->init.busy ){
103490 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
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) ){
103536 /* If pList==0, it means this routine was called to make a primary
103538 ** So create a fake list to simulate this.
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 */
103601 ** a table column, store that column in aiColumn[]. For general expressions,
103602 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
103604 ** TODO: Issue a warning if two or more columns of the index are identical.
103605 ** TODO: Issue a warning if the table primary key is used as part of the
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);
103692 ** it as a covering index */
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 ){
103706 /* This routine has been called to create an automatic index as a
103707 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
103708 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
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 ){
103746 /* This constraint creates the same index as a previous
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;
103789 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
103794 ** of a WITHOUT ROWID table.
103797 ** or UNIQUE index in a CREATE TABLE statement. Since the table
103804 int iMem = ++pParse->nMem;
103812 ** doing so, code a Noop instruction and store its address in
103813 ** Index.tnum. This is required in case this index is actually a
103814 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
103816 ** the Noop with a Goto to jump over the VDBE code generated below. */
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--;
103826 /* A named index with an explicit CREATE INDEX statement */
103828 onError==OE_None ? "" : " UNIQUE", n, pName->z);
103830 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
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);
103861 /* When adding an index to the list of indices for a table, make
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 );
103922 ** for a partial index. But do not let the estimate drop below 10. */
103923 a[0] = pIdx->pTable->nRowLogEst;
103924 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
103925 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
103927 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
103929 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
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);
104007 ** pArray is a pointer to an array of objects. Each object in the
104009 ** to extend the array so that there is space for a new object at the end.
104012 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
104017 ** reflect the new size of the array and a pointer to the new allocation
104020 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
104021 ** unchanged and a copy of pArray returned.
104028 int *pIdx /* Write the index of a new slot here */
104032 if( (n & (n-1))==0 ){
104036 *pIdx = -1;
104049 ** Append a new element to the given IdList. Create a new IdList if
104052 ** A new IdList is returned, or NULL if malloc() fails.
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;
104106 ** For example, suppose a SrcList initially contains two entries: A,B.
104111 ** After the call above it would contain: A, B, nil, nil, nil.
104113 ** would have been: A, nil, nil, nil, B. To prepend the new slots,
104115 ** be: nil, nil, nil, A, B.
104117 ** If a memory allocation fails the SrcList is unchanged. The
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;
104163 /* Return a pointer to the enlarged SrcList */
104169 ** Append a new table name to the given SrcList. Create a new SrcList if
104170 ** need be. A new entry is created in the SrcList even if pTable is NULL.
104172 ** A SrcList is returned, or NULL if there is an OOM error. The returned
104174 ** a new one. If an OOM error does occurs, then the prior value of pList
104180 ** The SrcList.a[].zName field is filled with the table name which might
104182 ** SrcList.a[].zDatabase is filled with the database name from pTable,
104187 ** sqlite3SrcListAppend(D,A,B,0);
104189 ** Then B is a table name and the database name is unspecified. If called
104192 ** sqlite3SrcListAppend(D,A,B,C);
104195 ** then so is B. In other words, we never have a case where:
104197 ** sqlite3SrcListAppend(D,A,0,C);
104204 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
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;
104240 ** Assign VdbeCursor index numbers to all tables in a SrcList
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);
104279 ** This routine is called by the parser to add a new term to the
104280 ** end of a growing FROM clause. The "p" parameter is the part of
104284 ** pDatabase is NULL if the database name qualifier is missing - the
104286 ** alias token. If the term is a subquery, then pSubquery is the
104291 ** Return a new SrcList which encodes is the FROM with the new
104299 Token *pAlias, /* The right-hand side of the AS subexpression */
104300 Select *pSubquery, /* A subquery used in place of a table name */
104301 Expr *pOn, /* The ON clause of a join */
104302 IdList *pUsing /* The USING clause of a join */
104305 sqlite3 *db = pParse->db;
104307 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
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 ){
104346 /* A "NOT INDEXED" clause was supplied. See parse.y
104348 pItem->fg.notIndexed = 1;
104350 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
104351 pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0);
104357 ** Add the list of function arguments to the SrcList entry for a
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);
104374 ** When building up a FROM clause in the parser, the join operator
104382 ** A natural cross join B
104384 ** The operator is "natural cross join". The A and B operands are stored
104385 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
104386 ** operator with A. This routine shifts that operator over to B.
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;
104399 ** Generate VDBE code for a BEGIN statement.
104407 db = pParse->db;
104415 for(i=0; i<db->nDb; i++){
104424 ** Generate VDBE code for a COMMIT or ROLLBACK statement.
104426 ** code is generated for a COMMIT.
104433 assert( pParse->db!=0 );
104447 ** This function is called by the parser when it parses a command to create,
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);
104484 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
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)) ){
104539 ** This routine starts a new transaction if we are not already within
104540 ** a transaction. If we are already within a transaction, then a checkpoint
104541 ** is set if the setStatement parameter is true. A checkpoint should
104542 ** be set for operations that might fail (due to a constraint) part of
104546 ** necessary to undo a write and the checkpoint should not be set.
104551 DbMaskSet(pToplevel->writeMask, iDb);
104552 pToplevel->isMultiWrite |= setStatement;
104558 ** inserting multiple rows in a table, or inserting a row and index entries.)
104564 pToplevel->isMultiWrite = 1;
104569 ** possible to abort a statement prior to completion. In order to
104571 ** sure that the statement is protected by a statement transaction.
104574 ** isMultiWrite flag was previously set. There is a time dependency
104577 ** go a little faster. But taking advantage of this time dependency
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);
104711 Db *pDb; /* A single database */
104713 sqlite3 *db = pParse->db; /* The database connection */
104715 Table *pTab; /* A table in the database */
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
104744 char *z; /* Name of a table or index */
104746 Table *pTab; /* A table in the database */
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);
104797 ** Return a KeyInfo structure that is appropriate for the given Index.
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 ){
104831 ** This routine is invoked once per CTE by the parser while parsing a
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);
104904 ** a legal notice, here is a blessing:
104919 ** Invoke the 'collation needed' callback to request a collation sequence
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);
104945 ** This routine is called if the collation factory fails to deliver a
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 */
104969 ** or substituting a collation sequence of a different encoding when the
104988 sqlite3 *db = pParse->db;
104994 if( !p || !p->xCmp ){
105001 if( p && !p->xCmp && synthCollSeq(db, p) ){
105004 assert( !p || p->xCmp );
105012 ** This routine is called on a collation sequence before it is used to
105014 ** a database is loaded that contains references to collation sequences
105018 ** request a definition of the collating sequence. If this doesn't work,
105019 ** an equivalent collating sequence that uses a text encoding different
105023 if( pColl && pColl->xCmp==0 ){
105024 const char *zName = pColl->zName;
105025 sqlite3 *db = pParse->db;
105040 ** true, then create a new entry. Otherwise return NULL.
105044 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
105046 ** Stored immediately after the three collation sequences is a copy of
105047 ** the collation sequence name. A pointer to this string is stored in
105053 int create /* Create a new entry if true */
105056 pColl = sqlite3HashFind(&db->aCollSeq, zName);
105070 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
105072 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
105088 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
105092 ** If the entry specified is not found and 'create' is true, then create a
105095 ** A separate function sqlite3LocateCollSeq() is a wrapper around
105112 pColl = db->pDfltColl;
105116 if( pColl ) pColl += enc-1;
105122 ** matches the request for a function with nArg arguments in a system
105124 ** request is matched. A higher value indicates a better match.
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
105128 ** takes a variable number of arguments.
105130 ** If nArg is -2 that means that we are searching for any function
105131 ** regardless of the number of arguments it uses, so return a positive
105136 ** 0: Not a match.
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
105145 ** a perfect match and any function with xSFunc NULL is
105146 ** a non-match.
105148 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect 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;
105162 /* Give a better score to a function with a specific number of arguments
105164 if( p->nArg==nArg ){
105171 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
105173 }else if( (enc & p->funcFlags & 2)!=0 ){
105181 ** Search a FuncDefHash for a function with the given name. Return
105182 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
105189 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
105190 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
105198 ** Insert a new FuncDef into a FuncDefHash hash table.
105210 assert( zName[0]>='a' && zName[0]<='z' );
105213 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
105214 aDef[i].pNext = pOther->pNext;
105215 pOther->pNext = &aDef[i];
105218 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
105219 sqlite3BuiltinFunctions.a[h] = &aDef[i];
105227 ** Locate a user function given a name, a number of arguments and a flag
105228 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
105232 ** If the createFlag argument is true, then a new (blank) FuncDef
105233 ** structure is created and liked into the "db" structure if 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)
105238 ** case is used to see if zName is a valid function name for some number
105239 ** of arguments. If nArg is -2, then createFlag must be 0.
105241 ** If createFlag is false, then a function with the required name and
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.
105281 ** install a new function. Whatever FuncDef structure is returned it will
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;
105301 ** exact match for the name, number of arguments and encoding, then add a
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) ){
105329 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
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;
105364 ** Find and return the schema associated with a BTree. Create
105365 ** a new one if necessary.
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;
105392 ** a legal notice, here is a blessing:
105405 ** While a SrcList can in general represent multiple tables and subqueries
105406 ** (as in the FROM clause of a SELECT statement) in this case it contains
105407 ** the name of a single table, as one might find in an INSERT, DELETE,
105409 ** return a pointer. Set an error message and return NULL if the table
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++;
105440 /* A table is not writable under the following circumstances:
105442 ** 1) It is a virtual table and no implementation of the xUpdate method
105444 ** 2) It is a system table (i.e. sqlite_master), this call is not
105445 ** part of a nested parse and writable_schema pragma has not
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);
105472 ** Evaluate a view and store its result in an ephemeral table. The
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 */
105529 /* Check that there isn't an ORDER BY without a LIMIT clause.
105536 /* We only need to generate a select expression if there
105537 ** is a limit/offset term to enforce.
105545 /* Generate a select expression tree to enforce the limit/offset
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);
105589 ** Generate code for a DELETE FROM statement.
105591 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
105613 int memCnt = -1; /* Memory cell used for change counting */
105632 int isView; /* True if attempting to delete from a view */
105637 db = pParse->db;
105638 if( pParse->nErr || db->mallocFailed ){
105641 assert( pTabList->nSrc==1 );
105646 ** an SrcList* parameter instead of just a Table* parameter.
105652 ** deleted from is a view
105656 isView = pTab->pSelect!=0;
105667 /* If pTab is really a view, make sure it has been initialized.
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);
105709 /* If we are trying to delete from a view, realize that view into
105731 if( db->flags & SQLITE_CountRows ){
105732 memCnt = ++pParse->nMem;
105737 /* Special case: A DELETE without a WHERE clause deletes everything.
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);
105773 /* For a rowid table, initialize the RowSet to an empty set */
105776 iRowSet = ++pParse->nMem;
105779 /* For a WITHOUT ROWID table, create an ephemeral table used to
105783 nPk = pPk->nKeyCol;
105784 iPk = pParse->nMem+1;
105785 pParse->nMem += nPk;
105786 iEphCur = pParse->nTab++;
105791 /* Construct a query to find the rowid or primary key for every row
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;
105843 nKey = 0; /* Zero tells OP_Found to use a composite key */
105845 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
105849 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
105862 /* Unless this is a view, open cursors for the table we are
105863 ** deleting from and all its indices. If this is a view, then the
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 );
105893 assert( nKey==0 ); /* OP_Found will use a composite key */
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 ){
105942 ** generating code because of a call to sqlite3NestedParse(), do not
105945 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
105969 ** This routine generates VDBE code that causes a single row of a
105977 ** in the case of a rowid table, or the PRIMARY KEY index in the case
105978 ** of a WITHOUT ROWID table.)
105981 ** cursor number iIdxCur+i for the i-th index.
105983 ** 3. The primary key for the row to be deleted must be stored in a
105985 ** that a search record formed from OP_MakeRecord is contained in the
105996 ** of a ONEPASS delete that affects multiple rows. In this case, if
105997 ** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
105999 ** operation. Or, if iIdxNoSeek is not a valid cursor number, the
106003 ** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
106017 u8 count, /* If non-zero, increment the row change counter */
106022 Vdbe *v = pParse->pVdbe; /* Vdbe */
106033 ** (this can happen if a trigger program has already deleted it), do
106043 /* If there are any triggers to fire, allocate a range of registers to
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;
106099 ** a view (in which case the only effect of the DELETE statement is to
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
106104 ** be invoked unless table pTab is a system table. The difference is that
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 ){
106126 ** handle rows (possibly in other tables) that refer via a foreign key
106136 ** trigger programs were invoked. Or if a trigger program throws a
106144 ** index entries associated with a single row of a single table, pTab
106148 ** 1. A read/write cursor "iDataCur" must be open on the canonical storage
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);
106196 ** iCur is the index of a cursor open on the pTab table and pointing to
106197 ** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
106200 ** Return a register number which is the first in a block of
106205 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
106206 ** to that label if pIdx is a partial index that should be skipped.
106208 ** A partial index should be skipped if its WHERE clause evaluates
106209 ** to false or null. If pIdx is not a partial index, *piPartIdxLabel
106213 ** The pPrior and regPrior parameters are used to implement a cache to
106215 ** a pointer to a different index for which an index key has just been
106218 ** a column in common, then the register corresponding to that column already
106220 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
106221 ** on a table with multiple indices, and especially with the ROWID or
106226 Index *pIdx, /* The index for which to generate a key */
106229 int prefixOnly, /* Compute only a unique prefix of the key */
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
106264 ** might be stored in the table as an integer (using a compact
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
106284 ** because it was a partial index, then this routine should be called to
106289 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
106300 ** a legal notice, here is a blessing:
106307 ** This file contains the C-language implementations for many of the SQL
106317 ** Return the collating function associated with a function.
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;
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. */
106460 ** something that can be converted into a number, we have:
106461 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
106462 ** that cannot be converted to a numeric value.
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.
106558 ** If x is a blob, then we count bytes.
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] ){
106766 ** is. We might as well use the "version()" function as a substitute.
106768 #define noopFunc versionFunc /* Substitute function - never called */
106771 ** Implementation of random(). Return a random integer.
106782 /* We need to prevent a random number of 0x8000000000000000
106783 ** (or -9223372036854775808) since when you do abs() of that
106785 ** in a way that is testable, mask the sign bit off of negative
106786 ** values, resulting in a positive value. Then take the
106788 ** therefore be no less than -9223372036854775807.
106790 r = -(r & LARGEST_INT64);
106796 ** Implementation of randomblob(N). Return a random blob
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.
106886 # define sqlite3Utf8Read(A) (*((*A)++))
106887 # define Utf8Read(A) (*(A++))
106889 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
106893 /* The correct SQL-92 behavior is for the LIKE operator to ignore
106894 ** case. Thus 'a' LIKE 'A' would be true. */
106897 ** is case sensitive causing 'a' LIKE 'A' to be false */
106908 ** Compare two UTF-8 strings for equality where the first string is
106909 ** a GLOB or LIKE expression. Return values:
106926 ** With the [...] and [^...] matching, a ']' character can be included
106927 ** in the list by making it the first character after '[' or '^'. A
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 */
106960 ** are also "?" characters, skip those as well, but consume a
106970 if( pInfo->matchSet==0 ){
106974 /* "[...]" immediately follows the "*". We have to do a slow
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 ){
107070 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
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().
107087 ** just a variation of LIKE) gets called. This is used for testing
107097 ** the build-in LIKE operator. The first argument to the function is the
107100 ** A LIKE B
107102 ** is implemented as like(B,A).
107104 ** This same function (with a different compareInfo structure) computes
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);
107200 ** Implementation of the sqlite_source_id() function. The result is a string
107210 /* IMP: R-24470-31136 This function is an SQL wrapper around the
107212 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
107216 ** Implementation of the sqlite_log() function. This is a wrapper around
107218 ** its side-effects.
107244 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
107245 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
107256 ** The result is a string that identifies the compiler options
107268 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
107269 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
107272 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
107276 /* Array for converting from half-bytes (nybbles) into ASCII hex
107280 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
107284 ** Implementation of the QUOTE() function. This function takes a single
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
107378 ** an integer. It constructs a string where each character of the string
107415 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
107419 ** The hex() function. Interpret the argument as a blob. Return
107420 ** a hexadecimal rendering as text.
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 */
107469 ** them A, B, and C. The result is also a string which is derived
107470 ** from A by replacing every occurrence of B with C. The match
107478 const unsigned char *zStr; /* The input string A */
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
107664 ** Compute the soundex encoding of a word.
107666 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
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);
107750 ** An instance of the following structure holds the context of a
107759 u8 approx; /* True if non-integer value was input to the sum */
107767 ** 0.0 in that case. In addition, TOTAL always returns a float where
107768 ** SUM might return an integer if it never encounters a floating point
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
108002 ** pExpr points to an expression which implements a function. If
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
108042 ** a consequence of calling sqlite3_initialize()).
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);
108176 ** a legal notice, here is a blessing:
108193 ** --------------------------
108198 ** statement transaction rolled back. If a
108203 ** Deferred constraints are implemented using a simple counter associated
108204 ** with the database handle. The counter is set to zero each time a
108205 ** database transaction is opened. Each time a statement is executed
108206 ** that causes a foreign key violation, the counter is incremented. Each
108207 ** time a statement is executed that removes an existing violation from
108212 ** * When a commit fails due to a deferred foreign key constraint,
108225 ** the parent table for a match. If none is found increment the
108236 ** search the parent table for a row that corresponds to the
108237 ** deleted row in the child table. If such a row is not found,
108251 ** This simplifies the implementation a bit.
108255 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
108265 ** statement that inserts a single row only (no triggers). In this case,
108266 ** instead of using a counter, an exception is thrown immediately if the
108267 ** INSERT violates a foreign key constraint. This is necessary as such
108268 ** an INSERT does not open a statement transaction.
108270 ** TODO: How should dropping a table be handled? How should renaming a
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 ** -----------------------
108306 ** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
108316 ** A foreign key constraint requires that the key columns in the parent
108317 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
108319 ** search the schema for a unique index on the parent key columns.
108325 ** If the parent key consists of a single column (the foreign key constraint
108326 ** is not a composite foreign key), output variable *paiCol is set to NULL.
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
108339 ** 2) The named parent key columns do exist, but are not subject to a
108343 ** foreign key definition, and the parent table does not have a
108348 ** consists of a different number of columns to the child key in
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
108376 ** Otherwise, for a composite foreign key (more than one column), allocate
108378 ** Non-composite foreign keys do not require the aiCol array.
108385 ** 2) The FK is explicitly mapped to a column declared as INTEGER
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 ){
108401 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
108402 ** of columns. If each indexed column corresponds to a foreign key
108403 ** column of pFKey, then this index is a winner. */
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 */
108429 /* If the index uses a collation sequence that is different from
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);
108465 ** This function is called when a row is inserted into or deleted from the
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 */
108512 ** search for a matching row in the parent table. */
108514 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
108517 for(i=0; i<pFKey->nCol; i++){
108531 ** is no matching parent key. Before using MustBeInt, make a copy of
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 ){
108582 /* The parent key is a composite key that includes the IPK column */
108592 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
108600 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
108601 && !pParse->pToplevel
108602 && !pParse->isMultiWrite
108605 ** one row into the table, raise a constraint immediately instead of
108606 ** incrementing a counter. This is necessary as the VM code is being
108607 ** generated for will not open a statement transaction. */
108612 if( nIncr>0 && pFKey->isDeferred==0 ){
108615 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
108624 ** Return an Expr object that refers to a memory register corresponding
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;
108679 ** This function is called to generate code executed when a row is deleted
108681 ** deferred, when a row is inserted into the same table. When generating
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 ** --------------------------------------------------------------------------
108697 ** throw a "FOREIGN KEY constraint failed" exception.
108703 ** throw a "FOREIGN KEY constraint failed" exception.
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;
108768 ** NOT( $current_a==a AND $current_b==b AND ... )
108772 ** (a,b,...)
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);
108824 ** This function returns a linked list of FKey objects (connected by
108828 ** CREATE TABLE t1(a PRIMARY KEY);
108829 ** CREATE TABLE t2(b REFERENCES t1(a);
108831 ** Calling this function with table "t1" as an argument returns a pointer
108833 ** "t2". Calling this function with "t2" as the argument would return a
108838 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
108842 ** The second argument is a Trigger structure allocated by the
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);
108863 ** to this function contains a single entry guaranteed to resolve to
108868 ** (a) The table is the parent table of a FK constraint, or
108869 ** (b) The table is the child table of a deferred FK constraint and it is
108878 sqlite3 *db = pParse->db;
108879 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
108885 /* Search for a deferred foreign key constraint for which this table
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 ){
108927 ** The second argument points to an FKey object representing a foreign key
108931 ** is zero or greater (if a column is unmodified the corresponding element
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;
108954 ** The second argument points to an FKey object representing a foreign key
108958 ** is zero or greater (if a column is unmodified the corresponding element
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 ){
108991 ** used to code a trigger that is really a "SET NULL" action belonging
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)
109008 ** This function is called when inserting, deleting or updating a row of
109012 ** For a DELETE operation, parameter regOld is passed the index of the
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
109068 /* Find the parent table of this foreign key. Also find a unique index
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;
109081 /* If isIgnoreErrors is true, then a table is being dropped. In this
109082 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
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
109125 ** a cursor to use to search the unique index on the parent key columns
109127 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
109128 pParse->nTab++;
109131 /* A row is being removed from the child table. Search for the parent.
109134 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
109137 /* A row is being added to the child table. If a parent row cannot
109140 ** If this operation is being performed as part of a trigger program
109141 ** that is actually a "SET NULL" action belonging to this very
109153 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
109162 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
109163 && !pParse->pToplevel && !pParse->isMultiWrite
109166 /* Inserting a single row into a parent table cannot cause (or fix)
109172 if( !isIgnoreErrors || db->mallocFailed ) return;
109175 assert( aiCol || pFKey->nCol==1 );
109177 /* Create a SrcList structure containing the child table. We need the
109178 ** child table as a SrcList for sqlite3WhereBegin() */
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];
109193 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
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;
109224 ** This function is called before generating code to update or delete a
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]);
109254 ** This function is called before generating code to update or delete a
109255 ** row contained in table pTab. If the operation is a DELETE, then
109256 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
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 ){
109284 /* A DELETE operation. Foreign key processing is required if the
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.
109317 ** passed a pointer to the list of columns being modified. If it is a
109318 ** DELETE, pChanges is passed a NULL pointer.
109320 ** It returns a pointer to a Trigger structure containing a trigger
109322 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
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);
109521 ** This function is called when deleting or updating a row to implement
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;
109601 ** a legal notice, here is a blessing:
109616 ** (1) acquire a lock for table pTab then
109619 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
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));
109649 ** Return a pointer to the column affinity string associated with index
109650 ** pIdx. A column affinity string has one character for each column in
109654 ** ------------------------------
109655 ** 'A' BLOB
109669 if( !pIdx->zColAff ){
109670 /* The first time a column affinity string for a particular index is
109672 ** a member of the Index structure for subsequent use.
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;
109716 ** A column affinity string has one character per column:
109719 ** ------------------------------
109720 ** 'A' BLOB
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
109758 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
109759 ** run without using a temporary table for the results of the SELECT.
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 );
109800 ** table. (Also return zero when doing a VACUUM since we do not want to
109801 ** update the AUTOINCREMENT counters during a VACUUM.)
109805 ** triggers. A new AutoincInfo structure is created if this is the
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){
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;
109902 ** This routine should be called when the regRowid register holds a
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
109963 # define autoIncBegin(A,B,C) (0)
109964 # define autoIncStep(A,B,C)
109972 Select *pSelect, /* A SELECT statement to use as the data source */
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
109991 ** for a SELECT statement that omits the FROM clause and everything else
109995 ** The code generated follows one of four templates. For a simple
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>" -
110010 ** in other words if the SELECT pulls all columns from a single table
110013 ** schemas, including all the same indices, then a special optimization
110018 ** open a write cursor to <table>
110023 ** open a write cursor on the <table> index
110024 ** open a read cursor on the corresponding <table2> index
110033 ** X <- A
110035 ** A: setup for the SELECT
110041 ** end-coroutine X
110049 ** values from a SELECT but the data is being inserted into a table
110054 ** X <- A
110056 ** A: setup for the SELECT
110062 ** end co-routine R
110077 Select *pSelect, /* A SELECT statement to use as the data source */
110091 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
110114 int isView; /* True if attempting to insert into a view */
110119 db = pParse->db;
110120 if( pParse->nErr || db->mallocFailed ){
110123 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
110125 /* If the Select object is really just a simple VALUES() list with a
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) ){
110154 ** inserted into is a view
110158 isView = pTab->pSelect!=0;
110170 /* If pTab is really a view, make sure it has been initialized.
110171 ** ViewGetColumnNames() is a no-op if pTab is not a view.
110177 /* Cannot insert into a read-only table.
110183 /* Allocate a VDBE
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
110266 ** produces a single row of the SELECT on each invocation. The
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;
110291 ** should be written into a temporary table (template 4). Set to
110295 ** A temp table must be used if the table being updated is also one
110296 ** of the tables being read by the SELECT statement. Also use a
110305 ** and add it to a transient table srcTab. The code generated
110318 srcTab = pParse->nTab++;
110332 /* This is the case if the data for the INSERT is coming from a
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;
110382 /* If this is not a view, open the table and and all indices */
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);
110431 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
110432 ** translated into a unique ID for the row. But on a BEFORE trigger,
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);
110453 /* Cannot have triggers on a virtual table. If it were possible,
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++;
110478 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
110480 ** If this is a real table, attempt conversions as required by the
110489 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
110491 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
110494 /* Compute the content of the next row to insert into a range of
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;
110520 ** to generate a unique primary key value.
110546 for(i=0; i<pTab->nCol; i++){
110548 if( i==pTab->iPKey ){
110549 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
110551 ** in its place. Hence, fill this column with a NULL to avoid
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);
110595 int isReplace; /* Set to true if constraints may cause a replace */
110602 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
110603 ** constraints or (b) there are no triggers and this table is not a
110604 ** parent table in a foreign key constraint. It is safe to set the
110611 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
110621 if( (db->flags & SQLITE_CountRows)!=0 ){
110628 pTab, regData-2-pTab->nCol, onError, endOfLoop);
110632 ** is a SELECT statement.
110649 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
110655 ** generating code because of a call to sqlite3NestedParse(), do not
110658 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
110686 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
110688 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
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
110742 ** The regNewData parameter is the first register in a range that contains
110744 ** pTab->nCol+1 registers in this range. The first register (the one
110746 ** case of a WITHOUT ROWID table. The second register in the range will
110756 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
110763 ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
110771 ** at pTab->pIndex.
110776 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
110777 ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
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.
110782 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
110787 ** --------------- ---------- ----------------------------------------
110789 ** sqlite3_step() returns immediately with a
110793 ** only (do not do a complete rollback) then
110797 ** any FAIL Sqlite3_step() returns immediately with a
110817 ** Or if overrideError==OE_Default, then the pParse->onError parameter
110818 ** is used. Or if pParse->onError==OE_Default then the onError value
110827 int regNewData, /* First register in a range holding values to insert */
110829 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
110832 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
110852 db = pParse->db;
110855 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
110856 nCol = pTab->nCol;
110860 ** pPk index or 1 for a rowid table. In other words, nPkField is the
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;
110962 /* Figure out what action to take in case of a rowid collision */
110963 onError = pTab->keyConf;
110979 /* If the response to a rowid conflict is REPLACE but the response
110985 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110986 if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
110998 /* Generate code that deals with a rowid collision */
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
111030 ** to run without a statement journal if there are no indexes on the
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
111047 ** existing entry and then insert a new one. */
111052 if( pTab->pIndex ){
111054 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
111077 ** This loop also handles the case of the PRIMARY KEY index for a
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;
111103 /* Create a record for this index entry as it should appear after
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);
111132 ** of a WITHOUT ROWID table and there has been no change the
111140 /* Find out what action to take in case there is a uniqueness conflict */
111141 onError = pIdx->onError;
111144 continue; /* pIdx is not a UNIQUE index */
111154 ** (2) The table is a WITHOUT ROWID table
111156 ** (4) No delete triggers need to be fired if there is a conflict
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));
111201 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
111205 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
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;
111295 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
111296 ** A consecutive range of registers starting at regNewData contains the
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 ){
111388 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
111393 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
111396 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
111397 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
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
111472 ** Check to see if index pSrc is compatible as a source of data
111474 ** for a compatible index:
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
111535 ** does a test to see if the destination table is empty and jumps over the
111546 Select *pSelect, /* A SELECT statement to use as the data source */
111550 sqlite3 *db = pParse->db;
111554 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
111563 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
111569 if( pParse->pWith || pSelect->pWith ){
111571 ** attached to it. Proceeding may generate a false "no such table: xxx"
111572 ** error if pSelect reads from a CTE named "xxx". */
111580 return 0; /* tab1 must not be a virtual table */
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 ){
111592 return 0; /* FROM clause cannot contain a subquery */
111594 if( pSelect->pWhere ){
111595 return 0; /* SELECT may not have a WHERE clause */
111597 if( pSelect->pOrderBy ){
111600 /* Do not need to test for a HAVING clause. If HAVING is present but
111602 if( pSelect->pGroupBy ){
111603 return 0; /* SELECT may not have a GROUP BY clause */
111605 if( pSelect->pLimit ){
111606 return 0; /* SELECT may not have a LIMIT clause */
111608 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
111609 if( pSelect->pPrior ){
111610 return 0; /* SELECT may not be a compound query */
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;
111632 return 0; /* FROM clause does not contain a real table */
111642 return 0; /* tab2 must not be a virtual table */
111645 if( pSrc->pSelect ){
111646 return 0; /* tab2 may not be a view */
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 ){
111718 ** least a possibility, though it might only work if the destination
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) */
111751 ** (2) The destination has a unique index. (The xfer optimization
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 ){
111811 /* This INSERT command is part of a VACUUM operation, which guarantees
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
111820 ** If any of the indexed columns use a collation sequence other than
111822 ** might change the definition of a collation sequence and then run
111823 ** a VACUUM command. In that case keys may not be written in strictly
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 ){
111865 ** a legal notice, here is a blessing:
111885 ** If the SQL is a query, then for each row in the query result
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);
112010 ** a legal notice, here is a blessing:
112030 ** a legal notice, here is a blessing:
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
112589 /* This case when the file really is being compiled as a loadable
112598 # define SQLITE_EXTENSION_INIT1 /*no-op*/
112600 # define SQLITE_EXTENSION_INIT3 /*no-op*/
112612 ** excluded from a build of SQLite. Substitute a NULL pointer
112706 ** A pointer to this structure is passed into extensions when they are
112856 ** a library that is new enough to support that API.
113025 ** zFile. The entry point is zProc. zProc may be 0 in which case a
113041 sqlite3_vfs *pVfs = db->pVfs;
113066 /* Ticket #1863. To avoid a creating security problems for older
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
113250 ** Register a statically linked extension that is automatically
113292 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
113295 ** routine is a no-op.
113310 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
113312 wsdAutoext.nExt--;
113355 /* Common case: early out without every having to acquire a mutex */
113392 ** a legal notice, here is a blessing:
113415 ** lexicographical order to facility a binary search of the pragma name.
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
113544 /* Definitions of all built-in pragmas */
114074 ** Interpret the given string as a safety level. Return 0 for OFF,
114107 ** Interpret the given string as a boolean value.
114120 ** Interpret the given string as a locking mode value.
114132 ** Interpret the given string as an auto-vacuum mode value.
114149 ** Interpret the given string as a temp db location. Return 1 for file
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) ){
114176 "from within a transaction");
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;
114206 ** Set result column names for a pragma.
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++){
114225 ** Generate code to return a single integer value.
114233 ** Generate code to return a single text value.
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.
114319 ** Locate a pragma in the aPragmaName[] array.
114324 upr = ArraySize(aPragmaName)-1;
114330 upr = mid - 1;
114341 ** Generate code to output a single-column result row with the result
114355 ** Process a pragma statement.
114361 ** The identifier might also be a string. The value is a string, and
114362 ** identifier, or a number. If minusFlag is true, then the value is
114363 ** a number that was preceded by a minus sign.
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 ){
114487 ** Older versions of SQLite would set the default cache size to a
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 ){
114626 /* Simple "PRAGMA locking_mode;" statement. This is a query for
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);
114673 /* If there is no "=MODE" part of the pragma, do a query for the
114684 ** then do a query */
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;
114780 ** Do N steps of incremental vacuuming on a database.
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);
114831 ** current cache_size. The third form sets a spill size that
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);
114935 ** the value sets a specific directory to be used for temporary files.
114936 ** Setting to a null string reverts to the default temporary directory search.
114947 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
114949 sqlite3ErrorMsg(pParse, "not a writable directory");
114954 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
114955 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
114976 ** the value sets a specific directory to be used for database files that
114977 ** were specified with a relative pathname. Setting to a null string reverts
114979 ** a relative path will probably be based on the current directory for the
114991 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
114993 sqlite3ErrorMsg(pParse, "not a writable directory");
115015 ** the value sets a specific file to be used for database access locks.
115020 Pager *pPager = sqlite3BtreePager(pDb->pBt);
115027 Pager *pPager = sqlite3BtreePager(pDb->pBt);
115057 returnSingleInt(v, pDb->safety_level-1);
115059 if( !db->autoCommit ){
115061 "Safety level may not be changed inside a transaction");
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
115102 ** compiled SQL statements after modifying a pragma value.
115115 ** Return a single row for each column of the named table. The columns of
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);
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;
115349 FKey *pFK; /* A foreign key constraint */
115357 int regResult; /* 3 registers to hold a result row */
115359 int regRow; /* Registers to hold a row from pTab */
115360 int addrTop; /* Top of a loop checking foreign keys */
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;
115425 /* Generate code to query the parent index for a matching parent
115426 ** key. If a match is found, jump to addrOk. */
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);
115677 ** or (2) the next entry has a different key */
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
115751 ** encoding that will be used for the main database file if a new 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
115834 ** creating or dropping a table or index). The schema version is used by
115835 ** SQLite each time a query is executed to ensure that the internal cache
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;
115930 ** Configure a database connection to automatically checkpoint a database
115939 db->xWalCallback==sqlite3WalDefaultHook ?
115940 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
115948 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
115970 ** The optional argument is a bitmask of optimizations to perform:
115990 ** ever added that should be off by default, those off-by-default
115995 ** In the current implementation, a table is analyzed if only if all of
116000 ** (2) The query planner used sqlite_stat1-style statistics for one or
116013 int iTabCur; /* Cursor for a table whose size needs checking */
116014 HashElem *k; /* Loop over tables of a schema */
116016 Table *pTab; /* A table in the schema */
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)){
116036 /* If table pTab has not been used in a way that would benefit from
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);
116075 ** if one is set. If no busy handler or a different busy handler is set
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 ){
116215 ** Implementation of an eponymous virtual table that runs a pragma.
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;
116306 /* Figure out the best index to use to search a pragma virtual table.
116310 ** possible, and especially on the first hidden parameter. So return a
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;
116349 /* Create a new cursor for the pragma virtual table */
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;
116371 /* Close a pragma virtual table cursor */
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 */
116508 ** Check to see if zTabName is really the name of a pragma. If it is,
116510 ** a pointer to the Module object for the new virtual table.
116517 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
116518 assert( sqlite3HashFind(&db->aModule, zName)==0 );
116532 ** a legal notice, here is a blessing:
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 );
116597 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
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;
116633 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
116634 ** constraint for a CREATE TABLE. The index should have already
116639 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
116641 /* This can occur if there exists an index on a TEMP table which
116642 ** has the same name as another index on a permanent index. Since
116647 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
116656 ** data structures for a single database file. The index of the
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. */
116700 /* Create a cursor to hold the database open
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
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 ){
116802 /* Ticket #2804: When we open a database in the newer file format,
116803 ** clear the legacy_file_format pragma flag so that a VACUUM will
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
116875 ** created using ATTACH statements. Return a success code. If an
116878 ** After a database is initialized, the DB_SchemaLoaded bit is set
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++){
116955 int openedTransaction = 0; /* True if a transaction is opened */
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;
116989 ** Convert a schema pointer into the iDb index that indicates
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
116999 ** expr.c is trying to resolve a reference to a transient table (i.e. one
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. */
117045 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared 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++;
117068 /* Check to verify that it is possible to get a read lock on all
117069 ** database schemas. The inability to get a read lock indicates that
117070 ** some other database connection is holding a write-lock, which in
117082 ** is not possible for another thread to start a new schema change
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. */
117203 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117215 sqlite3_mutex_enter(db->mutex);
117223 sqlite3_mutex_leave(db->mutex);
117229 ** Rerun the compilation of a statement after a schema change.
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);
117270 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
117277 const char *zSql, /* UTF-8 encoded SQL statement. */
117279 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117289 const char *zSql, /* UTF-8 encoded SQL statement. */
117291 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117295 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
117296 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
117307 const char *zSql, /* UTF-8 encoded SQL statement. */
117310 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117314 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
117316 ** which is a bit array consisting of zero or more of the
117331 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
117335 const void *zSql, /* UTF-16 encoded SQL statement. */
117338 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared 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);
117369 /* If sqlite3_prepare returns a tail pointer, we calculate the
117370 ** equivalent pointer into the UTF-16 string by counting the unicode
117371 ** characters between zSql8 and zTail8, and then returning a pointer
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);
117386 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
117393 const void *zSql, /* UTF-16 encoded SQL statement. */
117395 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117405 const void *zSql, /* UTF-16 encoded SQL statement. */
117407 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117417 const void *zSql, /* UTF-16 encoded SQL statement. */
117420 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
117439 ** a legal notice, here is a blessing:
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 */
117498 ** Delete all the content of a Select structure. Deallocate the structure
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);
117520 ** Initialize a SelectDest structure.
117523 pDest->eDest = (u8)eDest;
117524 pDest->iSDParm = iParm;
117525 pDest->zAffSdst = 0;
117526 pDest->iSdst = 0;
117527 pDest->nSdst = 0;
117532 ** Allocate a new Select structure and return a pointer to that
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 );
117592 ** Set the name of a Select object
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;
117629 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
117632 ** a join type, but put an error in the pParse structure.
117660 if( p->n==aKeyword[j].nChar
117661 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
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;
117704 ** Search the first N tables in pSrc, from left to right, looking for a
117705 ** table that has a column named zCol.
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);
117738 ** WHERE clause expression of a SELECT statement. The new term, which
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;
117787 ** join restriction specified in the ON or USING clause and not a part
117797 ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
117800 ** term until after the t2 loop of the join. In that way, a
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;
117824 ** This routine processes the join information for a SELECT statement.
117828 ** The terms of a FROM clause are contained in the Select.pSrc structure.
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 ){
117859 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
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;
117895 ** A and B and the USING clause names X, Y, and Z, then add this
117896 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
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 ){
118071 ** form a distinct entry. iTab is a sorting index that holds previously
118072 ** seen combinations of the N values. A new entry is made in iTab
118075 ** A jump to addrRepeat is made and the N+1 values are popped from the
118080 int iTab, /* A sorting index used to test for distinctness */
118088 v = pParse->pVdbe;
118099 ** of a SELECT.
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 ){
118155 /* This is an error condition that can result, for example, when a SELECT
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));
118180 /* For each expression in pEList that is a copy of an expression in
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);
118276 /* Construct a record from the query result, but instead of
118277 ** saving that record, use it as a key to delete elements from
118286 /* Store the result as data using a unique key.
118326 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
118327 ** then there should be a single item on the stack. Write this
118334 ** does not matter. But there might be a LIMIT clause, in which
118340 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
118342 r1, pDest->zAffSdst, nResultCol);
118358 /* If this is a scalar select that is part of an expression, then
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);
118393 /* Write the results into a priority queue that is order according to
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
118397 ** final OP_Sequence column. The last column is the record as a blob.
118405 pSO = pDest->pOrderBy;
118407 nKey = pSO->nExpr;
118413 ** on a second ephemeral index that holds all values every previously
118426 regResult + pSO->a[i].u.x.iOrderByCol - 1,
118443 ** the body of a TRIGGER. The purpose of such selects is to call
118444 ** user-defined functions that have side effects. We do not care
118455 ** there is a sorter, in which case the sorter has already limited
118458 if( pSort==0 && p->iLimit ){
118459 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
118464 ** Allocate a KeyInfo object sufficient for an index of N key columns and
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;
118485 ** Deallocate a KeyInfo object
118489 assert( p->nRef>0 );
118490 p->nRef--;
118491 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
118496 ** Make a new pointer to a KeyInfo object
118500 assert( p->nRef>0 );
118501 p->nRef++;
118508 ** Return TRUE if a KeyInfo object can be change. The KeyInfo object
118509 ** can only be changed if this is just a single reference to the object.
118513 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
118517 ** Given an expression list, generate a KeyInfo structure that records
118521 ** KeyInfo structure is appropriate for initializing a virtual index to
118522 ** implement that clause. If the ExprList is the result set of a SELECT
118523 ** then the KeyInfo structure is appropriate for initializing a virtual
118524 ** index to implement a DISTINCT test.
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
118597 # define explainSetInteger(a, b) a = b
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)"
118625 int bUseTmp /* True if a temp table was used */
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,
118644 ** then the results were placed in a sorter. After the loop is terminated
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);
118776 ** Return a pointer to a string containing the 'declaration type' of the
118783 ** original CREATE TABLE statement if the expression is a column. The
118784 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
118785 ** is considered a column can be complex in the presence of subqueries. The
118786 ** result-set expression in all of the following SELECT statements is
118787 ** considered a column by this function.
118794 ** The declaration type for any expression other than a column is NULL.
118797 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
118800 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
118802 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
118824 assert( pNC->pSrcList!=0 );
118825 switch( pExpr->op ){
118828 /* The expression is a column. Locate the table the column is being
118830 ** database table or a subquery.
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;
118849 /* At one time, code such as "SELECT new.x" within a trigger would
118859 ** sub-select. In this case, set the column type to NULL, even
118862 ** This is not a problem, as the column type of "t1.col" is never
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 ){
118888 /* A real table */
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
118990 ** Compute the column names for a SELECT statement.
118993 ** column has an AS clause assigning it a name, that will be the name used.
119011 ** refers directly to a table column, then the result
119015 ** full=ON, short=ANY: If the result refers directly to a table column,
119023 Vdbe *v = pParse->pVdbe;
119028 sqlite3 *db = pParse->db;
119029 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
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;
119088 ** that form the result set of a SELECT statement) compute appropriate
119089 ** column names for a table that would hold the expression list.
119096 ** Return SQLITE_OK on success. If a memory allocation error occurs,
119100 ** column has an AS clause assigning it a name, that will be the name used.
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 ){
119200 ** Add type and collation information to a column list based on
119201 ** a SELECT statement.
119215 sqlite3 *db = pParse->db;
119221 struct ExprList_item *a;
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++){
119234 p = a[i].pExpr;
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);
119256 ** Given a SELECT statement, generate a Table structure that describes
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 ){
119292 ** Get a VDBE for the given parser context. Create a new one if necessary.
119293 ** If an error occurs, return NULL and leave a message in pParse.
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;
119321 ** a limit or offset is defined by pLimit and pOffset. iLimit and
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);
119407 ** The select statement passed as the second parameter is a compound SELECT
119408 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
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;
119446 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
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
119458 ** into a Queue table. Rows are extracted from the Queue table one by
119461 ** recursive-table for a recursive-query run. The output of the recursive-query
119466 ** inserted into the Queue table. The iDistinct table keeps a copy of all rows
119472 ** an ORDER BY, the Queue table is just a FIFO.
119474 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
119475 ** have been output to pDest. A LIMIT of zero means to output no rows and a
119477 ** with a positive value, then the first OFFSET outputs are discarded rather
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 */
119504 /* Obtain authorization to do a recursive 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
119622 ** VALUES clause. By handling this as a special case, we avoid deep
119624 ** on a VALUES clause.
119626 ** Because the Select object originates from a VALUES clause:
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;
119664 ** This routine is called to process a compound query form from
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
119672 ** The results of the total query are to be written into a destination
119675 ** Example 1: Consider a three-way compound SQL statement.
119677 ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
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 */
119699 int rc = SQLITE_OK; /* Success code from a subroutine */
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 );
119826 /* We can reuse a temporary table generated by a SELECT to our
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;
120042 ** Error message for when two or more terms of a compound select have different
120046 if( p->selFlags & SF_Values ){
120050 " do not have the same number of result columns", selectOpName(p->op));
120055 ** Code an output subroutine for a coroutine implementation of a
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
120065 ** If regPrev>0 then it is the first register in a vector that
120066 ** records the previous output. mem[regPrev] is a flag that is false
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 ){
120112 /* Store the result as data using a unique key.
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);
120127 /* If we are creating a set for an "expr IN (SELECT ...)".
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);
120142 /* If this is a scalar select that is part of an expression, then
120147 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
120148 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
120154 /* The results are stored in a sequence of registers
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);
120171 ** For SRT_Output, results are stored in a sequence of registers.
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);
120201 ** We assume a query of the following form:
120207 ** co-routines. Then run the co-routines in parallel and merge the results
120216 ** UNION ALL. EXCEPT and INSERTSECT never output a row that
120219 ** AltB: Called when there is data from both coroutines and A<B.
120221 ** AeqB: Called when there is data from both coroutines and A==B.
120223 ** AgtB: Called when there is data from both coroutines and A>B.
120234 ** ------------- ----------------- -------------- -----------------
120245 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
120248 ** following nextX causes a jump to the end of the select processing.
120252 ** output value. A comparison is made against this value and the output
120259 ** coA: coroutine for left query (A)
120261 ** outA: output one row of A
120270 ** if eof(A) goto EofA
120273 ** Cmpr: Compare A, B
120285 Select *p, /* The right-most of SELECTs to be coded */
120291 SelectDest destA; /* Destination for coroutine A */
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 */
120304 int addrAltB; /* Address of the A<B subroutine */
120305 int addrAeqB; /* Address of the A==B subroutine */
120306 int addrAgtB; /* Address of the A>B subroutine */
120307 int regLimitA; /* Limit register for select-A */
120308 int regLimitB; /* Limit register for select-A */
120309 int regPrev; /* A range of registers to hold previous output */
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);
120393 /* Allocate a range of temporary registers and the KeyInfo needed
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;
120447 /* Generate a coroutine to evaluate the SELECT statement to the
120448 ** left of the compound operator - the "A" select.
120453 pPrior->iLimit = regLimitA;
120454 explainSetInteger(iSub1, pParse->iNextSelectId);
120459 /* Generate a coroutine to evaluate the SELECT statement on
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;
120475 /* Generate a subroutine that outputs the current row of the A
120478 VdbeNoopComment((v, "Output routine for A"));
120483 /* Generate a subroutine that outputs the current row of the B
120494 /* Generate a subroutine to run when the results from select A
120500 VdbeNoopComment((v, "eof-A subroutine"));
120505 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120508 /* Generate a subroutine to run when the results from select B
120509 ** are exhausted and only data in select A remains.
120513 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
120515 VdbeNoopComment((v, "eof-B subroutine"));
120521 /* Generate code to handle the case of A<B
120523 VdbeNoopComment((v, "A-lt-B subroutine"));
120528 /* Generate code to handle the case of A==B
120536 VdbeNoopComment((v, "A-eq-B subroutine"));
120542 /* Generate code to handle the case of A>B
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;
120589 ** to be performed on a parse tree.
120608 ** a column in table number iTable with a copy of the iColumn-th
120612 ** This routine is part of the flattening procedure. A subquery
120614 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
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 );
120712 ** This routine attempts to flatten subqueries as a performance optimization.
120718 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
120721 ** subquery first and store the results in a temporary table, then
120728 ** a single flat select, like this:
120730 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
120734 ** exist on the table t1, a complete scan of the data might be
120741 ** (2) The subquery is not an aggregate or (2a) the outer query is not a join
120743 ** FROM-clause subquery that is a candidate for flattening. (2b is
120744 ** due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
120746 ** (3) The subquery is not the right operand of a LEFT JOIN
120747 ** or (a) the subquery is not itself a join and (b) the FROM clause
120748 ** of the subquery does not contain a virtual table and (c) the
120753 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
120754 ** sub-queries that were excluded from this optimization. Restriction
120760 ** (7) The subquery has a FROM clause. TODO: For subqueries without
120761 ** A FROM clause, consider adding a FROM clause with the special
120762 ** table sqlite_once that consists of a single row containing a
120765 ** (8) The subquery does not use LIMIT or the outer query is not a join.
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
120778 ** a separate restriction deriving from ticket #350.
120784 ** (15) The outer query is not part of a compound select or the
120785 ** subquery does not have a LIMIT clause.
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
120796 ** * is not itself part of a compound select,
120798 ** * is not a join
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
120808 ** of result columns. This is actually a requirement for any compound
120810 ** such (illegal) sub-query is flattened. The caller will detect the
120811 ** syntax error and return a detailed message.
120813 ** (18) If the sub-query is a compound select, then all terms of the
120815 ** columns of the sub-query.
120818 ** have a WHERE clause.
120820 ** (20) If the sub-query is a compound select, then it must not use
120829 ** (22) The subquery is not a recursive CTE.
120831 ** (23) The parent is not a recursive CTE, or the sub-query is not a
120833 ** parent to a compound query confuses the code that handles
120836 ** (24) The subquery is not an aggregate that uses the built-in min() or
120837 ** or max() functions. (Without this restriction, a query like:
120842 ** In this routine, the "p" parameter is a pointer to the outer query.
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 */
120867 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
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 ){
120933 ** If the subquery is the right operand of a LEFT JOIN, then the
120934 ** subquery may not be a join itself. Example of why this is not allowed:
120944 ** If the subquery is the right operand of a LEFT JOIN, then the outer
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
120960 ** every reference to any result column from subquery in a join, even though
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
121012 ** 17 and 18 above) it must be a UNION ALL and the parent query must
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.
121025 ** SELECT a+1 FROM (
121031 ** ) WHERE a!=5 ORDER BY 1
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
121112 ** flattening (as described above). If we are doing a different kind
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 );
121143 /* The subquery uses a single slot of the FROM clause of the outer
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;
121187 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
121189 if( pSub->pOrderBy ){
121190 /* At this point, any non-zero iOrderByCol values indicate that the
121196 ** Not doing this may cause an error if a subsequent call to this
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;
121241 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
121243 ** One is tempted to try to add a and b to combine the limits. But this
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)
121288 ** to copy the outer WHERE-clause terms onto the HAVING clause of the
121291 ** (2) The inner query is the recursive part of a common table expression.
121293 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
121296 ** (4) The inner query is the right operand of a LEFT JOIN. (The caller
121301 ** of a LEFT JOIN.
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;
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;
121355 ** * the query contains just a single aggregate function,
121357 ** * the argument to the aggregate function is a column value.
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
121399 ** does match this pattern, then a pointer to the Table object representing
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
121429 ** was such a clause and the named index cannot be found, return
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;
121457 ** These are rewritten as a subquery:
121463 ** above that generates the code for a compound SELECT with an ORDER BY clause
121464 ** uses a merge algorithm that requires the same collating sequence on the
121477 struct ExprList_item *a;
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);
121537 ** Argument pWith (which may be NULL) points to a linked list of nested
121539 ** FROM clause element pItem is really a common-table-expression (CTE)
121540 ** then return a pointer to the CTE definition for that table. Otherwise
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];
121567 /* The code generator maintains a stack of active WITH clauses
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;
121588 ** This function checks if argument pFrom refers to a CTE declared by
121589 ** a WITH clause on the stack currently maintained by the parser. And,
121590 ** if currently processing a CTE expression, if it is a recursive
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
121598 ** Whether or not a match is found, SQLITE_OK is returned if no error
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 );
121644 /* Check if this is a recursive CTE. */
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;
121723 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
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;
121741 ** This routine is a Walker callback for "expanding" a SELECT statement.
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
121750 ** that implements the view. A copy is made of the view's SELECT
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);
121793 ** an entry of the FROM clause is a subquery instead of a table or view,
121794 ** then create a transient table structure to describe the subquery.
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) ){
121866 ** of all columns in TABLE. The parser inserted a special expression
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++){
121895 pE = a[k].pExpr;
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)
121903 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
121905 pNew->a[pNew->nExpr-1].zName = a[k].zName;
121906 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
121907 a[k].zName = 0;
121908 a[k].zSpan = 0;
121910 a[k].pExpr = 0;
121912 /* This expression is a "*" or a "TABLE.*" and needs to be
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;
121943 Token sColname; /* Computed column name as a token */
121947 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
121952 /* If a column is marked as 'hidden', omit it from the expanded
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
121967 /* In a NATURAL join, omit the join columns from the
121971 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
121972 /* In a join with a USING clause, omit columns in the
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.
122068 ** This routine "expands" a SELECT statement and all of its subqueries.
122069 ** For additional information on what it means to "expand" a SELECT
122072 ** Expanding a SELECT statement is the first step in processing a
122077 ** The calling function can detect the problem by looking at pParse->nErr
122078 ** and/or pParse->db->mallocFailed.
122084 if( pParse->hasCompound ){
122097 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
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
122156 ** This routine sets up a SELECT statement for processing. The
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;
122187 ** The aggregate accumulator is a set of memory cells that hold
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;
122328 ** Add a single OP_Explain instruction to the VDBE to explain a simple
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
122350 # define explainSimpleCount(a,b,c)
122364 ** If the node passed to the callback is a TK_AND node, return
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
122370 ** within the HAVING expression with a constant "1".
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;
122391 ** Transfer eligible terms from the HAVING clause of a query, which is
122395 ** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
122399 ** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
122401 ** A term of the HAVING expression is eligible for transfer if it consists
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)
122454 ** Attempt to transform a query of the form
122464 ** * The subquery is a UNION ALL of two or more terms
122466 ** * The outer query is a simple count(*)
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;
122483 if( pSub==0 ) return 0; /* The FROM is a subquery */
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;
122618 /* Get a pointer the VDBE under construction, allocating a new VDBE if one
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;
122636 /* Catch mismatch in the declared columns of a view and the number of
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];
122685 /* Issue SQLITE_READ authorizations with a fake column name for any tables that
122689 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
122690 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
122692 ** The fake column name is an empty string. It is possible for a table to
122693 ** have a column named by the empty string, in which case there is no way to
122695 ** "" column. The original design was for the fake column name to be a NULL,
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;
122710 /* Sometimes the code for a subquery will be generated more than
122711 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
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,
122720 ** encodes a correlated subquery. */
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);
122788 /* Generate a subroutine that will fill an ephemeral table with
122789 ** the content of this subquery. pItem->addrFillSub will point
122790 ** to the address of the generated subroutine. pItem->regReturn
122791 ** is a register allocated to hold the subroutine return address
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 ){
122804 ** a trigger, then we only need to compute the value of the subquery
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
122861 ** can be rewritten as a GROUP BY. In other words, this:
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;
122913 /* If the output is destined for a temporary table, open that table.
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 ){
122969 /* If sorting index that was created by a prior OP_OpenEphemeral
122978 selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
122986 /* This case when there exist aggregate functions or a GROUP BY clause
123006 struct ExprList_item *pItem; /* For looping over expression in a list */
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;
123021 /* If there is both a GROUP BY and an ORDER BY clause and they are
123024 ** in the correct order. It also may not - the GROUP BY might use a
123029 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
123033 /* Create a label to jump to when we want to abort the query */
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;
123068 ** much more complex than aggregates without a GROUP BY.
123072 int addr1; /* A-vs-B comparision jump */
123073 int addrOutputRow; /* Start of subroutine that outputs a result row */
123081 /* If there is a GROUP BY clause we might need a sorting index to
123084 ** will be converted into a Noop.
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);
123110 /* Begin a loop that will extract all source rows in GROUP BY order.
123112 ** it might be a single loop that uses an index to extract information
123120 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
123128 ** each row into a sorting index, terminate the first loop,
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);
123262 /* Generate a subroutine that outputs a single row of the result
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
123299 /* If isSimpleCount() returns a pointer to a Table structure, then
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);
123370 ** A special flag must be passed to sqlite3WhereBegin() to slightly
123373 ** + If the query is a "SELECT min(x)", then the loop coded by
123374 ** where.c should not iterate over any values with a NULL value
123378 ** index or indices to use) should place a different priority on
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;
123403 ** processing is much simpler since there is only a single row
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--;
123475 ** a legal notice, here is a blessing:
123486 ** These routines are in a separate files so that they will not be linked
123514 int need; /* Slots needed in p->azResult[] */
123516 char *z; /* A single column of result */
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;
123577 ** Query the database. But instead of invoking a callback for each row,
123612 db->errCode = SQLITE_NOMEM;
123628 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
123641 db->errCode = SQLITE_NOMEM;
123660 azResult--;
123675 ** a legal notice, here is a blessing:
123688 ** Delete a linked list of TriggerStep structures.
123693 pTriggerStep = pTriggerStep->pNext;
123695 sqlite3ExprDelete(db, pTmp->pWhere);
123696 sqlite3ExprListDelete(db, pTmp->pExprList);
123697 sqlite3SelectDelete(db, pTmp->pSelect);
123698 sqlite3IdListDelete(db, pTmp->pIdList);
123705 ** Given table pTab, return a list of all the triggers attached to
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
123714 ** To state it another way: This routine returns a list of all triggers
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);
123744 ** This is called by the parser when it sees a CREATE TRIGGER statement
123745 ** up to the point of the BEGIN before the trigger actions. A Trigger
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;
123807 /* If the trigger name was unqualified, and the table is a temp table,
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 ){
123830 ** Normally, whenever a table is dropped, all associated triggers are
123831 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
123832 ** and the table is dropped by a different database connection, the
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 );
123863 /* Do not create a trigger on a system table */
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) ){
123901 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
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 );
124014 ** Turn a SELECT statement (that the pSelect parameter points to) into
124015 ** a trigger step. Return a pointer to a TriggerStep structure.
124017 ** The parser calls this routine when it finds a SELECT statement in
124018 ** body of a TRIGGER.
124026 pTriggerStep->op = TK_SELECT;
124027 pTriggerStep->pSelect = pSelect;
124028 pTriggerStep->orconf = OE_Default;
124033 ** Allocate space to hold a new trigger step. The allocated space
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;
124057 ** Build a trigger step out of an INSERT statement. Return a pointer
124061 ** body of a trigger.
124067 Select *pSelect, /* A SELECT statement that supplies values */
124072 assert(pSelect != 0 || db->mallocFailed);
124076 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
124077 pTriggerStep->pIdList = pColumn;
124078 pTriggerStep->orconf = orconf;
124088 ** Construct a trigger step that implements an UPDATE statement and return
124089 ** a pointer to that trigger step. The parser calls this routine when it
124090 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
124103 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
124104 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124105 pTriggerStep->orconf = orconf;
124113 ** Construct a trigger step that implements a DELETE statement and return
124114 ** a pointer to that trigger step. The parser calls this routine when it
124115 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
124126 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124127 pTriggerStep->orconf = OE_Default;
124134 ** Recursively delete a Trigger structure
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);
124147 ** This function is called to drop a trigger from the database schema.
124151 ** same job as this routine except it takes a pointer to the trigger
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;
124193 ** Return a pointer to the Table structure for the table that a trigger
124197 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
124202 ** Drop a trigger given a pointer to that trigger.
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);
124242 ** Remove a trigger from the hash tables of the sqlite* pointer.
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;
124267 ** then return TRUE. If pIdList==NULL, then it is considered a
124275 for(e=0; e<pEList->nExpr; e++){
124276 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
124282 ** Return a list of all triggers on table pTab if there exists at least
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
124317 ** This routine adds a specific database name, if needed, to the target when
124318 ** forming the SrcList. This prevents a trigger in one database from
124319 ** referring to a target in another database. An exception is when the
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);
124347 ** Generate VDBE code for the statements inside the body of a single
124356 Vdbe *v = pParse->pVdbe;
124357 sqlite3 *db = pParse->db;
124359 assert( pParse->pTriggerTab && pParse->pToplevel );
124362 for(pStep=pStepList; pStep; pStep=pStep->pNext){
124370 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
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 ){
124424 ** This function is used to add VdbeComment() annotations to a VDBE
124436 return "n/a";
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;
124495 /* Allocate and populate a new Parse context to use for coding the
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
124571 ** TriggerPrg object exists, a new object is allocated and populated before
124583 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
124587 ** a matching TriggerPrg.pTrigger field will be present somewhere
124589 for(pPrg=pRoot->pTriggerPrg;
124590 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
124591 pPrg=pPrg->pNext
124594 /* If an existing TriggerPrg could not be located, create a new one. */
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,
124634 ** not a foreign key action, and (b) the flag to enable recursive triggers
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
124672 ** is not a readable register, although registers (reg+N) through
124677 ** is the instruction that control should jump to if a trigger program
124697 for(p=pTrigger; p; p=p->pNext){
124701 ** or else it must be a TEMP trigger. */
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
124736 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
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];
124779 ** a legal notice, here is a blessing:
124807 ** i-th column of table pTab. This routine sets the P4 parameter of the
124810 ** The default value of a column is specified by a DEFAULT clause in the
124813 ** command. If the latter, then the row-records in the table btree on disk
124814 ** may not contain a value for the column and the default value, taken
124816 ** If the former, then all row-records are guaranteed to include a value
124820 ** literal default values specified: a number, null or a string. (If a more
124826 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
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. */
124887 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
124888 u8 chngRowid; /* Rowid changed in a normal table */
124892 NameContext sNC; /* The name-context to resolve expressions in */
124901 int isView; /* True when updating a view (INSTEAD OF trigger) */
124915 int regRowCount = 0; /* A count of rows changed */
124924 db = pParse->db;
124925 if( pParse->nErr || db->mallocFailed ){
124928 assert( pTabList->nSrc==1 );
124934 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124937 ** updated is a view.
124941 isView = pTab->pSelect!=0;
124960 /* Allocate a cursors for the main database table and for all indices.
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;
125046 ** So reset the colUsed mask. Unless this is a virtual table. In that
125050 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
125055 ** being updated. Fill in aRegIdx[] with a register number that will hold
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);
125116 /* If we are trying to update a view, realize that view into
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
125166 ** be deleted as a result of REPLACE conflict handling. Any of these
125167 ** things might disturb a cursor being used to scan through the table
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
125183 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
125189 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
125197 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
125205 ** ONEPASS_OFF mode, serialize the array into a record and store it in
125207 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
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
125314 ** registers associated with columns that are (a) not modified by
125324 for(i=0; i<pTab->nCol; i++){
125325 if( i==pTab->iPKey ){
125330 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
125332 /* This branch loads the value of a column that will not be changed
125333 ** into a register. This is done if there are no BEFORE triggers, or
125335 ** a new.* reference in a trigger program.
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);
125406 ** to process, delete the old record. Otherwise, add a noop OP_Delete
125407 ** to invoke the pre-update hook.
125410 ** pre-update hook. If the caller invokes preupdate_new(), the returned
125424 if( !pParse->nested ){
125448 ** handle rows (possibly in other tables) that refer via a foreign key
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 ){
125490 ** generating code because of a call to sqlite3NestedParse(), do not
125493 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
125519 ** Generate code for an UPDATE of a virtual table.
125521 ** There are two possible strategies - the default and the special
125528 ** (A) The original rowid of that row.
125532 ** Then loop through the contents of this ephemeral table executing a
125536 ** stores the same values (A, B and C above) in a register array and
125537 ** makes a single invocation of VUpdate.
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;
125605 /* Create a record from the argument register contents and insert it into
125649 ** a legal notice, here is a blessing:
125674 ** a format string as its third argument
125681 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
125718 ** (1) Create a new transient database file
125739 ** And a power loss in between deleting the original and renaming the
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 */
125782 int isMemDb; /* True if vacuuming a :memory: database */
125787 if( !db->autoCommit ){
125788 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
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;
125813 ** can be set to 'off' for this file, as it is not recovered if a crash
125814 ** occurs anyway. The integrity of the database is maintained by a
125818 ** An optimisation would be to use a non-journaled pager.
125821 ** actually occurs when doing a vacuum since the vacuum_db is initially
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;
125842 /* A VACUUM cannot change the pagesize of an encrypted database. */
125844 if( db->nextPagesize ){
125849 if( nKey ) db->nextPagesize = 0;
125853 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
125857 /* Begin a transaction and take an exclusive lock on the main database
125859 ** to ensure that we do not try to change the page-size on a WAL database.
125866 /* Do not attempt to change the page size for 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 :
125885 /* Query the schema of the main database. Create a mirror schema
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;
125933 /* At this point, there is a write transaction open on both the
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[]
126020 ** a legal notice, here is a blessing:
126033 ** Before a virtual table xCreate() or xConnect() method is invoked, the
126047 ** Construct and install a Module object for a virtual table. When this
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);
126082 ** The actual function that does the work of creating a new module.
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.
126141 ** Locks nest. Every lock should have a corresponding unlock.
126144 ** If a disconnect is attempted while a virtual table is locked,
126148 pVTab->nRef++;
126153 ** pTab is a pointer to a Table structure representing a virtual-table.
126154 ** Return a pointer to the VTable object used by connection db to access
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);
126186 ** Table p is a virtual table. This function moves all elements in the
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;
126225 ** Table *p is a virtual table. This function removes the VTable object
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
126261 ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
126264 ** or, if the virtual table is stored in a non-sharable database, then
126267 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
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
126293 ** to the head of a linked list of VTable structures. Each VTable
126294 ** structure is associated with a single sqlite3* user of the schema.
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;
126333 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
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
126369 /* Creating a virtual table invokes the authorization callback twice.
126370 ** The first invocation, to obtain permission to INSERT a row into the
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);
126427 /* A slot for the record has already been allocated in the
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
126462 ** allows a schema that contains virtual tables to be loaded before
126466 Schema *pSchema = pTab->pSchema;
126467 const char *zName = pTab->zName;
126469 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
126475 pParse->pNewTable = 0;
126481 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
126485 pParse->sArg.z = 0;
126486 pParse->sArg.n = 0;
126491 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
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);
126505 ** Invoke a virtual table constructor (either xCreate or xConnect). The
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) ){
126574 /* Justification of ALWAYS(): A correct vtab constructor must allocate
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.
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);
126721 /* If the module has been registered and includes a Create method,
126725 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
126729 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
126733 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
126745 ** This function is used to set the schema of a virtual table. It is only
126746 ** valid to call this function from within the xCreate() or xConnect() of a
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);
126830 ** when a DROP TABLE is mentioned.
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;
126938 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
126948 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
126949 ** than zero, then this function is being called from within a
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);
127038 ** The first parameter (pDef) is a function implementation. The
127040 ** If pExpr is a column in a virtual table, then let the virtual
127046 ** Return either the pDef argument (indicating no change) or a
127067 /* Check to see the left operand is a column in a virtual table */
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);
127094 /* Create a new ephemeral function definition for the overloaded
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
127142 ** module, and more importantly, does not require a CREATE VIRTUAL TABLE
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;
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
127262 ** a legal notice, here is a blessing:
127272 ** This file was split off from where.c on 2015-06-06 in order to reduce the
127281 ** 2013-11-12
127284 ** a legal notice, here is a blessing:
127294 ** a separate source file for easier editing.
127327 ** This object contains information needed to implement a single nested
127332 ** This object contains a pointer to the WhereLoop algorithm as one of
127335 ** The WhereInfo object contains a single instance of this object for
127338 ** the loop nested order, with WhereInfo.a[0] being the outer loop and
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 */
127378 ** term of a join. Every term of the FROM clause will have at least
127380 ** prevent a query solution - which is an error) and many terms of the
127381 ** FROM clause will have multiple WhereLoop objects, each describing a
127382 ** potential way of implementing that FROM-clause term, together with
127385 ** Query planning consists of building up a collection of these WhereLoop
127386 ** objects, then computing a particular sequence of WhereLoop objects, with
127398 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
127428 /* This object holds the prerequisites and the cost of running a
127438 /* The WhereOrSet object holds a set of possible WhereOrCosts that
127439 ** correspond to the subquery(s) of OR-clause processing. Only the
127444 u16 n; /* Number of valid a[] entries */
127445 WhereOrCost a[N_OR_COST]; /* Set of best costs */
127449 ** Each instance of this object holds a sequence of WhereLoop objects
127450 ** that implement some or all of a query plan.
127452 ** Think of each WhereLoop object as a node in a graph with arcs
127454 ** not a completely accurate description because WhereLoop costs are a
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.)
127457 ** Then a WherePath object is a path through the graph that visits some
127461 ** 1. Then using those as a basis to compute the N best WherePath objects
127472 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
127482 ** All WhereTerms are collected into a single WhereClause structure.
127485 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
127487 ** When a term is of the form:
127491 ** where X is a column name and <op> is one of certain operators,
127494 ** the <op> using a bitmask encoding defined by WO_xxx below. The
127495 ** use of a bitmask encoding for the operator allows us to search
127498 ** A WhereTerm might also be two or more subterms connected by OR:
127506 ** If a term in the WHERE clause does not match either of the two previous
127512 ** but they do so indirectly. A single WhereMaskSet structure translates
127515 ** bits that will fit in a Bitmask. The VDBE cursor numbers might be
127516 ** spread out over the non-negative integers. For example, the cursor
127523 ** The number of terms in a join is limited by the number of bits
127532 u16 eOperator; /* A WO_xx value describing <op> */
127535 int iParent; /* Disable pWC->a[iParent] when this term disabled */
127543 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
127553 #define TERM_COPIED 0x08 /* Has a child */
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 */
127587 ** An instance of the following structure holds all information about a
127588 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
127590 ** Explanation of pOuter: For a WHERE clause of the form
127592 ** a AND ((b AND c) OR (d AND e)) AND f
127603 int nSlot; /* Number of entries in a[] */
127604 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
127606 WhereTerm aStatic[1]; /* Initial static space for a[] */
127608 WhereTerm aStatic[8]; /* Initial static space for a[] */
127613 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
127614 ** a dynamically allocated instance of the following structure.
127622 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
127623 ** a dynamically allocated instance of the following structure.
127630 ** An instance of the following structure keeps track of a mapping
127637 ** use of the bits in our bitmasks. This structure provides a mapping
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.
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
127662 ** Initialize a WhereMaskSet object
127664 #define initMaskSet(P) (P)->n=0
127667 ** This object is a convenience wrapper holding all information needed
127668 ** to construct WhereLoop objects for a particular query.
127685 #define SQLITE_BLDF_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
127707 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
127715 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
127722 WhereLevel a[1]; /* Information about each nest loop in WHERE */
127726 ** Private interfaces - callable only by other where.c routines.
127764 # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
127768 int iLevel, /* Which level of pWInfo->a[] should be coded */
127788 ** OR-ed combination of these values can be used when searching for
127789 ** particular WhereTerms within a WhereClause.
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 */
127810 #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
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;
127857 ** This routine is a helper for explainIndexRange() below
127860 ** at a time. This routine adds a new term to the end of the expression.
127868 int iTerm, /* Zero-based index of first term. */
127869 int bAnd, /* Non-zero to append " AND " */
127895 ** Argument pLevel describes a strategy for scanning table pTab. This
127901 ** SELECT * FROM t1 WHERE a=1 AND b>2;
127903 ** is run and there is an index on (a, b), then this function returns a
127906 ** "a=? AND b>?"
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;
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) */
127959 int isSearch; /* True for a SEARCH. False for SCAN. */
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));
128048 ** implement level pLvl. Argument pSrclist is a pointer to the FROM
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
128075 ** Disable a term in the WHERE clause. Except, do not disable the term
128076 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
128081 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
128082 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
128083 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
128087 ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
128089 ** Disabling a term causes that term to not be tested in the inner loop
128093 ** but joins might run a little slower. The trick is to disable as much
128097 ** If all the children of a term are disabled, then that term is also
128113 ** a conditional such that is only evaluated on the second pass of a
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--;
128175 ** Expression pRight, which is the RHS of a comparison operation, is
128176 ** either a vector of n elements or, if n==1, a scalar expression.
128201 ** Generate code for a single equality term of the WHERE clause. An equality
128205 ** The current value for the constraint is left in a register, the index
128208 ** constraint is a TK_EQ or TK_IS, then the current value might be left in
128211 ** For a constraint of the form X=expr, the expression is evaluated in
128212 ** straight-line code. For constraints of the form X IN (...)
128213 ** this routine sets up a loop that will iterate over all values of X.
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;
128303 /* Take care here not to generate a TK_VECTOR containing only a
128304 ** single value. Since the parser never creates such a vector, some
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;
128354 testcase( nEq>1 ); /* Happens with a UNIQUE index on ROWID */
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);
128384 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
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
128402 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
128410 ** Before returning, *pzAff is set to point to a buffer containing a
128416 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
128417 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
128419 ** In the example above, the index on t1(a) has TEXT affinity. But since
128421 ** no conversion should be attempted before using a t2.b value as part of
128422 ** a key to search the index. Hence the first byte in the returned affinity
128433 u16 nSkip; /* Number of left-most columns to skip */
128434 Vdbe *v = pParse->pVdbe; /* The vm under construction */
128436 WhereTerm *pTerm; /* A single constraint term */
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];
128487 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
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 ){
128501 /* No affinity ever needs to be (or should be) applied to a value
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);
128529 ** If the most recently coded instruction is a constant range constraint
128530 ** (a string literal) that originated from the LIKE optimization, then
128532 ** to a BLOB at appropriate times.
128534 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
128536 ** scan loop run twice, once for strings and a second time for BLOBs.
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 */
128562 # define whereLikeOptimizationStringFixup(A,B,C)
128578 ** This function is called for every node of an expression that is a candidate
128579 ** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
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;
128596 ** Test whether or not expression pExpr, which was part of a WHERE clause,
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
128602 ** a TK_COLUMN node that does affect the value of the expression is set
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;
128630 ** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
128634 ** 1) allocate a register and code an OP_Column instruction to read
128637 ** 2) transform the expression node to a TK_REGISTER node that reads
128640 ** Also, if the node is a TK_COLUMN that does access the table idenified
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 ){
128663 /* An aggregate function in the WHERE clause of a query means this must
128664 ** be a correlated sub-query, and expression pExpr is an aggregate from
128667 ** todo: It should be possible to replace this node with a TK_REGISTER
128668 ** expression, as the result of the expression must be stored in a
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.
128715 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
128719 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
128721 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
128724 ** SQLite to synthesize a row of NULL values. Which does match the
128725 ** WHERE clause, and so the query returns a row. Which is incorrect.
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
128748 ** the cursor. These terms are not needed as hints for a pure range
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
128784 ** a rowid value just read from cursor iIdxCur, open on index pIdx. This
128785 ** function generates code to do a deferred seek of cursor iCur to the
128792 ** However, if the scan currently being coded is a branch of an OR-loop and
128793 ** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
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);
128831 ** If the expression passed as the second argument is a vector, generate
128835 ** If the expression is not a vector, then nReg must be passed 1. In
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);
128863 /* An instance of the IdxExprTrans object carries information about a
128864 ** mapping from an expression on table columns into a column in an index
128875 ** a reference to an index column for an index on an expression.
128877 ** If pExpr matches, then transform it into a reference to the index column
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;
128895 ** and change that subexpression into a reference to the appropriate column of
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 */
128943 WhereTerm *pTerm; /* A WHERE clause term */
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));
128971 ** for the current loop. Jump to addrBrk to break out of a loop.
128975 ** When there is an IN operator, we also have a "addrNxt" label that
128980 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
128981 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
128983 /* If this is the right table of a LEFT OUTER JOIN, allocate and
128984 ** initialize a memory cell that records if this table matches any
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"));
128993 /* Compute a safe address to jump to if we discover that the table for
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
129098 /* Case 2: We can directly reference a single row using an
129100 ** we reference multiple rows using a "rowid IN (...)"
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++];
129147 ** seek opcodes. It depends on a particular ordering of TK_xx
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 ){
129223 /* Case 4: A scan using an index.
129227 ** left-most columns of the index. It may also contain
129230 ** the right-most column can be an inequality - the rest must
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 );
129292 /* If this loop satisfies a sort order (pOrderBy) request that
129293 ** was passed to this function to implement a "SELECT min(x) ..."
129295 ** a single iteration. This means that the first row returned
129296 ** should not have a NULL value stored in 'x'. If column 'x' is
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 );
129352 /* If we are doing a reverse order scan on an ascending index, or
129353 ** a forward order scan on a descending index, interchange the
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);
129483 /* pIdx is a covering index. No need to access the main table. */
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 ){
129539 ** CREATE TABLE t1(a,b,c,d);
129540 ** CREATE INDEX i1 ON t1(a);
129544 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
129558 ** Gosub 2 A
129561 ** Following the above, code to terminate the loop. Label A, the target
129567 ** A: <loop body> # Return data, whatever.
129573 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
129574 ** use an ephemeral index instead of a RowSet to record the primary
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;
129602 /* Set up a new SrcList in pOrTab containing the table being scanned
129603 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
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;
129626 ** capable of holding primary keys in the case of a WITHOUT ROWID.
129630 ** is required in a few obscure LEFT JOIN cases where control jumps
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
129657 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
129661 ** is not contained in the ON clause of a LEFT JOIN.
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 );
129683 /* Run a separate WHERE clause for each term of the OR clause. After
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
129762 /* Invoke the main loop body as a subroutine */
129766 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
129769 /* The pSubWInfo->untestedTerms flag means that this OR term
129770 ** contained one or more AND term from a notReady table. The
129774 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
129776 /* If all of the OR-connected terms are optimized using the same
129779 ** be possible to use that index as a covering index.
129781 ** If the call to sqlite3WhereBegin() above resulted in a scan that
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);
129821 /* Case 6: There is no usable index. We must do a complete
129827 if( pTabItem->fg.isRecursive ){
129828 /* Tables marked isRecursive have only a single row that is stored in
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 ){
129893 ** can skip the call to the like(A,B) function. But this only works
129899 u32 x = pLevel->iLikeRepCntr;
129908 pWC->nTerm-j, pTerm, iLoop));
129913 pTerm->wtFlags |= TERM_CODED;
129921 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
129923 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
129924 ** the implied "t1.a=123" constraint.
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;
129950 /* For a LEFT OUTER JOIN, generate code that will record the fact that
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
129981 ** a legal notice, here is a blessing:
130002 ** Deallocate all memory associated with a WhereOrInfo object.
130005 sqlite3WhereClauseClear(&p->wc);
130010 ** Deallocate all memory associated with a WhereAndInfo object.
130013 sqlite3WhereClauseClear(&p->wc);
130018 ** Add a single new WhereTerm entry to the WhereClause object pWC.
130020 ** The index in pWC->a[] of the new WhereTerm is returned on success.
130021 ** 0 is returned if the new WhereTerm could not be added due to a memory
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.
130029 ** This is true even if this routine fails to allocate a new WhereTerm.
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));
130086 ** Commute a comparison operator. Expressions of the form "X op Y"
130093 ** the left hand side of a comparison overrides any collation sequence
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));
130155 ** Check to see if the given expression is a LIKE or GLOB operator that
130159 ** In order for the operator to be optimizible, the RHS must be a string
130160 ** literal that does not begin with a wildcard. The LHS must be a column
130161 ** that may only be NULL, a string, or a BLOB, never a number. (This means
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;
130209 /* If the RHS begins with a digit or a minus sign, then the LHS must
130210 ** be an ordinary column (not a virtual table column) with TEXT affinity.
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] ){
130239 /* If the rhs of the LIKE expression is a variable, and the current
130243 ** API. To work around them, add a dummy OP_Variable here.
130247 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
130269 ** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a
130270 ** column of a virtual table.
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 ){
130314 ** a join, then transfer the appropriate markings over to derived.
130318 pDerived->flags |= pBase->flags & EP_FromJoin;
130319 pDerived->iRightJoinTable = pBase->iRightJoinTable;
130324 ** Mark term iChild as being a child of term iParent
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
130334 ** a conjunction, then return just pTerm when N==0. If N is exceeds
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.
130351 ** If these two terms are both of the form: "A op B" with the same
130352 ** A and B values but different operators and if the operators are
130354 ** add a new virtual AND term to pWC that is the combination of the
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)
130414 ** A WhereOrTerm object is computed and attached to the term under
130418 ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
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)
130429 ** (F) x>A OR (x=A AND y>=B)
130434 ** a single table T (as shown in example B above) then create a new virtual
130440 ** then create a new virtual term like this:
130446 ** If there are exactly two disjuncts and one side has x>A and the other side
130447 ** has x=A (for the same x and A) then add a new virtual conjunct term to the
130448 ** WHERE clause of the form "x>=A". Example:
130450 ** x>A OR (x=A AND y>B) adds: x>=A
130456 ** If all subterms are indexable by a single table T, then set
130459 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
130461 ** A subterm is "indexable" if it is of the form
130464 ** A subterm is also indexable if it is an AND of two or more
130467 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
130475 ** All examples A through E above satisfy case 3. But if a term
130485 ** the union of all rowids using a RowSet object. This is similar
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 */
130512 ** stored in a WhereClause structure containing within the WhereOrInfo
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 ){
130606 ** chngToIN holds a set of tables that *might* satisfy case 1. But
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 */
130632 /* Search for a table and column that appears on one side or the
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 ){
130651 /* This term must be of the form t1.a==t2.b where t2 is in the
130653 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
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) );
130674 /* We have found a candidate table and column. Check to see if that
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;
130701 ** case 1. In that case, construct a new virtual term that is
130705 Expr *pDup; /* A transient duplicate expression */
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 */
130742 ** We already know that pExpr is a binary operator where both operands are
130748 ** 4. The affinities of A and B must be compatible
130749 ** 5a. Both operands use the same collating sequence OR
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;
130780 ** Recursively walk the expressions of a SELECT statement and generate
130781 ** a bitmask indicating which tables are used in that expression
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;
130806 ** Expression pExpr is one operand of a comparison operator that might
130811 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
130814 ** If pExpr is a TK_COLUMN column reference, then this routine always returns
130822 Expr *pExpr /* An operand of a comparison operator */
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 ){
130846 Expr *pExpr, /* An operand of a comparison operator */
130849 /* If this expression is a vector to the left or right of a
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 */
130879 ** columns, then the original expression is unchanged and a new virtual
130884 ** is a commuted copy of a prior term.) The original term has nChild=1
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
130937 ** on left table of a LEFT JOIN. Ticket #3015 */
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;
131006 /* If a term is the BETWEEN operator, create two new virtual terms
131009 ** a BETWEEN b AND c
131013 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
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];
131044 /* Analyze a term that is composed of two or more subterms connected by
131047 else if( pExpr->op==TK_OR ){
131048 assert( pWC->op==TK_AND );
131050 pTerm = &pWC->a[idxTerm];
131055 /* Add constraints to reduce the search space on a LIKE or GLOB
131058 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
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];
131139 /* Add a WO_MATCH auxiliary term to the constraint set if the
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
131210 ** a virtual term for each vector component. The expression object
131215 ** This only works if the RHS is a simple SELECT, not a compound
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;
131234 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
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;
131269 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
131272 testcase( pTerm!=&pWC->a[idxTerm] );
131273 pTerm = &pWC->a[idxTerm];
131274 pTerm->prereqRight |= extraRight;
131288 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
131296 ** the WhereClause.a[] array. The slot[] array grows as needed to contain
131301 pWC->op = op;
131303 if( pE2->op!=op ){
131306 sqlite3WhereSplit(pWC, pE2->pLeft, op);
131307 sqlite3WhereSplit(pWC, pE2->pRight, op);
131312 ** Initialize a preallocated WhereClause structure.
131318 pWC->pWInfo = pWInfo;
131319 pWC->pOuter = 0;
131320 pWC->nTerm = 0;
131321 pWC->nSlot = ArraySize(pWC->aStatic);
131322 pWC->a = pWC->aStatic;
131326 ** Deallocate a WhereClause structure. The WhereClause structure
131332 WhereTerm *a;
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);
131352 ** a bitmask indicating which tables are used in that expression
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);
131388 ** Call exprAnalyze on all terms in a WHERE clause.
131400 for(i=pWC->nTerm-1; i>=0; i--){
131406 ** For table-valued-functions, transform the function arguments into
131410 ** a HIDDEN column in the table.
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));
131451 ** a legal notice, here is a blessing:
131460 ** generating the code that loops through a table looking for applicable
131478 ** Return the estimated number of output rows from a WHERE clause
131481 return pWInfo->nRowOut;
131489 return pWInfo->eDistinct;
131497 return pWInfo->nOBSat;
131509 return pWInfo->bOrderedInnerLoop;
131514 ** immediately with the next row of a WHERE clause.
131517 assert( pWInfo->iContinue!=0 );
131518 return pWInfo->iContinue;
131523 ** out of a WHERE loop.
131526 return pWInfo->iBreak;
131531 ** operate directly on the rowis returned by a WHERE clause. Return
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]));
131567 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
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 ){
131622 ** Create a new mask for cursor iCursor.
131625 ** tables in the FROM clause is limited by a test early in the
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++;
131722 ** Initialize a WHERE clause scanner object. Return a pointer to the
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;
131778 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
131779 ** where X is a reference to the iColumn of table iCur or of index pIdx
131781 ** the op parameter. Return a pointer to the term. Return 0 if not found.
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
131792 ** other equivalent values. Hence a search for X will return <expr> if X=A1
131796 ** then try for the one with no dependencies on <expr> - in other words where
131797 ** <expr> is a constant expression of some kind. Only return entries of
131798 ** the form "X <op> Y" where Y is a column in another table if no terms of
131799 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
131800 ** exist, try to return a term that does not use WO_EQUIV.
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) );
131876 return 0; /* Assume an indexed expression can always yield a NULL */
131882 ** Return true if the DISTINCT expression-list passed as the third argument
131885 ** A DISTINCT list is redundant if any subset of the columns in the
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;
131921 ** list, or else the WHERE clause contains a term of the form "col=X",
131922 ** where X is a constant value. The collation sequences of the
131923 ** comparison and select-list expressions must match those of the index.
131926 ** contain a "col=X" term are subject to a NOT NULL constraint.
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);
132039 #define TRACE_IDX_INPUTS(A)
132040 #define TRACE_IDX_OUTPUTS(A)
132045 ** Return TRUE if the WHERE clause term pTerm is of a form where it
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)
132062 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
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 );
132090 WhereTerm *pTerm; /* A single term of the WHERE clause */
132091 WhereTerm *pWCEnd; /* End of pWC->a[] */
132100 int mxBitCol; /* Maximum column in pSrc->colUsed */
132101 CollSeq *pColl; /* Collating sequence to on a column */
132106 u8 sentWarning = 0; /* True if a warnning has been issued */
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
132163 /* Count the number of additional columns needed to create a
132164 ** covering index. A "covering index" is an index that contains all
132165 ** columns that are needed by the query. With a covering index, the
132167 ** be a covering index because the index will not be updated if the
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 );
132209 ** a covering index */
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;
132417 ** must represent a virtual table. This function invokes the xBestIndex()
132421 ** If an error occurs, pParse is populated with an error message and a
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;
132466 ** Estimate the location of a particular key among all keys in an
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 );
132502 /* Do a binary search to find the first sample greater than or equal
132503 ** to pRec. If pRec contains a single field, the set of samples to search
132512 ** aSample[0] = (a, 5)
132513 ** aSample[1] = (a, 10)
132519 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
132522 ** 0: (a)
132523 ** 1: (a, 5)
132524 ** 2: (a, 10)
132525 ** 3: (a, 10)
132540 ** sample 2 is the first sample of a block of N samples, so at first it
132542 ** smaller than sample 1, so the binary search would not work. As a result,
132546 nField = pRec->nField;
132548 iSample = pIdx->nSample * nField;
132556 /* The proposed effective sample is a prefix of sample aSample[iSamp].
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;
132660 ** If it is not NULL, pTerm is a term that provides an upper or lower
132661 ** bound on a range scan. Without considering pTerm, it is estimated
132665 ** If the user explicitly specified a likelihood() value for this term,
132668 ** has a likelihood of 0.50, and any other term a likelihood of 0.25.
132673 if( pTerm->truthProb<=0 ){
132674 nRet += pTerm->truthProb;
132675 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
132676 nRet -= 20; assert( 20==sqlite3LogEst(4) );
132685 ** Return the affinity for a single column of an index.
132688 assert( iCol>=0 && iCol<pIdx->nColumn );
132689 if( !pIdx->zColAff ){
132692 return pIdx->zColAff[iCol];
132699 ** This function is called to estimate the number of rows visited by a
132700 ** range-scan on a skip-scan index. For example:
132702 ** CREATE INDEX i1 ON t1(a, b, c);
132703 ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
132705 ** Value pLoop->nOut is currently set to the estimated number of rows
132706 ** visited for scanning (a=? AND b=?). This function reduces that estimate
132709 ** times (once for each (a,b) combination that matches a=?) is dealt with
132716 ** N is the total number of samples, the pLoop->nOut value is adjusted
132719 ** nOut = nOut * ( min(U - L, 1) / N )
132721 ** If pLower is NULL, or a value cannot be extracted from the term, L is
132722 ** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
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));
132807 ** by scanning an index for a range of values. The range may have an upper
132808 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
132810 ** example, assuming that index p is on t1(a):
132812 ** ... FROM t1 WHERE a > ? AND a < ? ...
132820 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
132823 ** assuming index p is on t1(a, b), and the SQL query is:
132825 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
132828 ** left-most column of the index). Or, if the query is:
132830 ** ... FROM t1 WHERE a > ? AND a < ? ...
132841 ** used, a single range inequality reduces the search space by a factor of 4.
132842 ** and a pair of constraints (x>? AND x<?) reduces the expected number of
132843 ** rows visited by a factor of 64.
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;
132863 tRowcnt a[2];
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
132874 ** is not a simple variable or literal value), the lower bound of the
132875 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
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
132902 whereKeyStats(pParse, p, pRec, 0, a);
132903 iLower = a[0];
132904 iUpper = a[0] + a[1];
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] ){
132911 /* The roles of pLower and pUpper are swapped for a DESC index */
132919 Expr *pExpr = pLower->pExpr->pRight;
132925 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
132926 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
132928 nOut--;
132936 Expr *pExpr = pUpper->pExpr->pRight;
132942 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
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
132987 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
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.
133019 ** This routine can fail if it is unable to load a collating sequence
133021 ** for a UTF conversion required for comparison. The error is stored
133030 Index *p = pBuilder->pNew->u.btree.pIndex;
133031 int nEq = pBuilder->pNew->u.btree.nEq;
133032 UnpackedRecord *pRec = pBuilder->pRec;
133034 tRowcnt a[2]; /* Statistics */
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;
133062 whereKeyStats(pParse, p, pRec, 0, a);
133064 p->zName, nEq-1, (int)a[1]));
133065 *pnRow = a[1];
133074 ** an IN constraint where the right-hand side of the IN operator
133075 ** is a list of values. Example:
133081 ** non-zero.
133083 ** This routine can fail if it is unable to load a collating sequence
133085 ** for a UTF conversion required for comparison. The error is stored
133094 Index *p = pBuilder->pNew->u.btree.pIndex;
133095 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
133096 int nRecValid = pBuilder->nRecValid;
133098 tRowcnt nEst; /* Number of rows for a single term */
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 );
133123 ** Print the content of a WhereTerm object
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);
133160 ** Show the complete content of a WhereClause
133164 for(i=0; i<pWC->nTerm; i++){
133165 whereTermPrint(&pWC->a[i], i);
133172 ** Print a WhereLoop object for debugging purposes
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);
133223 ** Convert bulk memory into a valid WhereLoop that can be passed
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;
133251 ** Deallocate internal memory used by a WhereLoop object
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;
133295 ** Delete a WhereLoop object
133303 ** Free a WhereInfo structure
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;
133328 ** (2) X is a proper subset of Y
133335 ** If X is a proper subset of Y then Y is a better choice and ought
133336 ** to have a lower cost. This routine returns TRUE when that cost
133338 ** was added because if X uses skip-scan less than Y it still might
133339 ** deserve a lower cost even if it is a proper subset of Y.
133346 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
133347 return 0; /* X is not a subset of Y */
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;
133359 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
133368 ** (1) pTemplate costs less than any other WhereLoops that are a proper
133372 ** is a proper subset.
133374 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
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;
133392 ** pTemplate is a proper subset of p */
133394 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
133395 pTemplate->rRun = p->rRun;
133396 pTemplate->nOut = p->nOut + 1;
133408 ** If pX is a WhereLoop that pTemplate can replace, then return the
133412 ** to be added to the list as a new entry, then return a pointer to the
133420 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
133421 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
133424 ** a candidate to replace the other. */
133430 assert( p->rSetup==0 || pTemplate->rSetup==0
133431 || p->rSetup==pTemplate->rSetup );
133434 ** case first. Hence compatible candidate WhereLoops never have a larger
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 */
133480 ** Insert or replace a WhereLoop entry using the template supplied.
133485 ** fewer dependencies than the template. Otherwise a new WhereLoop is
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);
133536 /* There already exists a WhereLoop on the list that is better
133541 whereLoopPrint(pTemplate, pBuilder->pWC);
133550 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
133557 whereLoopPrint(p, pBuilder->pWC);
133562 whereLoopPrint(pTemplate, pBuilder->pWC);
133566 /* Allocate a new WhereLoop to add to the end of the list */
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;
133608 ** and which has a truth probability assigned by one of the likelihood(),
133615 ** TODO --> Perhaps this is something that could be improved by better
133619 ** value corresponds to -1 in LogEst notation, so this means decrement
133623 ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
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 ){
133654 /* If a truth probability is specified using the likelihood() hints,
133656 pLoop->nOut += pTerm->truthProb;
133659 ** guess a reasonable truth probability. */
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;
133678 ** Term pTerm is a vector range comparison operation. The first comparison
133685 ** WHERE a = ? AND (b, c, d) > (?, ?, ?)
133689 ** CREATE INDEX ... ON (a, b, c, d, e)
133697 Index *pIdx, /* The index to be used for a inequality constraint */
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;
133719 /* Check that the LHS of the comparison is a column reference to
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 */
133775 WhereTerm *pTerm; /* A WhereTerm under consideration */
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;
133836 /* Do not allow the upper bound of a LIKE optimization range constraint
133837 ** to mix with a lower range bound from some other source */
133838 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
133841 ** right table of a LEFT JOIN. Only constraints in the ON clause are
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;
133881 ** In this case there is a separate term for each of (x) and (y).
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]);
133993 /* TUNING: If there is no likelihood() value, assume that a
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. */
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 ){
134122 ** Return a bitmask where 1s indicate that the corresponding column of
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);
134139 /* Check to see if a partial index with pPartIndexWhere can be used
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)
134163 ** Add all WhereLoop objects for a single table of the join where the table
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
134170 ** For a full scan, assuming the table (or index) contains nRow rows:
134172 ** cost = nRow * 3.0 // full-table scan
134174 ** cost = nRow * (K+3.0) // scan of non-covering index
134176 ** where K is a value between 1.1 and 3.0 set based on the relative
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.
134190 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount
134192 ** "do the least harm" if the estimates are inaccurate. For example, a
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
134196 ** of a full table scan.
134204 Index sPk; /* A fake index object for the primary key */
134206 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
134212 int b; /* A boolean value */
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 ){
134227 /* An INDEXED BY clause specifies a particular index to use */
134228 pProbe = pSrc->pIBIndex;
134230 pProbe = pTab->pIndex;
134232 /* There is no INDEXED BY clause. Create a fake Index object in local
134234 ** fake index the first in a chain of Index objects with all of the real
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
134375 ** satisfied using just the index, and that do not require a
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
134433 ** function marks a subset of those constraints usable, invokes the
134436 ** A constraint is marked usable if:
134443 ** Argument mPrereq is a mask of tables that must be scanned before the
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 ){
134534 /* A virtual table that is constrained by an IN clause may not
134537 ** (2) Multiple outputs from a single IN value will not merge
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)));
134580 ** Add all WhereLoop objects for a table of the join identified by
134581 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
134584 ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
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);
134645 /* If the call to xBestIndex() with all terms enabled produced a plan
134646 ** that does not require any source tables (IOW: a plan with mBest==0),
134650 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
134651 int seenZero = 0; /* True if a plan with no prereqs seen */
134663 mBestNoIn = pNew->prereq & ~mPrereq;
134678 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
134689 if( pNew->prereq==mPrereq ){
134695 /* If the calls to xBestIndex() in the above loop did not find a plan
134697 ** usable), make a call here with all source tables disabled */
134705 /* If the calls to xBestIndex() have so far failed to find a plan
134707 ** operator, make a final call to obtain one here. */
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;
134771 tempWC.a = pOrTerm;
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) ){
134808 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
134809 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
134810 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
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));
134822 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
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;
134900 ** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
134902 ** (or GROUP BY) without requiring a separate sort operation. Return N:
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 */
134930 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
134938 int iColumn; /* A column number within table iCur */
134940 WhereTerm *pTerm; /* A single term of the WHERE clause */
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:
134953 ** (a) All index columns match with WHERE_COLUMN_EQ.
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
134963 ** UNIQUE since a UNIQUE column or index can have multiple rows that
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.
134967 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
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;
135000 /* Mark off any ORDER BY term X that is a column in the table of
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;
135068 ** If the current term is a column of an ((?,?) IN (SELECT...))
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;
135149 ** Sort order is irrelevant for a GROUP BY clause. */
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;
135209 ** the planner assumes that the specified pOrderBy list is actually a GROUP
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;
135259 /* TUNING: Estimated cost of a full external sort, where N is
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.
135295 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
135307 LogEst mxCost = 0; /* Maximum cost of a set of paths */
135308 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
135320 pParse = pWInfo->pParse;
135321 db = pParse->db;
135322 nLoop = pWInfo->nLevel;
135324 ** For 2-way joins, the 5 best paths are followed.
135327 assert( nLoop<=pWInfo->pTabList->nSrc );
135328 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
135333 ** will invoke this function a second time, passing the estimate as the
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
135366 /* Seed the search with a single WherePath containing zero WhereLoops.
135371 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
135376 ** in this case the query may return a maximum of one row, the results
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 ){
135406 /* At this point, pWLoop is a candidate to be the next loop.
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' : '?');
135535 /* pWLoop is a winner. Add it to the set of best so far */
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], ¬Used);
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;
135667 ** Most queries use only a single table (they are not joins) and have
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;
135707 /* TUNING: Cost of a rowid lookup is 10 */
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;
135732 /* TUNING: Cost of a unique index lookup is 15 */
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.
135783 ** The return value is a pointer to an opaque structure that contains
135790 ** The basic idea is to do a nested loop, one loop for each table in
135791 ** the FROM clause of a select. (INSERT and UPDATE statements are the
135792 ** same as a SELECT with only a single table in the FROM clause.) For
135800 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
135804 ** end |-- by sqlite3WhereEnd()
135808 ** appear in the FROM clause if a different order is better able to make
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
135826 ** refer to those indices, a complete table scan can be avoided and the
135833 ** loops are evaluated and if false a jump is made around all subsequent
135834 ** inner loops (or around the "..." if the test occurs within the inner-
135849 ** move the row2 cursor to a null row
135856 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
135857 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
135871 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
135882 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
135886 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
135887 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
135903 db = pParse->db;
135907 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
135908 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
135918 ** bits in a Bitmask
135920 testcase( pTabList->nSrc==BMS );
135921 if( pTabList->nSrc>BMS ){
135922 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
135926 /* This function normally generates a nested loop for all tables in
135931 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
135934 ** return value. A single allocation is used to store the WhereInfo
135935 ** struct, the contents of WhereInfo.a[], the WhereClause structure
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;
135988 /* Assign a bit from the bitmask to every term in the FROM clause.
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.
135994 ** Knowing the bitmask for all tables to the left of a left join is
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;
136239 /* iAuxArg is always set if to a positive value if ONEPASS is possible */
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
136245 ** WITHOUT ROWID table. No need for a separate index */
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;
136301 ** loop below generates code for a single nested loop of the VM
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);
136481 ** yield a significant performance boost.
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
136541 ** a legal notice, here is a blessing:
136551 ** this template to construct a parser. The "lemon" program inserts text
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
136578 ** Indicate that sqlite3ParserFree() will never be called with a null
136604 ** LIMIT clause of a SELECT statement.
136612 ** An instance of the following structure describes the event of a
136613 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
136616 ** UPDATE ON (a,b,c)
136618 ** Then the "b" IdList records the list "a,b,c".
136620 struct TrigEvent { int a; IdList * b; };
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 &&
136654 /* This is a utility routine used to set the ExprSpan.zStart and
136659 pOut->zStart = pStart->z;
136660 pOut->zEnd = &pEnd->z[pEnd->n];
136663 /* Construct a new Expr object from a single identifier. Use the
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];
136690 /* This routine constructs a binary expression node out of two ExprSpan
136691 ** objects and uses the result to populate a new ExprSpan object.
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);
136712 /* Construct an expression node for a unary postfix operator
136720 pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
136721 pOperand->zEnd = &pPostOp->z[pPostOp->n];
136724 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
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;
136735 /* Construct an expression node for a unary prefix operator
136744 pOut->zStart = pPreOp->z;
136745 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
136746 pOut->zEnd = pOperand->zEnd;
136749 /* Add a single new term to an ExprList that is used to store a
136751 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
136752 ** error while parsing a legacy schema.
136763 && pParse->db->init.busy==0
136766 pIdToken->n, pIdToken->z);
136773 ** in a format understandable to "makeheaders". This section is blank unless
136774 ** "lemon" is run with the "-m" command-line option.
136778 /* The next sections is a series of control #defines.
136781 ** that represent terminal and non-terminal symbols.
136784 ** YYNOCODE is a number of type YYCODETYPE that is not used for
136787 ** (also known as: "terminal symbols") have fall-back
136791 ** YYACTIONTYPE is the data type used for "action codes" - numbers
136795 ** symbols. Background: A "minor type" is a semantic
136796 ** value associated with a terminal or non-terminal
136799 ** Each non-terminal can have a different minor type.
136804 ** This is typically a union of many types, one of
136809 ** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
136810 ** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
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
136874 ** to a macro that can assist in verifying code coverage. For production
136885 ** functions that take a state number and lookahead value and return an
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
136900 ** N == YY_ERROR_ACTION A syntax error has occurred.
136907 ** The action table is constructed as a single large table named yy_action[].
136910 ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
136913 ** The (A) formula is preferred. The B formula is used instead if:
136922 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
136923 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
136929 ** yy_action[] A single table containing all actions.
136930 ** yy_lookahead[] A table containing the lookahead for each entry in
136935 ** shifting non-terminals after a reduce.
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 *****************************/
137400 ** If a construct like the following:
137404 ** appears in the grammar, then ID becomes a fallback token for X, Y,
137409 ** This feature can be used, for example, to cause some keywords in a language
137487 /* The following structure represents a single element of the
137499 ** After the "shift" half of a SHIFTREDUCE action, the stateno field
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 */
137522 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
137542 ** Turn parser tracing on by giving a stream to which to write the trace
137543 ** and a prompt to preface each trace message. Tracing is turned off
137548 ** <li> A FILE* to which trace output should be written.
137550 ** <li> A prefix string written at the beginning of every
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;
138015 /* Initialize a new parser that has already been allocated.
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];
138044 ** This function allocates a new parser.
138045 ** The only argument is a pointer to a function which works like
138049 ** A pointer to the function used to allocate memory.
138052 ** A pointer to a parser. This pointer is used in subsequent calls
138065 ** associated with a symbol. The symbol can be either a terminal
138067 ** a pointer to the value to be deleted. The code used to do the
138078 /* Here is inserted the actions which take place when a
138079 ** terminal or non-terminal is destroyed. This can happen
138080 ** when the symbol is popped from the stack during a
138081 ** reduce or during error processing or when a parser is
138084 ** Note: during a reduce, the only symbols destroyed are those
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);
138170 ** If there is a destructor routine associated with the token which
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);
138201 ** Deallocate and destroy a parser. Destructors are called for
138205 ** is defined in a %include section of the input grammar) then it is
138221 ** Return the peak depth of the stack for a parser.
138226 return pParser->yyhwm;
138231 ** Find the appropriate action for a parser given the terminal
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);
138345 ** Print tracing information for a SHIFT action
138352 yyTracePrompt,yyTokenName[yypParser->yytos->major],
138356 yyTracePrompt,yyTokenName[yypParser->yytos->major]);
138365 ** Perform a shift action.
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 },
138747 ** Perform a reduce action and the shift that must immediately
138759 yymsp = yypParser->yytos;
138768 /* Check that the stack is large enough to grow by a single entry
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;
138795 /* Beginning here are the reduction cases. A typical example
138806 { pParse->explain = 1; }
138809 { pParse->explain = 2; }
138815 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy194);}
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);
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…
139119 ** objects in a complex query.
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);
139445 /* When doing a nested parse, one can include terms in an expression
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);
139633 /* pRHS cannot be NULL because a malloc error would have been detected
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[…
140034 /* It is not possible for a REDUCE to be followed by an error */
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);
140072 ** The following code executes when a syntax error first occurs.
140084 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
140103 yypParser->yyerrcnt = -1;
140105 assert( yypParser->yytos==yypParser->yystack );
140114 ** The first argument is a pointer to a structure obtained from
140123 ** <li> A pointer to the parser (an opaque structure.)
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);
140183 /* A syntax error has occurred.
140191 ** * Begin popping the stack until we enter a state where
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]);
140295 ** a legal notice, here is a blessing:
140305 ** individual tokens and sends those tokens one-by-one over to the
140313 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
140314 ** using a lookup table, whereas a switch() directly on c uses a binary search.
140316 ** a lookup table is used, all of the classes need to be small integers and
140320 #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
140330 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
140335 #define CC_SLASH 16 /* '/'. / or c-style comment */
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 */
140426 ** it is a keyword. If it is a keyword, the token code of that keyword is
140427 ** returned. If the input is not a keyword, TK_ID is returned.
140429 ** The implementation of this routine was generated by a program,
140431 ** The output of the mkkeywordhash.c program is written into a file
140443 ** The code in this file implements a function that determines whether
140444 ** or not a given identifier is really an SQL keyword. The same thing
140445 ** might be implemented more directly using a hand-written hash table.
140463 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
140465 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
140466 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
140467 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
140468 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
140469 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
140471 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
140472 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
140473 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
140474 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
140476 'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
140477 'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
140478 'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
140479 'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
140480 'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
140481 'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
140482 'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
140483 'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
140484 'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
140485 'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
140487 'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
140489 'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
140491 'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
140492 'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
140493 'V','I','E','W','I','N','I','T','I','A','L','L','Y',
140495 /* aKWHash[i] is the hash value for the i-th keyword */
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 */
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){
140739 ** If X is a character that can be used in an identifier then
140742 ** For ASCII, any character with the high-order bit set is
140743 ** allowed in an identifier. For 7-bit characters,
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 */
140938 /* If the next character is a digit, this is a floating point
140962 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
141020 ** but z[i] is a character not allowed within keywords, so this must
141042 /* If it is not a BLOB literal, then it must be an ID, since no
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 );
141214 ** a legal notice, here is a blessing:
141240 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
141261 ** Return TRUE if the given SQL string ends in a semicolon.
141267 ** This implementation uses a state machine with 8 states:
141269 ** (0) INVALID We have not yet seen a non-whitespace character.
141275 ** (2) NORMAL We are in the middle of statement which ends with a single
141279 ** a statement.
141281 ** (4) CREATE The keyword CREATE has been seen at the beginning of a
141285 ** (5) TRIGGER We are in the middle of a trigger definition that must be
141286 ** ended by a semicolon, the keyword END, and another semicolon.
141289 ** the end of a trigger definition.
141292 ** of a trigger definition.
141297 ** (0) tkSEMI A semicolon.
141306 ** Whitespace never causes a state transition and is always ignored.
141307 ** This means that a SQL string of all whitespace is invalid.
141310 ** to recognize the end of a trigger can be omitted. All we have to do
141311 ** is look for a semicolon that is not part of an string or comment.
141318 /* A complex statement machine used to detect the end of a CREATE TRIGGER
141335 ** used to detect the end of a statement is much simpler
141355 case ';': { /* A semicolon */
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);
141507 ** a legal notice, here is a blessing:
141528 ** a legal notice, here is a blessing:
141561 ** a legal notice, here is a blessing:
141594 ** a legal notice, here is a blessing:
141632 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
141638 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
141639 ** a pointer to the to the sqlite3_version[] string constant.
141643 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
141644 ** pointer to a string constant whose value is the same as the
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
141683 ** If the following global variable points to a string which is the
141684 ** name of a directory, then that directory will be used to store
141692 ** If the following global variable points to a string which is the
141693 ** name of a directory, then that directory will be used to store
141694 ** all database files specified with a relative pathname.
141709 ** This routine is a no-op except on its very first call for the process,
141710 ** or for the first call after a call to sqlite3_shutdown.
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
141759 ** If the system is so sick that we are unable to allocate a mutex,
141770 ** MutexAlloc() is called for a static mutex prior to initializing the
141771 ** malloc subsystem - this implies that the allocation of a static
141808 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
141846 ** mutex to prevent a resource leak.
141849 sqlite3GlobalConfig.nRefInitMutex--;
141857 /* The following is just a sanity check to make sure SQLite has
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.
141962 /* Mutex configuration options are only available in a threadsafe
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
142009 ** single argument which is a pointer to an instance of the
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
142018 ** single argument which is a pointer to an instance of the
142026 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
142027 ** single argument of type int, interpreted as a boolean, which enables
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
142054 ** a single parameter which is a pointer to an integer and writes into
142065 /* no-op */
142075 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
142076 ** single argument which is a pointer to an sqlite3_pcache_methods2
142077 ** object. This object specifies the interface to a custom page cache
142083 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
142084 ** single argument which is a pointer to an sqlite3_pcache_methods2
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
142145 /* Record a pointer to the logger function and its first argument.
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
142176 ** option takes a single integer argument which is interpreted as a
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
142247 ** Set up the lookaside buffers for a database connection.
142260 if( db->lookaside.nOut ){
142264 ** allocating a new one so we don't have to have space for
142267 if( db->lookaside.bMalloced ){
142268 sqlite3_free(db->lookaside.pStart);
142270 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
142271 ** than a pointer to be useful.
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;
142314 ** Return the mutex associated with a database connection.
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;
142584 ** copies of a single function are created when create_function() is called
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);
142664 /* If a transaction is open, the disconnectAllVtab() call above
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);
142690 /* Convert the connection into a zombie and then close it.
142692 db->magic = SQLITE_MAGIC_ZOMBIE;
142698 ** Two variations on the public interface for closing a database
142702 ** version forces the connection to become a zombie if there are
142713 ** Furthermore, if database connection db is a zombie (meaning that there
142714 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
142726 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
142727 sqlite3_mutex_leave(db->mutex);
142733 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
142737 /* If a transaction is open, roll it back. This also ensures that if
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);
142887 ** Return a static string containing the name corresponding to the error code
142996 ** Return a static string that describes the kind of error specified in the
143009 /* SQLITE_READONLY */ "attempt to write a readonly database",
143031 /* SQLITE_NOTADB */ "file is not a database",
143051 ** This routine implements a busy callback that sleeps and tries
143052 ** again until a timeout value is reached. The timeout value is
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);
143098 ** This routine is called when an operation failed with a lock.
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);
143169 ** This routine installs a default busy handler that waits for the
143178 db->busyTimeout = ms;
143190 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
143195 db->u1.isInterrupted = 1;
143202 ** that if a malloc() fails in sqlite3_create_function(), an error code
143220 assert( sqlite3_mutex_held(db->mutex) );
143225 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
143262 ** and there are active VMs, then return SQLITE_BUSY. If a function
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);
143284 /* If an older version of the function with a configured destructor is
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);
143390 ** Declare that a function has been overloaded by a virtual table.
143392 ** If the function already exists as a regular global function, then
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.
143398 ** A global function must exist in order for name resolution to work
143409 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
143413 sqlite3_mutex_enter(db->mutex);
143419 sqlite3_mutex_leave(db->mutex);
143425 ** Register a trace function. The pArg from the previously registered trace
143428 ** A NULL trace function means that no tracing is executes. A non-NULL
143429 ** trace is a pointer to a function that is invoked at the start of each
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);
143477 ** Register a profile function. The pArg from the previously registered
143480 ** A NULL profile function means that no profiling is executes. A non-NULL
143481 ** profile is a pointer to a function that is invoked at the conclusion of
143497 sqlite3_mutex_enter(db->mutex);
143498 pOld = db->pProfileArg;
143499 db->xProfile = xProfile;
143500 db->pProfileArg = pArg;
143501 sqlite3_mutex_leave(db->mutex);
143508 ** Register a function to be invoked when a transaction commits.
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);
143534 ** Register a callback to be invoked each time a row is updated,
143550 sqlite3_mutex_enter(db->mutex);
143551 pRet = db->pUpdateArg;
143552 db->xUpdateCallback = xCallback;
143553 db->pUpdateArg = pArg;
143554 sqlite3_mutex_leave(db->mutex);
143559 ** Register a callback to be invoked each time a transaction is rolled
143575 sqlite3_mutex_enter(db->mutex);
143576 pRet = db->pRollbackArg;
143577 db->xRollbackCallback = xCallback;
143578 db->pRollbackArg = pArg;
143579 sqlite3_mutex_leave(db->mutex);
143585 ** Register a callback to be invoked each time a row is updated,
143595 sqlite3_mutex_enter(db->mutex);
143596 pRet = db->pPreUpdateArg;
143597 db->xPreUpdateCallback = xCallback;
143598 db->pPreUpdateArg = pArg;
143599 sqlite3_mutex_leave(db->mutex);
143628 ** a database after committing a transaction if there are nFrame or
143629 ** more frames in the log file. Passing zero or a negative value as the
143633 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
143655 ** Register a callback to be invoked each time a transaction is written
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
143758 ** If a transaction is open on the database being checkpointed, this
143759 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
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
143800 ** a temporary file for transient pager files and statement journals.
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
143871 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
143872 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
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);
143892 /* A malloc() may have failed within the call to sqlite3_value_text16()
143893 ** above. If this is the case, then the db->mallocFailed flag needs to
143899 sqlite3_mutex_leave(db->mutex);
143906 ** passed to this function, we assume a malloc() failed during sqlite3_open().
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;
143931 ** Return a string that describes the kind of error specified in the
143940 ** Create a new collating function for database "db". The name is zName
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 ){
143983 /* If collation sequence pColl was created directly by a call to
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 */
144074 ** Change the value of a limit. Report the old value.
144075 ** If an invalid limit index is supplied, report -1.
144079 ** A new lower limit does not shrink existing constructs.
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
144133 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
144134 ** query parameter. The second argument contains the URI (or non-URI filename)
144142 ** point to a buffer containing the name of the file to open. It is the
144147 ** may be set to point to a buffer containing an English language error
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. */
144193 ** file://host/path or file:////host/path. But 5 leading slashes is a
144194 ** common error, we are told, so we handle it as a special case. */
144206 iIn-7, &zUri[7]);
144219 ** 0: Parsing file-name.
144220 ** 1: Parsing name section of a name=value query parameter.
144221 ** 2: Parsing value section of a name=value query parameter.
144249 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
144257 if( zFile[iOut-1]==0 ){
144259 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
144383 ** This routine does the work of opening a database on behalf of
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
144510 ** conversions. The only error that can occur here is a malloc() failure.
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;
144702 /* Opening a db handle. Fourth parameter is passed 0. */
144729 ** Open a new database handle.
144739 const char *filename, /* Database filename (UTF-8) */
144749 ** Open a new database handle.
144755 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
144769 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
144788 ** Register a new collation sequence with the database handle db.
144801 ** Register a new collation sequence with the database handle db.
144816 sqlite3_mutex_enter(db->mutex);
144817 assert( !db->mallocFailed );
144820 sqlite3_mutex_leave(db->mutex);
144826 ** Register a new collation sequence with the database handle db.
144841 sqlite3_mutex_enter(db->mutex);
144842 assert( !db->mallocFailed );
144843 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
144849 sqlite3_mutex_leave(db->mutex);
144855 ** Register a collation sequence factory callback with the database handle
144866 sqlite3_mutex_enter(db->mutex);
144867 db->xCollNeeded = xCollNeeded;
144868 db->xCollNeeded16 = 0;
144869 db->pCollNeededArg = pCollNeededArg;
144870 sqlite3_mutex_leave(db->mutex);
144876 ** Register a collation sequence factory callback with the database handle
144887 sqlite3_mutex_enter(db->mutex);
144888 db->xCollNeeded = 0;
144889 db->xCollNeeded16 = xCollNeeded16;
144890 db->pCollNeededArg = pCollNeededArg;
144891 sqlite3_mutex_leave(db->mutex);
144898 ** This function is now an anachronism. It used to be used to recover from a
144909 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
144919 return db->autoCommit;
144927 ** 1. Serve as a convenient place to set a breakpoint in a debugger
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.
144980 ** Return meta information about a specific column of a database table.
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;
145055 ** 2. The table is not a view and the column name identified an
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);
145099 ** Sleep for a little while. Return the amount of time slept.
145121 sqlite3_mutex_enter(db->mutex);
145122 db->errMask = onoff ? 0xffffffff : 0xff;
145123 sqlite3_mutex_leave(db->mutex);
145128 ** Invoke the xFileControl method on a particular database.
145137 sqlite3_mutex_enter(db->mutex);
145156 }else if( fd->pMethods ){
145163 sqlite3_mutex_leave(db->mutex);
145199 ** to sqlite3_randomness() will reseed the PRNG using a single call
145210 ** Run a test against a Bitvec object of size. The program argument
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.
145228 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
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
145326 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
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);
145368 ** argument N is a bitmask of optimizations to be disabled. For normal
145369 ** operation N should be 0. The idea is that a test program (like the
145376 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
145383 ** If zWord is a keyword recognized by the parser, then return the
145384 ** number of keywords. Or if zWord is not a keyword, return 0.
145401 ** If sz>0 then allocate a scratch buffer into pNew.
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.
145440 ** too big to test in a reasonable amount of time, so this control is
145441 ** provided to set a small and easily reachable reset value.
145465 db->nMaxSorterMmap = va_arg(ap, int);
145481 ** This test control is used to create imposter tables. "db" is a pointer
145484 ** or off. "tnum" is the root page of the b-tree to which the imposter
145488 ** run a single CREATE TABLE statement to construct the imposter table in
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);
145514 ** This is a utility routine, useful to VFS implementations, that checks
145515 ** to see if a database file was a URI that contained a specific query
145519 ** method of a VFS implementation. The zParam argument is the name of the
145522 ** returns a NULL pointer.
145537 ** Return a boolean value for a query parameter.
145546 ** Return a 64-bit integer value for a query parameter.
145566 return iDb<0 ? 0 : db->aDb[iDb].pBt;
145570 ** Return the filename of the database associated with a database
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;
145603 ** Obtain a snapshot handle for the snapshot of database zDb currently
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);
145709 ** Free a snapshot handle obtained from sqlite3_snapshot_get().
145718 ** Given the name of a compile-time option, return true if that option
145722 ** is not required for a match.
145741 /* Since nOpt is normally in single digits, a linear search is
145742 ** adequate. No need for a binary search. */
145754 ** Return the N-th compile-time option string. If N is out of range,
145755 ** return a NULL pointer.
145774 ** a legal notice, here is a blessing:
145804 ** Head of a linked list of all sqlite3 objects created by this process
145813 ** This function is a complex assert() that verifies the following
145816 ** 1) Each entry in the list has a non-NULL value for either
145819 ** 2) All entries in the list that share a common value for
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;
145865 ** that it is not already a part of the list.
145872 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;