Fossil SCM

Update the built-in SQLite to the latest 3.8.7 beta from upstream.

drh 2014-10-05 20:11 trunk
Commit c001fa0edfc412aea5e8ca8d5a45680c0296b92e
3 files changed +9 -9 +913 -757 +16 -18
+9 -9
--- src/shell.c
+++ src/shell.c
@@ -1351,19 +1351,10 @@
13511351
}
13521352
sqlite3_finalize(pExplain);
13531353
sqlite3_free(zEQP);
13541354
}
13551355
1356
- /* Output TESTCTRL_EXPLAIN text of requested */
1357
- if( pArg && pArg->mode==MODE_Explain ){
1358
- const char *zExplain = 0;
1359
- sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1360
- if( zExplain && zExplain[0] ){
1361
- fprintf(pArg->out, "%s", zExplain);
1362
- }
1363
- }
1364
-
13651356
/* If the shell is currently in ".explain" mode, gather the extra
13661357
** data required to add indents to the output.*/
13671358
if( pArg && pArg->mode==MODE_Explain ){
13681359
explain_data_prepare(pArg, pStmt);
13691360
}
@@ -3098,10 +3089,19 @@
30983089
rc = 1;
30993090
}else{
31003091
rc = 0;
31013092
}
31023093
}else
3094
+
3095
+
3096
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
3097
+ if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
3098
+ extern int sqlite3SelectTrace;
3099
+ sqlite3SelectTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
3100
+ }else
3101
+#endif
3102
+
31033103
31043104
#ifdef SQLITE_DEBUG
31053105
/* Undocumented commands for internal testing. Subject to change
31063106
** without notice. */
31073107
if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
31083108
--- src/shell.c
+++ src/shell.c
@@ -1351,19 +1351,10 @@
1351 }
1352 sqlite3_finalize(pExplain);
1353 sqlite3_free(zEQP);
1354 }
1355
1356 /* Output TESTCTRL_EXPLAIN text of requested */
1357 if( pArg && pArg->mode==MODE_Explain ){
1358 const char *zExplain = 0;
1359 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1360 if( zExplain && zExplain[0] ){
1361 fprintf(pArg->out, "%s", zExplain);
1362 }
1363 }
1364
1365 /* If the shell is currently in ".explain" mode, gather the extra
1366 ** data required to add indents to the output.*/
1367 if( pArg && pArg->mode==MODE_Explain ){
1368 explain_data_prepare(pArg, pStmt);
1369 }
@@ -3098,10 +3089,19 @@
3098 rc = 1;
3099 }else{
3100 rc = 0;
3101 }
3102 }else
 
 
 
 
 
 
 
 
 
3103
3104 #ifdef SQLITE_DEBUG
3105 /* Undocumented commands for internal testing. Subject to change
3106 ** without notice. */
3107 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
3108
--- src/shell.c
+++ src/shell.c
@@ -1351,19 +1351,10 @@
1351 }
1352 sqlite3_finalize(pExplain);
1353 sqlite3_free(zEQP);
1354 }
1355
 
 
 
 
 
 
 
 
 
