Lines Matching +full:int +full:- +full:clock +full:- +full:stable +full:- +full:broken

17 ** language. The code for the "sqlite3" command-line shell is also in a
20 ** The content in this amalgamation comes from Fossil check-in
51 ** NO_TEST - The branches on this line are not
56 ** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
60 ** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
64 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
69 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
144 ** 2015-03-02
182 ** large file support, or if the OS is windows, these should be no-ops.
188 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
197 ** on 2008-11-28.) These days, all Linux kernels support large files, so
214 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
221 ** so the GCC_VERSION macro will be set to a correct non-zero value even
274 ** some MinGW-specific macros). When compiling for MinGW, either the
297 /* Optionally #include a user-defined header, whereby compilation options
315 ** 2001-09-15
326 ** presents to client programs. If a C-function, structure, datatype,
337 ** The official C-language API documentation for SQLite is derived
382 ** that require non-default calling conventions.
407 ** These no-op macros are used in front of interfaces to mark those
409 ** should not use deprecated interfaces - they are supported for backwards
433 ** CAPI3REF: Compile-Time Library Version Numbers
449 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
451 ** a string which identifies a particular check-in of SQLite
453 ** string contains the date and time of the check-in (UTC) and a SHA1
454 ** or SHA3-256 hash of the entire source tree. If the source code has
464 #define SQLITE_SOURCE_ID "2024-08-13 09:16:08 c9c2ab54ba1f5f46360f1b4f35d849cd3f080e6fc2b6c60e…
467 ** CAPI3REF: Run-Time Library Version Numbers
501 SQLITE_API int sqlite3_libversion_number(void);
504 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
513 ** returning the N-th compile time option string. ^If N is out of range,
526 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
527 SQLITE_API const char *sqlite3_compileoption_get(int N);
538 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
556 ** This interface only reports on the compile-time mutex setting
562 ** sqlite3_threadsafe() function shows only the compile-time setting of
563 ** thread safety, not any run-time changes to that setting made by
569 SQLITE_API int sqlite3_threadsafe(void);
588 ** CAPI3REF: 64-Bit Integer Types
591 ** Because there is no cross-platform way to specify 64-bit integer types
592 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
599 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
614 typedef long long int sqlite_int64;
615 typedef unsigned long long int sqlite_uint64;
622 ** substitute integer for floating-point.
664 ** argument is a harmless no-op.
666 SQLITE_API int sqlite3_close(sqlite3*);
667 SQLITE_API int sqlite3_close_v2(sqlite3*);
674 typedef int (*sqlite3_callback)(void*,int,char**, char**);
677 ** CAPI3REF: One-Step Query Execution Interface
685 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
686 ** semicolon-separate SQL statements passed into its 2nd argument,
708 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
740 SQLITE_API int sqlite3_exec(
743 int (*callback)(void*,int,char**,char**), /* Callback function */
760 /* beginning-of-error-codes */
791 /* end-of-error-codes */
799 ** these result codes are too coarse-grained. They do not provide as
905 ** [sqlite3_open_v2()] has historically be a no-op and might become an
961 ** read-only media and cannot be changed even by processes with
1023 ** (Third-party VFS implementations might also make the distinction
1058 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1082 ** requested lock, then the call to xLock() is a no-op.
1085 ** to xUnlock() is a no-op.
1098 ** locking strategy (for example to use dot-file locks), to inquire
1145 ** fails to zero-fill short reads might seem to work. However,
1146 ** failure to zero-fill short reads will eventually lead to
1151 int iVersion;
1152 int (*xClose)(sqlite3_file*);
1153 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1154 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1155 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1156 int (*xSync)(sqlite3_file*, int flags);
1157 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1158 int (*xLock)(sqlite3_file*, int);
1159 int (*xUnlock)(sqlite3_file*, int);
1160 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1161 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1162 int (*xSectorSize)(sqlite3_file*);
1163 int (*xDeviceCharacteristics)(sqlite3_file*);
1165 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1166 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1168 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1170 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1171 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1202 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1204 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1214 ** point to an integer (type int) containing the new chunk-size to use
1216 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1227 ** the [rollback journal] or the [write-ahead log]) for a particular database
1240 ** this file-control is NULL. However, if the database file is being synced
1241 ** as part of a multi-database commit, the argument points to a nul-terminated
1242 ** string containing the transactions super-journal file name. VFSes that
1259 ** anti-virus programs. By default, the windows VFS will retry file read,
1285 ** WAL mode. If the integer is -1, then it is overwritten with the current
1290 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1294 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1295 ** mode. If the integer is -1, then it is overwritten with the current
1296 ** zero-damage mode setting.
1307 ** final bottom-level VFS are written into memory obtained from
1311 ** all file-control actions, there is no guarantee that this will actually
1313 ** pointer in case this file-control is not implemented. This file-control
1317 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1321 ** to a pointer to the top-level VFS.)^
1323 ** upper-most shim only.
1340 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1342 ** of the result string if the string is non-NULL.
1348 ** it is able to override built-in [PRAGMA] statements.
1352 ** file-control may be invoked by SQLite on the database file handle
1354 ** to the connection's busy-handler callback. The argument is of type (void**)
1355 ** - an array of two (void *) values. The first (void *) actually points
1356 ** to a function of type (int (*)(void *)). In order to invoke the connection's
1357 ** busy-handler, this function should be invoked with the second (void *) in
1358 ** the array as the only argument. If it returns non-zero, then the operation
1363 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1373 ** maximum number of bytes that will be used for memory-mapped I/O.
1379 ** file-control is used internally to implement [PRAGMA mmap_size].
1385 ** The argument is a zero-terminated string. Higher layers in the
1387 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1412 ** Applications should <em>not</em> use this file-control.
1461 ** The parameter is a pointer to a 32-bit signed integer that contains
1462 ** the value that M is to be set to. Before returning, the 32-bit signed
1467 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1493 ** file to the database file, but before the *-shm file is updated to
1498 ** whether or not there is a database client in another process with a wal-mode
1500 ** (void*) argument passed with this file-control should be a pointer to a
1501 ** value of type (int). The integer value is set to 1 if the database is a wal
1504 ** the database is not a wal-mode db, or if there is no such connection in any
1514 ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1515 ** purges the contents of the in-memory page cache. If there is an open
1516 ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1594 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1637 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1655 ** consist of a single "-" character followed by no more than
1656 ** 11 alphanumeric and/or "-" characters.
1671 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1691 ** the open of a journal file a no-op. Writes to this journal would
1692 ** also be no-ops, and any attempt to read the journal would return
1694 ** file will be doing page-aligned sector reads and writes in a random
1733 ** flag is never actually used and is not implemented in the built-in
1736 ** non-zero error code if there is an I/O error or if the name of
1738 ** is returned, then non-zero or zero is written into *pResOut to indicate
1752 ** of good-quality randomness into zOut. The return value is
1760 ** a 24-hour day).
1781 int iVersion; /* Structure version number (currently 3) */
1782 int szOsFile; /* Size of subclassed sqlite3_file */
1783 int mxPathname; /* Maximum file pathname length */
1786 void *pAppData; /* Pointer to application-specific data */
1787 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1788 int flags, int *pOutFlags);
1789 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1790 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1791 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1793 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1796 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1797 int (*xSleep)(sqlite3_vfs*, int microseconds);
1798 int (*xCurrentTime)(sqlite3_vfs*, double*);
1799 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1804 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1809 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1896 ** are harmless no-ops.)^
1901 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1924 ** compile-time option, then the automatic calls to sqlite3_initialize()
1933 ** The sqlite3_os_init() routine does operating-system specific
1949 ** (using the [SQLITE_OS_OTHER=1] compile-time
1951 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1956 SQLITE_API int sqlite3_initialize(void);
1957 SQLITE_API int sqlite3_shutdown(void);
1958 SQLITE_API int sqlite3_os_init(void);
1959 SQLITE_API int sqlite3_os_end(void);
1989 ** implementation of an application-defined [sqlite3_os_init()].
1993 ** then this routine returns a non-zero [error code].
1995 SQLITE_API int sqlite3_config(int, ...);
2007 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2014 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2020 ** and low-level memory allocation routines.
2032 ** Note that SQLite comes with several [built-in memory allocators]
2037 ** memory allocator that simulates memory out-of-memory conditions in
2081 void *(*xMalloc)(int); /* Memory allocation function */
2083 void *(*xRealloc)(void*,int); /* Resize an allocation */
2084 int (*xSize)(void*); /* Return the size of an allocation */
2085 int (*xRoundup)(int); /* Round up request size to allocation size */
2086 int (*xInit)(void*); /* Initialize the memory allocator */
2104 ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2119 ** non-zero [error code] if a discontinued or unsupported configuration option
2125 ** [threading mode] to Single-thread. In other words, it disables
2128 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2130 ** value of Single-thread and so [sqlite3_config()] will return
2136 ** [threading mode] to Multi-thread. In other words, it disables
2140 ** are enabled so that SQLite will be safe to use in a multi-threaded
2143 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2144 ** it is not possible to set the Multi-thread [threading mode] and
2159 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2168 ** alternative low-level memory allocation routines to be used in place of
2184 ** type int, interpreted as a boolean, which if true provides a hint to
2193 ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2196 ** disabled, the following SQLite interfaces become non-operational:
2217 ** This configuration option is a no-op if an application-defined page
2220 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2228 ** argument must be either a NULL pointer or a pointer to an 8-byte
2235 ** ^If pMem is NULL and N is non-zero, then each database connection
2238 ** of -1024*N bytes if N is negative, . ^If additional
2251 ** An 8-byte aligned pointer to the memory,
2258 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2266 ** The argument specifies alternative low-level mutex routines to be used
2270 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2283 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2313 ** function with a call signature of void(*)(void*,int,const char*),
2316 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2318 ** passed through as the first parameter to the application-defined logger
2326 ** In a multi-threaded application, the application-defined logger
2330 ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2331 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2348 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2349 ** if that compile-time option is omitted.
2359 ** They are retained for backwards compatibility but are now no-ops.
2365 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2366 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2381 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2388 ** compile-time maximum mmap size set by the
2389 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2391 ** changed to its compile-time default.
2396 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2397 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2413 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2422 ** becomes the [statement journal] spill-to-disk threshold.
2425 ** Or if the threshold is -1, statement journals are always held
2431 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2436 ** of type (int) - the new value of the sorter-reference size threshold.
2440 ** of a table column that its values are likely to be very large - larger
2441 ** than the configured sorter-reference size threshold - then a reference
2447 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2452 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2455 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2457 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2458 ** compile-time option is not set, then the default maximum is 1073741824.
2464 ** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2472 ** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2483 #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
2484 #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
2489 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2490 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2491 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2493 #define SQLITE_CONFIG_URI 17 /* int */
2496 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
2499 #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
2500 #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
2501 #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
2502 #define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
2504 #define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
2506 #define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */
2518 ** non-zero [error code] if a discontinued or unsupported configuration option
2534 ** must be aligned to an 8-byte boundary. ^If the second argument to
2570 ** triggers in the main database schema or in the schemas of ATTACH-ed
2587 ** views in the main database schema or in the schemas of ATTACH-ed
2594 ** [FTS3] full-text search engine extension.
2609 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2611 ** When the first argument to this interface is 1, then only the C-API is
2613 ** this interface is 0, then both the C-API and the SQL function are disabled.
2614 ** If the first argument is -1, then no changes are made to state of either the
2615 ** C-API or the SQL function.
2638 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2641 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2642 ** have been disabled - 0 if they are not disabled, 1 if they are.
2665 ** behavior. The first parameter passed to this operation is an integer -
2669 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2727 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2736 ** the legacy [double-quoted string literal] misfeature for DML statements
2738 ** default value of this setting is determined by the [-DSQLITE_DQS]
2739 ** compile-time option.
2745 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2747 ** default value of this setting is determined by the [-DSQLITE_DQS]
2748 ** compile-time option.
2775 ** created database file to have a schema format version number (the 4-byte
2802 ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2817 ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2827 #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
2828 #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
2829 #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
2830 #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2831 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2832 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
2833 #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
2834 #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2835 #define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
2836 #define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
2837 #define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */
2838 #define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */
2839 #define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */
2840 #define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */
2841 #define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */
2842 #define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */
2843 #define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */
2844 #define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */
2845 #define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */
2856 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2863 ** has a unique 64-bit signed
2939 ** or DELETE is greater than the maximum value supported by type "int", then
2944 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2966 ** any changes performed by sub-triggers, as the sqlite3_changes()
2967 ** value will be saved and restored after each sub-trigger has run.)^
2989 SQLITE_API int sqlite3_changes(sqlite3*);
3001 ** connection exceeds the maximum value supported by type "int", then
3031 SQLITE_API int sqlite3_total_changes(sqlite3*);
3035 ** CAPI3REF: Interrupt A Long-Running Query
3041 ** or Ctrl-C where the user wants a long query operation to halt
3066 ** SQL statements is a no-op and has no effect on SQL statements
3074 SQLITE_API int sqlite3_is_interrupted(sqlite3*);
3079 ** These routines are useful during command-line input to determine if the
3085 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3100 ** then the return value from sqlite3_complete16() will be non-zero
3103 ** The input to [sqlite3_complete()] must be a zero-terminated
3104 ** UTF-8 string.
3106 ** The input to [sqlite3_complete16()] must be a zero-terminated
3107 ** UTF-16 string in native byte order.
3109 SQLITE_API int sqlite3_complete(const char *sql);
3110 SQLITE_API int sqlite3_complete16(const void *sql);
3114 ** KEYWORDS: {busy-handler callback} {busy handler}
3136 ** ^If the callback returns non-zero, then another attempt
3171 SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3194 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3212 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3214 ** to zero-terminated strings that contain the names of the columns.
3216 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3228 ** -----------------------
3250 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3269 SQLITE_API int sqlite3_get_table(
3273 int *pnRow, /* Number of result rows written here */
3274 int *pnColumn, /* Number of result columns written here */
3282 ** These routines are work-alikes of the "printf()" family of functions
3286 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3287 ** See the [built-in printf()] documentation for details.
3310 ** guarantees that the buffer is always zero-terminated. ^The first
3313 ** written will be n-1 characters.
3317 ** See also: [built-in printf()], [printf() SQL function]
3321 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3322 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3329 ** does not include operating-system specific [VFS] implementation. The
3340 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3341 ** of a signed 32-bit integer.
3346 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3351 ** might result if sqlite3_free() is called with a non-NULL pointer that
3371 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3372 ** of a 32-bit signed integer.
3387 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3399 SQLITE_API void *sqlite3_malloc(int);
3401 SQLITE_API void *sqlite3_realloc(void*, int);
3411 ** routines, which form the built-in memory allocation subsystem.
3416 ** value of [sqlite3_memory_used()] since the high-water mark
3423 ** ^The memory high-water mark is reset to the current value of
3426 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3430 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3433 ** CAPI3REF: Pseudo-Random Number Generator
3435 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3438 ** the built-in random() and randomblob() SQL functions. This interface allows
3449 ** non-NULL P then the pseudo-randomness is generated
3453 SQLITE_API void sqlite3_randomness(int N, void *P);
3456 ** CAPI3REF: Compile-Time Authorization Callbacks
3487 ** to the callback are either NULL pointers or zero-terminated strings
3515 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3534 ** statement might be re-prepared during [sqlite3_step()] due to a
3544 SQLITE_API int sqlite3_set_authorizer(
3546 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3580 ** is the name of the inner-most trigger or view that is responsible for
3582 ** top-level SQL code.
3632 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3636 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3638 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3643 ** the original statement text and an estimate of wall-clock time
3663 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3685 ** interface by using the X argument when X begins with "--" and invoking
3692 ** X argument points to a 64-bit integer which is approximately
3722 ** M argument should be the bitwise OR-ed combination of
3745 SQLITE_API int sqlite3_trace_v2(
3748 int(*xCallback)(unsigned,void*,void*,void*),
3774 ** ^If the progress callback returns non-zero, the operation is
3791 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3798 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3799 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3810 ** ^The default encoding will be UTF-8 for databases created using
3812 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3826 ** <dd>The database is opened in read-only mode. If the database does
3834 ** read-write mode fails due to OS-level permissions, an attempt is
3835 ** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3837 ** read-write.</dd>)^
3853 ** <dd>The database will be opened as an in-memory database. The database
3854 ** is named by the "filename" argument for the purposes of cache-sharing,
3859 ** <dd>The new database connection will use the "multi-thread"
3877 ** this option is a no-op.
3903 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3914 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3915 ** is created for the connection. ^This in-memory database will vanish when
3923 ** on-disk database will be created. ^This private database will be
3933 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3956 ** SQLite and its built-in [VFSes] interpret the
3971 ** ^If "ro" is specified, then the database is opened for read-only
3974 ** "rw", then the database is opened for read-write (but not create)
3978 ** set to "memory" then a pure [in-memory database] that never reads
4005 ** read-only media. ^When immutable is set, SQLite assumes that the
4007 ** privilege, and so the database is opened read-only and all locking
4032 ** <tr><td style="white-space:nowrap">
4036 ** necessary - space characters can be used literally
4039 ** Open file "data.db" in the current directory for read-only access.
4040 ** Regardless of whether or not shared-cache mode is enabled by
4042 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4043 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4044 ** that uses dot-files in place of posix advisory locking.
4052 ** percent sign - "%" - followed by exactly two hexadecimal digits
4054 ** URI filename are interpreted, they are encoded using UTF-8 and all
4056 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4060 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4062 ** characters must be converted to UTF-8 prior to passing them into
4071 SQLITE_API int sqlite3_open(
4072 const char *filename, /* Database filename (UTF-8) */
4075 SQLITE_API int sqlite3_open16(
4076 const void *filename, /* Database filename (UTF-16) */
4079 SQLITE_API int sqlite3_open_v2(
4080 const char *filename, /* Database filename (UTF-8) */
4082 int flags, /* Flags */
4117 ** case or if the value begins with a non-zero number. The
4125 ** 64-bit signed integer and returns that integer, or D if P does not
4130 ** the value) of the N-th query parameter for filename F, or a NULL
4132 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4153 SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4155 SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
4191 ** ^If X is the name of a rollback or WAL-mode journal file that is
4197 ** only. It is not a general-purpose interface.
4241 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4256 int nParam,
4277 ** change the value of the error code. The error-code preserving
4288 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4289 ** text that describes the error, as either UTF-8 or UTF-16 respectively,
4297 ** ^The sqlite3_errstr(E) interface returns the English-language text
4298 ** that describes the [result code] E, as UTF-8, or NULL if E is not an
4308 ** SQL, then the sqlite3_error_offset() function returns -1.
4324 SQLITE_API int sqlite3_errcode(sqlite3 *db);
4325 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4328 SQLITE_API const char *sqlite3_errstr(int);
4329 SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4343 ** The life-cycle of a prepared statement object usually goes like this:
4358 ** CAPI3REF: Run-time Limits
4371 ** set at compile-time by a C preprocessor macro called
4380 ** simply invoke this interface with the third parameter set to -1.
4382 ** Run-time limits are intended for use in applications that manage
4395 ** New run-time limit categories may be added in future releases.
4397 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4400 ** CAPI3REF: Run-Time Limit Categories
4404 ** that can be lowered at run-time using [sqlite3_limit()].
4490 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4513 ** To execute an SQL statement, it must first be compiled into a byte-code
4522 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4523 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4524 ** as a convenience. The UTF-16 interfaces work by converting the
4525 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4532 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4534 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4535 ** and sqlite3_prepare16_v3() use UTF-16.
4541 ** If the caller knows that the supplied string is nul-terminated, then
4544 ** the nul-terminator.
4594 ** ^The specific value of a WHERE-clause [parameter] might influence the
4595 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4597 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4607 SQLITE_API int sqlite3_prepare(
4609 const char *zSql, /* SQL statement, UTF-8 encoded */
4610 int nByte, /* Maximum length of zSql in bytes. */
4614 SQLITE_API int sqlite3_prepare_v2(
4616 const char *zSql, /* SQL statement, UTF-8 encoded */
4617 int nByte, /* Maximum length of zSql in bytes. */
4621 SQLITE_API int sqlite3_prepare_v3(
4623 const char *zSql, /* SQL statement, UTF-8 encoded */
4624 int nByte, /* Maximum length of zSql in bytes. */
4625 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4629 SQLITE_API int sqlite3_prepare16(
4631 const void *zSql, /* SQL statement, UTF-16 encoded */
4632 int nByte, /* Maximum length of zSql in bytes. */
4636 SQLITE_API int sqlite3_prepare16_v2(
4638 const void *zSql, /* SQL statement, UTF-16 encoded */
4639 int nByte, /* Maximum length of zSql in bytes. */
4643 SQLITE_API int sqlite3_prepare16_v3(
4645 const void *zSql, /* SQL statement, UTF-16 encoded */
4646 int nByte, /* Maximum length of zSql in bytes. */
4647 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4656 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4660 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4663 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4679 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4680 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4691 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4703 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4707 ** Note that [application-defined SQL functions] or
4711 ** change the database file through side-effects:
4737 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4739 ** read-only no-op if the table already exists, but
4746 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4758 SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4793 SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4799 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4814 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4836 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4847 ** implementation of [application-defined SQL functions] are protected.
4865 ** is always first parameter to [application-defined SQL functions].
4866 ** The application-defined SQL function implementation will pass this
4916 ** it should be a pointer to well-formed UTF8 text.
4918 ** it should be a pointer to well-formed UTF16 text.
4920 ** it should be a pointer to a well-formed unicode string that is
4924 ** [[byte-order determination rules]] ^The byte-order of
4925 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4942 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4982 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4984 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5014 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5015 SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5017 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5018 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5019 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5020 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5021 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5022 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5023 SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5025 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5026 SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5027 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5028 SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5049 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5056 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5067 ** ^If the value N is out of range or if the N-th parameter is
5069 ** always in UTF-8 encoding even if the named parameter was
5070 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5077 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5087 ** name must be given in UTF-8 even if the original statement
5088 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5095 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5105 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5121 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5129 ** interface returns a pointer to a zero-terminated UTF-8 string
5130 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5131 ** UTF-16 string. ^The first parameter is the [prepared statement]
5142 ** (for example during a conversion from UTF-8 to UTF-16) then a
5150 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5151 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5161 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5170 ** ^The names returned are the original un-aliased names of the
5176 ** ^The left-most column is column 0 for these routines.
5185 ** UTF-16 encoded strings and the other functions return UTF-8.
5188 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5195 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5196 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5197 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5198 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5199 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5200 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5212 ** ^The returned string is always UTF-8 encoded.
5225 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5232 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5233 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5275 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5301 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5317 SQLITE_API int sqlite3_step(sqlite3_stmt*);
5331 ** will return non-zero if previous call to [sqlite3_step](P) returned
5333 ** where it always returns zero since each step of that multi-step
5338 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5347 ** <li> 64-bit signed integer
5348 ** <li> 64-bit IEEE floating point number
5381 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5382 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5383 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5384 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5389 ** or a UTF-8 TEXT result in bytes
5391 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5443 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5445 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5446 ** the string to UTF-8 and then returns the number of bytes.
5448 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5452 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5454 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5455 ** the string to UTF-16 and then returns the number of bytes.
5457 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5468 ** even empty strings, are always zero-terminated. ^The return
5469 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5485 ** [application-defined SQL functions] or [virtual tables], not within
5486 ** top-level application code.
5504 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5525 ** sqlite3_column_text16() is called. A zero-terminator might
5527 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5529 ** to UTF-16.</li>
5530 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5532 ** to UTF-8.</li>
5535 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5566 ** fail if an out-of-memory error occurs during a format conversion.
5567 ** Only the following subset of interfaces are subject to out-of-memory
5578 ** If an out-of-memory error occurs, then the return value from these
5580 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5585 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5586 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5587 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5588 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5589 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5590 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5591 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5592 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5593 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5594 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5614 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5622 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5629 ** object back to its initial state, ready to be re-executed.
5661 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5681 ** connection then application-defined SQL functions must be added
5685 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5686 ** representation, exclusive of the zero-terminator. ^Note that the name
5687 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5693 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5696 ** parameter is less than -1 or greater than 127 then the behavior is
5715 ** deterministic. The built-in [random()] SQL function is an example of a
5726 ** all application-defined SQL functions that do not need to be
5740 ** pointers to C-language functions that implement the SQL function or
5750 ** C-language callbacks that implement the new function. xStep and xFinal
5751 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5753 ** non-NULL, in which case the new function may be used as either an aggregate
5756 ** [user-defined window functions|available here].
5771 ** SQL function is used. ^A function implementation with a non-negative
5780 ** ^Built-in functions may be overloaded by new application-defined functions.
5782 ** ^An application-defined function is permitted to call other
5787 SQLITE_API int sqlite3_create_function(
5790 int nArg,
5791 int eTextRep,
5793 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5794 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5797 SQLITE_API int sqlite3_create_function16(
5800 int nArg,
5801 int eTextRep,
5803 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5804 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5807 SQLITE_API int sqlite3_create_function_v2(
5810 int nArg,
5811 int eTextRep,
5813 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5814 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5818 SQLITE_API int sqlite3_create_window_function(
5821 int nArg,
5822 int eTextRep,
5824 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5827 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5837 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5838 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5839 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5866 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5871 ** [application-defined SQL function]
5872 ** that has side-effects or that could potentially leak sensitive information.
5875 ** modified to invoke the application-defined function in ways that are
5879 ** [application-defined SQL functions], regardless of whether or not they
5883 ** that do not have access to the application-defined functions.
5902 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5904 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5906 ** security-adverse side-effects and information-leaks.
5911 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5912 ** This flag instructs SQLite to omit some corner-case optimizations that
5918 ** a non-zero subtype was specified by the function argument expression.
5922 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5926 ** might become a no-op if the function is used as term in an
5951 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5952 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5953 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5954 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5956 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5968 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5969 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5971 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5972 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5974 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5975 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5978 ** or a UTF-8 TEXT in bytes
5980 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5998 ** implement [application-defined SQL functions] and [virtual tables].
6008 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6009 ** in the native byte-order of the host machine. ^The
6011 ** extract UTF-16 strings as big-endian and little-endian respectively.
6052 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6067 ** fail if an out-of-memory error occurs during a format conversion.
6068 ** Only the following subset of interfaces are subject to out-of-memory
6081 ** If an out-of-memory error occurs, then the return value from these
6083 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6090 SQLITE_API int sqlite3_value_int(sqlite3_value*);
6097 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6098 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6099 SQLITE_API int sqlite3_value_type(sqlite3_value*);
6100 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6101 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6102 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6124 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6131 ** an [application-defined SQL function] argument V. The subtype
6136 ** Every [application-defined SQL function] that invoke this interface
6142 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6157 ** then sqlite3_value_free(V) is a harmless no-op.
6205 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6218 ** the application-defined function is running.
6238 ** These functions may be used by (non-aggregate) SQL functions to
6243 ** regular-expression matching function. The compiled version of the regular
6251 ** value to the application-defined function. ^N is zero for the left-most
6257 ** N-th argument of the application-defined function. ^Subsequent
6283 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6289 ** function parameters that are compile-time constants, including literal
6292 ** The value of the N parameter to these interfaces should be non-negative.
6301 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6302 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6317 ** If P and X are both non-NULL, then the destructor X is invoked with
6320 ** <li> An out-of-memory error occurs during the call to
6358 SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*));
6376 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6392 ** an application-defined function to be the BLOB whose content is pointed
6397 ** interfaces set the result of the application-defined function to be
6401 ** an application-defined function to be a floating point value specified
6409 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6410 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6411 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6416 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6434 ** of the application-defined function to be the 32-bit signed integer
6437 ** of the application-defined function to be the 64-bit signed integer
6441 ** of the application-defined function to be NULL.
6445 ** set the return value of the application-defined function to be
6446 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6447 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6449 ** application-defined function to be a text string in an encoding
6459 ** is non-negative, then as many bytes (not characters) of the text
6460 ** pointed to by the 2nd parameter are taken as the application-defined
6461 ** function result. If the 3rd parameter is non-negative, then it
6468 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6484 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6486 ** byte-order specified by the BOM. ^The byte-order specified by
6487 ** the BOM at the beginning of the text overrides the byte-order
6490 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6501 ** the application-defined function to be a copy of the
6512 ** also associates the host-language pointer P or type T with that
6514 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6522 ** than the one containing the application-defined function that received
6525 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6529 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6530 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6533 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6534 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6537 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6540 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6541 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6542 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6545 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6546 SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6554 ** the result from the [application-defined SQL function] with
6561 ** Every [application-defined SQL function] that invokes this interface
6569 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6572 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6575 SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6584 ** ^The name of the collation is a UTF-8 string
6586 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6649 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6658 SQLITE_API int sqlite3_create_collation(
6661 int eTextRep,
6663 int(*xCompare)(void*,int,const void*,int,const void*)
6665 SQLITE_API int sqlite3_create_collation_v2(
6668 int eTextRep,
6670 int(*xCompare)(void*,int,const void*,int,const void*),
6673 SQLITE_API int sqlite3_create_collation16(
6676 int eTextRep,
6678 int(*xCompare)(void*,int,const void*,int,const void*)
6692 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6693 ** the names are passed as UTF-16 in machine native byte order.
6694 ** ^A call to either function replaces the existing collation-needed callback.
6708 SQLITE_API int sqlite3_collation_needed(
6711 void(*)(void*,sqlite3*,int eTextRep,const char*)
6713 SQLITE_API int sqlite3_collation_needed16(
6716 void(*)(void*,sqlite3*,int eTextRep,const void*)
6753 SQLITE_API int sqlite3_sleep(int);
6760 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6802 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6803 ** &nbsp; TemporaryFolder->Path->Data();
6806 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6819 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6858 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6863 ** the current directory on the sub-platforms of Win32 where that concept is
6867 ** UTF-8 or UTF-16, respectively.
6869 SQLITE_API int sqlite3_win32_set_directory(
6873 SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6874 SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6886 ** CAPI3REF: Test For Auto-Commit Mode
6890 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6894 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6896 ** If certain kinds of errors occur on a statement within a multi-statement
6907 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6927 ** for the N-th database on database connection D, or a NULL pointer of N is
6929 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6937 ** remember the string long-term should make their own copy. Applications that
6939 ** threads should mutex-protect calls to this API and should make their own
6942 SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6951 ** connection D, or if database N is a temporary or in-memory database, then
6956 ** is [DETACH]-ed or until the database connection closes.
6977 ** CAPI3REF: Determine if a database is read-only
6981 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6984 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7000 ** a valid schema, then -1 is returned.
7002 SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
7066 ** ^If the callback on a commit hook function returns non-zero,
7088 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7090 ** hook returning non-zero, just as it would be with any other rollback.
7100 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
7110 ** the schema-name of the attached database that is being autovacuumed,
7118 ** <p>^If there are multiple ATTACH-ed database files that are being
7150 ** &nbsp; unsigned int demonstration_autovac_pages_callback(
7153 ** &nbsp; unsigned int nDbPage,
7154 ** &nbsp; unsigned int nFreePage,
7155 ** &nbsp; unsigned int nBytePerPage
7161 SQLITE_API int sqlite3_autovacuum_pages(
7163 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
7226 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7239 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7240 ** compile-time option is recommended because the
7266 ** shared cache mode should be enabled per-database connection via
7270 ** 32-bit integer is atomic.
7272 ** See Also: [SQLite Shared-Cache Mode]
7274 SQLITE_API int sqlite3_enable_shared_cache(int);
7280 ** of heap memory by deallocating non-essential memory allocations
7282 ** pages to improve performance is an example of non-essential memory.
7285 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7290 SQLITE_API int sqlite3_release_memory(int);
7299 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7304 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7334 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7358 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7359 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7382 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7392 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7396 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7423 ** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7424 ** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7425 ** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7454 SQLITE_API int sqlite3_table_column_metadata(
7461 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7462 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7463 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7475 ** with various operating-system specific extensions added.
7484 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7510 SQLITE_API int sqlite3_load_extension(
7523 ** [extension loading] while evaluating user-entered SQL, the following API
7531 ** ^This interface enables or disables both the C-API
7534 ** to enable or disable only the C-API.)^
7542 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7558 ** &nbsp; int xEntryPoint(
7574 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7580 SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7592 SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7627 int iVersion;
7628 int (*xCreate)(sqlite3*, void *pAux,
7629 int argc, const char *const*argv,
7631 int (*xConnect)(sqlite3*, void *pAux,
7632 int argc, const char *const*argv,
7634 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7635 int (*xDisconnect)(sqlite3_vtab *pVTab);
7636 int (*xDestroy)(sqlite3_vtab *pVTab);
7637 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7638 int (*xClose)(sqlite3_vtab_cursor*);
7639 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7640 int argc, sqlite3_value **argv);
7641 int (*xNext)(sqlite3_vtab_cursor*);
7642 int (*xEof)(sqlite3_vtab_cursor*);
7643 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7644 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7645 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7646 int (*xBegin)(sqlite3_vtab *pVTab);
7647 int (*xSync)(sqlite3_vtab *pVTab);
7648 int (*xCommit)(sqlite3_vtab *pVTab);
7649 int (*xRollback)(sqlite3_vtab *pVTab);
7650 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7651 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7653 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7656 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7657 int (*xRelease)(sqlite3_vtab *pVTab, int);
7658 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7661 int (*xShadowName)(const char*);
7664 int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema,
7665 const char *zTabName, int mFlags, char **pzErr);
7676 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7688 ** expr on the right-hand side can be evaluated (and thus the constraint
7703 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7709 ** non-zero.
7713 ** the right-hand side of the corresponding aConstraint[] is evaluated
7714 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7743 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7772 int nConstraint; /* Number of entries in aConstraint */
7774 int iColumn; /* Column constrained. -1 for ROWID */
7777 int iTermOffset; /* Used internally - xBestIndex should ignore */
7779 int nOrderBy; /* Number of terms in the ORDER BY clause */
7781 int iColumn; /* Column number */
7786 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7789 int idxNum; /* Number used to identify the index */
7791 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7792 int orderByConsumed; /* True if output is already ordered */
7797 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7819 ** ^The left-hand operand of the operator is given by the corresponding
7820 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7823 ** operators have no left-hand operand, and so for those operators the
7832 ** The right-hand operands for each constraint might be accessible using
7833 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7835 ** in the input SQL. If the right-hand operand is another column or an
7839 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7844 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7899 SQLITE_API int sqlite3_create_module(
7905 SQLITE_API int sqlite3_create_module_v2(
7925 SQLITE_API int sqlite3_drop_modules(
7950 int nRef; /* Number of open cursors */
7985 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
8004 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
8039 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
8041 ** read-only access.
8077 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8093 ** and the built-in [zeroblob] SQL function may be used to create a
8094 ** zero-filled blob to read or write using the incremental-blob interface.
8103 SQLITE_API int sqlite3_blob_open(
8109 int flags,
8124 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8136 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8146 ** ^If the blob handle being closed was opened for read-write access, and if
8147 ** the database is in auto-commit mode and there are no other open read-write
8155 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8159 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8175 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8182 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8204 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8211 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8246 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8260 ** ^Names are zero-terminated UTF-8 strings.
8278 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8279 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8291 ** is selected automatically at compile-time. The following
8302 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8307 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8344 ** cases where it really needs one. If a faster non-recursive mutex
8393 ** then any of the four routines behaves as a no-op.
8397 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8400 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8406 ** An instance of this structure defines the low-level routines
8456 ** xMutexInit() must be no-ops.
8470 int (*xMutexInit)(void);
8471 int (*xMutexEnd)(void);
8472 sqlite3_mutex *(*xMutexAlloc)(int);
8475 int (*xMutexTry)(sqlite3_mutex *);
8477 int (*xMutexHeld)(sqlite3_mutex *);
8478 int (*xMutexNotheld)(sqlite3_mutex *);
8502 ** the routine should return 1. This seems counter-intuitive since
8506 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8511 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8512 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8522 ** next. Applications that override the built-in mutex logic must be
8538 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8553 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8559 ** CAPI3REF: Low-Level Control Of Database Files
8599 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8618 SQLITE_API int sqlite3_test_control(int op, ...);
8674 ** by enclosing in double-quotes) so as not to confuse the parser.
8679 ** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8682 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8688 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8700 ** <li> Put all identifier names inside double-quotes. This is the official
8711 ** compile-time options. For example, "VACUUM" is not a keyword if
8712 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8715 SQLITE_API int sqlite3_keyword_count(void);
8716 SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8717 SQLITE_API int sqlite3_keyword_check(const char*,int);
8723 ** An instance of the sqlite3_str object contains a dynamically-sized
8747 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8787 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8792 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8793 ** S must contain at least N non-zero bytes of content. To append a
8794 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8798 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8801 ** single-byte character C onto the end of [sqlite3_str] object X.
8813 SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8815 SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8827 ** [SQLITE_NOMEM] following any out-of-memory error, or
8834 ** zero-termination byte.
8847 SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8848 SQLITE_API int sqlite3_str_length(sqlite3_str*);
8869 ** SQLITE_OK on success and a non-zero [error code] on failure.
8872 ** be represented by a 32-bit integer, then the values returned by
8877 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8878 SQLITE_API int sqlite3_status64(
8879 int op,
8882 int resetFlag
8890 ** These integer constants designate various run-time status parameters
8898 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8983 ** non-zero [error code] on failure.
8987 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8999 ** The [sqlite3_db_status()] interface will return a non-zero error code
9009 ** satisfied using lookaside memory. Only the high-water value is meaningful;
9017 ** Only the high-water value is meaningful;
9025 ** Only the high-water value is meaningful;
9048 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9088 ** to disk all at once. When pages spill mid-transaction, that introduces
9140 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9159 ** A non-zero value in this counter may indicate an opportunity to
9165 ** A non-zero value in this counter may indicate an opportunity to
9194 ** step was bypassed because a Bloom filter returned not-found. The
9264 ** The built-in page cache is recommended for most uses.
9279 ** built-in default page cache is used instead of the application defined
9311 ** false if it is used for an in-memory database. The cache implementation
9322 ** suggested maximum cache-size (number of pages stored by) the cache
9368 ** as its second argument. If the third parameter, discard, is non-zero,
9402 ** is not obligated to free any memory, but well-behaved implementations should
9407 int iVersion;
9409 int (*xInit)(void*);
9411 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9412 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9413 int (*xPagecount)(sqlite3_pcache*);
9414 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9415 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9431 int (*xInit)(void*);
9433 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9434 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9435 int (*xPagecount)(sqlite3_pcache*);
9436 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9437 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9461 ** for copying in-memory databases to or from persistent files.
9467 ** ^The source database is read-locked only while it is being read;
9501 ** there is already a read or read-write transaction open on the
9533 ** <li> the destination database was opened read-only, or
9534 ** <li> the destination database is using write-ahead-log journaling
9536 ** <li> the destination database is an in-memory database and the
9540 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9541 ** the [sqlite3_busy_handler | busy-handler function]
9543 ** busy-handler returns non-zero before the lock is available, then
9564 ** sqlite3_backup_step(), the source database may be modified mid-way
9581 ** active write-transaction on the destination database is rolled back.
9588 ** ^If an out-of-memory condition or IO error occurred during any prior
9648 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9649 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9650 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9651 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9657 ** ^When running in shared-cache mode, a database operation may fail with
9658 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9659 ** individual tables within the shared-cache cannot be obtained. See
9660 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9664 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9668 ** ^Shared-cache locks are released when a database connection concludes
9672 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9682 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9688 ** ^If the blocked connection is attempting to obtain a write-lock on a
9689 ** shared-cache table, and more than one other connection currently holds
9690 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9693 ** ^(There may be at most one unlock-notify callback registered by a
9695 ** blocked connection already has a registered unlock-notify callback,
9698 ** unlock-notify callback is canceled. ^The blocked connections
9699 ** unlock-notify callback may also be canceled by closing the blocked
9702 ** The unlock-notify callback is not reentrant. If an application invokes
9703 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9711 ** When an unlock-notify callback is registered, the application provides a
9715 ** an unlock-notify callback is a pointer to an array of void* pointers,
9719 ** more than one blocked connection that has registered for an unlock-notify
9729 ** Assuming that after registering for an unlock-notify callback a
9740 ** unlock-notify callback is registered. The system is said to be in
9741 ** a deadlocked state if connection A has registered for an unlock-notify
9743 ** B has itself registered for an unlock-notify callback when connection
9746 ** registered for an unlock-notify callback on the conclusion of connection
9758 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9759 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9768 SQLITE_API int sqlite3_unlock_notify(
9770 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9779 ** and extensions to compare the contents of two buffers containing UTF-8
9780 ** strings in a case-independent fashion, using the same definition of "case
9783 SQLITE_API int sqlite3_stricmp(const char *, const char *);
9784 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9796 ** Note that this routine returns zero on a match and non-zero if the strings
9801 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9813 ** insensitive - equivalent upper and lower case ASCII characters match
9819 ** Note that this routine returns zero on a match and non-zero if the strings
9824 SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9843 ** a fixed-length buffer on the stack. If the log message is longer than
9847 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9850 ** CAPI3REF: Write-Ahead Log Commit Hook
9857 ** the associated write-lock on the database released)^, so the implementation
9863 ** ^The third parameter is the name of the database that was written to -
9864 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9865 ** is the number of pages currently in the write-ahead log file,
9876 ** A single database handle may have at most a single write-ahead log callback
9878 ** previously registered write-ahead log callback. ^The return value is
9886 int(*)(void *,sqlite3*,const char*,int),
9891 ** CAPI3REF: Configure an auto-checkpoint
9898 ** more frames in the [write-ahead log] file. ^Passing zero or
9913 ** ^Every new [database connection] defaults to having the auto-checkpoint
9919 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9929 ** [write-ahead log] for database X on [database connection] D to be
9930 ** transferred into the database file and for the write-ahead log to
9941 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9956 ** in the log were checkpointed. ^The [busy-handler callback]
9963 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9972 ** [busy-handler callback])
9985 ** the log file or to -1 if the checkpoint could not run because
9989 ** was called) or to -1 if the checkpoint could not run due to an error or
9997 ** busy-handler configured, it will not be invoked in this case.
10001 ** obtained immediately, and a busy-handler is configured, it is invoked and
10002 ** the writer lock retried until either the busy-handler returns 0 or the lock
10003 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
10004 ** database readers as described above. ^If the busy-handler returns 0 before
10007 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10023 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10035 SQLITE_API int sqlite3_wal_checkpoint_v2(
10038 int eMode, /* SQLITE_CHECKPOINT_* value */
10039 int *pnLog, /* OUT: Size of WAL log in frames */
10040 int *pnCkpt /* OUT: Total number of frames checkpointed */
10075 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10099 ** If X is non-zero, then the virtual table implementation guarantees
10143 ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10163 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10174 ** [xUpdate] method understands as a "no-change" value.
10189 SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10224 SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10318 SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
10336 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10337 ** only sees a single value from the right-hand side of the IN operator
10341 ** table to see all values on the right-hand of the IN operator all at
10346 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10347 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10349 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10350 ** by which the virtual table can ask SQLite if all-at-once processing
10356 ** the IN operator all-at-once, respectively. ^Thus when the third
10357 ** parameter (F) is non-negative, this interface is the mechanism by
10366 ** (non-zero), that means that the constraint is an IN operator
10367 ** that can be processed all-at-once. ^If the constraint is not an IN
10368 ** operator or cannot be processed all-at-once, then the interface returns
10371 ** ^(All-at-once processing of the IN operator is selected if both of the
10375 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10377 ** use the N-th constraint.
10380 ** non-negative had F>=1.
10384 ** the traditional one-at-a-time processing strategy for the IN constraint.
10385 ** ^If both conditions are true, then the argvIndex-th parameter to the
10388 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10391 SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle);
10394 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10404 ** a parameter that was previously selected for all-at-once IN constraint
10407 ** an xFilter argument that was selected for all-at-once IN constraint
10410 ** ^(Use these routines to access all values on the right-hand side
10438 SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut);
10439 SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10452 ** J being a 0-based index into P->aConstraint[], then this routine
10453 ** attempts to set *V to the value of the right-hand operand of
10454 ** that constraint if the right-hand operand is known. ^If the
10455 ** right-hand operand is not known, then *V is set to a NULL pointer.
10458 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10464 ** the right-hand operand of a constraint is a literal value in the original
10465 ** SQL statement. If the right-hand operand is an expression or a reference
10470 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10479 ** "Right-Hand Side".
10481 SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
10514 ** not available, the output variable is set to -1 if the value is numeric,
10520 ** set to the total number of times that the X-th loop has run.</dd>
10524 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10529 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10536 ** to a zero-terminated UTF-8 string containing the name of the index or table
10537 ** used for the X-th loop.
10541 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10542 ** description for the X-th loop.
10545 ** <dd>^The "int" variable pointed to by the V parameter will be set to the
10546 ** id for the X-th query plan element. The id value is unique within the
10547 ** statement. The select-id is the same value as is output in the first
10551 ** <dd>The "int" variable pointed to by the V parameter will be set to the
10558 ** according to the processor time-stamp counter, that elapsed while the
10560 ** all query elements - if it is unavailable the output variable is
10561 ** set to -1.
10584 ** compile-time option.
10592 ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10602 ** for. Query elements are numbered starting from zero. A value of -1 may be
10604 ** - less than -1 or greater than or equal to the total number of query
10605 ** elements used to implement the statement - a non-zero value is returned and
10610 SQLITE_API int sqlite3_stmt_scanstatus(
10612 int idx, /* Index of loop to report on */
10613 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10616 SQLITE_API int sqlite3_stmt_scanstatus_v2(
10618 int idx, /* Index of loop to report on */
10619 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10620 int flags, /* Mask of flags defined below */
10631 ** CAPI3REF: Zero Scan-Status Counters
10636 ** This API is only available if the library is built with pre-processor
10642 ** CAPI3REF: Flush caches to disk mid-transaction
10645 ** ^If a write-transaction is open on [database connection] D when the
10647 ** pages in the pager-cache that are not currently in use are written out
10651 ** interface flushes caches for all schemas - "main", "temp", and
10656 ** immediately and there is a busy-handler callback configured, it is invoked
10664 ** example an IO error or out-of-memory condition), then processing is
10672 SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10675 ** CAPI3REF: The pre-update hook.
10679 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10755 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10756 ** triggers; or 2 for changes resulting from triggers called by top-level
10760 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10765 ** pre-update hook is being invoked for some other reason, including a
10766 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10776 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10784 SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10785 SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10786 SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10787 SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10788 SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
10792 ** CAPI3REF: Low-level system error code
10797 ** The return value is OS-dependent. For example, on unix systems, after
10802 SQLITE_API int sqlite3_system_errno(sqlite3*);
10837 ** If there is not already a read-transaction open on schema S when
10869 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10871 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10903 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10904 ** SQLITE_IOERR error code - is returned, then the final state of the
10918 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10920 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10935 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10964 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10992 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
11002 ** For an ordinary on-disk database file, the serialization is just a
11003 ** copy of the disk file. For an in-memory database or a "TEMP" database,
11041 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
11047 ** Zero or more of the following constants can be OR-ed together for
11051 ** a pointer to contiguous in-memory database that it is currently using,
11053 ** a contiguous in-memory database, then this option causes
11055 ** using a contiguous in-memory database if it has been initialized by a
11065 ** reopen S as an in-memory database based on the serialization contained
11069 ** permitted to add content to the in-memory database as long as the total
11103 SQLITE_API int sqlite3_deserialize(
11131 ** should be treated as read-only.
11135 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11188 /* The double-precision datatype used by RTree depends on the
11189 ** SQLITE_RTREE_INT_ONLY compile-time option.
11199 ** R-Tree geometry query as follows:
11203 SQLITE_API int sqlite3_rtree_geometry_callback(
11206 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
11217 int nParam; /* Size of array aParam[] */
11224 ** Register a 2nd-generation geometry callback named zScore that can be
11225 ** used as part of an R-Tree geometry query as follows:
11229 SQLITE_API int sqlite3_rtree_query_callback(
11232 int (*xQueryFunc)(sqlite3_rtree_query_info*),
11249 int nParam; /* Number of function parameters */
11254 unsigned int *anQueue; /* Number of pending entries in the queue */
11255 int nCoord; /* Number of coordinates */
11256 int iLevel; /* Level of current node or entry */
11257 int mxLevel; /* The largest iLevel value in the tree */
11260 int eParentWithin; /* Visibility of parent node */
11261 int eWithin; /* OUT: Visibility */
11331 ** is not possible for an application to register a pre-update hook on a
11334 ** which a pre-update hook is already defined. The results of attempting
11342 SQLITE_API int sqlite3session_create(
11372 SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
11384 ** pArg must point to a value of type (int). If the value is initially
11387 ** value is less than zero, no change is made. In all cases the (int)
11415 ** disabled - it does not. A newly created session object is enabled.
11422 ** no-op, and may be used to query the current state of the session.
11427 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11457 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11499 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11505 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11509 ** conflict-handler callback) then the X'' value is returned. The application
11517 SQLITE_API int sqlite3session_attach(
11534 int(*xFilter)(
11557 ** database row along with the updated values for each updated non-primary-key
11566 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11568 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11581 ** are sorted in the same order in which they were attached (or auto-attached)
11595 ** recorded once - the first time a row with said primary key is inserted,
11602 ** The session object therefore accumulates two types of records - those
11620 ** found and one or more of the non-primary key fields have been
11637 ** This may appear to have some counter-intuitive effects if a single row
11646 SQLITE_API int sqlite3session_changeset(
11648 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11653 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11675 ** does not have a primary key, this function is a no-op (but does not return
11695 ** used to update the table in database zFrom (call this the "from-table")
11697 ** object (call this the "to-table"). Specifically:
11700 ** <li> For each row (primary key) that exists in the to-table but not in
11701 ** the from-table, an INSERT record is added to the session object.
11703 ** <li> For each row (primary key) that exists in the to-table but not in
11704 ** the from-table, a DELETE record is added to the session object.
11707 ** different non-PK values in each, an UPDATE record is added to the
11725 SQLITE_API int sqlite3session_diff(
11752 ** Because the non-primary key "old.*" fields are omitted, no
11762 SQLITE_API int sqlite3session_patchset(
11764 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11771 ** Return non-zero if no changes to attached tables have been recorded by
11779 ** are restored. However, if this function returns non-zero, then it is
11783 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11834 SQLITE_API int sqlite3changeset_start(
11836 int nChangeset, /* Size of changeset blob in bytes */
11839 SQLITE_API int sqlite3changeset_start_v2(
11841 int nChangeset, /* Size of changeset blob in bytes */
11843 int flags /* SESSION_CHANGESETSTART_* flags */
11866 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11883 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11890 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11903 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11906 ** or until the conflict-handler function returns.
11917 SQLITE_API int sqlite3changeset_op(
11920 int *pnCol, /* OUT: Number of columns in table */
11921 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11922 int *pbIndirect /* OUT: True for an 'indirect' change */
11951 SQLITE_API int sqlite3changeset_pk(
11953 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11954 int *pnCol /* OUT: Number of entries in output array */
11962 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11982 SQLITE_API int sqlite3changeset_old(
11984 int iVal, /* Column number */
11993 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12016 SQLITE_API int sqlite3changeset_new(
12018 int iVal, /* Column number */
12027 ** conflict-handler callback by [sqlite3changeset_apply()] with either
12038 ** "conflicting row" associated with the current conflict-handler callback
12044 SQLITE_API int sqlite3changeset_conflict(
12046 int iVal, /* Column number */
12061 SQLITE_API int sqlite3changeset_fk_conflicts(
12063 int *pnOut /* OUT: Number of FK violations */
12076 ** function with an iterator passed to a conflict-handler by
12084 ** returned. This is to allow the following pattern (pseudo-code):
12097 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
12127 SQLITE_API int sqlite3changeset_invert(
12128 int nIn, const void *pIn, /* Input changeset */
12129 int *pnOut, void **ppOut /* OUT: Inverse of input */
12158 SQLITE_API int sqlite3changeset_concat(
12159 int nA, /* Number of bytes in buffer pA */
12161 int nB, /* Number of bytes in buffer pB */
12163 int *pnOut, /* OUT: Number of bytes in output changeset */
12171 SQLITE_API int sqlite3changeset_upgrade(
12174 int nIn, const void *pIn, /* Input changeset */
12175 int *pnOut, void **ppOut /* OUT: Inverse of input */
12224 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12256 SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
12281 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12282 ** <tr><th style="white-space:pre">Existing Change </th>
12283 ** <th style="white-space:pre">New Change </th>
12333 ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12339 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12358 SQLITE_API int sqlite3changegroup_add_change(
12390 SQLITE_API int sqlite3changegroup_output(
12392 int *pnData, /* OUT: Size of output buffer in bytes */
12415 ** Otherwise, if the return value is non-zero or the xFilter argument to
12451 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12455 ** returned by each invocation of the conflict-handler function. Refer to
12464 ** stored in all non-primary key columns also match the values stored in
12468 ** the non-primary key fields contains a value different from the original
12469 ** row value stored in the changeset, the conflict-handler function is
12472 ** only the values of those non-primary key fields are compared against
12473 ** the current database contents - any trailing database table columns
12477 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12482 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12501 ** This includes the case where the INSERT operation is re-attempted because
12509 ** stored in all modified non-primary key columns also match the values
12513 ** the modified non-primary key fields contains a value different from an
12514 ** original row value stored in the changeset, the conflict-handler function
12516 ** UPDATE changes only contain values for non-primary key fields that are
12518 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12521 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12525 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12543 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12560 SQLITE_API int sqlite3changeset_apply(
12562 int nChangeset, /* Size of changeset in bytes */
12564 int(*xFilter)(
12568 int(*xConflict)(
12570 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12575 SQLITE_API int sqlite3changeset_apply_v2(
12577 int nChangeset, /* Size of changeset in bytes */
12579 int(*xFilter)(
12583 int(*xConflict)(
12585 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12589 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12590 int flags /* SESSION_CHANGESETAPPLY_* flags */
12641 ** Values that may be passed as the second argument to a conflict-handler.
12648 ** (non primary-key) fields modified by the update do not contain the
12809 ** combined on a per-field basis, not per-row. This means that in the
12843 SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12854 SQLITE_API int sqlite3rebaser_configure(
12856 int nRebase, const void *pRebase
12873 SQLITE_API int sqlite3rebaser_rebase(
12875 int nIn, const void *pIn,
12876 int *pnOut, void **ppOut
12893 ** corresponding non-streaming API functions:
12895 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12896 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12906 ** Non-streaming functions that accept changesets (or patchsets) as input
12911 ** low-memory environment is required to handle very large changesets, the
12920 ** &nbsp; int nChangeset,
12927 ** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12954 ** &nbsp; int *pnChangeset,
12961 ** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12979 SQLITE_API int sqlite3changeset_apply_strm(
12981 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12983 int(*xFilter)(
12987 int(*xConflict)(
12989 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12994 SQLITE_API int sqlite3changeset_apply_v2_strm(
12996 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12998 int(*xFilter)(
13002 int(*xConflict)(
13004 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
13008 void **ppRebase, int *pnRebase,
13009 int flags
13011 SQLITE_API int sqlite3changeset_concat_strm(
13012 int (*xInputA)(void *pIn, void *pData, int *pnData),
13014 int (*xInputB)(void *pIn, void *pData, int *pnData),
13016 int (*xOutput)(void *pOut, const void *pData, int nData),
13019 SQLITE_API int sqlite3changeset_invert_strm(
13020 int (*xInput)(void *pIn, void *pData, int *pnData),
13022 int (*xOutput)(void *pOut, const void *pData, int nData),
13025 SQLITE_API int sqlite3changeset_start_strm(
13027 int (*xInput)(void *pIn, void *pData, int *pnData),
13030 SQLITE_API int sqlite3changeset_start_v2_strm(
13032 int (*xInput)(void *pIn, void *pData, int *pnData),
13034 int flags
13036 SQLITE_API int sqlite3session_changeset_strm(
13038 int (*xOutput)(void *pOut, const void *pData, int nData),
13041 SQLITE_API int sqlite3session_patchset_strm(
13043 int (*xOutput)(void *pOut, const void *pData, int nData),
13046 SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
13047 int (*xInput)(void *pIn, void *pData, int *pnData),
13050 SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
13051 int (*xOutput)(void *pOut, const void *pData, int nData),
13054 SQLITE_API int sqlite3rebaser_rebase_strm(
13056 int (*xInput)(void *pIn, void *pData, int *pnData),
13058 int (*xOutput)(void *pOut, const void *pData, int nData),
13085 ** passed as the second argument must point to a value of type (int).
13087 ** chunk size for both input and output. Before returning, the (int) value
13095 SQLITE_API int sqlite3session_config(int op, void *pArg);
13156 int nVal, /* Number of values in apVal[] array */
13175 ** non-negative but less than the number of columns in the table, return
13190 ** non-negative but less than the number of columns in the table, set
13207 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13312 ** If the bClear argument is non-zero, then the auxiliary data is cleared
13333 ** int iCol, iOff;
13334 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13336 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13342 ** modify this structure directly - it should only be used as shown above
13350 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13363 ** int iCol;
13364 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13366 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13375 ** xPhraseFirstColumn() set iCol to -1).
13420 int iVersion; /* Currently always set to 3 */
13424 int (*xColumnCount)(Fts5Context*);
13425 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
13426 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
13428 int (*xTokenize)(Fts5Context*,
13429 const char *pText, int nText, /* Text to tokenize */
13431 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13434 int (*xPhraseCount)(Fts5Context*);
13435 int (*xPhraseSize)(Fts5Context*, int iPhrase);
13437 int (*xInstCount)(Fts5Context*, int *pnInst);
13438 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
13441 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
13442 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
13444 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
13445 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
13447 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
13448 void *(*xGetAuxdata)(Fts5Context*, int bClear);
13450 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
13451 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
13453 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13454 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13457 int (*xQueryToken)(Fts5Context*,
13458 int iPhrase, int iToken,
13459 const char **ppToken, int *pnToken
13461 int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
13484 ** The second and third arguments are an array of nul-terminated strings
13502 ** by argument pText. pText may or may not be nul-terminated. The first
13510 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13515 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13519 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13524 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13557 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13591 ** still appears to contain just two phrases - "(first OR 1st)"
13602 ** when tokenizing query text (it should not - to do so would be
13649 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13663 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13665 int (*xTokenize)(Fts5Tokenizer*,
13667 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13668 const char *pText, int nText,
13669 int (*xToken)(
13671 int tflags, /* Mask of FTS5_TOKEN_* flags */
13673 int nToken, /* Size of token in bytes */
13674 int iStart, /* Byte offset of token within input text */
13675 int iEnd /* Byte offset of end of token within input text */
13699 int iVersion; /* Currently always set to 2 */
13702 int (*xCreateTokenizer)(
13711 int (*xFindTokenizer)(
13719 int (*xCreateFunction)(
13750 ** autoconf-based build
13778 ** The hard limit is the ability of a 32-bit signed integer
13779 ** to count the size: 2^31-1 or 2147483647.
13797 ** tell you that in a well-normalized database, you usually should
13855 ** The suggested maximum number of in-memory pages to use for
13858 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13860 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13861 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13864 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13878 ** counted using a signed 8-bit integer which has a maximum value of 127
13890 ** as a signed 32-bit integer can hold.
13897 ** imposed by the use of 16-bit offsets within each page.
13903 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13904 ** compiled with the default page-size limit will not be able to rollback
13927 ** device characteristics (sector-size and atomic write() support),
13944 ** This value can be lowered (or raised) at run-time using that the
13975 #pragma warn -rch /* unreachable code */
13976 #pragma warn -ccc /* Condition is always true or false */
13977 #pragma warn -aus /* Assigned value is never used */
13978 #pragma warn -csu /* Comparing signed and unsigned */
13979 #pragma warn -spa /* Suspicious pointer arithmetic */
13987 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
14020 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
14027 # define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
14030 # define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
14033 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
14034 #else /* Generates a warning - but it always works */
14036 # define SQLITE_PTR_TO_INT(X) ((int)(X))
14079 ** SEH support if the -DSQLITE_OMIT_SEH option is given.
14089 ** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14092 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14104 ** level of threadsafety. 2 means the library is multithreaded - multiple
14112 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
14120 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
14126 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14133 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14162 # error "Two or more of the following compile-time configuration options\
14200 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14232 extern unsigned int sqlite3CoverageCounter;
14265 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14277 ** of SQLite to unexpected behavior - to make the code "self-healing"
14283 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
14333 extern int sqlite3OSTrace;
14371 ** Return true (non-zero) if the input is an integer that is too large
14372 ** to fit in 32-bits. This macro is used inside of various testcase()
14373 ** macros to verify that we have tested SQLite for large-file support.
14400 ** This is the header file for the generic hash-table implementation
14411 ** The internals of this structure are intended to be opaque -- client
14418 ** All elements of the hash table are on a single doubly-linked list.
14422 ** the global doubly-linked list. The contents of the bucket are the
14423 ** element pointed to plus the next _ht.count-1 elements in the list.
14432 unsigned int htsize; /* Number of buckets in the hash table */
14433 unsigned int count; /* Number of entries in this table */
14436 unsigned int count; /* Number of entries with this hash */
14442 ** structure. All elements are stored on a single doubly-linked list.
14473 #define sqliteHashFirst(H) ((H)->first)
14474 #define sqliteHashNext(E) ((E)->next)
14475 #define sqliteHashData(E) ((E)->data)
14476 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14477 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14482 #define sqliteHashCount(H) ((H)->count)
14693 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14698 ** substitute integer for floating-point
14729 ** the VDBE-level file format changes. The following macros define the
14740 ** changed at run-time using a pragma.
14748 ** on the command-line
14778 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14780 ** The default value of "20" was chosen to minimize the run-time of the
14781 ** speedtest1 test program with options: --shrink-memory --reprepare
14795 ** The compile-time options SQLITE_MMAP_READWRITE and
14808 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14839 ** types can be conveniently redefined at compile-type. Like this:
14841 ** cc '-DUINTPTR_TYPE=long long int' ...
14847 # define UINT32_TYPE unsigned int
14854 # define UINT16_TYPE unsigned short int
14861 # define INT16_TYPE short int
14881 typedef sqlite_int64 i64; /* 8-byte signed integer */
14882 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14883 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14884 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14885 typedef INT16_TYPE i16; /* 2-byte signed integer */
14886 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14887 typedef INT8_TYPE i8; /* 1-byte signed integer */
14895 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14904 ** Estimated quantities used for query planning are stored as 16-bit
14906 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14915 ** 1 -> 0 20 -> 43 10000 -> 132
14916 ** 2 -> 10 25 -> 46 25000 -> 146
14917 ** 3 -> 16 100 -> 66 1000000 -> 199
14918 ** 4 -> 20 1000 -> 99 1048576 -> 200
14919 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14924 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14967 ** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
14972 ** |-----------------| FALSE
14973 ** |-------|
14977 ** |-----------------|
14978 ** |-------| TRUE
14982 ** |-----------------|
14983 ** |-------| FALSE
14990 ** and whether or not that determination is run-time or compile-time.
14992 ** For best performance, an attempt is made to guess at the byte-order
14993 ** using C-preprocessor macros. If that is unsuccessful, or if
14994 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14995 ** at run-time.
15000 ** -DSQLITE_BYTEORDER=1234
15004 ** -DSQLITE_BYTEORDER=4321
15006 ** to cause the build to work for little-endian or big-endian processors,
15009 #ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
15037 const int sqlite3one = 1;
15039 extern const int sqlite3one;
15047 ** Constants for the largest and smallest possible 64-bit signed integers.
15048 ** These macros are designed to work correctly on both 32-bit and 64-bit
15053 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
15057 ** to force 8-byte alignment on 64-bit architectures.
15062 ** pointers in size, and so it is a no-op on systems where the pointer
15078 ** Assert that the pointer X is aligned to an 8-byte boundary. This
15083 ** underlying malloc() implementation might return us 4-byte aligned
15084 ** pointers. In that case, only verify 4-byte alignment.
15087 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
15089 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
15101 ** Default maximum size of memory used by memory-mapped I/O in the VFS
15121 ** default MMAP_SIZE is specified at compile-time, make sure that it does
15145 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15157 ** 0x00000008 Result-set wildcard expansion
15163 ** 0x00000200 Count-of-view optimization
15168 ** 0x00004000 Push-down optimization
15169 ** 0x00008000 After all FROM-clause analysis
15191 ** (---any--) Top-level block structure
15192 ** 0x-------F High-level debug messages
15193 ** 0x----FFF- More detail
15194 ** 0xFFFF---- Low-level debug messages
15220 ** An instance of the following structure is used to store the busy-handler
15225 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15230 int (*xBusyHandler)(void *,int); /* The busy callback */
15232 int nBusy; /* Incremented with each busy call */
15259 ** The root-page of the schema table.
15273 #define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
15278 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
15297 ** directly, we use its constant as a key to lookup the run-time allocated
15299 ** for the run-time allocated buffer.
15302 ** macros become no-ops and have zero performance impact.
15308 SQLITE_API int sqlite3_wsd_init(int N, int J);
15309 SQLITE_API void *sqlite3_wsd_find(void *K, int L);
15403 ** Changing this from a 64-bit to a 32-bit type limits the number of
15416 #define BMS ((int)(sizeof(Bitmask)*8))
15423 #define MASKBIT32(n) (((unsigned int)1)<<(n))
15424 #define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
15425 #define ALLBITS ((Bitmask)-1)
15426 #define TOPBIT (((Bitmask)1)<<(BMS-1))
15434 typedef int VList;
15455 ** This header file (together with is companion C source-code file
15459 ** This header file is #include-ed by sqliteInt.h and thus ends up
15467 ** necessary pre-processor macros for it.
15483 ** This file contains pre-processor directives related to operating system
15504 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15567 ** a no-op
15600 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15602 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15603 ** Mcafee started using SQLite in their anti-virus product and it
15649 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15708 SQLITE_PRIVATE int sqlite3OsInit(void);
15714 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15715 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15716 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15717 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15718 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15719 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15720 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15721 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15722 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15723 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15725 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15726 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15728 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15729 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15731 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15733 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15734 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15740 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15741 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15742 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15743 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15746 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15750 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15751 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15752 SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15753 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15757 ** sqlite3_malloc() to obtain space for the file-handle structure.
15759 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15793 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15816 ** is devoted to storing a super-journal name - there are no more pages to
15820 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15821 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15829 #define PAGER_MEMORY 0x0002 /* In-memory database */
15834 #define PAGER_LOCKINGMODE_QUERY -1
15845 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15850 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15851 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15857 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15879 ** that make up the Pager sub-system API. See source code comments for
15884 SQLITE_PRIVATE int sqlite3PagerOpen(
15888 int,
15889 int,
15890 int,
15893 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
15894 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15897 SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15898 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
15900 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
15901 SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
15905 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
15906 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
15907 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
15908 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
15911 SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
15914 SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15922 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
15924 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15925 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
15930 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
15931 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
15932 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
15933 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
15934 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper);
15935 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
15936 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
15937 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15938 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15939 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
15942 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15943 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
15944 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
15945 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15946 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15948 SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
15949 SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
15950 SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
15951 SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15957 SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int);
15965 SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
15969 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
15976 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
15978 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
15979 SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager*, int);
15985 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15986 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, u64*);
15988 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15998 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
16001 SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
16011 SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager*);
16031 ** This header file defines the interface that the sqlite B-Tree file
16044 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
16051 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
16052 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
16064 SQLITE_PRIVATE int sqlite3BtreeOpen(
16065 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
16069 int flags, /* Flags */
16070 int vfsFlags /* Flags passed through to VFS open */
16080 #define BTREE_MEMORY 2 /* This is an in-memory DB */
16081 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
16084 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
16085 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
16086 SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
16088 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
16090 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
16091 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
16092 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
16095 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
16096 SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree*);
16097 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
16098 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
16099 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
16100 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
16101 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
16102 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
16103 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
16104 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
16105 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
16106 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
16107 SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree*);
16108 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
16110 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
16111 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
16113 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
16118 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
16122 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16127 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
16129 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
16135 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16138 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16141 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
16142 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
16144 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
16145 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, i64*);
16146 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
16147 SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
16149 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
16150 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
16152 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
16162 ** For example, the free-page-count field is located at byte offset 36 of
16163 ** the database file header. The incr-vacuum-flag field is located at
16167 ** It is a read-only number computed by the pager. But we merge it with
16180 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
16194 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16195 ** column of the b-tree of the cursor. The Expr tree will not contain
16196 ** any function calls nor subqueries nor references to b-trees other than
16199 ** The design of the _RANGE hint is aid b-tree implementations that try
16200 ** to prefetch content from remote machines - to provide those
16225 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
16230 ** For read-only cursors the wrFlag argument is always zero. For read-write
16241 ** by this, the native b-tree engine of SQLite, but it is available to
16243 ** b-tree system. For alternative storage engines in which a delete of
16247 ** and DELETE operations as no-ops, and any READ operation against a
16250 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
16253 SQLITE_PRIVATE int sqlite3BtreeCursor(
16256 int wrFlag, /* 1 for writing. 0 for read-only */
16261 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
16265 SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
16268 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
16269 SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
16272 int bias,
16273 int *pRes
16275 SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
16278 int *pRes
16280 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
16281 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
16282 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
16329 int nData; /* Size of pData. 0 if none. */
16330 int nZero; /* Extra zero data appended after pData,nData */
16333 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
16334 int flags, int seekResult);
16335 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
16336 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
16337 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
16338 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
16339 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
16344 SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
16349 SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
16354 int nRoot, /* Number of entries in aRoot[] */
16355 int mxErr, /* Stop reporting errors after this many */
16356 int *pnErr, /* OUT: Write number of errors seen to this variable */
16363 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
16364 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
16368 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
16369 SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
16370 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
16371 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
16380 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
16382 SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
16384 SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
16387 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
16392 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16395 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
16402 ** Enter and Leave procedures no-ops.
16407 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
16409 SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
16424 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
16425 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
16426 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
16489 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16490 int p1; /* First operand */
16491 int p2; /* Second parameter (often the jump destination) */
16492 int p3; /* The third parameter */
16494 int i; /* Integer value if p4type==P4_INT32 */
16516 u32 iSrcLine; /* Source-code line that generated this opcode
16528 ** A sub-routine used to implement a trigger program.
16531 VdbeOp *aOp; /* Array of opcodes for sub-program */
16532 int nOp; /* Elements in aOp[] */
16533 int nMem; /* Number of memory cells required */
16534 int nCsr; /* Number of cursors required */
16537 SubProgram *pNext; /* Next sub-program already visited */
16557 #define P4_STATIC (-1) /* Pointer to a static string */
16558 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16559 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16560 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16561 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16563 #define P4_FREE_IF_LE (-6)
16564 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16565 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16566 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16567 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
16568 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
16569 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16570 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16571 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16572 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16573 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16574 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16675 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16676 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16677 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16706 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16723 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16740 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16776 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16857 ** Additional non-public SQLITE_PREPARE_* flags
16868 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
16869 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
16870 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
16871 SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
16872 SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
16873 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
16874 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
16875 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
16876 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
16877 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
16878 SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
16879 SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
16881 SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
16888 SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
16889 SQLITE_PRIVATE void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int);
16894 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
16896 SQLITE_PRIVATE int sqlite3VdbeExplain(Parse*,u8,const char*,...);
16898 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
16912 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16917 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16919 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
16920 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
16921 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
16922 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
16923 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
16925 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe*, int);
16926 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
16927 SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
16928 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
16929 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
16931 SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
16935 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
16936 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
16938 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
16939 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
16941 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
16946 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
16947 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
16948 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
16950 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
16954 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
16955 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
16956 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
16960 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
16963 SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
16966 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
16967 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
16968 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
16972 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
16973 SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
16975 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
16976 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
16977 SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
16980 typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
16984 SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
16986 SQLITE_PRIVATE void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val);
16988 SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
16990 SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
16996 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17033 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
17038 ** // in distinguishing equal and not-equal.
17041 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
17042 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
17050 ** is responsible for keeping track of this and reporting byte-code branches
17057 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
17083 SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
17084 SQLITE_PRIVATE void sqlite3VdbeScanStatusRange(Vdbe*, int, int, int);
17085 SQLITE_PRIVATE void sqlite3VdbeScanStatusCounters(Vdbe*, int, int, int);
17093 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
17097 SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr);
17167 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
17173 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
17179 SQLITE_PRIVATE int sqlite3PcacheOpen(
17180 int szPage, /* Size of every page */
17181 int szExtra, /* Extra space associated with each page */
17182 int bPurgeable, /* True if pages are on backing store */
17183 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
17188 /* Modify the page-size after the cache has been created. */
17189 SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
17194 SQLITE_PRIVATE int sqlite3PcacheSize(void);
17199 SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
17200 SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
17210 /* Change a page number. Used by incr-vacuum. */
17237 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
17249 SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
17252 /* Set and get the suggested cache-size for the specified pager-cache.
17255 ** the total number of pages cached by purgeable pager-caches to the sum
17256 ** of the suggested cache-sizes.
17258 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
17260 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
17263 /* Set or get the suggested spill-size for the specified pager-cache.
17265 ** The spill-size is the minimum number of pages in cache before the cache
17268 SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
17275 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
17279 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
17285 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
17286 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
17289 SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
17292 SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
17329 ** at start-time.
17331 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
17334 ** start-time.
17336 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17338 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17355 ** If this is a no-op implementation, implement everything as macros.
17370 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
17376 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17396 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17397 ** In other words, the zero-based numbers are used for all external interfaces
17398 ** and the one-based values are used internally.
17426 ** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17440 int schema_cookie; /* Database schema version number for this file */
17441 int iGeneration; /* Generation counter. Incremented with each change */
17450 int cache_size; /* Number of pages to use in the cache */
17455 ** Db.pSchema->flags field.
17457 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17458 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17459 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17460 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17463 ** Allowed values for the DB.pSchema->flags field.
17483 ** Lookaside malloc is a set of fixed-size buffers that can be used
17505 ** in a performance-critical path. sz should be set by to szTrue whenever
17510 ** come off of pFree first, then pInit as a fallback. This dual-list
17511 ** allows use to compute a high-water mark - the maximum number of allocations
17512 ** outstanding at any point in the past - by subtracting the number of
17515 ** Enhancement on 2019-12-12: Two-size-lookaside
17519 ** The two-size-lookaside enhancement breaks up the lookaside allocation
17520 ** into two pools: One of 128-byte slots and the other of the default size
17521 ** (1200-byte) slots. Allocations are filled from the small-pool first,
17522 ** failing over to the full-size pool if that does not work. Thus more
17539 void *pMiddle; /* First byte past end of full-size buffers and
17544 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17550 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17551 #define EnableLookaside db->lookaside.bDisable--;\
17552 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17554 /* Size of the smaller allocations in two-size lookaside */
17562 ** A hash table for built-in function definitions. (Application-defined
17577 See ext/userauth/user-auth.txt for details."
17587 int nAuthPW; /* Size of the zAuthPW in bytes */
17599 SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
17600 SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
17602 SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
17610 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17613 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17644 int nDb; /* Number of backends currently in use */
17649 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17650 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
17651 int errCode; /* Most recent error code (SQLITE_*) */
17652 int errByteOffset; /* Byte offset of error in SQL statement */
17653 int errMask; /* & result codes with this before returning */
17654 int iSysErrno; /* Errno value from last system error */
17657 u8 autoCommit; /* The auto-commit flag. */
17661 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17667 u8 noSharedCache; /* True if no shared-cache backends */
17670 int nextPagesize; /* Pagesize after VACUUM if >0 */
17673 int aLimit[SQLITE_N_LIMIT]; /* Limits */
17674 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
17684 int nVdbeActive; /* Number of VDBEs currently running */
17685 int nVdbeRead; /* Number of active VDBEs that read or write */
17686 int nVdbeWrite; /* Number of active VDBEs that read and write */
17687 int nVdbeExec; /* Number of nested calls to VdbeExec() */
17688 int nVDestroy; /* Number of active OP_VDestroy operations */
17689 int nExtension; /* Number of loaded extensions */
17693 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
17701 int (*xCommitCallback)(void*); /* Invoked at every commit. */
17705 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
17708 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
17713 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
17715 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17718 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
17721 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
17722 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
17726 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
17735 int (*xProgress)(void *); /* The progress callback */
17740 int nVTrans; /* Allocated size of aVTrans */
17751 int nAnalysisLimit; /* Number of index rows to ANALYZE */
17752 int busyTimeout; /* Busy handler timeout, in msec */
17753 int nSavepoint; /* Number of non-transaction savepoints */
17754 int nStatement; /* Number of nested statement-transactions */
17757 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
17773 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
17784 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17785 #define ENC(db) ((db)->enc)
17790 ** C-compilers still do not accept LL integer literals.
17833 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17834 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17840 #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17857 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17878 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17882 #define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */
17884 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17892 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17897 #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17899 #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
17905 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17906 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17912 #define ConstFactorOk(P) ((P)->okConstFactor)
17927 ** structure. For global built-in functions (ex: substr(), max(), count())
17929 ** For per-connection application-defined functions, a pointer to this
17930 ** structure is held in the db->aHash hash table.
17932 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17933 ** field is used by per-connection app-def functions.
17936 i8 nArg; /* Number of arguments. -1 means unlimited */
17940 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17943 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17952 ** This structure encapsulates a user-function destructor callback (as
17966 int nRef;
17985 ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
17992 ** See multiple instances of tag-20230109-1.
17996 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17999 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
18000 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
18001 #define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
18002 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
18003 /* 0x0200 -- available for reuse */
18004 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
18008 ** single query - might change over time */
18009 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
18011 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
18016 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
18017 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
18021 /* Identifier numbers for each in-line function */
18039 ** as the user-data (sqlite3_user_data()) for the function. If
18050 ** zName is the name of a function that is implemented by in-line
18056 ** zName is the name of a test-only function implemented by in-line
18065 ** a single query. The iArg is ignored. The user-data is always set
18069 ** For math-library functions. xPtr is an arbitrary pointer.
18074 ** ignored and the user-data for these functions is set to an
18075 ** arbitrary non-NULL pointer. The bNC parameter is not used.
18093 ** available as the function user-data (sqlite3_user_data()). The
18158 char *zName; /* Savepoint name (nul-terminated) */
18181 int nRefModule; /* Number of pointers to this object */
18217 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
18219 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18250 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
18270 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18273 int (*xCmp)(void*,int, const void*, int, const void*);
18282 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
18336 ** database connections, even when the rest of the in-memory database
18346 ** database schema are initially stored in a linked-list pointed to by
18353 ** When an in-memory Table object is deleted (for example when the
18373 int nRef; /* Number of pointers to this structure */
18377 int iSavepoint; /* Depth of the SAVEPOINT stack */
18404 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18413 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
18422 int nArg; /* Number of arguments to the module */
18435 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18436 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18446 #define TF_Readonly 0x00000001 /* Read-only system table */
18456 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18457 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18472 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18473 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18481 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18483 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18491 ** only works for non-virtual tables (ordinary tables and views) and is
18496 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18497 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18499 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18508 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18509 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18511 /* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18533 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18536 ** from-table == child-table
18537 ** to-table == parent-table
18540 ** which is attached to the from-table. The to-table need not exist when
18541 ** the from-table is created. The existence of the to-table is not checked.
18554 int nCol; /* Number of columns in this key */
18555 /* EV: R-30323-21917 */
18560 int iFrom; /* Index of column in pFrom */
18621 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18649 ** pKeyInfo->nField.
18653 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18657 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18658 ** multiple entries in the b-tree with the same key (when only looking
18659 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18664 ** get and equal results when comparing this structure to a b-tree record.
18668 ** b-tree.
18671 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18677 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
18695 ** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
18710 ** algorithm to employ when an attempt is made to insert a non-unique
18717 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18719 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18720 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18730 ** number (it cannot - the database page is not allocated until the VDBE
18755 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18757 unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
18764 int nSample; /* Number of elements in aSample[] */
18765 int mxSample; /* Number of slots allocated to aSample[] */
18766 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
18768 IndexSample *aSample; /* Samples of the left-most key */
18769 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18770 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18784 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18787 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18792 #define XN_ROWID (-1) /* Indexed column is the rowid */
18793 #define XN_EXPR (-2) /* Indexed column is an expression */
18802 int n; /* Size of record in bytes */
18825 const char *z; /* Text of the token. Not NULL-terminated! */
18826 unsigned int n; /* Number of characters in this token */
18848 int sortingIdx; /* Cursor number of the sorting index */
18849 int sortingIdxPTab; /* Cursor number of pseudo-table */
18850 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
18855 int iTable; /* Cursor number of the source table */
18859 int nColumn; /* Number of used entries in aCol[] */
18860 int nAccumulator; /* Number of columns that show through to the output.
18866 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18867 int iDistAddr; /* Address of OP_OpenEphemeral */
18868 int iOBTab; /* Ephemeral table to implement ORDER BY */
18873 int nFunc; /* Number of entries in aFunc[] */
18884 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18887 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18889 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18892 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18893 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18894 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18899 ** the option is available (at compile-time).
18904 typedef int ynVar;
18914 ** to represent the greater-than-or-equal-to operator in the expression
18928 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18947 ** subquery gives a constant result, then iTable is -1. If the subquery
18952 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18983 int iValue; /* Non-negative integer value if EP_IntValue */
19004 int nHeight; /* Height of the tree headed by this node */
19006 int iTable; /* TK_COLUMN: cursor number of table holding column
19008 ** TK_TRIGGER: 1 -> new, 0 -> old
19013 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
19016 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
19018 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
19019 int iOfst; /* else: start of token from start of statement */
19027 int iAddr; /* Subroutine entry address */
19028 int regReturn; /* Register used to hold return address */
19080 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
19081 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
19082 #define ExprSetProperty(E,P) (E)->flags|=(P)
19083 #define ExprClearProperty(E,P) (E)->flags&=~(P)
19084 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19085 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19086 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19091 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
19092 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
19093 #define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
19094 #define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
19095 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
19096 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
19097 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
19098 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
19099 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
19108 ** processes but is a no-op for delivery.
19111 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
19112 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
19113 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
19133 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
19143 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
19159 ** ---------- -------------------------
19170 int nExpr; /* Number of expressions on the list */
19171 int nAlloc; /* Number of a[] slots allocated */
19192 int iConstExprReg; /* Register in which Expr value is cached. Used only
19219 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19222 int nId; /* Number of identifiers on the list */
19227 int idx; /* Index in some Table.aCol[] of a column named zName */
19228 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
19239 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19250 ** In the colUsed field, the high-order bit (bit 63) is set if the table
19251 ** contains more than 63 columns and the 64-th or later column is used.
19269 int addrFillSub; /* Address of subroutine to manifest a subquery */
19270 int regReturn; /* Register holding return address of addrFillSub */
19271 int regResult; /* Registers holding results of a co-routine */
19276 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19277 unsigned isCorrelated :1; /* True if sub-query is correlated */
19279 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19285 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19290 int iCursor; /* The VDBE cursor number used to access this table */
19298 ExprList *pFuncArg; /* Arguments to table-valued-function */
19324 int nSrc; /* Number of tables or subqueries in the FROM clause */
19349 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
19352 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
19355 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
19364 #define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */
19384 ** NameContexts can be nested. When resolving names, the inner-most
19400 ExprList *pEList; /* Optional list of result-set columns */
19403 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
19406 int nRef; /* Number of names resolved by this context */
19407 int nNcErr; /* Number of errors encountered while resolving names */
19408 int ncFlags; /* Zero or more NC_* flags defined below */
19442 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19451 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19452 ** conflict-target clause.) The pUpsertTargetWhere is the optional
19478 int regData; /* First register holding array of VALUES */
19479 int iDataCur; /* Index of the data cursor */
19480 int iIdxCur; /* Index of the first index cursor */
19504 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
19506 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
19557 #define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
19565 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
19573 ** identified by pDest->iSDParm.
19575 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
19577 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19582 ** the side-effects of functions.
19589 ** in register pDest->iSDParm then abandon the rest
19593 ** row of result as the key in table pDest->iSDParm.
19594 ** Apply the affinity pDest->affSdst before storing
19597 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19603 ** SRT_Coroutine Generate a co-routine that returns a new row of
19605 ** of the co-routine is stored in register pDest->iSDParm
19606 ** and the result row is stored in pDest->nDest registers
19607 ** starting with pDest->iSdst.
19609 ** SRT_Table Store results in temporary table pDest->iSDParm.
19615 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19616 ** But also use temporary table pDest->iSDParm+1 as
19620 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
19624 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19626 ** index at pDest->iSDParm+1 hold all prior stores.
19629 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19630 ** table is an intkey table - in this case the first
19632 ** key. If (pDest->iSDParm>0), then the table is an index
19633 ** table. (pDest->iSDParm) is the number of key columns in
19645 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19651 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19667 int iSDParm; /* A parameter used by the eDest disposal method */
19668 int iSDParm2; /* A second parameter for the eDest disposal method */
19669 int iSdst; /* Base register where results are written */
19670 int nSdst; /* Number of registers allocated */
19679 ** the code generator needs. We have to keep per-table autoincrement
19687 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
19688 int regCtr; /* Memory register holding the rowid counter */
19698 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19713 int orconf; /* Default ON CONFLICT policy */
19728 typedef unsigned int yDbMask;
19748 int iDataCur; /* The data cursor associated with the index */
19749 int iIdxCur; /* The index cursor */
19750 int iIdxCol; /* The index column that contains value of pExpr */
19780 ** The nTableLock and aTableLock variables are only used if the shared-cache
19781 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19782 ** used to store the set of table-locks required by the statement being
19790 int rc; /* Return code from execution */
19809 int nRangeReg; /* Size of the temporary register block */
19810 int iRangeReg; /* First register in temporary register block */
19811 int nErr; /* Number of errors seen */
19812 int nTab; /* Number of previously allocated VDBE cursors */
19813 int nMem; /* Number of memory cells used so far */
19814 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
19815 int iSelfTab; /* Table associated with an index on expr, or negative
19816 ** of the base register during check-constraint eval */
19817 int nLabel; /* The *negative* of the number of labels used */
19818 int nLabelAlloc; /* Number of slots in aLabel */
19819 int *aLabel; /* Space to hold the labels */
19826 int regRowid; /* Register holding rowid of CREATE TABLE entry */
19827 int regRoot; /* Register holding root page number for new objects */
19828 int nMaxArg; /* Max args passed to user function by sub-program */
19829 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
19834 int nTableLock; /* Number of locks in aTableLock */
19835 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19843 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
19861 int aTempReg[8]; /* Holding area for temporary registers */
19878 int nVtabLock; /* Number of virtual tables to lock */
19880 int nHeight; /* Expression tree height of current sub-select */
19882 int addrExplain; /* Address of current OP_Explain opcode */
19892 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19914 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19916 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19925 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19931 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19937 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19961 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19965 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19969 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
20005 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
20006 the <column-list> is stored here */
20025 ** that is a part of a trigger-program.
20030 ** the first step of the trigger-program.
20037 ** orconf -> stores the ON CONFLICT algorithm
20038 ** pSelect -> The content to be inserted - either a SELECT statement or
20040 ** zTarget -> Dequoted name of the table to insert into.
20041 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
20042 ** statement, then this stores the column-names to be
20044 ** pUpsert -> The ON CONFLICT clauses for an Upsert
20047 ** zTarget -> Dequoted name of the table to delete from.
20048 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
20052 ** zTarget -> Dequoted name of the table to update.
20053 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
20055 ** pExprList -> A list of the columns to update and the expressions to update
20060 ** pSelect -> The SELECT statement
20063 ** pExprList -> The list of expressions that follow the RETURNING keyword.
20079 TriggerStep *pNext; /* Next in the link-list */
20080 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
20091 int iRetCur; /* Transient table holding RETURNING results */
20092 int nRetCol; /* Number of in pReturnEL after expansion */
20093 int iRetReg; /* Register array for holding a row of RETURNING */
20110 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
20114 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
20117 ** The following object is the header for an "RCStr" or "reference-counted
20128 ** 3. Make a (read-only) copy of a read-only RCStr string using
20146 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
20147 int rc; /* Result code stored here */
20162 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20165 ** be used on trunk check-ins. They are a temporary mechanism available
20170 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
20172 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
20183 int bMemstat; /* True to enable memory status */
20187 u8 bUseCis; /* Use covering indices for full-scans */
20192 u8 bJsonSelfcheck; /* Double-check JSON parsing */
20194 int mxStrlen; /* Maximum string length */
20195 int neverCorrupt; /* Database is always well-formed */
20196 int szLookaside; /* Default lookaside buffer size */
20197 int nLookaside; /* Default lookaside buffer count */
20198 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20199 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20200 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20201 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20203 int nHeap; /* Size of pHeap[] */
20204 int mnReq, mxReq; /* Min and max heap requests sizes */
20208 int szPage; /* Size of each page in pPage[] */
20209 int nPage; /* Number of pages in pPage[] */
20210 int mxParserStack; /* maximum depth of the parser stack */
20211 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20213 /* The above might be initialized to non-zero. The following need to always
20215 int isInit; /* True after initialization has finished */
20216 int inProgress; /* True while initialization in progress */
20217 int isMutexInit; /* True after mutexes are initialized */
20218 int isMallocInit; /* True after malloc is initialized */
20219 int isPCacheInit; /* True after malloc is initialized */
20220 int nRefInitMutex; /* Number of users of pInitMutex */
20222 void (*xLog)(void*,int,const char*); /* Function for logging */
20225 void(*xSqllog)(void*,sqlite3*,const char*, int);
20239 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
20246 int bLocaltimeFault; /* True to fail localtime() calls */
20247 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
20248 int iOnceResetThreshold; /* When to reset OP_Once counters */
20249 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20250 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
20251 /* vvvv--- must be last ---vvv */
20259 ** the assert is only valid on a well-formed database. Instead of:
20271 ** things that are always true for well-formed databases.
20276 ** Context pointer passed down through the tree-walk.
20280 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
20281 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
20283 int walkerDepth; /* Number of subqueries */
20285 u16 mWFlags; /* Use-dependent flags */
20288 int n; /* A counter */
20289 int iCur; /* A cursor number */
20293 int *aiCol; /* array of column indexes */
20319 const char *zType; /* Type of the container - used for error messages */
20320 const Token *pName; /* Name of the container - used for error messages */
20324 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
20325 SQLITE_PRIVATE int sqlite3WalkExprNN(Walker*, Expr*);
20326 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
20327 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
20328 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
20329 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
20330 SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
20331 SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
20332 SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
20333 SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*);
20348 ** Return code from the parse-tree walking primitives and their
20379 int nCte; /* Number of CTEs in the WITH clause */
20380 int bView; /* Belongs to the outermost Select of a view */
20397 int nUse; /* Number of users of this CTE */
20398 int addrM9e; /* Start of subroutine to compute materialization */
20399 int regRtn; /* Return address register for addrM9e subroutine */
20400 int iCur; /* Ephemeral table holding the materialization */
20419 ** data structures on sqlite3DebugPrintf() using a tree-like view.
20422 int iLevel; /* Which level of the tree we are on */
20436 ** (2) All window functions in a single SELECT form a linked-list
20466 int iEphCsr; /* Partition buffer or Peer buffer */
20467 int regAccum; /* Accumulator */
20468 int regResult; /* Interim result */
20469 int csrApp; /* Function cursor (used by min/max) */
20470 int regApp; /* Function register (also used by min/max) */
20471 int regPart; /* Array of registers for PARTITION BY values */
20473 int nBufferCol; /* Number of columns in buffer table */
20474 int iArgCol; /* Offset of first argument for this function */
20475 int regOne; /* Register containing constant value 1 */
20476 int regStartRowid;
20477 int regEndRowid;
20489 SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
20492 SQLITE_PRIVATE int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
20494 SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
20495 SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
20509 ** Assuming zIn points to the first byte of a UTF-8 character,
20510 ** advance zIn to point to the first byte of the next UTF-8 character.
20521 ** routines that report the line-number on which the error originated
20525 SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
20526 SQLITE_PRIVATE int sqlite3CorruptError(int);
20527 SQLITE_PRIVATE int sqlite3MisuseError(int);
20528 SQLITE_PRIVATE int sqlite3CantopenError(int);
20533 SQLITE_PRIVATE int sqlite3NomemError(int);
20534 SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
20542 SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
20566 ** The ctype.h header is needed for non-ASCII systems. It is also
20602 SQLITE_PRIVATE int sqlite3IsIdChar(u8);
20607 SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
20608 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
20613 SQLITE_PRIVATE int sqlite3MallocInit(void);
20629 SQLITE_PRIVATE int sqlite3MallocSize(const void*);
20630 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
20631 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
20637 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
20674 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
20675 SQLITE_PRIVATE int sqlite3MutexInit(void);
20676 SQLITE_PRIVATE int sqlite3MutexEnd(void);
20684 SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
20685 SQLITE_PRIVATE void sqlite3StatusUp(int, int);
20686 SQLITE_PRIVATE void sqlite3StatusDown(int, int);
20687 SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
20688 SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
20702 # define MAN754 ((((u64)1)<<52)-1)
20705 SQLITE_PRIVATE int sqlite3IsNaN(double);
20706 SQLITE_PRIVATE int sqlite3IsOverflow(double);
20718 int nArg; /* Total number of arguments */
20719 int nUsed; /* Number of arguments used so far */
20728 char sign; /* '+' or '-' */
20730 int n; /* Significant digits in the decode */
20731 int iDP; /* Location of the decimal point */
20736 SQLITE_PRIVATE void sqlite3FpDecode(FpDecode*,double,int,int);
20753 SQLITE_PRIVATE void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
20763 int, const Upsert*, const Trigger*);
20765 const Expr*, int, const ExprList*, const Expr*,
20798 SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
20804 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
20805 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*);
20807 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
20808 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
20809 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
20810 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
20812 SQLITE_PRIVATE void sqlite3TouchRegister(Parse*,int);
20814 SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse*,int);
20817 SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
20819 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
20820 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
20822 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
20826 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
20833 SQLITE_PRIVATE int sqlite3ExprDeferredDelete(Parse*, Expr*);
20837 SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
20838 SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
20839 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
20844 SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
20845 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
20846 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
20847 SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
20848 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
20853 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
20862 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
20865 SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
20869 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20870 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20875 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
20879 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20882 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
20883 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
20890 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
20898 SQLITE_PRIVATE int sqlite3FaultSim(int);
20902 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
20903 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
20904 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
20909 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
20916 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
20917 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
20919 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
20922 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
20928 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
20930 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
20931 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
20942 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
20944 SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
20946 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
20948 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
20949 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
20956 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, SrcItem *);
20962 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
20963 SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
20964 Expr*, int, int, u8);
20965 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
20966 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
20972 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
20973 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
20977 SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*);
20979 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
20982 ExprList*,Select*,u16,int);
20985 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
20986 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
20987 SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
20989 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
20990 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
20991 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
20992 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
20996 SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo*);
20997 SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
20998 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
20999 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
21000 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
21001 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
21003 SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
21005 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
21006 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
21007 SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
21008 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
21009 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
21010 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
21015 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
21016 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
21017 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
21025 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
21026 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
21028 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
21030 SQLITE_PRIVATE int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
21031 SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*,Expr*,int);
21032 SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
21033 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
21034 SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
21038 SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
21039 SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
21045 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
21046 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
21048 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
21049 SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
21050 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
21054 SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
21055 SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
21056 SQLITE_PRIVATE int sqlite3ExprIsConstant(Parse*,Expr*);
21057 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
21058 SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
21059 SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int);
21061 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
21063 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*);
21064 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
21065 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21066 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
21069 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21070 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
21071 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
21072 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
21073 SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
21074 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
21075 u8,u8,int,int*,int*,Upsert*);
21081 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
21082 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
21083 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
21086 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
21087 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
21088 SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
21089 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
21090 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
21091 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
21093 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21094 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
21095 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21096 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21103 SQLITE_PRIVATE int sqlite3JsonTableFunctions(sqlite3*);
21105 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
21106 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
21107 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
21111 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
21115 SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
21116 Expr*,int, int);
21118 SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
21120 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
21122 SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
21123 int, int, int);
21124 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
21125 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
21137 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
21138 SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
21140 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
21141 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
21156 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
21157 SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol);
21158 SQLITE_PRIVATE void sqlite3SrcItemColumnUsed(SrcItem*,int);
21159 SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr*,int,u32);
21160 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
21161 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
21164 SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
21167 SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
21174 SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
21177 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
21178 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
21179 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
21180 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
21181 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
21183 SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
21185 SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
21186 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
21187 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
21188 SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
21189 SQLITE_PRIVATE int sqlite3Atoi(const char*);
21191 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
21193 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
21195 SQLITE_PRIVATE int sqlite3Utf8ReadLimited(const u8*, int, u32*);
21200 SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
21201 SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
21202 SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
21205 ** Routines to read and write variable-length integers. These used to
21209 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
21212 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
21232 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
21234 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
21235 SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
21237 SQLITE_PRIVATE int sqlite3ExprDataType(const Expr *pExpr);
21238 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
21239 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
21240 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
21241 SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
21243 SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
21245 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
21247 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
21248 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
21251 SQLITE_PRIVATE const char *sqlite3ErrName(int);
21255 SQLITE_PRIVATE int sqlite3MemdbInit(void);
21256 SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs*);
21261 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
21262 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
21263 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
21264 SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
21269 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
21270 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
21274 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
21275 SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
21276 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
21278 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
21279 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
21280 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
21281 SQLITE_PRIVATE int sqlite3AbsInt32(int);
21290 SQLITE_PRIVATE int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
21291 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
21292 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
21301 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
21303 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
21319 SQLITE_PRIVATE int sqlite3PendingByte;
21325 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
21330 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
21332 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
21333 SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
21334 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
21336 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, SrcItem*);
21338 SQLITE_PRIVATE int sqlite3MatchEName(
21343 int*
21347 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
21348 SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
21350 SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
21351 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
21352 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
21363 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
21364 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
21365 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
21366 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
21369 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
21370 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
21373 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
21374 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
21378 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
21379 SQLITE_PRIVATE const char *sqlite3SelectOpName(int);
21380 SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse*, ExprList*);
21383 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
21385 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
21386 void (*)(sqlite3_context*,int,sqlite3_value **),
21387 void (*)(sqlite3_context*,int,sqlite3_value **),
21390 void (*)(sqlite3_context*,int,sqlite3_value **),
21396 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
21397 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
21404 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
21405 SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, i64);
21409 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
21410 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
21418 SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
21424 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
21425 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
21426 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
21428 SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
21429 SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
21433 ** The interface to the LEMON-generated parser
21439 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
21440 SQLITE_PRIVATE int sqlite3ParserFallback(int);
21442 SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
21453 SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
21459 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
21477 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
21478 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
21479 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
21484 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
21494 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
21496 SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db);
21498 SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
21499 SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
21506 SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
21509 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
21513 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
21514 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
21515 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
21516 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
21518 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
21521 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
21522 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
21529 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
21533 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
21534 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
21536 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
21537 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
21557 SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*);
21558 SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
21560 SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert*);
21571 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21573 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21575 ** provided (enforcement of FK constraints requires the triggers sub-system).
21578 SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
21580 SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
21581 SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
21584 SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3*,int);
21596 SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
21626 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21636 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
21638 SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
21639 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
21642 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
21645 SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
21650 SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *);
21651 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
21656 SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr*,int);
21675 SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
21680 ** sqlite3IoTrace is a pointer to a printf-like routine used to
21713 ** passed back to non-lookaside free() routines. Asserts such as the
21714 ** example above are placed on the non-lookaside free() routines to verify
21717 ** All of this is no-op for a production build. It only comes into
21718 ** play when the SQLITE_MEMDEBUG compile-time option is used.
21722 SQLITE_PRIVATE int sqlite3MemdebugHasType(const void*,u8);
21723 SQLITE_PRIVATE int sqlite3MemdebugNoType(const void*,u8);
21725 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21737 SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
21738 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
21742 SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
21745 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
21748 SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr);
21749 SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr);
21750 SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
21751 SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
21755 SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
21759 SQLITE_PRIVATE int sqlite3KvvfsInit(void);
21769 # define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
21791 ** all of the platform-specific files (os_*.c) and is #included into those
21803 ** switch. The following code should catch this problem at compile-time.
21818 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21832 SQLITE_API extern int sqlite3_io_error_hit;
21833 SQLITE_API extern int sqlite3_io_error_hardhit;
21834 SQLITE_API extern int sqlite3_io_error_pending;
21835 SQLITE_API extern int sqlite3_io_error_persist;
21836 SQLITE_API extern int sqlite3_io_error_benign;
21837 SQLITE_API extern int sqlite3_diskfull_pending;
21838 SQLITE_API extern int sqlite3_diskfull;
21842 || sqlite3_io_error_pending-- == 1 ) \
21857 sqlite3_diskfull_pending--; \
21870 SQLITE_API extern int sqlite3_open_file_count;
21900 ** This file implements routines used to report what compile-time options
21903 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21907 ** autoconf-based build
21921 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21927 ** An array of names of all compile-time options. This array should
21928 ** be sorted A-Z.
21931 ** only a handful of compile-time options, so most times this array is usually
21969 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21973 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21975 "COMPILER=gcc-" __VERSION__,
22670 SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){ in sqlite3CompileOptions()
22695 /* An array to map all upper-case characters into their corresponding
22696 ** lower-case character.
22698 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22738 /* All of the upper-to-lower conversion data is above. The following
22755 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22756 ** be undefined behavior. That's goofy, but the C-standards people thought
22764 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22765 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22766 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22769 ** The following 256 byte lookup table is used to support SQLites built-in
22778 ** SQLite identifier character 0x40 $, _, or non-ascii
22782 ** case. i.e. if the character is a lower-case ASCII character.
22783 ** If x is a lower-case ASCII character, then its upper-case equivalent
22784 ** is (x - 0x20). Therefore toupper() can be implemented as:
22791 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22793 ** non-ASCII UTF character. Hence the test for whether or not a character is
22802 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22834 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22838 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22839 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22841 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22849 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22850 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22851 ** that compile-time option is omitted.
22857 # error "Compile-time disabling of covering index scan using the\
22858 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22874 ** before 3.12.0). -1 means always keep the entire statement journal in
22884 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22886 ** and N is the number of slots. The lookaside-configuration can be
22887 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22888 ** or at run-time for an individual database connection using
22891 ** With the two-size-lookaside enhancement, less lookaside is required.
22892 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22893 ** and 93 128-byte slots, which is more lookaside than is available
22894 ** using the older 1200,100 configuration without two-size-lookaside.
22905 /* The default maximum size of an in-memory database created using
22971 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
22984 ** Hash table for global functions - functions common to all
22986 ** read-only.
22999 SQLITE_PRIVATE unsigned int sqlite3CoverageCounter;
23005 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
23012 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
23029 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
23076 "INT",
23172 ** * A b-tree cursor
23173 ** - In the main database or in an ephemeral database
23174 ** - On either an index or a table
23177 ** * A one-row "pseudotable" stored in a single register
23182 i8 iDb; /* Index of cursor database in db->aDb[] */
23191 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23194 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23207 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
23243 /* Return true if P is a null-only cursor
23246 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
23262 int iCol; /* Column for which the cache is valid */
23268 ** When a sub-program is executed (OP_Program), a structure of this type
23271 ** values stored in the Vdbe struct. When the sub-program is finished,
23273 ** restoring the state of the VM to as it was before the sub-program
23302 int nCursor; /* Number of entries in apCsr */
23303 int pc; /* Program Counter in parent (calling) frame */
23304 int nOp; /* Size of aOp array */
23305 int nMem; /* Number of entries in aMem */
23306 int nChildMem; /* Number of memory cells for child frame */
23307 int nChildCsr; /* Number of cursors for child frame */
23309 i64 nDbChange; /* Value of db->nChange */
23330 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
23335 int n; /* Number of characters in string value, excluding '\0' */
23341 int szMalloc; /* Size of the zMalloc allocation */
23344 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23363 ** UPDATE no-change flag set
23369 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23373 ** length Mem.n. Zero-terminated if
23429 /* Return TRUE if Mem X contains dynamically allocated content - anything
23433 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
23439 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
23442 ** True if Mem X is a NULL-nochng type.
23445 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
23446 && (X)->n==0 && (X)->u.nZero==0)
23457 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
23468 int iAuxOp; /* Instruction number of OP_Function opcode */
23469 int iAuxArg; /* Index of function argument. */
23493 int iOp; /* Instruction number of OP_Function */
23494 int isError; /* Error code returned by the function. */
23519 int addrExplain; /* OP_Explain for loop */
23520 int aAddrRange[6];
23521 int addrLoop; /* Address of "loops" counter */
23522 int addrVisit; /* Address of "rows visited" counter */
23523 int iSelectID; /* The "Select-ID" for this loop */
23528 /* The DblquoteStr object holds the text of a double-quoted
23532 ** list is consulted for each double-quoted identifier to see if the
23553 int nMem; /* Number of memory locations currently allocated */
23554 int nCursor; /* Number of slots in apCsr[] */
23556 int pc; /* The program counter */
23557 int rc; /* Value to return */
23559 int iStatement; /* Statement number (or 0 if has no opened stmt) */
23573 int nOp; /* Number of instructions in the program */
23574 int nOpAlloc; /* Slots allocated for aOp[] */
23580 i64 startTime; /* Time when query started - used for profiling */
23583 int rcApp; /* errcode set by sqlite3_result_error_code() */
23594 bft changeCntOn:1; /* True to update the change-counter */
23599 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23605 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23610 int nFrame; /* Number of frames in pFrame list */
23612 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23615 int nScan; /* Entries in aScan[] */
23635 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
23640 int iNewReg; /* Register for new.* values */
23641 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
23652 ** set of values on the right-hand side of an IN constraint.
23668 ** single-byte varint.
23680 void sqliteVdbePopStack(Vdbe*,int);
23681 SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p);
23682 SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*);
23683 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
23693 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
23695 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
23696 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
23697 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
23698 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
23700 SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
23707 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
23709 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
23710 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
23711 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
23712 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
23713 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
23715 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
23716 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
23727 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
23729 SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem*,int);
23732 SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
23734 SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
23736 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
23737 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
23738 SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
23740 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
23742 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
23744 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
23745 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
23746 SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
23747 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
23748 SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
23751 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
23753 SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
23756 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
23758 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
23759 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
23760 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
23762 SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
23766 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
23769 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
23771 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
23773 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
23776 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
23777 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
23778 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
23779 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
23780 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
23806 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
23810 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
23820 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
23821 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
23825 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
23826 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23871 ** we have to locate the state vector at run-time. In the more common
23887 SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){ in sqlite3StatusValue()
23901 ** The value of N is added to the current status value and the high-water
23905 ** mark is unchanged. N must be non-negative for StatusDown().
23907 SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){ in sqlite3StatusUp()
23918 SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){ in sqlite3StatusDown()
23925 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
23932 SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){ in sqlite3StatusHighwater()
23952 SQLITE_API int sqlite3_status64( in sqlite3_status64()
23953 int op, in sqlite3_status64()
23956 int resetFlag in sqlite3_status64()
23977 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ in sqlite3_status()
23979 int rc; in sqlite3_status()
23985 *pCurrent = (int)iCur; in sqlite3_status()
23986 *pHighwater = (int)iHwtr; in sqlite3_status()
23997 p = p->pNext; in countLookasideSlots()
24006 SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){ in sqlite3LookasideUsed()
24007 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
24008 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
24010 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
24011 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
24013 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
24014 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
24020 SQLITE_API int sqlite3_db_status( in sqlite3_db_status()
24022 int op, /* Status verb */ in sqlite3_db_status()
24023 int *pCurrent, /* Write current value here */ in sqlite3_db_status()
24024 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
24025 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
24027 int rc = SQLITE_OK; /* Return code */ in sqlite3_db_status()
24033 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
24038 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
24040 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24041 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
24042 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
24043 db->lookaside.pFree = 0; in sqlite3_db_status()
24046 p = db->lookaside.pSmallFree; in sqlite3_db_status()
24048 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24049 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
24050 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
24051 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
24064 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
24065 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
24067 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
24069 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
24081 int totalUsed = 0; in sqlite3_db_status()
24082 int i; in sqlite3_db_status()
24084 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24085 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
24088 int nByte = sqlite3PagerMemUsed(pPager); in sqlite3_db_status()
24107 int i; /* Used to iterate through schemas */ in sqlite3_db_status()
24108 int nByte = 0; /* Used to accumulate return value */ in sqlite3_db_status()
24111 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24112 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24113 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24114 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24115 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
24120 pSchema->tblHash.count in sqlite3_db_status()
24121 + pSchema->trigHash.count in sqlite3_db_status()
24122 + pSchema->idxHash.count in sqlite3_db_status()
24123 + pSchema->fkeyHash.count in sqlite3_db_status()
24125 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
24126 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
24127 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
24128 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
24130 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24133 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24138 db->pnBytesFreed = 0; in sqlite3_db_status()
24139 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24154 int nByte = 0; /* Used to accumulate return value */ in sqlite3_db_status()
24156 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24157 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24158 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24159 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ in sqlite3_db_status()
24162 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24163 db->pnBytesFreed = 0; in sqlite3_db_status()
24165 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
24182 int i; in sqlite3_db_status()
24187 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24188 if( db->aDb[i].pBt ){ in sqlite3_db_status()
24189 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
24193 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
24194 /* IMP: R-54100-20147 */ in sqlite3_db_status()
24195 /* IMP: R-29431-39229 */ in sqlite3_db_status()
24196 *pCurrent = (int)nRet & 0x7fffffff; in sqlite3_db_status()
24200 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
24205 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
24206 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
24214 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
24234 ** There is only one exported symbol in this file - the function
24243 ** 1970-01-01 00:00:00 is JD 2440587.5
24244 ** 2000-01-01 00:00:00 is JD 2451544.5
24246 ** This implementation requires years to be expressed as a 4-digit number
24247 ** which means that only dates between 0000-01-01 and 9999-12-31 can
24253 ** use the julian calendar for dates prior to 1582-10-15 and for some
24261 ** ISBN 0-943396-61-1
24262 ** Willmann-Bell, Inc
24288 int Y, M, D; /* Year, month, and day */
24289 int h, m; /* Hour and minutes */
24290 int tz; /* Timezone offset in minutes */
24310 ** of a four-character format specifiers ABCD is:
24324 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24325 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24326 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24327 ** the 2-digit day which is the last integer in the set.
24331 static int getDigits(const char *zDate, const char *zFormat, ...){ in getDigits()
24336 int cnt = 0; in getDigits()
24340 char N = zFormat[0] - '0'; in getDigits()
24341 char min = zFormat[1] - '0'; in getDigits()
24342 int val = 0; in getDigits()
24346 max = aMx[zFormat[2] - 'a']; in getDigits()
24349 while( N-- ){ in getDigits()
24353 val = val*10 + *zDate - '0'; in getDigits()
24356 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){ in getDigits()
24359 *va_arg(ap,int*) = val; in getDigits()
24370 ** Parse a timezone extension on the end of a date-time.
24373 ** (+/-)HH:MM
24380 ** of change in p->tz and return 0. If a parser error occurs,
24381 ** return non-zero.
24385 static int parseTimezone(const char *zDate, DateTime *p){ in parseTimezone()
24386 int sgn = 0; in parseTimezone()
24387 int nHr, nMn; in parseTimezone()
24388 int c; in parseTimezone()
24390 p->tz = 0; in parseTimezone()
24392 if( c=='-' ){ in parseTimezone()
24393 sgn = -1; in parseTimezone()
24398 p->isLocal = 0; in parseTimezone()
24399 p->isUtc = 1; in parseTimezone()
24409 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
24422 static int parseHhMmSs(const char *zDate, DateTime *p){ in parseHhMmSs()
24423 int h, m, s; in parseHhMmSs()
24439 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
24448 p->validJD = 0; in parseHhMmSs()
24449 p->rawS = 0; in parseHhMmSs()
24450 p->validHMS = 1; in parseHhMmSs()
24451 p->h = h; in parseHhMmSs()
24452 p->m = m; in parseHhMmSs()
24453 p->s = s + ms; in parseHhMmSs()
24463 p->isError = 1; in datetimeError()
24467 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24468 ** that the YYYY-MM-DD is according to the Gregorian calendar.
24473 int Y, M, D, A, B, X1, X2; in computeJD()
24475 if( p->validJD ) return; in computeJD()
24476 if( p->validYMD ){ in computeJD()
24477 Y = p->Y; in computeJD()
24478 M = p->M; in computeJD()
24479 D = p->D; in computeJD()
24481 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
24485 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
24490 Y--; in computeJD()
24494 B = 2 - A + (A/4); in computeJD()
24497 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
24498 p->validJD = 1; in computeJD()
24499 if( p->validHMS ){ in computeJD()
24500 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); in computeJD()
24501 if( p->tz ){ in computeJD()
24502 p->iJD -= p->tz*60000; in computeJD()
24503 p->validYMD = 0; in computeJD()
24504 p->validHMS = 0; in computeJD()
24505 p->tz = 0; in computeJD()
24506 p->isUtc = 1; in computeJD()
24507 p->isLocal = 0; in computeJD()
24513 ** Given the YYYY-MM-DD information current in p, determine if there
24514 ** is day-of-month overflow and set nFloor to the number of days that
24519 assert( p->validYMD || p->isError ); in computeFloor()
24520 assert( p->D>=0 && p->D<=31 ); in computeFloor()
24521 assert( p->M>=0 && p->M<=12 ); in computeFloor()
24522 if( p->D<=28 ){ in computeFloor()
24523 p->nFloor = 0; in computeFloor()
24524 }else if( (1<<p->M) & 0x15aa ){ in computeFloor()
24525 p->nFloor = 0; in computeFloor()
24526 }else if( p->M!=2 ){ in computeFloor()
24527 p->nFloor = (p->D==31); in computeFloor()
24528 }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){ in computeFloor()
24529 p->nFloor = p->D - 28; in computeFloor()
24531 p->nFloor = p->D - 29; in computeFloor()
24538 ** YYYY-MM-DD HH:MM:SS.FFF
24539 ** YYYY-MM-DD HH:MM:SS
24540 ** YYYY-MM-DD HH:MM
24541 ** YYYY-MM-DD
24544 ** on success and 1 if the input string is not a well-formed
24547 static int parseYyyyMmDd(const char *zDate, DateTime *p){ in parseYyyyMmDd()
24548 int Y, M, D, neg; in parseYyyyMmDd()
24550 if( zDate[0]=='-' ){ in parseYyyyMmDd()
24556 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
24564 p->validHMS = 0; in parseYyyyMmDd()
24568 p->validJD = 0; in parseYyyyMmDd()
24569 p->validYMD = 1; in parseYyyyMmDd()
24570 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
24571 p->M = M; in parseYyyyMmDd()
24572 p->D = D; in parseYyyyMmDd()
24574 if( p->tz ){ in parseYyyyMmDd()
24588 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ in setDateTimeToCurrent()
24589 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
24590 if( p->iJD>0 ){ in setDateTimeToCurrent()
24591 p->validJD = 1; in setDateTimeToCurrent()
24592 p->isUtc = 1; in setDateTimeToCurrent()
24593 p->isLocal = 0; in setDateTimeToCurrent()
24605 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
24608 p->s = r; in setRawDateNumber()
24609 p->rawS = 1; in setRawDateNumber()
24611 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
24612 p->validJD = 1; in setRawDateNumber()
24622 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
24626 ** In the first form, the +/-HH:MM is always optional. The fractional
24632 static int parseDateOrTime( in parseDateOrTime()
24650 p->useSubsec = 1; in parseDateOrTime()
24656 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24670 static int validJulianDay(sqlite3_int64 iJD){ in validJulianDay()
24678 int Z, A, B, C, D, E, X1; in computeYMD()
24679 if( p->validYMD ) return; in computeYMD()
24680 if( !p->validJD ){ in computeYMD()
24681 p->Y = 2000; in computeYMD()
24682 p->M = 1; in computeYMD()
24683 p->D = 1; in computeYMD()
24684 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
24688 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
24689 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
24690 A = Z + 1 + A - (A/4); in computeYMD()
24692 C = (int)((B - 122.1)/365.25); in computeYMD()
24694 E = (int)((B-D)/30.6001); in computeYMD()
24695 X1 = (int)(30.6001*E); in computeYMD()
24696 p->D = B - D - X1; in computeYMD()
24697 p->M = E<14 ? E-1 : E-13; in computeYMD()
24698 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
24700 p->validYMD = 1; in computeYMD()
24707 int day_ms, day_min; /* milliseconds, minutes into the day */ in computeHMS()
24708 if( p->validHMS ) return; in computeHMS()
24710 day_ms = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
24711 p->s = (day_ms % 60000)/1000.0; in computeHMS()
24713 p->m = day_min % 60; in computeHMS()
24714 p->h = day_min / 60; in computeHMS()
24715 p->rawS = 0; in computeHMS()
24716 p->validHMS = 1; in computeHMS()
24731 p->validYMD = 0; in clearYMD_HMS_TZ()
24732 p->validHMS = 0; in clearYMD_HMS_TZ()
24733 p->tz = 0; in clearYMD_HMS_TZ()
24743 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24757 ** using whatever operating-system specific localtime facility that
24759 ** non-zero on any kind of error.
24761 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24764 ** invoked in place of the OS-defined localtime() function.
24766 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24770 static int osLocaltime(time_t *t, struct tm *pTm){ in osLocaltime()
24771 int rc; in osLocaltime()
24820 static int toLocaltime( in toLocaltime()
24826 int iYearDiff; in toLocaltime()
24832 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ in toLocaltime()
24833 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ in toLocaltime()
24835 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in toLocaltime()
24842 iYearDiff = (2000 + x.Y%4) - x.Y; in toLocaltime()
24846 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24849 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24852 sqlite3_result_error(pCtx, "local time unavailable", -1); in toLocaltime()
24855 p->Y = sLocal.tm_year + 1900 - iYearDiff; in toLocaltime()
24856 p->M = sLocal.tm_mon + 1; in toLocaltime()
24857 p->D = sLocal.tm_mday; in toLocaltime()
24858 p->h = sLocal.tm_hour; in toLocaltime()
24859 p->m = sLocal.tm_min; in toLocaltime()
24860 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; in toLocaltime()
24861 p->validYMD = 1; in toLocaltime()
24862 p->validHMS = 1; in toLocaltime()
24863 p->validJD = 0; in toLocaltime()
24864 p->rawS = 0; in toLocaltime()
24865 p->tz = 0; in toLocaltime()
24866 p->isError = 0; in toLocaltime()
24876 ** Where NNN is an arbitrary floating-point number and "days" can be one
24899 if( !p->rawS || p->validJD ){ in autoAdjustDate()
24900 p->rawS = 0; in autoAdjustDate()
24901 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ in autoAdjustDate()
24902 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ in autoAdjustDate()
24904 double r = p->s*1000.0 + 210866760000000.0; in autoAdjustDate()
24906 p->iJD = (sqlite3_int64)(r + 0.5); in autoAdjustDate()
24907 p->validJD = 1; in autoAdjustDate()
24908 p->rawS = 0; in autoAdjustDate()
24913 ** Process a modifier to a date-time stamp. The modifiers are
24922 ** +/-YYYY-MM-DD HH:MM:SS.SSS
24942 static int parseModifier( in parseModifier()
24945 int n, /* Length of zMod in bytes */ in parseModifier()
24947 int idx /* Parameter index of the modifier */ in parseModifier()
24949 int rc = 1; in parseModifier()
24960 if( idx>1 ) return 1; /* IMP: R-33611-57934 */ in parseModifier()
24970 ** Resolve day-of-month overflow by rolling forward into the next in parseModifier()
24972 ** a no-op that is only included for symmetry. See "floor". in parseModifier()
24978 p->nFloor = 0; in parseModifier()
24986 ** Resolve day-of-month overflow by rolling back to the end of the in parseModifier()
24991 p->iJD -= p->nFloor*86400000; in parseModifier()
25001 ** Always interpret the prior number as a julian-day value. If this in parseModifier()
25007 if( idx>1 ) return 1; /* IMP: R-31176-64601 */ in parseModifier()
25008 if( p->validJD && p->rawS ){ in parseModifier()
25010 p->rawS = 0; in parseModifier()
25023 rc = p->isLocal ? SQLITE_OK : toLocaltime(p, pCtx); in parseModifier()
25024 p->isUtc = 0; in parseModifier()
25025 p->isLocal = 1; in parseModifier()
25034 ** Treat the current value of p->s as the number of in parseModifier()
25037 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
25038 if( idx>1 ) return 1; /* IMP: R-49255-55373 */ in parseModifier()
25039 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
25042 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
25043 p->validJD = 1; in parseModifier()
25044 p->rawS = 0; in parseModifier()
25050 if( p->isUtc==0 ){ in parseModifier()
25053 int cnt = 0; /* Safety to prevent infinite loop */ in parseModifier()
25057 iGuess = iOrigJD = p->iJD; in parseModifier()
25062 iGuess -= iErr; in parseModifier()
25068 iErr = new.iJD - iOrigJD; in parseModifier()
25071 p->iJD = iGuess; in parseModifier()
25072 p->validJD = 1; in parseModifier()
25073 p->isUtc = 1; in parseModifier()
25074 p->isLocal = 0; in parseModifier()
25087 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
25091 && r>=0.0 && r<7.0 && (n=(int)r)==r ){ in parseModifier()
25094 p->tz = 0; in parseModifier()
25095 p->validJD = 0; in parseModifier()
25097 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
25098 if( Z>n ) Z -= 7; in parseModifier()
25099 p->iJD += (n - Z)*86400000; in parseModifier()
25122 p->useSubsec = 1; in parseModifier()
25127 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
25130 p->validHMS = 1; in parseModifier()
25131 p->h = p->m = 0; in parseModifier()
25132 p->s = 0.0; in parseModifier()
25133 p->rawS = 0; in parseModifier()
25134 p->tz = 0; in parseModifier()
25135 p->validJD = 0; in parseModifier()
25137 p->D = 1; in parseModifier()
25140 p->M = 1; in parseModifier()
25141 p->D = 1; in parseModifier()
25149 case '-': in parseModifier()
25161 int i; in parseModifier()
25162 int Y,M,D,h,m,x; in parseModifier()
25168 if( z[n]=='-' ){ in parseModifier()
25177 if( z[n]=='-' ){ in parseModifier()
25178 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the in parseModifier()
25180 ** the range 0-11 and DD is limited to 0-30. in parseModifier()
25182 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */ in parseModifier()
25184 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25187 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25193 p->validJD = 0; in parseModifier()
25194 if( z0=='-' ){ in parseModifier()
25195 p->Y -= Y; in parseModifier()
25196 p->M -= M; in parseModifier()
25197 D = -D; in parseModifier()
25199 p->Y += Y; in parseModifier()
25200 p->M += M; in parseModifier()
25202 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25203 p->Y += x; in parseModifier()
25204 p->M -= x*12; in parseModifier()
25207 p->validHMS = 0; in parseModifier()
25208 p->validYMD = 0; in parseModifier()
25209 p->iJD += (i64)D*86400000; in parseModifier()
25224 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
25236 tx.iJD -= 43200000; in parseModifier()
25238 tx.iJD -= day*86400000; in parseModifier()
25239 if( z0=='-' ) tx.iJD = -tx.iJD; in parseModifier()
25242 p->iJD += tx.iJD; in parseModifier()
25253 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
25256 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
25257 p->nFloor = 0; in parseModifier()
25261 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
25267 p->M += (int)r; in parseModifier()
25268 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25269 p->Y += x; in parseModifier()
25270 p->M -= x*12; in parseModifier()
25272 p->validJD = 0; in parseModifier()
25273 r -= (int)r; in parseModifier()
25277 int y = (int)r; in parseModifier()
25280 assert( p->M>=0 && p->M<=12 ); in parseModifier()
25281 p->Y += y; in parseModifier()
25283 p->validJD = 0; in parseModifier()
25284 r -= (int)r; in parseModifier()
25289 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); in parseModifier()
25305 ** Process time function arguments. argv[0] is a date-time stamp.
25313 static int isDate( in isDate()
25315 int argc, in isDate()
25319 int i, n; in isDate()
25321 int eType; in isDate()
25342 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
25343 if( argc==1 && p->validYMD && p->D>28 ){ in isDate()
25344 /* Make sure a YYYY-MM-DD is normalized. in isDate()
25345 ** Example: 2023-02-31 -> 2023-03-03 */ in isDate()
25346 assert( p->validJD ); in isDate()
25347 p->validYMD = 0; in isDate()
25365 int argc, in juliandayFunc()
25379 ** the unix epoch of 1970-01-01 00:00:00 GMT.
25383 int argc, in unixepochFunc()
25390 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); in unixepochFunc()
25392 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); in unixepochFunc()
25400 ** Return YYYY-MM-DD HH:MM:SS
25404 int argc, in datetimeFunc()
25409 int Y, s, n; in datetimeFunc()
25413 if( Y<0 ) Y = -Y; in datetimeFunc()
25418 zBuf[5] = '-'; in datetimeFunc()
25421 zBuf[8] = '-'; in datetimeFunc()
25432 s = (int)(1000.0*x.s + 0.5); in datetimeFunc()
25442 s = (int)x.s; in datetimeFunc()
25449 zBuf[0] = '-'; in datetimeFunc()
25452 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); in datetimeFunc()
25464 int argc, in timeFunc()
25469 int s, n; in timeFunc()
25479 s = (int)(1000.0*x.s + 0.5); in timeFunc()
25489 s = (int)x.s; in timeFunc()
25502 ** Return YYYY-MM-DD
25506 int argc, in dateFunc()
25511 int Y; in dateFunc()
25515 if( Y<0 ) Y = -Y; in dateFunc()
25520 zBuf[5] = '-'; in dateFunc()
25523 zBuf[8] = '-'; in dateFunc()
25528 zBuf[0] = '-'; in dateFunc()
25539 ** In other words, compute the zero-based day number for the
25545 static int daysAfterJan01(DateTime *pDate){ in daysAfterJan01()
25549 assert( pDate->validJD ); in daysAfterJan01()
25554 return (int)((pDate->iJD-jan01.iJD+43200000)/86400000); in daysAfterJan01()
25565 static int daysAfterMonday(DateTime *pDate){ in daysAfterMonday()
25566 assert( pDate->validJD ); in daysAfterMonday()
25567 return (int)((pDate->iJD+43200000)/86400000) % 7; in daysAfterMonday()
25578 static int daysAfterSunday(DateTime *pDate){ in daysAfterSunday()
25579 assert( pDate->validJD ); in daysAfterSunday()
25580 return (int)((pDate->iJD+129600000)/86400000) % 7; in daysAfterSunday()
25588 ** %d day of month 01-31
25589 ** %e day of month 1-31
25591 ** %F ISO date. YYYY-MM-DD
25592 ** %G ISO year corresponding to %V 0000-9999.
25593 ** %g 2-digit ISO year corresponding to %V 00-99
25594 ** %H hour 00-24
25595 ** %k hour 0-24 (leading zero converted to space)
25596 ** %I hour 01-12
25597 ** %j day of year 001-366
25599 ** %l hour 1-12 (leading zero converted to space)
25600 ** %m month 01-12
25601 ** %M minute 00-59
25605 ** %s seconds since 1970-01-01
25606 ** %S seconds 00-59
25608 ** %u day of week 1-7 Monday==1, Sunday==7
25609 ** %w day of week 0-6 Sunday==0, Monday==1
25610 ** %U week of year 00-53 (First Sunday is start of week 01)
25611 ** %V week of year 01-53 (First week containing Thursday is week 01)
25612 ** %W week of year 00-53 (First Monday is start of week 01)
25613 ** %Y year 0000-9999
25618 int argc, in strftimeFunc()
25630 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
25632 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); in strftimeFunc()
25639 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25649 case 'f': { /* Fractional seconds. (Non-standard) */ in strftimeFunc()
25656 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); in strftimeFunc()
25664 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
25681 int h = x.h; in strftimeFunc()
25682 if( h>12 ) h -= 12; in strftimeFunc()
25691 case 'J': { /* Julian day number. (Non-standard) */ in strftimeFunc()
25719 (x.iJD - 21086676*(i64)10000000)/1000.0); in strftimeFunc()
25721 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
25727 sqlite3_str_appendf(&sRes,"%02d",(int)x.s); in strftimeFunc()
25731 sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s); in strftimeFunc()
25741 case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */ in strftimeFunc()
25743 (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7); in strftimeFunc()
25746 case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ in strftimeFunc()
25750 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
25756 case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ in strftimeFunc()
25758 (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7); in strftimeFunc()
25775 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25786 int NotUsed, in ctimeFunc()
25800 int NotUsed, in cdateFunc()
25813 ** +YYYY-MM-DD HH:MM:SS.SSS
25815 ** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
25821 ** ISO-8601 string. The unix timestamps are not supported by this
25826 int NotUsed1, in timediffFunc()
25830 int Y, M; in timediffFunc()
25840 Y = d1.Y - d2.Y; in timediffFunc()
25846 M = d1.M - d2.M; in timediffFunc()
25848 Y--; in timediffFunc()
25857 M--; in timediffFunc()
25860 Y--; in timediffFunc()
25862 d2.M--; in timediffFunc()
25865 d2.Y--; in timediffFunc()
25870 d1.iJD -= d2.iJD; in timediffFunc()
25873 sign = '-'; in timediffFunc()
25874 Y = d2.Y - d1.Y; in timediffFunc()
25880 M = d2.M - d1.M; in timediffFunc()
25882 Y--; in timediffFunc()
25891 M--; in timediffFunc()
25894 Y--; in timediffFunc()
25904 d1.iJD = d2.iJD - d1.iJD; in timediffFunc()
25910 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f", in timediffFunc()
25911 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s); in timediffFunc()
25923 int NotUsed, in ctimestampFunc()
25933 ** If the library is compiled to omit the full-scale date and time
25939 ** This function uses the C-library functions time(), gmtime()
25941 ** as the user-data for the function.
25945 int argc, in currentTimeFunc()
25960 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
25971 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
25985 int argc, in datedebugFunc()
26000 sqlite3_result_text(context, zJson, -1, sqlite3_free); in datedebugFunc()
26014 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
26015 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), in sqlite3RegisterDateTimeFunctions()
26016 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
26017 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
26018 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
26019 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
26022 PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ), in sqlite3RegisterDateTimeFunctions()
26029 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26030 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26061 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
26062 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
26063 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
26064 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
26065 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
26066 SQLITE_API int sqlite3_diskfull_pending = 0;
26067 SQLITE_API int sqlite3_diskfull = 0;
26074 SQLITE_API int sqlite3_open_file_count = 0;
26080 ** from within OsOpen()), but some third-party implementations may.
26102 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
26120 if( pId->pMethods ){ in sqlite3OsClose()
26121 pId->pMethods->xClose(pId); in sqlite3OsClose()
26122 pId->pMethods = 0; in sqlite3OsClose()
26125 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ in sqlite3OsRead()
26127 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
26129 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){ in sqlite3OsWrite()
26131 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
26133 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){ in sqlite3OsTruncate()
26134 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
26136 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){ in sqlite3OsSync()
26138 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
26140 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ in sqlite3OsFileSize()
26142 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
26144 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){ in sqlite3OsLock()
26147 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
26149 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){ in sqlite3OsUnlock()
26151 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
26153 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ in sqlite3OsCheckReservedLock()
26155 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
26166 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ in sqlite3OsFileControl()
26167 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
26177 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM in sqlite3OsFileControl()
26185 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
26192 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
26194 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){ in sqlite3OsFileControlHint()
26195 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
26198 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){ in sqlite3OsSectorSize()
26199 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
26202 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ in sqlite3OsDeviceCharacteristics()
26203 if( NEVER(id->pMethods==0) ) return 0; in sqlite3OsDeviceCharacteristics()
26204 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
26207 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ in sqlite3OsShmLock()
26208 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
26211 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
26213 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){ in sqlite3OsShmUnmap()
26214 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
26216 SQLITE_PRIVATE int sqlite3OsShmMap( in sqlite3OsShmMap()
26218 int iPage, in sqlite3OsShmMap()
26219 int pgsz, in sqlite3OsShmMap()
26220 int bExtend, /* True to extend file if necessary */ in sqlite3OsShmMap()
26224 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
26230 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ in sqlite3OsFetch()
26232 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
26234 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ in sqlite3OsUnfetch()
26235 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
26238 /* No-op stubs to use when memory-mapped I/O is disabled */
26239 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ in sqlite3OsFetch()
26243 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ in sqlite3OsUnfetch()
26252 SQLITE_PRIVATE int sqlite3OsOpen( in sqlite3OsOpen()
26256 int flags, in sqlite3OsOpen()
26257 int *pFlagsOut in sqlite3OsOpen()
26259 int rc; in sqlite3OsOpen()
26266 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
26267 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
26270 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ in sqlite3OsDelete()
26273 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; in sqlite3OsDelete()
26275 SQLITE_PRIVATE int sqlite3OsAccess( in sqlite3OsAccess()
26278 int flags, in sqlite3OsAccess()
26279 int *pResOut in sqlite3OsAccess()
26282 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
26284 SQLITE_PRIVATE int sqlite3OsFullPathname( in sqlite3OsFullPathname()
26287 int nPathOut, in sqlite3OsFullPathname()
26292 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
26297 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ in sqlite3OsDlOpen()
26298 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
26300 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ in sqlite3OsDlError()
26301 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
26304 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
26307 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
26310 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ in sqlite3OsRandomness()
26313 if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int); in sqlite3OsRandomness()
26317 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
26321 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ in sqlite3OsSleep()
26322 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
26324 SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){ in sqlite3OsGetLastError()
26325 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
26327 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ in sqlite3OsCurrentTimeInt64()
26328 int rc; in sqlite3OsCurrentTimeInt64()
26329 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
26335 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
26336 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
26339 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
26345 SQLITE_PRIVATE int sqlite3OsOpenMalloc( in sqlite3OsOpenMalloc()
26349 int flags, in sqlite3OsOpenMalloc()
26350 int *pOutFlags in sqlite3OsOpenMalloc()
26352 int rc; in sqlite3OsOpenMalloc()
26354 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
26382 SQLITE_PRIVATE int sqlite3OsInit(void){ in sqlite3OsInit()
26405 int rc = sqlite3_initialize(); in sqlite3_vfs_find()
26412 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
26414 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
26426 /* No-op */ in vfsUnlink()
26428 vfsList = pVfs->pNext; in vfsUnlink()
26431 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
26432 p = p->pNext; in vfsUnlink()
26434 if( p->pNext==pVfs ){ in vfsUnlink()
26435 p->pNext = pVfs->pNext; in vfsUnlink()
26445 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ in sqlite3_vfs_register()
26448 int rc = sqlite3_initialize(); in sqlite3_vfs_register()
26459 pVfs->pNext = vfsList; in sqlite3_vfs_register()
26462 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
26463 vfsList->pNext = pVfs; in sqlite3_vfs_register()
26473 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ in sqlite3_vfs_unregister()
26476 int rc = sqlite3_initialize(); in sqlite3_vfs_unregister()
26505 ** Most malloc failures are non-benign. After they occur, SQLite
26529 ** we have to locate the state vector at run-time. In the more common
26559 ** indicates that subsequent malloc failures are non-benign.
26590 ** This file contains a no-op memory allocation drivers for use when
26600 ** used when no other memory allocator is specified using compile-time
26606 ** No-op versions of all memory allocation routines
26608 static void *sqlite3MemMalloc(int nByte){ return 0; } in sqlite3MemMalloc()
26610 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; } in sqlite3MemRealloc()
26611 static int sqlite3MemSize(void *pPrior){ return 0; } in sqlite3MemSize()
26612 static int sqlite3MemRoundup(int n){ return n; } in sqlite3MemRoundup()
26613 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; } in sqlite3MemInit()
26619 ** Populate the low-level memory allocation function pointers in
26652 ** This file contains low-level memory allocation drivers for when
26653 ** SQLite will use the standard C-library malloc/realloc/free interface
26656 ** This file contains implementations of the low-level memory allocation
26664 ** C-preprocessor macro summary:
26671 ** a different name, using a separate -D
26687 ** used when no other memory allocator is specified using compile-time
26707 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
26712 ** Use standard C library malloc and free on non-Apple systems.
26729 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
26763 ** For this low-level routine, we are guaranteed that nByte>0 because
26767 static void *sqlite3MemMalloc(int nByte){ in sqlite3MemMalloc()
26797 ** For this low-level routine, we already know that pPrior!=0 since
26799 ** by higher-level routines.
26807 p--; in sqlite3MemFree()
26816 static int sqlite3MemSize(void *pPrior){ in sqlite3MemSize()
26819 return (int)SQLITE_MALLOCSIZE(pPrior); in sqlite3MemSize()
26824 p--; in sqlite3MemSize()
26825 return (int)p[0]; in sqlite3MemSize()
26833 ** For this low-level interface, we know that pPrior!=0. Cases where
26834 ** pPrior==0 while have been intercepted by higher-level routine and
26836 ** cases where nByte<=0 will have been intercepted by higher-level
26839 static void *sqlite3MemRealloc(void *pPrior, int nByte){ in sqlite3MemRealloc()
26852 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
26853 p--; in sqlite3MemRealloc()
26871 static int sqlite3MemRoundup(int n){ in sqlite3MemRoundup()
26878 static int sqlite3MemInit(void *NotUsed){ in sqlite3MemInit()
26880 int cpuCount; in sqlite3MemInit()
26913 ** Populate the low-level memory allocation function pointers in
26946 ** This file contains low-level memory allocation drivers for when
26947 ** SQLite will use the standard C-library malloc/realloc/free interface
26952 ** This file contains implementations of the low-level memory allocation
26967 extern int backtrace(void**,int);
26968 extern void backtrace_symbols_fd(void*const*,int,int);
26978 ** ------------------------------------------------------------------------
26980 ** ------------------------------------------------------------------------
26995 int iForeGuard; /* Guard word for sanity */
27031 int nBacktrace;
27032 void (*xBacktrace)(int, int, void **);
27037 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
27044 int disallow; /* Do not allow memory allocation */
27052 int nAlloc[NCSIZE]; /* Total number of allocations */
27053 int nCurrent[NCSIZE]; /* Current number of allocations */
27054 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
27062 static void adjustStats(int iSize, int increment){ in adjustStats()
27063 int i = ROUND8(iSize)/8; in adjustStats()
27064 if( i>NCSIZE-1 ){ in adjustStats()
27065 i = NCSIZE - 1; in adjustStats()
27074 mem.nCurrent[i]--; in adjustStats()
27087 int *pInt; in sqlite3MemsysGetHeader()
27089 int nReserve; in sqlite3MemsysGetHeader()
27092 p--; in sqlite3MemsysGetHeader()
27093 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
27094 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
27095 pInt = (int*)pAllocation; in sqlite3MemsysGetHeader()
27097 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD ); in sqlite3MemsysGetHeader()
27102 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
27109 static int sqlite3MemSize(void *p){ in sqlite3MemSize()
27115 return (int)pHdr->iSize; in sqlite3MemSize()
27121 static int sqlite3MemInit(void *NotUsed){ in sqlite3MemInit()
27143 static int sqlite3MemRoundup(int n){ in sqlite3MemRoundup()
27148 ** Fill a buffer with pseudo-random bytes. This is used to preset
27152 static void randomFill(char *pBuf, int nByte){ in randomFill()
27153 unsigned int x, y, r; in randomFill()
27157 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27160 *(int*)pBuf = r; in randomFill()
27162 nByte -= 4; in randomFill()
27164 while( nByte-- > 0 ){ in randomFill()
27165 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27175 static void *sqlite3MemMalloc(int nByte){ in sqlite3MemMalloc()
27179 int *pInt; in sqlite3MemMalloc()
27181 int totalSize; in sqlite3MemMalloc()
27182 int nReserve; in sqlite3MemMalloc()
27186 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) + in sqlite3MemMalloc()
27193 pHdr->pNext = 0; in sqlite3MemMalloc()
27194 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
27196 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
27201 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
27202 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
27203 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
27204 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
27207 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
27208 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
27211 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
27214 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
27219 pHdr->iSize = nByte; in sqlite3MemMalloc()
27221 pInt = (int*)&pHdr[1]; in sqlite3MemMalloc()
27222 pInt[nReserve/sizeof(int)] = REARGUARD; in sqlite3MemMalloc()
27224 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
27242 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
27244 if( pHdr->pPrev ){ in sqlite3MemFree()
27245 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
27246 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
27249 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
27251 if( pHdr->pNext ){ in sqlite3MemFree()
27252 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
27253 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
27256 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
27259 z -= pHdr->nTitle; in sqlite3MemFree()
27260 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
27261 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
27262 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
27276 static void *sqlite3MemRealloc(void *pPrior, int nByte){ in sqlite3MemRealloc()
27280 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
27284 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
27285 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
27286 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
27294 ** Populate the low-level memory allocation function pointers in
27318 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
27319 pHdr->eType = eType; in sqlite3MemdebugSetType()
27332 SQLITE_PRIVATE int sqlite3MemdebugHasType(const void *p, u8 eType){ in sqlite3MemdebugHasType()
27333 int rc = 1; in sqlite3MemdebugHasType()
27337 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
27338 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
27354 SQLITE_PRIVATE int sqlite3MemdebugNoType(const void *p, u8 eType){ in sqlite3MemdebugNoType()
27355 int rc = 1; in sqlite3MemdebugNoType()
27359 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
27360 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
27372 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){ in sqlite3MemdebugBacktrace()
27379 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){ in sqlite3MemdebugBacktraceCallback() argument
27387 unsigned int n = sqlite3Strlen30(zTitle) + 1; in sqlite3MemdebugSettitle()
27389 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
27398 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
27400 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
27401 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
27413 int i; in sqlite3MemdebugDump()
27420 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
27422 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
27424 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
27425 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
27428 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
27429 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
27434 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
27440 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
27442 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
27443 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
27451 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){ in sqlite3MemdebugMallocCount()
27452 int i; in sqlite3MemdebugMallocCount()
27453 int nTotal = 0; in sqlite3MemdebugMallocCount()
27495 ** mean that the library will use a memory-pool by default, just that
27536 ** two fields form a double-linked list of chunks of related sizes.
27573 ** True if we are evaluating an out-of-memory callback.
27575 int alarmBusy;
27601 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
27634 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
27636 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
27637 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
27640 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
27669 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
27670 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
27671 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
27674 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
27699 static void memsys3OutOfMemory(int nByte){ in memsys3OutOfMemory()
27713 ** size parameters for check-out and return a pointer to the
27720 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
27721 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
27722 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
27723 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
27724 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
27725 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
27737 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
27747 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
27749 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
27750 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
27751 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
27752 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
27753 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
27754 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
27755 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
27785 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
27789 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
27790 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
27795 size = i + size/4 - prev; in memsys3Merge()
27796 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
27797 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
27798 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
27818 static void *memsys3MallocUnsafe(int nByte){ in memsys3MallocUnsafe()
27838 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
27840 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
27844 int hash = nBlock % N_HASH; in memsys3MallocUnsafe()
27846 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
27879 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
27902 int i; in memsys3FreeUnsafe()
27906 i = p - mem3.aPool; in memsys3FreeUnsafe()
27907 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
27908 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27910 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
27911 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
27912 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
27917 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
27918 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
27919 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
27922 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27923 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27924 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27926 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27927 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
27929 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27930 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27931 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27938 ** size returned omits the 8-byte header overhead. This only
27941 static int memsys3Size(void *p){ in memsys3Size()
27945 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
27946 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
27952 static int memsys3Roundup(int n){ in memsys3Roundup()
27956 return ((n+11)&~7) - 4; in memsys3Roundup()
27963 static void *memsys3Malloc(int nBytes){ in memsys3Malloc()
27985 static void *memsys3Realloc(void *pPrior, int nBytes){ in memsys3Realloc()
27986 int nOld; in memsys3Realloc()
27996 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
28016 static int memsys3Init(void *NotUsed){ in memsys3Init()
28025 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
28071 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
28077 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
28082 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
28088 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
28090 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
28094 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
28099 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28108 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28113 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
28114 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
28130 ** Populate the low-level memory allocation function pointers in
28191 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
28200 ** N >= M*(1 + log2(n)/2) - n + 1
28223 int next; /* Index of next free chunk */
28224 int prev; /* Index of previous free chunk */
28229 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
28250 int szAtom; /* Smallest possible allocation in bytes */
28251 int nBlock; /* Number of szAtom sized blocks in zPool */
28264 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
28278 int aiFreelist[LOGMAX+1];
28295 ** structures, return a pointer to the idx-th such link.
28303 static void memsys5Unlink(int i, int iLogsize){ in memsys5Unlink()
28304 int next, prev; in memsys5Unlink()
28309 next = MEM5LINK(i)->next; in memsys5Unlink()
28310 prev = MEM5LINK(i)->prev; in memsys5Unlink()
28314 MEM5LINK(prev)->next = next; in memsys5Unlink()
28317 MEM5LINK(next)->prev = prev; in memsys5Unlink()
28325 static void memsys5Link(int i, int iLogsize){ in memsys5Link()
28326 int x; in memsys5Link()
28332 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
28333 MEM5LINK(i)->prev = -1; in memsys5Link()
28336 MEM5LINK(x)->prev = i; in memsys5Link()
28355 static int memsys5Size(void *p){ in memsys5Size()
28356 int iSize, i; in memsys5Size()
28358 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
28374 static void *memsys5MallocUnsafe(int nByte){ in memsys5MallocUnsafe()
28375 int i; /* Index of a mem5.aPool[] slot */ in memsys5MallocUnsafe()
28376 int iBin; /* Index into mem5.aiFreelist[] */ in memsys5MallocUnsafe()
28377 int iFullSz; /* Size of allocation rounded up to power of 2 */ in memsys5MallocUnsafe()
28378 int iLogsize; /* Log2 of iFullSz/POW2_MIN */ in memsys5MallocUnsafe()
28411 int newSize; in memsys5MallocUnsafe()
28413 iBin--; in memsys5MallocUnsafe()
28424 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
28446 int iBlock; in memsys5FreeUnsafe()
28451 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
28453 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
28455 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
28460 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
28463 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
28468 mem5.currentCount--; in memsys5FreeUnsafe()
28469 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
28476 int iBuddy; in memsys5FreeUnsafe()
28478 iBuddy = iBlock - size; in memsys5FreeUnsafe()
28510 static void *memsys5Malloc(int nBytes){ in memsys5Malloc()
28540 ** memsys5Round(). Hence nBytes is always a non-negative power
28545 static void *memsys5Realloc(void *pPrior, int nBytes){ in memsys5Realloc()
28546 int nOld; in memsys5Realloc()
28549 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
28572 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
28575 static int memsys5Roundup(int n){ in memsys5Roundup()
28576 int iFullSz; in memsys5Roundup()
28594 ** Examples: memsys5Log(1) -> 0
28595 ** memsys5Log(2) -> 1
28596 ** memsys5Log(4) -> 2
28597 ** memsys5Log(5) -> 3
28598 ** memsys5Log(8) -> 3
28599 ** memsys5Log(9) -> 4
28601 static int memsys5Log(int iValue){ in memsys5Log()
28602 int iLog; in memsys5Log()
28603 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
28613 static int memsys5Init(void *NotUsed){ in memsys5Init()
28614 int ii; /* Loop counter */ in memsys5Init()
28615 int nByte; /* Number of bytes of memory available to this allocator */ in memsys5Init()
28617 int nMinLog; /* Log base 2 of minimum allocation size in bytes */ in memsys5Init()
28618 int iOffset; /* An offset into mem5.aCtrl[] */ in memsys5Init()
28628 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
28637 while( (int)sizeof(Mem5Link)>mem5.szAtom ){ in memsys5Init()
28646 mem5.aiFreelist[ii] = -1; in memsys5Init()
28650 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
28651 int nAlloc = (1<<ii); in memsys5Init()
28684 int i, j, n; in sqlite3Memsys5Dump()
28685 int nMinLog; in sqlite3Memsys5Dump()
28700 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
28766 static SQLITE_WSD int mutexIsInit = 0;
28791 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
28798 int iType;
28802 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
28811 static int checkMutexHeld(sqlite3_mutex *p){ in checkMutexHeld()
28812 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
28814 static int checkMutexNotheld(sqlite3_mutex *p){ in checkMutexNotheld()
28815 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
28822 static int checkMutexInit(void){ in checkMutexInit()
28826 static int checkMutexEnd(void){ in checkMutexEnd()
28834 static sqlite3_mutex *checkMutexAlloc(int iType){ in checkMutexAlloc()
28846 p->iType = iType; in checkMutexAlloc()
28849 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
28854 p = &staticMutexes[iType-2]; in checkMutexAlloc()
28857 if( p->mutex==0 ){ in checkMutexAlloc()
28858 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
28859 if( p->mutex==0 ){ in checkMutexAlloc()
28879 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
28883 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
28898 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
28899 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
28903 "illegal multi-threaded access to database connection" in checkMutexEnter()
28906 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
28912 static int checkMutexTry(sqlite3_mutex *p){ in checkMutexTry()
28914 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
28922 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
28952 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
28953 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
28961 SQLITE_PRIVATE int sqlite3MutexInit(void){ in sqlite3MutexInit()
28962 int rc = SQLITE_OK; in sqlite3MutexInit()
28981 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
28982 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
28983 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
28984 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
28985 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
28986 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
28987 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
28988 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
28990 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
28996 GLOBAL(int, mutexIsInit) = 1; in sqlite3MutexInit()
29007 SQLITE_PRIVATE int sqlite3MutexEnd(void){ in sqlite3MutexEnd()
29008 int rc = SQLITE_OK; in sqlite3MutexEnd()
29014 GLOBAL(int, mutexIsInit) = 0; in sqlite3MutexEnd()
29023 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ in sqlite3_mutex_alloc()
29032 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ in sqlite3MutexAlloc()
29036 assert( GLOBAL(int, mutexIsInit) ); in sqlite3MutexAlloc()
29066 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){ in sqlite3_mutex_try()
29067 int rc = SQLITE_OK; in sqlite3_mutex_try()
29079 ** this function is a no-op.
29093 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){ in sqlite3_mutex_held()
29097 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){ in sqlite3_mutex_notheld()
29123 ** here are place-holders. Applications can substitute working
29124 ** mutex routines at start-time using the
29144 static int noopMutexInit(void){ return SQLITE_OK; } in noopMutexInit()
29145 static int noopMutexEnd(void){ return SQLITE_OK; } in noopMutexEnd()
29146 static sqlite3_mutex *noopMutexAlloc(int id){ in noopMutexAlloc()
29152 static int noopMutexTry(sqlite3_mutex *p){ in noopMutexTry()
29187 int id; /* The mutex type */
29188 int cnt; /* Number of entries without a matching leave */
29195 static int debugMutexHeld(sqlite3_mutex *pX){ in debugMutexHeld()
29197 return p==0 || p->cnt>0; in debugMutexHeld()
29199 static int debugMutexNotheld(sqlite3_mutex *pX){ in debugMutexNotheld()
29201 return p==0 || p->cnt==0; in debugMutexNotheld()
29207 static int debugMutexInit(void){ return SQLITE_OK; } in debugMutexInit()
29208 static int debugMutexEnd(void){ return SQLITE_OK; } in debugMutexEnd()
29215 static sqlite3_mutex *debugMutexAlloc(int id){ in debugMutexAlloc()
29216 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
29223 pNew->id = id; in debugMutexAlloc()
29224 pNew->cnt = 0; in debugMutexAlloc()
29230 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
29235 pNew = &aStatic[id-2]; in debugMutexAlloc()
29236 pNew->id = id; in debugMutexAlloc()
29248 assert( p->cnt==0 ); in debugMutexFree()
29249 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
29271 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
29272 p->cnt++; in debugMutexEnter()
29274 static int debugMutexTry(sqlite3_mutex *pX){ in debugMutexTry()
29276 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
29277 p->cnt++; in debugMutexTry()
29290 p->cnt--; in debugMutexLeave()
29291 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
29313 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
29314 ** is used regardless of the run-time threadsafety setting.
29354 ** home-grown mutexes. Encapsulate these conditions into a single #define.
29368 int id; /* Mutex type */
29371 volatile int nRef; /* Number of entrances */
29373 int trace; /* True to trace changes */
29397 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29402 static int pthreadMutexHeld(sqlite3_mutex *p){ in pthreadMutexHeld()
29403 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
29405 static int pthreadMutexNotheld(sqlite3_mutex *p){ in pthreadMutexNotheld()
29406 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
29426 static int pthreadMutexInit(void){ return SQLITE_OK; } in pthreadMutexInit()
29427 static int pthreadMutexEnd(void){ return SQLITE_OK; } in pthreadMutexEnd()
29459 ** cases where it really needs one. If a faster non-recursive mutex
29477 static sqlite3_mutex *pthreadMutexAlloc(int iType){ in pthreadMutexAlloc()
29500 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29506 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
29510 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
29518 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29520 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
29527 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
29532 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
29537 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
29549 assert( p->nRef==0 ); in pthreadMutexFree()
29551 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
29554 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
29576 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
29581 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
29582 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
29584 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
29591 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
29592 p->nRef++; in pthreadMutexEnter()
29594 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29595 assert( p->nRef==0 ); in pthreadMutexEnter()
29596 p->owner = self; in pthreadMutexEnter()
29597 p->nRef = 1; in pthreadMutexEnter()
29601 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
29603 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29605 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
29606 p->owner = pthread_self(); in pthreadMutexEnter()
29607 p->nRef++; in pthreadMutexEnter()
29612 if( p->trace ){ in pthreadMutexEnter()
29613 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
29617 static int pthreadMutexTry(sqlite3_mutex *p){ in pthreadMutexTry()
29618 int rc; in pthreadMutexTry()
29619 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
29624 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
29625 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
29627 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
29634 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
29635 p->nRef++; in pthreadMutexTry()
29637 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29638 assert( p->nRef==0 ); in pthreadMutexTry()
29639 p->owner = self; in pthreadMutexTry()
29640 p->nRef = 1; in pthreadMutexTry()
29647 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
29649 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29651 p->owner = pthread_self(); in pthreadMutexTry()
29652 p->nRef++; in pthreadMutexTry()
29661 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
29662 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
29677 p->nRef--; in pthreadMutexLeave()
29678 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
29680 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
29683 if( p->nRef==0 ){ in pthreadMutexLeave()
29684 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29687 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29691 if( p->trace ){ in pthreadMutexLeave()
29692 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
29797 ** Determine if we are dealing with Windows CE - which has a much reduced
29825 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
29852 int id; /* Mutex type */
29854 volatile int nRef; /* Number of entrances */
29879 static int winMutexHeld(sqlite3_mutex *p){ in winMutexHeld()
29880 return p->nRef!=0 && p->owner==GetCurrentThreadId(); in winMutexHeld()
29883 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){ in winMutexNotheld2()
29884 return p->nRef==0 || p->owner!=tid; in winMutexNotheld2()
29887 static int winMutexNotheld(sqlite3_mutex *p){ in winMutexNotheld()
29928 static int winMutex_isInit = 0;
29929 static int winMutex_isNt = -1; /* <0 means "need to query" */
29937 SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
29940 static int winMutexInit(void){ in winMutexInit()
29943 int i; in winMutexInit()
29962 static int winMutexEnd(void){ in winMutexEnd()
29967 int i; in winMutexEnd()
30007 ** cases where it really needs one. If a faster non-recursive mutex
30025 static sqlite3_mutex *winMutexAlloc(int iType){ in winMutexAlloc()
30033 p->id = iType; in winMutexAlloc()
30036 p->trace = 1; in winMutexAlloc()
30040 InitializeCriticalSectionEx(&p->mutex, 0, 0); in winMutexAlloc()
30042 InitializeCriticalSection(&p->mutex); in winMutexAlloc()
30049 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ in winMutexAlloc()
30054 p = &winMutex_staticMutexes[iType-2]; in winMutexAlloc()
30057 InterlockedCompareExchange(&p->trace, 1, 0); in winMutexAlloc()
30063 assert( p==0 || p->id==iType ); in winMutexAlloc()
30075 assert( p->nRef==0 && p->owner==0 ); in winMutexFree()
30076 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ in winMutexFree()
30077 DeleteCriticalSection(&p->mutex); in winMutexFree()
30103 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexEnter()
30108 EnterCriticalSection(&p->mutex); in winMutexEnter()
30110 assert( p->nRef>0 || p->owner==0 ); in winMutexEnter()
30111 p->owner = tid; in winMutexEnter()
30112 p->nRef++; in winMutexEnter()
30113 if( p->trace ){ in winMutexEnter()
30114 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexEnter()
30115 tid, p->id, p, p->trace, p->nRef)); in winMutexEnter()
30120 static int winMutexTry(sqlite3_mutex *p){ in winMutexTry()
30124 int rc = SQLITE_BUSY; in winMutexTry()
30126 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexTry()
30140 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); in winMutexTry()
30145 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ in winMutexTry()
30147 p->owner = tid; in winMutexTry()
30148 p->nRef++; in winMutexTry()
30156 if( p->trace ){ in winMutexTry()
30157 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", in winMutexTry()
30158 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); in winMutexTry()
30176 assert( p->nRef>0 ); in winMutexLeave()
30177 assert( p->owner==tid ); in winMutexLeave()
30178 p->nRef--; in winMutexLeave()
30179 if( p->nRef==0 ) p->owner = 0; in winMutexLeave()
30180 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in winMutexLeave()
30183 LeaveCriticalSection(&p->mutex); in winMutexLeave()
30185 if( p->trace ){ in winMutexLeave()
30186 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexLeave()
30187 tid, p->id, p, p->trace, p->nRef)); in winMutexLeave()
30234 ** Attempt to release up to n bytes of non-essential memory currently
30235 ** held by SQLite. An example of non-essential memory is memory used to
30238 SQLITE_API int sqlite3_release_memory(int n){ in sqlite3_release_memory()
30242 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine in sqlite3_release_memory()
30243 ** is a no-op returning zero if SQLite is not compiled with in sqlite3_release_memory()
30269 int nearlyFull;
30285 ** no-op.
30287 SQLITE_API int sqlite3_memory_alarm( in sqlite3_memory_alarm()
30288 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), in sqlite3_memory_alarm()
30300 ** Set the soft heap-size limit for the library. An argument of
30301 ** zero disables the limit. A negative argument is a no-op used to
30315 int rc = sqlite3_initialize(); in sqlite3_soft_heap_limit64()
30316 if( rc ) return -1; in sqlite3_soft_heap_limit64()
30331 excess = sqlite3_memory_used() - n; in sqlite3_soft_heap_limit64()
30332 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); in sqlite3_soft_heap_limit64()
30335 SQLITE_API void sqlite3_soft_heap_limit(int n){ in sqlite3_soft_heap_limit()
30341 ** Set the hard heap-size limit for the library. An argument of zero
30342 ** disables the hard heap limit. A negative argument is a no-op used
30355 int rc = sqlite3_initialize(); in sqlite3_hard_heap_limit64()
30356 if( rc ) return -1; in sqlite3_hard_heap_limit64()
30374 SQLITE_PRIVATE int sqlite3MallocInit(void){ in sqlite3MallocInit()
30375 int rc; in sqlite3MallocInit()
30391 ** Return true if the heap is currently under memory pressure - in other
30395 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ in sqlite3HeapNearlyFull()
30423 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ in sqlite3_memory_highwater()
30432 static void sqlite3MallocAlarm(int nByte){ in sqlite3MallocAlarm()
30441 ** This routine is called whenever an out-of-memory condition is seen,
30443 ** code debuggers when working on out-of-memory conditions, for example
30454 # define test_oom_breakpoint(X) /* No-op for production builds */
30461 static void mallocWithAlarm(int n, void **pp){ in mallocWithAlarm()
30463 int nFull; in mallocWithAlarm()
30467 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal in mallocWithAlarm()
30477 if( nUsed >= mem0.alarmThreshold - nFull ){ in mallocWithAlarm()
30482 if( nUsed >= mem0.hardLimit - nFull ){ in mallocWithAlarm()
30514 ** This provides a 256-byte safety margin for defense against 32-bit
30538 mallocWithAlarm((int)n, &p); in sqlite3Malloc()
30541 p = sqlite3GlobalConfig.m.xMalloc((int)n); in sqlite3Malloc()
30543 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ in sqlite3Malloc()
30552 SQLITE_API void *sqlite3_malloc(int n){ in sqlite3_malloc()
30569 static int isLookaside(sqlite3 *db, const void *p){ in isLookaside()
30570 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); in isLookaside()
30580 SQLITE_PRIVATE int sqlite3MallocSize(const void *p){ in sqlite3MallocSize()
30584 static int lookasideMallocSize(sqlite3 *db, const void *p){ in lookasideMallocSize()
30586 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; in lookasideMallocSize()
30588 return db->lookaside.szTrue; in lookasideMallocSize()
30591 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){ in sqlite3DbMallocSize()
30603 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ in sqlite3DbMallocSize()
30605 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbMallocSize()
30606 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30610 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbMallocSize()
30611 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30612 return db->lookaside.szTrue; in sqlite3DbMallocSize()
30628 if( p==0 ) return; /* IMP: R-49053-54554 */ in sqlite3_free()
30644 ** *db->pnBytesFreed.
30647 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); in measureAllocationSize()
30652 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
30653 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
30656 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFreeNN()
30659 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbFreeNN()
30661 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbFreeNN()
30663 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30667 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbFreeNN()
30668 db->lookaside.pSmallFree = pBuf; in sqlite3DbFreeNN()
30672 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbFreeNN()
30674 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30676 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbFreeNN()
30678 pBuf->pNext = db->lookaside.pFree; in sqlite3DbFreeNN()
30679 db->lookaside.pFree = pBuf; in sqlite3DbFreeNN()
30683 if( db->pnBytesFreed ){ in sqlite3DbFreeNN()
30696 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbNNFreeNN()
30698 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbNNFreeNN()
30700 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbNNFreeNN()
30702 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30706 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbNNFreeNN()
30707 db->lookaside.pSmallFree = pBuf; in sqlite3DbNNFreeNN()
30711 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbNNFreeNN()
30713 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30715 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbNNFreeNN()
30717 pBuf->pNext = db->lookaside.pFree; in sqlite3DbNNFreeNN()
30718 db->lookaside.pFree = pBuf; in sqlite3DbNNFreeNN()
30722 if( db->pnBytesFreed ){ in sqlite3DbNNFreeNN()
30732 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFree()
30740 int nOld, nNew, nDiff; in sqlite3Realloc()
30745 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ in sqlite3Realloc()
30748 sqlite3_free(pOld); /* IMP: R-26507-47431 */ in sqlite3Realloc()
30756 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second in sqlite3Realloc()
30759 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); in sqlite3Realloc()
30765 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); in sqlite3Realloc()
30766 nDiff = nNew - nOld; in sqlite3Realloc()
30768 mem0.alarmThreshold-nDiff ){ in sqlite3Realloc()
30770 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ in sqlite3Realloc()
30779 sqlite3MallocAlarm((int)nBytes); in sqlite3Realloc()
30785 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); in sqlite3Realloc()
30791 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ in sqlite3Realloc()
30799 SQLITE_API void *sqlite3_realloc(void *pOld, int n){ in sqlite3_realloc()
30803 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ in sqlite3_realloc()
30847 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); in dbMallocRawFinish()
30856 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
30863 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
30864 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
30884 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30885 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30886 if( n>db->lookaside.sz ){ in sqlite3DbMallocRawNN()
30887 if( !db->lookaside.bDisable ){ in sqlite3DbMallocRawNN()
30888 db->lookaside.anStat[1]++; in sqlite3DbMallocRawNN()
30889 }else if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30896 if( (pBuf = db->lookaside.pSmallFree)!=0 ){ in sqlite3DbMallocRawNN()
30897 db->lookaside.pSmallFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30898 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30900 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ in sqlite3DbMallocRawNN()
30901 db->lookaside.pSmallInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30902 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30907 if( (pBuf = db->lookaside.pFree)!=0 ){ in sqlite3DbMallocRawNN()
30908 db->lookaside.pFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30909 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30911 }else if( (pBuf = db->lookaside.pInit)!=0 ){ in sqlite3DbMallocRawNN()
30912 db->lookaside.pInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30913 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30916 db->lookaside.anStat[2]++; in sqlite3DbMallocRawNN()
30920 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30921 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30922 if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30939 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbRealloc()
30940 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ in sqlite3DbRealloc()
30942 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ in sqlite3DbRealloc()
30946 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ in sqlite3DbRealloc()
30947 if( n<=db->lookaside.szTrue ) return p; in sqlite3DbRealloc()
30956 if( db->mallocFailed==0 ){ in dbReallocFinish()
30972 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); in dbReallocFinish()
31030 int n; in sqlite3DbSpanDup()
31033 ** at least one non-space character */ in sqlite3DbSpanDup()
31037 n = (int)(zEnd - zStart); in sqlite3DbSpanDup()
31038 while( sqlite3Isspace(zStart[n-1]) ) n--; in sqlite3DbSpanDup()
31052 ** Call this routine to record the fact that an OOM (out-of-memory) error
31053 ** has happened. This routine will set db->mallocFailed, and also
31065 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ in sqlite3OomFault()
31066 db->mallocFailed = 1; in sqlite3OomFault()
31067 if( db->nVdbeExec>0 ){ in sqlite3OomFault()
31068 AtomicStore(&db->u1.isInterrupted, 1); in sqlite3OomFault()
31071 if( db->pParse ){ in sqlite3OomFault()
31073 sqlite3ErrorMsg(db->pParse, "out of memory"); in sqlite3OomFault()
31074 db->pParse->rc = SQLITE_NOMEM_BKPT; in sqlite3OomFault()
31075 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ in sqlite3OomFault()
31076 pParse->nErr++; in sqlite3OomFault()
31077 pParse->rc = SQLITE_NOMEM; in sqlite3OomFault()
31086 ** db->mallocFailed flag as necessary.
31092 if( db->mallocFailed && db->nVdbeExec==0 ){ in sqlite3OomClear()
31093 db->mallocFailed = 0; in sqlite3OomClear()
31094 AtomicStore(&db->u1.isInterrupted, 0); in sqlite3OomClear()
31095 assert( db->lookaside.bDisable>0 ); in sqlite3OomClear()
31103 static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){ in apiHandleError()
31104 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ in apiHandleError()
31109 return rc & db->errMask; in apiHandleError()
31121 ** If an OOM as occurred, then the connection error-code (the value
31124 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ in sqlite3ApiExit()
31126 ** Otherwise the read (and possible write) of db->mallocFailed in sqlite3ApiExit()
31130 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3ApiExit()
31131 if( db->mallocFailed || rc ){ in sqlite3ApiExit()
31145 ** This file contains code for a set of "printf"-like routines. These
31156 #define etRADIX 0 /* non-decimal integer types. %x %o */
31172 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
31173 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31180 ** An "etByte" is an 8-bit unsigned value.
31209 static const char aPrefix[] = "-x0\000X0";
31251 p->accError = eError; in sqlite3StrAccumSetError()
31252 if( p->mxAlloc ) sqlite3_str_reset(p); in sqlite3StrAccumSetError()
31253 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); in sqlite3StrAccumSetError()
31260 if( p->nArg<=p->nUsed ) return 0; in getIntArg()
31261 return sqlite3_value_int64(p->apArg[p->nUsed++]); in getIntArg()
31264 if( p->nArg<=p->nUsed ) return 0.0; in getDoubleArg()
31265 return sqlite3_value_double(p->apArg[p->nUsed++]); in getDoubleArg()
31268 if( p->nArg<=p->nUsed ) return 0; in getTextArg()
31269 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); in getTextArg()
31283 if( pAccum->accError ) return 0; in printfTempBuf()
31284 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ in printfTempBuf()
31288 z = sqlite3DbMallocRaw(pAccum->db, n); in printfTempBuf()
31305 ** Hard limit on the precision of floating-point conversions.
31319 int c; /* Next character in the format string */ in sqlite3_str_vappendf()
31321 int precision; /* Precision of the current field */ in sqlite3_str_vappendf()
31322 int length; /* Length of the field */ in sqlite3_str_vappendf()
31323 int idx; /* A general purpose loop counter */ in sqlite3_str_vappendf()
31324 int width; /* Width of the current field */ in sqlite3_str_vappendf()
31325 etByte flag_leftjustify; /* True if "-" flag is present */ in sqlite3_str_vappendf()
31335 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ in sqlite3_str_vappendf()
31340 int nOut; /* Size of the rendering buffer */ in sqlite3_str_vappendf()
31342 int exp, e2; /* exponent of real numbers */ in sqlite3_str_vappendf()
31352 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31355 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ in sqlite3_str_vappendf()
31369 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); in sqlite3_str_vappendf()
31382 precision = -1; in sqlite3_str_vappendf()
31385 case '-': flag_leftjustify = 1; break; in sqlite3_str_vappendf()
31405 unsigned wx = c - '0'; in sqlite3_str_vappendf()
31407 wx = wx*10 + c - '0'; in sqlite3_str_vappendf()
31419 fmt--; in sqlite3_str_vappendf()
31425 width = (int)getIntArg(pArgList); in sqlite3_str_vappendf()
31427 width = va_arg(ap,int); in sqlite3_str_vappendf()
31431 width = width >= -2147483647 ? -width : 0; in sqlite3_str_vappendf()
31448 precision = (int)getIntArg(pArgList); in sqlite3_str_vappendf()
31450 precision = va_arg(ap,int); in sqlite3_str_vappendf()
31453 precision = precision >= -2147483647 ? -precision : -1; in sqlite3_str_vappendf()
31459 px = px*10 + c - '0'; in sqlite3_str_vappendf()
31471 --fmt; in sqlite3_str_vappendf()
31486 xtype = infop->type; in sqlite3_str_vappendf()
31497 ** flag_leftjustify TRUE if a '-' is present or if the in sqlite3_str_vappendf()
31502 ** always non-negative. Zero is the default. in sqlite3_str_vappendf()
31504 ** is -1. in sqlite3_str_vappendf()
31509 assert( precision>=(-1) ); in sqlite3_str_vappendf()
31513 sizeof(char*)==sizeof(long int) ? 1 : 0; in sqlite3_str_vappendf()
31520 if( infop->flags & FLAG_SIGNED ){ in sqlite3_str_vappendf()
31528 v = va_arg(ap,long int); in sqlite3_str_vappendf()
31531 v = va_arg(ap,int); in sqlite3_str_vappendf()
31535 testcase( v==(-1) ); in sqlite3_str_vappendf()
31538 prefix = '-'; in sqlite3_str_vappendf()
31550 longvalue = va_arg(ap,unsigned long int); in sqlite3_str_vappendf()
31553 longvalue = va_arg(ap,unsigned int); in sqlite3_str_vappendf()
31558 if( flag_zeropad && precision<width-(prefix!=0) ){ in sqlite3_str_vappendf()
31559 precision = width-(prefix!=0); in sqlite3_str_vappendf()
31561 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ in sqlite3_str_vappendf()
31570 nOut = (int)n; in sqlite3_str_vappendf()
31572 bufpt = &zOut[nOut-1]; in sqlite3_str_vappendf()
31575 int x = (int)(longvalue % 10); in sqlite3_str_vappendf()
31579 *(--bufpt) = zOrd[x*2+1]; in sqlite3_str_vappendf()
31580 *(--bufpt) = zOrd[x*2]; in sqlite3_str_vappendf()
31583 const char *cset = &aDigits[infop->charset]; in sqlite3_str_vappendf()
31584 u8 base = infop->base; in sqlite3_str_vappendf()
31586 *(--bufpt) = cset[longvalue%base]; in sqlite3_str_vappendf()
31590 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31592 *(--bufpt) = '0'; /* Zero pad */ in sqlite3_str_vappendf()
31596 int nn = (length - 1)/3; /* Number of "," to insert */ in sqlite3_str_vappendf()
31597 int ix = (length - 1)%3 + 1; in sqlite3_str_vappendf()
31598 bufpt -= nn; in sqlite3_str_vappendf()
31601 ix--; in sqlite3_str_vappendf()
31604 nn--; in sqlite3_str_vappendf()
31609 if( prefix ) *(--bufpt) = prefix; /* Add sign */ in sqlite3_str_vappendf()
31610 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ in sqlite3_str_vappendf()
31613 pre = &aPrefix[infop->prefix]; in sqlite3_str_vappendf()
31614 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; in sqlite3_str_vappendf()
31616 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31622 int iRound; in sqlite3_str_vappendf()
31623 int j; in sqlite3_str_vappendf()
31637 iRound = -precision; in sqlite3_str_vappendf()
31655 memcpy(buf, "-Inf", 5); in sqlite3_str_vappendf()
31657 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31658 /* no-op */ in sqlite3_str_vappendf()
31668 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31669 prefix = '-'; in sqlite3_str_vappendf()
31674 exp = s.iDP-1; in sqlite3_str_vappendf()
31682 precision--; in sqlite3_str_vappendf()
31684 if( exp<-4 || exp>precision ){ in sqlite3_str_vappendf()
31687 precision = precision - exp; in sqlite3_str_vappendf()
31696 e2 = s.iDP - 1; in sqlite3_str_vappendf()
31719 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
31730 for(e2++; e2<0 && precision>0; precision--, e2++){ in sqlite3_str_vappendf()
31734 while( (precision--)>0 ){ in sqlite3_str_vappendf()
31739 while( bufpt[-1]=='0' ) *(--bufpt) = 0; in sqlite3_str_vappendf()
31741 if( bufpt[-1]=='.' ){ in sqlite3_str_vappendf()
31745 *(--bufpt) = 0; in sqlite3_str_vappendf()
31751 exp = s.iDP - 1; in sqlite3_str_vappendf()
31752 *(bufpt++) = aDigits[infop->charset]; in sqlite3_str_vappendf()
31754 *(bufpt++) = '-'; exp = -exp; in sqlite3_str_vappendf()
31770 length = (int)(bufpt-zOut); in sqlite3_str_vappendf()
31776 int i; in sqlite3_str_vappendf()
31777 int nPad = width - length; in sqlite3_str_vappendf()
31778 for(i=width; i>=nPad; i--){ in sqlite3_str_vappendf()
31779 bufpt[i] = bufpt[i-nPad]; in sqlite3_str_vappendf()
31782 while( nPad-- ) bufpt[i++] = '0'; in sqlite3_str_vappendf()
31789 *(va_arg(ap,int*)) = pAccum->nChar; in sqlite3_str_vappendf()
31813 unsigned int ch = va_arg(ap,unsigned int); in sqlite3_str_vappendf()
31836 width -= precision-1; in sqlite3_str_vappendf()
31838 sqlite3_str_appendchar(pAccum, width-1, ' '); in sqlite3_str_vappendf()
31842 precision--; in sqlite3_str_vappendf()
31845 if( nPrior > precision-1 ) nPrior = precision - 1; in sqlite3_str_vappendf()
31847 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ in sqlite3_str_vappendf()
31850 if( pAccum->accError ) break; in sqlite3_str_vappendf()
31852 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); in sqlite3_str_vappendf()
31853 precision -= nPrior; in sqlite3_str_vappendf()
31871 if( pAccum->nChar==0 in sqlite3_str_vappendf()
31872 && pAccum->mxAlloc in sqlite3_str_vappendf()
31875 && pAccum->accError==0 in sqlite3_str_vappendf()
31880 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31881 pAccum->zText = bufpt; in sqlite3_str_vappendf()
31882 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); in sqlite3_str_vappendf()
31883 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
31884 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3_str_vappendf()
31895 while( precision-- > 0 && z[0] ){ in sqlite3_str_vappendf()
31898 length = (int)(z - (unsigned char*)bufpt); in sqlite3_str_vappendf()
31903 length = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
31907 /* Adjust width to account for extra bytes in UTF-8 characters */ in sqlite3_str_vappendf()
31908 int ii = length - 1; in sqlite3_str_vappendf()
31909 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; in sqlite3_str_vappendf()
31916 int needQuote, isnull; in sqlite3_str_vappendf()
31934 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ in sqlite3_str_vappendf()
31961 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31966 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); in sqlite3_str_vappendf()
31967 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); in sqlite3_str_vappendf()
31973 if( pToken && pToken->n ){ in sqlite3_str_vappendf()
31974 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); in sqlite3_str_vappendf()
31975 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); in sqlite3_str_vappendf()
31983 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31986 if( pItem->zAlias && !flag_altform2 ){ in sqlite3_str_vappendf()
31987 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31988 }else if( pItem->zName ){ in sqlite3_str_vappendf()
31989 if( pItem->zDatabase ){ in sqlite3_str_vappendf()
31990 sqlite3_str_appendall(pAccum, pItem->zDatabase); in sqlite3_str_vappendf()
31993 sqlite3_str_appendall(pAccum, pItem->zName); in sqlite3_str_vappendf()
31994 }else if( pItem->zAlias ){ in sqlite3_str_vappendf()
31995 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31997 Select *pSel = pItem->pSelect; in sqlite3_str_vappendf()
31998 assert( pSel!=0 ); /* Because of tag-20240424-1 */ in sqlite3_str_vappendf()
31999 if( pSel->selFlags & SF_NestedFrom ){ in sqlite3_str_vappendf()
32000 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); in sqlite3_str_vappendf()
32001 }else if( pSel->selFlags & SF_MultiValue ){ in sqlite3_str_vappendf()
32002 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); in sqlite3_str_vappendf()
32003 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", in sqlite3_str_vappendf()
32004 pItem->u1.nRow); in sqlite3_str_vappendf()
32006 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); in sqlite3_str_vappendf()
32025 width -= length; in sqlite3_str_vappendf()
32035 sqlite3DbFree(pAccum->db, zExtra); in sqlite3_str_vappendf()
32054 if( db->errByteOffset!=(-2) ) return; in sqlite3RecordErrorByteOffset()
32055 pParse = db->pParse; in sqlite3RecordErrorByteOffset()
32057 zText =pParse->zTail; in sqlite3RecordErrorByteOffset()
32061 db->errByteOffset = (int)(z-zText); in sqlite3RecordErrorByteOffset()
32071 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) in sqlite3RecordErrorOffsetOfExpr()
32073 pExpr = pExpr->pLeft; in sqlite3RecordErrorOffsetOfExpr()
32076 db->errByteOffset = pExpr->w.iOfst; in sqlite3RecordErrorOffsetOfExpr()
32086 SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){ in sqlite3StrAccumEnlarge()
32088 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ in sqlite3StrAccumEnlarge()
32089 if( p->accError ){ in sqlite3StrAccumEnlarge()
32090 testcase(p->accError==SQLITE_TOOBIG); in sqlite3StrAccumEnlarge()
32091 testcase(p->accError==SQLITE_NOMEM); in sqlite3StrAccumEnlarge()
32094 if( p->mxAlloc==0 ){ in sqlite3StrAccumEnlarge()
32096 return p->nAlloc - p->nChar - 1; in sqlite3StrAccumEnlarge()
32098 char *zOld = isMalloced(p) ? p->zText : 0; in sqlite3StrAccumEnlarge()
32099 i64 szNew = p->nChar + N + 1; in sqlite3StrAccumEnlarge()
32100 if( szNew+p->nChar<=p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32103 szNew += p->nChar; in sqlite3StrAccumEnlarge()
32105 if( szNew > p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32110 p->nAlloc = (int)szNew; in sqlite3StrAccumEnlarge()
32112 if( p->db ){ in sqlite3StrAccumEnlarge()
32113 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32115 zNew = sqlite3Realloc(zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32118 assert( p->zText!=0 || p->nChar==0 ); in sqlite3StrAccumEnlarge()
32119 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); in sqlite3StrAccumEnlarge()
32120 p->zText = zNew; in sqlite3StrAccumEnlarge()
32121 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); in sqlite3StrAccumEnlarge()
32122 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3StrAccumEnlarge()
32130 return (int)N; in sqlite3StrAccumEnlarge()
32136 SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ in sqlite3_str_appendchar()
32137 testcase( p->nChar + (i64)N > 0x7fffffff ); in sqlite3_str_appendchar()
32138 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ in sqlite3_str_appendchar()
32141 while( (N--)>0 ) p->zText[p->nChar++] = c; in sqlite3_str_appendchar()
32148 ** This is a helper routine to sqlite3_str_append() that does special-case
32152 static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ in enlargeAndAppend()
32155 memcpy(&p->zText[p->nChar], z, N); in enlargeAndAppend()
32156 p->nChar += N; in enlargeAndAppend()
32164 SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ in sqlite3_str_append()
32166 assert( p->zText!=0 || p->nChar==0 || p->accError ); in sqlite3_str_append()
32168 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); in sqlite3_str_append()
32169 if( p->nChar+N >= p->nAlloc ){ in sqlite3_str_append()
32172 assert( p->zText ); in sqlite3_str_append()
32173 p->nChar += N; in sqlite3_str_append()
32174 memcpy(&p->zText[p->nChar-N], z, N); in sqlite3_str_append()
32179 ** Append the complete text of zero-terminated string z[] to the p string.
32187 ** Finish off a string by making sure it is zero-terminated.
32193 assert( p->mxAlloc>0 && !isMalloced(p) ); in strAccumFinishRealloc()
32194 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); in strAccumFinishRealloc()
32196 memcpy(zText, p->zText, p->nChar+1); in strAccumFinishRealloc()
32197 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in strAccumFinishRealloc()
32201 p->zText = zText; in strAccumFinishRealloc()
32205 if( p->zText ){ in sqlite3StrAccumFinish()
32206 p->zText[p->nChar] = 0; in sqlite3StrAccumFinish()
32207 if( p->mxAlloc>0 && !isMalloced(p) ){ in sqlite3StrAccumFinish()
32211 return p->zText; in sqlite3StrAccumFinish()
32219 if( p->accError ){ in sqlite3ResultStrAccum()
32220 sqlite3_result_error_code(pCtx, p->accError); in sqlite3ResultStrAccum()
32223 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); in sqlite3ResultStrAccum()
32254 SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){ in sqlite3_str_errcode()
32255 return p ? p->accError : SQLITE_NOMEM; in sqlite3_str_errcode()
32259 SQLITE_API int sqlite3_str_length(sqlite3_str *p){ in sqlite3_str_length()
32260 return p ? p->nChar : 0; in sqlite3_str_length()
32265 if( p==0 || p->nChar==0 ) return 0; in sqlite3_str_value()
32266 p->zText[p->nChar] = 0; in sqlite3_str_value()
32267 return p->zText; in sqlite3_str_value()
32275 sqlite3DbFree(p->db, p->zText); in sqlite3_str_reset()
32276 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; in sqlite3_str_reset()
32278 p->nAlloc = 0; in sqlite3_str_reset()
32279 p->nChar = 0; in sqlite3_str_reset()
32280 p->zText = 0; in sqlite3_str_reset()
32288 ** memory is used if not NULL. db->mallocFailed is set appropriately
32297 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ in sqlite3StrAccumInit()
32298 p->zText = zBase; in sqlite3StrAccumInit()
32299 p->db = db; in sqlite3StrAccumInit()
32300 p->nAlloc = n; in sqlite3StrAccumInit()
32301 p->mxAlloc = mx; in sqlite3StrAccumInit()
32302 p->nChar = 0; in sqlite3StrAccumInit()
32303 p->accError = 0; in sqlite3StrAccumInit()
32304 p->printfFlags = 0; in sqlite3StrAccumInit()
32312 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); in sqlite3_str_new()
32321 ** %-conversion extensions.
32329 db->aLimit[SQLITE_LIMIT_LENGTH]); in sqlite3VMPrintf()
32341 ** %-conversion extensions.
32354 ** %-conversion extensions.
32378 ** %-conversion extensions.
32405 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ in sqlite3_vsnprintf()
32420 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ in sqlite3_snprintf()
32442 ** stack space on small-stack systems when logging is disabled.
32453 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ in renderLogMsg()
32466 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){ in sqlite3_log()
32479 ** and segfaults if you give it a long long int.
32492 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); in sqlite3DebugPrintf()
32504 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
32527 p--; in sqlite3RCStrRef()
32528 p->nRCRef++; in sqlite3RCStrRef()
32539 p--; in sqlite3RCStrUnref()
32540 assert( p->nRCRef>0 ); in sqlite3RCStrUnref()
32541 if( p->nRCRef>=2 ){ in sqlite3RCStrUnref()
32542 p->nRCRef--; in sqlite3RCStrUnref()
32560 p->nRCRef = 1; in sqlite3RCStrNew()
32572 p--; in sqlite3RCStrResize()
32573 assert( p->nRCRef==1 ); in sqlite3RCStrResize()
32586 ** 2015-06-08
32618 p->iLevel++; in sqlite3TreeViewPush()
32621 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; in sqlite3TreeViewPush()
32630 p->iLevel--; in sqlite3TreeViewPop()
32631 if( p->iLevel<0 ){ in sqlite3TreeViewPop()
32643 int i; in sqlite3TreeViewLine()
32648 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ in sqlite3TreeViewLine()
32649 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); in sqlite3TreeViewLine()
32651 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); in sqlite3TreeViewLine()
32679 int nCol, in sqlite3TreeViewColumnList()
32682 int i; in sqlite3TreeViewColumnList()
32687 int colMoreToFollow = i<(nCol - 1); in sqlite3TreeViewColumnList()
32694 case COLTYPE_INT: printf(" INT"); break; in sqlite3TreeViewColumnList()
32702 printf(" X-%s", z); in sqlite3TreeViewColumnList()
32710 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); in sqlite3TreeViewColumnList()
32721 ** Generate a human-readable description of a WITH clause.
32724 int i; in sqlite3TreeViewWith()
32726 if( pWith->nCte==0 ) return; in sqlite3TreeViewWith()
32727 if( pWith->pOuter ){ in sqlite3TreeViewWith()
32728 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); in sqlite3TreeViewWith()
32732 if( pWith->nCte>0 ){ in sqlite3TreeViewWith()
32734 for(i=0; i<pWith->nCte; i++){ in sqlite3TreeViewWith()
32737 const struct Cte *pCte = &pWith->a[i]; in sqlite3TreeViewWith()
32739 sqlite3_str_appendf(&x, "%s", pCte->zName); in sqlite3TreeViewWith()
32740 if( pCte->pCols && pCte->pCols->nExpr>0 ){ in sqlite3TreeViewWith()
32742 int j; in sqlite3TreeViewWith()
32743 for(j=0; j<pCte->pCols->nExpr; j++){ in sqlite3TreeViewWith()
32744 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); in sqlite3TreeViewWith()
32749 if( pCte->eM10d!=M10d_Any ){ in sqlite3TreeViewWith()
32751 pCte->eM10d==M10d_No ? "NOT " : ""); in sqlite3TreeViewWith()
32753 if( pCte->pUse ){ in sqlite3TreeViewWith()
32754 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, in sqlite3TreeViewWith()
32755 pCte->pUse->nUse); in sqlite3TreeViewWith()
32758 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); in sqlite3TreeViewWith()
32759 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); in sqlite3TreeViewWith()
32767 ** Generate a human-readable description of a SrcList object.
32770 int i; in sqlite3TreeViewSrcList()
32772 for(i=0; i<pSrc->nSrc; i++){ in sqlite3TreeViewSrcList()
32773 const SrcItem *pItem = &pSrc->a[i]; in sqlite3TreeViewSrcList()
32775 int n = 0; in sqlite3TreeViewSrcList()
32779 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); in sqlite3TreeViewSrcList()
32780 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32782 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, in sqlite3TreeViewSrcList()
32783 pItem->colUsed, in sqlite3TreeViewSrcList()
32784 pItem->fg.rowidUsed ? "+rowid" : ""); in sqlite3TreeViewSrcList()
32786 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ in sqlite3TreeViewSrcList()
32787 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); in sqlite3TreeViewSrcList()
32788 }else if( pItem->fg.jointype & JT_LEFT ){ in sqlite3TreeViewSrcList()
32789 sqlite3_str_appendf(&x, " LEFT-JOIN"); in sqlite3TreeViewSrcList()
32790 }else if( pItem->fg.jointype & JT_RIGHT ){ in sqlite3TreeViewSrcList()
32791 sqlite3_str_appendf(&x, " RIGHT-JOIN"); in sqlite3TreeViewSrcList()
32792 }else if( pItem->fg.jointype & JT_CROSS ){ in sqlite3TreeViewSrcList()
32793 sqlite3_str_appendf(&x, " CROSS-JOIN"); in sqlite3TreeViewSrcList()
32795 if( pItem->fg.jointype & JT_LTORJ ){ in sqlite3TreeViewSrcList()
32798 if( pItem->fg.fromDDL ){ in sqlite3TreeViewSrcList()
32801 if( pItem->fg.isCte ){ in sqlite3TreeViewSrcList()
32802 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); in sqlite3TreeViewSrcList()
32804 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ in sqlite3TreeViewSrcList()
32807 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); in sqlite3TreeViewSrcList()
32808 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); in sqlite3TreeViewSrcList()
32809 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); in sqlite3TreeViewSrcList()
32810 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); in sqlite3TreeViewSrcList()
32811 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); in sqlite3TreeViewSrcList()
32812 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); in sqlite3TreeViewSrcList()
32815 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); in sqlite3TreeViewSrcList()
32817 if( pItem->pSelect ) n++; in sqlite3TreeViewSrcList()
32818 if( pItem->fg.isTabFunc ) n++; in sqlite3TreeViewSrcList()
32819 if( pItem->fg.isUsing ) n++; in sqlite3TreeViewSrcList()
32820 if( pItem->fg.isUsing ){ in sqlite3TreeViewSrcList()
32821 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); in sqlite3TreeViewSrcList()
32823 if( pItem->pSelect ){ in sqlite3TreeViewSrcList()
32824 sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc); in sqlite3TreeViewSrcList()
32825 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32826 Table *pTab = pItem->pTab; in sqlite3TreeViewSrcList()
32827 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); in sqlite3TreeViewSrcList()
32829 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) ); in sqlite3TreeViewSrcList()
32830 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0); in sqlite3TreeViewSrcList()
32833 if( pItem->fg.isTabFunc ){ in sqlite3TreeViewSrcList()
32834 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); in sqlite3TreeViewSrcList()
32841 ** Generate a human-readable description of a Select object.
32844 int n = 0; in sqlite3TreeViewSelect()
32845 int cnt = 0; in sqlite3TreeViewSelect()
32847 sqlite3TreeViewLine(pView, "nil-SELECT"); in sqlite3TreeViewSelect()
32851 if( p->pWith ){ in sqlite3TreeViewSelect()
32852 sqlite3TreeViewWith(pView, p->pWith, 1); in sqlite3TreeViewSelect()
32857 if( p->selFlags & SF_WhereBegin ){ in sqlite3TreeViewSelect()
32862 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), in sqlite3TreeViewSelect()
32863 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), in sqlite3TreeViewSelect()
32864 p->selId, p, p->selFlags, in sqlite3TreeViewSelect()
32865 (int)p->nSelectRow in sqlite3TreeViewSelect()
32869 if( p->pPrior ){ in sqlite3TreeViewSelect()
32873 if( p->pSrc && p->pSrc->nSrc ) n++; in sqlite3TreeViewSelect()
32874 if( p->pWhere ) n++; in sqlite3TreeViewSelect()
32875 if( p->pGroupBy ) n++; in sqlite3TreeViewSelect()
32876 if( p->pHaving ) n++; in sqlite3TreeViewSelect()
32877 if( p->pOrderBy ) n++; in sqlite3TreeViewSelect()
32878 if( p->pLimit ) n++; in sqlite3TreeViewSelect()
32880 if( p->pWin ) n++; in sqlite3TreeViewSelect()
32881 if( p->pWinDefn ) n++; in sqlite3TreeViewSelect()
32884 if( p->pEList ){ in sqlite3TreeViewSelect()
32885 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); in sqlite3TreeViewSelect()
32887 n--; in sqlite3TreeViewSelect()
32889 if( p->pWin ){ in sqlite3TreeViewSelect()
32891 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32892 sqlite3TreeViewLine(pView, "window-functions"); in sqlite3TreeViewSelect()
32893 for(pX=p->pWin; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32894 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32899 if( p->pSrc && p->pSrc->nSrc ){ in sqlite3TreeViewSelect()
32900 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32902 sqlite3TreeViewSrcList(pView, p->pSrc); in sqlite3TreeViewSelect()
32905 if( p->pWhere ){ in sqlite3TreeViewSelect()
32906 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewSelect()
32907 sqlite3TreeViewExpr(pView, p->pWhere, 0); in sqlite3TreeViewSelect()
32910 if( p->pGroupBy ){ in sqlite3TreeViewSelect()
32911 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); in sqlite3TreeViewSelect()
32913 if( p->pHaving ){ in sqlite3TreeViewSelect()
32914 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); in sqlite3TreeViewSelect()
32915 sqlite3TreeViewExpr(pView, p->pHaving, 0); in sqlite3TreeViewSelect()
32919 if( p->pWinDefn ){ in sqlite3TreeViewSelect()
32921 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); in sqlite3TreeViewSelect()
32922 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32923 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32928 if( p->pOrderBy ){ in sqlite3TreeViewSelect()
32929 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); in sqlite3TreeViewSelect()
32931 if( p->pLimit ){ in sqlite3TreeViewSelect()
32932 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); in sqlite3TreeViewSelect()
32933 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); in sqlite3TreeViewSelect()
32934 if( p->pLimit->pRight ){ in sqlite3TreeViewSelect()
32936 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); in sqlite3TreeViewSelect()
32941 if( p->pPrior ){ in sqlite3TreeViewSelect()
32943 switch( p->op ){ in sqlite3TreeViewSelect()
32950 p = p->pPrior; in sqlite3TreeViewSelect()
32994 ** Generate a human-readable explanation for a Window object
32997 int nElement = 0; in sqlite3TreeViewWindow()
32999 if( pWin->pFilter ){ in sqlite3TreeViewWindow()
33001 sqlite3TreeViewExpr(pView, pWin->pFilter, 0); in sqlite3TreeViewWindow()
33003 if( pWin->eFrmType==TK_FILTER ) return; in sqlite3TreeViewWindow()
33006 if( pWin->zName ){ in sqlite3TreeViewWindow()
33007 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); in sqlite3TreeViewWindow()
33011 if( pWin->zBase ) nElement++; in sqlite3TreeViewWindow()
33012 if( pWin->pOrderBy ) nElement++; in sqlite3TreeViewWindow()
33013 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++; in sqlite3TreeViewWindow()
33014 if( pWin->eExclude ) nElement++; in sqlite3TreeViewWindow()
33015 if( pWin->zBase ){ in sqlite3TreeViewWindow()
33016 sqlite3TreeViewPush(&pView, (--nElement)>0); in sqlite3TreeViewWindow()
33017 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); in sqlite3TreeViewWindow()
33020 if( pWin->pPartition ){ in sqlite3TreeViewWindow()
33021 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); in sqlite3TreeViewWindow()
33023 if( pWin->pOrderBy ){ in sqlite3TreeViewWindow()
33024 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); in sqlite3TreeViewWindow()
33026 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){ in sqlite3TreeViewWindow()
33029 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; in sqlite3TreeViewWindow()
33030 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; in sqlite3TreeViewWindow()
33032 pWin->bImplicitFrame ? " (implied)" : ""); in sqlite3TreeViewWindow()
33033 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); in sqlite3TreeViewWindow()
33034 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); in sqlite3TreeViewWindow()
33035 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); in sqlite3TreeViewWindow()
33038 if( pWin->eExclude ){ in sqlite3TreeViewWindow()
33041 switch( pWin->eExclude ){ in sqlite3TreeViewWindow()
33047 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); in sqlite3TreeViewWindow()
33061 ** Generate a human-readable explanation for a Window Function object
33067 pWin->pWFunc->zName, pWin->pWFunc->nArg); in sqlite3TreeViewWinFunc()
33074 ** Generate a human-readable explanation of an expression tree.
33086 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ in sqlite3TreeViewExpr()
33090 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); in sqlite3TreeViewExpr()
33092 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33095 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33103 if( pExpr->pAggInfo!=0 ){ in sqlite3TreeViewExpr()
33104 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); in sqlite3TreeViewExpr()
33110 switch( pExpr->op ){ in sqlite3TreeViewExpr()
33113 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33117 if( pExpr->iTable<0 ){ in sqlite3TreeViewExpr()
33120 if( pExpr->op2 ){ in sqlite3TreeViewExpr()
33121 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33126 pExpr->iColumn, zFlgs, zOp2); in sqlite3TreeViewExpr()
33130 pExpr->iTable, pExpr->iColumn, in sqlite3TreeViewExpr()
33131 pExpr->y.pTab, zFlgs); in sqlite3TreeViewExpr()
33134 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33139 if( pExpr->flags & EP_IntValue ){ in sqlite3TreeViewExpr()
33140 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); in sqlite3TreeViewExpr()
33142 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33149 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33155 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33170 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33177 pExpr->u.zToken, pExpr->iColumn); in sqlite3TreeViewExpr()
33181 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); in sqlite3TreeViewExpr()
33186 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); in sqlite3TreeViewExpr()
33193 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33194 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33229 int x; in sqlite3TreeViewExpr()
33231 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" in sqlite3TreeViewExpr()
33233 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); in sqlite3TreeViewExpr()
33234 assert( pExpr->pRight ); in sqlite3TreeViewExpr()
33235 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op in sqlite3TreeViewExpr()
33237 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); in sqlite3TreeViewExpr()
33244 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33245 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33252 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE in sqlite3TreeViewExpr()
33257 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", in sqlite3TreeViewExpr()
33258 pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33259 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33272 pFarg = pExpr->x.pList; in sqlite3TreeViewExpr()
33274 pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0; in sqlite3TreeViewExpr()
33280 if( pExpr->op==TK_AGG_FUNCTION ){ in sqlite3TreeViewExpr()
33282 pExpr->op2, pExpr->u.zToken, zFlgs, in sqlite3TreeViewExpr()
33283 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, in sqlite3TreeViewExpr()
33284 pExpr->iAgg, pExpr->pAggInfo); in sqlite3TreeViewExpr()
33285 }else if( pExpr->op2!=0 ){ in sqlite3TreeViewExpr()
33288 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33290 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; in sqlite3TreeViewExpr()
33291 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; in sqlite3TreeViewExpr()
33292 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; in sqlite3TreeViewExpr()
33293 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; in sqlite3TreeViewExpr()
33295 pExpr->u.zToken, zFlgs, zOp2); in sqlite3TreeViewExpr()
33297 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33300 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33301 if( pExpr->pLeft ){ in sqlite3TreeViewExpr()
33302 Expr *pOB = pExpr->pLeft; in sqlite3TreeViewExpr()
33303 assert( pOB->op==TK_ORDER ); in sqlite3TreeViewExpr()
33305 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY"); in sqlite3TreeViewExpr()
33316 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY"); in sqlite3TreeViewExpr()
33322 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33323 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33328 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33329 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33335 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33336 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); in sqlite3TreeViewExpr()
33339 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); in sqlite3TreeViewExpr()
33344 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33346 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33348 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33361 ** X is stored in pExpr->pLeft. in sqlite3TreeViewExpr()
33362 ** Y is stored in pExpr->pList->a[0].pExpr. in sqlite3TreeViewExpr()
33363 ** Z is stored in pExpr->pList->a[1].pExpr. in sqlite3TreeViewExpr()
33367 pX = pExpr->pLeft; in sqlite3TreeViewExpr()
33369 assert( pExpr->x.pList->nExpr==2 ); in sqlite3TreeViewExpr()
33370 pY = pExpr->x.pList->a[0].pExpr; in sqlite3TreeViewExpr()
33371 pZ = pExpr->x.pList->a[1].pExpr; in sqlite3TreeViewExpr()
33380 ** to a column in the new.* or old.* pseudo-tables available to in sqlite3TreeViewExpr()
33382 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn in sqlite3TreeViewExpr()
33383 ** is set to the column of the pseudo-table to read, or to -1 to in sqlite3TreeViewExpr()
33387 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); in sqlite3TreeViewExpr()
33392 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33394 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33400 switch( pExpr->affExpr ){ in sqlite3TreeViewExpr()
33407 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); in sqlite3TreeViewExpr()
33413 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33414 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33420 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); in sqlite3TreeViewExpr()
33425 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", in sqlite3TreeViewExpr()
33426 pExpr->iColumn, pExpr->iTable-1, in sqlite3TreeViewExpr()
33427 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); in sqlite3TreeViewExpr()
33428 assert( ExprUseXSelect(pExpr->pLeft) ); in sqlite3TreeViewExpr()
33429 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); in sqlite3TreeViewExpr()
33433 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); in sqlite3TreeViewExpr()
33434 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33441 tmp.op = pExpr->op2; in sqlite3TreeViewExpr()
33446 if( pExpr->iColumn<=0 ){ in sqlite3TreeViewExpr()
33450 pExpr->iColumn-1); in sqlite3TreeViewExpr()
33455 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); in sqlite3TreeViewExpr()
33461 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33462 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33465 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33472 ** Generate a human-readable explanation of an expression list.
33483 int i; in sqlite3TreeViewBareExprList()
33485 for(i=0; i<pList->nExpr; i++){ in sqlite3TreeViewBareExprList()
33486 int j = pList->a[i].u.x.iOrderByCol; in sqlite3TreeViewBareExprList()
33487 char *zName = pList->a[i].zEName; in sqlite3TreeViewBareExprList()
33488 int moreToFollow = i<pList->nExpr - 1; in sqlite3TreeViewBareExprList()
33494 switch( pList->a[i].fg.eEName ){ in sqlite3TreeViewBareExprList()
33499 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); in sqlite3TreeViewBareExprList()
33500 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); in sqlite3TreeViewBareExprList()
33501 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); in sqlite3TreeViewBareExprList()
33502 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); in sqlite3TreeViewBareExprList()
33515 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); in sqlite3TreeViewBareExprList()
33534 ** Generate a human-readable explanation of an id-list.
33545 int i; in sqlite3TreeViewBareIdList()
33547 for(i=0; i<pList->nId; i++){ in sqlite3TreeViewBareIdList()
33548 char *zName = pList->a[i].zName; in sqlite3TreeViewBareIdList()
33549 int moreToFollow = i<pList->nId - 1; in sqlite3TreeViewBareIdList()
33553 if( pList->eU4==EU4_NONE ){ in sqlite3TreeViewBareIdList()
33555 }else if( pList->eU4==EU4_IDX ){ in sqlite3TreeViewBareIdList()
33556 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx); in sqlite3TreeViewBareIdList()
33558 assert( pList->eU4==EU4_EXPR ); in sqlite3TreeViewBareIdList()
33559 if( pList->a[i].u4.pExpr==0 ){ in sqlite3TreeViewBareIdList()
33563 sqlite3TreeViewPush(&pView, i<pList->nId-1); in sqlite3TreeViewBareIdList()
33564 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0); in sqlite3TreeViewBareIdList()
33584 ** Generate a human-readable explanation of a list of Upsert objects
33594 int n; in sqlite3TreeViewUpsert()
33595 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); in sqlite3TreeViewUpsert()
33597 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); in sqlite3TreeViewUpsert()
33598 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); in sqlite3TreeViewUpsert()
33599 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); in sqlite3TreeViewUpsert()
33600 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); in sqlite3TreeViewUpsert()
33601 if( pUpsert->pUpsertWhere ){ in sqlite3TreeViewUpsert()
33602 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewUpsert()
33603 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); in sqlite3TreeViewUpsert()
33607 pUpsert = pUpsert->pNextUpsert; in sqlite3TreeViewUpsert()
33614 ** Generate a human-readable diagram of the data structure that go
33625 int n = 0; in sqlite3TreeViewDelete()
33636 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33641 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33647 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33653 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewDelete()
33656 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33662 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewDelete()
33670 ** Generate a human-readable diagram of the data structure that go
33679 int onError, in sqlite3TreeViewInsert()
33684 int n = 0; in sqlite3TreeViewInsert()
33703 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33708 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33714 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); in sqlite3TreeViewInsert()
33717 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33718 sqlite3TreeViewLine(pView, "DATA-SOURCE"); in sqlite3TreeViewInsert()
33723 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); in sqlite3TreeViewInsert()
33726 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33732 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewInsert()
33740 ** Generate a human-readable diagram of the data structure that go
33748 int onError, in sqlite3TreeViewUpdate()
33754 int n = 0; in sqlite3TreeViewUpdate()
33775 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33780 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33786 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); in sqlite3TreeViewUpdate()
33789 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33795 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewUpdate()
33798 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33804 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33810 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewUpdate()
33818 ** Show a human-readable graph of a TriggerStep
33826 int cnt = 0; in sqlite3TreeViewTriggerStep()
33829 moreToFollow || (showFullList && pStep->pNext!=0)); in sqlite3TreeViewTriggerStep()
33831 if( cnt++ && pStep->pNext==0 ){ in sqlite3TreeViewTriggerStep()
33835 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); in sqlite3TreeViewTriggerStep()
33836 }while( showFullList && (pStep = pStep->pNext)!=0 ); in sqlite3TreeViewTriggerStep()
33841 ** Show a human-readable graph of a Trigger
33849 int cnt = 0; in sqlite3TreeViewTrigger()
33852 moreToFollow || (showFullList && pTrigger->pNext!=0)); in sqlite3TreeViewTrigger()
33854 if( cnt++ && pTrigger->pNext==0 ){ in sqlite3TreeViewTrigger()
33858 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); in sqlite3TreeViewTrigger()
33860 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); in sqlite3TreeViewTrigger()
33862 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); in sqlite3TreeViewTrigger()
33869 ** These simplified versions of the tree-view routines omit unnecessary
33915 ** This file contains code to implement a pseudo-random number
33934 /* The RFC-7539 ChaCha20 block function
33936 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
33943 int i; in chacha_block()
33962 SQLITE_API void sqlite3_randomness(int N, void *pBuf){ in sqlite3_randomness()
33965 /* The "wsdPrng" macro will resolve to the pseudo-random number generator in sqlite3_randomness()
33967 ** we have to locate the state vector at run-time. In the more common in sqlite3_randomness()
34019 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); in sqlite3_randomness()
34020 wsdPrng.n -= N; in sqlite3_randomness()
34025 N -= wsdPrng.n; in sqlite3_randomness()
34076 ** This file presents a simple cross-platform threading interface for
34089 ** single-threaded if desired.
34101 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34107 int done; /* Set to true when thread finishes */
34114 SQLITE_PRIVATE int sqlite3ThreadCreate( in sqlite3ThreadCreate()
34120 int rc; in sqlite3ThreadCreate()
34124 /* This routine is never used in single-threaded mode */ in sqlite3ThreadCreate()
34131 p->xTask = xTask; in sqlite3ThreadCreate()
34132 p->pIn = pIn; in sqlite3ThreadCreate()
34140 rc = pthread_create(&p->tid, 0, xTask, pIn); in sqlite3ThreadCreate()
34143 p->done = 1; in sqlite3ThreadCreate()
34144 p->pOut = xTask(pIn); in sqlite3ThreadCreate()
34151 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ in sqlite3ThreadJoin()
34152 int rc; in sqlite3ThreadJoin()
34156 if( p->done ){ in sqlite3ThreadJoin()
34157 *ppOut = p->pOut; in sqlite3ThreadJoin()
34160 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; in sqlite3ThreadJoin()
34173 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34199 assert( p->id==GetCurrentThreadId() ); in sqlite3ThreadProc()
34201 assert( p->xTask!=0 ); in sqlite3ThreadProc()
34202 p->pResult = p->xTask(p->pIn); in sqlite3ThreadProc()
34209 SQLITE_PRIVATE int sqlite3ThreadCreate( in sqlite3ThreadCreate()
34229 p->xTask = xTask; in sqlite3ThreadCreate()
34230 p->pIn = pIn; in sqlite3ThreadCreate()
34231 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); in sqlite3ThreadCreate()
34232 if( p->tid==0 ){ in sqlite3ThreadCreate()
34236 if( p->xTask==0 ){ in sqlite3ThreadCreate()
34237 p->id = GetCurrentThreadId(); in sqlite3ThreadCreate()
34238 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34247 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ in sqlite3ThreadJoin()
34253 if( p->xTask==0 ){ in sqlite3ThreadJoin()
34254 /* assert( p->id==GetCurrentThreadId() ); */ in sqlite3ThreadJoin()
34256 assert( p->tid==0 ); in sqlite3ThreadJoin()
34258 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); in sqlite3ThreadJoin()
34259 rc = sqlite3Win32Wait((HANDLE)p->tid); in sqlite3ThreadJoin()
34261 bRc = CloseHandle((HANDLE)p->tid); in sqlite3ThreadJoin()
34264 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; in sqlite3ThreadJoin()
34273 /********************************* Single-Threaded **************************/
34289 SQLITE_PRIVATE int sqlite3ThreadCreate( in sqlite3ThreadCreate()
34302 p->xTask = xTask; in sqlite3ThreadCreate()
34303 p->pIn = pIn; in sqlite3ThreadCreate()
34305 p->xTask = 0; in sqlite3ThreadCreate()
34306 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34313 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ in sqlite3ThreadJoin()
34317 if( p->xTask ){ in sqlite3ThreadJoin()
34318 *ppOut = p->xTask(p->pIn); in sqlite3ThreadJoin()
34320 *ppOut = p->pResult; in sqlite3ThreadJoin()
34336 /****************************** End Single-Threaded *************************/
34352 ** This file contains routines used to translate between UTF-8,
34353 ** UTF-16, UTF-16BE, and UTF-16LE.
34355 ** Notes on UTF-8:
34357 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
34364 ** Notes on UTF-16: (with wwww+1==uuuuu)
34366 ** Word-0 Word-1 Value
34372 ** 0xff 0xfe little-endian utf-16 follows
34373 ** 0xfe 0xff big-endian utf-16 follows
34385 SQLITE_PRIVATE const int sqlite3one = 1;
34390 ** a multi-byte UTF8 character.
34429 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34430 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34441 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34442 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34449 ** Translate a single UTF-8 character. Return the unicode value.
34456 ** Notes On Invalid UTF-8:
34458 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
34459 ** be encoded as a multi-byte character. Any multi-byte character that
34463 ** If a multi-byte character attempts to encode a value between
34467 ** byte of a character are interpreted as single-byte characters
34471 ** * This routine accepts over-length UTF8 encodings
34472 ** for unicode values 0x80 and greater. It does not change over-length
34478 c = sqlite3Utf8Trans1[c-0xc0]; \
34489 unsigned int c; in sqlite3Utf8Read()
34492 ** For this routine, we assume the UTF8 string is always zero-terminated. in sqlite3Utf8Read()
34496 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8Read()
34509 ** more than n characters from the buffer. z[] is not zero-terminated.
34519 SQLITE_PRIVATE int sqlite3Utf8ReadLimited( in sqlite3Utf8ReadLimited()
34521 int n, in sqlite3Utf8ReadLimited()
34525 int i = 1; in sqlite3Utf8ReadLimited()
34529 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8ReadLimited()
34553 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ in sqlite3VdbeMemTranslate()
34559 unsigned int c; in sqlite3VdbeMemTranslate()
34561 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); in sqlite3VdbeMemTranslate()
34562 assert( pMem->flags&MEM_Str ); in sqlite3VdbeMemTranslate()
34563 assert( pMem->enc!=desiredEnc ); in sqlite3VdbeMemTranslate()
34564 assert( pMem->enc!=0 ); in sqlite3VdbeMemTranslate()
34565 assert( pMem->n>=0 ); in sqlite3VdbeMemTranslate()
34577 /* If the translation is between UTF-16 little and big endian, then in sqlite3VdbeMemTranslate()
34581 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34583 int rc; in sqlite3VdbeMemTranslate()
34589 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34590 zTerm = &zIn[pMem->n&~1]; in sqlite3VdbeMemTranslate()
34597 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34603 /* When converting from UTF-16, the maximum growth results from in sqlite3VdbeMemTranslate()
34604 ** translating a 2-byte character to a 4-byte UTF-8 character. in sqlite3VdbeMemTranslate()
34606 ** nul-terminator. in sqlite3VdbeMemTranslate()
34608 pMem->n &= ~1; in sqlite3VdbeMemTranslate()
34609 len = 2 * (sqlite3_int64)pMem->n + 1; in sqlite3VdbeMemTranslate()
34611 /* When converting from UTF-8 to UTF-16 the maximum growth is caused in sqlite3VdbeMemTranslate()
34612 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 in sqlite3VdbeMemTranslate()
34614 ** nul-terminator. in sqlite3VdbeMemTranslate()
34616 len = 2 * (sqlite3_int64)pMem->n + 2; in sqlite3VdbeMemTranslate()
34625 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34626 zTerm = &zIn[pMem->n]; in sqlite3VdbeMemTranslate()
34627 zOut = sqlite3DbMallocRaw(pMem->db, len); in sqlite3VdbeMemTranslate()
34633 if( pMem->enc==SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34635 /* UTF-8 -> UTF-16 Little-endian */ in sqlite3VdbeMemTranslate()
34642 /* UTF-8 -> UTF-16 Big-endian */ in sqlite3VdbeMemTranslate()
34648 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34652 if( pMem->enc==SQLITE_UTF16LE ){ in sqlite3VdbeMemTranslate()
34653 /* UTF-16 Little-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34662 int c2 = *(zIn++); in sqlite3VdbeMemTranslate()
34665 zIn -= 2; in sqlite3VdbeMemTranslate()
34673 int c2 = (*zIn++); in sqlite3VdbeMemTranslate()
34682 /* UTF-16 Big-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34691 int c2 = (*(zIn++))<<8; in sqlite3VdbeMemTranslate()
34694 zIn -= 2; in sqlite3VdbeMemTranslate()
34702 int c2 = ((*zIn++)<<8); in sqlite3VdbeMemTranslate()
34711 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34714 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); in sqlite3VdbeMemTranslate()
34716 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); in sqlite3VdbeMemTranslate()
34718 pMem->flags = c; in sqlite3VdbeMemTranslate()
34719 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34720 pMem->z = (char*)zOut; in sqlite3VdbeMemTranslate()
34721 pMem->zMalloc = pMem->z; in sqlite3VdbeMemTranslate()
34722 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); in sqlite3VdbeMemTranslate()
34740 ** This routine checks for a byte-order mark at the beginning of the
34741 ** UTF-16 string stored in *pMem. If one is present, it is removed and
34743 ** byte-swapping, it just sets Mem.enc appropriately.
34748 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){ in sqlite3VdbeMemHandleBom()
34749 int rc = SQLITE_OK; in sqlite3VdbeMemHandleBom()
34752 assert( pMem->n>=0 ); in sqlite3VdbeMemHandleBom()
34753 if( pMem->n>1 ){ in sqlite3VdbeMemHandleBom()
34754 u8 b1 = *(u8 *)pMem->z; in sqlite3VdbeMemHandleBom()
34755 u8 b2 = *(((u8 *)pMem->z) + 1); in sqlite3VdbeMemHandleBom()
34767 pMem->n -= 2; in sqlite3VdbeMemHandleBom()
34768 memmove(pMem->z, &pMem->z[2], pMem->n); in sqlite3VdbeMemHandleBom()
34769 pMem->z[pMem->n] = '\0'; in sqlite3VdbeMemHandleBom()
34770 pMem->z[pMem->n+1] = '\0'; in sqlite3VdbeMemHandleBom()
34771 pMem->flags |= MEM_Term; in sqlite3VdbeMemHandleBom()
34772 pMem->enc = bom; in sqlite3VdbeMemHandleBom()
34780 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
34786 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){ in sqlite3Utf8CharLen()
34787 int r = 0; in sqlite3Utf8CharLen()
34793 zTerm = (const u8*)(-1); in sqlite3Utf8CharLen()
34803 /* This test function is not currently used by the automated test-suite.
34808 ** Translate UTF-8 to UTF-8.
34810 ** This has the effect of making sure that the string is well-formed
34811 ** UTF-8. Miscoded characters are removed.
34813 ** The translation is done in-place and aborted if the output
34816 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){ in sqlite3Utf8To8()
34828 return (int)(zOut - zStart); in sqlite3Utf8To8()
34834 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
34835 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
34840 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){ in sqlite3Utf16to8()
34846 if( db->mallocFailed ){ in sqlite3Utf16to8()
34850 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34851 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34852 assert( m.z || db->mallocFailed ); in sqlite3Utf16to8()
34857 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
34859 ** in pZ. nChar must be non-negative.
34861 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){ in sqlite3Utf16ByteLen()
34862 int c; in sqlite3Utf16ByteLen()
34864 int n = 0; in sqlite3Utf16ByteLen()
34873 return (int)(z-(unsigned char const *)zIn) in sqlite3Utf16ByteLen()
34874 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); in sqlite3Utf16ByteLen()
34884 unsigned int i, t; in sqlite3UtfSelfTest()
34887 int n; in sqlite3UtfSelfTest()
34888 unsigned int c; in sqlite3UtfSelfTest()
34893 n = (int)(z-zBuf); in sqlite3UtfSelfTest()
34902 assert( (z-zBuf)==n ); in sqlite3UtfSelfTest()
34939 ** sqlite3FaultSim() function only returns non-zero during testing.
34941 ** During testing, if the test harness has set a fault-sim callback using
34943 ** each call to sqlite3FaultSim() is relayed to that application-supplied
34944 ** callback and the integer return value form the application-supplied
34953 SQLITE_PRIVATE int sqlite3FaultSim(int iTest){ in sqlite3FaultSim()
34954 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; in sqlite3FaultSim()
34966 SQLITE_PRIVATE int sqlite3IsNaN(double x){ in sqlite3IsNaN()
34967 int rc; /* The value return */ in sqlite3IsNaN()
34982 ** Return true if the floating point value is NaN or +Inf or -Inf.
34984 SQLITE_PRIVATE int sqlite3IsOverflow(double x){ in sqlite3IsOverflow()
34985 int rc; /* The value return */ in sqlite3IsOverflow()
34995 ** lower 30 bits of a 32-bit signed integer.
35001 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){ in sqlite3Strlen30()
35003 return 0x3fffffff & (int)strlen(z); in sqlite3Strlen30()
35010 ** The column type is an extra string stored after the zero-terminator on
35014 if( pCol->colFlags & COLFLAG_HASTYPE ){ in sqlite3ColumnType()
35015 return pCol->zCnName + strlen(pCol->zCnName) + 1; in sqlite3ColumnType()
35016 }else if( pCol->eCType ){ in sqlite3ColumnType()
35017 assert( pCol->eCType<=SQLITE_N_STDTYPE ); in sqlite3ColumnType()
35018 return (char*)sqlite3StdType[pCol->eCType-1]; in sqlite3ColumnType()
35025 ** Helper function for sqlite3Error() - called rarely. Broken out into
35029 static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ in sqlite3ErrorFinish()
35030 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorFinish()
35039 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ in sqlite3Error()
35041 db->errCode = err_code; in sqlite3Error()
35042 if( err_code || db->pErr ){ in sqlite3Error()
35045 db->errByteOffset = -1; in sqlite3Error()
35055 db->errCode = SQLITE_OK; in sqlite3ErrorClear()
35056 db->errByteOffset = -1; in sqlite3ErrorClear()
35057 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorClear()
35064 SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){ in sqlite3SystemError()
35068 int ii; in sqlite3SystemError()
35069 int iErr; in sqlite3SystemError()
35071 for(ii=0; ii<db->nDb; ii++){ in sqlite3SystemError()
35072 if( db->aDb[ii].pBt ){ in sqlite3SystemError()
35073 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); in sqlite3SystemError()
35075 db->iSysErrno = iErr; in sqlite3SystemError()
35085 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); in sqlite3SystemError()
35095 ** assumed to be encoded in UTF-8.
35101 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ in sqlite3ErrorWithMsg()
35103 db->errCode = err_code; in sqlite3ErrorWithMsg()
35107 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ in sqlite3ErrorWithMsg()
35113 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); in sqlite3ErrorWithMsg()
35121 sqlite3 *db = p->db; in sqlite3ProgressCheck()
35122 if( AtomicLoad(&db->u1.isInterrupted) ){ in sqlite3ProgressCheck()
35123 p->nErr++; in sqlite3ProgressCheck()
35124 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35127 if( db->xProgress ){ in sqlite3ProgressCheck()
35128 if( p->rc==SQLITE_INTERRUPT ){ in sqlite3ProgressCheck()
35129 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35130 }else if( (++p->nProgressSteps)>=db->nProgressOps ){ in sqlite3ProgressCheck()
35131 if( db->xProgress(db->pProgressArg) ){ in sqlite3ProgressCheck()
35132 p->nErr++; in sqlite3ProgressCheck()
35133 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35135 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35142 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
35154 sqlite3 *db = pParse->db; in sqlite3ErrorMsg()
35156 assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); in sqlite3ErrorMsg()
35157 db->errByteOffset = -2; in sqlite3ErrorMsg()
35161 if( db->errByteOffset<-1 ) db->errByteOffset = -1; in sqlite3ErrorMsg()
35162 if( db->suppressErr ){ in sqlite3ErrorMsg()
35164 if( db->mallocFailed ){ in sqlite3ErrorMsg()
35165 pParse->nErr++; in sqlite3ErrorMsg()
35166 pParse->rc = SQLITE_NOMEM; in sqlite3ErrorMsg()
35169 pParse->nErr++; in sqlite3ErrorMsg()
35170 sqlite3DbFree(db, pParse->zErrMsg); in sqlite3ErrorMsg()
35171 pParse->zErrMsg = zMsg; in sqlite3ErrorMsg()
35172 pParse->rc = SQLITE_ERROR; in sqlite3ErrorMsg()
35173 pParse->pWith = 0; in sqlite3ErrorMsg()
35182 SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){ in sqlite3ErrorToParser()
35184 if( db==0 || (pParse = db->pParse)==0 ) return errCode; in sqlite3ErrorToParser()
35185 pParse->rc = errCode; in sqlite3ErrorToParser()
35186 pParse->nErr++; in sqlite3ErrorToParser()
35191 ** Convert an SQL-style quoted string into a normal string by removing
35192 ** the quote characters. The conversion is done in-place. If the
35194 ** is a no-op.
35196 ** The input string must be zero-terminated. A new zero-terminator
35199 ** The return value is -1 if no dequoting occurs or the length of the
35203 ** 2002-02-14: This routine is extended to remove MS-Access style
35204 ** brackets from around identifiers. For example: "[a-b-c]" becomes
35205 ** "a-b-c".
35209 int i, j; in sqlite3Dequote()
35231 assert( sqlite3Isquote(p->u.zToken[0]) ); in sqlite3DequoteExpr()
35232 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; in sqlite3DequoteExpr()
35233 sqlite3Dequote(p->u.zToken); in sqlite3DequoteExpr()
35237 ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35242 assert( p!=0 || pParse->db->mallocFailed ); in sqlite3DequoteNumber()
35244 const char *pIn = p->u.zToken; in sqlite3DequoteNumber()
35245 char *pOut = p->u.zToken; in sqlite3DequoteNumber()
35246 int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X')); in sqlite3DequoteNumber()
35247 int iValue; in sqlite3DequoteNumber()
35248 assert( p->op==TK_QNUMBER ); in sqlite3DequoteNumber()
35249 p->op = TK_INTEGER; in sqlite3DequoteNumber()
35253 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; in sqlite3DequoteNumber()
35255 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) in sqlite3DequoteNumber()
35256 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) in sqlite3DequoteNumber()
35258 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); in sqlite3DequoteNumber()
35262 if( bHex ) p->op = TK_INTEGER; in sqlite3DequoteNumber()
35264 /* tag-20240227-a: If after dequoting, the number is an integer that in sqlite3DequoteNumber()
35266 ** parts of the code expect this. See also tag-20240227-b. */ in sqlite3DequoteNumber()
35267 if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ in sqlite3DequoteNumber()
35268 p->u.iValue = iValue; in sqlite3DequoteNumber()
35269 p->flags |= EP_IntValue; in sqlite3DequoteNumber()
35278 ** "abc" -> abc
35279 ** "ab""cd" -> (not possible because of the interior "")
35283 ** is always a no-op.
35286 unsigned int i; in sqlite3DequoteToken()
35287 if( p->n<2 ) return; in sqlite3DequoteToken()
35288 if( !sqlite3Isquote(p->z[0]) ) return; in sqlite3DequoteToken()
35289 for(i=1; i<p->n-1; i++){ in sqlite3DequoteToken()
35290 if( sqlite3Isquote(p->z[i]) ) return; in sqlite3DequoteToken()
35292 p->n -= 2; in sqlite3DequoteToken()
35293 p->z++; in sqlite3DequoteToken()
35300 p->z = z; in sqlite3TokenInit()
35301 p->n = sqlite3Strlen30(z); in sqlite3TokenInit()
35304 /* Convenient short-hand */
35311 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
35313 ** the contents of two buffers containing UTF-8 strings in a
35314 ** case-independent fashion, using the same definition of "case
35317 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ in sqlite3_stricmp()
35319 return zRight ? -1 : 0; in sqlite3_stricmp()
35325 SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ in sqlite3StrICmp()
35327 int c, x; in sqlite3StrICmp()
35336 c = (int)UpperToLower[c] - (int)UpperToLower[x]; in sqlite3StrICmp()
35344 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ in sqlite3_strnicmp()
35347 return zRight ? -1 : 0; in sqlite3_strnicmp()
35353 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } in sqlite3_strnicmp()
35354 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; in sqlite3_strnicmp()
35358 ** Compute an 8-bit hash on a string that is insensitive to case differences
35370 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35373 ** T. J. Dekker, "A Floating-Point Technique for Extending the
35374 ** Available Precision". 1971-07-26.
35380 ** binary64 rather than be carried around in an extended-precision in dekkerMul2()
35391 tx = x[0] - hx; in dekkerMul2()
35395 ty = y - hy; in dekkerMul2()
35399 cc = p - c + q + tx*ty; in dekkerMul2()
35402 x[1] = c - x[0]; in dekkerMul2()
35411 ** uses the encoding enc. The string is not necessarily zero-terminated.
35419 ** -1 => Not a valid number, but has a valid prefix which
35424 ** [+-]digits[E[+-]digits]
35425 ** [+-]digits.[digits][E[+-]digits]
35426 ** [+-].digits[E[+-]digits]
35438 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ in sqlite3AtoF()
35440 int incr; in sqlite3AtoF()
35443 int sign = 1; /* sign of significand */ in sqlite3AtoF()
35445 int d = 0; /* adjust exponent for shifting decimal point */ in sqlite3AtoF()
35446 int esign = 1; /* sign of exponent */ in sqlite3AtoF()
35447 int e = 0; /* exponent */ in sqlite3AtoF()
35448 int eValid = 1; /* True exponent is either not used or is well-formed */ in sqlite3AtoF()
35449 int nDigit = 0; /* Number of digits processed */ in sqlite3AtoF()
35450 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ in sqlite3AtoF()
35460 int i; in sqlite3AtoF()
35466 for(i=3-enc; i<length && z[i]==0; i+=2){} in sqlite3AtoF()
35467 if( i<length ) eType = -100; in sqlite3AtoF()
35477 if( *z=='-' ){ in sqlite3AtoF()
35478 sign = -1; in sqlite3AtoF()
35486 s = s*10 + (*z - '0'); in sqlite3AtoF()
35488 if( s>=((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35489 /* skip non-significant significand digits in sqlite3AtoF()
35503 if( s<((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35504 s = s*10 + (*z - '0'); in sqlite3AtoF()
35505 d--; in sqlite3AtoF()
35522 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ in sqlite3AtoF()
35525 if( *z=='-' ){ in sqlite3AtoF()
35526 esign = -1; in sqlite3AtoF()
35533 e = e<10000 ? (e*10 + (*z - '0')) : 10000; in sqlite3AtoF()
35545 *pResult = sign<0 ? -0.0 : +0.0; in sqlite3AtoF()
35555 e--; in sqlite3AtoF()
35567 while( e>=100 ){ e-=100; r *= 1.0e+100L; } in sqlite3AtoF()
35568 while( e>=10 ){ e-=10; r *= 1.0e+10L; } in sqlite3AtoF()
35569 while( e>=1 ){ e-=1; r *= 1.0e+01L; } in sqlite3AtoF()
35571 while( e<=-100 ){ e+=100; r *= 1.0e-100L; } in sqlite3AtoF()
35572 while( e<=-10 ){ e+=10; r *= 1.0e-10L; } in sqlite3AtoF()
35573 while( e<=-1 ){ e+=1; r *= 1.0e-01L; } in sqlite3AtoF()
35593 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); in sqlite3AtoF()
35596 e -= 100; in sqlite3AtoF()
35597 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3AtoF()
35600 e -= 10; in sqlite3AtoF()
35604 e -= 1; in sqlite3AtoF()
35608 while( e<=-100 ){ in sqlite3AtoF()
35610 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3AtoF()
35612 while( e<=-10 ){ in sqlite3AtoF()
35614 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3AtoF()
35616 while( e<=-1 ){ in sqlite3AtoF()
35618 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3AtoF()
35624 if( sign<0 ) *pResult = -*pResult; in sqlite3AtoF()
35628 /* return true if number and no extra non-whitespace characters after */ in sqlite3AtoF()
35632 return -1; in sqlite3AtoF()
35645 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
35652 SQLITE_PRIVATE int sqlite3Int64ToText(i64 v, char *zOut){ in sqlite3Int64ToText()
35653 int i; in sqlite3Int64ToText()
35657 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; in sqlite3Int64ToText()
35661 i = sizeof(zTemp)-2; in sqlite3Int64ToText()
35662 zTemp[sizeof(zTemp)-1] = 0; in sqlite3Int64ToText()
35663 while( 1 /*exit-by-break*/ ){ in sqlite3Int64ToText()
35667 i--; in sqlite3Int64ToText()
35669 if( v<0 ) zTemp[--i] = '-'; in sqlite3Int64ToText()
35670 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); in sqlite3Int64ToText()
35671 return sizeof(zTemp)-1-i; in sqlite3Int64ToText()
35675 ** Compare the 19-character string zNum against the text representation
35686 ** will return -8.
35688 static int compare2pow63(const char *zNum, int incr){ in compare2pow63()
35689 int c = 0; in compare2pow63()
35690 int i; in compare2pow63()
35694 c = (zNum[i*incr]-pow63[i])*10; in compare2pow63()
35697 c = zNum[18*incr] - '8'; in compare2pow63()
35698 testcase( c==(-1) ); in compare2pow63()
35706 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
35711 ** -1 Not even a prefix of the input text looks like an integer
35712 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35713 ** 1 Excess non-space text after the integer value
35714 ** 2 Integer too large for a 64-bit signed integer or is malformed
35718 ** The string is not necessarily zero-terminated. The encoding is
35721 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ in sqlite3Atoi64()
35722 int incr; in sqlite3Atoi64()
35724 int neg = 0; /* assume positive */ in sqlite3Atoi64()
35725 int i; in sqlite3Atoi64()
35726 int c = 0; in sqlite3Atoi64()
35727 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ in sqlite3Atoi64()
35728 int rc; /* Baseline return code */ in sqlite3Atoi64()
35738 for(i=3-enc; i<length && zNum[i]==0; i+=2){} in sqlite3Atoi64()
35745 if( *zNum=='-' ){ in sqlite3Atoi64()
35755 u = u*10 + c - '0'; in sqlite3Atoi64()
35762 ** from clang and -fsanitize=undefined. This test and assignment make in sqlite3Atoi64()
35764 ** them, but we must appease the undefined-behavior pharisees. */ in sqlite3Atoi64()
35767 *pNum = -(i64)u; in sqlite3Atoi64()
35773 rc = -1; in sqlite3Atoi64()
35774 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ in sqlite3Atoi64()
35777 int jj = i; in sqlite3Atoi64()
35780 rc = 1; /* Extra non-space text after the integer */ in sqlite3Atoi64()
35791 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ in sqlite3Atoi64()
35805 assert( u-1==LARGEST_INT64 ); in sqlite3Atoi64()
35813 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
35814 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
35819 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35821 ** 2 Integer too large for a 64-bit signed integer or is malformed
35824 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ in sqlite3DecOrHexToI64()
35830 int i, k; in sqlite3DecOrHexToI64()
35836 if( k-i>16 ) return 2; in sqlite3DecOrHexToI64()
35842 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); in sqlite3DecOrHexToI64()
35849 ** If zNum represents an integer that will fit in 32-bits, then set
35854 ** Any non-numeric characters that following zNum are ignored.
35856 ** input number to be zero-terminated.
35858 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){ in sqlite3GetInt32()
35860 int i, c; in sqlite3GetInt32()
35861 int neg = 0; in sqlite3GetInt32()
35862 if( zNum[0]=='-' ){ in sqlite3GetInt32()
35889 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ in sqlite3GetInt32()
35896 ** 2^31 -> 2147483648 in sqlite3GetInt32()
35902 testcase( v-neg==2147483647 ); in sqlite3GetInt32()
35903 if( v-neg>2147483647 ){ in sqlite3GetInt32()
35907 v = -v; in sqlite3GetInt32()
35909 *pValue = (int)v; in sqlite3GetInt32()
35914 ** Return a 32-bit integer value extracted from a string. If the
35917 SQLITE_PRIVATE int sqlite3Atoi(const char *z){ in sqlite3Atoi()
35918 int x = 0; in sqlite3Atoi()
35924 ** Decode a floating-point value into an approximate decimal
35928 ** n is positive. Or round to -n signficant digits after the
35933 ** stored in p->z[] which is a often (but not always) a pointer
35934 ** into the middle of p->zBuf[]. There are p->n significant digits.
35935 ** The p->z[] array is *not* zero-terminated.
35937 SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ in sqlite3FpDecode()
35938 int i; in sqlite3FpDecode()
35940 int e, exp = 0; in sqlite3FpDecode()
35941 p->isSpecial = 0; in sqlite3FpDecode()
35942 p->z = p->zBuf; in sqlite3FpDecode()
35947 p->sign = '-'; in sqlite3FpDecode()
35948 r = -r; in sqlite3FpDecode()
35950 p->sign = '+'; in sqlite3FpDecode()
35951 p->n = 1; in sqlite3FpDecode()
35952 p->iDP = 1; in sqlite3FpDecode()
35953 p->z = "0"; in sqlite3FpDecode()
35956 p->sign = '+'; in sqlite3FpDecode()
35961 p->isSpecial = 1 + (v!=0x7ff0000000000000LL); in sqlite3FpDecode()
35962 p->n = 0; in sqlite3FpDecode()
35963 p->iDP = 0; in sqlite3FpDecode()
35973 while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; } in sqlite3FpDecode()
35974 while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; } in sqlite3FpDecode()
35975 while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; } in sqlite3FpDecode()
35977 while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; } in sqlite3FpDecode()
35978 while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; } in sqlite3FpDecode()
35979 while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; } in sqlite3FpDecode()
35983 /* If high-precision floating point is not available using "long double", in sqlite3FpDecode()
35984 ** then use Dekker-style double-double computation to increase the in sqlite3FpDecode()
35998 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3FpDecode()
36002 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3FpDecode()
36006 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3FpDecode()
36009 while( rr[0]<9.223372036854774784e-83 ){ in sqlite3FpDecode()
36010 exp -= 100; in sqlite3FpDecode()
36011 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3FpDecode()
36014 exp -= 10; in sqlite3FpDecode()
36018 exp -= 1; in sqlite3FpDecode()
36022 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; in sqlite3FpDecode()
36027 i = sizeof(p->zBuf)-1; in sqlite3FpDecode()
36029 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } in sqlite3FpDecode()
36030 assert( i>=0 && i<sizeof(p->zBuf)-1 ); in sqlite3FpDecode()
36031 p->n = sizeof(p->zBuf) - 1 - i; in sqlite3FpDecode()
36032 assert( p->n>0 ); in sqlite3FpDecode()
36033 assert( p->n<sizeof(p->zBuf) ); in sqlite3FpDecode()
36034 p->iDP = p->n + exp; in sqlite3FpDecode()
36036 iRound = p->iDP - iRound; in sqlite3FpDecode()
36037 if( iRound==0 && p->zBuf[i+1]>='5' ){ in sqlite3FpDecode()
36039 p->zBuf[i--] = '0'; in sqlite3FpDecode()
36040 p->n++; in sqlite3FpDecode()
36041 p->iDP++; in sqlite3FpDecode()
36044 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ in sqlite3FpDecode()
36045 char *z = &p->zBuf[i+1]; in sqlite3FpDecode()
36047 p->n = iRound; in sqlite3FpDecode()
36049 int j = iRound-1; in sqlite3FpDecode()
36050 while( 1 /*exit-by-break*/ ){ in sqlite3FpDecode()
36055 p->z[i--] = '1'; in sqlite3FpDecode()
36056 p->n++; in sqlite3FpDecode()
36057 p->iDP++; in sqlite3FpDecode()
36060 j--; in sqlite3FpDecode()
36065 p->z = &p->zBuf[i+1]; in sqlite3FpDecode()
36066 assert( i+p->n < sizeof(p->zBuf) ); in sqlite3FpDecode()
36067 while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; } in sqlite3FpDecode()
36071 ** Try to convert z into an unsigned 32-bit integer. Return true on
36076 SQLITE_PRIVATE int sqlite3GetUInt32(const char *z, u32 *pI){ in sqlite3GetUInt32()
36078 int i; in sqlite3GetUInt32()
36080 v = v*10 + z[i] - '0'; in sqlite3GetUInt32()
36089 ** The variable-length integer encoding is as follows:
36096 ** 7 bits - A
36097 ** 14 bits - BA
36098 ** 21 bits - BBA
36099 ** 28 bits - BBBA
36100 ** 35 bits - BBBBA
36101 ** 42 bits - BBBBBA
36102 ** 49 bits - BBBBBBA
36103 ** 56 bits - BBBBBBBA
36104 ** 64 bits - BBBBBBBBC
36108 ** Write a 64-bit variable-length integer to memory starting at p[0].
36112 ** A variable-length integer consists of the lower 7 bits of each byte
36117 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ in putVarint64()
36118 int i, j, n; in putVarint64()
36123 for(i=7; i>=0; i--){ in putVarint64()
36136 for(i=0, j=n-1; j>=0; j--, i++){ in putVarint64()
36141 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){ in sqlite3PutVarint()
36168 ** Read a 64-bit variable-length integer from memory starting at p[0].
36311 b = p[-4]; in sqlite3GetVarint()
36322 ** Read a 32-bit variable-length integer from memory starting at p[0].
36325 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
36329 ** single-byte case. All code should use the MACRO version as
36330 ** this function assumes the single-byte case has already been handled.
36336 /* Assume that the single-byte case has already been handled by in sqlite3GetVarint32()
36341 /* This is the two-byte case */ in sqlite3GetVarint32()
36346 /* This is the three-byte case */ in sqlite3GetVarint32()
36363 ** 64-bit integer.
36365 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){ in sqlite3VarintLen()
36366 int i; in sqlite3VarintLen()
36373 ** Read or write a four-byte big-endian integer value.
36417 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){ in sqlite3HexToInt()
36435 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ in sqlite3HexToBlob()
36437 int i; in sqlite3HexToBlob()
36440 n--; in sqlite3HexToBlob()
36477 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){ in sqlite3SafetyCheckOk()
36483 eOpenState = db->eOpenState; in sqlite3SafetyCheckOk()
36494 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ in sqlite3SafetyCheckSickOrOk()
36496 eOpenState = db->eOpenState; in sqlite3SafetyCheckSickOrOk()
36509 ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
36510 ** the other 64-bit signed integer at *pA and store the result in *pA.
36514 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){ in sqlite3AddInt64()
36520 testcase( iB==-1 ); testcase( iB==0 ); in sqlite3AddInt64()
36522 testcase( iA>0 && LARGEST_INT64 - iA == iB ); in sqlite3AddInt64()
36523 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); in sqlite3AddInt64()
36524 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; in sqlite3AddInt64()
36526 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); in sqlite3AddInt64()
36527 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); in sqlite3AddInt64()
36528 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; in sqlite3AddInt64()
36534 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ in sqlite3SubInt64()
36540 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); in sqlite3SubInt64()
36542 *pA -= iB; in sqlite3SubInt64()
36545 return sqlite3AddInt64(pA, -iB); in sqlite3SubInt64()
36549 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){ in sqlite3MulInt64()
36563 if( -iA>LARGEST_INT64/-iB ) return 1; in sqlite3MulInt64()
36572 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
36573 ** if the integer has a value of -2147483648, return +2147483647
36575 SQLITE_PRIVATE int sqlite3AbsInt32(int x){ in sqlite3AbsInt32()
36577 if( x==(int)0x80000000 ) return 0x7fffffff; in sqlite3AbsInt32()
36578 return -x; in sqlite3AbsInt32()
36583 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
36589 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
36594 ** test.db-journal => test.nal
36595 ** test.db-wal => test.wal
36596 ** test.db-shm => test.shm
36597 ** test.db-mj7f3319fa => test.9fa
36604 int i, sz; in sqlite3FileSuffix3()
36606 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} in sqlite3FileSuffix3()
36607 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); in sqlite3FileSuffix3()
36625 5, 5, 5, /* 12-14 */ in sqlite3LogEstAdd()
36626 4, 4, 4, 4, /* 15-18 */ in sqlite3LogEstAdd()
36627 3, 3, 3, 3, 3, 3, /* 19-24 */ in sqlite3LogEstAdd()
36628 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ in sqlite3LogEstAdd()
36633 return a+x[a-b]; in sqlite3LogEstAdd()
36637 return b+x[b-a]; in sqlite3LogEstAdd()
36650 while( x<8 ){ y -= 10; x <<= 1; } in sqlite3LogEst()
36653 int i = 60 - __builtin_clzll(x); in sqlite3LogEst()
36657 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ in sqlite3LogEst()
36661 return a[x&7] + y - 10; in sqlite3LogEst()
36675 e = (a>>52) - 1022; in sqlite3LogEstFromDouble()
36686 if( n>=5 ) n -= 2; in sqlite3LogEstToInt()
36687 else if( n>=1 ) n -= 1; in sqlite3LogEstToInt()
36689 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); in sqlite3LogEstToInt()
36696 ** db->mallocFailed flag is set.
36709 ** is always zero-terminated.
36714 ** int nAlloc; // Number of allocated slots
36715 ** int nUsed; // Number of used slots
36717 ** int iValue; // Value for this entry
36718 ** int nSlot; // Slots used by this entry
36732 int nName, /* Bytes of text in zName */ in sqlite3VListAdd()
36733 int iVal /* Value to associate with zName */ in sqlite3VListAdd()
36735 int nInt; /* number of sizeof(int) objects needed for zName */ in sqlite3VListAdd()
36737 int i; /* Index in pIn[] where zName is stored */ in sqlite3VListAdd()
36744 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); in sqlite3VListAdd()
36766 SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){ in sqlite3VListNumToName()
36767 int i, mx; in sqlite3VListNumToName()
36782 SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ in sqlite3VListNameToNum()
36783 int i, mx; in sqlite3VListNameToNum()
36796 ** High-resolution hardware timer used for debugging and testing only.
36815 ** This file contains inline asm code for retrieving "high-performance"
36822 ** The following routine only works on Pentium-class (or newer) processors.
36824 ** processor and returns that value. This can be used for high-res
36834 unsigned int lo, hi; in sqlite3Hwtime()
36853 unsigned int lo, hi;
36906 ** This is the implementation of generic hash-tables
36919 pNew->first = 0;
36920 pNew->count = 0;
36921 pNew->htsize = 0;
36922 pNew->ht = 0;
36933 elem = pH->first;
36934 pH->first = 0;
36935 sqlite3_free(pH->ht);
36936 pH->ht = 0;
36937 pH->htsize = 0;
36939 HashElem *next_elem = elem->next;
36943 pH->count = 0;
36949 static unsigned int strHash(const char *z){
36950 unsigned int h = 0;
36952 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
36955 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
36973 pHead = pEntry->count ? pEntry->chain : 0;
36974 pEntry->count++;
36975 pEntry->chain = pNew;
36980 pNew->next = pHead;
36981 pNew->prev = pHead->prev;
36982 if( pHead->prev ){ pHead->prev->next = pNew; }
36983 else { pH->first = pNew; }
36984 pHead->prev = pNew;
36986 pNew->next = pH->first;
36987 if( pH->first ){ pH->first->prev = pNew; }
36988 pNew->prev = 0;
36989 pH->first = pNew;
37000 static int rehash(Hash *pH, unsigned int new_size){
37008 if( new_size==pH->htsize ) return 0;
37024 sqlite3_free(pH->ht);
37025 pH->ht = new_ht;
37026 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
37028 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
37029 unsigned int h = strHash(elem->pKey) % new_size;
37030 next_elem = elem->next;
37044 unsigned int *pHash /* Write the hash value here */
37047 unsigned int count; /* Number of elements left to test */
37048 unsigned int h; /* The computed hash */
37051 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
37053 h = strHash(pKey) % pH->htsize;
37054 pEntry = &pH->ht[h];
37055 elem = pEntry->chain;
37056 count = pEntry->count;
37059 elem = pH->first;
37060 count = pH->count;
37065 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
37068 elem = elem->next;
37069 count--;
37080 unsigned int h /* Hash value for the element */
37083 if( elem->prev ){
37084 elem->prev->next = elem->next;
37086 pH->first = elem->next;
37088 if( elem->next ){
37089 elem->next->prev = elem->prev;
37091 if( pH->ht ){
37092 pEntry = &pH->ht[h];
37093 if( pEntry->chain==elem ){
37094 pEntry->chain = elem->next;
37096 assert( pEntry->count>0 );
37097 pEntry->count--;
37100 pH->count--;
37101 if( pH->count==0 ){
37102 assert( pH->first==0 );
37103 assert( pH->count==0 );
37115 return findElementWithHash(pH, pKey, 0)->data;
37133 unsigned int h; /* the hash of the key modulo hash table size */
37140 if( elem->data ){
37141 void *old_data = elem->data;
37145 elem->data = data;
37146 elem->pKey = pKey;
37153 new_elem->pKey = pKey;
37154 new_elem->data = data;
37155 pH->count++;
37156 if( pH->count>=10 && pH->count > 2*pH->htsize ){
37157 if( rehash(pH, pH->count*2) ){
37158 assert( pH->htsize>0 );
37159 h = strHash(pKey) % pH->htsize;
37162 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
37178 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
37239 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
37240 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
37241 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
37270 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
37287 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
37304 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
37340 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
37378 ** 2022-09-06
37421 ** VFS - the database and the rollback journal.
37426 int isJournal; /* True if this is a journal file */
37427 unsigned int nJrnl; /* Space allocated for aJrnl[] */
37429 int szPage; /* Last known page size */
37430 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
37438 static int kvvfsClose(sqlite3_file*);
37439 static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37440 static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37441 static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37442 static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37443 static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
37444 static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
37445 static int kvvfsSyncDb(sqlite3_file*, int flags);
37446 static int kvvfsSyncJrnl(sqlite3_file*, int flags);
37447 static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
37448 static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
37449 static int kvvfsLock(sqlite3_file*, int);
37450 static int kvvfsUnlock(sqlite3_file*, int);
37451 static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
37452 static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
37453 static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
37454 static int kvvfsSectorSize(sqlite3_file*);
37455 static int kvvfsDeviceCharacteristics(sqlite3_file*);
37460 static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
37461 static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
37462 static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
37463 static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
37465 static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
37466 static int kvvfsSleep(sqlite3_vfs*, int microseconds);
37467 static int kvvfsCurrentTime(sqlite3_vfs*, double*);
37468 static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
37545 /* Forward declarations for the low-level storage engine
37547 static int kvstorageWrite(const char*, const char *zKey, const char *zData);
37548 static int kvstorageDelete(const char*, const char *zKey);
37549 static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
37561 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
37565 ** underlying key/value store to use - either "local" or "session".
37567 ** Both zKey and zData are zero-terminated pure text strings.
37571 static int kvstorageWrite(
37581 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
37582 (int)strlen(zData), zData,
37594 ** this routine is a no-op.
37596 static int kvstorageDelete(const char *zClass, const char *zKey){
37600 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
37611 ** not counting the final zero terminator. Return -1 if the key does
37617 static int kvstorageRead(
37621 int nBuf
37631 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37632 return -1;
37635 return (int)buf.st_size;
37638 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
37639 (int)buf.st_size));
37640 return (int)buf.st_size;
37647 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37648 return -1;
37650 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
37653 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
37655 return (int)n;
37672 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
37673 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
37674 int (*xDelete)(const char *zClass, const char *zKey);
37675 const int nKeySize;
37680 ** for JavaScript-side implementations in WASM builds. In such builds
37709 ** ---------------
37711 ** * Non-zero bytes are encoded as upper-case hexadecimal
37713 ** * A sequence of one or more zero-bytes that are not at the
37714 ** beginning of the buffer are encoded as a little-endian
37715 ** base-26 number using a..z. "a" means 0. "b" means 1,
37719 ** of hexadecimal and base-26 numbers, it is always clear where
37722 static int kvvfsEncode(const char *aData, int nData, char *aOut){
37723 int i, j;
37731 /* A sequence of 1 or more zeros is stored as a little-endian
37732 ** base-26 number using a..z as the digits. So one zero is "b".
37736 int k;
37738 i += k-1;
37750 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37751 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37752 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37753 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37754 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37755 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37756 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37757 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37759 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37760 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37761 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37762 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37763 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37764 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37765 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37766 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37775 static int kvvfsDecode(const char *a, char *aOut, int nOut){
37776 int i, j;
37777 int c;
37784 int n = 0;
37785 int mult = 1;
37789 n += (c - 'a')*mult;
37793 if( j+n>nOut ) return -1;
37809 ** Decode a complete journal file. Allocate space in pFile->aJrnl
37810 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
37813 ** The first few characters of the text encoding will be a little-endian
37814 ** base-26 number (digits a..z) that is the total number of bytes
37815 ** in the decoded journal file image. This base-26 number is followed
37817 ** separator is required to act as a terminator for the base-26 number.
37820 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
37821 const char *zTxt, /* Text encoding. Zero-terminated */
37822 int nTxt /* Bytes in zTxt, excluding zero terminator */
37824 unsigned int n = 0;
37825 int c, i, mult;
37829 n += (zTxt[i] - 'a')*mult;
37832 sqlite3_free(pFile->aJrnl);
37833 pFile->aJrnl = sqlite3_malloc64( n );
37834 if( pFile->aJrnl==0 ){
37835 pFile->nJrnl = 0;
37838 pFile->nJrnl = n;
37839 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
37840 if( n<pFile->nJrnl ){
37841 sqlite3_free(pFile->aJrnl);
37842 pFile->aJrnl = 0;
37843 pFile->nJrnl = 0;
37853 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
37856 static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
37859 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
37865 ** Close an kvvfs-file.
37867 static int kvvfsClose(sqlite3_file *pProtoFile){
37870 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
37871 pFile->isJournal ? "journal" : "db"));
37872 sqlite3_free(pFile->aJrnl);
37873 sqlite3_free(pFile->aData);
37878 ** Read from the -journal file.
37880 static int kvvfsReadJrnl(
37883 int iAmt,
37887 assert( pFile->isJournal );
37888 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37889 if( pFile->aJrnl==0 ){
37890 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
37897 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
37900 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
37902 if( iOfst+iAmt>pFile->nJrnl ){
37905 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
37912 static int kvvfsReadDb(
37915 int iAmt,
37919 unsigned int pgno;
37920 int got, n;
37922 char *aData = pFile->aData;
37925 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37930 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
37933 pFile->szPage = iAmt;
37939 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
37940 aData, SQLITE_KVOS_SZ-1);
37946 int k = iOfst+iAmt;
37948 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
37960 memset(zBuf+n, 0, iAmt-n);
37968 ** Write into the -journal file.
37970 static int kvvfsWriteJrnl(
37973 int iAmt,
37978 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37980 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
37981 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
37985 pFile->aJrnl = aNew;
37986 if( pFile->nJrnl<iOfst ){
37987 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
37989 pFile->nJrnl = iEnd;
37991 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
37998 static int kvvfsWriteDb(
38001 int iAmt,
38005 unsigned int pgno;
38007 char *aData = pFile->aData;
38008 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38010 assert( (iAmt & (iAmt-1))==0 );
38011 assert( pFile->szPage<0 || pFile->szPage==iAmt );
38012 pFile->szPage = iAmt;
38016 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
38019 if( iOfst+iAmt > pFile->szDb ){
38020 pFile->szDb = iOfst + iAmt;
38026 ** Truncate an kvvfs-file.
38028 static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
38030 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
38032 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
38033 sqlite3_free(pFile->aJrnl);
38034 pFile->aJrnl = 0;
38035 pFile->nJrnl = 0;
38038 static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
38040 if( pFile->szDb>size
38041 && pFile->szPage>0
38042 && (size % pFile->szPage)==0
38045 unsigned int pgno, pgnoMax;
38046 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
38047 pgno = 1 + size/pFile->szPage;
38048 pgnoMax = 2 + pFile->szDb/pFile->szPage;
38051 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
38054 pFile->szDb = size;
38061 ** Sync an kvvfs-file.
38063 static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
38064 int i, n;
38067 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
38068 if( pFile->nJrnl<=0 ){
38071 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
38075 n = pFile->nJrnl;
38082 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
38083 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
38087 static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
38092 ** Return the current file-size of an kvvfs-file.
38094 static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38096 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
38097 *pSize = pFile->nJrnl;
38100 static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38102 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
38103 if( pFile->szDb>=0 ){
38104 *pSize = pFile->szDb;
38112 ** Lock an kvvfs-file.
38114 static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
38116 assert( !pFile->isJournal );
38117 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
38120 pFile->szDb = kvvfsReadFileSize(pFile);
38126 ** Unlock an kvvfs-file.
38128 static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
38130 assert( !pFile->isJournal );
38131 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
38133 pFile->szDb = -1;
38139 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
38141 static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
38148 ** File control method. For custom operations on an kvvfs-file.
38150 static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
38154 static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
38158 int rc = SQLITE_OK;
38159 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
38160 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
38169 ** Return the sector-size in bytes for an kvvfs-file.
38171 static int kvvfsSectorSize(sqlite3_file *pFile){
38176 ** Return the device characteristic flags supported by an kvvfs-file.
38178 static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
38187 static int kvvfsOpen(
38191 int flags,
38192 int *pOutFlags
38200 pFile->isJournal = 0;
38201 pFile->base.pMethods = &kvvfs_db_io_methods;
38203 if( strcmp(zName, "local-journal")==0
38204 || strcmp(zName, "session-journal")==0
38206 pFile->isJournal = 1;
38207 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
38212 pFile->zClass = "session";
38214 pFile->zClass = "local";
38216 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
38217 if( pFile->aData==0 ){
38220 pFile->aJrnl = 0;
38221 pFile->nJrnl = 0;
38222 pFile->szPage = -1;
38223 pFile->szDb = -1;
38229 ** ensure the file-system modifications are synced to disk before
38232 static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
38233 if( strcmp(zPath, "local-journal")==0 ){
38236 if( strcmp(zPath, "session-journal")==0 ){
38246 static int kvvfsAccess(
38249 int flags,
38250 int *pResOut
38253 if( strcmp(zPath, "local-journal")==0 ){
38256 if( strcmp(zPath, "session-journal")==0 ){
38277 static int kvvfsFullPathname(
38280 int nOut,
38289 if( nOut<nPath+1 ) nPath = nOut - 1;
38306 static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
38315 static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
38322 static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
38324 int rc;
38330 static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
38341 ** This routine is called initialize the KV-vfs as the default VFS.
38343 SQLITE_API int sqlite3_os_init(void){
38346 SQLITE_API int sqlite3_os_end(void){
38352 SQLITE_PRIVATE int sqlite3KvvfsInit(void){
38371 ** This file contains the VFS implementation for unix-like operating systems
38377 ** use flock(), dot-files, various proprietary locking schemas, or simply
38387 ** * General-purpose declarations and utility functions.
38391 ** + for no-op locks
38392 ** + for dot-file locks
38400 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
38413 ** 3. Dot-file locking,
38473 ** -DHAVE_GETHOSTUUID=0
38474 ** -DHAVE_GETHOSTUUID=1
38477 ** -DSQLITE_ENABLE_LOCKING_STYLE.
38515 ** If we are to be thread-safe, include the pthreads header.
38536 ** Maximum supported path-length.
38555 # define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
38556 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
38595 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
38605 int fd; /* File descriptor to close */
38606 int flags; /* Flags this file descriptor was opened with */
38619 int h; /* The file descriptor */
38621 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
38622 int lastErrno; /* The unix errno from last I/O error */
38624 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
38627 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
38629 int nFetchOut; /* Number of outstanding xFetch refs */
38635 int sectorSize; /* Device sector size */
38636 int deviceCharacteristics; /* Precomputed device characteristics */
38638 int openFlags; /* The flags specified at open() */
38651 ** transaction counter in bytes 24-27 of database files are updated
38716 ** The threadid macro resolves to the thread-id or to 0. Used for
38737 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
38738 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
38746 ** Linux-specific IOCTL magic numbers used for controlling F2FS
38760 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
38764 ** which always has the same well-defined interface.
38766 static int posixOpen(const char *zFile, int flags, int mode){
38771 static int openDirectory(const char*, int*);
38772 static int unixGetpagesize(void);
38775 ** Many system calls are accessed through pointer-to-functions so that
38786 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
38789 #define osClose ((int(*)(int))aSyscall[1].pCurrent)
38792 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
38798 #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
38804 ** DJGPP. But it is DOS - what did you expect?
38811 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
38815 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
38818 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
38821 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
38828 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
38835 #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
38838 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
38845 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
38853 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
38861 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
38868 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
38871 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
38874 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
38877 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
38880 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
38887 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
38902 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
38910 #define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
38917 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
38924 #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
38938 #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
38942 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
38943 #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
38946 #define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
38957 ** log if they come from non-root processes. So avoid calling fchown() if
38960 static int robustFchown(int fd, uid_t uid, gid_t gid){
38974 static int unixSetSystemCall(
38979 unsigned int i;
38980 int rc = SQLITE_NOTFOUND;
39021 unsigned int i;
39037 int i = -1;
39041 for(i=0; i<ArraySize(aSyscall)-1; i++){
39069 ** The m parameter will be non-zero only when creating -wal, -journal,
39070 ** and -shm files. We want those files to have *exactly* the same
39072 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
39077 static int robust_open(const char *z, int f, mode_t m){
39078 int fd;
39097 fd = -1;
39152 static int unixMutexHeld(void) {
39162 ** integer lock-type.
39164 static const char *azFileLock(int eFileLock){
39181 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
39182 ** command-line option on the compiler. This code is normally
39185 static int lockTrace(int fd, int op, struct flock *p){
39187 int s;
39188 int savedErrno;
39198 if( p->l_type==F_RDLCK ){
39200 }else if( p->l_type==F_WRLCK ){
39202 }else if( p->l_type==F_UNLCK ){
39207 assert( p->l_whence==SEEK_SET );
39211 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
39212 (int)p->l_pid, s);
39213 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
39226 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
39227 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
39243 static int robust_ftruncate(int h, sqlite3_int64 sz){
39244 int rc;
39246 /* On Android, ftruncate() always uses 32-bit offsets, even if
39268 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
39307 ** a performance-critical path, so it is sufficient to put these
39312 int nRef; /* Number of references to this one */
39313 int nName; /* Length of the zCanonicalName[] string */
39332 ** Changes are made in-place. Return the new name length.
39334 ** The original filename is in z[0..n-1]. Return the number of
39337 static int vxworksSimplifyName(char *z, int n){
39338 int i, j;
39339 while( n>1 && z[n-1]=='/' ){ n--; }
39348 while( j>0 && z[j-1]!='/' ){ j--; }
39349 if( j>0 ){ j--; }
39374 int n; /* Length of zAbsoluteName string */
39377 n = (int)strlen(zAbsoluteName);
39380 pNew->zCanonicalName = (char*)&pNew[1];
39381 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
39382 n = vxworksSimplifyName(pNew->zCanonicalName, n);
39389 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
39390 if( pCandidate->nName==n
39391 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
39394 pCandidate->nRef++;
39401 pNew->nRef = 1;
39402 pNew->nName = n;
39403 pNew->pNext = vxworksFileList;
39415 assert( pId->nRef>0 );
39416 pId->nRef--;
39417 if( pId->nRef==0 ){
39419 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
39421 *pp = pId->pNext;
39434 ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
39441 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
39442 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
39479 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
39507 ** on linux - with NPTL a lock created by thread A can override locks
39508 ** in thread B. But there is no way to know at compile-time which
39510 ** compile-time whether or not thread A can override locks on thread B.
39511 ** One has to do a run-time check to discover the behavior of the
39531 ** sizes ino_t at only 32-bits instead of 64-bits. (See
39532 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
39533 ** To work around this, always allocate 64-bits for the inode number.
39534 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
39569 int nShared; /* Number of SHARED locks held */
39570 int nLock; /* Number of outstanding file locks */
39574 int nRef; /* Number of pointers to this structure */
39600 int unixFileMutexHeld(unixFile *pFile){
39601 assert( pFile->pInode );
39602 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
39604 int unixFileMutexNotheld(unixFile *pFile){
39605 assert( pFile->pInode );
39606 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
39612 ** This function - unixLogErrorAtLine(), is only ever called via the macro
39617 ** errno and, if possible, the human-readable equivalent from strerror() or
39623 ** failed (e.g. "unlink", "open") and the associated file-system path,
39627 static int unixLogErrorAtLine(
39628 int errcode, /* SQLite error code */
39631 int iLine /* Source line number where error occurred */
39634 int iErrno = errno; /* Saved syscall error number */
39637 ** the strerror() function to obtain the human-readable error message
39658 ** int-type return, depending on its version.
39664 strerror_r(iErrno, aErr, sizeof(aErr)-1);
39670 /* Non-threadsafe build, use strerror(). */
39676 "os_unix.c:%d: (%d) %s(%s) - %s",
39696 static void robust_close(unixFile *pFile, int h, int lineno){
39699 pFile ? pFile->zPath : 0, lineno);
39704 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
39707 static void storeLastErrno(unixFile *pFile, int error){
39708 pFile->lastErrno = error;
39712 ** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
39715 unixInodeInfo *pInode = pFile->pInode;
39719 for(p=pInode->pUnused; p; p=pNext){
39720 pNext = p->pNext;
39721 robust_close(pFile, p->fd, __LINE__);
39724 pInode->pUnused = 0;
39734 unixInodeInfo *pInode = pFile->pInode;
39738 pInode->nRef--;
39739 if( pInode->nRef==0 ){
39740 assert( pInode->pShmNode==0 );
39741 sqlite3_mutex_enter(pInode->pLockMutex);
39743 sqlite3_mutex_leave(pInode->pLockMutex);
39744 if( pInode->pPrev ){
39745 assert( pInode->pPrev->pNext==pInode );
39746 pInode->pPrev->pNext = pInode->pNext;
39749 inodeList = pInode->pNext;
39751 if( pInode->pNext ){
39752 assert( pInode->pNext->pPrev==pInode );
39753 pInode->pNext->pPrev = pInode->pPrev;
39755 sqlite3_mutex_free(pInode->pLockMutex);
39770 static int findInodeInfo(
39774 int rc; /* System call return code */
39775 int fd; /* The file descriptor for pFile */
39777 struct stat statbuf; /* Low-level file information */
39782 /* Get low-level information about the file that we can used to
39785 fd = pFile->h;
39790 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
39797 ** incorrectly for zero-size files. See ticket #3260. To work
39806 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
39823 fileId.pId = pFile->pId;
39829 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
39830 pInode = pInode->pNext;
39838 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
39840 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
39841 if( pInode->pLockMutex==0 ){
39846 pInode->nRef = 1;
39848 pInode->pNext = inodeList;
39849 pInode->pPrev = 0;
39850 if( inodeList ) inodeList->pPrev = pInode;
39853 pInode->nRef++;
39862 static int fileHasMoved(unixFile *pFile){
39864 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
39867 return pFile->pInode!=0 &&
39868 (osStat(pFile->zPath, &buf)!=0
39869 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
39885 int rc;
39888 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
39890 rc = osFstat(pFile->h, &buf);
39892 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
39896 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
39900 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
39904 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
39913 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39916 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
39917 int rc = SQLITE_OK;
39918 int reserved = 0;
39924 assert( pFile->eFileLock<=SHARED_LOCK );
39925 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
39928 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39935 if( !reserved && !pFile->pInode->bProcessLock ){
39941 if( osFcntl(pFile->h, F_GETLK, &lock) ){
39950 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
39951 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
39958 static int unixSleep(sqlite3_vfs*,int);
39961 ** Set a posix-advisory-lock.
39965 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
39970 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
39976 static int osSetPosixAdvisoryLock(
39977 int h, /* The file descriptor on which to take the lock */
39981 int tm = pFile->iBusyTimeout;
39982 int rc = osFcntl(h,F_SETLK,pLock);
39991 tm--;
39999 ** Attempt to set a system-lock on the file pFile. The lock is
40002 ** If the pFile was opened read/write from unix-excl, then the only lock
40005 ** operations become no-ops. Locking operations still happen internally,
40010 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
40011 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
40012 ** and is read-only.
40014 ** Zero is returned if the call completes successfully, or -1 if a call
40017 static int unixFileLock(unixFile *pFile, struct flock *pLock){
40018 int rc;
40019 unixInodeInfo *pInode = pFile->pInode;
40021 assert( sqlite3_mutex_held(pInode->pLockMutex) );
40022 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
40023 if( pInode->bProcessLock==0 ){
40025 assert( pInode->nLock==0 );
40030 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
40032 pInode->bProcessLock = 1;
40033 pInode->nLock++;
40038 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
40044 ** Lock the file with the lock specified by parameter eFileLock - one
40058 ** UNLOCKED -> SHARED
40059 ** SHARED -> RESERVED
40060 ** SHARED -> EXCLUSIVE
40061 ** RESERVED -> (PENDING) -> EXCLUSIVE
40062 ** PENDING -> EXCLUSIVE
40067 static int unixLock(sqlite3_file *id, int eFileLock){
40070 ** lock primitives (called read-locks and write-locks below, to avoid
40079 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
40080 ** byte'. If this is successful, 'shared byte range' is read-locked
40083 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
40085 ** Windows95 is now pretty much extinct, but this work-around for the
40086 ** lack of shared-locks on Windows95 lives on, for backwards
40090 ** A RESERVED lock is implemented by grabbing a write-lock on the
40095 ** a write-lock on the entire 'shared byte range'. Since all other locks
40096 ** require a read-lock on one of the bytes within this range, this ensures
40101 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
40105 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
40108 int rc = SQLITE_OK;
40112 int tErrno = 0;
40115 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
40116 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40117 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
40124 if( pFile->eFileLock>=eFileLock ){
40125 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
40135 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
40137 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
40139 /* This mutex is needed because pFile->pInode is shared across threads
40141 pInode = pFile->pInode;
40142 sqlite3_mutex_enter(pInode->pLockMutex);
40147 if( (pFile->eFileLock!=pInode->eFileLock &&
40148 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
40159 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
40161 assert( pFile->eFileLock==0 );
40162 assert( pInode->nShared>0 );
40163 pFile->eFileLock = SHARED_LOCK;
40164 pInode->nShared++;
40165 pInode->nLock++;
40177 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK)
40189 pFile->eFileLock = PENDING_LOCK;
40190 pInode->eFileLock = PENDING_LOCK;
40199 assert( pInode->nShared==0 );
40200 assert( pInode->eFileLock==0 );
40203 /* Now get the read-lock */
40227 pFile->eFileLock = SHARED_LOCK;
40228 pInode->nLock++;
40229 pInode->nShared = 1;
40231 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40240 assert( 0!=pFile->eFileLock );
40263 /* Set up the transaction-counter change checking flags when
40269 && pFile->eFileLock<=SHARED_LOCK
40272 pFile->transCntrChng = 0;
40273 pFile->dbUpdate = 0;
40274 pFile->inNormalWrite = 1;
40279 pFile->eFileLock = eFileLock;
40280 pInode->eFileLock = eFileLock;
40284 sqlite3_mutex_leave(pInode->pLockMutex);
40285 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
40295 unixInodeInfo *pInode = pFile->pInode;
40296 UnixUnusedFd *p = pFile->pPreallocatedUnused;
40298 p->pNext = pInode->pUnused;
40299 pInode->pUnused = p;
40300 pFile->h = -1;
40301 pFile->pPreallocatedUnused = 0;
40309 ** the requested locking level, this routine is a no-op.
40317 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
40321 int rc = SQLITE_OK;
40324 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
40325 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40329 if( pFile->eFileLock<=eFileLock ){
40332 pInode = pFile->pInode;
40333 sqlite3_mutex_enter(pInode->pLockMutex);
40334 assert( pInode->nShared!=0 );
40335 if( pFile->eFileLock>SHARED_LOCK ){
40336 assert( pInode->eFileLock==pFile->eFileLock );
40347 pFile->inNormalWrite = 0;
40351 ** before establishing the readlock - to avoid a race condition we downgrade
40366 int tErrno; /* Error code from system call errors */
40367 off_t divSize = SHARED_SIZE - 1;
40373 if( unixFileLock(pFile, &lock)==(-1) ){
40383 if( unixFileLock(pFile, &lock)==(-1) ){
40394 lock.l_len = SHARED_SIZE-divSize;
40395 if( unixFileLock(pFile, &lock)==(-1) ){
40426 pInode->eFileLock = SHARED_LOCK;
40438 pInode->nShared--;
40439 if( pInode->nShared==0 ){
40444 pInode->eFileLock = NO_LOCK;
40448 pInode->eFileLock = NO_LOCK;
40449 pFile->eFileLock = NO_LOCK;
40457 pInode->nLock--;
40458 assert( pInode->nLock>=0 );
40459 if( pInode->nLock==0 ) closePendingFds(pFile);
40463 sqlite3_mutex_leave(pInode->pLockMutex);
40465 pFile->eFileLock = eFileLock;
40475 ** the requested locking level, this routine is a no-op.
40477 static int unixUnlock(sqlite3_file *id, int eFileLock){
40479 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
40485 static int unixMapfile(unixFile *pFd, i64 nByte);
40499 static int closeUnixFile(sqlite3_file *id){
40504 if( pFile->h>=0 ){
40505 robust_close(pFile, pFile->h, __LINE__);
40506 pFile->h = -1;
40509 if( pFile->pId ){
40510 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40511 osUnlink(pFile->pId->zCanonicalName);
40513 vxworksReleaseFileId(pFile->pId);
40514 pFile->pId = 0;
40518 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40519 osUnlink(pFile->zPath);
40520 sqlite3_free(*(char**)&pFile->zPath);
40521 pFile->zPath = 0;
40524 OSTRACE(("CLOSE %-3d\n", pFile->h));
40525 OpenCounter(-1);
40526 sqlite3_free(pFile->pPreallocatedUnused);
40534 static int unixClose(sqlite3_file *id){
40535 int rc = SQLITE_OK;
40537 unixInodeInfo *pInode = pFile->pInode;
40548 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
40549 sqlite3_mutex_enter(pInode->pLockMutex);
40550 if( pInode->nLock ){
40553 ** descriptor to pInode->pUnused list. It will be automatically closed
40558 sqlite3_mutex_leave(pInode->pLockMutex);
40560 assert( pFile->pShm==0 );
40570 ****************************** No-op Locking **********************************
40576 ** This locking mode is appropriate for use on read-only databases
40577 ** (ex: databases that are burned into CD-ROM, for example.) It can
40586 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
40591 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
40595 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
40603 static int nolockClose(sqlite3_file *id) {
40607 /******************* End of the no-op lock implementation *********************
40611 ************************* Begin dot-file Locking ******************************
40641 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40648 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
40649 int rc = SQLITE_OK;
40650 int reserved = 0;
40656 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
40657 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
40663 ** Lock the file with the lock specified by parameter eFileLock - one
40677 ** UNLOCKED -> SHARED
40678 ** SHARED -> RESERVED
40679 ** SHARED -> (PENDING) -> EXCLUSIVE
40680 ** RESERVED -> (PENDING) -> EXCLUSIVE
40681 ** PENDING -> EXCLUSIVE
40689 static int dotlockLock(sqlite3_file *id, int eFileLock) {
40691 char *zLockFile = (char *)pFile->lockingContext;
40692 int rc = SQLITE_OK;
40698 if( pFile->eFileLock > NO_LOCK ){
40699 pFile->eFileLock = eFileLock;
40713 int tErrno = errno;
40726 pFile->eFileLock = eFileLock;
40735 ** the requested locking level, this routine is a no-op.
40739 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
40741 char *zLockFile = (char *)pFile->lockingContext;
40742 int rc;
40745 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
40746 pFile->eFileLock, osGetpid(0)));
40749 /* no-op if possible */
40750 if( pFile->eFileLock==eFileLock ){
40758 pFile->eFileLock = SHARED_LOCK;
40766 int tErrno = errno;
40775 pFile->eFileLock = NO_LOCK;
40782 static int dotlockClose(sqlite3_file *id) {
40786 sqlite3_free(pFile->lockingContext);
40789 /****************** End of the dot-file lock implementation *******************
40797 ** flock() locking is like dot-file locking in that the various
40798 ** fine-grain locking levels supported by SQLite are collapsed into
40812 static int robust_flock(int fd, int op){
40813 int rc;
40825 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40828 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
40829 int rc = SQLITE_OK;
40830 int reserved = 0;
40838 if( pFile->eFileLock>SHARED_LOCK ){
40845 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
40848 lrc = robust_flock(pFile->h, LOCK_UN);
40850 int tErrno = errno;
40857 int tErrno = errno;
40867 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
40880 ** Lock the file with the lock specified by parameter eFileLock - one
40894 ** UNLOCKED -> SHARED
40895 ** SHARED -> RESERVED
40896 ** SHARED -> (PENDING) -> EXCLUSIVE
40897 ** RESERVED -> (PENDING) -> EXCLUSIVE
40898 ** PENDING -> EXCLUSIVE
40908 static int flockLock(sqlite3_file *id, int eFileLock) {
40909 int rc = SQLITE_OK;
40916 if (pFile->eFileLock > NO_LOCK) {
40917 pFile->eFileLock = eFileLock;
40923 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
40924 int tErrno = errno;
40932 pFile->eFileLock = eFileLock;
40934 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
40950 ** the requested locking level, this routine is a no-op.
40952 static int flockUnlock(sqlite3_file *id, int eFileLock) {
40956 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
40957 pFile->eFileLock, osGetpid(0)));
40960 /* no-op if possible */
40961 if( pFile->eFileLock==eFileLock ){
40967 pFile->eFileLock = eFileLock;
40972 if( robust_flock(pFile->h, LOCK_UN) ){
40978 pFile->eFileLock = NO_LOCK;
40986 static int flockClose(sqlite3_file *id) {
41002 ** Semaphore locking is like dot-lock and flock in that it really only
41012 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41015 static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
41016 int rc = SQLITE_OK;
41017 int reserved = 0;
41025 if( pFile->eFileLock>SHARED_LOCK ){
41031 sem_t *pSem = pFile->pInode->pSem;
41033 if( sem_trywait(pSem)==-1 ){
41034 int tErrno = errno;
41040 reserved = (pFile->eFileLock < SHARED_LOCK);
41047 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
41054 ** Lock the file with the lock specified by parameter eFileLock - one
41068 ** UNLOCKED -> SHARED
41069 ** SHARED -> RESERVED
41070 ** SHARED -> (PENDING) -> EXCLUSIVE
41071 ** RESERVED -> (PENDING) -> EXCLUSIVE
41072 ** PENDING -> EXCLUSIVE
41082 static int semXLock(sqlite3_file *id, int eFileLock) {
41084 sem_t *pSem = pFile->pInode->pSem;
41085 int rc = SQLITE_OK;
41089 if (pFile->eFileLock > NO_LOCK) {
41090 pFile->eFileLock = eFileLock;
41096 if( sem_trywait(pSem)==-1 ){
41102 pFile->eFileLock = eFileLock;
41113 ** the requested locking level, this routine is a no-op.
41115 static int semXUnlock(sqlite3_file *id, int eFileLock) {
41117 sem_t *pSem = pFile->pInode->pSem;
41121 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
41122 pFile->eFileLock, osGetpid(0)));
41125 /* no-op if possible */
41126 if( pFile->eFileLock==eFileLock ){
41132 pFile->eFileLock = eFileLock;
41137 if ( sem_post(pSem)==-1 ) {
41138 int rc, tErrno = errno;
41145 pFile->eFileLock = NO_LOCK;
41152 static int semXClose(sqlite3_file *id) {
41178 ** on Apple Macintosh computers - both OS9 and OSX.
41180 ** Third-party implementations of AFP are available. But this code here
41190 int reserved;
41201 int fd; /* file desc to assoc this lock with */
41207 ** This is a utility for setting or clearing a bit-range lock on an
41212 static int afpSetLock(
41217 int setLockFlag /* True to set lock. False to clear lock */
41220 int err;
41226 pb.fd = pFile->h;
41229 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
41232 if ( err==-1 ) {
41233 int rc;
41234 int tErrno = errno;
41255 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41258 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
41259 int rc = SQLITE_OK;
41260 int reserved = 0;
41267 context = (afpLockingContext *) pFile->lockingContext;
41268 if( context->reserved ){
41272 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
41274 if( pFile->pInode->eFileLock>SHARED_LOCK ){
41282 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
41286 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
41296 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
41297 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
41304 ** Lock the file with the lock specified by parameter eFileLock - one
41318 ** UNLOCKED -> SHARED
41319 ** SHARED -> RESERVED
41320 ** SHARED -> (PENDING) -> EXCLUSIVE
41321 ** RESERVED -> (PENDING) -> EXCLUSIVE
41322 ** PENDING -> EXCLUSIVE
41327 static int afpLock(sqlite3_file *id, int eFileLock){
41328 int rc = SQLITE_OK;
41330 unixInodeInfo *pInode = pFile->pInode;
41331 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41334 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
41335 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
41336 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
41342 if( pFile->eFileLock>=eFileLock ){
41343 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
41353 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
41355 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
41357 /* This mutex is needed because pFile->pInode is shared across threads
41359 pInode = pFile->pInode;
41360 sqlite3_mutex_enter(pInode->pLockMutex);
41365 if( (pFile->eFileLock!=pInode->eFileLock &&
41366 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
41377 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
41379 assert( pFile->eFileLock==0 );
41380 assert( pInode->nShared>0 );
41381 pFile->eFileLock = SHARED_LOCK;
41382 pInode->nShared++;
41383 pInode->nLock++;
41392 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
41394 int failed;
41395 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
41406 int lrc1, lrc2, lrc1Errno = 0;
41409 assert( pInode->nShared==0 );
41410 assert( pInode->eFileLock==0 );
41413 /* Now get the read-lock SHARED_LOCK */
41416 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
41417 lrc1 = afpSetLock(context->dbPath, pFile,
41418 SHARED_FIRST+pInode->sharedByte, 1, 1);
41420 lrc1Errno = pFile->lastErrno;
41423 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
41435 pFile->eFileLock = SHARED_LOCK;
41436 pInode->nLock++;
41437 pInode->nShared = 1;
41439 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
41448 int failed = 0;
41449 assert( 0!=pFile->eFileLock );
41450 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
41452 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
41454 context->reserved = 1;
41463 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
41464 pInode->sharedByte, 1, 0)) ){
41465 int failed2 = SQLITE_OK;
41467 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
41469 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
41470 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
41488 pFile->eFileLock = eFileLock;
41489 pInode->eFileLock = eFileLock;
41491 pFile->eFileLock = PENDING_LOCK;
41492 pInode->eFileLock = PENDING_LOCK;
41496 sqlite3_mutex_leave(pInode->pLockMutex);
41497 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
41507 ** the requested locking level, this routine is a no-op.
41509 static int afpUnlock(sqlite3_file *id, int eFileLock) {
41510 int rc = SQLITE_OK;
41513 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41514 int skipShared = 0;
41517 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
41518 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
41522 if( pFile->eFileLock<=eFileLock ){
41525 pInode = pFile->pInode;
41526 sqlite3_mutex_enter(pInode->pLockMutex);
41527 assert( pInode->nShared!=0 );
41528 if( pFile->eFileLock>SHARED_LOCK ){
41529 assert( pInode->eFileLock==pFile->eFileLock );
41540 assert( pFile->inNormalWrite==0
41541 || pFile->dbUpdate==0
41542 || pFile->transCntrChng==1 );
41543 pFile->inNormalWrite = 0;
41546 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
41547 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
41548 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
41549 /* only re-establish the shared lock if necessary */
41550 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41551 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
41556 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
41557 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
41559 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
41560 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
41562 context->reserved = 0;
41565 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
41566 pInode->eFileLock = SHARED_LOCK;
41575 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41576 pInode->nShared--;
41577 if( pInode->nShared==0 ){
41579 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
41582 pInode->eFileLock = NO_LOCK;
41583 pFile->eFileLock = NO_LOCK;
41587 pInode->nLock--;
41588 assert( pInode->nLock>=0 );
41589 if( pInode->nLock==0 ) closePendingFds(pFile);
41593 sqlite3_mutex_leave(pInode->pLockMutex);
41595 pFile->eFileLock = eFileLock;
41603 static int afpClose(sqlite3_file *id) {
41604 int rc = SQLITE_OK;
41610 if( pFile->pInode ){
41611 unixInodeInfo *pInode = pFile->pInode;
41612 sqlite3_mutex_enter(pInode->pLockMutex);
41613 if( pInode->nLock ){
41616 ** descriptor to pInode->aPending. It will be automatically closed when
41621 sqlite3_mutex_leave(pInode->pLockMutex);
41624 sqlite3_free(pFile->lockingContext);
41634 ** is available. If you don't compile for a mac, then the "unix-afp"
41649 ** the requested locking level, this routine is a no-op.
41651 static int nfsUnlock(sqlite3_file *id, int eFileLock){
41665 **************** Non-locking sqlite3_file methods *****************************
41681 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
41682 int got;
41683 int prior = 0;
41689 assert( id->h>2 );
41692 got = osPread(id->h, pBuf, cnt, offset);
41693 SimulateIOError( got = -1 );
41695 got = osPread64(id->h, pBuf, cnt, offset);
41696 SimulateIOError( got = -1 );
41698 newOffset = lseek(id->h, offset, SEEK_SET);
41699 SimulateIOError( newOffset = -1 );
41702 return -1;
41704 got = osRead(id->h, pBuf, cnt);
41713 cnt -= got;
41720 OSTRACE(("READ %-3d %5d %7lld %llu\n",
41721 id->h, got+prior, offset-prior, TIMER_ELAPSED));
41730 static int unixRead(
41733 int amt,
41737 int got;
41742 /* If this is a database file (not a journal, super-journal or temp
41745 assert( pFile->pPreallocatedUnused==0
41754 if( offset<pFile->mmapSize ){
41755 if( offset+amt <= pFile->mmapSize ){
41756 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
41759 int nCopy = pFile->mmapSize - offset;
41760 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
41762 amt -= nCopy;
41772 /* pFile->lastErrno has been set by seekAndRead().
41779 switch( pFile->lastErrno ){
41793 /* Unread parts of the buffer must be zero-filled */
41794 memset(&((char*)pBuf)[got], 0, amt-got);
41800 ** Attempt to seek the file-descriptor passed as the first argument to
41802 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
41806 static int seekAndWriteFd(
41807 int fd, /* File descriptor to write to */
41810 int nBuf, /* Size of buffer pBuf in bytes */
41811 int *piErrno /* OUT: Error number if error occurs */
41813 int rc = 0; /* Value returned by system call */
41822 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
41824 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
41828 SimulateIOError( iSeek = -1 );
41830 rc = -1;
41838 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
41846 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
41852 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
41853 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
41861 static int unixWrite(
41864 int amt,
41868 int wrote = 0;
41872 /* If this is a database file (not a journal, super-journal or temp
41875 assert( pFile->pPreallocatedUnused==0
41883 ** doing a hot-journal rollback or a write to some file other than a
41888 if( pFile->inNormalWrite ){
41889 pFile->dbUpdate = 1; /* The database has been modified */
41891 int rc;
41896 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
41897 pFile->transCntrChng = 1; /* The transaction counter has changed */
41906 if( offset<pFile->mmapSize ){
41907 if( offset+amt <= pFile->mmapSize ){
41908 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41911 int nCopy = pFile->mmapSize - offset;
41912 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41914 amt -= nCopy;
41921 amt -= wrote;
41925 SimulateIOError(( wrote=(-1), amt=1 ));
41929 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
41946 SQLITE_API int sqlite3_sync_count = 0;
41947 SQLITE_API int sqlite3_fullsync_count = 0;
41954 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
41996 static int full_fsync(int fd, int fullSync, int dataOnly){
41997 int rc;
42024 ** no-op. But go ahead and call fstat() to validate the file
42057 if( rc==-1 && errno==ENOTSUP ){
42063 if( OS_VXWORKS && rc!= -1 ){
42076 ** The directory file descriptor is used for only one thing - to
42085 ** replace this routine with a harmless no-op. To make this routine
42086 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
42092 static int openDirectory(const char *zFilename, int *pFd){
42093 int ii;
42094 int fd = -1;
42098 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
42107 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
42122 ** has been created by fsync-ing the directory that contains the file.
42127 ** will not roll back - possibly leading to database corruption.
42129 static int unixSync(sqlite3_file *id, int flags){
42130 int rc;
42133 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
42134 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
42147 OSTRACE(("SYNC %-3d\n", pFile->h));
42148 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
42152 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
42156 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
42159 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
42160 int dirfd;
42161 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
42163 rc = osOpenDirectory(pFile->zPath, &dirfd);
42171 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
42179 static int unixTruncate(sqlite3_file *id, i64 nByte){
42181 int rc;
42185 /* If the user has configured a chunk-size for this file, truncate the
42190 if( pFile->szChunk>0 ){
42191 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
42194 rc = robust_ftruncate(pFile->h, nByte);
42197 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
42201 ** doing a hot-journal rollback or a write to some file other than a
42204 ** when restoring a database using the backup API from a zero-length
42207 if( pFile->inNormalWrite && nByte==0 ){
42208 pFile->transCntrChng = 1;
42217 if( nByte<pFile->mmapSize ){
42218 pFile->mmapSize = nByte;
42229 static int unixFileSize(sqlite3_file *id, i64 *pSize){
42230 int rc;
42233 rc = osFstat(((unixFile*)id)->h, &buf);
42241 /* When opening a zero-size database, the findInodeInfo() procedure
42243 ** in the OS-X msdos filesystem. In order to avoid problems with upper
42255 ** Handler for proxy-locking file-control verbs. Defined below in the
42258 static int proxyFileControl(sqlite3_file*,int,void*);
42263 ** file-control operation. Enlarge the database to nBytes in size
42264 ** (rounded up to the next chunk-size). If the database is already
42265 ** nBytes or larger, this routine is a no-op.
42267 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
42268 if( pFile->szChunk>0 ){
42272 if( osFstat(pFile->h, &buf) ){
42276 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
42283 int err;
42285 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
42292 ** at offset (nSize-1), to set the size of the file correctly.
42296 int nBlk = buf.st_blksize; /* File-system block size */
42297 int nWrite = 0; /* Number of bytes written by seekAndWrite */
42300 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
42303 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
42304 if( iWrite>=nSize ) iWrite = nSize - 1;
42313 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
42314 int rc;
42315 if( pFile->szChunk<=0 ){
42316 if( robust_ftruncate(pFile->h, nByte) ){
42318 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
42332 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42334 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42336 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
42338 *pArg = (pFile->ctrlFlags & mask)!=0;
42340 pFile->ctrlFlags &= ~mask;
42342 pFile->ctrlFlags |= mask;
42347 static int unixGetTempname(int nBuf, char *zBuf);
42349 static int unixFcntlExternalReader(unixFile*, int*);
42355 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42360 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
42364 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
42368 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
42374 *(int*)pArg = pFile->eFileLock;
42378 *(int*)pArg = pFile->lastErrno;
42382 pFile->szChunk = *(int *)pArg;
42386 int rc;
42393 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
42397 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
42401 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
42405 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
42407 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
42413 *(int*)pArg = fileHasMoved(pFile);
42418 int iOld = pFile->iBusyTimeout;
42420 pFile->iBusyTimeout = *(int*)pArg;
42422 pFile->iBusyTimeout = !!(*(int*)pArg);
42426 *(int*)pArg = iOld;
42433 int rc = SQLITE_OK;
42440 ** 64-bit type. */
42445 *(i64*)pArg = pFile->mmapSizeMax;
42446 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
42447 pFile->mmapSizeMax = newLimit;
42448 if( pFile->mmapSize>0 ){
42450 rc = unixMapfile(pFile, -1);
42463 ((unixFile*)id)->dbUpdate = 0;
42476 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
42478 *(int*)pArg = 0;
42487 ** If pFd->sectorSize is non-zero when this function is called, it is a
42488 ** no-op. Otherwise, the values of pFd->sectorSize and
42489 ** pFd->deviceCharacteristics are set according to the file-system
42497 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
42498 if( pFd->sectorSize==0 ){
42500 int res;
42504 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
42506 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
42511 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
42512 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
42515 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42522 if( pFile->sectorSize == 0 ){
42525 /* Set defaults for non-supported filesystems */
42526 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42527 pFile->deviceCharacteristics = 0;
42528 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
42533 pFile->sectorSize = fsInfo.f_bsize;
42534 pFile->deviceCharacteristics =
42542 pFile->sectorSize = fsInfo.f_bsize;
42543 pFile->deviceCharacteristics =
42545 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
42552 pFile->sectorSize = fsInfo.f_bsize;
42553 pFile->deviceCharacteristics =
42561 pFile->sectorSize = fsInfo.f_bsize;
42562 pFile->deviceCharacteristics =
42564 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42569 pFile->sectorSize = fsInfo.f_bsize;
42570 pFile->deviceCharacteristics =
42572 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42577 pFile->deviceCharacteristics =
42586 if( pFile->sectorSize % 512 != 0 ){
42587 pFile->deviceCharacteristics = 0;
42588 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42599 ** if two files are created in the same file-system directory (i.e.
42603 static int unixSectorSize(sqlite3_file *id){
42606 return pFd->sectorSize;
42615 ** words, after a power-loss event, parts of the file that were never
42616 ** written might end up being altered.) However, non-PSOW behavior is very,
42619 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
42622 static int unixDeviceCharacteristics(sqlite3_file *id){
42625 return pFd->deviceCharacteristics;
42636 static int unixGetpagesize(void){
42642 return (int)sysconf(_SC_PAGESIZE);
42653 ** When multiple threads all reference the same wal-index, each thread
42655 ** of this unixShmNode object. In other words, each wal-index is opened
42670 ** The following fields are read-only after the object is created:
42682 ** locks are held by the process on this slot. If it is set to -1, then
42693 ** of mutexes - one for each locking slot. To read or write locking
42702 int hShm; /* Open file descriptor */
42703 int szRegion; /* Size of shared-memory regions */
42705 u8 isReadonly; /* True if read-only */
42707 char **apRegion; /* Array of mapped shared-memory regions */
42708 int nRef; /* Number of unixShm objects pointing to this */
42713 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
42724 ** are read-only thereafter:
42729 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
42735 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
42749 ** wal-mode transactions in other processes on database file pFile. If
42755 static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
42756 int rc = SQLITE_OK;
42758 if( pFile->pShm){
42759 unixShmNode *pShmNode = pFile->pShm->pShmNode;
42766 f.l_len = SQLITE_SHM_NLOCK - 3;
42768 sqlite3_mutex_enter(pShmNode->pShmMutex);
42769 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
42774 sqlite3_mutex_leave(pShmNode->pShmMutex);
42782 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
42784 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
42787 static int unixShmSystemLock(
42789 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
42790 int ofst, /* First byte of the locking range */
42791 int n /* Number of bytes to lock */
42793 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
42795 int rc = SQLITE_OK; /* Result code form fcntl() */
42797 pShmNode = pFile->pInode->pShmNode;
42801 assert( pShmNode->nRef>=0 );
42806 assert( pShmNode->nRef>0 || unixMutexHeld() );
42807 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42810 int ii;
42811 for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
42812 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
42815 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42816 assert( pShmNode->nRef>0 );
42827 if( pShmNode->hShm>=0 ){
42828 int res;
42834 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
42835 if( res==-1 ){
42837 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
42846 OSTRACE(("SHM-LOCK "));
42849 OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1));
42851 OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1));
42854 OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1));
42858 OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1));
42860 OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1));
42863 OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1));
42874 ** current system page-size.
42877 ** to use 64KB pages - in this case each mapping must cover at least two
42880 static int unixShmRegionPerMap(void){
42881 int shmsz = 32*1024; /* SHM region size */
42882 int pgsz = osGetpagesize(); /* System page size */
42883 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
42891 ** This is not a VFS shared-memory method; it is a utility function called
42892 ** by VFS shared-memory methods.
42895 unixShmNode *p = pFd->pInode->pShmNode;
42897 if( p && ALWAYS(p->nRef==0) ){
42898 int nShmPerMap = unixShmRegionPerMap();
42899 int i;
42900 assert( p->pInode==pFd->pInode );
42901 sqlite3_mutex_free(p->pShmMutex);
42904 sqlite3_mutex_free(p->aMutex[i]);
42907 for(i=0; i<p->nRegion; i+=nShmPerMap){
42908 if( p->hShm>=0 ){
42909 osMunmap(p->apRegion[i], p->szRegion);
42911 sqlite3_free(p->apRegion[i]);
42914 sqlite3_free(p->apRegion);
42915 if( p->hShm>=0 ){
42916 robust_close(pFd, p->hShm, __LINE__);
42917 p->hShm = -1;
42919 p->pInode->pShmNode = 0;
42931 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
42933 static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
42935 int rc = SQLITE_OK;
42940 ** and proceed with opening the *-shm file.
42944 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
42951 ** EXCLUSIVE failed just before truncating the *-shm file, then this
42952 ** process might open and use the *-shm file without truncating it.
42953 ** And if the *-shm file has been corrupted by a power failure or
42959 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
42962 if( pShmNode->isReadonly ){
42963 pShmNode->isUnlocked = 1;
42969 ** *-shm file. And after it has done so, it will not release its
42973 int iSaveTimeout = pDbFd->iBusyTimeout;
42974 pDbFd->iBusyTimeout = 0;
42978 pDbFd->iBusyTimeout = iSaveTimeout;
42980 /* The first connection to attach must truncate the -shm file. We
42982 ** -shm header size) rather than 0 as a system debugging aid, to
42983 ** help detect if a -shm file truncation is legitimate or is the work
42985 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
42986 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
43001 ** Open a shared-memory area associated with open database file pDbFd.
43004 ** The file used to implement shared-memory is in the same directory
43006 ** file with the "-shm" suffix added. For example, if the database file
43008 ** for shared memory will be called "/home/user1/config.db-shm".
43014 ** database to end up using different files for shared memory -
43015 ** meaning that their memory would not really be shared - resulting
43017 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
43018 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
43022 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
43025 ** When opening a new shared-memory file, if no other instances of that
43029 ** If the original database file (pDbFd) is using the "unix-excl" VFS
43035 static int unixOpenSharedMemory(unixFile *pDbFd){
43038 int rc = SQLITE_OK; /* Result code */
43041 int nShmFilename; /* Size of the SHM filename in bytes */
43047 assert( pDbFd->pShm==0 );
43054 pInode = pDbFd->pInode;
43055 pShmNode = pInode->pShmNode;
43059 const char *zBasePath = pDbFd->zPath;
43063 ** a new *-shm file is created, an attempt will be made to create it
43066 if( osFstat(pDbFd->h, &sStat) ){
43074 nShmFilename = 6 + (int)strlen(zBasePath);
43082 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
43085 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
43088 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
43089 sqlite3FileSuffix3(pDbFd->zPath, zShm);
43091 pShmNode->hShm = -1;
43092 pDbFd->pInode->pShmNode = pShmNode;
43093 pShmNode->pInode = pDbFd->pInode;
43095 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
43096 if( pShmNode->pShmMutex==0 ){
43102 int ii;
43104 pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
43105 if( pShmNode->aMutex[ii]==0 ){
43114 if( pInode->bProcessLock==0 ){
43115 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
43116 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
43119 if( pShmNode->hShm<0 ){
43120 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
43122 if( pShmNode->hShm<0 ){
43126 pShmNode->isReadonly = 1;
43133 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
43141 p->pShmNode = pShmNode;
43143 p->id = pShmNode->nextShmId++;
43145 pShmNode->nRef++;
43146 pDbFd->pShm = p;
43153 ** at pShmNode->pFirst. This must be done while holding the
43154 ** pShmNode->pShmMutex.
43156 sqlite3_mutex_enter(pShmNode->pShmMutex);
43157 p->pNext = pShmNode->pFirst;
43158 pShmNode->pFirst = p;
43159 sqlite3_mutex_leave(pShmNode->pShmMutex);
43172 ** shared-memory associated with the database file fd. Shared-memory regions
43173 ** are numbered starting from zero. Each shared-memory region is szRegion
43178 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
43181 ** bExtend is non-zero and the requested shared-memory region has not yet
43184 ** If the shared-memory region has already been allocated or is allocated by
43189 static int unixShmMap(
43191 int iRegion, /* Region to retrieve */
43192 int szRegion, /* Size of regions */
43193 int bExtend, /* True to extend file if necessary */
43199 int rc = SQLITE_OK;
43200 int nShmPerMap = unixShmRegionPerMap();
43201 int nReqRegion;
43203 /* If the shared-memory file has not yet been opened, open it now. */
43204 if( pDbFd->pShm==0 ){
43209 p = pDbFd->pShm;
43210 pShmNode = p->pShmNode;
43211 sqlite3_mutex_enter(pShmNode->pShmMutex);
43212 if( pShmNode->isUnlocked ){
43215 pShmNode->isUnlocked = 0;
43217 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
43218 assert( pShmNode->pInode==pDbFd->pInode );
43219 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
43220 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
43225 if( pShmNode->nRegion<nReqRegion ){
43227 int nByte = nReqRegion*szRegion; /* Minimum required file size */
43230 pShmNode->szRegion = szRegion;
43232 if( pShmNode->hShm>=0 ){
43234 ** Check to see if it has been allocated (i.e. if the wal-index file is
43237 if( osFstat(pShmNode->hShm, &sStat) ){
43258 static const int pgsz = 4096;
43259 int iPg;
43264 int x = 0;
43265 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
43266 const char *zFile = pShmNode->zFilename;
43277 pShmNode->apRegion, nReqRegion*sizeof(char *)
43283 pShmNode->apRegion = apNew;
43284 while( pShmNode->nRegion<nReqRegion ){
43285 int nMap = szRegion*nShmPerMap;
43286 int i;
43288 if( pShmNode->hShm>=0 ){
43290 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
43291 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
43294 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
43307 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
43309 pShmNode->nRegion += nShmPerMap;
43314 if( pShmNode->nRegion>iRegion ){
43315 *pp = pShmNode->apRegion[iRegion];
43319 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
43320 sqlite3_mutex_leave(pShmNode->pShmMutex);
43325 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
43332 static int assertLockingArrayOk(unixShmNode *pShmNode){
43337 int aLock[SQLITE_SHM_NLOCK];
43340 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
43341 int i;
43343 if( pX->exclMask & (1<<i) ){
43345 aLock[i] = -1;
43346 }else if( pX->sharedMask & (1<<i) ){
43353 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
43354 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
43360 ** Change the lock state for a shared-memory segment.
43367 static int unixShmLock(
43369 int ofst, /* First lock to acquire or release */
43370 int n, /* Number of locks to acquire or release */
43371 int flags /* What to do with the lock */
43376 int rc = SQLITE_OK; /* Result code */
43377 u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */
43378 int *aLock;
43380 p = pDbFd->pShm;
43382 pShmNode = p->pShmNode;
43384 aLock = pShmNode->aLock;
43386 assert( pShmNode==pDbFd->pInode->pShmNode );
43387 assert( pShmNode->pInode==pDbFd->pInode );
43395 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
43396 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
43413 u16 lockMask = (p->exclMask|p->sharedMask);
43414 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43433 || 0==(p->exclMask & mask)
43435 if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
43436 || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
43449 ** case thread 2 does a non-blocking request for the WRITER lock. But -
43453 ** a lock on a read-locking slot at this point, this breaks the
43454 ** anti-deadlock rules (see above). */
43456 int iMutex;
43459 rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]);
43462 sqlite3_mutex_enter(pShmNode->aMutex[iMutex]);
43466 sqlite3_mutex_enter(pShmNode->pShmMutex);
43471 /* Case (a) - unlock. */
43472 int bUnlock = 1;
43473 assert( (p->exclMask & p->sharedMask)==0 );
43474 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
43475 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
43480 ** from the file-descriptor below. */
43486 aLock[ofst]--;
43487 p->sharedMask &= ~mask;
43494 memset(&aLock[ofst], 0, sizeof(int)*n);
43495 p->sharedMask &= ~mask;
43496 p->exclMask &= ~mask;
43500 /* Case (b) - a shared lock. */
43511 p->sharedMask |= mask;
43515 /* Case (c) - an exclusive lock. */
43516 int ii;
43519 assert( (p->sharedMask & mask)==0 );
43520 assert( (p->exclMask & mask)==0 );
43532 ** also update the in-memory values. */
43536 p->exclMask |= mask;
43538 aLock[ii] = -1;
43549 for(iMutex--; iMutex>=ofst; iMutex--){
43550 sqlite3_mutex_leave(pShmNode->aMutex[iMutex]);
43553 sqlite3_mutex_leave(pShmNode->pShmMutex);
43557 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
43558 p->id, osGetpid(0), p->sharedMask, p->exclMask));
43572 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
43573 assert( fd->pMethods->xLock==nolockLock
43581 ** Close a connection to shared-memory. Delete the underlying
43585 ** routine is a harmless no-op.
43587 static int unixShmUnmap(
43589 int deleteFlag /* Delete shared-memory if true */
43592 unixShmNode *pShmNode; /* The underlying shared-memory file */
43597 p = pDbFd->pShm;
43599 pShmNode = p->pShmNode;
43601 assert( pShmNode==pDbFd->pInode->pShmNode );
43602 assert( pShmNode->pInode==pDbFd->pInode );
43606 sqlite3_mutex_enter(pShmNode->pShmMutex);
43607 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
43608 *pp = p->pNext;
43612 pDbFd->pShm = 0;
43613 sqlite3_mutex_leave(pShmNode->pShmMutex);
43615 /* If pShmNode->nRef has reached 0, then close the underlying
43616 ** shared-memory file, too */
43619 assert( pShmNode->nRef>0 );
43620 pShmNode->nRef--;
43621 if( pShmNode->nRef==0 ){
43622 if( deleteFlag && pShmNode->hShm>=0 ){
43623 osUnlink(pShmNode->zFilename);
43645 assert( pFd->nFetchOut==0 );
43646 if( pFd->pMapRegion ){
43647 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
43648 pFd->pMapRegion = 0;
43649 pFd->mmapSize = 0;
43650 pFd->mmapSizeActual = 0;
43674 int h = pFd->h; /* File descriptor open on db file */
43675 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
43676 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
43678 int flags = PROT_READ; /* Flags to pass to mmap() */
43680 assert( pFd->nFetchOut==0 );
43681 assert( nNew>pFd->mmapSize );
43682 assert( nNew<=pFd->mmapSizeMax );
43684 assert( pFd->mmapSizeActual>=pFd->mmapSize );
43688 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
43693 i64 nReuse = pFd->mmapSize;
43695 const int szSyspage = osGetpagesize();
43696 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
43702 osMunmap(pReq, nOrig-nReuse);
43709 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
43712 osMunmap(pNew, nNew - nReuse);
43734 unixLogError(SQLITE_OK, zErr, pFd->zPath);
43739 pFd->mmapSizeMax = 0;
43741 pFd->pMapRegion = (void *)pNew;
43742 pFd->mmapSize = pFd->mmapSizeActual = nNew;
43746 ** Memory map or remap the file opened by file-descriptor pFd (if the file
43749 ** outstanding xFetch() references to it, this function is a no-op.
43751 ** If parameter nByte is non-negative, then it is the requested size of
43761 static int unixMapfile(unixFile *pFd, i64 nMap){
43762 assert( nMap>=0 || pFd->nFetchOut==0 );
43763 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43764 if( pFd->nFetchOut>0 ) return SQLITE_OK;
43767 struct stat statbuf; /* Low-level file information */
43768 if( osFstat(pFd->h, &statbuf) ){
43773 if( nMap>pFd->mmapSizeMax ){
43774 nMap = pFd->mmapSizeMax;
43777 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43778 if( nMap!=pFd->mmapSize ){
43798 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
43805 if( pFd->mmapSizeMax>0 ){
43810 const int nEofBuffer = 256;
43811 if( pFd->pMapRegion==0 ){
43812 int rc = unixMapfile(pFd, -1);
43815 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
43816 *pp = &((u8 *)pFd->pMapRegion)[iOff];
43817 pFd->nFetchOut++;
43825 ** If the third argument is non-NULL, then this function releases a
43834 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
43842 assert( (p==0)==(pFd->nFetchOut==0) );
43845 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
43848 pFd->nFetchOut--;
43853 assert( pFd->nFetchOut>=0 );
43871 ** of "finder" functions. A finder-function is used to locate the appropriate
43874 ** the correct finder-function for that VFS.
43877 ** object. The only interesting finder-function is autolockIoFinder, which
43881 ** For finder-function F, two objects are created:
43883 ** (1) The real finder-function named "FImpt()".
43890 ** directly at the finder-function since C90 rules prevent a void*
44007 ** The proxy locking method is a "super-method" in the sense that it
44009 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
44016 static int proxyClose(sqlite3_file*);
44017 static int proxyLock(sqlite3_file*, int);
44018 static int proxyUnlock(sqlite3_file*, int);
44019 static int proxyCheckReservedLock(sqlite3_file*, int*);
44069 int i;
44078 if( statfs(filePath, &fsInfo) != -1 ){
44090 ** Test byte-range lock using fcntl(). If the call succeeds,
44091 ** assume that the file-system supports POSIX style locks.
44097 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
44137 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
44164 static int fillInUnixFile(
44166 int h, /* Open file descriptor of file being opened */
44169 int ctrlFlags /* Zero or more UNIXFILE_* values */
44173 int rc = SQLITE_OK;
44175 assert( pNew->pInode==NULL );
44180 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
44181 pNew->h = h;
44182 pNew->pVfs = pVfs;
44183 pNew->zPath = zFilename;
44184 pNew->ctrlFlags = (u8)ctrlFlags;
44186 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
44190 pNew->ctrlFlags |= UNIXFILE_PSOW;
44192 if( strcmp(pVfs->zName,"unix-excl")==0 ){
44193 pNew->ctrlFlags |= UNIXFILE_EXCL;
44197 pNew->pId = vxworksFindFileId(zFilename);
44198 if( pNew->pId==0 ){
44207 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
44212 pNew->lockingContext = (void*)zFilename;
44222 rc = findInodeInfo(pNew, &pNew->pInode);
44235 ** handle h - as it is guaranteed that no posix locks will be released
44243 h = -1;
44254 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
44261 pCtx->dbPath = zFilename;
44262 pCtx->reserved = 0;
44265 rc = findInodeInfo(pNew, &pNew->pInode);
44267 sqlite3_free(pNew->lockingContext);
44269 h = -1;
44281 int nFilename;
44283 nFilename = (int)strlen(zFilename) + 6;
44290 pNew->lockingContext = zLockFile;
44299 rc = findInodeInfo(pNew, &pNew->pInode);
44300 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
44301 char *zSemName = pNew->pInode->aSemName;
44302 int n;
44304 pNew->pId->zCanonicalName);
44307 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
44308 if( pNew->pInode->pSem == SEM_FAILED ){
44310 pNew->pInode->aSemName[0] = '\0';
44321 h = -1;
44323 pNew->ctrlFlags |= UNIXFILE_DELETE;
44329 pId->pMethods = pLockingStyle;
44361 unsigned int i = 0;
44382 ** pVfs->mxPathname bytes.
44384 static int unixGetTempname(int nBuf, char *zBuf){
44386 int iLimit = 0;
44387 int rc = SQLITE_OK;
44389 /* It's odd to simulate an io-error here, but really this is just
44390 ** using the io-error infrastructure to test that SQLite handles this
44405 zBuf[nBuf-2] = 0;
44408 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
44420 ** Routine to transform a unixFile into a proxy-locking unixFile.
44421 ** Implementation in the proxy-lock division, but used by unixOpen()
44424 static int proxyTransformUnixFile(unixFile*, const char*);
44429 ** file (not a journal or super-journal file) identified by pathname
44435 ** other file descriptor open on the same file is holding a file-lock.
44441 ** such file descriptor is located, -1 is returned.
44443 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
44459 ** ignored and -1 is returned. The caller will try to open a new file
44468 while( pInode && (pInode->fileId.dev!=sStat.st_dev
44469 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
44470 pInode = pInode->pNext;
44474 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
44475 sqlite3_mutex_enter(pInode->pLockMutex);
44477 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
44480 *pp = pUnused->pNext;
44482 sqlite3_mutex_leave(pInode->pLockMutex);
44493 static int getFileMode(
44500 int rc = SQLITE_OK;
44522 ** this function queries the file-system for the permissions on the
44532 static int findCreateFileMode(
44534 int flags, /* Flags passed as 4th argument to xOpen() */
44539 int rc = SQLITE_OK; /* Return Code */
44545 int nDb; /* Number of valid bytes in zDb */
44551 ** "<path to db>-journal"
44552 ** "<path to db>-wal"
44553 ** "<path to db>-journalNN"
44554 ** "<path to db>-walNN"
44560 ** a '-' character. However in 8+3 filename mode, or if a corrupt
44561 ** rollback journal specifies a super-journal with a goofy name, then
44562 ** the '-' might be missing or the '-' might be the first character in
44565 nDb = sqlite3Strlen30(zPath) - 1;
44567 if( zPath[nDb]=='-' ){
44573 nDb--;
44602 ** ReadWrite() -> (READWRITE | CREATE)
44603 ** ReadOnly() -> (READONLY)
44604 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
44606 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
44612 static int unixOpen(
44616 int flags, /* Input flags to control the opening */
44617 int *pOutFlags /* Output flags returned to SQLite core */
44620 int fd = -1; /* File descriptor returned by open() */
44621 int openFlags = 0; /* Flags to pass to open() */
44622 int eType = flags&0x0FFF00; /* Type of file to open */
44623 int noLock; /* True to omit locking primitives */
44624 int rc = SQLITE_OK; /* Function Return Code */
44625 int ctrlFlags = 0; /* UNIXFILE_* flags */
44627 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
44628 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
44629 int isCreate = (flags & SQLITE_OPEN_CREATE);
44630 int isReadonly = (flags & SQLITE_OPEN_READONLY);
44631 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
44633 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
44639 /* If creating a super- or main-file journal, this function will open
44640 ** a file-descriptor on the directory too. The first time unixSync()
44643 int isNewJrnl = (isCreate && (
44667 /* The main DB, main journal, WAL file and super-journal are never
44674 /* Assert that the upper layer has set one of the "file-type" flags. */
44701 fd = pUnused->fd;
44708 p->pPreallocatedUnused = pUnused;
44710 /* Database filenames are double-zero terminated if they are not
44718 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
44724 /* Generated temporary filenames are always double-zero terminated
44745 assert( !p->pPreallocatedUnused );
44750 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
44758 /* Failed to open the file for read/write access. Try read-only. */
44767 fd = pReadonly->fd;
44775 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
44782 ** the case. The chown() system call will be a no-op if the current
44801 if( p->pPreallocatedUnused ){
44802 p->pPreallocatedUnused->fd = fd;
44803 p->pPreallocatedUnused->flags =
44822 p->openFlags = openFlags;
44827 if( fstatfs(fd, &fsInfo) == -1 ){
44833 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44836 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44852 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
44854 int useProxy = 0;
44857 ** never use proxy, NULL means use proxy for non-local files only. */
44870 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
44888 sqlite3_free(p->pPreallocatedUnused);
44898 static int unixDelete(
44901 int dirSync /* If true, fsync() directory after deleting file */
44903 int rc = SQLITE_OK;
44906 if( osUnlink(zPath)==(-1) ){
44920 int fd;
44946 static int unixAccess(
44949 int flags, /* What do we want to learn about the zPath file? */
44950 int *pResOut /* Write result boolean here */
44975 int rc; /* Non-zero following any error */
44976 int nSymlink; /* Number of symlinks resolved */
44978 int nOut; /* Bytes of space available to zOut[] */
44979 int nUsed; /* Bytes of zOut[] currently being used */
44990 const char *zName, /* Name to append to pPath. Not zero-terminated */
44991 int nName /* Number of significant bytes in zName */
44998 if( pPath->nUsed>1 ){
44999 assert( pPath->zOut[0]=='/' );
45000 while( pPath->zOut[--pPath->nUsed]!='/' ){}
45005 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
45006 pPath->rc = SQLITE_ERROR;
45009 pPath->zOut[pPath->nUsed++] = '/';
45010 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
45011 pPath->nUsed += nName;
45013 if( pPath->rc==SQLITE_OK ){
45016 pPath->zOut[pPath->nUsed] = 0;
45017 zIn = pPath->zOut;
45020 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
45025 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
45026 pPath->rc = SQLITE_CANTOPEN_BKPT;
45029 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
45030 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
45031 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
45036 pPath->nUsed = 0;
45038 pPath->nUsed -= nName + 1;
45051 const char *zPath /* Path to append to pPath. Is zero-terminated */
45053 int i = 0;
45054 int j = 0;
45058 appendOnePathElement(pPath, &zPath[j], i-j);
45066 ** is stored as a nul-terminated string in the buffer pointed to by
45070 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
45073 static int unixFullPathname(
45076 int nOut, /* Size of output buffer in bytes */
45088 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
45118 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
45130 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
45133 ** use dlsym() with -pedantic-errors?
45141 ** This work-around is unlikely to work correctly on any system where
45165 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
45167 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
45170 ** errors. The reports issued by valgrind are incorrect - we would
45172 ** uninitialized space in zBuf - but valgrind errors tend to worry
45185 int fd, got;
45212 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
45220 ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
45232 int seconds = (microseconds+999999)/1000000;
45240 ** The following variable, if set to a non-zero value, is interpreted as
45245 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
45258 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
45260 int rc = SQLITE_OK;
45290 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
45292 int rc;
45304 ** low-level error message when operating-system problems come up
45308 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
45323 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
45325 ** meta-layer over top of the primitive locking implemented above. For
45328 ** been defined - so that the primitive locking methods are available
45333 ** The default locking schemes in SQLite use byte-range locks on the
45338 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
45346 ** SHARED_RANGE 0x40000002 -> 0x40000200
45350 ** the read cache when byte-range locks are present. Enabling the read
45353 ** close-to-open semantics for ensuring cache coherency
45357 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
45366 ** -----------------
45398 ** -----------------------
45408 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
45409 ** by taking an sqlite-style shared lock on the conch file, reading the
45413 ** is patterned after the database file name as ".<databasename>-conch".
45421 ** The proxy file - a single-byte file used for all advisory file locks
45437 ** ---------------------
45441 ** Database files accessed on non-local file systems are
45486 int conchHeld; /* 1 if the conch is held, -1 if lockless */
45487 int nFails; /* Number of conch taking failures */
45497 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
45498 int len;
45499 int dbLen;
45500 int i;
45519 if( lPath[len-1]!='/' ){
45524 dbLen = (int)strlen(dbPath);
45525 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
45538 static int proxyCreateLockPath(const char *lockPath){
45539 int i, len;
45541 int start = 0;
45545 len = (int)strlen(lockPath);
45548 if( lockPath[i] == '/' && (i - start > 0) ){
45550 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
45551 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
45554 int err=errno;
45578 static int proxyCreateUnixFile(
45581 int islockfile /* if non zero missing dirs will be created */
45583 int fd = -1;
45585 int rc = SQLITE_OK;
45586 int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW;
45588 int terrno = 0;
45592 ** 2. if that fails, and this is a lock file (not-conch), try creating
45594 ** 3. if that fails, try to open the file read-only
45599 fd = pUnused->fd;
45640 pNew->openFlags = openFlags;
45644 pUnused->fd = fd;
45645 pUnused->flags = openFlags;
45646 pNew->pPreallocatedUnused = pUnused;
45662 SQLITE_API int sqlite3_hostid_num = 0;
45669 extern int gethostuuid(uuid_t id, const struct timespec *wait);
45675 static int proxyGetHostID(unsigned char *pHostID, int *pError){
45682 int err = errno;
45704 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
45715 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
45716 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45717 unixFile *conchFile = pCtx->conchFile;
45720 char *cPath = pCtx->conchFilePath;
45724 int fd = -1;
45725 int rc = -1;
45728 /* create a new path by replace the trailing '-conch' with '-break' */
45731 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
45732 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
45736 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
45738 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
45757 robust_close(pFile, conchFile->h, __LINE__);
45758 conchFile->h = fd;
45759 conchFile->openFlags = O_RDWR | O_CREAT;
45775 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
45776 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45777 unixFile *conchFile = pCtx->conchFile;
45778 int rc = SQLITE_OK;
45779 int nTries = 0;
45784 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45794 if( osFstat(conchFile->h, &buf) ){
45813 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
45835 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
45838 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45848 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
45853 static int proxyTakeConch(unixFile *pFile){
45854 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45856 if( pCtx->conchHeld!=0 ){
45859 unixFile *conchFile = pCtx->conchFile;
45861 int pError = 0;
45865 int rc = SQLITE_OK;
45866 int createConch = 0;
45867 int hostIdMatch = 0;
45868 int readLen = 0;
45869 int tryOldLockPath = 0;
45870 int forceNewLockPath = 0;
45872 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
45873 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45889 storeLastErrno(pFile, conchFile->lastErrno);
45901 ** retry with a new auto-generated path
45909 if( !pCtx->lockProxyPath ){
45910 /* for auto-named local lock file, just check the host ID and we'll
45914 size_t pathLen = (readLen - PROXY_PATHINDEX);
45917 pathLen=MAXPATHLEN-1;
45927 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
45928 readLen-PROXY_PATHINDEX)
45936 if( (conchFile->openFlags&O_RDWR) == 0 ){
45942 if( !pCtx->lockProxyPath ){
45943 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
45952 futimes(conchFile->h, NULL);
45954 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
45966 int writeSize = 0;
45970 if( pCtx->lockProxyPath!=NULL ){
45971 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
45977 robust_ftruncate(conchFile->h, writeSize);
45979 full_fsync(conchFile->h,0,0);
45985 int err = osFstat(pFile->h, &buf);
45991 osFchmod(conchFile->h, cmode);
45994 rc = osFchmod(conchFile->h, cmode);
45995 }while( rc==(-1) && errno==EINTR );
45997 int code = errno;
46004 int code = errno;
46011 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
46014 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
46015 if( rc==SQLITE_OK && pFile->openFlags ){
46016 int fd;
46017 if( pFile->h>=0 ){
46018 robust_close(pFile, pFile->h, __LINE__);
46020 pFile->h = -1;
46021 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
46024 pFile->h = fd;
46030 if( rc==SQLITE_OK && !pCtx->lockProxy ){
46031 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
46032 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
46035 ** so try again via auto-naming
46047 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
46048 if( !pCtx->lockProxyPath ){
46054 pCtx->conchHeld = 1;
46056 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
46058 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
46059 afpCtx->dbPath = pCtx->lockProxyPath;
46062 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
46064 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
46067 } while (1); /* in case we need to retry the :auto: lock file -
46075 static int proxyReleaseConch(unixFile *pFile){
46076 int rc = SQLITE_OK; /* Subroutine return code */
46080 pCtx = (proxyLockingContext *)pFile->lockingContext;
46081 conchFile = pCtx->conchFile;
46082 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
46083 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
46085 if( pCtx->conchHeld>0 ){
46086 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
46088 pCtx->conchHeld = 0;
46089 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
46105 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
46106 int i; /* Loop counter */
46107 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
46119 for( i=(len-1); i>=0; i-- ){
46131 /* append the "-conch" suffix to the file */
46132 memcpy(&conchPath[i+1], "-conch", 7);
46133 assert( (int)strlen(conchPath) == len+7 );
46139 /* Takes a fully configured proxy locking-style unix file and switches
46142 static int switchLockProxyPath(unixFile *pFile, const char *path) {
46143 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46144 char *oldPath = pCtx->lockProxyPath;
46145 int rc = SQLITE_OK;
46147 if( pFile->eFileLock!=NO_LOCK ){
46156 unixFile *lockProxy = pCtx->lockProxy;
46157 pCtx->lockProxy=NULL;
46158 pCtx->conchHeld = 0;
46160 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
46165 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
46176 ** int dbPath.
46178 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
46180 if( pFile->pMethod == &afpIoMethods ){
46183 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
46184 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
46188 if( pFile->pMethod == &dotlockIoMethods ){
46191 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
46192 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
46195 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
46196 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
46206 ** ->lockingContext
46207 ** ->pMethod
46209 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
46213 int rc = SQLITE_OK;
46215 if( pFile->eFileLock!=NO_LOCK ){
46225 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
46234 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
46236 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
46237 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
46239 ** (c) the file system is read-only, then enable no-locking access.
46245 int goLockless = 0;
46247 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
46248 int err = errno;
46249 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
46254 pCtx->conchHeld = -1; /* read only FS/ lockless */
46260 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
46264 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
46265 if( pCtx->dbPath==NULL ){
46273 pCtx->oldLockingContext = pFile->lockingContext;
46274 pFile->lockingContext = pCtx;
46275 pCtx->pOldMethod = pFile->pMethod;
46276 pFile->pMethod = &proxyIoMethods;
46278 if( pCtx->conchFile ){
46279 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
46280 sqlite3_free(pCtx->conchFile);
46282 sqlite3DbFree(0, pCtx->lockProxyPath);
46283 sqlite3_free(pCtx->conchFilePath);
46286 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
46296 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
46300 if( pFile->pMethod == &proxyIoMethods ){
46301 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46303 if( pCtx->lockProxyPath ){
46304 *(const char **)pArg = pCtx->lockProxyPath;
46315 int rc = SQLITE_OK;
46316 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
46319 /* turn off proxy locking - not supported. If support is added for
46325 /* turn off proxy locking - already off - NOOP */
46332 (proxyLockingContext*)pFile->lockingContext;
46334 || (pCtx->lockProxyPath &&
46335 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
46358 ** above this point are all utilities. The lock-related methods of the
46359 ** proxy-locking sqlite3_io_method object follow.
46366 ** to a non-zero value otherwise *pResOut is set to zero. The return value
46369 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
46371 int rc = proxyTakeConch(pFile);
46373 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46374 if( pCtx->conchHeld>0 ){
46375 unixFile *proxy = pCtx->lockProxy;
46376 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
46385 ** Lock the file with the lock specified by parameter eFileLock - one
46399 ** UNLOCKED -> SHARED
46400 ** SHARED -> RESERVED
46401 ** SHARED -> (PENDING) -> EXCLUSIVE
46402 ** RESERVED -> (PENDING) -> EXCLUSIVE
46403 ** PENDING -> EXCLUSIVE
46408 static int proxyLock(sqlite3_file *id, int eFileLock) {
46410 int rc = proxyTakeConch(pFile);
46412 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46413 if( pCtx->conchHeld>0 ){
46414 unixFile *proxy = pCtx->lockProxy;
46415 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
46416 pFile->eFileLock = proxy->eFileLock;
46430 ** the requested locking level, this routine is a no-op.
46432 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
46434 int rc = proxyTakeConch(pFile);
46436 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46437 if( pCtx->conchHeld>0 ){
46438 unixFile *proxy = pCtx->lockProxy;
46439 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
46440 pFile->eFileLock = proxy->eFileLock;
46451 static int proxyClose(sqlite3_file *id) {
46454 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46455 unixFile *lockProxy = pCtx->lockProxy;
46456 unixFile *conchFile = pCtx->conchFile;
46457 int rc = SQLITE_OK;
46460 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
46462 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
46465 pCtx->lockProxy = 0;
46468 if( pCtx->conchHeld ){
46472 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
46476 sqlite3DbFree(0, pCtx->lockProxyPath);
46477 sqlite3_free(pCtx->conchFilePath);
46478 sqlite3DbFree(0, pCtx->dbPath);
46480 pFile->lockingContext = pCtx->oldLockingContext;
46481 pFile->pMethod = pCtx->pOldMethod;
46483 return pFile->pMethod->xClose(id);
46503 ** This routine registers all VFS implementations for unix-like operating
46513 SQLITE_API int sqlite3_os_init(void){
46520 ** when compiling with -pedantic-errors on GCC.)
46523 ** finder-function. The finder-function returns a pointer to the
46526 ** macro for addition information on finder-functions.
46574 UNIXVFS("unix-none", nolockIoFinder ),
46575 UNIXVFS("unix-dotfile", dotlockIoFinder ),
46576 UNIXVFS("unix-excl", posixIoFinder ),
46578 UNIXVFS("unix-namedsem", semIoFinder ),
46581 UNIXVFS("unix-posix", posixIoFinder ),
46584 UNIXVFS("unix-flock", flockIoFinder ),
46587 UNIXVFS("unix-afp", afpIoFinder ),
46588 UNIXVFS("unix-nfs", nfsIoFinder ),
46589 UNIXVFS("unix-proxy", proxyIoFinder ),
46592 unsigned int i; /* Loop counter */
46594 /* Double-check that the aSyscall[] array has been constructed
46620 ** READ-0 UNIX_SHM_BASE+3 123
46621 ** READ-1 UNIX_SHM_BASE+4 124
46622 ** READ-2 UNIX_SHM_BASE+5 125
46623 ** READ-3 UNIX_SHM_BASE+6 126
46624 ** READ-4 UNIX_SHM_BASE+7 127
46627 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
46641 ** This routine is a no-op for unix.
46643 SQLITE_API int sqlite3_os_end(void){
46695 ** based on the sub-platform)?
46703 ** based on the sub-platform)?
46791 ** characters, so we allocate 4 bytes per character assuming worst-case of
46792 ** 4-bytes-per-character for UTF8.
46815 ** Returns non-zero if the character should be treated as a directory
46879 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
46892 typedef struct winShm winShm; /* A connection to shared-memory */
46893 typedef struct winShmNode winShmNode; /* A region of shared-memory */
46902 int nReaders; /* Number of reader locks obtained */
46926 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
46935 int nFetchOut; /* Number of outstanding xFetch references */
46951 BOOL bNoLock; /* Non-zero if locking is disabled. */
46965 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
46975 * If this is non-zero, an isolated heap will be created by the native Win32
46981 * WARNING: It is important to note that when this setting is non-zero and the
46993 * This is the maximum possible initial size of the Win32-specific heap, in
47001 * This is the extra space for the initial size of the Win32-specific heap,
47014 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
47021 * Win32-specific heap. It cannot be negative.
47027 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
47033 * initial size of the Win32-specific heap to exceed the maximum amount
47042 * The initial size of the Win32-specific heap. This value may be zero.
47051 * The maximum size of the Win32-specific heap. This value may be zero.
47067 ** The winMemData structure stores information required by the Win32-specific
47109 static void *winMemMalloc(int nBytes);
47111 static void *winMemRealloc(void *pPrior, int nBytes);
47112 static int winMemSize(void *p);
47113 static int winMemRoundup(int n);
47114 static int winMemInit(void *pAppData);
47151 ** Many system calls are accessed through pointer-to-functions so that
47572 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
47573 int))aSyscall[50].pCurrent)
47641 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
47768 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
47817 static int winSetSystemCall(
47822 unsigned int i;
47823 int rc = SQLITE_NOTFOUND;
47864 unsigned int i;
47880 int i = -1;
47884 for(i=0; i<ArraySize(aSyscall)-1; i++){
47899 ** "pnLargest" argument, if non-zero, will be used to return the size of the
47902 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
47903 int rc = SQLITE_OK;
47942 SQLITE_API int sqlite3_win32_reset_heap(){
47943 int rc;
47987 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
47989 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
47990 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
47991 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
48057 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
48082 SQLITE_API int sqlite3_win32_is_nt(void){
48085 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
48110 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48121 static void *winMemMalloc(int nBytes){
48164 static void *winMemRealloc(void *pPrior, int nBytes){
48192 static int winMemSize(void *p){
48205 if( n==(SIZE_T)-1 ){
48210 return (int)n;
48216 static int winMemRoundup(int n){
48223 static int winMemInit(void *pAppData){
48227 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
48228 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
48231 if( !pWinMemData->hHeap ){
48239 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
48241 if( !pWinMemData->hHeap ){
48248 pWinMemData->bOwned = TRUE;
48249 assert( pWinMemData->bOwned );
48252 pWinMemData->hHeap = osGetProcessHeap();
48253 if( !pWinMemData->hHeap ){
48258 pWinMemData->bOwned = FALSE;
48259 assert( !pWinMemData->bOwned );
48261 assert( pWinMemData->hHeap!=0 );
48262 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
48264 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
48276 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
48277 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
48279 if( pWinMemData->hHeap ){
48280 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
48282 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
48284 if( pWinMemData->bOwned ){
48285 if( !osHeapDestroy(pWinMemData->hHeap) ){
48287 osGetLastError(), (void*)pWinMemData->hHeap);
48289 pWinMemData->bOwned = FALSE;
48291 pWinMemData->hHeap = NULL;
48296 ** Populate the low-level memory allocation function pointers in
48323 ** Convert a UTF-8 string to Microsoft Unicode.
48328 int nChar;
48331 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
48339 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
48349 ** Convert a Microsoft Unicode string to UTF-8.
48354 int nByte;
48357 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
48365 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
48380 static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
48381 int nByte;
48383 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48385 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
48394 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48404 ** Convert a Microsoft Unicode string to a multi-byte character string,
48409 static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
48410 int nByte;
48412 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48414 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
48422 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
48432 ** Convert a multi-byte character string to UTF-8.
48436 static char *winMbcsToUtf8(const char *zText, int useAnsi){
48450 ** Convert a UTF-8 string to a multi-byte character string.
48454 static char *winUtf8ToMbcs(const char *zText, int useAnsi){
48518 SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
48550 SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
48565 ** it accepts a UTF-8 string.
48567 SQLITE_API int sqlite3_win32_set_directory8(
48572 int rc;
48609 ** it accepts a UTF-16 string.
48611 SQLITE_API int sqlite3_win32_set_directory16(
48615 int rc;
48633 SQLITE_API int sqlite3_win32_set_directory(
48642 ** is zero if the error message fits in the buffer, or non-zero
48645 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
48722 ** This function - winLogErrorAtLine() - is only ever called via the macro
48727 ** error code and, if possible, the human-readable equivalent from
48733 ** failed and the associated file-system path, if any.
48736 static int winLogErrorAtLine(
48737 int errcode, /* SQLite error code */
48741 int iLine /* Source line number where error occurred */
48744 int i; /* Loop counter */
48753 "os_win.c:%d: (%lu) %s(%s) - %s",
48762 ** will be retried following a locking error - probably caused by
48772 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
48773 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
48779 ** non-zero if the error code is transient in nature and the operation
48806 static int winRetryIoerr(int *pnRetry, DWORD *pError){
48835 static void winLogIoerr(int nRetry, int lineno){
48867 y.tm_year = pTm.wYear - 1900;
48868 y.tm_mon = pTm.wMonth - 1;
48882 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
48902 static int winceCreateLock(const char *zFilename, winFile *pFile){
48916 memset(&pFile->local, 0, sizeof(pFile->local));
48926 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
48927 if (!pFile->hMutex){
48928 pFile->lastErrno = osGetLastError();
48930 return winLogError(SQLITE_IOERR, pFile->lastErrno,
48935 winceMutexAcquire(pFile->hMutex);
48938 ** case-sensitive, take advantage of that by uppercasing the mutex name
48942 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
48947 ** must be zero-initialized */
48956 if( pFile->hShared ){
48957 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
48960 if( !pFile->shared ){
48961 pFile->lastErrno = osGetLastError();
48962 winLogError(SQLITE_IOERR, pFile->lastErrno,
48965 osCloseHandle(pFile->hShared);
48966 pFile->hShared = NULL;
48971 if( pFile->hShared==NULL ){
48973 pFile->lastErrno = lastErrno;
48974 winLogError(SQLITE_IOERR, pFile->lastErrno,
48978 winceMutexRelease(pFile->hMutex);
48979 osCloseHandle(pFile->hMutex);
48980 pFile->hMutex = NULL;
48986 memset(pFile->shared, 0, sizeof(winceLock));
48989 winceMutexRelease(pFile->hMutex);
48997 if (pFile->hMutex){
48999 winceMutexAcquire(pFile->hMutex);
49003 if (pFile->local.nReaders){
49004 pFile->shared->nReaders --;
49006 if (pFile->local.bReserved){
49007 pFile->shared->bReserved = FALSE;
49009 if (pFile->local.bPending){
49010 pFile->shared->bPending = FALSE;
49012 if (pFile->local.bExclusive){
49013 pFile->shared->bExclusive = FALSE;
49016 /* De-reference and close our copy of the shared memory handle */
49017 osUnmapViewOfFile(pFile->shared);
49018 osCloseHandle(pFile->hShared);
49021 winceMutexRelease(pFile->hMutex);
49022 osCloseHandle(pFile->hMutex);
49023 pFile->hMutex = NULL;
49043 if (!pFile->hMutex) return TRUE;
49044 winceMutexAcquire(pFile->hMutex);
49049 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
49050 pFile->shared->bExclusive = TRUE;
49051 pFile->local.bExclusive = TRUE;
49056 /* Want a read-only lock? */
49059 if (pFile->shared->bExclusive == 0){
49060 pFile->local.nReaders ++;
49061 if (pFile->local.nReaders == 1){
49062 pFile->shared->nReaders ++;
49072 if (pFile->shared->bPending == 0) {
49073 pFile->shared->bPending = TRUE;
49074 pFile->local.bPending = TRUE;
49082 if (pFile->shared->bReserved == 0) {
49083 pFile->shared->bReserved = TRUE;
49084 pFile->local.bReserved = TRUE;
49089 winceMutexRelease(pFile->hMutex);
49109 if (!pFile->hMutex) return TRUE;
49110 winceMutexAcquire(pFile->hMutex);
49115 if (pFile->local.bExclusive){
49117 pFile->local.bExclusive = FALSE;
49118 pFile->shared->bExclusive = FALSE;
49123 else if (pFile->local.nReaders){
49126 pFile->local.nReaders --;
49127 if (pFile->local.nReaders == 0)
49129 pFile->shared->nReaders --;
49138 if (pFile->local.bPending){
49139 pFile->local.bPending = FALSE;
49140 pFile->shared->bPending = FALSE;
49147 if (pFile->local.bReserved) {
49148 pFile->local.bReserved = FALSE;
49149 pFile->shared->bReserved = FALSE;
49154 winceMutexRelease(pFile->hMutex);
49234 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
49240 ** Otherwise, set pFile->lastErrno and return non-zero.
49242 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49249 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
49255 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49261 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49265 pFile->lastErrno = lastErrno;
49266 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49267 "winSeekFile", pFile->zPath);
49268 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49272 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49283 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49286 pFile->lastErrno = osGetLastError();
49287 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49288 "winSeekFile", pFile->zPath);
49289 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49293 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49300 static int winMapfile(winFile*, sqlite3_int64);
49301 static int winUnmapfile(winFile*);
49315 static int winClose(sqlite3_file *id){
49316 int rc, cnt = 0;
49321 assert( pFile->pShm==0 );
49323 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
49325 osGetCurrentProcessId(), pFile, pFile->h));
49332 rc = osCloseHandle(pFile->h);
49338 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
49339 if( pAppData==NULL || !pAppData->bNoLock ){
49343 if( pFile->zDeleteOnClose ){
49344 int cnt = 0;
49346 osDeleteFileW(pFile->zDeleteOnClose)==0
49347 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
49352 sqlite3_free(pFile->zDeleteOnClose);
49356 pFile->h = NULL;
49358 OpenCounter(-1);
49360 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
49363 "winClose", pFile->zPath);
49371 static int winRead(
49374 int amt, /* Number of bytes to read */
49382 int nRetry = 0; /* Number of retrys */
49390 pFile->h, pBuf, amt, offset, pFile->locktype));
49395 if( offset<pFile->mmapSize ){
49396 if( offset+amt <= pFile->mmapSize ){
49397 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
49398 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49399 osGetCurrentProcessId(), pFile, pFile->h));
49402 int nCopy = (int)(pFile->mmapSize - offset);
49403 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
49405 amt -= nCopy;
49414 osGetCurrentProcessId(), pFile, pFile->h));
49417 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
49422 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
49427 pFile->lastErrno = lastErrno;
49429 osGetCurrentProcessId(), pFile, pFile->h));
49430 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
49431 "winRead", pFile->zPath);
49435 /* Unread parts of the buffer must be zero-filled */
49436 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
49438 osGetCurrentProcessId(), pFile, pFile->h));
49443 osGetCurrentProcessId(), pFile, pFile->h));
49451 static int winWrite(
49454 int amt, /* Number of bytes to write */
49457 int rc = 0; /* True if error has occurred, else false */
49459 int nRetry = 0; /* Number of retries */
49468 pFile->h, pBuf, amt, offset, pFile->locktype));
49473 if( offset<pFile->mmapSize ){
49474 if( offset+amt <= pFile->mmapSize ){
49475 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
49476 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49477 osGetCurrentProcessId(), pFile, pFile->h));
49480 int nCopy = (int)(pFile->mmapSize - offset);
49481 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
49483 amt -= nCopy;
49499 int nRem = amt; /* Number of bytes yet to be written */
49511 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
49513 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
49529 nRem -= nWrite;
49532 pFile->lastErrno = lastErrno;
49538 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
49539 || ( pFile->lastErrno==ERROR_DISK_FULL )){
49541 osGetCurrentProcessId(), pFile, pFile->h));
49542 return winLogError(SQLITE_FULL, pFile->lastErrno,
49543 "winWrite1", pFile->zPath);
49546 osGetCurrentProcessId(), pFile, pFile->h));
49547 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
49548 "winWrite2", pFile->zPath);
49553 osGetCurrentProcessId(), pFile, pFile->h));
49560 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
49562 int rc = SQLITE_OK; /* Return code for this function */
49566 if( pFile->nFetchOut>0 ){
49567 /* File truncation is a no-op if there are outstanding memory mapped
49573 ** No real harm comes of this - the database file is not corrupted,
49577 ** The only feasible work-around is to defer the truncation until after
49578 ** all references to memory-mapped content are closed. That is doable,
49581 ** now to simply make transactions a no-op if there are pending reads. We
49591 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
49593 /* If the user has configured a chunk-size for this file, truncate the
49598 if( pFile->szChunk>0 ){
49599 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
49603 if( pFile->pMapRegion ){
49604 oldMmapSize = pFile->mmapSize;
49611 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
49613 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49614 "winTruncate1", pFile->zPath);
49615 }else if( 0==osSetEndOfFile(pFile->h) &&
49617 pFile->lastErrno = lastErrno;
49618 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49619 "winTruncate2", pFile->zPath);
49625 winMapfile(pFile, -1);
49633 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
49642 SQLITE_API int sqlite3_sync_count = 0;
49643 SQLITE_API int sqlite3_fullsync_count = 0;
49649 static int winSync(sqlite3_file *id, int flags){
49679 osGetCurrentProcessId(), pFile, pFile->h, flags,
49680 pFile->locktype));
49692 ** no-op
49695 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49696 osGetCurrentProcessId(), pFile, pFile->h));
49700 if( pFile->pMapRegion ){
49701 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
49702 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49704 pFile, pFile->pMapRegion));
49706 pFile->lastErrno = osGetLastError();
49707 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49709 pFile, pFile->pMapRegion));
49710 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49711 "winSync1", pFile->zPath);
49715 rc = osFlushFileBuffers(pFile->h);
49719 osGetCurrentProcessId(), pFile, pFile->h));
49722 pFile->lastErrno = osGetLastError();
49724 osGetCurrentProcessId(), pFile, pFile->h));
49725 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
49726 "winSync2", pFile->zPath);
49734 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
49736 int rc = SQLITE_OK;
49741 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
49746 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
49750 pFile->lastErrno = osGetLastError();
49751 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49752 "winFileSize", pFile->zPath);
49761 lowerBits = osGetFileSize(pFile->h, &upperBits);
49765 pFile->lastErrno = lastErrno;
49766 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49767 "winFileSize", pFile->zPath);
49772 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
49812 static int winGetReadLock(winFile *pFile){
49813 int res;
49814 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49821 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
49823 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
49829 int lk;
49831 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
49832 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49833 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49837 pFile->lastErrno = osGetLastError();
49840 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
49847 static int winUnlockReadLock(winFile *pFile){
49848 int res;
49850 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49852 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
49856 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49860 pFile->lastErrno = lastErrno;
49861 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
49862 "winUnlockReadLock", pFile->zPath);
49864 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
49869 ** Lock the file with the lock specified by parameter locktype - one
49883 ** UNLOCKED -> SHARED
49884 ** SHARED -> RESERVED
49885 ** SHARED -> (PENDING) -> EXCLUSIVE
49886 ** RESERVED -> (PENDING) -> EXCLUSIVE
49887 ** PENDING -> EXCLUSIVE
49894 static int winLock(sqlite3_file *id, int locktype){
49895 int rc = SQLITE_OK; /* Return code from subroutines */
49896 int res = 1; /* Result of a Windows lock call */
49897 int newLocktype; /* Set pFile->locktype to this value before exiting */
49898 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
49904 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49910 if( pFile->locktype>=locktype ){
49911 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
49915 /* Do not allow any kind of write-lock on a read-only database
49917 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
49923 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
49925 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
49931 newLocktype = pFile->locktype;
49932 if( pFile->locktype==NO_LOCK
49933 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
49935 int cnt = 3;
49936 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49939 ** around problems caused by indexing and/or anti-virus software on
49945 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
49946 pFile->h, cnt, res));
49948 pFile->lastErrno = lastErrno;
49950 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
49951 pFile->h, cnt, sqlite3ErrName(rc)));
49965 assert( pFile->locktype==NO_LOCK );
49977 assert( pFile->locktype==SHARED_LOCK );
49978 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
49996 assert( pFile->locktype>=SHARED_LOCK );
49998 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
50012 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50021 pFile->lastErrno = lastErrno;
50023 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
50024 pFile->h, locktype, newLocktype));
50026 pFile->locktype = (u8)newLocktype;
50028 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50035 ** non-zero, otherwise zero.
50037 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
50038 int res;
50042 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
50045 if( pFile->locktype>=RESERVED_LOCK ){
50047 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
50049 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
50051 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50054 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
50057 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
50058 pFile->h, pResOut, *pResOut));
50067 ** the requested locking level, this routine is a no-op.
50073 static int winUnlock(sqlite3_file *id, int locktype){
50074 int type;
50076 int rc = SQLITE_OK;
50080 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50081 type = pFile->locktype;
50083 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50088 "winUnlock", pFile->zPath);
50092 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50098 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50100 pFile->locktype = (u8)locktype;
50102 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50107 ****************************** No-op Locking **********************************
50113 ** This locking mode is appropriate for use on read-only databases
50114 ** (ex: databases that are burned into CD-ROM, for example.) It can
50123 static int winNolockLock(sqlite3_file *id, int locktype){
50129 static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
50135 static int winNolockUnlock(sqlite3_file *id, int locktype){
50141 /******************* End of the no-op lock implementation *********************
50146 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
50148 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
50150 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
50152 *pArg = (pFile->ctrlFlags & mask)!=0;
50154 pFile->ctrlFlags &= ~mask;
50156 pFile->ctrlFlags |= mask;
50161 static int winGetTempname(sqlite3_vfs *, char **);
50162 static int winIsDir(const void *);
50169 static int winFileControl(sqlite3_file *id, int op, void *pArg){
50171 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
50174 *(int*)pArg = pFile->locktype;
50175 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50179 *(int*)pArg = (int)pFile->lastErrno;
50180 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50184 pFile->szChunk = *(int *)pArg;
50185 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50189 if( pFile->szChunk>0 ){
50191 int rc = winFileSize(id, &oldSz);
50200 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50203 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50207 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
50208 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50212 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
50213 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50217 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
50218 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50222 int *a = (int*)pArg;
50233 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50238 *phFile = pFile->h;
50239 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50245 HANDLE hOldFile = pFile->h;
50246 pFile->h = *phFile;
50249 hOldFile, pFile->h));
50255 int rc = winGetTempname(pFile->pVfs, &zTFile);
50259 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50265 int rc = SQLITE_OK;
50272 ** least a 64-bit type. */
50277 *(i64*)pArg = pFile->mmapSizeMax;
50278 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
50279 pFile->mmapSizeMax = newLimit;
50280 if( pFile->mmapSize>0 ){
50282 rc = winMapfile(pFile, -1);
50285 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50290 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
50300 ** if two files are created in the same file-system directory (i.e.
50304 static int winSectorSize(sqlite3_file *id){
50312 static int winDeviceCharacteristics(sqlite3_file *id){
50315 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
50349 static int winShmMutexHeld(void) {
50357 ** log-summary, each thread has its own winFile object, but they all
50359 ** log-summary is opened only once per process.
50367 ** The following fields are read-only after the object is created:
50382 int szRegion; /* Size of shared-memory regions */
50383 int nRegion; /* Size of array apRegion */
50384 u8 isReadonly; /* True if read-only */
50393 int nRef; /* Number of winShm objects pointing to this */
50413 ** are read-only thereafter:
50418 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
50444 static int winShmSystemLock(
50445 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50446 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
50447 int ofst, /* Offset to first byte to be locked/unlocked */
50448 int nByte /* Number of bytes to lock or unlock */
50450 int rc = 0; /* Result code form Lock/UnlockFileEx() */
50453 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
50455 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50456 pFile->hFile.h, lockType, ofst, nByte));
50458 /* Release/Acquire the system-level lock */
50460 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50465 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50471 pFile->lastErrno = osGetLastError();
50475 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50476 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50477 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50483 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
50484 static int winDelete(sqlite3_vfs *,const char*,int);
50489 ** This is not a VFS shared-memory method; it is a utility function called
50490 ** by VFS shared-memory methods.
50492 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
50496 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
50500 if( p->nRef==0 ){
50501 int i;
50502 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
50503 for(i=0; i<p->nRegion; i++){
50504 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
50505 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
50508 bRc = osCloseHandle(p->aRegion[i].hMap);
50509 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
50513 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
50515 winClose((sqlite3_file *)&p->hFile);
50521 winDelete(pVfs, p->zFilename, 0);
50525 *pp = p->pNext;
50526 sqlite3_free(p->aRegion);
50529 pp = &p->pNext;
50541 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
50543 static int winLockSharedMemory(winShmNode *pShmNode){
50544 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
50547 if( pShmNode->isReadonly ){
50548 pShmNode->isUnlocked = 1;
50551 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50554 "winLockSharedMemory", pShmNode->zFilename);
50566 ** Open the shared-memory area associated with database file pDbFd.
50568 ** When opening a new shared-memory file, if no other instances of that
50572 static int winOpenSharedMemory(winFile *pDbFd){
50575 int rc = SQLITE_OK; /* Result code */
50577 int nName; /* Size of zName in bytes */
50579 assert( pDbFd->pShm==0 ); /* Not previously opened */
50586 nName = sqlite3Strlen30(pDbFd->zPath);
50592 pNew->zFilename = (char*)&pNew[1];
50593 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
50594 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
50600 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
50604 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
50609 int inFlags = SQLITE_OPEN_WAL;
50610 int outFlags = 0;
50614 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50615 pShmNode->pNext = winShmNodeList;
50619 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50620 if( pShmNode->mutex==0 ){
50626 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50631 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50632 (sqlite3_file*)&pShmNode->hFile,
50636 pShmNode->zFilename);
50639 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
50646 p->pShmNode = pShmNode;
50648 p->id = pShmNode->nextShmId++;
50650 pShmNode->nRef++;
50651 pDbFd->pShm = p;
50658 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50661 sqlite3_mutex_enter(pShmNode->mutex);
50662 p->pNext = pShmNode->pFirst;
50663 pShmNode->pFirst = p;
50664 sqlite3_mutex_leave(pShmNode->mutex);
50670 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50678 ** Close a connection to shared-memory. Delete the underlying
50681 static int winShmUnmap(
50683 int deleteFlag /* Delete after closing if true */
50685 winFile *pDbFd; /* Database holding shared-memory */
50687 winShmNode *pShmNode; /* The underlying shared-memory file */
50691 p = pDbFd->pShm;
50693 pShmNode = p->pShmNode;
50697 sqlite3_mutex_enter(pShmNode->mutex);
50698 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50699 *pp = p->pNext;
50703 pDbFd->pShm = 0;
50704 sqlite3_mutex_leave(pShmNode->mutex);
50706 /* If pShmNode->nRef has reached 0, then close the underlying
50707 ** shared-memory file, too */
50709 assert( pShmNode->nRef>0 );
50710 pShmNode->nRef--;
50711 if( pShmNode->nRef==0 ){
50712 winShmPurge(pDbFd->pVfs, deleteFlag);
50720 ** Change the lock state for a shared-memory segment.
50722 static int winShmLock(
50724 int ofst, /* First lock to acquire or release */
50725 int n, /* Number of locks to acquire or release */
50726 int flags /* What to do with the lock */
50729 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50732 int rc = SQLITE_OK; /* Result code */
50736 pShmNode = p->pShmNode;
50747 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
50749 sqlite3_mutex_enter(pShmNode->mutex);
50754 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50756 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
50757 allMask |= pX->sharedMask;
50760 /* Unlock the system-level locks */
50769 p->exclMask &= ~mask;
50770 p->sharedMask &= ~mask;
50779 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50780 if( (pX->exclMask & mask)!=0 ){
50784 allShared |= pX->sharedMask;
50798 p->sharedMask |= mask;
50804 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50805 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
50817 assert( (p->sharedMask & mask)==0 );
50818 p->exclMask |= mask;
50822 sqlite3_mutex_leave(pShmNode->mutex);
50823 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
50824 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
50839 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
50846 ** shared-memory associated with the database file fd. Shared-memory regions
50847 ** are numbered starting from zero. Each shared-memory region is szRegion
50852 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
50855 ** isWrite is non-zero and the requested shared-memory region has not yet
50858 ** If the shared-memory region has already been allocated or is allocated by
50863 static int winShmMap(
50865 int iRegion, /* Region to retrieve */
50866 int szRegion, /* Size of regions */
50867 int isWrite, /* True to extend file if necessary */
50871 winShm *pShm = pDbFd->pShm;
50875 int rc = SQLITE_OK;
50880 pShm = pDbFd->pShm;
50883 pShmNode = pShm->pShmNode;
50885 sqlite3_mutex_enter(pShmNode->mutex);
50886 if( pShmNode->isUnlocked ){
50889 pShmNode->isUnlocked = 0;
50891 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
50893 if( pShmNode->nRegion<=iRegion ){
50895 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
50896 sqlite3_int64 sz; /* Current size of wal-index file */
50898 pShmNode->szRegion = szRegion;
50901 ** Check to see if it has been allocated (i.e. if the wal-index file is
50904 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
50907 "winShmMap1", pDbFd->zPath);
50915 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
50919 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
50922 "winShmMap2", pDbFd->zPath);
50929 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
50935 pShmNode->aRegion = apNew;
50937 if( pShmNode->isReadonly ){
50942 while( pShmNode->nRegion<=iRegion ){
50943 HANDLE hMap = NULL; /* file-mapping handle */
50947 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
50951 hMap = osCreateFileMappingW(pShmNode->hFile.h,
50955 hMap = osCreateFileMappingA(pShmNode->hFile.h,
50959 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
50960 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
50963 int iOffset = pShmNode->nRegion*szRegion;
50964 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
50967 iOffset - iOffsetShift, szRegion + iOffsetShift
50971 0, iOffset - iOffsetShift, szRegion + iOffsetShift
50974 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
50975 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
50979 pShmNode->lastErrno = osGetLastError();
50980 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
50981 "winShmMap3", pDbFd->zPath);
50986 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
50987 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
50988 pShmNode->nRegion++;
50993 if( pShmNode->nRegion>iRegion ){
50994 int iOffset = iRegion*szRegion;
50995 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
50996 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51001 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
51002 sqlite3_mutex_leave(pShmNode->mutex);
51017 static int winUnmapfile(winFile *pFile){
51019 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
51021 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
51022 pFile->mmapSize, pFile->mmapSizeMax));
51023 if( pFile->pMapRegion ){
51024 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
51025 pFile->lastErrno = osGetLastError();
51026 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
51028 pFile->pMapRegion));
51029 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51030 "winUnmapfile1", pFile->zPath);
51032 pFile->pMapRegion = 0;
51033 pFile->mmapSize = 0;
51035 if( pFile->hMap!=NULL ){
51036 if( !osCloseHandle(pFile->hMap) ){
51037 pFile->lastErrno = osGetLastError();
51038 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
51039 osGetCurrentProcessId(), pFile, pFile->hMap));
51040 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51041 "winUnmapfile2", pFile->zPath);
51043 pFile->hMap = NULL;
51045 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51051 ** Memory map or remap the file opened by file-descriptor pFd (if the file
51054 ** outstanding xFetch() references to it, this function is a no-op.
51056 ** If parameter nByte is non-negative, then it is the requested size of
51066 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
51068 int rc;
51070 assert( nMap>=0 || pFd->nFetchOut==0 );
51071 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
51074 if( pFd->nFetchOut>0 ) return SQLITE_OK;
51079 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
51084 if( nMap>pFd->mmapSizeMax ){
51085 nMap = pFd->mmapSizeMax;
51087 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
51089 if( nMap==0 && pFd->mmapSize>0 ){
51092 if( nMap!=pFd->mmapSize ){
51099 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
51105 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
51107 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
51111 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
51115 if( pFd->hMap==NULL ){
51116 pFd->lastErrno = osGetLastError();
51117 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
51118 "winMapfile1", pFd->zPath);
51120 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
51127 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
51129 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
51132 osCloseHandle(pFd->hMap);
51133 pFd->hMap = NULL;
51134 pFd->lastErrno = osGetLastError();
51135 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
51136 "winMapfile2", pFd->zPath);
51138 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
51142 pFd->pMapRegion = pNew;
51143 pFd->mmapSize = nMap;
51146 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51164 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
51174 if( pFd->mmapSizeMax>0 ){
51179 const int nEofBuffer = 256;
51180 if( pFd->pMapRegion==0 ){
51181 int rc = winMapfile(pFd, -1);
51188 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
51189 assert( pFd->pMapRegion!=0 );
51190 *pp = &((u8 *)pFd->pMapRegion)[iOff];
51191 pFd->nFetchOut++;
51202 ** If the third argument is non-NULL, then this function releases a
51211 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
51218 assert( (p==0)==(pFd->nFetchOut==0) );
51221 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
51227 pFd->nFetchOut--;
51231 ** is unnecessary can be omitted - potentially improving
51236 assert( pFd->nFetchOut>=0 );
51324 ** supports for filenames into UTF-8. Space to hold the result is
51343 ** Convert a UTF-8 filename into whatever form the underlying
51363 ** This function returns non-zero if the specified UTF-8 string buffer
51367 static int winMakeEndInDirSep(int nBuf, char *zBuf){
51369 int nLen = sqlite3Strlen30(zBuf);
51371 if( winIsDirSep(zBuf[nLen-1]) ){
51389 static int winTempDirDefined(void){
51400 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
51407 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
51408 int nMax, nBuf, nDir, nLen;
51411 /* It's odd to simulate an io-error here, but really this is just
51412 ** using the io-error infrastructure to test that SQLite handles this
51420 nMax = pVfs->mxPathname; nBuf = nMax + 2;
51423 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51431 nDir = nMax - (nPre + 15);
51434 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
51436 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
51442 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51464 unsigned int i;
51484 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51497 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51505 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
51512 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
51518 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51536 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51542 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51554 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51564 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51569 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51579 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51593 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51611 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51615 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
51623 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
51629 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
51638 static int winIsDir(const void *zConverted){
51640 int rc = 0;
51644 int cnt = 0;
51663 static int winAccess(
51666 int flags, /* Type of test to make on this file */
51667 int *pResOut /* OUT: Result */
51673 static int winOpen(
51675 const char *zName, /* Name of the file (UTF-8) */
51677 int flags, /* Open mode flags */
51678 int *pOutFlags /* Status return flags */
51687 int isTemp = 0;
51692 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
51693 int cnt = 0;
51700 int rc = SQLITE_OK; /* Function Return Code */
51702 int eType = flags&0xFFFFFF00; /* Type of file to open */
51705 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
51706 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
51707 int isCreate = (flags & SQLITE_OPEN_CREATE);
51708 int isReadonly = (flags & SQLITE_OPEN_READONLY);
51709 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
51712 int isOpenJournal = (isCreate && (
51734 /* The main DB, main journal, WAL file and super-journal are never
51741 /* Assert that the upper layer has set one of the "file-type" flags. */
51750 pFile->h = INVALID_HANDLE_VALUE;
51772 /* Database filenames are double-zero terminated if they are not
51858 int rc2, isRO = 0;
51875 int rc2, isRO = 0;
51895 int rc2, isRO = 0;
51918 pFile->lastErrno = lastErrno;
51919 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
51936 pAppData = (winVfsAppData*)pVfs->pAppData;
51941 && ((pAppData==NULL) || !pAppData->bNoLock)
51947 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
51952 pFile->zDeleteOnClose = zConverted;
51960 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
51961 pFile->pVfs = pVfs;
51962 pFile->h = h;
51964 pFile->ctrlFlags |= WINFILE_RDONLY;
51969 pFile->ctrlFlags |= WINFILE_PSOW;
51971 pFile->lastErrno = NO_ERROR;
51972 pFile->zPath = zName;
51974 pFile->hMap = NULL;
51975 pFile->pMapRegion = 0;
51976 pFile->mmapSize = 0;
51977 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
51996 static int winDelete(
51999 int syncDir /* Not used on win32 */
52001 int cnt = 0;
52002 int rc;
52104 static int winAccess(
52107 int flags, /* Type of test to make on this file */
52108 int *pResOut /* OUT: Result */
52111 int rc = 0;
52133 int cnt = 0;
52140 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
52186 ** Returns non-zero if the specified path name starts with the "long path"
52197 ** Returns non-zero if the specified path name starts with a drive letter
52207 ** Returns non-zero if the specified path name should be used verbatim. If
52208 ** non-zero is returned from this function, the calling function must simply
52209 ** use the provided path name verbatim -OR- resolve it into a full path name
52245 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
52248 static int winFullPathnameNoMutex(
52251 int nFull, /* Size of output buffer in bytes */
52271 assert( nFull>=pVfs->mxPathname );
52279 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52285 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
52295 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52301 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52307 zRelative, zOut, pVfs->mxPathname+1)<0 ){
52317 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
52336 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52339 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
52345 /* It's odd to simulate an io-error here, but really this is just
52346 ** using the io-error infrastructure to test that SQLite handles this
52358 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52419 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
52427 static int winFullPathname(
52430 int nFull, /* Size of output buffer in bytes */
52433 int rc;
52450 int nFull = pVfs->mxPathname+1;
52488 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
52516 int na; /* Size of a[] in bytes */
52517 int i; /* XOR next input into a[i] */
52518 int nXor; /* Number of XOR operations done */
52523 static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
52524 int j, k;
52525 for(j=0, k=p->i; j<sz; j++){
52526 p->a[k++] ^= x[j];
52527 if( k>=p->na ) k = 0;
52529 p->i = k;
52530 p->nXor += sz;
52537 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52594 static int winSleep(sqlite3_vfs *pVfs, int microsec){
52601 ** The following variable, if set to a non-zero value, is interpreted as
52606 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
52619 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
52620 /* FILETIME structure is a 64-bit value representing the number of
52621 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
52628 /* 2^32 - to avoid use of LL and warnings in gcc */
52662 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
52663 int rc;
52678 ** buffer with a nul-terminated UTF-8 encoded error message
52684 ** is zero if the error message fits in the buffer, or non-zero
52685 ** otherwise (if the message was truncated). If non-zero is returned,
52686 ** then it is not necessary to include the nul-terminator character
52693 ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52702 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52712 SQLITE_API int sqlite3_os_init(void){
52743 "win32-longpath", /* zName */
52768 "win32-none", /* zName */
52793 "win32-longpath-none", /* zName */
52814 /* Double-check that the aSyscall[] array has been constructed
52847 SQLITE_API int sqlite3_os_end(void){
52867 ** 2016-09-07
52878 ** This file implements an in-memory VFS. A database is held as a contiguous
52894 /* Access to a lower-level VFS that (might) implement dynamic loading,
52897 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
52941 int nMmap; /* Number of memory mapped pages */
52943 int nRdLock; /* Number of readers */
52944 int nWrLock; /* Number of writers. (Always 0 or 1) */
52945 int nRef; /* Number of users of this MemStore */
52953 int eLock; /* Most recent lock against this file */
52957 ** File-scope variables for holding the memdb files that are accessible
52963 int nMemStore; /* Number of shared MemStore objects */
52970 static int memdbClose(sqlite3_file*);
52971 static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
52972 static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
52973 static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
52974 static int memdbSync(sqlite3_file*, int flags);
52975 static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
52976 static int memdbLock(sqlite3_file*, int);
52977 static int memdbUnlock(sqlite3_file*, int);
52978 /* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
52979 static int memdbFileControl(sqlite3_file*, int op, void *pArg);
52980 /* static int memdbSectorSize(sqlite3_file*); // not used */
52981 static int memdbDeviceCharacteristics(sqlite3_file*);
52982 static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
52983 static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
52988 static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
52989 /* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
52990 static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
52991 static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
52993 static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
52996 static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
52997 static int memdbSleep(sqlite3_vfs*, int microseconds);
52998 /* static int memdbCurrentTime(sqlite3_vfs*, double*); */
52999 static int memdbGetLastError(sqlite3_vfs*, int, char *);
53000 static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
53061 sqlite3_mutex_enter(p->pMutex);
53064 sqlite3_mutex_leave(p->pMutex);
53071 ** Close an memdb-file.
53075 static int memdbClose(sqlite3_file *pFile){
53076 MemStore *p = ((MemFile*)pFile)->pStore;
53077 if( p->zFName ){
53078 int i;
53086 if( p->nRef==1 ){
53087 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
53100 p->nRef--;
53101 if( p->nRef<=0 ){
53102 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
53103 sqlite3_free(p->aData);
53106 sqlite3_mutex_free(p->pMutex);
53115 ** Read data from an memdb-file.
53117 static int memdbRead(
53120 int iAmt,
53123 MemStore *p = ((MemFile*)pFile)->pStore;
53125 if( iOfst+iAmt>p->sz ){
53127 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
53131 memcpy(zBuf, p->aData+iOfst, iAmt);
53139 static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
53141 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
53144 if( newSz>p->szMax ){
53148 if( newSz>p->szMax ) newSz = p->szMax;
53149 pNew = sqlite3Realloc(p->aData, newSz);
53151 p->aData = pNew;
53152 p->szAlloc = newSz;
53157 ** Write data to an memdb-file.
53159 static int memdbWrite(
53162 int iAmt,
53165 MemStore *p = ((MemFile*)pFile)->pStore;
53167 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
53173 if( iOfst+iAmt>p->sz ){
53174 int rc;
53175 if( iOfst+iAmt>p->szAlloc
53181 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
53182 p->sz = iOfst+iAmt;
53184 memcpy(p->aData+iOfst, z, iAmt);
53190 ** Truncate an memdb-file.
53196 static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
53197 MemStore *p = ((MemFile*)pFile)->pStore;
53198 int rc = SQLITE_OK;
53200 if( size>p->sz ){
53204 p->sz = size;
53211 ** Sync an memdb-file.
53213 static int memdbSync(sqlite3_file *pFile, int flags){
53220 ** Return the current file-size of an memdb-file.
53222 static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
53223 MemStore *p = ((MemFile*)pFile)->pStore;
53225 *pSize = p->sz;
53231 ** Lock an memdb-file.
53233 static int memdbLock(sqlite3_file *pFile, int eLock){
53235 MemStore *p = pThis->pStore;
53236 int rc = SQLITE_OK;
53237 if( eLock<=pThis->eLock ) return SQLITE_OK;
53240 assert( p->nWrLock==0 || p->nWrLock==1 );
53241 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
53242 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
53244 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
53249 assert( pThis->eLock==SQLITE_LOCK_NONE );
53250 if( p->nWrLock>0 ){
53253 p->nRdLock++;
53260 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
53261 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
53262 if( p->nWrLock>0 ){
53265 p->nWrLock = 1;
53273 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
53274 if( p->nRdLock>1 ){
53276 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
53277 p->nWrLock = 1;
53283 if( rc==SQLITE_OK ) pThis->eLock = eLock;
53289 ** Unlock an memdb-file.
53291 static int memdbUnlock(sqlite3_file *pFile, int eLock){
53293 MemStore *p = pThis->pStore;
53294 if( eLock>=pThis->eLock ) return SQLITE_OK;
53299 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
53300 p->nWrLock--;
53303 if( pThis->eLock>SQLITE_LOCK_SHARED ){
53304 p->nWrLock--;
53306 p->nRdLock--;
53309 pThis->eLock = eLock;
53317 ** occur on an in-memory database.
53319 static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
53327 ** File control method. For custom operations on an memdb-file.
53329 static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
53330 MemStore *p = ((MemFile*)pFile)->pStore;
53331 int rc = SQLITE_NOTFOUND;
53334 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
53339 if( iLimit<p->sz ){
53341 iLimit = p->szMax;
53343 iLimit = p->sz;
53346 p->szMax = iLimit;
53356 ** Return the sector-size in bytes for an memdb-file.
53358 static int memdbSectorSize(sqlite3_file *pFile){
53364 ** Return the device characteristic flags supported by an memdb-file.
53366 static int memdbDeviceCharacteristics(sqlite3_file *pFile){
53374 /* Fetch a page of a memory-mapped file */
53375 static int memdbFetch(
53378 int iAmt,
53381 MemStore *p = ((MemFile*)pFile)->pStore;
53383 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
53386 p->nMmap++;
53387 *pp = (void*)(p->aData + iOfst);
53393 /* Release a memory-mapped page */
53394 static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
53395 MemStore *p = ((MemFile*)pFile)->pStore;
53399 p->nMmap--;
53407 static int memdbOpen(
53411 int flags,
53412 int *pOutFlags
53416 int szName;
53422 int i;
53428 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
53450 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
53451 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
53452 p->zFName = (char*)&p[1];
53453 memcpy(p->zFName, zName, szName+1);
53454 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
53455 if( p->pMutex==0 ){
53456 memdb_g.nMemStore--;
53461 p->nRef = 1;
53465 p->nRef++;
53474 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
53475 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
53477 pFile->pStore = p;
53481 pFd->pMethods = &memdb_io_methods;
53486 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
53490 ** ensure the file-system modifications are synced to disk before
53493 static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
53504 static int memdbAccess(
53507 int flags,
53508 int *pResOut
53522 static int memdbFullPathname(
53525 int nOut,
53537 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
53542 ** utf-8 string describing the most recent error encountered associated
53545 static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
53546 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
53553 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
53560 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
53567 static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
53568 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
53575 static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
53576 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
53583 static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
53584 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
53588 static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
53589 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
53591 static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
53592 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
53602 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
53604 if( p->base.pMethods!=&memdb_io_methods ) return 0;
53605 pStore = p->pStore;
53607 if( pStore->zFName!=0 ) p = 0;
53619 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
53622 int iDb;
53625 int szPage = 0;
53629 int rc;
53638 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53641 if( piSize ) *piSize = -1;
53644 MemStore *pStore = p->pStore;
53645 assert( pStore->pMutex==0 );
53646 if( piSize ) *piSize = pStore->sz;
53648 pOut = pStore->aData;
53650 pOut = sqlite3_malloc64( pStore->sz );
53651 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
53655 pBt = db->aDb[iDb].pBt;
53659 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
53681 int nPage = sqlite3_column_int(pStmt, 0);
53683 int pgno;
53686 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
53704 SQLITE_API int sqlite3_deserialize(
53715 int rc;
53716 int iDb;
53726 sqlite3_mutex_enter(db->mutex);
53727 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53738 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
53742 db->init.iDb = (u8)iDb;
53743 db->init.reopenMemdb = 1;
53745 db->init.reopenMemdb = 0;
53754 MemStore *pStore = p->pStore;
53755 pStore->aData = pData;
53757 pStore->sz = szDb;
53758 pStore->szAlloc = szBuf;
53759 pStore->szMax = szBuf;
53760 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
53761 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
53763 pStore->mFlags = mFlags;
53772 sqlite3_mutex_leave(db->mutex);
53779 SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
53787 SQLITE_PRIVATE int sqlite3MemdbInit(void){
53789 unsigned int sz;
53791 sz = pLower->szOsFile;
53816 ** This file implements an object that represents a fixed-length
53820 ** journalled during a transaction, or which pages have the "dont-write"
53849 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
53866 ** sub-dividing and re-hashing. */
53871 ** no fewer collisions than the no-op *1. */
53891 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
53900 u32 nSet; /* Number of bits that are set - only valid for aHash
53924 p->iSize = iSize;
53930 ** Check to see if the i-th bit is set. Return true or false.
53934 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
53936 i--;
53937 if( i>=p->iSize ) return 0;
53938 while( p->iDivisor ){
53939 u32 bin = i/p->iDivisor;
53940 i = i%p->iDivisor;
53941 p = p->u.apSub[bin];
53946 if( p->iSize<=BITVEC_NBIT ){
53947 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
53950 while( p->u.aHash[h] ){
53951 if( p->u.aHash[h]==i ) return 1;
53957 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
53962 ** Set the i-th bit. Return 0 on success and an error code if
53965 ** This routine might cause sub-bitmaps to be allocated. Failing
53966 ** to get the memory needed to hold the sub-bitmap is the only
53973 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
53977 assert( i<=p->iSize );
53978 i--;
53979 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
53980 u32 bin = i/p->iDivisor;
53981 i = i%p->iDivisor;
53982 if( p->u.apSub[bin]==0 ){
53983 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
53984 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
53986 p = p->u.apSub[bin];
53988 if( p->iSize<=BITVEC_NBIT ){
53989 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
53995 /* worrying about sub-dividing and re-hashing. */
53996 if( !p->u.aHash[h] ){
53997 if (p->nSet<(BITVEC_NINT-1)) {
54006 if( p->u.aHash[h]==i ) return SQLITE_OK;
54009 } while( p->u.aHash[h] );
54014 if( p->nSet>=BITVEC_MXHASH ){
54015 unsigned int j;
54016 int rc;
54017 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
54021 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54022 memset(p->u.apSub, 0, sizeof(p->u.apSub));
54023 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
54033 p->nSet++;
54034 p->u.aHash[h] = i;
54039 ** Clear the i-th bit.
54047 i--;
54048 while( p->iDivisor ){
54049 u32 bin = i/p->iDivisor;
54050 i = i%p->iDivisor;
54051 p = p->u.apSub[bin];
54056 if( p->iSize<=BITVEC_NBIT ){
54057 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
54059 unsigned int j;
54061 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54062 memset(p->u.aHash, 0, sizeof(p->u.aHash));
54063 p->nSet = 0;
54066 u32 h = BITVEC_HASH(aiValues[j]-1);
54067 p->nSet++;
54068 while( p->u.aHash[h] ){
54072 p->u.aHash[h] = aiValues[j];
54083 if( p->iDivisor ){
54084 unsigned int i;
54086 sqlite3BitvecDestroy(p->u.apSub[i]);
54097 return p->iSize;
54139 ** If a memory allocation error occurs, return -1.
54141 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
54144 int rc = -1;
54145 int i, nx, pc, op;
54167 i = aOp[pc+2] - 1;
54179 if( (--aOp[pc+1]) > 0 ) nx = 0;
54195 ** match (rc==0). Change rc to non-zero if a discrepancy
54200 + (sqlite3BitvecSize(pBitvec) - sz);
54246 ** such that p was added to the list more recently than p->pDirtyNext.
54251 ** page to eject from the cache mid-transaction. It is better to eject
54263 int szCache; /* Configured cache size */
54264 int szSpill; /* Size before spilling occurs */
54265 int szPage; /* Size of every page in this cache */
54266 int szExtra; /* Size of extra space for each page */
54269 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
54284 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
54285 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
54287 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
54290 int j;
54294 pPg = (PgHdr*)pLower->pExtra;
54295 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
54296 a = (unsigned char *)pLower->pBuf;
54302 int N;
54303 int i;
54307 if( pCache->pCache==0 ) return;
54311 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
54313 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
54314 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
54329 static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
54331 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54336 static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){
54338 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54358 SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
54361 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
54362 pCache = pPg->pCache;
54364 if( pPg->flags & PGHDR_CLEAN ){
54365 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
54368 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
54369 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
54370 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
54371 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
54375 if( pPg->flags & PGHDR_WRITEABLE ){
54376 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
54414 PCache *p = pPage->pCache;
54418 pPage->pgno));
54420 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
54421 assert( pPage->pDirtyPrev || pPage==p->pDirty );
54424 if( p->pSynced==pPage ){
54425 p->pSynced = pPage->pDirtyPrev;
54428 if( pPage->pDirtyNext ){
54429 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
54431 assert( pPage==p->pDirtyTail );
54432 p->pDirtyTail = pPage->pDirtyPrev;
54434 if( pPage->pDirtyPrev ){
54435 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
54441 assert( pPage==p->pDirty );
54442 p->pDirty = pPage->pDirtyNext;
54443 assert( p->bPurgeable || p->eCreate==2 );
54444 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
54445 assert( p->bPurgeable==0 || p->eCreate==1 );
54446 p->eCreate = 2;
54451 pPage->pDirtyPrev = 0;
54452 pPage->pDirtyNext = p->pDirty;
54453 if( pPage->pDirtyNext ){
54454 assert( pPage->pDirtyNext->pDirtyPrev==0 );
54455 pPage->pDirtyNext->pDirtyPrev = pPage;
54457 p->pDirtyTail = pPage;
54458 if( p->bPurgeable ){
54459 assert( p->eCreate==2 );
54460 p->eCreate = 1;
54463 p->pDirty = pPage;
54469 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
54470 if( !p->pSynced
54471 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
54473 p->pSynced = pPage;
54481 ** being used for an in-memory database, this function is a no-op.
54484 if( p->pCache->bPurgeable ){
54485 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
54486 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
54487 pcacheDump(p->pCache);
54492 ** Compute the number of pages of cache requested. p->szCache is the
54495 static int numberOfCachePages(PCache *p){
54496 if( p->szCache>=0 ){
54497 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
54499 return p->szCache;
54502 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
54506 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
54508 return (int)n;
54517 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
54519 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
54520 ** built-in default page cache is used instead of the application defined
54529 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
54537 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
54551 SQLITE_PRIVATE int sqlite3PcacheOpen(
54552 int szPage, /* Size of every page */
54553 int szExtra, /* Extra space associated with each page */
54554 int bPurgeable, /* True if pages are on backing store */
54555 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
54560 p->szPage = 1;
54561 p->szExtra = szExtra;
54563 p->bPurgeable = bPurgeable;
54564 p->eCreate = 2;
54565 p->xStress = xStress;
54566 p->pStress = pStress;
54567 p->szCache = 100;
54568 p->szSpill = 1;
54577 SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
54578 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
54579 if( pCache->szPage ){
54582 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
54583 pCache->bPurgeable
54587 if( pCache->pCache ){
54588 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54590 pCache->pCache = pNew;
54591 pCache->szPage = szPage;
54624 int createFlag /* If true, create page if it does not exist already */
54626 int eCreate;
54630 assert( pCache->pCache!=0 );
54632 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
54641 eCreate = createFlag & pCache->eCreate;
54643 assert( createFlag==0 || pCache->eCreate==eCreate );
54644 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
54645 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
54663 SQLITE_PRIVATE int sqlite3PcacheFetchStress(
54669 if( pCache->eCreate==2 ) return 0;
54671 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
54672 /* Find a dirty page to write-out and recycle. First try to find a
54673 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
54681 for(pPg=pCache->pSynced;
54682 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
54683 pPg=pPg->pDirtyPrev
54685 pCache->pSynced = pPg;
54687 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
54690 int rc;
54693 "spill page %d making room for %d - cache used: %d/%d",
54694 pPg->pgno, pgno,
54695 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
54698 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
54699 rc = pCache->xStress(pCache->pStress, pPg);
54706 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
54715 ** This routine is broken out into a separate function since it
54726 pPgHdr = (PgHdr*)pPage->pExtra;
54727 assert( pPgHdr->pPage==0 );
54728 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
54729 pPgHdr->pPage = pPage;
54730 pPgHdr->pData = pPage->pBuf;
54731 pPgHdr->pExtra = (void *)&pPgHdr[1];
54732 memset(pPgHdr->pExtra, 0, 8);
54733 pPgHdr->pCache = pCache;
54734 pPgHdr->pgno = pgno;
54735 pPgHdr->flags = PGHDR_CLEAN;
54753 pPgHdr = (PgHdr *)pPage->pExtra;
54755 if( !pPgHdr->pPage ){
54758 pCache->nRefSum++;
54759 pPgHdr->nRef++;
54769 assert( p->nRef>0 );
54770 p->pCache->nRefSum--;
54771 if( (--p->nRef)==0 ){
54772 if( p->flags&PGHDR_CLEAN ){
54785 assert(p->nRef>0);
54787 p->nRef++;
54788 p->pCache->nRefSum++;
54797 assert( p->nRef==1 );
54799 if( p->flags&PGHDR_DIRTY ){
54802 p->pCache->nRefSum--;
54803 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
54811 assert( p->nRef>0 );
54813 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
54814 p->flags &= ~PGHDR_DONT_WRITE;
54815 if( p->flags & PGHDR_CLEAN ){
54816 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
54817 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
54818 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
54832 assert( (p->flags & PGHDR_DIRTY)!=0 );
54833 assert( (p->flags & PGHDR_CLEAN)==0 );
54835 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54836 p->flags |= PGHDR_CLEAN;
54837 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
54839 if( p->nRef==0 ){
54849 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
54850 while( (p = pCache->pDirty)!=0 ){
54860 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
54861 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54862 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54864 pCache->pSynced = pCache->pDirtyTail;
54872 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54873 p->flags &= ~PGHDR_NEED_SYNC;
54875 pCache->pSynced = pCache->pDirtyTail;
54882 PCache *pCache = p->pCache;
54884 assert( p->nRef>0 );
54887 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
54888 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
54890 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
54891 assert( pXPage->nRef==0 );
54892 pXPage->nRef++;
54893 pCache->nRefSum++;
54896 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
54897 p->pgno = newPgno;
54898 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
54914 if( pCache->pCache ){
54918 for(p=pCache->pDirty; p; p=pNext){
54919 pNext = p->pDirtyNext;
54924 assert( p->pgno>0 );
54925 if( p->pgno>pgno ){
54926 assert( p->flags&PGHDR_DIRTY );
54930 if( pgno==0 && pCache->nRefSum ){
54932 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
54934 ** pCache->nRefSum>0 */
54935 memset(pPage1->pBuf, 0, pCache->szPage);
54939 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
54947 assert( pCache->pCache!=0 );
54949 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54968 if( pA->pgno<pB->pgno ){
54969 pTail->pDirty = pA;
54971 pA = pA->pDirty;
54973 pTail->pDirty = pB;
54977 pTail->pDirty = pB;
54979 pB = pB->pDirty;
54981 pTail->pDirty = pA;
55002 int i;
55006 pIn = p->pDirty;
55007 p->pDirty = 0;
55008 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
55017 if( NEVER(i==N_SORT_BUCKET-1) ){
55037 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55038 p->pDirty = p->pDirtyNext;
55040 return pcacheSortDirtyList(pCache->pDirty);
55050 return pCache->nRefSum;
55057 return p->nRef;
55063 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
55064 assert( pCache->pCache!=0 );
55065 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
55070 ** Get the suggested cache-size value.
55072 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
55078 ** Set the suggested cache-size value.
55080 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
55081 assert( pCache->pCache!=0 );
55082 pCache->szCache = mxPage;
55083 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
55088 ** Set the suggested cache-spill value. Make no changes if if the
55089 ** argument is zero. Return the effective cache-spill size, which will
55092 SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
55093 int res;
55094 assert( p->pCache!=0 );
55097 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
55099 p->szSpill = mxPage;
55102 if( res<p->szSpill ) res = p->szSpill;
55110 assert( pCache->pCache!=0 );
55111 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
55116 ** in the page-cache hierarchy.
55118 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
55124 SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
55126 int nDirty = 0;
55127 int nCache = numberOfCachePages(pCache);
55128 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
55129 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
55136 SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
55137 return (pCache->pDirty!=0);
55149 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
55177 ** -------------------------------------------------------------
55179 ** -------------------------------------------------------------
55195 ** size can vary according to architecture, compile-time options, and
55201 ** allocations. However, the btree layer needs a small (16-byte) overrun
55216 ** (1) The general-purpose memory allocator - sqlite3Malloc()
55217 ** (2) Global page-cache memory provided using sqlite3_config() with
55219 ** (3) PCache-local bulk allocation.
55229 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
55233 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
55249 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
55259 ** but causes a 2-byte gap in the structure for most architectures (since
55260 ** pointers must be either 4 or 8-byte aligned). As this structure is located
55262 ** corrupt, code at the b-tree layer may overread the page buffer and
55268 ** The pLruNext and pLruPrev pointers form a double-linked circular list
55275 unsigned int iKey; /* Key value (page number) */
55289 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
55290 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
55316 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
55317 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
55318 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
55319 unsigned int nPurgeable; /* Number of purgeable pages allocated */
55324 ** open database file (including each in-memory database and each
55339 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
55340 int szPage; /* Size of database content section */
55341 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
55342 int szAlloc; /* Total size of one pcache line */
55343 int bPurgeable; /* True if cache is purgeable */
55344 unsigned int nMin; /* Minimum number of pages reserved */
55345 unsigned int nMax; /* Configured "cache_size" value */
55346 unsigned int n90pct; /* nMax*9/10 */
55347 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
55348 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
55353 unsigned int nRecyclable; /* Number of pages in the LRU list */
55354 unsigned int nPage; /* Total number of pages in apHash */
55355 unsigned int nHash; /* Number of slots in apHash[] */
55357 PgHdr1 *pFree; /* List of unused pcache-local pages */
55358 void *pBulk; /* Bulk memory used by pcache-local */
55380 int isInit; /* True if initialized */
55381 int separateCache; /* Use a new PGroup for each PCache */
55382 int nInitPage; /* Initial bulk allocation size */
55383 int szSlot; /* Size of each free slot */
55384 int nSlot; /* The number of pcache slots */
55385 int nReserve; /* Try to keep nFreeSlot above this */
55390 int nFreeSlot; /* Number of unused pcache slots */
55392 ** reading because (1) most platforms read a 32-bit integer atomically and
55395 int bUnderPressure; /* True if low on PAGECACHE memory */
55409 # define pcache1EnterMutex(X) assert((X)->mutex==0)
55410 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
55413 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
55414 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
55424 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
55431 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
55443 while( n-- ){
55445 p->pNext = pcache1.pFree;
55454 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
55455 ** true if pCache->pFree ends up containing one or more free pages.
55457 static int pcache1InitBulk(PCache1 *pCache){
55462 if( pCache->nMax<3 ) return 0;
55465 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
55467 szBulk = -1024 * (i64)pcache1.nInitPage;
55469 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
55470 szBulk = pCache->szAlloc*(i64)pCache->nMax;
55472 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
55475 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
55477 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
55478 pX->page.pBuf = zBulk;
55479 pX->page.pExtra = &pX[1];
55480 pX->isBulkLocal = 1;
55481 pX->isAnchor = 0;
55482 pX->pNext = pCache->pFree;
55483 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
55484 pCache->pFree = pX;
55485 zBulk += pCache->szAlloc;
55486 }while( --nBulk );
55488 return pCache->pFree!=0;
55500 static void *pcache1Alloc(int nByte){
55507 pcache1.pFree = pcache1.pFree->pNext;
55508 pcache1.nFreeSlot--;
55523 int sz = sqlite3MallocSize(p);
55545 pSlot->pNext = pcache1.pFree;
55556 int nFreed = 0;
55571 static int pcache1MemSize(void *p){
55575 int iSize;
55588 static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
55592 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55593 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
55594 assert( pCache->pFree!=0 );
55595 p = pCache->pFree;
55596 pCache->pFree = p->pNext;
55597 p->pNext = 0;
55604 assert( pCache->pGroup==&pcache1.grp );
55605 pcache1LeaveMutex(pCache->pGroup);
55608 pPg = pcache1Alloc(pCache->szAlloc);
55611 pcache1EnterMutex(pCache->pGroup);
55614 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55615 p->page.pBuf = pPg;
55616 p->page.pExtra = &p[1];
55617 p->isBulkLocal = 0;
55618 p->isAnchor = 0;
55619 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55621 (*pCache->pnPurgeable)++;
55631 pCache = p->pCache;
55632 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
55633 if( p->isBulkLocal ){
55634 p->pNext = pCache->pFree;
55635 pCache->pFree = p;
55637 pcache1Free(p->page.pBuf);
55639 (*pCache->pnPurgeable)--;
55647 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
55676 static int pcache1UnderMemoryPressure(PCache1 *pCache){
55677 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
55695 unsigned int nNew;
55696 unsigned int i;
55698 assert( sqlite3_mutex_held(p->pGroup->mutex) );
55700 nNew = p->nHash*2;
55705 pcache1LeaveMutex(p->pGroup);
55706 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
55708 if( p->nHash ){ sqlite3EndBenignMalloc(); }
55709 pcache1EnterMutex(p->pGroup);
55711 for(i=0; i<p->nHash; i++){
55713 PgHdr1 *pNext = p->apHash[i];
55715 unsigned int h = pPage->iKey % nNew;
55716 pNext = pPage->pNext;
55717 pPage->pNext = apNew[h];
55721 sqlite3_free(p->apHash);
55722 p->apHash = apNew;
55723 p->nHash = nNew;
55730 ** LRU list, then this function is a no-op.
55737 assert( pPage->pLruNext );
55738 assert( pPage->pLruPrev );
55739 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
55740 pPage->pLruPrev->pLruNext = pPage->pLruNext;
55741 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
55742 pPage->pLruNext = 0;
55743 /* pPage->pLruPrev = 0;
55745 assert( pPage->isAnchor==0 );
55746 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
55747 pPage->pCache->nRecyclable--;
55759 static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
55760 unsigned int h;
55761 PCache1 *pCache = pPage->pCache;
55764 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55765 h = pPage->iKey % pCache->nHash;
55766 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
55767 *pp = (*pp)->pNext;
55769 pCache->nPage--;
55778 PGroup *pGroup = pCache->pGroup;
55780 assert( sqlite3_mutex_held(pGroup->mutex) );
55781 while( pGroup->nPurgeable>pGroup->nMaxPage
55782 && (p=pGroup->lru.pLruPrev)->isAnchor==0
55784 assert( p->pCache->pGroup==pGroup );
55789 if( pCache->nPage==0 && pCache->pBulk ){
55790 sqlite3_free(pCache->pBulk);
55791 pCache->pBulk = pCache->pFree = 0;
55804 unsigned int iLimit /* Drop pages with this pgno or larger */
55806 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
55807 unsigned int h, iStop;
55808 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55809 assert( pCache->iMaxKey >= iLimit );
55810 assert( pCache->nHash > 0 );
55811 if( pCache->iMaxKey - iLimit < pCache->nHash ){
55816 h = iLimit % pCache->nHash;
55817 iStop = pCache->iMaxKey % pCache->nHash;
55818 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
55822 h = pCache->nHash/2;
55823 iStop = h - 1;
55828 assert( h<pCache->nHash );
55829 pp = &pCache->apHash[h];
55831 if( pPage->iKey>=iLimit ){
55832 pCache->nPage--;
55833 *pp = pPage->pNext;
55837 pp = &pPage->pNext;
55842 h = (h+1) % pCache->nHash;
55844 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
55853 static int pcache1Init(void *NotUsed){
55861 ** private PGroup (mode-1). pcache1.separateCache is false if the single
55862 ** PGroup in pcache1.grp is used for all page caches (mode-2).
55864 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
55866 ** * Use a unified cache in single-threaded applications that have
55867 ** configured a start-time buffer for use as page-cache memory using
55868 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
55871 ** * Otherwise use separate caches (mode-1)
55920 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
55923 int sz; /* Bytes of memory required to allocate the new cache */
55925 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
55933 pGroup->mxPinned = 10;
55938 if( pGroup->lru.isAnchor==0 ){
55939 pGroup->lru.isAnchor = 1;
55940 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
55942 pCache->pGroup = pGroup;
55943 pCache->szPage = szPage;
55944 pCache->szExtra = szExtra;
55945 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
55946 pCache->bPurgeable = (bPurgeable ? 1 : 0);
55949 pCache->nMin = 10;
55950 pGroup->nMinPage += pCache->nMin;
55951 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55952 pCache->pnPurgeable = &pGroup->nPurgeable;
55954 pCache->pnPurgeable = &pCache->nPurgeableDummy;
55957 if( pCache->nHash==0 ){
55970 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
55974 if( pCache->bPurgeable ){
55975 PGroup *pGroup = pCache->pGroup;
55978 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
55979 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
55981 pGroup->nMaxPage += (n - pCache->nMax);
55982 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55983 pCache->nMax = n;
55984 pCache->n90pct = pCache->nMax*9/10;
55997 if( pCache->bPurgeable ){
55998 PGroup *pGroup = pCache->pGroup;
55999 unsigned int savedMaxPage;
56001 savedMaxPage = pGroup->nMaxPage;
56002 pGroup->nMaxPage = 0;
56004 pGroup->nMaxPage = savedMaxPage;
56012 static int pcache1Pagecount(sqlite3_pcache *p){
56013 int n;
56015 pcache1EnterMutex(pCache->pGroup);
56016 n = pCache->nPage;
56017 pcache1LeaveMutex(pCache->pGroup);
56026 ** This steps are broken out into a separate procedure because they are
56032 unsigned int iKey,
56033 int createFlag
56035 unsigned int nPinned;
56036 PGroup *pGroup = pCache->pGroup;
56040 assert( pCache->nPage >= pCache->nRecyclable );
56041 nPinned = pCache->nPage - pCache->nRecyclable;
56042 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
56043 assert( pCache->n90pct == pCache->nMax*9/10 );
56045 nPinned>=pGroup->mxPinned
56046 || nPinned>=pCache->n90pct
56047 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
56052 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
56053 assert( pCache->nHash>0 && pCache->apHash );
56056 if( pCache->bPurgeable
56057 && !pGroup->lru.pLruPrev->isAnchor
56058 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
56061 pPage = pGroup->lru.pLruPrev;
56065 pOther = pPage->pCache;
56066 if( pOther->szAlloc != pCache->szAlloc ){
56070 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
56082 unsigned int h = iKey % pCache->nHash;
56083 pCache->nPage++;
56084 pPage->iKey = iKey;
56085 pPage->pNext = pCache->apHash[h];
56086 pPage->pCache = pCache;
56087 pPage->pLruNext = 0;
56088 /* pPage->pLruPrev = 0;
56090 *(void **)pPage->page.pExtra = 0;
56091 pCache->apHash[h] = pPage;
56092 if( iKey>pCache->iMaxKey ){
56093 pCache->iMaxKey = iKey;
56109 ** For a non-purgeable cache (a cache used as the storage for an in-memory
56112 ** a non-purgeable cache.
56155 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
56160 unsigned int iKey,
56161 int createFlag
56167 pPage = pCache->apHash[iKey % pCache->nHash];
56168 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
56190 unsigned int iKey,
56191 int createFlag
56196 pcache1EnterMutex(pCache->pGroup);
56198 assert( pPage==0 || pCache->iMaxKey>=iKey );
56199 pcache1LeaveMutex(pCache->pGroup);
56205 unsigned int iKey,
56206 int createFlag
56213 assert( pCache->bPurgeable || createFlag!=1 );
56214 assert( pCache->bPurgeable || pCache->nMin==0 );
56215 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
56216 assert( pCache->nMin==0 || pCache->bPurgeable );
56217 assert( pCache->nHash>0 );
56219 if( pCache->pGroup->mutex ){
56237 int reuseUnlikely
56241 PGroup *pGroup = pCache->pGroup;
56243 assert( pPage->pCache==pCache );
56249 assert( pPage->pLruNext==0 );
56252 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
56256 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
56257 pPage->pLruPrev = &pGroup->lru;
56258 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
56260 pCache->nRecyclable++;
56263 pcache1LeaveMutex(pCache->pGroup);
56272 unsigned int iOld,
56273 unsigned int iNew
56278 unsigned int hOld, hNew;
56279 assert( pPage->iKey==iOld );
56280 assert( pPage->pCache==pCache );
56283 pcache1EnterMutex(pCache->pGroup);
56286 hOld = iOld%pCache->nHash;
56287 pp = &pCache->apHash[hOld];
56289 pp = &(*pp)->pNext;
56291 *pp = pPage->pNext;
56294 hNew = iNew%pCache->nHash;
56295 pPage->iKey = iNew;
56296 pPage->pNext = pCache->apHash[hNew];
56297 pCache->apHash[hNew] = pPage;
56298 if( iNew>pCache->iMaxKey ){
56299 pCache->iMaxKey = iNew;
56302 pcache1LeaveMutex(pCache->pGroup);
56312 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
56314 pcache1EnterMutex(pCache->pGroup);
56315 if( iLimit<=pCache->iMaxKey ){
56317 pCache->iMaxKey = iLimit-1;
56319 pcache1LeaveMutex(pCache->pGroup);
56329 PGroup *pGroup = pCache->pGroup;
56330 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
56332 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
56333 assert( pGroup->nMaxPage >= pCache->nMax );
56334 pGroup->nMaxPage -= pCache->nMax;
56335 assert( pGroup->nMinPage >= pCache->nMin );
56336 pGroup->nMinPage -= pCache->nMin;
56337 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56340 sqlite3_free(pCache->pBulk);
56341 sqlite3_free(pCache->apHash);
56372 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
56392 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
56393 int nFree = 0;
56401 && p->isAnchor==0
56403 nFree += pcache1MemSize(p->page.pBuf);
56420 int *pnCurrent, /* OUT: Total number of pages cached */
56421 int *pnMax, /* OUT: Global maximum cache size */
56422 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
56423 int *pnRecyclable /* OUT: Total number of pages available for recycling */
56426 int nRecyclable = 0;
56427 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
56432 *pnMax = (int)pcache1.grp.nMaxPage;
56433 *pnMin = (int)pcache1.grp.nMinPage;
56487 ** a non-zero batch number, it will see all prior INSERTs.
56515 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
56556 int iBatch; /* Current insert batch */
56572 int N = sqlite3DbMallocSize(db, p);
56573 p->pChunk = 0;
56574 p->db = db;
56575 p->pEntry = 0;
56576 p->pLast = 0;
56577 p->pForest = 0;
56578 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
56579 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
56580 p->rsFlags = ROWSET_SORTED;
56581 p->iBatch = 0;
56594 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
56595 pNextChunk = pChunk->pNextChunk;
56596 sqlite3DbFree(p->db, pChunk);
56598 p->pChunk = 0;
56599 p->nFresh = 0;
56600 p->pEntry = 0;
56601 p->pLast = 0;
56602 p->pForest = 0;
56603 p->rsFlags = ROWSET_SORTED;
56613 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
56621 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
56626 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
56630 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
56634 pNew->pNextChunk = p->pChunk;
56635 p->pChunk = pNew;
56636 p->pFresh = pNew->aEntry;
56637 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
56639 p->nFresh--;
56640 return p->pFresh++;
56654 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
56658 pEntry->v = rowid;
56659 pEntry->pRight = 0;
56660 pLast = p->pLast;
56662 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
56665 p->rsFlags &= ~ROWSET_SORTED;
56667 pLast->pRight = pEntry;
56669 p->pEntry = pEntry;
56671 p->pLast = pEntry;
56690 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
56691 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
56692 if( pA->v<=pB->v ){
56693 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
56694 pA = pA->pRight;
56696 pTail->pRight = pB;
56700 pTail = pTail->pRight = pB;
56701 pB = pB->pRight;
56703 pTail->pRight = pA;
56716 unsigned int i;
56721 pNext = pIn->pRight;
56722 pIn->pRight = 0;
56750 if( pIn->pLeft ){
56752 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
56753 p->pRight = pIn;
56757 if( pIn->pRight ){
56758 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
56762 assert( (*ppLast)->pRight==0 );
56781 int iDepth
56785 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
56789 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
56793 pLeft = rowSetNDeepTree(ppList, iDepth-1);
56795 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
56800 p->pLeft = pLeft;
56801 *ppList = p->pRight;
56802 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
56805 *ppList = p->pRight;
56806 p->pLeft = p->pRight = 0;
56816 int iDepth; /* Depth of the tree so far */
56822 pList = p->pRight;
56823 p->pLeft = p->pRight = 0;
56827 pList = p->pRight;
56828 p->pLeft = pLeft;
56829 p->pRight = rowSetNDeepTree(&pList, iDepth);
56847 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
56849 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
56852 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56853 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56854 p->pEntry = rowSetEntrySort(p->pEntry);
56856 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
56860 if( p->pEntry ){
56861 *pRowid = p->pEntry->v;
56862 p->pEntry = p->pEntry->pRight;
56863 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
56878 ** on pRowSet->pEntry, then sort those entries into the forest at
56879 ** pRowSet->pForest so that they can be tested.
56881 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
56885 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
56890 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
56891 p = pRowSet->pEntry;
56893 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
56894 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56898 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56899 ppPrevTree = &pTree->pRight;
56900 if( pTree->pLeft==0 ){
56901 pTree->pLeft = rowSetListToTree(p);
56905 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
56906 pTree->pLeft = 0;
56913 pTree->v = 0;
56914 pTree->pRight = 0;
56915 pTree->pLeft = rowSetListToTree(p);
56918 pRowSet->pEntry = 0;
56919 pRowSet->pLast = 0;
56920 pRowSet->rsFlags |= ROWSET_SORTED;
56922 pRowSet->iBatch = iBatch;
56928 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56929 p = pTree->pLeft;
56931 if( p->v<iRowid ){
56932 p = p->pRight;
56933 }else if( p->v>iRowid ){
56934 p = p->pLeft;
56980 ** This header file defines the interface to the write-ahead logging
57021 /* Connection to a write-ahead log (WAL) file.
57026 /* Open and close a connection to a write-ahead log. */
57027 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
57028 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
57034 ** snapshot is like a read-transaction. It is the state of the database
57040 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
57043 /* Read a page from the write-ahead log, if it is present. */
57044 SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
57045 SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
57051 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
57052 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
57055 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
57063 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
57066 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
57069 SQLITE_PRIVATE int sqlite3WalCheckpoint(
57070 Wal *pWal, /* Write-ahead log connection */
57072 int eMode, /* One of PASSIVE, FULL and RESTART */
57073 int (*xBusy)(void*), /* Function to call when busy */
57075 int sync_flags, /* Flags to sync db file with (or 0) */
57076 int nBuf, /* Size of buffer nBuf */
57078 int *pnLog, /* OUT: Number of frames in WAL */
57079 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
57087 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
57092 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
57094 /* Return true if the argument is non-NULL and the WAL module is using
57095 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
57096 ** WAL module is using shared-memory, return false.
57098 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
57101 SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
57103 SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
57104 SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
57110 ** stored in each frame (i.e. the db page-size when the WAL was created).
57112 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
57119 SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
57124 SQLITE_PRIVATE int sqlite3WalSystemErrno(Wal*);
57181 ** (6) If a super-journal file is used, then all writes to the database file
57182 ** are synced prior to the super-journal being deleted.
57198 ** method is a no-op, but that does not change the fact the SQLite will
57209 ** (11) A database file is well-formed at the beginning and at the conclusion
57224 int sqlite3PagerTrace=1; /* True to enable tracing */
57233 ** to print out file-descriptors.
57236 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
57239 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
57247 ** OPEN <------+------+
57250 ** +---------> READER-------+ |
57253 ** |<-------WRITER_LOCKED------> ERROR
57256 ** |<------WRITER_CACHEMOD-------->|
57259 ** |<-------WRITER_DBMOD---------->|
57262 ** +<------WRITER_FINISHED-------->+
57267 ** OPEN -> READER [sqlite3PagerSharedLock]
57268 ** READER -> OPEN [pager_unlock]
57270 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
57271 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
57272 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
57273 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
57274 ** WRITER_*** -> READER [pager_end_transaction]
57276 ** WRITER_*** -> ERROR [pager_error]
57277 ** ERROR -> OPEN [pager_unlock]
57283 ** state - the file may or may not be locked and the database size is
57293 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
57294 ** was) in exclusive-locking mode, a user-level read transaction is
57298 ** it opens a read-transaction on the database and returns to state
57299 ** OPEN after the read-transaction is completed. However a connection
57301 ** this state even after the read-transaction is closed. The only way
57305 ** * A read transaction may be active (but a write-transaction cannot).
57307 ** * The dbSize variable may be trusted (even if a user-level read
57311 ** * Even if a read-transaction is not open, it is guaranteed that
57312 ** there is no hot-journal in the file-system.
57316 ** The pager moves to this state from READER when a write-transaction
57318 ** required to start a write-transaction are held, but no actual
57333 ** * If the connection is open in rollback-mode, a RESERVED or greater
57335 ** * If the connection is open in WAL-mode, a WAL write transaction
57374 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
57390 ** The ERROR state is entered when an IO or disk-full error (including
57392 ** difficult to be sure that the in-memory pager state (cache contents,
57393 ** db size etc.) are consistent with the contents of the file-system.
57395 ** Temporary pager files may enter the ERROR state, but in-memory pagers
57399 ** the contents of the page-cache may be left in an inconsistent state.
57411 ** page-cache and any other in-memory state at the same time. Everything
57412 ** is reloaded from disk (and, if necessary, hot-journal rollback performed)
57413 ** when a read-transaction is next opened on the pager (transitioning
57429 ** In other cases, the error is returned to the b-tree layer. The b-tree
57433 ** Condition (3) is necessary because it can be triggered by a read-only
57435 ** code were simply returned to the user, the b-tree layer would not
57437 ** read-only statement cannot leave the pager in an internally inconsistent
57443 ** * The pager is not an in-memory pager.
57453 ** state. There are two exceptions: immediately after exclusive-mode has
57469 ** following locking-states, according to the lock currently held on
57477 ** pagerUnlockDb() take a conservative approach - eLock is always updated
57488 ** from ERROR to OPEN state. At this point there may be a hot-journal file
57489 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
57493 ** of hot-journal detection.
57499 ** a hot-journal may be mistaken for a journal being created by an active
57506 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
57507 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
57508 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
57533 ** set to 0. If a journal-header is written into the main journal while
57536 ** journal before the journal-header. This is required during savepoint
57545 Pgno iSubRec; /* Index of first record in sub-journal */
57546 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
57570 ** For a real on-disk database, the current lock held on the database file -
57573 ** For a temporary or in-memory database (neither of which require any
57585 ** This boolean variable is used to make sure that the change-counter
57586 ** (the 4-byte header field at byte offset 24 of the database file) is
57589 ** It is set to true when the change-counter field is updated, which
57594 ** updating the change-counter is omitted for the current transaction.
57597 ** need only update the change-counter once, for the first transaction
57603 ** (or may not) specify a super-journal name to be written into the
57606 ** Whether or not a journal file contains a super-journal pointer affects
57609 ** If a journal file does not contain a super-journal pointer, it is
57611 ** it does contain a super-journal pointer the journal file is finalized
57615 ** Journal files that contain super-journal pointers cannot be finalized
57616 ** simply by overwriting the first journal-header with zeroes, as the
57617 ** super-journal pointer could interfere with hot-journal rollback of any
57627 ** This variables control the behavior of cache-spills (calls made by
57629 ** to the file-system in order to free up memory).
57641 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
57642 ** is larger than the database page-size in order to prevent a journal sync
57647 ** This is a boolean variable. If true, then any required sub-journal
57648 ** is opened as an in-memory journal file. If false, then in-memory
57649 ** sub-journals are only used for in-memory pager files.
57652 ** write-transaction is opened.
57663 ** is not an integer multiple of the page-size, the value stored in
57664 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
57666 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
57669 ** During a write-transaction, if pages with page-numbers greater than
57680 ** Throughout a write-transaction, dbFileSize contains the size of
57682 ** write-transaction is first opened, and updated when VFS calls are made
57702 ** write-transaction is opened (at the same time as dbFileSize and
57705 ** size-hint passed to the method call. See pager_write_pagelist() for
57713 ** sub-codes.
57739 u8 readOnly; /* True for a read-only database */
57741 u8 memVfs; /* VFS-implemented memory database */
57754 u8 changeCountDone; /* Set after incrementing the change-counter */
57755 u8 setSuper; /* Super-jrnl name is written into jrnl */
57756 u8 doNotSpill; /* Do not spill the cache when non-zero */
57757 u8 subjInMemory; /* True to use in-memory sub-journals */
57764 int errCode; /* One of several kinds of errors */
57765 int nRec; /* Pages journalled since last j-header written */
57766 u32 cksumInit; /* Quasi-random value added to every checksum */
57767 u32 nSubRec; /* Number of records written to sub-journal */
57771 sqlite3_file *sjfd; /* File descriptor for sub-journal */
57776 int nSavepoint; /* Number of elements in aSavepoint[] */
57780 int nMmapOut; /* Number of mmap pages currently outstanding */
57784 ** End of the routinely-changing class members
57787 u16 nExtra; /* Add this many bytes to each in-memory page */
57797 int (*xBusyHandler)(void*); /* Function to call when busy */
57801 int nRead; /* Database pages read */
57804 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
57808 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
57809 char *zWal; /* File name for write-ahead log */
57826 ** a non-testing build. These variables are not thread-safe.
57829 SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
57830 SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
57831 SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
57845 ** written, semi-random garbage data might appear in the journal
57852 ** of a 32-bit checksum on each page of data. The checksum covers both
57853 ** the page number and the pPager->pageSize bytes of data for the page.
57854 ** This cksum is initialized to a 32-bit random value that appears in the
57870 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
57876 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
57879 ** The macro MEMDB is true if we are dealing with an in-memory database.
57887 # define MEMDB pPager->memDb
57892 ** interfaces to access the database using memory-mapped I/O.
57895 # define USEFETCH(x) ((x)->bUseFetch)
57902 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
57906 ** if( isOpen(pPager->jfd) ){ ...
57910 ** if( pPager->jfd->pMethods ){ ...
57912 #define isOpen(pFd) ((pFd)->pMethods!=0)
57917 ** by the b-tree layer. This is the case if:
57923 SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
57924 if( pPager->fd->pMethods==0 ) return 0;
57925 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
57927 if( pPager->pWal ){
57929 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
57938 # define pagerUseWal(x) ((x)->pWal!=0)
57956 static int assert_pager_state(Pager *p){
57960 assert( p->eState==PAGER_OPEN
57961 || p->eState==PAGER_READER
57962 || p->eState==PAGER_WRITER_LOCKED
57963 || p->eState==PAGER_WRITER_CACHEMOD
57964 || p->eState==PAGER_WRITER_DBMOD
57965 || p->eState==PAGER_WRITER_FINISHED
57966 || p->eState==PAGER_ERROR
57969 /* Regardless of the current state, a temp-file connection always behaves
57971 ** the change-counter field, so the changeCountDone flag is always set.
57973 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
57974 assert( p->tempFile==0 || pPager->changeCountDone );
57976 /* If the useJournal flag is clear, the journal-mode must be "OFF".
57977 ** And if the journal-mode is "OFF", the journal file must not be open.
57979 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
57980 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
57982 /* Check that MEMDB implies noSync. And an in-memory journal. Since
57983 ** this means an in-memory pager performs no IO at all, it cannot encounter
57985 ** a journal file. (although the in-memory journal implementation may
57987 ** is therefore not possible for an in-memory pager to enter the ERROR
57991 assert( !isOpen(p->fd) );
57992 assert( p->noSync );
57993 assert( p->journalMode==PAGER_JOURNALMODE_OFF
57994 || p->journalMode==PAGER_JOURNALMODE_MEMORY
57996 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
58003 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
58004 assert( p->eLock!=PENDING_LOCK );
58006 switch( p->eState ){
58009 assert( pPager->errCode==SQLITE_OK );
58010 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
58014 assert( pPager->errCode==SQLITE_OK );
58015 assert( p->eLock!=UNKNOWN_LOCK );
58016 assert( p->eLock>=SHARED_LOCK );
58020 assert( p->eLock!=UNKNOWN_LOCK );
58021 assert( pPager->errCode==SQLITE_OK );
58023 assert( p->eLock>=RESERVED_LOCK );
58025 assert( pPager->dbSize==pPager->dbOrigSize );
58026 assert( pPager->dbOrigSize==pPager->dbFileSize );
58027 assert( pPager->dbOrigSize==pPager->dbHintSize );
58028 assert( pPager->setSuper==0 );
58032 assert( p->eLock!=UNKNOWN_LOCK );
58033 assert( pPager->errCode==SQLITE_OK );
58040 assert( p->eLock>=RESERVED_LOCK );
58041 assert( isOpen(p->jfd)
58042 || p->journalMode==PAGER_JOURNALMODE_OFF
58043 || p->journalMode==PAGER_JOURNALMODE_WAL
58046 assert( pPager->dbOrigSize==pPager->dbFileSize );
58047 assert( pPager->dbOrigSize==pPager->dbHintSize );
58051 assert( p->eLock==EXCLUSIVE_LOCK );
58052 assert( pPager->errCode==SQLITE_OK );
58054 assert( p->eLock>=EXCLUSIVE_LOCK );
58055 assert( isOpen(p->jfd)
58056 || p->journalMode==PAGER_JOURNALMODE_OFF
58057 || p->journalMode==PAGER_JOURNALMODE_WAL
58058 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58060 assert( pPager->dbOrigSize<=pPager->dbHintSize );
58064 assert( p->eLock==EXCLUSIVE_LOCK );
58065 assert( pPager->errCode==SQLITE_OK );
58067 assert( isOpen(p->jfd)
58068 || p->journalMode==PAGER_JOURNALMODE_OFF
58069 || p->journalMode==PAGER_JOURNALMODE_WAL
58070 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58079 assert( pPager->errCode!=SQLITE_OK );
58080 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
58113 , p->zFilename
58114 , p->eState==PAGER_OPEN ? "OPEN" :
58115 p->eState==PAGER_READER ? "READER" :
58116 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
58117 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
58118 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
58119 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
58120 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
58121 , (int)p->errCode
58122 , p->eLock==NO_LOCK ? "NO_LOCK" :
58123 p->eLock==RESERVED_LOCK ? "RESERVED" :
58124 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
58125 p->eLock==SHARED_LOCK ? "SHARED" :
58126 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
58127 , p->exclusiveMode ? "exclusive" : "normal"
58128 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
58129 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
58130 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
58131 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
58132 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
58133 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
58134 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
58135 , p->journalOff, p->journalHdr
58136 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
58144 static int getPageNormal(Pager*,Pgno,DbPage**,int);
58145 static int getPageError(Pager*,Pgno,DbPage**,int);
58147 static int getPageMMap(Pager*,Pgno,DbPage**,int);
58155 if( pPager->errCode ){
58156 pPager->xGet = getPageError;
58159 pPager->xGet = getPageMMap;
58162 pPager->xGet = getPageNormal;
58167 ** Return true if it is necessary to write page *pPg into the sub-journal.
58168 ** A page needs to be written into the sub-journal if there exists one
58171 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
58172 ** * The bit corresponding to the page-number is not set in
58175 static int subjRequiresPage(PgHdr *pPg){
58176 Pager *pPager = pPg->pPager;
58178 Pgno pgno = pPg->pgno;
58179 int i;
58180 for(i=0; i<pPager->nSavepoint; i++){
58181 p = &pPager->aSavepoint[i];
58182 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
58183 for(i=i+1; i<pPager->nSavepoint; i++){
58184 pPager->aSavepoint[i].bTruncateOnRelease = 0;
58196 static int pageInJournal(Pager *pPager, PgHdr *pPg){
58197 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
58202 ** Read a 32-bit integer from the given file descriptor. Store the integer
58206 ** All values are stored on disk as big-endian.
58208 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
58210 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
58218 ** Write a 32-bit integer into a string buffer in big-endian byte order.
58224 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
58227 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
58242 static int pagerUnlockDb(Pager *pPager, int eLock){
58243 int rc = SQLITE_OK;
58245 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
58248 if( isOpen(pPager->fd) ){
58249 assert( pPager->eLock>=eLock );
58250 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
58251 if( pPager->eLock!=UNKNOWN_LOCK ){
58252 pPager->eLock = (u8)eLock;
58256 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
58270 static int pagerLockDb(Pager *pPager, int eLock){
58271 int rc = SQLITE_OK;
58274 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
58275 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
58276 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
58277 pPager->eLock = (u8)eLock;
58285 ** This function determines whether or not the atomic-write or
58286 ** atomic-batch-write optimizations can be used with this pager. The
58287 ** atomic-write optimization can be used if:
58297 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
58298 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
58303 static int jrnlBufferSize(Pager *pPager){
58308 int dc; /* Device characteristics */
58310 assert( isOpen(pPager->fd) );
58311 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
58317 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
58318 return -1;
58324 int nSector = pPager->sectorSize;
58325 int szPage = pPager->pageSize;
58347 ** Return a 32-bit hash of the page data for pPage.
58349 static u32 pager_datahash(int nByte, unsigned char *pData){
58351 int i;
58358 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
58361 pPage->pageHash = pager_pagehash(pPage);
58367 ** that the page is either dirty or still matches the calculated page-hash.
58371 Pager *pPager = pPg->pPager;
58372 assert( pPager->eState!=PAGER_ERROR );
58373 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
58385 ** This function attempts to read a super-journal file name from the
58388 ** used to store a super-journal file name at the end of a journal file.
58392 ** enough space to write the super-journal name). If the super-journal
58394 ** nul-terminator), then this is handled as if no super-journal name
58397 ** If a super-journal file name is present at the end of the journal
58399 ** nul-terminator byte is appended to the buffer following the
58400 ** super-journal file name.
58402 ** If it is determined that no super-journal file name is present
58408 static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
58409 int rc; /* Return code */
58410 u32 len; /* Length in bytes of super-journal name */
58419 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
58421 || len>szJ-16
58423 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
58424 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
58426 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
58431 /* See if the checksum matches the super-journal name */
58433 cksum -= zSuper[u];
58437 ** containing the super-journal filename is corrupted. This means
58439 ** super-journal filename.
58451 ** following the value in pPager->journalOff, assuming a sector
58452 ** size of pPager->sectorSize bytes.
58457 ** ---------------------------------------
58466 i64 c = pPager->journalOff;
58468 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
58472 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
58479 ** This function is a no-op if the journal file has not been written to
58482 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
58484 ** zero the 28-byte header at the start of the journal file. In either case,
58485 ** if the pager is not in no-sync mode, sync the journal file immediately
58488 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
58497 static int zeroJournalHdr(Pager *pPager, int doTruncate){
58498 int rc = SQLITE_OK; /* Return code */
58499 assert( isOpen(pPager->jfd) );
58500 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
58501 if( pPager->journalOff ){
58502 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
58506 rc = sqlite3OsTruncate(pPager->jfd, 0);
58509 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
58511 if( rc==SQLITE_OK && !pPager->noSync ){
58512 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
58523 rc = sqlite3OsFileSize(pPager->jfd, &sz);
58525 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
58538 ** - 8 bytes: Magic identifying journal format.
58539 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
58540 ** - 4 bytes: Random number used for page hash.
58541 ** - 4 bytes: Initial database page count.
58542 ** - 4 bytes: Sector size used by the process that wrote this journal.
58543 ** - 4 bytes: Database page size.
58545 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
58547 static int writeJournalHdr(Pager *pPager){
58548 int rc = SQLITE_OK; /* Return code */
58549 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
58550 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
58552 int ii; /* Loop counter */
58554 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
58564 for(ii=0; ii<pPager->nSavepoint; ii++){
58565 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
58566 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
58570 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
58573 ** Write the nRec Field - the number of page records that follow this
58576 ** if in full-sync mode), the zero is overwritten with the true number
58586 ** * When the pager is in no-sync mode. Corruption can follow a
58592 assert( isOpen(pPager->fd) || pPager->noSync );
58593 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
58594 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
58604 /* The random check-hash initializer */
58605 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58606 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58620 sqlite3OsFileSize(pPager->jfd, &sz);
58622 assert( pPager->journalOff==journalHdrOffset(pPager) );
58623 assert( sqlite3JournalIsInMemory(pPager->jfd) );
58626 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
58629 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
58631 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
58634 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
58642 nHeader-(sizeof(aJournalMagic)+20));
58653 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
58656 ** The loop is required here in case the sector-size is larger than the
58662 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
58663 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
58664 assert( pPager->journalHdr <= pPager->journalOff );
58665 pPager->journalOff += nHeader;
58675 ** pPager->journalOff. See comments above function writeJournalHdr() for
58680 ** database before the transaction began, in pages. Also, pPager->cksumInit
58688 static int readJournalHdr(
58690 int isHot,
58695 int rc; /* Return code */
58699 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
58705 pPager->journalOff = journalHdrOffset(pPager);
58706 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
58709 iHdrOff = pPager->journalOff;
58716 if( isHot || iHdrOff!=pPager->journalHdr ){
58717 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
58726 /* Read the first three 32-bit fields of the journal header: The nRec
58727 ** field, the checksum-initializer and the database size at the start
58730 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
58731 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
58732 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
58737 if( pPager->journalOff==0 ){
58738 u32 iPageSize; /* Page-size field of journal header */
58739 u32 iSectorSize; /* Sector-size field of journal header */
58741 /* Read the page-size and sector-size journal header fields. */
58742 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
58743 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
58748 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
58753 iPageSize = pPager->pageSize;
58756 /* Check that the values read from the page-size and sector-size fields
58763 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
58765 /* If the either the page-size or sector-size in the journal-header is
58766 ** invalid, then the process that wrote the journal-header must have
58773 /* Update the page-size to match the value read from the journal.
58777 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
58780 /* Update the assumed sector-size to match the value used by
58786 pPager->sectorSize = iSectorSize;
58789 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
58795 ** Write the supplied super-journal name into the journal file for pager
58796 ** pPager at the current location. The super-journal name must be the last
58797 ** thing written to a journal file. If the pager is in full-sync mode, the
58802 ** + N bytes: super-journal filename in utf-8.
58803 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
58804 ** + 4 bytes: super-journal name checksum.
58807 ** The super-journal page checksum is the sum of the bytes in the super-journal
58808 ** name, where each byte is interpreted as a signed 8-bit integer.
58811 ** this call is a no-op.
58813 static int writeSuperJournal(Pager *pPager, const char *zSuper){
58814 int rc; /* Return code */
58815 int nSuper; /* Length of string zSuper */
58820 assert( pPager->setSuper==0 );
58824 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58825 || !isOpen(pPager->jfd)
58829 pPager->setSuper = 1;
58830 assert( pPager->journalHdr <= pPager->journalOff );
58837 /* If in full-sync mode, advance to the next disk sector before writing
58838 ** the super-journal name. This is in case the previous page written to
58841 if( pPager->fullSync ){
58842 pPager->journalOff = journalHdrOffset(pPager);
58844 iHdrOff = pPager->journalOff;
58846 /* Write the super-journal data to the end of the journal file. If
58849 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
58850 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
58851 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
58852 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
58853 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
58858 pPager->journalOff += (nSuper+20);
58860 /* If the pager is in persistent-journal mode, then the physical
58861 ** journal-file may extend past the end of the super-journal name
58863 ** dangerous because the code to rollback a hot-journal file
58864 ** will not be able to find the super-journal name to determine
58870 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
58871 && jrnlSize>pPager->journalOff
58873 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
58879 ** Discard the entire contents of the in-memory page-cache.
58882 pPager->iDataVersion++;
58883 sqlite3BackupRestart(pPager->pBackup);
58884 sqlite3PcacheClear(pPager->pPCache);
58888 ** Return the pPager->iDataVersion value
58891 return pPager->iDataVersion;
58896 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
58900 int ii; /* Iterator for looping through Pager.aSavepoint */
58901 for(ii=0; ii<pPager->nSavepoint; ii++){
58902 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
58904 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
58905 sqlite3OsClose(pPager->sjfd);
58907 sqlite3_free(pPager->aSavepoint);
58908 pPager->aSavepoint = 0;
58909 pPager->nSavepoint = 0;
58910 pPager->nSubRec = 0;
58918 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
58919 int ii; /* Loop counter */
58920 int rc = SQLITE_OK; /* Result code */
58922 for(ii=0; ii<pPager->nSavepoint; ii++){
58923 PagerSavepoint *p = &pPager->aSavepoint[ii];
58924 if( pgno<=p->nOrig ){
58925 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
58934 ** This function is a no-op if the pager is in exclusive mode and not
58938 ** If the pager is not in exclusive-access mode, the database file is
58939 ** completely unlocked. If the file is unlocked and the file-system does
58945 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
58946 ** or not, any journal file left in the file-system will be treated
58947 ** as a hot-journal and rolled back the next time a read-transaction
58952 assert( pPager->eState==PAGER_READER
58953 || pPager->eState==PAGER_OPEN
58954 || pPager->eState==PAGER_ERROR
58957 sqlite3BitvecDestroy(pPager->pInJournal);
58958 pPager->pInJournal = 0;
58962 assert( !isOpen(pPager->jfd) );
58963 sqlite3WalEndReadTransaction(pPager->pWal);
58964 pPager->eState = PAGER_OPEN;
58965 }else if( !pPager->exclusiveMode ){
58966 int rc; /* Error code returned by pagerUnlockDb() */
58967 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
58981 || 1!=(pPager->journalMode & 5)
58983 sqlite3OsClose(pPager->jfd);
58992 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
58993 pPager->eLock = UNKNOWN_LOCK;
58997 ** without clearing the error code. This is intentional - the error
59000 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
59001 pPager->eState = PAGER_OPEN;
59007 ** normal and exclusive-locking mode.
59009 assert( pPager->errCode==SQLITE_OK || !MEMDB );
59010 if( pPager->errCode ){
59011 if( pPager->tempFile==0 ){
59013 pPager->changeCountDone = 0;
59014 pPager->eState = PAGER_OPEN;
59016 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
59018 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59019 pPager->errCode = SQLITE_OK;
59023 pPager->journalOff = 0;
59024 pPager->journalHdr = 0;
59025 pPager->setSuper = 0;
59032 ** the error-code about to be returned by a pager API function. The
59036 ** IOERR sub-codes, the pager enters the ERROR state and the error code
59040 ** The ERROR state indicates that the contents of the pager-cache
59042 ** the contents of the pager-cache. If a transaction was active when
59045 ** it were a hot-journal).
59047 static int pager_error(Pager *pPager, int rc){
59048 int rc2 = rc & 0xff;
59051 pPager->errCode==SQLITE_FULL ||
59052 pPager->errCode==SQLITE_OK ||
59053 (pPager->errCode & 0xff)==SQLITE_IOERR
59056 pPager->errCode = rc;
59057 pPager->eState = PAGER_ERROR;
59063 static int pager_truncate(Pager *pPager, Pgno nPage);
59073 ** * For non-TEMP databases, always sync to disk. This is necessary
59081 static int pagerFlushOnCommit(Pager *pPager, int bCommit){
59082 if( pPager->tempFile==0 ) return 1;
59084 if( !isOpen(pPager->fd) ) return 0;
59085 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
59091 ** after rollback of a hot-journal, or if an error occurs while opening
59092 ** the journal file or writing the very first journal-header of a
59097 ** exclusive than a RESERVED lock, it is a no-op.
59103 ** transaction. Nor will it be considered to be a hot-journal by this
59106 ** the current journal-mode (Pager.journalMode value), as follows:
59110 ** in-memory journal.
59129 ** If running in non-exclusive rollback mode, the lock on the file is
59141 static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
59142 int rc = SQLITE_OK; /* Error code from journal finalization operation */
59143 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
59147 ** is no write-transaction active but a RESERVED or greater lock is
59150 ** 1. After a successful hot-journal rollback, it is called with
59155 ** read-transaction, this function is called with eState==PAGER_READER
59156 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
59159 assert( pPager->eState!=PAGER_ERROR );
59160 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
59165 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
59166 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
59168 if( isOpen(pPager->jfd) ){
59172 if( sqlite3JournalIsInMemory(pPager->jfd) ){
59173 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
59174 sqlite3OsClose(pPager->jfd);
59175 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
59176 if( pPager->journalOff==0 ){
59179 rc = sqlite3OsTruncate(pPager->jfd, 0);
59180 if( rc==SQLITE_OK && pPager->fullSync ){
59186 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
59189 pPager->journalOff = 0;
59190 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
59191 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
59193 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
59194 pPager->journalOff = 0;
59197 ** a hot-journal was just rolled back. In this case the journal
59199 ** the database file, it will do so using an in-memory journal.
59201 int bDelete = !pPager->tempFile;
59202 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
59203 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
59204 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
59205 || pPager->journalMode==PAGER_JOURNALMODE_WAL
59207 sqlite3OsClose(pPager->jfd);
59209 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
59215 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
59216 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
59219 p->pageHash = 0;
59225 sqlite3BitvecDestroy(pPager->pInJournal);
59226 pPager->pInJournal = 0;
59227 pPager->nRec = 0;
59230 sqlite3PcacheCleanAll(pPager->pPCache);
59232 sqlite3PcacheClearWritable(pPager->pPCache);
59234 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
59238 /* Drop the WAL write-lock, if any. Also, if the connection was in
59242 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
59244 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
59245 /* This branch is taken when committing a transaction in rollback-journal
59251 assert( pPager->eLock==EXCLUSIVE_LOCK );
59252 rc = pager_truncate(pPager, pPager->dbSize);
59256 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
59260 if( !pPager->exclusiveMode
59261 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
59265 pPager->eState = PAGER_READER;
59266 pPager->setSuper = 0;
59272 static int pager_playback(Pager *pPager, int isHot);
59280 ** call to pager_unlock() will discard all in-memory pages, unlock
59282 ** means that there is a hot-journal left in the file-system, the next
59292 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
59294 if( pPager->eState>=PAGER_WRITER_LOCKED ){
59298 }else if( !pPager->exclusiveMode ){
59299 assert( pPager->eState==PAGER_READER );
59302 }else if( pPager->eState==PAGER_ERROR
59303 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY
59304 && isOpen(pPager->jfd)
59306 /* Special case for a ROLLBACK due to I/O error with an in-memory
59309 int errCode = pPager->errCode;
59310 u8 eLock = pPager->eLock;
59311 pPager->eState = PAGER_OPEN;
59312 pPager->errCode = SQLITE_OK;
59313 pPager->eLock = EXCLUSIVE_LOCK;
59315 pPager->errCode = errCode;
59316 pPager->eLock = eLock;
59322 ** Parameter aData must point to a buffer of pPager->pageSize bytes
59324 ** page of data and the current value of pPager->cksumInit.
59327 ** random initial value (pPager->cksumInit) and every 200th byte
59328 ** of the page data, starting with byte offset (pPager->pageSize%200).
59329 ** Each byte is interpreted as an 8-bit unsigned integer.
59341 u32 cksum = pPager->cksumInit; /* Checksum value to return */
59342 int i = pPager->pageSize-200; /* Loop counter */
59345 i -= 200;
59352 ** from the sub-journal (if isMainJrnl==0) and playback that page.
59356 ** The main rollback journal uses checksums - the statement journal does
59359 ** If the page number of the page record read from the (sub-)journal file
59369 ** If the page record is successfully read from the (sub-)journal file
59371 ** while reading the record from the (sub-)journal file or while writing
59373 ** is successfully read from the (sub-)journal file but appears to be
59377 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
59387 static int pager_playback_one_page(
59391 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
59392 int isSavepnt /* True for a savepoint rollback */
59394 int rc;
59400 int isSynced; /* True if journal page is synced */
59404 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
59405 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
59407 aData = pPager->pTmpSpace;
59413 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
59414 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
59415 ** only reads from the main journal, not the sub-journal.
59417 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
59418 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
59420 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
59422 /* Read the page number and page data from the journal or sub-journal
59425 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
59428 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
59430 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
59441 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
59445 rc = read32bits(jfd, (*pOffset)-4, &cksum);
59461 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
59462 pPager->nReserve = ((u8*)aData)[20];
59469 ** An exception to the above rule: If the database is in no-sync mode
59479 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
59496 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
59506 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
59508 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
59509 (isMainJrnl?"main-journal":"sub-journal")
59512 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
59514 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
59516 if( isOpen(pPager->fd)
59517 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59520 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
59521 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
59525 ** This is usually safe even for an encrypted database - as the data
59527 ** is if the data was just read from an in-memory sub-journal. In that
59530 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
59532 if( pgno>pPager->dbFileSize ){
59533 pPager->dbFileSize = pgno;
59535 if( pPager->pBackup ){
59536 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
59540 ** the database and the page is not in-memory, there is a potential
59541 ** problem. When the page is next fetched by the b-tree layer, it
59547 ** if the page is on the free-list at the start of the transaction, then
59550 ** The solution is to add an in-memory page to the cache containing
59551 ** the data just read from the sub-journal. Mark the page as dirty
59552 ** and if the pager requires a journal-sync, then mark the page as
59553 ** requiring a journal-sync before it is written.
59556 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
59557 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
59559 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
59560 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
59572 pData = pPg->pData;
59573 memcpy(pData, (u8*)aData, pPager->pageSize);
59574 pPager->xReiniter(pPg);
59584 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
59592 ** Parameter zSuper is the name of a super-journal file. A single journal
59593 ** file that referred to the super-journal file has just been rolled back.
59594 ** This routine checks if it is possible to delete the super-journal file,
59600 ** When a super-journal file is created, it is populated with the names
59601 ** of all of its child journals, one after another, formatted as utf-8
59603 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
59606 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
59608 ** A super-journal file may only be deleted once all of its child
59611 ** This function reads the contents of the super-journal file into
59616 ** * if the child journal contains a reference to super-journal
59622 ** the file-system using sqlite3OsDelete().
59630 ** the entire contents of the super-journal file. This could be
59631 ** a couple of kilobytes or so - potentially larger than the page
59634 static int pager_delsuper(Pager *pPager, const char *zSuper){
59635 sqlite3_vfs *pVfs = pPager->pVfs;
59636 int rc; /* Return code */
59637 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
59638 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
59639 char *zSuperJournal = 0; /* Contents of super-journal file */
59640 i64 nSuperJournal; /* Size of super-journal file */
59642 char *zSuperPtr; /* Space to hold super-journal filename */
59644 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
59647 ** If successful, open the super-journal file for reading.
59649 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
59654 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
59656 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
59660 /* Load the entire super-journal file into space obtained from
59662 ** sufficient space (in zSuperPtr) to hold the names of super-journal
59663 ** files extracted from regular rollback-journals.
59667 nSuperPtr = pVfs->mxPathname+1;
59676 rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0);
59682 while( (zJournal-zSuperJournal)<nSuperJournal ){
59683 int exists;
59689 /* One of the journals pointed to by the super-journal exists.
59690 ** Open it and check if it points at the super-journal. If
59691 ** so, return without deleting the super-journal file.
59696 int c;
59697 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
59711 /* We have a match. Do not delete the super-journal file. */
59734 ** file in the file-system. This only happens when committing a transaction,
59735 ** or rolling back a transaction (including rolling back a hot-journal).
59738 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
59739 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
59752 static int pager_truncate(Pager *pPager, Pgno nPage){
59753 int rc = SQLITE_OK;
59754 assert( pPager->eState!=PAGER_ERROR );
59755 assert( pPager->eState!=PAGER_READER );
59759 if( isOpen(pPager->fd)
59760 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59763 int szPage = pPager->pageSize;
59764 assert( pPager->eLock==EXCLUSIVE_LOCK );
59766 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
59770 rc = sqlite3OsTruncate(pPager->fd, newSize);
59772 char *pTmp = pPager->pTmpSpace;
59774 testcase( (newSize-szPage) == currentSize );
59775 testcase( (newSize-szPage) > currentSize );
59776 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
59777 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
59780 pPager->dbFileSize = nPage;
59788 ** Return a sanitized version of the sector-size of OS file pFile. The
59791 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
59792 int iRet = sqlite3OsSectorSize(pFile);
59807 ** super-journal pointers within created journal files.
59811 ** Otherwise, for non-temporary files, the effective sector size is
59818 ** pPager->sectorSize is to define the "blast radius" of bytes that
59826 assert( isOpen(pPager->fd) || pPager->tempFile );
59828 if( pPager->tempFile
59829 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
59835 pPager->sectorSize = 512;
59837 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
59848 ** (2) 4 byte big-endian integer which is the number of valid page records
59851 ** (3) 4 byte big-endian integer which is the initial value for the
59855 ** (5) 4 byte big-endian integer which is the sector size. The header
59857 ** (6) 4 byte big-endian integer which is the page size.
59861 ** + pPager->pageSize bytes of data.
59878 ** no-sync option for the journal. A power failure could lead to corruption
59882 ** If the file opened as the journal file is not a well-formed
59888 ** If an I/O or malloc() error occurs, the journal-file is not deleted
59898 static int pager_playback(Pager *pPager, int isHot){
59899 sqlite3_vfs *pVfs = pPager->pVfs;
59904 int rc; /* Result code of a subroutine */
59905 int res = 1; /* Value returned by sqlite3OsAccess() */
59906 char *zSuper = 0; /* Name of super-journal file if any */
59907 int needPagerReset; /* True to reset page prior to first page rollback */
59908 int nPlayback = 0; /* Total number of pages restored from journal */
59909 u32 savedPageSize = pPager->pageSize;
59914 assert( isOpen(pPager->jfd) );
59915 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
59920 /* Read the super-journal name from the journal, if it is present.
59921 ** If a super-journal file name is specified, but the file is not
59927 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
59931 zSuper = pPager->pTmpSpace;
59932 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
59940 pPager->journalOff = 0;
59962 ** working in no-sync mode. This means that the rest of the journal
59967 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
59968 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
59986 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
59987 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
59993 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
59998 pPager->dbSize = mxPg;
59999 if( pPager->mxPgno<mxPg ){
60000 pPager->mxPgno = mxPg;
60012 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
60017 pPager->journalOff = szJ;
60043 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
60047 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
60051 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
60055 ** malloc error that occurred after the change-counter was updated but
60056 ** before the transaction was committed, then the change-counter
60059 ** update the change-counter at all. This may lead to cache inconsistency
60063 pPager->changeCountDone = pPager->tempFile;
60066 /* Leave 4 bytes of space before the super-journal filename in memory.
60070 zSuper = &pPager->pTmpSpace[4];
60071 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
60075 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
60084 /* If there was a super-journal and this routine will return success,
60085 ** see if it is possible to delete the super-journal.
60087 assert( zSuper==&pPager->pTmpSpace[4] );
60088 memset(pPager->pTmpSpace, 0, 4);
60094 nPlayback, pPager->zJournal);
60109 ** pPg->pData. A shared lock or greater must be held on the database
60118 static int readDbPage(PgHdr *pPg){
60119 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
60120 int rc = SQLITE_OK; /* Return code */
60125 assert( pPager->eState>=PAGER_READER && !MEMDB );
60126 assert( isOpen(pPager->fd) );
60129 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
60133 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
60137 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
60138 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
60144 if( pPg->pgno==1 ){
60151 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
60158 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
60160 u8 *dbFileVers = &((u8*)pPg->pData)[24];
60161 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
60165 PAGER_INCR(pPager->nRead);
60166 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
60168 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
60174 ** Update the value of the change-counter at offsets 24 and 92 in
60178 ** routine which only updates the change-counter if the update is actually
60179 ** needed, as determined by the pPager->changeCountDone state variable.
60186 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
60187 put32bits(((char*)pPg->pData)+24, change_counter);
60192 put32bits(((char*)pPg->pData)+92, change_counter);
60193 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
60209 static int pagerUndoCallback(void *pCtx, Pgno iPg){
60210 int rc = SQLITE_OK;
60222 pPager->xReiniter(pPg);
60236 sqlite3BackupRestart(pPager->pBackup);
60244 static int pagerRollbackWal(Pager *pPager){
60245 int rc; /* Return Code */
60255 pPager->dbSize = pPager->dbOrigSize;
60256 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
60257 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60259 PgHdr *pNext = pList->pDirty;
60260 rc = pagerUndoCallback((void *)pPager, pList->pgno);
60276 static int pagerWalFrames(
60280 int isCommit /* True if this is a commit */
60282 int rc; /* Return code */
60283 int nList; /* Number of pages in pList */
60286 assert( pPager->pWal );
60290 for(p=pList; p && p->pDirty; p=p->pDirty){
60291 assert( p->pgno < p->pDirty->pgno );
60295 assert( pList->pDirty==0 || isCommit );
60303 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
60304 if( p->pgno<=nTruncate ){
60305 ppNext = &p->pDirty;
60313 pPager->aStat[PAGER_STAT_WRITE] += nList;
60315 if( pList->pgno==1 ) pager_write_changecounter(pList);
60316 rc = sqlite3WalFrames(pPager->pWal,
60317 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
60319 if( rc==SQLITE_OK && pPager->pBackup ){
60320 for(p=pList; p; p=p->pDirty){
60321 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
60326 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60327 for(p=pList; p; p=p->pDirty){
60343 static int pagerBeginReadTransaction(Pager *pPager){
60344 int rc; /* Return code */
60345 int changed = 0; /* True if cache must be reset */
60348 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
60355 sqlite3WalEndReadTransaction(pPager->pWal);
60357 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
60360 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
60376 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
60379 /* Query the WAL sub-system for the database size. The WalDbsize()
60382 ** available from the WAL sub-system if the log file is empty or
60385 assert( pPager->eState==PAGER_OPEN );
60386 assert( pPager->eLock>=SHARED_LOCK );
60387 assert( isOpen(pPager->fd) );
60388 assert( pPager->tempFile==0 );
60389 nPage = sqlite3WalDbsize(pPager->pWal);
60392 ** WAL sub-system, determine the page count based on the size of
60394 ** integer multiple of the page-size, round up the result.
60396 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
60398 int rc = sqlite3OsFileSize(pPager->fd, &n);
60402 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
60409 if( nPage>pPager->mxPgno ){
60410 pPager->mxPgno = (Pgno)nPage;
60419 ** Check if the *-wal file that corresponds to the database opened by pPager
60420 ** exists if the database is not empty, or verify that the *-wal file does
60423 ** If the database is not empty and the *-wal file exists, open the pager
60424 ** in WAL mode. If the database is empty or if no *-wal file exists and
60432 ** a WAL on a none-empty database, this ensures there is no race condition
60436 static int pagerOpenWalIfPresent(Pager *pPager){
60437 int rc = SQLITE_OK;
60438 assert( pPager->eState==PAGER_OPEN );
60439 assert( pPager->eLock>=SHARED_LOCK );
60441 if( !pPager->tempFile ){
60442 int isWal; /* True if WAL file exists */
60444 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
60453 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
60455 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
60458 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
60459 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
60469 ** the entire super-journal file. The case pSavepoint==NULL occurs when
60473 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
60486 ** * Pages are then played back from the sub-journal file, starting
60500 ** (or transaction). No page with a page-number greater than this value
60503 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
60505 i64 iHdrOff; /* End of first segment of main-journal records */
60506 int rc = SQLITE_OK; /* Return code */
60509 assert( pPager->eState!=PAGER_ERROR );
60510 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60514 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
60523 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
60524 pPager->changeCountDone = pPager->tempFile;
60530 /* Use pPager->journalOff as the effective size of the main rollback
60533 ** past pPager->journalOff is off-limits to us.
60535 szJ = pPager->journalOff;
60541 ** greater than the current database size (pPager->dbSize) but those
60546 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
60547 pPager->journalOff = pSavepoint->iOffset;
60548 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
60549 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60553 pPager->journalOff = 0;
60558 ** of the main journal file. Continue to skip out-of-range pages and
60561 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
60569 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
60574 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
60576 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
60578 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
60579 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60583 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
60585 /* Finally, rollback pages from the sub-journal. Page that were
60587 ** will be skipped. Out-of-range pages are also skipped.
60591 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
60594 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
60596 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
60597 assert( offset==(i64)ii*(4+pPager->pageSize) );
60605 pPager->journalOff = szJ;
60612 ** Change the maximum number of in-memory pages that are allowed
60615 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
60616 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
60620 ** Change the maximum number of in-memory pages that are allowed
60623 SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
60624 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
60632 sqlite3_file *fd = pPager->fd;
60633 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
60635 sz = pPager->szMmap;
60636 pPager->bUseFetch = (sz>0);
60638 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
60647 pPager->szMmap = szMmap;
60655 sqlite3PcacheShrink(pPager->pPCache);
60677 ** database (with some additional information - the nRec field
60678 ** of the journal header - being written in between the two
60688 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
60699 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
60714 if( pPager->tempFile ){
60715 pPager->noSync = 1;
60716 pPager->fullSync = 0;
60717 pPager->extraSync = 0;
60719 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
60720 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
60721 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
60723 if( pPager->noSync ){
60724 pPager->syncFlags = 0;
60726 pPager->syncFlags = SQLITE_SYNC_FULL;
60728 pPager->syncFlags = SQLITE_SYNC_NORMAL;
60730 pPager->walSyncFlags = (pPager->syncFlags<<2);
60731 if( pPager->fullSync ){
60732 pPager->walSyncFlags |= pPager->syncFlags;
60734 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
60735 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
60738 pPager->doNotSpill &= ~SPILLFLAG_OFF;
60740 pPager->doNotSpill |= SPILLFLAG_OFF;
60750 SQLITE_API int sqlite3_opentemp_count = 0;
60768 static int pagerOpentemp(
60771 int vfsFlags /* Flags passed through to the VFS */
60773 int rc; /* Return code */
60781 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
60789 ** The pager invokes the busy-handler if sqlite3OsLock() returns
60790 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
60794 ** (which occurs during hot-journal rollback). Summary:
60797 ** --------------------------------------------------------
60798 ** NO_LOCK -> SHARED_LOCK | Yes
60799 ** SHARED_LOCK -> RESERVED_LOCK | No
60800 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
60801 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
60803 ** If the busy-handler callback returns non-zero, the lock is
60809 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
60813 pPager->xBusyHandler = xBusyHandler;
60814 pPager->pBusyHandlerArg = pBusyHandlerArg;
60815 ap = (void **)&pPager->xBusyHandler;
60816 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
60818 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
60826 ** is a no-op. The value returned is the error state error code (i.e.
60827 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
60836 ** * the database is either not an in-memory database or it is
60837 ** an in-memory database that currently consists of zero pages.
60851 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
60852 int rc = SQLITE_OK;
60861 ** is a no-op for that case anyhow.
60866 if( (pPager->memDb==0 || pPager->dbSize==0)
60867 && sqlite3PcacheRefCount(pPager->pPCache)==0
60868 && pageSize && pageSize!=(u32)pPager->pageSize
60873 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
60874 rc = sqlite3OsFileSize(pPager->fd, &nByte);
60877 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
60889 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
60892 sqlite3PageFree(pPager->pTmpSpace);
60893 pPager->pTmpSpace = pNew;
60894 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
60895 pPager->pageSize = pageSize;
60896 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
60902 *pPageSize = pPager->pageSize;
60904 if( nReserve<0 ) nReserve = pPager->nReserve;
60906 pPager->nReserve = (i16)nReserve;
60921 return pPager->pTmpSpace;
60933 pPager->mxPgno = mxPage;
60935 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
60936 /* assert( pPager->mxPgno>=pPager->dbSize ); */
60940 return pPager->mxPgno;
60948 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
60952 SQLITE_API extern int sqlite3_io_error_pending;
60953 SQLITE_API extern int sqlite3_io_error_hit;
60954 static int saved_cnt;
60957 sqlite3_io_error_pending = -1;
60981 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
60982 int rc = SQLITE_OK;
60984 assert( isOpen(pPager->fd) || pPager->tempFile );
60992 if( isOpen(pPager->fd) ){
60994 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
61003 ** This function may only be called when a read-transaction is open on
61006 ** However, if the file is between 1 and <page-size> bytes in size, then
61009 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
61010 assert( pPager->eState>=PAGER_READER );
61011 assert( pPager->eState!=PAGER_WRITER_FINISHED );
61012 *pnPage = (int)pPager->dbSize;
61018 ** a similar or greater lock is already held, this function is a no-op
61030 static int pager_wait_on_lock(Pager *pPager, int locktype){
61031 int rc; /* Return code */
61033 /* Check that this is either a no-op (because the requested lock is
61034 ** already held), or one of the transitions that the busy-handler
61038 assert( (pPager->eLock>=locktype)
61039 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
61040 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
61045 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
61051 ** following is true for all dirty pages currently in the page-cache:
61057 ** be necessary to write the current content out to the sub-journal.
61066 ** sub-journal rolled back the content could not be restored and the
61072 Pager *pPager = pPg->pPager;
61073 assert( pPg->flags&PGHDR_DIRTY );
61074 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
61075 Pgno pgno = pPg->pgno;
61076 int i;
61077 for(i=0; i<pPg->pPager->nSavepoint; i++){
61078 PagerSavepoint *p = &pPager->aSavepoint[i];
61079 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
61084 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
61091 ** Truncate the in-memory database file image to nPage pages. This
61102 assert( pPager->dbSize>=nPage || CORRUPT_DB );
61103 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
61104 pPager->dbSize = nPage;
61119 ** This function is called before attempting a hot-journal rollback. It
61120 ** syncs the journal file to disk, then sets pPager->journalHdr to the
61124 ** Syncing a hot-journal to disk before attempting to roll it back ensures
61125 ** that if a power-failure occurs during the rollback, the process that
61132 static int pagerSyncHotJournal(Pager *pPager){
61133 int rc = SQLITE_OK;
61134 if( !pPager->noSync ){
61135 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
61138 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
61154 static int pagerAcquireMapPage(
61162 if( pPager->pMmapFreelist ){
61163 *ppPage = p = pPager->pMmapFreelist;
61164 pPager->pMmapFreelist = p->pDirty;
61165 p->pDirty = 0;
61166 assert( pPager->nExtra>=8 );
61167 memset(p->pExtra, 0, 8);
61169 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
61171 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
61174 p->pExtra = (void *)&p[1];
61175 p->flags = PGHDR_MMAP;
61176 p->nRef = 1;
61177 p->pPager = pPager;
61180 assert( p->pExtra==(void *)&p[1] );
61181 assert( p->pPage==0 );
61182 assert( p->flags==PGHDR_MMAP );
61183 assert( p->pPager==pPager );
61184 assert( p->nRef==1 );
61186 p->pgno = pgno;
61187 p->pData = pData;
61188 pPager->nMmapOut++;
61199 Pager *pPager = pPg->pPager;
61200 pPager->nMmapOut--;
61201 pPg->pDirty = pPager->pMmapFreelist;
61202 pPager->pMmapFreelist = pPg;
61204 assert( pPager->fd->pMethods->iVersion>=3 );
61205 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
61214 for(p=pPager->pMmapFreelist; p; p=pNext){
61215 pNext = p->pDirty;
61222 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
61225 static int databaseIsUnmoved(Pager *pPager){
61226 int bHasMoved = 0;
61227 int rc;
61229 if( pPager->tempFile ) return SQLITE_OK;
61230 if( pPager->dbSize==0 ) return SQLITE_OK;
61231 assert( pPager->zFilename && pPager->zFilename[0] );
61232 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
61234 /* If the HAS_MOVED file-control is unimplemented, assume that the file
61259 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
61260 u8 *pTmp = (u8*)pPager->pTmpSpace;
61266 /* pPager->errCode = 0; */
61267 pPager->exclusiveMode = 0;
61271 assert( db || pPager->pWal==0 );
61272 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
61277 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
61278 pPager->pWal = 0;
61293 ** back or finalize it. The next database user will have to do hot-journal
61296 if( isOpen(pPager->jfd) ){
61305 sqlite3OsClose(pPager->jfd);
61306 sqlite3OsClose(pPager->fd);
61308 sqlite3PcacheClose(pPager->pPCache);
61309 assert( !pPager->aSavepoint && !pPager->pInJournal );
61310 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
61321 return pPg->pgno;
61335 ** disk and can be restored in the event of a hot-journal rollback.
61337 ** If the Pager.noSync flag is set, then this function is a no-op.
61338 ** Otherwise, the actions required depend on the journal-mode and the
61339 ** device characteristics of the file-system, as follows:
61341 ** * If the journal file is an in-memory journal file, no action need
61347 ** been written following it. If the pager is operating in full-sync
61353 ** Or, in pseudo-code:
61355 ** if( NOT <in-memory journal> ){
61357 ** if( <full-sync mode> ) xSync(<journal file>);
61367 static int syncJournal(Pager *pPager, int newHdr){
61368 int rc; /* Return code */
61370 assert( pPager->eState==PAGER_WRITER_CACHEMOD
61371 || pPager->eState==PAGER_WRITER_DBMOD
61379 if( !pPager->noSync ){
61380 assert( !pPager->tempFile );
61381 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
61382 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
61383 assert( isOpen(pPager->jfd) );
61387 ** that wrote to this database was operating in persistent-journal
61390 ** file happens to be a journal-header (written as part of the
61391 ** previous connection's transaction), and a crash or power-failure
61395 ** hot-journal rollback following recovery. It may roll back all
61397 ** out-of-date data that follows it. Database corruption.
61413 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
61416 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
61419 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
61426 ** full-synchronous mode, sync the journal first. This ensures that
61436 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
61439 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
61442 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
61444 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
61451 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
61452 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
61457 pPager->journalHdr = pPager->journalOff;
61459 pPager->nRec = 0;
61464 pPager->journalHdr = pPager->journalOff;
61472 sqlite3PcacheClearSyncFlags(pPager->pPCache);
61473 pPager->eState = PAGER_WRITER_DBMOD;
61481 ** in-memory pages in the list to the database file. The argument may
61483 ** a no-op.
61490 ** If the pager is a temp-file pager and the actual file-system file
61510 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
61511 int rc = SQLITE_OK; /* Return code */
61515 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
61516 assert( pPager->eLock==EXCLUSIVE_LOCK );
61517 assert( isOpen(pPager->fd) || pList->pDirty==0 );
61519 /* If the file is a temp-file has not yet been opened, open it now. It
61521 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
61523 if( !isOpen(pPager->fd) ){
61524 assert( pPager->tempFile && rc==SQLITE_OK );
61525 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
61531 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
61533 && pPager->dbHintSize<pPager->dbSize
61534 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
61536 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
61537 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
61538 pPager->dbHintSize = pPager->dbSize;
61542 Pgno pgno = pList->pgno;
61546 ** make the file smaller (presumably by auto-vacuum code). Do not write
61552 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
61553 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
61556 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
61557 if( pList->pgno==1 ) pager_write_changecounter(pList);
61559 pData = pList->pData;
61562 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
61569 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
61571 if( pgno>pPager->dbFileSize ){
61572 pPager->dbFileSize = pgno;
61574 pPager->aStat[PAGER_STAT_WRITE]++;
61577 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
61587 pList = pList->pDirty;
61594 ** Ensure that the sub-journal file is open. If it is already open, this
61595 ** function is a no-op.
61601 static int openSubJournal(Pager *pPager){
61602 int rc = SQLITE_OK;
61603 if( !isOpen(pPager->sjfd) ){
61604 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
61607 int nStmtSpill = sqlite3Config.nStmtSpill;
61608 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
61609 nStmtSpill = -1;
61611 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
61617 ** Append a record of the current state of page pPg to the sub-journal.
61619 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
61623 ** error code if the attempt to write to the sub-journal fails, or
61627 static int subjournalPage(PgHdr *pPg){
61628 int rc = SQLITE_OK;
61629 Pager *pPager = pPg->pPager;
61630 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61632 /* Open the sub-journal, if it has not already been opened */
61633 assert( pPager->useJournal );
61634 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
61635 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
61638 || pPg->pgno>pPager->dbOrigSize
61642 /* If the sub-journal was opened successfully (or was already open),
61645 void *pData = pPg->pData;
61646 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
61649 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
61650 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
61652 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
61657 pPager->nSubRec++;
61658 assert( pPager->nSavepoint>0 );
61659 rc = addToSavepointBitvecs(pPager, pPg->pgno);
61663 static int subjournalPageIfRequired(PgHdr *pPg){
61674 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
61690 static int pagerStress(void *p, PgHdr *pPg){
61692 int rc = SQLITE_OK;
61694 assert( pPg->pPager==pPager );
61695 assert( pPg->flags&PGHDR_DIRTY );
61713 if( NEVER(pPager->errCode) ) return SQLITE_OK;
61714 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
61715 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
61716 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
61717 if( pPager->doNotSpill
61718 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
61719 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
61724 pPager->aStat[PAGER_STAT_SPILL]++;
61725 pPg->pDirty = 0;
61735 if( pPager->tempFile==0 ){
61736 rc = sqlite3JournalCreate(pPager->jfd);
61742 if( pPg->flags&PGHDR_NEED_SYNC
61743 || pPager->eState==PAGER_WRITER_CACHEMOD
61750 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
61757 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
61767 SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
61768 int rc = pPager->errCode;
61770 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
61773 PgHdr *pNext = pList->pDirty;
61774 if( pList->nRef==0 ){
61790 ** If zFilename is NULL then a randomly-named temporary file is created
61794 ** This can be used to implement an in-memory database.
61816 SQLITE_PRIVATE int sqlite3PagerOpen(
61820 int nExtra, /* Extra bytes append to each in-memory page */
61821 int flags, /* flags controlling this file */
61822 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
61827 int rc = SQLITE_OK; /* Return code */
61828 int tempFile = 0; /* True for temp files (incl. in-memory files) */
61829 int memDb = 0; /* True if this is an in-memory file */
61830 int memJM = 0; /* Memory journal mode */
61831 int readOnly = 0; /* True if this is a read-only file */
61832 int journalFileSize; /* Bytes to allocate for each journal fd */
61834 int nPathname = 0; /* Number of bytes in zPathname */
61835 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
61836 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
61839 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
61841 /* Figure out how much space is required for each journal file-handle
61842 ** (there are two of them, the main journal and the sub-journal). */
61866 nPathname = pVfs->mxPathname+1;
61888 nUriByte = (int)(&z[1] - zUri);
61890 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
61892 ** the database being opened will be more than pVfs->mxPathname
61895 ** check for a hot-journal before reading.
61911 ** Database file handle (pVfs->szOsFile bytes)
61912 ** Sub-journal file handle (journalFileSize bytes)
61922 ** Some 3rd-party software, over which we have no control, depends on
61926 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
61929 ** filename format expected by 3rd-party software should be as follows:
61931 ** - Main Database Path
61932 ** - \0
61933 ** - Multiple URI components consisting of:
61934 ** - Key
61935 ** - \0
61936 ** - Value
61937 ** - \0
61938 ** - \0
61939 ** - Journal Path
61940 ** - \0
61941 ** - WAL Path (zWALName)
61942 ** - \0
61953 ROUND8(pVfs->szOsFile) + /* The main db file */
61971 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
61972 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
61973 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61974 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61975 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
61980 pPager->zFilename = (char*)pPtr;
61993 pPager->zJournal = (char*)pPtr;
61995 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
61997 sqlite3FileSuffix3(zFilename,pPager->zJournal);
61998 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
62001 pPager->zJournal = 0;
62007 pPager->zWal = (char*)pPtr;
62009 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
62011 sqlite3FileSuffix3(zFilename, pPager->zWal);
62012 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
62015 pPager->zWal = 0;
62021 pPager->pVfs = pVfs;
62022 pPager->vfsFlags = vfsFlags;
62027 int fout = 0; /* VFS flags returned by xOpen() */
62028 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
62030 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
62042 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
62046 if( szPageDflt<pPager->sectorSize ){
62047 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
62050 szPageDflt = (u32)pPager->sectorSize;
62055 int ii;
62067 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
62069 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
62079 ** This branch is also run for an in-memory database. An in-memory
62080 ** database is the same as a temp-file that is never written out to
62081 ** disk and uses an in-memory rollback journal.
62087 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
62088 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
62089 pPager->noLock = 1; /* Do no locking */
62097 assert( pPager->memDb==0 );
62098 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
62107 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
62113 sqlite3OsClose(pPager->fd);
62114 sqlite3PageFree(pPager->pTmpSpace);
62119 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
62120 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
62122 pPager->useJournal = (u8)useJournal;
62123 /* pPager->stmtOpen = 0; */
62124 /* pPager->stmtInUse = 0; */
62125 /* pPager->nRef = 0; */
62126 /* pPager->stmtSize = 0; */
62127 /* pPager->stmtJSize = 0; */
62128 /* pPager->nPage = 0; */
62129 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
62130 /* pPager->state = PAGER_UNLOCK; */
62131 /* pPager->errMask = 0; */
62132 pPager->tempFile = (u8)tempFile;
62136 pPager->exclusiveMode = (u8)tempFile;
62137 pPager->changeCountDone = pPager->tempFile;
62138 pPager->memDb = (u8)memDb;
62139 pPager->readOnly = (u8)readOnly;
62140 assert( useJournal || pPager->tempFile );
62142 /* pPager->pFirst = 0; */
62143 /* pPager->pFirstSynced = 0; */
62144 /* pPager->pLast = 0; */
62145 pPager->nExtra = (u16)nExtra;
62146 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
62147 assert( isOpen(pPager->fd) || tempFile );
62150 pPager->journalMode = PAGER_JOURNALMODE_OFF;
62152 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
62154 /* pPager->xBusyHandler = 0; */
62155 /* pPager->pBusyHandlerArg = 0; */
62156 pPager->xReiniter = xReinit;
62158 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
62159 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
62173 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
62174 zName--;
62176 p = zName - 4 - sizeof(Pager*);
62179 return pPager->fd;
62186 ** the file-system for the given pager. A hot journal is one that
62187 ** needs to be played back. According to this function, a hot-journal
62201 ** This routine does not check if there is a super-journal filename
62202 ** at the end of the file. If there is, and that super-journal file
62204 ** case this routine will return a false-positive. The pager_playback()
62208 ** If a hot-journal file is found to exist, *pExists is set to 1 and
62209 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
62211 ** to determine whether or not a hot-journal file exists, the IO error
62214 static int hasHotJournal(Pager *pPager, int *pExists){
62215 sqlite3_vfs * const pVfs = pPager->pVfs;
62216 int rc = SQLITE_OK; /* Return code */
62217 int exists = 1; /* True if a journal file is present */
62218 int jrnlOpen = !!isOpen(pPager->jfd);
62220 assert( pPager->useJournal );
62221 assert( isOpen(pPager->fd) );
62222 assert( pPager->eState==PAGER_OPEN );
62224 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
62230 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
62233 int locked = 0; /* True if some process holds a RESERVED lock */
62240 ** in fact there is none. This results in a false-positive which will
62243 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
62247 assert( pPager->tempFile==0 );
62261 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
62262 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
62268 ** at least one non-zero bytes at the start of the journal file.
62273 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
62274 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
62278 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
62283 sqlite3OsClose(pPager->jfd);
62310 ** has been successfully called. If a shared-lock is already held when
62311 ** this function is called, it is a no-op.
62318 ** the SHARED lock, the file-system is checked for a hot-journal,
62319 ** which is played back if present. Following any hot-journal
62321 ** the 'change-counter' field of the database file header and
62324 ** 2) If the pager is running in exclusive-mode, and there are currently
62331 ** occurs while locking the database, checking for a hot-journal file or
62334 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
62335 int rc = SQLITE_OK; /* Return code */
62337 /* This routine is only called from b-tree and only when there are no
62341 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
62343 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
62344 assert( pPager->errCode==SQLITE_OK );
62346 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
62347 int bHotJournal = 1; /* True if there exists a hot journal-file */
62350 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
62354 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
62361 if( pPager->eLock<=SHARED_LOCK ){
62368 if( pPager->readOnly ){
62378 ** hot-journal back.
62395 ** in exclusive-access mode the file descriptor will be kept open
62396 ** and possibly used for a transaction later on. Also, write-access
62403 ** may mean that the pager was in the error-state when this
62406 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62407 sqlite3_vfs * const pVfs = pPager->pVfs;
62408 int bExists; /* True if journal file exists */
62410 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
62412 int fout = 0;
62413 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
62414 assert( !pPager->tempFile );
62415 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
62416 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62419 sqlite3OsClose(pPager->jfd);
62426 ** playing back the hot-journal so that we don't end up with
62432 if( isOpen(pPager->jfd) ){
62436 rc = pager_playback(pPager, !pPager->tempFile);
62437 pPager->eState = PAGER_OPEN;
62439 }else if( !pPager->exclusiveMode ){
62445 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
62464 assert( pPager->eState==PAGER_OPEN );
62465 assert( (pPager->eLock==SHARED_LOCK)
62466 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
62470 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
62471 /* The shared-lock has just been acquired then check to
62475 ** single unnecessary sqlite3OsRead() call at the start-up.
62479 ** a 32-bit counter that is incremented with each change. The
62487 char dbFileVers[sizeof(pPager->dbFileVers)];
62490 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
62498 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
62508 sqlite3OsUnfetch(pPager->fd, 0, 0);
62513 /* If there is a WAL file in the file-system, open this database in WAL
62514 ** mode. Otherwise, the following function call is a no-op.
62518 assert( pPager->pWal==0 || rc==SQLITE_OK );
62527 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
62528 rc = pagerPagecount(pPager, &pPager->dbSize);
62535 assert( pPager->eState==PAGER_OPEN );
62537 pPager->eState = PAGER_READER;
62538 pPager->hasHeldSharedLock = 1;
62549 ** nothing to rollback, so this routine is a no-op.
62552 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
62553 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
62566 ** getPageNormal() -- The normal getter
62567 ** getPageError() -- Used if the pager is in an error state
62568 ** getPageMmap() -- Used if memory-mapped I/O is enabled
62590 ** a) When reading a free-list leaf page from the database, and
62608 ** to find a page in the in-memory cache first. If the page is not already
62610 ** just returns 0. This routine acquires a read-lock the first time it
62615 static int getPageNormal(
62619 int flags /* PAGER_GET_XXX flags */
62621 int rc = SQLITE_OK;
62626 assert( pPager->errCode==SQLITE_OK );
62627 assert( pPager->eState>=PAGER_READER );
62629 assert( pPager->hasHeldSharedLock==1 );
62632 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
62635 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
62642 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
62644 assert( pPg->pgno==pgno );
62645 assert( pPg->pPager==pPager || pPg->pPager==0 );
62648 if( pPg->pPager && !noContent ){
62652 pPager->aStat[PAGER_STAT_HIT]++;
62667 pPg->pPager = pPager;
62669 assert( !isOpen(pPager->fd) || !MEMDB );
62670 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
62671 if( pgno>pPager->mxPgno ){
62673 if( pgno<=pPager->dbSize ){
62680 /* Failure to set the bits in the InJournal bit-vectors is benign.
62687 if( pgno<=pPager->dbOrigSize ){
62688 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
62695 memset(pPg->pData, 0, pPager->pageSize);
62698 assert( pPg->pPager==pPager );
62699 pPager->aStat[PAGER_STAT_MISS]++;
62720 /* The page getter for when memory-mapped I/O is enabled */
62721 static int getPageMMap(
62725 int flags /* PAGER_GET_XXX flags */
62727 int rc = SQLITE_OK;
62731 /* It is acceptable to use a read-only (mmap) page for any page except
62732 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
62734 ** temporary or in-memory database. */
62735 const int bMmapOk = (pgno>1
62736 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
62748 assert( pPager->eState>=PAGER_READER );
62750 assert( pPager->hasHeldSharedLock==1 );
62751 assert( pPager->errCode==SQLITE_OK );
62754 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
62762 rc = sqlite3OsFetch(pPager->fd,
62763 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
62766 if( pPager->eState>PAGER_READER || pPager->tempFile ){
62772 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
62790 static int getPageError(
62794 int flags /* PAGER_GET_XXX flags */
62798 assert( pPager->errCode!=SQLITE_OK );
62800 return pPager->errCode;
62806 SQLITE_PRIVATE int sqlite3PagerGet(
62810 int flags /* PAGER_GET_XXX flags */
62813 int rc;
62816 rc = pPager->xGet(pPager, pgno, ppPage, flags);
62823 /* Normal, high-speed version of sqlite3PagerGet() */
62824 return pPager->xGet(pPager, pgno, ppPage, flags);
62829 ** Acquire a page if it is already in the in-memory cache. Do
62843 assert( pPager->pPCache!=0 );
62844 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
62845 assert( pPage==0 || pPager->hasHeldSharedLock );
62847 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
62857 ** The assert() at tag-20230419-2 proves that this constraint is always
62865 TESTONLY( Pager *pPager = pPg->pPager; )
62867 if( pPg->flags & PGHDR_MMAP ){
62868 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
62874 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
62882 assert( pPg->pgno==1 );
62883 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
62884 pPager = pPg->pPager;
62895 ** to the start of it. If there are active savepoints, open the sub-journal
62911 static int pager_open_journal(Pager *pPager){
62912 int rc = SQLITE_OK; /* Return code */
62913 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
62915 assert( pPager->eState==PAGER_WRITER_LOCKED );
62917 assert( pPager->pInJournal==0 );
62919 /* If already in the error state, this function is a no-op. But on
62922 if( NEVER(pPager->errCode) ) return pPager->errCode;
62924 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62925 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
62926 if( pPager->pInJournal==0 ){
62931 if( !isOpen(pPager->jfd) ){
62932 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
62933 sqlite3MemJournalOpen(pPager->jfd);
62935 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
62936 int nSpill;
62938 if( pPager->tempFile ){
62952 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
62956 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62961 ** the sub-journal if necessary.
62965 pPager->nRec = 0;
62966 pPager->journalOff = 0;
62967 pPager->setSuper = 0;
62968 pPager->journalHdr = 0;
62974 sqlite3BitvecDestroy(pPager->pInJournal);
62975 pPager->pInJournal = 0;
62976 pPager->journalOff = 0;
62978 assert( pPager->eState==PAGER_WRITER_LOCKED );
62979 pPager->eState = PAGER_WRITER_CACHEMOD;
62986 ** Begin a write-transaction on the specified pager object. If a
62987 ** write-transaction has already been opened, this function is a no-op.
62994 ** If the subjInMemory argument is non-zero, then any sub-journal opened
62995 ** within this transaction will be opened as an in-memory file. This
62996 ** has no effect if the sub-journal is already opened (as it may be when
62998 ** sub-journal. If the subjInMemory argument is zero, then any required
62999 ** sub-journal is implemented in-memory if pPager is an in-memory database,
63002 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
63003 int rc = SQLITE_OK;
63005 if( pPager->errCode ) return pPager->errCode;
63006 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
63007 pPager->subjInMemory = (u8)subjInMemory;
63009 if( pPager->eState==PAGER_READER ){
63010 assert( pPager->pInJournal==0 );
63016 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
63021 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
63026 ** The busy-handler is not invoked if another connection already
63027 ** holds the write-lock. If possible, the upper layer will call it.
63029 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
63033 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
63048 ** transactions may copy data from the sub-journal into the database
63052 pPager->eState = PAGER_WRITER_LOCKED;
63053 pPager->dbHintSize = pPager->dbSize;
63054 pPager->dbFileSize = pPager->dbSize;
63055 pPager->dbOrigSize = pPager->dbSize;
63056 pPager->journalOff = 0;
63059 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
63060 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
63071 static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
63072 Pager *pPager = pPg->pPager;
63073 int rc;
63076 i64 iOff = pPager->journalOff;
63081 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
63083 assert( pPager->journalHdr<=pPager->journalOff );
63084 pData2 = pPg->pData;
63088 ** page in the block above, set the need-sync flag for the page.
63094 pPg->flags |= PGHDR_NEED_SYNC;
63096 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
63098 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
63100 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
63103 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
63104 pPager->journalOff, pPager->pageSize));
63107 PAGERID(pPager), pPg->pgno,
63108 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
63110 pPager->journalOff += 8 + pPager->pageSize;
63111 pPager->nRec++;
63112 assert( pPager->pInJournal!=0 );
63113 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
63116 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
63123 ** main journal or sub-journal as required. If the page is written into
63128 static int pager_write(PgHdr *pPg){
63129 Pager *pPager = pPg->pPager;
63130 int rc = SQLITE_OK;
63132 /* This routine is not called unless a write-transaction has already
63136 assert( pPager->eState==PAGER_WRITER_LOCKED
63137 || pPager->eState==PAGER_WRITER_CACHEMOD
63138 || pPager->eState==PAGER_WRITER_DBMOD
63141 assert( pPager->errCode==0 );
63142 assert( pPager->readOnly==0 );
63146 ** obtained the necessary locks to begin the write-transaction, but the
63154 if( pPager->eState==PAGER_WRITER_LOCKED ){
63158 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
63168 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
63169 if( pPager->pInJournal!=0
63170 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
63173 if( pPg->pgno<=pPager->dbOrigSize ){
63179 if( pPager->eState!=PAGER_WRITER_DBMOD ){
63180 pPg->flags |= PGHDR_NEED_SYNC;
63183 PAGERID(pPager), pPg->pgno,
63184 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
63188 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
63193 pPg->flags |= PGHDR_WRITEABLE;
63198 if( pPager->nSavepoint>0 ){
63203 if( pPager->dbSize<pPg->pgno ){
63204 pPager->dbSize = pPg->pgno;
63220 static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
63221 int rc = SQLITE_OK; /* Return code */
63224 int nPage = 0; /* Number of pages starting at pg1 to journal */
63225 int ii; /* Loop counter */
63226 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
63227 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
63228 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
63235 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
63236 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
63238 /* This trick assumes that both the page-size and sector-size are
63242 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
63244 nPageCount = pPager->dbSize;
63245 if( pPg->pgno>nPageCount ){
63246 nPage = (pPg->pgno - pg1)+1;
63247 }else if( (pg1+nPagePerSector-1)>nPageCount ){
63248 nPage = nPageCount+1-pg1;
63253 assert(pg1<=pPg->pgno);
63254 assert((pg1+nPage)>pPg->pgno);
63259 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
63264 if( pPage->flags&PGHDR_NEED_SYNC ){
63271 if( pPage->flags&PGHDR_NEED_SYNC ){
63289 pPage->flags |= PGHDR_NEED_SYNC;
63295 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
63296 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
63308 ** fit on a single disk sector. In this case all co-resident pages
63314 SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
63315 Pager *pPager = pPg->pPager;
63316 assert( (pPg->flags & PGHDR_MMAP)==0 );
63317 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63319 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
63320 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
63322 }else if( pPager->errCode ){
63323 return pPager->errCode;
63324 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
63325 assert( pPager->tempFile==0 );
63338 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
63339 return pPg->flags & PGHDR_WRITEABLE;
63357 ** This optimization cannot be used with a temp-file, as the page may
63364 Pager *pPager = pPg->pPager;
63365 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
63366 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
63367 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
63368 pPg->flags |= PGHDR_DONT_WRITE;
63369 pPg->flags &= ~PGHDR_WRITEABLE;
63370 testcase( pPg->flags & PGHDR_NEED_SYNC );
63377 ** change-counter, stored as a 4-byte big-endian integer starting at
63381 ** But this only happens if the pPager->changeCountDone flag is false.
63391 ** The isDirectMode flag may only be non-zero if the library was compiled
63393 ** if isDirect is non-zero, then the database file is updated directly
63397 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
63398 int rc = SQLITE_OK;
63400 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63401 || pPager->eState==PAGER_WRITER_DBMOD
63406 ** atomic-write optimization is enabled in this build, then isDirect
63410 ** The idea is that if the atomic-write optimization is not
63423 if( !pPager->changeCountDone && pPager->dbSize>0 ){
63426 assert( !pPager->tempFile && isOpen(pPager->fd) );
63433 ** operating in direct-mode, make page 1 writable. When not in
63435 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
63448 assert( pPager->dbFileSize>0 );
63449 zBuf = pPgHdr->pData;
63451 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
63452 pPager->aStat[PAGER_STAT_WRITE]++;
63455 /* Update the pager's copy of the change-counter. Otherwise, the
63457 ** flushed (as the change-counter values will not match). */
63459 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
63460 pPager->changeCountDone = 1;
63463 pPager->changeCountDone = 1;
63474 ** Sync the database file to disk. This is a no-op for in-memory databases
63477 ** If successful, or if called on a pager for which it is a no-op, this
63480 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper){
63481 int rc = SQLITE_OK;
63483 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
63485 if( rc==SQLITE_OK && !pPager->noSync ){
63487 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
63493 ** This function may only be called while a write-transaction is active in
63494 ** rollback. If the connection is in WAL mode, this call is a no-op.
63503 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
63504 int rc = pPager->errCode;
63507 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63508 || pPager->eState==PAGER_WRITER_DBMOD
63509 || pPager->eState==PAGER_WRITER_LOCKED
63521 ** of a super-journal file that should be written into the individual
63523 ** super-journal (a single database transaction).
63527 ** * The database file change-counter is updated,
63528 ** * the journal is synced (unless the atomic-write optimization is used),
63535 ** delete the super-journal file if specified).
63540 ** If the final parameter - noSync - is true, then the database file itself
63545 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
63547 const char *zSuper, /* If not NULL, the super-journal name */
63548 int noSync /* True to omit the xSync on the db file */
63550 int rc = SQLITE_OK; /* Return code */
63552 assert( pPager->eState==PAGER_WRITER_LOCKED
63553 || pPager->eState==PAGER_WRITER_CACHEMOD
63554 || pPager->eState==PAGER_WRITER_DBMOD
63555 || pPager->eState==PAGER_ERROR
63560 if( NEVER(pPager->errCode) ) return pPager->errCode;
63566 pPager->zFilename, zSuper, pPager->dbSize));
63569 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
63571 assert( MEMDB==0 || pPager->tempFile );
63572 assert( isOpen(pPager->fd) || pPager->tempFile );
63574 /* If this is an in-memory db, or no pages have been written to, or this
63575 ** function has already been called, it is mostly a no-op. However, any
63577 sqlite3BackupRestart(pPager->pBackup);
63582 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63585 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
63588 pList->pDirty = 0;
63592 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
63596 sqlite3PcacheCleanAll(pPager->pPCache);
63599 /* The bBatch boolean is true if the batch-atomic-write commit method
63600 ** should be used. No rollback journal is created if batch-atomic-write
63604 sqlite3_file *fd = pPager->fd;
63605 int bBatch = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
63607 && !pPager->noSync
63608 && sqlite3JournalIsInMemory(pPager->jfd);
63614 /* The following block updates the change-counter. Exactly how it
63615 ** does this depends on whether or not the atomic-update optimization
63619 ** * The file-system supports the atomic-write property for
63620 ** blocks of size page-size, and
63621 ** * This commit is not part of a multi-file transaction, and
63626 ** counter in 'indirect-mode'. If the optimization is compiled in but
63629 ** pager_incr_changecounter() to update the change-counter in indirect
63633 ** then call pager_incr_changecounter() to update the change-counter
63639 assert( isOpen(pPager->jfd)
63640 || pPager->journalMode==PAGER_JOURNALMODE_OFF
63641 || pPager->journalMode==PAGER_JOURNALMODE_WAL
63643 if( !zSuper && isOpen(pPager->jfd)
63644 && pPager->journalOff==jrnlBufferSize(pPager)
63645 && pPager->dbSize>=pPager->dbOrigSize
63646 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
63648 /* Update the db file change counter via the direct-write method. The
63649 ** following call will modify the in-memory representation of page 1
63651 ** directly to the database file. Because of the atomic-write
63652 ** property of the host file-system, this is safe.
63656 rc = sqlite3JournalCreate(pPager->jfd);
63665 rc = sqlite3JournalCreate(pPager->jfd);
63674 /* Write the super-journal name into the journal file. If a
63675 ** super-journal file name has already been written to the journal file,
63676 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
63682 ** If the atomic-update optimization is being used, this sync will not
63685 ** Because the change-counter page was just modified, unless the
63686 ** atomic-update optimization is used it is almost certain that the
63690 ** xSync() call will be changed to a no-op by the OS anyhow.
63695 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63701 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
63702 char *pTmp = pPager->pTmpSpace;
63703 int szPage = (int)pPager->pageSize;
63705 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
63706 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
63717 rc = sqlite3JournalCreate(pPager->jfd);
63719 sqlite3OsClose(pPager->jfd);
63724 sqlite3OsClose(pPager->jfd);
63736 sqlite3PcacheCleanAll(pPager->pPCache);
63741 ** last page in the db image moved to the free-list. In this case the
63744 if( pPager->dbSize>pPager->dbFileSize ){
63745 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
63746 assert( pPager->eState==PAGER_WRITER_DBMOD );
63761 pPager->eState = PAGER_WRITER_FINISHED;
63770 ** synced to disk. The journal file still exists in the file-system
63772 ** be used as a hot-journal and the current transaction rolled back.
63776 ** for hot-journal rollback. Once this is done the transaction is
63782 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
63783 int rc = SQLITE_OK; /* Return code */
63788 if( NEVER(pPager->errCode) ) return pPager->errCode;
63789 pPager->iDataVersion++;
63791 assert( pPager->eState==PAGER_WRITER_LOCKED
63792 || pPager->eState==PAGER_WRITER_FINISHED
63793 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
63798 ** this transaction, the pager is running in exclusive-mode and is
63799 ** using persistent journals, then this function is a no-op.
63803 ** a hot-journal during hot-journal rollback, 0 changes will be made
63808 if( pPager->eState==PAGER_WRITER_LOCKED
63809 && pPager->exclusiveMode
63810 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
63812 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
63813 pPager->eState = PAGER_READER;
63818 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
63824 ** transaction are reverted and the current write-transaction is closed.
63834 ** in-memory cache pages to the state they were in when the transaction
63843 ** In WAL mode, all cache-entries containing data modified within the
63845 ** their pre-transaction state by re-reading data from the database or
63848 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
63849 int rc = SQLITE_OK; /* Return code */
63852 /* PagerRollback() is a no-op if called in READER or OPEN state. If
63857 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
63858 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
63861 int rc2;
63862 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
63863 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
63865 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
63866 int eState = pPager->eState;
63873 pPager->errCode = SQLITE_ABORT;
63874 pPager->eState = PAGER_ERROR;
63882 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
63895 ** Return TRUE if the database file is opened read-only. Return FALSE
63899 return pPager->readOnly;
63906 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
63907 return sqlite3PcacheRefCount(pPager->pPCache);
63915 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
63916 int perPageSize = pPager->pageSize + pPager->nExtra
63917 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
63918 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
63920 + pPager->pageSize;
63926 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
63934 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
63935 static int a[11];
63936 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
63937 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63938 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63939 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63940 a[4] = pPager->eState;
63941 a[5] = pPager->errCode;
63942 a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
63943 a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
63944 a[8] = 0; /* Used to be pPager->nOvfl */
63945 a[9] = pPager->nRead;
63946 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
63959 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
63962 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
63975 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
63976 *pnVal += pPager->aStat[eStat];
63978 pPager->aStat[eStat] = 0;
63983 ** Return true if this is an in-memory or temp-file backed pager.
63985 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
63986 return pPager->tempFile || pPager->memVfs;
63993 ** equal to nSavepoint, then this function is a no-op.
63996 ** occurs while opening the sub-journal file, then an IO error code is
63999 static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
64000 int rc = SQLITE_OK; /* Return code */
64001 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
64002 int ii; /* Iterator variable */
64005 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64007 assert( nSavepoint>nCurrent && pPager->useJournal );
64014 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
64019 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
64020 pPager->aSavepoint = aNew;
64024 aNew[ii].nOrig = pPager->dbSize;
64025 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
64026 aNew[ii].iOffset = pPager->journalOff;
64030 aNew[ii].iSubRec = pPager->nSubRec;
64031 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
64037 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
64039 pPager->nSavepoint = ii+1;
64041 assert( pPager->nSavepoint==nSavepoint );
64045 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
64046 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64049 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
64069 ** (the first created). A value of (Pager.nSavepoint-1) means operate
64071 ** (Pager.nSavepoint-1), then this function is a no-op.
64087 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
64088 int rc = pPager->errCode;
64097 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
64098 int ii; /* Iterator variable */
64099 int nNew; /* Number of remaining savepoints after this op. */
64106 for(ii=nNew; ii<pPager->nSavepoint; ii++){
64107 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
64109 pPager->nSavepoint = nNew;
64111 /* Truncate the sub-journal so that it only includes the parts
64114 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
64115 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
64116 /* Only truncate if it is an in-memory sub-journal. */
64117 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
64118 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
64119 rc = sqlite3OsTruncate(pPager->sjfd, sz);
64122 pPager->nSubRec = pRel->iSubRec;
64126 ** If this is a temp-file, it is possible that the journal file has
64130 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
64131 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
64142 pPager->journalMode==PAGER_JOURNALMODE_OFF
64143 && pPager->eState>=PAGER_WRITER_CACHEMOD
64145 pPager->errCode = SQLITE_ABORT;
64146 pPager->eState = PAGER_ERROR;
64158 ** Except, if the pager is in-memory only, then return an empty string if
64162 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
64163 ** participate in shared-cache.
64168 SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
64170 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
64173 return pPager->zFilename;
64181 return pPager->pVfs;
64190 return pPager->fd;
64199 return pPager->jfd;
64201 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
64209 return pPager->zJournal;
64222 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
64230 ** If the fourth argument, isCommit, is non-zero, then this page is being
64238 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
64240 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
64241 int rc; /* Return code */
64244 assert( pPg->nRef>0 );
64245 assert( pPager->eState==PAGER_WRITER_CACHEMOD
64246 || pPager->eState==PAGER_WRITER_DBMOD
64250 /* In order to be able to rollback, an in-memory database must journal
64253 assert( pPager->tempFile || !MEMDB );
64254 if( pPager->tempFile ){
64261 ** sub-journal now. This is required to handle the following scenario:
64269 ** If page X were not written to the sub-journal here, it would not
64273 ** subjournalPage() may need to allocate space to store pPg->pgno into
64277 if( (pPg->flags & PGHDR_DIRTY)!=0
64284 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
64285 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
64287 /* If the journal needs to be sync()ed before page pPg->pgno can
64288 ** be written to, store pPg->pgno in local variable needSyncPgno.
64291 ** the journal needs to be sync()ed before database page pPg->pgno
64294 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
64295 needSyncPgno = pPg->pgno;
64296 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
64297 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
64298 assert( pPg->flags&PGHDR_DIRTY );
64301 /* If the cache contains a page with page-number pgno, remove it
64306 pPg->flags &= ~PGHDR_NEED_SYNC;
64308 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
64310 if( NEVER(pPgOld->nRef>1) ){
64314 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
64315 if( pPager->tempFile ){
64316 /* Do not discard pages from an in-memory database since we might
64318 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
64324 origPgno = pPg->pgno;
64328 /* For an in-memory database, make sure the original page continues
64332 if( pPager->tempFile && pPgOld ){
64338 /* If needSyncPgno is non-zero, then the journal file needs to be
64340 ** Currently, no such page exists in the page-cache and the
64342 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
64345 ** If the attempt to load the page into the page-cache fails, (due
64355 if( needSyncPgno<=pPager->dbOrigSize ){
64356 assert( pPager->pTmpSpace!=0 );
64357 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
64361 pPgHdr->flags |= PGHDR_NEED_SYNC;
64377 assert( pPg->pgno!=iNew );
64378 pPg->flags = flags;
64386 assert( pPg->nRef>0 || pPg->pPager->memDb );
64387 return pPg->pData;
64395 return pPg->pExtra;
64399 ** Get/set the locking-mode for this pager. Parameter eMode must be one
64402 ** the locking-mode is set to the value specified.
64406 ** locking-mode.
64408 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
64414 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
64415 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
64416 pPager->exclusiveMode = (u8)eMode;
64418 return (int)pPager->exclusiveMode;
64422 ** Set the journal-mode for this pager. Parameter eMode must be one of:
64434 ** * An in-memory database can only have its journal_mode set to _OFF
64439 ** The returned indicate the current (possibly updated) journal-mode.
64441 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
64442 u8 eOld = pPager->journalMode; /* Prior journalmode */
64456 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
64458 /* Do allow the journalmode of an in-memory database to be set to
64471 assert( pPager->eState!=PAGER_ERROR );
64472 pPager->journalMode = (u8)eMode;
64485 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
64486 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
64495 sqlite3OsClose(pPager->jfd);
64496 if( pPager->eLock>=RESERVED_LOCK ){
64497 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64499 int rc = SQLITE_OK;
64500 int state = pPager->eState;
64505 if( pPager->eState==PAGER_READER ){
64510 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64517 assert( state==pPager->eState );
64520 sqlite3OsClose(pPager->jfd);
64525 return (int)pPager->journalMode;
64531 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
64532 return (int)pPager->journalMode;
64540 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
64542 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
64543 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
64548 ** Get/set the size-limit used for persistent journal files.
64550 ** Setting the size limit to -1 means no limit is enforced.
64551 ** An attempt to set a limit smaller than -1 is a no-op.
64554 if( iLimit>=-1 ){
64555 pPager->journalSizeLimit = iLimit;
64556 sqlite3WalLimit(pPager->pWal, iLimit);
64558 return pPager->journalSizeLimit;
64562 ** Return a pointer to the pPager->pBackup variable. The backup module
64568 return &pPager->pBackup;
64573 ** Unless this is an in-memory or temporary database, clear the pager cache.
64576 assert( MEMDB==0 || pPager->tempFile );
64577 if( pPager->tempFile==0 ) pager_reset(pPager);
64590 SQLITE_PRIVATE int sqlite3PagerCheckpoint(
64593 int eMode, /* Type of checkpoint */
64594 int *pnLog, /* OUT: Final number of frames in log */
64595 int *pnCkpt /* OUT: Final number of checkpointed frames */
64597 int rc = SQLITE_OK;
64598 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
64610 if( pPager->pWal ){
64611 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
64612 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
64613 pPager->pBusyHandlerArg,
64614 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
64621 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
64622 return sqlite3WalCallback(pPager->pWal);
64627 ** primitives necessary for write-ahead logging.
64629 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
64630 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
64631 if( pPager->noLock ) return 0;
64632 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
64639 static int pagerExclusiveLock(Pager *pPager){
64640 int rc; /* Return code */
64643 assert( pPager->eLock>=SHARED_LOCK );
64644 eOrigLock = pPager->eLock;
64657 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
64658 ** lock on the database file and use heap-memory to store the wal-index
64659 ** in. Otherwise, use the normal shared-memory.
64661 static int pagerOpenWal(Pager *pPager){
64662 int rc = SQLITE_OK;
64664 assert( pPager->pWal==0 && pPager->tempFile==0 );
64665 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
64667 /* If the pager is already in exclusive-mode, the WAL module will use
64668 ** heap-memory for the wal-index instead of the VFS shared-memory
64672 if( pPager->exclusiveMode ){
64680 rc = sqlite3WalOpen(pPager->pVfs,
64681 pPager->fd, pPager->zWal, pPager->exclusiveMode,
64682 pPager->journalSizeLimit, &pPager->pWal
64696 ** file (not a temp file or an in-memory database), and the WAL file
64702 ** If the pager is open on a temp-file (or in-memory database), or if
64706 SQLITE_PRIVATE int sqlite3PagerOpenWal(
64708 int *pbOpen /* OUT: Set to true if call is a no-op */
64710 int rc = SQLITE_OK; /* Return code */
64713 assert( pPager->eState==PAGER_OPEN || pbOpen );
64714 assert( pPager->eState==PAGER_READER || !pbOpen );
64716 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
64718 if( !pPager->tempFile && !pPager->pWal ){
64722 sqlite3OsClose(pPager->jfd);
64726 pPager->journalMode = PAGER_JOURNALMODE_WAL;
64727 pPager->eState = PAGER_OPEN;
64745 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
64746 int rc = SQLITE_OK;
64748 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
64750 /* If the log file is not already open, but does exist in the file-system,
64754 if( !pPager->pWal ){
64755 int logexists = 0;
64759 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
64768 ** the database file, the log and log-summary files will be deleted.
64770 if( rc==SQLITE_OK && pPager->pWal ){
64773 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
64774 pPager->pageSize, (u8*)pPager->pTmpSpace);
64775 pPager->pWal = 0;
64777 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
64785 ** If pager pPager is a wal-mode database not in exclusive locking mode,
64790 SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
64791 int rc = SQLITE_OK;
64792 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
64793 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
64804 sqlite3WalDb(pPager->pWal, db);
64814 SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
64815 int rc = SQLITE_ERROR;
64816 if( pPager->pWal ){
64817 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
64827 SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(
64831 int rc = SQLITE_OK;
64832 if( pPager->pWal ){
64833 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
64844 SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
64845 int rc;
64846 if( pPager->pWal ){
64847 rc = sqlite3WalSnapshotRecover(pPager->pWal);
64866 SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
64867 int rc;
64868 if( pPager->pWal ){
64869 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
64881 assert( pPager->pWal );
64882 sqlite3WalSnapshotUnlock(pPager->pWal);
64890 ** A read-lock must be held on the pager when this function is called. If
64896 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
64897 assert( pPager->eState>=PAGER_READER );
64898 return sqlite3WalFramesize(pPager->pWal);
64903 SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
64904 return sqlite3WalSystemErrno(pPager->pWal);
64924 ** This file contains the implementation of a write-ahead log (WAL) used in
64927 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
64946 ** big-endian 32-bit unsigned integer values:
64952 ** 16: Salt-1, random integer incremented with each checkpoint
64953 ** 20: Salt-2, a different random integer changing with each ckpt
64954 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
64955 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
64957 ** Immediately following the wal-header are zero or more frames. Each
64958 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
64959 ** of page data. The frame-header is six big-endian 32-bit unsigned
64965 ** 8: Salt-1 (copied from the header)
64966 ** 12: Salt-2 (copied from the header)
64967 ** 16: Checksum-1.
64968 ** 20: Checksum-2.
64973 ** (1) The salt-1 and salt-2 values in the frame-header match
64974 ** salt values in the wal-header
64976 ** (2) The checksum values in the final 8 bytes of the frame-header
64981 ** The checksum is computed using 32-bit big-endian integers if the
64983 ** is computed using little-endian if the magic number is 0x377f0682.
64985 ** big-endian format regardless of which byte order is used to compute
64987 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
64990 ** for i from 0 to n-1 step 2:
64997 ** of the sequence being summed.) The s1 value spans all 32-bit
65000 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
65001 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
65002 ** The VFS.xSync operations serve as write barriers - all writes launched
65006 ** After each checkpoint, the salt-1 value is incremented and the salt-2
65035 ** data structure called the wal-index is maintained to expedite the
65038 ** WAL-INDEX FORMAT
65040 ** Conceptually, the wal-index is shared memory, though VFS implementations
65041 ** might choose to implement the wal-index using a mmapped file. Because
65042 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
65046 ** In the default unix and windows implementation, the wal-index is a mmapped
65047 ** file whose name is the database name with a "-shm" suffix added. For that
65048 ** reason, the wal-index is sometimes called the "shm" file.
65050 ** The wal-index is transient. After a crash, the wal-index can (and should
65052 ** to either truncate or zero the header of the wal-index when the last
65053 ** connection to it closes. Because the wal-index is transient, it can
65054 ** use an architecture-specific format; it does not have to be cross-platform.
65056 ** as big endian, the wal-index can store multi-byte values in the native
65059 ** The purpose of the wal-index is to answer this question quickly: Given
65064 ** The wal-index consists of a header region, followed by an one or
65067 ** The wal-index header contains the total number of frames within the WAL
65073 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
65075 ** wal-index. The values are:
65080 ** Each index block contains two sections, a page-mapping that contains the
65081 ** database page number associated with each wal frame, and a hash-table
65083 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
65084 ** for the first index block) 32-bit page numbers. The first entry in the
65085 ** first index-block contains the database page number corresponding to the
65090 ** The last index block in a wal-index usually contains less than the full
65091 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
65093 ** allocated size of the page-mapping array - the page-mapping array merely
65097 ** can be found by scanning the page-mapping sections of each index block
65103 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
65108 ** 1-based index of an entry in the mapping section of the same
65109 ** index block. Let K be the 1-based index of the largest entry in
65124 ** wrap-around.) Because the hash table is never more than half full,
65129 ** current index block. Otherwise the iMax-th mapping entry of the
65147 ** and to the wal-index) might be using a different value K1, where K1>K0.
65153 ** in the first place - which is what reader one wants. Meanwhile, the
65169 SQLITE_PRIVATE int sqlite3WalTrace = 0;
65176 ** The maximum (and only) versions of the wal and wal-index formats
65180 ** values in the wal-header are correct and (b) the version field is not
65183 ** Similarly, if a client successfully reads a wal-index header (i.e. the
65185 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
65200 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
65210 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
65220 ** The following object holds a copy of the wal-index header content.
65222 ** The actual header in the wal-index consists of two copies of this
65229 ** Or it can be 1 to represent a 65536-byte page. The latter case was
65233 u32 iVersion; /* Wal-index version */
65237 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
65247 ** A copy of the following object occurs in the wal-index immediately
65270 ** holds read-lock K, then the value in aReadMark[K] is no greater than
65273 ** a special case; its value is never used and it exists as a place-holder
65302 ** We assume that 32-bit loads are atomic and so no locks are needed in
65315 ** This is a schematic view of the complete 136-byte header of the
65316 ** wal-index file (also known as the -shm file):
65318 ** +-----------------------------+
65320 ** +-----------------------------+ |
65322 ** +-----------------------------+ |
65324 ** +-------+-------+-------------+ |
65326 ** +-------+-------+-------------+ |
65328 ** +-----------------------------+ | WalIndexHdr object
65330 ** +-----------------------------+ |
65333 ** +-----------------------------+ |
65336 ** +-----------------------------+ |
65339 ** +-----------------------------+
65341 ** +-----------------------------+ |
65343 ** +-----------------------------+ |
65345 ** +-------+-------+-------------+ |
65347 ** +-------+-------+-------------+ | Second copy of the
65349 ** +-----------------------------+ |
65351 ** +-----------------------------+ |
65354 ** +-----------------------------+ |
65357 ** +-----------------------------+ |
65360 ** +-----------------------------+
65362 ** +-----------------------------+
65368 ** +-------+-------+------+------+
65370 ** +-------+-------+------+------+ ) 8 lock bytes
65372 ** +-------+-------+------+------+
65374 ** +-----------------------------+
65376 ** +-----------------------------+
65381 ** only support mandatory file-locks, we do not read or write data
65394 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
65395 ** big-endian format in the first 4 bytes of a WAL file.
65398 ** file are calculated by treating all data as an array of 32-bit
65399 ** big-endian words. Otherwise, they are calculated by interpreting
65400 ** all data as 32-bit little-endian words.
65405 ** Return the offset of frame iFrame in the write-ahead log file,
65407 ** is to the start of the write-ahead log frame-header.
65410 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
65414 ** An open write-ahead log file is represented by an instance of the
65423 int nWiData; /* Size of array apWiData */
65424 int szFirstBlock; /* Size of first block written to WAL file */
65425 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
65427 i16 readLock; /* Which read lock is being held. -1 for none */
65429 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
65436 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
65437 WalIndexHdr hdr; /* Wal-index header for current transaction */
65441 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
65446 int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
65447 int iSysErrno; /* System error code following exception */
65450 int nSehTry; /* Number of nested SEH_TRY{} blocks */
65476 ** Each page of the wal-index mapping contains a hash-table made up of
65490 ** walIteratorInit() - Create a new iterator,
65491 ** walIteratorNext() - Step an iterator,
65492 ** walIteratorFree() - Free an iterator.
65498 int nSegment; /* Number of entries in aSegment[] */
65500 int iNext; /* Next slot in aIndex[] not yet returned */
65503 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
65504 int iZero; /* Frame number associated with aPgno[0] */
65505 } aSegment[1]; /* One for every 32KB page in the wal-index */
65509 ** Define the parameters of the hash tables in the wal-index file. There
65510 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
65511 ** wal-index.
65513 ** Changing any of these constants will alter the wal-index format and
65521 ** The block of page numbers associated with the first hash-table in a
65522 ** wal-index is smaller than usual. This is so that there is a complete
65523 ** hash-table on each aligned 32KB page of the wal-index.
65525 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
65527 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
65533 ** Structured Exception Handling (SEH) is a Windows-specific technique
65534 ** for catching exceptions raised while accessing memory-mapped files.
65536 ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
65537 ** deal with system-level errors that arise during WAL -shm file processing.
65538 ** Without this compile-time option, any system-level faults that appear
65539 ** while accessing the memory-mapped -shm file will cause a process-wide
65548 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
65549 VVA_ONLY(pWal->nSehTry++);
65553 VVA_ONLY(pWal->nSehTry--); \
65554 assert( pWal->nSehTry==0 ); \
65557 /* Simulate a memory-mapping fault in the -shm file for testing purposes */
65563 ** indicates that the exception may have been caused by accessing the *-shm
65566 static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
65567 VVA_ONLY(pWal->nSehTry--);
65569 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
65571 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
65575 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
65585 ** thrown by the system if the *-shm file mapping is accessed after it
65589 int res;
65590 assert( pWal->nSehTry>0 );
65616 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
65619 ** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
65628 #define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
65631 # define SEH_TRY VVA_ONLY(pWal->nSehTry++);
65632 # define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
65633 # define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
65640 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
65641 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
65644 ** If the wal-index is currently smaller the iPage pages then the size
65645 ** of the wal-index might be increased, but only if it is safe to do
65646 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
65647 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
65651 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
65655 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
65657 static SQLITE_NOINLINE int walIndexPageRealloc(
65659 int iPage, /* The page we seek */
65662 int rc = SQLITE_OK;
65664 /* Enlarge the pWal->apWiData[] array if required */
65665 if( pWal->nWiData<=iPage ){
65668 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
65673 memset((void*)&apNew[pWal->nWiData], 0,
65674 sizeof(u32*)*(iPage+1-pWal->nWiData));
65675 pWal->apWiData = apNew;
65676 pWal->nWiData = iPage+1;
65680 assert( pWal->apWiData[iPage]==0 );
65681 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
65682 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
65683 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
65685 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
65686 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
65688 assert( pWal->apWiData[iPage]!=0
65690 || (pWal->writeLock==0 && iPage==0) );
65691 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
65695 pWal->readOnly |= WAL_SHM_RDONLY;
65702 *ppPage = pWal->apWiData[iPage];
65706 static int walIndexPage(
65708 int iPage, /* The page we seek */
65712 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
65719 ** Return a pointer to the WalCkptInfo structure in the wal-index.
65722 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65724 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
65728 ** Return a pointer to the WalIndexHdr structure in the wal-index.
65731 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65733 return (volatile WalIndexHdr*)pWal->apWiData[0];
65737 ** The argument to this macro must be of type u32. On a little-endian
65739 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
65741 ** of the input value as a little-endian integer.
65758 int nativeCksum, /* True for native byte-order, false for non-native */
65760 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
65822 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65823 sqlite3OsShmBarrier(pWal->pDbFd);
65828 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
65830 ** might give false-positive TSAN warnings.
65832 ** See tag-20200519-1.
65841 ** Write the header information in pWal->hdr into the wal-index.
65843 ** The checksum on pWal->hdr is updated before it is written.
65847 const int nCksum = offsetof(WalIndexHdr, aCksum);
65849 assert( pWal->writeLock );
65850 pWal->hdr.isInit = 1;
65851 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
65852 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
65853 /* Possible TSAN false-positive. See tag-20200519-1 */
65854 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65856 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65861 ** supplied by the caller. A frame-header is made up of a series of
65862 ** 4-byte big-endian integers, as follows:
65867 ** 8: Salt-1 (copied from the wal-header)
65868 ** 12: Salt-2 (copied from the wal-header)
65869 ** 16: Checksum-1.
65870 ** 20: Checksum-2.
65873 Wal *pWal, /* The write-ahead log */
65875 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
65879 int nativeCksum; /* True for native byte-order checksums */
65880 u32 *aCksum = pWal->hdr.aFrameCksum;
65884 if( pWal->iReCksum==0 ){
65885 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
65887 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65889 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65903 static int walDecodeFrame(
65904 Wal *pWal, /* The write-ahead log */
65910 int nativeCksum; /* True for native byte-order checksums */
65911 u32 *aCksum = pWal->hdr.aFrameCksum;
65915 /* A frame is only valid if the salt values in the frame-header
65916 ** match the salt values in the wal-header.
65918 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
65930 ** all prior frames, the first 16 bytes of this frame-header,
65931 ** and the frame-data matches the checksum in the last 8
65932 ** bytes of this frame-header.
65934 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65936 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65958 static const char *walLockName(int lockIdx){
65960 return "WRITE-LOCK";
65962 return "CKPT-LOCK";
65964 return "RECOVER-LOCK";
65967 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
65968 lockIdx-WAL_READ_LOCK(0));
65977 ** A lock cannot be moved directly between shared and exclusive - it must go
65980 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
65982 static int walLockShared(Wal *pWal, int lockIdx){
65983 int rc;
65984 if( pWal->exclusiveMode ) return SQLITE_OK;
65985 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
65987 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
65989 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65991 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
65995 static void walUnlockShared(Wal *pWal, int lockIdx){
65996 if( pWal->exclusiveMode ) return;
65997 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66000 pWal->lockMask &= ~(1 << lockIdx);
66002 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
66004 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
66005 int rc;
66006 if( pWal->exclusiveMode ) return SQLITE_OK;
66007 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66009 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
66011 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66014 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
66019 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
66020 if( pWal->exclusiveMode ) return;
66021 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66024 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
66026 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
66032 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
66035 static int walHash(u32 iPage){
66037 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
66038 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
66040 static int walNextHash(int iPriorHash){
66041 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
66046 ** of a page hash table in the wal-index. This becomes the return value
66051 volatile ht_slot *aHash; /* Start of the wal-index hash table */
66058 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
66061 ** Set output variable pLoc->aHash to point to the start of the hash table
66062 ** in the wal-index file. Set pLoc->iZero to one less than the frame
66065 ** (pLoc->iZero+N) in the log.
66067 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
66068 ** first frame indexed by the hash table, frame (pLoc->iZero).
66070 static int walHashGet(
66072 int iHash, /* Find the iHash'th table */
66075 int rc; /* Return code */
66077 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
66080 if( pLoc->aPgno ){
66081 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
66083 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
66084 pLoc->iZero = 0;
66086 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
66095 ** Return the number of the wal-index page that contains the hash-table
66096 ** and page-number array that contain entries corresponding to WAL frame
66097 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
66100 static int walFramePage(u32 iFrame){
66101 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
66116 int iHash = walFramePage(iFrame);
66119 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
66121 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
66126 ** than pWal->hdr.mxFrame.
66128 ** This function is called whenever pWal->hdr.mxFrame is decreased due
66131 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
66133 ** pWal->hdr.mxFrame advances to the point where those hash tables are
66138 int iLimit = 0; /* Zero values greater than this */
66139 int nByte; /* Number of bytes to zero in aPgno[] */
66140 int i; /* Used to iterate through aHash[] */
66142 assert( pWal->writeLock );
66143 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
66144 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
66145 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
66147 if( pWal->hdr.mxFrame==0 ) return;
66149 /* Obtain pointers to the hash-table and page-number array containing
66150 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
66151 ** that the page said hash-table and array reside on is already mapped.(1)
66153 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
66154 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
66155 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
66156 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
66158 /* Zero all hash-table entries that correspond to frame numbers greater
66159 ** than pWal->hdr.mxFrame.
66161 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
66170 ** frame numbers greater than pWal->hdr.mxFrame.
66172 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
66181 int j; /* Loop counter */
66182 int iKey; /* Hash key */
66195 ** Set an entry in the wal-index that will map database page number
66198 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
66199 int rc; /* Return code */
66200 WalHashLoc sLoc; /* Wal-index hash table location */
66204 /* Assuming the wal-index file was successfully mapped, populate the
66208 int iKey; /* Hash table key */
66209 int idx; /* Value to write to hash-table slot */
66210 int nCollide; /* Number of hash collisions */
66212 idx = iFrame - sLoc.iZero;
66215 /* If this is the first entry to be added to this hash-table, zero the
66219 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
66228 ** the hash-table before writing any new entries.
66230 if( sLoc.aPgno[idx-1] ){
66232 assert( !sLoc.aPgno[idx-1] );
66235 /* Write the aPgno[] array entry and the hash-table slot. */
66238 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
66240 sLoc.aPgno[idx-1] = iPage;
66248 int i; /* Loop counter */
66249 int nEntry = 0; /* Number of entries in the hash table */
66256 ** thing to check, so only do this occasionally - not on every
66260 int i; /* Loop counter */
66278 ** Recover the wal-index by reading the write-ahead log file.
66281 ** wal-index to prevent other threads/processes from doing anything
66282 ** with the WAL or wal-index while recovery is running. The
66287 static int walIndexRecover(Wal *pWal){
66288 int rc; /* Return Code */
66291 int iLock; /* Lock offset to lock for checkpoint */
66299 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
66302 assert( pWal->writeLock );
66303 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
66304 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
66311 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
66313 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
66320 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
66322 int szFrame; /* Number of bytes in buffer aFrame[] */
66324 int szPage; /* Page size according to the log */
66327 int isValid; /* True if this frame is valid */
66328 u32 iPg; /* Current 32KB wal-index page */
66332 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
66345 || szPage&(szPage-1)
66351 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
66352 pWal->szPage = szPage;
66353 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
66354 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
66357 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
66358 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
66360 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
66361 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
66386 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
66391 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
66397 pWal->apWiData[iPg] = aPrivate;
66405 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
66412 /* If nTruncate is non-zero, this is a commit record. */
66414 pWal->hdr.mxFrame = iFrame;
66415 pWal->hdr.nPage = nTruncate;
66416 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
66419 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
66420 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
66423 pWal->apWiData[iPg] = aShare;
66436 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
66444 int i;
66466 int i;
66467 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
66468 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
66471 /* Reset the checkpoint-header. This is safe because this thread is
66473 ** checkpointers. Then set the values of read-mark slots 1 through N.
66476 pInfo->nBackfill = 0;
66477 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
66478 pInfo->aReadMark[0] = 0;
66482 if( i==1 && pWal->hdr.mxFrame ){
66483 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
66485 pInfo->aReadMark[i] = READMARK_NOT_USED;
66499 if( pWal->hdr.nPage ){
66502 pWal->hdr.mxFrame, pWal->zWalName
66509 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
66514 ** Close an open wal-index.
66516 static void walIndexClose(Wal *pWal, int isDelete){
66517 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
66518 int i;
66519 for(i=0; i<pWal->nWiData; i++){
66520 sqlite3_free((void *)pWal->apWiData[i]);
66521 pWal->apWiData[i] = 0;
66524 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
66525 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
66536 ** client from unlinking the WAL or wal-index file. If another process
66538 ** system would be badly broken.
66544 SQLITE_PRIVATE int sqlite3WalOpen(
66545 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
66548 int bNoShm, /* True to run in heap-memory mode */
66552 int rc; /* Return Code */
66554 int flags; /* Flags passed to OsOpen() */
66560 ** of these constants would result in an incompatible on-disk format
66561 ** for the -shm file. Any change that causes one of these asserts to
66565 ** This table also serves as a helpful cross-reference when trying to
66566 ** interpret hex dumps of the -shm file.
66606 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
66611 pRet->pVfs = pVfs;
66612 pRet->pWalFd = (sqlite3_file *)&pRet[1];
66613 pRet->pDbFd = pDbFd;
66614 pRet->readLock = -1;
66615 pRet->mxWalSize = mxWalSize;
66616 pRet->zWalName = zWalName;
66617 pRet->syncHeader = 1;
66618 pRet->padToSectorBoundary = 1;
66619 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
66621 /* Open file handle on the write-ahead log file. */
66623 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
66625 pRet->readOnly = WAL_RDONLY;
66630 sqlite3OsClose(pRet->pWalFd);
66633 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
66634 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
66636 pRet->padToSectorBoundary = 0;
66648 if( pWal ) pWal->mxWalSize = iLimit;
66661 static int walIteratorNext(
66668 int i; /* For looping through segments */
66670 iMin = p->iPrior;
66672 for(i=p->nSegment-1; i>=0; i--){
66673 struct WalSegment *pSegment = &p->aSegment[i];
66674 while( pSegment->iNext<pSegment->nEntry ){
66675 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
66679 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
66683 pSegment->iNext++;
66687 *piPage = p->iPrior = iRet;
66715 const u32 *aContent, /* Pages in wal - keys for the sort */
66717 int nLeft, /* IN: Elements in array *paLeft */
66719 int *pnRight, /* IN/OUT: Elements in *paRight */
66722 int iLeft = 0; /* Current index in aLeft */
66723 int iRight = 0; /* Current index in aRight */
66724 int iOut = 0; /* Current index in output buffer */
66725 int nRight = *pnRight;
66775 int *pnList /* IN/OUT: Number of elements in aList[] */
66778 int nList; /* Number of elements in aList */
66779 ht_slot *aList; /* Pointer to sub-list content */
66782 const int nList = *pnList; /* Size of input list */
66783 int nMerge = 0; /* Number of elements in list aMerge */
66785 int iList; /* Index into input list */
66787 struct Sublist aSub[13]; /* Array of sub-lists */
66791 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
66800 assert( p->aList && p->nList<=(1<<iSub) );
66801 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
66802 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66813 assert( p->nList<=(1<<iSub) );
66814 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
66815 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66823 int i;
66825 assert( aContent[aList[i]] > aContent[aList[i-1]] );
66841 ** nBackfill or earlier may be included - excluding them is an optimization
66851 static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
66853 int nSegment; /* Number of segments to merge */
66856 int i; /* Iterator variable */
66857 ht_slot *aTmp; /* Temp space used by merge-sort */
66858 int rc = SQLITE_OK; /* Return Code */
66863 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
66864 iLast = pWal->hdr.mxFrame;
66869 + (nSegment-1)*sizeof(struct WalSegment)
66878 p->nSegment = nSegment;
66886 int j; /* Counter variable */
66887 int nEntry; /* Number of entries in this segment */
66891 nEntry = (int)(iLast - sLoc.iZero);
66893 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
66895 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
66902 p->aSegment[i].iZero = sLoc.iZero;
66903 p->aSegment[i].nEntry = nEntry;
66904 p->aSegment[i].aIndex = aIndex;
66905 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
66924 static int walEnableBlockingMs(Wal *pWal, int nMs){
66925 int rc = sqlite3OsFileControl(
66926 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs
66934 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
66937 static int walEnableBlocking(Wal *pWal){
66938 int res = 0;
66939 if( pWal->db ){
66940 int tmout = pWal->db->busyTimeout;
66952 int tmout = 0;
66953 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
66965 SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){
66966 int rc = SQLITE_OK;
66967 assert( pWal->readLock<0 || bLock==0 );
66969 assert( pWal->db );
66973 pWal->writeLock = 1;
66977 }else if( pWal->writeLock ){
66979 pWal->writeLock = 0;
66988 pWal->db = db;
67002 ** busy-handler function. Invoke it and retry the lock until either the
67003 ** lock is successfully obtained or the busy-handler returns 0.
67005 static int walBusyLock(
67007 int (*xBusy)(void*), /* Function to call when busy */
67009 int lockIdx, /* Offset of first byte to lock */
67010 int n /* Number of bytes to lock */
67012 int rc;
67026 ** The cache of the wal-index header must be valid to call this function.
67027 ** Return the page-size in bytes used by the database.
67029 static int walPagesize(Wal *pWal){
67030 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67039 ** file - there are no readers that may attempt to read a frame from
67042 ** This function updates the shared-memory structures so that the next
67047 ** new wal-index header. It should be passed a pseudo-random value (i.e.
67052 int i; /* Loop counter */
67053 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
67054 pWal->nCkpt++;
67055 pWal->hdr.mxFrame = 0;
67057 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
67059 AtomicStore(&pInfo->nBackfill, 0);
67060 pInfo->nBackfillAttempted = 0;
67061 pInfo->aReadMark[1] = 0;
67062 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
67063 assert( pInfo->aReadMark[0]==0 );
67075 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
67081 ** in the WAL and can be recovered following a power-loss or hard reset.
67088 ** This routine uses and updates the nBackfill field of the wal-index header.
67097 static int walCheckpoint(
67100 int eMode, /* One of PASSIVE, FULL or RESTART */
67101 int (*xBusy)(void*), /* Function to call when busy */
67103 int sync_flags, /* Flags for OsSync() (or 0) */
67106 int rc = SQLITE_OK; /* Return code */
67107 int szPage; /* Database page-size */
67113 int i; /* Loop counter */
67120 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67122 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67131 mxSafeFrame = pWal->hdr.mxFrame;
67132 mxPage = pWal->hdr.nPage;
67134 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
67136 assert( y<=pWal->hdr.mxFrame );
67140 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
67152 if( pInfo->nBackfill<mxSafeFrame ){
67153 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
67160 u32 nBackfill = pInfo->nBackfill;
67161 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
67164 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67172 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
67173 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
67175 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
67178 ** maximum size of the pending-byte page (65536 bytes), then
67182 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
67193 if( AtomicLoad(&db->u1.isInterrupted) ){
67194 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
67202 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
67204 iOffset = (iDbpage-1)*(i64)szPage;
67206 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
67209 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
67213 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
67214 i64 szDb = pWal->hdr.nPage*(i64)szPage;
67216 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
67218 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
67222 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
67243 assert( pWal->writeLock );
67245 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67250 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
67251 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
67254 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
67260 ** wal-index header in shared memory, as all subsequent reader or
67264 ** the wal-index header do not match the contents of the
67265 ** file-system. To avoid this, update the wal-index header to
67268 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
67270 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67287 int rx;
67289 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
67291 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
67295 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
67302 ** an exception thrown by reading from the *-shm mapping after it has become
67317 ** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
67321 static int walHandleException(Wal *pWal){
67322 if( pWal->exclusiveMode==0 ){
67323 static const int S = 1;
67324 static const int E = (1<<SQLITE_SHM_NLOCK);
67325 int ii;
67326 u32 mUnlock = pWal->lockMask & ~(
67327 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
67328 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
67329 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
67336 sqlite3_free(pWal->pFree);
67337 pWal->pFree = 0;
67338 if( pWal->pWiValue ){
67339 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
67340 pWal->pWiValue = 0;
67352 static int walAssertLockmask(Wal *pWal){
67353 if( pWal->exclusiveMode==0 ){
67354 static const int S = 1;
67355 static const int E = (1<<SQLITE_SHM_NLOCK);
67357 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
67358 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
67359 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
67361 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
67364 assert( mExpect==pWal->lockMask );
67373 SQLITE_PRIVATE int sqlite3WalSystemErrno(Wal *pWal){
67374 int iRet = 0;
67376 iRet = pWal->iSysErrno;
67377 pWal->iSysErrno = 0;
67389 SQLITE_PRIVATE int sqlite3WalClose(
67392 int sync_flags, /* Flags to pass to OsSync() (or 0) */
67393 int nBuf,
67396 int rc = SQLITE_OK;
67398 int isDelete = 0; /* True to unlink wal and wal-index files */
67403 ** ordinary, rollback-mode locking methods, this guarantees that the
67406 ** the wal and wal-index files.
67411 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
67413 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
67414 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67420 int bPersist = -1;
67422 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
67426 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
67429 }else if( pWal->mxWalSize>=0 ){
67433 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
67442 sqlite3OsClose(pWal->pWalFd);
67445 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
67449 sqlite3_free((void *)pWal->apWiData);
67456 ** Try to read the wal-index header. Return 0 on success and 1 if
67459 ** The wal-index is in shared memory. Another thread or process might
67466 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
67469 ** If the checksum cannot be verified return non-zero. If the header
67472 static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
67477 /* The first page of the wal-index must be mapped at this point. */
67478 assert( pWal->nWiData>0 && pWal->apWiData[0] );
67483 ** from the file. If this happens, return non-zero.
67485 ** tag-20200519-1:
67486 ** There are two copies of the header at the beginning of the wal-index.
67490 ** give false-positive warnings about these accesses because the tools do not
67491 ** account for the double-read and the memory barrier. The use of mutexes
67497 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
67505 return 1; /* Malformed header - probably all zeros */
67507 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
67512 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
67514 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
67515 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67516 testcase( pWal->szPage<=32768 );
67517 testcase( pWal->szPage>=65536 );
67528 #define WAL_RETRY (-1)
67531 ** Read the wal-index header from the wal-index and into pWal->hdr.
67532 ** If the wal-header appears to be corrupt, try to reconstruct the
67533 ** wal-index from the WAL before returning.
67535 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
67536 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
67539 ** If the wal-index header is successfully read, return SQLITE_OK.
67542 static int walIndexReadHdr(Wal *pWal, int *pChanged){
67543 int rc; /* Return code */
67544 int badHdr; /* True if a header read failed */
67545 volatile u32 *page0; /* Chunk of wal-index containing header */
67547 /* Ensure that page 0 of the wal-index (the page that contains the
67548 ** wal-index header) is mapped. Return early if an error occurs here.
67555 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
67557 ** confirm that another write-capable connection has the shared-memory
67558 ** open, and hence the content of the shared-memory is unreliable,
67559 ** since the shared-memory might be inconsistent with the WAL file
67562 assert( pWal->writeLock==0 );
67563 assert( pWal->readOnly & WAL_SHM_RDONLY );
67564 pWal->bShmUnreliable = 1;
67565 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
67568 return rc; /* Any other non-OK return is just an error */
67571 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
67575 assert( page0!=0 || pWal->writeLock==0 );
67577 /* If the first page of the wal-index has been mapped, try to read the
67578 ** wal-index header immediately, without holding any lock. This usually
67579 ** works, but may fail if the wal-index header is corrupt or currently
67588 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
67594 int bWriteLock = pWal->writeLock;
67598 pWal->writeLock = 1;
67602 /* If the wal-index header is still malformed even while holding
67612 pWal->writeLock = 0;
67620 ** sure the wal-index was not constructed with some future format that
67623 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
67626 if( pWal->bShmUnreliable ){
67629 pWal->bShmUnreliable = 0;
67630 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
67636 pWal->exclusiveMode = WAL_NORMAL_MODE;
67643 ** Open a transaction in a connection where the shared-memory is read-only
67644 ** and where we cannot verify that there is a separate write-capable connection
67645 ** on hand to keep the shared-memory up-to-date with the WAL file.
67647 ** This can happen, for example, when the shared-memory is implemented by
67648 ** memory-mapping a *-shm file, where a prior writer has shut down and
67649 ** left the *-shm file on disk, and now the present connection is trying
67650 ** to use that database but lacks write permission on the *-shm file.
67655 ** The *-wal file has been read and an appropriate wal-index has been
67656 ** constructed in pWal->apWiData[] using heap memory instead of shared
67663 ** WAL_RETRY, then the heap memory wal-index has been discarded and
67665 ** beginning (including attempting to map the *-shm file).
67669 static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
67674 int szFrame; /* Number of bytes in buffer aFrame[] */
67677 int rc; /* Return code */
67678 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
67680 assert( pWal->bShmUnreliable );
67681 assert( pWal->readOnly & WAL_SHM_RDONLY );
67682 assert( pWal->nWiData>0 && pWal->apWiData[0] );
67692 pWal->readLock = 0;
67694 /* Check to see if a separate writer has attached to the shared-memory area,
67695 ** thus making the shared-memory "reliable" again. Do this by invoking
67699 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
67700 ** cause the heap-memory WAL-index to be discarded and the actual
67712 ** even if some external agent does a "chmod" to make the shared-memory
67716 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
67717 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
67723 /* We reach this point only if the real shared-memory is still unreliable.
67724 ** Assume the in-memory WAL-index substitute is correct and load it
67725 ** into pWal->hdr.
67727 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
67732 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
67737 /* If the wal file is too small to contain a wal-header and the
67738 ** wal-index header has mxFrame==0, then it must be safe to proceed
67744 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
67749 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
67753 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
67755 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
67762 assert( (pWal->szPage & (pWal->szPage-1))==0 );
67763 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
67764 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
67773 ** wal file since the heap-memory wal-index was created. If so, the
67774 ** heap-memory wal-index is discarded and WAL_RETRY returned to
67776 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
67777 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
67778 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
67786 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67790 /* If nTruncate is non-zero, then a complete transaction has been
67798 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
67799 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
67804 int i;
67805 for(i=0; i<pWal->nWiData; i++){
67806 sqlite3_free((void*)pWal->apWiData[i]);
67807 pWal->apWiData[i] = 0;
67809 pWal->bShmUnreliable = 0;
67817 ** The final argument passed to walTryBeginRead() is of type (int*). The
67820 ** int cnt = 0;
67829 ** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
67830 ** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
67859 ** to make a copy of the wal-index header into pWal->hdr. If the
67860 ** wal-index header has changed, *pChanged is set to 1 (as an indication
67862 ** flushed.) When useWal==1, the wal-index header is assumed to already
67873 ** bad luck when there is lots of contention for the wal-index, but that
67877 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
67878 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
67881 ** including frame number aReadMark[pWal->readLock]. The reader will
67882 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
67883 ** Or if pWal->readLock==0, then the reader will ignore the WAL
67886 ** this routine will always set pWal->readLock>0 on success.
67888 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
67897 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
67898 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
67900 int mxI; /* Index of largest aReadMark[] value */
67901 int i; /* Loop counter */
67902 int rc = SQLITE_OK; /* Return code */
67905 int nBlockTmout = 0;
67908 assert( pWal->readLock<0 ); /* Not currently locked */
67911 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
67919 ** paged out or take a page-fault that is time-consuming to resolve,
67932 int nDelay = 1; /* Pause time in microseconds */
67933 int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK);
67935 VVA_ONLY( pWal->lockError = 1; )
67938 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
67940 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
67945 ** read-mark.
67956 sqlite3OsSleep(pWal->pVfs, nDelay);
67962 if( pWal->bShmUnreliable==0 ){
67981 if( pWal->apWiData[0]==0 ){
67986 ** code that determines whether or not the shared-memory region
68000 else if( pWal->bShmUnreliable ){
68005 assert( pWal->nWiData>0 );
68006 assert( pWal->apWiData[0]!=0 );
68009 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68011 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
68020 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
68037 pWal->readLock = 0;
68047 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68051 mxFrame = pWal->hdr.mxFrame;
68053 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68054 mxFrame = pWal->pSnapshot->mxFrame;
68058 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68065 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
68071 AtomicStore(pInfo->aReadMark+i,mxFrame);
68082 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
68100 /* Now that the read-lock has been obtained, check that neither the
68101 ** value in the aReadMark[] array or the contents of the wal-index
68104 ** It is necessary to check that the wal-index header did not change
68105 ** between the time it was read and when the shared-lock was obtained
68108 ** that occur later in the log than pWal->hdr.mxFrame may have been
68111 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68114 ** Before checking that the live wal-index header has not changed
68117 ** to read any frames earlier than minFrame from the wal file - they
68121 ** nBackfill and checking that the wal-header in shared-memory still
68122 ** matches the one cached in pWal->hdr, it is guaranteed that the
68123 ** checkpointer that set nBackfill was not working with a wal-index
68124 ** header newer than that cached in pWal->hdr. If it were, that could
68126 ** a version of page X that lies before pWal->minFrame (call that version
68129 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68132 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68134 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
68136 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
68137 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68142 assert( mxReadMark<=pWal->hdr.mxFrame );
68143 pWal->readLock = (i16)mxI;
68152 static int walSnapshotRecover(
68154 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
68155 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
68157 int szPage = (int)pWal->szPage;
68158 int rc;
68161 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
68164 u32 i = pInfo->nBackfillAttempted;
68165 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
68173 assert( i - sLoc.iZero - 1 >=0 );
68174 pgno = sLoc.aPgno[i-sLoc.iZero-1];
68175 iDbOff = (i64)(pgno-1) * szPage;
68179 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
68182 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
68190 pInfo->nBackfillAttempted = i-1;
68205 ** This is only really safe if the file-system is such that any page
68216 SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
68217 int rc;
68219 assert( pWal->readLock>=0 );
68222 void *pBuf1 = sqlite3_malloc(pWal->szPage);
68223 void *pBuf2 = sqlite3_malloc(pWal->szPage);
68227 pWal->ckptLock = 1;
68232 pWal->ckptLock = 0;
68248 static int walBeginReadTransaction(Wal *pWal, int *pChanged){
68249 int rc; /* Return code */
68250 int cnt = 0; /* Number of TryBeginRead attempts */
68252 int ckptLock = 0;
68253 int bChanged = 0;
68254 WalIndexHdr *pSnapshot = pWal->pSnapshot;
68257 assert( pWal->ckptLock==0 );
68258 assert( pWal->nSehTry>0 );
68262 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68270 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
68273 ** before checking pInfo->nBackfillAttempted. */
68295 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68297 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
68298 ** is populated with the wal-index header corresponding to the head
68306 ** pSnapshot->mxFrame into the database file. Note that the
68311 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
68312 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
68316 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
68317 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
68320 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
68321 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
68323 assert( pWal->readLock>0 );
68324 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
68330 /* A client using a non-current snapshot may not ignore any frames
68336 pWal->minFrame = 1;
68357 ** it takes a snapshot of the state of the WAL and wal-index for the current
68359 ** Other threads might append new content to the WAL and wal-index but
68367 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
68368 int rc;
68378 ** read-lock.
68382 if( pWal->readLock>=0 ){
68383 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
68384 pWal->readLock = -1;
68396 static int walFindFrame(
68402 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
68403 int iHash; /* Used to loop through N hash tables */
68404 int iMinHash;
68407 assert( pWal->readLock>=0 || pWal->lockError );
68409 /* If the "last page" field of the wal-index header snapshot is 0, then
68411 ** in this case as an optimization. Likewise, if pWal->readLock==0,
68415 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
68425 ** that adds entries to the wal-index (and possibly to this hash
68429 ** read transaction was opened may have been written incorrectly -
68436 ** if we had exclusive access to the hash-table:
68439 ** This condition filters out normal hash-table collisions.
68443 ** table after the current read-transaction had started.
68445 iMinHash = walFramePage(pWal->minFrame);
68446 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
68448 int iKey; /* Hash slot index */
68449 int nCollide; /* Number of hash collisions remaining */
68450 int rc; /* Error code */
68462 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
68466 if( (nCollide--)==0 ){
68477 ** of the wal-index file content. Make sure the results agree with the
68482 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
68483 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
68508 SQLITE_PRIVATE int sqlite3WalFindFrame(
68513 int rc;
68526 SQLITE_PRIVATE int sqlite3WalReadFrame(
68529 int nOut, /* Size of buffer pOut in bytes */
68532 int sz;
68534 sz = pWal->hdr.szPage;
68540 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
68547 if( pWal && ALWAYS(pWal->readLock>=0) ){
68548 return pWal->hdr.nPage;
68567 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
68568 int rc;
68571 /* If the write-lock is already held, then it was obtained before the
68572 ** read-transaction was even opened, making this call a no-op.
68574 if( pWal->writeLock ){
68575 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
68582 assert( pWal->readLock>=0 );
68583 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
68585 if( pWal->readOnly ){
68596 pWal->writeLock = 1;
68603 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
68611 pWal->writeLock = 0;
68620 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
68621 if( pWal->writeLock ){
68623 pWal->writeLock = 0;
68624 pWal->iReCksum = 0;
68625 pWal->truncateOnCommit = 0;
68632 ** function moves the write-pointer back to the start of the transaction.
68642 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
68643 int rc = SQLITE_OK;
68644 if( ALWAYS(pWal->writeLock) ){
68645 Pgno iMax = pWal->hdr.mxFrame;
68649 /* Restore the clients cache of the wal-index header to the state it
68652 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
68654 for(iFrame=pWal->hdr.mxFrame+1;
68660 ** (b) has an outstanding reference, then xUndo is either a no-op
68672 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
68686 assert( pWal->writeLock );
68687 aWalData[0] = pWal->hdr.mxFrame;
68688 aWalData[1] = pWal->hdr.aFrameCksum[0];
68689 aWalData[2] = pWal->hdr.aFrameCksum[1];
68690 aWalData[3] = pWal->nCkpt;
68699 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
68700 int rc = SQLITE_OK;
68702 assert( pWal->writeLock );
68703 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
68705 if( aWalData[3]!=pWal->nCkpt ){
68706 /* This savepoint was opened immediately after the write-transaction
68711 aWalData[3] = pWal->nCkpt;
68714 if( aWalData[0]<pWal->hdr.mxFrame ){
68715 pWal->hdr.mxFrame = aWalData[0];
68716 pWal->hdr.aFrameCksum[0] = aWalData[1];
68717 pWal->hdr.aFrameCksum[1] = aWalData[2];
68732 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
68736 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
68739 static int walRestartLog(Wal *pWal){
68740 int rc = SQLITE_OK;
68741 int cnt;
68743 if( pWal->readLock==0 ){
68745 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
68746 if( pInfo->nBackfill>0 ){
68749 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68754 ** wal-index header to reflect this.
68757 ** at this point. But updating the actual wal-index header is also
68761 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68767 pWal->readLock = -1;
68770 int notUsed;
68783 ** the next fsync should occur - passed from sqlite3WalFrames() into
68790 int syncFlags; /* Flags for the fsync */
68791 int szPage; /* Size of one page */
68796 ** Do a sync when crossing the p->iSyncPoint boundary.
68802 static int walWriteToLog(
68805 int iAmt, /* Number of bytes to write */
68808 int rc;
68809 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
68810 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
68811 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
68814 iAmt -= iFirstAmt;
68816 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
68817 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
68820 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
68827 static int walWriteOneFrame(
68830 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
68833 int rc; /* Result code from subfunctions */
68835 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
68836 pData = pPage->pData;
68837 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
68841 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
68853 static int walRewriteChecksums(Wal *pWal, u32 iLast){
68854 const int szPage = pWal->szPage;/* Database page size */
68855 int rc = SQLITE_OK; /* Return code */
68857 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
68867 ** wal-file header. Otherwise, read them from the frame header of the
68869 assert( pWal->iReCksum>0 );
68870 if( pWal->iReCksum==1 ){
68873 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
68875 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
68876 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
68877 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
68879 iRead = pWal->iReCksum;
68880 pWal->iReCksum = 0;
68883 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
68890 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
68899 ** Write a set of frames to the log. The caller must hold the write-lock
68902 static int walFrames(
68904 int szPage, /* Database page-size in bytes */
68907 int isCommit, /* True if this is a commit */
68908 int sync_flags /* Flags to pass to OsSync() (or 0) */
68910 int rc; /* Used to catch return codes */
68914 int nExtra = 0; /* Number of extra copies of last page */
68915 int szFrame; /* The size of a single frame */
68922 assert( pWal->writeLock );
68929 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
68931 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
68936 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
68937 iFirst = pLive->mxFrame+1;
68941 ** log file, instead of appending to it at pWal->hdr.mxFrame.
68951 iFrame = pWal->hdr.mxFrame;
68953 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
68954 u32 aCksum[2]; /* Checksum for wal-header */
68959 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
68960 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
68961 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
68962 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
68966 pWal->szPage = szPage;
68967 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
68968 pWal->hdr.aFrameCksum[0] = aCksum[0];
68969 pWal->hdr.aFrameCksum[1] = aCksum[1];
68970 pWal->truncateOnCommit = 1;
68972 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
68973 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
68980 ** an out-of-order write following a WAL restart could result in
68985 if( pWal->syncHeader ){
68986 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
68990 if( (int)pWal->szPage!=szPage ){
68996 w.pFd = pWal->pWalFd;
69004 for(p=pList; p; p=p->pDirty){
69005 int nDbSize; /* 0 normally. Positive == commit flag */
69009 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
69011 if( iFirst && (p->pDirty || isCommit==0) ){
69013 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
69018 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
69019 pWal->iReCksum = iWrite;
69021 pData = p->pData;
69022 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
69024 p->flags &= ~PGHDR_WAL_APPEND;
69031 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
69036 p->flags |= PGHDR_WAL_APPEND;
69040 if( isCommit && pWal->iReCksum ){
69060 int bSync = 1;
69061 if( pWal->padToSectorBoundary ){
69062 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
69063 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
69084 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
69085 i64 sz = pWal->mxWalSize;
69086 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
69090 pWal->truncateOnCommit = 0;
69093 /* Append data to the wal-index. It is not necessary to lock the
69094 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
69098 iFrame = pWal->hdr.mxFrame;
69099 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
69100 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
69102 rc = walIndexAppend(pWal, iFrame, p->pgno);
69107 nExtra--;
69108 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
69113 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
69116 pWal->hdr.mxFrame = iFrame;
69118 pWal->hdr.iChange++;
69119 pWal->hdr.nPage = nTruncate;
69121 /* If this is a commit, update the wal-index header too. */
69124 pWal->iCallback = iFrame;
69133 ** Write a set of frames to the log. The caller must hold the write-lock
69139 SQLITE_PRIVATE int sqlite3WalFrames(
69141 int szPage, /* Database page-size in bytes */
69144 int isCommit, /* True if this is a commit */
69145 int sync_flags /* Flags to pass to OsSync() (or 0) */
69147 int rc;
69162 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
69165 SQLITE_PRIVATE int sqlite3WalCheckpoint(
69168 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
69169 int (*xBusy)(void*), /* Function to call when busy */
69171 int sync_flags, /* Flags to sync db file with (or 0) */
69172 int nBuf, /* Size of temporary buffer */
69174 int *pnLog, /* OUT: Number of frames in WAL */
69175 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
69177 int rc; /* Return code */
69178 int isChanged = 0; /* True if a new wal-index header is loaded */
69179 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
69180 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
69182 assert( pWal->ckptLock==0 );
69183 assert( pWal->writeLock==0 );
69185 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
69189 if( pWal->readOnly ) return SQLITE_READONLY;
69196 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
69198 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
69201 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
69208 pWal->ckptLock = 1;
69210 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
69214 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
69215 ** immediately, and a busy-handler is configured, it is invoked and the
69216 ** writer lock retried until either the busy-handler returns 0 or the
69222 pWal->writeLock = 1;
69232 /* Read the wal-index header. */
69235 /* For a passive checkpoint, do not re-enable blocking locks after
69236 ** reading the wal-index header. A passive checkpoint should not block
69238 ** attempt to obtain is a lock on a read-slot, and it should give up
69243 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
69244 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
69250 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
69258 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
69260 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
69267 /* If a new wal-index header was loaded before the checkpoint was
69268 ** performed, then the pager-cache associated with pWal is now
69269 ** out of date. So zero the cached wal-index header to ensure that
69273 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
69281 if( pWal->ckptLock ){
69283 pWal->ckptLock = 0;
69297 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
69300 ret = pWal->iCallback;
69301 pWal->iCallback = 0;
69303 return (int)ret;
69312 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
69314 ** transition out of exclusive-mode is successful, return 1. This
69319 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
69321 ** WAL is already in exclusive-locking mode - meaning that this
69322 ** routine is a no-op. The pager must already hold the exclusive lock
69325 ** If op is negative, then do a dry-run of the op==1 case but do
69330 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
69331 int rc;
69332 assert( pWal->writeLock==0 );
69333 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
69335 /* pWal->readLock is usually set, but might be -1 if there was a
69336 ** prior error while attempting to acquire are read-lock. This cannot
69339 ** upgrade to exclusive-mode following such an error.
69342 assert( pWal->readLock>=0 || pWal->lockError );
69344 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
69347 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
69348 pWal->exclusiveMode = WAL_NORMAL_MODE;
69349 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
69350 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
69352 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
69358 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
69359 assert( pWal->readLock>=0 );
69360 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
69361 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
69364 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
69370 ** Return true if the argument is non-NULL and the WAL module is using
69371 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
69372 ** WAL module is using shared-memory, return false.
69374 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
69375 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
69383 SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
69384 int rc = SQLITE_OK;
69388 assert( pWal->readLock>=0 && pWal->writeLock==0 );
69390 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
69398 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
69405 /* Try to open on pSnapshot when the next read-transaction starts
69411 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
69415 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
69418 SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
69424 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
69425 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
69426 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
69427 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
69442 SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
69443 int rc;
69448 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
69449 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
69475 ** read-lock. This function returns the database page-size if it is known,
69478 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
69479 assert( pWal==0 || pWal->readLock>=0 );
69480 return (pWal ? pWal->szPage : 0);
69487 return pWal->pWalFd;
69524 ** This file implements an external (disk-based) database using BTrees.
69528 ** "Sorting And Searching", pages 473-480. Addison-Wesley
69534 ** ----------------------------------------------------------------
69535 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
69536 ** ----------------------------------------------------------------
69541 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
69563 ** page, or a pointer-map page.
69582 ** 40 60 15 4-byte meta values passed to higher layers
69587 ** 52 4 Largest root-page (auto/incr_vacuum)
69588 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
69591 ** 68 4 Application-ID
69593 ** 92 4 The version-valid-for number
69596 ** All of the integer values are big-endian (most significant byte first).
69604 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
69619 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
69622 ** |----------------|
69624 ** |----------------|
69626 ** |----------------|
69630 ** |----------------|
69633 ** |----------------| ^ Grows upwards
69636 ** |----------------|
69655 ** The cell pointer array contains zero or more 2-byte numbers which are
69686 ** appears first. A variable-length integer may not be more than 9 bytes long.
69688 ** allows a 64-bit integer to be encoded in 9 bytes.
69711 ** filled with data (pagesize - 4 bytes). The last page can have as little
69734 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
69741 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
69752 ** You can change this value at compile-time by specifying a
69753 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
69754 ** header must be exactly 16 bytes including the zero-terminator so
69766 ** first byte of on-disk image of every BTree page.
69776 ** is derived from the raw on-disk page content.
69783 ** stored in MemPage.pBt->mutex.
69787 u8 intKey; /* True if table b-trees. False for index b-trees */
69800 int nFree; /* Number of free bytes on the page. -1 for unknown */
69803 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
69804 ** non-overflow cell */
69808 u8 *aDataEnd; /* One byte past the end of the entire page - not just
69810 ** corruption-induced buffer overflow. */
69864 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
69865 int nBackup; /* Number of backup operations reading this btree */
69866 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
69880 ** If the shared-data extension is enabled, there may be multiple users
69892 # error wrong numeric code for no-transaction
69895 # error wrong numeric code for read-transaction
69898 # error wrong numeric code for write-transaction
69921 ** If a BtShared client fails to obtain a write-lock on a database
69922 ** table (because there exists one or more read-locks on the table),
69923 ** the shared-cache enters 'pending-lock' state and isPending is
69926 ** The shared-cache leaves the 'pending lock' state when either of
69932 ** while in the 'pending-lock' state, no connection may start a new
69935 ** This feature is included to help prevent writer-starvation.
69944 u8 autoVacuum; /* True if auto-vacuum is enabled */
69945 u8 incrVacuum; /* True if incr-vacuum is enabled */
69949 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
69952 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
69953 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
69958 int nTransaction; /* Number of open transactions (read + write) */
69962 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
69963 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
69965 int nRef; /* Number of references to this structure */
69967 BtLock *pLock; /* List of locks held on this shared-btree struct */
69971 int nPreformatSize; /* Size of last cell written by TransferRow() */
69983 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
69985 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
69997 u16 nSize; /* Size of the cell content on the main b-tree page */
70001 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
70004 ** root-node and 3 for all other internal nodes.
70013 ** b-tree within a database file.
70023 ** found at self->pBt->mutex.
70031 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
70032 ** sqlite3BtreePrevious() is no-op if skipNext<0.
70042 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
70057 u8 curIntKey; /* Value of apPage[0]->intKey */
70059 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
70062 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
70088 ** Cursor is valid except that the Cursor.skipNext field is non-zero
70090 ** operation should be a no-op.
70115 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
70118 ** These macros define the location of the pointer-map entry for a
70123 ** PTRMAP_PAGENO returns the database page number of the pointer-map
70127 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
70129 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
70133 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
70150 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
70153 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
70161 ** overflow pages. The page-number identifies the previous
70164 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
70177 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
70178 assert( p->pBt->inTransaction>=p->inTrans );
70183 ** if the database supports auto-vacuum or not. Because it is used
70189 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
70200 ** the database. As the integrity-check proceeds, for each page used in
70201 ** the database the corresponding bit is set. This allows integrity-check to
70208 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
70211 int mxErr; /* Stop accumulating errors when this reaches zero */
70212 int nErr; /* Number of messages written to zErrMsg so far */
70213 int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */
70218 int v2; /* Value for third %d substitution in zPfx */
70220 u32 *heap; /* Min-heap used for analyzing cell coverage */
70226 ** Routines to read or write a two- and four-byte big-endian integer values.
70235 ** two-byte aligned address. get2byteAligned() is only used for accessing the
70254 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
70256 ** p->locked boolean to true.
70259 assert( p->locked==0 );
70260 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
70261 assert( sqlite3_mutex_held(p->db->mutex) );
70263 sqlite3_mutex_enter(p->pBt->mutex);
70264 p->pBt->db = p->db;
70265 p->locked = 1;
70269 ** Release the BtShared mutex associated with B-Tree handle p and
70270 ** clear the p->locked boolean.
70273 BtShared *pBt = p->pBt;
70274 assert( p->locked==1 );
70275 assert( sqlite3_mutex_held(pBt->mutex) );
70276 assert( sqlite3_mutex_held(p->db->mutex) );
70277 assert( p->db==pBt->db );
70279 sqlite3_mutex_leave(pBt->mutex);
70280 p->locked = 0;
70290 ** and this routine is a no-op. The underlying mutex is non-recursive.
70295 ** by all database connections. The p->pNext is a list of other
70298 ** p, then first unlock all of the others on p->pNext, then wait
70307 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
70308 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
70309 assert( p->pNext==0 || p->pNext->db==p->db );
70310 assert( p->pPrev==0 || p->pPrev->db==p->db );
70311 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
70314 assert( !p->locked || p->wantToLock>0 );
70315 assert( p->sharable || p->wantToLock==0 );
70318 assert( sqlite3_mutex_held(p->db->mutex) );
70322 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
70324 if( !p->sharable ) return;
70325 p->wantToLock++;
70326 if( p->locked ) return;
70343 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
70344 p->pBt->db = p->db;
70345 p->locked = 1;
70354 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70355 assert( pLater->sharable );
70356 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
70357 assert( !pLater->locked || pLater->wantToLock>0 );
70358 if( pLater->locked ){
70363 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70364 if( pLater->wantToLock ){
70375 assert( sqlite3_mutex_held(p->db->mutex) );
70376 if( p->sharable ){
70377 assert( p->wantToLock>0 );
70378 p->wantToLock--;
70379 if( p->wantToLock==0 ){
70388 ** B-Tree is not marked as sharable.
70392 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
70393 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
70394 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
70395 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
70396 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
70398 return (p->sharable==0 || p->locked);
70410 ** There is a corresponding leave-all procedures.
70418 int i;
70419 int skipOk = 1;
70421 assert( sqlite3_mutex_held(db->mutex) );
70422 for(i=0; i<db->nDb; i++){
70423 p = db->aDb[i].pBt;
70424 if( p && p->sharable ){
70429 db->noSharedCache = skipOk;
70432 if( db->noSharedCache==0 ) btreeEnterAll(db);
70435 int i;
70437 assert( sqlite3_mutex_held(db->mutex) );
70438 for(i=0; i<db->nDb; i++){
70439 p = db->aDb[i].pBt;
70444 if( db->noSharedCache==0 ) btreeLeaveAll(db);
70454 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
70455 int i;
70456 if( !sqlite3_mutex_held(db->mutex) ){
70459 for(i=0; i<db->nDb; i++){
70461 p = db->aDb[i].pBt;
70462 if( p && p->sharable &&
70463 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
70474 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
70478 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
70483 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
70486 if( db->pVfs==0 && db->nDb==0 ) return 1;
70488 assert( iDb>=0 && iDb<db->nDb );
70489 if( !sqlite3_mutex_held(db->mutex) ) return 0;
70491 p = db->aDb[iDb].pBt;
70493 return p->sharable==0 || p->locked==1;
70501 ** these two routines, all mutex operations are no-ops in that case and
70505 ** the ones below, are no-ops and are null #defines in btree.h.
70509 p->pBt->db = p->db;
70512 int i;
70513 for(i=0; i<db->nDb; i++){
70514 Btree *p = db->aDb[i].pBt;
70516 p->pBt->db = p->db;
70531 sqlite3BtreeEnter(pCur->pBtree);
70535 sqlite3BtreeLeave(pCur->pBtree);
70555 ** This file implements an external (disk-based) database using BTrees.
70572 int sqlite3BtreeTrace=1; /* True to enable tracing */
70579 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
70584 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
70587 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
70600 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
70632 SQLITE_API int sqlite3_enable_shared_cache(int enable){
70645 ** shared-cache table level locks. If the library is compiled with the
70646 ** shared-cache feature disabled, then there is only ever one user
70648 ** So define the lock related functions as no-ops.
70663 u64 n = pBt->nSeek;
70664 pBt->nSeek = 0;
70675 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
70679 int corruptPageError(int lineno, MemPage *p){
70683 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
70694 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
70697 /* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
70701 #define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
70706 /* ^---- Change to 1 and recompile to enable shared-lock tracing
70709 ** Print all shared-cache locks on a BtShared. Debugging use only.
70714 int iRoot,
70715 int eLockType
70719 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W");
70721 printf("%s-%p:", zMsg, pBt);
70723 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70724 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
70725 pLock->eLock==READ_LOCK ? "R" : "W");
70726 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
70727 pLock = pLock->pNext;
70728 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W");
70736 #endif /* Shared-lock tracing */
70745 ** For example, when writing to a table with root-page iRoot via
70758 ** hold a write-lock on the schema table (root page 1). This is also
70761 static int hasSharedCacheTableLock(
70763 Pgno iRoot, /* Root page of b-tree */
70764 int isIndex, /* True if iRoot is the root of an index b-tree */
70765 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
70767 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
70772 ** and has the read-uncommitted flag set, then no lock is required.
70775 if( (pBtree->sharable==0)
70776 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
70783 ** the correct locks are held. So do not bother - just return true.
70786 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
70790 /* Figure out the root-page that the lock should be held on. For table
70791 ** b-trees, this is just the root page of the b-tree being read or
70792 ** written. For index b-trees, it is the root page of the associated
70796 int bSeen = 0;
70797 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
70799 if( pIdx->tnum==iRoot ){
70806 iTab = pIdx->pTable->tnum;
70814 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
70816 /* Search for the required lock. Either a write-lock on root-page iTab, a
70817 ** write-lock on the schema table, or (if the client is reading) a
70818 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
70819 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
70820 if( pLock->pBtree==pBtree
70821 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
70822 && pLock->eLock>=eLockType
70844 ** read-uncommitted flag set, then it is OK for the other object to
70852 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
70854 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70855 if( p->pgnoRoot==iRoot
70856 && p->pBtree!=pBtree
70857 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
70868 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
70872 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
70873 BtShared *pBt = p->pBt;
70878 assert( p->db!=0 );
70879 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
70881 /* If requesting a write-lock, then the Btree must have an open write
70885 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
70886 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
70888 /* This routine is a no-op if the shared-cache is not enabled */
70889 if( !p->sharable ){
70896 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
70897 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
70901 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70902 /* The condition (pIter->eLock!=eLock) in the following if(...)
70905 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
70911 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
70912 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
70913 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
70914 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
70916 assert( p==pBt->pWriter );
70917 pBt->btsFlags |= BTS_PENDING;
70928 ** Add a lock on the table with root-page iTable to the shared-btree used
70944 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
70945 BtShared *pBt = p->pBt;
70953 assert( p->db!=0 );
70955 /* A connection with the read-uncommitted flag set will never try to
70956 ** obtain a read-lock using this function. The only read-lock obtained
70957 ** by a connection in read-uncommitted mode is on the sqlite_schema
70959 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
70961 /* This function should only be called on a sharable b-tree after it
70962 ** has been determined that no other b-tree holds a conflicting lock. */
70963 assert( p->sharable );
70967 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70968 if( pIter->iTable==iTable && pIter->pBtree==p ){
70982 pLock->iTable = iTable;
70983 pLock->pBtree = p;
70984 pLock->pNext = pBt->pLock;
70985 pBt->pLock = pLock;
70989 ** and the requested lock. This means if a write-lock was already held
70990 ** and a read-lock requested, we don't incorrectly downgrade the lock.
70993 if( eLock>pLock->eLock ){
70994 pLock->eLock = eLock;
71011 BtShared *pBt = p->pBt;
71012 BtLock **ppIter = &pBt->pLock;
71015 assert( p->sharable || 0==*ppIter );
71016 assert( p->inTrans>0 );
71022 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
71023 assert( pLock->pBtree->inTrans>=pLock->eLock );
71024 if( pLock->pBtree==p ){
71025 *ppIter = pLock->pNext;
71026 assert( pLock->iTable!=1 || pLock==&p->lock );
71027 if( pLock->iTable!=1 ){
71031 ppIter = &pLock->pNext;
71035 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
71036 if( pBt->pWriter==p ){
71037 pBt->pWriter = 0;
71038 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
71039 }else if( pBt->nTransaction==2 ){
71049 pBt->btsFlags &= ~BTS_PENDING;
71054 ** This function changes all write-locks held by Btree p into read-locks.
71057 BtShared *pBt = p->pBt;
71061 if( pBt->pWriter==p ){
71063 pBt->pWriter = 0;
71064 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
71065 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
71066 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
71067 pLock->eLock = READ_LOCK;
71084 static int cursorHoldsMutex(BtCursor *p){
71085 return sqlite3_mutex_held(p->pBt->mutex);
71089 ** database connetion. This is important in shared-cache mode. If the database
71090 ** connection pointers get out-of-sync, it is possible for routines like
71093 ** statements only and for the purpose of double-checking that the btree code
71094 ** does keep the database connection pointers up-to-date.
71096 static int cursorOwnsBtShared(BtCursor *p){
71098 return (p->pBtree->db==p->pBt->db);
71106 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
71109 ** Invalidate the overflow page-list cache for all cursors opened
71114 assert( sqlite3_mutex_held(pBt->mutex) );
71115 for(p=pBt->pCursor; p; p=p->pNext){
71128 ** cursors open on any row within the table with root-page pgnoRoot.
71138 int isClearTable /* True if all rows are being deleted */
71141 assert( pBtree->hasIncrblobCur );
71143 pBtree->hasIncrblobCur = 0;
71144 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71145 if( (p->curFlags & BTCF_Incrblob)!=0 ){
71146 pBtree->hasIncrblobCur = 1;
71147 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
71148 p->eState = CURSOR_INVALID;
71161 ** when a page that previously contained data becomes a free-list leaf
71166 ** free-list leaf pages:
71169 ** a free-list leaf page, the page is not written to the database
71170 ** (as free-list leaf pages contain no meaningful data). Sometimes
71174 ** 2) When a free-list leaf page is reused, its content is not read
71180 ** a page is moved to the free-list and then reused within the same
71182 ** it is moved to the free-list and it is also not journalled when it
71183 ** is extracted from the free-list and reused, then the original data
71188 ** moved to become a free-list leaf page, the corresponding bit is
71189 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
71194 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
71195 int rc = SQLITE_OK;
71196 if( !pBt->pHasContent ){
71197 assert( pgno<=pBt->nPage );
71198 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
71199 if( !pBt->pHasContent ){
71203 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
71204 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
71212 ** This function is called when a free-list leaf page is removed from the
71213 ** free-list for reuse. It returns false if it is safe to retrieve the
71214 ** page from the pager layer with the 'no-content' flag set. True otherwise.
71216 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
71217 Bitvec *p = pBt->pHasContent;
71223 ** invoked at the conclusion of each write-transaction.
71226 sqlite3BitvecDestroy(pBt->pHasContent);
71227 pBt->pHasContent = 0;
71234 int i;
71235 if( pCur->iPage>=0 ){
71236 for(i=0; i<pCur->iPage; i++){
71237 releasePageNotNull(pCur->apPage[i]);
71239 releasePageNotNull(pCur->pPage);
71240 pCur->iPage = -1;
71247 ** function saves the current cursor key in variables pCur->nKey and
71248 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
71252 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
71253 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
71254 ** set to point to a malloced buffer pCur->nKey bytes in size containing
71257 static int saveCursorKey(BtCursor *pCur){
71258 int rc = SQLITE_OK;
71259 assert( CURSOR_VALID==pCur->eState );
71260 assert( 0==pCur->pKey );
71263 if( pCur->curIntKey ){
71265 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
71270 ** up to the size of 1 varint plus 1 8-byte value when the cursor
71274 pCur->nKey = sqlite3BtreePayloadSize(pCur);
71275 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
71277 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
71279 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
71280 pCur->pKey = pKey;
71288 assert( !pCur->curIntKey || !pCur->pKey );
71299 static int saveCursorPosition(BtCursor *pCur){
71300 int rc;
71302 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
71303 assert( 0==pCur->pKey );
71306 if( pCur->curFlags & BTCF_Pinned ){
71309 if( pCur->eState==CURSOR_SKIPNEXT ){
71310 pCur->eState = CURSOR_VALID;
71312 pCur->skipNext = 0;
71318 pCur->eState = CURSOR_REQUIRESEEK;
71321 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
71326 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
71330 ** the table with root-page iRoot. "Saving the cursor position" means that
71341 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
71349 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
71351 assert( sqlite3_mutex_held(pBt->mutex) );
71352 assert( pExcept==0 || pExcept->pBt==pBt );
71353 for(p=pBt->pCursor; p; p=p->pNext){
71354 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
71357 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
71364 ** broken out from its caller to avoid unnecessary stack pointer movement.
71366 static int SQLITE_NOINLINE saveCursorsOnList(
71372 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
71373 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
71374 int rc = saveCursorPosition(p);
71379 testcase( p->iPage>=0 );
71383 p = p->pNext;
71393 sqlite3_free(pCur->pKey);
71394 pCur->pKey = 0;
71395 pCur->eState = CURSOR_INVALID;
71403 static int btreeMoveto(
71407 int bias, /* Bias search to the high end */
71408 int *pRes /* Write search results here */
71410 int rc; /* Status code */
71414 KeyInfo *pKeyInfo = pCur->pKeyInfo;
71415 assert( nKey==(i64)(int)nKey );
71418 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
71419 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
71424 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
71439 static int btreeRestoreCursorPosition(BtCursor *pCur){
71440 int rc;
71441 int skipNext = 0;
71443 assert( pCur->eState>=CURSOR_REQUIRESEEK );
71444 if( pCur->eState==CURSOR_FAULT ){
71445 return pCur->skipNext;
71447 pCur->eState = CURSOR_INVALID;
71451 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
71454 sqlite3_free(pCur->pKey);
71455 pCur->pKey = 0;
71456 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
71457 if( skipNext ) pCur->skipNext = skipNext;
71458 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
71459 pCur->eState = CURSOR_SKIPNEXT;
71466 (p->eState>=CURSOR_REQUIRESEEK ? \
71482 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
71486 assert( sizeof(pCur->eState)==1 );
71514 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
71515 int rc;
71518 assert( pCur->eState!=CURSOR_VALID );
71524 if( pCur->eState!=CURSOR_VALID ){
71538 SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
71565 pCur->hints = x;
71572 ** number for the pointer-map page that contains the entry for the
71580 int nPagesPerMapPage;
71582 assert( sqlite3_mutex_held(pBt->mutex) );
71584 nPagesPerMapPage = (pBt->usableSize/5)+1;
71585 iPtrMap = (pgno-2)/nPagesPerMapPage;
71599 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
71600 ** a no-op. If an error occurs, the appropriate error code is written
71603 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
71607 int offset; /* Offset in pointer map page */
71608 int rc; /* Return code from subfunctions */
71612 assert( sqlite3_mutex_held(pBt->mutex) );
71613 /* The super-journal page number must never be used as a pointer map page */
71616 assert( pBt->autoVacuum );
71622 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71639 assert( offset <= (int)pBt->usableSize-5 );
71643 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
71662 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
71664 int iPtrmap; /* Pointer map page index */
71666 int offset; /* Offset of entry in pointer map */
71667 int rc;
71669 assert( sqlite3_mutex_held(pBt->mutex) );
71672 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71683 assert( offset <= (int)pBt->usableSize-5 );
71705 ** 4-byte child pointer found on interior pages, if there is one.
71710 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
71712 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
71718 ** on a single B-tree page. Make necessary adjustments to the CellInfo
71735 int minLocal; /* Minimum amount of payload held locally */
71736 int maxLocal; /* Maximum amount of payload held locally */
71737 int surplus; /* Overflow payload available for local storage */
71739 minLocal = pPage->minLocal;
71740 maxLocal = pPage->maxLocal;
71741 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
71745 pInfo->nLocal = (u16)surplus;
71747 pInfo->nLocal = (u16)minLocal;
71749 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
71756 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
71757 int maxLocal; /* Maximum amount of payload held locally */
71758 maxLocal = pPage->maxLocal;
71762 int minLocal; /* Minimum amount of payload held locally */
71763 int surplus; /* Overflow payload available for local storage */
71764 minLocal = pPage->minLocal;
71765 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
71789 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71790 assert( pPage->leaf==0 );
71791 assert( pPage->childPtrSize==4 );
71795 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
71796 pInfo->nPayload = 0;
71797 pInfo->nLocal = 0;
71798 pInfo->pPayload = 0;
71810 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71811 assert( pPage->leaf==0 || pPage->leaf==1 );
71812 assert( pPage->intKeyLeaf );
71813 assert( pPage->childPtrSize==0 );
71834 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
71837 ** This routine is a high-runner.
71871 pInfo->nKey = *(i64*)&iKey;
71872 pInfo->nPayload = nPayload;
71873 pInfo->pPayload = pIter;
71874 testcase( nPayload==pPage->maxLocal );
71875 testcase( nPayload==(u32)pPage->maxLocal+1 );
71876 if( nPayload<=pPage->maxLocal ){
71880 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71881 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71882 pInfo->nLocal = (u16)nPayload;
71895 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71896 assert( pPage->leaf==0 || pPage->leaf==1 );
71897 assert( pPage->intKeyLeaf==0 );
71898 pIter = pCell + pPage->childPtrSize;
71908 pInfo->nKey = nPayload;
71909 pInfo->nPayload = nPayload;
71910 pInfo->pPayload = pIter;
71911 testcase( nPayload==pPage->maxLocal );
71912 testcase( nPayload==(u32)pPage->maxLocal+1 );
71913 if( nPayload<=pPage->maxLocal ){
71917 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71918 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71919 pInfo->nLocal = (u16)nPayload;
71926 int iCell, /* The cell index. First cell is 0 */
71929 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
71937 ** data area of the btree-page. The return number includes the cell
71957 pPage->xParseCell(pPage, pCell, &debuginfo);
71960 assert( pPage->childPtrSize==4 );
71970 testcase( nSize==pPage->maxLocal );
71971 testcase( nSize==(u32)pPage->maxLocal+1 );
71972 if( nSize<=pPage->maxLocal ){
71973 nSize += (u32)(pIter - pCell);
71976 int minLocal = pPage->minLocal;
71977 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71978 testcase( nSize==pPage->maxLocal );
71979 testcase( nSize==(u32)pPage->maxLocal+1 );
71980 if( nSize>pPage->maxLocal ){
71983 nSize += 4 + (u16)(pIter - pCell);
71999 pPage->xParseCell(pPage, pCell, &debuginfo);
72002 assert( pPage->childPtrSize==0 );
72012 testcase( nSize==pPage->maxLocal );
72013 testcase( nSize==(u32)pPage->maxLocal+1 );
72014 if( nSize<=pPage->maxLocal ){
72015 nSize += (u32)(pIter - pCell);
72018 int minLocal = pPage->minLocal;
72019 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72020 testcase( nSize==pPage->maxLocal );
72021 testcase( nSize==(u32)pPage->maxLocal+1 );
72022 if( nSize>pPage->maxLocal ){
72025 nSize += 4 + (u16)(pIter - pCell);
72040 pPage->xParseCell(pPage, pCell, &debuginfo);
72045 assert( pPage->childPtrSize==4 );
72048 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
72049 return (u16)(pIter - pCell);
72062 pPage->xParseCell(pPage, pCell, &debuginfo);
72074 /* pIter now points at the 64-bit integer key value, a variable length
72085 testcase( nSize==pPage->maxLocal );
72086 testcase( nSize==(u32)pPage->maxLocal+1 );
72087 if( nSize<=pPage->maxLocal ){
72088 nSize += (u32)(pIter - pCell);
72091 int minLocal = pPage->minLocal;
72092 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72093 testcase( nSize==pPage->maxLocal );
72094 testcase( nSize==(u32)pPage->maxLocal+1 );
72095 if( nSize>pPage->maxLocal ){
72098 nSize += 4 + (u16)(pIter - pCell);
72108 static u16 cellSize(MemPage *pPage, int iCell){
72109 return pPage->xCellSize(pPage, findCell(pPage, iCell));
72117 ** pointer to an overflow page, insert an entry into the pointer-map for
72120 static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
72124 pPage->xParseCell(pPage, pCell, &info);
72127 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
72132 ovfl = get4byte(&pCell[info.nSize-4]);
72133 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
72141 ** page so that there are no free-blocks on the free-block list.
72146 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
72147 ** b-tree page so that there are no freeblocks or fragment bytes, all
72151 static int defragmentPage(MemPage *pPage, int nMaxFrag){
72152 int i; /* Loop counter */
72153 int pc; /* Address of the i-th cell */
72154 int hdr; /* Offset to the page header */
72155 int size; /* Size of a cell */
72156 int usableSize; /* Number of usable bytes on a page */
72157 int cellOffset; /* Offset to the cell pointer array */
72158 int cbrk; /* Offset to the cell content area */
72159 int nCell; /* Number of cells on the page */
72163 int iCellFirst; /* First allowable cell index */
72164 int iCellLast; /* Last possible cell index */
72165 int iCellStart; /* First cell offset in input */
72167 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72168 assert( pPage->pBt!=0 );
72169 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
72170 assert( pPage->nOverflow==0 );
72171 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72172 data = pPage->aData;
72173 hdr = pPage->hdrOffset;
72174 cellOffset = pPage->cellOffset;
72175 nCell = pPage->nCell;
72178 usableSize = pPage->pBt->usableSize;
72183 ** offsets to each pointer in the cell-pointer array than it is to
72185 if( (int)data[hdr+7]<=nMaxFrag ){
72186 int iFree = get2byte(&data[hdr+1]);
72187 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72189 int iFree2 = get2byte(&data[iFree]);
72190 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72194 int sz2 = 0;
72195 int sz = get2byte(&data[iFree+2]);
72196 int top = get2byte(&data[hdr+5]);
72204 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
72211 assert( cbrk+(iFree-top) <= usableSize );
72212 memmove(&data[cbrk], &data[top], iFree-top);
72224 iCellLast = usableSize - 4;
72227 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
72231 u8 *pAddr; /* The i-th cell pointer */
72243 size = pPage->xCellSize(pPage, &src[pc]);
72244 cbrk -= size;
72258 assert( pPage->nFree>=0 );
72259 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
72266 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
72267 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72272 ** Search the free-list on page pPg for space to store a cell nByte bytes in
72274 ** from the free-list.
72276 ** If no suitable space can be found on the free-list, return NULL.
72285 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
72286 const int hdr = pPg->hdrOffset; /* Offset to page header */
72287 u8 * const aData = pPg->aData; /* Page data */
72288 int iAddr = hdr + 1; /* Address of ptr to pc */
72290 int pc = get2byte(pTmp); /* Address of a free slot */
72291 int x; /* Excess size of the slot */
72292 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
72293 int size; /* Size of the free slot */
72297 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
72298 ** freeblock form a big-endian integer which is the size of the freeblock
72299 ** in bytes, including the 4-byte header. */
72302 if( (x = size - nByte)>=0 ){
72306 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
72310 /* Remove the slot from the free-list. Update the number of
72320 /* The slot remains on the free-list. Reduce its size to account
72337 if( pc>maxPC+nByte-4 ){
72345 ** Allocate nByte bytes of space from within the B-Tree page passed
72346 ** as the first argument. Write into *pIdx the index into pPage->aData[]
72357 static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
72358 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
72359 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
72360 int top; /* First byte of cell content area */
72361 int rc = SQLITE_OK; /* Integer return code */
72363 int gap; /* First byte of gap between cell pointers and cell content */
72365 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72366 assert( pPage->pBt );
72367 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72369 assert( pPage->nFree>=nByte );
72370 assert( pPage->nOverflow==0 );
72371 assert( nByte < (int)(pPage->pBt->usableSize-8) );
72373 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
72374 gap = pPage->cellOffset + 2*pPage->nCell;
72376 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
72379 ** However, that integer is too large to be stored in a 2-byte unsigned
72384 if( top==0 && pPage->pBt->usableSize==65536 ){
72389 }else if( top>(int)pPage->pBt->usableSize ){
72403 int g2;
72404 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
72405 *pIdx = g2 = (int)(pSpace-data);
72421 assert( pPage->nCell>0 || CORRUPT_DB );
72422 assert( pPage->nFree>=0 );
72423 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
72436 top -= nByte;
72438 assert( top+nByte <= (int)pPage->pBt->usableSize );
72444 ** Return a section of the pPage->aData to the freelist.
72445 ** The first byte of the new free block is pPage->aData[iStart]
72456 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72464 unsigned char *data = pPage->aData; /* Page content */
72467 assert( pPage->pBt!=0 );
72468 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72469 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
72470 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
72471 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72473 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
72478 hdr = pPage->hdrOffset;
72490 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
72502 nFrag = iFreeBlk - iEnd;
72505 if( iEnd > pPage->pBt->usableSize ){
72508 iSize = iEnd - iStart;
72517 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
72520 nFrag += iStart - iPtrEnd;
72521 iSize = iEnd - iPtr;
72526 data[hdr+7] -= nFrag;
72530 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
72549 pPage->nFree += iOrigSize;
72565 static int decodeFlags(MemPage *pPage, int flagByte){
72566 BtShared *pBt; /* A copy of pPage->pBt */
72568 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
72569 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72570 pBt = pPage->pBt;
72571 pPage->max1bytePayload = pBt->max1bytePayload;
72573 pPage->childPtrSize = 0;
72574 pPage->leaf = 1;
72576 pPage->intKeyLeaf = 1;
72577 pPage->xCellSize = cellSizePtrTableLeaf;
72578 pPage->xParseCell = btreeParseCellPtr;
72579 pPage->intKey = 1;
72580 pPage->maxLocal = pBt->maxLeaf;
72581 pPage->minLocal = pBt->minLeaf;
72583 pPage->intKey = 0;
72584 pPage->intKeyLeaf = 0;
72585 pPage->xCellSize = cellSizePtrIdxLeaf;
72586 pPage->xParseCell = btreeParseCellPtrIndex;
72587 pPage->maxLocal = pBt->maxLocal;
72588 pPage->minLocal = pBt->minLocal;
72590 pPage->intKey = 0;
72591 pPage->intKeyLeaf = 0;
72592 pPage->xCellSize = cellSizePtrIdxLeaf;
72593 pPage->xParseCell = btreeParseCellPtrIndex;
72597 pPage->childPtrSize = 4;
72598 pPage->leaf = 0;
72600 pPage->intKey = 0;
72601 pPage->intKeyLeaf = 0;
72602 pPage->xCellSize = cellSizePtr;
72603 pPage->xParseCell = btreeParseCellPtrIndex;
72604 pPage->maxLocal = pBt->maxLocal;
72605 pPage->minLocal = pBt->minLocal;
72607 pPage->intKeyLeaf = 0;
72608 pPage->xCellSize = cellSizePtrNoPayload;
72609 pPage->xParseCell = btreeParseCellPtrNoPayload;
72610 pPage->intKey = 1;
72611 pPage->maxLocal = pBt->maxLeaf;
72612 pPage->minLocal = pBt->minLeaf;
72614 pPage->intKey = 0;
72615 pPage->intKeyLeaf = 0;
72616 pPage->xCellSize = cellSizePtr;
72617 pPage->xParseCell = btreeParseCellPtrIndex;
72626 ** in the pPage->nFree field.
72628 static int btreeComputeFreeSpace(MemPage *pPage){
72629 int pc; /* Address of a freeblock within pPage->aData[] */
72631 u8 *data; /* Equal to pPage->aData */
72632 int usableSize; /* Amount of usable space on each page */
72633 int nFree; /* Number of unused bytes on the page */
72634 int top; /* First byte of the cell content area */
72635 int iCellFirst; /* First allowable cell or freeblock offset */
72636 int iCellLast; /* Last possible cell or freeblock offset */
72638 assert( pPage->pBt!=0 );
72639 assert( pPage->pBt->db!=0 );
72640 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72641 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72642 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72643 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72644 assert( pPage->isInit==1 );
72645 assert( pPage->nFree<0 );
72647 usableSize = pPage->pBt->usableSize;
72648 hdr = pPage->hdrOffset;
72649 data = pPage->aData;
72650 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
72654 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
72655 iCellLast = usableSize - 4;
72658 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
72662 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
72666 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
72686 if( pc+size>(unsigned int)usableSize ){
72693 ** of the cell-content area plus the number of free bytes within
72694 ** the cell-content area. If this is greater than the usable-size
72696 ** serves to verify that the offset to the start of the cell-content
72702 pPage->nFree = (u16)(nFree - iCellFirst);
72710 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
72711 int iCellFirst; /* First allowable cell or freeblock offset */
72712 int iCellLast; /* Last possible cell or freeblock offset */
72713 int i; /* Index into the cell pointer array */
72714 int sz; /* Size of a cell */
72715 int pc; /* Address of a freeblock within pPage->aData[] */
72716 u8 *data; /* Equal to pPage->aData */
72717 int usableSize; /* Maximum usable space on the page */
72718 int cellOffset; /* Start of cell content area */
72720 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
72721 usableSize = pPage->pBt->usableSize;
72722 iCellLast = usableSize - 4;
72723 data = pPage->aData;
72724 cellOffset = pPage->cellOffset;
72725 if( !pPage->leaf ) iCellLast--;
72726 for(i=0; i<pPage->nCell; i++){
72733 sz = pPage->xCellSize(pPage, &data[pc]);
72746 ** not contain a well-formed database page, then return
72748 ** guarantee that the page is well-formed. It only shows that
72751 static int btreeInitPage(MemPage *pPage){
72752 u8 *data; /* Equal to pPage->aData */
72755 assert( pPage->pBt!=0 );
72756 assert( pPage->pBt->db!=0 );
72757 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72758 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72759 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72760 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72761 assert( pPage->isInit==0 );
72763 pBt = pPage->pBt;
72764 data = pPage->aData + pPage->hdrOffset;
72765 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
72766 ** the b-tree page type. */
72770 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72771 pPage->maskPage = (u16)(pBt->pageSize - 1);
72772 pPage->nOverflow = 0;
72773 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
72774 pPage->aCellIdx = data + pPage->childPtrSize + 8;
72775 pPage->aDataEnd = pPage->aData + pBt->pageSize;
72776 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
72777 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72779 pPage->nCell = get2byte(&data[3]);
72780 if( pPage->nCell>MX_CELL(pBt) ){
72784 testcase( pPage->nCell==MX_CELL(pBt) );
72785 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
72789 assert( pPage->nCell>0
72790 || get2byteNotZero(&data[5])==(int)pBt->usableSize
72792 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
72793 pPage->isInit = 1;
72794 if( pBt->db->flags & SQLITE_CellSizeCk ){
72804 static void zeroPage(MemPage *pPage, int flags){
72805 unsigned char *data = pPage->aData;
72806 BtShared *pBt = pPage->pBt;
72807 u8 hdr = pPage->hdrOffset;
72810 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
72811 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72812 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
72813 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72814 assert( sqlite3_mutex_held(pBt->mutex) );
72815 if( pBt->btsFlags & BTS_FAST_SECURE ){
72816 memset(&data[hdr], 0, pBt->usableSize - hdr);
72822 put2byte(&data[hdr+5], pBt->usableSize);
72823 pPage->nFree = (u16)(pBt->usableSize - first);
72825 pPage->cellOffset = first;
72826 pPage->aDataEnd = &data[pBt->pageSize];
72827 pPage->aCellIdx = &data[first];
72828 pPage->aDataOfst = &data[pPage->childPtrSize];
72829 pPage->nOverflow = 0;
72830 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72831 pPage->maskPage = (u16)(pBt->pageSize - 1);
72832 pPage->nCell = 0;
72833 pPage->isInit = 1;
72843 if( pgno!=pPage->pgno ){
72844 pPage->aData = sqlite3PagerGetData(pDbPage);
72845 pPage->pDbPage = pDbPage;
72846 pPage->pBt = pBt;
72847 pPage->pgno = pgno;
72848 pPage->hdrOffset = pgno==1 ? 100 : 0;
72850 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72865 static int btreeGetPage(
72869 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
72871 int rc;
72875 assert( sqlite3_mutex_held(pBt->mutex) );
72876 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
72889 assert( sqlite3_mutex_held(pBt->mutex) );
72890 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
72899 ** error, return ((unsigned int)-1).
72902 return pBt->nPage;
72906 return btreePagecount(p->pBt);
72912 static int getAndInitPage(
72916 int bReadOnly /* True for a read-only page */
72918 int rc;
72921 assert( sqlite3_mutex_held(pBt->mutex) );
72927 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
72933 if( pPage->isInit==0 ){
72942 assert( pPage->pgno==pgno || CORRUPT_DB );
72943 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72955 assert( pPage->aData );
72956 assert( pPage->pBt );
72957 assert( pPage->pDbPage!=0 );
72958 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72959 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72960 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72961 sqlite3PagerUnrefNotNull(pPage->pDbPage);
72968 assert( pPage->aData );
72969 assert( pPage->pBt );
72970 assert( pPage->pDbPage!=0 );
72971 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72972 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72973 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72974 sqlite3PagerUnrefPageOne(pPage->pDbPage);
72986 static int btreeGetUnusedPage(
72990 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
72992 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
72994 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
72999 (*ppPage)->isInit = 0;
73019 if( pPage->isInit ){
73020 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73021 pPage->isInit = 0;
73028 ** the call for every page that comes in for re-initializing. */
73037 static int btreeInvokeBusyHandler(void *pArg){
73039 assert( pBt->db );
73040 assert( sqlite3_mutex_held(pBt->db->mutex) );
73041 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
73049 ** be exclusively in memory, or it might use a disk-based memory cache.
73053 ** If zFilename is ":memory:" then an in-memory database is created
73065 SQLITE_PRIVATE int sqlite3BtreeOpen(
73066 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
73070 int flags, /* Options */
73071 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
73076 int rc = SQLITE_OK; /* Result code from this function */
73081 const int isTempDb = zFilename==0 || zFilename[0]==0;
73083 /* Set the variable isMemdb to true for an in-memory database, or
73084 ** false for a file-based database.
73087 const int isMemdb = 0;
73089 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
73096 assert( sqlite3_mutex_held(db->mutex) );
73115 p->inTrans = TRANS_NONE;
73116 p->db = db;
73118 p->lock.pBtree = p;
73119 p->lock.iTable = 1;
73129 int nFilename = sqlite3Strlen30(zFilename)+1;
73130 int nFullPathname = pVfs->mxPathname+1;
73134 p->sharable = 1;
73160 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
73161 assert( pBt->nRef>0 );
73162 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
73163 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
73164 int iDb;
73165 for(iDb=db->nDb-1; iDb>=0; iDb--){
73166 Btree *pExisting = db->aDb[iDb].pBt;
73167 if( pExisting && pExisting->pBt==pBt ){
73175 p->pBt = pBt;
73176 pBt->nRef++;
73190 p->sharable = 1;
73207 /* Suppress false-positive compiler warning from PVS-Studio */
73215 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
73218 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
73219 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
73224 pBt->openFlags = (u8)flags;
73225 pBt->db = db;
73226 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
73227 p->pBt = pBt;
73229 pBt->pCursor = 0;
73230 pBt->pPage1 = 0;
73231 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
73233 pBt->btsFlags |= BTS_SECURE_DELETE;
73235 pBt->btsFlags |= BTS_OVERWRITE;
73237 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73238 ** determined by the 2-byte integer located at an offset of 16 bytes from
73240 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
73241 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
73242 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
73243 pBt->pageSize = 0;
73245 /* If the magic name ":memory:" will create an in-memory database, then
73246 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
73249 ** regular file-name. In this case the auto-vacuum applies as per normal.
73252 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
73253 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
73258 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73259 ** determined by the one-byte unsigned integer found at an offset of 20
73262 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73264 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
73265 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
73268 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73270 pBt->usableSize = pBt->pageSize - nReserve;
73271 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
73276 pBt->nRef = 1;
73277 if( p->sharable ){
73281 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
73282 if( pBt->mutex==0 ){
73288 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
73300 if( p->sharable ){
73301 int i;
73303 for(i=0; i<db->nDb; i++){
73304 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
73305 while( pSib->pPrev ){ pSib = pSib->pPrev; }
73306 if( (uptr)p->pBt<(uptr)pSib->pBt ){
73307 p->pNext = pSib;
73308 p->pPrev = 0;
73309 pSib->pPrev = p;
73311 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
73312 pSib = pSib->pNext;
73314 p->pNext = pSib->pNext;
73315 p->pPrev = pSib;
73316 if( p->pNext ){
73317 p->pNext->pPrev = p;
73319 pSib->pNext = p;
73330 if( pBt && pBt->pPager ){
73331 sqlite3PagerClose(pBt->pPager, 0);
73339 /* If the B-Tree was successfully opened, set the pager-cache size to the
73340 ** default value. Except, when opening on an existing shared pager-cache,
73341 ** do not change the pager-cache size.
73347 pFile = sqlite3PagerFile(pBt->pPager);
73348 if( pFile->pMethods ){
73349 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
73366 static int removeFromSharingList(BtShared *pBt){
73370 int removed = 0;
73372 assert( sqlite3_mutex_notheld(pBt->mutex) );
73375 pBt->nRef--;
73376 if( pBt->nRef<=0 ){
73378 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
73381 while( ALWAYS(pList) && pList->pNext!=pBt ){
73382 pList=pList->pNext;
73385 pList->pNext = pBt->pNext;
73389 sqlite3_mutex_free(pBt->mutex);
73401 ** Make sure pBt->pTmpSpace points to an allocation of
73402 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
73405 static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){
73407 assert( pBt->pTmpSpace==0 );
73410 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
73411 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
73412 if( pBt->pTmpSpace==0 ){
73413 BtCursor *pCur = pBt->pCursor;
73414 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
73419 /* One of the uses of pBt->pTmpSpace is to format cells before
73432 ** left-child pointer to the beginning of a cell.
73434 memset(pBt->pTmpSpace, 0, 8);
73435 pBt->pTmpSpace += 4;
73440 ** Free the pBt->pTmpSpace allocation
73443 if( pBt->pTmpSpace ){
73444 pBt->pTmpSpace -= 4;
73445 sqlite3PageFree(pBt->pTmpSpace);
73446 pBt->pTmpSpace = 0;
73453 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
73454 BtShared *pBt = p->pBt;
73457 assert( sqlite3_mutex_held(p->db->mutex) );
73463 BtCursor *pCur = pBt->pCursor;
73466 pCur = pCur->pNext;
73467 assert( pTmp->pBtree!=p );
73474 ** The call to sqlite3BtreeRollback() drops any table-locks held by
73480 /* If there are still other outstanding references to the shared-btree
73482 ** up the shared-btree.
73484 assert( p->wantToLock==0 && p->locked==0 );
73485 if( !p->sharable || removeFromSharingList(pBt) ){
73491 assert( !pBt->pCursor );
73492 sqlite3PagerClose(pBt->pPager, p->db);
73493 if( pBt->xFreeSchema && pBt->pSchema ){
73494 pBt->xFreeSchema(pBt->pSchema);
73496 sqlite3DbFree(0, pBt->pSchema);
73502 assert( p->wantToLock==0 );
73503 assert( p->locked==0 );
73504 if( p->pPrev ) p->pPrev->pNext = p->pNext;
73505 if( p->pNext ) p->pNext->pPrev = p->pPrev;
73519 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
73520 BtShared *pBt = p->pBt;
73521 assert( sqlite3_mutex_held(p->db->mutex) );
73523 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
73538 SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
73539 BtShared *pBt = p->pBt;
73540 int res;
73541 assert( sqlite3_mutex_held(p->db->mutex) );
73543 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
73553 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
73554 BtShared *pBt = p->pBt;
73555 assert( sqlite3_mutex_held(p->db->mutex) );
73557 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
73568 ** is a very low but non-zero probability of damage. Level 3 reduces the
73572 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
73576 BtShared *pBt = p->pBt;
73577 assert( sqlite3_mutex_held(p->db->mutex) );
73579 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
73605 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
73606 int rc = SQLITE_OK;
73607 int x;
73608 BtShared *pBt = p->pBt;
73611 pBt->nReserveWanted = nReserve;
73612 x = pBt->pageSize - pBt->usableSize;
73614 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
73620 ((pageSize-1)&pageSize)==0 ){
73622 assert( !pBt->pCursor );
73624 pBt->pageSize = (u32)pageSize;
73627 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73628 pBt->usableSize = pBt->pageSize - (u16)nReserve;
73629 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73637 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
73638 return p->pBt->pageSize;
73643 ** may only be called if it is guaranteed that the b-tree mutex is already
73647 ** known that the shared b-tree mutex is held, but the mutex on the
73652 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
73653 int n;
73654 assert( sqlite3_mutex_held(p->pBt->mutex) );
73655 n = p->pBt->pageSize - p->pBt->usableSize;
73666 ** The amount of reserve can only grow - never shrink.
73668 SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){
73669 int n1, n2;
73671 n1 = (int)p->pBt->nReserveWanted;
73686 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
73697 ** newFlag==(-1) No changes
73702 ** freelist leaf pages are not written back to the database. Thus in-page
73709 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
73710 int b;
73716 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
73717 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
73719 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
73725 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
73726 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
73727 ** is disabled. The default value for the auto-vacuum property is
73730 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
73734 BtShared *pBt = p->pBt;
73735 int rc = SQLITE_OK;
73739 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
73742 pBt->autoVacuum = av ?1:0;
73743 pBt->incrVacuum = av==2 ?1:0;
73751 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
73754 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
73758 int rc;
73761 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
73762 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
73771 ** If the user has not set the safety-level for this database connection
73772 ** using "PRAGMA synchronous", and if the safety-level is not already
73781 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
73782 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
73783 if( pDb->bSyncSet==0
73784 && pDb->safety_level!=safety_level
73785 && pDb!=&db->aDb[1]
73787 pDb->safety_level = safety_level;
73788 sqlite3PagerSetFlags(pBt->pPager,
73789 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
73798 static int newDatabase(BtShared*);
73806 ** well-formed database file, then SQLITE_CORRUPT is returned.
73810 static int lockBtree(BtShared *pBt){
73811 int rc; /* Result code from subfunctions */
73816 assert( sqlite3_mutex_held(pBt->mutex) );
73817 assert( pBt->pPage1==0 );
73818 rc = sqlite3PagerSharedLock(pBt->pPager);
73826 nPage = get4byte(28+(u8*)pPage1->aData);
73827 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
73828 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
73831 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
73837 u8 *page1 = pPage1->aData;
73839 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
73848 pBt->btsFlags |= BTS_READ_ONLY;
73855 pBt->btsFlags |= BTS_READ_ONLY;
73866 ** may not be the latest version - there may be a newer one in the log
73869 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
73870 int isOpen = 0;
73871 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
73887 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
73896 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73897 ** determined by the 2-byte integer located at an offset of 16 bytes from
73900 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
73902 if( ((pageSize-1)&pageSize)!=0
73909 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
73913 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73914 ** determined by the one-byte unsigned integer found at an offset of 20
73916 usableSize = pageSize - page1[20];
73917 if( (u32)pageSize!=pBt->pageSize ){
73919 ** of BtShared.pageSize, we have discovered that the page-size is
73920 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
73922 ** again with the correct page-size.
73925 pBt->usableSize = usableSize;
73926 pBt->pageSize = pageSize;
73927 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73929 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
73930 pageSize-usableSize);
73934 if( sqlite3WritableSchema(pBt->db)==0 ){
73941 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
73947 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73948 pBt->pageSize = pageSize;
73949 pBt->usableSize = usableSize;
73951 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
73952 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
73958 ** cells can will fit on one page. We assume a 10-byte page header.
73960 ** 2-byte pointer to the cell
73961 ** 4-byte child pointer
73962 ** 9-byte nKey value
73963 ** 4-byte nData value
73964 ** 4-byte overflow page pointer
73965 ** So a cell consists of a 2-byte pointer, a header which is as much as
73969 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
73970 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
73971 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
73972 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
73973 if( pBt->maxLocal>127 ){
73974 pBt->max1bytePayload = 127;
73976 pBt->max1bytePayload = (u8)pBt->maxLocal;
73978 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
73979 pBt->pPage1 = pPage1;
73980 pBt->nPage = nPage;
73985 pBt->pPage1 = 0;
74002 static int countValidCursors(BtShared *pBt, int wrOnly){
74004 int r = 0;
74005 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
74006 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
74007 && pCur->eState!=CURSOR_FAULT ) r++;
74019 ** If there is a transaction in progress, this routine is a no-op.
74022 assert( sqlite3_mutex_held(pBt->mutex) );
74023 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
74024 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
74025 MemPage *pPage1 = pBt->pPage1;
74026 assert( pPage1->aData );
74027 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
74028 pBt->pPage1 = 0;
74038 static int newDatabase(BtShared *pBt){
74041 int rc;
74043 assert( sqlite3_mutex_held(pBt->mutex) );
74044 if( pBt->nPage>0 ){
74047 pP1 = pBt->pPage1;
74049 data = pP1->aData;
74050 rc = sqlite3PagerWrite(pP1->pDbPage);
74054 data[16] = (u8)((pBt->pageSize>>8)&0xff);
74055 data[17] = (u8)((pBt->pageSize>>16)&0xff);
74058 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
74059 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
74063 memset(&data[24], 0, 100-24);
74065 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74067 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
74068 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
74069 put4byte(&data[36 + 4*4], pBt->autoVacuum);
74070 put4byte(&data[36 + 7*4], pBt->incrVacuum);
74072 pBt->nPage = 1;
74082 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
74083 int rc;
74085 p->pBt->nPage = 0;
74086 rc = newDatabase(p->pBt);
74092 ** Attempt to start a new transaction. A write-transaction
74093 ** is started if the second argument is nonzero, otherwise a read-
74097 ** upgraded to exclusive by calling this routine a second time - the
74100 ** A write-transaction must be started before attempting any
74114 ** if there is one. But if there was previously a read-lock, do not
74115 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
74116 ** returned when there is already a read-lock in order to avoid a deadlock.
74126 static SQLITE_NOINLINE int btreeBeginTrans(
74128 int wrflag, /* True to start a write transaction */
74129 int *pSchemaVersion /* Put schema version number here, if not NULL */
74131 BtShared *pBt = p->pBt;
74132 Pager *pPager = pBt->pPager;
74133 int rc = SQLITE_OK;
74138 /* If the btree is already in a write-transaction, or it
74139 ** is already in a read-transaction and a read-transaction
74140 ** is requested, this is a no-op.
74142 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
74145 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
74147 if( (p->db->flags & SQLITE_ResetDatabase)
74150 pBt->btsFlags &= ~BTS_READ_ONLY;
74153 /* Write transactions are not possible on a read-only database */
74154 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
74163 ** on this shared-btree structure and a second write transaction is
74166 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
74167 || (pBt->btsFlags & BTS_PENDING)!=0
74169 pBlock = pBt->pWriter->db;
74172 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
74173 if( pIter->pBtree!=p ){
74174 pBlock = pIter->pBtree->db;
74180 sqlite3ConnectionBlocked(p->db, pBlock);
74187 /* Any read-only or read-write transaction implies a read-lock on
74188 ** page 1. So if some other shared-cache client already has a write-lock
74193 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
74194 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
74196 sqlite3PagerWalDb(pPager, p->db);
74201 if( pBt->pPage1==0 && wrflag ){
74202 assert( pBt->inTransaction==TRANS_NONE );
74208 /* Call lockBtree() until either pBt->pPage1 is populated or
74210 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
74211 ** reading page 1 it discovers that the page-size of the database
74212 ** file is not pBt->pageSize. In this case lockBtree() will update
74213 ** pBt->pageSize to the page-size of the file on disk.
74215 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
74218 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
74221 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
74224 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
74237 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
74245 if( p->inTrans==TRANS_NONE ){
74246 pBt->nTransaction++;
74248 if( p->sharable ){
74249 assert( p->lock.pBtree==p && p->lock.iTable==1 );
74250 p->lock.eLock = READ_LOCK;
74251 p->lock.pNext = pBt->pLock;
74252 pBt->pLock = &p->lock;
74256 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
74257 if( p->inTrans>pBt->inTransaction ){
74258 pBt->inTransaction = p->inTrans;
74261 MemPage *pPage1 = pBt->pPage1;
74263 assert( !pBt->pWriter );
74264 pBt->pWriter = p;
74265 pBt->btsFlags &= ~BTS_EXCLUSIVE;
74266 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
74269 /* If the db-size header field is incorrect (as it may be if an old
74272 ** re-read the database size from page 1 if a savepoint or transaction
74275 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
74276 rc = sqlite3PagerWrite(pPage1->pDbPage);
74278 put4byte(&pPage1->aData[28], pBt->nPage);
74287 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
74292 ** the sub-journal is not already open, then it will be opened here.
74294 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
74302 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
74304 if( p->sharable
74305 || p->inTrans==TRANS_NONE
74306 || (p->inTrans==TRANS_READ && wrflag!=0)
74310 pBt = p->pBt;
74312 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
74317 ** the sub-journal is not already open, then it will be opened here.
74319 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
74328 ** Set the pointer-map entries for all children of page pPage. Also, if
74332 static int setChildPtrmaps(MemPage *pPage){
74333 int i; /* Counter variable */
74334 int nCell; /* Number of cells in page pPage */
74335 int rc; /* Return code */
74336 BtShared *pBt = pPage->pBt;
74337 Pgno pgno = pPage->pgno;
74339 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74340 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74342 nCell = pPage->nCell;
74349 if( !pPage->leaf ){
74355 if( !pPage->leaf ){
74356 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74368 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
74371 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
74374 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
74377 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
74378 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74379 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
74382 if( get4byte(pPage->aData)!=iFrom ){
74385 put4byte(pPage->aData, iTo);
74387 int i;
74388 int nCell;
74389 int rc;
74391 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74393 nCell = pPage->nCell;
74399 pPage->xParseCell(pPage, pCell, &info);
74401 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
74404 if( iFrom==get4byte(pCell+info.nSize-4) ){
74405 put4byte(pCell+info.nSize-4, iTo);
74410 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
74422 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
74425 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
74437 ** the journal needs to be sync()ed before database page pDbPage->pgno
74441 static int relocatePage(
74445 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
74447 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
74450 Pgno iDbPage = pDbPage->pgno;
74451 Pager *pPager = pBt->pPager;
74452 int rc;
74456 assert( sqlite3_mutex_held(pBt->mutex) );
74457 assert( pDbPage->pBt==pBt );
74463 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
74467 pDbPage->pgno = iFreePage;
74469 /* If pDbPage was a btree-page, then it may have child pages and/or cells
74483 Pgno nextOvfl = get4byte(pDbPage->aData);
74501 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
74516 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
74519 ** Perform a single step of an incremental-vacuum. If successful, return
74524 ** More specifically, this function attempts to re-organize the database so
74530 ** If the bCommit parameter is non-zero, this function assumes that the
74532 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
74535 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
74536 Pgno nFreeList; /* Number of pages still on the free-list */
74537 int rc;
74539 assert( sqlite3_mutex_held(pBt->mutex) );
74546 nFreeList = get4byte(&pBt->pPage1->aData[36]);
74561 /* Remove the page from the files free-list. This is not required
74562 ** if bCommit is non-zero. In that case, the free-list will be
74590 ** looping until a free-page located within the first nFin pages
74623 iLastPg--;
74625 pBt->bDoTruncate = 1;
74626 pBt->nPage = iLastPg;
74632 ** The database opened by the first argument is an auto-vacuum database
74634 ** size of the database in pages following an auto-vacuum operation.
74637 int nEntry; /* Number of entries on one ptrmap page */
74641 nEntry = pBt->usableSize/5;
74642 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
74643 nFin = nOrig - nFree - nPtrmap;
74645 nFin--;
74648 nFin--;
74655 ** A write-transaction must be opened before calling this function.
74662 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
74663 int rc;
74664 BtShared *pBt = p->pBt;
74667 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
74668 if( !pBt->autoVacuum ){
74672 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
74684 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74685 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
74697 ** is committed for an auto-vacuum database.
74699 static int autoVacuumCommit(Btree *p){
74700 int rc = SQLITE_OK;
74704 VVA_ONLY( int nRef );
74707 pBt = p->pBt;
74708 pPager = pBt->pPager;
74711 assert( sqlite3_mutex_held(pBt->mutex) );
74713 assert(pBt->autoVacuum);
74714 if( !pBt->incrVacuum ){
74724 ** is either a pointer-map page or the pending-byte page. If one
74730 nFree = get4byte(&pBt->pPage1->aData[36]);
74731 db = p->db;
74732 if( db->xAutovacPages ){
74733 int iDb;
74734 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
74735 if( db->aDb[iDb].pBt==p ) break;
74737 nVac = db->xAutovacPages(
74738 db->pAutovacPagesArg,
74739 db->aDb[iDb].zDbSName,
74742 pBt->pageSize
74758 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
74762 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74764 put4byte(&pBt->pPage1->aData[32], 0);
74765 put4byte(&pBt->pPage1->aData[36], 0);
74767 put4byte(&pBt->pPage1->aData[28], nFin);
74768 pBt->bDoTruncate = 1;
74769 pBt->nPage = nFin;
74785 ** This routine does the first phase of a two-phase commit. This routine
74797 ** This call is a no-op if no write-transaction is currently active on pBt.
74800 ** the name of a super-journal file that should be written into the
74801 ** individual journal file, or is NULL, indicating no super-journal file
74804 ** When this is called, the super-journal should already have been
74808 ** the write-transaction for this database file is to delete the journal.
74810 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
74811 int rc = SQLITE_OK;
74812 if( p->inTrans==TRANS_WRITE ){
74813 BtShared *pBt = p->pBt;
74816 if( pBt->autoVacuum ){
74823 if( pBt->bDoTruncate ){
74824 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
74827 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
74838 BtShared *pBt = p->pBt;
74839 sqlite3 *db = p->db;
74843 pBt->bDoTruncate = 0;
74845 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
74847 ** handle, downgrade to a read-only transaction. The other statements
74850 p->inTrans = TRANS_READ;
74856 if( p->inTrans!=TRANS_NONE ){
74858 pBt->nTransaction--;
74859 if( 0==pBt->nTransaction ){
74860 pBt->inTransaction = TRANS_NONE;
74866 p->inTrans = TRANS_NONE;
74876 ** This routine implements the second phase of a 2-phase commit. The
74888 ** is non-zero then this b-tree transaction is part of a multi-file
74890 ** (by deleting a super-journal file) and the caller will ignore this
74892 ** reset the b-tree objects internal state to indicate that the write
74899 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
74901 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
74905 /* If the handle has a write-transaction open, commit the shared-btrees
74908 if( p->inTrans==TRANS_WRITE ){
74909 int rc;
74910 BtShared *pBt = p->pBt;
74911 assert( pBt->inTransaction==TRANS_WRITE );
74912 assert( pBt->nTransaction>0 );
74913 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
74918 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
74919 pBt->inTransaction = TRANS_READ;
74931 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
74932 int rc;
74953 ** flag is true, then only write-cursors need be tripped - read-only
74957 ** rolled back modified the database schema. In this case b-tree root
74962 ** saving the current position of a read-only cursor, all cursors,
74963 ** including all read-cursors are tripped.
74968 SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
74970 int rc = SQLITE_OK;
74975 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
74976 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
74977 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
74986 p->eState = CURSOR_FAULT;
74987 p->skipNext = errCode;
74997 ** Set the pBt->nPage field correctly, according to the current
74998 ** state of the database. Assume pBt->pPage1 is valid.
75001 int nPage = get4byte(&pPage1->aData[28]);
75003 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
75004 testcase( pBt->nPage!=(u32)nPage );
75005 pBt->nPage = nPage;
75019 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
75020 int rc;
75021 BtShared *pBt = p->pBt;
75034 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
75040 if( p->inTrans==TRANS_WRITE ){
75041 int rc2;
75043 assert( TRANS_WRITE==pBt->inTransaction );
75044 rc2 = sqlite3PagerRollback(pBt->pPager);
75049 /* The rollback may have destroyed the pPage1->aData value. So
75051 ** sure pPage1->aData is set correctly. */
75057 pBt->inTransaction = TRANS_READ;
75077 ** A statement sub-transaction is implemented as an anonymous savepoint. The
75079 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
75080 ** are no active savepoints and no other statement-transactions open,
75084 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
75085 int rc;
75086 BtShared *pBt = p->pBt;
75088 assert( p->inTrans==TRANS_WRITE );
75089 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
75091 assert( iStatement>p->db->nSavepoint );
75092 assert( pBt->inTransaction==TRANS_WRITE );
75098 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
75110 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
75115 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
75116 int rc = SQLITE_OK;
75117 if( p && p->inTrans==TRANS_WRITE ){
75118 BtShared *pBt = p->pBt;
75120 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
75126 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
75129 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
75130 pBt->nPage = 0;
75133 btreeSetNPage(pBt, pBt->pPage1);
75135 /* pBt->nPage might be zero if the database was corrupt when
75137 assert( CORRUPT_DB || pBt->nPage>0 );
75146 ** iTable. If a read-only cursor is requested, it is assumed that
75147 ** the caller already has at least a read-only transaction open
75148 ** on the database already. If a write-cursor is requested, then
75165 ** 3: The database must be writable (not on read-only media)
75176 ** operations on this cursor can be no-ops and all READ operations can
75177 ** return a null row (2-bytes: 0x01 0x00).
75180 ** root page of a b-tree. If it is not, then the cursor acquired
75186 static int btreeCursor(
75189 int wrFlag, /* 1 to write. 0 read-only */
75193 BtShared *pBt = p->pBt; /* Shared b-tree handle */
75203 ** b-tree database, the connection is holding the required table locks,
75211 assert( p->inTrans>TRANS_NONE );
75212 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
75213 assert( pBt->pPage1 && pBt->pPage1->aData );
75214 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
75227 pCur->pgnoRoot = iTable;
75228 pCur->iPage = -1;
75229 pCur->pKeyInfo = pKeyInfo;
75230 pCur->pBtree = p;
75231 pCur->pBt = pBt;
75232 pCur->curFlags = 0;
75235 for(pX=pBt->pCursor; pX; pX=pX->pNext){
75236 if( pX->pgnoRoot==iTable ){
75237 pX->curFlags |= BTCF_Multiple;
75238 pCur->curFlags = BTCF_Multiple;
75241 pCur->eState = CURSOR_INVALID;
75242 pCur->pNext = pBt->pCursor;
75243 pBt->pCursor = pCur;
75245 pCur->curFlags |= BTCF_WriteFlag;
75246 pCur->curPagerFlags = 0;
75247 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
75249 pCur->curPagerFlags = PAGER_GET_READONLY;
75253 static int btreeCursorWithLock(
75256 int wrFlag, /* 1 to write. 0 read-only */
75260 int rc;
75266 SQLITE_PRIVATE int sqlite3BtreeCursor(
75269 int wrFlag, /* 1 to write. 0 read-only */
75273 if( p->sharable ){
75285 ** to users so they cannot do the sizeof() themselves - they must call
75288 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
75298 ** of run-time by skipping the initialization of those elements.
75308 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
75309 Btree *pBtree = pCur->pBtree;
75311 BtShared *pBt = pCur->pBt;
75313 assert( pBt->pCursor!=0 );
75314 if( pBt->pCursor==pCur ){
75315 pBt->pCursor = pCur->pNext;
75317 BtCursor *pPrev = pBt->pCursor;
75319 if( pPrev->pNext==pCur ){
75320 pPrev->pNext = pCur->pNext;
75323 pPrev = pPrev->pNext;
75328 sqlite3_free(pCur->aOverflow);
75329 sqlite3_free(pCur->pKey);
75330 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
75333 assert( pBtree->sharable==0 );
75338 pCur->pBtree = 0;
75352 static int cellInfoEqual(CellInfo *a, CellInfo *b){
75353 if( a->nKey!=b->nKey ) return 0;
75354 if( a->pPayload!=b->pPayload ) return 0;
75355 if( a->nPayload!=b->nPayload ) return 0;
75356 if( a->nLocal!=b->nLocal ) return 0;
75357 if( a->nSize!=b->nSize ) return 0;
75363 btreeParseCell(pCur->pPage, pCur->ix, &info);
75364 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
75370 if( pCur->info.nSize==0 ){
75371 pCur->curFlags |= BTCF_ValidNKey;
75372 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
75381 ** that is currently pointing to a row in a (non-empty) table.
75384 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
75385 return pCur && pCur->eState==CURSOR_VALID;
75388 SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
75390 return pCur->eState==CURSOR_VALID;
75401 assert( pCur->eState==CURSOR_VALID );
75402 assert( pCur->curIntKey );
75404 return pCur->info.nKey;
75411 assert( (pCur->curFlags & BTCF_Pinned)==0 );
75412 pCur->curFlags |= BTCF_Pinned;
75415 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
75416 pCur->curFlags &= ~BTCF_Pinned;
75425 assert( pCur->eState==CURSOR_VALID );
75427 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
75428 (i64)(pCur->info.pPayload - pCur->pPage->aData);
75436 ** The caller must guarantee that the cursor is pointing to a non-NULL
75442 assert( pCur->eState==CURSOR_VALID );
75444 return pCur->info.nPayload;
75462 assert( pCur->eState==CURSOR_VALID );
75463 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
75469 ** linked list of overflow pages. If possible, it uses the auto-vacuum
75470 ** pointer-map data instead of reading the content of page ovfl to do so.
75482 ** the pointer-map was used to obtain the value for *pPgnoNext), then
75485 static int getOverflowPage(
75493 int rc = SQLITE_OK;
75495 assert( sqlite3_mutex_held(pBt->mutex) );
75500 ** autovacuum pointer-map pages. Guess that the next page in
75505 if( pBt->autoVacuum ){
75529 next = get4byte(pPage->aData);
75553 static int copyPayload(
75556 int nByte, /* Number of bytes to copy */
75557 int eOp, /* 0 -> copy from page, 1 -> copy to page */
75562 int rc = sqlite3PagerWrite(pDbPage);
75590 ** the overflow page-list cache array (BtCursor.aOverflow).
75594 ** Once an overflow page-list cache has been allocated, it must be
75596 ** the cursor is moved to a different row. Additionally, in auto-vacuum
75597 ** mode, the following events may invalidate an overflow page-list cache.
75603 static int accessPayload(
75608 int eOp /* zero to read. non-zero to write. */
75611 int rc = SQLITE_OK;
75612 int iIdx = 0;
75613 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
75614 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
75621 assert( pCur->eState==CURSOR_VALID );
75622 if( pCur->ix>=pPage->nCell ){
75628 aPayload = pCur->info.pPayload;
75629 assert( offset+amt <= pCur->info.nPayload );
75631 assert( aPayload > pPage->aData );
75632 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
75635 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
75642 if( offset<pCur->info.nLocal ){
75643 int a = amt;
75644 if( a+offset>pCur->info.nLocal ){
75645 a = pCur->info.nLocal - offset;
75647 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
75650 amt -= a;
75652 offset -= pCur->info.nLocal;
75657 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
75660 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
75669 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
75670 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
75671 if( pCur->aOverflow==0
75672 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
75678 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
75683 pCur->aOverflow = aNew;
75686 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
75687 pCur->curFlags |= BTCF_ValidOvfl;
75690 assert( pCur->aOverflow[0]==nextPage
75691 || pCur->aOverflow[0]==0
75693 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 );
75695 /* If the overflow page-list cache has been allocated and the
75699 if( pCur->aOverflow[offset/ovflSize] ){
75701 nextPage = pCur->aOverflow[iIdx];
75708 /* If required, populate the overflow page-list cache. */
75709 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
75710 assert( pCur->aOverflow[iIdx]==0
75711 || pCur->aOverflow[iIdx]==nextPage
75713 pCur->aOverflow[iIdx] = nextPage;
75719 ** page-list cache, if any, then fall back to the getOverflowPage()
75722 assert( pCur->curFlags & BTCF_ValidOvfl );
75723 assert( pCur->pBtree->db==pBt->db );
75724 if( pCur->aOverflow[iIdx+1] ){
75725 nextPage = pCur->aOverflow[iIdx+1];
75729 offset -= ovflSize;
75734 int a = amt;
75736 a = ovflSize - offset;
75744 ** 3) there are no dirty pages in the page-cache
75745 ** 4) the database is file-backed, and
75750 ** output buffer, bypassing the page-cache altogether. This speeds
75755 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
75756 && &pBuf[-4]>=pBufStart /* (6) */
75758 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
75760 u8 *aWrite = &pBuf[-4];
75763 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
75771 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
75782 amt -= a;
75803 ** pCur can be pointing to either a table or an index b-tree.
75805 ** pCur is pointing to an index b-tree then the key section is read.
75815 SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
75817 assert( pCur->eState==CURSOR_VALID );
75818 assert( pCur->iPage>=0 && pCur->pPage );
75828 static SQLITE_NOINLINE int accessPayloadChecked(
75834 int rc;
75835 if ( pCur->eState==CURSOR_INVALID ){
75842 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
75843 if( pCur->eState==CURSOR_VALID ){
75855 ** the key if index btrees (pPage->intKey==0) and is the data for
75856 ** table btrees (pPage->intKey==1). The number of bytes of available
75875 int amt;
75876 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
75877 assert( pCur->eState==CURSOR_VALID );
75878 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75880 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75881 assert( pCur->info.nSize>0 );
75882 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
75883 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
75884 amt = pCur->info.nLocal;
75885 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
75889 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
75892 return (void*)pCur->info.pPayload;
75899 ** b-tree page. Write the number of available bytes into *pAmt.
75919 ** This function returns SQLITE_CORRUPT if the page-header flags field of
75921 ** if an intkey page appears to be the parent of a non-intkey page, or
75922 ** vice-versa).
75924 static int moveToChild(BtCursor *pCur, u32 newPgno){
75925 int rc;
75927 assert( pCur->eState==CURSOR_VALID );
75928 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
75929 assert( pCur->iPage>=0 );
75930 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
75933 pCur->info.nSize = 0;
75934 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75935 pCur->aiIdx[pCur->iPage] = pCur->ix;
75936 pCur->apPage[pCur->iPage] = pCur->pPage;
75937 pCur->ix = 0;
75938 pCur->iPage++;
75939 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
75940 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
75942 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75944 releasePage(pCur->pPage);
75948 pCur->pPage = pCur->apPage[--pCur->iPage];
75955 ** Page pParent is an internal (non-leaf) tree page. This function
75956 ** asserts that page number iChild is the left-child if the iIdx'th
75958 ** cells in pParent, that page number iChild is the right-child of
75961 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
75964 assert( iIdx<=pParent->nCell );
75965 if( iIdx==pParent->nCell ){
75966 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
75978 ** pCur->idx is set to the cell index that contains the pointer
75980 ** right-most child page then pCur->idx is set to one more than
75986 assert( pCur->eState==CURSOR_VALID );
75987 assert( pCur->iPage>0 );
75988 assert( pCur->pPage );
75990 pCur->apPage[pCur->iPage-1],
75991 pCur->aiIdx[pCur->iPage-1],
75992 pCur->pPage->pgno
75994 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
75995 pCur->info.nSize = 0;
75996 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75997 pCur->ix = pCur->aiIdx[pCur->iPage-1];
75998 pLeaf = pCur->pPage;
75999 pCur->pPage = pCur->apPage[--pCur->iPage];
76004 ** Move the cursor to point to the root page of its b-tree structure.
76011 ** If the b-tree structure is empty, the cursor state is set to
76017 ** page-header flags indicate that the [virtual] root-page is the expected
76018 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
76020 ** indicating a table b-tree, or if the caller did specify a KeyInfo
76022 ** b-tree).
76024 static int moveToRoot(BtCursor *pCur){
76026 int rc = SQLITE_OK;
76032 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
76033 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
76035 if( pCur->iPage>=0 ){
76036 if( pCur->iPage ){
76037 releasePageNotNull(pCur->pPage);
76038 while( --pCur->iPage ){
76039 releasePageNotNull(pCur->apPage[pCur->iPage]);
76041 pRoot = pCur->pPage = pCur->apPage[0];
76044 }else if( pCur->pgnoRoot==0 ){
76045 pCur->eState = CURSOR_INVALID;
76048 assert( pCur->iPage==(-1) );
76049 if( pCur->eState>=CURSOR_REQUIRESEEK ){
76050 if( pCur->eState==CURSOR_FAULT ){
76051 assert( pCur->skipNext!=SQLITE_OK );
76052 return pCur->skipNext;
76056 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
76057 pCur->curPagerFlags);
76059 pCur->eState = CURSOR_INVALID;
76062 pCur->iPage = 0;
76063 pCur->curIntKey = pCur->pPage->intKey;
76065 pRoot = pCur->pPage;
76066 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
76068 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
76069 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
76070 ** NULL, the caller expects a table b-tree. If this is not the case,
76075 ** if pCur->iPage>=0). But this is not so if the database is corrupted
76076 ** in such a way that page pRoot is linked into a second b-tree table
76078 assert( pRoot->intKey==1 || pRoot->intKey==0 );
76079 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
76080 return SQLITE_CORRUPT_PAGE(pCur->pPage);
76084 pCur->ix = 0;
76085 pCur->info.nSize = 0;
76086 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
76088 if( pRoot->nCell>0 ){
76089 pCur->eState = CURSOR_VALID;
76090 }else if( !pRoot->leaf ){
76092 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
76093 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
76094 pCur->eState = CURSOR_VALID;
76097 pCur->eState = CURSOR_INVALID;
76104 ** Move the cursor down to the left-most leaf entry beneath the
76107 ** The left-most leaf is the one with the smallest key - the first
76110 static int moveToLeftmost(BtCursor *pCur){
76112 int rc = SQLITE_OK;
76116 assert( pCur->eState==CURSOR_VALID );
76117 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
76118 assert( pCur->ix<pPage->nCell );
76119 pgno = get4byte(findCell(pPage, pCur->ix));
76126 ** Move the cursor down to the right-most leaf entry beneath the
76129 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
76130 ** finds the right-most entry beneath the *page*.
76132 ** The right-most entry is the one with the largest key - the last
76135 static int moveToRightmost(BtCursor *pCur){
76137 int rc = SQLITE_OK;
76141 assert( pCur->eState==CURSOR_VALID );
76142 while( !(pPage = pCur->pPage)->leaf ){
76143 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76144 pCur->ix = pPage->nCell;
76148 pCur->ix = pPage->nCell-1;
76149 assert( pCur->info.nSize==0 );
76150 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
76158 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
76159 int rc;
76162 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76165 assert( pCur->pPage->nCell>0 );
76169 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
76184 static int cursorIsAtLastEntry(BtCursor *pCur){
76185 int ii;
76186 for(ii=0; ii<pCur->iPage; ii++){
76187 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
76189 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
76197 static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
76198 int rc = moveToRoot(pCur);
76200 assert( pCur->eState==CURSOR_VALID );
76204 pCur->curFlags |= BTCF_AtLast;
76206 pCur->curFlags &= ~BTCF_AtLast;
76209 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76215 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
76217 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76219 /* If the cursor already points to the last entry, this is a no-op. */
76220 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
76251 SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
76254 int biasRight, /* If true, bias the search to the high end */
76255 int *pRes /* Write search results here */
76257 int rc;
76260 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76262 assert( pCur->pKeyInfo==0 );
76263 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
76267 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
76268 if( pCur->info.nKey==intKey ){
76272 if( pCur->info.nKey<intKey ){
76273 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
76275 *pRes = -1;
76282 if( pCur->info.nKey+1==intKey ){
76287 if( pCur->info.nKey==intKey ){
76298 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76304 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76305 *pRes = -1;
76310 assert( pCur->pPage );
76311 assert( pCur->pPage->isInit );
76312 assert( pCur->eState==CURSOR_VALID );
76313 assert( pCur->pPage->nCell > 0 );
76314 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
76315 assert( pCur->curIntKey );
76318 int lwr, upr, idx, c;
76320 MemPage *pPage = pCur->pPage;
76323 /* pPage->nCell must be greater than zero. If this is the root-page
76325 ** not run. If this is not the root-page, then the moveToChild() routine
76327 ** be the right kind (index or table) of b-tree page. Otherwise
76329 assert( pPage->nCell>0 );
76330 assert( pPage->intKey );
76332 upr = pPage->nCell-1;
76334 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
76338 if( pPage->intKeyLeaf ){
76340 if( pCell>=pPage->aDataEnd ){
76348 if( lwr>upr ){ c = -1; break; }
76350 upr = idx-1;
76354 pCur->ix = (u16)idx;
76355 if( !pPage->leaf ){
76359 pCur->curFlags |= BTCF_ValidNKey;
76360 pCur->info.nKey = nCellKey;
76361 pCur->info.nSize = 0;
76369 assert( lwr==upr+1 || !pPage->leaf );
76370 assert( pPage->isInit );
76371 if( pPage->leaf ){
76372 assert( pCur->ix<pCur->pPage->nCell );
76373 pCur->ix = (u16)idx;
76379 if( lwr>=pPage->nCell ){
76380 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76384 pCur->ix = (u16)lwr;
76389 pCur->info.nSize = 0;
76390 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76395 ** Compare the "idx"-th cell on the page the cursor pCur is currently
76410 static int indexCellCompare(
76412 int idx,
76416 MemPage *pPage = pCur->pPage;
76417 int c;
76418 int nCell; /* Size of the pCell cell in bytes */
76422 if( nCell<=pPage->max1bytePayload ){
76423 /* This branch runs if the record-size field of the cell is a
76425 ** b-tree page. */
76426 testcase( pCell+nCell+1==pPage->aDataEnd );
76429 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76431 /* The record-size field is a 2 byte varint and the record
76432 ** fits entirely on the main b-tree page. */
76433 testcase( pCell+nCell+2==pPage->aDataEnd );
76444 ** Return true (non-zero) if pCur is current pointing to the last
76447 static int cursorOnLastPage(BtCursor *pCur){
76448 int i;
76449 assert( pCur->eState==CURSOR_VALID );
76450 for(i=0; i<pCur->iPage; i++){
76451 MemPage *pPage = pCur->apPage[i];
76452 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
76480 ** The pIdxKey->eqSeen field is set to 1 if there
76483 SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
76486 int *pRes /* Write search results here */
76488 int rc;
76492 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76494 assert( pCur->pKeyInfo!=0 );
76497 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76501 pIdxKey->errCode = 0;
76502 assert( pIdxKey->default_rc==1
76503 || pIdxKey->default_rc==0
76504 || pIdxKey->default_rc==-1
76519 if( pCur->eState==CURSOR_VALID
76520 && pCur->pPage->leaf
76523 int c;
76524 if( pCur->ix==pCur->pPage->nCell-1
76525 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
76526 && pIdxKey->errCode==SQLITE_OK
76531 if( pCur->iPage>0
76533 && pIdxKey->errCode==SQLITE_OK
76535 pCur->curFlags &= ~BTCF_ValidOvfl;
76536 if( !pCur->pPage->isInit ){
76541 pIdxKey->errCode = SQLITE_OK;
76547 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76548 *pRes = -1;
76555 assert( pCur->pPage );
76556 assert( pCur->pPage->isInit );
76557 assert( pCur->eState==CURSOR_VALID );
76558 assert( pCur->pPage->nCell > 0 );
76559 assert( pCur->curIntKey==0 );
76562 int lwr, upr, idx, c;
76564 MemPage *pPage = pCur->pPage;
76567 /* pPage->nCell must be greater than zero. If this is the root-page
76569 ** not run. If this is not the root-page, then the moveToChild() routine
76571 ** be the right kind (index or table) of b-tree page. Otherwise
76573 assert( pPage->nCell>0 );
76574 assert( pPage->intKey==0 );
76576 upr = pPage->nCell-1;
76579 int nCell; /* Size of the pCell cell in bytes */
76582 /* The maximum supported page-size is 65536 bytes. This means that
76583 ** the maximum number of record bytes stored on an index B-Tree
76584 ** page is less than 16384 bytes and may be stored as a 2-byte
76587 ** stored entirely within the b-tree page by inspecting the first
76591 if( nCell<=pPage->max1bytePayload ){
76592 /* This branch runs if the record-size field of the cell is a
76594 ** b-tree page. */
76595 testcase( pCell+nCell+1==pPage->aDataEnd );
76598 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76600 /* The record-size field is a 2 byte varint and the record
76601 ** fits entirely on the main b-tree page. */
76602 testcase( pCell+nCell+2==pPage->aDataEnd );
76615 u8 * const pCellBody = pCell - pPage->childPtrSize;
76616 const int nOverrun = 18; /* Size of the overrun padding */
76617 pPage->xParseCell(pPage, pCellBody, &pCur->info);
76618 nCell = (int)pCur->info.nKey;
76623 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
76632 pCur->ix = (u16)idx;
76635 pCur->curFlags &= ~BTCF_ValidOvfl;
76644 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
76645 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
76650 upr = idx-1;
76655 pCur->ix = (u16)idx;
76656 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
76663 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
76664 assert( pPage->isInit );
76665 if( pPage->leaf ){
76666 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
76667 pCur->ix = (u16)idx;
76672 if( lwr>=pPage->nCell ){
76673 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76678 /* This block is similar to an in-lined version of:
76680 ** pCur->ix = (u16)lwr;
76684 pCur->info.nSize = 0;
76685 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76686 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
76689 pCur->aiIdx[pCur->iPage] = (u16)lwr;
76690 pCur->apPage[pCur->iPage] = pCur->pPage;
76691 pCur->ix = 0;
76692 pCur->iPage++;
76693 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
76695 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76697 releasePage(pCur->pPage);
76701 pCur->pPage = pCur->apPage[--pCur->iPage];
76705 ***** End of in-lined moveToChild() call */
76708 pCur->info.nSize = 0;
76709 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76721 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
76726 return (CURSOR_VALID!=pCur->eState);
76739 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76744 if( pCur->eState!=CURSOR_VALID ) return 0;
76745 if( NEVER(pCur->pPage->leaf==0) ) return -1;
76747 n = pCur->pPage->nCell;
76748 for(i=0; i<pCur->iPage; i++){
76749 n *= pCur->apPage[i]->nCell;
76774 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
76775 int rc;
76776 int idx;
76780 if( pCur->eState!=CURSOR_VALID ){
76781 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76786 if( CURSOR_INVALID==pCur->eState ){
76789 if( pCur->eState==CURSOR_SKIPNEXT ){
76790 pCur->eState = CURSOR_VALID;
76791 if( pCur->skipNext>0 ) return SQLITE_OK;
76795 pPage = pCur->pPage;
76796 idx = ++pCur->ix;
76797 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76798 if( !pPage->isInit ){
76802 if( idx>=pPage->nCell ){
76803 if( !pPage->leaf ){
76804 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76809 if( pCur->iPage==0 ){
76810 pCur->eState = CURSOR_INVALID;
76814 pPage = pCur->pPage;
76815 }while( pCur->ix>=pPage->nCell );
76816 if( pPage->intKey ){
76822 if( pPage->leaf ){
76828 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
76833 pCur->info.nSize = 0;
76834 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76835 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
76836 pPage = pCur->pPage;
76837 if( (++pCur->ix)>=pPage->nCell ){
76838 pCur->ix--;
76841 if( pPage->leaf ){
76868 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
76869 int rc;
76873 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
76874 assert( pCur->info.nSize==0 );
76875 if( pCur->eState!=CURSOR_VALID ){
76880 if( CURSOR_INVALID==pCur->eState ){
76883 if( CURSOR_SKIPNEXT==pCur->eState ){
76884 pCur->eState = CURSOR_VALID;
76885 if( pCur->skipNext<0 ) return SQLITE_OK;
76889 pPage = pCur->pPage;
76890 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76891 if( !pPage->isInit ){
76894 if( !pPage->leaf ){
76895 int idx = pCur->ix;
76900 while( pCur->ix==0 ){
76901 if( pCur->iPage==0 ){
76902 pCur->eState = CURSOR_INVALID;
76907 assert( pCur->info.nSize==0 );
76908 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
76910 pCur->ix--;
76911 pPage = pCur->pPage;
76912 if( pPage->intKey && !pPage->leaf ){
76920 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
76924 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
76925 pCur->info.nSize = 0;
76926 if( pCur->eState!=CURSOR_VALID
76927 || pCur->ix==0
76928 || pCur->pPage->leaf==0
76932 pCur->ix--;
76953 ** anywhere on the free-list, then it is guaranteed to be returned. If
76958 static int allocateBtreePage(
76966 int rc;
76973 assert( sqlite3_mutex_held(pBt->mutex) );
76974 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
76975 pPage1 = pBt->pPage1;
76977 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
76979 n = get4byte(&pPage1->aData[36]);
76980 testcase( n==mxPage-1 );
76987 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
76990 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
76991 ** shows that the page 'nearby' is somewhere on the free-list, then
76992 ** the entire-list will be searched for that page.
76999 assert( pBt->autoVacuum );
77011 /* Decrement the free-list count by 1. Set iTrunk to the index of the
77012 ** first free-list trunk page. iPrevTrunk is initially 1.
77014 rc = sqlite3PagerWrite(pPage1->pDbPage);
77016 put4byte(&pPage1->aData[36], n-1);
77019 ** is not true. Otherwise, it runs once for each trunk-page on the
77020 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
77026 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
77029 iTrunk = get4byte(&pPrevTrunk->aData[0]);
77031 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
77034 iTrunk = get4byte(&pPage1->aData[32]);
77038 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
77047 assert( pTrunk->aData!=0 );
77048 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
77050 k = get4byte(&pTrunk->aData[4]);
77056 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77061 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77064 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77065 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
77079 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77085 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77087 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77091 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
77095 ** pointers to free-list leaves. The first leaf becomes a trunk
77099 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
77109 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
77114 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
77115 put4byte(&pNewTrunk->aData[4], k-1);
77116 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
77119 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
77120 put4byte(&pPage1->aData[32], iNewTrunk);
77122 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77126 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
77130 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77136 unsigned char *aData = pTrunk->aData;
77149 int dist;
77150 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
77152 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
77173 int noContent;
77177 *pPgno, closest+1, k, pTrunk->pgno, n-1));
77178 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77180 if( closest<k-1 ){
77183 put4byte(&aData[4], k-1);
77187 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77204 ** pager layer with the 'no-content' flag set. This prevents the pager
77206 ** current transaction has already run one or more incremental-vacuum
77209 ** not set the no-content flag. This causes the pager to load and journal
77214 ** file on disk. So the effects of disabling the no-content optimization
77218 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
77220 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77222 pBt->nPage++;
77223 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
77226 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
77227 /* If *pPgno refers to a pointer-map page, allocate two new pages
77229 ** becomes a new pointer-map page, the second is used by the caller.
77232 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
77233 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
77234 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
77236 rc = sqlite3PagerWrite(pPg->pDbPage);
77240 pBt->nPage++;
77241 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
77244 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
77245 *pPgno = pBt->nPage;
77250 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77263 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
77264 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
77269 ** This function is used to add page iPage to the database file free-list.
77270 ** It is assumed that the page is not already a part of the free-list.
77280 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
77281 MemPage *pTrunk = 0; /* Free-list trunk page */
77282 Pgno iTrunk = 0; /* Page number of free-list trunk page */
77283 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
77285 int rc; /* Return Code */
77286 u32 nFree; /* Initial number of pages on free-list */
77288 assert( sqlite3_mutex_held(pBt->mutex) );
77290 assert( !pMemPage || pMemPage->pgno==iPage );
77292 if( iPage<2 || iPage>pBt->nPage ){
77297 sqlite3PagerRef(pPage->pDbPage);
77303 rc = sqlite3PagerWrite(pPage1->pDbPage);
77305 nFree = get4byte(&pPage1->aData[36]);
77306 put4byte(&pPage1->aData[36], nFree+1);
77308 if( pBt->btsFlags & BTS_SECURE_DELETE ){
77313 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
77317 memset(pPage->aData, 0, pPage->pBt->pageSize);
77320 /* If the database supports auto-vacuum, write an entry in the pointer-map
77328 /* Now manipulate the actual database free-list structure. There are two
77329 ** possibilities. If the free-list is currently empty, or if the first
77330 ** trunk page in the free-list is full, then this page will become a
77331 ** new free-list trunk page. Otherwise, it will become a leaf of the
77332 ** first trunk page in the current free-list. This block tests if it
77333 ** is possible to add the page as a new free-list leaf.
77338 iTrunk = get4byte(&pPage1->aData[32]);
77348 nLeaf = get4byte(&pTrunk->aData[4]);
77349 assert( pBt->usableSize>32 );
77350 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
77354 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
77359 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
77362 ** usableSize/4 - 8 entries will be reported as corrupt. In order
77364 ** we will continue to restrict the number of entries to usableSize/4 - 8
77367 ** to read "usableSize/4-2" instead of "usableSize/4-8".
77369 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
77374 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77376 put4byte(&pTrunk->aData[4], nLeaf+1);
77377 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
77378 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
77379 sqlite3PagerDontWrite(pPage->pDbPage);
77383 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
77389 ** the page being freed as a leaf page of the first trunk in the free-list.
77390 ** Possibly because the free-list is empty, or possibly because the
77391 ** first trunk in the free-list is full. Either way, the page being freed
77392 ** will become the new first trunk page in the free-list.
77397 rc = sqlite3PagerWrite(pPage->pDbPage);
77401 put4byte(pPage->aData, iTrunk);
77402 put4byte(&pPage->aData[4], 0);
77403 put4byte(&pPage1->aData[32], iPage);
77404 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
77408 pPage->isInit = 0;
77414 static void freePage(MemPage *pPage, int *pRC){
77416 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
77423 static SQLITE_NOINLINE int clearCellOverflow(
77430 int rc;
77431 int nOvfl;
77434 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77435 assert( pInfo->nLocal!=pInfo->nPayload );
77436 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
77437 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
77438 if( pCell + pInfo->nSize > pPage->aDataEnd ){
77442 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
77443 pBt = pPage->pBt;
77444 assert( pBt->usableSize > 4 );
77445 ovflPageSize = pBt->usableSize - 4;
77446 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
77448 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
77450 while( nOvfl-- ){
77465 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
77472 ** freePage2() may zero the page contents if secure-delete mode is
77483 sqlite3PagerUnref(pOvfl->pDbPage);
77498 pPage->xParseCell(pPage, pCell, &sInfo); \
77513 ** Note that pCell does not necessary need to point to the pPage->aData
77515 ** be constructed in this temporary area then copied into pPage->aData
77518 static int fillInCell(
77522 int *pnSize /* Write cell size here */
77524 int nPayload;
77526 int nSrc, n, rc, mn;
77527 int spaceLeft;
77533 int nHeader;
77535 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77539 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
77540 || sqlite3PagerIswriteable(pPage->pDbPage) );
77543 nHeader = pPage->childPtrSize;
77544 if( pPage->intKey ){
77545 nPayload = pX->nData + pX->nZero;
77546 pSrc = pX->pData;
77547 nSrc = pX->nData;
77548 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
77550 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
77552 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
77553 nSrc = nPayload = (int)pX->nKey;
77554 pSrc = pX->pKey;
77560 if( nPayload<=pPage->maxLocal ){
77574 memset(pPayload+nSrc, 0, nPayload-nSrc);
77581 mn = pPage->minLocal;
77582 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
77583 testcase( n==pPage->maxLocal );
77584 testcase( n==pPage->maxLocal+1 );
77585 if( n > pPage->maxLocal ) n = mn;
77591 pBt = pPage->pBt;
77608 pPage->xParseCell(pPage, pCell, &info);
77609 assert( nHeader==(int)(info.pPayload - pCell) );
77610 assert( info.nKey==pX->nKey );
77623 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
77627 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
77628 || sqlite3PagerIswriteable(pPage->pDbPage) );
77638 nPayload -= n;
77642 nSrc -= n;
77643 spaceLeft -= n;
77647 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
77648 if( pBt->autoVacuum ){
77658 /* If the database supports auto-vacuum, and the second or subsequent
77659 ** overflow page is being allocated, add an entry to the pointer-map
77663 ** to the pointer-map. If we write nothing to this pointer-map slot,
77668 if( pBt->autoVacuum && rc==SQLITE_OK ){
77683 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
77687 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
77688 || sqlite3PagerIswriteable(pPage->pDbPage) );
77693 pPrior = pOvfl->aData;
77695 pPayload = &pOvfl->aData[4];
77696 spaceLeft = pBt->usableSize - 4;
77704 ** Remove the i-th cell from pPage. This routine effects pPage only.
77711 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
77713 u8 *data; /* pPage->aData */
77715 int rc; /* The return code */
77716 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
77720 assert( idx<pPage->nCell );
77722 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77723 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77724 assert( pPage->nFree>=0 );
77725 data = pPage->aData;
77726 ptr = &pPage->aCellIdx[2*idx];
77727 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
77729 hdr = pPage->hdrOffset;
77731 testcase( pc+sz==pPage->pBt->usableSize );
77732 if( pc+sz > pPage->pBt->usableSize ){
77741 pPage->nCell--;
77742 if( pPage->nCell==0 ){
77745 put2byte(&data[hdr+5], pPage->pBt->usableSize);
77746 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
77747 - pPage->childPtrSize - 8;
77749 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
77750 put2byte(&data[hdr+3], pPage->nCell);
77751 pPage->nFree += 2;
77762 ** in pPage->apOvfl[] and make it point to the cell content (either
77764 ** Allocating a new entry in pPage->aCell[] implies that
77765 ** pPage->nOverflow is incremented.
77775 static int insertCell(
77777 int i, /* New cell becomes the i-th cell of the page */
77779 int sz, /* Bytes of content in pCell */
77781 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
77783 int idx = 0; /* Where to write new cell content in data[] */
77784 int j; /* Loop counter */
77786 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77788 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77789 assert( MX_CELL(pPage->pBt)<=10921 );
77790 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77791 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77792 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77793 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77794 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77795 assert( pPage->nFree>=0 );
77797 if( pPage->nOverflow || sz+2>pPage->nFree ){
77803 j = pPage->nOverflow++;
77804 /* Comparison against ArraySize-1 since we hold back one extra slot
77807 assert( j < ArraySize(pPage->apOvfl)-1 );
77808 pPage->apOvfl[j] = pCell;
77809 pPage->aiOvfl[j] = (u16)i;
77816 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77817 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77819 int rc = sqlite3PagerWrite(pPage->pDbPage);
77823 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77824 data = pPage->aData;
77825 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77831 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77832 assert( idx+sz <= (int)pPage->pBt->usableSize );
77833 pPage->nFree -= (u16)(2 + sz);
77839 memcpy(&data[idx+4], pCell+4, sz-4);
77841 pIns = pPage->aCellIdx + i*2;
77842 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77844 pPage->nCell++;
77846 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77847 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77849 if( pPage->pBt->autoVacuum ){
77850 int rc2 = SQLITE_OK;
77872 static int insertCellFast(
77874 int i, /* New cell becomes the i-th cell of the page */
77876 int sz /* Bytes of content in pCell */
77878 int idx = 0; /* Where to write new cell content in data[] */
77879 int j; /* Loop counter */
77881 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77883 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77884 assert( MX_CELL(pPage->pBt)<=10921 );
77885 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77886 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77887 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77888 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77889 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77890 assert( pPage->nFree>=0 );
77891 assert( pPage->nOverflow==0 );
77892 if( sz+2>pPage->nFree ){
77893 j = pPage->nOverflow++;
77894 /* Comparison against ArraySize-1 since we hold back one extra slot
77897 assert( j < ArraySize(pPage->apOvfl)-1 );
77898 pPage->apOvfl[j] = pCell;
77899 pPage->aiOvfl[j] = (u16)i;
77906 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77907 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77909 int rc = sqlite3PagerWrite(pPage->pDbPage);
77913 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77914 data = pPage->aData;
77915 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77921 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77922 assert( idx+sz <= (int)pPage->pBt->usableSize );
77923 pPage->nFree -= (u16)(2 + sz);
77925 pIns = pPage->aCellIdx + i*2;
77926 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77928 pPage->nCell++;
77930 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77931 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77933 if( pPage->pBt->autoVacuum ){
77934 int rc2 = SQLITE_OK;
77959 ** tunable - as if you could change them and recompile and it would all work.
77974 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
77986 ** -----------
77988 ** -----------
77991 ** --------- --------- ---------
77992 ** |Child-1| |Child-2| |Child-3|
77993 ** --------- --------- ---------
77997 ** 1. All cells from Child-1 in order
77999 ** 3. All cells from Child-2 in order
78001 ** 5. All cells from Child-3 in order
78003 ** For a table-btree (with rowids) the items 2 and 4 are empty because
78007 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
78011 ** ixNx[0] = Number of cells in Child-1.
78012 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
78013 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
78014 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
78017 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
78020 ** ixNx[0] = Number of cells in Child-1.
78021 ** ixNx[1] = Number of cells in Child-1 and Child-2.
78031 int nCell; /* Number of cells in apCell[] */
78036 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
78040 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
78043 static void populateCellCache(CellArray *p, int idx, int N){
78044 MemPage *pRef = p->pRef;
78045 u16 *szCell = p->szCell;
78046 assert( idx>=0 && idx+N<=p->nCell );
78048 assert( p->apCell[idx]!=0 );
78050 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
78053 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
78056 N--;
78063 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
78064 assert( N>=0 && N<p->nCell );
78065 assert( p->szCell[N]==0 );
78066 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
78067 return p->szCell[N];
78069 static u16 cachedCellSize(CellArray *p, int N){
78070 assert( N>=0 && N<p->nCell );
78071 if( p->szCell[N] ) return p->szCell[N];
78076 ** Array apCell[] contains pointers to nCell b-tree page cells. The
78088 static int rebuildPage(
78090 int iFirst, /* First cell in pCArray to use */
78091 int nCell, /* Final number of cells on page */
78094 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
78095 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
78096 const int usableSize = pPg->pBt->usableSize;
78098 int i = iFirst; /* Which cell to copy from pCArray*/
78100 int iEnd = i+nCell; /* Loop terminator */
78101 u8 *pCellptr = pPg->aCellIdx;
78102 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78104 int k; /* Current slot in pCArray->apEnd[] */
78105 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
78111 memcpy(&pTmp[j], &aData[j], usableSize - j);
78113 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i; k++){}
78114 pSrcEnd = pCArray->apEnd[k];
78118 u8 *pCell = pCArray->apCell[i];
78119 u16 sz = pCArray->szCell[i];
78123 pCell = &pTmp[pCell - aData];
78130 pData -= sz;
78131 put2byte(pCellptr, (pData - aData));
78135 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
78138 if( pCArray->ixNx[k]<=i ){
78140 pSrcEnd = pCArray->apEnd[k];
78144 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78145 pPg->nCell = nCell;
78146 pPg->nOverflow = 0;
78149 put2byte(&aData[hdr+3], pPg->nCell);
78150 put2byte(&aData[hdr+5], pData - aData);
78156 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
78159 ** will fit), non-zero is returned. Otherwise, if the cells are added
78162 ** Argument pCellptr points to the first entry in the cell-pointer array
78164 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
78166 ** that it is safe to overwrite this part of the cell-pointer array.
78174 ** end of the space required by this page for the cell-pointer area (for
78175 ** all cells - not just those inserted by the current call). If the content
78177 ** cells in apCell[], then the cells do not fit and non-zero is returned.
78179 static int pageInsertArray(
78181 u8 *pBegin, /* End of cell-pointer array */
78182 u8 **ppData, /* IN/OUT: Page content-area pointer */
78183 u8 *pCellptr, /* Pointer to cell-pointer area */
78184 int iFirst, /* Index of first cell to add */
78185 int nCell, /* Number of cells to add to pPg */
78188 int i = iFirst; /* Loop counter - cell index to insert */
78189 u8 *aData = pPg->aData; /* Complete page */
78191 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
78192 int k; /* Current slot in pCArray->apEnd[] */
78194 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
78196 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i ; k++){}
78197 pEnd = pCArray->apEnd[k];
78199 int sz, rc;
78201 assert( pCArray->szCell[i]!=0 );
78202 sz = pCArray->szCell[i];
78204 if( (pData - pBegin)<sz ) return 1;
78205 pData -= sz;
78208 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
78211 assert( (pSlot+sz)<=pCArray->apCell[i]
78212 || pSlot>=(pCArray->apCell[i]+sz)
78214 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
78215 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
78221 memmove(pSlot, pCArray->apCell[i], sz);
78222 put2byte(pCellptr, (pSlot - aData));
78226 if( pCArray->ixNx[k]<=i ){
78228 pEnd = pCArray->apEnd[k];
78236 ** The pCArray object contains pointers to b-tree cells and their sizes.
78239 ** that is currently stored within the body of pPg to the pPg free-list.
78240 ** The cell-pointers and other fields of the page are not updated.
78242 ** This function returns the total number of cells added to the free-list.
78244 static int pageFreeArray(
78246 int iFirst, /* First cell to delete */
78247 int nCell, /* Cells to delete */
78250 u8 * const aData = pPg->aData;
78251 u8 * const pEnd = &aData[pPg->pBt->usableSize];
78252 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
78253 int nRet = 0;
78254 int i, j;
78255 int iEnd = iFirst + nCell;
78256 int nFree = 0;
78257 int aOfst[10];
78258 int aAfter[10];
78261 u8 *pCell = pCArray->apCell[i];
78263 int sz;
78264 int iAfter;
78265 int iOfst;
78269 sz = pCArray->szCell[i]; assert( sz>0 );
78270 iOfst = (u16)(pCell - aData);
78282 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
78284 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78297 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78304 ** balanced. The current page, pPg, has pPg->nCell cells starting with
78305 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
78311 ** The pPg->nFree field is invalid when this function returns. It is the
78314 static int editPage(
78316 int iOld, /* Index of first cell currently on page */
78317 int iNew, /* Index of new first cell on page */
78318 int nNew, /* Final number of cells on page */
78321 u8 * const aData = pPg->aData;
78322 const int hdr = pPg->hdrOffset;
78323 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
78324 int nCell = pPg->nCell; /* Cells stored on pPg */
78327 int i;
78328 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
78329 int iNewEnd = iNew + nNew;
78332 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78333 memcpy(pTmp, aData, pPg->pBt->usableSize);
78339 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
78341 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
78342 nCell -= nShift;
78345 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
78347 nCell -= nTail;
78352 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
78356 int nAdd = MIN(nNew,iOld-iNew);
78357 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
78359 pCellptr = pPg->aCellIdx;
78369 for(i=0; i<pPg->nOverflow; i++){
78370 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
78372 pCellptr = &pPg->aCellIdx[iCell * 2];
78374 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
78387 pCellptr = &pPg->aCellIdx[nCell*2];
78390 iNew+nCell, nNew-nCell, pCArray
78393 pPg->nCell = nNew;
78394 pPg->nOverflow = 0;
78396 put2byte(&aData[hdr+3], pPg->nCell);
78397 put2byte(&aData[hdr+5], pData - aData);
78401 u8 *pCell = pCArray->apCell[i+iNew];
78402 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
78403 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
78404 pCell = &pTmp[pCell - aData];
78407 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
78423 ** a new entry is being inserted on the extreme right-end of the
78427 ** Instead of trying to balance the 3 right-most leaf pages, just add
78428 ** a new page to the right-hand side and put the one new entry in
78434 ** pPage is the leaf page which is the right-most page in the tree.
78436 ** which is also the right-most entry on the page.
78444 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
78445 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
78447 int rc; /* Return Code */
78450 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78451 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78452 assert( pPage->nOverflow==1 );
78454 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
78455 assert( pPage->nFree>=0 );
78456 assert( pParent->nFree>=0 );
78458 /* Allocate a new page. This page will become the right-sibling of
78467 u8 *pCell = pPage->apOvfl[0];
78468 u16 szCell = pPage->xCellSize(pPage, pCell);
78472 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
78473 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
78479 b.apEnd[0] = pPage->aDataEnd;
78486 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
78488 /* If this is an auto-vacuum database, update the pointer map
78498 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
78499 if( szCell>pNew->minLocal ){
78505 ** consists of a 4-byte page number (the page number of pPage) and
78509 ** To find the largest key value on pPage, first find the right-most
78511 ** record-length (a variable length integer at most 32-bits in size)
78513 ** The first of the while(...) loops below skips over the record-length
78517 pCell = findCell(pPage, pPage->nCell-1);
78525 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
78526 0, pPage->pgno);
78529 /* Set the right-child pointer of pParent to point to the new page. */
78530 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
78544 ** for setting pointer-map entries.
78546 static int ptrmapCheckPages(MemPage **apPage, int nPage){
78547 int i, j;
78552 BtShared *pBt = pPage->pBt;
78553 assert( pPage->isInit );
78555 for(j=0; j<pPage->nCell; j++){
78560 pPage->xParseCell(pPage, z, &info);
78562 Pgno ovfl = get4byte(&z[info.nSize-4]);
78564 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
78566 if( !pPage->leaf ){
78569 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78572 if( !pPage->leaf ){
78573 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
78575 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78583 ** This function is used to copy the contents of the b-tree node stored
78585 ** the pointer-map entries for each child page are updated so that the
78599 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
78601 BtShared * const pBt = pFrom->pBt;
78602 u8 * const aFrom = pFrom->aData;
78603 u8 * const aTo = pTo->aData;
78604 int const iFromHdr = pFrom->hdrOffset;
78605 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
78606 int rc;
78607 int iData;
78610 assert( pFrom->isInit );
78611 assert( pFrom->nFree>=iToHdr );
78612 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
78614 /* Copy the b-tree node content from page pFrom to page pTo. */
78616 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
78617 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
78624 pTo->isInit = 0;
78632 /* If this is an auto-vacuum database, update the pointer-map entries
78633 ** for any b-tree or overflow pages that pTo now contains the pointers to.
78675 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
78681 static int balance_nonroot(
78683 int iParentIdx, /* Index of "the page" in pParent */
78684 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
78685 int isRoot, /* True if pParent is a root-page */
78686 int bBulk /* True if this call is part of a bulk load */
78689 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
78690 int nNew = 0; /* Number of pages in apNew[] */
78691 int nOld; /* Number of pages in apOld[] */
78692 int i, j, k; /* Loop counters */
78693 int nxDiv; /* Next divider slot in pParent->aCell[] */
78694 int rc = SQLITE_OK; /* The return code */
78696 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
78697 int usableSpace; /* Bytes in pPage beyond the header */
78698 int pageFlags; /* Value of pPage->aData[0] */
78699 int iSpace1 = 0; /* First unused byte of aSpace1[] */
78700 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
78701 int szScratch; /* Size of scratch memory requested */
78704 u8 *pRight; /* Location in parent of right-sibling pointer */
78705 u8 *apDiv[NB-1]; /* Divider cells in pParent */
78706 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
78707 int cntOld[NB+2]; /* Old index in b.apCell[] */
78708 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
78717 pBt = pParent->pBt;
78718 assert( sqlite3_mutex_held(pBt->mutex) );
78719 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78726 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
78727 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
78732 assert( pParent->nFree>=0 );
78745 i = pParent->nOverflow + pParent->nCell;
78753 nxDiv = i-2+bBulk;
78755 nxDiv = iParentIdx-1;
78757 i = 2-bBulk;
78760 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
78761 pRight = &pParent->aData[pParent->hdrOffset+8];
78763 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
78774 if( apOld[i]->nFree<0 ){
78781 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
78782 if( (i--)==0 ) break;
78784 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
78785 apDiv[i] = pParent->apOvfl[0];
78787 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78788 pParent->nOverflow = 0;
78790 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
78792 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78801 ** But not if we are in secure-delete mode. In secure-delete mode,
78806 if( pBt->btsFlags & BTS_FAST_SECURE ){
78807 int iOff;
78811 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
78812 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
78814 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
78817 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
78821 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
78831 + pBt->pageSize; /* aSpace1 */
78833 assert( szScratch<=7*(int)pBt->pageSize );
78860 leafCorrection = b.pRef->leaf*4;
78861 leafData = b.pRef->intKeyLeaf;
78864 int limit = pOld->nCell;
78865 u8 *aData = pOld->aData;
78866 u16 maskPage = pOld->maskPage;
78867 u8 *piCell = aData + pOld->cellOffset;
78869 VVA_ONLY( int nCellAtStart = b.nCell; )
78871 /* Verify that all sibling pages are of the same "type" (table-leaf,
78872 ** table-interior, index-leaf, or index-interior).
78874 if( pOld->aData[0]!=apOld[0]->aData[0] ){
78896 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
78897 if( pOld->nOverflow>0 ){
78898 if( NEVER(limit<pOld->aiOvfl[0]) ){
78902 limit = pOld->aiOvfl[0];
78908 for(k=0; k<pOld->nOverflow; k++){
78909 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
78910 b.apCell[b.nCell] = pOld->apOvfl[k];
78914 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
78921 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
78924 if( i<nOld-1 && !leafData){
78931 assert( sz<=pBt->maxLocal+23 );
78932 assert( iSpace1 <= (int)pBt->pageSize );
78936 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
78937 if( !pOld->leaf ){
78939 assert( pOld->hdrOffset==0 || CORRUPT_DB );
78942 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
78949 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
78961 ** size of all cells on the i-th page and cntNew[] which is the index
78968 ** szNew[i]: Spaced used on the i-th sibling page.
78970 ** the right of the i-th sibling page.
78974 usableSpace = pBt->usableSize - 12 + leafCorrection;
78977 b.apEnd[k] = p->aDataEnd;
78979 if( k && b.ixNx[k]==b.ixNx[k-1] ){
78980 k--; /* Omit b.ixNx[] entry for child pages with no cells */
78984 b.apEnd[k] = pParent->aDataEnd;
78987 assert( p->nFree>=0 );
78988 szNew[i] = usableSpace - p->nFree;
78989 for(j=0; j<p->nOverflow; j++){
78990 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
78996 int sz;
79001 szNew[k-1] = 0;
79002 cntNew[k-1] = b.nCell;
79004 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
79005 szNew[i] -= sz;
79014 cntNew[i]--;
79028 szNew[i+1] -= sz;
79032 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
79041 ** always nearly full, while the right-most sibling might be nearly empty.
79046 ** be so out of balance as to be illegal. For example, the right-most
79049 for(i=k-1; i>0; i--){
79050 int szRight = szNew[i]; /* Size of sibling on the right */
79051 int szLeft = szNew[i-1]; /* Size of sibling on the left */
79052 int r; /* Index of right-most cell in left sibling */
79053 int d; /* Index of first cell to the left of right sibling */
79055 r = cntNew[i-1] - 1;
79056 d = r + 1 - leafData;
79059 int szR, szD;
79065 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
79069 szLeft -= szR + 2;
79070 cntNew[i-1] = r;
79071 r--;
79072 d--;
79075 szNew[i-1] = szLeft;
79076 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
79082 /* Sanity check: For a non-corrupt database file one of the following
79089 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
79091 apOld[0]->pgno, apOld[0]->nCell,
79092 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
79093 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
79099 pageFlags = apOld[0]->aData[0];
79105 rc = sqlite3PagerWrite(pNew->pDbPage);
79107 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
79122 /* Set the pointer-map entry for the new sibling page. */
79124 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
79145 aPgno[i] = apNew[i]->pgno;
79146 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
79147 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
79149 for(i=0; i<nNew-1; i++){
79150 int iB = i;
79152 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
79161 Pgno pgnoA = apNew[i]->pgno;
79162 Pgno pgnoB = apNew[iB]->pgno;
79163 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
79164 u16 fgA = apNew[i]->pDbPage->flags;
79165 u16 fgB = apNew[iB]->pDbPage->flags;
79166 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
79167 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
79168 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
79169 apNew[i]->pgno = pgnoB;
79170 apNew[iB]->pgno = pgnoA;
79176 apNew[0]->pgno, szNew[0], cntNew[0],
79177 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
79178 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
79179 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
79180 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
79181 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
79182 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
79183 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
79184 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
79187 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79189 assert( apNew[nNew-1]!=0 );
79190 put4byte(pRight, apNew[nNew-1]->pgno);
79192 /* If the sibling pages are not leaves, ensure that the right-child pointer
79193 ** of the right-most new sibling page is set to the value that was
79194 ** originally in the same field of the right-most old sibling page. */
79196 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
79197 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
79212 ** associated with the right-child of each sibling may also need to be
79219 int cntOldNext = pNew->nCell + pNew->nOverflow;
79220 int iNew = 0;
79221 int iOld = 0;
79230 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
79244 || pNew->pgno!=aPgno[iOld]
79245 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
79248 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
79250 if( cachedCellSize(&b,i)>pNew->minLocal ){
79259 for(i=0; i<nNew-1; i++){
79262 int sz;
79272 if( !pNew->leaf ){
79273 memcpy(&pNew->aData[8], pCell, 4);
79275 /* If the tree is a leaf-data tree, and the siblings are leaves,
79277 ** cell consists of the integer key for the right-most cell of
79278 ** the sibling-page assembled above only.
79281 j--;
79282 pNew->xParseCell(pNew, b.apCell[j], &info);
79287 pCell -= 4;
79288 /* Obscure case for non-leaf-data trees: If the cell at pCell was
79295 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
79301 sz = pParent->xCellSize(pParent, pCell);
79305 assert( sz<=pBt->maxLocal+23 );
79306 assert( iOvflSpace <= (int)pBt->pageSize );
79313 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
79315 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79322 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
79324 ** the left-hand sibling apNew[iPg-1] has been updated.
79328 ** the right-hand sibling apNew[iPg+1] has been updated.
79332 ** The iPg value in the following loop starts at nNew-1 goes down
79333 ** to 0, then back up to nNew-1 again, thus making two passes over
79340 for(i=1-nNew; i<nNew; i++){
79341 int iPg = i<0 ? -i : i;
79347 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
79349 int iNew;
79350 int iOld;
79351 int nNewCell;
79354 ** only after iPg-1 has already been updated. */
79355 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
79365 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
79366 iNew = cntNew[iPg-1] + !leafData;
79367 nNewCell = cntNew[iPg] - iNew;
79373 apNew[iPg]->nFree = usableSpace-szNew[iPg];
79374 assert( apNew[iPg]->nOverflow==0 );
79375 assert( apNew[iPg]->nCell==nNewCell );
79385 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
79386 /* The root page of the b-tree now contains no cells. The only sibling
79387 ** page is the right-child of the parent. Copy the contents of the
79389 ** b-tree structure by one. This is described as the "balance-shallower"
79390 ** sub-algorithm in some documentation.
79392 ** If this is an auto-vacuum database, the call to copyNodeContent()
79393 ** sets all pointer-map entries corresponding to database image pages
79402 rc = defragmentPage(apNew[0], -1);
79404 assert( apNew[0]->nFree ==
79405 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
79406 - apNew[0]->nCell*2)
79412 /* Fix the pointer map entries associated with the right-child of each
79416 u32 key = get4byte(&apNew[i]->aData[8]);
79417 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
79421 assert( pParent->isInit );
79432 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
79459 ** This function is called when the root page of a b-tree structure is
79464 ** page is then overwritten to make it an empty page with the right-child
79467 ** Before returning, all pointer-map entries corresponding to pages
79468 ** that the new child-page now contains pointers to are updated. The
79469 ** entry corresponding to the new right-child pointer of the root
79477 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
79478 int rc; /* Return value from subprocedures */
79481 BtShared *pBt = pRoot->pBt; /* The BTree */
79483 assert( pRoot->nOverflow>0 );
79484 assert( sqlite3_mutex_held(pBt->mutex) );
79486 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
79487 ** page that will become the new right-child of pPage. Copy the contents
79490 rc = sqlite3PagerWrite(pRoot->pDbPage);
79492 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
79495 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
79503 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
79504 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
79505 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
79507 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
79510 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
79511 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
79512 memcpy(pChild->apOvfl, pRoot->apOvfl,
79513 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
79514 pChild->nOverflow = pRoot->nOverflow;
79516 /* Zero the contents of pRoot. Then install pChild as the right-child. */
79517 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
79518 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
79526 ** on the same B-tree as pCur.
79529 ** pointing to the same b-tree. If an insert occurs on one SQL table
79531 ** table linked to the same b-tree. If the secondary insert causes a
79535 static int anotherValidCursor(BtCursor *pCur){
79537 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
79539 && pOther->eState==CURSOR_VALID
79540 && pOther->pPage==pCur->pPage
79542 return SQLITE_CORRUPT_PAGE(pCur->pPage);
79558 static int balance(BtCursor *pCur){
79559 int rc = SQLITE_OK;
79563 VVA_ONLY( int balance_quick_called = 0 );
79564 VVA_ONLY( int balance_deeper_called = 0 );
79567 int iPage;
79568 MemPage *pPage = pCur->pPage;
79570 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
79571 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79577 }else if( (iPage = pCur->iPage)==0 ){
79578 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
79579 /* The root page of the b-tree is overfull. In this case call the
79580 ** balance_deeper() function to create a new child for the root-page
79581 ** and copy the current contents of the root-page to it. The
79582 ** next iteration of the do-loop will balance the child page.
79586 rc = balance_deeper(pPage, &pCur->apPage[1]);
79588 pCur->iPage = 1;
79589 pCur->ix = 0;
79590 pCur->aiIdx[0] = 0;
79591 pCur->apPage[0] = pPage;
79592 pCur->pPage = pCur->apPage[1];
79593 assert( pCur->pPage->nOverflow );
79598 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
79604 MemPage * const pParent = pCur->apPage[iPage-1];
79605 int const iIdx = pCur->aiIdx[iPage-1];
79607 rc = sqlite3PagerWrite(pParent->pDbPage);
79608 if( rc==SQLITE_OK && pParent->nFree<0 ){
79613 if( pPage->intKeyLeaf
79614 && pPage->nOverflow==1
79615 && pPage->aiOvfl[0]==pPage->nCell
79616 && pParent->pgno!=1
79617 && pParent->nCell==iIdx
79622 ** happens, the next iteration of the do-loop will balance pParent
79641 ** become overfull or underfull. The next iteration of the do-loop
79646 ** A subsequent iteration of the do-loop will deal with this by
79648 ** but it doesn't deal with overflow cells - just moves them to a
79655 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
79657 pCur->hints&BTREE_BULKLOAD);
79673 pPage->nOverflow = 0;
79675 /* The next iteration of the do-loop balances the parent page. */
79677 pCur->iPage--;
79678 assert( pCur->iPage>=0 );
79679 pCur->pPage = pCur->apPage[pCur->iPage];
79692 static int btreeOverwriteContent(
79696 int iOffset, /* Offset of first byte to write */
79697 int iAmt /* Number of bytes to be written */
79699 int nData = pX->nData - iOffset;
79702 int i;
79705 int rc = sqlite3PagerWrite(pPage->pDbPage);
79707 memset(pDest + i, 0, iAmt - i);
79713 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
79714 iAmt-nData);
79718 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
79719 int rc = sqlite3PagerWrite(pPage->pDbPage);
79725 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
79736 static SQLITE_NOINLINE int btreeOverwriteOverflowCell(
79740 int iOffset; /* Next byte of pX->pData to write */
79741 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79742 int rc; /* Return code */
79743 MemPage *pPage = pCur->pPage; /* Page being written */
79748 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
79751 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79752 0, pCur->info.nLocal);
79756 iOffset = pCur->info.nLocal;
79759 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
79760 pBt = pPage->pBt;
79761 ovflPageSize = pBt->usableSize - 4;
79765 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79769 ovflPgno = get4byte(pPage->aData);
79771 ovflPageSize = nTotal - iOffset;
79773 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
79776 sqlite3PagerUnref(pPage->pDbPage);
79787 static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
79788 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79789 MemPage *pPage = pCur->pPage; /* Page being written */
79791 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79792 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79796 if( pCur->info.nLocal==nTotal ){
79798 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79799 0, pCur->info.nLocal);
79822 ** If the seekResult parameter is non-zero, then a successful call to
79833 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
79834 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
79837 SQLITE_PRIVATE int sqlite3BtreeInsert(
79840 int flags, /* True if this is likely an append */
79841 int seekResult /* Result of prior IndexMoveto() call */
79843 int rc;
79844 int loc = seekResult; /* -1: before desired location +1: after */
79845 int szNew = 0;
79846 int idx;
79848 Btree *p = pCur->pBtree;
79853 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
79857 ** In some cases, the call to btreeMoveto() below is a no-op. For
79858 ** example, when inserting data into a table with auto-generated integer
79861 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
79866 if( pCur->curFlags & BTCF_Multiple ){
79867 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
79869 if( loc && pCur->iPage<0 ){
79875 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
79882 if( pCur->eState>=CURSOR_REQUIRESEEK ){
79883 testcase( pCur->eState==CURSOR_REQUIRESEEK );
79884 testcase( pCur->eState==CURSOR_FAULT );
79890 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
79891 && p->pBt->inTransaction==TRANS_WRITE
79892 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
79893 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
79896 ** expecting an index b-tree, then the caller should be inserting blob
79900 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
79902 if( pCur->pKeyInfo==0 ){
79903 assert( pX->pKey==0 );
79904 /* If this is an insert into a table b-tree, invalidate any incrblob
79906 if( p->hasIncrblobCur ){
79907 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
79915 assert( pCur->curFlags & BTCF_ValidNKey );
79916 assert( pX->nKey==pCur->info.nKey );
79925 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
79928 assert( pX->nData>=0 && pX->nZero>=0 );
79929 if( pCur->info.nSize!=0
79930 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
79941 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
79959 if( pX->nMem ){
79961 r.pKeyInfo = pCur->pKeyInfo;
79962 r.aMem = pX->aMem;
79963 r.nField = pX->nMem;
79968 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
79980 if( pCur->info.nKey==pX->nKey ){
79982 x2.pData = pX->pKey;
79983 x2.nData = pX->nKey;
79989 assert( pCur->eState==CURSOR_VALID
79990 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
79992 pPage = pCur->pPage;
79993 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
79994 assert( pPage->leaf || !pPage->intKey );
79995 if( pPage->nFree<0 ){
79996 if( NEVER(pCur->eState>CURSOR_INVALID) ){
79997 /* ^^^^^--- due to the moveToRoot() call above */
80006 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
80008 assert( pPage->isInit || CORRUPT_DB );
80009 newCell = p->pBt->pTmpSpace;
80014 szNew = p->pBt->nPreformatSize;
80019 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
80021 pPage->xParseCell(pPage, newCell, &info);
80023 Pgno ovfl = get4byte(&newCell[szNew-4]);
80024 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
80032 assert( szNew==pPage->xCellSize(pPage, newCell) );
80033 assert( szNew <= MX_CELL_SIZE(p->pBt) );
80034 idx = pCur->ix;
80035 pCur->info.nSize = 0;
80039 if( idx>=pPage->nCell ){
80042 rc = sqlite3PagerWrite(pPage->pDbPage);
80047 if( !pPage->leaf ){
80051 testcase( pCur->curFlags & BTCF_ValidOvfl );
80054 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
80064 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
80066 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
80069 if( oldCell+szNew > pPage->aDataEnd ){
80077 }else if( loc<0 && pPage->nCell>0 ){
80078 assert( pPage->leaf );
80079 idx = ++pCur->ix;
80080 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
80082 assert( pPage->leaf );
80085 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
80086 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
80100 ** multiple records into an intkey b-tree using a single cursor (as can
80103 ** the b-tree if possible. If the cursor is left pointing to the last
80108 if( pPage->nOverflow ){
80110 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
80117 pCur->pPage->nOverflow = 0;
80118 pCur->eState = CURSOR_INVALID;
80121 if( pCur->pKeyInfo ){
80122 assert( pCur->pKey==0 );
80123 pCur->pKey = sqlite3Malloc( pX->nKey );
80124 if( pCur->pKey==0 ){
80127 memcpy(pCur->pKey, pX->pKey, pX->nKey);
80130 pCur->eState = CURSOR_REQUIRESEEK;
80131 pCur->nKey = pX->nKey;
80134 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
80155 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
80156 BtShared *pBt = pDest->pBt;
80157 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
80163 if( pSrc->info.nPayload<0x80 ){
80164 *(aOut++) = pSrc->info.nPayload;
80166 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
80168 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
80169 nIn = pSrc->info.nLocal;
80170 aIn = pSrc->info.pPayload;
80171 if( aIn+nIn>pSrc->pPage->aDataEnd ){
80172 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80174 nRem = pSrc->info.nPayload;
80175 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80177 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
80180 int rc = SQLITE_OK;
80181 Pager *pSrcPager = pSrc->pBt->pPager;
80188 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80189 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
80190 if( nOut<pSrc->info.nPayload ){
80192 pBt->nPreformatSize += 4;
80196 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
80197 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80199 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
80203 nRem -= nOut;
80207 int nCopy = MIN(nOut, nIn);
80209 nOut -= nCopy;
80210 nIn -= nCopy;
80222 nIn = pSrc->pBt->usableSize - 4;
80233 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
80238 pPgnoOut = pPageOut->aData;
80241 nOut = MIN(pBt->usableSize - 4, nRem);
80265 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
80269 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
80270 Btree *p = pCur->pBtree;
80271 BtShared *pBt = p->pBt;
80272 int rc; /* Return code */
80275 int iCellIdx; /* Index of cell to delete */
80276 int iCellDepth; /* Depth of node containing pCell */
80281 assert( pBt->inTransaction==TRANS_WRITE );
80282 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
80283 assert( pCur->curFlags & BTCF_WriteFlag );
80284 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
80285 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
80287 if( pCur->eState!=CURSOR_VALID ){
80288 if( pCur->eState>=CURSOR_REQUIRESEEK ){
80290 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
80291 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
80293 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
80296 assert( pCur->eState==CURSOR_VALID );
80298 iCellDepth = pCur->iPage;
80299 iCellIdx = pCur->ix;
80300 pPage = pCur->pPage;
80301 if( pPage->nCell<=iCellIdx ){
80305 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
80308 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
80314 ** will cause a b-tree rebalance, then this is done by saving the cursor
80330 if( !pPage->leaf
80331 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
80332 (int)(pBt->usableSize*2/3)
80333 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
80335 /* A b-tree rebalance will be required after deleting this entry.
80349 ** sub-tree headed by the child page of the cell being deleted. This makes
80351 if( !pPage->leaf ){
80359 if( pCur->curFlags & BTCF_Multiple ){
80360 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
80364 /* If this is a delete operation to remove a row from a table b-tree,
80366 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
80367 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
80373 rc = sqlite3PagerWrite(pPage->pDbPage);
80380 ** is currently pointing to the largest entry in the sub-tree headed
80381 ** by the child-page of the cell that was just deleted from an internal
80384 if( !pPage->leaf ){
80385 MemPage *pLeaf = pCur->pPage;
80386 int nCell;
80390 if( pLeaf->nFree<0 ){
80394 if( iCellDepth<pCur->iPage-1 ){
80395 n = pCur->apPage[iCellDepth+1]->pgno;
80397 n = pCur->pPage->pgno;
80399 pCell = findCell(pLeaf, pLeaf->nCell-1);
80400 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf);
80401 nCell = pLeaf->xCellSize(pLeaf, pCell);
80403 pTmp = pBt->pTmpSpace;
80405 rc = sqlite3PagerWrite(pLeaf->pDbPage);
80407 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
80409 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
80428 assert( pCur->pPage->nOverflow==0 );
80429 assert( pCur->pPage->nFree>=0 );
80430 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80432 ** then balance() will always be a no-op. No need to invoke it. */
80437 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
80438 releasePageNotNull(pCur->pPage);
80439 pCur->iPage--;
80440 while( pCur->iPage>iCellDepth ){
80441 releasePage(pCur->apPage[pCur->iPage--]);
80443 pCur->pPage = pCur->apPage[pCur->iPage];
80449 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
80450 assert( pPage==pCur->pPage || CORRUPT_DB );
80451 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
80452 pCur->eState = CURSOR_SKIPNEXT;
80453 if( iCellIdx>=pPage->nCell ){
80454 pCur->skipNext = -1;
80455 pCur->ix = pPage->nCell-1;
80457 pCur->skipNext = 1;
80463 pCur->eState = CURSOR_REQUIRESEEK;
80482 static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
80483 BtShared *pBt = p->pBt;
80486 int rc;
80487 int ptfFlags; /* Page-type flags for the root page of new table */
80490 assert( pBt->inTransaction==TRANS_WRITE );
80491 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
80499 if( pBt->autoVacuum ){
80500 Pgno pgnoMove; /* Move a page here to make room for the root-page */
80505 ** out to be an overflow page, delete all overflow page-map caches
80511 ** root page of the new table should go. meta[3] is the largest root-page
80512 ** created so far, so the new root-page is (meta[3]+1).
80520 /* The new root-page may not be allocated on a pointer-map page, or the
80539 /* pgnoRoot is the page that will be used for the root-page of
80583 rc = sqlite3PagerWrite(pRoot->pDbPage);
80592 /* Update the pointer-map and meta-data with the new root-page number. */
80603 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
80615 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
80622 sqlite3PagerUnref(pRoot->pDbPage);
80623 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
80627 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
80628 int rc;
80639 static int clearDatabasePage(
80642 int freePageFlag, /* Deallocate page if true */
80646 int rc;
80648 int i;
80649 int hdr;
80652 assert( sqlite3_mutex_held(pBt->mutex) );
80658 if( (pBt->openFlags & BTREE_SINGLE)==0
80659 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
80664 hdr = pPage->hdrOffset;
80665 for(i=0; i<pPage->nCell; i++){
80667 if( !pPage->leaf ){
80674 if( !pPage->leaf ){
80675 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
80677 if( pPage->intKey ) pnChange = 0;
80680 testcase( !pPage->intKey );
80681 *pnChange += pPage->nCell;
80685 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
80686 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
80706 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
80707 int rc;
80708 BtShared *pBt = p->pBt;
80710 assert( p->inTrans==TRANS_WRITE );
80716 ** is the root of a table b-tree - if it is not, the following call is
80717 ** a no-op). */
80718 if( p->hasIncrblobCur ){
80732 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
80733 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
80756 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
80757 int rc;
80759 BtShared *pBt = p->pBt;
80762 assert( p->inTrans==TRANS_WRITE );
80782 if( pBt->autoVacuum ){
80787 /* If the table being dropped is the table with the largest root-page
80796 /* The table being dropped does not have the largest root-page
80798 ** gap left by the deleted root-page.
80821 /* Set the new 'max-root-page' value in the database header. This
80823 ** be a root-page number, less one again if that is the
80826 maxRootPgno--;
80829 maxRootPgno--;
80841 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
80842 int rc;
80851 ** This function may only be called if the b-tree connection already
80854 ** Read the meta-information out of a database file. Meta[0]
80857 ** is read-only, the others are read/write.
80870 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
80871 BtShared *pBt = p->pBt;
80874 assert( p->inTrans>TRANS_NONE );
80876 assert( pBt->pPage1 );
80880 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
80882 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
80885 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
80886 ** database, mark the database as read-only. */
80889 pBt->btsFlags |= BTS_READ_ONLY;
80897 ** Write meta-information back into the database. Meta[0] is
80898 ** read-only and may not be written.
80900 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
80901 BtShared *pBt = p->pBt;
80903 int rc;
80906 assert( p->inTrans==TRANS_WRITE );
80907 assert( pBt->pPage1!=0 );
80908 pP1 = pBt->pPage1->aData;
80909 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
80914 assert( pBt->autoVacuum || iMeta==0 );
80916 pBt->incrVacuum = (u8)iMeta;
80925 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
80926 ** number of entries in the b-tree and write the result to *pnEntry.
80932 SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
80934 int rc; /* Return code */
80943 ** page in the B-Tree structure (not including overflow pages).
80945 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
80946 int iIdx; /* Index of child node in parent */
80947 MemPage *pPage; /* Current page of the b-tree */
80949 /* If this is a leaf page or the tree is not an int-key tree, then
80953 pPage = pCur->pPage;
80954 if( pPage->leaf || !pPage->intKey ){
80955 nEntry += pPage->nCell;
80961 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
80963 ** to visit is the right-child of its parent.
80968 if( pPage->leaf ){
80970 if( pCur->iPage==0 ){
80971 /* All pages of the b-tree have been visited. Return successfully. */
80976 }while ( pCur->ix>=pCur->pPage->nCell );
80978 pCur->ix++;
80979 pPage = pCur->pPage;
80983 ** points at. This is the right-child if (iIdx==pPage->nCell).
80985 iIdx = pCur->ix;
80986 if( iIdx==pPage->nCell ){
80987 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
81002 return p->pBt->pPager;
81010 pCheck->rc = SQLITE_NOMEM;
81011 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
81012 if( pCheck->nErr==0 ) pCheck->nErr++;
81020 sqlite3 *db = pCheck->db;
81021 if( AtomicLoad(&db->u1.isInterrupted) ){
81022 pCheck->rc = SQLITE_INTERRUPT;
81023 pCheck->nErr++;
81024 pCheck->mxErr = 0;
81027 if( db->xProgress ){
81028 assert( db->nProgressOps>0 );
81029 pCheck->nStep++;
81030 if( (pCheck->nStep % db->nProgressOps)==0
81031 && db->xProgress(db->pProgressArg)
81033 pCheck->rc = SQLITE_INTERRUPT;
81034 pCheck->nErr++;
81035 pCheck->mxErr = 0;
81051 if( !pCheck->mxErr ) return;
81052 pCheck->mxErr--;
81053 pCheck->nErr++;
81055 if( pCheck->errMsg.nChar ){
81056 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
81058 if( pCheck->zPfx ){
81059 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
81060 pCheck->v0, pCheck->v1, pCheck->v2);
81062 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
81064 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
81073 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
81076 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
81077 assert( pCheck->aPgRef!=0 );
81078 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
81079 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
81086 assert( pCheck->aPgRef!=0 );
81087 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
81088 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
81094 ** reference to the page, add an error message to pCheck->zErrMsg.
81100 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
81101 if( iPage>pCheck->nCkPage || iPage==0 ){
81115 ** Check that the entry in the pointer-map for page iChild maps to
81125 int rc;
81129 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
81150 int isFreeList, /* True for a freelist. False for overflow page list */
81154 int i;
81156 int nErrAtStart = pCheck->nErr;
81157 while( iPage!=0 && pCheck->mxErr ){
81161 N--;
81162 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
81170 if( pCheck->pBt->autoVacuum ){
81174 if( n>pCheck->pBt->usableSize/4-2 ){
81177 N--;
81179 for(i=0; i<(int)n; i++){
81182 if( pCheck->pBt->autoVacuum ){
81188 N -= n;
81193 /* If this database supports auto-vacuum and iPage is not the last
81194 ** page in this overflow list, check that the pointer-map entry for
81197 if( pCheck->pBt->autoVacuum && N>0 ){
81206 if( N && nErrAtStart==pCheck->nErr ){
81210 expected-N, expected);
81216 ** An implementation of a min-heap.
81226 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
81249 static int btreeHeapPull(u32 *aHeap, u32 *pOut){
81255 aHeap[0]--;
81283 static int checkTreePage(
81290 int i; /* Loop counter */
81291 int rc; /* Result code from subroutine call */
81292 int depth = -1, d2; /* Depth of a subtree */
81293 int pgno; /* Page number */
81294 int nFrag; /* Number of fragmented bytes on the page */
81295 int hdr; /* Offset to the page header */
81296 int cellStart; /* Offset to the start of the cell pointer array */
81297 int nCell; /* Number of cells */
81298 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
81299 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
81308 u32 *heap = 0; /* Min-heap used for checking cell coverage */
81309 u32 x, prev = 0; /* Next and previous entry on the min-heap */
81310 const char *saved_zPfx = pCheck->zPfx;
81311 int saved_v1 = pCheck->v1;
81312 int saved_v2 = pCheck->v2;
81318 if( pCheck->mxErr==0 ) goto end_of_check;
81319 pBt = pCheck->pBt;
81320 usableSize = pBt->usableSize;
81323 pCheck->zPfx = "Tree %u page %u: ";
81324 pCheck->v1 = iPage;
81328 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
81334 savedIsInit = pPage->isInit;
81335 pPage->isInit = 0;
81347 data = pPage->aData;
81348 hdr = pPage->hdrOffset;
81351 pCheck->zPfx = "Tree %u page %u cell %u: ";
81355 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
81358 assert( pPage->nCell==nCell );
81359 if( pPage->leaf || pPage->intKey==0 ){
81360 pCheck->nRow += nCell;
81363 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
81364 ** immediately follows the b-tree page header. */
81365 cellStart = hdr + 12 - 4*pPage->leaf;
81366 assert( pPage->aCellIdx==&data[cellStart] );
81367 pCellIdx = &data[cellStart + 2*(nCell-1)];
81369 if( !pPage->leaf ){
81370 /* Analyze the right-child page of internal pages */
81373 if( pBt->autoVacuum ){
81374 pCheck->zPfx = "Tree %u page %u right child: ";
81383 heap = pCheck->heap;
81387 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
81389 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
81393 pCheck->v2 = i;
81396 pCellIdx -= 2;
81397 if( pc<contentOffset || pc>usableSize-4 ){
81399 pc, contentOffset, usableSize-4);
81404 pPage->xParseCell(pPage, pCell, &info);
81412 if( pPage->intKey ){
81424 assert( pc + info.nSize - 4 <= usableSize );
81425 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
81426 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
81428 if( pBt->autoVacuum ){
81435 if( !pPage->leaf ){
81439 if( pBt->autoVacuum ){
81450 /* Populate the coverage-checking heap for leaf pages */
81451 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
81458 pCheck->zPfx = 0;
81459 if( doCoverageCheck && pCheck->mxErr>0 ){
81460 /* For leaf pages, the min-heap has already been initialized and the
81463 if( !pPage->leaf ){
81464 heap = pCheck->heap;
81466 for(i=nCell-1; i>=0; i--){
81469 size = pPage->xCellSize(pPage, &data[pc]);
81470 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
81474 /* Add the freeblocks to the min-heap
81476 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
81482 int size, j;
81483 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
81486 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
81487 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
81488 ** big-endian integer which is the offset in the b-tree page of the next
81492 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
81495 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
81498 /* Analyze the min-heap looking for overlap between cells and/or
81501 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
81506 ** The loop below pulls entries from the min-heap in order and compares
81512 prev = contentOffset - 1; /* Implied first min-heap entry */
81519 nFrag += (x>>16) - (prev&0xffff) - 1;
81523 nFrag += usableSize - (prev&0xffff) - 1;
81524 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
81525 ** is stored in the fifth field of the b-tree page header.
81526 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
81537 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
81539 pCheck->zPfx = saved_zPfx;
81540 pCheck->v1 = saved_v1;
81541 pCheck->v2 = saved_v2;
81552 ** A read-only or read-write transaction must be opened before calling
81557 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
81561 ** root pages is incomplete. This is a "partial integrity-check". This
81569 SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
81574 int nRoot, /* Number of entries in aRoot[] */
81575 int mxErr, /* Stop reporting errors after this many */
81576 int *pnErr, /* OUT: Write number of errors seen to this variable */
81581 BtShared *pBt = p->pBt;
81582 u64 savedDbFlags = pBt->db->flags;
81584 int bPartial = 0; /* True if not checking all btrees */
81585 int bCkFreelist = 1; /* True to scan the freelist */
81586 VVA_ONLY( int nRef );
81599 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
81600 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
81605 sCheck.pPager = pBt->pPager;
81619 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
81632 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
81633 get4byte(&pBt->pPage1->aData[36]));
81641 if( pBt->autoVacuum ){
81644 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
81645 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
81652 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
81659 testcase( pBt->db->flags & SQLITE_CellSizeCk );
81660 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
81661 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
81666 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
81675 pBt->db->flags = savedDbFlags;
81686 /* If the database supports auto-vacuum, make sure no tables contain
81687 ** references to pointer-map pages.
81690 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
81694 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
81714 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
81722 ** an empty string if the database is in-memory or a TEMP database.
81728 assert( p->pBt->pPager!=0 );
81729 return sqlite3PagerFilename(p->pBt->pPager, 1);
81741 assert( p->pBt->pPager!=0 );
81742 return sqlite3PagerJournalname(p->pBt->pPager);
81749 SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree *p){
81750 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
81751 return p ? p->inTrans : 0;
81759 ** transaction on the shared-cache the argument Btree is connected to.
81763 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
81764 int rc = SQLITE_OK;
81766 BtShared *pBt = p->pBt;
81768 if( pBt->inTransaction!=TRANS_NONE ){
81771 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
81782 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
81784 assert( sqlite3_mutex_held(p->db->mutex) );
81785 return p->nBackup!=0;
81790 ** a single shared-btree. The memory is used by client code for its own
81791 ** purposes (for example, to store a high-level schema associated with
81792 ** the shared-btree). The btree layer manages reference counting issues.
81794 ** The first time this is called on a shared-btree, nBytes bytes of memory
81803 ** Just before the shared-btree is closed, the function passed as the
81808 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
81809 BtShared *pBt = p->pBt;
81811 if( !pBt->pSchema && nBytes ){
81812 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
81813 pBt->xFreeSchema = xFree;
81816 return pBt->pSchema;
81824 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
81825 int rc;
81826 assert( sqlite3_mutex_held(p->db->mutex) );
81841 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
81842 int rc = SQLITE_OK;
81843 assert( p->inTrans!=TRANS_NONE );
81844 if( p->sharable ){
81871 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
81872 int rc;
81874 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
81875 assert( pCsr->curFlags & BTCF_Incrblob );
81881 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
81882 if( pCsr->eState!=CURSOR_VALID ){
81888 ** version of the b-tree page modified by the accessPayload call below.
81894 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
81900 ** (c) the connection holds a write-lock on the table (if required),
81901 ** (d) there are no conflicting read-locks, and
81904 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
81907 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
81908 && pCsr->pBt->inTransaction==TRANS_WRITE );
81909 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
81910 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
81911 assert( pCsr->pPage->intKey );
81920 pCur->curFlags |= BTCF_Incrblob;
81921 pCur->pBtree->hasIncrblobCur = 1;
81930 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
81931 BtShared *pBt = pBtree->pBt;
81932 int rc; /* Return code */
81939 pBt->btsFlags &= ~BTS_NO_WAL;
81940 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
81944 u8 *aData = pBt->pPage1->aData;
81948 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81957 pBt->btsFlags &= ~BTS_NO_WAL;
81965 SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
81966 return (pCsr->hints & mask)!=0;
81970 ** Return true if the given Btree is read-only.
81972 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
81973 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
81979 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
81982 ** If no transaction is active and the database is not a temp-db, clear
81983 ** the in-memory pager cache.
81986 BtShared *pBt = p->pBt;
81987 if( pBt->inTransaction==TRANS_NONE ){
81988 sqlite3PagerClearCache(pBt->pPager);
81996 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
81997 return p->sharable;
82005 SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
82006 testcase( p->sharable );
82007 return p->pBt->nRef;
82035 Btree *pDest; /* Destination b-tree file */
82037 int bDestLocked; /* True once a write-transaction is open on pDest */
82041 Btree *pSrc; /* Source b-tree file */
82043 int rc; /* Backup process error code */
82051 int isAttached; /* True once backup has been registered with pager */
82059 ** structure may be accessed via two groups of thread-safe entry points:
82073 ** backup_pagecount() are not thread-safe functions. If they are called
82081 ** Non-sharable Btrees (in-memory databases for example), do not have
82095 int i = sqlite3FindDbName(pDb, zDb);
82099 int rc = 0;
82117 return pDb->aDb[i].pBt;
82124 static int setDestPgsz(sqlite3_backup *p){
82125 int rc;
82126 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
82131 ** Check that there is no open read-transaction on the b-tree passed as the
82133 ** is an open read-transaction, return SQLITE_ERROR and leave an error
82136 static int checkReadTransaction(sqlite3 *db, Btree *p){
82175 sqlite3_mutex_enter(pSrcDb->mutex);
82176 sqlite3_mutex_enter(pDestDb->mutex);
82185 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82196 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
82197 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
82198 p->pDestDb = pDestDb;
82199 p->pSrcDb = pSrcDb;
82200 p->iNext = 1;
82201 p->isAttached = 0;
82203 if( 0==p->pSrc || 0==p->pDest
82204 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
82216 p->pSrc->nBackup++;
82219 sqlite3_mutex_leave(pDestDb->mutex);
82220 sqlite3_mutex_leave(pSrcDb->mutex);
82229 static int isFatalError(int rc){
82238 static int backupOnePage(
82242 int bUpdate /* True for an update, false otherwise */
82244 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
82245 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
82246 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
82247 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
82249 int rc = SQLITE_OK;
82252 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
82253 assert( p->bDestLocked );
82254 assert( !isFatalError(p->rc) );
82255 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
82263 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
82266 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
82284 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
82296 ** this function is a no-op.
82301 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
82303 int rc = sqlite3OsFileSize(pFile, &iCurrent);
82316 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
82317 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82318 p->pNext = *pp;
82320 p->isAttached = 1;
82324 ** Copy nPage pages from the source b-tree to the destination.
82326 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
82327 int rc;
82328 int destMode; /* Destination journal mode */
82329 int pgszSrc = 0; /* Source page size */
82330 int pgszDest = 0; /* Destination page size */
82335 sqlite3_mutex_enter(p->pSrcDb->mutex);
82336 sqlite3BtreeEnter(p->pSrc);
82337 if( p->pDestDb ){
82338 sqlite3_mutex_enter(p->pDestDb->mutex);
82341 rc = p->rc;
82343 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
82344 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
82345 int ii; /* Iterator variable */
82346 int nSrcPage = -1; /* Size of source db in pages */
82347 int bCloseTrans = 0; /* True if src db requires unlocking */
82349 /* If the source pager is currently in a write-transaction, return
82352 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
82358 /* If there is no open read-transaction on the source database, open
82362 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
82363 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
82373 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
82378 if( SQLITE_OK==rc && p->bDestLocked==0
82379 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
82380 (int*)&p->iDestSchema))
82382 p->bDestLocked = 1;
82387 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
82388 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
82389 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
82397 /* Now that there is a read-lock on the source database, query the
82400 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
82402 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
82403 const Pgno iSrcPg = p->iNext; /* Source page number */
82404 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
82412 p->iNext++;
82415 p->nPagecount = nSrcPage;
82416 p->nRemaining = nSrcPage+1-p->iNext;
82417 if( p->iNext>(Pgno)nSrcPage ){
82419 }else if( !p->isAttached ){
82425 ** is to make sure that the schema-version really does change in
82431 rc = sqlite3BtreeNewDb(p->pDest);
82435 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
82438 if( p->pDestDb ){
82439 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
82442 rc = sqlite3BtreeSetVersion(p->pDest, 2);
82446 int nDestTruncate;
82459 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
82460 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
82462 int ratio = pgszDest/pgszSrc;
82463 nDestTruncate = (nSrcPage+ratio-1)/ratio;
82464 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
82465 nDestTruncate--;
82473 /* If the source page-size is smaller than the destination page-size,
82479 ** pending-byte page in the source database may need to be
82485 int nDstPage;
82492 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
82504 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
82548 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
82558 ** "committing" a read-only transaction cannot fail.
82561 TESTONLY( int rc2 );
82562 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
82563 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
82570 p->rc = rc;
82572 if( p->pDestDb ){
82573 sqlite3_mutex_leave(p->pDestDb->mutex);
82575 sqlite3BtreeLeave(p->pSrc);
82576 sqlite3_mutex_leave(p->pSrcDb->mutex);
82583 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
82586 int rc; /* Value to return */
82590 pSrcDb = p->pSrcDb;
82591 sqlite3_mutex_enter(pSrcDb->mutex);
82592 sqlite3BtreeEnter(p->pSrc);
82593 if( p->pDestDb ){
82594 sqlite3_mutex_enter(p->pDestDb->mutex);
82598 if( p->pDestDb ){
82599 p->pSrc->nBackup--;
82601 if( p->isAttached ){
82602 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82605 pp = &(*pp)->pNext;
82608 *pp = p->pNext;
82612 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
82615 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
82616 if( p->pDestDb ){
82617 sqlite3Error(p->pDestDb, rc);
82620 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
82622 sqlite3BtreeLeave(p->pSrc);
82623 if( p->pDestDb ){
82624 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82637 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
82644 return p->nRemaining;
82651 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
82658 return p->nPagecount;
82680 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
82681 if( !isFatalError(p->rc) && iPage<p->iNext ){
82686 int rc;
82687 assert( p->pDestDb );
82688 sqlite3_mutex_enter(p->pDestDb->mutex);
82690 sqlite3_mutex_leave(p->pDestDb->mutex);
82693 p->rc = rc;
82696 }while( (p = p->pNext)!=0 );
82715 for(p=pBackup; p; p=p->pNext){
82716 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
82717 p->iNext = 1;
82730 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
82731 int rc;
82739 if( pFd->pMethods ){
82752 b.pSrcDb = pFrom->db;
82761 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
82768 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
82806 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
82815 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
82819 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
82825 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
82828 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
82830 if( p->flags & MEM_Null ){
82832 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
82839 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
82842 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82843 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82844 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
82847 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
82855 assert( (p->flags & MEM_Cleared)==0 );
82859 assert( p->szMalloc==0
82860 || (p->flags==MEM_Undefined
82861 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
82862 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
82872 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
82874 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
82875 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82876 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82877 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
82888 static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
82890 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
82892 if( p->flags & MEM_Int ){
82894 /* Work-around for GCC bug
82897 assert( (p->flags&MEM_Int)*2==sizeof(x) );
82898 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
82899 p->n = sqlite3Int64ToText(x, zBuf);
82901 p->n = sqlite3Int64ToText(p->u.i, zBuf);
82906 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
82909 p->n = acc.nChar;
82920 ** A single int or real value always converts to the same strings. But
82921 ** many different strings can be converted into the same int or real.
82926 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
82936 SQLITE_PRIVATE int sqlite3VdbeMemValidStrRep(Mem *p){
82940 int i, j, incr;
82941 if( (p->flags & MEM_Str)==0 ) return 1;
82942 if( p->db && p->db->mallocFailed ) return 1;
82943 if( p->flags & MEM_Term ){
82944 /* Insure that the string is properly zero-terminated. Pay particular
82945 ** attention to the case where p->n is odd */
82946 if( p->szMalloc>0 && p->z==p->zMalloc ){
82947 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
82948 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
82950 assert( p->z[p->n]==0 );
82951 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
82952 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
82954 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
82957 z = p->z;
82960 if( p->enc!=SQLITE_UTF8 ){
82962 if( p->enc==SQLITE_UTF16BE ) z++;
82979 ** routine is a no-op.
82985 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
82987 int rc;
82993 if( !(pMem->flags&MEM_Str) ){
82994 pMem->enc = desiredEnc;
82997 if( pMem->enc==desiredEnc ){
83000 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83010 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
83011 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
83017 ** Make sure pMem->z points to a writable allocation of at least n bytes.
83020 ** pMem->z into the new allocation. pMem must be either a string or
83022 ** in pMem->z is discarded.
83024 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
83027 testcase( pMem->db==0 );
83031 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
83032 testcase( bPreserve && pMem->z==0 );
83034 assert( pMem->szMalloc==0
83035 || (pMem->flags==MEM_Undefined
83036 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
83037 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
83038 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
83039 if( pMem->db ){
83040 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
83042 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
83043 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
83044 pMem->z = pMem->zMalloc;
83048 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83049 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
83051 if( pMem->zMalloc==0 ){
83053 pMem->z = 0;
83054 pMem->szMalloc = 0;
83057 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
83060 if( bPreserve && pMem->z ){
83061 assert( pMem->z!=pMem->zMalloc );
83062 memcpy(pMem->zMalloc, pMem->z, pMem->n);
83064 if( (pMem->flags&MEM_Dyn)!=0 ){
83065 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
83066 pMem->xDel((void *)(pMem->z));
83069 pMem->z = pMem->zMalloc;
83070 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
83075 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
83076 ** If pMem->zMalloc already meets or exceeds the requested size, this
83077 ** routine is a no-op.
83080 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
83087 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
83089 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
83090 if( pMem->szMalloc<szNew ){
83093 assert( (pMem->flags & MEM_Dyn)==0 );
83094 pMem->z = pMem->zMalloc;
83095 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
83100 ** If pMem is already a string, detect if it is a zero-terminated
83104 ** this routine is a no-op.
83107 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){
83111 if( pMem->enc!=SQLITE_UTF8 ) return;
83112 if( NEVER(pMem->z==0) ) return;
83113 if( pMem->flags & MEM_Dyn ){
83114 if( pMem->xDel==sqlite3_free
83115 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
83117 pMem->z[pMem->n] = 0;
83118 pMem->flags |= MEM_Term;
83121 if( pMem->xDel==sqlite3RCStrUnref ){
83122 /* Blindly assume that all RCStr objects are zero-terminated */
83123 pMem->flags |= MEM_Term;
83126 }else if( pMem->szMalloc >= pMem->n+1 ){
83127 pMem->z[pMem->n] = 0;
83128 pMem->flags |= MEM_Term;
83138 ** to be a double-zero byte at an even byte boundary in order to
83142 static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
83143 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
83146 pMem->z[pMem->n] = 0;
83147 pMem->z[pMem->n+1] = 0;
83148 pMem->z[pMem->n+2] = 0;
83149 pMem->flags |= MEM_Term;
83159 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
83161 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83163 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
83165 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
83166 int rc = vdbeMemAddTerminator(pMem);
83170 pMem->flags &= ~MEM_Ephem;
83172 pMem->pScopyFrom = 0;
83179 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
83183 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
83184 int nByte;
83186 assert( pMem->flags & MEM_Zero );
83187 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
83190 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83193 nByte = pMem->n + pMem->u.nZero;
83195 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
83201 assert( pMem->z!=0 );
83202 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
83204 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
83205 pMem->n += pMem->u.nZero;
83206 pMem->flags &= ~(MEM_Zero|MEM_Term);
83214 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
83216 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83217 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
83218 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
83219 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
83240 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
83241 const int nByte = 32;
83244 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83245 assert( !(pMem->flags&MEM_Zero) );
83246 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
83247 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
83253 pMem->enc = 0;
83257 vdbeMemRenderNum(nByte, pMem->z, pMem);
83258 assert( pMem->z!=0 );
83259 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
83260 pMem->enc = SQLITE_UTF8;
83261 pMem->flags |= MEM_Str|MEM_Term;
83262 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
83275 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
83280 assert( pMem->db!=0 );
83281 assert( pFunc->xFinalize!=0 );
83282 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
83283 assert( sqlite3_mutex_held(pMem->db->mutex) );
83287 t.db = pMem->db;
83292 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
83293 assert( (pMem->flags & MEM_Dyn)==0 );
83294 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83308 SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
83311 assert( pFunc->xValue!=0 );
83312 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
83313 assert( pAccum->db!=0 );
83314 assert( sqlite3_mutex_held(pAccum->db->mutex) );
83320 ctx.enc = ENC(pAccum->db);
83321 pFunc->xValue(&ctx);
83336 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
83338 if( p->flags&MEM_Agg ){
83339 sqlite3VdbeMemFinalize(p, p->u.pDef);
83340 assert( (p->flags & MEM_Agg)==0 );
83341 testcase( p->flags & MEM_Dyn );
83343 if( p->flags&MEM_Dyn ){
83344 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
83345 p->xDel((void *)p->z);
83347 p->flags = MEM_Null;
83352 ** by p->xDel and memory in p->zMalloc.
83362 if( p->szMalloc ){
83363 sqlite3DbFreeNN(p->db, p->zMalloc);
83364 p->szMalloc = 0;
83366 p->z = 0;
83381 if( VdbeMemDynamic(p) || p->szMalloc ){
83391 if( p->szMalloc ) vdbeMemClear(p);
83398 ** a floating-point then the value returned is the integer part.
83401 ** an SQL-NULL value, return 0.
83407 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
83411 int flags;
83413 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83415 flags = pMem->flags;
83418 return pMem->u.i;
83420 return sqlite3RealToI64(pMem->u.r);
83421 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
83437 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
83442 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83444 if( pMem->flags & MEM_Real ){
83445 return pMem->u.r;
83446 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
83447 testcase( pMem->flags & MEM_IntReal );
83448 return (double)pMem->u.i;
83449 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
83461 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
83462 testcase( pMem->flags & MEM_IntReal );
83463 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
83464 if( pMem->flags & MEM_Null ) return ifNull;
83474 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
83476 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83479 if( pMem->flags & MEM_IntReal ){
83482 i64 ix = sqlite3RealToI64(pMem->u.r);
83486 ** (1) the round-trip conversion real->int->real is a no-op, and
83494 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
83495 pMem->u.i = ix;
83504 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
83506 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83510 pMem->u.i = sqlite3VdbeIntValue(pMem);
83519 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
83521 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83524 pMem->u.r = sqlite3VdbeRealValue(pMem);
83534 ** For some versions of GCC on 32-bit machines, if you do the more obvious
83536 ** though the r1 and (double)i values are bit-for-bit the same.
83538 SQLITE_PRIVATE int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
83542 && i >= -2251799813685248LL && i < 2251799813685248LL);
83550 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
83563 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
83565 testcase( pMem->flags & MEM_Int );
83566 testcase( pMem->flags & MEM_Real );
83567 testcase( pMem->flags & MEM_IntReal );
83568 testcase( pMem->flags & MEM_Null );
83569 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
83570 int rc;
83572 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
83573 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83574 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
83575 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
83576 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
83578 pMem->u.i = ix;
83584 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
83585 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
83596 SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
83597 if( pMem->flags & MEM_Null ) return SQLITE_OK;
83600 if( (pMem->flags & MEM_Blob)==0 ){
83602 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
83603 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
83605 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
83622 int rc;
83625 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
83627 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
83628 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
83629 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
83645 pMem->flags = flags;
83646 pMem->db = db;
83647 pMem->szMalloc = 0;
83667 pMem->flags = MEM_Null;
83679 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
83681 pMem->flags = MEM_Blob|MEM_Zero;
83682 pMem->n = 0;
83684 pMem->u.nZero = n;
83685 pMem->enc = SQLITE_UTF8;
83686 pMem->z = 0;
83689 SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
83690 int nByte = n>0?n:1;
83694 assert( pMem->z!=0 );
83695 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
83696 memset(pMem->z, 0, nByte);
83697 pMem->n = n>0?n:0;
83698 pMem->flags = MEM_Blob;
83699 pMem->enc = SQLITE_UTF8;
83707 ** a 64-bit integer.
83711 pMem->u.i = val;
83712 pMem->flags = MEM_Int;
83723 pMem->u.i = val;
83724 pMem->flags = MEM_Int;
83731 SQLITE_PRIVATE void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){
83735 /* A no-op destructor */
83748 assert( pMem->flags==MEM_Null );
83750 pMem->u.zPType = zPType ? zPType : "";
83751 pMem->z = pPtr;
83752 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
83753 pMem->eSubtype = 'p';
83754 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
83765 pMem->u.r = val;
83766 pMem->flags = MEM_Real;
83776 SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
83777 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
83778 && pMem->xDel==sqlite3RowSetDelete;
83789 SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
83790 sqlite3 *db = pMem->db;
83797 pMem->z = (char*)p;
83798 pMem->flags = MEM_Blob|MEM_Dyn;
83799 pMem->xDel = sqlite3RowSetDelete;
83805 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
83807 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
83808 assert( p->db!=0 );
83809 if( p->flags & (MEM_Str|MEM_Blob) ){
83810 int n = p->n;
83811 if( p->flags & MEM_Zero ){
83812 n += p->u.nZero;
83814 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
83825 ** This is used for testing and debugging only - to help ensure that shallow
83829 int i;
83831 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
83832 if( pX->pScopyFrom==pMem ){
83834 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
83836 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
83844 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
83845 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
83848 ** undefined so that we can quickly detect the shallow-copy error */
83849 pX->flags = MEM_Undefined;
83850 pX->pScopyFrom = 0;
83853 pMem->pScopyFrom = 0;
83859 ** pTo are freed. The pFrom->z field is not duplicated. If
83860 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
83863 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
83868 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
83870 assert( pTo->db==pFrom->db );
83873 if( (pFrom->flags&MEM_Static)==0 ){
83874 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
83876 pTo->flags |= srcType;
83884 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
83885 int rc = SQLITE_OK;
83890 pTo->flags &= ~MEM_Dyn;
83891 if( pTo->flags&(MEM_Str|MEM_Blob) ){
83892 if( 0==(pFrom->flags&MEM_Static) ){
83893 pTo->flags |= MEM_Ephem;
83908 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
83909 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
83910 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
83914 pFrom->flags = MEM_Null;
83915 pFrom->szMalloc = 0;
83938 ** non-negative for blobs.
83940 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
83947 i64 nByte = n; /* New value for pMem->n */
83948 int iLimit; /* Maximum allowed string or blob size */
83949 u16 flags; /* New value for pMem->flags */
83952 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83962 if( pMem->db ){
83963 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
83984 sqlite3DbFree(pMem->db, (void*)z);
83990 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
84005 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
84008 memcpy(pMem->z, z, nAlloc);
84011 pMem->z = (char *)z;
84013 pMem->zMalloc = pMem->z;
84014 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
84016 pMem->xDel = xDel;
84021 pMem->n = (int)(nByte & 0x7fffffff);
84022 pMem->flags = flags;
84023 pMem->enc = enc;
84042 ** pMem->zMalloc to hold the content from the btree, if possible. New
84043 ** pMem->zMalloc space will be allocated if necessary. The calling routine
84050 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
84056 int rc;
84057 pMem->flags = MEM_Null;
84062 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
84064 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
84065 pMem->flags = MEM_Blob;
84066 pMem->n = (int)amt;
84073 SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(
84079 int rc = SQLITE_OK; /* Return code */
84087 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
84088 assert( pMem->z!=0 );
84091 pMem->flags = MEM_Blob|MEM_Ephem;
84092 pMem->n = (int)amt;
84103 ** to a zero-terminated version of that string.
84107 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
84110 assert( (pVal->flags & (MEM_Null))==0 );
84111 if( pVal->flags & (MEM_Blob|MEM_Str) ){
84113 pVal->flags |= MEM_Str;
84114 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
84117 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
84118 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
84123 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
84126 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
84128 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
84129 || pVal->db->mallocFailed );
84130 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
84132 return pVal->z;
84144 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
84150 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
84153 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
84155 return pVal->z;
84157 if( pVal->flags&MEM_Null ){
84167 ** that third-party extensions can get access to it?
84169 SQLITE_PRIVATE int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){
84171 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)
84172 && (pVal->flags & MEM_Dyn)!=0
84173 && pVal->xDel==xFree
84187 p->flags = MEM_Null;
84188 p->db = db;
84201 int iVal;
84209 ** Otherwise, if the second argument is non-zero, then this function is
84218 UnpackedRecord *pRec = p->ppRec[0];
84221 Index *pIdx = p->pIdx; /* Index being probed */
84222 int nByte; /* Bytes of space to allocate */
84223 int i; /* Counter variable */
84224 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
84229 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
84230 if( pRec->pKeyInfo ){
84231 assert( pRec->pKeyInfo->nAllField==nCol );
84232 assert( pRec->pKeyInfo->enc==ENC(db) );
84233 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
84235 pRec->aMem[i].flags = MEM_Null;
84236 pRec->aMem[i].db = db;
84244 p->ppRec[0] = pRec;
84247 pRec->nField = p->iVal+1;
84248 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
84249 return &pRec->aMem[p->iVal];
84278 static int valueFromFunction(
84288 int nVal = 0; /* Size of apVal[] array */
84291 int rc = SQLITE_OK; /* Return code */
84293 int i; /* Iterator variable */
84296 assert( (p->flags & EP_TokenOnly)==0 );
84298 pList = p->x.pList;
84299 if( pList ) nVal = pList->nExpr;
84301 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
84306 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
84307 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
84319 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
84334 pFunc->xSFunc(&ctx, nVal, apVal);
84337 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
84344 pCtx->pParse->nErr++;
84351 pCtx->pParse->rc = rc;
84377 static int valueFromExpr(
84385 int op;
84388 int negInt = 1;
84390 int rc = SQLITE_OK;
84393 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
84394 if( op==TK_REGISTER ) op = pExpr->op2;
84400 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
84405 aff = sqlite3AffinityType(pExpr->u.zToken,0);
84406 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
84412 /* zero-blobs only come from functions, not literal values. And
84423 ** case when the value is -9223372036854775808. Except - do not do this
84426 Expr *pLeft = pExpr->pLeft;
84427 if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){
84429 || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
84432 op = pExpr->op;
84433 negInt = -1;
84434 zNeg = "-";
84443 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
84446 if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
84449 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
84451 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
84456 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
84457 sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8);
84458 pVal->flags = MEM_Real;
84460 /* This case is required by -9223372036854775808 and other strings
84468 assert( (pVal->flags & MEM_IntReal)==0 );
84469 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
84470 testcase( pVal->flags & MEM_Int );
84471 testcase( pVal->flags & MEM_Real );
84472 pVal->flags &= ~MEM_Str;
84478 /* This branch happens for multiple negative signs. Ex: -(-5) */
84479 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
84483 if( pVal->flags & MEM_Real ){
84484 pVal->u.r = -pVal->u.r;
84485 }else if( pVal->u.i==SMALLEST_INT64 ){
84487 pVal->u.r = -(double)SMALLEST_INT64;
84489 pVal->u.r = LARGEST_INT64;
84493 pVal->u.i = -pVal->u.i;
84504 int nVal;
84506 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
84507 assert( pExpr->u.zToken[1]=='\'' );
84510 zVal = &pExpr->u.zToken[2];
84511 nVal = sqlite3Strlen30(zVal)-1;
84526 pVal->flags = MEM_Int;
84527 pVal->u.i = pExpr->u.zToken[4]==0;
84537 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
84560 SQLITE_PRIVATE int sqlite3ValueFromExpr(
84589 static int stat4ValueFromExpr(
84596 int rc = SQLITE_OK;
84598 sqlite3 *db = pParse->db;
84603 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
84609 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
84611 int iBindVar = pExpr->iColumn;
84612 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
84613 if( (v = pParse->pReprepare)!=0 ){
84616 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
84618 pVal->db = pParse->db;
84625 assert( pVal==0 || pVal->db==db );
84664 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
84669 int nElem, /* Maximum number of values to append */
84670 int iVal, /* Array element to populate */
84671 int *pnExtract /* OUT: Values appended to the record */
84673 int rc = SQLITE_OK;
84674 int nExtract = 0;
84676 if( pExpr==0 || pExpr->op!=TK_SELECT ){
84677 int i;
84687 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
84709 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
84719 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
84726 SQLITE_PRIVATE int sqlite3Stat4Column(
84729 int nRec, /* Size of buffer pRec in bytes */
84730 int iCol, /* Column to extract */
84738 int i; /* Column index */
84761 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
84762 pMem->enc = ENC(db);
84773 int i;
84774 int nCol = pRec->pKeyInfo->nAllField;
84775 Mem *aMem = pRec->aMem;
84780 sqlite3KeyInfoUnref(pRec->pKeyInfo);
84791 int n, /* Length of string z */
84805 sqlite3DbFreeNN(((Mem*)v)->db, v);
84813 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
84814 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
84816 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
84818 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
84819 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
84820 return p->n;
84822 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
84823 return p->n;
84825 if( (p->flags & MEM_Blob)!=0 ){
84826 if( p->flags & MEM_Zero ){
84827 return p->n + p->u.nZero;
84829 return p->n;
84832 if( p->flags & MEM_Null ) return 0;
84857 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
84863 sqlite3 *db = pParse->db;
84867 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
84868 p->db = db;
84869 if( db->pVdbe ){
84870 db->pVdbe->ppVPrev = &p->pVNext;
84872 p->pVNext = db->pVdbe;
84873 p->ppVPrev = &db->pVdbe;
84874 db->pVdbe = p;
84875 assert( p->eVdbeState==VDBE_INIT_STATE );
84876 p->pParse = pParse;
84877 pParse->pVdbe = p;
84878 assert( pParse->aLabel==0 );
84879 assert( pParse->nLabel==0 );
84880 assert( p->nOpAlloc==0 );
84881 assert( pParse->szOpAlloc==0 );
84890 return p->pParse;
84898 sqlite3DbFree(p->db, p->zErrMsg);
84900 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
84907 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
84909 p->prepFlags = prepFlags;
84911 p->expmask = 0;
84913 assert( p->zSql==0 );
84914 p->zSql = sqlite3DbStrNDup(p->db, z, n);
84919 ** Add a new element to the Vdbe->pDblStr list.
84923 int n = sqlite3Strlen30(z);
84925 sizeof(*pStr)+n+1-sizeof(pStr->z));
84927 pStr->pNextStr = p->pDblStr;
84928 p->pDblStr = pStr;
84929 memcpy(pStr->z, z, n+1);
84937 ** zId of length nId is a double-quoted identifier. Check to see if
84940 SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
84942 const char *zId /* The double-quoted identifier, already dequoted */
84946 if( pVdbe->pDblStr==0 ) return 0;
84947 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
84948 if( strcmp(zId, pStr->z)==0 ) return 1;
84955 ** Swap byte-code between two VDBE structures.
84967 assert( pA->db==pB->db );
84971 pTmp = pA->pVNext;
84972 pA->pVNext = pB->pVNext;
84973 pB->pVNext = pTmp;
84974 ppTmp = pA->ppVPrev;
84975 pA->ppVPrev = pB->ppVPrev;
84976 pB->ppVPrev = ppTmp;
84977 zTmp = pA->zSql;
84978 pA->zSql = pB->zSql;
84979 pB->zSql = zTmp;
84981 zTmp = pA->zNormSql;
84982 pA->zNormSql = pB->zNormSql;
84983 pB->zNormSql = zTmp;
84985 pB->expmask = pA->expmask;
84986 pB->prepFlags = pA->prepFlags;
84987 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
84988 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
84996 ** If an out-of-memory error occurs while resizing the array, return
85001 static int growOpArray(Vdbe *v, int nOp){
85003 Parse *p = v->pParse;
85005 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
85013 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
85014 : (sqlite3_int64)v->nOpAlloc+nOp);
85016 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
85022 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
85023 sqlite3OomFault(p->db);
85027 assert( nOp<=(int)(1024/sizeof(Op)) );
85028 assert( nNew>=(v->nOpAlloc+nOp) );
85029 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
85031 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
85032 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
85033 v->aOp = pNew;
85050 static void test_addop_breakpoint(int pc, Op *pOp){
85064 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
85065 assert( p->nOpAlloc<=p->nOp );
85067 assert( p->nOpAlloc>p->nOp );
85070 static SQLITE_NOINLINE int addOp4IntSlow(
85072 int op, /* The new opcode */
85073 int p1, /* The P1 operand */
85074 int p2, /* The P2 operand */
85075 int p3, /* The P3 operand */
85076 int p4 /* The P4 operand as an integer */
85078 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
85079 if( p->db->mallocFailed==0 ){
85080 VdbeOp *pOp = &p->aOp[addr];
85081 pOp->p4type = P4_INT32;
85082 pOp->p4.i = p4;
85100 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
85103 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
85106 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
85109 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
85110 int i;
85113 i = p->nOp;
85114 assert( p->eVdbeState==VDBE_INIT_STATE );
85116 if( p->nOpAlloc<=i ){
85119 assert( p->aOp!=0 );
85120 p->nOp++;
85121 pOp = &p->aOp[i];
85123 pOp->opcode = (u8)op;
85124 pOp->p5 = 0;
85125 pOp->p1 = p1;
85126 pOp->p2 = p2;
85127 pOp->p3 = p3;
85128 pOp->p4.p = 0;
85129 pOp->p4type = P4_NOTUSED;
85134 pOp->zComment = 0;
85137 pOp->nExec = 0;
85138 pOp->nCycle = 0;
85141 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85142 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85143 test_addop_breakpoint(i, &p->aOp[i]);
85147 pOp->iSrcLine = 0;
85154 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
85156 int op, /* The new opcode */
85157 int p1, /* The P1 operand */
85158 int p2, /* The P2 operand */
85159 int p3, /* The P3 operand */
85160 int p4 /* The P4 operand as an integer */
85162 int i;
85165 i = p->nOp;
85166 if( p->nOpAlloc<=i ){
85169 p->nOp++;
85170 pOp = &p->aOp[i];
85172 pOp->opcode = (u8)op;
85173 pOp->p5 = 0;
85174 pOp->p1 = p1;
85175 pOp->p2 = p2;
85176 pOp->p3 = p3;
85177 pOp->p4.i = p4;
85178 pOp->p4type = P4_INT32;
85183 pOp->zComment = 0;
85186 pOp->nExec = 0;
85187 pOp->nCycle = 0;
85190 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85191 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85192 test_addop_breakpoint(i, &p->aOp[i]);
85196 pOp->iSrcLine = 0;
85206 SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
85213 SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
85228 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
85230 int i;
85238 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
85251 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
85253 int op, /* The new opcode */
85254 int p1, /* The P1 operand */
85255 int p2, /* The P2 operand */
85256 int p3, /* The P3 operand */
85258 int p4type /* P4 operand type */
85260 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
85275 SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
85277 int p1, /* Constant argument mask */
85278 int p2, /* First argument register */
85279 int p3, /* Register into which results are written */
85280 int nArg, /* Number of argument */
85282 int eCallCtx /* Calling context */
85284 Vdbe *v = pParse->pVdbe;
85285 int nByte;
85286 int addr;
85289 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
85290 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
85292 assert( pParse->db->mallocFailed );
85293 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
85296 pCtx->pOut = 0;
85297 pCtx->pFunc = (FuncDef*)pFunc;
85298 pCtx->pVdbe = 0;
85299 pCtx->isError = 0;
85300 pCtx->argc = nArg;
85301 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
85313 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
85315 int op, /* The new opcode */
85316 int p1, /* The P1 operand */
85317 int p2, /* The P2 operand */
85318 int p3, /* The P3 operand */
85320 int p4type /* P4 operand type */
85332 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
85334 if( pParse->addrExplain==0 ) return 0;
85335 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
85336 return pOp->p2;
85356 SQLITE_PRIVATE int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
85357 int addr = 0;
85361 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
85367 int iThis;
85369 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
85371 v = pParse->pVdbe;
85372 iThis = v->nOp;
85373 addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
85375 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
85377 pParse->addrExplain = iThis;
85379 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
85389 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
85394 ** Add an OP_ParseSchema opcode. This routine is broken out from
85401 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
85402 int j;
85405 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
85406 sqlite3MayAbort(p->pParse);
85409 /* Insert the end of a co-routine
85411 SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
85415 ** co-routine has its own independent set of registers, because co-routines
85417 ** that could cause problems if two or more co-routines are using the same
85420 v->pParse->nTempReg = 0;
85421 v->pParse->nRangeReg = 0;
85433 ** always negative and P2 values are suppose to be non-negative.
85440 ** Parse.aLabel[x] Stores the address that the x-th label resolves
85442 ** labels stores -1, but that is not required.
85449 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
85450 return --pParse->nLabel;
85458 static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
85459 int nNewSize = 10 - p->nLabel;
85460 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
85461 nNewSize*sizeof(p->aLabel[0]));
85462 if( p->aLabel==0 ){
85463 p->nLabelAlloc = 0;
85466 int i;
85467 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
85469 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
85472 p->nLabelAlloc = nNewSize;
85473 p->aLabel[j] = v->nOp;
85476 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
85477 Parse *p = v->pParse;
85478 int j = ADDR(x);
85479 assert( v->eVdbeState==VDBE_INIT_STATE );
85480 assert( j<-p->nLabel );
85483 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85484 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
85487 if( p->nLabelAlloc + p->nLabel < 0 ){
85490 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
85491 p->aLabel[j] = v->nOp;
85506 int i;
85507 for(i=1; ALWAYS(i<p->nOp); i++){
85508 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
85509 p->aOp[1].opcode = OP_Noop;
85519 ** in a Vdbe main program and each of the sub-programs (triggers) it may
85530 ** sqlite3DbFree(v->db, sIter.apSub);
85537 int nSub; /* Number of entries in apSub */
85538 int iAddr; /* Address of next instruction to return */
85539 int iSub; /* 0 = main program, 1 = first sub-program etc. */
85542 Vdbe *v = p->v;
85545 int nOp;
85547 if( p->iSub<=p->nSub ){
85549 if( p->iSub==0 ){
85550 aOp = v->aOp;
85551 nOp = v->nOp;
85553 aOp = p->apSub[p->iSub-1]->aOp;
85554 nOp = p->apSub[p->iSub-1]->nOp;
85556 assert( p->iAddr<nOp );
85558 pRet = &aOp[p->iAddr];
85559 p->iAddr++;
85560 if( p->iAddr==nOp ){
85561 p->iSub++;
85562 p->iAddr = 0;
85565 if( pRet->p4type==P4_SUBPROGRAM ){
85566 int nByte = (p->nSub+1)*sizeof(SubProgram*);
85567 int j;
85568 for(j=0; j<p->nSub; j++){
85569 if( p->apSub[j]==pRet->p4.pProgram ) break;
85571 if( j==p->nSub ){
85572 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
85573 if( !p->apSub ){
85576 p->apSub[p->nSub++] = pRet->p4.pProgram;
85589 ** sub-programs contains any of the following:
85606 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
85608 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
85609 int hasAbort = 0;
85610 int hasFkCounter = 0;
85611 int hasCreateTable = 0;
85612 int hasCreateIndex = 0;
85613 int hasInitCoroutine = 0;
85622 int opcode = pOp->opcode;
85629 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
85634 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
85638 ** where a "DELETE FROM tbl" has a statement-journal but does not
85639 ** require one. This is not so bad - it is an inefficiency, not a bug. */
85640 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
85645 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
85650 sqlite3DbFree(v->db, sIter.apSub);
85657 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
85661 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
85670 || (pC->eCurType!=CURTYPE_SORTER
85671 && pC->eCurType!=CURTYPE_PSEUDO
85672 && !pC->isEphemeral)
85674 p->nWrite++;
85685 assert( p->nWrite==0 || p->usesStmtJournal );
85710 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
85711 int nMaxArgs = *pMaxFuncArgs;
85713 Parse *pParse = p->pParse;
85714 int *aLabel = pParse->aLabel;
85716 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
85717 p->readOnly = 1;
85718 p->bIsReader = 0;
85719 pOp = &p->aOp[p->nOp-1];
85720 assert( p->aOp[0].opcode==OP_Init );
85728 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
85731 switch( pOp->opcode ){
85733 if( pOp->p2!=0 ) p->readOnly = 0;
85738 p->bIsReader = 1;
85746 p->readOnly = 0;
85747 p->bIsReader = 1;
85751 assert( pOp->p2>=0 );
85756 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
85760 int n;
85761 assert( (pOp - p->aOp) >= 3 );
85762 assert( pOp[-1].opcode==OP_Integer );
85763 n = pOp[-1].p1;
85770 if( pOp->p2<0 ){
85772 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85773 ** have non-negative values for P2. */
85774 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
85775 assert( ADDR(pOp->p2)<-pParse->nLabel );
85776 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
85777 pOp->p2 = aLabel[ADDR(pOp->p2)];
85782 assert( pOp->p2>0
85783 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 );
85786 assert( pOp->p2<p->nOp
85787 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 );
85792 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85793 ** have non-negative values for P2. */
85794 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
85796 assert( pOp>p->aOp );
85797 pOp--;
85801 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
85802 pParse->aLabel = 0;
85804 pParse->nLabel = 0;
85806 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
85831 Vdbe *v, /* The byte-code program under construction */
85832 int iFirst, /* First opcode of the subroutine */
85833 int iLast, /* Last opcode of the subroutine */
85834 int iRetReg /* Subroutine return address register */
85838 int i;
85841 pParse = v->pParse;
85843 if( pParse->nErr ) return;
85845 assert( iLast<v->nOp );
85846 pOp = &v->aOp[iFirst];
85848 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
85849 int iDest = pOp->p2; /* Jump destination */
85851 if( pOp->opcode==OP_Gosub ) continue;
85852 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull ){
85853 /* This is a deliberately taken illegal branch. tag-20230325-2 */
85857 int j = ADDR(iDest);
85859 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
85862 iDest = pParse->aLabel[j];
85865 int j = iDest;
85866 for(; j<v->nOp; j++){
85867 VdbeOp *pX = &v->aOp[j];
85868 if( pX->opcode==OP_Return ){
85869 if( pX->p1==iRetReg ) break;
85872 if( pX->opcode==OP_Noop ) continue;
85873 if( pX->opcode==OP_Explain ) continue;
85900 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
85901 assert( p->eVdbeState==VDBE_INIT_STATE );
85902 return p->nOp;
85911 ** sqlite3VdbeAddOpList() will always be non-NULL.
85914 SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
85915 assert( p->nOp + N <= p->nOpAlloc );
85928 int i;
85929 for(i=0; i<p->nOp; i++){
85930 assert( p->aOp[i].opcode!=OP_ResultRow );
85941 SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
85957 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
85958 VdbeOp *aOp = p->aOp;
85959 assert( aOp && !p->db->mallocFailed );
85962 assert( DbMaskAllZero(p->btreeMask) );
85965 *pnOp = p->nOp;
85966 p->aOp = 0;
85974 ** Non-zero P2 arguments to jump instructions are automatically adjusted
85979 int nOp, /* Number of opcodes to add */
85981 int iLineno /* Source-file line number of first opcode */
85983 int i;
85986 assert( p->eVdbeState==VDBE_INIT_STATE );
85987 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
85990 pFirst = pOut = &p->aOp[p->nOp];
85992 pOut->opcode = aOp->opcode;
85993 pOut->p1 = aOp->p1;
85994 pOut->p2 = aOp->p2;
85995 assert( aOp->p2>=0 );
85996 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
85997 pOut->p2 += p->nOp;
85999 pOut->p3 = aOp->p3;
86000 pOut->p4type = P4_NOTUSED;
86001 pOut->p4.p = 0;
86002 pOut->p5 = 0;
86004 pOut->zComment = 0;
86007 pOut->iSrcLine = iLineno+i;
86012 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86013 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
86017 p->nOp += nOp;
86027 int addrExplain, /* Address of OP_Explain (or 0) */
86028 int addrLoop, /* Address of loop counter */
86029 int addrVisit, /* Address of rows visited counter */
86033 if( IS_STMT_SCANSTATUS(p->db) ){
86034 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
86036 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
86038 ScanStatus *pNew = &aNew[p->nScan++];
86040 pNew->addrExplain = addrExplain;
86041 pNew->addrLoop = addrLoop;
86042 pNew->addrVisit = addrVisit;
86043 pNew->nEst = nEst;
86044 pNew->zName = sqlite3DbStrDup(p->db, zName);
86045 p->aScan = aNew;
86059 int addrExplain,
86060 int addrStart,
86061 int addrEnd
86063 if( IS_STMT_SCANSTATUS(p->db) ){
86065 int ii;
86066 for(ii=p->nScan-1; ii>=0; ii--){
86067 pScan = &p->aScan[ii];
86068 if( pScan->addrExplain==addrExplain ) break;
86072 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
86073 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
86074 if( pScan->aAddrRange[ii]==0 ){
86075 pScan->aAddrRange[ii] = addrStart;
86076 pScan->aAddrRange[ii+1] = addrEnd;
86091 int addrExplain,
86092 int addrLoop,
86093 int addrVisit
86095 if( IS_STMT_SCANSTATUS(p->db) ){
86097 int ii;
86098 for(ii=p->nScan-1; ii>=0; ii--){
86099 pScan = &p->aScan[ii];
86100 if( pScan->addrExplain==addrExplain ) break;
86104 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
86105 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
86116 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
86118 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
86120 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
86122 sqlite3VdbeGetOp(p,addr)->p1 = val;
86124 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
86125 assert( addr>=0 || p->db->mallocFailed );
86126 sqlite3VdbeGetOp(p,addr)->p2 = val;
86128 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
86130 sqlite3VdbeGetOp(p,addr)->p3 = val;
86133 assert( p->nOp>0 || p->db->mallocFailed );
86134 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
86142 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
86144 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
86145 pOp->p5 |= OPFLAG_TYPEOFARG;
86153 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
86154 sqlite3VdbeChangeP2(p, addr, p->nOp);
86160 ** the previous opcode (and is thus a no-op) then simply back up
86165 ** strives to omit useless byte-code like this:
86170 SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
86171 if( addr==p->nOp-1 ){
86172 assert( p->aOp[addr].opcode==OP_Once
86173 || p->aOp[addr].opcode==OP_If
86174 || p->aOp[addr].opcode==OP_FkIfZero );
86175 assert( p->aOp[addr].p4type==0 );
86177 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
86179 p->nOp--;
86181 sqlite3VdbeChangeP2(p, addr, p->nOp);
86192 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
86201 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
86206 freeEphemeralFunction(db, p->pFunc);
86209 static void freeP4(sqlite3 *db, int p4type, void *p4){
86224 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
86238 if( db->pnBytesFreed==0 ){
86246 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
86250 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
86261 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
86265 Op *pOp = &aOp[nOp-1];
86267 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
86269 sqlite3DbFree(db, pOp->zComment);
86272 pOp--;
86280 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
86284 p->pNext = pVdbe->pProgram;
86285 pVdbe->pProgram = p;
86291 SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
86292 return pVdbe->pProgram!=0;
86298 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
86300 if( p->db->mallocFailed ) return 0;
86301 assert( addr>=0 && addr<p->nOp );
86302 pOp = &p->aOp[addr];
86303 freeP4(p->db, pOp->p4type, pOp->p4.p);
86304 pOp->p4type = P4_NOTUSED;
86305 pOp->p4.z = 0;
86306 pOp->opcode = OP_Noop;
86314 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
86315 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
86316 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
86329 int iFirst, /* Index of first register to be released */
86330 int N, /* Number of registers to release */
86332 int bUndefine /* If true, mark registers as undefined */
86334 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
86335 assert( pParse->pVdbe );
86337 assert( iFirst+N-1<=pParse->nMem );
86342 N--;
86344 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
86345 mask &= ~MASKBIT32(N-1);
86346 N--;
86350 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
86351 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
86377 int n
86379 if( pOp->p4type ){
86380 assert( pOp->p4type > P4_FREE_IF_LE );
86381 pOp->p4type = 0;
86382 pOp->p4.p = 0;
86385 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
86388 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
86389 pOp->p4type = P4_DYNAMIC;
86392 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
86396 db = p->db;
86397 assert( p->eVdbeState==VDBE_INIT_STATE );
86398 assert( p->aOp!=0 || db->mallocFailed );
86399 if( db->mallocFailed ){
86403 assert( p->nOp>0 );
86404 assert( addr<p->nOp );
86406 addr = p->nOp - 1;
86408 pOp = &p->aOp[addr];
86409 if( n>=0 || pOp->p4type ){
86414 /* Note: this cast is safe, because the origin data point was an int
86416 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
86417 pOp->p4type = P4_INT32;
86420 pOp->p4.p = (void*)zP4;
86421 pOp->p4type = (signed char)n;
86428 ** to the value defined by the arguments. This is a high-speed
86435 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
86439 if( p->db->mallocFailed ){
86440 freeP4(p->db, n, pP4);
86443 assert( p->nOp>0 );
86444 pOp = &p->aOp[p->nOp-1];
86445 assert( pOp->p4type==P4_NOTUSED );
86446 pOp->p4type = n;
86447 pOp->p4.p = pP4;
86456 Vdbe *v = pParse->pVdbe;
86467 ** insert a No-op and add the comment to that new instruction. This
86472 assert( p->nOp>0 || p->aOp==0 );
86473 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
86474 if( p->nOp ){
86475 assert( p->aOp );
86476 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
86477 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
86503 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
86504 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
86509 ** Return the opcode for a given address. The address must be non-negative.
86521 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
86525 assert( p->eVdbeState==VDBE_INIT_STATE );
86526 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
86527 if( p->db->mallocFailed ){
86530 return &p->aOp[addr];
86537 return sqlite3VdbeGetOp(p, p->nOp - 1);
86545 static int translateP(char c, const Op *pOp){
86546 if( c=='1' ) return pOp->p1;
86547 if( c=='2' ) return pOp->p2;
86548 if( c=='3' ) return pOp->p3;
86549 if( c=='4' ) return pOp->p4.i;
86550 return pOp->p5;
86561 ** "PX" -> "r[X]"
86562 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
86563 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
86564 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
86567 sqlite3 *db, /* Optional - Oom error reporting only */
86573 int nOpName;
86574 int ii;
86579 zOpName = sqlite3OpcodeName(pOp->opcode);
86582 int seenCom = 0;
86595 if( pOp->zComment && pOp->zComment[0] ){
86596 sqlite3_str_appendall(&x, pOp->zComment);
86601 int v1 = translateP(c, pOp);
86602 int v2;
86613 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
86616 sqlite3_context *pCtx = pOp->p4.pCtx;
86617 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
86619 }else if( pCtx->argc>1 ){
86620 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
86623 x.nChar -= 2;
86629 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
86638 if( !seenCom && pOp->zComment ){
86639 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
86641 }else if( pOp->zComment ){
86642 sqlite3_str_appendall(&x, pOp->zComment);
86658 switch( pExpr->op ){
86661 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
86664 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
86670 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
86674 if( pExpr->iColumn<0 ){
86677 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
86715 displayP4Expr(p, pExpr->pLeft);
86716 if( pExpr->pRight ){
86718 displayP4Expr(p, pExpr->pRight);
86736 switch( pOp->p4type ){
86738 int j;
86739 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
86740 assert( pKeyInfo->aSortFlags!=0 );
86741 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
86742 for(j=0; j<pKeyInfo->nKeyField; j++){
86743 CollSeq *pColl = pKeyInfo->aColl[j];
86744 const char *zColl = pColl ? pColl->zName : "";
86747 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
86748 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
86756 displayP4Expr(&x, pOp->p4.pExpr);
86762 CollSeq *pColl = pOp->p4.pColl;
86763 assert( pColl->enc<4 );
86764 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
86765 encnames[pColl->enc]);
86769 FuncDef *pDef = pOp->p4.pFunc;
86770 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86774 FuncDef *pDef = pOp->p4.pCtx->pFunc;
86775 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86779 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
86783 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
86787 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
86791 Mem *pMem = pOp->p4.pMem;
86792 if( pMem->flags & MEM_Str ){
86793 zP4 = pMem->z;
86794 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
86795 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
86796 }else if( pMem->flags & MEM_Real ){
86797 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
86798 }else if( pMem->flags & MEM_Null ){
86801 assert( pMem->flags & MEM_Blob );
86808 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
86815 u32 *ai = pOp->p4.ai;
86829 zP4 = pOp->p4.pTab->zName;
86833 zP4 = pOp->p4.z;
86845 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
86849 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
86850 ** p->btreeMask of databases that will require a lock.
86852 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
86853 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
86854 assert( i<(int)sizeof(p->btreeMask)*8 );
86855 DbMaskSet(p->btreeMask, i);
86856 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
86857 DbMaskSet(p->lockMask, i);
86863 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
86867 ** that the correct busy-handler callback is invoked if required.
86869 ** If SQLite is not threadsafe but does support shared-cache mode, then
86874 ** If SQLite is not threadsafe and does not support shared-cache mode, this
86875 ** function is a no-op.
86877 ** The p->btreeMask field is a bitmask of all btrees that the prepared
86878 ** statement p will ever use. Let N be the number of bits in p->btreeMask
86884 int i;
86887 int nDb;
86888 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86889 db = p->db;
86890 aDb = db->aDb;
86891 nDb = db->nDb;
86893 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86905 int i;
86908 int nDb;
86909 db = p->db;
86910 aDb = db->aDb;
86911 nDb = db->nDb;
86913 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86919 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86928 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
86932 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
86946 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
86947 zP4 ? zP4 : "", pOp->p5,
86960 ** This is a high-runner, so only those fields that really do need to
86972 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
86975 p->flags = flags;
86976 p->db = db;
86977 p->szMalloc = 0;
86979 p->pScopyFrom = 0;
86982 }while( (--N)>0 );
86994 static void releaseMemArray(Mem *p, int N){
86997 sqlite3 *db = p->db;
86998 if( db->pnBytesFreed ){
87000 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87014 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
87015 ** sqlite3MemRelease() were called from here. With -O2, this jumps
87020 testcase( p->flags & MEM_Agg );
87021 testcase( p->flags & MEM_Dyn );
87022 if( p->flags&(MEM_Agg|MEM_Dyn) ){
87023 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
87025 p->flags = MEM_Undefined;
87026 }else if( p->szMalloc ){
87027 sqlite3DbNNFreeNN(db, p->zMalloc);
87028 p->szMalloc = 0;
87029 p->flags = MEM_Undefined;
87033 p->flags = MEM_Undefined;
87047 SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
87048 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
87064 pFrame->pParent = pFrame->v->pDelFrame;
87065 pFrame->v->pDelFrame = pFrame;
87076 SQLITE_PRIVATE int sqlite3VdbeNextOpcode(
87079 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
87080 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
87081 int *piAddr, /* OUT: Write index into (*paOp)[] here */
87084 int nRow; /* Stop when row count reaches this */
87085 int nSub = 0; /* Number of sub-vdbes seen so far */
87086 SubProgram **apSub = 0; /* Array of sub-vdbes */
87087 int i; /* Next instruction address */
87088 int rc = SQLITE_OK; /* Result code */
87090 int iPc; /* Rowid. Copy of value in *piPc */
87097 ** encountered, but p->pc will eventually catch up to nRow.
87099 nRow = p->nOp;
87101 if( pSub->flags&MEM_Blob ){
87104 nSub = pSub->n/sizeof(Vdbe*);
87105 apSub = (SubProgram **)pSub->z;
87108 nRow += apSub[i]->nOp;
87115 p->rc = SQLITE_OK;
87119 if( i<p->nOp ){
87122 aOp = p->aOp;
87126 int j;
87127 i -= p->nOp;
87130 for(j=0; i>=apSub[j]->nOp; j++){
87131 i -= apSub[j]->nOp;
87132 assert( i<apSub[j]->nOp || j+1<nSub );
87134 aOp = apSub[j]->aOp;
87139 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
87143 int nByte = (nSub+1)*sizeof(SubProgram*);
87144 int j;
87149 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
87150 if( p->rc!=SQLITE_OK ){
87154 apSub = (SubProgram **)pSub->z;
87157 pSub->n = nSub*sizeof(SubProgram*);
87158 nRow += aOp[i].p4.pProgram->nOp;
87165 if( pOp->opcode==OP_OpenRead ) break;
87166 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
87167 if( pOp->opcode==OP_ReopenIdx ) break;
87189 int i;
87191 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
87193 for(i=0; i<p->nChildCsr; i++){
87194 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
87196 releaseMemArray(aMem, p->nChildMem);
87197 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
87198 sqlite3DbFree(p->v->db, p);
87209 ** When p->explain==1, each instruction is listed. When
87210 ** p->explain==2, only OP_Explain instructions are listed and these
87211 ** are shown in a different format. p->explain==2 is used to implement
87213 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
87217 ** When p->explain==1, first the main program is listed, then each of
87220 SQLITE_PRIVATE int sqlite3VdbeList(
87224 sqlite3 *db = p->db; /* The database connection */
87225 int i; /* Loop counter */
87226 int rc = SQLITE_OK; /* Return code */
87227 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
87228 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
87232 assert( p->explain );
87233 assert( p->eVdbeState==VDBE_RUN_STATE );
87234 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
87238 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
87242 if( p->rc==SQLITE_NOMEM ){
87254 assert( p->nMem>9 );
87255 pSub = &p->aMem[9];
87261 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
87265 if( AtomicLoad(&db->u1.isInterrupted) ){
87266 p->rc = SQLITE_INTERRUPT;
87268 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
87271 if( p->explain==2 ){
87272 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
87273 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
87274 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
87275 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
87276 assert( p->nResColumn==4 );
87279 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
87280 -1, SQLITE_UTF8, SQLITE_STATIC);
87281 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
87282 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
87283 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
87285 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
87289 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
87294 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
87295 assert( p->nResColumn==8 );
87297 p->pResultRow = pMem;
87298 if( db->mallocFailed ){
87299 p->rc = SQLITE_NOMEM;
87302 p->rc = SQLITE_OK;
87317 if( p->zSql ){
87318 z = p->zSql;
87319 }else if( p->nOp>=1 ){
87320 const VdbeOp *pOp = &p->aOp[0];
87321 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
87322 z = pOp->p4.z;
87335 int nOp = p->nOp;
87339 pOp = &p->aOp[0];
87340 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
87341 int i, j;
87343 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
87347 if( z[i-1]!=' ' ){
87370 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
87389 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
87392 if( nByte <= p->nFree ){
87393 p->nFree -= nByte;
87394 pBuf = &p->pSpace[p->nFree];
87396 p->nNeeded += nByte;
87409 int i;
87412 assert( p->eVdbeState==VDBE_INIT_STATE
87413 || p->eVdbeState==VDBE_READY_STATE
87414 || p->eVdbeState==VDBE_HALT_STATE );
87418 assert( p->nOp>0 );
87420 p->eVdbeState = VDBE_READY_STATE;
87423 for(i=0; i<p->nMem; i++){
87424 assert( p->aMem[i].db==p->db );
87427 p->pc = -1;
87428 p->rc = SQLITE_OK;
87429 p->errorAction = OE_Abort;
87430 p->nChange = 0;
87431 p->cacheCtr = 1;
87432 p->minWriteFileFormat = 255;
87433 p->iStatement = 0;
87434 p->nFkConstraint = 0;
87436 for(i=0; i<p->nOp; i++){
87437 p->aOp[i].nExec = 0;
87438 p->aOp[i].nCycle = 0;
87466 int nVar; /* Number of parameters */
87467 int nMem; /* Number of VM memory registers */
87468 int nCursor; /* Number of cursors required */
87469 int nArg; /* Number of arguments in subprograms */
87470 int n; /* Loop counter */
87474 assert( p->nOp>0 );
87476 assert( p->eVdbeState==VDBE_INIT_STATE );
87477 assert( pParse==p->pParse );
87478 p->pVList = pParse->pVList;
87479 pParse->pVList = 0;
87480 db = p->db;
87481 assert( db->mallocFailed==0 );
87482 nVar = pParse->nVar;
87483 nMem = pParse->nMem;
87484 nCursor = pParse->nTab;
87485 nArg = pParse->nMaxArg;
87499 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
87500 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
87502 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
87507 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
87508 if( pParse->explain ){
87510 p->explain = pParse->explain;
87511 p->nResColumn = 12 - 4*p->explain;
87513 p->expired = 0;
87521 ** This two-pass approach that reuses as much memory as possible from
87526 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
87527 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
87528 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
87529 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
87531 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
87533 if( !db->mallocFailed ){
87534 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
87535 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
87536 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
87537 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
87541 if( db->mallocFailed ){
87542 p->nVar = 0;
87543 p->nCursor = 0;
87544 p->nMem = 0;
87546 p->nCursor = nCursor;
87547 p->nVar = (ynVar)nVar;
87548 initMemArray(p->aVar, nVar, db, MEM_Null);
87549 p->nMem = nMem;
87550 initMemArray(p->aMem, nMem, db, MEM_Undefined);
87551 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
87564 VdbeTxtBlbCache *pCache = pCx->pCache;
87565 assert( pCx->colCache );
87566 pCx->colCache = 0;
87567 pCx->pCache = 0;
87568 if( pCache->pCValue ){
87569 sqlite3RCStrUnref(pCache->pCValue);
87570 pCache->pCValue = 0;
87572 sqlite3DbFree(p->db, pCache);
87576 if( pCx->colCache ){
87580 switch( pCx->eCurType ){
87582 sqlite3VdbeSorterClose(p->db, pCx);
87586 assert( pCx->uc.pCursor!=0 );
87587 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
87592 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
87593 const sqlite3_module *pModule = pVCur->pVtab->pModule;
87594 assert( pVCur->pVtab->nRef>0 );
87595 pVCur->pVtab->nRef--;
87596 pModule->xClose(pVCur);
87607 int i;
87608 for(i=0; i<p->nCursor; i++){
87609 VdbeCursor *pC = p->apCsr[i];
87612 p->apCsr[i] = 0;
87619 ** is used, for example, when a trigger sub-program is halted to restore
87622 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
87623 Vdbe *v = pFrame->v;
87625 v->aOp = pFrame->aOp;
87626 v->nOp = pFrame->nOp;
87627 v->aMem = pFrame->aMem;
87628 v->nMem = pFrame->nMem;
87629 v->apCsr = pFrame->apCsr;
87630 v->nCursor = pFrame->nCursor;
87631 v->db->lastRowid = pFrame->lastRowid;
87632 v->nChange = pFrame->nChange;
87633 v->db->nChange = pFrame->nDbChange;
87634 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
87635 v->pAuxData = pFrame->pAuxData;
87636 pFrame->pAuxData = 0;
87637 return pFrame->pc;
87649 if( p->pFrame ){
87651 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
87653 p->pFrame = 0;
87654 p->nFrame = 0;
87656 assert( p->nFrame==0 );
87658 releaseMemArray(p->aMem, p->nMem);
87659 while( p->pDelFrame ){
87660 VdbeFrame *pDel = p->pDelFrame;
87661 p->pDelFrame = pDel->pParent;
87666 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
87667 assert( p->pAuxData==0 );
87676 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
87677 int n;
87678 sqlite3 *db = p->db;
87680 if( p->nResAlloc ){
87681 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
87682 sqlite3DbFree(db, p->aColName);
87685 p->nResColumn = p->nResAlloc = (u16)nResColumn;
87686 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
87687 if( p->aColName==0 ) return;
87688 initMemArray(p->aColName, n, db, MEM_Null);
87701 SQLITE_PRIVATE int sqlite3VdbeSetColName(
87703 int idx, /* Index of column zName applies to */
87704 int var, /* One of the COLNAME_* constants */
87708 int rc;
87710 assert( idx<p->nResAlloc );
87712 if( p->db->mallocFailed ){
87716 assert( p->aColName!=0 );
87717 pColName = &(p->aColName[idx+var*p->nResAlloc]);
87718 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
87719 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
87726 ** write-transaction spanning more than one database file, this routine
87727 ** takes care of the super-journal trickery.
87729 static int vdbeCommit(sqlite3 *db, Vdbe *p){
87730 int i;
87731 int nTrans = 0; /* Number of databases with an active write-transaction
87732 ** that are candidates for a two-phase commit using a
87733 ** super-journal */
87734 int rc = SQLITE_OK;
87735 int needXcommit = 0;
87746 ** be done before determining whether a super-journal file is
87755 ** one database file has an open write transaction, a super-journal
87758 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87759 Btree *pBt = db->aDb[i].pBt;
87761 /* Whether or not a database might need a super-journal depends upon
87763 ** journal modes use a super-journal and which do not */
87776 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
87791 /* If there are any write-transactions at all, invoke the commit hook */
87792 if( needXcommit && db->xCommitCallback ){
87793 rc = db->xCommitCallback(db->pCommitArg);
87799 /* The simple case - no more than one database file (not counting the
87801 ** super-journal.
87805 ** that case we do not support atomic multi-file commits, so use the
87808 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
87811 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87812 Btree *pBt = db->aDb[i].pBt;
87823 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87824 Btree *pBt = db->aDb[i].pBt;
87834 /* The complex case - There is a multi-file write-transaction active.
87835 ** This requires a super-journal file to ensure the transaction is
87840 sqlite3_vfs *pVfs = db->pVfs;
87841 char *zSuper = 0; /* File-name for the super-journal */
87842 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
87845 int res;
87846 int retryCount = 0;
87847 int nMainFile;
87849 /* Select a super-journal file name */
87867 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
87869 /* The antipenultimate character of the super-journal name must
87871 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
87876 /* Open the super-journal. */
87883 sqlite3DbFree(db, zSuper-4);
87888 ** super-journal file. If an error occurs at this point close
87889 ** and delete the super-journal file. All the individual journal files
87890 ** still have 'null' as the super-journal pointer, so they will roll
87893 for(i=0; i<db->nDb; i++){
87894 Btree *pBt = db->aDb[i].pBt;
87906 sqlite3DbFree(db, zSuper-4);
87912 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
87920 sqlite3DbFree(db, zSuper-4);
87925 ** sets the super-journal pointer in each individual journal. If
87926 ** an error occurs here, do not delete the super-journal file.
87930 ** super-journal file will be orphaned. But we cannot delete it,
87931 ** in case the super-journal file name was written into the journal
87934 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87935 Btree *pBt = db->aDb[i].pBt;
87943 sqlite3DbFree(db, zSuper-4);
87947 /* Delete the super-journal file. This commits the transaction. After
87952 sqlite3DbFree(db, zSuper-4);
87967 for(i=0; i<db->nDb; i++){
87968 Btree *pBt = db->aDb[i].pBt;
87987 ** This is an internal self-check only - it is not an essential processing
87990 ** This is a no-op if NDEBUG is defined.
87995 int cnt = 0;
87996 int nWrite = 0;
87997 int nRead = 0;
87998 p = db->pVdbe;
88002 if( p->readOnly==0 ) nWrite++;
88003 if( p->bIsReader ) nRead++;
88005 p = p->pVNext;
88007 assert( cnt==db->nVdbeActive );
88008 assert( nWrite==db->nVdbeWrite );
88009 assert( nRead==db->nVdbeRead );
88016 ** If the Vdbe passed as the first argument opened a statement-transaction,
88025 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
88026 sqlite3 *const db = p->db;
88027 int rc = SQLITE_OK;
88028 int i;
88029 const int iSavepoint = p->iStatement-1;
88032 assert( db->nStatement>0 );
88033 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
88035 for(i=0; i<db->nDb; i++){
88036 int rc2 = SQLITE_OK;
88037 Btree *pBt = db->aDb[i].pBt;
88050 db->nStatement--;
88051 p->iStatement = 0;
88066 db->nDeferredCons = p->nStmtDefCons;
88067 db->nDeferredImmCons = p->nStmtDefImmCons;
88071 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
88072 if( p->db->nStatement && p->iStatement ){
88090 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
88091 sqlite3 *db = p->db;
88092 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
88093 || (!deferred && p->nFkConstraint>0)
88095 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
88096 p->errorAction = OE_Abort;
88098 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
88118 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
88119 int rc; /* Used to store transient return codes */
88120 sqlite3 *db = p->db;
88138 assert( p->eVdbeState==VDBE_RUN_STATE );
88139 if( db->mallocFailed ){
88140 p->rc = SQLITE_NOMEM_BKPT;
88147 if( p->bIsReader ){
88148 int mrc; /* Primary error code from p->rc */
88149 int eStatementOp = 0;
88150 int isSpecialError; /* Set to true if a 'special' error */
88156 if( p->rc ){
88157 mrc = p->rc & 0xff;
88166 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
88171 ** Even if the statement is read-only, it is important to perform
88173 ** occurred while writing to the journal, sub-journal or database
88178 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
88179 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
88187 db->autoCommit = 1;
88188 p->nChange = 0;
88194 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
88198 /* If the auto-commit flag is set and this is the only active writer
88205 && db->autoCommit
88206 && db->nVdbeWrite==(p->readOnly==0)
88208 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
88211 if( NEVER(p->readOnly) ){
88216 }else if( db->flags & SQLITE_CorruptRdOnly ){
88218 db->flags &= ~SQLITE_CorruptRdOnly;
88220 /* The auto-commit flag is true, the vdbe program was successful
88226 if( rc==SQLITE_BUSY && p->readOnly ){
88231 p->rc = rc;
88233 p->nChange = 0;
88235 db->nDeferredCons = 0;
88236 db->nDeferredImmCons = 0;
88237 db->flags &= ~(u64)SQLITE_DeferFKs;
88240 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
88241 p->nChange = 0;
88244 p->nChange = 0;
88246 db->nStatement = 0;
88248 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
88250 }else if( p->errorAction==OE_Abort ){
88255 db->autoCommit = 1;
88256 p->nChange = 0;
88260 /* If eStatementOp is non-zero, then a statement transaction needs to
88269 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
88270 p->rc = rc;
88271 sqlite3DbFree(db, p->zErrMsg);
88272 p->zErrMsg = 0;
88276 db->autoCommit = 1;
88277 p->nChange = 0;
88282 ** has been rolled back, update the database connection change-counter.
88284 if( p->changeCntOn ){
88286 sqlite3VdbeSetChanges(db, p->nChange);
88290 p->nChange = 0;
88298 db->nVdbeActive--;
88299 if( !p->readOnly ) db->nVdbeWrite--;
88300 if( p->bIsReader ) db->nVdbeRead--;
88301 assert( db->nVdbeActive>=db->nVdbeRead );
88302 assert( db->nVdbeRead>=db->nVdbeWrite );
88303 assert( db->nVdbeWrite>=0 );
88304 p->eVdbeState = VDBE_HALT_STATE;
88306 if( db->mallocFailed ){
88307 p->rc = SQLITE_NOMEM_BKPT;
88310 /* If the auto-commit flag is set to true, then any locks that were held
88312 ** to invoke any required unlock-notify callbacks.
88314 if( db->autoCommit ){
88318 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
88319 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
88325 ** in p->rc. This routine sets that result back to SQLITE_OK.
88328 p->rc = SQLITE_OK;
88339 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
88340 sqlite3 *db = p->db;
88341 int rc = p->rc;
88342 if( p->zErrMsg ){
88343 db->bBenignMalloc++;
88345 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
88346 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
88348 db->bBenignMalloc--;
88349 }else if( db->pErr ){
88350 sqlite3ValueSetNull(db->pErr);
88352 db->errCode = rc;
88353 db->errByteOffset = -1;
88363 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
88364 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
88365 assert( v->db->init.busy==0 );
88368 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
88370 sqlite3DbFree(v->db, zExpanded);
88389 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
88391 int i;
88395 db = p->db;
88401 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
88408 if( p->pc>=0 ){
88410 if( db->pErr || p->zErrMsg ){
88413 db->errCode = p->rc;
88422 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
88423 if( p->aMem ){
88424 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
88427 if( p->zErrMsg ){
88428 sqlite3DbFree(db, p->zErrMsg);
88429 p->zErrMsg = 0;
88431 p->pResultRow = 0;
88433 p->nWrite = 0;
88442 fprintf(out, "---- ");
88443 for(i=0; i<p->nOp; i++){
88444 fprintf(out, "%02x", p->aOp[i].opcode);
88447 if( p->zSql ){
88449 fprintf(out, "-- ");
88450 for(i=0; (c = p->zSql[i])!=0; i++){
88451 if( pc=='\n' ) fprintf(out, "-- ");
88457 for(i=0; i<p->nOp; i++){
88459 i64 cnt = p->aOp[i].nExec;
88460 i64 cycles = p->aOp[i].nCycle;
88467 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
88473 return p->rc & db->errMask;
88480 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
88481 int rc = SQLITE_OK;
88485 if( p->eVdbeState>=VDBE_READY_STATE ){
88487 assert( (rc & p->db->errMask)==rc );
88509 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
88513 || (pAux->iAuxOp==iOp
88514 && pAux->iAuxArg>=0
88515 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
88517 testcase( pAux->iAuxArg==31 );
88518 if( pAux->xDeleteAux ){
88519 pAux->xDeleteAux(pAux->pAux);
88521 *pp = pAux->pNextAux;
88524 pp= &pAux->pNextAux;
88540 assert( p->db==0 || p->db==db );
88541 if( p->aColName ){
88542 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
88543 sqlite3DbNNFreeNN(db, p->aColName);
88545 for(pSub=p->pProgram; pSub; pSub=pNext){
88546 pNext = pSub->pNext;
88547 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
88550 if( p->eVdbeState!=VDBE_INIT_STATE ){
88551 releaseMemArray(p->aVar, p->nVar);
88552 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
88553 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
88555 vdbeFreeOpArray(db, p->aOp, p->nOp);
88556 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
88558 sqlite3DbFree(db, p->zNormSql);
88561 for(pThis=p->pDblStr; pThis; pThis=pNxt){
88562 pNxt = pThis->pNextStr;
88569 int i;
88570 for(i=0; i<p->nScan; i++){
88571 sqlite3DbFree(db, p->aScan[i].zName);
88573 sqlite3DbFree(db, p->aScan);
88585 db = p->db;
88587 assert( sqlite3_mutex_held(db->mutex) );
88589 if( db->pnBytesFreed==0 ){
88590 assert( p->ppVPrev!=0 );
88591 *p->ppVPrev = p->pVNext;
88592 if( p->pVNext ){
88593 p->pVNext->ppVPrev = p->ppVPrev;
88604 SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
88605 int res, rc;
88607 extern int sqlite3_search_count;
88609 assert( p->deferredMoveto );
88610 assert( p->isTable );
88611 assert( p->eCurType==CURTYPE_BTREE );
88612 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
88618 p->deferredMoveto = 0;
88619 p->cacheStatus = CACHE_STALE;
88630 SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
88631 int isDifferentRow, rc;
88632 assert( p->eCurType==CURTYPE_BTREE );
88633 assert( p->uc.pCursor!=0 );
88634 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
88635 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
88636 p->cacheStatus = CACHE_STALE;
88637 if( isDifferentRow ) p->nullRow = 1;
88645 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
88646 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
88647 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
88659 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
88664 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
88671 ** serial-type and data blob separately.
88676 ** -------------- --------------- ---------------
88688 ** N>=12 and even (N-12)/2 BLOB
88689 ** N>=13 and odd (N-13)/2 text
88697 ** Return the serial-type for the value stored in pMem.
88701 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
88702 ** opcode in the byte-code engine. But by moving this routine in-line, we
88707 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
88708 int flags = pMem->flags;
88718 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
88719 i64 i = pMem->u.i;
88744 ** as an integer, then we might as well make it an 8-byte floating
88746 pMem->u.r = (double)pMem->u.i;
88747 pMem->flags &= ~MEM_IntReal;
88748 pMem->flags |= MEM_Real;
88757 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
88758 assert( pMem->n>=0 );
88759 n = (u32)pMem->n;
88761 n += pMem->u.nZero;
88789 ** Return the length of the data corresponding to the supplied serial-type.
88793 return (serial_type-12)/2;
88796 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
88806 ** If we are on an architecture with mixed-endian floating
88810 ** For most architectures, this is a no-op.
88812 ** (later): It is reported to me that the mixed-endian problem
88814 ** that early versions of GCC stored the two words of a 64-bit
88822 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
88826 ** (2007-08-30) Frank van Vugt has studied this problem closely
88829 ** emulation that uses only 32-bit mantissas instead of a full
88830 ** 48-bits as required by the IEEE standard. (This is the
88833 ** the necessary byte swapping is carried out using a 64-bit integer
88834 ** rather than a 64-bit float. Frank assures us that the code here
88857 ** big-endian integer. Return the equivalent native integer
88870 ** The few cases that require local variables are broken out into a separate
88883 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
88884 ** twos-complement integer. */
88885 pMem->u.i = *(i64*)&x;
88886 pMem->flags = MEM_Int;
88887 testcase( pMem->u.i<0 );
88889 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
88894 ** defined that 64-bit floating point values really are mixed
88903 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88905 memcpy(&pMem->u.r, &x, sizeof(x));
88906 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
88909 static int serialGet7(
88916 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88918 memcpy(&pMem->u.r, &x, sizeof(x));
88920 pMem->flags = MEM_Null;
88923 pMem->flags = MEM_Real;
88933 ** UPDATE no-change flag set */
88934 pMem->flags = MEM_Null|MEM_Zero;
88935 pMem->n = 0;
88936 pMem->u.nZero = 0;
88941 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
88942 pMem->flags = MEM_Null;
88946 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
88948 pMem->u.i = ONE_BYTE_INT(buf);
88949 pMem->flags = MEM_Int;
88950 testcase( pMem->u.i<0 );
88953 case 2: { /* 2-byte signed integer */
88954 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
88955 ** twos-complement integer. */
88956 pMem->u.i = TWO_BYTE_INT(buf);
88957 pMem->flags = MEM_Int;
88958 testcase( pMem->u.i<0 );
88961 case 3: { /* 3-byte signed integer */
88962 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
88963 ** twos-complement integer. */
88964 pMem->u.i = THREE_BYTE_INT(buf);
88965 pMem->flags = MEM_Int;
88966 testcase( pMem->u.i<0 );
88969 case 4: { /* 4-byte signed integer */
88970 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
88971 ** twos-complement integer. */
88972 pMem->u.i = FOUR_BYTE_INT(buf);
88974 /* Work around a sign-extension bug in the HP compiler for HP/UX */
88975 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
88977 pMem->flags = MEM_Int;
88978 testcase( pMem->u.i<0 );
88981 case 5: { /* 6-byte signed integer */
88982 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
88983 ** twos-complement integer. */
88984 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
88985 pMem->flags = MEM_Int;
88986 testcase( pMem->u.i<0 );
88989 case 6: /* 8-byte signed integer */
88998 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
88999 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
89000 pMem->u.i = serial_type-8;
89001 pMem->flags = MEM_Int;
89005 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
89007 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
89008 ** (N-13)/2 bytes in length. */
89010 pMem->z = (char *)buf;
89011 pMem->n = (serial_type-12)/2;
89012 pMem->flags = aFlag[serial_type&1];
89036 int nByte; /* Number of bytes required for *p */
89037 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
89038 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
89040 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
89041 assert( pKeyInfo->aSortFlags!=0 );
89042 p->pKeyInfo = pKeyInfo;
89043 p->nField = pKeyInfo->nKeyField + 1;
89048 ** Given the nKey-byte encoding of a record in pKey[], populate the
89054 int nKey, /* Size of the binary record */
89063 Mem *pMem = p->aMem;
89065 p->default_rc = 0;
89074 pMem->enc = pKeyInfo->enc;
89075 pMem->db = pKeyInfo->db;
89076 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
89077 pMem->szMalloc = 0;
89078 pMem->z = 0;
89082 if( (++u)>=p->nField ) break;
89089 sqlite3VdbeMemSetNull(pMem-1);
89091 assert( u<=pKeyInfo->nKeyField + 1 );
89092 p->nField = u;
89107 static int vdbeRecordCompareDebug(
89108 int nKey1, const void *pKey1, /* Left key */
89110 int desiredResult /* Correct answer */
89115 int i = 0;
89116 int rc = 0;
89121 pKeyInfo = pPKey2->pKeyInfo;
89122 if( pKeyInfo->db==0 ) return 1;
89123 mem1.enc = pKeyInfo->enc;
89124 mem1.db = pKeyInfo->db;
89140 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
89141 assert( pKeyInfo->aSortFlags!=0 );
89142 assert( pKeyInfo->nKeyField>0 );
89178 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
89179 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
89182 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
89183 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
89185 rc = -rc;
89187 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
89188 rc = -rc; /* Invert the result for DESC sort order. */
89193 }while( idx1<szHdr1 && i<pPKey2->nField );
89204 rc = pPKey2->default_rc;
89211 if( pKeyInfo->db->mallocFailed ) return 1;
89220 ** limit given by pKeyInfo->nAllField.
89222 ** If this constraint is not satisfied, it means that the high-speed
89229 int nKey, const void *pKey, /* The record to verify */
89232 int nField = 0;
89246 assert( nField <= pKeyInfo->nAllField );
89256 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
89258 static int vdbeCompareMemString(
89264 if( pMem1->enc==pColl->enc ){
89267 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
89269 int rc;
89273 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
89274 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
89277 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
89278 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
89283 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
89293 ** with MEM_Zero. Return true if it could be a zero-blob.
89295 static int isAllZero(const char *z, int n){
89296 int i;
89308 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
89309 int c;
89310 int n1 = pB1->n;
89311 int n2 = pB2->n;
89313 /* It is possible to have a Blob value that has some non-zero content
89317 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
89318 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
89320 if( (pB1->flags|pB2->flags) & MEM_Zero ){
89321 if( pB1->flags & pB2->flags & MEM_Zero ){
89322 return pB1->u.nZero - pB2->u.nZero;
89323 }else if( pB1->flags & MEM_Zero ){
89324 if( !isAllZero(pB2->z, pB2->n) ) return -1;
89325 return pB1->u.nZero - n2;
89327 if( !isAllZero(pB1->z, pB1->n) ) return +1;
89328 return n1 - pB2->u.nZero;
89331 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
89333 return n1 - n2;
89342 static int SQLITE_NOINLINE doubleLt(double a, double b){ return a<b; }
89343 static int SQLITE_NOINLINE doubleEq(double a, double b){ return a==b; }
89347 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
89351 SQLITE_PRIVATE int sqlite3IntFloatCompare(i64 i, double r){
89362 return (x<r) ? -1 : (x>r);
89365 if( r<-9223372036854775808.0 ) return +1;
89366 if( r>=9223372036854775808.0 ) return -1;
89368 if( i<y ) return -1;
89373 return (((double)i)<r) ? -1 : (((double)i)>r);
89386 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
89387 int f1, f2;
89388 int combined_flags;
89390 f1 = pMem1->flags;
89391 f2 = pMem2->flags;
89399 return (f2&MEM_Null) - (f1&MEM_Null);
89411 if( pMem1->u.i < pMem2->u.i ) return -1;
89412 if( pMem1->u.i > pMem2->u.i ) return +1;
89416 if( pMem1->u.r < pMem2->u.r ) return -1;
89417 if( pMem1->u.r > pMem2->u.r ) return +1;
89424 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
89426 if( pMem1->u.i < pMem2->u.i ) return -1;
89427 if( pMem1->u.i > pMem2->u.i ) return +1;
89430 return -1;
89437 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
89439 return -1;
89453 return -1;
89456 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
89457 assert( pMem1->enc==SQLITE_UTF8 ||
89458 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
89464 assert( !pColl || pColl->xCmp );
89479 ** The first argument passed to this function is a serial-type that
89480 ** corresponds to an integer - all values between 1 and 9 inclusive
89502 return (i64)*(int*)&y;
89516 return (serial_type - 8);
89528 ** If argument bSkip is non-zero, it is assumed that the caller has already
89532 ** fields that appear in both keys are equal, then pPKey2->default_rc is
89535 ** If database corruption is discovered, set pPKey2->errCode to
89537 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
89538 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
89540 SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
89541 int nKey1, const void *pKey1, /* Left key */
89543 int bSkip /* If true, skip the first field */
89546 int i; /* Index of next field to compare */
89549 int rc = 0; /* Return value */
89550 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
89579 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89584 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
89586 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
89587 assert( pPKey2->pKeyInfo->nKeyField>0 );
89589 while( 1 /*exit-by-break*/ ){
89593 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
89594 testcase( pRhs->flags & MEM_Int );
89595 testcase( pRhs->flags & MEM_IntReal );
89599 rc = serial_type==10 ? -1 : +1;
89601 rc = -1;
89604 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
89607 i64 rhs = pRhs->u.i;
89609 rc = -1;
89617 else if( pRhs->flags & MEM_Real ){
89624 rc = serial_type==10 ? -1 : +1;
89626 rc = -1;
89630 rc = -1; /* mem1 is a NaN */
89631 }else if( mem1.u.r<pRhs->u.r ){
89632 rc = -1;
89633 }else if( mem1.u.r>pRhs->u.r ){
89640 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
89646 else if( pRhs->flags & MEM_Str ){
89650 rc = -1;
89654 mem1.n = (serial_type - 12) / 2;
89658 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
89660 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89662 }else if( pKeyInfo->aColl[i] ){
89663 mem1.enc = pKeyInfo->enc;
89664 mem1.db = pKeyInfo->db;
89668 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
89671 int nCmp = MIN(mem1.n, pRhs->n);
89672 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89673 if( rc==0 ) rc = mem1.n - pRhs->n;
89679 else if( pRhs->flags & MEM_Blob ){
89680 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
89684 rc = -1;
89686 int nStr = (serial_type - 12) / 2;
89690 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89692 }else if( pRhs->flags & MEM_Zero ){
89696 rc = nStr - pRhs->u.nZero;
89699 int nCmp = MIN(nStr, pRhs->n);
89700 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89701 if( rc==0 ) rc = nStr - pRhs->n;
89720 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
89724 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
89726 rc = -rc;
89735 if( i==pPKey2->nField ) break;
89741 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89755 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
89756 || pPKey2->pKeyInfo->db->mallocFailed
89758 pPKey2->eqSeen = 1;
89759 return pPKey2->default_rc;
89761 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
89762 int nKey1, const void *pKey1, /* Left key */
89772 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
89778 static int vdbeRecordCompareInt(
89779 int nKey1, const void *pKey1, /* Left key */
89783 int serial_type = ((const u8*)pKey1)[1];
89784 int res;
89790 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89793 case 1: { /* 1-byte signed integer */
89798 case 2: { /* 2-byte signed integer */
89803 case 3: { /* 3-byte signed integer */
89808 case 4: { /* 4-byte signed integer */
89810 lhs = (i64)*(int*)&y;
89814 case 5: { /* 6-byte signed integer */
89819 case 6: { /* 8-byte signed integer */
89846 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
89847 v = pPKey2->u.i;
89849 res = pPKey2->r1;
89851 res = pPKey2->r2;
89852 }else if( pPKey2->nField>1 ){
89858 ** fields. Return pPKey2->default_rc in this case. */
89859 res = pPKey2->default_rc;
89860 pPKey2->eqSeen = 1;
89870 ** uses the collation sequence BINARY and (c) that the size-of-header varint
89873 static int vdbeRecordCompareString(
89874 int nKey1, const void *pKey1, /* Left key */
89878 int serial_type;
89879 int res;
89881 assert( pPKey2->aMem[0].flags & MEM_Str );
89882 assert( pPKey2->aMem[0].n == pPKey2->n );
89883 assert( pPKey2->aMem[0].z == pPKey2->u.z );
89884 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89894 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
89896 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
89898 int nCmp;
89899 int nStr;
89900 int szHdr = aKey1[0];
89902 nStr = (serial_type-12) / 2;
89904 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89907 nCmp = MIN( pPKey2->n, nStr );
89908 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
89911 res = pPKey2->r2;
89913 res = pPKey2->r1;
89915 res = nStr - pPKey2->n;
89917 if( pPKey2->nField>1 ){
89920 res = pPKey2->default_rc;
89921 pPKey2->eqSeen = 1;
89924 res = pPKey2->r2;
89926 res = pPKey2->r1;
89933 || pPKey2->pKeyInfo->db->mallocFailed
89945 ** that the size-of-header varint that occurs at the start of each record
89957 if( p->pKeyInfo->nAllField<=13 ){
89958 int flags = p->aMem[0].flags;
89959 if( p->pKeyInfo->aSortFlags[0] ){
89960 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
89963 p->r1 = 1;
89964 p->r2 = -1;
89966 p->r1 = -1;
89967 p->r2 = 1;
89970 p->u.i = p->aMem[0].u.i;
89977 && p->pKeyInfo->aColl[0]==0
89980 p->u.z = p->aMem[0].z;
89981 p->n = p->aMem[0].n;
89997 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
89999 int rc;
90006 ** than 2GiB are support - anything large must be database corruption.
90008 ** this code can safely assume that nCellKey is 32-bits
90031 /* The last field of the index should be an integer - the ROWID.
90033 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
90052 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
90076 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
90080 int *res /* Write the comparison result here */
90083 int rc;
90087 assert( pC->eCurType==CURTYPE_BTREE );
90088 pCur = pC->uc.pCursor;
90112 assert( sqlite3_mutex_held(db->mutex) );
90113 db->nChange = nChange;
90114 db->nTotalChange += nChange;
90122 v->changeCntOn = 1;
90131 ** programs obsolete. Removing user-defined functions or collating
90143 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
90145 for(p = db->pVdbe; p; p=p->pVNext){
90146 p->expired = iCode+1;
90154 return v->db;
90161 return v->prepFlags;
90172 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
90175 Mem *pMem = &v->aVar[iVar-1];
90176 assert( (v->db->flags & SQLITE_EnableQPSG)==0
90177 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90178 if( 0==(pMem->flags & MEM_Null) ){
90179 sqlite3_value *pRet = sqlite3ValueNew(v->db);
90192 ** to sqlite3_reoptimize() that re-preparing the statement may result
90195 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
90197 assert( (v->db->flags & SQLITE_EnableQPSG)==0
90198 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90200 v->expmask |= 0x80000000;
90202 v->expmask |= ((u32)1 << (iVar-1));
90211 ** throw an error if it is given inputs that would make it non-deterministic.
90212 ** This routine is invoked by date/time functions that use non-deterministic
90215 SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
90218 if( pCtx->pVdbe==0 ) return 1;
90220 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
90221 if( pOp->opcode==OP_PureFunc ){
90224 if( pOp->p5 & NC_IsCheck ){
90226 }else if( pOp->p5 & NC_GenCol ){
90231 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
90232 pCtx->pFunc->zName, zContext);
90233 sqlite3_result_error(pCtx, zMsg, -1);
90244 ** byte-code register values correctly initialized.
90246 SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){
90247 if( pExpr->op==TK_REGISTER ){
90248 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
90261 if( pVtab->zErrMsg ){
90262 sqlite3 *db = p->db;
90263 sqlite3DbFree(db, p->zErrMsg);
90264 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
90265 sqlite3_free(pVtab->zErrMsg);
90266 pVtab->zErrMsg = 0;
90275 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
90281 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
90284 int i;
90286 Mem *pMem = &p->aMem[i];
90287 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
90296 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
90302 Vdbe *v, /* Vdbe pre-update hook is invoked by */
90304 int op, /* SQLITE_INSERT, UPDATE or DELETE */
90308 int iReg, /* Register for new.* record */
90309 int iBlobWrite
90311 sqlite3 *db = v->db;
90314 const char *zTbl = pTab->zName;
90317 int nRealCol;
90318 if( pTab->tabFlags & TF_WithoutRowid ){
90319 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
90320 }else if( pTab->tabFlags & TF_HasVirtual ){
90321 nRealCol = pTab->nNVCol;
90323 nRealCol = pTab->nCol;
90327 assert( db->pPreUpdate==0 );
90334 iKey2 = v->aMem[iReg].u.i;
90341 assert( pCsr->eCurType==CURTYPE_BTREE );
90342 assert( pCsr->nField==nRealCol
90343 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
90352 preupdate.keyinfo.nKeyField = pTab->nCol;
90359 db->pPreUpdate = &preupdate;
90360 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
90361 db->pPreUpdate = 0;
90366 int i;
90367 for(i=0; i<pCsr->nField; i++){
90398 ** Return TRUE (non-zero) of the statement supplied as an argument needs
90405 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
90407 return p==0 || p->expired;
90416 static int vdbeSafety(Vdbe *p){
90417 if( p->db==0 ){
90424 static int vdbeSafetyNotNull(Vdbe *p){
90441 assert( p->startTime>0 );
90442 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
90443 assert( db->init.busy==0 );
90444 assert( p->zSql!=0 );
90445 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
90446 iElapse = (iNow - p->startTime)*1000000;
90448 if( db->xProfile ){
90449 db->xProfile(db->pProfileArg, p->zSql, iElapse);
90452 if( db->mTrace & SQLITE_TRACE_PROFILE ){
90453 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
90455 p->startTime = 0;
90462 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
90464 # define checkProfileCallback(DB,P) /*no-op*/
90476 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
90477 int rc;
90479 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
90480 ** pointer is a harmless no-op. */
90484 sqlite3 *db = v->db;
90486 sqlite3_mutex_enter(db->mutex);
90488 assert( v->eVdbeState>=VDBE_READY_STATE );
90505 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
90506 int rc;
90511 sqlite3 *db = v->db;
90512 sqlite3_mutex_enter(db->mutex);
90516 assert( (rc & (db->errMask))==rc );
90518 sqlite3_mutex_leave(db->mutex);
90526 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
90527 int i;
90528 int rc = SQLITE_OK;
90539 mutex = p->db->mutex;
90542 for(i=0; i<p->nVar; i++){
90543 sqlite3VdbeMemRelease(&p->aVar[i]);
90544 p->aVar[i].flags = MEM_Null;
90546 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
90547 if( p->expmask ){
90548 p->expired = 1;
90561 if( p->flags & (MEM_Blob|MEM_Str) ){
90563 assert( p->flags==MEM_Null && p->z==0 );
90566 p->flags |= MEM_Blob;
90567 return p->n ? p->z : 0;
90572 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
90575 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
90581 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
90582 return (int)sqlite3VdbeIntValue((Mem*)pVal);
90587 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
90589 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
90593 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
90596 && p->eSubtype=='p'
90597 && strcmp(p->u.zPType, zPType)==0
90599 return (void*)p->z;
90618 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
90619 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
90622 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
90691 int eType = SQLITE_BLOB;
90692 if( pVal->flags & MEM_Null ){
90694 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
90696 }else if( pVal->flags & MEM_Int ){
90698 }else if( pVal->flags & MEM_Str ){
90701 assert( eType == aType[pVal->flags&MEM_AffMask] );
90704 return aType[pVal->flags&MEM_AffMask];
90706 SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
90707 return pVal->enc;
90711 SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
90712 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
90716 SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
90717 return (pVal->flags&MEM_FromBind)!=0;
90729 pNew->flags &= ~MEM_Dyn;
90730 pNew->db = 0;
90731 if( pNew->flags&(MEM_Str|MEM_Blob) ){
90732 pNew->flags &= ~(MEM_Static|MEM_Dyn);
90733 pNew->flags |= MEM_Ephem;
90738 }else if( pNew->flags & MEM_Null ){
90740 pNew->flags &= ~(MEM_Term|MEM_Subtype);
90754 ** The following routines are used by user-defined functions to specify
90767 int n, /* Bytes in string, or negative */
90771 Mem *pOut = pCtx->pOut;
90772 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
90784 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
90789 static int invokeValueDestructor(
90815 int n,
90825 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90841 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90845 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
90852 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90853 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
90855 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
90859 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90860 pCtx->isError = SQLITE_ERROR;
90861 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
90864 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
90868 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90869 pCtx->isError = SQLITE_ERROR;
90870 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
90873 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
90877 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90878 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
90884 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90885 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
90891 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90892 sqlite3VdbeMemSetNull(pCtx->pOut);
90907 pOut = pCtx->pOut;
90908 assert( sqlite3_mutex_held(pOut->db->mutex) );
90910 pOut->flags = MEM_Null;
90913 SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
90919 if( pCtx->pFunc!=0
90920 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90925 pCtx->pFunc->zName);
90926 sqlite3_result_error(pCtx, zErr, -1);
90930 pOut = pCtx->pOut;
90931 assert( sqlite3_mutex_held(pOut->db->mutex) );
90932 pOut->eSubtype = eSubtype & 0xff;
90933 pOut->flags |= MEM_Subtype;
90938 int n,
90947 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90963 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90972 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
90973 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
90980 int n,
90983 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90989 int n,
90992 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90998 int n,
91001 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91015 pOut = pCtx->pOut;
91016 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91018 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
91023 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
91026 SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
91032 pOut = pCtx->pOut;
91033 assert( sqlite3_mutex_held(pOut->db->mutex) );
91034 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
91039 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91042 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91045 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
91049 pCtx->isError = errCode ? errCode : -1;
91051 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
91053 if( pCtx->pOut->flags & MEM_Null ){
91054 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
91064 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91065 pCtx->isError = SQLITE_TOOBIG;
91066 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
91075 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91076 sqlite3VdbeMemSetNull(pCtx->pOut);
91077 pCtx->isError = SQLITE_NOMEM_BKPT;
91078 sqlite3OomFault(pCtx->pOut->db);
91084 ** test-control.
91087 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91088 if( pCtx->pOut->flags & MEM_Int ){
91089 pCtx->pOut->flags &= ~MEM_Int;
91090 pCtx->pOut->flags |= MEM_IntReal;
91100 static int doWalCallbacks(sqlite3 *db){
91101 int rc = SQLITE_OK;
91103 int i;
91104 for(i=0; i<db->nDb; i++){
91105 Btree *pBt = db->aDb[i].pBt;
91107 int nEntry;
91111 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
91112 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
91130 static int sqlite3Step(Vdbe *p){
91132 int rc;
91135 db = p->db;
91136 if( p->eVdbeState!=VDBE_RUN_STATE ){
91138 if( p->eVdbeState==VDBE_READY_STATE ){
91139 if( p->expired ){
91140 p->rc = SQLITE_SCHEMA;
91142 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
91144 ** error has occurred, then return the error code in p->rc to the
91157 if( db->nVdbeActive==0 ){
91158 AtomicStore(&db->u1.isInterrupted, 0);
91161 assert( db->nVdbeWrite>0 || db->autoCommit==0
91162 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
91166 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
91167 && !db->init.busy && p->zSql ){
91168 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
91170 assert( p->startTime==0 );
91174 db->nVdbeActive++;
91175 if( p->readOnly==0 ) db->nVdbeWrite++;
91176 if( p->bIsReader ) db->nVdbeRead++;
91177 p->pc = 0;
91178 p->eVdbeState = VDBE_RUN_STATE;
91181 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
91186 ** This "automatic-reset" change is not technically an incompatibility,
91187 ** since any application that receives an SQLITE_MISUSE is broken by
91192 ** returns, and those were broken by the automatic-reset change. As a
91193 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
91199 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
91207 assert( p->eVdbeState==VDBE_READY_STATE );
91213 p->rcApp = SQLITE_OK;
91216 if( p->explain ){
91221 db->nVdbeExec++;
91223 db->nVdbeExec--;
91227 assert( p->rc==SQLITE_OK );
91228 assert( db->mallocFailed==0 );
91229 db->errCode = SQLITE_ROW;
91236 p->pResultRow = 0;
91237 if( rc==SQLITE_DONE && db->autoCommit ){
91238 assert( p->rc==SQLITE_OK );
91239 p->rc = doWalCallbacks(db);
91240 if( p->rc!=SQLITE_OK ){
91243 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
91245 ** error has occurred, then return the error code in p->rc to the
91252 db->errCode = rc;
91253 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
91254 p->rc = SQLITE_NOMEM_BKPT;
91255 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
91260 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
91264 return (rc&db->errMask);
91268 ** This is the top-level implementation of sqlite3_step(). Call
91272 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
91273 int rc = SQLITE_OK; /* Result from sqlite3Step() */
91275 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
91281 db = v->db;
91282 sqlite3_mutex_enter(db->mutex);
91285 int savedPc = v->pc;
91296 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
91297 sqlite3DbFree(db, v->zErrMsg);
91298 if( !db->mallocFailed ){
91299 v->zErrMsg = sqlite3DbStrDup(db, zErr);
91300 v->rc = rc = sqlite3ApiExit(db, rc);
91302 v->zErrMsg = 0;
91303 v->rc = rc = SQLITE_NOMEM_BKPT;
91312 ** SQLITE_SCHEMA. tag-20220401a */
91313 v->minWriteFileFormat = 254;
91315 assert( v->expired==0 );
91317 sqlite3_mutex_leave(db->mutex);
91330 assert( p && p->pFunc );
91331 return p->pFunc->pUserData;
91338 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
91348 assert( p && p->pOut );
91350 return p->pOut->db;
91364 ** performance by substituting a NULL result, or some other light-weight
91367 SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
91373 return sqlite3_value_nochange(p->pOut);
91391 static int valueFromValueList(
91394 int bNext /* 1 for _next(). 0 for _first() */
91396 int rc;
91401 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
91404 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
91406 assert( pVal->eSubtype=='p' );
91407 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
91408 pRhs = (ValueList*)pVal->z;
91411 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
91413 int dummy = 0;
91414 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
91415 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
91416 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
91422 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
91423 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
91427 sqlite3_value *pOut = pRhs->pOut;
91428 int iOff = 1 + getVarint32(&zBuf[1], iSerial);
91430 pOut->enc = ENC(pOut->db);
91431 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
91446 SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
91454 SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
91466 int rc;
91468 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
91469 assert( p->pVdbe!=0 );
91472 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
91475 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
91483 ** its pMem->z element.
91485 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
91486 Mem *pMem = p->pMem;
91487 assert( (pMem->flags & MEM_Agg)==0 );
91490 pMem->z = 0;
91493 pMem->flags = MEM_Agg;
91494 pMem->u.pDef = p->pFunc;
91495 if( pMem->z ){
91496 memset(pMem->z, 0, nByte);
91499 return (void*)pMem->z;
91507 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
91508 assert( p && p->pFunc && p->pFunc->xFinalize );
91509 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
91511 if( (p->pMem->flags & MEM_Agg)==0 ){
91514 return (void*)p->pMem->z;
91520 ** the user-function defined by pCtx.
91522 ** The left-most argument is 0.
91528 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
91534 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91536 if( pCtx->pVdbe==0 ) return 0;
91538 assert( pCtx->pVdbe!=0 );
91540 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91541 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91542 return pAuxData->pAux;
91550 ** argument to the user-function defined by pCtx. Any previous value is
91553 ** The left-most argument is 0.
91561 int iArg,
91571 pVdbe= pCtx->pVdbe;
91572 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91579 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91580 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91585 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
91587 pAuxData->iAuxOp = pCtx->iOp;
91588 pAuxData->iAuxArg = iArg;
91589 pAuxData->pNextAux = pVdbe->pAuxData;
91590 pVdbe->pAuxData = pAuxData;
91591 if( pCtx->isError==0 ) pCtx->isError = -1;
91592 }else if( pAuxData->xDeleteAux ){
91593 pAuxData->xDeleteAux(pAuxData->pAux);
91596 pAuxData->pAux = pAux;
91597 pAuxData->xDeleteAux = xDelete;
91616 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
91617 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
91618 return p->pMem->n;
91625 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
91628 return pVm->nResColumn;
91635 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
91637 if( pVm==0 || pVm->pResultRow==0 ) return 0;
91638 return pVm->nResColumn;
91647 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
91648 ** instead of an 8-byte one. This all works fine, except that when
91650 ** that a Mem structure is located on an 8-byte boundary. To prevent
91652 ** using gcc, we force nullMem to be 8-byte aligned using the magical
91661 /* .n = */ (int)0,
91666 /* .szMalloc = */ (int)0,
91684 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
91690 assert( pVm->db );
91691 sqlite3_mutex_enter(pVm->db->mutex);
91692 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
91693 pOut = &pVm->pResultRow[i];
91695 sqlite3Error(pVm->db, SQLITE_RANGE);
91728 assert( p->db!=0 );
91729 assert( sqlite3_mutex_held(p->db->mutex) );
91730 p->rc = sqlite3ApiExit(p->db, p->rc);
91731 sqlite3_mutex_leave(p->db->mutex);
91739 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
91749 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
91750 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
91754 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
91755 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
91759 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
91764 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
91765 int val = sqlite3_value_int( columnMem(pStmt,i) );
91769 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
91774 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
91779 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
91781 if( pOut->flags&MEM_Static ){
91782 pOut->flags &= ~MEM_Static;
91783 pOut->flags |= MEM_Ephem;
91789 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
91795 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
91796 int iType = sqlite3_value_type( columnMem(pStmt,i) );
91828 ** Convert the N-th element of pStmt->pColName[] into a string using
91845 int N, /* Which column to get the name for */
91846 int useUtf16, /* True to return the name as UTF16 */
91847 int useType /* What type of name */
91851 int n;
91862 db = p->db;
91864 sqlite3_mutex_enter(db->mutex);
91866 if( p->explain ){
91868 n = p->explain==1 ? 8 : 4;
91871 int i = iExplainColNames16[N + 8*p->explain - 8];
91874 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
91878 n = p->nResColumn;
91880 u8 prior_mallocFailed = db->mallocFailed;
91884 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
91888 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
91893 assert( db->mallocFailed==0 || db->mallocFailed==1 );
91894 if( db->mallocFailed > prior_mallocFailed ){
91900 sqlite3_mutex_leave(db->mutex);
91908 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
91912 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
91931 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
91935 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
91947 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
91951 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
91961 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
91965 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
91975 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
91979 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
91998 ** The error code stored in database p->db is overwritten with the return
92001 static int vdbeUnbind(Vdbe *p, unsigned int i){
92006 sqlite3_mutex_enter(p->db->mutex);
92007 if( p->eVdbeState!=VDBE_READY_STATE ){
92008 sqlite3Error(p->db, SQLITE_MISUSE_BKPT);
92009 sqlite3_mutex_leave(p->db->mutex);
92011 "bind on a busy prepared statement: [%s]", p->zSql);
92014 if( i>=(unsigned int)p->nVar ){
92015 sqlite3Error(p->db, SQLITE_RANGE);
92016 sqlite3_mutex_leave(p->db->mutex);
92019 pVar = &p->aVar[i];
92021 pVar->flags = MEM_Null;
92022 p->db->errCode = SQLITE_OK;
92027 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
92033 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
92034 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
92035 p->expired = 1;
92043 static int bindText(
92045 int i, /* Index of the parameter to bind */
92053 int rc;
92055 rc = vdbeUnbind(p, (u32)(i-1));
92058 pVar = &p->aVar[i-1];
92061 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
92064 sqlite3Error(p->db, rc);
92065 rc = sqlite3ApiExit(p->db, rc);
92068 sqlite3_mutex_leave(p->db->mutex);
92079 SQLITE_API int sqlite3_bind_blob(
92081 int i,
92083 int nData,
92091 SQLITE_API int sqlite3_bind_blob64(
92093 int i,
92101 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
92102 int rc;
92104 rc = vdbeUnbind(p, (u32)(i-1));
92106 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
92107 sqlite3_mutex_leave(p->db->mutex);
92111 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
92114 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
92115 int rc;
92117 rc = vdbeUnbind(p, (u32)(i-1));
92119 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
92120 sqlite3_mutex_leave(p->db->mutex);
92124 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
92125 int rc;
92127 rc = vdbeUnbind(p, (u32)(i-1));
92129 sqlite3_mutex_leave(p->db->mutex);
92133 SQLITE_API int sqlite3_bind_pointer(
92135 int i,
92140 int rc;
92142 rc = vdbeUnbind(p, (u32)(i-1));
92144 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
92145 sqlite3_mutex_leave(p->db->mutex);
92151 SQLITE_API int sqlite3_bind_text(
92153 int i,
92155 int nData,
92160 SQLITE_API int sqlite3_bind_text64(
92162 int i,
92176 SQLITE_API int sqlite3_bind_text16(
92178 int i,
92180 int n,
92186 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
92187 int rc;
92190 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
92194 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
92196 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
92201 if( pValue->flags & MEM_Zero ){
92202 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
92204 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
92209 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
92210 pValue->enc);
92220 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
92221 int rc;
92223 rc = vdbeUnbind(p, (u32)(i-1));
92226 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92228 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92230 sqlite3_mutex_leave(p->db->mutex);
92234 SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
92235 int rc;
92240 sqlite3_mutex_enter(p->db->mutex);
92241 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
92247 rc = sqlite3ApiExit(p->db, rc);
92248 sqlite3_mutex_leave(p->db->mutex);
92256 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
92258 return p ? p->nVar : 0;
92265 ** The result is always UTF-8.
92267 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
92270 return sqlite3VListNumToName(p->pVList, i);
92278 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
92280 return sqlite3VListNameToNum(p->pVList, zName, nName);
92282 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
92289 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
92292 int i;
92293 assert( pTo->db==pFrom->db );
92294 assert( pTo->nVar==pFrom->nVar );
92295 sqlite3_mutex_enter(pTo->db->mutex);
92296 for(i=0; i<pFrom->nVar; i++){
92297 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
92299 sqlite3_mutex_leave(pTo->db->mutex);
92316 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
92319 if( pFrom->nVar!=pTo->nVar ){
92322 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
92323 if( pTo->expmask ){
92324 pTo->expired = 1;
92326 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
92327 if( pFrom->expmask ){
92328 pFrom->expired = 1;
92341 return pStmt ? ((Vdbe*)pStmt)->db : 0;
92348 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
92349 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
92356 SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
92357 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
92363 SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){
92365 int rc;
92369 sqlite3_mutex_enter(v->db->mutex);
92370 if( ((int)v->explain)==eMode ){
92374 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
92376 }else if( v->eVdbeState!=VDBE_READY_STATE ){
92378 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
92380 v->explain = eMode;
92383 v->explain = eMode;
92385 v->haveEqpOps = eMode==2;
92387 if( v->explain ){
92388 v->nResColumn = 12 - 4*v->explain;
92390 v->nResColumn = v->nResAlloc;
92392 sqlite3_mutex_leave(v->db->mutex);
92399 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
92401 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
92418 sqlite3_mutex_enter(pDb->mutex);
92420 pNext = (sqlite3_stmt*)pDb->pVdbe;
92422 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
92424 sqlite3_mutex_leave(pDb->mutex);
92431 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
92436 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
92443 sqlite3 *db = pVdbe->db;
92444 sqlite3_mutex_enter(db->mutex);
92446 db->pnBytesFreed = (int*)&v;
92447 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
92448 db->lookaside.pEnd = db->lookaside.pStart;
92450 db->pnBytesFreed = 0;
92451 db->lookaside.pEnd = db->lookaside.pTrueEnd;
92452 sqlite3_mutex_leave(db->mutex);
92454 v = pVdbe->aCounter[op];
92455 if( resetFlag ) pVdbe->aCounter[op] = 0;
92457 return (int)v;
92465 return p ? p->zSql : 0;
92485 sqlite3_mutex_enter(p->db->mutex);
92487 sqlite3_mutex_leave(p->db->mutex);
92500 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
92501 sqlite3_mutex_enter(p->db->mutex);
92502 p->zNormSql = sqlite3Normalize(p, p->zSql);
92503 sqlite3_mutex_leave(p->db->mutex);
92505 return p->zNormSql;
92517 int nKey,
92524 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
92531 ** This function is called from within a pre-update callback to retrieve
92534 SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
92537 int rc = SQLITE_OK;
92544 p = db->pPreUpdate;
92546 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
92547 if( !p || p->op==SQLITE_INSERT ){
92551 if( p->pPk ){
92552 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92554 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92560 if( p->pUnpacked==0 ){
92564 assert( p->pCsr->eCurType==CURTYPE_BTREE );
92565 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92568 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92570 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92571 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
92577 p->aRecord = aRec;
92580 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
92581 if( iIdx==p->pTab->iPKey ){
92582 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
92583 }else if( iIdx>=p->pUnpacked->nField ){
92585 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
92586 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
92587 testcase( pMem->flags & MEM_Int );
92588 testcase( pMem->flags & MEM_IntReal );
92601 ** This function is called from within a pre-update callback to retrieve
92604 SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
92607 p = db!=0 ? db->pPreUpdate : 0;
92609 p = db->pPreUpdate;
92611 return (p ? p->keyinfo.nKeyField : 0);
92617 ** This function is designed to be called from within a pre-update callback
92621 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
92622 ** top-level trigger etc.).
92627 SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
92630 p = db!=0 ? db->pPreUpdate : 0;
92632 p = db->pPreUpdate;
92634 return (p ? p->v->nFrame : 0);
92640 ** This function is designed to be called from within a pre-update callback
92643 SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
92646 p = db!=0 ? db->pPreUpdate : 0;
92648 p = db->pPreUpdate;
92650 return (p ? p->iBlobWrite : -1);
92656 ** This function is called from within a pre-update callback to retrieve
92659 SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
92661 int rc = SQLITE_OK;
92669 p = db->pPreUpdate;
92670 if( !p || p->op==SQLITE_DELETE ){
92674 if( p->pPk && p->op!=SQLITE_UPDATE ){
92675 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92677 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92682 if( p->op==SQLITE_INSERT ){
92683 /* For an INSERT, memory cell p->iNewReg contains the serialized record
92685 UnpackedRecord *pUnpack = p->pNewUnpacked;
92687 Mem *pData = &p->v->aMem[p->iNewReg];
92690 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
92695 p->pNewUnpacked = pUnpack;
92697 pMem = &pUnpack->aMem[iIdx];
92698 if( iIdx==p->pTab->iPKey ){
92699 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92700 }else if( iIdx>=pUnpack->nField ){
92704 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
92709 assert( p->op==SQLITE_UPDATE );
92710 if( !p->aNew ){
92711 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
92712 if( !p->aNew ){
92717 assert( iIdx>=0 && iIdx<p->pCsr->nField );
92718 pMem = &p->aNew[iIdx];
92719 if( pMem->flags==0 ){
92720 if( iIdx==p->pTab->iPKey ){
92721 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92723 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
92740 SQLITE_API int sqlite3_stmt_scanstatus_v2(
92742 int iScan, /* Index of loop to report on */
92743 int iScanStatusOp, /* Which metric to return */
92744 int flags,
92749 int nOp;
92751 int idx;
92760 aOp = p->aOp;
92761 nOp = p->nOp;
92762 if( p->pFrame ){
92764 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
92765 aOp = pFrame->aOp;
92766 nOp = pFrame->nOp;
92770 int ii;
92786 for(idx=0; idx<p->nScan; idx++){
92787 pScan = &p->aScan[idx];
92788 if( pScan->zName ){
92789 iScan--;
92794 if( idx>=p->nScan ) return 1;
92795 assert( pScan==0 || pScan==&p->aScan[idx] );
92796 pScan = &p->aScan[idx];
92800 if( pScan->addrLoop>0 ){
92801 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
92803 *(sqlite3_int64*)pOut = -1;
92808 if( pScan->addrVisit>0 ){
92809 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
92811 *(sqlite3_int64*)pOut = -1;
92817 LogEst x = pScan->nEst;
92826 *(const char**)pOut = pScan->zName;
92830 if( pScan->addrExplain ){
92831 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
92838 if( pScan->addrExplain ){
92839 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
92841 *(int*)pOut = -1;
92846 if( pScan->addrExplain ){
92847 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
92849 *(int*)pOut = -1;
92855 if( pScan->aAddrRange[0]==0 ){
92856 res = -1;
92858 int ii;
92859 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
92860 int iIns = pScan->aAddrRange[ii];
92861 int iEnd = pScan->aAddrRange[ii+1];
92869 int iOp;
92872 if( pOp->p1!=iEnd ) continue;
92873 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
92894 SQLITE_API int sqlite3_stmt_scanstatus(
92896 int iScan, /* Index of loop to report on */
92897 int iScanStatusOp, /* Which metric to return */
92908 int ii;
92909 for(ii=0; p!=0 && ii<p->nOp; ii++){
92910 Op *pOp = &p->aOp[ii];
92911 pOp->nExec = 0;
92912 pOp->nCycle = 0;
92934 ** The Vdbe parse-tree explainer is also found here.
92942 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
92947 static int findNextHostParameter(const char *zSql, int *pnToken){
92948 int tokenType;
92949 int nTotal = 0;
92950 int n;
92967 ** This function returns a pointer to a nul-terminated string in memory
92971 ** then the returned string holds a copy of zRawSql with "-- " prepended
92977 ** with large (multi-megabyte) strings and blobs.
92987 ** parameter index is known, locate the value in p->aVar[]. Then render
92995 int idx = 0; /* Index of a host parameter */
92996 int nextIndex = 1; /* Index of next ? host parameter */
92997 int n; /* Length of a token prefix */
92998 int nToken; /* Length of the parameter token */
92999 int i; /* Loop counter */
93006 db = p->db;
93007 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
93008 if( db->nVdbeExec>1 ){
93012 sqlite3_str_append(&out, "-- ", 3);
93013 assert( (zRawSql - zStart) > 0 );
93014 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
93016 }else if( p->nVar==0 ){
93045 assert( idx>0 && idx<=p->nVar );
93046 pVar = &p->aVar[idx-1];
93047 if( pVar->flags & MEM_Null ){
93049 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
93050 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
93051 }else if( pVar->flags & MEM_Real ){
93052 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
93053 }else if( pVar->flags & MEM_Str ){
93054 int nOut; /* Number of bytes of the string text to include in output */
93060 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
93068 nOut = pVar->n;
93072 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
93075 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
93077 if( nOut<pVar->n ){
93078 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93084 }else if( pVar->flags & MEM_Zero ){
93085 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
93087 int nOut; /* Number of bytes of the blob to include in output */
93088 assert( pVar->flags & MEM_Blob );
93090 nOut = pVar->n;
93095 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
93099 if( nOut<pVar->n ){
93100 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93160 SQLITE_API int sqlite3_search_count = 0;
93172 SQLITE_API int sqlite3_interrupt_count = 0;
93183 SQLITE_API int sqlite3_sort_count = 0;
93189 ** use this information to make sure that the zero-blob functionality
93194 SQLITE_API int sqlite3_max_blobsize = 0;
93196 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
93197 sqlite3_max_blobsize = p->n;
93207 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
93209 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
93220 SQLITE_API int sqlite3_found_count = 0;
93247 static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
93265 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
93266 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
93267 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
93269 ** In other words, if M is 2, then I is either 0 (for fall-through) or
93271 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
93282 ** always taken, the flags should be 0x05 since the fall-through and
93284 ** flags should be 0x06 since only the fall-through approach is allowed.
93287 ** interested in equal or not-equal. In other words, I==0 and I==2
93292 ** should be no-ops except for special builds used to measure test coverage.
93297 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
93301 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
93313 ** iSrcLine - the line number of the VdbeCoverage() macro, with
93315 ** I - Mask of bits 0x07 indicating which cases are are
93317 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
93322 ** M - Type of jump. Same as M argument above
93347 if( ((P)->flags&MEM_Ephem)!=0 \
93351 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
93359 int iCur, /* Index of the new VdbeCursor */
93360 int nField, /* Number of fields in the table or index */
93378 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
93379 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
93381 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
93383 int nByte;
93389 assert( iCur>=0 && iCur<p->nCursor );
93390 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
93391 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
93392 p->apCsr[iCur] = 0;
93397 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
93398 ** to hold cursors, it is faster to in-line the logic. */
93399 assert( pMem->flags==MEM_Undefined );
93400 assert( (pMem->flags & MEM_Dyn)==0 );
93401 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
93402 if( pMem->szMalloc<nByte ){
93403 if( pMem->szMalloc>0 ){
93404 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
93406 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
93407 if( pMem->zMalloc==0 ){
93408 pMem->szMalloc = 0;
93411 pMem->szMalloc = nByte;
93414 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
93416 pCx->eCurType = eCurType;
93417 pCx->nField = nField;
93418 pCx->aOffset = &pCx->aType[nField];
93420 pCx->uc.pCursor = (BtCursor*)
93421 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
93422 sqlite3BtreeCursorZero(pCx->uc.pCursor);
93433 static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
93440 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
93458 static void applyNumericAffinity(Mem *pRec, int bTryForInt){
93460 u8 enc = pRec->enc;
93461 int rc;
93462 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
93463 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
93465 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
93466 pRec->flags |= MEM_Int;
93468 pRec->u.r = rValue;
93469 pRec->flags |= MEM_Real;
93472 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
93475 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
93476 pRec->flags &= ~MEM_Str;
93486 ** floating-point representation if an integer representation
93500 ** No-op. pRec is unchanged.
93510 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
93511 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
93512 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
93523 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
93524 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
93525 testcase( pRec->flags & MEM_Int );
93526 testcase( pRec->flags & MEM_Real );
93527 testcase( pRec->flags & MEM_IntReal );
93531 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
93541 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
93542 int eType = sqlite3_value_type(pVal);
93566 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
93570 int rc;
93572 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
93573 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
93575 pMem->u.i = 0;
93578 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
93580 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
93581 pMem->u.i = ix;
93586 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
93587 pMem->u.i = ix;
93597 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
93598 ** But it does set pMem->u.r and pMem->u.i appropriately.
93601 assert( (pMem->flags & MEM_Null)==0
93602 || pMem->db==0 || pMem->db->mallocFailed );
93603 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
93604 testcase( pMem->flags & MEM_Int );
93605 testcase( pMem->flags & MEM_Real );
93606 testcase( pMem->flags & MEM_IntReal );
93607 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
93609 assert( pMem->flags & (MEM_Str|MEM_Blob) );
93610 testcase( pMem->flags & MEM_Str );
93611 testcase( pMem->flags & MEM_Blob );
93622 int f = pMem->flags;
93625 int i;
93640 for(i=0; i<25 && i<pMem->n; i++){
93641 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
93644 for(i=0; i<25 && i<pMem->n; i++){
93645 char z = pMem->z[i];
93650 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
93653 int j;
93667 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
93668 for(j=0; j<25 && j<pMem->n; j++){
93669 c = pMem->z[j];
93672 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
93674 sqlite3_str_appendf(pStr, "(0-term)");
93685 if( p->flags & MEM_Undefined ){
93687 }else if( p->flags & MEM_Null ){
93688 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
93689 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
93690 printf(" si:%lld", p->u.i);
93691 }else if( (p->flags & (MEM_IntReal))!=0 ){
93692 printf(" ir:%lld", p->u.i);
93693 }else if( p->flags & MEM_Int ){
93694 printf(" i:%lld", p->u.i);
93696 }else if( p->flags & MEM_Real ){
93697 printf(" r:%.17g", p->u.r);
93708 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
93710 static void registerTrace(int iReg, Mem *p){
93713 if( p->pScopyFrom ){
93714 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
93732 int i;
93733 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
93739 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
93748 ** the number of non-transaction savepoints currently in the
93755 static int checkSavepointCount(sqlite3 *db){
93756 int n = 0;
93758 for(p=db->pSavepoint; p; p=p->pNext) n++;
93759 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
93765 ** Return the register of pOp->p2 after first preparing it to be
93770 pOut->flags = MEM_Int;
93775 assert( pOp->p2>0 );
93776 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
93777 pOut = &p->aMem[pOp->p2];
93779 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
93782 pOut->flags = MEM_Int;
93788 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
93789 ** with pOp->p3. Return the hash.
93792 int i, mx;
93795 assert( pOp->p4type==P4_INT32 );
93796 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
93798 if( p->flags & (MEM_Int|MEM_IntReal) ){
93799 h += p->u.i;
93800 }else if( p->flags & MEM_Real ){
93802 }else if( p->flags & (MEM_Str|MEM_Blob) ){
93806 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
93820 static SQLITE_NOINLINE int vdbeColumnFromOverflow(
93822 int iCol, /* The column to read */
93823 int t, /* The serial-type code for the column value */
93829 int rc;
93830 sqlite3 *db = pDest->db;
93831 int encoding = pDest->enc;
93832 int len = sqlite3VdbeSerialTypeLen(t);
93833 assert( pC->eCurType==CURTYPE_BTREE );
93834 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG;
93835 if( len > 4000 && pC->pKeyInfo==0 ){
93842 ** Only do this on table-btrees so that writes to index-btrees do not
93848 if( pC->colCache==0 ){
93849 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
93850 if( pC->pCache==0 ) return SQLITE_NOMEM;
93851 pC->colCache = 1;
93853 pCache = pC->pCache;
93854 if( pCache->pCValue==0
93855 || pCache->iCol!=iCol
93856 || pCache->cacheStatus!=cacheStatus
93857 || pCache->colCacheCtr!=colCacheCtr
93858 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
93860 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
93861 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
93863 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
93868 pCache->iCol = iCol;
93869 pCache->cacheStatus = cacheStatus;
93870 pCache->colCacheCtr = colCacheCtr;
93871 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
93873 pBuf = pCache->pCValue;
93880 pDest->flags |= MEM_Term;
93886 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
93888 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
93890 pDest->z[len] = 0;
93891 pDest->flags |= MEM_Term;
93894 pDest->flags &= ~MEM_Ephem;
93904 /* SQLITE_INTEGER */ "INT",
93910 return azTypes[sqlite3_value_type(pMem)-1];
93917 SQLITE_PRIVATE int sqlite3VdbeExec(
93920 Op *aOp = p->aOp; /* Copy of p->aOp */
93924 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
93927 int rc = SQLITE_OK; /* Value to return */
93928 sqlite3 *db = p->db; /* The database */
93931 int iCompare = 0; /* Result of last comparison */
93936 Mem *aMem = p->aMem; /* Copy of p->aMem */
93944 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0;
93948 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
93949 if( DbMaskNonZero(p->lockMask) ){
93953 if( db->xProgress ){
93954 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
93955 assert( 0 < db->nProgressOps );
93956 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
93961 if( p->rc==SQLITE_NOMEM ){
93966 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
93967 testcase( p->rc!=SQLITE_OK );
93968 p->rc = SQLITE_OK;
93969 assert( p->bIsReader || p->readOnly!=0 );
93970 p->iCurrentTime = 0;
93971 assert( p->explain==0 );
93972 db->busyHandler.nBusy = 0;
93973 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
93977 if( p->pc==0
93978 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
93980 int i;
93981 int once = 1;
93983 if( p->db->flags & SQLITE_VdbeListing ){
93985 for(i=0; i<p->nOp; i++){
93989 if( p->db->flags & SQLITE_VdbeEQP ){
93990 for(i=0; i<p->nOp; i++){
93998 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
94002 for(pOp=&aOp[p->pc]; 1; pOp++){
94007 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
94011 pOp->nExec++;
94012 pnCycle = &pOp->nCycle;
94013 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
94016 pOp->nExec++;
94017 pnCycle = &pOp->nCycle;
94018 *pnCycle -= sqlite3Hwtime();
94025 if( db->flags & SQLITE_VdbeTrace ){
94026 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
94027 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
94037 sqlite3_interrupt_count--;
94047 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
94049 assert( pOp->p1>0 );
94050 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
94051 assert( memIsValid(&aMem[pOp->p1]) );
94052 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
94053 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
94056 assert( pOp->p2>0 );
94057 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
94058 assert( memIsValid(&aMem[pOp->p2]) );
94059 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
94060 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
94063 assert( pOp->p3>0 );
94064 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94065 assert( memIsValid(&aMem[pOp->p3]) );
94066 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
94067 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
94070 assert( pOp->p2>0 );
94071 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
94072 memAboutToChange(p, &aMem[pOp->p2]);
94075 assert( pOp->p3>0 );
94076 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94077 memAboutToChange(p, &aMem[pOp->p3]);
94085 switch( pOp->opcode ){
94092 ** the switch statement will break with convention and be flush-left. Another
94130 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
94140 if( pOp->p5 ){
94141 assert( pOp->p2 < (int)(pOp - aOp) );
94142 assert( pOp->p2 > 1 );
94143 pOp = &aOp[pOp->p2 - 2];
94150 pOp = &aOp[pOp->p2 - 1];
94161 ** faster according to "valgrind --tool=cachegrind" */
94163 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
94168 ** If the progress callback returns non-zero, exit the virtual machine with
94171 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
94172 assert( db->nProgressOps!=0 );
94173 nProgressLimit += db->nProgressOps;
94174 if( db->xProgress(db->pProgressArg) ){
94191 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94192 pIn1 = &aMem[pOp->p1];
94195 pIn1->flags = MEM_Int;
94196 pIn1->u.i = (int)(pOp-aOp);
94197 REGISTER_TRACE(pOp->p1, pIn1);
94208 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
94215 ** P2 is not used by the byte-code engine. However, if P2 is positive
94220 ** value is a byte-code indentation hint. See tag-20220407a in
94224 pIn1 = &aMem[pOp->p1];
94225 if( pIn1->flags & MEM_Int ){
94226 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
94227 pOp = &aOp[pIn1->u.i];
94228 }else if( ALWAYS(pOp->p3) ){
94246 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94247 assert( pOp->p2>=0 && pOp->p2<p->nOp );
94248 assert( pOp->p3>=0 && pOp->p3<p->nOp );
94249 pOut = &aMem[pOp->p1];
94251 pOut->u.i = pOp->p3 - 1;
94252 pOut->flags = MEM_Int;
94253 if( pOp->p2==0 ) break;
94258 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
94259 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
94260 pOp = &aOp[pOp->p2 - 1];
94276 pIn1 = &aMem[pOp->p1];
94277 assert( pIn1->flags==MEM_Int );
94278 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
94279 pCaller = &aOp[pIn1->u.i];
94280 assert( pCaller->opcode==OP_Yield );
94281 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
94282 pIn1->u.i = (int)(pOp - p->aOp) - 1;
94283 pOp = &aOp[pCaller->p2 - 1];
94301 int pcDest;
94302 pIn1 = &aMem[pOp->p1];
94304 pIn1->flags = MEM_Int;
94305 pcDest = (int)pIn1->u.i;
94306 pIn1->u.i = (int)(pOp - aOp);
94307 REGISTER_TRACE(pOp->p1, pIn1);
94317 ** value in register P3 is not NULL, then this routine is a no-op.
94321 pIn3 = &aMem[pOp->p3];
94323 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
94325 if( (pIn3->flags & MEM_Null)==0 ) break;
94362 int pcx;
94365 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
94371 assert( pOp->p1!=SQLITE_INTERNAL );
94373 if( p->pFrame && pOp->p1==SQLITE_OK ){
94374 /* Halt the sub-program. Return control to the parent frame. */
94375 pFrame = p->pFrame;
94376 p->pFrame = pFrame->pParent;
94377 p->nFrame--;
94378 sqlite3VdbeSetChanges(db, p->nChange);
94380 if( pOp->p2==OE_Ignore ){
94381 /* Instruction pcx is the OP_Program that invoked the sub-program
94383 ** instruction is set to OE_Ignore, then the sub-program is throwing
94386 pcx = p->aOp[pcx].p2-1;
94388 aOp = p->aOp;
94389 aMem = p->aMem;
94393 p->rc = pOp->p1;
94394 p->errorAction = (u8)pOp->p2;
94395 assert( pOp->p5<=4 );
94396 if( p->rc ){
94397 if( pOp->p5 ){
94400 testcase( pOp->p5==1 );
94401 testcase( pOp->p5==2 );
94402 testcase( pOp->p5==3 );
94403 testcase( pOp->p5==4 );
94404 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
94405 if( pOp->p4.z ){
94406 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
94409 sqlite3VdbeError(p, "%s", pOp->p4.z);
94411 pcx = (int)(pOp - aOp);
94412 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
94417 p->rc = SQLITE_BUSY;
94419 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
94420 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
94421 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
94429 ** The 32-bit integer value P1 is written into register P2.
94433 pOut->u.i = pOp->p1;
94440 ** P4 is a pointer to a 64-bit integer value.
94445 assert( pOp->p4.pI64!=0 );
94446 pOut->u.i = *pOp->p4.pI64;
94454 ** P4 is a pointer to a 64-bit floating point value.
94459 pOut->flags = MEM_Real;
94460 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
94461 pOut->u.r = *pOp->p4.pReal;
94469 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
94475 assert( pOp->p4.z!=0 );
94477 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
94481 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
94485 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
94487 pOut->szMalloc = 0;
94488 pOut->flags |= MEM_Static;
94489 if( pOp->p4type==P4_DYNAMIC ){
94490 sqlite3DbFree(db, pOp->p4.z);
94492 pOp->p4type = P4_DYNAMIC;
94493 pOp->p4.z = pOut->z;
94494 pOp->p1 = pOut->n;
94497 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94500 pOp->opcode = OP_String;
94519 assert( pOp->p4.z!=0 );
94521 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
94522 pOut->z = pOp->p4.z;
94523 pOut->n = pOp->p1;
94524 pOut->enc = encoding;
94527 if( pOp->p3>0 ){
94528 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94529 pIn3 = &aMem[pOp->p3];
94530 assert( pIn3->flags & MEM_Int );
94531 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
94540 ** Mark the beginning of a subroutine that can be entered in-line
94544 ** If the subroutine is entered in-line, then the OP_Return will simply
94550 ** a no-op that simply falls through to the next instruction (assuming that
94552 ** entered in-line, then the OP_Return will cause in-line execution to
94567 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
94573 int cnt;
94576 cnt = pOp->p3-pOp->p2;
94577 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94578 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
94579 pOut->n = 0;
94581 pOut->uTemp = 0;
94587 pOut->flags = nullFlag;
94588 pOut->n = 0;
94589 cnt--;
94603 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94604 pOut = &aMem[pOp->p1];
94605 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
94614 ** a zero-filled blob that is P1 bytes long in P2.
94617 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
94619 if( pOp->p4.z==0 ){
94620 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
94623 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
94625 pOut->enc = encoding;
94638 assert( pOp->p1>0 && pOp->p1<=p->nVar );
94639 pVar = &p->aVar[pOp->p1 - 1];
94643 pOut = &aMem[pOp->p2];
94646 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
94647 pOut->flags |= MEM_Static|MEM_FromBind;
94655 ** Move the P3 values in register P1..P1+P3-1 over into
94656 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
94658 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
94662 int n; /* Number of registers left to copy */
94663 int p1; /* Register to copy from */
94664 int p2; /* Register to copy to */
94666 n = pOp->p3;
94667 p1 = pOp->p1;
94668 p2 = pOp->p2;
94675 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
94676 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
94681 pIn1->pScopyFrom = 0;
94682 { int i;
94683 for(i=1; i<p->nMem; i++){
94694 }while( --n );
94712 int n;
94714 n = pOp->p3;
94715 pIn1 = &aMem[pOp->p1];
94716 pOut = &aMem[pOp->p2];
94722 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
94723 pOut->flags &= ~MEM_Subtype;
94726 pOut->pScopyFrom = 0;
94728 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
94729 if( (n--)==0 ) break;
94750 pIn1 = &aMem[pOp->p1];
94751 pOut = &aMem[pOp->p2];
94755 pOut->pScopyFrom = pIn1;
94756 pOut->mScopyFlags = pIn1->flags;
94770 pIn1 = &aMem[pOp->p1];
94771 assert( (pIn1->flags & MEM_Int)!=0 );
94772 pOut = &aMem[pOp->p2];
94773 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
94781 ** constraint violations, this is a no-op.
94798 ** The registers P1 through P1+P2-1 contain a single row of
94801 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
94805 assert( p->nResColumn==pOp->p2 );
94806 assert( pOp->p1>0 || CORRUPT_DB );
94807 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
94809 p->cacheCtr = (p->cacheCtr + 2)|1;
94810 p->pResultRow = &aMem[pOp->p1];
94813 Mem *pMem = p->pResultRow;
94814 int i;
94815 for(i=0; i<pOp->p2; i++){
94817 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
94827 if( db->mallocFailed ) goto no_mem;
94828 if( db->mTrace & SQLITE_TRACE_ROW ){
94829 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
94831 p->pc = (int)(pOp - aOp) + 1;
94854 pIn1 = &aMem[pOp->p1];
94855 pIn2 = &aMem[pOp->p2];
94856 pOut = &aMem[pOp->p3];
94859 flags1 = pIn1->flags;
94861 testcase( pIn2->flags & MEM_Null );
94862 if( (flags1 | pIn2->flags) & MEM_Null ){
94868 flags1 = pIn1->flags & ~MEM_Str;
94871 flags1 = pIn1->flags & ~MEM_Str;
94873 flags2 = pIn2->flags;
94876 flags2 = pIn2->flags & ~MEM_Str;
94879 flags2 = pIn2->flags & ~MEM_Str;
94881 nByte = pIn1->n + pIn2->n;
94882 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94885 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
94890 memcpy(pOut->z, pIn2->z, pIn2->n);
94891 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
94892 pIn2->flags = flags2;
94894 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
94895 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
94896 pIn1->flags = flags1;
94898 pOut->z[nByte]=0;
94899 pOut->z[nByte+1] = 0;
94900 pOut->flags |= MEM_Term;
94901 pOut->n = (int)nByte;
94902 pOut->enc = encoding;
94923 ** Synopsis: r[P3]=r[P2]-r[P1]
94957 pIn1 = &aMem[pOp->p1];
94958 type1 = pIn1->flags;
94959 pIn2 = &aMem[pOp->p2];
94960 type2 = pIn2->flags;
94961 pOut = &aMem[pOp->p3];
94964 iA = pIn1->u.i;
94965 iB = pIn2->u.i;
94966 switch( pOp->opcode ){
94972 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
94978 if( iA==-1 ) iA = 1;
94983 pOut->u.i = iB;
94994 switch( pOp->opcode ){
94996 case OP_Subtract: rB -= rA; break;
95008 if( iA==-1 ) iA = 1;
95014 pOut->u.i = rB;
95020 pOut->u.r = rB;
95035 ** be returned. This is used by the built-in min(), max() and nullif()
95044 ** publicly. Only built-in functions have access to this feature.
95047 assert( pOp->p4type==P4_COLLSEQ );
95048 if( pOp->p1 ){
95049 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
95057 ** Take the bit-wise AND of the values in register P1 and P2 and
95064 ** Take the bit-wise OR of the values in register P1 and P2 and
95093 pIn1 = &aMem[pOp->p1];
95094 pIn2 = &aMem[pOp->p2];
95095 pOut = &aMem[pOp->p3];
95096 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
95102 op = pOp->opcode;
95113 op = 2*OP_ShiftLeft + 1 - op;
95114 iB = iB>(-64) ? -iB : 64;
95118 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
95125 /* Sign-extend on a right shift of a negative number */
95126 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
95131 pOut->u.i = iA;
95145 pIn1 = &aMem[pOp->p1];
95148 *(u64*)&pIn1->u.i += (u64)pOp->p2;
95160 pIn1 = &aMem[pOp->p1];
95161 if( (pIn1->flags & MEM_Int)==0 ){
95163 if( (pIn1->flags & MEM_Int)==0 ){
95165 if( pOp->p2==0 ){
95189 pIn1 = &aMem[pOp->p1];
95190 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
95191 testcase( pIn1->flags & MEM_Int );
95192 testcase( pIn1->flags & MEM_IntReal );
95194 REGISTER_TRACE(pOp->p1, pIn1);
95217 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
95218 testcase( pOp->p2==SQLITE_AFF_TEXT );
95219 testcase( pOp->p2==SQLITE_AFF_BLOB );
95220 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
95221 testcase( pOp->p2==SQLITE_AFF_INTEGER );
95222 testcase( pOp->p2==SQLITE_AFF_REAL );
95223 pIn1 = &aMem[pOp->p1];
95227 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
95230 REGISTER_TRACE(pOp->p1, pIn1);
95241 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95285 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95333 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
95335 u16 flags1; /* Copy of initial value of pIn1->flags */
95336 u16 flags3; /* Copy of initial value of pIn3->flags */
95338 pIn1 = &aMem[pOp->p1];
95339 pIn3 = &aMem[pOp->p3];
95340 flags1 = pIn1->flags;
95341 flags3 = pIn3->flags;
95344 if( pIn3->u.i > pIn1->u.i ){
95345 if( sqlite3aGTb[pOp->opcode] ){
95346 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95351 }else if( pIn3->u.i < pIn1->u.i ){
95352 if( sqlite3aLTb[pOp->opcode] ){
95353 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95356 iCompare = -1;
95359 if( sqlite3aEQb[pOp->opcode] ){
95360 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95366 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95371 if( pOp->p5 & SQLITE_NULLEQ ){
95377 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
95378 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
95384 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
95392 if( pOp->p5 & SQLITE_JUMPIFNULL ){
95400 /* Neither operand is NULL and we couldn't do the special high-speed
95401 ** integer comparison case. So do a general-case comparison. */
95402 affinity = pOp->p5 & SQLITE_AFF_MASK;
95407 assert( flags3==pIn3->flags || CORRUPT_DB );
95408 flags3 = pIn3->flags;
95416 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
95418 testcase( pIn1->flags & MEM_Int );
95419 testcase( pIn1->flags & MEM_Real );
95420 testcase( pIn1->flags & MEM_IntReal );
95422 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
95423 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
95427 pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
95429 testcase( pIn3->flags & MEM_Int );
95430 testcase( pIn3->flags & MEM_Real );
95431 testcase( pIn3->flags & MEM_IntReal );
95433 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
95434 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
95437 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
95438 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
95450 res2 = sqlite3aLTb[pOp->opcode];
95452 res2 = sqlite3aEQb[pOp->opcode];
95454 res2 = sqlite3aGTb[pOp->opcode];
95460 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
95461 pIn3->flags = flags3;
95462 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
95463 pIn1->flags = flags1;
95465 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95487 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
95489 int iAddr;
95490 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
95515 assert( pOp->p4type==P4_INTARRAY );
95516 assert( pOp->p4.ai );
95523 ** Synopsis: r[P1@P3] <-> r[P2@P3]
95525 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
95526 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
95545 int n;
95546 int i;
95547 int p1;
95548 int p2;
95552 int bRev; /* True for DESCENDING sort order */
95555 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
95559 assert( pOp[-1].opcode==OP_Permutation );
95560 assert( pOp[-1].p4type==P4_INTARRAY );
95561 aPermute = pOp[-1].p4.ai + 1;
95564 n = pOp->p3;
95565 pKeyInfo = pOp->p4.pKeyInfo;
95568 p1 = pOp->p1;
95569 p2 = pOp->p2;
95572 int k, mx = 0;
95574 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
95575 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
95577 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
95578 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
95587 assert( i<pKeyInfo->nKeyField );
95588 pColl = pKeyInfo->aColl[i];
95589 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
95593 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
95596 iCompare = -iCompare;
95598 if( bRev ) iCompare = -iCompare;
95615 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
95618 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
95620 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
95622 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
95649 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
95650 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
95652 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
95653 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
95654 if( pOp->opcode==OP_And ){
95661 pOut = &aMem[pOp->p3];
95665 pOut->u.i = v1;
95692 assert( pOp->p4type==P4_INT32 );
95693 assert( pOp->p4.i==0 || pOp->p4.i==1 );
95694 assert( pOp->p3==0 || pOp->p3==1 );
95695 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
95696 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
95708 pIn1 = &aMem[pOp->p1];
95709 pOut = &aMem[pOp->p2];
95710 if( (pIn1->flags & MEM_Null)==0 ){
95722 ** ones-complement of the P1 value into register P2. If P1 holds
95726 pIn1 = &aMem[pOp->p1];
95727 pOut = &aMem[pOp->p2];
95729 if( (pIn1->flags & MEM_Null)==0 ){
95730 pOut->flags = MEM_Int;
95731 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
95739 ** encountered on each invocation of the byte-code program. Jump to P2
95742 ** Top-level programs determine first invocation by comparing the P1
95750 ** because the self-altering code trick does not work for recursive
95755 assert( p->aOp[0].opcode==OP_Init );
95756 if( p->pFrame ){
95757 iAddr = (int)(pOp - p->aOp);
95758 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
95762 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
95764 if( p->aOp[0].p1==pOp->p1 ){
95770 pOp->p1 = p->aOp[0].p1;
95777 ** is considered true if it is numeric and non-zero. If the value
95778 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
95781 int c;
95782 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
95792 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
95795 int c;
95796 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
95808 pIn1 = &aMem[pOp->p1];
95809 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
95810 if( (pIn1->flags & MEM_Null)!=0 ){
95829 ** If P1 is -1, then P3 is a register number and the datatype is taken
95852 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
95853 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
95854 if( pOp->p1>=0 ){
95855 pC = p->apCsr[pOp->p1];
95857 assert( pOp->p3>=0 );
95858 if( pOp->p3<pC->nHdrParsed ){
95859 serialType = pC->aType[pOp->p3];
95886 typeMask = 1 << (pOp->p4.i - 1);
95894 assert( memIsValid(&aMem[pOp->p3]) );
95895 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
95902 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
95903 if( typeMask & pOp->p5 ){
95917 if( (aMem[pOp->p1].flags & MEM_Null)!=0
95918 || (aMem[pOp->p3].flags & MEM_Null)!=0
95920 sqlite3VdbeMemSetNull(aMem + pOp->p2);
95922 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
95933 pIn1 = &aMem[pOp->p1];
95934 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
95935 if( (pIn1->flags & MEM_Null)==0 ){
95949 ** If P1 is not an open cursor, then this opcode is a no-op.
95953 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95954 pC = p->apCsr[pOp->p1];
95955 if( pC && pC->nullRow ){
95956 sqlite3VdbeMemSetNull(aMem + pOp->p3);
95976 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
95980 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95981 pC = p->apCsr[pOp->p1];
95982 pOut = &p->aMem[pOp->p3];
95983 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
95986 if( pC->deferredMoveto ){
95990 if( sqlite3BtreeEof(pC->uc.pCursor) ){
95993 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
96005 ** information about the format of the data.) Extract the P2-th column
96025 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
96026 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
96027 int len; /* The length of the serialized data for the column */
96028 int i; /* Loop counter */
96034 u64 offset64; /* 64-bit offset */
96038 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96039 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96040 pC = p->apCsr[pOp->p1];
96041 p2 = (u32)pOp->p2;
96045 assert( p2<(u32)pC->nField
96046 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
96047 aOffset = pC->aOffset;
96048 assert( aOffset==pC->aType+pC->nField );
96049 assert( pC->eCurType!=CURTYPE_VTAB );
96050 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
96051 assert( pC->eCurType!=CURTYPE_SORTER );
96053 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
96054 if( pC->nullRow ){
96055 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
96056 /* For the special case of as pseudo-cursor, the seekResult field
96058 pReg = &aMem[pC->seekResult];
96059 assert( pReg->flags & MEM_Blob );
96061 pC->payloadSize = pC->szRow = pReg->n;
96062 pC->aRow = (u8*)pReg->z;
96064 pDest = &aMem[pOp->p3];
96070 pCrsr = pC->uc.pCursor;
96071 if( pC->deferredMoveto ){
96073 assert( !pC->isEphemeral );
96074 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
96075 pC = pC->pAltCursor;
96076 p2 = iMap - 1;
96086 assert( pC->eCurType==CURTYPE_BTREE );
96089 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
96090 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
96091 assert( pC->szRow<=pC->payloadSize );
96092 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
96094 pC->cacheStatus = p->cacheCtr;
96095 if( (aOffset[0] = pC->aRow[0])<0x80 ){
96096 pC->iHdrOffset = 1;
96098 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
96100 pC->nHdrParsed = 0;
96102 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
96103 /* pC->aRow does not have to hold the entire row, but it does at least
96104 ** need to cover the header of the record. If pC->aRow does not contain
96107 pC->aRow = 0;
96108 pC->szRow = 0;
96116 ** 3-byte type for each of the maximum of 32768 columns plus three
96119 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
96124 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
96136 zData = pC->aRow;
96137 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
96141 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
96148 ** parsed and valid information is in aOffset[] and pC->aType[].
96150 if( pC->nHdrParsed<=p2 ){
96152 ** to extract additional fields up through the p2+1-th field
96154 if( pC->iHdrOffset<aOffset[0] ){
96156 if( pC->aRow==0 ){
96158 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
96162 zData = pC->aRow;
96165 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
96167 i = pC->nHdrParsed;
96169 zHdr = zData + pC->iHdrOffset;
96173 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
96178 pC->aType[i] = t;
96189 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
96190 || (offset64 > pC->payloadSize)
96196 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96201 pC->nHdrParsed = i;
96202 pC->iHdrOffset = (u32)(zHdr - zData);
96203 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96212 if( pC->nHdrParsed<=p2 ){
96213 pDest = &aMem[pOp->p3];
96215 if( pOp->p4type==P4_MEM ){
96216 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
96223 t = pC->aType[p2];
96226 /* Extract the content for the p2+1-th column. Control can only
96227 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
96230 assert( p2<pC->nHdrParsed );
96232 pDest = &aMem[pOp->p3];
96238 assert( t==pC->aType[p2] );
96239 if( pC->szRow>=aOffset[p2+1] ){
96241 ** page - where the content is not on an overflow page */
96242 zData = pC->aRow + aOffset[p2];
96247 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
96251 pDest->n = len = (t-12)/2;
96252 pDest->enc = encoding;
96253 if( pDest->szMalloc < len+2 ){
96254 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
96255 pDest->flags = MEM_Null;
96258 pDest->z = pDest->zMalloc;
96260 memcpy(pDest->z, zData, len);
96261 pDest->z[len] = 0;
96262 pDest->z[len+1] = 0;
96263 pDest->flags = aFlag[t&1];
96267 pDest->enc = encoding;
96268 assert( pDest->db==db );
96270 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0
96293 p->cacheCtr, colCacheCtr, pDest);
96304 REGISTER_TRACE(pOp->p3, pDest);
96309 pOp = &aOp[aOp[0].p3-1];
96329 ** is zero. When P3 is non-zero, no type checking occurs for
96336 ** <li> P2 should be the number of non-virtual columns in the
96346 int i;
96348 assert( pOp->p4type==P4_TABLE );
96349 pTab = pOp->p4.pTab;
96350 assert( pTab->tabFlags & TF_Strict );
96351 assert( pTab->nNVCol==pOp->p2 );
96352 aCol = pTab->aCol;
96353 pIn1 = &aMem[pOp->p1];
96354 for(i=0; i<pTab->nCol; i++){
96357 if( pOp->p3 ){ pIn1++; continue; }
96359 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
96361 if( (pIn1->flags & MEM_Null)==0 ){
96364 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
96369 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
96373 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
96377 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
96378 assert( (pIn1->flags & MEM_IntReal)==0 );
96379 if( pIn1->flags & MEM_Int ){
96382 ** so that we keep the high-resolution integer value but know that
96384 testcase( pIn1->u.i==140737488355328LL );
96385 testcase( pIn1->u.i==140737488355327LL );
96386 testcase( pIn1->u.i==-140737488355328LL );
96387 testcase( pIn1->u.i==-140737488355329LL );
96388 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
96389 pIn1->flags |= MEM_IntReal;
96390 pIn1->flags &= ~MEM_Int;
96392 pIn1->u.r = (double)pIn1->u.i;
96393 pIn1->flags |= MEM_Real;
96394 pIn1->flags &= ~MEM_Int;
96396 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
96407 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96410 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
96415 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
96416 pTab->zName, aCol[i].zCnName);
96426 ** P4 is a string that is P2 characters long. The N-th character of the
96427 ** string indicates the column affinity that should be used for the N-th
96433 zAffinity = pOp->p4.z;
96435 assert( pOp->p2>0 );
96436 assert( zAffinity[pOp->p2]==0 );
96437 pIn1 = &aMem[pOp->p1];
96438 while( 1 /*exit-by-break*/ ){
96439 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
96442 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
96445 ** so that we keep the high-resolution integer value but know that
96447 testcase( pIn1->u.i==140737488355328LL );
96448 testcase( pIn1->u.i==140737488355327LL );
96449 testcase( pIn1->u.i==-140737488355328LL );
96450 testcase( pIn1->u.i==-140737488355329LL );
96451 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
96452 pIn1->flags |= MEM_IntReal;
96453 pIn1->flags &= ~MEM_Int;
96455 pIn1->u.r = (double)pIn1->u.i;
96456 pIn1->flags |= MEM_Real;
96457 pIn1->flags &= ~(MEM_Int|MEM_Str);
96460 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96475 ** P4 may be a string that is P2 characters long. The N-th character of the
96476 ** string indicates the column affinity that should be used for the N-th
96485 ** compile-time option is enabled:
96488 ** of the right-most table that can be null-trimmed.
96492 ** accept no-change records with serial_type 10. This value is
96498 int nHdr; /* Number of bytes of header space */
96501 int nVarint; /* Number of bytes in a varint */
96505 int nField; /* Number of fields in the record */
96514 ** ------------------------------------------------------------------------
96515 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
96516 ** ------------------------------------------------------------------------
96523 ** hdr-size field is also a varint which is the offset from the beginning
96529 nField = pOp->p1;
96530 zAffinity = pOp->p4.z;
96531 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
96533 nField = pOp->p2;
96534 pLast = &pData0[nField-1];
96537 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
96538 pOut = &aMem[pOp->p3];
96548 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
96549 pRec->flags |= MEM_IntReal;
96550 pRec->flags &= ~(MEM_Int);
96552 REGISTER_TRACE((int)(pRec-aMem), pRec);
96562 ** have a non-NULL default value. Also, the record must be left with
96564 ** index of the right-most column with a non-NULL default value */
96565 if( pOp->p5 ){
96566 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
96567 pLast--;
96568 nField--;
96575 ** the Mem.uTemp field of each term should hold the serial-type that will
96579 ** --------------- ---------------
96581 ** 1 1-byte signed integer
96582 ** 2 2-byte signed integer
96583 ** 3 3-byte signed integer
96584 ** 4 4-byte signed integer
96585 ** 5 6-byte signed integer
96586 ** 6 8-byte signed integer
96602 if( pRec->flags & MEM_Null ){
96603 if( pRec->flags & MEM_Zero ){
96607 ** Give such values a special internal-use-only serial-type of 10
96611 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
96613 pRec->uTemp = 10;
96615 pRec->uTemp = 0;
96618 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
96620 i64 i = pRec->u.i;
96622 testcase( pRec->flags & MEM_Int );
96623 testcase( pRec->flags & MEM_IntReal );
96636 if( (i&1)==i && p->minWriteFileFormat>=4 ){
96637 pRec->uTemp = 8+(u32)uu;
96640 pRec->uTemp = 1;
96644 pRec->uTemp = 2;
96647 pRec->uTemp = 3;
96650 pRec->uTemp = 4;
96653 pRec->uTemp = 5;
96656 if( pRec->flags & MEM_IntReal ){
96658 ** as an integer, then we might as well make it an 8-byte floating
96660 pRec->u.r = (double)pRec->u.i;
96661 pRec->flags &= ~MEM_IntReal;
96662 pRec->flags |= MEM_Real;
96663 pRec->uTemp = 7;
96665 pRec->uTemp = 6;
96668 }else if( pRec->flags & MEM_Real ){
96671 pRec->uTemp = 7;
96673 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
96674 assert( pRec->n>=0 );
96675 len = (u32)pRec->n;
96676 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
96677 if( pRec->flags & MEM_Zero ){
96678 serial_type += pRec->u.nZero*2;
96681 len += pRec->u.nZero;
96683 nZero += pRec->u.nZero;
96688 pRec->uTemp = serial_type;
96691 pRec--;
96694 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
96712 ** the new record. The output register (pOp->p3) is not allowed to
96716 if( nByte+nZero<=pOut->szMalloc ){
96719 pOut->z = pOut->zMalloc;
96723 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
96726 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
96730 pOut->n = (int)nByte;
96731 pOut->flags = MEM_Blob;
96733 pOut->u.nZero = nZero;
96734 pOut->flags |= MEM_Zero;
96737 zHdr = (u8 *)pOut->z;
96748 while( 1 /*exit-by-break*/ ){
96749 serial_type = pRec->uTemp;
96750 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
96752 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
96761 assert( sizeof(v)==sizeof(pRec->u.r) );
96762 memcpy(&v, &pRec->u.r, sizeof(v));
96765 v = pRec->u.i;
96788 if( serial_type>=14 && pRec->n>0 ){
96789 assert( pRec->z!=0 );
96790 memcpy(zPayload, pRec->z, pRec->n);
96791 zPayload += pRec->n;
96795 if( pRec->n ){
96796 assert( pRec->z!=0 );
96797 memcpy(zPayload, pRec->z, pRec->n);
96798 zPayload += pRec->n;
96804 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
96805 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
96807 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96808 REGISTER_TRACE(pOp->p3, pOut);
96819 ** every btree page of the table. But if P3 is non-zero, an estimate
96826 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
96827 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
96829 if( pOp->p3 ){
96837 pOut->u.i = nEntry;
96849 int p1; /* Value of P1 operand */
96851 int nName;
96855 int iSavepoint;
96856 int ii;
96858 p1 = pOp->p1;
96859 zName = pOp->p4.z;
96864 assert( db->pSavepoint==0 || db->autoCommit==0 );
96866 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
96868 assert( p->bIsReader );
96871 if( db->nVdbeWrite>0 ){
96875 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
96884 ** that the db->aVTrans[] array is empty. */
96885 assert( db->autoCommit==0 || db->nVTrans==0 );
96887 db->nStatement+db->nSavepoint);
96894 pNew->zName = (char *)&pNew[1];
96895 memcpy(pNew->zName, zName, nName+1);
96899 if( db->autoCommit ){
96900 db->autoCommit = 0;
96901 db->isTransactionSavepoint = 1;
96903 db->nSavepoint++;
96907 pNew->pNext = db->pSavepoint;
96908 db->pSavepoint = pNew;
96909 pNew->nDeferredCons = db->nDeferredCons;
96910 pNew->nDeferredImmCons = db->nDeferredImmCons;
96920 pSavepoint = db->pSavepoint;
96921 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
96922 pSavepoint = pSavepoint->pNext
96929 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
96933 sqlite3VdbeError(p, "cannot release savepoint - "
96942 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
96947 db->autoCommit = 1;
96949 p->pc = (int)(pOp - aOp);
96950 db->autoCommit = 0;
96951 p->rc = rc = SQLITE_BUSY;
96954 rc = p->rc;
96956 db->autoCommit = 0;
96958 db->isTransactionSavepoint = 0;
96961 int isSchemaChange;
96962 iSavepoint = db->nSavepoint - iSavepoint - 1;
96964 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
96965 for(ii=0; ii<db->nDb; ii++){
96966 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
96975 for(ii=0; ii<db->nDb; ii++){
96976 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
96984 db->mDbFlags |= DBFLAG_SchemaChange;
96991 while( db->pSavepoint!=pSavepoint ){
96992 pTmp = db->pSavepoint;
96993 db->pSavepoint = pTmp->pNext;
96995 db->nSavepoint--;
97003 assert( pSavepoint==db->pSavepoint );
97004 db->pSavepoint = pSavepoint->pNext;
97007 db->nSavepoint--;
97011 db->nDeferredCons = pSavepoint->nDeferredCons;
97012 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
97022 if( p->eVdbeState==VDBE_HALT_STATE ){
97031 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
97039 int desiredAutoCommit;
97040 int iRollback;
97042 desiredAutoCommit = pOp->p1;
97043 iRollback = pOp->p2;
97046 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
97047 assert( p->bIsReader );
97049 if( desiredAutoCommit!=db->autoCommit ){
97053 db->autoCommit = 1;
97054 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
97058 sqlite3VdbeError(p, "cannot commit transaction - "
97065 db->autoCommit = (u8)desiredAutoCommit;
97068 p->pc = (int)(pOp - aOp);
97069 db->autoCommit = (u8)(1-desiredAutoCommit);
97070 p->rc = rc = SQLITE_BUSY;
97074 if( p->rc==SQLITE_OK ){
97083 (iRollback)?"cannot rollback - no transaction is active":
97084 "cannot commit - no transaction is active"));
97096 ** If P2 is non-zero, then a write-transaction is started, or if a
97097 ** read-transaction is already active, it is upgraded to a write-transaction.
97098 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
97106 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
97130 int iMeta = 0;
97132 assert( p->bIsReader );
97133 assert( p->readOnly==0 || pOp->p2==0 );
97134 assert( pOp->p2>=0 && pOp->p2<=2 );
97135 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97136 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97138 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
97139 if( db->flags & SQLITE_QueryOnly ){
97149 pDb = &db->aDb[pOp->p1];
97150 pBt = pDb->pBt;
97153 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
97158 p->pc = (int)(pOp - aOp);
97159 p->rc = rc;
97165 if( p->usesStmtJournal
97166 && pOp->p2
97167 && (db->autoCommit==0 || db->nVdbeRead>1)
97170 if( p->iStatement==0 ){
97171 assert( db->nStatement>=0 && db->nSavepoint>=0 );
97172 db->nStatement++;
97173 p->iStatement = db->nSavepoint + db->nStatement;
97176 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
97178 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
97184 p->nStmtDefCons = db->nDeferredCons;
97185 p->nStmtDefImmCons = db->nDeferredImmCons;
97188 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
97190 && pOp->p5
97191 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
97194 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
97198 sqlite3DbFree(db, p->zErrMsg);
97199 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
97200 /* If the schema-cookie from the database file matches the cookie
97201 ** stored with the in-memory representation of the schema, do
97204 ** If virtual-tables are in use, this is not just an optimization.
97205 ** Often, v-tables store their data in other SQLite tables, which
97206 ** are queried from within xNext() and other v-table methods using
97207 ** prepared queries. If such a query is out-of-date, we do not want to
97209 ** v-table would have to be ready for the sqlite3_vtab structure itself
97211 ** a v-table method.
97213 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
97214 sqlite3ResetOneSchema(db, pOp->p1);
97216 p->expired = 1;
97222 p->changeCntOn = 0;
97236 ** There must be a read-lock on the database (either a transaction
97241 int iMeta;
97242 int iDb;
97243 int iCookie;
97245 assert( p->bIsReader );
97246 iDb = pOp->p1;
97247 iCookie = pOp->p3;
97248 assert( pOp->p3<SQLITE_N_BTREE_META );
97249 assert( iDb>=0 && iDb<db->nDb );
97250 assert( db->aDb[iDb].pBt!=0 );
97251 assert( DbMaskTest(p->btreeMask, iDb) );
97253 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
97255 pOut->u.i = iMeta;
97270 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
97278 assert( pOp->p2<SQLITE_N_BTREE_META );
97279 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97280 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97281 assert( p->readOnly==0 );
97282 pDb = &db->aDb[pOp->p1];
97283 assert( pDb->pBt!=0 );
97284 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
97286 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
97287 if( pOp->p2==BTREE_SCHEMA_VERSION ){
97289 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
97290 db->mDbFlags |= DBFLAG_SchemaChange;
97291 sqlite3FkClearTriggerCache(db, pOp->p1);
97292 }else if( pOp->p2==BTREE_FILE_FORMAT ){
97294 pDb->pSchema->file_format = pOp->p3;
97296 if( pOp->p1==1 ){
97300 p->expired = 0;
97309 ** Open a read-only cursor for the database table whose root page is
97326 ** object, then table being opened must be an [index b-tree] where the
97328 ** sequence of that index b-tree. Otherwise, if P4 is an integer
97329 ** value, then the table being opened must be a [table b-tree] with a
97339 ** b-tree and if it is this opcode becomes a no-op. In other words,
97361 ** OPFLAG_P2ISREG bit is set in P5 - see below).
97365 ** object, then table being opened must be an [index b-tree] where the
97367 ** sequence of that index b-tree. Otherwise, if P4 is an integer
97368 ** value, then the table being opened must be a [table b-tree] with a
97391 int nField;
97394 int iDb;
97395 int wrFlag;
97400 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
97401 assert( pOp->p4type==P4_KEYINFO );
97402 pCur = p->apCsr[pOp->p1];
97403 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
97404 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
97405 assert( pCur->eCurType==CURTYPE_BTREE );
97406 sqlite3BtreeClearCursor(pCur->uc.pCursor);
97414 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
97415 assert( p->bIsReader );
97416 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
97417 || p->readOnly==0 );
97419 if( p->expired==1 ){
97426 p2 = (u32)pOp->p2;
97427 iDb = pOp->p3;
97428 assert( iDb>=0 && iDb<db->nDb );
97429 assert( DbMaskTest(p->btreeMask, iDb) );
97430 pDb = &db->aDb[iDb];
97431 pX = pDb->pBt;
97433 if( pOp->opcode==OP_OpenWrite ){
97435 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
97437 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
97438 p->minWriteFileFormat = pDb->pSchema->file_format;
97443 if( pOp->p5 & OPFLAG_P2ISREG ){
97445 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
97446 assert( pOp->opcode==OP_OpenWrite );
97449 assert( (pIn2->flags & MEM_Int)!=0 );
97451 p2 = (int)pIn2->u.i;
97458 if( pOp->p4type==P4_KEYINFO ){
97459 pKeyInfo = pOp->p4.pKeyInfo;
97460 assert( pKeyInfo->enc==ENC(db) );
97461 assert( pKeyInfo->db==db );
97462 nField = pKeyInfo->nAllField;
97463 }else if( pOp->p4type==P4_INT32 ){
97464 nField = pOp->p4.i;
97466 assert( pOp->p1>=0 );
97469 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
97471 pCur->iDb = iDb;
97472 pCur->nullRow = 1;
97473 pCur->isOrdered = 1;
97474 pCur->pgnoRoot = p2;
97476 pCur->wrFlag = wrFlag;
97478 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
97479 pCur->pKeyInfo = pKeyInfo;
97481 ** SQLite used to check if the root-page flags were sane at this point
97484 pCur->isTable = pOp->p4type!=P4_KEYINFO;
97489 testcase( pOp->p5 & OPFLAG_BULKCSR );
97490 testcase( pOp->p2 & OPFLAG_SEEKEQ );
97491 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
97492 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
97503 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
97509 pOrig = p->apCsr[pOp->p2];
97511 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
97513 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
97515 pCx->nullRow = 1;
97516 pCx->isEphemeral = 1;
97517 pCx->pKeyInfo = pOrig->pKeyInfo;
97518 pCx->isTable = pOrig->isTable;
97519 pCx->pgnoRoot = pOrig->pgnoRoot;
97520 pCx->isOrdered = pOrig->isOrdered;
97521 pCx->ub.pBtx = pOrig->ub.pBtx;
97522 pCx->noReuse = 1;
97523 pOrig->noReuse = 1;
97524 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
97525 pCx->pKeyInfo, pCx->uc.pCursor);
97539 ** the main database is read-only. The ephemeral
97556 ** can be used as zero-length data for OP_Insert. This is an optimization
97572 static const int vfsFlags =
97578 assert( pOp->p1>=0 );
97579 assert( pOp->p2>=0 );
97580 if( pOp->p3>0 ){
97583 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
97584 assert( pOp->opcode==OP_OpenEphemeral );
97585 assert( aMem[pOp->p3].flags & MEM_Null );
97586 aMem[pOp->p3].n = 0;
97587 aMem[pOp->p3].z = "";
97589 pCx = p->apCsr[pOp->p1];
97590 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
97594 assert( pCx->isEphemeral );
97595 pCx->seqCount = 0;
97596 pCx->cacheStatus = CACHE_STALE;
97597 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
97599 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
97601 pCx->isEphemeral = 1;
97602 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
97603 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
97606 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
97611 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
97613 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
97614 assert( pOp->p4type==P4_KEYINFO );
97615 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
97616 BTREE_BLOBKEY | pOp->p5);
97618 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
97619 assert( pKeyInfo->db==db );
97620 assert( pKeyInfo->enc==ENC(db) );
97621 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
97622 pKeyInfo, pCx->uc.pCursor);
97624 pCx->isTable = 0;
97626 pCx->pgnoRoot = SCHEMA_ROOT;
97627 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
97628 0, pCx->uc.pCursor);
97629 pCx->isTable = 1;
97632 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
97634 sqlite3BtreeClose(pCx->ub.pBtx);
97639 pCx->nullRow = 1;
97647 ** tables using an external merge-sort algorithm.
97649 ** If argument P3 is non-zero, then it indicates that the sorter may
97650 ** assume that a stable sort considering the first P3 fields of each
97656 assert( pOp->p1>=0 );
97657 assert( pOp->p2>=0 );
97658 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
97660 pCx->pKeyInfo = pOp->p4.pKeyInfo;
97661 assert( pCx->pKeyInfo->db==db );
97662 assert( pCx->pKeyInfo->enc==ENC(db) );
97663 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
97677 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97678 pC = p->apCsr[pOp->p1];
97680 if( (pC->seqCount++)==0 ){
97694 ** A pseudo-table created by this opcode is used to hold a single
97697 ** is the only cursor opcode that works with a pseudo-table.
97700 ** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
97706 assert( pOp->p1>=0 );
97707 assert( pOp->p3>=0 );
97708 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
97710 pCx->nullRow = 1;
97711 pCx->seekResult = pOp->p2;
97712 pCx->isTable = 1;
97713 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
97715 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
97717 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
97718 assert( pOp->p5==0 );
97725 ** currently open, this instruction is a no-op.
97728 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97729 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
97730 p->apCsr[pOp->p1] = 0;
97739 ** table or index for cursor P1 are used. P4 is a 64-bit integer
97742 ** by the cursor. The high-order bit is set if any column after
97747 pC = p->apCsr[pOp->p1];
97748 assert( pC->eCurType==CURTYPE_BTREE );
97749 pC->maskUsed = *(u64*)pOp->p4.pI64;
97757 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97784 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97802 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97820 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97848 int res; /* Comparison result */
97849 int oc; /* Opcode */
97852 int nField; /* Number of columns or fields in the key */
97854 int eqOnly; /* Only interested in == results */
97856 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97857 assert( pOp->p2!=0 );
97858 pC = p->apCsr[pOp->p1];
97860 assert( pC->eCurType==CURTYPE_BTREE );
97864 assert( pC->isOrdered );
97865 assert( pC->uc.pCursor!=0 );
97866 oc = pOp->opcode;
97868 pC->nullRow = 0;
97870 pC->seekOp = pOp->opcode;
97873 pC->deferredMoveto = 0;
97874 pC->cacheStatus = CACHE_STALE;
97875 if( pC->isTable ){
97878 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
97884 pIn3 = &aMem[pOp->p3];
97885 flags3 = pIn3->flags;
97890 newType = pIn3->flags; /* Record the type after applying numeric affinity */
97891 pIn3->flags = flags3; /* But convert the type back to its original */
97896 int c;
97902 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
97907 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
97913 ** (x > 4.9) -> (x >= 5)
97914 ** (x <= 4.9) -> (x < 5)
97917 assert( OP_SeekGE==(OP_SeekGT-1) );
97918 assert( OP_SeekLT==(OP_SeekLE-1) );
97920 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
97932 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
97933 pC->movetoTarget = iKey; /* Used by OP_Delete */
97943 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
97945 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
97947 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
97948 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
97955 nField = pOp->p4.i;
97956 assert( pOp->p4type==P4_INT32 );
97958 r.pKeyInfo = pC->pKeyInfo;
97963 ** r.default_rc = -1;
97968 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
97969 assert( oc!=OP_SeekGT || r.default_rc==-1 );
97970 assert( oc!=OP_SeekLE || r.default_rc==-1 );
97974 r.aMem = &aMem[pOp->p3];
97977 int i;
97980 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
97985 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
98000 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98016 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
98029 res = sqlite3BtreeEof(pC->uc.pCursor);
98033 assert( pOp->p2>0 );
98046 ** Synopsis: Scan-ahead up to P1 rows
98058 ** This opcode helps to optimize IN operators on a multi-column index
98061 ** of the b-tree instead. A correct answer is obtained if this opcode
98062 ** is omitted or is a no-op.
98069 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
98081 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
98115 int res;
98116 int nStep;
98121 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
98124 assert( pOp->p2>=(int)(pOp-aOp)+2 );
98126 if( pOp->p5==0 ){
98128 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
98129 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
98130 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
98131 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
98132 || aOp[pOp->p2-1].opcode==OP_IdxGE );
98133 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
98136 assert( pOp->p2==(int)(pOp-aOp)+2 );
98137 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
98141 assert( pOp->p1>0 );
98142 pC = p->apCsr[pOp[1].p1];
98144 assert( pC->eCurType==CURTYPE_BTREE );
98145 assert( !pC->isTable );
98146 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
98148 if( db->flags&SQLITE_VdbeTrace ){
98149 printf("... cursor not valid - fall through\n");
98154 nStep = pOp->p1;
98156 r.pKeyInfo = pC->pKeyInfo;
98162 int i;
98173 if( res>0 && pOp->p5==0 ){
98177 if( db->flags&SQLITE_VdbeTrace ){
98178 printf("... %d steps and then skip\n", pOp->p1 - nStep);
98188 if( db->flags&SQLITE_VdbeTrace ){
98189 printf("... %d steps and then success\n", pOp->p1 - nStep);
98198 if( db->flags&SQLITE_VdbeTrace ){
98199 printf("... fall through after %d steps\n", pOp->p1);
98205 nStep--;
98206 pC->cacheStatus = CACHE_STALE;
98207 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98232 ** early, thus saving work. This is part of the IN-early-out optimization.
98234 ** P1 must be a valid b-tree cursor.
98238 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98239 pC = p->apCsr[pOp->p1];
98241 assert( pOp->p3>=pOp->p2 );
98242 if( pC->seekHit<pOp->p2 ){
98244 if( db->flags&SQLITE_VdbeTrace ){
98245 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
98248 pC->seekHit = pOp->p2;
98249 }else if( pC->seekHit>pOp->p3 ){
98251 if( db->flags&SQLITE_VdbeTrace ){
98252 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
98255 pC->seekHit = pOp->p3;
98269 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98270 pCur = p->apCsr[pOp->p1];
98271 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
98272 if( pCur==0 || pCur->nullRow ){
98333 ** This opcode is used in IN clause processing for a multi-column key.
98335 ** left-most element, and if there are no matches on the most recent
98354 ** record are not-NULL then a check is done to determine if any row in the
98370 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98371 pC = p->apCsr[pOp->p1];
98374 if( db->flags&SQLITE_VdbeTrace ){
98375 printf("seekHit is %d\n", pC->seekHit);
98378 if( pC->seekHit>=pOp->p4.i ) break;
98385 int alreadyExists;
98386 int ii;
98392 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
98395 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98396 assert( pOp->p4type==P4_INT32 );
98397 pC = p->apCsr[pOp->p1];
98400 pC->seekOp = pOp->opcode;
98402 r.aMem = &aMem[pOp->p3];
98403 assert( pC->eCurType==CURTYPE_BTREE );
98404 assert( pC->uc.pCursor!=0 );
98405 assert( pC->isTable==0 );
98406 r.nField = (u16)pOp->p4.i;
98409 r.pKeyInfo = pC->pKeyInfo;
98415 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
98418 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
98421 assert( r.aMem->flags & MEM_Blob );
98422 assert( pOp->opcode!=OP_NoConflict );
98426 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
98428 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
98429 pIdxKey->default_rc = 0;
98430 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
98436 alreadyExists = (pC->seekResult==0);
98437 pC->nullRow = 1-alreadyExists;
98438 pC->deferredMoveto = 0;
98439 pC->cacheStatus = CACHE_STALE;
98440 if( pOp->opcode==OP_Found ){
98448 if( pOp->opcode==OP_NoConflict ){
98460 if( pOp->opcode==OP_IfNoHope ){
98461 pC->seekHit = pOp->p4.i;
98483 ** (with arbitrary multi-value keys).
98502 ** P3 register to contain a non-integer value, in which case the jump is
98506 ** (with arbitrary multi-value keys).
98517 int res;
98520 pIn3 = &aMem[pOp->p3];
98521 testcase( pIn3->flags & MEM_Int );
98522 testcase( pIn3->flags & MEM_IntReal );
98523 testcase( pIn3->flags & MEM_Real );
98524 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
98525 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
98526 /* If pIn3->u.i does not contain an integer, compute iKey as the
98540 pIn3 = &aMem[pOp->p3];
98541 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
98542 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98543 iKey = pIn3->u.i;
98545 pC = p->apCsr[pOp->p1];
98548 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
98550 assert( pC->isTable );
98551 assert( pC->eCurType==CURTYPE_BTREE );
98552 pCrsr = pC->uc.pCursor;
98557 pC->movetoTarget = iKey; /* Used by OP_Delete */
98558 pC->nullRow = 0;
98559 pC->cacheStatus = CACHE_STALE;
98560 pC->deferredMoveto = 0;
98562 pC->seekResult = res;
98565 if( pOp->p2==0 ){
98584 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98585 assert( p->apCsr[pOp->p1]!=0 );
98586 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
98588 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
98611 int res; /* Result of an sqlite3BtreeLast() */
98612 int cnt; /* Counter to limit the number of searches */
98621 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98622 pC = p->apCsr[pOp->p1];
98624 assert( pC->isTable );
98625 assert( pC->eCurType==CURTYPE_BTREE );
98626 assert( pC->uc.pCursor!=0 );
98629 ** thing) is obtained in a two-step algorithm.
98641 assert( pC->isTable );
98653 if( !pC->useRandomRowid ){
98654 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
98659 v = 1; /* IMP: R-61914-48074 */
98661 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
98662 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98664 pC->useRandomRowid = 1;
98666 v++; /* IMP: R-29538-34987 */
98672 if( pOp->p3 ){
98674 assert( pOp->p3>0 );
98675 if( p->pFrame ){
98676 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
98678 assert( pOp->p3<=pFrame->nMem );
98679 pMem = &pFrame->aMem[pOp->p3];
98682 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
98683 pMem = &aMem[pOp->p3];
98688 REGISTER_TRACE(pOp->p3, pMem);
98690 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
98691 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
98692 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
98695 if( v<pMem->u.i+1 ){
98696 v = pMem->u.i + 1;
98698 pMem->u.i = v;
98701 if( pC->useRandomRowid ){
98702 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
98706 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
98712 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
98718 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
98721 assert( v>0 ); /* EV: R-40812-03570 */
98723 pC->deferredMoveto = 0;
98724 pC->cacheStatus = CACHE_STALE;
98726 pOut->u.i = v;
98755 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
98758 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
98759 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
98771 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
98772 const char *zDb; /* database name - used by the update hook */
98773 Table *pTab; /* Table structure - used by update and pre-update hooks */
98776 pData = &aMem[pOp->p2];
98777 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98779 pC = p->apCsr[pOp->p1];
98781 assert( pC->eCurType==CURTYPE_BTREE );
98782 assert( pC->deferredMoveto==0 );
98783 assert( pC->uc.pCursor!=0 );
98784 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
98785 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
98786 REGISTER_TRACE(pOp->p2, pData);
98789 pKey = &aMem[pOp->p3];
98790 assert( pKey->flags & MEM_Int );
98792 REGISTER_TRACE(pOp->p3, pKey);
98793 x.nKey = pKey->u.i;
98795 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
98796 assert( pC->iDb>=0 );
98797 zDb = db->aDb[pC->iDb].zDbSName;
98798 pTab = pOp->p4.pTab;
98799 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
98806 /* Invoke the pre-update hook, if any */
98808 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
98809 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
98811 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
98812 /* Prevent post-update hook from running in cases when it should not */
98816 if( pOp->p5 & OPFLAG_ISNOOP ) break;
98819 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
98820 if( pOp->p5 & OPFLAG_NCHANGE ){
98821 p->nChange++;
98822 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
98824 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
98825 x.pData = pData->z;
98826 x.nData = pData->n;
98827 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
98828 if( pData->flags & MEM_Zero ){
98829 x.nZero = pData->u.nZero;
98835 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
98836 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
98839 pC->deferredMoveto = 0;
98840 pC->cacheStatus = CACHE_STALE;
98843 /* Invoke the update-hook if required. */
98846 assert( db->xUpdateCallback!=0 );
98847 assert( pTab->aCol!=0 );
98848 db->xUpdateCallback(db->pUpdateArg,
98849 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
98850 zDb, pTab->zName, x.nKey);
98858 ** of table - intkey or index. This opcode is used as part of copying
98871 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
98872 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
98874 pDest = p->apCsr[pOp->p1];
98875 pSrc = p->apCsr[pOp->p2];
98876 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
98877 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
98889 ** the next Next instruction will be a no-op. As a result, in this case
98904 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
98908 ** P1 must not be pseudo-table. It has to be a real table with
98912 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
98914 ** this case. Specifically, if one is configured, the pre-update hook is
98915 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
98926 int opflags;
98928 opflags = pOp->p2;
98929 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98930 pC = p->apCsr[pOp->p1];
98932 assert( pC->eCurType==CURTYPE_BTREE );
98933 assert( pC->uc.pCursor!=0 );
98934 assert( pC->deferredMoveto==0 );
98938 if( pOp->p4type==P4_TABLE
98939 && HasRowid(pOp->p4.pTab)
98940 && pOp->p5==0
98941 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
98944 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
98946 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98947 assert( CORRUPT_DB || pC->movetoTarget==iKey );
98951 /* If the update-hook or pre-update-hook will be invoked, set zDb to
98956 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
98957 assert( pC->iDb>=0 );
98958 assert( pOp->p4.pTab!=0 );
98959 zDb = db->aDb[pC->iDb].zDbSName;
98960 pTab = pOp->p4.pTab;
98961 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
98962 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98970 /* Invoke the pre-update-hook if required. */
98971 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
98972 if( db->xPreUpdateCallback && pTab ){
98975 || (aMem[pOp->p3].flags & MEM_Int)
98979 zDb, pTab, pC->movetoTarget,
98980 pOp->p3, -1
98987 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
98992 if( p->pFrame==0 ){
98993 if( pC->isEphemeral==0
98994 && (pOp->p5 & OPFLAG_AUXDELETE)==0
98995 && (pC->wrFlag & OPFLAG_FORDELETE)==0
98999 if( pOp->p2 & OPFLAG_NCHANGE ){
99000 nExtraDelete--;
99005 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
99006 pC->cacheStatus = CACHE_STALE;
99008 pC->seekResult = 0;
99011 /* Invoke the update-hook if required. */
99013 p->nChange++;
99014 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
99015 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
99016 pC->movetoTarget);
99017 assert( pC->iDb>=0 );
99031 sqlite3VdbeSetChanges(db, p->nChange);
99032 p->nChange = 0;
99053 int res;
99054 int nKeyCol;
99056 pC = p->apCsr[pOp->p1];
99058 assert( pOp->p4type==P4_INT32 );
99059 pIn3 = &aMem[pOp->p3];
99060 nKeyCol = pOp->p4.i;
99076 ** a register that is the source for a pseudo-table cursor created using
99077 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
99084 pOut = &aMem[pOp->p2];
99085 pC = p->apCsr[pOp->p1];
99088 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
99089 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99091 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
99108 ** of a real table, not a pseudo-table.
99112 ** register will be invalidated as soon as the cursor moves - including
99130 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99131 pC = p->apCsr[pOp->p1];
99133 assert( pC->eCurType==CURTYPE_BTREE );
99135 assert( pC->nullRow==0 );
99136 assert( pC->uc.pCursor!=0 );
99137 pCrsr = pC->uc.pCursor;
99147 assert( pC->deferredMoveto==0 );
99151 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
99157 if( !pOp->p3 ) Deephemeralize(pOut);
99159 REGISTER_TRACE(pOp->p2, pOut);
99180 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99181 pC = p->apCsr[pOp->p1];
99183 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
99184 if( pC->nullRow ){
99185 pOut->flags = MEM_Null;
99187 }else if( pC->deferredMoveto ){
99188 v = pC->movetoTarget;
99190 }else if( pC->eCurType==CURTYPE_VTAB ){
99191 assert( pC->uc.pVCur!=0 );
99192 pVtab = pC->uc.pVCur->pVtab;
99193 pModule = pVtab->pModule;
99194 assert( pModule->xRowid );
99195 rc = pModule->xRowid(pC->uc.pVCur, &v);
99200 assert( pC->eCurType==CURTYPE_BTREE );
99201 assert( pC->uc.pCursor!=0 );
99204 if( pC->nullRow ){
99205 pOut->flags = MEM_Null;
99208 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99210 pOut->u.i = v;
99221 ** pseudo-cursor that always returns NULL for every column.
99226 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99227 pC = p->apCsr[pOp->p1];
99230 ** pseudo-cursor that always gives null rows. */
99231 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
99233 pC->seekResult = 0;
99234 pC->isTable = 1;
99235 pC->noReuse = 1;
99236 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
99238 pC->nullRow = 1;
99239 pC->cacheStatus = CACHE_STALE;
99240 if( pC->eCurType==CURTYPE_BTREE ){
99241 assert( pC->uc.pCursor!=0 );
99242 sqlite3BtreeClearCursor(pC->uc.pCursor);
99245 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
99276 int res;
99278 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99279 pC = p->apCsr[pOp->p1];
99281 assert( pC->eCurType==CURTYPE_BTREE );
99282 pCrsr = pC->uc.pCursor;
99286 pC->seekOp = pOp->opcode;
99288 if( pOp->opcode==OP_SeekEnd ){
99289 assert( pOp->p2==0 );
99290 pC->seekResult = -1;
99296 pC->nullRow = (u8)res;
99297 pC->deferredMoveto = 0;
99298 pC->cacheStatus = CACHE_STALE;
99300 if( pOp->p2>0 ){
99310 ** with cursor P1 and let X be 10*log2(N) if N is positive or -1
99318 int res;
99321 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99322 assert( pOp->p4type==P4_INT32 );
99323 assert( pOp->p3>=-1 && pOp->p3<=640*2 );
99324 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 );
99325 pC = p->apCsr[pOp->p1];
99327 pCrsr = pC->uc.pCursor;
99332 sz = -1; /* -Infinity encoding */
99338 res = sz>=pOp->p3 && sz<=pOp->p4.i;
99370 sqlite3_search_count--;
99372 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
99394 int res;
99396 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99397 assert( pOp->p5==0 );
99398 assert( pOp->p2>=0 && pOp->p2<p->nOp );
99400 pC = p->apCsr[pOp->p1];
99402 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
99405 pC->seekOp = OP_Rewind;
99410 assert( pC->eCurType==CURTYPE_BTREE );
99411 pCrsr = pC->uc.pCursor;
99414 pC->deferredMoveto = 0;
99415 pC->cacheStatus = CACHE_STALE;
99418 pC->nullRow = (u8)res;
99419 if( pOp->p2>0 ){
99437 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
99446 ** number P5-1 in the prepared statement is incremented.
99462 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
99471 ** number P5-1 in the prepared statement is incremented.
99483 pC = p->apCsr[pOp->p1];
99489 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99490 assert( pOp->p5==0
99491 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
99492 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
99493 pC = p->apCsr[pOp->p1];
99495 assert( pC->deferredMoveto==0 );
99496 assert( pC->eCurType==CURTYPE_BTREE );
99497 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
99498 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
99499 || pC->seekOp==OP_NullRow);
99500 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
99504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99505 assert( pOp->p5==0
99506 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
99507 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
99508 pC = p->apCsr[pOp->p1];
99510 assert( pC->deferredMoveto==0 );
99511 assert( pC->eCurType==CURTYPE_BTREE );
99512 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
99513 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
99514 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
99515 || pC->seekOp==OP_IfNoHope);
99516 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
99519 pC->cacheStatus = CACHE_STALE;
99522 pC->nullRow = 0;
99523 p->aCounter[pOp->p5]++;
99531 pC->nullRow = 1;
99547 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
99567 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99568 pC = p->apCsr[pOp->p1];
99572 pIn2 = &aMem[pOp->p2];
99573 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
99574 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
99575 assert( pC->eCurType==CURTYPE_BTREE );
99576 assert( pC->isTable==0 );
99579 x.nKey = pIn2->n;
99580 x.pKey = pIn2->z;
99581 x.aMem = aMem + pOp->p3;
99582 x.nMem = (u16)pOp->p4.i;
99583 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
99584 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
99585 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
99587 assert( pC->deferredMoveto==0 );
99588 pC->cacheStatus = CACHE_STALE;
99603 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99604 pC = p->apCsr[pOp->p1];
99608 pIn2 = &aMem[pOp->p2];
99609 assert( pIn2->flags & MEM_Blob );
99610 assert( pC->isTable==0 );
99631 ** this (self-correcting and non-critical) error if in writable_schema mode.
99636 int res;
99639 assert( pOp->p3>0 );
99640 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
99641 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99642 pC = p->apCsr[pOp->p1];
99644 assert( pC->eCurType==CURTYPE_BTREE );
99646 pCrsr = pC->uc.pCursor;
99648 r.pKeyInfo = pC->pKeyInfo;
99649 r.nField = (u16)pOp->p3;
99651 r.aMem = &aMem[pOp->p2];
99657 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
99661 assert( pC->deferredMoveto==0 );
99662 pC->cacheStatus = CACHE_STALE;
99663 pC->seekResult = 0;
99680 ** is non-zero, then reading column a(i)-1 from cursor P3 is
99701 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99702 pC = p->apCsr[pOp->p1];
99704 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
99705 assert( pC->uc.pCursor!=0 );
99706 assert( pC->isTable==0 || IsNullCursor(pC) );
99707 assert( pC->deferredMoveto==0 );
99708 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
99719 if( !pC->nullRow ){
99721 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
99725 if( pOp->opcode==OP_DeferredSeek ){
99726 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
99727 pTabCur = p->apCsr[pOp->p3];
99729 assert( pTabCur->eCurType==CURTYPE_BTREE );
99730 assert( pTabCur->uc.pCursor!=0 );
99731 assert( pTabCur->isTable );
99732 pTabCur->nullRow = 0;
99733 pTabCur->movetoTarget = rowid;
99734 pTabCur->deferredMoveto = 1;
99735 pTabCur->cacheStatus = CACHE_STALE;
99736 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
99737 assert( !pTabCur->isEphemeral );
99738 pTabCur->ub.aAltMap = pOp->p4.ai;
99739 assert( !pC->isEphemeral );
99740 pTabCur->pAltCursor = pC;
99743 pOut->u.i = rowid;
99746 assert( pOp->opcode==OP_IdxRowid );
99747 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
99756 ** already occurred, this instruction is a no-op.
99761 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99762 pC = p->apCsr[pOp->p1];
99763 if( pC->deferredMoveto ){
99819 int res;
99822 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99823 pC = p->apCsr[pOp->p1];
99825 assert( pC->isOrdered );
99826 assert( pC->eCurType==CURTYPE_BTREE );
99827 assert( pC->uc.pCursor!=0);
99828 assert( pC->deferredMoveto==0 );
99829 assert( pOp->p4type==P4_INT32 );
99830 r.pKeyInfo = pC->pKeyInfo;
99831 r.nField = (u16)pOp->p4.i;
99832 if( pOp->opcode<OP_IdxLT ){
99833 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
99834 r.default_rc = -1;
99836 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
99839 r.aMem = &aMem[pOp->p3];
99842 int i;
99845 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
99856 assert( pC->eCurType==CURTYPE_BTREE );
99857 pCur = pC->uc.pCursor;
99875 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
99876 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
99877 res = -res;
99879 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
99900 ** value of the root page that moved - its value before the move occurred -
99911 ** and non-autovacuum modes.
99916 int iMoved;
99917 int iDb;
99920 assert( p->readOnly==0 );
99921 assert( pOp->p1>1 );
99923 pOut->flags = MEM_Null;
99924 if( db->nVdbeRead > db->nVDestroy+1 ){
99926 p->errorAction = OE_Abort;
99929 iDb = pOp->p3;
99930 assert( DbMaskTest(p->btreeMask, iDb) );
99932 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
99933 pOut->flags = MEM_Int;
99934 pOut->u.i = iMoved;
99938 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
99958 ** If the P3 value is non-zero, then the row change count is incremented
99970 assert( p->readOnly==0 );
99971 assert( DbMaskTest(p->btreeMask, pOp->p2) );
99972 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
99973 if( pOp->p3 ){
99974 p->nChange += nChange;
99975 if( pOp->p3>0 ){
99976 assert( memIsValid(&aMem[pOp->p3]) );
99977 memAboutToChange(p, &aMem[pOp->p3]);
99978 aMem[pOp->p3].u.i += nChange;
99996 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99997 pC = p->apCsr[pOp->p1];
100000 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
100002 assert( pC->eCurType==CURTYPE_BTREE );
100003 assert( pC->isEphemeral );
100004 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
100013 ** Allocate a new b-tree in the main database file if P1==0 or in the
100017 ** The root page number of the new b-tree is stored in register P2.
100026 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
100027 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100028 assert( DbMaskTest(p->btreeMask, pOp->p1) );
100029 assert( p->readOnly==0 );
100030 pDb = &db->aDb[pOp->p1];
100031 assert( pDb->pBt!=0 );
100032 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
100034 pOut->u.i = pgno;
100047 ** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
100057 int savedAnalysisLimit;
100060 db->nSqlExec++;
100063 xAuth = db->xAuth;
100065 mTrace = db->mTrace;
100066 savedAnalysisLimit = db->nAnalysisLimit;
100067 if( pOp->p1 & 0x0001 ){
100069 db->xAuth = 0;
100071 db->mTrace = 0;
100073 if( pOp->p1 & 0x0002 ){
100074 db->nAnalysisLimit = pOp->p2;
100076 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
100077 db->nSqlExec--;
100079 db->xAuth = xAuth;
100081 db->mTrace = mTrace;
100082 db->nAnalysisLimit = savedAnalysisLimit;
100099 ** then runs the new virtual machine. It is thus a re-entrant opcode.
100102 int iDb;
100112 for(iDb=0; iDb<db->nDb; iDb++){
100113 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
100117 iDb = pOp->p1;
100118 assert( iDb>=0 && iDb<db->nDb );
100120 || db->mallocFailed
100121 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
100124 if( pOp->p4.z==0 ){
100125 sqlite3SchemaClear(db->aDb[iDb].pSchema);
100126 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
100127 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
100128 db->mDbFlags |= DBFLAG_SchemaChange;
100129 p->expired = 0;
100136 initData.pzErrMsg = &p->zErrMsg;
100138 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
100141 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
100145 assert( db->init.busy==0 );
100146 db->init.busy = 1;
100149 assert( !db->mallocFailed );
100153 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
100159 db->init.busy = 0;
100180 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100181 rc = sqlite3AnalysisLoad(db, pOp->p1);
100189 ** Remove the internal (in-memory) data structures that describe
100197 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
100203 ** Remove the internal (in-memory) data structures that describe
100211 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
100217 ** Remove the internal (in-memory) data structures that describe
100225 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
100251 int nRoot; /* Number of tables to check. (Number of root pages.) */
100253 int nErr; /* Number of errors reported */
100257 assert( p->bIsReader );
100258 assert( pOp->p4type==P4_INTARRAY );
100259 nRoot = pOp->p2;
100260 aRoot = pOp->p4.ai;
100264 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) );
100265 pnErr = &aMem[pOp->p1];
100266 assert( (pnErr->flags & MEM_Int)!=0 );
100267 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
100268 pIn1 = &aMem[pOp->p1+1];
100269 assert( pOp->p5<db->nDb );
100270 assert( DbMaskTest(p->btreeMask, pOp->p5) );
100271 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
100272 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
100280 pnErr->u.i -= nErr-1;
100281 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
100298 pIn1 = &aMem[pOp->p1];
100299 pIn2 = &aMem[pOp->p2];
100300 assert( (pIn2->flags & MEM_Int)!=0 );
100301 if( (pIn1->flags & MEM_Blob)==0 ){
100305 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
100320 pIn1 = &aMem[pOp->p1];
100321 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
100322 if( (pIn1->flags & MEM_Blob)==0
100323 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
100332 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
100340 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
100349 ** must have P4==0, the final set must have P4==-1, and for all other sets
100354 ** (b) when P4==-1 there is no need to insert the value, as it will
100361 int iSet;
100362 int exists;
100364 pIn1 = &aMem[pOp->p1];
100365 pIn3 = &aMem[pOp->p3];
100366 iSet = pOp->p4.i;
100367 assert( pIn3->flags&MEM_Int );
100372 if( (pIn1->flags & MEM_Blob)==0 ){
100376 assert( pOp->p4type==P4_INT32 );
100377 assert( iSet==-1 || iSet>=0 );
100379 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
100384 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
100397 ** cell in an array of values used as arguments to the sub-program. P2
100398 ** contains the address to jump to if the sub-program throws an IGNORE
100403 ** memory required by the sub-vdbe at runtime.
100407 ** If P5 is non-zero, then recursive program invocation is enabled.
100410 int nMem; /* Number of memory registers for sub-program */
100411 int nByte; /* Bytes of runtime space required for sub-program */
100416 SubProgram *pProgram; /* Sub-program to execute */
100419 pProgram = pOp->p4.pProgram;
100420 pRt = &aMem[pOp->p3];
100421 assert( pProgram->nOp>0 );
100424 ** disabled for backwards compatibility (p5 is set if this sub-program
100434 if( pOp->p5 ){
100435 t = pProgram->token;
100436 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
100440 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
100450 if( (pRt->flags&MEM_Blob)==0 ){
100456 nMem = pProgram->nMem + pProgram->nCsr;
100458 if( pProgram->nCsr==0 ) nMem++;
100461 + pProgram->nCsr * sizeof(VdbeCursor*)
100462 + (pProgram->nOp + 7)/8;
100468 pRt->flags = MEM_Blob|MEM_Dyn;
100469 pRt->z = (char*)pFrame;
100470 pRt->n = nByte;
100471 pRt->xDel = sqlite3VdbeFrameMemDel;
100473 pFrame->v = p;
100474 pFrame->nChildMem = nMem;
100475 pFrame->nChildCsr = pProgram->nCsr;
100476 pFrame->pc = (int)(pOp - aOp);
100477 pFrame->aMem = p->aMem;
100478 pFrame->nMem = p->nMem;
100479 pFrame->apCsr = p->apCsr;
100480 pFrame->nCursor = p->nCursor;
100481 pFrame->aOp = p->aOp;
100482 pFrame->nOp = p->nOp;
100483 pFrame->token = pProgram->token;
100485 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
100488 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
100490 pMem->flags = MEM_Undefined;
100491 pMem->db = db;
100494 pFrame = (VdbeFrame*)pRt->z;
100495 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
100496 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
100497 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
100498 assert( pProgram->nCsr==pFrame->nChildCsr );
100499 assert( (int)(pOp - aOp)==pFrame->pc );
100502 p->nFrame++;
100503 pFrame->pParent = p->pFrame;
100504 pFrame->lastRowid = db->lastRowid;
100505 pFrame->nChange = p->nChange;
100506 pFrame->nDbChange = p->db->nChange;
100507 assert( pFrame->pAuxData==0 );
100508 pFrame->pAuxData = p->pAuxData;
100509 p->pAuxData = 0;
100510 p->nChange = 0;
100511 p->pFrame = pFrame;
100512 p->aMem = aMem = VdbeFrameMem(pFrame);
100513 p->nMem = pFrame->nChildMem;
100514 p->nCursor = (u16)pFrame->nChildCsr;
100515 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
100516 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
100517 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
100518 p->aOp = aOp = pProgram->aOp;
100519 p->nOp = pProgram->nOp;
100524 int i;
100525 for(i=0; i<p->nMem; i++){
100526 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
100531 pOp = &aOp[-1];
100537 ** This opcode is only ever present in sub-programs called via the
100551 pFrame = p->pFrame;
100552 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
100564 ** If P1 is non-zero, the database constraint counter is incremented
100569 if( db->flags & SQLITE_DeferFKs ){
100570 db->nDeferredImmCons += pOp->p2;
100571 }else if( pOp->p1 ){
100572 db->nDeferredCons += pOp->p2;
100574 p->nFkConstraint += pOp->p2;
100582 ** This opcode tests if a foreign key constraint-counter is currently zero.
100586 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
100588 ** zero, the jump is taken if the statement constraint-counter is zero
100592 if( pOp->p1 ){
100593 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
100594 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100596 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
100597 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100609 ** within a sub-program). Set the value of register P1 to the maximum of
100617 if( p->pFrame ){
100618 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
100619 pIn1 = &pFrame->aMem[pOp->p1];
100621 pIn1 = &aMem[pOp->p1];
100625 pIn2 = &aMem[pOp->p2];
100627 if( pIn1->u.i<pIn2->u.i){
100628 pIn1->u.i = pIn2->u.i;
100635 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
100645 pIn1 = &aMem[pOp->p1];
100646 assert( pIn1->flags&MEM_Int );
100647 VdbeBranchTaken( pIn1->u.i>0, 2);
100648 if( pIn1->u.i>0 ){
100649 pIn1->u.i -= pOp->p3;
100656 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
100669 ** and r[P2] is set to -1.
100675 pIn1 = &aMem[pOp->p1];
100676 pIn3 = &aMem[pOp->p3];
100678 assert( pIn1->flags & MEM_Int );
100679 assert( pIn3->flags & MEM_Int );
100680 x = pIn1->u.i;
100681 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
100689 pOut->u.i = -1;
100691 pOut->u.i = x;
100697 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
100701 ** If it is non-zero (negative or positive) and then also jump to P2.
100705 pIn1 = &aMem[pOp->p1];
100706 assert( pIn1->flags&MEM_Int );
100707 VdbeBranchTaken(pIn1->u.i<0, 2);
100708 if( pIn1->u.i ){
100709 if( pIn1->u.i>0 ) pIn1->u.i--;
100716 ** Synopsis: if (--r[P1])==0 goto P2
100722 pIn1 = &aMem[pOp->p1];
100723 assert( pIn1->flags&MEM_Int );
100724 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
100725 VdbeBranchTaken(pIn1->u.i==0, 2);
100726 if( pIn1->u.i==0 ) goto jump_to_p2;
100772 int n;
100775 assert( pOp->p4type==P4_FUNCDEF );
100776 n = pOp->p5;
100777 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100778 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
100779 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
100781 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
100783 pCtx->pMem = 0;
100784 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
100785 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
100786 pCtx->pFunc = pOp->p4.pFunc;
100787 pCtx->iOp = (int)(pOp - aOp);
100788 pCtx->pVdbe = p;
100789 pCtx->skipFlag = 0;
100790 pCtx->isError = 0;
100791 pCtx->enc = encoding;
100792 pCtx->argc = n;
100793 pOp->p4type = P4_FUNCCTX;
100794 pOp->p4.pCtx = pCtx;
100797 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
100799 pOp->opcode = OP_AggStep1;
100804 int i;
100808 assert( pOp->p4type==P4_FUNCCTX );
100809 pCtx = pOp->p4.pCtx;
100810 pMem = &aMem[pOp->p3];
100813 if( pOp->p1 ){
100816 assert( pMem->uTemp==0x1122e0e3 );
100819 pMem->uTemp = 0x1122e0e3;
100827 if( pCtx->pMem != pMem ){
100828 pCtx->pMem = pMem;
100829 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
100833 for(i=0; i<pCtx->argc; i++){
100834 assert( memIsValid(pCtx->argv[i]) );
100835 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
100839 pMem->n++;
100840 assert( pCtx->pOut->flags==MEM_Null );
100841 assert( pCtx->isError==0 );
100842 assert( pCtx->skipFlag==0 );
100844 if( pOp->p1 ){
100845 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
100848 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
100850 if( pCtx->isError ){
100851 if( pCtx->isError>0 ){
100852 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
100853 rc = pCtx->isError;
100855 if( pCtx->skipFlag ){
100856 assert( pOp[-1].opcode==OP_CollSeq );
100857 i = pOp[-1].p1;
100859 pCtx->skipFlag = 0;
100861 sqlite3VdbeMemRelease(pCtx->pOut);
100862 pCtx->pOut->flags = MEM_Null;
100863 pCtx->isError = 0;
100866 assert( pCtx->pOut->flags==MEM_Null );
100867 assert( pCtx->skipFlag==0 );
100900 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100901 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
100902 pMem = &aMem[pOp->p1];
100903 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
100905 if( pOp->p3 ){
100906 memAboutToChange(p, &aMem[pOp->p3]);
100907 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
100908 pMem = &aMem[pOp->p3];
100912 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
100921 REGISTER_TRACE((int)(pMem-aMem), pMem);
100928 ** Checkpoint database P1. This is a no-op if P1 is not currently in
100935 ** mem[P3+2] are initialized to -1.
100938 int i; /* Loop counter */
100939 int aRes[3]; /* Results */
100942 assert( p->readOnly==0 );
100944 aRes[1] = aRes[2] = -1;
100945 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
100946 || pOp->p2==SQLITE_CHECKPOINT_FULL
100947 || pOp->p2==SQLITE_CHECKPOINT_RESTART
100948 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
100950 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
100956 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
100973 ** Write a string containing the final journal-mode to register P2.
100978 int eNew; /* New journal mode */
100979 int eOld; /* The old journal mode */
100985 eNew = pOp->p3;
100994 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100995 assert( p->readOnly==0 );
100997 pBt = db->aDb[pOp->p1].pBt;
101012 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
101020 if( !db->autoCommit || db->nVdbeRead>1 ){
101031 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
101059 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
101060 pOut->z = (char *)sqlite3JournalModename(eNew);
101061 pOut->n = sqlite3Strlen30(pOut->z);
101062 pOut->enc = SQLITE_UTF8;
101080 assert( p->readOnly==0 );
101081 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
101082 pOp->p2 ? &aMem[pOp->p2] : 0);
101098 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101099 assert( DbMaskTest(p->btreeMask, pOp->p1) );
101100 assert( p->readOnly==0 );
101101 pBt = db->aDb[pOp->p1].pBt;
101120 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
101130 assert( pOp->p2==0 || pOp->p2==1 );
101131 if( !pOp->p1 ){
101132 sqlite3ExpirePreparedStatements(db, pOp->p2);
101134 p->expired = pOp->p2+1;
101146 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101147 pC = p->apCsr[pOp->p1];
101149 assert( pC->eCurType==CURTYPE_BTREE );
101150 sqlite3BtreeCursorPin(pC->uc.pCursor);
101161 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101162 pC = p->apCsr[pOp->p1];
101164 assert( pC->eCurType==CURTYPE_BTREE );
101165 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
101174 ** the shared-cache feature is enabled.
101180 ** P2 contains the root-page of the table to lock.
101186 u8 isWriteLock = (u8)pOp->p3;
101187 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
101188 int p1 = pOp->p1;
101189 assert( p1>=0 && p1<db->nDb );
101190 assert( DbMaskTest(p->btreeMask, p1) );
101192 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
101195 const char *z = pOp->p4.z;
101217 pVTab = pOp->p4.pVtab;
101219 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
101239 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
101240 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
101241 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
101244 assert( zTab || db->mallocFailed );
101246 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
101261 db->nVDestroy++;
101262 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
101263 db->nVDestroy--;
101264 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
101283 assert( p->bIsReader );
101286 pVtab = pOp->p4.pVtab->pVtab;
101287 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
101291 pModule = pVtab->pModule;
101292 rc = pModule->xOpen(pVtab, &pVCur);
101297 pVCur->pVtab = pVtab;
101300 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
101302 pCur->uc.pVCur = pVCur;
101303 pVtab->nRef++;
101305 assert( db->mallocFailed );
101306 pModule->xClose(pVCur);
101329 pOut = &aMem[pOp->p2];
101331 assert( pOp->p4type==P4_TABLEREF );
101332 pTab = pOp->p4.pTab;
101334 assert( pTab->nTabRef>0 );
101336 if( pTab->u.vtab.p==0 ) break;
101337 pVtab = pTab->u.vtab.p->pVtab;
101339 pModule = pVtab->pModule;
101341 assert( pModule->iVersion>=4 );
101342 assert( pModule->xIntegrity!=0 );
101343 sqlite3VtabLock(pTab->u.vtab.p);
101344 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101345 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
101346 pOp->p3, &zErr);
101347 sqlite3VtabUnlock(pTab->u.vtab.p);
101353 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free);
101374 pC = p->apCsr[pOp->p1];
101377 pRhs->pCsr = pC->uc.pCursor;
101378 pRhs->pOut = &aMem[pOp->p3];
101380 pOut->flags = MEM_Null;
101408 int nArg;
101409 int iQuery;
101416 int res;
101417 int i;
101420 pQuery = &aMem[pOp->p3];
101422 pCur = p->apCsr[pOp->p1];
101424 REGISTER_TRACE(pOp->p3, pQuery);
101426 assert( pCur->eCurType==CURTYPE_VTAB );
101427 pVCur = pCur->uc.pVCur;
101428 pVtab = pVCur->pVtab;
101429 pModule = pVtab->pModule;
101432 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
101433 nArg = (int)pArgc->u.i;
101434 iQuery = (int)pQuery->u.i;
101437 apArg = p->apArg;
101441 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
101444 res = pModule->xEof(pVCur);
101445 pCur->nullRow = 0;
101456 ** Store in register P3 the value of the P2-th column of
101457 ** the current row of the virtual-table of cursor P1.
101474 VdbeCursor *pCur = p->apCsr[pOp->p1];
101476 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
101477 pDest = &aMem[pOp->p3];
101479 if( pCur->nullRow ){
101483 assert( pCur->eCurType==CURTYPE_VTAB );
101484 pVtab = pCur->uc.pVCur->pVtab;
101485 pModule = pVtab->pModule;
101486 assert( pModule->xColumn );
101493 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
101494 if( pOp->p5 & OPFLAG_NOCHNG ){
101496 pDest->flags = MEM_Null|MEM_Zero;
101497 pDest->u.nZero = 0;
101501 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
101508 REGISTER_TRACE(pOp->p3, pDest);
101526 int res;
101529 pCur = p->apCsr[pOp->p1];
101531 assert( pCur->eCurType==CURTYPE_VTAB );
101532 if( pCur->nullRow ){
101535 pVtab = pCur->uc.pVCur->pVtab;
101536 pModule = pVtab->pModule;
101537 assert( pModule->xNext );
101545 rc = pModule->xNext(pCur->uc.pVCur);
101548 res = pModule->xEof(pCur->uc.pVCur);
101568 int isLegacy;
101570 isLegacy = (db->flags & SQLITE_LegacyAlter);
101571 db->flags |= SQLITE_LegacyAlter;
101572 pVtab = pOp->p4.pVtab->pVtab;
101573 pName = &aMem[pOp->p1];
101574 assert( pVtab->pModule->xRename );
101576 assert( p->readOnly==0 );
101577 REGISTER_TRACE(pOp->p1, pName);
101578 assert( pName->flags & MEM_Str );
101579 testcase( pName->enc==SQLITE_UTF8 );
101580 testcase( pName->enc==SQLITE_UTF16BE );
101581 testcase( pName->enc==SQLITE_UTF16LE );
101584 rc = pVtab->pModule->xRename(pVtab, pName->z);
101585 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
101587 p->expired = 0;
101600 ** invocation. The value in register (P3+P2-1) corresponds to the
101624 int nArg;
101625 int i;
101630 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
101631 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
101633 assert( p->readOnly==0 );
101634 if( db->mallocFailed ) goto no_mem;
101636 pVtab = pOp->p4.pVtab->pVtab;
101637 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
101641 pModule = pVtab->pModule;
101642 nArg = pOp->p2;
101643 assert( pOp->p4type==P4_VTAB );
101644 if( ALWAYS(pModule->xUpdate) ){
101645 u8 vtabOnConflict = db->vtabOnConflict;
101646 apArg = p->apArg;
101647 pX = &aMem[pOp->p3];
101654 db->vtabOnConflict = pOp->p5;
101655 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
101656 db->vtabOnConflict = vtabOnConflict;
101658 if( rc==SQLITE_OK && pOp->p1 ){
101659 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
101660 db->lastRowid = rowid;
101662 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
101663 if( pOp->p5==OE_Ignore ){
101666 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
101669 p->nChange++;
101684 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
101700 unsigned int newMax;
101704 pBt = db->aDb[pOp->p1].pBt;
101706 if( pOp->p3 ){
101708 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
101710 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
101725 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
101744 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
101753 ** purely non-deterministic. Some built-in date/time functions can be
101754 ** either deterministic of non-deterministic, depending on their arguments.
101755 ** When those function are used in a non-deterministic way, they will check
101763 int i;
101766 assert( pOp->p4type==P4_FUNCCTX );
101767 pCtx = pOp->p4.pCtx;
101773 pOut = &aMem[pOp->p3];
101774 if( pCtx->pOut != pOut ){
101775 pCtx->pVdbe = p;
101776 pCtx->pOut = pOut;
101777 pCtx->enc = encoding;
101778 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
101780 assert( pCtx->pVdbe==p );
101784 for(i=0; i<pCtx->argc; i++){
101785 assert( memIsValid(pCtx->argv[i]) );
101786 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
101790 assert( pCtx->isError==0 );
101791 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
101794 if( pCtx->isError ){
101795 if( pCtx->isError>0 ){
101797 rc = pCtx->isError;
101799 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
101800 pCtx->isError = 0;
101804 assert( (pOut->flags&MEM_Str)==0
101805 || pOut->enc==encoding
101806 || db->mallocFailed );
101809 REGISTER_TRACE(pOp->p3, pOut);
101820 pIn1 = &aMem[pOp->p1];
101821 pIn1->flags &= ~MEM_Subtype;
101832 pIn1 = &aMem[pOp->p1];
101833 pOut = &aMem[pOp->p2];
101834 if( pIn1->flags & MEM_Subtype ){
101835 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype);
101849 pIn1 = &aMem[pOp->p1];
101850 pOut = &aMem[pOp->p2];
101851 if( pIn1->flags & MEM_Null ){
101852 pOut->flags &= ~MEM_Subtype;
101854 assert( pIn1->flags & MEM_Int );
101855 pOut->flags |= MEM_Subtype;
101856 pOut->eSubtype = (u8)(pIn1->u.i & 0xff);
101870 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
101871 pIn1 = &aMem[pOp->p1];
101872 assert( pIn1->flags & MEM_Blob );
101873 assert( pIn1->n>0 );
101876 if( db->flags&SQLITE_VdbeTrace ){
101877 int ii;
101878 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101881 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101884 h %= (pIn1->n*8);
101885 pIn1->z[h/8] |= 1<<(h&7);
101901 ** false positive - if the jump is taken when it should fall through.
101906 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
101907 pIn1 = &aMem[pOp->p1];
101908 assert( (pIn1->flags & MEM_Blob)!=0 );
101909 assert( pIn1->n >= 1 );
101912 if( db->flags&SQLITE_VdbeTrace ){
101913 int ii;
101914 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101917 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101920 h %= (pIn1->n*8);
101921 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
101923 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
101926 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
101946 ** the UTF-8 string contained in P4 is emitted on the trace callback.
101959 int i;
101965 ** The "--" string is broken up to prevent false-positives with srcck1.c.
101968 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
101970 ** using the X argument when X begins with "--" and invoking
101973 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
101976 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
101979 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
101980 && p->minWriteFileFormat!=254 /* tag-20220401a */
101981 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
101984 if( db->mTrace & SQLITE_TRACE_LEGACY ){
101986 db->trace.xLegacy(db->pTraceArg, z);
101990 if( db->nVdbeExec>1 ){
101991 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
101992 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
101995 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
101999 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
102001 int j;
102002 for(j=0; j<db->nDb; j++){
102003 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
102004 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
102009 if( (db->flags & SQLITE_SqlTrace)!=0
102010 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
102012 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
102016 assert( pOp->p2>0 );
102017 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
102018 if( pOp->opcode==OP_Trace ) break;
102019 for(i=1; i<p->nOp; i++){
102020 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
102022 pOp->p1 = 0;
102024 pOp->p1++;
102025 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
102035 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
102040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
102041 assert( pOp->p4type==P4_EXPR );
102042 pC = p->apCsr[pOp->p1];
102044 assert( pC->eCurType==CURTYPE_BTREE );
102045 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
102046 pOp->p4.pExpr, aMem);
102101 int i;
102103 assert( pOp->p1>0 );
102104 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
102105 pMem = &aMem[pOp->p1];
102106 constMask = pOp->p3;
102107 for(i=0; i<pOp->p2; i++, pMem++){
102109 pMem->pScopyFrom = 0;
102110 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
102126 ** the same as a no-op. This opcodesnever appears in a real VM program.
102129 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
102136 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
102158 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
102161 if( db->flags & SQLITE_VdbeTrace ){
102162 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
102165 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
102168 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
102185 if( db->mallocFailed ){
102192 if( db->flags & SQLITE_VdbeTrace ){
102193 const char *zTrace = p->zSql;
102200 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
102203 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
102206 p->rc = rc;
102210 (int)(pOp - aOp), p->zSql, p->zErrMsg);
102211 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
102213 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
102214 db->flags |= SQLITE_CorruptRdOnly;
102218 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
102238 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
102239 nProgressLimit += db->nProgressOps;
102240 if( db->xProgress(db->pProgressArg) ){
102247 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
102248 if( DbMaskNonZero(p->lockMask) ){
102252 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
102276 assert( AtomicLoad(&db->u1.isInterrupted) );
102309 int nByte; /* Size of open blob, in bytes */
102310 int iOffset; /* Byte offset of blob in cursor data */
102322 ** the b-tree cursor associated with blob handle p to point to row iRow.
102333 ** If an error does occur, then the b-tree cursor is closed. All subsequent
102337 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
102338 int rc; /* Error code */
102340 Vdbe *v = (Vdbe *)p->pStmt;
102345 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
102351 if( v->pc>4 ){
102352 v->pc = 4;
102353 assert( v->aOp[v->pc].opcode==OP_NotExists );
102356 rc = sqlite3_step(p->pStmt);
102359 VdbeCursor *pC = v->apCsr[0];
102362 assert( pC->eCurType==CURTYPE_BTREE );
102363 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
102364 testcase( pC->nHdrParsed==p->iCol );
102365 testcase( pC->nHdrParsed==p->iCol+1 );
102367 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
102371 sqlite3_finalize(p->pStmt);
102372 p->pStmt = 0;
102374 p->iOffset = pC->aType[p->iCol + pC->nField];
102375 p->nByte = sqlite3VdbeSerialTypeLen(type);
102376 p->pCsr = pC->uc.pCursor;
102377 sqlite3BtreeIncrblobCursor(p->pCsr);
102383 }else if( p->pStmt ){
102384 rc = sqlite3_finalize(p->pStmt);
102385 p->pStmt = 0;
102387 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
102390 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
102404 SQLITE_API int sqlite3_blob_open(
102410 int wrFlag, /* True -> read/write access, false -> read-only */
102413 int nAttempt = 0;
102414 int iCol; /* Index of zColumn in row-record */
102415 int rc = SQLITE_OK;
102434 sqlite3_mutex_enter(db->mutex);
102469 pBlob->pTab = pTab;
102470 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
102473 for(iCol=0; iCol<pTab->nCol; iCol++) {
102474 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
102478 if( iCol==pTab->nCol ){
102493 if( db->flags&SQLITE_ForeignKeys ){
102500 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
102501 int j;
102502 for(j=0; j<pFKey->nCol; j++){
102503 if( pFKey->aCol[j].iFrom==iCol ){
102510 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102511 int j;
102512 for(j=0; j<pIdx->nKeyCol; j++){
102514 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
102528 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
102529 assert( pBlob->pStmt || db->mallocFailed );
102530 if( pBlob->pStmt ){
102534 ** of writing code to use the b-tree layer directly is that the
102539 ** Code external to the Vdbe then "borrows" the b-tree cursor and
102544 ** which closes the b-tree cursor and (possibly) commits the
102547 static const int iLn = VDBE_OFFSET_LINENO(2);
102557 Vdbe *v = (Vdbe *)pBlob->pStmt;
102558 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102562 pTab->pSchema->schema_cookie,
102563 pTab->pSchema->iGeneration);
102565 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
102571 if( db->mallocFailed==0 ){
102578 aOp[0].p2 = pTab->tnum;
102580 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
102582 if( db->mallocFailed==0 ){
102586 ** parameter of the other to pTab->tnum. */
102588 aOp[1].p2 = pTab->tnum;
102599 aOp[1].p4.i = pTab->nCol+1;
102600 aOp[3].p2 = pTab->nCol;
102609 pBlob->iCol = iCol;
102610 pBlob->db = db;
102612 if( db->mallocFailed ){
102621 if( rc==SQLITE_OK && db->mallocFailed==0 ){
102624 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
102631 sqlite3_mutex_leave(db->mutex);
102639 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
102641 int rc;
102645 sqlite3_stmt *pStmt = p->pStmt;
102646 db = p->db;
102647 sqlite3_mutex_enter(db->mutex);
102649 sqlite3_mutex_leave(db->mutex);
102660 static int blobReadWrite(
102663 int n,
102664 int iOffset,
102665 int (*xCall)(BtCursor*, u32, u32, void*)
102667 int rc;
102673 db = p->db;
102674 sqlite3_mutex_enter(db->mutex);
102675 v = (Vdbe*)p->pStmt;
102677 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
102681 /* If there is no statement handle, then the blob-handle has
102687 ** returned, clean-up the statement handle.
102689 assert( db == v->db );
102690 sqlite3BtreeEnterCursor(p->pCsr);
102693 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
102694 /* If a pre-update hook is registered and this is a write cursor,
102697 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
102704 ** using the incremental-blob API, this works. For the sessions module
102708 iKey = sqlite3BtreeIntegerKey(p->pCsr);
102709 assert( v->apCsr[0]!=0 );
102710 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
102712 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
102717 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
102718 sqlite3BtreeLeaveCursor(p->pCsr);
102721 p->pStmt = 0;
102723 v->rc = rc;
102728 sqlite3_mutex_leave(db->mutex);
102735 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
102742 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
102752 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
102754 return (p && p->pStmt) ? p->nByte : 0;
102767 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
102768 int rc;
102773 db = p->db;
102774 sqlite3_mutex_enter(db->mutex);
102776 if( p->pStmt==0 ){
102777 /* If there is no statement handle, then the blob-handle has
102783 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
102793 assert( rc==SQLITE_OK || p->pStmt==0 );
102794 sqlite3_mutex_leave(db->mutex);
102803 ** 2011-07-09
102818 ** The VdbeSorter object implements a multi-threaded external merge sort
102822 ** Here is the (internal, non-API) interface between this module and the
102875 ** an in-memory merge sort. In this case, no temporary files are required
102882 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
102883 ** of PMAs may be created by merging existing PMAs together - for example
102884 ** merging two or more level-0 PMAs together creates a level-1 PMA.
102888 ** page-cache of the main database. Specifically, the threshold is set to
102892 ** If the sorter is running in single-threaded mode, then all PMAs generated
102894 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
102901 ** The sorter is running in multi-threaded mode if (a) the library was built
102902 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
102911 ** sorter is running in single-threaded mode, then these PMAs are merged
102916 ** Or, if running in multi-threaded mode, then a background thread is
102926 ** Rewind() is called, then a hierarchy of incremental-merges is used.
102932 ** If running in multi-threaded mode and there are more than
102945 ** characteristics of the sorter in multi-threaded mode.
102952 ** Hard-coded maximum amount of data to accumulate in memory before flushing
102965 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
102967 typedef struct SorterList SorterList; /* In-memory list of records */
102980 ** An in-memory list of objects to be sorted.
102989 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
103012 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
103026 ** aReadr[0] -> Banana
103027 ** aReadr[1] -> Feijoa
103028 ** aReadr[2] -> Elderberry
103029 ** aReadr[3] -> Currant
103030 ** aReadr[4] -> Grapefruit
103031 ** aReadr[5] -> Apple
103032 ** aReadr[6] -> Durian
103033 ** aReadr[7] -> EOF
103042 ** aReadr[5] -> Eggplant
103047 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
103058 int nTree; /* Used size of aTree/aReadr (power of 2) */
103060 int *aTree; /* Current state of incremental merge */
103070 ** single-threaded operation, there is exactly one instance of this object
103071 ** and for multi-threaded operation there are two or more instances.
103082 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
103095 typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
103098 int bDone; /* Set if thread is finished but not joined */
103099 int nPMA; /* Number of PMAs currently in file */
103100 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
103104 SorterFile file; /* Temp file for level-0 PMAs */
103119 int mnPmaSize; /* Minimum PMA size, in bytes */
103120 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
103121 int mxKeysize; /* Largest serialized key seen so far */
103122 int pgsz; /* Main database page size */
103128 SorterList list; /* List of in-memory records */
103129 int iMemory; /* Offset of free space in list.aMemory */
103130 int nMemory; /* Size of list.aMemory allocation in bytes */
103154 int nAlloc; /* Bytes of space at aAlloc */
103155 int nKey; /* Number of bytes in key */
103160 int nBuffer; /* Size of read buffer in bytes */
103171 ** There are two types of IncrMerger object - single (bUseThread==0) and
103172 ** multi-threaded (bUseThread==1).
103174 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
103189 ** A single-threaded IncrMerger does not open any temporary files of its
103191 ** at offset iStartOff of file pTask->file2. And instead of using a
103193 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
103201 int mxSz; /* Maximum bytes of data to store */
103202 int bEof; /* Set to true when merge is finished */
103203 int bUseThread; /* True to use a bg thread for this object */
103211 ** size. But I/O is more efficient if it occurs in page-sized blocks where
103213 ** the PMA so that aligned, page-size blocks are written.
103216 int eFWErr; /* Non-zero if in an error state */
103218 int nBuffer; /* Size of write buffer in bytes */
103219 int iBufStart; /* First byte of buffer to write */
103220 int iBufEnd; /* Last byte of buffer to write */
103230 ** by this module. If using a separate allocation for each in-memory record
103238 ** has finished passing records to the sorter, or when the in-memory buffer
103244 int nVal; /* Size of the record in bytes */
103247 int iNext; /* Offset within aMemory of next record */
103263 static int vdbeIncrSwap(IncrMerger*);
103271 sqlite3_free(pReadr->aAlloc);
103272 sqlite3_free(pReadr->aBuffer);
103273 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103274 vdbeIncrFree(pReadr->pIncr);
103287 static int vdbePmaReadBlob(
103289 int nByte, /* Bytes of data to read */
103292 int iBuf; /* Offset within buffer to read from */
103293 int nAvail; /* Bytes of data available in buffer */
103295 if( p->aMap ){
103296 *ppOut = &p->aMap[p->iReadOff];
103297 p->iReadOff += nByte;
103301 assert( p->aBuffer );
103304 ** p->nBuffer bytes of data from the file into it. Or, if there are less
103305 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
103306 iBuf = p->iReadOff % p->nBuffer;
103308 int nRead; /* Bytes to read from disk */
103309 int rc; /* sqlite3OsRead() return code */
103312 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
103313 nRead = p->nBuffer;
103315 nRead = (int)(p->iEof - p->iReadOff);
103320 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
103324 nAvail = p->nBuffer - iBuf;
103327 /* The requested data is available in the in-memory buffer. In this
103330 *ppOut = &p->aBuffer[iBuf];
103331 p->iReadOff += nByte;
103333 /* The requested data is not all available in the in-memory buffer.
103334 ** In this case, allocate space at p->aAlloc[] to copy the requested
103335 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
103336 int nRem; /* Bytes remaining to copy */
103338 /* Extend the p->aAlloc[] allocation if required. */
103339 if( p->nAlloc<nByte ){
103341 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
103343 aNew = sqlite3Realloc(p->aAlloc, nNew);
103345 p->nAlloc = nNew;
103346 p->aAlloc = aNew;
103350 ** p->aAlloc[]. */
103351 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
103352 p->iReadOff += nAvail;
103353 nRem = nByte - nAvail;
103355 /* The following loop copies up to p->nBuffer bytes per iteration into
103356 ** the p->aAlloc[] buffer. */
103358 int rc; /* vdbePmaReadBlob() return code */
103359 int nCopy; /* Number of bytes to copy */
103363 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
103366 assert( aNext!=p->aAlloc );
103367 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
103368 nRem -= nCopy;
103371 *ppOut = p->aAlloc;
103381 static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
103382 int iBuf;
103384 if( p->aMap ){
103385 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
103387 iBuf = p->iReadOff % p->nBuffer;
103388 if( iBuf && (p->nBuffer-iBuf)>=9 ){
103389 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
103392 int i = 0, rc;
103414 static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
103415 int rc = SQLITE_OK;
103416 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
103417 sqlite3_file *pFd = pFile->pFd;
103418 if( pFd->pMethods->iVersion>=3 ){
103419 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
103431 static int vdbePmaReaderSeek(
103437 int rc = SQLITE_OK;
103439 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
103442 if( pReadr->aMap ){
103443 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103444 pReadr->aMap = 0;
103446 pReadr->iReadOff = iOff;
103447 pReadr->iEof = pFile->iEof;
103448 pReadr->pFd = pFile->pFd;
103450 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
103451 if( rc==SQLITE_OK && pReadr->aMap==0 ){
103452 int pgsz = pTask->pSorter->pgsz;
103453 int iBuf = pReadr->iReadOff % pgsz;
103454 if( pReadr->aBuffer==0 ){
103455 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
103456 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
103457 pReadr->nBuffer = pgsz;
103460 int nRead = pgsz - iBuf;
103461 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
103462 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
103465 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
103478 static int vdbePmaReaderNext(PmaReader *pReadr){
103479 int rc = SQLITE_OK; /* Return Code */
103483 if( pReadr->iReadOff>=pReadr->iEof ){
103484 IncrMerger *pIncr = pReadr->pIncr;
103485 int bEof = 1;
103488 if( rc==SQLITE_OK && pIncr->bEof==0 ){
103490 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
103508 pReadr->nKey = (int)nRec;
103509 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
103518 ** starting at offset iStart and ending at offset iEof-1. This function
103525 static int vdbePmaReaderInit(
103532 int rc;
103534 assert( pFile->iEof>iStart );
103535 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
103536 assert( pReadr->aBuffer==0 );
103537 assert( pReadr->aMap==0 );
103543 pReadr->iEof = pReadr->iReadOff + nByte;
103558 static int vdbeSorterCompareTail(
103560 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103561 const void *pKey1, int nKey1, /* Left side of comparison */
103562 const void *pKey2, int nKey2 /* Right side of comparison */
103564 UnpackedRecord *r2 = pTask->pUnpacked;
103566 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103574 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
103578 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
103579 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
103582 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
103585 static int vdbeSorterCompare(
103587 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103588 const void *pKey1, int nKey1, /* Left side of comparison */
103589 const void *pKey2, int nKey2 /* Right side of comparison */
103591 UnpackedRecord *r2 = pTask->pUnpacked;
103593 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103604 static int vdbeSorterCompareText(
103606 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103607 const void *pKey1, int nKey1, /* Left side of comparison */
103608 const void *pKey2, int nKey2 /* Right side of comparison */
103615 int n1;
103616 int n2;
103617 int res;
103621 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
103623 res = n1 - n2;
103627 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
103633 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
103634 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
103635 res = res * -1;
103646 static int vdbeSorterCompareInt(
103648 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103649 const void *pKey1, int nKey1, /* Left side of comparison */
103650 const void *pKey2, int nKey2 /* Right side of comparison */
103654 const int s1 = p1[1]; /* Left hand serial type */
103655 const int s2 = p2[1]; /* Right hand serial type */
103658 int res; /* Return value */
103667 int i;
103670 if( (res = v1[i] - v2[i])!=0 ){
103672 res = v1[0] & 0x80 ? -1 : +1;
103678 res = s1 - s2;
103683 res = -1;
103685 res = s1 - s2;
103690 if( *v1 & 0x80 ) res = -1;
103697 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
103702 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
103703 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
103704 res = res * -1;
103713 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
103716 ** is non-zero and the sorter is able to guarantee a stable sort, nField
103720 ** of the records being sorted. So if the sort is stable, there is never
103724 ** The sorter can guarantee a stable sort when running in single-threaded
103725 ** mode, but not in multi-threaded mode.
103729 SQLITE_PRIVATE int sqlite3VdbeSorterInit(
103731 int nField, /* Number of key fields in each record */
103734 int pgsz; /* Page size of main database */
103735 int i; /* Used to iterate through aTask[] */
103737 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
103738 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
103739 int sz; /* Size of pSorter in bytes */
103740 int rc = SQLITE_OK;
103744 int nWorker;
103752 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
103760 nWorker = SORTER_MAX_MERGE_COUNT-1;
103764 assert( pCsr->pKeyInfo );
103765 assert( !pCsr->isEphemeral );
103766 assert( pCsr->eCurType==CURTYPE_SORTER );
103767 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
103771 pCsr->uc.pSorter = pSorter;
103775 Btree *pBt = db->aDb[0].pBt;
103776 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
103777 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
103778 pKeyInfo->db = 0;
103780 pKeyInfo->nKeyField = nField;
103783 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
103785 pSorter->nTask = nWorker + 1;
103786 pSorter->iPrev = (u8)(nWorker - 1);
103787 pSorter->bUseThreads = (pSorter->nTask>1);
103788 pSorter->db = db;
103789 for(i=0; i<pSorter->nTask; i++){
103790 SortSubtask *pTask = &pSorter->aTask[i];
103791 pTask->pSorter = pSorter;
103797 pSorter->mnPmaSize = szPma * pgsz;
103799 mxCache = db->aDb[0].pSchema->cache_size;
103801 /* A negative cache-size value C indicates that the cache is abs(C)
103803 mxCache = mxCache * -1024;
103808 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
103813 assert( pSorter->iMemory==0 );
103814 pSorter->nMemory = pgsz;
103815 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
103816 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
103820 if( pKeyInfo->nAllField<13
103821 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
103822 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
103824 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
103839 pNext = p->u.pNext;
103849 sqlite3DbFree(db, pTask->pUnpacked);
103851 /* pTask->list.aMemory can only be non-zero if it was handed memory
103853 if( pTask->list.aMemory ){
103854 sqlite3_free(pTask->list.aMemory);
103858 assert( pTask->list.aMemory==0 );
103859 vdbeSorterRecordFree(0, pTask->list.pList);
103861 if( pTask->file.pFd ){
103862 sqlite3OsCloseFree(pTask->file.pFd);
103864 if( pTask->file2.pFd ){
103865 sqlite3OsCloseFree(pTask->file2.pFd);
103873 int iTask = (pTask - pTask->pSorter->aTask);
103874 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103888 int iTask = (pTask - pTask->pSorter->aTask);
103889 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103894 int bBlocked,
103899 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103912 ** Join thread pTask->thread.
103914 static int vdbeSorterJoinThread(SortSubtask *pTask){
103915 int rc = SQLITE_OK;
103916 if( pTask->pThread ){
103918 int bDone = pTask->bDone;
103922 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
103925 assert( pTask->bDone==1 );
103926 pTask->bDone = 0;
103927 pTask->pThread = 0;
103935 static int vdbeSorterCreateThread(
103940 assert( pTask->pThread==0 && pTask->bDone==0 );
103941 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
103946 ** level-0 PMAs.
103948 static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
103949 int rc = rcin;
103950 int i;
103955 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
103958 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
103959 for(i=pSorter->nTask-1; i>=0; i--){
103960 SortSubtask *pTask = &pSorter->aTask[i];
103961 int rc2 = vdbeSorterJoinThread(pTask);
103978 static MergeEngine *vdbeMergeEngineNew(int nReader){
103979 int N = 2; /* Smallest power of two >= nReader */
103980 int nByte; /* Total bytes of space to allocate */
103986 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
103990 pNew->nTree = N;
103991 pNew->pTask = 0;
103992 pNew->aReadr = (PmaReader*)&pNew[1];
103993 pNew->aTree = (int*)&pNew->aReadr[N];
104002 int i;
104004 for(i=0; i<pMerger->nTree; i++){
104005 vdbePmaReaderClear(&pMerger->aReadr[i]);
104018 if( pIncr->bUseThread ){
104019 vdbeSorterJoinThread(pIncr->pTask);
104020 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
104021 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
104024 vdbeMergeEngineFree(pIncr->pMerger);
104033 int i;
104035 assert( pSorter->bUseThreads || pSorter->pReader==0 );
104037 if( pSorter->pReader ){
104038 vdbePmaReaderClear(pSorter->pReader);
104039 sqlite3DbFree(db, pSorter->pReader);
104040 pSorter->pReader = 0;
104043 vdbeMergeEngineFree(pSorter->pMerger);
104044 pSorter->pMerger = 0;
104045 for(i=0; i<pSorter->nTask; i++){
104046 SortSubtask *pTask = &pSorter->aTask[i];
104048 pTask->pSorter = pSorter;
104050 if( pSorter->list.aMemory==0 ){
104051 vdbeSorterRecordFree(0, pSorter->list.pList);
104053 pSorter->list.pList = 0;
104054 pSorter->list.szPMA = 0;
104055 pSorter->bUsePMA = 0;
104056 pSorter->iMemory = 0;
104057 pSorter->mxKeysize = 0;
104058 sqlite3DbFree(db, pSorter->pUnpacked);
104059 pSorter->pUnpacked = 0;
104067 assert( pCsr->eCurType==CURTYPE_SORTER );
104068 pSorter = pCsr->uc.pSorter;
104071 sqlite3_free(pSorter->list.aMemory);
104073 pCsr->uc.pSorter = 0;
104079 ** The first argument is a file-handle open on a temporary file. The file
104088 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
104090 int chunksize = 4*1024;
104093 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
104102 ** Allocate space for a file-handle and open a temporary file. If successful,
104103 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
104106 static int vdbeSorterOpenTempFile(
104111 int rc;
104113 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
104130 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
104133 static int vdbeSortAllocUnpacked(SortSubtask *pTask){
104134 if( pTask->pUnpacked==0 ){
104135 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
104136 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
104137 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
104138 pTask->pUnpacked->errCode = 0;
104154 int bCached = 0;
104158 int res;
104159 res = pTask->xCompare(
104160 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
104165 pp = &p1->u.pNext;
104166 p1 = p1->u.pNext;
104173 pp = &p2->u.pNext;
104174 p2 = p2->u.pNext;
104190 if( p->typeMask==SORTER_TYPE_INTEGER ){
104192 }else if( p->typeMask==SORTER_TYPE_TEXT ){
104199 ** Sort the linked list of records headed at pTask->pList. Return
104203 static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
104204 int i;
104206 int rc;
104212 p = pList->pList;
104213 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
104218 if( pList->aMemory ){
104219 if( (u8*)p==pList->aMemory ){
104222 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
104223 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
104226 pNext = p->u.pNext;
104229 p->u.pNext = 0;
104243 pList->pList = p;
104245 assert( pTask->pUnpacked->errCode==SQLITE_OK
104246 || pTask->pUnpacked->errCode==SQLITE_NOMEM
104248 return pTask->pUnpacked->errCode;
104252 ** Initialize a PMA-writer object.
104257 int nBuf, /* Buffer size */
104261 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
104262 if( !p->aBuffer ){
104263 p->eFWErr = SQLITE_NOMEM_BKPT;
104265 p->iBufEnd = p->iBufStart = (iStart % nBuf);
104266 p->iWriteOff = iStart - p->iBufStart;
104267 p->nBuffer = nBuf;
104268 p->pFd = pFd;
104276 static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
104277 int nRem = nData;
104278 while( nRem>0 && p->eFWErr==0 ){
104279 int nCopy = nRem;
104280 if( nCopy>(p->nBuffer - p->iBufEnd) ){
104281 nCopy = p->nBuffer - p->iBufEnd;
104284 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
104285 p->iBufEnd += nCopy;
104286 if( p->iBufEnd==p->nBuffer ){
104287 p->eFWErr = sqlite3OsWrite(p->pFd,
104288 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104289 p->iWriteOff + p->iBufStart
104291 p->iBufStart = p->iBufEnd = 0;
104292 p->iWriteOff += p->nBuffer;
104294 assert( p->iBufEnd<p->nBuffer );
104296 nRem -= nCopy;
104301 ** Flush any buffered data to disk and clean up the PMA-writer object.
104302 ** The results of using the PMA-writer after this call are undefined.
104309 static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
104310 int rc;
104311 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
104312 p->eFWErr = sqlite3OsWrite(p->pFd,
104313 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104314 p->iWriteOff + p->iBufStart
104317 *piEof = (p->iWriteOff + p->iBufEnd);
104318 sqlite3_free(p->aBuffer);
104319 rc = p->eFWErr;
104329 int nByte;
104336 ** Write the current contents of in-memory linked-list pList to a level-0
104337 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
104345 ** * One or more records packed end-to-end in order of ascending keys.
104349 static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
104350 sqlite3 *db = pTask->pSorter->db;
104351 int rc = SQLITE_OK; /* Return code */
104355 /* Set iSz to the expected size of file pTask->file after writing the PMA.
104357 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
104362 assert( pList->szPMA>0 );
104365 if( pTask->file.pFd==0 ){
104366 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
104367 assert( rc!=SQLITE_OK || pTask->file.pFd );
104368 assert( pTask->file.iEof==0 );
104369 assert( pTask->nPMA==0 );
104374 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
104386 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
104387 pTask->file.iEof);
104388 pTask->nPMA++;
104389 vdbePmaWriteVarint(&writer, pList->szPMA);
104390 for(p=pList->pList; p; p=pNext){
104391 pNext = p->u.pNext;
104392 vdbePmaWriteVarint(&writer, p->nVal);
104393 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
104394 if( pList->aMemory==0 ) sqlite3_free(p);
104396 pList->pList = p;
104397 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
104401 assert( rc!=SQLITE_OK || pList->pList==0 );
104402 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
104413 static int vdbeMergeEngineStep(
104415 int *pbEof /* Set TRUE at EOF. Set false for more content */
104417 int rc;
104418 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
104419 SortSubtask *pTask = pMerger->pTask;
104422 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
104426 int i; /* Index of aTree[] to recalculate */
104429 int bCached = 0;
104433 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
104434 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
104436 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
104438 int iRes;
104439 if( pReadr1->pFd==0 ){
104441 }else if( pReadr2->pFd==0 ){
104442 iRes = -1;
104444 iRes = pTask->xCompare(pTask, &bCached,
104445 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
104451 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
104456 ** was actually called above, then pTask->pUnpacked now contains
104465 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
104466 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104469 if( pReadr1->pFd ) bCached = 0;
104470 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
104471 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104474 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
104477 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
104482 ** The main routine for background threads that write level-0 PMAs.
104486 int rc; /* Return code */
104487 assert( pTask->bDone==0 );
104488 rc = vdbeSorterListToPMA(pTask, &pTask->list);
104489 pTask->bDone = 1;
104498 static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
104500 pSorter->bUsePMA = 1;
104501 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
104503 int rc = SQLITE_OK;
104504 int i;
104506 int nWorker = (pSorter->nTask-1);
104510 pSorter->bUsePMA = 1;
104512 /* Select a sub-task to sort and flush the current list of in-memory
104513 ** records to disk. If the sorter is running in multi-threaded mode,
104514 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
104515 ** the background thread from a sub-tasks previous turn is still running,
104516 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
104517 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
104518 ** sub-tasks are preferred as they use background threads - the final
104519 ** sub-task uses the main thread. */
104521 int iTest = (pSorter->iPrev + i + 1) % nWorker;
104522 pTask = &pSorter->aTask[iTest];
104523 if( pTask->bDone ){
104526 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
104532 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
104539 assert( pTask->pThread==0 && pTask->bDone==0 );
104540 assert( pTask->list.pList==0 );
104541 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
104543 aMem = pTask->list.aMemory;
104545 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
104546 pTask->list = pSorter->list;
104547 pSorter->list.pList = 0;
104548 pSorter->list.szPMA = 0;
104550 pSorter->list.aMemory = aMem;
104551 pSorter->nMemory = sqlite3MallocSize(aMem);
104552 }else if( pSorter->list.aMemory ){
104553 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
104554 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
104568 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
104573 int rc = SQLITE_OK; /* Return Code */
104575 int bFlush; /* True to flush contents of memory to PMA */
104578 int t; /* serial type of first record field */
104580 assert( pCsr->eCurType==CURTYPE_SORTER );
104581 pSorter = pCsr->uc.pSorter;
104582 getVarint32NR((const u8*)&pVal->z[1], t);
104584 pSorter->typeMask &= SORTER_TYPE_INTEGER;
104586 pSorter->typeMask &= SORTER_TYPE_TEXT;
104588 pSorter->typeMask = 0;
104596 ** If using the single large allocation mode (pSorter->aMemory!=0), then
104603 ** * The total memory allocated for the in-memory list is greater
104604 ** than (page-size * cache-size), or
104606 ** * The total memory allocated for the in-memory list is greater
104607 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
104609 nReq = pVal->n + sizeof(SorterRecord);
104610 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
104611 if( pSorter->mxPmaSize ){
104612 if( pSorter->list.aMemory ){
104613 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
104616 (pSorter->list.szPMA > pSorter->mxPmaSize)
104617 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
104622 pSorter->list.szPMA = 0;
104623 pSorter->iMemory = 0;
104624 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
104628 pSorter->list.szPMA += nPMA;
104629 if( nPMA>pSorter->mxKeysize ){
104630 pSorter->mxKeysize = nPMA;
104633 if( pSorter->list.aMemory ){
104634 int nMin = pSorter->iMemory + nReq;
104636 if( nMin>pSorter->nMemory ){
104638 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
104639 int iListOff = -1;
104640 if( pSorter->list.pList ){
104641 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
104644 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
104646 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
104649 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
104651 pSorter->list.aMemory = aNew;
104652 pSorter->nMemory = nNew;
104655 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
104656 pSorter->iMemory += ROUND8(nReq);
104657 if( pSorter->list.pList ){
104658 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
104665 pNew->u.pNext = pSorter->list.pList;
104668 memcpy(SRVAL(pNew), pVal->z, pVal->n);
104669 pNew->nVal = pVal->n;
104670 pSorter->list.pList = pNew;
104676 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
104678 ** except that the number-of-bytes varint is omitted from the start.
104680 static int vdbeIncrPopulate(IncrMerger *pIncr){
104681 int rc = SQLITE_OK;
104682 int rc2;
104683 i64 iStart = pIncr->iStartOff;
104684 SorterFile *pOut = &pIncr->aFile[1];
104685 SortSubtask *pTask = pIncr->pTask;
104686 MergeEngine *pMerger = pIncr->pMerger;
104688 assert( pIncr->bEof==0 );
104692 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
104694 int dummy;
104695 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
104696 int nKey = pReader->nKey;
104701 if( pReader->pFd==0 ) break;
104702 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
104706 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
104707 assert( pIncr->pMerger->pTask==pTask );
104708 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
104711 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
104720 ** multi-threaded IncrMerger objects.
104725 pIncr->pTask->bDone = 1;
104732 static int vdbeIncrBgPopulate(IncrMerger *pIncr){
104734 assert( pIncr->bUseThread );
104735 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
104745 ** For single-threaded objects, this is accomplished by literally reading
104746 ** keys from pIncr->pMerger and repopulating aFile[0].
104748 ** For multi-threaded objects, all that is required is to wait until the
104756 static int vdbeIncrSwap(IncrMerger *pIncr){
104757 int rc = SQLITE_OK;
104760 if( pIncr->bUseThread ){
104761 rc = vdbeSorterJoinThread(pIncr->pTask);
104764 SorterFile f0 = pIncr->aFile[0];
104765 pIncr->aFile[0] = pIncr->aFile[1];
104766 pIncr->aFile[1] = f0;
104770 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
104771 pIncr->bEof = 1;
104780 pIncr->aFile[0] = pIncr->aFile[1];
104781 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
104782 pIncr->bEof = 1;
104795 static int vdbeIncrMergerNew(
104800 int rc = SQLITE_OK;
104804 pIncr->pMerger = pMerger;
104805 pIncr->pTask = pTask;
104806 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
104807 pTask->file2.iEof += pIncr->mxSz;
104818 ** Set the "use-threads" flag on object pIncr.
104821 pIncr->bUseThread = 1;
104822 pIncr->pTask->file2.iEof -= pIncr->mxSz;
104829 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
104835 int iOut /* Store the result in pMerger->aTree[iOut] */
104837 int i1;
104838 int i2;
104839 int iRes;
104843 assert( iOut<pMerger->nTree && iOut>0 );
104845 if( iOut>=(pMerger->nTree/2) ){
104846 i1 = (iOut - pMerger->nTree/2) * 2;
104849 i1 = pMerger->aTree[iOut*2];
104850 i2 = pMerger->aTree[iOut*2+1];
104853 p1 = &pMerger->aReadr[i1];
104854 p2 = &pMerger->aReadr[i2];
104856 if( p1->pFd==0 ){
104858 }else if( p2->pFd==0 ){
104861 SortSubtask *pTask = pMerger->pTask;
104862 int bCached = 0;
104863 int res;
104864 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
104865 res = pTask->xCompare(
104866 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
104875 pMerger->aTree[iOut] = iRes;
104882 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
104895 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
104915 static int vdbeMergeEngineInit(
104918 int eMode /* One of the INCRINIT_XXX constants */
104920 int rc = SQLITE_OK; /* Return code */
104921 int i; /* For looping over PmaReader objects */
104922 int nTree; /* Number of subtrees to merge */
104928 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
104932 assert( pMerger->pTask==0 );
104933 pMerger->pTask = pTask;
104935 nTree = pMerger->nTree;
104940 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
104943 ** on this PmaReader before any of the multi-threaded PmaReaders takes
104944 ** better advantage of multi-processor hardware. */
104945 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
104947 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
104952 for(i=pMerger->nTree-1; i>0; i--){
104955 return pTask->pUnpacked->errCode;
104960 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
104962 ** object at (pReadr->pIncr).
104965 ** in the sub-tree headed by pReadr are also initialized. Data is then
104970 ** to be a multi-threaded PmaReader and this function is being called in a
104971 ** background thread. In this case all PmaReaders in the sub-tree are
104979 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
104980 ** this entire function is being run by thread (pTask->thread), that will
104984 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
104985 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
104991 static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
104992 int rc = SQLITE_OK;
104993 IncrMerger *pIncr = pReadr->pIncr;
104994 SortSubtask *pTask = pIncr->pTask;
104995 sqlite3 *db = pTask->pSorter->db;
104997 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
105000 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
105002 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
105003 ** requires two temp files to itself, whereas a single-threaded object
105004 ** only requires a region of pTask->file2. */
105006 int mxSz = pIncr->mxSz;
105008 if( pIncr->bUseThread ){
105009 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
105011 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
105015 /*if( !pIncr->bUseThread )*/{
105016 if( pTask->file2.pFd==0 ){
105017 assert( pTask->file2.iEof>0 );
105018 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
105019 pTask->file2.iEof = 0;
105022 pIncr->aFile[1].pFd = pTask->file2.pFd;
105023 pIncr->iStartOff = pTask->file2.iEof;
105024 pTask->file2.iEof += mxSz;
105030 if( rc==SQLITE_OK && pIncr->bUseThread ){
105032 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
105034 ** pIncr->pTask->thread.
105063 pReader->pIncr->pTask->bDone = 1;
105069 ** If the PmaReader passed as the first argument is not an incremental-reader
105070 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
105074 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
105079 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
105080 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
105081 int rc = SQLITE_OK; /* Return code */
105084 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
105085 if( pIncr->bUseThread ){
105087 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
105098 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
105099 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
105104 ** first PMA to read from pTask->file. Assuming no error occurs, it is
105109 static int vdbeMergeEngineLevel0(
105111 int nPMA, /* Number of PMAs to read */
105112 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
105113 MergeEngine **ppOut /* OUT: New merge-engine */
105117 int i;
105118 int rc = SQLITE_OK;
105125 PmaReader *pReadr = &pNew->aReadr[i];
105126 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
105127 iOff = pReadr->iEof;
105144 ** nPMA<=16 -> TreeDepth() == 0
105145 ** nPMA<=256 -> TreeDepth() == 1
105146 ** nPMA<=65536 -> TreeDepth() == 2
105148 static int vdbeSorterTreeDepth(int nPMA){
105149 int nDepth = 0;
105159 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
105166 static int vdbeSorterAddToTree(
105168 int nDepth, /* Depth of tree according to TreeDepth() */
105169 int iSeq, /* Sequence number of leaf within tree */
105173 int rc = SQLITE_OK;
105174 int nDiv = 1;
105175 int i;
105186 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
105187 PmaReader *pReadr = &p->aReadr[iIter];
105189 if( pReadr->pIncr==0 ){
105194 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
105198 p = pReadr->pIncr->pMerger;
105204 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
105213 ** that has already written two or more level-0 PMAs to one or more temp
105222 static int vdbeSorterMergeTreeBuild(
105227 int rc = SQLITE_OK;
105228 int iTask;
105231 /* If the sorter uses more than one task, then create the top-level
105233 ** one PmaReader per sub-task. */
105234 assert( pSorter->bUseThreads || pSorter->nTask==1 );
105235 if( pSorter->nTask>1 ){
105236 pMain = vdbeMergeEngineNew(pSorter->nTask);
105241 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
105242 SortSubtask *pTask = &pSorter->aTask[iTask];
105243 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
105244 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
105246 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
105249 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
105250 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
105252 int i;
105253 int iSeq = 0;
105256 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
105257 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
105258 int nReader; /* Number of level-0 PMAs to merge */
105260 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
105271 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
105296 ** (for multi-threaded sorters) so that it can be used to iterate through
105301 static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
105302 int rc; /* Return code */
105303 SortSubtask *pTask0 = &pSorter->aTask[0];
105306 sqlite3 *db = pTask0->pSorter->db;
105307 int i;
105309 for(i=0; i<pSorter->nTask; i++){
105310 pSorter->aTask[i].xCompare = xCompare;
105317 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
105318 if( pSorter->bUseThreads ){
105319 int iTask;
105321 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
105325 pSorter->pReader = pReadr;
105329 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
105331 vdbeIncrMergerSetThreads(pReadr->pIncr);
105332 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
105334 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
105336 assert( pIncr->pTask!=pLast );
105339 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
105344 ** b) If it is using task (nTask-1), it is configured to run
105345 ** in single-threaded mode. This is important, as the
105349 PmaReader *p = &pMain->aReadr[iTask];
105350 assert( p->pIncr==0 || (
105351 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
105352 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
105366 pSorter->pMerger = pMain;
105383 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
105385 int rc = SQLITE_OK; /* Return code */
105387 assert( pCsr->eCurType==CURTYPE_SORTER );
105388 pSorter = pCsr->uc.pSorter;
105393 ** from the in-memory list. */
105394 if( pSorter->bUsePMA==0 ){
105395 if( pSorter->list.pList ){
105397 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
105404 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
105408 assert( pSorter->list.pList );
105418 assert( pSorter->pReader==0 );
105435 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
105437 int rc; /* Return code */
105439 assert( pCsr->eCurType==CURTYPE_SORTER );
105440 pSorter = pCsr->uc.pSorter;
105441 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
105442 if( pSorter->bUsePMA ){
105443 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
105444 assert( pSorter->bUseThreads==0 || pSorter->pReader );
105445 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
105447 if( pSorter->bUseThreads ){
105448 rc = vdbePmaReaderNext(pSorter->pReader);
105449 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
105452 /*if( !pSorter->bUseThreads )*/ {
105453 int res = 0;
105454 assert( pSorter->pMerger!=0 );
105455 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
105456 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
105460 SorterRecord *pFree = pSorter->list.pList;
105461 pSorter->list.pList = pFree->u.pNext;
105462 pFree->u.pNext = 0;
105463 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
105464 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
105475 int *pnKey /* OUT: Size of current key in bytes */
105478 if( pSorter->bUsePMA ){
105481 if( pSorter->bUseThreads ){
105482 pReader = pSorter->pReader;
105485 /*if( !pSorter->bUseThreads )*/{
105486 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
105488 *pnKey = pReader->nKey;
105489 pKey = pReader->aKey;
105491 *pnKey = pSorter->list.pList->nVal;
105492 pKey = SRVAL(pSorter->list.pList);
105500 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
105502 void *pKey; int nKey; /* Sorter key to copy into pOut */
105504 assert( pCsr->eCurType==CURTYPE_SORTER );
105505 pSorter = pCsr->uc.pSorter;
105510 pOut->n = nKey;
105512 memcpy(pOut->z, pKey, nKey);
105533 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
105536 int nKeyCol, /* Compare this many columns */
105537 int *pRes /* OUT: Result of comparison */
105542 int i;
105543 void *pKey; int nKey; /* Sorter key to compare pVal with */
105545 assert( pCsr->eCurType==CURTYPE_SORTER );
105546 pSorter = pCsr->uc.pSorter;
105547 r2 = pSorter->pUnpacked;
105548 pKeyInfo = pCsr->pKeyInfo;
105550 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
105552 r2->nField = nKeyCol;
105554 assert( r2->nField==nKeyCol );
105559 if( r2->aMem[i].flags & MEM_Null ){
105560 *pRes = -1;
105565 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
105572 ** 2020-03-23
105583 ** This file implements virtual-tables for examining the bytecode content
105590 /* An instance of the bytecode() table-valued function.
105594 sqlite3_vtab base; /* Base class - must be first */
105596 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
105603 sqlite3_vtab_cursor base; /* Base class - must be first */
105605 int iRowid; /* The rowid of the output table */
105606 int iAddr; /* Address */
105607 int needFinalize; /* Cursors owns pStmt and must finalize it */
105608 int showSubprograms; /* Provide a listing of subprograms */
105618 ** Create a new bytecode() table-valued function.
105620 static int bytecodevtabConnect(
105623 int argc, const char *const*argv,
105628 int rc;
105629 int isTabUsed = pAux!=0;
105633 "addr INT,"
105635 "p1 INT,"
105636 "p2 INT,"
105637 "p3 INT,"
105639 "p5 INT,"
105642 "nexec INT,"
105643 "ncycle INT,"
105652 "wr INT,"
105667 pNew->db = db;
105668 pNew->bTablesUsed = isTabUsed*2;
105676 static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
105685 static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
105691 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
105692 *ppCursor = &pCur->base;
105700 sqlite3_free(pCur->zP4);
105701 pCur->zP4 = 0;
105702 sqlite3VdbeMemRelease(&pCur->sub);
105703 sqlite3VdbeMemSetNull(&pCur->sub);
105704 if( pCur->needFinalize ){
105705 sqlite3_finalize(pCur->pStmt);
105707 pCur->pStmt = 0;
105708 pCur->needFinalize = 0;
105709 pCur->zType = 0;
105710 pCur->zSchema = 0;
105711 pCur->zName = 0;
105717 static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
105728 static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
105730 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
105731 int rc;
105732 if( pCur->zP4 ){
105733 sqlite3_free(pCur->zP4);
105734 pCur->zP4 = 0;
105736 if( pCur->zName ){
105737 pCur->zName = 0;
105738 pCur->zType = 0;
105739 pCur->zSchema = 0;
105742 (Vdbe*)pCur->pStmt,
105743 pCur->showSubprograms ? &pCur->sub : 0,
105744 pTab->bTablesUsed,
105745 &pCur->iRowid,
105746 &pCur->iAddr,
105747 &pCur->aOp);
105749 sqlite3VdbeMemSetNull(&pCur->sub);
105750 pCur->aOp = 0;
105759 static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
105761 return pCur->aOp==0;
105768 static int bytecodevtabColumn(
105771 int i /* Which column to return */
105774 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
105775 Op *pOp = pCur->aOp + pCur->iAddr;
105776 if( pVTab->bTablesUsed ){
105780 if( i<=2 && pCur->zType==0 ){
105783 int iDb = pOp->p3;
105784 Pgno iRoot = (Pgno)pOp->p2;
105785 sqlite3 *db = pVTab->db;
105786 pSchema = db->aDb[iDb].pSchema;
105787 pCur->zSchema = db->aDb[iDb].zDbSName;
105788 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
105790 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
105791 pCur->zName = pTab->zName;
105792 pCur->zType = "table";
105796 if( pCur->zName==0 ){
105797 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
105799 if( pIdx->tnum==iRoot ){
105800 pCur->zName = pIdx->zName;
105801 pCur->zType = "index";
105811 sqlite3_result_int(ctx, pCur->iAddr);
105814 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
105815 -1, SQLITE_STATIC);
105818 sqlite3_result_int(ctx, pOp->p1);
105821 sqlite3_result_int(ctx, pOp->p2);
105824 sqlite3_result_int(ctx, pOp->p3);
105828 if( pCur->zP4==0 ){
105829 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
105832 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
105835 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
105836 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
105841 sqlite3_result_int(ctx, pOp->p5);
105844 Op *aOp = pCur->aOp;
105846 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
105847 if( pCur->iRowid==pCur->iAddr+1 ){
105850 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
105859 sqlite3_result_int64(ctx, pOp->nExec);
105862 sqlite3_result_int64(ctx, pOp->nCycle);
105872 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
105875 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
105878 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
105881 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
105891 static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
105893 *pRowid = pCur->iRowid;
105903 static int bytecodevtabFilter(
105905 int idxNum, const char *idxStr,
105906 int argc, sqlite3_value **argv
105909 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
105910 int rc = SQLITE_OK;
105914 pCur->iRowid = 0;
105915 pCur->iAddr = 0;
105916 pCur->showSubprograms = idxNum==0;
105923 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
105924 pCur->needFinalize = 1;
105927 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
105929 if( pCur->pStmt==0 ){
105930 pVTab->base.zErrMsg = sqlite3_mprintf(
105932 pVTab->bTablesUsed ? "tables_used" : "bytecode"
105946 static int bytecodevtabBestIndex(
105950 int i;
105951 int rc = SQLITE_CONSTRAINT;
105954 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
105955 pIdxInfo->estimatedCost = (double)100;
105956 pIdxInfo->estimatedRows = 100;
105957 pIdxInfo->idxNum = 0;
105958 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
105959 if( p->usable==0 ) continue;
105960 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
105962 pIdxInfo->aConstraintUsage[i].omit = 1;
105963 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
105965 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
105966 pIdxInfo->aConstraintUsage[i].omit = 1;
105967 pIdxInfo->idxNum = 1;
106006 SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
106007 int rc;
106015 SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
106032 ** This file contains code use to implement an in-memory rollback journal.
106033 ** The in-memory rollback journal is used to journal transactions for
106036 ** Update: The in-memory journal is also used to temporarily cache
106037 ** smaller journals that are not critical for power-loss recovery.
106055 ** The zChunk array is always at least 8 bytes in size - usually much more.
106072 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
106084 ** This structure is a subclass of sqlite3_file. Each open memory-journal
106089 int nChunkSize; /* In-memory chunk-size */
106091 int nSpill; /* Bytes of data before flushing */
106092 FileChunk *pFirst; /* Head of in-memory chunk-list */
106096 int flags; /* xOpen flags */
106102 ** Read data from the in-memory journal file. This is the implementation
106105 static int memjrnlRead(
106108 int iAmt, /* Number of bytes to read */
106113 int nRead = iAmt;
106114 int iChunkOffset;
106117 if( (iAmt+iOfst)>p->endpoint.iOffset ){
106120 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
106121 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
106123 for(pChunk=p->pFirst;
106124 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
106125 pChunk=pChunk->pNext
106127 iOff += p->nChunkSize;
106130 pChunk = p->readpoint.pChunk;
106134 iChunkOffset = (int)(iOfst%p->nChunkSize);
106136 int iSpace = p->nChunkSize - iChunkOffset;
106137 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
106138 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
106140 nRead -= iSpace;
106142 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
106143 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
106144 p->readpoint.pChunk = pChunk;
106156 pNext = pIter->pNext;
106164 static int memjrnlCreateFile(MemJournal *p){
106165 int rc;
106172 int nChunk = copy.nChunkSize;
106175 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
106177 nChunk = copy.endpoint.iOffset - iOff;
106179 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
106184 /* No error has occurred. Free the in-memory buffers. */
106190 ** the original before returning. This way, SQLite uses the in-memory
106191 ** journal data to roll back changes made to the internal page-cache
106201 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size);
106206 static int memjrnlWrite(
106209 int iAmt, /* Number of bytes to write */
106213 int nWrite = iAmt;
106218 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
106219 int rc = memjrnlCreateFile(p);
106228 /* An in-memory journal file should only ever be appended to. Random
106230 ** the in-memory journal is being used by a connection using the
106231 ** atomic-write optimization. In this case the first 28 bytes of the
106233 assert( iOfst<=p->endpoint.iOffset );
106234 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
106237 if( iOfst==0 && p->pFirst ){
106238 assert( p->nChunkSize>iAmt );
106239 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
106242 FileChunk *pChunk = p->endpoint.pChunk;
106243 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
106244 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
106249 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
106253 pNew->pNext = 0;
106255 assert( p->pFirst );
106256 pChunk->pNext = pNew;
106258 assert( !p->pFirst );
106259 p->pFirst = pNew;
106261 pChunk = p->endpoint.pChunk = pNew;
106265 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
106267 nWrite -= iSpace;
106268 p->endpoint.iOffset += iSpace;
106277 ** Truncate the in-memory file.
106279 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
106281 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
106282 if( size<p->endpoint.iOffset ){
106285 memjrnlFreeChunks(p->pFirst);
106286 p->pFirst = 0;
106288 i64 iOff = p->nChunkSize;
106289 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
106290 iOff += p->nChunkSize;
106293 memjrnlFreeChunks(pIter->pNext);
106294 pIter->pNext = 0;
106298 p->endpoint.pChunk = pIter;
106299 p->endpoint.iOffset = size;
106300 p->readpoint.pChunk = 0;
106301 p->readpoint.iOffset = 0;
106309 static int memjrnlClose(sqlite3_file *pJfd){
106311 memjrnlFreeChunks(p->pFirst);
106319 ** syncing an in-memory journal is a no-op.
106321 static int memjrnlSync(sqlite3_file *pJfd, int flags){
106329 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
106331 *pSize = (sqlite_int64) p->endpoint.iOffset;
106366 ** all content is always stored in main-memory. Finally, if nSpill is a
106367 ** positive value, then the journal file is initially created in-memory
106372 SQLITE_PRIVATE int sqlite3JournalOpen(
106376 int flags, /* Opening flags */
106377 int nSpill /* Bytes buffered before opening the file */
106383 /* Zero the file-handle object. If nSpill was passed zero, initialize
106386 ** made on the journal file-handle. */
106393 p->nChunkSize = nSpill;
106395 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
106396 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
106399 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
106400 p->nSpill = nSpill;
106401 p->flags = flags;
106402 p->zJournal = zName;
106403 p->pVfs = pVfs;
106408 ** Open an in-memory journal file.
106411 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
106418 ** in-memory-only journal file (i.e. is one that was opened with a +ve
106422 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
106423 int rc = SQLITE_OK;
106425 if( pJfd->pMethods==&MemJournalMethods && (
106427 p->nSpill>0
106432 NEVER(p->nSpill>0)
106435 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
106445 ** The file-handle passed as the only argument is open on a journal file.
106449 SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
106450 return p->pMethods==&MemJournalMethods;
106455 ** pVfs to create the underlying on-disk files.
106457 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
106458 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
106487 static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
106489 for(pWin=pList; pWin; pWin=pWin->pNextWin){
106490 int rc;
106491 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
106493 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
106495 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
106497 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
106499 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
106521 ** return from the top-level walk call.
106526 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){
106527 int rc;
106531 rc = pWalker->xExprCallback(pWalker, pExpr);
106534 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
106535 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
106538 if( pExpr->pRight ){
106540 pExpr = pExpr->pRight;
106544 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
106546 if( pExpr->x.pList ){
106547 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
106551 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
106560 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
106568 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
106569 int i;
106572 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
106573 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
106580 ** This is a no-op callback for Walker->xSelectCallback2. If this
106581 ** callback is set, then the Select->pWinDefn list is traversed.
106586 /* No-op */
106595 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
106596 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
106597 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
106598 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
106599 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
106600 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
106601 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
106603 if( p->pWinDefn ){
106605 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
106606 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
106608 || pWalker->xSelectCallback2==sqlite3SelectPopWith
106613 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
106628 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
106630 int i;
106633 pSrc = p->pSrc;
106635 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
106636 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
106639 if( pItem->fg.isTabFunc
106640 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
106652 ** on the compound select chain, p->pPrior.
106657 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
106664 ** is a no-op returning WRC_Continue.
106666 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
106667 int rc;
106669 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
106671 rc = pWalker->xSelectCallback(pWalker, p);
106678 if( pWalker->xSelectCallback2 ){
106679 pWalker->xSelectCallback2(pWalker, p);
106681 p = p->pPrior;
106689 SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
106691 pWalker->walkerDepth++;
106696 pWalker->walkerDepth--;
106701 ** No-op routine for the parse-tree walker.
106709 SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
106715 ** No-op routine for the parse-tree walker for SELECT statements.
106718 SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
106755 ** is a helper function - a callback for the tree walker.
106759 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
106760 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
106763 static void incrAggFunctionDepth(Expr *pExpr, int N){
106774 ** Turn the pExpr expression into an alias for the iCol-th column of the
106795 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
106797 int nSubquery /* Number of subqueries that the label is moving */
106799 Expr *pOrig; /* The iCol-th column of the result set */
106803 assert( iCol>=0 && iCol<pEList->nExpr );
106804 pOrig = pEList->a[iCol].pExpr;
106807 if( pExpr->pAggInfo ) return;
106808 db = pParse->db;
106810 if( db->mallocFailed ){
106816 if( pExpr->op==TK_COLLATE ){
106818 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
106824 if( ALWAYS(pExpr->y.pWin!=0) ){
106825 pExpr->y.pWin->pOwner = pExpr;
106835 ** and mark the expression-list item by setting ExprList.a[].fg.eEName
106838 ** Check to see if the zSpan/eEName of the expression-list item passed to this
106845 ** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
106849 SQLITE_PRIVATE int sqlite3MatchEName(
106854 int *pbRowid
106856 int n;
106858 int eEName = pItem->fg.eEName;
106863 zSpan = pItem->zEName;
106883 ** Return TRUE if the double-quoted string mis-feature should be supported.
106885 static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
106886 if( db->init.busy ) return 1; /* Always support for legacy schemas */
106887 if( pTopNC->ncFlags & NC_IsDDL ){
106889 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
106892 return (db->flags & SQLITE_DqsDDL)!=0;
106895 return (db->flags & SQLITE_DqsDML)!=0;
106900 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
106904 int n;
106907 n = pExpr->iColumn;
106909 pExTab = pExpr->y.pTab;
106911 assert( n < pExTab->nCol );
106912 if( (pExTab->tabFlags & TF_HasGenerated)!=0
106913 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
106915 testcase( pExTab->nCol==BMS-1 );
106916 testcase( pExTab->nCol==BMS );
106917 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
106919 testcase( n==BMS-1 );
106921 if( n>=BMS ) n = BMS-1;
106938 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
106940 pNew->iTable = pMatch->iCursor;
106941 pNew->iColumn = iColumn;
106942 pNew->y.pTab = pMatch->pTab;
106943 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
106950 ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
106952 static SQLITE_NOINLINE int isValidSchemaTableName(
106955 const char *zDb /* non-NULL if a database qualifier is present */
106959 assert( pTab->tnum==1 );
106961 zLegacy = pTab->zName;
106981 ** pExpr->iDb Set the index in db->aDb[] of the database X
106983 ** pExpr->iTable Set to the cursor number for the table obtained
106985 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
106987 ** pExpr->iColumn Set to the column number within the table.
106988 ** pExpr->op Set to TK_COLUMN.
106989 ** pExpr->pLeft Any expression this points to is deleted
106990 ** pExpr->pRight Any expression this points to is deleted.
107002 static int lookupName(
107010 int i, j; /* Loop counters */
107011 int cnt = 0; /* Number of matching column names */
107012 int cntTab = 0; /* Number of potential "rowid" matches */
107013 int nSubquery = 0; /* How many levels of subquery */
107014 sqlite3 *db = pParse->db; /* The database connection */
107019 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
107023 const char *zCol = pRight->u.zToken;
107030 /* Initialize the node to no-match */
107031 pExpr->iTable = -1;
107039 testcase( pNC->ncFlags & NC_PartIdx );
107040 testcase( pNC->ncFlags & NC_IsCheck );
107041 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
107048 for(i=0; i<db->nDb; i++){
107049 assert( db->aDb[i].zDbSName );
107050 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
107051 pSchema = db->aDb[i].pSchema;
107055 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
107058 pSchema = db->aDb[0].pSchema;
107059 zDb = db->aDb[0].zDbSName;
107064 /* Start at the inner-most context and move outward until a match is found */
107068 SrcList *pSrcList = pNC->pSrcList;
107071 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
107073 pTab = pItem->pTab;
107074 assert( pTab!=0 && pTab->zName!=0 );
107075 assert( pTab->nCol>0 || pParse->nErr );
107076 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
107077 if( pItem->fg.isNestedFrom ){
107082 ** This pItem -------------^
107084 int hit = 0;
107085 assert( pItem->pSelect!=0 );
107086 pEList = pItem->pSelect->pEList;
107088 assert( pEList->nExpr==pTab->nCol );
107089 for(j=0; j<pEList->nExpr; j++){
107090 int bRowid = 0; /* True if possible rowid match */
107091 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
107096 if( pItem->fg.isUsing==0
107097 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107105 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
107106 /* An INNER or LEFT JOIN. Use the left-most table */
107109 if( (pItem->fg.jointype & JT_LEFT)==0 ){
107110 /* A RIGHT JOIN. Use the right-most table */
107116 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107128 pExpr->iColumn = j;
107129 pEList->a[j].fg.bUsed = 1;
107131 /* rowid cannot be part of a USING clause - assert() this. */
107132 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 );
107133 if( pEList->a[j].fg.bUsingTerm ) break;
107140 if( pTab->pSchema!=pSchema ) continue;
107143 if( pItem->zAlias!=0 ){
107144 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
107147 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
107148 if( pTab->tnum!=1 ) continue;
107152 if( IN_RENAME_OBJECT && pItem->zAlias ){
107153 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
107157 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
107158 if( pCol->hName==hCol
107159 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107162 if( pItem->fg.isUsing==0
107163 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107171 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
107172 /* An INNER or LEFT JOIN. Use the left-most table */
107175 if( (pItem->fg.jointype & JT_LEFT)==0 ){
107176 /* A RIGHT JOIN. Use the right-most table */
107182 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107187 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
107188 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
107189 if( pItem->fg.isNestedFrom ){
107204 ** non-VIEW candidate plus multiple VIEW candidates. In other
107205 ** words non-VIEW candidate terms take precedence over VIEWs.
107210 && ALWAYS(pMatch->pTab!=0)
107211 && (pMatch->pTab->tabFlags & TF_Ephemeral)!=0
107212 && (pTab->tabFlags & TF_Ephemeral)==0)
107220 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
107222 ** always be a non-VIEW
107230 pExpr->iTable = pMatch->iCursor;
107232 pExpr->y.pTab = pMatch->pTab;
107233 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
107236 pSchema = pExpr->y.pTab->pSchema;
107249 if( pParse->pTriggerTab!=0 ){
107250 int op = pParse->eTriggerOp;
107252 if( pParse->bReturning ){
107253 if( (pNC->ncFlags & NC_UBaseReg)!=0
107255 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
107256 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
107258 pExpr->iTable = op!=TK_DELETE;
107259 pTab = pParse->pTriggerTab;
107262 pExpr->iTable = 1;
107263 pTab = pParse->pTriggerTab;
107265 pExpr->iTable = 0;
107266 pTab = pParse->pTriggerTab;
107271 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
107272 Upsert *pUpsert = pNC->uNC.pUpsert;
107274 pTab = pUpsert->pUpsertSrc->a[0].pTab;
107275 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
107281 int iCol;
107283 pSchema = pTab->pSchema;
107285 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
107286 if( pCol->hName==hCol
107287 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107289 if( iCol==pTab->iPKey ){
107290 iCol = -1;
107295 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
107296 /* IMP: R-51414-32910 */
107297 iCol = -1;
107299 if( iCol<pTab->nCol ){
107303 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
107304 testcase( iCol==(-1) );
107307 pExpr->iColumn = iCol;
107308 pExpr->y.pTab = pTab;
107311 pExpr->iTable = pNC->uNC.pUpsert->regData +
107319 pExpr->y.pTab = pTab;
107320 if( pParse->bReturning ){
107322 pExpr->op2 = TK_COLUMN;
107323 pExpr->iColumn = iCol;
107324 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
107327 pExpr->iColumn = (i16)iCol;
107331 pExpr->affExpr = SQLITE_AFF_INTEGER;
107332 }else if( pExpr->iTable==0 ){
107335 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107339 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107355 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
107357 && ALWAYS(VisibleRowid(pMatch->pTab) || pMatch->fg.isNestedFrom)
107361 if( pMatch->pTab!=0 && IsView(pMatch->pTab) ){
107365 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
107366 pExpr->affExpr = SQLITE_AFF_INTEGER;
107371 ** might refer to an result-set alias. This happens, for example, when
107381 ** The ability to use an output result-set column in the WHERE, GROUP BY,
107388 && (pNC->ncFlags & NC_UEList)!=0
107391 pEList = pNC->uNC.pEList;
107393 for(j=0; j<pEList->nExpr; j++){
107394 char *zAs = pEList->a[j].zEName;
107395 if( pEList->a[j].fg.eEName==ENAME_NAME
107399 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
107400 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
107401 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
107402 pOrig = pEList->a[j].pExpr;
107403 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
107408 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
107433 pNC = pNC->pNext;
107440 ** supplied) and the value of Z is enclosed in double-quotes, then
107445 ** Because no reference was made to outer contexts, the pNC->nRef
107449 assert( pExpr->op==TK_ID );
107453 /* If a double-quoted identifier does not match any known column name,
107457 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
107468 "double-quoted string literal: \"%w\"", zCol);
107470 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
107472 pExpr->op = TK_STRING;
107473 memset(&pExpr->y, 0, sizeof(pExpr->y));
107493 if( pFJMatch->nExpr==cnt-1 ){
107497 sqlite3ExprDelete(db, pExpr->pLeft);
107498 pExpr->pLeft = 0;
107499 sqlite3ExprDelete(db, pExpr->pRight);
107500 pExpr->pRight = 0;
107502 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107503 pExpr->op = TK_FUNCTION;
107504 pExpr->u.zToken = "coalesce";
107505 pExpr->x.pList = pFJMatch;
107519 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
107520 " string literal in single-quotes?",
107525 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107526 pParse->checkSchema = 1;
107527 pTopNC->nNcErr++;
107534 sqlite3ExprDelete(db, pExpr->pLeft);
107535 pExpr->pLeft = 0;
107536 sqlite3ExprDelete(db, pExpr->pRight);
107537 pExpr->pRight = 0;
107556 if( pExpr->iColumn>=0 ){
107557 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
107559 pMatch->fg.rowidUsed = 1;
107563 pExpr->op = eNewExprOp;
107568 if( pParse->db->xAuth
107569 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
107571 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
107578 pTopNC->nRef++;
107580 pTopNC = pTopNC->pNext;
107592 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
107595 SrcItem *pItem = &pSrc->a[iSrc];
107598 pTab = p->y.pTab = pItem->pTab;
107599 p->iTable = pItem->iCursor;
107600 if( p->y.pTab->iPKey==iCol ){
107601 p->iColumn = -1;
107603 p->iColumn = (ynVar)iCol;
107604 if( (pTab->tabFlags & TF_HasGenerated)!=0
107605 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
107607 testcase( pTab->nCol==63 );
107608 testcase( pTab->nCol==64 );
107609 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
107612 testcase( iCol==BMS-1 );
107613 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
107622 ** pNC->ncFlags values determined by validMask.
107628 ** int validMask, // Set of contexts for which prohibited
107644 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
107646 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
107649 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
107652 if( pExpr ) pExpr->op = TK_NULL;
107653 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
107657 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
107661 ** Return 1024 times this value. Or return -1 if p is not a floating point
107664 static int exprProbability(Expr *p){
107665 double r = -1.0;
107666 if( p->op!=TK_FLOAT ) return -1;
107668 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
107670 if( r>1.0 ) return -1;
107671 return (int)(r*134217728.0);
107685 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
107689 pNC = pWalker->u.pNC;
107691 pParse = pNC->pParse;
107692 assert( pParse==pWalker->pParse );
107695 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
107696 SrcList *pSrcList = pNC->pSrcList;
107697 int i;
107698 for(i=0; i<pNC->pSrcList->nSrc; i++){
107699 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
107703 switch( pExpr->op ){
107711 SrcList *pSrcList = pNC->pSrcList;
107713 assert( pSrcList && pSrcList->nSrc>=1 );
107714 pItem = pSrcList->a;
107715 pExpr->op = TK_COLUMN;
107717 pExpr->y.pTab = pItem->pTab;
107718 pExpr->iTable = pItem->iCursor;
107719 pExpr->iColumn--;
107720 pExpr->affExpr = SQLITE_AFF_INTEGER;
107726 ** "expr IS NOT NULL" --> "TRUE"
107727 ** "expr IS NULL" --> "FALSE"
107732 ** If this optimization occurs, also restore the NameContext ref-counts
107738 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table
107742 ** CREATE TABLE t1(a INT NOT NULL);
107753 int anRef[8];
107755 int i;
107756 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
107757 anRef[i] = p->nRef;
107759 sqlite3WalkExpr(pWalker, pExpr->pLeft);
107761 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
107766 for(i=0, p=pNC; p; p=p->pNext, i++){
107767 if( (p->ncFlags & NC_Where)==0 ){
107777 pExpr->op==TK_NOTNULL
107782 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
107783 pExpr->flags |= EP_IntValue;
107784 pExpr->op = TK_INTEGER;
107785 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
107786 p->nRef = anRef[i];
107788 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
107789 pExpr->pLeft = 0;
107798 ** be one call to lookupName(). Then the compiler will in-line
107807 if( pExpr->op==TK_ID ){
107813 Expr *pLeft = pExpr->pLeft;
107814 testcase( pNC->ncFlags & NC_IdxExpr );
107815 testcase( pNC->ncFlags & NC_GenCol );
107818 pRight = pExpr->pRight;
107819 if( pRight->op==TK_ID ){
107822 assert( pRight->op==TK_DOT );
107824 zDb = pLeft->u.zToken;
107825 pLeft = pRight->pLeft;
107826 pRight = pRight->pRight;
107829 zTable = pLeft->u.zToken;
107833 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
107842 ExprList *pList = pExpr->x.pList; /* The argument list */
107843 int n = pList ? pList->nExpr : 0; /* Number of arguments */
107844 int no_such_func = 0; /* True if no such function exists */
107845 int wrong_num_args = 0; /* True if wrong number of arguments */
107846 int is_agg = 0; /* True if is an aggregate function */
107849 u8 enc = ENC(pParse->db); /* The database encoding */
107850 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
107852 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
107855 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER );
107856 zId = pExpr->u.zToken;
107857 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
107859 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
107866 is_agg = pDef->xFinalize!=0;
107867 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
107870 pExpr->iTable = exprProbability(pList->a[1].pExpr);
107871 if( pExpr->iTable<0 ){
107875 pNC->nNcErr++;
107878 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
107880 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
107881 ** short-hand for likelihood(X,0.0625).
107882 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
107884 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
107887 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
107892 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
107897 pNC->nNcErr++;
107899 pExpr->op = TK_NULL;
107904 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
107911 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
107912 /* Clearly non-deterministic functions like random(), but also
107918 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
107922 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
107923 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
107925 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
107926 && pParse->nested==0
107927 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
107929 /* Internal-use-only functions are disallowed unless the
107931 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
107936 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
107945 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
107946 || (pDef->xValue==0 && pDef->xInverse==0)
107947 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
107949 if( pDef && pDef->xValue==0 && pWin ){
107953 pNC->nNcErr++;
107955 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
107956 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
107957 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
107960 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
107966 pNC->nNcErr++;
107970 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
107972 pNC->nNcErr++;
107976 else if( no_such_func && pParse->db->init.busy==0
107978 && pParse->explain==0
107982 pNC->nNcErr++;
107986 pNC->nNcErr++;
107991 "FILTER may not be used with non-aggregate %#T()",
107994 pNC->nNcErr++;
107997 else if( is_agg==0 && pExpr->pLeft ){
107999 pNC->nNcErr++;
108006 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
108008 pNC->ncFlags &= ~NC_AllowAgg;
108012 else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){
108017 if( pExpr->pLeft ){
108018 assert( pExpr->pLeft->op==TK_ORDER );
108019 assert( ExprUseXList(pExpr->pLeft) );
108020 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
108024 Select *pSel = pNC->pWinSelect;
108025 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
108027 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
108028 if( pParse->db->mallocFailed ) break;
108030 sqlite3WalkExprList(pWalker, pWin->pPartition);
108031 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
108032 sqlite3WalkExpr(pWalker, pWin->pFilter);
108034 pNC->ncFlags |= NC_HasWin;
108039 pExpr->op = TK_AGG_FUNCTION;
108040 pExpr->op2 = 0;
108043 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
108048 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
108050 pExpr->op2 += (1 + pNC2->nNestedSelect);
108051 pNC2 = pNC2->pNext;
108055 pExpr->op2 += pNC2->nNestedSelect;
108058 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
108059 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
108060 pNC2->ncFlags |= NC_HasAgg
108061 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
108065 pNC->ncFlags |= savedAllowFlags;
108067 /* FIX ME: Compute pExpr->affinity based on the expected return
108074 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
108077 testcase( pExpr->op==TK_IN );
108079 int nRef = pNC->nRef;
108080 testcase( pNC->ncFlags & NC_IsCheck );
108081 testcase( pNC->ncFlags & NC_PartIdx );
108082 testcase( pNC->ncFlags & NC_IdxExpr );
108083 testcase( pNC->ncFlags & NC_GenCol );
108084 assert( pExpr->x.pSelect );
108085 if( pNC->ncFlags & NC_SelfRef ){
108088 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
108090 assert( pNC->nRef>=nRef );
108091 if( nRef!=pNC->nRef ){
108093 pExpr->x.pSelect->selFlags |= SF_Correlated;
108095 pNC->ncFlags |= NC_Subquery;
108100 testcase( pNC->ncFlags & NC_IsCheck );
108101 testcase( pNC->ncFlags & NC_PartIdx );
108102 testcase( pNC->ncFlags & NC_IdxExpr );
108103 testcase( pNC->ncFlags & NC_GenCol );
108110 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
108114 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
108115 int rc = resolveExprStep(pWalker, pRight);
108117 if( pRight->op==TK_TRUEFALSE ){
108118 pExpr->op2 = pExpr->op;
108119 pExpr->op = TK_TRUTH;
108132 int nLeft, nRight;
108133 if( pParse->db->mallocFailed ) break;
108134 assert( pExpr->pLeft!=0 );
108135 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
108136 if( pExpr->op==TK_BETWEEN ){
108138 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
108140 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
108143 assert( pExpr->pRight!=0 );
108144 nRight = sqlite3ExprVectorSize(pExpr->pRight);
108147 testcase( pExpr->op==TK_EQ );
108148 testcase( pExpr->op==TK_NE );
108149 testcase( pExpr->op==TK_LT );
108150 testcase( pExpr->op==TK_LE );
108151 testcase( pExpr->op==TK_GT );
108152 testcase( pExpr->op==TK_GE );
108153 testcase( pExpr->op==TK_IS );
108154 testcase( pExpr->op==TK_ISNOT );
108155 testcase( pExpr->op==TK_BETWEEN );
108157 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
108162 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
108163 return pParse->nErr ? WRC_Abort : WRC_Continue;
108170 ** to the AS-name of one of the terms of the expression list. If it is,
108178 static int resolveAsName(
108183 int i; /* Loop counter */
108187 if( pE->op==TK_ID ){
108190 zCol = pE->u.zToken;
108191 for(i=0; i<pEList->nExpr; i++){
108192 if( pEList->a[i].fg.eEName==ENAME_NAME
108193 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
108211 ** Attempt to match pE against result set columns in the left-most
108213 ** as an indication to the caller that it should sort by the i-th column.
108214 ** The left-most column is 1. In other words, the value returned is the
108218 ** If there is no match, return 0. Return -1 if an error occurs.
108220 static int resolveOrderByTermToExprList(
108225 int i; /* Loop counter */
108229 int rc; /* Return code from subprocedures */
108230 u8 savedSuppErr; /* Saved value of db->suppressErr */
108233 pEList = pSelect->pEList;
108239 nc.pSrcList = pSelect->pSrc;
108243 db = pParse->db;
108244 savedSuppErr = db->suppressErr;
108245 db->suppressErr = 1;
108247 db->suppressErr = savedSuppErr;
108251 ** in the result set. Return an 1-based index of the matching
108252 ** result-set entry.
108254 for(i=0; i<pEList->nExpr; i++){
108255 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
108265 ** Generate an ORDER BY or GROUP BY term out-of-range error.
108270 int i, /* The index (1-based) of the term out of range */
108271 int mx, /* Largest permissible value of i */
108275 "%r %s BY term out of range - should be "
108277 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108289 ** beginning with the left-most SELECT and working toward the right.
108295 static int resolveCompoundOrderBy(
108299 int i;
108303 int moreToDo = 1;
108305 pOrderBy = pSelect->pOrderBy;
108307 db = pParse->db;
108308 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108312 for(i=0; i<pOrderBy->nExpr; i++){
108313 pOrderBy->a[i].fg.done = 0;
108315 pSelect->pNext = 0;
108316 while( pSelect->pPrior ){
108317 pSelect->pPrior->pNext = pSelect;
108318 pSelect = pSelect->pPrior;
108323 pEList = pSelect->pEList;
108325 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108326 int iCol = -1;
108328 if( pItem->fg.done ) continue;
108329 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
108332 if( iCol<=0 || iCol>pEList->nExpr ){
108333 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
108351 if( !db->mallocFailed ){
108367 pNew->flags |= EP_IntValue;
108368 pNew->u.iValue = iCol;
108369 if( pItem->pExpr==pE ){
108370 pItem->pExpr = pNew;
108372 Expr *pParent = pItem->pExpr;
108373 assert( pParent->op==TK_COLLATE );
108374 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
108375 assert( pParent->pLeft==pE );
108376 pParent->pLeft = pNew;
108379 pItem->u.x.iOrderByCol = (u16)iCol;
108381 pItem->fg.done = 1;
108386 pSelect = pSelect->pNext;
108388 for(i=0; i<pOrderBy->nExpr; i++){
108389 if( pOrderBy->a[i].fg.done==0 ){
108406 ** return non-zero. Return zero if no errors are seen.
108408 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
108414 int i;
108415 sqlite3 *db = pParse->db;
108419 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
108420 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108424 pEList = pSelect->pEList;
108426 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108427 if( pItem->u.x.iOrderByCol ){
108428 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
108429 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
108432 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
108442 static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
108445 Window *pWin = pExpr->y.pWin;
108456 if( pSelect->pWin ){
108474 ** If the order-by term is an integer I between 1 and N (where N is the
108476 ** in the resolution is a copy of the I-th result-set expression. If
108477 ** the order-by term is an identifier that corresponds to the AS-name of
108478 ** a result-set expression, then the term resolves to a copy of the
108479 ** result-set expression. Otherwise, the expression is resolved in
108480 ** the usual way - using sqlite3ResolveExprNames().
108486 static int resolveOrderGroupBy(
108492 int i, j; /* Loop counters */
108493 int iCol; /* Column number */
108496 int nResult; /* Number of terms in the result set */
108499 nResult = pSelect->pEList->nExpr;
108500 pParse = pNC->pParse;
108501 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108502 Expr *pE = pItem->pExpr;
108506 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
108508 /* If an AS-name match is found, mark this ORDER BY column as being
108509 ** a copy of the iCol-th result-set column. The subsequent call to
108511 ** copy of the iCol-th result-set expression. */
108512 pItem->u.x.iOrderByCol = (u16)iCol;
108519 ** order-by term to a copy of the result-set expression */
108524 pItem->u.x.iOrderByCol = (u16)iCol;
108529 pItem->u.x.iOrderByCol = 0;
108533 for(j=0; j<pSelect->pEList->nExpr; j++){
108534 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
108539 pItem->u.x.iOrderByCol = j+1;
108549 static int resolveSelectStep(Walker *pWalker, Select *p){
108552 int isCompound; /* True if p is a compound select */
108553 int nCompound; /* Number of compound terms processed so far */
108555 int i; /* Loop counter */
108557 Select *pLeftmost; /* Left-most of SELECT of a compound */
108562 if( p->selFlags & SF_Resolved ){
108565 pOuterNC = pWalker->u.pNC;
108566 pParse = pWalker->pParse;
108567 db = pParse->db;
108577 if( (p->selFlags & SF_Expanded)==0 ){
108579 return pParse->nErr ? WRC_Abort : WRC_Prune;
108582 isCompound = p->pPrior!=0;
108586 assert( (p->selFlags & SF_Expanded)!=0 );
108587 assert( (p->selFlags & SF_Resolved)==0 );
108588 p->selFlags |= SF_Resolved;
108596 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
108602 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
108603 ** as if it were part of the sub-query, not the parent. This block
108604 ** moves the pOrderBy down to the sub-query. It will be moved back
108606 if( p->selFlags & SF_Converted ){
108607 Select *pSub = p->pSrc->a[0].pSelect;
108608 assert( p->pSrc->nSrc==1 && p->pOrderBy );
108609 assert( pSub->pPrior && pSub->pOrderBy==0 );
108610 pSub->pOrderBy = p->pOrderBy;
108611 p->pOrderBy = 0;
108616 if( pOuterNC ) pOuterNC->nNestedSelect++;
108617 for(i=0; i<p->pSrc->nSrc; i++){
108618 SrcItem *pItem = &p->pSrc->a[i];
108619 assert( pItem->zName!=0 || pItem->pSelect!=0 );/* Test of tag-20240424-1*/
108620 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108621 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108622 const char *zSavedContext = pParse->zAuthContext;
108624 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
108625 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
108626 pParse->zAuthContext = zSavedContext;
108627 if( pParse->nErr ) return WRC_Abort;
108628 assert( db->mallocFailed==0 );
108631 ** expressions in the sub-select were resolved, the sub-select
108637 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
108638 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
108642 if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){
108643 pOuterNC->nNestedSelect--;
108646 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
108647 ** resolve the result-set expression list.
108650 sNC.pSrcList = p->pSrc;
108654 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
108657 /* If there are no aggregate functions in the result-set, and no GROUP BY
108660 assert( (p->selFlags & SF_Aggregate)==0 );
108661 pGroupBy = p->pGroupBy;
108665 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
108670 /* Add the output column list to the name-context before parsing the
108676 ** re-evaluated for each reference to it.
108679 sNC.uNC.pEList = p->pEList;
108681 if( p->pHaving ){
108682 if( (p->selFlags & SF_Aggregate)==0 ){
108683 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
108686 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
108689 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
108692 /* Resolve names in table-valued-function arguments */
108693 for(i=0; i<p->pSrc->nSrc; i++){
108694 SrcItem *pItem = &p->pSrc->a[i];
108695 if( pItem->fg.isTabFunc
108696 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
108705 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
108706 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
108707 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
108722 ** the sub-query back to the parent query. At this point each term
108726 if( p->selFlags & SF_Converted ){
108727 Select *pSub = p->pSrc->a[0].pSelect;
108728 p->pOrderBy = pSub->pOrderBy;
108729 pSub->pOrderBy = 0;
108734 ** below, after all of the result-sets for all of the elements of
108737 ** If there is an ORDER BY clause on a term of a compound-select other
108738 ** than the right-most term, then that is a syntax error. But the error
108742 if( p->pOrderBy!=0
108743 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
108744 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
108748 if( db->mallocFailed ){
108759 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
108762 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
108763 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
108773 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
108774 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
108780 p = p->pPrior;
108796 ** table columns and result-set columns. At the same time, do error
108804 ** the symbolic name assigned to an ATTACH-ed database.
108815 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
108819 ** To resolve result-set references, look for expression nodes of the
108820 ** form Z (with no X and Y prefix) where the Z matches the right-hand
108821 ** size of an AS clause in the result-set of a SELECT. The Z expression
108822 ** is replaced by a copy of the left-hand side of the result-set expression.
108823 ** Table-name and function resolution occurs on the substituted expression
108842 SQLITE_PRIVATE int sqlite3ResolveExprNames(
108846 int savedHasAgg;
108850 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108851 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108852 w.pParse = pNC->pParse;
108854 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
108858 w.pParse->nHeight += pExpr->nHeight;
108859 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108866 w.pParse->nHeight -= pExpr->nHeight;
108870 testcase( pNC->ncFlags & NC_HasAgg );
108871 testcase( pNC->ncFlags & NC_HasWin );
108872 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
108873 pNC->ncFlags |= savedHasAgg;
108874 return pNC->nNcErr>0 || w.pParse->nErr>0;
108885 SQLITE_PRIVATE int sqlite3ResolveExprListNames(
108889 int i;
108890 int savedHasAgg = 0;
108893 w.pParse = pNC->pParse;
108898 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108899 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108900 for(i=0; i<pList->nExpr; i++){
108901 Expr *pExpr = pList->a[i].pExpr;
108904 w.pParse->nHeight += pExpr->nHeight;
108905 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108911 w.pParse->nHeight -= pExpr->nHeight;
108915 testcase( pNC->ncFlags & NC_HasAgg );
108916 testcase( pNC->ncFlags & NC_HasWin );
108917 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
108918 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
108919 savedHasAgg |= pNC->ncFlags &
108921 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108923 if( w.pParse->nErr>0 ) return SQLITE_ERROR;
108925 pNC->ncFlags |= savedHasAgg;
108931 ** descendants of the SELECT, including compounds off of p->pPrior,
108962 ** ------------
108970 ** nodes of the expression is set to -1 and the Expr.iColumn value is
108975 SQLITE_PRIVATE int sqlite3ResolveSelfReference(
108978 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
108982 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
108983 NameContext sNC; /* Name context for pParse->pNewTable */
108984 int rc;
108993 sSrc.a[0].zName = pTab->zName;
108995 sSrc.a[0].iCursor = -1;
108996 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
108997 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
109029 static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
109030 static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
109035 SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
109036 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
109037 return pTab->aCol[iCol].affinity;
109044 ** or a sub-select with a column as the return value, then the
109057 int op;
109058 op = pExpr->op;
109059 while( 1 /* exit-by-break */ ){
109060 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
109062 assert( pExpr->y.pTab!=0 );
109063 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
109067 assert( pExpr->x.pSelect!=0 );
109068 assert( pExpr->x.pSelect->pEList!=0 );
109069 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
109070 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
109075 return sqlite3AffinityType(pExpr->u.zToken, 0);
109079 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
109080 assert( pExpr->iColumn < pExpr->iTable );
109081 assert( pExpr->iColumn >= 0 );
109082 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
109084 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
109089 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
109092 assert( pExpr->op==TK_COLLATE
109093 || pExpr->op==TK_IF_NULL_ROW
109094 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
109095 pExpr = pExpr->pLeft;
109096 op = pExpr->op;
109099 if( op!=TK_REGISTER || (op = pExpr->op2)==TK_REGISTER ) break;
109101 return pExpr->affExpr;
109114 SQLITE_PRIVATE int sqlite3ExprDataType(const Expr *pExpr){
109116 switch( pExpr->op ){
109120 pExpr = pExpr->pLeft;
109147 int aff = sqlite3ExprAffinity(pExpr);
109153 int res = 0;
109154 int ii;
109155 ExprList *pList = pExpr->x.pList;
109157 assert( pList->nExpr > 0);
109158 for(ii=1; ii<pList->nExpr; ii+=2){
109159 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
109161 if( pList->nExpr % 2 ){
109162 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
109179 ** If a memory allocation error occurs, that fact is recorded in pParse->db
109186 int dequote /* True to dequote pCollName */
109188 if( pCollName->n>0 ){
109189 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
109191 pNew->pLeft = pExpr;
109192 pNew->flags |= EP_Collate|EP_Skip;
109214 assert( pExpr->op==TK_COLLATE );
109215 pExpr = pExpr->pLeft;
109229 assert( pExpr->x.pList->nExpr>0 );
109230 assert( pExpr->op==TK_FUNCTION );
109231 pExpr = pExpr->x.pList->a[0].pExpr;
109232 }else if( pExpr->op==TK_COLLATE ){
109233 pExpr = pExpr->pLeft;
109256 sqlite3 *db = pParse->db;
109260 int op = p->op;
109261 if( op==TK_REGISTER ) op = p->op2;
109262 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
109265 int j;
109267 assert( p->y.pTab!=0 );
109268 if( (j = p->iColumn)>=0 ){
109269 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
109275 p = p->pLeft;
109280 p = p->x.pList->a[0].pExpr;
109285 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
109288 if( p->flags & EP_Collate ){
109289 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
109290 p = p->pLeft;
109292 Expr *pNext = p->pRight;
109294 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
109295 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
109296 int i;
109297 for(i=0; i<p->x.pList->nExpr; i++){
109298 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
109299 pNext = p->x.pList->a[i].pExpr;
109328 if( p==0 ) p = pParse->db->pDfltColl;
109336 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
109339 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
109371 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
109372 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
109373 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
109374 assert( pExpr->pLeft );
109375 aff = sqlite3ExprAffinity(pExpr->pLeft);
109376 if( pExpr->pRight ){
109377 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
109379 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
109392 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
109410 int jumpIfNull /* Extra flags added to P5 */
109436 if( pLeft->flags & EP_Collate ){
109438 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
109459 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
109461 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
109468 static int codeCompare(
109472 int opcode, /* The comparison opcode */
109473 int in1, int in2, /* Register holding operands */
109474 int dest, /* Jump here if true. */
109475 int jumpIfNull, /* If true, jump if either operand is NULL */
109476 int isCommuted /* The comparison has been commuted */
109478 int p5;
109479 int addr;
109482 if( pParse->nErr ) return 0;
109489 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
109491 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
109504 SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr){
109511 ** is a sub-select, return the number of columns in the sub-select. For
109514 SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr){
109515 u8 op = pExpr->op;
109516 if( op==TK_REGISTER ) op = pExpr->op2;
109519 return pExpr->x.pList->nExpr;
109522 return pExpr->x.pSelect->pEList->nExpr;
109529 ** Return a pointer to a subexpression of pVector that is the i-th
109539 ** just the expression for the i-th term of the result set, and may
109543 SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
109544 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
109546 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
109547 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
109549 return pVector->x.pSelect->pEList->a[i].pExpr;
109552 return pVector->x.pList->a[i].pExpr;
109561 ** the iField-th column of the vector expression pVector.
109581 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
109582 int iField, /* Which column of the vector to return */
109583 int nField /* Total number of columns in the vector */
109586 if( pVector->op==TK_SELECT ){
109594 ** pLeft->iTable: First in an array of register holding result, or 0
109607 pRet->iTable = nField;
109608 pRet->iColumn = iField;
109609 pRet->pLeft = pVector;
109612 if( pVector->op==TK_VECTOR ){
109615 ppVector = &pVector->x.pList->a[iField].pExpr;
109623 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
109631 ** sub-select returns more than one column, the first in an array
109636 static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
109637 int reg = 0;
109639 if( pExpr->op==TK_SELECT ){
109647 ** Argument pVector points to a vector expression - either a TK_VECTOR
109655 ** containing the results of the sub-select.
109664 static int exprVectorRegister(
109667 int iField, /* Field to extract from pVector */
109668 int regSelect, /* First in array of registers */
109670 int *pRegFree /* OUT: Temp register to free */
109672 u8 op = pVector->op;
109676 return pVector->iTable+iField;
109680 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
109685 *ppExpr = pVector->x.pList->a[iField].pExpr;
109698 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
109699 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
109700 ** otherwise: op==pExpr->op and p5==0
109705 int dest, /* Write results into this register */
109709 Vdbe *v = pParse->pVdbe;
109710 Expr *pLeft = pExpr->pLeft;
109711 Expr *pRight = pExpr->pRight;
109712 int nLeft = sqlite3ExprVectorSize(pLeft);
109713 int i;
109714 int regLeft = 0;
109715 int regRight = 0;
109717 int addrCmp = 0;
109718 int addrDone = sqlite3VdbeMakeLabel(pParse);
109719 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
109722 if( pParse->nErr ) return;
109727 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
109728 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
109729 || pExpr->op==TK_LT || pExpr->op==TK_GT
109730 || pExpr->op==TK_LE || pExpr->op==TK_GE
109732 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
109733 || (pExpr->op==TK_ISNOT && op==TK_NE) );
109734 assert( p5==0 || pExpr->op!=op );
109735 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
109746 int regFree1 = 0, regFree2 = 0;
109748 int r1, r2;
109763 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
109773 if( i==nLeft-1 ){
109781 if( i==nLeft-2 ) opx = op;
109797 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
109798 int rc = SQLITE_OK;
109799 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
109818 static void heightOfExpr(const Expr *p, int *pnHeight){
109820 if( p->nHeight>*pnHeight ){
109821 *pnHeight = p->nHeight;
109825 static void heightOfExprList(const ExprList *p, int *pnHeight){
109827 int i;
109828 for(i=0; i<p->nExpr; i++){
109829 heightOfExpr(p->a[i].pExpr, pnHeight);
109833 static void heightOfSelect(const Select *pSelect, int *pnHeight){
109835 for(p=pSelect; p; p=p->pPrior){
109836 heightOfExpr(p->pWhere, pnHeight);
109837 heightOfExpr(p->pHaving, pnHeight);
109838 heightOfExpr(p->pLimit, pnHeight);
109839 heightOfExprList(p->pEList, pnHeight);
109840 heightOfExprList(p->pGroupBy, pnHeight);
109841 heightOfExprList(p->pOrderBy, pnHeight);
109856 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
109857 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
109858 nHeight = p->pRight->nHeight;
109861 heightOfSelect(p->x.pSelect, &nHeight);
109862 }else if( p->x.pList ){
109863 heightOfExprList(p->x.pList, &nHeight);
109864 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
109866 p->nHeight = nHeight + 1;
109878 if( pParse->nErr ) return;
109880 sqlite3ExprCheckHeight(pParse, p->nHeight);
109887 SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *p){
109888 int nHeight = 0;
109898 if( pParse->nErr ) return;
109899 if( p && ExprUseXList(p) && p->x.pList ){
109900 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
109909 SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){
109912 pExpr->w.iOfst = iOfst;
109926 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
109929 ** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
109930 ** a string that can be translated into a 32-bit integer, then the token is
109934 ** See also tag-20240227-b.
109938 int op, /* Expression opcode */
109940 int dequote /* True to dequote */
109943 int nExtra = 0;
109944 int iValue = 0;
109948 if( op!=TK_INTEGER || pToken->z==0
109949 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
109950 nExtra = pToken->n+1; /* tag-20240227-a */
109957 pNew->op = (u8)op;
109958 pNew->iAgg = -1;
109961 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
109962 pNew->u.iValue = iValue;
109964 pNew->u.zToken = (char*)&pNew[1];
109965 assert( pToken->z!=0 || pToken->n==0 );
109966 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
109967 pNew->u.zToken[pToken->n] = 0;
109968 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
109974 pNew->nHeight = 1;
109981 ** Allocate a new expression node from a zero-terminated token that has
109986 int op, /* Expression opcode */
110008 assert( db->mallocFailed );
110013 assert( pRoot->x.pSelect==0 );
110015 pRoot->pRight = pRight;
110016 pRoot->flags |= EP_Propagate & pRight->flags;
110018 pRoot->nHeight = pRight->nHeight+1;
110020 pRoot->nHeight = 1;
110024 pRoot->pLeft = pLeft;
110025 pRoot->flags |= EP_Propagate & pLeft->flags;
110027 if( pLeft->nHeight>=pRoot->nHeight ){
110028 pRoot->nHeight = pLeft->nHeight+1;
110039 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
110044 int op, /* Expression opcode */
110049 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
110052 p->op = op & 0xff;
110053 p->iAgg = -1;
110054 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
110055 sqlite3ExprCheckHeight(pParse, p->nHeight);
110057 sqlite3ExprDelete(pParse->db, pLeft);
110058 sqlite3ExprDelete(pParse->db, pRight);
110069 pExpr->x.pSelect = pSelect;
110073 assert( pParse->db->mallocFailed );
110074 sqlite3SelectDelete(pParse->db, pSelect);
110097 SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
110098 int ii;
110101 for(ii=0; ii<pEList->nExpr; ii++){
110103 Expr *pExpr = pEList->a[ii].pExpr;
110104 int nExprElem;
110105 if( pExpr->op==TK_VECTOR ){
110107 nExprElem = pExpr->x.pList->nExpr;
110112 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
110118 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
110119 pExpr->x.pList = 0;
110122 pSel->op = TK_ALL;
110123 pSel->pPrior = pRet;
110129 if( pRet && pRet->pPrior ){
110130 pRet->selFlags |= SF_MultiValue;
110132 sqlite3ExprListDelete(pParse->db, pEList);
110145 sqlite3 *db = pParse->db;
110151 u32 f = pLeft->flags | pRight->flags;
110172 int eDistinct /* SF_Distinct or SF_ALL or 0 */
110175 sqlite3 *db = pParse->db;
110183 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
110185 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
110186 && !pParse->nested
110190 pNew->x.pList = pList;
110200 ** the arguments of a non-aggregate function.
110204 "ORDER BY may not be used with non-aggregate %#T()", p
110224 sqlite3 *db = pParse->db;
110226 assert( db->mallocFailed );
110230 assert( db->mallocFailed );
110234 assert( pExpr->op==TK_FUNCTION );
110235 assert( pExpr->pLeft==0 );
110237 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){
110238 /* Ignore ORDER BY on zero-argument aggregates */
110253 pOB->x.pList = pOrderBy;
110255 pExpr->pLeft = pOB;
110263 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
110265 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
110266 ** top-level SQL
110276 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
110278 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
110279 || (pParse->db->flags & SQLITE_TrustedSchema)==0
110310 sqlite3 *db = pParse->db;
110316 z = pExpr->u.zToken;
110323 x = (ynVar)(++pParse->nVar);
110325 int doAdd = 0;
110330 int bOk;
110331 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
110332 i = z[1]-'0'; /* The common case of ?N for a single digit N */
110335 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
110339 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
110340 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
110341 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
110343 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
110344 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110348 if( x>pParse->nVar ){
110349 pParse->nVar = (int)x;
110351 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
110359 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
110361 x = (ynVar)(++pParse->nVar);
110366 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
110369 pExpr->iColumn = x;
110370 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
110372 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110383 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110385 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
110386 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
110389 assert( p->pLeft==0 );
110390 assert( p->pRight==0 );
110391 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
110392 assert( !ExprUseXList(p) || p->x.pList==0 );
110397 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
110398 if( p->pRight ){
110400 sqlite3ExprDeleteNN(db, p->pRight);
110403 sqlite3SelectDelete(db, p->x.pSelect);
110405 sqlite3ExprListDelete(db, p->x.pList);
110408 sqlite3WindowDelete(db, p->y.pWin);
110412 if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
110413 Expr *pLeft = p->pLeft;
110443 }else if( p->pOn ){
110444 sqlite3ExprDeleteNN(db, p->pOn);
110445 }else if( p->pUsing ){
110446 sqlite3IdListDelete(db, p->pUsing);
110457 ** Return 0 if the delete was successfully deferred. Return non-zero
110460 SQLITE_PRIVATE int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
110472 sqlite3ExprDeleteNN(pParse->db, p);
110481 static int exprStructSize(const Expr *p){
110498 ** The dupedExprStructSize() function returns two values OR-ed together:
110511 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
110521 static int dupedExprStructSize(const Expr *p, int flags){
110522 int nSize;
110532 if( p->pLeft || p->x.pList ){
110535 assert( p->pRight==0 );
110547 static int dupedExprNodeSize(const Expr *p, int flags){
110548 int nByte = dupedExprStructSize(p, flags) & 0xfff;
110549 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110550 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
110566 static int dupedExprSize(const Expr *p){
110567 int nByte;
110570 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
110571 if( p->pRight ) nByte += dupedExprSize(p->pRight);
110579 ** compression while doing sqlite3ExprDup(). The top-level Expr does the
110595 ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
110601 int dupFlags, /* EXPRDUP_REDUCE for compression. 0 if not */
110607 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
110616 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
110618 sEdupBuf.zEnd = pEdupBuf->zEnd;
110624 int nAlloc;
110627 }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110628 nToken = sqlite3Strlen30NN(p->u.zToken)+1;
110649 ** by the copy of the p->u.zToken string (if any).
110652 int nNewSize = nStructSize & 0xfff;
110654 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110655 nToken = sqlite3Strlen30(p->u.zToken) + 1;
110661 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken );
110666 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=
110667 (int)EXPR_FULLSIZE+nToken );
110670 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
110676 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
110677 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
110678 pNew->flags |= staticFlag;
110684 /* Copy the p->u.zToken string, if any. */
110687 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
110688 memcpy(zToken, p->u.zToken, nToken);
110693 if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){
110695 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
110697 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
110699 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
110700 p->op!=TK_ORDER ? dupFlags : 0);
110705 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
110710 /* Fill in pNew->pLeft and pNew->pRight. */
110712 if( p->op==TK_SELECT_COLUMN ){
110713 pNew->pLeft = p->pLeft;
110714 assert( p->pRight==0
110715 || p->pRight==p->pLeft
110716 || ExprHasProperty(p->pLeft, EP_Subquery) );
110718 pNew->pLeft = p->pLeft ?
110719 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0;
110721 pNew->pRight = p->pRight ?
110722 exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0;
110724 if( p->op==TK_SELECT_COLUMN ){
110725 pNew->pLeft = p->pLeft;
110726 assert( p->pRight==0
110727 || p->pRight==p->pLeft
110728 || ExprHasProperty(p->pLeft, EP_Subquery) );
110730 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
110732 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
110744 ** and the db->mallocFailed flag set.
110750 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
110753 int i;
110754 pRet->nCte = p->nCte;
110755 for(i=0; i<p->nCte; i++){
110756 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
110757 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
110758 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
110759 pRet->a[i].eM10d = p->a[i].eM10d;
110774 ** objects found there, assembling them onto the linked list at Select->pWin.
110776 static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
110777 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
110778 Select *pSelect = pWalker->u.pSelect;
110779 Window *pWin = pExpr->y.pWin;
110782 assert( pWin->ppThis==0 );
110787 static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
110788 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
110817 ** part of the in-memory representation of the database schema.
110819 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
110823 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
110827 int i;
110834 pNew->nExpr = p->nExpr;
110835 pNew->nAlloc = p->nAlloc;
110836 pItem = pNew->a;
110837 pOldItem = p->a;
110838 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
110839 Expr *pOldExpr = pOldItem->pExpr;
110841 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
110843 && pOldExpr->op==TK_SELECT_COLUMN
110844 && (pNewExpr = pItem->pExpr)!=0
110846 if( pNewExpr->pRight ){
110847 pPriorSelectColOld = pOldExpr->pRight;
110848 pPriorSelectColNew = pNewExpr->pRight;
110849 pNewExpr->pLeft = pNewExpr->pRight;
110851 if( pOldExpr->pLeft!=pPriorSelectColOld ){
110852 pPriorSelectColOld = pOldExpr->pLeft;
110854 pNewExpr->pRight = pPriorSelectColNew;
110856 pNewExpr->pLeft = pPriorSelectColNew;
110859 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
110860 pItem->fg = pOldItem->fg;
110861 pItem->fg.done = 0;
110862 pItem->u = pOldItem->u;
110875 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
110877 int i;
110878 int nByte;
110881 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
110884 pNew->nSrc = pNew->nAlloc = p->nSrc;
110885 for(i=0; i<p->nSrc; i++){
110886 SrcItem *pNewItem = &pNew->a[i];
110887 const SrcItem *pOldItem = &p->a[i];
110889 pNewItem->pSchema = pOldItem->pSchema;
110890 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
110891 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
110892 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
110893 pNewItem->fg = pOldItem->fg;
110894 pNewItem->iCursor = pOldItem->iCursor;
110895 pNewItem->addrFillSub = pOldItem->addrFillSub;
110896 pNewItem->regReturn = pOldItem->regReturn;
110897 pNewItem->regResult = pOldItem->regResult;
110898 if( pNewItem->fg.isIndexedBy ){
110899 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
110900 }else if( pNewItem->fg.isTabFunc ){
110901 pNewItem->u1.pFuncArg =
110902 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
110904 pNewItem->u1.nRow = pOldItem->u1.nRow;
110906 pNewItem->u2 = pOldItem->u2;
110907 if( pNewItem->fg.isCte ){
110908 pNewItem->u2.pCteUse->nUse++;
110910 pTab = pNewItem->pTab = pOldItem->pTab;
110912 pTab->nTabRef++;
110914 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
110915 if( pOldItem->fg.isUsing ){
110916 assert( pNewItem->fg.isUsing );
110917 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
110919 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
110921 pNewItem->colUsed = pOldItem->colUsed;
110927 int i;
110930 assert( p->eU4!=EU4_EXPR );
110931 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
110933 pNew->nId = p->nId;
110934 pNew->eU4 = p->eU4;
110935 for(i=0; i<p->nId; i++){
110936 struct IdList_item *pNewItem = &pNew->a[i];
110937 const struct IdList_item *pOldItem = &p->a[i];
110938 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
110939 pNewItem->u4 = pOldItem->u4;
110943 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
110950 for(p=pDup; p; p=p->pPrior){
110953 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
110954 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
110955 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
110956 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
110957 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
110958 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
110959 pNew->op = p->op;
110960 pNew->pNext = pNext;
110961 pNew->pPrior = 0;
110962 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
110963 pNew->iLimit = 0;
110964 pNew->iOffset = 0;
110965 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
110966 pNew->addrOpenEphm[0] = -1;
110967 pNew->addrOpenEphm[1] = -1;
110968 pNew->nSelectRow = p->nSelectRow;
110969 pNew->pWith = sqlite3WithDup(db, p->pWith);
110971 pNew->pWin = 0;
110972 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
110973 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
110975 pNew->selId = p->selId;
110976 if( db->mallocFailed ){
110980 pNew->pNext = 0;
110985 pp = &pNew->pPrior;
110992 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
111007 ** NULL is returned. If non-NULL is returned, then it is guaranteed
111018 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
111023 pList->nAlloc = 4;
111024 pList->nExpr = 1;
111025 pItem = &pList->a[0];
111027 pItem->pExpr = pExpr;
111037 pList->nAlloc *= 2;
111039 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
111047 pItem = &pList->a[pList->nExpr++];
111049 pItem->pExpr = pExpr;
111059 return sqlite3ExprListAppendNew(pParse->db,pExpr);
111061 if( pList->nAlloc<pList->nExpr+1 ){
111062 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
111064 pItem = &pList->a[pList->nExpr++];
111066 pItem->pExpr = pExpr;
111087 sqlite3 *db = pParse->db;
111088 int n;
111089 int i;
111090 int iFirst = pList ? pList->nExpr : 0;
111101 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
111103 pColumns->nId, n);
111107 for(i=0; i<pColumns->nId; i++){
111108 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
111109 assert( pSubExpr!=0 || db->mallocFailed );
111113 assert( pList->nExpr==iFirst+i+1 );
111114 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
111115 pColumns->a[i].zName = 0;
111119 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
111120 Expr *pFirst = pList->a[iFirst].pExpr;
111122 assert( pFirst->op==TK_SELECT_COLUMN );
111126 pFirst->pRight = pExpr;
111131 pFirst->iTable = pColumns->nId;
111143 SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
111146 assert( p->nExpr>0 );
111158 pItem = &p->a[p->nExpr-1];
111159 assert( pItem->fg.bNulls==0 );
111163 pItem->fg.sortFlags = (u8)iSortOrder;
111166 pItem->fg.bNulls = 1;
111168 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
111178 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111185 int dequote /* True to cause the name to be dequoted */
111187 assert( pList!=0 || pParse->db->mallocFailed!=0 );
111188 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
111191 assert( pList->nExpr>0 );
111192 pItem = &pList->a[pList->nExpr-1];
111193 assert( pItem->zEName==0 );
111194 assert( pItem->fg.eEName==ENAME_NAME );
111195 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
111197 /* If dequote==0, then pName->z does not point to part of a DDL
111199 ** to the token-map. */
111200 sqlite3Dequote(pItem->zEName);
111202 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
111213 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111222 sqlite3 *db = pParse->db;
111223 assert( pList!=0 || db->mallocFailed!=0 );
111225 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
111226 assert( pList->nExpr>0 );
111227 if( pItem->zEName==0 ){
111228 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
111229 pItem->fg.eEName = ENAME_SPAN;
111243 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
111244 testcase( pEList && pEList->nExpr==mx );
111245 testcase( pEList && pEList->nExpr==mx+1 );
111246 if( pEList && pEList->nExpr>mx ){
111255 int i = pList->nExpr;
111256 struct ExprList_item *pItem = pList->a;
111257 assert( pList->nExpr>0 );
111260 sqlite3ExprDelete(db, pItem->pExpr);
111261 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
111263 }while( --i>0 );
111274 ** Return the bitwise-OR of all Expr.flags fields in the given
111278 int i;
111281 for(i=0; i<pList->nExpr; i++){
111282 Expr *pExpr = pList->a[i].pExpr;
111284 m |= pExpr->flags;
111290 ** This is a SELECT-node callback for the expression walker that
111292 ** pWalker->eCode to zero and abort.
111296 SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
111298 pWalker->eCode = 0;
111319 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
111322 SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
111324 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
111326 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
111328 pExpr->op = TK_TRUEFALSE;
111339 SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
111341 assert( pExpr->op==TK_TRUEFALSE );
111343 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
111344 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
111345 return pExpr->u.zToken[4]==0;
111363 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
111364 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
111365 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
111367 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
111369 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
111385 ** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
111386 ** It makes no changes to pWalker->eCode if pExpr is constant. In
111391 static SQLITE_NOINLINE int exprNodeIsConstantFunction(
111395 int n; /* Number of arguments */
111400 assert( pExpr->op==TK_FUNCTION );
111402 || (pList = pExpr->x.pList)==0
111406 n = pList->nExpr;
111408 if( pWalker->eCode==0 ) return WRC_Abort;
111410 db = pWalker->pParse->db;
111411 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
111413 || pDef->xFinalize!=0
111414 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
111417 pWalker->eCode = 0;
111432 ** sqlite3ExprIsConstant() pWalker->eCode==1
111433 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
111434 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
111435 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
111450 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111451 assert( pWalker->eCode>0 );
111453 /* If pWalker->eCode is 2 then any term of the expression that comes from
111456 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
111457 pWalker->eCode = 0;
111461 switch( pExpr->op ){
111463 ** and either pWalker->eCode==4 or 5 or the function has the
111466 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
111469 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
111471 }else if( pWalker->pParse ){
111474 pWalker->eCode = 0;
111487 testcase( pExpr->op==TK_ID );
111488 testcase( pExpr->op==TK_COLUMN );
111489 testcase( pExpr->op==TK_AGG_FUNCTION );
111490 testcase( pExpr->op==TK_AGG_COLUMN );
111491 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
111494 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
111502 testcase( pExpr->op==TK_REGISTER );
111503 testcase( pExpr->op==TK_IF_NULL_ROW );
111504 testcase( pExpr->op==TK_DOT );
111505 testcase( pExpr->op==TK_RAISE );
111506 pWalker->eCode = 0;
111509 if( pWalker->eCode==5 ){
111513 pExpr->op = TK_NULL;
111514 }else if( pWalker->eCode==4 ){
111517 pWalker->eCode = 0;
111522 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
111523 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
111527 static int exprIsConst(Parse *pParse, Expr *p, int initFlag){
111541 ** Walk an expression tree. Return non-zero if the expression is constant
111544 ** For the purposes of this function, a double-quoted string (ex: "abc")
111545 ** is considered a variable but a single-quoted string (ex: 'abc') is
111550 ** function calls will be considered to be non-constant. If pParse is
111554 SQLITE_PRIVATE int sqlite3ExprIsConstant(Parse *pParse, Expr *p){
111559 ** Walk an expression tree. Return non-zero if
111568 ** can be added to the pParse->pConstExpr list and evaluated once when
111571 static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){
111576 ** This routine examines sub-SELECT statements as an expression is being
111577 ** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
111578 ** constant as long as they are uncorrelated - meaning that they do not
111581 static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){
111583 assert( pWalker->eCode==3 || pWalker->eCode==0 );
111584 if( (pSelect->selFlags & SF_Correlated)!=0 ){
111585 pWalker->eCode = 0;
111592 ** Walk an expression tree. Return non-zero if the expression is constant
111594 ** expression must not refer to any non-deterministic function nor any
111600 static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){
111620 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
111622 ** sources anywhere else in the query. Return true (non-zero) if pExpr
111629 ** To be an single-source constraint, the following must be true:
111631 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
111634 ** true and the subquery is non-correlated
111636 ** (2b) pExpr cannot use non-deterministic functions.
111656 ** Without this restriction, accepting pExpr as a single-table
111660 ** on push-down.
111662 SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(
111665 int iSrc, /* Which element of pSrcList to use */
111666 int bAllowSubq /* Allow non-correlated subqueries */
111668 const SrcItem *pSrc = &pSrcList->a[iSrc];
111669 if( pSrc->fg.jointype & JT_LTORJ ){
111672 if( pSrc->fg.jointype & JT_LEFT ){
111674 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
111679 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
111681 int jj;
111683 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
111684 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
111692 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
111699 static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
111700 ExprList *pGroupBy = pWalker->u.pGroupBy;
111701 int i;
111705 for(i=0; i<pGroupBy->nExpr; i++){
111706 Expr *p = pGroupBy->a[i].pExpr;
111707 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
111708 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
111715 /* Check if pExpr is a sub-select. If so, consider it variable. */
111717 pWalker->eCode = 0;
111725 ** Walk the expression tree passed as the first argument. Return non-zero
111733 ** assumes that no other collating sequence will have a finer-grained
111743 SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
111756 ** in a CREATE TABLE statement. Return non-zero if the expression is
111757 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
111771 ** For the purposes of this function, a double-quoted string (ex: "abc")
111772 ** is considered a variable but a single-quoted string (ex: 'abc') is
111775 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
111785 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
111800 ** to fit in a 32-bit integer, return 1 and put the value of the integer
111802 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
111804 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue){
111805 int rc = 0;
111808 /* If an expression is an integer literal that fits in a signed 32-bit
111810 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
111811 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
111813 if( p->flags & EP_IntValue ){
111814 *pValue = p->u.iValue;
111817 switch( p->op ){
111819 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
111823 int v = 0;
111824 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
111825 assert( ((unsigned int)v)!=0x80000000 );
111826 *pValue = -v;
111850 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
111853 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
111854 p = p->pLeft;
111857 op = p->op;
111858 if( op==TK_REGISTER ) op = p->op2;
111868 || NEVER(p->y.pTab==0) /* Reference to column of index on expr */
111870 || (p->iColumn==XN_ROWID && IsView(p->y.pTab))
111872 || (p->iColumn>=0
111873 && p->y.pTab->aCol!=0 /* Possible due to prior error */
111874 && ALWAYS(p->iColumn<p->y.pTab->nCol)
111875 && p->y.pTab->aCol[p->iColumn].notNull==0);
111891 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
111893 int unaryMinus = 0;
111895 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
111896 if( p->op==TK_UMINUS ) unaryMinus = 1;
111897 p = p->pLeft;
111899 op = p->op;
111900 if( op==TK_REGISTER ) op = p->op2;
111915 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
111916 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
111925 ** Return TRUE if the given string is a row-id column name.
111927 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
111936 ** pTab. An alias is usable if there is not an explicit user-defined column
111941 int ii;
111944 int iCol;
111945 for(iCol=0; iCol<pTab->nCol; iCol++){
111946 if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break;
111948 if( iCol==pTab->nCol ){
111968 int i;
111971 p = pX->x.pSelect;
111972 if( p->pPrior ) return 0; /* Not a compound SELECT */
111973 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
111974 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
111975 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
111978 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
111979 if( p->pLimit ) return 0; /* Has no LIMIT clause */
111980 if( p->pWhere ) return 0; /* Has no WHERE clause */
111981 pSrc = p->pSrc;
111983 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
111984 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
111985 pTab = pSrc->a[0].pTab;
111989 pEList = p->pEList;
111992 for(i=0; i<pEList->nExpr; i++){
111993 Expr *pRes = pEList->a[i].pExpr;
111994 if( pRes->op!=TK_COLUMN ) return 0;
111995 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
112003 ** Generate code that checks the left-most column of index table iCur to see if
112005 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
112008 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
112009 int addr1;
112023 ** right-hand side. Return TRUE if that list is constant.
112025 static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){
112027 int res;
112029 pLHS = pIn->pLeft;
112030 pIn->pLeft = 0;
112032 pIn->pLeft = pLHS;
112042 ** The job of this routine is to find or create a b-tree object that can
112046 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
112049 ** The returned value of this function indicates the b-tree type, as follows:
112051 ** IN_INDEX_ROWID - The cursor was opened on a database table.
112052 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
112053 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
112054 ** IN_INDEX_EPH - The cursor was opened on a specially created and
112056 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
112059 ** An existing b-tree might be used if the RHS expression pX is a simple
112066 ** pX->iTable made to point to the ephemeral table instead of an
112069 ** will be set on pX and the pX->y.sub fields will be set to show where
112078 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
112079 ** through the set members) then the b-tree must not contain duplicates.
112081 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
112084 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
112087 ** index can be found with the specified <columns> as its left-most.
112091 ** routine might decide that creating an ephemeral b-tree for membership
112096 ** When the b-tree is being used for membership tests, the calling function
112105 ** the value in that register will be NULL if the b-tree contains one or more
112106 ** NULL values, and it will be some non-NULL value if the b-tree contains no
112121 SQLITE_PRIVATE int sqlite3FindInIndex(
112125 int *prRhsHasNull, /* Register holding NULL status. See notes */
112126 int *aiMap, /* Mapping from Index fields to RHS fields */
112127 int *piTab /* OUT: index to use */
112130 int eType = 0; /* Type of RHS table. IN_INDEX_* */
112131 int iTab; /* Cursor of the RHS table */
112132 int mustBeUnique; /* True if RHS must be unique */
112135 assert( pX->op==TK_IN );
112137 iTab = pParse->nTab++;
112145 int i;
112146 ExprList *pEList = pX->x.pSelect->pEList;
112147 for(i=0; i<pEList->nExpr; i++){
112148 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
112150 if( i==pEList->nExpr ){
112158 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
112159 sqlite3 *db = pParse->db; /* Database connection */
112161 int iDb; /* Database idx for pTab */
112162 ExprList *pEList = p->pEList;
112163 int nExpr = pEList->nExpr;
112165 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
112166 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
112167 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
112168 pTab = p->pSrc->a[0].pTab;
112171 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112174 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
112177 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
112179 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
112185 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
112189 int affinity_ok = 1;
112190 int i;
112197 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112198 int iCol = pEList->a[i].pExpr->iColumn;
112220 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
112223 if( pIdx->nColumn<nExpr ) continue;
112224 if( pIdx->pPartIdxWhere!=0 ) continue;
112225 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
112227 testcase( pIdx->nColumn==BMS-2 );
112228 testcase( pIdx->nColumn==BMS-1 );
112229 if( pIdx->nColumn>=BMS-1 ) continue;
112231 if( pIdx->nKeyCol>nExpr
112232 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
112240 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112241 Expr *pRhs = pEList->a[i].pExpr;
112243 int j;
112246 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
112247 assert( pIdx->azColl[j] );
112248 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
112260 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
112261 if( colUsed==(MASKBIT(nExpr)-1) ){
112263 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112265 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
112266 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
112268 VdbeComment((v, "%s", pIdx->zName));
112270 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
112274 i64 mask = (1<<nExpr)-1;
112278 *prRhsHasNull = ++pParse->nMem;
112300 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
112302 pParse->nTab--; /* Back out the allocation of the unused cursor */
112303 iTab = -1; /* Cursor is not allocated */
112308 /* Could not find an existing table or index to use as the RHS b-tree.
112311 u32 savedNQueryLoop = pParse->nQueryLoop;
112312 int rMayHaveNull = 0;
112315 pParse->nQueryLoop = 0;
112317 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
112319 assert( pX->op==TK_IN );
112324 pParse->nQueryLoop = savedNQueryLoop;
112328 int i, n;
112329 n = sqlite3ExprVectorSize(pX->pLeft);
112340 ** function allocates and returns a nul-terminated string containing
112347 Expr *pLeft = pExpr->pLeft;
112348 int nVal = sqlite3ExprVectorSize(pLeft);
112349 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
112352 assert( pExpr->op==TK_IN );
112353 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
112355 int i;
112360 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
112376 ** "sub-select returns N columns - expected M"
112378 SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
112379 if( pParse->nErr==0 ){
112380 const char *zFmt = "sub-select returns %d columns - expected %d";
112388 ** it is not permitted. If pExpr is a sub-select vector, this routine
112391 ** "sub-select returns N columns - expected 1"
112400 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
112414 ** x IN (4,5,11) -- IN operator with list on right-hand side
112415 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
112419 ** table is computed, the cursor number is also stored in pExpr->iTable,
112434 int iTab /* Use this cursor number */
112436 int addrOnce = 0; /* Address of the OP_Once instruction at top */
112437 int addr; /* Address of OP_OpenEphemeral instruction */
112440 int nVal; /* Size of vector pLeft */
112443 v = pParse->pVdbe;
112449 ** * The right-hand side is a correlated subquery
112450 ** * The right-hand side is an expression list containing variables
112456 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
112465 pExpr->x.pSelect->selId));
112468 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112469 pExpr->y.sub.iAddr);
112470 assert( iTab!=pExpr->iTable );
112471 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
112480 pExpr->y.sub.regReturn = ++pParse->nMem;
112481 pExpr->y.sub.iAddr =
112482 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112488 pLeft = pExpr->pLeft;
112494 pExpr->iTable = iTab;
112495 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
112498 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
112503 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
112511 Select *pSelect = pExpr->x.pSelect;
112512 ExprList *pEList = pSelect->pEList;
112515 addrOnce?"":"CORRELATED ", pSelect->selId
112519 if( ALWAYS(pEList->nExpr==nVal) ){
112522 int i;
112523 int rc;
112526 pSelect->iLimit = 0;
112527 testcase( pSelect->selFlags & SF_Distinct );
112529 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
112530 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
112531 sqlite3SelectDelete(pParse->db, pCopy);
112532 sqlite3DbFree(pParse->db, dest.zAffSdst);
112539 assert( pEList->nExpr>0 );
112543 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
112544 pParse, p, pEList->a[i].pExpr
112548 }else if( ALWAYS(pExpr->x.pList!=0) ){
112557 int i;
112558 ExprList *pList = pExpr->x.pList;
112560 int r1, r2;
112569 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
112575 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
112576 Expr *pE2 = pItem->pExpr;
112580 ** this code only executes once. Because for a non-constant
112584 sqlite3VdbeChangeToNoop(v, addrOnce-1);
112606 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
112607 || pParse->nErr );
112608 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
112609 pExpr->y.sub.iAddr, 1);
112620 ** (SELECT a FROM b) -- subquery
112621 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
112625 ** Return the register that holds the result. For a multi-column SELECT,
112627 ** return value is the register of the left-most result column.
112631 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
112632 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
112633 int rReg = 0; /* Register storing resulting */
112636 int nReg; /* Registers to allocate */
112639 int addrExplain; /* Address of OP_Explain instruction */
112642 Vdbe *v = pParse->pVdbe;
112644 if( pParse->nErr ) return 0;
112645 testcase( pExpr->op==TK_EXISTS );
112646 testcase( pExpr->op==TK_SELECT );
112647 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
112649 pSel = pExpr->x.pSelect;
112654 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
112656 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112657 pExpr->y.sub.iAddr);
112658 return pExpr->iTable;
112665 pExpr->y.sub.regReturn = ++pParse->nMem;
112666 pExpr->y.sub.iAddr =
112667 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112672 ** * The right-hand side is a correlated subquery
112673 ** * The right-hand side is an expression list containing variables
112694 addrOnce?"":"CORRELATED ", pSel->selId));
112695 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
112696 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
112697 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
112698 pParse->nMem += nReg;
112699 if( pExpr->op==TK_SELECT ){
112703 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
112710 if( pSel->pLimit ){
112711 /* The subquery already has a limit. If the pre-existing limit is X
112713 sqlite3 *db = pParse->db;
112716 pLimit->affExpr = SQLITE_AFF_NUMERIC;
112718 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
112720 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
112721 pSel->pLimit->pLeft = pLimit;
112723 /* If there is no pre-existing limit add a limit of 1 */
112724 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
112725 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
112727 pSel->iLimit = 0;
112729 pExpr->op2 = pExpr->op;
112730 pExpr->op = TK_ERROR;
112733 pExpr->iTable = rReg = dest.iSDParm;
112738 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
112742 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
112743 || pParse->nErr );
112744 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
112745 pExpr->y.sub.iAddr, 1);
112755 ** sub-select on the RHS of the IN() operator has the same number of
112757 ** a sub-query, that the LHS is a vector of size 1.
112759 SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
112760 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
112761 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
112762 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
112763 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
112767 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
112781 ** The left-hand side (LHS) is a scalar or vector expression. The
112782 ** right-hand side (RHS) is an array of zero or more scalar values, or a
112797 ** See the separate in-operator.md documentation file in the canonical
112803 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
112804 int destIfNull /* Jump here if the results are unknown due to NULLs */
112806 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
112807 int eType; /* Type of the RHS */
112808 int rLhs; /* Register(s) holding the LHS values */
112809 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
112811 int *aiMap = 0; /* Map from vector field to index column */
112813 int nVector; /* Size of vectors for this IN operator */
112814 int iDummy; /* Dummy parameter to exprCodeVector() */
112816 int i; /* loop counter */
112817 int destStep2; /* Where to jump when NULLs seen in step 2 */
112818 int destStep6 = 0; /* Start of code for Step 6 */
112819 int addrTruthOp; /* Address of opcode that determines the IN is true */
112820 int destNotNull; /* Jump here if a comparison is not true in step 6 */
112821 int addrTop; /* Top of the step-6 loop */
112822 int iTab = 0; /* Index to use */
112823 u8 okConstFactor = pParse->okConstFactor;
112826 pLeft = pExpr->pLeft;
112829 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
112830 aiMap = (int*)sqlite3DbMallocZero(
112831 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
112833 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
112839 v = pParse->pVdbe;
112847 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
112852 ** nVector-1. */
112854 int j, cnt;
112872 assert( pParse->okConstFactor==okConstFactor );
112873 pParse->okConstFactor = 0;
112875 pParse->okConstFactor = okConstFactor;
112892 ** This is step (1) in the in-operator.md optimized algorithm.
112897 int labelOk = sqlite3VdbeMakeLabel(pParse);
112898 int r2, regToFree;
112899 int regCkNull = 0;
112900 int ii;
112902 pList = pExpr->x.pList;
112903 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
112908 for(ii=0; ii<pList->nExpr; ii++){
112909 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
112910 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
112914 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
112915 int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
112918 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
112919 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
112920 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
112921 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
112924 int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
112952 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
112953 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
112960 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
112965 /* In this case, the RHS is the ROWID of table b-tree and so we also
112966 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
112984 /* Step 4. If the RHS is known to be non-NULL and we did not find
113017 int r3 = sqlite3GetTempReg(pParse);
113044 sqlite3DbFree(pParse->db, aiMap);
113045 sqlite3DbFree(pParse->db, zAff);
113052 ** value described by z[0..n-1] into register iMem.
113054 ** The z[] string will probably not be zero-terminated. But the
113058 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
113063 if( negateFlag ) value = -value;
113072 ** text z[0..n-1] into register iMem.
113074 ** Expr.u.zToken is always UTF8 and zero-terminated.
113076 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
113077 Vdbe *v = pParse->pVdbe;
113078 if( pExpr->flags & EP_IntValue ){
113079 int i = pExpr->u.iValue;
113081 if( negFlag ) i = -i;
113084 int c;
113086 const char *z = pExpr->u.zToken;
113091 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
113096 negFlag?"-":"",pExpr);
113104 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
113112 ** appropriate for the iIdxCol-th column of index pIdx.
113117 int iTabCur, /* Cursor pointing to a table row */
113118 int iIdxCol, /* The column of the index to be loaded */
113119 int regOut /* Store the index column value in this register */
113121 i16 iTabCol = pIdx->aiColumn[iIdxCol];
113123 assert( pIdx->aColExpr );
113124 assert( pIdx->aColExpr->nExpr>iIdxCol );
113125 pParse->iSelfTab = iTabCur + 1;
113126 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
113127 pParse->iSelfTab = 0;
113129 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
113143 int regOut /* Put the result in this register */
113145 int iAddr;
113146 Vdbe *v = pParse->pVdbe;
113147 int nErr = pParse->nErr;
113149 assert( pParse->iSelfTab!=0 );
113150 if( pParse->iSelfTab>0 ){
113151 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
113156 if( pCol->affinity>=SQLITE_AFF_TEXT ){
113157 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
113160 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
113165 ** Generate code to extract the value of the iCol-th column of a table.
113170 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
113171 int iCol, /* Index of the column to extract */
113172 int regOut /* Extract the value into this register */
113178 if( iCol<0 || iCol==pTab->iPKey ){
113180 VdbeComment((v, "%s.rowid", pTab->zName));
113182 int op;
113183 int x;
113188 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
113190 if( pCol->colFlags & COLFLAG_BUSY ){
113192 pCol->zCnName);
113194 int savedSelfTab = pParse->iSelfTab;
113195 pCol->colFlags |= COLFLAG_BUSY;
113196 pParse->iSelfTab = iTabCur+1;
113198 pParse->iSelfTab = savedSelfTab;
113199 pCol->colFlags &= ~COLFLAG_BUSY;
113218 ** Generate code that will extract the iColumn-th column from
113224 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
113227 int iColumn, /* Index of the table column */
113228 int iTable, /* The cursor pointing to the table */
113229 int iReg, /* Store results here */
113232 assert( pParse->pVdbe!=0 );
113235 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
113237 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
113238 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
113239 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG);
113245 ** Generate code to move content from registers iFrom...iFrom+nReg-1
113246 ** over to iTo..iTo+nReg-1.
113248 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
113249 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
113257 static void exprToRegister(Expr *pExpr, int iReg){
113260 p->op2 = p->op;
113261 p->op = TK_REGISTER;
113262 p->iTable = iReg;
113276 static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
113277 int iResult;
113278 int nResult = sqlite3ExprVectorSize(p);
113283 if( p->op==TK_SELECT ){
113290 int i;
113291 iResult = pParse->nMem+1;
113292 pParse->nMem += nResult;
113295 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
113303 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
113307 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
113314 ** in-line rather than by using the usual callbacks.
113316 static int exprCodeInlineFunction(
113319 int iFuncId, /* Function ID. One of the INTFUNC_... values */
113320 int target /* Store function result in this register */
113322 int nFarg;
113323 Vdbe *v = pParse->pVdbe;
113326 nFarg = pFarg->nExpr;
113327 assert( nFarg>0 ); /* All in-line functions have at least one argument */
113330 /* Attempt a direct implementation of the built-in COALESCE() and
113332 ** arguments past the first non-NULL argument.
113334 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
113335 int i;
113337 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
113341 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
113356 Expr *pArg = pFarg->a[0].pExpr;
113357 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
113358 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
113366 /* The UNLIKELY() function is a no-op. The result is the value
113370 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
113375 ** Test-only SQL functions that are only usable if enabled
113383 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113392 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113401 pA1 = pFarg->a[1].pExpr;
113402 if( pA1->op==TK_COLUMN ){
113404 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
113421 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
113425 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
113434 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
113439 static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
113442 int target /* Where to store the result of the expression */
113446 for(p=pParse->pIdxEpr; p; p=p->pIENext){
113448 int iDataCur = p->iDataCur;
113450 if( pParse->iSelfTab ){
113451 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
113452 iDataCur = -1;
113454 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
113455 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
113457 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
113458 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
113459 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
113465 v = pParse->pVdbe;
113467 if( p->bMaybeNullRow ){
113471 int addr = sqlite3VdbeCurrentAddr(v);
113472 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
113474 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
113475 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
113477 p = pParse->pIdxEpr;
113478 pParse->pIdxEpr = 0;
113480 pParse->pIdxEpr = p;
113483 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
113484 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
113488 return -1; /* Not found */
113502 static int exprPartidxExprLookup(Parse *pParse, Expr *pExpr, int iTarget){
113504 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
113505 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
113506 Vdbe *v = pParse->pVdbe;
113507 int addr = 0;
113508 int ret;
113510 if( p->bMaybeNullRow ){
113511 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur);
113513 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
113514 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0,
113515 (const char*)&p->aff, 1);
113538 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
113539 Vdbe *v = pParse->pVdbe; /* The VM under construction */
113540 int op; /* The opcode being coded */
113541 int inReg = target; /* Results stored in register inReg */
113542 int regFree1 = 0; /* If non-zero free this temporary register */
113543 int regFree2 = 0; /* If non-zero free this temporary register */
113544 int r1, r2; /* Various register numbers */
113546 int p5 = 0;
113548 assert( target>0 && target<=pParse->nMem );
113554 }else if( pParse->pIdxEpr!=0
113561 op = pExpr->op;
113566 AggInfo *pAggInfo = pExpr->pAggInfo;
113569 assert( pExpr->iAgg>=0 );
113570 if( pExpr->iAgg>=pAggInfo->nColumn ){
113576 ** tag-20230325-2 */
113582 pCol = &pAggInfo->aCol[pExpr->iAgg];
113583 if( !pAggInfo->directMode ){
113584 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
113585 }else if( pAggInfo->useSortingIdx ){
113586 Table *pTab = pCol->pTab;
113587 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
113588 pCol->iSorterColumn, target);
113591 }else if( pCol->iColumn<0 ){
113592 VdbeComment((v,"%s.rowid",pTab->zName));
113595 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
113596 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
113601 }else if( pExpr->y.pTab==0 ){
113604 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
113611 int iTab = pExpr->iTable;
113612 int iReg;
113615 ** constraints, and that constant is coded by the pExpr->pLeft
113620 int aff;
113621 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
113623 assert( pExpr->y.pTab!=0 );
113624 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
113630 &zAff[(aff-'B')*2], P4_STATIC);
113635 if( pParse->iSelfTab<0 ){
113639 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
113644 int iSrc;
113645 int iCol = pExpr->iColumn;
113647 pTab = pExpr->y.pTab;
113650 assert( iCol<pTab->nCol );
113652 return -1-pParse->iSelfTab;
113654 pCol = pTab->aCol + iCol;
113656 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
113658 if( pCol->colFlags & COLFLAG_GENERATED ){
113659 if( pCol->colFlags & COLFLAG_BUSY ){
113661 pCol->zCnName);
113664 pCol->colFlags |= COLFLAG_BUSY;
113665 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
113668 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
113672 if( pCol->affinity==SQLITE_AFF_REAL ){
113682 iTab = pParse->iSelfTab - 1;
113685 else if( pParse->pIdxPartExpr
113691 assert( pExpr->y.pTab!=0 );
113692 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
113693 pExpr->iColumn, iTab, target,
113694 pExpr->op2);
113708 codeReal(v, pExpr->u.zToken, 0, target);
113714 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
113722 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
113728 int n;
113732 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
113733 assert( pExpr->u.zToken[1]=='\'' );
113734 z = &pExpr->u.zToken[2];
113735 n = sqlite3Strlen30(z) - 1;
113744 assert( pExpr->u.zToken!=0 );
113745 assert( pExpr->u.zToken[0]!=0 );
113746 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
113750 return pExpr->iTable;
113755 sqlite3ExprCode(pParse, pExpr->pLeft, target);
113759 sqlite3AffinityType(pExpr->u.zToken, 0));
113767 /* fall-through */
113774 Expr *pLeft = pExpr->pLeft;
113779 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
113781 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
113823 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
113824 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
113831 Expr *pLeft = pExpr->pLeft;
113833 if( pLeft->op==TK_INTEGER ){
113837 }else if( pLeft->op==TK_FLOAT ){
113839 codeReal(v, pLeft->u.zToken, 1, target);
113848 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
113858 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
113864 int isTrue; /* IS TRUE or IS NOT TRUE */
113865 int bNormal; /* IS TRUE or IS FALSE */
113866 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
113868 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
113869 bNormal = pExpr->op2==TK_IS;
113877 int addr;
113881 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
113891 AggInfo *pInfo = pExpr->pAggInfo;
113893 || NEVER(pExpr->iAgg<0)
113894 || NEVER(pExpr->iAgg>=pInfo->nFunc)
113899 return AggInfoFuncReg(pInfo, pExpr->iAgg);
113905 int nFarg; /* Number of function arguments */
113909 int i; /* Loop counter */
113910 sqlite3 *db = pParse->db; /* The database connection */
113916 return pExpr->y.pWin->regResult;
113925 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
113929 pFarg = pExpr->x.pList;
113930 nFarg = pFarg ? pFarg->nExpr : 0;
113932 zId = pExpr->u.zToken;
113935 if( pDef==0 && pParse->explain ){
113939 if( pDef==0 || pDef->xFinalize!=0 ){
113943 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){
113944 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
113945 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
113947 SQLITE_PTR_TO_INT(pDef->pUserData), target);
113948 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
113953 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
113957 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
113958 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
113963 r1 = pParse->nMem+1;
113964 pParse->nMem += nFarg;
113974 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
113977 assert( pFarg->a[0].pExpr!=0 );
113978 exprOp = pFarg->a[0].pExpr->op;
113984 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
113985 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
113986 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
113987 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG;
114009 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
114011 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
114014 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
114015 if( !pColl ) pColl = db->pDfltColl;
114019 pDef, pExpr->op2);
114032 int nCol;
114035 if( pParse->db->mallocFailed ){
114039 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
114048 int n;
114049 Expr *pLeft = pExpr->pLeft;
114050 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
114051 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
114052 pLeft->op2 = pParse->withinRJSubrtn;
114054 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
114056 if( pExpr->iTable!=n ){
114058 pExpr->iTable, n);
114060 return pLeft->iTable + pExpr->iColumn;
114063 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
114064 int destIfNull = sqlite3VdbeMakeLabel(pParse);
114083 ** X is stored in pExpr->pLeft.
114084 ** Y is stored in pExpr->pList->a[0].pExpr.
114085 ** Z is stored in pExpr->pList->a[1].pExpr.
114093 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
114094 ** "SOFT-COLLATE" that is added to constraints that are pushed down
114095 ** from outer queries into sub-queries by the WHERE-clause push-down
114099 assert( pExpr->pLeft );
114100 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114104 pExpr = pExpr->pLeft;
114105 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
114110 pExpr = pExpr->pLeft;
114111 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
114116 ** to a column in the new.* or old.* pseudo-tables available to
114118 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
114119 ** is set to the column of the pseudo-table to read, or to -1 to
114124 ** to reference another column of the old.* pseudo-table, where
114126 ** set to (n+1), where n is the number of columns in each pseudo-table.
114127 ** For a reference to any other column in the new.* pseudo-table, p1
114136 ** p1==0 -> old.rowid p1==3 -> new.rowid
114137 ** p1==1 -> old.a p1==4 -> new.a
114138 ** p1==2 -> old.b p1==5 -> new.b
114141 int iCol;
114142 int p1;
114145 pTab = pExpr->y.pTab;
114146 iCol = pExpr->iColumn;
114147 p1 = pExpr->iTable * (pTab->nCol+1) + 1
114150 assert( pExpr->iTable==0 || pExpr->iTable==1 );
114151 assert( iCol>=-1 && iCol<pTab->nCol );
114152 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
114153 assert( p1>=0 && p1<(pTab->nCol*2+2) );
114157 (pExpr->iTable ? "new" : "old"),
114158 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
114165 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
114167 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
114180 ** that derive from the right-hand table of a LEFT JOIN. The
114181 ** Expr.iTable value is the table number for the right-hand table.
114186 int addrINR;
114187 u8 okConstFactor = pParse->okConstFactor;
114188 AggInfo *pAggInfo = pExpr->pAggInfo;
114190 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
114191 if( !pAggInfo->directMode ){
114192 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
114195 if( pExpr->pAggInfo->useSortingIdx ){
114196 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
114197 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
114203 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target);
114211 pParse->okConstFactor = 0; /* note (1) above */
114212 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114214 pParse->okConstFactor = okConstFactor;
114230 ** X (if it exists) is in pExpr->pLeft.
114231 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
114234 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
114241 int endLabel; /* GOTO label for end of CASE stmt */
114242 int nextCase; /* GOTO label for next WHEN clause */
114243 int nExpr; /* 2x number of WHEN terms */
114244 int i; /* Loop counter */
114251 sqlite3 *db = pParse->db;
114253 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
114254 assert(pExpr->x.pList->nExpr > 0);
114255 pEList = pExpr->x.pList;
114256 aListelem = pEList->a;
114257 nExpr = pEList->nExpr;
114259 if( (pX = pExpr->pLeft)!=0 ){
114261 if( db->mallocFailed ){
114265 testcase( pX->op==TK_COLUMN );
114273 ** The value in regFree1 might get SCopy-ed into the file result.
114278 for(i=0; i<nExpr-1; i=i+2){
114286 testcase( pTest->op==TK_COLUMN );
114288 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
114294 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
114305 assert( pExpr->affExpr==OE_Rollback
114306 || pExpr->affExpr==OE_Abort
114307 || pExpr->affExpr==OE_Fail
114308 || pExpr->affExpr==OE_Ignore
114310 if( !pParse->pTriggerTab && !pParse->nested ){
114312 "RAISE() may only be used within a trigger-program");
114315 if( pExpr->affExpr==OE_Abort ){
114319 if( pExpr->affExpr==OE_Ignore ){
114321 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
114325 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
114326 pExpr->affExpr, pExpr->u.zToken, 0, 0);
114355 SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
114358 int regDest /* Store the value in this register */
114363 p = pParse->pConstExpr;
114366 int i;
114367 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
114368 if( pItem->fg.reusable
114369 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
114371 return pItem->u.iConstExprReg;
114375 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
114377 Vdbe *v = pParse->pVdbe;
114378 int addr;
114381 pParse->okConstFactor = 0;
114382 if( !pParse->db->mallocFailed ){
114383 if( regDest<0 ) regDest = ++pParse->nMem;
114386 pParse->okConstFactor = 1;
114387 sqlite3ExprDelete(pParse->db, pExpr);
114392 struct ExprList_item *pItem = &p->a[p->nExpr-1];
114393 pItem->fg.reusable = regDest<0;
114394 if( regDest<0 ) regDest = ++pParse->nMem;
114395 pItem->u.iConstExprReg = regDest;
114397 pParse->pConstExpr = p;
114415 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
114416 int r2;
114420 && pExpr->op!=TK_REGISTER
114424 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
114426 int r1 = sqlite3GetTempReg(pParse);
114443 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
114444 int inReg;
114447 assert( target>0 && target<=pParse->nMem );
114448 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
114449 if( pParse->pVdbe==0 ) return;
114456 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
114462 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
114471 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
114472 sqlite3 *db = pParse->db;
114474 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
114484 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
114485 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
114497 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
114512 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
114515 int target, /* Where to write results */
114516 int srcReg, /* Source registers if SQLITE_ECEL_REF */
114520 int i, j, n;
114522 Vdbe *v = pParse->pVdbe;
114525 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
114526 n = pList->nExpr;
114528 for(pItem=pList->a, i=0; i<n; i++, pItem++){
114529 Expr *pExpr = pItem->pExpr;
114531 if( pItem->fg.bSorterRef ){
114532 i--;
114533 n--;
114536 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
114538 i--;
114539 n--;
114541 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
114548 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
114552 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
114553 && pOp->p1+pOp->p3+1==inReg
114554 && pOp->p2+pOp->p3+1==target+i
114555 && pOp->p5==0 /* The do-not-merge flag must be clear */
114557 pOp->p3++;
114590 int dest, /* Jump destination or storage location */
114591 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
114592 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
114597 int regFree1 = 0; /* Temporary use register */
114599 sqlite3 *db = pParse->db;
114606 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
114607 if( db->mallocFailed==0 ){
114613 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
114616 compRight.pRight = pExpr->x.pList->a[1].pExpr;
114626 pDel->flags |= EP_OuterON;
114659 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
114660 Vdbe *v = pParse->pVdbe;
114661 int op = 0;
114662 int regFree1 = 0;
114663 int regFree2 = 0;
114664 int r1, r2;
114670 op = pExpr->op;
114678 int d2 = sqlite3VdbeMakeLabel(pParse);
114680 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
114682 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
114686 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
114687 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
114693 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
114697 int isNot; /* IS NOT TRUE or IS NOT FALSE */
114698 int isTrue; /* IS TRUE or IS NOT TRUE */
114700 isNot = pExpr->op2==TK_ISNOT;
114701 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
114705 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
114708 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
114726 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
114728 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114729 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
114730 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
114750 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114765 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
114766 int destIfNull = jumpIfNull ? dest : destIfFalse;
114778 /* No-op */
114802 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
114803 Vdbe *v = pParse->pVdbe;
114804 int op = 0;
114805 int regFree1 = 0;
114806 int regFree2 = 0;
114807 int r1, r2;
114814 /* The value of pExpr->op and op are related as follows:
114816 ** pExpr->op op
114817 ** --------- ----------
114827 ** For other values of pExpr->op, op is undefined and unused.
114832 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
114836 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
114837 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
114838 assert( pExpr->op!=TK_NE || op==OP_Eq );
114839 assert( pExpr->op!=TK_EQ || op==OP_Ne );
114840 assert( pExpr->op!=TK_LT || op==OP_Ge );
114841 assert( pExpr->op!=TK_LE || op==OP_Gt );
114842 assert( pExpr->op!=TK_GT || op==OP_Le );
114843 assert( pExpr->op!=TK_GE || op==OP_Lt );
114845 switch( pExpr->op ){
114851 }else if( pExpr->op==TK_AND ){
114853 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
114854 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
114856 int d2 = sqlite3VdbeMakeLabel(pParse);
114858 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
114860 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
114867 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
114871 int isNot; /* IS NOT TRUE or IS NOT FALSE */
114872 int isTrue; /* IS TRUE or IS NOT TRUE */
114874 isNot = pExpr->op2==TK_ISNOT;
114875 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
114880 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
114885 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
114892 testcase( pExpr->op==TK_IS );
114893 testcase( pExpr->op==TK_ISNOT );
114894 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
114903 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
114905 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114906 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
114907 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
114925 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114943 int destIfNull = sqlite3VdbeMakeLabel(pParse);
114955 /* no-op */
114975 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
114976 sqlite3 *db = pParse->db;
114978 if( db->mallocFailed==0 ){
114988 ** If pExpr is a simple SQL value - an integer, real, string, blob
114989 ** or NULL value - then the VDBE currently being prepared is configured
114990 ** to re-prepare each time a new value is bound to variable pVar.
114993 ** same as that currently bound to variable pVar, non-zero is returned.
114997 static int exprCompareVariable(
115002 int res = 0;
115003 int iVar;
115006 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
115008 iVar = pVar->iColumn;
115009 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
115010 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
115013 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
115028 ** other than the top-level COLLATE operator.
115030 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115042 ** this routine is used, it does not hurt to get an extra 2 - that
115047 ** pParse->pReprepare can be matched against literals in pB. The
115048 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
115053 SQLITE_PRIVATE int sqlite3ExprCompare(
115057 int iTab
115063 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
115066 combinedFlags = pA->flags | pB->flags;
115068 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
115073 if( pA->op!=pB->op || pA->op==TK_RAISE ){
115074 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
115077 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
115080 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
115081 && pB->iTable<0 && pA->iTable==iTab
115090 if( pA->u.zToken ){
115091 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
115092 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115094 assert( pA->op==pB->op );
115099 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
115104 }else if( pA->op==TK_NULL ){
115106 }else if( pA->op==TK_COLLATE ){
115107 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115109 if( pB->u.zToken!=0
115110 && pA->op!=TK_COLUMN
115111 && pA->op!=TK_AGG_COLUMN
115112 && strcmp(pA->u.zToken,pB->u.zToken)!=0
115117 if( (pA->flags & (EP_Distinct|EP_Commuted))
115118 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
115122 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
115123 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
115124 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
115125 if( pA->op!=TK_STRING
115126 && pA->op!=TK_TRUEFALSE
115129 if( pA->iColumn!=pB->iColumn ) return 2;
115130 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
115131 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
115144 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115147 ** This routine might return non-zero for equivalent ExprLists. The
115153 ** always differs from a non-NULL pointer.
115155 SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
115156 int i;
115159 if( pA->nExpr!=pB->nExpr ) return 1;
115160 for(i=0; i<pA->nExpr; i++){
115161 int res;
115162 Expr *pExprA = pA->a[i].pExpr;
115163 Expr *pExprB = pB->a[i].pExpr;
115164 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
115171 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
115174 SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
115182 ** Return non-zero if Expr p can only be true if pNN is not NULL.
115184 ** Or if seenNot is true, return non-zero if Expr p can only be
115185 ** non-NULL if pNN is not NULL
115187 static int exprImpliesNotNull(
115191 int iTab, /* Table being evaluated */
115192 int seenNot /* Return true only if p can be any non-NULL value */
115197 return pNN->op!=TK_NULL;
115199 switch( p->op ){
115202 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
115203 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115208 pList = p->x.pList;
115210 assert( pList->nExpr==2 );
115212 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
115213 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
115217 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115237 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
115244 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
115248 if( p->op2!=TK_IS ) return 0;
115249 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115253 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115276 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
115284 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(
115288 int iTab
115293 if( pE2->op==TK_OR
115294 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
115295 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
115299 if( pE2->op==TK_NOTNULL
115300 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
115308 ** set pWalker->eCode to one only if *both* of the input expressions
115309 ** separately have the implies-not-null-row property.
115312 if( pWalker->eCode==0 ){
115314 if( pWalker->eCode ){
115315 pWalker->eCode = 0;
115323 ** If the expression node requires that the table at pWalker->iCur
115324 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
115326 ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
115331 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
115332 ** (never setting pWalker->eCode) is a harmless missed optimization.
115334 static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
115335 testcase( pExpr->op==TK_AGG_COLUMN );
115336 testcase( pExpr->op==TK_AGG_FUNCTION );
115338 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){
115339 /* If iCur is used in an inner-join ON clause to the left of a
115340 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
115342 ** To keep things simple, any use of iCur from any inner-join is
115346 switch( pExpr->op ){
115355 testcase( pExpr->op==TK_ISNOT );
115356 testcase( pExpr->op==TK_ISNULL );
115357 testcase( pExpr->op==TK_NOTNULL );
115358 testcase( pExpr->op==TK_IS );
115359 testcase( pExpr->op==TK_VECTOR );
115360 testcase( pExpr->op==TK_FUNCTION );
115361 testcase( pExpr->op==TK_TRUTH );
115362 testcase( pExpr->op==TK_CASE );
115366 if( pWalker->u.iCur==pExpr->iTable ){
115367 pWalker->eCode = 1;
115374 /* Both sides of an AND or OR must separately imply non-null-row.
115378 ** If only one of x or y is non-null-row, then the overall expression
115381 testcase( pExpr->op==TK_OR );
115382 testcase( pExpr->op==TK_AND );
115383 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
115389 ** the left-hand side of the IN is NULL then the IN itself will be
115391 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){
115392 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115397 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
115398 ** both y and z must be non-null row */
115400 assert( pExpr->x.pList->nExpr==2 );
115401 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115402 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
115403 pExpr->x.pList->a[1].pExpr);
115415 Expr *pLeft = pExpr->pLeft;
115416 Expr *pRight = pExpr->pRight;
115417 testcase( pExpr->op==TK_EQ );
115418 testcase( pExpr->op==TK_NE );
115419 testcase( pExpr->op==TK_LT );
115420 testcase( pExpr->op==TK_LE );
115421 testcase( pExpr->op==TK_GT );
115422 testcase( pExpr->op==TK_GE );
115425 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
115426 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
115427 if( (pLeft->op==TK_COLUMN
115428 && ALWAYS(pLeft->y.pTab!=0)
115429 && IsVirtual(pLeft->y.pTab))
115430 || (pRight->op==TK_COLUMN
115431 && ALWAYS(pRight->y.pTab!=0)
115432 && IsVirtual(pRight->y.pTab))
115444 ** Return true (non-zero) if expression p can only be true if at least
115445 ** one column of table iTab is non-null. In other words, return true
115462 ** be non-NULL, then the LEFT JOIN can be safely converted into an
115465 SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){
115469 if( p->op==TK_NOTNULL ){
115470 p = p->pLeft;
115472 while( p->op==TK_AND ){
115473 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
115474 p = p->pRight;
115496 int iCur; /* Cursor number for the table corresponding to the index */
115501 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
115502 ** pWalker->u.pIdxCover->pIdx.
115504 static int exprIdxCover(Walker *pWalker, Expr *pExpr){
115505 if( pExpr->op==TK_COLUMN
115506 && pExpr->iTable==pWalker->u.pIdxCover->iCur
115507 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
115509 pWalker->eCode = 1;
115525 SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
115527 int iCur, /* The cursor number for the corresponding table */
115549 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
115560 static int selectRefEnter(Walker *pWalker, Select *pSelect){
115561 struct RefSrcList *p = pWalker->u.pRefSrcList;
115562 SrcList *pSrc = pSelect->pSrc;
115564 int *piNew;
115565 if( pSrc->nSrc==0 ) return WRC_Continue;
115566 j = p->nExclude;
115567 p->nExclude += pSrc->nSrc;
115568 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
115570 p->nExclude = 0;
115573 p->aiExclude = piNew;
115575 for(i=0; i<pSrc->nSrc; i++, j++){
115576 p->aiExclude[j] = pSrc->a[i].iCursor;
115581 struct RefSrcList *p = pWalker->u.pRefSrcList;
115582 SrcList *pSrc = pSelect->pSrc;
115583 if( p->nExclude ){
115584 assert( p->nExclude>=pSrc->nSrc );
115585 p->nExclude -= pSrc->nSrc;
115591 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
115594 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
115597 static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
115598 if( pExpr->op==TK_COLUMN
115599 || pExpr->op==TK_AGG_COLUMN
115601 int i;
115602 struct RefSrcList *p = pWalker->u.pRefSrcList;
115603 SrcList *pSrc = p->pRef;
115604 int nSrc = pSrc ? pSrc->nSrc : 0;
115606 if( pExpr->iTable==pSrc->a[i].iCursor ){
115607 pWalker->eCode |= 1;
115611 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
115612 if( i>=p->nExclude ){
115613 pWalker->eCode |= 2;
115628 ** -1 pExpr only references no tables at all, or it only
115634 SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
115637 assert( pParse->db!=0 );
115644 x.db = pParse->db;
115646 assert( pExpr->op==TK_AGG_FUNCTION );
115648 sqlite3WalkExprList(&w, pExpr->x.pList);
115649 if( pExpr->pLeft ){
115650 assert( pExpr->pLeft->op==TK_ORDER );
115651 assert( ExprUseXList(pExpr->pLeft) );
115652 assert( pExpr->pLeft->x.pList!=0 );
115653 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
115657 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
115660 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
115666 return -1;
115681 static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
115683 && pExpr->pAggInfo!=0
115685 AggInfo *pAggInfo = pExpr->pAggInfo;
115686 int iAgg = pExpr->iAgg;
115687 Parse *pParse = pWalker->pParse;
115688 sqlite3 *db = pParse->db;
115690 if( pExpr->op!=TK_AGG_FUNCTION ){
115691 if( iAgg<pAggInfo->nColumn
115692 && pAggInfo->aCol[iAgg].pCExpr==pExpr
115696 pAggInfo->aCol[iAgg].pCExpr = pExpr;
115700 assert( pExpr->op==TK_AGG_FUNCTION );
115701 if( ALWAYS(iAgg<pAggInfo->nFunc)
115702 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
115706 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
115720 pWalker->pParse = pParse;
115721 pWalker->xExprCallback = agginfoPersistExprCb;
115722 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
115726 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
115729 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
115730 int i;
115731 pInfo->aCol = sqlite3ArrayAllocate(
115733 pInfo->aCol,
115734 sizeof(pInfo->aCol[0]),
115735 &pInfo->nColumn,
115742 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
115745 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
115746 int i;
115747 pInfo->aFunc = sqlite3ArrayAllocate(
115749 pInfo->aFunc,
115750 sizeof(pInfo->aFunc[0]),
115751 &pInfo->nFunc,
115761 ** If no prior entry is found, create a new one and return -1. The
115762 ** new column will have an index of pAggInfo->nColumn-1.
115770 int k;
115772 assert( pAggInfo->iFirstReg==0 );
115773 pCol = pAggInfo->aCol;
115774 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
115775 if( pCol->pCExpr==pExpr ) return;
115776 if( pCol->iTable==pExpr->iTable
115777 && pCol->iColumn==pExpr->iColumn
115778 && pExpr->op!=TK_IF_NULL_ROW
115783 k = addAggInfoColumn(pParse->db, pAggInfo);
115786 assert( pParse->db->mallocFailed );
115789 pCol = &pAggInfo->aCol[k];
115791 pCol->pTab = pExpr->y.pTab;
115792 pCol->iTable = pExpr->iTable;
115793 pCol->iColumn = pExpr->iColumn;
115794 pCol->iSorterColumn = -1;
115795 pCol->pCExpr = pExpr;
115796 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
115797 int j, n;
115798 ExprList *pGB = pAggInfo->pGroupBy;
115799 struct ExprList_item *pTerm = pGB->a;
115800 n = pGB->nExpr;
115802 Expr *pE = pTerm->pExpr;
115803 if( pE->op==TK_COLUMN
115804 && pE->iTable==pExpr->iTable
115805 && pE->iColumn==pExpr->iColumn
115807 pCol->iSorterColumn = j;
115812 if( pCol->iSorterColumn<0 ){
115813 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
115817 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
115818 pExpr->pAggInfo = pAggInfo;
115819 if( pExpr->op==TK_COLUMN ){
115820 pExpr->op = TK_AGG_COLUMN;
115822 pExpr->iAgg = (i16)k;
115830 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
115831 int i;
115832 NameContext *pNC = pWalker->u.pNC;
115833 Parse *pParse = pNC->pParse;
115834 SrcList *pSrcList = pNC->pSrcList;
115835 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
115837 assert( pNC->ncFlags & NC_UAggInfo );
115838 assert( pAggInfo->iFirstReg==0 );
115839 switch( pExpr->op ){
115843 assert( pParse->iSelfTab==0 );
115844 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
115845 if( pParse->pIdxEpr==0 ) break;
115846 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
115847 int iDataCur = pIEpr->iDataCur;
115849 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
115853 for(i=0; i<pSrcList->nSrc; i++){
115854 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
115856 if( i>=pSrcList->nSrc ) break;
115857 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
115858 if( pParse->nErr ){ return WRC_Abort; }
115866 tmp.iTable = pIEpr->iIdxCur;
115867 tmp.iColumn = pIEpr->iIdxCol;
115869 if( pParse->nErr ){ return WRC_Abort; }
115870 assert( pAggInfo->aCol!=0 );
115871 assert( tmp.iAgg<pAggInfo->nColumn );
115872 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
115873 pExpr->pAggInfo = pAggInfo;
115874 pExpr->iAgg = tmp.iAgg;
115880 testcase( pExpr->op==TK_AGG_COLUMN );
115881 testcase( pExpr->op==TK_COLUMN );
115882 testcase( pExpr->op==TK_IF_NULL_ROW );
115886 SrcItem *pItem = pSrcList->a;
115887 for(i=0; i<pSrcList->nSrc; i++, pItem++){
115889 if( pExpr->iTable==pItem->iCursor ){
115892 } /* endif pExpr->iTable==pItem->iCursor */
115898 if( (pNC->ncFlags & NC_InAggFunc)==0
115899 && pWalker->walkerDepth==pExpr->op2
115900 && pExpr->pAggInfo==0
115905 struct AggInfo_func *pItem = pAggInfo->aFunc;
115906 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
115907 if( NEVER(pItem->pFExpr==pExpr) ) break;
115908 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
115912 if( i>=pAggInfo->nFunc ){
115913 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
115915 u8 enc = ENC(pParse->db);
115916 i = addAggInfoFunc(pParse->db, pAggInfo);
115918 int nArg;
115920 pItem = &pAggInfo->aFunc[i];
115921 pItem->pFExpr = pExpr;
115923 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
115924 pItem->pFunc = sqlite3FindFunction(pParse->db,
115925 pExpr->u.zToken, nArg, enc, 0);
115926 assert( pItem->bOBUnique==0 );
115927 if( pExpr->pLeft
115928 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0
115934 assert( pExpr->pLeft->op==TK_ORDER );
115935 assert( ExprUseXList(pExpr->pLeft) );
115936 pItem->iOBTab = pParse->nTab++;
115937 pOBList = pExpr->pLeft->x.pList;
115938 assert( pOBList->nExpr>0 );
115939 assert( pItem->bOBUnique==0 );
115940 if( pOBList->nExpr==1
115942 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
115943 pExpr->x.pList->a[0].pExpr,0)==0
115945 pItem->bOBPayload = 0;
115946 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct);
115948 pItem->bOBPayload = 1;
115950 pItem->bUseSubtype =
115951 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0;
115953 pItem->iOBTab = -1;
115955 if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){
115956 pItem->iDistinct = pParse->nTab++;
115958 pItem->iDistinct = -1;
115962 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
115966 pExpr->iAgg = (i16)i;
115967 pExpr->pAggInfo = pAggInfo;
115979 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
115994 assert( pNC->pSrcList!=0 );
116006 int i;
116008 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
116009 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
116017 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
116018 if( pParse->nTempReg==0 ){
116019 return ++pParse->nMem;
116021 return pParse->aTempReg[--pParse->nTempReg];
116028 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
116031 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
116032 pParse->aTempReg[pParse->nTempReg++] = iReg;
116040 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
116041 int i, n;
116043 i = pParse->iRangeReg;
116044 n = pParse->nRangeReg;
116046 pParse->iRangeReg += nReg;
116047 pParse->nRangeReg -= nReg;
116049 i = pParse->nMem+1;
116050 pParse->nMem += nReg;
116054 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
116060 if( nReg>pParse->nRangeReg ){
116061 pParse->nRangeReg = nReg;
116062 pParse->iRangeReg = iReg;
116069 ** Always invoke this procedure after coding a subroutine or co-routine
116071 ** the sub/co-routine does not use registers in common with the code that
116072 ** invokes the sub/co-routine.
116075 pParse->nTempReg = 0;
116076 pParse->nRangeReg = 0;
116083 SQLITE_PRIVATE void sqlite3TouchRegister(Parse *pParse, int iReg){
116084 if( pParse->nMem<iReg ) pParse->nMem = iReg;
116094 SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){
116095 const ExprList *pList = pParse->pConstExpr;
116097 int i;
116098 for(i=0; i<pList->nExpr; i++){
116099 if( pList->a[i].u.iConstExprReg>=iMin ){
116100 iMin = pList->a[i].u.iConstExprReg + 1;
116104 pParse->nTempReg = 0;
116105 pParse->nRangeReg = 0;
116116 SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
116117 int i;
116118 if( pParse->nRangeReg>0
116119 && pParse->iRangeReg+pParse->nRangeReg > iFirst
116120 && pParse->iRangeReg <= iLast
116124 for(i=0; i<pParse->nTempReg; i++){
116125 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
116129 if( pParse->pConstExpr ){
116130 ExprList *pList = pParse->pConstExpr;
116131 for(i=0; i<pList->nExpr; i++){
116132 int iReg = pList->a[i].u.iConstExprReg;
116169 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
116173 static int isAlterableTable(Parse *pParse, Table *pTab){
116174 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
116176 || (pTab->tabFlags & TF_Eponymous)!=0
116177 || ( (pTab->tabFlags & TF_Shadow)!=0
116178 && sqlite3ReadOnlyShadowTables(pParse->db)
116182 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
116198 int bTemp, /* True if this is the temp db */
116200 int bNoDQS /* Do not allow DQS in the schema */
116202 pParse->colNamesSet = 1;
116226 ** Generate VM code to replace any double-quoted strings (but not double-quoted
116228 ** database zDb with their single-quoted equivalents. If argument bTemp is
116232 static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
116253 static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
116254 Vdbe *v = pParse->pVdbe;
116257 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
116258 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
116271 int iDb; /* Database that contains the table */
116274 char *zName = 0; /* NULL-terminated version of pName */
116275 sqlite3 *db = pParse->db; /* Database connection */
116276 int nTabName; /* Number of UTF-8 characters in zTabName */
116279 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
116281 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
116282 assert( pSrc->nSrc==1 );
116283 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116285 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116287 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116288 zDb = db->aDb[iDb].zDbSName;
116318 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
116325 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116336 if( pVTab->pVtab->pModule->xRename==0 ){
116352 /* figure out how many UTF-8 characters are in zName */
116353 zTabName = pTab->zName;
116354 nTabName = sqlite3Utf8CharLen(zTabName, -1);
116391 zDb, zName, pTab->zName);
116412 ** of any resources used by the v-table implementation (including other
116417 int i = ++pParse->nMem;
116452 ** The Table structure pParse->pNewTable was extended to include
116456 Table *pNew; /* Copy of pParse->pNewTable */
116458 int iDb; /* Database number */
116461 char *zCol; /* Null-terminated column definition */
116466 int r1; /* Temporary registers */
116468 db = pParse->db;
116469 assert( db->pParse==pParse );
116470 if( pParse->nErr ) return;
116471 assert( db->mallocFailed==0 );
116472 pNew = pParse->pNewTable;
116476 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
116477 zDb = db->aDb[iDb].zDbSName;
116478 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
116479 pCol = &pNew->aCol[pNew->nCol-1];
116486 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116496 if( pCol->colFlags & COLFLAG_PRIMKEY ){
116500 if( pNew->pIndex ){
116505 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
116510 assert( pDflt==0 || pDflt->op==TK_SPAN );
116511 if( pDflt && pDflt->pLeft->op==TK_NULL ){
116515 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
116517 "Cannot add a REFERENCES column with non-NULL default value");
116519 if( pCol->notNull && !pDflt ){
116530 int rc;
116534 assert( db->mallocFailed == 1 );
116539 "Cannot add a column with non-constant default");
116543 }else if( pCol->colFlags & COLFLAG_STORED ){
116549 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
116551 char *zEnd = &zCol[pColDef->n-1];
116553 *zEnd-- = '\0';
116564 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
116579 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
116589 if( pNew->pCheck!=0
116590 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
116591 || (pTab->tabFlags & TF_Strict)!=0
116596 " WHEN quick_check GLOB 'non-* value in*'"
116603 " OR quick_check GLOB 'non-* value in*'",
116611 ** This function is called by the parser after the table-name in
116612 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
116613 ** pSrc is the full-name of the table being altered.
116628 int iDb;
116629 int i;
116630 int nAlloc;
116631 sqlite3 *db = pParse->db;
116634 assert( pParse->pNewTable==0 );
116636 if( db->mallocFailed ) goto exit_begin_add_column;
116637 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116658 assert( pTab->u.tab.addColOffset>0 );
116659 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116670 pParse->pNewTable = pNew;
116671 pNew->nTabRef = 1;
116672 pNew->nCol = pTab->nCol;
116673 assert( pNew->nCol>0 );
116674 nAlloc = (((pNew->nCol-1)/8)*8)+8;
116675 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
116676 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
116677 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
116678 if( !pNew->aCol || !pNew->zName ){
116679 assert( db->mallocFailed );
116682 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
116683 for(i=0; i<pNew->nCol; i++){
116684 Column *pCol = &pNew->aCol[i];
116685 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
116686 pCol->hName = sqlite3StrIHash(pCol->zCnName);
116689 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
116690 pNew->pSchema = db->aDb[iDb].pSchema;
116691 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
116692 assert( pNew->nTabRef==1 );
116703 ** it loads an error message into pParse and returns non-zero.
116708 static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
116723 zType, pTab->zName
116740 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
116744 sqlite3 *db = pParse->db; /* Database connection */
116746 int iCol; /* Index of column being renamed */
116750 int iSchema; /* Index of the schema */
116751 int bQuote; /* True to quote the new name */
116754 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116762 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
116764 zDb = db->aDb[iSchema].zDbSName;
116768 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116777 for(iCol=0; iCol<pTab->nCol; iCol++){
116778 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
116780 if( iCol==pTab->nCol ){
116785 /* Ensure the schema contains no double-quoted strings */
116796 assert( pNew->n>0 );
116797 bQuote = sqlite3Isquote(pNew->z[0]);
116804 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
116805 pTab->zName
116812 zDb, pTab->zName, iCol, zNew, bQuote
116856 int nList; /* Number of tokens in pList */
116857 int iCol; /* Index of column being renamed */
116867 ** rename-token list.
116869 ** 2. Dereferences each pointer in the rename-token list.
116872 ** address-sanitizer or similar. If any of these pointers no longer
116873 ** point to valid objects, an exception is raised by the memory-checking
116887 assert( pParse==pParse->db->pParse );
116888 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
116889 if( pParse->nErr==0 ){
116892 for(p=pParse->pRename; p; p=p->pNext){
116893 if( p->p ){
116894 assert( p->p!=pPtr );
116895 i += *(u8*)(p->p) | 1;
116911 ** in pParse->pRename.
116923 assert( pPtr || pParse->db->mallocFailed );
116925 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
116926 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
116928 pNew->p = pPtr;
116929 pNew->t = *pToken;
116930 pNew->pNext = pParse->pRename;
116931 pParse->pRename = pNew;
116946 for(p=pParse->pRename; p; p=p->pNext){
116947 if( p->p==pFrom ){
116948 p->p = pTo;
116957 static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
116958 Parse *pParse = pWalker->pParse;
116961 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
116971 With *pWith = pSelect->pWith;
116973 Parse *pParse = pWalker->pParse;
116974 int i;
116976 assert( pWith->nCte>0 );
116977 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
116978 /* Push a copy of the With object onto the with-stack. We use a copy
116980 ** and SF_Resolved) below. And the parser code that uses the with-stack
116983 pCopy = sqlite3WithDup(pParse->db, pWith);
116986 for(i=0; i<pWith->nCte; i++){
116987 Select *p = pWith->a[i].pSelect;
116992 if( sNC.pParse->db->mallocFailed ) return;
116994 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
116996 if( pCopy && pParse->pWith==pCopy ){
116997 pParse->pWith = pCopy->pOuter;
117009 int ii;
117011 for(ii=0; ii<pIdList->nId; ii++){
117012 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
117019 static int renameUnmapSelectCb(Walker *pWalker, Select *p){
117020 Parse *pParse = pWalker->pParse;
117021 int i;
117022 if( pParse->nErr ) return WRC_Abort;
117023 testcase( p->selFlags & SF_View );
117024 testcase( p->selFlags & SF_CopyCte );
117025 if( p->selFlags & (SF_View|SF_CopyCte) ){
117028 if( ALWAYS(p->pEList) ){
117029 ExprList *pList = p->pEList;
117030 for(i=0; i<pList->nExpr; i++){
117031 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
117032 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
117036 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
117037 SrcList *pSrc = p->pSrc;
117038 for(i=0; i<pSrc->nSrc; i++){
117039 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
117040 if( pSrc->a[i].fg.isUsing==0 ){
117041 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
117043 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
117056 u8 eMode = pParse->eParseMode;
117062 pParse->eParseMode = PARSE_MODE_UNMAP;
117064 pParse->eParseMode = eMode;
117068 ** Remove all nodes that are part of expression-list pEList from the
117073 int i;
117079 for(i=0; i<pEList->nExpr; i++){
117080 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
117081 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
117094 pNext = p->pNext;
117117 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
117118 if( (*pp)->p==pPtr ){
117121 *pp = pToken->pNext;
117122 pToken->pNext = pCtx->pList;
117123 pCtx->pList = pToken;
117124 pCtx->nList++;
117135 ** descend into sub-select statements.
117137 static int renameColumnSelectCb(Walker *pWalker, Select *p){
117138 if( p->selFlags & (SF_View|SF_CopyCte) ){
117139 testcase( p->selFlags & SF_View );
117140 testcase( p->selFlags & SF_CopyCte );
117154 ** constructed in RenameCtx object at pWalker->u.pRename.
117156 static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
117157 RenameCtx *p = pWalker->u.pRename;
117158 if( pExpr->op==TK_TRIGGER
117159 && pExpr->iColumn==p->iCol
117160 && pWalker->pParse->pTriggerTab==p->pTab
117162 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117163 }else if( pExpr->op==TK_COLUMN
117164 && pExpr->iColumn==p->iCol
117166 && p->pTab==pExpr->y.pTab
117168 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117183 RenameToken *pBest = pCtx->pList;
117187 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
117188 if( pToken->t.z>pBest->t.z ) pBest = pToken;
117190 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
117191 *pp = pBest->pNext;
117198 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
117200 ** sub-routine is currently stored in pParse->zErrMsg. This function
117214 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
117216 pParse->zErrMsg
117218 sqlite3_result_error(pCtx, zErr, -1);
117219 sqlite3DbFree(pParse->db, zErr);
117223 ** For each name in the the expression-list pEList (i.e. each
117224 ** pEList->a[i].zName) that matches the string in zOld, extract the
117225 ** corresponding rename-token from Parse object pParse and add it
117235 int i;
117236 for(i=0; i<pEList->nExpr; i++){
117237 const char *zName = pEList->a[i].zEName;
117238 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
117249 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
117250 ** that matches the string in zOld, extract the corresponding rename-token
117260 int i;
117261 for(i=0; i<pIdList->nId; i++){
117262 const char *zName = pIdList->a[i].zName;
117275 static int renameParseSql(
117280 int bTemp /* True if SQL is from temp schema */
117282 int rc;
117291 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
117292 p->eParseMode = PARSE_MODE_RENAME;
117293 p->db = db;
117294 p->nQueryLoop = 1;
117296 if( db->mallocFailed ) rc = SQLITE_NOMEM;
117298 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
117307 int nSql = sqlite3Strlen30(zSql);
117309 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
117310 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
117315 db->init.iDb = 0;
117328 static int renameEditSql(
117333 int bQuote /* True to always quote token */
117338 int rc = SQLITE_OK;
117355 nQuot = sqlite3Strlen30(zQuot)-1;
117359 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
117368 /* At this point pRename->pList contains a list of RenameToken objects
117370 ** with the new column name, or with single-quoted versions of themselves.
117373 int nOut = nSql;
117375 while( pRename->pList ){
117376 int iOff; /* Offset of token to replace in zOut */
117382 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
117388 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
117391 /* Dequote the double-quoted token. Then requote it again, this time
117394 ** add another space after the new, single-quoted version of the
117397 memcpy(zBuf1, pBest->t.z, pBest->t.n);
117398 zBuf1[pBest->t.n] = 0;
117401 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
117407 iOff = pBest->t.z - zSql;
117408 if( pBest->t.n!=nReplace ){
117409 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
117410 nOut - (iOff + pBest->t.n)
117412 nOut += nReplace - pBest->t.n;
117419 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
117430 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
117432 static void renameSetENames(ExprList *pEList, int val){
117434 int i;
117435 for(i=0; i<pEList->nExpr; i++){
117436 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
117437 pEList->a[i].fg.eEName = val;
117443 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
117448 static int renameResolveTrigger(Parse *pParse){
117449 sqlite3 *db = pParse->db;
117450 Trigger *pNew = pParse->pNewTrigger;
117453 int rc = SQLITE_OK;
117457 assert( pNew->pTabSchema );
117458 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
117459 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
117461 pParse->eTriggerOp = pNew->op;
117464 if( ALWAYS(pParse->pTriggerTab) ){
117465 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
117469 if( rc==SQLITE_OK && pNew->pWhen ){
117470 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
117473 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
117474 if( pStep->pSelect ){
117475 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
117476 if( pParse->nErr ) rc = pParse->rc;
117478 if( rc==SQLITE_OK && pStep->zTarget ){
117482 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
117485 pStep->pExprList = 0;
117489 /* pStep->pExprList contains an expression-list used for an UPDATE
117493 ** pStep->pExprList to ENAME_SPAN (from their current value of
117497 renameSetENames(pStep->pExprList, ENAME_SPAN);
117499 renameSetENames(pStep->pExprList, ENAME_NAME);
117500 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
117501 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
117502 assert( pSrc==pSel->pSrc );
117503 if( pStep->pExprList ) pSel->pEList = 0;
117504 pSel->pSrc = 0;
117507 if( pStep->pFrom ){
117508 int i;
117509 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
117510 SrcItem *p = &pStep->pFrom->a[i];
117511 if( p->pSelect ){
117512 sqlite3SelectPrep(pParse, p->pSelect, 0);
117517 if( db->mallocFailed ){
117521 if( rc==SQLITE_OK && pStep->pWhere ){
117522 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
117525 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
117527 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
117528 if( pStep->pUpsert && rc==SQLITE_OK ){
117529 Upsert *pUpsert = pStep->pUpsert;
117530 pUpsert->pUpsertSrc = pSrc;
117533 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
117535 ExprList *pUpsertSet = pUpsert->pUpsertSet;
117539 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
117542 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
117564 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
117567 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
117568 sqlite3WalkSelect(pWalker, pStep->pSelect);
117569 sqlite3WalkExpr(pWalker, pStep->pWhere);
117570 sqlite3WalkExprList(pWalker, pStep->pExprList);
117571 if( pStep->pUpsert ){
117572 Upsert *pUpsert = pStep->pUpsert;
117573 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
117574 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
117575 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
117576 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
117578 if( pStep->pFrom ){
117579 int i;
117580 for(i=0; i<pStep->pFrom->nSrc; i++){
117581 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
117592 sqlite3 *db = pParse->db;
117594 if( pParse->pVdbe ){
117595 sqlite3VdbeFinalize(pParse->pVdbe);
117597 sqlite3DeleteTable(db, pParse->pNewTable);
117598 while( (pIdx = pParse->pNewIndex)!=0 ){
117599 pParse->pNewIndex = pIdx->pNext;
117602 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
117603 sqlite3DbFree(db, pParse->zErrMsg);
117604 renameTokenFree(db, pParse->pRename);
117620 ** 7. bQuote: Non-zero if the new column name should be quoted.
117624 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
117634 int NotUsed,
117642 int iCol = sqlite3_value_int(argv[5]);
117644 int bQuote = sqlite3_value_int(argv[7]);
117645 int bTemp = sqlite3_value_int(argv[8]);
117647 int rc;
117651 int i;
117654 sqlite3_xauth xAuth = db->xAuth;
117664 if( pTab==0 || iCol>=pTab->nCol ){
117668 zOld = pTab->aCol[iCol].zCnName;
117670 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
117673 db->xAuth = 0;
117688 Select *pSelect = sParse.pNewTable->u.view.pSelect;
117689 pSelect->selFlags &= ~SF_View;
117692 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
117699 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
117703 if( iCol<sParse.pNewTable->nCol ){
117705 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
117709 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
117711 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
117712 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
117713 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
117715 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
117716 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
117719 for(i=0; i<sParse.pNewTable->nCol; i++){
117721 &sParse.pNewTable->aCol[i]);
117728 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
117729 for(i=0; i<pFKey->nCol; i++){
117730 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
117731 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
117733 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
117734 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
117736 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
117742 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
117743 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
117750 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
117751 if( pStep->zTarget ){
117752 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
117754 if( pStep->pUpsert ){
117755 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
117758 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
117759 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
117767 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
117791 db->xAuth = xAuth;
117799 static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
117800 RenameCtx *p = pWalker->u.pRename;
117801 if( pExpr->op==TK_COLUMN
117803 && p->pTab==pExpr->y.pTab
117805 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
117813 static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
117814 int i;
117815 RenameCtx *p = pWalker->u.pRename;
117816 SrcList *pSrc = pSelect->pSrc;
117817 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
117818 testcase( pSelect->selFlags & SF_View );
117819 testcase( pSelect->selFlags & SF_CopyCte );
117823 assert( pWalker->pParse->db->mallocFailed );
117826 for(i=0; i<pSrc->nSrc; i++){
117827 SrcItem *pItem = &pSrc->a[i];
117828 if( pItem->pTab==p->pTab ){
117829 renameTokenFind(pWalker->pParse, p, pItem->zName);
117855 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
117859 int NotUsed,
117867 int bTemp = sqlite3_value_int(argv[6]);
117872 int rc;
117873 int bQuote = 1;
117878 sqlite3_xauth xAuth = db->xAuth;
117879 db->xAuth = 0;
117895 int isLegacy = (db->flags & SQLITE_LegacyAlter);
117901 Select *pSelect = pTab->u.view.pSelect;
117906 assert( pSelect->selFlags & SF_View );
117907 pSelect->selFlags &= ~SF_View;
117908 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
117912 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
117918 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
117923 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
117924 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
117925 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
117934 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
117937 sqlite3WalkExprList(&sWalker, pTab->pCheck);
117939 renameTokenFind(&sParse, &sCtx, pTab->zName);
117945 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
117947 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
117955 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
117956 && sCtx.pTab->pSchema==pTrigger->pTabSchema
117958 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
117965 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
117966 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
117967 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
117969 if( pStep->pFrom ){
117970 int i;
117971 for(i=0; i<pStep->pFrom->nSrc; i++){
117972 SrcItem *pItem = &pStep->pFrom->a[i];
117973 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
117974 renameTokenFind(&sParse, &sCtx, pItem->zName);
118002 db->xAuth = xAuth;
118009 static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
118010 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
118011 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
118019 ** double-quotes use single quotes instead.
118045 int NotUsed,
118053 sqlite3_xauth xAuth = db->xAuth;
118054 db->xAuth = 0;
118061 int rc;
118079 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118080 pSelect->selFlags &= ~SF_View;
118083 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118088 int i;
118089 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
118091 for(i=0; i<sParse.pNewTable->nCol; i++){
118094 &sParse.pNewTable->aCol[i]));
118099 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
118100 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
118126 db->xAuth = xAuth;
118158 int NotUsed,
118164 int bTemp = sqlite3_value_int(argv[4]);
118165 int isLegacy = (db->flags & SQLITE_LegacyAlter);
118167 int bNoDQS = sqlite3_value_int(argv[6]);
118170 sqlite3_xauth xAuth = db->xAuth;
118171 db->xAuth = 0;
118177 int rc;
118179 int flags = db->flags;
118180 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
118182 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
118188 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
118197 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
118198 int i2 = sqlite3FindDbName(db, zDb);
118215 db->xAuth = xAuth;
118224 ** argv[0]: An integer - the index of the schema containing the table
118226 ** argv[2]: An integer - the index of the column to remove.
118233 int NotUsed,
118237 int iSchema = sqlite3_value_int(argv[0]);
118239 int iCol = sqlite3_value_int(argv[2]);
118240 const char *zDb = db->aDb[iSchema].zDbSName;
118241 int rc;
118249 sqlite3_xauth xAuth = db->xAuth;
118250 db->xAuth = 0;
118257 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
118263 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
118264 if( iCol<pTab->nCol-1 ){
118266 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
118267 zEnd = (const char*)pEnd->t.z;
118270 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
118271 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
118274 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
118275 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
118281 db->xAuth = xAuth;
118297 sqlite3 *db = pParse->db; /* Database handle */
118299 int iDb; /* Index of db containing pTab in aDb[] */
118302 int iCol; /* Index of column zCol in pTab->aCol[] */
118305 assert( pParse->pNewTable==0 );
118307 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
118308 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118319 assert( db->mallocFailed );
118330 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
118332 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
118339 if( pTab->nCol<=1 ){
118345 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118347 zDb = db->aDb[iDb].zDbSName;
118350 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
118360 , zDb, iDb, iCol, pTab->zName
118368 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
118369 int i;
118370 int addr;
118371 int reg;
118372 int regRec;
118374 int nField = 0; /* Number of non-virtual columns after drop */
118375 int iCur;
118377 iCur = pParse->nTab++;
118380 reg = ++pParse->nMem;
118383 pParse->nMem += pTab->nCol;
118386 pParse->nMem += pPk->nColumn;
118387 for(i=0; i<pPk->nKeyCol; i++){
118390 nField = pPk->nKeyCol;
118392 regRec = ++pParse->nMem;
118393 for(i=0; i<pTab->nCol; i++){
118394 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
118395 int regOut;
118397 int iPos = sqlite3TableColumnToIndex(pPk, i);
118398 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
118399 if( iPos<pPk->nKeyCol ) continue;
118400 regOut = reg+1+iPos-(iPos>iColPos);
118404 if( i==pTab->iPKey ){
118407 char aff = pTab->aCol[i].affinity;
118409 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
118412 pTab->aCol[i].affinity = aff;
118419 pParse->nMem++;
118425 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
118441 ** Register built-in functions used to help implement ALTER TABLE
118458 ** 2005-07-08
118508 ** columns. The N-th integer (for N>1) is the average number of rows in
118509 ** the index which have the same value for the first N-1 columns. For
118510 ** a K-column index, there will be K+1 integers in the stat column. If
118534 ** inclusive are samples of the left-most key value in the index taken at
118541 ** For i between 0 and S-1. Conceptually, the index space is divided into
118568 ** of entries in the index whose left-most column exactly matches
118569 ** the left-most column of the sample. The second integer in nEq
118575 ** left-most column is less than the left-most column of the sample.
118576 ** The K-th integer in the nLt entry is the number of index entries
118591 ** looks at the left-most column of the index. The sqlite_stat3.sample
118592 ** column contains the actual value of the left-most column instead
118613 ** appropriate compile-time options are provided.
118624 int iDb, /* The database we are looking in */
118625 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
118641 int i;
118642 sqlite3 *db = pParse->db;
118648 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1;
118650 const int nToOpen = 1;
118656 pDb = &db->aDb[iDb];
118665 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
118668 ** side-effect of the CREATE TABLE statement is to leave the rootpage
118669 ** of the new table in register pParse->regRoot. This is important
118672 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
118674 aRoot[i] = (u32)pParse->regRoot;
118681 aRoot[i] = pStat->tnum;
118686 pDb->zDbSName, zTab, zWhereType, zWhere
118689 }else if( db->xPreUpdateCallback ){
118690 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
118694 sqlite3VdbeAddOp2(v, OP_Clear, (int)aRoot[i], iDb);
118702 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, (int)aRoot[i], iDb, 3);
118716 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
118733 int iCol; /* If !isPSample, the reason for inclusion */
118741 int nLimit; /* Analysis row-scan limit */
118742 int nCol; /* Number of columns in index + pk/rowid */
118743 int nKeyCol; /* Number of index columns w/o the pk/rowid */
118744 u8 nSkipAhead; /* Number of times of skip-ahead */
118748 int mxSample; /* Maximum number of samples to accumulate */
118749 u32 iPrn; /* Pseudo-random number used for sampling */
118751 int iMin; /* Index in a[] of entry with minimum score */
118752 int nSample; /* Current number of samples */
118753 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
118754 int iGet; /* Index of current sample accessed by stat_get() */
118764 if( p->nRowid ){
118765 sqlite3DbFree(db, p->u.aRowid);
118766 p->nRowid = 0;
118774 static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
118776 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
118777 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
118778 if( p->u.aRowid ){
118779 p->nRowid = n;
118780 memcpy(p->u.aRowid, pData, n);
118782 p->nRowid = 0;
118792 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
118793 p->nRowid = 0;
118794 p->u.iRowid = iRowid;
118804 pTo->isPSample = pFrom->isPSample;
118805 pTo->iCol = pFrom->iCol;
118806 pTo->iHash = pFrom->iHash;
118807 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
118808 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
118809 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
118810 if( pFrom->nRowid ){
118811 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
118813 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
118824 if( p->mxSample ){
118825 int i;
118826 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
118827 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
118828 sampleClear(p->db, &p->current);
118831 sqlite3DbFree(p->db, p);
118840 ** L: A limit on the number of rows to scan, or 0 for no-limit
118858 int argc,
118862 int nCol; /* Number of columns in index being sampled */
118863 int nKeyCol; /* Number of key columns */
118864 int nColUp; /* nCol rounded up for alignment */
118865 int n; /* Bytes of space to allocate */
118869 int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0;
118898 p->db = db;
118899 p->nEst = sqlite3_value_int64(argv[2]);
118900 p->nRow = 0;
118901 p->nLimit = sqlite3_value_int64(argv[3]);
118902 p->nCol = nCol;
118903 p->nKeyCol = nKeyCol;
118904 p->nSkipAhead = 0;
118905 p->current.anDLt = (tRowcnt*)&p[1];
118908 p->current.anEq = &p->current.anDLt[nColUp];
118909 p->mxSample = p->nLimit==0 ? mxSample : 0;
118912 int i; /* Used to iterate through p->aSample[] */
118914 p->iGet = -1;
118915 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
118916 p->current.anLt = &p->current.anEq[nColUp];
118917 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
118920 p->a = (struct StatSample*)&p->current.anLt[nColUp];
118921 p->aBest = &p->a[mxSample];
118922 pSpace = (u8*)(&p->a[mxSample+nCol]);
118924 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118925 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118926 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118928 assert( (pSpace - (u8*)p)==n );
118931 p->aBest[i].iCol = i;
118956 ** pNew and pOld are both candidate non-periodic samples selected for
118957 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
118964 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
118966 static int sampleIsBetterPost(
118971 int nCol = pAccum->nCol;
118972 int i;
118973 assert( pNew->iCol==pOld->iCol );
118974 for(i=pNew->iCol+1; i<nCol; i++){
118975 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
118976 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
118978 if( pNew->iHash>pOld->iHash ) return 1;
118988 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
118990 static int sampleIsBetter(
118995 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
118996 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
118998 assert( pOld->isPSample==0 && pNew->isPSample==0 );
118999 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
119003 if( pNew->iCol<pOld->iCol ) return 1;
119004 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
119010 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
119011 ** remove the least desirable sample from p->a[] to make room.
119013 static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
119015 int i;
119023 if( nEqZero>p->nMaxEqZero ){
119024 p->nMaxEqZero = nEqZero;
119026 if( pNew->isPSample==0 ){
119028 assert( pNew->anEq[pNew->iCol]>0 );
119035 for(i=p->nSample-1; i>=0; i--){
119036 StatSample *pOld = &p->a[i];
119037 if( pOld->anEq[pNew->iCol]==0 ){
119038 if( pOld->isPSample ) return;
119039 assert( pOld->iCol>pNew->iCol );
119047 pUpgrade->iCol = pNew->iCol;
119048 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
119054 if( p->nSample>=p->mxSample ){
119055 StatSample *pMin = &p->a[p->iMin];
119056 tRowcnt *anEq = pMin->anEq;
119057 tRowcnt *anLt = pMin->anLt;
119058 tRowcnt *anDLt = pMin->anDLt;
119059 sampleClear(p->db, pMin);
119060 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
119061 pSample = &p->a[p->nSample-1];
119062 pSample->nRowid = 0;
119063 pSample->anEq = anEq;
119064 pSample->anDLt = anDLt;
119065 pSample->anLt = anLt;
119066 p->nSample = p->mxSample-1;
119069 /* The "rows less-than" for the rowid column must be greater than that
119070 ** for the last sample in the p->a[] array. Otherwise, the samples would
119072 assert( p->nSample==0
119073 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
119076 pSample = &p->a[p->nSample];
119078 p->nSample++;
119081 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
119084 if( p->nSample>=p->mxSample ){
119085 int iMin = -1;
119086 for(i=0; i<p->mxSample; i++){
119087 if( p->a[i].isPSample ) continue;
119088 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
119093 p->iMin = iMin;
119101 ** p->current contains a sample that reflects the previous row of the
119105 static void samplePushPrevious(StatAccum *p, int iChng){
119106 int i;
119110 for(i=(p->nCol-2); i>=iChng; i--){
119111 StatSample *pBest = &p->aBest[i];
119112 pBest->anEq[i] = p->current.anEq[i];
119113 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
119119 ** p->nMaxEqZero or greater set to zero. */
119120 for(i=p->nSample-1; i>=0; i--){
119121 int j;
119122 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
119126 if( iChng<p->nMaxEqZero ){
119127 for(i=p->nSample-1; i>=0; i--){
119128 int j;
119129 for(j=iChng; j<p->nCol; j++){
119130 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
119133 p->nMaxEqZero = iChng;
119143 ** C Index of left-most column to differ from previous row
119153 ** if it wants the byte-code to do special processing.
119159 int argc,
119162 int i;
119166 int iChng = sqlite3_value_int(argv[1]);
119170 assert( p->nCol>0 );
119171 assert( iChng<p->nCol );
119173 if( p->nRow==0 ){
119176 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
119181 if( p->mxSample ) samplePushPrevious(p, iChng);
119188 p->current.anEq[i]++;
119191 for(i=iChng; i<p->nCol; i++){
119192 p->current.anDLt[i]++;
119194 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
119195 p->current.anEq[i] = 1;
119200 p->nRow++;
119202 if( p->mxSample ){
119205 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
119207 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
119210 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
119212 nLt = p->current.anLt[p->nCol-1];
119214 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
119215 p->current.isPSample = 1;
119216 p->current.iCol = 0;
119217 sampleInsert(p, &p->current, p->nCol-1);
119218 p->current.isPSample = 0;
119222 for(i=0; i<(p->nCol-1); i++){
119223 p->current.iCol = i;
119224 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
119225 sampleCopy(p, &p->aBest[i], &p->current);
119230 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
119231 p->nSkipAhead++;
119232 sqlite3_result_int(context, p->current.anDLt[0]>0);
119270 ** a one-parameter function, stat_get(P), that always returns the
119275 int argc,
119281 int eCall = sqlite3_value_int(argv[1]);
119287 assert( eCall==STAT_GET_STAT1 || p->mxSample );
119312 ** I = (K+D-1)/D
119320 int i; /* Loop counter */
119322 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
119324 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
119325 for(i=0; i<p->nKeyCol; i++){
119326 u64 nDistinct = p->current.anDLt[i] + 1;
119327 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
119328 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
119331 assert( p->current.anEq[i] || p->nRow==0 );
119338 if( p->iGet<0 ){
119340 p->iGet = 0;
119342 if( p->iGet<p->nSample ){
119343 StatSample *pS = p->a + p->iGet;
119344 if( pS->nRowid==0 ){
119345 sqlite3_result_int64(context, pS->u.iRowid);
119347 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
119354 int i;
119356 assert( p->iGet<p->nSample );
119358 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
119359 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
119361 aCnt = p->a[p->iGet].anDLt;
119362 p->iGet++;
119366 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
119367 for(i=0; i<p->nCol; i++){
119370 if( sStat.nChar ) sStat.nChar--;
119390 static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
119392 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
119405 ** of the k-th column of the pIdx index.
119410 int k /* Which column index */
119412 int i; /* Index of column in the table */
119413 assert( k>=0 && k<pIdx->nColumn );
119414 i = pIdx->aiColumn[k];
119416 VdbeComment((v,"%s.rowid",pIdx->zName));
119418 assert( pIdx->bHasExpr );
119419 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
119421 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
119436 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
119437 int iMem, /* Available memory locations begin here */
119438 int iTab /* Next available cursor */
119440 sqlite3 *db = pParse->db; /* Database handle */
119442 int iIdxCur; /* Cursor open on index being analyzed */
119443 int iTabCur; /* Table cursor */
119445 int i; /* Loop counter */
119446 int jZeroRows = -1; /* Jump from here if number of rows is zero */
119447 int iDb; /* Index of database containing pTab */
119449 int regNewRowid = iMem++; /* Rowid for the inserted record */
119450 int regStat = iMem++; /* Register to hold StatAccum object */
119451 int regChng = iMem++; /* Index of changed index field */
119452 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
119453 int regTemp = iMem++; /* Temporary use register */
119454 int regTemp2 = iMem++; /* Second temporary use register */
119455 int regTabname = iMem++; /* Register containing table name */
119456 int regIdxname = iMem++; /* Register containing index name */
119457 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
119458 int regPrev = iMem; /* MUST BE LAST (see below) */
119460 int doOnce = 1; /* Flag for a one-time computation */
119476 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
119481 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119485 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
119486 db->aDb[iDb].zDbSName ) ){
119492 if( db->xPreUpdateCallback ){
119495 pStat1->zName = (char*)&pStat1[1];
119496 memcpy(pStat1->zName, "sqlite_stat1", 13);
119497 pStat1->nCol = 3;
119498 pStat1->iPKey = -1;
119499 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
119503 /* Establish a read-lock on the table at the shared-cache level.
119504 ** Open a read-only cursor on the table. Also allocate a cursor number
119507 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
119510 pParse->nTab = MAX(pParse->nTab, iTab);
119512 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
119514 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119515 int nCol; /* Number of columns in pIdx. "N" */
119516 int addrGotoEnd; /* Address of "OP_Rewind iIdxCur" */
119517 int addrNextRow; /* Address of "next_row:" */
119519 int nColTest; /* Number of columns to test for changes */
119522 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
119524 nCol = pIdx->nKeyCol;
119525 zIdxName = pTab->zName;
119526 nColTest = nCol - 1;
119528 nCol = pIdx->nColumn;
119529 zIdxName = pIdx->zName;
119530 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
119535 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
119538 ** Pseudo-code for loop that calls stat_push():
119580 /* Open a read-only cursor on the index being analyzed. */
119581 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
119582 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
119584 VdbeComment((v, "%s", pIdx->zName));
119599 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
119608 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
119620 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
119621 int *aGotoChng; /* Array of jump instruction addresses */
119622 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
119637 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
119638 /* For a single-column UNIQUE index, once we have found a non-NULL
119645 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
119665 sqlite3VdbeJumpHere(v, addrNextRow-1);
119688 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
119689 int j, k, regKey;
119690 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
119691 for(j=0; j<pPk->nKeyCol; j++){
119692 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
119693 assert( k>=0 && k<pIdx->nColumn );
119697 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
119698 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
119706 if( db->nAnalysisLimit ){
119707 int j1, j2, j3;
119722 if( pIdx->pPartIdxWhere ){
119723 /* Partial indexes might get a zero-entry in sqlite_stat1. But
119734 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
119740 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
119741 int regEq = regStat1;
119742 int regLt = regStat1+1;
119743 int regDLt = regStat1+2;
119744 int regSample = regStat1+3;
119745 int regCol = regStat1+4;
119746 int regSampleRowid = regCol + nCol;
119747 int addrNext;
119748 int addrIsNull;
119759 int mxCol = nCol;
119763 for(pX=pTab->pIndex; pX; pX=pX->pNext){
119764 int nColX; /* Number of columns in pX */
119766 nColX = pX->nKeyCol;
119768 nColX = pX->nColumn;
119779 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
119783 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
119804 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
119818 VdbeComment((v, "%s", pTab->zName));
119828 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
119839 static void loadAnalysis(Parse *pParse, int iDb){
119849 static void analyzeDatabase(Parse *pParse, int iDb){
119850 sqlite3 *db = pParse->db;
119851 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
119853 int iStatCur;
119854 int iMem;
119855 int iTab;
119858 iStatCur = pParse->nTab;
119859 pParse->nTab += 3;
119861 iMem = pParse->nMem+1;
119862 iTab = pParse->nTab;
119864 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
119882 int iDb;
119883 int iStatCur;
119886 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
119887 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
119889 iStatCur = pParse->nTab;
119890 pParse->nTab += 3;
119892 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
119894 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
119896 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
119904 ** ANALYZE -- 1
119905 ** ANALYZE <database> -- 2
119906 ** ANALYZE ?<database>.?<tablename> -- 3
119913 sqlite3 *db = pParse->db;
119914 int iDb;
119915 int i;
119924 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
119932 for(i=0; i<db->nDb; i++){
119936 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
119943 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
119947 analyzeTable(pParse, pIdx->pTable, pIdx);
119955 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
119971 ** The first argument points to a nul-terminated string containing a
119976 char *zIntArray, /* String containing int array to decode */
119977 int nOut, /* Number of slots in aOut[] */
119983 int c;
119984 int i;
119995 v = v*10 + c - '0';
120014 pIndex->bUnordered = 0;
120015 pIndex->noSkipScan = 0;
120018 pIndex->bUnordered = 1;
120019 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
120020 int sz = sqlite3Atoi(z+3);
120022 pIndex->szIdxRow = sqlite3LogEst(sz);
120024 pIndex->noSkipScan = 1;
120027 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
120028 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
120040 && aLog[0] <= aLog[nOut-1] /* And only a single value seen */
120042 pIndex->bLowQual = 1;
120053 ** argv[2] = results of analysis - on integer for each column
120058 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
120070 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
120079 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
120085 int nCol = pIndex->nKeyCol+1;
120090 if( pIndex->aiRowEst==0 ){
120091 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
120092 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
120094 aiRowEst = pIndex->aiRowEst;
120096 pIndex->bUnordered = 0;
120097 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
120098 pIndex->hasStat1 = 1;
120099 if( pIndex->pPartIdxWhere==0 ){
120100 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
120101 pTable->tabFlags |= TF_HasStat1;
120105 fakeIdx.szIdxRow = pTable->szTabRow;
120109 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
120110 pTable->szTabRow = fakeIdx.szIdxRow;
120111 pTable->tabFlags |= TF_HasStat1;
120125 if( pIdx->aSample ){
120126 int j;
120127 for(j=0; j<pIdx->nSample; j++){
120128 IndexSample *p = &pIdx->aSample[j];
120129 sqlite3DbFree(db, p->p);
120131 sqlite3DbFree(db, pIdx->aSample);
120133 if( db->pnBytesFreed==0 ){
120134 pIdx->nSample = 0;
120135 pIdx->aSample = 0;
120145 ** Populate the pIdx->aAvgEq[] array based on the samples currently
120146 ** stored in pIdx->aSample[].
120150 IndexSample *aSample = pIdx->aSample;
120151 IndexSample *pFinal = &aSample[pIdx->nSample-1];
120152 int iCol;
120153 int nCol = 1;
120154 if( pIdx->nSampleCol>1 ){
120159 nCol = pIdx->nSampleCol-1;
120160 pIdx->aAvgEq[nCol] = 1;
120163 int nSample = pIdx->nSample;
120164 int i; /* Used to iterate through samples */
120171 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
120172 nRow = pFinal->anLt[iCol];
120173 nDist100 = (i64)100 * pFinal->anDLt[iCol];
120174 nSample--;
120176 nRow = pIdx->aiRowEst[0];
120177 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
120179 pIdx->nRowEst0 = nRow;
120186 if( i==(pIdx->nSample-1)
120195 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
120198 pIdx->aAvgEq[iCol] = avgEq;
120232 static int loadStatTbl(
120238 int rc; /* Result codes from subroutines */
120242 IndexSample *pSample; /* A slot in pIdx->aSample[] */
120244 assert( db->lookaside.bDisable );
120249 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120254 int nIdxCol = 1; /* Number of columns in stat4 records */
120258 int nSample; /* Number of samples */
120259 int nByte; /* Bytes of space required */
120260 int i; /* Bytes of space required */
120267 assert( pIdx==0 || pIdx->nSample==0 );
120269 if( pIdx->aSample!=0 ){
120273 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
120274 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
120275 nIdxCol = pIdx->nKeyCol;
120277 nIdxCol = pIdx->nColumn;
120279 pIdx->nSampleCol = nIdxCol;
120280 pIdx->mxSample = nSample;
120285 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
120286 if( pIdx->aSample==0 ){
120290 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
120291 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120292 pIdx->pTable->tabFlags |= TF_HasStat4;
120294 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120295 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
120296 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
120298 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
120307 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120314 int nCol = 1; /* Number of columns in index */
120320 if( pIdx->nSample>=pIdx->mxSample ){
120327 nCol = pIdx->nSampleCol;
120332 pSample = &pIdx->aSample[pIdx->nSample];
120333 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
120334 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
120335 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
120344 pSample->n = sqlite3_column_bytes(pStmt, 4);
120345 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
120346 if( pSample->p==0 ){
120350 if( pSample->n ){
120351 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
120353 pIdx->nSample++;
120364 static int loadStat4(sqlite3 *db, const char *zDb){
120365 int rc = SQLITE_OK; /* Result codes from subroutines */
120368 assert( db->lookaside.bDisable );
120399 ** If an OOM error occurs, this function always sets db->mallocFailed.
120403 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
120407 int rc = SQLITE_OK;
120408 Schema *pSchema = db->aDb[iDb].pSchema;
120411 assert( iDb>=0 && iDb<db->nDb );
120412 assert( db->aDb[iDb].pBt!=0 );
120416 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
120418 pTab->tabFlags &= ~TF_HasStat1;
120420 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120422 pIdx->hasStat1 = 0;
120425 pIdx->aSample = 0;
120431 sInfo.zDatabase = db->aDb[iDb].zDbSName;
120447 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120449 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
120459 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120461 sqlite3_free(pIdx->aiRowEst);
120462 pIdx->aiRowEst = 0;
120511 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
120513 int rc = SQLITE_OK;
120515 if( pExpr->op!=TK_ID ){
120518 pExpr->op = TK_STRING;
120528 SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
120530 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
120536 ** An SQL user-function registered to do the work of an ATTACH statement. The
120546 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
120547 ** new database, close the database on db->init.iDb and reopen it as an
120552 int NotUsed,
120555 int i;
120556 int rc = 0;
120562 unsigned int flags;
120575 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
120582 ** from sqlite3_deserialize() to close database db->init.iDb and
120594 pNew = &db->aDb[db->init.iDb];
120595 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
120596 pNew->pBt = pNewBt;
120597 pNew->pSchema = pNewSchema;
120613 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
120614 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
120615 db->aLimit[SQLITE_LIMIT_ATTACHED]
120619 for(i=0; i<db->nDb; i++){
120627 /* Allocate the new entry in the db->aDb[] array and initialize the schema
120630 if( db->aDb==db->aDbStatic ){
120631 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
120633 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
120635 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
120638 db->aDb = aNew;
120639 pNew = &db->aDb[db->nDb];
120646 flags = db->openFlags;
120647 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
120650 sqlite3_result_error(context, zErr, -1);
120656 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
120657 db->nDb++;
120658 pNew->zDbSName = sqlite3DbStrDup(db, zName);
120660 db->noSharedCache = 0;
120666 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
120667 if( !pNew->pSchema ){
120669 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
120674 sqlite3BtreeEnter(pNew->pBt);
120675 pPager = sqlite3BtreePager(pNew->pBt);
120676 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
120677 sqlite3BtreeSecureDelete(pNew->pBt,
120678 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
120680 sqlite3BtreeSetPagerFlags(pNew->pBt,
120681 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
120683 sqlite3BtreeLeave(pNew->pBt);
120685 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
120686 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
120693 ** remove the entry from the db->aDb[] array. i.e. put everything back the
120698 db->init.iDb = 0;
120699 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
120710 if( newAuth<db->auth.authLevel ){
120717 int iDb = db->nDb - 1;
120719 if( db->aDb[iDb].pBt ){
120720 sqlite3BtreeClose(db->aDb[iDb].pBt);
120721 db->aDb[iDb].pBt = 0;
120722 db->aDb[iDb].pSchema = 0;
120725 db->nDb = iDb;
120742 sqlite3_result_error(context, zErrDyn, -1);
120749 ** An SQL user-function registered to do the work of an DETACH statement. The
120758 int NotUsed,
120763 int i;
120771 for(i=0; i<db->nDb; i++){
120772 pDb = &db->aDb[i];
120773 if( pDb->pBt==0 ) continue;
120777 if( i>=db->nDb ){
120785 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
120786 || sqlite3BtreeIsInBackup(pDb->pBt)
120794 assert( db->aDb[1].pSchema );
120795 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
120798 if( pTrig->pTabSchema==pDb->pSchema ){
120799 pTrig->pTabSchema = pTrig->pSchema;
120804 sqlite3BtreeClose(pDb->pBt);
120805 pDb->pBt = 0;
120806 pDb->pSchema = 0;
120811 sqlite3_result_error(context, zErr, -1);
120820 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
120827 int rc;
120830 sqlite3* db = pParse->db;
120831 int regArgs;
120835 if( pParse->nErr ) goto attach_end;
120850 if( pAuthArg->op==TK_STRING ){
120852 zAuthArg = pAuthArg->u.zToken;
120870 assert( v || db->mallocFailed );
120872 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
120873 pFunc->nArg, pFunc, 0);
120931 static int fixExprCb(Walker *p, Expr *pExpr){
120932 DbFixer *pFix = p->u.pFix;
120933 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
120934 if( pExpr->op==TK_VARIABLE ){
120935 if( pFix->pParse->db->init.busy ){
120936 pExpr->op = TK_NULL;
120938 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
120948 static int fixSelectCb(Walker *p, Select *pSelect){
120949 DbFixer *pFix = p->u.pFix;
120950 int i;
120952 sqlite3 *db = pFix->pParse->db;
120953 int iDb = sqlite3FindDbName(db, pFix->zDb);
120954 SrcList *pList = pSelect->pSrc;
120957 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
120958 if( pFix->bTemp==0 ){
120959 if( pItem->zDatabase ){
120960 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
120961 sqlite3ErrorMsg(pFix->pParse,
120963 pFix->zType, pFix->pName, pItem->zDatabase);
120966 sqlite3DbFree(db, pItem->zDatabase);
120967 pItem->zDatabase = 0;
120968 pItem->fg.notCte = 1;
120970 pItem->pSchema = pFix->pSchema;
120971 pItem->fg.fromDDL = 1;
120974 if( pList->a[i].fg.isUsing==0
120975 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
120981 if( pSelect->pWith ){
120982 for(i=0; i<pSelect->pWith->nCte; i++){
120983 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
120998 int iDb, /* This is the database that must be used */
121002 sqlite3 *db = pParse->db;
121003 assert( db->nDb>iDb );
121004 pFix->pParse = pParse;
121005 pFix->zDb = db->aDb[iDb].zDbSName;
121006 pFix->pSchema = db->aDb[iDb].pSchema;
121007 pFix->zType = zType;
121008 pFix->pName = pName;
121009 pFix->bTemp = (iDb==1);
121010 pFix->w.pParse = pParse;
121011 pFix->w.xExprCallback = fixExprCb;
121012 pFix->w.xSelectCallback = fixSelectCb;
121013 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
121014 pFix->w.walkerDepth = 0;
121015 pFix->w.eCode = 0;
121016 pFix->w.u.pFix = pFix;
121030 ** pParse->zErrMsg and these routines return non-zero. If everything
121033 SQLITE_PRIVATE int sqlite3FixSrcList(
121037 int res = 0;
121042 res = sqlite3WalkSelect(&pFix->w, &s);
121047 SQLITE_PRIVATE int sqlite3FixSelect(
121051 return sqlite3WalkSelect(&pFix->w, pSelect);
121053 SQLITE_PRIVATE int sqlite3FixExpr(
121057 return sqlite3WalkExpr(&pFix->w, pExpr);
121062 SQLITE_PRIVATE int sqlite3FixTriggerStep(
121067 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
121068 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
121069 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
121070 || sqlite3FixSrcList(pFix, pStep->pFrom)
121077 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
121078 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
121079 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
121080 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
121081 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
121088 pStep = pStep->pNext;
121111 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
121158 ** means that the SQL statement will never-run - the sqlite3_exec() call
121166 SQLITE_API int sqlite3_set_authorizer(
121168 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
121174 sqlite3_mutex_enter(db->mutex);
121175 db->xAuth = (sqlite3_xauth)xAuth;
121176 db->pAuthArg = pArg;
121177 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
121178 sqlite3_mutex_leave(db->mutex);
121183 ** Write an error message into pParse->zErrMsg that explains that the
121184 ** user-supplied authorization function returned an illegal value.
121188 pParse->rc = SQLITE_ERROR;
121200 SQLITE_PRIVATE int sqlite3AuthReadCol(
121204 int iDb /* Index of containing database. */
121206 sqlite3 *db = pParse->db; /* Database handle */
121207 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
121208 int rc; /* Auth callback return code */
121210 if( db->init.busy ) return SQLITE_OK;
121211 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
121213 ,db->auth.zAuthUser
121218 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
121220 pParse->rc = SQLITE_AUTH;
121244 int iSrc; /* Index in pTabList->a[] of table being read */
121245 int iDb; /* The index of the database the expression refers to */
121246 int iCol; /* Index of column in table */
121248 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
121250 assert( pParse->db->xAuth!=0 );
121251 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
121258 if( pExpr->op==TK_TRIGGER ){
121259 pTab = pParse->pTriggerTab;
121262 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
121263 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
121264 pTab = pTabList->a[iSrc].pTab;
121269 iCol = pExpr->iColumn;
121273 assert( iCol<pTab->nCol );
121274 zCol = pTab->aCol[iCol].zCnName;
121275 }else if( pTab->iPKey>=0 ){
121276 assert( pTab->iPKey<pTab->nCol );
121277 zCol = pTab->aCol[pTab->iPKey].zCnName;
121281 assert( iDb>=0 && iDb<pParse->db->nDb );
121282 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
121283 pExpr->op = TK_NULL;
121293 SQLITE_PRIVATE int sqlite3AuthCheck(
121295 int code,
121300 sqlite3 *db = pParse->db;
121301 int rc;
121306 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
121307 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
121311 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
121312 ** callback are either NULL pointers or zero-terminated strings that
121320 testcase( pParse->zAuthContext==0 );
121322 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
121324 ,db->auth.zAuthUser
121329 pParse->rc = SQLITE_AUTH;
121340 ** popped. Or if pParse==0, this routine is a no-op.
121348 pContext->pParse = pParse;
121349 pContext->zAuthContext = pParse->zAuthContext;
121350 pParse->zAuthContext = zContext;
121358 if( pContext->pParse ){
121359 pContext->pParse->zAuthContext = pContext->zAuthContext;
121360 pContext->pParse = 0;
121400 int iDb; /* The database containing the table to be locked */
121407 ** Record the fact that we want to lock a table at run-time.
121418 int iDb, /* Index of the database containing the table to lock */
121424 int i;
121425 int nBytes;
121430 for(i=0; i<pToplevel->nTableLock; i++){
121431 p = &pToplevel->aTableLock[i];
121432 if( p->iDb==iDb && p->iTab==iTab ){
121433 p->isWriteLock = (p->isWriteLock || isWriteLock);
121438 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
121439 pToplevel->aTableLock =
121440 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
121441 if( pToplevel->aTableLock ){
121442 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
121443 p->iDb = iDb;
121444 p->iTab = iTab;
121445 p->isWriteLock = isWriteLock;
121446 p->zLockName = zName;
121448 pToplevel->nTableLock = 0;
121449 sqlite3OomFault(pToplevel->db);
121454 int iDb, /* Index of the database containing the table to lock */
121460 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
121469 int i;
121470 Vdbe *pVdbe = pParse->pVdbe;
121473 for(i=0; i<pParse->nTableLock; i++){
121474 TableLock *p = &pParse->aTableLock[i];
121475 int p1 = p->iDb;
121476 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
121477 p->zLockName, P4_STATIC);
121485 ** Return TRUE if the given yDbMask object is empty - if it contains no
121490 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
121491 int i;
121510 int iDb, i;
121512 assert( pParse->pToplevel==0 );
121513 db = pParse->db;
121514 assert( db->pParse==pParse );
121515 if( pParse->nested ) return;
121516 if( pParse->nErr ){
121517 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
121520 assert( db->mallocFailed==0 );
121525 v = pParse->pVdbe;
121527 if( db->init.busy ){
121528 pParse->rc = SQLITE_DONE;
121532 if( v==0 ) pParse->rc = SQLITE_ERROR;
121534 assert( !pParse->isMultiWrite
121535 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
121537 if( pParse->bReturning ){
121538 Returning *pReturning = pParse->u1.pReturning;
121539 int addrRewind;
121540 int reg;
121542 if( pReturning->nRetCol ){
121545 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
121547 reg = pReturning->iRetReg;
121548 for(i=0; i<pReturning->nRetCol; i++){
121549 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
121552 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
121560 if( pParse->nTableLock>0 && db->init.busy==0 ){
121562 if( db->auth.authLevel<UAUTH_User ){
121564 pParse->rc = SQLITE_AUTH_USER;
121576 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
121578 assert( db->nDb>0 );
121582 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
121584 pSchema = db->aDb[iDb].pSchema;
121588 DbMaskTest(pParse->writeMask,iDb), /* P2 */
121589 pSchema->schema_cookie, /* P3 */
121590 pSchema->iGeneration /* P4 */
121592 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
121594 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
121595 }while( ++iDb<db->nDb );
121597 for(i=0; i<pParse->nVtabLock; i++){
121598 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
121601 pParse->nVtabLock = 0;
121606 ** obtain the required table-locks. This is a no-op unless the
121607 ** shared-cache feature is enabled.
121609 if( pParse->nTableLock ) codeTableLocks(pParse);
121614 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
121618 if( pParse->pConstExpr ){
121619 ExprList *pEL = pParse->pConstExpr;
121620 pParse->okConstFactor = 0;
121621 for(i=0; i<pEL->nExpr; i++){
121622 assert( pEL->a[i].u.iConstExprReg>0 );
121623 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
121627 if( pParse->bReturning ){
121628 Returning *pRet = pParse->u1.pReturning;
121629 if( pRet->nRetCol ){
121630 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
121640 assert( v!=0 || pParse->nErr );
121641 assert( db->mallocFailed==0 || pParse->nErr );
121642 if( pParse->nErr==0 ){
121645 assert( pParse->pAinc==0 || pParse->nTab>0 );
121647 pParse->rc = SQLITE_DONE;
121649 pParse->rc = SQLITE_ERROR;
121662 ** * Built-in SQL functions always take precedence over application-defined
121664 ** built-in function.
121669 sqlite3 *db = pParse->db;
121670 u32 savedDbFlags = db->mDbFlags;
121673 if( pParse->nErr ) return;
121674 if( pParse->eParseMode ) return;
121675 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
121683 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
121684 pParse->nErr++;
121687 pParse->nested++;
121690 db->mDbFlags |= DBFLAG_PreferBuiltin;
121692 db->mDbFlags = savedDbFlags;
121695 pParse->nested--;
121703 SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
121709 ** Locate the in-memory structure that describes a particular database
121722 int i;
121729 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
121734 for(i=0; i<db->nDb; i++){
121735 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
121737 if( i>=db->nDb ){
121746 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
121753 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
121758 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
121765 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
121768 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
121771 for(i=2; i<db->nDb; i++){
121773 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
121778 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
121780 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
121789 ** Locate the in-memory structure that describes a particular database
121792 ** error message in pParse->zErrMsg.
121795 ** routine leaves an error message in pParse->zErrMsg where
121805 sqlite3 *db = pParse->db;
121809 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
121821 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
121822 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
121827 testcase( pMod->pEpoTab==0 );
121828 return pMod->pEpoTab;
121833 pParse->checkSchema = 1;
121834 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
121846 assert( HasRowid(p) || p->iPKey<0 );
121857 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
121858 ** non-NULL if it is part of a view or trigger program definition. See
121867 assert( p->pSchema==0 || p->zDatabase==0 );
121868 if( p->pSchema ){
121869 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
121870 zDb = pParse->db->aDb[iDb].zDbSName;
121872 zDb = p->zDatabase;
121874 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
121894 ** Locate the in-memory structure that describes
121907 int i;
121910 for(i=OMIT_TEMPDB; i<db->nDb; i++){
121911 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
121912 Schema *pSchema = db->aDb[j].pSchema;
121916 p = sqlite3HashFind(&pSchema->idxHash, zName);
121929 sqlite3ExprDelete(db, p->pPartIdxWhere);
121930 sqlite3ExprListDelete(db, p->aColExpr);
121931 sqlite3DbFree(db, p->zColAff);
121932 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
121934 sqlite3_free(p->aiRowEst);
121945 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
121950 pHash = &db->aDb[iDb].pSchema->idxHash;
121953 if( pIndex->pTable->pIndex==pIndex ){
121954 pIndex->pTable->pIndex = pIndex->pNext;
121959 p = pIndex->pTable->pIndex;
121960 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
121961 if( ALWAYS(p && p->pNext==pIndex) ){
121962 p->pNext = pIndex->pNext;
121967 db->mDbFlags |= DBFLAG_SchemaChange;
121971 ** Look through the list of open database files in db->aDb[] and if
121973 ** db->aDb[] structure to a smaller size, if possible.
121979 int i, j;
121980 for(i=j=2; i<db->nDb; i++){
121981 struct Db *pDb = &db->aDb[i];
121982 if( pDb->pBt==0 ){
121983 sqlite3DbFree(db, pDb->zDbSName);
121984 pDb->zDbSName = 0;
121988 db->aDb[j] = db->aDb[i];
121992 db->nDb = j;
121993 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
121994 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
121995 sqlite3DbFree(db, db->aDb);
121996 db->aDb = db->aDbStatic;
122002 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
122005 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
122006 int i;
122007 assert( iDb<db->nDb );
122013 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
122016 if( db->nSchemaLock==0 ){
122017 for(i=0; i<db->nDb; i++){
122019 sqlite3SchemaClear(db->aDb[i].pSchema);
122030 int i;
122032 for(i=0; i<db->nDb; i++){
122033 Db *pDb = &db->aDb[i];
122034 if( pDb->pSchema ){
122035 if( db->nSchemaLock==0 ){
122036 sqlite3SchemaClear(pDb->pSchema);
122042 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
122045 if( db->nSchemaLock==0 ){
122054 db->mDbFlags &= ~DBFLAG_SchemaChange;
122070 pList = pTab->u.tab.pDfltList;
122071 if( pCol->iDflt==0
122073 || NEVER(pList->nExpr<pCol->iDflt)
122075 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
122076 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
122078 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
122079 pList->a[pCol->iDflt-1].pExpr = pExpr;
122089 if( pCol->iDflt==0 ) return 0;
122091 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
122092 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
122093 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
122108 n = sqlite3Strlen30(pCol->zCnName) + 1;
122109 if( pCol->colFlags & COLFLAG_HASTYPE ){
122110 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
122113 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
122115 pCol->zCnName = zNew;
122116 memcpy(pCol->zCnName + n, zColl, nColl);
122117 pCol->colFlags |= COLFLAG_HASCOLL;
122126 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
122127 z = pCol->zCnName;
122129 if( pCol->colFlags & COLFLAG_HASTYPE ){
122140 int i;
122144 if( (pCol = pTable->aCol)!=0 ){
122145 for(i=0; i<pTable->nCol; i++, pCol++){
122146 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
122147 sqlite3DbFree(db, pCol->zCnName);
122149 sqlite3DbNNFreeNN(db, pTable->aCol);
122151 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
122153 if( db->pnBytesFreed==0 ){
122154 pTable->aCol = 0;
122155 pTable->nCol = 0;
122157 pTable->u.tab.pDfltList = 0;
122175 ** db parameter can be used with db->pnBytesFreed to measure the memory
122189 int nLookaside = 0;
122191 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
122197 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
122198 pNext = pIndex->pNext;
122199 assert( pIndex->pSchema==pTable->pSchema
122200 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
122201 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
122202 char *zName = pIndex->zName;
122204 &pIndex->pSchema->idxHash, zName, 0
122206 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
122222 sqlite3SelectDelete(db, pTable->u.view.pSelect);
122228 sqlite3DbFree(db, pTable->zName);
122229 sqlite3DbFree(db, pTable->zColAff);
122230 sqlite3ExprListDelete(db, pTable->pCheck);
122240 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
122252 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
122257 assert( iDb>=0 && iDb<db->nDb );
122260 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
122261 pDb = &db->aDb[iDb];
122262 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
122264 db->mDbFlags |= DBFLAG_SchemaChange;
122283 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
122295 SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *p, int iDb){
122299 if( p->nTab==0 ){
122300 p->nTab = 1;
122305 ** Parameter zName points to a nul-terminated buffer containing the name
122307 ** function returns the index of the named database in db->aDb[], or
122308 ** -1 if the named db cannot be found.
122310 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
122311 int i = -1; /* Database number */
122314 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
122315 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
122327 ** index of the named database in db->aDb[], or -1 if the named db
122330 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
122331 int i; /* Database number */
122355 SQLITE_PRIVATE int sqlite3TwoPartName(
122361 int iDb; /* Database holding the object */
122362 sqlite3 *db = pParse->db;
122365 if( pName2->n>0 ){
122366 if( db->init.busy ) {
122368 return -1;
122374 return -1;
122377 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
122378 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
122379 iDb = db->init.iDb;
122388 SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
122389 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
122390 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122392 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122394 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122396 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
122400 ** This routine is used to check if the UTF-8 string zName is a legal
122410 SQLITE_PRIVATE int sqlite3CheckObjectName(
122416 sqlite3 *db = pParse->db;
122418 || db->init.imposterTable
122424 if( db->init.busy ){
122425 if( sqlite3_stricmp(zType, db->init.azInit[0])
122426 || sqlite3_stricmp(zName, db->init.azInit[1])
122427 || sqlite3_stricmp(zTblName, db->init.azInit[2])
122433 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
122450 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
122457 ** find the (first) offset of that column in index pIdx. Or return -1
122461 int i;
122462 for(i=0; i<pIdx->nColumn; i++){
122463 if( iCol==pIdx->aiColumn[i] ) return i;
122465 return -1;
122478 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
122481 if( pTab->tabFlags & TF_HasVirtual ){
122482 int i;
122484 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
122496 ** the N-th virtual column (zero-based) then the storage number is
122497 ** the number of non-virtual columns in the table plus N.
122513 ** -- 0 1 2 3 4 5 6 7 8
122524 ** this routine is a no-op macro. If the pTab does not have any virtual
122525 ** columns, then this routine is no-op that always return iCol. If iCol
122529 int i;
122531 assert( iCol<pTab->nCol );
122532 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
122534 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
122536 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
122538 return pTab->nNVCol + i - n;
122552 ** is a read-only no-op.
122555 int iReg = ++pParse->nMem;
122573 ** The new table record is initialized and put in pParse->pNewTable.
122583 int isTemp, /* True if this is a TEMP table */
122584 int isView, /* True if this is a VIEW */
122585 int isVirtual, /* True if this is a VIRTUAL table */
122586 int noErr /* Do nothing if table already exists */
122590 sqlite3 *db = pParse->db;
122592 int iDb; /* Database number to create the table in */
122595 if( db->init.busy && db->init.newTnum==1 ){
122597 iDb = db->init.iDb;
122604 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
122616 pParse->sNameToken = *pName;
122621 if( db->init.iDb==1 ) isTemp = 1;
122632 char *zDb = db->aDb[iDb].zDbSName;
122636 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
122651 char *zDb = db->aDb[iDb].zDbSName;
122661 assert( !db->init.busy || CORRUPT_DB );
122675 assert( db->mallocFailed );
122676 pParse->rc = SQLITE_NOMEM_BKPT;
122677 pParse->nErr++;
122680 pTable->zName = zName;
122681 pTable->iPKey = -1;
122682 pTable->pSchema = db->aDb[iDb].pSchema;
122683 pTable->nTabRef = 1;
122685 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
122687 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
122689 assert( pParse->pNewTable==0 );
122690 pParse->pNewTable = pTable;
122700 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
122701 int addr1;
122702 int fileFormat;
122703 int reg1, reg2, reg3;
122717 reg1 = pParse->regRowid = ++pParse->nMem;
122718 reg2 = pParse->regRoot = ++pParse->nMem;
122719 reg3 = ++pParse->nMem;
122723 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
122729 /* This just creates a place-holder record in the sqlite_schema table.
122733 ** The rowid for the new entry is left in register pParse->regRowid.
122734 ** The root page number of the new table is left in reg pParse->regRoot.
122744 assert( !pParse->bReturning );
122745 pParse->u1.addrCrTab =
122756 /* Normal (non-error) return. */
122761 pParse->checkSchema = 1;
122771 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
122772 pCol->colFlags |= COLFLAG_HIDDEN;
122773 if( pTab ) pTab->tabFlags |= TF_HasHidden;
122774 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
122775 pTab->tabFlags |= TF_OOOHidden;
122786 pHash = &(db->aDb[1].pSchema->trigHash);
122787 sqlite3HashInsert(pHash, pRet->zName, 0);
122788 sqlite3ExprListDelete(db, pRet->pReturnEL);
122811 sqlite3 *db = pParse->db;
122812 if( pParse->pNewTrigger ){
122815 assert( pParse->bReturning==0 || pParse->ifNotExists );
122817 pParse->bReturning = 1;
122823 pParse->u1.pReturning = pRet;
122824 pRet->pParse = pParse;
122825 pRet->pReturnEL = pList;
122827 testcase( pParse->earlyCleanup );
122828 if( db->mallocFailed ) return;
122829 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
122831 pRet->retTrig.zName = pRet->zName;
122832 pRet->retTrig.op = TK_RETURNING;
122833 pRet->retTrig.tr_tm = TRIGGER_AFTER;
122834 pRet->retTrig.bReturning = 1;
122835 pRet->retTrig.pSchema = db->aDb[1].pSchema;
122836 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
122837 pRet->retTrig.step_list = &pRet->retTStep;
122838 pRet->retTStep.op = TK_RETURNING;
122839 pRet->retTStep.pTrig = &pRet->retTrig;
122840 pRet->retTStep.pExprList = pList;
122841 pHash = &(db->aDb[1].pSchema->trigHash);
122842 assert( sqlite3HashFind(pHash, pRet->zName)==0
122843 || pParse->nErr || pParse->ifNotExists );
122844 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
122845 ==&pRet->retTrig ){
122860 int i;
122864 sqlite3 *db = pParse->db;
122871 if( (p = pParse->pNewTable)==0 ) return;
122872 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
122873 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
122883 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
122885 sType.n -= 6;
122886 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
122888 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
122890 sType.n -= 9;
122891 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
122920 for(i=0; i<p->nCol; i++){
122921 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
122927 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
122932 p->aCol = aNew;
122933 pCol = &p->aCol[p->nCol];
122934 memset(pCol, 0, sizeof(p->aCol[0]));
122935 pCol->zCnName = z;
122936 pCol->hName = hName;
122942 pCol->affinity = affinity;
122943 pCol->eCType = eType;
122944 pCol->szEst = szEst;
122948 pCol->colFlags |= COLFLAG_SORTERREF;
122957 pCol->affinity = sqlite3AffinityType(zType, pCol);
122958 pCol->colFlags |= COLFLAG_HASTYPE;
122960 p->nCol++;
122961 p->nNVCol++;
122962 pParse->constraintName.n = 0;
122971 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
122974 p = pParse->pNewTable;
122975 if( p==0 || NEVER(p->nCol<1) ) return;
122976 pCol = &p->aCol[p->nCol-1];
122977 pCol->notNull = (u8)onError;
122978 p->tabFlags |= TF_HasNotNull;
122982 if( pCol->colFlags & COLFLAG_UNIQUE ){
122984 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
122985 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
122986 if( pIdx->aiColumn[0]==p->nCol-1 ){
122987 pIdx->uniqNotNull = 1;
122997 ** This routine does a case-independent search of zType for the
123005 ** --------------------------------
123006 ** 'INT' | SQLITE_AFF_INTEGER
123050 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
123059 int v = 0; /* default size is approx 4 bytes */
123064 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
123071 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
123076 pCol->colFlags |= COLFLAG_SORTERREF;
123081 pCol->szEst = v;
123104 sqlite3 *db = pParse->db;
123105 p = pParse->pNewTable;
123107 int isInit = db->init.busy && db->init.iDb!=1;
123108 pCol = &(p->aCol[p->nCol-1]);
123111 pCol->zCnName);
123113 }else if( pCol->colFlags & COLFLAG_GENERATED ){
123114 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123115 testcase( pCol->colFlags & COLFLAG_STORED );
123156 if( p->op==TK_STRING ){
123157 p->op = TK_ID;
123158 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
123159 p->pLeft->op = TK_ID;
123167 pCol->colFlags |= COLFLAG_PRIMKEY;
123169 if( pCol->colFlags & COLFLAG_GENERATED ){
123170 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123171 testcase( pCol->colFlags & COLFLAG_STORED );
123190 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
123199 int onError, /* What to do with a uniqueness conflict */
123200 int autoInc, /* True if the AUTOINCREMENT keyword is present */
123201 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
123203 Table *pTab = pParse->pNewTable;
123205 int iCol = -1, i;
123206 int nTerm;
123208 if( pTab->tabFlags & TF_HasPrimaryKey ){
123210 "table \"%s\" has more than one primary key", pTab->zName);
123213 pTab->tabFlags |= TF_HasPrimaryKey;
123215 iCol = pTab->nCol - 1;
123216 pCol = &pTab->aCol[iCol];
123220 nTerm = pList->nExpr;
123222 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
123225 if( pCExpr->op==TK_ID ){
123228 zCName = pCExpr->u.zToken;
123229 for(iCol=0; iCol<pTab->nCol; iCol++){
123230 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
123231 pCol = &pTab->aCol[iCol];
123241 && pCol->eCType==COLTYPE_INTEGER
123245 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
123246 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
123248 pTab->iPKey = iCol;
123249 pTab->keyConf = (u8)onError;
123251 pTab->tabFlags |= autoInc*TF_Autoincrement;
123252 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
123266 sqlite3ExprListDelete(pParse->db, pList);
123280 Table *pTab = pParse->pNewTable;
123281 sqlite3 *db = pParse->db;
123283 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
123285 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
123286 if( pParse->constraintName.n ){
123287 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
123291 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
123293 t.n = (int)(zEnd - t.z);
123294 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
123299 sqlite3ExprDelete(pParse->db, pCheckExpr);
123309 int i;
123313 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
123314 i = p->nCol-1;
123315 db = pParse->db;
123321 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
123327 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
123328 assert( pIdx->nKeyCol==1 );
123329 if( pIdx->aiColumn[0]==i ){
123330 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
123343 Table *pTab = pParse->pNewTable;
123349 pCol = &(pTab->aCol[pTab->nCol-1]);
123354 if( pCol->iDflt>0 ) goto generated_error;
123356 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
123357 /* no-op */
123358 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
123364 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
123365 pCol->colFlags |= eType;
123368 pTab->tabFlags |= eType;
123369 if( pCol->colFlags & COLFLAG_PRIMKEY ){
123372 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
123379 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
123386 pCol->zCnName);
123388 sqlite3ExprDelete(pParse->db, pExpr);
123391 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
123393 sqlite3ExprDelete(pParse->db, pExpr);
123407 ** This plan is not completely bullet-proof. It is possible for
123413 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
123414 ** the schema-version whenever the schema changes.
123416 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
123417 sqlite3 *db = pParse->db;
123418 Vdbe *v = pParse->pVdbe;
123421 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
123432 static int identLength(const char *z){
123433 int n;
123444 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
123451 ** it is quoted using double-quotes.
123453 static void identPut(char *z, int *pIdx, char *zSignedIdent){
123455 int i, j, needQuote;
123482 int i, k, n;
123487 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
123488 n += identLength(pCol->zCnName) + 5;
123490 n += identLength(p->zName);
123500 n += 35 + 6*p->nCol;
123508 identPut(zStmt, &k, p->zName);
123510 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
123515 /* SQLITE_AFF_INTEGER */ " INT",
123519 int len;
123522 sqlite3_snprintf(n-k, &zStmt[k], zSep);
123525 identPut(zStmt, &k, pCol->zCnName);
123526 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
123527 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
123528 testcase( pCol->affinity==SQLITE_AFF_BLOB );
123529 testcase( pCol->affinity==SQLITE_AFF_TEXT );
123530 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
123531 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
123532 testcase( pCol->affinity==SQLITE_AFF_REAL );
123533 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
123535 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
123537 assert( pCol->affinity==SQLITE_AFF_BLOB
123538 || pCol->affinity==SQLITE_AFF_FLEXNUM
123539 || pCol->affinity==sqlite3AffinityType(zType, 0) );
123544 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
123552 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
123554 int nByte;
123555 if( pIdx->nColumn>=N ) return SQLITE_OK;
123556 assert( pIdx->isResized==0 );
123560 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
123561 pIdx->azColl = (const char**)zExtra;
123563 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
123564 pIdx->aiRowLogEst = (LogEst*)zExtra;
123566 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
123567 pIdx->aiColumn = (i16*)zExtra;
123569 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
123570 pIdx->aSortOrder = (u8*)zExtra;
123571 pIdx->nColumn = N;
123572 pIdx->isResized = 1;
123582 int i;
123583 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
123584 wTable += pTabCol->szEst;
123586 if( pTab->iPKey<0 ) wTable++;
123587 pTab->szTabRow = sqlite3LogEst(wTable*4);
123595 int i;
123596 const Column *aCol = pIdx->pTable->aCol;
123597 for(i=0; i<pIdx->nColumn; i++){
123598 i16 x = pIdx->aiColumn[i];
123599 assert( x<pIdx->pTable->nCol );
123602 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
123609 static int hasColumn(const i16 *aiCol, int nCol, int x){
123610 while( nCol-- > 0 ){
123620 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
123631 static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
123632 int i, j;
123633 assert( nKey<=pIdx->nColumn );
123634 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
123635 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
123636 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
123637 assert( pPk->pTable==pIdx->pTable );
123639 j = pPk->aiColumn[iCol];
123642 assert( pIdx->aiColumn[i]>=0 || j>=0 );
123643 if( pIdx->aiColumn[i]==j
123644 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
123657 ** high-order bit of colNotIdxed is always 1. All unindexed columns
123660 ** 2019-10-24: For the purpose of this computation, virtual columns are
123668 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
123673 int j;
123674 Table *pTab = pIdx->pTable;
123675 for(j=pIdx->nColumn-1; j>=0; j--){
123676 int x = pIdx->aiColumn[j];
123677 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
123678 testcase( x==BMS-1 );
123679 testcase( x==BMS-2 );
123680 if( x<BMS-1 ) m |= MASKBIT(x);
123683 pIdx->colNotIdxed = ~m;
123684 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
123714 int nPk;
123715 int nExtra;
123716 int i, j;
123717 sqlite3 *db = pParse->db;
123718 Vdbe *v = pParse->pVdbe;
123722 if( !db->init.imposterTable ){
123723 for(i=0; i<pTab->nCol; i++){
123724 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
123725 && (pTab->aCol[i].notNull==OE_None)
123727 pTab->aCol[i].notNull = OE_Abort;
123730 pTab->tabFlags |= TF_HasNotNull;
123736 assert( !pParse->bReturning );
123737 if( pParse->u1.addrCrTab ){
123739 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
123745 if( pTab->iPKey>=0 ){
123748 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
123752 pTab->tabFlags &= ~TF_WithoutRowid;
123756 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
123758 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
123759 assert( pParse->pNewTable==pTab );
123760 pTab->iPKey = -1;
123761 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
123763 if( pParse->nErr ){
123764 pTab->tabFlags &= ~TF_WithoutRowid;
123767 assert( db->mallocFailed==0 );
123769 assert( pPk->nKeyCol==1 );
123779 for(i=j=1; i<pPk->nKeyCol; i++){
123781 pPk->nColumn--;
123783 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
123784 pPk->azColl[j] = pPk->azColl[i];
123785 pPk->aSortOrder[j] = pPk->aSortOrder[i];
123786 pPk->aiColumn[j++] = pPk->aiColumn[i];
123789 pPk->nKeyCol = j;
123792 pPk->isCovering = 1;
123793 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
123794 nPk = pPk->nColumn = pPk->nKeyCol;
123800 if( v && pPk->tnum>0 ){
123801 assert( db->init.busy==0 );
123802 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
123806 pPk->tnum = pTab->tnum;
123808 /* Update the in-memory representation of all UNIQUE indices by converting
123811 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123812 int n;
123815 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
123816 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
123822 pIdx->nColumn = pIdx->nKeyCol;
123825 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
123826 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
123827 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
123828 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
123829 pIdx->aiColumn[j] = pPk->aiColumn[i];
123830 pIdx->azColl[j] = pPk->azColl[i];
123831 if( pPk->aSortOrder[i] ){
123833 pIdx->bAscKeyBug = 1;
123838 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
123839 assert( pIdx->nColumn>=j );
123845 for(i=0; i<pTab->nCol; i++){
123846 if( !hasColumn(pPk->aiColumn, nPk, i)
123847 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
123850 for(i=0, j=nPk; i<pTab->nCol; i++){
123851 if( !hasColumn(pPk->aiColumn, j, i)
123852 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
123854 assert( j<pPk->nColumn );
123855 pPk->aiColumn[j] = i;
123856 pPk->azColl[j] = sqlite3StrBINARY;
123860 assert( pPk->nColumn==j );
123861 assert( pTab->nNVCol<=j );
123871 SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
123872 int nName; /* Length of zName */
123876 nName = sqlite3Strlen30(pTab->zName);
123877 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
123879 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
123881 if( pMod->pModule->iVersion<3 ) return 0;
123882 if( pMod->pModule->xShadowName==0 ) return 0;
123883 return pMod->pModule->xShadowName(zName+nName+1);
123895 int nName; /* Length of pTab->zName */
123900 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
123902 if( NEVER(pMod->pModule==0) ) return;
123903 if( pMod->pModule->iVersion<3 ) return;
123904 if( pMod->pModule->xShadowName==0 ) return;
123905 assert( pTab->zName!=0 );
123906 nName = sqlite3Strlen30(pTab->zName);
123907 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
123909 assert( pOther->zName!=0 );
123911 if( pOther->tabFlags & TF_Shadow ) continue;
123912 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
123913 && pOther->zName[nName]=='_'
123914 && pMod->pModule->xShadowName(pOther->zName+nName+1)
123916 pOther->tabFlags |= TF_Shadow;
123930 SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
123952 static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
123968 #define markExprListImmutable(X) /* no-op */
123981 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
124000 sqlite3 *db = pParse->db; /* The database connection */
124001 int iDb; /* Database in which the table lives */
124007 p = pParse->pNewTable;
124010 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
124011 p->tabFlags |= TF_Shadow;
124014 /* If the db->init.busy is 1 it means we are reading the SQL off the
124017 ** for the table from the db->init.newTnum field. (The page number
124021 ** table itself. So mark it read-only.
124023 if( db->init.busy ){
124024 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
124028 p->tnum = db->init.newTnum;
124029 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
124035 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
124042 int ii;
124043 p->tabFlags |= TF_Strict;
124044 for(ii=0; ii<p->nCol; ii++){
124045 Column *pCol = &p->aCol[ii];
124046 if( pCol->eCType==COLTYPE_CUSTOM ){
124047 if( pCol->colFlags & COLFLAG_HASTYPE ){
124050 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
124054 p->zName, pCol->zCnName);
124057 }else if( pCol->eCType==COLTYPE_ANY ){
124058 pCol->affinity = SQLITE_AFF_BLOB;
124060 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
124061 && p->iPKey!=ii
124062 && pCol->notNull == OE_None
124064 pCol->notNull = OE_Abort;
124065 p->tabFlags |= TF_HasNotNull;
124070 assert( (p->tabFlags & TF_HasPrimaryKey)==0
124071 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
124072 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
124073 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
124077 if( (p->tabFlags & TF_Autoincrement) ){
124082 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
124083 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
124086 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
124089 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124094 if( p->pCheck ){
124095 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
124096 if( pParse->nErr ){
124099 sqlite3ExprListDelete(db, p->pCheck);
124100 p->pCheck = 0;
124102 markExprListImmutable(p->pCheck);
124107 if( p->tabFlags & TF_HasGenerated ){
124108 int ii, nNG = 0;
124109 testcase( p->tabFlags & TF_HasVirtual );
124110 testcase( p->tabFlags & TF_HasStored );
124111 for(ii=0; ii<p->nCol; ii++){
124112 u32 colFlags = p->aCol[ii].colFlags;
124114 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
124124 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
124132 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
124140 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124150 if( !db->init.busy ){
124151 int n;
124178 ** statement to populate the new table. The root-page number for the
124179 ** new table is in register pParse->regRoot.
124185 ** A shared-cache write-lock is not required to write to the new table,
124186 ** as a schema-lock must have already been obtained to create it. Since
124187 ** a schema-lock excludes all other database users, the write-lock would
124192 int regYield; /* Register holding co-routine entry-point */
124193 int addrTop; /* Top of the co-routine */
124194 int regRec; /* A record to be insert into the new table */
124195 int regRowid; /* Rowid of the next row to insert */
124196 int addrInsLoop; /* Top of the loop for inserting rows */
124198 int iCsr; /* Write cursor on the new table */
124201 pParse->rc = SQLITE_ERROR;
124202 pParse->nErr++;
124205 iCsr = pParse->nTab++;
124206 regYield = ++pParse->nMem;
124207 regRec = ++pParse->nMem;
124208 regRowid = ++pParse->nMem;
124210 sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->regRoot, iDb);
124214 if( pParse->nErr ) return;
124217 assert( p->aCol==0 );
124218 p->nCol = p->nNVCol = pSelTab->nCol;
124219 p->aCol = pSelTab->aCol;
124220 pSelTab->nCol = 0;
124221 pSelTab->aCol = 0;
124225 if( pParse->nErr ) return;
124227 sqlite3VdbeJumpHere(v, addrTop - 1);
124243 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
124244 n = (int)(pEnd2->z - pParse->sNameToken.z);
124245 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
124247 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
124259 db->aDb[iDb].zDbSName,
124261 p->zName,
124262 p->zName,
124263 pParse->regRoot,
124265 pParse->regRowid
124274 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
124275 Db *pDb = &db->aDb[iDb];
124277 if( pDb->pSchema->pSeqTab==0 ){
124280 pDb->zDbSName
124288 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
124292 if( p->tabFlags & TF_HasGenerated ){
124295 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124299 /* Add the table to the in-memory representation of the database.
124301 if( db->init.busy ){
124303 Schema *pSchema = p->pSchema;
124305 assert( HasRowid(p) || p->iPKey<0 );
124306 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
124312 pParse->pNewTable = 0;
124313 db->mDbFlags |= DBFLAG_SchemaChange;
124318 assert( !pParse->nested );
124320 if( strcmp(p->zName, "sqlite_sequence")==0 ){
124322 p->pSchema->pSeqTab = p;
124330 if( pCons->z==0 ){
124333 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
124349 int isTemp, /* TRUE for a TEMPORARY view */
124350 int noErr /* Suppress error messages if VIEW already exists */
124353 int n;
124358 int iDb;
124359 sqlite3 *db = pParse->db;
124361 if( pParse->nVar>0 ){
124366 p = pParse->pNewTable;
124367 if( p==0 || pParse->nErr ) goto create_view_fail;
124372 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
124376 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
124378 p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */
124382 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124388 ** allocated rather than point to the input string - which means that
124391 pSelect->selFlags |= SF_View;
124393 p->u.view.pSelect = pSelect;
124396 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
124398 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
124399 p->eTabType = TABTYP_VIEW;
124400 if( db->mallocFailed ) goto create_view_fail;
124405 sEnd = pParse->sLastToken;
124411 n = (int)(sEnd.z - pBegin->z);
124413 z = pBegin->z;
124414 while( sqlite3Isspace(z[n-1]) ){ n--; }
124415 sEnd.z = &z[n-1];
124434 ** the columns of the view in the pTable structure. Return non-zero if
124436 ** in pParse->zErrMsg.
124438 static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
124441 int nErr = 0; /* Number of errors encountered */
124442 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
124444 int rc;
124454 db->nSchemaLock++;
124456 db->nSchemaLock--;
124466 assert( pTable->nCol<=0 );
124483 if( pTable->nCol<0 ){
124484 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
124487 assert( pTable->nCol>=0 );
124497 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
124499 u8 eParseMode = pParse->eParseMode;
124500 int nTab = pParse->nTab;
124501 int nSelect = pParse->nSelect;
124502 pParse->eParseMode = PARSE_MODE_NORMAL;
124503 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
124504 pTable->nCol = -1;
124507 xAuth = db->xAuth;
124508 db->xAuth = 0;
124510 db->xAuth = xAuth;
124514 pParse->nTab = nTab;
124515 pParse->nSelect = nSelect;
124517 pTable->nCol = 0;
124519 }else if( pTable->pCheck ){
124522 ** arglist which is stored in pTable->pCheck. The pCheck field
124526 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
124527 &pTable->nCol, &pTable->aCol);
124528 if( pParse->nErr==0
124529 && pTable->nCol==pSel->pEList->nExpr
124531 assert( db->mallocFailed==0 );
124538 assert( pTable->aCol==0 );
124539 pTable->nCol = pSelTab->nCol;
124540 pTable->aCol = pSelTab->aCol;
124541 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
124542 pSelTab->nCol = 0;
124543 pSelTab->aCol = 0;
124544 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
124546 pTable->nNVCol = pTable->nCol;
124550 pParse->eParseMode = eParseMode;
124554 pTable->pSchema->schemaFlags |= DB_UnresetViews;
124555 if( db->mallocFailed ){
124559 return nErr + pParse->nErr;
124561 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
124563 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
124572 static void sqliteViewResetAll(sqlite3 *db, int idx){
124576 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
124591 ** root-page of a table or index in database iDb has changed from iFrom
124606 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
124612 pDb = &db->aDb[iDb];
124613 pHash = &pDb->pSchema->tblHash;
124616 if( pTab->tnum==iFrom ){
124617 pTab->tnum = iTo;
124620 pHash = &pDb->pSchema->idxHash;
124623 if( pIdx->tnum==iFrom ){
124624 pIdx->tnum = iTo;
124631 ** Write code to erase the table with root-page iTable from database iDb.
124633 ** if a root-page of another table is moved by the btree-layer whilst
124634 ** erasing iTable (this can happen with an auto-vacuum database).
124636 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
124638 int r1 = sqlite3GetTempReg(pParse);
124644 ** is non-zero, then it is the root page number of a table moved to
124655 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
124663 ** in case a root-page belonging to another table is moved by the btree layer
124664 ** is also added (this can happen with an auto-vacuum database).
124667 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
124669 ** table and index root-pages in order, starting with the numerically
124670 ** largest root-page number. This guarantees that none of the root-pages
124678 ** and root page 5 happened to be the largest root-page number in the
124681 ** a free-list page.
124683 Pgno iTab = pTab->tnum;
124693 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124694 Pgno iIdx = pIdx->tnum;
124695 assert( pIdx->pSchema==pTab->pSchema );
124703 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124704 assert( iDb>=0 && iDb<pParse->db->nDb );
124717 int iDb, /* The database number */
124721 int i;
124722 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
124726 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
124738 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
124740 sqlite3 *db = pParse->db;
124742 Db *pDb = &db->aDb[iDb];
124760 assert( pTrigger->pSchema==pTab->pSchema ||
124761 pTrigger->pSchema==db->aDb[1].pSchema );
124763 pTrigger = pTrigger->pNext;
124770 ** move as a result of the drop (can happen in auto-vacuum mode).
124772 if( pTab->tabFlags & TF_Autoincrement ){
124775 pDb->zDbSName, pTab->zName
124790 pDb->zDbSName, pTab->zName);
124799 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
124802 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
124808 ** Return TRUE if shadow tables should be read-only in the current
124811 SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db){
124813 if( (db->flags & SQLITE_Defensive)!=0
124814 && db->pVtabCtx==0
124815 && db->nVdbeExec==0
124827 static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
124828 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
124829 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
124830 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
124833 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
124836 if( pTab->tabFlags & TF_Eponymous ){
124846 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
124849 sqlite3 *db = pParse->db;
124850 int iDb;
124852 if( db->mallocFailed ){
124855 assert( pParse->nErr==0 );
124856 assert( pName->nSrc==1 );
124858 if( noErr ) db->suppressErr++;
124860 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
124861 if( noErr ) db->suppressErr--;
124865 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
124870 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124871 assert( iDb>=0 && iDb<db->nDb );
124881 int code;
124883 const char *zDb = db->aDb[iDb].zDbSName;
124897 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
124906 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
124909 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
124915 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
124924 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
124928 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
124940 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
124961 ** under construction in the pParse->pNewTable field.
124971 int flags /* Conflict resolution algorithms. */
124973 sqlite3 *db = pParse->db;
124977 Table *p = pParse->pNewTable;
124979 int i;
124980 int nCol;
124986 int iCol = p->nCol-1;
124988 if( pToCol && pToCol->nExpr!=1 ){
124991 p->aCol[iCol].zCnName, pTo);
124995 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
125001 nCol = pFromCol->nExpr;
125003 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
125005 for(i=0; i<pToCol->nExpr; i++){
125006 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
125013 pFKey->pFrom = p;
125015 pFKey->pNextFrom = p->u.tab.pFKey;
125016 z = (char*)&pFKey->aCol[nCol];
125017 pFKey->zTo = z;
125021 memcpy(z, pTo->z, pTo->n);
125022 z[pTo->n] = 0;
125024 z += pTo->n+1;
125025 pFKey->nCol = nCol;
125027 pFKey->aCol[0].iFrom = p->nCol-1;
125030 int j;
125031 for(j=0; j<p->nCol; j++){
125032 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
125033 pFKey->aCol[i].iFrom = j;
125037 if( j>=p->nCol ){
125040 pFromCol->a[i].zEName);
125044 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
125050 int n = sqlite3Strlen30(pToCol->a[i].zEName);
125051 pFKey->aCol[i].zCol = z;
125053 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
125055 memcpy(z, pToCol->a[i].zEName, n);
125060 pFKey->isDeferred = 0;
125061 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
125062 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
125064 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
125065 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
125066 pFKey->zTo, (void *)pFKey
125073 assert( pNextTo->pPrevTo==0 );
125074 pFKey->pNextTo = pNextTo;
125075 pNextTo->pPrevTo = pFKey;
125081 p->u.tab.pFKey = pFKey;
125098 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
125102 if( (pTab = pParse->pNewTable)==0 ) return;
125104 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
125105 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
125106 pFKey->isDeferred = (u8)isDeferred;
125119 ** the root page number of the index is taken from pIndex->tnum.
125121 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
125122 Table *pTab = pIndex->pTable; /* The table that is indexed */
125123 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
125124 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
125125 int iSorter; /* Cursor opened by OpenSorter (if in use) */
125126 int addr1; /* Address of top of loop */
125127 int addr2; /* Address to jump to for next iteration */
125129 int iPartIdxLabel; /* Jump to this label to skip a row */
125132 int regRecord; /* Register holding assembled index record */
125133 sqlite3 *db = pParse->db; /* The database connection */
125134 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
125137 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
125138 db->aDb[iDb].zDbSName ) ){
125143 /* Require a write-lock on the table to perform this operation */
125144 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
125151 tnum = pIndex->tnum;
125154 assert( pKey!=0 || pParse->nErr );
125157 iSorter = pParse->nTab++;
125158 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
125174 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb,
125180 int j2 = sqlite3VdbeGoto(v, 1);
125184 pIndex->nKeyCol); VdbeCoverage(v);
125199 if( !pIndex->bAscKeyBug ){
125224 ** of 8-byte aligned space after the Index object and return a
125230 int nExtra, /* Number of bytes of extra space to alloc */
125234 int nByte; /* Bytes of space for Index object + arrays */
125244 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
125245 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
125246 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
125247 p->aSortOrder = (u8*)pExtra;
125248 p->nColumn = nCol;
125249 p->nKeyCol = nCol - 1;
125258 ** pParse and return non-zero. Otherwise, return zero.
125260 SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
125262 int i;
125263 for(i=0; i<pList->nExpr; i++){
125264 if( pList->a[i].fg.bNulls ){
125265 u8 sf = pList->a[i].fg.sortFlags;
125280 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
125281 ** as the table to be indexed. pParse->pNewTable is a table that is
125285 ** is a primary key or unique-constraint on the most recent column added
125292 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
125294 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
125297 int sortOrder, /* Sort order of primary key when pList==NULL */
125298 int ifNotExist, /* Omit error if index already exists */
125304 int nName; /* Number of characters in zName */
125305 int i, j;
125307 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
125308 sqlite3 *db = pParse->db;
125310 int iDb; /* Index of the database that is being written */
125313 int nExtra = 0; /* Space allocated for zExtra[] */
125314 int nExtraCol; /* Number of extra columns needed */
125318 assert( db->pParse==pParse );
125319 if( pParse->nErr ){
125322 assert( db->mallocFailed==0 );
125338 /* Use the two-part index name to determine the database
125345 assert( pName && pName->z );
125352 if( !db->init.busy ){
125354 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
125366 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
125367 assert( db->mallocFailed==0 || pTab==0 );
125369 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
125371 "cannot create a TEMP index on non-TEMP table \"%s\"",
125372 pTab->zName);
125379 pTab = pParse->pNewTable;
125381 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125383 pDb = &db->aDb[iDb];
125386 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
125387 && db->init.busy==0
125390 && sqlite3UserAuthTable(pTab->zName)==0
125393 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
125425 assert( pName->z!=0 );
125426 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
125430 if( !db->init.busy ){
125431 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
125436 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
125440 assert( !db->init.busy );
125448 int n;
125450 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
125451 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
125468 const char *zDb = pDb->zDbSName;
125474 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
125486 Column *pCol = &pTab->aCol[pTab->nCol-1];
125487 pCol->colFlags |= COLFLAG_UNIQUE;
125488 sqlite3TokenInit(&prevCol, pCol->zCnName);
125492 assert( pList->nExpr==1 );
125496 if( pParse->nErr ) goto exit_create_index;
125502 for(i=0; i<pList->nExpr; i++){
125503 Expr *pExpr = pList->a[i].pExpr;
125505 if( pExpr->op==TK_COLLATE ){
125507 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
125515 nExtraCol = pPk ? pPk->nKeyCol : 1;
125516 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
125517 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
125519 if( db->mallocFailed ){
125522 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
125523 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
125524 pIndex->zName = zExtra;
125526 memcpy(pIndex->zName, zName, nName+1);
125527 pIndex->pTable = pTab;
125528 pIndex->onError = (u8)onError;
125529 pIndex->uniqNotNull = onError!=OE_None;
125530 pIndex->idxType = idxType;
125531 pIndex->pSchema = db->aDb[iDb].pSchema;
125532 pIndex->nKeyCol = pList->nExpr;
125535 pIndex->pPartIdxWhere = pPIWhere;
125542 if( pDb->pSchema->file_format>=4 ){
125543 sortOrderMask = -1; /* Honor DESC */
125551 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
125557 pListItem = pList->a;
125559 pIndex->aColExpr = pList;
125562 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
125563 Expr *pCExpr; /* The i-th index expression */
125564 int requestedSortOrder; /* ASC or DESC on the i-th expression */
125567 sqlite3StringToId(pListItem->pExpr);
125568 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
125569 if( pParse->nErr ) goto exit_create_index;
125570 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
125571 if( pCExpr->op!=TK_COLUMN ){
125572 if( pTab==pParse->pNewTable ){
125577 if( pIndex->aColExpr==0 ){
125578 pIndex->aColExpr = pList;
125582 pIndex->aiColumn[i] = XN_EXPR;
125583 pIndex->uniqNotNull = 0;
125584 pIndex->bHasExpr = 1;
125586 j = pCExpr->iColumn;
125589 j = pTab->iPKey;
125591 if( pTab->aCol[j].notNull==0 ){
125592 pIndex->uniqNotNull = 0;
125594 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
125595 pIndex->bHasVCol = 1;
125596 pIndex->bHasExpr = 1;
125599 pIndex->aiColumn[i] = (i16)j;
125602 if( pListItem->pExpr->op==TK_COLLATE ){
125603 int nColl;
125604 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
125605 zColl = pListItem->pExpr->u.zToken;
125611 nExtra -= nColl;
125613 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
125616 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
125619 pIndex->azColl[i] = zColl;
125620 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
125621 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
125629 for(j=0; j<pPk->nKeyCol; j++){
125630 int x = pPk->aiColumn[j];
125632 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
125633 pIndex->nColumn--;
125635 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
125636 pIndex->aiColumn[i] = x;
125637 pIndex->azColl[i] = pPk->azColl[j];
125638 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
125642 assert( i==pIndex->nColumn );
125644 pIndex->aiColumn[i] = XN_ROWID;
125645 pIndex->azColl[i] = sqlite3StrBINARY;
125648 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
125653 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
125655 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
125656 pIndex->isCovering = 1;
125657 for(j=0; j<pTab->nCol; j++){
125658 if( j==pTab->iPKey ) continue;
125660 pIndex->isCovering = 0;
125665 if( pTab==pParse->pNewTable ){
125688 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
125689 int k;
125691 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
125694 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
125695 for(k=0; k<pIdx->nKeyCol; k++){
125698 assert( pIdx->aiColumn[k]>=0 );
125699 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
125700 z1 = pIdx->azColl[k];
125701 z2 = pIndex->azColl[k];
125704 if( k==pIdx->nKeyCol ){
125705 if( pIdx->onError!=pIndex->onError ){
125713 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
125717 if( pIdx->onError==OE_Default ){
125718 pIdx->onError = pIndex->onError;
125721 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
125723 pIndex->pNext = pParse->pNewIndex;
125724 pParse->pNewIndex = pIndex;
125735 ** in-memory database structures.
125737 assert( pParse->nErr==0 );
125738 if( db->init.busy ){
125741 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
125743 pIndex->tnum = db->init.newTnum;
125746 pParse->rc = SQLITE_CORRUPT_BKPT;
125750 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
125751 pIndex->zName, pIndex);
125757 db->mDbFlags |= DBFLAG_SchemaChange;
125776 int iMem = ++pParse->nMem;
125789 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
125797 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
125798 if( pName->z[n-1]==';' ) n--;
125801 onError==OE_None ? "" : " UNIQUE", n, pName->z);
125812 db->aDb[iDb].zDbSName,
125813 pIndex->zName,
125814 pTab->zName,
125821 ** to invalidate all pre-compiled statements.
125827 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
125831 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
125834 if( db->init.busy || pTblName==0 ){
125835 pIndex->pNext = pTab->pIndex;
125836 pTab->pIndex = pIndex;
125840 assert( pParse->pNewIndex==0 );
125841 pParse->pNewIndex = pIndex;
125855 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
125857 if( pThis->onError!=OE_Replace ) continue;
125858 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
125860 pThis->pNext = pNext->pNext;
125861 pNext->pNext = pThis;
125862 ppFrom = &pNext->pNext;
125870 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
125871 assert( pThis->onError!=OE_Replace
125872 || pThis->pNext==0
125873 || pThis->pNext->onError==OE_Replace );
125884 ** Fill the Index.aiRowEst[] array with default information - information
125894 ** aiRowEst[N]<=aiRowEst[N-1]
125904 LogEst *a = pIdx->aiRowLogEst;
125906 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
125907 int i;
125910 assert( !pIdx->hasStat1 );
125916 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
125922 x = pIdx->pTable->nRowLogEst;
125925 pIdx->pTable->nRowLogEst = x = 99;
125927 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
125933 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
125938 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
125945 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
125948 sqlite3 *db = pParse->db;
125949 int iDb;
125951 if( db->mallocFailed ){
125954 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
125955 assert( pName->nSrc==1 );
125959 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
125962 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
125964 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
125967 pParse->checkSchema = 1;
125970 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
125975 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
125978 int code = SQLITE_DROP_INDEX;
125979 Table *pTab = pIndex->pTable;
125980 const char *zDb = db->aDb[iDb].zDbSName;
125986 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
125998 db->aDb[iDb].zDbSName, pIndex->zName
126000 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
126002 destroyRootPage(pParse, pIndex->tnum, iDb);
126003 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
126016 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
126024 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
126030 int szEntry, /* Size of each object in the array */
126031 int *pnEntry, /* Number of objects currently in use */
126032 int *pIdx /* Write the index of a new slot here */
126036 if( (n & (n-1))==0 ){
126040 *pIdx = -1;
126058 sqlite3 *db = pParse->db;
126059 int i;
126066 sizeof(IdList) + pList->nId*sizeof(pList->a));
126073 i = pList->nId++;
126074 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
126075 if( IN_RENAME_OBJECT && pList->a[i].zName ){
126076 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
126085 int i;
126088 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
126089 for(i=0; i<pList->nId; i++){
126090 sqlite3DbFree(db, pList->a[i].zName);
126096 ** Return the index in pList of the identifier named zId. Return -1
126099 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
126100 int i;
126102 for(i=0; i<pList->nId; i++){
126103 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
126105 return -1;
126114 ** appropriate for small and memory-limited applications.
126143 int nExtra, /* Number of new slots to add to pSrc->a[] */
126144 int iStart /* Index in pSrc->a[] of first new slot */
126146 int i;
126152 assert( iStart<=pSrc->nSrc );
126155 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
126157 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
126158 sqlite3 *db = pParse->db;
126160 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
126167 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
126169 assert( db->mallocFailed );
126173 pSrc->nAlloc = nAlloc;
126178 for(i=pSrc->nSrc-1; i>=iStart; i--){
126179 pSrc->a[i+nExtra] = pSrc->a[i];
126181 pSrc->nSrc += nExtra;
126184 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
126186 pSrc->a[i].iCursor = -1;
126239 assert( pParse->db!=0 );
126240 db = pParse->db;
126242 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
126244 pList->nAlloc = 1;
126245 pList->nSrc = 1;
126246 memset(&pList->a[0], 0, sizeof(pList->a[0]));
126247 pList->a[0].iCursor = -1;
126249 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
126257 pItem = &pList->a[pList->nSrc-1];
126258 if( pDatabase && pDatabase->z==0 ){
126262 pItem->zName = sqlite3NameFromToken(db, pDatabase);
126263 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
126265 pItem->zName = sqlite3NameFromToken(db, pTable);
126266 pItem->zDatabase = 0;
126275 int i;
126277 assert( pList || pParse->db->mallocFailed );
126279 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
126280 if( pItem->iCursor>=0 ) continue;
126281 pItem->iCursor = pParse->nTab++;
126282 if( pItem->pSelect ){
126283 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
126293 int i;
126297 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
126298 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
126299 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
126300 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
126301 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
126302 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
126303 sqlite3DeleteTable(db, pItem->pTab);
126304 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
126305 if( pItem->fg.isUsing ){
126306 sqlite3IdListDelete(db, pItem->u3.pUsing);
126307 }else if( pItem->u3.pOn ){
126308 sqlite3ExprDelete(db, pItem->u3.pOn);
126320 ** pDatabase is NULL if the database name qualifier is missing - the
126335 Token *pAlias, /* The right-hand side of the AS subexpression */
126340 sqlite3 *db = pParse->db;
126341 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
126343 (pOnUsing->pOn ? "ON" : "USING")
126351 assert( p->nSrc>0 );
126352 pItem = &p->a[p->nSrc-1];
126354 assert( pItem->zName==0 || pDatabase!=0 );
126355 if( IN_RENAME_OBJECT && pItem->zName ){
126356 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
126357 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
126360 if( pAlias->n ){
126361 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
126364 pItem->pSelect = pSubquery;
126365 if( pSubquery->selFlags & SF_NestedFrom ){
126366 pItem->fg.isNestedFrom = 1;
126369 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
126370 assert( pItem->fg.isUsing==0 );
126372 pItem->u3.pOn = 0;
126373 }else if( pOnUsing->pUsing ){
126374 pItem->fg.isUsing = 1;
126375 pItem->u3.pUsing = pOnUsing->pUsing;
126377 pItem->u3.pOn = pOnUsing->pOn;
126390 ** element of the source-list passed as the second argument.
126394 if( p && pIndexedBy->n>0 ){
126396 assert( p->nSrc>0 );
126397 pItem = &p->a[p->nSrc-1];
126398 assert( pItem->fg.notIndexed==0 );
126399 assert( pItem->fg.isIndexedBy==0 );
126400 assert( pItem->fg.isTabFunc==0 );
126401 if( pIndexedBy->n==1 && !pIndexedBy->z ){
126404 pItem->fg.notIndexed = 1;
126406 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
126407 pItem->fg.isIndexedBy = 1;
126408 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
126419 assert( p1 && p1->nSrc==1 );
126421 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
126423 sqlite3SrcListDelete(pParse->db, p2);
126426 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
126427 sqlite3DbFree(pParse->db, p2);
126428 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
126436 ** table-valued-function.
126440 SrcItem *pItem = &p->a[p->nSrc-1];
126441 assert( pItem->fg.notIndexed==0 );
126442 assert( pItem->fg.isIndexedBy==0 );
126443 assert( pItem->fg.isTabFunc==0 );
126444 pItem->u1.pFuncArg = pList;
126445 pItem->fg.isTabFunc = 1;
126447 sqlite3ExprListDelete(pParse->db, pList);
126463 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
126468 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
126475 if( p && p->nSrc>1 ){
126476 int i = p->nSrc-1;
126479 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
126480 }while( (--i)>0 );
126481 p->a[0].fg.jointype = 0;
126486 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
126487 i--;
126490 p->a[i].fg.jointype |= JT_LTORJ;
126491 }while( (--i)>=0 );
126499 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
126502 int i;
126505 db = pParse->db;
126513 for(i=0; i<db->nDb; i++){
126514 int eTxnType;
126515 Btree *pBt = db->aDb[i].pBt;
126535 SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
126537 int isRollback;
126540 assert( pParse->db!=0 );
126557 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
126558 char *zName = sqlite3NameFromToken(pParse->db, pName);
126566 sqlite3DbFree(pParse->db, zName);
126577 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
126578 sqlite3 *db = pParse->db;
126579 if( db->aDb[1].pBt==0 && !pParse->explain ){
126580 int rc;
126582 static const int flags =
126589 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
126593 pParse->rc = rc;
126596 db->aDb[1].pBt = pBt;
126597 assert( db->aDb[1].pSchema );
126598 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
126609 ** will occur at the end of the top-level VDBE and will be generated
126612 static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
126613 assert( iDb>=0 && iDb<pToplevel->db->nDb );
126614 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
126616 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
126617 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
126618 DbMaskSet(pToplevel->cookieMask, iDb);
126624 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
126634 sqlite3 *db = pParse->db;
126635 int i;
126636 for(i=0; i<db->nDb; i++){
126637 Db *pDb = &db->aDb[i];
126638 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
126657 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
126660 DbMaskSet(pToplevel->writeMask, iDb);
126661 pToplevel->isMultiWrite |= setStatement;
126673 pToplevel->isMultiWrite = 1;
126694 pToplevel->mayAbort = 1;
126704 int errCode, /* extended error code */
126705 int onError, /* Constraint type */
126711 assert( pParse->pVdbe!=0 );
126713 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
126726 int onError, /* Constraint type */
126730 int j;
126732 Table *pTab = pIdx->pTable;
126734 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
126735 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
126736 if( pIdx->aColExpr ){
126737 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
126739 for(j=0; j<pIdx->nKeyCol; j++){
126741 assert( pIdx->aiColumn[j]>=0 );
126742 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
126744 sqlite3_str_appendall(&errMsg, pTab->zName);
126758 ** Code an OP_Halt due to non-unique rowid.
126762 int onError, /* Conflict resolution algorithm */
126763 Table *pTab /* The table with the non-unique rowid */
126766 int rc;
126767 if( pTab->iPKey>=0 ){
126768 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
126769 pTab->aCol[pTab->iPKey].zCnName);
126772 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
126784 static int collationMatch(const char *zColl, Index *pIndex){
126785 int i;
126787 for(i=0; i<pIndex->nColumn; i++){
126788 const char *z = pIndex->azColl[i];
126789 assert( z!=0 || pIndex->aiColumn[i]<0 );
126790 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
126807 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
126809 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126811 sqlite3RefillIndex(pParse, pIndex, -1);
126826 int iDb; /* The database index number */
126827 sqlite3 *db = pParse->db; /* The database connection */
126832 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
126834 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
126845 ** REINDEX -- 1
126846 ** REINDEX <collation> -- 2
126847 ** REINDEX ?<database>.?<tablename> -- 3
126848 ** REINDEX ?<database>.?<indexname> -- 4
126862 int iDb; /* The database index number */
126863 sqlite3 *db = pParse->db; /* The database connection */
126875 }else if( NEVER(pName2==0) || pName2->z==0 ){
126877 assert( pName1->z );
126878 zColl = sqlite3NameFromToken(pParse->db, pName1);
126892 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
126902 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
126904 sqlite3RefillIndex(pParse, pIndex, -1);
126918 int i;
126919 int nCol = pIdx->nColumn;
126920 int nKey = pIdx->nKeyCol;
126922 if( pParse->nErr ) return 0;
126923 if( pIdx->uniqNotNull ){
126924 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
126926 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
126931 const char *zColl = pIdx->azColl[i];
126932 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
126934 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
126935 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
126937 if( pParse->nErr ){
126938 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
126939 if( pIdx->bNoQuery==0 ){
126944 ** the missing index using the collation-needed callback. For
126947 pIdx->bNoQuery = 1;
126948 pParse->rc = SQLITE_ERROR_RETRY;
126963 Token *pName, /* Name of the common-table */
126969 sqlite3 *db = pParse->db;
126972 assert( pNew!=0 || db->mallocFailed );
126974 if( db->mallocFailed ){
126978 pNew->pSelect = pQuery;
126979 pNew->pCols = pArglist;
126980 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
126981 pNew->eM10d = eM10d;
126992 sqlite3ExprListDelete(db, pCte->pCols);
126993 sqlite3SelectDelete(db, pCte->pSelect);
126994 sqlite3DbFree(db, pCte->zName);
127017 sqlite3 *db = pParse->db;
127027 zName = pCte->zName;
127029 int i;
127030 for(i=0; i<pWith->nCte; i++){
127031 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
127038 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
127043 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
127045 if( db->mallocFailed ){
127049 pNew->a[pNew->nCte++] = *pCte;
127061 int i;
127062 for(i=0; i<pWith->nCte; i++){
127063 cteClear(db, &pWith->a[i]);
127097 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
127098 assert( !db->xCollNeeded || !db->xCollNeeded16 );
127099 if( db->xCollNeeded ){
127102 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
127106 if( db->xCollNeeded16 ){
127109 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
127112 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
127123 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
127126 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
127128 char *z = pColl->zName;
127129 int i;
127133 if( pColl2->xCmp!=0 ){
127135 pColl->xDel = 0; /* Do not copy the destructor */
127153 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
127154 if( pColl && pColl->xCmp==0 ){
127155 const char *zName = pColl->zName;
127156 sqlite3 *db = pParse->db;
127175 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
127184 int create /* Create a new entry if true */
127187 pColl = sqlite3HashFind(&db->aCollSeq, zName);
127190 int nName = sqlite3Strlen30(zName) + 1;
127201 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
127219 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
127237 int create /* True to create CollSeq if doesn't already exist */
127244 if( pColl ) pColl += enc-1;
127246 pColl = db->pDfltColl;
127257 db->enc = enc;
127258 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
127261 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
127286 sqlite3 *db = pParse->db;
127292 if( !p || !p->xCmp ){
127299 if( p && !p->xCmp && synthCollSeq(db, p) ){
127302 assert( !p || p->xCmp );
127305 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
127331 sqlite3 *db = pParse->db;
127333 u8 initbusy = db->init.busy;
127337 if( !initbusy && (!pColl || !pColl->xCmp) ){
127350 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
127351 ** is also -1. In other words, we are searching for a function that
127354 ** If nArg is -2 that means that we are searching for any function
127364 ** 4: UTF8/16 conversion required - argument count matches exactly
127365 ** 5: UTF16 byte order conversion required - argument count matches exactly
127368 ** If nArg==(-2) then any function with a non-null xSFunc is
127370 ** a non-match.
127373 static int matchQuality(
127375 int nArg, /* Desired number of arguments. (-1)==any */
127378 int match;
127379 assert( p->nArg>=-1 );
127382 if( p->nArg!=nArg ){
127383 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
127384 if( p->nArg>=0 ) return 0;
127389 if( p->nArg==nArg ){
127396 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
127398 }else if( (enc & p->funcFlags & 2)!=0 ){
127410 int h, /* Hash of the name */
127414 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
127415 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
127416 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
127428 int nDef /* Length of the apDef[] list */
127430 int i;
127434 int nName = sqlite3Strlen30(zName);
127435 int h = SQLITE_FUNC_HASH(zName[0], nName);
127439 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
127440 aDef[i].pNext = pOther->pNext;
127441 pOther->pNext = &aDef[i];
127454 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
127462 ** If nArg is -2, then the first valid function found is returned. A
127463 ** function is valid if xSFunc is non-zero. The nArg==(-2)
127465 ** of arguments. If nArg is -2, then createFlag must be 0.
127473 const char *zName, /* Name of the function. zero-terminated */
127474 int nArg, /* Number of arguments. -1 means any number */
127480 int bestScore = 0; /* Score of best match */
127481 int h; /* Hash value */
127482 int nName; /* Length of the name */
127484 assert( nArg>=(-2) );
127485 assert( nArg>=(-1) || createFlag==0 );
127488 /* First search for a match amongst the application-defined functions.
127490 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
127492 int score = matchQuality(p, nArg, enc);
127497 p = p->pNext;
127500 /* If no match is found, search the built-in functions.
127502 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
127503 ** functions even if a prior app-defined function was found. And give
127504 ** priority to built-in functions.
127509 ** new function. But the FuncDefs for built-in functions are read-only.
127510 ** So we must not search for built-ins when creating a new function.
127512 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
127517 int score = matchQuality(p, nArg, enc);
127522 p = p->pNext;
127534 pBest->zName = (const char*)&pBest[1];
127535 pBest->nArg = (u16)nArg;
127536 pBest->funcFlags = enc;
127538 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
127539 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
127545 pBest->pNext = pOther;
127549 if( pBest && (pBest->xSFunc || createFlag) ){
127571 temp1 = pSchema->tblHash;
127572 temp2 = pSchema->trigHash;
127573 sqlite3HashInit(&pSchema->trigHash);
127574 sqlite3HashClear(&pSchema->idxHash);
127579 sqlite3HashInit(&pSchema->tblHash);
127585 sqlite3HashClear(&pSchema->fkeyHash);
127586 pSchema->pSeqTab = 0;
127587 if( pSchema->schemaFlags & DB_SchemaLoaded ){
127588 pSchema->iGeneration++;
127590 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
127606 }else if ( 0==p->file_format ){
127607 sqlite3HashInit(&p->tblHash);
127608 sqlite3HashInit(&p->idxHash);
127609 sqlite3HashInit(&p->trigHash);
127610 sqlite3HashInit(&p->fkeyHash);
127611 p->enc = SQLITE_UTF8;
127644 ** pSrc->a[0].pTab Pointer to the Table object
127645 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
127649 SrcItem *pItem = pSrc->a;
127651 assert( pItem && pSrc->nSrc>=1 );
127653 if( pItem->pTab ) sqlite3DeleteTable(pParse->db, pItem->pTab);
127654 pItem->pTab = pTab;
127655 pItem->fg.notCte = 1;
127657 pTab->nTabRef++;
127658 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
127665 /* Generate byte-code that will report the number of rows modified
127668 SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
127675 /* Return true if table pTab is read-only.
127677 ** A table is read-only if any of the following are true:
127692 ** is for a top-level SQL statement.
127694 static int vtabIsReadOnly(Parse *pParse, Table *pTab){
127695 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
127702 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
127705 if( pParse->pToplevel!=0
127706 && pTab->u.vtab.p->eVtabRisk >
127707 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
127710 pTab->zName);
127714 static int tabIsReadOnly(Parse *pParse, Table *pTab){
127719 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
127720 db = pParse->db;
127721 if( (pTab->tabFlags & TF_Readonly)!=0 ){
127722 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
127724 assert( pTab->tabFlags & TF_Shadow );
127731 ** If pTab is not writable -> generate an error message and return 1.
127732 ** If pTab is writable but other errors have occurred -> return 1.
127733 ** If pTab is writable and no prior errors -> return 0;
127735 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){
127737 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
127742 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
127744 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
127764 int iCur /* Cursor number for ephemeral table */
127769 sqlite3 *db = pParse->db;
127770 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
127774 assert( pFrom->nSrc==1 );
127775 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
127776 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
127777 assert( pFrom->a[0].fg.isUsing==0 );
127778 assert( pFrom->a[0].u3.pOn==0 );
127799 SrcList *pSrc, /* the FROM clause -- which tables to scan */
127805 sqlite3 *db = pParse->db;
127817 sqlite3ExprDelete(pParse->db, pWhere);
127818 sqlite3ExprListDelete(pParse->db, pOrderBy);
127838 pTab = pSrc->a[0].pTab;
127847 assert( pPk->nKeyCol>=1 );
127848 if( pPk->nKeyCol==1 ){
127850 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol );
127851 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
127855 int i;
127856 for(i=0; i<pPk->nKeyCol; i++){
127858 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol );
127859 p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
127864 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
127871 pSrc->a[0].pTab = 0;
127873 pSrc->a[0].pTab = pTab;
127874 if( pSrc->a[0].fg.isIndexedBy ){
127875 assert( pSrc->a[0].fg.isCte==0 );
127876 pSrc->a[0].u2.pIBIndex = 0;
127877 pSrc->a[0].fg.isIndexedBy = 0;
127878 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
127879 }else if( pSrc->a[0].fg.isCte ){
127880 pSrc->a[0].u2.pCteUse->nUse++;
127912 int i; /* Loop counter */
127915 int iTabCur; /* Cursor number for the table */
127916 int iDataCur = 0; /* VDBE cursor for the canonical data source */
127917 int iIdxCur = 0; /* Cursor number of the first index */
127918 int nIdx; /* Number of indices */
127922 int iDb; /* Database number */
127923 int memCnt = 0; /* Memory cell used for change counting */
127924 int rcauth; /* Value returned by authorization callback */
127925 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
127926 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
127929 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
127931 int iKey; /* Memory cell holding key of row to be deleted */
127933 int iEphCur = 0; /* Ephemeral table holding all primary key values */
127934 int iRowSet = 0; /* Register for rowset of rows to delete */
127935 int addrBypass = 0; /* Address of jump over the delete logic */
127936 int addrLoop = 0; /* Top of the delete loop */
127937 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
127938 int bComplex; /* True if there are triggers or FKs or
127942 int isView; /* True if attempting to delete from a view */
127947 db = pParse->db;
127948 assert( db->pParse==pParse );
127949 if( pParse->nErr ){
127952 assert( db->mallocFailed==0 );
127953 assert( pTabList->nSrc==1 );
127982 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
128006 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128007 assert( iDb<db->nDb );
128008 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
128009 db->aDb[iDb].zDbSName);
128018 assert( pTabList->nSrc==1 );
128019 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
128020 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128021 pParse->nTab++;
128027 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
128036 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
128065 if( (db->flags & SQLITE_CountRows)!=0
128066 && !pParse->nested
128067 && !pParse->pTriggerTab
128068 && !pParse->bReturning
128070 memCnt = ++pParse->nMem;
128081 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
128091 && db->xPreUpdateCallback==0
128095 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
128097 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
128098 pTab->zName, P4_STATIC);
128100 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
128101 assert( pIdx->pSchema==pTab->pSchema );
128103 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
128105 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
128118 iRowSet = ++pParse->nMem;
128125 nPk = pPk->nKeyCol;
128126 iPk = pParse->nMem+1;
128127 pParse->nMem += nPk;
128128 iEphCur = pParse->nTab++;
128137 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
128138 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
128139 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
128160 assert( pPk->aiColumn[i]>=0 );
128162 pPk->aiColumn[i], iPk+i);
128166 iKey = ++pParse->nMem;
128167 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
128171 /* For ONEPASS, no need to store the rowid/primary-key. There is only
128182 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
128183 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
128189 iKey = ++pParse->nMem;
128192 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
128208 int iAddrOnce = 0;
128222 /* Set up a loop over the rowids/primary-keys that were found in the
128223 ** where-clause loop above.
128227 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
128256 pParse->isMultiWrite = 0;
128264 int count = (pParse->nested==0); /* True to count changes */
128269 /* End of the loop over all rowids/primary-keys. */
128280 } /* End non-truncate path */
128286 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
128332 ** cursor number iIdxCur+i for the i-th index.
128364 int iDataCur, /* Cursor from which column data is extracted */
128365 int iIdxCur, /* First index cursor */
128366 int iPk, /* First memory cell containing the PRIMARY KEY */
128368 u8 count, /* If non-zero, increment the row change counter */
128371 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
128373 Vdbe *v = pParse->pVdbe; /* Vdbe */
128374 int iOld = 0; /* First register in OLD.* array */
128375 int iLabel; /* Label resolved to end of generated code */
128381 iDataCur, iIdxCur, iPk, (int)nPk));
128398 int iCol; /* Iterator used while populating OLD.* */
128399 int addrStart; /* Start of BEFORE trigger programs */
128407 iOld = pParse->nMem+1;
128408 pParse->nMem += (1 + pTab->nCol);
128410 /* Populate the OLD.* pseudo-table register array. These values will be
128413 for(iCol=0; iCol<pTab->nCol; iCol++){
128417 int kk = sqlite3TableColumnToStorage(pTab, iCol);
128441 iIdxNoSeek = -1;
128454 ** If variable 'count' is non-zero, then this OP_Delete instruction should
128455 ** invoke the update-hook. The pre-update-hook, on the other hand should
128457 ** the update-hook is not invoked for rows removed by REPLACE, but the
128458 ** pre-update-hook is.
128464 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
128508 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
128509 ** index is the 0-th index.)
128517 int iDataCur, /* Cursor of table holding data. */
128518 int iIdxCur, /* First index cursor */
128519 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
128520 int iIdxNoSeek /* Do not delete from this cursor */
128522 int i; /* Index loop counter */
128523 int r1 = -1; /* Register holding an index key */
128524 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
128530 v = pParse->pVdbe;
128532 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
128537 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
128541 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
128579 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
128582 int iDataCur, /* Cursor number from which to take column data */
128583 int regOut, /* Put the new key into this register if not 0 */
128584 int prefixOnly, /* Compute only a unique prefix of the key */
128585 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
128587 int regPrior /* Register holding previous generated key */
128589 Vdbe *v = pParse->pVdbe;
128590 int j;
128591 int regBase;
128592 int nCol;
128595 if( pIdx->pPartIdxWhere ){
128597 pParse->iSelfTab = iDataCur + 1;
128598 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
128600 pParse->iSelfTab = 0;
128601 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
128607 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
128609 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
128612 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
128613 && pPrior->aiColumn[j]!=XN_EXPR
128619 if( pIdx->aiColumn[j]>=0 ){
128637 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
128641 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
128643 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
128660 ** This file contains the C-language implementations for many of the SQL
128677 assert( context->pVdbe!=0 );
128678 pOp = &context->pVdbe->aOp[context->iOp-1];
128679 assert( pOp->opcode==OP_CollSeq );
128680 assert( pOp->p4type==P4_COLLSEQ );
128681 return pOp->p4.pColl;
128689 assert( context->isError<=0 );
128690 context->isError = -1;
128691 context->skipFlag = 1;
128695 ** Implementation of the non-aggregate min() and max() functions
128699 int argc,
128702 int i;
128703 int mask; /* 0 for min() or 0xffffffff for max() */
128704 int iBest;
128708 mask = sqlite3_user_data(context)==0 ? 0 : -1;
128711 assert( mask==-1 || mask==0 );
128729 int NotUsed,
128733 int i = sqlite3_value_type(argv[0]) - 1;
128741 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
128745 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
128754 int argc,
128766 int argc,
128790 sqlite3_result_int(context, (int)(z-z0));
128805 int argc,
128817 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2;
128839 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
128842 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
128850 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
128852 ** equivalent positive 64-bit two complement value. */
128853 sqlite3_result_error(context, "integer overflow", -1);
128856 iVal = -iVal;
128862 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
128869 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
128873 if( rVal<0 ) rVal = -rVal;
128893 int argc,
128898 int nHaystack;
128899 int nNeedle;
128900 int typeHaystack, typeNeedle;
128901 int N = 1;
128902 int isText;
128940 nHaystack--;
128961 int argc,
128967 int n;
128971 x.nArg = argc-1;
128974 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
128987 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
128988 ** of x. If x is text, then we actually count UTF-8 characters.
128997 int argc,
129002 int len;
129003 int p0type;
129005 int negP2 = 0;
129032 ** as substr(X,1,N) - it returns the first N characters of X. This
129033 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
129034 ** from 2009-02-02 for compatibility of applications that exploited the
129041 p2 = -p2;
129045 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
129055 p1--;
129057 p2--;
129060 p1 -= p2;
129070 p1--;
129072 for(z2=z; *z2 && p2; p2--){
129075 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
129079 p2 = len-p1;
129090 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129091 int n = 0;
129103 /* If Y==0 and X will fit in a 64-bit int,
129107 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
129110 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
129135 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
129136 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
129137 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
129152 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129155 int i, n;
129171 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129174 int i, n;
129199 #define noopFunc versionFunc /* Substitute function - never called */
129206 int NotUsed,
129214 ** (or -9223372036854775808) since when you do abs() of that
129219 ** therefore be no less than -9223372036854775807.
129221 r = -(r & LARGEST_INT64);
129232 int argc,
129256 int NotUsed,
129261 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
129270 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
129276 int NotUsed,
129290 int NotUsed,
129295 /* IMP: R-11217-42568 This function is a wrapper around the
129301 ** A structure defining how to do GLOB-style comparisons.
129324 /* The correct SQL-92 behavior is for the LIKE operator to ignore
129339 ** Compare two UTF-8 strings for equality where the first string is
129359 ** range of characters can be specified using '-'. Example:
129360 ** "[a-z]" matches any single lower-case letter. To match a '-', make
129376 static int patternCompare(
129383 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
129384 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
129385 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
129402 if( pInfo->matchSet==0 ){
129408 assert( matchOther<0x80 ); /* '[' is a single-byte character */
129410 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
129423 ** For a case-insensitive search, set variable cx to be the same as
129429 int bMatch;
129446 int bMatch;
129456 if( pInfo->matchSet==0 ){
129462 int seen = 0;
129463 int invert = 0;
129476 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
129507 ** non-zero if there is no match.
129509 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
129520 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
129521 ** a miss - like strcmp().
129523 SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
129539 SQLITE_API int sqlite3_like_count = 0;
129545 ** the built-in LIKE operator. The first argument to the function is the
129557 int argc,
129562 int nPat;
129583 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
129584 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
129585 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
129586 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
129590 /* The escape character string must consist of a single UTF-8 character.
129595 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
129597 "ESCAPE expression must be a single character", -1);
129601 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
129604 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
129605 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
129608 escape = pInfo->matchSet;
129628 int NotUsed,
129644 int NotUsed,
129648 /* IMP: R-48699-48617 This function is an SQL wrapper around the
129649 ** sqlite3_libversion() C-interface. */
129650 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
129660 int NotUsed,
129664 /* IMP: R-24470-31136 This function is an SQL wrapper around the
129666 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
129672 ** its side-effects.
129676 int argc,
129692 int argc,
129698 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
129716 int argc,
129719 int n;
129722 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
129726 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
129730 /* Array for converting from half-bytes (nybbles) into ASCII hex
129745 assert( pStr!=0 && pStr->nChar==0 );
129755 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
129772 if( pStr->accError==0 ){
129773 char *zText = pStr->zText;
129774 int i;
129783 pStr->nChar = nBlob*2 + 3;
129805 ** Strings are surrounded by single-quotes with escapes on interior quotes
129810 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129815 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
129826 ** The unicode() function. Return the integer unicode code-point value
129831 int argc,
129846 int argc,
129850 int i;
129879 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
129888 int argc,
129891 int i, n;
129907 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
129913 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
129916 static int strContainsChar(const u8 *zStr, int nStr, u32 ch){
129950 int argc,
129954 int nPass = 0;
129956 int nHex = sqlite3_value_bytes(argv[0]);
129993 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
130003 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
130007 int argc,
130011 int rc;
130016 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
130030 int argc,
130037 int nStr; /* Size of zStr */
130038 int nPattern; /* Size of zPattern */
130039 int nRep; /* Size of zRep */
130041 int loopLimit; /* Last zStr[] that might match zPattern[] */
130042 int i, j; /* Loop counters */
130055 || sqlite3_context_db_handle(context)->mallocFailed );
130075 loopLimit = nStr - nPattern;
130082 nOut += nRep - nPattern;
130083 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
130084 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
130085 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
130091 if( (cntExpand&(cntExpand-1))==0 ){
130096 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
130106 i += nPattern-1;
130109 assert( j+nStr-i+1<=nOut );
130110 memcpy(&zOut[j], &zStr[i], nStr-i);
130111 j += nStr - i;
130123 int argc,
130128 unsigned int nIn; /* Number of bytes in input */
130129 int flags; /* 1: trimleft 2: trimright 3: trim */
130130 int i; /* Loop counter */
130131 unsigned int *aLen = 0; /* Length of each character in zCharSet */
130133 int nChar; /* Number of characters in zCharSet */
130166 aLen[nChar] = (unsigned)(z - azChar[nChar]);
130174 unsigned int len = 0;
130181 nIn -= len;
130186 unsigned int len = 0;
130189 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
130192 nIn -= len;
130205 ** Return a string value that is the concatenation of all non-null
130210 int argc,
130212 int nSep,
130216 int i;
130221 n += (argc-1)*nSep;
130249 ** concatentation of all non-null arguments.
130253 int argc,
130263 ** argument. Use the first argument (which must be non-NULL) as the
130268 int argc,
130271 int nSep = sqlite3_value_bytes(argv[0]);
130274 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
130282 ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
130283 ** When the "sqlite3" command-line shell is built using this functionality,
130285 ** involving application-defined functions to be examined in a generic
130290 int argc,
130293 /* no-op */
130301 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
130302 ** is only available if the SQLITE_SOUNDEX compile-time option is used
130309 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
130314 int argc,
130319 int i, j;
130338 int code = iCode[zIn[i]&0x7f];
130354 /* IMP: R-64894-50321 The string "?000" is returned if the argument
130363 ** A function that loads a shared-library extension then returns NULL.
130365 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
130374 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
130375 sqlite3_result_error(context, "not authorized", -1);
130385 sqlite3_result_error(context, zErrMsg, -1);
130399 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
130402 u8 approx; /* True if any non-integer value was input to the sum */
130407 ** Do one step of the Kahan-Babushka-Neumaier summation.
130418 volatile double s = pSum->rSum;
130421 pSum->rErr += (s - t) + r;
130423 pSum->rErr += (r - t) + s;
130425 pSum->rSum = t;
130432 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
130435 iBig = iVal - iSm;
130444 ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
130450 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
130452 p->rSum = (double)(iVal - iSm);
130453 p->rErr = (double)iSm;
130455 p->rSum = (double)iVal;
130456 p->rErr = 0.0;
130463 ** The SUM() function follows the (broken) SQL standard which means
130470 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
130472 int type;
130478 p->cnt++;
130479 if( p->approx==0 ){
130481 kahanBabuskaNeumaierInit(p, p->iSum);
130482 p->approx = 1;
130485 i64 x = p->iSum;
130487 p->iSum = x;
130489 p->ovrfl = 1;
130490 kahanBabuskaNeumaierInit(p, p->iSum);
130491 p->approx = 1;
130499 p->ovrfl = 0;
130506 static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
130508 int type;
130513 /* p is always non-NULL because sumStep() will have been called first
130516 assert( p->cnt>0 );
130517 p->cnt--;
130518 if( !p->approx ){
130519 p->iSum -= sqlite3_value_int64(argv[0]);
130523 kahanBabuskaNeumaierStepInt64(p, -iVal);
130529 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
130539 if( p && p->cnt>0 ){
130540 if( p->approx ){
130541 if( p->ovrfl ){
130542 sqlite3_result_error(context,"integer overflow",-1);
130543 }else if( !sqlite3IsOverflow(p->rErr) ){
130544 sqlite3_result_double(context, p->rSum+p->rErr);
130546 sqlite3_result_double(context, p->rSum);
130549 sqlite3_result_int64(context, p->iSum);
130556 if( p && p->cnt>0 ){
130558 if( p->approx ){
130559 r = p->rSum;
130560 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
130562 r = (double)(p->iSum);
130564 sqlite3_result_double(context, r/(double)p->cnt);
130572 if( p->approx ){
130573 r = p->rSum;
130574 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
130576 r = (double)(p->iSum);
130590 int bInverse; /* True if xInverse() ever called */
130597 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
130601 p->n++;
130608 ** expressed as a 32-bit integer. */
130609 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
130610 || p->n==sqlite3_aggregate_count(context) );
130616 sqlite3_result_int64(context, p ? p->n : 0);
130619 static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
130622 /* p is always non-NULL since countStep() will have been called first */
130624 p->n--;
130626 p->bInverse = 1;
130639 int NotUsed,
130650 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
130651 }else if( pBest->flags ){
130652 int max;
130653 int cmp;
130658 ** sqlite3_user_data() function returns (void *)-1. For min() it
130671 pBest->db = sqlite3_context_db_handle(context);
130675 static void minMaxValueFinalize(sqlite3_context *context, int bValue){
130679 if( pRes->flags ){
130709 int nAccum; /* Number of strings presently concatenated */
130710 int nFirstSepLength; /* Used to detect separator length change */
130711 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
130713 ** (Hence, its slots in use number nAccum-1 between method calls.)
130716 int *pnSepLengths;
130722 int argc,
130728 int nVal, nSep;
130734 int firstTerm = pGCC->str.mxAlloc==0;
130735 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
130738 sqlite3_str_appendchar(&pGCC->str, 1, ',');
130742 pGCC->nFirstSepLength = 1;
130749 sqlite3_str_append(&pGCC->str, zSep, nSep);
130755 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
130756 int *pnsl = pGCC->pnSepLengths;
130759 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
130761 int i = 0, nA = pGCC->nAccum-1;
130762 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
130765 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
130768 if( ALWAYS(pGCC->nAccum>0) ){
130769 pnsl[pGCC->nAccum-1] = nSep;
130771 pGCC->pnSepLengths = pnsl;
130773 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
130780 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
130782 pGCC->nAccum += 1;
130786 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
130793 int argc,
130801 /* pGCC is always non-NULL since groupConcatStep() will have always
130804 int nVS;
130809 pGCC->nAccum -= 1;
130810 if( pGCC->pnSepLengths!=0 ){
130811 assert(pGCC->nAccum >= 0);
130812 if( pGCC->nAccum>0 ){
130813 nVS += *pGCC->pnSepLengths;
130814 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
130815 (pGCC->nAccum-1)*sizeof(int));
130818 /* If removing single accumulated string, harmlessly over-do. */
130819 nVS += pGCC->nFirstSepLength;
130821 if( nVS>=(int)pGCC->str.nChar ){
130822 pGCC->str.nChar = 0;
130824 pGCC->str.nChar -= nVS;
130825 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
130827 if( pGCC->str.nChar==0 ){
130828 pGCC->str.mxAlloc = 0;
130829 sqlite3_free(pGCC->pnSepLengths);
130830 pGCC->pnSepLengths = 0;
130841 sqlite3ResultStrAccum(context, &pGCC->str);
130843 sqlite3_free(pGCC->pnSepLengths);
130852 StrAccum *pAccum = &pGCC->str;
130853 if( pAccum->accError==SQLITE_TOOBIG ){
130855 }else if( pAccum->accError==SQLITE_NOMEM ){
130857 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
130861 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
130870 ** This routine does per-connection function registration. Most
130871 ** of the built-in functions above are part of the global function set.
130875 int rc = sqlite3_overload_function(db, "MATCH", 2);
130883 ** Re-register the built-in LIKE functions. The caseSensitive
130887 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
130890 int flags;
130891 int nArg;
130903 pDef->funcFlags |= flags;
130904 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
130913 ** LIKE-style function then return FALSE.
130925 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
130927 int nExpr;
130929 assert( pExpr->op==TK_FUNCTION );
130931 if( !pExpr->x.pList ){
130934 nExpr = pExpr->x.pList->nExpr;
130936 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
130940 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
130948 memcpy(aWc, pDef->pUserData, 3);
130956 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
130958 if( pEscape->op!=TK_STRING ) return 0;
130960 zEscape = pEscape->u.zToken;
130967 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
130983 /* Extra math functions that require linking with -lm
130998 int argc,
131041 ** ln(X) - natural logarithm
131042 ** log(X) - log X base 10
131043 ** log10(X) - log X base 10
131044 ** log(B,X) - log X base B
131048 int argc,
131098 ** Implementation of 1-argument SQL math functions:
131100 ** exp(X) - Compute e to the X-th power
131104 int argc,
131107 int type0;
131120 ** Implementation of 2-argument SQL math functions:
131122 ** power(X,Y) - Compute X to the Y-th power
131126 int argc,
131129 int type0, type1;
131145 ** Implementation of 0-argument pi() function.
131149 int argc,
131164 int argc,
131167 int type0;
131174 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
131186 int argc,
131191 int y, z;
131204 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
131210 ** to the global function hash table. This occurs at start-time (as
131221 ** FuncDef.pHash elements at start-time. The elements of this array
131222 ** are read-only after initialization is complete.
131261 FUNCTION(min, -1, 0, 1, minmaxFunc ),
131265 FUNCTION(max, -1, 1, 1, minmaxFunc ),
131274 FUNCTION(printf, -1, 0, 0, printfFunc ),
131275 FUNCTION(format, -1, 0, 0, printfFunc ),
131277 FUNCTION(char, -1, 0, 0, charFunc ),
131291 FUNCTION(concat, -1, 0, 0, concatFunc ),
131293 FUNCTION(concat_ws, -1, 0, 0, concatwsFunc ),
131337 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
131378 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
131389 #if 0 /* Enable to print out how the built-in functions are hashed */
131391 int i;
131394 printf("FUNC-HASH %02d:", i);
131395 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
131396 int n = sqlite3Strlen30(p->zName);
131397 int h = p->zName[0] + n;
131398 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
131399 printf(" %s(%d)", p->zName, h);
131429 ** --------------------------
131511 ** ---------------
131513 ** Before coding an UPDATE or DELETE row operation, the code-generator
131518 ** accessed). No information is required by the code-generator before
131522 ** sqlite3FkRequired() - Test to see if FK processing is required.
131523 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
131527 ** --------------------------------------
131529 ** sqlite3FkCheck() - Check for foreign key violations.
131530 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
131531 ** sqlite3FkDelete() - Delete an FKey structure.
131536 ** -----------------------
131566 ** constraint to the parent table column stored in the left-most column
131568 ** child table column that corresponds to the second left-most column of
131587 ** then non-zero is returned, and a "foreign key mismatch" error loaded
131588 ** into pParse. If an OOM error occurs, non-zero is returned and the
131589 ** pParse->db->mallocFailed flag is set.
131591 SQLITE_PRIVATE int sqlite3FkLocateIndex(
131596 int **paiCol /* OUT: Map of index columns in pFKey */
131599 int *aiCol = 0; /* Value to return via *paiCol */
131600 int nCol = pFKey->nCol; /* Number of columns in parent key */
131601 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
131608 /* If this is a non-composite (single column) foreign key, check if it
131614 ** Non-composite foreign keys do not require the aiCol array.
131624 if( pParent->iPKey>=0 ){
131626 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
131632 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
131637 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
131638 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
131649 int i;
131650 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
131655 /* If zKey is non-NULL, then this foreign key was declared to
131659 int i, j;
131661 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
131670 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
131672 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
131674 zIdxCol = pParent->aCol[iCol].zCnName;
131676 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
131677 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
131689 if( !pParse->disableTriggers ){
131691 "foreign key mismatch - \"%w\" referencing \"%w\"",
131692 pFKey->pFrom->zName, pFKey->zTo);
131694 sqlite3DbFree(pParse->db, aiCol);
131706 ** affected - once to "delete" the old row, and then again to "insert" the
131716 ** --------------------------------------------------------------------------
131730 int iDb, /* Index of database housing pTab */
131734 int *aiCol, /* Map from parent key columns to child table columns */
131735 int regData, /* Address of array containing child table row */
131736 int nIncr, /* Increment constraint counter by this */
131737 int isIgnore /* If true, pretend pTab contains all NULL values */
131739 int i; /* Iterator variable */
131741 int iCur = pParse->nTab - 1; /* Cursor number to use */
131742 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
131745 (!pFKey->isDeferred
131746 && !(pParse->db->flags & SQLITE_DeferFKs)
131747 && !pParse->pToplevel
131748 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
131758 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
131761 for(i=0; i<pFKey->nCol; i++){
131762 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
131770 int iMustBeInt; /* Address of MustBeInt instruction */
131771 int regTemp = sqlite3GetTempReg(pParse);
131779 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
131784 ** to increment the constraint-counter (i.e. this is an INSERT operation),
131786 ** increment the constraint-counter. */
131787 if( pTab==pFKey->pFrom && nIncr==1 ){
131795 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
131799 int nCol = pFKey->nCol;
131800 int regTemp = sqlite3GetTempRange(pParse, nCol);
131802 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
131806 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
131811 ** to increment the constraint-counter (i.e. this is an INSERT operation),
131813 ** increment the constraint-counter.
131815 ** If any of the parent-key values are NULL, then the row cannot match
131817 ** of the parent-key values are NULL (at this point it is known that
131820 if( pTab==pFKey->pFrom && nIncr==1 ){
131821 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
131823 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
131825 int iParent = 1+regData;
131826 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
131827 pIdx->aiColumn[i]);
131828 assert( pIdx->aiColumn[i]>=0 );
131829 assert( aiCol[i]!=pTab->iPKey );
131830 if( pIdx->aiColumn[i]==pTab->iPKey ){
131841 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
131848 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
131849 && !pParse->pToplevel
131850 && !pParse->isMultiWrite
131860 if( nIncr>0 && pFKey->isDeferred==0 ){
131863 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
131882 int regBase, /* Contents of table pTab */
131888 sqlite3 *db = pParse->db;
131892 if( iCol>=0 && iCol!=pTab->iPKey ){
131893 pCol = &pTab->aCol[iCol];
131894 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
131895 pExpr->affExpr = pCol->affinity;
131897 if( zColl==0 ) zColl = db->pDfltColl->zName;
131900 pExpr->iTable = regBase;
131901 pExpr->affExpr = SQLITE_AFF_INTEGER;
131914 int iCursor, /* The open cursor on the table */
131920 pExpr->y.pTab = pTab;
131921 pExpr->iTable = iCursor;
131922 pExpr->iColumn = iCol;
131931 ** code for an SQL UPDATE operation, this function may be called twice -
131934 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
131943 ** --------------------------------------------------------------------------
131961 int *aiCol, /* Map from pIdx cols to child table cols */
131962 int regData, /* Parent row data starts here */
131963 int nIncr /* Amount to increment deferred counter by */
131965 sqlite3 *db = pParse->db; /* Database handle */
131966 int i; /* Iterator variable */
131970 int iFkIfZero = 0; /* Address of OP_FkIfZero */
131973 assert( pIdx==0 || pIdx->pTable==pTab );
131974 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
131975 assert( pIdx!=0 || pFKey->nCol==1 );
131979 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
131985 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
131991 for(i=0; i<pFKey->nCol; i++){
131998 iCol = pIdx ? pIdx->aiColumn[i] : -1;
132000 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
132002 zCol = pFKey->pFrom->aCol[iCol].zCnName;
132022 if( pTab==pFKey->pFrom && nIncr>0 ){
132027 pLeft = exprTableRegister(pParse, pTab, regData, -1);
132028 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
132033 for(i=0; i<pIdx->nKeyCol; i++){
132034 i16 iCol = pIdx->aiColumn[i];
132037 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
132055 if( pParse->nErr==0 ){
132057 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
132085 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
132091 ** and all of its sub-components.
132093 ** The Trigger structure or any of its sub-components may be allocated from
132098 TriggerStep *pStep = p->step_list;
132099 sqlite3ExprDelete(dbMem, pStep->pWhere);
132100 sqlite3ExprListDelete(dbMem, pStep->pExprList);
132101 sqlite3SelectDelete(dbMem, pStep->pSelect);
132102 sqlite3ExprDelete(dbMem, p->pWhen);
132112 SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
132114 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
132119 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
132120 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
132121 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
132144 sqlite3 *db = pParse->db;
132145 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
132146 int iSkip = 0;
132158 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132159 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
132166 pParse->disableTriggers = 1;
132168 pParse->disableTriggers = 0;
132179 if( (db->flags & SQLITE_DeferFKs)==0 ){
132200 ** is set to -1). If the rowid column is modified by the UPDATE statement
132201 ** the bChngRowid argument is non-zero.
132206 static int fkChildIsModified(
132209 int *aChange, /* Array indicating modified columns */
132210 int bChngRowid /* True if rowid is modified by this update */
132212 int i;
132213 for(i=0; i<p->nCol; i++){
132214 int iChildKey = p->aCol[i].iFrom;
132216 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
132227 ** is set to -1). If the rowid column is modified by the UPDATE statement
132228 ** the bChngRowid argument is non-zero.
132233 static int fkParentIsModified(
132236 int *aChange,
132237 int bChngRowid
132239 int i;
132240 for(i=0; i<p->nCol; i++){
132241 char *zKey = p->aCol[i].zCol;
132242 int iKey;
132243 for(iKey=0; iKey<pTab->nCol; iKey++){
132244 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
132245 Column *pCol = &pTab->aCol[iKey];
132247 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
132248 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
132262 static int isSetNullAction(Parse *pParse, FKey *pFKey){
132264 if( pTop->pTriggerPrg ){
132265 Trigger *p = pTop->pTriggerPrg->pTrigger;
132266 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
132267 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
132269 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
132282 ** first register in an array of (pTab->nCol+1) registers containing the
132288 ** first register of an array of (pTab->nCol+1) registers containing the new
132299 int regOld, /* Previous row data is stored here */
132300 int regNew, /* New row data is stored here */
132301 int *aChange, /* Array indicating UPDATEd columns (or 0) */
132302 int bChngRowid /* True if rowid is UPDATEd */
132304 sqlite3 *db = pParse->db; /* Database handle */
132306 int iDb; /* Index of database containing pTab */
132308 int isIgnoreErrors = pParse->disableTriggers;
132310 /* Exactly one of regOld and regNew should be non-zero. */
132313 /* If foreign-keys are disabled, this function is a no-op. */
132314 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
132317 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132318 zDb = db->aDb[iDb].zDbSName;
132322 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
132325 int *aiFree = 0;
132326 int *aiCol;
132327 int iCol;
132328 int i;
132329 int bIgnore = 0;
132332 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
132342 if( pParse->disableTriggers ){
132343 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
132345 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
132349 if( !isIgnoreErrors || db->mallocFailed ) return;
132356 ** FK counter for each row of the current table with non-NULL keys.
132359 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
132360 for(i=0; i<pFKey->nCol; i++){
132361 int iFromCol, iReg;
132362 iFromCol = pFKey->aCol[i].iFrom;
132363 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
132366 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
132370 assert( pFKey->nCol==1 || (aiFree && pIdx) );
132375 iCol = pFKey->aCol[0].iFrom;
132378 for(i=0; i<pFKey->nCol; i++){
132379 if( aiCol[i]==pTab->iPKey ){
132380 aiCol[i] = -1;
132382 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
132387 if( db->xAuth ){
132388 int rcauth;
132389 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
132390 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
132396 /* Take a shared-cache advisory read-lock on the parent table. Allocate
132399 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
132400 pParse->nTab++;
132406 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
132425 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
132428 int *aiCol = 0;
132434 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
132435 && !pParse->pToplevel && !pParse->isMultiWrite
132444 if( !isIgnoreErrors || db->mallocFailed ) return;
132447 assert( aiCol || pFKey->nCol==1 );
132453 SrcItem *pItem = pSrc->a;
132454 pItem->pTab = pFKey->pFrom;
132455 pItem->zName = pFKey->pFrom->zName;
132456 pItem->pTab->nTabRef++;
132457 pItem->iCursor = pParse->nTab++;
132460 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
132463 int eAction = pFKey->aAction[aChange!=0];
132464 if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None;
132470 ** So do not set the "may-abort" flag in this case.
132473 ** may-abort flag will eventually be set on this statement anyway
132484 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
132488 pItem->zName = 0;
132506 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
132508 int i;
132509 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132510 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
132512 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
132516 for(i=0; i<pIdx->nKeyCol; i++){
132517 assert( pIdx->aiColumn[i]>=0 );
132518 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
132533 ** entry in the aChange[] array is set to -1. If the column is modified,
132538 ** non-zero. If there is no foreign key related processing, this function
132551 SQLITE_PRIVATE int sqlite3FkRequired(
132554 int *aChange, /* Non-NULL for UPDATE operations */
132555 int chngRowid /* True for UPDATE that affects rowid */
132557 int eRet = 1; /* Value to return if bHaveFK is true */
132558 int bHaveFK = 0; /* If FK processing is required */
132559 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
132564 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
132571 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132573 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
132579 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
132581 if( (pParse->db->flags & SQLITE_FkNoAction)==0
132582 && p->aAction[1]!=OE_None
132596 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
132604 ** require no special handling by the triggers sub-system, code for them is
132627 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
132629 sqlite3 *db = pParse->db; /* Database handle */
132630 int action; /* One of OE_None, OE_Cascade etc. */
132632 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
132634 action = pFKey->aAction[iAction];
132635 if( (db->flags & SQLITE_FkNoAction) ) action = OE_None;
132636 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
132639 pTrigger = pFKey->apTrigger[iAction];
132643 int nFrom; /* Length in bytes of zFrom */
132645 int *aiCol = 0; /* child table cols -> parent key cols */
132650 int i; /* Iterator variable */
132654 assert( aiCol || pFKey->nCol==1 );
132656 for(i=0; i<pFKey->nCol; i++){
132661 int iFromCol; /* Idx of column in child table */
132664 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
132666 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
132667 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
132669 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
132670 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
132708 Column *pCol = pFKey->pFrom->aCol + iFromCol;
132710 if( pCol->colFlags & COLFLAG_GENERATED ){
132711 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
132712 testcase( pCol->colFlags & COLFLAG_STORED );
132715 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
132731 zFrom = pFKey->pFrom->zName;
132735 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132741 pRaise->affExpr = OE_Abort;
132745 assert( pSrc->nSrc==1 );
132746 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
132747 pSrc->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
132764 nFrom + 1 /* Space for pStep->zTarget */
132767 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
132768 pStep->zTarget = (char *)&pStep[1];
132769 memcpy((char *)pStep->zTarget, zFrom, nFrom);
132771 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
132772 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
132773 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
132776 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
132780 /* Re-enable the lookaside buffer, if it was disabled earlier. */
132787 if( db->mallocFailed==1 ){
132796 pStep->op = TK_SELECT;
132800 pStep->op = TK_DELETE;
132805 pStep->op = TK_UPDATE;
132807 pStep->pTrig = pTrigger;
132808 pTrigger->pSchema = pTab->pSchema;
132809 pTrigger->pTabSchema = pTab->pSchema;
132810 pFKey->apTrigger[iAction] = pTrigger;
132811 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
132824 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
132825 int regOld, /* Address of array containing old row */
132826 int *aChange, /* Array indicating UPDATEd columns (or 0) */
132827 int bChngRowid /* True if rowid is UPDATEd */
132829 /* If foreign-key support is enabled, iterate through all FKs that
132832 ** trigger sub-program. */
132833 if( pParse->db->flags&SQLITE_ForeignKeys ){
132835 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
132855 FKey *pNext; /* Copy of pFKey->pNextFrom */
132859 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
132860 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
132863 if( db->pnBytesFreed==0 ){
132864 if( pFKey->pPrevTo ){
132865 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
132867 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
132868 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
132870 if( pFKey->pNextTo ){
132871 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
132875 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
132878 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
132882 fkTriggerDelete(db, pFKey->apTrigger[0]);
132883 fkTriggerDelete(db, pFKey->apTrigger[1]);
132886 pNext = pFKey->pNextFrom;
132921 int iCur, /* The cursor number of the table */
132922 int iDb, /* The database index in sqlite3.aDb[] */
132924 int opcode /* OP_OpenRead or OP_OpenWrite */
132928 assert( pParse->pVdbe!=0 );
132929 v = pParse->pVdbe;
132931 if( !pParse->db->noSharedCache ){
132932 sqlite3TableLock(pParse, iDb, pTab->tnum,
132933 (opcode==OP_OpenWrite)?1:0, pTab->zName);
132936 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
132937 VdbeComment((v, "%s", pTab->zName));
132941 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
132942 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
132944 VdbeComment((v, "%s", pTab->zName));
132954 ** ------------------------------
132977 int n;
132978 Table *pTab = pIdx->pTable;
132979 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
132980 if( !pIdx->zColAff ){
132984 for(n=0; n<pIdx->nColumn; n++){
132985 i16 x = pIdx->aiColumn[n];
132988 aff = pTab->aCol[x].affinity;
132993 assert( pIdx->bHasExpr );
132994 assert( pIdx->aColExpr!=0 );
132995 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
132999 pIdx->zColAff[n] = aff;
133001 pIdx->zColAff[n] = 0;
133002 return pIdx->zColAff;
133005 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
133006 return pIdx->zColAff;
133017 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
133019 int i, j;
133020 for(i=j=0; i<pTab->nCol; i++){
133021 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
133022 zColAff[j++] = pTab->aCol[i].affinity;
133026 zColAff[j--] = 0;
133036 ** For ordinary (legacy, non-strict) tables:
133037 ** -----------------------------------------
133043 ** which were then optimized out) then this routine becomes a no-op.
133053 ** --------- ---------------
133061 ** ------------------
133072 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
133073 int i;
133075 if( pTab->tabFlags & TF_Strict ){
133084 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
133085 pPrev->opcode = OP_TypeCheck;
133086 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
133089 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
133094 zColAff = pTab->zColAff;
133101 pTab->zColAff = zColAff;
133109 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
133110 || sqlite3VdbeDb(v)->mallocFailed );
133111 sqlite3VdbeChangeP4(v, -1, zColAff, i);
133117 ** Return non-zero if the table pTab in database iDb or any of its indices
133122 static int readsTable(Parse *p, int iDb, Table *pTab){
133124 int i;
133125 int iEnd = sqlite3VdbeCurrentAddr(v);
133127 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
133133 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
133135 Pgno tnum = pOp->p2;
133136 if( tnum==pTab->tnum ){
133139 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
133140 if( tnum==pIndex->tnum ){
133146 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
133147 assert( pOp->p4.pVtab!=0 );
133148 assert( pOp->p4type==P4_VTAB );
133159 static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
133160 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
133161 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
133162 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
133177 int iRegStore, /* Register holding the first column */
133180 int i;
133183 int eProgress;
133186 assert( pTab->tabFlags & TF_HasGenerated );
133187 testcase( pTab->tabFlags & TF_HasVirtual );
133188 testcase( pTab->tabFlags & TF_HasStored );
133193 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
133194 if( (pTab->tabFlags & TF_HasStored)!=0 ){
133195 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
133196 if( pOp->opcode==OP_Affinity ){
133198 ** columns. '@' is the no-op affinity and those columns have not
133200 int ii, jj;
133201 char *zP4 = pOp->p4.z;
133203 assert( pOp->p4type==P4_DYNAMIC );
133205 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
133208 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
133213 }else if( pOp->opcode==OP_TypeCheck ){
133217 pOp->p3 = 1;
133222 ** this is a two-pass algorithm. On the first pass, mark all generated
133225 for(i=0; i<pTab->nCol; i++){
133226 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
133227 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
133228 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
133229 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
133238 /* On the second pass, compute the value of each NOT-AVAILABLE column.
133243 pParse->iSelfTab = -iRegStore;
133247 for(i=0; i<pTab->nCol; i++){
133248 Column *pCol = pTab->aCol + i;
133249 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
133250 int x;
133251 pCol->colFlags |= COLFLAG_BUSY;
133254 pCol->colFlags &= ~COLFLAG_BUSY;
133260 assert( pCol->colFlags & COLFLAG_GENERATED );
133263 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
133268 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
133270 pParse->iSelfTab = 0;
133299 static int autoIncBegin(
133301 int iDb, /* Index of the database holding pTab */
133304 int memId = 0; /* Register holding maximum rowid */
133305 assert( pParse->db->aDb[iDb].pSchema!=0 );
133306 if( (pTab->tabFlags & TF_Autoincrement)!=0
133307 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
133311 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
133315 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
133319 || pSeqTab->nCol!=2
133321 pParse->nErr++;
133322 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
133326 pInfo = pToplevel->pAinc;
133327 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
133329 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
133331 testcase( pParse->earlyCleanup );
133332 if( pParse->db->mallocFailed ) return 0;
133333 pInfo->pNext = pToplevel->pAinc;
133334 pToplevel->pAinc = pInfo;
133335 pInfo->pTab = pTab;
133336 pInfo->iDb = iDb;
133337 pToplevel->nMem++; /* Register to hold name of table */
133338 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
133339 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
133341 memId = pInfo->regCtr;
133352 sqlite3 *db = pParse->db; /* The database connection */
133354 int memId; /* Register holding max rowid */
133355 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
133357 /* This routine is never called during trigger-generation. It is
133358 ** only called from the top-level */
133359 assert( pParse->pTriggerTab==0 );
133363 for(p = pParse->pAinc; p; p = p->pNext){
133364 static const int iLn = VDBE_OFFSET_LINENO(2);
133380 pDb = &db->aDb[p->iDb];
133381 memId = p->regCtr;
133382 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
133383 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
133384 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
133390 aOp[3].p1 = memId-1;
133399 if( pParse->nTab==0 ) pParse->nTab = 1;
133411 static void autoIncStep(Parse *pParse, int memId, int regRowid){
133413 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
133426 Vdbe *v = pParse->pVdbe;
133427 sqlite3 *db = pParse->db;
133430 for(p = pParse->pAinc; p; p = p->pNext){
133431 static const int iLn = VDBE_OFFSET_LINENO(2);
133440 Db *pDb = &db->aDb[p->iDb];
133441 int iRec;
133442 int memId = p->regCtr;
133445 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
133448 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
133453 aOp[2].p1 = memId-1;
133462 if( pParse->pAinc ) autoIncrementEnd(pParse);
133467 ** above are all no-ops
133475 ** that was able to use the co-routine optimization, finish coding the
133476 ** co-routine.
133479 if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){
133480 SrcItem *pItem = &pVal->pSrc->a[0];
133481 sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->regReturn);
133482 sqlite3VdbeJumpHere(pParse->pVdbe, pItem->addrFillSub - 1);
133487 ** Return true if all expressions in the expression-list passed as the
133490 static int exprListIsConstant(Parse *pParse, ExprList *pRow){
133491 int ii;
133492 for(ii=0; ii<pRow->nExpr; ii++){
133493 if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
133499 ** Return true if all expressions in the expression-list passed as the
133502 static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){
133503 int ii;
133505 for(ii=0; ii<pRow->nExpr; ii++){
133506 Expr *pExpr = pRow->a[ii].pExpr;
133507 assert( pExpr->op!=TK_RAISE );
133508 assert( pExpr->affExpr==0 );
133517 ** rows of a multi-row VALUES clause. Argument pLeft is the part of
133522 ** There are two ways in which this may be achieved - by incremental
133523 ** coding of a co-routine (the "co-routine" method) or by returning a
133531 ** When the co-routine method is used, each row that will be returned
133532 ** by the VALUES clause is coded into part of a co-routine as it is
133536 ** Select object to read co-routine
133539 ** The co-routine method is used in most cases. Exceptions are:
133547 ** This will not work, as the co-routine uses a hard-coded register
133554 ** a co-routine is not possible.
133556 ** c) There are non-constant expressions in the VALUES clause (e.g.
133557 ** the VALUES clause is part of a correlated sub-query).
133568 if( pParse->bHasWith /* condition (a) above */
133569 || pParse->db->init.busy /* condition (b) above */
133571 || (pLeft->pSrc->nSrc==0 &&
133572 exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
133575 /* The co-routine method cannot be used. Fall back to UNION ALL. */
133577 int f = SF_Values | SF_MultiValue;
133578 if( pLeft->pSrc->nSrc ){
133581 }else if( pLeft->pPrior ){
133583 f = (f & pLeft->selFlags);
133586 pLeft->selFlags &= ~SF_MultiValue;
133588 pSelect->op = TK_ALL;
133589 pSelect->pPrior = pLeft;
133593 SrcItem *p = 0; /* SrcItem that reads from co-routine */
133595 if( pLeft->pSrc->nSrc==0 ){
133596 /* Co-routine has not yet been started and the special Select object
133597 ** that accesses the co-routine has not yet been created. This block
133604 if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){
133610 pRet->pSrc->nSrc = 1;
133611 pRet->pPrior = pLeft->pPrior;
133612 pRet->op = pLeft->op;
133613 if( pRet->pPrior ) pRet->selFlags |= SF_Values;
133614 pLeft->pPrior = 0;
133615 pLeft->op = TK_SELECT;
133616 assert( pLeft->pNext==0 );
133617 assert( pRet->pNext==0 );
133618 p = &pRet->pSrc->a[0];
133619 p->pSelect = pLeft;
133620 p->fg.viaCoroutine = 1;
133621 p->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
133622 p->regReturn = ++pParse->nMem;
133623 p->iCursor = -1;
133624 p->u1.nRow = 2;
133625 sqlite3VdbeAddOp3(v,OP_InitCoroutine,p->regReturn,0,p->addrFillSub);
133626 sqlite3SelectDestInit(&dest, SRT_Coroutine, p->regReturn);
133628 /* Allocate registers for the output of the co-routine. Do so so
133630 ** used by the co-routine. This allows the code in sqlite3Insert()
133632 ** of the co-routine to a separate array for processing. */
133633 dest.iSdst = pParse->nMem + 3;
133634 dest.nSdst = pLeft->pEList->nExpr;
133635 pParse->nMem += 2 + dest.nSdst;
133637 pLeft->selFlags |= SF_MultiValue;
133639 p->regResult = dest.iSdst;
133640 assert( pParse->nErr || dest.iSdst>0 );
133644 p = &pLeft->pSrc->a[0];
133645 assert( !p->fg.isTabFunc && !p->fg.isIndexedBy );
133646 p->u1.nRow++;
133649 if( pParse->nErr==0 ){
133651 if( p->pSelect->pEList->nExpr!=pRow->nExpr ){
133652 sqlite3SelectWrongNumTermsError(pParse, p->pSelect);
133654 sqlite3ExprCodeExprList(pParse, pRow, p->regResult, 0, 0);
133655 sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, p->regReturn);
133658 sqlite3ExprListDelete(pParse->db, pRow);
133665 static int xferOptimization(
133669 int onError, /* How to handle constraint errors */
133670 int iDbDest /* The database of pDest */
133681 ** then a list of all (non-hidden) columns for the table is substituted.
133686 ** first two forms shown above. A VALUES clause is really just short-hand
133692 ** insert with data coming from a single-row VALUES clause, the code executes
133693 ** once straight down through. Pseudo-code follows (we call this
133705 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
133729 ** X <- A
133737 ** end-coroutine X
133750 ** X <- A
133758 ** end co-routine R
133775 int onError, /* How to handle constraint errors */
133780 int i, j; /* Loop counters */
133783 int nColumn; /* Number of columns in the data */
133784 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
133785 int iDataCur = 0; /* VDBE cursor that is the main data repository */
133786 int iIdxCur = 0; /* First index cursor */
133787 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
133788 int endOfLoop; /* Label for the end of the insertion loop */
133789 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
133790 int addrInsTop = 0; /* Jump to label "D" */
133791 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
133793 int iDb; /* Index of database holding TABLE */
133799 int iRegStore; /* Register in which to store next column */
133802 int regFromSelect = 0;/* Base register for data coming from SELECT */
133803 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
133804 int regRowCount = 0; /* Memory cell used for the row counter */
133805 int regIns; /* Block of regs holding rowid+data being inserted */
133806 int regRowid; /* registers holding insert rowid */
133807 int regData; /* register holding first column to insert */
133808 int *aRegIdx = 0; /* One register allocated to each index */
133811 int isView; /* True if attempting to insert into a view */
133813 int tmask; /* Mask of trigger times */
133816 db = pParse->db;
133817 assert( db->pParse==pParse );
133818 if( pParse->nErr ){
133821 assert( db->mallocFailed==0 );
133828 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
133829 pList = pSelect->pEList;
133830 pSelect->pEList = 0;
133837 assert( pTabList->nSrc==1 );
133842 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133843 assert( iDb<db->nDb );
133844 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
133845 db->aDb[iDb].zDbSName) ){
133870 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
133876 ** ViewGetColumnNames() is a no-op if pTab is not a view.
133882 /* Cannot insert into a read-only table.
133892 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
133924 regRowid = regIns = pParse->nMem+1;
133925 pParse->nMem += pTab->nCol + 1;
133928 pParse->nMem++;
133941 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
133942 ** loop, if ipkColumn==(-1), that means that integer primary key
133951 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
133953 assert( pColumn->eU4!=EU4_EXPR );
133954 pColumn->eU4 = EU4_IDX;
133955 for(i=0; i<pColumn->nId; i++){
133956 pColumn->a[i].u4.idx = -1;
133958 for(i=0; i<pColumn->nId; i++){
133959 for(j=0; j<pTab->nCol; j++){
133960 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
133961 pColumn->a[i].u4.idx = j;
133963 if( j==pTab->iPKey ){
133967 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
133970 pTab->aCol[j].zCnName);
133977 if( j>=pTab->nCol ){
133978 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
133983 pTabList->a, pColumn->a[i].zName);
133984 pParse->checkSchema = 1;
133992 ** is coming from a SELECT statement, then generate a co-routine that
133994 ** co-routine is the common header to the 3rd and 4th templates.
133997 /* Data is coming from a SELECT or from a multi-row VALUES clause.
133998 ** Generate a co-routine to run the SELECT. */
133999 int rc; /* Result code */
134001 if( pSelect->pSrc->nSrc==1
134002 && pSelect->pSrc->a[0].fg.viaCoroutine
134003 && pSelect->pPrior==0
134005 SrcItem *pItem = &pSelect->pSrc->a[0];
134006 dest.iSDParm = pItem->regReturn;
134007 regFromSelect = pItem->regResult;
134008 nColumn = pItem->pSelect->pEList->nExpr;
134010 if( bIdListInOrder && nColumn==pTab->nCol ){
134012 regRowid = regData - 1;
134013 regIns = regRowid - (IsVirtual(pTab) ? 1 : 0);
134016 int addrTop; /* Top of the co-routine */
134017 int regYield = ++pParse->nMem;
134022 dest.nSdst = pTab->nCol;
134025 assert( db->pParse==pParse );
134026 if( rc || pParse->nErr ) goto insert_cleanup;
134027 assert( db->mallocFailed==0 );
134029 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
134030 assert( pSelect->pEList );
134031 nColumn = pSelect->pEList->nExpr;
134058 int regRec; /* Register to hold packed record */
134059 int regTempRowid; /* Register to hold temp table ROWID */
134060 int addrL; /* Label "L" */
134062 srcTab = pParse->nTab++;
134077 ** single-row VALUES clause
134082 srcTab = -1;
134085 nColumn = pList->nExpr;
134099 ipkColumn = pTab->iPKey;
134101 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
134102 testcase( pTab->tabFlags & TF_HasVirtual );
134103 testcase( pTab->tabFlags & TF_HasStored );
134104 for(i=ipkColumn-1; i>=0; i--){
134105 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
134106 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
134107 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
134108 ipkColumn--;
134120 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
134121 for(i=0; i<pTab->nCol; i++){
134122 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
134125 if( nColumn!=(pTab->nCol-nHidden) ){
134128 pTabList->a, pTab->nCol-nHidden, nColumn);
134132 if( pColumn!=0 && nColumn!=pColumn->nId ){
134133 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
134139 if( (db->flags & SQLITE_CountRows)!=0
134140 && !pParse->nested
134141 && !pParse->pTriggerTab
134142 && !pParse->bReturning
134144 regRowCount = ++pParse->nMem;
134150 int nIdx;
134151 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
134153 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
134157 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
134159 aRegIdx[i] = ++pParse->nMem;
134160 pParse->nMem += pIdx->nColumn;
134162 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
134169 pTab->zName);
134176 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
134179 pTabList->a[0].iCursor = iDataCur;
134182 pNx->pUpsertSrc = pTabList;
134183 pNx->regData = regData;
134184 pNx->iDataCur = iDataCur;
134185 pNx->iIdxCur = iIdxCur;
134186 if( pNx->pUpsertTarget ){
134191 pNx = pNx->pNextUpsert;
134219 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
134223 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
134225 ** the value does not get overwritten by a NULL at tag-20191021-002. */
134239 for(i=0; i<pTab->nCol; i++, iRegStore++){
134240 int k;
134243 if( i==pTab->iPKey ){
134244 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
134247 ** NULL - we cannot optimize further by skipping the column completely */
134251 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
134257 iRegStore--;
134261 ** triggers, the slots used for stored columns will be OP_Copy-ed
134272 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134278 assert( pColumn->eU4==EU4_IDX );
134279 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
134280 if( j>=pColumn->nId ){
134284 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134292 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134296 k = i - nHidden;
134306 Expr *pX = pList->a[k].pExpr;
134307 int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
134320 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
134326 ** not happened yet) so we substitute a rowid of -1
134329 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
134331 int addr1;
134337 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
134340 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
134346 assert( pTab->nNVCol>0 || pParse->nErr>0 );
134347 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
134354 if( pTab->tabFlags & TF_HasGenerated ){
134355 testcase( pTab->tabFlags & TF_HasVirtual );
134356 testcase( pTab->tabFlags & TF_HasStored );
134372 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
134374 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
134387 /* Rowid already initialized at tag-20191021-001 */
134389 Expr *pIpk = pList->a[ipkColumn].pExpr;
134390 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
134394 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
134401 int addr1;
134425 if( pTab->tabFlags & TF_HasGenerated ){
134437 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
134443 int isReplace = 0;/* Set to true if constraints may cause a replace */
134444 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
134448 if( db->flags & SQLITE_ForeignKeys ){
134466 }else if( pParse->bReturning ){
134468 ** constant value -1, in case one or more of the returned expressions
134470 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
134483 pTab, regData-2-pTab->nCol, onError, endOfLoop);
134500 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
134501 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
134515 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
134551 ** Meanings of bits in of pWalker->eCode for
134558 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
134562 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
134563 if( pExpr->op==TK_COLUMN ){
134564 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
134565 if( pExpr->iColumn>=0 ){
134566 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
134567 pWalker->eCode |= CKCNSTRNT_COLUMN;
134570 pWalker->eCode |= CKCNSTRNT_ROWID;
134577 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
134586 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
134587 ** The operation of this routine is the same - return true if an only if
134591 SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
134593 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
134594 int chngRowid /* True if UPDATE changes the rowid */
134615 ** the indexes of a table in the order provided in the Table->pIndex list.
134616 ** However, sometimes (rarely - when there is an upsert) it wants to visit
134627 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
134628 int i; /* Index of the current item from the list */
134634 int nIdx; /* Size of the array */
134645 int ix; /* Which entry in the original Table.pIndex list is this index*/
134649 static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
134650 assert( pIter->i==0 );
134651 if( pIter->eType ){
134652 *pIx = pIter->u.ax.aIdx[0].ix;
134653 return pIter->u.ax.aIdx[0].p;
134656 return pIter->u.lx.pIdx;
134661 static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
134662 if( pIter->eType ){
134663 int i = ++pIter->i;
134664 if( i>=pIter->u.ax.nIdx ){
134668 *pIx = pIter->u.ax.aIdx[i].ix;
134669 return pIter->u.ax.aIdx[i].p;
134672 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
134673 return pIter->u.lx.pIdx;
134683 ** pTab->nCol+1 registers in this range. The first register (the one
134710 ** at pTab->pIndex.
134712 ** (2019-05-07) The generated code also creates a new record for the
134714 ** register identified by aRegIdx[nIdx] - in other words in the first
134725 ** for the first index in the pTab->pIndex list. Cursors for other indices
134726 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
134734 ** --------------- ---------- ----------------------------------------
134764 ** Or if overrideError==OE_Default, then the pParse->onError parameter
134765 ** is used. Or if pParse->onError==OE_Default then the onError value
134771 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
134772 int iDataCur, /* Canonical data cursor (main table or PK index) */
134773 int iIdxCur, /* First index cursor */
134774 int regNewData, /* First register in a range holding values to insert */
134775 int regOldData, /* Previous content. 0 for INSERTs */
134776 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
134778 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
134779 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
134780 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
134787 int i; /* loop counter */
134788 int ix; /* Index loop counter */
134789 int nCol; /* Number of columns */
134790 int onError; /* Conflict resolution strategy */
134791 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
134792 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
134796 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
134797 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
134798 int ipkTop = 0; /* Top of the IPK uniqueness check */
134799 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
134802 int regTrigCnt; /* Register used to count replace trigger invocations */
134803 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
134804 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
134806 int nReplaceTrig = 0; /* Number of replace triggers coded */
134810 db = pParse->db;
134811 v = pParse->pVdbe;
134814 nCol = pTab->nCol;
134825 nPkField = pPk->nKeyCol;
134834 if( pTab->tabFlags & TF_HasNotNull ){
134835 int b2ndPass = 0; /* True if currently running 2nd pass */
134836 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
134837 int nGenerated = 0; /* Number of generated columns with NOT NULL */
134840 int iReg; /* Register holding column value */
134841 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
134842 int isGenerated; /* non-zero if column is generated */
134843 onError = pCol->notNull;
134845 if( i==pTab->iPKey ){
134848 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
134864 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
134866 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
134867 testcase( pCol->colFlags & COLFLAG_STORED );
134868 testcase( pCol->colFlags & COLFLAG_GENERATED );
134882 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
134884 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
134896 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
134897 pCol->zCnName);
134898 testcase( zMsg==0 && db->mallocFailed==0 );
134923 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
134931 } /* end of 2-pass loop */
134932 } /* end if( has-not-null-constraints ) */
134937 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
134938 ExprList *pCheck = pTab->pCheck;
134939 pParse->iSelfTab = -(regNewData+1);
134941 for(i=0; i<pCheck->nExpr; i++){
134942 int allOk;
134944 Expr *pExpr = pCheck->a[i].pExpr;
134959 if( !db->mallocFailed ){
134966 char *zName = pCheck->a[i].zEName;
134967 assert( zName!=0 || pParse->db->mallocFailed );
134968 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
134975 pParse->iSelfTab = 0;
134992 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
135010 sIdxIter.u.lx.pIdx = pTab->pIndex;
135012 if( pUpsert->pUpsertTarget==0 ){
135013 /* There is just on ON CONFLICT clause and it has no constraint-target */
135014 assert( pUpsert->pNextUpsert==0 );
135015 if( pUpsert->isDoUpdate==0 ){
135016 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
135024 }else if( pTab->pIndex!=0 ){
135028 int nIdx, jj;
135032 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
135041 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
135042 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
135043 if( pTerm->pUpsertTarget==0 ) break;
135044 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
135046 pIdx = pTab->pIndex;
135047 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
135048 pIdx = pIdx->pNext;
135057 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
135087 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
135093 if( db->flags&SQLITE_RecTriggers ){
135103 regTrigCnt = ++pParse->nMem;
135115 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
135118 onError = pTab->keyConf;
135129 if( pUpsertClause->isDoUpdate==0 ){
135150 && pTab->pIndex /* There exist other constraints */
135189 ** recursive-triggers flag is set, call GenerateRowDelete() to
135191 ** the triggers and remove both the table and index b-tree entries.
135193 ** Otherwise, if there are no triggers or the recursive-triggers
135195 ** GenerateRowIndexDelete(). This removes the index b-tree entries
135196 ** only. The table b-tree entry will be replaced by the new entry
135213 regNewData, 1, 0, OE_Replace, 1, -1);
135219 /* This OP_Delete opcode fires the pre-update-hook only. It does
135220 ** not modify the b-tree. It is more efficient to let the coming
135226 if( pTab->pIndex ){
135228 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
135251 sqlite3VdbeJumpHere(v, ipkTop-1);
135266 int regIdx; /* Range of registers holding content for pIdx */
135267 int regR; /* Range of registers holding conflicting PK */
135268 int iThisCur; /* Cursor for this UNIQUE index */
135269 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
135270 int addrConflictCk; /* First opcode in the conflict check logic */
135284 VdbeNoopComment((v, "prep index %s", pIdx->zName));
135289 if( pIdx->pPartIdxWhere ){
135291 pParse->iSelfTab = -(regNewData+1);
135292 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
135294 pParse->iSelfTab = 0;
135301 for(i=0; i<pIdx->nColumn; i++){
135302 int iField = pIdx->aiColumn[i];
135303 int x;
135305 pParse->iSelfTab = -(regNewData+1);
135306 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
135307 pParse->iSelfTab = 0;
135308 VdbeComment((v, "%s column %d", pIdx->zName, i));
135309 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
135317 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
135320 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
135321 VdbeComment((v, "for %s", pIdx->zName));
135323 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
135324 sqlite3SetMakeRecordP5(v, pIdx->pTable);
135327 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
135339 onError = pIdx->onError;
135352 if( pUpsertClause->isDoUpdate==0 ){
135367 ** must be explicitly deleted in order to ensure any pre-update hook
135371 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
135374 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
135376 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
135377 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
135388 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
135396 ** is different from old-rowid */
135403 int x;
135405 ** store it in registers regR..regR+nPk-1 */
135407 for(i=0; i<pPk->nKeyCol; i++){
135408 assert( pPk->aiColumn[i]>=0 );
135409 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
135411 VdbeComment((v, "%s.%s", pTab->zName,
135412 pTab->aCol[pPk->aiColumn[i]].zCnName));
135423 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
135424 int op = OP_Ne;
135425 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
135427 for(i=0; i<pPk->nKeyCol; i++){
135428 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
135429 x = pPk->aiColumn[i];
135431 if( i==(pPk->nKeyCol-1) ){
135472 int nConflictCk; /* Number of opcodes in conflict check logic */
135475 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
135476 assert( nConflictCk>0 || db->mallocFailed );
135493 int addrBypass; /* Jump destination to bypass recheck logic */
135504 if( pIdx->pPartIdxWhere ){
135507 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
135511 ** the constraint-ok jump destination to be the address of
135520 int p2; /* New P2 value for copied conflict check opcode */
135532 nConflictCk--;
135586 int regRec = aRegIdx[ix];
135587 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
135601 ** to be the number of columns in table pTab that must not be NULL-trimmed.
135603 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
135609 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
135610 if( pTab->pSchema->file_format<2 ) return;
135612 for(i=pTab->nCol-1; i>0; i--){
135613 if( pTab->aCol[i].iDflt!=0 ) break;
135614 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
135623 ** PK index. This function adds code to invoke the pre-update hook,
135630 int iCur, /* Cursor number for table */
135631 int regData /* Data containing new record */
135633 Vdbe *v = pParse->pVdbe;
135634 int r = sqlite3GetTempReg(pParse);
135636 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
135658 int iDataCur, /* Cursor of the canonical data source */
135659 int iIdxCur, /* First index cursor */
135660 int regNewData, /* Range of content */
135661 int *aRegIdx, /* Register used by each index. 0 for unused indices */
135662 int update_flags, /* True for UPDATE, False for INSERT */
135663 int appendBias, /* True if this is likely to be an append */
135664 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
135669 int i; /* Loop counter */
135676 v = pParse->pVdbe;
135679 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
135681 assert( pIdx->onError!=OE_Replace
135682 || pIdx->pNext==0
135683 || pIdx->pNext->onError==OE_Replace );
135685 if( pIdx->pPartIdxWhere ){
135699 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
135703 if( pParse->nested ){
135716 if( !pParse->nested ){
135732 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
135738 ** pTab->pIndex list.
135740 ** If pTab is a virtual table, then this routine is a no-op and the
135743 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
135746 int op, /* OP_OpenRead or OP_OpenWrite */
135748 int iBase, /* Use this for the table cursor, if there is one */
135750 int *piDataCur, /* Write the database source cursor number here */
135751 int *piIdxCur /* Write the first index cursor number here */
135753 int i;
135754 int iDb;
135755 int iDataCur;
135764 /* This routine is a no-op for virtual tables. Leave the output
135767 *piDataCur = *piIdxCur = -999;
135770 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
135771 v = pParse->pVdbe;
135773 if( iBase<0 ) iBase = pParse->nTab;
135778 }else if( pParse->db->noSharedCache==0 ){
135779 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
135782 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
135783 int iIdxCur = iBase++;
135784 assert( pIdx->pSchema==pTab->pSchema );
135790 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
135793 VdbeComment((v, "%s", pIdx->zName));
135796 if( iBase>pParse->nTab ) pParse->nTab = iBase;
135805 ** purposes only - to make sure the transfer optimization really
135808 SQLITE_API int sqlite3_xferopt_count;
135824 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
135825 int i;
135827 assert( pDest->pTable!=pSrc->pTable );
135828 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
135831 if( pDest->onError!=pSrc->onError ){
135834 for(i=0; i<pSrc->nKeyCol; i++){
135835 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
135838 if( pSrc->aiColumn[i]==XN_EXPR ){
135839 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
135840 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
135841 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
135845 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
135848 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
135852 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
135870 ** There are lots of rules for determining compatibility - see comments
135875 ** is empty - a factor that can only be determined at run-time. In that
135885 static int xferOptimization(
135889 int onError, /* How to handle constraint errors */
135890 int iDbDest /* The database of pDest */
135892 sqlite3 *db = pParse->db;
135896 SrcItem *pItem; /* An element of pSelect->pSrc */
135897 int i; /* Loop counter */
135898 int iDbSrc; /* The database of pSrc */
135899 int iSrc, iDest; /* Cursors from source and destination */
135900 int addr1, addr2; /* Loop addresses */
135901 int emptyDestTest = 0; /* Address of test for empty pDest */
135902 int emptySrcTest = 0; /* Address of test for empty pSrc */
135904 int regAutoinc; /* Memory register used by AUTOINC */
135905 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
135906 int regData, regRowid; /* Registers holding data and rowid */
135909 if( pParse->pWith || pSelect->pWith ){
135921 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
135924 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
135925 if( pSelect->pSrc->nSrc!=1 ){
135928 if( pSelect->pSrc->a[0].pSelect ){
135931 if( pSelect->pWhere ){
135934 if( pSelect->pOrderBy ){
135939 if( pSelect->pGroupBy ){
135942 if( pSelect->pLimit ){
135945 if( pSelect->pPrior ){
135948 if( pSelect->selFlags & SF_Distinct ){
135951 pEList = pSelect->pEList;
135953 if( pEList->nExpr!=1 ){
135956 assert( pEList->a[0].pExpr );
135957 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
135965 pItem = pSelect->pSrc->a;
135970 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
135980 if( pDest->nCol!=pSrc->nCol ){
135983 if( pDest->iPKey!=pSrc->iPKey ){
135986 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
135987 return 0; /* Cannot feed from a non-strict into a strict table */
135989 for(i=0; i<pDest->nCol; i++){
135990 Column *pDestCol = &pDest->aCol[i];
135991 Column *pSrcCol = &pSrc->aCol[i];
135993 if( (db->mDbFlags & DBFLAG_Vacuum)==0
135994 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
136016 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
136017 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
136018 return 0; /* Both columns have the same generated-column type */
136024 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
136027 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
136028 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
136029 testcase( pDestCol->colFlags & COLFLAG_STORED );
136034 if( pDestCol->affinity!=pSrcCol->affinity ){
136041 if( pDestCol->notNull && !pSrcCol->notNull ){
136045 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
136048 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
136050 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
136053 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
136054 pSrcExpr->u.zToken)!=0)
136060 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136064 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
136070 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
136075 return 0; /* Corrupt schema - two indexes on the same btree */
136079 if( pDest->pCheck
136080 && (db->mDbFlags & DBFLAG_Vacuum)==0
136081 && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1)
136095 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
136099 if( (db->flags & SQLITE_CountRows)!=0 ){
136110 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
136113 iSrc = pParse->nTab++;
136114 iDest = pParse->nTab++;
136121 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
136122 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
136151 if( pDest->iPKey>=0 ){
136153 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136161 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
136165 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
136168 if( db->mDbFlags & DBFLAG_Vacuum ){
136175 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136184 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136185 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
136193 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
136194 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
136196 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136198 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
136202 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
136204 VdbeComment((v, "%s", pSrcIdx->zName));
136205 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
136208 VdbeComment((v, "%s", pDestIdx->zName));
136210 if( db->mDbFlags & DBFLAG_Vacuum ){
136215 ** order. In this case, instead of seeking within the b-tree as part
136217 ** OP_IdxInsert to seek to the point within the b-tree where each key
136225 for(i=0; i<pSrcIdx->nColumn; i++){
136226 const char *zColl = pSrcIdx->azColl[i];
136229 if( i==pSrcIdx->nColumn ){
136234 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
136239 if( (db->mDbFlags & DBFLAG_Vacuum)==0
136299 SQLITE_API int sqlite3_exec(
136306 int rc = SQLITE_OK; /* Return code */
136310 int callbackIsInit; /* True if callback data is initialized */
136315 sqlite3_mutex_enter(db->mutex);
136318 int nCol = 0;
136322 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
136328 /* this happens for a comment or white-space */
136335 int i;
136341 && db->flags&SQLITE_NullCallback)) ){
136368 /* EVIDENCE-OF: R-38229-40159 If the callback function to
136369 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
136407 assert( (rc&db->errMask)==rc );
136408 sqlite3_mutex_leave(db->mutex);
136466 void * (*aggregate_context)(sqlite3_context*,int nBytes);
136467 int (*aggregate_count)(sqlite3_context*);
136468 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
136469 int (*bind_double)(sqlite3_stmt*,int,double);
136470 int (*bind_int)(sqlite3_stmt*,int,int);
136471 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
136472 int (*bind_null)(sqlite3_stmt*,int);
136473 int (*bind_parameter_count)(sqlite3_stmt*);
136474 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
136475 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
136476 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
136477 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
136478 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
136479 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
136480 int (*busy_timeout)(sqlite3*,int ms);
136481 int (*changes)(sqlite3*);
136482 int (*close)(sqlite3*);
136483 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
136484 int eTextRep,const char*));
136485 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
136486 int eTextRep,const void*));
136487 const void * (*column_blob)(sqlite3_stmt*,int iCol);
136488 int (*column_bytes)(sqlite3_stmt*,int iCol);
136489 int (*column_bytes16)(sqlite3_stmt*,int iCol);
136490 int (*column_count)(sqlite3_stmt*pStmt);
136491 const char * (*column_database_name)(sqlite3_stmt*,int);
136492 const void * (*column_database_name16)(sqlite3_stmt*,int);
136493 const char * (*column_decltype)(sqlite3_stmt*,int i);
136494 const void * (*column_decltype16)(sqlite3_stmt*,int);
136495 double (*column_double)(sqlite3_stmt*,int iCol);
136496 int (*column_int)(sqlite3_stmt*,int iCol);
136497 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
136498 const char * (*column_name)(sqlite3_stmt*,int);
136499 const void * (*column_name16)(sqlite3_stmt*,int);
136500 const char * (*column_origin_name)(sqlite3_stmt*,int);
136501 const void * (*column_origin_name16)(sqlite3_stmt*,int);
136502 const char * (*column_table_name)(sqlite3_stmt*,int);
136503 const void * (*column_table_name16)(sqlite3_stmt*,int);
136504 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
136505 const void * (*column_text16)(sqlite3_stmt*,int iCol);
136506 int (*column_type)(sqlite3_stmt*,int iCol);
136507 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
136508 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
136509 int (*complete)(const char*sql);
136510 int (*complete16)(const void*sql);
136511 int (*create_collation)(sqlite3*,const char*,int,void*,
136512 int(*)(void*,int,const void*,int,const void*));
136513 int (*create_collation16)(sqlite3*,const void*,int,void*,
136514 int(*)(void*,int,const void*,int,const void*));
136515 int (*create_function)(sqlite3*,const char*,int,int,void*,
136516 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
136517 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
136519 int (*create_function16)(sqlite3*,const void*,int,int,void*,
136520 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
136521 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
136523 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
136524 int (*data_count)(sqlite3_stmt*pStmt);
136526 int (*declare_vtab)(sqlite3*,const char*);
136527 int (*enable_shared_cache)(int);
136528 int (*errcode)(sqlite3*db);
136531 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
136532 int (*expired)(sqlite3_stmt*);
136533 int (*finalize)(sqlite3_stmt*pStmt);
136536 int (*get_autocommit)(sqlite3*);
136537 void * (*get_auxdata)(sqlite3_context*,int);
136538 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
136539 int (*global_recover)(void);
136543 int (*libversion_number)(void);
136544 void *(*malloc)(int);
136546 int (*open)(const char*,sqlite3**);
136547 int (*open16)(const void*,sqlite3**);
136548 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
136549 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
136551 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
136552 void *(*realloc)(void*,int);
136553 int (*reset)(sqlite3_stmt*pStmt);
136554 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
136556 void (*result_error)(sqlite3_context*,const char*,int);
136557 void (*result_error16)(sqlite3_context*,const void*,int);
136558 void (*result_int)(sqlite3_context*,int);
136561 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
136562 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
136563 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
136564 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
136567 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
136569 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
136570 char * (*xsnprintf)(int,char*,const char*,...);
136571 int (*step)(sqlite3_stmt*);
136572 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
136573 char const**,char const**,int*,int*,int*);
136575 int (*total_changes)(sqlite3*);
136577 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
136578 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
136582 int (*value_bytes)(sqlite3_value*);
136583 int (*value_bytes16)(sqlite3_value*);
136585 int (*value_int)(sqlite3_value*);
136587 int (*value_numeric_type)(sqlite3_value*);
136592 int (*value_type)(sqlite3_value*);
136595 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
136597 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
136598 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
136599 int (*clear_bindings)(sqlite3_stmt*);
136601 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
136604 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
136605 int (*blob_bytes)(sqlite3_blob*);
136606 int (*blob_close)(sqlite3_blob*);
136607 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
136608 int,sqlite3_blob**);
136609 int (*blob_read)(sqlite3_blob*,void*,int,int);
136610 int (*blob_write)(sqlite3_blob*,const void*,int,int);
136611 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
136612 int(*)(void*,int,const void*,int,const void*),
136614 int (*file_control)(sqlite3*,const char*,int,void*);
136615 sqlite3_int64 (*memory_highwater)(int);
136617 sqlite3_mutex *(*mutex_alloc)(int);
136621 int (*mutex_try)(sqlite3_mutex*);
136622 int (*open_v2)(const char*,sqlite3**,int,const char*);
136623 int (*release_memory)(int);
136626 int (*sleep)(int);
136627 void (*soft_heap_limit)(int);
136629 int (*vfs_register)(sqlite3_vfs*,int);
136630 int (*vfs_unregister)(sqlite3_vfs*);
136631 int (*xthreadsafe)(void);
136632 void (*result_zeroblob)(sqlite3_context*,int);
136633 void (*result_error_code)(sqlite3_context*,int);
136634 int (*test_control)(int, ...);
136635 void (*randomness)(int,void*);
136637 int (*extended_result_codes)(sqlite3*,int);
136638 int (*limit)(sqlite3*,int,int);
136641 int (*status)(int,int*,int*,int);
136642 int (*backup_finish)(sqlite3_backup*);
136644 int (*backup_pagecount)(sqlite3_backup*);
136645 int (*backup_remaining)(sqlite3_backup*);
136646 int (*backup_step)(sqlite3_backup*,int);
136647 const char *(*compileoption_get)(int);
136648 int (*compileoption_used)(const char*);
136649 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
136650 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
136651 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
136654 int (*db_config)(sqlite3*,int,...);
136656 int (*db_status)(sqlite3*,int,int*,int*,int);
136657 int (*extended_errcode)(sqlite3*);
136658 void (*log)(int,const char*,...);
136661 int (*stmt_status)(sqlite3_stmt*,int,int);
136662 int (*strnicmp)(const char*,const char*,int);
136663 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
136664 int (*wal_autocheckpoint)(sqlite3*,int);
136665 int (*wal_checkpoint)(sqlite3*,const char*);
136666 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
136667 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
136668 int (*vtab_config)(sqlite3*,int op,...);
136669 int (*vtab_on_conflict)(sqlite3*);
136671 int (*close_v2)(sqlite3*);
136673 int (*db_readonly)(sqlite3*,const char*);
136674 int (*db_release_memory)(sqlite3*);
136675 const char *(*errstr)(int);
136676 int (*stmt_busy)(sqlite3_stmt*);
136677 int (*stmt_readonly)(sqlite3_stmt*);
136678 int (*stricmp)(const char*,const char*);
136679 int (*uri_boolean)(const char*,const char*,int);
136682 char *(*xvsnprintf)(int,char*,const char*,va_list);
136683 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
136685 int (*auto_extension)(void(*)(void));
136686 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
136688 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
136690 int (*cancel_auto_extension)(void(*)(void));
136691 int (*load_extension)(sqlite3*,const char*,const char*,char**);
136700 int (*strglob)(const char*,const char*);
136704 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
136705 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
136707 unsigned int (*value_subtype)(sqlite3_value*);
136708 void (*result_subtype)(sqlite3_context*,unsigned int);
136710 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
136711 int (*strlike)(const char*,const char*,unsigned int);
136712 int (*db_cacheflush)(sqlite3*);
136714 int (*system_errno)(sqlite3*);
136716 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
136721 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
136723 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
136725 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
136728 int (*vtab_nochange)(sqlite3_context*);
136729 int (*value_nochange)(sqlite3_value*);
136730 const char *(*vtab_collation)(sqlite3_index_info*,int);
136732 int (*keyword_count)(void);
136733 int (*keyword_name)(int,const char**,int*);
136734 int (*keyword_check)(const char*,int);
136739 void (*str_append)(sqlite3_str*, const char *zIn, int N);
136741 void (*str_appendchar)(sqlite3_str*, int N, char C);
136743 int (*str_errcode)(sqlite3_str*);
136744 int (*str_length)(sqlite3_str*);
136747 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
136748 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
136751 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
136756 int (*stmt_isexplain)(sqlite3_stmt*);
136757 int (*value_frombind)(sqlite3_value*);
136759 int (*drop_modules)(sqlite3*,const char**);
136762 const char *(*uri_key)(const char*,int);
136768 int,const char**);
136772 int (*txn_state)(sqlite3*,const char*);
136777 int (*autovacuum_pages)(sqlite3*,
136778 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
136781 int (*error_offset)(sqlite3*);
136782 int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
136783 int (*vtab_distinct)(sqlite3_index_info*);
136784 int (*vtab_in)(sqlite3_index_info*,int,int);
136785 int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
136786 int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
136788 int (*deserialize)(sqlite3*,const char*,unsigned char*,
136791 unsigned int);
136792 const char *(*db_name)(sqlite3*,int);
136794 int (*value_encoding)(sqlite3_value*);
136796 int (*is_interrupted)(sqlite3*);
136798 int (*stmt_explain)(sqlite3_stmt*,int);
136801 int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
136808 typedef int (*sqlite3_loadext_entry)(
136819 ** (part of the main SQLite library - not an extension) so that
136826 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
136828 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
136830 #define sqlite3_bind_blob sqlite3_api->bind_blob
136831 #define sqlite3_bind_double sqlite3_api->bind_double
136832 #define sqlite3_bind_int sqlite3_api->bind_int
136833 #define sqlite3_bind_int64 sqlite3_api->bind_int64
136834 #define sqlite3_bind_null sqlite3_api->bind_null
136835 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
136836 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
136837 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
136838 #define sqlite3_bind_text sqlite3_api->bind_text
136839 #define sqlite3_bind_text16 sqlite3_api->bind_text16
136840 #define sqlite3_bind_value sqlite3_api->bind_value
136841 #define sqlite3_busy_handler sqlite3_api->busy_handler
136842 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
136843 #define sqlite3_changes sqlite3_api->changes
136844 #define sqlite3_close sqlite3_api->close
136845 #define sqlite3_collation_needed sqlite3_api->collation_needed
136846 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
136847 #define sqlite3_column_blob sqlite3_api->column_blob
136848 #define sqlite3_column_bytes sqlite3_api->column_bytes
136849 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
136850 #define sqlite3_column_count sqlite3_api->column_count
136851 #define sqlite3_column_database_name sqlite3_api->column_database_name
136852 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
136853 #define sqlite3_column_decltype sqlite3_api->column_decltype
136854 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
136855 #define sqlite3_column_double sqlite3_api->column_double
136856 #define sqlite3_column_int sqlite3_api->column_int
136857 #define sqlite3_column_int64 sqlite3_api->column_int64
136858 #define sqlite3_column_name sqlite3_api->column_name
136859 #define sqlite3_column_name16 sqlite3_api->column_name16
136860 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
136861 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
136862 #define sqlite3_column_table_name sqlite3_api->column_table_name
136863 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
136864 #define sqlite3_column_text sqlite3_api->column_text
136865 #define sqlite3_column_text16 sqlite3_api->column_text16
136866 #define sqlite3_column_type sqlite3_api->column_type
136867 #define sqlite3_column_value sqlite3_api->column_value
136868 #define sqlite3_commit_hook sqlite3_api->commit_hook
136869 #define sqlite3_complete sqlite3_api->complete
136870 #define sqlite3_complete16 sqlite3_api->complete16
136871 #define sqlite3_create_collation sqlite3_api->create_collation
136872 #define sqlite3_create_collation16 sqlite3_api->create_collation16
136873 #define sqlite3_create_function sqlite3_api->create_function
136874 #define sqlite3_create_function16 sqlite3_api->create_function16
136875 #define sqlite3_create_module sqlite3_api->create_module
136876 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
136877 #define sqlite3_data_count sqlite3_api->data_count
136878 #define sqlite3_db_handle sqlite3_api->db_handle
136879 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
136880 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
136881 #define sqlite3_errcode sqlite3_api->errcode
136882 #define sqlite3_errmsg sqlite3_api->errmsg
136883 #define sqlite3_errmsg16 sqlite3_api->errmsg16
136884 #define sqlite3_exec sqlite3_api->exec
136886 #define sqlite3_expired sqlite3_api->expired
136888 #define sqlite3_finalize sqlite3_api->finalize
136889 #define sqlite3_free sqlite3_api->free
136890 #define sqlite3_free_table sqlite3_api->free_table
136891 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
136892 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
136893 #define sqlite3_get_table sqlite3_api->get_table
136895 #define sqlite3_global_recover sqlite3_api->global_recover
136897 #define sqlite3_interrupt sqlite3_api->interruptx
136898 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
136899 #define sqlite3_libversion sqlite3_api->libversion
136900 #define sqlite3_libversion_number sqlite3_api->libversion_number
136901 #define sqlite3_malloc sqlite3_api->malloc
136902 #define sqlite3_mprintf sqlite3_api->mprintf
136903 #define sqlite3_open sqlite3_api->open
136904 #define sqlite3_open16 sqlite3_api->open16
136905 #define sqlite3_prepare sqlite3_api->prepare
136906 #define sqlite3_prepare16 sqlite3_api->prepare16
136907 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
136908 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
136909 #define sqlite3_profile sqlite3_api->profile
136910 #define sqlite3_progress_handler sqlite3_api->progress_handler
136911 #define sqlite3_realloc sqlite3_api->realloc
136912 #define sqlite3_reset sqlite3_api->reset
136913 #define sqlite3_result_blob sqlite3_api->result_blob
136914 #define sqlite3_result_double sqlite3_api->result_double
136915 #define sqlite3_result_error sqlite3_api->result_error
136916 #define sqlite3_result_error16 sqlite3_api->result_error16
136917 #define sqlite3_result_int sqlite3_api->result_int
136918 #define sqlite3_result_int64 sqlite3_api->result_int64
136919 #define sqlite3_result_null sqlite3_api->result_null
136920 #define sqlite3_result_text sqlite3_api->result_text
136921 #define sqlite3_result_text16 sqlite3_api->result_text16
136922 #define sqlite3_result_text16be sqlite3_api->result_text16be
136923 #define sqlite3_result_text16le sqlite3_api->result_text16le
136924 #define sqlite3_result_value sqlite3_api->result_value
136925 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
136926 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
136927 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
136928 #define sqlite3_snprintf sqlite3_api->xsnprintf
136929 #define sqlite3_step sqlite3_api->step
136930 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
136931 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
136932 #define sqlite3_total_changes sqlite3_api->total_changes
136933 #define sqlite3_trace sqlite3_api->trace
136935 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
136937 #define sqlite3_update_hook sqlite3_api->update_hook
136938 #define sqlite3_user_data sqlite3_api->user_data
136939 #define sqlite3_value_blob sqlite3_api->value_blob
136940 #define sqlite3_value_bytes sqlite3_api->value_bytes
136941 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
136942 #define sqlite3_value_double sqlite3_api->value_double
136943 #define sqlite3_value_int sqlite3_api->value_int
136944 #define sqlite3_value_int64 sqlite3_api->value_int64
136945 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
136946 #define sqlite3_value_text sqlite3_api->value_text
136947 #define sqlite3_value_text16 sqlite3_api->value_text16
136948 #define sqlite3_value_text16be sqlite3_api->value_text16be
136949 #define sqlite3_value_text16le sqlite3_api->value_text16le
136950 #define sqlite3_value_type sqlite3_api->value_type
136951 #define sqlite3_vmprintf sqlite3_api->vmprintf
136952 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
136953 #define sqlite3_overload_function sqlite3_api->overload_function
136954 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
136955 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
136956 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
136957 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
136958 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
136959 #define sqlite3_blob_close sqlite3_api->blob_close
136960 #define sqlite3_blob_open sqlite3_api->blob_open
136961 #define sqlite3_blob_read sqlite3_api->blob_read
136962 #define sqlite3_blob_write sqlite3_api->blob_write
136963 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
136964 #define sqlite3_file_control sqlite3_api->file_control
136965 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
136966 #define sqlite3_memory_used sqlite3_api->memory_used
136967 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
136968 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
136969 #define sqlite3_mutex_free sqlite3_api->mutex_free
136970 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
136971 #define sqlite3_mutex_try sqlite3_api->mutex_try
136972 #define sqlite3_open_v2 sqlite3_api->open_v2
136973 #define sqlite3_release_memory sqlite3_api->release_memory
136974 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
136975 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
136976 #define sqlite3_sleep sqlite3_api->sleep
136977 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
136978 #define sqlite3_vfs_find sqlite3_api->vfs_find
136979 #define sqlite3_vfs_register sqlite3_api->vfs_register
136980 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
136981 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
136982 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
136983 #define sqlite3_result_error_code sqlite3_api->result_error_code
136984 #define sqlite3_test_control sqlite3_api->test_control
136985 #define sqlite3_randomness sqlite3_api->randomness
136986 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
136987 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
136988 #define sqlite3_limit sqlite3_api->limit
136989 #define sqlite3_next_stmt sqlite3_api->next_stmt
136990 #define sqlite3_sql sqlite3_api->sql
136991 #define sqlite3_status sqlite3_api->status
136992 #define sqlite3_backup_finish sqlite3_api->backup_finish
136993 #define sqlite3_backup_init sqlite3_api->backup_init
136994 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
136995 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
136996 #define sqlite3_backup_step sqlite3_api->backup_step
136997 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
136998 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
136999 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
137000 #define sqlite3_db_config sqlite3_api->db_config
137001 #define sqlite3_db_mutex sqlite3_api->db_mutex
137002 #define sqlite3_db_status sqlite3_api->db_status
137003 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
137004 #define sqlite3_log sqlite3_api->log
137005 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
137006 #define sqlite3_sourceid sqlite3_api->sourceid
137007 #define sqlite3_stmt_status sqlite3_api->stmt_status
137008 #define sqlite3_strnicmp sqlite3_api->strnicmp
137009 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
137010 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
137011 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
137012 #define sqlite3_wal_hook sqlite3_api->wal_hook
137013 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
137014 #define sqlite3_vtab_config sqlite3_api->vtab_config
137015 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
137017 #define sqlite3_close_v2 sqlite3_api->close_v2
137018 #define sqlite3_db_filename sqlite3_api->db_filename
137019 #define sqlite3_db_readonly sqlite3_api->db_readonly
137020 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
137021 #define sqlite3_errstr sqlite3_api->errstr
137022 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
137023 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
137024 #define sqlite3_stricmp sqlite3_api->stricmp
137025 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
137026 #define sqlite3_uri_int64 sqlite3_api->uri_int64
137027 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
137028 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
137029 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
137031 #define sqlite3_auto_extension sqlite3_api->auto_extension
137032 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
137033 #define sqlite3_bind_text64 sqlite3_api->bind_text64
137034 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
137035 #define sqlite3_load_extension sqlite3_api->load_extension
137036 #define sqlite3_malloc64 sqlite3_api->malloc64
137037 #define sqlite3_msize sqlite3_api->msize
137038 #define sqlite3_realloc64 sqlite3_api->realloc64
137039 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
137040 #define sqlite3_result_blob64 sqlite3_api->result_blob64
137041 #define sqlite3_result_text64 sqlite3_api->result_text64
137042 #define sqlite3_strglob sqlite3_api->strglob
137044 #define sqlite3_value_dup sqlite3_api->value_dup
137045 #define sqlite3_value_free sqlite3_api->value_free
137046 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
137047 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
137049 #define sqlite3_value_subtype sqlite3_api->value_subtype
137050 #define sqlite3_result_subtype sqlite3_api->result_subtype
137052 #define sqlite3_status64 sqlite3_api->status64
137053 #define sqlite3_strlike sqlite3_api->strlike
137054 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
137056 #define sqlite3_system_errno sqlite3_api->system_errno
137058 #define sqlite3_trace_v2 sqlite3_api->trace_v2
137059 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
137061 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
137063 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
137064 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
137065 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
137066 #define sqlite3_result_pointer sqlite3_api->result_pointer
137067 #define sqlite3_value_pointer sqlite3_api->value_pointer
137069 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
137070 #define sqlite3_value_nochange sqlite3_api->value_nochange
137071 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
137073 #define sqlite3_keyword_count sqlite3_api->keyword_count
137074 #define sqlite3_keyword_name sqlite3_api->keyword_name
137075 #define sqlite3_keyword_check sqlite3_api->keyword_check
137076 #define sqlite3_str_new sqlite3_api->str_new
137077 #define sqlite3_str_finish sqlite3_api->str_finish
137078 #define sqlite3_str_appendf sqlite3_api->str_appendf
137079 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
137080 #define sqlite3_str_append sqlite3_api->str_append
137081 #define sqlite3_str_appendall sqlite3_api->str_appendall
137082 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
137083 #define sqlite3_str_reset sqlite3_api->str_reset
137084 #define sqlite3_str_errcode sqlite3_api->str_errcode
137085 #define sqlite3_str_length sqlite3_api->str_length
137086 #define sqlite3_str_value sqlite3_api->str_value
137088 #define sqlite3_create_window_function sqlite3_api->create_window_function
137090 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
137092 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
137093 #define sqlite3_value_frombind sqlite3_api->value_frombind
137095 #define sqlite3_drop_modules sqlite3_api->drop_modules
137097 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
137098 #define sqlite3_uri_key sqlite3_api->uri_key
137099 #define sqlite3_filename_database sqlite3_api->filename_database
137100 #define sqlite3_filename_journal sqlite3_api->filename_journal
137101 #define sqlite3_filename_wal sqlite3_api->filename_wal
137103 #define sqlite3_create_filename sqlite3_api->create_filename
137104 #define sqlite3_free_filename sqlite3_api->free_filename
137105 #define sqlite3_database_file_object sqlite3_api->database_file_object
137107 #define sqlite3_txn_state sqlite3_api->txn_state
137109 #define sqlite3_changes64 sqlite3_api->changes64
137110 #define sqlite3_total_changes64 sqlite3_api->total_changes64
137112 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
137114 #define sqlite3_error_offset sqlite3_api->error_offset
137115 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
137116 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
137117 #define sqlite3_vtab_in sqlite3_api->vtab_in
137118 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
137119 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
137122 #define sqlite3_deserialize sqlite3_api->deserialize
137123 #define sqlite3_serialize sqlite3_api->serialize
137125 #define sqlite3_db_name sqlite3_api->db_name
137127 #define sqlite3_value_encoding sqlite3_api->value_encoding
137129 #define sqlite3_is_interrupted sqlite3_api->is_interrupted
137131 #define sqlite3_stmt_explain sqlite3_api->stmt_explain
137133 #define sqlite3_get_clientdata sqlite3_api->get_clientdata
137134 #define sqlite3_set_clientdata sqlite3_api->set_clientdata
137147 # define SQLITE_EXTENSION_INIT1 /*no-op*/
137149 # define SQLITE_EXTENSION_INIT3 /*no-op*/
137679 static int sqlite3LoadExtension(
137685 sqlite3_vfs *pVfs = db->pVfs;
137693 int ii;
137694 int rc;
137717 if( (db->flags & SQLITE_LoadExtension)==0 ){
137726 /* tag-20210611-1. Some dlopen() implementations will segfault if given
137731 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
137766 int iFile, iEntry, c;
137767 int ncFile = sqlite3Strlen30(zFile);
137774 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
137792 sqlite3_snprintf((int)nMsg, zErrmsg,
137794 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
137813 /* Append the new shared library handle to the db->aExtension array. */
137814 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
137818 if( db->nExtension>0 ){
137819 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
137821 sqlite3DbFree(db, db->aExtension);
137822 db->aExtension = aHandle;
137824 db->aExtension[db->nExtension++] = handle;
137833 sqlite3_snprintf((int)nMsg, zErrmsg,
137835 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
137840 SQLITE_API int sqlite3_load_extension(
137846 int rc;
137847 sqlite3_mutex_enter(db->mutex);
137850 sqlite3_mutex_leave(db->mutex);
137859 int i;
137860 assert( sqlite3_mutex_held(db->mutex) );
137861 for(i=0; i<db->nExtension; i++){
137862 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
137864 sqlite3DbFree(db, db->aExtension);
137871 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
137875 sqlite3_mutex_enter(db->mutex);
137877 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
137879 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
137881 sqlite3_mutex_leave(db->mutex);
137902 ** we have to locate the state vector at run-time. In the more common
137920 SQLITE_API int sqlite3_auto_extension(
137923 int rc = SQLITE_OK;
137965 ** routine is a no-op.
137970 SQLITE_API int sqlite3_cancel_auto_extension(
137976 int i;
137977 int n = 0;
137983 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
137985 wsdAutoext.nExt--;
138022 int go = 1;
138023 int rc;
138151 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
138155 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
138157 /* Names of columns for pragmas that return multi-column result
138158 ** or that return single-column results where the name of the
138225 /* Definitions of all built-in pragmas */
138760 ** the following macro or to the actual analysis_limit if it is non-zero,
138763 ** The value of 2000 is chosen emperically so that the worst-case run-time
138765 ** of test databases on a RaspberryPI-4 compiled using -Os and without
138766 ** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
138767 ** this paragraph, "worst-case" means that ANALYZE ends up being
138774 ** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
138796 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
138803 int i, n;
138834 static int getLockingMode(const char *z){
138844 ** Interpret the given string as an auto-vacuum mode value.
138849 static int getAutoVacuum(const char *z){
138850 int i;
138862 ** backed temporary databases, 2 for the Red-Black tree in memory database
138863 ** and 0 to use the compile-time default.
138865 static int getTempStore(const char *z){
138867 return z[0] - '0';
138883 static int invalidateTempStorage(Parse *pParse){
138884 sqlite3 *db = pParse->db;
138885 if( db->aDb[1].pBt!=0 ){
138886 if( !db->autoCommit
138887 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
138893 sqlite3BtreeClose(db->aDb[1].pBt);
138894 db->aDb[1].pBt = 0;
138907 static int changeTempStorage(Parse *pParse, const char *zStorageType){
138908 int ts = getTempStore(zStorageType);
138909 sqlite3 *db = pParse->db;
138910 if( db->temp_store==ts ) return SQLITE_OK;
138914 db->temp_store = (u8)ts;
138926 u8 n = pPragma->nPragCName;
138929 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
138931 int i, j;
138932 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
138966 if( db->autoCommit ){
138967 Db *pDb = db->aDb;
138968 int n = db->nDb;
138974 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
138975 while( (n--) > 0 ){
138976 if( pDb->pBt ){
138977 sqlite3BtreeSetPagerFlags(pDb->pBt,
138978 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
138985 # define setAllPagerFlags(X) /* no-op */
138990 ** Return a human-readable name for a constraint resolution action.
139011 ** journal-mode name.
139013 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
139036 int upr, lwr, mid = 0, rc;
139038 upr = ArraySize(aPragmaName)-1;
139044 upr = mid - 1;
139059 int isBuiltin, /* True if this is a built-in function */
139060 int showInternFuncs /* True if showing internal functions */
139070 for(; p; p=p->pNext){
139079 if( p->xSFunc==0 ) continue;
139080 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
139085 if( p->xValue!=0 ){
139087 }else if( p->xFinalize!=0 ){
139093 p->zName, isBuiltin,
139094 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
139095 p->nArg,
139096 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
139105 ** Generate code to output a single-column result row with a value of the
139109 static int integrityCheckResultRow(Vdbe *v){
139110 int addr;
139138 int minusFlag /* True if a '-' sign preceded <value> */
139140 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
139141 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
139145 int iDb; /* Database index for <database> */
139146 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
139147 sqlite3 *db = pParse->db; /* The database connection */
139154 pParse->nMem = 2;
139160 pDb = &db->aDb[iDb];
139172 zRight = sqlite3MPrintf(db, "-%T", pValue);
139178 zDb = pId2->n>0 ? pDb->zDbSName : 0;
139183 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
139185 ** handled the pragma and generate a no-op prepared statement.
139187 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
139192 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
139202 db->busyHandler.nBusy = 0;
139216 pParse->nErr++;
139217 pParse->rc = rc;
139224 /* IMP: R-43042-22504 No error messages are generated if an
139230 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
139235 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
139236 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
139242 switch( pPragma->ePragTyp ){
139262 static const int iLn = VDBE_OFFSET_LINENO(2);
139277 pParse->nMem += 2;
139285 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
139289 pDb->pSchema->cache_size = size;
139290 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
139307 Btree *pBt = pDb->pBt;
139310 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
139313 /* Malloc may fail when setting the page-size, as there is an internal
139316 db->nextPagesize = sqlite3Atoi(zRight);
139317 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
139333 Btree *pBt = pDb->pBt;
139334 int b = -1;
139343 if( pId2->n==0 && b>=0 ){
139344 int ii;
139345 for(ii=0; ii<db->nDb; ii++){
139346 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
139372 int iReg;
139375 iReg = ++pParse->nMem;
139385 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
139397 int eMode = getLockingMode(zRight);
139399 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
139402 ** the locking-mode of the main database).
139404 eMode = db->dfltLockMode;
139407 if( pId2->n==0 ){
139409 ** of the PRAGMA command. In this case the locking-mode must be
139416 int ii;
139417 assert(pDb==&db->aDb[0]);
139418 for(ii=2; ii<db->nDb; ii++){
139419 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
139422 db->dfltLockMode = (u8)eMode;
139424 pPager = sqlite3BtreePager(pDb->pBt);
139443 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
139444 int ii; /* Loop counter */
139452 int n = sqlite3Strlen30(zRight);
139461 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
139462 /* Do not allow journal-mode "OFF" in defensive since the database
139467 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
139470 pId2->n = 1;
139472 for(ii=db->nDb-1; ii>=0; ii--){
139473 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
139489 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139490 i64 iLimit = -2;
139493 if( iLimit<-1 ) iLimit = -1;
139506 ** Get or set the value of the database 'auto-vacuum' parameter.
139511 Btree *pBt = pDb->pBt;
139516 int eAuto = getAutoVacuum(zRight);
139518 db->nextAutovac = (u8)eAuto;
139520 ** incr-vacuum flags. This is required in case this connection
139522 ** as an auto-vacuum capable db.
139529 ** that this really is an auto-vacuum capable database.
139531 static const int iLn = VDBE_OFFSET_LINENO(2);
139540 int iAddr = sqlite3VdbeCurrentAddr(v);
139548 aOp[4].p3 = eAuto - 1;
139563 int iLimit = 0, addr;
139571 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
139587 ** number of pages is adjusted so that the cache uses -N kibibytes
139593 returnSingleInt(v, pDb->pSchema->cache_size);
139595 int size = sqlite3Atoi(zRight);
139596 pDb->pSchema->cache_size = size;
139597 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
139614 ** number of pages is adjusted so that the cache uses -N kibibytes
139628 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
139629 sqlite3BtreeSetSpillSize(pDb->pBt,0));
139631 int size = 1;
139633 sqlite3BtreeSetSpillSize(pDb->pBt, size);
139636 db->flags |= SQLITE_CacheSpill;
139638 db->flags &= ~(u64)SQLITE_CacheSpill;
139664 int ii;
139667 if( pId2->n==0 ) db->szMmap = sz;
139668 for(ii=db->nDb-1; ii>=0; ii--){
139669 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
139670 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
139674 sz = -1;
139683 pParse->nErr++;
139684 pParse->rc = rc;
139697 ** Note that it is possible for the library compile-time options to
139702 returnSingleInt(v, db->temp_store);
139726 int res;
139727 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
139735 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
139736 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
139773 int res;
139774 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
139805 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139812 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139814 int res;
139842 returnSingleInt(v, pDb->safety_level-1);
139844 if( !db->autoCommit ){
139848 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
139850 pDb->safety_level = iLevel;
139851 pDb->bSyncSet = 1;
139863 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
139865 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
139866 if( db->autoCommit==0 ){
139868 ** in auto-commit mode. */
139872 if( db->auth.authLevel==UAUTH_User ){
139873 /* Do not allow non-admin users to modify the schema arbitrarily */
139880 || (db->flags & SQLITE_Defensive)==0
139882 db->flags |= mask;
139885 db->flags &= ~mask;
139886 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
139890 /* IMP: R-60817-01178 If the argument is "RESET" then schema
139897 /* Many of the flag-pragmas modify the code generated by the SQL
139920 ** pk: Non-zero for PK fields.
139927 int i, k;
139928 int nHidden = 0;
139931 pParse->nMem = 7;
139933 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
139934 int isHidden = 0;
139936 if( pCol->colFlags & COLFLAG_NOINSERT ){
139937 if( pPragma->iArg==0 ){
139941 if( pCol->colFlags & COLFLAG_VIRTUAL ){
139943 }else if( pCol->colFlags & COLFLAG_STORED ){
139945 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
139949 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
139954 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
139957 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
139960 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
139961 i-nHidden,
139962 pCol->zCnName,
139964 pCol->notNull ? 1 : 0,
139965 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
139987 int ii;
139988 pParse->nMem = 6;
139990 for(ii=0; ii<db->nDb; ii++){
139993 int initNCol;
139994 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
140001 pHash = &db->aDb[ii].pSchema->tblHash;
140003 while( initNCol-- ){
140008 if( pTab->nCol==0 ){
140009 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
140012 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
140016 if( db->mallocFailed ){
140017 sqlite3ErrorMsg(db->pParse, "out of memory");
140018 db->pParse->rc = SQLITE_NOMEM_BKPT;
140020 pHash = &db->aDb[ii].pSchema->tblHash;
140029 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
140034 }else if( pTab->tabFlags & TF_Shadow ){
140040 db->aDb[ii].zDbSName,
140041 sqlite3PreferredTableName(pTab->zName),
140043 pTab->nCol,
140044 (pTab->tabFlags & TF_WithoutRowid)!=0,
140045 (pTab->tabFlags & TF_Strict)!=0
140056 pParse->nMem = 5;
140058 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
140061 sqlite3PreferredTableName(pTab->zName),
140063 pTab->szTabRow,
140064 pTab->nRowLogEst,
140065 pTab->tabFlags);
140066 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140068 pIdx->zName,
140069 pIdx->szIdxRow,
140070 pIdx->aiRowLogEst[0],
140071 pIdx->hasStat1);
140093 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
140094 int i;
140095 int mx;
140096 if( pPragma->iArg ){
140098 mx = pIdx->nColumn;
140099 pParse->nMem = 6;
140102 mx = pIdx->nKeyCol;
140103 pParse->nMem = 3;
140105 pTab = pIdx->pTable;
140107 assert( pParse->nMem<=pPragma->nPragCName );
140109 i16 cnum = pIdx->aiColumn[i];
140111 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
140112 if( pPragma->iArg ){
140114 pIdx->aSortOrder[i],
140115 pIdx->azColl[i],
140116 i<pIdx->nKeyCol);
140118 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
140127 int i;
140130 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140131 pParse->nMem = 5;
140133 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
140137 pIdx->zName,
140139 azOrigin[pIdx->idxType],
140140 pIdx->pPartIdxWhere!=0);
140147 int i;
140148 pParse->nMem = 3;
140149 for(i=0; i<db->nDb; i++){
140150 if( db->aDb[i].pBt==0 ) continue;
140151 assert( db->aDb[i].zDbSName!=0 );
140154 db->aDb[i].zDbSName,
140155 sqlite3BtreeGetFilename(db->aDb[i].pBt));
140161 int i = 0;
140163 pParse->nMem = 2;
140164 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
140166 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
140173 int i;
140176 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
140177 pParse->nMem = 6;
140179 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
140180 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
140184 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
140186 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
140195 pParse->nMem = 1;
140196 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
140198 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
140205 int i;
140221 pFK = pTab->u.tab.pFKey;
140223 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140224 int i = 0;
140225 pParse->nMem = 8;
140228 int j;
140229 for(j=0; j<pFK->nCol; j++){
140233 pFK->zTo,
140234 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
140235 pFK->aCol[j].zCol,
140236 actionName(pFK->aAction[1]), /* ON UPDATE */
140237 actionName(pFK->aAction[0]), /* ON DELETE */
140241 pFK = pFK->pNextFrom;
140256 int i; /* Loop counter: Foreign key number for pTab */
140257 int j; /* Loop counter: Field of the foreign key */
140259 int x; /* result variable */
140260 int regResult; /* 3 registers to hold a result row */
140261 int regRow; /* Registers to hold a row from pTab */
140262 int addrTop; /* Top of a loop checking foreign keys */
140263 int addrOk; /* Jump here if the key is OK */
140264 int *aiCols; /* child to parent column mapping */
140266 regResult = pParse->nMem+1;
140267 pParse->nMem += 4;
140268 regRow = ++pParse->nMem;
140269 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
140278 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
140279 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140280 zDb = db->aDb[iDb].zDbSName;
140282 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
140283 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
140285 sqlite3VdbeLoadString(v, regResult, pTab->zName);
140287 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
140288 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
140291 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
140297 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
140305 assert( pParse->nErr>0 || pFK==0 );
140307 if( pParse->nTab<i ) pParse->nTab = i;
140310 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
140311 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
140316 assert( x==0 || db->mallocFailed );
140324 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
140325 for(j=0; j<pFK->nCol; j++){
140326 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
140334 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
140335 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
140336 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
140339 int jmp = sqlite3VdbeCurrentAddr(v)+2;
140342 assert( pFK->nCol==1 || db->mallocFailed );
140351 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
140390 ** without the overhead of cross-checking indexes. Quick_check
140406 int i, j, addr, mxErr;
140409 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
140418 ** to -1 here, to indicate that the VDBE should verify the integrity
140421 assert( iDb==0 || pId2->z );
140422 if( pId2->z==0 ) iDb = -1;
140425 pParse->nMem = 6;
140430 if( sqlite3GetInt32(pValue->z, &mxErr) ){
140436 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
140439 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
140442 for(i=0; i<db->nDb; i++){
140445 int *aRoot; /* Array of root page numbers of all btrees */
140446 int cnt = 0; /* Number of entries in aRoot[] */
140452 pParse->okConstFactor = 0; /* tag-20230327-1 */
140454 /* Do an integrity check of the B-Tree
140460 pTbls = &db->aDb[i].pSchema->tblHash;
140464 int nIdx; /* Number of indexes on pTab */
140467 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
140471 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
140479 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
140480 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140481 aRoot[++cnt] = pIdx->tnum;
140490 /* Do the b-tree integrity checks */
140495 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
140505 int iTab = 0;
140513 for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){
140518 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140519 if( pIdx->pPartIdxWhere==0 ){
140522 sqlite3VdbeLoadString(v, 4, pIdx->zName);
140537 int loopTop;
140538 int iDataCur, iIdxCur;
140539 int r1 = -1;
140540 int bStrict; /* True for a STRICT table */
140541 int r2; /* Previous key for WITHOUT ROWID tables */
140542 int mxCol; /* Maximum non-virtual column number */
140551 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
140552 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
140557 ** reg[8+i] counts the number of entries in the i-th index
140560 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
140563 assert( pParse->nMem>=8+j );
140568 /* Fetch the right-most column from the table. This will cause
140575 mxCol = -1;
140576 for(j=0; j<pTab->nCol; j++){
140577 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
140579 if( mxCol==pTab->iPKey ) mxCol--;
140582 ** PK index column-count, so there is no need to account for them
140584 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
140594 int a1;
140596 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
140601 pTab->zName);
140606 for(j=0; j<pPk->nKeyCol; j++){
140614 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
140615 ** (3) Datatype for TEXT columns in non-STRICT tables must be
140617 ** (4) Datatype for numeric columns in non-STRICT tables must not
140620 bStrict = (pTab->tabFlags & TF_Strict)!=0;
140621 for(j=0; j<pTab->nCol; j++){
140623 Column *pCol = pTab->aCol + j; /* The column to be checked */
140624 int labelError; /* Jump here to report an error */
140625 int labelOk; /* Jump here if all looks ok */
140626 int p1, p3, p4; /* Operands to the OP_IsType opcode */
140627 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
140629 if( j==pTab->iPKey ) continue;
140631 doTypeCheck = pCol->eCType>COLTYPE_ANY;
140633 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
140635 if( pCol->notNull==0 && !doTypeCheck ) continue;
140639 if( pCol->colFlags & COLFLAG_VIRTUAL ){
140641 p1 = -1;
140644 if( pCol->iDflt ){
140647 pCol->affinity, &pDfltValue);
140665 if( pCol->notNull ){
140667 int jmp3;
140668 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
140671 sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */
140674 sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */
140684 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
140685 pCol->zCnName);
140696 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
140700 0x11, /* INT */
140706 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
140707 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
140709 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
140710 sqlite3StdType[pCol->eCType-1],
140711 pTab->zName, pTab->aCol[j].zCnName);
140713 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
140714 /* (3) Datatype for TEXT columns in non-STRICT tables must be
140720 pTab->zName, pTab->aCol[j].zCnName);
140722 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
140723 /* (4) Datatype for numeric columns in non-STRICT tables must not
140726 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
140732 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
140736 pTab->zName, pTab->aCol[j].zCnName);
140744 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
140745 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
140746 if( db->mallocFailed==0 ){
140747 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
140748 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
140750 int k;
140751 pParse->iSelfTab = iDataCur + 1;
140752 for(k=pCheck->nExpr-1; k>0; k--){
140753 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
140755 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
140758 pParse->iSelfTab = 0;
140760 pTab->zName);
140769 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
140770 int jmp2, jmp3, jmp4, jmp5, label6;
140771 int kk;
140772 int ckUniq = sqlite3VdbeMakeLabel(pParse);
140780 pIdx->nColumn); VdbeCoverage(v);
140785 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
140795 int jmp7;
140797 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
140800 "rowid not at end-of-record for row ");
140803 sqlite3VdbeGoto(v, jmp5-1);
140807 /* Any indexed columns with non-BINARY collations must still hold
140810 for(kk=0; kk<pIdx->nKeyCol; kk++){
140811 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
140817 int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto);
140822 sqlite3VdbeGoto(v, jmp5-1);
140830 int uniqOk = sqlite3VdbeMakeLabel(pParse);
140831 int jmp6;
140832 for(kk=0; kk<pIdx->nKeyCol; kk++){
140833 int iCol = pIdx->aiColumn[kk];
140834 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
140835 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
140843 pIdx->nKeyCol); VdbeCoverage(v);
140844 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
140853 sqlite3VdbeJumpHere(v, loopTop-1);
140856 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
140867 int a1;
140871 if( pTab->nCol<=0 ){
140872 const char *zMod = pTab->u.vtab.azArg[0];
140873 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
140876 if( pTab->u.vtab.p==0 ) continue;
140877 pVTab = pTab->u.vtab.p->pVtab;
140879 if( NEVER(pVTab->pModule==0) ) continue;
140880 if( pVTab->pModule->iVersion<4 ) continue;
140881 if( pVTab->pModule->xIntegrity==0 ) continue;
140883 pTab->nTabRef++;
140893 static const int iLn = VDBE_OFFSET_LINENO(2);
140907 aOp[0].p2 = 1-mxErr;
140913 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
140922 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
140927 ** The second form of this pragma is a no-op if the main database file
140948 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
140949 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
140950 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
140953 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
140963 returnSingleText(v, encnames[ENC(pParse->db)].zName);
140970 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
140971 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
140972 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
140973 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
140979 if( !pEnc->zName ){
141004 ** the value of the schema-version and user-version, respectively. Both
141005 ** the schema-version and the user-version are 32-bit signed integers
141008 ** The schema-cookie is usually only manipulated internally by SQLite. It
141015 ** the schema-version is potentially dangerous and may lead to program
141018 ** The user-version is not used internally by SQLite. It may be used by
141022 int iCookie = pPragma->iArg; /* Which cookie to read or write */
141024 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
141039 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
141041 ** mode. Change the OP_SetCookie opcode into a no-op. */
141068 ** Return the names of all compile-time options used in this build,
141072 int i = 0;
141074 pParse->nMem = 1;
141091 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
141092 int eMode = SQLITE_CHECKPOINT_PASSIVE;
141102 pParse->nMem = 3;
141121 db->xWalCallback==sqlite3WalDefaultHook ?
141122 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
141130 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
141163 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
141165 ** currently (2024-02-19) set to 2000, which is such that
141166 ** the worst case run-time for PRAGMA optimize on a 100MB
141168 ** a RaspberryPI-4 class machine. On by default.
141195 ** (4c) The query planner used sqlite_stat1-style statistics for one
141203 ** 10-fold. In other words, the current size of the table is
141213 int iDbLast; /* Loop termination point for the schema loop */
141214 int iTabCur; /* Cursor for a table whose size needs checking */
141222 int nLimit; /* Analysis limit to use */
141223 int nCheck = 0; /* Number of tables to be optimized */
141224 int nBtree = 0; /* Number of btrees to scan */
141225 int nIndex; /* Number of indexes on the current table */
141235 }else if( db->nAnalysisLimit>0
141236 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT ){
141241 iTabCur = pParse->nTab++;
141242 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
141245 pSchema = db->aDb[iDb].pSchema;
141246 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
141253 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue;
141256 ** If any index is unanalyzed, then the threshold is -1 to
141259 szThreshold = pTab->nRowLogEst;
141261 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141263 if( !pIdx->hasStat1 ){
141264 szThreshold = -1; /* Always analyze if any index lacks statistics */
141271 if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){
141275 }else if( pTab->pIndex!=0 && szThreshold<0 ){
141298 szThreshold>=iRange ? szThreshold-iRange : -1,
141307 db->aDb[iDb].zDbSName, pTab->zName);
141309 int r1 = sqlite3GetTempReg(pParse);
141321 ** the analysis_limit to avoid excess run-time in the worst case.
141323 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
141324 int iAddr, iEnd;
141347 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
141351 returnSingleInt(v, db->busyTimeout);
141359 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
141361 ** specified and is a non-negative integer.
141362 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
141364 ** sqlite3_soft_heap_limit64(-1) C-language function.
141371 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
141382 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
141389 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
141392 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
141409 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
141411 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
141425 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
141428 db->nAnalysisLimit = (int)(N&0x7fffffff);
141430 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
141442 int i;
141443 pParse->nMem = 2;
141444 for(i=0; i<db->nDb; i++){
141447 int j;
141448 if( db->aDb[i].zDbSName==0 ) continue;
141449 pBt = db->aDb[i].pBt;
141452 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
141456 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
141464 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
141473 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
141478 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
141510 static int pragmaVtabConnect(
141513 int argc, const char *const*argv,
141519 int rc;
141520 int i, j;
141529 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
141534 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
141538 if( pPragma->mPragFlg & PragFlg_Result1 ){
141542 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
141548 assert( strlen(zBuf) < sizeof(zBuf)-1 );
141556 pTab->pName = pPragma;
141557 pTab->db = db;
141558 pTab->iHidden = i;
141559 pTab->nHidden = j;
141572 static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
141585 static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
141588 int i, j;
141589 int seen[2];
141591 pIdxInfo->estimatedCost = (double)1;
141592 if( pTab->nHidden==0 ){ return SQLITE_OK; }
141593 pConstraint = pIdxInfo->aConstraint;
141596 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
141597 if( pConstraint->iColumn < pTab->iHidden ) continue;
141598 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
141599 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT;
141600 j = pConstraint->iColumn - pTab->iHidden;
141605 pIdxInfo->estimatedCost = (double)2147483647;
141606 pIdxInfo->estimatedRows = 2147483647;
141609 j = seen[0]-1;
141610 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
141611 pIdxInfo->aConstraintUsage[j].omit = 1;
141612 pIdxInfo->estimatedCost = (double)20;
141613 pIdxInfo->estimatedRows = 20;
141615 j = seen[1]-1;
141616 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
141617 pIdxInfo->aConstraintUsage[j].omit = 1;
141623 static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
141628 pCsr->base.pVtab = pVtab;
141629 *ppCursor = &pCsr->base;
141635 int i;
141636 sqlite3_finalize(pCsr->pPragma);
141637 pCsr->pPragma = 0;
141638 pCsr->iRowid = 0;
141639 for(i=0; i<ArraySize(pCsr->azArg); i++){
141640 sqlite3_free(pCsr->azArg[i]);
141641 pCsr->azArg[i] = 0;
141646 static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
141654 static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
141656 int rc = SQLITE_OK;
141659 pCsr->iRowid++;
141660 assert( pCsr->pPragma );
141661 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
141662 rc = sqlite3_finalize(pCsr->pPragma);
141663 pCsr->pPragma = 0;
141672 static int pragmaVtabFilter(
141674 int idxNum, const char *idxStr,
141675 int argc, sqlite3_value **argv
141678 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
141679 int rc;
141680 int i, j;
141687 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
141690 assert( j<ArraySize(pCsr->azArg) );
141691 assert( pCsr->azArg[j]==0 );
141693 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
141694 if( pCsr->azArg[j]==0 ){
141699 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
141701 if( pCsr->azArg[1] ){
141702 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
141704 sqlite3_str_appendall(&acc, pTab->pName->zName);
141705 if( pCsr->azArg[0] ){
141706 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
141710 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
141713 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
141722 static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
141724 return (pCsr->pPragma==0);
141730 static int pragmaVtabColumn(
141733 int i
141736 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
141737 if( i<pTab->iHidden ){
141738 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
141740 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
141748 static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
141750 *p = pCsr->iRowid;
141757 0, /* xCreate - create a table */
141758 pragmaVtabConnect, /* xConnect - connect to an existing table */
141759 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
141760 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
141761 0, /* xDestroy - Drop a table */
141762 pragmaVtabOpen, /* xOpen - open a cursor */
141763 pragmaVtabClose, /* xClose - close a cursor */
141764 pragmaVtabFilter, /* xFilter - configure scan constraints */
141765 pragmaVtabNext, /* xNext - advance a cursor */
141767 pragmaVtabColumn, /* xColumn - read data */
141768 pragmaVtabRowid, /* xRowid - read data */
141769 0, /* xUpdate - write data */
141770 0, /* xBegin - begin transaction */
141771 0, /* xSync - sync transaction */
141772 0, /* xCommit - commit transaction */
141773 0, /* xRollback - rollback transaction */
141774 0, /* xFindFunction - function overloading */
141775 0, /* xRename - rename the table */
141793 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
141794 assert( sqlite3HashFind(&db->aModule, zName)==0 );
141830 sqlite3 *db = pData->db;
141831 if( db->mallocFailed ){
141832 pData->rc = SQLITE_NOMEM_BKPT;
141833 }else if( pData->pzErrMsg[0]!=0 ){
141835 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
141841 *pData->pzErrMsg = sqlite3MPrintf(db,
141843 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
141846 pData->rc = SQLITE_ERROR;
141847 }else if( db->flags & SQLITE_WriteSchema ){
141848 pData->rc = SQLITE_CORRUPT_BKPT;
141853 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
141854 *pData->pzErrMsg = z;
141855 pData->rc = SQLITE_CORRUPT_BKPT;
141864 SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
141866 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
141867 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
141873 static int sqlite3Prepare(
141875 const char *zSql, /* UTF-8 encoded SQL statement. */
141876 int nBytes, /* Length of zSql in bytes. */
141898 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
141900 sqlite3 *db = pData->db;
141901 int iDb = pData->iDb;
141905 assert( sqlite3_mutex_held(db->mutex) );
141906 db->mDbFlags |= DBFLAG_EncodingFixed;
141908 pData->nInitRow++;
141909 if( db->mallocFailed ){
141914 assert( iDb>=0 && iDb<db->nDb );
141921 ** But because db->init.busy is set to 1, no VDBE code is generated
141930 int rc;
141931 u8 saved_iDb = db->init.iDb;
141933 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
141935 assert( db->init.busy );
141936 db->init.iDb = iDb;
141937 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
141938 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
141944 db->init.orphanTrigger = 0;
141945 db->init.azInit = (const char**)argv;
141947 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
141948 rc = db->errCode;
141950 db->init.iDb = saved_iDb;
141951 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
141953 if( db->init.orphanTrigger ){
141956 if( rc > pData->rc ) pData->rc = rc;
141964 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
141976 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
141980 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
141981 || pIndex->tnum<2
141982 || pIndex->tnum>pData->mxPage
142001 SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
142002 int rc;
142003 int i;
142005 int size;
142009 int meta[5];
142012 int openedTransaction = 0;
142013 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
142015 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
142016 assert( iDb>=0 && iDb<db->nDb );
142017 assert( db->aDb[iDb].pSchema );
142018 assert( sqlite3_mutex_held(db->mutex) );
142019 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
142021 db->init.busy = 1;
142023 /* Construct the in-memory representation schema tables (sqlite_schema or
142027 ** the schema table as read-only. */
142033 "rootpage int,sql text)";
142043 db->mDbFlags &= mask;
142051 pDb = &db->aDb[iDb];
142052 if( pDb->pBt==0 ){
142059 /* If there is not already a read-only (or read-write) transaction opened
142060 ** on the b-tree database, open one now. If a transaction is opened, it
142062 sqlite3BtreeEnter(pDb->pBt);
142063 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
142064 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
142079 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
142090 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
142092 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
142095 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
142097 /* If opening a non-empty database, check the text encoding. For the
142102 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
142103 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
142107 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
142112 if( db->nVdbeActive>0 && encoding!=ENC(db)
142113 && (db->mDbFlags & DBFLAG_Vacuum)==0
142122 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
142130 pDb->pSchema->enc = ENC(db);
142132 if( pDb->pSchema->cache_size==0 ){
142134 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
142136 pDb->pSchema->cache_size = size;
142138 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
142140 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
142146 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
142149 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
142150 if( pDb->pSchema->file_format==0 ){
142151 pDb->pSchema->file_format = 1;
142153 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
142164 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
142165 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
142170 assert( db->init.busy );
142171 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
142176 db->aDb[iDb].zDbSName, zSchemaTabName);
142180 xAuth = db->xAuth;
142181 db->xAuth = 0;
142185 db->xAuth = xAuth;
142196 assert( pDb == &(db->aDb[iDb]) );
142197 if( db->mallocFailed ){
142200 pDb = &db->aDb[iDb];
142202 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
142223 sqlite3BtreeCommit(pDb->pBt);
142225 sqlite3BtreeLeave(pDb->pBt);
142234 db->init.busy = 0;
142239 ** Initialize all database files - the main database file, the file
142247 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
142248 int i, rc;
142249 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
142251 assert( sqlite3_mutex_held(db->mutex) );
142252 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
142253 assert( db->init.busy==0 );
142255 assert( db->nDb>0 );
142262 for(i=db->nDb-1; i>0; i--){
142263 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
142276 ** This routine is a no-op if the database schema is already initialized.
142279 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
142280 int rc = SQLITE_OK;
142281 sqlite3 *db = pParse->db;
142282 assert( sqlite3_mutex_held(db->mutex) );
142283 if( !db->init.busy ){
142284 rc = sqlite3Init(db, &pParse->zErrMsg);
142286 pParse->rc = rc;
142287 pParse->nErr++;
142288 }else if( db->noSharedCache ){
142289 db->mDbFlags |= DBFLAG_SchemaKnownOk;
142298 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
142299 ** make no changes to pParse->rc.
142302 sqlite3 *db = pParse->db;
142303 int iDb;
142304 int rc;
142305 int cookie;
142307 assert( pParse->checkSchema );
142308 assert( sqlite3_mutex_held(db->mutex) );
142309 for(iDb=0; iDb<db->nDb; iDb++){
142310 int openedTransaction = 0; /* True if a transaction is opened */
142311 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
142314 /* If there is not already a read-only (or read-write) transaction opened
142315 ** on the b-tree database, open one now. If a transaction is opened, it
142316 ** will be closed immediately after reading the meta-value. */
142321 pParse->rc = SQLITE_NOMEM;
142328 ** value stored as part of the in-memory schema representation,
142332 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
142333 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
142346 ** which database file in db->aDb[] the schema refers to.
142351 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
142352 int i = -32768;
142354 /* If pSchema is NULL, then return -32768. This happens when code in
142356 ** created by a sub-select). In this case the return value of this
142359 ** We return -32768 instead of the more usual -1 simply because using
142360 ** -32768 as the incorrect index into db->aDb[] is much
142361 ** more likely to cause a segfault than -1 (of course there are assert()
142363 ** -32768 will still fit into a 16-bit signed integer.
142365 assert( sqlite3_mutex_held(db->mutex) );
142368 assert( i<db->nDb );
142369 if( db->aDb[i].pSchema==pSchema ){
142373 assert( i>=0 && i<db->nDb );
142382 sqlite3 *db = pParse->db;
142384 assert( db->pParse==pParse );
142385 assert( pParse->nested==0 );
142387 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
142389 while( pParse->pCleanup ){
142390 ParseCleanup *pCleanup = pParse->pCleanup;
142391 pParse->pCleanup = pCleanup->pNext;
142392 pCleanup->xCleanup(db, pCleanup->pPtr);
142395 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
142396 if( pParse->pConstExpr ){
142397 sqlite3ExprListDelete(db, pParse->pConstExpr);
142399 assert( db->lookaside.bDisable >= pParse->disableLookaside );
142400 db->lookaside.bDisable -= pParse->disableLookaside;
142401 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
142402 assert( pParse->db->pParse==pParse );
142403 db->pParse = pParse->pOuterParse;
142414 ** common cleanups, we save a single NULL-pointer comparison in
142419 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
142421 ** use-after-free errors following an OOM. The preferred way to do this is
142424 ** testcase( pParse->earlyCleanup );
142442 sqlite3OomFault(pParse->db);
142444 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
142447 pCleanup->pNext = pParse->pCleanup;
142448 pParse->pCleanup = pCleanup;
142449 pCleanup->pPtr = pPtr;
142450 pCleanup->xCleanup = xCleanup;
142452 xCleanup(pParse->db, pPtr);
142455 pParse->earlyCleanup = 1;
142473 assert( db->pParse!=pParse );
142474 pParse->pOuterParse = db->pParse;
142475 db->pParse = pParse;
142476 pParse->db = db;
142477 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
142489 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
142491 static int sqlite3Prepare(
142493 const char *zSql, /* UTF-8 encoded SQL statement. */
142494 int nBytes, /* Length of zSql in bytes. */
142500 int rc = SQLITE_OK; /* Result code */
142501 int i; /* Loop counter */
142507 sParse.pOuterParse = db->pParse;
142508 db->pParse = &sParse;
142517 if( db->mallocFailed ){
142519 db->errCode = rc = SQLITE_NOMEM;
142522 assert( sqlite3_mutex_held(db->mutex) );
142524 /* For a long-term use prepared statement avoid the use of
142535 ** some other database connection is holding a write-lock, which in
142556 if( !db->noSharedCache ){
142557 for(i=0; i<db->nDb; i++) {
142558 Btree *pBt = db->aDb[i].pBt;
142563 const char *zDb = db->aDb[i].zDbSName;
142565 testcase( db->flags & SQLITE_ReadUncommit );
142573 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
142576 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
142578 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
142589 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
142603 if( db->init.busy==0 ){
142604 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
142606 if( db->mallocFailed ){
142611 if( sParse.checkSchema && db->init.busy==0 ){
142636 sParse.pTriggerPrg = pT->pNext;
142645 static int sqlite3LockAndPrepare(
142647 const char *zSql, /* UTF-8 encoded SQL statement. */
142648 int nBytes, /* Length of zSql in bytes. */
142654 int rc;
142655 int cnt = 0;
142664 sqlite3_mutex_enter(db->mutex);
142672 if( rc==SQLITE_OK || db->mallocFailed ) break;
142674 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
142677 assert( (rc&db->errMask)==rc );
142678 db->busyHandler.nBusy = 0;
142679 sqlite3_mutex_leave(db->mutex);
142693 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
142694 int rc;
142700 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
142704 assert( sqlite3_mutex_held(db->mutex) );
142706 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
142732 SQLITE_API int sqlite3_prepare(
142734 const char *zSql, /* UTF-8 encoded SQL statement. */
142735 int nBytes, /* Length of zSql in bytes. */
142739 int rc;
142744 SQLITE_API int sqlite3_prepare_v2(
142746 const char *zSql, /* UTF-8 encoded SQL statement. */
142747 int nBytes, /* Length of zSql in bytes. */
142751 int rc;
142752 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
142762 SQLITE_API int sqlite3_prepare_v3(
142764 const char *zSql, /* UTF-8 encoded SQL statement. */
142765 int nBytes, /* Length of zSql in bytes. */
142766 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
142770 int rc;
142771 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
142788 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
142790 static int sqlite3Prepare16(
142792 const void *zSql, /* UTF-16 encoded SQL statement. */
142793 int nBytes, /* Length of zSql in bytes. */
142798 /* This function currently works by first transforming the UTF-16
142799 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
142804 int rc = SQLITE_OK;
142814 int sz;
142819 sqlite3_mutex_enter(db->mutex);
142822 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
142827 ** equivalent pointer into the UTF-16 string by counting the unicode
142829 ** the same number of characters into the UTF-16 string.
142831 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
142836 sqlite3_mutex_leave(db->mutex);
142848 SQLITE_API int sqlite3_prepare16(
142850 const void *zSql, /* UTF-16 encoded SQL statement. */
142851 int nBytes, /* Length of zSql in bytes. */
142855 int rc;
142860 SQLITE_API int sqlite3_prepare16_v2(
142862 const void *zSql, /* UTF-16 encoded SQL statement. */
142863 int nBytes, /* Length of zSql in bytes. */
142867 int rc;
142872 SQLITE_API int sqlite3_prepare16_v3(
142874 const void *zSql, /* UTF-16 encoded SQL statement. */
142875 int nBytes, /* Length of zSql in bytes. */
142876 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
142880 int rc;
142917 int tabTnct; /* Ephemeral table used for DISTINCT processing */
142918 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
142925 ** The aDefer[] array is used by the sorter-references optimization. For
142935 ** When the sorter-reference optimization is used, there is one entry in the
142942 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
142943 int iECursor; /* Cursor number for the sorter */
142944 int regReturn; /* Register holding block-output return address */
142945 int labelBkOut; /* Start label for the block-output subroutine */
142946 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
142947 int labelDone; /* Jump here when done, ex: LIMIT reached */
142948 int labelOBLopt; /* Jump here when sorter is full */
142954 int iCsr; /* Cursor number for table */
142955 int nKey; /* Number of PK columns for table pTab (>=1) */
142960 int addrPush; /* First instruction to push data into sorter */
142961 int addrPushEnd; /* Last instruction that pushes data into sorter */
142973 static void clearSelect(sqlite3 *db, Select *p, int bFree){
142976 Select *pPrior = p->pPrior;
142977 sqlite3ExprListDelete(db, p->pEList);
142978 sqlite3SrcListDelete(db, p->pSrc);
142979 sqlite3ExprDelete(db, p->pWhere);
142980 sqlite3ExprListDelete(db, p->pGroupBy);
142981 sqlite3ExprDelete(db, p->pHaving);
142982 sqlite3ExprListDelete(db, p->pOrderBy);
142983 sqlite3ExprDelete(db, p->pLimit);
142984 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
142986 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
142987 sqlite3WindowListDelete(db, p->pWinDefn);
142989 while( p->pWin ){
142990 assert( p->pWin->ppThis==&p->pWin );
142991 sqlite3WindowUnlinkFromSelect(p->pWin);
143003 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
143004 pDest->eDest = (u8)eDest;
143005 pDest->iSDParm = iParm;
143006 pDest->iSDParm2 = 0;
143007 pDest->zAffSdst = 0;
143008 pDest->iSdst = 0;
143009 pDest->nSdst = 0;
143020 SrcList *pSrc, /* the FROM clause -- which tables to scan */
143030 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
143032 assert( pParse->db->mallocFailed );
143037 sqlite3Expr(pParse->db,TK_ASTERISK,0));
143039 pNew->pEList = pEList;
143040 pNew->op = TK_SELECT;
143041 pNew->selFlags = selFlags;
143042 pNew->iLimit = 0;
143043 pNew->iOffset = 0;
143044 pNew->selId = ++pParse->nSelect;
143045 pNew->addrOpenEphm[0] = -1;
143046 pNew->addrOpenEphm[1] = -1;
143047 pNew->nSelectRow = 0;
143048 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
143049 pNew->pSrc = pSrc;
143050 pNew->pWhere = pWhere;
143051 pNew->pGroupBy = pGroupBy;
143052 pNew->pHaving = pHaving;
143053 pNew->pOrderBy = pOrderBy;
143054 pNew->pPrior = 0;
143055 pNew->pNext = 0;
143056 pNew->pLimit = pLimit;
143057 pNew->pWith = 0;
143059 pNew->pWin = 0;
143060 pNew->pWinDefn = 0;
143062 if( pParse->db->mallocFailed ) {
143063 clearSelect(pParse->db, pNew, pNew!=&standin);
143066 assert( pNew->pSrc!=0 || pParse->nErr>0 );
143083 ** Return a pointer to the right-most SELECT statement in a compound.
143086 while( p->pNext ) p = p->pNext;
143111 ** ------- ----- ----- ------------
143112 ** CROSS - - JT_CROSS
143113 ** INNER - - JT_INNER
143114 ** LEFT - - JT_LEFT|JT_OUTER
143115 ** LEFT OUTER - JT_LEFT|JT_OUTER
143116 ** RIGHT - - JT_RIGHT|JT_OUTER
143117 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
143118 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
143119 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
143120 ** NATURAL INNER - JT_NATURAL|JT_INNER
143121 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
143123 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
143125 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
143129 ** of other non-standard and in many cases nonsensical join types.
143134 ** INNER CROSS JOIN -> same as JOIN
143135 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
143136 ** OUTER LEFT JOIN -> same as LEFT JOIN
143137 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
143138 ** LEFT RIGHT JOIN -> same as FULL JOIN
143139 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
143140 ** CROSS CROSS CROSS JOIN -> same as JOIN
143153 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
143154 int jointype = 0;
143172 int i, j;
143179 if( p->n==aKeyword[j].nChar
143180 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
143208 ** Return the index of a column in a table. Return -1 if the column
143211 SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol){
143212 int i;
143215 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
143216 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
143218 return -1;
143224 SQLITE_PRIVATE void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
143226 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
143227 if( pItem->fg.isNestedFrom ){
143229 assert( pItem->pSelect!=0 );
143230 pResults = pItem->pSelect->pEList;
143232 assert( iCol>=0 && iCol<pResults->nExpr );
143233 pResults->a[iCol].fg.bUsed = 1;
143239 ** table that has a column named zCol. The search is left-to-right.
143247 static int tableAndColumnIndex(
143249 int iStart, /* First member of pSrc->a[] to check */
143250 int iEnd, /* Last member of pSrc->a[] to check */
143252 int *piTab, /* Write index of pSrc->a[] here */
143253 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
143254 int bIgnoreHidden /* Ignore hidden columns */
143256 int i; /* For looping over tables in pSrc */
143257 int iCol; /* Index of column matching zCol */
143259 assert( iEnd<pSrc->nSrc );
143264 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
143266 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
143269 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
143305 SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){
143311 p->w.iJoin = iTable;
143312 if( p->op==TK_FUNCTION ){
143314 if( p->x.pList ){
143315 int i;
143316 for(i=0; i<p->x.pList->nExpr; i++){
143317 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
143321 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
143322 p = p->pRight;
143340 static void unsetJoinExpr(Expr *p, int iTable, int nullable){
143342 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
143346 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
143349 if( p->op==TK_FUNCTION ){
143351 assert( p->pLeft==0 );
143352 if( p->x.pList ){
143353 int i;
143354 for(i=0; i<p->x.pList->nExpr; i++){
143355 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
143359 unsetJoinExpr(p->pLeft, iTable, nullable);
143360 p = p->pRight;
143377 ** The left most table is the first entry in Select.pSrc. The right-most
143385 static int sqlite3ProcessJoin(Parse *pParse, Select *p){
143387 int i, j; /* Loop counters */
143391 pSrc = p->pSrc;
143392 pLeft = &pSrc->a[0];
143394 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
143395 Table *pRightTab = pRight->pTab;
143398 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
143399 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
143404 if( pRight->fg.jointype & JT_NATURAL ){
143406 if( pRight->fg.isUsing || pRight->u3.pOn ){
143411 for(j=0; j<pRightTab->nCol; j++){
143414 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
143415 zName = pRightTab->aCol[j].zCnName;
143419 assert( pUsing->nId>0 );
143420 assert( pUsing->a[pUsing->nId-1].zName==0 );
143421 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
143426 pRight->fg.isUsing = 1;
143427 pRight->fg.isSynthUsing = 1;
143428 pRight->u3.pUsing = pUsing;
143430 if( pParse->nErr ) return 1;
143440 if( pRight->fg.isUsing ){
143441 IdList *pList = pRight->u3.pUsing;
143442 sqlite3 *db = pParse->db;
143444 for(j=0; j<pList->nId; j++){
143446 int iLeft; /* Table on the left with matching column name */
143447 int iLeftCol; /* Column number of matching column on the left */
143448 int iRightCol; /* Column number of matching column on the right */
143453 zName = pList->a[j].zName;
143457 pRight->fg.isSynthUsing)==0
143459 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
143464 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
143465 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143468 ** contains the zName column, then this branch is a no-op.
143475 ** non-USING references to zName on the left of an INNER or LEFT
143482 pRight->fg.isSynthUsing)!=0 ){
143483 if( pSrc->a[iLeft].fg.isUsing==0
143484 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
143492 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
143507 pEq->w.iJoin = pE2->iTable;
143509 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
143516 else if( pRight->u3.pOn ){
143517 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
143518 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
143519 pRight->u3.pOn = 0;
143520 pRight->fg.isOn = 1;
143532 int regResult; /* Store results in array of registers here */
143536 int regExtraResult; /* Where to load the extra columns */
143549 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
143550 0, pInfo->ecelFlags);
143552 if( pInfo->pExtra ){
143553 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
143554 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
143565 static int makeSorterRecord(
143569 int regBase,
143570 int nBase
143572 int nOBSat = pSort->nOBSat;
143573 Vdbe *v = pParse->pVdbe;
143574 int regOut = ++pParse->nMem;
143575 if( pSort->pDeferredRowLoad ){
143576 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
143578 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
143584 ** through regData+nData-1 onto the sorter.
143590 int regData, /* First register holding data to be sorted */
143591 int regOrigData, /* First register holding data before packing */
143592 int nData, /* Number of elements in the regData data array */
143593 int nPrefixReg /* No. of reg prior to regData available for use */
143595 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
143596 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
143597 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
143598 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
143599 int regBase; /* Regs for sorter record */
143600 int regRecord = 0; /* Assembled sorter record */
143601 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
143602 int op; /* Opcode to add sorter record to sorter */
143603 int iLimit; /* LIMIT counter */
143604 int iSkip = 0; /* End of the sorter insert loop */
143624 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
143629 regBase = regData - nPrefixReg;
143631 regBase = pParse->nMem + 1;
143632 pParse->nMem += nBase;
143634 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
143635 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
143636 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
143637 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
143640 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
143646 int regPrevKey; /* The first nOBSat columns of the previous row */
143647 int addrFirst; /* Address of the OP_IfNot opcode */
143648 int addrJmp; /* Address of the OP_Jump opcode */
143650 int nKey; /* Number of sorting key columns, including OP_Sequence */
143654 regPrevKey = pParse->nMem+1;
143655 pParse->nMem += pSort->nOBSat;
143656 nKey = nExpr - pSort->nOBSat + bSeq;
143660 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
143663 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
143664 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
143665 if( pParse->db->mallocFailed ) return;
143666 pOp->p2 = nKey + nData;
143667 pKI = pOp->p4.pKeyInfo;
143668 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
143669 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
143670 testcase( pKI->nAllField > pKI->nKeyField+2 );
143671 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
143672 pKI->nAllField-pKI->nKeyField-1);
143676 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
143677 pSort->regReturn = ++pParse->nMem;
143678 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
143679 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
143681 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
143685 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
143699 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
143704 int iCsr = pSort->iECursor;
143709 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
143716 if( pSort->sortFlags & SORTFLAG_UseSorter ){
143721 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
143722 regBase+nOBSat, nBase-nOBSat);
143725 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
143728 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
143737 int iOffset, /* Register holding the offset counter */
143738 int iContinue /* Jump here to skip the current record */
143789 static int codeDistinct(
143791 int eTnctType, /* WHERE_DISTINCT_* value */
143792 int iTab, /* A sorting index used to test for distinctness */
143793 int addrRepeat, /* Jump to here if not distinct */
143795 int regElem /* First element */
143797 int iRet = 0;
143798 int nResultCol = pEList->nExpr;
143799 Vdbe *v = pParse->pVdbe;
143803 int i;
143804 int iJump; /* Jump destination */
143805 int regPrev; /* Previous row content */
143808 iRet = regPrev = pParse->nMem+1;
143809 pParse->nMem += nResultCol;
143813 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
143814 if( i<nResultCol-1 ){
143821 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
143824 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
143825 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
143835 int r1 = sqlite3GetTempReg(pParse);
143860 ** No adjustments necessary. This function is a no-op.
143875 int eTnctType, /* WHERE_DISTINCT_* value */
143876 int iVal, /* Value returned by codeDistinct() */
143877 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
143879 if( pParse->nErr==0
143882 Vdbe *v = pParse->pVdbe;
143884 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
143893 pOp->opcode = OP_Null;
143894 pOp->p1 = 1;
143895 pOp->p2 = iVal;
143902 ** This function is called as part of inner-loop generation for a SELECT
143904 ** determines the expressions, if any, that the sorter-reference
143905 ** optimization should be used for. The sorter-reference optimization
143918 ** for which the sorter-reference optimization should be enabled.
143919 ** Additionally, the pSort->aDefer[] array is populated with entries
143931 int i;
143932 int nDefer = 0;
143934 for(i=0; i<pEList->nExpr; i++){
143935 struct ExprList_item *pItem = &pEList->a[i];
143936 if( pItem->u.x.iOrderByCol==0 ){
143937 Expr *pExpr = pItem->pExpr;
143939 if( pExpr->op==TK_COLUMN
143940 && pExpr->iColumn>=0
143942 && (pTab = pExpr->y.pTab)!=0
143944 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
143946 int j;
143948 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
143951 if( nDefer==ArraySize(pSort->aDefer) ){
143954 int nKey = 1;
143955 int k;
143959 nKey = pPk->nKeyCol;
143964 pNew->iTable = pExpr->iTable;
143966 pNew->y.pTab = pExpr->y.pTab;
143967 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
143971 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
143972 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
143973 pSort->aDefer[nDefer].nKey = nKey;
143977 pItem->fg.bSorterRef = 1;
143981 pSort->nDefer = (u8)nDefer;
143990 ** If srcTab is negative, then the p->pEList expressions
143992 ** zero or more, then data is pulled from srcTab and p->pEList is used only
143998 int srcTab, /* Pull data from this table if non-negative */
144002 int iContinue, /* Jump here to continue with next row */
144003 int iBreak /* Jump here to break out of the inner loop */
144005 Vdbe *v = pParse->pVdbe;
144006 int i;
144007 int hasDistinct; /* True if the DISTINCT keyword is present */
144008 int eDest = pDest->eDest; /* How to dispose of results */
144009 int iParm = pDest->iSDParm; /* First argument to disposal method */
144010 int nResultCol; /* Number of result columns */
144011 int nPrefixReg = 0; /* Number of extra registers before regResult */
144017 ** values for any expressions that are also part of the sort-key are omitted
144019 int regResult; /* Start of memory holding current results */
144020 int regOrig; /* Start of memory holding full result (or 0) */
144023 assert( p->pEList!=0 );
144024 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
144025 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
144028 codeOffset(v, p->iOffset, iContinue);
144033 nResultCol = p->pEList->nExpr;
144035 if( pDest->iSdst==0 ){
144037 nPrefixReg = pSort->pOrderBy->nExpr;
144038 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
144039 pParse->nMem += nPrefixReg;
144041 pDest->iSdst = pParse->nMem+1;
144042 pParse->nMem += nResultCol;
144043 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
144045 ** on the right-hand side of an INSERT contains more result columns than
144049 pParse->nMem += nResultCol;
144051 pDest->nSdst = nResultCol;
144052 regOrig = regResult = pDest->iSdst;
144056 VdbeComment((v, "%s", p->pEList->a[i].zEName));
144073 /* For each expression in p->pEList that is a copy of an expression in
144074 ** the ORDER BY clause (pSort->pOrderBy), set the associated
144076 ** expression within the sort-key that pushOntoSorter() will generate.
144077 ** This allows the p->pEList field to be omitted from the sorted record,
144081 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
144082 int j;
144083 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
144084 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
144088 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
144089 if( pExtra && pParse->db->mallocFailed==0 ){
144095 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
144096 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
144097 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
144098 pParse->nMem += pExtra->nExpr;
144104 pEList = p->pEList;
144105 for(i=0; i<pEList->nExpr; i++){
144106 if( pEList->a[i].u.x.iOrderByCol>0
144108 || pEList->a[i].fg.bSorterRef
144111 nResultCol--;
144130 if( pExtra ) nResultCol += pExtra->nExpr;
144132 if( p->iLimit
144138 pSort->pDeferredRowLoad = &sRowLoadInfo;
144150 int eType = pDistinct->eTnctType;
144151 int iTab = pDistinct->tabTnct;
144152 assert( nResultCol==p->pEList->nExpr );
144153 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
144154 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
144156 codeOffset(v, p->iOffset, iContinue);
144166 int r1;
144190 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
144197 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
144200 ** This does not affect operation in any way - it just allows MakeRecord
144202 if( eDest==SRT_Table && pDest->iSDParm2 ){
144213 int addr = sqlite3VdbeCurrentAddr(v) + 4;
144224 int r2 = sqlite3GetTempReg(pParse);
144239 int i2 = pDest->iSDParm2;
144240 int r1 = sqlite3GetTempReg(pParse);
144248 regResult+(i2<0), nResultCol-(i2<0), r1);
144272 int r1 = sqlite3GetTempReg(pParse);
144273 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
144275 r1, pDest->zAffSdst, nResultCol);
144297 assert( nResultCol<=pDest->nSdst );
144301 assert( nResultCol==pDest->nSdst );
144309 case SRT_Coroutine: /* Send data to a co-routine */
144317 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
144326 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
144327 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
144328 ** pSO->nExpr columns, then make sure all keys are unique by adding a
144333 int nKey;
144334 int r1, r2, r3;
144335 int addrTest = 0;
144337 pSO = pDest->pOrderBy;
144339 nKey = pSO->nExpr;
144358 regResult + pSO->a[i].u.x.iOrderByCol - 1,
144376 ** user-defined functions that have side effects. We do not care
144390 if( pSort==0 && p->iLimit ){
144391 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
144399 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
144400 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
144403 p->aSortFlags = (u8*)&p->aColl[N+X];
144404 p->nKeyField = (u16)N;
144405 p->nAllField = (u16)(N+X);
144406 p->enc = ENC(db);
144407 p->db = db;
144408 p->nRef = 1;
144421 assert( p->db!=0 );
144422 assert( p->nRef>0 );
144423 p->nRef--;
144424 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
144433 assert( p->nRef>0 );
144434 p->nRef++;
144446 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
144466 int iStart, /* Begin with this column of pList */
144467 int nExtra /* Add this many extra columns to the end */
144469 int nExpr;
144472 sqlite3 *db = pParse->db;
144473 int i;
144475 nExpr = pList->nExpr;
144476 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
144479 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
144480 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
144481 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
144490 SQLITE_PRIVATE const char *sqlite3SelectOpName(int id){
144504 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
144507 ** "USE TEMP B-TREE FOR xxx"
144513 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
144517 ** Assign expression b to lvalue a. A second, no-op, version of this macro
144526 /* No-op versions of the explainXXX() functions and macros. */
144533 ** If the inner loop was generated using a non-null pOrderBy argument,
144542 int nColumn, /* Number of columns of data */
144545 Vdbe *v = pParse->pVdbe; /* The prepared statement */
144546 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
144547 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
144548 int addr; /* Top of output loop. Jump for Next. */
144549 int addrOnce = 0;
144550 int iTab;
144551 ExprList *pOrderBy = pSort->pOrderBy;
144552 int eDest = pDest->eDest;
144553 int iParm = pDest->iSDParm;
144554 int regRow;
144555 int regRowid;
144556 int iCol;
144557 int nKey; /* Number of key columns in sorter record */
144558 int iSortTab; /* Sorter cursor to read from */
144559 int i;
144560 int bSeq; /* True if sorter record includes seq. no. */
144561 int nRefKey = 0;
144562 struct ExprList_item *aOutEx = p->pEList->a;
144564 int addrExplain; /* Address of OP_Explain instruction */
144567 nKey = pOrderBy->nExpr - pSort->nOBSat;
144568 if( pSort->nOBSat==0 || nKey==1 ){
144570 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":""
144574 "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey
144577 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
144578 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
144582 if( pSort->labelBkOut ){
144583 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
144585 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
144589 /* Open any cursors needed for sorter-reference expressions */
144590 for(i=0; i<pSort->nDefer; i++){
144591 Table *pTab = pSort->aDefer[i].pTab;
144592 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
144593 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
144594 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
144598 iTab = pSort->iECursor;
144600 if( eDest==SRT_Mem && p->iOffset ){
144601 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
144604 regRow = pDest->iSdst;
144614 if( pSort->sortFlags & SORTFLAG_UseSorter ){
144615 int regSortOut = ++pParse->nMem;
144616 iSortTab = pParse->nTab++;
144617 if( pSort->labelBkOut ){
144625 assert( p->iLimit==0 && p->iOffset==0 );
144630 codeOffset(v, p->iOffset, addrContinue);
144633 if( p->iOffset>0 ){
144634 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
144637 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
144644 if( pSort->nDefer ){
144645 int iKey = iCol+1;
144646 int regKey = sqlite3GetTempRange(pParse, nRefKey);
144648 for(i=0; i<pSort->nDefer; i++){
144649 int iCsr = pSort->aDefer[i].iCsr;
144650 Table *pTab = pSort->aDefer[i].pTab;
144651 int nKey = pSort->aDefer[i].nKey;
144659 int k;
144660 int iJmp;
144661 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
144674 for(i=nColumn-1; i>=0; i--){
144681 int iRead;
144683 iRead = aOutEx[i].u.x.iOrderByCol-1;
144685 iRead = iCol--;
144691 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
144703 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
144705 pDest->zAffSdst, nColumn);
144715 int i2 = pDest->iSDParm2;
144716 int r1 = sqlite3GetTempReg(pParse);
144717 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
144730 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
144732 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
144748 if( pSort->sortFlags & SORTFLAG_UseSorter ){
144753 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
144754 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
144766 ** result-set expression in all of the following SELECT statements is
144777 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
144796 int j;
144804 assert( pNC->pSrcList!=0 );
144805 switch( pExpr->op ){
144813 int iCol = pExpr->iColumn; /* Index of column in pTab */
144815 SrcList *pTabList = pNC->pSrcList;
144816 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
144817 if( j<pTabList->nSrc ){
144818 pTab = pTabList->a[j].pTab;
144819 pS = pTabList->a[j].pSelect;
144821 pNC = pNC->pNext;
144836 ** sub-select. In this case, set the column type to NULL, even
144846 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
144848 /* The "table" is actually a sub-select or a view in the FROM clause
144850 ** data for the result-set column of the sub-select.
144852 if( iCol<pS->pEList->nExpr
144856 ** rowid of the sub-select or view. This expression is legal (see
144857 ** test case misc2.2.2) - it always evaluates to NULL.
144860 Expr *p = pS->pEList->a[iCol].pExpr;
144861 sNC.pSrcList = pS->pSrc;
144863 sNC.pParse = pNC->pParse;
144870 if( iCol<0 ) iCol = pTab->iPKey;
144871 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
144876 zOrigCol = pTab->aCol[iCol].zCnName;
144877 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
144879 zOrigTab = pTab->zName;
144880 if( pNC->pParse && pTab->pSchema ){
144881 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
144882 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
144885 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
144889 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
144897 /* The expression is a sub-select. Return the declaration type and
144905 pS = pExpr->x.pSelect;
144906 p = pS->pEList->a[0].pExpr;
144907 sNC.pSrcList = pS->pSrc;
144909 sNC.pParse = pNC->pParse;
144937 Vdbe *v = pParse->pVdbe;
144938 int i;
144943 for(i=0; i<pEList->nExpr; i++){
144944 Expr *p = pEList->a[i].pExpr;
144952 /* The vdbe must make its own copy of the column-type and other
145002 Vdbe *v = pParse->pVdbe;
145003 int i;
145007 sqlite3 *db = pParse->db;
145008 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
145009 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
145011 if( pParse->colNamesSet ) return;
145012 /* Column names are determined by the left-most term of a compound select */
145013 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145015 pTabList = pSelect->pSrc;
145016 pEList = pSelect->pEList;
145019 pParse->colNamesSet = 1;
145020 fullName = (db->flags & SQLITE_FullColNames)!=0;
145021 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
145022 sqlite3VdbeSetNumCols(v, pEList->nExpr);
145023 for(i=0; i<pEList->nExpr; i++){
145024 Expr *p = pEList->a[i].pExpr;
145027 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
145028 assert( p->op!=TK_COLUMN
145029 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
145030 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
145032 char *zName = pEList->a[i].zEName;
145034 }else if( srcName && p->op==TK_COLUMN ){
145036 int iCol = p->iColumn;
145037 pTab = p->y.pTab;
145039 if( iCol<0 ) iCol = pTab->iPKey;
145040 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
145044 zCol = pTab->aCol[iCol].zCnName;
145048 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
145054 const char *z = pEList->a[i].zEName;
145084 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
145090 sqlite3 *db = pParse->db; /* Database connection */
145091 int i, j; /* Loop counters */
145094 int nCol; /* Number of columns in the result set */
145096 int nName; /* Size of name in zName[] */
145102 nCol = pEList->nExpr;
145114 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
145115 struct ExprList_item *pX = &pEList->a[i];
145119 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
145122 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
145123 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
145124 pColExpr = pColExpr->pRight;
145127 if( pColExpr->op==TK_COLUMN
145129 && ALWAYS( pColExpr->y.pTab!=0 )
145132 int iCol = pColExpr->iColumn;
145133 pTab = pColExpr->y.pTab;
145134 if( iCol<0 ) iCol = pTab->iPKey;
145135 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
145136 }else if( pColExpr->op==TK_ID ){
145138 zName = pColExpr->u.zToken;
145141 assert( zName==pX->zEName ); /* pointer comparison intended */
145155 if( pCollide->fg.bUsingTerm ){
145156 pCol->colFlags |= COLFLAG_NOEXPAND;
145160 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
145169 pCol->zCnName = zName;
145170 pCol->hName = sqlite3StrIHash(zName);
145171 if( pX->fg.bNoExpand ){
145172 pCol->colFlags |= COLFLAG_NOEXPAND;
145180 if( pParse->nErr ){
145187 return pParse->rc;
145209 sqlite3 *db = pParse->db;
145212 int i,j;
145218 assert( (pSelect->selFlags & SF_Resolved)!=0 );
145219 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
145221 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
145222 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145223 a = pSelect->pEList->a;
145225 sNC.pSrcList = pSelect->pSrc;
145226 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145229 int m = 0;
145231 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145233 /* pCol->szEst = ... // Column size est for SELECT tables never used */
145234 pCol->affinity = sqlite3ExprAffinity(p);
145235 while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
145236 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145237 pS2 = pS2->pNext;
145238 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
145240 if( pCol->affinity<=SQLITE_AFF_NONE ){
145241 pCol->affinity = aff;
145243 if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
145244 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
145245 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145247 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
145248 pCol->affinity = SQLITE_AFF_BLOB;
145250 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
145251 pCol->affinity = SQLITE_AFF_BLOB;
145253 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
145254 pCol->affinity = SQLITE_AFF_FLEXNUM;
145258 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
145259 if( pCol->affinity==SQLITE_AFF_NUMERIC
145260 || pCol->affinity==SQLITE_AFF_FLEXNUM
145266 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
145275 n = sqlite3Strlen30(pCol->zCnName);
145276 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
145277 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
145278 if( pCol->zCnName ){
145279 memcpy(&pCol->zCnName[n+1], zType, k+1);
145280 pCol->colFlags |= COLFLAG_HASTYPE;
145285 assert( pTab->pIndex==0 );
145286 sqlite3ColumnSetColl(db, pCol, pColl->zName);
145289 pTab->szTabRow = 1; /* Any non-zero value works */
145298 sqlite3 *db = pParse->db;
145301 savedFlags = db->flags;
145302 db->flags &= ~(u64)SQLITE_FullColNames;
145303 db->flags |= SQLITE_ShortColNames;
145305 db->flags = savedFlags;
145306 if( pParse->nErr ) return 0;
145307 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145312 pTab->nTabRef = 1;
145313 pTab->zName = 0;
145314 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
145315 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
145317 pTab->iPKey = -1;
145318 if( db->mallocFailed ){
145330 if( pParse->pVdbe ){
145331 return pParse->pVdbe;
145333 if( pParse->pToplevel==0
145334 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
145336 pParse->okConstFactor = 1;
145344 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
145352 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
145360 ** Only if pLimit->pLeft!=0 do the limit registers get
145365 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
145367 int iLimit = 0;
145368 int iOffset;
145369 int n;
145370 Expr *pLimit = p->pLimit;
145372 if( p->iLimit ) return;
145375 ** "LIMIT -1" always shows all rows. There is some
145381 assert( pLimit->op==TK_LIMIT );
145382 assert( pLimit->pLeft!=0 );
145383 p->iLimit = iLimit = ++pParse->nMem;
145386 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
145391 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
145392 p->nSelectRow = sqlite3LogEst((u64)n);
145393 p->selFlags |= SF_FixedLimit;
145396 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
145401 if( pLimit->pRight ){
145402 p->iOffset = iOffset = ++pParse->nMem;
145403 pParse->nMem++; /* Allocate an extra register for limit+offset */
145404 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
145415 ** Return the appropriate collating sequence for the iCol-th column of
145416 ** the result set for the compound-select statement "p". Return NULL if
145420 ** left-most term of the select that has a collating sequence.
145422 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
145424 if( p->pPrior ){
145425 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
145430 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
145433 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
145434 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
145448 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
145449 ExprList *pOrderBy = p->pOrderBy;
145450 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
145451 sqlite3 *db = pParse->db;
145454 int i;
145456 struct ExprList_item *pItem = &pOrderBy->a[i];
145457 Expr *pTerm = pItem->pExpr;
145460 if( pTerm->flags & EP_Collate ){
145463 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
145464 if( pColl==0 ) pColl = db->pDfltColl;
145465 pOrderBy->a[i].pExpr =
145466 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
145469 pRet->aColl[i] = pColl;
145470 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
145482 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
145484 ** p->pPrior p
145487 ** There is exactly one reference to the recursive-table in the FROM clause
145488 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
145490 ** The setup-query runs once to generate an initial set of rows that go
145494 ** recursive-table for a recursive-query run. The output of the recursive-query
145519 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
145520 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
145521 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
145523 Select *pFirstRec; /* Left-most recursive term */
145524 int addrTop; /* Top of the loop */
145525 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
145526 int iCurrent = 0; /* The Current table */
145527 int regCurrent; /* Register holding Current table */
145528 int iQueue; /* The Queue table */
145529 int iDistinct = 0; /* To ensure unique results if UNION */
145530 int eDest = SRT_Fifo; /* How to write to Queue */
145532 int i; /* Loop counter */
145533 int rc; /* Result code */
145536 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
145539 if( p->pWin ){
145550 p->nSelectRow = 320; /* 4 billion rows */
145552 pLimit = p->pLimit;
145553 regLimit = p->iLimit;
145554 regOffset = p->iOffset;
145555 p->pLimit = 0;
145556 p->iLimit = p->iOffset = 0;
145557 pOrderBy = p->pOrderBy;
145560 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
145561 if( pSrc->a[i].fg.isRecursive ){
145562 iCurrent = pSrc->a[i].iCursor;
145570 iQueue = pParse->nTab++;
145571 if( p->op==TK_UNION ){
145573 iDistinct = pParse->nTab++;
145580 regCurrent = ++pParse->nMem;
145584 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
145592 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
145593 p->selFlags |= SF_UsesEphemeral;
145597 p->pOrderBy = 0;
145603 ** iDistinct table. pFirstRec is left pointing to the left-most
145606 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
145607 if( pFirstRec->selFlags & SF_Aggregate ){
145611 pFirstRec->op = TK_ALL;
145612 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
145615 /* Store the results of the setup-query in Queue. */
145616 pSetup = pFirstRec->pPrior;
145617 pSetup->pNext = 0;
145620 pSetup->pNext = p;
145629 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
145647 ** the value for the recursive-table. Store the results in the Queue.
145649 pFirstRec->pPrior = 0;
145652 assert( pFirstRec->pPrior==0 );
145653 pFirstRec->pPrior = pSetup;
145660 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
145661 p->pOrderBy = pOrderBy;
145662 p->pLimit = pLimit;
145668 static int multiSelectOrderBy(
145670 Select *p, /* The right-most of SELECTs to be coded */
145675 ** Handle the special case of a compound-select that originates from a
145688 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
145690 static int multiSelectValues(
145692 Select *p, /* The right-most of SELECTs to be coded */
145695 int nRow = 1;
145696 int rc = 0;
145697 int bShowAll = p->pLimit==0;
145698 assert( p->selFlags & SF_MultiValue );
145700 assert( p->selFlags & SF_Values );
145701 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
145702 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
145704 if( p->pWin ) return -1;
145706 if( p->pPrior==0 ) break;
145707 assert( p->pPrior->pNext==p );
145708 p = p->pPrior;
145714 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
145716 p->nSelectRow = nRow;
145717 p = p->pNext;
145727 static int hasAnchor(Select *p){
145728 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
145737 ** "p" points to the right-most of the two queries. the query on the
145738 ** left is p->pPrior. The left query could also be a compound query
145744 ** Example 1: Consider a three-way compound SQL statement.
145752 ** `-----> SELECT b FROM t2
145754 ** `------> SELECT a FROM t1
145758 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
145763 static int multiSelect(
145765 Select *p, /* The right-most of SELECTs to be coded */
145768 int rc = SQLITE_OK; /* Success code from a subroutine */
145776 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
145778 assert( p && p->pPrior ); /* Calling function guarantees this much */
145779 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
145780 assert( p->selFlags & SF_Compound );
145781 db = pParse->db;
145782 pPrior = p->pPrior;
145784 assert( pPrior->pOrderBy==0 );
145785 assert( pPrior->pLimit==0 );
145793 assert( p->pEList );
145794 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
145798 /* Special handling for a compound-select that originates as a VALUES clause.
145800 if( p->selFlags & SF_MultiValue ){
145809 assert( p->pEList && pPrior->pEList );
145810 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
145813 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
145820 if( p->pOrderBy ){
145825 if( pPrior->pPrior==0 ){
145827 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
145833 switch( p->op ){
145835 int addr = 0;
145836 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
145837 assert( !pPrior->pLimit );
145838 pPrior->iLimit = p->iLimit;
145839 pPrior->iOffset = p->iOffset;
145840 pPrior->pLimit = p->pLimit;
145843 pPrior->pLimit = 0;
145847 p->pPrior = 0;
145848 p->iLimit = pPrior->iLimit;
145849 p->iOffset = pPrior->iOffset;
145850 if( p->iLimit ){
145851 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
145853 if( p->iOffset ){
145855 p->iLimit, p->iOffset+1, p->iOffset);
145862 pDelete = p->pPrior;
145863 p->pPrior = pPrior;
145864 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
145865 if( p->pLimit
145866 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
145867 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
145869 p->nSelectRow = sqlite3LogEst((u64)nLimit);
145878 int unionTab; /* Cursor number of the temp table holding result */
145880 int priorOp; /* The SRT_ operation to apply to prior selects */
145881 Expr *pLimit; /* Saved values of p->nLimit */
145882 int addr;
145885 testcase( p->op==TK_EXCEPT );
145886 testcase( p->op==TK_UNION );
145892 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
145898 unionTab = pParse->nTab++;
145899 assert( p->pOrderBy==0 );
145901 assert( p->addrOpenEphm[0] == -1 );
145902 p->addrOpenEphm[0] = addr;
145903 findRightmost(p)->selFlags |= SF_UsesEphemeral;
145904 assert( p->pEList );
145910 assert( !pPrior->pOrderBy );
145920 if( p->op==TK_EXCEPT ){
145923 assert( p->op==TK_UNION );
145926 p->pPrior = 0;
145927 pLimit = p->pLimit;
145928 p->pLimit = 0;
145930 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
145931 sqlite3SelectOpName(p->op)));
145935 assert( p->pOrderBy==0 );
145936 pDelete = p->pPrior;
145937 p->pPrior = pPrior;
145938 p->pOrderBy = 0;
145939 if( p->op==TK_UNION ){
145940 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
145942 sqlite3ExprDelete(db, p->pLimit);
145943 p->pLimit = pLimit;
145944 p->iLimit = 0;
145945 p->iOffset = 0;
145951 assert( p->pEList || db->mallocFailed );
145952 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
145953 int iCont, iBreak, iStart;
145968 default: assert( p->op==TK_INTERSECT ); {
145969 int tab1, tab2;
145970 int iCont, iBreak, iStart;
145972 int addr;
145974 int r1;
145980 tab1 = pParse->nTab++;
145981 tab2 = pParse->nTab++;
145982 assert( p->pOrderBy==0 );
145985 assert( p->addrOpenEphm[0] == -1 );
145986 p->addrOpenEphm[0] = addr;
145987 findRightmost(p)->selFlags |= SF_UsesEphemeral;
145988 assert( p->pEList );
146002 assert( p->addrOpenEphm[1] == -1 );
146003 p->addrOpenEphm[1] = addr;
146004 p->pPrior = 0;
146005 pLimit = p->pLimit;
146006 p->pLimit = 0;
146008 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
146009 sqlite3SelectOpName(p->op)));
146013 pDelete = p->pPrior;
146014 p->pPrior = pPrior;
146015 if( p->nSelectRow>pPrior->nSelectRow ){
146016 p->nSelectRow = pPrior->nSelectRow;
146018 sqlite3ExprDelete(db, p->pLimit);
146019 p->pLimit = pLimit;
146025 assert( p->pEList );
146047 if( p->pNext==0 ){
146052 if( pParse->nErr ) goto multi_select_end;
146058 ** This section is run by the right-most SELECT statement only.
146059 ** SELECT statements to the left always skip this part. The right-most
146063 if( p->selFlags & SF_UsesEphemeral ){
146064 int i; /* Loop counter */
146067 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
146068 int nCol; /* Number of columns in result set */
146070 assert( p->pNext==0 );
146071 assert( p->pEList!=0 );
146072 nCol = p->pEList->nExpr;
146078 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
146081 *apColl = db->pDfltColl;
146085 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
146087 int addr = pLoop->addrOpenEphm[i];
146091 assert( pLoop->addrOpenEphm[1]<0 );
146097 pLoop->addrOpenEphm[i] = -1;
146104 pDest->iSdst = dest.iSdst;
146105 pDest->nSdst = dest.nSdst;
146118 if( p->selFlags & SF_Values ){
146123 sqlite3SelectOpName(p->op));
146131 ** The data to be output is contained in pIn->iSdst. There are
146132 ** pIn->nSdst columns to be output. pDest is where the output should
146144 ** If the LIMIT found in p->iLimit is reached, jump immediately to
146147 static int generateOutputSubroutine(
146152 int regReturn, /* The return address register */
146153 int regPrev, /* Previous result register. No uniqueness if 0 */
146155 int iBreak /* Jump here if we hit the LIMIT */
146157 Vdbe *v = pParse->pVdbe;
146158 int iContinue;
146159 int addr;
146167 int addr1, addr2;
146169 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
146173 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
146176 if( pParse->db->mallocFailed ) return 0;
146180 codeOffset(v, p->iOffset, iContinue);
146182 assert( pDest->eDest!=SRT_Exists );
146183 assert( pDest->eDest!=SRT_Table );
146184 switch( pDest->eDest ){
146188 int r1 = sqlite3GetTempReg(pParse);
146189 int r2 = sqlite3GetTempReg(pParse);
146190 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
146191 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
146192 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
146203 int r1;
146204 testcase( pIn->nSdst>1 );
146206 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
146207 r1, pDest->zAffSdst, pIn->nSdst);
146208 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
146209 pIn->iSdst, pIn->nSdst);
146217 ** if it is the RHS of a row-value IN operator.
146220 testcase( pIn->nSdst>1 );
146221 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
146228 ** starting at pDest->iSdst. Then the co-routine yields.
146231 if( pDest->iSdst==0 ){
146232 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
146233 pDest->nSdst = pIn->nSdst;
146235 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
146236 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
146249 assert( pDest->eDest==SRT_Output );
146250 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
146257 if( p->iLimit ){
146258 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
146279 ** co-routines. Then run the co-routines in parallel and merge the results
146306 ** ------------- ----------------- -------------- -----------------
146355 static int multiSelectOrderBy(
146357 Select *p, /* The right-most of SELECTs to be coded */
146360 int i, j; /* Loop counters */
146362 Select *pSplit; /* Left-most SELECT in the right-hand group */
146363 int nSelect; /* Number of SELECT statements in the compound */
146367 int regAddrA; /* Address register for select-A coroutine */
146368 int regAddrB; /* Address register for select-B coroutine */
146369 int addrSelectA; /* Address of the select-A coroutine */
146370 int addrSelectB; /* Address of the select-B coroutine */
146371 int regOutA; /* Address register for the output-A subroutine */
146372 int regOutB; /* Address register for the output-B subroutine */
146373 int addrOutA; /* Address of the output-A subroutine */
146374 int addrOutB = 0; /* Address of the output-B subroutine */
146375 int addrEofA; /* Address of the select-A-exhausted subroutine */
146376 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
146377 int addrEofB; /* Address of the select-B-exhausted subroutine */
146378 int addrAltB; /* Address of the A<B subroutine */
146379 int addrAeqB; /* Address of the A==B subroutine */
146380 int addrAgtB; /* Address of the A>B subroutine */
146381 int regLimitA; /* Limit register for select-A */
146382 int regLimitB; /* Limit register for select-A */
146383 int regPrev; /* A range of registers to hold previous output */
146384 int savedLimit; /* Saved value of p->iLimit */
146385 int savedOffset; /* Saved value of p->iOffset */
146386 int labelCmpr; /* Label for the start of the merge algorithm */
146387 int labelEnd; /* Label for the end of the overall SELECT stmt */
146388 int addr1; /* Jump instructions that get retargeted */
146389 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
146394 int nOrderBy; /* Number of terms in the ORDER BY clause */
146397 assert( p->pOrderBy!=0 );
146399 db = pParse->db;
146400 v = pParse->pVdbe;
146408 op = p->op;
146409 assert( p->pPrior->pOrderBy==0 );
146410 pOrderBy = p->pOrderBy;
146412 nOrderBy = pOrderBy->nExpr;
146419 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
146421 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
146423 assert( pItem->u.x.iOrderByCol>0 );
146424 if( pItem->u.x.iOrderByCol==i ) break;
146429 pNew->flags |= EP_IntValue;
146430 pNew->u.iValue = i;
146431 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
146432 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
146448 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
146450 assert( pItem->u.x.iOrderByCol>0 );
146451 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
146452 aPermute[i] = pItem->u.x.iOrderByCol - 1;
146466 int nExpr = p->pEList->nExpr;
146467 assert( nOrderBy>=nExpr || db->mallocFailed );
146468 regPrev = pParse->nMem+1;
146469 pParse->nMem += nExpr+1;
146475 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
146476 pKeyDup->aSortFlags[i] = 0;
146487 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
146489 assert( pSplit->pPrior->pNext==pSplit );
146496 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
146498 pPrior = pSplit->pPrior;
146500 pSplit->pPrior = 0;
146501 pPrior->pNext = 0;
146502 assert( p->pOrderBy == pOrderBy );
146503 assert( pOrderBy!=0 || db->mallocFailed );
146504 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
146505 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
146506 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
146510 if( p->iLimit && op==TK_ALL ){
146511 regLimitA = ++pParse->nMem;
146512 regLimitB = ++pParse->nMem;
146513 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
146519 sqlite3ExprDelete(db, p->pLimit);
146520 p->pLimit = 0;
146522 regAddrA = ++pParse->nMem;
146523 regAddrB = ++pParse->nMem;
146524 regOutA = ++pParse->nMem;
146525 regOutB = ++pParse->nMem;
146529 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
146532 ** left of the compound operator - the "A" select.
146537 pPrior->iLimit = regLimitA;
146544 ** the right - the "B" select
146549 savedLimit = p->iLimit;
146550 savedOffset = p->iOffset;
146551 p->iLimit = regLimitB;
146552 p->iOffset = 0;
146555 p->iLimit = savedLimit;
146556 p->iOffset = savedOffset;
146584 VdbeNoopComment((v, "eof-A subroutine"));
146589 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
146597 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
146599 VdbeNoopComment((v, "eof-B subroutine"));
146607 VdbeNoopComment((v, "A-lt-B subroutine"));
146620 VdbeNoopComment((v, "A-eq-B subroutine"));
146628 VdbeNoopComment((v, "A-gt-B subroutine"));
146657 if( pSplit->pPrior ){
146658 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior);
146660 pSplit->pPrior = pPrior;
146661 pPrior->pNext = pSplit;
146662 sqlite3ExprListDelete(db, pPrior->pOrderBy);
146663 pPrior->pOrderBy = 0;
146668 return pParse->nErr!=0;
146683 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
146698 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
146699 ** CREATE TABLE t2(x INT UNIQUE);
146706 ** when processing a non-matched row of the left.
146710 int iTable; /* Replace references to this table */
146711 int iNewTable; /* New table number */
146712 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
146719 static void substSelect(SubstContext*, Select*, int);
146723 ** a column in table number iTable with a copy of the iColumn-th
146740 && pExpr->w.iJoin==pSubst->iTable
146743 pExpr->w.iJoin = pSubst->iNewTable;
146745 if( pExpr->op==TK_COLUMN
146746 && pExpr->iTable==pSubst->iTable