| | @@ -231,11 +231,11 @@ |
| 231 | 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | 233 | */ |
| 234 | 234 | #define SQLITE_VERSION "3.8.7" |
| 235 | 235 | #define SQLITE_VERSION_NUMBER 3008007 |
| 236 | | -#define SQLITE_SOURCE_ID "2014-09-20 00:35:05 59e2c9df02d7e988c5ad44c560ead1e5288b12e7" |
| 236 | +#define SQLITE_SOURCE_ID "2014-10-04 19:31:53 b8f7f19dc06c59de2e194d83e6c052fb7d28c71d" |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| | @@ -2791,13 +2791,13 @@ |
| 2791 | 2791 | ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The |
| 2792 | 2792 | ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain |
| 2793 | 2793 | ** an English language description of the error following a failure of any |
| 2794 | 2794 | ** of the sqlite3_open() routines. |
| 2795 | 2795 | ** |
| 2796 | | -** ^The default encoding for the database will be UTF-8 if |
| 2797 | | -** sqlite3_open() or sqlite3_open_v2() is called and |
| 2798 | | -** UTF-16 in the native byte order if sqlite3_open16() is used. |
| 2796 | +** ^The default encoding will be UTF-8 for databases created using |
| 2797 | +** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases |
| 2798 | +** created using sqlite3_open16() will be UTF-16 in the native byte order. |
| 2799 | 2799 | ** |
| 2800 | 2800 | ** Whether or not an error occurs when it is opened, resources |
| 2801 | 2801 | ** associated with the [database connection] handle should be released by |
| 2802 | 2802 | ** passing it to [sqlite3_close()] when it is no longer required. |
| 2803 | 2803 | ** |
| | @@ -2881,17 +2881,18 @@ |
| 2881 | 2881 | ** ^SQLite uses the path component of the URI as the name of the disk file |
| 2882 | 2882 | ** which contains the database. ^If the path begins with a '/' character, |
| 2883 | 2883 | ** then it is interpreted as an absolute path. ^If the path does not begin |
| 2884 | 2884 | ** with a '/' (meaning that the authority section is omitted from the URI) |
| 2885 | 2885 | ** then the path is interpreted as a relative path. |
| 2886 | | -** ^On windows, the first component of an absolute path |
| 2887 | | -** is a drive specification (e.g. "C:"). |
| 2886 | +** ^(On windows, the first component of an absolute path |
| 2887 | +** is a drive specification (e.g. "C:").)^ |
| 2888 | 2888 | ** |
| 2889 | 2889 | ** [[core URI query parameters]] |
| 2890 | 2890 | ** The query component of a URI may contain parameters that are interpreted |
| 2891 | 2891 | ** either by SQLite itself, or by a [VFS | custom VFS implementation]. |
| 2892 | | -** SQLite interprets the following three query parameters: |
| 2892 | +** SQLite and its built-in [VFSes] interpret the |
| 2893 | +** following query parameters: |
| 2893 | 2894 | ** |
| 2894 | 2895 | ** <ul> |
| 2895 | 2896 | ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of |
| 2896 | 2897 | ** a VFS object that provides the operating system interface that should |
| 2897 | 2898 | ** be used to access the database file on disk. ^If this option is set to |
| | @@ -2922,15 +2923,13 @@ |
| 2922 | 2923 | ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. |
| 2923 | 2924 | ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in |
| 2924 | 2925 | ** a URI filename, its value overrides any behavior requested by setting |
| 2925 | 2926 | ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. |
| 2926 | 2927 | ** |
| 2927 | | -** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or |
| 2928 | | -** "1") or "false" (or "off" or "no" or "0") to indicate that the |
| 2928 | +** <li> <b>psow</b>: ^The psow parameter indicates whether or not the |
| 2929 | 2929 | ** [powersafe overwrite] property does or does not apply to the |
| 2930 | | -** storage media on which the database file resides. ^The psow query |
| 2931 | | -** parameter only works for the built-in unix and Windows VFSes. |
| 2930 | +** storage media on which the database file resides. |
| 2932 | 2931 | ** |
| 2933 | 2932 | ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter |
| 2934 | 2933 | ** which if set disables file locking in rollback journal modes. This |
| 2935 | 2934 | ** is useful for accessing a database on a filesystem that does not |
| 2936 | 2935 | ** support locking. Caution: Database corruption might result if two |
| | @@ -3521,15 +3520,14 @@ |
| 3521 | 3520 | ** terminated. If any NUL characters occur at byte offsets less than |
| 3522 | 3521 | ** the value of the fourth parameter then the resulting string value will |
| 3523 | 3522 | ** contain embedded NULs. The result of expressions involving strings |
| 3524 | 3523 | ** with embedded NULs is undefined. |
| 3525 | 3524 | ** |
| 3526 | | -** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and |
| 3527 | | -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or |
| 3525 | +** ^The fifth argument to the BLOB and string binding interfaces |
| 3526 | +** is a destructor used to dispose of the BLOB or |
| 3528 | 3527 | ** string after SQLite has finished with it. ^The destructor is called |
| 3529 | | -** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(), |
| 3530 | | -** sqlite3_bind_text(), or sqlite3_bind_text16() fails. |
| 3528 | +** to dispose of the BLOB or string even if the call to bind API fails. |
| 3531 | 3529 | ** ^If the fifth argument is |
| 3532 | 3530 | ** the special value [SQLITE_STATIC], then SQLite assumes that the |
| 3533 | 3531 | ** information is in static, unmanaged space and does not need to be freed. |
| 3534 | 3532 | ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then |
| 3535 | 3533 | ** SQLite makes its own private copy of the data immediately, before |
| | @@ -3536,11 +3534,11 @@ |
| 3536 | 3534 | ** the sqlite3_bind_*() routine returns. |
| 3537 | 3535 | ** |
| 3538 | 3536 | ** ^The sixth argument to sqlite3_bind_text64() must be one of |
| 3539 | 3537 | ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] |
| 3540 | 3538 | ** to specify the encoding of the text in the third parameter. If |
| 3541 | | -** the sixth argument to sqlite3_bind_text64() is not how of the |
| 3539 | +** the sixth argument to sqlite3_bind_text64() is not one of the |
| 3542 | 3540 | ** allowed values shown above, or if the text encoding is different |
| 3543 | 3541 | ** from the encoding specified by the sixth parameter, then the behavior |
| 3544 | 3542 | ** is undefined. |
| 3545 | 3543 | ** |
| 3546 | 3544 | ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that |
| | @@ -4572,11 +4570,11 @@ |
| 4572 | 4570 | ** |
| 4573 | 4571 | ** ^The sqlite3_result_null() interface sets the return value |
| 4574 | 4572 | ** of the application-defined function to be NULL. |
| 4575 | 4573 | ** |
| 4576 | 4574 | ** ^The sqlite3_result_text(), sqlite3_result_text16(), |
| 4577 | | -** sqlite3_result_text16le(), and sqlite3_result_text16be() |
| 4575 | +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces |
| 4578 | 4576 | ** set the return value of the application-defined function to be |
| 4579 | 4577 | ** a text string which is represented as UTF-8, UTF-16 native byte order, |
| 4580 | 4578 | ** UTF-16 little endian, or UTF-16 big endian, respectively. |
| 4581 | 4579 | ** ^The sqlite3_result_text64() interface sets the return value of an |
| 4582 | 4580 | ** application-defined function to be a text string in an encoding |
| | @@ -6332,11 +6330,11 @@ |
| 6332 | 6330 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 6333 | 6331 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 6334 | 6332 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 6335 | 6333 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 17 |
| 6336 | 6334 | #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 |
| 6337 | | -#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 |
| 6335 | +#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ |
| 6338 | 6336 | #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 |
| 6339 | 6337 | #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 |
| 6340 | 6338 | #define SQLITE_TESTCTRL_BYTEORDER 22 |
| 6341 | 6339 | #define SQLITE_TESTCTRL_ISINIT 23 |
| 6342 | 6340 | #define SQLITE_TESTCTRL_SORTER_MMAP 24 |
| | @@ -8519,10 +8517,15 @@ |
| 8519 | 8517 | ** Macros to compute minimum and maximum of two numbers. |
| 8520 | 8518 | */ |
| 8521 | 8519 | #define MIN(A,B) ((A)<(B)?(A):(B)) |
| 8522 | 8520 | #define MAX(A,B) ((A)>(B)?(A):(B)) |
| 8523 | 8521 | |
| 8522 | +/* |
| 8523 | +** Swap two objects of type TYPE. |
| 8524 | +*/ |
| 8525 | +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 8526 | + |
| 8524 | 8527 | /* |
| 8525 | 8528 | ** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 8526 | 8529 | ** not, there are still machines out there that use EBCDIC.) |
| 8527 | 8530 | */ |
| 8528 | 8531 | #if 'A' == '\301' |
| | @@ -8756,10 +8759,20 @@ |
| 8756 | 8759 | # define SQLITE_ENABLE_STAT3_OR_STAT4 1 |
| 8757 | 8760 | #elif SQLITE_ENABLE_STAT3_OR_STAT4 |
| 8758 | 8761 | # undef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 8759 | 8762 | #endif |
| 8760 | 8763 | |
| 8764 | +/* |
| 8765 | +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not |
| 8766 | +** the Select query generator tracing logic is turned on. |
| 8767 | +*/ |
| 8768 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) |
| 8769 | +# define SELECTTRACE_ENABLED 1 |
| 8770 | +#else |
| 8771 | +# define SELECTTRACE_ENABLED 0 |
| 8772 | +#endif |
| 8773 | + |
| 8761 | 8774 | /* |
| 8762 | 8775 | ** An instance of the following structure is used to store the busy-handler |
| 8763 | 8776 | ** callback for a given sqlite handle. |
| 8764 | 8777 | ** |
| 8765 | 8778 | ** The sqlite.busyHandler member of the sqlite struct contains the busy |
| | @@ -8895,10 +8908,11 @@ |
| 8895 | 8908 | typedef struct SrcList SrcList; |
| 8896 | 8909 | typedef struct StrAccum StrAccum; |
| 8897 | 8910 | typedef struct Table Table; |
| 8898 | 8911 | typedef struct TableLock TableLock; |
| 8899 | 8912 | typedef struct Token Token; |
| 8913 | +typedef struct TreeView TreeView; |
| 8900 | 8914 | typedef struct Trigger Trigger; |
| 8901 | 8915 | typedef struct TriggerPrg TriggerPrg; |
| 8902 | 8916 | typedef struct TriggerStep TriggerStep; |
| 8903 | 8917 | typedef struct UnpackedRecord UnpackedRecord; |
| 8904 | 8918 | typedef struct VTable VTable; |
| | @@ -11738,11 +11752,11 @@ |
| 11738 | 11752 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 11739 | 11753 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 11740 | 11754 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 11741 | 11755 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 11742 | 11756 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 11743 | | -#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */ |
| 11757 | + /* 0x0080 // not currently used */ |
| 11744 | 11758 | #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ |
| 11745 | 11759 | #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ |
| 11746 | 11760 | #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ |
| 11747 | 11761 | #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ |
| 11748 | 11762 | #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ |
| | @@ -11823,10 +11837,13 @@ |
| 11823 | 11837 | struct Select { |
| 11824 | 11838 | ExprList *pEList; /* The fields of the result */ |
| 11825 | 11839 | u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 11826 | 11840 | u16 selFlags; /* Various SF_* values */ |
| 11827 | 11841 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 11842 | +#if SELECTTRACE_ENABLED |
| 11843 | + char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ |
| 11844 | +#endif |
| 11828 | 11845 | int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ |
| 11829 | 11846 | u64 nSelectRow; /* Estimated number of result rows */ |
| 11830 | 11847 | SrcList *pSrc; /* The FROM clause */ |
| 11831 | 11848 | Expr *pWhere; /* The WHERE clause */ |
| 11832 | 11849 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| | @@ -12081,10 +12098,14 @@ |
| 12081 | 12098 | yDbMask cookieMask; /* Bitmask of schema verified databases */ |
| 12082 | 12099 | int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ |
| 12083 | 12100 | int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| 12084 | 12101 | int regRoot; /* Register holding root page number for new objects */ |
| 12085 | 12102 | int nMaxArg; /* Max args passed to user function by sub-program */ |
| 12103 | +#if SELECTTRACE_ENABLED |
| 12104 | + int nSelect; /* Number of SELECT statements seen */ |
| 12105 | + int nSelectIndent; /* How far to indent SELECTTRACE() output */ |
| 12106 | +#endif |
| 12086 | 12107 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 12087 | 12108 | int nTableLock; /* Number of locks in aTableLock */ |
| 12088 | 12109 | TableLock *aTableLock; /* Required table locks for shared-cache mode */ |
| 12089 | 12110 | #endif |
| 12090 | 12111 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| | @@ -12160,10 +12181,11 @@ |
| 12160 | 12181 | |
| 12161 | 12182 | /* |
| 12162 | 12183 | ** Bitfield flags for P5 value in various opcodes. |
| 12163 | 12184 | */ |
| 12164 | 12185 | #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */ |
| 12186 | +#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ |
| 12165 | 12187 | #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ |
| 12166 | 12188 | #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ |
| 12167 | 12189 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 12168 | 12190 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 12169 | 12191 | #define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */ |
| | @@ -12428,10 +12450,21 @@ |
| 12428 | 12450 | Select *pSelect; /* The definition of this CTE */ |
| 12429 | 12451 | const char *zErr; /* Error message for circular references */ |
| 12430 | 12452 | } a[1]; |
| 12431 | 12453 | }; |
| 12432 | 12454 | |
| 12455 | +#ifdef SQLITE_DEBUG |
| 12456 | +/* |
| 12457 | +** An instance of the TreeView object is used for printing the content of |
| 12458 | +** data structures on sqlite3DebugPrintf() using a tree-like view. |
| 12459 | +*/ |
| 12460 | +struct TreeView { |
| 12461 | + int iLevel; /* Which level of the tree we are on */ |
| 12462 | + u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */ |
| 12463 | +}; |
| 12464 | +#endif /* SQLITE_DEBUG */ |
| 12465 | + |
| 12433 | 12466 | /* |
| 12434 | 12467 | ** Assuming zIn points to the first byte of a UTF-8 character, |
| 12435 | 12468 | ** advance zIn to point to the first byte of the next UTF-8 character. |
| 12436 | 12469 | */ |
| 12437 | 12470 | #define SQLITE_SKIP_UTF8(zIn) { \ |
| | @@ -12493,10 +12526,11 @@ |
| 12493 | 12526 | # define sqlite3Isalpha(x) isalpha((unsigned char)(x)) |
| 12494 | 12527 | # define sqlite3Isdigit(x) isdigit((unsigned char)(x)) |
| 12495 | 12528 | # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) |
| 12496 | 12529 | # define sqlite3Tolower(x) tolower((unsigned char)(x)) |
| 12497 | 12530 | #endif |
| 12531 | +SQLITE_PRIVATE int sqlite3IsIdChar(u8); |
| 12498 | 12532 | |
| 12499 | 12533 | /* |
| 12500 | 12534 | ** Internal function prototypes |
| 12501 | 12535 | */ |
| 12502 | 12536 | #define sqlite3StrICmp sqlite3_stricmp |
| | @@ -12591,29 +12625,18 @@ |
| 12591 | 12625 | #endif |
| 12592 | 12626 | #if defined(SQLITE_TEST) |
| 12593 | 12627 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| 12594 | 12628 | #endif |
| 12595 | 12629 | |
| 12596 | | -/* Output formatting for SQLITE_TESTCTRL_EXPLAIN */ |
| 12597 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 12598 | | -SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe*); |
| 12599 | | -SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe*, const char*, ...); |
| 12600 | | -SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe*); |
| 12601 | | -SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe*); |
| 12602 | | -SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe*); |
| 12603 | | -SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe*); |
| 12604 | | -SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe*, Select*); |
| 12605 | | -SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe*, Expr*); |
| 12606 | | -SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe*, ExprList*); |
| 12607 | | -SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe*); |
| 12608 | | -#else |
| 12609 | | -# define sqlite3ExplainBegin(X) |
| 12610 | | -# define sqlite3ExplainSelect(A,B) |
| 12611 | | -# define sqlite3ExplainExpr(A,B) |
| 12612 | | -# define sqlite3ExplainExprList(A,B) |
| 12613 | | -# define sqlite3ExplainFinish(X) |
| 12614 | | -# define sqlite3VdbeExplanation(X) 0 |
| 12630 | +#if defined(SQLITE_DEBUG) |
| 12631 | +SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8); |
| 12632 | +SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*); |
| 12633 | +SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...); |
| 12634 | +SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8); |
| 12635 | +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); |
| 12636 | +SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); |
| 12637 | +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); |
| 12615 | 12638 | #endif |
| 12616 | 12639 | |
| 12617 | 12640 | |
| 12618 | 12641 | SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...); |
| 12619 | 12642 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...); |
| | @@ -12791,11 +12814,11 @@ |
| 12791 | 12814 | SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*); |
| 12792 | 12815 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12793 | 12816 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12794 | 12817 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12795 | 12818 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12796 | | -SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*); |
| 12819 | +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12797 | 12820 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12798 | 12821 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12799 | 12822 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12800 | 12823 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12801 | 12824 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| | @@ -12815,10 +12838,15 @@ |
| 12815 | 12838 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int); |
| 12816 | 12839 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); |
| 12817 | 12840 | SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); |
| 12818 | 12841 | SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*); |
| 12819 | 12842 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int); |
| 12843 | +#if SELECTTRACE_ENABLED |
| 12844 | +SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*); |
| 12845 | +#else |
| 12846 | +# define sqlite3SelectSetName(A,B) |
| 12847 | +#endif |
| 12820 | 12848 | SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*); |
| 12821 | 12849 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8); |
| 12822 | 12850 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*); |
| 12823 | 12851 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 12824 | 12852 | SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void); |
| | @@ -13439,10 +13467,17 @@ |
| 13439 | 13467 | 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */ |
| 13440 | 13468 | 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */ |
| 13441 | 13469 | }; |
| 13442 | 13470 | #endif |
| 13443 | 13471 | |
| 13472 | +/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards |
| 13473 | +** compatibility for legacy applications, the URI filename capability is |
| 13474 | +** disabled by default. |
| 13475 | +** |
| 13476 | +** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13477 | +** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13478 | +*/ |
| 13444 | 13479 | #ifndef SQLITE_USE_URI |
| 13445 | 13480 | # define SQLITE_USE_URI 0 |
| 13446 | 13481 | #endif |
| 13447 | 13482 | |
| 13448 | 13483 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| | @@ -13945,11 +13980,11 @@ |
| 13945 | 13980 | |
| 13946 | 13981 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13947 | 13982 | ** linear search is adequate. No need for a binary search. */ |
| 13948 | 13983 | for(i=0; i<ArraySize(azCompileOpt); i++){ |
| 13949 | 13984 | if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0 |
| 13950 | | - && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0 |
| 13985 | + && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0 |
| 13951 | 13986 | ){ |
| 13952 | 13987 | return 1; |
| 13953 | 13988 | } |
| 13954 | 13989 | } |
| 13955 | 13990 | return 0; |
| | @@ -14259,11 +14294,10 @@ |
| 14259 | 14294 | */ |
| 14260 | 14295 | struct sqlite3_context { |
| 14261 | 14296 | Mem *pOut; /* The return value is stored here */ |
| 14262 | 14297 | FuncDef *pFunc; /* Pointer to function information */ |
| 14263 | 14298 | Mem *pMem; /* Memory cell used to store aggregate context */ |
| 14264 | | - CollSeq *pColl; /* Collating sequence */ |
| 14265 | 14299 | Vdbe *pVdbe; /* The VM that owns this context */ |
| 14266 | 14300 | int iOp; /* Instruction number of OP_Function */ |
| 14267 | 14301 | int isError; /* Error code returned by the function. */ |
| 14268 | 14302 | u8 skipFlag; /* Skip accumulator loading if true */ |
| 14269 | 14303 | u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ |
| | @@ -14348,14 +14382,10 @@ |
| 14348 | 14382 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 14349 | 14383 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 14350 | 14384 | i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ |
| 14351 | 14385 | char *zSql; /* Text of the SQL statement that generated this */ |
| 14352 | 14386 | void *pFree; /* Free this when deleting the vdbe */ |
| 14353 | | -#ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 14354 | | - Explain *pExplain; /* The explainer */ |
| 14355 | | - char *zExplain; /* Explanation of data structures */ |
| 14356 | | -#endif |
| 14357 | 14387 | VdbeFrame *pFrame; /* Parent frame */ |
| 14358 | 14388 | VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ |
| 14359 | 14389 | int nFrame; /* Number of frames in pFrame list */ |
| 14360 | 14390 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14361 | 14391 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| | @@ -14675,11 +14705,11 @@ |
| 14675 | 14705 | sqlite3VdbeClearObject(db, pVdbe); |
| 14676 | 14706 | sqlite3DbFree(db, pVdbe); |
| 14677 | 14707 | } |
| 14678 | 14708 | db->pnBytesFreed = 0; |
| 14679 | 14709 | |
| 14680 | | - *pHighwater = 0; |
| 14710 | + *pHighwater = 0; /* IMP: R-64479-57858 */ |
| 14681 | 14711 | *pCurrent = nByte; |
| 14682 | 14712 | |
| 14683 | 14713 | break; |
| 14684 | 14714 | } |
| 14685 | 14715 | |
| | @@ -14700,21 +14730,23 @@ |
| 14700 | 14730 | if( db->aDb[i].pBt ){ |
| 14701 | 14731 | Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); |
| 14702 | 14732 | sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); |
| 14703 | 14733 | } |
| 14704 | 14734 | } |
| 14705 | | - *pHighwater = 0; |
| 14735 | + *pHighwater = 0; /* IMP: R-42420-56072 */ |
| 14736 | + /* IMP: R-54100-20147 */ |
| 14737 | + /* IMP: R-29431-39229 */ |
| 14706 | 14738 | *pCurrent = nRet; |
| 14707 | 14739 | break; |
| 14708 | 14740 | } |
| 14709 | 14741 | |
| 14710 | 14742 | /* Set *pCurrent to non-zero if there are unresolved deferred foreign |
| 14711 | 14743 | ** key constraints. Set *pCurrent to zero if all foreign key constraints |
| 14712 | 14744 | ** have been satisfied. The *pHighwater is always set to zero. |
| 14713 | 14745 | */ |
| 14714 | 14746 | case SQLITE_DBSTATUS_DEFERRED_FKS: { |
| 14715 | | - *pHighwater = 0; |
| 14747 | + *pHighwater = 0; /* IMP: R-11967-56545 */ |
| 14716 | 14748 | *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; |
| 14717 | 14749 | break; |
| 14718 | 14750 | } |
| 14719 | 14751 | |
| 14720 | 14752 | default: { |
| | @@ -20195,11 +20227,11 @@ |
| 20195 | 20227 | mallocWithAlarm((int)n, &p); |
| 20196 | 20228 | sqlite3_mutex_leave(mem0.mutex); |
| 20197 | 20229 | }else{ |
| 20198 | 20230 | p = sqlite3GlobalConfig.m.xMalloc((int)n); |
| 20199 | 20231 | } |
| 20200 | | - assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ |
| 20232 | + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ |
| 20201 | 20233 | return p; |
| 20202 | 20234 | } |
| 20203 | 20235 | |
| 20204 | 20236 | /* |
| 20205 | 20237 | ** This version of the memory allocation is for use by the application. |
| | @@ -20418,14 +20450,14 @@ |
| 20418 | 20450 | */ |
| 20419 | 20451 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 20420 | 20452 | int nOld, nNew, nDiff; |
| 20421 | 20453 | void *pNew; |
| 20422 | 20454 | if( pOld==0 ){ |
| 20423 | | - return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */ |
| 20455 | + return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 20424 | 20456 | } |
| 20425 | 20457 | if( nBytes==0 ){ |
| 20426 | | - sqlite3_free(pOld); /* IMP: R-31593-10574 */ |
| 20458 | + sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| 20427 | 20459 | return 0; |
| 20428 | 20460 | } |
| 20429 | 20461 | if( nBytes>=0x7fffff00 ){ |
| 20430 | 20462 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 20431 | 20463 | return 0; |
| | @@ -20458,11 +20490,11 @@ |
| 20458 | 20490 | } |
| 20459 | 20491 | sqlite3_mutex_leave(mem0.mutex); |
| 20460 | 20492 | }else{ |
| 20461 | 20493 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20462 | 20494 | } |
| 20463 | | - assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */ |
| 20495 | + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ |
| 20464 | 20496 | return pNew; |
| 20465 | 20497 | } |
| 20466 | 20498 | |
| 20467 | 20499 | /* |
| 20468 | 20500 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| | @@ -20470,11 +20502,11 @@ |
| 20470 | 20502 | */ |
| 20471 | 20503 | SQLITE_API void *sqlite3_realloc(void *pOld, int n){ |
| 20472 | 20504 | #ifndef SQLITE_OMIT_AUTOINIT |
| 20473 | 20505 | if( sqlite3_initialize() ) return 0; |
| 20474 | 20506 | #endif |
| 20475 | | - if( n<0 ) n = 0; |
| 20507 | + if( n<0 ) n = 0; /* IMP: R-26507-47431 */ |
| 20476 | 20508 | return sqlite3Realloc(pOld, n); |
| 20477 | 20509 | } |
| 20478 | 20510 | SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ |
| 20479 | 20511 | #ifndef SQLITE_OMIT_AUTOINIT |
| 20480 | 20512 | if( sqlite3_initialize() ) return 0; |
| | @@ -21757,10 +21789,73 @@ |
| 21757 | 21789 | fprintf(stdout,"%s", zBuf); |
| 21758 | 21790 | fflush(stdout); |
| 21759 | 21791 | } |
| 21760 | 21792 | #endif |
| 21761 | 21793 | |
| 21794 | +#ifdef SQLITE_DEBUG |
| 21795 | +/************************************************************************* |
| 21796 | +** Routines for implementing the "TreeView" display of hierarchical |
| 21797 | +** data structures for debugging. |
| 21798 | +** |
| 21799 | +** The main entry points (coded elsewhere) are: |
| 21800 | +** sqlite3TreeViewExpr(0, pExpr, 0); |
| 21801 | +** sqlite3TreeViewExprList(0, pList, 0, 0); |
| 21802 | +** sqlite3TreeViewSelect(0, pSelect, 0); |
| 21803 | +** Insert calls to those routines while debugging in order to display |
| 21804 | +** a diagram of Expr, ExprList, and Select objects. |
| 21805 | +** |
| 21806 | +*/ |
| 21807 | +/* Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 21808 | +** is not the last item in the tree. */ |
| 21809 | +SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 21810 | + if( p==0 ){ |
| 21811 | + p = sqlite3_malloc( sizeof(*p) ); |
| 21812 | + if( p==0 ) return 0; |
| 21813 | + memset(p, 0, sizeof(*p)); |
| 21814 | + }else{ |
| 21815 | + p->iLevel++; |
| 21816 | + } |
| 21817 | + assert( moreToFollow==0 || moreToFollow==1 ); |
| 21818 | + if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 21819 | + return p; |
| 21820 | +} |
| 21821 | +/* Finished with one layer of the tree */ |
| 21822 | +SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){ |
| 21823 | + if( p==0 ) return; |
| 21824 | + p->iLevel--; |
| 21825 | + if( p->iLevel<0 ) sqlite3_free(p); |
| 21826 | +} |
| 21827 | +/* Generate a single line of output for the tree, with a prefix that contains |
| 21828 | +** all the appropriate tree lines */ |
| 21829 | +SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 21830 | + va_list ap; |
| 21831 | + int i; |
| 21832 | + StrAccum acc; |
| 21833 | + char zBuf[500]; |
| 21834 | + sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); |
| 21835 | + acc.useMalloc = 0; |
| 21836 | + if( p ){ |
| 21837 | + for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ |
| 21838 | + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); |
| 21839 | + } |
| 21840 | + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| 21841 | + } |
| 21842 | + va_start(ap, zFormat); |
| 21843 | + sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21844 | + va_end(ap); |
| 21845 | + if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); |
| 21846 | + sqlite3StrAccumFinish(&acc); |
| 21847 | + fprintf(stdout,"%s", zBuf); |
| 21848 | + fflush(stdout); |
| 21849 | +} |
| 21850 | +/* Shorthand for starting a new tree item that consists of a single label */ |
| 21851 | +SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){ |
| 21852 | + p = sqlite3TreeViewPush(p, moreToFollow); |
| 21853 | + sqlite3TreeViewLine(p, "%s", zLabel); |
| 21854 | +} |
| 21855 | +#endif /* SQLITE_DEBUG */ |
| 21856 | + |
| 21762 | 21857 | /* |
| 21763 | 21858 | ** variable-argument wrapper around sqlite3VXPrintf(). |
| 21764 | 21859 | */ |
| 21765 | 21860 | SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ |
| 21766 | 21861 | va_list ap; |
| | @@ -29659,11 +29754,11 @@ |
| 29659 | 29754 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 29660 | 29755 | ** |
| 29661 | 29756 | ** * An I/O method finder function called FINDER that returns a pointer |
| 29662 | 29757 | ** to the METHOD object in the previous bullet. |
| 29663 | 29758 | */ |
| 29664 | | -#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
| 29759 | +#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \ |
| 29665 | 29760 | static const sqlite3_io_methods METHOD = { \ |
| 29666 | 29761 | VERSION, /* iVersion */ \ |
| 29667 | 29762 | CLOSE, /* xClose */ \ |
| 29668 | 29763 | unixRead, /* xRead */ \ |
| 29669 | 29764 | unixWrite, /* xWrite */ \ |
| | @@ -29674,11 +29769,11 @@ |
| 29674 | 29769 | UNLOCK, /* xUnlock */ \ |
| 29675 | 29770 | CKLOCK, /* xCheckReservedLock */ \ |
| 29676 | 29771 | unixFileControl, /* xFileControl */ \ |
| 29677 | 29772 | unixSectorSize, /* xSectorSize */ \ |
| 29678 | 29773 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
| 29679 | | - unixShmMap, /* xShmMap */ \ |
| 29774 | + SHMMAP, /* xShmMap */ \ |
| 29680 | 29775 | unixShmLock, /* xShmLock */ \ |
| 29681 | 29776 | unixShmBarrier, /* xShmBarrier */ \ |
| 29682 | 29777 | unixShmUnmap, /* xShmUnmap */ \ |
| 29683 | 29778 | unixFetch, /* xFetch */ \ |
| 29684 | 29779 | unixUnfetch, /* xUnfetch */ \ |
| | @@ -29700,29 +29795,32 @@ |
| 29700 | 29795 | posixIoMethods, /* sqlite3_io_methods object name */ |
| 29701 | 29796 | 3, /* shared memory and mmap are enabled */ |
| 29702 | 29797 | unixClose, /* xClose method */ |
| 29703 | 29798 | unixLock, /* xLock method */ |
| 29704 | 29799 | unixUnlock, /* xUnlock method */ |
| 29705 | | - unixCheckReservedLock /* xCheckReservedLock method */ |
| 29800 | + unixCheckReservedLock, /* xCheckReservedLock method */ |
| 29801 | + unixShmMap /* xShmMap method */ |
| 29706 | 29802 | ) |
| 29707 | 29803 | IOMETHODS( |
| 29708 | 29804 | nolockIoFinder, /* Finder function name */ |
| 29709 | 29805 | nolockIoMethods, /* sqlite3_io_methods object name */ |
| 29710 | 29806 | 3, /* shared memory is disabled */ |
| 29711 | 29807 | nolockClose, /* xClose method */ |
| 29712 | 29808 | nolockLock, /* xLock method */ |
| 29713 | 29809 | nolockUnlock, /* xUnlock method */ |
| 29714 | | - nolockCheckReservedLock /* xCheckReservedLock method */ |
| 29810 | + nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 29811 | + 0 /* xShmMap method */ |
| 29715 | 29812 | ) |
| 29716 | 29813 | IOMETHODS( |
| 29717 | 29814 | dotlockIoFinder, /* Finder function name */ |
| 29718 | 29815 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
| 29719 | 29816 | 1, /* shared memory is disabled */ |
| 29720 | 29817 | dotlockClose, /* xClose method */ |
| 29721 | 29818 | dotlockLock, /* xLock method */ |
| 29722 | 29819 | dotlockUnlock, /* xUnlock method */ |
| 29723 | | - dotlockCheckReservedLock /* xCheckReservedLock method */ |
| 29820 | + dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 29821 | + 0 /* xShmMap method */ |
| 29724 | 29822 | ) |
| 29725 | 29823 | |
| 29726 | 29824 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
| 29727 | 29825 | IOMETHODS( |
| 29728 | 29826 | flockIoFinder, /* Finder function name */ |
| | @@ -29729,11 +29827,12 @@ |
| 29729 | 29827 | flockIoMethods, /* sqlite3_io_methods object name */ |
| 29730 | 29828 | 1, /* shared memory is disabled */ |
| 29731 | 29829 | flockClose, /* xClose method */ |
| 29732 | 29830 | flockLock, /* xLock method */ |
| 29733 | 29831 | flockUnlock, /* xUnlock method */ |
| 29734 | | - flockCheckReservedLock /* xCheckReservedLock method */ |
| 29832 | + flockCheckReservedLock, /* xCheckReservedLock method */ |
| 29833 | + 0 /* xShmMap method */ |
| 29735 | 29834 | ) |
| 29736 | 29835 | #endif |
| 29737 | 29836 | |
| 29738 | 29837 | #if OS_VXWORKS |
| 29739 | 29838 | IOMETHODS( |
| | @@ -29741,11 +29840,12 @@ |
| 29741 | 29840 | semIoMethods, /* sqlite3_io_methods object name */ |
| 29742 | 29841 | 1, /* shared memory is disabled */ |
| 29743 | 29842 | semClose, /* xClose method */ |
| 29744 | 29843 | semLock, /* xLock method */ |
| 29745 | 29844 | semUnlock, /* xUnlock method */ |
| 29746 | | - semCheckReservedLock /* xCheckReservedLock method */ |
| 29845 | + semCheckReservedLock, /* xCheckReservedLock method */ |
| 29846 | + 0 /* xShmMap method */ |
| 29747 | 29847 | ) |
| 29748 | 29848 | #endif |
| 29749 | 29849 | |
| 29750 | 29850 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 29751 | 29851 | IOMETHODS( |
| | @@ -29753,11 +29853,12 @@ |
| 29753 | 29853 | afpIoMethods, /* sqlite3_io_methods object name */ |
| 29754 | 29854 | 1, /* shared memory is disabled */ |
| 29755 | 29855 | afpClose, /* xClose method */ |
| 29756 | 29856 | afpLock, /* xLock method */ |
| 29757 | 29857 | afpUnlock, /* xUnlock method */ |
| 29758 | | - afpCheckReservedLock /* xCheckReservedLock method */ |
| 29858 | + afpCheckReservedLock, /* xCheckReservedLock method */ |
| 29859 | + 0 /* xShmMap method */ |
| 29759 | 29860 | ) |
| 29760 | 29861 | #endif |
| 29761 | 29862 | |
| 29762 | 29863 | /* |
| 29763 | 29864 | ** The proxy locking method is a "super-method" in the sense that it |
| | @@ -29778,11 +29879,12 @@ |
| 29778 | 29879 | proxyIoMethods, /* sqlite3_io_methods object name */ |
| 29779 | 29880 | 1, /* shared memory is disabled */ |
| 29780 | 29881 | proxyClose, /* xClose method */ |
| 29781 | 29882 | proxyLock, /* xLock method */ |
| 29782 | 29883 | proxyUnlock, /* xUnlock method */ |
| 29783 | | - proxyCheckReservedLock /* xCheckReservedLock method */ |
| 29884 | + proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 29885 | + 0 /* xShmMap method */ |
| 29784 | 29886 | ) |
| 29785 | 29887 | #endif |
| 29786 | 29888 | |
| 29787 | 29889 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 29788 | 29890 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| | @@ -29791,11 +29893,12 @@ |
| 29791 | 29893 | nfsIoMethods, /* sqlite3_io_methods object name */ |
| 29792 | 29894 | 1, /* shared memory is disabled */ |
| 29793 | 29895 | unixClose, /* xClose method */ |
| 29794 | 29896 | unixLock, /* xLock method */ |
| 29795 | 29897 | nfsUnlock, /* xUnlock method */ |
| 29796 | | - unixCheckReservedLock /* xCheckReservedLock method */ |
| 29898 | + unixCheckReservedLock, /* xCheckReservedLock method */ |
| 29899 | + 0 /* xShmMap method */ |
| 29797 | 29900 | ) |
| 29798 | 29901 | #endif |
| 29799 | 29902 | |
| 29800 | 29903 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 29801 | 29904 | /* |
| | @@ -51383,13 +51486,14 @@ |
| 51383 | 51486 | ** stored in MemPage.pBt->mutex. |
| 51384 | 51487 | */ |
| 51385 | 51488 | struct MemPage { |
| 51386 | 51489 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
| 51387 | 51490 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 51388 | | - u8 intKey; /* True if intkey flag is set */ |
| 51389 | | - u8 leaf; /* True if leaf flag is set */ |
| 51390 | | - u8 hasData; /* True if this page stores data */ |
| 51491 | + u8 intKey; /* True if table b-trees. False for index b-trees */ |
| 51492 | + u8 intKeyLeaf; /* True if the leaf of an intKey table */ |
| 51493 | + u8 noPayload; /* True if internal intKey page (thus w/o data) */ |
| 51494 | + u8 leaf; /* True if a leaf page */ |
| 51391 | 51495 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
| 51392 | 51496 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
| 51393 | 51497 | u8 max1bytePayload; /* min(maxLocal,127) */ |
| 51394 | 51498 | u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ |
| 51395 | 51499 | u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ |
| | @@ -51566,16 +51670,14 @@ |
| 51566 | 51670 | ** about a cell. The parseCellPtr() function fills in this structure |
| 51567 | 51671 | ** based on information extract from the raw disk page. |
| 51568 | 51672 | */ |
| 51569 | 51673 | typedef struct CellInfo CellInfo; |
| 51570 | 51674 | struct CellInfo { |
| 51571 | | - i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 51572 | | - u8 *pCell; /* Pointer to the start of cell content */ |
| 51573 | | - u32 nData; /* Number of bytes of data */ |
| 51574 | | - u32 nPayload; /* Total amount of payload */ |
| 51575 | | - u16 nHeader; /* Size of the cell content header in bytes */ |
| 51576 | | - u16 nLocal; /* Amount of payload held locally */ |
| 51675 | + i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ |
| 51676 | + u8 *pPayload; /* Pointer to the start of payload */ |
| 51677 | + u32 nPayload; /* Bytes of payload */ |
| 51678 | + u16 nLocal; /* Amount of payload held locally, not on overflow */ |
| 51577 | 51679 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
| 51578 | 51680 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
| 51579 | 51681 | }; |
| 51580 | 51682 | |
| 51581 | 51683 | /* |
| | @@ -51768,10 +51870,12 @@ |
| 51768 | 51870 | u8 *aPgRef; /* 1 bit per page in the db (see above) */ |
| 51769 | 51871 | Pgno nPage; /* Number of pages in the database */ |
| 51770 | 51872 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 51771 | 51873 | int nErr; /* Number of messages written to zErrMsg so far */ |
| 51772 | 51874 | int mallocFailed; /* A memory allocation error has occurred */ |
| 51875 | + const char *zPfx; /* Error message prefix */ |
| 51876 | + int v1, v2; /* Values for up to two %d fields in zPfx */ |
| 51773 | 51877 | StrAccum errMsg; /* Accumulate the error message text here */ |
| 51774 | 51878 | }; |
| 51775 | 51879 | |
| 51776 | 51880 | /* |
| 51777 | 51881 | ** Routines to read or write a two- and four-byte big-endian integer values. |
| | @@ -52554,11 +52658,13 @@ |
| 52554 | 52658 | ){ |
| 52555 | 52659 | BtCursor *p; |
| 52556 | 52660 | BtShared *pBt = pBtree->pBt; |
| 52557 | 52661 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
| 52558 | 52662 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 52559 | | - if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){ |
| 52663 | + if( (p->curFlags & BTCF_Incrblob)!=0 |
| 52664 | + && (isClearTable || p->info.nKey==iRow) |
| 52665 | + ){ |
| 52560 | 52666 | p->eState = CURSOR_INVALID; |
| 52561 | 52667 | } |
| 52562 | 52668 | } |
| 52563 | 52669 | } |
| 52564 | 52670 | |
| | @@ -52727,13 +52833,13 @@ |
| 52727 | 52833 | ** the cursors if and when a cursor is found that actually requires saving. |
| 52728 | 52834 | ** The common case is that no cursors need to be saved, so this routine is |
| 52729 | 52835 | ** broken out from its caller to avoid unnecessary stack pointer movement. |
| 52730 | 52836 | */ |
| 52731 | 52837 | static int SQLITE_NOINLINE saveCursorsOnList( |
| 52732 | | - BtCursor *p, /* The first cursor that needs saving */ |
| 52733 | | - Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ |
| 52734 | | - BtCursor *pExcept /* Do not save this cursor */ |
| 52838 | + BtCursor *p, /* The first cursor that needs saving */ |
| 52839 | + Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ |
| 52840 | + BtCursor *pExcept /* Do not save this cursor */ |
| 52735 | 52841 | ){ |
| 52736 | 52842 | do{ |
| 52737 | 52843 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ |
| 52738 | 52844 | if( p->eState==CURSOR_VALID ){ |
| 52739 | 52845 | int rc = saveCursorPosition(p); |
| | @@ -53035,51 +53141,48 @@ |
| 53035 | 53141 | /* |
| 53036 | 53142 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 53037 | 53143 | ** are two versions of this function. btreeParseCell() takes a |
| 53038 | 53144 | ** cell index as the second argument and btreeParseCellPtr() |
| 53039 | 53145 | ** takes a pointer to the body of the cell as its second argument. |
| 53040 | | -** |
| 53041 | | -** Within this file, the parseCell() macro can be called instead of |
| 53042 | | -** btreeParseCellPtr(). Using some compilers, this will be faster. |
| 53043 | 53146 | */ |
| 53044 | 53147 | static void btreeParseCellPtr( |
| 53045 | 53148 | MemPage *pPage, /* Page containing the cell */ |
| 53046 | 53149 | u8 *pCell, /* Pointer to the cell text. */ |
| 53047 | 53150 | CellInfo *pInfo /* Fill in this structure */ |
| 53048 | 53151 | ){ |
| 53049 | | - u16 n; /* Number bytes in cell content header */ |
| 53152 | + u8 *pIter; /* For scanning through pCell */ |
| 53050 | 53153 | u32 nPayload; /* Number of bytes of cell payload */ |
| 53051 | 53154 | |
| 53052 | 53155 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53053 | | - |
| 53054 | | - pInfo->pCell = pCell; |
| 53055 | 53156 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
| 53056 | | - n = pPage->childPtrSize; |
| 53057 | | - assert( n==4-4*pPage->leaf ); |
| 53058 | | - if( pPage->intKey ){ |
| 53059 | | - if( pPage->hasData ){ |
| 53060 | | - assert( n==0 ); |
| 53061 | | - n = getVarint32(pCell, nPayload); |
| 53062 | | - }else{ |
| 53063 | | - nPayload = 0; |
| 53064 | | - } |
| 53065 | | - n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
| 53066 | | - pInfo->nData = nPayload; |
| 53067 | | - }else{ |
| 53068 | | - pInfo->nData = 0; |
| 53069 | | - n += getVarint32(&pCell[n], nPayload); |
| 53157 | + if( pPage->intKeyLeaf ){ |
| 53158 | + assert( pPage->childPtrSize==0 ); |
| 53159 | + pIter = pCell + getVarint32(pCell, nPayload); |
| 53160 | + pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
| 53161 | + }else if( pPage->noPayload ){ |
| 53162 | + assert( pPage->childPtrSize==4 ); |
| 53163 | + pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 53164 | + pInfo->nPayload = 0; |
| 53165 | + pInfo->nLocal = 0; |
| 53166 | + pInfo->iOverflow = 0; |
| 53167 | + pInfo->pPayload = 0; |
| 53168 | + return; |
| 53169 | + }else{ |
| 53170 | + pIter = pCell + pPage->childPtrSize; |
| 53171 | + pIter += getVarint32(pIter, nPayload); |
| 53070 | 53172 | pInfo->nKey = nPayload; |
| 53071 | 53173 | } |
| 53072 | 53174 | pInfo->nPayload = nPayload; |
| 53073 | | - pInfo->nHeader = n; |
| 53175 | + pInfo->pPayload = pIter; |
| 53074 | 53176 | testcase( nPayload==pPage->maxLocal ); |
| 53075 | 53177 | testcase( nPayload==pPage->maxLocal+1 ); |
| 53076 | | - if( likely(nPayload<=pPage->maxLocal) ){ |
| 53178 | + if( nPayload<=pPage->maxLocal ){ |
| 53077 | 53179 | /* This is the (easy) common case where the entire payload fits |
| 53078 | 53180 | ** on the local page. No overflow is required. |
| 53079 | 53181 | */ |
| 53080 | | - if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4; |
| 53182 | + pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| 53183 | + if( pInfo->nSize<4 ) pInfo->nSize = 4; |
| 53081 | 53184 | pInfo->nLocal = (u16)nPayload; |
| 53082 | 53185 | pInfo->iOverflow = 0; |
| 53083 | 53186 | }else{ |
| 53084 | 53187 | /* If the payload will not fit completely on the local page, we have |
| 53085 | 53188 | ** to decide how much to store locally and how much to spill onto |
| | @@ -53102,33 +53205,32 @@ |
| 53102 | 53205 | if( surplus <= maxLocal ){ |
| 53103 | 53206 | pInfo->nLocal = (u16)surplus; |
| 53104 | 53207 | }else{ |
| 53105 | 53208 | pInfo->nLocal = (u16)minLocal; |
| 53106 | 53209 | } |
| 53107 | | - pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
| 53210 | + pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
| 53108 | 53211 | pInfo->nSize = pInfo->iOverflow + 4; |
| 53109 | 53212 | } |
| 53110 | 53213 | } |
| 53111 | | -#define parseCell(pPage, iCell, pInfo) \ |
| 53112 | | - btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
| 53113 | 53214 | static void btreeParseCell( |
| 53114 | 53215 | MemPage *pPage, /* Page containing the cell */ |
| 53115 | 53216 | int iCell, /* The cell index. First cell is 0 */ |
| 53116 | 53217 | CellInfo *pInfo /* Fill in this structure */ |
| 53117 | 53218 | ){ |
| 53118 | | - parseCell(pPage, iCell, pInfo); |
| 53219 | + btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 53119 | 53220 | } |
| 53120 | 53221 | |
| 53121 | 53222 | /* |
| 53122 | 53223 | ** Compute the total number of bytes that a Cell needs in the cell |
| 53123 | 53224 | ** data area of the btree-page. The return number includes the cell |
| 53124 | 53225 | ** data header and the local payload, but not any overflow page or |
| 53125 | 53226 | ** the space used by the cell pointer. |
| 53126 | 53227 | */ |
| 53127 | 53228 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 53128 | | - u8 *pIter = &pCell[pPage->childPtrSize]; |
| 53129 | | - u32 nSize; |
| 53229 | + u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 53230 | + u8 *pEnd; /* End mark for a varint */ |
| 53231 | + u32 nSize; /* Size value to return */ |
| 53130 | 53232 | |
| 53131 | 53233 | #ifdef SQLITE_DEBUG |
| 53132 | 53234 | /* The value returned by this function should always be the same as |
| 53133 | 53235 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 53134 | 53236 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| | @@ -53135,47 +53237,48 @@ |
| 53135 | 53237 | ** this function verifies that this invariant is not violated. */ |
| 53136 | 53238 | CellInfo debuginfo; |
| 53137 | 53239 | btreeParseCellPtr(pPage, pCell, &debuginfo); |
| 53138 | 53240 | #endif |
| 53139 | 53241 | |
| 53242 | + if( pPage->noPayload ){ |
| 53243 | + pEnd = &pIter[9]; |
| 53244 | + while( (*pIter++)&0x80 && pIter<pEnd ); |
| 53245 | + assert( pPage->childPtrSize==4 ); |
| 53246 | + return (u16)(pIter - pCell); |
| 53247 | + } |
| 53248 | + nSize = *pIter; |
| 53249 | + if( nSize>=0x80 ){ |
| 53250 | + pEnd = &pIter[9]; |
| 53251 | + nSize &= 0x7f; |
| 53252 | + do{ |
| 53253 | + nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 53254 | + }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 53255 | + } |
| 53256 | + pIter++; |
| 53140 | 53257 | if( pPage->intKey ){ |
| 53141 | | - u8 *pEnd; |
| 53142 | | - if( pPage->hasData ){ |
| 53143 | | - pIter += getVarint32(pIter, nSize); |
| 53144 | | - }else{ |
| 53145 | | - nSize = 0; |
| 53146 | | - } |
| 53147 | | - |
| 53148 | 53258 | /* pIter now points at the 64-bit integer key value, a variable length |
| 53149 | 53259 | ** integer. The following block moves pIter to point at the first byte |
| 53150 | 53260 | ** past the end of the key value. */ |
| 53151 | 53261 | pEnd = &pIter[9]; |
| 53152 | 53262 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 53153 | | - }else{ |
| 53154 | | - pIter += getVarint32(pIter, nSize); |
| 53155 | 53263 | } |
| 53156 | | - |
| 53157 | 53264 | testcase( nSize==pPage->maxLocal ); |
| 53158 | 53265 | testcase( nSize==pPage->maxLocal+1 ); |
| 53159 | | - if( nSize>pPage->maxLocal ){ |
| 53266 | + if( nSize<=pPage->maxLocal ){ |
| 53267 | + nSize += (u32)(pIter - pCell); |
| 53268 | + if( nSize<4 ) nSize = 4; |
| 53269 | + }else{ |
| 53160 | 53270 | int minLocal = pPage->minLocal; |
| 53161 | 53271 | nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
| 53162 | 53272 | testcase( nSize==pPage->maxLocal ); |
| 53163 | 53273 | testcase( nSize==pPage->maxLocal+1 ); |
| 53164 | 53274 | if( nSize>pPage->maxLocal ){ |
| 53165 | 53275 | nSize = minLocal; |
| 53166 | 53276 | } |
| 53167 | | - nSize += 4; |
| 53168 | | - } |
| 53169 | | - nSize += (u32)(pIter - pCell); |
| 53170 | | - |
| 53171 | | - /* The minimum size of any cell is 4 bytes. */ |
| 53172 | | - if( nSize<4 ){ |
| 53173 | | - nSize = 4; |
| 53174 | | - } |
| 53175 | | - |
| 53176 | | - assert( nSize==debuginfo.nSize ); |
| 53277 | + nSize += 4 + (u16)(pIter - pCell); |
| 53278 | + } |
| 53279 | + assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
| 53177 | 53280 | return (u16)nSize; |
| 53178 | 53281 | } |
| 53179 | 53282 | |
| 53180 | 53283 | #ifdef SQLITE_DEBUG |
| 53181 | 53284 | /* This variation on cellSizePtr() is used inside of assert() statements |
| | @@ -53194,11 +53297,10 @@ |
| 53194 | 53297 | static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
| 53195 | 53298 | CellInfo info; |
| 53196 | 53299 | if( *pRC ) return; |
| 53197 | 53300 | assert( pCell!=0 ); |
| 53198 | 53301 | btreeParseCellPtr(pPage, pCell, &info); |
| 53199 | | - assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 53200 | 53302 | if( info.iOverflow ){ |
| 53201 | 53303 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 53202 | 53304 | ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
| 53203 | 53305 | } |
| 53204 | 53306 | } |
| | @@ -53407,11 +53509,11 @@ |
| 53407 | 53509 | ** does it detect cells or freeblocks that encrouch into the reserved bytes |
| 53408 | 53510 | ** at the end of the page. So do additional corruption checks inside this |
| 53409 | 53511 | ** routine and return SQLITE_CORRUPT if any problems are found. |
| 53410 | 53512 | */ |
| 53411 | 53513 | static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
| 53412 | | - u16 iPtr; /* Address of pointer to next freeblock */ |
| 53514 | + u16 iPtr; /* Address of ptr to next freeblock */ |
| 53413 | 53515 | u16 iFreeBlk; /* Address of the next freeblock */ |
| 53414 | 53516 | u8 hdr; /* Page header size. 0 or 100 */ |
| 53415 | 53517 | u8 nFrag = 0; /* Reduction in fragmentation */ |
| 53416 | 53518 | u16 iOrigSize = iSize; /* Original value of iSize */ |
| 53417 | 53519 | u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ |
| | @@ -53459,13 +53561,13 @@ |
| 53459 | 53561 | iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
| 53460 | 53562 | iSize = iEnd - iStart; |
| 53461 | 53563 | iFreeBlk = get2byte(&data[iFreeBlk]); |
| 53462 | 53564 | } |
| 53463 | 53565 | |
| 53464 | | - /* If iPtr is another freeblock (that is, if iPtr is not the freelist pointer |
| 53465 | | - ** in the page header) then check to see if iStart should be coalesced |
| 53466 | | - ** onto the end of iPtr. |
| 53566 | + /* If iPtr is another freeblock (that is, if iPtr is not the freelist |
| 53567 | + ** pointer in the page header) then check to see if iStart should be |
| 53568 | + ** coalesced onto the end of iPtr. |
| 53467 | 53569 | */ |
| 53468 | 53570 | if( iPtr>hdr+1 ){ |
| 53469 | 53571 | int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); |
| 53470 | 53572 | if( iPtrEnd+3>=iStart ){ |
| 53471 | 53573 | if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; |
| | @@ -53515,16 +53617,18 @@ |
| 53515 | 53617 | flagByte &= ~PTF_LEAF; |
| 53516 | 53618 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53517 | 53619 | pBt = pPage->pBt; |
| 53518 | 53620 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 53519 | 53621 | pPage->intKey = 1; |
| 53520 | | - pPage->hasData = pPage->leaf; |
| 53622 | + pPage->intKeyLeaf = pPage->leaf; |
| 53623 | + pPage->noPayload = !pPage->leaf; |
| 53521 | 53624 | pPage->maxLocal = pBt->maxLeaf; |
| 53522 | 53625 | pPage->minLocal = pBt->minLeaf; |
| 53523 | 53626 | }else if( flagByte==PTF_ZERODATA ){ |
| 53524 | 53627 | pPage->intKey = 0; |
| 53525 | | - pPage->hasData = 0; |
| 53628 | + pPage->intKeyLeaf = 0; |
| 53629 | + pPage->noPayload = 0; |
| 53526 | 53630 | pPage->maxLocal = pBt->maxLocal; |
| 53527 | 53631 | pPage->minLocal = pBt->minLocal; |
| 53528 | 53632 | }else{ |
| 53529 | 53633 | return SQLITE_CORRUPT_BKPT; |
| 53530 | 53634 | } |
| | @@ -54694,15 +54798,15 @@ |
| 54694 | 54798 | */ |
| 54695 | 54799 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 54696 | 54800 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 54697 | 54801 | assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); |
| 54698 | 54802 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
| 54699 | | - assert( pBt->pPage1->aData ); |
| 54803 | + MemPage *pPage1 = pBt->pPage1; |
| 54804 | + assert( pPage1->aData ); |
| 54700 | 54805 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
| 54701 | | - assert( pBt->pPage1->aData ); |
| 54702 | | - releasePage(pBt->pPage1); |
| 54703 | 54806 | pBt->pPage1 = 0; |
| 54807 | + releasePage(pPage1); |
| 54704 | 54808 | } |
| 54705 | 54809 | } |
| 54706 | 54810 | |
| 54707 | 54811 | /* |
| 54708 | 54812 | ** If pBt points to an empty file then convert that empty file |
| | @@ -55739,10 +55843,14 @@ |
| 55739 | 55843 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 55740 | 55844 | |
| 55741 | 55845 | if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
| 55742 | 55846 | return SQLITE_READONLY; |
| 55743 | 55847 | } |
| 55848 | + if( wrFlag ){ |
| 55849 | + allocateTempSpace(pBt); |
| 55850 | + if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; |
| 55851 | + } |
| 55744 | 55852 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 55745 | 55853 | assert( wrFlag==0 ); |
| 55746 | 55854 | iTable = 0; |
| 55747 | 55855 | } |
| 55748 | 55856 | |
| | @@ -55928,12 +56036,13 @@ |
| 55928 | 56036 | ** to return an integer result code for historical reasons. |
| 55929 | 56037 | */ |
| 55930 | 56038 | SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
| 55931 | 56039 | assert( cursorHoldsMutex(pCur) ); |
| 55932 | 56040 | assert( pCur->eState==CURSOR_VALID ); |
| 56041 | + assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 ); |
| 55933 | 56042 | getCellInfo(pCur); |
| 55934 | | - *pSize = pCur->info.nData; |
| 56043 | + *pSize = pCur->info.nPayload; |
| 55935 | 56044 | return SQLITE_OK; |
| 55936 | 56045 | } |
| 55937 | 56046 | |
| 55938 | 56047 | /* |
| 55939 | 56048 | ** Given the page number of an overflow page in the database (parameter |
| | @@ -56080,34 +56189,32 @@ |
| 56080 | 56189 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 56081 | 56190 | int eOp /* zero to read. non-zero to write. */ |
| 56082 | 56191 | ){ |
| 56083 | 56192 | unsigned char *aPayload; |
| 56084 | 56193 | int rc = SQLITE_OK; |
| 56085 | | - u32 nKey; |
| 56086 | 56194 | int iIdx = 0; |
| 56087 | 56195 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
| 56088 | 56196 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
| 56089 | 56197 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 56090 | | - int bEnd; /* True if reading to end of data */ |
| 56198 | + unsigned char * const pBufStart = pBuf; |
| 56199 | + int bEnd; /* True if reading to end of data */ |
| 56091 | 56200 | #endif |
| 56092 | 56201 | |
| 56093 | 56202 | assert( pPage ); |
| 56094 | 56203 | assert( pCur->eState==CURSOR_VALID ); |
| 56095 | 56204 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 56096 | 56205 | assert( cursorHoldsMutex(pCur) ); |
| 56097 | | - assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
| 56206 | + assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
| 56098 | 56207 | |
| 56099 | 56208 | getCellInfo(pCur); |
| 56100 | | - aPayload = pCur->info.pCell + pCur->info.nHeader; |
| 56101 | | - nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
| 56209 | + aPayload = pCur->info.pPayload; |
| 56102 | 56210 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 56103 | | - bEnd = (offset+amt==nKey+pCur->info.nData); |
| 56211 | + bEnd = offset+amt==pCur->info.nPayload; |
| 56104 | 56212 | #endif |
| 56213 | + assert( offset+amt <= pCur->info.nPayload ); |
| 56105 | 56214 | |
| 56106 | | - if( NEVER(offset+amt > nKey+pCur->info.nData) |
| 56107 | | - || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 56108 | | - ){ |
| 56215 | + if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ |
| 56109 | 56216 | /* Trying to read or write past the end of the data is an error */ |
| 56110 | 56217 | return SQLITE_CORRUPT_BKPT; |
| 56111 | 56218 | } |
| 56112 | 56219 | |
| 56113 | 56220 | /* Check if data must be read/written to/from the btree page itself. */ |
| | @@ -56159,11 +56266,13 @@ |
| 56159 | 56266 | |
| 56160 | 56267 | /* If the overflow page-list cache has been allocated and the |
| 56161 | 56268 | ** entry for the first required overflow page is valid, skip |
| 56162 | 56269 | ** directly to it. |
| 56163 | 56270 | */ |
| 56164 | | - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){ |
| 56271 | + if( (pCur->curFlags & BTCF_ValidOvfl)!=0 |
| 56272 | + && pCur->aOverflow[offset/ovflSize] |
| 56273 | + ){ |
| 56165 | 56274 | iIdx = (offset/ovflSize); |
| 56166 | 56275 | nextPage = pCur->aOverflow[iIdx]; |
| 56167 | 56276 | offset = (offset%ovflSize); |
| 56168 | 56277 | } |
| 56169 | 56278 | |
| | @@ -56212,10 +56321,11 @@ |
| 56212 | 56321 | ** 2) data is required from the start of this overflow page, and |
| 56213 | 56322 | ** 3) the database is file-backed, and |
| 56214 | 56323 | ** 4) there is no open write-transaction, and |
| 56215 | 56324 | ** 5) the database is not a WAL database, |
| 56216 | 56325 | ** 6) all data from the page is being read. |
| 56326 | + ** 7) at least 4 bytes have already been read into the output buffer |
| 56217 | 56327 | ** |
| 56218 | 56328 | ** then data can be read directly from the database file into the |
| 56219 | 56329 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 56220 | 56330 | ** up loading large records that span many overflow pages. |
| 56221 | 56331 | */ |
| | @@ -56223,13 +56333,15 @@ |
| 56223 | 56333 | && offset==0 /* (2) */ |
| 56224 | 56334 | && (bEnd || a==ovflSize) /* (6) */ |
| 56225 | 56335 | && pBt->inTransaction==TRANS_READ /* (4) */ |
| 56226 | 56336 | && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 56227 | 56337 | && pBt->pPage1->aData[19]==0x01 /* (5) */ |
| 56338 | + && &pBuf[-4]>=pBufStart /* (7) */ |
| 56228 | 56339 | ){ |
| 56229 | 56340 | u8 aSave[4]; |
| 56230 | 56341 | u8 *aWrite = &pBuf[-4]; |
| 56342 | + assert( aWrite>=pBufStart ); /* hence (7) */ |
| 56231 | 56343 | memcpy(aSave, aWrite, 4); |
| 56232 | 56344 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| 56233 | 56345 | nextPage = get4byte(aWrite); |
| 56234 | 56346 | memcpy(aWrite, aSave, 4); |
| 56235 | 56347 | }else |
| | @@ -56337,11 +56449,11 @@ |
| 56337 | 56449 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 56338 | 56450 | assert( cursorHoldsMutex(pCur) ); |
| 56339 | 56451 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 56340 | 56452 | assert( pCur->info.nSize>0 ); |
| 56341 | 56453 | *pAmt = pCur->info.nLocal; |
| 56342 | | - return (void*)(pCur->info.pCell + pCur->info.nHeader); |
| 56454 | + return (void*)pCur->info.pPayload; |
| 56343 | 56455 | } |
| 56344 | 56456 | |
| 56345 | 56457 | |
| 56346 | 56458 | /* |
| 56347 | 56459 | ** For the entry that cursor pCur is point to, return as |
| | @@ -56765,11 +56877,11 @@ |
| 56765 | 56877 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 56766 | 56878 | if( xRecordCompare==0 ){ |
| 56767 | 56879 | for(;;){ |
| 56768 | 56880 | i64 nCellKey; |
| 56769 | 56881 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 56770 | | - if( pPage->hasData ){ |
| 56882 | + if( pPage->intKeyLeaf ){ |
| 56771 | 56883 | while( 0x80 <= *(pCell++) ){ |
| 56772 | 56884 | if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
| 56773 | 56885 | } |
| 56774 | 56886 | } |
| 56775 | 56887 | getVarint(pCell, (u64*)&nCellKey); |
| | @@ -57024,13 +57136,13 @@ |
| 57024 | 57136 | ** was already pointing to the first entry in the database before |
| 57025 | 57137 | ** this routine was called, then set *pRes=1. |
| 57026 | 57138 | ** |
| 57027 | 57139 | ** The main entry point is sqlite3BtreePrevious(). That routine is optimized |
| 57028 | 57140 | ** for the common case of merely decrementing the cell counter BtCursor.aiIdx |
| 57029 | | -** to the previous cell on the current page. The (slower) btreePrevious() helper |
| 57030 | | -** routine is called when it is necessary to move to a different page or |
| 57031 | | -** to restore the cursor. |
| 57141 | +** to the previous cell on the current page. The (slower) btreePrevious() |
| 57142 | +** helper routine is called when it is necessary to move to a different page |
| 57143 | +** or to restore the cursor. |
| 57032 | 57144 | ** |
| 57033 | 57145 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 57034 | 57146 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 57035 | 57147 | ** if this routine could have been skipped if that SQL index had been |
| 57036 | 57148 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| | @@ -57048,12 +57160,11 @@ |
| 57048 | 57160 | assert( *pRes==0 ); |
| 57049 | 57161 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 57050 | 57162 | assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); |
| 57051 | 57163 | assert( pCur->info.nSize==0 ); |
| 57052 | 57164 | if( pCur->eState!=CURSOR_VALID ){ |
| 57053 | | - assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 57054 | | - rc = btreeRestoreCursorPosition(pCur); |
| 57165 | + rc = restoreCursorPosition(pCur); |
| 57055 | 57166 | if( rc!=SQLITE_OK ){ |
| 57056 | 57167 | return rc; |
| 57057 | 57168 | } |
| 57058 | 57169 | if( CURSOR_INVALID==pCur->eState ){ |
| 57059 | 57170 | *pRes = 1; |
| | @@ -57354,11 +57465,11 @@ |
| 57354 | 57465 | if( rc ) goto end_allocate_page; |
| 57355 | 57466 | if( closest<k-1 ){ |
| 57356 | 57467 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 57357 | 57468 | } |
| 57358 | 57469 | put4byte(&aData[4], k-1); |
| 57359 | | - noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0; |
| 57470 | + noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
| 57360 | 57471 | rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
| 57361 | 57472 | if( rc==SQLITE_OK ){ |
| 57362 | 57473 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 57363 | 57474 | if( rc!=SQLITE_OK ){ |
| 57364 | 57475 | releasePage(*ppPage); |
| | @@ -57387,11 +57498,11 @@ |
| 57387 | 57498 | ** content for any page that really does lie past the end of the database |
| 57388 | 57499 | ** file on disk. So the effects of disabling the no-content optimization |
| 57389 | 57500 | ** here are confined to those pages that lie between the end of the |
| 57390 | 57501 | ** database image and the end of the database file. |
| 57391 | 57502 | */ |
| 57392 | | - int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0; |
| 57503 | + int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; |
| 57393 | 57504 | |
| 57394 | 57505 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 57395 | 57506 | if( rc ) return rc; |
| 57396 | 57507 | pBt->nPage++; |
| 57397 | 57508 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
| | @@ -57586,22 +57697,29 @@ |
| 57586 | 57697 | *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); |
| 57587 | 57698 | } |
| 57588 | 57699 | } |
| 57589 | 57700 | |
| 57590 | 57701 | /* |
| 57591 | | -** Free any overflow pages associated with the given Cell. |
| 57702 | +** Free any overflow pages associated with the given Cell. Write the |
| 57703 | +** local Cell size (the number of bytes on the original page, omitting |
| 57704 | +** overflow) into *pnSize. |
| 57592 | 57705 | */ |
| 57593 | | -static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 57706 | +static int clearCell( |
| 57707 | + MemPage *pPage, /* The page that contains the Cell */ |
| 57708 | + unsigned char *pCell, /* First byte of the Cell */ |
| 57709 | + u16 *pnSize /* Write the size of the Cell here */ |
| 57710 | +){ |
| 57594 | 57711 | BtShared *pBt = pPage->pBt; |
| 57595 | 57712 | CellInfo info; |
| 57596 | 57713 | Pgno ovflPgno; |
| 57597 | 57714 | int rc; |
| 57598 | 57715 | int nOvfl; |
| 57599 | 57716 | u32 ovflPageSize; |
| 57600 | 57717 | |
| 57601 | 57718 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 57602 | 57719 | btreeParseCellPtr(pPage, pCell, &info); |
| 57720 | + *pnSize = info.nSize; |
| 57603 | 57721 | if( info.iOverflow==0 ){ |
| 57604 | 57722 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| 57605 | 57723 | } |
| 57606 | 57724 | if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
| 57607 | 57725 | return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ |
| | @@ -57681,54 +57799,87 @@ |
| 57681 | 57799 | unsigned char *pPrior; |
| 57682 | 57800 | unsigned char *pPayload; |
| 57683 | 57801 | BtShared *pBt = pPage->pBt; |
| 57684 | 57802 | Pgno pgnoOvfl = 0; |
| 57685 | 57803 | int nHeader; |
| 57686 | | - CellInfo info; |
| 57687 | 57804 | |
| 57688 | 57805 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 57689 | 57806 | |
| 57690 | 57807 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 57691 | 57808 | ** buffer space that is separate from the pPage buffer area */ |
| 57692 | 57809 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 57693 | 57810 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 57694 | 57811 | |
| 57695 | 57812 | /* Fill in the header. */ |
| 57696 | | - nHeader = 0; |
| 57697 | | - if( !pPage->leaf ){ |
| 57698 | | - nHeader += 4; |
| 57699 | | - } |
| 57700 | | - if( pPage->hasData ){ |
| 57701 | | - nHeader += putVarint32(&pCell[nHeader], nData+nZero); |
| 57813 | + nHeader = pPage->childPtrSize; |
| 57814 | + nPayload = nData + nZero; |
| 57815 | + if( pPage->intKeyLeaf ){ |
| 57816 | + nHeader += putVarint32(&pCell[nHeader], nPayload); |
| 57702 | 57817 | }else{ |
| 57703 | | - nData = nZero = 0; |
| 57818 | + assert( nData==0 ); |
| 57819 | + assert( nZero==0 ); |
| 57704 | 57820 | } |
| 57705 | 57821 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
| 57706 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 57707 | | - assert( info.nHeader==nHeader ); |
| 57708 | | - assert( info.nKey==nKey ); |
| 57709 | | - assert( info.nData==(u32)(nData+nZero) ); |
| 57710 | 57822 | |
| 57711 | | - /* Fill in the payload */ |
| 57712 | | - nPayload = nData + nZero; |
| 57823 | + /* Fill in the payload size */ |
| 57713 | 57824 | if( pPage->intKey ){ |
| 57714 | 57825 | pSrc = pData; |
| 57715 | 57826 | nSrc = nData; |
| 57716 | 57827 | nData = 0; |
| 57717 | 57828 | }else{ |
| 57718 | 57829 | if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 57719 | 57830 | return SQLITE_CORRUPT_BKPT; |
| 57720 | 57831 | } |
| 57721 | | - nPayload += (int)nKey; |
| 57832 | + nPayload = (int)nKey; |
| 57722 | 57833 | pSrc = pKey; |
| 57723 | 57834 | nSrc = (int)nKey; |
| 57724 | 57835 | } |
| 57725 | | - *pnSize = info.nSize; |
| 57726 | | - spaceLeft = info.nLocal; |
| 57836 | + if( nPayload<=pPage->maxLocal ){ |
| 57837 | + n = nHeader + nPayload; |
| 57838 | + testcase( n==3 ); |
| 57839 | + testcase( n==4 ); |
| 57840 | + if( n<4 ) n = 4; |
| 57841 | + *pnSize = n; |
| 57842 | + spaceLeft = nPayload; |
| 57843 | + pPrior = pCell; |
| 57844 | + }else{ |
| 57845 | + int mn = pPage->minLocal; |
| 57846 | + n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); |
| 57847 | + testcase( n==pPage->maxLocal ); |
| 57848 | + testcase( n==pPage->maxLocal+1 ); |
| 57849 | + if( n > pPage->maxLocal ) n = mn; |
| 57850 | + spaceLeft = n; |
| 57851 | + *pnSize = n + nHeader + 4; |
| 57852 | + pPrior = &pCell[nHeader+n]; |
| 57853 | + } |
| 57727 | 57854 | pPayload = &pCell[nHeader]; |
| 57728 | | - pPrior = &pCell[info.iOverflow]; |
| 57729 | 57855 | |
| 57856 | + /* At this point variables should be set as follows: |
| 57857 | + ** |
| 57858 | + ** nPayload Total payload size in bytes |
| 57859 | + ** pPayload Begin writing payload here |
| 57860 | + ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, |
| 57861 | + ** that means content must spill into overflow pages. |
| 57862 | + ** *pnSize Size of the local cell (not counting overflow pages) |
| 57863 | + ** pPrior Where to write the pgno of the first overflow page |
| 57864 | + ** |
| 57865 | + ** Use a call to btreeParseCellPtr() to verify that the values above |
| 57866 | + ** were computed correctly. |
| 57867 | + */ |
| 57868 | +#if SQLITE_DEBUG |
| 57869 | + { |
| 57870 | + CellInfo info; |
| 57871 | + btreeParseCellPtr(pPage, pCell, &info); |
| 57872 | + assert( nHeader=(int)(info.pPayload - pCell) ); |
| 57873 | + assert( info.nKey==nKey ); |
| 57874 | + assert( *pnSize == info.nSize ); |
| 57875 | + assert( spaceLeft == info.nLocal ); |
| 57876 | + assert( pPrior == &pCell[info.iOverflow] ); |
| 57877 | + } |
| 57878 | +#endif |
| 57879 | + |
| 57880 | + /* Write the payload into the local Cell and any extra into overflow pages */ |
| 57730 | 57881 | while( nPayload>0 ){ |
| 57731 | 57882 | if( spaceLeft==0 ){ |
| 57732 | 57883 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 57733 | 57884 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 57734 | 57885 | if( pBt->autoVacuum ){ |
| | @@ -58432,11 +58583,11 @@ |
| 58432 | 58583 | ** |
| 58433 | 58584 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 58434 | 58585 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
| 58435 | 58586 | */ |
| 58436 | 58587 | leafCorrection = apOld[0]->leaf*4; |
| 58437 | | - leafData = apOld[0]->hasData; |
| 58588 | + leafData = apOld[0]->intKeyLeaf; |
| 58438 | 58589 | for(i=0; i<nOld; i++){ |
| 58439 | 58590 | int limit; |
| 58440 | 58591 | |
| 58441 | 58592 | /* Before doing anything else, take a copy of the i'th original sibling |
| 58442 | 58593 | ** The rest of this function will use data from the copies rather |
| | @@ -59008,11 +59159,11 @@ |
| 59008 | 59159 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 59009 | 59160 | |
| 59010 | 59161 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| 59011 | 59162 | if( rc==SQLITE_OK ){ |
| 59012 | 59163 | #ifndef SQLITE_OMIT_QUICKBALANCE |
| 59013 | | - if( pPage->hasData |
| 59164 | + if( pPage->intKeyLeaf |
| 59014 | 59165 | && pPage->nOverflow==1 |
| 59015 | 59166 | && pPage->aiOvfl[0]==pPage->nCell |
| 59016 | 59167 | && pParent->pgno!=1 |
| 59017 | 59168 | && pParent->nCell==iIdx |
| 59018 | 59169 | ){ |
| | @@ -59127,11 +59278,12 @@ |
| 59127 | 59278 | assert( pCur->skipNext!=SQLITE_OK ); |
| 59128 | 59279 | return pCur->skipNext; |
| 59129 | 59280 | } |
| 59130 | 59281 | |
| 59131 | 59282 | assert( cursorHoldsMutex(pCur) ); |
| 59132 | | - assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE |
| 59283 | + assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 59284 | + && pBt->inTransaction==TRANS_WRITE |
| 59133 | 59285 | && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 59134 | 59286 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 59135 | 59287 | |
| 59136 | 59288 | /* Assert that the caller has been consistent. If this cursor was opened |
| 59137 | 59289 | ** expecting an index b-tree, then the caller should be inserting blob |
| | @@ -59160,11 +59312,12 @@ |
| 59160 | 59312 | invalidateIncrblobCursors(p, nKey, 0); |
| 59161 | 59313 | |
| 59162 | 59314 | /* If the cursor is currently on the last row and we are appending a |
| 59163 | 59315 | ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
| 59164 | 59316 | ** call */ |
| 59165 | | - if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){ |
| 59317 | + if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
| 59318 | + && pCur->info.nKey==nKey-1 ){ |
| 59166 | 59319 | loc = -1; |
| 59167 | 59320 | } |
| 59168 | 59321 | } |
| 59169 | 59322 | |
| 59170 | 59323 | if( !loc ){ |
| | @@ -59179,13 +59332,12 @@ |
| 59179 | 59332 | |
| 59180 | 59333 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 59181 | 59334 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 59182 | 59335 | loc==0 ? "overwrite" : "new entry")); |
| 59183 | 59336 | assert( pPage->isInit ); |
| 59184 | | - allocateTempSpace(pBt); |
| 59185 | 59337 | newCell = pBt->pTmpSpace; |
| 59186 | | - if( newCell==0 ) return SQLITE_NOMEM; |
| 59338 | + assert( newCell!=0 ); |
| 59187 | 59339 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
| 59188 | 59340 | if( rc ) goto end_insert; |
| 59189 | 59341 | assert( szNew==cellSizePtr(pPage, newCell) ); |
| 59190 | 59342 | assert( szNew <= MX_CELL_SIZE(pBt) ); |
| 59191 | 59343 | idx = pCur->aiIdx[pCur->iPage]; |
| | @@ -59198,12 +59350,11 @@ |
| 59198 | 59350 | } |
| 59199 | 59351 | oldCell = findCell(pPage, idx); |
| 59200 | 59352 | if( !pPage->leaf ){ |
| 59201 | 59353 | memcpy(newCell, oldCell, 4); |
| 59202 | 59354 | } |
| 59203 | | - szOld = cellSizePtr(pPage, oldCell); |
| 59204 | | - rc = clearCell(pPage, oldCell); |
| 59355 | + rc = clearCell(pPage, oldCell, &szOld); |
| 59205 | 59356 | dropCell(pPage, idx, szOld, &rc); |
| 59206 | 59357 | if( rc ) goto end_insert; |
| 59207 | 59358 | }else if( loc<0 && pPage->nCell>0 ){ |
| 59208 | 59359 | assert( pPage->leaf ); |
| 59209 | 59360 | idx = ++pCur->aiIdx[pCur->iPage]; |
| | @@ -59261,10 +59412,11 @@ |
| 59261 | 59412 | int rc; /* Return code */ |
| 59262 | 59413 | MemPage *pPage; /* Page to delete cell from */ |
| 59263 | 59414 | unsigned char *pCell; /* Pointer to cell to delete */ |
| 59264 | 59415 | int iCellIdx; /* Index of cell to delete */ |
| 59265 | 59416 | int iCellDepth; /* Depth of node containing pCell */ |
| 59417 | + u16 szCell; /* Size of the cell being deleted */ |
| 59266 | 59418 | |
| 59267 | 59419 | assert( cursorHoldsMutex(pCur) ); |
| 59268 | 59420 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 59269 | 59421 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 59270 | 59422 | assert( pCur->curFlags & BTCF_WriteFlag ); |
| | @@ -59309,12 +59461,12 @@ |
| 59309 | 59461 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
| 59310 | 59462 | } |
| 59311 | 59463 | |
| 59312 | 59464 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 59313 | 59465 | if( rc ) return rc; |
| 59314 | | - rc = clearCell(pPage, pCell); |
| 59315 | | - dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc); |
| 59466 | + rc = clearCell(pPage, pCell, &szCell); |
| 59467 | + dropCell(pPage, iCellIdx, szCell, &rc); |
| 59316 | 59468 | if( rc ) return rc; |
| 59317 | 59469 | |
| 59318 | 59470 | /* If the cell deleted was not located on a leaf page, then the cursor |
| 59319 | 59471 | ** is currently pointing to the largest entry in the sub-tree headed |
| 59320 | 59472 | ** by the child-page of the cell that was just deleted from an internal |
| | @@ -59327,14 +59479,12 @@ |
| 59327 | 59479 | unsigned char *pTmp; |
| 59328 | 59480 | |
| 59329 | 59481 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 59330 | 59482 | nCell = cellSizePtr(pLeaf, pCell); |
| 59331 | 59483 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
| 59332 | | - |
| 59333 | | - allocateTempSpace(pBt); |
| 59334 | 59484 | pTmp = pBt->pTmpSpace; |
| 59335 | | - |
| 59485 | + assert( pTmp!=0 ); |
| 59336 | 59486 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
| 59337 | 59487 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| 59338 | 59488 | dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); |
| 59339 | 59489 | if( rc ) return rc; |
| 59340 | 59490 | } |
| | @@ -59542,10 +59692,11 @@ |
| 59542 | 59692 | MemPage *pPage; |
| 59543 | 59693 | int rc; |
| 59544 | 59694 | unsigned char *pCell; |
| 59545 | 59695 | int i; |
| 59546 | 59696 | int hdr; |
| 59697 | + u16 szCell; |
| 59547 | 59698 | |
| 59548 | 59699 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 59549 | 59700 | if( pgno>btreePagecount(pBt) ){ |
| 59550 | 59701 | return SQLITE_CORRUPT_BKPT; |
| 59551 | 59702 | } |
| | @@ -59557,11 +59708,11 @@ |
| 59557 | 59708 | pCell = findCell(pPage, i); |
| 59558 | 59709 | if( !pPage->leaf ){ |
| 59559 | 59710 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
| 59560 | 59711 | if( rc ) goto cleardatabasepage_out; |
| 59561 | 59712 | } |
| 59562 | | - rc = clearCell(pPage, pCell); |
| 59713 | + rc = clearCell(pPage, pCell, &szCell); |
| 59563 | 59714 | if( rc ) goto cleardatabasepage_out; |
| 59564 | 59715 | } |
| 59565 | 59716 | if( !pPage->leaf ){ |
| 59566 | 59717 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
| 59567 | 59718 | if( rc ) goto cleardatabasepage_out; |
| | @@ -59903,24 +60054,25 @@ |
| 59903 | 60054 | /* |
| 59904 | 60055 | ** Append a message to the error message string. |
| 59905 | 60056 | */ |
| 59906 | 60057 | static void checkAppendMsg( |
| 59907 | 60058 | IntegrityCk *pCheck, |
| 59908 | | - char *zMsg1, |
| 59909 | 60059 | const char *zFormat, |
| 59910 | 60060 | ... |
| 59911 | 60061 | ){ |
| 59912 | 60062 | va_list ap; |
| 60063 | + char zBuf[200]; |
| 59913 | 60064 | if( !pCheck->mxErr ) return; |
| 59914 | 60065 | pCheck->mxErr--; |
| 59915 | 60066 | pCheck->nErr++; |
| 59916 | 60067 | va_start(ap, zFormat); |
| 59917 | 60068 | if( pCheck->errMsg.nChar ){ |
| 59918 | 60069 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
| 59919 | 60070 | } |
| 59920 | | - if( zMsg1 ){ |
| 59921 | | - sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1); |
| 60071 | + if( pCheck->zPfx ){ |
| 60072 | + sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2); |
| 60073 | + sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf); |
| 59922 | 60074 | } |
| 59923 | 60075 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 59924 | 60076 | va_end(ap); |
| 59925 | 60077 | if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ |
| 59926 | 60078 | pCheck->mallocFailed = 1; |
| | @@ -59954,18 +60106,18 @@ |
| 59954 | 60106 | ** Return 1 if there are 2 or more references to the page and 0 if |
| 59955 | 60107 | ** if this is the first reference to the page. |
| 59956 | 60108 | ** |
| 59957 | 60109 | ** Also check that the page number is in bounds. |
| 59958 | 60110 | */ |
| 59959 | | -static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
| 60111 | +static int checkRef(IntegrityCk *pCheck, Pgno iPage){ |
| 59960 | 60112 | if( iPage==0 ) return 1; |
| 59961 | 60113 | if( iPage>pCheck->nPage ){ |
| 59962 | | - checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
| 60114 | + checkAppendMsg(pCheck, "invalid page number %d", iPage); |
| 59963 | 60115 | return 1; |
| 59964 | 60116 | } |
| 59965 | 60117 | if( getPageReferenced(pCheck, iPage) ){ |
| 59966 | | - checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
| 60118 | + checkAppendMsg(pCheck, "2nd reference to page %d", iPage); |
| 59967 | 60119 | return 1; |
| 59968 | 60120 | } |
| 59969 | 60121 | setPageReferenced(pCheck, iPage); |
| 59970 | 60122 | return 0; |
| 59971 | 60123 | } |
| | @@ -59978,26 +60130,25 @@ |
| 59978 | 60130 | */ |
| 59979 | 60131 | static void checkPtrmap( |
| 59980 | 60132 | IntegrityCk *pCheck, /* Integrity check context */ |
| 59981 | 60133 | Pgno iChild, /* Child page number */ |
| 59982 | 60134 | u8 eType, /* Expected pointer map type */ |
| 59983 | | - Pgno iParent, /* Expected pointer map parent page number */ |
| 59984 | | - char *zContext /* Context description (used for error msg) */ |
| 60135 | + Pgno iParent /* Expected pointer map parent page number */ |
| 59985 | 60136 | ){ |
| 59986 | 60137 | int rc; |
| 59987 | 60138 | u8 ePtrmapType; |
| 59988 | 60139 | Pgno iPtrmapParent; |
| 59989 | 60140 | |
| 59990 | 60141 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 59991 | 60142 | if( rc!=SQLITE_OK ){ |
| 59992 | 60143 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; |
| 59993 | | - checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 60144 | + checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); |
| 59994 | 60145 | return; |
| 59995 | 60146 | } |
| 59996 | 60147 | |
| 59997 | 60148 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 59998 | | - checkAppendMsg(pCheck, zContext, |
| 60149 | + checkAppendMsg(pCheck, |
| 59999 | 60150 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 60000 | 60151 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 60001 | 60152 | } |
| 60002 | 60153 | } |
| 60003 | 60154 | #endif |
| | @@ -60008,51 +60159,50 @@ |
| 60008 | 60159 | */ |
| 60009 | 60160 | static void checkList( |
| 60010 | 60161 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 60011 | 60162 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 60012 | 60163 | int iPage, /* Page number for first page in the list */ |
| 60013 | | - int N, /* Expected number of pages in the list */ |
| 60014 | | - char *zContext /* Context for error messages */ |
| 60164 | + int N /* Expected number of pages in the list */ |
| 60015 | 60165 | ){ |
| 60016 | 60166 | int i; |
| 60017 | 60167 | int expected = N; |
| 60018 | 60168 | int iFirst = iPage; |
| 60019 | 60169 | while( N-- > 0 && pCheck->mxErr ){ |
| 60020 | 60170 | DbPage *pOvflPage; |
| 60021 | 60171 | unsigned char *pOvflData; |
| 60022 | 60172 | if( iPage<1 ){ |
| 60023 | | - checkAppendMsg(pCheck, zContext, |
| 60173 | + checkAppendMsg(pCheck, |
| 60024 | 60174 | "%d of %d pages missing from overflow list starting at %d", |
| 60025 | 60175 | N+1, expected, iFirst); |
| 60026 | 60176 | break; |
| 60027 | 60177 | } |
| 60028 | | - if( checkRef(pCheck, iPage, zContext) ) break; |
| 60178 | + if( checkRef(pCheck, iPage) ) break; |
| 60029 | 60179 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
| 60030 | | - checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
| 60180 | + checkAppendMsg(pCheck, "failed to get page %d", iPage); |
| 60031 | 60181 | break; |
| 60032 | 60182 | } |
| 60033 | 60183 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
| 60034 | 60184 | if( isFreeList ){ |
| 60035 | 60185 | int n = get4byte(&pOvflData[4]); |
| 60036 | 60186 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60037 | 60187 | if( pCheck->pBt->autoVacuum ){ |
| 60038 | | - checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 60188 | + checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); |
| 60039 | 60189 | } |
| 60040 | 60190 | #endif |
| 60041 | 60191 | if( n>(int)pCheck->pBt->usableSize/4-2 ){ |
| 60042 | | - checkAppendMsg(pCheck, zContext, |
| 60192 | + checkAppendMsg(pCheck, |
| 60043 | 60193 | "freelist leaf count too big on page %d", iPage); |
| 60044 | 60194 | N--; |
| 60045 | 60195 | }else{ |
| 60046 | 60196 | for(i=0; i<n; i++){ |
| 60047 | 60197 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
| 60048 | 60198 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60049 | 60199 | if( pCheck->pBt->autoVacuum ){ |
| 60050 | | - checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 60200 | + checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); |
| 60051 | 60201 | } |
| 60052 | 60202 | #endif |
| 60053 | | - checkRef(pCheck, iFreePage, zContext); |
| 60203 | + checkRef(pCheck, iFreePage); |
| 60054 | 60204 | } |
| 60055 | 60205 | N -= n; |
| 60056 | 60206 | } |
| 60057 | 60207 | } |
| 60058 | 60208 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| | @@ -60061,11 +60211,11 @@ |
| 60061 | 60211 | ** page in this overflow list, check that the pointer-map entry for |
| 60062 | 60212 | ** the following page matches iPage. |
| 60063 | 60213 | */ |
| 60064 | 60214 | if( pCheck->pBt->autoVacuum && N>0 ){ |
| 60065 | 60215 | i = get4byte(pOvflData); |
| 60066 | | - checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 60216 | + checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); |
| 60067 | 60217 | } |
| 60068 | 60218 | } |
| 60069 | 60219 | #endif |
| 60070 | 60220 | iPage = get4byte(pOvflData); |
| 60071 | 60221 | sqlite3PagerUnref(pOvflPage); |
| | @@ -60093,11 +60243,10 @@ |
| 60093 | 60243 | ** the root of the tree. |
| 60094 | 60244 | */ |
| 60095 | 60245 | static int checkTreePage( |
| 60096 | 60246 | IntegrityCk *pCheck, /* Context for the sanity check */ |
| 60097 | 60247 | int iPage, /* Page number of the page to check */ |
| 60098 | | - char *zParentContext, /* Parent context */ |
| 60099 | 60248 | i64 *pnParentMinKey, |
| 60100 | 60249 | i64 *pnParentMaxKey |
| 60101 | 60250 | ){ |
| 60102 | 60251 | MemPage *pPage; |
| 60103 | 60252 | int i, rc, depth, d2, pgno, cnt; |
| | @@ -60104,38 +60253,42 @@ |
| 60104 | 60253 | int hdr, cellStart; |
| 60105 | 60254 | int nCell; |
| 60106 | 60255 | u8 *data; |
| 60107 | 60256 | BtShared *pBt; |
| 60108 | 60257 | int usableSize; |
| 60109 | | - char zContext[100]; |
| 60110 | 60258 | char *hit = 0; |
| 60111 | 60259 | i64 nMinKey = 0; |
| 60112 | 60260 | i64 nMaxKey = 0; |
| 60113 | | - |
| 60114 | | - sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
| 60261 | + const char *saved_zPfx = pCheck->zPfx; |
| 60262 | + int saved_v1 = pCheck->v1; |
| 60263 | + int saved_v2 = pCheck->v2; |
| 60115 | 60264 | |
| 60116 | 60265 | /* Check that the page exists |
| 60117 | 60266 | */ |
| 60118 | 60267 | pBt = pCheck->pBt; |
| 60119 | 60268 | usableSize = pBt->usableSize; |
| 60120 | 60269 | if( iPage==0 ) return 0; |
| 60121 | | - if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
| 60270 | + if( checkRef(pCheck, iPage) ) return 0; |
| 60271 | + pCheck->zPfx = "Page %d: "; |
| 60272 | + pCheck->v1 = iPage; |
| 60122 | 60273 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
| 60123 | | - checkAppendMsg(pCheck, zContext, |
| 60274 | + checkAppendMsg(pCheck, |
| 60124 | 60275 | "unable to get the page. error code=%d", rc); |
| 60125 | | - return 0; |
| 60276 | + depth = -1; |
| 60277 | + goto end_of_check; |
| 60126 | 60278 | } |
| 60127 | 60279 | |
| 60128 | 60280 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 60129 | 60281 | ** btreeInitPage() is executed. */ |
| 60130 | 60282 | pPage->isInit = 0; |
| 60131 | 60283 | if( (rc = btreeInitPage(pPage))!=0 ){ |
| 60132 | 60284 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
| 60133 | | - checkAppendMsg(pCheck, zContext, |
| 60285 | + checkAppendMsg(pCheck, |
| 60134 | 60286 | "btreeInitPage() returns error code %d", rc); |
| 60135 | 60287 | releasePage(pPage); |
| 60136 | | - return 0; |
| 60288 | + depth = -1; |
| 60289 | + goto end_of_check; |
| 60137 | 60290 | } |
| 60138 | 60291 | |
| 60139 | 60292 | /* Check out all the cells. |
| 60140 | 60293 | */ |
| 60141 | 60294 | depth = 0; |
| | @@ -60144,99 +60297,101 @@ |
| 60144 | 60297 | u32 sz; |
| 60145 | 60298 | CellInfo info; |
| 60146 | 60299 | |
| 60147 | 60300 | /* Check payload overflow pages |
| 60148 | 60301 | */ |
| 60149 | | - sqlite3_snprintf(sizeof(zContext), zContext, |
| 60150 | | - "On tree page %d cell %d: ", iPage, i); |
| 60302 | + pCheck->zPfx = "On tree page %d cell %d: "; |
| 60303 | + pCheck->v1 = iPage; |
| 60304 | + pCheck->v2 = i; |
| 60151 | 60305 | pCell = findCell(pPage,i); |
| 60152 | 60306 | btreeParseCellPtr(pPage, pCell, &info); |
| 60153 | | - sz = info.nData; |
| 60154 | | - if( !pPage->intKey ) sz += (int)info.nKey; |
| 60307 | + sz = info.nPayload; |
| 60155 | 60308 | /* For intKey pages, check that the keys are in order. |
| 60156 | 60309 | */ |
| 60157 | | - else if( i==0 ) nMinKey = nMaxKey = info.nKey; |
| 60158 | | - else{ |
| 60159 | | - if( info.nKey <= nMaxKey ){ |
| 60160 | | - checkAppendMsg(pCheck, zContext, |
| 60161 | | - "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 60310 | + if( pPage->intKey ){ |
| 60311 | + if( i==0 ){ |
| 60312 | + nMinKey = nMaxKey = info.nKey; |
| 60313 | + }else if( info.nKey <= nMaxKey ){ |
| 60314 | + checkAppendMsg(pCheck, |
| 60315 | + "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 60162 | 60316 | } |
| 60163 | 60317 | nMaxKey = info.nKey; |
| 60164 | 60318 | } |
| 60165 | | - assert( sz==info.nPayload ); |
| 60166 | 60319 | if( (sz>info.nLocal) |
| 60167 | 60320 | && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 60168 | 60321 | ){ |
| 60169 | 60322 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
| 60170 | 60323 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 60171 | 60324 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60172 | 60325 | if( pBt->autoVacuum ){ |
| 60173 | | - checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
| 60326 | + checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); |
| 60174 | 60327 | } |
| 60175 | 60328 | #endif |
| 60176 | | - checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
| 60329 | + checkList(pCheck, 0, pgnoOvfl, nPage); |
| 60177 | 60330 | } |
| 60178 | 60331 | |
| 60179 | 60332 | /* Check sanity of left child page. |
| 60180 | 60333 | */ |
| 60181 | 60334 | if( !pPage->leaf ){ |
| 60182 | 60335 | pgno = get4byte(pCell); |
| 60183 | 60336 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60184 | 60337 | if( pBt->autoVacuum ){ |
| 60185 | | - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 60338 | + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 60186 | 60339 | } |
| 60187 | 60340 | #endif |
| 60188 | | - d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey); |
| 60341 | + d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey); |
| 60189 | 60342 | if( i>0 && d2!=depth ){ |
| 60190 | | - checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 60343 | + checkAppendMsg(pCheck, "Child page depth differs"); |
| 60191 | 60344 | } |
| 60192 | 60345 | depth = d2; |
| 60193 | 60346 | } |
| 60194 | 60347 | } |
| 60195 | 60348 | |
| 60196 | 60349 | if( !pPage->leaf ){ |
| 60197 | 60350 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 60198 | | - sqlite3_snprintf(sizeof(zContext), zContext, |
| 60199 | | - "On page %d at right child: ", iPage); |
| 60351 | + pCheck->zPfx = "On page %d at right child: "; |
| 60352 | + pCheck->v1 = iPage; |
| 60200 | 60353 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60201 | 60354 | if( pBt->autoVacuum ){ |
| 60202 | | - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 60355 | + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 60203 | 60356 | } |
| 60204 | 60357 | #endif |
| 60205 | | - checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey); |
| 60358 | + checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey); |
| 60206 | 60359 | } |
| 60207 | 60360 | |
| 60208 | 60361 | /* For intKey leaf pages, check that the min/max keys are in order |
| 60209 | 60362 | ** with any left/parent/right pages. |
| 60210 | 60363 | */ |
| 60364 | + pCheck->zPfx = "Page %d: "; |
| 60365 | + pCheck->v1 = iPage; |
| 60211 | 60366 | if( pPage->leaf && pPage->intKey ){ |
| 60212 | 60367 | /* if we are a left child page */ |
| 60213 | 60368 | if( pnParentMinKey ){ |
| 60214 | 60369 | /* if we are the left most child page */ |
| 60215 | 60370 | if( !pnParentMaxKey ){ |
| 60216 | 60371 | if( nMaxKey > *pnParentMinKey ){ |
| 60217 | | - checkAppendMsg(pCheck, zContext, |
| 60372 | + checkAppendMsg(pCheck, |
| 60218 | 60373 | "Rowid %lld out of order (max larger than parent min of %lld)", |
| 60219 | 60374 | nMaxKey, *pnParentMinKey); |
| 60220 | 60375 | } |
| 60221 | 60376 | }else{ |
| 60222 | 60377 | if( nMinKey <= *pnParentMinKey ){ |
| 60223 | | - checkAppendMsg(pCheck, zContext, |
| 60378 | + checkAppendMsg(pCheck, |
| 60224 | 60379 | "Rowid %lld out of order (min less than parent min of %lld)", |
| 60225 | 60380 | nMinKey, *pnParentMinKey); |
| 60226 | 60381 | } |
| 60227 | 60382 | if( nMaxKey > *pnParentMaxKey ){ |
| 60228 | | - checkAppendMsg(pCheck, zContext, |
| 60383 | + checkAppendMsg(pCheck, |
| 60229 | 60384 | "Rowid %lld out of order (max larger than parent max of %lld)", |
| 60230 | 60385 | nMaxKey, *pnParentMaxKey); |
| 60231 | 60386 | } |
| 60232 | 60387 | *pnParentMinKey = nMaxKey; |
| 60233 | 60388 | } |
| 60234 | 60389 | /* else if we're a right child page */ |
| 60235 | 60390 | } else if( pnParentMaxKey ){ |
| 60236 | 60391 | if( nMinKey <= *pnParentMaxKey ){ |
| 60237 | | - checkAppendMsg(pCheck, zContext, |
| 60392 | + checkAppendMsg(pCheck, |
| 60238 | 60393 | "Rowid %lld out of order (min less than parent max of %lld)", |
| 60239 | 60394 | nMinKey, *pnParentMaxKey); |
| 60240 | 60395 | } |
| 60241 | 60396 | } |
| 60242 | 60397 | } |
| | @@ -60244,10 +60399,11 @@ |
| 60244 | 60399 | /* Check for complete coverage of the page |
| 60245 | 60400 | */ |
| 60246 | 60401 | data = pPage->aData; |
| 60247 | 60402 | hdr = pPage->hdrOffset; |
| 60248 | 60403 | hit = sqlite3PageMalloc( pBt->pageSize ); |
| 60404 | + pCheck->zPfx = 0; |
| 60249 | 60405 | if( hit==0 ){ |
| 60250 | 60406 | pCheck->mallocFailed = 1; |
| 60251 | 60407 | }else{ |
| 60252 | 60408 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 60253 | 60409 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| | @@ -60261,11 +60417,12 @@ |
| 60261 | 60417 | int j; |
| 60262 | 60418 | if( pc<=usableSize-4 ){ |
| 60263 | 60419 | size = cellSizePtr(pPage, &data[pc]); |
| 60264 | 60420 | } |
| 60265 | 60421 | if( (int)(pc+size-1)>=usableSize ){ |
| 60266 | | - checkAppendMsg(pCheck, 0, |
| 60422 | + pCheck->zPfx = 0; |
| 60423 | + checkAppendMsg(pCheck, |
| 60267 | 60424 | "Corruption detected in cell %d on page %d",i,iPage); |
| 60268 | 60425 | }else{ |
| 60269 | 60426 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 60270 | 60427 | } |
| 60271 | 60428 | } |
| | @@ -60283,23 +60440,28 @@ |
| 60283 | 60440 | } |
| 60284 | 60441 | for(i=cnt=0; i<usableSize; i++){ |
| 60285 | 60442 | if( hit[i]==0 ){ |
| 60286 | 60443 | cnt++; |
| 60287 | 60444 | }else if( hit[i]>1 ){ |
| 60288 | | - checkAppendMsg(pCheck, 0, |
| 60445 | + checkAppendMsg(pCheck, |
| 60289 | 60446 | "Multiple uses for byte %d of page %d", i, iPage); |
| 60290 | 60447 | break; |
| 60291 | 60448 | } |
| 60292 | 60449 | } |
| 60293 | 60450 | if( cnt!=data[hdr+7] ){ |
| 60294 | | - checkAppendMsg(pCheck, 0, |
| 60451 | + checkAppendMsg(pCheck, |
| 60295 | 60452 | "Fragmentation of %d bytes reported as %d on page %d", |
| 60296 | 60453 | cnt, data[hdr+7], iPage); |
| 60297 | 60454 | } |
| 60298 | 60455 | } |
| 60299 | 60456 | sqlite3PageFree(hit); |
| 60300 | 60457 | releasePage(pPage); |
| 60458 | + |
| 60459 | +end_of_check: |
| 60460 | + pCheck->zPfx = saved_zPfx; |
| 60461 | + pCheck->v1 = saved_v1; |
| 60462 | + pCheck->v2 = saved_v2; |
| 60301 | 60463 | return depth+1; |
| 60302 | 60464 | } |
| 60303 | 60465 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 60304 | 60466 | |
| 60305 | 60467 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| | @@ -60336,10 +60498,13 @@ |
| 60336 | 60498 | sCheck.pPager = pBt->pPager; |
| 60337 | 60499 | sCheck.nPage = btreePagecount(sCheck.pBt); |
| 60338 | 60500 | sCheck.mxErr = mxErr; |
| 60339 | 60501 | sCheck.nErr = 0; |
| 60340 | 60502 | sCheck.mallocFailed = 0; |
| 60503 | + sCheck.zPfx = 0; |
| 60504 | + sCheck.v1 = 0; |
| 60505 | + sCheck.v2 = 0; |
| 60341 | 60506 | *pnErr = 0; |
| 60342 | 60507 | if( sCheck.nPage==0 ){ |
| 60343 | 60508 | sqlite3BtreeLeave(p); |
| 60344 | 60509 | return 0; |
| 60345 | 60510 | } |
| | @@ -60355,53 +60520,57 @@ |
| 60355 | 60520 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 60356 | 60521 | sCheck.errMsg.useMalloc = 2; |
| 60357 | 60522 | |
| 60358 | 60523 | /* Check the integrity of the freelist |
| 60359 | 60524 | */ |
| 60525 | + sCheck.zPfx = "Main freelist: "; |
| 60360 | 60526 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 60361 | | - get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
| 60527 | + get4byte(&pBt->pPage1->aData[36])); |
| 60528 | + sCheck.zPfx = 0; |
| 60362 | 60529 | |
| 60363 | 60530 | /* Check all the tables. |
| 60364 | 60531 | */ |
| 60365 | 60532 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
| 60366 | 60533 | if( aRoot[i]==0 ) continue; |
| 60367 | 60534 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60368 | 60535 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 60369 | | - checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 60536 | + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); |
| 60370 | 60537 | } |
| 60371 | 60538 | #endif |
| 60372 | | - checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL); |
| 60539 | + sCheck.zPfx = "List of tree roots: "; |
| 60540 | + checkTreePage(&sCheck, aRoot[i], NULL, NULL); |
| 60541 | + sCheck.zPfx = 0; |
| 60373 | 60542 | } |
| 60374 | 60543 | |
| 60375 | 60544 | /* Make sure every page in the file is referenced |
| 60376 | 60545 | */ |
| 60377 | 60546 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
| 60378 | 60547 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 60379 | 60548 | if( getPageReferenced(&sCheck, i)==0 ){ |
| 60380 | | - checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 60549 | + checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 60381 | 60550 | } |
| 60382 | 60551 | #else |
| 60383 | 60552 | /* If the database supports auto-vacuum, make sure no tables contain |
| 60384 | 60553 | ** references to pointer-map pages. |
| 60385 | 60554 | */ |
| 60386 | 60555 | if( getPageReferenced(&sCheck, i)==0 && |
| 60387 | 60556 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
| 60388 | | - checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 60557 | + checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 60389 | 60558 | } |
| 60390 | 60559 | if( getPageReferenced(&sCheck, i)!=0 && |
| 60391 | 60560 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
| 60392 | | - checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 60561 | + checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); |
| 60393 | 60562 | } |
| 60394 | 60563 | #endif |
| 60395 | 60564 | } |
| 60396 | 60565 | |
| 60397 | 60566 | /* Make sure this analysis did not leave any unref() pages. |
| 60398 | 60567 | ** This is an internal consistency check; an integrity check |
| 60399 | 60568 | ** of the integrity check. |
| 60400 | 60569 | */ |
| 60401 | 60570 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
| 60402 | | - checkAppendMsg(&sCheck, 0, |
| 60571 | + checkAppendMsg(&sCheck, |
| 60403 | 60572 | "Outstanding page count goes from %d to %d during this analysis", |
| 60404 | 60573 | nRef, sqlite3PagerRefcount(pBt->pPager) |
| 60405 | 60574 | ); |
| 60406 | 60575 | } |
| 60407 | 60576 | |
| | @@ -60593,11 +60762,11 @@ |
| 60593 | 60762 | |
| 60594 | 60763 | /* Save the positions of all other cursors open on this table. This is |
| 60595 | 60764 | ** required in case any of them are holding references to an xFetch |
| 60596 | 60765 | ** version of the b-tree page modified by the accessPayload call below. |
| 60597 | 60766 | ** |
| 60598 | | - ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition() |
| 60767 | + ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() |
| 60599 | 60768 | ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence |
| 60600 | 60769 | ** saveAllCursors can only return SQLITE_OK. |
| 60601 | 60770 | */ |
| 60602 | 60771 | VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); |
| 60603 | 60772 | assert( rc==SQLITE_OK ); |
| | @@ -63751,11 +63920,12 @@ |
| 63751 | 63920 | if( addr==p->nOp-1 ) p->nOp--; |
| 63752 | 63921 | } |
| 63753 | 63922 | } |
| 63754 | 63923 | |
| 63755 | 63924 | /* |
| 63756 | | -** Remove the last opcode inserted |
| 63925 | +** If the last opcode is "op" and it is not a jump destination, |
| 63926 | +** then remove it. Return true if and only if an opcode was removed. |
| 63757 | 63927 | */ |
| 63758 | 63928 | SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
| 63759 | 63929 | if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ |
| 63760 | 63930 | sqlite3VdbeChangeToNoop(p, p->nOp-1); |
| 63761 | 63931 | return 1; |
| | @@ -65676,14 +65846,10 @@ |
| 65676 | 65846 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65677 | 65847 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65678 | 65848 | sqlite3DbFree(db, p->aColName); |
| 65679 | 65849 | sqlite3DbFree(db, p->zSql); |
| 65680 | 65850 | sqlite3DbFree(db, p->pFree); |
| 65681 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 65682 | | - sqlite3DbFree(db, p->zExplain); |
| 65683 | | - sqlite3DbFree(db, p->pExplain); |
| 65684 | | -#endif |
| 65685 | 65851 | } |
| 65686 | 65852 | |
| 65687 | 65853 | /* |
| 65688 | 65854 | ** Delete an entire VDBE. |
| 65689 | 65855 | */ |
| | @@ -67390,10 +67556,11 @@ |
| 67390 | 67556 | void (*xDel)(void *), |
| 67391 | 67557 | unsigned char enc |
| 67392 | 67558 | ){ |
| 67393 | 67559 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 67394 | 67560 | assert( xDel!=SQLITE_DYNAMIC ); |
| 67561 | + if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
| 67395 | 67562 | if( n>0x7fffffff ){ |
| 67396 | 67563 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 67397 | 67564 | }else{ |
| 67398 | 67565 | setResultStrOrError(pCtx, z, (int)n, enc, xDel); |
| 67399 | 67566 | } |
| | @@ -68712,125 +68879,10 @@ |
| 68712 | 68879 | return sqlite3StrAccumFinish(&out); |
| 68713 | 68880 | } |
| 68714 | 68881 | |
| 68715 | 68882 | #endif /* #ifndef SQLITE_OMIT_TRACE */ |
| 68716 | 68883 | |
| 68717 | | -/***************************************************************************** |
| 68718 | | -** The following code implements the data-structure explaining logic |
| 68719 | | -** for the Vdbe. |
| 68720 | | -*/ |
| 68721 | | - |
| 68722 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 68723 | | - |
| 68724 | | -/* |
| 68725 | | -** Allocate a new Explain object |
| 68726 | | -*/ |
| 68727 | | -SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){ |
| 68728 | | - if( pVdbe ){ |
| 68729 | | - Explain *p; |
| 68730 | | - sqlite3BeginBenignMalloc(); |
| 68731 | | - p = (Explain *)sqlite3MallocZero( sizeof(Explain) ); |
| 68732 | | - if( p ){ |
| 68733 | | - p->pVdbe = pVdbe; |
| 68734 | | - sqlite3_free(pVdbe->pExplain); |
| 68735 | | - pVdbe->pExplain = p; |
| 68736 | | - sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase), |
| 68737 | | - SQLITE_MAX_LENGTH); |
| 68738 | | - p->str.useMalloc = 2; |
| 68739 | | - }else{ |
| 68740 | | - sqlite3EndBenignMalloc(); |
| 68741 | | - } |
| 68742 | | - } |
| 68743 | | -} |
| 68744 | | - |
| 68745 | | -/* |
| 68746 | | -** Return true if the Explain ends with a new-line. |
| 68747 | | -*/ |
| 68748 | | -static int endsWithNL(Explain *p){ |
| 68749 | | - return p && p->str.zText && p->str.nChar |
| 68750 | | - && p->str.zText[p->str.nChar-1]=='\n'; |
| 68751 | | -} |
| 68752 | | - |
| 68753 | | -/* |
| 68754 | | -** Append text to the indentation |
| 68755 | | -*/ |
| 68756 | | -SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){ |
| 68757 | | - Explain *p; |
| 68758 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 ){ |
| 68759 | | - va_list ap; |
| 68760 | | - if( p->nIndent && endsWithNL(p) ){ |
| 68761 | | - int n = p->nIndent; |
| 68762 | | - if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent); |
| 68763 | | - sqlite3AppendSpace(&p->str, p->aIndent[n-1]); |
| 68764 | | - } |
| 68765 | | - va_start(ap, zFormat); |
| 68766 | | - sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap); |
| 68767 | | - va_end(ap); |
| 68768 | | - } |
| 68769 | | -} |
| 68770 | | - |
| 68771 | | -/* |
| 68772 | | -** Append a '\n' if there is not already one. |
| 68773 | | -*/ |
| 68774 | | -SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){ |
| 68775 | | - Explain *p; |
| 68776 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){ |
| 68777 | | - sqlite3StrAccumAppend(&p->str, "\n", 1); |
| 68778 | | - } |
| 68779 | | -} |
| 68780 | | - |
| 68781 | | -/* |
| 68782 | | -** Push a new indentation level. Subsequent lines will be indented |
| 68783 | | -** so that they begin at the current cursor position. |
| 68784 | | -*/ |
| 68785 | | -SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){ |
| 68786 | | - Explain *p; |
| 68787 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 ){ |
| 68788 | | - if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){ |
| 68789 | | - const char *z = p->str.zText; |
| 68790 | | - int i = p->str.nChar-1; |
| 68791 | | - int x; |
| 68792 | | - while( i>=0 && z[i]!='\n' ){ i--; } |
| 68793 | | - x = (p->str.nChar - 1) - i; |
| 68794 | | - if( p->nIndent && x<p->aIndent[p->nIndent-1] ){ |
| 68795 | | - x = p->aIndent[p->nIndent-1]; |
| 68796 | | - } |
| 68797 | | - p->aIndent[p->nIndent] = x; |
| 68798 | | - } |
| 68799 | | - p->nIndent++; |
| 68800 | | - } |
| 68801 | | -} |
| 68802 | | - |
| 68803 | | -/* |
| 68804 | | -** Pop the indentation stack by one level. |
| 68805 | | -*/ |
| 68806 | | -SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){ |
| 68807 | | - if( p && p->pExplain ) p->pExplain->nIndent--; |
| 68808 | | -} |
| 68809 | | - |
| 68810 | | -/* |
| 68811 | | -** Free the indentation structure |
| 68812 | | -*/ |
| 68813 | | -SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){ |
| 68814 | | - if( pVdbe && pVdbe->pExplain ){ |
| 68815 | | - sqlite3_free(pVdbe->zExplain); |
| 68816 | | - sqlite3ExplainNL(pVdbe); |
| 68817 | | - pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str); |
| 68818 | | - sqlite3_free(pVdbe->pExplain); |
| 68819 | | - pVdbe->pExplain = 0; |
| 68820 | | - sqlite3EndBenignMalloc(); |
| 68821 | | - } |
| 68822 | | -} |
| 68823 | | - |
| 68824 | | -/* |
| 68825 | | -** Return the explanation of a virtual machine. |
| 68826 | | -*/ |
| 68827 | | -SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){ |
| 68828 | | - return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0; |
| 68829 | | -} |
| 68830 | | -#endif /* defined(SQLITE_DEBUG) */ |
| 68831 | | - |
| 68832 | 68884 | /************** End of vdbetrace.c *******************************************/ |
| 68833 | 68885 | /************** Begin file vdbe.c ********************************************/ |
| 68834 | 68886 | /* |
| 68835 | 68887 | ** 2001 September 15 |
| 68836 | 68888 | ** |
| | @@ -70475,21 +70527,14 @@ |
| 70475 | 70527 | assert( pOp->p4type==P4_FUNCDEF ); |
| 70476 | 70528 | ctx.pFunc = pOp->p4.pFunc; |
| 70477 | 70529 | ctx.iOp = pc; |
| 70478 | 70530 | ctx.pVdbe = p; |
| 70479 | 70531 | MemSetTypeFlag(ctx.pOut, MEM_Null); |
| 70480 | | - |
| 70481 | 70532 | ctx.fErrorOrAux = 0; |
| 70482 | | - if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 70483 | | - assert( pOp>aOp ); |
| 70484 | | - assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 70485 | | - assert( pOp[-1].opcode==OP_CollSeq ); |
| 70486 | | - ctx.pColl = pOp[-1].p4.pColl; |
| 70487 | | - } |
| 70488 | | - db->lastRowid = lastRowid; |
| 70533 | + assert( db->lastRowid==lastRowid ); |
| 70489 | 70534 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 70490 | | - lastRowid = db->lastRowid; |
| 70535 | + lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ |
| 70491 | 70536 | |
| 70492 | 70537 | /* If the function returned an error, throw an exception */ |
| 70493 | 70538 | if( ctx.fErrorOrAux ){ |
| 70494 | 70539 | if( ctx.isError ){ |
| 70495 | 70540 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut)); |
| | @@ -72205,14 +72250,10 @@ |
| 72205 | 72250 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 72206 | 72251 | pCur->pKeyInfo = pKeyInfo; |
| 72207 | 72252 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 72208 | 72253 | sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR)); |
| 72209 | 72254 | |
| 72210 | | - /* Since it performs no memory allocation or IO, the only value that |
| 72211 | | - ** sqlite3BtreeCursor() may return is SQLITE_OK. */ |
| 72212 | | - assert( rc==SQLITE_OK ); |
| 72213 | | - |
| 72214 | 72255 | /* Set the VdbeCursor.isTable variable. Previous versions of |
| 72215 | 72256 | ** SQLite used to check if the root-page flags were sane at this point |
| 72216 | 72257 | ** and report database corruption if they were not, but this check has |
| 72217 | 72258 | ** since moved into the btree layer. */ |
| 72218 | 72259 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| | @@ -72942,29 +72983,18 @@ |
| 72942 | 72983 | ** largest possible integer (9223372036854775807) then the database |
| 72943 | 72984 | ** engine starts picking positive candidate ROWIDs at random until |
| 72944 | 72985 | ** it finds one that is not previously used. */ |
| 72945 | 72986 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 72946 | 72987 | ** an AUTOINCREMENT table. */ |
| 72947 | | - /* on the first attempt, simply do one more than previous */ |
| 72948 | | - v = lastRowid; |
| 72949 | | - v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 72950 | | - v++; /* ensure non-zero */ |
| 72951 | 72988 | cnt = 0; |
| 72952 | | - while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, |
| 72989 | + do{ |
| 72990 | + sqlite3_randomness(sizeof(v), &v); |
| 72991 | + v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
| 72992 | + }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, |
| 72953 | 72993 | 0, &res))==SQLITE_OK) |
| 72954 | 72994 | && (res==0) |
| 72955 | | - && (++cnt<100)){ |
| 72956 | | - /* collision - try another random rowid */ |
| 72957 | | - sqlite3_randomness(sizeof(v), &v); |
| 72958 | | - if( cnt<5 ){ |
| 72959 | | - /* try "small" random rowids for the initial attempts */ |
| 72960 | | - v &= 0xffffff; |
| 72961 | | - }else{ |
| 72962 | | - v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 72963 | | - } |
| 72964 | | - v++; /* ensure non-zero */ |
| 72965 | | - } |
| 72995 | + && (++cnt<100)); |
| 72966 | 72996 | if( rc==SQLITE_OK && res==0 ){ |
| 72967 | 72997 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 72968 | 72998 | goto abort_due_to_error; |
| 72969 | 72999 | } |
| 72970 | 73000 | assert( v>0 ); /* EV: R-40812-03570 */ |
| | @@ -74556,18 +74586,13 @@ |
| 74556 | 74586 | ctx.pMem = pMem = &aMem[pOp->p3]; |
| 74557 | 74587 | pMem->n++; |
| 74558 | 74588 | sqlite3VdbeMemInit(&t, db, MEM_Null); |
| 74559 | 74589 | ctx.pOut = &t; |
| 74560 | 74590 | ctx.isError = 0; |
| 74561 | | - ctx.pColl = 0; |
| 74591 | + ctx.pVdbe = p; |
| 74592 | + ctx.iOp = pc; |
| 74562 | 74593 | ctx.skipFlag = 0; |
| 74563 | | - if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 74564 | | - assert( pOp>p->aOp ); |
| 74565 | | - assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 74566 | | - assert( pOp[-1].opcode==OP_CollSeq ); |
| 74567 | | - ctx.pColl = pOp[-1].p4.pColl; |
| 74568 | | - } |
| 74569 | 74594 | (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 74570 | 74595 | if( ctx.isError ){ |
| 74571 | 74596 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t)); |
| 74572 | 74597 | rc = ctx.isError; |
| 74573 | 74598 | } |
| | @@ -81597,10 +81622,11 @@ |
| 81597 | 81622 | pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 81598 | 81623 | pNew->addrOpenEphm[0] = -1; |
| 81599 | 81624 | pNew->addrOpenEphm[1] = -1; |
| 81600 | 81625 | pNew->nSelectRow = p->nSelectRow; |
| 81601 | 81626 | pNew->pWith = withDup(db, p->pWith); |
| 81627 | + sqlite3SelectSetName(pNew, p->zSelName); |
| 81602 | 81628 | return pNew; |
| 81603 | 81629 | } |
| 81604 | 81630 | #else |
| 81605 | 81631 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 81606 | 81632 | assert( p==0 ); |
| | @@ -81739,36 +81765,44 @@ |
| 81739 | 81765 | } |
| 81740 | 81766 | |
| 81741 | 81767 | /* |
| 81742 | 81768 | ** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 81743 | 81769 | ** to an integer. These routines are checking an expression to see |
| 81744 | | -** if it is a constant. Set *Walker.u.pi to 0 if the expression is |
| 81770 | +** if it is a constant. Set *Walker.u.i to 0 if the expression is |
| 81745 | 81771 | ** not constant. |
| 81746 | 81772 | ** |
| 81747 | 81773 | ** These callback routines are used to implement the following: |
| 81748 | 81774 | ** |
| 81749 | | -** sqlite3ExprIsConstant() |
| 81750 | | -** sqlite3ExprIsConstantNotJoin() |
| 81751 | | -** sqlite3ExprIsConstantOrFunction() |
| 81775 | +** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 81776 | +** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 81777 | +** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 81752 | 81778 | ** |
| 81779 | +** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 81780 | +** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing |
| 81781 | +** an existing schema and 3 when processing a new statement. A bound |
| 81782 | +** parameter raises an error for new statements, but is silently converted |
| 81783 | +** to NULL for existing schemas. This allows sqlite_master tables that |
| 81784 | +** contain a bound parameter because they were generated by older versions |
| 81785 | +** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81786 | +** malformed schema error. |
| 81753 | 81787 | */ |
| 81754 | 81788 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81755 | 81789 | |
| 81756 | | - /* If pWalker->u.i is 3 then any term of the expression that comes from |
| 81790 | + /* If pWalker->u.i is 2 then any term of the expression that comes from |
| 81757 | 81791 | ** the ON or USING clauses of a join disqualifies the expression |
| 81758 | 81792 | ** from being considered constant. */ |
| 81759 | | - if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81793 | + if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81760 | 81794 | pWalker->u.i = 0; |
| 81761 | 81795 | return WRC_Abort; |
| 81762 | 81796 | } |
| 81763 | 81797 | |
| 81764 | 81798 | switch( pExpr->op ){ |
| 81765 | 81799 | /* Consider functions to be constant if all their arguments are constant |
| 81766 | | - ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST |
| 81800 | + ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST |
| 81767 | 81801 | ** flag. */ |
| 81768 | 81802 | case TK_FUNCTION: |
| 81769 | | - if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81803 | + if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81770 | 81804 | return WRC_Continue; |
| 81771 | 81805 | } |
| 81772 | 81806 | /* Fall through */ |
| 81773 | 81807 | case TK_ID: |
| 81774 | 81808 | case TK_COLUMN: |
| | @@ -81778,10 +81812,23 @@ |
| 81778 | 81812 | testcase( pExpr->op==TK_COLUMN ); |
| 81779 | 81813 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81780 | 81814 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81781 | 81815 | pWalker->u.i = 0; |
| 81782 | 81816 | return WRC_Abort; |
| 81817 | + case TK_VARIABLE: |
| 81818 | + if( pWalker->u.i==4 ){ |
| 81819 | + /* Silently convert bound parameters that appear inside of CREATE |
| 81820 | + ** statements into a NULL when parsing the CREATE statement text out |
| 81821 | + ** of the sqlite_master table */ |
| 81822 | + pExpr->op = TK_NULL; |
| 81823 | + }else if( pWalker->u.i==3 ){ |
| 81824 | + /* A bound parameter in a CREATE statement that originates from |
| 81825 | + ** sqlite3_prepare() causes an error */ |
| 81826 | + pWalker->u.i = 0; |
| 81827 | + return WRC_Abort; |
| 81828 | + } |
| 81829 | + /* Fall through */ |
| 81783 | 81830 | default: |
| 81784 | 81831 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| 81785 | 81832 | testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ |
| 81786 | 81833 | return WRC_Continue; |
| 81787 | 81834 | } |
| | @@ -81818,11 +81865,11 @@ |
| 81818 | 81865 | ** that does no originate from the ON or USING clauses of a join. |
| 81819 | 81866 | ** Return 0 if it involves variables or function calls or terms from |
| 81820 | 81867 | ** an ON or USING clause. |
| 81821 | 81868 | */ |
| 81822 | 81869 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81823 | | - return exprIsConst(p, 3); |
| 81870 | + return exprIsConst(p, 2); |
| 81824 | 81871 | } |
| 81825 | 81872 | |
| 81826 | 81873 | /* |
| 81827 | 81874 | ** Walk an expression tree. Return 1 if the expression is constant |
| 81828 | 81875 | ** or a function call with constant arguments. Return and 0 if there |
| | @@ -81830,12 +81877,13 @@ |
| 81830 | 81877 | ** |
| 81831 | 81878 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81832 | 81879 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81833 | 81880 | ** a constant. |
| 81834 | 81881 | */ |
| 81835 | | -SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){ |
| 81836 | | - return exprIsConst(p, 2); |
| 81882 | +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81883 | + assert( isInit==0 || isInit==1 ); |
| 81884 | + return exprIsConst(p, 3+isInit); |
| 81837 | 81885 | } |
| 81838 | 81886 | |
| 81839 | 81887 | /* |
| 81840 | 81888 | ** If the expression p codes a constant integer that is small enough |
| 81841 | 81889 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| | @@ -83741,94 +83789,90 @@ |
| 83741 | 83789 | iMem = ++pParse->nMem; |
| 83742 | 83790 | sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
| 83743 | 83791 | exprToRegister(pExpr, iMem); |
| 83744 | 83792 | } |
| 83745 | 83793 | |
| 83746 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 83794 | +#ifdef SQLITE_DEBUG |
| 83747 | 83795 | /* |
| 83748 | 83796 | ** Generate a human-readable explanation of an expression tree. |
| 83749 | 83797 | */ |
| 83750 | | -SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){ |
| 83751 | | - int op; /* The opcode being coded */ |
| 83798 | +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 83752 | 83799 | const char *zBinOp = 0; /* Binary operator */ |
| 83753 | 83800 | const char *zUniOp = 0; /* Unary operator */ |
| 83801 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 83754 | 83802 | if( pExpr==0 ){ |
| 83755 | | - op = TK_NULL; |
| 83756 | | - }else{ |
| 83757 | | - op = pExpr->op; |
| 83803 | + sqlite3TreeViewLine(pView, "nil"); |
| 83804 | + sqlite3TreeViewPop(pView); |
| 83805 | + return; |
| 83758 | 83806 | } |
| 83759 | | - switch( op ){ |
| 83807 | + switch( pExpr->op ){ |
| 83760 | 83808 | case TK_AGG_COLUMN: { |
| 83761 | | - sqlite3ExplainPrintf(pOut, "AGG{%d:%d}", |
| 83809 | + sqlite3TreeViewLine(pView, "AGG{%d:%d}", |
| 83762 | 83810 | pExpr->iTable, pExpr->iColumn); |
| 83763 | 83811 | break; |
| 83764 | 83812 | } |
| 83765 | 83813 | case TK_COLUMN: { |
| 83766 | 83814 | if( pExpr->iTable<0 ){ |
| 83767 | 83815 | /* This only happens when coding check constraints */ |
| 83768 | | - sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn); |
| 83816 | + sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn); |
| 83769 | 83817 | }else{ |
| 83770 | | - sqlite3ExplainPrintf(pOut, "{%d:%d}", |
| 83818 | + sqlite3TreeViewLine(pView, "{%d:%d}", |
| 83771 | 83819 | pExpr->iTable, pExpr->iColumn); |
| 83772 | 83820 | } |
| 83773 | 83821 | break; |
| 83774 | 83822 | } |
| 83775 | 83823 | case TK_INTEGER: { |
| 83776 | 83824 | if( pExpr->flags & EP_IntValue ){ |
| 83777 | | - sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue); |
| 83825 | + sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 83778 | 83826 | }else{ |
| 83779 | | - sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken); |
| 83827 | + sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 83780 | 83828 | } |
| 83781 | 83829 | break; |
| 83782 | 83830 | } |
| 83783 | 83831 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 83784 | 83832 | case TK_FLOAT: { |
| 83785 | | - sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken); |
| 83833 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 83786 | 83834 | break; |
| 83787 | 83835 | } |
| 83788 | 83836 | #endif |
| 83789 | 83837 | case TK_STRING: { |
| 83790 | | - sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken); |
| 83838 | + sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 83791 | 83839 | break; |
| 83792 | 83840 | } |
| 83793 | 83841 | case TK_NULL: { |
| 83794 | | - sqlite3ExplainPrintf(pOut,"NULL"); |
| 83842 | + sqlite3TreeViewLine(pView,"NULL"); |
| 83795 | 83843 | break; |
| 83796 | 83844 | } |
| 83797 | 83845 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 83798 | 83846 | case TK_BLOB: { |
| 83799 | | - sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken); |
| 83847 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 83800 | 83848 | break; |
| 83801 | 83849 | } |
| 83802 | 83850 | #endif |
| 83803 | 83851 | case TK_VARIABLE: { |
| 83804 | | - sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)", |
| 83805 | | - pExpr->u.zToken, pExpr->iColumn); |
| 83852 | + sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 83853 | + pExpr->u.zToken, pExpr->iColumn); |
| 83806 | 83854 | break; |
| 83807 | 83855 | } |
| 83808 | 83856 | case TK_REGISTER: { |
| 83809 | | - sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable); |
| 83857 | + sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 83810 | 83858 | break; |
| 83811 | 83859 | } |
| 83812 | 83860 | case TK_AS: { |
| 83813 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83861 | + sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 83862 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83863 | + break; |
| 83864 | + } |
| 83865 | + case TK_ID: { |
| 83866 | + sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken); |
| 83814 | 83867 | break; |
| 83815 | 83868 | } |
| 83816 | 83869 | #ifndef SQLITE_OMIT_CAST |
| 83817 | 83870 | case TK_CAST: { |
| 83818 | 83871 | /* Expressions of the form: CAST(pLeft AS token) */ |
| 83819 | | - const char *zAff = "unk"; |
| 83820 | | - switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){ |
| 83821 | | - case SQLITE_AFF_TEXT: zAff = "TEXT"; break; |
| 83822 | | - case SQLITE_AFF_NONE: zAff = "NONE"; break; |
| 83823 | | - case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break; |
| 83824 | | - case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break; |
| 83825 | | - case SQLITE_AFF_REAL: zAff = "REAL"; break; |
| 83826 | | - } |
| 83827 | | - sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff); |
| 83828 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83829 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83872 | + sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 83873 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83830 | 83874 | break; |
| 83831 | 83875 | } |
| 83832 | 83876 | #endif /* SQLITE_OMIT_CAST */ |
| 83833 | 83877 | case TK_LT: zBinOp = "LT"; break; |
| 83834 | 83878 | case TK_LE: zBinOp = "LE"; break; |
| | @@ -83848,21 +83892,22 @@ |
| 83848 | 83892 | case TK_BITOR: zBinOp = "BITOR"; break; |
| 83849 | 83893 | case TK_SLASH: zBinOp = "DIV"; break; |
| 83850 | 83894 | case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 83851 | 83895 | case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 83852 | 83896 | case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 83897 | + case TK_DOT: zBinOp = "DOT"; break; |
| 83853 | 83898 | |
| 83854 | 83899 | case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 83855 | 83900 | case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 83856 | 83901 | case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 83857 | 83902 | case TK_NOT: zUniOp = "NOT"; break; |
| 83858 | 83903 | case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 83859 | 83904 | case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 83860 | 83905 | |
| 83861 | 83906 | case TK_COLLATE: { |
| 83862 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83863 | | - sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken); |
| 83907 | + sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 83908 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83864 | 83909 | break; |
| 83865 | 83910 | } |
| 83866 | 83911 | |
| 83867 | 83912 | case TK_AGG_FUNCTION: |
| 83868 | 83913 | case TK_FUNCTION: { |
| | @@ -83870,45 +83915,40 @@ |
| 83870 | 83915 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 83871 | 83916 | pFarg = 0; |
| 83872 | 83917 | }else{ |
| 83873 | 83918 | pFarg = pExpr->x.pList; |
| 83874 | 83919 | } |
| 83875 | | - if( op==TK_AGG_FUNCTION ){ |
| 83876 | | - sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(", |
| 83920 | + if( pExpr->op==TK_AGG_FUNCTION ){ |
| 83921 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 83877 | 83922 | pExpr->op2, pExpr->u.zToken); |
| 83878 | 83923 | }else{ |
| 83879 | | - sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken); |
| 83924 | + sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 83880 | 83925 | } |
| 83881 | 83926 | if( pFarg ){ |
| 83882 | | - sqlite3ExplainExprList(pOut, pFarg); |
| 83927 | + sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 83883 | 83928 | } |
| 83884 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83885 | 83929 | break; |
| 83886 | 83930 | } |
| 83887 | 83931 | #ifndef SQLITE_OMIT_SUBQUERY |
| 83888 | 83932 | case TK_EXISTS: { |
| 83889 | | - sqlite3ExplainPrintf(pOut, "EXISTS("); |
| 83890 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83891 | | - sqlite3ExplainPrintf(pOut,")"); |
| 83933 | + sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 83934 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83892 | 83935 | break; |
| 83893 | 83936 | } |
| 83894 | 83937 | case TK_SELECT: { |
| 83895 | | - sqlite3ExplainPrintf(pOut, "("); |
| 83896 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83897 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83938 | + sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 83939 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83898 | 83940 | break; |
| 83899 | 83941 | } |
| 83900 | 83942 | case TK_IN: { |
| 83901 | | - sqlite3ExplainPrintf(pOut, "IN("); |
| 83902 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83903 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83943 | + sqlite3TreeViewLine(pView, "IN"); |
| 83944 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 83904 | 83945 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 83905 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83946 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83906 | 83947 | }else{ |
| 83907 | | - sqlite3ExplainExprList(pOut, pExpr->x.pList); |
| 83948 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 83908 | 83949 | } |
| 83909 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83910 | 83950 | break; |
| 83911 | 83951 | } |
| 83912 | 83952 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 83913 | 83953 | |
| 83914 | 83954 | /* |
| | @@ -83924,17 +83964,14 @@ |
| 83924 | 83964 | */ |
| 83925 | 83965 | case TK_BETWEEN: { |
| 83926 | 83966 | Expr *pX = pExpr->pLeft; |
| 83927 | 83967 | Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 83928 | 83968 | Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 83929 | | - sqlite3ExplainPrintf(pOut, "BETWEEN("); |
| 83930 | | - sqlite3ExplainExpr(pOut, pX); |
| 83931 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83932 | | - sqlite3ExplainExpr(pOut, pY); |
| 83933 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83934 | | - sqlite3ExplainExpr(pOut, pZ); |
| 83935 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83969 | + sqlite3TreeViewLine(pView, "BETWEEN"); |
| 83970 | + sqlite3TreeViewExpr(pView, pX, 1); |
| 83971 | + sqlite3TreeViewExpr(pView, pY, 1); |
| 83972 | + sqlite3TreeViewExpr(pView, pZ, 0); |
| 83936 | 83973 | break; |
| 83937 | 83974 | } |
| 83938 | 83975 | case TK_TRIGGER: { |
| 83939 | 83976 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 83940 | 83977 | ** to a column in the new.* or old.* pseudo-tables available to |
| | @@ -83941,19 +83978,18 @@ |
| 83941 | 83978 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 83942 | 83979 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 83943 | 83980 | ** is set to the column of the pseudo-table to read, or to -1 to |
| 83944 | 83981 | ** read the rowid field. |
| 83945 | 83982 | */ |
| 83946 | | - sqlite3ExplainPrintf(pOut, "%s(%d)", |
| 83983 | + sqlite3TreeViewLine(pView, "%s(%d)", |
| 83947 | 83984 | pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 83948 | 83985 | break; |
| 83949 | 83986 | } |
| 83950 | 83987 | case TK_CASE: { |
| 83951 | | - sqlite3ExplainPrintf(pOut, "CASE("); |
| 83952 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83953 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83954 | | - sqlite3ExplainExprList(pOut, pExpr->x.pList); |
| 83988 | + sqlite3TreeViewLine(pView, "CASE"); |
| 83989 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 83990 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 83955 | 83991 | break; |
| 83956 | 83992 | } |
| 83957 | 83993 | #ifndef SQLITE_OMIT_TRIGGER |
| 83958 | 83994 | case TK_RAISE: { |
| 83959 | 83995 | const char *zType = "unk"; |
| | @@ -83961,59 +83997,61 @@ |
| 83961 | 83997 | case OE_Rollback: zType = "rollback"; break; |
| 83962 | 83998 | case OE_Abort: zType = "abort"; break; |
| 83963 | 83999 | case OE_Fail: zType = "fail"; break; |
| 83964 | 84000 | case OE_Ignore: zType = "ignore"; break; |
| 83965 | 84001 | } |
| 83966 | | - sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken); |
| 84002 | + sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 83967 | 84003 | break; |
| 83968 | 84004 | } |
| 83969 | 84005 | #endif |
| 84006 | + default: { |
| 84007 | + sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 84008 | + break; |
| 84009 | + } |
| 83970 | 84010 | } |
| 83971 | 84011 | if( zBinOp ){ |
| 83972 | | - sqlite3ExplainPrintf(pOut,"%s(", zBinOp); |
| 83973 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83974 | | - sqlite3ExplainPrintf(pOut,","); |
| 83975 | | - sqlite3ExplainExpr(pOut, pExpr->pRight); |
| 83976 | | - sqlite3ExplainPrintf(pOut,")"); |
| 84012 | + sqlite3TreeViewLine(pView, "%s", zBinOp); |
| 84013 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 84014 | + sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 83977 | 84015 | }else if( zUniOp ){ |
| 83978 | | - sqlite3ExplainPrintf(pOut,"%s(", zUniOp); |
| 83979 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83980 | | - sqlite3ExplainPrintf(pOut,")"); |
| 83981 | | - } |
| 83982 | | -} |
| 83983 | | -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */ |
| 83984 | | - |
| 83985 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 84016 | + sqlite3TreeViewLine(pView, "%s", zUniOp); |
| 84017 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 84018 | + } |
| 84019 | + sqlite3TreeViewPop(pView); |
| 84020 | +} |
| 84021 | +#endif /* SQLITE_DEBUG */ |
| 84022 | + |
| 84023 | +#ifdef SQLITE_DEBUG |
| 83986 | 84024 | /* |
| 83987 | 84025 | ** Generate a human-readable explanation of an expression list. |
| 83988 | 84026 | */ |
| 83989 | | -SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){ |
| 84027 | +SQLITE_PRIVATE void sqlite3TreeViewExprList( |
| 84028 | + TreeView *pView, |
| 84029 | + const ExprList *pList, |
| 84030 | + u8 moreToFollow, |
| 84031 | + const char *zLabel |
| 84032 | +){ |
| 83990 | 84033 | int i; |
| 83991 | | - if( pList==0 || pList->nExpr==0 ){ |
| 83992 | | - sqlite3ExplainPrintf(pOut, "(empty-list)"); |
| 83993 | | - return; |
| 83994 | | - }else if( pList->nExpr==1 ){ |
| 83995 | | - sqlite3ExplainExpr(pOut, pList->a[0].pExpr); |
| 84034 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 84035 | + if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 84036 | + if( pList==0 ){ |
| 84037 | + sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 83996 | 84038 | }else{ |
| 83997 | | - sqlite3ExplainPush(pOut); |
| 84039 | + sqlite3TreeViewLine(pView, "%s", zLabel); |
| 83998 | 84040 | for(i=0; i<pList->nExpr; i++){ |
| 83999 | | - sqlite3ExplainPrintf(pOut, "item[%d] = ", i); |
| 84000 | | - sqlite3ExplainPush(pOut); |
| 84001 | | - sqlite3ExplainExpr(pOut, pList->a[i].pExpr); |
| 84002 | | - sqlite3ExplainPop(pOut); |
| 84003 | | - if( pList->a[i].zName ){ |
| 84041 | + sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 84042 | +#if 0 |
| 84043 | + if( pList->a[i].zName ){ |
| 84004 | 84044 | sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); |
| 84005 | 84045 | } |
| 84006 | 84046 | if( pList->a[i].bSpanIsTab ){ |
| 84007 | 84047 | sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); |
| 84008 | 84048 | } |
| 84009 | | - if( i<pList->nExpr-1 ){ |
| 84010 | | - sqlite3ExplainNL(pOut); |
| 84011 | | - } |
| 84049 | +#endif |
| 84012 | 84050 | } |
| 84013 | | - sqlite3ExplainPop(pOut); |
| 84014 | 84051 | } |
| 84052 | + sqlite3TreeViewPop(pView); |
| 84015 | 84053 | } |
| 84016 | 84054 | #endif /* SQLITE_DEBUG */ |
| 84017 | 84055 | |
| 84018 | 84056 | /* |
| 84019 | 84057 | ** Generate code that pushes the value of every element of the given |
| | @@ -89633,11 +89671,11 @@ |
| 89633 | 89671 | Column *pCol; |
| 89634 | 89672 | sqlite3 *db = pParse->db; |
| 89635 | 89673 | p = pParse->pNewTable; |
| 89636 | 89674 | if( p!=0 ){ |
| 89637 | 89675 | pCol = &(p->aCol[p->nCol-1]); |
| 89638 | | - if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){ |
| 89676 | + if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ |
| 89639 | 89677 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
| 89640 | 89678 | pCol->zName); |
| 89641 | 89679 | }else{ |
| 89642 | 89680 | /* A copy of pExpr is used instead of the original, as pExpr contains |
| 89643 | 89681 | ** tokens that point to volatile memory. The 'span' of the expression |
| | @@ -94030,11 +94068,14 @@ |
| 94030 | 94068 | |
| 94031 | 94069 | /* |
| 94032 | 94070 | ** Return the collating function associated with a function. |
| 94033 | 94071 | */ |
| 94034 | 94072 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 94035 | | - return context->pColl; |
| 94073 | + VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 94074 | + assert( pOp->opcode==OP_CollSeq ); |
| 94075 | + assert( pOp->p4type==P4_COLLSEQ ); |
| 94076 | + return pOp->p4.pColl; |
| 94036 | 94077 | } |
| 94037 | 94078 | |
| 94038 | 94079 | /* |
| 94039 | 94080 | ** Indicate that the accumulator load should be skipped on this |
| 94040 | 94081 | ** iteration of the aggregate loop. |
| | @@ -94575,14 +94616,16 @@ |
| 94575 | 94616 | ** character is exactly one byte in size. Also, all characters are |
| 94576 | 94617 | ** able to participate in upper-case-to-lower-case mappings in EBCDIC |
| 94577 | 94618 | ** whereas only characters less than 0x80 do in ASCII. |
| 94578 | 94619 | */ |
| 94579 | 94620 | #if defined(SQLITE_EBCDIC) |
| 94580 | | -# define sqlite3Utf8Read(A) (*((*A)++)) |
| 94581 | | -# define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 94621 | +# define sqlite3Utf8Read(A) (*((*A)++)) |
| 94622 | +# define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 94623 | +# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 94582 | 94624 | #else |
| 94583 | | -# define GlobUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; } |
| 94625 | +# define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } |
| 94626 | +# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 94584 | 94627 | #endif |
| 94585 | 94628 | |
| 94586 | 94629 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 94587 | 94630 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 94588 | 94631 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| | @@ -94591,11 +94634,11 @@ |
| 94591 | 94634 | ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 94592 | 94635 | static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
| 94593 | 94636 | |
| 94594 | 94637 | /* |
| 94595 | 94638 | ** Compare two UTF-8 strings for equality where the first string can |
| 94596 | | -** potentially be a "glob" expression. Return true (1) if they |
| 94639 | +** potentially be a "glob" or "like" expression. Return true (1) if they |
| 94597 | 94640 | ** are the same and false (0) if they are different. |
| 94598 | 94641 | ** |
| 94599 | 94642 | ** Globbing rules: |
| 94600 | 94643 | ** |
| 94601 | 94644 | ** '*' Matches any sequence of zero or more characters. |
| | @@ -94611,120 +94654,147 @@ |
| 94611 | 94654 | ** in the list by making it the first character after '[' or '^'. A |
| 94612 | 94655 | ** range of characters can be specified using '-'. Example: |
| 94613 | 94656 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 94614 | 94657 | ** it the last character in the list. |
| 94615 | 94658 | ** |
| 94659 | +** Like matching rules: |
| 94660 | +** |
| 94661 | +** '%' Matches any sequence of zero or more characters |
| 94662 | +** |
| 94663 | +*** '_' Matches any one character |
| 94664 | +** |
| 94665 | +** Ec Where E is the "esc" character and c is any other |
| 94666 | +** character, including '%', '_', and esc, match exactly c. |
| 94667 | +** |
| 94668 | +** The comments through this routine usually assume glob matching. |
| 94669 | +** |
| 94616 | 94670 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 94617 | | -** |
| 94618 | | -** Hints: to match '*' or '?', put them in "[]". Like this: |
| 94619 | | -** |
| 94620 | | -** abc[*]xyz Matches "abc*xyz" only |
| 94621 | 94671 | */ |
| 94622 | 94672 | static int patternCompare( |
| 94623 | 94673 | const u8 *zPattern, /* The glob pattern */ |
| 94624 | 94674 | const u8 *zString, /* The string to compare against the glob */ |
| 94625 | 94675 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 94626 | 94676 | u32 esc /* The escape character */ |
| 94627 | 94677 | ){ |
| 94628 | | - u32 c, c2; |
| 94629 | | - int invert; |
| 94630 | | - int seen; |
| 94631 | | - u8 matchOne = pInfo->matchOne; |
| 94632 | | - u8 matchAll = pInfo->matchAll; |
| 94633 | | - u8 matchSet = pInfo->matchSet; |
| 94634 | | - u8 noCase = pInfo->noCase; |
| 94635 | | - int prevEscape = 0; /* True if the previous character was 'escape' */ |
| 94678 | + u32 c, c2; /* Next pattern and input string chars */ |
| 94679 | + u32 matchOne = pInfo->matchOne; /* "?" or "_" */ |
| 94680 | + u32 matchAll = pInfo->matchAll; /* "*" or "%" */ |
| 94681 | + u32 matchOther; /* "[" or the escape character */ |
| 94682 | + u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ |
| 94683 | + const u8 *zEscaped = 0; /* One past the last escaped input char */ |
| 94684 | + |
| 94685 | + /* The GLOB operator does not have an ESCAPE clause. And LIKE does not |
| 94686 | + ** have the matchSet operator. So we either have to look for one or |
| 94687 | + ** the other, never both. Hence the single variable matchOther is used |
| 94688 | + ** to store the one we have to look for. |
| 94689 | + */ |
| 94690 | + matchOther = esc ? esc : pInfo->matchSet; |
| 94636 | 94691 | |
| 94637 | 94692 | while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ |
| 94638 | | - if( c==matchAll && !prevEscape ){ |
| 94693 | + if( c==matchAll ){ /* Match "*" */ |
| 94694 | + /* Skip over multiple "*" characters in the pattern. If there |
| 94695 | + ** are also "?" characters, skip those as well, but consume a |
| 94696 | + ** single character of the input string for each "?" skipped */ |
| 94639 | 94697 | while( (c=sqlite3Utf8Read(&zPattern)) == matchAll |
| 94640 | 94698 | || c == matchOne ){ |
| 94641 | 94699 | if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
| 94642 | 94700 | return 0; |
| 94643 | 94701 | } |
| 94644 | 94702 | } |
| 94645 | 94703 | if( c==0 ){ |
| 94646 | | - return 1; |
| 94647 | | - }else if( c==esc ){ |
| 94648 | | - c = sqlite3Utf8Read(&zPattern); |
| 94649 | | - if( c==0 ){ |
| 94650 | | - return 0; |
| 94651 | | - } |
| 94652 | | - }else if( c==matchSet ){ |
| 94653 | | - assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 94654 | | - assert( matchSet<0x80 ); /* '[' is a single-byte character */ |
| 94655 | | - while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
| 94656 | | - SQLITE_SKIP_UTF8(zString); |
| 94657 | | - } |
| 94658 | | - return *zString!=0; |
| 94659 | | - } |
| 94660 | | - while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 94661 | | - if( noCase ){ |
| 94662 | | - GlobUpperToLower(c2); |
| 94663 | | - GlobUpperToLower(c); |
| 94664 | | - while( c2 != 0 && c2 != c ){ |
| 94665 | | - c2 = sqlite3Utf8Read(&zString); |
| 94666 | | - GlobUpperToLower(c2); |
| 94667 | | - } |
| 94668 | | - }else{ |
| 94669 | | - while( c2 != 0 && c2 != c ){ |
| 94670 | | - c2 = sqlite3Utf8Read(&zString); |
| 94671 | | - } |
| 94672 | | - } |
| 94673 | | - if( c2==0 ) return 0; |
| 94674 | | - if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94675 | | - } |
| 94676 | | - return 0; |
| 94677 | | - }else if( c==matchOne && !prevEscape ){ |
| 94678 | | - if( sqlite3Utf8Read(&zString)==0 ){ |
| 94679 | | - return 0; |
| 94680 | | - } |
| 94681 | | - }else if( c==matchSet ){ |
| 94682 | | - u32 prior_c = 0; |
| 94683 | | - assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
| 94684 | | - seen = 0; |
| 94685 | | - invert = 0; |
| 94686 | | - c = sqlite3Utf8Read(&zString); |
| 94687 | | - if( c==0 ) return 0; |
| 94688 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94689 | | - if( c2=='^' ){ |
| 94690 | | - invert = 1; |
| 94691 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94692 | | - } |
| 94693 | | - if( c2==']' ){ |
| 94694 | | - if( c==']' ) seen = 1; |
| 94695 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94696 | | - } |
| 94697 | | - while( c2 && c2!=']' ){ |
| 94698 | | - if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 94699 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94700 | | - if( c>=prior_c && c<=c2 ) seen = 1; |
| 94701 | | - prior_c = 0; |
| 94702 | | - }else{ |
| 94703 | | - if( c==c2 ){ |
| 94704 | | - seen = 1; |
| 94705 | | - } |
| 94706 | | - prior_c = c2; |
| 94707 | | - } |
| 94708 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94709 | | - } |
| 94710 | | - if( c2==0 || (seen ^ invert)==0 ){ |
| 94711 | | - return 0; |
| 94712 | | - } |
| 94713 | | - }else if( esc==c && !prevEscape ){ |
| 94714 | | - prevEscape = 1; |
| 94715 | | - }else{ |
| 94716 | | - c2 = sqlite3Utf8Read(&zString); |
| 94717 | | - if( noCase ){ |
| 94718 | | - GlobUpperToLower(c); |
| 94719 | | - GlobUpperToLower(c2); |
| 94720 | | - } |
| 94721 | | - if( c!=c2 ){ |
| 94722 | | - return 0; |
| 94723 | | - } |
| 94724 | | - prevEscape = 0; |
| 94725 | | - } |
| 94704 | + return 1; /* "*" at the end of the pattern matches */ |
| 94705 | + }else if( c==matchOther ){ |
| 94706 | + if( esc ){ |
| 94707 | + c = sqlite3Utf8Read(&zPattern); |
| 94708 | + if( c==0 ) return 0; |
| 94709 | + }else{ |
| 94710 | + /* "[...]" immediately follows the "*". We have to do a slow |
| 94711 | + ** recursive search in this case, but it is an unusual case. */ |
| 94712 | + assert( matchOther<0x80 ); /* '[' is a single-byte character */ |
| 94713 | + while( *zString |
| 94714 | + && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
| 94715 | + SQLITE_SKIP_UTF8(zString); |
| 94716 | + } |
| 94717 | + return *zString!=0; |
| 94718 | + } |
| 94719 | + } |
| 94720 | + |
| 94721 | + /* At this point variable c contains the first character of the |
| 94722 | + ** pattern string past the "*". Search in the input string for the |
| 94723 | + ** first matching character and recursively contine the match from |
| 94724 | + ** that point. |
| 94725 | + ** |
| 94726 | + ** For a case-insensitive search, set variable cx to be the same as |
| 94727 | + ** c but in the other case and search the input string for either |
| 94728 | + ** c or cx. |
| 94729 | + */ |
| 94730 | + if( c<=0x80 ){ |
| 94731 | + u32 cx; |
| 94732 | + if( noCase ){ |
| 94733 | + cx = sqlite3Toupper(c); |
| 94734 | + c = sqlite3Tolower(c); |
| 94735 | + }else{ |
| 94736 | + cx = c; |
| 94737 | + } |
| 94738 | + while( (c2 = *(zString++))!=0 ){ |
| 94739 | + if( c2!=c && c2!=cx ) continue; |
| 94740 | + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94741 | + } |
| 94742 | + }else{ |
| 94743 | + while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 94744 | + if( c2!=c ) continue; |
| 94745 | + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94746 | + } |
| 94747 | + } |
| 94748 | + return 0; |
| 94749 | + } |
| 94750 | + if( c==matchOther ){ |
| 94751 | + if( esc ){ |
| 94752 | + c = sqlite3Utf8Read(&zPattern); |
| 94753 | + if( c==0 ) return 0; |
| 94754 | + zEscaped = zPattern; |
| 94755 | + }else{ |
| 94756 | + u32 prior_c = 0; |
| 94757 | + int seen = 0; |
| 94758 | + int invert = 0; |
| 94759 | + c = sqlite3Utf8Read(&zString); |
| 94760 | + if( c==0 ) return 0; |
| 94761 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94762 | + if( c2=='^' ){ |
| 94763 | + invert = 1; |
| 94764 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94765 | + } |
| 94766 | + if( c2==']' ){ |
| 94767 | + if( c==']' ) seen = 1; |
| 94768 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94769 | + } |
| 94770 | + while( c2 && c2!=']' ){ |
| 94771 | + if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 94772 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94773 | + if( c>=prior_c && c<=c2 ) seen = 1; |
| 94774 | + prior_c = 0; |
| 94775 | + }else{ |
| 94776 | + if( c==c2 ){ |
| 94777 | + seen = 1; |
| 94778 | + } |
| 94779 | + prior_c = c2; |
| 94780 | + } |
| 94781 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94782 | + } |
| 94783 | + if( c2==0 || (seen ^ invert)==0 ){ |
| 94784 | + return 0; |
| 94785 | + } |
| 94786 | + continue; |
| 94787 | + } |
| 94788 | + } |
| 94789 | + c2 = sqlite3Utf8Read(&zString); |
| 94790 | + if( c==c2 ) continue; |
| 94791 | + if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ |
| 94792 | + continue; |
| 94793 | + } |
| 94794 | + if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
| 94795 | + return 0; |
| 94726 | 94796 | } |
| 94727 | 94797 | return *zString==0; |
| 94728 | 94798 | } |
| 94729 | 94799 | |
| 94730 | 94800 | /* |
| | @@ -103884,10 +103954,24 @@ |
| 103884 | 103954 | ** |
| 103885 | 103955 | ************************************************************************* |
| 103886 | 103956 | ** This file contains C code routines that are called by the parser |
| 103887 | 103957 | ** to handle SELECT statements in SQLite. |
| 103888 | 103958 | */ |
| 103959 | + |
| 103960 | +/* |
| 103961 | +** Trace output macros |
| 103962 | +*/ |
| 103963 | +#if SELECTTRACE_ENABLED |
| 103964 | +/***/ int sqlite3SelectTrace = 0; |
| 103965 | +# define SELECTTRACE(K,P,S,X) \ |
| 103966 | + if(sqlite3SelectTrace&(K)) \ |
| 103967 | + sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\ |
| 103968 | + sqlite3DebugPrintf X |
| 103969 | +#else |
| 103970 | +# define SELECTTRACE(K,P,S,X) |
| 103971 | +#endif |
| 103972 | + |
| 103889 | 103973 | |
| 103890 | 103974 | /* |
| 103891 | 103975 | ** An instance of the following object is used to record information about |
| 103892 | 103976 | ** how to process the DISTINCT keyword, to simplify passing that information |
| 103893 | 103977 | ** into the selectInnerLoop() routine. |
| | @@ -103996,10 +104080,22 @@ |
| 103996 | 104080 | assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
| 103997 | 104081 | } |
| 103998 | 104082 | assert( pNew!=&standin ); |
| 103999 | 104083 | return pNew; |
| 104000 | 104084 | } |
| 104085 | + |
| 104086 | +#if SELECTTRACE_ENABLED |
| 104087 | +/* |
| 104088 | +** Set the name of a Select object |
| 104089 | +*/ |
| 104090 | +SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){ |
| 104091 | + if( p && zName ){ |
| 104092 | + sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); |
| 104093 | + } |
| 104094 | +} |
| 104095 | +#endif |
| 104096 | + |
| 104001 | 104097 | |
| 104002 | 104098 | /* |
| 104003 | 104099 | ** Delete the given Select structure and all of its substructures. |
| 104004 | 104100 | */ |
| 104005 | 104101 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
| | @@ -107226,10 +107322,12 @@ |
| 107226 | 107322 | } |
| 107227 | 107323 | } |
| 107228 | 107324 | } |
| 107229 | 107325 | |
| 107230 | 107326 | /***** If we reach this point, flattening is permitted. *****/ |
| 107327 | + SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n", |
| 107328 | + pSub->zSelName, pSub, iFrom)); |
| 107231 | 107329 | |
| 107232 | 107330 | /* Authorize the subquery */ |
| 107233 | 107331 | pParse->zAuthContext = pSubitem->zName; |
| 107234 | 107332 | TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); |
| 107235 | 107333 | testcase( i==SQLITE_DENY ); |
| | @@ -107278,10 +107376,11 @@ |
| 107278 | 107376 | p->pSrc = 0; |
| 107279 | 107377 | p->pPrior = 0; |
| 107280 | 107378 | p->pLimit = 0; |
| 107281 | 107379 | p->pOffset = 0; |
| 107282 | 107380 | pNew = sqlite3SelectDup(db, p, 0); |
| 107381 | + sqlite3SelectSetName(pNew, pSub->zSelName); |
| 107283 | 107382 | p->pOffset = pOffset; |
| 107284 | 107383 | p->pLimit = pLimit; |
| 107285 | 107384 | p->pOrderBy = pOrderBy; |
| 107286 | 107385 | p->pSrc = pSrc; |
| 107287 | 107386 | p->op = TK_ALL; |
| | @@ -107290,10 +107389,13 @@ |
| 107290 | 107389 | }else{ |
| 107291 | 107390 | pNew->pPrior = pPrior; |
| 107292 | 107391 | if( pPrior ) pPrior->pNext = pNew; |
| 107293 | 107392 | pNew->pNext = p; |
| 107294 | 107393 | p->pPrior = pNew; |
| 107394 | + SELECTTRACE(2,pParse,p, |
| 107395 | + ("compound-subquery flattener creates %s.%p as peer\n", |
| 107396 | + pNew->zSelName, pNew)); |
| 107295 | 107397 | } |
| 107296 | 107398 | if( db->mallocFailed ) return 1; |
| 107297 | 107399 | } |
| 107298 | 107400 | |
| 107299 | 107401 | /* Begin flattening the iFrom-th entry of the FROM clause |
| | @@ -107419,12 +107521,27 @@ |
| 107419 | 107521 | if( isAgg ){ |
| 107420 | 107522 | substExprList(db, pParent->pGroupBy, iParent, pSub->pEList); |
| 107421 | 107523 | pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList); |
| 107422 | 107524 | } |
| 107423 | 107525 | if( pSub->pOrderBy ){ |
| 107526 | + /* At this point, any non-zero iOrderByCol values indicate that the |
| 107527 | + ** ORDER BY column expression is identical to the iOrderByCol'th |
| 107528 | + ** expression returned by SELECT statement pSub. Since these values |
| 107529 | + ** do not necessarily correspond to columns in SELECT statement pParent, |
| 107530 | + ** zero them before transfering the ORDER BY clause. |
| 107531 | + ** |
| 107532 | + ** Not doing this may cause an error if a subsequent call to this |
| 107533 | + ** function attempts to flatten a compound sub-query into pParent |
| 107534 | + ** (the only way this can happen is if the compound sub-query is |
| 107535 | + ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */ |
| 107536 | + ExprList *pOrderBy = pSub->pOrderBy; |
| 107537 | + for(i=0; i<pOrderBy->nExpr; i++){ |
| 107538 | + pOrderBy->a[i].u.x.iOrderByCol = 0; |
| 107539 | + } |
| 107424 | 107540 | assert( pParent->pOrderBy==0 ); |
| 107425 | | - pParent->pOrderBy = pSub->pOrderBy; |
| 107541 | + assert( pSub->pPrior==0 ); |
| 107542 | + pParent->pOrderBy = pOrderBy; |
| 107426 | 107543 | pSub->pOrderBy = 0; |
| 107427 | 107544 | }else if( pParent->pOrderBy ){ |
| 107428 | 107545 | substExprList(db, pParent->pOrderBy, iParent, pSub->pEList); |
| 107429 | 107546 | } |
| 107430 | 107547 | if( pSub->pWhere ){ |
| | @@ -107465,10 +107582,17 @@ |
| 107465 | 107582 | |
| 107466 | 107583 | /* Finially, delete what is left of the subquery and return |
| 107467 | 107584 | ** success. |
| 107468 | 107585 | */ |
| 107469 | 107586 | sqlite3SelectDelete(db, pSub1); |
| 107587 | + |
| 107588 | +#if SELECTTRACE_ENABLED |
| 107589 | + if( sqlite3SelectTrace & 0x100 ){ |
| 107590 | + sqlite3DebugPrintf("After flattening:\n"); |
| 107591 | + sqlite3TreeViewSelect(0, p, 0); |
| 107592 | + } |
| 107593 | +#endif |
| 107470 | 107594 | |
| 107471 | 107595 | return 1; |
| 107472 | 107596 | } |
| 107473 | 107597 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 107474 | 107598 | |
| | @@ -107936,10 +108060,11 @@ |
| 107936 | 108060 | if( pTab->pSelect || IsVirtual(pTab) ){ |
| 107937 | 108061 | /* We reach here if the named table is a really a view */ |
| 107938 | 108062 | if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; |
| 107939 | 108063 | assert( pFrom->pSelect==0 ); |
| 107940 | 108064 | pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); |
| 108065 | + sqlite3SelectSetName(pFrom->pSelect, pTab->zName); |
| 107941 | 108066 | sqlite3WalkSelect(pWalker, pFrom->pSelect); |
| 107942 | 108067 | } |
| 107943 | 108068 | #endif |
| 107944 | 108069 | } |
| 107945 | 108070 | |
| | @@ -108470,10 +108595,17 @@ |
| 108470 | 108595 | if( p==0 || db->mallocFailed || pParse->nErr ){ |
| 108471 | 108596 | return 1; |
| 108472 | 108597 | } |
| 108473 | 108598 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
| 108474 | 108599 | memset(&sAggInfo, 0, sizeof(sAggInfo)); |
| 108600 | +#if SELECTTRACE_ENABLED |
| 108601 | + pParse->nSelectIndent++; |
| 108602 | + SELECTTRACE(1,pParse,p, ("begin processing:\n")); |
| 108603 | + if( sqlite3SelectTrace & 0x100 ){ |
| 108604 | + sqlite3TreeViewSelect(0, p, 0); |
| 108605 | + } |
| 108606 | +#endif |
| 108475 | 108607 | |
| 108476 | 108608 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo ); |
| 108477 | 108609 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo ); |
| 108478 | 108610 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue ); |
| 108479 | 108611 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue ); |
| | @@ -108626,10 +108758,14 @@ |
| 108626 | 108758 | /* If there is are a sequence of queries, do the earlier ones first. |
| 108627 | 108759 | */ |
| 108628 | 108760 | if( p->pPrior ){ |
| 108629 | 108761 | rc = multiSelect(pParse, p, pDest); |
| 108630 | 108762 | explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 108763 | +#if SELECTTRACE_ENABLED |
| 108764 | + SELECTTRACE(1,pParse,p,("end compound-select processing\n")); |
| 108765 | + pParse->nSelectIndent--; |
| 108766 | +#endif |
| 108631 | 108767 | return rc; |
| 108632 | 108768 | } |
| 108633 | 108769 | #endif |
| 108634 | 108770 | |
| 108635 | 108771 | /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and |
| | @@ -109225,107 +109361,110 @@ |
| 109225 | 109361 | generateColumnNames(pParse, pTabList, pEList); |
| 109226 | 109362 | } |
| 109227 | 109363 | |
| 109228 | 109364 | sqlite3DbFree(db, sAggInfo.aCol); |
| 109229 | 109365 | sqlite3DbFree(db, sAggInfo.aFunc); |
| 109366 | +#if SELECTTRACE_ENABLED |
| 109367 | + SELECTTRACE(1,pParse,p,("end processing\n")); |
| 109368 | + pParse->nSelectIndent--; |
| 109369 | +#endif |
| 109230 | 109370 | return rc; |
| 109231 | 109371 | } |
| 109232 | 109372 | |
| 109233 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 109373 | +#ifdef SQLITE_DEBUG |
| 109234 | 109374 | /* |
| 109235 | 109375 | ** Generate a human-readable description of a the Select object. |
| 109236 | 109376 | */ |
| 109237 | | -static void explainOneSelect(Vdbe *pVdbe, Select *p){ |
| 109238 | | - sqlite3ExplainPrintf(pVdbe, "SELECT "); |
| 109239 | | - if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
| 109240 | | - if( p->selFlags & SF_Distinct ){ |
| 109241 | | - sqlite3ExplainPrintf(pVdbe, "DISTINCT "); |
| 109242 | | - } |
| 109243 | | - if( p->selFlags & SF_Aggregate ){ |
| 109244 | | - sqlite3ExplainPrintf(pVdbe, "agg_flag "); |
| 109245 | | - } |
| 109246 | | - sqlite3ExplainNL(pVdbe); |
| 109247 | | - sqlite3ExplainPrintf(pVdbe, " "); |
| 109248 | | - } |
| 109249 | | - sqlite3ExplainExprList(pVdbe, p->pEList); |
| 109250 | | - sqlite3ExplainNL(pVdbe); |
| 109377 | +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 109378 | + int n = 0; |
| 109379 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 109380 | + sqlite3TreeViewLine(pView, "SELECT%s%s", |
| 109381 | + ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 109382 | + ((p->selFlags & SF_Aggregate) ? " agg_flag" : "") |
| 109383 | + ); |
| 109384 | + if( p->pSrc && p->pSrc->nSrc ) n++; |
| 109385 | + if( p->pWhere ) n++; |
| 109386 | + if( p->pGroupBy ) n++; |
| 109387 | + if( p->pHaving ) n++; |
| 109388 | + if( p->pOrderBy ) n++; |
| 109389 | + if( p->pLimit ) n++; |
| 109390 | + if( p->pOffset ) n++; |
| 109391 | + if( p->pPrior ) n++; |
| 109392 | + sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
| 109251 | 109393 | if( p->pSrc && p->pSrc->nSrc ){ |
| 109252 | 109394 | int i; |
| 109253 | | - sqlite3ExplainPrintf(pVdbe, "FROM "); |
| 109254 | | - sqlite3ExplainPush(pVdbe); |
| 109395 | + pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 109396 | + sqlite3TreeViewLine(pView, "FROM"); |
| 109255 | 109397 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 109256 | 109398 | struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 109257 | | - sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor); |
| 109258 | | - if( pItem->pSelect ){ |
| 109259 | | - sqlite3ExplainSelect(pVdbe, pItem->pSelect); |
| 109260 | | - if( pItem->pTab ){ |
| 109261 | | - sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName); |
| 109262 | | - } |
| 109399 | + StrAccum x; |
| 109400 | + char zLine[100]; |
| 109401 | + sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0); |
| 109402 | + sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 109403 | + if( pItem->zDatabase ){ |
| 109404 | + sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 109263 | 109405 | }else if( pItem->zName ){ |
| 109264 | | - sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName); |
| 109406 | + sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| 109407 | + } |
| 109408 | + if( pItem->pTab ){ |
| 109409 | + sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName); |
| 109265 | 109410 | } |
| 109266 | 109411 | if( pItem->zAlias ){ |
| 109267 | | - sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias); |
| 109412 | + sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias); |
| 109268 | 109413 | } |
| 109269 | 109414 | if( pItem->jointype & JT_LEFT ){ |
| 109270 | | - sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN"); |
| 109415 | + sqlite3XPrintf(&x, 0, " LEFT-JOIN"); |
| 109271 | 109416 | } |
| 109272 | | - sqlite3ExplainNL(pVdbe); |
| 109417 | + sqlite3StrAccumFinish(&x); |
| 109418 | + sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 109419 | + if( pItem->pSelect ){ |
| 109420 | + sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 109421 | + } |
| 109422 | + sqlite3TreeViewPop(pView); |
| 109273 | 109423 | } |
| 109274 | | - sqlite3ExplainPop(pVdbe); |
| 109424 | + sqlite3TreeViewPop(pView); |
| 109275 | 109425 | } |
| 109276 | 109426 | if( p->pWhere ){ |
| 109277 | | - sqlite3ExplainPrintf(pVdbe, "WHERE "); |
| 109278 | | - sqlite3ExplainExpr(pVdbe, p->pWhere); |
| 109279 | | - sqlite3ExplainNL(pVdbe); |
| 109427 | + sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 109428 | + sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 109429 | + sqlite3TreeViewPop(pView); |
| 109280 | 109430 | } |
| 109281 | 109431 | if( p->pGroupBy ){ |
| 109282 | | - sqlite3ExplainPrintf(pVdbe, "GROUPBY "); |
| 109283 | | - sqlite3ExplainExprList(pVdbe, p->pGroupBy); |
| 109284 | | - sqlite3ExplainNL(pVdbe); |
| 109432 | + sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 109285 | 109433 | } |
| 109286 | 109434 | if( p->pHaving ){ |
| 109287 | | - sqlite3ExplainPrintf(pVdbe, "HAVING "); |
| 109288 | | - sqlite3ExplainExpr(pVdbe, p->pHaving); |
| 109289 | | - sqlite3ExplainNL(pVdbe); |
| 109435 | + sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 109436 | + sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 109437 | + sqlite3TreeViewPop(pView); |
| 109290 | 109438 | } |
| 109291 | 109439 | if( p->pOrderBy ){ |
| 109292 | | - sqlite3ExplainPrintf(pVdbe, "ORDERBY "); |
| 109293 | | - sqlite3ExplainExprList(pVdbe, p->pOrderBy); |
| 109294 | | - sqlite3ExplainNL(pVdbe); |
| 109440 | + sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 109295 | 109441 | } |
| 109296 | 109442 | if( p->pLimit ){ |
| 109297 | | - sqlite3ExplainPrintf(pVdbe, "LIMIT "); |
| 109298 | | - sqlite3ExplainExpr(pVdbe, p->pLimit); |
| 109299 | | - sqlite3ExplainNL(pVdbe); |
| 109443 | + sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
| 109444 | + sqlite3TreeViewExpr(pView, p->pLimit, 0); |
| 109445 | + sqlite3TreeViewPop(pView); |
| 109300 | 109446 | } |
| 109301 | 109447 | if( p->pOffset ){ |
| 109302 | | - sqlite3ExplainPrintf(pVdbe, "OFFSET "); |
| 109303 | | - sqlite3ExplainExpr(pVdbe, p->pOffset); |
| 109304 | | - sqlite3ExplainNL(pVdbe); |
| 109305 | | - } |
| 109306 | | -} |
| 109307 | | -SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){ |
| 109308 | | - if( p==0 ){ |
| 109309 | | - sqlite3ExplainPrintf(pVdbe, "(null-select)"); |
| 109310 | | - return; |
| 109311 | | - } |
| 109312 | | - sqlite3ExplainPush(pVdbe); |
| 109313 | | - while( p ){ |
| 109314 | | - explainOneSelect(pVdbe, p); |
| 109315 | | - p = p->pNext; |
| 109316 | | - if( p==0 ) break; |
| 109317 | | - sqlite3ExplainNL(pVdbe); |
| 109318 | | - sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op)); |
| 109319 | | - } |
| 109320 | | - sqlite3ExplainPrintf(pVdbe, "END"); |
| 109321 | | - sqlite3ExplainPop(pVdbe); |
| 109322 | | -} |
| 109323 | | - |
| 109324 | | -/* End of the structure debug printing code |
| 109325 | | -*****************************************************************************/ |
| 109326 | | -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */ |
| 109448 | + sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 109449 | + sqlite3TreeViewExpr(pView, p->pOffset, 0); |
| 109450 | + sqlite3TreeViewPop(pView); |
| 109451 | + } |
| 109452 | + if( p->pPrior ){ |
| 109453 | + const char *zOp = "UNION"; |
| 109454 | + switch( p->op ){ |
| 109455 | + case TK_ALL: zOp = "UNION ALL"; break; |
| 109456 | + case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 109457 | + case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 109458 | + } |
| 109459 | + sqlite3TreeViewItem(pView, zOp, (n--)>0); |
| 109460 | + sqlite3TreeViewSelect(pView, p->pPrior, 0); |
| 109461 | + sqlite3TreeViewPop(pView); |
| 109462 | + } |
| 109463 | + sqlite3TreeViewPop(pView); |
| 109464 | +} |
| 109465 | +#endif /* SQLITE_DEBUG */ |
| 109327 | 109466 | |
| 109328 | 109467 | /************** End of select.c **********************************************/ |
| 109329 | 109468 | /************** Begin file table.c *******************************************/ |
| 109330 | 109469 | /* |
| 109331 | 109470 | ** 2001 September 15 |
| | @@ -113719,15 +113858,10 @@ |
| 113719 | 113858 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 113720 | 113859 | assert( TK_GE==TK_EQ+4 ); |
| 113721 | 113860 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
| 113722 | 113861 | } |
| 113723 | 113862 | |
| 113724 | | -/* |
| 113725 | | -** Swap two objects of type TYPE. |
| 113726 | | -*/ |
| 113727 | | -#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 113728 | | - |
| 113729 | 113863 | /* |
| 113730 | 113864 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 113731 | 113865 | ** are converted into "Y op X". |
| 113732 | 113866 | ** |
| 113733 | 113867 | ** If left/right precedence rules come into play when determining the |
| | @@ -116884,12 +117018,13 @@ |
| 116884 | 117018 | |
| 116885 | 117019 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 116886 | 117020 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 116887 | 117021 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 116888 | 117022 | */ |
| 116889 | | - wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 116890 | | - WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY; |
| 117023 | + wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 117024 | + | WHERE_FORCE_TABLE |
| 117025 | + | WHERE_ONETABLE_ONLY; |
| 116891 | 117026 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 116892 | 117027 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 116893 | 117028 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 116894 | 117029 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 116895 | 117030 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| | @@ -116897,10 +117032,11 @@ |
| 116897 | 117032 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
| 116898 | 117033 | pAndExpr->pLeft = pOrExpr; |
| 116899 | 117034 | pOrExpr = pAndExpr; |
| 116900 | 117035 | } |
| 116901 | 117036 | /* Loop through table entries that match term pOrTerm. */ |
| 117037 | + WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 116902 | 117038 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 116903 | 117039 | wctrlFlags, iCovCur); |
| 116904 | 117040 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 116905 | 117041 | if( pSubWInfo ){ |
| 116906 | 117042 | WhereLoop *pSubLoop; |
| | @@ -117116,25 +117252,30 @@ |
| 117116 | 117252 | } |
| 117117 | 117253 | |
| 117118 | 117254 | return pLevel->notReady; |
| 117119 | 117255 | } |
| 117120 | 117256 | |
| 117121 | | -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 117257 | +#ifdef WHERETRACE_ENABLED |
| 117122 | 117258 | /* |
| 117123 | | -** Generate "Explanation" text for a WhereTerm. |
| 117259 | +** Print the content of a WhereTerm object |
| 117124 | 117260 | */ |
| 117125 | | -static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){ |
| 117126 | | - char zType[4]; |
| 117127 | | - memcpy(zType, "...", 4); |
| 117128 | | - if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 117129 | | - if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 117130 | | - if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 117131 | | - sqlite3ExplainPrintf(v, "%s ", zType); |
| 117132 | | - sqlite3ExplainExpr(v, pTerm->pExpr); |
| 117133 | | -} |
| 117134 | | -#endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */ |
| 117135 | | - |
| 117261 | +static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
| 117262 | + if( pTerm==0 ){ |
| 117263 | + sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 117264 | + }else{ |
| 117265 | + char zType[4]; |
| 117266 | + memcpy(zType, "...", 4); |
| 117267 | + if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 117268 | + if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 117269 | + if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 117270 | + sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n", |
| 117271 | + iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 117272 | + pTerm->eOperator); |
| 117273 | + sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 117274 | + } |
| 117275 | +} |
| 117276 | +#endif |
| 117136 | 117277 | |
| 117137 | 117278 | #ifdef WHERETRACE_ENABLED |
| 117138 | 117279 | /* |
| 117139 | 117280 | ** Print a WhereLoop object for debugging purposes |
| 117140 | 117281 | */ |
| | @@ -117174,31 +117315,16 @@ |
| 117174 | 117315 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); |
| 117175 | 117316 | }else{ |
| 117176 | 117317 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117177 | 117318 | } |
| 117178 | 117319 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117179 | | -#ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 117180 | | - /* If the 0x100 bit of wheretracing is set, then show all of the constraint |
| 117181 | | - ** expressions in the WhereLoop.aLTerm[] array. |
| 117182 | | - */ |
| 117183 | | - if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ /* WHERETRACE 0x100 */ |
| 117320 | + if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 117184 | 117321 | int i; |
| 117185 | | - Vdbe *v = pWInfo->pParse->pVdbe; |
| 117186 | | - sqlite3ExplainBegin(v); |
| 117187 | 117322 | for(i=0; i<p->nLTerm; i++){ |
| 117188 | | - WhereTerm *pTerm = p->aLTerm[i]; |
| 117189 | | - if( pTerm==0 ) continue; |
| 117190 | | - sqlite3ExplainPrintf(v, " (%d) #%-2d ", i+1, (int)(pTerm-pWC->a)); |
| 117191 | | - sqlite3ExplainPush(v); |
| 117192 | | - whereExplainTerm(v, pTerm); |
| 117193 | | - sqlite3ExplainPop(v); |
| 117194 | | - sqlite3ExplainNL(v); |
| 117195 | | - } |
| 117196 | | - sqlite3ExplainFinish(v); |
| 117197 | | - sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 117198 | | - } |
| 117199 | | -#endif |
| 117323 | + whereTermPrint(p->aLTerm[i], i); |
| 117324 | + } |
| 117325 | + } |
| 117200 | 117326 | } |
| 117201 | 117327 | #endif |
| 117202 | 117328 | |
| 117203 | 117329 | /* |
| 117204 | 117330 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| | @@ -117714,15 +117840,18 @@ |
| 117714 | 117840 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 117715 | 117841 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 117716 | 117842 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 117717 | 117843 | if( pTerm ){ |
| 117718 | 117844 | /* TUNING: When estimating skip-scan for a term that is also indexable, |
| 117719 | | - ** increase the cost of the skip-scan by 2x, to make it a little less |
| 117845 | + ** multiply the cost of the skip-scan by 2.0, to make it a little less |
| 117720 | 117846 | ** desirable than the regular index lookup. */ |
| 117721 | 117847 | nIter += 10; assert( 10==sqlite3LogEst(2) ); |
| 117722 | 117848 | } |
| 117723 | 117849 | pNew->nOut -= nIter; |
| 117850 | + /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 117851 | + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 117852 | + nIter += 5; |
| 117724 | 117853 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 117725 | 117854 | pNew->nOut = saved_nOut; |
| 117726 | 117855 | pNew->u.btree.nEq = saved_nEq; |
| 117727 | 117856 | pNew->u.btree.nSkip = saved_nSkip; |
| 117728 | 117857 | } |
| | @@ -118073,13 +118202,21 @@ |
| 118073 | 118202 | pNew->u.btree.nSkip = 0; |
| 118074 | 118203 | pNew->u.btree.pIndex = 0; |
| 118075 | 118204 | pNew->nLTerm = 1; |
| 118076 | 118205 | pNew->aLTerm[0] = pTerm; |
| 118077 | 118206 | /* TUNING: One-time cost for computing the automatic index is |
| 118078 | | - ** approximately 7*N*log2(N) where N is the number of rows in |
| 118079 | | - ** the table being indexed. */ |
| 118080 | | - pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) ); |
| 118207 | + ** estimated to be X*N*log2(N) where N is the number of rows in |
| 118208 | + ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 118209 | + ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 118210 | + ** of X is smaller for views and subqueries so that the query planner |
| 118211 | + ** will be more aggressive about generating automatic indexes for |
| 118212 | + ** those objects, since there is no opportunity to add schema |
| 118213 | + ** indexes on subqueries and views. */ |
| 118214 | + pNew->rSetup = rLogSize + rSize + 4; |
| 118215 | + if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 118216 | + pNew->rSetup += 24; |
| 118217 | + } |
| 118081 | 118218 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
| 118082 | 118219 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 118083 | 118220 | ** is more than the usual guess of 10 rows, since we have no way |
| 118084 | 118221 | ** of knowing how selective the index will ultimately be. It would |
| 118085 | 118222 | ** not be unreasonable to make this value much larger. */ |
| | @@ -118363,11 +118500,10 @@ |
| 118363 | 118500 | WhereLoopBuilder sSubBuild; |
| 118364 | 118501 | WhereOrSet sSum, sCur; |
| 118365 | 118502 | struct SrcList_item *pItem; |
| 118366 | 118503 | |
| 118367 | 118504 | pWC = pBuilder->pWC; |
| 118368 | | - if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 118369 | 118505 | pWCEnd = pWC->a + pWC->nTerm; |
| 118370 | 118506 | pNew = pBuilder->pNew; |
| 118371 | 118507 | memset(&sSum, 0, sizeof(sSum)); |
| 118372 | 118508 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 118373 | 118509 | iCur = pItem->iCursor; |
| | @@ -118384,10 +118520,11 @@ |
| 118384 | 118520 | |
| 118385 | 118521 | sSubBuild = *pBuilder; |
| 118386 | 118522 | sSubBuild.pOrderBy = 0; |
| 118387 | 118523 | sSubBuild.pOrSet = &sCur; |
| 118388 | 118524 | |
| 118525 | + WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
| 118389 | 118526 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 118390 | 118527 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 118391 | 118528 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 118392 | 118529 | }else if( pOrTerm->leftCursor==iCur ){ |
| 118393 | 118530 | tempWC.pWInfo = pWC->pWInfo; |
| | @@ -118398,18 +118535,30 @@ |
| 118398 | 118535 | sSubBuild.pWC = &tempWC; |
| 118399 | 118536 | }else{ |
| 118400 | 118537 | continue; |
| 118401 | 118538 | } |
| 118402 | 118539 | sCur.n = 0; |
| 118540 | +#ifdef WHERETRACE_ENABLED |
| 118541 | + WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 118542 | + (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 118543 | + if( sqlite3WhereTrace & 0x400 ){ |
| 118544 | + for(i=0; i<sSubBuild.pWC->nTerm; i++){ |
| 118545 | + whereTermPrint(&sSubBuild.pWC->a[i], i); |
| 118546 | + } |
| 118547 | + } |
| 118548 | +#endif |
| 118403 | 118549 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 118404 | 118550 | if( IsVirtual(pItem->pTab) ){ |
| 118405 | 118551 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 118406 | 118552 | }else |
| 118407 | 118553 | #endif |
| 118408 | 118554 | { |
| 118409 | 118555 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 118410 | 118556 | } |
| 118557 | + if( rc==SQLITE_OK ){ |
| 118558 | + rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 118559 | + } |
| 118411 | 118560 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 118412 | 118561 | if( sCur.n==0 ){ |
| 118413 | 118562 | sSum.n = 0; |
| 118414 | 118563 | break; |
| 118415 | 118564 | }else if( once ){ |
| | @@ -118450,10 +118599,11 @@ |
| 118450 | 118599 | pNew->rRun = sSum.a[i].rRun + 1; |
| 118451 | 118600 | pNew->nOut = sSum.a[i].nOut; |
| 118452 | 118601 | pNew->prereq = sSum.a[i].prereq; |
| 118453 | 118602 | rc = whereLoopInsert(pBuilder, pNew); |
| 118454 | 118603 | } |
| 118604 | + WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
| 118455 | 118605 | } |
| 118456 | 118606 | } |
| 118457 | 118607 | return rc; |
| 118458 | 118608 | } |
| 118459 | 118609 | |
| | @@ -119516,27 +119666,20 @@ |
| 119516 | 119666 | } |
| 119517 | 119667 | } |
| 119518 | 119668 | |
| 119519 | 119669 | /* Construct the WhereLoop objects */ |
| 119520 | 119670 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
| 119671 | +#if defined(WHERETRACE_ENABLED) |
| 119521 | 119672 | /* Display all terms of the WHERE clause */ |
| 119522 | | -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 119523 | 119673 | if( sqlite3WhereTrace & 0x100 ){ |
| 119524 | 119674 | int i; |
| 119525 | | - Vdbe *v = pParse->pVdbe; |
| 119526 | | - sqlite3ExplainBegin(v); |
| 119527 | 119675 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 119528 | | - sqlite3ExplainPrintf(v, "#%-2d ", i); |
| 119529 | | - sqlite3ExplainPush(v); |
| 119530 | | - whereExplainTerm(v, &sWLB.pWC->a[i]); |
| 119531 | | - sqlite3ExplainPop(v); |
| 119532 | | - sqlite3ExplainNL(v); |
| 119533 | | - } |
| 119534 | | - sqlite3ExplainFinish(v); |
| 119535 | | - sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 119676 | + whereTermPrint(&sWLB.pWC->a[i], i); |
| 119677 | + } |
| 119536 | 119678 | } |
| 119537 | 119679 | #endif |
| 119680 | + |
| 119538 | 119681 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 119539 | 119682 | rc = whereLoopAddAll(&sWLB); |
| 119540 | 119683 | if( rc ) goto whereBeginError; |
| 119541 | 119684 | |
| 119542 | 119685 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
| | @@ -120059,11 +120202,11 @@ |
| 120059 | 120202 | |
| 120060 | 120203 | /* A routine to convert a binary TK_IS or TK_ISNOT expression into a |
| 120061 | 120204 | ** unary TK_ISNULL or TK_NOTNULL expression. */ |
| 120062 | 120205 | static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ |
| 120063 | 120206 | sqlite3 *db = pParse->db; |
| 120064 | | - if( db->mallocFailed==0 && pY->op==TK_NULL ){ |
| 120207 | + if( pY && pA && pY->op==TK_NULL ){ |
| 120065 | 120208 | pA->op = (u8)op; |
| 120066 | 120209 | sqlite3ExprDelete(db, pA->pRight); |
| 120067 | 120210 | pA->pRight = 0; |
| 120068 | 120211 | } |
| 120069 | 120212 | } |
| | @@ -122318,13 +122461,10 @@ |
| 122318 | 122461 | break; |
| 122319 | 122462 | case 111: /* cmd ::= select */ |
| 122320 | 122463 | { |
| 122321 | 122464 | SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; |
| 122322 | 122465 | sqlite3Select(pParse, yymsp[0].minor.yy3, &dest); |
| 122323 | | - sqlite3ExplainBegin(pParse->pVdbe); |
| 122324 | | - sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3); |
| 122325 | | - sqlite3ExplainFinish(pParse->pVdbe); |
| 122326 | 122466 | sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3); |
| 122327 | 122467 | } |
| 122328 | 122468 | break; |
| 122329 | 122469 | case 112: /* select ::= with selectnowith */ |
| 122330 | 122470 | { |
| | @@ -122377,10 +122517,34 @@ |
| 122377 | 122517 | {yygotominor.yy328 = TK_ALL;} |
| 122378 | 122518 | break; |
| 122379 | 122519 | case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ |
| 122380 | 122520 | { |
| 122381 | 122521 | yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset); |
| 122522 | +#if SELECTTRACE_ENABLED |
| 122523 | + /* Populate the Select.zSelName[] string that is used to help with |
| 122524 | + ** query planner debugging, to differentiate between multiple Select |
| 122525 | + ** objects in a complex query. |
| 122526 | + ** |
| 122527 | + ** If the SELECT keyword is immediately followed by a C-style comment |
| 122528 | + ** then extract the first few alphanumeric characters from within that |
| 122529 | + ** comment to be the zSelName value. Otherwise, the label is #N where |
| 122530 | + ** is an integer that is incremented with each SELECT statement seen. |
| 122531 | + */ |
| 122532 | + if( yygotominor.yy3!=0 ){ |
| 122533 | + const char *z = yymsp[-8].minor.yy0.z+6; |
| 122534 | + int i; |
| 122535 | + sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "#%d", |
| 122536 | + ++pParse->nSelect); |
| 122537 | + while( z[0]==' ' ) z++; |
| 122538 | + if( z[0]=='/' && z[1]=='*' ){ |
| 122539 | + z += 2; |
| 122540 | + while( z[0]==' ' ) z++; |
| 122541 | + for(i=0; sqlite3Isalnum(z[i]); i++){} |
| 122542 | + sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "%.*s", i, z); |
| 122543 | + } |
| 122544 | + } |
| 122545 | +#endif /* SELECTRACE_ENABLED */ |
| 122382 | 122546 | } |
| 122383 | 122547 | break; |
| 122384 | 122548 | case 120: /* values ::= VALUES LP nexprlist RP */ |
| 122385 | 122549 | { |
| 122386 | 122550 | yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0); |
| | @@ -123868,10 +124032,11 @@ |
| 123868 | 124032 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 123869 | 124033 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 123870 | 124034 | }; |
| 123871 | 124035 | #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 123872 | 124036 | #endif |
| 124037 | +SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
| 123873 | 124038 | |
| 123874 | 124039 | |
| 123875 | 124040 | /* |
| 123876 | 124041 | ** Return the length of the token that begins at z[0]. |
| 123877 | 124042 | ** Store the token type in *tokenType before returning. |
| | @@ -125138,10 +125303,15 @@ |
| 125138 | 125303 | sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); |
| 125139 | 125304 | sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
| 125140 | 125305 | break; |
| 125141 | 125306 | } |
| 125142 | 125307 | |
| 125308 | + /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames |
| 125309 | + ** can be changed at start-time using the |
| 125310 | + ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125311 | + ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125312 | + */ |
| 125143 | 125313 | case SQLITE_CONFIG_URI: { |
| 125144 | 125314 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125145 | 125315 | break; |
| 125146 | 125316 | } |
| 125147 | 125317 | |
| | @@ -126875,11 +127045,11 @@ |
| 126875 | 127045 | int nUri = sqlite3Strlen30(zUri); |
| 126876 | 127046 | |
| 126877 | 127047 | assert( *pzErrMsg==0 ); |
| 126878 | 127048 | |
| 126879 | 127049 | if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 126880 | | - && nUri>=5 && memcmp(zUri, "file:", 5)==0 |
| 127050 | + && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 126881 | 127051 | ){ |
| 126882 | 127052 | char *zOpt; |
| 126883 | 127053 | int eState; /* Parser state when parsing URI */ |
| 126884 | 127054 | int iIn; /* Input character index */ |
| 126885 | 127055 | int iOut = 0; /* Output character index */ |
| | @@ -127105,11 +127275,13 @@ |
| 127105 | 127275 | assert( SQLITE_OPEN_READWRITE == 0x02 ); |
| 127106 | 127276 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 127107 | 127277 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 127108 | 127278 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 127109 | 127279 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 127110 | | - if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT; |
| 127280 | + if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 127281 | + return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 127282 | + } |
| 127111 | 127283 | |
| 127112 | 127284 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 127113 | 127285 | isThreadsafe = 0; |
| 127114 | 127286 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 127115 | 127287 | isThreadsafe = 0; |
| | @@ -127989,26 +128161,10 @@ |
| 127989 | 128161 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 127990 | 128162 | sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 127991 | 128163 | break; |
| 127992 | 128164 | } |
| 127993 | 128165 | |
| 127994 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 127995 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, |
| 127996 | | - ** sqlite3_stmt*,const char**); |
| 127997 | | - ** |
| 127998 | | - ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds |
| 127999 | | - ** a string that describes the optimized parse tree. This test-control |
| 128000 | | - ** returns a pointer to that string. |
| 128001 | | - */ |
| 128002 | | - case SQLITE_TESTCTRL_EXPLAIN_STMT: { |
| 128003 | | - sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*); |
| 128004 | | - const char **pzRet = va_arg(ap, const char**); |
| 128005 | | - *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt); |
| 128006 | | - break; |
| 128007 | | - } |
| 128008 | | -#endif |
| 128009 | | - |
| 128010 | 128166 | /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); |
| 128011 | 128167 | ** |
| 128012 | 128168 | ** Set or clear a flag that indicates that the database file is always well- |
| 128013 | 128169 | ** formed and never corrupt. This flag is clear by default, indicating that |
| 128014 | 128170 | ** database files might have arbitrary corruption. Setting the flag during |
| 128015 | 128171 | |