1356 /* If the shell is currently in ".explain" mode, gather the extra
1357 ** data required to add indents to the output.*/
1358 if( pArg && pArg->mode==MODE_Explain ){
1359 explain_data_prepare(pArg, pStmt);
1360 }
@@ -3098,10 +3089,19 @@
3089 rc = 1;
3090 }else{
3091 rc = 0;
3092 }
3093 }else
3094
3095
3096 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
3097 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
3098 extern int sqlite3SelectTrace;
3099 sqlite3SelectTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
3100 }else
3101 #endif
3102
3103
3104 #ifdef SQLITE_DEBUG
3105 /* Undocumented commands for internal testing. Subject to change
3106 ** without notice. */
3107 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
3108
+913 -757
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -231,11 +231,11 @@
231231
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232232
** [sqlite_version()] and [sqlite_source_id()].
233233
*/
234234
#define SQLITE_VERSION "3.8.7"
235235
#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"
237237
238238
/*
239239
** CAPI3REF: Run-Time Library Version Numbers
240240
** KEYWORDS: sqlite3_version, sqlite3_sourceid
241241
**
@@ -2791,13 +2791,13 @@
27912791
** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
27922792
** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
27932793
** an English language description of the error following a failure of any
27942794
** of the sqlite3_open() routines.
27952795
**
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.
27992799
**
28002800
** Whether or not an error occurs when it is opened, resources
28012801
** associated with the [database connection] handle should be released by
28022802
** passing it to [sqlite3_close()] when it is no longer required.
28032803
**
@@ -2881,17 +2881,18 @@
28812881
** ^SQLite uses the path component of the URI as the name of the disk file
28822882
** which contains the database. ^If the path begins with a '/' character,
28832883
** then it is interpreted as an absolute path. ^If the path does not begin
28842884
** with a '/' (meaning that the authority section is omitted from the URI)
28852885
** 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:").)^
28882888
**
28892889
** [[core URI query parameters]]
28902890
** The query component of a URI may contain parameters that are interpreted
28912891
** 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:
28932894
**
28942895
** <ul>
28952896
** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
28962897
** a VFS object that provides the operating system interface that should
28972898
** be used to access the database file on disk. ^If this option is set to
@@ -2922,15 +2923,13 @@
29222923
** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
29232924
** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
29242925
** a URI filename, its value overrides any behavior requested by setting
29252926
** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
29262927
**
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
29292929
** [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.
29322931
**
29332932
** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
29342933
** which if set disables file locking in rollback journal modes. This
29352934
** is useful for accessing a database on a filesystem that does not
29362935
** support locking. Caution: Database corruption might result if two
@@ -3521,15 +3520,14 @@
35213520
** terminated. If any NUL characters occur at byte offsets less than
35223521
** the value of the fourth parameter then the resulting string value will
35233522
** contain embedded NULs. The result of expressions involving strings
35243523
** with embedded NULs is undefined.
35253524
**
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
35283527
** 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.
35313529
** ^If the fifth argument is
35323530
** the special value [SQLITE_STATIC], then SQLite assumes that the
35333531
** information is in static, unmanaged space and does not need to be freed.
35343532
** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
35353533
** SQLite makes its own private copy of the data immediately, before
@@ -3536,11 +3534,11 @@
35363534
** the sqlite3_bind_*() routine returns.
35373535
**
35383536
** ^The sixth argument to sqlite3_bind_text64() must be one of
35393537
** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
35403538
** 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
35423540
** allowed values shown above, or if the text encoding is different
35433541
** from the encoding specified by the sixth parameter, then the behavior
35443542
** is undefined.
35453543
**
35463544
** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4572,11 +4570,11 @@
45724570
**
45734571
** ^The sqlite3_result_null() interface sets the return value
45744572
** of the application-defined function to be NULL.
45754573
**
45764574
** ^The sqlite3_result_text(), sqlite3_result_text16(),
4577
-** sqlite3_result_text16le(), and sqlite3_result_text16be()
4575
+** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
45784576
** set the return value of the application-defined function to be
45794577
** a text string which is represented as UTF-8, UTF-16 native byte order,
45804578
** UTF-16 little endian, or UTF-16 big endian, respectively.
45814579
** ^The sqlite3_result_text64() interface sets the return value of an
45824580
** application-defined function to be a text string in an encoding
@@ -6332,11 +6330,11 @@
63326330
#define SQLITE_TESTCTRL_RESERVE 14
63336331
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
63346332
#define SQLITE_TESTCTRL_ISKEYWORD 16
63356333
#define SQLITE_TESTCTRL_SCRATCHMALLOC 17
63366334
#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6337
-#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6335
+#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
63386336
#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
63396337
#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
63406338
#define SQLITE_TESTCTRL_BYTEORDER 22
63416339
#define SQLITE_TESTCTRL_ISINIT 23
63426340
#define SQLITE_TESTCTRL_SORTER_MMAP 24
@@ -8519,10 +8517,15 @@
85198517
** Macros to compute minimum and maximum of two numbers.
85208518
*/
85218519
#define MIN(A,B) ((A)<(B)?(A):(B))
85228520
#define MAX(A,B) ((A)>(B)?(A):(B))
85238521
8522
+/*
8523
+** Swap two objects of type TYPE.
8524
+*/
8525
+#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
8526
+
85248527
/*
85258528
** Check to see if this machine uses EBCDIC. (Yes, believe it or
85268529
** not, there are still machines out there that use EBCDIC.)
85278530
*/
85288531
#if 'A' == '\301'
@@ -8756,10 +8759,20 @@
87568759
# define SQLITE_ENABLE_STAT3_OR_STAT4 1
87578760
#elif SQLITE_ENABLE_STAT3_OR_STAT4
87588761
# undef SQLITE_ENABLE_STAT3_OR_STAT4
87598762
#endif
87608763
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
+
87618774
/*
87628775
** An instance of the following structure is used to store the busy-handler
87638776
** callback for a given sqlite handle.
87648777
**
87658778
** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8895,10 +8908,11 @@
88958908
typedef struct SrcList SrcList;
88968909
typedef struct StrAccum StrAccum;
88978910
typedef struct Table Table;
88988911
typedef struct TableLock TableLock;
88998912
typedef struct Token Token;
8913
+typedef struct TreeView TreeView;
89008914
typedef struct Trigger Trigger;
89018915
typedef struct TriggerPrg TriggerPrg;
89028916
typedef struct TriggerStep TriggerStep;
89038917
typedef struct UnpackedRecord UnpackedRecord;
89048918
typedef struct VTable VTable;
@@ -11738,11 +11752,11 @@
1173811752
#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
1173911753
#define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
1174011754
#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
1174111755
#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
1174211756
#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 */
1174411758
#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
1174511759
#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
1174611760
#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
1174711761
#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
1174811762
#define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */
@@ -11823,10 +11837,13 @@
1182311837
struct Select {
1182411838
ExprList *pEList; /* The fields of the result */
1182511839
u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
1182611840
u16 selFlags; /* Various SF_* values */
1182711841
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
1182811845
int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
1182911846
u64 nSelectRow; /* Estimated number of result rows */
1183011847
SrcList *pSrc; /* The FROM clause */
1183111848
Expr *pWhere; /* The WHERE clause */
1183211849
ExprList *pGroupBy; /* The GROUP BY clause */
@@ -12081,10 +12098,14 @@
1208112098
yDbMask cookieMask; /* Bitmask of schema verified databases */
1208212099
int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
1208312100
int regRowid; /* Register holding rowid of CREATE TABLE entry */
1208412101
int regRoot; /* Register holding root page number for new objects */
1208512102
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
1208612107
#ifndef SQLITE_OMIT_SHARED_CACHE
1208712108
int nTableLock; /* Number of locks in aTableLock */
1208812109
TableLock *aTableLock; /* Required table locks for shared-cache mode */
1208912110
#endif
1209012111
AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
@@ -12160,10 +12181,11 @@
1216012181
1216112182
/*
1216212183
** Bitfield flags for P5 value in various opcodes.
1216312184
*/
1216412185
#define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
12186
+#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
1216512187
#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
1216612188
#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
1216712189
#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
1216812190
#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
1216912191
#define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */
@@ -12428,10 +12450,21 @@
1242812450
Select *pSelect; /* The definition of this CTE */
1242912451
const char *zErr; /* Error message for circular references */
1243012452
} a[1];
1243112453
};
1243212454
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
+
1243312466
/*
1243412467
** Assuming zIn points to the first byte of a UTF-8 character,
1243512468
** advance zIn to point to the first byte of the next UTF-8 character.
1243612469
*/
1243712470
#define SQLITE_SKIP_UTF8(zIn) { \
@@ -12493,10 +12526,11 @@
1249312526
# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
1249412527
# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
1249512528
# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
1249612529
# define sqlite3Tolower(x) tolower((unsigned char)(x))
1249712530
#endif
12531
+SQLITE_PRIVATE int sqlite3IsIdChar(u8);
1249812532
1249912533
/*
1250012534
** Internal function prototypes
1250112535
*/
1250212536
#define sqlite3StrICmp sqlite3_stricmp
@@ -12591,29 +12625,18 @@
1259112625
#endif
1259212626
#if defined(SQLITE_TEST)
1259312627
SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
1259412628
#endif
1259512629
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);
1261512638
#endif
1261612639
1261712640
1261812641
SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
1261912642
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
@@ -12791,11 +12814,11 @@
1279112814
SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
1279212815
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
1279312816
SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
1279412817
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
1279512818
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12796
-SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
12819
+SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
1279712820
SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
1279812821
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
1279912822
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
1280012823
SQLITE_PRIVATE int sqlite3IsRowid(const char*);
1280112824
SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -12815,10 +12838,15 @@
1281512838
SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
1281612839
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
1281712840
SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
1281812841
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
1281912842
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
1282012848
SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
1282112849
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
1282212850
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
1282312851
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
1282412852
SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
@@ -13439,10 +13467,17 @@
1343913467
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
1344013468
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
1344113469
};
1344213470
#endif
1344313471
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
+*/
1344413479
#ifndef SQLITE_USE_URI
1344513480
# define SQLITE_USE_URI 0
1344613481
#endif
1344713482
1344813483
#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
@@ -13945,11 +13980,11 @@
1394513980
1394613981
/* Since ArraySize(azCompileOpt) is normally in single digits, a
1394713982
** linear search is adequate. No need for a binary search. */
1394813983
for(i=0; i<ArraySize(azCompileOpt); i++){
1394913984
if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13950
- && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13985
+ && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
1395113986
){
1395213987
return 1;
1395313988
}
1395413989
}
1395513990
return 0;
@@ -14259,11 +14294,10 @@
1425914294
*/
1426014295
struct sqlite3_context {
1426114296
Mem *pOut; /* The return value is stored here */
1426214297
FuncDef *pFunc; /* Pointer to function information */
1426314298
Mem *pMem; /* Memory cell used to store aggregate context */
14264
- CollSeq *pColl; /* Collating sequence */
1426514299
Vdbe *pVdbe; /* The VM that owns this context */
1426614300
int iOp; /* Instruction number of OP_Function */
1426714301
int isError; /* Error code returned by the function. */
1426814302
u8 skipFlag; /* Skip accumulator loading if true */
1426914303
u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
@@ -14348,14 +14382,10 @@
1434814382
i64 nFkConstraint; /* Number of imm. FK constraints this VM */
1434914383
i64 nStmtDefCons; /* Number of def. constraints when stmt started */
1435014384
i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
1435114385
char *zSql; /* Text of the SQL statement that generated this */
1435214386
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
1435714387
VdbeFrame *pFrame; /* Parent frame */
1435814388
VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
1435914389
int nFrame; /* Number of frames in pFrame list */
1436014390
u32 expmask; /* Binding to these vars invalidates VM */
1436114391
SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
@@ -14675,11 +14705,11 @@
1467514705
sqlite3VdbeClearObject(db, pVdbe);
1467614706
sqlite3DbFree(db, pVdbe);
1467714707
}
1467814708
db->pnBytesFreed = 0;
1467914709
14680
- *pHighwater = 0;
14710
+ *pHighwater = 0; /* IMP: R-64479-57858 */
1468114711
*pCurrent = nByte;
1468214712
1468314713
break;
1468414714
}
1468514715
@@ -14700,21 +14730,23 @@
1470014730
if( db->aDb[i].pBt ){
1470114731
Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
1470214732
sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
1470314733
}
1470414734
}
14705
- *pHighwater = 0;
14735
+ *pHighwater = 0; /* IMP: R-42420-56072 */
14736
+ /* IMP: R-54100-20147 */
14737
+ /* IMP: R-29431-39229 */
1470614738
*pCurrent = nRet;
1470714739
break;
1470814740
}
1470914741
1471014742
/* Set *pCurrent to non-zero if there are unresolved deferred foreign
1471114743
** key constraints. Set *pCurrent to zero if all foreign key constraints
1471214744
** have been satisfied. The *pHighwater is always set to zero.
1471314745
*/
1471414746
case SQLITE_DBSTATUS_DEFERRED_FKS: {
14715
- *pHighwater = 0;
14747
+ *pHighwater = 0; /* IMP: R-11967-56545 */
1471614748
*pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
1471714749
break;
1471814750
}
1471914751
1472014752
default: {
@@ -20195,11 +20227,11 @@
2019520227
mallocWithAlarm((int)n, &p);
2019620228
sqlite3_mutex_leave(mem0.mutex);
2019720229
}else{
2019820230
p = sqlite3GlobalConfig.m.xMalloc((int)n);
2019920231
}
20200
- assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
20232
+ assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
2020120233
return p;
2020220234
}
2020320235
2020420236
/*
2020520237
** This version of the memory allocation is for use by the application.
@@ -20418,14 +20450,14 @@
2041820450
*/
2041920451
SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
2042020452
int nOld, nNew, nDiff;
2042120453
void *pNew;
2042220454
if( pOld==0 ){
20423
- return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
20455
+ return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
2042420456
}
2042520457
if( nBytes==0 ){
20426
- sqlite3_free(pOld); /* IMP: R-31593-10574 */
20458
+ sqlite3_free(pOld); /* IMP: R-26507-47431 */
2042720459
return 0;
2042820460
}
2042920461
if( nBytes>=0x7fffff00 ){
2043020462
/* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
2043120463
return 0;
@@ -20458,11 +20490,11 @@
2045820490
}
2045920491
sqlite3_mutex_leave(mem0.mutex);
2046020492
}else{
2046120493
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
2046220494
}
20463
- assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
20495
+ assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
2046420496
return pNew;
2046520497
}
2046620498
2046720499
/*
2046820500
** The public interface to sqlite3Realloc. Make sure that the memory
@@ -20470,11 +20502,11 @@
2047020502
*/
2047120503
SQLITE_API void *sqlite3_realloc(void *pOld, int n){
2047220504
#ifndef SQLITE_OMIT_AUTOINIT
2047320505
if( sqlite3_initialize() ) return 0;
2047420506
#endif
20475
- if( n<0 ) n = 0;
20507
+ if( n<0 ) n = 0; /* IMP: R-26507-47431 */
2047620508
return sqlite3Realloc(pOld, n);
2047720509
}
2047820510
SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
2047920511
#ifndef SQLITE_OMIT_AUTOINIT
2048020512
if( sqlite3_initialize() ) return 0;
@@ -21757,10 +21789,73 @@
2175721789
fprintf(stdout,"%s", zBuf);
2175821790
fflush(stdout);
2175921791
}
2176021792
#endif
2176121793
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
+
2176221857
/*
2176321858
** variable-argument wrapper around sqlite3VXPrintf().
2176421859
*/
2176521860
SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
2176621861
va_list ap;
@@ -29659,11 +29754,11 @@
2965929754
** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
2966029755
**
2966129756
** * An I/O method finder function called FINDER that returns a pointer
2966229757
** to the METHOD object in the previous bullet.
2966329758
*/
29664
-#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
29759
+#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \
2966529760
static const sqlite3_io_methods METHOD = { \
2966629761
VERSION, /* iVersion */ \
2966729762
CLOSE, /* xClose */ \
2966829763
unixRead, /* xRead */ \
2966929764
unixWrite, /* xWrite */ \
@@ -29674,11 +29769,11 @@
2967429769
UNLOCK, /* xUnlock */ \
2967529770
CKLOCK, /* xCheckReservedLock */ \
2967629771
unixFileControl, /* xFileControl */ \
2967729772
unixSectorSize, /* xSectorSize */ \
2967829773
unixDeviceCharacteristics, /* xDeviceCapabilities */ \
29679
- unixShmMap, /* xShmMap */ \
29774
+ SHMMAP, /* xShmMap */ \
2968029775
unixShmLock, /* xShmLock */ \
2968129776
unixShmBarrier, /* xShmBarrier */ \
2968229777
unixShmUnmap, /* xShmUnmap */ \
2968329778
unixFetch, /* xFetch */ \
2968429779
unixUnfetch, /* xUnfetch */ \
@@ -29700,29 +29795,32 @@
2970029795
posixIoMethods, /* sqlite3_io_methods object name */
2970129796
3, /* shared memory and mmap are enabled */
2970229797
unixClose, /* xClose method */
2970329798
unixLock, /* xLock method */
2970429799
unixUnlock, /* xUnlock method */
29705
- unixCheckReservedLock /* xCheckReservedLock method */
29800
+ unixCheckReservedLock, /* xCheckReservedLock method */
29801
+ unixShmMap /* xShmMap method */
2970629802
)
2970729803
IOMETHODS(
2970829804
nolockIoFinder, /* Finder function name */
2970929805
nolockIoMethods, /* sqlite3_io_methods object name */
2971029806
3, /* shared memory is disabled */
2971129807
nolockClose, /* xClose method */
2971229808
nolockLock, /* xLock method */
2971329809
nolockUnlock, /* xUnlock method */
29714
- nolockCheckReservedLock /* xCheckReservedLock method */
29810
+ nolockCheckReservedLock, /* xCheckReservedLock method */
29811
+ 0 /* xShmMap method */
2971529812
)
2971629813
IOMETHODS(
2971729814
dotlockIoFinder, /* Finder function name */
2971829815
dotlockIoMethods, /* sqlite3_io_methods object name */
2971929816
1, /* shared memory is disabled */
2972029817
dotlockClose, /* xClose method */
2972129818
dotlockLock, /* xLock method */
2972229819
dotlockUnlock, /* xUnlock method */
29723
- dotlockCheckReservedLock /* xCheckReservedLock method */
29820
+ dotlockCheckReservedLock, /* xCheckReservedLock method */
29821
+ 0 /* xShmMap method */
2972429822
)
2972529823
2972629824
#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
2972729825
IOMETHODS(
2972829826
flockIoFinder, /* Finder function name */
@@ -29729,11 +29827,12 @@
2972929827
flockIoMethods, /* sqlite3_io_methods object name */
2973029828
1, /* shared memory is disabled */
2973129829
flockClose, /* xClose method */
2973229830
flockLock, /* xLock method */
2973329831
flockUnlock, /* xUnlock method */
29734
- flockCheckReservedLock /* xCheckReservedLock method */
29832
+ flockCheckReservedLock, /* xCheckReservedLock method */
29833
+ 0 /* xShmMap method */
2973529834
)
2973629835
#endif
2973729836
2973829837
#if OS_VXWORKS
2973929838
IOMETHODS(
@@ -29741,11 +29840,12 @@
2974129840
semIoMethods, /* sqlite3_io_methods object name */
2974229841
1, /* shared memory is disabled */
2974329842
semClose, /* xClose method */
2974429843
semLock, /* xLock method */
2974529844
semUnlock, /* xUnlock method */
29746
- semCheckReservedLock /* xCheckReservedLock method */
29845
+ semCheckReservedLock, /* xCheckReservedLock method */
29846
+ 0 /* xShmMap method */
2974729847
)
2974829848
#endif
2974929849
2975029850
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2975129851
IOMETHODS(
@@ -29753,11 +29853,12 @@
2975329853
afpIoMethods, /* sqlite3_io_methods object name */
2975429854
1, /* shared memory is disabled */
2975529855
afpClose, /* xClose method */
2975629856
afpLock, /* xLock method */
2975729857
afpUnlock, /* xUnlock method */
29758
- afpCheckReservedLock /* xCheckReservedLock method */
29858
+ afpCheckReservedLock, /* xCheckReservedLock method */
29859
+ 0 /* xShmMap method */
2975929860
)
2976029861
#endif
2976129862
2976229863
/*
2976329864
** The proxy locking method is a "super-method" in the sense that it
@@ -29778,11 +29879,12 @@
2977829879
proxyIoMethods, /* sqlite3_io_methods object name */
2977929880
1, /* shared memory is disabled */
2978029881
proxyClose, /* xClose method */
2978129882
proxyLock, /* xLock method */
2978229883
proxyUnlock, /* xUnlock method */
29783
- proxyCheckReservedLock /* xCheckReservedLock method */
29884
+ proxyCheckReservedLock, /* xCheckReservedLock method */
29885
+ 0 /* xShmMap method */
2978429886
)
2978529887
#endif
2978629888
2978729889
/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
2978829890
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29791,11 +29893,12 @@
2979129893
nfsIoMethods, /* sqlite3_io_methods object name */
2979229894
1, /* shared memory is disabled */
2979329895
unixClose, /* xClose method */
2979429896
unixLock, /* xLock method */
2979529897
nfsUnlock, /* xUnlock method */
29796
- unixCheckReservedLock /* xCheckReservedLock method */
29898
+ unixCheckReservedLock, /* xCheckReservedLock method */
29899
+ 0 /* xShmMap method */
2979729900
)
2979829901
#endif
2979929902
2980029903
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2980129904
/*
@@ -51383,13 +51486,14 @@
5138351486
** stored in MemPage.pBt->mutex.
5138451487
*/
5138551488
struct MemPage {
5138651489
u8 isInit; /* True if previously initialized. MUST BE FIRST! */
5138751490
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 */
5139151495
u8 hdrOffset; /* 100 for page 1. 0 otherwise */
5139251496
u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
5139351497
u8 max1bytePayload; /* min(maxLocal,127) */
5139451498
u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
5139551499
u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
@@ -51566,16 +51670,14 @@
5156651670
** about a cell. The parseCellPtr() function fills in this structure
5156751671
** based on information extract from the raw disk page.
5156851672
*/
5156951673
typedef struct CellInfo CellInfo;
5157051674
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 */
5157751679
u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
5157851680
u16 nSize; /* Size of the cell content on the main b-tree page */
5157951681
};
5158051682
5158151683
/*
@@ -51768,10 +51870,12 @@
5176851870
u8 *aPgRef; /* 1 bit per page in the db (see above) */
5176951871
Pgno nPage; /* Number of pages in the database */
5177051872
int mxErr; /* Stop accumulating errors when this reaches zero */
5177151873
int nErr; /* Number of messages written to zErrMsg so far */
5177251874
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 */
5177351877
StrAccum errMsg; /* Accumulate the error message text here */
5177451878
};
5177551879
5177651880
/*
5177751881
** Routines to read or write a two- and four-byte big-endian integer values.
@@ -52554,11 +52658,13 @@
5255452658
){
5255552659
BtCursor *p;
5255652660
BtShared *pBt = pBtree->pBt;
5255752661
assert( sqlite3BtreeHoldsMutex(pBtree) );
5255852662
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
+ ){
5256052666
p->eState = CURSOR_INVALID;
5256152667
}
5256252668
}
5256352669
}
5256452670
@@ -52727,13 +52833,13 @@
5272752833
** the cursors if and when a cursor is found that actually requires saving.
5272852834
** The common case is that no cursors need to be saved, so this routine is
5272952835
** broken out from its caller to avoid unnecessary stack pointer movement.
5273052836
*/
5273152837
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 */
5273552841
){
5273652842
do{
5273752843
if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
5273852844
if( p->eState==CURSOR_VALID ){
5273952845
int rc = saveCursorPosition(p);
@@ -53035,51 +53141,48 @@
5303553141
/*
5303653142
** Parse a cell content block and fill in the CellInfo structure. There
5303753143
** are two versions of this function. btreeParseCell() takes a
5303853144
** cell index as the second argument and btreeParseCellPtr()
5303953145
** 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.
5304353146
*/
5304453147
static void btreeParseCellPtr(
5304553148
MemPage *pPage, /* Page containing the cell */
5304653149
u8 *pCell, /* Pointer to the cell text. */
5304753150
CellInfo *pInfo /* Fill in this structure */
5304853151
){
53049
- u16 n; /* Number bytes in cell content header */
53152
+ u8 *pIter; /* For scanning through pCell */
5305053153
u32 nPayload; /* Number of bytes of cell payload */
5305153154
5305253155
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53053
-
53054
- pInfo->pCell = pCell;
5305553156
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);
5307053172
pInfo->nKey = nPayload;
5307153173
}
5307253174
pInfo->nPayload = nPayload;
53073
- pInfo->nHeader = n;
53175
+ pInfo->pPayload = pIter;
5307453176
testcase( nPayload==pPage->maxLocal );
5307553177
testcase( nPayload==pPage->maxLocal+1 );
53076
- if( likely(nPayload<=pPage->maxLocal) ){
53178
+ if( nPayload<=pPage->maxLocal ){
5307753179
/* This is the (easy) common case where the entire payload fits
5307853180
** on the local page. No overflow is required.
5307953181
*/
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;
5308153184
pInfo->nLocal = (u16)nPayload;
5308253185
pInfo->iOverflow = 0;
5308353186
}else{
5308453187
/* If the payload will not fit completely on the local page, we have
5308553188
** to decide how much to store locally and how much to spill onto
@@ -53102,33 +53205,32 @@
5310253205
if( surplus <= maxLocal ){
5310353206
pInfo->nLocal = (u16)surplus;
5310453207
}else{
5310553208
pInfo->nLocal = (u16)minLocal;
5310653209
}
53107
- pInfo->iOverflow = (u16)(pInfo->nLocal + n);
53210
+ pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
5310853211
pInfo->nSize = pInfo->iOverflow + 4;
5310953212
}
5311053213
}
53111
-#define parseCell(pPage, iCell, pInfo) \
53112
- btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
5311353214
static void btreeParseCell(
5311453215
MemPage *pPage, /* Page containing the cell */
5311553216
int iCell, /* The cell index. First cell is 0 */
5311653217
CellInfo *pInfo /* Fill in this structure */
5311753218
){
53118
- parseCell(pPage, iCell, pInfo);
53219
+ btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
5311953220
}
5312053221
5312153222
/*
5312253223
** Compute the total number of bytes that a Cell needs in the cell
5312353224
** data area of the btree-page. The return number includes the cell
5312453225
** data header and the local payload, but not any overflow page or
5312553226
** the space used by the cell pointer.
5312653227
*/
5312753228
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 */
5313053232
5313153233
#ifdef SQLITE_DEBUG
5313253234
/* The value returned by this function should always be the same as
5313353235
** the (CellInfo.nSize) value found by doing a full parse of the
5313453236
** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
@@ -53135,47 +53237,48 @@
5313553237
** this function verifies that this invariant is not violated. */
5313653238
CellInfo debuginfo;
5313753239
btreeParseCellPtr(pPage, pCell, &debuginfo);
5313853240
#endif
5313953241
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++;
5314053257
if( pPage->intKey ){
53141
- u8 *pEnd;
53142
- if( pPage->hasData ){
53143
- pIter += getVarint32(pIter, nSize);
53144
- }else{
53145
- nSize = 0;
53146
- }
53147
-
5314853258
/* pIter now points at the 64-bit integer key value, a variable length
5314953259
** integer. The following block moves pIter to point at the first byte
5315053260
** past the end of the key value. */
5315153261
pEnd = &pIter[9];
5315253262
while( (*pIter++)&0x80 && pIter<pEnd );
53153
- }else{
53154
- pIter += getVarint32(pIter, nSize);
5315553263
}
53156
-
5315753264
testcase( nSize==pPage->maxLocal );
5315853265
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{
5316053270
int minLocal = pPage->minLocal;
5316153271
nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
5316253272
testcase( nSize==pPage->maxLocal );
5316353273
testcase( nSize==pPage->maxLocal+1 );
5316453274
if( nSize>pPage->maxLocal ){
5316553275
nSize = minLocal;
5316653276
}
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 );
5317753280
return (u16)nSize;
5317853281
}
5317953282
5318053283
#ifdef SQLITE_DEBUG
5318153284
/* This variation on cellSizePtr() is used inside of assert() statements
@@ -53194,11 +53297,10 @@
5319453297
static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
5319553298
CellInfo info;
5319653299
if( *pRC ) return;
5319753300
assert( pCell!=0 );
5319853301
btreeParseCellPtr(pPage, pCell, &info);
53199
- assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
5320053302
if( info.iOverflow ){
5320153303
Pgno ovfl = get4byte(&pCell[info.iOverflow]);
5320253304
ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
5320353305
}
5320453306
}
@@ -53407,11 +53509,11 @@
5340753509
** does it detect cells or freeblocks that encrouch into the reserved bytes
5340853510
** at the end of the page. So do additional corruption checks inside this
5340953511
** routine and return SQLITE_CORRUPT if any problems are found.
5341053512
*/
5341153513
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 */
5341353515
u16 iFreeBlk; /* Address of the next freeblock */
5341453516
u8 hdr; /* Page header size. 0 or 100 */
5341553517
u8 nFrag = 0; /* Reduction in fragmentation */
5341653518
u16 iOrigSize = iSize; /* Original value of iSize */
5341753519
u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
@@ -53459,13 +53561,13 @@
5345953561
iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
5346053562
iSize = iEnd - iStart;
5346153563
iFreeBlk = get2byte(&data[iFreeBlk]);
5346253564
}
5346353565
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.
5346753569
*/
5346853570
if( iPtr>hdr+1 ){
5346953571
int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
5347053572
if( iPtrEnd+3>=iStart ){
5347153573
if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
@@ -53515,16 +53617,18 @@
5351553617
flagByte &= ~PTF_LEAF;
5351653618
pPage->childPtrSize = 4-4*pPage->leaf;
5351753619
pBt = pPage->pBt;
5351853620
if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
5351953621
pPage->intKey = 1;
53520
- pPage->hasData = pPage->leaf;
53622
+ pPage->intKeyLeaf = pPage->leaf;
53623
+ pPage->noPayload = !pPage->leaf;
5352153624
pPage->maxLocal = pBt->maxLeaf;
5352253625
pPage->minLocal = pBt->minLeaf;
5352353626
}else if( flagByte==PTF_ZERODATA ){
5352453627
pPage->intKey = 0;
53525
- pPage->hasData = 0;
53628
+ pPage->intKeyLeaf = 0;
53629
+ pPage->noPayload = 0;
5352653630
pPage->maxLocal = pBt->maxLocal;
5352753631
pPage->minLocal = pBt->minLocal;
5352853632
}else{
5352953633
return SQLITE_CORRUPT_BKPT;
5353053634
}
@@ -54694,15 +54798,15 @@
5469454798
*/
5469554799
static void unlockBtreeIfUnused(BtShared *pBt){
5469654800
assert( sqlite3_mutex_held(pBt->mutex) );
5469754801
assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
5469854802
if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
54699
- assert( pBt->pPage1->aData );
54803
+ MemPage *pPage1 = pBt->pPage1;
54804
+ assert( pPage1->aData );
5470054805
assert( sqlite3PagerRefcount(pBt->pPager)==1 );
54701
- assert( pBt->pPage1->aData );
54702
- releasePage(pBt->pPage1);
5470354806
pBt->pPage1 = 0;
54807
+ releasePage(pPage1);
5470454808
}
5470554809
}
5470654810
5470754811
/*
5470854812
** If pBt points to an empty file then convert that empty file
@@ -55739,10 +55843,14 @@
5573955843
assert( pBt->pPage1 && pBt->pPage1->aData );
5574055844
5574155845
if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
5574255846
return SQLITE_READONLY;
5574355847
}
55848
+ if( wrFlag ){
55849
+ allocateTempSpace(pBt);
55850
+ if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
55851
+ }
5574455852
if( iTable==1 && btreePagecount(pBt)==0 ){
5574555853
assert( wrFlag==0 );
5574655854
iTable = 0;
5574755855
}
5574855856
@@ -55928,12 +56036,13 @@
5592856036
** to return an integer result code for historical reasons.
5592956037
*/
5593056038
SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
5593156039
assert( cursorHoldsMutex(pCur) );
5593256040
assert( pCur->eState==CURSOR_VALID );
56041
+ assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
5593356042
getCellInfo(pCur);
55934
- *pSize = pCur->info.nData;
56043
+ *pSize = pCur->info.nPayload;
5593556044
return SQLITE_OK;
5593656045
}
5593756046
5593856047
/*
5593956048
** Given the page number of an overflow page in the database (parameter
@@ -56080,34 +56189,32 @@
5608056189
unsigned char *pBuf, /* Write the bytes into this buffer */
5608156190
int eOp /* zero to read. non-zero to write. */
5608256191
){
5608356192
unsigned char *aPayload;
5608456193
int rc = SQLITE_OK;
56085
- u32 nKey;
5608656194
int iIdx = 0;
5608756195
MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
5608856196
BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
5608956197
#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 */
5609156200
#endif
5609256201
5609356202
assert( pPage );
5609456203
assert( pCur->eState==CURSOR_VALID );
5609556204
assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
5609656205
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 */
5609856207
5609956208
getCellInfo(pCur);
56100
- aPayload = pCur->info.pCell + pCur->info.nHeader;
56101
- nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
56209
+ aPayload = pCur->info.pPayload;
5610256210
#ifdef SQLITE_DIRECT_OVERFLOW_READ
56103
- bEnd = (offset+amt==nKey+pCur->info.nData);
56211
+ bEnd = offset+amt==pCur->info.nPayload;
5610456212
#endif
56213
+ assert( offset+amt <= pCur->info.nPayload );
5610556214
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] ){
5610956216
/* Trying to read or write past the end of the data is an error */
5611056217
return SQLITE_CORRUPT_BKPT;
5611156218
}
5611256219
5611356220
/* Check if data must be read/written to/from the btree page itself. */
@@ -56159,11 +56266,13 @@
5615956266
5616056267
/* If the overflow page-list cache has been allocated and the
5616156268
** entry for the first required overflow page is valid, skip
5616256269
** directly to it.
5616356270
*/
56164
- if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
56271
+ if( (pCur->curFlags & BTCF_ValidOvfl)!=0
56272
+ && pCur->aOverflow[offset/ovflSize]
56273
+ ){
5616556274
iIdx = (offset/ovflSize);
5616656275
nextPage = pCur->aOverflow[iIdx];
5616756276
offset = (offset%ovflSize);
5616856277
}
5616956278
@@ -56212,10 +56321,11 @@
5621256321
** 2) data is required from the start of this overflow page, and
5621356322
** 3) the database is file-backed, and
5621456323
** 4) there is no open write-transaction, and
5621556324
** 5) the database is not a WAL database,
5621656325
** 6) all data from the page is being read.
56326
+ ** 7) at least 4 bytes have already been read into the output buffer
5621756327
**
5621856328
** then data can be read directly from the database file into the
5621956329
** output buffer, bypassing the page-cache altogether. This speeds
5622056330
** up loading large records that span many overflow pages.
5622156331
*/
@@ -56223,13 +56333,15 @@
5622356333
&& offset==0 /* (2) */
5622456334
&& (bEnd || a==ovflSize) /* (6) */
5622556335
&& pBt->inTransaction==TRANS_READ /* (4) */
5622656336
&& (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
5622756337
&& pBt->pPage1->aData[19]==0x01 /* (5) */
56338
+ && &pBuf[-4]>=pBufStart /* (7) */
5622856339
){
5622956340
u8 aSave[4];
5623056341
u8 *aWrite = &pBuf[-4];
56342
+ assert( aWrite>=pBufStart ); /* hence (7) */
5623156343
memcpy(aSave, aWrite, 4);
5623256344
rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
5623356345
nextPage = get4byte(aWrite);
5623456346
memcpy(aWrite, aSave, 4);
5623556347
}else
@@ -56337,11 +56449,11 @@
5633756449
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5633856450
assert( cursorHoldsMutex(pCur) );
5633956451
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
5634056452
assert( pCur->info.nSize>0 );
5634156453
*pAmt = pCur->info.nLocal;
56342
- return (void*)(pCur->info.pCell + pCur->info.nHeader);
56454
+ return (void*)pCur->info.pPayload;
5634356455
}
5634456456
5634556457
5634656458
/*
5634756459
** For the entry that cursor pCur is point to, return as
@@ -56765,11 +56877,11 @@
5676556877
pCur->aiIdx[pCur->iPage] = (u16)idx;
5676656878
if( xRecordCompare==0 ){
5676756879
for(;;){
5676856880
i64 nCellKey;
5676956881
pCell = findCell(pPage, idx) + pPage->childPtrSize;
56770
- if( pPage->hasData ){
56882
+ if( pPage->intKeyLeaf ){
5677156883
while( 0x80 <= *(pCell++) ){
5677256884
if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
5677356885
}
5677456886
}
5677556887
getVarint(pCell, (u64*)&nCellKey);
@@ -57024,13 +57136,13 @@
5702457136
** was already pointing to the first entry in the database before
5702557137
** this routine was called, then set *pRes=1.
5702657138
**
5702757139
** The main entry point is sqlite3BtreePrevious(). That routine is optimized
5702857140
** 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.
5703257144
**
5703357145
** The calling function will set *pRes to 0 or 1. The initial *pRes value
5703457146
** will be 1 if the cursor being stepped corresponds to an SQL index and
5703557147
** if this routine could have been skipped if that SQL index had been
5703657148
** a unique index. Otherwise the caller will have set *pRes to zero.
@@ -57048,12 +57160,11 @@
5704857160
assert( *pRes==0 );
5704957161
assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5705057162
assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
5705157163
assert( pCur->info.nSize==0 );
5705257164
if( pCur->eState!=CURSOR_VALID ){
57053
- assert( pCur->eState>=CURSOR_REQUIRESEEK );
57054
- rc = btreeRestoreCursorPosition(pCur);
57165
+ rc = restoreCursorPosition(pCur);
5705557166
if( rc!=SQLITE_OK ){
5705657167
return rc;
5705757168
}
5705857169
if( CURSOR_INVALID==pCur->eState ){
5705957170
*pRes = 1;
@@ -57354,11 +57465,11 @@
5735457465
if( rc ) goto end_allocate_page;
5735557466
if( closest<k-1 ){
5735657467
memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5735757468
}
5735857469
put4byte(&aData[4], k-1);
57359
- noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
57470
+ noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
5736057471
rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
5736157472
if( rc==SQLITE_OK ){
5736257473
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5736357474
if( rc!=SQLITE_OK ){
5736457475
releasePage(*ppPage);
@@ -57387,11 +57498,11 @@
5738757498
** content for any page that really does lie past the end of the database
5738857499
** file on disk. So the effects of disabling the no-content optimization
5738957500
** here are confined to those pages that lie between the end of the
5739057501
** database image and the end of the database file.
5739157502
*/
57392
- int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
57503
+ int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
5739357504
5739457505
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
5739557506
if( rc ) return rc;
5739657507
pBt->nPage++;
5739757508
if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -57586,22 +57697,29 @@
5758657697
*pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
5758757698
}
5758857699
}
5758957700
5759057701
/*
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.
5759257705
*/
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
+){
5759457711
BtShared *pBt = pPage->pBt;
5759557712
CellInfo info;
5759657713
Pgno ovflPgno;
5759757714
int rc;
5759857715
int nOvfl;
5759957716
u32 ovflPageSize;
5760057717
5760157718
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5760257719
btreeParseCellPtr(pPage, pCell, &info);
57720
+ *pnSize = info.nSize;
5760357721
if( info.iOverflow==0 ){
5760457722
return SQLITE_OK; /* No overflow pages. Return without doing anything */
5760557723
}
5760657724
if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
5760757725
return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
@@ -57681,54 +57799,87 @@
5768157799
unsigned char *pPrior;
5768257800
unsigned char *pPayload;
5768357801
BtShared *pBt = pPage->pBt;
5768457802
Pgno pgnoOvfl = 0;
5768557803
int nHeader;
57686
- CellInfo info;
5768757804
5768857805
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5768957806
5769057807
/* pPage is not necessarily writeable since pCell might be auxiliary
5769157808
** buffer space that is separate from the pPage buffer area */
5769257809
assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
5769357810
|| sqlite3PagerIswriteable(pPage->pDbPage) );
5769457811
5769557812
/* 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);
5770257817
}else{
57703
- nData = nZero = 0;
57818
+ assert( nData==0 );
57819
+ assert( nZero==0 );
5770457820
}
5770557821
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) );
5771057822
57711
- /* Fill in the payload */
57712
- nPayload = nData + nZero;
57823
+ /* Fill in the payload size */
5771357824
if( pPage->intKey ){
5771457825
pSrc = pData;
5771557826
nSrc = nData;
5771657827
nData = 0;
5771757828
}else{
5771857829
if( NEVER(nKey>0x7fffffff || pKey==0) ){
5771957830
return SQLITE_CORRUPT_BKPT;
5772057831
}
57721
- nPayload += (int)nKey;
57832
+ nPayload = (int)nKey;
5772257833
pSrc = pKey;
5772357834
nSrc = (int)nKey;
5772457835
}
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
+ }
5772757854
pPayload = &pCell[nHeader];
57728
- pPrior = &pCell[info.iOverflow];
5772957855
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 */
5773057881
while( nPayload>0 ){
5773157882
if( spaceLeft==0 ){
5773257883
#ifndef SQLITE_OMIT_AUTOVACUUM
5773357884
Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
5773457885
if( pBt->autoVacuum ){
@@ -58432,11 +58583,11 @@
5843258583
**
5843358584
** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5843458585
** leafData: 1 if pPage holds key+data and pParent holds only keys.
5843558586
*/
5843658587
leafCorrection = apOld[0]->leaf*4;
58437
- leafData = apOld[0]->hasData;
58588
+ leafData = apOld[0]->intKeyLeaf;
5843858589
for(i=0; i<nOld; i++){
5843958590
int limit;
5844058591
5844158592
/* Before doing anything else, take a copy of the i'th original sibling
5844258593
** The rest of this function will use data from the copies rather
@@ -59008,11 +59159,11 @@
5900859159
int const iIdx = pCur->aiIdx[iPage-1];
5900959160
5901059161
rc = sqlite3PagerWrite(pParent->pDbPage);
5901159162
if( rc==SQLITE_OK ){
5901259163
#ifndef SQLITE_OMIT_QUICKBALANCE
59013
- if( pPage->hasData
59164
+ if( pPage->intKeyLeaf
5901459165
&& pPage->nOverflow==1
5901559166
&& pPage->aiOvfl[0]==pPage->nCell
5901659167
&& pParent->pgno!=1
5901759168
&& pParent->nCell==iIdx
5901859169
){
@@ -59127,11 +59278,12 @@
5912759278
assert( pCur->skipNext!=SQLITE_OK );
5912859279
return pCur->skipNext;
5912959280
}
5913059281
5913159282
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
5913359285
&& (pBt->btsFlags & BTS_READ_ONLY)==0 );
5913459286
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
5913559287
5913659288
/* Assert that the caller has been consistent. If this cursor was opened
5913759289
** expecting an index b-tree, then the caller should be inserting blob
@@ -59160,11 +59312,12 @@
5916059312
invalidateIncrblobCursors(p, nKey, 0);
5916159313
5916259314
/* If the cursor is currently on the last row and we are appending a
5916359315
** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
5916459316
** 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 ){
5916659319
loc = -1;
5916759320
}
5916859321
}
5916959322
5917059323
if( !loc ){
@@ -59179,13 +59332,12 @@
5917959332
5918059333
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5918159334
pCur->pgnoRoot, nKey, nData, pPage->pgno,
5918259335
loc==0 ? "overwrite" : "new entry"));
5918359336
assert( pPage->isInit );
59184
- allocateTempSpace(pBt);
5918559337
newCell = pBt->pTmpSpace;
59186
- if( newCell==0 ) return SQLITE_NOMEM;
59338
+ assert( newCell!=0 );
5918759339
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
5918859340
if( rc ) goto end_insert;
5918959341
assert( szNew==cellSizePtr(pPage, newCell) );
5919059342
assert( szNew <= MX_CELL_SIZE(pBt) );
5919159343
idx = pCur->aiIdx[pCur->iPage];
@@ -59198,12 +59350,11 @@
5919859350
}
5919959351
oldCell = findCell(pPage, idx);
5920059352
if( !pPage->leaf ){
5920159353
memcpy(newCell, oldCell, 4);
5920259354
}
59203
- szOld = cellSizePtr(pPage, oldCell);
59204
- rc = clearCell(pPage, oldCell);
59355
+ rc = clearCell(pPage, oldCell, &szOld);
5920559356
dropCell(pPage, idx, szOld, &rc);
5920659357
if( rc ) goto end_insert;
5920759358
}else if( loc<0 && pPage->nCell>0 ){
5920859359
assert( pPage->leaf );
5920959360
idx = ++pCur->aiIdx[pCur->iPage];
@@ -59261,10 +59412,11 @@
5926159412
int rc; /* Return code */
5926259413
MemPage *pPage; /* Page to delete cell from */
5926359414
unsigned char *pCell; /* Pointer to cell to delete */
5926459415
int iCellIdx; /* Index of cell to delete */
5926559416
int iCellDepth; /* Depth of node containing pCell */
59417
+ u16 szCell; /* Size of the cell being deleted */
5926659418
5926759419
assert( cursorHoldsMutex(pCur) );
5926859420
assert( pBt->inTransaction==TRANS_WRITE );
5926959421
assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
5927059422
assert( pCur->curFlags & BTCF_WriteFlag );
@@ -59309,12 +59461,12 @@
5930959461
invalidateIncrblobCursors(p, pCur->info.nKey, 0);
5931059462
}
5931159463
5931259464
rc = sqlite3PagerWrite(pPage->pDbPage);
5931359465
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);
5931659468
if( rc ) return rc;
5931759469
5931859470
/* If the cell deleted was not located on a leaf page, then the cursor
5931959471
** is currently pointing to the largest entry in the sub-tree headed
5932059472
** by the child-page of the cell that was just deleted from an internal
@@ -59327,14 +59479,12 @@
5932759479
unsigned char *pTmp;
5932859480
5932959481
pCell = findCell(pLeaf, pLeaf->nCell-1);
5933059482
nCell = cellSizePtr(pLeaf, pCell);
5933159483
assert( MX_CELL_SIZE(pBt) >= nCell );
59332
-
59333
- allocateTempSpace(pBt);
5933459484
pTmp = pBt->pTmpSpace;
59335
-
59485
+ assert( pTmp!=0 );
5933659486
rc = sqlite3PagerWrite(pLeaf->pDbPage);
5933759487
insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
5933859488
dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
5933959489
if( rc ) return rc;
5934059490
}
@@ -59542,10 +59692,11 @@
5954259692
MemPage *pPage;
5954359693
int rc;
5954459694
unsigned char *pCell;
5954559695
int i;
5954659696
int hdr;
59697
+ u16 szCell;
5954759698
5954859699
assert( sqlite3_mutex_held(pBt->mutex) );
5954959700
if( pgno>btreePagecount(pBt) ){
5955059701
return SQLITE_CORRUPT_BKPT;
5955159702
}
@@ -59557,11 +59708,11 @@
5955759708
pCell = findCell(pPage, i);
5955859709
if( !pPage->leaf ){
5955959710
rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
5956059711
if( rc ) goto cleardatabasepage_out;
5956159712
}
59562
- rc = clearCell(pPage, pCell);
59713
+ rc = clearCell(pPage, pCell, &szCell);
5956359714
if( rc ) goto cleardatabasepage_out;
5956459715
}
5956559716
if( !pPage->leaf ){
5956659717
rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
5956759718
if( rc ) goto cleardatabasepage_out;
@@ -59903,24 +60054,25 @@
5990360054
/*
5990460055
** Append a message to the error message string.
5990560056
*/
5990660057
static void checkAppendMsg(
5990760058
IntegrityCk *pCheck,
59908
- char *zMsg1,
5990960059
const char *zFormat,
5991060060
...
5991160061
){
5991260062
va_list ap;
60063
+ char zBuf[200];
5991360064
if( !pCheck->mxErr ) return;
5991460065
pCheck->mxErr--;
5991560066
pCheck->nErr++;
5991660067
va_start(ap, zFormat);
5991760068
if( pCheck->errMsg.nChar ){
5991860069
sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
5991960070
}
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);
5992260074
}
5992360075
sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
5992460076
va_end(ap);
5992560077
if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
5992660078
pCheck->mallocFailed = 1;
@@ -59954,18 +60106,18 @@
5995460106
** Return 1 if there are 2 or more references to the page and 0 if
5995560107
** if this is the first reference to the page.
5995660108
**
5995760109
** Also check that the page number is in bounds.
5995860110
*/
59959
-static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
60111
+static int checkRef(IntegrityCk *pCheck, Pgno iPage){
5996060112
if( iPage==0 ) return 1;
5996160113
if( iPage>pCheck->nPage ){
59962
- checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
60114
+ checkAppendMsg(pCheck, "invalid page number %d", iPage);
5996360115
return 1;
5996460116
}
5996560117
if( getPageReferenced(pCheck, iPage) ){
59966
- checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
60118
+ checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
5996760119
return 1;
5996860120
}
5996960121
setPageReferenced(pCheck, iPage);
5997060122
return 0;
5997160123
}
@@ -59978,26 +60130,25 @@
5997860130
*/
5997960131
static void checkPtrmap(
5998060132
IntegrityCk *pCheck, /* Integrity check context */
5998160133
Pgno iChild, /* Child page number */
5998260134
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 */
5998560136
){
5998660137
int rc;
5998760138
u8 ePtrmapType;
5998860139
Pgno iPtrmapParent;
5998960140
5999060141
rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
5999160142
if( rc!=SQLITE_OK ){
5999260143
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);
5999460145
return;
5999560146
}
5999660147
5999760148
if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
59998
- checkAppendMsg(pCheck, zContext,
60149
+ checkAppendMsg(pCheck,
5999960150
"Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6000060151
iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6000160152
}
6000260153
}
6000360154
#endif
@@ -60008,51 +60159,50 @@
6000860159
*/
6000960160
static void checkList(
6001060161
IntegrityCk *pCheck, /* Integrity checking context */
6001160162
int isFreeList, /* True for a freelist. False for overflow page list */
6001260163
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 */
6001560165
){
6001660166
int i;
6001760167
int expected = N;
6001860168
int iFirst = iPage;
6001960169
while( N-- > 0 && pCheck->mxErr ){
6002060170
DbPage *pOvflPage;
6002160171
unsigned char *pOvflData;
6002260172
if( iPage<1 ){
60023
- checkAppendMsg(pCheck, zContext,
60173
+ checkAppendMsg(pCheck,
6002460174
"%d of %d pages missing from overflow list starting at %d",
6002560175
N+1, expected, iFirst);
6002660176
break;
6002760177
}
60028
- if( checkRef(pCheck, iPage, zContext) ) break;
60178
+ if( checkRef(pCheck, iPage) ) break;
6002960179
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);
6003160181
break;
6003260182
}
6003360183
pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
6003460184
if( isFreeList ){
6003560185
int n = get4byte(&pOvflData[4]);
6003660186
#ifndef SQLITE_OMIT_AUTOVACUUM
6003760187
if( pCheck->pBt->autoVacuum ){
60038
- checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
60188
+ checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
6003960189
}
6004060190
#endif
6004160191
if( n>(int)pCheck->pBt->usableSize/4-2 ){
60042
- checkAppendMsg(pCheck, zContext,
60192
+ checkAppendMsg(pCheck,
6004360193
"freelist leaf count too big on page %d", iPage);
6004460194
N--;
6004560195
}else{
6004660196
for(i=0; i<n; i++){
6004760197
Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
6004860198
#ifndef SQLITE_OMIT_AUTOVACUUM
6004960199
if( pCheck->pBt->autoVacuum ){
60050
- checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
60200
+ checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
6005160201
}
6005260202
#endif
60053
- checkRef(pCheck, iFreePage, zContext);
60203
+ checkRef(pCheck, iFreePage);
6005460204
}
6005560205
N -= n;
6005660206
}
6005760207
}
6005860208
#ifndef SQLITE_OMIT_AUTOVACUUM
@@ -60061,11 +60211,11 @@
6006160211
** page in this overflow list, check that the pointer-map entry for
6006260212
** the following page matches iPage.
6006360213
*/
6006460214
if( pCheck->pBt->autoVacuum && N>0 ){
6006560215
i = get4byte(pOvflData);
60066
- checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
60216
+ checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
6006760217
}
6006860218
}
6006960219
#endif
6007060220
iPage = get4byte(pOvflData);
6007160221
sqlite3PagerUnref(pOvflPage);
@@ -60093,11 +60243,10 @@
6009360243
** the root of the tree.
6009460244
*/
6009560245
static int checkTreePage(
6009660246
IntegrityCk *pCheck, /* Context for the sanity check */
6009760247
int iPage, /* Page number of the page to check */
60098
- char *zParentContext, /* Parent context */
6009960248
i64 *pnParentMinKey,
6010060249
i64 *pnParentMaxKey
6010160250
){
6010260251
MemPage *pPage;
6010360252
int i, rc, depth, d2, pgno, cnt;
@@ -60104,38 +60253,42 @@
6010460253
int hdr, cellStart;
6010560254
int nCell;
6010660255
u8 *data;
6010760256
BtShared *pBt;
6010860257
int usableSize;
60109
- char zContext[100];
6011060258
char *hit = 0;
6011160259
i64 nMinKey = 0;
6011260260
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;
6011560264
6011660265
/* Check that the page exists
6011760266
*/
6011860267
pBt = pCheck->pBt;
6011960268
usableSize = pBt->usableSize;
6012060269
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;
6012260273
if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
60123
- checkAppendMsg(pCheck, zContext,
60274
+ checkAppendMsg(pCheck,
6012460275
"unable to get the page. error code=%d", rc);
60125
- return 0;
60276
+ depth = -1;
60277
+ goto end_of_check;
6012660278
}
6012760279
6012860280
/* Clear MemPage.isInit to make sure the corruption detection code in
6012960281
** btreeInitPage() is executed. */
6013060282
pPage->isInit = 0;
6013160283
if( (rc = btreeInitPage(pPage))!=0 ){
6013260284
assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
60133
- checkAppendMsg(pCheck, zContext,
60285
+ checkAppendMsg(pCheck,
6013460286
"btreeInitPage() returns error code %d", rc);
6013560287
releasePage(pPage);
60136
- return 0;
60288
+ depth = -1;
60289
+ goto end_of_check;
6013760290
}
6013860291
6013960292
/* Check out all the cells.
6014060293
*/
6014160294
depth = 0;
@@ -60144,99 +60297,101 @@
6014460297
u32 sz;
6014560298
CellInfo info;
6014660299
6014760300
/* Check payload overflow pages
6014860301
*/
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;
6015160305
pCell = findCell(pPage,i);
6015260306
btreeParseCellPtr(pPage, pCell, &info);
60153
- sz = info.nData;
60154
- if( !pPage->intKey ) sz += (int)info.nKey;
60307
+ sz = info.nPayload;
6015560308
/* For intKey pages, check that the keys are in order.
6015660309
*/
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);
6016260316
}
6016360317
nMaxKey = info.nKey;
6016460318
}
60165
- assert( sz==info.nPayload );
6016660319
if( (sz>info.nLocal)
6016760320
&& (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
6016860321
){
6016960322
int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
6017060323
Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6017160324
#ifndef SQLITE_OMIT_AUTOVACUUM
6017260325
if( pBt->autoVacuum ){
60173
- checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
60326
+ checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
6017460327
}
6017560328
#endif
60176
- checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
60329
+ checkList(pCheck, 0, pgnoOvfl, nPage);
6017760330
}
6017860331
6017960332
/* Check sanity of left child page.
6018060333
*/
6018160334
if( !pPage->leaf ){
6018260335
pgno = get4byte(pCell);
6018360336
#ifndef SQLITE_OMIT_AUTOVACUUM
6018460337
if( pBt->autoVacuum ){
60185
- checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
60338
+ checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
6018660339
}
6018760340
#endif
60188
- d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
60341
+ d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey);
6018960342
if( i>0 && d2!=depth ){
60190
- checkAppendMsg(pCheck, zContext, "Child page depth differs");
60343
+ checkAppendMsg(pCheck, "Child page depth differs");
6019160344
}
6019260345
depth = d2;
6019360346
}
6019460347
}
6019560348
6019660349
if( !pPage->leaf ){
6019760350
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;
6020060353
#ifndef SQLITE_OMIT_AUTOVACUUM
6020160354
if( pBt->autoVacuum ){
60202
- checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
60355
+ checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
6020360356
}
6020460357
#endif
60205
- checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
60358
+ checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey);
6020660359
}
6020760360
6020860361
/* For intKey leaf pages, check that the min/max keys are in order
6020960362
** with any left/parent/right pages.
6021060363
*/
60364
+ pCheck->zPfx = "Page %d: ";
60365
+ pCheck->v1 = iPage;
6021160366
if( pPage->leaf && pPage->intKey ){
6021260367
/* if we are a left child page */
6021360368
if( pnParentMinKey ){
6021460369
/* if we are the left most child page */
6021560370
if( !pnParentMaxKey ){
6021660371
if( nMaxKey > *pnParentMinKey ){
60217
- checkAppendMsg(pCheck, zContext,
60372
+ checkAppendMsg(pCheck,
6021860373
"Rowid %lld out of order (max larger than parent min of %lld)",
6021960374
nMaxKey, *pnParentMinKey);
6022060375
}
6022160376
}else{
6022260377
if( nMinKey <= *pnParentMinKey ){
60223
- checkAppendMsg(pCheck, zContext,
60378
+ checkAppendMsg(pCheck,
6022460379
"Rowid %lld out of order (min less than parent min of %lld)",
6022560380
nMinKey, *pnParentMinKey);
6022660381
}
6022760382
if( nMaxKey > *pnParentMaxKey ){
60228
- checkAppendMsg(pCheck, zContext,
60383
+ checkAppendMsg(pCheck,
6022960384
"Rowid %lld out of order (max larger than parent max of %lld)",
6023060385
nMaxKey, *pnParentMaxKey);
6023160386
}
6023260387
*pnParentMinKey = nMaxKey;
6023360388
}
6023460389
/* else if we're a right child page */
6023560390
} else if( pnParentMaxKey ){
6023660391
if( nMinKey <= *pnParentMaxKey ){
60237
- checkAppendMsg(pCheck, zContext,
60392
+ checkAppendMsg(pCheck,
6023860393
"Rowid %lld out of order (min less than parent max of %lld)",
6023960394
nMinKey, *pnParentMaxKey);
6024060395
}
6024160396
}
6024260397
}
@@ -60244,10 +60399,11 @@
6024460399
/* Check for complete coverage of the page
6024560400
*/
6024660401
data = pPage->aData;
6024760402
hdr = pPage->hdrOffset;
6024860403
hit = sqlite3PageMalloc( pBt->pageSize );
60404
+ pCheck->zPfx = 0;
6024960405
if( hit==0 ){
6025060406
pCheck->mallocFailed = 1;
6025160407
}else{
6025260408
int contentOffset = get2byteNotZero(&data[hdr+5]);
6025360409
assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
@@ -60261,11 +60417,12 @@
6026160417
int j;
6026260418
if( pc<=usableSize-4 ){
6026360419
size = cellSizePtr(pPage, &data[pc]);
6026460420
}
6026560421
if( (int)(pc+size-1)>=usableSize ){
60266
- checkAppendMsg(pCheck, 0,
60422
+ pCheck->zPfx = 0;
60423
+ checkAppendMsg(pCheck,
6026760424
"Corruption detected in cell %d on page %d",i,iPage);
6026860425
}else{
6026960426
for(j=pc+size-1; j>=pc; j--) hit[j]++;
6027060427
}
6027160428
}
@@ -60283,23 +60440,28 @@
6028360440
}
6028460441
for(i=cnt=0; i<usableSize; i++){
6028560442
if( hit[i]==0 ){
6028660443
cnt++;
6028760444
}else if( hit[i]>1 ){
60288
- checkAppendMsg(pCheck, 0,
60445
+ checkAppendMsg(pCheck,
6028960446
"Multiple uses for byte %d of page %d", i, iPage);
6029060447
break;
6029160448
}
6029260449
}
6029360450
if( cnt!=data[hdr+7] ){
60294
- checkAppendMsg(pCheck, 0,
60451
+ checkAppendMsg(pCheck,
6029560452
"Fragmentation of %d bytes reported as %d on page %d",
6029660453
cnt, data[hdr+7], iPage);
6029760454
}
6029860455
}
6029960456
sqlite3PageFree(hit);
6030060457
releasePage(pPage);
60458
+
60459
+end_of_check:
60460
+ pCheck->zPfx = saved_zPfx;
60461
+ pCheck->v1 = saved_v1;
60462
+ pCheck->v2 = saved_v2;
6030160463
return depth+1;
6030260464
}
6030360465
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6030460466
6030560467
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
@@ -60336,10 +60498,13 @@
6033660498
sCheck.pPager = pBt->pPager;
6033760499
sCheck.nPage = btreePagecount(sCheck.pBt);
6033860500
sCheck.mxErr = mxErr;
6033960501
sCheck.nErr = 0;
6034060502
sCheck.mallocFailed = 0;
60503
+ sCheck.zPfx = 0;
60504
+ sCheck.v1 = 0;
60505
+ sCheck.v2 = 0;
6034160506
*pnErr = 0;
6034260507
if( sCheck.nPage==0 ){
6034360508
sqlite3BtreeLeave(p);
6034460509
return 0;
6034560510
}
@@ -60355,53 +60520,57 @@
6035560520
sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
6035660521
sCheck.errMsg.useMalloc = 2;
6035760522
6035860523
/* Check the integrity of the freelist
6035960524
*/
60525
+ sCheck.zPfx = "Main freelist: ";
6036060526
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;
6036260529
6036360530
/* Check all the tables.
6036460531
*/
6036560532
for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
6036660533
if( aRoot[i]==0 ) continue;
6036760534
#ifndef SQLITE_OMIT_AUTOVACUUM
6036860535
if( pBt->autoVacuum && aRoot[i]>1 ){
60369
- checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
60536
+ checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
6037060537
}
6037160538
#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;
6037360542
}
6037460543
6037560544
/* Make sure every page in the file is referenced
6037660545
*/
6037760546
for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
6037860547
#ifdef SQLITE_OMIT_AUTOVACUUM
6037960548
if( getPageReferenced(&sCheck, i)==0 ){
60380
- checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
60549
+ checkAppendMsg(&sCheck, "Page %d is never used", i);
6038160550
}
6038260551
#else
6038360552
/* If the database supports auto-vacuum, make sure no tables contain
6038460553
** references to pointer-map pages.
6038560554
*/
6038660555
if( getPageReferenced(&sCheck, i)==0 &&
6038760556
(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);
6038960558
}
6039060559
if( getPageReferenced(&sCheck, i)!=0 &&
6039160560
(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);
6039360562
}
6039460563
#endif
6039560564
}
6039660565
6039760566
/* Make sure this analysis did not leave any unref() pages.
6039860567
** This is an internal consistency check; an integrity check
6039960568
** of the integrity check.
6040060569
*/
6040160570
if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
60402
- checkAppendMsg(&sCheck, 0,
60571
+ checkAppendMsg(&sCheck,
6040360572
"Outstanding page count goes from %d to %d during this analysis",
6040460573
nRef, sqlite3PagerRefcount(pBt->pPager)
6040560574
);
6040660575
}
6040760576
@@ -60593,11 +60762,11 @@
6059360762
6059460763
/* Save the positions of all other cursors open on this table. This is
6059560764
** required in case any of them are holding references to an xFetch
6059660765
** version of the b-tree page modified by the accessPayload call below.
6059760766
**
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()
6059960768
** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
6060060769
** saveAllCursors can only return SQLITE_OK.
6060160770
*/
6060260771
VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
6060360772
assert( rc==SQLITE_OK );
@@ -63751,11 +63920,12 @@
6375163920
if( addr==p->nOp-1 ) p->nOp--;
6375263921
}
6375363922
}
6375463923
6375563924
/*
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.
6375763927
*/
6375863928
SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
6375963929
if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
6376063930
sqlite3VdbeChangeToNoop(p, p->nOp-1);
6376163931
return 1;
@@ -65676,14 +65846,10 @@
6567665846
for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
6567765847
vdbeFreeOpArray(db, p->aOp, p->nOp);
6567865848
sqlite3DbFree(db, p->aColName);
6567965849
sqlite3DbFree(db, p->zSql);
6568065850
sqlite3DbFree(db, p->pFree);
65681
-#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
65682
- sqlite3DbFree(db, p->zExplain);
65683
- sqlite3DbFree(db, p->pExplain);
65684
-#endif
6568565851
}
6568665852
6568765853
/*
6568865854
** Delete an entire VDBE.
6568965855
*/
@@ -67390,10 +67556,11 @@
6739067556
void (*xDel)(void *),
6739167557
unsigned char enc
6739267558
){
6739367559
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6739467560
assert( xDel!=SQLITE_DYNAMIC );
67561
+ if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
6739567562
if( n>0x7fffffff ){
6739667563
(void)invokeValueDestructor(z, xDel, pCtx);
6739767564
}else{
6739867565
setResultStrOrError(pCtx, z, (int)n, enc, xDel);
6739967566
}
@@ -68712,125 +68879,10 @@
6871268879
return sqlite3StrAccumFinish(&out);
6871368880
}
6871468881
6871568882
#endif /* #ifndef SQLITE_OMIT_TRACE */
6871668883
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
-
6883268884
/************** End of vdbetrace.c *******************************************/
6883368885
/************** Begin file vdbe.c ********************************************/
6883468886
/*
6883568887
** 2001 September 15
6883668888
**
@@ -70475,21 +70527,14 @@
7047570527
assert( pOp->p4type==P4_FUNCDEF );
7047670528
ctx.pFunc = pOp->p4.pFunc;
7047770529
ctx.iOp = pc;
7047870530
ctx.pVdbe = p;
7047970531
MemSetTypeFlag(ctx.pOut, MEM_Null);
70480
-
7048170532
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 );
7048970534
(*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
70490
- lastRowid = db->lastRowid;
70535
+ lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
7049170536
7049270537
/* If the function returned an error, throw an exception */
7049370538
if( ctx.fErrorOrAux ){
7049470539
if( ctx.isError ){
7049570540
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
@@ -72205,14 +72250,10 @@
7220572250
rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
7220672251
pCur->pKeyInfo = pKeyInfo;
7220772252
assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
7220872253
sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
7220972254
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
-
7221472255
/* Set the VdbeCursor.isTable variable. Previous versions of
7221572256
** SQLite used to check if the root-page flags were sane at this point
7221672257
** and report database corruption if they were not, but this check has
7221772258
** since moved into the btree layer. */
7221872259
pCur->isTable = pOp->p4type!=P4_KEYINFO;
@@ -72942,29 +72983,18 @@
7294272983
** largest possible integer (9223372036854775807) then the database
7294372984
** engine starts picking positive candidate ROWIDs at random until
7294472985
** it finds one that is not previously used. */
7294572986
assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
7294672987
** 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 */
7295172988
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,
7295372993
0, &res))==SQLITE_OK)
7295472994
&& (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));
7296672996
if( rc==SQLITE_OK && res==0 ){
7296772997
rc = SQLITE_FULL; /* IMP: R-38219-53002 */
7296872998
goto abort_due_to_error;
7296972999
}
7297073000
assert( v>0 ); /* EV: R-40812-03570 */
@@ -74556,18 +74586,13 @@
7455674586
ctx.pMem = pMem = &aMem[pOp->p3];
7455774587
pMem->n++;
7455874588
sqlite3VdbeMemInit(&t, db, MEM_Null);
7455974589
ctx.pOut = &t;
7456074590
ctx.isError = 0;
74561
- ctx.pColl = 0;
74591
+ ctx.pVdbe = p;
74592
+ ctx.iOp = pc;
7456274593
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
- }
7456974594
(ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
7457074595
if( ctx.isError ){
7457174596
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
7457274597
rc = ctx.isError;
7457374598
}
@@ -81597,10 +81622,11 @@
8159781622
pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
8159881623
pNew->addrOpenEphm[0] = -1;
8159981624
pNew->addrOpenEphm[1] = -1;
8160081625
pNew->nSelectRow = p->nSelectRow;
8160181626
pNew->pWith = withDup(db, p->pWith);
81627
+ sqlite3SelectSetName(pNew, p->zSelName);
8160281628
return pNew;
8160381629
}
8160481630
#else
8160581631
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
8160681632
assert( p==0 );
@@ -81739,36 +81765,44 @@
8173981765
}
8174081766
8174181767
/*
8174281768
** These routines are Walker callbacks. Walker.u.pi is a pointer
8174381769
** 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
8174581771
** not constant.
8174681772
**
8174781773
** These callback routines are used to implement the following:
8174881774
**
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
8175281778
**
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.
8175381787
*/
8175481788
static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
8175581789
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
8175781791
** the ON or USING clauses of a join disqualifies the expression
8175881792
** from being considered constant. */
81759
- if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
81793
+ if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
8176081794
pWalker->u.i = 0;
8176181795
return WRC_Abort;
8176281796
}
8176381797
8176481798
switch( pExpr->op ){
8176581799
/* 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
8176781801
** flag. */
8176881802
case TK_FUNCTION:
81769
- if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
81803
+ if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){
8177081804
return WRC_Continue;
8177181805
}
8177281806
/* Fall through */
8177381807
case TK_ID:
8177481808
case TK_COLUMN:
@@ -81778,10 +81812,23 @@
8177881812
testcase( pExpr->op==TK_COLUMN );
8177981813
testcase( pExpr->op==TK_AGG_FUNCTION );
8178081814
testcase( pExpr->op==TK_AGG_COLUMN );
8178181815
pWalker->u.i = 0;
8178281816
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 */
8178381830
default:
8178481831
testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
8178581832
testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
8178681833
return WRC_Continue;
8178781834
}
@@ -81818,11 +81865,11 @@
8181881865
** that does no originate from the ON or USING clauses of a join.
8181981866
** Return 0 if it involves variables or function calls or terms from
8182081867
** an ON or USING clause.
8182181868
*/
8182281869
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
81823
- return exprIsConst(p, 3);
81870
+ return exprIsConst(p, 2);
8182481871
}
8182581872
8182681873
/*
8182781874
** Walk an expression tree. Return 1 if the expression is constant
8182881875
** or a function call with constant arguments. Return and 0 if there
@@ -81830,12 +81877,13 @@
8183081877
**
8183181878
** For the purposes of this function, a double-quoted string (ex: "abc")
8183281879
** is considered a variable but a single-quoted string (ex: 'abc') is
8183381880
** a constant.
8183481881
*/
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);
8183781885
}
8183881886
8183981887
/*
8184081888
** If the expression p codes a constant integer that is small enough
8184181889
** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -83741,94 +83789,90 @@
8374183789
iMem = ++pParse->nMem;
8374283790
sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
8374383791
exprToRegister(pExpr, iMem);
8374483792
}
8374583793
83746
-#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
83794
+#ifdef SQLITE_DEBUG
8374783795
/*
8374883796
** Generate a human-readable explanation of an expression tree.
8374983797
*/
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){
8375283799
const char *zBinOp = 0; /* Binary operator */
8375383800
const char *zUniOp = 0; /* Unary operator */
83801
+ pView = sqlite3TreeViewPush(pView, moreToFollow);
8375483802
if( pExpr==0 ){
83755
- op = TK_NULL;
83756
- }else{
83757
- op = pExpr->op;
83803
+ sqlite3TreeViewLine(pView, "nil");
83804
+ sqlite3TreeViewPop(pView);
83805
+ return;
8375883806
}
83759
- switch( op ){
83807
+ switch( pExpr->op ){
8376083808
case TK_AGG_COLUMN: {
83761
- sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
83809
+ sqlite3TreeViewLine(pView, "AGG{%d:%d}",
8376283810
pExpr->iTable, pExpr->iColumn);
8376383811
break;
8376483812
}
8376583813
case TK_COLUMN: {
8376683814
if( pExpr->iTable<0 ){
8376783815
/* This only happens when coding check constraints */
83768
- sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
83816
+ sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
8376983817
}else{
83770
- sqlite3ExplainPrintf(pOut, "{%d:%d}",
83818
+ sqlite3TreeViewLine(pView, "{%d:%d}",
8377183819
pExpr->iTable, pExpr->iColumn);
8377283820
}
8377383821
break;
8377483822
}
8377583823
case TK_INTEGER: {
8377683824
if( pExpr->flags & EP_IntValue ){
83777
- sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
83825
+ sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
8377883826
}else{
83779
- sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
83827
+ sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
8378083828
}
8378183829
break;
8378283830
}
8378383831
#ifndef SQLITE_OMIT_FLOATING_POINT
8378483832
case TK_FLOAT: {
83785
- sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
83833
+ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
8378683834
break;
8378783835
}
8378883836
#endif
8378983837
case TK_STRING: {
83790
- sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
83838
+ sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
8379183839
break;
8379283840
}
8379383841
case TK_NULL: {
83794
- sqlite3ExplainPrintf(pOut,"NULL");
83842
+ sqlite3TreeViewLine(pView,"NULL");
8379583843
break;
8379683844
}
8379783845
#ifndef SQLITE_OMIT_BLOB_LITERAL
8379883846
case TK_BLOB: {
83799
- sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
83847
+ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
8380083848
break;
8380183849
}
8380283850
#endif
8380383851
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);
8380683854
break;
8380783855
}
8380883856
case TK_REGISTER: {
83809
- sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
83857
+ sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
8381083858
break;
8381183859
}
8381283860
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);
8381483867
break;
8381583868
}
8381683869
#ifndef SQLITE_OMIT_CAST
8381783870
case TK_CAST: {
8381883871
/* 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);
8383083874
break;
8383183875
}
8383283876
#endif /* SQLITE_OMIT_CAST */
8383383877
case TK_LT: zBinOp = "LT"; break;
8383483878
case TK_LE: zBinOp = "LE"; break;
@@ -83848,21 +83892,22 @@
8384883892
case TK_BITOR: zBinOp = "BITOR"; break;
8384983893
case TK_SLASH: zBinOp = "DIV"; break;
8385083894
case TK_LSHIFT: zBinOp = "LSHIFT"; break;
8385183895
case TK_RSHIFT: zBinOp = "RSHIFT"; break;
8385283896
case TK_CONCAT: zBinOp = "CONCAT"; break;
83897
+ case TK_DOT: zBinOp = "DOT"; break;
8385383898
8385483899
case TK_UMINUS: zUniOp = "UMINUS"; break;
8385583900
case TK_UPLUS: zUniOp = "UPLUS"; break;
8385683901
case TK_BITNOT: zUniOp = "BITNOT"; break;
8385783902
case TK_NOT: zUniOp = "NOT"; break;
8385883903
case TK_ISNULL: zUniOp = "ISNULL"; break;
8385983904
case TK_NOTNULL: zUniOp = "NOTNULL"; break;
8386083905
8386183906
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);
8386483909
break;
8386583910
}
8386683911
8386783912
case TK_AGG_FUNCTION:
8386883913
case TK_FUNCTION: {
@@ -83870,45 +83915,40 @@
8387083915
if( ExprHasProperty(pExpr, EP_TokenOnly) ){
8387183916
pFarg = 0;
8387283917
}else{
8387383918
pFarg = pExpr->x.pList;
8387483919
}
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",
8387783922
pExpr->op2, pExpr->u.zToken);
8387883923
}else{
83879
- sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
83924
+ sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
8388083925
}
8388183926
if( pFarg ){
83882
- sqlite3ExplainExprList(pOut, pFarg);
83927
+ sqlite3TreeViewExprList(pView, pFarg, 0, 0);
8388383928
}
83884
- sqlite3ExplainPrintf(pOut, ")");
8388583929
break;
8388683930
}
8388783931
#ifndef SQLITE_OMIT_SUBQUERY
8388883932
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);
8389283935
break;
8389383936
}
8389483937
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);
8389883940
break;
8389983941
}
8390083942
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);
8390483945
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83905
- sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
83946
+ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
8390683947
}else{
83907
- sqlite3ExplainExprList(pOut, pExpr->x.pList);
83948
+ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
8390883949
}
83909
- sqlite3ExplainPrintf(pOut, ")");
8391083950
break;
8391183951
}
8391283952
#endif /* SQLITE_OMIT_SUBQUERY */
8391383953
8391483954
/*
@@ -83924,17 +83964,14 @@
8392483964
*/
8392583965
case TK_BETWEEN: {
8392683966
Expr *pX = pExpr->pLeft;
8392783967
Expr *pY = pExpr->x.pList->a[0].pExpr;
8392883968
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);
8393683973
break;
8393783974
}
8393883975
case TK_TRIGGER: {
8393983976
/* If the opcode is TK_TRIGGER, then the expression is a reference
8394083977
** to a column in the new.* or old.* pseudo-tables available to
@@ -83941,19 +83978,18 @@
8394183978
** trigger programs. In this case Expr.iTable is set to 1 for the
8394283979
** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
8394383980
** is set to the column of the pseudo-table to read, or to -1 to
8394483981
** read the rowid field.
8394583982
*/
83946
- sqlite3ExplainPrintf(pOut, "%s(%d)",
83983
+ sqlite3TreeViewLine(pView, "%s(%d)",
8394783984
pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
8394883985
break;
8394983986
}
8395083987
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);
8395583991
break;
8395683992
}
8395783993
#ifndef SQLITE_OMIT_TRIGGER
8395883994
case TK_RAISE: {
8395983995
const char *zType = "unk";
@@ -83961,59 +83997,61 @@
8396183997
case OE_Rollback: zType = "rollback"; break;
8396283998
case OE_Abort: zType = "abort"; break;
8396383999
case OE_Fail: zType = "fail"; break;
8396484000
case OE_Ignore: zType = "ignore"; break;
8396584001
}
83966
- sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
84002
+ sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
8396784003
break;
8396884004
}
8396984005
#endif
84006
+ default: {
84007
+ sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
84008
+ break;
84009
+ }
8397084010
}
8397184011
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);
8397784015
}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
8398684024
/*
8398784025
** Generate a human-readable explanation of an expression list.
8398884026
*/
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
+){
8399084033
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);
8399684038
}else{
83997
- sqlite3ExplainPush(pOut);
84039
+ sqlite3TreeViewLine(pView, "%s", zLabel);
8399884040
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 ){
8400484044
sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
8400584045
}
8400684046
if( pList->a[i].bSpanIsTab ){
8400784047
sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
8400884048
}
84009
- if( i<pList->nExpr-1 ){
84010
- sqlite3ExplainNL(pOut);
84011
- }
84049
+#endif
8401284050
}
84013
- sqlite3ExplainPop(pOut);
8401484051
}
84052
+ sqlite3TreeViewPop(pView);
8401584053
}
8401684054
#endif /* SQLITE_DEBUG */
8401784055
8401884056
/*
8401984057
** Generate code that pushes the value of every element of the given
@@ -89633,11 +89671,11 @@
8963389671
Column *pCol;
8963489672
sqlite3 *db = pParse->db;
8963589673
p = pParse->pNewTable;
8963689674
if( p!=0 ){
8963789675
pCol = &(p->aCol[p->nCol-1]);
89638
- if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
89676
+ if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
8963989677
sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
8964089678
pCol->zName);
8964189679
}else{
8964289680
/* A copy of pExpr is used instead of the original, as pExpr contains
8964389681
** tokens that point to volatile memory. The 'span' of the expression
@@ -94030,11 +94068,14 @@
9403094068
9403194069
/*
9403294070
** Return the collating function associated with a function.
9403394071
*/
9403494072
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;
9403694077
}
9403794078
9403894079
/*
9403994080
** Indicate that the accumulator load should be skipped on this
9404094081
** iteration of the aggregate loop.
@@ -94575,14 +94616,16 @@
9457594616
** character is exactly one byte in size. Also, all characters are
9457694617
** able to participate in upper-case-to-lower-case mappings in EBCDIC
9457794618
** whereas only characters less than 0x80 do in ASCII.
9457894619
*/
9457994620
#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]
9458294624
#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]
9458494627
#endif
9458594628
9458694629
static const struct compareInfo globInfo = { '*', '?', '[', 0 };
9458794630
/* The correct SQL-92 behavior is for the LIKE operator to ignore
9458894631
** case. Thus 'a' LIKE 'A' would be true. */
@@ -94591,11 +94634,11 @@
9459194634
** is case sensitive causing 'a' LIKE 'A' to be false */
9459294635
static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
9459394636
9459494637
/*
9459594638
** 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
9459794640
** are the same and false (0) if they are different.
9459894641
**
9459994642
** Globbing rules:
9460094643
**
9460194644
** '*' Matches any sequence of zero or more characters.
@@ -94611,120 +94654,147 @@
9461194654
** in the list by making it the first character after '[' or '^'. A
9461294655
** range of characters can be specified using '-'. Example:
9461394656
** "[a-z]" matches any single lower-case letter. To match a '-', make
9461494657
** it the last character in the list.
9461594658
**
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
+**
9461694670
** 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
9462194671
*/
9462294672
static int patternCompare(
9462394673
const u8 *zPattern, /* The glob pattern */
9462494674
const u8 *zString, /* The string to compare against the glob */
9462594675
const struct compareInfo *pInfo, /* Information about how to do the compare */
9462694676
u32 esc /* The escape character */
9462794677
){
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;
9463694691
9463794692
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 */
9463994697
while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
9464094698
|| c == matchOne ){
9464194699
if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
9464294700
return 0;
9464394701
}
9464494702
}
9464594703
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;
9472694796
}
9472794797
return *zString==0;
9472894798
}
9472994799
9473094800
/*
@@ -103884,10 +103954,24 @@
103884103954
**
103885103955
*************************************************************************
103886103956
** This file contains C code routines that are called by the parser
103887103957
** to handle SELECT statements in SQLite.
103888103958
*/
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
+
103889103973
103890103974
/*
103891103975
** An instance of the following object is used to record information about
103892103976
** how to process the DISTINCT keyword, to simplify passing that information
103893103977
** into the selectInnerLoop() routine.
@@ -103996,10 +104080,22 @@
103996104080
assert( pNew->pSrc!=0 || pParse->nErr>0 );
103997104081
}
103998104082
assert( pNew!=&standin );
103999104083
return pNew;
104000104084
}
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
+
104001104097
104002104098
/*
104003104099
** Delete the given Select structure and all of its substructures.
104004104100
*/
104005104101
SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
@@ -107226,10 +107322,12 @@
107226107322
}
107227107323
}
107228107324
}
107229107325
107230107326
/***** 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));
107231107329
107232107330
/* Authorize the subquery */
107233107331
pParse->zAuthContext = pSubitem->zName;
107234107332
TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
107235107333
testcase( i==SQLITE_DENY );
@@ -107278,10 +107376,11 @@
107278107376
p->pSrc = 0;
107279107377
p->pPrior = 0;
107280107378
p->pLimit = 0;
107281107379
p->pOffset = 0;
107282107380
pNew = sqlite3SelectDup(db, p, 0);
107381
+ sqlite3SelectSetName(pNew, pSub->zSelName);
107283107382
p->pOffset = pOffset;
107284107383
p->pLimit = pLimit;
107285107384
p->pOrderBy = pOrderBy;
107286107385
p->pSrc = pSrc;
107287107386
p->op = TK_ALL;
@@ -107290,10 +107389,13 @@
107290107389
}else{
107291107390
pNew->pPrior = pPrior;
107292107391
if( pPrior ) pPrior->pNext = pNew;
107293107392
pNew->pNext = p;
107294107393
p->pPrior = pNew;
107394
+ SELECTTRACE(2,pParse,p,
107395
+ ("compound-subquery flattener creates %s.%p as peer\n",
107396
+ pNew->zSelName, pNew));
107295107397
}
107296107398
if( db->mallocFailed ) return 1;
107297107399
}
107298107400
107299107401
/* Begin flattening the iFrom-th entry of the FROM clause
@@ -107419,12 +107521,27 @@
107419107521
if( isAgg ){
107420107522
substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
107421107523
pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
107422107524
}
107423107525
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
+ }
107424107540
assert( pParent->pOrderBy==0 );
107425
- pParent->pOrderBy = pSub->pOrderBy;
107541
+ assert( pSub->pPrior==0 );
107542
+ pParent->pOrderBy = pOrderBy;
107426107543
pSub->pOrderBy = 0;
107427107544
}else if( pParent->pOrderBy ){
107428107545
substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
107429107546
}
107430107547
if( pSub->pWhere ){
@@ -107465,10 +107582,17 @@
107465107582
107466107583
/* Finially, delete what is left of the subquery and return
107467107584
** success.
107468107585
*/
107469107586
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
107470107594
107471107595
return 1;
107472107596
}
107473107597
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
107474107598
@@ -107936,10 +108060,11 @@
107936108060
if( pTab->pSelect || IsVirtual(pTab) ){
107937108061
/* We reach here if the named table is a really a view */
107938108062
if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
107939108063
assert( pFrom->pSelect==0 );
107940108064
pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
108065
+ sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
107941108066
sqlite3WalkSelect(pWalker, pFrom->pSelect);
107942108067
}
107943108068
#endif
107944108069
}
107945108070
@@ -108470,10 +108595,17 @@
108470108595
if( p==0 || db->mallocFailed || pParse->nErr ){
108471108596
return 1;
108472108597
}
108473108598
if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
108474108599
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
108475108607
108476108608
assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
108477108609
assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
108478108610
assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
108479108611
assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
@@ -108626,10 +108758,14 @@
108626108758
/* If there is are a sequence of queries, do the earlier ones first.
108627108759
*/
108628108760
if( p->pPrior ){
108629108761
rc = multiSelect(pParse, p, pDest);
108630108762
explainSetInteger(pParse->iSelectId, iRestoreSelectId);
108763
+#if SELECTTRACE_ENABLED
108764
+ SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
108765
+ pParse->nSelectIndent--;
108766
+#endif
108631108767
return rc;
108632108768
}
108633108769
#endif
108634108770
108635108771
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -109225,107 +109361,110 @@
109225109361
generateColumnNames(pParse, pTabList, pEList);
109226109362
}
109227109363
109228109364
sqlite3DbFree(db, sAggInfo.aCol);
109229109365
sqlite3DbFree(db, sAggInfo.aFunc);
109366
+#if SELECTTRACE_ENABLED
109367
+ SELECTTRACE(1,pParse,p,("end processing\n"));
109368
+ pParse->nSelectIndent--;
109369
+#endif
109230109370
return rc;
109231109371
}
109232109372
109233
-#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
109373
+#ifdef SQLITE_DEBUG
109234109374
/*
109235109375
** Generate a human-readable description of a the Select object.
109236109376
*/
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");
109251109393
if( p->pSrc && p->pSrc->nSrc ){
109252109394
int i;
109253
- sqlite3ExplainPrintf(pVdbe, "FROM ");
109254
- sqlite3ExplainPush(pVdbe);
109395
+ pView = sqlite3TreeViewPush(pView, (n--)>0);
109396
+ sqlite3TreeViewLine(pView, "FROM");
109255109397
for(i=0; i<p->pSrc->nSrc; i++){
109256109398
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);
109263109405
}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);
109265109410
}
109266109411
if( pItem->zAlias ){
109267
- sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
109412
+ sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
109268109413
}
109269109414
if( pItem->jointype & JT_LEFT ){
109270
- sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
109415
+ sqlite3XPrintf(&x, 0, " LEFT-JOIN");
109271109416
}
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);
109273109423
}
109274
- sqlite3ExplainPop(pVdbe);
109424
+ sqlite3TreeViewPop(pView);
109275109425
}
109276109426
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);
109280109430
}
109281109431
if( p->pGroupBy ){
109282
- sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
109283
- sqlite3ExplainExprList(pVdbe, p->pGroupBy);
109284
- sqlite3ExplainNL(pVdbe);
109432
+ sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
109285109433
}
109286109434
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);
109290109438
}
109291109439
if( p->pOrderBy ){
109292
- sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
109293
- sqlite3ExplainExprList(pVdbe, p->pOrderBy);
109294
- sqlite3ExplainNL(pVdbe);
109440
+ sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
109295109441
}
109296109442
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);
109300109446
}
109301109447
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 */
109327109466
109328109467
/************** End of select.c **********************************************/
109329109468
/************** Begin file table.c *******************************************/
109330109469
/*
109331109470
** 2001 September 15
@@ -113719,15 +113858,10 @@
113719113858
assert( TK_LE>TK_EQ && TK_LE<TK_GE );
113720113859
assert( TK_GE==TK_EQ+4 );
113721113860
return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
113722113861
}
113723113862
113724
-/*
113725
-** Swap two objects of type TYPE.
113726
-*/
113727
-#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
113728
-
113729113863
/*
113730113864
** Commute a comparison operator. Expressions of the form "X op Y"
113731113865
** are converted into "Y op X".
113732113866
**
113733113867
** If left/right precedence rules come into play when determining the
@@ -116884,12 +117018,13 @@
116884117018
116885117019
/* Run a separate WHERE clause for each term of the OR clause. After
116886117020
** eliminating duplicates from other WHERE clauses, the action for each
116887117021
** sub-WHERE clause is to to invoke the main loop body as a subroutine.
116888117022
*/
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;
116891117026
for(ii=0; ii<pOrWc->nTerm; ii++){
116892117027
WhereTerm *pOrTerm = &pOrWc->a[ii];
116893117028
if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
116894117029
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
116895117030
Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -116897,10 +117032,11 @@
116897117032
if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
116898117033
pAndExpr->pLeft = pOrExpr;
116899117034
pOrExpr = pAndExpr;
116900117035
}
116901117036
/* Loop through table entries that match term pOrTerm. */
117037
+ WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
116902117038
pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
116903117039
wctrlFlags, iCovCur);
116904117040
assert( pSubWInfo || pParse->nErr || db->mallocFailed );
116905117041
if( pSubWInfo ){
116906117042
WhereLoop *pSubLoop;
@@ -117116,25 +117252,30 @@
117116117252
}
117117117253
117118117254
return pLevel->notReady;
117119117255
}
117120117256
117121
-#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
117257
+#ifdef WHERETRACE_ENABLED
117122117258
/*
117123
-** Generate "Explanation" text for a WhereTerm.
117259
+** Print the content of a WhereTerm object
117124117260
*/
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
117136117277
117137117278
#ifdef WHERETRACE_ENABLED
117138117279
/*
117139117280
** Print a WhereLoop object for debugging purposes
117140117281
*/
@@ -117174,31 +117315,16 @@
117174117315
sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
117175117316
}else{
117176117317
sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
117177117318
}
117178117319
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 ){
117184117321
int i;
117185
- Vdbe *v = pWInfo->pParse->pVdbe;
117186
- sqlite3ExplainBegin(v);
117187117322
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
+ }
117200117326
}
117201117327
#endif
117202117328
117203117329
/*
117204117330
** Convert bulk memory into a valid WhereLoop that can be passed
@@ -117714,15 +117840,18 @@
117714117840
pNew->aLTerm[pNew->nLTerm++] = 0;
117715117841
pNew->wsFlags |= WHERE_SKIPSCAN;
117716117842
nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
117717117843
if( pTerm ){
117718117844
/* 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
117720117846
** desirable than the regular index lookup. */
117721117847
nIter += 10; assert( 10==sqlite3LogEst(2) );
117722117848
}
117723117849
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;
117724117853
whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
117725117854
pNew->nOut = saved_nOut;
117726117855
pNew->u.btree.nEq = saved_nEq;
117727117856
pNew->u.btree.nSkip = saved_nSkip;
117728117857
}
@@ -118073,13 +118202,21 @@
118073118202
pNew->u.btree.nSkip = 0;
118074118203
pNew->u.btree.pIndex = 0;
118075118204
pNew->nLTerm = 1;
118076118205
pNew->aLTerm[0] = pTerm;
118077118206
/* 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
+ }
118081118218
ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
118082118219
/* TUNING: Each index lookup yields 20 rows in the table. This
118083118220
** is more than the usual guess of 10 rows, since we have no way
118084118221
** of knowing how selective the index will ultimately be. It would
118085118222
** not be unreasonable to make this value much larger. */
@@ -118363,11 +118500,10 @@
118363118500
WhereLoopBuilder sSubBuild;
118364118501
WhereOrSet sSum, sCur;
118365118502
struct SrcList_item *pItem;
118366118503
118367118504
pWC = pBuilder->pWC;
118368
- if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
118369118505
pWCEnd = pWC->a + pWC->nTerm;
118370118506
pNew = pBuilder->pNew;
118371118507
memset(&sSum, 0, sizeof(sSum));
118372118508
pItem = pWInfo->pTabList->a + pNew->iTab;
118373118509
iCur = pItem->iCursor;
@@ -118384,10 +118520,11 @@
118384118520
118385118521
sSubBuild = *pBuilder;
118386118522
sSubBuild.pOrderBy = 0;
118387118523
sSubBuild.pOrSet = &sCur;
118388118524
118525
+ WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
118389118526
for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
118390118527
if( (pOrTerm->eOperator & WO_AND)!=0 ){
118391118528
sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
118392118529
}else if( pOrTerm->leftCursor==iCur ){
118393118530
tempWC.pWInfo = pWC->pWInfo;
@@ -118398,18 +118535,30 @@
118398118535
sSubBuild.pWC = &tempWC;
118399118536
}else{
118400118537
continue;
118401118538
}
118402118539
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
118403118549
#ifndef SQLITE_OMIT_VIRTUALTABLE
118404118550
if( IsVirtual(pItem->pTab) ){
118405118551
rc = whereLoopAddVirtual(&sSubBuild, mExtra);
118406118552
}else
118407118553
#endif
118408118554
{
118409118555
rc = whereLoopAddBtree(&sSubBuild, mExtra);
118410118556
}
118557
+ if( rc==SQLITE_OK ){
118558
+ rc = whereLoopAddOr(&sSubBuild, mExtra);
118559
+ }
118411118560
assert( rc==SQLITE_OK || sCur.n==0 );
118412118561
if( sCur.n==0 ){
118413118562
sSum.n = 0;
118414118563
break;
118415118564
}else if( once ){
@@ -118450,10 +118599,11 @@
118450118599
pNew->rRun = sSum.a[i].rRun + 1;
118451118600
pNew->nOut = sSum.a[i].nOut;
118452118601
pNew->prereq = sSum.a[i].prereq;
118453118602
rc = whereLoopInsert(pBuilder, pNew);
118454118603
}
118604
+ WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
118455118605
}
118456118606
}
118457118607
return rc;
118458118608
}
118459118609
@@ -119516,27 +119666,20 @@
119516119666
}
119517119667
}
119518119668
119519119669
/* Construct the WhereLoop objects */
119520119670
WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
119671
+#if defined(WHERETRACE_ENABLED)
119521119672
/* Display all terms of the WHERE clause */
119522
-#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
119523119673
if( sqlite3WhereTrace & 0x100 ){
119524119674
int i;
119525
- Vdbe *v = pParse->pVdbe;
119526
- sqlite3ExplainBegin(v);
119527119675
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
+ }
119536119678
}
119537119679
#endif
119680
+
119538119681
if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
119539119682
rc = whereLoopAddAll(&sWLB);
119540119683
if( rc ) goto whereBeginError;
119541119684
119542119685
/* Display all of the WhereLoop objects if wheretrace is enabled */
@@ -120059,11 +120202,11 @@
120059120202
120060120203
/* A routine to convert a binary TK_IS or TK_ISNOT expression into a
120061120204
** unary TK_ISNULL or TK_NOTNULL expression. */
120062120205
static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
120063120206
sqlite3 *db = pParse->db;
120064
- if( db->mallocFailed==0 && pY->op==TK_NULL ){
120207
+ if( pY && pA && pY->op==TK_NULL ){
120065120208
pA->op = (u8)op;
120066120209
sqlite3ExprDelete(db, pA->pRight);
120067120210
pA->pRight = 0;
120068120211
}
120069120212
}
@@ -122318,13 +122461,10 @@
122318122461
break;
122319122462
case 111: /* cmd ::= select */
122320122463
{
122321122464
SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
122322122465
sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
122323
- sqlite3ExplainBegin(pParse->pVdbe);
122324
- sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3);
122325
- sqlite3ExplainFinish(pParse->pVdbe);
122326122466
sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
122327122467
}
122328122468
break;
122329122469
case 112: /* select ::= with selectnowith */
122330122470
{
@@ -122377,10 +122517,34 @@
122377122517
{yygotominor.yy328 = TK_ALL;}
122378122518
break;
122379122519
case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
122380122520
{
122381122521
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 */
122382122546
}
122383122547
break;
122384122548
case 120: /* values ::= VALUES LP nexprlist RP */
122385122549
{
122386122550
yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
@@ -123868,10 +124032,11 @@
123868124032
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
123869124033
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
123870124034
};
123871124035
#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
123872124036
#endif
124037
+SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
123873124038
123874124039
123875124040
/*
123876124041
** Return the length of the token that begins at z[0].
123877124042
** Store the token type in *tokenType before returning.
@@ -125138,10 +125303,15 @@
125138125303
sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
125139125304
sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
125140125305
break;
125141125306
}
125142125307
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
+ */
125143125313
case SQLITE_CONFIG_URI: {
125144125314
sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
125145125315
break;
125146125316
}
125147125317
@@ -126875,11 +127045,11 @@
126875127045
int nUri = sqlite3Strlen30(zUri);
126876127046
126877127047
assert( *pzErrMsg==0 );
126878127048
126879127049
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 */
126881127051
){
126882127052
char *zOpt;
126883127053
int eState; /* Parser state when parsing URI */
126884127054
int iIn; /* Input character index */
126885127055
int iOut = 0; /* Output character index */
@@ -127105,11 +127275,13 @@
127105127275
assert( SQLITE_OPEN_READWRITE == 0x02 );
127106127276
assert( SQLITE_OPEN_CREATE == 0x04 );
127107127277
testcase( (1<<(flags&7))==0x02 ); /* READONLY */
127108127278
testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
127109127279
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
+ }
127111127283
127112127284
if( sqlite3GlobalConfig.bCoreMutex==0 ){
127113127285
isThreadsafe = 0;
127114127286
}else if( flags & SQLITE_OPEN_NOMUTEX ){
127115127287
isThreadsafe = 0;
@@ -127989,26 +128161,10 @@
127989128161
case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
127990128162
sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
127991128163
break;
127992128164
}
127993128165
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
-
128010128166
/* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
128011128167
**
128012128168
** Set or clear a flag that indicates that the database file is always well-
128013128169
** formed and never corrupt. This flag is clear by default, indicating that
128014128170
** database files might have arbitrary corruption. Setting the flag during
128015128171
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -231,11 +231,11 @@
231 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232 ** [sqlite_version()] and [sqlite_source_id()].
233 */
234 #define SQLITE_VERSION "3.8.7"
235 #define SQLITE_VERSION_NUMBER 3008007
236 #define SQLITE_SOURCE_ID "2014-09-20 00:35:05 59e2c9df02d7e988c5ad44c560ead1e5288b12e7"
237
238 /*
239 ** CAPI3REF: Run-Time Library Version Numbers
240 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
241 **
@@ -2791,13 +2791,13 @@
2791 ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
2792 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2793 ** an English language description of the error following a failure of any
2794 ** of the sqlite3_open() routines.
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.
2799 **
2800 ** Whether or not an error occurs when it is opened, resources
2801 ** associated with the [database connection] handle should be released by
2802 ** passing it to [sqlite3_close()] when it is no longer required.
2803 **
@@ -2881,17 +2881,18 @@
2881 ** ^SQLite uses the path component of the URI as the name of the disk file
2882 ** which contains the database. ^If the path begins with a '/' character,
2883 ** then it is interpreted as an absolute path. ^If the path does not begin
2884 ** with a '/' (meaning that the authority section is omitted from the URI)
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:").
2888 **
2889 ** [[core URI query parameters]]
2890 ** The query component of a URI may contain parameters that are interpreted
2891 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2892 ** SQLite interprets the following three query parameters:
 
2893 **
2894 ** <ul>
2895 ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2896 ** a VFS object that provides the operating system interface that should
2897 ** be used to access the database file on disk. ^If this option is set to
@@ -2922,15 +2923,13 @@
2922 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2923 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2924 ** a URI filename, its value overrides any behavior requested by setting
2925 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2926 **
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
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.
2932 **
2933 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2934 ** which if set disables file locking in rollback journal modes. This
2935 ** is useful for accessing a database on a filesystem that does not
2936 ** support locking. Caution: Database corruption might result if two
@@ -3521,15 +3520,14 @@
3521 ** terminated. If any NUL characters occur at byte offsets less than
3522 ** the value of the fourth parameter then the resulting string value will
3523 ** contain embedded NULs. The result of expressions involving strings
3524 ** with embedded NULs is undefined.
3525 **
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
3528 ** 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.
3531 ** ^If the fifth argument is
3532 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3533 ** information is in static, unmanaged space and does not need to be freed.
3534 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3535 ** SQLite makes its own private copy of the data immediately, before
@@ -3536,11 +3534,11 @@
3536 ** the sqlite3_bind_*() routine returns.
3537 **
3538 ** ^The sixth argument to sqlite3_bind_text64() must be one of
3539 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
3540 ** 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
3542 ** allowed values shown above, or if the text encoding is different
3543 ** from the encoding specified by the sixth parameter, then the behavior
3544 ** is undefined.
3545 **
3546 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4572,11 +4570,11 @@
4572 **
4573 ** ^The sqlite3_result_null() interface sets the return value
4574 ** of the application-defined function to be NULL.
4575 **
4576 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4577 ** sqlite3_result_text16le(), and sqlite3_result_text16be()
4578 ** set the return value of the application-defined function to be
4579 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4580 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4581 ** ^The sqlite3_result_text64() interface sets the return value of an
4582 ** application-defined function to be a text string in an encoding
@@ -6332,11 +6330,11 @@
6332 #define SQLITE_TESTCTRL_RESERVE 14
6333 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6334 #define SQLITE_TESTCTRL_ISKEYWORD 16
6335 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6336 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6337 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6338 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6339 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6340 #define SQLITE_TESTCTRL_BYTEORDER 22
6341 #define SQLITE_TESTCTRL_ISINIT 23
6342 #define SQLITE_TESTCTRL_SORTER_MMAP 24
@@ -8519,10 +8517,15 @@
8519 ** Macros to compute minimum and maximum of two numbers.
8520 */
8521 #define MIN(A,B) ((A)<(B)?(A):(B))
8522 #define MAX(A,B) ((A)>(B)?(A):(B))
8523
 
 
 
 
 
8524 /*
8525 ** Check to see if this machine uses EBCDIC. (Yes, believe it or
8526 ** not, there are still machines out there that use EBCDIC.)
8527 */
8528 #if 'A' == '\301'
@@ -8756,10 +8759,20 @@
8756 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8757 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8758 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8759 #endif
8760
 
 
 
 
 
 
 
 
 
 
8761 /*
8762 ** An instance of the following structure is used to store the busy-handler
8763 ** callback for a given sqlite handle.
8764 **
8765 ** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8895,10 +8908,11 @@
8895 typedef struct SrcList SrcList;
8896 typedef struct StrAccum StrAccum;
8897 typedef struct Table Table;
8898 typedef struct TableLock TableLock;
8899 typedef struct Token Token;
 
8900 typedef struct Trigger Trigger;
8901 typedef struct TriggerPrg TriggerPrg;
8902 typedef struct TriggerStep TriggerStep;
8903 typedef struct UnpackedRecord UnpackedRecord;
8904 typedef struct VTable VTable;
@@ -11738,11 +11752,11 @@
11738 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
11739 #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
11740 #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
11741 #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
11742 #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 */
11744 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11745 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11746 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11747 #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
11748 #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */
@@ -11823,10 +11837,13 @@
11823 struct Select {
11824 ExprList *pEList; /* The fields of the result */
11825 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11826 u16 selFlags; /* Various SF_* values */
11827 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
 
 
 
11828 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
11829 u64 nSelectRow; /* Estimated number of result rows */
11830 SrcList *pSrc; /* The FROM clause */
11831 Expr *pWhere; /* The WHERE clause */
11832 ExprList *pGroupBy; /* The GROUP BY clause */
@@ -12081,10 +12098,14 @@
12081 yDbMask cookieMask; /* Bitmask of schema verified databases */
12082 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
12083 int regRowid; /* Register holding rowid of CREATE TABLE entry */
12084 int regRoot; /* Register holding root page number for new objects */
12085 int nMaxArg; /* Max args passed to user function by sub-program */
 
 
 
 
12086 #ifndef SQLITE_OMIT_SHARED_CACHE
12087 int nTableLock; /* Number of locks in aTableLock */
12088 TableLock *aTableLock; /* Required table locks for shared-cache mode */
12089 #endif
12090 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
@@ -12160,10 +12181,11 @@
12160
12161 /*
12162 ** Bitfield flags for P5 value in various opcodes.
12163 */
12164 #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
 
12165 #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
12166 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
12167 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
12168 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
12169 #define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */
@@ -12428,10 +12450,21 @@
12428 Select *pSelect; /* The definition of this CTE */
12429 const char *zErr; /* Error message for circular references */
12430 } a[1];
12431 };
12432
 
 
 
 
 
 
 
 
 
 
 
12433 /*
12434 ** Assuming zIn points to the first byte of a UTF-8 character,
12435 ** advance zIn to point to the first byte of the next UTF-8 character.
12436 */
12437 #define SQLITE_SKIP_UTF8(zIn) { \
@@ -12493,10 +12526,11 @@
12493 # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
12494 # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
12495 # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
12496 # define sqlite3Tolower(x) tolower((unsigned char)(x))
12497 #endif
 
12498
12499 /*
12500 ** Internal function prototypes
12501 */
12502 #define sqlite3StrICmp sqlite3_stricmp
@@ -12591,29 +12625,18 @@
12591 #endif
12592 #if defined(SQLITE_TEST)
12593 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
12594 #endif
12595
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
12615 #endif
12616
12617
12618 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
12619 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
@@ -12791,11 +12814,11 @@
12791 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
12792 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12793 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12794 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12795 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12796 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
12797 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12798 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12799 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12800 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12801 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -12815,10 +12838,15 @@
12815 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
12816 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
12817 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
12818 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
12819 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 
 
 
 
 
12820 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
12821 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
12822 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
12823 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
12824 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
@@ -13439,10 +13467,17 @@
13439 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
13440 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
13441 };
13442 #endif
13443
 
 
 
 
 
 
 
13444 #ifndef SQLITE_USE_URI
13445 # define SQLITE_USE_URI 0
13446 #endif
13447
13448 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
@@ -13945,11 +13980,11 @@
13945
13946 /* Since ArraySize(azCompileOpt) is normally in single digits, a
13947 ** linear search is adequate. No need for a binary search. */
13948 for(i=0; i<ArraySize(azCompileOpt); i++){
13949 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13950 && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13951 ){
13952 return 1;
13953 }
13954 }
13955 return 0;
@@ -14259,11 +14294,10 @@
14259 */
14260 struct sqlite3_context {
14261 Mem *pOut; /* The return value is stored here */
14262 FuncDef *pFunc; /* Pointer to function information */
14263 Mem *pMem; /* Memory cell used to store aggregate context */
14264 CollSeq *pColl; /* Collating sequence */
14265 Vdbe *pVdbe; /* The VM that owns this context */
14266 int iOp; /* Instruction number of OP_Function */
14267 int isError; /* Error code returned by the function. */
14268 u8 skipFlag; /* Skip accumulator loading if true */
14269 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
@@ -14348,14 +14382,10 @@
14348 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
14349 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
14350 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
14351 char *zSql; /* Text of the SQL statement that generated this */
14352 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 VdbeFrame *pFrame; /* Parent frame */
14358 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
14359 int nFrame; /* Number of frames in pFrame list */
14360 u32 expmask; /* Binding to these vars invalidates VM */
14361 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
@@ -14675,11 +14705,11 @@
14675 sqlite3VdbeClearObject(db, pVdbe);
14676 sqlite3DbFree(db, pVdbe);
14677 }
14678 db->pnBytesFreed = 0;
14679
14680 *pHighwater = 0;
14681 *pCurrent = nByte;
14682
14683 break;
14684 }
14685
@@ -14700,21 +14730,23 @@
14700 if( db->aDb[i].pBt ){
14701 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
14702 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
14703 }
14704 }
14705 *pHighwater = 0;
 
 
14706 *pCurrent = nRet;
14707 break;
14708 }
14709
14710 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
14711 ** key constraints. Set *pCurrent to zero if all foreign key constraints
14712 ** have been satisfied. The *pHighwater is always set to zero.
14713 */
14714 case SQLITE_DBSTATUS_DEFERRED_FKS: {
14715 *pHighwater = 0;
14716 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
14717 break;
14718 }
14719
14720 default: {
@@ -20195,11 +20227,11 @@
20195 mallocWithAlarm((int)n, &p);
20196 sqlite3_mutex_leave(mem0.mutex);
20197 }else{
20198 p = sqlite3GlobalConfig.m.xMalloc((int)n);
20199 }
20200 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
20201 return p;
20202 }
20203
20204 /*
20205 ** This version of the memory allocation is for use by the application.
@@ -20418,14 +20450,14 @@
20418 */
20419 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
20420 int nOld, nNew, nDiff;
20421 void *pNew;
20422 if( pOld==0 ){
20423 return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
20424 }
20425 if( nBytes==0 ){
20426 sqlite3_free(pOld); /* IMP: R-31593-10574 */
20427 return 0;
20428 }
20429 if( nBytes>=0x7fffff00 ){
20430 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
20431 return 0;
@@ -20458,11 +20490,11 @@
20458 }
20459 sqlite3_mutex_leave(mem0.mutex);
20460 }else{
20461 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
20462 }
20463 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
20464 return pNew;
20465 }
20466
20467 /*
20468 ** The public interface to sqlite3Realloc. Make sure that the memory
@@ -20470,11 +20502,11 @@
20470 */
20471 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
20472 #ifndef SQLITE_OMIT_AUTOINIT
20473 if( sqlite3_initialize() ) return 0;
20474 #endif
20475 if( n<0 ) n = 0;
20476 return sqlite3Realloc(pOld, n);
20477 }
20478 SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
20479 #ifndef SQLITE_OMIT_AUTOINIT
20480 if( sqlite3_initialize() ) return 0;
@@ -21757,10 +21789,73 @@
21757 fprintf(stdout,"%s", zBuf);
21758 fflush(stdout);
21759 }
21760 #endif
21761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21762 /*
21763 ** variable-argument wrapper around sqlite3VXPrintf().
21764 */
21765 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
21766 va_list ap;
@@ -29659,11 +29754,11 @@
29659 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
29660 **
29661 ** * An I/O method finder function called FINDER that returns a pointer
29662 ** to the METHOD object in the previous bullet.
29663 */
29664 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
29665 static const sqlite3_io_methods METHOD = { \
29666 VERSION, /* iVersion */ \
29667 CLOSE, /* xClose */ \
29668 unixRead, /* xRead */ \
29669 unixWrite, /* xWrite */ \
@@ -29674,11 +29769,11 @@
29674 UNLOCK, /* xUnlock */ \
29675 CKLOCK, /* xCheckReservedLock */ \
29676 unixFileControl, /* xFileControl */ \
29677 unixSectorSize, /* xSectorSize */ \
29678 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
29679 unixShmMap, /* xShmMap */ \
29680 unixShmLock, /* xShmLock */ \
29681 unixShmBarrier, /* xShmBarrier */ \
29682 unixShmUnmap, /* xShmUnmap */ \
29683 unixFetch, /* xFetch */ \
29684 unixUnfetch, /* xUnfetch */ \
@@ -29700,29 +29795,32 @@
29700 posixIoMethods, /* sqlite3_io_methods object name */
29701 3, /* shared memory and mmap are enabled */
29702 unixClose, /* xClose method */
29703 unixLock, /* xLock method */
29704 unixUnlock, /* xUnlock method */
29705 unixCheckReservedLock /* xCheckReservedLock method */
 
29706 )
29707 IOMETHODS(
29708 nolockIoFinder, /* Finder function name */
29709 nolockIoMethods, /* sqlite3_io_methods object name */
29710 3, /* shared memory is disabled */
29711 nolockClose, /* xClose method */
29712 nolockLock, /* xLock method */
29713 nolockUnlock, /* xUnlock method */
29714 nolockCheckReservedLock /* xCheckReservedLock method */
 
29715 )
29716 IOMETHODS(
29717 dotlockIoFinder, /* Finder function name */
29718 dotlockIoMethods, /* sqlite3_io_methods object name */
29719 1, /* shared memory is disabled */
29720 dotlockClose, /* xClose method */
29721 dotlockLock, /* xLock method */
29722 dotlockUnlock, /* xUnlock method */
29723 dotlockCheckReservedLock /* xCheckReservedLock method */
 
29724 )
29725
29726 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
29727 IOMETHODS(
29728 flockIoFinder, /* Finder function name */
@@ -29729,11 +29827,12 @@
29729 flockIoMethods, /* sqlite3_io_methods object name */
29730 1, /* shared memory is disabled */
29731 flockClose, /* xClose method */
29732 flockLock, /* xLock method */
29733 flockUnlock, /* xUnlock method */
29734 flockCheckReservedLock /* xCheckReservedLock method */
 
29735 )
29736 #endif
29737
29738 #if OS_VXWORKS
29739 IOMETHODS(
@@ -29741,11 +29840,12 @@
29741 semIoMethods, /* sqlite3_io_methods object name */
29742 1, /* shared memory is disabled */
29743 semClose, /* xClose method */
29744 semLock, /* xLock method */
29745 semUnlock, /* xUnlock method */
29746 semCheckReservedLock /* xCheckReservedLock method */
 
29747 )
29748 #endif
29749
29750 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29751 IOMETHODS(
@@ -29753,11 +29853,12 @@
29753 afpIoMethods, /* sqlite3_io_methods object name */
29754 1, /* shared memory is disabled */
29755 afpClose, /* xClose method */
29756 afpLock, /* xLock method */
29757 afpUnlock, /* xUnlock method */
29758 afpCheckReservedLock /* xCheckReservedLock method */
 
29759 )
29760 #endif
29761
29762 /*
29763 ** The proxy locking method is a "super-method" in the sense that it
@@ -29778,11 +29879,12 @@
29778 proxyIoMethods, /* sqlite3_io_methods object name */
29779 1, /* shared memory is disabled */
29780 proxyClose, /* xClose method */
29781 proxyLock, /* xLock method */
29782 proxyUnlock, /* xUnlock method */
29783 proxyCheckReservedLock /* xCheckReservedLock method */
 
29784 )
29785 #endif
29786
29787 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
29788 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29791,11 +29893,12 @@
29791 nfsIoMethods, /* sqlite3_io_methods object name */
29792 1, /* shared memory is disabled */
29793 unixClose, /* xClose method */
29794 unixLock, /* xLock method */
29795 nfsUnlock, /* xUnlock method */
29796 unixCheckReservedLock /* xCheckReservedLock method */
 
29797 )
29798 #endif
29799
29800 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29801 /*
@@ -51383,13 +51486,14 @@
51383 ** stored in MemPage.pBt->mutex.
51384 */
51385 struct MemPage {
51386 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
51387 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 */
 
51391 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
51392 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
51393 u8 max1bytePayload; /* min(maxLocal,127) */
51394 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
51395 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
@@ -51566,16 +51670,14 @@
51566 ** about a cell. The parseCellPtr() function fills in this structure
51567 ** based on information extract from the raw disk page.
51568 */
51569 typedef struct CellInfo CellInfo;
51570 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 */
51577 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
51578 u16 nSize; /* Size of the cell content on the main b-tree page */
51579 };
51580
51581 /*
@@ -51768,10 +51870,12 @@
51768 u8 *aPgRef; /* 1 bit per page in the db (see above) */
51769 Pgno nPage; /* Number of pages in the database */
51770 int mxErr; /* Stop accumulating errors when this reaches zero */
51771 int nErr; /* Number of messages written to zErrMsg so far */
51772 int mallocFailed; /* A memory allocation error has occurred */
 
 
51773 StrAccum errMsg; /* Accumulate the error message text here */
51774 };
51775
51776 /*
51777 ** Routines to read or write a two- and four-byte big-endian integer values.
@@ -52554,11 +52658,13 @@
52554 ){
52555 BtCursor *p;
52556 BtShared *pBt = pBtree->pBt;
52557 assert( sqlite3BtreeHoldsMutex(pBtree) );
52558 for(p=pBt->pCursor; p; p=p->pNext){
52559 if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){
 
 
52560 p->eState = CURSOR_INVALID;
52561 }
52562 }
52563 }
52564
@@ -52727,13 +52833,13 @@
52727 ** the cursors if and when a cursor is found that actually requires saving.
52728 ** The common case is that no cursors need to be saved, so this routine is
52729 ** broken out from its caller to avoid unnecessary stack pointer movement.
52730 */
52731 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 */
52735 ){
52736 do{
52737 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
52738 if( p->eState==CURSOR_VALID ){
52739 int rc = saveCursorPosition(p);
@@ -53035,51 +53141,48 @@
53035 /*
53036 ** Parse a cell content block and fill in the CellInfo structure. There
53037 ** are two versions of this function. btreeParseCell() takes a
53038 ** cell index as the second argument and btreeParseCellPtr()
53039 ** 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 */
53044 static void btreeParseCellPtr(
53045 MemPage *pPage, /* Page containing the cell */
53046 u8 *pCell, /* Pointer to the cell text. */
53047 CellInfo *pInfo /* Fill in this structure */
53048 ){
53049 u16 n; /* Number bytes in cell content header */
53050 u32 nPayload; /* Number of bytes of cell payload */
53051
53052 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53053
53054 pInfo->pCell = pCell;
53055 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);
 
53070 pInfo->nKey = nPayload;
53071 }
53072 pInfo->nPayload = nPayload;
53073 pInfo->nHeader = n;
53074 testcase( nPayload==pPage->maxLocal );
53075 testcase( nPayload==pPage->maxLocal+1 );
53076 if( likely(nPayload<=pPage->maxLocal) ){
53077 /* This is the (easy) common case where the entire payload fits
53078 ** on the local page. No overflow is required.
53079 */
53080 if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
 
53081 pInfo->nLocal = (u16)nPayload;
53082 pInfo->iOverflow = 0;
53083 }else{
53084 /* If the payload will not fit completely on the local page, we have
53085 ** to decide how much to store locally and how much to spill onto
@@ -53102,33 +53205,32 @@
53102 if( surplus <= maxLocal ){
53103 pInfo->nLocal = (u16)surplus;
53104 }else{
53105 pInfo->nLocal = (u16)minLocal;
53106 }
53107 pInfo->iOverflow = (u16)(pInfo->nLocal + n);
53108 pInfo->nSize = pInfo->iOverflow + 4;
53109 }
53110 }
53111 #define parseCell(pPage, iCell, pInfo) \
53112 btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
53113 static void btreeParseCell(
53114 MemPage *pPage, /* Page containing the cell */
53115 int iCell, /* The cell index. First cell is 0 */
53116 CellInfo *pInfo /* Fill in this structure */
53117 ){
53118 parseCell(pPage, iCell, pInfo);
53119 }
53120
53121 /*
53122 ** Compute the total number of bytes that a Cell needs in the cell
53123 ** data area of the btree-page. The return number includes the cell
53124 ** data header and the local payload, but not any overflow page or
53125 ** the space used by the cell pointer.
53126 */
53127 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
53128 u8 *pIter = &pCell[pPage->childPtrSize];
53129 u32 nSize;
 
53130
53131 #ifdef SQLITE_DEBUG
53132 /* The value returned by this function should always be the same as
53133 ** the (CellInfo.nSize) value found by doing a full parse of the
53134 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
@@ -53135,47 +53237,48 @@
53135 ** this function verifies that this invariant is not violated. */
53136 CellInfo debuginfo;
53137 btreeParseCellPtr(pPage, pCell, &debuginfo);
53138 #endif
53139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53140 if( pPage->intKey ){
53141 u8 *pEnd;
53142 if( pPage->hasData ){
53143 pIter += getVarint32(pIter, nSize);
53144 }else{
53145 nSize = 0;
53146 }
53147
53148 /* pIter now points at the 64-bit integer key value, a variable length
53149 ** integer. The following block moves pIter to point at the first byte
53150 ** past the end of the key value. */
53151 pEnd = &pIter[9];
53152 while( (*pIter++)&0x80 && pIter<pEnd );
53153 }else{
53154 pIter += getVarint32(pIter, nSize);
53155 }
53156
53157 testcase( nSize==pPage->maxLocal );
53158 testcase( nSize==pPage->maxLocal+1 );
53159 if( nSize>pPage->maxLocal ){
 
 
 
53160 int minLocal = pPage->minLocal;
53161 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
53162 testcase( nSize==pPage->maxLocal );
53163 testcase( nSize==pPage->maxLocal+1 );
53164 if( nSize>pPage->maxLocal ){
53165 nSize = minLocal;
53166 }
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 );
53177 return (u16)nSize;
53178 }
53179
53180 #ifdef SQLITE_DEBUG
53181 /* This variation on cellSizePtr() is used inside of assert() statements
@@ -53194,11 +53297,10 @@
53194 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
53195 CellInfo info;
53196 if( *pRC ) return;
53197 assert( pCell!=0 );
53198 btreeParseCellPtr(pPage, pCell, &info);
53199 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
53200 if( info.iOverflow ){
53201 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
53202 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
53203 }
53204 }
@@ -53407,11 +53509,11 @@
53407 ** does it detect cells or freeblocks that encrouch into the reserved bytes
53408 ** at the end of the page. So do additional corruption checks inside this
53409 ** routine and return SQLITE_CORRUPT if any problems are found.
53410 */
53411 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
53412 u16 iPtr; /* Address of pointer to next freeblock */
53413 u16 iFreeBlk; /* Address of the next freeblock */
53414 u8 hdr; /* Page header size. 0 or 100 */
53415 u8 nFrag = 0; /* Reduction in fragmentation */
53416 u16 iOrigSize = iSize; /* Original value of iSize */
53417 u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
@@ -53459,13 +53561,13 @@
53459 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
53460 iSize = iEnd - iStart;
53461 iFreeBlk = get2byte(&data[iFreeBlk]);
53462 }
53463
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.
53467 */
53468 if( iPtr>hdr+1 ){
53469 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
53470 if( iPtrEnd+3>=iStart ){
53471 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
@@ -53515,16 +53617,18 @@
53515 flagByte &= ~PTF_LEAF;
53516 pPage->childPtrSize = 4-4*pPage->leaf;
53517 pBt = pPage->pBt;
53518 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
53519 pPage->intKey = 1;
53520 pPage->hasData = pPage->leaf;
 
53521 pPage->maxLocal = pBt->maxLeaf;
53522 pPage->minLocal = pBt->minLeaf;
53523 }else if( flagByte==PTF_ZERODATA ){
53524 pPage->intKey = 0;
53525 pPage->hasData = 0;
 
53526 pPage->maxLocal = pBt->maxLocal;
53527 pPage->minLocal = pBt->minLocal;
53528 }else{
53529 return SQLITE_CORRUPT_BKPT;
53530 }
@@ -54694,15 +54798,15 @@
54694 */
54695 static void unlockBtreeIfUnused(BtShared *pBt){
54696 assert( sqlite3_mutex_held(pBt->mutex) );
54697 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
54698 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
54699 assert( pBt->pPage1->aData );
 
54700 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
54701 assert( pBt->pPage1->aData );
54702 releasePage(pBt->pPage1);
54703 pBt->pPage1 = 0;
 
54704 }
54705 }
54706
54707 /*
54708 ** If pBt points to an empty file then convert that empty file
@@ -55739,10 +55843,14 @@
55739 assert( pBt->pPage1 && pBt->pPage1->aData );
55740
55741 if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
55742 return SQLITE_READONLY;
55743 }
 
 
 
 
55744 if( iTable==1 && btreePagecount(pBt)==0 ){
55745 assert( wrFlag==0 );
55746 iTable = 0;
55747 }
55748
@@ -55928,12 +56036,13 @@
55928 ** to return an integer result code for historical reasons.
55929 */
55930 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
55931 assert( cursorHoldsMutex(pCur) );
55932 assert( pCur->eState==CURSOR_VALID );
 
55933 getCellInfo(pCur);
55934 *pSize = pCur->info.nData;
55935 return SQLITE_OK;
55936 }
55937
55938 /*
55939 ** Given the page number of an overflow page in the database (parameter
@@ -56080,34 +56189,32 @@
56080 unsigned char *pBuf, /* Write the bytes into this buffer */
56081 int eOp /* zero to read. non-zero to write. */
56082 ){
56083 unsigned char *aPayload;
56084 int rc = SQLITE_OK;
56085 u32 nKey;
56086 int iIdx = 0;
56087 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
56088 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
56089 #ifdef SQLITE_DIRECT_OVERFLOW_READ
56090 int bEnd; /* True if reading to end of data */
 
56091 #endif
56092
56093 assert( pPage );
56094 assert( pCur->eState==CURSOR_VALID );
56095 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
56096 assert( cursorHoldsMutex(pCur) );
56097 assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */
56098
56099 getCellInfo(pCur);
56100 aPayload = pCur->info.pCell + pCur->info.nHeader;
56101 nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
56102 #ifdef SQLITE_DIRECT_OVERFLOW_READ
56103 bEnd = (offset+amt==nKey+pCur->info.nData);
56104 #endif
 
56105
56106 if( NEVER(offset+amt > nKey+pCur->info.nData)
56107 || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
56108 ){
56109 /* Trying to read or write past the end of the data is an error */
56110 return SQLITE_CORRUPT_BKPT;
56111 }
56112
56113 /* Check if data must be read/written to/from the btree page itself. */
@@ -56159,11 +56266,13 @@
56159
56160 /* If the overflow page-list cache has been allocated and the
56161 ** entry for the first required overflow page is valid, skip
56162 ** directly to it.
56163 */
56164 if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
 
 
56165 iIdx = (offset/ovflSize);
56166 nextPage = pCur->aOverflow[iIdx];
56167 offset = (offset%ovflSize);
56168 }
56169
@@ -56212,10 +56321,11 @@
56212 ** 2) data is required from the start of this overflow page, and
56213 ** 3) the database is file-backed, and
56214 ** 4) there is no open write-transaction, and
56215 ** 5) the database is not a WAL database,
56216 ** 6) all data from the page is being read.
 
56217 **
56218 ** then data can be read directly from the database file into the
56219 ** output buffer, bypassing the page-cache altogether. This speeds
56220 ** up loading large records that span many overflow pages.
56221 */
@@ -56223,13 +56333,15 @@
56223 && offset==0 /* (2) */
56224 && (bEnd || a==ovflSize) /* (6) */
56225 && pBt->inTransaction==TRANS_READ /* (4) */
56226 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
56227 && pBt->pPage1->aData[19]==0x01 /* (5) */
 
56228 ){
56229 u8 aSave[4];
56230 u8 *aWrite = &pBuf[-4];
 
56231 memcpy(aSave, aWrite, 4);
56232 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
56233 nextPage = get4byte(aWrite);
56234 memcpy(aWrite, aSave, 4);
56235 }else
@@ -56337,11 +56449,11 @@
56337 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
56338 assert( cursorHoldsMutex(pCur) );
56339 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
56340 assert( pCur->info.nSize>0 );
56341 *pAmt = pCur->info.nLocal;
56342 return (void*)(pCur->info.pCell + pCur->info.nHeader);
56343 }
56344
56345
56346 /*
56347 ** For the entry that cursor pCur is point to, return as
@@ -56765,11 +56877,11 @@
56765 pCur->aiIdx[pCur->iPage] = (u16)idx;
56766 if( xRecordCompare==0 ){
56767 for(;;){
56768 i64 nCellKey;
56769 pCell = findCell(pPage, idx) + pPage->childPtrSize;
56770 if( pPage->hasData ){
56771 while( 0x80 <= *(pCell++) ){
56772 if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
56773 }
56774 }
56775 getVarint(pCell, (u64*)&nCellKey);
@@ -57024,13 +57136,13 @@
57024 ** was already pointing to the first entry in the database before
57025 ** this routine was called, then set *pRes=1.
57026 **
57027 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
57028 ** 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.
57032 **
57033 ** The calling function will set *pRes to 0 or 1. The initial *pRes value
57034 ** will be 1 if the cursor being stepped corresponds to an SQL index and
57035 ** if this routine could have been skipped if that SQL index had been
57036 ** a unique index. Otherwise the caller will have set *pRes to zero.
@@ -57048,12 +57160,11 @@
57048 assert( *pRes==0 );
57049 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
57050 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
57051 assert( pCur->info.nSize==0 );
57052 if( pCur->eState!=CURSOR_VALID ){
57053 assert( pCur->eState>=CURSOR_REQUIRESEEK );
57054 rc = btreeRestoreCursorPosition(pCur);
57055 if( rc!=SQLITE_OK ){
57056 return rc;
57057 }
57058 if( CURSOR_INVALID==pCur->eState ){
57059 *pRes = 1;
@@ -57354,11 +57465,11 @@
57354 if( rc ) goto end_allocate_page;
57355 if( closest<k-1 ){
57356 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
57357 }
57358 put4byte(&aData[4], k-1);
57359 noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
57360 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
57361 if( rc==SQLITE_OK ){
57362 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
57363 if( rc!=SQLITE_OK ){
57364 releasePage(*ppPage);
@@ -57387,11 +57498,11 @@
57387 ** content for any page that really does lie past the end of the database
57388 ** file on disk. So the effects of disabling the no-content optimization
57389 ** here are confined to those pages that lie between the end of the
57390 ** database image and the end of the database file.
57391 */
57392 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
57393
57394 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
57395 if( rc ) return rc;
57396 pBt->nPage++;
57397 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -57586,22 +57697,29 @@
57586 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
57587 }
57588 }
57589
57590 /*
57591 ** Free any overflow pages associated with the given Cell.
 
 
57592 */
57593 static int clearCell(MemPage *pPage, unsigned char *pCell){
 
 
 
 
57594 BtShared *pBt = pPage->pBt;
57595 CellInfo info;
57596 Pgno ovflPgno;
57597 int rc;
57598 int nOvfl;
57599 u32 ovflPageSize;
57600
57601 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57602 btreeParseCellPtr(pPage, pCell, &info);
 
57603 if( info.iOverflow==0 ){
57604 return SQLITE_OK; /* No overflow pages. Return without doing anything */
57605 }
57606 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
57607 return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
@@ -57681,54 +57799,87 @@
57681 unsigned char *pPrior;
57682 unsigned char *pPayload;
57683 BtShared *pBt = pPage->pBt;
57684 Pgno pgnoOvfl = 0;
57685 int nHeader;
57686 CellInfo info;
57687
57688 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57689
57690 /* pPage is not necessarily writeable since pCell might be auxiliary
57691 ** buffer space that is separate from the pPage buffer area */
57692 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
57693 || sqlite3PagerIswriteable(pPage->pDbPage) );
57694
57695 /* 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);
57702 }else{
57703 nData = nZero = 0;
 
57704 }
57705 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
57711 /* Fill in the payload */
57712 nPayload = nData + nZero;
57713 if( pPage->intKey ){
57714 pSrc = pData;
57715 nSrc = nData;
57716 nData = 0;
57717 }else{
57718 if( NEVER(nKey>0x7fffffff || pKey==0) ){
57719 return SQLITE_CORRUPT_BKPT;
57720 }
57721 nPayload += (int)nKey;
57722 pSrc = pKey;
57723 nSrc = (int)nKey;
57724 }
57725 *pnSize = info.nSize;
57726 spaceLeft = info.nLocal;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57727 pPayload = &pCell[nHeader];
57728 pPrior = &pCell[info.iOverflow];
57729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57730 while( nPayload>0 ){
57731 if( spaceLeft==0 ){
57732 #ifndef SQLITE_OMIT_AUTOVACUUM
57733 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
57734 if( pBt->autoVacuum ){
@@ -58432,11 +58583,11 @@
58432 **
58433 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
58434 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
58435 */
58436 leafCorrection = apOld[0]->leaf*4;
58437 leafData = apOld[0]->hasData;
58438 for(i=0; i<nOld; i++){
58439 int limit;
58440
58441 /* Before doing anything else, take a copy of the i'th original sibling
58442 ** The rest of this function will use data from the copies rather
@@ -59008,11 +59159,11 @@
59008 int const iIdx = pCur->aiIdx[iPage-1];
59009
59010 rc = sqlite3PagerWrite(pParent->pDbPage);
59011 if( rc==SQLITE_OK ){
59012 #ifndef SQLITE_OMIT_QUICKBALANCE
59013 if( pPage->hasData
59014 && pPage->nOverflow==1
59015 && pPage->aiOvfl[0]==pPage->nCell
59016 && pParent->pgno!=1
59017 && pParent->nCell==iIdx
59018 ){
@@ -59127,11 +59278,12 @@
59127 assert( pCur->skipNext!=SQLITE_OK );
59128 return pCur->skipNext;
59129 }
59130
59131 assert( cursorHoldsMutex(pCur) );
59132 assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE
 
59133 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
59134 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
59135
59136 /* Assert that the caller has been consistent. If this cursor was opened
59137 ** expecting an index b-tree, then the caller should be inserting blob
@@ -59160,11 +59312,12 @@
59160 invalidateIncrblobCursors(p, nKey, 0);
59161
59162 /* If the cursor is currently on the last row and we are appending a
59163 ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
59164 ** call */
59165 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){
 
59166 loc = -1;
59167 }
59168 }
59169
59170 if( !loc ){
@@ -59179,13 +59332,12 @@
59179
59180 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
59181 pCur->pgnoRoot, nKey, nData, pPage->pgno,
59182 loc==0 ? "overwrite" : "new entry"));
59183 assert( pPage->isInit );
59184 allocateTempSpace(pBt);
59185 newCell = pBt->pTmpSpace;
59186 if( newCell==0 ) return SQLITE_NOMEM;
59187 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
59188 if( rc ) goto end_insert;
59189 assert( szNew==cellSizePtr(pPage, newCell) );
59190 assert( szNew <= MX_CELL_SIZE(pBt) );
59191 idx = pCur->aiIdx[pCur->iPage];
@@ -59198,12 +59350,11 @@
59198 }
59199 oldCell = findCell(pPage, idx);
59200 if( !pPage->leaf ){
59201 memcpy(newCell, oldCell, 4);
59202 }
59203 szOld = cellSizePtr(pPage, oldCell);
59204 rc = clearCell(pPage, oldCell);
59205 dropCell(pPage, idx, szOld, &rc);
59206 if( rc ) goto end_insert;
59207 }else if( loc<0 && pPage->nCell>0 ){
59208 assert( pPage->leaf );
59209 idx = ++pCur->aiIdx[pCur->iPage];
@@ -59261,10 +59412,11 @@
59261 int rc; /* Return code */
59262 MemPage *pPage; /* Page to delete cell from */
59263 unsigned char *pCell; /* Pointer to cell to delete */
59264 int iCellIdx; /* Index of cell to delete */
59265 int iCellDepth; /* Depth of node containing pCell */
 
59266
59267 assert( cursorHoldsMutex(pCur) );
59268 assert( pBt->inTransaction==TRANS_WRITE );
59269 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
59270 assert( pCur->curFlags & BTCF_WriteFlag );
@@ -59309,12 +59461,12 @@
59309 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
59310 }
59311
59312 rc = sqlite3PagerWrite(pPage->pDbPage);
59313 if( rc ) return rc;
59314 rc = clearCell(pPage, pCell);
59315 dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
59316 if( rc ) return rc;
59317
59318 /* If the cell deleted was not located on a leaf page, then the cursor
59319 ** is currently pointing to the largest entry in the sub-tree headed
59320 ** by the child-page of the cell that was just deleted from an internal
@@ -59327,14 +59479,12 @@
59327 unsigned char *pTmp;
59328
59329 pCell = findCell(pLeaf, pLeaf->nCell-1);
59330 nCell = cellSizePtr(pLeaf, pCell);
59331 assert( MX_CELL_SIZE(pBt) >= nCell );
59332
59333 allocateTempSpace(pBt);
59334 pTmp = pBt->pTmpSpace;
59335
59336 rc = sqlite3PagerWrite(pLeaf->pDbPage);
59337 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
59338 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
59339 if( rc ) return rc;
59340 }
@@ -59542,10 +59692,11 @@
59542 MemPage *pPage;
59543 int rc;
59544 unsigned char *pCell;
59545 int i;
59546 int hdr;
 
59547
59548 assert( sqlite3_mutex_held(pBt->mutex) );
59549 if( pgno>btreePagecount(pBt) ){
59550 return SQLITE_CORRUPT_BKPT;
59551 }
@@ -59557,11 +59708,11 @@
59557 pCell = findCell(pPage, i);
59558 if( !pPage->leaf ){
59559 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
59560 if( rc ) goto cleardatabasepage_out;
59561 }
59562 rc = clearCell(pPage, pCell);
59563 if( rc ) goto cleardatabasepage_out;
59564 }
59565 if( !pPage->leaf ){
59566 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
59567 if( rc ) goto cleardatabasepage_out;
@@ -59903,24 +60054,25 @@
59903 /*
59904 ** Append a message to the error message string.
59905 */
59906 static void checkAppendMsg(
59907 IntegrityCk *pCheck,
59908 char *zMsg1,
59909 const char *zFormat,
59910 ...
59911 ){
59912 va_list ap;
 
59913 if( !pCheck->mxErr ) return;
59914 pCheck->mxErr--;
59915 pCheck->nErr++;
59916 va_start(ap, zFormat);
59917 if( pCheck->errMsg.nChar ){
59918 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
59919 }
59920 if( zMsg1 ){
59921 sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
 
59922 }
59923 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
59924 va_end(ap);
59925 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
59926 pCheck->mallocFailed = 1;
@@ -59954,18 +60106,18 @@
59954 ** Return 1 if there are 2 or more references to the page and 0 if
59955 ** if this is the first reference to the page.
59956 **
59957 ** Also check that the page number is in bounds.
59958 */
59959 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
59960 if( iPage==0 ) return 1;
59961 if( iPage>pCheck->nPage ){
59962 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
59963 return 1;
59964 }
59965 if( getPageReferenced(pCheck, iPage) ){
59966 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
59967 return 1;
59968 }
59969 setPageReferenced(pCheck, iPage);
59970 return 0;
59971 }
@@ -59978,26 +60130,25 @@
59978 */
59979 static void checkPtrmap(
59980 IntegrityCk *pCheck, /* Integrity check context */
59981 Pgno iChild, /* Child page number */
59982 u8 eType, /* Expected pointer map type */
59983 Pgno iParent, /* Expected pointer map parent page number */
59984 char *zContext /* Context description (used for error msg) */
59985 ){
59986 int rc;
59987 u8 ePtrmapType;
59988 Pgno iPtrmapParent;
59989
59990 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
59991 if( rc!=SQLITE_OK ){
59992 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
59993 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
59994 return;
59995 }
59996
59997 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
59998 checkAppendMsg(pCheck, zContext,
59999 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
60000 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
60001 }
60002 }
60003 #endif
@@ -60008,51 +60159,50 @@
60008 */
60009 static void checkList(
60010 IntegrityCk *pCheck, /* Integrity checking context */
60011 int isFreeList, /* True for a freelist. False for overflow page list */
60012 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 */
60015 ){
60016 int i;
60017 int expected = N;
60018 int iFirst = iPage;
60019 while( N-- > 0 && pCheck->mxErr ){
60020 DbPage *pOvflPage;
60021 unsigned char *pOvflData;
60022 if( iPage<1 ){
60023 checkAppendMsg(pCheck, zContext,
60024 "%d of %d pages missing from overflow list starting at %d",
60025 N+1, expected, iFirst);
60026 break;
60027 }
60028 if( checkRef(pCheck, iPage, zContext) ) break;
60029 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
60030 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
60031 break;
60032 }
60033 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
60034 if( isFreeList ){
60035 int n = get4byte(&pOvflData[4]);
60036 #ifndef SQLITE_OMIT_AUTOVACUUM
60037 if( pCheck->pBt->autoVacuum ){
60038 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
60039 }
60040 #endif
60041 if( n>(int)pCheck->pBt->usableSize/4-2 ){
60042 checkAppendMsg(pCheck, zContext,
60043 "freelist leaf count too big on page %d", iPage);
60044 N--;
60045 }else{
60046 for(i=0; i<n; i++){
60047 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
60048 #ifndef SQLITE_OMIT_AUTOVACUUM
60049 if( pCheck->pBt->autoVacuum ){
60050 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
60051 }
60052 #endif
60053 checkRef(pCheck, iFreePage, zContext);
60054 }
60055 N -= n;
60056 }
60057 }
60058 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -60061,11 +60211,11 @@
60061 ** page in this overflow list, check that the pointer-map entry for
60062 ** the following page matches iPage.
60063 */
60064 if( pCheck->pBt->autoVacuum && N>0 ){
60065 i = get4byte(pOvflData);
60066 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
60067 }
60068 }
60069 #endif
60070 iPage = get4byte(pOvflData);
60071 sqlite3PagerUnref(pOvflPage);
@@ -60093,11 +60243,10 @@
60093 ** the root of the tree.
60094 */
60095 static int checkTreePage(
60096 IntegrityCk *pCheck, /* Context for the sanity check */
60097 int iPage, /* Page number of the page to check */
60098 char *zParentContext, /* Parent context */
60099 i64 *pnParentMinKey,
60100 i64 *pnParentMaxKey
60101 ){
60102 MemPage *pPage;
60103 int i, rc, depth, d2, pgno, cnt;
@@ -60104,38 +60253,42 @@
60104 int hdr, cellStart;
60105 int nCell;
60106 u8 *data;
60107 BtShared *pBt;
60108 int usableSize;
60109 char zContext[100];
60110 char *hit = 0;
60111 i64 nMinKey = 0;
60112 i64 nMaxKey = 0;
60113
60114 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
 
60115
60116 /* Check that the page exists
60117 */
60118 pBt = pCheck->pBt;
60119 usableSize = pBt->usableSize;
60120 if( iPage==0 ) return 0;
60121 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
 
 
60122 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
60123 checkAppendMsg(pCheck, zContext,
60124 "unable to get the page. error code=%d", rc);
60125 return 0;
 
60126 }
60127
60128 /* Clear MemPage.isInit to make sure the corruption detection code in
60129 ** btreeInitPage() is executed. */
60130 pPage->isInit = 0;
60131 if( (rc = btreeInitPage(pPage))!=0 ){
60132 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
60133 checkAppendMsg(pCheck, zContext,
60134 "btreeInitPage() returns error code %d", rc);
60135 releasePage(pPage);
60136 return 0;
 
60137 }
60138
60139 /* Check out all the cells.
60140 */
60141 depth = 0;
@@ -60144,99 +60297,101 @@
60144 u32 sz;
60145 CellInfo info;
60146
60147 /* Check payload overflow pages
60148 */
60149 sqlite3_snprintf(sizeof(zContext), zContext,
60150 "On tree page %d cell %d: ", iPage, i);
 
60151 pCell = findCell(pPage,i);
60152 btreeParseCellPtr(pPage, pCell, &info);
60153 sz = info.nData;
60154 if( !pPage->intKey ) sz += (int)info.nKey;
60155 /* For intKey pages, check that the keys are in order.
60156 */
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);
 
60162 }
60163 nMaxKey = info.nKey;
60164 }
60165 assert( sz==info.nPayload );
60166 if( (sz>info.nLocal)
60167 && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
60168 ){
60169 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
60170 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
60171 #ifndef SQLITE_OMIT_AUTOVACUUM
60172 if( pBt->autoVacuum ){
60173 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
60174 }
60175 #endif
60176 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
60177 }
60178
60179 /* Check sanity of left child page.
60180 */
60181 if( !pPage->leaf ){
60182 pgno = get4byte(pCell);
60183 #ifndef SQLITE_OMIT_AUTOVACUUM
60184 if( pBt->autoVacuum ){
60185 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
60186 }
60187 #endif
60188 d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
60189 if( i>0 && d2!=depth ){
60190 checkAppendMsg(pCheck, zContext, "Child page depth differs");
60191 }
60192 depth = d2;
60193 }
60194 }
60195
60196 if( !pPage->leaf ){
60197 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
60198 sqlite3_snprintf(sizeof(zContext), zContext,
60199 "On page %d at right child: ", iPage);
60200 #ifndef SQLITE_OMIT_AUTOVACUUM
60201 if( pBt->autoVacuum ){
60202 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
60203 }
60204 #endif
60205 checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
60206 }
60207
60208 /* For intKey leaf pages, check that the min/max keys are in order
60209 ** with any left/parent/right pages.
60210 */
 
 
60211 if( pPage->leaf && pPage->intKey ){
60212 /* if we are a left child page */
60213 if( pnParentMinKey ){
60214 /* if we are the left most child page */
60215 if( !pnParentMaxKey ){
60216 if( nMaxKey > *pnParentMinKey ){
60217 checkAppendMsg(pCheck, zContext,
60218 "Rowid %lld out of order (max larger than parent min of %lld)",
60219 nMaxKey, *pnParentMinKey);
60220 }
60221 }else{
60222 if( nMinKey <= *pnParentMinKey ){
60223 checkAppendMsg(pCheck, zContext,
60224 "Rowid %lld out of order (min less than parent min of %lld)",
60225 nMinKey, *pnParentMinKey);
60226 }
60227 if( nMaxKey > *pnParentMaxKey ){
60228 checkAppendMsg(pCheck, zContext,
60229 "Rowid %lld out of order (max larger than parent max of %lld)",
60230 nMaxKey, *pnParentMaxKey);
60231 }
60232 *pnParentMinKey = nMaxKey;
60233 }
60234 /* else if we're a right child page */
60235 } else if( pnParentMaxKey ){
60236 if( nMinKey <= *pnParentMaxKey ){
60237 checkAppendMsg(pCheck, zContext,
60238 "Rowid %lld out of order (min less than parent max of %lld)",
60239 nMinKey, *pnParentMaxKey);
60240 }
60241 }
60242 }
@@ -60244,10 +60399,11 @@
60244 /* Check for complete coverage of the page
60245 */
60246 data = pPage->aData;
60247 hdr = pPage->hdrOffset;
60248 hit = sqlite3PageMalloc( pBt->pageSize );
 
60249 if( hit==0 ){
60250 pCheck->mallocFailed = 1;
60251 }else{
60252 int contentOffset = get2byteNotZero(&data[hdr+5]);
60253 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
@@ -60261,11 +60417,12 @@
60261 int j;
60262 if( pc<=usableSize-4 ){
60263 size = cellSizePtr(pPage, &data[pc]);
60264 }
60265 if( (int)(pc+size-1)>=usableSize ){
60266 checkAppendMsg(pCheck, 0,
 
60267 "Corruption detected in cell %d on page %d",i,iPage);
60268 }else{
60269 for(j=pc+size-1; j>=pc; j--) hit[j]++;
60270 }
60271 }
@@ -60283,23 +60440,28 @@
60283 }
60284 for(i=cnt=0; i<usableSize; i++){
60285 if( hit[i]==0 ){
60286 cnt++;
60287 }else if( hit[i]>1 ){
60288 checkAppendMsg(pCheck, 0,
60289 "Multiple uses for byte %d of page %d", i, iPage);
60290 break;
60291 }
60292 }
60293 if( cnt!=data[hdr+7] ){
60294 checkAppendMsg(pCheck, 0,
60295 "Fragmentation of %d bytes reported as %d on page %d",
60296 cnt, data[hdr+7], iPage);
60297 }
60298 }
60299 sqlite3PageFree(hit);
60300 releasePage(pPage);
 
 
 
 
 
60301 return depth+1;
60302 }
60303 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
60304
60305 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
@@ -60336,10 +60498,13 @@
60336 sCheck.pPager = pBt->pPager;
60337 sCheck.nPage = btreePagecount(sCheck.pBt);
60338 sCheck.mxErr = mxErr;
60339 sCheck.nErr = 0;
60340 sCheck.mallocFailed = 0;
 
 
 
60341 *pnErr = 0;
60342 if( sCheck.nPage==0 ){
60343 sqlite3BtreeLeave(p);
60344 return 0;
60345 }
@@ -60355,53 +60520,57 @@
60355 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
60356 sCheck.errMsg.useMalloc = 2;
60357
60358 /* Check the integrity of the freelist
60359 */
 
60360 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
60361 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
 
60362
60363 /* Check all the tables.
60364 */
60365 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
60366 if( aRoot[i]==0 ) continue;
60367 #ifndef SQLITE_OMIT_AUTOVACUUM
60368 if( pBt->autoVacuum && aRoot[i]>1 ){
60369 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
60370 }
60371 #endif
60372 checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
 
 
60373 }
60374
60375 /* Make sure every page in the file is referenced
60376 */
60377 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
60378 #ifdef SQLITE_OMIT_AUTOVACUUM
60379 if( getPageReferenced(&sCheck, i)==0 ){
60380 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
60381 }
60382 #else
60383 /* If the database supports auto-vacuum, make sure no tables contain
60384 ** references to pointer-map pages.
60385 */
60386 if( getPageReferenced(&sCheck, i)==0 &&
60387 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
60388 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
60389 }
60390 if( getPageReferenced(&sCheck, i)!=0 &&
60391 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
60392 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
60393 }
60394 #endif
60395 }
60396
60397 /* Make sure this analysis did not leave any unref() pages.
60398 ** This is an internal consistency check; an integrity check
60399 ** of the integrity check.
60400 */
60401 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
60402 checkAppendMsg(&sCheck, 0,
60403 "Outstanding page count goes from %d to %d during this analysis",
60404 nRef, sqlite3PagerRefcount(pBt->pPager)
60405 );
60406 }
60407
@@ -60593,11 +60762,11 @@
60593
60594 /* Save the positions of all other cursors open on this table. This is
60595 ** required in case any of them are holding references to an xFetch
60596 ** version of the b-tree page modified by the accessPayload call below.
60597 **
60598 ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
60599 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
60600 ** saveAllCursors can only return SQLITE_OK.
60601 */
60602 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
60603 assert( rc==SQLITE_OK );
@@ -63751,11 +63920,12 @@
63751 if( addr==p->nOp-1 ) p->nOp--;
63752 }
63753 }
63754
63755 /*
63756 ** Remove the last opcode inserted
 
63757 */
63758 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
63759 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
63760 sqlite3VdbeChangeToNoop(p, p->nOp-1);
63761 return 1;
@@ -65676,14 +65846,10 @@
65676 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
65677 vdbeFreeOpArray(db, p->aOp, p->nOp);
65678 sqlite3DbFree(db, p->aColName);
65679 sqlite3DbFree(db, p->zSql);
65680 sqlite3DbFree(db, p->pFree);
65681 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
65682 sqlite3DbFree(db, p->zExplain);
65683 sqlite3DbFree(db, p->pExplain);
65684 #endif
65685 }
65686
65687 /*
65688 ** Delete an entire VDBE.
65689 */
@@ -67390,10 +67556,11 @@
67390 void (*xDel)(void *),
67391 unsigned char enc
67392 ){
67393 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
67394 assert( xDel!=SQLITE_DYNAMIC );
 
67395 if( n>0x7fffffff ){
67396 (void)invokeValueDestructor(z, xDel, pCtx);
67397 }else{
67398 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
67399 }
@@ -68712,125 +68879,10 @@
68712 return sqlite3StrAccumFinish(&out);
68713 }
68714
68715 #endif /* #ifndef SQLITE_OMIT_TRACE */
68716
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 /************** End of vdbetrace.c *******************************************/
68833 /************** Begin file vdbe.c ********************************************/
68834 /*
68835 ** 2001 September 15
68836 **
@@ -70475,21 +70527,14 @@
70475 assert( pOp->p4type==P4_FUNCDEF );
70476 ctx.pFunc = pOp->p4.pFunc;
70477 ctx.iOp = pc;
70478 ctx.pVdbe = p;
70479 MemSetTypeFlag(ctx.pOut, MEM_Null);
70480
70481 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;
70489 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
70490 lastRowid = db->lastRowid;
70491
70492 /* If the function returned an error, throw an exception */
70493 if( ctx.fErrorOrAux ){
70494 if( ctx.isError ){
70495 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
@@ -72205,14 +72250,10 @@
72205 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
72206 pCur->pKeyInfo = pKeyInfo;
72207 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
72208 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
72209
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 /* Set the VdbeCursor.isTable variable. Previous versions of
72215 ** SQLite used to check if the root-page flags were sane at this point
72216 ** and report database corruption if they were not, but this check has
72217 ** since moved into the btree layer. */
72218 pCur->isTable = pOp->p4type!=P4_KEYINFO;
@@ -72942,29 +72983,18 @@
72942 ** largest possible integer (9223372036854775807) then the database
72943 ** engine starts picking positive candidate ROWIDs at random until
72944 ** it finds one that is not previously used. */
72945 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
72946 ** 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 cnt = 0;
72952 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
 
 
 
72953 0, &res))==SQLITE_OK)
72954 && (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 }
72966 if( rc==SQLITE_OK && res==0 ){
72967 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
72968 goto abort_due_to_error;
72969 }
72970 assert( v>0 ); /* EV: R-40812-03570 */
@@ -74556,18 +74586,13 @@
74556 ctx.pMem = pMem = &aMem[pOp->p3];
74557 pMem->n++;
74558 sqlite3VdbeMemInit(&t, db, MEM_Null);
74559 ctx.pOut = &t;
74560 ctx.isError = 0;
74561 ctx.pColl = 0;
 
74562 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 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
74570 if( ctx.isError ){
74571 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
74572 rc = ctx.isError;
74573 }
@@ -81597,10 +81622,11 @@
81597 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
81598 pNew->addrOpenEphm[0] = -1;
81599 pNew->addrOpenEphm[1] = -1;
81600 pNew->nSelectRow = p->nSelectRow;
81601 pNew->pWith = withDup(db, p->pWith);
 
81602 return pNew;
81603 }
81604 #else
81605 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
81606 assert( p==0 );
@@ -81739,36 +81765,44 @@
81739 }
81740
81741 /*
81742 ** These routines are Walker callbacks. Walker.u.pi is a pointer
81743 ** 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
81745 ** not constant.
81746 **
81747 ** These callback routines are used to implement the following:
81748 **
81749 ** sqlite3ExprIsConstant()
81750 ** sqlite3ExprIsConstantNotJoin()
81751 ** sqlite3ExprIsConstantOrFunction()
81752 **
 
 
 
 
 
 
 
 
81753 */
81754 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
81755
81756 /* If pWalker->u.i is 3 then any term of the expression that comes from
81757 ** the ON or USING clauses of a join disqualifies the expression
81758 ** from being considered constant. */
81759 if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
81760 pWalker->u.i = 0;
81761 return WRC_Abort;
81762 }
81763
81764 switch( pExpr->op ){
81765 /* 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
81767 ** flag. */
81768 case TK_FUNCTION:
81769 if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
81770 return WRC_Continue;
81771 }
81772 /* Fall through */
81773 case TK_ID:
81774 case TK_COLUMN:
@@ -81778,10 +81812,23 @@
81778 testcase( pExpr->op==TK_COLUMN );
81779 testcase( pExpr->op==TK_AGG_FUNCTION );
81780 testcase( pExpr->op==TK_AGG_COLUMN );
81781 pWalker->u.i = 0;
81782 return WRC_Abort;
 
 
 
 
 
 
 
 
 
 
 
 
 
81783 default:
81784 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
81785 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
81786 return WRC_Continue;
81787 }
@@ -81818,11 +81865,11 @@
81818 ** that does no originate from the ON or USING clauses of a join.
81819 ** Return 0 if it involves variables or function calls or terms from
81820 ** an ON or USING clause.
81821 */
81822 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
81823 return exprIsConst(p, 3);
81824 }
81825
81826 /*
81827 ** Walk an expression tree. Return 1 if the expression is constant
81828 ** or a function call with constant arguments. Return and 0 if there
@@ -81830,12 +81877,13 @@
81830 **
81831 ** For the purposes of this function, a double-quoted string (ex: "abc")
81832 ** is considered a variable but a single-quoted string (ex: 'abc') is
81833 ** a constant.
81834 */
81835 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
81836 return exprIsConst(p, 2);
 
81837 }
81838
81839 /*
81840 ** If the expression p codes a constant integer that is small enough
81841 ** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -83741,94 +83789,90 @@
83741 iMem = ++pParse->nMem;
83742 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
83743 exprToRegister(pExpr, iMem);
83744 }
83745
83746 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
83747 /*
83748 ** Generate a human-readable explanation of an expression tree.
83749 */
83750 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
83751 int op; /* The opcode being coded */
83752 const char *zBinOp = 0; /* Binary operator */
83753 const char *zUniOp = 0; /* Unary operator */
 
83754 if( pExpr==0 ){
83755 op = TK_NULL;
83756 }else{
83757 op = pExpr->op;
83758 }
83759 switch( op ){
83760 case TK_AGG_COLUMN: {
83761 sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
83762 pExpr->iTable, pExpr->iColumn);
83763 break;
83764 }
83765 case TK_COLUMN: {
83766 if( pExpr->iTable<0 ){
83767 /* This only happens when coding check constraints */
83768 sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
83769 }else{
83770 sqlite3ExplainPrintf(pOut, "{%d:%d}",
83771 pExpr->iTable, pExpr->iColumn);
83772 }
83773 break;
83774 }
83775 case TK_INTEGER: {
83776 if( pExpr->flags & EP_IntValue ){
83777 sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
83778 }else{
83779 sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
83780 }
83781 break;
83782 }
83783 #ifndef SQLITE_OMIT_FLOATING_POINT
83784 case TK_FLOAT: {
83785 sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
83786 break;
83787 }
83788 #endif
83789 case TK_STRING: {
83790 sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
83791 break;
83792 }
83793 case TK_NULL: {
83794 sqlite3ExplainPrintf(pOut,"NULL");
83795 break;
83796 }
83797 #ifndef SQLITE_OMIT_BLOB_LITERAL
83798 case TK_BLOB: {
83799 sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
83800 break;
83801 }
83802 #endif
83803 case TK_VARIABLE: {
83804 sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
83805 pExpr->u.zToken, pExpr->iColumn);
83806 break;
83807 }
83808 case TK_REGISTER: {
83809 sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
83810 break;
83811 }
83812 case TK_AS: {
83813 sqlite3ExplainExpr(pOut, pExpr->pLeft);
 
 
 
 
 
83814 break;
83815 }
83816 #ifndef SQLITE_OMIT_CAST
83817 case TK_CAST: {
83818 /* 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, ")");
83830 break;
83831 }
83832 #endif /* SQLITE_OMIT_CAST */
83833 case TK_LT: zBinOp = "LT"; break;
83834 case TK_LE: zBinOp = "LE"; break;
@@ -83848,21 +83892,22 @@
83848 case TK_BITOR: zBinOp = "BITOR"; break;
83849 case TK_SLASH: zBinOp = "DIV"; break;
83850 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
83851 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
83852 case TK_CONCAT: zBinOp = "CONCAT"; break;
 
83853
83854 case TK_UMINUS: zUniOp = "UMINUS"; break;
83855 case TK_UPLUS: zUniOp = "UPLUS"; break;
83856 case TK_BITNOT: zUniOp = "BITNOT"; break;
83857 case TK_NOT: zUniOp = "NOT"; break;
83858 case TK_ISNULL: zUniOp = "ISNULL"; break;
83859 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
83860
83861 case TK_COLLATE: {
83862 sqlite3ExplainExpr(pOut, pExpr->pLeft);
83863 sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
83864 break;
83865 }
83866
83867 case TK_AGG_FUNCTION:
83868 case TK_FUNCTION: {
@@ -83870,45 +83915,40 @@
83870 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
83871 pFarg = 0;
83872 }else{
83873 pFarg = pExpr->x.pList;
83874 }
83875 if( op==TK_AGG_FUNCTION ){
83876 sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
83877 pExpr->op2, pExpr->u.zToken);
83878 }else{
83879 sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
83880 }
83881 if( pFarg ){
83882 sqlite3ExplainExprList(pOut, pFarg);
83883 }
83884 sqlite3ExplainPrintf(pOut, ")");
83885 break;
83886 }
83887 #ifndef SQLITE_OMIT_SUBQUERY
83888 case TK_EXISTS: {
83889 sqlite3ExplainPrintf(pOut, "EXISTS(");
83890 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
83891 sqlite3ExplainPrintf(pOut,")");
83892 break;
83893 }
83894 case TK_SELECT: {
83895 sqlite3ExplainPrintf(pOut, "(");
83896 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
83897 sqlite3ExplainPrintf(pOut, ")");
83898 break;
83899 }
83900 case TK_IN: {
83901 sqlite3ExplainPrintf(pOut, "IN(");
83902 sqlite3ExplainExpr(pOut, pExpr->pLeft);
83903 sqlite3ExplainPrintf(pOut, ",");
83904 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83905 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
83906 }else{
83907 sqlite3ExplainExprList(pOut, pExpr->x.pList);
83908 }
83909 sqlite3ExplainPrintf(pOut, ")");
83910 break;
83911 }
83912 #endif /* SQLITE_OMIT_SUBQUERY */
83913
83914 /*
@@ -83924,17 +83964,14 @@
83924 */
83925 case TK_BETWEEN: {
83926 Expr *pX = pExpr->pLeft;
83927 Expr *pY = pExpr->x.pList->a[0].pExpr;
83928 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, ")");
83936 break;
83937 }
83938 case TK_TRIGGER: {
83939 /* If the opcode is TK_TRIGGER, then the expression is a reference
83940 ** to a column in the new.* or old.* pseudo-tables available to
@@ -83941,19 +83978,18 @@
83941 ** trigger programs. In this case Expr.iTable is set to 1 for the
83942 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
83943 ** is set to the column of the pseudo-table to read, or to -1 to
83944 ** read the rowid field.
83945 */
83946 sqlite3ExplainPrintf(pOut, "%s(%d)",
83947 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
83948 break;
83949 }
83950 case TK_CASE: {
83951 sqlite3ExplainPrintf(pOut, "CASE(");
83952 sqlite3ExplainExpr(pOut, pExpr->pLeft);
83953 sqlite3ExplainPrintf(pOut, ",");
83954 sqlite3ExplainExprList(pOut, pExpr->x.pList);
83955 break;
83956 }
83957 #ifndef SQLITE_OMIT_TRIGGER
83958 case TK_RAISE: {
83959 const char *zType = "unk";
@@ -83961,59 +83997,61 @@
83961 case OE_Rollback: zType = "rollback"; break;
83962 case OE_Abort: zType = "abort"; break;
83963 case OE_Fail: zType = "fail"; break;
83964 case OE_Ignore: zType = "ignore"; break;
83965 }
83966 sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
83967 break;
83968 }
83969 #endif
 
 
 
 
83970 }
83971 if( zBinOp ){
83972 sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
83973 sqlite3ExplainExpr(pOut, pExpr->pLeft);
83974 sqlite3ExplainPrintf(pOut,",");
83975 sqlite3ExplainExpr(pOut, pExpr->pRight);
83976 sqlite3ExplainPrintf(pOut,")");
83977 }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)
83986 /*
83987 ** Generate a human-readable explanation of an expression list.
83988 */
83989 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
 
 
 
 
 
83990 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);
83996 }else{
83997 sqlite3ExplainPush(pOut);
83998 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 ){
84004 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
84005 }
84006 if( pList->a[i].bSpanIsTab ){
84007 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
84008 }
84009 if( i<pList->nExpr-1 ){
84010 sqlite3ExplainNL(pOut);
84011 }
84012 }
84013 sqlite3ExplainPop(pOut);
84014 }
 
84015 }
84016 #endif /* SQLITE_DEBUG */
84017
84018 /*
84019 ** Generate code that pushes the value of every element of the given
@@ -89633,11 +89671,11 @@
89633 Column *pCol;
89634 sqlite3 *db = pParse->db;
89635 p = pParse->pNewTable;
89636 if( p!=0 ){
89637 pCol = &(p->aCol[p->nCol-1]);
89638 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
89639 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
89640 pCol->zName);
89641 }else{
89642 /* A copy of pExpr is used instead of the original, as pExpr contains
89643 ** tokens that point to volatile memory. The 'span' of the expression
@@ -94030,11 +94068,14 @@
94030
94031 /*
94032 ** Return the collating function associated with a function.
94033 */
94034 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
94035 return context->pColl;
 
 
 
94036 }
94037
94038 /*
94039 ** Indicate that the accumulator load should be skipped on this
94040 ** iteration of the aggregate loop.
@@ -94575,14 +94616,16 @@
94575 ** character is exactly one byte in size. Also, all characters are
94576 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
94577 ** whereas only characters less than 0x80 do in ASCII.
94578 */
94579 #if defined(SQLITE_EBCDIC)
94580 # define sqlite3Utf8Read(A) (*((*A)++))
94581 # define GlobUpperToLower(A) A = sqlite3UpperToLower[A]
 
94582 #else
94583 # define GlobUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
 
94584 #endif
94585
94586 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
94587 /* The correct SQL-92 behavior is for the LIKE operator to ignore
94588 ** case. Thus 'a' LIKE 'A' would be true. */
@@ -94591,11 +94634,11 @@
94591 ** is case sensitive causing 'a' LIKE 'A' to be false */
94592 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
94593
94594 /*
94595 ** Compare two UTF-8 strings for equality where the first string can
94596 ** potentially be a "glob" expression. Return true (1) if they
94597 ** are the same and false (0) if they are different.
94598 **
94599 ** Globbing rules:
94600 **
94601 ** '*' Matches any sequence of zero or more characters.
@@ -94611,120 +94654,147 @@
94611 ** in the list by making it the first character after '[' or '^'. A
94612 ** range of characters can be specified using '-'. Example:
94613 ** "[a-z]" matches any single lower-case letter. To match a '-', make
94614 ** it the last character in the list.
94615 **
 
 
 
 
 
 
 
 
 
 
 
94616 ** 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 */
94622 static int patternCompare(
94623 const u8 *zPattern, /* The glob pattern */
94624 const u8 *zString, /* The string to compare against the glob */
94625 const struct compareInfo *pInfo, /* Information about how to do the compare */
94626 u32 esc /* The escape character */
94627 ){
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' */
 
 
 
 
 
94636
94637 while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
94638 if( c==matchAll && !prevEscape ){
 
 
 
94639 while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
94640 || c == matchOne ){
94641 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
94642 return 0;
94643 }
94644 }
94645 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 }
 
 
 
 
 
 
 
 
 
 
 
 
94726 }
94727 return *zString==0;
94728 }
94729
94730 /*
@@ -103884,10 +103954,24 @@
103884 **
103885 *************************************************************************
103886 ** This file contains C code routines that are called by the parser
103887 ** to handle SELECT statements in SQLite.
103888 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103889
103890 /*
103891 ** An instance of the following object is used to record information about
103892 ** how to process the DISTINCT keyword, to simplify passing that information
103893 ** into the selectInnerLoop() routine.
@@ -103996,10 +104080,22 @@
103996 assert( pNew->pSrc!=0 || pParse->nErr>0 );
103997 }
103998 assert( pNew!=&standin );
103999 return pNew;
104000 }
 
 
 
 
 
 
 
 
 
 
 
 
104001
104002 /*
104003 ** Delete the given Select structure and all of its substructures.
104004 */
104005 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
@@ -107226,10 +107322,12 @@
107226 }
107227 }
107228 }
107229
107230 /***** If we reach this point, flattening is permitted. *****/
 
 
107231
107232 /* Authorize the subquery */
107233 pParse->zAuthContext = pSubitem->zName;
107234 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
107235 testcase( i==SQLITE_DENY );
@@ -107278,10 +107376,11 @@
107278 p->pSrc = 0;
107279 p->pPrior = 0;
107280 p->pLimit = 0;
107281 p->pOffset = 0;
107282 pNew = sqlite3SelectDup(db, p, 0);
 
107283 p->pOffset = pOffset;
107284 p->pLimit = pLimit;
107285 p->pOrderBy = pOrderBy;
107286 p->pSrc = pSrc;
107287 p->op = TK_ALL;
@@ -107290,10 +107389,13 @@
107290 }else{
107291 pNew->pPrior = pPrior;
107292 if( pPrior ) pPrior->pNext = pNew;
107293 pNew->pNext = p;
107294 p->pPrior = pNew;
 
 
 
107295 }
107296 if( db->mallocFailed ) return 1;
107297 }
107298
107299 /* Begin flattening the iFrom-th entry of the FROM clause
@@ -107419,12 +107521,27 @@
107419 if( isAgg ){
107420 substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
107421 pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
107422 }
107423 if( pSub->pOrderBy ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107424 assert( pParent->pOrderBy==0 );
107425 pParent->pOrderBy = pSub->pOrderBy;
 
107426 pSub->pOrderBy = 0;
107427 }else if( pParent->pOrderBy ){
107428 substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
107429 }
107430 if( pSub->pWhere ){
@@ -107465,10 +107582,17 @@
107465
107466 /* Finially, delete what is left of the subquery and return
107467 ** success.
107468 */
107469 sqlite3SelectDelete(db, pSub1);
 
 
 
 
 
 
 
107470
107471 return 1;
107472 }
107473 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
107474
@@ -107936,10 +108060,11 @@
107936 if( pTab->pSelect || IsVirtual(pTab) ){
107937 /* We reach here if the named table is a really a view */
107938 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
107939 assert( pFrom->pSelect==0 );
107940 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
 
107941 sqlite3WalkSelect(pWalker, pFrom->pSelect);
107942 }
107943 #endif
107944 }
107945
@@ -108470,10 +108595,17 @@
108470 if( p==0 || db->mallocFailed || pParse->nErr ){
108471 return 1;
108472 }
108473 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
108474 memset(&sAggInfo, 0, sizeof(sAggInfo));
 
 
 
 
 
 
 
108475
108476 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
108477 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
108478 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
108479 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
@@ -108626,10 +108758,14 @@
108626 /* If there is are a sequence of queries, do the earlier ones first.
108627 */
108628 if( p->pPrior ){
108629 rc = multiSelect(pParse, p, pDest);
108630 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 
 
 
 
108631 return rc;
108632 }
108633 #endif
108634
108635 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -109225,107 +109361,110 @@
109225 generateColumnNames(pParse, pTabList, pEList);
109226 }
109227
109228 sqlite3DbFree(db, sAggInfo.aCol);
109229 sqlite3DbFree(db, sAggInfo.aFunc);
 
 
 
 
109230 return rc;
109231 }
109232
109233 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
109234 /*
109235 ** Generate a human-readable description of a the Select object.
109236 */
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);
 
 
109251 if( p->pSrc && p->pSrc->nSrc ){
109252 int i;
109253 sqlite3ExplainPrintf(pVdbe, "FROM ");
109254 sqlite3ExplainPush(pVdbe);
109255 for(i=0; i<p->pSrc->nSrc; i++){
109256 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 }
109263 }else if( pItem->zName ){
109264 sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
 
 
 
109265 }
109266 if( pItem->zAlias ){
109267 sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
109268 }
109269 if( pItem->jointype & JT_LEFT ){
109270 sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
109271 }
109272 sqlite3ExplainNL(pVdbe);
 
 
 
 
 
109273 }
109274 sqlite3ExplainPop(pVdbe);
109275 }
109276 if( p->pWhere ){
109277 sqlite3ExplainPrintf(pVdbe, "WHERE ");
109278 sqlite3ExplainExpr(pVdbe, p->pWhere);
109279 sqlite3ExplainNL(pVdbe);
109280 }
109281 if( p->pGroupBy ){
109282 sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
109283 sqlite3ExplainExprList(pVdbe, p->pGroupBy);
109284 sqlite3ExplainNL(pVdbe);
109285 }
109286 if( p->pHaving ){
109287 sqlite3ExplainPrintf(pVdbe, "HAVING ");
109288 sqlite3ExplainExpr(pVdbe, p->pHaving);
109289 sqlite3ExplainNL(pVdbe);
109290 }
109291 if( p->pOrderBy ){
109292 sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
109293 sqlite3ExplainExprList(pVdbe, p->pOrderBy);
109294 sqlite3ExplainNL(pVdbe);
109295 }
109296 if( p->pLimit ){
109297 sqlite3ExplainPrintf(pVdbe, "LIMIT ");
109298 sqlite3ExplainExpr(pVdbe, p->pLimit);
109299 sqlite3ExplainNL(pVdbe);
109300 }
109301 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) */
109327
109328 /************** End of select.c **********************************************/
109329 /************** Begin file table.c *******************************************/
109330 /*
109331 ** 2001 September 15
@@ -113719,15 +113858,10 @@
113719 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
113720 assert( TK_GE==TK_EQ+4 );
113721 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
113722 }
113723
113724 /*
113725 ** Swap two objects of type TYPE.
113726 */
113727 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
113728
113729 /*
113730 ** Commute a comparison operator. Expressions of the form "X op Y"
113731 ** are converted into "Y op X".
113732 **
113733 ** If left/right precedence rules come into play when determining the
@@ -116884,12 +117018,13 @@
116884
116885 /* Run a separate WHERE clause for each term of the OR clause. After
116886 ** eliminating duplicates from other WHERE clauses, the action for each
116887 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
116888 */
116889 wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
116890 WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY;
 
116891 for(ii=0; ii<pOrWc->nTerm; ii++){
116892 WhereTerm *pOrTerm = &pOrWc->a[ii];
116893 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
116894 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
116895 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -116897,10 +117032,11 @@
116897 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
116898 pAndExpr->pLeft = pOrExpr;
116899 pOrExpr = pAndExpr;
116900 }
116901 /* Loop through table entries that match term pOrTerm. */
 
116902 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
116903 wctrlFlags, iCovCur);
116904 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
116905 if( pSubWInfo ){
116906 WhereLoop *pSubLoop;
@@ -117116,25 +117252,30 @@
117116 }
117117
117118 return pLevel->notReady;
117119 }
117120
117121 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
117122 /*
117123 ** Generate "Explanation" text for a WhereTerm.
117124 */
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
 
 
 
 
 
117136
117137 #ifdef WHERETRACE_ENABLED
117138 /*
117139 ** Print a WhereLoop object for debugging purposes
117140 */
@@ -117174,31 +117315,16 @@
117174 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
117175 }else{
117176 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
117177 }
117178 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 */
117184 int i;
117185 Vdbe *v = pWInfo->pParse->pVdbe;
117186 sqlite3ExplainBegin(v);
117187 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
117200 }
117201 #endif
117202
117203 /*
117204 ** Convert bulk memory into a valid WhereLoop that can be passed
@@ -117714,15 +117840,18 @@
117714 pNew->aLTerm[pNew->nLTerm++] = 0;
117715 pNew->wsFlags |= WHERE_SKIPSCAN;
117716 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
117717 if( pTerm ){
117718 /* 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
117720 ** desirable than the regular index lookup. */
117721 nIter += 10; assert( 10==sqlite3LogEst(2) );
117722 }
117723 pNew->nOut -= nIter;
 
 
 
117724 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
117725 pNew->nOut = saved_nOut;
117726 pNew->u.btree.nEq = saved_nEq;
117727 pNew->u.btree.nSkip = saved_nSkip;
117728 }
@@ -118073,13 +118202,21 @@
118073 pNew->u.btree.nSkip = 0;
118074 pNew->u.btree.pIndex = 0;
118075 pNew->nLTerm = 1;
118076 pNew->aLTerm[0] = pTerm;
118077 /* 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) );
 
 
 
 
 
 
 
 
118081 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
118082 /* TUNING: Each index lookup yields 20 rows in the table. This
118083 ** is more than the usual guess of 10 rows, since we have no way
118084 ** of knowing how selective the index will ultimately be. It would
118085 ** not be unreasonable to make this value much larger. */
@@ -118363,11 +118500,10 @@
118363 WhereLoopBuilder sSubBuild;
118364 WhereOrSet sSum, sCur;
118365 struct SrcList_item *pItem;
118366
118367 pWC = pBuilder->pWC;
118368 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
118369 pWCEnd = pWC->a + pWC->nTerm;
118370 pNew = pBuilder->pNew;
118371 memset(&sSum, 0, sizeof(sSum));
118372 pItem = pWInfo->pTabList->a + pNew->iTab;
118373 iCur = pItem->iCursor;
@@ -118384,10 +118520,11 @@
118384
118385 sSubBuild = *pBuilder;
118386 sSubBuild.pOrderBy = 0;
118387 sSubBuild.pOrSet = &sCur;
118388
 
118389 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
118390 if( (pOrTerm->eOperator & WO_AND)!=0 ){
118391 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
118392 }else if( pOrTerm->leftCursor==iCur ){
118393 tempWC.pWInfo = pWC->pWInfo;
@@ -118398,18 +118535,30 @@
118398 sSubBuild.pWC = &tempWC;
118399 }else{
118400 continue;
118401 }
118402 sCur.n = 0;
 
 
 
 
 
 
 
 
 
118403 #ifndef SQLITE_OMIT_VIRTUALTABLE
118404 if( IsVirtual(pItem->pTab) ){
118405 rc = whereLoopAddVirtual(&sSubBuild, mExtra);
118406 }else
118407 #endif
118408 {
118409 rc = whereLoopAddBtree(&sSubBuild, mExtra);
118410 }
 
 
 
118411 assert( rc==SQLITE_OK || sCur.n==0 );
118412 if( sCur.n==0 ){
118413 sSum.n = 0;
118414 break;
118415 }else if( once ){
@@ -118450,10 +118599,11 @@
118450 pNew->rRun = sSum.a[i].rRun + 1;
118451 pNew->nOut = sSum.a[i].nOut;
118452 pNew->prereq = sSum.a[i].prereq;
118453 rc = whereLoopInsert(pBuilder, pNew);
118454 }
 
118455 }
118456 }
118457 return rc;
118458 }
118459
@@ -119516,27 +119666,20 @@
119516 }
119517 }
119518
119519 /* Construct the WhereLoop objects */
119520 WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
 
119521 /* Display all terms of the WHERE clause */
119522 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
119523 if( sqlite3WhereTrace & 0x100 ){
119524 int i;
119525 Vdbe *v = pParse->pVdbe;
119526 sqlite3ExplainBegin(v);
119527 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));
119536 }
119537 #endif
 
119538 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
119539 rc = whereLoopAddAll(&sWLB);
119540 if( rc ) goto whereBeginError;
119541
119542 /* Display all of the WhereLoop objects if wheretrace is enabled */
@@ -120059,11 +120202,11 @@
120059
120060 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
120061 ** unary TK_ISNULL or TK_NOTNULL expression. */
120062 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
120063 sqlite3 *db = pParse->db;
120064 if( db->mallocFailed==0 && pY->op==TK_NULL ){
120065 pA->op = (u8)op;
120066 sqlite3ExprDelete(db, pA->pRight);
120067 pA->pRight = 0;
120068 }
120069 }
@@ -122318,13 +122461,10 @@
122318 break;
122319 case 111: /* cmd ::= select */
122320 {
122321 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
122322 sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
122323 sqlite3ExplainBegin(pParse->pVdbe);
122324 sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3);
122325 sqlite3ExplainFinish(pParse->pVdbe);
122326 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
122327 }
122328 break;
122329 case 112: /* select ::= with selectnowith */
122330 {
@@ -122377,10 +122517,34 @@
122377 {yygotominor.yy328 = TK_ALL;}
122378 break;
122379 case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
122380 {
122381 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);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122382 }
122383 break;
122384 case 120: /* values ::= VALUES LP nexprlist RP */
122385 {
122386 yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
@@ -123868,10 +124032,11 @@
123868 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
123869 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
123870 };
123871 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
123872 #endif
 
123873
123874
123875 /*
123876 ** Return the length of the token that begins at z[0].
123877 ** Store the token type in *tokenType before returning.
@@ -125138,10 +125303,15 @@
125138 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
125139 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
125140 break;
125141 }
125142
 
 
 
 
 
125143 case SQLITE_CONFIG_URI: {
125144 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
125145 break;
125146 }
125147
@@ -126875,11 +127045,11 @@
126875 int nUri = sqlite3Strlen30(zUri);
126876
126877 assert( *pzErrMsg==0 );
126878
126879 if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
126880 && nUri>=5 && memcmp(zUri, "file:", 5)==0
126881 ){
126882 char *zOpt;
126883 int eState; /* Parser state when parsing URI */
126884 int iIn; /* Input character index */
126885 int iOut = 0; /* Output character index */
@@ -127105,11 +127275,13 @@
127105 assert( SQLITE_OPEN_READWRITE == 0x02 );
127106 assert( SQLITE_OPEN_CREATE == 0x04 );
127107 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
127108 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
127109 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
127110 if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
 
 
127111
127112 if( sqlite3GlobalConfig.bCoreMutex==0 ){
127113 isThreadsafe = 0;
127114 }else if( flags & SQLITE_OPEN_NOMUTEX ){
127115 isThreadsafe = 0;
@@ -127989,26 +128161,10 @@
127989 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
127990 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
127991 break;
127992 }
127993
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 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
128011 **
128012 ** Set or clear a flag that indicates that the database file is always well-
128013 ** formed and never corrupt. This flag is clear by default, indicating that
128014 ** database files might have arbitrary corruption. Setting the flag during
128015
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -231,11 +231,11 @@
231 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232 ** [sqlite_version()] and [sqlite_source_id()].
233 */
234 #define SQLITE_VERSION "3.8.7"
235 #define SQLITE_VERSION_NUMBER 3008007
236 #define SQLITE_SOURCE_ID "2014-10-04 19:31:53 b8f7f19dc06c59de2e194d83e6c052fb7d28c71d"
237
238 /*
239 ** CAPI3REF: Run-Time Library Version Numbers
240 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
241 **
@@ -2791,13 +2791,13 @@
2791 ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
2792 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2793 ** an English language description of the error following a failure of any
2794 ** of the sqlite3_open() routines.
2795 **
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 **
2800 ** Whether or not an error occurs when it is opened, resources
2801 ** associated with the [database connection] handle should be released by
2802 ** passing it to [sqlite3_close()] when it is no longer required.
2803 **
@@ -2881,17 +2881,18 @@
2881 ** ^SQLite uses the path component of the URI as the name of the disk file
2882 ** which contains the database. ^If the path begins with a '/' character,
2883 ** then it is interpreted as an absolute path. ^If the path does not begin
2884 ** with a '/' (meaning that the authority section is omitted from the URI)
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:").)^
2888 **
2889 ** [[core URI query parameters]]
2890 ** The query component of a URI may contain parameters that are interpreted
2891 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2892 ** SQLite and its built-in [VFSes] interpret the
2893 ** following query parameters:
2894 **
2895 ** <ul>
2896 ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2897 ** a VFS object that provides the operating system interface that should
2898 ** be used to access the database file on disk. ^If this option is set to
@@ -2922,15 +2923,13 @@
2923 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2924 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2925 ** a URI filename, its value overrides any behavior requested by setting
2926 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2927 **
2928 ** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
 
2929 ** [powersafe overwrite] property does or does not apply to the
2930 ** storage media on which the database file resides.
 
2931 **
2932 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2933 ** which if set disables file locking in rollback journal modes. This
2934 ** is useful for accessing a database on a filesystem that does not
2935 ** support locking. Caution: Database corruption might result if two
@@ -3521,15 +3520,14 @@
3520 ** terminated. If any NUL characters occur at byte offsets less than
3521 ** the value of the fourth parameter then the resulting string value will
3522 ** contain embedded NULs. The result of expressions involving strings
3523 ** with embedded NULs is undefined.
3524 **
3525 ** ^The fifth argument to the BLOB and string binding interfaces
3526 ** is a destructor used to dispose of the BLOB or
3527 ** string after SQLite has finished with it. ^The destructor is called
3528 ** to dispose of the BLOB or string even if the call to bind API fails.
 
3529 ** ^If the fifth argument is
3530 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3531 ** information is in static, unmanaged space and does not need to be freed.
3532 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3533 ** SQLite makes its own private copy of the data immediately, before
@@ -3536,11 +3534,11 @@
3534 ** the sqlite3_bind_*() routine returns.
3535 **
3536 ** ^The sixth argument to sqlite3_bind_text64() must be one of
3537 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
3538 ** to specify the encoding of the text in the third parameter. If
3539 ** the sixth argument to sqlite3_bind_text64() is not one of the
3540 ** allowed values shown above, or if the text encoding is different
3541 ** from the encoding specified by the sixth parameter, then the behavior
3542 ** is undefined.
3543 **
3544 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4572,11 +4570,11 @@
4570 **
4571 ** ^The sqlite3_result_null() interface sets the return value
4572 ** of the application-defined function to be NULL.
4573 **
4574 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4575 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4576 ** set the return value of the application-defined function to be
4577 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4578 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4579 ** ^The sqlite3_result_text64() interface sets the return value of an
4580 ** application-defined function to be a text string in an encoding
@@ -6332,11 +6330,11 @@
6330 #define SQLITE_TESTCTRL_RESERVE 14
6331 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6332 #define SQLITE_TESTCTRL_ISKEYWORD 16
6333 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6334 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6335 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
6336 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6337 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6338 #define SQLITE_TESTCTRL_BYTEORDER 22
6339 #define SQLITE_TESTCTRL_ISINIT 23
6340 #define SQLITE_TESTCTRL_SORTER_MMAP 24
@@ -8519,10 +8517,15 @@
8517 ** Macros to compute minimum and maximum of two numbers.
8518 */
8519 #define MIN(A,B) ((A)<(B)?(A):(B))
8520 #define MAX(A,B) ((A)>(B)?(A):(B))
8521
8522 /*
8523 ** Swap two objects of type TYPE.
8524 */
8525 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
8526
8527 /*
8528 ** Check to see if this machine uses EBCDIC. (Yes, believe it or
8529 ** not, there are still machines out there that use EBCDIC.)
8530 */
8531 #if 'A' == '\301'
@@ -8756,10 +8759,20 @@
8759 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8760 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8761 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8762 #endif
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
8774 /*
8775 ** An instance of the following structure is used to store the busy-handler
8776 ** callback for a given sqlite handle.
8777 **
8778 ** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8895,10 +8908,11 @@
8908 typedef struct SrcList SrcList;
8909 typedef struct StrAccum StrAccum;
8910 typedef struct Table Table;
8911 typedef struct TableLock TableLock;
8912 typedef struct Token Token;
8913 typedef struct TreeView TreeView;
8914 typedef struct Trigger Trigger;
8915 typedef struct TriggerPrg TriggerPrg;
8916 typedef struct TriggerStep TriggerStep;
8917 typedef struct UnpackedRecord UnpackedRecord;
8918 typedef struct VTable VTable;
@@ -11738,11 +11752,11 @@
11752 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
11753 #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
11754 #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
11755 #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
11756 #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
11757 /* 0x0080 // not currently used */
11758 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11759 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11760 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11761 #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
11762 #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */
@@ -11823,10 +11837,13 @@
11837 struct Select {
11838 ExprList *pEList; /* The fields of the result */
11839 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11840 u16 selFlags; /* Various SF_* values */
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
11845 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
11846 u64 nSelectRow; /* Estimated number of result rows */
11847 SrcList *pSrc; /* The FROM clause */
11848 Expr *pWhere; /* The WHERE clause */
11849 ExprList *pGroupBy; /* The GROUP BY clause */
@@ -12081,10 +12098,14 @@
12098 yDbMask cookieMask; /* Bitmask of schema verified databases */
12099 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
12100 int regRowid; /* Register holding rowid of CREATE TABLE entry */
12101 int regRoot; /* Register holding root page number for new objects */
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
12107 #ifndef SQLITE_OMIT_SHARED_CACHE
12108 int nTableLock; /* Number of locks in aTableLock */
12109 TableLock *aTableLock; /* Required table locks for shared-cache mode */
12110 #endif
12111 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
@@ -12160,10 +12181,11 @@
12181
12182 /*
12183 ** Bitfield flags for P5 value in various opcodes.
12184 */
12185 #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
12186 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
12187 #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
12188 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
12189 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
12190 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
12191 #define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */
@@ -12428,10 +12450,21 @@
12450 Select *pSelect; /* The definition of this CTE */
12451 const char *zErr; /* Error message for circular references */
12452 } a[1];
12453 };
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
12466 /*
12467 ** Assuming zIn points to the first byte of a UTF-8 character,
12468 ** advance zIn to point to the first byte of the next UTF-8 character.
12469 */
12470 #define SQLITE_SKIP_UTF8(zIn) { \
@@ -12493,10 +12526,11 @@
12526 # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
12527 # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
12528 # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
12529 # define sqlite3Tolower(x) tolower((unsigned char)(x))
12530 #endif
12531 SQLITE_PRIVATE int sqlite3IsIdChar(u8);
12532
12533 /*
12534 ** Internal function prototypes
12535 */
12536 #define sqlite3StrICmp sqlite3_stricmp
@@ -12591,29 +12625,18 @@
12625 #endif
12626 #if defined(SQLITE_TEST)
12627 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
12628 #endif
12629
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);
 
 
 
 
 
 
 
 
 
 
 
12638 #endif
12639
12640
12641 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
12642 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
@@ -12791,11 +12814,11 @@
12814 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
12815 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12816 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12817 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12818 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12819 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
12820 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12821 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12822 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12823 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12824 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -12815,10 +12838,15 @@
12838 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
12839 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
12840 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
12841 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
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
12848 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
12849 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
12850 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
12851 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
12852 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
@@ -13439,10 +13467,17 @@
13467 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
13468 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
13469 };
13470 #endif
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 */
13479 #ifndef SQLITE_USE_URI
13480 # define SQLITE_USE_URI 0
13481 #endif
13482
13483 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
@@ -13945,11 +13980,11 @@
13980
13981 /* Since ArraySize(azCompileOpt) is normally in single digits, a
13982 ** linear search is adequate. No need for a binary search. */
13983 for(i=0; i<ArraySize(azCompileOpt); i++){
13984 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13985 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
13986 ){
13987 return 1;
13988 }
13989 }
13990 return 0;
@@ -14259,11 +14294,10 @@
14294 */
14295 struct sqlite3_context {
14296 Mem *pOut; /* The return value is stored here */
14297 FuncDef *pFunc; /* Pointer to function information */
14298 Mem *pMem; /* Memory cell used to store aggregate context */
 
14299 Vdbe *pVdbe; /* The VM that owns this context */
14300 int iOp; /* Instruction number of OP_Function */
14301 int isError; /* Error code returned by the function. */
14302 u8 skipFlag; /* Skip accumulator loading if true */
14303 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
@@ -14348,14 +14382,10 @@
14382 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
14383 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
14384 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
14385 char *zSql; /* Text of the SQL statement that generated this */
14386 void *pFree; /* Free this when deleting the vdbe */
 
 
 
 
14387 VdbeFrame *pFrame; /* Parent frame */
14388 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
14389 int nFrame; /* Number of frames in pFrame list */
14390 u32 expmask; /* Binding to these vars invalidates VM */
14391 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
@@ -14675,11 +14705,11 @@
14705 sqlite3VdbeClearObject(db, pVdbe);
14706 sqlite3DbFree(db, pVdbe);
14707 }
14708 db->pnBytesFreed = 0;
14709
14710 *pHighwater = 0; /* IMP: R-64479-57858 */
14711 *pCurrent = nByte;
14712
14713 break;
14714 }
14715
@@ -14700,21 +14730,23 @@
14730 if( db->aDb[i].pBt ){
14731 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
14732 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
14733 }
14734 }
14735 *pHighwater = 0; /* IMP: R-42420-56072 */
14736 /* IMP: R-54100-20147 */
14737 /* IMP: R-29431-39229 */
14738 *pCurrent = nRet;
14739 break;
14740 }
14741
14742 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
14743 ** key constraints. Set *pCurrent to zero if all foreign key constraints
14744 ** have been satisfied. The *pHighwater is always set to zero.
14745 */
14746 case SQLITE_DBSTATUS_DEFERRED_FKS: {
14747 *pHighwater = 0; /* IMP: R-11967-56545 */
14748 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
14749 break;
14750 }
14751
14752 default: {
@@ -20195,11 +20227,11 @@
20227 mallocWithAlarm((int)n, &p);
20228 sqlite3_mutex_leave(mem0.mutex);
20229 }else{
20230 p = sqlite3GlobalConfig.m.xMalloc((int)n);
20231 }
20232 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
20233 return p;
20234 }
20235
20236 /*
20237 ** This version of the memory allocation is for use by the application.
@@ -20418,14 +20450,14 @@
20450 */
20451 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
20452 int nOld, nNew, nDiff;
20453 void *pNew;
20454 if( pOld==0 ){
20455 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
20456 }
20457 if( nBytes==0 ){
20458 sqlite3_free(pOld); /* IMP: R-26507-47431 */
20459 return 0;
20460 }
20461 if( nBytes>=0x7fffff00 ){
20462 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
20463 return 0;
@@ -20458,11 +20490,11 @@
20490 }
20491 sqlite3_mutex_leave(mem0.mutex);
20492 }else{
20493 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
20494 }
20495 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
20496 return pNew;
20497 }
20498
20499 /*
20500 ** The public interface to sqlite3Realloc. Make sure that the memory
@@ -20470,11 +20502,11 @@
20502 */
20503 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
20504 #ifndef SQLITE_OMIT_AUTOINIT
20505 if( sqlite3_initialize() ) return 0;
20506 #endif
20507 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
20508 return sqlite3Realloc(pOld, n);
20509 }
20510 SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
20511 #ifndef SQLITE_OMIT_AUTOINIT
20512 if( sqlite3_initialize() ) return 0;
@@ -21757,10 +21789,73 @@
21789 fprintf(stdout,"%s", zBuf);
21790 fflush(stdout);
21791 }
21792 #endif
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
21857 /*
21858 ** variable-argument wrapper around sqlite3VXPrintf().
21859 */
21860 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
21861 va_list ap;
@@ -29659,11 +29754,11 @@
29754 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
29755 **
29756 ** * An I/O method finder function called FINDER that returns a pointer
29757 ** to the METHOD object in the previous bullet.
29758 */
29759 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \
29760 static const sqlite3_io_methods METHOD = { \
29761 VERSION, /* iVersion */ \
29762 CLOSE, /* xClose */ \
29763 unixRead, /* xRead */ \
29764 unixWrite, /* xWrite */ \
@@ -29674,11 +29769,11 @@
29769 UNLOCK, /* xUnlock */ \
29770 CKLOCK, /* xCheckReservedLock */ \
29771 unixFileControl, /* xFileControl */ \
29772 unixSectorSize, /* xSectorSize */ \
29773 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
29774 SHMMAP, /* xShmMap */ \
29775 unixShmLock, /* xShmLock */ \
29776 unixShmBarrier, /* xShmBarrier */ \
29777 unixShmUnmap, /* xShmUnmap */ \
29778 unixFetch, /* xFetch */ \
29779 unixUnfetch, /* xUnfetch */ \
@@ -29700,29 +29795,32 @@
29795 posixIoMethods, /* sqlite3_io_methods object name */
29796 3, /* shared memory and mmap are enabled */
29797 unixClose, /* xClose method */
29798 unixLock, /* xLock method */
29799 unixUnlock, /* xUnlock method */
29800 unixCheckReservedLock, /* xCheckReservedLock method */
29801 unixShmMap /* xShmMap method */
29802 )
29803 IOMETHODS(
29804 nolockIoFinder, /* Finder function name */
29805 nolockIoMethods, /* sqlite3_io_methods object name */
29806 3, /* shared memory is disabled */
29807 nolockClose, /* xClose method */
29808 nolockLock, /* xLock method */
29809 nolockUnlock, /* xUnlock method */
29810 nolockCheckReservedLock, /* xCheckReservedLock method */
29811 0 /* xShmMap method */
29812 )
29813 IOMETHODS(
29814 dotlockIoFinder, /* Finder function name */
29815 dotlockIoMethods, /* sqlite3_io_methods object name */
29816 1, /* shared memory is disabled */
29817 dotlockClose, /* xClose method */
29818 dotlockLock, /* xLock method */
29819 dotlockUnlock, /* xUnlock method */
29820 dotlockCheckReservedLock, /* xCheckReservedLock method */
29821 0 /* xShmMap method */
29822 )
29823
29824 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
29825 IOMETHODS(
29826 flockIoFinder, /* Finder function name */
@@ -29729,11 +29827,12 @@
29827 flockIoMethods, /* sqlite3_io_methods object name */
29828 1, /* shared memory is disabled */
29829 flockClose, /* xClose method */
29830 flockLock, /* xLock method */
29831 flockUnlock, /* xUnlock method */
29832 flockCheckReservedLock, /* xCheckReservedLock method */
29833 0 /* xShmMap method */
29834 )
29835 #endif
29836
29837 #if OS_VXWORKS
29838 IOMETHODS(
@@ -29741,11 +29840,12 @@
29840 semIoMethods, /* sqlite3_io_methods object name */
29841 1, /* shared memory is disabled */
29842 semClose, /* xClose method */
29843 semLock, /* xLock method */
29844 semUnlock, /* xUnlock method */
29845 semCheckReservedLock, /* xCheckReservedLock method */
29846 0 /* xShmMap method */
29847 )
29848 #endif
29849
29850 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29851 IOMETHODS(
@@ -29753,11 +29853,12 @@
29853 afpIoMethods, /* sqlite3_io_methods object name */
29854 1, /* shared memory is disabled */
29855 afpClose, /* xClose method */
29856 afpLock, /* xLock method */
29857 afpUnlock, /* xUnlock method */
29858 afpCheckReservedLock, /* xCheckReservedLock method */
29859 0 /* xShmMap method */
29860 )
29861 #endif
29862
29863 /*
29864 ** The proxy locking method is a "super-method" in the sense that it
@@ -29778,11 +29879,12 @@
29879 proxyIoMethods, /* sqlite3_io_methods object name */
29880 1, /* shared memory is disabled */
29881 proxyClose, /* xClose method */
29882 proxyLock, /* xLock method */
29883 proxyUnlock, /* xUnlock method */
29884 proxyCheckReservedLock, /* xCheckReservedLock method */
29885 0 /* xShmMap method */
29886 )
29887 #endif
29888
29889 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
29890 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29791,11 +29893,12 @@
29893 nfsIoMethods, /* sqlite3_io_methods object name */
29894 1, /* shared memory is disabled */
29895 unixClose, /* xClose method */
29896 unixLock, /* xLock method */
29897 nfsUnlock, /* xUnlock method */
29898 unixCheckReservedLock, /* xCheckReservedLock method */
29899 0 /* xShmMap method */
29900 )
29901 #endif
29902
29903 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29904 /*
@@ -51383,13 +51486,14 @@
51486 ** stored in MemPage.pBt->mutex.
51487 */
51488 struct MemPage {
51489 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
51490 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
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 */
51495 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
51496 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
51497 u8 max1bytePayload; /* min(maxLocal,127) */
51498 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
51499 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
@@ -51566,16 +51670,14 @@
51670 ** about a cell. The parseCellPtr() function fills in this structure
51671 ** based on information extract from the raw disk page.
51672 */
51673 typedef struct CellInfo CellInfo;
51674 struct CellInfo {
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 */
 
 
51679 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
51680 u16 nSize; /* Size of the cell content on the main b-tree page */
51681 };
51682
51683 /*
@@ -51768,10 +51870,12 @@
51870 u8 *aPgRef; /* 1 bit per page in the db (see above) */
51871 Pgno nPage; /* Number of pages in the database */
51872 int mxErr; /* Stop accumulating errors when this reaches zero */
51873 int nErr; /* Number of messages written to zErrMsg so far */
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 */
51877 StrAccum errMsg; /* Accumulate the error message text here */
51878 };
51879
51880 /*
51881 ** Routines to read or write a two- and four-byte big-endian integer values.
@@ -52554,11 +52658,13 @@
52658 ){
52659 BtCursor *p;
52660 BtShared *pBt = pBtree->pBt;
52661 assert( sqlite3BtreeHoldsMutex(pBtree) );
52662 for(p=pBt->pCursor; p; p=p->pNext){
52663 if( (p->curFlags & BTCF_Incrblob)!=0
52664 && (isClearTable || p->info.nKey==iRow)
52665 ){
52666 p->eState = CURSOR_INVALID;
52667 }
52668 }
52669 }
52670
@@ -52727,13 +52833,13 @@
52833 ** the cursors if and when a cursor is found that actually requires saving.
52834 ** The common case is that no cursors need to be saved, so this routine is
52835 ** broken out from its caller to avoid unnecessary stack pointer movement.
52836 */
52837 static int SQLITE_NOINLINE saveCursorsOnList(
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 */
52841 ){
52842 do{
52843 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
52844 if( p->eState==CURSOR_VALID ){
52845 int rc = saveCursorPosition(p);
@@ -53035,51 +53141,48 @@
53141 /*
53142 ** Parse a cell content block and fill in the CellInfo structure. There
53143 ** are two versions of this function. btreeParseCell() takes a
53144 ** cell index as the second argument and btreeParseCellPtr()
53145 ** takes a pointer to the body of the cell as its second argument.
 
 
 
53146 */
53147 static void btreeParseCellPtr(
53148 MemPage *pPage, /* Page containing the cell */
53149 u8 *pCell, /* Pointer to the cell text. */
53150 CellInfo *pInfo /* Fill in this structure */
53151 ){
53152 u8 *pIter; /* For scanning through pCell */
53153 u32 nPayload; /* Number of bytes of cell payload */
53154
53155 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
 
53156 assert( pPage->leaf==0 || pPage->leaf==1 );
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);
53172 pInfo->nKey = nPayload;
53173 }
53174 pInfo->nPayload = nPayload;
53175 pInfo->pPayload = pIter;
53176 testcase( nPayload==pPage->maxLocal );
53177 testcase( nPayload==pPage->maxLocal+1 );
53178 if( nPayload<=pPage->maxLocal ){
53179 /* This is the (easy) common case where the entire payload fits
53180 ** on the local page. No overflow is required.
53181 */
53182 pInfo->nSize = nPayload + (u16)(pIter - pCell);
53183 if( pInfo->nSize<4 ) pInfo->nSize = 4;
53184 pInfo->nLocal = (u16)nPayload;
53185 pInfo->iOverflow = 0;
53186 }else{
53187 /* If the payload will not fit completely on the local page, we have
53188 ** to decide how much to store locally and how much to spill onto
@@ -53102,33 +53205,32 @@
53205 if( surplus <= maxLocal ){
53206 pInfo->nLocal = (u16)surplus;
53207 }else{
53208 pInfo->nLocal = (u16)minLocal;
53209 }
53210 pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
53211 pInfo->nSize = pInfo->iOverflow + 4;
53212 }
53213 }
 
 
53214 static void btreeParseCell(
53215 MemPage *pPage, /* Page containing the cell */
53216 int iCell, /* The cell index. First cell is 0 */
53217 CellInfo *pInfo /* Fill in this structure */
53218 ){
53219 btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
53220 }
53221
53222 /*
53223 ** Compute the total number of bytes that a Cell needs in the cell
53224 ** data area of the btree-page. The return number includes the cell
53225 ** data header and the local payload, but not any overflow page or
53226 ** the space used by the cell pointer.
53227 */
53228 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
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 */
53232
53233 #ifdef SQLITE_DEBUG
53234 /* The value returned by this function should always be the same as
53235 ** the (CellInfo.nSize) value found by doing a full parse of the
53236 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
@@ -53135,47 +53237,48 @@
53237 ** this function verifies that this invariant is not violated. */
53238 CellInfo debuginfo;
53239 btreeParseCellPtr(pPage, pCell, &debuginfo);
53240 #endif
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++;
53257 if( pPage->intKey ){
 
 
 
 
 
 
 
53258 /* pIter now points at the 64-bit integer key value, a variable length
53259 ** integer. The following block moves pIter to point at the first byte
53260 ** past the end of the key value. */
53261 pEnd = &pIter[9];
53262 while( (*pIter++)&0x80 && pIter<pEnd );
 
 
53263 }
 
53264 testcase( nSize==pPage->maxLocal );
53265 testcase( nSize==pPage->maxLocal+1 );
53266 if( nSize<=pPage->maxLocal ){
53267 nSize += (u32)(pIter - pCell);
53268 if( nSize<4 ) nSize = 4;
53269 }else{
53270 int minLocal = pPage->minLocal;
53271 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
53272 testcase( nSize==pPage->maxLocal );
53273 testcase( nSize==pPage->maxLocal+1 );
53274 if( nSize>pPage->maxLocal ){
53275 nSize = minLocal;
53276 }
53277 nSize += 4 + (u16)(pIter - pCell);
53278 }
53279 assert( nSize==debuginfo.nSize || CORRUPT_DB );
 
 
 
 
 
 
 
53280 return (u16)nSize;
53281 }
53282
53283 #ifdef SQLITE_DEBUG
53284 /* This variation on cellSizePtr() is used inside of assert() statements
@@ -53194,11 +53297,10 @@
53297 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
53298 CellInfo info;
53299 if( *pRC ) return;
53300 assert( pCell!=0 );
53301 btreeParseCellPtr(pPage, pCell, &info);
 
53302 if( info.iOverflow ){
53303 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
53304 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
53305 }
53306 }
@@ -53407,11 +53509,11 @@
53509 ** does it detect cells or freeblocks that encrouch into the reserved bytes
53510 ** at the end of the page. So do additional corruption checks inside this
53511 ** routine and return SQLITE_CORRUPT if any problems are found.
53512 */
53513 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
53514 u16 iPtr; /* Address of ptr to next freeblock */
53515 u16 iFreeBlk; /* Address of the next freeblock */
53516 u8 hdr; /* Page header size. 0 or 100 */
53517 u8 nFrag = 0; /* Reduction in fragmentation */
53518 u16 iOrigSize = iSize; /* Original value of iSize */
53519 u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
@@ -53459,13 +53561,13 @@
53561 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
53562 iSize = iEnd - iStart;
53563 iFreeBlk = get2byte(&data[iFreeBlk]);
53564 }
53565
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.
53569 */
53570 if( iPtr>hdr+1 ){
53571 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
53572 if( iPtrEnd+3>=iStart ){
53573 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
@@ -53515,16 +53617,18 @@
53617 flagByte &= ~PTF_LEAF;
53618 pPage->childPtrSize = 4-4*pPage->leaf;
53619 pBt = pPage->pBt;
53620 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
53621 pPage->intKey = 1;
53622 pPage->intKeyLeaf = pPage->leaf;
53623 pPage->noPayload = !pPage->leaf;
53624 pPage->maxLocal = pBt->maxLeaf;
53625 pPage->minLocal = pBt->minLeaf;
53626 }else if( flagByte==PTF_ZERODATA ){
53627 pPage->intKey = 0;
53628 pPage->intKeyLeaf = 0;
53629 pPage->noPayload = 0;
53630 pPage->maxLocal = pBt->maxLocal;
53631 pPage->minLocal = pBt->minLocal;
53632 }else{
53633 return SQLITE_CORRUPT_BKPT;
53634 }
@@ -54694,15 +54798,15 @@
54798 */
54799 static void unlockBtreeIfUnused(BtShared *pBt){
54800 assert( sqlite3_mutex_held(pBt->mutex) );
54801 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
54802 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
54803 MemPage *pPage1 = pBt->pPage1;
54804 assert( pPage1->aData );
54805 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
 
 
54806 pBt->pPage1 = 0;
54807 releasePage(pPage1);
54808 }
54809 }
54810
54811 /*
54812 ** If pBt points to an empty file then convert that empty file
@@ -55739,10 +55843,14 @@
55843 assert( pBt->pPage1 && pBt->pPage1->aData );
55844
55845 if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
55846 return SQLITE_READONLY;
55847 }
55848 if( wrFlag ){
55849 allocateTempSpace(pBt);
55850 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
55851 }
55852 if( iTable==1 && btreePagecount(pBt)==0 ){
55853 assert( wrFlag==0 );
55854 iTable = 0;
55855 }
55856
@@ -55928,12 +56036,13 @@
56036 ** to return an integer result code for historical reasons.
56037 */
56038 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
56039 assert( cursorHoldsMutex(pCur) );
56040 assert( pCur->eState==CURSOR_VALID );
56041 assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
56042 getCellInfo(pCur);
56043 *pSize = pCur->info.nPayload;
56044 return SQLITE_OK;
56045 }
56046
56047 /*
56048 ** Given the page number of an overflow page in the database (parameter
@@ -56080,34 +56189,32 @@
56189 unsigned char *pBuf, /* Write the bytes into this buffer */
56190 int eOp /* zero to read. non-zero to write. */
56191 ){
56192 unsigned char *aPayload;
56193 int rc = SQLITE_OK;
 
56194 int iIdx = 0;
56195 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
56196 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
56197 #ifdef SQLITE_DIRECT_OVERFLOW_READ
56198 unsigned char * const pBufStart = pBuf;
56199 int bEnd; /* True if reading to end of data */
56200 #endif
56201
56202 assert( pPage );
56203 assert( pCur->eState==CURSOR_VALID );
56204 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
56205 assert( cursorHoldsMutex(pCur) );
56206 assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */
56207
56208 getCellInfo(pCur);
56209 aPayload = pCur->info.pPayload;
 
56210 #ifdef SQLITE_DIRECT_OVERFLOW_READ
56211 bEnd = offset+amt==pCur->info.nPayload;
56212 #endif
56213 assert( offset+amt <= pCur->info.nPayload );
56214
56215 if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
 
 
56216 /* Trying to read or write past the end of the data is an error */
56217 return SQLITE_CORRUPT_BKPT;
56218 }
56219
56220 /* Check if data must be read/written to/from the btree page itself. */
@@ -56159,11 +56266,13 @@
56266
56267 /* If the overflow page-list cache has been allocated and the
56268 ** entry for the first required overflow page is valid, skip
56269 ** directly to it.
56270 */
56271 if( (pCur->curFlags & BTCF_ValidOvfl)!=0
56272 && pCur->aOverflow[offset/ovflSize]
56273 ){
56274 iIdx = (offset/ovflSize);
56275 nextPage = pCur->aOverflow[iIdx];
56276 offset = (offset%ovflSize);
56277 }
56278
@@ -56212,10 +56321,11 @@
56321 ** 2) data is required from the start of this overflow page, and
56322 ** 3) the database is file-backed, and
56323 ** 4) there is no open write-transaction, and
56324 ** 5) the database is not a WAL database,
56325 ** 6) all data from the page is being read.
56326 ** 7) at least 4 bytes have already been read into the output buffer
56327 **
56328 ** then data can be read directly from the database file into the
56329 ** output buffer, bypassing the page-cache altogether. This speeds
56330 ** up loading large records that span many overflow pages.
56331 */
@@ -56223,13 +56333,15 @@
56333 && offset==0 /* (2) */
56334 && (bEnd || a==ovflSize) /* (6) */
56335 && pBt->inTransaction==TRANS_READ /* (4) */
56336 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
56337 && pBt->pPage1->aData[19]==0x01 /* (5) */
56338 && &pBuf[-4]>=pBufStart /* (7) */
56339 ){
56340 u8 aSave[4];
56341 u8 *aWrite = &pBuf[-4];
56342 assert( aWrite>=pBufStart ); /* hence (7) */
56343 memcpy(aSave, aWrite, 4);
56344 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
56345 nextPage = get4byte(aWrite);
56346 memcpy(aWrite, aSave, 4);
56347 }else
@@ -56337,11 +56449,11 @@
56449 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
56450 assert( cursorHoldsMutex(pCur) );
56451 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
56452 assert( pCur->info.nSize>0 );
56453 *pAmt = pCur->info.nLocal;
56454 return (void*)pCur->info.pPayload;
56455 }
56456
56457
56458 /*
56459 ** For the entry that cursor pCur is point to, return as
@@ -56765,11 +56877,11 @@
56877 pCur->aiIdx[pCur->iPage] = (u16)idx;
56878 if( xRecordCompare==0 ){
56879 for(;;){
56880 i64 nCellKey;
56881 pCell = findCell(pPage, idx) + pPage->childPtrSize;
56882 if( pPage->intKeyLeaf ){
56883 while( 0x80 <= *(pCell++) ){
56884 if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
56885 }
56886 }
56887 getVarint(pCell, (u64*)&nCellKey);
@@ -57024,13 +57136,13 @@
57136 ** was already pointing to the first entry in the database before
57137 ** this routine was called, then set *pRes=1.
57138 **
57139 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
57140 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
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.
57144 **
57145 ** The calling function will set *pRes to 0 or 1. The initial *pRes value
57146 ** will be 1 if the cursor being stepped corresponds to an SQL index and
57147 ** if this routine could have been skipped if that SQL index had been
57148 ** a unique index. Otherwise the caller will have set *pRes to zero.
@@ -57048,12 +57160,11 @@
57160 assert( *pRes==0 );
57161 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
57162 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
57163 assert( pCur->info.nSize==0 );
57164 if( pCur->eState!=CURSOR_VALID ){
57165 rc = restoreCursorPosition(pCur);
 
57166 if( rc!=SQLITE_OK ){
57167 return rc;
57168 }
57169 if( CURSOR_INVALID==pCur->eState ){
57170 *pRes = 1;
@@ -57354,11 +57465,11 @@
57465 if( rc ) goto end_allocate_page;
57466 if( closest<k-1 ){
57467 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
57468 }
57469 put4byte(&aData[4], k-1);
57470 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
57471 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
57472 if( rc==SQLITE_OK ){
57473 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
57474 if( rc!=SQLITE_OK ){
57475 releasePage(*ppPage);
@@ -57387,11 +57498,11 @@
57498 ** content for any page that really does lie past the end of the database
57499 ** file on disk. So the effects of disabling the no-content optimization
57500 ** here are confined to those pages that lie between the end of the
57501 ** database image and the end of the database file.
57502 */
57503 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
57504
57505 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
57506 if( rc ) return rc;
57507 pBt->nPage++;
57508 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -57586,22 +57697,29 @@
57697 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
57698 }
57699 }
57700
57701 /*
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.
57705 */
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 ){
57711 BtShared *pBt = pPage->pBt;
57712 CellInfo info;
57713 Pgno ovflPgno;
57714 int rc;
57715 int nOvfl;
57716 u32 ovflPageSize;
57717
57718 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57719 btreeParseCellPtr(pPage, pCell, &info);
57720 *pnSize = info.nSize;
57721 if( info.iOverflow==0 ){
57722 return SQLITE_OK; /* No overflow pages. Return without doing anything */
57723 }
57724 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
57725 return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
@@ -57681,54 +57799,87 @@
57799 unsigned char *pPrior;
57800 unsigned char *pPayload;
57801 BtShared *pBt = pPage->pBt;
57802 Pgno pgnoOvfl = 0;
57803 int nHeader;
 
57804
57805 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57806
57807 /* pPage is not necessarily writeable since pCell might be auxiliary
57808 ** buffer space that is separate from the pPage buffer area */
57809 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
57810 || sqlite3PagerIswriteable(pPage->pDbPage) );
57811
57812 /* Fill in the header. */
57813 nHeader = pPage->childPtrSize;
57814 nPayload = nData + nZero;
57815 if( pPage->intKeyLeaf ){
57816 nHeader += putVarint32(&pCell[nHeader], nPayload);
 
 
57817 }else{
57818 assert( nData==0 );
57819 assert( nZero==0 );
57820 }
57821 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
 
 
 
 
57822
57823 /* Fill in the payload size */
 
57824 if( pPage->intKey ){
57825 pSrc = pData;
57826 nSrc = nData;
57827 nData = 0;
57828 }else{
57829 if( NEVER(nKey>0x7fffffff || pKey==0) ){
57830 return SQLITE_CORRUPT_BKPT;
57831 }
57832 nPayload = (int)nKey;
57833 pSrc = pKey;
57834 nSrc = (int)nKey;
57835 }
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 }
57854 pPayload = &pCell[nHeader];
 
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 */
57881 while( nPayload>0 ){
57882 if( spaceLeft==0 ){
57883 #ifndef SQLITE_OMIT_AUTOVACUUM
57884 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
57885 if( pBt->autoVacuum ){
@@ -58432,11 +58583,11 @@
58583 **
58584 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
58585 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
58586 */
58587 leafCorrection = apOld[0]->leaf*4;
58588 leafData = apOld[0]->intKeyLeaf;
58589 for(i=0; i<nOld; i++){
58590 int limit;
58591
58592 /* Before doing anything else, take a copy of the i'th original sibling
58593 ** The rest of this function will use data from the copies rather
@@ -59008,11 +59159,11 @@
59159 int const iIdx = pCur->aiIdx[iPage-1];
59160
59161 rc = sqlite3PagerWrite(pParent->pDbPage);
59162 if( rc==SQLITE_OK ){
59163 #ifndef SQLITE_OMIT_QUICKBALANCE
59164 if( pPage->intKeyLeaf
59165 && pPage->nOverflow==1
59166 && pPage->aiOvfl[0]==pPage->nCell
59167 && pParent->pgno!=1
59168 && pParent->nCell==iIdx
59169 ){
@@ -59127,11 +59278,12 @@
59278 assert( pCur->skipNext!=SQLITE_OK );
59279 return pCur->skipNext;
59280 }
59281
59282 assert( cursorHoldsMutex(pCur) );
59283 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
59284 && pBt->inTransaction==TRANS_WRITE
59285 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
59286 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
59287
59288 /* Assert that the caller has been consistent. If this cursor was opened
59289 ** expecting an index b-tree, then the caller should be inserting blob
@@ -59160,11 +59312,12 @@
59312 invalidateIncrblobCursors(p, nKey, 0);
59313
59314 /* If the cursor is currently on the last row and we are appending a
59315 ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
59316 ** call */
59317 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
59318 && pCur->info.nKey==nKey-1 ){
59319 loc = -1;
59320 }
59321 }
59322
59323 if( !loc ){
@@ -59179,13 +59332,12 @@
59332
59333 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
59334 pCur->pgnoRoot, nKey, nData, pPage->pgno,
59335 loc==0 ? "overwrite" : "new entry"));
59336 assert( pPage->isInit );
 
59337 newCell = pBt->pTmpSpace;
59338 assert( newCell!=0 );
59339 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
59340 if( rc ) goto end_insert;
59341 assert( szNew==cellSizePtr(pPage, newCell) );
59342 assert( szNew <= MX_CELL_SIZE(pBt) );
59343 idx = pCur->aiIdx[pCur->iPage];
@@ -59198,12 +59350,11 @@
59350 }
59351 oldCell = findCell(pPage, idx);
59352 if( !pPage->leaf ){
59353 memcpy(newCell, oldCell, 4);
59354 }
59355 rc = clearCell(pPage, oldCell, &szOld);
 
59356 dropCell(pPage, idx, szOld, &rc);
59357 if( rc ) goto end_insert;
59358 }else if( loc<0 && pPage->nCell>0 ){
59359 assert( pPage->leaf );
59360 idx = ++pCur->aiIdx[pCur->iPage];
@@ -59261,10 +59412,11 @@
59412 int rc; /* Return code */
59413 MemPage *pPage; /* Page to delete cell from */
59414 unsigned char *pCell; /* Pointer to cell to delete */
59415 int iCellIdx; /* Index of cell to delete */
59416 int iCellDepth; /* Depth of node containing pCell */
59417 u16 szCell; /* Size of the cell being deleted */
59418
59419 assert( cursorHoldsMutex(pCur) );
59420 assert( pBt->inTransaction==TRANS_WRITE );
59421 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
59422 assert( pCur->curFlags & BTCF_WriteFlag );
@@ -59309,12 +59461,12 @@
59461 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
59462 }
59463
59464 rc = sqlite3PagerWrite(pPage->pDbPage);
59465 if( rc ) return rc;
59466 rc = clearCell(pPage, pCell, &szCell);
59467 dropCell(pPage, iCellIdx, szCell, &rc);
59468 if( rc ) return rc;
59469
59470 /* If the cell deleted was not located on a leaf page, then the cursor
59471 ** is currently pointing to the largest entry in the sub-tree headed
59472 ** by the child-page of the cell that was just deleted from an internal
@@ -59327,14 +59479,12 @@
59479 unsigned char *pTmp;
59480
59481 pCell = findCell(pLeaf, pLeaf->nCell-1);
59482 nCell = cellSizePtr(pLeaf, pCell);
59483 assert( MX_CELL_SIZE(pBt) >= nCell );
 
 
59484 pTmp = pBt->pTmpSpace;
59485 assert( pTmp!=0 );
59486 rc = sqlite3PagerWrite(pLeaf->pDbPage);
59487 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
59488 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
59489 if( rc ) return rc;
59490 }
@@ -59542,10 +59692,11 @@
59692 MemPage *pPage;
59693 int rc;
59694 unsigned char *pCell;
59695 int i;
59696 int hdr;
59697 u16 szCell;
59698
59699 assert( sqlite3_mutex_held(pBt->mutex) );
59700 if( pgno>btreePagecount(pBt) ){
59701 return SQLITE_CORRUPT_BKPT;
59702 }
@@ -59557,11 +59708,11 @@
59708 pCell = findCell(pPage, i);
59709 if( !pPage->leaf ){
59710 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
59711 if( rc ) goto cleardatabasepage_out;
59712 }
59713 rc = clearCell(pPage, pCell, &szCell);
59714 if( rc ) goto cleardatabasepage_out;
59715 }
59716 if( !pPage->leaf ){
59717 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
59718 if( rc ) goto cleardatabasepage_out;
@@ -59903,24 +60054,25 @@
60054 /*
60055 ** Append a message to the error message string.
60056 */
60057 static void checkAppendMsg(
60058 IntegrityCk *pCheck,
 
60059 const char *zFormat,
60060 ...
60061 ){
60062 va_list ap;
60063 char zBuf[200];
60064 if( !pCheck->mxErr ) return;
60065 pCheck->mxErr--;
60066 pCheck->nErr++;
60067 va_start(ap, zFormat);
60068 if( pCheck->errMsg.nChar ){
60069 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
60070 }
60071 if( pCheck->zPfx ){
60072 sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2);
60073 sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf);
60074 }
60075 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
60076 va_end(ap);
60077 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
60078 pCheck->mallocFailed = 1;
@@ -59954,18 +60106,18 @@
60106 ** Return 1 if there are 2 or more references to the page and 0 if
60107 ** if this is the first reference to the page.
60108 **
60109 ** Also check that the page number is in bounds.
60110 */
60111 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
60112 if( iPage==0 ) return 1;
60113 if( iPage>pCheck->nPage ){
60114 checkAppendMsg(pCheck, "invalid page number %d", iPage);
60115 return 1;
60116 }
60117 if( getPageReferenced(pCheck, iPage) ){
60118 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
60119 return 1;
60120 }
60121 setPageReferenced(pCheck, iPage);
60122 return 0;
60123 }
@@ -59978,26 +60130,25 @@
60130 */
60131 static void checkPtrmap(
60132 IntegrityCk *pCheck, /* Integrity check context */
60133 Pgno iChild, /* Child page number */
60134 u8 eType, /* Expected pointer map type */
60135 Pgno iParent /* Expected pointer map parent page number */
 
60136 ){
60137 int rc;
60138 u8 ePtrmapType;
60139 Pgno iPtrmapParent;
60140
60141 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
60142 if( rc!=SQLITE_OK ){
60143 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
60144 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
60145 return;
60146 }
60147
60148 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
60149 checkAppendMsg(pCheck,
60150 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
60151 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
60152 }
60153 }
60154 #endif
@@ -60008,51 +60159,50 @@
60159 */
60160 static void checkList(
60161 IntegrityCk *pCheck, /* Integrity checking context */
60162 int isFreeList, /* True for a freelist. False for overflow page list */
60163 int iPage, /* Page number for first page in the list */
60164 int N /* Expected number of pages in the list */
 
60165 ){
60166 int i;
60167 int expected = N;
60168 int iFirst = iPage;
60169 while( N-- > 0 && pCheck->mxErr ){
60170 DbPage *pOvflPage;
60171 unsigned char *pOvflData;
60172 if( iPage<1 ){
60173 checkAppendMsg(pCheck,
60174 "%d of %d pages missing from overflow list starting at %d",
60175 N+1, expected, iFirst);
60176 break;
60177 }
60178 if( checkRef(pCheck, iPage) ) break;
60179 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
60180 checkAppendMsg(pCheck, "failed to get page %d", iPage);
60181 break;
60182 }
60183 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
60184 if( isFreeList ){
60185 int n = get4byte(&pOvflData[4]);
60186 #ifndef SQLITE_OMIT_AUTOVACUUM
60187 if( pCheck->pBt->autoVacuum ){
60188 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
60189 }
60190 #endif
60191 if( n>(int)pCheck->pBt->usableSize/4-2 ){
60192 checkAppendMsg(pCheck,
60193 "freelist leaf count too big on page %d", iPage);
60194 N--;
60195 }else{
60196 for(i=0; i<n; i++){
60197 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
60198 #ifndef SQLITE_OMIT_AUTOVACUUM
60199 if( pCheck->pBt->autoVacuum ){
60200 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
60201 }
60202 #endif
60203 checkRef(pCheck, iFreePage);
60204 }
60205 N -= n;
60206 }
60207 }
60208 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -60061,11 +60211,11 @@
60211 ** page in this overflow list, check that the pointer-map entry for
60212 ** the following page matches iPage.
60213 */
60214 if( pCheck->pBt->autoVacuum && N>0 ){
60215 i = get4byte(pOvflData);
60216 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
60217 }
60218 }
60219 #endif
60220 iPage = get4byte(pOvflData);
60221 sqlite3PagerUnref(pOvflPage);
@@ -60093,11 +60243,10 @@
60243 ** the root of the tree.
60244 */
60245 static int checkTreePage(
60246 IntegrityCk *pCheck, /* Context for the sanity check */
60247 int iPage, /* Page number of the page to check */
 
60248 i64 *pnParentMinKey,
60249 i64 *pnParentMaxKey
60250 ){
60251 MemPage *pPage;
60252 int i, rc, depth, d2, pgno, cnt;
@@ -60104,38 +60253,42 @@
60253 int hdr, cellStart;
60254 int nCell;
60255 u8 *data;
60256 BtShared *pBt;
60257 int usableSize;
 
60258 char *hit = 0;
60259 i64 nMinKey = 0;
60260 i64 nMaxKey = 0;
60261 const char *saved_zPfx = pCheck->zPfx;
60262 int saved_v1 = pCheck->v1;
60263 int saved_v2 = pCheck->v2;
60264
60265 /* Check that the page exists
60266 */
60267 pBt = pCheck->pBt;
60268 usableSize = pBt->usableSize;
60269 if( iPage==0 ) return 0;
60270 if( checkRef(pCheck, iPage) ) return 0;
60271 pCheck->zPfx = "Page %d: ";
60272 pCheck->v1 = iPage;
60273 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
60274 checkAppendMsg(pCheck,
60275 "unable to get the page. error code=%d", rc);
60276 depth = -1;
60277 goto end_of_check;
60278 }
60279
60280 /* Clear MemPage.isInit to make sure the corruption detection code in
60281 ** btreeInitPage() is executed. */
60282 pPage->isInit = 0;
60283 if( (rc = btreeInitPage(pPage))!=0 ){
60284 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
60285 checkAppendMsg(pCheck,
60286 "btreeInitPage() returns error code %d", rc);
60287 releasePage(pPage);
60288 depth = -1;
60289 goto end_of_check;
60290 }
60291
60292 /* Check out all the cells.
60293 */
60294 depth = 0;
@@ -60144,99 +60297,101 @@
60297 u32 sz;
60298 CellInfo info;
60299
60300 /* Check payload overflow pages
60301 */
60302 pCheck->zPfx = "On tree page %d cell %d: ";
60303 pCheck->v1 = iPage;
60304 pCheck->v2 = i;
60305 pCell = findCell(pPage,i);
60306 btreeParseCellPtr(pPage, pCell, &info);
60307 sz = info.nPayload;
 
60308 /* For intKey pages, check that the keys are in order.
60309 */
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);
60316 }
60317 nMaxKey = info.nKey;
60318 }
 
60319 if( (sz>info.nLocal)
60320 && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
60321 ){
60322 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
60323 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
60324 #ifndef SQLITE_OMIT_AUTOVACUUM
60325 if( pBt->autoVacuum ){
60326 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
60327 }
60328 #endif
60329 checkList(pCheck, 0, pgnoOvfl, nPage);
60330 }
60331
60332 /* Check sanity of left child page.
60333 */
60334 if( !pPage->leaf ){
60335 pgno = get4byte(pCell);
60336 #ifndef SQLITE_OMIT_AUTOVACUUM
60337 if( pBt->autoVacuum ){
60338 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
60339 }
60340 #endif
60341 d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey);
60342 if( i>0 && d2!=depth ){
60343 checkAppendMsg(pCheck, "Child page depth differs");
60344 }
60345 depth = d2;
60346 }
60347 }
60348
60349 if( !pPage->leaf ){
60350 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
60351 pCheck->zPfx = "On page %d at right child: ";
60352 pCheck->v1 = iPage;
60353 #ifndef SQLITE_OMIT_AUTOVACUUM
60354 if( pBt->autoVacuum ){
60355 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
60356 }
60357 #endif
60358 checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey);
60359 }
60360
60361 /* For intKey leaf pages, check that the min/max keys are in order
60362 ** with any left/parent/right pages.
60363 */
60364 pCheck->zPfx = "Page %d: ";
60365 pCheck->v1 = iPage;
60366 if( pPage->leaf && pPage->intKey ){
60367 /* if we are a left child page */
60368 if( pnParentMinKey ){
60369 /* if we are the left most child page */
60370 if( !pnParentMaxKey ){
60371 if( nMaxKey > *pnParentMinKey ){
60372 checkAppendMsg(pCheck,
60373 "Rowid %lld out of order (max larger than parent min of %lld)",
60374 nMaxKey, *pnParentMinKey);
60375 }
60376 }else{
60377 if( nMinKey <= *pnParentMinKey ){
60378 checkAppendMsg(pCheck,
60379 "Rowid %lld out of order (min less than parent min of %lld)",
60380 nMinKey, *pnParentMinKey);
60381 }
60382 if( nMaxKey > *pnParentMaxKey ){
60383 checkAppendMsg(pCheck,
60384 "Rowid %lld out of order (max larger than parent max of %lld)",
60385 nMaxKey, *pnParentMaxKey);
60386 }
60387 *pnParentMinKey = nMaxKey;
60388 }
60389 /* else if we're a right child page */
60390 } else if( pnParentMaxKey ){
60391 if( nMinKey <= *pnParentMaxKey ){
60392 checkAppendMsg(pCheck,
60393 "Rowid %lld out of order (min less than parent max of %lld)",
60394 nMinKey, *pnParentMaxKey);
60395 }
60396 }
60397 }
@@ -60244,10 +60399,11 @@
60399 /* Check for complete coverage of the page
60400 */
60401 data = pPage->aData;
60402 hdr = pPage->hdrOffset;
60403 hit = sqlite3PageMalloc( pBt->pageSize );
60404 pCheck->zPfx = 0;
60405 if( hit==0 ){
60406 pCheck->mallocFailed = 1;
60407 }else{
60408 int contentOffset = get2byteNotZero(&data[hdr+5]);
60409 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
@@ -60261,11 +60417,12 @@
60417 int j;
60418 if( pc<=usableSize-4 ){
60419 size = cellSizePtr(pPage, &data[pc]);
60420 }
60421 if( (int)(pc+size-1)>=usableSize ){
60422 pCheck->zPfx = 0;
60423 checkAppendMsg(pCheck,
60424 "Corruption detected in cell %d on page %d",i,iPage);
60425 }else{
60426 for(j=pc+size-1; j>=pc; j--) hit[j]++;
60427 }
60428 }
@@ -60283,23 +60440,28 @@
60440 }
60441 for(i=cnt=0; i<usableSize; i++){
60442 if( hit[i]==0 ){
60443 cnt++;
60444 }else if( hit[i]>1 ){
60445 checkAppendMsg(pCheck,
60446 "Multiple uses for byte %d of page %d", i, iPage);
60447 break;
60448 }
60449 }
60450 if( cnt!=data[hdr+7] ){
60451 checkAppendMsg(pCheck,
60452 "Fragmentation of %d bytes reported as %d on page %d",
60453 cnt, data[hdr+7], iPage);
60454 }
60455 }
60456 sqlite3PageFree(hit);
60457 releasePage(pPage);
60458
60459 end_of_check:
60460 pCheck->zPfx = saved_zPfx;
60461 pCheck->v1 = saved_v1;
60462 pCheck->v2 = saved_v2;
60463 return depth+1;
60464 }
60465 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
60466
60467 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
@@ -60336,10 +60498,13 @@
60498 sCheck.pPager = pBt->pPager;
60499 sCheck.nPage = btreePagecount(sCheck.pBt);
60500 sCheck.mxErr = mxErr;
60501 sCheck.nErr = 0;
60502 sCheck.mallocFailed = 0;
60503 sCheck.zPfx = 0;
60504 sCheck.v1 = 0;
60505 sCheck.v2 = 0;
60506 *pnErr = 0;
60507 if( sCheck.nPage==0 ){
60508 sqlite3BtreeLeave(p);
60509 return 0;
60510 }
@@ -60355,53 +60520,57 @@
60520 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
60521 sCheck.errMsg.useMalloc = 2;
60522
60523 /* Check the integrity of the freelist
60524 */
60525 sCheck.zPfx = "Main freelist: ";
60526 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
60527 get4byte(&pBt->pPage1->aData[36]));
60528 sCheck.zPfx = 0;
60529
60530 /* Check all the tables.
60531 */
60532 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
60533 if( aRoot[i]==0 ) continue;
60534 #ifndef SQLITE_OMIT_AUTOVACUUM
60535 if( pBt->autoVacuum && aRoot[i]>1 ){
60536 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
60537 }
60538 #endif
60539 sCheck.zPfx = "List of tree roots: ";
60540 checkTreePage(&sCheck, aRoot[i], NULL, NULL);
60541 sCheck.zPfx = 0;
60542 }
60543
60544 /* Make sure every page in the file is referenced
60545 */
60546 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
60547 #ifdef SQLITE_OMIT_AUTOVACUUM
60548 if( getPageReferenced(&sCheck, i)==0 ){
60549 checkAppendMsg(&sCheck, "Page %d is never used", i);
60550 }
60551 #else
60552 /* If the database supports auto-vacuum, make sure no tables contain
60553 ** references to pointer-map pages.
60554 */
60555 if( getPageReferenced(&sCheck, i)==0 &&
60556 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
60557 checkAppendMsg(&sCheck, "Page %d is never used", i);
60558 }
60559 if( getPageReferenced(&sCheck, i)!=0 &&
60560 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
60561 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
60562 }
60563 #endif
60564 }
60565
60566 /* Make sure this analysis did not leave any unref() pages.
60567 ** This is an internal consistency check; an integrity check
60568 ** of the integrity check.
60569 */
60570 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
60571 checkAppendMsg(&sCheck,
60572 "Outstanding page count goes from %d to %d during this analysis",
60573 nRef, sqlite3PagerRefcount(pBt->pPager)
60574 );
60575 }
60576
@@ -60593,11 +60762,11 @@
60762
60763 /* Save the positions of all other cursors open on this table. This is
60764 ** required in case any of them are holding references to an xFetch
60765 ** version of the b-tree page modified by the accessPayload call below.
60766 **
60767 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
60768 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
60769 ** saveAllCursors can only return SQLITE_OK.
60770 */
60771 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
60772 assert( rc==SQLITE_OK );
@@ -63751,11 +63920,12 @@
63920 if( addr==p->nOp-1 ) p->nOp--;
63921 }
63922 }
63923
63924 /*
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.
63927 */
63928 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
63929 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
63930 sqlite3VdbeChangeToNoop(p, p->nOp-1);
63931 return 1;
@@ -65676,14 +65846,10 @@
65846 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
65847 vdbeFreeOpArray(db, p->aOp, p->nOp);
65848 sqlite3DbFree(db, p->aColName);
65849 sqlite3DbFree(db, p->zSql);
65850 sqlite3DbFree(db, p->pFree);
 
 
 
 
65851 }
65852
65853 /*
65854 ** Delete an entire VDBE.
65855 */
@@ -67390,10 +67556,11 @@
67556 void (*xDel)(void *),
67557 unsigned char enc
67558 ){
67559 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
67560 assert( xDel!=SQLITE_DYNAMIC );
67561 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
67562 if( n>0x7fffffff ){
67563 (void)invokeValueDestructor(z, xDel, pCtx);
67564 }else{
67565 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
67566 }
@@ -68712,125 +68879,10 @@
68879 return sqlite3StrAccumFinish(&out);
68880 }
68881
68882 #endif /* #ifndef SQLITE_OMIT_TRACE */
68883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68884 /************** End of vdbetrace.c *******************************************/
68885 /************** Begin file vdbe.c ********************************************/
68886 /*
68887 ** 2001 September 15
68888 **
@@ -70475,21 +70527,14 @@
70527 assert( pOp->p4type==P4_FUNCDEF );
70528 ctx.pFunc = pOp->p4.pFunc;
70529 ctx.iOp = pc;
70530 ctx.pVdbe = p;
70531 MemSetTypeFlag(ctx.pOut, MEM_Null);
 
70532 ctx.fErrorOrAux = 0;
70533 assert( db->lastRowid==lastRowid );
 
 
 
 
 
 
70534 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
70535 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
70536
70537 /* If the function returned an error, throw an exception */
70538 if( ctx.fErrorOrAux ){
70539 if( ctx.isError ){
70540 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
@@ -72205,14 +72250,10 @@
72250 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
72251 pCur->pKeyInfo = pKeyInfo;
72252 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
72253 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
72254
 
 
 
 
72255 /* Set the VdbeCursor.isTable variable. Previous versions of
72256 ** SQLite used to check if the root-page flags were sane at this point
72257 ** and report database corruption if they were not, but this check has
72258 ** since moved into the btree layer. */
72259 pCur->isTable = pOp->p4type!=P4_KEYINFO;
@@ -72942,29 +72983,18 @@
72983 ** largest possible integer (9223372036854775807) then the database
72984 ** engine starts picking positive candidate ROWIDs at random until
72985 ** it finds one that is not previously used. */
72986 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
72987 ** an AUTOINCREMENT table. */
 
 
 
 
72988 cnt = 0;
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,
72993 0, &res))==SQLITE_OK)
72994 && (res==0)
72995 && (++cnt<100));
 
 
 
 
 
 
 
 
 
 
72996 if( rc==SQLITE_OK && res==0 ){
72997 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
72998 goto abort_due_to_error;
72999 }
73000 assert( v>0 ); /* EV: R-40812-03570 */
@@ -74556,18 +74586,13 @@
74586 ctx.pMem = pMem = &aMem[pOp->p3];
74587 pMem->n++;
74588 sqlite3VdbeMemInit(&t, db, MEM_Null);
74589 ctx.pOut = &t;
74590 ctx.isError = 0;
74591 ctx.pVdbe = p;
74592 ctx.iOp = pc;
74593 ctx.skipFlag = 0;
 
 
 
 
 
 
74594 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
74595 if( ctx.isError ){
74596 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
74597 rc = ctx.isError;
74598 }
@@ -81597,10 +81622,11 @@
81622 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
81623 pNew->addrOpenEphm[0] = -1;
81624 pNew->addrOpenEphm[1] = -1;
81625 pNew->nSelectRow = p->nSelectRow;
81626 pNew->pWith = withDup(db, p->pWith);
81627 sqlite3SelectSetName(pNew, p->zSelName);
81628 return pNew;
81629 }
81630 #else
81631 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
81632 assert( p==0 );
@@ -81739,36 +81765,44 @@
81765 }
81766
81767 /*
81768 ** These routines are Walker callbacks. Walker.u.pi is a pointer
81769 ** to an integer. These routines are checking an expression to see
81770 ** if it is a constant. Set *Walker.u.i to 0 if the expression is
81771 ** not constant.
81772 **
81773 ** These callback routines are used to implement the following:
81774 **
81775 ** sqlite3ExprIsConstant() pWalker->u.i==1
81776 ** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2
81777 ** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4
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.
81787 */
81788 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
81789
81790 /* If pWalker->u.i is 2 then any term of the expression that comes from
81791 ** the ON or USING clauses of a join disqualifies the expression
81792 ** from being considered constant. */
81793 if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
81794 pWalker->u.i = 0;
81795 return WRC_Abort;
81796 }
81797
81798 switch( pExpr->op ){
81799 /* Consider functions to be constant if all their arguments are constant
81800 ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST
81801 ** flag. */
81802 case TK_FUNCTION:
81803 if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){
81804 return WRC_Continue;
81805 }
81806 /* Fall through */
81807 case TK_ID:
81808 case TK_COLUMN:
@@ -81778,10 +81812,23 @@
81812 testcase( pExpr->op==TK_COLUMN );
81813 testcase( pExpr->op==TK_AGG_FUNCTION );
81814 testcase( pExpr->op==TK_AGG_COLUMN );
81815 pWalker->u.i = 0;
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 */
81830 default:
81831 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
81832 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
81833 return WRC_Continue;
81834 }
@@ -81818,11 +81865,11 @@
81865 ** that does no originate from the ON or USING clauses of a join.
81866 ** Return 0 if it involves variables or function calls or terms from
81867 ** an ON or USING clause.
81868 */
81869 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
81870 return exprIsConst(p, 2);
81871 }
81872
81873 /*
81874 ** Walk an expression tree. Return 1 if the expression is constant
81875 ** or a function call with constant arguments. Return and 0 if there
@@ -81830,12 +81877,13 @@
81877 **
81878 ** For the purposes of this function, a double-quoted string (ex: "abc")
81879 ** is considered a variable but a single-quoted string (ex: 'abc') is
81880 ** a constant.
81881 */
81882 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
81883 assert( isInit==0 || isInit==1 );
81884 return exprIsConst(p, 3+isInit);
81885 }
81886
81887 /*
81888 ** If the expression p codes a constant integer that is small enough
81889 ** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -83741,94 +83789,90 @@
83789 iMem = ++pParse->nMem;
83790 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
83791 exprToRegister(pExpr, iMem);
83792 }
83793
83794 #ifdef SQLITE_DEBUG
83795 /*
83796 ** Generate a human-readable explanation of an expression tree.
83797 */
83798 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
 
83799 const char *zBinOp = 0; /* Binary operator */
83800 const char *zUniOp = 0; /* Unary operator */
83801 pView = sqlite3TreeViewPush(pView, moreToFollow);
83802 if( pExpr==0 ){
83803 sqlite3TreeViewLine(pView, "nil");
83804 sqlite3TreeViewPop(pView);
83805 return;
83806 }
83807 switch( pExpr->op ){
83808 case TK_AGG_COLUMN: {
83809 sqlite3TreeViewLine(pView, "AGG{%d:%d}",
83810 pExpr->iTable, pExpr->iColumn);
83811 break;
83812 }
83813 case TK_COLUMN: {
83814 if( pExpr->iTable<0 ){
83815 /* This only happens when coding check constraints */
83816 sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
83817 }else{
83818 sqlite3TreeViewLine(pView, "{%d:%d}",
83819 pExpr->iTable, pExpr->iColumn);
83820 }
83821 break;
83822 }
83823 case TK_INTEGER: {
83824 if( pExpr->flags & EP_IntValue ){
83825 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
83826 }else{
83827 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
83828 }
83829 break;
83830 }
83831 #ifndef SQLITE_OMIT_FLOATING_POINT
83832 case TK_FLOAT: {
83833 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
83834 break;
83835 }
83836 #endif
83837 case TK_STRING: {
83838 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
83839 break;
83840 }
83841 case TK_NULL: {
83842 sqlite3TreeViewLine(pView,"NULL");
83843 break;
83844 }
83845 #ifndef SQLITE_OMIT_BLOB_LITERAL
83846 case TK_BLOB: {
83847 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
83848 break;
83849 }
83850 #endif
83851 case TK_VARIABLE: {
83852 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
83853 pExpr->u.zToken, pExpr->iColumn);
83854 break;
83855 }
83856 case TK_REGISTER: {
83857 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
83858 break;
83859 }
83860 case TK_AS: {
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);
83867 break;
83868 }
83869 #ifndef SQLITE_OMIT_CAST
83870 case TK_CAST: {
83871 /* Expressions of the form: CAST(pLeft AS token) */
83872 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
83873 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 
 
 
 
 
 
 
 
 
83874 break;
83875 }
83876 #endif /* SQLITE_OMIT_CAST */
83877 case TK_LT: zBinOp = "LT"; break;
83878 case TK_LE: zBinOp = "LE"; break;
@@ -83848,21 +83892,22 @@
83892 case TK_BITOR: zBinOp = "BITOR"; break;
83893 case TK_SLASH: zBinOp = "DIV"; break;
83894 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
83895 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
83896 case TK_CONCAT: zBinOp = "CONCAT"; break;
83897 case TK_DOT: zBinOp = "DOT"; break;
83898
83899 case TK_UMINUS: zUniOp = "UMINUS"; break;
83900 case TK_UPLUS: zUniOp = "UPLUS"; break;
83901 case TK_BITNOT: zUniOp = "BITNOT"; break;
83902 case TK_NOT: zUniOp = "NOT"; break;
83903 case TK_ISNULL: zUniOp = "ISNULL"; break;
83904 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
83905
83906 case TK_COLLATE: {
83907 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
83908 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
83909 break;
83910 }
83911
83912 case TK_AGG_FUNCTION:
83913 case TK_FUNCTION: {
@@ -83870,45 +83915,40 @@
83915 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
83916 pFarg = 0;
83917 }else{
83918 pFarg = pExpr->x.pList;
83919 }
83920 if( pExpr->op==TK_AGG_FUNCTION ){
83921 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
83922 pExpr->op2, pExpr->u.zToken);
83923 }else{
83924 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
83925 }
83926 if( pFarg ){
83927 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
83928 }
 
83929 break;
83930 }
83931 #ifndef SQLITE_OMIT_SUBQUERY
83932 case TK_EXISTS: {
83933 sqlite3TreeViewLine(pView, "EXISTS-expr");
83934 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
 
83935 break;
83936 }
83937 case TK_SELECT: {
83938 sqlite3TreeViewLine(pView, "SELECT-expr");
83939 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
 
83940 break;
83941 }
83942 case TK_IN: {
83943 sqlite3TreeViewLine(pView, "IN");
83944 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
 
83945 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83946 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
83947 }else{
83948 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
83949 }
 
83950 break;
83951 }
83952 #endif /* SQLITE_OMIT_SUBQUERY */
83953
83954 /*
@@ -83924,17 +83964,14 @@
83964 */
83965 case TK_BETWEEN: {
83966 Expr *pX = pExpr->pLeft;
83967 Expr *pY = pExpr->x.pList->a[0].pExpr;
83968 Expr *pZ = pExpr->x.pList->a[1].pExpr;
83969 sqlite3TreeViewLine(pView, "BETWEEN");
83970 sqlite3TreeViewExpr(pView, pX, 1);
83971 sqlite3TreeViewExpr(pView, pY, 1);
83972 sqlite3TreeViewExpr(pView, pZ, 0);
 
 
 
83973 break;
83974 }
83975 case TK_TRIGGER: {
83976 /* If the opcode is TK_TRIGGER, then the expression is a reference
83977 ** to a column in the new.* or old.* pseudo-tables available to
@@ -83941,19 +83978,18 @@
83978 ** trigger programs. In this case Expr.iTable is set to 1 for the
83979 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
83980 ** is set to the column of the pseudo-table to read, or to -1 to
83981 ** read the rowid field.
83982 */
83983 sqlite3TreeViewLine(pView, "%s(%d)",
83984 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
83985 break;
83986 }
83987 case TK_CASE: {
83988 sqlite3TreeViewLine(pView, "CASE");
83989 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
83990 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
 
83991 break;
83992 }
83993 #ifndef SQLITE_OMIT_TRIGGER
83994 case TK_RAISE: {
83995 const char *zType = "unk";
@@ -83961,59 +83997,61 @@
83997 case OE_Rollback: zType = "rollback"; break;
83998 case OE_Abort: zType = "abort"; break;
83999 case OE_Fail: zType = "fail"; break;
84000 case OE_Ignore: zType = "ignore"; break;
84001 }
84002 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
84003 break;
84004 }
84005 #endif
84006 default: {
84007 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
84008 break;
84009 }
84010 }
84011 if( zBinOp ){
84012 sqlite3TreeViewLine(pView, "%s", zBinOp);
84013 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
84014 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
 
 
84015 }else if( zUniOp ){
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
84024 /*
84025 ** Generate a human-readable explanation of an expression list.
84026 */
84027 SQLITE_PRIVATE void sqlite3TreeViewExprList(
84028 TreeView *pView,
84029 const ExprList *pList,
84030 u8 moreToFollow,
84031 const char *zLabel
84032 ){
84033 int i;
84034 pView = sqlite3TreeViewPush(pView, moreToFollow);
84035 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
84036 if( pList==0 ){
84037 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 
84038 }else{
84039 sqlite3TreeViewLine(pView, "%s", zLabel);
84040 for(i=0; i<pList->nExpr; i++){
84041 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
84042 #if 0
84043 if( pList->a[i].zName ){
 
 
84044 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
84045 }
84046 if( pList->a[i].bSpanIsTab ){
84047 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
84048 }
84049 #endif
 
 
84050 }
 
84051 }
84052 sqlite3TreeViewPop(pView);
84053 }
84054 #endif /* SQLITE_DEBUG */
84055
84056 /*
84057 ** Generate code that pushes the value of every element of the given
@@ -89633,11 +89671,11 @@
89671 Column *pCol;
89672 sqlite3 *db = pParse->db;
89673 p = pParse->pNewTable;
89674 if( p!=0 ){
89675 pCol = &(p->aCol[p->nCol-1]);
89676 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
89677 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
89678 pCol->zName);
89679 }else{
89680 /* A copy of pExpr is used instead of the original, as pExpr contains
89681 ** tokens that point to volatile memory. The 'span' of the expression
@@ -94030,11 +94068,14 @@
94068
94069 /*
94070 ** Return the collating function associated with a function.
94071 */
94072 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
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;
94077 }
94078
94079 /*
94080 ** Indicate that the accumulator load should be skipped on this
94081 ** iteration of the aggregate loop.
@@ -94575,14 +94616,16 @@
94616 ** character is exactly one byte in size. Also, all characters are
94617 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
94618 ** whereas only characters less than 0x80 do in ASCII.
94619 */
94620 #if defined(SQLITE_EBCDIC)
94621 # define sqlite3Utf8Read(A) (*((*A)++))
94622 # define GlobUpperToLower(A) A = sqlite3UpperToLower[A]
94623 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
94624 #else
94625 # define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
94626 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
94627 #endif
94628
94629 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
94630 /* The correct SQL-92 behavior is for the LIKE operator to ignore
94631 ** case. Thus 'a' LIKE 'A' would be true. */
@@ -94591,11 +94634,11 @@
94634 ** is case sensitive causing 'a' LIKE 'A' to be false */
94635 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
94636
94637 /*
94638 ** Compare two UTF-8 strings for equality where the first string can
94639 ** potentially be a "glob" or "like" expression. Return true (1) if they
94640 ** are the same and false (0) if they are different.
94641 **
94642 ** Globbing rules:
94643 **
94644 ** '*' Matches any sequence of zero or more characters.
@@ -94611,120 +94654,147 @@
94654 ** in the list by making it the first character after '[' or '^'. A
94655 ** range of characters can be specified using '-'. Example:
94656 ** "[a-z]" matches any single lower-case letter. To match a '-', make
94657 ** it the last character in the list.
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 **
94670 ** This routine is usually quick, but can be N**2 in the worst case.
 
 
 
 
94671 */
94672 static int patternCompare(
94673 const u8 *zPattern, /* The glob pattern */
94674 const u8 *zString, /* The string to compare against the glob */
94675 const struct compareInfo *pInfo, /* Information about how to do the compare */
94676 u32 esc /* The escape character */
94677 ){
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;
94691
94692 while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
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 */
94697 while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
94698 || c == matchOne ){
94699 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
94700 return 0;
94701 }
94702 }
94703 if( c==0 ){
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;
94796 }
94797 return *zString==0;
94798 }
94799
94800 /*
@@ -103884,10 +103954,24 @@
103954 **
103955 *************************************************************************
103956 ** This file contains C code routines that are called by the parser
103957 ** to handle SELECT statements in SQLite.
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
103973
103974 /*
103975 ** An instance of the following object is used to record information about
103976 ** how to process the DISTINCT keyword, to simplify passing that information
103977 ** into the selectInnerLoop() routine.
@@ -103996,10 +104080,22 @@
104080 assert( pNew->pSrc!=0 || pParse->nErr>0 );
104081 }
104082 assert( pNew!=&standin );
104083 return pNew;
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
104097
104098 /*
104099 ** Delete the given Select structure and all of its substructures.
104100 */
104101 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
@@ -107226,10 +107322,12 @@
107322 }
107323 }
107324 }
107325
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));
107329
107330 /* Authorize the subquery */
107331 pParse->zAuthContext = pSubitem->zName;
107332 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
107333 testcase( i==SQLITE_DENY );
@@ -107278,10 +107376,11 @@
107376 p->pSrc = 0;
107377 p->pPrior = 0;
107378 p->pLimit = 0;
107379 p->pOffset = 0;
107380 pNew = sqlite3SelectDup(db, p, 0);
107381 sqlite3SelectSetName(pNew, pSub->zSelName);
107382 p->pOffset = pOffset;
107383 p->pLimit = pLimit;
107384 p->pOrderBy = pOrderBy;
107385 p->pSrc = pSrc;
107386 p->op = TK_ALL;
@@ -107290,10 +107389,13 @@
107389 }else{
107390 pNew->pPrior = pPrior;
107391 if( pPrior ) pPrior->pNext = pNew;
107392 pNew->pNext = p;
107393 p->pPrior = pNew;
107394 SELECTTRACE(2,pParse,p,
107395 ("compound-subquery flattener creates %s.%p as peer\n",
107396 pNew->zSelName, pNew));
107397 }
107398 if( db->mallocFailed ) return 1;
107399 }
107400
107401 /* Begin flattening the iFrom-th entry of the FROM clause
@@ -107419,12 +107521,27 @@
107521 if( isAgg ){
107522 substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
107523 pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
107524 }
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 }
107540 assert( pParent->pOrderBy==0 );
107541 assert( pSub->pPrior==0 );
107542 pParent->pOrderBy = pOrderBy;
107543 pSub->pOrderBy = 0;
107544 }else if( pParent->pOrderBy ){
107545 substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
107546 }
107547 if( pSub->pWhere ){
@@ -107465,10 +107582,17 @@
107582
107583 /* Finially, delete what is left of the subquery and return
107584 ** success.
107585 */
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
107594
107595 return 1;
107596 }
107597 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
107598
@@ -107936,10 +108060,11 @@
108060 if( pTab->pSelect || IsVirtual(pTab) ){
108061 /* We reach here if the named table is a really a view */
108062 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
108063 assert( pFrom->pSelect==0 );
108064 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
108065 sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
108066 sqlite3WalkSelect(pWalker, pFrom->pSelect);
108067 }
108068 #endif
108069 }
108070
@@ -108470,10 +108595,17 @@
108595 if( p==0 || db->mallocFailed || pParse->nErr ){
108596 return 1;
108597 }
108598 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
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
108607
108608 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
108609 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
108610 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
108611 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
@@ -108626,10 +108758,14 @@
108758 /* If there is are a sequence of queries, do the earlier ones first.
108759 */
108760 if( p->pPrior ){
108761 rc = multiSelect(pParse, p, pDest);
108762 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
108763 #if SELECTTRACE_ENABLED
108764 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
108765 pParse->nSelectIndent--;
108766 #endif
108767 return rc;
108768 }
108769 #endif
108770
108771 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -109225,107 +109361,110 @@
109361 generateColumnNames(pParse, pTabList, pEList);
109362 }
109363
109364 sqlite3DbFree(db, sAggInfo.aCol);
109365 sqlite3DbFree(db, sAggInfo.aFunc);
109366 #if SELECTTRACE_ENABLED
109367 SELECTTRACE(1,pParse,p,("end processing\n"));
109368 pParse->nSelectIndent--;
109369 #endif
109370 return rc;
109371 }
109372
109373 #ifdef SQLITE_DEBUG
109374 /*
109375 ** Generate a human-readable description of a the Select object.
109376 */
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");
109393 if( p->pSrc && p->pSrc->nSrc ){
109394 int i;
109395 pView = sqlite3TreeViewPush(pView, (n--)>0);
109396 sqlite3TreeViewLine(pView, "FROM");
109397 for(i=0; i<p->pSrc->nSrc; i++){
109398 struct SrcList_item *pItem = &p->pSrc->a[i];
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);
109405 }else if( pItem->zName ){
109406 sqlite3XPrintf(&x, 0, " %s", pItem->zName);
109407 }
109408 if( pItem->pTab ){
109409 sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
109410 }
109411 if( pItem->zAlias ){
109412 sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
109413 }
109414 if( pItem->jointype & JT_LEFT ){
109415 sqlite3XPrintf(&x, 0, " LEFT-JOIN");
109416 }
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);
109423 }
109424 sqlite3TreeViewPop(pView);
109425 }
109426 if( p->pWhere ){
109427 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
109428 sqlite3TreeViewExpr(pView, p->pWhere, 0);
109429 sqlite3TreeViewPop(pView);
109430 }
109431 if( p->pGroupBy ){
109432 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
 
 
109433 }
109434 if( p->pHaving ){
109435 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
109436 sqlite3TreeViewExpr(pView, p->pHaving, 0);
109437 sqlite3TreeViewPop(pView);
109438 }
109439 if( p->pOrderBy ){
109440 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
 
 
109441 }
109442 if( p->pLimit ){
109443 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
109444 sqlite3TreeViewExpr(pView, p->pLimit, 0);
109445 sqlite3TreeViewPop(pView);
109446 }
109447 if( p->pOffset ){
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 */
 
 
 
 
 
 
 
109466
109467 /************** End of select.c **********************************************/
109468 /************** Begin file table.c *******************************************/
109469 /*
109470 ** 2001 September 15
@@ -113719,15 +113858,10 @@
113858 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
113859 assert( TK_GE==TK_EQ+4 );
113860 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
113861 }
113862
 
 
 
 
 
113863 /*
113864 ** Commute a comparison operator. Expressions of the form "X op Y"
113865 ** are converted into "Y op X".
113866 **
113867 ** If left/right precedence rules come into play when determining the
@@ -116884,12 +117018,13 @@
117018
117019 /* Run a separate WHERE clause for each term of the OR clause. After
117020 ** eliminating duplicates from other WHERE clauses, the action for each
117021 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
117022 */
117023 wctrlFlags = WHERE_OMIT_OPEN_CLOSE
117024 | WHERE_FORCE_TABLE
117025 | WHERE_ONETABLE_ONLY;
117026 for(ii=0; ii<pOrWc->nTerm; ii++){
117027 WhereTerm *pOrTerm = &pOrWc->a[ii];
117028 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
117029 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
117030 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -116897,10 +117032,11 @@
117032 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
117033 pAndExpr->pLeft = pOrExpr;
117034 pOrExpr = pAndExpr;
117035 }
117036 /* Loop through table entries that match term pOrTerm. */
117037 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
117038 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
117039 wctrlFlags, iCovCur);
117040 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
117041 if( pSubWInfo ){
117042 WhereLoop *pSubLoop;
@@ -117116,25 +117252,30 @@
117252 }
117253
117254 return pLevel->notReady;
117255 }
117256
117257 #ifdef WHERETRACE_ENABLED
117258 /*
117259 ** Print the content of a WhereTerm object
117260 */
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
117277
117278 #ifdef WHERETRACE_ENABLED
117279 /*
117280 ** Print a WhereLoop object for debugging purposes
117281 */
@@ -117174,31 +117315,16 @@
117315 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
117316 }else{
117317 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
117318 }
117319 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
117320 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
 
 
 
 
117321 int i;
 
 
117322 for(i=0; i<p->nLTerm; i++){
117323 whereTermPrint(p->aLTerm[i], i);
117324 }
117325 }
 
 
 
 
 
 
 
 
 
117326 }
117327 #endif
117328
117329 /*
117330 ** Convert bulk memory into a valid WhereLoop that can be passed
@@ -117714,15 +117840,18 @@
117840 pNew->aLTerm[pNew->nLTerm++] = 0;
117841 pNew->wsFlags |= WHERE_SKIPSCAN;
117842 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
117843 if( pTerm ){
117844 /* TUNING: When estimating skip-scan for a term that is also indexable,
117845 ** multiply the cost of the skip-scan by 2.0, to make it a little less
117846 ** desirable than the regular index lookup. */
117847 nIter += 10; assert( 10==sqlite3LogEst(2) );
117848 }
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;
117853 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
117854 pNew->nOut = saved_nOut;
117855 pNew->u.btree.nEq = saved_nEq;
117856 pNew->u.btree.nSkip = saved_nSkip;
117857 }
@@ -118073,13 +118202,21 @@
118202 pNew->u.btree.nSkip = 0;
118203 pNew->u.btree.pIndex = 0;
118204 pNew->nLTerm = 1;
118205 pNew->aLTerm[0] = pTerm;
118206 /* TUNING: One-time cost for computing the automatic index is
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 }
118218 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
118219 /* TUNING: Each index lookup yields 20 rows in the table. This
118220 ** is more than the usual guess of 10 rows, since we have no way
118221 ** of knowing how selective the index will ultimately be. It would
118222 ** not be unreasonable to make this value much larger. */
@@ -118363,11 +118500,10 @@
118500 WhereLoopBuilder sSubBuild;
118501 WhereOrSet sSum, sCur;
118502 struct SrcList_item *pItem;
118503
118504 pWC = pBuilder->pWC;
 
118505 pWCEnd = pWC->a + pWC->nTerm;
118506 pNew = pBuilder->pNew;
118507 memset(&sSum, 0, sizeof(sSum));
118508 pItem = pWInfo->pTabList->a + pNew->iTab;
118509 iCur = pItem->iCursor;
@@ -118384,10 +118520,11 @@
118520
118521 sSubBuild = *pBuilder;
118522 sSubBuild.pOrderBy = 0;
118523 sSubBuild.pOrSet = &sCur;
118524
118525 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
118526 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
118527 if( (pOrTerm->eOperator & WO_AND)!=0 ){
118528 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
118529 }else if( pOrTerm->leftCursor==iCur ){
118530 tempWC.pWInfo = pWC->pWInfo;
@@ -118398,18 +118535,30 @@
118535 sSubBuild.pWC = &tempWC;
118536 }else{
118537 continue;
118538 }
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
118549 #ifndef SQLITE_OMIT_VIRTUALTABLE
118550 if( IsVirtual(pItem->pTab) ){
118551 rc = whereLoopAddVirtual(&sSubBuild, mExtra);
118552 }else
118553 #endif
118554 {
118555 rc = whereLoopAddBtree(&sSubBuild, mExtra);
118556 }
118557 if( rc==SQLITE_OK ){
118558 rc = whereLoopAddOr(&sSubBuild, mExtra);
118559 }
118560 assert( rc==SQLITE_OK || sCur.n==0 );
118561 if( sCur.n==0 ){
118562 sSum.n = 0;
118563 break;
118564 }else if( once ){
@@ -118450,10 +118599,11 @@
118599 pNew->rRun = sSum.a[i].rRun + 1;
118600 pNew->nOut = sSum.a[i].nOut;
118601 pNew->prereq = sSum.a[i].prereq;
118602 rc = whereLoopInsert(pBuilder, pNew);
118603 }
118604 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
118605 }
118606 }
118607 return rc;
118608 }
118609
@@ -119516,27 +119666,20 @@
119666 }
119667 }
119668
119669 /* Construct the WhereLoop objects */
119670 WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
119671 #if defined(WHERETRACE_ENABLED)
119672 /* Display all terms of the WHERE clause */
 
119673 if( sqlite3WhereTrace & 0x100 ){
119674 int i;
 
 
119675 for(i=0; i<sWLB.pWC->nTerm; i++){
119676 whereTermPrint(&sWLB.pWC->a[i], i);
119677 }
 
 
 
 
 
 
119678 }
119679 #endif
119680
119681 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
119682 rc = whereLoopAddAll(&sWLB);
119683 if( rc ) goto whereBeginError;
119684
119685 /* Display all of the WhereLoop objects if wheretrace is enabled */
@@ -120059,11 +120202,11 @@
120202
120203 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
120204 ** unary TK_ISNULL or TK_NOTNULL expression. */
120205 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
120206 sqlite3 *db = pParse->db;
120207 if( pY && pA && pY->op==TK_NULL ){
120208 pA->op = (u8)op;
120209 sqlite3ExprDelete(db, pA->pRight);
120210 pA->pRight = 0;
120211 }
120212 }
@@ -122318,13 +122461,10 @@
122461 break;
122462 case 111: /* cmd ::= select */
122463 {
122464 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
122465 sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
 
 
 
122466 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
122467 }
122468 break;
122469 case 112: /* select ::= with selectnowith */
122470 {
@@ -122377,10 +122517,34 @@
122517 {yygotominor.yy328 = TK_ALL;}
122518 break;
122519 case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
122520 {
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 */
122546 }
122547 break;
122548 case 120: /* values ::= VALUES LP nexprlist RP */
122549 {
122550 yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
@@ -123868,10 +124032,11 @@
124032 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
124033 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
124034 };
124035 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
124036 #endif
124037 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
124038
124039
124040 /*
124041 ** Return the length of the token that begins at z[0].
124042 ** Store the token type in *tokenType before returning.
@@ -125138,10 +125303,15 @@
125303 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
125304 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
125305 break;
125306 }
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 */
125313 case SQLITE_CONFIG_URI: {
125314 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
125315 break;
125316 }
125317
@@ -126875,11 +127045,11 @@
127045 int nUri = sqlite3Strlen30(zUri);
127046
127047 assert( *pzErrMsg==0 );
127048
127049 if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
127050 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
127051 ){
127052 char *zOpt;
127053 int eState; /* Parser state when parsing URI */
127054 int iIn; /* Input character index */
127055 int iOut = 0; /* Output character index */
@@ -127105,11 +127275,13 @@
127275 assert( SQLITE_OPEN_READWRITE == 0x02 );
127276 assert( SQLITE_OPEN_CREATE == 0x04 );
127277 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
127278 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
127279 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
127280 if( ((1<<(flags&7)) & 0x46)==0 ){
127281 return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
127282 }
127283
127284 if( sqlite3GlobalConfig.bCoreMutex==0 ){
127285 isThreadsafe = 0;
127286 }else if( flags & SQLITE_OPEN_NOMUTEX ){
127287 isThreadsafe = 0;
@@ -127989,26 +128161,10 @@
128161 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
128162 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
128163 break;
128164 }
128165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128166 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
128167 **
128168 ** Set or clear a flag that indicates that the database file is always well-
128169 ** formed and never corrupt. This flag is clear by default, indicating that
128170 ** database files might have arbitrary corruption. Setting the flag during
128171
+16 -18
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.7"
111111
#define SQLITE_VERSION_NUMBER 3008007
112
-#define SQLITE_SOURCE_ID "2014-09-20 00:35:05 59e2c9df02d7e988c5ad44c560ead1e5288b12e7"
112
+#define SQLITE_SOURCE_ID "2014-10-04 19:31:53 b8f7f19dc06c59de2e194d83e6c052fb7d28c71d"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -2667,13 +2667,13 @@
26672667
** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
26682668
** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
26692669
** an English language description of the error following a failure of any
26702670
** of the sqlite3_open() routines.
26712671
**
2672
-** ^The default encoding for the database will be UTF-8 if
2673
-** sqlite3_open() or sqlite3_open_v2() is called and
2674
-** UTF-16 in the native byte order if sqlite3_open16() is used.
2672
+** ^The default encoding will be UTF-8 for databases created using
2673
+** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
2674
+** created using sqlite3_open16() will be UTF-16 in the native byte order.
26752675
**
26762676
** Whether or not an error occurs when it is opened, resources
26772677
** associated with the [database connection] handle should be released by
26782678
** passing it to [sqlite3_close()] when it is no longer required.
26792679
**
@@ -2757,17 +2757,18 @@
27572757
** ^SQLite uses the path component of the URI as the name of the disk file
27582758
** which contains the database. ^If the path begins with a '/' character,
27592759
** then it is interpreted as an absolute path. ^If the path does not begin
27602760
** with a '/' (meaning that the authority section is omitted from the URI)
27612761
** then the path is interpreted as a relative path.
2762
-** ^On windows, the first component of an absolute path
2763
-** is a drive specification (e.g. "C:").
2762
+** ^(On windows, the first component of an absolute path
2763
+** is a drive specification (e.g. "C:").)^
27642764
**
27652765
** [[core URI query parameters]]
27662766
** The query component of a URI may contain parameters that are interpreted
27672767
** either by SQLite itself, or by a [VFS | custom VFS implementation].
2768
-** SQLite interprets the following three query parameters:
2768
+** SQLite and its built-in [VFSes] interpret the
2769
+** following query parameters:
27692770
**
27702771
** <ul>
27712772
** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
27722773
** a VFS object that provides the operating system interface that should
27732774
** be used to access the database file on disk. ^If this option is set to
@@ -2798,15 +2799,13 @@
27982799
** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
27992800
** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
28002801
** a URI filename, its value overrides any behavior requested by setting
28012802
** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
28022803
**
2803
-** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2804
-** "1") or "false" (or "off" or "no" or "0") to indicate that the
2804
+** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
28052805
** [powersafe overwrite] property does or does not apply to the
2806
-** storage media on which the database file resides. ^The psow query
2807
-** parameter only works for the built-in unix and Windows VFSes.
2806
+** storage media on which the database file resides.
28082807
**
28092808
** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
28102809
** which if set disables file locking in rollback journal modes. This
28112810
** is useful for accessing a database on a filesystem that does not
28122811
** support locking. Caution: Database corruption might result if two
@@ -3397,15 +3396,14 @@
33973396
** terminated. If any NUL characters occur at byte offsets less than
33983397
** the value of the fourth parameter then the resulting string value will
33993398
** contain embedded NULs. The result of expressions involving strings
34003399
** with embedded NULs is undefined.
34013400
**
3402
-** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3403
-** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3401
+** ^The fifth argument to the BLOB and string binding interfaces
3402
+** is a destructor used to dispose of the BLOB or
34043403
** string after SQLite has finished with it. ^The destructor is called
3405
-** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3406
-** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3404
+** to dispose of the BLOB or string even if the call to bind API fails.
34073405
** ^If the fifth argument is
34083406
** the special value [SQLITE_STATIC], then SQLite assumes that the
34093407
** information is in static, unmanaged space and does not need to be freed.
34103408
** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
34113409
** SQLite makes its own private copy of the data immediately, before
@@ -3412,11 +3410,11 @@
34123410
** the sqlite3_bind_*() routine returns.
34133411
**
34143412
** ^The sixth argument to sqlite3_bind_text64() must be one of
34153413
** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
34163414
** to specify the encoding of the text in the third parameter. If
3417
-** the sixth argument to sqlite3_bind_text64() is not how of the
3415
+** the sixth argument to sqlite3_bind_text64() is not one of the
34183416
** allowed values shown above, or if the text encoding is different
34193417
** from the encoding specified by the sixth parameter, then the behavior
34203418
** is undefined.
34213419
**
34223420
** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4448,11 +4446,11 @@
44484446
**
44494447
** ^The sqlite3_result_null() interface sets the return value
44504448
** of the application-defined function to be NULL.
44514449
**
44524450
** ^The sqlite3_result_text(), sqlite3_result_text16(),
4453
-** sqlite3_result_text16le(), and sqlite3_result_text16be()
4451
+** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
44544452
** set the return value of the application-defined function to be
44554453
** a text string which is represented as UTF-8, UTF-16 native byte order,
44564454
** UTF-16 little endian, or UTF-16 big endian, respectively.
44574455
** ^The sqlite3_result_text64() interface sets the return value of an
44584456
** application-defined function to be a text string in an encoding
@@ -6208,11 +6206,11 @@
62086206
#define SQLITE_TESTCTRL_RESERVE 14
62096207
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
62106208
#define SQLITE_TESTCTRL_ISKEYWORD 16
62116209
#define SQLITE_TESTCTRL_SCRATCHMALLOC 17
62126210
#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6213
-#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6211
+#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
62146212
#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
62156213
#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
62166214
#define SQLITE_TESTCTRL_BYTEORDER 22
62176215
#define SQLITE_TESTCTRL_ISINIT 23
62186216
#define SQLITE_TESTCTRL_SORTER_MMAP 24
62196217
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.7"
111 #define SQLITE_VERSION_NUMBER 3008007
112 #define SQLITE_SOURCE_ID "2014-09-20 00:35:05 59e2c9df02d7e988c5ad44c560ead1e5288b12e7"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -2667,13 +2667,13 @@
2667 ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
2668 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2669 ** an English language description of the error following a failure of any
2670 ** of the sqlite3_open() routines.
2671 **
2672 ** ^The default encoding for the database will be UTF-8 if
2673 ** sqlite3_open() or sqlite3_open_v2() is called and
2674 ** UTF-16 in the native byte order if sqlite3_open16() is used.
2675 **
2676 ** Whether or not an error occurs when it is opened, resources
2677 ** associated with the [database connection] handle should be released by
2678 ** passing it to [sqlite3_close()] when it is no longer required.
2679 **
@@ -2757,17 +2757,18 @@
2757 ** ^SQLite uses the path component of the URI as the name of the disk file
2758 ** which contains the database. ^If the path begins with a '/' character,
2759 ** then it is interpreted as an absolute path. ^If the path does not begin
2760 ** with a '/' (meaning that the authority section is omitted from the URI)
2761 ** then the path is interpreted as a relative path.
2762 ** ^On windows, the first component of an absolute path
2763 ** is a drive specification (e.g. "C:").
2764 **
2765 ** [[core URI query parameters]]
2766 ** The query component of a URI may contain parameters that are interpreted
2767 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2768 ** SQLite interprets the following three query parameters:
 
2769 **
2770 ** <ul>
2771 ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2772 ** a VFS object that provides the operating system interface that should
2773 ** be used to access the database file on disk. ^If this option is set to
@@ -2798,15 +2799,13 @@
2798 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2799 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2800 ** a URI filename, its value overrides any behavior requested by setting
2801 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2802 **
2803 ** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2804 ** "1") or "false" (or "off" or "no" or "0") to indicate that the
2805 ** [powersafe overwrite] property does or does not apply to the
2806 ** storage media on which the database file resides. ^The psow query
2807 ** parameter only works for the built-in unix and Windows VFSes.
2808 **
2809 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2810 ** which if set disables file locking in rollback journal modes. This
2811 ** is useful for accessing a database on a filesystem that does not
2812 ** support locking. Caution: Database corruption might result if two
@@ -3397,15 +3396,14 @@
3397 ** terminated. If any NUL characters occur at byte offsets less than
3398 ** the value of the fourth parameter then the resulting string value will
3399 ** contain embedded NULs. The result of expressions involving strings
3400 ** with embedded NULs is undefined.
3401 **
3402 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3403 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3404 ** string after SQLite has finished with it. ^The destructor is called
3405 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3406 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3407 ** ^If the fifth argument is
3408 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3409 ** information is in static, unmanaged space and does not need to be freed.
3410 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3411 ** SQLite makes its own private copy of the data immediately, before
@@ -3412,11 +3410,11 @@
3412 ** the sqlite3_bind_*() routine returns.
3413 **
3414 ** ^The sixth argument to sqlite3_bind_text64() must be one of
3415 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
3416 ** to specify the encoding of the text in the third parameter. If
3417 ** the sixth argument to sqlite3_bind_text64() is not how of the
3418 ** allowed values shown above, or if the text encoding is different
3419 ** from the encoding specified by the sixth parameter, then the behavior
3420 ** is undefined.
3421 **
3422 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4448,11 +4446,11 @@
4448 **
4449 ** ^The sqlite3_result_null() interface sets the return value
4450 ** of the application-defined function to be NULL.
4451 **
4452 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4453 ** sqlite3_result_text16le(), and sqlite3_result_text16be()
4454 ** set the return value of the application-defined function to be
4455 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4456 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4457 ** ^The sqlite3_result_text64() interface sets the return value of an
4458 ** application-defined function to be a text string in an encoding
@@ -6208,11 +6206,11 @@
6208 #define SQLITE_TESTCTRL_RESERVE 14
6209 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6210 #define SQLITE_TESTCTRL_ISKEYWORD 16
6211 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6212 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6213 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6214 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6215 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6216 #define SQLITE_TESTCTRL_BYTEORDER 22
6217 #define SQLITE_TESTCTRL_ISINIT 23
6218 #define SQLITE_TESTCTRL_SORTER_MMAP 24
6219
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.7"
111 #define SQLITE_VERSION_NUMBER 3008007
112 #define SQLITE_SOURCE_ID "2014-10-04 19:31:53 b8f7f19dc06c59de2e194d83e6c052fb7d28c71d"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -2667,13 +2667,13 @@
2667 ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
2668 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2669 ** an English language description of the error following a failure of any
2670 ** of the sqlite3_open() routines.
2671 **
2672 ** ^The default encoding will be UTF-8 for databases created using
2673 ** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
2674 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
2675 **
2676 ** Whether or not an error occurs when it is opened, resources
2677 ** associated with the [database connection] handle should be released by
2678 ** passing it to [sqlite3_close()] when it is no longer required.
2679 **
@@ -2757,17 +2757,18 @@
2757 ** ^SQLite uses the path component of the URI as the name of the disk file
2758 ** which contains the database. ^If the path begins with a '/' character,
2759 ** then it is interpreted as an absolute path. ^If the path does not begin
2760 ** with a '/' (meaning that the authority section is omitted from the URI)
2761 ** then the path is interpreted as a relative path.
2762 ** ^(On windows, the first component of an absolute path
2763 ** is a drive specification (e.g. "C:").)^
2764 **
2765 ** [[core URI query parameters]]
2766 ** The query component of a URI may contain parameters that are interpreted
2767 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2768 ** SQLite and its built-in [VFSes] interpret the
2769 ** following query parameters:
2770 **
2771 ** <ul>
2772 ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2773 ** a VFS object that provides the operating system interface that should
2774 ** be used to access the database file on disk. ^If this option is set to
@@ -2798,15 +2799,13 @@
2799 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2800 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2801 ** a URI filename, its value overrides any behavior requested by setting
2802 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2803 **
2804 ** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
 
2805 ** [powersafe overwrite] property does or does not apply to the
2806 ** storage media on which the database file resides.
 
2807 **
2808 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2809 ** which if set disables file locking in rollback journal modes. This
2810 ** is useful for accessing a database on a filesystem that does not
2811 ** support locking. Caution: Database corruption might result if two
@@ -3397,15 +3396,14 @@
3396 ** terminated. If any NUL characters occur at byte offsets less than
3397 ** the value of the fourth parameter then the resulting string value will
3398 ** contain embedded NULs. The result of expressions involving strings
3399 ** with embedded NULs is undefined.
3400 **
3401 ** ^The fifth argument to the BLOB and string binding interfaces
3402 ** is a destructor used to dispose of the BLOB or
3403 ** string after SQLite has finished with it. ^The destructor is called
3404 ** to dispose of the BLOB or string even if the call to bind API fails.
 
3405 ** ^If the fifth argument is
3406 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3407 ** information is in static, unmanaged space and does not need to be freed.
3408 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3409 ** SQLite makes its own private copy of the data immediately, before
@@ -3412,11 +3410,11 @@
3410 ** the sqlite3_bind_*() routine returns.
3411 **
3412 ** ^The sixth argument to sqlite3_bind_text64() must be one of
3413 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
3414 ** to specify the encoding of the text in the third parameter. If
3415 ** the sixth argument to sqlite3_bind_text64() is not one of the
3416 ** allowed values shown above, or if the text encoding is different
3417 ** from the encoding specified by the sixth parameter, then the behavior
3418 ** is undefined.
3419 **
3420 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -4448,11 +4446,11 @@
4446 **
4447 ** ^The sqlite3_result_null() interface sets the return value
4448 ** of the application-defined function to be NULL.
4449 **
4450 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4451 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4452 ** set the return value of the application-defined function to be
4453 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4454 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4455 ** ^The sqlite3_result_text64() interface sets the return value of an
4456 ** application-defined function to be a text string in an encoding
@@ -6208,11 +6206,11 @@
6206 #define SQLITE_TESTCTRL_RESERVE 14
6207 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6208 #define SQLITE_TESTCTRL_ISKEYWORD 16
6209 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6210 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6211 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
6212 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6213 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6214 #define SQLITE_TESTCTRL_BYTEORDER 22
6215 #define SQLITE_TESTCTRL_ISINIT 23
6216 #define SQLITE_TESTCTRL_SORTER_MMAP 24
6217

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button