Fossil SCM

Update the built-in SQLite to version 3.8.6

jan.nijtmans 2014-08-15 13:45 UTC branch-1.29 merge
Commit 1c9faaecbe80d110830367f81f46fb58660321b7
+1 -1
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549549
int rc;
550550
if( sqlite3_libversion_number()<3008003 ){
551551
fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552552
sqlite3_libversion());
553553
}
554
- sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
554
+ sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
555555
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556556
memset(&g, 0, sizeof(g));
557557
g.now = time(0);
558558
g.httpHeader = empty_blob;
559559
#ifdef FOSSIL_ENABLE_JSON
560560
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549 int rc;
550 if( sqlite3_libversion_number()<3008003 ){
551 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552 sqlite3_libversion());
553 }
554 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
555 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556 memset(&g, 0, sizeof(g));
557 g.now = time(0);
558 g.httpHeader = empty_blob;
559 #ifdef FOSSIL_ENABLE_JSON
560
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549 int rc;
550 if( sqlite3_libversion_number()<3008003 ){
551 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552 sqlite3_libversion());
553 }
554 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
555 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556 memset(&g, 0, sizeof(g));
557 g.now = time(0);
558 g.httpHeader = empty_blob;
559 #ifdef FOSSIL_ENABLE_JSON
560
+1 -1
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549549
int rc;
550550
if( sqlite3_libversion_number()<3008003 ){
551551
fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552552
sqlite3_libversion());
553553
}
554
- sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
554
+ sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
555555
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556556
memset(&g, 0, sizeof(g));
557557
g.now = time(0);
558558
g.httpHeader = empty_blob;
559559
#ifdef FOSSIL_ENABLE_JSON
560560
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549 int rc;
550 if( sqlite3_libversion_number()<3008003 ){
551 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552 sqlite3_libversion());
553 }
554 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
555 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556 memset(&g, 0, sizeof(g));
557 g.now = time(0);
558 g.httpHeader = empty_blob;
559 #ifdef FOSSIL_ENABLE_JSON
560
--- src/main.c
+++ src/main.c
@@ -549,11 +549,11 @@
549 int rc;
550 if( sqlite3_libversion_number()<3008003 ){
551 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.3",
552 sqlite3_libversion());
553 }
554 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
555 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
556 memset(&g, 0, sizeof(g));
557 g.now = time(0);
558 g.httpHeader = empty_blob;
559 #ifdef FOSSIL_ENABLE_JSON
560
+137 -87
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430430
#endif
431431
}
432432
return zResult;
433433
}
434434
435
-struct previous_mode_data {
436
- int valid; /* Is there legit data in here? */
437
- int mode;
438
- int showHeader;
439
- int colWidth[100];
435
+/*
436
+** Shell output mode information from before ".explain on",
437
+** saved so that it can be restored by ".explain off"
438
+*/
439
+typedef struct SavedModeInfo SavedModeInfo;
440
+struct SavedModeInfo {
441
+ int valid; /* Is there legit data in here? */
442
+ int mode; /* Mode prior to ".explain on" */
443
+ int showHeader; /* The ".header" setting prior to ".explain on" */
444
+ int colWidth[100]; /* Column widths prior to ".explain on" */
440445
};
441446
442447
/*
443
-** An pointer to an instance of this structure is passed from
444
-** the main program to the callback. This is used to communicate
445
-** state and mode information.
448
+** State information about the database connection is contained in an
449
+** instance of the following structure.
446450
*/
447
-struct callback_data {
451
+typedef struct ShellState ShellState;
452
+struct ShellState {
448453
sqlite3 *db; /* The database */
449454
int echoOn; /* True to echo input commands */
450455
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
451456
int statsOn; /* True to display memory stats before each finalize */
452457
int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
455460
FILE *traceOut; /* Output for sqlite3_trace() */
456461
int nErr; /* Number of errors seen */
457462
int mode; /* An output mode setting */
458463
int writableSchema; /* True if PRAGMA writable_schema=ON */
459464
int showHeader; /* True to show column names in List or Column mode */
465
+ unsigned shellFlgs; /* Various flags */
460466
char *zDestTable; /* Name of destination table when MODE_Insert */
461467
char separator[20]; /* Separator character for MODE_List */
462468
char newline[20]; /* Record separator in MODE_Csv */
463469
int colWidth[100]; /* Requested width of each column when in column mode*/
464470
int actualWidth[100]; /* Actual width of each column */
465471
char nullvalue[20]; /* The text to print when a NULL comes back from
466472
** the database */
467
- struct previous_mode_data explainPrev;
468
- /* Holds the mode information just before
469
- ** .explain ON */
473
+ SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
470474
char outfile[FILENAME_MAX]; /* Filename for *out */
471475
const char *zDbFilename; /* name of the database file */
472476
char *zFreeOnClose; /* Filename to free when closing */
473477
const char *zVfs; /* Name of VFS to use */
474478
sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
476480
int *aiIndent; /* Array of indents used in MODE_Explain */
477481
int nIndent; /* Size of array aiIndent[] */
478482
int iIndent; /* Index of current op in aiIndent[] */
479483
};
480484
485
+/*
486
+** These are the allowed shellFlgs values
487
+*/
488
+#define SHFLG_Scratch 0x00001 /* The --scratch option is used */
489
+#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
490
+#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
491
+
481492
/*
482493
** These are the allowed modes.
483494
*/
484495
#define MODE_Line 0 /* One column per line. Blank line between records */
485496
#define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
520531
521532
/*
522533
** A callback for the sqlite3_log() interface.
523534
*/
524535
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
525
- struct callback_data *p = (struct callback_data*)pArg;
536
+ ShellState *p = (ShellState*)pArg;
526537
if( p->pLog==0 ) return;
527538
fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
528539
fflush(p->pLog);
529540
}
530541
@@ -662,11 +673,11 @@
662673
** Output a single term of CSV. Actually, p->separator is used for
663674
** the separator, which may or may not be a comma. p->nullvalue is
664675
** the null value. Strings are quoted if necessary. The separator
665676
** is only issued if bSep is true.
666677
*/
667
-static void output_csv(struct callback_data *p, const char *z, int bSep){
678
+static void output_csv(ShellState *p, const char *z, int bSep){
668679
FILE *out = p->out;
669680
if( z==0 ){
670681
fprintf(out,"%s",p->nullvalue);
671682
}else{
672683
int i;
@@ -711,11 +722,11 @@
711722
** This is the callback routine that the shell
712723
** invokes for each row of a query result.
713724
*/
714725
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
715726
int i;
716
- struct callback_data *p = (struct callback_data*)pArg;
727
+ ShellState *p = (ShellState*)pArg;
717728
718729
switch( p->mode ){
719730
case MODE_Line: {
720731
int w = 5;
721732
if( azArg==0 ) break;
@@ -921,15 +932,15 @@
921932
/* since we don't have type info, call the shell_callback with a NULL value */
922933
return shell_callback(pArg, nArg, azArg, azCol, NULL);
923934
}
924935
925936
/*
926
-** Set the destination table field of the callback_data structure to
937
+** Set the destination table field of the ShellState structure to
927938
** the name of the table given. Escape any quote characters in the
928939
** table name.
929940
*/
930
-static void set_table_name(struct callback_data *p, const char *zName){
941
+static void set_table_name(ShellState *p, const char *zName){
931942
int i, n;
932943
int needQuote;
933944
char *z;
934945
935946
if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
10151026
** then write the semicolon on a separate line. That way, if a
10161027
** "--" comment occurs at the end of the statement, the comment
10171028
** won't consume the semicolon terminator.
10181029
*/
10191030
static int run_table_dump_query(
1020
- struct callback_data *p, /* Query context */
1031
+ ShellState *p, /* Query context */
10211032
const char *zSelect, /* SELECT statement to extract content */
10221033
const char *zFirstRow /* Print before first row, if not NULL */
10231034
){
10241035
sqlite3_stmt *pSelect;
10251036
int rc;
@@ -1078,11 +1089,11 @@
10781089
/*
10791090
** Display memory stats.
10801091
*/
10811092
static int display_stats(
10821093
sqlite3 *db, /* Database to query */
1083
- struct callback_data *pArg, /* Pointer to struct callback_data */
1094
+ ShellState *pArg, /* Pointer to ShellState */
10841095
int bReset /* True to reset the stats */
10851096
){
10861097
int iCur;
10871098
int iHiwtr;
10881099
@@ -1092,25 +1103,23 @@
10921103
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
10931104
fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
10941105
iHiwtr = iCur = -1;
10951106
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
10961107
fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1097
-/*
1098
-** Not currently used by the CLI.
1099
-** iHiwtr = iCur = -1;
1100
-** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1101
-** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1102
-*/
1108
+ if( pArg->shellFlgs & SHFLG_Pagecache ){
1109
+ iHiwtr = iCur = -1;
1110
+ sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1111
+ fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1112
+ }
11031113
iHiwtr = iCur = -1;
11041114
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
11051115
fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1106
-/*
1107
-** Not currently used by the CLI.
1108
-** iHiwtr = iCur = -1;
1109
-** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1110
-** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1111
-*/
1116
+ if( pArg->shellFlgs & SHFLG_Scratch ){
1117
+ iHiwtr = iCur = -1;
1118
+ sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1119
+ fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1120
+ }
11121121
iHiwtr = iCur = -1;
11131122
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
11141123
fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
11151124
iHiwtr = iCur = -1;
11161125
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
11271136
fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
11281137
#endif
11291138
}
11301139
11311140
if( pArg && pArg->out && db ){
1132
- iHiwtr = iCur = -1;
1133
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1134
- fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1135
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1136
- fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1137
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1138
- fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1139
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1140
- fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1141
+ if( pArg->shellFlgs & SHFLG_Lookaside ){
1142
+ iHiwtr = iCur = -1;
1143
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1144
+ fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1145
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1146
+ fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1147
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1148
+ fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1149
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1150
+ fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1151
+ }
11411152
iHiwtr = iCur = -1;
11421153
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11431154
fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
11441155
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11451156
fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
11871198
return 0;
11881199
}
11891200
11901201
/*
11911202
** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1192
-** and populate the callback_data.aiIndent[] array with the number of
1203
+** and populate the ShellState.aiIndent[] array with the number of
11931204
** spaces each opcode should be indented before it is output.
11941205
**
11951206
** The indenting rules are:
11961207
**
11971208
** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
12031214
** Yield SeekGt SeekLt RowSetRead Rewind
12041215
** or if the P1 parameter is one instead of zero,
12051216
** then indent all opcodes between the earlier instruction
12061217
** and "Goto" by 2 spaces.
12071218
*/
1208
-static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1219
+static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12091220
const char *zSql; /* The text of the SQL statement */
12101221
const char *z; /* Used to check if this is an EXPLAIN */
12111222
int *abYield = 0; /* True if op is an OP_Yield */
12121223
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12131224
int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
12631274
}
12641275
12651276
/*
12661277
** Free the array allocated by explain_data_prepare().
12671278
*/
1268
-static void explain_data_delete(struct callback_data *p){
1279
+static void explain_data_delete(ShellState *p){
12691280
sqlite3_free(p->aiIndent);
12701281
p->aiIndent = 0;
12711282
p->nIndent = 0;
12721283
p->iIndent = 0;
12731284
}
@@ -1280,16 +1291,16 @@
12801291
** This is very similar to SQLite's built-in sqlite3_exec()
12811292
** function except it takes a slightly different callback
12821293
** and callback data argument.
12831294
*/
12841295
static int shell_exec(
1285
- sqlite3 *db, /* An open database */
1286
- const char *zSql, /* SQL to be evaluated */
1296
+ sqlite3 *db, /* An open database */
1297
+ const char *zSql, /* SQL to be evaluated */
12871298
int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1288
- /* (not the same as sqlite3_exec) */
1289
- struct callback_data *pArg, /* Pointer to struct callback_data */
1290
- char **pzErrMsg /* Error msg written here */
1299
+ /* (not the same as sqlite3_exec) */
1300
+ ShellState *pArg, /* Pointer to ShellState */
1301
+ char **pzErrMsg /* Error msg written here */
12911302
){
12921303
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
12931304
int rc = SQLITE_OK; /* Return Code */
12941305
int rc2;
12951306
const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
14531464
int rc;
14541465
const char *zTable;
14551466
const char *zType;
14561467
const char *zSql;
14571468
const char *zPrepStmt = 0;
1458
- struct callback_data *p = (struct callback_data *)pArg;
1469
+ ShellState *p = (ShellState *)pArg;
14591470
14601471
UNUSED_PARAMETER(azCol);
14611472
if( nArg!=3 ) return 1;
14621473
zTable = azArg[0];
14631474
zType = azArg[1];
@@ -1549,11 +1560,11 @@
15491560
**
15501561
** If we get a SQLITE_CORRUPT error, rerun the query after appending
15511562
** "ORDER BY rowid DESC" to the end.
15521563
*/
15531564
static int run_schema_dump_query(
1554
- struct callback_data *p,
1565
+ ShellState *p,
15551566
const char *zQuery
15561567
){
15571568
int rc;
15581569
char *zErr = 0;
15591570
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
16491660
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
16501661
" Negative values right-justify\n"
16511662
;
16521663
16531664
/* Forward reference */
1654
-static int process_input(struct callback_data *p, FILE *in);
1665
+static int process_input(ShellState *p, FILE *in);
16551666
/*
16561667
** Implementation of the "readfile(X)" SQL function. The entire content
16571668
** of the file named X is read and returned as a BLOB. NULL is returned
16581669
** if the file does not exist or is unreadable.
16591670
*/
@@ -1694,35 +1705,32 @@
16941705
int argc,
16951706
sqlite3_value **argv
16961707
){
16971708
FILE *out;
16981709
const char *z;
1699
- int n;
17001710
sqlite3_int64 rc;
17011711
const char *zFile;
17021712
17031713
zFile = (const char*)sqlite3_value_text(argv[0]);
17041714
if( zFile==0 ) return;
17051715
out = fopen(zFile, "wb");
17061716
if( out==0 ) return;
17071717
z = (const char*)sqlite3_value_blob(argv[1]);
17081718
if( z==0 ){
1709
- n = 0;
17101719
rc = 0;
17111720
}else{
1712
- n = sqlite3_value_bytes(argv[1]);
1713
- rc = fwrite(z, 1, n, out);
1721
+ rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
17141722
}
17151723
fclose(out);
17161724
sqlite3_result_int64(context, rc);
17171725
}
17181726
17191727
/*
17201728
** Make sure the database is open. If it is not, then open it. If
17211729
** the database fails to open, print an error message and exit.
17221730
*/
1723
-static void open_db(struct callback_data *p, int keepAlive){
1731
+static void open_db(ShellState *p, int keepAlive){
17241732
if( p->db==0 ){
17251733
sqlite3_initialize();
17261734
sqlite3_open(p->zDbFilename, &p->db);
17271735
db = p->db;
17281736
if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
18991907
/*
19001908
** A routine for handling output from sqlite3_trace().
19011909
*/
19021910
static void sql_trace_callback(void *pArg, const char *z){
19031911
FILE *f = (FILE*)pArg;
1904
- if( f ) fprintf(f, "%s\n", z);
1912
+ if( f ){
1913
+ int i = (int)strlen(z);
1914
+ while( i>0 && z[i-1]==';' ){ i--; }
1915
+ fprintf(f, "%.*s;\n", i, z);
1916
+ }
19051917
}
19061918
19071919
/*
19081920
** A no-op routine that runs with the ".breakpoint" doc-command. This is
19091921
** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
20172029
** Try to transfer data for table zTable. If an error is seen while
20182030
** moving forward, try to go backwards. The backwards movement won't
20192031
** work for WITHOUT ROWID tables.
20202032
*/
20212033
static void tryToCloneData(
2022
- struct callback_data *p,
2034
+ ShellState *p,
20232035
sqlite3 *newDb,
20242036
const char *zTable
20252037
){
20262038
sqlite3_stmt *pQuery = 0;
20272039
sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
21302142
** each row, invoke xForEach() on the object defined by that row.
21312143
** If an error is encountered while moving forward through the
21322144
** sqlite_master table, try again moving backwards.
21332145
*/
21342146
static void tryToCloneSchema(
2135
- struct callback_data *p,
2147
+ ShellState *p,
21362148
sqlite3 *newDb,
21372149
const char *zWhere,
2138
- void (*xForEach)(struct callback_data*,sqlite3*,const char*)
2150
+ void (*xForEach)(ShellState*,sqlite3*,const char*)
21392151
){
21402152
sqlite3_stmt *pQuery = 0;
21412153
char *zQuery = 0;
21422154
int rc;
21432155
const unsigned char *zName;
@@ -2204,11 +2216,11 @@
22042216
/*
22052217
** Open a new database file named "zNewDb". Try to recover as much information
22062218
** as possible out of the main database (which might be corrupt) and write it
22072219
** into zNewDb.
22082220
*/
2209
-static void tryToClone(struct callback_data *p, const char *zNewDb){
2221
+static void tryToClone(ShellState *p, const char *zNewDb){
22102222
int rc;
22112223
sqlite3 *newDb = 0;
22122224
if( access(zNewDb,0)==0 ){
22132225
fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
22142226
return;
@@ -2229,11 +2241,11 @@
22292241
}
22302242
22312243
/*
22322244
** Change the output file back to stdout
22332245
*/
2234
-static void output_reset(struct callback_data *p){
2246
+static void output_reset(ShellState *p){
22352247
if( p->outfile[0]=='|' ){
22362248
pclose(p->out);
22372249
}else{
22382250
output_file_close(p->out);
22392251
}
@@ -2245,11 +2257,11 @@
22452257
** If an input line begins with "." then invoke this routine to
22462258
** process that line.
22472259
**
22482260
** Return 1 on error, 2 to exit, and 0 otherwise.
22492261
*/
2250
-static int do_meta_command(char *zLine, struct callback_data *p){
2262
+static int do_meta_command(char *zLine, ShellState *p){
22512263
int i = 1;
22522264
int nArg = 0;
22532265
int n, c;
22542266
int rc = 0;
22552267
char *azArg[50];
@@ -2363,11 +2375,11 @@
23632375
rc = 1;
23642376
}
23652377
}else
23662378
23672379
if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2368
- struct callback_data data;
2380
+ ShellState data;
23692381
char *zErrMsg = 0;
23702382
open_db(p, 0);
23712383
memcpy(&data, p, sizeof(data));
23722384
data.showHeader = 1;
23732385
data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
24612473
}else
24622474
24632475
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
24642476
int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
24652477
if(val == 1) {
2466
- if(!p->explainPrev.valid) {
2467
- p->explainPrev.valid = 1;
2468
- p->explainPrev.mode = p->mode;
2469
- p->explainPrev.showHeader = p->showHeader;
2470
- memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2478
+ if(!p->normalMode.valid) {
2479
+ p->normalMode.valid = 1;
2480
+ p->normalMode.mode = p->mode;
2481
+ p->normalMode.showHeader = p->showHeader;
2482
+ memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
24712483
}
24722484
/* We could put this code under the !p->explainValid
24732485
** condition so that it does not execute if we are already in
24742486
** explain mode. However, always executing it allows us an easy
24752487
** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
24852497
p->colWidth[3] = 4; /* P2 */
24862498
p->colWidth[4] = 4; /* P3 */
24872499
p->colWidth[5] = 13; /* P4 */
24882500
p->colWidth[6] = 2; /* P5 */
24892501
p->colWidth[7] = 13; /* Comment */
2490
- }else if (p->explainPrev.valid) {
2491
- p->explainPrev.valid = 0;
2492
- p->mode = p->explainPrev.mode;
2493
- p->showHeader = p->explainPrev.showHeader;
2494
- memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2502
+ }else if (p->normalMode.valid) {
2503
+ p->normalMode.valid = 0;
2504
+ p->mode = p->normalMode.mode;
2505
+ p->showHeader = p->normalMode.showHeader;
2506
+ memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
24952507
}
24962508
}else
24972509
24982510
if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2499
- struct callback_data data;
2511
+ ShellState data;
25002512
char *zErrMsg = 0;
25012513
int doStats = 0;
25022514
if( nArg!=1 ){
25032515
fprintf(stderr, "Usage: .fullschema\n");
25042516
rc = 1;
@@ -2511,11 +2523,11 @@
25112523
rc = sqlite3_exec(p->db,
25122524
"SELECT sql FROM"
25132525
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
25142526
" FROM sqlite_master UNION ALL"
25152527
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2516
- "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2528
+ "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
25172529
"ORDER BY rowid",
25182530
callback, &data, &zErrMsg
25192531
);
25202532
if( rc==SQLITE_OK ){
25212533
sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
27152727
sqlite3_finalize(pStmt);
27162728
if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
27172729
}else
27182730
27192731
if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2720
- struct callback_data data;
2732
+ ShellState data;
27212733
char *zErrMsg = 0;
27222734
open_db(p, 0);
27232735
memcpy(&data, p, sizeof(data));
27242736
data.showHeader = 0;
27252737
data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
30093021
}
30103022
sqlite3_close(pSrc);
30113023
}else
30123024
30133025
if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3014
- struct callback_data data;
3026
+ ShellState data;
30153027
char *zErrMsg = 0;
30163028
open_db(p, 0);
30173029
memcpy(&data, p, sizeof(data));
30183030
data.showHeader = 0;
30193031
data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
30653077
rc = sqlite3_exec(p->db,
30663078
"SELECT sql FROM "
30673079
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
30683080
" FROM sqlite_master UNION ALL"
30693081
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3070
- "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
3082
+ "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
30713083
"ORDER BY rowid",
30723084
callback, &data, &zErrMsg
30733085
);
30743086
}else{
30753087
fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
31263138
31273139
if( c=='s'
31283140
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
31293141
){
31303142
char *zCmd;
3131
- int i;
3143
+ int i, x;
31323144
if( nArg<2 ){
31333145
fprintf(stderr, "Usage: .system COMMAND\n");
31343146
rc = 1;
31353147
goto meta_command_exit;
31363148
}
@@ -3137,12 +3149,13 @@
31373149
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
31383150
for(i=2; i<nArg; i++){
31393151
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
31403152
zCmd, azArg[i]);
31413153
}
3142
- (void)system(zCmd);
3154
+ x = system(zCmd);
31433155
sqlite3_free(zCmd);
3156
+ if( x ) fprintf(stderr, "System command returns %d\n", x);
31443157
}else
31453158
31463159
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
31473160
int i;
31483161
if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
31503163
rc = 1;
31513164
goto meta_command_exit;
31523165
}
31533166
fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
31543167
fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3155
- fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
3168
+ fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
31563169
fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
31573170
fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
31583171
fprintf(p->out,"%9.9s: ", "nullvalue");
31593172
output_c_string(p->out, p->nullvalue);
31603173
fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
35423555
** is saved only if input is interactive. An interrupt signal will
35433556
** cause this routine to exit immediately, unless input is interactive.
35443557
**
35453558
** Return the number of errors.
35463559
*/
3547
-static int process_input(struct callback_data *p, FILE *in){
3560
+static int process_input(ShellState *p, FILE *in){
35483561
char *zLine = 0; /* A single input line */
35493562
char *zSql = 0; /* Accumulated SQL text */
35503563
int nLine; /* Length of current line */
35513564
int nSql = 0; /* Bytes of zSql[] used */
35523565
int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
37213734
** parameter is NULL, take input from ~/.sqliterc
37223735
**
37233736
** Returns the number of errors.
37243737
*/
37253738
static int process_sqliterc(
3726
- struct callback_data *p, /* Configuration data */
3739
+ ShellState *p, /* Configuration data */
37273740
const char *sqliterc_override /* Name of config file. NULL to use default */
37283741
){
37293742
char *home_dir = NULL;
37303743
const char *sqliterc = sqliterc_override;
37313744
char *zBuf = 0;
@@ -3774,16 +3787,19 @@
37743787
" -help show this message\n"
37753788
" -html set output mode to HTML\n"
37763789
" -interactive force interactive I/O\n"
37773790
" -line set output mode to 'line'\n"
37783791
" -list set output mode to 'list'\n"
3792
+ " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
37793793
" -mmap N default mmap size set to N\n"
37803794
#ifdef SQLITE_ENABLE_MULTIPLEX
37813795
" -multiplex enable the multiplexor VFS\n"
37823796
#endif
37833797
" -newline SEP set newline character(s) for CSV\n"
37843798
" -nullvalue TEXT set text string for NULL values. Default ''\n"
3799
+ " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
3800
+ " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
37853801
" -separator SEP set output field separator. Default: '|'\n"
37863802
" -stats print memory stats before each finalize\n"
37873803
" -version show SQLite version\n"
37883804
" -vfs NAME use NAME as the default VFS\n"
37893805
#ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
38043820
}
38053821
38063822
/*
38073823
** Initialize the state information in data
38083824
*/
3809
-static void main_init(struct callback_data *data) {
3825
+static void main_init(ShellState *data) {
38103826
memset(data, 0, sizeof(*data));
38113827
data->mode = MODE_List;
38123828
memcpy(data->separator,"|", 2);
38133829
memcpy(data->newline,"\r\n", 3);
38143830
data->showHeader = 0;
3831
+ data->shellFlgs = SHFLG_Lookaside;
38153832
sqlite3_config(SQLITE_CONFIG_URI, 1);
38163833
sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
3834
+ sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
38173835
sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
38183836
sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
3819
- sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
38203837
}
38213838
38223839
/*
38233840
** Output text to the console in a font that attracts extra attention.
38243841
*/
@@ -3852,11 +3869,11 @@
38523869
return argv[i];
38533870
}
38543871
38553872
int main(int argc, char **argv){
38563873
char *zErrMsg = 0;
3857
- struct callback_data data;
3874
+ ShellState data;
38583875
const char *zInitFile = 0;
38593876
char *zFirstCmd = 0;
38603877
int i;
38613878
int rc = 0;
38623879
int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
39233940
zSize = cmdline_option_value(argc, argv, ++i);
39243941
szHeap = integerValue(zSize);
39253942
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
39263943
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
39273944
#endif
3945
+ }else if( strcmp(z,"-scratch")==0 ){
3946
+ int n, sz;
3947
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3948
+ if( sz>400000 ) sz = 400000;
3949
+ if( sz<2500 ) sz = 2500;
3950
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3951
+ if( n>10 ) n = 10;
3952
+ if( n<1 ) n = 1;
3953
+ sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
3954
+ data.shellFlgs |= SHFLG_Scratch;
3955
+ }else if( strcmp(z,"-pagecache")==0 ){
3956
+ int n, sz;
3957
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3958
+ if( sz>70000 ) sz = 70000;
3959
+ if( sz<800 ) sz = 800;
3960
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3961
+ if( n<10 ) n = 10;
3962
+ sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
3963
+ data.shellFlgs |= SHFLG_Pagecache;
3964
+ }else if( strcmp(z,"-lookaside")==0 ){
3965
+ int n, sz;
3966
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3967
+ if( sz<0 ) sz = 0;
3968
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3969
+ if( n<0 ) n = 0;
3970
+ sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
3971
+ if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
39283972
#ifdef SQLITE_ENABLE_VFSTRACE
39293973
}else if( strcmp(z,"-vfstrace")==0 ){
39303974
extern int vfstrace_register(
39313975
const char *zTraceName,
39323976
const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
40384082
stdin_is_interactive = 1;
40394083
}else if( strcmp(z,"-batch")==0 ){
40404084
stdin_is_interactive = 0;
40414085
}else if( strcmp(z,"-heap")==0 ){
40424086
i++;
4087
+ }else if( strcmp(z,"-scratch")==0 ){
4088
+ i+=2;
4089
+ }else if( strcmp(z,"-pagecache")==0 ){
4090
+ i+=2;
4091
+ }else if( strcmp(z,"-lookaside")==0 ){
4092
+ i+=2;
40434093
}else if( strcmp(z,"-mmap")==0 ){
40444094
i++;
40454095
}else if( strcmp(z,"-vfs")==0 ){
40464096
i++;
40474097
#ifdef SQLITE_ENABLE_VFSTRACE
40484098
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430 #endif
431 }
432 return zResult;
433 }
434
435 struct previous_mode_data {
436 int valid; /* Is there legit data in here? */
437 int mode;
438 int showHeader;
439 int colWidth[100];
 
 
 
 
 
440 };
441
442 /*
443 ** An pointer to an instance of this structure is passed from
444 ** the main program to the callback. This is used to communicate
445 ** state and mode information.
446 */
447 struct callback_data {
 
448 sqlite3 *db; /* The database */
449 int echoOn; /* True to echo input commands */
450 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
451 int statsOn; /* True to display memory stats before each finalize */
452 int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
455 FILE *traceOut; /* Output for sqlite3_trace() */
456 int nErr; /* Number of errors seen */
457 int mode; /* An output mode setting */
458 int writableSchema; /* True if PRAGMA writable_schema=ON */
459 int showHeader; /* True to show column names in List or Column mode */
 
460 char *zDestTable; /* Name of destination table when MODE_Insert */
461 char separator[20]; /* Separator character for MODE_List */
462 char newline[20]; /* Record separator in MODE_Csv */
463 int colWidth[100]; /* Requested width of each column when in column mode*/
464 int actualWidth[100]; /* Actual width of each column */
465 char nullvalue[20]; /* The text to print when a NULL comes back from
466 ** the database */
467 struct previous_mode_data explainPrev;
468 /* Holds the mode information just before
469 ** .explain ON */
470 char outfile[FILENAME_MAX]; /* Filename for *out */
471 const char *zDbFilename; /* name of the database file */
472 char *zFreeOnClose; /* Filename to free when closing */
473 const char *zVfs; /* Name of VFS to use */
474 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
476 int *aiIndent; /* Array of indents used in MODE_Explain */
477 int nIndent; /* Size of array aiIndent[] */
478 int iIndent; /* Index of current op in aiIndent[] */
479 };
480
 
 
 
 
 
 
 
481 /*
482 ** These are the allowed modes.
483 */
484 #define MODE_Line 0 /* One column per line. Blank line between records */
485 #define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
520
521 /*
522 ** A callback for the sqlite3_log() interface.
523 */
524 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
525 struct callback_data *p = (struct callback_data*)pArg;
526 if( p->pLog==0 ) return;
527 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
528 fflush(p->pLog);
529 }
530
@@ -662,11 +673,11 @@
662 ** Output a single term of CSV. Actually, p->separator is used for
663 ** the separator, which may or may not be a comma. p->nullvalue is
664 ** the null value. Strings are quoted if necessary. The separator
665 ** is only issued if bSep is true.
666 */
667 static void output_csv(struct callback_data *p, const char *z, int bSep){
668 FILE *out = p->out;
669 if( z==0 ){
670 fprintf(out,"%s",p->nullvalue);
671 }else{
672 int i;
@@ -711,11 +722,11 @@
711 ** This is the callback routine that the shell
712 ** invokes for each row of a query result.
713 */
714 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
715 int i;
716 struct callback_data *p = (struct callback_data*)pArg;
717
718 switch( p->mode ){
719 case MODE_Line: {
720 int w = 5;
721 if( azArg==0 ) break;
@@ -921,15 +932,15 @@
921 /* since we don't have type info, call the shell_callback with a NULL value */
922 return shell_callback(pArg, nArg, azArg, azCol, NULL);
923 }
924
925 /*
926 ** Set the destination table field of the callback_data structure to
927 ** the name of the table given. Escape any quote characters in the
928 ** table name.
929 */
930 static void set_table_name(struct callback_data *p, const char *zName){
931 int i, n;
932 int needQuote;
933 char *z;
934
935 if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
1015 ** then write the semicolon on a separate line. That way, if a
1016 ** "--" comment occurs at the end of the statement, the comment
1017 ** won't consume the semicolon terminator.
1018 */
1019 static int run_table_dump_query(
1020 struct callback_data *p, /* Query context */
1021 const char *zSelect, /* SELECT statement to extract content */
1022 const char *zFirstRow /* Print before first row, if not NULL */
1023 ){
1024 sqlite3_stmt *pSelect;
1025 int rc;
@@ -1078,11 +1089,11 @@
1078 /*
1079 ** Display memory stats.
1080 */
1081 static int display_stats(
1082 sqlite3 *db, /* Database to query */
1083 struct callback_data *pArg, /* Pointer to struct callback_data */
1084 int bReset /* True to reset the stats */
1085 ){
1086 int iCur;
1087 int iHiwtr;
1088
@@ -1092,25 +1103,23 @@
1092 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1093 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1094 iHiwtr = iCur = -1;
1095 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1096 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1097 /*
1098 ** Not currently used by the CLI.
1099 ** iHiwtr = iCur = -1;
1100 ** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1101 ** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1102 */
1103 iHiwtr = iCur = -1;
1104 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1105 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1106 /*
1107 ** Not currently used by the CLI.
1108 ** iHiwtr = iCur = -1;
1109 ** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1110 ** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1111 */
1112 iHiwtr = iCur = -1;
1113 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1114 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1115 iHiwtr = iCur = -1;
1116 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
1127 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1128 #endif
1129 }
1130
1131 if( pArg && pArg->out && db ){
1132 iHiwtr = iCur = -1;
1133 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1134 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1135 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1136 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1137 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1138 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1139 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1140 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
 
 
1141 iHiwtr = iCur = -1;
1142 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1143 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1144 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1145 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
1187 return 0;
1188 }
1189
1190 /*
1191 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1192 ** and populate the callback_data.aiIndent[] array with the number of
1193 ** spaces each opcode should be indented before it is output.
1194 **
1195 ** The indenting rules are:
1196 **
1197 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
1203 ** Yield SeekGt SeekLt RowSetRead Rewind
1204 ** or if the P1 parameter is one instead of zero,
1205 ** then indent all opcodes between the earlier instruction
1206 ** and "Goto" by 2 spaces.
1207 */
1208 static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1209 const char *zSql; /* The text of the SQL statement */
1210 const char *z; /* Used to check if this is an EXPLAIN */
1211 int *abYield = 0; /* True if op is an OP_Yield */
1212 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1213 int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
1263 }
1264
1265 /*
1266 ** Free the array allocated by explain_data_prepare().
1267 */
1268 static void explain_data_delete(struct callback_data *p){
1269 sqlite3_free(p->aiIndent);
1270 p->aiIndent = 0;
1271 p->nIndent = 0;
1272 p->iIndent = 0;
1273 }
@@ -1280,16 +1291,16 @@
1280 ** This is very similar to SQLite's built-in sqlite3_exec()
1281 ** function except it takes a slightly different callback
1282 ** and callback data argument.
1283 */
1284 static int shell_exec(
1285 sqlite3 *db, /* An open database */
1286 const char *zSql, /* SQL to be evaluated */
1287 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1288 /* (not the same as sqlite3_exec) */
1289 struct callback_data *pArg, /* Pointer to struct callback_data */
1290 char **pzErrMsg /* Error msg written here */
1291 ){
1292 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1293 int rc = SQLITE_OK; /* Return Code */
1294 int rc2;
1295 const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
1453 int rc;
1454 const char *zTable;
1455 const char *zType;
1456 const char *zSql;
1457 const char *zPrepStmt = 0;
1458 struct callback_data *p = (struct callback_data *)pArg;
1459
1460 UNUSED_PARAMETER(azCol);
1461 if( nArg!=3 ) return 1;
1462 zTable = azArg[0];
1463 zType = azArg[1];
@@ -1549,11 +1560,11 @@
1549 **
1550 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1551 ** "ORDER BY rowid DESC" to the end.
1552 */
1553 static int run_schema_dump_query(
1554 struct callback_data *p,
1555 const char *zQuery
1556 ){
1557 int rc;
1558 char *zErr = 0;
1559 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
1649 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1650 " Negative values right-justify\n"
1651 ;
1652
1653 /* Forward reference */
1654 static int process_input(struct callback_data *p, FILE *in);
1655 /*
1656 ** Implementation of the "readfile(X)" SQL function. The entire content
1657 ** of the file named X is read and returned as a BLOB. NULL is returned
1658 ** if the file does not exist or is unreadable.
1659 */
@@ -1694,35 +1705,32 @@
1694 int argc,
1695 sqlite3_value **argv
1696 ){
1697 FILE *out;
1698 const char *z;
1699 int n;
1700 sqlite3_int64 rc;
1701 const char *zFile;
1702
1703 zFile = (const char*)sqlite3_value_text(argv[0]);
1704 if( zFile==0 ) return;
1705 out = fopen(zFile, "wb");
1706 if( out==0 ) return;
1707 z = (const char*)sqlite3_value_blob(argv[1]);
1708 if( z==0 ){
1709 n = 0;
1710 rc = 0;
1711 }else{
1712 n = sqlite3_value_bytes(argv[1]);
1713 rc = fwrite(z, 1, n, out);
1714 }
1715 fclose(out);
1716 sqlite3_result_int64(context, rc);
1717 }
1718
1719 /*
1720 ** Make sure the database is open. If it is not, then open it. If
1721 ** the database fails to open, print an error message and exit.
1722 */
1723 static void open_db(struct callback_data *p, int keepAlive){
1724 if( p->db==0 ){
1725 sqlite3_initialize();
1726 sqlite3_open(p->zDbFilename, &p->db);
1727 db = p->db;
1728 if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
1899 /*
1900 ** A routine for handling output from sqlite3_trace().
1901 */
1902 static void sql_trace_callback(void *pArg, const char *z){
1903 FILE *f = (FILE*)pArg;
1904 if( f ) fprintf(f, "%s\n", z);
 
 
 
 
1905 }
1906
1907 /*
1908 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1909 ** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
2017 ** Try to transfer data for table zTable. If an error is seen while
2018 ** moving forward, try to go backwards. The backwards movement won't
2019 ** work for WITHOUT ROWID tables.
2020 */
2021 static void tryToCloneData(
2022 struct callback_data *p,
2023 sqlite3 *newDb,
2024 const char *zTable
2025 ){
2026 sqlite3_stmt *pQuery = 0;
2027 sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
2130 ** each row, invoke xForEach() on the object defined by that row.
2131 ** If an error is encountered while moving forward through the
2132 ** sqlite_master table, try again moving backwards.
2133 */
2134 static void tryToCloneSchema(
2135 struct callback_data *p,
2136 sqlite3 *newDb,
2137 const char *zWhere,
2138 void (*xForEach)(struct callback_data*,sqlite3*,const char*)
2139 ){
2140 sqlite3_stmt *pQuery = 0;
2141 char *zQuery = 0;
2142 int rc;
2143 const unsigned char *zName;
@@ -2204,11 +2216,11 @@
2204 /*
2205 ** Open a new database file named "zNewDb". Try to recover as much information
2206 ** as possible out of the main database (which might be corrupt) and write it
2207 ** into zNewDb.
2208 */
2209 static void tryToClone(struct callback_data *p, const char *zNewDb){
2210 int rc;
2211 sqlite3 *newDb = 0;
2212 if( access(zNewDb,0)==0 ){
2213 fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
2214 return;
@@ -2229,11 +2241,11 @@
2229 }
2230
2231 /*
2232 ** Change the output file back to stdout
2233 */
2234 static void output_reset(struct callback_data *p){
2235 if( p->outfile[0]=='|' ){
2236 pclose(p->out);
2237 }else{
2238 output_file_close(p->out);
2239 }
@@ -2245,11 +2257,11 @@
2245 ** If an input line begins with "." then invoke this routine to
2246 ** process that line.
2247 **
2248 ** Return 1 on error, 2 to exit, and 0 otherwise.
2249 */
2250 static int do_meta_command(char *zLine, struct callback_data *p){
2251 int i = 1;
2252 int nArg = 0;
2253 int n, c;
2254 int rc = 0;
2255 char *azArg[50];
@@ -2363,11 +2375,11 @@
2363 rc = 1;
2364 }
2365 }else
2366
2367 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2368 struct callback_data data;
2369 char *zErrMsg = 0;
2370 open_db(p, 0);
2371 memcpy(&data, p, sizeof(data));
2372 data.showHeader = 1;
2373 data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
2461 }else
2462
2463 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2464 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2465 if(val == 1) {
2466 if(!p->explainPrev.valid) {
2467 p->explainPrev.valid = 1;
2468 p->explainPrev.mode = p->mode;
2469 p->explainPrev.showHeader = p->showHeader;
2470 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2471 }
2472 /* We could put this code under the !p->explainValid
2473 ** condition so that it does not execute if we are already in
2474 ** explain mode. However, always executing it allows us an easy
2475 ** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
2485 p->colWidth[3] = 4; /* P2 */
2486 p->colWidth[4] = 4; /* P3 */
2487 p->colWidth[5] = 13; /* P4 */
2488 p->colWidth[6] = 2; /* P5 */
2489 p->colWidth[7] = 13; /* Comment */
2490 }else if (p->explainPrev.valid) {
2491 p->explainPrev.valid = 0;
2492 p->mode = p->explainPrev.mode;
2493 p->showHeader = p->explainPrev.showHeader;
2494 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2495 }
2496 }else
2497
2498 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2499 struct callback_data data;
2500 char *zErrMsg = 0;
2501 int doStats = 0;
2502 if( nArg!=1 ){
2503 fprintf(stderr, "Usage: .fullschema\n");
2504 rc = 1;
@@ -2511,11 +2523,11 @@
2511 rc = sqlite3_exec(p->db,
2512 "SELECT sql FROM"
2513 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2514 " FROM sqlite_master UNION ALL"
2515 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2516 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2517 "ORDER BY rowid",
2518 callback, &data, &zErrMsg
2519 );
2520 if( rc==SQLITE_OK ){
2521 sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
2715 sqlite3_finalize(pStmt);
2716 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2717 }else
2718
2719 if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2720 struct callback_data data;
2721 char *zErrMsg = 0;
2722 open_db(p, 0);
2723 memcpy(&data, p, sizeof(data));
2724 data.showHeader = 0;
2725 data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
3009 }
3010 sqlite3_close(pSrc);
3011 }else
3012
3013 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3014 struct callback_data data;
3015 char *zErrMsg = 0;
3016 open_db(p, 0);
3017 memcpy(&data, p, sizeof(data));
3018 data.showHeader = 0;
3019 data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
3065 rc = sqlite3_exec(p->db,
3066 "SELECT sql FROM "
3067 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3068 " FROM sqlite_master UNION ALL"
3069 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3070 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
3071 "ORDER BY rowid",
3072 callback, &data, &zErrMsg
3073 );
3074 }else{
3075 fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
3126
3127 if( c=='s'
3128 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
3129 ){
3130 char *zCmd;
3131 int i;
3132 if( nArg<2 ){
3133 fprintf(stderr, "Usage: .system COMMAND\n");
3134 rc = 1;
3135 goto meta_command_exit;
3136 }
@@ -3137,12 +3149,13 @@
3137 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3138 for(i=2; i<nArg; i++){
3139 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3140 zCmd, azArg[i]);
3141 }
3142 (void)system(zCmd);
3143 sqlite3_free(zCmd);
 
3144 }else
3145
3146 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
3147 int i;
3148 if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
3150 rc = 1;
3151 goto meta_command_exit;
3152 }
3153 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
3154 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3155 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
3156 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
3157 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
3158 fprintf(p->out,"%9.9s: ", "nullvalue");
3159 output_c_string(p->out, p->nullvalue);
3160 fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
3542 ** is saved only if input is interactive. An interrupt signal will
3543 ** cause this routine to exit immediately, unless input is interactive.
3544 **
3545 ** Return the number of errors.
3546 */
3547 static int process_input(struct callback_data *p, FILE *in){
3548 char *zLine = 0; /* A single input line */
3549 char *zSql = 0; /* Accumulated SQL text */
3550 int nLine; /* Length of current line */
3551 int nSql = 0; /* Bytes of zSql[] used */
3552 int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
3721 ** parameter is NULL, take input from ~/.sqliterc
3722 **
3723 ** Returns the number of errors.
3724 */
3725 static int process_sqliterc(
3726 struct callback_data *p, /* Configuration data */
3727 const char *sqliterc_override /* Name of config file. NULL to use default */
3728 ){
3729 char *home_dir = NULL;
3730 const char *sqliterc = sqliterc_override;
3731 char *zBuf = 0;
@@ -3774,16 +3787,19 @@
3774 " -help show this message\n"
3775 " -html set output mode to HTML\n"
3776 " -interactive force interactive I/O\n"
3777 " -line set output mode to 'line'\n"
3778 " -list set output mode to 'list'\n"
 
3779 " -mmap N default mmap size set to N\n"
3780 #ifdef SQLITE_ENABLE_MULTIPLEX
3781 " -multiplex enable the multiplexor VFS\n"
3782 #endif
3783 " -newline SEP set newline character(s) for CSV\n"
3784 " -nullvalue TEXT set text string for NULL values. Default ''\n"
 
 
3785 " -separator SEP set output field separator. Default: '|'\n"
3786 " -stats print memory stats before each finalize\n"
3787 " -version show SQLite version\n"
3788 " -vfs NAME use NAME as the default VFS\n"
3789 #ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
3804 }
3805
3806 /*
3807 ** Initialize the state information in data
3808 */
3809 static void main_init(struct callback_data *data) {
3810 memset(data, 0, sizeof(*data));
3811 data->mode = MODE_List;
3812 memcpy(data->separator,"|", 2);
3813 memcpy(data->newline,"\r\n", 3);
3814 data->showHeader = 0;
 
3815 sqlite3_config(SQLITE_CONFIG_URI, 1);
3816 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
 
3817 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3818 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
3819 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
3820 }
3821
3822 /*
3823 ** Output text to the console in a font that attracts extra attention.
3824 */
@@ -3852,11 +3869,11 @@
3852 return argv[i];
3853 }
3854
3855 int main(int argc, char **argv){
3856 char *zErrMsg = 0;
3857 struct callback_data data;
3858 const char *zInitFile = 0;
3859 char *zFirstCmd = 0;
3860 int i;
3861 int rc = 0;
3862 int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
3923 zSize = cmdline_option_value(argc, argv, ++i);
3924 szHeap = integerValue(zSize);
3925 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3926 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3927 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3928 #ifdef SQLITE_ENABLE_VFSTRACE
3929 }else if( strcmp(z,"-vfstrace")==0 ){
3930 extern int vfstrace_register(
3931 const char *zTraceName,
3932 const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
4038 stdin_is_interactive = 1;
4039 }else if( strcmp(z,"-batch")==0 ){
4040 stdin_is_interactive = 0;
4041 }else if( strcmp(z,"-heap")==0 ){
4042 i++;
 
 
 
 
 
 
4043 }else if( strcmp(z,"-mmap")==0 ){
4044 i++;
4045 }else if( strcmp(z,"-vfs")==0 ){
4046 i++;
4047 #ifdef SQLITE_ENABLE_VFSTRACE
4048
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430 #endif
431 }
432 return zResult;
433 }
434
435 /*
436 ** Shell output mode information from before ".explain on",
437 ** saved so that it can be restored by ".explain off"
438 */
439 typedef struct SavedModeInfo SavedModeInfo;
440 struct SavedModeInfo {
441 int valid; /* Is there legit data in here? */
442 int mode; /* Mode prior to ".explain on" */
443 int showHeader; /* The ".header" setting prior to ".explain on" */
444 int colWidth[100]; /* Column widths prior to ".explain on" */
445 };
446
447 /*
448 ** State information about the database connection is contained in an
449 ** instance of the following structure.
 
450 */
451 typedef struct ShellState ShellState;
452 struct ShellState {
453 sqlite3 *db; /* The database */
454 int echoOn; /* True to echo input commands */
455 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
456 int statsOn; /* True to display memory stats before each finalize */
457 int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
460 FILE *traceOut; /* Output for sqlite3_trace() */
461 int nErr; /* Number of errors seen */
462 int mode; /* An output mode setting */
463 int writableSchema; /* True if PRAGMA writable_schema=ON */
464 int showHeader; /* True to show column names in List or Column mode */
465 unsigned shellFlgs; /* Various flags */
466 char *zDestTable; /* Name of destination table when MODE_Insert */
467 char separator[20]; /* Separator character for MODE_List */
468 char newline[20]; /* Record separator in MODE_Csv */
469 int colWidth[100]; /* Requested width of each column when in column mode*/
470 int actualWidth[100]; /* Actual width of each column */
471 char nullvalue[20]; /* The text to print when a NULL comes back from
472 ** the database */
473 SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
 
 
474 char outfile[FILENAME_MAX]; /* Filename for *out */
475 const char *zDbFilename; /* name of the database file */
476 char *zFreeOnClose; /* Filename to free when closing */
477 const char *zVfs; /* Name of VFS to use */
478 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
480 int *aiIndent; /* Array of indents used in MODE_Explain */
481 int nIndent; /* Size of array aiIndent[] */
482 int iIndent; /* Index of current op in aiIndent[] */
483 };
484
485 /*
486 ** These are the allowed shellFlgs values
487 */
488 #define SHFLG_Scratch 0x00001 /* The --scratch option is used */
489 #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
490 #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
491
492 /*
493 ** These are the allowed modes.
494 */
495 #define MODE_Line 0 /* One column per line. Blank line between records */
496 #define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
531
532 /*
533 ** A callback for the sqlite3_log() interface.
534 */
535 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
536 ShellState *p = (ShellState*)pArg;
537 if( p->pLog==0 ) return;
538 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
539 fflush(p->pLog);
540 }
541
@@ -662,11 +673,11 @@
673 ** Output a single term of CSV. Actually, p->separator is used for
674 ** the separator, which may or may not be a comma. p->nullvalue is
675 ** the null value. Strings are quoted if necessary. The separator
676 ** is only issued if bSep is true.
677 */
678 static void output_csv(ShellState *p, const char *z, int bSep){
679 FILE *out = p->out;
680 if( z==0 ){
681 fprintf(out,"%s",p->nullvalue);
682 }else{
683 int i;
@@ -711,11 +722,11 @@
722 ** This is the callback routine that the shell
723 ** invokes for each row of a query result.
724 */
725 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
726 int i;
727 ShellState *p = (ShellState*)pArg;
728
729 switch( p->mode ){
730 case MODE_Line: {
731 int w = 5;
732 if( azArg==0 ) break;
@@ -921,15 +932,15 @@
932 /* since we don't have type info, call the shell_callback with a NULL value */
933 return shell_callback(pArg, nArg, azArg, azCol, NULL);
934 }
935
936 /*
937 ** Set the destination table field of the ShellState structure to
938 ** the name of the table given. Escape any quote characters in the
939 ** table name.
940 */
941 static void set_table_name(ShellState *p, const char *zName){
942 int i, n;
943 int needQuote;
944 char *z;
945
946 if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
1026 ** then write the semicolon on a separate line. That way, if a
1027 ** "--" comment occurs at the end of the statement, the comment
1028 ** won't consume the semicolon terminator.
1029 */
1030 static int run_table_dump_query(
1031 ShellState *p, /* Query context */
1032 const char *zSelect, /* SELECT statement to extract content */
1033 const char *zFirstRow /* Print before first row, if not NULL */
1034 ){
1035 sqlite3_stmt *pSelect;
1036 int rc;
@@ -1078,11 +1089,11 @@
1089 /*
1090 ** Display memory stats.
1091 */
1092 static int display_stats(
1093 sqlite3 *db, /* Database to query */
1094 ShellState *pArg, /* Pointer to ShellState */
1095 int bReset /* True to reset the stats */
1096 ){
1097 int iCur;
1098 int iHiwtr;
1099
@@ -1092,25 +1103,23 @@
1103 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1104 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1105 iHiwtr = iCur = -1;
1106 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1107 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1108 if( pArg->shellFlgs & SHFLG_Pagecache ){
1109 iHiwtr = iCur = -1;
1110 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1111 fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1112 }
 
1113 iHiwtr = iCur = -1;
1114 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1115 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1116 if( pArg->shellFlgs & SHFLG_Scratch ){
1117 iHiwtr = iCur = -1;
1118 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1119 fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1120 }
 
1121 iHiwtr = iCur = -1;
1122 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1123 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1124 iHiwtr = iCur = -1;
1125 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
1136 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1137 #endif
1138 }
1139
1140 if( pArg && pArg->out && db ){
1141 if( pArg->shellFlgs & SHFLG_Lookaside ){
1142 iHiwtr = iCur = -1;
1143 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1144 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1145 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1146 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1147 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1148 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1149 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1150 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1151 }
1152 iHiwtr = iCur = -1;
1153 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1154 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1155 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1156 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
1198 return 0;
1199 }
1200
1201 /*
1202 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1203 ** and populate the ShellState.aiIndent[] array with the number of
1204 ** spaces each opcode should be indented before it is output.
1205 **
1206 ** The indenting rules are:
1207 **
1208 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
1214 ** Yield SeekGt SeekLt RowSetRead Rewind
1215 ** or if the P1 parameter is one instead of zero,
1216 ** then indent all opcodes between the earlier instruction
1217 ** and "Goto" by 2 spaces.
1218 */
1219 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
1220 const char *zSql; /* The text of the SQL statement */
1221 const char *z; /* Used to check if this is an EXPLAIN */
1222 int *abYield = 0; /* True if op is an OP_Yield */
1223 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1224 int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
1274 }
1275
1276 /*
1277 ** Free the array allocated by explain_data_prepare().
1278 */
1279 static void explain_data_delete(ShellState *p){
1280 sqlite3_free(p->aiIndent);
1281 p->aiIndent = 0;
1282 p->nIndent = 0;
1283 p->iIndent = 0;
1284 }
@@ -1280,16 +1291,16 @@
1291 ** This is very similar to SQLite's built-in sqlite3_exec()
1292 ** function except it takes a slightly different callback
1293 ** and callback data argument.
1294 */
1295 static int shell_exec(
1296 sqlite3 *db, /* An open database */
1297 const char *zSql, /* SQL to be evaluated */
1298 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1299 /* (not the same as sqlite3_exec) */
1300 ShellState *pArg, /* Pointer to ShellState */
1301 char **pzErrMsg /* Error msg written here */
1302 ){
1303 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1304 int rc = SQLITE_OK; /* Return Code */
1305 int rc2;
1306 const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
1464 int rc;
1465 const char *zTable;
1466 const char *zType;
1467 const char *zSql;
1468 const char *zPrepStmt = 0;
1469 ShellState *p = (ShellState *)pArg;
1470
1471 UNUSED_PARAMETER(azCol);
1472 if( nArg!=3 ) return 1;
1473 zTable = azArg[0];
1474 zType = azArg[1];
@@ -1549,11 +1560,11 @@
1560 **
1561 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1562 ** "ORDER BY rowid DESC" to the end.
1563 */
1564 static int run_schema_dump_query(
1565 ShellState *p,
1566 const char *zQuery
1567 ){
1568 int rc;
1569 char *zErr = 0;
1570 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
1660 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1661 " Negative values right-justify\n"
1662 ;
1663
1664 /* Forward reference */
1665 static int process_input(ShellState *p, FILE *in);
1666 /*
1667 ** Implementation of the "readfile(X)" SQL function. The entire content
1668 ** of the file named X is read and returned as a BLOB. NULL is returned
1669 ** if the file does not exist or is unreadable.
1670 */
@@ -1694,35 +1705,32 @@
1705 int argc,
1706 sqlite3_value **argv
1707 ){
1708 FILE *out;
1709 const char *z;
 
1710 sqlite3_int64 rc;
1711 const char *zFile;
1712
1713 zFile = (const char*)sqlite3_value_text(argv[0]);
1714 if( zFile==0 ) return;
1715 out = fopen(zFile, "wb");
1716 if( out==0 ) return;
1717 z = (const char*)sqlite3_value_blob(argv[1]);
1718 if( z==0 ){
 
1719 rc = 0;
1720 }else{
1721 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
 
1722 }
1723 fclose(out);
1724 sqlite3_result_int64(context, rc);
1725 }
1726
1727 /*
1728 ** Make sure the database is open. If it is not, then open it. If
1729 ** the database fails to open, print an error message and exit.
1730 */
1731 static void open_db(ShellState *p, int keepAlive){
1732 if( p->db==0 ){
1733 sqlite3_initialize();
1734 sqlite3_open(p->zDbFilename, &p->db);
1735 db = p->db;
1736 if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
1907 /*
1908 ** A routine for handling output from sqlite3_trace().
1909 */
1910 static void sql_trace_callback(void *pArg, const char *z){
1911 FILE *f = (FILE*)pArg;
1912 if( f ){
1913 int i = (int)strlen(z);
1914 while( i>0 && z[i-1]==';' ){ i--; }
1915 fprintf(f, "%.*s;\n", i, z);
1916 }
1917 }
1918
1919 /*
1920 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1921 ** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
2029 ** Try to transfer data for table zTable. If an error is seen while
2030 ** moving forward, try to go backwards. The backwards movement won't
2031 ** work for WITHOUT ROWID tables.
2032 */
2033 static void tryToCloneData(
2034 ShellState *p,
2035 sqlite3 *newDb,
2036 const char *zTable
2037 ){
2038 sqlite3_stmt *pQuery = 0;
2039 sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
2142 ** each row, invoke xForEach() on the object defined by that row.
2143 ** If an error is encountered while moving forward through the
2144 ** sqlite_master table, try again moving backwards.
2145 */
2146 static void tryToCloneSchema(
2147 ShellState *p,
2148 sqlite3 *newDb,
2149 const char *zWhere,
2150 void (*xForEach)(ShellState*,sqlite3*,const char*)
2151 ){
2152 sqlite3_stmt *pQuery = 0;
2153 char *zQuery = 0;
2154 int rc;
2155 const unsigned char *zName;
@@ -2204,11 +2216,11 @@
2216 /*
2217 ** Open a new database file named "zNewDb". Try to recover as much information
2218 ** as possible out of the main database (which might be corrupt) and write it
2219 ** into zNewDb.
2220 */
2221 static void tryToClone(ShellState *p, const char *zNewDb){
2222 int rc;
2223 sqlite3 *newDb = 0;
2224 if( access(zNewDb,0)==0 ){
2225 fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
2226 return;
@@ -2229,11 +2241,11 @@
2241 }
2242
2243 /*
2244 ** Change the output file back to stdout
2245 */
2246 static void output_reset(ShellState *p){
2247 if( p->outfile[0]=='|' ){
2248 pclose(p->out);
2249 }else{
2250 output_file_close(p->out);
2251 }
@@ -2245,11 +2257,11 @@
2257 ** If an input line begins with "." then invoke this routine to
2258 ** process that line.
2259 **
2260 ** Return 1 on error, 2 to exit, and 0 otherwise.
2261 */
2262 static int do_meta_command(char *zLine, ShellState *p){
2263 int i = 1;
2264 int nArg = 0;
2265 int n, c;
2266 int rc = 0;
2267 char *azArg[50];
@@ -2363,11 +2375,11 @@
2375 rc = 1;
2376 }
2377 }else
2378
2379 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2380 ShellState data;
2381 char *zErrMsg = 0;
2382 open_db(p, 0);
2383 memcpy(&data, p, sizeof(data));
2384 data.showHeader = 1;
2385 data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
2473 }else
2474
2475 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2476 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2477 if(val == 1) {
2478 if(!p->normalMode.valid) {
2479 p->normalMode.valid = 1;
2480 p->normalMode.mode = p->mode;
2481 p->normalMode.showHeader = p->showHeader;
2482 memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
2483 }
2484 /* We could put this code under the !p->explainValid
2485 ** condition so that it does not execute if we are already in
2486 ** explain mode. However, always executing it allows us an easy
2487 ** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
2497 p->colWidth[3] = 4; /* P2 */
2498 p->colWidth[4] = 4; /* P3 */
2499 p->colWidth[5] = 13; /* P4 */
2500 p->colWidth[6] = 2; /* P5 */
2501 p->colWidth[7] = 13; /* Comment */
2502 }else if (p->normalMode.valid) {
2503 p->normalMode.valid = 0;
2504 p->mode = p->normalMode.mode;
2505 p->showHeader = p->normalMode.showHeader;
2506 memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
2507 }
2508 }else
2509
2510 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2511 ShellState data;
2512 char *zErrMsg = 0;
2513 int doStats = 0;
2514 if( nArg!=1 ){
2515 fprintf(stderr, "Usage: .fullschema\n");
2516 rc = 1;
@@ -2511,11 +2523,11 @@
2523 rc = sqlite3_exec(p->db,
2524 "SELECT sql FROM"
2525 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2526 " FROM sqlite_master UNION ALL"
2527 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2528 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
2529 "ORDER BY rowid",
2530 callback, &data, &zErrMsg
2531 );
2532 if( rc==SQLITE_OK ){
2533 sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
2727 sqlite3_finalize(pStmt);
2728 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2729 }else
2730
2731 if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2732 ShellState data;
2733 char *zErrMsg = 0;
2734 open_db(p, 0);
2735 memcpy(&data, p, sizeof(data));
2736 data.showHeader = 0;
2737 data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
3021 }
3022 sqlite3_close(pSrc);
3023 }else
3024
3025 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3026 ShellState data;
3027 char *zErrMsg = 0;
3028 open_db(p, 0);
3029 memcpy(&data, p, sizeof(data));
3030 data.showHeader = 0;
3031 data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
3077 rc = sqlite3_exec(p->db,
3078 "SELECT sql FROM "
3079 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3080 " FROM sqlite_master UNION ALL"
3081 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3082 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
3083 "ORDER BY rowid",
3084 callback, &data, &zErrMsg
3085 );
3086 }else{
3087 fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
3138
3139 if( c=='s'
3140 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
3141 ){
3142 char *zCmd;
3143 int i, x;
3144 if( nArg<2 ){
3145 fprintf(stderr, "Usage: .system COMMAND\n");
3146 rc = 1;
3147 goto meta_command_exit;
3148 }
@@ -3137,12 +3149,13 @@
3149 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3150 for(i=2; i<nArg; i++){
3151 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3152 zCmd, azArg[i]);
3153 }
3154 x = system(zCmd);
3155 sqlite3_free(zCmd);
3156 if( x ) fprintf(stderr, "System command returns %d\n", x);
3157 }else
3158
3159 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
3160 int i;
3161 if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
3163 rc = 1;
3164 goto meta_command_exit;
3165 }
3166 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
3167 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3168 fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
3169 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
3170 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
3171 fprintf(p->out,"%9.9s: ", "nullvalue");
3172 output_c_string(p->out, p->nullvalue);
3173 fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
3555 ** is saved only if input is interactive. An interrupt signal will
3556 ** cause this routine to exit immediately, unless input is interactive.
3557 **
3558 ** Return the number of errors.
3559 */
3560 static int process_input(ShellState *p, FILE *in){
3561 char *zLine = 0; /* A single input line */
3562 char *zSql = 0; /* Accumulated SQL text */
3563 int nLine; /* Length of current line */
3564 int nSql = 0; /* Bytes of zSql[] used */
3565 int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
3734 ** parameter is NULL, take input from ~/.sqliterc
3735 **
3736 ** Returns the number of errors.
3737 */
3738 static int process_sqliterc(
3739 ShellState *p, /* Configuration data */
3740 const char *sqliterc_override /* Name of config file. NULL to use default */
3741 ){
3742 char *home_dir = NULL;
3743 const char *sqliterc = sqliterc_override;
3744 char *zBuf = 0;
@@ -3774,16 +3787,19 @@
3787 " -help show this message\n"
3788 " -html set output mode to HTML\n"
3789 " -interactive force interactive I/O\n"
3790 " -line set output mode to 'line'\n"
3791 " -list set output mode to 'list'\n"
3792 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
3793 " -mmap N default mmap size set to N\n"
3794 #ifdef SQLITE_ENABLE_MULTIPLEX
3795 " -multiplex enable the multiplexor VFS\n"
3796 #endif
3797 " -newline SEP set newline character(s) for CSV\n"
3798 " -nullvalue TEXT set text string for NULL values. Default ''\n"
3799 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
3800 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
3801 " -separator SEP set output field separator. Default: '|'\n"
3802 " -stats print memory stats before each finalize\n"
3803 " -version show SQLite version\n"
3804 " -vfs NAME use NAME as the default VFS\n"
3805 #ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
3820 }
3821
3822 /*
3823 ** Initialize the state information in data
3824 */
3825 static void main_init(ShellState *data) {
3826 memset(data, 0, sizeof(*data));
3827 data->mode = MODE_List;
3828 memcpy(data->separator,"|", 2);
3829 memcpy(data->newline,"\r\n", 3);
3830 data->showHeader = 0;
3831 data->shellFlgs = SHFLG_Lookaside;
3832 sqlite3_config(SQLITE_CONFIG_URI, 1);
3833 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
3834 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
3835 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3836 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
 
3837 }
3838
3839 /*
3840 ** Output text to the console in a font that attracts extra attention.
3841 */
@@ -3852,11 +3869,11 @@
3869 return argv[i];
3870 }
3871
3872 int main(int argc, char **argv){
3873 char *zErrMsg = 0;
3874 ShellState data;
3875 const char *zInitFile = 0;
3876 char *zFirstCmd = 0;
3877 int i;
3878 int rc = 0;
3879 int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
3940 zSize = cmdline_option_value(argc, argv, ++i);
3941 szHeap = integerValue(zSize);
3942 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3943 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3944 #endif
3945 }else if( strcmp(z,"-scratch")==0 ){
3946 int n, sz;
3947 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3948 if( sz>400000 ) sz = 400000;
3949 if( sz<2500 ) sz = 2500;
3950 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3951 if( n>10 ) n = 10;
3952 if( n<1 ) n = 1;
3953 sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
3954 data.shellFlgs |= SHFLG_Scratch;
3955 }else if( strcmp(z,"-pagecache")==0 ){
3956 int n, sz;
3957 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3958 if( sz>70000 ) sz = 70000;
3959 if( sz<800 ) sz = 800;
3960 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3961 if( n<10 ) n = 10;
3962 sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
3963 data.shellFlgs |= SHFLG_Pagecache;
3964 }else if( strcmp(z,"-lookaside")==0 ){
3965 int n, sz;
3966 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3967 if( sz<0 ) sz = 0;
3968 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3969 if( n<0 ) n = 0;
3970 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
3971 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
3972 #ifdef SQLITE_ENABLE_VFSTRACE
3973 }else if( strcmp(z,"-vfstrace")==0 ){
3974 extern int vfstrace_register(
3975 const char *zTraceName,
3976 const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
4082 stdin_is_interactive = 1;
4083 }else if( strcmp(z,"-batch")==0 ){
4084 stdin_is_interactive = 0;
4085 }else if( strcmp(z,"-heap")==0 ){
4086 i++;
4087 }else if( strcmp(z,"-scratch")==0 ){
4088 i+=2;
4089 }else if( strcmp(z,"-pagecache")==0 ){
4090 i+=2;
4091 }else if( strcmp(z,"-lookaside")==0 ){
4092 i+=2;
4093 }else if( strcmp(z,"-mmap")==0 ){
4094 i++;
4095 }else if( strcmp(z,"-vfs")==0 ){
4096 i++;
4097 #ifdef SQLITE_ENABLE_VFSTRACE
4098
+137 -87
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430430
#endif
431431
}
432432
return zResult;
433433
}
434434
435
-struct previous_mode_data {
436
- int valid; /* Is there legit data in here? */
437
- int mode;
438
- int showHeader;
439
- int colWidth[100];
435
+/*
436
+** Shell output mode information from before ".explain on",
437
+** saved so that it can be restored by ".explain off"
438
+*/
439
+typedef struct SavedModeInfo SavedModeInfo;
440
+struct SavedModeInfo {
441
+ int valid; /* Is there legit data in here? */
442
+ int mode; /* Mode prior to ".explain on" */
443
+ int showHeader; /* The ".header" setting prior to ".explain on" */
444
+ int colWidth[100]; /* Column widths prior to ".explain on" */
440445
};
441446
442447
/*
443
-** An pointer to an instance of this structure is passed from
444
-** the main program to the callback. This is used to communicate
445
-** state and mode information.
448
+** State information about the database connection is contained in an
449
+** instance of the following structure.
446450
*/
447
-struct callback_data {
451
+typedef struct ShellState ShellState;
452
+struct ShellState {
448453
sqlite3 *db; /* The database */
449454
int echoOn; /* True to echo input commands */
450455
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
451456
int statsOn; /* True to display memory stats before each finalize */
452457
int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
455460
FILE *traceOut; /* Output for sqlite3_trace() */
456461
int nErr; /* Number of errors seen */
457462
int mode; /* An output mode setting */
458463
int writableSchema; /* True if PRAGMA writable_schema=ON */
459464
int showHeader; /* True to show column names in List or Column mode */
465
+ unsigned shellFlgs; /* Various flags */
460466
char *zDestTable; /* Name of destination table when MODE_Insert */
461467
char separator[20]; /* Separator character for MODE_List */
462468
char newline[20]; /* Record separator in MODE_Csv */
463469
int colWidth[100]; /* Requested width of each column when in column mode*/
464470
int actualWidth[100]; /* Actual width of each column */
465471
char nullvalue[20]; /* The text to print when a NULL comes back from
466472
** the database */
467
- struct previous_mode_data explainPrev;
468
- /* Holds the mode information just before
469
- ** .explain ON */
473
+ SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
470474
char outfile[FILENAME_MAX]; /* Filename for *out */
471475
const char *zDbFilename; /* name of the database file */
472476
char *zFreeOnClose; /* Filename to free when closing */
473477
const char *zVfs; /* Name of VFS to use */
474478
sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
476480
int *aiIndent; /* Array of indents used in MODE_Explain */
477481
int nIndent; /* Size of array aiIndent[] */
478482
int iIndent; /* Index of current op in aiIndent[] */
479483
};
480484
485
+/*
486
+** These are the allowed shellFlgs values
487
+*/
488
+#define SHFLG_Scratch 0x00001 /* The --scratch option is used */
489
+#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
490
+#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
491
+
481492
/*
482493
** These are the allowed modes.
483494
*/
484495
#define MODE_Line 0 /* One column per line. Blank line between records */
485496
#define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
520531
521532
/*
522533
** A callback for the sqlite3_log() interface.
523534
*/
524535
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
525
- struct callback_data *p = (struct callback_data*)pArg;
536
+ ShellState *p = (ShellState*)pArg;
526537
if( p->pLog==0 ) return;
527538
fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
528539
fflush(p->pLog);
529540
}
530541
@@ -662,11 +673,11 @@
662673
** Output a single term of CSV. Actually, p->separator is used for
663674
** the separator, which may or may not be a comma. p->nullvalue is
664675
** the null value. Strings are quoted if necessary. The separator
665676
** is only issued if bSep is true.
666677
*/
667
-static void output_csv(struct callback_data *p, const char *z, int bSep){
678
+static void output_csv(ShellState *p, const char *z, int bSep){
668679
FILE *out = p->out;
669680
if( z==0 ){
670681
fprintf(out,"%s",p->nullvalue);
671682
}else{
672683
int i;
@@ -711,11 +722,11 @@
711722
** This is the callback routine that the shell
712723
** invokes for each row of a query result.
713724
*/
714725
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
715726
int i;
716
- struct callback_data *p = (struct callback_data*)pArg;
727
+ ShellState *p = (ShellState*)pArg;
717728
718729
switch( p->mode ){
719730
case MODE_Line: {
720731
int w = 5;
721732
if( azArg==0 ) break;
@@ -921,15 +932,15 @@
921932
/* since we don't have type info, call the shell_callback with a NULL value */
922933
return shell_callback(pArg, nArg, azArg, azCol, NULL);
923934
}
924935
925936
/*
926
-** Set the destination table field of the callback_data structure to
937
+** Set the destination table field of the ShellState structure to
927938
** the name of the table given. Escape any quote characters in the
928939
** table name.
929940
*/
930
-static void set_table_name(struct callback_data *p, const char *zName){
941
+static void set_table_name(ShellState *p, const char *zName){
931942
int i, n;
932943
int needQuote;
933944
char *z;
934945
935946
if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
10151026
** then write the semicolon on a separate line. That way, if a
10161027
** "--" comment occurs at the end of the statement, the comment
10171028
** won't consume the semicolon terminator.
10181029
*/
10191030
static int run_table_dump_query(
1020
- struct callback_data *p, /* Query context */
1031
+ ShellState *p, /* Query context */
10211032
const char *zSelect, /* SELECT statement to extract content */
10221033
const char *zFirstRow /* Print before first row, if not NULL */
10231034
){
10241035
sqlite3_stmt *pSelect;
10251036
int rc;
@@ -1078,11 +1089,11 @@
10781089
/*
10791090
** Display memory stats.
10801091
*/
10811092
static int display_stats(
10821093
sqlite3 *db, /* Database to query */
1083
- struct callback_data *pArg, /* Pointer to struct callback_data */
1094
+ ShellState *pArg, /* Pointer to ShellState */
10841095
int bReset /* True to reset the stats */
10851096
){
10861097
int iCur;
10871098
int iHiwtr;
10881099
@@ -1092,25 +1103,23 @@
10921103
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
10931104
fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
10941105
iHiwtr = iCur = -1;
10951106
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
10961107
fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1097
-/*
1098
-** Not currently used by the CLI.
1099
-** iHiwtr = iCur = -1;
1100
-** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1101
-** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1102
-*/
1108
+ if( pArg->shellFlgs & SHFLG_Pagecache ){
1109
+ iHiwtr = iCur = -1;
1110
+ sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1111
+ fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1112
+ }
11031113
iHiwtr = iCur = -1;
11041114
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
11051115
fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1106
-/*
1107
-** Not currently used by the CLI.
1108
-** iHiwtr = iCur = -1;
1109
-** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1110
-** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1111
-*/
1116
+ if( pArg->shellFlgs & SHFLG_Scratch ){
1117
+ iHiwtr = iCur = -1;
1118
+ sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1119
+ fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1120
+ }
11121121
iHiwtr = iCur = -1;
11131122
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
11141123
fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
11151124
iHiwtr = iCur = -1;
11161125
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
11271136
fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
11281137
#endif
11291138
}
11301139
11311140
if( pArg && pArg->out && db ){
1132
- iHiwtr = iCur = -1;
1133
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1134
- fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1135
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1136
- fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1137
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1138
- fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1139
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1140
- fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1141
+ if( pArg->shellFlgs & SHFLG_Lookaside ){
1142
+ iHiwtr = iCur = -1;
1143
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1144
+ fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1145
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1146
+ fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1147
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1148
+ fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1149
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1150
+ fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1151
+ }
11411152
iHiwtr = iCur = -1;
11421153
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11431154
fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
11441155
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11451156
fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
11871198
return 0;
11881199
}
11891200
11901201
/*
11911202
** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1192
-** and populate the callback_data.aiIndent[] array with the number of
1203
+** and populate the ShellState.aiIndent[] array with the number of
11931204
** spaces each opcode should be indented before it is output.
11941205
**
11951206
** The indenting rules are:
11961207
**
11971208
** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
12031214
** Yield SeekGt SeekLt RowSetRead Rewind
12041215
** or if the P1 parameter is one instead of zero,
12051216
** then indent all opcodes between the earlier instruction
12061217
** and "Goto" by 2 spaces.
12071218
*/
1208
-static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1219
+static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12091220
const char *zSql; /* The text of the SQL statement */
12101221
const char *z; /* Used to check if this is an EXPLAIN */
12111222
int *abYield = 0; /* True if op is an OP_Yield */
12121223
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12131224
int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
12631274
}
12641275
12651276
/*
12661277
** Free the array allocated by explain_data_prepare().
12671278
*/
1268
-static void explain_data_delete(struct callback_data *p){
1279
+static void explain_data_delete(ShellState *p){
12691280
sqlite3_free(p->aiIndent);
12701281
p->aiIndent = 0;
12711282
p->nIndent = 0;
12721283
p->iIndent = 0;
12731284
}
@@ -1280,16 +1291,16 @@
12801291
** This is very similar to SQLite's built-in sqlite3_exec()
12811292
** function except it takes a slightly different callback
12821293
** and callback data argument.
12831294
*/
12841295
static int shell_exec(
1285
- sqlite3 *db, /* An open database */
1286
- const char *zSql, /* SQL to be evaluated */
1296
+ sqlite3 *db, /* An open database */
1297
+ const char *zSql, /* SQL to be evaluated */
12871298
int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1288
- /* (not the same as sqlite3_exec) */
1289
- struct callback_data *pArg, /* Pointer to struct callback_data */
1290
- char **pzErrMsg /* Error msg written here */
1299
+ /* (not the same as sqlite3_exec) */
1300
+ ShellState *pArg, /* Pointer to ShellState */
1301
+ char **pzErrMsg /* Error msg written here */
12911302
){
12921303
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
12931304
int rc = SQLITE_OK; /* Return Code */
12941305
int rc2;
12951306
const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
14531464
int rc;
14541465
const char *zTable;
14551466
const char *zType;
14561467
const char *zSql;
14571468
const char *zPrepStmt = 0;
1458
- struct callback_data *p = (struct callback_data *)pArg;
1469
+ ShellState *p = (ShellState *)pArg;
14591470
14601471
UNUSED_PARAMETER(azCol);
14611472
if( nArg!=3 ) return 1;
14621473
zTable = azArg[0];
14631474
zType = azArg[1];
@@ -1549,11 +1560,11 @@
15491560
**
15501561
** If we get a SQLITE_CORRUPT error, rerun the query after appending
15511562
** "ORDER BY rowid DESC" to the end.
15521563
*/
15531564
static int run_schema_dump_query(
1554
- struct callback_data *p,
1565
+ ShellState *p,
15551566
const char *zQuery
15561567
){
15571568
int rc;
15581569
char *zErr = 0;
15591570
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
16491660
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
16501661
" Negative values right-justify\n"
16511662
;
16521663
16531664
/* Forward reference */
1654
-static int process_input(struct callback_data *p, FILE *in);
1665
+static int process_input(ShellState *p, FILE *in);
16551666
/*
16561667
** Implementation of the "readfile(X)" SQL function. The entire content
16571668
** of the file named X is read and returned as a BLOB. NULL is returned
16581669
** if the file does not exist or is unreadable.
16591670
*/
@@ -1694,35 +1705,32 @@
16941705
int argc,
16951706
sqlite3_value **argv
16961707
){
16971708
FILE *out;
16981709
const char *z;
1699
- int n;
17001710
sqlite3_int64 rc;
17011711
const char *zFile;
17021712
17031713
zFile = (const char*)sqlite3_value_text(argv[0]);
17041714
if( zFile==0 ) return;
17051715
out = fopen(zFile, "wb");
17061716
if( out==0 ) return;
17071717
z = (const char*)sqlite3_value_blob(argv[1]);
17081718
if( z==0 ){
1709
- n = 0;
17101719
rc = 0;
17111720
}else{
1712
- n = sqlite3_value_bytes(argv[1]);
1713
- rc = fwrite(z, 1, n, out);
1721
+ rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
17141722
}
17151723
fclose(out);
17161724
sqlite3_result_int64(context, rc);
17171725
}
17181726
17191727
/*
17201728
** Make sure the database is open. If it is not, then open it. If
17211729
** the database fails to open, print an error message and exit.
17221730
*/
1723
-static void open_db(struct callback_data *p, int keepAlive){
1731
+static void open_db(ShellState *p, int keepAlive){
17241732
if( p->db==0 ){
17251733
sqlite3_initialize();
17261734
sqlite3_open(p->zDbFilename, &p->db);
17271735
db = p->db;
17281736
if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
18991907
/*
19001908
** A routine for handling output from sqlite3_trace().
19011909
*/
19021910
static void sql_trace_callback(void *pArg, const char *z){
19031911
FILE *f = (FILE*)pArg;
1904
- if( f ) fprintf(f, "%s\n", z);
1912
+ if( f ){
1913
+ int i = (int)strlen(z);
1914
+ while( i>0 && z[i-1]==';' ){ i--; }
1915
+ fprintf(f, "%.*s;\n", i, z);
1916
+ }
19051917
}
19061918
19071919
/*
19081920
** A no-op routine that runs with the ".breakpoint" doc-command. This is
19091921
** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
20172029
** Try to transfer data for table zTable. If an error is seen while
20182030
** moving forward, try to go backwards. The backwards movement won't
20192031
** work for WITHOUT ROWID tables.
20202032
*/
20212033
static void tryToCloneData(
2022
- struct callback_data *p,
2034
+ ShellState *p,
20232035
sqlite3 *newDb,
20242036
const char *zTable
20252037
){
20262038
sqlite3_stmt *pQuery = 0;
20272039
sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
21302142
** each row, invoke xForEach() on the object defined by that row.
21312143
** If an error is encountered while moving forward through the
21322144
** sqlite_master table, try again moving backwards.
21332145
*/
21342146
static void tryToCloneSchema(
2135
- struct callback_data *p,
2147
+ ShellState *p,
21362148
sqlite3 *newDb,
21372149
const char *zWhere,
2138
- void (*xForEach)(struct callback_data*,sqlite3*,const char*)
2150
+ void (*xForEach)(ShellState*,sqlite3*,const char*)
21392151
){
21402152
sqlite3_stmt *pQuery = 0;
21412153
char *zQuery = 0;
21422154
int rc;
21432155
const unsigned char *zName;
@@ -2204,11 +2216,11 @@
22042216
/*
22052217
** Open a new database file named "zNewDb". Try to recover as much information
22062218
** as possible out of the main database (which might be corrupt) and write it
22072219
** into zNewDb.
22082220
*/
2209
-static void tryToClone(struct callback_data *p, const char *zNewDb){
2221
+static void tryToClone(ShellState *p, const char *zNewDb){
22102222
int rc;
22112223
sqlite3 *newDb = 0;
22122224
if( access(zNewDb,0)==0 ){
22132225
fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
22142226
return;
@@ -2229,11 +2241,11 @@
22292241
}
22302242
22312243
/*
22322244
** Change the output file back to stdout
22332245
*/
2234
-static void output_reset(struct callback_data *p){
2246
+static void output_reset(ShellState *p){
22352247
if( p->outfile[0]=='|' ){
22362248
pclose(p->out);
22372249
}else{
22382250
output_file_close(p->out);
22392251
}
@@ -2245,11 +2257,11 @@
22452257
** If an input line begins with "." then invoke this routine to
22462258
** process that line.
22472259
**
22482260
** Return 1 on error, 2 to exit, and 0 otherwise.
22492261
*/
2250
-static int do_meta_command(char *zLine, struct callback_data *p){
2262
+static int do_meta_command(char *zLine, ShellState *p){
22512263
int i = 1;
22522264
int nArg = 0;
22532265
int n, c;
22542266
int rc = 0;
22552267
char *azArg[50];
@@ -2363,11 +2375,11 @@
23632375
rc = 1;
23642376
}
23652377
}else
23662378
23672379
if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2368
- struct callback_data data;
2380
+ ShellState data;
23692381
char *zErrMsg = 0;
23702382
open_db(p, 0);
23712383
memcpy(&data, p, sizeof(data));
23722384
data.showHeader = 1;
23732385
data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
24612473
}else
24622474
24632475
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
24642476
int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
24652477
if(val == 1) {
2466
- if(!p->explainPrev.valid) {
2467
- p->explainPrev.valid = 1;
2468
- p->explainPrev.mode = p->mode;
2469
- p->explainPrev.showHeader = p->showHeader;
2470
- memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2478
+ if(!p->normalMode.valid) {
2479
+ p->normalMode.valid = 1;
2480
+ p->normalMode.mode = p->mode;
2481
+ p->normalMode.showHeader = p->showHeader;
2482
+ memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
24712483
}
24722484
/* We could put this code under the !p->explainValid
24732485
** condition so that it does not execute if we are already in
24742486
** explain mode. However, always executing it allows us an easy
24752487
** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
24852497
p->colWidth[3] = 4; /* P2 */
24862498
p->colWidth[4] = 4; /* P3 */
24872499
p->colWidth[5] = 13; /* P4 */
24882500
p->colWidth[6] = 2; /* P5 */
24892501
p->colWidth[7] = 13; /* Comment */
2490
- }else if (p->explainPrev.valid) {
2491
- p->explainPrev.valid = 0;
2492
- p->mode = p->explainPrev.mode;
2493
- p->showHeader = p->explainPrev.showHeader;
2494
- memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2502
+ }else if (p->normalMode.valid) {
2503
+ p->normalMode.valid = 0;
2504
+ p->mode = p->normalMode.mode;
2505
+ p->showHeader = p->normalMode.showHeader;
2506
+ memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
24952507
}
24962508
}else
24972509
24982510
if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2499
- struct callback_data data;
2511
+ ShellState data;
25002512
char *zErrMsg = 0;
25012513
int doStats = 0;
25022514
if( nArg!=1 ){
25032515
fprintf(stderr, "Usage: .fullschema\n");
25042516
rc = 1;
@@ -2511,11 +2523,11 @@
25112523
rc = sqlite3_exec(p->db,
25122524
"SELECT sql FROM"
25132525
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
25142526
" FROM sqlite_master UNION ALL"
25152527
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2516
- "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2528
+ "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
25172529
"ORDER BY rowid",
25182530
callback, &data, &zErrMsg
25192531
);
25202532
if( rc==SQLITE_OK ){
25212533
sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
27152727
sqlite3_finalize(pStmt);
27162728
if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
27172729
}else
27182730
27192731
if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2720
- struct callback_data data;
2732
+ ShellState data;
27212733
char *zErrMsg = 0;
27222734
open_db(p, 0);
27232735
memcpy(&data, p, sizeof(data));
27242736
data.showHeader = 0;
27252737
data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
30093021
}
30103022
sqlite3_close(pSrc);
30113023
}else
30123024
30133025
if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3014
- struct callback_data data;
3026
+ ShellState data;
30153027
char *zErrMsg = 0;
30163028
open_db(p, 0);
30173029
memcpy(&data, p, sizeof(data));
30183030
data.showHeader = 0;
30193031
data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
30653077
rc = sqlite3_exec(p->db,
30663078
"SELECT sql FROM "
30673079
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
30683080
" FROM sqlite_master UNION ALL"
30693081
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3070
- "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
3082
+ "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
30713083
"ORDER BY rowid",
30723084
callback, &data, &zErrMsg
30733085
);
30743086
}else{
30753087
fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
31263138
31273139
if( c=='s'
31283140
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
31293141
){
31303142
char *zCmd;
3131
- int i;
3143
+ int i, x;
31323144
if( nArg<2 ){
31333145
fprintf(stderr, "Usage: .system COMMAND\n");
31343146
rc = 1;
31353147
goto meta_command_exit;
31363148
}
@@ -3137,12 +3149,13 @@
31373149
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
31383150
for(i=2; i<nArg; i++){
31393151
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
31403152
zCmd, azArg[i]);
31413153
}
3142
- (void)system(zCmd);
3154
+ x = system(zCmd);
31433155
sqlite3_free(zCmd);
3156
+ if( x ) fprintf(stderr, "System command returns %d\n", x);
31443157
}else
31453158
31463159
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
31473160
int i;
31483161
if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
31503163
rc = 1;
31513164
goto meta_command_exit;
31523165
}
31533166
fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
31543167
fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3155
- fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
3168
+ fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
31563169
fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
31573170
fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
31583171
fprintf(p->out,"%9.9s: ", "nullvalue");
31593172
output_c_string(p->out, p->nullvalue);
31603173
fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
35423555
** is saved only if input is interactive. An interrupt signal will
35433556
** cause this routine to exit immediately, unless input is interactive.
35443557
**
35453558
** Return the number of errors.
35463559
*/
3547
-static int process_input(struct callback_data *p, FILE *in){
3560
+static int process_input(ShellState *p, FILE *in){
35483561
char *zLine = 0; /* A single input line */
35493562
char *zSql = 0; /* Accumulated SQL text */
35503563
int nLine; /* Length of current line */
35513564
int nSql = 0; /* Bytes of zSql[] used */
35523565
int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
37213734
** parameter is NULL, take input from ~/.sqliterc
37223735
**
37233736
** Returns the number of errors.
37243737
*/
37253738
static int process_sqliterc(
3726
- struct callback_data *p, /* Configuration data */
3739
+ ShellState *p, /* Configuration data */
37273740
const char *sqliterc_override /* Name of config file. NULL to use default */
37283741
){
37293742
char *home_dir = NULL;
37303743
const char *sqliterc = sqliterc_override;
37313744
char *zBuf = 0;
@@ -3774,16 +3787,19 @@
37743787
" -help show this message\n"
37753788
" -html set output mode to HTML\n"
37763789
" -interactive force interactive I/O\n"
37773790
" -line set output mode to 'line'\n"
37783791
" -list set output mode to 'list'\n"
3792
+ " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
37793793
" -mmap N default mmap size set to N\n"
37803794
#ifdef SQLITE_ENABLE_MULTIPLEX
37813795
" -multiplex enable the multiplexor VFS\n"
37823796
#endif
37833797
" -newline SEP set newline character(s) for CSV\n"
37843798
" -nullvalue TEXT set text string for NULL values. Default ''\n"
3799
+ " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
3800
+ " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
37853801
" -separator SEP set output field separator. Default: '|'\n"
37863802
" -stats print memory stats before each finalize\n"
37873803
" -version show SQLite version\n"
37883804
" -vfs NAME use NAME as the default VFS\n"
37893805
#ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
38043820
}
38053821
38063822
/*
38073823
** Initialize the state information in data
38083824
*/
3809
-static void main_init(struct callback_data *data) {
3825
+static void main_init(ShellState *data) {
38103826
memset(data, 0, sizeof(*data));
38113827
data->mode = MODE_List;
38123828
memcpy(data->separator,"|", 2);
38133829
memcpy(data->newline,"\r\n", 3);
38143830
data->showHeader = 0;
3831
+ data->shellFlgs = SHFLG_Lookaside;
38153832
sqlite3_config(SQLITE_CONFIG_URI, 1);
38163833
sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
3834
+ sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
38173835
sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
38183836
sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
3819
- sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
38203837
}
38213838
38223839
/*
38233840
** Output text to the console in a font that attracts extra attention.
38243841
*/
@@ -3852,11 +3869,11 @@
38523869
return argv[i];
38533870
}
38543871
38553872
int main(int argc, char **argv){
38563873
char *zErrMsg = 0;
3857
- struct callback_data data;
3874
+ ShellState data;
38583875
const char *zInitFile = 0;
38593876
char *zFirstCmd = 0;
38603877
int i;
38613878
int rc = 0;
38623879
int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
39233940
zSize = cmdline_option_value(argc, argv, ++i);
39243941
szHeap = integerValue(zSize);
39253942
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
39263943
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
39273944
#endif
3945
+ }else if( strcmp(z,"-scratch")==0 ){
3946
+ int n, sz;
3947
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3948
+ if( sz>400000 ) sz = 400000;
3949
+ if( sz<2500 ) sz = 2500;
3950
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3951
+ if( n>10 ) n = 10;
3952
+ if( n<1 ) n = 1;
3953
+ sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
3954
+ data.shellFlgs |= SHFLG_Scratch;
3955
+ }else if( strcmp(z,"-pagecache")==0 ){
3956
+ int n, sz;
3957
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3958
+ if( sz>70000 ) sz = 70000;
3959
+ if( sz<800 ) sz = 800;
3960
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3961
+ if( n<10 ) n = 10;
3962
+ sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
3963
+ data.shellFlgs |= SHFLG_Pagecache;
3964
+ }else if( strcmp(z,"-lookaside")==0 ){
3965
+ int n, sz;
3966
+ sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3967
+ if( sz<0 ) sz = 0;
3968
+ n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3969
+ if( n<0 ) n = 0;
3970
+ sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
3971
+ if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
39283972
#ifdef SQLITE_ENABLE_VFSTRACE
39293973
}else if( strcmp(z,"-vfstrace")==0 ){
39303974
extern int vfstrace_register(
39313975
const char *zTraceName,
39323976
const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
40384082
stdin_is_interactive = 1;
40394083
}else if( strcmp(z,"-batch")==0 ){
40404084
stdin_is_interactive = 0;
40414085
}else if( strcmp(z,"-heap")==0 ){
40424086
i++;
4087
+ }else if( strcmp(z,"-scratch")==0 ){
4088
+ i+=2;
4089
+ }else if( strcmp(z,"-pagecache")==0 ){
4090
+ i+=2;
4091
+ }else if( strcmp(z,"-lookaside")==0 ){
4092
+ i+=2;
40434093
}else if( strcmp(z,"-mmap")==0 ){
40444094
i++;
40454095
}else if( strcmp(z,"-vfs")==0 ){
40464096
i++;
40474097
#ifdef SQLITE_ENABLE_VFSTRACE
40484098
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430 #endif
431 }
432 return zResult;
433 }
434
435 struct previous_mode_data {
436 int valid; /* Is there legit data in here? */
437 int mode;
438 int showHeader;
439 int colWidth[100];
 
 
 
 
 
440 };
441
442 /*
443 ** An pointer to an instance of this structure is passed from
444 ** the main program to the callback. This is used to communicate
445 ** state and mode information.
446 */
447 struct callback_data {
 
448 sqlite3 *db; /* The database */
449 int echoOn; /* True to echo input commands */
450 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
451 int statsOn; /* True to display memory stats before each finalize */
452 int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
455 FILE *traceOut; /* Output for sqlite3_trace() */
456 int nErr; /* Number of errors seen */
457 int mode; /* An output mode setting */
458 int writableSchema; /* True if PRAGMA writable_schema=ON */
459 int showHeader; /* True to show column names in List or Column mode */
 
460 char *zDestTable; /* Name of destination table when MODE_Insert */
461 char separator[20]; /* Separator character for MODE_List */
462 char newline[20]; /* Record separator in MODE_Csv */
463 int colWidth[100]; /* Requested width of each column when in column mode*/
464 int actualWidth[100]; /* Actual width of each column */
465 char nullvalue[20]; /* The text to print when a NULL comes back from
466 ** the database */
467 struct previous_mode_data explainPrev;
468 /* Holds the mode information just before
469 ** .explain ON */
470 char outfile[FILENAME_MAX]; /* Filename for *out */
471 const char *zDbFilename; /* name of the database file */
472 char *zFreeOnClose; /* Filename to free when closing */
473 const char *zVfs; /* Name of VFS to use */
474 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
476 int *aiIndent; /* Array of indents used in MODE_Explain */
477 int nIndent; /* Size of array aiIndent[] */
478 int iIndent; /* Index of current op in aiIndent[] */
479 };
480
 
 
 
 
 
 
 
481 /*
482 ** These are the allowed modes.
483 */
484 #define MODE_Line 0 /* One column per line. Blank line between records */
485 #define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
520
521 /*
522 ** A callback for the sqlite3_log() interface.
523 */
524 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
525 struct callback_data *p = (struct callback_data*)pArg;
526 if( p->pLog==0 ) return;
527 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
528 fflush(p->pLog);
529 }
530
@@ -662,11 +673,11 @@
662 ** Output a single term of CSV. Actually, p->separator is used for
663 ** the separator, which may or may not be a comma. p->nullvalue is
664 ** the null value. Strings are quoted if necessary. The separator
665 ** is only issued if bSep is true.
666 */
667 static void output_csv(struct callback_data *p, const char *z, int bSep){
668 FILE *out = p->out;
669 if( z==0 ){
670 fprintf(out,"%s",p->nullvalue);
671 }else{
672 int i;
@@ -711,11 +722,11 @@
711 ** This is the callback routine that the shell
712 ** invokes for each row of a query result.
713 */
714 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
715 int i;
716 struct callback_data *p = (struct callback_data*)pArg;
717
718 switch( p->mode ){
719 case MODE_Line: {
720 int w = 5;
721 if( azArg==0 ) break;
@@ -921,15 +932,15 @@
921 /* since we don't have type info, call the shell_callback with a NULL value */
922 return shell_callback(pArg, nArg, azArg, azCol, NULL);
923 }
924
925 /*
926 ** Set the destination table field of the callback_data structure to
927 ** the name of the table given. Escape any quote characters in the
928 ** table name.
929 */
930 static void set_table_name(struct callback_data *p, const char *zName){
931 int i, n;
932 int needQuote;
933 char *z;
934
935 if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
1015 ** then write the semicolon on a separate line. That way, if a
1016 ** "--" comment occurs at the end of the statement, the comment
1017 ** won't consume the semicolon terminator.
1018 */
1019 static int run_table_dump_query(
1020 struct callback_data *p, /* Query context */
1021 const char *zSelect, /* SELECT statement to extract content */
1022 const char *zFirstRow /* Print before first row, if not NULL */
1023 ){
1024 sqlite3_stmt *pSelect;
1025 int rc;
@@ -1078,11 +1089,11 @@
1078 /*
1079 ** Display memory stats.
1080 */
1081 static int display_stats(
1082 sqlite3 *db, /* Database to query */
1083 struct callback_data *pArg, /* Pointer to struct callback_data */
1084 int bReset /* True to reset the stats */
1085 ){
1086 int iCur;
1087 int iHiwtr;
1088
@@ -1092,25 +1103,23 @@
1092 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1093 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1094 iHiwtr = iCur = -1;
1095 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1096 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1097 /*
1098 ** Not currently used by the CLI.
1099 ** iHiwtr = iCur = -1;
1100 ** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1101 ** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1102 */
1103 iHiwtr = iCur = -1;
1104 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1105 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1106 /*
1107 ** Not currently used by the CLI.
1108 ** iHiwtr = iCur = -1;
1109 ** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1110 ** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1111 */
1112 iHiwtr = iCur = -1;
1113 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1114 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1115 iHiwtr = iCur = -1;
1116 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
1127 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1128 #endif
1129 }
1130
1131 if( pArg && pArg->out && db ){
1132 iHiwtr = iCur = -1;
1133 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1134 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1135 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1136 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1137 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1138 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1139 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1140 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
 
 
1141 iHiwtr = iCur = -1;
1142 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1143 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1144 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1145 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
1187 return 0;
1188 }
1189
1190 /*
1191 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1192 ** and populate the callback_data.aiIndent[] array with the number of
1193 ** spaces each opcode should be indented before it is output.
1194 **
1195 ** The indenting rules are:
1196 **
1197 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
1203 ** Yield SeekGt SeekLt RowSetRead Rewind
1204 ** or if the P1 parameter is one instead of zero,
1205 ** then indent all opcodes between the earlier instruction
1206 ** and "Goto" by 2 spaces.
1207 */
1208 static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1209 const char *zSql; /* The text of the SQL statement */
1210 const char *z; /* Used to check if this is an EXPLAIN */
1211 int *abYield = 0; /* True if op is an OP_Yield */
1212 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1213 int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
1263 }
1264
1265 /*
1266 ** Free the array allocated by explain_data_prepare().
1267 */
1268 static void explain_data_delete(struct callback_data *p){
1269 sqlite3_free(p->aiIndent);
1270 p->aiIndent = 0;
1271 p->nIndent = 0;
1272 p->iIndent = 0;
1273 }
@@ -1280,16 +1291,16 @@
1280 ** This is very similar to SQLite's built-in sqlite3_exec()
1281 ** function except it takes a slightly different callback
1282 ** and callback data argument.
1283 */
1284 static int shell_exec(
1285 sqlite3 *db, /* An open database */
1286 const char *zSql, /* SQL to be evaluated */
1287 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1288 /* (not the same as sqlite3_exec) */
1289 struct callback_data *pArg, /* Pointer to struct callback_data */
1290 char **pzErrMsg /* Error msg written here */
1291 ){
1292 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1293 int rc = SQLITE_OK; /* Return Code */
1294 int rc2;
1295 const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
1453 int rc;
1454 const char *zTable;
1455 const char *zType;
1456 const char *zSql;
1457 const char *zPrepStmt = 0;
1458 struct callback_data *p = (struct callback_data *)pArg;
1459
1460 UNUSED_PARAMETER(azCol);
1461 if( nArg!=3 ) return 1;
1462 zTable = azArg[0];
1463 zType = azArg[1];
@@ -1549,11 +1560,11 @@
1549 **
1550 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1551 ** "ORDER BY rowid DESC" to the end.
1552 */
1553 static int run_schema_dump_query(
1554 struct callback_data *p,
1555 const char *zQuery
1556 ){
1557 int rc;
1558 char *zErr = 0;
1559 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
1649 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1650 " Negative values right-justify\n"
1651 ;
1652
1653 /* Forward reference */
1654 static int process_input(struct callback_data *p, FILE *in);
1655 /*
1656 ** Implementation of the "readfile(X)" SQL function. The entire content
1657 ** of the file named X is read and returned as a BLOB. NULL is returned
1658 ** if the file does not exist or is unreadable.
1659 */
@@ -1694,35 +1705,32 @@
1694 int argc,
1695 sqlite3_value **argv
1696 ){
1697 FILE *out;
1698 const char *z;
1699 int n;
1700 sqlite3_int64 rc;
1701 const char *zFile;
1702
1703 zFile = (const char*)sqlite3_value_text(argv[0]);
1704 if( zFile==0 ) return;
1705 out = fopen(zFile, "wb");
1706 if( out==0 ) return;
1707 z = (const char*)sqlite3_value_blob(argv[1]);
1708 if( z==0 ){
1709 n = 0;
1710 rc = 0;
1711 }else{
1712 n = sqlite3_value_bytes(argv[1]);
1713 rc = fwrite(z, 1, n, out);
1714 }
1715 fclose(out);
1716 sqlite3_result_int64(context, rc);
1717 }
1718
1719 /*
1720 ** Make sure the database is open. If it is not, then open it. If
1721 ** the database fails to open, print an error message and exit.
1722 */
1723 static void open_db(struct callback_data *p, int keepAlive){
1724 if( p->db==0 ){
1725 sqlite3_initialize();
1726 sqlite3_open(p->zDbFilename, &p->db);
1727 db = p->db;
1728 if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
1899 /*
1900 ** A routine for handling output from sqlite3_trace().
1901 */
1902 static void sql_trace_callback(void *pArg, const char *z){
1903 FILE *f = (FILE*)pArg;
1904 if( f ) fprintf(f, "%s\n", z);
 
 
 
 
1905 }
1906
1907 /*
1908 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1909 ** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
2017 ** Try to transfer data for table zTable. If an error is seen while
2018 ** moving forward, try to go backwards. The backwards movement won't
2019 ** work for WITHOUT ROWID tables.
2020 */
2021 static void tryToCloneData(
2022 struct callback_data *p,
2023 sqlite3 *newDb,
2024 const char *zTable
2025 ){
2026 sqlite3_stmt *pQuery = 0;
2027 sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
2130 ** each row, invoke xForEach() on the object defined by that row.
2131 ** If an error is encountered while moving forward through the
2132 ** sqlite_master table, try again moving backwards.
2133 */
2134 static void tryToCloneSchema(
2135 struct callback_data *p,
2136 sqlite3 *newDb,
2137 const char *zWhere,
2138 void (*xForEach)(struct callback_data*,sqlite3*,const char*)
2139 ){
2140 sqlite3_stmt *pQuery = 0;
2141 char *zQuery = 0;
2142 int rc;
2143 const unsigned char *zName;
@@ -2204,11 +2216,11 @@
2204 /*
2205 ** Open a new database file named "zNewDb". Try to recover as much information
2206 ** as possible out of the main database (which might be corrupt) and write it
2207 ** into zNewDb.
2208 */
2209 static void tryToClone(struct callback_data *p, const char *zNewDb){
2210 int rc;
2211 sqlite3 *newDb = 0;
2212 if( access(zNewDb,0)==0 ){
2213 fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
2214 return;
@@ -2229,11 +2241,11 @@
2229 }
2230
2231 /*
2232 ** Change the output file back to stdout
2233 */
2234 static void output_reset(struct callback_data *p){
2235 if( p->outfile[0]=='|' ){
2236 pclose(p->out);
2237 }else{
2238 output_file_close(p->out);
2239 }
@@ -2245,11 +2257,11 @@
2245 ** If an input line begins with "." then invoke this routine to
2246 ** process that line.
2247 **
2248 ** Return 1 on error, 2 to exit, and 0 otherwise.
2249 */
2250 static int do_meta_command(char *zLine, struct callback_data *p){
2251 int i = 1;
2252 int nArg = 0;
2253 int n, c;
2254 int rc = 0;
2255 char *azArg[50];
@@ -2363,11 +2375,11 @@
2363 rc = 1;
2364 }
2365 }else
2366
2367 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2368 struct callback_data data;
2369 char *zErrMsg = 0;
2370 open_db(p, 0);
2371 memcpy(&data, p, sizeof(data));
2372 data.showHeader = 1;
2373 data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
2461 }else
2462
2463 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2464 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2465 if(val == 1) {
2466 if(!p->explainPrev.valid) {
2467 p->explainPrev.valid = 1;
2468 p->explainPrev.mode = p->mode;
2469 p->explainPrev.showHeader = p->showHeader;
2470 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2471 }
2472 /* We could put this code under the !p->explainValid
2473 ** condition so that it does not execute if we are already in
2474 ** explain mode. However, always executing it allows us an easy
2475 ** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
2485 p->colWidth[3] = 4; /* P2 */
2486 p->colWidth[4] = 4; /* P3 */
2487 p->colWidth[5] = 13; /* P4 */
2488 p->colWidth[6] = 2; /* P5 */
2489 p->colWidth[7] = 13; /* Comment */
2490 }else if (p->explainPrev.valid) {
2491 p->explainPrev.valid = 0;
2492 p->mode = p->explainPrev.mode;
2493 p->showHeader = p->explainPrev.showHeader;
2494 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2495 }
2496 }else
2497
2498 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2499 struct callback_data data;
2500 char *zErrMsg = 0;
2501 int doStats = 0;
2502 if( nArg!=1 ){
2503 fprintf(stderr, "Usage: .fullschema\n");
2504 rc = 1;
@@ -2511,11 +2523,11 @@
2511 rc = sqlite3_exec(p->db,
2512 "SELECT sql FROM"
2513 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2514 " FROM sqlite_master UNION ALL"
2515 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2516 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2517 "ORDER BY rowid",
2518 callback, &data, &zErrMsg
2519 );
2520 if( rc==SQLITE_OK ){
2521 sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
2715 sqlite3_finalize(pStmt);
2716 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2717 }else
2718
2719 if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2720 struct callback_data data;
2721 char *zErrMsg = 0;
2722 open_db(p, 0);
2723 memcpy(&data, p, sizeof(data));
2724 data.showHeader = 0;
2725 data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
3009 }
3010 sqlite3_close(pSrc);
3011 }else
3012
3013 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3014 struct callback_data data;
3015 char *zErrMsg = 0;
3016 open_db(p, 0);
3017 memcpy(&data, p, sizeof(data));
3018 data.showHeader = 0;
3019 data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
3065 rc = sqlite3_exec(p->db,
3066 "SELECT sql FROM "
3067 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3068 " FROM sqlite_master UNION ALL"
3069 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3070 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
3071 "ORDER BY rowid",
3072 callback, &data, &zErrMsg
3073 );
3074 }else{
3075 fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
3126
3127 if( c=='s'
3128 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
3129 ){
3130 char *zCmd;
3131 int i;
3132 if( nArg<2 ){
3133 fprintf(stderr, "Usage: .system COMMAND\n");
3134 rc = 1;
3135 goto meta_command_exit;
3136 }
@@ -3137,12 +3149,13 @@
3137 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3138 for(i=2; i<nArg; i++){
3139 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3140 zCmd, azArg[i]);
3141 }
3142 (void)system(zCmd);
3143 sqlite3_free(zCmd);
 
3144 }else
3145
3146 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
3147 int i;
3148 if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
3150 rc = 1;
3151 goto meta_command_exit;
3152 }
3153 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
3154 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3155 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
3156 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
3157 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
3158 fprintf(p->out,"%9.9s: ", "nullvalue");
3159 output_c_string(p->out, p->nullvalue);
3160 fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
3542 ** is saved only if input is interactive. An interrupt signal will
3543 ** cause this routine to exit immediately, unless input is interactive.
3544 **
3545 ** Return the number of errors.
3546 */
3547 static int process_input(struct callback_data *p, FILE *in){
3548 char *zLine = 0; /* A single input line */
3549 char *zSql = 0; /* Accumulated SQL text */
3550 int nLine; /* Length of current line */
3551 int nSql = 0; /* Bytes of zSql[] used */
3552 int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
3721 ** parameter is NULL, take input from ~/.sqliterc
3722 **
3723 ** Returns the number of errors.
3724 */
3725 static int process_sqliterc(
3726 struct callback_data *p, /* Configuration data */
3727 const char *sqliterc_override /* Name of config file. NULL to use default */
3728 ){
3729 char *home_dir = NULL;
3730 const char *sqliterc = sqliterc_override;
3731 char *zBuf = 0;
@@ -3774,16 +3787,19 @@
3774 " -help show this message\n"
3775 " -html set output mode to HTML\n"
3776 " -interactive force interactive I/O\n"
3777 " -line set output mode to 'line'\n"
3778 " -list set output mode to 'list'\n"
 
3779 " -mmap N default mmap size set to N\n"
3780 #ifdef SQLITE_ENABLE_MULTIPLEX
3781 " -multiplex enable the multiplexor VFS\n"
3782 #endif
3783 " -newline SEP set newline character(s) for CSV\n"
3784 " -nullvalue TEXT set text string for NULL values. Default ''\n"
 
 
3785 " -separator SEP set output field separator. Default: '|'\n"
3786 " -stats print memory stats before each finalize\n"
3787 " -version show SQLite version\n"
3788 " -vfs NAME use NAME as the default VFS\n"
3789 #ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
3804 }
3805
3806 /*
3807 ** Initialize the state information in data
3808 */
3809 static void main_init(struct callback_data *data) {
3810 memset(data, 0, sizeof(*data));
3811 data->mode = MODE_List;
3812 memcpy(data->separator,"|", 2);
3813 memcpy(data->newline,"\r\n", 3);
3814 data->showHeader = 0;
 
3815 sqlite3_config(SQLITE_CONFIG_URI, 1);
3816 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
 
3817 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3818 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
3819 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
3820 }
3821
3822 /*
3823 ** Output text to the console in a font that attracts extra attention.
3824 */
@@ -3852,11 +3869,11 @@
3852 return argv[i];
3853 }
3854
3855 int main(int argc, char **argv){
3856 char *zErrMsg = 0;
3857 struct callback_data data;
3858 const char *zInitFile = 0;
3859 char *zFirstCmd = 0;
3860 int i;
3861 int rc = 0;
3862 int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
3923 zSize = cmdline_option_value(argc, argv, ++i);
3924 szHeap = integerValue(zSize);
3925 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3926 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3927 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3928 #ifdef SQLITE_ENABLE_VFSTRACE
3929 }else if( strcmp(z,"-vfstrace")==0 ){
3930 extern int vfstrace_register(
3931 const char *zTraceName,
3932 const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
4038 stdin_is_interactive = 1;
4039 }else if( strcmp(z,"-batch")==0 ){
4040 stdin_is_interactive = 0;
4041 }else if( strcmp(z,"-heap")==0 ){
4042 i++;
 
 
 
 
 
 
4043 }else if( strcmp(z,"-mmap")==0 ){
4044 i++;
4045 }else if( strcmp(z,"-vfs")==0 ){
4046 i++;
4047 #ifdef SQLITE_ENABLE_VFSTRACE
4048
--- src/shell.c
+++ src/shell.c
@@ -430,23 +430,28 @@
430 #endif
431 }
432 return zResult;
433 }
434
435 /*
436 ** Shell output mode information from before ".explain on",
437 ** saved so that it can be restored by ".explain off"
438 */
439 typedef struct SavedModeInfo SavedModeInfo;
440 struct SavedModeInfo {
441 int valid; /* Is there legit data in here? */
442 int mode; /* Mode prior to ".explain on" */
443 int showHeader; /* The ".header" setting prior to ".explain on" */
444 int colWidth[100]; /* Column widths prior to ".explain on" */
445 };
446
447 /*
448 ** State information about the database connection is contained in an
449 ** instance of the following structure.
 
450 */
451 typedef struct ShellState ShellState;
452 struct ShellState {
453 sqlite3 *db; /* The database */
454 int echoOn; /* True to echo input commands */
455 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
456 int statsOn; /* True to display memory stats before each finalize */
457 int outCount; /* Revert to stdout when reaching zero */
@@ -455,20 +460,19 @@
460 FILE *traceOut; /* Output for sqlite3_trace() */
461 int nErr; /* Number of errors seen */
462 int mode; /* An output mode setting */
463 int writableSchema; /* True if PRAGMA writable_schema=ON */
464 int showHeader; /* True to show column names in List or Column mode */
465 unsigned shellFlgs; /* Various flags */
466 char *zDestTable; /* Name of destination table when MODE_Insert */
467 char separator[20]; /* Separator character for MODE_List */
468 char newline[20]; /* Record separator in MODE_Csv */
469 int colWidth[100]; /* Requested width of each column when in column mode*/
470 int actualWidth[100]; /* Actual width of each column */
471 char nullvalue[20]; /* The text to print when a NULL comes back from
472 ** the database */
473 SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
 
 
474 char outfile[FILENAME_MAX]; /* Filename for *out */
475 const char *zDbFilename; /* name of the database file */
476 char *zFreeOnClose; /* Filename to free when closing */
477 const char *zVfs; /* Name of VFS to use */
478 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -476,10 +480,17 @@
480 int *aiIndent; /* Array of indents used in MODE_Explain */
481 int nIndent; /* Size of array aiIndent[] */
482 int iIndent; /* Index of current op in aiIndent[] */
483 };
484
485 /*
486 ** These are the allowed shellFlgs values
487 */
488 #define SHFLG_Scratch 0x00001 /* The --scratch option is used */
489 #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
490 #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
491
492 /*
493 ** These are the allowed modes.
494 */
495 #define MODE_Line 0 /* One column per line. Blank line between records */
496 #define MODE_Column 1 /* One record per line in neat columns */
@@ -520,11 +531,11 @@
531
532 /*
533 ** A callback for the sqlite3_log() interface.
534 */
535 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
536 ShellState *p = (ShellState*)pArg;
537 if( p->pLog==0 ) return;
538 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
539 fflush(p->pLog);
540 }
541
@@ -662,11 +673,11 @@
673 ** Output a single term of CSV. Actually, p->separator is used for
674 ** the separator, which may or may not be a comma. p->nullvalue is
675 ** the null value. Strings are quoted if necessary. The separator
676 ** is only issued if bSep is true.
677 */
678 static void output_csv(ShellState *p, const char *z, int bSep){
679 FILE *out = p->out;
680 if( z==0 ){
681 fprintf(out,"%s",p->nullvalue);
682 }else{
683 int i;
@@ -711,11 +722,11 @@
722 ** This is the callback routine that the shell
723 ** invokes for each row of a query result.
724 */
725 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
726 int i;
727 ShellState *p = (ShellState*)pArg;
728
729 switch( p->mode ){
730 case MODE_Line: {
731 int w = 5;
732 if( azArg==0 ) break;
@@ -921,15 +932,15 @@
932 /* since we don't have type info, call the shell_callback with a NULL value */
933 return shell_callback(pArg, nArg, azArg, azCol, NULL);
934 }
935
936 /*
937 ** Set the destination table field of the ShellState structure to
938 ** the name of the table given. Escape any quote characters in the
939 ** table name.
940 */
941 static void set_table_name(ShellState *p, const char *zName){
942 int i, n;
943 int needQuote;
944 char *z;
945
946 if( p->zDestTable ){
@@ -1015,11 +1026,11 @@
1026 ** then write the semicolon on a separate line. That way, if a
1027 ** "--" comment occurs at the end of the statement, the comment
1028 ** won't consume the semicolon terminator.
1029 */
1030 static int run_table_dump_query(
1031 ShellState *p, /* Query context */
1032 const char *zSelect, /* SELECT statement to extract content */
1033 const char *zFirstRow /* Print before first row, if not NULL */
1034 ){
1035 sqlite3_stmt *pSelect;
1036 int rc;
@@ -1078,11 +1089,11 @@
1089 /*
1090 ** Display memory stats.
1091 */
1092 static int display_stats(
1093 sqlite3 *db, /* Database to query */
1094 ShellState *pArg, /* Pointer to ShellState */
1095 int bReset /* True to reset the stats */
1096 ){
1097 int iCur;
1098 int iHiwtr;
1099
@@ -1092,25 +1103,23 @@
1103 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1104 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1105 iHiwtr = iCur = -1;
1106 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1107 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1108 if( pArg->shellFlgs & SHFLG_Pagecache ){
1109 iHiwtr = iCur = -1;
1110 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1111 fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1112 }
 
1113 iHiwtr = iCur = -1;
1114 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1115 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1116 if( pArg->shellFlgs & SHFLG_Scratch ){
1117 iHiwtr = iCur = -1;
1118 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1119 fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1120 }
 
1121 iHiwtr = iCur = -1;
1122 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1123 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1124 iHiwtr = iCur = -1;
1125 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
@@ -1127,19 +1136,21 @@
1136 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1137 #endif
1138 }
1139
1140 if( pArg && pArg->out && db ){
1141 if( pArg->shellFlgs & SHFLG_Lookaside ){
1142 iHiwtr = iCur = -1;
1143 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1144 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1145 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1146 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1147 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1148 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1149 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1150 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1151 }
1152 iHiwtr = iCur = -1;
1153 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1154 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1155 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1156 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
@@ -1187,11 +1198,11 @@
1198 return 0;
1199 }
1200
1201 /*
1202 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1203 ** and populate the ShellState.aiIndent[] array with the number of
1204 ** spaces each opcode should be indented before it is output.
1205 **
1206 ** The indenting rules are:
1207 **
1208 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
@@ -1203,11 +1214,11 @@
1214 ** Yield SeekGt SeekLt RowSetRead Rewind
1215 ** or if the P1 parameter is one instead of zero,
1216 ** then indent all opcodes between the earlier instruction
1217 ** and "Goto" by 2 spaces.
1218 */
1219 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
1220 const char *zSql; /* The text of the SQL statement */
1221 const char *z; /* Used to check if this is an EXPLAIN */
1222 int *abYield = 0; /* True if op is an OP_Yield */
1223 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1224 int iOp; /* Index of operation in p->aiIndent[] */
@@ -1263,11 +1274,11 @@
1274 }
1275
1276 /*
1277 ** Free the array allocated by explain_data_prepare().
1278 */
1279 static void explain_data_delete(ShellState *p){
1280 sqlite3_free(p->aiIndent);
1281 p->aiIndent = 0;
1282 p->nIndent = 0;
1283 p->iIndent = 0;
1284 }
@@ -1280,16 +1291,16 @@
1291 ** This is very similar to SQLite's built-in sqlite3_exec()
1292 ** function except it takes a slightly different callback
1293 ** and callback data argument.
1294 */
1295 static int shell_exec(
1296 sqlite3 *db, /* An open database */
1297 const char *zSql, /* SQL to be evaluated */
1298 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1299 /* (not the same as sqlite3_exec) */
1300 ShellState *pArg, /* Pointer to ShellState */
1301 char **pzErrMsg /* Error msg written here */
1302 ){
1303 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1304 int rc = SQLITE_OK; /* Return Code */
1305 int rc2;
1306 const char *zLeftover; /* Tail of unprocessed SQL */
@@ -1453,11 +1464,11 @@
1464 int rc;
1465 const char *zTable;
1466 const char *zType;
1467 const char *zSql;
1468 const char *zPrepStmt = 0;
1469 ShellState *p = (ShellState *)pArg;
1470
1471 UNUSED_PARAMETER(azCol);
1472 if( nArg!=3 ) return 1;
1473 zTable = azArg[0];
1474 zType = azArg[1];
@@ -1549,11 +1560,11 @@
1560 **
1561 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1562 ** "ORDER BY rowid DESC" to the end.
1563 */
1564 static int run_schema_dump_query(
1565 ShellState *p,
1566 const char *zQuery
1567 ){
1568 int rc;
1569 char *zErr = 0;
1570 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
@@ -1649,11 +1660,11 @@
1660 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1661 " Negative values right-justify\n"
1662 ;
1663
1664 /* Forward reference */
1665 static int process_input(ShellState *p, FILE *in);
1666 /*
1667 ** Implementation of the "readfile(X)" SQL function. The entire content
1668 ** of the file named X is read and returned as a BLOB. NULL is returned
1669 ** if the file does not exist or is unreadable.
1670 */
@@ -1694,35 +1705,32 @@
1705 int argc,
1706 sqlite3_value **argv
1707 ){
1708 FILE *out;
1709 const char *z;
 
1710 sqlite3_int64 rc;
1711 const char *zFile;
1712
1713 zFile = (const char*)sqlite3_value_text(argv[0]);
1714 if( zFile==0 ) return;
1715 out = fopen(zFile, "wb");
1716 if( out==0 ) return;
1717 z = (const char*)sqlite3_value_blob(argv[1]);
1718 if( z==0 ){
 
1719 rc = 0;
1720 }else{
1721 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
 
1722 }
1723 fclose(out);
1724 sqlite3_result_int64(context, rc);
1725 }
1726
1727 /*
1728 ** Make sure the database is open. If it is not, then open it. If
1729 ** the database fails to open, print an error message and exit.
1730 */
1731 static void open_db(ShellState *p, int keepAlive){
1732 if( p->db==0 ){
1733 sqlite3_initialize();
1734 sqlite3_open(p->zDbFilename, &p->db);
1735 db = p->db;
1736 if( db && sqlite3_errcode(db)==SQLITE_OK ){
@@ -1899,11 +1907,15 @@
1907 /*
1908 ** A routine for handling output from sqlite3_trace().
1909 */
1910 static void sql_trace_callback(void *pArg, const char *z){
1911 FILE *f = (FILE*)pArg;
1912 if( f ){
1913 int i = (int)strlen(z);
1914 while( i>0 && z[i-1]==';' ){ i--; }
1915 fprintf(f, "%.*s;\n", i, z);
1916 }
1917 }
1918
1919 /*
1920 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1921 ** a useful spot to set a debugger breakpoint.
@@ -2017,11 +2029,11 @@
2029 ** Try to transfer data for table zTable. If an error is seen while
2030 ** moving forward, try to go backwards. The backwards movement won't
2031 ** work for WITHOUT ROWID tables.
2032 */
2033 static void tryToCloneData(
2034 ShellState *p,
2035 sqlite3 *newDb,
2036 const char *zTable
2037 ){
2038 sqlite3_stmt *pQuery = 0;
2039 sqlite3_stmt *pInsert = 0;
@@ -2130,14 +2142,14 @@
2142 ** each row, invoke xForEach() on the object defined by that row.
2143 ** If an error is encountered while moving forward through the
2144 ** sqlite_master table, try again moving backwards.
2145 */
2146 static void tryToCloneSchema(
2147 ShellState *p,
2148 sqlite3 *newDb,
2149 const char *zWhere,
2150 void (*xForEach)(ShellState*,sqlite3*,const char*)
2151 ){
2152 sqlite3_stmt *pQuery = 0;
2153 char *zQuery = 0;
2154 int rc;
2155 const unsigned char *zName;
@@ -2204,11 +2216,11 @@
2216 /*
2217 ** Open a new database file named "zNewDb". Try to recover as much information
2218 ** as possible out of the main database (which might be corrupt) and write it
2219 ** into zNewDb.
2220 */
2221 static void tryToClone(ShellState *p, const char *zNewDb){
2222 int rc;
2223 sqlite3 *newDb = 0;
2224 if( access(zNewDb,0)==0 ){
2225 fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
2226 return;
@@ -2229,11 +2241,11 @@
2241 }
2242
2243 /*
2244 ** Change the output file back to stdout
2245 */
2246 static void output_reset(ShellState *p){
2247 if( p->outfile[0]=='|' ){
2248 pclose(p->out);
2249 }else{
2250 output_file_close(p->out);
2251 }
@@ -2245,11 +2257,11 @@
2257 ** If an input line begins with "." then invoke this routine to
2258 ** process that line.
2259 **
2260 ** Return 1 on error, 2 to exit, and 0 otherwise.
2261 */
2262 static int do_meta_command(char *zLine, ShellState *p){
2263 int i = 1;
2264 int nArg = 0;
2265 int n, c;
2266 int rc = 0;
2267 char *azArg[50];
@@ -2363,11 +2375,11 @@
2375 rc = 1;
2376 }
2377 }else
2378
2379 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2380 ShellState data;
2381 char *zErrMsg = 0;
2382 open_db(p, 0);
2383 memcpy(&data, p, sizeof(data));
2384 data.showHeader = 1;
2385 data.mode = MODE_Column;
@@ -2461,15 +2473,15 @@
2473 }else
2474
2475 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2476 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2477 if(val == 1) {
2478 if(!p->normalMode.valid) {
2479 p->normalMode.valid = 1;
2480 p->normalMode.mode = p->mode;
2481 p->normalMode.showHeader = p->showHeader;
2482 memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
2483 }
2484 /* We could put this code under the !p->explainValid
2485 ** condition so that it does not execute if we are already in
2486 ** explain mode. However, always executing it allows us an easy
2487 ** was to reset to explain mode in case the user previously
@@ -2485,20 +2497,20 @@
2497 p->colWidth[3] = 4; /* P2 */
2498 p->colWidth[4] = 4; /* P3 */
2499 p->colWidth[5] = 13; /* P4 */
2500 p->colWidth[6] = 2; /* P5 */
2501 p->colWidth[7] = 13; /* Comment */
2502 }else if (p->normalMode.valid) {
2503 p->normalMode.valid = 0;
2504 p->mode = p->normalMode.mode;
2505 p->showHeader = p->normalMode.showHeader;
2506 memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
2507 }
2508 }else
2509
2510 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2511 ShellState data;
2512 char *zErrMsg = 0;
2513 int doStats = 0;
2514 if( nArg!=1 ){
2515 fprintf(stderr, "Usage: .fullschema\n");
2516 rc = 1;
@@ -2511,11 +2523,11 @@
2523 rc = sqlite3_exec(p->db,
2524 "SELECT sql FROM"
2525 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2526 " FROM sqlite_master UNION ALL"
2527 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2528 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
2529 "ORDER BY rowid",
2530 callback, &data, &zErrMsg
2531 );
2532 if( rc==SQLITE_OK ){
2533 sqlite3_stmt *pStmt;
@@ -2715,11 +2727,11 @@
2727 sqlite3_finalize(pStmt);
2728 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2729 }else
2730
2731 if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2732 ShellState data;
2733 char *zErrMsg = 0;
2734 open_db(p, 0);
2735 memcpy(&data, p, sizeof(data));
2736 data.showHeader = 0;
2737 data.mode = MODE_List;
@@ -3009,11 +3021,11 @@
3021 }
3022 sqlite3_close(pSrc);
3023 }else
3024
3025 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3026 ShellState data;
3027 char *zErrMsg = 0;
3028 open_db(p, 0);
3029 memcpy(&data, p, sizeof(data));
3030 data.showHeader = 0;
3031 data.mode = MODE_Semi;
@@ -3065,11 +3077,11 @@
3077 rc = sqlite3_exec(p->db,
3078 "SELECT sql FROM "
3079 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3080 " FROM sqlite_master UNION ALL"
3081 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
3082 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
3083 "ORDER BY rowid",
3084 callback, &data, &zErrMsg
3085 );
3086 }else{
3087 fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
@@ -3126,11 +3138,11 @@
3138
3139 if( c=='s'
3140 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
3141 ){
3142 char *zCmd;
3143 int i, x;
3144 if( nArg<2 ){
3145 fprintf(stderr, "Usage: .system COMMAND\n");
3146 rc = 1;
3147 goto meta_command_exit;
3148 }
@@ -3137,12 +3149,13 @@
3149 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3150 for(i=2; i<nArg; i++){
3151 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3152 zCmd, azArg[i]);
3153 }
3154 x = system(zCmd);
3155 sqlite3_free(zCmd);
3156 if( x ) fprintf(stderr, "System command returns %d\n", x);
3157 }else
3158
3159 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
3160 int i;
3161 if( nArg!=1 ){
@@ -3150,11 +3163,11 @@
3163 rc = 1;
3164 goto meta_command_exit;
3165 }
3166 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
3167 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3168 fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
3169 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
3170 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
3171 fprintf(p->out,"%9.9s: ", "nullvalue");
3172 output_c_string(p->out, p->nullvalue);
3173 fprintf(p->out, "\n");
@@ -3542,11 +3555,11 @@
3555 ** is saved only if input is interactive. An interrupt signal will
3556 ** cause this routine to exit immediately, unless input is interactive.
3557 **
3558 ** Return the number of errors.
3559 */
3560 static int process_input(ShellState *p, FILE *in){
3561 char *zLine = 0; /* A single input line */
3562 char *zSql = 0; /* Accumulated SQL text */
3563 int nLine; /* Length of current line */
3564 int nSql = 0; /* Bytes of zSql[] used */
3565 int nAlloc = 0; /* Allocated zSql[] space */
@@ -3721,11 +3734,11 @@
3734 ** parameter is NULL, take input from ~/.sqliterc
3735 **
3736 ** Returns the number of errors.
3737 */
3738 static int process_sqliterc(
3739 ShellState *p, /* Configuration data */
3740 const char *sqliterc_override /* Name of config file. NULL to use default */
3741 ){
3742 char *home_dir = NULL;
3743 const char *sqliterc = sqliterc_override;
3744 char *zBuf = 0;
@@ -3774,16 +3787,19 @@
3787 " -help show this message\n"
3788 " -html set output mode to HTML\n"
3789 " -interactive force interactive I/O\n"
3790 " -line set output mode to 'line'\n"
3791 " -list set output mode to 'list'\n"
3792 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
3793 " -mmap N default mmap size set to N\n"
3794 #ifdef SQLITE_ENABLE_MULTIPLEX
3795 " -multiplex enable the multiplexor VFS\n"
3796 #endif
3797 " -newline SEP set newline character(s) for CSV\n"
3798 " -nullvalue TEXT set text string for NULL values. Default ''\n"
3799 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
3800 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
3801 " -separator SEP set output field separator. Default: '|'\n"
3802 " -stats print memory stats before each finalize\n"
3803 " -version show SQLite version\n"
3804 " -vfs NAME use NAME as the default VFS\n"
3805 #ifdef SQLITE_ENABLE_VFSTRACE
@@ -3804,21 +3820,22 @@
3820 }
3821
3822 /*
3823 ** Initialize the state information in data
3824 */
3825 static void main_init(ShellState *data) {
3826 memset(data, 0, sizeof(*data));
3827 data->mode = MODE_List;
3828 memcpy(data->separator,"|", 2);
3829 memcpy(data->newline,"\r\n", 3);
3830 data->showHeader = 0;
3831 data->shellFlgs = SHFLG_Lookaside;
3832 sqlite3_config(SQLITE_CONFIG_URI, 1);
3833 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
3834 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
3835 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3836 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
 
3837 }
3838
3839 /*
3840 ** Output text to the console in a font that attracts extra attention.
3841 */
@@ -3852,11 +3869,11 @@
3869 return argv[i];
3870 }
3871
3872 int main(int argc, char **argv){
3873 char *zErrMsg = 0;
3874 ShellState data;
3875 const char *zInitFile = 0;
3876 char *zFirstCmd = 0;
3877 int i;
3878 int rc = 0;
3879 int warnInmemoryDb = 0;
@@ -3923,10 +3940,37 @@
3940 zSize = cmdline_option_value(argc, argv, ++i);
3941 szHeap = integerValue(zSize);
3942 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3943 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3944 #endif
3945 }else if( strcmp(z,"-scratch")==0 ){
3946 int n, sz;
3947 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3948 if( sz>400000 ) sz = 400000;
3949 if( sz<2500 ) sz = 2500;
3950 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3951 if( n>10 ) n = 10;
3952 if( n<1 ) n = 1;
3953 sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
3954 data.shellFlgs |= SHFLG_Scratch;
3955 }else if( strcmp(z,"-pagecache")==0 ){
3956 int n, sz;
3957 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3958 if( sz>70000 ) sz = 70000;
3959 if( sz<800 ) sz = 800;
3960 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3961 if( n<10 ) n = 10;
3962 sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
3963 data.shellFlgs |= SHFLG_Pagecache;
3964 }else if( strcmp(z,"-lookaside")==0 ){
3965 int n, sz;
3966 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
3967 if( sz<0 ) sz = 0;
3968 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3969 if( n<0 ) n = 0;
3970 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
3971 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
3972 #ifdef SQLITE_ENABLE_VFSTRACE
3973 }else if( strcmp(z,"-vfstrace")==0 ){
3974 extern int vfstrace_register(
3975 const char *zTraceName,
3976 const char *zOldVfsName,
@@ -4038,10 +4082,16 @@
4082 stdin_is_interactive = 1;
4083 }else if( strcmp(z,"-batch")==0 ){
4084 stdin_is_interactive = 0;
4085 }else if( strcmp(z,"-heap")==0 ){
4086 i++;
4087 }else if( strcmp(z,"-scratch")==0 ){
4088 i+=2;
4089 }else if( strcmp(z,"-pagecache")==0 ){
4090 i+=2;
4091 }else if( strcmp(z,"-lookaside")==0 ){
4092 i+=2;
4093 }else if( strcmp(z,"-mmap")==0 ){
4094 i++;
4095 }else if( strcmp(z,"-vfs")==0 ){
4096 i++;
4097 #ifdef SQLITE_ENABLE_VFSTRACE
4098
+2232 -1132
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.8.5. By combining all the individual C code files into this
3
+** version 3.8.6. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -220,13 +220,13 @@
220220
**
221221
** See also: [sqlite3_libversion()],
222222
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223223
** [sqlite_version()] and [sqlite_source_id()].
224224
*/
225
-#define SQLITE_VERSION "3.8.5"
226
-#define SQLITE_VERSION_NUMBER 3008005
227
-#define SQLITE_SOURCE_ID "2014-06-04 14:06:34 b1ed4f2a34ba66c29b130f8d13e9092758019212"
225
+#define SQLITE_VERSION "3.8.6"
226
+#define SQLITE_VERSION_NUMBER 3008006
227
+#define SQLITE_SOURCE_ID "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
228228
229229
/*
230230
** CAPI3REF: Run-Time Library Version Numbers
231231
** KEYWORDS: sqlite3_version, sqlite3_sourceid
232232
**
@@ -382,19 +382,19 @@
382382
/*
383383
** CAPI3REF: Closing A Database Connection
384384
**
385385
** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
386386
** for the [sqlite3] object.
387
-** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
387
+** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
388388
** the [sqlite3] object is successfully destroyed and all associated
389389
** resources are deallocated.
390390
**
391391
** ^If the database connection is associated with unfinalized prepared
392392
** statements or unfinished sqlite3_backup objects then sqlite3_close()
393393
** will leave the database connection open and return [SQLITE_BUSY].
394394
** ^If sqlite3_close_v2() is called with unfinalized prepared statements
395
-** and unfinished sqlite3_backups, then the database connection becomes
395
+** and/or unfinished sqlite3_backups, then the database connection becomes
396396
** an unusable "zombie" which will automatically be deallocated when the
397397
** last prepared statement is finalized or the last sqlite3_backup is
398398
** finished. The sqlite3_close_v2() interface is intended for use with
399399
** host languages that are garbage collected, and where the order in which
400400
** destructors are called is arbitrary.
@@ -403,11 +403,11 @@
403403
** [sqlite3_blob_close | close] all [BLOB handles], and
404404
** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
405405
** with the [sqlite3] object prior to attempting to close the object. ^If
406406
** sqlite3_close_v2() is called on a [database connection] that still has
407407
** outstanding [prepared statements], [BLOB handles], and/or
408
-** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
408
+** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
409409
** of resources is deferred until all [prepared statements], [BLOB handles],
410410
** and [sqlite3_backup] objects are also destroyed.
411411
**
412412
** ^If an [sqlite3] object is destroyed while a transaction is open,
413413
** the transaction is automatically rolled back.
@@ -499,20 +499,18 @@
499499
char **errmsg /* Error msg written here */
500500
);
501501
502502
/*
503503
** CAPI3REF: Result Codes
504
-** KEYWORDS: SQLITE_OK {error code} {error codes}
505
-** KEYWORDS: {result code} {result codes}
504
+** KEYWORDS: {result code definitions}
506505
**
507506
** Many SQLite functions return an integer result code from the set shown
508507
** here in order to indicate success or failure.
509508
**
510509
** New error codes may be added in future versions of SQLite.
511510
**
512
-** See also: [SQLITE_IOERR_READ | extended result codes],
513
-** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
511
+** See also: [extended result code definitions]
514512
*/
515513
#define SQLITE_OK 0 /* Successful result */
516514
/* beginning-of-error-codes */
517515
#define SQLITE_ERROR 1 /* SQL error or missing database */
518516
#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -546,30 +544,23 @@
546544
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
547545
/* end-of-error-codes */
548546
549547
/*
550548
** CAPI3REF: Extended Result Codes
551
-** KEYWORDS: {extended error code} {extended error codes}
552
-** KEYWORDS: {extended result code} {extended result codes}
549
+** KEYWORDS: {extended result code definitions}
553550
**
554
-** In its default configuration, SQLite API routines return one of 26 integer
555
-** [SQLITE_OK | result codes]. However, experience has shown that many of
551
+** In its default configuration, SQLite API routines return one of 30 integer
552
+** [result codes]. However, experience has shown that many of
556553
** these result codes are too coarse-grained. They do not provide as
557554
** much information about problems as programmers might like. In an effort to
558555
** address this, newer versions of SQLite (version 3.3.8 and later) include
559556
** support for additional result codes that provide more detailed information
560
-** about errors. The extended result codes are enabled or disabled
557
+** about errors. These [extended result codes] are enabled or disabled
561558
** on a per database connection basis using the
562
-** [sqlite3_extended_result_codes()] API.
563
-**
564
-** Some of the available extended result codes are listed here.
565
-** One may expect the number of extended result codes will increase
566
-** over time. Software that uses extended result codes should expect
567
-** to see new result codes in future releases of SQLite.
568
-**
569
-** The SQLITE_OK result code will never be extended. It will always
570
-** be exactly zero.
559
+** [sqlite3_extended_result_codes()] API. Or, the extended code for
560
+** the most recent error can be obtained using
561
+** [sqlite3_extended_errcode()].
571562
*/
572563
#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
573564
#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
574565
#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
575566
#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -798,11 +789,11 @@
798789
** write return values. Potential uses for xFileControl() might be
799790
** functions to enable blocking locks with timeouts, to change the
800791
** locking strategy (for example to use dot-file locks), to inquire
801792
** about the status of a lock, or to break stale locks. The SQLite
802793
** core reserves all opcodes less than 100 for its own use.
803
-** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
794
+** A [file control opcodes | list of opcodes] less than 100 is available.
804795
** Applications that define a custom xFileControl method should use opcodes
805796
** greater than 100 to avoid conflicts. VFS implementations should
806797
** return [SQLITE_NOTFOUND] for file control opcodes that they do not
807798
** recognize.
808799
**
@@ -871,10 +862,11 @@
871862
/* Additional methods may be added in future releases */
872863
};
873864
874865
/*
875866
** CAPI3REF: Standard File Control Opcodes
867
+** KEYWORDS: {file control opcodes} {file control opcode}
876868
**
877869
** These integer constants are opcodes for the xFileControl method
878870
** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
879871
** interface.
880872
**
@@ -2150,31 +2142,37 @@
21502142
SQLITE_API int sqlite3_complete16(const void *sql);
21512143
21522144
/*
21532145
** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
21542146
**
2155
-** ^This routine sets a callback function that might be invoked whenever
2156
-** an attempt is made to open a database table that another thread
2157
-** or process has locked.
2147
+** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2148
+** that might be invoked with argument P whenever
2149
+** an attempt is made to access a database table associated with
2150
+** [database connection] D when another thread
2151
+** or process has the table locked.
2152
+** The sqlite3_busy_handler() interface is used to implement
2153
+** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
21582154
**
2159
-** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2155
+** ^If the busy callback is NULL, then [SQLITE_BUSY]
21602156
** is returned immediately upon encountering the lock. ^If the busy callback
21612157
** is not NULL, then the callback might be invoked with two arguments.
21622158
**
21632159
** ^The first argument to the busy handler is a copy of the void* pointer which
21642160
** is the third argument to sqlite3_busy_handler(). ^The second argument to
21652161
** the busy handler callback is the number of times that the busy handler has
2166
-** been invoked for this locking event. ^If the
2162
+** been invoked for the same locking event. ^If the
21672163
** busy callback returns 0, then no additional attempts are made to
2168
-** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2164
+** access the database and [SQLITE_BUSY] is returned
2165
+** to the application.
21692166
** ^If the callback returns non-zero, then another attempt
2170
-** is made to open the database for reading and the cycle repeats.
2167
+** is made to access the database and the cycle repeats.
21712168
**
21722169
** The presence of a busy handler does not guarantee that it will be invoked
21732170
** when there is lock contention. ^If SQLite determines that invoking the busy
21742171
** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2175
-** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2172
+** to the application instead of invoking the
2173
+** busy handler.
21762174
** Consider a scenario where one process is holding a read lock that
21772175
** it is trying to promote to a reserved lock and
21782176
** a second process is holding a reserved lock that it is trying
21792177
** to promote to an exclusive lock. The first process cannot proceed
21802178
** because it is blocked by the second and the second process cannot
@@ -2184,32 +2182,19 @@
21842182
** will induce the first process to release its read lock and allow
21852183
** the second process to proceed.
21862184
**
21872185
** ^The default busy callback is NULL.
21882186
**
2189
-** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2190
-** when SQLite is in the middle of a large transaction where all the
2191
-** changes will not fit into the in-memory cache. SQLite will
2192
-** already hold a RESERVED lock on the database file, but it needs
2193
-** to promote this lock to EXCLUSIVE so that it can spill cache
2194
-** pages into the database file without harm to concurrent
2195
-** readers. ^If it is unable to promote the lock, then the in-memory
2196
-** cache will be left in an inconsistent state and so the error
2197
-** code is promoted from the relatively benign [SQLITE_BUSY] to
2198
-** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
2199
-** forces an automatic rollback of the changes. See the
2200
-** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2201
-** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2202
-** this is important.
2203
-**
22042187
** ^(There can only be a single busy handler defined for each
22052188
** [database connection]. Setting a new busy handler clears any
22062189
** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2207
-** will also set or clear the busy handler.
2190
+** or evaluating [PRAGMA busy_timeout=N] will change the
2191
+** busy handler and thus clear any previously set busy handler.
22082192
**
22092193
** The busy callback should not take any actions which modify the
2210
-** database connection that invoked the busy handler. Any such actions
2194
+** database connection that invoked the busy handler. In other words,
2195
+** the busy handler is not reentrant. Any such actions
22112196
** result in undefined behavior.
22122197
**
22132198
** A busy handler must not close the database connection
22142199
** or [prepared statement] that invoked the busy handler.
22152200
*/
@@ -2221,19 +2206,21 @@
22212206
** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
22222207
** for a specified amount of time when a table is locked. ^The handler
22232208
** will sleep multiple times until at least "ms" milliseconds of sleeping
22242209
** have accumulated. ^After at least "ms" milliseconds of sleeping,
22252210
** the handler returns 0 which causes [sqlite3_step()] to return
2226
-** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2211
+** [SQLITE_BUSY].
22272212
**
22282213
** ^Calling this routine with an argument less than or equal to zero
22292214
** turns off all busy handlers.
22302215
**
22312216
** ^(There can only be a single busy handler for a particular
22322217
** [database connection] any any given moment. If another busy handler
22332218
** was defined (using [sqlite3_busy_handler()]) prior to calling
22342219
** this routine, that other busy handler is cleared.)^
2220
+**
2221
+** See also: [PRAGMA busy_timeout]
22352222
*/
22362223
SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
22372224
22382225
/*
22392226
** CAPI3REF: Convenience Routines For Running Queries
@@ -2631,12 +2618,12 @@
26312618
** return either [SQLITE_OK] or one of these two constants in order
26322619
** to signal SQLite whether or not the action is permitted. See the
26332620
** [sqlite3_set_authorizer | authorizer documentation] for additional
26342621
** information.
26352622
**
2636
-** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2637
-** from the [sqlite3_vtab_on_conflict()] interface.
2623
+** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
2624
+** returned from the [sqlite3_vtab_on_conflict()] interface.
26382625
*/
26392626
#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
26402627
#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
26412628
26422629
/*
@@ -4818,10 +4805,17 @@
48184805
** the name of a folder (a.k.a. directory), then all temporary files
48194806
** created by SQLite when using a built-in [sqlite3_vfs | VFS]
48204807
** will be placed in that directory.)^ ^If this variable
48214808
** is a NULL pointer, then SQLite performs a search for an appropriate
48224809
** temporary file directory.
4810
+**
4811
+** Applications are strongly discouraged from using this global variable.
4812
+** It is required to set a temporary folder on Windows Runtime (WinRT).
4813
+** But for all other platforms, it is highly recommended that applications
4814
+** neither read nor write this variable. This global variable is a relic
4815
+** that exists for backwards compatibility of legacy applications and should
4816
+** be avoided in new projects.
48234817
**
48244818
** It is not safe to read or modify this variable in more than one
48254819
** thread at a time. It is not safe to read or modify this variable
48264820
** if a [database connection] is being used at the same time in a separate
48274821
** thread.
@@ -4837,10 +4831,15 @@
48374831
** [sqlite3_malloc] and the pragma may attempt to free that memory
48384832
** using [sqlite3_free].
48394833
** Hence, if this variable is modified directly, either it should be
48404834
** made NULL or made to point to memory obtained from [sqlite3_malloc]
48414835
** or else the use of the [temp_store_directory pragma] should be avoided.
4836
+** Except when requested by the [temp_store_directory pragma], SQLite
4837
+** does not free the memory that sqlite3_temp_directory points to. If
4838
+** the application wants that memory to be freed, it must do
4839
+** so itself, taking care to only do so after all [database connection]
4840
+** objects have been destroyed.
48424841
**
48434842
** <b>Note to Windows Runtime users:</b> The temporary directory must be set
48444843
** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
48454844
** features that require the use of temporary files may fail. Here is an
48464845
** example of how to do this using C++ with the Windows Runtime:
@@ -5971,14 +5970,16 @@
59715970
** <ul>
59725971
** <li> SQLITE_MUTEX_FAST
59735972
** <li> SQLITE_MUTEX_RECURSIVE
59745973
** <li> SQLITE_MUTEX_STATIC_MASTER
59755974
** <li> SQLITE_MUTEX_STATIC_MEM
5976
-** <li> SQLITE_MUTEX_STATIC_MEM2
5975
+** <li> SQLITE_MUTEX_STATIC_OPEN
59775976
** <li> SQLITE_MUTEX_STATIC_PRNG
59785977
** <li> SQLITE_MUTEX_STATIC_LRU
5979
-** <li> SQLITE_MUTEX_STATIC_LRU2
5978
+** <li> SQLITE_MUTEX_STATIC_PMEM
5979
+** <li> SQLITE_MUTEX_STATIC_APP1
5980
+** <li> SQLITE_MUTEX_STATIC_APP2
59805981
** </ul>)^
59815982
**
59825983
** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
59835984
** cause sqlite3_mutex_alloc() to create
59845985
** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6178,10 +6179,13 @@
61786179
#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
61796180
#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
61806181
#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
61816182
#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
61826183
#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
6184
+#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
6185
+#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
6186
+#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
61836187
61846188
/*
61856189
** CAPI3REF: Retrieve the mutex for a database connection
61866190
**
61876191
** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6273,11 +6277,12 @@
62736277
#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
62746278
#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
62756279
#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
62766280
#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
62776281
#define SQLITE_TESTCTRL_BYTEORDER 22
6278
-#define SQLITE_TESTCTRL_LAST 22
6282
+#define SQLITE_TESTCTRL_ISINIT 23
6283
+#define SQLITE_TESTCTRL_LAST 23
62796284
62806285
/*
62816286
** CAPI3REF: SQLite Runtime Status
62826287
**
62836288
** ^This interface is used to retrieve runtime status information
@@ -7256,10 +7261,13 @@
72567261
** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
72577262
** configured by this function.
72587263
**
72597264
** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
72607265
** from SQL.
7266
+**
7267
+** ^Checkpoints initiated by this mechanism are
7268
+** [sqlite3_wal_checkpoint_v2|PASSIVE].
72617269
**
72627270
** ^Every new [database connection] defaults to having the auto-checkpoint
72637271
** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
72647272
** pages. The use of this interface
72657273
** is only necessary if the default setting is found to be suboptimal
@@ -7273,10 +7281,14 @@
72737281
** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
72747282
** on [database connection] D to be [checkpointed]. ^If X is NULL or an
72757283
** empty string, then a checkpoint is run on all databases of
72767284
** connection D. ^If the database connection D is not in
72777285
** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7286
+** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
7287
+** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
7288
+** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
7289
+** or RESET checkpoint.
72787290
**
72797291
** ^The [wal_checkpoint pragma] can be used to invoke this interface
72807292
** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
72817293
** [wal_autocheckpoint pragma] can be used to cause this interface to be
72827294
** run whenever the WAL reaches a certain size threshold.
@@ -7295,22 +7307,25 @@
72957307
** <dl>
72967308
** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
72977309
** Checkpoint as many frames as possible without waiting for any database
72987310
** readers or writers to finish. Sync the db file if all frames in the log
72997311
** are checkpointed. This mode is the same as calling
7300
-** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7312
+** sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
7313
+** is never invoked.
73017314
**
73027315
** <dt>SQLITE_CHECKPOINT_FULL<dd>
7303
-** This mode blocks (calls the busy-handler callback) until there is no
7316
+** This mode blocks (it invokes the
7317
+** [sqlite3_busy_handler|busy-handler callback]) until there is no
73047318
** database writer and all readers are reading from the most recent database
73057319
** snapshot. It then checkpoints all frames in the log file and syncs the
73067320
** database file. This call blocks database writers while it is running,
73077321
** but not database readers.
73087322
**
73097323
** <dt>SQLITE_CHECKPOINT_RESTART<dd>
73107324
** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7311
-** checkpointing the log file it blocks (calls the busy-handler callback)
7325
+** checkpointing the log file it blocks (calls the
7326
+** [sqlite3_busy_handler|busy-handler callback])
73127327
** until all readers are reading from the database file only. This ensures
73137328
** that the next client to write to the database file restarts the log file
73147329
** from the beginning. This call blocks database writers while it is running,
73157330
** but not database readers.
73167331
** </dl>
@@ -7444,10 +7459,11 @@
74447459
*/
74457460
SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
74467461
74477462
/*
74487463
** CAPI3REF: Conflict resolution modes
7464
+** KEYWORDS: {conflict resolution mode}
74497465
**
74507466
** These constants are returned by [sqlite3_vtab_on_conflict()] to
74517467
** inform a [virtual table] implementation what the [ON CONFLICT] mode
74527468
** is for the SQL statement being evaluated.
74537469
**
@@ -9285,43 +9301,43 @@
92859301
#define OP_Affinity 47 /* synopsis: affinity(r[P1@P2]) */
92869302
#define OP_MakeRecord 48 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
92879303
#define OP_Count 49 /* synopsis: r[P2]=count() */
92889304
#define OP_ReadCookie 50
92899305
#define OP_SetCookie 51
9290
-#define OP_OpenRead 52 /* synopsis: root=P2 iDb=P3 */
9291
-#define OP_OpenWrite 53 /* synopsis: root=P2 iDb=P3 */
9292
-#define OP_OpenAutoindex 54 /* synopsis: nColumn=P2 */
9293
-#define OP_OpenEphemeral 55 /* synopsis: nColumn=P2 */
9294
-#define OP_SorterOpen 56
9295
-#define OP_OpenPseudo 57 /* synopsis: P3 columns in r[P2] */
9296
-#define OP_Close 58
9297
-#define OP_SeekLT 59
9298
-#define OP_SeekLE 60
9299
-#define OP_SeekGE 61
9300
-#define OP_SeekGT 62
9301
-#define OP_Seek 63 /* synopsis: intkey=r[P2] */
9302
-#define OP_NoConflict 64 /* synopsis: key=r[P3@P4] */
9303
-#define OP_NotFound 65 /* synopsis: key=r[P3@P4] */
9304
-#define OP_Found 66 /* synopsis: key=r[P3@P4] */
9305
-#define OP_NotExists 67 /* synopsis: intkey=r[P3] */
9306
-#define OP_Sequence 68 /* synopsis: r[P2]=cursor[P1].ctr++ */
9307
-#define OP_NewRowid 69 /* synopsis: r[P2]=rowid */
9308
-#define OP_Insert 70 /* synopsis: intkey=r[P3] data=r[P2] */
9306
+#define OP_ReopenIdx 52 /* synopsis: root=P2 iDb=P3 */
9307
+#define OP_OpenRead 53 /* synopsis: root=P2 iDb=P3 */
9308
+#define OP_OpenWrite 54 /* synopsis: root=P2 iDb=P3 */
9309
+#define OP_OpenAutoindex 55 /* synopsis: nColumn=P2 */
9310
+#define OP_OpenEphemeral 56 /* synopsis: nColumn=P2 */
9311
+#define OP_SorterOpen 57
9312
+#define OP_OpenPseudo 58 /* synopsis: P3 columns in r[P2] */
9313
+#define OP_Close 59
9314
+#define OP_SeekLT 60 /* synopsis: key=r[P3@P4] */
9315
+#define OP_SeekLE 61 /* synopsis: key=r[P3@P4] */
9316
+#define OP_SeekGE 62 /* synopsis: key=r[P3@P4] */
9317
+#define OP_SeekGT 63 /* synopsis: key=r[P3@P4] */
9318
+#define OP_Seek 64 /* synopsis: intkey=r[P2] */
9319
+#define OP_NoConflict 65 /* synopsis: key=r[P3@P4] */
9320
+#define OP_NotFound 66 /* synopsis: key=r[P3@P4] */
9321
+#define OP_Found 67 /* synopsis: key=r[P3@P4] */
9322
+#define OP_NotExists 68 /* synopsis: intkey=r[P3] */
9323
+#define OP_Sequence 69 /* synopsis: r[P2]=cursor[P1].ctr++ */
9324
+#define OP_NewRowid 70 /* synopsis: r[P2]=rowid */
93099325
#define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
93109326
#define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9311
-#define OP_InsertInt 73 /* synopsis: intkey=P3 data=r[P2] */
9312
-#define OP_Delete 74
9313
-#define OP_ResetCount 75
9327
+#define OP_Insert 73 /* synopsis: intkey=r[P3] data=r[P2] */
9328
+#define OP_InsertInt 74 /* synopsis: intkey=P3 data=r[P2] */
9329
+#define OP_Delete 75
93149330
#define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
93159331
#define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
93169332
#define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
93179333
#define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
93189334
#define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
93199335
#define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
93209336
#define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
93219337
#define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9322
-#define OP_SorterCompare 84 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
9338
+#define OP_ResetCount 84
93239339
#define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
93249340
#define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
93259341
#define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
93269342
#define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
93279343
#define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9328,73 +9344,74 @@
93289344
#define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
93299345
#define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
93309346
#define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
93319347
#define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
93329348
#define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9333
-#define OP_SorterData 95 /* synopsis: r[P2]=data */
9349
+#define OP_SorterCompare 95 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
93349350
#define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
93359351
#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9336
-#define OP_RowKey 98 /* synopsis: r[P2]=key */
9337
-#define OP_RowData 99 /* synopsis: r[P2]=data */
9338
-#define OP_Rowid 100 /* synopsis: r[P2]=rowid */
9339
-#define OP_NullRow 101
9340
-#define OP_Last 102
9341
-#define OP_SorterSort 103
9342
-#define OP_Sort 104
9343
-#define OP_Rewind 105
9344
-#define OP_SorterInsert 106
9345
-#define OP_IdxInsert 107 /* synopsis: key=r[P2] */
9346
-#define OP_IdxDelete 108 /* synopsis: key=r[P2@P3] */
9347
-#define OP_IdxRowid 109 /* synopsis: r[P2]=rowid */
9348
-#define OP_IdxLE 110 /* synopsis: key=r[P3@P4] */
9349
-#define OP_IdxGT 111 /* synopsis: key=r[P3@P4] */
9350
-#define OP_IdxLT 112 /* synopsis: key=r[P3@P4] */
9351
-#define OP_IdxGE 113 /* synopsis: key=r[P3@P4] */
9352
-#define OP_Destroy 114
9353
-#define OP_Clear 115
9354
-#define OP_ResetSorter 116
9355
-#define OP_CreateIndex 117 /* synopsis: r[P2]=root iDb=P1 */
9356
-#define OP_CreateTable 118 /* synopsis: r[P2]=root iDb=P1 */
9357
-#define OP_ParseSchema 119
9358
-#define OP_LoadAnalysis 120
9359
-#define OP_DropTable 121
9360
-#define OP_DropIndex 122
9361
-#define OP_DropTrigger 123
9362
-#define OP_IntegrityCk 124
9363
-#define OP_RowSetAdd 125 /* synopsis: rowset(P1)=r[P2] */
9364
-#define OP_RowSetRead 126 /* synopsis: r[P3]=rowset(P1) */
9365
-#define OP_RowSetTest 127 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9366
-#define OP_Program 128
9367
-#define OP_Param 129
9368
-#define OP_FkCounter 130 /* synopsis: fkctr[P1]+=P2 */
9369
-#define OP_FkIfZero 131 /* synopsis: if fkctr[P1]==0 goto P2 */
9370
-#define OP_MemMax 132 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9352
+#define OP_SorterData 98 /* synopsis: r[P2]=data */
9353
+#define OP_RowKey 99 /* synopsis: r[P2]=key */
9354
+#define OP_RowData 100 /* synopsis: r[P2]=data */
9355
+#define OP_Rowid 101 /* synopsis: r[P2]=rowid */
9356
+#define OP_NullRow 102
9357
+#define OP_Last 103
9358
+#define OP_SorterSort 104
9359
+#define OP_Sort 105
9360
+#define OP_Rewind 106
9361
+#define OP_SorterInsert 107
9362
+#define OP_IdxInsert 108 /* synopsis: key=r[P2] */
9363
+#define OP_IdxDelete 109 /* synopsis: key=r[P2@P3] */
9364
+#define OP_IdxRowid 110 /* synopsis: r[P2]=rowid */
9365
+#define OP_IdxLE 111 /* synopsis: key=r[P3@P4] */
9366
+#define OP_IdxGT 112 /* synopsis: key=r[P3@P4] */
9367
+#define OP_IdxLT 113 /* synopsis: key=r[P3@P4] */
9368
+#define OP_IdxGE 114 /* synopsis: key=r[P3@P4] */
9369
+#define OP_Destroy 115
9370
+#define OP_Clear 116
9371
+#define OP_ResetSorter 117
9372
+#define OP_CreateIndex 118 /* synopsis: r[P2]=root iDb=P1 */
9373
+#define OP_CreateTable 119 /* synopsis: r[P2]=root iDb=P1 */
9374
+#define OP_ParseSchema 120
9375
+#define OP_LoadAnalysis 121
9376
+#define OP_DropTable 122
9377
+#define OP_DropIndex 123
9378
+#define OP_DropTrigger 124
9379
+#define OP_IntegrityCk 125
9380
+#define OP_RowSetAdd 126 /* synopsis: rowset(P1)=r[P2] */
9381
+#define OP_RowSetRead 127 /* synopsis: r[P3]=rowset(P1) */
9382
+#define OP_RowSetTest 128 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9383
+#define OP_Program 129
9384
+#define OP_Param 130
9385
+#define OP_FkCounter 131 /* synopsis: fkctr[P1]+=P2 */
9386
+#define OP_FkIfZero 132 /* synopsis: if fkctr[P1]==0 goto P2 */
93719387
#define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9372
-#define OP_IfPos 134 /* synopsis: if r[P1]>0 goto P2 */
9373
-#define OP_IfNeg 135 /* synopsis: if r[P1]<0 goto P2 */
9374
-#define OP_IfZero 136 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2 */
9375
-#define OP_AggFinal 137 /* synopsis: accum=r[P1] N=P2 */
9376
-#define OP_IncrVacuum 138
9377
-#define OP_Expire 139
9378
-#define OP_TableLock 140 /* synopsis: iDb=P1 root=P2 write=P3 */
9379
-#define OP_VBegin 141
9380
-#define OP_VCreate 142
9388
+#define OP_MemMax 134 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9389
+#define OP_IfPos 135 /* synopsis: if r[P1]>0 goto P2 */
9390
+#define OP_IfNeg 136 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9391
+#define OP_IfZero 137 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2 */
9392
+#define OP_AggFinal 138 /* synopsis: accum=r[P1] N=P2 */
9393
+#define OP_IncrVacuum 139
9394
+#define OP_Expire 140
9395
+#define OP_TableLock 141 /* synopsis: iDb=P1 root=P2 write=P3 */
9396
+#define OP_VBegin 142
93819397
#define OP_ToText 143 /* same as TK_TO_TEXT */
93829398
#define OP_ToBlob 144 /* same as TK_TO_BLOB */
93839399
#define OP_ToNumeric 145 /* same as TK_TO_NUMERIC */
93849400
#define OP_ToInt 146 /* same as TK_TO_INT */
93859401
#define OP_ToReal 147 /* same as TK_TO_REAL */
9386
-#define OP_VDestroy 148
9387
-#define OP_VOpen 149
9388
-#define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */
9389
-#define OP_VNext 151
9390
-#define OP_VRename 152
9391
-#define OP_Pagecount 153
9392
-#define OP_MaxPgcnt 154
9393
-#define OP_Init 155 /* synopsis: Start at P2 */
9394
-#define OP_Noop 156
9395
-#define OP_Explain 157
9402
+#define OP_VCreate 148
9403
+#define OP_VDestroy 149
9404
+#define OP_VOpen 150
9405
+#define OP_VColumn 151 /* synopsis: r[P3]=vcolumn(P2) */
9406
+#define OP_VNext 152
9407
+#define OP_VRename 153
9408
+#define OP_Pagecount 154
9409
+#define OP_MaxPgcnt 155
9410
+#define OP_Init 156 /* synopsis: Start at P2 */
9411
+#define OP_Noop 157
9412
+#define OP_Explain 158
93969413
93979414
93989415
/* Properties such as "out2" or "jump" that are specified in
93999416
** comments following the "case" for each opcode in the vdbe.c
94009417
** are encoded into bitvectors as follows:
@@ -9412,23 +9429,23 @@
94129429
/* 16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
94139430
/* 24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
94149431
/* 32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
94159432
/* 40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
94169433
/* 48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
9417
-/* 56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
9418
-/* 64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
9434
+/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
9435
+/* 64 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x4c,\
94199436
/* 72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
94209437
/* 80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
94219438
/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
9422
-/* 96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
9423
-/* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
9424
-/* 112 */ 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02, 0x00,\
9425
-/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15,\
9426
-/* 128 */ 0x01, 0x02, 0x00, 0x01, 0x08, 0x02, 0x05, 0x05,\
9427
-/* 136 */ 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04,\
9428
-/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01,\
9429
-/* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
9439
+/* 96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
9440
+/* 104 */ 0x01, 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01,\
9441
+/* 112 */ 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02,\
9442
+/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45,\
9443
+/* 128 */ 0x15, 0x01, 0x02, 0x00, 0x01, 0x02, 0x08, 0x05,\
9444
+/* 136 */ 0x05, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,\
9445
+/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,\
9446
+/* 152 */ 0x01, 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
94309447
94319448
/************** End of opcodes.h *********************************************/
94329449
/************** Continuing where we left off in vdbe.h ***********************/
94339450
94349451
/*
@@ -9479,10 +9496,11 @@
94799496
SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
94809497
SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
94819498
#ifndef SQLITE_OMIT_TRACE
94829499
SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
94839500
#endif
9501
+SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
94849502
94859503
SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
94869504
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
94879505
SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
94889506
@@ -10330,22 +10348,22 @@
1033010348
Hash trigHash; /* All triggers indexed by name */
1033110349
Hash fkeyHash; /* All foreign keys by referenced table name */
1033210350
Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
1033310351
u8 file_format; /* Schema format version for this file */
1033410352
u8 enc; /* Text encoding used by this database */
10335
- u16 flags; /* Flags associated with this schema */
10353
+ u16 schemaFlags; /* Flags associated with this schema */
1033610354
int cache_size; /* Number of pages to use in the cache */
1033710355
};
1033810356
1033910357
/*
1034010358
** These macros can be used to test, set, or clear bits in the
1034110359
** Db.pSchema->flags field.
1034210360
*/
10343
-#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
10344
-#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
10345
-#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
10346
-#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
10361
+#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
10362
+#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
10363
+#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
10364
+#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
1034710365
1034810366
/*
1034910367
** Allowed values for the DB.pSchema->flags field.
1035010368
**
1035110369
** The DB_SchemaLoaded flag is set after the database schema has been
@@ -10931,10 +10949,13 @@
1093110949
int tnum; /* Root BTree node for this table (see note above) */
1093210950
i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
1093310951
i16 nCol; /* Number of columns in this table */
1093410952
u16 nRef; /* Number of pointers to this Table */
1093510953
LogEst szTabRow; /* Estimated size of each table row in bytes */
10954
+#ifdef SQLITE_ENABLE_COSTMULT
10955
+ LogEst costMult; /* Cost multiplier for using this table */
10956
+#endif
1093610957
u8 tabFlags; /* Mask of TF_* values */
1093710958
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
1093810959
#ifndef SQLITE_OMIT_ALTERTABLE
1093910960
int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
1094010961
#endif
@@ -11171,10 +11192,13 @@
1117111192
#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
1117211193
1117311194
/* Return true if index X is a PRIMARY KEY index */
1117411195
#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
1117511196
11197
+/* Return true if index X is a UNIQUE index */
11198
+#define IsUniqueIndex(X) ((X)->onError!=OE_None)
11199
+
1117611200
/*
1117711201
** Each sample stored in the sqlite_stat3 table is represented in memory
1117811202
** using a structure of this type. See documentation at the top of the
1117911203
** analyze.c source file for additional information.
1118011204
*/
@@ -11590,10 +11614,11 @@
1159011614
#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
1159111615
#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
1159211616
#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
1159311617
#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
1159411618
#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
11619
+#define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */
1159511620
1159611621
/* Allowed return values from sqlite3WhereIsDistinct()
1159711622
*/
1159811623
#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
1159911624
#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -11846,13 +11871,23 @@
1184611871
1184711872
/*
1184811873
** The yDbMask datatype for the bitmask of all attached databases.
1184911874
*/
1185011875
#if SQLITE_MAX_ATTACHED>30
11851
- typedef sqlite3_uint64 yDbMask;
11876
+ typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
11877
+# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
11878
+# define DbMaskZero(M) memset((M),0,sizeof(M))
11879
+# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
11880
+# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
11881
+# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
1185211882
#else
1185311883
typedef unsigned int yDbMask;
11884
+# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
11885
+# define DbMaskZero(M) (M)=0
11886
+# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
11887
+# define DbMaskAllZero(M) (M)==0
11888
+# define DbMaskNonZero(M) (M)!=0
1185411889
#endif
1185511890
1185611891
/*
1185711892
** An SQL parser context. A copy of this structure is passed through
1185811893
** the parser and down into all the parser action routine in order to
@@ -12521,10 +12556,13 @@
1252112556
SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
1252212557
#else
1252312558
# define sqlite3ViewGetColumnNames(A,B) 0
1252412559
#endif
1252512560
12561
+#if SQLITE_MAX_ATTACHED>30
12562
+SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
12563
+#endif
1252612564
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
1252712565
SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
1252812566
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
1252912567
#ifndef SQLITE_OMIT_AUTOINCREMENT
1253012568
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
@@ -12771,10 +12809,11 @@
1277112809
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
1277212810
SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
1277312811
SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
1277412812
SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
1277512813
SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
12814
+SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
1277612815
SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
1277712816
SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
1277812817
SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
1277912818
SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
1278012819
@@ -12800,11 +12839,11 @@
1280012839
#ifdef SQLITE_ENABLE_8_3_NAMES
1280112840
SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
1280212841
#else
1280312842
# define sqlite3FileSuffix3(X,Y)
1280412843
#endif
12805
-SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
12844
+SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
1280612845
1280712846
SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
1280812847
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
1280912848
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1281012849
void(*)(void*));
@@ -12885,11 +12924,13 @@
1288512924
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
1288612925
1288712926
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1288812927
SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
1288912928
SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12929
+SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
1289012930
SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12931
+SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
1289112932
#endif
1289212933
1289312934
/*
1289412935
** The interface to the LEMON-generated parser
1289512936
*/
@@ -13022,15 +13063,25 @@
1302213063
#else
1302313064
#define sqlite3BeginBenignMalloc()
1302413065
#define sqlite3EndBenignMalloc()
1302513066
#endif
1302613067
13027
-#define IN_INDEX_ROWID 1
13028
-#define IN_INDEX_EPH 2
13029
-#define IN_INDEX_INDEX_ASC 3
13030
-#define IN_INDEX_INDEX_DESC 4
13031
-SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
13068
+/*
13069
+** Allowed return values from sqlite3FindInIndex()
13070
+*/
13071
+#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
13072
+#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
13073
+#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
13074
+#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
13075
+#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
13076
+/*
13077
+** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
13078
+*/
13079
+#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
13080
+#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
13081
+#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
13082
+SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
1303213083
1303313084
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
1303413085
SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
1303513086
SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
1303613087
SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
@@ -13873,18 +13924,22 @@
1387313924
KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
1387413925
int seekResult; /* Result of previous sqlite3BtreeMoveto() */
1387513926
int pseudoTableReg; /* Register holding pseudotable content. */
1387613927
i16 nField; /* Number of fields in the header */
1387713928
u16 nHdrParsed; /* Number of header fields parsed so far */
13929
+#ifdef SQLITE_DEBUG
13930
+ u8 seekOp; /* Most recent seek operation on this cursor */
13931
+#endif
1387813932
i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
1387913933
u8 nullRow; /* True if pointing to a row with no data */
1388013934
u8 rowidIsValid; /* True if lastRowid is valid */
1388113935
u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
1388213936
Bool isEphemeral:1; /* True for an ephemeral table */
1388313937
Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
1388413938
Bool isTable:1; /* True if a table requiring integer keys */
1388513939
Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
13940
+ Pgno pgnoRoot; /* Root page of the open btree cursor */
1388613941
sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
1388713942
i64 seqCount; /* Sequence counter */
1388813943
i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
1388913944
i64 lastRowid; /* Rowid being deleted by OP_Delete */
1389013945
VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
@@ -14199,11 +14254,10 @@
1419914254
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
1420014255
1420114256
int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
1420214257
SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
1420314258
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14204
-SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1420514259
SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
1420614260
SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
1420714261
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
1420814262
SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
1420914263
SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
@@ -18394,11 +18448,11 @@
1839418448
/*
1839518449
** Retrieve a pointer to a static mutex or allocate a new dynamic one.
1839618450
*/
1839718451
SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
1839818452
#ifndef SQLITE_OMIT_AUTOINIT
18399
- if( sqlite3_initialize() ) return 0;
18453
+ if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
1840018454
#endif
1840118455
return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
1840218456
}
1840318457
1840418458
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -18575,11 +18629,11 @@
1857518629
** The sqlite3_mutex_alloc() routine allocates a new
1857618630
** mutex and returns a pointer to it. If it returns NULL
1857718631
** that means that a mutex could not be allocated.
1857818632
*/
1857918633
static sqlite3_mutex *debugMutexAlloc(int id){
18580
- static sqlite3_debug_mutex aStatic[6];
18634
+ static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
1858118635
sqlite3_debug_mutex *pNew = 0;
1858218636
switch( id ){
1858318637
case SQLITE_MUTEX_FAST:
1858418638
case SQLITE_MUTEX_RECURSIVE: {
1858518639
pNew = sqlite3Malloc(sizeof(*pNew));
@@ -18772,14 +18826,17 @@
1877218826
** <ul>
1877318827
** <li> SQLITE_MUTEX_FAST
1877418828
** <li> SQLITE_MUTEX_RECURSIVE
1877518829
** <li> SQLITE_MUTEX_STATIC_MASTER
1877618830
** <li> SQLITE_MUTEX_STATIC_MEM
18777
-** <li> SQLITE_MUTEX_STATIC_MEM2
18831
+** <li> SQLITE_MUTEX_STATIC_OPEN
1877818832
** <li> SQLITE_MUTEX_STATIC_PRNG
1877918833
** <li> SQLITE_MUTEX_STATIC_LRU
1878018834
** <li> SQLITE_MUTEX_STATIC_PMEM
18835
+** <li> SQLITE_MUTEX_STATIC_APP1
18836
+** <li> SQLITE_MUTEX_STATIC_APP2
18837
+** <li> SQLITE_MUTEX_STATIC_APP3
1878118838
** </ul>
1878218839
**
1878318840
** The first two constants cause sqlite3_mutex_alloc() to create
1878418841
** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
1878518842
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -18804,10 +18861,13 @@
1880418861
** mutex types, the same mutex is returned on every call that has
1880518862
** the same type number.
1880618863
*/
1880718864
static sqlite3_mutex *pthreadMutexAlloc(int iType){
1880818865
static sqlite3_mutex staticMutexes[] = {
18866
+ SQLITE3_MUTEX_INITIALIZER,
18867
+ SQLITE3_MUTEX_INITIALIZER,
18868
+ SQLITE3_MUTEX_INITIALIZER,
1880918869
SQLITE3_MUTEX_INITIALIZER,
1881018870
SQLITE3_MUTEX_INITIALIZER,
1881118871
SQLITE3_MUTEX_INITIALIZER,
1881218872
SQLITE3_MUTEX_INITIALIZER,
1881318873
SQLITE3_MUTEX_INITIALIZER,
@@ -19039,14 +19099,227 @@
1903919099
** May you do good and not evil.
1904019100
** May you find forgiveness for yourself and forgive others.
1904119101
** May you share freely, never taking more than you give.
1904219102
**
1904319103
*************************************************************************
19044
-** This file contains the C functions that implement mutexes for win32
19104
+** This file contains the C functions that implement mutexes for Win32.
1904519105
*/
1904619106
1904719107
#if SQLITE_OS_WIN
19108
+/*
19109
+** Include code that is common to all os_*.c files
19110
+*/
19111
+/************** Include os_common.h in the middle of mutex_w32.c *************/
19112
+/************** Begin file os_common.h ***************************************/
19113
+/*
19114
+** 2004 May 22
19115
+**
19116
+** The author disclaims copyright to this source code. In place of
19117
+** a legal notice, here is a blessing:
19118
+**
19119
+** May you do good and not evil.
19120
+** May you find forgiveness for yourself and forgive others.
19121
+** May you share freely, never taking more than you give.
19122
+**
19123
+******************************************************************************
19124
+**
19125
+** This file contains macros and a little bit of code that is common to
19126
+** all of the platform-specific files (os_*.c) and is #included into those
19127
+** files.
19128
+**
19129
+** This file should be #included by the os_*.c files only. It is not a
19130
+** general purpose header file.
19131
+*/
19132
+#ifndef _OS_COMMON_H_
19133
+#define _OS_COMMON_H_
19134
+
19135
+/*
19136
+** At least two bugs have slipped in because we changed the MEMORY_DEBUG
19137
+** macro to SQLITE_DEBUG and some older makefiles have not yet made the
19138
+** switch. The following code should catch this problem at compile-time.
19139
+*/
19140
+#ifdef MEMORY_DEBUG
19141
+# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
19142
+#endif
19143
+
19144
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
19145
+# ifndef SQLITE_DEBUG_OS_TRACE
19146
+# define SQLITE_DEBUG_OS_TRACE 0
19147
+# endif
19148
+ int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
19149
+# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
19150
+#else
19151
+# define OSTRACE(X)
19152
+#endif
19153
+
19154
+/*
19155
+** Macros for performance tracing. Normally turned off. Only works
19156
+** on i486 hardware.
19157
+*/
19158
+#ifdef SQLITE_PERFORMANCE_TRACE
19159
+
19160
+/*
19161
+** hwtime.h contains inline assembler code for implementing
19162
+** high-performance timing routines.
19163
+*/
19164
+/************** Include hwtime.h in the middle of os_common.h ****************/
19165
+/************** Begin file hwtime.h ******************************************/
19166
+/*
19167
+** 2008 May 27
19168
+**
19169
+** The author disclaims copyright to this source code. In place of
19170
+** a legal notice, here is a blessing:
19171
+**
19172
+** May you do good and not evil.
19173
+** May you find forgiveness for yourself and forgive others.
19174
+** May you share freely, never taking more than you give.
19175
+**
19176
+******************************************************************************
19177
+**
19178
+** This file contains inline asm code for retrieving "high-performance"
19179
+** counters for x86 class CPUs.
19180
+*/
19181
+#ifndef _HWTIME_H_
19182
+#define _HWTIME_H_
19183
+
19184
+/*
19185
+** The following routine only works on pentium-class (or newer) processors.
19186
+** It uses the RDTSC opcode to read the cycle count value out of the
19187
+** processor and returns that value. This can be used for high-res
19188
+** profiling.
19189
+*/
19190
+#if (defined(__GNUC__) || defined(_MSC_VER)) && \
19191
+ (defined(i386) || defined(__i386__) || defined(_M_IX86))
19192
+
19193
+ #if defined(__GNUC__)
19194
+
19195
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
19196
+ unsigned int lo, hi;
19197
+ __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
19198
+ return (sqlite_uint64)hi << 32 | lo;
19199
+ }
19200
+
19201
+ #elif defined(_MSC_VER)
19202
+
19203
+ __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
19204
+ __asm {
19205
+ rdtsc
19206
+ ret ; return value at EDX:EAX
19207
+ }
19208
+ }
19209
+
19210
+ #endif
19211
+
19212
+#elif (defined(__GNUC__) && defined(__x86_64__))
19213
+
19214
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
19215
+ unsigned long val;
19216
+ __asm__ __volatile__ ("rdtsc" : "=A" (val));
19217
+ return val;
19218
+ }
19219
+
19220
+#elif (defined(__GNUC__) && defined(__ppc__))
19221
+
19222
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
19223
+ unsigned long long retval;
19224
+ unsigned long junk;
19225
+ __asm__ __volatile__ ("\n\
19226
+ 1: mftbu %1\n\
19227
+ mftb %L0\n\
19228
+ mftbu %0\n\
19229
+ cmpw %0,%1\n\
19230
+ bne 1b"
19231
+ : "=r" (retval), "=r" (junk));
19232
+ return retval;
19233
+ }
19234
+
19235
+#else
19236
+
19237
+ #error Need implementation of sqlite3Hwtime() for your platform.
19238
+
19239
+ /*
19240
+ ** To compile without implementing sqlite3Hwtime() for your platform,
19241
+ ** you can remove the above #error and use the following
19242
+ ** stub function. You will lose timing support for many
19243
+ ** of the debugging and testing utilities, but it should at
19244
+ ** least compile and run.
19245
+ */
19246
+SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
19247
+
19248
+#endif
19249
+
19250
+#endif /* !defined(_HWTIME_H_) */
19251
+
19252
+/************** End of hwtime.h **********************************************/
19253
+/************** Continuing where we left off in os_common.h ******************/
19254
+
19255
+static sqlite_uint64 g_start;
19256
+static sqlite_uint64 g_elapsed;
19257
+#define TIMER_START g_start=sqlite3Hwtime()
19258
+#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
19259
+#define TIMER_ELAPSED g_elapsed
19260
+#else
19261
+#define TIMER_START
19262
+#define TIMER_END
19263
+#define TIMER_ELAPSED ((sqlite_uint64)0)
19264
+#endif
19265
+
19266
+/*
19267
+** If we compile with the SQLITE_TEST macro set, then the following block
19268
+** of code will give us the ability to simulate a disk I/O error. This
19269
+** is used for testing the I/O recovery logic.
19270
+*/
19271
+#ifdef SQLITE_TEST
19272
+SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
19273
+SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
19274
+SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
19275
+SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
19276
+SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
19277
+SQLITE_API int sqlite3_diskfull_pending = 0;
19278
+SQLITE_API int sqlite3_diskfull = 0;
19279
+#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
19280
+#define SimulateIOError(CODE) \
19281
+ if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
19282
+ || sqlite3_io_error_pending-- == 1 ) \
19283
+ { local_ioerr(); CODE; }
19284
+static void local_ioerr(){
19285
+ IOTRACE(("IOERR\n"));
19286
+ sqlite3_io_error_hit++;
19287
+ if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
19288
+}
19289
+#define SimulateDiskfullError(CODE) \
19290
+ if( sqlite3_diskfull_pending ){ \
19291
+ if( sqlite3_diskfull_pending == 1 ){ \
19292
+ local_ioerr(); \
19293
+ sqlite3_diskfull = 1; \
19294
+ sqlite3_io_error_hit = 1; \
19295
+ CODE; \
19296
+ }else{ \
19297
+ sqlite3_diskfull_pending--; \
19298
+ } \
19299
+ }
19300
+#else
19301
+#define SimulateIOErrorBenign(X)
19302
+#define SimulateIOError(A)
19303
+#define SimulateDiskfullError(A)
19304
+#endif
19305
+
19306
+/*
19307
+** When testing, keep a count of the number of open files.
19308
+*/
19309
+#ifdef SQLITE_TEST
19310
+SQLITE_API int sqlite3_open_file_count = 0;
19311
+#define OpenCounter(X) sqlite3_open_file_count+=(X)
19312
+#else
19313
+#define OpenCounter(X)
19314
+#endif
19315
+
19316
+#endif /* !defined(_OS_COMMON_H_) */
19317
+
19318
+/************** End of os_common.h *******************************************/
19319
+/************** Continuing where we left off in mutex_w32.c ******************/
19320
+
1904819321
/*
1904919322
** Include the header file for the Windows VFS.
1905019323
*/
1905119324
/************** Include os_win.h in the middle of mutex_w32.c ****************/
1905219325
/************** Begin file os_win.h ******************************************/
@@ -19122,11 +19395,11 @@
1912219395
/************** Continuing where we left off in mutex_w32.c ******************/
1912319396
#endif
1912419397
1912519398
/*
1912619399
** The code in this file is only used if we are compiling multithreaded
19127
-** on a win32 system.
19400
+** on a Win32 system.
1912819401
*/
1912919402
#ifdef SQLITE_MUTEX_W32
1913019403
1913119404
/*
1913219405
** Each recursive mutex is an instance of the following structure.
@@ -19135,94 +19408,75 @@
1913519408
CRITICAL_SECTION mutex; /* Mutex controlling the lock */
1913619409
int id; /* Mutex type */
1913719410
#ifdef SQLITE_DEBUG
1913819411
volatile int nRef; /* Number of enterances */
1913919412
volatile DWORD owner; /* Thread holding this mutex */
19140
- int trace; /* True to trace changes */
19413
+ volatile int trace; /* True to trace changes */
1914119414
#endif
1914219415
};
19416
+
19417
+/*
19418
+** These are the initializer values used when declaring a "static" mutex
19419
+** on Win32. It should be noted that all mutexes require initialization
19420
+** on the Win32 platform.
19421
+*/
1914319422
#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
19423
+
1914419424
#ifdef SQLITE_DEBUG
19145
-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
19425
+#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
19426
+ 0L, (DWORD)0, 0 }
1914619427
#else
1914719428
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
1914819429
#endif
1914919430
19150
-/*
19151
-** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
19152
-** or WinCE. Return false (zero) for Win95, Win98, or WinME.
19153
-**
19154
-** Here is an interesting observation: Win95, Win98, and WinME lack
19155
-** the LockFileEx() API. But we can still statically link against that
19156
-** API as long as we don't call it win running Win95/98/ME. A call to
19157
-** this routine is used to determine if the host is Win95/98/ME or
19158
-** WinNT/2K/XP so that we will know whether or not we can safely call
19159
-** the LockFileEx() API.
19160
-**
19161
-** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
19162
-** which is only available if your application was compiled with
19163
-** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
19164
-** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
19165
-** this out as well.
19166
-*/
19167
-#if 0
19168
-#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
19169
-# define mutexIsNT() (1)
19170
-#else
19171
- static int mutexIsNT(void){
19172
- static int osType = 0;
19173
- if( osType==0 ){
19174
- OSVERSIONINFO sInfo;
19175
- sInfo.dwOSVersionInfoSize = sizeof(sInfo);
19176
- GetVersionEx(&sInfo);
19177
- osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
19178
- }
19179
- return osType==2;
19180
- }
19181
-#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
19182
-#endif
19183
-
1918419431
#ifdef SQLITE_DEBUG
1918519432
/*
1918619433
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1918719434
** intended for use only inside assert() statements.
1918819435
*/
1918919436
static int winMutexHeld(sqlite3_mutex *p){
1919019437
return p->nRef!=0 && p->owner==GetCurrentThreadId();
1919119438
}
19439
+
1919219440
static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
1919319441
return p->nRef==0 || p->owner!=tid;
1919419442
}
19443
+
1919519444
static int winMutexNotheld(sqlite3_mutex *p){
19196
- DWORD tid = GetCurrentThreadId();
19445
+ DWORD tid = GetCurrentThreadId();
1919719446
return winMutexNotheld2(p, tid);
1919819447
}
1919919448
#endif
1920019449
19201
-
1920219450
/*
1920319451
** Initialize and deinitialize the mutex subsystem.
1920419452
*/
19205
-static sqlite3_mutex winMutex_staticMutexes[6] = {
19453
+static sqlite3_mutex winMutex_staticMutexes[] = {
19454
+ SQLITE3_MUTEX_INITIALIZER,
19455
+ SQLITE3_MUTEX_INITIALIZER,
19456
+ SQLITE3_MUTEX_INITIALIZER,
1920619457
SQLITE3_MUTEX_INITIALIZER,
1920719458
SQLITE3_MUTEX_INITIALIZER,
1920819459
SQLITE3_MUTEX_INITIALIZER,
1920919460
SQLITE3_MUTEX_INITIALIZER,
1921019461
SQLITE3_MUTEX_INITIALIZER,
1921119462
SQLITE3_MUTEX_INITIALIZER
1921219463
};
19464
+
1921319465
static int winMutex_isInit = 0;
19214
-/* As winMutexInit() and winMutexEnd() are called as part
19215
-** of the sqlite3_initialize and sqlite3_shutdown()
19216
-** processing, the "interlocked" magic is probably not
19217
-** strictly necessary.
19466
+static int winMutex_isNt = -1; /* <0 means "need to query" */
19467
+
19468
+/* As the winMutexInit() and winMutexEnd() functions are called as part
19469
+** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
19470
+** "interlocked" magic used here is probably not strictly necessary.
1921819471
*/
19219
-static LONG winMutex_lock = 0;
19472
+static LONG volatile winMutex_lock = 0;
1922019473
19474
+SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
1922119475
SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
1922219476
19223
-static int winMutexInit(void){
19477
+static int winMutexInit(void){
1922419478
/* The first to increment to 1 does actual initialization */
1922519479
if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
1922619480
int i;
1922719481
for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
1922819482
#if SQLITE_OS_WINRT
@@ -19231,20 +19485,21 @@
1923119485
InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
1923219486
#endif
1923319487
}
1923419488
winMutex_isInit = 1;
1923519489
}else{
19236
- /* Someone else is in the process of initing the static mutexes */
19490
+ /* Another thread is (in the process of) initializing the static
19491
+ ** mutexes */
1923719492
while( !winMutex_isInit ){
1923819493
sqlite3_win32_sleep(1);
1923919494
}
1924019495
}
19241
- return SQLITE_OK;
19496
+ return SQLITE_OK;
1924219497
}
1924319498
19244
-static int winMutexEnd(void){
19245
- /* The first to decrement to 0 does actual shutdown
19499
+static int winMutexEnd(void){
19500
+ /* The first to decrement to 0 does actual shutdown
1924619501
** (which should be the last to shutdown.) */
1924719502
if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
1924819503
if( winMutex_isInit==1 ){
1924919504
int i;
1925019505
for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
@@ -19251,11 +19506,11 @@
1925119506
DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
1925219507
}
1925319508
winMutex_isInit = 0;
1925419509
}
1925519510
}
19256
- return SQLITE_OK;
19511
+ return SQLITE_OK;
1925719512
}
1925819513
1925919514
/*
1926019515
** The sqlite3_mutex_alloc() routine allocates a new
1926119516
** mutex and returns a pointer to it. If it returns NULL
@@ -19266,14 +19521,17 @@
1926619521
** <ul>
1926719522
** <li> SQLITE_MUTEX_FAST
1926819523
** <li> SQLITE_MUTEX_RECURSIVE
1926919524
** <li> SQLITE_MUTEX_STATIC_MASTER
1927019525
** <li> SQLITE_MUTEX_STATIC_MEM
19271
-** <li> SQLITE_MUTEX_STATIC_MEM2
19526
+** <li> SQLITE_MUTEX_STATIC_OPEN
1927219527
** <li> SQLITE_MUTEX_STATIC_PRNG
1927319528
** <li> SQLITE_MUTEX_STATIC_LRU
1927419529
** <li> SQLITE_MUTEX_STATIC_PMEM
19530
+** <li> SQLITE_MUTEX_STATIC_APP1
19531
+** <li> SQLITE_MUTEX_STATIC_APP2
19532
+** <li> SQLITE_MUTEX_STATIC_APP3
1927519533
** </ul>
1927619534
**
1927719535
** The first two constants cause sqlite3_mutex_alloc() to create
1927819536
** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
1927919537
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -19292,11 +19550,11 @@
1929219550
** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
1929319551
** SQLITE_MUTEX_RECURSIVE.
1929419552
**
1929519553
** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
1929619554
** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
19297
-** returns a different mutex on every call. But for the static
19555
+** returns a different mutex on every call. But for the static
1929819556
** mutex types, the same mutex is returned on every call that has
1929919557
** the same type number.
1930019558
*/
1930119559
static sqlite3_mutex *winMutexAlloc(int iType){
1930219560
sqlite3_mutex *p;
@@ -19303,13 +19561,16 @@
1930319561
1930419562
switch( iType ){
1930519563
case SQLITE_MUTEX_FAST:
1930619564
case SQLITE_MUTEX_RECURSIVE: {
1930719565
p = sqlite3MallocZero( sizeof(*p) );
19308
- if( p ){
19566
+ if( p ){
1930919567
#ifdef SQLITE_DEBUG
1931019568
p->id = iType;
19569
+#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
19570
+ p->trace = 1;
19571
+#endif
1931119572
#endif
1931219573
#if SQLITE_OS_WINRT
1931319574
InitializeCriticalSectionEx(&p->mutex, 0, 0);
1931419575
#else
1931519576
InitializeCriticalSection(&p->mutex);
@@ -19316,16 +19577,19 @@
1931619577
#endif
1931719578
}
1931819579
break;
1931919580
}
1932019581
default: {
19321
- assert( winMutex_isInit==1 );
1932219582
assert( iType-2 >= 0 );
1932319583
assert( iType-2 < ArraySize(winMutex_staticMutexes) );
19584
+ assert( winMutex_isInit==1 );
1932419585
p = &winMutex_staticMutexes[iType-2];
1932519586
#ifdef SQLITE_DEBUG
1932619587
p->id = iType;
19588
+#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
19589
+ p->trace = 1;
19590
+#endif
1932719591
#endif
1932819592
break;
1932919593
}
1933019594
}
1933119595
return p;
@@ -19337,12 +19601,15 @@
1933719601
** allocated mutex. SQLite is careful to deallocate every
1933819602
** mutex that it allocates.
1933919603
*/
1934019604
static void winMutexFree(sqlite3_mutex *p){
1934119605
assert( p );
19606
+#ifdef SQLITE_DEBUG
1934219607
assert( p->nRef==0 && p->owner==0 );
1934319608
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
19609
+#endif
19610
+ assert( winMutex_isInit==1 );
1934419611
DeleteCriticalSection(&p->mutex);
1934519612
sqlite3_free(p);
1934619613
}
1934719614
1934819615
/*
@@ -19355,53 +19622,71 @@
1935519622
** mutex must be exited an equal number of times before another thread
1935619623
** can enter. If the same thread tries to enter any other kind of mutex
1935719624
** more than once, the behavior is undefined.
1935819625
*/
1935919626
static void winMutexEnter(sqlite3_mutex *p){
19627
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
19628
+ DWORD tid = GetCurrentThreadId();
19629
+#endif
1936019630
#ifdef SQLITE_DEBUG
19361
- DWORD tid = GetCurrentThreadId();
19631
+ assert( p );
1936219632
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19633
+#else
19634
+ assert( p );
1936319635
#endif
19636
+ assert( winMutex_isInit==1 );
1936419637
EnterCriticalSection(&p->mutex);
1936519638
#ifdef SQLITE_DEBUG
1936619639
assert( p->nRef>0 || p->owner==0 );
19367
- p->owner = tid;
19640
+ p->owner = tid;
1936819641
p->nRef++;
1936919642
if( p->trace ){
19370
- printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19643
+ OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
19644
+ tid, p, p->trace, p->nRef));
1937119645
}
1937219646
#endif
1937319647
}
19648
+
1937419649
static int winMutexTry(sqlite3_mutex *p){
19375
-#ifndef NDEBUG
19376
- DWORD tid = GetCurrentThreadId();
19650
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
19651
+ DWORD tid = GetCurrentThreadId();
1937719652
#endif
1937819653
int rc = SQLITE_BUSY;
19654
+ assert( p );
1937919655
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
1938019656
/*
1938119657
** The sqlite3_mutex_try() routine is very rarely used, and when it
1938219658
** is used it is merely an optimization. So it is OK for it to always
19383
- ** fail.
19659
+ ** fail.
1938419660
**
1938519661
** The TryEnterCriticalSection() interface is only available on WinNT.
1938619662
** And some windows compilers complain if you try to use it without
1938719663
** first doing some #defines that prevent SQLite from building on Win98.
1938819664
** For that reason, we will omit this optimization for now. See
1938919665
** ticket #2685.
1939019666
*/
19391
-#if 0
19392
- if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
19667
+#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
19668
+ assert( winMutex_isInit==1 );
19669
+ assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
19670
+ if( winMutex_isNt<0 ){
19671
+ winMutex_isNt = sqlite3_win32_is_nt();
19672
+ }
19673
+ assert( winMutex_isNt==0 || winMutex_isNt==1 );
19674
+ if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
19675
+#ifdef SQLITE_DEBUG
1939319676
p->owner = tid;
1939419677
p->nRef++;
19678
+#endif
1939519679
rc = SQLITE_OK;
1939619680
}
1939719681
#else
1939819682
UNUSED_PARAMETER(p);
1939919683
#endif
1940019684
#ifdef SQLITE_DEBUG
19401
- if( rc==SQLITE_OK && p->trace ){
19402
- printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19685
+ if( p->trace ){
19686
+ OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
19687
+ tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
1940319688
}
1940419689
#endif
1940519690
return rc;
1940619691
}
1940719692
@@ -19410,22 +19695,27 @@
1941019695
** previously entered by the same thread. The behavior
1941119696
** is undefined if the mutex is not currently entered or
1941219697
** is not currently allocated. SQLite will never do either.
1941319698
*/
1941419699
static void winMutexLeave(sqlite3_mutex *p){
19415
-#ifndef NDEBUG
19700
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1941619701
DWORD tid = GetCurrentThreadId();
19702
+#endif
19703
+ assert( p );
19704
+#ifdef SQLITE_DEBUG
1941719705
assert( p->nRef>0 );
1941819706
assert( p->owner==tid );
1941919707
p->nRef--;
1942019708
if( p->nRef==0 ) p->owner = 0;
1942119709
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1942219710
#endif
19711
+ assert( winMutex_isInit==1 );
1942319712
LeaveCriticalSection(&p->mutex);
1942419713
#ifdef SQLITE_DEBUG
1942519714
if( p->trace ){
19426
- printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19715
+ OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
19716
+ tid, p, p->trace, p->nRef));
1942719717
}
1942819718
#endif
1942919719
}
1943019720
1943119721
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
@@ -19443,13 +19733,13 @@
1944319733
#else
1944419734
0,
1944519735
0
1944619736
#endif
1944719737
};
19448
-
1944919738
return &sMutex;
1945019739
}
19740
+
1945119741
#endif /* SQLITE_MUTEX_W32 */
1945219742
1945319743
/************** End of mutex_w32.c *******************************************/
1945419744
/************** Begin file malloc.c ******************************************/
1945519745
/*
@@ -21562,12 +21852,12 @@
2156221852
** * Bytes in the range of 0x80 through 0xbf which occur as the first
2156321853
** byte of a character are interpreted as single-byte characters
2156421854
** and rendered as themselves even though they are technically
2156521855
** invalid characters.
2156621856
**
21567
-** * This routine accepts an infinite number of different UTF8 encodings
21568
-** for unicode values 0x80 and greater. It do not change over-length
21857
+** * This routine accepts over-length UTF8 encodings
21858
+** for unicode values 0x80 and greater. It does not change over-length
2156921859
** encodings to 0xfffd as some systems recommend.
2157021860
*/
2157121861
#define READ_UTF8(zIn, zTerm, c) \
2157221862
c = *(zIn++); \
2157321863
if( c>=0xc0 ){ \
@@ -22420,13 +22710,13 @@
2242022710
testcase( c==(+1) );
2242122711
}
2242222712
return c;
2242322713
}
2242422714
22425
-
2242622715
/*
22427
-** Convert zNum to a 64-bit signed integer.
22716
+** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
22717
+** routine does *not* accept hexadecimal notation.
2242822718
**
2242922719
** If the zNum value is representable as a 64-bit twos-complement
2243022720
** integer, then write that value into *pNum and return 0.
2243122721
**
2243222722
** If zNum is exactly 9223372036854775808, return 2. This special
@@ -22509,14 +22799,48 @@
2250922799
assert( u-1==LARGEST_INT64 );
2251022800
return neg ? 0 : 2;
2251122801
}
2251222802
}
2251322803
}
22804
+
22805
+/*
22806
+** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
22807
+** into a 64-bit signed integer. This routine accepts hexadecimal literals,
22808
+** whereas sqlite3Atoi64() does not.
22809
+**
22810
+** Returns:
22811
+**
22812
+** 0 Successful transformation. Fits in a 64-bit signed integer.
22813
+** 1 Integer too large for a 64-bit signed integer or is malformed
22814
+** 2 Special case of 9223372036854775808
22815
+*/
22816
+SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
22817
+#ifndef SQLITE_OMIT_HEX_INTEGER
22818
+ if( z[0]=='0'
22819
+ && (z[1]=='x' || z[1]=='X')
22820
+ && sqlite3Isxdigit(z[2])
22821
+ ){
22822
+ u64 u = 0;
22823
+ int i, k;
22824
+ for(i=2; z[i]=='0'; i++){}
22825
+ for(k=i; sqlite3Isxdigit(z[k]); k++){
22826
+ u = u*16 + sqlite3HexToInt(z[k]);
22827
+ }
22828
+ memcpy(pOut, &u, 8);
22829
+ return (z[k]==0 && k-i<=16) ? 0 : 1;
22830
+ }else
22831
+#endif /* SQLITE_OMIT_HEX_INTEGER */
22832
+ {
22833
+ return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
22834
+ }
22835
+}
2251422836
2251522837
/*
2251622838
** If zNum represents an integer that will fit in 32-bits, then set
2251722839
** *pValue to that integer and return true. Otherwise return false.
22840
+**
22841
+** This routine accepts both decimal and hexadecimal notation for integers.
2251822842
**
2251922843
** Any non-numeric characters that following zNum are ignored.
2252022844
** This is different from sqlite3Atoi64() which requires the
2252122845
** input number to be zero-terminated.
2252222846
*/
@@ -22528,11 +22852,29 @@
2252822852
neg = 1;
2252922853
zNum++;
2253022854
}else if( zNum[0]=='+' ){
2253122855
zNum++;
2253222856
}
22533
- while( zNum[0]=='0' ) zNum++;
22857
+#ifndef SQLITE_OMIT_HEX_INTEGER
22858
+ else if( zNum[0]=='0'
22859
+ && (zNum[1]=='x' || zNum[1]=='X')
22860
+ && sqlite3Isxdigit(zNum[2])
22861
+ ){
22862
+ u32 u = 0;
22863
+ zNum += 2;
22864
+ while( zNum[0]=='0' ) zNum++;
22865
+ for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
22866
+ u = u*16 + sqlite3HexToInt(zNum[i]);
22867
+ }
22868
+ if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
22869
+ memcpy(pValue, &u, 4);
22870
+ return 1;
22871
+ }else{
22872
+ return 0;
22873
+ }
22874
+ }
22875
+#endif
2253422876
for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
2253522877
v = v*10 + c;
2253622878
}
2253722879
2253822880
/* The longest decimal representation of a 32 bit integer is 10 digits:
@@ -23604,43 +23946,43 @@
2360423946
/* 47 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
2360523947
/* 48 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
2360623948
/* 49 */ "Count" OpHelp("r[P2]=count()"),
2360723949
/* 50 */ "ReadCookie" OpHelp(""),
2360823950
/* 51 */ "SetCookie" OpHelp(""),
23609
- /* 52 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
23610
- /* 53 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
23611
- /* 54 */ "OpenAutoindex" OpHelp("nColumn=P2"),
23612
- /* 55 */ "OpenEphemeral" OpHelp("nColumn=P2"),
23613
- /* 56 */ "SorterOpen" OpHelp(""),
23614
- /* 57 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
23615
- /* 58 */ "Close" OpHelp(""),
23616
- /* 59 */ "SeekLT" OpHelp(""),
23617
- /* 60 */ "SeekLE" OpHelp(""),
23618
- /* 61 */ "SeekGE" OpHelp(""),
23619
- /* 62 */ "SeekGT" OpHelp(""),
23620
- /* 63 */ "Seek" OpHelp("intkey=r[P2]"),
23621
- /* 64 */ "NoConflict" OpHelp("key=r[P3@P4]"),
23622
- /* 65 */ "NotFound" OpHelp("key=r[P3@P4]"),
23623
- /* 66 */ "Found" OpHelp("key=r[P3@P4]"),
23624
- /* 67 */ "NotExists" OpHelp("intkey=r[P3]"),
23625
- /* 68 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
23626
- /* 69 */ "NewRowid" OpHelp("r[P2]=rowid"),
23627
- /* 70 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
23951
+ /* 52 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
23952
+ /* 53 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
23953
+ /* 54 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
23954
+ /* 55 */ "OpenAutoindex" OpHelp("nColumn=P2"),
23955
+ /* 56 */ "OpenEphemeral" OpHelp("nColumn=P2"),
23956
+ /* 57 */ "SorterOpen" OpHelp(""),
23957
+ /* 58 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
23958
+ /* 59 */ "Close" OpHelp(""),
23959
+ /* 60 */ "SeekLT" OpHelp("key=r[P3@P4]"),
23960
+ /* 61 */ "SeekLE" OpHelp("key=r[P3@P4]"),
23961
+ /* 62 */ "SeekGE" OpHelp("key=r[P3@P4]"),
23962
+ /* 63 */ "SeekGT" OpHelp("key=r[P3@P4]"),
23963
+ /* 64 */ "Seek" OpHelp("intkey=r[P2]"),
23964
+ /* 65 */ "NoConflict" OpHelp("key=r[P3@P4]"),
23965
+ /* 66 */ "NotFound" OpHelp("key=r[P3@P4]"),
23966
+ /* 67 */ "Found" OpHelp("key=r[P3@P4]"),
23967
+ /* 68 */ "NotExists" OpHelp("intkey=r[P3]"),
23968
+ /* 69 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
23969
+ /* 70 */ "NewRowid" OpHelp("r[P2]=rowid"),
2362823970
/* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
2362923971
/* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
23630
- /* 73 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
23631
- /* 74 */ "Delete" OpHelp(""),
23632
- /* 75 */ "ResetCount" OpHelp(""),
23972
+ /* 73 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
23973
+ /* 74 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
23974
+ /* 75 */ "Delete" OpHelp(""),
2363323975
/* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
2363423976
/* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
2363523977
/* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
2363623978
/* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
2363723979
/* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
2363823980
/* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
2363923981
/* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
2364023982
/* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
23641
- /* 84 */ "SorterCompare" OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
23983
+ /* 84 */ "ResetCount" OpHelp(""),
2364223984
/* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
2364323985
/* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
2364423986
/* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
2364523987
/* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
2364623988
/* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -23647,73 +23989,74 @@
2364723989
/* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
2364823990
/* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
2364923991
/* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
2365023992
/* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
2365123993
/* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
23652
- /* 95 */ "SorterData" OpHelp("r[P2]=data"),
23994
+ /* 95 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
2365323995
/* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
2365423996
/* 97 */ "String8" OpHelp("r[P2]='P4'"),
23655
- /* 98 */ "RowKey" OpHelp("r[P2]=key"),
23656
- /* 99 */ "RowData" OpHelp("r[P2]=data"),
23657
- /* 100 */ "Rowid" OpHelp("r[P2]=rowid"),
23658
- /* 101 */ "NullRow" OpHelp(""),
23659
- /* 102 */ "Last" OpHelp(""),
23660
- /* 103 */ "SorterSort" OpHelp(""),
23661
- /* 104 */ "Sort" OpHelp(""),
23662
- /* 105 */ "Rewind" OpHelp(""),
23663
- /* 106 */ "SorterInsert" OpHelp(""),
23664
- /* 107 */ "IdxInsert" OpHelp("key=r[P2]"),
23665
- /* 108 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
23666
- /* 109 */ "IdxRowid" OpHelp("r[P2]=rowid"),
23667
- /* 110 */ "IdxLE" OpHelp("key=r[P3@P4]"),
23668
- /* 111 */ "IdxGT" OpHelp("key=r[P3@P4]"),
23669
- /* 112 */ "IdxLT" OpHelp("key=r[P3@P4]"),
23670
- /* 113 */ "IdxGE" OpHelp("key=r[P3@P4]"),
23671
- /* 114 */ "Destroy" OpHelp(""),
23672
- /* 115 */ "Clear" OpHelp(""),
23673
- /* 116 */ "ResetSorter" OpHelp(""),
23674
- /* 117 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
23675
- /* 118 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
23676
- /* 119 */ "ParseSchema" OpHelp(""),
23677
- /* 120 */ "LoadAnalysis" OpHelp(""),
23678
- /* 121 */ "DropTable" OpHelp(""),
23679
- /* 122 */ "DropIndex" OpHelp(""),
23680
- /* 123 */ "DropTrigger" OpHelp(""),
23681
- /* 124 */ "IntegrityCk" OpHelp(""),
23682
- /* 125 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
23683
- /* 126 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
23684
- /* 127 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
23685
- /* 128 */ "Program" OpHelp(""),
23686
- /* 129 */ "Param" OpHelp(""),
23687
- /* 130 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
23688
- /* 131 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
23689
- /* 132 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
23997
+ /* 98 */ "SorterData" OpHelp("r[P2]=data"),
23998
+ /* 99 */ "RowKey" OpHelp("r[P2]=key"),
23999
+ /* 100 */ "RowData" OpHelp("r[P2]=data"),
24000
+ /* 101 */ "Rowid" OpHelp("r[P2]=rowid"),
24001
+ /* 102 */ "NullRow" OpHelp(""),
24002
+ /* 103 */ "Last" OpHelp(""),
24003
+ /* 104 */ "SorterSort" OpHelp(""),
24004
+ /* 105 */ "Sort" OpHelp(""),
24005
+ /* 106 */ "Rewind" OpHelp(""),
24006
+ /* 107 */ "SorterInsert" OpHelp(""),
24007
+ /* 108 */ "IdxInsert" OpHelp("key=r[P2]"),
24008
+ /* 109 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
24009
+ /* 110 */ "IdxRowid" OpHelp("r[P2]=rowid"),
24010
+ /* 111 */ "IdxLE" OpHelp("key=r[P3@P4]"),
24011
+ /* 112 */ "IdxGT" OpHelp("key=r[P3@P4]"),
24012
+ /* 113 */ "IdxLT" OpHelp("key=r[P3@P4]"),
24013
+ /* 114 */ "IdxGE" OpHelp("key=r[P3@P4]"),
24014
+ /* 115 */ "Destroy" OpHelp(""),
24015
+ /* 116 */ "Clear" OpHelp(""),
24016
+ /* 117 */ "ResetSorter" OpHelp(""),
24017
+ /* 118 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
24018
+ /* 119 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
24019
+ /* 120 */ "ParseSchema" OpHelp(""),
24020
+ /* 121 */ "LoadAnalysis" OpHelp(""),
24021
+ /* 122 */ "DropTable" OpHelp(""),
24022
+ /* 123 */ "DropIndex" OpHelp(""),
24023
+ /* 124 */ "DropTrigger" OpHelp(""),
24024
+ /* 125 */ "IntegrityCk" OpHelp(""),
24025
+ /* 126 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
24026
+ /* 127 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
24027
+ /* 128 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
24028
+ /* 129 */ "Program" OpHelp(""),
24029
+ /* 130 */ "Param" OpHelp(""),
24030
+ /* 131 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
24031
+ /* 132 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
2369024032
/* 133 */ "Real" OpHelp("r[P2]=P4"),
23691
- /* 134 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
23692
- /* 135 */ "IfNeg" OpHelp("if r[P1]<0 goto P2"),
23693
- /* 136 */ "IfZero" OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
23694
- /* 137 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
23695
- /* 138 */ "IncrVacuum" OpHelp(""),
23696
- /* 139 */ "Expire" OpHelp(""),
23697
- /* 140 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
23698
- /* 141 */ "VBegin" OpHelp(""),
23699
- /* 142 */ "VCreate" OpHelp(""),
24033
+ /* 134 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
24034
+ /* 135 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
24035
+ /* 136 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
24036
+ /* 137 */ "IfZero" OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
24037
+ /* 138 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
24038
+ /* 139 */ "IncrVacuum" OpHelp(""),
24039
+ /* 140 */ "Expire" OpHelp(""),
24040
+ /* 141 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
24041
+ /* 142 */ "VBegin" OpHelp(""),
2370024042
/* 143 */ "ToText" OpHelp(""),
2370124043
/* 144 */ "ToBlob" OpHelp(""),
2370224044
/* 145 */ "ToNumeric" OpHelp(""),
2370324045
/* 146 */ "ToInt" OpHelp(""),
2370424046
/* 147 */ "ToReal" OpHelp(""),
23705
- /* 148 */ "VDestroy" OpHelp(""),
23706
- /* 149 */ "VOpen" OpHelp(""),
23707
- /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
23708
- /* 151 */ "VNext" OpHelp(""),
23709
- /* 152 */ "VRename" OpHelp(""),
23710
- /* 153 */ "Pagecount" OpHelp(""),
23711
- /* 154 */ "MaxPgcnt" OpHelp(""),
23712
- /* 155 */ "Init" OpHelp("Start at P2"),
23713
- /* 156 */ "Noop" OpHelp(""),
23714
- /* 157 */ "Explain" OpHelp(""),
24047
+ /* 148 */ "VCreate" OpHelp(""),
24048
+ /* 149 */ "VDestroy" OpHelp(""),
24049
+ /* 150 */ "VOpen" OpHelp(""),
24050
+ /* 151 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
24051
+ /* 152 */ "VNext" OpHelp(""),
24052
+ /* 153 */ "VRename" OpHelp(""),
24053
+ /* 154 */ "Pagecount" OpHelp(""),
24054
+ /* 155 */ "MaxPgcnt" OpHelp(""),
24055
+ /* 156 */ "Init" OpHelp("Start at P2"),
24056
+ /* 157 */ "Noop" OpHelp(""),
24057
+ /* 158 */ "Explain" OpHelp(""),
2371524058
};
2371624059
return azName[i];
2371724060
}
2371824061
#endif
2371924062
@@ -23812,15 +24155,14 @@
2381224155
#include <unistd.h>
2381324156
/* #include <time.h> */
2381424157
#include <sys/time.h>
2381524158
#include <errno.h>
2381624159
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23817
-#include <sys/mman.h>
24160
+# include <sys/mman.h>
2381824161
#endif
2381924162
23820
-
23821
-#if SQLITE_ENABLE_LOCKING_STYLE
24163
+#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
2382224164
# include <sys/ioctl.h>
2382324165
# if OS_VXWORKS
2382424166
# include <semaphore.h>
2382524167
# include <limits.h>
2382624168
# else
@@ -24244,11 +24586,15 @@
2424424586
** On some systems, calls to fchown() will trigger a message in a security
2424524587
** log if they come from non-root processes. So avoid calling fchown() if
2424624588
** we are not running as root.
2424724589
*/
2424824590
static int posixFchown(int fd, uid_t uid, gid_t gid){
24591
+#if OS_VXWORKS
24592
+ return 0;
24593
+#else
2424924594
return geteuid() ? 0 : fchown(fd,uid,gid);
24595
+#endif
2425024596
}
2425124597
2425224598
/* Forward reference */
2425324599
static int openDirectory(const char*, int*);
2425424600
static int unixGetpagesize(void);
@@ -24300,11 +24646,11 @@
2430024646
#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
2430124647
2430224648
{ "read", (sqlite3_syscall_ptr)read, 0 },
2430324649
#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
2430424650
24305
-#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24651
+#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
2430624652
{ "pread", (sqlite3_syscall_ptr)pread, 0 },
2430724653
#else
2430824654
{ "pread", (sqlite3_syscall_ptr)0, 0 },
2430924655
#endif
2431024656
#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
@@ -24317,11 +24663,11 @@
2431724663
#define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
2431824664
2431924665
{ "write", (sqlite3_syscall_ptr)write, 0 },
2432024666
#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
2432124667
24322
-#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24668
+#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
2432324669
{ "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
2432424670
#else
2432524671
{ "pwrite", (sqlite3_syscall_ptr)0, 0 },
2432624672
#endif
2432724673
#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
@@ -24371,14 +24717,14 @@
2437124717
{ "mremap", (sqlite3_syscall_ptr)mremap, 0 },
2437224718
#else
2437324719
{ "mremap", (sqlite3_syscall_ptr)0, 0 },
2437424720
#endif
2437524721
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
24376
-#endif
24377
-
2437824722
{ "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
2437924723
#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
24724
+
24725
+#endif
2438024726
2438124727
}; /* End of the overrideable system calls */
2438224728
2438324729
/*
2438424730
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -24687,20 +25033,10 @@
2468725033
}
2468825034
/* else fall through */
2468925035
case EPERM:
2469025036
return SQLITE_PERM;
2469125037
24692
- /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24693
- ** this module never makes such a call. And the code in SQLite itself
24694
- ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24695
- ** this case is also commented out. If the system does set errno to EDEADLK,
24696
- ** the default SQLITE_IOERR_XXX code will be returned. */
24697
-#if 0
24698
- case EDEADLK:
24699
- return SQLITE_IOERR_BLOCKED;
24700
-#endif
24701
-
2470225038
#if EOPNOTSUPP!=ENOTSUP
2470325039
case EOPNOTSUPP:
2470425040
/* something went terribly awry, unless during file system support
2470525041
* introspection, in which it actually means what it says */
2470625042
#endif
@@ -25229,13 +25565,17 @@
2522925565
2523025566
/*
2523125567
** Return TRUE if pFile has been renamed or unlinked since it was first opened.
2523225568
*/
2523325569
static int fileHasMoved(unixFile *pFile){
25570
+#if OS_VXWORKS
25571
+ return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
25572
+#else
2523425573
struct stat buf;
2523525574
return pFile->pInode!=0 &&
25236
- (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
25575
+ (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
25576
+#endif
2523725577
}
2523825578
2523925579
2524025580
/*
2524125581
** Check a unixFile that is a database. Verify the following:
@@ -25844,10 +26184,17 @@
2584426184
osUnlink(pFile->pId->zCanonicalName);
2584526185
}
2584626186
vxworksReleaseFileId(pFile->pId);
2584726187
pFile->pId = 0;
2584826188
}
26189
+#endif
26190
+#ifdef SQLITE_UNLINK_AFTER_CLOSE
26191
+ if( pFile->ctrlFlags & UNIXFILE_DELETE ){
26192
+ osUnlink(pFile->zPath);
26193
+ sqlite3_free(*(char**)&pFile->zPath);
26194
+ pFile->zPath = 0;
26195
+ }
2584926196
#endif
2585026197
OSTRACE(("CLOSE %-3d\n", pFile->h));
2585126198
OpenCounter(-1);
2585226199
sqlite3_free(pFile->pUnused);
2585326200
memset(pFile, 0, sizeof(unixFile));
@@ -26367,11 +26714,10 @@
2636726714
}
2636826715
2636926716
/* Otherwise see if some other process holds it. */
2637026717
if( !reserved ){
2637126718
sem_t *pSem = pFile->pInode->pSem;
26372
- struct stat statBuf;
2637326719
2637426720
if( sem_trywait(pSem)==-1 ){
2637526721
int tErrno = errno;
2637626722
if( EAGAIN != tErrno ){
2637726723
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
@@ -26420,11 +26766,10 @@
2642026766
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2642126767
** routine to lower a locking level.
2642226768
*/
2642326769
static int semLock(sqlite3_file *id, int eFileLock) {
2642426770
unixFile *pFile = (unixFile*)id;
26425
- int fd;
2642626771
sem_t *pSem = pFile->pInode->pSem;
2642726772
int rc = SQLITE_OK;
2642826773
2642926774
/* if we already have a lock, it is exclusive.
2643026775
** Just adjust level and punt on outta here. */
@@ -27883,12 +28228,29 @@
2788328228
rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
2788428229
}
2788528230
return rc;
2788628231
}
2788728232
28233
+#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
28234
+
28235
+/*
28236
+** Return the system page size.
28237
+**
28238
+** This function should not be called directly by other code in this file.
28239
+** Instead, it should be called via macro osGetpagesize().
28240
+*/
28241
+static int unixGetpagesize(void){
28242
+#if defined(_BSD_SOURCE)
28243
+ return getpagesize();
28244
+#else
28245
+ return (int)sysconf(_SC_PAGESIZE);
28246
+#endif
28247
+}
28248
+
28249
+#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
28250
+
2788828251
#ifndef SQLITE_OMIT_WAL
27889
-
2789028252
2789128253
/*
2789228254
** Object used to represent an shared memory buffer.
2789328255
**
2789428256
** When multiple threads all reference the same wal-index, each thread
@@ -28035,24 +28397,10 @@
2803528397
#endif
2803628398
2803728399
return rc;
2803828400
}
2803928401
28040
-/*
28041
-** Return the system page size.
28042
-**
28043
-** This function should not be called directly by other code in this file.
28044
-** Instead, it should be called via macro osGetpagesize().
28045
-*/
28046
-static int unixGetpagesize(void){
28047
-#if defined(_BSD_SOURCE)
28048
- return getpagesize();
28049
-#else
28050
- return (int)sysconf(_SC_PAGESIZE);
28051
-#endif
28052
-}
28053
-
2805428402
/*
2805528403
** Return the minimum number of 32KB shm regions that should be mapped at
2805628404
** a time, assuming that each mapping must be an integer multiple of the
2805728405
** current system page-size.
2805828406
**
@@ -29698,10 +30046,16 @@
2969830046
}
2969930047
2970030048
if( isDelete ){
2970130049
#if OS_VXWORKS
2970230050
zPath = zName;
30051
+#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
30052
+ zPath = sqlite3_mprintf("%s", zName);
30053
+ if( zPath==0 ){
30054
+ robust_close(p, fd, __LINE__);
30055
+ return SQLITE_NOMEM;
30056
+ }
2970330057
#else
2970430058
osUnlink(zName);
2970530059
#endif
2970630060
}
2970730061
#if SQLITE_ENABLE_LOCKING_STYLE
@@ -29798,11 +30152,15 @@
2979830152
){
2979930153
int rc = SQLITE_OK;
2980030154
UNUSED_PARAMETER(NotUsed);
2980130155
SimulateIOError(return SQLITE_IOERR_DELETE);
2980230156
if( osUnlink(zPath)==(-1) ){
29803
- if( errno==ENOENT ){
30157
+ if( errno==ENOENT
30158
+#if OS_VXWORKS
30159
+ || errno==0x380003
30160
+#endif
30161
+ ){
2980430162
rc = SQLITE_IOERR_DELETE_NOENT;
2980530163
}else{
2980630164
rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
2980730165
}
2980830166
return rc;
@@ -31695,22 +32053,18 @@
3169532053
#ifndef NTDDI_WINBLUE
3169632054
# define NTDDI_WINBLUE 0x06030000
3169732055
#endif
3169832056
3169932057
/*
31700
-** Check if the GetVersionEx[AW] functions should be considered deprecated
31701
-** and avoid using them in that case. It should be noted here that if the
31702
-** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
31703
-** (whether via this block or via being manually specified), that implies
31704
-** the underlying operating system will always be based on the Windows NT
31705
-** Kernel.
32058
+** Check to see if the GetVersionEx[AW] functions are deprecated on the
32059
+** target system. GetVersionEx was first deprecated in Win8.1.
3170632060
*/
3170732061
#ifndef SQLITE_WIN32_GETVERSIONEX
3170832062
# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
31709
-# define SQLITE_WIN32_GETVERSIONEX 0
32063
+# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
3171032064
# else
31711
-# define SQLITE_WIN32_GETVERSIONEX 1
32065
+# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
3171232066
# endif
3171332067
#endif
3171432068
3171532069
/*
3171632070
** This constant should already be defined (in the "WinDef.h" SDK file).
@@ -31778,11 +32132,11 @@
3177832132
/*
3177932133
** This macro is used when a local variable is set to a value that is
3178032134
** [sometimes] not used by the code (e.g. via conditional compilation).
3178132135
*/
3178232136
#ifndef UNUSED_VARIABLE_VALUE
31783
-# define UNUSED_VARIABLE_VALUE(x) (void)(x)
32137
+# define UNUSED_VARIABLE_VALUE(x) (void)(x)
3178432138
#endif
3178532139
3178632140
/*
3178732141
** Returns the character that should be used as the directory separator.
3178832142
*/
@@ -31827,11 +32181,11 @@
3182732181
3182832182
/*
3182932183
** Some Microsoft compilers lack this definition.
3183032184
*/
3183132185
#ifndef INVALID_FILE_ATTRIBUTES
31832
-# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
32186
+# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
3183332187
#endif
3183432188
3183532189
#ifndef FILE_FLAG_MASK
3183632190
# define FILE_FLAG_MASK (0xFF3C0000)
3183732191
#endif
@@ -31877,11 +32231,11 @@
3187732231
#endif
3187832232
const char *zPath; /* Full pathname of this file */
3187932233
int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
3188032234
#if SQLITE_OS_WINCE
3188132235
LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
31882
- HANDLE hMutex; /* Mutex used to control access to shared lock */
32236
+ HANDLE hMutex; /* Mutex used to control access to shared lock */
3188332237
HANDLE hShared; /* Shared memory segment used for locking */
3188432238
winceLock local; /* Locks obtained by this instance of winFile */
3188532239
winceLock *shared; /* Global shared lock memory for the file */
3188632240
#endif
3188732241
#if SQLITE_MAX_MMAP_SIZE>0
@@ -32037,14 +32391,13 @@
3203732391
**
3203832392
** In order to facilitate testing on a WinNT system, the test fixture
3203932393
** can manually set this value to 1 to emulate Win98 behavior.
3204032394
*/
3204132395
#ifdef SQLITE_TEST
32042
-SQLITE_API int sqlite3_os_type = 0;
32043
-#elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
32044
- defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
32045
-static int sqlite3_os_type = 0;
32396
+SQLITE_API LONG volatile sqlite3_os_type = 0;
32397
+#else
32398
+static LONG volatile sqlite3_os_type = 0;
3204632399
#endif
3204732400
3204832401
#ifndef SYSCALL
3204932402
# define SYSCALL sqlite3_syscall_ptr
3205032403
#endif
@@ -32671,10 +33024,26 @@
3267133024
#endif
3267233025
3267333026
#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
3267433027
LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
3267533028
33029
+/*
33030
+** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
33031
+** is really just a macro that uses a compiler intrinsic (e.g. x64).
33032
+** So do not try to make this is into a redefinable interface.
33033
+*/
33034
+#if defined(InterlockedCompareExchange)
33035
+ { "InterlockedCompareExchange", (SYSCALL)0, 0 },
33036
+
33037
+#define osInterlockedCompareExchange InterlockedCompareExchange
33038
+#else
33039
+ { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
33040
+
33041
+#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
33042
+ LONG,LONG))aSyscall[76].pCurrent)
33043
+#endif /* defined(InterlockedCompareExchange) */
33044
+
3267633045
}; /* End of the overrideable system calls */
3267733046
3267833047
/*
3267933048
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
3268033049
** "win32" VFSes. Return SQLITE_OK opon successfully updating the
@@ -32921,26 +33290,42 @@
3292133290
#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
3292233291
# define osIsNT() (1)
3292333292
#elif !defined(SQLITE_WIN32_HAS_WIDE)
3292433293
# define osIsNT() (0)
3292533294
#else
32926
- static int osIsNT(void){
32927
- if( sqlite3_os_type==0 ){
32928
-#if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
32929
- OSVERSIONINFOW sInfo;
32930
- sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32931
- osGetVersionExW(&sInfo);
33295
+# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
33296
+#endif
33297
+
33298
+/*
33299
+** This function determines if the machine is running a version of Windows
33300
+** based on the NT kernel.
33301
+*/
33302
+SQLITE_API int sqlite3_win32_is_nt(void){
33303
+#if defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
33304
+ if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
33305
+#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
33306
+ defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
33307
+ OSVERSIONINFOW sInfo;
33308
+ sInfo.dwOSVersionInfoSize = sizeof(sInfo);
33309
+ osGetVersionExW(&sInfo);
33310
+ osInterlockedCompareExchange(&sqlite3_os_type,
33311
+ (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
33312
+#elif defined(SQLITE_WIN32_HAS_ANSI)
33313
+ OSVERSIONINFOA sInfo;
33314
+ sInfo.dwOSVersionInfoSize = sizeof(sInfo);
33315
+ osGetVersionExA(&sInfo);
33316
+ osInterlockedCompareExchange(&sqlite3_os_type,
33317
+ (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
33318
+#endif
33319
+ }
33320
+ return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
33321
+#elif SQLITE_TEST
33322
+ return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
3293233323
#else
32933
- OSVERSIONINFOA sInfo;
32934
- sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32935
- osGetVersionExA(&sInfo);
32936
-#endif
32937
- sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
32938
- }
32939
- return sqlite3_os_type==2;
32940
- }
32941
-#endif
33324
+ return 1;
33325
+#endif
33326
+}
3294233327
3294333328
#ifdef SQLITE_WIN32_MALLOC
3294433329
/*
3294533330
** Allocate nBytes of memory.
3294633331
*/
@@ -33144,11 +33529,11 @@
3314433529
sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
3314533530
}
3314633531
#endif /* SQLITE_WIN32_MALLOC */
3314733532
3314833533
/*
33149
-** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
33534
+** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
3315033535
**
3315133536
** Space to hold the returned string is obtained from malloc.
3315233537
*/
3315333538
static LPWSTR winUtf8ToUnicode(const char *zFilename){
3315433539
int nChar;
@@ -33197,11 +33582,11 @@
3319733582
}
3319833583
3319933584
/*
3320033585
** Convert an ANSI string to Microsoft Unicode, based on the
3320133586
** current codepage settings for file apis.
33202
-**
33587
+**
3320333588
** Space to hold the returned string is obtained
3320433589
** from sqlite3_malloc.
3320533590
*/
3320633591
static LPWSTR winMbcsToUnicode(const char *zFilename){
3320733592
int nByte;
@@ -33271,11 +33656,11 @@
3327133656
sqlite3_free(zTmpWide);
3327233657
return zFilenameUtf8;
3327333658
}
3327433659
3327533660
/*
33276
-** Convert UTF-8 to multibyte character string. Space to hold the
33661
+** Convert UTF-8 to multibyte character string. Space to hold the
3327733662
** returned string is obtained from sqlite3_malloc().
3327833663
*/
3327933664
SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
3328033665
char *zFilenameMbcs;
3328133666
LPWSTR zTmpWide;
@@ -33411,15 +33796,15 @@
3341133796
** This function - winLogErrorAtLine() - is only ever called via the macro
3341233797
** winLogError().
3341333798
**
3341433799
** This routine is invoked after an error occurs in an OS function.
3341533800
** It logs a message using sqlite3_log() containing the current value of
33416
-** error code and, if possible, the human-readable equivalent from
33801
+** error code and, if possible, the human-readable equivalent from
3341733802
** FormatMessage.
3341833803
**
3341933804
** The first argument passed to the macro should be the error code that
33420
-** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33805
+** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
3342133806
** The two subsequent arguments should be the name of the OS function that
3342233807
** failed and the associated file-system path, if any.
3342333808
*/
3342433809
#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
3342533810
static int winLogErrorAtLine(
@@ -33446,11 +33831,11 @@
3344633831
return errcode;
3344733832
}
3344833833
3344933834
/*
3345033835
** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
33451
-** will be retried following a locking error - probably caused by
33836
+** will be retried following a locking error - probably caused by
3345233837
** antivirus software. Also the initial delay before the first retry.
3345333838
** The delay increases linearly with each retry.
3345433839
*/
3345533840
#ifndef SQLITE_WIN32_IOERR_RETRY
3345633841
# define SQLITE_WIN32_IOERR_RETRY 10
@@ -33521,11 +33906,11 @@
3352133906
/*
3352233907
** Log a I/O error retry episode.
3352333908
*/
3352433909
static void winLogIoerr(int nRetry){
3352533910
if( nRetry ){
33526
- sqlite3_log(SQLITE_IOERR,
33911
+ sqlite3_log(SQLITE_IOERR,
3352733912
"delayed %dms for lock/sharing conflict",
3352833913
winIoerrRetryDelay*nRetry*(nRetry+1)/2
3352933914
);
3353033915
}
3353133916
}
@@ -33615,21 +34000,21 @@
3361534000
"winceCreateLock1", zFilename);
3361634001
}
3361734002
3361834003
/* Acquire the mutex before continuing */
3361934004
winceMutexAcquire(pFile->hMutex);
33620
-
33621
- /* Since the names of named mutexes, semaphores, file mappings etc are
34005
+
34006
+ /* Since the names of named mutexes, semaphores, file mappings etc are
3362234007
** case-sensitive, take advantage of that by uppercasing the mutex name
3362334008
** and using that as the shared filemapping name.
3362434009
*/
3362534010
osCharUpperW(zName);
3362634011
pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
3362734012
PAGE_READWRITE, 0, sizeof(winceLock),
33628
- zName);
34013
+ zName);
3362934014
33630
- /* Set a flag that indicates we're the first to create the memory so it
34015
+ /* Set a flag that indicates we're the first to create the memory so it
3363134016
** must be zero-initialized */
3363234017
lastErrno = osGetLastError();
3363334018
if (lastErrno == ERROR_ALREADY_EXISTS){
3363434019
bInit = FALSE;
3363534020
}
@@ -33636,11 +34021,11 @@
3363634021
3363734022
sqlite3_free(zName);
3363834023
3363934024
/* If we succeeded in making the shared memory handle, map it. */
3364034025
if( pFile->hShared ){
33641
- pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
34026
+ pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
3364234027
FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
3364334028
/* If mapping failed, close the shared memory handle and erase it */
3364434029
if( !pFile->shared ){
3364534030
pFile->lastErrno = osGetLastError();
3364634031
winLogError(SQLITE_IOERR, pFile->lastErrno,
@@ -33662,11 +34047,11 @@
3366234047
winceMutexRelease(pFile->hMutex);
3366334048
osCloseHandle(pFile->hMutex);
3366434049
pFile->hMutex = NULL;
3366534050
return SQLITE_IOERR;
3366634051
}
33667
-
34052
+
3366834053
/* Initialize the shared memory if we're supposed to */
3366934054
if( bInit ){
3367034055
memset(pFile->shared, 0, sizeof(winceLock));
3367134056
}
3367234057
@@ -33700,17 +34085,17 @@
3370034085
/* De-reference and close our copy of the shared memory handle */
3370134086
osUnmapViewOfFile(pFile->shared);
3370234087
osCloseHandle(pFile->hShared);
3370334088
3370434089
/* Done with the mutex */
33705
- winceMutexRelease(pFile->hMutex);
34090
+ winceMutexRelease(pFile->hMutex);
3370634091
osCloseHandle(pFile->hMutex);
3370734092
pFile->hMutex = NULL;
3370834093
}
3370934094
}
3371034095
33711
-/*
34096
+/*
3371234097
** An implementation of the LockFile() API of Windows for CE
3371334098
*/
3371434099
static BOOL winceLockFile(
3371534100
LPHANDLE phFile,
3371634101
DWORD dwFileOffsetLow,
@@ -33917,12 +34302,12 @@
3391734302
#ifndef INVALID_SET_FILE_POINTER
3391834303
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
3391934304
#endif
3392034305
3392134306
/*
33922
-** Move the current position of the file handle passed as the first
33923
-** argument to offset iOffset within the file. If successful, return 0.
34307
+** Move the current position of the file handle passed as the first
34308
+** argument to offset iOffset within the file. If successful, return 0.
3392434309
** Otherwise, set pFile->lastErrno and return non-zero.
3392534310
*/
3392634311
static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
3392734312
#if !SQLITE_OS_WINRT
3392834313
LONG upperBits; /* Most sig. 32 bits of new offset */
@@ -33933,15 +34318,15 @@
3393334318
OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
3393434319
3393534320
upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
3393634321
lowerBits = (LONG)(iOffset & 0xffffffff);
3393734322
33938
- /* API oddity: If successful, SetFilePointer() returns a dword
34323
+ /* API oddity: If successful, SetFilePointer() returns a dword
3393934324
** containing the lower 32-bits of the new file-offset. Or, if it fails,
33940
- ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
33941
- ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
33942
- ** whether an error has actually occurred, it is also necessary to call
34325
+ ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
34326
+ ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
34327
+ ** whether an error has actually occurred, it is also necessary to call
3394334328
** GetLastError().
3394434329
*/
3394534330
dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
3394634331
3394734332
if( (dwRet==INVALID_SET_FILE_POINTER
@@ -34020,11 +34405,11 @@
3402034405
winceDestroyLock(pFile);
3402134406
if( pFile->zDeleteOnClose ){
3402234407
int cnt = 0;
3402334408
while(
3402434409
osDeleteFileW(pFile->zDeleteOnClose)==0
34025
- && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
34410
+ && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
3402634411
&& cnt++ < WINCE_DELETION_ATTEMPTS
3402734412
){
3402834413
sqlite3_win32_sleep(100); /* Wait a little before trying again */
3402934414
}
3403034415
sqlite3_free(pFile->zDeleteOnClose);
@@ -34868,11 +35253,11 @@
3486835253
winFile *p = (winFile*)id;
3486935254
return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
3487035255
((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
3487135256
}
3487235257
34873
-/*
35258
+/*
3487435259
** Windows will only let you create file view mappings
3487535260
** on allocation size granularity boundaries.
3487635261
** During sqlite3_os_init() we do a GetSystemInfo()
3487735262
** to get the granularity size.
3487835263
*/
@@ -34880,15 +35265,15 @@
3488035265
3488135266
#ifndef SQLITE_OMIT_WAL
3488235267
3488335268
/*
3488435269
** Helper functions to obtain and relinquish the global mutex. The
34885
-** global mutex is used to protect the winLockInfo objects used by
35270
+** global mutex is used to protect the winLockInfo objects used by
3488635271
** this file, all of which may be shared by multiple threads.
3488735272
**
34888
-** Function winShmMutexHeld() is used to assert() that the global mutex
34889
-** is held when required. This function is only used as part of assert()
35273
+** Function winShmMutexHeld() is used to assert() that the global mutex
35274
+** is held when required. This function is only used as part of assert()
3489035275
** statements. e.g.
3489135276
**
3489235277
** winShmEnterMutex()
3489335278
** assert( winShmMutexHeld() );
3489435279
** winShmLeaveMutex()
@@ -34914,14 +35299,14 @@
3491435299
**
3491535300
** winShmMutexHeld() must be true when creating or destroying
3491635301
** this object or while reading or writing the following fields:
3491735302
**
3491835303
** nRef
34919
-** pNext
35304
+** pNext
3492035305
**
3492135306
** The following fields are read-only after the object is created:
34922
-**
35307
+**
3492335308
** fid
3492435309
** zFilename
3492535310
**
3492635311
** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
3492735312
** winShmMutexHeld() is true when reading or writing any other field
@@ -35013,11 +35398,11 @@
3501335398
/* Initialize the locking parameters */
3501435399
DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
3501535400
if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
3501635401
rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
3501735402
}
35018
-
35403
+
3501935404
if( rc!= 0 ){
3502035405
rc = SQLITE_OK;
3502135406
}else{
3502235407
pFile->lastErrno = osGetLastError();
3502335408
rc = SQLITE_BUSY;
@@ -35109,11 +35494,11 @@
3510935494
sqlite3_free(p);
3511035495
return SQLITE_IOERR_NOMEM;
3511135496
}
3511235497
pNew->zFilename = (char*)&pNew[1];
3511335498
sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
35114
- sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
35499
+ sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
3511535500
3511635501
/* Look to see if there is an existing winShmNode that can be used.
3511735502
** If no matching winShmNode currently exists, create a new one.
3511835503
*/
3511935504
winShmEnterMutex();
@@ -35146,11 +35531,11 @@
3514635531
if( SQLITE_OK!=rc ){
3514735532
goto shm_open_err;
3514835533
}
3514935534
3515035535
/* Check to see if another process is holding the dead-man switch.
35151
- ** If not, truncate the file to zero length.
35536
+ ** If not, truncate the file to zero length.
3515235537
*/
3515335538
if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
3515435539
rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
3515535540
if( rc!=SQLITE_OK ){
3515635541
rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
@@ -35175,11 +35560,11 @@
3517535560
3517635561
/* The reference count on pShmNode has already been incremented under
3517735562
** the cover of the winShmEnterMutex() mutex and the pointer from the
3517835563
** new (struct winShm) object to the pShmNode has been set. All that is
3517935564
** left to do is to link the new object into the linked list starting
35180
- ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
35565
+ ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
3518135566
** mutex.
3518235567
*/
3518335568
sqlite3_mutex_enter(pShmNode->mutex);
3518435569
p->pNext = pShmNode->pFirst;
3518535570
pShmNode->pFirst = p;
@@ -35195,11 +35580,11 @@
3519535580
winShmLeaveMutex();
3519635581
return rc;
3519735582
}
3519835583
3519935584
/*
35200
-** Close a connection to shared-memory. Delete the underlying
35585
+** Close a connection to shared-memory. Delete the underlying
3520135586
** storage if deleteFlag is true.
3520235587
*/
3520335588
static int winShmUnmap(
3520435589
sqlite3_file *fd, /* Database holding shared memory */
3520535590
int deleteFlag /* Delete after closing if true */
@@ -35284,11 +35669,11 @@
3528435669
3528535670
/* Undo the local locks */
3528635671
if( rc==SQLITE_OK ){
3528735672
p->exclMask &= ~mask;
3528835673
p->sharedMask &= ~mask;
35289
- }
35674
+ }
3529035675
}else if( flags & SQLITE_SHM_SHARED ){
3529135676
u16 allShared = 0; /* Union of locks held by connections other than "p" */
3529235677
3529335678
/* Find out which shared locks are already held by sibling connections.
3529435679
** If any sibling already holds an exclusive lock, go ahead and return
@@ -35323,11 +35708,11 @@
3532335708
if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
3532435709
rc = SQLITE_BUSY;
3532535710
break;
3532635711
}
3532735712
}
35328
-
35713
+
3532935714
/* Get the exclusive locks at the system level. Then if successful
3533035715
** also mark the local connection as being locked.
3533135716
*/
3533235717
if( rc==SQLITE_OK ){
3533335718
rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
@@ -35343,11 +35728,11 @@
3534335728
sqlite3ErrName(rc)));
3534435729
return rc;
3534535730
}
3534635731
3534735732
/*
35348
-** Implement a memory barrier or memory fence on shared memory.
35733
+** Implement a memory barrier or memory fence on shared memory.
3534935734
**
3535035735
** All loads and stores begun before the barrier must complete before
3535135736
** any load or store begun after the barrier.
3535235737
*/
3535335738
static void winShmBarrier(
@@ -35358,26 +35743,26 @@
3535835743
winShmEnterMutex();
3535935744
winShmLeaveMutex();
3536035745
}
3536135746
3536235747
/*
35363
-** This function is called to obtain a pointer to region iRegion of the
35364
-** shared-memory associated with the database file fd. Shared-memory regions
35365
-** are numbered starting from zero. Each shared-memory region is szRegion
35748
+** This function is called to obtain a pointer to region iRegion of the
35749
+** shared-memory associated with the database file fd. Shared-memory regions
35750
+** are numbered starting from zero. Each shared-memory region is szRegion
3536635751
** bytes in size.
3536735752
**
3536835753
** If an error occurs, an error code is returned and *pp is set to NULL.
3536935754
**
3537035755
** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
3537135756
** region has not been allocated (by any client, including one running in a
35372
-** separate process), then *pp is set to NULL and SQLITE_OK returned. If
35373
-** isWrite is non-zero and the requested shared-memory region has not yet
35757
+** separate process), then *pp is set to NULL and SQLITE_OK returned. If
35758
+** isWrite is non-zero and the requested shared-memory region has not yet
3537435759
** been allocated, it is allocated by this function.
3537535760
**
3537635761
** If the shared-memory region has already been allocated or is allocated by
35377
-** this call as described above, then it is mapped into this processes
35378
-** address space (if it is not already), *pp is set to point to the mapped
35762
+** this call as described above, then it is mapped into this processes
35763
+** address space (if it is not already), *pp is set to point to the mapped
3537935764
** memory and SQLITE_OK returned.
3538035765
*/
3538135766
static int winShmMap(
3538235767
sqlite3_file *fd, /* Handle open on database file */
3538335768
int iRegion, /* Region to retrieve */
@@ -35445,21 +35830,21 @@
3544535830
pShmNode->aRegion = apNew;
3544635831
3544735832
while( pShmNode->nRegion<=iRegion ){
3544835833
HANDLE hMap = NULL; /* file-mapping handle */
3544935834
void *pMap = 0; /* Mapped memory region */
35450
-
35835
+
3545135836
#if SQLITE_OS_WINRT
3545235837
hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
3545335838
NULL, PAGE_READWRITE, nByte, NULL
3545435839
);
3545535840
#elif defined(SQLITE_WIN32_HAS_WIDE)
35456
- hMap = osCreateFileMappingW(pShmNode->hFile.h,
35841
+ hMap = osCreateFileMappingW(pShmNode->hFile.h,
3545735842
NULL, PAGE_READWRITE, 0, nByte, NULL
3545835843
);
3545935844
#elif defined(SQLITE_WIN32_HAS_ANSI)
35460
- hMap = osCreateFileMappingA(pShmNode->hFile.h,
35845
+ hMap = osCreateFileMappingA(pShmNode->hFile.h,
3546135846
NULL, PAGE_READWRITE, 0, nByte, NULL
3546235847
);
3546335848
#endif
3546435849
OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
3546535850
osGetCurrentProcessId(), pShmNode->nRegion, nByte,
@@ -35552,18 +35937,18 @@
3555235937
return SQLITE_OK;
3555335938
}
3555435939
3555535940
/*
3555635941
** Memory map or remap the file opened by file-descriptor pFd (if the file
35557
-** is already mapped, the existing mapping is replaced by the new). Or, if
35558
-** there already exists a mapping for this file, and there are still
35942
+** is already mapped, the existing mapping is replaced by the new). Or, if
35943
+** there already exists a mapping for this file, and there are still
3555935944
** outstanding xFetch() references to it, this function is a no-op.
3556035945
**
35561
-** If parameter nByte is non-negative, then it is the requested size of
35562
-** the mapping to create. Otherwise, if nByte is less than zero, then the
35946
+** If parameter nByte is non-negative, then it is the requested size of
35947
+** the mapping to create. Otherwise, if nByte is less than zero, then the
3556335948
** requested size is the size of the file on disk. The actual size of the
35564
-** created mapping is either the requested size or the value configured
35949
+** created mapping is either the requested size or the value configured
3556535950
** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
3556635951
**
3556735952
** SQLITE_OK is returned if no error occurs (even if the mapping is not
3556835953
** recreated as a result of outstanding references) or an SQLite error
3556935954
** code otherwise.
@@ -35588,11 +35973,11 @@
3558835973
}
3558935974
if( nMap>pFd->mmapSizeMax ){
3559035975
nMap = pFd->mmapSizeMax;
3559135976
}
3559235977
nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
35593
-
35978
+
3559435979
if( nMap==0 && pFd->mmapSize>0 ){
3559535980
winUnmapfile(pFd);
3559635981
}
3559735982
if( nMap!=pFd->mmapSize ){
3559835983
void *pNew = 0;
@@ -35660,11 +36045,11 @@
3566036045
** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
3566136046
** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
3566236047
** Finally, if an error does occur, return an SQLite error code. The final
3566336048
** value of *pp is undefined in this case.
3566436049
**
35665
-** If this function does return a pointer, the caller must eventually
36050
+** If this function does return a pointer, the caller must eventually
3566636051
** release the reference by calling winUnfetch().
3566736052
*/
3566836053
static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
3566936054
#if SQLITE_MAX_MMAP_SIZE>0
3567036055
winFile *pFd = (winFile*)fd; /* The underlying database file */
@@ -35695,24 +36080,24 @@
3569536080
osGetCurrentProcessId(), fd, pp, *pp));
3569636081
return SQLITE_OK;
3569736082
}
3569836083
3569936084
/*
35700
-** If the third argument is non-NULL, then this function releases a
36085
+** If the third argument is non-NULL, then this function releases a
3570136086
** reference obtained by an earlier call to winFetch(). The second
3570236087
** argument passed to this function must be the same as the corresponding
35703
-** argument that was passed to the winFetch() invocation.
36088
+** argument that was passed to the winFetch() invocation.
3570436089
**
35705
-** Or, if the third argument is NULL, then this function is being called
35706
-** to inform the VFS layer that, according to POSIX, any existing mapping
36090
+** Or, if the third argument is NULL, then this function is being called
36091
+** to inform the VFS layer that, according to POSIX, any existing mapping
3570736092
** may now be invalid and should be unmapped.
3570836093
*/
3570936094
static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
3571036095
#if SQLITE_MAX_MMAP_SIZE>0
3571136096
winFile *pFd = (winFile*)fd; /* The underlying database file */
3571236097
35713
- /* If p==0 (unmap the entire file) then there must be no outstanding
36098
+ /* If p==0 (unmap the entire file) then there must be no outstanding
3571436099
** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
3571536100
** then there must be at least one outstanding. */
3571636101
assert( (p==0)==(pFd->nFetchOut==0) );
3571736102
3571836103
/* If p!=0, it must match the iOff value. */
@@ -35854,11 +36239,11 @@
3585436239
int nMax, nBuf, nDir, nLen;
3585536240
char *zBuf;
3585636241
3585736242
/* It's odd to simulate an io-error here, but really this is just
3585836243
** using the io-error infrastructure to test that SQLite handles this
35859
- ** function failing.
36244
+ ** function failing.
3586036245
*/
3586136246
SimulateIOError( return SQLITE_IOERR );
3586236247
3586336248
/* Allocate a temporary buffer to store the fully qualified file
3586436249
** name for the temporary file. If this fails, we cannot continue.
@@ -36036,11 +36421,11 @@
3603636421
OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
3603736422
return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
3603836423
}
3603936424
3604036425
/*
36041
- ** Check that the output buffer is large enough for the temporary file
36426
+ ** Check that the output buffer is large enough for the temporary file
3604236427
** name in the following format:
3604336428
**
3604436429
** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
3604536430
**
3604636431
** If not, return SQLITE_ERROR. The number 17 is used here in order to
@@ -36139,42 +36524,42 @@
3613936524
int isReadonly = (flags & SQLITE_OPEN_READONLY);
3614036525
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3614136526
3614236527
#ifndef NDEBUG
3614336528
int isOpenJournal = (isCreate && (
36144
- eType==SQLITE_OPEN_MASTER_JOURNAL
36145
- || eType==SQLITE_OPEN_MAIN_JOURNAL
36529
+ eType==SQLITE_OPEN_MASTER_JOURNAL
36530
+ || eType==SQLITE_OPEN_MAIN_JOURNAL
3614636531
|| eType==SQLITE_OPEN_WAL
3614736532
));
3614836533
#endif
3614936534
3615036535
OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
3615136536
zUtf8Name, id, flags, pOutFlags));
3615236537
36153
- /* Check the following statements are true:
36538
+ /* Check the following statements are true:
3615436539
**
36155
- ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
36540
+ ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3615636541
** (b) if CREATE is set, then READWRITE must also be set, and
3615736542
** (c) if EXCLUSIVE is set, then CREATE must also be set.
3615836543
** (d) if DELETEONCLOSE is set, then CREATE must also be set.
3615936544
*/
3616036545
assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
3616136546
assert(isCreate==0 || isReadWrite);
3616236547
assert(isExclusive==0 || isCreate);
3616336548
assert(isDelete==0 || isCreate);
3616436549
36165
- /* The main DB, main journal, WAL file and master journal are never
36550
+ /* The main DB, main journal, WAL file and master journal are never
3616636551
** automatically deleted. Nor are they ever temporary files. */
3616736552
assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
3616836553
assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
3616936554
assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
3617036555
assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
3617136556
3617236557
/* Assert that the upper layer has set one of the "file-type" flags. */
36173
- assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
36174
- || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
36175
- || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
36558
+ assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
36559
+ || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
36560
+ || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
3617636561
|| eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
3617736562
);
3617836563
3617936564
assert( pFile!=0 );
3618036565
memset(pFile, 0, sizeof(winFile));
@@ -36185,12 +36570,12 @@
3618536570
sqlite3_log(SQLITE_ERROR,
3618636571
"sqlite3_temp_directory variable should be set for WinRT");
3618736572
}
3618836573
#endif
3618936574
36190
- /* If the second argument to this function is NULL, generate a
36191
- ** temporary file name to use
36575
+ /* If the second argument to this function is NULL, generate a
36576
+ ** temporary file name to use
3619236577
*/
3619336578
if( !zUtf8Name ){
3619436579
assert( isDelete && !isOpenJournal );
3619536580
rc = winGetTempname(pVfs, &zTmpname);
3619636581
if( rc!=SQLITE_OK ){
@@ -36226,12 +36611,12 @@
3622636611
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
3622736612
}else{
3622836613
dwDesiredAccess = GENERIC_READ;
3622936614
}
3623036615
36231
- /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
36232
- ** created. SQLite doesn't use it to indicate "exclusive access"
36616
+ /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
36617
+ ** created. SQLite doesn't use it to indicate "exclusive access"
3623336618
** as it is usually understood.
3623436619
*/
3623536620
if( isExclusive ){
3623636621
/* Creates a new file, only if it does not already exist. */
3623736622
/* If the file exists, it fails. */
@@ -36316,11 +36701,11 @@
3631636701
pFile->lastErrno = lastErrno;
3631736702
winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
3631836703
sqlite3_free(zConverted);
3631936704
sqlite3_free(zTmpname);
3632036705
if( isReadWrite && !isExclusive ){
36321
- return winOpen(pVfs, zName, id,
36706
+ return winOpen(pVfs, zName, id,
3632236707
((flags|SQLITE_OPEN_READONLY) &
3632336708
~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
3632436709
pOutFlags);
3632536710
}else{
3632636711
return SQLITE_CANTOPEN_BKPT;
@@ -36525,18 +36910,18 @@
3652536910
if( osIsNT() ){
3652636911
int cnt = 0;
3652736912
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3652836913
memset(&sAttrData, 0, sizeof(sAttrData));
3652936914
while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
36530
- GetFileExInfoStandard,
36915
+ GetFileExInfoStandard,
3653136916
&sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
3653236917
if( rc ){
3653336918
/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
3653436919
** as if it does not exist.
3653536920
*/
3653636921
if( flags==SQLITE_ACCESS_EXISTS
36537
- && sAttrData.nFileSizeHigh==0
36922
+ && sAttrData.nFileSizeHigh==0
3653836923
&& sAttrData.nFileSizeLow==0 ){
3653936924
attr = INVALID_FILE_ATTRIBUTES;
3654036925
}else{
3654136926
attr = sAttrData.dwFileAttributes;
3654236927
}
@@ -36631,11 +37016,11 @@
3663137016
sqlite3_vfs *pVfs, /* Pointer to vfs object */
3663237017
const char *zRelative, /* Possibly relative input path */
3663337018
int nFull, /* Size of output buffer in bytes */
3663437019
char *zFull /* Output buffer */
3663537020
){
36636
-
37021
+
3663737022
#if defined(__CYGWIN__)
3663837023
SimulateIOError( return SQLITE_ERROR );
3663937024
UNUSED_PARAMETER(nFull);
3664037025
assert( nFull>=pVfs->mxPathname );
3664137026
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
@@ -36944,24 +37329,24 @@
3694437329
** the current time and date as a Julian Day number times 86_400_000. In
3694537330
** other words, write into *piNow the number of milliseconds since the Julian
3694637331
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
3694737332
** proleptic Gregorian calendar.
3694837333
**
36949
-** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
37334
+** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
3695037335
** cannot be found.
3695137336
*/
3695237337
static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
36953
- /* FILETIME structure is a 64-bit value representing the number of
36954
- 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
37338
+ /* FILETIME structure is a 64-bit value representing the number of
37339
+ 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
3695537340
*/
3695637341
FILETIME ft;
3695737342
static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
3695837343
#ifdef SQLITE_TEST
3695937344
static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
3696037345
#endif
3696137346
/* 2^32 - to avoid use of LL and warnings in gcc */
36962
- static const sqlite3_int64 max32BitValue =
37347
+ static const sqlite3_int64 max32BitValue =
3696337348
(sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
3696437349
(sqlite3_int64)294967296;
3696537350
3696637351
#if SQLITE_OS_WINCE
3696737352
SYSTEMTIME time;
@@ -36973,11 +37358,11 @@
3697337358
#else
3697437359
osGetSystemTimeAsFileTime( &ft );
3697537360
#endif
3697637361
3697737362
*piNow = winFiletimeEpoch +
36978
- ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
37363
+ ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
3697937364
(sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
3698037365
3698137366
#ifdef SQLITE_TEST
3698237367
if( sqlite3_current_time ){
3698337368
*piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
@@ -37092,11 +37477,11 @@
3709237477
};
3709337478
#endif
3709437479
3709537480
/* Double-check that the aSyscall[] array has been constructed
3709637481
** correctly. See ticket [bb3a86e890c8e96ab] */
37097
- assert( ArraySize(aSyscall)==76 );
37482
+ assert( ArraySize(aSyscall)==77 );
3709837483
3709937484
/* get memory map allocation granularity */
3710037485
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
3710137486
#if SQLITE_OS_WINRT
3710237487
osGetNativeSystemInfo(&winSysInfo);
@@ -37110,14 +37495,14 @@
3711037495
3711137496
#if defined(SQLITE_WIN32_HAS_WIDE)
3711237497
sqlite3_vfs_register(&winLongPathVfs, 0);
3711337498
#endif
3711437499
37115
- return SQLITE_OK;
37500
+ return SQLITE_OK;
3711637501
}
3711737502
37118
-SQLITE_API int sqlite3_os_end(void){
37503
+SQLITE_API int sqlite3_os_end(void){
3711937504
#if SQLITE_OS_WINRT
3712037505
if( sleepObj!=NULL ){
3712137506
osCloseHandle(sleepObj);
3712237507
sleepObj = NULL;
3712337508
}
@@ -49189,20 +49574,20 @@
4918949574
**
4919049575
** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
4919149576
** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
4919249577
** is more of a scheduler yield than an actual delay. But on the 10th
4919349578
** an subsequent retries, the delays start becoming longer and longer,
49194
- ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
49195
- ** The total delay time before giving up is less than 1 second.
49579
+ ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
49580
+ ** The total delay time before giving up is less than 10 seconds.
4919649581
*/
4919749582
if( cnt>5 ){
4919849583
int nDelay = 1; /* Pause time in microseconds */
4919949584
if( cnt>100 ){
4920049585
VVA_ONLY( pWal->lockError = 1; )
4920149586
return SQLITE_PROTOCOL;
4920249587
}
49203
- if( cnt>=10 ) nDelay = (cnt-9)*238; /* Max delay 21ms. Total delay 996ms */
49588
+ if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
4920449589
sqlite3OsSleep(pWal->pVfs, nDelay);
4920549590
}
4920649591
4920749592
if( !useWal ){
4920849593
rc = walIndexReadHdr(pWal, pChanged);
@@ -51314,11 +51699,11 @@
5131451699
/* If the client is reading or writing an index and the schema is
5131551700
** not loaded, then it is too difficult to actually check to see if
5131651701
** the correct locks are held. So do not bother - just return true.
5131751702
** This case does not come up very often anyhow.
5131851703
*/
51319
- if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
51704
+ if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
5132051705
return 1;
5132151706
}
5132251707
5132351708
/* Figure out the root-page that the lock should be held on. For table
5132451709
** b-trees, this is just the root page of the b-tree being read or
@@ -52784,11 +53169,11 @@
5278453169
return pBt->nPage;
5278553170
}
5278653171
SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
5278753172
assert( sqlite3BtreeHoldsMutex(p) );
5278853173
assert( ((p->pBt->nPage)&0x8000000)==0 );
52789
- return (int)btreePagecount(p->pBt);
53174
+ return btreePagecount(p->pBt);
5279053175
}
5279153176
5279253177
/*
5279353178
** Get a page from the pager and initialize it. This routine is just a
5279453179
** convenience wrapper around separate calls to btreeGetPage() and
@@ -56893,11 +57278,12 @@
5689357278
int nSkip = (iChild ? 4 : 0);
5689457279
5689557280
if( *pRC ) return;
5689657281
5689757282
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
56898
- assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
57283
+ assert( MX_CELL(pPage->pBt)<=10921 );
57284
+ assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
5689957285
assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
5690057286
assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
5690157287
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5690257288
/* The cell should normally be sized correctly. However, when moving a
5690357289
** malformed cell from a leaf page to an interior page, if the cell size
@@ -61582,10 +61968,72 @@
6158261968
FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
6158361969
for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
6158461970
sqlite3FuncDefInsert(pHash, &aFunc[i]);
6158561971
}
6158661972
}
61973
+
61974
+/*
61975
+** Attempt to extract a value from pExpr and use it to construct *ppVal.
61976
+**
61977
+** If pAlloc is not NULL, then an UnpackedRecord object is created for
61978
+** pAlloc if one does not exist and the new value is added to the
61979
+** UnpackedRecord object.
61980
+**
61981
+** A value is extracted in the following cases:
61982
+**
61983
+** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
61984
+**
61985
+** * The expression is a bound variable, and this is a reprepare, or
61986
+**
61987
+** * The expression is a literal value.
61988
+**
61989
+** On success, *ppVal is made to point to the extracted value. The caller
61990
+** is responsible for ensuring that the value is eventually freed.
61991
+*/
61992
+static int stat4ValueFromExpr(
61993
+ Parse *pParse, /* Parse context */
61994
+ Expr *pExpr, /* The expression to extract a value from */
61995
+ u8 affinity, /* Affinity to use */
61996
+ struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
61997
+ sqlite3_value **ppVal /* OUT: New value object (or NULL) */
61998
+){
61999
+ int rc = SQLITE_OK;
62000
+ sqlite3_value *pVal = 0;
62001
+ sqlite3 *db = pParse->db;
62002
+
62003
+ /* Skip over any TK_COLLATE nodes */
62004
+ pExpr = sqlite3ExprSkipCollate(pExpr);
62005
+
62006
+ if( !pExpr ){
62007
+ pVal = valueNew(db, pAlloc);
62008
+ if( pVal ){
62009
+ sqlite3VdbeMemSetNull((Mem*)pVal);
62010
+ }
62011
+ }else if( pExpr->op==TK_VARIABLE
62012
+ || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
62013
+ ){
62014
+ Vdbe *v;
62015
+ int iBindVar = pExpr->iColumn;
62016
+ sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
62017
+ if( (v = pParse->pReprepare)!=0 ){
62018
+ pVal = valueNew(db, pAlloc);
62019
+ if( pVal ){
62020
+ rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
62021
+ if( rc==SQLITE_OK ){
62022
+ sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
62023
+ }
62024
+ pVal->db = pParse->db;
62025
+ }
62026
+ }
62027
+ }else{
62028
+ rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
62029
+ }
62030
+
62031
+ assert( pVal==0 || pVal->db==db );
62032
+ *ppVal = pVal;
62033
+ return rc;
62034
+}
6158762035
6158862036
/*
6158962037
** This function is used to allocate and populate UnpackedRecord
6159062038
** structures intended to be compared against sample index keys stored
6159162039
** in the sqlite_stat4 table.
@@ -61622,52 +62070,90 @@
6162262070
Expr *pExpr, /* The expression to extract a value from */
6162362071
u8 affinity, /* Affinity to use */
6162462072
int iVal, /* Array element to populate */
6162562073
int *pbOk /* OUT: True if value was extracted */
6162662074
){
61627
- int rc = SQLITE_OK;
62075
+ int rc;
6162862076
sqlite3_value *pVal = 0;
61629
- sqlite3 *db = pParse->db;
61630
-
61631
-
6163262077
struct ValueNewStat4Ctx alloc;
62078
+
6163362079
alloc.pParse = pParse;
6163462080
alloc.pIdx = pIdx;
6163562081
alloc.ppRec = ppRec;
6163662082
alloc.iVal = iVal;
6163762083
61638
- /* Skip over any TK_COLLATE nodes */
61639
- pExpr = sqlite3ExprSkipCollate(pExpr);
61640
-
61641
- if( !pExpr ){
61642
- pVal = valueNew(db, &alloc);
61643
- if( pVal ){
61644
- sqlite3VdbeMemSetNull((Mem*)pVal);
61645
- }
61646
- }else if( pExpr->op==TK_VARIABLE
61647
- || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
61648
- ){
61649
- Vdbe *v;
61650
- int iBindVar = pExpr->iColumn;
61651
- sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
61652
- if( (v = pParse->pReprepare)!=0 ){
61653
- pVal = valueNew(db, &alloc);
61654
- if( pVal ){
61655
- rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
61656
- if( rc==SQLITE_OK ){
61657
- sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
61658
- }
61659
- pVal->db = pParse->db;
61660
- }
61661
- }
61662
- }else{
61663
- rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
61664
- }
62084
+ rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
62085
+ assert( pVal==0 || pVal->db==pParse->db );
6166562086
*pbOk = (pVal!=0);
62087
+ return rc;
62088
+}
6166662089
61667
- assert( pVal==0 || pVal->db==db );
61668
- return rc;
62090
+/*
62091
+** Attempt to extract a value from expression pExpr using the methods
62092
+** as described for sqlite3Stat4ProbeSetValue() above.
62093
+**
62094
+** If successful, set *ppVal to point to a new value object and return
62095
+** SQLITE_OK. If no value can be extracted, but no other error occurs
62096
+** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
62097
+** does occur, return an SQLite error code. The final value of *ppVal
62098
+** is undefined in this case.
62099
+*/
62100
+SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
62101
+ Parse *pParse, /* Parse context */
62102
+ Expr *pExpr, /* The expression to extract a value from */
62103
+ u8 affinity, /* Affinity to use */
62104
+ sqlite3_value **ppVal /* OUT: New value object (or NULL) */
62105
+){
62106
+ return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
62107
+}
62108
+
62109
+/*
62110
+** Extract the iCol-th column from the nRec-byte record in pRec. Write
62111
+** the column value into *ppVal. If *ppVal is initially NULL then a new
62112
+** sqlite3_value object is allocated.
62113
+**
62114
+** If *ppVal is initially NULL then the caller is responsible for
62115
+** ensuring that the value written into *ppVal is eventually freed.
62116
+*/
62117
+SQLITE_PRIVATE int sqlite3Stat4Column(
62118
+ sqlite3 *db, /* Database handle */
62119
+ const void *pRec, /* Pointer to buffer containing record */
62120
+ int nRec, /* Size of buffer pRec in bytes */
62121
+ int iCol, /* Column to extract */
62122
+ sqlite3_value **ppVal /* OUT: Extracted value */
62123
+){
62124
+ u32 t; /* a column type code */
62125
+ int nHdr; /* Size of the header in the record */
62126
+ int iHdr; /* Next unread header byte */
62127
+ int iField; /* Next unread data byte */
62128
+ int szField; /* Size of the current data field */
62129
+ int i; /* Column index */
62130
+ u8 *a = (u8*)pRec; /* Typecast byte array */
62131
+ Mem *pMem = *ppVal; /* Write result into this Mem object */
62132
+
62133
+ assert( iCol>0 );
62134
+ iHdr = getVarint32(a, nHdr);
62135
+ if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
62136
+ iField = nHdr;
62137
+ for(i=0; i<=iCol; i++){
62138
+ iHdr += getVarint32(&a[iHdr], t);
62139
+ testcase( iHdr==nHdr );
62140
+ testcase( iHdr==nHdr+1 );
62141
+ if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
62142
+ szField = sqlite3VdbeSerialTypeLen(t);
62143
+ iField += szField;
62144
+ }
62145
+ testcase( iField==nRec );
62146
+ testcase( iField==nRec+1 );
62147
+ if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
62148
+ if( pMem==0 ){
62149
+ pMem = *ppVal = sqlite3ValueNew(db);
62150
+ if( pMem==0 ) return SQLITE_NOMEM;
62151
+ }
62152
+ sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
62153
+ pMem->enc = ENC(db);
62154
+ return SQLITE_OK;
6166962155
}
6167062156
6167162157
/*
6167262158
** Unless it is NULL, the argument must be an UnpackedRecord object returned
6167362159
** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
@@ -61810,22 +62296,39 @@
6181062296
pB->zSql = zTmp;
6181162297
pB->isPrepareV2 = pA->isPrepareV2;
6181262298
}
6181362299
6181462300
/*
61815
-** Resize the Vdbe.aOp array so that it is at least one op larger than
61816
-** it was.
62301
+** Resize the Vdbe.aOp array so that it is at least nOp elements larger
62302
+** than its current size. nOp is guaranteed to be less than or equal
62303
+** to 1024/sizeof(Op).
6181762304
**
6181862305
** If an out-of-memory error occurs while resizing the array, return
61819
-** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
62306
+** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
6182062307
** unchanged (this is so that any opcodes already allocated can be
6182162308
** correctly deallocated along with the rest of the Vdbe).
6182262309
*/
61823
-static int growOpArray(Vdbe *v){
62310
+static int growOpArray(Vdbe *v, int nOp){
6182462311
VdbeOp *pNew;
6182562312
Parse *p = v->pParse;
62313
+
62314
+ /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
62315
+ ** more frequent reallocs and hence provide more opportunities for
62316
+ ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
62317
+ ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
62318
+ ** by the minimum* amount required until the size reaches 512. Normal
62319
+ ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
62320
+ ** size of the op array or add 1KB of space, whichever is smaller. */
62321
+#ifdef SQLITE_TEST_REALLOC_STRESS
62322
+ int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
62323
+#else
6182662324
int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
62325
+ UNUSED_PARAMETER(nOp);
62326
+#endif
62327
+
62328
+ assert( nOp<=(1024/sizeof(Op)) );
62329
+ assert( nNew>=(p->nOpAlloc+nOp) );
6182762330
pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
6182862331
if( pNew ){
6182962332
p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
6183062333
v->aOp = pNew;
6183162334
}
@@ -61865,11 +62368,11 @@
6186562368
6186662369
i = p->nOp;
6186762370
assert( p->magic==VDBE_MAGIC_INIT );
6186862371
assert( op>0 && op<0xff );
6186962372
if( p->pParse->nOpAlloc<=i ){
61870
- if( growOpArray(p) ){
62373
+ if( growOpArray(p, 1) ){
6187162374
return 1;
6187262375
}
6187362376
}
6187462377
p->nOp++;
6187562378
pOp = &p->aOp[i];
@@ -62225,11 +62728,11 @@
6222562728
}
6222662729
sqlite3DbFree(p->db, pParse->aLabel);
6222762730
pParse->aLabel = 0;
6222862731
pParse->nLabel = 0;
6222962732
*pMaxFuncArgs = nMaxArgs;
62230
- assert( p->bIsReader!=0 || p->btreeMask==0 );
62733
+ assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
6223162734
}
6223262735
6223362736
/*
6223462737
** Return the address of the next instruction to be inserted.
6223562738
*/
@@ -62252,11 +62755,11 @@
6225262755
SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
6225362756
VdbeOp *aOp = p->aOp;
6225462757
assert( aOp && !p->db->mallocFailed );
6225562758
6225662759
/* Check that sqlite3VdbeUsesBtree() was not called on this VM */
62257
- assert( p->btreeMask==0 );
62760
+ assert( DbMaskAllZero(p->btreeMask) );
6225862761
6225962762
resolveP2Values(p, pnMaxArg);
6226062763
*pnOp = p->nOp;
6226162764
p->aOp = 0;
6226262765
return aOp;
@@ -62267,11 +62770,11 @@
6226762770
** address of the first operation added.
6226862771
*/
6226962772
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
6227062773
int addr;
6227162774
assert( p->magic==VDBE_MAGIC_INIT );
62272
- if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
62775
+ if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
6227362776
return 0;
6227462777
}
6227562778
addr = p->nOp;
6227662779
if( ALWAYS(nOp>0) ){
6227762780
int i;
@@ -62452,11 +62955,11 @@
6245262955
6245362956
/*
6245462957
** Change the opcode at addr into OP_Noop
6245562958
*/
6245662959
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
62457
- if( p->aOp ){
62960
+ if( addr<p->nOp ){
6245862961
VdbeOp *pOp = &p->aOp[addr];
6245962962
sqlite3 *db = p->db;
6246062963
freeP4(db, pOp->p4type, pOp->p4.p);
6246162964
memset(pOp, 0, sizeof(pOp[0]));
6246262965
pOp->opcode = OP_Noop;
@@ -62837,13 +63340,13 @@
6283763340
** p->btreeMask of databases that will require a lock.
6283863341
*/
6283963342
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
6284063343
assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
6284163344
assert( i<(int)sizeof(p->btreeMask)*8 );
62842
- p->btreeMask |= ((yDbMask)1)<<i;
63345
+ DbMaskSet(p->btreeMask, i);
6284363346
if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
62844
- p->lockMask |= ((yDbMask)1)<<i;
63347
+ DbMaskSet(p->lockMask, i);
6284563348
}
6284663349
}
6284763350
6284863351
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
6284963352
/*
@@ -62867,20 +63370,19 @@
6286763370
** this routine is N*N. But as N is rarely more than 1, this should not
6286863371
** be a problem.
6286963372
*/
6287063373
SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
6287163374
int i;
62872
- yDbMask mask;
6287363375
sqlite3 *db;
6287463376
Db *aDb;
6287563377
int nDb;
62876
- if( p->lockMask==0 ) return; /* The common case */
63378
+ if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
6287763379
db = p->db;
6287863380
aDb = db->aDb;
6287963381
nDb = db->nDb;
62880
- for(i=0, mask=1; i<nDb; i++, mask += mask){
62881
- if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
63382
+ for(i=0; i<nDb; i++){
63383
+ if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
6288263384
sqlite3BtreeEnter(aDb[i].pBt);
6288363385
}
6288463386
}
6288563387
}
6288663388
#endif
@@ -62889,20 +63391,19 @@
6288963391
/*
6289063392
** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
6289163393
*/
6289263394
SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
6289363395
int i;
62894
- yDbMask mask;
6289563396
sqlite3 *db;
6289663397
Db *aDb;
6289763398
int nDb;
62898
- if( p->lockMask==0 ) return; /* The common case */
63399
+ if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
6289963400
db = p->db;
6290063401
aDb = db->aDb;
6290163402
nDb = db->nDb;
62902
- for(i=0, mask=1; i<nDb; i++, mask += mask){
62903
- if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
63403
+ for(i=0; i<nDb; i++){
63404
+ if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
6290463405
sqlite3BtreeLeave(aDb[i].pBt);
6290563406
}
6290663407
}
6290763408
}
6290863409
#endif
@@ -63869,11 +64370,11 @@
6386964370
int cnt = 0;
6387064371
int nWrite = 0;
6387164372
int nRead = 0;
6387264373
p = db->pVdbe;
6387364374
while( p ){
63874
- if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
64375
+ if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
6387564376
cnt++;
6387664377
if( p->readOnly==0 ) nWrite++;
6387764378
if( p->bIsReader ) nRead++;
6387864379
}
6387964380
p = p->pNext;
@@ -64029,11 +64530,10 @@
6402964530
/* Lock all btrees used by the statement */
6403064531
sqlite3VdbeEnter(p);
6403164532
6403264533
/* Check for one of the special errors */
6403364534
mrc = p->rc & 0xff;
64034
- assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
6403564535
isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
6403664536
|| mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
6403764537
if( isSpecialError ){
6403864538
/* If the query was read-only and the error code is SQLITE_INTERRUPT,
6403964539
** no rollback is necessary. Otherwise, at least a savepoint
@@ -64514,11 +65014,11 @@
6451465014
/*
6451565015
** Return the serial-type for the value stored in pMem.
6451665016
*/
6451765017
SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
6451865018
int flags = pMem->flags;
64519
- int n;
65019
+ u32 n;
6452065020
6452165021
if( flags&MEM_Null ){
6452265022
return 0;
6452365023
}
6452465024
if( flags&MEM_Int ){
@@ -64544,15 +65044,15 @@
6454465044
}
6454565045
if( flags&MEM_Real ){
6454665046
return 7;
6454765047
}
6454865048
assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
64549
- n = pMem->n;
65049
+ assert( pMem->n>=0 );
65050
+ n = (u32)pMem->n;
6455065051
if( flags & MEM_Zero ){
6455165052
n += pMem->u.nZero;
6455265053
}
64553
- assert( n>=0 );
6455465054
return ((n*2) + 12 + ((flags&MEM_Str)!=0));
6455565055
}
6455665056
6455765057
/*
6455865058
** Return the length of the data corresponding to the supplied serial-type.
@@ -65315,10 +65815,11 @@
6531565815
/* rc==0 here means that one or both of the keys ran out of fields and
6531665816
** all the fields up to that point were equal. Return the the default_rc
6531765817
** value. */
6531865818
assert( CORRUPT_DB
6531965819
|| pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
65820
+ || pKeyInfo->db->mallocFailed
6532065821
);
6532165822
return pPKey2->default_rc;
6532265823
}
6532365824
6532465825
/*
@@ -65480,10 +65981,11 @@
6548065981
6548165982
assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
6548265983
|| (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
6548365984
|| (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
6548465985
|| CORRUPT_DB
65986
+ || pPKey2->pKeyInfo->db->mallocFailed
6548565987
);
6548665988
return res;
6548765989
}
6548865990
6548965991
/*
@@ -67064,11 +67566,11 @@
6706467566
/*
6706567567
** Return true if the prepared statement is in need of being reset.
6706667568
*/
6706767569
SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
6706867570
Vdbe *v = (Vdbe*)pStmt;
67069
- return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
67571
+ return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
6707067572
}
6707167573
6707267574
/*
6707367575
** Return a pointer to the next prepared statement after pStmt associated
6707467576
** with database connection pDb. If pStmt is NULL, return the first
@@ -67514,10 +68016,16 @@
6751468016
**
6751568017
** M is an integer, 2 or 3, that indices how many different ways the
6751668018
** branch can go. It is usually 2. "I" is the direction the branch
6751768019
** goes. 0 means falls through. 1 means branch is taken. 2 means the
6751868020
** second alternative branch is taken.
68021
+**
68022
+** iSrcLine is the source code line (from the __LINE__ macro) that
68023
+** generated the VDBE instruction. This instrumentation assumes that all
68024
+** source code is in a single file (the amalgamation). Special values 1
68025
+** and 2 for the iSrcLine parameter mean that this particular branch is
68026
+** always taken or never taken, respectively.
6751968027
*/
6752068028
#if !defined(SQLITE_VDBE_COVERAGE)
6752168029
# define VdbeBranchTaken(I,M)
6752268030
#else
6752368031
# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
@@ -67622,25 +68130,25 @@
6762268130
** do so without loss of information. In other words, if the string
6762368131
** looks like a number, convert it into a number. If it does not
6762468132
** look like a number, leave it alone.
6762568133
*/
6762668134
static void applyNumericAffinity(Mem *pRec){
67627
- if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
67628
- double rValue;
67629
- i64 iValue;
67630
- u8 enc = pRec->enc;
67631
- if( (pRec->flags&MEM_Str)==0 ) return;
67632
- if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
67633
- if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
67634
- pRec->u.i = iValue;
67635
- pRec->flags |= MEM_Int;
67636
- }else{
67637
- pRec->r = rValue;
67638
- pRec->flags |= MEM_Real;
67639
- }
67640
- }
67641
-}
68135
+ double rValue;
68136
+ i64 iValue;
68137
+ u8 enc = pRec->enc;
68138
+ if( (pRec->flags&MEM_Str)==0 ) return;
68139
+ if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
68140
+ if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
68141
+ pRec->u.i = iValue;
68142
+ pRec->flags |= MEM_Int;
68143
+ }else{
68144
+ pRec->r = rValue;
68145
+ pRec->flags |= MEM_Real;
68146
+ }
68147
+}
68148
+#define ApplyNumericAffinity(X) \
68149
+ if(((X)->flags&(MEM_Real|MEM_Int))==0){applyNumericAffinity(X);}
6764268150
6764368151
/*
6764468152
** Processing is determine by the affinity parameter:
6764568153
**
6764668154
** SQLITE_AFF_INTEGER:
@@ -67673,11 +68181,11 @@
6767368181
}
6767468182
pRec->flags &= ~(MEM_Real|MEM_Int);
6767568183
}else if( affinity!=SQLITE_AFF_NONE ){
6767668184
assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
6767768185
|| affinity==SQLITE_AFF_NUMERIC );
67678
- applyNumericAffinity(pRec);
68186
+ ApplyNumericAffinity(pRec);
6767968187
if( pRec->flags & MEM_Real ){
6768068188
sqlite3VdbeIntegerAffinity(pRec);
6768168189
}
6768268190
}
6768368191
}
@@ -68254,16 +68762,18 @@
6825468762
break;
6825568763
}
6825668764
6825768765
/* Opcode: InitCoroutine P1 P2 P3 * *
6825868766
**
68259
-** Set up register P1 so that it will OP_Yield to the co-routine
68767
+** Set up register P1 so that it will Yield to the coroutine
6826068768
** located at address P3.
6826168769
**
68262
-** If P2!=0 then the co-routine implementation immediately follows
68263
-** this opcode. So jump over the co-routine implementation to
68770
+** If P2!=0 then the coroutine implementation immediately follows
68771
+** this opcode. So jump over the coroutine implementation to
6826468772
** address P2.
68773
+**
68774
+** See also: EndCoroutine
6826568775
*/
6826668776
case OP_InitCoroutine: { /* jump */
6826768777
assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
6826868778
assert( pOp->p2>=0 && pOp->p2<p->nOp );
6826968779
assert( pOp->p3>=0 && pOp->p3<p->nOp );
@@ -68275,13 +68785,15 @@
6827568785
break;
6827668786
}
6827768787
6827868788
/* Opcode: EndCoroutine P1 * * * *
6827968789
**
68280
-** The instruction at the address in register P1 is an OP_Yield.
68281
-** Jump to the P2 parameter of that OP_Yield.
68790
+** The instruction at the address in register P1 is a Yield.
68791
+** Jump to the P2 parameter of that Yield.
6828268792
** After the jump, register P1 becomes undefined.
68793
+**
68794
+** See also: InitCoroutine
6828368795
*/
6828468796
case OP_EndCoroutine: { /* in1 */
6828568797
VdbeOp *pCaller;
6828668798
pIn1 = &aMem[pOp->p1];
6828768799
assert( pIn1->flags==MEM_Int );
@@ -68294,15 +68806,20 @@
6829468806
break;
6829568807
}
6829668808
6829768809
/* Opcode: Yield P1 P2 * * *
6829868810
**
68299
-** Swap the program counter with the value in register P1.
68811
+** Swap the program counter with the value in register P1. This
68812
+** has the effect of yielding to a coroutine.
6830068813
**
68301
-** If the co-routine ends with OP_Yield or OP_Return then continue
68302
-** to the next instruction. But if the co-routine ends with
68303
-** OP_EndCoroutine, jump immediately to P2.
68814
+** If the coroutine that is launched by this instruction ends with
68815
+** Yield or Return then continue to the next instruction. But if
68816
+** the coroutine launched by this instruction ends with
68817
+** EndCoroutine, then jump to P2 rather than continuing with the
68818
+** next instruction.
68819
+**
68820
+** See also: InitCoroutine
6830468821
*/
6830568822
case OP_Yield: { /* in1, jump */
6830668823
int pcDest;
6830768824
pIn1 = &aMem[pOp->p1];
6830868825
assert( VdbeMemDynamic(pIn1)==0 );
@@ -68461,11 +68978,11 @@
6846168978
6846268979
/* Opcode: String8 * P2 * P4 *
6846368980
** Synopsis: r[P2]='P4'
6846468981
**
6846568982
** P4 points to a nul terminated UTF-8 string. This opcode is transformed
68466
-** into an OP_String before it is executed for the first time. During
68983
+** into a String before it is executed for the first time. During
6846768984
** this transformation, the length of string P4 is computed and stored
6846868985
** as the P1 parameter.
6846968986
*/
6847068987
case OP_String8: { /* same as TK_STRING, out2-prerelease */
6847168988
assert( pOp->p4.z!=0 );
@@ -69683,14 +70200,18 @@
6968370200
break;
6968470201
}
6968570202
6968670203
/* Opcode: Once P1 P2 * * *
6968770204
**
69688
-** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
69689
-** set the flag and fall through to the next instruction. In other words,
69690
-** this opcode causes all following opcodes up through P2 (but not including
69691
-** P2) to run just once and to be skipped on subsequent times through the loop.
70205
+** Check the "once" flag number P1. If it is set, jump to instruction P2.
70206
+** Otherwise, set the flag and fall through to the next instruction.
70207
+** In other words, this opcode causes all following opcodes up through P2
70208
+** (but not including P2) to run just once and to be skipped on subsequent
70209
+** times through the loop.
70210
+**
70211
+** All "once" flags are initially cleared whenever a prepared statement
70212
+** first begins to run.
6969270213
*/
6969370214
case OP_Once: { /* jump */
6969470215
assert( pOp->p1<p->nOnceFlag );
6969570216
VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
6969670217
if( p->aOnceFlag[pOp->p1] ){
@@ -69703,17 +70224,17 @@
6970370224
6970470225
/* Opcode: If P1 P2 P3 * *
6970570226
**
6970670227
** Jump to P2 if the value in register P1 is true. The value
6970770228
** is considered true if it is numeric and non-zero. If the value
69708
-** in P1 is NULL then take the jump if P3 is non-zero.
70229
+** in P1 is NULL then take the jump if and only if P3 is non-zero.
6970970230
*/
6971070231
/* Opcode: IfNot P1 P2 P3 * *
6971170232
**
6971270233
** Jump to P2 if the value in register P1 is False. The value
6971370234
** is considered false if it has a numeric value of zero. If the value
69714
-** in P1 is NULL then take the jump if P3 is zero.
70235
+** in P1 is NULL then take the jump if and only if P3 is non-zero.
6971570236
*/
6971670237
case OP_If: /* jump, in1 */
6971770238
case OP_IfNot: { /* jump, in1 */
6971870239
int c;
6971970240
pIn1 = &aMem[pOp->p1];
@@ -70521,11 +71042,11 @@
7052171042
int iGen;
7052271043
7052371044
assert( p->bIsReader );
7052471045
assert( p->readOnly==0 || pOp->p2==0 );
7052571046
assert( pOp->p1>=0 && pOp->p1<db->nDb );
70526
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
71047
+ assert( DbMaskTest(p->btreeMask, pOp->p1) );
7052771048
if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
7052871049
rc = SQLITE_READONLY;
7052971050
goto abort_due_to_error;
7053071051
}
7053171052
pBt = db->aDb[pOp->p1].pBt;
@@ -70616,11 +71137,11 @@
7061671137
iDb = pOp->p1;
7061771138
iCookie = pOp->p3;
7061871139
assert( pOp->p3<SQLITE_N_BTREE_META );
7061971140
assert( iDb>=0 && iDb<db->nDb );
7062071141
assert( db->aDb[iDb].pBt!=0 );
70621
- assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
71142
+ assert( DbMaskTest(p->btreeMask, iDb) );
7062271143
7062371144
sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
7062471145
pOut->u.i = iMeta;
7062571146
break;
7062671147
}
@@ -70637,11 +71158,11 @@
7063771158
*/
7063871159
case OP_SetCookie: { /* in3 */
7063971160
Db *pDb;
7064071161
assert( pOp->p2<SQLITE_N_BTREE_META );
7064171162
assert( pOp->p1>=0 && pOp->p1<db->nDb );
70642
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
71163
+ assert( DbMaskTest(p->btreeMask, pOp->p1) );
7064371164
assert( p->readOnly==0 );
7064471165
pDb = &db->aDb[pOp->p1];
7064571166
assert( pDb->pBt!=0 );
7064671167
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
7064771168
pIn3 = &aMem[pOp->p3];
@@ -70692,11 +71213,25 @@
7069271213
** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
7069371214
** structure, then said structure defines the content and collating
7069471215
** sequence of the index being opened. Otherwise, if P4 is an integer
7069571216
** value, it is set to the number of columns in the table.
7069671217
**
70697
-** See also OpenWrite.
71218
+** See also: OpenWrite, ReopenIdx
71219
+*/
71220
+/* Opcode: ReopenIdx P1 P2 P3 P4 P5
71221
+** Synopsis: root=P2 iDb=P3
71222
+**
71223
+** The ReopenIdx opcode works exactly like ReadOpen except that it first
71224
+** checks to see if the cursor on P1 is already open with a root page
71225
+** number of P2 and if it is this opcode becomes a no-op. In other words,
71226
+** if the cursor is already open, do not reopen it.
71227
+**
71228
+** The ReopenIdx opcode may only be used with P5==0 and with P4 being
71229
+** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
71230
+** every other ReopenIdx or OpenRead for the same cursor number.
71231
+**
71232
+** See the OpenRead opcode documentation for additional information.
7069871233
*/
7069971234
/* Opcode: OpenWrite P1 P2 P3 P4 P5
7070071235
** Synopsis: root=P2 iDb=P3
7070171236
**
7070271237
** Open a read/write cursor named P1 on the table or index whose root
@@ -70714,10 +71249,23 @@
7071471249
** in read/write mode. For a given table, there can be one or more read-only
7071571250
** cursors or a single read/write cursor but not both.
7071671251
**
7071771252
** See also OpenRead.
7071871253
*/
71254
+case OP_ReopenIdx: {
71255
+ VdbeCursor *pCur;
71256
+
71257
+ assert( pOp->p5==0 );
71258
+ assert( pOp->p4type==P4_KEYINFO );
71259
+ pCur = p->apCsr[pOp->p1];
71260
+ if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
71261
+ assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
71262
+ break;
71263
+ }
71264
+ /* If the cursor is not currently open or is open on a different
71265
+ ** index, then fall through into OP_OpenRead to force a reopen */
71266
+}
7071971267
case OP_OpenRead:
7072071268
case OP_OpenWrite: {
7072171269
int nField;
7072271270
KeyInfo *pKeyInfo;
7072371271
int p2;
@@ -70728,11 +71276,12 @@
7072871276
Db *pDb;
7072971277
7073071278
assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
7073171279
assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
7073271280
assert( p->bIsReader );
70733
- assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
71281
+ assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
71282
+ || p->readOnly==0 );
7073471283
7073571284
if( p->expired ){
7073671285
rc = SQLITE_ABORT;
7073771286
break;
7073871287
}
@@ -70740,11 +71289,11 @@
7074071289
nField = 0;
7074171290
pKeyInfo = 0;
7074271291
p2 = pOp->p2;
7074371292
iDb = pOp->p3;
7074471293
assert( iDb>=0 && iDb<db->nDb );
70745
- assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
71294
+ assert( DbMaskTest(p->btreeMask, iDb) );
7074671295
pDb = &db->aDb[iDb];
7074771296
pX = pDb->pBt;
7074871297
assert( pX!=0 );
7074971298
if( pOp->opcode==OP_OpenWrite ){
7075071299
wrFlag = 1;
@@ -70785,10 +71334,11 @@
7078571334
testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
7078671335
pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
7078771336
if( pCur==0 ) goto no_mem;
7078871337
pCur->nullRow = 1;
7078971338
pCur->isOrdered = 1;
71339
+ pCur->pgnoRoot = p2;
7079071340
rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
7079171341
pCur->pKeyInfo = pKeyInfo;
7079271342
assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
7079371343
sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
7079471344
@@ -70939,11 +71489,11 @@
7093971489
sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
7094071490
p->apCsr[pOp->p1] = 0;
7094171491
break;
7094271492
}
7094371493
70944
-/* Opcode: SeekGe P1 P2 P3 P4 *
71494
+/* Opcode: SeekGE P1 P2 P3 P4 *
7094571495
** Synopsis: key=r[P3@P4]
7094671496
**
7094771497
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
7094871498
** use the value in register P3 as the key. If cursor P1 refers
7094971499
** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70950,14 +71500,18 @@
7095071500
** that are used as an unpacked index key.
7095171501
**
7095271502
** Reposition cursor P1 so that it points to the smallest entry that
7095371503
** is greater than or equal to the key value. If there are no records
7095471504
** greater than or equal to the key and P2 is not zero, then jump to P2.
71505
+**
71506
+** This opcode leaves the cursor configured to move in forward order,
71507
+** from the beginning toward the end. In other words, the cursor is
71508
+** configured to use Next, not Prev.
7095571509
**
7095671510
** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
7095771511
*/
70958
-/* Opcode: SeekGt P1 P2 P3 P4 *
71512
+/* Opcode: SeekGT P1 P2 P3 P4 *
7095971513
** Synopsis: key=r[P3@P4]
7096071514
**
7096171515
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
7096271516
** use the value in register P3 as a key. If cursor P1 refers
7096371517
** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70964,14 +71518,18 @@
7096471518
** that are used as an unpacked index key.
7096571519
**
7096671520
** Reposition cursor P1 so that it points to the smallest entry that
7096771521
** is greater than the key value. If there are no records greater than
7096871522
** the key and P2 is not zero, then jump to P2.
71523
+**
71524
+** This opcode leaves the cursor configured to move in forward order,
71525
+** from the beginning toward the end. In other words, the cursor is
71526
+** configured to use Next, not Prev.
7096971527
**
7097071528
** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
7097171529
*/
70972
-/* Opcode: SeekLt P1 P2 P3 P4 *
71530
+/* Opcode: SeekLT P1 P2 P3 P4 *
7097371531
** Synopsis: key=r[P3@P4]
7097471532
**
7097571533
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
7097671534
** use the value in register P3 as a key. If cursor P1 refers
7097771535
** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70978,14 +71536,18 @@
7097871536
** that are used as an unpacked index key.
7097971537
**
7098071538
** Reposition cursor P1 so that it points to the largest entry that
7098171539
** is less than the key value. If there are no records less than
7098271540
** the key and P2 is not zero, then jump to P2.
71541
+**
71542
+** This opcode leaves the cursor configured to move in reverse order,
71543
+** from the end toward the beginning. In other words, the cursor is
71544
+** configured to use Prev, not Next.
7098371545
**
7098471546
** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
7098571547
*/
70986
-/* Opcode: SeekLe P1 P2 P3 P4 *
71548
+/* Opcode: SeekLE P1 P2 P3 P4 *
7098771549
** Synopsis: key=r[P3@P4]
7098871550
**
7098971551
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
7099071552
** use the value in register P3 as a key. If cursor P1 refers
7099171553
** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70992,10 +71554,14 @@
7099271554
** that are used as an unpacked index key.
7099371555
**
7099471556
** Reposition cursor P1 so that it points to the largest entry that
7099571557
** is less than or equal to the key value. If there are no records
7099671558
** less than or equal to the key and P2 is not zero, then jump to P2.
71559
+**
71560
+** This opcode leaves the cursor configured to move in reverse order,
71561
+** from the end toward the beginning. In other words, the cursor is
71562
+** configured to use Prev, not Next.
7099771563
**
7099871564
** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
7099971565
*/
7100071566
case OP_SeekLT: /* jump, in3 */
7100171567
case OP_SeekLE: /* jump, in3 */
@@ -71018,16 +71584,19 @@
7101871584
assert( OP_SeekGT == OP_SeekLT+3 );
7101971585
assert( pC->isOrdered );
7102071586
assert( pC->pCursor!=0 );
7102171587
oc = pOp->opcode;
7102271588
pC->nullRow = 0;
71589
+#ifdef SQLITE_DEBUG
71590
+ pC->seekOp = pOp->opcode;
71591
+#endif
7102371592
if( pC->isTable ){
7102471593
/* The input value in P3 might be of any type: integer, real, string,
7102571594
** blob, or NULL. But it needs to be an integer before we can do
7102671595
** the seek, so covert it. */
7102771596
pIn3 = &aMem[pOp->p3];
71028
- applyNumericAffinity(pIn3);
71597
+ ApplyNumericAffinity(pIn3);
7102971598
iKey = sqlite3VdbeIntValue(pIn3);
7103071599
pC->rowidIsValid = 0;
7103171600
7103271601
/* If the P3 value could not be converted into an integer without
7103371602
** loss of information, then special processing is required... */
@@ -71172,10 +71741,14 @@
7117271741
** record.
7117371742
**
7117471743
** Cursor P1 is on an index btree. If the record identified by P3 and P4
7117571744
** is a prefix of any entry in P1 then a jump is made to P2 and
7117671745
** P1 is left pointing at the matching entry.
71746
+**
71747
+** This operation leaves the cursor in a state where it can be
71748
+** advanced in the forward direction. The Next instruction will work,
71749
+** but not the Prev instruction.
7117771750
**
7117871751
** See also: NotFound, NoConflict, NotExists. SeekGe
7117971752
*/
7118071753
/* Opcode: NotFound P1 P2 P3 P4 *
7118171754
** Synopsis: key=r[P3@P4]
@@ -71187,10 +71760,14 @@
7118771760
** Cursor P1 is on an index btree. If the record identified by P3 and P4
7118871761
** is not the prefix of any entry in P1 then a jump is made to P2. If P1
7118971762
** does contain an entry whose prefix matches the P3/P4 record then control
7119071763
** falls through to the next instruction and P1 is left pointing at the
7119171764
** matching entry.
71765
+**
71766
+** This operation leaves the cursor in a state where it cannot be
71767
+** advanced in either direction. In other words, the Next and Prev
71768
+** opcodes do not work after this operation.
7119271769
**
7119371770
** See also: Found, NotExists, NoConflict
7119471771
*/
7119571772
/* Opcode: NoConflict P1 P2 P3 P4 *
7119671773
** Synopsis: key=r[P3@P4]
@@ -71206,10 +71783,14 @@
7120671783
** immediately to P2. If there is a match, fall through and leave the P1
7120771784
** cursor pointing to the matching row.
7120871785
**
7120971786
** This opcode is similar to OP_NotFound with the exceptions that the
7121071787
** branch is always taken if any part of the search key input is NULL.
71788
+**
71789
+** This operation leaves the cursor in a state where it cannot be
71790
+** advanced in either direction. In other words, the Next and Prev
71791
+** opcodes do not work after this operation.
7121171792
**
7121271793
** See also: NotFound, Found, NotExists
7121371794
*/
7121471795
case OP_NoConflict: /* jump, in3 */
7121571796
case OP_NotFound: /* jump, in3 */
@@ -71229,10 +71810,13 @@
7122971810
7123071811
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7123171812
assert( pOp->p4type==P4_INT32 );
7123271813
pC = p->apCsr[pOp->p1];
7123371814
assert( pC!=0 );
71815
+#ifdef SQLITE_DEBUG
71816
+ pC->seekOp = pOp->opcode;
71817
+#endif
7123471818
pIn3 = &aMem[pOp->p3];
7123571819
assert( pC->pCursor!=0 );
7123671820
assert( pC->isTable==0 );
7123771821
pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
7123871822
if( pOp->p4.i>0 ){
@@ -71299,10 +71883,14 @@
7129971883
** with rowid P3 then leave the cursor pointing at that record and fall
7130071884
** through to the next instruction.
7130171885
**
7130271886
** The OP_NotFound opcode performs the same operation on index btrees
7130371887
** (with arbitrary multi-value keys).
71888
+**
71889
+** This opcode leaves the cursor in a state where it cannot be advanced
71890
+** in either direction. In other words, the Next and Prev opcodes will
71891
+** not work following this opcode.
7130471892
**
7130571893
** See also: Found, NotFound, NoConflict
7130671894
*/
7130771895
case OP_NotExists: { /* jump, in3 */
7130871896
VdbeCursor *pC;
@@ -71313,10 +71901,13 @@
7131371901
pIn3 = &aMem[pOp->p3];
7131471902
assert( pIn3->flags & MEM_Int );
7131571903
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7131671904
pC = p->apCsr[pOp->p1];
7131771905
assert( pC!=0 );
71906
+#ifdef SQLITE_DEBUG
71907
+ pC->seekOp = 0;
71908
+#endif
7131871909
assert( pC->isTable );
7131971910
assert( pC->pseudoTableReg==0 );
7132071911
pCrsr = pC->pCursor;
7132171912
assert( pCrsr!=0 );
7132271913
res = 0;
@@ -71615,11 +72206,11 @@
7161572206
** Delete the record at which the P1 cursor is currently pointing.
7161672207
**
7161772208
** The cursor will be left pointing at either the next or the previous
7161872209
** record in the table. If it is left pointing at the next record, then
7161972210
** the next Next instruction will be a no-op. Hence it is OK to delete
71620
-** a record from within an Next loop.
72211
+** a record from within a Next loop.
7162172212
**
7162272213
** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
7162372214
** incremented (otherwise not).
7162472215
**
7162572216
** P1 must not be pseudo-table. It has to be a real table with
@@ -71675,16 +72266,16 @@
7167572266
p->nChange = 0;
7167672267
break;
7167772268
}
7167872269
7167972270
/* Opcode: SorterCompare P1 P2 P3 P4
71680
-** Synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2
72271
+** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
7168172272
**
7168272273
** P1 is a sorter cursor. This instruction compares a prefix of the
71683
-** the record blob in register P3 against a prefix of the entry that
71684
-** the sorter cursor currently points to. The final P4 fields of both
71685
-** the P3 and sorter record are ignored.
72274
+** record blob in register P3 against a prefix of the entry that
72275
+** the sorter cursor currently points to. Only the first P4 fields
72276
+** of r[P3] and the sorter record are compared.
7168672277
**
7168772278
** If either P3 or the sorter contains a NULL in one of their significant
7168872279
** fields (not counting the P4 fields at the end which are ignored) then
7168972280
** the comparison is assumed to be equal.
7169072281
**
@@ -71692,18 +72283,18 @@
7169272283
** each other. Jump to P2 if they are different.
7169372284
*/
7169472285
case OP_SorterCompare: {
7169572286
VdbeCursor *pC;
7169672287
int res;
71697
- int nIgnore;
72288
+ int nKeyCol;
7169872289
7169972290
pC = p->apCsr[pOp->p1];
7170072291
assert( isSorter(pC) );
7170172292
assert( pOp->p4type==P4_INT32 );
7170272293
pIn3 = &aMem[pOp->p3];
71703
- nIgnore = pOp->p4.i;
71704
- rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
72294
+ nKeyCol = pOp->p4.i;
72295
+ rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
7170572296
VdbeBranchTaken(res!=0,2);
7170672297
if( res ){
7170772298
pc = pOp->p2-1;
7170872299
}
7170972300
break;
@@ -71879,15 +72470,19 @@
7187972470
break;
7188072471
}
7188172472
7188272473
/* Opcode: Last P1 P2 * * *
7188372474
**
71884
-** The next use of the Rowid or Column or Next instruction for P1
72475
+** The next use of the Rowid or Column or Prev instruction for P1
7188572476
** will refer to the last entry in the database table or index.
7188672477
** If the table or index is empty and P2>0, then jump immediately to P2.
7188772478
** If P2 is 0 or if the table or index is not empty, fall through
7188872479
** to the following instruction.
72480
+**
72481
+** This opcode leaves the cursor configured to move in reverse order,
72482
+** from the end toward the beginning. In other words, the cursor is
72483
+** configured to use Prev, not Next.
7188972484
*/
7189072485
case OP_Last: { /* jump */
7189172486
VdbeCursor *pC;
7189272487
BtCursor *pCrsr;
7189372488
int res;
@@ -71901,10 +72496,13 @@
7190172496
rc = sqlite3BtreeLast(pCrsr, &res);
7190272497
pC->nullRow = (u8)res;
7190372498
pC->deferredMoveto = 0;
7190472499
pC->rowidIsValid = 0;
7190572500
pC->cacheStatus = CACHE_STALE;
72501
+#ifdef SQLITE_DEBUG
72502
+ pC->seekOp = OP_Last;
72503
+#endif
7190672504
if( pOp->p2>0 ){
7190772505
VdbeBranchTaken(res!=0,2);
7190872506
if( res ) pc = pOp->p2 - 1;
7190972507
}
7191072508
break;
@@ -71937,10 +72535,14 @@
7193772535
** The next use of the Rowid or Column or Next instruction for P1
7193872536
** will refer to the first entry in the database table or index.
7193972537
** If the table or index is empty and P2>0, then jump immediately to P2.
7194072538
** If P2 is 0 or if the table or index is not empty, fall through
7194172539
** to the following instruction.
72540
+**
72541
+** This opcode leaves the cursor configured to move in forward order,
72542
+** from the beginning toward the end. In other words, the cursor is
72543
+** configured to use Next, not Prev.
7194272544
*/
7194372545
case OP_Rewind: { /* jump */
7194472546
VdbeCursor *pC;
7194572547
BtCursor *pCrsr;
7194672548
int res;
@@ -71948,10 +72550,13 @@
7194872550
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7194972551
pC = p->apCsr[pOp->p1];
7195072552
assert( pC!=0 );
7195172553
assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
7195272554
res = 1;
72555
+#ifdef SQLITE_DEBUG
72556
+ pC->seekOp = OP_Rewind;
72557
+#endif
7195372558
if( isSorter(pC) ){
7195472559
rc = sqlite3VdbeSorterRewind(db, pC, &res);
7195572560
}else{
7195672561
pCrsr = pC->pCursor;
7195772562
assert( pCrsr );
@@ -71973,10 +72578,14 @@
7197372578
**
7197472579
** Advance cursor P1 so that it points to the next key/data pair in its
7197572580
** table or index. If there are no more key/value pairs then fall through
7197672581
** to the following instruction. But if the cursor advance was successful,
7197772582
** jump immediately to P2.
72583
+**
72584
+** The Next opcode is only valid following an SeekGT, SeekGE, or
72585
+** OP_Rewind opcode used to position the cursor. Next is not allowed
72586
+** to follow SeekLT, SeekLE, or OP_Last.
7197872587
**
7197972588
** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
7198072589
** been opened prior to this opcode or the program will segfault.
7198172590
**
7198272591
** The P3 value is a hint to the btree implementation. If P3==1, that
@@ -71992,20 +72601,25 @@
7199272601
**
7199372602
** See also: Prev, NextIfOpen
7199472603
*/
7199572604
/* Opcode: NextIfOpen P1 P2 P3 P4 P5
7199672605
**
71997
-** This opcode works just like OP_Next except that if cursor P1 is not
72606
+** This opcode works just like Next except that if cursor P1 is not
7199872607
** open it behaves a no-op.
7199972608
*/
7200072609
/* Opcode: Prev P1 P2 P3 P4 P5
7200172610
**
7200272611
** Back up cursor P1 so that it points to the previous key/data pair in its
7200372612
** table or index. If there is no previous key/value pairs then fall through
7200472613
** to the following instruction. But if the cursor backup was successful,
7200572614
** jump immediately to P2.
7200672615
**
72616
+**
72617
+** The Prev opcode is only valid following an SeekLT, SeekLE, or
72618
+** OP_Last opcode used to position the cursor. Prev is not allowed
72619
+** to follow SeekGT, SeekGE, or OP_Rewind.
72620
+**
7200772621
** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
7200872622
** not open then the behavior is undefined.
7200972623
**
7201072624
** The P3 value is a hint to the btree implementation. If P3==1, that
7201172625
** means P1 is an SQL index and that this instruction could have been
@@ -72018,11 +72632,11 @@
7201872632
** If P5 is positive and the jump is taken, then event counter
7201972633
** number P5-1 in the prepared statement is incremented.
7202072634
*/
7202172635
/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
7202272636
**
72023
-** This opcode works just like OP_Prev except that if cursor P1 is not
72637
+** This opcode works just like Prev except that if cursor P1 is not
7202472638
** open it behaves a no-op.
7202572639
*/
7202672640
case OP_SorterNext: { /* jump */
7202772641
VdbeCursor *pC;
7202872642
int res;
@@ -72049,10 +72663,20 @@
7204972663
testcase( res==1 );
7205072664
assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
7205172665
assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
7205272666
assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
7205372667
assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
72668
+
72669
+ /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
72670
+ ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
72671
+ assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
72672
+ || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
72673
+ || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
72674
+ assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
72675
+ || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
72676
+ || pC->seekOp==OP_Last );
72677
+
7205472678
rc = pOp->p4.xAdvance(pC->pCursor, &res);
7205572679
next_tail:
7205672680
pC->cacheStatus = CACHE_STALE;
7205772681
VdbeBranchTaken(res==0,2);
7205872682
if( res==0 ){
@@ -72331,11 +72955,11 @@
7233172955
rc = SQLITE_LOCKED;
7233272956
p->errorAction = OE_Abort;
7233372957
}else{
7233472958
iDb = pOp->p3;
7233572959
assert( iCnt==1 );
72336
- assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
72960
+ assert( DbMaskTest(p->btreeMask, iDb) );
7233772961
iMoved = 0; /* Not needed. Only to silence a warning. */
7233872962
rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
7233972963
pOut->flags = MEM_Int;
7234072964
pOut->u.i = iMoved;
7234172965
#ifndef SQLITE_OMIT_AUTOVACUUM
@@ -72371,11 +72995,11 @@
7237172995
case OP_Clear: {
7237272996
int nChange;
7237372997
7237472998
nChange = 0;
7237572999
assert( p->readOnly==0 );
72376
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
73000
+ assert( DbMaskTest(p->btreeMask, pOp->p2) );
7237773001
rc = sqlite3BtreeClearTable(
7237873002
db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
7237973003
);
7238073004
if( pOp->p3 ){
7238173005
p->nChange += nChange;
@@ -72441,11 +73065,11 @@
7244173065
int flags;
7244273066
Db *pDb;
7244373067
7244473068
pgno = 0;
7244573069
assert( pOp->p1>=0 && pOp->p1<db->nDb );
72446
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
73070
+ assert( DbMaskTest(p->btreeMask, pOp->p1) );
7244773071
assert( p->readOnly==0 );
7244873072
pDb = &db->aDb[pOp->p1];
7244973073
assert( pDb->pBt!=0 );
7245073074
if( pOp->opcode==OP_CreateTable ){
7245173075
/* flags = BTREE_INTKEY; */
@@ -72529,11 +73153,12 @@
7252973153
7253073154
/* Opcode: DropTable P1 * * P4 *
7253173155
**
7253273156
** Remove the internal (in-memory) data structures that describe
7253373157
** the table named P4 in database P1. This is called after a table
72534
-** is dropped in order to keep the internal representation of the
73158
+** is dropped from disk (using the Destroy opcode) in order to keep
73159
+** the internal representation of the
7253573160
** schema consistent with what is on disk.
7253673161
*/
7253773162
case OP_DropTable: {
7253873163
sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
7253973164
break;
@@ -72541,11 +73166,12 @@
7254173166
7254273167
/* Opcode: DropIndex P1 * * P4 *
7254373168
**
7254473169
** Remove the internal (in-memory) data structures that describe
7254573170
** the index named P4 in database P1. This is called after an index
72546
-** is dropped in order to keep the internal representation of the
73171
+** is dropped from disk (using the Destroy opcode)
73172
+** in order to keep the internal representation of the
7254773173
** schema consistent with what is on disk.
7254873174
*/
7254973175
case OP_DropIndex: {
7255073176
sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
7255173177
break;
@@ -72553,11 +73179,12 @@
7255373179
7255473180
/* Opcode: DropTrigger P1 * * P4 *
7255573181
**
7255673182
** Remove the internal (in-memory) data structures that describe
7255773183
** the trigger named P4 in database P1. This is called after a trigger
72558
-** is dropped in order to keep the internal representation of the
73184
+** is dropped from disk (using the Destroy opcode) in order to keep
73185
+** the internal representation of the
7255973186
** schema consistent with what is on disk.
7256073187
*/
7256173188
case OP_DropTrigger: {
7256273189
sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
7256373190
break;
@@ -72606,11 +73233,11 @@
7260673233
for(j=0; j<nRoot; j++){
7260773234
aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
7260873235
}
7260973236
aRoot[j] = 0;
7261073237
assert( pOp->p5<db->nDb );
72611
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
73238
+ assert( DbMaskTest(p->btreeMask, pOp->p5) );
7261273239
z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
7261373240
(int)pnErr->u.i, &nErr);
7261473241
sqlite3DbFree(db, aRoot);
7261573242
pnErr->u.i -= nErr;
7261673243
sqlite3VdbeMemSetNull(pIn1);
@@ -72968,21 +73595,20 @@
7296873595
pc = pOp->p2 - 1;
7296973596
}
7297073597
break;
7297173598
}
7297273599
72973
-/* Opcode: IfNeg P1 P2 * * *
72974
-** Synopsis: if r[P1]<0 goto P2
73600
+/* Opcode: IfNeg P1 P2 P3 * *
73601
+** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
7297573602
**
72976
-** If the value of register P1 is less than zero, jump to P2.
72977
-**
72978
-** It is illegal to use this instruction on a register that does
72979
-** not contain an integer. An assertion fault will result if you try.
73603
+** Register P1 must contain an integer. Add literal P3 to the value in
73604
+** register P1 then if the value of register P1 is less than zero, jump to P2.
7298073605
*/
7298173606
case OP_IfNeg: { /* jump, in1 */
7298273607
pIn1 = &aMem[pOp->p1];
7298373608
assert( pIn1->flags&MEM_Int );
73609
+ pIn1->u.i += pOp->p3;
7298473610
VdbeBranchTaken(pIn1->u.i<0, 2);
7298573611
if( pIn1->u.i<0 ){
7298673612
pc = pOp->p2 - 1;
7298773613
}
7298873614
break;
@@ -72991,13 +73617,10 @@
7299173617
/* Opcode: IfZero P1 P2 P3 * *
7299273618
** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
7299373619
**
7299473620
** The register P1 must contain an integer. Add literal P3 to the
7299573621
** value in register P1. If the result is exactly 0, jump to P2.
72996
-**
72997
-** It is illegal to use this instruction on a register that does
72998
-** not contain an integer. An assertion fault will result if you try.
7299973622
*/
7300073623
case OP_IfZero: { /* jump, in1 */
7300173624
pIn1 = &aMem[pOp->p1];
7300273625
assert( pIn1->flags&MEM_Int );
7300373626
pIn1->u.i += pOp->p3;
@@ -73266,11 +73889,11 @@
7326673889
*/
7326773890
case OP_IncrVacuum: { /* jump */
7326873891
Btree *pBt;
7326973892
7327073893
assert( pOp->p1>=0 && pOp->p1<db->nDb );
73271
- assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
73894
+ assert( DbMaskTest(p->btreeMask, pOp->p1) );
7327273895
assert( p->readOnly==0 );
7327373896
pBt = db->aDb[pOp->p1].pBt;
7327473897
rc = sqlite3BtreeIncrVacuum(pBt);
7327573898
VdbeBranchTaken(rc==SQLITE_DONE,2);
7327673899
if( rc==SQLITE_DONE ){
@@ -73281,16 +73904,17 @@
7328173904
}
7328273905
#endif
7328373906
7328473907
/* Opcode: Expire P1 * * * *
7328573908
**
73286
-** Cause precompiled statements to become expired. An expired statement
73287
-** fails with an error code of SQLITE_SCHEMA if it is ever executed
73288
-** (via sqlite3_step()).
73909
+** Cause precompiled statements to expire. When an expired statement
73910
+** is executed using sqlite3_step() it will either automatically
73911
+** reprepare itself (if it was originally created using sqlite3_prepare_v2())
73912
+** or it will fail with SQLITE_SCHEMA.
7328973913
**
7329073914
** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
73291
-** then only the currently executing statement is affected.
73915
+** then only the currently executing statement is expired.
7329273916
*/
7329373917
case OP_Expire: {
7329473918
if( !pOp->p1 ){
7329573919
sqlite3ExpirePreparedStatements(db);
7329673920
}else{
@@ -73318,11 +73942,11 @@
7331873942
case OP_TableLock: {
7331973943
u8 isWriteLock = (u8)pOp->p3;
7332073944
if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
7332173945
int p1 = pOp->p1;
7332273946
assert( p1>=0 && p1<db->nDb );
73323
- assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
73947
+ assert( DbMaskTest(p->btreeMask, p1) );
7332473948
assert( isWriteLock==0 || isWriteLock==1 );
7332573949
rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
7332673950
if( (rc&0xFF)==SQLITE_LOCKED ){
7332773951
const char *z = pOp->p4.z;
7332873952
sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -73768,11 +74392,11 @@
7376874392
#ifdef SQLITE_USE_FCNTL_TRACE
7376974393
zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7377074394
if( zTrace ){
7377174395
int i;
7377274396
for(i=0; i<db->nDb; i++){
73773
- if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
74397
+ if( DbMaskTest(p->btreeMask, i)==0 ) continue;
7377474398
sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
7377574399
}
7377674400
}
7377774401
#endif /* SQLITE_USE_FCNTL_TRACE */
7377874402
#ifdef SQLITE_DEBUG
@@ -74758,11 +75382,11 @@
7475875382
** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
7475975383
** has been allocated and contains an unpacked record that is used as key2.
7476075384
*/
7476175385
static void vdbeSorterCompare(
7476275386
const VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
74763
- int nIgnore, /* Ignore the last nIgnore fields */
75387
+ int nKeyCol, /* Num of columns. 0 means "all" */
7476475388
const void *pKey1, int nKey1, /* Left side of comparison */
7476575389
const void *pKey2, int nKey2, /* Right side of comparison */
7476675390
int *pRes /* OUT: Result of comparison */
7476775391
){
7476875392
KeyInfo *pKeyInfo = pCsr->pKeyInfo;
@@ -74772,14 +75396,13 @@
7477275396
7477375397
if( pKey2 ){
7477475398
sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
7477575399
}
7477675400
74777
- if( nIgnore ){
74778
- r2->nField = pKeyInfo->nField - nIgnore;
74779
- assert( r2->nField>0 );
74780
- for(i=0; i<r2->nField; i++){
75401
+ if( nKeyCol ){
75402
+ r2->nField = nKeyCol;
75403
+ for(i=0; i<nKeyCol; i++){
7478175404
if( r2->aMem[i].flags & MEM_Null ){
7478275405
*pRes = -1;
7478375406
return;
7478475407
}
7478575408
}
@@ -75457,18 +76080,18 @@
7545776080
** key.
7545876081
*/
7545976082
SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
7546076083
const VdbeCursor *pCsr, /* Sorter cursor */
7546176084
Mem *pVal, /* Value to compare to current sorter key */
75462
- int nIgnore, /* Ignore this many fields at the end */
76085
+ int nKeyCol, /* Only compare this many fields */
7546376086
int *pRes /* OUT: Result of comparison */
7546476087
){
7546576088
VdbeSorter *pSorter = pCsr->pSorter;
7546676089
void *pKey; int nKey; /* Sorter key to compare pVal with */
7546776090
7546876091
pKey = vdbeSorterRowkey(pSorter, &nKey);
75469
- vdbeSorterCompare(pCsr, nIgnore, pVal->z, pVal->n, pKey, nKey, pRes);
76092
+ vdbeSorterCompare(pCsr, nKeyCol, pVal->z, pVal->n, pKey, nKey, pRes);
7547076093
return SQLITE_OK;
7547176094
}
7547276095
7547376096
/************** End of vdbesort.c ********************************************/
7547476097
/************** Begin file journal.c *****************************************/
@@ -76496,11 +77119,11 @@
7649677119
}
7649777120
break;
7649877121
}
7649977122
}
7650077123
if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
76501
- /* IMP: R-24309-18625 */
77124
+ /* IMP: R-51414-32910 */
7650277125
/* IMP: R-44911-55124 */
7650377126
iCol = -1;
7650477127
}
7650577128
if( iCol<pTab->nCol ){
7650677129
cnt++;
@@ -76852,12 +77475,17 @@
7685277475
}
7685377476
}else{
7685477477
/* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
7685577478
** likelihood(X, 0.0625).
7685677479
** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
76857
- ** likelihood(X,0.0625). */
76858
- pExpr->iTable = 62; /* TUNING: Default 2nd arg to unlikely() is 0.0625 */
77480
+ ** likelihood(X,0.0625).
77481
+ ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
77482
+ ** likelihood(X,0.9375).
77483
+ ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
77484
+ ** likelihood(X,0.9375). */
77485
+ /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
77486
+ pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
7685977487
}
7686077488
}
7686177489
}
7686277490
#ifndef SQLITE_OMIT_AUTHORIZATION
7686377491
if( pDef ){
@@ -77629,11 +78257,11 @@
7762978257
** SELECT * FROM t1 WHERE (select a from t1);
7763078258
*/
7763178259
SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
7763278260
int op;
7763378261
pExpr = sqlite3ExprSkipCollate(pExpr);
77634
- if( pExpr->flags & EP_Generic ) return SQLITE_AFF_NONE;
78262
+ if( pExpr->flags & EP_Generic ) return 0;
7763578263
op = pExpr->op;
7763678264
if( op==TK_SELECT ){
7763778265
assert( pExpr->flags&EP_xIsSelect );
7763878266
return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
7763978267
}
@@ -78964,10 +79592,13 @@
7896479592
case TK_INTEGER:
7896579593
case TK_STRING:
7896679594
case TK_FLOAT:
7896779595
case TK_BLOB:
7896879596
return 0;
79597
+ case TK_COLUMN:
79598
+ assert( p->pTab!=0 );
79599
+ return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0;
7896979600
default:
7897079601
return 1;
7897179602
}
7897279603
}
7897379604
@@ -79071,83 +79702,124 @@
7907179702
SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
7907279703
Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
7907379704
return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
7907479705
}
7907579706
79707
+/*
79708
+** Generate code that checks the left-most column of index table iCur to see if
79709
+** it contains any NULL entries. Cause the register at regHasNull to be set
79710
+** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
79711
+** to be set to NULL if iCur contains one or more NULL values.
79712
+*/
79713
+static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
79714
+ int j1;
79715
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
79716
+ j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
79717
+ sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
79718
+ sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
79719
+ VdbeComment((v, "first_entry_in(%d)", iCur));
79720
+ sqlite3VdbeJumpHere(v, j1);
79721
+}
79722
+
79723
+
79724
+#ifndef SQLITE_OMIT_SUBQUERY
79725
+/*
79726
+** The argument is an IN operator with a list (not a subquery) on the
79727
+** right-hand side. Return TRUE if that list is constant.
79728
+*/
79729
+static int sqlite3InRhsIsConstant(Expr *pIn){
79730
+ Expr *pLHS;
79731
+ int res;
79732
+ assert( !ExprHasProperty(pIn, EP_xIsSelect) );
79733
+ pLHS = pIn->pLeft;
79734
+ pIn->pLeft = 0;
79735
+ res = sqlite3ExprIsConstant(pIn);
79736
+ pIn->pLeft = pLHS;
79737
+ return res;
79738
+}
79739
+#endif
79740
+
7907679741
/*
7907779742
** This function is used by the implementation of the IN (...) operator.
7907879743
** The pX parameter is the expression on the RHS of the IN operator, which
7907979744
** might be either a list of expressions or a subquery.
7908079745
**
7908179746
** The job of this routine is to find or create a b-tree object that can
7908279747
** be used either to test for membership in the RHS set or to iterate through
7908379748
** all members of the RHS set, skipping duplicates.
7908479749
**
79085
-** A cursor is opened on the b-tree object that the RHS of the IN operator
79750
+** A cursor is opened on the b-tree object that is the RHS of the IN operator
7908679751
** and pX->iTable is set to the index of that cursor.
7908779752
**
7908879753
** The returned value of this function indicates the b-tree type, as follows:
7908979754
**
7909079755
** IN_INDEX_ROWID - The cursor was opened on a database table.
7909179756
** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
7909279757
** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
7909379758
** IN_INDEX_EPH - The cursor was opened on a specially created and
7909479759
** populated epheremal table.
79760
+** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
79761
+** implemented as a sequence of comparisons.
7909579762
**
7909679763
** An existing b-tree might be used if the RHS expression pX is a simple
7909779764
** subquery such as:
7909879765
**
7909979766
** SELECT <column> FROM <table>
7910079767
**
7910179768
** If the RHS of the IN operator is a list or a more complex subquery, then
7910279769
** an ephemeral table might need to be generated from the RHS and then
7910379770
** pX->iTable made to point to the ephermeral table instead of an
79104
-** existing table.
79771
+** existing table.
7910579772
**
79106
-** If the prNotFound parameter is 0, then the b-tree will be used to iterate
79107
-** through the set members, skipping any duplicates. In this case an
79108
-** epheremal table must be used unless the selected <column> is guaranteed
79773
+** The inFlags parameter must contain exactly one of the bits
79774
+** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
79775
+** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
79776
+** fast membership test. When the IN_INDEX_LOOP bit is set, the
79777
+** IN index will be used to loop over all values of the RHS of the
79778
+** IN operator.
79779
+**
79780
+** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
79781
+** through the set members) then the b-tree must not contain duplicates.
79782
+** An epheremal table must be used unless the selected <column> is guaranteed
7910979783
** to be unique - either because it is an INTEGER PRIMARY KEY or it
7911079784
** has a UNIQUE constraint or UNIQUE index.
7911179785
**
79112
-** If the prNotFound parameter is not 0, then the b-tree will be used
79113
-** for fast set membership tests. In this case an epheremal table must
79786
+** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
79787
+** for fast set membership tests) then an epheremal table must
7911479788
** be used unless <column> is an INTEGER PRIMARY KEY or an index can
7911579789
** be found with <column> as its left-most column.
79790
+**
79791
+** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
79792
+** if the RHS of the IN operator is a list (not a subquery) then this
79793
+** routine might decide that creating an ephemeral b-tree for membership
79794
+** testing is too expensive and return IN_INDEX_NOOP. In that case, the
79795
+** calling routine should implement the IN operator using a sequence
79796
+** of Eq or Ne comparison operations.
7911679797
**
7911779798
** When the b-tree is being used for membership tests, the calling function
79118
-** needs to know whether or not the structure contains an SQL NULL
79119
-** value in order to correctly evaluate expressions like "X IN (Y, Z)".
79120
-** If there is any chance that the (...) might contain a NULL value at
79799
+** might need to know whether or not the RHS side of the IN operator
79800
+** contains a NULL. If prRhsHasNull is not a NULL pointer and
79801
+** if there is any chance that the (...) might contain a NULL value at
7912179802
** runtime, then a register is allocated and the register number written
79122
-** to *prNotFound. If there is no chance that the (...) contains a
79123
-** NULL value, then *prNotFound is left unchanged.
79124
-**
79125
-** If a register is allocated and its location stored in *prNotFound, then
79126
-** its initial value is NULL. If the (...) does not remain constant
79127
-** for the duration of the query (i.e. the SELECT within the (...)
79128
-** is a correlated subquery) then the value of the allocated register is
79129
-** reset to NULL each time the subquery is rerun. This allows the
79130
-** caller to use vdbe code equivalent to the following:
79131
-**
79132
-** if( register==NULL ){
79133
-** has_null = <test if data structure contains null>
79134
-** register = 1
79135
-** }
79136
-**
79137
-** in order to avoid running the <test if data structure contains null>
79138
-** test more often than is necessary.
79803
+** to *prRhsHasNull. If there is no chance that the (...) contains a
79804
+** NULL value, then *prRhsHasNull is left unchanged.
79805
+**
79806
+** If a register is allocated and its location stored in *prRhsHasNull, then
79807
+** the value in that register will be NULL if the b-tree contains one or more
79808
+** NULL values, and it will be some non-NULL value if the b-tree contains no
79809
+** NULL values.
7913979810
*/
7914079811
#ifndef SQLITE_OMIT_SUBQUERY
79141
-SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
79812
+SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
7914279813
Select *p; /* SELECT to the right of IN operator */
7914379814
int eType = 0; /* Type of RHS table. IN_INDEX_* */
7914479815
int iTab = pParse->nTab++; /* Cursor of the RHS table */
79145
- int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
79816
+ int mustBeUnique; /* True if RHS must be unique */
7914679817
Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
7914779818
7914879819
assert( pX->op==TK_IN );
79820
+ mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
7914979821
7915079822
/* Check to see if an existing table or index can be used to
7915179823
** satisfy the query. This is preferable to generating a new
7915279824
** ephemeral table.
7915379825
*/
@@ -79200,44 +79872,59 @@
7920079872
int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
7920179873
7920279874
for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
7920379875
if( (pIdx->aiColumn[0]==iCol)
7920479876
&& sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
79205
- && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
79877
+ && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
7920679878
){
7920779879
int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
7920879880
sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
7920979881
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
7921079882
VdbeComment((v, "%s", pIdx->zName));
7921179883
assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
7921279884
eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
7921379885
79214
- if( prNotFound && !pTab->aCol[iCol].notNull ){
79215
- *prNotFound = ++pParse->nMem;
79216
- sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
79886
+ if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
79887
+ *prRhsHasNull = ++pParse->nMem;
79888
+ sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
7921779889
}
7921879890
sqlite3VdbeJumpHere(v, iAddr);
7921979891
}
7922079892
}
7922179893
}
7922279894
}
79895
+
79896
+ /* If no preexisting index is available for the IN clause
79897
+ ** and IN_INDEX_NOOP is an allowed reply
79898
+ ** and the RHS of the IN operator is a list, not a subquery
79899
+ ** and the RHS is not contant or has two or fewer terms,
79900
+ ** then it is not worth creating an ephermeral table to evaluate
79901
+ ** the IN operator so return IN_INDEX_NOOP.
79902
+ */
79903
+ if( eType==0
79904
+ && (inFlags & IN_INDEX_NOOP_OK)
79905
+ && !ExprHasProperty(pX, EP_xIsSelect)
79906
+ && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
79907
+ ){
79908
+ eType = IN_INDEX_NOOP;
79909
+ }
79910
+
7922379911
7922479912
if( eType==0 ){
79225
- /* Could not found an existing table or index to use as the RHS b-tree.
79913
+ /* Could not find an existing table or index to use as the RHS b-tree.
7922679914
** We will have to generate an ephemeral table to do the job.
7922779915
*/
7922879916
u32 savedNQueryLoop = pParse->nQueryLoop;
7922979917
int rMayHaveNull = 0;
7923079918
eType = IN_INDEX_EPH;
79231
- if( prNotFound ){
79232
- *prNotFound = rMayHaveNull = ++pParse->nMem;
79233
- sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
79234
- }else{
79919
+ if( inFlags & IN_INDEX_LOOP ){
7923579920
pParse->nQueryLoop = 0;
7923679921
if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
7923779922
eType = IN_INDEX_ROWID;
7923879923
}
79924
+ }else if( prRhsHasNull ){
79925
+ *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
7923979926
}
7924079927
sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
7924179928
pParse->nQueryLoop = savedNQueryLoop;
7924279929
}else{
7924379930
pX->iTable = iTab;
@@ -79264,31 +79951,25 @@
7926479951
** intkey B-Tree to store the set of IN(...) values instead of the usual
7926579952
** (slower) variable length keys B-Tree.
7926679953
**
7926779954
** If rMayHaveNull is non-zero, that means that the operation is an IN
7926879955
** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
79269
-** Furthermore, the IN is in a WHERE clause and that we really want
79270
-** to iterate over the RHS of the IN operator in order to quickly locate
79271
-** all corresponding LHS elements. All this routine does is initialize
79272
-** the register given by rMayHaveNull to NULL. Calling routines will take
79273
-** care of changing this register value to non-NULL if the RHS is NULL-free.
79274
-**
79275
-** If rMayHaveNull is zero, that means that the subquery is being used
79276
-** for membership testing only. There is no need to initialize any
79277
-** registers to indicate the presence or absence of NULLs on the RHS.
79956
+** All this routine does is initialize the register given by rMayHaveNull
79957
+** to NULL. Calling routines will take care of changing this register
79958
+** value to non-NULL if the RHS is NULL-free.
7927879959
**
7927979960
** For a SELECT or EXISTS operator, return the register that holds the
7928079961
** result. For IN operators or if an error occurs, the return value is 0.
7928179962
*/
7928279963
#ifndef SQLITE_OMIT_SUBQUERY
7928379964
SQLITE_PRIVATE int sqlite3CodeSubselect(
7928479965
Parse *pParse, /* Parsing context */
7928579966
Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
79286
- int rMayHaveNull, /* Register that records whether NULLs exist in RHS */
79967
+ int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
7928779968
int isRowid /* If true, LHS of IN operator is a rowid */
7928879969
){
79289
- int testAddr = -1; /* One-time test address */
79970
+ int jmpIfDynamic = -1; /* One-time test address */
7929079971
int rReg = 0; /* Register storing resulting */
7929179972
Vdbe *v = sqlite3GetVdbe(pParse);
7929279973
if( NEVER(v==0) ) return 0;
7929379974
sqlite3ExprCachePush(pParse);
7929479975
@@ -79301,17 +79982,17 @@
7930179982
**
7930279983
** If all of the above are false, then we can run this code just once
7930379984
** save the results, and reuse the same result on subsequent invocations.
7930479985
*/
7930579986
if( !ExprHasProperty(pExpr, EP_VarSelect) ){
79306
- testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
79987
+ jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
7930779988
}
7930879989
7930979990
#ifndef SQLITE_OMIT_EXPLAIN
7931079991
if( pParse->explain==2 ){
7931179992
char *zMsg = sqlite3MPrintf(
79312
- pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
79993
+ pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
7931379994
pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
7931479995
);
7931579996
sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
7931679997
}
7931779998
#endif
@@ -79321,14 +80002,10 @@
7932180002
char affinity; /* Affinity of the LHS of the IN */
7932280003
int addr; /* Address of OP_OpenEphemeral instruction */
7932380004
Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
7932480005
KeyInfo *pKeyInfo = 0; /* Key information */
7932580006
79326
- if( rMayHaveNull ){
79327
- sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
79328
- }
79329
-
7933080007
affinity = sqlite3ExprAffinity(pLeft);
7933180008
7933280009
/* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
7933380010
** expression it is handled the same way. An ephemeral table is
7933480011
** filled with single-field index keys representing the results
@@ -79350,24 +80027,27 @@
7935080027
/* Case 1: expr IN (SELECT ...)
7935180028
**
7935280029
** Generate code to write the results of the select into the temporary
7935380030
** table allocated and opened above.
7935480031
*/
80032
+ Select *pSelect = pExpr->x.pSelect;
7935580033
SelectDest dest;
7935680034
ExprList *pEList;
7935780035
7935880036
assert( !isRowid );
7935980037
sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
7936080038
dest.affSdst = (u8)affinity;
7936180039
assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
79362
- pExpr->x.pSelect->iLimit = 0;
80040
+ pSelect->iLimit = 0;
80041
+ testcase( pSelect->selFlags & SF_Distinct );
80042
+ pSelect->selFlags &= ~SF_Distinct;
7936380043
testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
79364
- if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
80044
+ if( sqlite3Select(pParse, pSelect, &dest) ){
7936580045
sqlite3KeyInfoUnref(pKeyInfo);
7936680046
return 0;
7936780047
}
79368
- pEList = pExpr->x.pSelect->pEList;
80048
+ pEList = pSelect->pEList;
7936980049
assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
7937080050
assert( pEList!=0 );
7937180051
assert( pEList->nExpr>0 );
7937280052
assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
7937380053
pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
@@ -79394,23 +80074,23 @@
7939480074
}
7939580075
7939680076
/* Loop through each expression in <exprlist>. */
7939780077
r1 = sqlite3GetTempReg(pParse);
7939880078
r2 = sqlite3GetTempReg(pParse);
79399
- sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
80079
+ if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
7940080080
for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
7940180081
Expr *pE2 = pItem->pExpr;
7940280082
int iValToIns;
7940380083
7940480084
/* If the expression is not constant then we will need to
7940580085
** disable the test that was generated above that makes sure
7940680086
** this code only executes once. Because for a non-constant
7940780087
** expression we need to rerun this code each time.
7940880088
*/
79409
- if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
79410
- sqlite3VdbeChangeToNoop(v, testAddr);
79411
- testAddr = -1;
80089
+ if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
80090
+ sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
80091
+ jmpIfDynamic = -1;
7941280092
}
7941380093
7941480094
/* Evaluate the expression and insert it into the temp table */
7941580095
if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
7941680096
sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
@@ -79476,12 +80156,16 @@
7947680156
ExprSetVVAProperty(pExpr, EP_NoReduce);
7947780157
break;
7947880158
}
7947980159
}
7948080160
79481
- if( testAddr>=0 ){
79482
- sqlite3VdbeJumpHere(v, testAddr);
80161
+ if( rHasNullFlag ){
80162
+ sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
80163
+ }
80164
+
80165
+ if( jmpIfDynamic>=0 ){
80166
+ sqlite3VdbeJumpHere(v, jmpIfDynamic);
7948380167
}
7948480168
sqlite3ExprCachePop(pParse);
7948580169
7948680170
return rReg;
7948780171
}
@@ -79498,11 +80182,11 @@
7949880182
** is an array of zero or more values. The expression is true if the LHS is
7949980183
** contained within the RHS. The value of the expression is unknown (NULL)
7950080184
** if the LHS is NULL or if the LHS is not contained within the RHS and the
7950180185
** RHS contains one or more NULL values.
7950280186
**
79503
-** This routine generates code will jump to destIfFalse if the LHS is not
80187
+** This routine generates code that jumps to destIfFalse if the LHS is not
7950480188
** contained within the RHS. If due to NULLs we cannot determine if the LHS
7950580189
** is contained in the RHS then jump to destIfNull. If the LHS is contained
7950680190
** within the RHS then fall through.
7950780191
*/
7950880192
static void sqlite3ExprCodeIN(
@@ -79521,11 +80205,13 @@
7952180205
** pExpr->iTable will contains the values that make up the RHS.
7952280206
*/
7952380207
v = pParse->pVdbe;
7952480208
assert( v!=0 ); /* OOM detected prior to this routine */
7952580209
VdbeNoopComment((v, "begin IN expr"));
79526
- eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
80210
+ eType = sqlite3FindInIndex(pParse, pExpr,
80211
+ IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
80212
+ destIfFalse==destIfNull ? 0 : &rRhsHasNull);
7952780213
7952880214
/* Figure out the affinity to use to create a key from the results
7952980215
** of the expression. affinityStr stores a static string suitable for
7953080216
** P4 of OP_MakeRecord.
7953180217
*/
@@ -79535,86 +80221,118 @@
7953580221
*/
7953680222
sqlite3ExprCachePush(pParse);
7953780223
r1 = sqlite3GetTempReg(pParse);
7953880224
sqlite3ExprCode(pParse, pExpr->pLeft, r1);
7953980225
79540
- /* If the LHS is NULL, then the result is either false or NULL depending
79541
- ** on whether the RHS is empty or not, respectively.
79542
- */
79543
- if( destIfNull==destIfFalse ){
79544
- /* Shortcut for the common case where the false and NULL outcomes are
79545
- ** the same. */
79546
- sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
79547
- }else{
79548
- int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
79549
- sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
79550
- VdbeCoverage(v);
79551
- sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79552
- sqlite3VdbeJumpHere(v, addr1);
79553
- }
79554
-
79555
- if( eType==IN_INDEX_ROWID ){
79556
- /* In this case, the RHS is the ROWID of table b-tree
79557
- */
79558
- sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
79559
- sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
79560
- VdbeCoverage(v);
79561
- }else{
79562
- /* In this case, the RHS is an index b-tree.
79563
- */
79564
- sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
79565
-
79566
- /* If the set membership test fails, then the result of the
79567
- ** "x IN (...)" expression must be either 0 or NULL. If the set
79568
- ** contains no NULL values, then the result is 0. If the set
79569
- ** contains one or more NULL values, then the result of the
79570
- ** expression is also NULL.
79571
- */
79572
- if( rRhsHasNull==0 || destIfFalse==destIfNull ){
79573
- /* This branch runs if it is known at compile time that the RHS
79574
- ** cannot contain NULL values. This happens as the result
79575
- ** of a "NOT NULL" constraint in the database schema.
79576
- **
79577
- ** Also run this branch if NULL is equivalent to FALSE
79578
- ** for this particular IN operator.
79579
- */
79580
- sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
79581
- VdbeCoverage(v);
79582
- }else{
79583
- /* In this branch, the RHS of the IN might contain a NULL and
79584
- ** the presence of a NULL on the RHS makes a difference in the
79585
- ** outcome.
79586
- */
79587
- int j1, j2;
79588
-
79589
- /* First check to see if the LHS is contained in the RHS. If so,
79590
- ** then the presence of NULLs in the RHS does not matter, so jump
79591
- ** over all of the code that follows.
79592
- */
79593
- j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
79594
- VdbeCoverage(v);
79595
-
79596
- /* Here we begin generating code that runs if the LHS is not
79597
- ** contained within the RHS. Generate additional code that
79598
- ** tests the RHS for NULLs. If the RHS contains a NULL then
79599
- ** jump to destIfNull. If there are no NULLs in the RHS then
79600
- ** jump to destIfFalse.
79601
- */
79602
- sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
79603
- sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
79604
- j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
79605
- VdbeCoverage(v);
79606
- sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
80226
+ /* If sqlite3FindInIndex() did not find or create an index that is
80227
+ ** suitable for evaluating the IN operator, then evaluate using a
80228
+ ** sequence of comparisons.
80229
+ */
80230
+ if( eType==IN_INDEX_NOOP ){
80231
+ ExprList *pList = pExpr->x.pList;
80232
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
80233
+ int labelOk = sqlite3VdbeMakeLabel(v);
80234
+ int r2, regToFree;
80235
+ int regCkNull = 0;
80236
+ int ii;
80237
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80238
+ if( destIfNull!=destIfFalse ){
80239
+ regCkNull = sqlite3GetTempReg(pParse);
80240
+ sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
80241
+ }
80242
+ for(ii=0; ii<pList->nExpr; ii++){
80243
+ r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
80244
+ if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
80245
+ sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
80246
+ }
80247
+ if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
80248
+ sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
80249
+ (void*)pColl, P4_COLLSEQ);
80250
+ VdbeCoverageIf(v, ii<pList->nExpr-1);
80251
+ VdbeCoverageIf(v, ii==pList->nExpr-1);
80252
+ sqlite3VdbeChangeP5(v, affinity);
80253
+ }else{
80254
+ assert( destIfNull==destIfFalse );
80255
+ sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
80256
+ (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
80257
+ sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
80258
+ }
80259
+ sqlite3ReleaseTempReg(pParse, regToFree);
80260
+ }
80261
+ if( regCkNull ){
80262
+ sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
7960780263
sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
79608
- sqlite3VdbeJumpHere(v, j2);
79609
- sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
79610
- sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79611
-
79612
- /* The OP_Found at the top of this branch jumps here when true,
79613
- ** causing the overall IN expression evaluation to fall through.
80264
+ }
80265
+ sqlite3VdbeResolveLabel(v, labelOk);
80266
+ sqlite3ReleaseTempReg(pParse, regCkNull);
80267
+ }else{
80268
+
80269
+ /* If the LHS is NULL, then the result is either false or NULL depending
80270
+ ** on whether the RHS is empty or not, respectively.
80271
+ */
80272
+ if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
80273
+ if( destIfNull==destIfFalse ){
80274
+ /* Shortcut for the common case where the false and NULL outcomes are
80275
+ ** the same. */
80276
+ sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
80277
+ }else{
80278
+ int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
80279
+ sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
80280
+ VdbeCoverage(v);
80281
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
80282
+ sqlite3VdbeJumpHere(v, addr1);
80283
+ }
80284
+ }
80285
+
80286
+ if( eType==IN_INDEX_ROWID ){
80287
+ /* In this case, the RHS is the ROWID of table b-tree
7961480288
*/
79615
- sqlite3VdbeJumpHere(v, j1);
80289
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
80290
+ sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
80291
+ VdbeCoverage(v);
80292
+ }else{
80293
+ /* In this case, the RHS is an index b-tree.
80294
+ */
80295
+ sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
80296
+
80297
+ /* If the set membership test fails, then the result of the
80298
+ ** "x IN (...)" expression must be either 0 or NULL. If the set
80299
+ ** contains no NULL values, then the result is 0. If the set
80300
+ ** contains one or more NULL values, then the result of the
80301
+ ** expression is also NULL.
80302
+ */
80303
+ assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
80304
+ if( rRhsHasNull==0 ){
80305
+ /* This branch runs if it is known at compile time that the RHS
80306
+ ** cannot contain NULL values. This happens as the result
80307
+ ** of a "NOT NULL" constraint in the database schema.
80308
+ **
80309
+ ** Also run this branch if NULL is equivalent to FALSE
80310
+ ** for this particular IN operator.
80311
+ */
80312
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
80313
+ VdbeCoverage(v);
80314
+ }else{
80315
+ /* In this branch, the RHS of the IN might contain a NULL and
80316
+ ** the presence of a NULL on the RHS makes a difference in the
80317
+ ** outcome.
80318
+ */
80319
+ int j1;
80320
+
80321
+ /* First check to see if the LHS is contained in the RHS. If so,
80322
+ ** then the answer is TRUE the presence of NULLs in the RHS does
80323
+ ** not matter. If the LHS is not contained in the RHS, then the
80324
+ ** answer is NULL if the RHS contains NULLs and the answer is
80325
+ ** FALSE if the RHS is NULL-free.
80326
+ */
80327
+ j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
80328
+ VdbeCoverage(v);
80329
+ sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
80330
+ VdbeCoverage(v);
80331
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
80332
+ sqlite3VdbeJumpHere(v, j1);
80333
+ }
7961680334
}
7961780335
}
7961880336
sqlite3ReleaseTempReg(pParse, r1);
7961980337
sqlite3ExprCachePop(pParse);
7962080338
VdbeComment((v, "end IN expr"));
@@ -79671,21 +80389,28 @@
7967180389
}else{
7967280390
int c;
7967380391
i64 value;
7967480392
const char *z = pExpr->u.zToken;
7967580393
assert( z!=0 );
79676
- c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
80394
+ c = sqlite3DecOrHexToI64(z, &value);
7967780395
if( c==0 || (c==2 && negFlag) ){
7967880396
char *zV;
7967980397
if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
7968080398
zV = dup8bytes(v, (char*)&value);
7968180399
sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
7968280400
}else{
7968380401
#ifdef SQLITE_OMIT_FLOATING_POINT
7968480402
sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
7968580403
#else
79686
- codeReal(v, z, negFlag, iMem);
80404
+#ifndef SQLITE_OMIT_HEX_INTEGER
80405
+ if( sqlite3_strnicmp(z,"0x",2)==0 ){
80406
+ sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
80407
+ }else
80408
+#endif
80409
+ {
80410
+ codeReal(v, z, negFlag, iMem);
80411
+ }
7968780412
#endif
7968880413
}
7968980414
}
7969080415
}
7969180416
@@ -80227,11 +80952,11 @@
8022780952
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
8022880953
testcase( regFree1==0 );
8022980954
addr = sqlite3VdbeAddOp1(v, op, r1);
8023080955
VdbeCoverageIf(v, op==TK_ISNULL);
8023180956
VdbeCoverageIf(v, op==TK_NOTNULL);
80232
- sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
80957
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
8023380958
sqlite3VdbeJumpHere(v, addr);
8023480959
break;
8023580960
}
8023680961
case TK_AGG_FUNCTION: {
8023780962
AggInfo *pInfo = pExpr->pAggInfo;
@@ -80263,11 +80988,11 @@
8026380988
nFarg = pFarg ? pFarg->nExpr : 0;
8026480989
assert( !ExprHasProperty(pExpr, EP_IntValue) );
8026580990
zId = pExpr->u.zToken;
8026680991
nId = sqlite3Strlen30(zId);
8026780992
pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
80268
- if( pDef==0 ){
80993
+ if( pDef==0 || pDef->xFunc==0 ){
8026980994
sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
8027080995
break;
8027180996
}
8027280997
8027380998
/* Attempt a direct implementation of the built-in COALESCE() and
@@ -82929,10 +83654,11 @@
8292983654
/* Open the sqlite_stat[134] tables for writing. */
8293083655
for(i=0; aTable[i].zCols; i++){
8293183656
assert( i<ArraySize(aTable) );
8293283657
sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
8293383658
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
83659
+ VdbeComment((v, aTable[i].zName));
8293483660
}
8293583661
}
8293683662
8293783663
/*
8293883664
** Recommended number of samples for sqlite_stat4
@@ -82964,11 +83690,12 @@
8296483690
#endif
8296583691
};
8296683692
struct Stat4Accum {
8296783693
tRowcnt nRow; /* Number of rows in the entire table */
8296883694
tRowcnt nPSample; /* How often to do a periodic sample */
82969
- int nCol; /* Number of columns in index + rowid */
83695
+ int nCol; /* Number of columns in index + pk/rowid */
83696
+ int nKeyCol; /* Number of index columns w/o the pk/rowid */
8297083697
int mxSample; /* Maximum number of samples to accumulate */
8297183698
Stat4Sample current; /* Current row as a Stat4Sample */
8297283699
u32 iPrn; /* Pseudo-random number used for sampling */
8297383700
Stat4Sample *aBest; /* Array of nCol best samples */
8297483701
int iMin; /* Index in a[] of entry with minimum score */
@@ -83050,13 +83777,26 @@
8305083777
#endif
8305183778
sqlite3DbFree(p->db, p);
8305283779
}
8305383780
8305483781
/*
83055
-** Implementation of the stat_init(N,C) SQL function. The two parameters
83056
-** are the number of rows in the table or index (C) and the number of columns
83057
-** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
83782
+** Implementation of the stat_init(N,K,C) SQL function. The three parameters
83783
+** are:
83784
+** N: The number of columns in the index including the rowid/pk (note 1)
83785
+** K: The number of columns in the index excluding the rowid/pk.
83786
+** C: The number of rows in the index (note 2)
83787
+**
83788
+** Note 1: In the special case of the covering index that implements a
83789
+** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
83790
+** total number of columns in the table.
83791
+**
83792
+** Note 2: C is only used for STAT3 and STAT4.
83793
+**
83794
+** For indexes on ordinary rowid tables, N==K+1. But for indexes on
83795
+** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
83796
+** PRIMARY KEY of the table. The covering index that implements the
83797
+** original WITHOUT ROWID table as N==K as a special case.
8305883798
**
8305983799
** This routine allocates the Stat4Accum object in heap memory. The return
8306083800
** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
8306183801
** the size of the blob is sizeof(void*) bytes).
8306283802
*/
@@ -83065,10 +83805,11 @@
8306583805
int argc,
8306683806
sqlite3_value **argv
8306783807
){
8306883808
Stat4Accum *p;
8306983809
int nCol; /* Number of columns in index being sampled */
83810
+ int nKeyCol; /* Number of key columns */
8307083811
int nColUp; /* nCol rounded up for alignment */
8307183812
int n; /* Bytes of space to allocate */
8307283813
sqlite3 *db; /* Database connection */
8307383814
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8307483815
int mxSample = SQLITE_STAT4_SAMPLES;
@@ -83075,12 +83816,15 @@
8307583816
#endif
8307683817
8307783818
/* Decode the three function arguments */
8307883819
UNUSED_PARAMETER(argc);
8307983820
nCol = sqlite3_value_int(argv[0]);
83080
- assert( nCol>1 ); /* >1 because it includes the rowid column */
83821
+ assert( nCol>0 );
8308183822
nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
83823
+ nKeyCol = sqlite3_value_int(argv[1]);
83824
+ assert( nKeyCol<=nCol );
83825
+ assert( nKeyCol>0 );
8308283826
8308383827
/* Allocate the space required for the Stat4Accum object */
8308483828
n = sizeof(*p)
8308583829
+ sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
8308683830
+ sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
@@ -83098,10 +83842,11 @@
8309883842
}
8309983843
8310083844
p->db = db;
8310183845
p->nRow = 0;
8310283846
p->nCol = nCol;
83847
+ p->nKeyCol = nKeyCol;
8310383848
p->current.anDLt = (tRowcnt*)&p[1];
8310483849
p->current.anEq = &p->current.anDLt[nColUp];
8310583850
8310683851
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8310783852
{
@@ -83108,13 +83853,13 @@
8310883853
u8 *pSpace; /* Allocated space not yet assigned */
8310983854
int i; /* Used to iterate through p->aSample[] */
8311083855
8311183856
p->iGet = -1;
8311283857
p->mxSample = mxSample;
83113
- p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
83858
+ p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
8311483859
p->current.anLt = &p->current.anEq[nColUp];
83115
- p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
83860
+ p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[2])*0xd0944565;
8311683861
8311783862
/* Set up the Stat4Accum.a[] and aBest[] arrays */
8311883863
p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
8311983864
p->aBest = &p->a[mxSample];
8312083865
pSpace = (u8*)(&p->a[mxSample+nCol]);
@@ -83133,11 +83878,11 @@
8313383878
8313483879
/* Return a pointer to the allocated object to the caller */
8313583880
sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
8313683881
}
8313783882
static const FuncDef statInitFuncdef = {
83138
- 1+IsStat34, /* nArg */
83883
+ 2+IsStat34, /* nArg */
8313983884
SQLITE_UTF8, /* funcFlags */
8314083885
0, /* pUserData */
8314183886
0, /* pNext */
8314283887
statInit, /* xFunc */
8314383888
0, /* xStep */
@@ -83357,11 +84102,14 @@
8335784102
** P Pointer to the Stat4Accum object created by stat_init()
8335884103
** C Index of left-most column to differ from previous row
8335984104
** R Rowid for the current row. Might be a key record for
8336084105
** WITHOUT ROWID tables.
8336184106
**
83362
-** The SQL function always returns NULL.
84107
+** This SQL function always returns NULL. It's purpose it to accumulate
84108
+** statistical data and/or samples in the Stat4Accum object about the
84109
+** index being analyzed. The stat_get() SQL function will later be used to
84110
+** extract relevant information for constructing the sqlite_statN tables.
8336384111
**
8336484112
** The R parameter is only used for STAT3 and STAT4
8336584113
*/
8336684114
static void statPush(
8336784115
sqlite3_context *context,
@@ -83374,11 +84122,11 @@
8337484122
Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
8337584123
int iChng = sqlite3_value_int(argv[1]);
8337684124
8337784125
UNUSED_PARAMETER( argc );
8337884126
UNUSED_PARAMETER( context );
83379
- assert( p->nCol>1 ); /* Includes rowid field */
84127
+ assert( p->nCol>0 );
8338084128
assert( iChng<p->nCol );
8338184129
8338284130
if( p->nRow==0 ){
8338384131
/* This is the first call to this function. Do initialization. */
8338484132
for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
@@ -83451,11 +84199,14 @@
8345184199
#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
8345284200
#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
8345384201
8345484202
/*
8345584203
** Implementation of the stat_get(P,J) SQL function. This routine is
83456
-** used to query the results. Content is returned for parameter J
84204
+** used to query statistical information that has been gathered into
84205
+** the Stat4Accum object by prior calls to stat_push(). The P parameter
84206
+** is a BLOB which is decoded into a pointer to the Stat4Accum objects.
84207
+** The content to returned is determined by the parameter J
8345784208
** which is one of the STAT_GET_xxxx values defined above.
8345884209
**
8345984210
** If neither STAT3 nor STAT4 are enabled, then J is always
8346084211
** STAT_GET_STAT1 and is hence omitted and this routine becomes
8346184212
** a one-parameter function, stat_get(P), that always returns the
@@ -83502,19 +84253,19 @@
8350284253
** I = (K+D-1)/D
8350384254
*/
8350484255
char *z;
8350584256
int i;
8350684257
83507
- char *zRet = sqlite3MallocZero(p->nCol * 25);
84258
+ char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
8350884259
if( zRet==0 ){
8350984260
sqlite3_result_error_nomem(context);
8351084261
return;
8351184262
}
8351284263
8351384264
sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
8351484265
z = zRet + sqlite3Strlen30(zRet);
83515
- for(i=0; i<(p->nCol-1); i++){
84266
+ for(i=0; i<p->nKeyCol; i++){
8351684267
u64 nDistinct = p->current.anDLt[i] + 1;
8351784268
u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
8351884269
sqlite3_snprintf(24, z, " %llu", iVal);
8351984270
z += sqlite3Strlen30(z);
8352084271
assert( p->current.anEq[i] );
@@ -83670,31 +84421,31 @@
8367084421
pParse->nTab = MAX(pParse->nTab, iTab);
8367184422
sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
8367284423
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
8367384424
8367484425
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
83675
- int nCol; /* Number of columns indexed by pIdx */
83676
- int *aGotoChng; /* Array of jump instruction addresses */
84426
+ int nCol; /* Number of columns in pIdx. "N" */
8367784427
int addrRewind; /* Address of "OP_Rewind iIdxCur" */
83678
- int addrGotoChng0; /* Address of "Goto addr_chng_0" */
8367984428
int addrNextRow; /* Address of "next_row:" */
8368084429
const char *zIdxName; /* Name of the index */
84430
+ int nColTest; /* Number of columns to test for changes */
8368184431
8368284432
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
8368384433
if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
83684
- VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
83685
- nCol = pIdx->nKeyCol;
83686
- aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
83687
- if( aGotoChng==0 ) continue;
84434
+ if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
84435
+ nCol = pIdx->nKeyCol;
84436
+ zIdxName = pTab->zName;
84437
+ nColTest = nCol - 1;
84438
+ }else{
84439
+ nCol = pIdx->nColumn;
84440
+ zIdxName = pIdx->zName;
84441
+ nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
84442
+ }
8368884443
8368984444
/* Populate the register containing the index name. */
83690
- if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
83691
- zIdxName = pTab->zName;
83692
- }else{
83693
- zIdxName = pIdx->zName;
83694
- }
8369584445
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
84446
+ VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
8369684447
8369784448
/*
8369884449
** Pseudo-code for loop that calls stat_push():
8369984450
**
8370084451
** Rewind csr
@@ -83715,11 +84466,11 @@
8371584466
** regPrev(0) = idx(0)
8371684467
** chng_addr_1:
8371784468
** regPrev(1) = idx(1)
8371884469
** ...
8371984470
**
83720
- ** chng_addr_N:
84471
+ ** endDistinctTest:
8372184472
** regRowid = idx(rowid)
8372284473
** stat_push(P, regChng, regRowid)
8372384474
** Next csr
8372484475
** if !eof(csr) goto next_row;
8372584476
**
@@ -83728,32 +84479,36 @@
8372884479
8372984480
/* Make sure there are enough memory cells allocated to accommodate
8373084481
** the regPrev array and a trailing rowid (the rowid slot is required
8373184482
** when building a record to insert into the sample column of
8373284483
** the sqlite_stat4 table. */
83733
- pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
84484
+ pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
8373484485
8373584486
/* Open a read-only cursor on the index being analyzed. */
8373684487
assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
8373784488
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
8373884489
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
8373984490
VdbeComment((v, "%s", pIdx->zName));
8374084491
8374184492
/* Invoke the stat_init() function. The arguments are:
8374284493
**
83743
- ** (1) the number of columns in the index including the rowid,
83744
- ** (2) the number of rows in the index,
84494
+ ** (1) the number of columns in the index including the rowid
84495
+ ** (or for a WITHOUT ROWID table, the number of PK columns),
84496
+ ** (2) the number of columns in the key without the rowid/pk
84497
+ ** (3) the number of rows in the index,
8374584498
**
83746
- ** The second argument is only used for STAT3 and STAT4
84499
+ **
84500
+ ** The third argument is only used for STAT3 and STAT4
8374784501
*/
8374884502
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83749
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
84503
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
8375084504
#endif
83751
- sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
84505
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
84506
+ sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
8375284507
sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
8375384508
sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
83754
- sqlite3VdbeChangeP5(v, 1+IsStat34);
84509
+ sqlite3VdbeChangeP5(v, 2+IsStat34);
8375584510
8375684511
/* Implementation of the following:
8375784512
**
8375884513
** Rewind csr
8375984514
** if eof(csr) goto end_of_scan;
@@ -83762,56 +84517,73 @@
8376284517
**
8376384518
*/
8376484519
addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
8376584520
VdbeCoverage(v);
8376684521
sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
83767
- addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
83768
-
83769
- /*
83770
- ** next_row:
83771
- ** regChng = 0
83772
- ** if( idx(0) != regPrev(0) ) goto chng_addr_0
83773
- ** regChng = 1
83774
- ** if( idx(1) != regPrev(1) ) goto chng_addr_1
83775
- ** ...
83776
- ** regChng = N
83777
- ** goto chng_addr_N
83778
- */
8377984522
addrNextRow = sqlite3VdbeCurrentAddr(v);
83780
- for(i=0; i<nCol; i++){
83781
- char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
83782
- sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
83783
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
83784
- aGotoChng[i] =
83785
- sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
83786
- sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
83787
- VdbeCoverage(v);
83788
- }
83789
- sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
83790
- aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
83791
-
83792
- /*
83793
- ** chng_addr_0:
83794
- ** regPrev(0) = idx(0)
83795
- ** chng_addr_1:
83796
- ** regPrev(1) = idx(1)
83797
- ** ...
83798
- */
83799
- sqlite3VdbeJumpHere(v, addrGotoChng0);
83800
- for(i=0; i<nCol; i++){
83801
- sqlite3VdbeJumpHere(v, aGotoChng[i]);
83802
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
83803
- }
83804
-
84523
+
84524
+ if( nColTest>0 ){
84525
+ int endDistinctTest = sqlite3VdbeMakeLabel(v);
84526
+ int *aGotoChng; /* Array of jump instruction addresses */
84527
+ aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
84528
+ if( aGotoChng==0 ) continue;
84529
+
84530
+ /*
84531
+ ** next_row:
84532
+ ** regChng = 0
84533
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0
84534
+ ** regChng = 1
84535
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1
84536
+ ** ...
84537
+ ** regChng = N
84538
+ ** goto endDistinctTest
84539
+ */
84540
+ sqlite3VdbeAddOp0(v, OP_Goto);
84541
+ addrNextRow = sqlite3VdbeCurrentAddr(v);
84542
+ if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
84543
+ /* For a single-column UNIQUE index, once we have found a non-NULL
84544
+ ** row, we know that all the rest will be distinct, so skip
84545
+ ** subsequent distinctness tests. */
84546
+ sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
84547
+ VdbeCoverage(v);
84548
+ }
84549
+ for(i=0; i<nColTest; i++){
84550
+ char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
84551
+ sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
84552
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
84553
+ aGotoChng[i] =
84554
+ sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
84555
+ sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
84556
+ VdbeCoverage(v);
84557
+ }
84558
+ sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
84559
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, endDistinctTest);
84560
+
84561
+
84562
+ /*
84563
+ ** chng_addr_0:
84564
+ ** regPrev(0) = idx(0)
84565
+ ** chng_addr_1:
84566
+ ** regPrev(1) = idx(1)
84567
+ ** ...
84568
+ */
84569
+ sqlite3VdbeJumpHere(v, addrNextRow-1);
84570
+ for(i=0; i<nColTest; i++){
84571
+ sqlite3VdbeJumpHere(v, aGotoChng[i]);
84572
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
84573
+ }
84574
+ sqlite3VdbeResolveLabel(v, endDistinctTest);
84575
+ sqlite3DbFree(db, aGotoChng);
84576
+ }
84577
+
8380584578
/*
8380684579
** chng_addr_N:
8380784580
** regRowid = idx(rowid) // STAT34 only
8380884581
** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
8380984582
** Next csr
8381084583
** if !eof(csr) goto next_row;
8381184584
*/
83812
- sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
8381384585
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8381484586
assert( regRowid==(regStat4+2) );
8381584587
if( HasRowid(pTab) ){
8381684588
sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
8381784589
}else{
@@ -83851,11 +84623,11 @@
8385184623
int regSampleRowid = regCol + nCol;
8385284624
int addrNext;
8385384625
int addrIsNull;
8385484626
u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
8385584627
83856
- pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
84628
+ pParse->nMem = MAX(pParse->nMem, regCol+nCol);
8385784629
8385884630
addrNext = sqlite3VdbeCurrentAddr(v);
8385984631
callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
8386084632
addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
8386184633
VdbeCoverage(v);
@@ -83873,11 +84645,11 @@
8387384645
#else
8387484646
for(i=0; i<nCol; i++){
8387584647
i16 iCol = pIdx->aiColumn[i];
8387684648
sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
8387784649
}
83878
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
84650
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
8387984651
#endif
8388084652
sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
8388184653
sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
8388284654
sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
8388384655
sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
@@ -83885,11 +84657,10 @@
8388584657
}
8388684658
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8388784659
8388884660
/* End of analysis */
8388984661
sqlite3VdbeJumpHere(v, addrRewind);
83890
- sqlite3DbFree(db, aGotoChng);
8389184662
}
8389284663
8389384664
8389484665
/* Create a single sqlite_stat1 entry containing NULL as the index
8389584666
** name and the row count as the content.
@@ -83986,10 +84757,11 @@
8398684757
int i;
8398784758
char *z, *zDb;
8398884759
Table *pTab;
8398984760
Index *pIdx;
8399084761
Token *pTableName;
84762
+ Vdbe *v;
8399184763
8399284764
/* Read the database schema. If an error occurs, leave an error message
8399384765
** and code in pParse and return NULL. */
8399484766
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
8399584767
if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
@@ -84033,10 +84805,12 @@
8403384805
}
8403484806
sqlite3DbFree(db, z);
8403584807
}
8403684808
}
8403784809
}
84810
+ v = sqlite3GetVdbe(pParse);
84811
+ if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
8403884812
}
8403984813
8404084814
/*
8404184815
** Used to pass information from the analyzer reader through to the
8404284816
** callback routine.
@@ -84091,18 +84865,23 @@
8409184865
#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
8409284866
assert( pIndex!=0 );
8409384867
#else
8409484868
if( pIndex )
8409584869
#endif
84096
- {
84097
- if( strcmp(z, "unordered")==0 ){
84870
+ while( z[0] ){
84871
+ if( sqlite3_strglob("unordered*", z)==0 ){
8409884872
pIndex->bUnordered = 1;
8409984873
}else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
84100
- int v32 = 0;
84101
- sqlite3GetInt32(z+3, &v32);
84102
- pIndex->szIdxRow = sqlite3LogEst(v32);
84874
+ pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
8410384875
}
84876
+#ifdef SQLITE_ENABLE_COSTMULT
84877
+ else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
84878
+ pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
84879
+ }
84880
+#endif
84881
+ while( z[0]!=0 && z[0]!=' ' ) z++;
84882
+ while( z[0]==' ' ) z++;
8410484883
}
8410584884
}
8410684885
8410784886
/*
8410884887
** This callback is invoked once for each index when reading the
@@ -84139,15 +84918,19 @@
8413984918
pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
8414084919
}
8414184920
z = argv[2];
8414284921
8414384922
if( pIndex ){
84923
+ pIndex->bUnordered = 0;
8414484924
decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
8414584925
if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
8414684926
}else{
8414784927
Index fakeIdx;
8414884928
fakeIdx.szIdxRow = pTable->szTabRow;
84929
+#ifdef SQLITE_ENABLE_COSTMULT
84930
+ fakeIdx.pTable = pTable;
84931
+#endif
8414984932
decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
8415084933
pTable->szTabRow = fakeIdx.szIdxRow;
8415184934
}
8415284935
8415384936
return 0;
@@ -84185,11 +84968,20 @@
8418584968
static void initAvgEq(Index *pIdx){
8418684969
if( pIdx ){
8418784970
IndexSample *aSample = pIdx->aSample;
8418884971
IndexSample *pFinal = &aSample[pIdx->nSample-1];
8418984972
int iCol;
84190
- for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
84973
+ int nCol = 1;
84974
+ if( pIdx->nSampleCol>1 ){
84975
+ /* If this is stat4 data, then calculate aAvgEq[] values for all
84976
+ ** sample columns except the last. The last is always set to 1, as
84977
+ ** once the trailing PK fields are considered all index keys are
84978
+ ** unique. */
84979
+ nCol = pIdx->nSampleCol-1;
84980
+ pIdx->aAvgEq[nCol] = 1;
84981
+ }
84982
+ for(iCol=0; iCol<nCol; iCol++){
8419184983
int i; /* Used to iterate through samples */
8419284984
tRowcnt sumEq = 0; /* Sum of the nEq values */
8419384985
tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
8419484986
tRowcnt avgEq = 0;
8419584987
tRowcnt nDLt = pFinal->anDLt[iCol];
@@ -84208,11 +85000,10 @@
8420885000
if( nDLt>nSum ){
8420985001
avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
8421085002
}
8421185003
if( avgEq==0 ) avgEq = 1;
8421285004
pIdx->aAvgEq[iCol] = avgEq;
84213
- if( pIdx->nSampleCol==1 ) break;
8421485005
}
8421585006
}
8421685007
}
8421785008
8421885009
/*
@@ -84267,11 +85058,10 @@
8426785058
sqlite3DbFree(db, zSql);
8426885059
if( rc ) return rc;
8426985060
8427085061
while( sqlite3_step(pStmt)==SQLITE_ROW ){
8427185062
int nIdxCol = 1; /* Number of columns in stat4 records */
84272
- int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
8427385063
8427485064
char *zIndex; /* Index name */
8427585065
Index *pIdx; /* Pointer to the index object */
8427685066
int nSample; /* Number of samples */
8427785067
int nByte; /* Bytes of space required */
@@ -84285,25 +85075,29 @@
8428585075
assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
8428685076
/* Index.nSample is non-zero at this point if data has already been
8428785077
** loaded from the stat4 table. In this case ignore stat3 data. */
8428885078
if( pIdx==0 || pIdx->nSample ) continue;
8428985079
if( bStat3==0 ){
84290
- nIdxCol = pIdx->nKeyCol+1;
84291
- nAvgCol = pIdx->nKeyCol;
85080
+ assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
85081
+ if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
85082
+ nIdxCol = pIdx->nKeyCol;
85083
+ }else{
85084
+ nIdxCol = pIdx->nColumn;
85085
+ }
8429285086
}
8429385087
pIdx->nSampleCol = nIdxCol;
8429485088
nByte = sizeof(IndexSample) * nSample;
8429585089
nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
84296
- nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
85090
+ nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
8429785091
8429885092
pIdx->aSample = sqlite3DbMallocZero(db, nByte);
8429985093
if( pIdx->aSample==0 ){
8430085094
sqlite3_finalize(pStmt);
8430185095
return SQLITE_NOMEM;
8430285096
}
8430385097
pSpace = (tRowcnt*)&pIdx->aSample[nSample];
84304
- pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
85098
+ pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
8430585099
for(i=0; i<nSample; i++){
8430685100
pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
8430785101
pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
8430885102
pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
8430985103
}
@@ -85408,10 +86202,23 @@
8540886202
}
8540986203
#else
8541086204
#define codeTableLocks(x)
8541186205
#endif
8541286206
86207
+/*
86208
+** Return TRUE if the given yDbMask object is empty - if it contains no
86209
+** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
86210
+** macros when SQLITE_MAX_ATTACHED is greater than 30.
86211
+*/
86212
+#if SQLITE_MAX_ATTACHED>30
86213
+SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
86214
+ int i;
86215
+ for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
86216
+ return 1;
86217
+}
86218
+#endif
86219
+
8541386220
/*
8541486221
** This routine is called after a single SQL statement has been
8541586222
** parsed and a VDBE program to execute that statement has been
8541686223
** prepared. This routine puts the finishing touches on the
8541786224
** VDBE program and resets the pParse structure for the next
@@ -85444,22 +86251,23 @@
8544486251
** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
8544586252
** set for each database that is used. Generate code to start a
8544686253
** transaction on each used database and to verify the schema cookie
8544786254
** on each used database.
8544886255
*/
85449
- if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
85450
- yDbMask mask;
86256
+ if( db->mallocFailed==0
86257
+ && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
86258
+ ){
8545186259
int iDb, i;
8545286260
assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
8545386261
sqlite3VdbeJumpHere(v, 0);
85454
- for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
85455
- if( (mask & pParse->cookieMask)==0 ) continue;
86262
+ for(iDb=0; iDb<db->nDb; iDb++){
86263
+ if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
8545686264
sqlite3VdbeUsesBtree(v, iDb);
8545786265
sqlite3VdbeAddOp4Int(v,
8545886266
OP_Transaction, /* Opcode */
8545986267
iDb, /* P1 */
85460
- (mask & pParse->writeMask)!=0, /* P2 */
86268
+ DbMaskTest(pParse->writeMask,iDb), /* P2 */
8546186269
pParse->cookieValue[iDb], /* P3 */
8546286270
db->aDb[iDb].pSchema->iGeneration /* P4 */
8546386271
);
8546486272
if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
8546586273
}
@@ -85511,11 +86319,11 @@
8551186319
}
8551286320
pParse->nTab = 0;
8551386321
pParse->nMem = 0;
8551486322
pParse->nSet = 0;
8551586323
pParse->nVar = 0;
85516
- pParse->cookieMask = 0;
86324
+ DbMaskZero(pParse->cookieMask);
8551786325
}
8551886326
8551986327
/*
8552086328
** Run the parser and code generator recursively in order to generate
8552186329
** code for the SQL statement given onto the end of the pParse context
@@ -87411,11 +88219,11 @@
8741188219
pTable->aCol = pSelTab->aCol;
8741288220
pSelTab->nCol = 0;
8741388221
pSelTab->aCol = 0;
8741488222
sqlite3DeleteTable(db, pSelTab);
8741588223
assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
87416
- pTable->pSchema->flags |= DB_UnresetViews;
88224
+ pTable->pSchema->schemaFlags |= DB_UnresetViews;
8741788225
}else{
8741888226
pTable->nCol = 0;
8741988227
nErr++;
8742088228
}
8742188229
sqlite3SelectDelete(db, pSel);
@@ -87988,16 +88796,16 @@
8798888796
(char *)pKey, P4_KEYINFO);
8798988797
sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
8799088798
8799188799
addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
8799288800
assert( pKey!=0 || db->mallocFailed || pParse->nErr );
87993
- if( pIndex->onError!=OE_None && pKey!=0 ){
88801
+ if( IsUniqueIndex(pIndex) && pKey!=0 ){
8799488802
int j2 = sqlite3VdbeCurrentAddr(v) + 3;
8799588803
sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
8799688804
addr2 = sqlite3VdbeCurrentAddr(v);
8799788805
sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
87998
- pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
88806
+ pIndex->nKeyCol); VdbeCoverage(v);
8799988807
sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
8800088808
}else{
8800188809
addr2 = sqlite3VdbeCurrentAddr(v);
8800288810
}
8800388811
sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
@@ -88385,13 +89193,13 @@
8838589193
** considered distinct and both result in separate indices.
8838689194
*/
8838789195
Index *pIdx;
8838889196
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
8838989197
int k;
88390
- assert( pIdx->onError!=OE_None );
89198
+ assert( IsUniqueIndex(pIdx) );
8839189199
assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
88392
- assert( pIndex->onError!=OE_None );
89200
+ assert( IsUniqueIndex(pIndex) );
8839389201
8839489202
if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
8839589203
for(k=0; k<pIdx->nKeyCol; k++){
8839689204
const char *z1;
8839789205
const char *z2;
@@ -88578,11 +89386,11 @@
8857889386
for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
8857989387
a[i] = 23; assert( 23==sqlite3LogEst(5) );
8858089388
}
8858189389
8858289390
assert( 0==sqlite3LogEst(1) );
88583
- if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
89391
+ if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
8858489392
}
8858589393
8858689394
/*
8858789395
** This routine will drop an existing named index. This routine
8858889396
** implements the DROP INDEX statement.
@@ -89138,19 +89946,17 @@
8913889946
** later, by sqlite3FinishCoding().
8913989947
*/
8914089948
SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
8914189949
Parse *pToplevel = sqlite3ParseToplevel(pParse);
8914289950
sqlite3 *db = pToplevel->db;
89143
- yDbMask mask;
8914489951
8914589952
assert( iDb>=0 && iDb<db->nDb );
8914689953
assert( db->aDb[iDb].pBt!=0 || iDb==1 );
8914789954
assert( iDb<SQLITE_MAX_ATTACHED+2 );
8914889955
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89149
- mask = ((yDbMask)1)<<iDb;
89150
- if( (pToplevel->cookieMask & mask)==0 ){
89151
- pToplevel->cookieMask |= mask;
89956
+ if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
89957
+ DbMaskSet(pToplevel->cookieMask, iDb);
8915289958
pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
8915389959
if( !OMIT_TEMPDB && iDb==1 ){
8915489960
sqlite3OpenTempDatabase(pToplevel);
8915589961
}
8915689962
}
@@ -89185,11 +89991,11 @@
8918589991
** necessary to undo a write and the checkpoint should not be set.
8918689992
*/
8918789993
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
8918889994
Parse *pToplevel = sqlite3ParseToplevel(pParse);
8918989995
sqlite3CodeVerifySchema(pParse, iDb);
89190
- pToplevel->writeMask |= ((yDbMask)1)<<iDb;
89996
+ DbMaskSet(pToplevel->writeMask, iDb);
8919189997
pToplevel->isMultiWrite |= setStatement;
8919289998
}
8919389999
8919490000
/*
8919590001
** Indicate that the statement currently under construction might write
@@ -89992,13 +90798,13 @@
8999290798
sqlite3DeleteTable(0, pTab);
8999390799
}
8999490800
sqlite3HashClear(&temp1);
8999590801
sqlite3HashClear(&pSchema->fkeyHash);
8999690802
pSchema->pSeqTab = 0;
89997
- if( pSchema->flags & DB_SchemaLoaded ){
90803
+ if( pSchema->schemaFlags & DB_SchemaLoaded ){
8999890804
pSchema->iGeneration++;
89999
- pSchema->flags &= ~DB_SchemaLoaded;
90805
+ pSchema->schemaFlags &= ~DB_SchemaLoaded;
9000090806
}
9000190807
}
9000290808
9000390809
/*
9000490810
** Find and return the schema associated with a BTree. Create
@@ -90873,16 +91679,13 @@
9087391679
** May you do good and not evil.
9087491680
** May you find forgiveness for yourself and forgive others.
9087591681
** May you share freely, never taking more than you give.
9087691682
**
9087791683
*************************************************************************
90878
-** This file contains the C functions that implement various SQL
90879
-** functions of SQLite.
90880
-**
90881
-** There is only one exported symbol in this file - the function
90882
-** sqliteRegisterBuildinFunctions() found at the bottom of the file.
90883
-** All other code has file scope.
91684
+** This file contains the C-language implementions for many of the SQL
91685
+** functions of SQLite. (Some function, and in particular the date and
91686
+** time functions, are implemented separately.)
9088491687
*/
9088591688
/* #include <stdlib.h> */
9088691689
/* #include <assert.h> */
9088791690
9088891691
/*
@@ -92553,10 +93356,11 @@
9255393356
FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
9255493357
FUNCTION(hex, 1, 0, 0, hexFunc ),
9255593358
FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
9255693359
FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
9255793360
FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
93361
+ FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
9255893362
VFUNCTION(random, 0, 0, 0, randomFunc ),
9255993363
VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
9256093364
FUNCTION(nullif, 2, 0, 1, nullifFunc ),
9256193365
FUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
9256293366
FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
@@ -92839,11 +93643,11 @@
9283993643
if( !aiCol ) return 1;
9284093644
*paiCol = aiCol;
9284193645
}
9284293646
9284393647
for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
92844
- if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){
93648
+ if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){
9284593649
/* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
9284693650
** of columns. If each indexed column corresponds to a foreign key
9284793651
** column of pFKey, then this index is a winner. */
9284893652
9284993653
if( zKey==0 ){
@@ -95865,11 +96669,11 @@
9586596669
){
9586696670
return 0; /* Default values must be the same for all columns */
9586796671
}
9586896672
}
9586996673
for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95870
- if( pDestIdx->onError!=OE_None ){
96674
+ if( IsUniqueIndex(pDestIdx) ){
9587196675
destHasUniqueIdx = 1;
9587296676
}
9587396677
for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
9587496678
if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
9587596679
}
@@ -96095,10 +96899,13 @@
9609596899
goto exec_out;
9609696900
}
9609796901
}
9609896902
}
9609996903
if( xCallback(pArg, nCol, azVals, azCols) ){
96904
+ /* EVIDENCE-OF: R-38229-40159 If the callback function to
96905
+ ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
96906
+ ** return SQLITE_ABORT. */
9610096907
rc = SQLITE_ABORT;
9610196908
sqlite3VdbeFinalize((Vdbe *)pStmt);
9610296909
pStmt = 0;
9610396910
sqlite3Error(db, SQLITE_ABORT, 0);
9610496911
goto exec_out;
@@ -97872,11 +98679,11 @@
9787298679
** Note that the values returned are one less that the values that
9787398680
** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
9787498681
** to support legacy SQL code. The safety level used to be boolean
9787598682
** and older scripts may have used numbers 0 for OFF and 1 for ON.
9787698683
*/
97877
-static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
98684
+static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
9787898685
/* 123456789 123456789 */
9787998686
static const char zText[] = "onoffalseyestruefull";
9788098687
static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
9788198688
static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
9788298689
static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
@@ -97894,11 +98701,11 @@
9789498701
}
9789598702
9789698703
/*
9789798704
** Interpret the given string as a boolean value.
9789898705
*/
97899
-SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
98706
+SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
9790098707
return getSafetyLevel(z,1,dflt)!=0;
9790198708
}
9790298709
9790398710
/* The sqlite3GetBoolean() function is used by other modules but the
9790498711
** remainder of this file is specific to PRAGMA processing. So omit
@@ -98440,11 +99247,11 @@
9844099247
*/
9844199248
case PragTyp_JOURNAL_SIZE_LIMIT: {
9844299249
Pager *pPager = sqlite3BtreePager(pDb->pBt);
9844399250
i64 iLimit = -2;
9844499251
if( zRight ){
98445
- sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
99252
+ sqlite3DecOrHexToI64(zRight, &iLimit);
9844699253
if( iLimit<-1 ) iLimit = -1;
9844799254
}
9844899255
iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
9844999256
returnSingleInt(pParse, "journal_size_limit", iLimit);
9845099257
break;
@@ -98568,11 +99375,11 @@
9856899375
sqlite3_int64 sz;
9856999376
#if SQLITE_MAX_MMAP_SIZE>0
9857099377
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9857199378
if( zRight ){
9857299379
int ii;
98573
- sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
99380
+ sqlite3DecOrHexToI64(zRight, &sz);
9857499381
if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
9857599382
if( pId2->n==0 ) db->szMmap = sz;
9857699383
for(ii=db->nDb-1; ii>=0; ii--){
9857799384
if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9857899385
sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -98936,11 +99743,11 @@
9893699743
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
9893799744
sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
9893899745
for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
9893999746
sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
9894099747
sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98941
- sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
99748
+ sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
9894299749
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
9894399750
}
9894499751
}
9894599752
}
9894699753
break;
@@ -99186,13 +99993,12 @@
9918699993
** messages have been generated, output OK. Otherwise output the
9918799994
** error message
9918899995
*/
9918999996
static const int iLn = VDBE_OFFSET_LINENO(2);
9919099997
static const VdbeOpList endCode[] = {
99191
- { OP_AddImm, 1, 0, 0}, /* 0 */
99192
- { OP_IfNeg, 1, 0, 0}, /* 1 */
99193
- { OP_String8, 0, 3, 0}, /* 2 */
99998
+ { OP_IfNeg, 1, 0, 0}, /* 0 */
99999
+ { OP_String8, 0, 3, 0}, /* 1 */
99194100000
{ OP_ResultRow, 3, 1, 0},
99195100001
};
99196100002
99197100003
int isQuick = (sqlite3Tolower(zLeft[0])=='q');
99198100004
@@ -99300,32 +100106,80 @@
99300100106
sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
99301100107
}
99302100108
pParse->nMem = MAX(pParse->nMem, 8+j);
99303100109
sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
99304100110
loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
100111
+ /* Verify that all NOT NULL columns really are NOT NULL */
100112
+ for(j=0; j<pTab->nCol; j++){
100113
+ char *zErr;
100114
+ int jmp2, jmp3;
100115
+ if( j==pTab->iPKey ) continue;
100116
+ if( pTab->aCol[j].notNull==0 ) continue;
100117
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
100118
+ sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
100119
+ jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
100120
+ sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
100121
+ zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
100122
+ pTab->aCol[j].zName);
100123
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
100124
+ sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
100125
+ jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
100126
+ sqlite3VdbeAddOp0(v, OP_Halt);
100127
+ sqlite3VdbeJumpHere(v, jmp2);
100128
+ sqlite3VdbeJumpHere(v, jmp3);
100129
+ }
100130
+ /* Validate index entries for the current row */
99305100131
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
99306
- int jmp2, jmp3, jmp4;
100132
+ int jmp2, jmp3, jmp4, jmp5;
100133
+ int ckUniq = sqlite3VdbeMakeLabel(v);
99307100134
if( pPk==pIdx ) continue;
99308100135
r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
99309100136
pPrior, r1);
99310100137
pPrior = pIdx;
99311100138
sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
99312
- jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
100139
+ /* Verify that an index entry exists for the current table row */
100140
+ jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
99313100141
pIdx->nColumn); VdbeCoverage(v);
99314100142
sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
99315100143
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
99316100144
sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
99317
- sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
99318
- P4_STATIC);
100145
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
100146
+ " missing from index ", P4_STATIC);
99319100147
sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
99320
- sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
100148
+ jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
100149
+ pIdx->zName, P4_TRANSIENT);
99321100150
sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
99322100151
sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
99323100152
jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
99324100153
sqlite3VdbeAddOp0(v, OP_Halt);
99325
- sqlite3VdbeJumpHere(v, jmp4);
99326100154
sqlite3VdbeJumpHere(v, jmp2);
100155
+ /* For UNIQUE indexes, verify that only one entry exists with the
100156
+ ** current key. The entry is unique if (1) any column is NULL
100157
+ ** or (2) the next entry has a different key */
100158
+ if( IsUniqueIndex(pIdx) ){
100159
+ int uniqOk = sqlite3VdbeMakeLabel(v);
100160
+ int jmp6;
100161
+ int kk;
100162
+ for(kk=0; kk<pIdx->nKeyCol; kk++){
100163
+ int iCol = pIdx->aiColumn[kk];
100164
+ assert( iCol>=0 && iCol<pTab->nCol );
100165
+ if( pTab->aCol[iCol].notNull ) continue;
100166
+ sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
100167
+ VdbeCoverage(v);
100168
+ }
100169
+ jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
100170
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
100171
+ sqlite3VdbeJumpHere(v, jmp6);
100172
+ sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
100173
+ pIdx->nKeyCol); VdbeCoverage(v);
100174
+ sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
100175
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
100176
+ "non-unique entry in index ", P4_STATIC);
100177
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
100178
+ sqlite3VdbeResolveLabel(v, uniqOk);
100179
+ }
100180
+ sqlite3VdbeJumpHere(v, jmp4);
99327100181
sqlite3ResolvePartIdxLabel(pParse, jmp3);
99328100182
}
99329100183
sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
99330100184
sqlite3VdbeJumpHere(v, loopTop-1);
99331100185
#ifndef SQLITE_OMIT_BTREECOUNT
@@ -99346,13 +100200,13 @@
99346100200
}
99347100201
#endif /* SQLITE_OMIT_BTREECOUNT */
99348100202
}
99349100203
}
99350100204
addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
99351
- sqlite3VdbeChangeP2(v, addr, -mxErr);
99352
- sqlite3VdbeJumpHere(v, addr+1);
99353
- sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
100205
+ sqlite3VdbeChangeP3(v, addr, -mxErr);
100206
+ sqlite3VdbeJumpHere(v, addr);
100207
+ sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
99354100208
}
99355100209
break;
99356100210
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
99357100211
99358100212
#ifndef SQLITE_OMIT_UTF16
@@ -99611,11 +100465,11 @@
99611100465
** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
99612100466
** use -1.
99613100467
*/
99614100468
case PragTyp_SOFT_HEAP_LIMIT: {
99615100469
sqlite3_int64 N;
99616
- if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
100470
+ if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
99617100471
sqlite3_soft_heap_limit64(N);
99618100472
}
99619100473
returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
99620100474
break;
99621100475
}
@@ -101134,12 +101988,11 @@
101134101988
int iOffset, /* Register holding the offset counter */
101135101989
int iContinue /* Jump here to skip the current record */
101136101990
){
101137101991
if( iOffset>0 ){
101138101992
int addr;
101139
- sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
101140
- addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
101993
+ addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
101141101994
sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
101142101995
VdbeComment((v, "skip OFFSET records"));
101143101996
sqlite3VdbeJumpHere(v, addr);
101144101997
}
101145101998
}
@@ -101300,11 +102153,11 @@
101300102153
VdbeCoverage(v);
101301102154
}
101302102155
sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
101303102156
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
101304102157
}
101305
- assert( sqlite3VdbeCurrentAddr(v)==iJump );
102158
+ assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
101306102159
sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
101307102160
break;
101308102161
}
101309102162
101310102163
case WHERE_DISTINCT_UNIQUE: {
@@ -107767,11 +108620,12 @@
107767108620
sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
107768108621
VdbeCoverageNeverTaken(v);
107769108622
}
107770108623
labelContinue = labelBreak;
107771108624
sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
107772
- VdbeCoverage(v);
108625
+ VdbeCoverageIf(v, pPk==0);
108626
+ VdbeCoverageIf(v, pPk!=0);
107773108627
}else if( pPk ){
107774108628
labelContinue = sqlite3VdbeMakeLabel(v);
107775108629
sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
107776108630
addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
107777108631
sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
@@ -109777,10 +110631,11 @@
109777110631
struct WherePath {
109778110632
Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
109779110633
Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
109780110634
LogEst nRow; /* Estimated number of rows generated by this path */
109781110635
LogEst rCost; /* Total cost of this path */
110636
+ LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
109782110637
i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
109783110638
WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
109784110639
};
109785110640
109786110641
/*
@@ -110581,11 +111436,11 @@
110581111436
pScan->pOrigWC = pWC;
110582111437
pScan->pWC = pWC;
110583111438
if( pIdx && iColumn>=0 ){
110584111439
pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
110585111440
for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
110586
- if( NEVER(j>=pIdx->nKeyCol) ) return 0;
111441
+ if( NEVER(j>pIdx->nColumn) ) return 0;
110587111442
}
110588111443
pScan->zCollName = pIdx->azColl[j];
110589111444
}else{
110590111445
pScan->idxaff = 0;
110591111446
pScan->zCollName = 0;
@@ -111507,11 +112362,11 @@
111507112362
**
111508112363
** 3. All of those index columns for which the WHERE clause does not
111509112364
** contain a "col=X" term are subject to a NOT NULL constraint.
111510112365
*/
111511112366
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111512
- if( pIdx->onError==OE_None ) continue;
112367
+ if( !IsUniqueIndex(pIdx) ) continue;
111513112368
for(i=0; i<pIdx->nKeyCol; i++){
111514112369
i16 iCol = pIdx->aiColumn[i];
111515112370
if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
111516112371
int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
111517112372
if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
@@ -111531,12 +112386,11 @@
111531112386
111532112387
/*
111533112388
** Estimate the logarithm of the input value to base 2.
111534112389
*/
111535112390
static LogEst estLog(LogEst N){
111536
- LogEst x = sqlite3LogEst(N);
111537
- return x>33 ? x - 33 : 0;
112391
+ return N<=10 ? 0 : sqlite3LogEst(N) - 33;
111538112392
}
111539112393
111540112394
/*
111541112395
** Two routines for printing the content of an sqlite3_index_info
111542112396
** structure. Used for testing and debugging only. If neither
@@ -111997,11 +112851,11 @@
111997112851
}else{
111998112852
i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
111999112853
iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
112000112854
iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
112001112855
}
112002
- aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
112856
+ aStat[1] = pIdx->aAvgEq[iCol];
112003112857
if( iLower>=iUpper ){
112004112858
iGap = 0;
112005112859
}else{
112006112860
iGap = iUpper - iLower;
112007112861
}
@@ -112036,10 +112890,119 @@
112036112890
}
112037112891
}
112038112892
return nRet;
112039112893
}
112040112894
112895
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112896
+/*
112897
+** This function is called to estimate the number of rows visited by a
112898
+** range-scan on a skip-scan index. For example:
112899
+**
112900
+** CREATE INDEX i1 ON t1(a, b, c);
112901
+** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
112902
+**
112903
+** Value pLoop->nOut is currently set to the estimated number of rows
112904
+** visited for scanning (a=? AND b=?). This function reduces that estimate
112905
+** by some factor to account for the (c BETWEEN ? AND ?) expression based
112906
+** on the stat4 data for the index. this scan will be peformed multiple
112907
+** times (once for each (a,b) combination that matches a=?) is dealt with
112908
+** by the caller.
112909
+**
112910
+** It does this by scanning through all stat4 samples, comparing values
112911
+** extracted from pLower and pUpper with the corresponding column in each
112912
+** sample. If L and U are the number of samples found to be less than or
112913
+** equal to the values extracted from pLower and pUpper respectively, and
112914
+** N is the total number of samples, the pLoop->nOut value is adjusted
112915
+** as follows:
112916
+**
112917
+** nOut = nOut * ( min(U - L, 1) / N )
112918
+**
112919
+** If pLower is NULL, or a value cannot be extracted from the term, L is
112920
+** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
112921
+** U is set to N.
112922
+**
112923
+** Normally, this function sets *pbDone to 1 before returning. However,
112924
+** if no value can be extracted from either pLower or pUpper (and so the
112925
+** estimate of the number of rows delivered remains unchanged), *pbDone
112926
+** is left as is.
112927
+**
112928
+** If an error occurs, an SQLite error code is returned. Otherwise,
112929
+** SQLITE_OK.
112930
+*/
112931
+static int whereRangeSkipScanEst(
112932
+ Parse *pParse, /* Parsing & code generating context */
112933
+ WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
112934
+ WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
112935
+ WhereLoop *pLoop, /* Update the .nOut value of this loop */
112936
+ int *pbDone /* Set to true if at least one expr. value extracted */
112937
+){
112938
+ Index *p = pLoop->u.btree.pIndex;
112939
+ int nEq = pLoop->u.btree.nEq;
112940
+ sqlite3 *db = pParse->db;
112941
+ int nLower = -1;
112942
+ int nUpper = p->nSample+1;
112943
+ int rc = SQLITE_OK;
112944
+ int iCol = p->aiColumn[nEq];
112945
+ u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
112946
+ CollSeq *pColl;
112947
+
112948
+ sqlite3_value *p1 = 0; /* Value extracted from pLower */
112949
+ sqlite3_value *p2 = 0; /* Value extracted from pUpper */
112950
+ sqlite3_value *pVal = 0; /* Value extracted from record */
112951
+
112952
+ pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
112953
+ if( pLower ){
112954
+ rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
112955
+ nLower = 0;
112956
+ }
112957
+ if( pUpper && rc==SQLITE_OK ){
112958
+ rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
112959
+ nUpper = p2 ? 0 : p->nSample;
112960
+ }
112961
+
112962
+ if( p1 || p2 ){
112963
+ int i;
112964
+ int nDiff;
112965
+ for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
112966
+ rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
112967
+ if( rc==SQLITE_OK && p1 ){
112968
+ int res = sqlite3MemCompare(p1, pVal, pColl);
112969
+ if( res>=0 ) nLower++;
112970
+ }
112971
+ if( rc==SQLITE_OK && p2 ){
112972
+ int res = sqlite3MemCompare(p2, pVal, pColl);
112973
+ if( res>=0 ) nUpper++;
112974
+ }
112975
+ }
112976
+ nDiff = (nUpper - nLower);
112977
+ if( nDiff<=0 ) nDiff = 1;
112978
+
112979
+ /* If there is both an upper and lower bound specified, and the
112980
+ ** comparisons indicate that they are close together, use the fallback
112981
+ ** method (assume that the scan visits 1/64 of the rows) for estimating
112982
+ ** the number of rows visited. Otherwise, estimate the number of rows
112983
+ ** using the method described in the header comment for this function. */
112984
+ if( nDiff!=1 || pUpper==0 || pLower==0 ){
112985
+ int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
112986
+ pLoop->nOut -= nAdjust;
112987
+ *pbDone = 1;
112988
+ WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
112989
+ nLower, nUpper, nAdjust*-1, pLoop->nOut));
112990
+ }
112991
+
112992
+ }else{
112993
+ assert( *pbDone==0 );
112994
+ }
112995
+
112996
+ sqlite3ValueFree(p1);
112997
+ sqlite3ValueFree(p2);
112998
+ sqlite3ValueFree(pVal);
112999
+
113000
+ return rc;
113001
+}
113002
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
113003
+
112041113004
/*
112042113005
** This function is used to estimate the number of rows that will be visited
112043113006
** by scanning an index for a range of values. The range may have an upper
112044113007
** bound, a lower bound, or both. The WHERE clause terms that set the upper
112045113008
** and lower bounds are represented by pLower and pUpper respectively. For
@@ -112072,13 +113035,13 @@
112072113035
** considering the range constraints. If nEq is 0, this is the number of
112073113036
** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
112074113037
** to account for the range contraints pLower and pUpper.
112075113038
**
112076113039
** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
112077
-** used, each range inequality reduces the search space by a factor of 4.
112078
-** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
112079
-** rows visited by a factor of 16.
113040
+** used, a single range inequality reduces the search space by a factor of 4.
113041
+** and a pair of constraints (x>? AND x<?) reduces the expected number of
113042
+** rows visited by a factor of 64.
112080113043
*/
112081113044
static int whereRangeScanEst(
112082113045
Parse *pParse, /* Parsing & code generating context */
112083113046
WhereLoopBuilder *pBuilder,
112084113047
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
@@ -112092,99 +113055,104 @@
112092113055
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112093113056
Index *p = pLoop->u.btree.pIndex;
112094113057
int nEq = pLoop->u.btree.nEq;
112095113058
112096113059
if( p->nSample>0
112097
- && nEq==pBuilder->nRecValid
112098113060
&& nEq<p->nSampleCol
112099113061
&& OptimizationEnabled(pParse->db, SQLITE_Stat3)
112100113062
){
112101
- UnpackedRecord *pRec = pBuilder->pRec;
112102
- tRowcnt a[2];
112103
- u8 aff;
112104
-
112105
- /* Variable iLower will be set to the estimate of the number of rows in
112106
- ** the index that are less than the lower bound of the range query. The
112107
- ** lower bound being the concatenation of $P and $L, where $P is the
112108
- ** key-prefix formed by the nEq values matched against the nEq left-most
112109
- ** columns of the index, and $L is the value in pLower.
112110
- **
112111
- ** Or, if pLower is NULL or $L cannot be extracted from it (because it
112112
- ** is not a simple variable or literal value), the lower bound of the
112113
- ** range is $P. Due to a quirk in the way whereKeyStats() works, even
112114
- ** if $L is available, whereKeyStats() is called for both ($P) and
112115
- ** ($P:$L) and the larger of the two returned values used.
112116
- **
112117
- ** Similarly, iUpper is to be set to the estimate of the number of rows
112118
- ** less than the upper bound of the range query. Where the upper bound
112119
- ** is either ($P) or ($P:$U). Again, even if $U is available, both values
112120
- ** of iUpper are requested of whereKeyStats() and the smaller used.
112121
- */
112122
- tRowcnt iLower;
112123
- tRowcnt iUpper;
112124
-
112125
- if( nEq==p->nKeyCol ){
112126
- aff = SQLITE_AFF_INTEGER;
112127
- }else{
112128
- aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
112129
- }
112130
- /* Determine iLower and iUpper using ($P) only. */
112131
- if( nEq==0 ){
112132
- iLower = 0;
112133
- iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
112134
- }else{
112135
- /* Note: this call could be optimized away - since the same values must
112136
- ** have been requested when testing key $P in whereEqualScanEst(). */
112137
- whereKeyStats(pParse, p, pRec, 0, a);
112138
- iLower = a[0];
112139
- iUpper = a[0] + a[1];
112140
- }
112141
-
112142
- /* If possible, improve on the iLower estimate using ($P:$L). */
112143
- if( pLower ){
112144
- int bOk; /* True if value is extracted from pExpr */
112145
- Expr *pExpr = pLower->pExpr->pRight;
112146
- assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
112147
- rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
112148
- if( rc==SQLITE_OK && bOk ){
112149
- tRowcnt iNew;
112150
- whereKeyStats(pParse, p, pRec, 0, a);
112151
- iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
112152
- if( iNew>iLower ) iLower = iNew;
112153
- nOut--;
112154
- }
112155
- }
112156
-
112157
- /* If possible, improve on the iUpper estimate using ($P:$U). */
112158
- if( pUpper ){
112159
- int bOk; /* True if value is extracted from pExpr */
112160
- Expr *pExpr = pUpper->pExpr->pRight;
112161
- assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
112162
- rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
112163
- if( rc==SQLITE_OK && bOk ){
112164
- tRowcnt iNew;
112165
- whereKeyStats(pParse, p, pRec, 1, a);
112166
- iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
112167
- if( iNew<iUpper ) iUpper = iNew;
112168
- nOut--;
112169
- }
112170
- }
112171
-
112172
- pBuilder->pRec = pRec;
112173
- if( rc==SQLITE_OK ){
112174
- if( iUpper>iLower ){
112175
- nNew = sqlite3LogEst(iUpper - iLower);
112176
- }else{
112177
- nNew = 10; assert( 10==sqlite3LogEst(2) );
112178
- }
112179
- if( nNew<nOut ){
112180
- nOut = nNew;
112181
- }
112182
- pLoop->nOut = (LogEst)nOut;
112183
- WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n",
112184
- (u32)iLower, (u32)iUpper, nOut));
112185
- return SQLITE_OK;
113063
+ if( nEq==pBuilder->nRecValid ){
113064
+ UnpackedRecord *pRec = pBuilder->pRec;
113065
+ tRowcnt a[2];
113066
+ u8 aff;
113067
+
113068
+ /* Variable iLower will be set to the estimate of the number of rows in
113069
+ ** the index that are less than the lower bound of the range query. The
113070
+ ** lower bound being the concatenation of $P and $L, where $P is the
113071
+ ** key-prefix formed by the nEq values matched against the nEq left-most
113072
+ ** columns of the index, and $L is the value in pLower.
113073
+ **
113074
+ ** Or, if pLower is NULL or $L cannot be extracted from it (because it
113075
+ ** is not a simple variable or literal value), the lower bound of the
113076
+ ** range is $P. Due to a quirk in the way whereKeyStats() works, even
113077
+ ** if $L is available, whereKeyStats() is called for both ($P) and
113078
+ ** ($P:$L) and the larger of the two returned values used.
113079
+ **
113080
+ ** Similarly, iUpper is to be set to the estimate of the number of rows
113081
+ ** less than the upper bound of the range query. Where the upper bound
113082
+ ** is either ($P) or ($P:$U). Again, even if $U is available, both values
113083
+ ** of iUpper are requested of whereKeyStats() and the smaller used.
113084
+ */
113085
+ tRowcnt iLower;
113086
+ tRowcnt iUpper;
113087
+
113088
+ if( nEq==p->nKeyCol ){
113089
+ aff = SQLITE_AFF_INTEGER;
113090
+ }else{
113091
+ aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
113092
+ }
113093
+ /* Determine iLower and iUpper using ($P) only. */
113094
+ if( nEq==0 ){
113095
+ iLower = 0;
113096
+ iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
113097
+ }else{
113098
+ /* Note: this call could be optimized away - since the same values must
113099
+ ** have been requested when testing key $P in whereEqualScanEst(). */
113100
+ whereKeyStats(pParse, p, pRec, 0, a);
113101
+ iLower = a[0];
113102
+ iUpper = a[0] + a[1];
113103
+ }
113104
+
113105
+ /* If possible, improve on the iLower estimate using ($P:$L). */
113106
+ if( pLower ){
113107
+ int bOk; /* True if value is extracted from pExpr */
113108
+ Expr *pExpr = pLower->pExpr->pRight;
113109
+ assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
113110
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
113111
+ if( rc==SQLITE_OK && bOk ){
113112
+ tRowcnt iNew;
113113
+ whereKeyStats(pParse, p, pRec, 0, a);
113114
+ iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
113115
+ if( iNew>iLower ) iLower = iNew;
113116
+ nOut--;
113117
+ }
113118
+ }
113119
+
113120
+ /* If possible, improve on the iUpper estimate using ($P:$U). */
113121
+ if( pUpper ){
113122
+ int bOk; /* True if value is extracted from pExpr */
113123
+ Expr *pExpr = pUpper->pExpr->pRight;
113124
+ assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
113125
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
113126
+ if( rc==SQLITE_OK && bOk ){
113127
+ tRowcnt iNew;
113128
+ whereKeyStats(pParse, p, pRec, 1, a);
113129
+ iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
113130
+ if( iNew<iUpper ) iUpper = iNew;
113131
+ nOut--;
113132
+ }
113133
+ }
113134
+
113135
+ pBuilder->pRec = pRec;
113136
+ if( rc==SQLITE_OK ){
113137
+ if( iUpper>iLower ){
113138
+ nNew = sqlite3LogEst(iUpper - iLower);
113139
+ }else{
113140
+ nNew = 10; assert( 10==sqlite3LogEst(2) );
113141
+ }
113142
+ if( nNew<nOut ){
113143
+ nOut = nNew;
113144
+ }
113145
+ pLoop->nOut = (LogEst)nOut;
113146
+ WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n",
113147
+ (u32)iLower, (u32)iUpper, nOut));
113148
+ return SQLITE_OK;
113149
+ }
113150
+ }else{
113151
+ int bDone = 0;
113152
+ rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
113153
+ if( bDone ) return rc;
112186113154
}
112187113155
}
112188113156
#else
112189113157
UNUSED_PARAMETER(pParse);
112190113158
UNUSED_PARAMETER(pBuilder);
@@ -112239,11 +113207,11 @@
112239113207
int rc; /* Subfunction return code */
112240113208
tRowcnt a[2]; /* Statistics */
112241113209
int bOk;
112242113210
112243113211
assert( nEq>=1 );
112244
- assert( nEq<=(p->nKeyCol+1) );
113212
+ assert( nEq<=p->nColumn );
112245113213
assert( p->aSample!=0 );
112246113214
assert( p->nSample>0 );
112247113215
assert( pBuilder->nRecValid<nEq );
112248113216
112249113217
/* If values are not available for all fields of the index to the left
@@ -112252,11 +113220,11 @@
112252113220
return SQLITE_NOTFOUND;
112253113221
}
112254113222
112255113223
/* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
112256113224
** below would return the same value. */
112257
- if( nEq>p->nKeyCol ){
113225
+ if( nEq>=p->nColumn ){
112258113226
*pnRow = 1;
112259113227
return SQLITE_OK;
112260113228
}
112261113229
112262113230
aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
@@ -112446,11 +113414,11 @@
112446113414
testcase( bRev );
112447113415
bRev = !bRev;
112448113416
}
112449113417
assert( pX->op==TK_IN );
112450113418
iReg = iTarget;
112451
- eType = sqlite3FindInIndex(pParse, pX, 0);
113419
+ eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
112452113420
if( eType==IN_INDEX_INDEX_DESC ){
112453113421
testcase( bRev );
112454113422
bRev = !bRev;
112455113423
}
112456113424
iTab = pX->iTable;
@@ -112683,11 +113651,11 @@
112683113651
}
112684113652
sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
112685113653
txt.db = db;
112686113654
sqlite3StrAccumAppend(&txt, " (", 2);
112687113655
for(i=0; i<nEq; i++){
112688
- char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
113656
+ char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
112689113657
if( i>=nSkip ){
112690113658
explainAppendTerm(&txt, i, z, "=");
112691113659
}else{
112692113660
if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
112693113661
sqlite3StrAccumAppend(&txt, "ANY(", 4);
@@ -112696,15 +113664,15 @@
112696113664
}
112697113665
}
112698113666
112699113667
j = i;
112700113668
if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
112701
- char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
113669
+ char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
112702113670
explainAppendTerm(&txt, i++, z, ">");
112703113671
}
112704113672
if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
112705
- char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
113673
+ char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
112706113674
explainAppendTerm(&txt, i, z, "<");
112707113675
}
112708113676
sqlite3StrAccumAppend(&txt, ")", 1);
112709113677
return sqlite3StrAccumFinish(&txt);
112710113678
}
@@ -113347,10 +114315,11 @@
113347114315
int regRowid = 0; /* Register holding rowid */
113348114316
int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
113349114317
int iRetInit; /* Address of regReturn init */
113350114318
int untestedTerms = 0; /* Some terms not completely tested */
113351114319
int ii; /* Loop counter */
114320
+ u16 wctrlFlags; /* Flags for sub-WHERE clause */
113352114321
Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
113353114322
Table *pTab = pTabItem->pTab;
113354114323
113355114324
pTerm = pLoop->aLTerm[0];
113356114325
assert( pTerm!=0 );
@@ -113442,10 +114411,12 @@
113442114411
113443114412
/* Run a separate WHERE clause for each term of the OR clause. After
113444114413
** eliminating duplicates from other WHERE clauses, the action for each
113445114414
** sub-WHERE clause is to to invoke the main loop body as a subroutine.
113446114415
*/
114416
+ wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
114417
+ WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY;
113447114418
for(ii=0; ii<pOrWc->nTerm; ii++){
113448114419
WhereTerm *pOrTerm = &pOrWc->a[ii];
113449114420
if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
113450114421
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113451114422
Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -113454,12 +114425,11 @@
113454114425
pAndExpr->pLeft = pOrExpr;
113455114426
pOrExpr = pAndExpr;
113456114427
}
113457114428
/* Loop through table entries that match term pOrTerm. */
113458114429
pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
113459
- WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
113460
- WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
114430
+ wctrlFlags, iCovCur);
113461114431
assert( pSubWInfo || pParse->nErr || db->mallocFailed );
113462114432
if( pSubWInfo ){
113463114433
WhereLoop *pSubLoop;
113464114434
explainOneScan(
113465114435
pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
@@ -113546,10 +114516,11 @@
113546114516
&& (ii==0 || pSubLoop->u.btree.pIndex==pCov)
113547114517
&& (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
113548114518
){
113549114519
assert( pSubWInfo->a[0].iIdxCur==iCovCur );
113550114520
pCov = pSubLoop->u.btree.pIndex;
114521
+ wctrlFlags |= WHERE_REOPEN_IDX;
113551114522
}else{
113552114523
pCov = 0;
113553114524
}
113554114525
113555114526
/* Finish the loop through table entries that match term pOrTerm. */
@@ -113724,11 +114695,11 @@
113724114695
z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
113725114696
}
113726114697
sqlite3DebugPrintf(" %-19s", z);
113727114698
sqlite3_free(z);
113728114699
}
113729
- sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm);
114700
+ sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
113730114701
sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
113731114702
#ifdef SQLITE_ENABLE_TREE_EXPLAIN
113732114703
/* If the 0x100 bit of wheretracing is set, then show all of the constraint
113733114704
** expressions in the WhereLoop.aLTerm[] array.
113734114705
*/
@@ -113960,10 +114931,21 @@
113960114931
113961114932
/* whereLoopAddBtree() always generates and inserts the automatic index
113962114933
** case first. Hence compatible candidate WhereLoops never have a larger
113963114934
** rSetup. Call this SETUP-INVARIANT */
113964114935
assert( p->rSetup>=pTemplate->rSetup );
114936
+
114937
+ /* Any loop using an appliation-defined index (or PRIMARY KEY or
114938
+ ** UNIQUE constraint) with one or more == constraints is better
114939
+ ** than an automatic index. */
114940
+ if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
114941
+ && (pTemplate->wsFlags & WHERE_INDEXED)!=0
114942
+ && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
114943
+ && (p->prereq & pTemplate->prereq)==pTemplate->prereq
114944
+ ){
114945
+ break;
114946
+ }
113965114947
113966114948
/* If existing WhereLoop p is better than pTemplate, pTemplate can be
113967114949
** discarded. WhereLoop p is better if:
113968114950
** (1) p has no more dependencies than pTemplate, and
113969114951
** (2) p has an equal or lower cost than pTemplate
@@ -114085,17 +115067,17 @@
114085115067
** p[] that are also supplated by pTemplate */
114086115068
WhereLoop **ppTail = &p->pNextLoop;
114087115069
WhereLoop *pToDel;
114088115070
while( *ppTail ){
114089115071
ppTail = whereLoopFindLesser(ppTail, pTemplate);
114090
- if( NEVER(ppTail==0) ) break;
115072
+ if( ppTail==0 ) break;
114091115073
pToDel = *ppTail;
114092115074
if( pToDel==0 ) break;
114093115075
*ppTail = pToDel->pNextLoop;
114094115076
#if WHERETRACE_ENABLED /* 0x8 */
114095115077
if( sqlite3WhereTrace & 0x8 ){
114096
- sqlite3DebugPrintf("ins-del: ");
115078
+ sqlite3DebugPrintf("ins-del: ");
114097115079
whereLoopPrint(pToDel, pBuilder->pWC);
114098115080
}
114099115081
#endif
114100115082
whereLoopDelete(db, pToDel);
114101115083
}
@@ -114141,10 +115123,20 @@
114141115123
pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
114142115124
}
114143115125
}
114144115126
}
114145115127
115128
+/*
115129
+** Adjust the cost C by the costMult facter T. This only occurs if
115130
+** compiled with -DSQLITE_ENABLE_COSTMULT
115131
+*/
115132
+#ifdef SQLITE_ENABLE_COSTMULT
115133
+# define ApplyCostMultiplier(C,T) C += T
115134
+#else
115135
+# define ApplyCostMultiplier(C,T)
115136
+#endif
115137
+
114146115138
/*
114147115139
** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
114148115140
** index pIndex. Try to match one more.
114149115141
**
114150115142
** When this function is called, pBuilder->pNew->nOut contains the
@@ -114191,16 +115183,13 @@
114191115183
}else{
114192115184
opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
114193115185
}
114194115186
if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
114195115187
114196
- assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
114197
- if( pNew->u.btree.nEq < pProbe->nKeyCol ){
114198
- iCol = pProbe->aiColumn[pNew->u.btree.nEq];
114199
- }else{
114200
- iCol = -1;
114201
- }
115188
+ assert( pNew->u.btree.nEq<pProbe->nColumn );
115189
+ iCol = pProbe->aiColumn[pNew->u.btree.nEq];
115190
+
114202115191
pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
114203115192
opMask, pProbe);
114204115193
saved_nEq = pNew->u.btree.nEq;
114205115194
saved_nSkip = pNew->u.btree.nSkip;
114206115195
saved_nLTerm = pNew->nLTerm;
@@ -114279,11 +115268,11 @@
114279115268
** changes "x IN (?)" into "x=?". */
114280115269
114281115270
}else if( eOp & (WO_EQ) ){
114282115271
pNew->wsFlags |= WHERE_COLUMN_EQ;
114283115272
if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
114284
- if( iCol>=0 && pProbe->onError==OE_None ){
115273
+ if( iCol>=0 && !IsUniqueIndex(pProbe) ){
114285115274
pNew->wsFlags |= WHERE_UNQ_WANTED;
114286115275
}else{
114287115276
pNew->wsFlags |= WHERE_ONEROW;
114288115277
}
114289115278
}
@@ -114340,11 +115329,10 @@
114340115329
testcase( eOp & WO_ISNULL );
114341115330
rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
114342115331
}else{
114343115332
rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
114344115333
}
114345
- assert( rc!=SQLITE_OK || nOut>0 );
114346115334
if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
114347115335
if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
114348115336
if( nOut ){
114349115337
pNew->nOut = sqlite3LogEst(nOut);
114350115338
if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
@@ -114372,10 +115360,11 @@
114372115360
rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
114373115361
pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
114374115362
if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
114375115363
pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
114376115364
}
115365
+ ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
114377115366
114378115367
nOutUnadjusted = pNew->nOut;
114379115368
pNew->rRun += nInMul + nIn;
114380115369
pNew->nOut += nInMul + nIn;
114381115370
whereLoopOutputAdjust(pBuilder->pWC, pNew);
@@ -114386,11 +115375,11 @@
114386115375
}else{
114387115376
pNew->nOut = nOutUnadjusted;
114388115377
}
114389115378
114390115379
if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
114391
- && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
115380
+ && pNew->u.btree.nEq<pProbe->nColumn
114392115381
){
114393115382
whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
114394115383
}
114395115384
pNew->nOut = saved_nOut;
114396115385
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -114491,10 +115480,18 @@
114491115480
** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
114492115481
**
114493115482
** Normally, nSeek is 1. nSeek values greater than 1 come about if the
114494115483
** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
114495115484
** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
115485
+**
115486
+** The estimated values (nRow, nVisit, nSeek) often contain a large amount
115487
+** of uncertainty. For this reason, scoring is designed to pick plans that
115488
+** "do the least harm" if the estimates are inaccurate. For example, a
115489
+** log(nRow) factor is omitted from a non-covering index scan in order to
115490
+** bias the scoring in favor of using an index, since the worst-case
115491
+** performance of using an index is far better than the worst-case performance
115492
+** of a full table scan.
114496115493
*/
114497115494
static int whereLoopAddBtree(
114498115495
WhereLoopBuilder *pBuilder, /* WHERE clause information */
114499115496
Bitmask mExtra /* Extra prerequesites for using this table */
114500115497
){
@@ -114533,10 +115530,11 @@
114533115530
** fake index the first in a chain of Index objects with all of the real
114534115531
** indices to follow */
114535115532
Index *pFirst; /* First of real indices on the table */
114536115533
memset(&sPk, 0, sizeof(Index));
114537115534
sPk.nKeyCol = 1;
115535
+ sPk.nColumn = 1;
114538115536
sPk.aiColumn = &aiColumnPk;
114539115537
sPk.aiRowLogEst = aiRowEstPk;
114540115538
sPk.onError = OE_Replace;
114541115539
sPk.pTable = pTab;
114542115540
sPk.szIdxRow = pTab->szTabRow;
@@ -114577,10 +115575,11 @@
114577115575
pNew->aLTerm[0] = pTerm;
114578115576
/* TUNING: One-time cost for computing the automatic index is
114579115577
** approximately 7*N*log2(N) where N is the number of rows in
114580115578
** the table being indexed. */
114581115579
pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) );
115580
+ ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
114582115581
/* TUNING: Each index lookup yields 20 rows in the table. This
114583115582
** is more than the usual guess of 10 rows, since we have no way
114584115583
** of knowning how selective the index will ultimately be. It would
114585115584
** not be unreasonable to make this value much larger. */
114586115585
pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
@@ -114618,10 +115617,11 @@
114618115617
114619115618
/* Full table scan */
114620115619
pNew->iSortIdx = b ? iSortIdx : 0;
114621115620
/* TUNING: Cost of full table scan is (N*3.0). */
114622115621
pNew->rRun = rSize + 16;
115622
+ ApplyCostMultiplier(pNew->rRun, pTab->costMult);
114623115623
whereLoopOutputAdjust(pWC, pNew);
114624115624
rc = whereLoopInsert(pBuilder, pNew);
114625115625
pNew->nOut = rSize;
114626115626
if( rc ) break;
114627115627
}else{
@@ -114653,11 +115653,11 @@
114653115653
** also add the cost of visiting table rows (N*3.0). */
114654115654
pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114655115655
if( m!=0 ){
114656115656
pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
114657115657
}
114658
-
115658
+ ApplyCostMultiplier(pNew->rRun, pTab->costMult);
114659115659
whereLoopOutputAdjust(pWC, pNew);
114660115660
rc = whereLoopInsert(pBuilder, pNew);
114661115661
pNew->nOut = rSize;
114662115662
if( rc ) break;
114663115663
}
@@ -115123,11 +116123,11 @@
115123116123
}else{
115124116124
nKeyCol = pIndex->nKeyCol;
115125116125
nColumn = pIndex->nColumn;
115126116126
assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
115127116127
assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
115128
- isOrderDistinct = pIndex->onError!=OE_None;
116128
+ isOrderDistinct = IsUniqueIndex(pIndex);
115129116129
}
115130116130
115131116131
/* Loop through all columns of the index and deal with the ones
115132116132
** that are not constrained by == or IN.
115133116133
*/
@@ -115291,10 +116291,49 @@
115291116291
if( pLast ) zName[i++] = pLast->cId;
115292116292
zName[i] = 0;
115293116293
return zName;
115294116294
}
115295116295
#endif
116296
+
116297
+/*
116298
+** Return the cost of sorting nRow rows, assuming that the keys have
116299
+** nOrderby columns and that the first nSorted columns are already in
116300
+** order.
116301
+*/
116302
+static LogEst whereSortingCost(
116303
+ WhereInfo *pWInfo,
116304
+ LogEst nRow,
116305
+ int nOrderBy,
116306
+ int nSorted
116307
+){
116308
+ /* TUNING: Estimated cost of a full external sort, where N is
116309
+ ** the number of rows to sort is:
116310
+ **
116311
+ ** cost = (3.0 * N * log(N)).
116312
+ **
116313
+ ** Or, if the order-by clause has X terms but only the last Y
116314
+ ** terms are out of order, then block-sorting will reduce the
116315
+ ** sorting cost to:
116316
+ **
116317
+ ** cost = (3.0 * N * log(N)) * (Y/X)
116318
+ **
116319
+ ** The (Y/X) term is implemented using stack variable rScale
116320
+ ** below. */
116321
+ LogEst rScale, rSortCost;
116322
+ assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
116323
+ rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
116324
+ rSortCost = nRow + estLog(nRow) + rScale + 16;
116325
+
116326
+ /* TUNING: The cost of implementing DISTINCT using a B-TREE is
116327
+ ** similar but with a larger constant of proportionality.
116328
+ ** Multiply by an additional factor of 3.0. */
116329
+ if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
116330
+ rSortCost += 16;
116331
+ }
116332
+
116333
+ return rSortCost;
116334
+}
115296116335
115297116336
/*
115298116337
** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
115299116338
** attempts to find the lowest cost path that visits each WhereLoop
115300116339
** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -115313,142 +116352,170 @@
115313116352
sqlite3 *db; /* The database connection */
115314116353
int iLoop; /* Loop counter over the terms of the join */
115315116354
int ii, jj; /* Loop counters */
115316116355
int mxI = 0; /* Index of next entry to replace */
115317116356
int nOrderBy; /* Number of ORDER BY clause terms */
115318
- LogEst rCost; /* Cost of a path */
115319
- LogEst nOut; /* Number of outputs */
115320116357
LogEst mxCost = 0; /* Maximum cost of a set of paths */
115321
- LogEst mxOut = 0; /* Maximum nOut value on the set of paths */
116358
+ LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
115322116359
int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
115323116360
WherePath *aFrom; /* All nFrom paths at the previous level */
115324116361
WherePath *aTo; /* The nTo best paths at the current level */
115325116362
WherePath *pFrom; /* An element of aFrom[] that we are working on */
115326116363
WherePath *pTo; /* An element of aTo[] that we are working on */
115327116364
WhereLoop *pWLoop; /* One of the WhereLoop objects */
115328116365
WhereLoop **pX; /* Used to divy up the pSpace memory */
116366
+ LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
115329116367
char *pSpace; /* Temporary memory used by this routine */
116368
+ int nSpace; /* Bytes of space allocated at pSpace */
115330116369
115331116370
pParse = pWInfo->pParse;
115332116371
db = pParse->db;
115333116372
nLoop = pWInfo->nLevel;
115334116373
/* TUNING: For simple queries, only the best path is tracked.
115335116374
** For 2-way joins, the 5 best paths are followed.
115336116375
** For joins of 3 or more tables, track the 10 best paths */
115337116376
mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
115338116377
assert( nLoop<=pWInfo->pTabList->nSrc );
115339
- WHERETRACE(0x002, ("---- begin solver\n"));
116378
+ WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
115340116379
115341
- /* Allocate and initialize space for aTo and aFrom */
115342
- ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
115343
- pSpace = sqlite3DbMallocRaw(db, ii);
116380
+ /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
116381
+ ** case the purpose of this call is to estimate the number of rows returned
116382
+ ** by the overall query. Once this estimate has been obtained, the caller
116383
+ ** will invoke this function a second time, passing the estimate as the
116384
+ ** nRowEst parameter. */
116385
+ if( pWInfo->pOrderBy==0 || nRowEst==0 ){
116386
+ nOrderBy = 0;
116387
+ }else{
116388
+ nOrderBy = pWInfo->pOrderBy->nExpr;
116389
+ }
116390
+
116391
+ /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
116392
+ nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
116393
+ nSpace += sizeof(LogEst) * nOrderBy;
116394
+ pSpace = sqlite3DbMallocRaw(db, nSpace);
115344116395
if( pSpace==0 ) return SQLITE_NOMEM;
115345116396
aTo = (WherePath*)pSpace;
115346116397
aFrom = aTo+mxChoice;
115347116398
memset(aFrom, 0, sizeof(aFrom[0]));
115348116399
pX = (WhereLoop**)(aFrom+mxChoice);
115349116400
for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
115350116401
pFrom->aLoop = pX;
115351116402
}
116403
+ if( nOrderBy ){
116404
+ /* If there is an ORDER BY clause and it is not being ignored, set up
116405
+ ** space for the aSortCost[] array. Each element of the aSortCost array
116406
+ ** is either zero - meaning it has not yet been initialized - or the
116407
+ ** cost of sorting nRowEst rows of data where the first X terms of
116408
+ ** the ORDER BY clause are already in order, where X is the array
116409
+ ** index. */
116410
+ aSortCost = (LogEst*)pX;
116411
+ memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
116412
+ }
116413
+ assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
116414
+ assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
115352116415
115353116416
/* Seed the search with a single WherePath containing zero WhereLoops.
115354116417
**
115355116418
** TUNING: Do not let the number of iterations go above 25. If the cost
115356116419
** of computing an automatic index is not paid back within the first 25
115357116420
** rows, then do not use the automatic index. */
115358116421
aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
115359116422
nFrom = 1;
115360
-
115361
- /* Precompute the cost of sorting the final result set, if the caller
115362
- ** to sqlite3WhereBegin() was concerned about sorting */
115363
- if( pWInfo->pOrderBy==0 || nRowEst==0 ){
115364
- aFrom[0].isOrdered = 0;
115365
- nOrderBy = 0;
115366
- }else{
115367
- aFrom[0].isOrdered = nLoop>0 ? -1 : 1;
115368
- nOrderBy = pWInfo->pOrderBy->nExpr;
116423
+ assert( aFrom[0].isOrdered==0 );
116424
+ if( nOrderBy ){
116425
+ /* If nLoop is zero, then there are no FROM terms in the query. Since
116426
+ ** in this case the query may return a maximum of one row, the results
116427
+ ** are already in the requested order. Set isOrdered to nOrderBy to
116428
+ ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
116429
+ ** -1, indicating that the result set may or may not be ordered,
116430
+ ** depending on the loops added to the current plan. */
116431
+ aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
115369116432
}
115370116433
115371116434
/* Compute successively longer WherePaths using the previous generation
115372116435
** of WherePaths as the basis for the next. Keep track of the mxChoice
115373116436
** best paths at each generation */
115374116437
for(iLoop=0; iLoop<nLoop; iLoop++){
115375116438
nTo = 0;
115376116439
for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
115377116440
for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
115378
- Bitmask maskNew;
115379
- Bitmask revMask = 0;
115380
- i8 isOrdered = pFrom->isOrdered;
116441
+ LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
116442
+ LogEst rCost; /* Cost of path (pFrom+pWLoop) */
116443
+ LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
116444
+ i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
116445
+ Bitmask maskNew; /* Mask of src visited by (..) */
116446
+ Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
116447
+
115381116448
if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
115382116449
if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
115383116450
/* At this point, pWLoop is a candidate to be the next loop.
115384116451
** Compute its cost */
115385
- rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
115386
- rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
116452
+ rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
116453
+ rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
115387116454
nOut = pFrom->nRow + pWLoop->nOut;
115388116455
maskNew = pFrom->maskLoop | pWLoop->maskSelf;
115389116456
if( isOrdered<0 ){
115390116457
isOrdered = wherePathSatisfiesOrderBy(pWInfo,
115391116458
pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
115392116459
iLoop, pWLoop, &revMask);
115393
- if( isOrdered>=0 && isOrdered<nOrderBy ){
115394
- /* TUNING: Estimated cost of a full external sort, where N is
115395
- ** the number of rows to sort is:
115396
- **
115397
- ** cost = (3.0 * N * log(N)).
115398
- **
115399
- ** Or, if the order-by clause has X terms but only the last Y
115400
- ** terms are out of order, then block-sorting will reduce the
115401
- ** sorting cost to:
115402
- **
115403
- ** cost = (3.0 * N * log(N)) * (Y/X)
115404
- **
115405
- ** The (Y/X) term is implemented using stack variable rScale
115406
- ** below. */
115407
- LogEst rScale, rSortCost;
115408
- assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
115409
- rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
115410
- rSortCost = nRowEst + estLog(nRowEst) + rScale + 16;
115411
-
115412
- /* TUNING: The cost of implementing DISTINCT using a B-TREE is
115413
- ** similar but with a larger constant of proportionality.
115414
- ** Multiply by an additional factor of 3.0. */
115415
- if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
115416
- rSortCost += 16;
115417
- }
115418
- WHERETRACE(0x002,
115419
- ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
115420
- rSortCost, (nOrderBy-isOrdered), nOrderBy, rCost,
115421
- sqlite3LogEstAdd(rCost,rSortCost)));
115422
- rCost = sqlite3LogEstAdd(rCost, rSortCost);
115423
- }
115424116460
}else{
115425116461
revMask = pFrom->revLoop;
115426116462
}
115427
- /* Check to see if pWLoop should be added to the mxChoice best so far */
116463
+ if( isOrdered>=0 && isOrdered<nOrderBy ){
116464
+ if( aSortCost[isOrdered]==0 ){
116465
+ aSortCost[isOrdered] = whereSortingCost(
116466
+ pWInfo, nRowEst, nOrderBy, isOrdered
116467
+ );
116468
+ }
116469
+ rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
116470
+
116471
+ WHERETRACE(0x002,
116472
+ ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
116473
+ aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
116474
+ rUnsorted, rCost));
116475
+ }else{
116476
+ rCost = rUnsorted;
116477
+ }
116478
+
116479
+ /* Check to see if pWLoop should be added to the set of
116480
+ ** mxChoice best-so-far paths.
116481
+ **
116482
+ ** First look for an existing path among best-so-far paths
116483
+ ** that covers the same set of loops and has the same isOrdered
116484
+ ** setting as the current path candidate.
116485
+ **
116486
+ ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
116487
+ ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
116488
+ ** of legal values for isOrdered, -1..64.
116489
+ */
115428116490
for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
115429116491
if( pTo->maskLoop==maskNew
115430
- && ((pTo->isOrdered^isOrdered)&80)==0
115431
- && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
115432
- (pTo->rCost>=rCost && pTo->nRow>=nOut))
116492
+ && ((pTo->isOrdered^isOrdered)&0x80)==0
115433116493
){
115434116494
testcase( jj==nTo-1 );
115435116495
break;
115436116496
}
115437116497
}
115438116498
if( jj>=nTo ){
115439
- if( nTo>=mxChoice && rCost>=mxCost ){
116499
+ /* None of the existing best-so-far paths match the candidate. */
116500
+ if( nTo>=mxChoice
116501
+ && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
116502
+ ){
116503
+ /* The current candidate is no better than any of the mxChoice
116504
+ ** paths currently in the best-so-far buffer. So discard
116505
+ ** this candidate as not viable. */
115440116506
#ifdef WHERETRACE_ENABLED /* 0x4 */
115441116507
if( sqlite3WhereTrace&0x4 ){
115442116508
sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",
115443116509
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
115444116510
isOrdered>=0 ? isOrdered+'0' : '?');
115445116511
}
115446116512
#endif
115447116513
continue;
115448116514
}
115449
- /* Add a new Path to the aTo[] set */
116515
+ /* If we reach this points it means that the new candidate path
116516
+ ** needs to be added to the set of best-so-far paths. */
115450116517
if( nTo<mxChoice ){
115451116518
/* Increase the size of the aTo set by one */
115452116519
jj = nTo++;
115453116520
}else{
115454116521
/* New path replaces the prior worst to keep count below mxChoice */
@@ -115461,11 +116528,15 @@
115461116528
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
115462116529
isOrdered>=0 ? isOrdered+'0' : '?');
115463116530
}
115464116531
#endif
115465116532
}else{
115466
- if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
116533
+ /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
116534
+ ** same set of loops and has the sam isOrdered setting as the
116535
+ ** candidate path. Check to see if the candidate should replace
116536
+ ** pTo or if the candidate should be skipped */
116537
+ if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
115467116538
#ifdef WHERETRACE_ENABLED /* 0x4 */
115468116539
if( sqlite3WhereTrace&0x4 ){
115469116540
sqlite3DebugPrintf(
115470116541
"Skip %s cost=%-3d,%3d order=%c",
115471116542
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115473,15 +116544,17 @@
115473116544
sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n",
115474116545
wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
115475116546
pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
115476116547
}
115477116548
#endif
116549
+ /* Discard the candidate path from further consideration */
115478116550
testcase( pTo->rCost==rCost );
115479116551
continue;
115480116552
}
115481116553
testcase( pTo->rCost==rCost+1 );
115482
- /* A new and better score for a previously created equivalent path */
116554
+ /* Control reaches here if the candidate path is better than the
116555
+ ** pTo path. Replace pTo with the candidate. */
115483116556
#ifdef WHERETRACE_ENABLED /* 0x4 */
115484116557
if( sqlite3WhereTrace&0x4 ){
115485116558
sqlite3DebugPrintf(
115486116559
"Update %s cost=%-3d,%3d order=%c",
115487116560
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115495,21 +116568,24 @@
115495116568
/* pWLoop is a winner. Add it to the set of best so far */
115496116569
pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
115497116570
pTo->revLoop = revMask;
115498116571
pTo->nRow = nOut;
115499116572
pTo->rCost = rCost;
116573
+ pTo->rUnsorted = rUnsorted;
115500116574
pTo->isOrdered = isOrdered;
115501116575
memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
115502116576
pTo->aLoop[iLoop] = pWLoop;
115503116577
if( nTo>=mxChoice ){
115504116578
mxI = 0;
115505116579
mxCost = aTo[0].rCost;
115506
- mxOut = aTo[0].nRow;
116580
+ mxUnsorted = aTo[0].nRow;
115507116581
for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
115508
- if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
116582
+ if( pTo->rCost>mxCost
116583
+ || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
116584
+ ){
115509116585
mxCost = pTo->rCost;
115510
- mxOut = pTo->nRow;
116586
+ mxUnsorted = pTo->rUnsorted;
115511116587
mxI = jj;
115512116588
}
115513116589
}
115514116590
}
115515116591
}
@@ -115643,11 +116719,11 @@
115643116719
pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
115644116720
}else{
115645116721
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115646116722
assert( pLoop->aLTermSpace==pLoop->aLTerm );
115647116723
assert( ArraySize(pLoop->aLTermSpace)==4 );
115648
- if( pIdx->onError==OE_None
116724
+ if( !IsUniqueIndex(pIdx)
115649116725
|| pIdx->pPartIdxWhere!=0
115650116726
|| pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
115651116727
) continue;
115652116728
for(j=0; j<pIdx->nKeyCol; j++){
115653116729
pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
@@ -116133,10 +117209,11 @@
116133117209
}
116134117210
op = OP_OpenWrite;
116135117211
pWInfo->aiCurOnePass[1] = iIndexCur;
116136117212
}else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
116137117213
iIndexCur = iIdxCur;
117214
+ if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
116138117215
}else{
116139117216
iIndexCur = pParse->nTab++;
116140117217
}
116141117218
pLevel->iIdxCur = iIndexCur;
116142117219
assert( pIx->pSchema==pTab->pSchema );
@@ -120457,10 +121534,16 @@
120457121534
testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
120458121535
testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
120459121536
testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
120460121537
testcase( z[0]=='9' );
120461121538
*tokenType = TK_INTEGER;
121539
+#ifndef SQLITE_OMIT_HEX_INTEGER
121540
+ if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
121541
+ for(i=3; sqlite3Isxdigit(z[i]); i++){}
121542
+ return i;
121543
+ }
121544
+#endif
120462121545
for(i=0; sqlite3Isdigit(z[i]); i++){}
120463121546
#ifndef SQLITE_OMIT_FLOATING_POINT
120464121547
if( z[i]=='.' ){
120465121548
i++;
120466121549
while( sqlite3Isdigit(z[i]) ){ i++; }
@@ -121904,10 +122987,12 @@
121904122987
/*
121905122988
** Close an existing SQLite database
121906122989
*/
121907122990
static int sqlite3Close(sqlite3 *db, int forceZombie){
121908122991
if( !db ){
122992
+ /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
122993
+ ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
121909122994
return SQLITE_OK;
121910122995
}
121911122996
if( !sqlite3SafetyCheckSickOrOk(db) ){
121912122997
return SQLITE_MISUSE_BKPT;
121913122998
}
@@ -122133,11 +123218,11 @@
122133123218
122134123219
/*
122135123220
** Return a static string containing the name corresponding to the error code
122136123221
** specified in the argument.
122137123222
*/
122138
-#if defined(SQLITE_TEST)
123223
+#if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST)
122139123224
SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
122140123225
const char *zName = 0;
122141123226
int i, origRc = rc;
122142123227
for(i=0; i<2 && zName==0; i++, rc &= 0xff){
122143123228
switch( rc ){
@@ -122168,11 +123253,10 @@
122168123253
case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
122169123254
case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
122170123255
case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
122171123256
case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
122172123257
case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
122173
- case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break;
122174123258
case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
122175123259
case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
122176123260
case SQLITE_IOERR_CHECKRESERVEDLOCK:
122177123261
zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
122178123262
case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
@@ -123153,11 +124237,11 @@
123153124237
SQLITE_MAX_COMPOUND_SELECT,
123154124238
SQLITE_MAX_VDBE_OP,
123155124239
SQLITE_MAX_FUNCTION_ARG,
123156124240
SQLITE_MAX_ATTACHED,
123157124241
SQLITE_MAX_LIKE_PATTERN_LENGTH,
123158
- SQLITE_MAX_VARIABLE_NUMBER,
124242
+ SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
123159124243
SQLITE_MAX_TRIGGER_DEPTH,
123160124244
};
123161124245
123162124246
/*
123163124247
** Make sure the hard limits are set to reasonable values
@@ -123178,12 +124262,12 @@
123178124262
# error SQLITE_MAX_VDBE_OP must be at least 40
123179124263
#endif
123180124264
#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
123181124265
# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
123182124266
#endif
123183
-#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
123184
-# error SQLITE_MAX_ATTACHED must be between 0 and 62
124267
+#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
124268
+# error SQLITE_MAX_ATTACHED must be between 0 and 125
123185124269
#endif
123186124270
#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
123187124271
# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
123188124272
#endif
123189124273
#if SQLITE_MAX_COLUMN>32767
@@ -124205,14 +125289,14 @@
124205125289
** sqlite3_test_control().
124206125290
*/
124207125291
case SQLITE_TESTCTRL_FAULT_INSTALL: {
124208125292
/* MSVC is picky about pulling func ptrs from va lists.
124209125293
** http://support.microsoft.com/kb/47961
124210
- ** sqlite3Config.xTestCallback = va_arg(ap, int(*)(int));
125294
+ ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
124211125295
*/
124212125296
typedef int(*TESTCALLBACKFUNC_t)(int);
124213
- sqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
125297
+ sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
124214125298
rc = sqlite3FaultSim(0);
124215125299
break;
124216125300
}
124217125301
124218125302
/*
@@ -124438,10 +125522,20 @@
124438125522
sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
124439125523
#endif
124440125524
break;
124441125525
}
124442125526
125527
+ /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
125528
+ **
125529
+ ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
125530
+ ** not.
125531
+ */
125532
+ case SQLITE_TESTCTRL_ISINIT: {
125533
+ if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
125534
+ break;
125535
+ }
125536
+
124443125537
}
124444125538
va_end(ap);
124445125539
#endif /* SQLITE_OMIT_BUILTIN_TEST */
124446125540
return rc;
124447125541
}
@@ -124486,11 +125580,11 @@
124486125580
const char *zParam, /* URI parameter sought */
124487125581
sqlite3_int64 bDflt /* return if parameter is missing */
124488125582
){
124489125583
const char *z = sqlite3_uri_parameter(zFilename, zParam);
124490125584
sqlite3_int64 v;
124491
- if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
125585
+ if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
124492125586
bDflt = v;
124493125587
}
124494125588
return bDflt;
124495125589
}
124496125590
@@ -126017,11 +127111,11 @@
126017127111
126018127112
/* fts3_tokenize_vtab.c */
126019127113
SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
126020127114
126021127115
/* fts3_unicode2.c (functions generated by parsing unicode text files) */
126022
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
127116
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
126023127117
SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
126024127118
SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
126025127119
SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
126026127120
#endif
126027127121
@@ -129487,11 +130581,11 @@
129487130581
** to by the argument to point to the "simple" tokenizer implementation.
129488130582
** And so on.
129489130583
*/
129490130584
SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129491130585
SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129492
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
130586
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
129493130587
SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
129494130588
#endif
129495130589
#ifdef SQLITE_ENABLE_ICU
129496130590
SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129497130591
#endif
@@ -129505,20 +130599,20 @@
129505130599
SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
129506130600
int rc = SQLITE_OK;
129507130601
Fts3Hash *pHash = 0;
129508130602
const sqlite3_tokenizer_module *pSimple = 0;
129509130603
const sqlite3_tokenizer_module *pPorter = 0;
129510
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
130604
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
129511130605
const sqlite3_tokenizer_module *pUnicode = 0;
129512130606
#endif
129513130607
129514130608
#ifdef SQLITE_ENABLE_ICU
129515130609
const sqlite3_tokenizer_module *pIcu = 0;
129516130610
sqlite3Fts3IcuTokenizerModule(&pIcu);
129517130611
#endif
129518130612
129519
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
130613
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
129520130614
sqlite3Fts3UnicodeTokenizer(&pUnicode);
129521130615
#endif
129522130616
129523130617
#ifdef SQLITE_TEST
129524130618
rc = sqlite3Fts3InitTerm(db);
@@ -129542,11 +130636,11 @@
129542130636
/* Load the built-in tokenizers into the hash table */
129543130637
if( rc==SQLITE_OK ){
129544130638
if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
129545130639
|| sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
129546130640
129547
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
130641
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
129548130642
|| sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
129549130643
#endif
129550130644
#ifdef SQLITE_ENABLE_ICU
129551130645
|| (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
129552130646
#endif
@@ -140777,38 +141871,40 @@
140777141871
i64 iDocid = sqlite3_column_int64(pStmt, 0);
140778141872
int iLang = langidFromSelect(p, pStmt);
140779141873
int iCol;
140780141874
140781141875
for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
140782
- const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
140783
- int nText = sqlite3_column_bytes(pStmt, iCol+1);
140784
- sqlite3_tokenizer_cursor *pT = 0;
140785
-
140786
- rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
140787
- while( rc==SQLITE_OK ){
140788
- char const *zToken; /* Buffer containing token */
140789
- int nToken = 0; /* Number of bytes in token */
140790
- int iDum1 = 0, iDum2 = 0; /* Dummy variables */
140791
- int iPos = 0; /* Position of token in zText */
140792
-
140793
- rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
140794
- if( rc==SQLITE_OK ){
140795
- int i;
140796
- cksum2 = cksum2 ^ fts3ChecksumEntry(
140797
- zToken, nToken, iLang, 0, iDocid, iCol, iPos
140798
- );
140799
- for(i=1; i<p->nIndex; i++){
140800
- if( p->aIndex[i].nPrefix<=nToken ){
140801
- cksum2 = cksum2 ^ fts3ChecksumEntry(
140802
- zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
140803
- );
140804
- }
140805
- }
140806
- }
140807
- }
140808
- if( pT ) pModule->xClose(pT);
140809
- if( rc==SQLITE_DONE ) rc = SQLITE_OK;
141876
+ if( p->abNotindexed[iCol]==0 ){
141877
+ const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
141878
+ int nText = sqlite3_column_bytes(pStmt, iCol+1);
141879
+ sqlite3_tokenizer_cursor *pT = 0;
141880
+
141881
+ rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
141882
+ while( rc==SQLITE_OK ){
141883
+ char const *zToken; /* Buffer containing token */
141884
+ int nToken = 0; /* Number of bytes in token */
141885
+ int iDum1 = 0, iDum2 = 0; /* Dummy variables */
141886
+ int iPos = 0; /* Position of token in zText */
141887
+
141888
+ rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
141889
+ if( rc==SQLITE_OK ){
141890
+ int i;
141891
+ cksum2 = cksum2 ^ fts3ChecksumEntry(
141892
+ zToken, nToken, iLang, 0, iDocid, iCol, iPos
141893
+ );
141894
+ for(i=1; i<p->nIndex; i++){
141895
+ if( p->aIndex[i].nPrefix<=nToken ){
141896
+ cksum2 = cksum2 ^ fts3ChecksumEntry(
141897
+ zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
141898
+ );
141899
+ }
141900
+ }
141901
+ }
141902
+ }
141903
+ if( pT ) pModule->xClose(pT);
141904
+ if( rc==SQLITE_DONE ) rc = SQLITE_OK;
141905
+ }
140810141906
}
140811141907
}
140812141908
140813141909
sqlite3_finalize(pStmt);
140814141910
}
@@ -142800,11 +143896,11 @@
142800143896
******************************************************************************
142801143897
**
142802143898
** Implementation of the "unicode" full-text-search tokenizer.
142803143899
*/
142804143900
142805
-#ifdef SQLITE_ENABLE_FTS4_UNICODE61
143901
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
142806143902
142807143903
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142808143904
142809143905
/* #include <assert.h> */
142810143906
/* #include <stdlib.h> */
@@ -143016,11 +144112,11 @@
143016144112
memset(pNew, 0, sizeof(unicode_tokenizer));
143017144113
pNew->bRemoveDiacritic = 1;
143018144114
143019144115
for(i=0; rc==SQLITE_OK && i<nArg; i++){
143020144116
const char *z = azArg[i];
143021
- int n = strlen(z);
144117
+ int n = (int)strlen(z);
143022144118
143023144119
if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
143024144120
pNew->bRemoveDiacritic = 1;
143025144121
}
143026144122
else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
@@ -143103,11 +144199,11 @@
143103144199
int *piEnd, /* OUT: Ending offset of token */
143104144200
int *piPos /* OUT: Position integer of token */
143105144201
){
143106144202
unicode_cursor *pCsr = (unicode_cursor *)pC;
143107144203
unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
143108
- int iCode;
144204
+ int iCode = 0;
143109144205
char *zOut;
143110144206
const unsigned char *z = &pCsr->aInput[pCsr->iOff];
143111144207
const unsigned char *zStart = z;
143112144208
const unsigned char *zEnd;
143113144209
const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
@@ -143148,15 +144244,15 @@
143148144244
}while( unicodeIsAlnum(p, iCode)
143149144245
|| sqlite3FtsUnicodeIsdiacritic(iCode)
143150144246
);
143151144247
143152144248
/* Set the output variables and return. */
143153
- pCsr->iOff = (z - pCsr->aInput);
144249
+ pCsr->iOff = (int)(z - pCsr->aInput);
143154144250
*paToken = pCsr->zToken;
143155
- *pnToken = zOut - pCsr->zToken;
143156
- *piStart = (zStart - pCsr->aInput);
143157
- *piEnd = (zEnd - pCsr->aInput);
144251
+ *pnToken = (int)(zOut - pCsr->zToken);
144252
+ *piStart = (int)(zStart - pCsr->aInput);
144253
+ *piEnd = (int)(zEnd - pCsr->aInput);
143158144254
*piPos = pCsr->iToken++;
143159144255
return SQLITE_OK;
143160144256
}
143161144257
143162144258
/*
@@ -143175,11 +144271,11 @@
143175144271
};
143176144272
*ppModule = &module;
143177144273
}
143178144274
143179144275
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
143180
-#endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
144276
+#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
143181144277
143182144278
/************** End of fts3_unicode.c ****************************************/
143183144279
/************** Begin file fts3_unicode2.c ***********************************/
143184144280
/*
143185144281
** 2012 May 25
@@ -143196,11 +144292,11 @@
143196144292
143197144293
/*
143198144294
** DO NOT EDIT THIS MACHINE GENERATED FILE.
143199144295
*/
143200144296
143201
-#if defined(SQLITE_ENABLE_FTS4_UNICODE61)
144297
+#ifndef SQLITE_DISABLE_FTS3_UNICODE
143202144298
#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
143203144299
143204144300
/* #include <assert.h> */
143205144301
143206144302
/*
@@ -143220,11 +144316,11 @@
143220144316
** the size of the range (always at least 1). In other words, the value
143221144317
** ((C<<22) + N) represents a range of N codepoints starting with codepoint
143222144318
** C. It is not possible to represent a range larger than 1023 codepoints
143223144319
** using this format.
143224144320
*/
143225
- const static unsigned int aEntry[] = {
144321
+ static const unsigned int aEntry[] = {
143226144322
0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
143227144323
0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
143228144324
0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
143229144325
0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
143230144326
0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
@@ -143312,11 +144408,11 @@
143312144408
143313144409
if( c<128 ){
143314144410
return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
143315144411
}else if( c<(1<<22) ){
143316144412
unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
143317
- int iRes;
144413
+ int iRes = 0;
143318144414
int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
143319144415
int iLo = 0;
143320144416
while( iHi>=iLo ){
143321144417
int iTest = (iHi + iLo) / 2;
143322144418
if( key >= aEntry[iTest] ){
@@ -143383,11 +144479,11 @@
143383144479
iHi = iTest-1;
143384144480
}
143385144481
}
143386144482
assert( key>=aDia[iRes] );
143387144483
return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
143388
-};
144484
+}
143389144485
143390144486
143391144487
/*
143392144488
** Return true if the argument interpreted as a unicode codepoint
143393144489
** is a diacritical modifier character.
@@ -143543,11 +144639,11 @@
143543144639
}
143544144640
143545144641
return ret;
143546144642
}
143547144643
#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
143548
-#endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
144644
+#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
143549144645
143550144646
/************** End of fts3_unicode2.c ***************************************/
143551144647
/************** Begin file rtree.c *******************************************/
143552144648
/*
143553144649
** 2001 September 15
@@ -145080,13 +146176,17 @@
145080146176
int rc = SQLITE_OK;
145081146177
int iCell = 0;
145082146178
145083146179
rtreeReference(pRtree);
145084146180
146181
+ /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
145085146182
freeCursorConstraints(pCsr);
146183
+ sqlite3_free(pCsr->aPoint);
146184
+ memset(pCsr, 0, sizeof(RtreeCursor));
146185
+ pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
146186
+
145086146187
pCsr->iStrategy = idxNum;
145087
-
145088146188
if( idxNum==1 ){
145089146189
/* Special case - lookup by rowid. */
145090146190
RtreeNode *pLeaf; /* Leaf on which the required cell resides */
145091146191
RtreeSearchPoint *p; /* Search point for the the leaf */
145092146192
i64 iRowid = sqlite3_value_int64(argv[0]);
145093146193
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.5. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -220,13 +220,13 @@
220 **
221 ** See also: [sqlite3_libversion()],
222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223 ** [sqlite_version()] and [sqlite_source_id()].
224 */
225 #define SQLITE_VERSION "3.8.5"
226 #define SQLITE_VERSION_NUMBER 3008005
227 #define SQLITE_SOURCE_ID "2014-06-04 14:06:34 b1ed4f2a34ba66c29b130f8d13e9092758019212"
228
229 /*
230 ** CAPI3REF: Run-Time Library Version Numbers
231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
232 **
@@ -382,19 +382,19 @@
382 /*
383 ** CAPI3REF: Closing A Database Connection
384 **
385 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
386 ** for the [sqlite3] object.
387 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
388 ** the [sqlite3] object is successfully destroyed and all associated
389 ** resources are deallocated.
390 **
391 ** ^If the database connection is associated with unfinalized prepared
392 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
393 ** will leave the database connection open and return [SQLITE_BUSY].
394 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
395 ** and unfinished sqlite3_backups, then the database connection becomes
396 ** an unusable "zombie" which will automatically be deallocated when the
397 ** last prepared statement is finalized or the last sqlite3_backup is
398 ** finished. The sqlite3_close_v2() interface is intended for use with
399 ** host languages that are garbage collected, and where the order in which
400 ** destructors are called is arbitrary.
@@ -403,11 +403,11 @@
403 ** [sqlite3_blob_close | close] all [BLOB handles], and
404 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
405 ** with the [sqlite3] object prior to attempting to close the object. ^If
406 ** sqlite3_close_v2() is called on a [database connection] that still has
407 ** outstanding [prepared statements], [BLOB handles], and/or
408 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
409 ** of resources is deferred until all [prepared statements], [BLOB handles],
410 ** and [sqlite3_backup] objects are also destroyed.
411 **
412 ** ^If an [sqlite3] object is destroyed while a transaction is open,
413 ** the transaction is automatically rolled back.
@@ -499,20 +499,18 @@
499 char **errmsg /* Error msg written here */
500 );
501
502 /*
503 ** CAPI3REF: Result Codes
504 ** KEYWORDS: SQLITE_OK {error code} {error codes}
505 ** KEYWORDS: {result code} {result codes}
506 **
507 ** Many SQLite functions return an integer result code from the set shown
508 ** here in order to indicate success or failure.
509 **
510 ** New error codes may be added in future versions of SQLite.
511 **
512 ** See also: [SQLITE_IOERR_READ | extended result codes],
513 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
514 */
515 #define SQLITE_OK 0 /* Successful result */
516 /* beginning-of-error-codes */
517 #define SQLITE_ERROR 1 /* SQL error or missing database */
518 #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -546,30 +544,23 @@
546 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
547 /* end-of-error-codes */
548
549 /*
550 ** CAPI3REF: Extended Result Codes
551 ** KEYWORDS: {extended error code} {extended error codes}
552 ** KEYWORDS: {extended result code} {extended result codes}
553 **
554 ** In its default configuration, SQLite API routines return one of 26 integer
555 ** [SQLITE_OK | result codes]. However, experience has shown that many of
556 ** these result codes are too coarse-grained. They do not provide as
557 ** much information about problems as programmers might like. In an effort to
558 ** address this, newer versions of SQLite (version 3.3.8 and later) include
559 ** support for additional result codes that provide more detailed information
560 ** about errors. The extended result codes are enabled or disabled
561 ** on a per database connection basis using the
562 ** [sqlite3_extended_result_codes()] API.
563 **
564 ** Some of the available extended result codes are listed here.
565 ** One may expect the number of extended result codes will increase
566 ** over time. Software that uses extended result codes should expect
567 ** to see new result codes in future releases of SQLite.
568 **
569 ** The SQLITE_OK result code will never be extended. It will always
570 ** be exactly zero.
571 */
572 #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
573 #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
574 #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
575 #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -798,11 +789,11 @@
798 ** write return values. Potential uses for xFileControl() might be
799 ** functions to enable blocking locks with timeouts, to change the
800 ** locking strategy (for example to use dot-file locks), to inquire
801 ** about the status of a lock, or to break stale locks. The SQLite
802 ** core reserves all opcodes less than 100 for its own use.
803 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
804 ** Applications that define a custom xFileControl method should use opcodes
805 ** greater than 100 to avoid conflicts. VFS implementations should
806 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
807 ** recognize.
808 **
@@ -871,10 +862,11 @@
871 /* Additional methods may be added in future releases */
872 };
873
874 /*
875 ** CAPI3REF: Standard File Control Opcodes
 
876 **
877 ** These integer constants are opcodes for the xFileControl method
878 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
879 ** interface.
880 **
@@ -2150,31 +2142,37 @@
2150 SQLITE_API int sqlite3_complete16(const void *sql);
2151
2152 /*
2153 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2154 **
2155 ** ^This routine sets a callback function that might be invoked whenever
2156 ** an attempt is made to open a database table that another thread
2157 ** or process has locked.
 
 
 
 
2158 **
2159 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2160 ** is returned immediately upon encountering the lock. ^If the busy callback
2161 ** is not NULL, then the callback might be invoked with two arguments.
2162 **
2163 ** ^The first argument to the busy handler is a copy of the void* pointer which
2164 ** is the third argument to sqlite3_busy_handler(). ^The second argument to
2165 ** the busy handler callback is the number of times that the busy handler has
2166 ** been invoked for this locking event. ^If the
2167 ** busy callback returns 0, then no additional attempts are made to
2168 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
 
2169 ** ^If the callback returns non-zero, then another attempt
2170 ** is made to open the database for reading and the cycle repeats.
2171 **
2172 ** The presence of a busy handler does not guarantee that it will be invoked
2173 ** when there is lock contention. ^If SQLite determines that invoking the busy
2174 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2175 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
 
2176 ** Consider a scenario where one process is holding a read lock that
2177 ** it is trying to promote to a reserved lock and
2178 ** a second process is holding a reserved lock that it is trying
2179 ** to promote to an exclusive lock. The first process cannot proceed
2180 ** because it is blocked by the second and the second process cannot
@@ -2184,32 +2182,19 @@
2184 ** will induce the first process to release its read lock and allow
2185 ** the second process to proceed.
2186 **
2187 ** ^The default busy callback is NULL.
2188 **
2189 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2190 ** when SQLite is in the middle of a large transaction where all the
2191 ** changes will not fit into the in-memory cache. SQLite will
2192 ** already hold a RESERVED lock on the database file, but it needs
2193 ** to promote this lock to EXCLUSIVE so that it can spill cache
2194 ** pages into the database file without harm to concurrent
2195 ** readers. ^If it is unable to promote the lock, then the in-memory
2196 ** cache will be left in an inconsistent state and so the error
2197 ** code is promoted from the relatively benign [SQLITE_BUSY] to
2198 ** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
2199 ** forces an automatic rollback of the changes. See the
2200 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2201 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2202 ** this is important.
2203 **
2204 ** ^(There can only be a single busy handler defined for each
2205 ** [database connection]. Setting a new busy handler clears any
2206 ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2207 ** will also set or clear the busy handler.
 
2208 **
2209 ** The busy callback should not take any actions which modify the
2210 ** database connection that invoked the busy handler. Any such actions
 
2211 ** result in undefined behavior.
2212 **
2213 ** A busy handler must not close the database connection
2214 ** or [prepared statement] that invoked the busy handler.
2215 */
@@ -2221,19 +2206,21 @@
2221 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2222 ** for a specified amount of time when a table is locked. ^The handler
2223 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2224 ** have accumulated. ^After at least "ms" milliseconds of sleeping,
2225 ** the handler returns 0 which causes [sqlite3_step()] to return
2226 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2227 **
2228 ** ^Calling this routine with an argument less than or equal to zero
2229 ** turns off all busy handlers.
2230 **
2231 ** ^(There can only be a single busy handler for a particular
2232 ** [database connection] any any given moment. If another busy handler
2233 ** was defined (using [sqlite3_busy_handler()]) prior to calling
2234 ** this routine, that other busy handler is cleared.)^
 
 
2235 */
2236 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2237
2238 /*
2239 ** CAPI3REF: Convenience Routines For Running Queries
@@ -2631,12 +2618,12 @@
2631 ** return either [SQLITE_OK] or one of these two constants in order
2632 ** to signal SQLite whether or not the action is permitted. See the
2633 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2634 ** information.
2635 **
2636 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2637 ** from the [sqlite3_vtab_on_conflict()] interface.
2638 */
2639 #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
2640 #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
2641
2642 /*
@@ -4818,10 +4805,17 @@
4818 ** the name of a folder (a.k.a. directory), then all temporary files
4819 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4820 ** will be placed in that directory.)^ ^If this variable
4821 ** is a NULL pointer, then SQLite performs a search for an appropriate
4822 ** temporary file directory.
 
 
 
 
 
 
 
4823 **
4824 ** It is not safe to read or modify this variable in more than one
4825 ** thread at a time. It is not safe to read or modify this variable
4826 ** if a [database connection] is being used at the same time in a separate
4827 ** thread.
@@ -4837,10 +4831,15 @@
4837 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4838 ** using [sqlite3_free].
4839 ** Hence, if this variable is modified directly, either it should be
4840 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4841 ** or else the use of the [temp_store_directory pragma] should be avoided.
 
 
 
 
 
4842 **
4843 ** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4844 ** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
4845 ** features that require the use of temporary files may fail. Here is an
4846 ** example of how to do this using C++ with the Windows Runtime:
@@ -5971,14 +5970,16 @@
5971 ** <ul>
5972 ** <li> SQLITE_MUTEX_FAST
5973 ** <li> SQLITE_MUTEX_RECURSIVE
5974 ** <li> SQLITE_MUTEX_STATIC_MASTER
5975 ** <li> SQLITE_MUTEX_STATIC_MEM
5976 ** <li> SQLITE_MUTEX_STATIC_MEM2
5977 ** <li> SQLITE_MUTEX_STATIC_PRNG
5978 ** <li> SQLITE_MUTEX_STATIC_LRU
5979 ** <li> SQLITE_MUTEX_STATIC_LRU2
 
 
5980 ** </ul>)^
5981 **
5982 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5983 ** cause sqlite3_mutex_alloc() to create
5984 ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6178,10 +6179,13 @@
6178 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
6179 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
6180 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
6181 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
6182 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
 
 
 
6183
6184 /*
6185 ** CAPI3REF: Retrieve the mutex for a database connection
6186 **
6187 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6273,11 +6277,12 @@
6273 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6274 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6275 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6276 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6277 #define SQLITE_TESTCTRL_BYTEORDER 22
6278 #define SQLITE_TESTCTRL_LAST 22
 
6279
6280 /*
6281 ** CAPI3REF: SQLite Runtime Status
6282 **
6283 ** ^This interface is used to retrieve runtime status information
@@ -7256,10 +7261,13 @@
7256 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7257 ** configured by this function.
7258 **
7259 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7260 ** from SQL.
 
 
 
7261 **
7262 ** ^Every new [database connection] defaults to having the auto-checkpoint
7263 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7264 ** pages. The use of this interface
7265 ** is only necessary if the default setting is found to be suboptimal
@@ -7273,10 +7281,14 @@
7273 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7274 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
7275 ** empty string, then a checkpoint is run on all databases of
7276 ** connection D. ^If the database connection D is not in
7277 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
 
 
 
 
7278 **
7279 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7280 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
7281 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7282 ** run whenever the WAL reaches a certain size threshold.
@@ -7295,22 +7307,25 @@
7295 ** <dl>
7296 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7297 ** Checkpoint as many frames as possible without waiting for any database
7298 ** readers or writers to finish. Sync the db file if all frames in the log
7299 ** are checkpointed. This mode is the same as calling
7300 ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
 
7301 **
7302 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7303 ** This mode blocks (calls the busy-handler callback) until there is no
 
7304 ** database writer and all readers are reading from the most recent database
7305 ** snapshot. It then checkpoints all frames in the log file and syncs the
7306 ** database file. This call blocks database writers while it is running,
7307 ** but not database readers.
7308 **
7309 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7310 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7311 ** checkpointing the log file it blocks (calls the busy-handler callback)
 
7312 ** until all readers are reading from the database file only. This ensures
7313 ** that the next client to write to the database file restarts the log file
7314 ** from the beginning. This call blocks database writers while it is running,
7315 ** but not database readers.
7316 ** </dl>
@@ -7444,10 +7459,11 @@
7444 */
7445 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7446
7447 /*
7448 ** CAPI3REF: Conflict resolution modes
 
7449 **
7450 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7451 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7452 ** is for the SQL statement being evaluated.
7453 **
@@ -9285,43 +9301,43 @@
9285 #define OP_Affinity 47 /* synopsis: affinity(r[P1@P2]) */
9286 #define OP_MakeRecord 48 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
9287 #define OP_Count 49 /* synopsis: r[P2]=count() */
9288 #define OP_ReadCookie 50
9289 #define OP_SetCookie 51
9290 #define OP_OpenRead 52 /* synopsis: root=P2 iDb=P3 */
9291 #define OP_OpenWrite 53 /* synopsis: root=P2 iDb=P3 */
9292 #define OP_OpenAutoindex 54 /* synopsis: nColumn=P2 */
9293 #define OP_OpenEphemeral 55 /* synopsis: nColumn=P2 */
9294 #define OP_SorterOpen 56
9295 #define OP_OpenPseudo 57 /* synopsis: P3 columns in r[P2] */
9296 #define OP_Close 58
9297 #define OP_SeekLT 59
9298 #define OP_SeekLE 60
9299 #define OP_SeekGE 61
9300 #define OP_SeekGT 62
9301 #define OP_Seek 63 /* synopsis: intkey=r[P2] */
9302 #define OP_NoConflict 64 /* synopsis: key=r[P3@P4] */
9303 #define OP_NotFound 65 /* synopsis: key=r[P3@P4] */
9304 #define OP_Found 66 /* synopsis: key=r[P3@P4] */
9305 #define OP_NotExists 67 /* synopsis: intkey=r[P3] */
9306 #define OP_Sequence 68 /* synopsis: r[P2]=cursor[P1].ctr++ */
9307 #define OP_NewRowid 69 /* synopsis: r[P2]=rowid */
9308 #define OP_Insert 70 /* synopsis: intkey=r[P3] data=r[P2] */
9309 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9310 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9311 #define OP_InsertInt 73 /* synopsis: intkey=P3 data=r[P2] */
9312 #define OP_Delete 74
9313 #define OP_ResetCount 75
9314 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9315 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9316 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9317 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9318 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9319 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9320 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9321 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9322 #define OP_SorterCompare 84 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
9323 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9324 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9325 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9326 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9327 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9328,73 +9344,74 @@
9328 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9329 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9330 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9331 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9332 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9333 #define OP_SorterData 95 /* synopsis: r[P2]=data */
9334 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9335 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9336 #define OP_RowKey 98 /* synopsis: r[P2]=key */
9337 #define OP_RowData 99 /* synopsis: r[P2]=data */
9338 #define OP_Rowid 100 /* synopsis: r[P2]=rowid */
9339 #define OP_NullRow 101
9340 #define OP_Last 102
9341 #define OP_SorterSort 103
9342 #define OP_Sort 104
9343 #define OP_Rewind 105
9344 #define OP_SorterInsert 106
9345 #define OP_IdxInsert 107 /* synopsis: key=r[P2] */
9346 #define OP_IdxDelete 108 /* synopsis: key=r[P2@P3] */
9347 #define OP_IdxRowid 109 /* synopsis: r[P2]=rowid */
9348 #define OP_IdxLE 110 /* synopsis: key=r[P3@P4] */
9349 #define OP_IdxGT 111 /* synopsis: key=r[P3@P4] */
9350 #define OP_IdxLT 112 /* synopsis: key=r[P3@P4] */
9351 #define OP_IdxGE 113 /* synopsis: key=r[P3@P4] */
9352 #define OP_Destroy 114
9353 #define OP_Clear 115
9354 #define OP_ResetSorter 116
9355 #define OP_CreateIndex 117 /* synopsis: r[P2]=root iDb=P1 */
9356 #define OP_CreateTable 118 /* synopsis: r[P2]=root iDb=P1 */
9357 #define OP_ParseSchema 119
9358 #define OP_LoadAnalysis 120
9359 #define OP_DropTable 121
9360 #define OP_DropIndex 122
9361 #define OP_DropTrigger 123
9362 #define OP_IntegrityCk 124
9363 #define OP_RowSetAdd 125 /* synopsis: rowset(P1)=r[P2] */
9364 #define OP_RowSetRead 126 /* synopsis: r[P3]=rowset(P1) */
9365 #define OP_RowSetTest 127 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9366 #define OP_Program 128
9367 #define OP_Param 129
9368 #define OP_FkCounter 130 /* synopsis: fkctr[P1]+=P2 */
9369 #define OP_FkIfZero 131 /* synopsis: if fkctr[P1]==0 goto P2 */
9370 #define OP_MemMax 132 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9371 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9372 #define OP_IfPos 134 /* synopsis: if r[P1]>0 goto P2 */
9373 #define OP_IfNeg 135 /* synopsis: if r[P1]<0 goto P2 */
9374 #define OP_IfZero 136 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2 */
9375 #define OP_AggFinal 137 /* synopsis: accum=r[P1] N=P2 */
9376 #define OP_IncrVacuum 138
9377 #define OP_Expire 139
9378 #define OP_TableLock 140 /* synopsis: iDb=P1 root=P2 write=P3 */
9379 #define OP_VBegin 141
9380 #define OP_VCreate 142
9381 #define OP_ToText 143 /* same as TK_TO_TEXT */
9382 #define OP_ToBlob 144 /* same as TK_TO_BLOB */
9383 #define OP_ToNumeric 145 /* same as TK_TO_NUMERIC */
9384 #define OP_ToInt 146 /* same as TK_TO_INT */
9385 #define OP_ToReal 147 /* same as TK_TO_REAL */
9386 #define OP_VDestroy 148
9387 #define OP_VOpen 149
9388 #define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */
9389 #define OP_VNext 151
9390 #define OP_VRename 152
9391 #define OP_Pagecount 153
9392 #define OP_MaxPgcnt 154
9393 #define OP_Init 155 /* synopsis: Start at P2 */
9394 #define OP_Noop 156
9395 #define OP_Explain 157
 
9396
9397
9398 /* Properties such as "out2" or "jump" that are specified in
9399 ** comments following the "case" for each opcode in the vdbe.c
9400 ** are encoded into bitvectors as follows:
@@ -9412,23 +9429,23 @@
9412 /* 16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
9413 /* 24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
9414 /* 32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
9415 /* 40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
9416 /* 48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
9417 /* 56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
9418 /* 64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
9419 /* 72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
9420 /* 80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
9421 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
9422 /* 96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
9423 /* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
9424 /* 112 */ 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02, 0x00,\
9425 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15,\
9426 /* 128 */ 0x01, 0x02, 0x00, 0x01, 0x08, 0x02, 0x05, 0x05,\
9427 /* 136 */ 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04,\
9428 /* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01,\
9429 /* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
9430
9431 /************** End of opcodes.h *********************************************/
9432 /************** Continuing where we left off in vdbe.h ***********************/
9433
9434 /*
@@ -9479,10 +9496,11 @@
9479 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
9480 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
9481 #ifndef SQLITE_OMIT_TRACE
9482 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9483 #endif
 
9484
9485 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9486 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
9487 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
9488
@@ -10330,22 +10348,22 @@
10330 Hash trigHash; /* All triggers indexed by name */
10331 Hash fkeyHash; /* All foreign keys by referenced table name */
10332 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
10333 u8 file_format; /* Schema format version for this file */
10334 u8 enc; /* Text encoding used by this database */
10335 u16 flags; /* Flags associated with this schema */
10336 int cache_size; /* Number of pages to use in the cache */
10337 };
10338
10339 /*
10340 ** These macros can be used to test, set, or clear bits in the
10341 ** Db.pSchema->flags field.
10342 */
10343 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
10344 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
10345 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
10346 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
10347
10348 /*
10349 ** Allowed values for the DB.pSchema->flags field.
10350 **
10351 ** The DB_SchemaLoaded flag is set after the database schema has been
@@ -10931,10 +10949,13 @@
10931 int tnum; /* Root BTree node for this table (see note above) */
10932 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10933 i16 nCol; /* Number of columns in this table */
10934 u16 nRef; /* Number of pointers to this Table */
10935 LogEst szTabRow; /* Estimated size of each table row in bytes */
 
 
 
10936 u8 tabFlags; /* Mask of TF_* values */
10937 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
10938 #ifndef SQLITE_OMIT_ALTERTABLE
10939 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
10940 #endif
@@ -11171,10 +11192,13 @@
11171 #define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
11172
11173 /* Return true if index X is a PRIMARY KEY index */
11174 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
11175
 
 
 
11176 /*
11177 ** Each sample stored in the sqlite_stat3 table is represented in memory
11178 ** using a structure of this type. See documentation at the top of the
11179 ** analyze.c source file for additional information.
11180 */
@@ -11590,10 +11614,11 @@
11590 #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
11591 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11592 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11593 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11594 #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
 
11595
11596 /* Allowed return values from sqlite3WhereIsDistinct()
11597 */
11598 #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11599 #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -11846,13 +11871,23 @@
11846
11847 /*
11848 ** The yDbMask datatype for the bitmask of all attached databases.
11849 */
11850 #if SQLITE_MAX_ATTACHED>30
11851 typedef sqlite3_uint64 yDbMask;
 
 
 
 
 
11852 #else
11853 typedef unsigned int yDbMask;
 
 
 
 
 
11854 #endif
11855
11856 /*
11857 ** An SQL parser context. A copy of this structure is passed through
11858 ** the parser and down into all the parser action routine in order to
@@ -12521,10 +12556,13 @@
12521 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
12522 #else
12523 # define sqlite3ViewGetColumnNames(A,B) 0
12524 #endif
12525
 
 
 
12526 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
12527 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
12528 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
12529 #ifndef SQLITE_OMIT_AUTOINCREMENT
12530 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
@@ -12771,10 +12809,11 @@
12771 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
12772 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
12773 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
12774 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
12775 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 
12776 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
12777 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
12778 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
12779 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
12780
@@ -12800,11 +12839,11 @@
12800 #ifdef SQLITE_ENABLE_8_3_NAMES
12801 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
12802 #else
12803 # define sqlite3FileSuffix3(X,Y)
12804 #endif
12805 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
12806
12807 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
12808 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
12809 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12810 void(*)(void*));
@@ -12885,11 +12924,13 @@
12885 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12886
12887 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12888 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12889 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
 
12890 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
 
12891 #endif
12892
12893 /*
12894 ** The interface to the LEMON-generated parser
12895 */
@@ -13022,15 +13063,25 @@
13022 #else
13023 #define sqlite3BeginBenignMalloc()
13024 #define sqlite3EndBenignMalloc()
13025 #endif
13026
13027 #define IN_INDEX_ROWID 1
13028 #define IN_INDEX_EPH 2
13029 #define IN_INDEX_INDEX_ASC 3
13030 #define IN_INDEX_INDEX_DESC 4
13031 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
 
 
 
 
 
 
 
 
 
 
13032
13033 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13034 SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
13035 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
13036 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
@@ -13873,18 +13924,22 @@
13873 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
13874 int seekResult; /* Result of previous sqlite3BtreeMoveto() */
13875 int pseudoTableReg; /* Register holding pseudotable content. */
13876 i16 nField; /* Number of fields in the header */
13877 u16 nHdrParsed; /* Number of header fields parsed so far */
 
 
 
13878 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
13879 u8 nullRow; /* True if pointing to a row with no data */
13880 u8 rowidIsValid; /* True if lastRowid is valid */
13881 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
13882 Bool isEphemeral:1; /* True for an ephemeral table */
13883 Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
13884 Bool isTable:1; /* True if a table requiring integer keys */
13885 Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
 
13886 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
13887 i64 seqCount; /* Sequence counter */
13888 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
13889 i64 lastRowid; /* Rowid being deleted by OP_Delete */
13890 VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
@@ -14199,11 +14254,10 @@
14199 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14200
14201 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14202 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
14203 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14204 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14205 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14206 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14207 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
14208 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
14209 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
@@ -18394,11 +18448,11 @@
18394 /*
18395 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18396 */
18397 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18398 #ifndef SQLITE_OMIT_AUTOINIT
18399 if( sqlite3_initialize() ) return 0;
18400 #endif
18401 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18402 }
18403
18404 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -18575,11 +18629,11 @@
18575 ** The sqlite3_mutex_alloc() routine allocates a new
18576 ** mutex and returns a pointer to it. If it returns NULL
18577 ** that means that a mutex could not be allocated.
18578 */
18579 static sqlite3_mutex *debugMutexAlloc(int id){
18580 static sqlite3_debug_mutex aStatic[6];
18581 sqlite3_debug_mutex *pNew = 0;
18582 switch( id ){
18583 case SQLITE_MUTEX_FAST:
18584 case SQLITE_MUTEX_RECURSIVE: {
18585 pNew = sqlite3Malloc(sizeof(*pNew));
@@ -18772,14 +18826,17 @@
18772 ** <ul>
18773 ** <li> SQLITE_MUTEX_FAST
18774 ** <li> SQLITE_MUTEX_RECURSIVE
18775 ** <li> SQLITE_MUTEX_STATIC_MASTER
18776 ** <li> SQLITE_MUTEX_STATIC_MEM
18777 ** <li> SQLITE_MUTEX_STATIC_MEM2
18778 ** <li> SQLITE_MUTEX_STATIC_PRNG
18779 ** <li> SQLITE_MUTEX_STATIC_LRU
18780 ** <li> SQLITE_MUTEX_STATIC_PMEM
 
 
 
18781 ** </ul>
18782 **
18783 ** The first two constants cause sqlite3_mutex_alloc() to create
18784 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
18785 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -18804,10 +18861,13 @@
18804 ** mutex types, the same mutex is returned on every call that has
18805 ** the same type number.
18806 */
18807 static sqlite3_mutex *pthreadMutexAlloc(int iType){
18808 static sqlite3_mutex staticMutexes[] = {
 
 
 
18809 SQLITE3_MUTEX_INITIALIZER,
18810 SQLITE3_MUTEX_INITIALIZER,
18811 SQLITE3_MUTEX_INITIALIZER,
18812 SQLITE3_MUTEX_INITIALIZER,
18813 SQLITE3_MUTEX_INITIALIZER,
@@ -19039,14 +19099,227 @@
19039 ** May you do good and not evil.
19040 ** May you find forgiveness for yourself and forgive others.
19041 ** May you share freely, never taking more than you give.
19042 **
19043 *************************************************************************
19044 ** This file contains the C functions that implement mutexes for win32
19045 */
19046
19047 #if SQLITE_OS_WIN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19048 /*
19049 ** Include the header file for the Windows VFS.
19050 */
19051 /************** Include os_win.h in the middle of mutex_w32.c ****************/
19052 /************** Begin file os_win.h ******************************************/
@@ -19122,11 +19395,11 @@
19122 /************** Continuing where we left off in mutex_w32.c ******************/
19123 #endif
19124
19125 /*
19126 ** The code in this file is only used if we are compiling multithreaded
19127 ** on a win32 system.
19128 */
19129 #ifdef SQLITE_MUTEX_W32
19130
19131 /*
19132 ** Each recursive mutex is an instance of the following structure.
@@ -19135,94 +19408,75 @@
19135 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
19136 int id; /* Mutex type */
19137 #ifdef SQLITE_DEBUG
19138 volatile int nRef; /* Number of enterances */
19139 volatile DWORD owner; /* Thread holding this mutex */
19140 int trace; /* True to trace changes */
19141 #endif
19142 };
 
 
 
 
 
 
19143 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 
19144 #ifdef SQLITE_DEBUG
19145 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
 
19146 #else
19147 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
19148 #endif
19149
19150 /*
19151 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
19152 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
19153 **
19154 ** Here is an interesting observation: Win95, Win98, and WinME lack
19155 ** the LockFileEx() API. But we can still statically link against that
19156 ** API as long as we don't call it win running Win95/98/ME. A call to
19157 ** this routine is used to determine if the host is Win95/98/ME or
19158 ** WinNT/2K/XP so that we will know whether or not we can safely call
19159 ** the LockFileEx() API.
19160 **
19161 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
19162 ** which is only available if your application was compiled with
19163 ** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
19164 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
19165 ** this out as well.
19166 */
19167 #if 0
19168 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
19169 # define mutexIsNT() (1)
19170 #else
19171 static int mutexIsNT(void){
19172 static int osType = 0;
19173 if( osType==0 ){
19174 OSVERSIONINFO sInfo;
19175 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
19176 GetVersionEx(&sInfo);
19177 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
19178 }
19179 return osType==2;
19180 }
19181 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
19182 #endif
19183
19184 #ifdef SQLITE_DEBUG
19185 /*
19186 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
19187 ** intended for use only inside assert() statements.
19188 */
19189 static int winMutexHeld(sqlite3_mutex *p){
19190 return p->nRef!=0 && p->owner==GetCurrentThreadId();
19191 }
 
19192 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
19193 return p->nRef==0 || p->owner!=tid;
19194 }
 
19195 static int winMutexNotheld(sqlite3_mutex *p){
19196 DWORD tid = GetCurrentThreadId();
19197 return winMutexNotheld2(p, tid);
19198 }
19199 #endif
19200
19201
19202 /*
19203 ** Initialize and deinitialize the mutex subsystem.
19204 */
19205 static sqlite3_mutex winMutex_staticMutexes[6] = {
 
 
 
19206 SQLITE3_MUTEX_INITIALIZER,
19207 SQLITE3_MUTEX_INITIALIZER,
19208 SQLITE3_MUTEX_INITIALIZER,
19209 SQLITE3_MUTEX_INITIALIZER,
19210 SQLITE3_MUTEX_INITIALIZER,
19211 SQLITE3_MUTEX_INITIALIZER
19212 };
 
19213 static int winMutex_isInit = 0;
19214 /* As winMutexInit() and winMutexEnd() are called as part
19215 ** of the sqlite3_initialize and sqlite3_shutdown()
19216 ** processing, the "interlocked" magic is probably not
19217 ** strictly necessary.
 
19218 */
19219 static LONG winMutex_lock = 0;
19220
 
19221 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
19222
19223 static int winMutexInit(void){
19224 /* The first to increment to 1 does actual initialization */
19225 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
19226 int i;
19227 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
19228 #if SQLITE_OS_WINRT
@@ -19231,20 +19485,21 @@
19231 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
19232 #endif
19233 }
19234 winMutex_isInit = 1;
19235 }else{
19236 /* Someone else is in the process of initing the static mutexes */
 
19237 while( !winMutex_isInit ){
19238 sqlite3_win32_sleep(1);
19239 }
19240 }
19241 return SQLITE_OK;
19242 }
19243
19244 static int winMutexEnd(void){
19245 /* The first to decrement to 0 does actual shutdown
19246 ** (which should be the last to shutdown.) */
19247 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
19248 if( winMutex_isInit==1 ){
19249 int i;
19250 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
@@ -19251,11 +19506,11 @@
19251 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
19252 }
19253 winMutex_isInit = 0;
19254 }
19255 }
19256 return SQLITE_OK;
19257 }
19258
19259 /*
19260 ** The sqlite3_mutex_alloc() routine allocates a new
19261 ** mutex and returns a pointer to it. If it returns NULL
@@ -19266,14 +19521,17 @@
19266 ** <ul>
19267 ** <li> SQLITE_MUTEX_FAST
19268 ** <li> SQLITE_MUTEX_RECURSIVE
19269 ** <li> SQLITE_MUTEX_STATIC_MASTER
19270 ** <li> SQLITE_MUTEX_STATIC_MEM
19271 ** <li> SQLITE_MUTEX_STATIC_MEM2
19272 ** <li> SQLITE_MUTEX_STATIC_PRNG
19273 ** <li> SQLITE_MUTEX_STATIC_LRU
19274 ** <li> SQLITE_MUTEX_STATIC_PMEM
 
 
 
19275 ** </ul>
19276 **
19277 ** The first two constants cause sqlite3_mutex_alloc() to create
19278 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
19279 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -19292,11 +19550,11 @@
19292 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
19293 ** SQLITE_MUTEX_RECURSIVE.
19294 **
19295 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
19296 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
19297 ** returns a different mutex on every call. But for the static
19298 ** mutex types, the same mutex is returned on every call that has
19299 ** the same type number.
19300 */
19301 static sqlite3_mutex *winMutexAlloc(int iType){
19302 sqlite3_mutex *p;
@@ -19303,13 +19561,16 @@
19303
19304 switch( iType ){
19305 case SQLITE_MUTEX_FAST:
19306 case SQLITE_MUTEX_RECURSIVE: {
19307 p = sqlite3MallocZero( sizeof(*p) );
19308 if( p ){
19309 #ifdef SQLITE_DEBUG
19310 p->id = iType;
 
 
 
19311 #endif
19312 #if SQLITE_OS_WINRT
19313 InitializeCriticalSectionEx(&p->mutex, 0, 0);
19314 #else
19315 InitializeCriticalSection(&p->mutex);
@@ -19316,16 +19577,19 @@
19316 #endif
19317 }
19318 break;
19319 }
19320 default: {
19321 assert( winMutex_isInit==1 );
19322 assert( iType-2 >= 0 );
19323 assert( iType-2 < ArraySize(winMutex_staticMutexes) );
 
19324 p = &winMutex_staticMutexes[iType-2];
19325 #ifdef SQLITE_DEBUG
19326 p->id = iType;
 
 
 
19327 #endif
19328 break;
19329 }
19330 }
19331 return p;
@@ -19337,12 +19601,15 @@
19337 ** allocated mutex. SQLite is careful to deallocate every
19338 ** mutex that it allocates.
19339 */
19340 static void winMutexFree(sqlite3_mutex *p){
19341 assert( p );
 
19342 assert( p->nRef==0 && p->owner==0 );
19343 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 
 
19344 DeleteCriticalSection(&p->mutex);
19345 sqlite3_free(p);
19346 }
19347
19348 /*
@@ -19355,53 +19622,71 @@
19355 ** mutex must be exited an equal number of times before another thread
19356 ** can enter. If the same thread tries to enter any other kind of mutex
19357 ** more than once, the behavior is undefined.
19358 */
19359 static void winMutexEnter(sqlite3_mutex *p){
 
 
 
19360 #ifdef SQLITE_DEBUG
19361 DWORD tid = GetCurrentThreadId();
19362 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 
 
19363 #endif
 
19364 EnterCriticalSection(&p->mutex);
19365 #ifdef SQLITE_DEBUG
19366 assert( p->nRef>0 || p->owner==0 );
19367 p->owner = tid;
19368 p->nRef++;
19369 if( p->trace ){
19370 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 
19371 }
19372 #endif
19373 }
 
19374 static int winMutexTry(sqlite3_mutex *p){
19375 #ifndef NDEBUG
19376 DWORD tid = GetCurrentThreadId();
19377 #endif
19378 int rc = SQLITE_BUSY;
 
19379 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19380 /*
19381 ** The sqlite3_mutex_try() routine is very rarely used, and when it
19382 ** is used it is merely an optimization. So it is OK for it to always
19383 ** fail.
19384 **
19385 ** The TryEnterCriticalSection() interface is only available on WinNT.
19386 ** And some windows compilers complain if you try to use it without
19387 ** first doing some #defines that prevent SQLite from building on Win98.
19388 ** For that reason, we will omit this optimization for now. See
19389 ** ticket #2685.
19390 */
19391 #if 0
19392 if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
 
 
 
 
 
 
 
19393 p->owner = tid;
19394 p->nRef++;
 
19395 rc = SQLITE_OK;
19396 }
19397 #else
19398 UNUSED_PARAMETER(p);
19399 #endif
19400 #ifdef SQLITE_DEBUG
19401 if( rc==SQLITE_OK && p->trace ){
19402 printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 
19403 }
19404 #endif
19405 return rc;
19406 }
19407
@@ -19410,22 +19695,27 @@
19410 ** previously entered by the same thread. The behavior
19411 ** is undefined if the mutex is not currently entered or
19412 ** is not currently allocated. SQLite will never do either.
19413 */
19414 static void winMutexLeave(sqlite3_mutex *p){
19415 #ifndef NDEBUG
19416 DWORD tid = GetCurrentThreadId();
 
 
 
19417 assert( p->nRef>0 );
19418 assert( p->owner==tid );
19419 p->nRef--;
19420 if( p->nRef==0 ) p->owner = 0;
19421 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
19422 #endif
 
19423 LeaveCriticalSection(&p->mutex);
19424 #ifdef SQLITE_DEBUG
19425 if( p->trace ){
19426 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 
19427 }
19428 #endif
19429 }
19430
19431 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
@@ -19443,13 +19733,13 @@
19443 #else
19444 0,
19445 0
19446 #endif
19447 };
19448
19449 return &sMutex;
19450 }
 
19451 #endif /* SQLITE_MUTEX_W32 */
19452
19453 /************** End of mutex_w32.c *******************************************/
19454 /************** Begin file malloc.c ******************************************/
19455 /*
@@ -21562,12 +21852,12 @@
21562 ** * Bytes in the range of 0x80 through 0xbf which occur as the first
21563 ** byte of a character are interpreted as single-byte characters
21564 ** and rendered as themselves even though they are technically
21565 ** invalid characters.
21566 **
21567 ** * This routine accepts an infinite number of different UTF8 encodings
21568 ** for unicode values 0x80 and greater. It do not change over-length
21569 ** encodings to 0xfffd as some systems recommend.
21570 */
21571 #define READ_UTF8(zIn, zTerm, c) \
21572 c = *(zIn++); \
21573 if( c>=0xc0 ){ \
@@ -22420,13 +22710,13 @@
22420 testcase( c==(+1) );
22421 }
22422 return c;
22423 }
22424
22425
22426 /*
22427 ** Convert zNum to a 64-bit signed integer.
 
22428 **
22429 ** If the zNum value is representable as a 64-bit twos-complement
22430 ** integer, then write that value into *pNum and return 0.
22431 **
22432 ** If zNum is exactly 9223372036854775808, return 2. This special
@@ -22509,14 +22799,48 @@
22509 assert( u-1==LARGEST_INT64 );
22510 return neg ? 0 : 2;
22511 }
22512 }
22513 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22514
22515 /*
22516 ** If zNum represents an integer that will fit in 32-bits, then set
22517 ** *pValue to that integer and return true. Otherwise return false.
 
 
22518 **
22519 ** Any non-numeric characters that following zNum are ignored.
22520 ** This is different from sqlite3Atoi64() which requires the
22521 ** input number to be zero-terminated.
22522 */
@@ -22528,11 +22852,29 @@
22528 neg = 1;
22529 zNum++;
22530 }else if( zNum[0]=='+' ){
22531 zNum++;
22532 }
22533 while( zNum[0]=='0' ) zNum++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22534 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
22535 v = v*10 + c;
22536 }
22537
22538 /* The longest decimal representation of a 32 bit integer is 10 digits:
@@ -23604,43 +23946,43 @@
23604 /* 47 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
23605 /* 48 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
23606 /* 49 */ "Count" OpHelp("r[P2]=count()"),
23607 /* 50 */ "ReadCookie" OpHelp(""),
23608 /* 51 */ "SetCookie" OpHelp(""),
23609 /* 52 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
23610 /* 53 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
23611 /* 54 */ "OpenAutoindex" OpHelp("nColumn=P2"),
23612 /* 55 */ "OpenEphemeral" OpHelp("nColumn=P2"),
23613 /* 56 */ "SorterOpen" OpHelp(""),
23614 /* 57 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
23615 /* 58 */ "Close" OpHelp(""),
23616 /* 59 */ "SeekLT" OpHelp(""),
23617 /* 60 */ "SeekLE" OpHelp(""),
23618 /* 61 */ "SeekGE" OpHelp(""),
23619 /* 62 */ "SeekGT" OpHelp(""),
23620 /* 63 */ "Seek" OpHelp("intkey=r[P2]"),
23621 /* 64 */ "NoConflict" OpHelp("key=r[P3@P4]"),
23622 /* 65 */ "NotFound" OpHelp("key=r[P3@P4]"),
23623 /* 66 */ "Found" OpHelp("key=r[P3@P4]"),
23624 /* 67 */ "NotExists" OpHelp("intkey=r[P3]"),
23625 /* 68 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
23626 /* 69 */ "NewRowid" OpHelp("r[P2]=rowid"),
23627 /* 70 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
23628 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
23629 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
23630 /* 73 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
23631 /* 74 */ "Delete" OpHelp(""),
23632 /* 75 */ "ResetCount" OpHelp(""),
23633 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
23634 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
23635 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
23636 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
23637 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
23638 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
23639 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
23640 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
23641 /* 84 */ "SorterCompare" OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
23642 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
23643 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
23644 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
23645 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
23646 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -23647,73 +23989,74 @@
23647 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
23648 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
23649 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
23650 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
23651 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
23652 /* 95 */ "SorterData" OpHelp("r[P2]=data"),
23653 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
23654 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
23655 /* 98 */ "RowKey" OpHelp("r[P2]=key"),
23656 /* 99 */ "RowData" OpHelp("r[P2]=data"),
23657 /* 100 */ "Rowid" OpHelp("r[P2]=rowid"),
23658 /* 101 */ "NullRow" OpHelp(""),
23659 /* 102 */ "Last" OpHelp(""),
23660 /* 103 */ "SorterSort" OpHelp(""),
23661 /* 104 */ "Sort" OpHelp(""),
23662 /* 105 */ "Rewind" OpHelp(""),
23663 /* 106 */ "SorterInsert" OpHelp(""),
23664 /* 107 */ "IdxInsert" OpHelp("key=r[P2]"),
23665 /* 108 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
23666 /* 109 */ "IdxRowid" OpHelp("r[P2]=rowid"),
23667 /* 110 */ "IdxLE" OpHelp("key=r[P3@P4]"),
23668 /* 111 */ "IdxGT" OpHelp("key=r[P3@P4]"),
23669 /* 112 */ "IdxLT" OpHelp("key=r[P3@P4]"),
23670 /* 113 */ "IdxGE" OpHelp("key=r[P3@P4]"),
23671 /* 114 */ "Destroy" OpHelp(""),
23672 /* 115 */ "Clear" OpHelp(""),
23673 /* 116 */ "ResetSorter" OpHelp(""),
23674 /* 117 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
23675 /* 118 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
23676 /* 119 */ "ParseSchema" OpHelp(""),
23677 /* 120 */ "LoadAnalysis" OpHelp(""),
23678 /* 121 */ "DropTable" OpHelp(""),
23679 /* 122 */ "DropIndex" OpHelp(""),
23680 /* 123 */ "DropTrigger" OpHelp(""),
23681 /* 124 */ "IntegrityCk" OpHelp(""),
23682 /* 125 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
23683 /* 126 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
23684 /* 127 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
23685 /* 128 */ "Program" OpHelp(""),
23686 /* 129 */ "Param" OpHelp(""),
23687 /* 130 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
23688 /* 131 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
23689 /* 132 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
23690 /* 133 */ "Real" OpHelp("r[P2]=P4"),
23691 /* 134 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
23692 /* 135 */ "IfNeg" OpHelp("if r[P1]<0 goto P2"),
23693 /* 136 */ "IfZero" OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
23694 /* 137 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
23695 /* 138 */ "IncrVacuum" OpHelp(""),
23696 /* 139 */ "Expire" OpHelp(""),
23697 /* 140 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
23698 /* 141 */ "VBegin" OpHelp(""),
23699 /* 142 */ "VCreate" OpHelp(""),
23700 /* 143 */ "ToText" OpHelp(""),
23701 /* 144 */ "ToBlob" OpHelp(""),
23702 /* 145 */ "ToNumeric" OpHelp(""),
23703 /* 146 */ "ToInt" OpHelp(""),
23704 /* 147 */ "ToReal" OpHelp(""),
23705 /* 148 */ "VDestroy" OpHelp(""),
23706 /* 149 */ "VOpen" OpHelp(""),
23707 /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
23708 /* 151 */ "VNext" OpHelp(""),
23709 /* 152 */ "VRename" OpHelp(""),
23710 /* 153 */ "Pagecount" OpHelp(""),
23711 /* 154 */ "MaxPgcnt" OpHelp(""),
23712 /* 155 */ "Init" OpHelp("Start at P2"),
23713 /* 156 */ "Noop" OpHelp(""),
23714 /* 157 */ "Explain" OpHelp(""),
 
23715 };
23716 return azName[i];
23717 }
23718 #endif
23719
@@ -23812,15 +24155,14 @@
23812 #include <unistd.h>
23813 /* #include <time.h> */
23814 #include <sys/time.h>
23815 #include <errno.h>
23816 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23817 #include <sys/mman.h>
23818 #endif
23819
23820
23821 #if SQLITE_ENABLE_LOCKING_STYLE
23822 # include <sys/ioctl.h>
23823 # if OS_VXWORKS
23824 # include <semaphore.h>
23825 # include <limits.h>
23826 # else
@@ -24244,11 +24586,15 @@
24244 ** On some systems, calls to fchown() will trigger a message in a security
24245 ** log if they come from non-root processes. So avoid calling fchown() if
24246 ** we are not running as root.
24247 */
24248 static int posixFchown(int fd, uid_t uid, gid_t gid){
 
 
 
24249 return geteuid() ? 0 : fchown(fd,uid,gid);
 
24250 }
24251
24252 /* Forward reference */
24253 static int openDirectory(const char*, int*);
24254 static int unixGetpagesize(void);
@@ -24300,11 +24646,11 @@
24300 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
24301
24302 { "read", (sqlite3_syscall_ptr)read, 0 },
24303 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24304
24305 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24306 { "pread", (sqlite3_syscall_ptr)pread, 0 },
24307 #else
24308 { "pread", (sqlite3_syscall_ptr)0, 0 },
24309 #endif
24310 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
@@ -24317,11 +24663,11 @@
24317 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24318
24319 { "write", (sqlite3_syscall_ptr)write, 0 },
24320 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24321
24322 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24323 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
24324 #else
24325 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
24326 #endif
24327 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
@@ -24371,14 +24717,14 @@
24371 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
24372 #else
24373 { "mremap", (sqlite3_syscall_ptr)0, 0 },
24374 #endif
24375 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
24376 #endif
24377
24378 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
24379 #define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
 
 
24380
24381 }; /* End of the overrideable system calls */
24382
24383 /*
24384 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -24687,20 +25033,10 @@
24687 }
24688 /* else fall through */
24689 case EPERM:
24690 return SQLITE_PERM;
24691
24692 /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24693 ** this module never makes such a call. And the code in SQLite itself
24694 ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24695 ** this case is also commented out. If the system does set errno to EDEADLK,
24696 ** the default SQLITE_IOERR_XXX code will be returned. */
24697 #if 0
24698 case EDEADLK:
24699 return SQLITE_IOERR_BLOCKED;
24700 #endif
24701
24702 #if EOPNOTSUPP!=ENOTSUP
24703 case EOPNOTSUPP:
24704 /* something went terribly awry, unless during file system support
24705 * introspection, in which it actually means what it says */
24706 #endif
@@ -25229,13 +25565,17 @@
25229
25230 /*
25231 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
25232 */
25233 static int fileHasMoved(unixFile *pFile){
 
 
 
25234 struct stat buf;
25235 return pFile->pInode!=0 &&
25236 (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
 
25237 }
25238
25239
25240 /*
25241 ** Check a unixFile that is a database. Verify the following:
@@ -25844,10 +26184,17 @@
25844 osUnlink(pFile->pId->zCanonicalName);
25845 }
25846 vxworksReleaseFileId(pFile->pId);
25847 pFile->pId = 0;
25848 }
 
 
 
 
 
 
 
25849 #endif
25850 OSTRACE(("CLOSE %-3d\n", pFile->h));
25851 OpenCounter(-1);
25852 sqlite3_free(pFile->pUnused);
25853 memset(pFile, 0, sizeof(unixFile));
@@ -26367,11 +26714,10 @@
26367 }
26368
26369 /* Otherwise see if some other process holds it. */
26370 if( !reserved ){
26371 sem_t *pSem = pFile->pInode->pSem;
26372 struct stat statBuf;
26373
26374 if( sem_trywait(pSem)==-1 ){
26375 int tErrno = errno;
26376 if( EAGAIN != tErrno ){
26377 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
@@ -26420,11 +26766,10 @@
26420 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
26421 ** routine to lower a locking level.
26422 */
26423 static int semLock(sqlite3_file *id, int eFileLock) {
26424 unixFile *pFile = (unixFile*)id;
26425 int fd;
26426 sem_t *pSem = pFile->pInode->pSem;
26427 int rc = SQLITE_OK;
26428
26429 /* if we already have a lock, it is exclusive.
26430 ** Just adjust level and punt on outta here. */
@@ -27883,12 +28228,29 @@
27883 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
27884 }
27885 return rc;
27886 }
27887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27888 #ifndef SQLITE_OMIT_WAL
27889
27890
27891 /*
27892 ** Object used to represent an shared memory buffer.
27893 **
27894 ** When multiple threads all reference the same wal-index, each thread
@@ -28035,24 +28397,10 @@
28035 #endif
28036
28037 return rc;
28038 }
28039
28040 /*
28041 ** Return the system page size.
28042 **
28043 ** This function should not be called directly by other code in this file.
28044 ** Instead, it should be called via macro osGetpagesize().
28045 */
28046 static int unixGetpagesize(void){
28047 #if defined(_BSD_SOURCE)
28048 return getpagesize();
28049 #else
28050 return (int)sysconf(_SC_PAGESIZE);
28051 #endif
28052 }
28053
28054 /*
28055 ** Return the minimum number of 32KB shm regions that should be mapped at
28056 ** a time, assuming that each mapping must be an integer multiple of the
28057 ** current system page-size.
28058 **
@@ -29698,10 +30046,16 @@
29698 }
29699
29700 if( isDelete ){
29701 #if OS_VXWORKS
29702 zPath = zName;
 
 
 
 
 
 
29703 #else
29704 osUnlink(zName);
29705 #endif
29706 }
29707 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -29798,11 +30152,15 @@
29798 ){
29799 int rc = SQLITE_OK;
29800 UNUSED_PARAMETER(NotUsed);
29801 SimulateIOError(return SQLITE_IOERR_DELETE);
29802 if( osUnlink(zPath)==(-1) ){
29803 if( errno==ENOENT ){
 
 
 
 
29804 rc = SQLITE_IOERR_DELETE_NOENT;
29805 }else{
29806 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29807 }
29808 return rc;
@@ -31695,22 +32053,18 @@
31695 #ifndef NTDDI_WINBLUE
31696 # define NTDDI_WINBLUE 0x06030000
31697 #endif
31698
31699 /*
31700 ** Check if the GetVersionEx[AW] functions should be considered deprecated
31701 ** and avoid using them in that case. It should be noted here that if the
31702 ** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
31703 ** (whether via this block or via being manually specified), that implies
31704 ** the underlying operating system will always be based on the Windows NT
31705 ** Kernel.
31706 */
31707 #ifndef SQLITE_WIN32_GETVERSIONEX
31708 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
31709 # define SQLITE_WIN32_GETVERSIONEX 0
31710 # else
31711 # define SQLITE_WIN32_GETVERSIONEX 1
31712 # endif
31713 #endif
31714
31715 /*
31716 ** This constant should already be defined (in the "WinDef.h" SDK file).
@@ -31778,11 +32132,11 @@
31778 /*
31779 ** This macro is used when a local variable is set to a value that is
31780 ** [sometimes] not used by the code (e.g. via conditional compilation).
31781 */
31782 #ifndef UNUSED_VARIABLE_VALUE
31783 # define UNUSED_VARIABLE_VALUE(x) (void)(x)
31784 #endif
31785
31786 /*
31787 ** Returns the character that should be used as the directory separator.
31788 */
@@ -31827,11 +32181,11 @@
31827
31828 /*
31829 ** Some Microsoft compilers lack this definition.
31830 */
31831 #ifndef INVALID_FILE_ATTRIBUTES
31832 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31833 #endif
31834
31835 #ifndef FILE_FLAG_MASK
31836 # define FILE_FLAG_MASK (0xFF3C0000)
31837 #endif
@@ -31877,11 +32231,11 @@
31877 #endif
31878 const char *zPath; /* Full pathname of this file */
31879 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
31880 #if SQLITE_OS_WINCE
31881 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
31882 HANDLE hMutex; /* Mutex used to control access to shared lock */
31883 HANDLE hShared; /* Shared memory segment used for locking */
31884 winceLock local; /* Locks obtained by this instance of winFile */
31885 winceLock *shared; /* Global shared lock memory for the file */
31886 #endif
31887 #if SQLITE_MAX_MMAP_SIZE>0
@@ -32037,14 +32391,13 @@
32037 **
32038 ** In order to facilitate testing on a WinNT system, the test fixture
32039 ** can manually set this value to 1 to emulate Win98 behavior.
32040 */
32041 #ifdef SQLITE_TEST
32042 SQLITE_API int sqlite3_os_type = 0;
32043 #elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
32044 defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
32045 static int sqlite3_os_type = 0;
32046 #endif
32047
32048 #ifndef SYSCALL
32049 # define SYSCALL sqlite3_syscall_ptr
32050 #endif
@@ -32671,10 +33024,26 @@
32671 #endif
32672
32673 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
32674 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
32675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32676 }; /* End of the overrideable system calls */
32677
32678 /*
32679 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32680 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
@@ -32921,26 +33290,42 @@
32921 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
32922 # define osIsNT() (1)
32923 #elif !defined(SQLITE_WIN32_HAS_WIDE)
32924 # define osIsNT() (0)
32925 #else
32926 static int osIsNT(void){
32927 if( sqlite3_os_type==0 ){
32928 #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
32929 OSVERSIONINFOW sInfo;
32930 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32931 osGetVersionExW(&sInfo);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32932 #else
32933 OSVERSIONINFOA sInfo;
32934 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32935 osGetVersionExA(&sInfo);
32936 #endif
32937 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
32938 }
32939 return sqlite3_os_type==2;
32940 }
32941 #endif
32942
32943 #ifdef SQLITE_WIN32_MALLOC
32944 /*
32945 ** Allocate nBytes of memory.
32946 */
@@ -33144,11 +33529,11 @@
33144 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
33145 }
33146 #endif /* SQLITE_WIN32_MALLOC */
33147
33148 /*
33149 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
33150 **
33151 ** Space to hold the returned string is obtained from malloc.
33152 */
33153 static LPWSTR winUtf8ToUnicode(const char *zFilename){
33154 int nChar;
@@ -33197,11 +33582,11 @@
33197 }
33198
33199 /*
33200 ** Convert an ANSI string to Microsoft Unicode, based on the
33201 ** current codepage settings for file apis.
33202 **
33203 ** Space to hold the returned string is obtained
33204 ** from sqlite3_malloc.
33205 */
33206 static LPWSTR winMbcsToUnicode(const char *zFilename){
33207 int nByte;
@@ -33271,11 +33656,11 @@
33271 sqlite3_free(zTmpWide);
33272 return zFilenameUtf8;
33273 }
33274
33275 /*
33276 ** Convert UTF-8 to multibyte character string. Space to hold the
33277 ** returned string is obtained from sqlite3_malloc().
33278 */
33279 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
33280 char *zFilenameMbcs;
33281 LPWSTR zTmpWide;
@@ -33411,15 +33796,15 @@
33411 ** This function - winLogErrorAtLine() - is only ever called via the macro
33412 ** winLogError().
33413 **
33414 ** This routine is invoked after an error occurs in an OS function.
33415 ** It logs a message using sqlite3_log() containing the current value of
33416 ** error code and, if possible, the human-readable equivalent from
33417 ** FormatMessage.
33418 **
33419 ** The first argument passed to the macro should be the error code that
33420 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33421 ** The two subsequent arguments should be the name of the OS function that
33422 ** failed and the associated file-system path, if any.
33423 */
33424 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
33425 static int winLogErrorAtLine(
@@ -33446,11 +33831,11 @@
33446 return errcode;
33447 }
33448
33449 /*
33450 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
33451 ** will be retried following a locking error - probably caused by
33452 ** antivirus software. Also the initial delay before the first retry.
33453 ** The delay increases linearly with each retry.
33454 */
33455 #ifndef SQLITE_WIN32_IOERR_RETRY
33456 # define SQLITE_WIN32_IOERR_RETRY 10
@@ -33521,11 +33906,11 @@
33521 /*
33522 ** Log a I/O error retry episode.
33523 */
33524 static void winLogIoerr(int nRetry){
33525 if( nRetry ){
33526 sqlite3_log(SQLITE_IOERR,
33527 "delayed %dms for lock/sharing conflict",
33528 winIoerrRetryDelay*nRetry*(nRetry+1)/2
33529 );
33530 }
33531 }
@@ -33615,21 +34000,21 @@
33615 "winceCreateLock1", zFilename);
33616 }
33617
33618 /* Acquire the mutex before continuing */
33619 winceMutexAcquire(pFile->hMutex);
33620
33621 /* Since the names of named mutexes, semaphores, file mappings etc are
33622 ** case-sensitive, take advantage of that by uppercasing the mutex name
33623 ** and using that as the shared filemapping name.
33624 */
33625 osCharUpperW(zName);
33626 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
33627 PAGE_READWRITE, 0, sizeof(winceLock),
33628 zName);
33629
33630 /* Set a flag that indicates we're the first to create the memory so it
33631 ** must be zero-initialized */
33632 lastErrno = osGetLastError();
33633 if (lastErrno == ERROR_ALREADY_EXISTS){
33634 bInit = FALSE;
33635 }
@@ -33636,11 +34021,11 @@
33636
33637 sqlite3_free(zName);
33638
33639 /* If we succeeded in making the shared memory handle, map it. */
33640 if( pFile->hShared ){
33641 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
33642 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
33643 /* If mapping failed, close the shared memory handle and erase it */
33644 if( !pFile->shared ){
33645 pFile->lastErrno = osGetLastError();
33646 winLogError(SQLITE_IOERR, pFile->lastErrno,
@@ -33662,11 +34047,11 @@
33662 winceMutexRelease(pFile->hMutex);
33663 osCloseHandle(pFile->hMutex);
33664 pFile->hMutex = NULL;
33665 return SQLITE_IOERR;
33666 }
33667
33668 /* Initialize the shared memory if we're supposed to */
33669 if( bInit ){
33670 memset(pFile->shared, 0, sizeof(winceLock));
33671 }
33672
@@ -33700,17 +34085,17 @@
33700 /* De-reference and close our copy of the shared memory handle */
33701 osUnmapViewOfFile(pFile->shared);
33702 osCloseHandle(pFile->hShared);
33703
33704 /* Done with the mutex */
33705 winceMutexRelease(pFile->hMutex);
33706 osCloseHandle(pFile->hMutex);
33707 pFile->hMutex = NULL;
33708 }
33709 }
33710
33711 /*
33712 ** An implementation of the LockFile() API of Windows for CE
33713 */
33714 static BOOL winceLockFile(
33715 LPHANDLE phFile,
33716 DWORD dwFileOffsetLow,
@@ -33917,12 +34302,12 @@
33917 #ifndef INVALID_SET_FILE_POINTER
33918 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
33919 #endif
33920
33921 /*
33922 ** Move the current position of the file handle passed as the first
33923 ** argument to offset iOffset within the file. If successful, return 0.
33924 ** Otherwise, set pFile->lastErrno and return non-zero.
33925 */
33926 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
33927 #if !SQLITE_OS_WINRT
33928 LONG upperBits; /* Most sig. 32 bits of new offset */
@@ -33933,15 +34318,15 @@
33933 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
33934
33935 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
33936 lowerBits = (LONG)(iOffset & 0xffffffff);
33937
33938 /* API oddity: If successful, SetFilePointer() returns a dword
33939 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
33940 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
33941 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
33942 ** whether an error has actually occurred, it is also necessary to call
33943 ** GetLastError().
33944 */
33945 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
33946
33947 if( (dwRet==INVALID_SET_FILE_POINTER
@@ -34020,11 +34405,11 @@
34020 winceDestroyLock(pFile);
34021 if( pFile->zDeleteOnClose ){
34022 int cnt = 0;
34023 while(
34024 osDeleteFileW(pFile->zDeleteOnClose)==0
34025 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
34026 && cnt++ < WINCE_DELETION_ATTEMPTS
34027 ){
34028 sqlite3_win32_sleep(100); /* Wait a little before trying again */
34029 }
34030 sqlite3_free(pFile->zDeleteOnClose);
@@ -34868,11 +35253,11 @@
34868 winFile *p = (winFile*)id;
34869 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
34870 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
34871 }
34872
34873 /*
34874 ** Windows will only let you create file view mappings
34875 ** on allocation size granularity boundaries.
34876 ** During sqlite3_os_init() we do a GetSystemInfo()
34877 ** to get the granularity size.
34878 */
@@ -34880,15 +35265,15 @@
34880
34881 #ifndef SQLITE_OMIT_WAL
34882
34883 /*
34884 ** Helper functions to obtain and relinquish the global mutex. The
34885 ** global mutex is used to protect the winLockInfo objects used by
34886 ** this file, all of which may be shared by multiple threads.
34887 **
34888 ** Function winShmMutexHeld() is used to assert() that the global mutex
34889 ** is held when required. This function is only used as part of assert()
34890 ** statements. e.g.
34891 **
34892 ** winShmEnterMutex()
34893 ** assert( winShmMutexHeld() );
34894 ** winShmLeaveMutex()
@@ -34914,14 +35299,14 @@
34914 **
34915 ** winShmMutexHeld() must be true when creating or destroying
34916 ** this object or while reading or writing the following fields:
34917 **
34918 ** nRef
34919 ** pNext
34920 **
34921 ** The following fields are read-only after the object is created:
34922 **
34923 ** fid
34924 ** zFilename
34925 **
34926 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
34927 ** winShmMutexHeld() is true when reading or writing any other field
@@ -35013,11 +35398,11 @@
35013 /* Initialize the locking parameters */
35014 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
35015 if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
35016 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
35017 }
35018
35019 if( rc!= 0 ){
35020 rc = SQLITE_OK;
35021 }else{
35022 pFile->lastErrno = osGetLastError();
35023 rc = SQLITE_BUSY;
@@ -35109,11 +35494,11 @@
35109 sqlite3_free(p);
35110 return SQLITE_IOERR_NOMEM;
35111 }
35112 pNew->zFilename = (char*)&pNew[1];
35113 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
35114 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
35115
35116 /* Look to see if there is an existing winShmNode that can be used.
35117 ** If no matching winShmNode currently exists, create a new one.
35118 */
35119 winShmEnterMutex();
@@ -35146,11 +35531,11 @@
35146 if( SQLITE_OK!=rc ){
35147 goto shm_open_err;
35148 }
35149
35150 /* Check to see if another process is holding the dead-man switch.
35151 ** If not, truncate the file to zero length.
35152 */
35153 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
35154 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
35155 if( rc!=SQLITE_OK ){
35156 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
@@ -35175,11 +35560,11 @@
35175
35176 /* The reference count on pShmNode has already been incremented under
35177 ** the cover of the winShmEnterMutex() mutex and the pointer from the
35178 ** new (struct winShm) object to the pShmNode has been set. All that is
35179 ** left to do is to link the new object into the linked list starting
35180 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
35181 ** mutex.
35182 */
35183 sqlite3_mutex_enter(pShmNode->mutex);
35184 p->pNext = pShmNode->pFirst;
35185 pShmNode->pFirst = p;
@@ -35195,11 +35580,11 @@
35195 winShmLeaveMutex();
35196 return rc;
35197 }
35198
35199 /*
35200 ** Close a connection to shared-memory. Delete the underlying
35201 ** storage if deleteFlag is true.
35202 */
35203 static int winShmUnmap(
35204 sqlite3_file *fd, /* Database holding shared memory */
35205 int deleteFlag /* Delete after closing if true */
@@ -35284,11 +35669,11 @@
35284
35285 /* Undo the local locks */
35286 if( rc==SQLITE_OK ){
35287 p->exclMask &= ~mask;
35288 p->sharedMask &= ~mask;
35289 }
35290 }else if( flags & SQLITE_SHM_SHARED ){
35291 u16 allShared = 0; /* Union of locks held by connections other than "p" */
35292
35293 /* Find out which shared locks are already held by sibling connections.
35294 ** If any sibling already holds an exclusive lock, go ahead and return
@@ -35323,11 +35708,11 @@
35323 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
35324 rc = SQLITE_BUSY;
35325 break;
35326 }
35327 }
35328
35329 /* Get the exclusive locks at the system level. Then if successful
35330 ** also mark the local connection as being locked.
35331 */
35332 if( rc==SQLITE_OK ){
35333 rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
@@ -35343,11 +35728,11 @@
35343 sqlite3ErrName(rc)));
35344 return rc;
35345 }
35346
35347 /*
35348 ** Implement a memory barrier or memory fence on shared memory.
35349 **
35350 ** All loads and stores begun before the barrier must complete before
35351 ** any load or store begun after the barrier.
35352 */
35353 static void winShmBarrier(
@@ -35358,26 +35743,26 @@
35358 winShmEnterMutex();
35359 winShmLeaveMutex();
35360 }
35361
35362 /*
35363 ** This function is called to obtain a pointer to region iRegion of the
35364 ** shared-memory associated with the database file fd. Shared-memory regions
35365 ** are numbered starting from zero. Each shared-memory region is szRegion
35366 ** bytes in size.
35367 **
35368 ** If an error occurs, an error code is returned and *pp is set to NULL.
35369 **
35370 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
35371 ** region has not been allocated (by any client, including one running in a
35372 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
35373 ** isWrite is non-zero and the requested shared-memory region has not yet
35374 ** been allocated, it is allocated by this function.
35375 **
35376 ** If the shared-memory region has already been allocated or is allocated by
35377 ** this call as described above, then it is mapped into this processes
35378 ** address space (if it is not already), *pp is set to point to the mapped
35379 ** memory and SQLITE_OK returned.
35380 */
35381 static int winShmMap(
35382 sqlite3_file *fd, /* Handle open on database file */
35383 int iRegion, /* Region to retrieve */
@@ -35445,21 +35830,21 @@
35445 pShmNode->aRegion = apNew;
35446
35447 while( pShmNode->nRegion<=iRegion ){
35448 HANDLE hMap = NULL; /* file-mapping handle */
35449 void *pMap = 0; /* Mapped memory region */
35450
35451 #if SQLITE_OS_WINRT
35452 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
35453 NULL, PAGE_READWRITE, nByte, NULL
35454 );
35455 #elif defined(SQLITE_WIN32_HAS_WIDE)
35456 hMap = osCreateFileMappingW(pShmNode->hFile.h,
35457 NULL, PAGE_READWRITE, 0, nByte, NULL
35458 );
35459 #elif defined(SQLITE_WIN32_HAS_ANSI)
35460 hMap = osCreateFileMappingA(pShmNode->hFile.h,
35461 NULL, PAGE_READWRITE, 0, nByte, NULL
35462 );
35463 #endif
35464 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
35465 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
@@ -35552,18 +35937,18 @@
35552 return SQLITE_OK;
35553 }
35554
35555 /*
35556 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35557 ** is already mapped, the existing mapping is replaced by the new). Or, if
35558 ** there already exists a mapping for this file, and there are still
35559 ** outstanding xFetch() references to it, this function is a no-op.
35560 **
35561 ** If parameter nByte is non-negative, then it is the requested size of
35562 ** the mapping to create. Otherwise, if nByte is less than zero, then the
35563 ** requested size is the size of the file on disk. The actual size of the
35564 ** created mapping is either the requested size or the value configured
35565 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
35566 **
35567 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
35568 ** recreated as a result of outstanding references) or an SQLite error
35569 ** code otherwise.
@@ -35588,11 +35973,11 @@
35588 }
35589 if( nMap>pFd->mmapSizeMax ){
35590 nMap = pFd->mmapSizeMax;
35591 }
35592 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
35593
35594 if( nMap==0 && pFd->mmapSize>0 ){
35595 winUnmapfile(pFd);
35596 }
35597 if( nMap!=pFd->mmapSize ){
35598 void *pNew = 0;
@@ -35660,11 +36045,11 @@
35660 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
35661 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
35662 ** Finally, if an error does occur, return an SQLite error code. The final
35663 ** value of *pp is undefined in this case.
35664 **
35665 ** If this function does return a pointer, the caller must eventually
35666 ** release the reference by calling winUnfetch().
35667 */
35668 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
35669 #if SQLITE_MAX_MMAP_SIZE>0
35670 winFile *pFd = (winFile*)fd; /* The underlying database file */
@@ -35695,24 +36080,24 @@
35695 osGetCurrentProcessId(), fd, pp, *pp));
35696 return SQLITE_OK;
35697 }
35698
35699 /*
35700 ** If the third argument is non-NULL, then this function releases a
35701 ** reference obtained by an earlier call to winFetch(). The second
35702 ** argument passed to this function must be the same as the corresponding
35703 ** argument that was passed to the winFetch() invocation.
35704 **
35705 ** Or, if the third argument is NULL, then this function is being called
35706 ** to inform the VFS layer that, according to POSIX, any existing mapping
35707 ** may now be invalid and should be unmapped.
35708 */
35709 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
35710 #if SQLITE_MAX_MMAP_SIZE>0
35711 winFile *pFd = (winFile*)fd; /* The underlying database file */
35712
35713 /* If p==0 (unmap the entire file) then there must be no outstanding
35714 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
35715 ** then there must be at least one outstanding. */
35716 assert( (p==0)==(pFd->nFetchOut==0) );
35717
35718 /* If p!=0, it must match the iOff value. */
@@ -35854,11 +36239,11 @@
35854 int nMax, nBuf, nDir, nLen;
35855 char *zBuf;
35856
35857 /* It's odd to simulate an io-error here, but really this is just
35858 ** using the io-error infrastructure to test that SQLite handles this
35859 ** function failing.
35860 */
35861 SimulateIOError( return SQLITE_IOERR );
35862
35863 /* Allocate a temporary buffer to store the fully qualified file
35864 ** name for the temporary file. If this fails, we cannot continue.
@@ -36036,11 +36421,11 @@
36036 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
36037 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
36038 }
36039
36040 /*
36041 ** Check that the output buffer is large enough for the temporary file
36042 ** name in the following format:
36043 **
36044 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
36045 **
36046 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
@@ -36139,42 +36524,42 @@
36139 int isReadonly = (flags & SQLITE_OPEN_READONLY);
36140 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
36141
36142 #ifndef NDEBUG
36143 int isOpenJournal = (isCreate && (
36144 eType==SQLITE_OPEN_MASTER_JOURNAL
36145 || eType==SQLITE_OPEN_MAIN_JOURNAL
36146 || eType==SQLITE_OPEN_WAL
36147 ));
36148 #endif
36149
36150 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
36151 zUtf8Name, id, flags, pOutFlags));
36152
36153 /* Check the following statements are true:
36154 **
36155 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
36156 ** (b) if CREATE is set, then READWRITE must also be set, and
36157 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
36158 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
36159 */
36160 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
36161 assert(isCreate==0 || isReadWrite);
36162 assert(isExclusive==0 || isCreate);
36163 assert(isDelete==0 || isCreate);
36164
36165 /* The main DB, main journal, WAL file and master journal are never
36166 ** automatically deleted. Nor are they ever temporary files. */
36167 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
36168 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
36169 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
36170 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
36171
36172 /* Assert that the upper layer has set one of the "file-type" flags. */
36173 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
36174 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
36175 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
36176 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
36177 );
36178
36179 assert( pFile!=0 );
36180 memset(pFile, 0, sizeof(winFile));
@@ -36185,12 +36570,12 @@
36185 sqlite3_log(SQLITE_ERROR,
36186 "sqlite3_temp_directory variable should be set for WinRT");
36187 }
36188 #endif
36189
36190 /* If the second argument to this function is NULL, generate a
36191 ** temporary file name to use
36192 */
36193 if( !zUtf8Name ){
36194 assert( isDelete && !isOpenJournal );
36195 rc = winGetTempname(pVfs, &zTmpname);
36196 if( rc!=SQLITE_OK ){
@@ -36226,12 +36611,12 @@
36226 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
36227 }else{
36228 dwDesiredAccess = GENERIC_READ;
36229 }
36230
36231 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
36232 ** created. SQLite doesn't use it to indicate "exclusive access"
36233 ** as it is usually understood.
36234 */
36235 if( isExclusive ){
36236 /* Creates a new file, only if it does not already exist. */
36237 /* If the file exists, it fails. */
@@ -36316,11 +36701,11 @@
36316 pFile->lastErrno = lastErrno;
36317 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
36318 sqlite3_free(zConverted);
36319 sqlite3_free(zTmpname);
36320 if( isReadWrite && !isExclusive ){
36321 return winOpen(pVfs, zName, id,
36322 ((flags|SQLITE_OPEN_READONLY) &
36323 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
36324 pOutFlags);
36325 }else{
36326 return SQLITE_CANTOPEN_BKPT;
@@ -36525,18 +36910,18 @@
36525 if( osIsNT() ){
36526 int cnt = 0;
36527 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36528 memset(&sAttrData, 0, sizeof(sAttrData));
36529 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
36530 GetFileExInfoStandard,
36531 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
36532 if( rc ){
36533 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
36534 ** as if it does not exist.
36535 */
36536 if( flags==SQLITE_ACCESS_EXISTS
36537 && sAttrData.nFileSizeHigh==0
36538 && sAttrData.nFileSizeLow==0 ){
36539 attr = INVALID_FILE_ATTRIBUTES;
36540 }else{
36541 attr = sAttrData.dwFileAttributes;
36542 }
@@ -36631,11 +37016,11 @@
36631 sqlite3_vfs *pVfs, /* Pointer to vfs object */
36632 const char *zRelative, /* Possibly relative input path */
36633 int nFull, /* Size of output buffer in bytes */
36634 char *zFull /* Output buffer */
36635 ){
36636
36637 #if defined(__CYGWIN__)
36638 SimulateIOError( return SQLITE_ERROR );
36639 UNUSED_PARAMETER(nFull);
36640 assert( nFull>=pVfs->mxPathname );
36641 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
@@ -36944,24 +37329,24 @@
36944 ** the current time and date as a Julian Day number times 86_400_000. In
36945 ** other words, write into *piNow the number of milliseconds since the Julian
36946 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
36947 ** proleptic Gregorian calendar.
36948 **
36949 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
36950 ** cannot be found.
36951 */
36952 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
36953 /* FILETIME structure is a 64-bit value representing the number of
36954 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
36955 */
36956 FILETIME ft;
36957 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
36958 #ifdef SQLITE_TEST
36959 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36960 #endif
36961 /* 2^32 - to avoid use of LL and warnings in gcc */
36962 static const sqlite3_int64 max32BitValue =
36963 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
36964 (sqlite3_int64)294967296;
36965
36966 #if SQLITE_OS_WINCE
36967 SYSTEMTIME time;
@@ -36973,11 +37358,11 @@
36973 #else
36974 osGetSystemTimeAsFileTime( &ft );
36975 #endif
36976
36977 *piNow = winFiletimeEpoch +
36978 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
36979 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
36980
36981 #ifdef SQLITE_TEST
36982 if( sqlite3_current_time ){
36983 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
@@ -37092,11 +37477,11 @@
37092 };
37093 #endif
37094
37095 /* Double-check that the aSyscall[] array has been constructed
37096 ** correctly. See ticket [bb3a86e890c8e96ab] */
37097 assert( ArraySize(aSyscall)==76 );
37098
37099 /* get memory map allocation granularity */
37100 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
37101 #if SQLITE_OS_WINRT
37102 osGetNativeSystemInfo(&winSysInfo);
@@ -37110,14 +37495,14 @@
37110
37111 #if defined(SQLITE_WIN32_HAS_WIDE)
37112 sqlite3_vfs_register(&winLongPathVfs, 0);
37113 #endif
37114
37115 return SQLITE_OK;
37116 }
37117
37118 SQLITE_API int sqlite3_os_end(void){
37119 #if SQLITE_OS_WINRT
37120 if( sleepObj!=NULL ){
37121 osCloseHandle(sleepObj);
37122 sleepObj = NULL;
37123 }
@@ -49189,20 +49574,20 @@
49189 **
49190 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
49191 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
49192 ** is more of a scheduler yield than an actual delay. But on the 10th
49193 ** an subsequent retries, the delays start becoming longer and longer,
49194 ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
49195 ** The total delay time before giving up is less than 1 second.
49196 */
49197 if( cnt>5 ){
49198 int nDelay = 1; /* Pause time in microseconds */
49199 if( cnt>100 ){
49200 VVA_ONLY( pWal->lockError = 1; )
49201 return SQLITE_PROTOCOL;
49202 }
49203 if( cnt>=10 ) nDelay = (cnt-9)*238; /* Max delay 21ms. Total delay 996ms */
49204 sqlite3OsSleep(pWal->pVfs, nDelay);
49205 }
49206
49207 if( !useWal ){
49208 rc = walIndexReadHdr(pWal, pChanged);
@@ -51314,11 +51699,11 @@
51314 /* If the client is reading or writing an index and the schema is
51315 ** not loaded, then it is too difficult to actually check to see if
51316 ** the correct locks are held. So do not bother - just return true.
51317 ** This case does not come up very often anyhow.
51318 */
51319 if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
51320 return 1;
51321 }
51322
51323 /* Figure out the root-page that the lock should be held on. For table
51324 ** b-trees, this is just the root page of the b-tree being read or
@@ -52784,11 +53169,11 @@
52784 return pBt->nPage;
52785 }
52786 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
52787 assert( sqlite3BtreeHoldsMutex(p) );
52788 assert( ((p->pBt->nPage)&0x8000000)==0 );
52789 return (int)btreePagecount(p->pBt);
52790 }
52791
52792 /*
52793 ** Get a page from the pager and initialize it. This routine is just a
52794 ** convenience wrapper around separate calls to btreeGetPage() and
@@ -56893,11 +57278,12 @@
56893 int nSkip = (iChild ? 4 : 0);
56894
56895 if( *pRC ) return;
56896
56897 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
56898 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
 
56899 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
56900 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
56901 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56902 /* The cell should normally be sized correctly. However, when moving a
56903 ** malformed cell from a leaf page to an interior page, if the cell size
@@ -61582,10 +61968,72 @@
61582 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
61583 for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
61584 sqlite3FuncDefInsert(pHash, &aFunc[i]);
61585 }
61586 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61587
61588 /*
61589 ** This function is used to allocate and populate UnpackedRecord
61590 ** structures intended to be compared against sample index keys stored
61591 ** in the sqlite_stat4 table.
@@ -61622,52 +62070,90 @@
61622 Expr *pExpr, /* The expression to extract a value from */
61623 u8 affinity, /* Affinity to use */
61624 int iVal, /* Array element to populate */
61625 int *pbOk /* OUT: True if value was extracted */
61626 ){
61627 int rc = SQLITE_OK;
61628 sqlite3_value *pVal = 0;
61629 sqlite3 *db = pParse->db;
61630
61631
61632 struct ValueNewStat4Ctx alloc;
 
61633 alloc.pParse = pParse;
61634 alloc.pIdx = pIdx;
61635 alloc.ppRec = ppRec;
61636 alloc.iVal = iVal;
61637
61638 /* Skip over any TK_COLLATE nodes */
61639 pExpr = sqlite3ExprSkipCollate(pExpr);
61640
61641 if( !pExpr ){
61642 pVal = valueNew(db, &alloc);
61643 if( pVal ){
61644 sqlite3VdbeMemSetNull((Mem*)pVal);
61645 }
61646 }else if( pExpr->op==TK_VARIABLE
61647 || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
61648 ){
61649 Vdbe *v;
61650 int iBindVar = pExpr->iColumn;
61651 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
61652 if( (v = pParse->pReprepare)!=0 ){
61653 pVal = valueNew(db, &alloc);
61654 if( pVal ){
61655 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
61656 if( rc==SQLITE_OK ){
61657 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
61658 }
61659 pVal->db = pParse->db;
61660 }
61661 }
61662 }else{
61663 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
61664 }
61665 *pbOk = (pVal!=0);
 
 
61666
61667 assert( pVal==0 || pVal->db==db );
61668 return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61669 }
61670
61671 /*
61672 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
61673 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
@@ -61810,22 +62296,39 @@
61810 pB->zSql = zTmp;
61811 pB->isPrepareV2 = pA->isPrepareV2;
61812 }
61813
61814 /*
61815 ** Resize the Vdbe.aOp array so that it is at least one op larger than
61816 ** it was.
 
61817 **
61818 ** If an out-of-memory error occurs while resizing the array, return
61819 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
61820 ** unchanged (this is so that any opcodes already allocated can be
61821 ** correctly deallocated along with the rest of the Vdbe).
61822 */
61823 static int growOpArray(Vdbe *v){
61824 VdbeOp *pNew;
61825 Parse *p = v->pParse;
 
 
 
 
 
 
 
 
 
 
 
61826 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
 
 
 
 
 
61827 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
61828 if( pNew ){
61829 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
61830 v->aOp = pNew;
61831 }
@@ -61865,11 +62368,11 @@
61865
61866 i = p->nOp;
61867 assert( p->magic==VDBE_MAGIC_INIT );
61868 assert( op>0 && op<0xff );
61869 if( p->pParse->nOpAlloc<=i ){
61870 if( growOpArray(p) ){
61871 return 1;
61872 }
61873 }
61874 p->nOp++;
61875 pOp = &p->aOp[i];
@@ -62225,11 +62728,11 @@
62225 }
62226 sqlite3DbFree(p->db, pParse->aLabel);
62227 pParse->aLabel = 0;
62228 pParse->nLabel = 0;
62229 *pMaxFuncArgs = nMaxArgs;
62230 assert( p->bIsReader!=0 || p->btreeMask==0 );
62231 }
62232
62233 /*
62234 ** Return the address of the next instruction to be inserted.
62235 */
@@ -62252,11 +62755,11 @@
62252 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
62253 VdbeOp *aOp = p->aOp;
62254 assert( aOp && !p->db->mallocFailed );
62255
62256 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
62257 assert( p->btreeMask==0 );
62258
62259 resolveP2Values(p, pnMaxArg);
62260 *pnOp = p->nOp;
62261 p->aOp = 0;
62262 return aOp;
@@ -62267,11 +62770,11 @@
62267 ** address of the first operation added.
62268 */
62269 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
62270 int addr;
62271 assert( p->magic==VDBE_MAGIC_INIT );
62272 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
62273 return 0;
62274 }
62275 addr = p->nOp;
62276 if( ALWAYS(nOp>0) ){
62277 int i;
@@ -62452,11 +62955,11 @@
62452
62453 /*
62454 ** Change the opcode at addr into OP_Noop
62455 */
62456 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
62457 if( p->aOp ){
62458 VdbeOp *pOp = &p->aOp[addr];
62459 sqlite3 *db = p->db;
62460 freeP4(db, pOp->p4type, pOp->p4.p);
62461 memset(pOp, 0, sizeof(pOp[0]));
62462 pOp->opcode = OP_Noop;
@@ -62837,13 +63340,13 @@
62837 ** p->btreeMask of databases that will require a lock.
62838 */
62839 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
62840 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
62841 assert( i<(int)sizeof(p->btreeMask)*8 );
62842 p->btreeMask |= ((yDbMask)1)<<i;
62843 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
62844 p->lockMask |= ((yDbMask)1)<<i;
62845 }
62846 }
62847
62848 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
62849 /*
@@ -62867,20 +63370,19 @@
62867 ** this routine is N*N. But as N is rarely more than 1, this should not
62868 ** be a problem.
62869 */
62870 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
62871 int i;
62872 yDbMask mask;
62873 sqlite3 *db;
62874 Db *aDb;
62875 int nDb;
62876 if( p->lockMask==0 ) return; /* The common case */
62877 db = p->db;
62878 aDb = db->aDb;
62879 nDb = db->nDb;
62880 for(i=0, mask=1; i<nDb; i++, mask += mask){
62881 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62882 sqlite3BtreeEnter(aDb[i].pBt);
62883 }
62884 }
62885 }
62886 #endif
@@ -62889,20 +63391,19 @@
62889 /*
62890 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
62891 */
62892 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
62893 int i;
62894 yDbMask mask;
62895 sqlite3 *db;
62896 Db *aDb;
62897 int nDb;
62898 if( p->lockMask==0 ) return; /* The common case */
62899 db = p->db;
62900 aDb = db->aDb;
62901 nDb = db->nDb;
62902 for(i=0, mask=1; i<nDb; i++, mask += mask){
62903 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62904 sqlite3BtreeLeave(aDb[i].pBt);
62905 }
62906 }
62907 }
62908 #endif
@@ -63869,11 +64370,11 @@
63869 int cnt = 0;
63870 int nWrite = 0;
63871 int nRead = 0;
63872 p = db->pVdbe;
63873 while( p ){
63874 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
63875 cnt++;
63876 if( p->readOnly==0 ) nWrite++;
63877 if( p->bIsReader ) nRead++;
63878 }
63879 p = p->pNext;
@@ -64029,11 +64530,10 @@
64029 /* Lock all btrees used by the statement */
64030 sqlite3VdbeEnter(p);
64031
64032 /* Check for one of the special errors */
64033 mrc = p->rc & 0xff;
64034 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
64035 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
64036 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
64037 if( isSpecialError ){
64038 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
64039 ** no rollback is necessary. Otherwise, at least a savepoint
@@ -64514,11 +65014,11 @@
64514 /*
64515 ** Return the serial-type for the value stored in pMem.
64516 */
64517 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
64518 int flags = pMem->flags;
64519 int n;
64520
64521 if( flags&MEM_Null ){
64522 return 0;
64523 }
64524 if( flags&MEM_Int ){
@@ -64544,15 +65044,15 @@
64544 }
64545 if( flags&MEM_Real ){
64546 return 7;
64547 }
64548 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
64549 n = pMem->n;
 
64550 if( flags & MEM_Zero ){
64551 n += pMem->u.nZero;
64552 }
64553 assert( n>=0 );
64554 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
64555 }
64556
64557 /*
64558 ** Return the length of the data corresponding to the supplied serial-type.
@@ -65315,10 +65815,11 @@
65315 /* rc==0 here means that one or both of the keys ran out of fields and
65316 ** all the fields up to that point were equal. Return the the default_rc
65317 ** value. */
65318 assert( CORRUPT_DB
65319 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
 
65320 );
65321 return pPKey2->default_rc;
65322 }
65323
65324 /*
@@ -65480,10 +65981,11 @@
65480
65481 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65482 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65483 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65484 || CORRUPT_DB
 
65485 );
65486 return res;
65487 }
65488
65489 /*
@@ -67064,11 +67566,11 @@
67064 /*
67065 ** Return true if the prepared statement is in need of being reset.
67066 */
67067 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
67068 Vdbe *v = (Vdbe*)pStmt;
67069 return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
67070 }
67071
67072 /*
67073 ** Return a pointer to the next prepared statement after pStmt associated
67074 ** with database connection pDb. If pStmt is NULL, return the first
@@ -67514,10 +68016,16 @@
67514 **
67515 ** M is an integer, 2 or 3, that indices how many different ways the
67516 ** branch can go. It is usually 2. "I" is the direction the branch
67517 ** goes. 0 means falls through. 1 means branch is taken. 2 means the
67518 ** second alternative branch is taken.
 
 
 
 
 
 
67519 */
67520 #if !defined(SQLITE_VDBE_COVERAGE)
67521 # define VdbeBranchTaken(I,M)
67522 #else
67523 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
@@ -67622,25 +68130,25 @@
67622 ** do so without loss of information. In other words, if the string
67623 ** looks like a number, convert it into a number. If it does not
67624 ** look like a number, leave it alone.
67625 */
67626 static void applyNumericAffinity(Mem *pRec){
67627 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
67628 double rValue;
67629 i64 iValue;
67630 u8 enc = pRec->enc;
67631 if( (pRec->flags&MEM_Str)==0 ) return;
67632 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
67633 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
67634 pRec->u.i = iValue;
67635 pRec->flags |= MEM_Int;
67636 }else{
67637 pRec->r = rValue;
67638 pRec->flags |= MEM_Real;
67639 }
67640 }
67641 }
67642
67643 /*
67644 ** Processing is determine by the affinity parameter:
67645 **
67646 ** SQLITE_AFF_INTEGER:
@@ -67673,11 +68181,11 @@
67673 }
67674 pRec->flags &= ~(MEM_Real|MEM_Int);
67675 }else if( affinity!=SQLITE_AFF_NONE ){
67676 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
67677 || affinity==SQLITE_AFF_NUMERIC );
67678 applyNumericAffinity(pRec);
67679 if( pRec->flags & MEM_Real ){
67680 sqlite3VdbeIntegerAffinity(pRec);
67681 }
67682 }
67683 }
@@ -68254,16 +68762,18 @@
68254 break;
68255 }
68256
68257 /* Opcode: InitCoroutine P1 P2 P3 * *
68258 **
68259 ** Set up register P1 so that it will OP_Yield to the co-routine
68260 ** located at address P3.
68261 **
68262 ** If P2!=0 then the co-routine implementation immediately follows
68263 ** this opcode. So jump over the co-routine implementation to
68264 ** address P2.
 
 
68265 */
68266 case OP_InitCoroutine: { /* jump */
68267 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
68268 assert( pOp->p2>=0 && pOp->p2<p->nOp );
68269 assert( pOp->p3>=0 && pOp->p3<p->nOp );
@@ -68275,13 +68785,15 @@
68275 break;
68276 }
68277
68278 /* Opcode: EndCoroutine P1 * * * *
68279 **
68280 ** The instruction at the address in register P1 is an OP_Yield.
68281 ** Jump to the P2 parameter of that OP_Yield.
68282 ** After the jump, register P1 becomes undefined.
 
 
68283 */
68284 case OP_EndCoroutine: { /* in1 */
68285 VdbeOp *pCaller;
68286 pIn1 = &aMem[pOp->p1];
68287 assert( pIn1->flags==MEM_Int );
@@ -68294,15 +68806,20 @@
68294 break;
68295 }
68296
68297 /* Opcode: Yield P1 P2 * * *
68298 **
68299 ** Swap the program counter with the value in register P1.
 
68300 **
68301 ** If the co-routine ends with OP_Yield or OP_Return then continue
68302 ** to the next instruction. But if the co-routine ends with
68303 ** OP_EndCoroutine, jump immediately to P2.
 
 
 
 
68304 */
68305 case OP_Yield: { /* in1, jump */
68306 int pcDest;
68307 pIn1 = &aMem[pOp->p1];
68308 assert( VdbeMemDynamic(pIn1)==0 );
@@ -68461,11 +68978,11 @@
68461
68462 /* Opcode: String8 * P2 * P4 *
68463 ** Synopsis: r[P2]='P4'
68464 **
68465 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
68466 ** into an OP_String before it is executed for the first time. During
68467 ** this transformation, the length of string P4 is computed and stored
68468 ** as the P1 parameter.
68469 */
68470 case OP_String8: { /* same as TK_STRING, out2-prerelease */
68471 assert( pOp->p4.z!=0 );
@@ -69683,14 +70200,18 @@
69683 break;
69684 }
69685
69686 /* Opcode: Once P1 P2 * * *
69687 **
69688 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
69689 ** set the flag and fall through to the next instruction. In other words,
69690 ** this opcode causes all following opcodes up through P2 (but not including
69691 ** P2) to run just once and to be skipped on subsequent times through the loop.
 
 
 
 
69692 */
69693 case OP_Once: { /* jump */
69694 assert( pOp->p1<p->nOnceFlag );
69695 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
69696 if( p->aOnceFlag[pOp->p1] ){
@@ -69703,17 +70224,17 @@
69703
69704 /* Opcode: If P1 P2 P3 * *
69705 **
69706 ** Jump to P2 if the value in register P1 is true. The value
69707 ** is considered true if it is numeric and non-zero. If the value
69708 ** in P1 is NULL then take the jump if P3 is non-zero.
69709 */
69710 /* Opcode: IfNot P1 P2 P3 * *
69711 **
69712 ** Jump to P2 if the value in register P1 is False. The value
69713 ** is considered false if it has a numeric value of zero. If the value
69714 ** in P1 is NULL then take the jump if P3 is zero.
69715 */
69716 case OP_If: /* jump, in1 */
69717 case OP_IfNot: { /* jump, in1 */
69718 int c;
69719 pIn1 = &aMem[pOp->p1];
@@ -70521,11 +71042,11 @@
70521 int iGen;
70522
70523 assert( p->bIsReader );
70524 assert( p->readOnly==0 || pOp->p2==0 );
70525 assert( pOp->p1>=0 && pOp->p1<db->nDb );
70526 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
70527 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
70528 rc = SQLITE_READONLY;
70529 goto abort_due_to_error;
70530 }
70531 pBt = db->aDb[pOp->p1].pBt;
@@ -70616,11 +71137,11 @@
70616 iDb = pOp->p1;
70617 iCookie = pOp->p3;
70618 assert( pOp->p3<SQLITE_N_BTREE_META );
70619 assert( iDb>=0 && iDb<db->nDb );
70620 assert( db->aDb[iDb].pBt!=0 );
70621 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
70622
70623 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
70624 pOut->u.i = iMeta;
70625 break;
70626 }
@@ -70637,11 +71158,11 @@
70637 */
70638 case OP_SetCookie: { /* in3 */
70639 Db *pDb;
70640 assert( pOp->p2<SQLITE_N_BTREE_META );
70641 assert( pOp->p1>=0 && pOp->p1<db->nDb );
70642 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
70643 assert( p->readOnly==0 );
70644 pDb = &db->aDb[pOp->p1];
70645 assert( pDb->pBt!=0 );
70646 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
70647 pIn3 = &aMem[pOp->p3];
@@ -70692,11 +71213,25 @@
70692 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
70693 ** structure, then said structure defines the content and collating
70694 ** sequence of the index being opened. Otherwise, if P4 is an integer
70695 ** value, it is set to the number of columns in the table.
70696 **
70697 ** See also OpenWrite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70698 */
70699 /* Opcode: OpenWrite P1 P2 P3 P4 P5
70700 ** Synopsis: root=P2 iDb=P3
70701 **
70702 ** Open a read/write cursor named P1 on the table or index whose root
@@ -70714,10 +71249,23 @@
70714 ** in read/write mode. For a given table, there can be one or more read-only
70715 ** cursors or a single read/write cursor but not both.
70716 **
70717 ** See also OpenRead.
70718 */
 
 
 
 
 
 
 
 
 
 
 
 
 
70719 case OP_OpenRead:
70720 case OP_OpenWrite: {
70721 int nField;
70722 KeyInfo *pKeyInfo;
70723 int p2;
@@ -70728,11 +71276,12 @@
70728 Db *pDb;
70729
70730 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
70731 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
70732 assert( p->bIsReader );
70733 assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
 
70734
70735 if( p->expired ){
70736 rc = SQLITE_ABORT;
70737 break;
70738 }
@@ -70740,11 +71289,11 @@
70740 nField = 0;
70741 pKeyInfo = 0;
70742 p2 = pOp->p2;
70743 iDb = pOp->p3;
70744 assert( iDb>=0 && iDb<db->nDb );
70745 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
70746 pDb = &db->aDb[iDb];
70747 pX = pDb->pBt;
70748 assert( pX!=0 );
70749 if( pOp->opcode==OP_OpenWrite ){
70750 wrFlag = 1;
@@ -70785,10 +71334,11 @@
70785 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
70786 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
70787 if( pCur==0 ) goto no_mem;
70788 pCur->nullRow = 1;
70789 pCur->isOrdered = 1;
 
70790 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
70791 pCur->pKeyInfo = pKeyInfo;
70792 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
70793 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
70794
@@ -70939,11 +71489,11 @@
70939 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
70940 p->apCsr[pOp->p1] = 0;
70941 break;
70942 }
70943
70944 /* Opcode: SeekGe P1 P2 P3 P4 *
70945 ** Synopsis: key=r[P3@P4]
70946 **
70947 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70948 ** use the value in register P3 as the key. If cursor P1 refers
70949 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70950,14 +71500,18 @@
70950 ** that are used as an unpacked index key.
70951 **
70952 ** Reposition cursor P1 so that it points to the smallest entry that
70953 ** is greater than or equal to the key value. If there are no records
70954 ** greater than or equal to the key and P2 is not zero, then jump to P2.
 
 
 
 
70955 **
70956 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
70957 */
70958 /* Opcode: SeekGt P1 P2 P3 P4 *
70959 ** Synopsis: key=r[P3@P4]
70960 **
70961 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70962 ** use the value in register P3 as a key. If cursor P1 refers
70963 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70964,14 +71518,18 @@
70964 ** that are used as an unpacked index key.
70965 **
70966 ** Reposition cursor P1 so that it points to the smallest entry that
70967 ** is greater than the key value. If there are no records greater than
70968 ** the key and P2 is not zero, then jump to P2.
 
 
 
 
70969 **
70970 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
70971 */
70972 /* Opcode: SeekLt P1 P2 P3 P4 *
70973 ** Synopsis: key=r[P3@P4]
70974 **
70975 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70976 ** use the value in register P3 as a key. If cursor P1 refers
70977 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70978,14 +71536,18 @@
70978 ** that are used as an unpacked index key.
70979 **
70980 ** Reposition cursor P1 so that it points to the largest entry that
70981 ** is less than the key value. If there are no records less than
70982 ** the key and P2 is not zero, then jump to P2.
 
 
 
 
70983 **
70984 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
70985 */
70986 /* Opcode: SeekLe P1 P2 P3 P4 *
70987 ** Synopsis: key=r[P3@P4]
70988 **
70989 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70990 ** use the value in register P3 as a key. If cursor P1 refers
70991 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70992,10 +71554,14 @@
70992 ** that are used as an unpacked index key.
70993 **
70994 ** Reposition cursor P1 so that it points to the largest entry that
70995 ** is less than or equal to the key value. If there are no records
70996 ** less than or equal to the key and P2 is not zero, then jump to P2.
 
 
 
 
70997 **
70998 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
70999 */
71000 case OP_SeekLT: /* jump, in3 */
71001 case OP_SeekLE: /* jump, in3 */
@@ -71018,16 +71584,19 @@
71018 assert( OP_SeekGT == OP_SeekLT+3 );
71019 assert( pC->isOrdered );
71020 assert( pC->pCursor!=0 );
71021 oc = pOp->opcode;
71022 pC->nullRow = 0;
 
 
 
71023 if( pC->isTable ){
71024 /* The input value in P3 might be of any type: integer, real, string,
71025 ** blob, or NULL. But it needs to be an integer before we can do
71026 ** the seek, so covert it. */
71027 pIn3 = &aMem[pOp->p3];
71028 applyNumericAffinity(pIn3);
71029 iKey = sqlite3VdbeIntValue(pIn3);
71030 pC->rowidIsValid = 0;
71031
71032 /* If the P3 value could not be converted into an integer without
71033 ** loss of information, then special processing is required... */
@@ -71172,10 +71741,14 @@
71172 ** record.
71173 **
71174 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
71175 ** is a prefix of any entry in P1 then a jump is made to P2 and
71176 ** P1 is left pointing at the matching entry.
 
 
 
 
71177 **
71178 ** See also: NotFound, NoConflict, NotExists. SeekGe
71179 */
71180 /* Opcode: NotFound P1 P2 P3 P4 *
71181 ** Synopsis: key=r[P3@P4]
@@ -71187,10 +71760,14 @@
71187 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
71188 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
71189 ** does contain an entry whose prefix matches the P3/P4 record then control
71190 ** falls through to the next instruction and P1 is left pointing at the
71191 ** matching entry.
 
 
 
 
71192 **
71193 ** See also: Found, NotExists, NoConflict
71194 */
71195 /* Opcode: NoConflict P1 P2 P3 P4 *
71196 ** Synopsis: key=r[P3@P4]
@@ -71206,10 +71783,14 @@
71206 ** immediately to P2. If there is a match, fall through and leave the P1
71207 ** cursor pointing to the matching row.
71208 **
71209 ** This opcode is similar to OP_NotFound with the exceptions that the
71210 ** branch is always taken if any part of the search key input is NULL.
 
 
 
 
71211 **
71212 ** See also: NotFound, Found, NotExists
71213 */
71214 case OP_NoConflict: /* jump, in3 */
71215 case OP_NotFound: /* jump, in3 */
@@ -71229,10 +71810,13 @@
71229
71230 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71231 assert( pOp->p4type==P4_INT32 );
71232 pC = p->apCsr[pOp->p1];
71233 assert( pC!=0 );
 
 
 
71234 pIn3 = &aMem[pOp->p3];
71235 assert( pC->pCursor!=0 );
71236 assert( pC->isTable==0 );
71237 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
71238 if( pOp->p4.i>0 ){
@@ -71299,10 +71883,14 @@
71299 ** with rowid P3 then leave the cursor pointing at that record and fall
71300 ** through to the next instruction.
71301 **
71302 ** The OP_NotFound opcode performs the same operation on index btrees
71303 ** (with arbitrary multi-value keys).
 
 
 
 
71304 **
71305 ** See also: Found, NotFound, NoConflict
71306 */
71307 case OP_NotExists: { /* jump, in3 */
71308 VdbeCursor *pC;
@@ -71313,10 +71901,13 @@
71313 pIn3 = &aMem[pOp->p3];
71314 assert( pIn3->flags & MEM_Int );
71315 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71316 pC = p->apCsr[pOp->p1];
71317 assert( pC!=0 );
 
 
 
71318 assert( pC->isTable );
71319 assert( pC->pseudoTableReg==0 );
71320 pCrsr = pC->pCursor;
71321 assert( pCrsr!=0 );
71322 res = 0;
@@ -71615,11 +72206,11 @@
71615 ** Delete the record at which the P1 cursor is currently pointing.
71616 **
71617 ** The cursor will be left pointing at either the next or the previous
71618 ** record in the table. If it is left pointing at the next record, then
71619 ** the next Next instruction will be a no-op. Hence it is OK to delete
71620 ** a record from within an Next loop.
71621 **
71622 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
71623 ** incremented (otherwise not).
71624 **
71625 ** P1 must not be pseudo-table. It has to be a real table with
@@ -71675,16 +72266,16 @@
71675 p->nChange = 0;
71676 break;
71677 }
71678
71679 /* Opcode: SorterCompare P1 P2 P3 P4
71680 ** Synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2
71681 **
71682 ** P1 is a sorter cursor. This instruction compares a prefix of the
71683 ** the record blob in register P3 against a prefix of the entry that
71684 ** the sorter cursor currently points to. The final P4 fields of both
71685 ** the P3 and sorter record are ignored.
71686 **
71687 ** If either P3 or the sorter contains a NULL in one of their significant
71688 ** fields (not counting the P4 fields at the end which are ignored) then
71689 ** the comparison is assumed to be equal.
71690 **
@@ -71692,18 +72283,18 @@
71692 ** each other. Jump to P2 if they are different.
71693 */
71694 case OP_SorterCompare: {
71695 VdbeCursor *pC;
71696 int res;
71697 int nIgnore;
71698
71699 pC = p->apCsr[pOp->p1];
71700 assert( isSorter(pC) );
71701 assert( pOp->p4type==P4_INT32 );
71702 pIn3 = &aMem[pOp->p3];
71703 nIgnore = pOp->p4.i;
71704 rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
71705 VdbeBranchTaken(res!=0,2);
71706 if( res ){
71707 pc = pOp->p2-1;
71708 }
71709 break;
@@ -71879,15 +72470,19 @@
71879 break;
71880 }
71881
71882 /* Opcode: Last P1 P2 * * *
71883 **
71884 ** The next use of the Rowid or Column or Next instruction for P1
71885 ** will refer to the last entry in the database table or index.
71886 ** If the table or index is empty and P2>0, then jump immediately to P2.
71887 ** If P2 is 0 or if the table or index is not empty, fall through
71888 ** to the following instruction.
 
 
 
 
71889 */
71890 case OP_Last: { /* jump */
71891 VdbeCursor *pC;
71892 BtCursor *pCrsr;
71893 int res;
@@ -71901,10 +72496,13 @@
71901 rc = sqlite3BtreeLast(pCrsr, &res);
71902 pC->nullRow = (u8)res;
71903 pC->deferredMoveto = 0;
71904 pC->rowidIsValid = 0;
71905 pC->cacheStatus = CACHE_STALE;
 
 
 
71906 if( pOp->p2>0 ){
71907 VdbeBranchTaken(res!=0,2);
71908 if( res ) pc = pOp->p2 - 1;
71909 }
71910 break;
@@ -71937,10 +72535,14 @@
71937 ** The next use of the Rowid or Column or Next instruction for P1
71938 ** will refer to the first entry in the database table or index.
71939 ** If the table or index is empty and P2>0, then jump immediately to P2.
71940 ** If P2 is 0 or if the table or index is not empty, fall through
71941 ** to the following instruction.
 
 
 
 
71942 */
71943 case OP_Rewind: { /* jump */
71944 VdbeCursor *pC;
71945 BtCursor *pCrsr;
71946 int res;
@@ -71948,10 +72550,13 @@
71948 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71949 pC = p->apCsr[pOp->p1];
71950 assert( pC!=0 );
71951 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
71952 res = 1;
 
 
 
71953 if( isSorter(pC) ){
71954 rc = sqlite3VdbeSorterRewind(db, pC, &res);
71955 }else{
71956 pCrsr = pC->pCursor;
71957 assert( pCrsr );
@@ -71973,10 +72578,14 @@
71973 **
71974 ** Advance cursor P1 so that it points to the next key/data pair in its
71975 ** table or index. If there are no more key/value pairs then fall through
71976 ** to the following instruction. But if the cursor advance was successful,
71977 ** jump immediately to P2.
 
 
 
 
71978 **
71979 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
71980 ** been opened prior to this opcode or the program will segfault.
71981 **
71982 ** The P3 value is a hint to the btree implementation. If P3==1, that
@@ -71992,20 +72601,25 @@
71992 **
71993 ** See also: Prev, NextIfOpen
71994 */
71995 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
71996 **
71997 ** This opcode works just like OP_Next except that if cursor P1 is not
71998 ** open it behaves a no-op.
71999 */
72000 /* Opcode: Prev P1 P2 P3 P4 P5
72001 **
72002 ** Back up cursor P1 so that it points to the previous key/data pair in its
72003 ** table or index. If there is no previous key/value pairs then fall through
72004 ** to the following instruction. But if the cursor backup was successful,
72005 ** jump immediately to P2.
72006 **
 
 
 
 
 
72007 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
72008 ** not open then the behavior is undefined.
72009 **
72010 ** The P3 value is a hint to the btree implementation. If P3==1, that
72011 ** means P1 is an SQL index and that this instruction could have been
@@ -72018,11 +72632,11 @@
72018 ** If P5 is positive and the jump is taken, then event counter
72019 ** number P5-1 in the prepared statement is incremented.
72020 */
72021 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
72022 **
72023 ** This opcode works just like OP_Prev except that if cursor P1 is not
72024 ** open it behaves a no-op.
72025 */
72026 case OP_SorterNext: { /* jump */
72027 VdbeCursor *pC;
72028 int res;
@@ -72049,10 +72663,20 @@
72049 testcase( res==1 );
72050 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
72051 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
72052 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
72053 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
 
 
 
 
 
 
 
 
 
 
72054 rc = pOp->p4.xAdvance(pC->pCursor, &res);
72055 next_tail:
72056 pC->cacheStatus = CACHE_STALE;
72057 VdbeBranchTaken(res==0,2);
72058 if( res==0 ){
@@ -72331,11 +72955,11 @@
72331 rc = SQLITE_LOCKED;
72332 p->errorAction = OE_Abort;
72333 }else{
72334 iDb = pOp->p3;
72335 assert( iCnt==1 );
72336 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
72337 iMoved = 0; /* Not needed. Only to silence a warning. */
72338 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
72339 pOut->flags = MEM_Int;
72340 pOut->u.i = iMoved;
72341 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -72371,11 +72995,11 @@
72371 case OP_Clear: {
72372 int nChange;
72373
72374 nChange = 0;
72375 assert( p->readOnly==0 );
72376 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
72377 rc = sqlite3BtreeClearTable(
72378 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
72379 );
72380 if( pOp->p3 ){
72381 p->nChange += nChange;
@@ -72441,11 +73065,11 @@
72441 int flags;
72442 Db *pDb;
72443
72444 pgno = 0;
72445 assert( pOp->p1>=0 && pOp->p1<db->nDb );
72446 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
72447 assert( p->readOnly==0 );
72448 pDb = &db->aDb[pOp->p1];
72449 assert( pDb->pBt!=0 );
72450 if( pOp->opcode==OP_CreateTable ){
72451 /* flags = BTREE_INTKEY; */
@@ -72529,11 +73153,12 @@
72529
72530 /* Opcode: DropTable P1 * * P4 *
72531 **
72532 ** Remove the internal (in-memory) data structures that describe
72533 ** the table named P4 in database P1. This is called after a table
72534 ** is dropped in order to keep the internal representation of the
 
72535 ** schema consistent with what is on disk.
72536 */
72537 case OP_DropTable: {
72538 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
72539 break;
@@ -72541,11 +73166,12 @@
72541
72542 /* Opcode: DropIndex P1 * * P4 *
72543 **
72544 ** Remove the internal (in-memory) data structures that describe
72545 ** the index named P4 in database P1. This is called after an index
72546 ** is dropped in order to keep the internal representation of the
 
72547 ** schema consistent with what is on disk.
72548 */
72549 case OP_DropIndex: {
72550 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
72551 break;
@@ -72553,11 +73179,12 @@
72553
72554 /* Opcode: DropTrigger P1 * * P4 *
72555 **
72556 ** Remove the internal (in-memory) data structures that describe
72557 ** the trigger named P4 in database P1. This is called after a trigger
72558 ** is dropped in order to keep the internal representation of the
 
72559 ** schema consistent with what is on disk.
72560 */
72561 case OP_DropTrigger: {
72562 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
72563 break;
@@ -72606,11 +73233,11 @@
72606 for(j=0; j<nRoot; j++){
72607 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
72608 }
72609 aRoot[j] = 0;
72610 assert( pOp->p5<db->nDb );
72611 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
72612 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
72613 (int)pnErr->u.i, &nErr);
72614 sqlite3DbFree(db, aRoot);
72615 pnErr->u.i -= nErr;
72616 sqlite3VdbeMemSetNull(pIn1);
@@ -72968,21 +73595,20 @@
72968 pc = pOp->p2 - 1;
72969 }
72970 break;
72971 }
72972
72973 /* Opcode: IfNeg P1 P2 * * *
72974 ** Synopsis: if r[P1]<0 goto P2
72975 **
72976 ** If the value of register P1 is less than zero, jump to P2.
72977 **
72978 ** It is illegal to use this instruction on a register that does
72979 ** not contain an integer. An assertion fault will result if you try.
72980 */
72981 case OP_IfNeg: { /* jump, in1 */
72982 pIn1 = &aMem[pOp->p1];
72983 assert( pIn1->flags&MEM_Int );
 
72984 VdbeBranchTaken(pIn1->u.i<0, 2);
72985 if( pIn1->u.i<0 ){
72986 pc = pOp->p2 - 1;
72987 }
72988 break;
@@ -72991,13 +73617,10 @@
72991 /* Opcode: IfZero P1 P2 P3 * *
72992 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
72993 **
72994 ** The register P1 must contain an integer. Add literal P3 to the
72995 ** value in register P1. If the result is exactly 0, jump to P2.
72996 **
72997 ** It is illegal to use this instruction on a register that does
72998 ** not contain an integer. An assertion fault will result if you try.
72999 */
73000 case OP_IfZero: { /* jump, in1 */
73001 pIn1 = &aMem[pOp->p1];
73002 assert( pIn1->flags&MEM_Int );
73003 pIn1->u.i += pOp->p3;
@@ -73266,11 +73889,11 @@
73266 */
73267 case OP_IncrVacuum: { /* jump */
73268 Btree *pBt;
73269
73270 assert( pOp->p1>=0 && pOp->p1<db->nDb );
73271 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
73272 assert( p->readOnly==0 );
73273 pBt = db->aDb[pOp->p1].pBt;
73274 rc = sqlite3BtreeIncrVacuum(pBt);
73275 VdbeBranchTaken(rc==SQLITE_DONE,2);
73276 if( rc==SQLITE_DONE ){
@@ -73281,16 +73904,17 @@
73281 }
73282 #endif
73283
73284 /* Opcode: Expire P1 * * * *
73285 **
73286 ** Cause precompiled statements to become expired. An expired statement
73287 ** fails with an error code of SQLITE_SCHEMA if it is ever executed
73288 ** (via sqlite3_step()).
 
73289 **
73290 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
73291 ** then only the currently executing statement is affected.
73292 */
73293 case OP_Expire: {
73294 if( !pOp->p1 ){
73295 sqlite3ExpirePreparedStatements(db);
73296 }else{
@@ -73318,11 +73942,11 @@
73318 case OP_TableLock: {
73319 u8 isWriteLock = (u8)pOp->p3;
73320 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
73321 int p1 = pOp->p1;
73322 assert( p1>=0 && p1<db->nDb );
73323 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
73324 assert( isWriteLock==0 || isWriteLock==1 );
73325 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
73326 if( (rc&0xFF)==SQLITE_LOCKED ){
73327 const char *z = pOp->p4.z;
73328 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -73768,11 +74392,11 @@
73768 #ifdef SQLITE_USE_FCNTL_TRACE
73769 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
73770 if( zTrace ){
73771 int i;
73772 for(i=0; i<db->nDb; i++){
73773 if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
73774 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
73775 }
73776 }
73777 #endif /* SQLITE_USE_FCNTL_TRACE */
73778 #ifdef SQLITE_DEBUG
@@ -74758,11 +75382,11 @@
74758 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
74759 ** has been allocated and contains an unpacked record that is used as key2.
74760 */
74761 static void vdbeSorterCompare(
74762 const VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
74763 int nIgnore, /* Ignore the last nIgnore fields */
74764 const void *pKey1, int nKey1, /* Left side of comparison */
74765 const void *pKey2, int nKey2, /* Right side of comparison */
74766 int *pRes /* OUT: Result of comparison */
74767 ){
74768 KeyInfo *pKeyInfo = pCsr->pKeyInfo;
@@ -74772,14 +75396,13 @@
74772
74773 if( pKey2 ){
74774 sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
74775 }
74776
74777 if( nIgnore ){
74778 r2->nField = pKeyInfo->nField - nIgnore;
74779 assert( r2->nField>0 );
74780 for(i=0; i<r2->nField; i++){
74781 if( r2->aMem[i].flags & MEM_Null ){
74782 *pRes = -1;
74783 return;
74784 }
74785 }
@@ -75457,18 +76080,18 @@
75457 ** key.
75458 */
75459 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
75460 const VdbeCursor *pCsr, /* Sorter cursor */
75461 Mem *pVal, /* Value to compare to current sorter key */
75462 int nIgnore, /* Ignore this many fields at the end */
75463 int *pRes /* OUT: Result of comparison */
75464 ){
75465 VdbeSorter *pSorter = pCsr->pSorter;
75466 void *pKey; int nKey; /* Sorter key to compare pVal with */
75467
75468 pKey = vdbeSorterRowkey(pSorter, &nKey);
75469 vdbeSorterCompare(pCsr, nIgnore, pVal->z, pVal->n, pKey, nKey, pRes);
75470 return SQLITE_OK;
75471 }
75472
75473 /************** End of vdbesort.c ********************************************/
75474 /************** Begin file journal.c *****************************************/
@@ -76496,11 +77119,11 @@
76496 }
76497 break;
76498 }
76499 }
76500 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
76501 /* IMP: R-24309-18625 */
76502 /* IMP: R-44911-55124 */
76503 iCol = -1;
76504 }
76505 if( iCol<pTab->nCol ){
76506 cnt++;
@@ -76852,12 +77475,17 @@
76852 }
76853 }else{
76854 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
76855 ** likelihood(X, 0.0625).
76856 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
76857 ** likelihood(X,0.0625). */
76858 pExpr->iTable = 62; /* TUNING: Default 2nd arg to unlikely() is 0.0625 */
 
 
 
 
 
76859 }
76860 }
76861 }
76862 #ifndef SQLITE_OMIT_AUTHORIZATION
76863 if( pDef ){
@@ -77629,11 +78257,11 @@
77629 ** SELECT * FROM t1 WHERE (select a from t1);
77630 */
77631 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
77632 int op;
77633 pExpr = sqlite3ExprSkipCollate(pExpr);
77634 if( pExpr->flags & EP_Generic ) return SQLITE_AFF_NONE;
77635 op = pExpr->op;
77636 if( op==TK_SELECT ){
77637 assert( pExpr->flags&EP_xIsSelect );
77638 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
77639 }
@@ -78964,10 +79592,13 @@
78964 case TK_INTEGER:
78965 case TK_STRING:
78966 case TK_FLOAT:
78967 case TK_BLOB:
78968 return 0;
 
 
 
78969 default:
78970 return 1;
78971 }
78972 }
78973
@@ -79071,83 +79702,124 @@
79071 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
79072 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
79073 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
79074 }
79075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79076 /*
79077 ** This function is used by the implementation of the IN (...) operator.
79078 ** The pX parameter is the expression on the RHS of the IN operator, which
79079 ** might be either a list of expressions or a subquery.
79080 **
79081 ** The job of this routine is to find or create a b-tree object that can
79082 ** be used either to test for membership in the RHS set or to iterate through
79083 ** all members of the RHS set, skipping duplicates.
79084 **
79085 ** A cursor is opened on the b-tree object that the RHS of the IN operator
79086 ** and pX->iTable is set to the index of that cursor.
79087 **
79088 ** The returned value of this function indicates the b-tree type, as follows:
79089 **
79090 ** IN_INDEX_ROWID - The cursor was opened on a database table.
79091 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
79092 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
79093 ** IN_INDEX_EPH - The cursor was opened on a specially created and
79094 ** populated epheremal table.
 
 
79095 **
79096 ** An existing b-tree might be used if the RHS expression pX is a simple
79097 ** subquery such as:
79098 **
79099 ** SELECT <column> FROM <table>
79100 **
79101 ** If the RHS of the IN operator is a list or a more complex subquery, then
79102 ** an ephemeral table might need to be generated from the RHS and then
79103 ** pX->iTable made to point to the ephermeral table instead of an
79104 ** existing table.
79105 **
79106 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
79107 ** through the set members, skipping any duplicates. In this case an
79108 ** epheremal table must be used unless the selected <column> is guaranteed
 
 
 
 
 
 
 
79109 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
79110 ** has a UNIQUE constraint or UNIQUE index.
79111 **
79112 ** If the prNotFound parameter is not 0, then the b-tree will be used
79113 ** for fast set membership tests. In this case an epheremal table must
79114 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
79115 ** be found with <column> as its left-most column.
 
 
 
 
 
 
 
79116 **
79117 ** When the b-tree is being used for membership tests, the calling function
79118 ** needs to know whether or not the structure contains an SQL NULL
79119 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
79120 ** If there is any chance that the (...) might contain a NULL value at
79121 ** runtime, then a register is allocated and the register number written
79122 ** to *prNotFound. If there is no chance that the (...) contains a
79123 ** NULL value, then *prNotFound is left unchanged.
79124 **
79125 ** If a register is allocated and its location stored in *prNotFound, then
79126 ** its initial value is NULL. If the (...) does not remain constant
79127 ** for the duration of the query (i.e. the SELECT within the (...)
79128 ** is a correlated subquery) then the value of the allocated register is
79129 ** reset to NULL each time the subquery is rerun. This allows the
79130 ** caller to use vdbe code equivalent to the following:
79131 **
79132 ** if( register==NULL ){
79133 ** has_null = <test if data structure contains null>
79134 ** register = 1
79135 ** }
79136 **
79137 ** in order to avoid running the <test if data structure contains null>
79138 ** test more often than is necessary.
79139 */
79140 #ifndef SQLITE_OMIT_SUBQUERY
79141 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
79142 Select *p; /* SELECT to the right of IN operator */
79143 int eType = 0; /* Type of RHS table. IN_INDEX_* */
79144 int iTab = pParse->nTab++; /* Cursor of the RHS table */
79145 int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
79146 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
79147
79148 assert( pX->op==TK_IN );
 
79149
79150 /* Check to see if an existing table or index can be used to
79151 ** satisfy the query. This is preferable to generating a new
79152 ** ephemeral table.
79153 */
@@ -79200,44 +79872,59 @@
79200 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
79201
79202 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
79203 if( (pIdx->aiColumn[0]==iCol)
79204 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
79205 && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
79206 ){
79207 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
79208 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
79209 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
79210 VdbeComment((v, "%s", pIdx->zName));
79211 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
79212 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
79213
79214 if( prNotFound && !pTab->aCol[iCol].notNull ){
79215 *prNotFound = ++pParse->nMem;
79216 sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
79217 }
79218 sqlite3VdbeJumpHere(v, iAddr);
79219 }
79220 }
79221 }
79222 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79223
79224 if( eType==0 ){
79225 /* Could not found an existing table or index to use as the RHS b-tree.
79226 ** We will have to generate an ephemeral table to do the job.
79227 */
79228 u32 savedNQueryLoop = pParse->nQueryLoop;
79229 int rMayHaveNull = 0;
79230 eType = IN_INDEX_EPH;
79231 if( prNotFound ){
79232 *prNotFound = rMayHaveNull = ++pParse->nMem;
79233 sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
79234 }else{
79235 pParse->nQueryLoop = 0;
79236 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
79237 eType = IN_INDEX_ROWID;
79238 }
 
 
79239 }
79240 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
79241 pParse->nQueryLoop = savedNQueryLoop;
79242 }else{
79243 pX->iTable = iTab;
@@ -79264,31 +79951,25 @@
79264 ** intkey B-Tree to store the set of IN(...) values instead of the usual
79265 ** (slower) variable length keys B-Tree.
79266 **
79267 ** If rMayHaveNull is non-zero, that means that the operation is an IN
79268 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
79269 ** Furthermore, the IN is in a WHERE clause and that we really want
79270 ** to iterate over the RHS of the IN operator in order to quickly locate
79271 ** all corresponding LHS elements. All this routine does is initialize
79272 ** the register given by rMayHaveNull to NULL. Calling routines will take
79273 ** care of changing this register value to non-NULL if the RHS is NULL-free.
79274 **
79275 ** If rMayHaveNull is zero, that means that the subquery is being used
79276 ** for membership testing only. There is no need to initialize any
79277 ** registers to indicate the presence or absence of NULLs on the RHS.
79278 **
79279 ** For a SELECT or EXISTS operator, return the register that holds the
79280 ** result. For IN operators or if an error occurs, the return value is 0.
79281 */
79282 #ifndef SQLITE_OMIT_SUBQUERY
79283 SQLITE_PRIVATE int sqlite3CodeSubselect(
79284 Parse *pParse, /* Parsing context */
79285 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
79286 int rMayHaveNull, /* Register that records whether NULLs exist in RHS */
79287 int isRowid /* If true, LHS of IN operator is a rowid */
79288 ){
79289 int testAddr = -1; /* One-time test address */
79290 int rReg = 0; /* Register storing resulting */
79291 Vdbe *v = sqlite3GetVdbe(pParse);
79292 if( NEVER(v==0) ) return 0;
79293 sqlite3ExprCachePush(pParse);
79294
@@ -79301,17 +79982,17 @@
79301 **
79302 ** If all of the above are false, then we can run this code just once
79303 ** save the results, and reuse the same result on subsequent invocations.
79304 */
79305 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
79306 testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
79307 }
79308
79309 #ifndef SQLITE_OMIT_EXPLAIN
79310 if( pParse->explain==2 ){
79311 char *zMsg = sqlite3MPrintf(
79312 pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
79313 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
79314 );
79315 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
79316 }
79317 #endif
@@ -79321,14 +80002,10 @@
79321 char affinity; /* Affinity of the LHS of the IN */
79322 int addr; /* Address of OP_OpenEphemeral instruction */
79323 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
79324 KeyInfo *pKeyInfo = 0; /* Key information */
79325
79326 if( rMayHaveNull ){
79327 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
79328 }
79329
79330 affinity = sqlite3ExprAffinity(pLeft);
79331
79332 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
79333 ** expression it is handled the same way. An ephemeral table is
79334 ** filled with single-field index keys representing the results
@@ -79350,24 +80027,27 @@
79350 /* Case 1: expr IN (SELECT ...)
79351 **
79352 ** Generate code to write the results of the select into the temporary
79353 ** table allocated and opened above.
79354 */
 
79355 SelectDest dest;
79356 ExprList *pEList;
79357
79358 assert( !isRowid );
79359 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
79360 dest.affSdst = (u8)affinity;
79361 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
79362 pExpr->x.pSelect->iLimit = 0;
 
 
79363 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
79364 if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
79365 sqlite3KeyInfoUnref(pKeyInfo);
79366 return 0;
79367 }
79368 pEList = pExpr->x.pSelect->pEList;
79369 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
79370 assert( pEList!=0 );
79371 assert( pEList->nExpr>0 );
79372 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
79373 pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
@@ -79394,23 +80074,23 @@
79394 }
79395
79396 /* Loop through each expression in <exprlist>. */
79397 r1 = sqlite3GetTempReg(pParse);
79398 r2 = sqlite3GetTempReg(pParse);
79399 sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
79400 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
79401 Expr *pE2 = pItem->pExpr;
79402 int iValToIns;
79403
79404 /* If the expression is not constant then we will need to
79405 ** disable the test that was generated above that makes sure
79406 ** this code only executes once. Because for a non-constant
79407 ** expression we need to rerun this code each time.
79408 */
79409 if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
79410 sqlite3VdbeChangeToNoop(v, testAddr);
79411 testAddr = -1;
79412 }
79413
79414 /* Evaluate the expression and insert it into the temp table */
79415 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
79416 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
@@ -79476,12 +80156,16 @@
79476 ExprSetVVAProperty(pExpr, EP_NoReduce);
79477 break;
79478 }
79479 }
79480
79481 if( testAddr>=0 ){
79482 sqlite3VdbeJumpHere(v, testAddr);
 
 
 
 
79483 }
79484 sqlite3ExprCachePop(pParse);
79485
79486 return rReg;
79487 }
@@ -79498,11 +80182,11 @@
79498 ** is an array of zero or more values. The expression is true if the LHS is
79499 ** contained within the RHS. The value of the expression is unknown (NULL)
79500 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
79501 ** RHS contains one or more NULL values.
79502 **
79503 ** This routine generates code will jump to destIfFalse if the LHS is not
79504 ** contained within the RHS. If due to NULLs we cannot determine if the LHS
79505 ** is contained in the RHS then jump to destIfNull. If the LHS is contained
79506 ** within the RHS then fall through.
79507 */
79508 static void sqlite3ExprCodeIN(
@@ -79521,11 +80205,13 @@
79521 ** pExpr->iTable will contains the values that make up the RHS.
79522 */
79523 v = pParse->pVdbe;
79524 assert( v!=0 ); /* OOM detected prior to this routine */
79525 VdbeNoopComment((v, "begin IN expr"));
79526 eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
 
 
79527
79528 /* Figure out the affinity to use to create a key from the results
79529 ** of the expression. affinityStr stores a static string suitable for
79530 ** P4 of OP_MakeRecord.
79531 */
@@ -79535,86 +80221,118 @@
79535 */
79536 sqlite3ExprCachePush(pParse);
79537 r1 = sqlite3GetTempReg(pParse);
79538 sqlite3ExprCode(pParse, pExpr->pLeft, r1);
79539
79540 /* If the LHS is NULL, then the result is either false or NULL depending
79541 ** on whether the RHS is empty or not, respectively.
79542 */
79543 if( destIfNull==destIfFalse ){
79544 /* Shortcut for the common case where the false and NULL outcomes are
79545 ** the same. */
79546 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
79547 }else{
79548 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
79549 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
79550 VdbeCoverage(v);
79551 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79552 sqlite3VdbeJumpHere(v, addr1);
79553 }
79554
79555 if( eType==IN_INDEX_ROWID ){
79556 /* In this case, the RHS is the ROWID of table b-tree
79557 */
79558 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
79559 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
79560 VdbeCoverage(v);
79561 }else{
79562 /* In this case, the RHS is an index b-tree.
79563 */
79564 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
79565
79566 /* If the set membership test fails, then the result of the
79567 ** "x IN (...)" expression must be either 0 or NULL. If the set
79568 ** contains no NULL values, then the result is 0. If the set
79569 ** contains one or more NULL values, then the result of the
79570 ** expression is also NULL.
79571 */
79572 if( rRhsHasNull==0 || destIfFalse==destIfNull ){
79573 /* This branch runs if it is known at compile time that the RHS
79574 ** cannot contain NULL values. This happens as the result
79575 ** of a "NOT NULL" constraint in the database schema.
79576 **
79577 ** Also run this branch if NULL is equivalent to FALSE
79578 ** for this particular IN operator.
79579 */
79580 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
79581 VdbeCoverage(v);
79582 }else{
79583 /* In this branch, the RHS of the IN might contain a NULL and
79584 ** the presence of a NULL on the RHS makes a difference in the
79585 ** outcome.
79586 */
79587 int j1, j2;
79588
79589 /* First check to see if the LHS is contained in the RHS. If so,
79590 ** then the presence of NULLs in the RHS does not matter, so jump
79591 ** over all of the code that follows.
79592 */
79593 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
79594 VdbeCoverage(v);
79595
79596 /* Here we begin generating code that runs if the LHS is not
79597 ** contained within the RHS. Generate additional code that
79598 ** tests the RHS for NULLs. If the RHS contains a NULL then
79599 ** jump to destIfNull. If there are no NULLs in the RHS then
79600 ** jump to destIfFalse.
79601 */
79602 sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
79603 sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
79604 j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
79605 VdbeCoverage(v);
79606 sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
79607 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
79608 sqlite3VdbeJumpHere(v, j2);
79609 sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
79610 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79611
79612 /* The OP_Found at the top of this branch jumps here when true,
79613 ** causing the overall IN expression evaluation to fall through.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79614 */
79615 sqlite3VdbeJumpHere(v, j1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79616 }
79617 }
79618 sqlite3ReleaseTempReg(pParse, r1);
79619 sqlite3ExprCachePop(pParse);
79620 VdbeComment((v, "end IN expr"));
@@ -79671,21 +80389,28 @@
79671 }else{
79672 int c;
79673 i64 value;
79674 const char *z = pExpr->u.zToken;
79675 assert( z!=0 );
79676 c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
79677 if( c==0 || (c==2 && negFlag) ){
79678 char *zV;
79679 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
79680 zV = dup8bytes(v, (char*)&value);
79681 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
79682 }else{
79683 #ifdef SQLITE_OMIT_FLOATING_POINT
79684 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
79685 #else
79686 codeReal(v, z, negFlag, iMem);
 
 
 
 
 
 
 
79687 #endif
79688 }
79689 }
79690 }
79691
@@ -80227,11 +80952,11 @@
80227 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80228 testcase( regFree1==0 );
80229 addr = sqlite3VdbeAddOp1(v, op, r1);
80230 VdbeCoverageIf(v, op==TK_ISNULL);
80231 VdbeCoverageIf(v, op==TK_NOTNULL);
80232 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
80233 sqlite3VdbeJumpHere(v, addr);
80234 break;
80235 }
80236 case TK_AGG_FUNCTION: {
80237 AggInfo *pInfo = pExpr->pAggInfo;
@@ -80263,11 +80988,11 @@
80263 nFarg = pFarg ? pFarg->nExpr : 0;
80264 assert( !ExprHasProperty(pExpr, EP_IntValue) );
80265 zId = pExpr->u.zToken;
80266 nId = sqlite3Strlen30(zId);
80267 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
80268 if( pDef==0 ){
80269 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
80270 break;
80271 }
80272
80273 /* Attempt a direct implementation of the built-in COALESCE() and
@@ -82929,10 +83654,11 @@
82929 /* Open the sqlite_stat[134] tables for writing. */
82930 for(i=0; aTable[i].zCols; i++){
82931 assert( i<ArraySize(aTable) );
82932 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
82933 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
 
82934 }
82935 }
82936
82937 /*
82938 ** Recommended number of samples for sqlite_stat4
@@ -82964,11 +83690,12 @@
82964 #endif
82965 };
82966 struct Stat4Accum {
82967 tRowcnt nRow; /* Number of rows in the entire table */
82968 tRowcnt nPSample; /* How often to do a periodic sample */
82969 int nCol; /* Number of columns in index + rowid */
 
82970 int mxSample; /* Maximum number of samples to accumulate */
82971 Stat4Sample current; /* Current row as a Stat4Sample */
82972 u32 iPrn; /* Pseudo-random number used for sampling */
82973 Stat4Sample *aBest; /* Array of nCol best samples */
82974 int iMin; /* Index in a[] of entry with minimum score */
@@ -83050,13 +83777,26 @@
83050 #endif
83051 sqlite3DbFree(p->db, p);
83052 }
83053
83054 /*
83055 ** Implementation of the stat_init(N,C) SQL function. The two parameters
83056 ** are the number of rows in the table or index (C) and the number of columns
83057 ** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
 
 
 
 
 
 
 
 
 
 
 
 
 
83058 **
83059 ** This routine allocates the Stat4Accum object in heap memory. The return
83060 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
83061 ** the size of the blob is sizeof(void*) bytes).
83062 */
@@ -83065,10 +83805,11 @@
83065 int argc,
83066 sqlite3_value **argv
83067 ){
83068 Stat4Accum *p;
83069 int nCol; /* Number of columns in index being sampled */
 
83070 int nColUp; /* nCol rounded up for alignment */
83071 int n; /* Bytes of space to allocate */
83072 sqlite3 *db; /* Database connection */
83073 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83074 int mxSample = SQLITE_STAT4_SAMPLES;
@@ -83075,12 +83816,15 @@
83075 #endif
83076
83077 /* Decode the three function arguments */
83078 UNUSED_PARAMETER(argc);
83079 nCol = sqlite3_value_int(argv[0]);
83080 assert( nCol>1 ); /* >1 because it includes the rowid column */
83081 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
 
 
 
83082
83083 /* Allocate the space required for the Stat4Accum object */
83084 n = sizeof(*p)
83085 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
83086 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
@@ -83098,10 +83842,11 @@
83098 }
83099
83100 p->db = db;
83101 p->nRow = 0;
83102 p->nCol = nCol;
 
83103 p->current.anDLt = (tRowcnt*)&p[1];
83104 p->current.anEq = &p->current.anDLt[nColUp];
83105
83106 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83107 {
@@ -83108,13 +83853,13 @@
83108 u8 *pSpace; /* Allocated space not yet assigned */
83109 int i; /* Used to iterate through p->aSample[] */
83110
83111 p->iGet = -1;
83112 p->mxSample = mxSample;
83113 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
83114 p->current.anLt = &p->current.anEq[nColUp];
83115 p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
83116
83117 /* Set up the Stat4Accum.a[] and aBest[] arrays */
83118 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
83119 p->aBest = &p->a[mxSample];
83120 pSpace = (u8*)(&p->a[mxSample+nCol]);
@@ -83133,11 +83878,11 @@
83133
83134 /* Return a pointer to the allocated object to the caller */
83135 sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
83136 }
83137 static const FuncDef statInitFuncdef = {
83138 1+IsStat34, /* nArg */
83139 SQLITE_UTF8, /* funcFlags */
83140 0, /* pUserData */
83141 0, /* pNext */
83142 statInit, /* xFunc */
83143 0, /* xStep */
@@ -83357,11 +84102,14 @@
83357 ** P Pointer to the Stat4Accum object created by stat_init()
83358 ** C Index of left-most column to differ from previous row
83359 ** R Rowid for the current row. Might be a key record for
83360 ** WITHOUT ROWID tables.
83361 **
83362 ** The SQL function always returns NULL.
 
 
 
83363 **
83364 ** The R parameter is only used for STAT3 and STAT4
83365 */
83366 static void statPush(
83367 sqlite3_context *context,
@@ -83374,11 +84122,11 @@
83374 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
83375 int iChng = sqlite3_value_int(argv[1]);
83376
83377 UNUSED_PARAMETER( argc );
83378 UNUSED_PARAMETER( context );
83379 assert( p->nCol>1 ); /* Includes rowid field */
83380 assert( iChng<p->nCol );
83381
83382 if( p->nRow==0 ){
83383 /* This is the first call to this function. Do initialization. */
83384 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
@@ -83451,11 +84199,14 @@
83451 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
83452 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
83453
83454 /*
83455 ** Implementation of the stat_get(P,J) SQL function. This routine is
83456 ** used to query the results. Content is returned for parameter J
 
 
 
83457 ** which is one of the STAT_GET_xxxx values defined above.
83458 **
83459 ** If neither STAT3 nor STAT4 are enabled, then J is always
83460 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
83461 ** a one-parameter function, stat_get(P), that always returns the
@@ -83502,19 +84253,19 @@
83502 ** I = (K+D-1)/D
83503 */
83504 char *z;
83505 int i;
83506
83507 char *zRet = sqlite3MallocZero(p->nCol * 25);
83508 if( zRet==0 ){
83509 sqlite3_result_error_nomem(context);
83510 return;
83511 }
83512
83513 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
83514 z = zRet + sqlite3Strlen30(zRet);
83515 for(i=0; i<(p->nCol-1); i++){
83516 u64 nDistinct = p->current.anDLt[i] + 1;
83517 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
83518 sqlite3_snprintf(24, z, " %llu", iVal);
83519 z += sqlite3Strlen30(z);
83520 assert( p->current.anEq[i] );
@@ -83670,31 +84421,31 @@
83670 pParse->nTab = MAX(pParse->nTab, iTab);
83671 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
83672 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
83673
83674 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
83675 int nCol; /* Number of columns indexed by pIdx */
83676 int *aGotoChng; /* Array of jump instruction addresses */
83677 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
83678 int addrGotoChng0; /* Address of "Goto addr_chng_0" */
83679 int addrNextRow; /* Address of "next_row:" */
83680 const char *zIdxName; /* Name of the index */
 
83681
83682 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
83683 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
83684 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
83685 nCol = pIdx->nKeyCol;
83686 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
83687 if( aGotoChng==0 ) continue;
 
 
 
 
 
83688
83689 /* Populate the register containing the index name. */
83690 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
83691 zIdxName = pTab->zName;
83692 }else{
83693 zIdxName = pIdx->zName;
83694 }
83695 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
 
83696
83697 /*
83698 ** Pseudo-code for loop that calls stat_push():
83699 **
83700 ** Rewind csr
@@ -83715,11 +84466,11 @@
83715 ** regPrev(0) = idx(0)
83716 ** chng_addr_1:
83717 ** regPrev(1) = idx(1)
83718 ** ...
83719 **
83720 ** chng_addr_N:
83721 ** regRowid = idx(rowid)
83722 ** stat_push(P, regChng, regRowid)
83723 ** Next csr
83724 ** if !eof(csr) goto next_row;
83725 **
@@ -83728,32 +84479,36 @@
83728
83729 /* Make sure there are enough memory cells allocated to accommodate
83730 ** the regPrev array and a trailing rowid (the rowid slot is required
83731 ** when building a record to insert into the sample column of
83732 ** the sqlite_stat4 table. */
83733 pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
83734
83735 /* Open a read-only cursor on the index being analyzed. */
83736 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
83737 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
83738 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
83739 VdbeComment((v, "%s", pIdx->zName));
83740
83741 /* Invoke the stat_init() function. The arguments are:
83742 **
83743 ** (1) the number of columns in the index including the rowid,
83744 ** (2) the number of rows in the index,
 
 
83745 **
83746 ** The second argument is only used for STAT3 and STAT4
 
83747 */
83748 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83749 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
83750 #endif
83751 sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
 
83752 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
83753 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
83754 sqlite3VdbeChangeP5(v, 1+IsStat34);
83755
83756 /* Implementation of the following:
83757 **
83758 ** Rewind csr
83759 ** if eof(csr) goto end_of_scan;
@@ -83762,56 +84517,73 @@
83762 **
83763 */
83764 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
83765 VdbeCoverage(v);
83766 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
83767 addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
83768
83769 /*
83770 ** next_row:
83771 ** regChng = 0
83772 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
83773 ** regChng = 1
83774 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
83775 ** ...
83776 ** regChng = N
83777 ** goto chng_addr_N
83778 */
83779 addrNextRow = sqlite3VdbeCurrentAddr(v);
83780 for(i=0; i<nCol; i++){
83781 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
83782 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
83783 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
83784 aGotoChng[i] =
83785 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
83786 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
83787 VdbeCoverage(v);
83788 }
83789 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
83790 aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
83791
83792 /*
83793 ** chng_addr_0:
83794 ** regPrev(0) = idx(0)
83795 ** chng_addr_1:
83796 ** regPrev(1) = idx(1)
83797 ** ...
83798 */
83799 sqlite3VdbeJumpHere(v, addrGotoChng0);
83800 for(i=0; i<nCol; i++){
83801 sqlite3VdbeJumpHere(v, aGotoChng[i]);
83802 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
83803 }
83804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83805 /*
83806 ** chng_addr_N:
83807 ** regRowid = idx(rowid) // STAT34 only
83808 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
83809 ** Next csr
83810 ** if !eof(csr) goto next_row;
83811 */
83812 sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
83813 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83814 assert( regRowid==(regStat4+2) );
83815 if( HasRowid(pTab) ){
83816 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
83817 }else{
@@ -83851,11 +84623,11 @@
83851 int regSampleRowid = regCol + nCol;
83852 int addrNext;
83853 int addrIsNull;
83854 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
83855
83856 pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
83857
83858 addrNext = sqlite3VdbeCurrentAddr(v);
83859 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
83860 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
83861 VdbeCoverage(v);
@@ -83873,11 +84645,11 @@
83873 #else
83874 for(i=0; i<nCol; i++){
83875 i16 iCol = pIdx->aiColumn[i];
83876 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
83877 }
83878 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
83879 #endif
83880 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
83881 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
83882 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
83883 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
@@ -83885,11 +84657,10 @@
83885 }
83886 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83887
83888 /* End of analysis */
83889 sqlite3VdbeJumpHere(v, addrRewind);
83890 sqlite3DbFree(db, aGotoChng);
83891 }
83892
83893
83894 /* Create a single sqlite_stat1 entry containing NULL as the index
83895 ** name and the row count as the content.
@@ -83986,10 +84757,11 @@
83986 int i;
83987 char *z, *zDb;
83988 Table *pTab;
83989 Index *pIdx;
83990 Token *pTableName;
 
83991
83992 /* Read the database schema. If an error occurs, leave an error message
83993 ** and code in pParse and return NULL. */
83994 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
83995 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
@@ -84033,10 +84805,12 @@
84033 }
84034 sqlite3DbFree(db, z);
84035 }
84036 }
84037 }
 
 
84038 }
84039
84040 /*
84041 ** Used to pass information from the analyzer reader through to the
84042 ** callback routine.
@@ -84091,18 +84865,23 @@
84091 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
84092 assert( pIndex!=0 );
84093 #else
84094 if( pIndex )
84095 #endif
84096 {
84097 if( strcmp(z, "unordered")==0 ){
84098 pIndex->bUnordered = 1;
84099 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
84100 int v32 = 0;
84101 sqlite3GetInt32(z+3, &v32);
84102 pIndex->szIdxRow = sqlite3LogEst(v32);
84103 }
 
 
 
 
 
 
 
84104 }
84105 }
84106
84107 /*
84108 ** This callback is invoked once for each index when reading the
@@ -84139,15 +84918,19 @@
84139 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
84140 }
84141 z = argv[2];
84142
84143 if( pIndex ){
 
84144 decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
84145 if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
84146 }else{
84147 Index fakeIdx;
84148 fakeIdx.szIdxRow = pTable->szTabRow;
 
 
 
84149 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
84150 pTable->szTabRow = fakeIdx.szIdxRow;
84151 }
84152
84153 return 0;
@@ -84185,11 +84968,20 @@
84185 static void initAvgEq(Index *pIdx){
84186 if( pIdx ){
84187 IndexSample *aSample = pIdx->aSample;
84188 IndexSample *pFinal = &aSample[pIdx->nSample-1];
84189 int iCol;
84190 for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
 
 
 
 
 
 
 
 
 
84191 int i; /* Used to iterate through samples */
84192 tRowcnt sumEq = 0; /* Sum of the nEq values */
84193 tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
84194 tRowcnt avgEq = 0;
84195 tRowcnt nDLt = pFinal->anDLt[iCol];
@@ -84208,11 +85000,10 @@
84208 if( nDLt>nSum ){
84209 avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
84210 }
84211 if( avgEq==0 ) avgEq = 1;
84212 pIdx->aAvgEq[iCol] = avgEq;
84213 if( pIdx->nSampleCol==1 ) break;
84214 }
84215 }
84216 }
84217
84218 /*
@@ -84267,11 +85058,10 @@
84267 sqlite3DbFree(db, zSql);
84268 if( rc ) return rc;
84269
84270 while( sqlite3_step(pStmt)==SQLITE_ROW ){
84271 int nIdxCol = 1; /* Number of columns in stat4 records */
84272 int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
84273
84274 char *zIndex; /* Index name */
84275 Index *pIdx; /* Pointer to the index object */
84276 int nSample; /* Number of samples */
84277 int nByte; /* Bytes of space required */
@@ -84285,25 +85075,29 @@
84285 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
84286 /* Index.nSample is non-zero at this point if data has already been
84287 ** loaded from the stat4 table. In this case ignore stat3 data. */
84288 if( pIdx==0 || pIdx->nSample ) continue;
84289 if( bStat3==0 ){
84290 nIdxCol = pIdx->nKeyCol+1;
84291 nAvgCol = pIdx->nKeyCol;
 
 
 
 
84292 }
84293 pIdx->nSampleCol = nIdxCol;
84294 nByte = sizeof(IndexSample) * nSample;
84295 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
84296 nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
84297
84298 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
84299 if( pIdx->aSample==0 ){
84300 sqlite3_finalize(pStmt);
84301 return SQLITE_NOMEM;
84302 }
84303 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
84304 pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
84305 for(i=0; i<nSample; i++){
84306 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
84307 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
84308 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
84309 }
@@ -85408,10 +86202,23 @@
85408 }
85409 #else
85410 #define codeTableLocks(x)
85411 #endif
85412
 
 
 
 
 
 
 
 
 
 
 
 
 
85413 /*
85414 ** This routine is called after a single SQL statement has been
85415 ** parsed and a VDBE program to execute that statement has been
85416 ** prepared. This routine puts the finishing touches on the
85417 ** VDBE program and resets the pParse structure for the next
@@ -85444,22 +86251,23 @@
85444 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
85445 ** set for each database that is used. Generate code to start a
85446 ** transaction on each used database and to verify the schema cookie
85447 ** on each used database.
85448 */
85449 if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
85450 yDbMask mask;
 
85451 int iDb, i;
85452 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
85453 sqlite3VdbeJumpHere(v, 0);
85454 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
85455 if( (mask & pParse->cookieMask)==0 ) continue;
85456 sqlite3VdbeUsesBtree(v, iDb);
85457 sqlite3VdbeAddOp4Int(v,
85458 OP_Transaction, /* Opcode */
85459 iDb, /* P1 */
85460 (mask & pParse->writeMask)!=0, /* P2 */
85461 pParse->cookieValue[iDb], /* P3 */
85462 db->aDb[iDb].pSchema->iGeneration /* P4 */
85463 );
85464 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
85465 }
@@ -85511,11 +86319,11 @@
85511 }
85512 pParse->nTab = 0;
85513 pParse->nMem = 0;
85514 pParse->nSet = 0;
85515 pParse->nVar = 0;
85516 pParse->cookieMask = 0;
85517 }
85518
85519 /*
85520 ** Run the parser and code generator recursively in order to generate
85521 ** code for the SQL statement given onto the end of the pParse context
@@ -87411,11 +88219,11 @@
87411 pTable->aCol = pSelTab->aCol;
87412 pSelTab->nCol = 0;
87413 pSelTab->aCol = 0;
87414 sqlite3DeleteTable(db, pSelTab);
87415 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
87416 pTable->pSchema->flags |= DB_UnresetViews;
87417 }else{
87418 pTable->nCol = 0;
87419 nErr++;
87420 }
87421 sqlite3SelectDelete(db, pSel);
@@ -87988,16 +88796,16 @@
87988 (char *)pKey, P4_KEYINFO);
87989 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
87990
87991 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
87992 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
87993 if( pIndex->onError!=OE_None && pKey!=0 ){
87994 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
87995 sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
87996 addr2 = sqlite3VdbeCurrentAddr(v);
87997 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
87998 pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
87999 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
88000 }else{
88001 addr2 = sqlite3VdbeCurrentAddr(v);
88002 }
88003 sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
@@ -88385,13 +89193,13 @@
88385 ** considered distinct and both result in separate indices.
88386 */
88387 Index *pIdx;
88388 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
88389 int k;
88390 assert( pIdx->onError!=OE_None );
88391 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
88392 assert( pIndex->onError!=OE_None );
88393
88394 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
88395 for(k=0; k<pIdx->nKeyCol; k++){
88396 const char *z1;
88397 const char *z2;
@@ -88578,11 +89386,11 @@
88578 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
88579 a[i] = 23; assert( 23==sqlite3LogEst(5) );
88580 }
88581
88582 assert( 0==sqlite3LogEst(1) );
88583 if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
88584 }
88585
88586 /*
88587 ** This routine will drop an existing named index. This routine
88588 ** implements the DROP INDEX statement.
@@ -89138,19 +89946,17 @@
89138 ** later, by sqlite3FinishCoding().
89139 */
89140 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
89141 Parse *pToplevel = sqlite3ParseToplevel(pParse);
89142 sqlite3 *db = pToplevel->db;
89143 yDbMask mask;
89144
89145 assert( iDb>=0 && iDb<db->nDb );
89146 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
89147 assert( iDb<SQLITE_MAX_ATTACHED+2 );
89148 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89149 mask = ((yDbMask)1)<<iDb;
89150 if( (pToplevel->cookieMask & mask)==0 ){
89151 pToplevel->cookieMask |= mask;
89152 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
89153 if( !OMIT_TEMPDB && iDb==1 ){
89154 sqlite3OpenTempDatabase(pToplevel);
89155 }
89156 }
@@ -89185,11 +89991,11 @@
89185 ** necessary to undo a write and the checkpoint should not be set.
89186 */
89187 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
89188 Parse *pToplevel = sqlite3ParseToplevel(pParse);
89189 sqlite3CodeVerifySchema(pParse, iDb);
89190 pToplevel->writeMask |= ((yDbMask)1)<<iDb;
89191 pToplevel->isMultiWrite |= setStatement;
89192 }
89193
89194 /*
89195 ** Indicate that the statement currently under construction might write
@@ -89992,13 +90798,13 @@
89992 sqlite3DeleteTable(0, pTab);
89993 }
89994 sqlite3HashClear(&temp1);
89995 sqlite3HashClear(&pSchema->fkeyHash);
89996 pSchema->pSeqTab = 0;
89997 if( pSchema->flags & DB_SchemaLoaded ){
89998 pSchema->iGeneration++;
89999 pSchema->flags &= ~DB_SchemaLoaded;
90000 }
90001 }
90002
90003 /*
90004 ** Find and return the schema associated with a BTree. Create
@@ -90873,16 +91679,13 @@
90873 ** May you do good and not evil.
90874 ** May you find forgiveness for yourself and forgive others.
90875 ** May you share freely, never taking more than you give.
90876 **
90877 *************************************************************************
90878 ** This file contains the C functions that implement various SQL
90879 ** functions of SQLite.
90880 **
90881 ** There is only one exported symbol in this file - the function
90882 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
90883 ** All other code has file scope.
90884 */
90885 /* #include <stdlib.h> */
90886 /* #include <assert.h> */
90887
90888 /*
@@ -92553,10 +93356,11 @@
92553 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
92554 FUNCTION(hex, 1, 0, 0, hexFunc ),
92555 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
92556 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
92557 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
 
92558 VFUNCTION(random, 0, 0, 0, randomFunc ),
92559 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
92560 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
92561 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
92562 FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
@@ -92839,11 +93643,11 @@
92839 if( !aiCol ) return 1;
92840 *paiCol = aiCol;
92841 }
92842
92843 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
92844 if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){
92845 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
92846 ** of columns. If each indexed column corresponds to a foreign key
92847 ** column of pFKey, then this index is a winner. */
92848
92849 if( zKey==0 ){
@@ -95865,11 +96669,11 @@
95865 ){
95866 return 0; /* Default values must be the same for all columns */
95867 }
95868 }
95869 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95870 if( pDestIdx->onError!=OE_None ){
95871 destHasUniqueIdx = 1;
95872 }
95873 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
95874 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
95875 }
@@ -96095,10 +96899,13 @@
96095 goto exec_out;
96096 }
96097 }
96098 }
96099 if( xCallback(pArg, nCol, azVals, azCols) ){
 
 
 
96100 rc = SQLITE_ABORT;
96101 sqlite3VdbeFinalize((Vdbe *)pStmt);
96102 pStmt = 0;
96103 sqlite3Error(db, SQLITE_ABORT, 0);
96104 goto exec_out;
@@ -97872,11 +98679,11 @@
97872 ** Note that the values returned are one less that the values that
97873 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
97874 ** to support legacy SQL code. The safety level used to be boolean
97875 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
97876 */
97877 static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
97878 /* 123456789 123456789 */
97879 static const char zText[] = "onoffalseyestruefull";
97880 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
97881 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
97882 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
@@ -97894,11 +98701,11 @@
97894 }
97895
97896 /*
97897 ** Interpret the given string as a boolean value.
97898 */
97899 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
97900 return getSafetyLevel(z,1,dflt)!=0;
97901 }
97902
97903 /* The sqlite3GetBoolean() function is used by other modules but the
97904 ** remainder of this file is specific to PRAGMA processing. So omit
@@ -98440,11 +99247,11 @@
98440 */
98441 case PragTyp_JOURNAL_SIZE_LIMIT: {
98442 Pager *pPager = sqlite3BtreePager(pDb->pBt);
98443 i64 iLimit = -2;
98444 if( zRight ){
98445 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
98446 if( iLimit<-1 ) iLimit = -1;
98447 }
98448 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
98449 returnSingleInt(pParse, "journal_size_limit", iLimit);
98450 break;
@@ -98568,11 +99375,11 @@
98568 sqlite3_int64 sz;
98569 #if SQLITE_MAX_MMAP_SIZE>0
98570 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
98571 if( zRight ){
98572 int ii;
98573 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
98574 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
98575 if( pId2->n==0 ) db->szMmap = sz;
98576 for(ii=db->nDb-1; ii>=0; ii--){
98577 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
98578 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -98936,11 +99743,11 @@
98936 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
98937 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
98938 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
98939 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
98940 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98941 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
98942 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
98943 }
98944 }
98945 }
98946 break;
@@ -99186,13 +99993,12 @@
99186 ** messages have been generated, output OK. Otherwise output the
99187 ** error message
99188 */
99189 static const int iLn = VDBE_OFFSET_LINENO(2);
99190 static const VdbeOpList endCode[] = {
99191 { OP_AddImm, 1, 0, 0}, /* 0 */
99192 { OP_IfNeg, 1, 0, 0}, /* 1 */
99193 { OP_String8, 0, 3, 0}, /* 2 */
99194 { OP_ResultRow, 3, 1, 0},
99195 };
99196
99197 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
99198
@@ -99300,32 +100106,80 @@
99300 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
99301 }
99302 pParse->nMem = MAX(pParse->nMem, 8+j);
99303 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
99304 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99305 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
99306 int jmp2, jmp3, jmp4;
 
99307 if( pPk==pIdx ) continue;
99308 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
99309 pPrior, r1);
99310 pPrior = pIdx;
99311 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
99312 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
 
99313 pIdx->nColumn); VdbeCoverage(v);
99314 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
99315 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
99316 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
99317 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
99318 P4_STATIC);
99319 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
99320 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
 
99321 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
99322 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
99323 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
99324 sqlite3VdbeAddOp0(v, OP_Halt);
99325 sqlite3VdbeJumpHere(v, jmp4);
99326 sqlite3VdbeJumpHere(v, jmp2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99327 sqlite3ResolvePartIdxLabel(pParse, jmp3);
99328 }
99329 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
99330 sqlite3VdbeJumpHere(v, loopTop-1);
99331 #ifndef SQLITE_OMIT_BTREECOUNT
@@ -99346,13 +100200,13 @@
99346 }
99347 #endif /* SQLITE_OMIT_BTREECOUNT */
99348 }
99349 }
99350 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
99351 sqlite3VdbeChangeP2(v, addr, -mxErr);
99352 sqlite3VdbeJumpHere(v, addr+1);
99353 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
99354 }
99355 break;
99356 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
99357
99358 #ifndef SQLITE_OMIT_UTF16
@@ -99611,11 +100465,11 @@
99611 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
99612 ** use -1.
99613 */
99614 case PragTyp_SOFT_HEAP_LIMIT: {
99615 sqlite3_int64 N;
99616 if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
99617 sqlite3_soft_heap_limit64(N);
99618 }
99619 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
99620 break;
99621 }
@@ -101134,12 +101988,11 @@
101134 int iOffset, /* Register holding the offset counter */
101135 int iContinue /* Jump here to skip the current record */
101136 ){
101137 if( iOffset>0 ){
101138 int addr;
101139 sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
101140 addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
101141 sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
101142 VdbeComment((v, "skip OFFSET records"));
101143 sqlite3VdbeJumpHere(v, addr);
101144 }
101145 }
@@ -101300,11 +102153,11 @@
101300 VdbeCoverage(v);
101301 }
101302 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
101303 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
101304 }
101305 assert( sqlite3VdbeCurrentAddr(v)==iJump );
101306 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
101307 break;
101308 }
101309
101310 case WHERE_DISTINCT_UNIQUE: {
@@ -107767,11 +108620,12 @@
107767 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
107768 VdbeCoverageNeverTaken(v);
107769 }
107770 labelContinue = labelBreak;
107771 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
107772 VdbeCoverage(v);
 
107773 }else if( pPk ){
107774 labelContinue = sqlite3VdbeMakeLabel(v);
107775 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
107776 addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
107777 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
@@ -109777,10 +110631,11 @@
109777 struct WherePath {
109778 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
109779 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
109780 LogEst nRow; /* Estimated number of rows generated by this path */
109781 LogEst rCost; /* Total cost of this path */
 
109782 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
109783 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
109784 };
109785
109786 /*
@@ -110581,11 +111436,11 @@
110581 pScan->pOrigWC = pWC;
110582 pScan->pWC = pWC;
110583 if( pIdx && iColumn>=0 ){
110584 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
110585 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
110586 if( NEVER(j>=pIdx->nKeyCol) ) return 0;
110587 }
110588 pScan->zCollName = pIdx->azColl[j];
110589 }else{
110590 pScan->idxaff = 0;
110591 pScan->zCollName = 0;
@@ -111507,11 +112362,11 @@
111507 **
111508 ** 3. All of those index columns for which the WHERE clause does not
111509 ** contain a "col=X" term are subject to a NOT NULL constraint.
111510 */
111511 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111512 if( pIdx->onError==OE_None ) continue;
111513 for(i=0; i<pIdx->nKeyCol; i++){
111514 i16 iCol = pIdx->aiColumn[i];
111515 if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
111516 int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
111517 if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
@@ -111531,12 +112386,11 @@
111531
111532 /*
111533 ** Estimate the logarithm of the input value to base 2.
111534 */
111535 static LogEst estLog(LogEst N){
111536 LogEst x = sqlite3LogEst(N);
111537 return x>33 ? x - 33 : 0;
111538 }
111539
111540 /*
111541 ** Two routines for printing the content of an sqlite3_index_info
111542 ** structure. Used for testing and debugging only. If neither
@@ -111997,11 +112851,11 @@
111997 }else{
111998 i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
111999 iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
112000 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
112001 }
112002 aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
112003 if( iLower>=iUpper ){
112004 iGap = 0;
112005 }else{
112006 iGap = iUpper - iLower;
112007 }
@@ -112036,10 +112890,119 @@
112036 }
112037 }
112038 return nRet;
112039 }
112040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112041 /*
112042 ** This function is used to estimate the number of rows that will be visited
112043 ** by scanning an index for a range of values. The range may have an upper
112044 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
112045 ** and lower bounds are represented by pLower and pUpper respectively. For
@@ -112072,13 +113035,13 @@
112072 ** considering the range constraints. If nEq is 0, this is the number of
112073 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
112074 ** to account for the range contraints pLower and pUpper.
112075 **
112076 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
112077 ** used, each range inequality reduces the search space by a factor of 4.
112078 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
112079 ** rows visited by a factor of 16.
112080 */
112081 static int whereRangeScanEst(
112082 Parse *pParse, /* Parsing & code generating context */
112083 WhereLoopBuilder *pBuilder,
112084 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
@@ -112092,99 +113055,104 @@
112092 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112093 Index *p = pLoop->u.btree.pIndex;
112094 int nEq = pLoop->u.btree.nEq;
112095
112096 if( p->nSample>0
112097 && nEq==pBuilder->nRecValid
112098 && nEq<p->nSampleCol
112099 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
112100 ){
112101 UnpackedRecord *pRec = pBuilder->pRec;
112102 tRowcnt a[2];
112103 u8 aff;
112104
112105 /* Variable iLower will be set to the estimate of the number of rows in
112106 ** the index that are less than the lower bound of the range query. The
112107 ** lower bound being the concatenation of $P and $L, where $P is the
112108 ** key-prefix formed by the nEq values matched against the nEq left-most
112109 ** columns of the index, and $L is the value in pLower.
112110 **
112111 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
112112 ** is not a simple variable or literal value), the lower bound of the
112113 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
112114 ** if $L is available, whereKeyStats() is called for both ($P) and
112115 ** ($P:$L) and the larger of the two returned values used.
112116 **
112117 ** Similarly, iUpper is to be set to the estimate of the number of rows
112118 ** less than the upper bound of the range query. Where the upper bound
112119 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
112120 ** of iUpper are requested of whereKeyStats() and the smaller used.
112121 */
112122 tRowcnt iLower;
112123 tRowcnt iUpper;
112124
112125 if( nEq==p->nKeyCol ){
112126 aff = SQLITE_AFF_INTEGER;
112127 }else{
112128 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
112129 }
112130 /* Determine iLower and iUpper using ($P) only. */
112131 if( nEq==0 ){
112132 iLower = 0;
112133 iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
112134 }else{
112135 /* Note: this call could be optimized away - since the same values must
112136 ** have been requested when testing key $P in whereEqualScanEst(). */
112137 whereKeyStats(pParse, p, pRec, 0, a);
112138 iLower = a[0];
112139 iUpper = a[0] + a[1];
112140 }
112141
112142 /* If possible, improve on the iLower estimate using ($P:$L). */
112143 if( pLower ){
112144 int bOk; /* True if value is extracted from pExpr */
112145 Expr *pExpr = pLower->pExpr->pRight;
112146 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
112147 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
112148 if( rc==SQLITE_OK && bOk ){
112149 tRowcnt iNew;
112150 whereKeyStats(pParse, p, pRec, 0, a);
112151 iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
112152 if( iNew>iLower ) iLower = iNew;
112153 nOut--;
112154 }
112155 }
112156
112157 /* If possible, improve on the iUpper estimate using ($P:$U). */
112158 if( pUpper ){
112159 int bOk; /* True if value is extracted from pExpr */
112160 Expr *pExpr = pUpper->pExpr->pRight;
112161 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
112162 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
112163 if( rc==SQLITE_OK && bOk ){
112164 tRowcnt iNew;
112165 whereKeyStats(pParse, p, pRec, 1, a);
112166 iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
112167 if( iNew<iUpper ) iUpper = iNew;
112168 nOut--;
112169 }
112170 }
112171
112172 pBuilder->pRec = pRec;
112173 if( rc==SQLITE_OK ){
112174 if( iUpper>iLower ){
112175 nNew = sqlite3LogEst(iUpper - iLower);
112176 }else{
112177 nNew = 10; assert( 10==sqlite3LogEst(2) );
112178 }
112179 if( nNew<nOut ){
112180 nOut = nNew;
112181 }
112182 pLoop->nOut = (LogEst)nOut;
112183 WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n",
112184 (u32)iLower, (u32)iUpper, nOut));
112185 return SQLITE_OK;
 
 
 
 
 
 
112186 }
112187 }
112188 #else
112189 UNUSED_PARAMETER(pParse);
112190 UNUSED_PARAMETER(pBuilder);
@@ -112239,11 +113207,11 @@
112239 int rc; /* Subfunction return code */
112240 tRowcnt a[2]; /* Statistics */
112241 int bOk;
112242
112243 assert( nEq>=1 );
112244 assert( nEq<=(p->nKeyCol+1) );
112245 assert( p->aSample!=0 );
112246 assert( p->nSample>0 );
112247 assert( pBuilder->nRecValid<nEq );
112248
112249 /* If values are not available for all fields of the index to the left
@@ -112252,11 +113220,11 @@
112252 return SQLITE_NOTFOUND;
112253 }
112254
112255 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
112256 ** below would return the same value. */
112257 if( nEq>p->nKeyCol ){
112258 *pnRow = 1;
112259 return SQLITE_OK;
112260 }
112261
112262 aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
@@ -112446,11 +113414,11 @@
112446 testcase( bRev );
112447 bRev = !bRev;
112448 }
112449 assert( pX->op==TK_IN );
112450 iReg = iTarget;
112451 eType = sqlite3FindInIndex(pParse, pX, 0);
112452 if( eType==IN_INDEX_INDEX_DESC ){
112453 testcase( bRev );
112454 bRev = !bRev;
112455 }
112456 iTab = pX->iTable;
@@ -112683,11 +113651,11 @@
112683 }
112684 sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
112685 txt.db = db;
112686 sqlite3StrAccumAppend(&txt, " (", 2);
112687 for(i=0; i<nEq; i++){
112688 char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
112689 if( i>=nSkip ){
112690 explainAppendTerm(&txt, i, z, "=");
112691 }else{
112692 if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
112693 sqlite3StrAccumAppend(&txt, "ANY(", 4);
@@ -112696,15 +113664,15 @@
112696 }
112697 }
112698
112699 j = i;
112700 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
112701 char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
112702 explainAppendTerm(&txt, i++, z, ">");
112703 }
112704 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
112705 char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
112706 explainAppendTerm(&txt, i, z, "<");
112707 }
112708 sqlite3StrAccumAppend(&txt, ")", 1);
112709 return sqlite3StrAccumFinish(&txt);
112710 }
@@ -113347,10 +114315,11 @@
113347 int regRowid = 0; /* Register holding rowid */
113348 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
113349 int iRetInit; /* Address of regReturn init */
113350 int untestedTerms = 0; /* Some terms not completely tested */
113351 int ii; /* Loop counter */
 
113352 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
113353 Table *pTab = pTabItem->pTab;
113354
113355 pTerm = pLoop->aLTerm[0];
113356 assert( pTerm!=0 );
@@ -113442,10 +114411,12 @@
113442
113443 /* Run a separate WHERE clause for each term of the OR clause. After
113444 ** eliminating duplicates from other WHERE clauses, the action for each
113445 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
113446 */
 
 
113447 for(ii=0; ii<pOrWc->nTerm; ii++){
113448 WhereTerm *pOrTerm = &pOrWc->a[ii];
113449 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
113450 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113451 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -113454,12 +114425,11 @@
113454 pAndExpr->pLeft = pOrExpr;
113455 pOrExpr = pAndExpr;
113456 }
113457 /* Loop through table entries that match term pOrTerm. */
113458 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
113459 WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
113460 WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
113461 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
113462 if( pSubWInfo ){
113463 WhereLoop *pSubLoop;
113464 explainOneScan(
113465 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
@@ -113546,10 +114516,11 @@
113546 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
113547 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
113548 ){
113549 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
113550 pCov = pSubLoop->u.btree.pIndex;
 
113551 }else{
113552 pCov = 0;
113553 }
113554
113555 /* Finish the loop through table entries that match term pOrTerm. */
@@ -113724,11 +114695,11 @@
113724 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
113725 }
113726 sqlite3DebugPrintf(" %-19s", z);
113727 sqlite3_free(z);
113728 }
113729 sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm);
113730 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
113731 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
113732 /* If the 0x100 bit of wheretracing is set, then show all of the constraint
113733 ** expressions in the WhereLoop.aLTerm[] array.
113734 */
@@ -113960,10 +114931,21 @@
113960
113961 /* whereLoopAddBtree() always generates and inserts the automatic index
113962 ** case first. Hence compatible candidate WhereLoops never have a larger
113963 ** rSetup. Call this SETUP-INVARIANT */
113964 assert( p->rSetup>=pTemplate->rSetup );
 
 
 
 
 
 
 
 
 
 
 
113965
113966 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
113967 ** discarded. WhereLoop p is better if:
113968 ** (1) p has no more dependencies than pTemplate, and
113969 ** (2) p has an equal or lower cost than pTemplate
@@ -114085,17 +115067,17 @@
114085 ** p[] that are also supplated by pTemplate */
114086 WhereLoop **ppTail = &p->pNextLoop;
114087 WhereLoop *pToDel;
114088 while( *ppTail ){
114089 ppTail = whereLoopFindLesser(ppTail, pTemplate);
114090 if( NEVER(ppTail==0) ) break;
114091 pToDel = *ppTail;
114092 if( pToDel==0 ) break;
114093 *ppTail = pToDel->pNextLoop;
114094 #if WHERETRACE_ENABLED /* 0x8 */
114095 if( sqlite3WhereTrace & 0x8 ){
114096 sqlite3DebugPrintf("ins-del: ");
114097 whereLoopPrint(pToDel, pBuilder->pWC);
114098 }
114099 #endif
114100 whereLoopDelete(db, pToDel);
114101 }
@@ -114141,10 +115123,20 @@
114141 pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
114142 }
114143 }
114144 }
114145
 
 
 
 
 
 
 
 
 
 
114146 /*
114147 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
114148 ** index pIndex. Try to match one more.
114149 **
114150 ** When this function is called, pBuilder->pNew->nOut contains the
@@ -114191,16 +115183,13 @@
114191 }else{
114192 opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
114193 }
114194 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
114195
114196 assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
114197 if( pNew->u.btree.nEq < pProbe->nKeyCol ){
114198 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
114199 }else{
114200 iCol = -1;
114201 }
114202 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
114203 opMask, pProbe);
114204 saved_nEq = pNew->u.btree.nEq;
114205 saved_nSkip = pNew->u.btree.nSkip;
114206 saved_nLTerm = pNew->nLTerm;
@@ -114279,11 +115268,11 @@
114279 ** changes "x IN (?)" into "x=?". */
114280
114281 }else if( eOp & (WO_EQ) ){
114282 pNew->wsFlags |= WHERE_COLUMN_EQ;
114283 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
114284 if( iCol>=0 && pProbe->onError==OE_None ){
114285 pNew->wsFlags |= WHERE_UNQ_WANTED;
114286 }else{
114287 pNew->wsFlags |= WHERE_ONEROW;
114288 }
114289 }
@@ -114340,11 +115329,10 @@
114340 testcase( eOp & WO_ISNULL );
114341 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
114342 }else{
114343 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
114344 }
114345 assert( rc!=SQLITE_OK || nOut>0 );
114346 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
114347 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
114348 if( nOut ){
114349 pNew->nOut = sqlite3LogEst(nOut);
114350 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
@@ -114372,10 +115360,11 @@
114372 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
114373 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
114374 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
114375 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
114376 }
 
114377
114378 nOutUnadjusted = pNew->nOut;
114379 pNew->rRun += nInMul + nIn;
114380 pNew->nOut += nInMul + nIn;
114381 whereLoopOutputAdjust(pBuilder->pWC, pNew);
@@ -114386,11 +115375,11 @@
114386 }else{
114387 pNew->nOut = nOutUnadjusted;
114388 }
114389
114390 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
114391 && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
114392 ){
114393 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
114394 }
114395 pNew->nOut = saved_nOut;
114396 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -114491,10 +115480,18 @@
114491 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
114492 **
114493 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the
114494 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
114495 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
 
 
 
 
 
 
 
 
114496 */
114497 static int whereLoopAddBtree(
114498 WhereLoopBuilder *pBuilder, /* WHERE clause information */
114499 Bitmask mExtra /* Extra prerequesites for using this table */
114500 ){
@@ -114533,10 +115530,11 @@
114533 ** fake index the first in a chain of Index objects with all of the real
114534 ** indices to follow */
114535 Index *pFirst; /* First of real indices on the table */
114536 memset(&sPk, 0, sizeof(Index));
114537 sPk.nKeyCol = 1;
 
114538 sPk.aiColumn = &aiColumnPk;
114539 sPk.aiRowLogEst = aiRowEstPk;
114540 sPk.onError = OE_Replace;
114541 sPk.pTable = pTab;
114542 sPk.szIdxRow = pTab->szTabRow;
@@ -114577,10 +115575,11 @@
114577 pNew->aLTerm[0] = pTerm;
114578 /* TUNING: One-time cost for computing the automatic index is
114579 ** approximately 7*N*log2(N) where N is the number of rows in
114580 ** the table being indexed. */
114581 pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) );
 
114582 /* TUNING: Each index lookup yields 20 rows in the table. This
114583 ** is more than the usual guess of 10 rows, since we have no way
114584 ** of knowning how selective the index will ultimately be. It would
114585 ** not be unreasonable to make this value much larger. */
114586 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
@@ -114618,10 +115617,11 @@
114618
114619 /* Full table scan */
114620 pNew->iSortIdx = b ? iSortIdx : 0;
114621 /* TUNING: Cost of full table scan is (N*3.0). */
114622 pNew->rRun = rSize + 16;
 
114623 whereLoopOutputAdjust(pWC, pNew);
114624 rc = whereLoopInsert(pBuilder, pNew);
114625 pNew->nOut = rSize;
114626 if( rc ) break;
114627 }else{
@@ -114653,11 +115653,11 @@
114653 ** also add the cost of visiting table rows (N*3.0). */
114654 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114655 if( m!=0 ){
114656 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
114657 }
114658
114659 whereLoopOutputAdjust(pWC, pNew);
114660 rc = whereLoopInsert(pBuilder, pNew);
114661 pNew->nOut = rSize;
114662 if( rc ) break;
114663 }
@@ -115123,11 +116123,11 @@
115123 }else{
115124 nKeyCol = pIndex->nKeyCol;
115125 nColumn = pIndex->nColumn;
115126 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
115127 assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
115128 isOrderDistinct = pIndex->onError!=OE_None;
115129 }
115130
115131 /* Loop through all columns of the index and deal with the ones
115132 ** that are not constrained by == or IN.
115133 */
@@ -115291,10 +116291,49 @@
115291 if( pLast ) zName[i++] = pLast->cId;
115292 zName[i] = 0;
115293 return zName;
115294 }
115295 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115296
115297 /*
115298 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
115299 ** attempts to find the lowest cost path that visits each WhereLoop
115300 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -115313,142 +116352,170 @@
115313 sqlite3 *db; /* The database connection */
115314 int iLoop; /* Loop counter over the terms of the join */
115315 int ii, jj; /* Loop counters */
115316 int mxI = 0; /* Index of next entry to replace */
115317 int nOrderBy; /* Number of ORDER BY clause terms */
115318 LogEst rCost; /* Cost of a path */
115319 LogEst nOut; /* Number of outputs */
115320 LogEst mxCost = 0; /* Maximum cost of a set of paths */
115321 LogEst mxOut = 0; /* Maximum nOut value on the set of paths */
115322 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
115323 WherePath *aFrom; /* All nFrom paths at the previous level */
115324 WherePath *aTo; /* The nTo best paths at the current level */
115325 WherePath *pFrom; /* An element of aFrom[] that we are working on */
115326 WherePath *pTo; /* An element of aTo[] that we are working on */
115327 WhereLoop *pWLoop; /* One of the WhereLoop objects */
115328 WhereLoop **pX; /* Used to divy up the pSpace memory */
 
115329 char *pSpace; /* Temporary memory used by this routine */
 
115330
115331 pParse = pWInfo->pParse;
115332 db = pParse->db;
115333 nLoop = pWInfo->nLevel;
115334 /* TUNING: For simple queries, only the best path is tracked.
115335 ** For 2-way joins, the 5 best paths are followed.
115336 ** For joins of 3 or more tables, track the 10 best paths */
115337 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
115338 assert( nLoop<=pWInfo->pTabList->nSrc );
115339 WHERETRACE(0x002, ("---- begin solver\n"));
115340
115341 /* Allocate and initialize space for aTo and aFrom */
115342 ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
115343 pSpace = sqlite3DbMallocRaw(db, ii);
 
 
 
 
 
 
 
 
 
 
 
 
115344 if( pSpace==0 ) return SQLITE_NOMEM;
115345 aTo = (WherePath*)pSpace;
115346 aFrom = aTo+mxChoice;
115347 memset(aFrom, 0, sizeof(aFrom[0]));
115348 pX = (WhereLoop**)(aFrom+mxChoice);
115349 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
115350 pFrom->aLoop = pX;
115351 }
 
 
 
 
 
 
 
 
 
 
 
 
115352
115353 /* Seed the search with a single WherePath containing zero WhereLoops.
115354 **
115355 ** TUNING: Do not let the number of iterations go above 25. If the cost
115356 ** of computing an automatic index is not paid back within the first 25
115357 ** rows, then do not use the automatic index. */
115358 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
115359 nFrom = 1;
115360
115361 /* Precompute the cost of sorting the final result set, if the caller
115362 ** to sqlite3WhereBegin() was concerned about sorting */
115363 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
115364 aFrom[0].isOrdered = 0;
115365 nOrderBy = 0;
115366 }else{
115367 aFrom[0].isOrdered = nLoop>0 ? -1 : 1;
115368 nOrderBy = pWInfo->pOrderBy->nExpr;
115369 }
115370
115371 /* Compute successively longer WherePaths using the previous generation
115372 ** of WherePaths as the basis for the next. Keep track of the mxChoice
115373 ** best paths at each generation */
115374 for(iLoop=0; iLoop<nLoop; iLoop++){
115375 nTo = 0;
115376 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
115377 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
115378 Bitmask maskNew;
115379 Bitmask revMask = 0;
115380 i8 isOrdered = pFrom->isOrdered;
 
 
 
 
115381 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
115382 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
115383 /* At this point, pWLoop is a candidate to be the next loop.
115384 ** Compute its cost */
115385 rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
115386 rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
115387 nOut = pFrom->nRow + pWLoop->nOut;
115388 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
115389 if( isOrdered<0 ){
115390 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
115391 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
115392 iLoop, pWLoop, &revMask);
115393 if( isOrdered>=0 && isOrdered<nOrderBy ){
115394 /* TUNING: Estimated cost of a full external sort, where N is
115395 ** the number of rows to sort is:
115396 **
115397 ** cost = (3.0 * N * log(N)).
115398 **
115399 ** Or, if the order-by clause has X terms but only the last Y
115400 ** terms are out of order, then block-sorting will reduce the
115401 ** sorting cost to:
115402 **
115403 ** cost = (3.0 * N * log(N)) * (Y/X)
115404 **
115405 ** The (Y/X) term is implemented using stack variable rScale
115406 ** below. */
115407 LogEst rScale, rSortCost;
115408 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
115409 rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
115410 rSortCost = nRowEst + estLog(nRowEst) + rScale + 16;
115411
115412 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
115413 ** similar but with a larger constant of proportionality.
115414 ** Multiply by an additional factor of 3.0. */
115415 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
115416 rSortCost += 16;
115417 }
115418 WHERETRACE(0x002,
115419 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
115420 rSortCost, (nOrderBy-isOrdered), nOrderBy, rCost,
115421 sqlite3LogEstAdd(rCost,rSortCost)));
115422 rCost = sqlite3LogEstAdd(rCost, rSortCost);
115423 }
115424 }else{
115425 revMask = pFrom->revLoop;
115426 }
115427 /* Check to see if pWLoop should be added to the mxChoice best so far */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115428 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
115429 if( pTo->maskLoop==maskNew
115430 && ((pTo->isOrdered^isOrdered)&80)==0
115431 && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
115432 (pTo->rCost>=rCost && pTo->nRow>=nOut))
115433 ){
115434 testcase( jj==nTo-1 );
115435 break;
115436 }
115437 }
115438 if( jj>=nTo ){
115439 if( nTo>=mxChoice && rCost>=mxCost ){
 
 
 
 
 
 
115440 #ifdef WHERETRACE_ENABLED /* 0x4 */
115441 if( sqlite3WhereTrace&0x4 ){
115442 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",
115443 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
115444 isOrdered>=0 ? isOrdered+'0' : '?');
115445 }
115446 #endif
115447 continue;
115448 }
115449 /* Add a new Path to the aTo[] set */
 
115450 if( nTo<mxChoice ){
115451 /* Increase the size of the aTo set by one */
115452 jj = nTo++;
115453 }else{
115454 /* New path replaces the prior worst to keep count below mxChoice */
@@ -115461,11 +116528,15 @@
115461 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
115462 isOrdered>=0 ? isOrdered+'0' : '?');
115463 }
115464 #endif
115465 }else{
115466 if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
 
 
 
 
115467 #ifdef WHERETRACE_ENABLED /* 0x4 */
115468 if( sqlite3WhereTrace&0x4 ){
115469 sqlite3DebugPrintf(
115470 "Skip %s cost=%-3d,%3d order=%c",
115471 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115473,15 +116544,17 @@
115473 sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n",
115474 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
115475 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
115476 }
115477 #endif
 
115478 testcase( pTo->rCost==rCost );
115479 continue;
115480 }
115481 testcase( pTo->rCost==rCost+1 );
115482 /* A new and better score for a previously created equivalent path */
 
115483 #ifdef WHERETRACE_ENABLED /* 0x4 */
115484 if( sqlite3WhereTrace&0x4 ){
115485 sqlite3DebugPrintf(
115486 "Update %s cost=%-3d,%3d order=%c",
115487 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115495,21 +116568,24 @@
115495 /* pWLoop is a winner. Add it to the set of best so far */
115496 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
115497 pTo->revLoop = revMask;
115498 pTo->nRow = nOut;
115499 pTo->rCost = rCost;
 
115500 pTo->isOrdered = isOrdered;
115501 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
115502 pTo->aLoop[iLoop] = pWLoop;
115503 if( nTo>=mxChoice ){
115504 mxI = 0;
115505 mxCost = aTo[0].rCost;
115506 mxOut = aTo[0].nRow;
115507 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
115508 if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
 
 
115509 mxCost = pTo->rCost;
115510 mxOut = pTo->nRow;
115511 mxI = jj;
115512 }
115513 }
115514 }
115515 }
@@ -115643,11 +116719,11 @@
115643 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
115644 }else{
115645 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115646 assert( pLoop->aLTermSpace==pLoop->aLTerm );
115647 assert( ArraySize(pLoop->aLTermSpace)==4 );
115648 if( pIdx->onError==OE_None
115649 || pIdx->pPartIdxWhere!=0
115650 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
115651 ) continue;
115652 for(j=0; j<pIdx->nKeyCol; j++){
115653 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
@@ -116133,10 +117209,11 @@
116133 }
116134 op = OP_OpenWrite;
116135 pWInfo->aiCurOnePass[1] = iIndexCur;
116136 }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
116137 iIndexCur = iIdxCur;
 
116138 }else{
116139 iIndexCur = pParse->nTab++;
116140 }
116141 pLevel->iIdxCur = iIndexCur;
116142 assert( pIx->pSchema==pTab->pSchema );
@@ -120457,10 +121534,16 @@
120457 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
120458 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
120459 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
120460 testcase( z[0]=='9' );
120461 *tokenType = TK_INTEGER;
 
 
 
 
 
 
120462 for(i=0; sqlite3Isdigit(z[i]); i++){}
120463 #ifndef SQLITE_OMIT_FLOATING_POINT
120464 if( z[i]=='.' ){
120465 i++;
120466 while( sqlite3Isdigit(z[i]) ){ i++; }
@@ -121904,10 +122987,12 @@
121904 /*
121905 ** Close an existing SQLite database
121906 */
121907 static int sqlite3Close(sqlite3 *db, int forceZombie){
121908 if( !db ){
 
 
121909 return SQLITE_OK;
121910 }
121911 if( !sqlite3SafetyCheckSickOrOk(db) ){
121912 return SQLITE_MISUSE_BKPT;
121913 }
@@ -122133,11 +123218,11 @@
122133
122134 /*
122135 ** Return a static string containing the name corresponding to the error code
122136 ** specified in the argument.
122137 */
122138 #if defined(SQLITE_TEST)
122139 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
122140 const char *zName = 0;
122141 int i, origRc = rc;
122142 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
122143 switch( rc ){
@@ -122168,11 +123253,10 @@
122168 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
122169 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
122170 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
122171 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
122172 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
122173 case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break;
122174 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
122175 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
122176 case SQLITE_IOERR_CHECKRESERVEDLOCK:
122177 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
122178 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
@@ -123153,11 +124237,11 @@
123153 SQLITE_MAX_COMPOUND_SELECT,
123154 SQLITE_MAX_VDBE_OP,
123155 SQLITE_MAX_FUNCTION_ARG,
123156 SQLITE_MAX_ATTACHED,
123157 SQLITE_MAX_LIKE_PATTERN_LENGTH,
123158 SQLITE_MAX_VARIABLE_NUMBER,
123159 SQLITE_MAX_TRIGGER_DEPTH,
123160 };
123161
123162 /*
123163 ** Make sure the hard limits are set to reasonable values
@@ -123178,12 +124262,12 @@
123178 # error SQLITE_MAX_VDBE_OP must be at least 40
123179 #endif
123180 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
123181 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
123182 #endif
123183 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
123184 # error SQLITE_MAX_ATTACHED must be between 0 and 62
123185 #endif
123186 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
123187 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
123188 #endif
123189 #if SQLITE_MAX_COLUMN>32767
@@ -124205,14 +125289,14 @@
124205 ** sqlite3_test_control().
124206 */
124207 case SQLITE_TESTCTRL_FAULT_INSTALL: {
124208 /* MSVC is picky about pulling func ptrs from va lists.
124209 ** http://support.microsoft.com/kb/47961
124210 ** sqlite3Config.xTestCallback = va_arg(ap, int(*)(int));
124211 */
124212 typedef int(*TESTCALLBACKFUNC_t)(int);
124213 sqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
124214 rc = sqlite3FaultSim(0);
124215 break;
124216 }
124217
124218 /*
@@ -124438,10 +125522,20 @@
124438 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
124439 #endif
124440 break;
124441 }
124442
 
 
 
 
 
 
 
 
 
 
124443 }
124444 va_end(ap);
124445 #endif /* SQLITE_OMIT_BUILTIN_TEST */
124446 return rc;
124447 }
@@ -124486,11 +125580,11 @@
124486 const char *zParam, /* URI parameter sought */
124487 sqlite3_int64 bDflt /* return if parameter is missing */
124488 ){
124489 const char *z = sqlite3_uri_parameter(zFilename, zParam);
124490 sqlite3_int64 v;
124491 if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
124492 bDflt = v;
124493 }
124494 return bDflt;
124495 }
124496
@@ -126017,11 +127111,11 @@
126017
126018 /* fts3_tokenize_vtab.c */
126019 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
126020
126021 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
126022 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
126023 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
126024 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
126025 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
126026 #endif
126027
@@ -129487,11 +130581,11 @@
129487 ** to by the argument to point to the "simple" tokenizer implementation.
129488 ** And so on.
129489 */
129490 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129491 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129492 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
129493 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
129494 #endif
129495 #ifdef SQLITE_ENABLE_ICU
129496 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
129497 #endif
@@ -129505,20 +130599,20 @@
129505 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
129506 int rc = SQLITE_OK;
129507 Fts3Hash *pHash = 0;
129508 const sqlite3_tokenizer_module *pSimple = 0;
129509 const sqlite3_tokenizer_module *pPorter = 0;
129510 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
129511 const sqlite3_tokenizer_module *pUnicode = 0;
129512 #endif
129513
129514 #ifdef SQLITE_ENABLE_ICU
129515 const sqlite3_tokenizer_module *pIcu = 0;
129516 sqlite3Fts3IcuTokenizerModule(&pIcu);
129517 #endif
129518
129519 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
129520 sqlite3Fts3UnicodeTokenizer(&pUnicode);
129521 #endif
129522
129523 #ifdef SQLITE_TEST
129524 rc = sqlite3Fts3InitTerm(db);
@@ -129542,11 +130636,11 @@
129542 /* Load the built-in tokenizers into the hash table */
129543 if( rc==SQLITE_OK ){
129544 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
129545 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
129546
129547 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
129548 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
129549 #endif
129550 #ifdef SQLITE_ENABLE_ICU
129551 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
129552 #endif
@@ -140777,38 +141871,40 @@
140777 i64 iDocid = sqlite3_column_int64(pStmt, 0);
140778 int iLang = langidFromSelect(p, pStmt);
140779 int iCol;
140780
140781 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
140782 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
140783 int nText = sqlite3_column_bytes(pStmt, iCol+1);
140784 sqlite3_tokenizer_cursor *pT = 0;
140785
140786 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
140787 while( rc==SQLITE_OK ){
140788 char const *zToken; /* Buffer containing token */
140789 int nToken = 0; /* Number of bytes in token */
140790 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
140791 int iPos = 0; /* Position of token in zText */
140792
140793 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
140794 if( rc==SQLITE_OK ){
140795 int i;
140796 cksum2 = cksum2 ^ fts3ChecksumEntry(
140797 zToken, nToken, iLang, 0, iDocid, iCol, iPos
140798 );
140799 for(i=1; i<p->nIndex; i++){
140800 if( p->aIndex[i].nPrefix<=nToken ){
140801 cksum2 = cksum2 ^ fts3ChecksumEntry(
140802 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
140803 );
140804 }
140805 }
140806 }
140807 }
140808 if( pT ) pModule->xClose(pT);
140809 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 
 
140810 }
140811 }
140812
140813 sqlite3_finalize(pStmt);
140814 }
@@ -142800,11 +143896,11 @@
142800 ******************************************************************************
142801 **
142802 ** Implementation of the "unicode" full-text-search tokenizer.
142803 */
142804
142805 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
142806
142807 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142808
142809 /* #include <assert.h> */
142810 /* #include <stdlib.h> */
@@ -143016,11 +144112,11 @@
143016 memset(pNew, 0, sizeof(unicode_tokenizer));
143017 pNew->bRemoveDiacritic = 1;
143018
143019 for(i=0; rc==SQLITE_OK && i<nArg; i++){
143020 const char *z = azArg[i];
143021 int n = strlen(z);
143022
143023 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
143024 pNew->bRemoveDiacritic = 1;
143025 }
143026 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
@@ -143103,11 +144199,11 @@
143103 int *piEnd, /* OUT: Ending offset of token */
143104 int *piPos /* OUT: Position integer of token */
143105 ){
143106 unicode_cursor *pCsr = (unicode_cursor *)pC;
143107 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
143108 int iCode;
143109 char *zOut;
143110 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
143111 const unsigned char *zStart = z;
143112 const unsigned char *zEnd;
143113 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
@@ -143148,15 +144244,15 @@
143148 }while( unicodeIsAlnum(p, iCode)
143149 || sqlite3FtsUnicodeIsdiacritic(iCode)
143150 );
143151
143152 /* Set the output variables and return. */
143153 pCsr->iOff = (z - pCsr->aInput);
143154 *paToken = pCsr->zToken;
143155 *pnToken = zOut - pCsr->zToken;
143156 *piStart = (zStart - pCsr->aInput);
143157 *piEnd = (zEnd - pCsr->aInput);
143158 *piPos = pCsr->iToken++;
143159 return SQLITE_OK;
143160 }
143161
143162 /*
@@ -143175,11 +144271,11 @@
143175 };
143176 *ppModule = &module;
143177 }
143178
143179 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
143180 #endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
143181
143182 /************** End of fts3_unicode.c ****************************************/
143183 /************** Begin file fts3_unicode2.c ***********************************/
143184 /*
143185 ** 2012 May 25
@@ -143196,11 +144292,11 @@
143196
143197 /*
143198 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
143199 */
143200
143201 #if defined(SQLITE_ENABLE_FTS4_UNICODE61)
143202 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
143203
143204 /* #include <assert.h> */
143205
143206 /*
@@ -143220,11 +144316,11 @@
143220 ** the size of the range (always at least 1). In other words, the value
143221 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
143222 ** C. It is not possible to represent a range larger than 1023 codepoints
143223 ** using this format.
143224 */
143225 const static unsigned int aEntry[] = {
143226 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
143227 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
143228 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
143229 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
143230 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
@@ -143312,11 +144408,11 @@
143312
143313 if( c<128 ){
143314 return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
143315 }else if( c<(1<<22) ){
143316 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
143317 int iRes;
143318 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
143319 int iLo = 0;
143320 while( iHi>=iLo ){
143321 int iTest = (iHi + iLo) / 2;
143322 if( key >= aEntry[iTest] ){
@@ -143383,11 +144479,11 @@
143383 iHi = iTest-1;
143384 }
143385 }
143386 assert( key>=aDia[iRes] );
143387 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
143388 };
143389
143390
143391 /*
143392 ** Return true if the argument interpreted as a unicode codepoint
143393 ** is a diacritical modifier character.
@@ -143543,11 +144639,11 @@
143543 }
143544
143545 return ret;
143546 }
143547 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
143548 #endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
143549
143550 /************** End of fts3_unicode2.c ***************************************/
143551 /************** Begin file rtree.c *******************************************/
143552 /*
143553 ** 2001 September 15
@@ -145080,13 +146176,17 @@
145080 int rc = SQLITE_OK;
145081 int iCell = 0;
145082
145083 rtreeReference(pRtree);
145084
 
145085 freeCursorConstraints(pCsr);
 
 
 
 
145086 pCsr->iStrategy = idxNum;
145087
145088 if( idxNum==1 ){
145089 /* Special case - lookup by rowid. */
145090 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
145091 RtreeSearchPoint *p; /* Search point for the the leaf */
145092 i64 iRowid = sqlite3_value_int64(argv[0]);
145093
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.6. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -220,13 +220,13 @@
220 **
221 ** See also: [sqlite3_libversion()],
222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223 ** [sqlite_version()] and [sqlite_source_id()].
224 */
225 #define SQLITE_VERSION "3.8.6"
226 #define SQLITE_VERSION_NUMBER 3008006
227 #define SQLITE_SOURCE_ID "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
228
229 /*
230 ** CAPI3REF: Run-Time Library Version Numbers
231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
232 **
@@ -382,19 +382,19 @@
382 /*
383 ** CAPI3REF: Closing A Database Connection
384 **
385 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
386 ** for the [sqlite3] object.
387 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
388 ** the [sqlite3] object is successfully destroyed and all associated
389 ** resources are deallocated.
390 **
391 ** ^If the database connection is associated with unfinalized prepared
392 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
393 ** will leave the database connection open and return [SQLITE_BUSY].
394 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
395 ** and/or unfinished sqlite3_backups, then the database connection becomes
396 ** an unusable "zombie" which will automatically be deallocated when the
397 ** last prepared statement is finalized or the last sqlite3_backup is
398 ** finished. The sqlite3_close_v2() interface is intended for use with
399 ** host languages that are garbage collected, and where the order in which
400 ** destructors are called is arbitrary.
@@ -403,11 +403,11 @@
403 ** [sqlite3_blob_close | close] all [BLOB handles], and
404 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
405 ** with the [sqlite3] object prior to attempting to close the object. ^If
406 ** sqlite3_close_v2() is called on a [database connection] that still has
407 ** outstanding [prepared statements], [BLOB handles], and/or
408 ** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
409 ** of resources is deferred until all [prepared statements], [BLOB handles],
410 ** and [sqlite3_backup] objects are also destroyed.
411 **
412 ** ^If an [sqlite3] object is destroyed while a transaction is open,
413 ** the transaction is automatically rolled back.
@@ -499,20 +499,18 @@
499 char **errmsg /* Error msg written here */
500 );
501
502 /*
503 ** CAPI3REF: Result Codes
504 ** KEYWORDS: {result code definitions}
 
505 **
506 ** Many SQLite functions return an integer result code from the set shown
507 ** here in order to indicate success or failure.
508 **
509 ** New error codes may be added in future versions of SQLite.
510 **
511 ** See also: [extended result code definitions]
 
512 */
513 #define SQLITE_OK 0 /* Successful result */
514 /* beginning-of-error-codes */
515 #define SQLITE_ERROR 1 /* SQL error or missing database */
516 #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -546,30 +544,23 @@
544 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
545 /* end-of-error-codes */
546
547 /*
548 ** CAPI3REF: Extended Result Codes
549 ** KEYWORDS: {extended result code definitions}
 
550 **
551 ** In its default configuration, SQLite API routines return one of 30 integer
552 ** [result codes]. However, experience has shown that many of
553 ** these result codes are too coarse-grained. They do not provide as
554 ** much information about problems as programmers might like. In an effort to
555 ** address this, newer versions of SQLite (version 3.3.8 and later) include
556 ** support for additional result codes that provide more detailed information
557 ** about errors. These [extended result codes] are enabled or disabled
558 ** on a per database connection basis using the
559 ** [sqlite3_extended_result_codes()] API. Or, the extended code for
560 ** the most recent error can be obtained using
561 ** [sqlite3_extended_errcode()].
 
 
 
 
 
 
562 */
563 #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
564 #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
565 #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
566 #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -798,11 +789,11 @@
789 ** write return values. Potential uses for xFileControl() might be
790 ** functions to enable blocking locks with timeouts, to change the
791 ** locking strategy (for example to use dot-file locks), to inquire
792 ** about the status of a lock, or to break stale locks. The SQLite
793 ** core reserves all opcodes less than 100 for its own use.
794 ** A [file control opcodes | list of opcodes] less than 100 is available.
795 ** Applications that define a custom xFileControl method should use opcodes
796 ** greater than 100 to avoid conflicts. VFS implementations should
797 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
798 ** recognize.
799 **
@@ -871,10 +862,11 @@
862 /* Additional methods may be added in future releases */
863 };
864
865 /*
866 ** CAPI3REF: Standard File Control Opcodes
867 ** KEYWORDS: {file control opcodes} {file control opcode}
868 **
869 ** These integer constants are opcodes for the xFileControl method
870 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
871 ** interface.
872 **
@@ -2150,31 +2142,37 @@
2142 SQLITE_API int sqlite3_complete16(const void *sql);
2143
2144 /*
2145 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2146 **
2147 ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2148 ** that might be invoked with argument P whenever
2149 ** an attempt is made to access a database table associated with
2150 ** [database connection] D when another thread
2151 ** or process has the table locked.
2152 ** The sqlite3_busy_handler() interface is used to implement
2153 ** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2154 **
2155 ** ^If the busy callback is NULL, then [SQLITE_BUSY]
2156 ** is returned immediately upon encountering the lock. ^If the busy callback
2157 ** is not NULL, then the callback might be invoked with two arguments.
2158 **
2159 ** ^The first argument to the busy handler is a copy of the void* pointer which
2160 ** is the third argument to sqlite3_busy_handler(). ^The second argument to
2161 ** the busy handler callback is the number of times that the busy handler has
2162 ** been invoked for the same locking event. ^If the
2163 ** busy callback returns 0, then no additional attempts are made to
2164 ** access the database and [SQLITE_BUSY] is returned
2165 ** to the application.
2166 ** ^If the callback returns non-zero, then another attempt
2167 ** is made to access the database and the cycle repeats.
2168 **
2169 ** The presence of a busy handler does not guarantee that it will be invoked
2170 ** when there is lock contention. ^If SQLite determines that invoking the busy
2171 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2172 ** to the application instead of invoking the
2173 ** busy handler.
2174 ** Consider a scenario where one process is holding a read lock that
2175 ** it is trying to promote to a reserved lock and
2176 ** a second process is holding a reserved lock that it is trying
2177 ** to promote to an exclusive lock. The first process cannot proceed
2178 ** because it is blocked by the second and the second process cannot
@@ -2184,32 +2182,19 @@
2182 ** will induce the first process to release its read lock and allow
2183 ** the second process to proceed.
2184 **
2185 ** ^The default busy callback is NULL.
2186 **
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2187 ** ^(There can only be a single busy handler defined for each
2188 ** [database connection]. Setting a new busy handler clears any
2189 ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2190 ** or evaluating [PRAGMA busy_timeout=N] will change the
2191 ** busy handler and thus clear any previously set busy handler.
2192 **
2193 ** The busy callback should not take any actions which modify the
2194 ** database connection that invoked the busy handler. In other words,
2195 ** the busy handler is not reentrant. Any such actions
2196 ** result in undefined behavior.
2197 **
2198 ** A busy handler must not close the database connection
2199 ** or [prepared statement] that invoked the busy handler.
2200 */
@@ -2221,19 +2206,21 @@
2206 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2207 ** for a specified amount of time when a table is locked. ^The handler
2208 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2209 ** have accumulated. ^After at least "ms" milliseconds of sleeping,
2210 ** the handler returns 0 which causes [sqlite3_step()] to return
2211 ** [SQLITE_BUSY].
2212 **
2213 ** ^Calling this routine with an argument less than or equal to zero
2214 ** turns off all busy handlers.
2215 **
2216 ** ^(There can only be a single busy handler for a particular
2217 ** [database connection] any any given moment. If another busy handler
2218 ** was defined (using [sqlite3_busy_handler()]) prior to calling
2219 ** this routine, that other busy handler is cleared.)^
2220 **
2221 ** See also: [PRAGMA busy_timeout]
2222 */
2223 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2224
2225 /*
2226 ** CAPI3REF: Convenience Routines For Running Queries
@@ -2631,12 +2618,12 @@
2618 ** return either [SQLITE_OK] or one of these two constants in order
2619 ** to signal SQLite whether or not the action is permitted. See the
2620 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2621 ** information.
2622 **
2623 ** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
2624 ** returned from the [sqlite3_vtab_on_conflict()] interface.
2625 */
2626 #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
2627 #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
2628
2629 /*
@@ -4818,10 +4805,17 @@
4805 ** the name of a folder (a.k.a. directory), then all temporary files
4806 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4807 ** will be placed in that directory.)^ ^If this variable
4808 ** is a NULL pointer, then SQLite performs a search for an appropriate
4809 ** temporary file directory.
4810 **
4811 ** Applications are strongly discouraged from using this global variable.
4812 ** It is required to set a temporary folder on Windows Runtime (WinRT).
4813 ** But for all other platforms, it is highly recommended that applications
4814 ** neither read nor write this variable. This global variable is a relic
4815 ** that exists for backwards compatibility of legacy applications and should
4816 ** be avoided in new projects.
4817 **
4818 ** It is not safe to read or modify this variable in more than one
4819 ** thread at a time. It is not safe to read or modify this variable
4820 ** if a [database connection] is being used at the same time in a separate
4821 ** thread.
@@ -4837,10 +4831,15 @@
4831 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4832 ** using [sqlite3_free].
4833 ** Hence, if this variable is modified directly, either it should be
4834 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4835 ** or else the use of the [temp_store_directory pragma] should be avoided.
4836 ** Except when requested by the [temp_store_directory pragma], SQLite
4837 ** does not free the memory that sqlite3_temp_directory points to. If
4838 ** the application wants that memory to be freed, it must do
4839 ** so itself, taking care to only do so after all [database connection]
4840 ** objects have been destroyed.
4841 **
4842 ** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4843 ** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
4844 ** features that require the use of temporary files may fail. Here is an
4845 ** example of how to do this using C++ with the Windows Runtime:
@@ -5971,14 +5970,16 @@
5970 ** <ul>
5971 ** <li> SQLITE_MUTEX_FAST
5972 ** <li> SQLITE_MUTEX_RECURSIVE
5973 ** <li> SQLITE_MUTEX_STATIC_MASTER
5974 ** <li> SQLITE_MUTEX_STATIC_MEM
5975 ** <li> SQLITE_MUTEX_STATIC_OPEN
5976 ** <li> SQLITE_MUTEX_STATIC_PRNG
5977 ** <li> SQLITE_MUTEX_STATIC_LRU
5978 ** <li> SQLITE_MUTEX_STATIC_PMEM
5979 ** <li> SQLITE_MUTEX_STATIC_APP1
5980 ** <li> SQLITE_MUTEX_STATIC_APP2
5981 ** </ul>)^
5982 **
5983 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5984 ** cause sqlite3_mutex_alloc() to create
5985 ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6178,10 +6179,13 @@
6179 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
6180 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
6181 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
6182 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
6183 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
6184 #define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
6185 #define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
6186 #define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
6187
6188 /*
6189 ** CAPI3REF: Retrieve the mutex for a database connection
6190 **
6191 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6273,11 +6277,12 @@
6277 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6278 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6279 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6280 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6281 #define SQLITE_TESTCTRL_BYTEORDER 22
6282 #define SQLITE_TESTCTRL_ISINIT 23
6283 #define SQLITE_TESTCTRL_LAST 23
6284
6285 /*
6286 ** CAPI3REF: SQLite Runtime Status
6287 **
6288 ** ^This interface is used to retrieve runtime status information
@@ -7256,10 +7261,13 @@
7261 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7262 ** configured by this function.
7263 **
7264 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7265 ** from SQL.
7266 **
7267 ** ^Checkpoints initiated by this mechanism are
7268 ** [sqlite3_wal_checkpoint_v2|PASSIVE].
7269 **
7270 ** ^Every new [database connection] defaults to having the auto-checkpoint
7271 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7272 ** pages. The use of this interface
7273 ** is only necessary if the default setting is found to be suboptimal
@@ -7273,10 +7281,14 @@
7281 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7282 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
7283 ** empty string, then a checkpoint is run on all databases of
7284 ** connection D. ^If the database connection D is not in
7285 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7286 ** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
7287 ** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
7288 ** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
7289 ** or RESET checkpoint.
7290 **
7291 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7292 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
7293 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7294 ** run whenever the WAL reaches a certain size threshold.
@@ -7295,22 +7307,25 @@
7307 ** <dl>
7308 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7309 ** Checkpoint as many frames as possible without waiting for any database
7310 ** readers or writers to finish. Sync the db file if all frames in the log
7311 ** are checkpointed. This mode is the same as calling
7312 ** sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
7313 ** is never invoked.
7314 **
7315 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7316 ** This mode blocks (it invokes the
7317 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
7318 ** database writer and all readers are reading from the most recent database
7319 ** snapshot. It then checkpoints all frames in the log file and syncs the
7320 ** database file. This call blocks database writers while it is running,
7321 ** but not database readers.
7322 **
7323 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7324 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7325 ** checkpointing the log file it blocks (calls the
7326 ** [sqlite3_busy_handler|busy-handler callback])
7327 ** until all readers are reading from the database file only. This ensures
7328 ** that the next client to write to the database file restarts the log file
7329 ** from the beginning. This call blocks database writers while it is running,
7330 ** but not database readers.
7331 ** </dl>
@@ -7444,10 +7459,11 @@
7459 */
7460 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7461
7462 /*
7463 ** CAPI3REF: Conflict resolution modes
7464 ** KEYWORDS: {conflict resolution mode}
7465 **
7466 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7467 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7468 ** is for the SQL statement being evaluated.
7469 **
@@ -9285,43 +9301,43 @@
9301 #define OP_Affinity 47 /* synopsis: affinity(r[P1@P2]) */
9302 #define OP_MakeRecord 48 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
9303 #define OP_Count 49 /* synopsis: r[P2]=count() */
9304 #define OP_ReadCookie 50
9305 #define OP_SetCookie 51
9306 #define OP_ReopenIdx 52 /* synopsis: root=P2 iDb=P3 */
9307 #define OP_OpenRead 53 /* synopsis: root=P2 iDb=P3 */
9308 #define OP_OpenWrite 54 /* synopsis: root=P2 iDb=P3 */
9309 #define OP_OpenAutoindex 55 /* synopsis: nColumn=P2 */
9310 #define OP_OpenEphemeral 56 /* synopsis: nColumn=P2 */
9311 #define OP_SorterOpen 57
9312 #define OP_OpenPseudo 58 /* synopsis: P3 columns in r[P2] */
9313 #define OP_Close 59
9314 #define OP_SeekLT 60 /* synopsis: key=r[P3@P4] */
9315 #define OP_SeekLE 61 /* synopsis: key=r[P3@P4] */
9316 #define OP_SeekGE 62 /* synopsis: key=r[P3@P4] */
9317 #define OP_SeekGT 63 /* synopsis: key=r[P3@P4] */
9318 #define OP_Seek 64 /* synopsis: intkey=r[P2] */
9319 #define OP_NoConflict 65 /* synopsis: key=r[P3@P4] */
9320 #define OP_NotFound 66 /* synopsis: key=r[P3@P4] */
9321 #define OP_Found 67 /* synopsis: key=r[P3@P4] */
9322 #define OP_NotExists 68 /* synopsis: intkey=r[P3] */
9323 #define OP_Sequence 69 /* synopsis: r[P2]=cursor[P1].ctr++ */
9324 #define OP_NewRowid 70 /* synopsis: r[P2]=rowid */
9325 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9326 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9327 #define OP_Insert 73 /* synopsis: intkey=r[P3] data=r[P2] */
9328 #define OP_InsertInt 74 /* synopsis: intkey=P3 data=r[P2] */
9329 #define OP_Delete 75
9330 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9331 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9332 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9333 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9334 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9335 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9336 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9337 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9338 #define OP_ResetCount 84
9339 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9340 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9341 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9342 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9343 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9328,73 +9344,74 @@
9344 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9345 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9346 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9347 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9348 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9349 #define OP_SorterCompare 95 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9350 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9351 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9352 #define OP_SorterData 98 /* synopsis: r[P2]=data */
9353 #define OP_RowKey 99 /* synopsis: r[P2]=key */
9354 #define OP_RowData 100 /* synopsis: r[P2]=data */
9355 #define OP_Rowid 101 /* synopsis: r[P2]=rowid */
9356 #define OP_NullRow 102
9357 #define OP_Last 103
9358 #define OP_SorterSort 104
9359 #define OP_Sort 105
9360 #define OP_Rewind 106
9361 #define OP_SorterInsert 107
9362 #define OP_IdxInsert 108 /* synopsis: key=r[P2] */
9363 #define OP_IdxDelete 109 /* synopsis: key=r[P2@P3] */
9364 #define OP_IdxRowid 110 /* synopsis: r[P2]=rowid */
9365 #define OP_IdxLE 111 /* synopsis: key=r[P3@P4] */
9366 #define OP_IdxGT 112 /* synopsis: key=r[P3@P4] */
9367 #define OP_IdxLT 113 /* synopsis: key=r[P3@P4] */
9368 #define OP_IdxGE 114 /* synopsis: key=r[P3@P4] */
9369 #define OP_Destroy 115
9370 #define OP_Clear 116
9371 #define OP_ResetSorter 117
9372 #define OP_CreateIndex 118 /* synopsis: r[P2]=root iDb=P1 */
9373 #define OP_CreateTable 119 /* synopsis: r[P2]=root iDb=P1 */
9374 #define OP_ParseSchema 120
9375 #define OP_LoadAnalysis 121
9376 #define OP_DropTable 122
9377 #define OP_DropIndex 123
9378 #define OP_DropTrigger 124
9379 #define OP_IntegrityCk 125
9380 #define OP_RowSetAdd 126 /* synopsis: rowset(P1)=r[P2] */
9381 #define OP_RowSetRead 127 /* synopsis: r[P3]=rowset(P1) */
9382 #define OP_RowSetTest 128 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9383 #define OP_Program 129
9384 #define OP_Param 130
9385 #define OP_FkCounter 131 /* synopsis: fkctr[P1]+=P2 */
9386 #define OP_FkIfZero 132 /* synopsis: if fkctr[P1]==0 goto P2 */
9387 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9388 #define OP_MemMax 134 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9389 #define OP_IfPos 135 /* synopsis: if r[P1]>0 goto P2 */
9390 #define OP_IfNeg 136 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9391 #define OP_IfZero 137 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2 */
9392 #define OP_AggFinal 138 /* synopsis: accum=r[P1] N=P2 */
9393 #define OP_IncrVacuum 139
9394 #define OP_Expire 140
9395 #define OP_TableLock 141 /* synopsis: iDb=P1 root=P2 write=P3 */
9396 #define OP_VBegin 142
9397 #define OP_ToText 143 /* same as TK_TO_TEXT */
9398 #define OP_ToBlob 144 /* same as TK_TO_BLOB */
9399 #define OP_ToNumeric 145 /* same as TK_TO_NUMERIC */
9400 #define OP_ToInt 146 /* same as TK_TO_INT */
9401 #define OP_ToReal 147 /* same as TK_TO_REAL */
9402 #define OP_VCreate 148
9403 #define OP_VDestroy 149
9404 #define OP_VOpen 150
9405 #define OP_VColumn 151 /* synopsis: r[P3]=vcolumn(P2) */
9406 #define OP_VNext 152
9407 #define OP_VRename 153
9408 #define OP_Pagecount 154
9409 #define OP_MaxPgcnt 155
9410 #define OP_Init 156 /* synopsis: Start at P2 */
9411 #define OP_Noop 157
9412 #define OP_Explain 158
9413
9414
9415 /* Properties such as "out2" or "jump" that are specified in
9416 ** comments following the "case" for each opcode in the vdbe.c
9417 ** are encoded into bitvectors as follows:
@@ -9412,23 +9429,23 @@
9429 /* 16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
9430 /* 24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
9431 /* 32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
9432 /* 40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
9433 /* 48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
9434 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
9435 /* 64 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x4c,\
9436 /* 72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
9437 /* 80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
9438 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
9439 /* 96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
9440 /* 104 */ 0x01, 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01,\
9441 /* 112 */ 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02,\
9442 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45,\
9443 /* 128 */ 0x15, 0x01, 0x02, 0x00, 0x01, 0x02, 0x08, 0x05,\
9444 /* 136 */ 0x05, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,\
9445 /* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,\
9446 /* 152 */ 0x01, 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
9447
9448 /************** End of opcodes.h *********************************************/
9449 /************** Continuing where we left off in vdbe.h ***********************/
9450
9451 /*
@@ -9479,10 +9496,11 @@
9496 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
9497 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
9498 #ifndef SQLITE_OMIT_TRACE
9499 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9500 #endif
9501 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
9502
9503 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9504 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
9505 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
9506
@@ -10330,22 +10348,22 @@
10348 Hash trigHash; /* All triggers indexed by name */
10349 Hash fkeyHash; /* All foreign keys by referenced table name */
10350 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
10351 u8 file_format; /* Schema format version for this file */
10352 u8 enc; /* Text encoding used by this database */
10353 u16 schemaFlags; /* Flags associated with this schema */
10354 int cache_size; /* Number of pages to use in the cache */
10355 };
10356
10357 /*
10358 ** These macros can be used to test, set, or clear bits in the
10359 ** Db.pSchema->flags field.
10360 */
10361 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
10362 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
10363 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
10364 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
10365
10366 /*
10367 ** Allowed values for the DB.pSchema->flags field.
10368 **
10369 ** The DB_SchemaLoaded flag is set after the database schema has been
@@ -10931,10 +10949,13 @@
10949 int tnum; /* Root BTree node for this table (see note above) */
10950 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10951 i16 nCol; /* Number of columns in this table */
10952 u16 nRef; /* Number of pointers to this Table */
10953 LogEst szTabRow; /* Estimated size of each table row in bytes */
10954 #ifdef SQLITE_ENABLE_COSTMULT
10955 LogEst costMult; /* Cost multiplier for using this table */
10956 #endif
10957 u8 tabFlags; /* Mask of TF_* values */
10958 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
10959 #ifndef SQLITE_OMIT_ALTERTABLE
10960 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
10961 #endif
@@ -11171,10 +11192,13 @@
11192 #define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
11193
11194 /* Return true if index X is a PRIMARY KEY index */
11195 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
11196
11197 /* Return true if index X is a UNIQUE index */
11198 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
11199
11200 /*
11201 ** Each sample stored in the sqlite_stat3 table is represented in memory
11202 ** using a structure of this type. See documentation at the top of the
11203 ** analyze.c source file for additional information.
11204 */
@@ -11590,10 +11614,11 @@
11614 #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
11615 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11616 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11617 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11618 #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
11619 #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */
11620
11621 /* Allowed return values from sqlite3WhereIsDistinct()
11622 */
11623 #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11624 #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -11846,13 +11871,23 @@
11871
11872 /*
11873 ** The yDbMask datatype for the bitmask of all attached databases.
11874 */
11875 #if SQLITE_MAX_ATTACHED>30
11876 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
11877 # define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
11878 # define DbMaskZero(M) memset((M),0,sizeof(M))
11879 # define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
11880 # define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
11881 # define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
11882 #else
11883 typedef unsigned int yDbMask;
11884 # define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
11885 # define DbMaskZero(M) (M)=0
11886 # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
11887 # define DbMaskAllZero(M) (M)==0
11888 # define DbMaskNonZero(M) (M)!=0
11889 #endif
11890
11891 /*
11892 ** An SQL parser context. A copy of this structure is passed through
11893 ** the parser and down into all the parser action routine in order to
@@ -12521,10 +12556,13 @@
12556 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
12557 #else
12558 # define sqlite3ViewGetColumnNames(A,B) 0
12559 #endif
12560
12561 #if SQLITE_MAX_ATTACHED>30
12562 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
12563 #endif
12564 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
12565 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
12566 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
12567 #ifndef SQLITE_OMIT_AUTOINCREMENT
12568 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
@@ -12771,10 +12809,11 @@
12809 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
12810 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
12811 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
12812 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
12813 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
12814 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
12815 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
12816 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
12817 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
12818 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
12819
@@ -12800,11 +12839,11 @@
12839 #ifdef SQLITE_ENABLE_8_3_NAMES
12840 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
12841 #else
12842 # define sqlite3FileSuffix3(X,Y)
12843 #endif
12844 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
12845
12846 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
12847 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
12848 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12849 void(*)(void*));
@@ -12885,11 +12924,13 @@
12924 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12925
12926 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12927 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12928 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12929 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
12930 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12931 SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
12932 #endif
12933
12934 /*
12935 ** The interface to the LEMON-generated parser
12936 */
@@ -13022,15 +13063,25 @@
13063 #else
13064 #define sqlite3BeginBenignMalloc()
13065 #define sqlite3EndBenignMalloc()
13066 #endif
13067
13068 /*
13069 ** Allowed return values from sqlite3FindInIndex()
13070 */
13071 #define IN_INDEX_ROWID 1 /* Search the rowid of the table */
13072 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
13073 #define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
13074 #define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
13075 #define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
13076 /*
13077 ** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
13078 */
13079 #define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
13080 #define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
13081 #define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
13082 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
13083
13084 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13085 SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
13086 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
13087 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
@@ -13873,18 +13924,22 @@
13924 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
13925 int seekResult; /* Result of previous sqlite3BtreeMoveto() */
13926 int pseudoTableReg; /* Register holding pseudotable content. */
13927 i16 nField; /* Number of fields in the header */
13928 u16 nHdrParsed; /* Number of header fields parsed so far */
13929 #ifdef SQLITE_DEBUG
13930 u8 seekOp; /* Most recent seek operation on this cursor */
13931 #endif
13932 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
13933 u8 nullRow; /* True if pointing to a row with no data */
13934 u8 rowidIsValid; /* True if lastRowid is valid */
13935 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
13936 Bool isEphemeral:1; /* True for an ephemeral table */
13937 Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
13938 Bool isTable:1; /* True if a table requiring integer keys */
13939 Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
13940 Pgno pgnoRoot; /* Root page of the open btree cursor */
13941 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
13942 i64 seqCount; /* Sequence counter */
13943 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
13944 i64 lastRowid; /* Rowid being deleted by OP_Delete */
13945 VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
@@ -14199,11 +14254,10 @@
14254 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14255
14256 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14257 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
14258 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 
14259 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14260 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14261 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
14262 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
14263 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
@@ -18394,11 +18448,11 @@
18448 /*
18449 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18450 */
18451 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18452 #ifndef SQLITE_OMIT_AUTOINIT
18453 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
18454 #endif
18455 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18456 }
18457
18458 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -18575,11 +18629,11 @@
18629 ** The sqlite3_mutex_alloc() routine allocates a new
18630 ** mutex and returns a pointer to it. If it returns NULL
18631 ** that means that a mutex could not be allocated.
18632 */
18633 static sqlite3_mutex *debugMutexAlloc(int id){
18634 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
18635 sqlite3_debug_mutex *pNew = 0;
18636 switch( id ){
18637 case SQLITE_MUTEX_FAST:
18638 case SQLITE_MUTEX_RECURSIVE: {
18639 pNew = sqlite3Malloc(sizeof(*pNew));
@@ -18772,14 +18826,17 @@
18826 ** <ul>
18827 ** <li> SQLITE_MUTEX_FAST
18828 ** <li> SQLITE_MUTEX_RECURSIVE
18829 ** <li> SQLITE_MUTEX_STATIC_MASTER
18830 ** <li> SQLITE_MUTEX_STATIC_MEM
18831 ** <li> SQLITE_MUTEX_STATIC_OPEN
18832 ** <li> SQLITE_MUTEX_STATIC_PRNG
18833 ** <li> SQLITE_MUTEX_STATIC_LRU
18834 ** <li> SQLITE_MUTEX_STATIC_PMEM
18835 ** <li> SQLITE_MUTEX_STATIC_APP1
18836 ** <li> SQLITE_MUTEX_STATIC_APP2
18837 ** <li> SQLITE_MUTEX_STATIC_APP3
18838 ** </ul>
18839 **
18840 ** The first two constants cause sqlite3_mutex_alloc() to create
18841 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
18842 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -18804,10 +18861,13 @@
18861 ** mutex types, the same mutex is returned on every call that has
18862 ** the same type number.
18863 */
18864 static sqlite3_mutex *pthreadMutexAlloc(int iType){
18865 static sqlite3_mutex staticMutexes[] = {
18866 SQLITE3_MUTEX_INITIALIZER,
18867 SQLITE3_MUTEX_INITIALIZER,
18868 SQLITE3_MUTEX_INITIALIZER,
18869 SQLITE3_MUTEX_INITIALIZER,
18870 SQLITE3_MUTEX_INITIALIZER,
18871 SQLITE3_MUTEX_INITIALIZER,
18872 SQLITE3_MUTEX_INITIALIZER,
18873 SQLITE3_MUTEX_INITIALIZER,
@@ -19039,14 +19099,227 @@
19099 ** May you do good and not evil.
19100 ** May you find forgiveness for yourself and forgive others.
19101 ** May you share freely, never taking more than you give.
19102 **
19103 *************************************************************************
19104 ** This file contains the C functions that implement mutexes for Win32.
19105 */
19106
19107 #if SQLITE_OS_WIN
19108 /*
19109 ** Include code that is common to all os_*.c files
19110 */
19111 /************** Include os_common.h in the middle of mutex_w32.c *************/
19112 /************** Begin file os_common.h ***************************************/
19113 /*
19114 ** 2004 May 22
19115 **
19116 ** The author disclaims copyright to this source code. In place of
19117 ** a legal notice, here is a blessing:
19118 **
19119 ** May you do good and not evil.
19120 ** May you find forgiveness for yourself and forgive others.
19121 ** May you share freely, never taking more than you give.
19122 **
19123 ******************************************************************************
19124 **
19125 ** This file contains macros and a little bit of code that is common to
19126 ** all of the platform-specific files (os_*.c) and is #included into those
19127 ** files.
19128 **
19129 ** This file should be #included by the os_*.c files only. It is not a
19130 ** general purpose header file.
19131 */
19132 #ifndef _OS_COMMON_H_
19133 #define _OS_COMMON_H_
19134
19135 /*
19136 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
19137 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
19138 ** switch. The following code should catch this problem at compile-time.
19139 */
19140 #ifdef MEMORY_DEBUG
19141 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
19142 #endif
19143
19144 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
19145 # ifndef SQLITE_DEBUG_OS_TRACE
19146 # define SQLITE_DEBUG_OS_TRACE 0
19147 # endif
19148 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
19149 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
19150 #else
19151 # define OSTRACE(X)
19152 #endif
19153
19154 /*
19155 ** Macros for performance tracing. Normally turned off. Only works
19156 ** on i486 hardware.
19157 */
19158 #ifdef SQLITE_PERFORMANCE_TRACE
19159
19160 /*
19161 ** hwtime.h contains inline assembler code for implementing
19162 ** high-performance timing routines.
19163 */
19164 /************** Include hwtime.h in the middle of os_common.h ****************/
19165 /************** Begin file hwtime.h ******************************************/
19166 /*
19167 ** 2008 May 27
19168 **
19169 ** The author disclaims copyright to this source code. In place of
19170 ** a legal notice, here is a blessing:
19171 **
19172 ** May you do good and not evil.
19173 ** May you find forgiveness for yourself and forgive others.
19174 ** May you share freely, never taking more than you give.
19175 **
19176 ******************************************************************************
19177 **
19178 ** This file contains inline asm code for retrieving "high-performance"
19179 ** counters for x86 class CPUs.
19180 */
19181 #ifndef _HWTIME_H_
19182 #define _HWTIME_H_
19183
19184 /*
19185 ** The following routine only works on pentium-class (or newer) processors.
19186 ** It uses the RDTSC opcode to read the cycle count value out of the
19187 ** processor and returns that value. This can be used for high-res
19188 ** profiling.
19189 */
19190 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
19191 (defined(i386) || defined(__i386__) || defined(_M_IX86))
19192
19193 #if defined(__GNUC__)
19194
19195 __inline__ sqlite_uint64 sqlite3Hwtime(void){
19196 unsigned int lo, hi;
19197 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
19198 return (sqlite_uint64)hi << 32 | lo;
19199 }
19200
19201 #elif defined(_MSC_VER)
19202
19203 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
19204 __asm {
19205 rdtsc
19206 ret ; return value at EDX:EAX
19207 }
19208 }
19209
19210 #endif
19211
19212 #elif (defined(__GNUC__) && defined(__x86_64__))
19213
19214 __inline__ sqlite_uint64 sqlite3Hwtime(void){
19215 unsigned long val;
19216 __asm__ __volatile__ ("rdtsc" : "=A" (val));
19217 return val;
19218 }
19219
19220 #elif (defined(__GNUC__) && defined(__ppc__))
19221
19222 __inline__ sqlite_uint64 sqlite3Hwtime(void){
19223 unsigned long long retval;
19224 unsigned long junk;
19225 __asm__ __volatile__ ("\n\
19226 1: mftbu %1\n\
19227 mftb %L0\n\
19228 mftbu %0\n\
19229 cmpw %0,%1\n\
19230 bne 1b"
19231 : "=r" (retval), "=r" (junk));
19232 return retval;
19233 }
19234
19235 #else
19236
19237 #error Need implementation of sqlite3Hwtime() for your platform.
19238
19239 /*
19240 ** To compile without implementing sqlite3Hwtime() for your platform,
19241 ** you can remove the above #error and use the following
19242 ** stub function. You will lose timing support for many
19243 ** of the debugging and testing utilities, but it should at
19244 ** least compile and run.
19245 */
19246 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
19247
19248 #endif
19249
19250 #endif /* !defined(_HWTIME_H_) */
19251
19252 /************** End of hwtime.h **********************************************/
19253 /************** Continuing where we left off in os_common.h ******************/
19254
19255 static sqlite_uint64 g_start;
19256 static sqlite_uint64 g_elapsed;
19257 #define TIMER_START g_start=sqlite3Hwtime()
19258 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
19259 #define TIMER_ELAPSED g_elapsed
19260 #else
19261 #define TIMER_START
19262 #define TIMER_END
19263 #define TIMER_ELAPSED ((sqlite_uint64)0)
19264 #endif
19265
19266 /*
19267 ** If we compile with the SQLITE_TEST macro set, then the following block
19268 ** of code will give us the ability to simulate a disk I/O error. This
19269 ** is used for testing the I/O recovery logic.
19270 */
19271 #ifdef SQLITE_TEST
19272 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
19273 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
19274 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
19275 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
19276 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
19277 SQLITE_API int sqlite3_diskfull_pending = 0;
19278 SQLITE_API int sqlite3_diskfull = 0;
19279 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
19280 #define SimulateIOError(CODE) \
19281 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
19282 || sqlite3_io_error_pending-- == 1 ) \
19283 { local_ioerr(); CODE; }
19284 static void local_ioerr(){
19285 IOTRACE(("IOERR\n"));
19286 sqlite3_io_error_hit++;
19287 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
19288 }
19289 #define SimulateDiskfullError(CODE) \
19290 if( sqlite3_diskfull_pending ){ \
19291 if( sqlite3_diskfull_pending == 1 ){ \
19292 local_ioerr(); \
19293 sqlite3_diskfull = 1; \
19294 sqlite3_io_error_hit = 1; \
19295 CODE; \
19296 }else{ \
19297 sqlite3_diskfull_pending--; \
19298 } \
19299 }
19300 #else
19301 #define SimulateIOErrorBenign(X)
19302 #define SimulateIOError(A)
19303 #define SimulateDiskfullError(A)
19304 #endif
19305
19306 /*
19307 ** When testing, keep a count of the number of open files.
19308 */
19309 #ifdef SQLITE_TEST
19310 SQLITE_API int sqlite3_open_file_count = 0;
19311 #define OpenCounter(X) sqlite3_open_file_count+=(X)
19312 #else
19313 #define OpenCounter(X)
19314 #endif
19315
19316 #endif /* !defined(_OS_COMMON_H_) */
19317
19318 /************** End of os_common.h *******************************************/
19319 /************** Continuing where we left off in mutex_w32.c ******************/
19320
19321 /*
19322 ** Include the header file for the Windows VFS.
19323 */
19324 /************** Include os_win.h in the middle of mutex_w32.c ****************/
19325 /************** Begin file os_win.h ******************************************/
@@ -19122,11 +19395,11 @@
19395 /************** Continuing where we left off in mutex_w32.c ******************/
19396 #endif
19397
19398 /*
19399 ** The code in this file is only used if we are compiling multithreaded
19400 ** on a Win32 system.
19401 */
19402 #ifdef SQLITE_MUTEX_W32
19403
19404 /*
19405 ** Each recursive mutex is an instance of the following structure.
@@ -19135,94 +19408,75 @@
19408 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
19409 int id; /* Mutex type */
19410 #ifdef SQLITE_DEBUG
19411 volatile int nRef; /* Number of enterances */
19412 volatile DWORD owner; /* Thread holding this mutex */
19413 volatile int trace; /* True to trace changes */
19414 #endif
19415 };
19416
19417 /*
19418 ** These are the initializer values used when declaring a "static" mutex
19419 ** on Win32. It should be noted that all mutexes require initialization
19420 ** on the Win32 platform.
19421 */
19422 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
19423
19424 #ifdef SQLITE_DEBUG
19425 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
19426 0L, (DWORD)0, 0 }
19427 #else
19428 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
19429 #endif
19430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19431 #ifdef SQLITE_DEBUG
19432 /*
19433 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
19434 ** intended for use only inside assert() statements.
19435 */
19436 static int winMutexHeld(sqlite3_mutex *p){
19437 return p->nRef!=0 && p->owner==GetCurrentThreadId();
19438 }
19439
19440 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
19441 return p->nRef==0 || p->owner!=tid;
19442 }
19443
19444 static int winMutexNotheld(sqlite3_mutex *p){
19445 DWORD tid = GetCurrentThreadId();
19446 return winMutexNotheld2(p, tid);
19447 }
19448 #endif
19449
 
19450 /*
19451 ** Initialize and deinitialize the mutex subsystem.
19452 */
19453 static sqlite3_mutex winMutex_staticMutexes[] = {
19454 SQLITE3_MUTEX_INITIALIZER,
19455 SQLITE3_MUTEX_INITIALIZER,
19456 SQLITE3_MUTEX_INITIALIZER,
19457 SQLITE3_MUTEX_INITIALIZER,
19458 SQLITE3_MUTEX_INITIALIZER,
19459 SQLITE3_MUTEX_INITIALIZER,
19460 SQLITE3_MUTEX_INITIALIZER,
19461 SQLITE3_MUTEX_INITIALIZER,
19462 SQLITE3_MUTEX_INITIALIZER
19463 };
19464
19465 static int winMutex_isInit = 0;
19466 static int winMutex_isNt = -1; /* <0 means "need to query" */
19467
19468 /* As the winMutexInit() and winMutexEnd() functions are called as part
19469 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
19470 ** "interlocked" magic used here is probably not strictly necessary.
19471 */
19472 static LONG volatile winMutex_lock = 0;
19473
19474 SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
19475 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
19476
19477 static int winMutexInit(void){
19478 /* The first to increment to 1 does actual initialization */
19479 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
19480 int i;
19481 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
19482 #if SQLITE_OS_WINRT
@@ -19231,20 +19485,21 @@
19485 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
19486 #endif
19487 }
19488 winMutex_isInit = 1;
19489 }else{
19490 /* Another thread is (in the process of) initializing the static
19491 ** mutexes */
19492 while( !winMutex_isInit ){
19493 sqlite3_win32_sleep(1);
19494 }
19495 }
19496 return SQLITE_OK;
19497 }
19498
19499 static int winMutexEnd(void){
19500 /* The first to decrement to 0 does actual shutdown
19501 ** (which should be the last to shutdown.) */
19502 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
19503 if( winMutex_isInit==1 ){
19504 int i;
19505 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
@@ -19251,11 +19506,11 @@
19506 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
19507 }
19508 winMutex_isInit = 0;
19509 }
19510 }
19511 return SQLITE_OK;
19512 }
19513
19514 /*
19515 ** The sqlite3_mutex_alloc() routine allocates a new
19516 ** mutex and returns a pointer to it. If it returns NULL
@@ -19266,14 +19521,17 @@
19521 ** <ul>
19522 ** <li> SQLITE_MUTEX_FAST
19523 ** <li> SQLITE_MUTEX_RECURSIVE
19524 ** <li> SQLITE_MUTEX_STATIC_MASTER
19525 ** <li> SQLITE_MUTEX_STATIC_MEM
19526 ** <li> SQLITE_MUTEX_STATIC_OPEN
19527 ** <li> SQLITE_MUTEX_STATIC_PRNG
19528 ** <li> SQLITE_MUTEX_STATIC_LRU
19529 ** <li> SQLITE_MUTEX_STATIC_PMEM
19530 ** <li> SQLITE_MUTEX_STATIC_APP1
19531 ** <li> SQLITE_MUTEX_STATIC_APP2
19532 ** <li> SQLITE_MUTEX_STATIC_APP3
19533 ** </ul>
19534 **
19535 ** The first two constants cause sqlite3_mutex_alloc() to create
19536 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
19537 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -19292,11 +19550,11 @@
19550 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
19551 ** SQLITE_MUTEX_RECURSIVE.
19552 **
19553 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
19554 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
19555 ** returns a different mutex on every call. But for the static
19556 ** mutex types, the same mutex is returned on every call that has
19557 ** the same type number.
19558 */
19559 static sqlite3_mutex *winMutexAlloc(int iType){
19560 sqlite3_mutex *p;
@@ -19303,13 +19561,16 @@
19561
19562 switch( iType ){
19563 case SQLITE_MUTEX_FAST:
19564 case SQLITE_MUTEX_RECURSIVE: {
19565 p = sqlite3MallocZero( sizeof(*p) );
19566 if( p ){
19567 #ifdef SQLITE_DEBUG
19568 p->id = iType;
19569 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
19570 p->trace = 1;
19571 #endif
19572 #endif
19573 #if SQLITE_OS_WINRT
19574 InitializeCriticalSectionEx(&p->mutex, 0, 0);
19575 #else
19576 InitializeCriticalSection(&p->mutex);
@@ -19316,16 +19577,19 @@
19577 #endif
19578 }
19579 break;
19580 }
19581 default: {
 
19582 assert( iType-2 >= 0 );
19583 assert( iType-2 < ArraySize(winMutex_staticMutexes) );
19584 assert( winMutex_isInit==1 );
19585 p = &winMutex_staticMutexes[iType-2];
19586 #ifdef SQLITE_DEBUG
19587 p->id = iType;
19588 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
19589 p->trace = 1;
19590 #endif
19591 #endif
19592 break;
19593 }
19594 }
19595 return p;
@@ -19337,12 +19601,15 @@
19601 ** allocated mutex. SQLite is careful to deallocate every
19602 ** mutex that it allocates.
19603 */
19604 static void winMutexFree(sqlite3_mutex *p){
19605 assert( p );
19606 #ifdef SQLITE_DEBUG
19607 assert( p->nRef==0 && p->owner==0 );
19608 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
19609 #endif
19610 assert( winMutex_isInit==1 );
19611 DeleteCriticalSection(&p->mutex);
19612 sqlite3_free(p);
19613 }
19614
19615 /*
@@ -19355,53 +19622,71 @@
19622 ** mutex must be exited an equal number of times before another thread
19623 ** can enter. If the same thread tries to enter any other kind of mutex
19624 ** more than once, the behavior is undefined.
19625 */
19626 static void winMutexEnter(sqlite3_mutex *p){
19627 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
19628 DWORD tid = GetCurrentThreadId();
19629 #endif
19630 #ifdef SQLITE_DEBUG
19631 assert( p );
19632 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19633 #else
19634 assert( p );
19635 #endif
19636 assert( winMutex_isInit==1 );
19637 EnterCriticalSection(&p->mutex);
19638 #ifdef SQLITE_DEBUG
19639 assert( p->nRef>0 || p->owner==0 );
19640 p->owner = tid;
19641 p->nRef++;
19642 if( p->trace ){
19643 OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
19644 tid, p, p->trace, p->nRef));
19645 }
19646 #endif
19647 }
19648
19649 static int winMutexTry(sqlite3_mutex *p){
19650 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
19651 DWORD tid = GetCurrentThreadId();
19652 #endif
19653 int rc = SQLITE_BUSY;
19654 assert( p );
19655 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19656 /*
19657 ** The sqlite3_mutex_try() routine is very rarely used, and when it
19658 ** is used it is merely an optimization. So it is OK for it to always
19659 ** fail.
19660 **
19661 ** The TryEnterCriticalSection() interface is only available on WinNT.
19662 ** And some windows compilers complain if you try to use it without
19663 ** first doing some #defines that prevent SQLite from building on Win98.
19664 ** For that reason, we will omit this optimization for now. See
19665 ** ticket #2685.
19666 */
19667 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
19668 assert( winMutex_isInit==1 );
19669 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
19670 if( winMutex_isNt<0 ){
19671 winMutex_isNt = sqlite3_win32_is_nt();
19672 }
19673 assert( winMutex_isNt==0 || winMutex_isNt==1 );
19674 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
19675 #ifdef SQLITE_DEBUG
19676 p->owner = tid;
19677 p->nRef++;
19678 #endif
19679 rc = SQLITE_OK;
19680 }
19681 #else
19682 UNUSED_PARAMETER(p);
19683 #endif
19684 #ifdef SQLITE_DEBUG
19685 if( p->trace ){
19686 OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
19687 tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
19688 }
19689 #endif
19690 return rc;
19691 }
19692
@@ -19410,22 +19695,27 @@
19695 ** previously entered by the same thread. The behavior
19696 ** is undefined if the mutex is not currently entered or
19697 ** is not currently allocated. SQLite will never do either.
19698 */
19699 static void winMutexLeave(sqlite3_mutex *p){
19700 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
19701 DWORD tid = GetCurrentThreadId();
19702 #endif
19703 assert( p );
19704 #ifdef SQLITE_DEBUG
19705 assert( p->nRef>0 );
19706 assert( p->owner==tid );
19707 p->nRef--;
19708 if( p->nRef==0 ) p->owner = 0;
19709 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
19710 #endif
19711 assert( winMutex_isInit==1 );
19712 LeaveCriticalSection(&p->mutex);
19713 #ifdef SQLITE_DEBUG
19714 if( p->trace ){
19715 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
19716 tid, p, p->trace, p->nRef));
19717 }
19718 #endif
19719 }
19720
19721 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
@@ -19443,13 +19733,13 @@
19733 #else
19734 0,
19735 0
19736 #endif
19737 };
 
19738 return &sMutex;
19739 }
19740
19741 #endif /* SQLITE_MUTEX_W32 */
19742
19743 /************** End of mutex_w32.c *******************************************/
19744 /************** Begin file malloc.c ******************************************/
19745 /*
@@ -21562,12 +21852,12 @@
21852 ** * Bytes in the range of 0x80 through 0xbf which occur as the first
21853 ** byte of a character are interpreted as single-byte characters
21854 ** and rendered as themselves even though they are technically
21855 ** invalid characters.
21856 **
21857 ** * This routine accepts over-length UTF8 encodings
21858 ** for unicode values 0x80 and greater. It does not change over-length
21859 ** encodings to 0xfffd as some systems recommend.
21860 */
21861 #define READ_UTF8(zIn, zTerm, c) \
21862 c = *(zIn++); \
21863 if( c>=0xc0 ){ \
@@ -22420,13 +22710,13 @@
22710 testcase( c==(+1) );
22711 }
22712 return c;
22713 }
22714
 
22715 /*
22716 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
22717 ** routine does *not* accept hexadecimal notation.
22718 **
22719 ** If the zNum value is representable as a 64-bit twos-complement
22720 ** integer, then write that value into *pNum and return 0.
22721 **
22722 ** If zNum is exactly 9223372036854775808, return 2. This special
@@ -22509,14 +22799,48 @@
22799 assert( u-1==LARGEST_INT64 );
22800 return neg ? 0 : 2;
22801 }
22802 }
22803 }
22804
22805 /*
22806 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
22807 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
22808 ** whereas sqlite3Atoi64() does not.
22809 **
22810 ** Returns:
22811 **
22812 ** 0 Successful transformation. Fits in a 64-bit signed integer.
22813 ** 1 Integer too large for a 64-bit signed integer or is malformed
22814 ** 2 Special case of 9223372036854775808
22815 */
22816 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
22817 #ifndef SQLITE_OMIT_HEX_INTEGER
22818 if( z[0]=='0'
22819 && (z[1]=='x' || z[1]=='X')
22820 && sqlite3Isxdigit(z[2])
22821 ){
22822 u64 u = 0;
22823 int i, k;
22824 for(i=2; z[i]=='0'; i++){}
22825 for(k=i; sqlite3Isxdigit(z[k]); k++){
22826 u = u*16 + sqlite3HexToInt(z[k]);
22827 }
22828 memcpy(pOut, &u, 8);
22829 return (z[k]==0 && k-i<=16) ? 0 : 1;
22830 }else
22831 #endif /* SQLITE_OMIT_HEX_INTEGER */
22832 {
22833 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
22834 }
22835 }
22836
22837 /*
22838 ** If zNum represents an integer that will fit in 32-bits, then set
22839 ** *pValue to that integer and return true. Otherwise return false.
22840 **
22841 ** This routine accepts both decimal and hexadecimal notation for integers.
22842 **
22843 ** Any non-numeric characters that following zNum are ignored.
22844 ** This is different from sqlite3Atoi64() which requires the
22845 ** input number to be zero-terminated.
22846 */
@@ -22528,11 +22852,29 @@
22852 neg = 1;
22853 zNum++;
22854 }else if( zNum[0]=='+' ){
22855 zNum++;
22856 }
22857 #ifndef SQLITE_OMIT_HEX_INTEGER
22858 else if( zNum[0]=='0'
22859 && (zNum[1]=='x' || zNum[1]=='X')
22860 && sqlite3Isxdigit(zNum[2])
22861 ){
22862 u32 u = 0;
22863 zNum += 2;
22864 while( zNum[0]=='0' ) zNum++;
22865 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
22866 u = u*16 + sqlite3HexToInt(zNum[i]);
22867 }
22868 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
22869 memcpy(pValue, &u, 4);
22870 return 1;
22871 }else{
22872 return 0;
22873 }
22874 }
22875 #endif
22876 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
22877 v = v*10 + c;
22878 }
22879
22880 /* The longest decimal representation of a 32 bit integer is 10 digits:
@@ -23604,43 +23946,43 @@
23946 /* 47 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
23947 /* 48 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
23948 /* 49 */ "Count" OpHelp("r[P2]=count()"),
23949 /* 50 */ "ReadCookie" OpHelp(""),
23950 /* 51 */ "SetCookie" OpHelp(""),
23951 /* 52 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
23952 /* 53 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
23953 /* 54 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
23954 /* 55 */ "OpenAutoindex" OpHelp("nColumn=P2"),
23955 /* 56 */ "OpenEphemeral" OpHelp("nColumn=P2"),
23956 /* 57 */ "SorterOpen" OpHelp(""),
23957 /* 58 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
23958 /* 59 */ "Close" OpHelp(""),
23959 /* 60 */ "SeekLT" OpHelp("key=r[P3@P4]"),
23960 /* 61 */ "SeekLE" OpHelp("key=r[P3@P4]"),
23961 /* 62 */ "SeekGE" OpHelp("key=r[P3@P4]"),
23962 /* 63 */ "SeekGT" OpHelp("key=r[P3@P4]"),
23963 /* 64 */ "Seek" OpHelp("intkey=r[P2]"),
23964 /* 65 */ "NoConflict" OpHelp("key=r[P3@P4]"),
23965 /* 66 */ "NotFound" OpHelp("key=r[P3@P4]"),
23966 /* 67 */ "Found" OpHelp("key=r[P3@P4]"),
23967 /* 68 */ "NotExists" OpHelp("intkey=r[P3]"),
23968 /* 69 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
23969 /* 70 */ "NewRowid" OpHelp("r[P2]=rowid"),
23970 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
23971 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
23972 /* 73 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
23973 /* 74 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
23974 /* 75 */ "Delete" OpHelp(""),
23975 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
23976 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
23977 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
23978 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
23979 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
23980 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
23981 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
23982 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
23983 /* 84 */ "ResetCount" OpHelp(""),
23984 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
23985 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
23986 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
23987 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
23988 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -23647,73 +23989,74 @@
23989 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
23990 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
23991 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
23992 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
23993 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
23994 /* 95 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
23995 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
23996 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
23997 /* 98 */ "SorterData" OpHelp("r[P2]=data"),
23998 /* 99 */ "RowKey" OpHelp("r[P2]=key"),
23999 /* 100 */ "RowData" OpHelp("r[P2]=data"),
24000 /* 101 */ "Rowid" OpHelp("r[P2]=rowid"),
24001 /* 102 */ "NullRow" OpHelp(""),
24002 /* 103 */ "Last" OpHelp(""),
24003 /* 104 */ "SorterSort" OpHelp(""),
24004 /* 105 */ "Sort" OpHelp(""),
24005 /* 106 */ "Rewind" OpHelp(""),
24006 /* 107 */ "SorterInsert" OpHelp(""),
24007 /* 108 */ "IdxInsert" OpHelp("key=r[P2]"),
24008 /* 109 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
24009 /* 110 */ "IdxRowid" OpHelp("r[P2]=rowid"),
24010 /* 111 */ "IdxLE" OpHelp("key=r[P3@P4]"),
24011 /* 112 */ "IdxGT" OpHelp("key=r[P3@P4]"),
24012 /* 113 */ "IdxLT" OpHelp("key=r[P3@P4]"),
24013 /* 114 */ "IdxGE" OpHelp("key=r[P3@P4]"),
24014 /* 115 */ "Destroy" OpHelp(""),
24015 /* 116 */ "Clear" OpHelp(""),
24016 /* 117 */ "ResetSorter" OpHelp(""),
24017 /* 118 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
24018 /* 119 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
24019 /* 120 */ "ParseSchema" OpHelp(""),
24020 /* 121 */ "LoadAnalysis" OpHelp(""),
24021 /* 122 */ "DropTable" OpHelp(""),
24022 /* 123 */ "DropIndex" OpHelp(""),
24023 /* 124 */ "DropTrigger" OpHelp(""),
24024 /* 125 */ "IntegrityCk" OpHelp(""),
24025 /* 126 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
24026 /* 127 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
24027 /* 128 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
24028 /* 129 */ "Program" OpHelp(""),
24029 /* 130 */ "Param" OpHelp(""),
24030 /* 131 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
24031 /* 132 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
24032 /* 133 */ "Real" OpHelp("r[P2]=P4"),
24033 /* 134 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
24034 /* 135 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
24035 /* 136 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
24036 /* 137 */ "IfZero" OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
24037 /* 138 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
24038 /* 139 */ "IncrVacuum" OpHelp(""),
24039 /* 140 */ "Expire" OpHelp(""),
24040 /* 141 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
24041 /* 142 */ "VBegin" OpHelp(""),
24042 /* 143 */ "ToText" OpHelp(""),
24043 /* 144 */ "ToBlob" OpHelp(""),
24044 /* 145 */ "ToNumeric" OpHelp(""),
24045 /* 146 */ "ToInt" OpHelp(""),
24046 /* 147 */ "ToReal" OpHelp(""),
24047 /* 148 */ "VCreate" OpHelp(""),
24048 /* 149 */ "VDestroy" OpHelp(""),
24049 /* 150 */ "VOpen" OpHelp(""),
24050 /* 151 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
24051 /* 152 */ "VNext" OpHelp(""),
24052 /* 153 */ "VRename" OpHelp(""),
24053 /* 154 */ "Pagecount" OpHelp(""),
24054 /* 155 */ "MaxPgcnt" OpHelp(""),
24055 /* 156 */ "Init" OpHelp("Start at P2"),
24056 /* 157 */ "Noop" OpHelp(""),
24057 /* 158 */ "Explain" OpHelp(""),
24058 };
24059 return azName[i];
24060 }
24061 #endif
24062
@@ -23812,15 +24155,14 @@
24155 #include <unistd.h>
24156 /* #include <time.h> */
24157 #include <sys/time.h>
24158 #include <errno.h>
24159 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
24160 # include <sys/mman.h>
24161 #endif
24162
24163 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
 
24164 # include <sys/ioctl.h>
24165 # if OS_VXWORKS
24166 # include <semaphore.h>
24167 # include <limits.h>
24168 # else
@@ -24244,11 +24586,15 @@
24586 ** On some systems, calls to fchown() will trigger a message in a security
24587 ** log if they come from non-root processes. So avoid calling fchown() if
24588 ** we are not running as root.
24589 */
24590 static int posixFchown(int fd, uid_t uid, gid_t gid){
24591 #if OS_VXWORKS
24592 return 0;
24593 #else
24594 return geteuid() ? 0 : fchown(fd,uid,gid);
24595 #endif
24596 }
24597
24598 /* Forward reference */
24599 static int openDirectory(const char*, int*);
24600 static int unixGetpagesize(void);
@@ -24300,11 +24646,11 @@
24646 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
24647
24648 { "read", (sqlite3_syscall_ptr)read, 0 },
24649 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24650
24651 #if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
24652 { "pread", (sqlite3_syscall_ptr)pread, 0 },
24653 #else
24654 { "pread", (sqlite3_syscall_ptr)0, 0 },
24655 #endif
24656 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
@@ -24317,11 +24663,11 @@
24663 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24664
24665 { "write", (sqlite3_syscall_ptr)write, 0 },
24666 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24667
24668 #if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
24669 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
24670 #else
24671 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
24672 #endif
24673 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
@@ -24371,14 +24717,14 @@
24717 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
24718 #else
24719 { "mremap", (sqlite3_syscall_ptr)0, 0 },
24720 #endif
24721 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
 
 
24722 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
24723 #define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
24724
24725 #endif
24726
24727 }; /* End of the overrideable system calls */
24728
24729 /*
24730 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -24687,20 +25033,10 @@
25033 }
25034 /* else fall through */
25035 case EPERM:
25036 return SQLITE_PERM;
25037
 
 
 
 
 
 
 
 
 
 
25038 #if EOPNOTSUPP!=ENOTSUP
25039 case EOPNOTSUPP:
25040 /* something went terribly awry, unless during file system support
25041 * introspection, in which it actually means what it says */
25042 #endif
@@ -25229,13 +25565,17 @@
25565
25566 /*
25567 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
25568 */
25569 static int fileHasMoved(unixFile *pFile){
25570 #if OS_VXWORKS
25571 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
25572 #else
25573 struct stat buf;
25574 return pFile->pInode!=0 &&
25575 (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
25576 #endif
25577 }
25578
25579
25580 /*
25581 ** Check a unixFile that is a database. Verify the following:
@@ -25844,10 +26184,17 @@
26184 osUnlink(pFile->pId->zCanonicalName);
26185 }
26186 vxworksReleaseFileId(pFile->pId);
26187 pFile->pId = 0;
26188 }
26189 #endif
26190 #ifdef SQLITE_UNLINK_AFTER_CLOSE
26191 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
26192 osUnlink(pFile->zPath);
26193 sqlite3_free(*(char**)&pFile->zPath);
26194 pFile->zPath = 0;
26195 }
26196 #endif
26197 OSTRACE(("CLOSE %-3d\n", pFile->h));
26198 OpenCounter(-1);
26199 sqlite3_free(pFile->pUnused);
26200 memset(pFile, 0, sizeof(unixFile));
@@ -26367,11 +26714,10 @@
26714 }
26715
26716 /* Otherwise see if some other process holds it. */
26717 if( !reserved ){
26718 sem_t *pSem = pFile->pInode->pSem;
 
26719
26720 if( sem_trywait(pSem)==-1 ){
26721 int tErrno = errno;
26722 if( EAGAIN != tErrno ){
26723 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
@@ -26420,11 +26766,10 @@
26766 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
26767 ** routine to lower a locking level.
26768 */
26769 static int semLock(sqlite3_file *id, int eFileLock) {
26770 unixFile *pFile = (unixFile*)id;
 
26771 sem_t *pSem = pFile->pInode->pSem;
26772 int rc = SQLITE_OK;
26773
26774 /* if we already have a lock, it is exclusive.
26775 ** Just adjust level and punt on outta here. */
@@ -27883,12 +28228,29 @@
28228 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
28229 }
28230 return rc;
28231 }
28232
28233 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
28234
28235 /*
28236 ** Return the system page size.
28237 **
28238 ** This function should not be called directly by other code in this file.
28239 ** Instead, it should be called via macro osGetpagesize().
28240 */
28241 static int unixGetpagesize(void){
28242 #if defined(_BSD_SOURCE)
28243 return getpagesize();
28244 #else
28245 return (int)sysconf(_SC_PAGESIZE);
28246 #endif
28247 }
28248
28249 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
28250
28251 #ifndef SQLITE_OMIT_WAL
 
28252
28253 /*
28254 ** Object used to represent an shared memory buffer.
28255 **
28256 ** When multiple threads all reference the same wal-index, each thread
@@ -28035,24 +28397,10 @@
28397 #endif
28398
28399 return rc;
28400 }
28401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28402 /*
28403 ** Return the minimum number of 32KB shm regions that should be mapped at
28404 ** a time, assuming that each mapping must be an integer multiple of the
28405 ** current system page-size.
28406 **
@@ -29698,10 +30046,16 @@
30046 }
30047
30048 if( isDelete ){
30049 #if OS_VXWORKS
30050 zPath = zName;
30051 #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
30052 zPath = sqlite3_mprintf("%s", zName);
30053 if( zPath==0 ){
30054 robust_close(p, fd, __LINE__);
30055 return SQLITE_NOMEM;
30056 }
30057 #else
30058 osUnlink(zName);
30059 #endif
30060 }
30061 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -29798,11 +30152,15 @@
30152 ){
30153 int rc = SQLITE_OK;
30154 UNUSED_PARAMETER(NotUsed);
30155 SimulateIOError(return SQLITE_IOERR_DELETE);
30156 if( osUnlink(zPath)==(-1) ){
30157 if( errno==ENOENT
30158 #if OS_VXWORKS
30159 || errno==0x380003
30160 #endif
30161 ){
30162 rc = SQLITE_IOERR_DELETE_NOENT;
30163 }else{
30164 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
30165 }
30166 return rc;
@@ -31695,22 +32053,18 @@
32053 #ifndef NTDDI_WINBLUE
32054 # define NTDDI_WINBLUE 0x06030000
32055 #endif
32056
32057 /*
32058 ** Check to see if the GetVersionEx[AW] functions are deprecated on the
32059 ** target system. GetVersionEx was first deprecated in Win8.1.
 
 
 
 
32060 */
32061 #ifndef SQLITE_WIN32_GETVERSIONEX
32062 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
32063 # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
32064 # else
32065 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
32066 # endif
32067 #endif
32068
32069 /*
32070 ** This constant should already be defined (in the "WinDef.h" SDK file).
@@ -31778,11 +32132,11 @@
32132 /*
32133 ** This macro is used when a local variable is set to a value that is
32134 ** [sometimes] not used by the code (e.g. via conditional compilation).
32135 */
32136 #ifndef UNUSED_VARIABLE_VALUE
32137 # define UNUSED_VARIABLE_VALUE(x) (void)(x)
32138 #endif
32139
32140 /*
32141 ** Returns the character that should be used as the directory separator.
32142 */
@@ -31827,11 +32181,11 @@
32181
32182 /*
32183 ** Some Microsoft compilers lack this definition.
32184 */
32185 #ifndef INVALID_FILE_ATTRIBUTES
32186 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
32187 #endif
32188
32189 #ifndef FILE_FLAG_MASK
32190 # define FILE_FLAG_MASK (0xFF3C0000)
32191 #endif
@@ -31877,11 +32231,11 @@
32231 #endif
32232 const char *zPath; /* Full pathname of this file */
32233 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
32234 #if SQLITE_OS_WINCE
32235 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
32236 HANDLE hMutex; /* Mutex used to control access to shared lock */
32237 HANDLE hShared; /* Shared memory segment used for locking */
32238 winceLock local; /* Locks obtained by this instance of winFile */
32239 winceLock *shared; /* Global shared lock memory for the file */
32240 #endif
32241 #if SQLITE_MAX_MMAP_SIZE>0
@@ -32037,14 +32391,13 @@
32391 **
32392 ** In order to facilitate testing on a WinNT system, the test fixture
32393 ** can manually set this value to 1 to emulate Win98 behavior.
32394 */
32395 #ifdef SQLITE_TEST
32396 SQLITE_API LONG volatile sqlite3_os_type = 0;
32397 #else
32398 static LONG volatile sqlite3_os_type = 0;
 
32399 #endif
32400
32401 #ifndef SYSCALL
32402 # define SYSCALL sqlite3_syscall_ptr
32403 #endif
@@ -32671,10 +33024,26 @@
33024 #endif
33025
33026 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
33027 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
33028
33029 /*
33030 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
33031 ** is really just a macro that uses a compiler intrinsic (e.g. x64).
33032 ** So do not try to make this is into a redefinable interface.
33033 */
33034 #if defined(InterlockedCompareExchange)
33035 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
33036
33037 #define osInterlockedCompareExchange InterlockedCompareExchange
33038 #else
33039 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
33040
33041 #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
33042 LONG,LONG))aSyscall[76].pCurrent)
33043 #endif /* defined(InterlockedCompareExchange) */
33044
33045 }; /* End of the overrideable system calls */
33046
33047 /*
33048 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
33049 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
@@ -32921,26 +33290,42 @@
33290 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
33291 # define osIsNT() (1)
33292 #elif !defined(SQLITE_WIN32_HAS_WIDE)
33293 # define osIsNT() (0)
33294 #else
33295 # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
33296 #endif
33297
33298 /*
33299 ** This function determines if the machine is running a version of Windows
33300 ** based on the NT kernel.
33301 */
33302 SQLITE_API int sqlite3_win32_is_nt(void){
33303 #if defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
33304 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
33305 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
33306 defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
33307 OSVERSIONINFOW sInfo;
33308 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
33309 osGetVersionExW(&sInfo);
33310 osInterlockedCompareExchange(&sqlite3_os_type,
33311 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
33312 #elif defined(SQLITE_WIN32_HAS_ANSI)
33313 OSVERSIONINFOA sInfo;
33314 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
33315 osGetVersionExA(&sInfo);
33316 osInterlockedCompareExchange(&sqlite3_os_type,
33317 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
33318 #endif
33319 }
33320 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
33321 #elif SQLITE_TEST
33322 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
33323 #else
33324 return 1;
33325 #endif
33326 }
 
 
 
 
 
 
33327
33328 #ifdef SQLITE_WIN32_MALLOC
33329 /*
33330 ** Allocate nBytes of memory.
33331 */
@@ -33144,11 +33529,11 @@
33529 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
33530 }
33531 #endif /* SQLITE_WIN32_MALLOC */
33532
33533 /*
33534 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
33535 **
33536 ** Space to hold the returned string is obtained from malloc.
33537 */
33538 static LPWSTR winUtf8ToUnicode(const char *zFilename){
33539 int nChar;
@@ -33197,11 +33582,11 @@
33582 }
33583
33584 /*
33585 ** Convert an ANSI string to Microsoft Unicode, based on the
33586 ** current codepage settings for file apis.
33587 **
33588 ** Space to hold the returned string is obtained
33589 ** from sqlite3_malloc.
33590 */
33591 static LPWSTR winMbcsToUnicode(const char *zFilename){
33592 int nByte;
@@ -33271,11 +33656,11 @@
33656 sqlite3_free(zTmpWide);
33657 return zFilenameUtf8;
33658 }
33659
33660 /*
33661 ** Convert UTF-8 to multibyte character string. Space to hold the
33662 ** returned string is obtained from sqlite3_malloc().
33663 */
33664 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
33665 char *zFilenameMbcs;
33666 LPWSTR zTmpWide;
@@ -33411,15 +33796,15 @@
33796 ** This function - winLogErrorAtLine() - is only ever called via the macro
33797 ** winLogError().
33798 **
33799 ** This routine is invoked after an error occurs in an OS function.
33800 ** It logs a message using sqlite3_log() containing the current value of
33801 ** error code and, if possible, the human-readable equivalent from
33802 ** FormatMessage.
33803 **
33804 ** The first argument passed to the macro should be the error code that
33805 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33806 ** The two subsequent arguments should be the name of the OS function that
33807 ** failed and the associated file-system path, if any.
33808 */
33809 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
33810 static int winLogErrorAtLine(
@@ -33446,11 +33831,11 @@
33831 return errcode;
33832 }
33833
33834 /*
33835 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
33836 ** will be retried following a locking error - probably caused by
33837 ** antivirus software. Also the initial delay before the first retry.
33838 ** The delay increases linearly with each retry.
33839 */
33840 #ifndef SQLITE_WIN32_IOERR_RETRY
33841 # define SQLITE_WIN32_IOERR_RETRY 10
@@ -33521,11 +33906,11 @@
33906 /*
33907 ** Log a I/O error retry episode.
33908 */
33909 static void winLogIoerr(int nRetry){
33910 if( nRetry ){
33911 sqlite3_log(SQLITE_IOERR,
33912 "delayed %dms for lock/sharing conflict",
33913 winIoerrRetryDelay*nRetry*(nRetry+1)/2
33914 );
33915 }
33916 }
@@ -33615,21 +34000,21 @@
34000 "winceCreateLock1", zFilename);
34001 }
34002
34003 /* Acquire the mutex before continuing */
34004 winceMutexAcquire(pFile->hMutex);
34005
34006 /* Since the names of named mutexes, semaphores, file mappings etc are
34007 ** case-sensitive, take advantage of that by uppercasing the mutex name
34008 ** and using that as the shared filemapping name.
34009 */
34010 osCharUpperW(zName);
34011 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
34012 PAGE_READWRITE, 0, sizeof(winceLock),
34013 zName);
34014
34015 /* Set a flag that indicates we're the first to create the memory so it
34016 ** must be zero-initialized */
34017 lastErrno = osGetLastError();
34018 if (lastErrno == ERROR_ALREADY_EXISTS){
34019 bInit = FALSE;
34020 }
@@ -33636,11 +34021,11 @@
34021
34022 sqlite3_free(zName);
34023
34024 /* If we succeeded in making the shared memory handle, map it. */
34025 if( pFile->hShared ){
34026 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
34027 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
34028 /* If mapping failed, close the shared memory handle and erase it */
34029 if( !pFile->shared ){
34030 pFile->lastErrno = osGetLastError();
34031 winLogError(SQLITE_IOERR, pFile->lastErrno,
@@ -33662,11 +34047,11 @@
34047 winceMutexRelease(pFile->hMutex);
34048 osCloseHandle(pFile->hMutex);
34049 pFile->hMutex = NULL;
34050 return SQLITE_IOERR;
34051 }
34052
34053 /* Initialize the shared memory if we're supposed to */
34054 if( bInit ){
34055 memset(pFile->shared, 0, sizeof(winceLock));
34056 }
34057
@@ -33700,17 +34085,17 @@
34085 /* De-reference and close our copy of the shared memory handle */
34086 osUnmapViewOfFile(pFile->shared);
34087 osCloseHandle(pFile->hShared);
34088
34089 /* Done with the mutex */
34090 winceMutexRelease(pFile->hMutex);
34091 osCloseHandle(pFile->hMutex);
34092 pFile->hMutex = NULL;
34093 }
34094 }
34095
34096 /*
34097 ** An implementation of the LockFile() API of Windows for CE
34098 */
34099 static BOOL winceLockFile(
34100 LPHANDLE phFile,
34101 DWORD dwFileOffsetLow,
@@ -33917,12 +34302,12 @@
34302 #ifndef INVALID_SET_FILE_POINTER
34303 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
34304 #endif
34305
34306 /*
34307 ** Move the current position of the file handle passed as the first
34308 ** argument to offset iOffset within the file. If successful, return 0.
34309 ** Otherwise, set pFile->lastErrno and return non-zero.
34310 */
34311 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
34312 #if !SQLITE_OS_WINRT
34313 LONG upperBits; /* Most sig. 32 bits of new offset */
@@ -33933,15 +34318,15 @@
34318 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
34319
34320 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
34321 lowerBits = (LONG)(iOffset & 0xffffffff);
34322
34323 /* API oddity: If successful, SetFilePointer() returns a dword
34324 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
34325 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
34326 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
34327 ** whether an error has actually occurred, it is also necessary to call
34328 ** GetLastError().
34329 */
34330 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
34331
34332 if( (dwRet==INVALID_SET_FILE_POINTER
@@ -34020,11 +34405,11 @@
34405 winceDestroyLock(pFile);
34406 if( pFile->zDeleteOnClose ){
34407 int cnt = 0;
34408 while(
34409 osDeleteFileW(pFile->zDeleteOnClose)==0
34410 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
34411 && cnt++ < WINCE_DELETION_ATTEMPTS
34412 ){
34413 sqlite3_win32_sleep(100); /* Wait a little before trying again */
34414 }
34415 sqlite3_free(pFile->zDeleteOnClose);
@@ -34868,11 +35253,11 @@
35253 winFile *p = (winFile*)id;
35254 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
35255 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
35256 }
35257
35258 /*
35259 ** Windows will only let you create file view mappings
35260 ** on allocation size granularity boundaries.
35261 ** During sqlite3_os_init() we do a GetSystemInfo()
35262 ** to get the granularity size.
35263 */
@@ -34880,15 +35265,15 @@
35265
35266 #ifndef SQLITE_OMIT_WAL
35267
35268 /*
35269 ** Helper functions to obtain and relinquish the global mutex. The
35270 ** global mutex is used to protect the winLockInfo objects used by
35271 ** this file, all of which may be shared by multiple threads.
35272 **
35273 ** Function winShmMutexHeld() is used to assert() that the global mutex
35274 ** is held when required. This function is only used as part of assert()
35275 ** statements. e.g.
35276 **
35277 ** winShmEnterMutex()
35278 ** assert( winShmMutexHeld() );
35279 ** winShmLeaveMutex()
@@ -34914,14 +35299,14 @@
35299 **
35300 ** winShmMutexHeld() must be true when creating or destroying
35301 ** this object or while reading or writing the following fields:
35302 **
35303 ** nRef
35304 ** pNext
35305 **
35306 ** The following fields are read-only after the object is created:
35307 **
35308 ** fid
35309 ** zFilename
35310 **
35311 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
35312 ** winShmMutexHeld() is true when reading or writing any other field
@@ -35013,11 +35398,11 @@
35398 /* Initialize the locking parameters */
35399 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
35400 if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
35401 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
35402 }
35403
35404 if( rc!= 0 ){
35405 rc = SQLITE_OK;
35406 }else{
35407 pFile->lastErrno = osGetLastError();
35408 rc = SQLITE_BUSY;
@@ -35109,11 +35494,11 @@
35494 sqlite3_free(p);
35495 return SQLITE_IOERR_NOMEM;
35496 }
35497 pNew->zFilename = (char*)&pNew[1];
35498 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
35499 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
35500
35501 /* Look to see if there is an existing winShmNode that can be used.
35502 ** If no matching winShmNode currently exists, create a new one.
35503 */
35504 winShmEnterMutex();
@@ -35146,11 +35531,11 @@
35531 if( SQLITE_OK!=rc ){
35532 goto shm_open_err;
35533 }
35534
35535 /* Check to see if another process is holding the dead-man switch.
35536 ** If not, truncate the file to zero length.
35537 */
35538 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
35539 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
35540 if( rc!=SQLITE_OK ){
35541 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
@@ -35175,11 +35560,11 @@
35560
35561 /* The reference count on pShmNode has already been incremented under
35562 ** the cover of the winShmEnterMutex() mutex and the pointer from the
35563 ** new (struct winShm) object to the pShmNode has been set. All that is
35564 ** left to do is to link the new object into the linked list starting
35565 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
35566 ** mutex.
35567 */
35568 sqlite3_mutex_enter(pShmNode->mutex);
35569 p->pNext = pShmNode->pFirst;
35570 pShmNode->pFirst = p;
@@ -35195,11 +35580,11 @@
35580 winShmLeaveMutex();
35581 return rc;
35582 }
35583
35584 /*
35585 ** Close a connection to shared-memory. Delete the underlying
35586 ** storage if deleteFlag is true.
35587 */
35588 static int winShmUnmap(
35589 sqlite3_file *fd, /* Database holding shared memory */
35590 int deleteFlag /* Delete after closing if true */
@@ -35284,11 +35669,11 @@
35669
35670 /* Undo the local locks */
35671 if( rc==SQLITE_OK ){
35672 p->exclMask &= ~mask;
35673 p->sharedMask &= ~mask;
35674 }
35675 }else if( flags & SQLITE_SHM_SHARED ){
35676 u16 allShared = 0; /* Union of locks held by connections other than "p" */
35677
35678 /* Find out which shared locks are already held by sibling connections.
35679 ** If any sibling already holds an exclusive lock, go ahead and return
@@ -35323,11 +35708,11 @@
35708 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
35709 rc = SQLITE_BUSY;
35710 break;
35711 }
35712 }
35713
35714 /* Get the exclusive locks at the system level. Then if successful
35715 ** also mark the local connection as being locked.
35716 */
35717 if( rc==SQLITE_OK ){
35718 rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
@@ -35343,11 +35728,11 @@
35728 sqlite3ErrName(rc)));
35729 return rc;
35730 }
35731
35732 /*
35733 ** Implement a memory barrier or memory fence on shared memory.
35734 **
35735 ** All loads and stores begun before the barrier must complete before
35736 ** any load or store begun after the barrier.
35737 */
35738 static void winShmBarrier(
@@ -35358,26 +35743,26 @@
35743 winShmEnterMutex();
35744 winShmLeaveMutex();
35745 }
35746
35747 /*
35748 ** This function is called to obtain a pointer to region iRegion of the
35749 ** shared-memory associated with the database file fd. Shared-memory regions
35750 ** are numbered starting from zero. Each shared-memory region is szRegion
35751 ** bytes in size.
35752 **
35753 ** If an error occurs, an error code is returned and *pp is set to NULL.
35754 **
35755 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
35756 ** region has not been allocated (by any client, including one running in a
35757 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
35758 ** isWrite is non-zero and the requested shared-memory region has not yet
35759 ** been allocated, it is allocated by this function.
35760 **
35761 ** If the shared-memory region has already been allocated or is allocated by
35762 ** this call as described above, then it is mapped into this processes
35763 ** address space (if it is not already), *pp is set to point to the mapped
35764 ** memory and SQLITE_OK returned.
35765 */
35766 static int winShmMap(
35767 sqlite3_file *fd, /* Handle open on database file */
35768 int iRegion, /* Region to retrieve */
@@ -35445,21 +35830,21 @@
35830 pShmNode->aRegion = apNew;
35831
35832 while( pShmNode->nRegion<=iRegion ){
35833 HANDLE hMap = NULL; /* file-mapping handle */
35834 void *pMap = 0; /* Mapped memory region */
35835
35836 #if SQLITE_OS_WINRT
35837 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
35838 NULL, PAGE_READWRITE, nByte, NULL
35839 );
35840 #elif defined(SQLITE_WIN32_HAS_WIDE)
35841 hMap = osCreateFileMappingW(pShmNode->hFile.h,
35842 NULL, PAGE_READWRITE, 0, nByte, NULL
35843 );
35844 #elif defined(SQLITE_WIN32_HAS_ANSI)
35845 hMap = osCreateFileMappingA(pShmNode->hFile.h,
35846 NULL, PAGE_READWRITE, 0, nByte, NULL
35847 );
35848 #endif
35849 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
35850 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
@@ -35552,18 +35937,18 @@
35937 return SQLITE_OK;
35938 }
35939
35940 /*
35941 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35942 ** is already mapped, the existing mapping is replaced by the new). Or, if
35943 ** there already exists a mapping for this file, and there are still
35944 ** outstanding xFetch() references to it, this function is a no-op.
35945 **
35946 ** If parameter nByte is non-negative, then it is the requested size of
35947 ** the mapping to create. Otherwise, if nByte is less than zero, then the
35948 ** requested size is the size of the file on disk. The actual size of the
35949 ** created mapping is either the requested size or the value configured
35950 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
35951 **
35952 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
35953 ** recreated as a result of outstanding references) or an SQLite error
35954 ** code otherwise.
@@ -35588,11 +35973,11 @@
35973 }
35974 if( nMap>pFd->mmapSizeMax ){
35975 nMap = pFd->mmapSizeMax;
35976 }
35977 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
35978
35979 if( nMap==0 && pFd->mmapSize>0 ){
35980 winUnmapfile(pFd);
35981 }
35982 if( nMap!=pFd->mmapSize ){
35983 void *pNew = 0;
@@ -35660,11 +36045,11 @@
36045 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
36046 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
36047 ** Finally, if an error does occur, return an SQLite error code. The final
36048 ** value of *pp is undefined in this case.
36049 **
36050 ** If this function does return a pointer, the caller must eventually
36051 ** release the reference by calling winUnfetch().
36052 */
36053 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
36054 #if SQLITE_MAX_MMAP_SIZE>0
36055 winFile *pFd = (winFile*)fd; /* The underlying database file */
@@ -35695,24 +36080,24 @@
36080 osGetCurrentProcessId(), fd, pp, *pp));
36081 return SQLITE_OK;
36082 }
36083
36084 /*
36085 ** If the third argument is non-NULL, then this function releases a
36086 ** reference obtained by an earlier call to winFetch(). The second
36087 ** argument passed to this function must be the same as the corresponding
36088 ** argument that was passed to the winFetch() invocation.
36089 **
36090 ** Or, if the third argument is NULL, then this function is being called
36091 ** to inform the VFS layer that, according to POSIX, any existing mapping
36092 ** may now be invalid and should be unmapped.
36093 */
36094 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
36095 #if SQLITE_MAX_MMAP_SIZE>0
36096 winFile *pFd = (winFile*)fd; /* The underlying database file */
36097
36098 /* If p==0 (unmap the entire file) then there must be no outstanding
36099 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
36100 ** then there must be at least one outstanding. */
36101 assert( (p==0)==(pFd->nFetchOut==0) );
36102
36103 /* If p!=0, it must match the iOff value. */
@@ -35854,11 +36239,11 @@
36239 int nMax, nBuf, nDir, nLen;
36240 char *zBuf;
36241
36242 /* It's odd to simulate an io-error here, but really this is just
36243 ** using the io-error infrastructure to test that SQLite handles this
36244 ** function failing.
36245 */
36246 SimulateIOError( return SQLITE_IOERR );
36247
36248 /* Allocate a temporary buffer to store the fully qualified file
36249 ** name for the temporary file. If this fails, we cannot continue.
@@ -36036,11 +36421,11 @@
36421 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
36422 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
36423 }
36424
36425 /*
36426 ** Check that the output buffer is large enough for the temporary file
36427 ** name in the following format:
36428 **
36429 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
36430 **
36431 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
@@ -36139,42 +36524,42 @@
36524 int isReadonly = (flags & SQLITE_OPEN_READONLY);
36525 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
36526
36527 #ifndef NDEBUG
36528 int isOpenJournal = (isCreate && (
36529 eType==SQLITE_OPEN_MASTER_JOURNAL
36530 || eType==SQLITE_OPEN_MAIN_JOURNAL
36531 || eType==SQLITE_OPEN_WAL
36532 ));
36533 #endif
36534
36535 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
36536 zUtf8Name, id, flags, pOutFlags));
36537
36538 /* Check the following statements are true:
36539 **
36540 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
36541 ** (b) if CREATE is set, then READWRITE must also be set, and
36542 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
36543 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
36544 */
36545 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
36546 assert(isCreate==0 || isReadWrite);
36547 assert(isExclusive==0 || isCreate);
36548 assert(isDelete==0 || isCreate);
36549
36550 /* The main DB, main journal, WAL file and master journal are never
36551 ** automatically deleted. Nor are they ever temporary files. */
36552 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
36553 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
36554 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
36555 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
36556
36557 /* Assert that the upper layer has set one of the "file-type" flags. */
36558 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
36559 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
36560 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
36561 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
36562 );
36563
36564 assert( pFile!=0 );
36565 memset(pFile, 0, sizeof(winFile));
@@ -36185,12 +36570,12 @@
36570 sqlite3_log(SQLITE_ERROR,
36571 "sqlite3_temp_directory variable should be set for WinRT");
36572 }
36573 #endif
36574
36575 /* If the second argument to this function is NULL, generate a
36576 ** temporary file name to use
36577 */
36578 if( !zUtf8Name ){
36579 assert( isDelete && !isOpenJournal );
36580 rc = winGetTempname(pVfs, &zTmpname);
36581 if( rc!=SQLITE_OK ){
@@ -36226,12 +36611,12 @@
36611 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
36612 }else{
36613 dwDesiredAccess = GENERIC_READ;
36614 }
36615
36616 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
36617 ** created. SQLite doesn't use it to indicate "exclusive access"
36618 ** as it is usually understood.
36619 */
36620 if( isExclusive ){
36621 /* Creates a new file, only if it does not already exist. */
36622 /* If the file exists, it fails. */
@@ -36316,11 +36701,11 @@
36701 pFile->lastErrno = lastErrno;
36702 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
36703 sqlite3_free(zConverted);
36704 sqlite3_free(zTmpname);
36705 if( isReadWrite && !isExclusive ){
36706 return winOpen(pVfs, zName, id,
36707 ((flags|SQLITE_OPEN_READONLY) &
36708 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
36709 pOutFlags);
36710 }else{
36711 return SQLITE_CANTOPEN_BKPT;
@@ -36525,18 +36910,18 @@
36910 if( osIsNT() ){
36911 int cnt = 0;
36912 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36913 memset(&sAttrData, 0, sizeof(sAttrData));
36914 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
36915 GetFileExInfoStandard,
36916 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
36917 if( rc ){
36918 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
36919 ** as if it does not exist.
36920 */
36921 if( flags==SQLITE_ACCESS_EXISTS
36922 && sAttrData.nFileSizeHigh==0
36923 && sAttrData.nFileSizeLow==0 ){
36924 attr = INVALID_FILE_ATTRIBUTES;
36925 }else{
36926 attr = sAttrData.dwFileAttributes;
36927 }
@@ -36631,11 +37016,11 @@
37016 sqlite3_vfs *pVfs, /* Pointer to vfs object */
37017 const char *zRelative, /* Possibly relative input path */
37018 int nFull, /* Size of output buffer in bytes */
37019 char *zFull /* Output buffer */
37020 ){
37021
37022 #if defined(__CYGWIN__)
37023 SimulateIOError( return SQLITE_ERROR );
37024 UNUSED_PARAMETER(nFull);
37025 assert( nFull>=pVfs->mxPathname );
37026 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
@@ -36944,24 +37329,24 @@
37329 ** the current time and date as a Julian Day number times 86_400_000. In
37330 ** other words, write into *piNow the number of milliseconds since the Julian
37331 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
37332 ** proleptic Gregorian calendar.
37333 **
37334 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
37335 ** cannot be found.
37336 */
37337 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
37338 /* FILETIME structure is a 64-bit value representing the number of
37339 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
37340 */
37341 FILETIME ft;
37342 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
37343 #ifdef SQLITE_TEST
37344 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
37345 #endif
37346 /* 2^32 - to avoid use of LL and warnings in gcc */
37347 static const sqlite3_int64 max32BitValue =
37348 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
37349 (sqlite3_int64)294967296;
37350
37351 #if SQLITE_OS_WINCE
37352 SYSTEMTIME time;
@@ -36973,11 +37358,11 @@
37358 #else
37359 osGetSystemTimeAsFileTime( &ft );
37360 #endif
37361
37362 *piNow = winFiletimeEpoch +
37363 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
37364 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
37365
37366 #ifdef SQLITE_TEST
37367 if( sqlite3_current_time ){
37368 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
@@ -37092,11 +37477,11 @@
37477 };
37478 #endif
37479
37480 /* Double-check that the aSyscall[] array has been constructed
37481 ** correctly. See ticket [bb3a86e890c8e96ab] */
37482 assert( ArraySize(aSyscall)==77 );
37483
37484 /* get memory map allocation granularity */
37485 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
37486 #if SQLITE_OS_WINRT
37487 osGetNativeSystemInfo(&winSysInfo);
@@ -37110,14 +37495,14 @@
37495
37496 #if defined(SQLITE_WIN32_HAS_WIDE)
37497 sqlite3_vfs_register(&winLongPathVfs, 0);
37498 #endif
37499
37500 return SQLITE_OK;
37501 }
37502
37503 SQLITE_API int sqlite3_os_end(void){
37504 #if SQLITE_OS_WINRT
37505 if( sleepObj!=NULL ){
37506 osCloseHandle(sleepObj);
37507 sleepObj = NULL;
37508 }
@@ -49189,20 +49574,20 @@
49574 **
49575 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
49576 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
49577 ** is more of a scheduler yield than an actual delay. But on the 10th
49578 ** an subsequent retries, the delays start becoming longer and longer,
49579 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
49580 ** The total delay time before giving up is less than 10 seconds.
49581 */
49582 if( cnt>5 ){
49583 int nDelay = 1; /* Pause time in microseconds */
49584 if( cnt>100 ){
49585 VVA_ONLY( pWal->lockError = 1; )
49586 return SQLITE_PROTOCOL;
49587 }
49588 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
49589 sqlite3OsSleep(pWal->pVfs, nDelay);
49590 }
49591
49592 if( !useWal ){
49593 rc = walIndexReadHdr(pWal, pChanged);
@@ -51314,11 +51699,11 @@
51699 /* If the client is reading or writing an index and the schema is
51700 ** not loaded, then it is too difficult to actually check to see if
51701 ** the correct locks are held. So do not bother - just return true.
51702 ** This case does not come up very often anyhow.
51703 */
51704 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
51705 return 1;
51706 }
51707
51708 /* Figure out the root-page that the lock should be held on. For table
51709 ** b-trees, this is just the root page of the b-tree being read or
@@ -52784,11 +53169,11 @@
53169 return pBt->nPage;
53170 }
53171 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
53172 assert( sqlite3BtreeHoldsMutex(p) );
53173 assert( ((p->pBt->nPage)&0x8000000)==0 );
53174 return btreePagecount(p->pBt);
53175 }
53176
53177 /*
53178 ** Get a page from the pager and initialize it. This routine is just a
53179 ** convenience wrapper around separate calls to btreeGetPage() and
@@ -56893,11 +57278,12 @@
57278 int nSkip = (iChild ? 4 : 0);
57279
57280 if( *pRC ) return;
57281
57282 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
57283 assert( MX_CELL(pPage->pBt)<=10921 );
57284 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
57285 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
57286 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
57287 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57288 /* The cell should normally be sized correctly. However, when moving a
57289 ** malformed cell from a leaf page to an interior page, if the cell size
@@ -61582,10 +61968,72 @@
61968 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
61969 for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
61970 sqlite3FuncDefInsert(pHash, &aFunc[i]);
61971 }
61972 }
61973
61974 /*
61975 ** Attempt to extract a value from pExpr and use it to construct *ppVal.
61976 **
61977 ** If pAlloc is not NULL, then an UnpackedRecord object is created for
61978 ** pAlloc if one does not exist and the new value is added to the
61979 ** UnpackedRecord object.
61980 **
61981 ** A value is extracted in the following cases:
61982 **
61983 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
61984 **
61985 ** * The expression is a bound variable, and this is a reprepare, or
61986 **
61987 ** * The expression is a literal value.
61988 **
61989 ** On success, *ppVal is made to point to the extracted value. The caller
61990 ** is responsible for ensuring that the value is eventually freed.
61991 */
61992 static int stat4ValueFromExpr(
61993 Parse *pParse, /* Parse context */
61994 Expr *pExpr, /* The expression to extract a value from */
61995 u8 affinity, /* Affinity to use */
61996 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
61997 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
61998 ){
61999 int rc = SQLITE_OK;
62000 sqlite3_value *pVal = 0;
62001 sqlite3 *db = pParse->db;
62002
62003 /* Skip over any TK_COLLATE nodes */
62004 pExpr = sqlite3ExprSkipCollate(pExpr);
62005
62006 if( !pExpr ){
62007 pVal = valueNew(db, pAlloc);
62008 if( pVal ){
62009 sqlite3VdbeMemSetNull((Mem*)pVal);
62010 }
62011 }else if( pExpr->op==TK_VARIABLE
62012 || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
62013 ){
62014 Vdbe *v;
62015 int iBindVar = pExpr->iColumn;
62016 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
62017 if( (v = pParse->pReprepare)!=0 ){
62018 pVal = valueNew(db, pAlloc);
62019 if( pVal ){
62020 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
62021 if( rc==SQLITE_OK ){
62022 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
62023 }
62024 pVal->db = pParse->db;
62025 }
62026 }
62027 }else{
62028 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
62029 }
62030
62031 assert( pVal==0 || pVal->db==db );
62032 *ppVal = pVal;
62033 return rc;
62034 }
62035
62036 /*
62037 ** This function is used to allocate and populate UnpackedRecord
62038 ** structures intended to be compared against sample index keys stored
62039 ** in the sqlite_stat4 table.
@@ -61622,52 +62070,90 @@
62070 Expr *pExpr, /* The expression to extract a value from */
62071 u8 affinity, /* Affinity to use */
62072 int iVal, /* Array element to populate */
62073 int *pbOk /* OUT: True if value was extracted */
62074 ){
62075 int rc;
62076 sqlite3_value *pVal = 0;
 
 
 
62077 struct ValueNewStat4Ctx alloc;
62078
62079 alloc.pParse = pParse;
62080 alloc.pIdx = pIdx;
62081 alloc.ppRec = ppRec;
62082 alloc.iVal = iVal;
62083
62084 rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
62085 assert( pVal==0 || pVal->db==pParse->db );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62086 *pbOk = (pVal!=0);
62087 return rc;
62088 }
62089
62090 /*
62091 ** Attempt to extract a value from expression pExpr using the methods
62092 ** as described for sqlite3Stat4ProbeSetValue() above.
62093 **
62094 ** If successful, set *ppVal to point to a new value object and return
62095 ** SQLITE_OK. If no value can be extracted, but no other error occurs
62096 ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
62097 ** does occur, return an SQLite error code. The final value of *ppVal
62098 ** is undefined in this case.
62099 */
62100 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
62101 Parse *pParse, /* Parse context */
62102 Expr *pExpr, /* The expression to extract a value from */
62103 u8 affinity, /* Affinity to use */
62104 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
62105 ){
62106 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
62107 }
62108
62109 /*
62110 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
62111 ** the column value into *ppVal. If *ppVal is initially NULL then a new
62112 ** sqlite3_value object is allocated.
62113 **
62114 ** If *ppVal is initially NULL then the caller is responsible for
62115 ** ensuring that the value written into *ppVal is eventually freed.
62116 */
62117 SQLITE_PRIVATE int sqlite3Stat4Column(
62118 sqlite3 *db, /* Database handle */
62119 const void *pRec, /* Pointer to buffer containing record */
62120 int nRec, /* Size of buffer pRec in bytes */
62121 int iCol, /* Column to extract */
62122 sqlite3_value **ppVal /* OUT: Extracted value */
62123 ){
62124 u32 t; /* a column type code */
62125 int nHdr; /* Size of the header in the record */
62126 int iHdr; /* Next unread header byte */
62127 int iField; /* Next unread data byte */
62128 int szField; /* Size of the current data field */
62129 int i; /* Column index */
62130 u8 *a = (u8*)pRec; /* Typecast byte array */
62131 Mem *pMem = *ppVal; /* Write result into this Mem object */
62132
62133 assert( iCol>0 );
62134 iHdr = getVarint32(a, nHdr);
62135 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
62136 iField = nHdr;
62137 for(i=0; i<=iCol; i++){
62138 iHdr += getVarint32(&a[iHdr], t);
62139 testcase( iHdr==nHdr );
62140 testcase( iHdr==nHdr+1 );
62141 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
62142 szField = sqlite3VdbeSerialTypeLen(t);
62143 iField += szField;
62144 }
62145 testcase( iField==nRec );
62146 testcase( iField==nRec+1 );
62147 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
62148 if( pMem==0 ){
62149 pMem = *ppVal = sqlite3ValueNew(db);
62150 if( pMem==0 ) return SQLITE_NOMEM;
62151 }
62152 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
62153 pMem->enc = ENC(db);
62154 return SQLITE_OK;
62155 }
62156
62157 /*
62158 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
62159 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
@@ -61810,22 +62296,39 @@
62296 pB->zSql = zTmp;
62297 pB->isPrepareV2 = pA->isPrepareV2;
62298 }
62299
62300 /*
62301 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
62302 ** than its current size. nOp is guaranteed to be less than or equal
62303 ** to 1024/sizeof(Op).
62304 **
62305 ** If an out-of-memory error occurs while resizing the array, return
62306 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
62307 ** unchanged (this is so that any opcodes already allocated can be
62308 ** correctly deallocated along with the rest of the Vdbe).
62309 */
62310 static int growOpArray(Vdbe *v, int nOp){
62311 VdbeOp *pNew;
62312 Parse *p = v->pParse;
62313
62314 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
62315 ** more frequent reallocs and hence provide more opportunities for
62316 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
62317 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
62318 ** by the minimum* amount required until the size reaches 512. Normal
62319 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
62320 ** size of the op array or add 1KB of space, whichever is smaller. */
62321 #ifdef SQLITE_TEST_REALLOC_STRESS
62322 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
62323 #else
62324 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
62325 UNUSED_PARAMETER(nOp);
62326 #endif
62327
62328 assert( nOp<=(1024/sizeof(Op)) );
62329 assert( nNew>=(p->nOpAlloc+nOp) );
62330 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
62331 if( pNew ){
62332 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
62333 v->aOp = pNew;
62334 }
@@ -61865,11 +62368,11 @@
62368
62369 i = p->nOp;
62370 assert( p->magic==VDBE_MAGIC_INIT );
62371 assert( op>0 && op<0xff );
62372 if( p->pParse->nOpAlloc<=i ){
62373 if( growOpArray(p, 1) ){
62374 return 1;
62375 }
62376 }
62377 p->nOp++;
62378 pOp = &p->aOp[i];
@@ -62225,11 +62728,11 @@
62728 }
62729 sqlite3DbFree(p->db, pParse->aLabel);
62730 pParse->aLabel = 0;
62731 pParse->nLabel = 0;
62732 *pMaxFuncArgs = nMaxArgs;
62733 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
62734 }
62735
62736 /*
62737 ** Return the address of the next instruction to be inserted.
62738 */
@@ -62252,11 +62755,11 @@
62755 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
62756 VdbeOp *aOp = p->aOp;
62757 assert( aOp && !p->db->mallocFailed );
62758
62759 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
62760 assert( DbMaskAllZero(p->btreeMask) );
62761
62762 resolveP2Values(p, pnMaxArg);
62763 *pnOp = p->nOp;
62764 p->aOp = 0;
62765 return aOp;
@@ -62267,11 +62770,11 @@
62770 ** address of the first operation added.
62771 */
62772 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
62773 int addr;
62774 assert( p->magic==VDBE_MAGIC_INIT );
62775 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
62776 return 0;
62777 }
62778 addr = p->nOp;
62779 if( ALWAYS(nOp>0) ){
62780 int i;
@@ -62452,11 +62955,11 @@
62955
62956 /*
62957 ** Change the opcode at addr into OP_Noop
62958 */
62959 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
62960 if( addr<p->nOp ){
62961 VdbeOp *pOp = &p->aOp[addr];
62962 sqlite3 *db = p->db;
62963 freeP4(db, pOp->p4type, pOp->p4.p);
62964 memset(pOp, 0, sizeof(pOp[0]));
62965 pOp->opcode = OP_Noop;
@@ -62837,13 +63340,13 @@
63340 ** p->btreeMask of databases that will require a lock.
63341 */
63342 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
63343 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
63344 assert( i<(int)sizeof(p->btreeMask)*8 );
63345 DbMaskSet(p->btreeMask, i);
63346 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
63347 DbMaskSet(p->lockMask, i);
63348 }
63349 }
63350
63351 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
63352 /*
@@ -62867,20 +63370,19 @@
63370 ** this routine is N*N. But as N is rarely more than 1, this should not
63371 ** be a problem.
63372 */
63373 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
63374 int i;
 
63375 sqlite3 *db;
63376 Db *aDb;
63377 int nDb;
63378 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
63379 db = p->db;
63380 aDb = db->aDb;
63381 nDb = db->nDb;
63382 for(i=0; i<nDb; i++){
63383 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
63384 sqlite3BtreeEnter(aDb[i].pBt);
63385 }
63386 }
63387 }
63388 #endif
@@ -62889,20 +63391,19 @@
63391 /*
63392 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
63393 */
63394 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
63395 int i;
 
63396 sqlite3 *db;
63397 Db *aDb;
63398 int nDb;
63399 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
63400 db = p->db;
63401 aDb = db->aDb;
63402 nDb = db->nDb;
63403 for(i=0; i<nDb; i++){
63404 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
63405 sqlite3BtreeLeave(aDb[i].pBt);
63406 }
63407 }
63408 }
63409 #endif
@@ -63869,11 +64370,11 @@
64370 int cnt = 0;
64371 int nWrite = 0;
64372 int nRead = 0;
64373 p = db->pVdbe;
64374 while( p ){
64375 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
64376 cnt++;
64377 if( p->readOnly==0 ) nWrite++;
64378 if( p->bIsReader ) nRead++;
64379 }
64380 p = p->pNext;
@@ -64029,11 +64530,10 @@
64530 /* Lock all btrees used by the statement */
64531 sqlite3VdbeEnter(p);
64532
64533 /* Check for one of the special errors */
64534 mrc = p->rc & 0xff;
 
64535 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
64536 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
64537 if( isSpecialError ){
64538 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
64539 ** no rollback is necessary. Otherwise, at least a savepoint
@@ -64514,11 +65014,11 @@
65014 /*
65015 ** Return the serial-type for the value stored in pMem.
65016 */
65017 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
65018 int flags = pMem->flags;
65019 u32 n;
65020
65021 if( flags&MEM_Null ){
65022 return 0;
65023 }
65024 if( flags&MEM_Int ){
@@ -64544,15 +65044,15 @@
65044 }
65045 if( flags&MEM_Real ){
65046 return 7;
65047 }
65048 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
65049 assert( pMem->n>=0 );
65050 n = (u32)pMem->n;
65051 if( flags & MEM_Zero ){
65052 n += pMem->u.nZero;
65053 }
 
65054 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
65055 }
65056
65057 /*
65058 ** Return the length of the data corresponding to the supplied serial-type.
@@ -65315,10 +65815,11 @@
65815 /* rc==0 here means that one or both of the keys ran out of fields and
65816 ** all the fields up to that point were equal. Return the the default_rc
65817 ** value. */
65818 assert( CORRUPT_DB
65819 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
65820 || pKeyInfo->db->mallocFailed
65821 );
65822 return pPKey2->default_rc;
65823 }
65824
65825 /*
@@ -65480,10 +65981,11 @@
65981
65982 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65983 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65984 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65985 || CORRUPT_DB
65986 || pPKey2->pKeyInfo->db->mallocFailed
65987 );
65988 return res;
65989 }
65990
65991 /*
@@ -67064,11 +67566,11 @@
67566 /*
67567 ** Return true if the prepared statement is in need of being reset.
67568 */
67569 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
67570 Vdbe *v = (Vdbe*)pStmt;
67571 return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
67572 }
67573
67574 /*
67575 ** Return a pointer to the next prepared statement after pStmt associated
67576 ** with database connection pDb. If pStmt is NULL, return the first
@@ -67514,10 +68016,16 @@
68016 **
68017 ** M is an integer, 2 or 3, that indices how many different ways the
68018 ** branch can go. It is usually 2. "I" is the direction the branch
68019 ** goes. 0 means falls through. 1 means branch is taken. 2 means the
68020 ** second alternative branch is taken.
68021 **
68022 ** iSrcLine is the source code line (from the __LINE__ macro) that
68023 ** generated the VDBE instruction. This instrumentation assumes that all
68024 ** source code is in a single file (the amalgamation). Special values 1
68025 ** and 2 for the iSrcLine parameter mean that this particular branch is
68026 ** always taken or never taken, respectively.
68027 */
68028 #if !defined(SQLITE_VDBE_COVERAGE)
68029 # define VdbeBranchTaken(I,M)
68030 #else
68031 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
@@ -67622,25 +68130,25 @@
68130 ** do so without loss of information. In other words, if the string
68131 ** looks like a number, convert it into a number. If it does not
68132 ** look like a number, leave it alone.
68133 */
68134 static void applyNumericAffinity(Mem *pRec){
68135 double rValue;
68136 i64 iValue;
68137 u8 enc = pRec->enc;
68138 if( (pRec->flags&MEM_Str)==0 ) return;
68139 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
68140 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
68141 pRec->u.i = iValue;
68142 pRec->flags |= MEM_Int;
68143 }else{
68144 pRec->r = rValue;
68145 pRec->flags |= MEM_Real;
68146 }
68147 }
68148 #define ApplyNumericAffinity(X) \
68149 if(((X)->flags&(MEM_Real|MEM_Int))==0){applyNumericAffinity(X);}
68150
68151 /*
68152 ** Processing is determine by the affinity parameter:
68153 **
68154 ** SQLITE_AFF_INTEGER:
@@ -67673,11 +68181,11 @@
68181 }
68182 pRec->flags &= ~(MEM_Real|MEM_Int);
68183 }else if( affinity!=SQLITE_AFF_NONE ){
68184 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
68185 || affinity==SQLITE_AFF_NUMERIC );
68186 ApplyNumericAffinity(pRec);
68187 if( pRec->flags & MEM_Real ){
68188 sqlite3VdbeIntegerAffinity(pRec);
68189 }
68190 }
68191 }
@@ -68254,16 +68762,18 @@
68762 break;
68763 }
68764
68765 /* Opcode: InitCoroutine P1 P2 P3 * *
68766 **
68767 ** Set up register P1 so that it will Yield to the coroutine
68768 ** located at address P3.
68769 **
68770 ** If P2!=0 then the coroutine implementation immediately follows
68771 ** this opcode. So jump over the coroutine implementation to
68772 ** address P2.
68773 **
68774 ** See also: EndCoroutine
68775 */
68776 case OP_InitCoroutine: { /* jump */
68777 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
68778 assert( pOp->p2>=0 && pOp->p2<p->nOp );
68779 assert( pOp->p3>=0 && pOp->p3<p->nOp );
@@ -68275,13 +68785,15 @@
68785 break;
68786 }
68787
68788 /* Opcode: EndCoroutine P1 * * * *
68789 **
68790 ** The instruction at the address in register P1 is a Yield.
68791 ** Jump to the P2 parameter of that Yield.
68792 ** After the jump, register P1 becomes undefined.
68793 **
68794 ** See also: InitCoroutine
68795 */
68796 case OP_EndCoroutine: { /* in1 */
68797 VdbeOp *pCaller;
68798 pIn1 = &aMem[pOp->p1];
68799 assert( pIn1->flags==MEM_Int );
@@ -68294,15 +68806,20 @@
68806 break;
68807 }
68808
68809 /* Opcode: Yield P1 P2 * * *
68810 **
68811 ** Swap the program counter with the value in register P1. This
68812 ** has the effect of yielding to a coroutine.
68813 **
68814 ** If the coroutine that is launched by this instruction ends with
68815 ** Yield or Return then continue to the next instruction. But if
68816 ** the coroutine launched by this instruction ends with
68817 ** EndCoroutine, then jump to P2 rather than continuing with the
68818 ** next instruction.
68819 **
68820 ** See also: InitCoroutine
68821 */
68822 case OP_Yield: { /* in1, jump */
68823 int pcDest;
68824 pIn1 = &aMem[pOp->p1];
68825 assert( VdbeMemDynamic(pIn1)==0 );
@@ -68461,11 +68978,11 @@
68978
68979 /* Opcode: String8 * P2 * P4 *
68980 ** Synopsis: r[P2]='P4'
68981 **
68982 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
68983 ** into a String before it is executed for the first time. During
68984 ** this transformation, the length of string P4 is computed and stored
68985 ** as the P1 parameter.
68986 */
68987 case OP_String8: { /* same as TK_STRING, out2-prerelease */
68988 assert( pOp->p4.z!=0 );
@@ -69683,14 +70200,18 @@
70200 break;
70201 }
70202
70203 /* Opcode: Once P1 P2 * * *
70204 **
70205 ** Check the "once" flag number P1. If it is set, jump to instruction P2.
70206 ** Otherwise, set the flag and fall through to the next instruction.
70207 ** In other words, this opcode causes all following opcodes up through P2
70208 ** (but not including P2) to run just once and to be skipped on subsequent
70209 ** times through the loop.
70210 **
70211 ** All "once" flags are initially cleared whenever a prepared statement
70212 ** first begins to run.
70213 */
70214 case OP_Once: { /* jump */
70215 assert( pOp->p1<p->nOnceFlag );
70216 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
70217 if( p->aOnceFlag[pOp->p1] ){
@@ -69703,17 +70224,17 @@
70224
70225 /* Opcode: If P1 P2 P3 * *
70226 **
70227 ** Jump to P2 if the value in register P1 is true. The value
70228 ** is considered true if it is numeric and non-zero. If the value
70229 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
70230 */
70231 /* Opcode: IfNot P1 P2 P3 * *
70232 **
70233 ** Jump to P2 if the value in register P1 is False. The value
70234 ** is considered false if it has a numeric value of zero. If the value
70235 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
70236 */
70237 case OP_If: /* jump, in1 */
70238 case OP_IfNot: { /* jump, in1 */
70239 int c;
70240 pIn1 = &aMem[pOp->p1];
@@ -70521,11 +71042,11 @@
71042 int iGen;
71043
71044 assert( p->bIsReader );
71045 assert( p->readOnly==0 || pOp->p2==0 );
71046 assert( pOp->p1>=0 && pOp->p1<db->nDb );
71047 assert( DbMaskTest(p->btreeMask, pOp->p1) );
71048 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
71049 rc = SQLITE_READONLY;
71050 goto abort_due_to_error;
71051 }
71052 pBt = db->aDb[pOp->p1].pBt;
@@ -70616,11 +71137,11 @@
71137 iDb = pOp->p1;
71138 iCookie = pOp->p3;
71139 assert( pOp->p3<SQLITE_N_BTREE_META );
71140 assert( iDb>=0 && iDb<db->nDb );
71141 assert( db->aDb[iDb].pBt!=0 );
71142 assert( DbMaskTest(p->btreeMask, iDb) );
71143
71144 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
71145 pOut->u.i = iMeta;
71146 break;
71147 }
@@ -70637,11 +71158,11 @@
71158 */
71159 case OP_SetCookie: { /* in3 */
71160 Db *pDb;
71161 assert( pOp->p2<SQLITE_N_BTREE_META );
71162 assert( pOp->p1>=0 && pOp->p1<db->nDb );
71163 assert( DbMaskTest(p->btreeMask, pOp->p1) );
71164 assert( p->readOnly==0 );
71165 pDb = &db->aDb[pOp->p1];
71166 assert( pDb->pBt!=0 );
71167 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
71168 pIn3 = &aMem[pOp->p3];
@@ -70692,11 +71213,25 @@
71213 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
71214 ** structure, then said structure defines the content and collating
71215 ** sequence of the index being opened. Otherwise, if P4 is an integer
71216 ** value, it is set to the number of columns in the table.
71217 **
71218 ** See also: OpenWrite, ReopenIdx
71219 */
71220 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
71221 ** Synopsis: root=P2 iDb=P3
71222 **
71223 ** The ReopenIdx opcode works exactly like ReadOpen except that it first
71224 ** checks to see if the cursor on P1 is already open with a root page
71225 ** number of P2 and if it is this opcode becomes a no-op. In other words,
71226 ** if the cursor is already open, do not reopen it.
71227 **
71228 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being
71229 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
71230 ** every other ReopenIdx or OpenRead for the same cursor number.
71231 **
71232 ** See the OpenRead opcode documentation for additional information.
71233 */
71234 /* Opcode: OpenWrite P1 P2 P3 P4 P5
71235 ** Synopsis: root=P2 iDb=P3
71236 **
71237 ** Open a read/write cursor named P1 on the table or index whose root
@@ -70714,10 +71249,23 @@
71249 ** in read/write mode. For a given table, there can be one or more read-only
71250 ** cursors or a single read/write cursor but not both.
71251 **
71252 ** See also OpenRead.
71253 */
71254 case OP_ReopenIdx: {
71255 VdbeCursor *pCur;
71256
71257 assert( pOp->p5==0 );
71258 assert( pOp->p4type==P4_KEYINFO );
71259 pCur = p->apCsr[pOp->p1];
71260 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
71261 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
71262 break;
71263 }
71264 /* If the cursor is not currently open or is open on a different
71265 ** index, then fall through into OP_OpenRead to force a reopen */
71266 }
71267 case OP_OpenRead:
71268 case OP_OpenWrite: {
71269 int nField;
71270 KeyInfo *pKeyInfo;
71271 int p2;
@@ -70728,11 +71276,12 @@
71276 Db *pDb;
71277
71278 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
71279 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
71280 assert( p->bIsReader );
71281 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
71282 || p->readOnly==0 );
71283
71284 if( p->expired ){
71285 rc = SQLITE_ABORT;
71286 break;
71287 }
@@ -70740,11 +71289,11 @@
71289 nField = 0;
71290 pKeyInfo = 0;
71291 p2 = pOp->p2;
71292 iDb = pOp->p3;
71293 assert( iDb>=0 && iDb<db->nDb );
71294 assert( DbMaskTest(p->btreeMask, iDb) );
71295 pDb = &db->aDb[iDb];
71296 pX = pDb->pBt;
71297 assert( pX!=0 );
71298 if( pOp->opcode==OP_OpenWrite ){
71299 wrFlag = 1;
@@ -70785,10 +71334,11 @@
71334 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
71335 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
71336 if( pCur==0 ) goto no_mem;
71337 pCur->nullRow = 1;
71338 pCur->isOrdered = 1;
71339 pCur->pgnoRoot = p2;
71340 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
71341 pCur->pKeyInfo = pKeyInfo;
71342 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
71343 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
71344
@@ -70939,11 +71489,11 @@
71489 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
71490 p->apCsr[pOp->p1] = 0;
71491 break;
71492 }
71493
71494 /* Opcode: SeekGE P1 P2 P3 P4 *
71495 ** Synopsis: key=r[P3@P4]
71496 **
71497 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
71498 ** use the value in register P3 as the key. If cursor P1 refers
71499 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70950,14 +71500,18 @@
71500 ** that are used as an unpacked index key.
71501 **
71502 ** Reposition cursor P1 so that it points to the smallest entry that
71503 ** is greater than or equal to the key value. If there are no records
71504 ** greater than or equal to the key and P2 is not zero, then jump to P2.
71505 **
71506 ** This opcode leaves the cursor configured to move in forward order,
71507 ** from the beginning toward the end. In other words, the cursor is
71508 ** configured to use Next, not Prev.
71509 **
71510 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
71511 */
71512 /* Opcode: SeekGT P1 P2 P3 P4 *
71513 ** Synopsis: key=r[P3@P4]
71514 **
71515 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
71516 ** use the value in register P3 as a key. If cursor P1 refers
71517 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70964,14 +71518,18 @@
71518 ** that are used as an unpacked index key.
71519 **
71520 ** Reposition cursor P1 so that it points to the smallest entry that
71521 ** is greater than the key value. If there are no records greater than
71522 ** the key and P2 is not zero, then jump to P2.
71523 **
71524 ** This opcode leaves the cursor configured to move in forward order,
71525 ** from the beginning toward the end. In other words, the cursor is
71526 ** configured to use Next, not Prev.
71527 **
71528 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
71529 */
71530 /* Opcode: SeekLT P1 P2 P3 P4 *
71531 ** Synopsis: key=r[P3@P4]
71532 **
71533 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
71534 ** use the value in register P3 as a key. If cursor P1 refers
71535 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70978,14 +71536,18 @@
71536 ** that are used as an unpacked index key.
71537 **
71538 ** Reposition cursor P1 so that it points to the largest entry that
71539 ** is less than the key value. If there are no records less than
71540 ** the key and P2 is not zero, then jump to P2.
71541 **
71542 ** This opcode leaves the cursor configured to move in reverse order,
71543 ** from the end toward the beginning. In other words, the cursor is
71544 ** configured to use Prev, not Next.
71545 **
71546 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
71547 */
71548 /* Opcode: SeekLE P1 P2 P3 P4 *
71549 ** Synopsis: key=r[P3@P4]
71550 **
71551 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
71552 ** use the value in register P3 as a key. If cursor P1 refers
71553 ** to an SQL index, then P3 is the first in an array of P4 registers
@@ -70992,10 +71554,14 @@
71554 ** that are used as an unpacked index key.
71555 **
71556 ** Reposition cursor P1 so that it points to the largest entry that
71557 ** is less than or equal to the key value. If there are no records
71558 ** less than or equal to the key and P2 is not zero, then jump to P2.
71559 **
71560 ** This opcode leaves the cursor configured to move in reverse order,
71561 ** from the end toward the beginning. In other words, the cursor is
71562 ** configured to use Prev, not Next.
71563 **
71564 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
71565 */
71566 case OP_SeekLT: /* jump, in3 */
71567 case OP_SeekLE: /* jump, in3 */
@@ -71018,16 +71584,19 @@
71584 assert( OP_SeekGT == OP_SeekLT+3 );
71585 assert( pC->isOrdered );
71586 assert( pC->pCursor!=0 );
71587 oc = pOp->opcode;
71588 pC->nullRow = 0;
71589 #ifdef SQLITE_DEBUG
71590 pC->seekOp = pOp->opcode;
71591 #endif
71592 if( pC->isTable ){
71593 /* The input value in P3 might be of any type: integer, real, string,
71594 ** blob, or NULL. But it needs to be an integer before we can do
71595 ** the seek, so covert it. */
71596 pIn3 = &aMem[pOp->p3];
71597 ApplyNumericAffinity(pIn3);
71598 iKey = sqlite3VdbeIntValue(pIn3);
71599 pC->rowidIsValid = 0;
71600
71601 /* If the P3 value could not be converted into an integer without
71602 ** loss of information, then special processing is required... */
@@ -71172,10 +71741,14 @@
71741 ** record.
71742 **
71743 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
71744 ** is a prefix of any entry in P1 then a jump is made to P2 and
71745 ** P1 is left pointing at the matching entry.
71746 **
71747 ** This operation leaves the cursor in a state where it can be
71748 ** advanced in the forward direction. The Next instruction will work,
71749 ** but not the Prev instruction.
71750 **
71751 ** See also: NotFound, NoConflict, NotExists. SeekGe
71752 */
71753 /* Opcode: NotFound P1 P2 P3 P4 *
71754 ** Synopsis: key=r[P3@P4]
@@ -71187,10 +71760,14 @@
71760 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
71761 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
71762 ** does contain an entry whose prefix matches the P3/P4 record then control
71763 ** falls through to the next instruction and P1 is left pointing at the
71764 ** matching entry.
71765 **
71766 ** This operation leaves the cursor in a state where it cannot be
71767 ** advanced in either direction. In other words, the Next and Prev
71768 ** opcodes do not work after this operation.
71769 **
71770 ** See also: Found, NotExists, NoConflict
71771 */
71772 /* Opcode: NoConflict P1 P2 P3 P4 *
71773 ** Synopsis: key=r[P3@P4]
@@ -71206,10 +71783,14 @@
71783 ** immediately to P2. If there is a match, fall through and leave the P1
71784 ** cursor pointing to the matching row.
71785 **
71786 ** This opcode is similar to OP_NotFound with the exceptions that the
71787 ** branch is always taken if any part of the search key input is NULL.
71788 **
71789 ** This operation leaves the cursor in a state where it cannot be
71790 ** advanced in either direction. In other words, the Next and Prev
71791 ** opcodes do not work after this operation.
71792 **
71793 ** See also: NotFound, Found, NotExists
71794 */
71795 case OP_NoConflict: /* jump, in3 */
71796 case OP_NotFound: /* jump, in3 */
@@ -71229,10 +71810,13 @@
71810
71811 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71812 assert( pOp->p4type==P4_INT32 );
71813 pC = p->apCsr[pOp->p1];
71814 assert( pC!=0 );
71815 #ifdef SQLITE_DEBUG
71816 pC->seekOp = pOp->opcode;
71817 #endif
71818 pIn3 = &aMem[pOp->p3];
71819 assert( pC->pCursor!=0 );
71820 assert( pC->isTable==0 );
71821 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
71822 if( pOp->p4.i>0 ){
@@ -71299,10 +71883,14 @@
71883 ** with rowid P3 then leave the cursor pointing at that record and fall
71884 ** through to the next instruction.
71885 **
71886 ** The OP_NotFound opcode performs the same operation on index btrees
71887 ** (with arbitrary multi-value keys).
71888 **
71889 ** This opcode leaves the cursor in a state where it cannot be advanced
71890 ** in either direction. In other words, the Next and Prev opcodes will
71891 ** not work following this opcode.
71892 **
71893 ** See also: Found, NotFound, NoConflict
71894 */
71895 case OP_NotExists: { /* jump, in3 */
71896 VdbeCursor *pC;
@@ -71313,10 +71901,13 @@
71901 pIn3 = &aMem[pOp->p3];
71902 assert( pIn3->flags & MEM_Int );
71903 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71904 pC = p->apCsr[pOp->p1];
71905 assert( pC!=0 );
71906 #ifdef SQLITE_DEBUG
71907 pC->seekOp = 0;
71908 #endif
71909 assert( pC->isTable );
71910 assert( pC->pseudoTableReg==0 );
71911 pCrsr = pC->pCursor;
71912 assert( pCrsr!=0 );
71913 res = 0;
@@ -71615,11 +72206,11 @@
72206 ** Delete the record at which the P1 cursor is currently pointing.
72207 **
72208 ** The cursor will be left pointing at either the next or the previous
72209 ** record in the table. If it is left pointing at the next record, then
72210 ** the next Next instruction will be a no-op. Hence it is OK to delete
72211 ** a record from within a Next loop.
72212 **
72213 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
72214 ** incremented (otherwise not).
72215 **
72216 ** P1 must not be pseudo-table. It has to be a real table with
@@ -71675,16 +72266,16 @@
72266 p->nChange = 0;
72267 break;
72268 }
72269
72270 /* Opcode: SorterCompare P1 P2 P3 P4
72271 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
72272 **
72273 ** P1 is a sorter cursor. This instruction compares a prefix of the
72274 ** record blob in register P3 against a prefix of the entry that
72275 ** the sorter cursor currently points to. Only the first P4 fields
72276 ** of r[P3] and the sorter record are compared.
72277 **
72278 ** If either P3 or the sorter contains a NULL in one of their significant
72279 ** fields (not counting the P4 fields at the end which are ignored) then
72280 ** the comparison is assumed to be equal.
72281 **
@@ -71692,18 +72283,18 @@
72283 ** each other. Jump to P2 if they are different.
72284 */
72285 case OP_SorterCompare: {
72286 VdbeCursor *pC;
72287 int res;
72288 int nKeyCol;
72289
72290 pC = p->apCsr[pOp->p1];
72291 assert( isSorter(pC) );
72292 assert( pOp->p4type==P4_INT32 );
72293 pIn3 = &aMem[pOp->p3];
72294 nKeyCol = pOp->p4.i;
72295 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
72296 VdbeBranchTaken(res!=0,2);
72297 if( res ){
72298 pc = pOp->p2-1;
72299 }
72300 break;
@@ -71879,15 +72470,19 @@
72470 break;
72471 }
72472
72473 /* Opcode: Last P1 P2 * * *
72474 **
72475 ** The next use of the Rowid or Column or Prev instruction for P1
72476 ** will refer to the last entry in the database table or index.
72477 ** If the table or index is empty and P2>0, then jump immediately to P2.
72478 ** If P2 is 0 or if the table or index is not empty, fall through
72479 ** to the following instruction.
72480 **
72481 ** This opcode leaves the cursor configured to move in reverse order,
72482 ** from the end toward the beginning. In other words, the cursor is
72483 ** configured to use Prev, not Next.
72484 */
72485 case OP_Last: { /* jump */
72486 VdbeCursor *pC;
72487 BtCursor *pCrsr;
72488 int res;
@@ -71901,10 +72496,13 @@
72496 rc = sqlite3BtreeLast(pCrsr, &res);
72497 pC->nullRow = (u8)res;
72498 pC->deferredMoveto = 0;
72499 pC->rowidIsValid = 0;
72500 pC->cacheStatus = CACHE_STALE;
72501 #ifdef SQLITE_DEBUG
72502 pC->seekOp = OP_Last;
72503 #endif
72504 if( pOp->p2>0 ){
72505 VdbeBranchTaken(res!=0,2);
72506 if( res ) pc = pOp->p2 - 1;
72507 }
72508 break;
@@ -71937,10 +72535,14 @@
72535 ** The next use of the Rowid or Column or Next instruction for P1
72536 ** will refer to the first entry in the database table or index.
72537 ** If the table or index is empty and P2>0, then jump immediately to P2.
72538 ** If P2 is 0 or if the table or index is not empty, fall through
72539 ** to the following instruction.
72540 **
72541 ** This opcode leaves the cursor configured to move in forward order,
72542 ** from the beginning toward the end. In other words, the cursor is
72543 ** configured to use Next, not Prev.
72544 */
72545 case OP_Rewind: { /* jump */
72546 VdbeCursor *pC;
72547 BtCursor *pCrsr;
72548 int res;
@@ -71948,10 +72550,13 @@
72550 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
72551 pC = p->apCsr[pOp->p1];
72552 assert( pC!=0 );
72553 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
72554 res = 1;
72555 #ifdef SQLITE_DEBUG
72556 pC->seekOp = OP_Rewind;
72557 #endif
72558 if( isSorter(pC) ){
72559 rc = sqlite3VdbeSorterRewind(db, pC, &res);
72560 }else{
72561 pCrsr = pC->pCursor;
72562 assert( pCrsr );
@@ -71973,10 +72578,14 @@
72578 **
72579 ** Advance cursor P1 so that it points to the next key/data pair in its
72580 ** table or index. If there are no more key/value pairs then fall through
72581 ** to the following instruction. But if the cursor advance was successful,
72582 ** jump immediately to P2.
72583 **
72584 ** The Next opcode is only valid following an SeekGT, SeekGE, or
72585 ** OP_Rewind opcode used to position the cursor. Next is not allowed
72586 ** to follow SeekLT, SeekLE, or OP_Last.
72587 **
72588 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
72589 ** been opened prior to this opcode or the program will segfault.
72590 **
72591 ** The P3 value is a hint to the btree implementation. If P3==1, that
@@ -71992,20 +72601,25 @@
72601 **
72602 ** See also: Prev, NextIfOpen
72603 */
72604 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
72605 **
72606 ** This opcode works just like Next except that if cursor P1 is not
72607 ** open it behaves a no-op.
72608 */
72609 /* Opcode: Prev P1 P2 P3 P4 P5
72610 **
72611 ** Back up cursor P1 so that it points to the previous key/data pair in its
72612 ** table or index. If there is no previous key/value pairs then fall through
72613 ** to the following instruction. But if the cursor backup was successful,
72614 ** jump immediately to P2.
72615 **
72616 **
72617 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
72618 ** OP_Last opcode used to position the cursor. Prev is not allowed
72619 ** to follow SeekGT, SeekGE, or OP_Rewind.
72620 **
72621 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
72622 ** not open then the behavior is undefined.
72623 **
72624 ** The P3 value is a hint to the btree implementation. If P3==1, that
72625 ** means P1 is an SQL index and that this instruction could have been
@@ -72018,11 +72632,11 @@
72632 ** If P5 is positive and the jump is taken, then event counter
72633 ** number P5-1 in the prepared statement is incremented.
72634 */
72635 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
72636 **
72637 ** This opcode works just like Prev except that if cursor P1 is not
72638 ** open it behaves a no-op.
72639 */
72640 case OP_SorterNext: { /* jump */
72641 VdbeCursor *pC;
72642 int res;
@@ -72049,10 +72663,20 @@
72663 testcase( res==1 );
72664 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
72665 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
72666 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
72667 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
72668
72669 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
72670 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
72671 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
72672 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
72673 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
72674 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
72675 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
72676 || pC->seekOp==OP_Last );
72677
72678 rc = pOp->p4.xAdvance(pC->pCursor, &res);
72679 next_tail:
72680 pC->cacheStatus = CACHE_STALE;
72681 VdbeBranchTaken(res==0,2);
72682 if( res==0 ){
@@ -72331,11 +72955,11 @@
72955 rc = SQLITE_LOCKED;
72956 p->errorAction = OE_Abort;
72957 }else{
72958 iDb = pOp->p3;
72959 assert( iCnt==1 );
72960 assert( DbMaskTest(p->btreeMask, iDb) );
72961 iMoved = 0; /* Not needed. Only to silence a warning. */
72962 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
72963 pOut->flags = MEM_Int;
72964 pOut->u.i = iMoved;
72965 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -72371,11 +72995,11 @@
72995 case OP_Clear: {
72996 int nChange;
72997
72998 nChange = 0;
72999 assert( p->readOnly==0 );
73000 assert( DbMaskTest(p->btreeMask, pOp->p2) );
73001 rc = sqlite3BtreeClearTable(
73002 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
73003 );
73004 if( pOp->p3 ){
73005 p->nChange += nChange;
@@ -72441,11 +73065,11 @@
73065 int flags;
73066 Db *pDb;
73067
73068 pgno = 0;
73069 assert( pOp->p1>=0 && pOp->p1<db->nDb );
73070 assert( DbMaskTest(p->btreeMask, pOp->p1) );
73071 assert( p->readOnly==0 );
73072 pDb = &db->aDb[pOp->p1];
73073 assert( pDb->pBt!=0 );
73074 if( pOp->opcode==OP_CreateTable ){
73075 /* flags = BTREE_INTKEY; */
@@ -72529,11 +73153,12 @@
73153
73154 /* Opcode: DropTable P1 * * P4 *
73155 **
73156 ** Remove the internal (in-memory) data structures that describe
73157 ** the table named P4 in database P1. This is called after a table
73158 ** is dropped from disk (using the Destroy opcode) in order to keep
73159 ** the internal representation of the
73160 ** schema consistent with what is on disk.
73161 */
73162 case OP_DropTable: {
73163 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
73164 break;
@@ -72541,11 +73166,12 @@
73166
73167 /* Opcode: DropIndex P1 * * P4 *
73168 **
73169 ** Remove the internal (in-memory) data structures that describe
73170 ** the index named P4 in database P1. This is called after an index
73171 ** is dropped from disk (using the Destroy opcode)
73172 ** in order to keep the internal representation of the
73173 ** schema consistent with what is on disk.
73174 */
73175 case OP_DropIndex: {
73176 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
73177 break;
@@ -72553,11 +73179,12 @@
73179
73180 /* Opcode: DropTrigger P1 * * P4 *
73181 **
73182 ** Remove the internal (in-memory) data structures that describe
73183 ** the trigger named P4 in database P1. This is called after a trigger
73184 ** is dropped from disk (using the Destroy opcode) in order to keep
73185 ** the internal representation of the
73186 ** schema consistent with what is on disk.
73187 */
73188 case OP_DropTrigger: {
73189 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
73190 break;
@@ -72606,11 +73233,11 @@
73233 for(j=0; j<nRoot; j++){
73234 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
73235 }
73236 aRoot[j] = 0;
73237 assert( pOp->p5<db->nDb );
73238 assert( DbMaskTest(p->btreeMask, pOp->p5) );
73239 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
73240 (int)pnErr->u.i, &nErr);
73241 sqlite3DbFree(db, aRoot);
73242 pnErr->u.i -= nErr;
73243 sqlite3VdbeMemSetNull(pIn1);
@@ -72968,21 +73595,20 @@
73595 pc = pOp->p2 - 1;
73596 }
73597 break;
73598 }
73599
73600 /* Opcode: IfNeg P1 P2 P3 * *
73601 ** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
73602 **
73603 ** Register P1 must contain an integer. Add literal P3 to the value in
73604 ** register P1 then if the value of register P1 is less than zero, jump to P2.
 
 
73605 */
73606 case OP_IfNeg: { /* jump, in1 */
73607 pIn1 = &aMem[pOp->p1];
73608 assert( pIn1->flags&MEM_Int );
73609 pIn1->u.i += pOp->p3;
73610 VdbeBranchTaken(pIn1->u.i<0, 2);
73611 if( pIn1->u.i<0 ){
73612 pc = pOp->p2 - 1;
73613 }
73614 break;
@@ -72991,13 +73617,10 @@
73617 /* Opcode: IfZero P1 P2 P3 * *
73618 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
73619 **
73620 ** The register P1 must contain an integer. Add literal P3 to the
73621 ** value in register P1. If the result is exactly 0, jump to P2.
 
 
 
73622 */
73623 case OP_IfZero: { /* jump, in1 */
73624 pIn1 = &aMem[pOp->p1];
73625 assert( pIn1->flags&MEM_Int );
73626 pIn1->u.i += pOp->p3;
@@ -73266,11 +73889,11 @@
73889 */
73890 case OP_IncrVacuum: { /* jump */
73891 Btree *pBt;
73892
73893 assert( pOp->p1>=0 && pOp->p1<db->nDb );
73894 assert( DbMaskTest(p->btreeMask, pOp->p1) );
73895 assert( p->readOnly==0 );
73896 pBt = db->aDb[pOp->p1].pBt;
73897 rc = sqlite3BtreeIncrVacuum(pBt);
73898 VdbeBranchTaken(rc==SQLITE_DONE,2);
73899 if( rc==SQLITE_DONE ){
@@ -73281,16 +73904,17 @@
73904 }
73905 #endif
73906
73907 /* Opcode: Expire P1 * * * *
73908 **
73909 ** Cause precompiled statements to expire. When an expired statement
73910 ** is executed using sqlite3_step() it will either automatically
73911 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
73912 ** or it will fail with SQLITE_SCHEMA.
73913 **
73914 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
73915 ** then only the currently executing statement is expired.
73916 */
73917 case OP_Expire: {
73918 if( !pOp->p1 ){
73919 sqlite3ExpirePreparedStatements(db);
73920 }else{
@@ -73318,11 +73942,11 @@
73942 case OP_TableLock: {
73943 u8 isWriteLock = (u8)pOp->p3;
73944 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
73945 int p1 = pOp->p1;
73946 assert( p1>=0 && p1<db->nDb );
73947 assert( DbMaskTest(p->btreeMask, p1) );
73948 assert( isWriteLock==0 || isWriteLock==1 );
73949 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
73950 if( (rc&0xFF)==SQLITE_LOCKED ){
73951 const char *z = pOp->p4.z;
73952 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
@@ -73768,11 +74392,11 @@
74392 #ifdef SQLITE_USE_FCNTL_TRACE
74393 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
74394 if( zTrace ){
74395 int i;
74396 for(i=0; i<db->nDb; i++){
74397 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
74398 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
74399 }
74400 }
74401 #endif /* SQLITE_USE_FCNTL_TRACE */
74402 #ifdef SQLITE_DEBUG
@@ -74758,11 +75382,11 @@
75382 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
75383 ** has been allocated and contains an unpacked record that is used as key2.
75384 */
75385 static void vdbeSorterCompare(
75386 const VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
75387 int nKeyCol, /* Num of columns. 0 means "all" */
75388 const void *pKey1, int nKey1, /* Left side of comparison */
75389 const void *pKey2, int nKey2, /* Right side of comparison */
75390 int *pRes /* OUT: Result of comparison */
75391 ){
75392 KeyInfo *pKeyInfo = pCsr->pKeyInfo;
@@ -74772,14 +75396,13 @@
75396
75397 if( pKey2 ){
75398 sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
75399 }
75400
75401 if( nKeyCol ){
75402 r2->nField = nKeyCol;
75403 for(i=0; i<nKeyCol; i++){
 
75404 if( r2->aMem[i].flags & MEM_Null ){
75405 *pRes = -1;
75406 return;
75407 }
75408 }
@@ -75457,18 +76080,18 @@
76080 ** key.
76081 */
76082 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
76083 const VdbeCursor *pCsr, /* Sorter cursor */
76084 Mem *pVal, /* Value to compare to current sorter key */
76085 int nKeyCol, /* Only compare this many fields */
76086 int *pRes /* OUT: Result of comparison */
76087 ){
76088 VdbeSorter *pSorter = pCsr->pSorter;
76089 void *pKey; int nKey; /* Sorter key to compare pVal with */
76090
76091 pKey = vdbeSorterRowkey(pSorter, &nKey);
76092 vdbeSorterCompare(pCsr, nKeyCol, pVal->z, pVal->n, pKey, nKey, pRes);
76093 return SQLITE_OK;
76094 }
76095
76096 /************** End of vdbesort.c ********************************************/
76097 /************** Begin file journal.c *****************************************/
@@ -76496,11 +77119,11 @@
77119 }
77120 break;
77121 }
77122 }
77123 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
77124 /* IMP: R-51414-32910 */
77125 /* IMP: R-44911-55124 */
77126 iCol = -1;
77127 }
77128 if( iCol<pTab->nCol ){
77129 cnt++;
@@ -76852,12 +77475,17 @@
77475 }
77476 }else{
77477 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
77478 ** likelihood(X, 0.0625).
77479 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
77480 ** likelihood(X,0.0625).
77481 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
77482 ** likelihood(X,0.9375).
77483 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
77484 ** likelihood(X,0.9375). */
77485 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
77486 pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
77487 }
77488 }
77489 }
77490 #ifndef SQLITE_OMIT_AUTHORIZATION
77491 if( pDef ){
@@ -77629,11 +78257,11 @@
78257 ** SELECT * FROM t1 WHERE (select a from t1);
78258 */
78259 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
78260 int op;
78261 pExpr = sqlite3ExprSkipCollate(pExpr);
78262 if( pExpr->flags & EP_Generic ) return 0;
78263 op = pExpr->op;
78264 if( op==TK_SELECT ){
78265 assert( pExpr->flags&EP_xIsSelect );
78266 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
78267 }
@@ -78964,10 +79592,13 @@
79592 case TK_INTEGER:
79593 case TK_STRING:
79594 case TK_FLOAT:
79595 case TK_BLOB:
79596 return 0;
79597 case TK_COLUMN:
79598 assert( p->pTab!=0 );
79599 return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0;
79600 default:
79601 return 1;
79602 }
79603 }
79604
@@ -79071,83 +79702,124 @@
79702 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
79703 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
79704 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
79705 }
79706
79707 /*
79708 ** Generate code that checks the left-most column of index table iCur to see if
79709 ** it contains any NULL entries. Cause the register at regHasNull to be set
79710 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
79711 ** to be set to NULL if iCur contains one or more NULL values.
79712 */
79713 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
79714 int j1;
79715 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
79716 j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
79717 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
79718 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
79719 VdbeComment((v, "first_entry_in(%d)", iCur));
79720 sqlite3VdbeJumpHere(v, j1);
79721 }
79722
79723
79724 #ifndef SQLITE_OMIT_SUBQUERY
79725 /*
79726 ** The argument is an IN operator with a list (not a subquery) on the
79727 ** right-hand side. Return TRUE if that list is constant.
79728 */
79729 static int sqlite3InRhsIsConstant(Expr *pIn){
79730 Expr *pLHS;
79731 int res;
79732 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
79733 pLHS = pIn->pLeft;
79734 pIn->pLeft = 0;
79735 res = sqlite3ExprIsConstant(pIn);
79736 pIn->pLeft = pLHS;
79737 return res;
79738 }
79739 #endif
79740
79741 /*
79742 ** This function is used by the implementation of the IN (...) operator.
79743 ** The pX parameter is the expression on the RHS of the IN operator, which
79744 ** might be either a list of expressions or a subquery.
79745 **
79746 ** The job of this routine is to find or create a b-tree object that can
79747 ** be used either to test for membership in the RHS set or to iterate through
79748 ** all members of the RHS set, skipping duplicates.
79749 **
79750 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
79751 ** and pX->iTable is set to the index of that cursor.
79752 **
79753 ** The returned value of this function indicates the b-tree type, as follows:
79754 **
79755 ** IN_INDEX_ROWID - The cursor was opened on a database table.
79756 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
79757 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
79758 ** IN_INDEX_EPH - The cursor was opened on a specially created and
79759 ** populated epheremal table.
79760 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
79761 ** implemented as a sequence of comparisons.
79762 **
79763 ** An existing b-tree might be used if the RHS expression pX is a simple
79764 ** subquery such as:
79765 **
79766 ** SELECT <column> FROM <table>
79767 **
79768 ** If the RHS of the IN operator is a list or a more complex subquery, then
79769 ** an ephemeral table might need to be generated from the RHS and then
79770 ** pX->iTable made to point to the ephermeral table instead of an
79771 ** existing table.
79772 **
79773 ** The inFlags parameter must contain exactly one of the bits
79774 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
79775 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
79776 ** fast membership test. When the IN_INDEX_LOOP bit is set, the
79777 ** IN index will be used to loop over all values of the RHS of the
79778 ** IN operator.
79779 **
79780 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
79781 ** through the set members) then the b-tree must not contain duplicates.
79782 ** An epheremal table must be used unless the selected <column> is guaranteed
79783 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
79784 ** has a UNIQUE constraint or UNIQUE index.
79785 **
79786 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
79787 ** for fast set membership tests) then an epheremal table must
79788 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
79789 ** be found with <column> as its left-most column.
79790 **
79791 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
79792 ** if the RHS of the IN operator is a list (not a subquery) then this
79793 ** routine might decide that creating an ephemeral b-tree for membership
79794 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the
79795 ** calling routine should implement the IN operator using a sequence
79796 ** of Eq or Ne comparison operations.
79797 **
79798 ** When the b-tree is being used for membership tests, the calling function
79799 ** might need to know whether or not the RHS side of the IN operator
79800 ** contains a NULL. If prRhsHasNull is not a NULL pointer and
79801 ** if there is any chance that the (...) might contain a NULL value at
79802 ** runtime, then a register is allocated and the register number written
79803 ** to *prRhsHasNull. If there is no chance that the (...) contains a
79804 ** NULL value, then *prRhsHasNull is left unchanged.
79805 **
79806 ** If a register is allocated and its location stored in *prRhsHasNull, then
79807 ** the value in that register will be NULL if the b-tree contains one or more
79808 ** NULL values, and it will be some non-NULL value if the b-tree contains no
79809 ** NULL values.
 
 
 
 
 
 
 
 
 
 
79810 */
79811 #ifndef SQLITE_OMIT_SUBQUERY
79812 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
79813 Select *p; /* SELECT to the right of IN operator */
79814 int eType = 0; /* Type of RHS table. IN_INDEX_* */
79815 int iTab = pParse->nTab++; /* Cursor of the RHS table */
79816 int mustBeUnique; /* True if RHS must be unique */
79817 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
79818
79819 assert( pX->op==TK_IN );
79820 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
79821
79822 /* Check to see if an existing table or index can be used to
79823 ** satisfy the query. This is preferable to generating a new
79824 ** ephemeral table.
79825 */
@@ -79200,44 +79872,59 @@
79872 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
79873
79874 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
79875 if( (pIdx->aiColumn[0]==iCol)
79876 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
79877 && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
79878 ){
79879 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
79880 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
79881 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
79882 VdbeComment((v, "%s", pIdx->zName));
79883 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
79884 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
79885
79886 if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
79887 *prRhsHasNull = ++pParse->nMem;
79888 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
79889 }
79890 sqlite3VdbeJumpHere(v, iAddr);
79891 }
79892 }
79893 }
79894 }
79895
79896 /* If no preexisting index is available for the IN clause
79897 ** and IN_INDEX_NOOP is an allowed reply
79898 ** and the RHS of the IN operator is a list, not a subquery
79899 ** and the RHS is not contant or has two or fewer terms,
79900 ** then it is not worth creating an ephermeral table to evaluate
79901 ** the IN operator so return IN_INDEX_NOOP.
79902 */
79903 if( eType==0
79904 && (inFlags & IN_INDEX_NOOP_OK)
79905 && !ExprHasProperty(pX, EP_xIsSelect)
79906 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
79907 ){
79908 eType = IN_INDEX_NOOP;
79909 }
79910
79911
79912 if( eType==0 ){
79913 /* Could not find an existing table or index to use as the RHS b-tree.
79914 ** We will have to generate an ephemeral table to do the job.
79915 */
79916 u32 savedNQueryLoop = pParse->nQueryLoop;
79917 int rMayHaveNull = 0;
79918 eType = IN_INDEX_EPH;
79919 if( inFlags & IN_INDEX_LOOP ){
 
 
 
79920 pParse->nQueryLoop = 0;
79921 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
79922 eType = IN_INDEX_ROWID;
79923 }
79924 }else if( prRhsHasNull ){
79925 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
79926 }
79927 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
79928 pParse->nQueryLoop = savedNQueryLoop;
79929 }else{
79930 pX->iTable = iTab;
@@ -79264,31 +79951,25 @@
79951 ** intkey B-Tree to store the set of IN(...) values instead of the usual
79952 ** (slower) variable length keys B-Tree.
79953 **
79954 ** If rMayHaveNull is non-zero, that means that the operation is an IN
79955 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
79956 ** All this routine does is initialize the register given by rMayHaveNull
79957 ** to NULL. Calling routines will take care of changing this register
79958 ** value to non-NULL if the RHS is NULL-free.
 
 
 
 
 
 
79959 **
79960 ** For a SELECT or EXISTS operator, return the register that holds the
79961 ** result. For IN operators or if an error occurs, the return value is 0.
79962 */
79963 #ifndef SQLITE_OMIT_SUBQUERY
79964 SQLITE_PRIVATE int sqlite3CodeSubselect(
79965 Parse *pParse, /* Parsing context */
79966 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
79967 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
79968 int isRowid /* If true, LHS of IN operator is a rowid */
79969 ){
79970 int jmpIfDynamic = -1; /* One-time test address */
79971 int rReg = 0; /* Register storing resulting */
79972 Vdbe *v = sqlite3GetVdbe(pParse);
79973 if( NEVER(v==0) ) return 0;
79974 sqlite3ExprCachePush(pParse);
79975
@@ -79301,17 +79982,17 @@
79982 **
79983 ** If all of the above are false, then we can run this code just once
79984 ** save the results, and reuse the same result on subsequent invocations.
79985 */
79986 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
79987 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
79988 }
79989
79990 #ifndef SQLITE_OMIT_EXPLAIN
79991 if( pParse->explain==2 ){
79992 char *zMsg = sqlite3MPrintf(
79993 pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
79994 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
79995 );
79996 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
79997 }
79998 #endif
@@ -79321,14 +80002,10 @@
80002 char affinity; /* Affinity of the LHS of the IN */
80003 int addr; /* Address of OP_OpenEphemeral instruction */
80004 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
80005 KeyInfo *pKeyInfo = 0; /* Key information */
80006
 
 
 
 
80007 affinity = sqlite3ExprAffinity(pLeft);
80008
80009 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
80010 ** expression it is handled the same way. An ephemeral table is
80011 ** filled with single-field index keys representing the results
@@ -79350,24 +80027,27 @@
80027 /* Case 1: expr IN (SELECT ...)
80028 **
80029 ** Generate code to write the results of the select into the temporary
80030 ** table allocated and opened above.
80031 */
80032 Select *pSelect = pExpr->x.pSelect;
80033 SelectDest dest;
80034 ExprList *pEList;
80035
80036 assert( !isRowid );
80037 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
80038 dest.affSdst = (u8)affinity;
80039 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
80040 pSelect->iLimit = 0;
80041 testcase( pSelect->selFlags & SF_Distinct );
80042 pSelect->selFlags &= ~SF_Distinct;
80043 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
80044 if( sqlite3Select(pParse, pSelect, &dest) ){
80045 sqlite3KeyInfoUnref(pKeyInfo);
80046 return 0;
80047 }
80048 pEList = pSelect->pEList;
80049 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
80050 assert( pEList!=0 );
80051 assert( pEList->nExpr>0 );
80052 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
80053 pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
@@ -79394,23 +80074,23 @@
80074 }
80075
80076 /* Loop through each expression in <exprlist>. */
80077 r1 = sqlite3GetTempReg(pParse);
80078 r2 = sqlite3GetTempReg(pParse);
80079 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
80080 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
80081 Expr *pE2 = pItem->pExpr;
80082 int iValToIns;
80083
80084 /* If the expression is not constant then we will need to
80085 ** disable the test that was generated above that makes sure
80086 ** this code only executes once. Because for a non-constant
80087 ** expression we need to rerun this code each time.
80088 */
80089 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
80090 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
80091 jmpIfDynamic = -1;
80092 }
80093
80094 /* Evaluate the expression and insert it into the temp table */
80095 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
80096 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
@@ -79476,12 +80156,16 @@
80156 ExprSetVVAProperty(pExpr, EP_NoReduce);
80157 break;
80158 }
80159 }
80160
80161 if( rHasNullFlag ){
80162 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
80163 }
80164
80165 if( jmpIfDynamic>=0 ){
80166 sqlite3VdbeJumpHere(v, jmpIfDynamic);
80167 }
80168 sqlite3ExprCachePop(pParse);
80169
80170 return rReg;
80171 }
@@ -79498,11 +80182,11 @@
80182 ** is an array of zero or more values. The expression is true if the LHS is
80183 ** contained within the RHS. The value of the expression is unknown (NULL)
80184 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
80185 ** RHS contains one or more NULL values.
80186 **
80187 ** This routine generates code that jumps to destIfFalse if the LHS is not
80188 ** contained within the RHS. If due to NULLs we cannot determine if the LHS
80189 ** is contained in the RHS then jump to destIfNull. If the LHS is contained
80190 ** within the RHS then fall through.
80191 */
80192 static void sqlite3ExprCodeIN(
@@ -79521,11 +80205,13 @@
80205 ** pExpr->iTable will contains the values that make up the RHS.
80206 */
80207 v = pParse->pVdbe;
80208 assert( v!=0 ); /* OOM detected prior to this routine */
80209 VdbeNoopComment((v, "begin IN expr"));
80210 eType = sqlite3FindInIndex(pParse, pExpr,
80211 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
80212 destIfFalse==destIfNull ? 0 : &rRhsHasNull);
80213
80214 /* Figure out the affinity to use to create a key from the results
80215 ** of the expression. affinityStr stores a static string suitable for
80216 ** P4 of OP_MakeRecord.
80217 */
@@ -79535,86 +80221,118 @@
80221 */
80222 sqlite3ExprCachePush(pParse);
80223 r1 = sqlite3GetTempReg(pParse);
80224 sqlite3ExprCode(pParse, pExpr->pLeft, r1);
80225
80226 /* If sqlite3FindInIndex() did not find or create an index that is
80227 ** suitable for evaluating the IN operator, then evaluate using a
80228 ** sequence of comparisons.
80229 */
80230 if( eType==IN_INDEX_NOOP ){
80231 ExprList *pList = pExpr->x.pList;
80232 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
80233 int labelOk = sqlite3VdbeMakeLabel(v);
80234 int r2, regToFree;
80235 int regCkNull = 0;
80236 int ii;
80237 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80238 if( destIfNull!=destIfFalse ){
80239 regCkNull = sqlite3GetTempReg(pParse);
80240 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
80241 }
80242 for(ii=0; ii<pList->nExpr; ii++){
80243 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
80244 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
80245 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
80246 }
80247 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
80248 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
80249 (void*)pColl, P4_COLLSEQ);
80250 VdbeCoverageIf(v, ii<pList->nExpr-1);
80251 VdbeCoverageIf(v, ii==pList->nExpr-1);
80252 sqlite3VdbeChangeP5(v, affinity);
80253 }else{
80254 assert( destIfNull==destIfFalse );
80255 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
80256 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
80257 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
80258 }
80259 sqlite3ReleaseTempReg(pParse, regToFree);
80260 }
80261 if( regCkNull ){
80262 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80263 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
80264 }
80265 sqlite3VdbeResolveLabel(v, labelOk);
80266 sqlite3ReleaseTempReg(pParse, regCkNull);
80267 }else{
80268
80269 /* If the LHS is NULL, then the result is either false or NULL depending
80270 ** on whether the RHS is empty or not, respectively.
80271 */
80272 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
80273 if( destIfNull==destIfFalse ){
80274 /* Shortcut for the common case where the false and NULL outcomes are
80275 ** the same. */
80276 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
80277 }else{
80278 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
80279 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
80280 VdbeCoverage(v);
80281 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
80282 sqlite3VdbeJumpHere(v, addr1);
80283 }
80284 }
80285
80286 if( eType==IN_INDEX_ROWID ){
80287 /* In this case, the RHS is the ROWID of table b-tree
80288 */
80289 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
80290 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
80291 VdbeCoverage(v);
80292 }else{
80293 /* In this case, the RHS is an index b-tree.
80294 */
80295 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
80296
80297 /* If the set membership test fails, then the result of the
80298 ** "x IN (...)" expression must be either 0 or NULL. If the set
80299 ** contains no NULL values, then the result is 0. If the set
80300 ** contains one or more NULL values, then the result of the
80301 ** expression is also NULL.
80302 */
80303 assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
80304 if( rRhsHasNull==0 ){
80305 /* This branch runs if it is known at compile time that the RHS
80306 ** cannot contain NULL values. This happens as the result
80307 ** of a "NOT NULL" constraint in the database schema.
80308 **
80309 ** Also run this branch if NULL is equivalent to FALSE
80310 ** for this particular IN operator.
80311 */
80312 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
80313 VdbeCoverage(v);
80314 }else{
80315 /* In this branch, the RHS of the IN might contain a NULL and
80316 ** the presence of a NULL on the RHS makes a difference in the
80317 ** outcome.
80318 */
80319 int j1;
80320
80321 /* First check to see if the LHS is contained in the RHS. If so,
80322 ** then the answer is TRUE the presence of NULLs in the RHS does
80323 ** not matter. If the LHS is not contained in the RHS, then the
80324 ** answer is NULL if the RHS contains NULLs and the answer is
80325 ** FALSE if the RHS is NULL-free.
80326 */
80327 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
80328 VdbeCoverage(v);
80329 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
80330 VdbeCoverage(v);
80331 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
80332 sqlite3VdbeJumpHere(v, j1);
80333 }
80334 }
80335 }
80336 sqlite3ReleaseTempReg(pParse, r1);
80337 sqlite3ExprCachePop(pParse);
80338 VdbeComment((v, "end IN expr"));
@@ -79671,21 +80389,28 @@
80389 }else{
80390 int c;
80391 i64 value;
80392 const char *z = pExpr->u.zToken;
80393 assert( z!=0 );
80394 c = sqlite3DecOrHexToI64(z, &value);
80395 if( c==0 || (c==2 && negFlag) ){
80396 char *zV;
80397 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
80398 zV = dup8bytes(v, (char*)&value);
80399 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
80400 }else{
80401 #ifdef SQLITE_OMIT_FLOATING_POINT
80402 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
80403 #else
80404 #ifndef SQLITE_OMIT_HEX_INTEGER
80405 if( sqlite3_strnicmp(z,"0x",2)==0 ){
80406 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
80407 }else
80408 #endif
80409 {
80410 codeReal(v, z, negFlag, iMem);
80411 }
80412 #endif
80413 }
80414 }
80415 }
80416
@@ -80227,11 +80952,11 @@
80952 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80953 testcase( regFree1==0 );
80954 addr = sqlite3VdbeAddOp1(v, op, r1);
80955 VdbeCoverageIf(v, op==TK_ISNULL);
80956 VdbeCoverageIf(v, op==TK_NOTNULL);
80957 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
80958 sqlite3VdbeJumpHere(v, addr);
80959 break;
80960 }
80961 case TK_AGG_FUNCTION: {
80962 AggInfo *pInfo = pExpr->pAggInfo;
@@ -80263,11 +80988,11 @@
80988 nFarg = pFarg ? pFarg->nExpr : 0;
80989 assert( !ExprHasProperty(pExpr, EP_IntValue) );
80990 zId = pExpr->u.zToken;
80991 nId = sqlite3Strlen30(zId);
80992 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
80993 if( pDef==0 || pDef->xFunc==0 ){
80994 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
80995 break;
80996 }
80997
80998 /* Attempt a direct implementation of the built-in COALESCE() and
@@ -82929,10 +83654,11 @@
83654 /* Open the sqlite_stat[134] tables for writing. */
83655 for(i=0; aTable[i].zCols; i++){
83656 assert( i<ArraySize(aTable) );
83657 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
83658 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
83659 VdbeComment((v, aTable[i].zName));
83660 }
83661 }
83662
83663 /*
83664 ** Recommended number of samples for sqlite_stat4
@@ -82964,11 +83690,12 @@
83690 #endif
83691 };
83692 struct Stat4Accum {
83693 tRowcnt nRow; /* Number of rows in the entire table */
83694 tRowcnt nPSample; /* How often to do a periodic sample */
83695 int nCol; /* Number of columns in index + pk/rowid */
83696 int nKeyCol; /* Number of index columns w/o the pk/rowid */
83697 int mxSample; /* Maximum number of samples to accumulate */
83698 Stat4Sample current; /* Current row as a Stat4Sample */
83699 u32 iPrn; /* Pseudo-random number used for sampling */
83700 Stat4Sample *aBest; /* Array of nCol best samples */
83701 int iMin; /* Index in a[] of entry with minimum score */
@@ -83050,13 +83777,26 @@
83777 #endif
83778 sqlite3DbFree(p->db, p);
83779 }
83780
83781 /*
83782 ** Implementation of the stat_init(N,K,C) SQL function. The three parameters
83783 ** are:
83784 ** N: The number of columns in the index including the rowid/pk (note 1)
83785 ** K: The number of columns in the index excluding the rowid/pk.
83786 ** C: The number of rows in the index (note 2)
83787 **
83788 ** Note 1: In the special case of the covering index that implements a
83789 ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
83790 ** total number of columns in the table.
83791 **
83792 ** Note 2: C is only used for STAT3 and STAT4.
83793 **
83794 ** For indexes on ordinary rowid tables, N==K+1. But for indexes on
83795 ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
83796 ** PRIMARY KEY of the table. The covering index that implements the
83797 ** original WITHOUT ROWID table as N==K as a special case.
83798 **
83799 ** This routine allocates the Stat4Accum object in heap memory. The return
83800 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
83801 ** the size of the blob is sizeof(void*) bytes).
83802 */
@@ -83065,10 +83805,11 @@
83805 int argc,
83806 sqlite3_value **argv
83807 ){
83808 Stat4Accum *p;
83809 int nCol; /* Number of columns in index being sampled */
83810 int nKeyCol; /* Number of key columns */
83811 int nColUp; /* nCol rounded up for alignment */
83812 int n; /* Bytes of space to allocate */
83813 sqlite3 *db; /* Database connection */
83814 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83815 int mxSample = SQLITE_STAT4_SAMPLES;
@@ -83075,12 +83816,15 @@
83816 #endif
83817
83818 /* Decode the three function arguments */
83819 UNUSED_PARAMETER(argc);
83820 nCol = sqlite3_value_int(argv[0]);
83821 assert( nCol>0 );
83822 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
83823 nKeyCol = sqlite3_value_int(argv[1]);
83824 assert( nKeyCol<=nCol );
83825 assert( nKeyCol>0 );
83826
83827 /* Allocate the space required for the Stat4Accum object */
83828 n = sizeof(*p)
83829 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
83830 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
@@ -83098,10 +83842,11 @@
83842 }
83843
83844 p->db = db;
83845 p->nRow = 0;
83846 p->nCol = nCol;
83847 p->nKeyCol = nKeyCol;
83848 p->current.anDLt = (tRowcnt*)&p[1];
83849 p->current.anEq = &p->current.anDLt[nColUp];
83850
83851 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83852 {
@@ -83108,13 +83853,13 @@
83853 u8 *pSpace; /* Allocated space not yet assigned */
83854 int i; /* Used to iterate through p->aSample[] */
83855
83856 p->iGet = -1;
83857 p->mxSample = mxSample;
83858 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
83859 p->current.anLt = &p->current.anEq[nColUp];
83860 p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[2])*0xd0944565;
83861
83862 /* Set up the Stat4Accum.a[] and aBest[] arrays */
83863 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
83864 p->aBest = &p->a[mxSample];
83865 pSpace = (u8*)(&p->a[mxSample+nCol]);
@@ -83133,11 +83878,11 @@
83878
83879 /* Return a pointer to the allocated object to the caller */
83880 sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
83881 }
83882 static const FuncDef statInitFuncdef = {
83883 2+IsStat34, /* nArg */
83884 SQLITE_UTF8, /* funcFlags */
83885 0, /* pUserData */
83886 0, /* pNext */
83887 statInit, /* xFunc */
83888 0, /* xStep */
@@ -83357,11 +84102,14 @@
84102 ** P Pointer to the Stat4Accum object created by stat_init()
84103 ** C Index of left-most column to differ from previous row
84104 ** R Rowid for the current row. Might be a key record for
84105 ** WITHOUT ROWID tables.
84106 **
84107 ** This SQL function always returns NULL. It's purpose it to accumulate
84108 ** statistical data and/or samples in the Stat4Accum object about the
84109 ** index being analyzed. The stat_get() SQL function will later be used to
84110 ** extract relevant information for constructing the sqlite_statN tables.
84111 **
84112 ** The R parameter is only used for STAT3 and STAT4
84113 */
84114 static void statPush(
84115 sqlite3_context *context,
@@ -83374,11 +84122,11 @@
84122 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
84123 int iChng = sqlite3_value_int(argv[1]);
84124
84125 UNUSED_PARAMETER( argc );
84126 UNUSED_PARAMETER( context );
84127 assert( p->nCol>0 );
84128 assert( iChng<p->nCol );
84129
84130 if( p->nRow==0 ){
84131 /* This is the first call to this function. Do initialization. */
84132 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
@@ -83451,11 +84199,14 @@
84199 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
84200 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
84201
84202 /*
84203 ** Implementation of the stat_get(P,J) SQL function. This routine is
84204 ** used to query statistical information that has been gathered into
84205 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
84206 ** is a BLOB which is decoded into a pointer to the Stat4Accum objects.
84207 ** The content to returned is determined by the parameter J
84208 ** which is one of the STAT_GET_xxxx values defined above.
84209 **
84210 ** If neither STAT3 nor STAT4 are enabled, then J is always
84211 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
84212 ** a one-parameter function, stat_get(P), that always returns the
@@ -83502,19 +84253,19 @@
84253 ** I = (K+D-1)/D
84254 */
84255 char *z;
84256 int i;
84257
84258 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
84259 if( zRet==0 ){
84260 sqlite3_result_error_nomem(context);
84261 return;
84262 }
84263
84264 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
84265 z = zRet + sqlite3Strlen30(zRet);
84266 for(i=0; i<p->nKeyCol; i++){
84267 u64 nDistinct = p->current.anDLt[i] + 1;
84268 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
84269 sqlite3_snprintf(24, z, " %llu", iVal);
84270 z += sqlite3Strlen30(z);
84271 assert( p->current.anEq[i] );
@@ -83670,31 +84421,31 @@
84421 pParse->nTab = MAX(pParse->nTab, iTab);
84422 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
84423 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
84424
84425 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
84426 int nCol; /* Number of columns in pIdx. "N" */
 
84427 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
 
84428 int addrNextRow; /* Address of "next_row:" */
84429 const char *zIdxName; /* Name of the index */
84430 int nColTest; /* Number of columns to test for changes */
84431
84432 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
84433 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
84434 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
84435 nCol = pIdx->nKeyCol;
84436 zIdxName = pTab->zName;
84437 nColTest = nCol - 1;
84438 }else{
84439 nCol = pIdx->nColumn;
84440 zIdxName = pIdx->zName;
84441 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
84442 }
84443
84444 /* Populate the register containing the index name. */
 
 
 
 
 
84445 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
84446 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
84447
84448 /*
84449 ** Pseudo-code for loop that calls stat_push():
84450 **
84451 ** Rewind csr
@@ -83715,11 +84466,11 @@
84466 ** regPrev(0) = idx(0)
84467 ** chng_addr_1:
84468 ** regPrev(1) = idx(1)
84469 ** ...
84470 **
84471 ** endDistinctTest:
84472 ** regRowid = idx(rowid)
84473 ** stat_push(P, regChng, regRowid)
84474 ** Next csr
84475 ** if !eof(csr) goto next_row;
84476 **
@@ -83728,32 +84479,36 @@
84479
84480 /* Make sure there are enough memory cells allocated to accommodate
84481 ** the regPrev array and a trailing rowid (the rowid slot is required
84482 ** when building a record to insert into the sample column of
84483 ** the sqlite_stat4 table. */
84484 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
84485
84486 /* Open a read-only cursor on the index being analyzed. */
84487 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
84488 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
84489 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
84490 VdbeComment((v, "%s", pIdx->zName));
84491
84492 /* Invoke the stat_init() function. The arguments are:
84493 **
84494 ** (1) the number of columns in the index including the rowid
84495 ** (or for a WITHOUT ROWID table, the number of PK columns),
84496 ** (2) the number of columns in the key without the rowid/pk
84497 ** (3) the number of rows in the index,
84498 **
84499 **
84500 ** The third argument is only used for STAT3 and STAT4
84501 */
84502 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
84503 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
84504 #endif
84505 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
84506 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
84507 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
84508 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
84509 sqlite3VdbeChangeP5(v, 2+IsStat34);
84510
84511 /* Implementation of the following:
84512 **
84513 ** Rewind csr
84514 ** if eof(csr) goto end_of_scan;
@@ -83762,56 +84517,73 @@
84517 **
84518 */
84519 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
84520 VdbeCoverage(v);
84521 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
 
 
 
 
 
 
 
 
 
 
 
 
84522 addrNextRow = sqlite3VdbeCurrentAddr(v);
84523
84524 if( nColTest>0 ){
84525 int endDistinctTest = sqlite3VdbeMakeLabel(v);
84526 int *aGotoChng; /* Array of jump instruction addresses */
84527 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
84528 if( aGotoChng==0 ) continue;
84529
84530 /*
84531 ** next_row:
84532 ** regChng = 0
84533 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
84534 ** regChng = 1
84535 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
84536 ** ...
84537 ** regChng = N
84538 ** goto endDistinctTest
84539 */
84540 sqlite3VdbeAddOp0(v, OP_Goto);
84541 addrNextRow = sqlite3VdbeCurrentAddr(v);
84542 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
84543 /* For a single-column UNIQUE index, once we have found a non-NULL
84544 ** row, we know that all the rest will be distinct, so skip
84545 ** subsequent distinctness tests. */
84546 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
84547 VdbeCoverage(v);
84548 }
84549 for(i=0; i<nColTest; i++){
84550 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
84551 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
84552 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
84553 aGotoChng[i] =
84554 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
84555 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
84556 VdbeCoverage(v);
84557 }
84558 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
84559 sqlite3VdbeAddOp2(v, OP_Goto, 0, endDistinctTest);
84560
84561
84562 /*
84563 ** chng_addr_0:
84564 ** regPrev(0) = idx(0)
84565 ** chng_addr_1:
84566 ** regPrev(1) = idx(1)
84567 ** ...
84568 */
84569 sqlite3VdbeJumpHere(v, addrNextRow-1);
84570 for(i=0; i<nColTest; i++){
84571 sqlite3VdbeJumpHere(v, aGotoChng[i]);
84572 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
84573 }
84574 sqlite3VdbeResolveLabel(v, endDistinctTest);
84575 sqlite3DbFree(db, aGotoChng);
84576 }
84577
84578 /*
84579 ** chng_addr_N:
84580 ** regRowid = idx(rowid) // STAT34 only
84581 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
84582 ** Next csr
84583 ** if !eof(csr) goto next_row;
84584 */
 
84585 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
84586 assert( regRowid==(regStat4+2) );
84587 if( HasRowid(pTab) ){
84588 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
84589 }else{
@@ -83851,11 +84623,11 @@
84623 int regSampleRowid = regCol + nCol;
84624 int addrNext;
84625 int addrIsNull;
84626 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
84627
84628 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
84629
84630 addrNext = sqlite3VdbeCurrentAddr(v);
84631 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
84632 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
84633 VdbeCoverage(v);
@@ -83873,11 +84645,11 @@
84645 #else
84646 for(i=0; i<nCol; i++){
84647 i16 iCol = pIdx->aiColumn[i];
84648 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
84649 }
84650 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
84651 #endif
84652 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
84653 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
84654 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
84655 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
@@ -83885,11 +84657,10 @@
84657 }
84658 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
84659
84660 /* End of analysis */
84661 sqlite3VdbeJumpHere(v, addrRewind);
 
84662 }
84663
84664
84665 /* Create a single sqlite_stat1 entry containing NULL as the index
84666 ** name and the row count as the content.
@@ -83986,10 +84757,11 @@
84757 int i;
84758 char *z, *zDb;
84759 Table *pTab;
84760 Index *pIdx;
84761 Token *pTableName;
84762 Vdbe *v;
84763
84764 /* Read the database schema. If an error occurs, leave an error message
84765 ** and code in pParse and return NULL. */
84766 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
84767 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
@@ -84033,10 +84805,12 @@
84805 }
84806 sqlite3DbFree(db, z);
84807 }
84808 }
84809 }
84810 v = sqlite3GetVdbe(pParse);
84811 if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
84812 }
84813
84814 /*
84815 ** Used to pass information from the analyzer reader through to the
84816 ** callback routine.
@@ -84091,18 +84865,23 @@
84865 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
84866 assert( pIndex!=0 );
84867 #else
84868 if( pIndex )
84869 #endif
84870 while( z[0] ){
84871 if( sqlite3_strglob("unordered*", z)==0 ){
84872 pIndex->bUnordered = 1;
84873 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
84874 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
 
 
84875 }
84876 #ifdef SQLITE_ENABLE_COSTMULT
84877 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
84878 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
84879 }
84880 #endif
84881 while( z[0]!=0 && z[0]!=' ' ) z++;
84882 while( z[0]==' ' ) z++;
84883 }
84884 }
84885
84886 /*
84887 ** This callback is invoked once for each index when reading the
@@ -84139,15 +84918,19 @@
84918 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
84919 }
84920 z = argv[2];
84921
84922 if( pIndex ){
84923 pIndex->bUnordered = 0;
84924 decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
84925 if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
84926 }else{
84927 Index fakeIdx;
84928 fakeIdx.szIdxRow = pTable->szTabRow;
84929 #ifdef SQLITE_ENABLE_COSTMULT
84930 fakeIdx.pTable = pTable;
84931 #endif
84932 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
84933 pTable->szTabRow = fakeIdx.szIdxRow;
84934 }
84935
84936 return 0;
@@ -84185,11 +84968,20 @@
84968 static void initAvgEq(Index *pIdx){
84969 if( pIdx ){
84970 IndexSample *aSample = pIdx->aSample;
84971 IndexSample *pFinal = &aSample[pIdx->nSample-1];
84972 int iCol;
84973 int nCol = 1;
84974 if( pIdx->nSampleCol>1 ){
84975 /* If this is stat4 data, then calculate aAvgEq[] values for all
84976 ** sample columns except the last. The last is always set to 1, as
84977 ** once the trailing PK fields are considered all index keys are
84978 ** unique. */
84979 nCol = pIdx->nSampleCol-1;
84980 pIdx->aAvgEq[nCol] = 1;
84981 }
84982 for(iCol=0; iCol<nCol; iCol++){
84983 int i; /* Used to iterate through samples */
84984 tRowcnt sumEq = 0; /* Sum of the nEq values */
84985 tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
84986 tRowcnt avgEq = 0;
84987 tRowcnt nDLt = pFinal->anDLt[iCol];
@@ -84208,11 +85000,10 @@
85000 if( nDLt>nSum ){
85001 avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
85002 }
85003 if( avgEq==0 ) avgEq = 1;
85004 pIdx->aAvgEq[iCol] = avgEq;
 
85005 }
85006 }
85007 }
85008
85009 /*
@@ -84267,11 +85058,10 @@
85058 sqlite3DbFree(db, zSql);
85059 if( rc ) return rc;
85060
85061 while( sqlite3_step(pStmt)==SQLITE_ROW ){
85062 int nIdxCol = 1; /* Number of columns in stat4 records */
 
85063
85064 char *zIndex; /* Index name */
85065 Index *pIdx; /* Pointer to the index object */
85066 int nSample; /* Number of samples */
85067 int nByte; /* Bytes of space required */
@@ -84285,25 +85075,29 @@
85075 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
85076 /* Index.nSample is non-zero at this point if data has already been
85077 ** loaded from the stat4 table. In this case ignore stat3 data. */
85078 if( pIdx==0 || pIdx->nSample ) continue;
85079 if( bStat3==0 ){
85080 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
85081 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
85082 nIdxCol = pIdx->nKeyCol;
85083 }else{
85084 nIdxCol = pIdx->nColumn;
85085 }
85086 }
85087 pIdx->nSampleCol = nIdxCol;
85088 nByte = sizeof(IndexSample) * nSample;
85089 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
85090 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
85091
85092 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
85093 if( pIdx->aSample==0 ){
85094 sqlite3_finalize(pStmt);
85095 return SQLITE_NOMEM;
85096 }
85097 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
85098 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
85099 for(i=0; i<nSample; i++){
85100 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
85101 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
85102 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
85103 }
@@ -85408,10 +86202,23 @@
86202 }
86203 #else
86204 #define codeTableLocks(x)
86205 #endif
86206
86207 /*
86208 ** Return TRUE if the given yDbMask object is empty - if it contains no
86209 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
86210 ** macros when SQLITE_MAX_ATTACHED is greater than 30.
86211 */
86212 #if SQLITE_MAX_ATTACHED>30
86213 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
86214 int i;
86215 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
86216 return 1;
86217 }
86218 #endif
86219
86220 /*
86221 ** This routine is called after a single SQL statement has been
86222 ** parsed and a VDBE program to execute that statement has been
86223 ** prepared. This routine puts the finishing touches on the
86224 ** VDBE program and resets the pParse structure for the next
@@ -85444,22 +86251,23 @@
86251 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
86252 ** set for each database that is used. Generate code to start a
86253 ** transaction on each used database and to verify the schema cookie
86254 ** on each used database.
86255 */
86256 if( db->mallocFailed==0
86257 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
86258 ){
86259 int iDb, i;
86260 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
86261 sqlite3VdbeJumpHere(v, 0);
86262 for(iDb=0; iDb<db->nDb; iDb++){
86263 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
86264 sqlite3VdbeUsesBtree(v, iDb);
86265 sqlite3VdbeAddOp4Int(v,
86266 OP_Transaction, /* Opcode */
86267 iDb, /* P1 */
86268 DbMaskTest(pParse->writeMask,iDb), /* P2 */
86269 pParse->cookieValue[iDb], /* P3 */
86270 db->aDb[iDb].pSchema->iGeneration /* P4 */
86271 );
86272 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
86273 }
@@ -85511,11 +86319,11 @@
86319 }
86320 pParse->nTab = 0;
86321 pParse->nMem = 0;
86322 pParse->nSet = 0;
86323 pParse->nVar = 0;
86324 DbMaskZero(pParse->cookieMask);
86325 }
86326
86327 /*
86328 ** Run the parser and code generator recursively in order to generate
86329 ** code for the SQL statement given onto the end of the pParse context
@@ -87411,11 +88219,11 @@
88219 pTable->aCol = pSelTab->aCol;
88220 pSelTab->nCol = 0;
88221 pSelTab->aCol = 0;
88222 sqlite3DeleteTable(db, pSelTab);
88223 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
88224 pTable->pSchema->schemaFlags |= DB_UnresetViews;
88225 }else{
88226 pTable->nCol = 0;
88227 nErr++;
88228 }
88229 sqlite3SelectDelete(db, pSel);
@@ -87988,16 +88796,16 @@
88796 (char *)pKey, P4_KEYINFO);
88797 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
88798
88799 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
88800 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
88801 if( IsUniqueIndex(pIndex) && pKey!=0 ){
88802 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
88803 sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
88804 addr2 = sqlite3VdbeCurrentAddr(v);
88805 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
88806 pIndex->nKeyCol); VdbeCoverage(v);
88807 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
88808 }else{
88809 addr2 = sqlite3VdbeCurrentAddr(v);
88810 }
88811 sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
@@ -88385,13 +89193,13 @@
89193 ** considered distinct and both result in separate indices.
89194 */
89195 Index *pIdx;
89196 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89197 int k;
89198 assert( IsUniqueIndex(pIdx) );
89199 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
89200 assert( IsUniqueIndex(pIndex) );
89201
89202 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
89203 for(k=0; k<pIdx->nKeyCol; k++){
89204 const char *z1;
89205 const char *z2;
@@ -88578,11 +89386,11 @@
89386 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
89387 a[i] = 23; assert( 23==sqlite3LogEst(5) );
89388 }
89389
89390 assert( 0==sqlite3LogEst(1) );
89391 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
89392 }
89393
89394 /*
89395 ** This routine will drop an existing named index. This routine
89396 ** implements the DROP INDEX statement.
@@ -89138,19 +89946,17 @@
89946 ** later, by sqlite3FinishCoding().
89947 */
89948 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
89949 Parse *pToplevel = sqlite3ParseToplevel(pParse);
89950 sqlite3 *db = pToplevel->db;
 
89951
89952 assert( iDb>=0 && iDb<db->nDb );
89953 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
89954 assert( iDb<SQLITE_MAX_ATTACHED+2 );
89955 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89956 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
89957 DbMaskSet(pToplevel->cookieMask, iDb);
 
89958 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
89959 if( !OMIT_TEMPDB && iDb==1 ){
89960 sqlite3OpenTempDatabase(pToplevel);
89961 }
89962 }
@@ -89185,11 +89991,11 @@
89991 ** necessary to undo a write and the checkpoint should not be set.
89992 */
89993 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
89994 Parse *pToplevel = sqlite3ParseToplevel(pParse);
89995 sqlite3CodeVerifySchema(pParse, iDb);
89996 DbMaskSet(pToplevel->writeMask, iDb);
89997 pToplevel->isMultiWrite |= setStatement;
89998 }
89999
90000 /*
90001 ** Indicate that the statement currently under construction might write
@@ -89992,13 +90798,13 @@
90798 sqlite3DeleteTable(0, pTab);
90799 }
90800 sqlite3HashClear(&temp1);
90801 sqlite3HashClear(&pSchema->fkeyHash);
90802 pSchema->pSeqTab = 0;
90803 if( pSchema->schemaFlags & DB_SchemaLoaded ){
90804 pSchema->iGeneration++;
90805 pSchema->schemaFlags &= ~DB_SchemaLoaded;
90806 }
90807 }
90808
90809 /*
90810 ** Find and return the schema associated with a BTree. Create
@@ -90873,16 +91679,13 @@
91679 ** May you do good and not evil.
91680 ** May you find forgiveness for yourself and forgive others.
91681 ** May you share freely, never taking more than you give.
91682 **
91683 *************************************************************************
91684 ** This file contains the C-language implementions for many of the SQL
91685 ** functions of SQLite. (Some function, and in particular the date and
91686 ** time functions, are implemented separately.)
 
 
 
91687 */
91688 /* #include <stdlib.h> */
91689 /* #include <assert.h> */
91690
91691 /*
@@ -92553,10 +93356,11 @@
93356 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
93357 FUNCTION(hex, 1, 0, 0, hexFunc ),
93358 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
93359 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
93360 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
93361 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
93362 VFUNCTION(random, 0, 0, 0, randomFunc ),
93363 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
93364 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
93365 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
93366 FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
@@ -92839,11 +93643,11 @@
93643 if( !aiCol ) return 1;
93644 *paiCol = aiCol;
93645 }
93646
93647 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
93648 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){
93649 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
93650 ** of columns. If each indexed column corresponds to a foreign key
93651 ** column of pFKey, then this index is a winner. */
93652
93653 if( zKey==0 ){
@@ -95865,11 +96669,11 @@
96669 ){
96670 return 0; /* Default values must be the same for all columns */
96671 }
96672 }
96673 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
96674 if( IsUniqueIndex(pDestIdx) ){
96675 destHasUniqueIdx = 1;
96676 }
96677 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
96678 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
96679 }
@@ -96095,10 +96899,13 @@
96899 goto exec_out;
96900 }
96901 }
96902 }
96903 if( xCallback(pArg, nCol, azVals, azCols) ){
96904 /* EVIDENCE-OF: R-38229-40159 If the callback function to
96905 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
96906 ** return SQLITE_ABORT. */
96907 rc = SQLITE_ABORT;
96908 sqlite3VdbeFinalize((Vdbe *)pStmt);
96909 pStmt = 0;
96910 sqlite3Error(db, SQLITE_ABORT, 0);
96911 goto exec_out;
@@ -97872,11 +98679,11 @@
98679 ** Note that the values returned are one less that the values that
98680 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
98681 ** to support legacy SQL code. The safety level used to be boolean
98682 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
98683 */
98684 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
98685 /* 123456789 123456789 */
98686 static const char zText[] = "onoffalseyestruefull";
98687 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
98688 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
98689 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
@@ -97894,11 +98701,11 @@
98701 }
98702
98703 /*
98704 ** Interpret the given string as a boolean value.
98705 */
98706 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
98707 return getSafetyLevel(z,1,dflt)!=0;
98708 }
98709
98710 /* The sqlite3GetBoolean() function is used by other modules but the
98711 ** remainder of this file is specific to PRAGMA processing. So omit
@@ -98440,11 +99247,11 @@
99247 */
99248 case PragTyp_JOURNAL_SIZE_LIMIT: {
99249 Pager *pPager = sqlite3BtreePager(pDb->pBt);
99250 i64 iLimit = -2;
99251 if( zRight ){
99252 sqlite3DecOrHexToI64(zRight, &iLimit);
99253 if( iLimit<-1 ) iLimit = -1;
99254 }
99255 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
99256 returnSingleInt(pParse, "journal_size_limit", iLimit);
99257 break;
@@ -98568,11 +99375,11 @@
99375 sqlite3_int64 sz;
99376 #if SQLITE_MAX_MMAP_SIZE>0
99377 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
99378 if( zRight ){
99379 int ii;
99380 sqlite3DecOrHexToI64(zRight, &sz);
99381 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
99382 if( pId2->n==0 ) db->szMmap = sz;
99383 for(ii=db->nDb-1; ii>=0; ii--){
99384 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
99385 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -98936,11 +99743,11 @@
99743 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
99744 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
99745 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
99746 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
99747 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
99748 sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
99749 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
99750 }
99751 }
99752 }
99753 break;
@@ -99186,13 +99993,12 @@
99993 ** messages have been generated, output OK. Otherwise output the
99994 ** error message
99995 */
99996 static const int iLn = VDBE_OFFSET_LINENO(2);
99997 static const VdbeOpList endCode[] = {
99998 { OP_IfNeg, 1, 0, 0}, /* 0 */
99999 { OP_String8, 0, 3, 0}, /* 1 */
 
100000 { OP_ResultRow, 3, 1, 0},
100001 };
100002
100003 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
100004
@@ -99300,32 +100106,80 @@
100106 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
100107 }
100108 pParse->nMem = MAX(pParse->nMem, 8+j);
100109 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
100110 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
100111 /* Verify that all NOT NULL columns really are NOT NULL */
100112 for(j=0; j<pTab->nCol; j++){
100113 char *zErr;
100114 int jmp2, jmp3;
100115 if( j==pTab->iPKey ) continue;
100116 if( pTab->aCol[j].notNull==0 ) continue;
100117 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
100118 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
100119 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
100120 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
100121 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
100122 pTab->aCol[j].zName);
100123 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
100124 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
100125 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
100126 sqlite3VdbeAddOp0(v, OP_Halt);
100127 sqlite3VdbeJumpHere(v, jmp2);
100128 sqlite3VdbeJumpHere(v, jmp3);
100129 }
100130 /* Validate index entries for the current row */
100131 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
100132 int jmp2, jmp3, jmp4, jmp5;
100133 int ckUniq = sqlite3VdbeMakeLabel(v);
100134 if( pPk==pIdx ) continue;
100135 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
100136 pPrior, r1);
100137 pPrior = pIdx;
100138 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
100139 /* Verify that an index entry exists for the current table row */
100140 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
100141 pIdx->nColumn); VdbeCoverage(v);
100142 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
100143 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
100144 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
100145 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
100146 " missing from index ", P4_STATIC);
100147 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
100148 jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
100149 pIdx->zName, P4_TRANSIENT);
100150 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
100151 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
100152 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
100153 sqlite3VdbeAddOp0(v, OP_Halt);
 
100154 sqlite3VdbeJumpHere(v, jmp2);
100155 /* For UNIQUE indexes, verify that only one entry exists with the
100156 ** current key. The entry is unique if (1) any column is NULL
100157 ** or (2) the next entry has a different key */
100158 if( IsUniqueIndex(pIdx) ){
100159 int uniqOk = sqlite3VdbeMakeLabel(v);
100160 int jmp6;
100161 int kk;
100162 for(kk=0; kk<pIdx->nKeyCol; kk++){
100163 int iCol = pIdx->aiColumn[kk];
100164 assert( iCol>=0 && iCol<pTab->nCol );
100165 if( pTab->aCol[iCol].notNull ) continue;
100166 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
100167 VdbeCoverage(v);
100168 }
100169 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
100170 sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
100171 sqlite3VdbeJumpHere(v, jmp6);
100172 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
100173 pIdx->nKeyCol); VdbeCoverage(v);
100174 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
100175 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
100176 "non-unique entry in index ", P4_STATIC);
100177 sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
100178 sqlite3VdbeResolveLabel(v, uniqOk);
100179 }
100180 sqlite3VdbeJumpHere(v, jmp4);
100181 sqlite3ResolvePartIdxLabel(pParse, jmp3);
100182 }
100183 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
100184 sqlite3VdbeJumpHere(v, loopTop-1);
100185 #ifndef SQLITE_OMIT_BTREECOUNT
@@ -99346,13 +100200,13 @@
100200 }
100201 #endif /* SQLITE_OMIT_BTREECOUNT */
100202 }
100203 }
100204 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
100205 sqlite3VdbeChangeP3(v, addr, -mxErr);
100206 sqlite3VdbeJumpHere(v, addr);
100207 sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
100208 }
100209 break;
100210 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
100211
100212 #ifndef SQLITE_OMIT_UTF16
@@ -99611,11 +100465,11 @@
100465 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
100466 ** use -1.
100467 */
100468 case PragTyp_SOFT_HEAP_LIMIT: {
100469 sqlite3_int64 N;
100470 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
100471 sqlite3_soft_heap_limit64(N);
100472 }
100473 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
100474 break;
100475 }
@@ -101134,12 +101988,11 @@
101988 int iOffset, /* Register holding the offset counter */
101989 int iContinue /* Jump here to skip the current record */
101990 ){
101991 if( iOffset>0 ){
101992 int addr;
101993 addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
 
101994 sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
101995 VdbeComment((v, "skip OFFSET records"));
101996 sqlite3VdbeJumpHere(v, addr);
101997 }
101998 }
@@ -101300,11 +102153,11 @@
102153 VdbeCoverage(v);
102154 }
102155 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
102156 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
102157 }
102158 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
102159 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
102160 break;
102161 }
102162
102163 case WHERE_DISTINCT_UNIQUE: {
@@ -107767,11 +108620,12 @@
108620 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
108621 VdbeCoverageNeverTaken(v);
108622 }
108623 labelContinue = labelBreak;
108624 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
108625 VdbeCoverageIf(v, pPk==0);
108626 VdbeCoverageIf(v, pPk!=0);
108627 }else if( pPk ){
108628 labelContinue = sqlite3VdbeMakeLabel(v);
108629 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
108630 addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
108631 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
@@ -109777,10 +110631,11 @@
110631 struct WherePath {
110632 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
110633 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
110634 LogEst nRow; /* Estimated number of rows generated by this path */
110635 LogEst rCost; /* Total cost of this path */
110636 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
110637 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
110638 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
110639 };
110640
110641 /*
@@ -110581,11 +111436,11 @@
111436 pScan->pOrigWC = pWC;
111437 pScan->pWC = pWC;
111438 if( pIdx && iColumn>=0 ){
111439 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
111440 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
111441 if( NEVER(j>pIdx->nColumn) ) return 0;
111442 }
111443 pScan->zCollName = pIdx->azColl[j];
111444 }else{
111445 pScan->idxaff = 0;
111446 pScan->zCollName = 0;
@@ -111507,11 +112362,11 @@
112362 **
112363 ** 3. All of those index columns for which the WHERE clause does not
112364 ** contain a "col=X" term are subject to a NOT NULL constraint.
112365 */
112366 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112367 if( !IsUniqueIndex(pIdx) ) continue;
112368 for(i=0; i<pIdx->nKeyCol; i++){
112369 i16 iCol = pIdx->aiColumn[i];
112370 if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
112371 int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
112372 if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
@@ -111531,12 +112386,11 @@
112386
112387 /*
112388 ** Estimate the logarithm of the input value to base 2.
112389 */
112390 static LogEst estLog(LogEst N){
112391 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
 
112392 }
112393
112394 /*
112395 ** Two routines for printing the content of an sqlite3_index_info
112396 ** structure. Used for testing and debugging only. If neither
@@ -111997,11 +112851,11 @@
112851 }else{
112852 i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
112853 iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
112854 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
112855 }
112856 aStat[1] = pIdx->aAvgEq[iCol];
112857 if( iLower>=iUpper ){
112858 iGap = 0;
112859 }else{
112860 iGap = iUpper - iLower;
112861 }
@@ -112036,10 +112890,119 @@
112890 }
112891 }
112892 return nRet;
112893 }
112894
112895 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112896 /*
112897 ** This function is called to estimate the number of rows visited by a
112898 ** range-scan on a skip-scan index. For example:
112899 **
112900 ** CREATE INDEX i1 ON t1(a, b, c);
112901 ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
112902 **
112903 ** Value pLoop->nOut is currently set to the estimated number of rows
112904 ** visited for scanning (a=? AND b=?). This function reduces that estimate
112905 ** by some factor to account for the (c BETWEEN ? AND ?) expression based
112906 ** on the stat4 data for the index. this scan will be peformed multiple
112907 ** times (once for each (a,b) combination that matches a=?) is dealt with
112908 ** by the caller.
112909 **
112910 ** It does this by scanning through all stat4 samples, comparing values
112911 ** extracted from pLower and pUpper with the corresponding column in each
112912 ** sample. If L and U are the number of samples found to be less than or
112913 ** equal to the values extracted from pLower and pUpper respectively, and
112914 ** N is the total number of samples, the pLoop->nOut value is adjusted
112915 ** as follows:
112916 **
112917 ** nOut = nOut * ( min(U - L, 1) / N )
112918 **
112919 ** If pLower is NULL, or a value cannot be extracted from the term, L is
112920 ** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
112921 ** U is set to N.
112922 **
112923 ** Normally, this function sets *pbDone to 1 before returning. However,
112924 ** if no value can be extracted from either pLower or pUpper (and so the
112925 ** estimate of the number of rows delivered remains unchanged), *pbDone
112926 ** is left as is.
112927 **
112928 ** If an error occurs, an SQLite error code is returned. Otherwise,
112929 ** SQLITE_OK.
112930 */
112931 static int whereRangeSkipScanEst(
112932 Parse *pParse, /* Parsing & code generating context */
112933 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
112934 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
112935 WhereLoop *pLoop, /* Update the .nOut value of this loop */
112936 int *pbDone /* Set to true if at least one expr. value extracted */
112937 ){
112938 Index *p = pLoop->u.btree.pIndex;
112939 int nEq = pLoop->u.btree.nEq;
112940 sqlite3 *db = pParse->db;
112941 int nLower = -1;
112942 int nUpper = p->nSample+1;
112943 int rc = SQLITE_OK;
112944 int iCol = p->aiColumn[nEq];
112945 u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
112946 CollSeq *pColl;
112947
112948 sqlite3_value *p1 = 0; /* Value extracted from pLower */
112949 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
112950 sqlite3_value *pVal = 0; /* Value extracted from record */
112951
112952 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
112953 if( pLower ){
112954 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
112955 nLower = 0;
112956 }
112957 if( pUpper && rc==SQLITE_OK ){
112958 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
112959 nUpper = p2 ? 0 : p->nSample;
112960 }
112961
112962 if( p1 || p2 ){
112963 int i;
112964 int nDiff;
112965 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
112966 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
112967 if( rc==SQLITE_OK && p1 ){
112968 int res = sqlite3MemCompare(p1, pVal, pColl);
112969 if( res>=0 ) nLower++;
112970 }
112971 if( rc==SQLITE_OK && p2 ){
112972 int res = sqlite3MemCompare(p2, pVal, pColl);
112973 if( res>=0 ) nUpper++;
112974 }
112975 }
112976 nDiff = (nUpper - nLower);
112977 if( nDiff<=0 ) nDiff = 1;
112978
112979 /* If there is both an upper and lower bound specified, and the
112980 ** comparisons indicate that they are close together, use the fallback
112981 ** method (assume that the scan visits 1/64 of the rows) for estimating
112982 ** the number of rows visited. Otherwise, estimate the number of rows
112983 ** using the method described in the header comment for this function. */
112984 if( nDiff!=1 || pUpper==0 || pLower==0 ){
112985 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
112986 pLoop->nOut -= nAdjust;
112987 *pbDone = 1;
112988 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
112989 nLower, nUpper, nAdjust*-1, pLoop->nOut));
112990 }
112991
112992 }else{
112993 assert( *pbDone==0 );
112994 }
112995
112996 sqlite3ValueFree(p1);
112997 sqlite3ValueFree(p2);
112998 sqlite3ValueFree(pVal);
112999
113000 return rc;
113001 }
113002 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
113003
113004 /*
113005 ** This function is used to estimate the number of rows that will be visited
113006 ** by scanning an index for a range of values. The range may have an upper
113007 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
113008 ** and lower bounds are represented by pLower and pUpper respectively. For
@@ -112072,13 +113035,13 @@
113035 ** considering the range constraints. If nEq is 0, this is the number of
113036 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
113037 ** to account for the range contraints pLower and pUpper.
113038 **
113039 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
113040 ** used, a single range inequality reduces the search space by a factor of 4.
113041 ** and a pair of constraints (x>? AND x<?) reduces the expected number of
113042 ** rows visited by a factor of 64.
113043 */
113044 static int whereRangeScanEst(
113045 Parse *pParse, /* Parsing & code generating context */
113046 WhereLoopBuilder *pBuilder,
113047 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
@@ -112092,99 +113055,104 @@
113055 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113056 Index *p = pLoop->u.btree.pIndex;
113057 int nEq = pLoop->u.btree.nEq;
113058
113059 if( p->nSample>0
 
113060 && nEq<p->nSampleCol
113061 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
113062 ){
113063 if( nEq==pBuilder->nRecValid ){
113064 UnpackedRecord *pRec = pBuilder->pRec;
113065 tRowcnt a[2];
113066 u8 aff;
113067
113068 /* Variable iLower will be set to the estimate of the number of rows in
113069 ** the index that are less than the lower bound of the range query. The
113070 ** lower bound being the concatenation of $P and $L, where $P is the
113071 ** key-prefix formed by the nEq values matched against the nEq left-most
113072 ** columns of the index, and $L is the value in pLower.
113073 **
113074 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
113075 ** is not a simple variable or literal value), the lower bound of the
113076 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
113077 ** if $L is available, whereKeyStats() is called for both ($P) and
113078 ** ($P:$L) and the larger of the two returned values used.
113079 **
113080 ** Similarly, iUpper is to be set to the estimate of the number of rows
113081 ** less than the upper bound of the range query. Where the upper bound
113082 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
113083 ** of iUpper are requested of whereKeyStats() and the smaller used.
113084 */
113085 tRowcnt iLower;
113086 tRowcnt iUpper;
113087
113088 if( nEq==p->nKeyCol ){
113089 aff = SQLITE_AFF_INTEGER;
113090 }else{
113091 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
113092 }
113093 /* Determine iLower and iUpper using ($P) only. */
113094 if( nEq==0 ){
113095 iLower = 0;
113096 iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
113097 }else{
113098 /* Note: this call could be optimized away - since the same values must
113099 ** have been requested when testing key $P in whereEqualScanEst(). */
113100 whereKeyStats(pParse, p, pRec, 0, a);
113101 iLower = a[0];
113102 iUpper = a[0] + a[1];
113103 }
113104
113105 /* If possible, improve on the iLower estimate using ($P:$L). */
113106 if( pLower ){
113107 int bOk; /* True if value is extracted from pExpr */
113108 Expr *pExpr = pLower->pExpr->pRight;
113109 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
113110 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
113111 if( rc==SQLITE_OK && bOk ){
113112 tRowcnt iNew;
113113 whereKeyStats(pParse, p, pRec, 0, a);
113114 iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
113115 if( iNew>iLower ) iLower = iNew;
113116 nOut--;
113117 }
113118 }
113119
113120 /* If possible, improve on the iUpper estimate using ($P:$U). */
113121 if( pUpper ){
113122 int bOk; /* True if value is extracted from pExpr */
113123 Expr *pExpr = pUpper->pExpr->pRight;
113124 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
113125 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
113126 if( rc==SQLITE_OK && bOk ){
113127 tRowcnt iNew;
113128 whereKeyStats(pParse, p, pRec, 1, a);
113129 iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
113130 if( iNew<iUpper ) iUpper = iNew;
113131 nOut--;
113132 }
113133 }
113134
113135 pBuilder->pRec = pRec;
113136 if( rc==SQLITE_OK ){
113137 if( iUpper>iLower ){
113138 nNew = sqlite3LogEst(iUpper - iLower);
113139 }else{
113140 nNew = 10; assert( 10==sqlite3LogEst(2) );
113141 }
113142 if( nNew<nOut ){
113143 nOut = nNew;
113144 }
113145 pLoop->nOut = (LogEst)nOut;
113146 WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n",
113147 (u32)iLower, (u32)iUpper, nOut));
113148 return SQLITE_OK;
113149 }
113150 }else{
113151 int bDone = 0;
113152 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
113153 if( bDone ) return rc;
113154 }
113155 }
113156 #else
113157 UNUSED_PARAMETER(pParse);
113158 UNUSED_PARAMETER(pBuilder);
@@ -112239,11 +113207,11 @@
113207 int rc; /* Subfunction return code */
113208 tRowcnt a[2]; /* Statistics */
113209 int bOk;
113210
113211 assert( nEq>=1 );
113212 assert( nEq<=p->nColumn );
113213 assert( p->aSample!=0 );
113214 assert( p->nSample>0 );
113215 assert( pBuilder->nRecValid<nEq );
113216
113217 /* If values are not available for all fields of the index to the left
@@ -112252,11 +113220,11 @@
113220 return SQLITE_NOTFOUND;
113221 }
113222
113223 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
113224 ** below would return the same value. */
113225 if( nEq>=p->nColumn ){
113226 *pnRow = 1;
113227 return SQLITE_OK;
113228 }
113229
113230 aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
@@ -112446,11 +113414,11 @@
113414 testcase( bRev );
113415 bRev = !bRev;
113416 }
113417 assert( pX->op==TK_IN );
113418 iReg = iTarget;
113419 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
113420 if( eType==IN_INDEX_INDEX_DESC ){
113421 testcase( bRev );
113422 bRev = !bRev;
113423 }
113424 iTab = pX->iTable;
@@ -112683,11 +113651,11 @@
113651 }
113652 sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
113653 txt.db = db;
113654 sqlite3StrAccumAppend(&txt, " (", 2);
113655 for(i=0; i<nEq; i++){
113656 char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
113657 if( i>=nSkip ){
113658 explainAppendTerm(&txt, i, z, "=");
113659 }else{
113660 if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
113661 sqlite3StrAccumAppend(&txt, "ANY(", 4);
@@ -112696,15 +113664,15 @@
113664 }
113665 }
113666
113667 j = i;
113668 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
113669 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
113670 explainAppendTerm(&txt, i++, z, ">");
113671 }
113672 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
113673 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
113674 explainAppendTerm(&txt, i, z, "<");
113675 }
113676 sqlite3StrAccumAppend(&txt, ")", 1);
113677 return sqlite3StrAccumFinish(&txt);
113678 }
@@ -113347,10 +114315,11 @@
114315 int regRowid = 0; /* Register holding rowid */
114316 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
114317 int iRetInit; /* Address of regReturn init */
114318 int untestedTerms = 0; /* Some terms not completely tested */
114319 int ii; /* Loop counter */
114320 u16 wctrlFlags; /* Flags for sub-WHERE clause */
114321 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
114322 Table *pTab = pTabItem->pTab;
114323
114324 pTerm = pLoop->aLTerm[0];
114325 assert( pTerm!=0 );
@@ -113442,10 +114411,12 @@
114411
114412 /* Run a separate WHERE clause for each term of the OR clause. After
114413 ** eliminating duplicates from other WHERE clauses, the action for each
114414 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
114415 */
114416 wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
114417 WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY;
114418 for(ii=0; ii<pOrWc->nTerm; ii++){
114419 WhereTerm *pOrTerm = &pOrWc->a[ii];
114420 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
114421 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
114422 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -113454,12 +114425,11 @@
114425 pAndExpr->pLeft = pOrExpr;
114426 pOrExpr = pAndExpr;
114427 }
114428 /* Loop through table entries that match term pOrTerm. */
114429 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
114430 wctrlFlags, iCovCur);
 
114431 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
114432 if( pSubWInfo ){
114433 WhereLoop *pSubLoop;
114434 explainOneScan(
114435 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
@@ -113546,10 +114516,11 @@
114516 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
114517 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
114518 ){
114519 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
114520 pCov = pSubLoop->u.btree.pIndex;
114521 wctrlFlags |= WHERE_REOPEN_IDX;
114522 }else{
114523 pCov = 0;
114524 }
114525
114526 /* Finish the loop through table entries that match term pOrTerm. */
@@ -113724,11 +114695,11 @@
114695 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
114696 }
114697 sqlite3DebugPrintf(" %-19s", z);
114698 sqlite3_free(z);
114699 }
114700 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
114701 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
114702 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
114703 /* If the 0x100 bit of wheretracing is set, then show all of the constraint
114704 ** expressions in the WhereLoop.aLTerm[] array.
114705 */
@@ -113960,10 +114931,21 @@
114931
114932 /* whereLoopAddBtree() always generates and inserts the automatic index
114933 ** case first. Hence compatible candidate WhereLoops never have a larger
114934 ** rSetup. Call this SETUP-INVARIANT */
114935 assert( p->rSetup>=pTemplate->rSetup );
114936
114937 /* Any loop using an appliation-defined index (or PRIMARY KEY or
114938 ** UNIQUE constraint) with one or more == constraints is better
114939 ** than an automatic index. */
114940 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
114941 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
114942 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
114943 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
114944 ){
114945 break;
114946 }
114947
114948 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
114949 ** discarded. WhereLoop p is better if:
114950 ** (1) p has no more dependencies than pTemplate, and
114951 ** (2) p has an equal or lower cost than pTemplate
@@ -114085,17 +115067,17 @@
115067 ** p[] that are also supplated by pTemplate */
115068 WhereLoop **ppTail = &p->pNextLoop;
115069 WhereLoop *pToDel;
115070 while( *ppTail ){
115071 ppTail = whereLoopFindLesser(ppTail, pTemplate);
115072 if( ppTail==0 ) break;
115073 pToDel = *ppTail;
115074 if( pToDel==0 ) break;
115075 *ppTail = pToDel->pNextLoop;
115076 #if WHERETRACE_ENABLED /* 0x8 */
115077 if( sqlite3WhereTrace & 0x8 ){
115078 sqlite3DebugPrintf("ins-del: ");
115079 whereLoopPrint(pToDel, pBuilder->pWC);
115080 }
115081 #endif
115082 whereLoopDelete(db, pToDel);
115083 }
@@ -114141,10 +115123,20 @@
115123 pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
115124 }
115125 }
115126 }
115127
115128 /*
115129 ** Adjust the cost C by the costMult facter T. This only occurs if
115130 ** compiled with -DSQLITE_ENABLE_COSTMULT
115131 */
115132 #ifdef SQLITE_ENABLE_COSTMULT
115133 # define ApplyCostMultiplier(C,T) C += T
115134 #else
115135 # define ApplyCostMultiplier(C,T)
115136 #endif
115137
115138 /*
115139 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
115140 ** index pIndex. Try to match one more.
115141 **
115142 ** When this function is called, pBuilder->pNew->nOut contains the
@@ -114191,16 +115183,13 @@
115183 }else{
115184 opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
115185 }
115186 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
115187
115188 assert( pNew->u.btree.nEq<pProbe->nColumn );
115189 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
115190
 
 
 
115191 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
115192 opMask, pProbe);
115193 saved_nEq = pNew->u.btree.nEq;
115194 saved_nSkip = pNew->u.btree.nSkip;
115195 saved_nLTerm = pNew->nLTerm;
@@ -114279,11 +115268,11 @@
115268 ** changes "x IN (?)" into "x=?". */
115269
115270 }else if( eOp & (WO_EQ) ){
115271 pNew->wsFlags |= WHERE_COLUMN_EQ;
115272 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
115273 if( iCol>=0 && !IsUniqueIndex(pProbe) ){
115274 pNew->wsFlags |= WHERE_UNQ_WANTED;
115275 }else{
115276 pNew->wsFlags |= WHERE_ONEROW;
115277 }
115278 }
@@ -114340,11 +115329,10 @@
115329 testcase( eOp & WO_ISNULL );
115330 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
115331 }else{
115332 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
115333 }
 
115334 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
115335 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
115336 if( nOut ){
115337 pNew->nOut = sqlite3LogEst(nOut);
115338 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
@@ -114372,10 +115360,11 @@
115360 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
115361 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
115362 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
115363 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
115364 }
115365 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
115366
115367 nOutUnadjusted = pNew->nOut;
115368 pNew->rRun += nInMul + nIn;
115369 pNew->nOut += nInMul + nIn;
115370 whereLoopOutputAdjust(pBuilder->pWC, pNew);
@@ -114386,11 +115375,11 @@
115375 }else{
115376 pNew->nOut = nOutUnadjusted;
115377 }
115378
115379 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
115380 && pNew->u.btree.nEq<pProbe->nColumn
115381 ){
115382 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
115383 }
115384 pNew->nOut = saved_nOut;
115385 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -114491,10 +115480,18 @@
115480 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
115481 **
115482 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the
115483 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
115484 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
115485 **
115486 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount
115487 ** of uncertainty. For this reason, scoring is designed to pick plans that
115488 ** "do the least harm" if the estimates are inaccurate. For example, a
115489 ** log(nRow) factor is omitted from a non-covering index scan in order to
115490 ** bias the scoring in favor of using an index, since the worst-case
115491 ** performance of using an index is far better than the worst-case performance
115492 ** of a full table scan.
115493 */
115494 static int whereLoopAddBtree(
115495 WhereLoopBuilder *pBuilder, /* WHERE clause information */
115496 Bitmask mExtra /* Extra prerequesites for using this table */
115497 ){
@@ -114533,10 +115530,11 @@
115530 ** fake index the first in a chain of Index objects with all of the real
115531 ** indices to follow */
115532 Index *pFirst; /* First of real indices on the table */
115533 memset(&sPk, 0, sizeof(Index));
115534 sPk.nKeyCol = 1;
115535 sPk.nColumn = 1;
115536 sPk.aiColumn = &aiColumnPk;
115537 sPk.aiRowLogEst = aiRowEstPk;
115538 sPk.onError = OE_Replace;
115539 sPk.pTable = pTab;
115540 sPk.szIdxRow = pTab->szTabRow;
@@ -114577,10 +115575,11 @@
115575 pNew->aLTerm[0] = pTerm;
115576 /* TUNING: One-time cost for computing the automatic index is
115577 ** approximately 7*N*log2(N) where N is the number of rows in
115578 ** the table being indexed. */
115579 pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) );
115580 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
115581 /* TUNING: Each index lookup yields 20 rows in the table. This
115582 ** is more than the usual guess of 10 rows, since we have no way
115583 ** of knowning how selective the index will ultimately be. It would
115584 ** not be unreasonable to make this value much larger. */
115585 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
@@ -114618,10 +115617,11 @@
115617
115618 /* Full table scan */
115619 pNew->iSortIdx = b ? iSortIdx : 0;
115620 /* TUNING: Cost of full table scan is (N*3.0). */
115621 pNew->rRun = rSize + 16;
115622 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
115623 whereLoopOutputAdjust(pWC, pNew);
115624 rc = whereLoopInsert(pBuilder, pNew);
115625 pNew->nOut = rSize;
115626 if( rc ) break;
115627 }else{
@@ -114653,11 +115653,11 @@
115653 ** also add the cost of visiting table rows (N*3.0). */
115654 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
115655 if( m!=0 ){
115656 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
115657 }
115658 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
115659 whereLoopOutputAdjust(pWC, pNew);
115660 rc = whereLoopInsert(pBuilder, pNew);
115661 pNew->nOut = rSize;
115662 if( rc ) break;
115663 }
@@ -115123,11 +116123,11 @@
116123 }else{
116124 nKeyCol = pIndex->nKeyCol;
116125 nColumn = pIndex->nColumn;
116126 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
116127 assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
116128 isOrderDistinct = IsUniqueIndex(pIndex);
116129 }
116130
116131 /* Loop through all columns of the index and deal with the ones
116132 ** that are not constrained by == or IN.
116133 */
@@ -115291,10 +116291,49 @@
116291 if( pLast ) zName[i++] = pLast->cId;
116292 zName[i] = 0;
116293 return zName;
116294 }
116295 #endif
116296
116297 /*
116298 ** Return the cost of sorting nRow rows, assuming that the keys have
116299 ** nOrderby columns and that the first nSorted columns are already in
116300 ** order.
116301 */
116302 static LogEst whereSortingCost(
116303 WhereInfo *pWInfo,
116304 LogEst nRow,
116305 int nOrderBy,
116306 int nSorted
116307 ){
116308 /* TUNING: Estimated cost of a full external sort, where N is
116309 ** the number of rows to sort is:
116310 **
116311 ** cost = (3.0 * N * log(N)).
116312 **
116313 ** Or, if the order-by clause has X terms but only the last Y
116314 ** terms are out of order, then block-sorting will reduce the
116315 ** sorting cost to:
116316 **
116317 ** cost = (3.0 * N * log(N)) * (Y/X)
116318 **
116319 ** The (Y/X) term is implemented using stack variable rScale
116320 ** below. */
116321 LogEst rScale, rSortCost;
116322 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
116323 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
116324 rSortCost = nRow + estLog(nRow) + rScale + 16;
116325
116326 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
116327 ** similar but with a larger constant of proportionality.
116328 ** Multiply by an additional factor of 3.0. */
116329 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
116330 rSortCost += 16;
116331 }
116332
116333 return rSortCost;
116334 }
116335
116336 /*
116337 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
116338 ** attempts to find the lowest cost path that visits each WhereLoop
116339 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -115313,142 +116352,170 @@
116352 sqlite3 *db; /* The database connection */
116353 int iLoop; /* Loop counter over the terms of the join */
116354 int ii, jj; /* Loop counters */
116355 int mxI = 0; /* Index of next entry to replace */
116356 int nOrderBy; /* Number of ORDER BY clause terms */
 
 
116357 LogEst mxCost = 0; /* Maximum cost of a set of paths */
116358 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
116359 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
116360 WherePath *aFrom; /* All nFrom paths at the previous level */
116361 WherePath *aTo; /* The nTo best paths at the current level */
116362 WherePath *pFrom; /* An element of aFrom[] that we are working on */
116363 WherePath *pTo; /* An element of aTo[] that we are working on */
116364 WhereLoop *pWLoop; /* One of the WhereLoop objects */
116365 WhereLoop **pX; /* Used to divy up the pSpace memory */
116366 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
116367 char *pSpace; /* Temporary memory used by this routine */
116368 int nSpace; /* Bytes of space allocated at pSpace */
116369
116370 pParse = pWInfo->pParse;
116371 db = pParse->db;
116372 nLoop = pWInfo->nLevel;
116373 /* TUNING: For simple queries, only the best path is tracked.
116374 ** For 2-way joins, the 5 best paths are followed.
116375 ** For joins of 3 or more tables, track the 10 best paths */
116376 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
116377 assert( nLoop<=pWInfo->pTabList->nSrc );
116378 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
116379
116380 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
116381 ** case the purpose of this call is to estimate the number of rows returned
116382 ** by the overall query. Once this estimate has been obtained, the caller
116383 ** will invoke this function a second time, passing the estimate as the
116384 ** nRowEst parameter. */
116385 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
116386 nOrderBy = 0;
116387 }else{
116388 nOrderBy = pWInfo->pOrderBy->nExpr;
116389 }
116390
116391 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
116392 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
116393 nSpace += sizeof(LogEst) * nOrderBy;
116394 pSpace = sqlite3DbMallocRaw(db, nSpace);
116395 if( pSpace==0 ) return SQLITE_NOMEM;
116396 aTo = (WherePath*)pSpace;
116397 aFrom = aTo+mxChoice;
116398 memset(aFrom, 0, sizeof(aFrom[0]));
116399 pX = (WhereLoop**)(aFrom+mxChoice);
116400 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
116401 pFrom->aLoop = pX;
116402 }
116403 if( nOrderBy ){
116404 /* If there is an ORDER BY clause and it is not being ignored, set up
116405 ** space for the aSortCost[] array. Each element of the aSortCost array
116406 ** is either zero - meaning it has not yet been initialized - or the
116407 ** cost of sorting nRowEst rows of data where the first X terms of
116408 ** the ORDER BY clause are already in order, where X is the array
116409 ** index. */
116410 aSortCost = (LogEst*)pX;
116411 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
116412 }
116413 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
116414 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
116415
116416 /* Seed the search with a single WherePath containing zero WhereLoops.
116417 **
116418 ** TUNING: Do not let the number of iterations go above 25. If the cost
116419 ** of computing an automatic index is not paid back within the first 25
116420 ** rows, then do not use the automatic index. */
116421 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
116422 nFrom = 1;
116423 assert( aFrom[0].isOrdered==0 );
116424 if( nOrderBy ){
116425 /* If nLoop is zero, then there are no FROM terms in the query. Since
116426 ** in this case the query may return a maximum of one row, the results
116427 ** are already in the requested order. Set isOrdered to nOrderBy to
116428 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
116429 ** -1, indicating that the result set may or may not be ordered,
116430 ** depending on the loops added to the current plan. */
116431 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
116432 }
116433
116434 /* Compute successively longer WherePaths using the previous generation
116435 ** of WherePaths as the basis for the next. Keep track of the mxChoice
116436 ** best paths at each generation */
116437 for(iLoop=0; iLoop<nLoop; iLoop++){
116438 nTo = 0;
116439 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
116440 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
116441 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
116442 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
116443 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
116444 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
116445 Bitmask maskNew; /* Mask of src visited by (..) */
116446 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
116447
116448 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
116449 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
116450 /* At this point, pWLoop is a candidate to be the next loop.
116451 ** Compute its cost */
116452 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
116453 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
116454 nOut = pFrom->nRow + pWLoop->nOut;
116455 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
116456 if( isOrdered<0 ){
116457 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
116458 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
116459 iLoop, pWLoop, &revMask);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116460 }else{
116461 revMask = pFrom->revLoop;
116462 }
116463 if( isOrdered>=0 && isOrdered<nOrderBy ){
116464 if( aSortCost[isOrdered]==0 ){
116465 aSortCost[isOrdered] = whereSortingCost(
116466 pWInfo, nRowEst, nOrderBy, isOrdered
116467 );
116468 }
116469 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
116470
116471 WHERETRACE(0x002,
116472 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
116473 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
116474 rUnsorted, rCost));
116475 }else{
116476 rCost = rUnsorted;
116477 }
116478
116479 /* Check to see if pWLoop should be added to the set of
116480 ** mxChoice best-so-far paths.
116481 **
116482 ** First look for an existing path among best-so-far paths
116483 ** that covers the same set of loops and has the same isOrdered
116484 ** setting as the current path candidate.
116485 **
116486 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
116487 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
116488 ** of legal values for isOrdered, -1..64.
116489 */
116490 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
116491 if( pTo->maskLoop==maskNew
116492 && ((pTo->isOrdered^isOrdered)&0x80)==0
 
 
116493 ){
116494 testcase( jj==nTo-1 );
116495 break;
116496 }
116497 }
116498 if( jj>=nTo ){
116499 /* None of the existing best-so-far paths match the candidate. */
116500 if( nTo>=mxChoice
116501 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
116502 ){
116503 /* The current candidate is no better than any of the mxChoice
116504 ** paths currently in the best-so-far buffer. So discard
116505 ** this candidate as not viable. */
116506 #ifdef WHERETRACE_ENABLED /* 0x4 */
116507 if( sqlite3WhereTrace&0x4 ){
116508 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",
116509 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
116510 isOrdered>=0 ? isOrdered+'0' : '?');
116511 }
116512 #endif
116513 continue;
116514 }
116515 /* If we reach this points it means that the new candidate path
116516 ** needs to be added to the set of best-so-far paths. */
116517 if( nTo<mxChoice ){
116518 /* Increase the size of the aTo set by one */
116519 jj = nTo++;
116520 }else{
116521 /* New path replaces the prior worst to keep count below mxChoice */
@@ -115461,11 +116528,15 @@
116528 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
116529 isOrdered>=0 ? isOrdered+'0' : '?');
116530 }
116531 #endif
116532 }else{
116533 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
116534 ** same set of loops and has the sam isOrdered setting as the
116535 ** candidate path. Check to see if the candidate should replace
116536 ** pTo or if the candidate should be skipped */
116537 if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
116538 #ifdef WHERETRACE_ENABLED /* 0x4 */
116539 if( sqlite3WhereTrace&0x4 ){
116540 sqlite3DebugPrintf(
116541 "Skip %s cost=%-3d,%3d order=%c",
116542 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115473,15 +116544,17 @@
116544 sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n",
116545 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
116546 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
116547 }
116548 #endif
116549 /* Discard the candidate path from further consideration */
116550 testcase( pTo->rCost==rCost );
116551 continue;
116552 }
116553 testcase( pTo->rCost==rCost+1 );
116554 /* Control reaches here if the candidate path is better than the
116555 ** pTo path. Replace pTo with the candidate. */
116556 #ifdef WHERETRACE_ENABLED /* 0x4 */
116557 if( sqlite3WhereTrace&0x4 ){
116558 sqlite3DebugPrintf(
116559 "Update %s cost=%-3d,%3d order=%c",
116560 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
@@ -115495,21 +116568,24 @@
116568 /* pWLoop is a winner. Add it to the set of best so far */
116569 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
116570 pTo->revLoop = revMask;
116571 pTo->nRow = nOut;
116572 pTo->rCost = rCost;
116573 pTo->rUnsorted = rUnsorted;
116574 pTo->isOrdered = isOrdered;
116575 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
116576 pTo->aLoop[iLoop] = pWLoop;
116577 if( nTo>=mxChoice ){
116578 mxI = 0;
116579 mxCost = aTo[0].rCost;
116580 mxUnsorted = aTo[0].nRow;
116581 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
116582 if( pTo->rCost>mxCost
116583 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
116584 ){
116585 mxCost = pTo->rCost;
116586 mxUnsorted = pTo->rUnsorted;
116587 mxI = jj;
116588 }
116589 }
116590 }
116591 }
@@ -115643,11 +116719,11 @@
116719 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
116720 }else{
116721 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116722 assert( pLoop->aLTermSpace==pLoop->aLTerm );
116723 assert( ArraySize(pLoop->aLTermSpace)==4 );
116724 if( !IsUniqueIndex(pIdx)
116725 || pIdx->pPartIdxWhere!=0
116726 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
116727 ) continue;
116728 for(j=0; j<pIdx->nKeyCol; j++){
116729 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
@@ -116133,10 +117209,11 @@
117209 }
117210 op = OP_OpenWrite;
117211 pWInfo->aiCurOnePass[1] = iIndexCur;
117212 }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
117213 iIndexCur = iIdxCur;
117214 if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
117215 }else{
117216 iIndexCur = pParse->nTab++;
117217 }
117218 pLevel->iIdxCur = iIndexCur;
117219 assert( pIx->pSchema==pTab->pSchema );
@@ -120457,10 +121534,16 @@
121534 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
121535 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
121536 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
121537 testcase( z[0]=='9' );
121538 *tokenType = TK_INTEGER;
121539 #ifndef SQLITE_OMIT_HEX_INTEGER
121540 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
121541 for(i=3; sqlite3Isxdigit(z[i]); i++){}
121542 return i;
121543 }
121544 #endif
121545 for(i=0; sqlite3Isdigit(z[i]); i++){}
121546 #ifndef SQLITE_OMIT_FLOATING_POINT
121547 if( z[i]=='.' ){
121548 i++;
121549 while( sqlite3Isdigit(z[i]) ){ i++; }
@@ -121904,10 +122987,12 @@
122987 /*
122988 ** Close an existing SQLite database
122989 */
122990 static int sqlite3Close(sqlite3 *db, int forceZombie){
122991 if( !db ){
122992 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
122993 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
122994 return SQLITE_OK;
122995 }
122996 if( !sqlite3SafetyCheckSickOrOk(db) ){
122997 return SQLITE_MISUSE_BKPT;
122998 }
@@ -122133,11 +123218,11 @@
123218
123219 /*
123220 ** Return a static string containing the name corresponding to the error code
123221 ** specified in the argument.
123222 */
123223 #if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST)
123224 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
123225 const char *zName = 0;
123226 int i, origRc = rc;
123227 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
123228 switch( rc ){
@@ -122168,11 +123253,10 @@
123253 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
123254 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
123255 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
123256 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
123257 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
 
123258 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
123259 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
123260 case SQLITE_IOERR_CHECKRESERVEDLOCK:
123261 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
123262 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
@@ -123153,11 +124237,11 @@
124237 SQLITE_MAX_COMPOUND_SELECT,
124238 SQLITE_MAX_VDBE_OP,
124239 SQLITE_MAX_FUNCTION_ARG,
124240 SQLITE_MAX_ATTACHED,
124241 SQLITE_MAX_LIKE_PATTERN_LENGTH,
124242 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
124243 SQLITE_MAX_TRIGGER_DEPTH,
124244 };
124245
124246 /*
124247 ** Make sure the hard limits are set to reasonable values
@@ -123178,12 +124262,12 @@
124262 # error SQLITE_MAX_VDBE_OP must be at least 40
124263 #endif
124264 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
124265 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
124266 #endif
124267 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
124268 # error SQLITE_MAX_ATTACHED must be between 0 and 125
124269 #endif
124270 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
124271 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
124272 #endif
124273 #if SQLITE_MAX_COLUMN>32767
@@ -124205,14 +125289,14 @@
125289 ** sqlite3_test_control().
125290 */
125291 case SQLITE_TESTCTRL_FAULT_INSTALL: {
125292 /* MSVC is picky about pulling func ptrs from va lists.
125293 ** http://support.microsoft.com/kb/47961
125294 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
125295 */
125296 typedef int(*TESTCALLBACKFUNC_t)(int);
125297 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
125298 rc = sqlite3FaultSim(0);
125299 break;
125300 }
125301
125302 /*
@@ -124438,10 +125522,20 @@
125522 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
125523 #endif
125524 break;
125525 }
125526
125527 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
125528 **
125529 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
125530 ** not.
125531 */
125532 case SQLITE_TESTCTRL_ISINIT: {
125533 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
125534 break;
125535 }
125536
125537 }
125538 va_end(ap);
125539 #endif /* SQLITE_OMIT_BUILTIN_TEST */
125540 return rc;
125541 }
@@ -124486,11 +125580,11 @@
125580 const char *zParam, /* URI parameter sought */
125581 sqlite3_int64 bDflt /* return if parameter is missing */
125582 ){
125583 const char *z = sqlite3_uri_parameter(zFilename, zParam);
125584 sqlite3_int64 v;
125585 if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
125586 bDflt = v;
125587 }
125588 return bDflt;
125589 }
125590
@@ -126017,11 +127111,11 @@
127111
127112 /* fts3_tokenize_vtab.c */
127113 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
127114
127115 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
127116 #ifndef SQLITE_DISABLE_FTS3_UNICODE
127117 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
127118 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
127119 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
127120 #endif
127121
@@ -129487,11 +130581,11 @@
130581 ** to by the argument to point to the "simple" tokenizer implementation.
130582 ** And so on.
130583 */
130584 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
130585 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
130586 #ifndef SQLITE_DISABLE_FTS3_UNICODE
130587 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
130588 #endif
130589 #ifdef SQLITE_ENABLE_ICU
130590 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
130591 #endif
@@ -129505,20 +130599,20 @@
130599 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
130600 int rc = SQLITE_OK;
130601 Fts3Hash *pHash = 0;
130602 const sqlite3_tokenizer_module *pSimple = 0;
130603 const sqlite3_tokenizer_module *pPorter = 0;
130604 #ifndef SQLITE_DISABLE_FTS3_UNICODE
130605 const sqlite3_tokenizer_module *pUnicode = 0;
130606 #endif
130607
130608 #ifdef SQLITE_ENABLE_ICU
130609 const sqlite3_tokenizer_module *pIcu = 0;
130610 sqlite3Fts3IcuTokenizerModule(&pIcu);
130611 #endif
130612
130613 #ifndef SQLITE_DISABLE_FTS3_UNICODE
130614 sqlite3Fts3UnicodeTokenizer(&pUnicode);
130615 #endif
130616
130617 #ifdef SQLITE_TEST
130618 rc = sqlite3Fts3InitTerm(db);
@@ -129542,11 +130636,11 @@
130636 /* Load the built-in tokenizers into the hash table */
130637 if( rc==SQLITE_OK ){
130638 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
130639 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
130640
130641 #ifndef SQLITE_DISABLE_FTS3_UNICODE
130642 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
130643 #endif
130644 #ifdef SQLITE_ENABLE_ICU
130645 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
130646 #endif
@@ -140777,38 +141871,40 @@
141871 i64 iDocid = sqlite3_column_int64(pStmt, 0);
141872 int iLang = langidFromSelect(p, pStmt);
141873 int iCol;
141874
141875 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
141876 if( p->abNotindexed[iCol]==0 ){
141877 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
141878 int nText = sqlite3_column_bytes(pStmt, iCol+1);
141879 sqlite3_tokenizer_cursor *pT = 0;
141880
141881 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
141882 while( rc==SQLITE_OK ){
141883 char const *zToken; /* Buffer containing token */
141884 int nToken = 0; /* Number of bytes in token */
141885 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
141886 int iPos = 0; /* Position of token in zText */
141887
141888 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
141889 if( rc==SQLITE_OK ){
141890 int i;
141891 cksum2 = cksum2 ^ fts3ChecksumEntry(
141892 zToken, nToken, iLang, 0, iDocid, iCol, iPos
141893 );
141894 for(i=1; i<p->nIndex; i++){
141895 if( p->aIndex[i].nPrefix<=nToken ){
141896 cksum2 = cksum2 ^ fts3ChecksumEntry(
141897 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
141898 );
141899 }
141900 }
141901 }
141902 }
141903 if( pT ) pModule->xClose(pT);
141904 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
141905 }
141906 }
141907 }
141908
141909 sqlite3_finalize(pStmt);
141910 }
@@ -142800,11 +143896,11 @@
143896 ******************************************************************************
143897 **
143898 ** Implementation of the "unicode" full-text-search tokenizer.
143899 */
143900
143901 #ifndef SQLITE_DISABLE_FTS3_UNICODE
143902
143903 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143904
143905 /* #include <assert.h> */
143906 /* #include <stdlib.h> */
@@ -143016,11 +144112,11 @@
144112 memset(pNew, 0, sizeof(unicode_tokenizer));
144113 pNew->bRemoveDiacritic = 1;
144114
144115 for(i=0; rc==SQLITE_OK && i<nArg; i++){
144116 const char *z = azArg[i];
144117 int n = (int)strlen(z);
144118
144119 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
144120 pNew->bRemoveDiacritic = 1;
144121 }
144122 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
@@ -143103,11 +144199,11 @@
144199 int *piEnd, /* OUT: Ending offset of token */
144200 int *piPos /* OUT: Position integer of token */
144201 ){
144202 unicode_cursor *pCsr = (unicode_cursor *)pC;
144203 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
144204 int iCode = 0;
144205 char *zOut;
144206 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
144207 const unsigned char *zStart = z;
144208 const unsigned char *zEnd;
144209 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
@@ -143148,15 +144244,15 @@
144244 }while( unicodeIsAlnum(p, iCode)
144245 || sqlite3FtsUnicodeIsdiacritic(iCode)
144246 );
144247
144248 /* Set the output variables and return. */
144249 pCsr->iOff = (int)(z - pCsr->aInput);
144250 *paToken = pCsr->zToken;
144251 *pnToken = (int)(zOut - pCsr->zToken);
144252 *piStart = (int)(zStart - pCsr->aInput);
144253 *piEnd = (int)(zEnd - pCsr->aInput);
144254 *piPos = pCsr->iToken++;
144255 return SQLITE_OK;
144256 }
144257
144258 /*
@@ -143175,11 +144271,11 @@
144271 };
144272 *ppModule = &module;
144273 }
144274
144275 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
144276 #endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
144277
144278 /************** End of fts3_unicode.c ****************************************/
144279 /************** Begin file fts3_unicode2.c ***********************************/
144280 /*
144281 ** 2012 May 25
@@ -143196,11 +144292,11 @@
144292
144293 /*
144294 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
144295 */
144296
144297 #ifndef SQLITE_DISABLE_FTS3_UNICODE
144298 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
144299
144300 /* #include <assert.h> */
144301
144302 /*
@@ -143220,11 +144316,11 @@
144316 ** the size of the range (always at least 1). In other words, the value
144317 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
144318 ** C. It is not possible to represent a range larger than 1023 codepoints
144319 ** using this format.
144320 */
144321 static const unsigned int aEntry[] = {
144322 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
144323 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
144324 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
144325 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
144326 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
@@ -143312,11 +144408,11 @@
144408
144409 if( c<128 ){
144410 return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
144411 }else if( c<(1<<22) ){
144412 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
144413 int iRes = 0;
144414 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
144415 int iLo = 0;
144416 while( iHi>=iLo ){
144417 int iTest = (iHi + iLo) / 2;
144418 if( key >= aEntry[iTest] ){
@@ -143383,11 +144479,11 @@
144479 iHi = iTest-1;
144480 }
144481 }
144482 assert( key>=aDia[iRes] );
144483 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
144484 }
144485
144486
144487 /*
144488 ** Return true if the argument interpreted as a unicode codepoint
144489 ** is a diacritical modifier character.
@@ -143543,11 +144639,11 @@
144639 }
144640
144641 return ret;
144642 }
144643 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
144644 #endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
144645
144646 /************** End of fts3_unicode2.c ***************************************/
144647 /************** Begin file rtree.c *******************************************/
144648 /*
144649 ** 2001 September 15
@@ -145080,13 +146176,17 @@
146176 int rc = SQLITE_OK;
146177 int iCell = 0;
146178
146179 rtreeReference(pRtree);
146180
146181 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
146182 freeCursorConstraints(pCsr);
146183 sqlite3_free(pCsr->aPoint);
146184 memset(pCsr, 0, sizeof(RtreeCursor));
146185 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
146186
146187 pCsr->iStrategy = idxNum;
 
146188 if( idxNum==1 ){
146189 /* Special case - lookup by rowid. */
146190 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
146191 RtreeSearchPoint *p; /* Search point for the the leaf */
146192 i64 iRowid = sqlite3_value_int64(argv[0]);
146193
+75 -59
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.8.5"
111
-#define SQLITE_VERSION_NUMBER 3008005
112
-#define SQLITE_SOURCE_ID "2014-06-04 14:06:34 b1ed4f2a34ba66c29b130f8d13e9092758019212"
110
+#define SQLITE_VERSION "3.8.6"
111
+#define SQLITE_VERSION_NUMBER 3008006
112
+#define SQLITE_SOURCE_ID "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -267,19 +267,19 @@
267267
/*
268268
** CAPI3REF: Closing A Database Connection
269269
**
270270
** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
271271
** for the [sqlite3] object.
272
-** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
272
+** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
273273
** the [sqlite3] object is successfully destroyed and all associated
274274
** resources are deallocated.
275275
**
276276
** ^If the database connection is associated with unfinalized prepared
277277
** statements or unfinished sqlite3_backup objects then sqlite3_close()
278278
** will leave the database connection open and return [SQLITE_BUSY].
279279
** ^If sqlite3_close_v2() is called with unfinalized prepared statements
280
-** and unfinished sqlite3_backups, then the database connection becomes
280
+** and/or unfinished sqlite3_backups, then the database connection becomes
281281
** an unusable "zombie" which will automatically be deallocated when the
282282
** last prepared statement is finalized or the last sqlite3_backup is
283283
** finished. The sqlite3_close_v2() interface is intended for use with
284284
** host languages that are garbage collected, and where the order in which
285285
** destructors are called is arbitrary.
@@ -288,11 +288,11 @@
288288
** [sqlite3_blob_close | close] all [BLOB handles], and
289289
** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
290290
** with the [sqlite3] object prior to attempting to close the object. ^If
291291
** sqlite3_close_v2() is called on a [database connection] that still has
292292
** outstanding [prepared statements], [BLOB handles], and/or
293
-** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
293
+** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
294294
** of resources is deferred until all [prepared statements], [BLOB handles],
295295
** and [sqlite3_backup] objects are also destroyed.
296296
**
297297
** ^If an [sqlite3] object is destroyed while a transaction is open,
298298
** the transaction is automatically rolled back.
@@ -384,20 +384,18 @@
384384
char **errmsg /* Error msg written here */
385385
);
386386
387387
/*
388388
** CAPI3REF: Result Codes
389
-** KEYWORDS: SQLITE_OK {error code} {error codes}
390
-** KEYWORDS: {result code} {result codes}
389
+** KEYWORDS: {result code definitions}
391390
**
392391
** Many SQLite functions return an integer result code from the set shown
393392
** here in order to indicate success or failure.
394393
**
395394
** New error codes may be added in future versions of SQLite.
396395
**
397
-** See also: [SQLITE_IOERR_READ | extended result codes],
398
-** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
396
+** See also: [extended result code definitions]
399397
*/
400398
#define SQLITE_OK 0 /* Successful result */
401399
/* beginning-of-error-codes */
402400
#define SQLITE_ERROR 1 /* SQL error or missing database */
403401
#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -431,30 +429,23 @@
431429
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
432430
/* end-of-error-codes */
433431
434432
/*
435433
** CAPI3REF: Extended Result Codes
436
-** KEYWORDS: {extended error code} {extended error codes}
437
-** KEYWORDS: {extended result code} {extended result codes}
434
+** KEYWORDS: {extended result code definitions}
438435
**
439
-** In its default configuration, SQLite API routines return one of 26 integer
440
-** [SQLITE_OK | result codes]. However, experience has shown that many of
436
+** In its default configuration, SQLite API routines return one of 30 integer
437
+** [result codes]. However, experience has shown that many of
441438
** these result codes are too coarse-grained. They do not provide as
442439
** much information about problems as programmers might like. In an effort to
443440
** address this, newer versions of SQLite (version 3.3.8 and later) include
444441
** support for additional result codes that provide more detailed information
445
-** about errors. The extended result codes are enabled or disabled
442
+** about errors. These [extended result codes] are enabled or disabled
446443
** on a per database connection basis using the
447
-** [sqlite3_extended_result_codes()] API.
448
-**
449
-** Some of the available extended result codes are listed here.
450
-** One may expect the number of extended result codes will increase
451
-** over time. Software that uses extended result codes should expect
452
-** to see new result codes in future releases of SQLite.
453
-**
454
-** The SQLITE_OK result code will never be extended. It will always
455
-** be exactly zero.
444
+** [sqlite3_extended_result_codes()] API. Or, the extended code for
445
+** the most recent error can be obtained using
446
+** [sqlite3_extended_errcode()].
456447
*/
457448
#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
458449
#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
459450
#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
460451
#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -683,11 +674,11 @@
683674
** write return values. Potential uses for xFileControl() might be
684675
** functions to enable blocking locks with timeouts, to change the
685676
** locking strategy (for example to use dot-file locks), to inquire
686677
** about the status of a lock, or to break stale locks. The SQLite
687678
** core reserves all opcodes less than 100 for its own use.
688
-** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
679
+** A [file control opcodes | list of opcodes] less than 100 is available.
689680
** Applications that define a custom xFileControl method should use opcodes
690681
** greater than 100 to avoid conflicts. VFS implementations should
691682
** return [SQLITE_NOTFOUND] for file control opcodes that they do not
692683
** recognize.
693684
**
@@ -756,10 +747,11 @@
756747
/* Additional methods may be added in future releases */
757748
};
758749
759750
/*
760751
** CAPI3REF: Standard File Control Opcodes
752
+** KEYWORDS: {file control opcodes} {file control opcode}
761753
**
762754
** These integer constants are opcodes for the xFileControl method
763755
** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
764756
** interface.
765757
**
@@ -2035,31 +2027,37 @@
20352027
SQLITE_API int sqlite3_complete16(const void *sql);
20362028
20372029
/*
20382030
** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
20392031
**
2040
-** ^This routine sets a callback function that might be invoked whenever
2041
-** an attempt is made to open a database table that another thread
2042
-** or process has locked.
2032
+** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2033
+** that might be invoked with argument P whenever
2034
+** an attempt is made to access a database table associated with
2035
+** [database connection] D when another thread
2036
+** or process has the table locked.
2037
+** The sqlite3_busy_handler() interface is used to implement
2038
+** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
20432039
**
2044
-** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2040
+** ^If the busy callback is NULL, then [SQLITE_BUSY]
20452041
** is returned immediately upon encountering the lock. ^If the busy callback
20462042
** is not NULL, then the callback might be invoked with two arguments.
20472043
**
20482044
** ^The first argument to the busy handler is a copy of the void* pointer which
20492045
** is the third argument to sqlite3_busy_handler(). ^The second argument to
20502046
** the busy handler callback is the number of times that the busy handler has
2051
-** been invoked for this locking event. ^If the
2047
+** been invoked for the same locking event. ^If the
20522048
** busy callback returns 0, then no additional attempts are made to
2053
-** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2049
+** access the database and [SQLITE_BUSY] is returned
2050
+** to the application.
20542051
** ^If the callback returns non-zero, then another attempt
2055
-** is made to open the database for reading and the cycle repeats.
2052
+** is made to access the database and the cycle repeats.
20562053
**
20572054
** The presence of a busy handler does not guarantee that it will be invoked
20582055
** when there is lock contention. ^If SQLite determines that invoking the busy
20592056
** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2060
-** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2057
+** to the application instead of invoking the
2058
+** busy handler.
20612059
** Consider a scenario where one process is holding a read lock that
20622060
** it is trying to promote to a reserved lock and
20632061
** a second process is holding a reserved lock that it is trying
20642062
** to promote to an exclusive lock. The first process cannot proceed
20652063
** because it is blocked by the second and the second process cannot
@@ -2069,32 +2067,19 @@
20692067
** will induce the first process to release its read lock and allow
20702068
** the second process to proceed.
20712069
**
20722070
** ^The default busy callback is NULL.
20732071
**
2074
-** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2075
-** when SQLite is in the middle of a large transaction where all the
2076
-** changes will not fit into the in-memory cache. SQLite will
2077
-** already hold a RESERVED lock on the database file, but it needs
2078
-** to promote this lock to EXCLUSIVE so that it can spill cache
2079
-** pages into the database file without harm to concurrent
2080
-** readers. ^If it is unable to promote the lock, then the in-memory
2081
-** cache will be left in an inconsistent state and so the error
2082
-** code is promoted from the relatively benign [SQLITE_BUSY] to
2083
-** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
2084
-** forces an automatic rollback of the changes. See the
2085
-** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2086
-** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2087
-** this is important.
2088
-**
20892072
** ^(There can only be a single busy handler defined for each
20902073
** [database connection]. Setting a new busy handler clears any
20912074
** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2092
-** will also set or clear the busy handler.
2075
+** or evaluating [PRAGMA busy_timeout=N] will change the
2076
+** busy handler and thus clear any previously set busy handler.
20932077
**
20942078
** The busy callback should not take any actions which modify the
2095
-** database connection that invoked the busy handler. Any such actions
2079
+** database connection that invoked the busy handler. In other words,
2080
+** the busy handler is not reentrant. Any such actions
20962081
** result in undefined behavior.
20972082
**
20982083
** A busy handler must not close the database connection
20992084
** or [prepared statement] that invoked the busy handler.
21002085
*/
@@ -2106,19 +2091,21 @@
21062091
** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
21072092
** for a specified amount of time when a table is locked. ^The handler
21082093
** will sleep multiple times until at least "ms" milliseconds of sleeping
21092094
** have accumulated. ^After at least "ms" milliseconds of sleeping,
21102095
** the handler returns 0 which causes [sqlite3_step()] to return
2111
-** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2096
+** [SQLITE_BUSY].
21122097
**
21132098
** ^Calling this routine with an argument less than or equal to zero
21142099
** turns off all busy handlers.
21152100
**
21162101
** ^(There can only be a single busy handler for a particular
21172102
** [database connection] any any given moment. If another busy handler
21182103
** was defined (using [sqlite3_busy_handler()]) prior to calling
21192104
** this routine, that other busy handler is cleared.)^
2105
+**
2106
+** See also: [PRAGMA busy_timeout]
21202107
*/
21212108
SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
21222109
21232110
/*
21242111
** CAPI3REF: Convenience Routines For Running Queries
@@ -2516,12 +2503,12 @@
25162503
** return either [SQLITE_OK] or one of these two constants in order
25172504
** to signal SQLite whether or not the action is permitted. See the
25182505
** [sqlite3_set_authorizer | authorizer documentation] for additional
25192506
** information.
25202507
**
2521
-** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2522
-** from the [sqlite3_vtab_on_conflict()] interface.
2508
+** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
2509
+** returned from the [sqlite3_vtab_on_conflict()] interface.
25232510
*/
25242511
#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
25252512
#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
25262513
25272514
/*
@@ -4703,10 +4690,17 @@
47034690
** the name of a folder (a.k.a. directory), then all temporary files
47044691
** created by SQLite when using a built-in [sqlite3_vfs | VFS]
47054692
** will be placed in that directory.)^ ^If this variable
47064693
** is a NULL pointer, then SQLite performs a search for an appropriate
47074694
** temporary file directory.
4695
+**
4696
+** Applications are strongly discouraged from using this global variable.
4697
+** It is required to set a temporary folder on Windows Runtime (WinRT).
4698
+** But for all other platforms, it is highly recommended that applications
4699
+** neither read nor write this variable. This global variable is a relic
4700
+** that exists for backwards compatibility of legacy applications and should
4701
+** be avoided in new projects.
47084702
**
47094703
** It is not safe to read or modify this variable in more than one
47104704
** thread at a time. It is not safe to read or modify this variable
47114705
** if a [database connection] is being used at the same time in a separate
47124706
** thread.
@@ -4722,10 +4716,15 @@
47224716
** [sqlite3_malloc] and the pragma may attempt to free that memory
47234717
** using [sqlite3_free].
47244718
** Hence, if this variable is modified directly, either it should be
47254719
** made NULL or made to point to memory obtained from [sqlite3_malloc]
47264720
** or else the use of the [temp_store_directory pragma] should be avoided.
4721
+** Except when requested by the [temp_store_directory pragma], SQLite
4722
+** does not free the memory that sqlite3_temp_directory points to. If
4723
+** the application wants that memory to be freed, it must do
4724
+** so itself, taking care to only do so after all [database connection]
4725
+** objects have been destroyed.
47274726
**
47284727
** <b>Note to Windows Runtime users:</b> The temporary directory must be set
47294728
** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
47304729
** features that require the use of temporary files may fail. Here is an
47314730
** example of how to do this using C++ with the Windows Runtime:
@@ -5856,14 +5855,16 @@
58565855
** <ul>
58575856
** <li> SQLITE_MUTEX_FAST
58585857
** <li> SQLITE_MUTEX_RECURSIVE
58595858
** <li> SQLITE_MUTEX_STATIC_MASTER
58605859
** <li> SQLITE_MUTEX_STATIC_MEM
5861
-** <li> SQLITE_MUTEX_STATIC_MEM2
5860
+** <li> SQLITE_MUTEX_STATIC_OPEN
58625861
** <li> SQLITE_MUTEX_STATIC_PRNG
58635862
** <li> SQLITE_MUTEX_STATIC_LRU
5864
-** <li> SQLITE_MUTEX_STATIC_LRU2
5863
+** <li> SQLITE_MUTEX_STATIC_PMEM
5864
+** <li> SQLITE_MUTEX_STATIC_APP1
5865
+** <li> SQLITE_MUTEX_STATIC_APP2
58655866
** </ul>)^
58665867
**
58675868
** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
58685869
** cause sqlite3_mutex_alloc() to create
58695870
** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6063,10 +6064,13 @@
60636064
#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
60646065
#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
60656066
#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
60666067
#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
60676068
#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
6069
+#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
6070
+#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
6071
+#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
60686072
60696073
/*
60706074
** CAPI3REF: Retrieve the mutex for a database connection
60716075
**
60726076
** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6158,11 +6162,12 @@
61586162
#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
61596163
#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
61606164
#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
61616165
#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
61626166
#define SQLITE_TESTCTRL_BYTEORDER 22
6163
-#define SQLITE_TESTCTRL_LAST 22
6167
+#define SQLITE_TESTCTRL_ISINIT 23
6168
+#define SQLITE_TESTCTRL_LAST 23
61646169
61656170
/*
61666171
** CAPI3REF: SQLite Runtime Status
61676172
**
61686173
** ^This interface is used to retrieve runtime status information
@@ -7141,10 +7146,13 @@
71417146
** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
71427147
** configured by this function.
71437148
**
71447149
** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
71457150
** from SQL.
7151
+**
7152
+** ^Checkpoints initiated by this mechanism are
7153
+** [sqlite3_wal_checkpoint_v2|PASSIVE].
71467154
**
71477155
** ^Every new [database connection] defaults to having the auto-checkpoint
71487156
** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
71497157
** pages. The use of this interface
71507158
** is only necessary if the default setting is found to be suboptimal
@@ -7158,10 +7166,14 @@
71587166
** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
71597167
** on [database connection] D to be [checkpointed]. ^If X is NULL or an
71607168
** empty string, then a checkpoint is run on all databases of
71617169
** connection D. ^If the database connection D is not in
71627170
** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7171
+** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
7172
+** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
7173
+** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
7174
+** or RESET checkpoint.
71637175
**
71647176
** ^The [wal_checkpoint pragma] can be used to invoke this interface
71657177
** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
71667178
** [wal_autocheckpoint pragma] can be used to cause this interface to be
71677179
** run whenever the WAL reaches a certain size threshold.
@@ -7180,22 +7192,25 @@
71807192
** <dl>
71817193
** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
71827194
** Checkpoint as many frames as possible without waiting for any database
71837195
** readers or writers to finish. Sync the db file if all frames in the log
71847196
** are checkpointed. This mode is the same as calling
7185
-** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7197
+** sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
7198
+** is never invoked.
71867199
**
71877200
** <dt>SQLITE_CHECKPOINT_FULL<dd>
7188
-** This mode blocks (calls the busy-handler callback) until there is no
7201
+** This mode blocks (it invokes the
7202
+** [sqlite3_busy_handler|busy-handler callback]) until there is no
71897203
** database writer and all readers are reading from the most recent database
71907204
** snapshot. It then checkpoints all frames in the log file and syncs the
71917205
** database file. This call blocks database writers while it is running,
71927206
** but not database readers.
71937207
**
71947208
** <dt>SQLITE_CHECKPOINT_RESTART<dd>
71957209
** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7196
-** checkpointing the log file it blocks (calls the busy-handler callback)
7210
+** checkpointing the log file it blocks (calls the
7211
+** [sqlite3_busy_handler|busy-handler callback])
71977212
** until all readers are reading from the database file only. This ensures
71987213
** that the next client to write to the database file restarts the log file
71997214
** from the beginning. This call blocks database writers while it is running,
72007215
** but not database readers.
72017216
** </dl>
@@ -7329,10 +7344,11 @@
73297344
*/
73307345
SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
73317346
73327347
/*
73337348
** CAPI3REF: Conflict resolution modes
7349
+** KEYWORDS: {conflict resolution mode}
73347350
**
73357351
** These constants are returned by [sqlite3_vtab_on_conflict()] to
73367352
** inform a [virtual table] implementation what the [ON CONFLICT] mode
73377353
** is for the SQL statement being evaluated.
73387354
**
73397355
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.5"
111 #define SQLITE_VERSION_NUMBER 3008005
112 #define SQLITE_SOURCE_ID "2014-06-04 14:06:34 b1ed4f2a34ba66c29b130f8d13e9092758019212"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -267,19 +267,19 @@
267 /*
268 ** CAPI3REF: Closing A Database Connection
269 **
270 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
271 ** for the [sqlite3] object.
272 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
273 ** the [sqlite3] object is successfully destroyed and all associated
274 ** resources are deallocated.
275 **
276 ** ^If the database connection is associated with unfinalized prepared
277 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
278 ** will leave the database connection open and return [SQLITE_BUSY].
279 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
280 ** and unfinished sqlite3_backups, then the database connection becomes
281 ** an unusable "zombie" which will automatically be deallocated when the
282 ** last prepared statement is finalized or the last sqlite3_backup is
283 ** finished. The sqlite3_close_v2() interface is intended for use with
284 ** host languages that are garbage collected, and where the order in which
285 ** destructors are called is arbitrary.
@@ -288,11 +288,11 @@
288 ** [sqlite3_blob_close | close] all [BLOB handles], and
289 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
290 ** with the [sqlite3] object prior to attempting to close the object. ^If
291 ** sqlite3_close_v2() is called on a [database connection] that still has
292 ** outstanding [prepared statements], [BLOB handles], and/or
293 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
294 ** of resources is deferred until all [prepared statements], [BLOB handles],
295 ** and [sqlite3_backup] objects are also destroyed.
296 **
297 ** ^If an [sqlite3] object is destroyed while a transaction is open,
298 ** the transaction is automatically rolled back.
@@ -384,20 +384,18 @@
384 char **errmsg /* Error msg written here */
385 );
386
387 /*
388 ** CAPI3REF: Result Codes
389 ** KEYWORDS: SQLITE_OK {error code} {error codes}
390 ** KEYWORDS: {result code} {result codes}
391 **
392 ** Many SQLite functions return an integer result code from the set shown
393 ** here in order to indicate success or failure.
394 **
395 ** New error codes may be added in future versions of SQLite.
396 **
397 ** See also: [SQLITE_IOERR_READ | extended result codes],
398 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
399 */
400 #define SQLITE_OK 0 /* Successful result */
401 /* beginning-of-error-codes */
402 #define SQLITE_ERROR 1 /* SQL error or missing database */
403 #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -431,30 +429,23 @@
431 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
432 /* end-of-error-codes */
433
434 /*
435 ** CAPI3REF: Extended Result Codes
436 ** KEYWORDS: {extended error code} {extended error codes}
437 ** KEYWORDS: {extended result code} {extended result codes}
438 **
439 ** In its default configuration, SQLite API routines return one of 26 integer
440 ** [SQLITE_OK | result codes]. However, experience has shown that many of
441 ** these result codes are too coarse-grained. They do not provide as
442 ** much information about problems as programmers might like. In an effort to
443 ** address this, newer versions of SQLite (version 3.3.8 and later) include
444 ** support for additional result codes that provide more detailed information
445 ** about errors. The extended result codes are enabled or disabled
446 ** on a per database connection basis using the
447 ** [sqlite3_extended_result_codes()] API.
448 **
449 ** Some of the available extended result codes are listed here.
450 ** One may expect the number of extended result codes will increase
451 ** over time. Software that uses extended result codes should expect
452 ** to see new result codes in future releases of SQLite.
453 **
454 ** The SQLITE_OK result code will never be extended. It will always
455 ** be exactly zero.
456 */
457 #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
458 #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
459 #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
460 #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -683,11 +674,11 @@
683 ** write return values. Potential uses for xFileControl() might be
684 ** functions to enable blocking locks with timeouts, to change the
685 ** locking strategy (for example to use dot-file locks), to inquire
686 ** about the status of a lock, or to break stale locks. The SQLite
687 ** core reserves all opcodes less than 100 for its own use.
688 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
689 ** Applications that define a custom xFileControl method should use opcodes
690 ** greater than 100 to avoid conflicts. VFS implementations should
691 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
692 ** recognize.
693 **
@@ -756,10 +747,11 @@
756 /* Additional methods may be added in future releases */
757 };
758
759 /*
760 ** CAPI3REF: Standard File Control Opcodes
 
761 **
762 ** These integer constants are opcodes for the xFileControl method
763 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
764 ** interface.
765 **
@@ -2035,31 +2027,37 @@
2035 SQLITE_API int sqlite3_complete16(const void *sql);
2036
2037 /*
2038 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2039 **
2040 ** ^This routine sets a callback function that might be invoked whenever
2041 ** an attempt is made to open a database table that another thread
2042 ** or process has locked.
 
 
 
 
2043 **
2044 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2045 ** is returned immediately upon encountering the lock. ^If the busy callback
2046 ** is not NULL, then the callback might be invoked with two arguments.
2047 **
2048 ** ^The first argument to the busy handler is a copy of the void* pointer which
2049 ** is the third argument to sqlite3_busy_handler(). ^The second argument to
2050 ** the busy handler callback is the number of times that the busy handler has
2051 ** been invoked for this locking event. ^If the
2052 ** busy callback returns 0, then no additional attempts are made to
2053 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
 
2054 ** ^If the callback returns non-zero, then another attempt
2055 ** is made to open the database for reading and the cycle repeats.
2056 **
2057 ** The presence of a busy handler does not guarantee that it will be invoked
2058 ** when there is lock contention. ^If SQLite determines that invoking the busy
2059 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2060 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
 
2061 ** Consider a scenario where one process is holding a read lock that
2062 ** it is trying to promote to a reserved lock and
2063 ** a second process is holding a reserved lock that it is trying
2064 ** to promote to an exclusive lock. The first process cannot proceed
2065 ** because it is blocked by the second and the second process cannot
@@ -2069,32 +2067,19 @@
2069 ** will induce the first process to release its read lock and allow
2070 ** the second process to proceed.
2071 **
2072 ** ^The default busy callback is NULL.
2073 **
2074 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2075 ** when SQLite is in the middle of a large transaction where all the
2076 ** changes will not fit into the in-memory cache. SQLite will
2077 ** already hold a RESERVED lock on the database file, but it needs
2078 ** to promote this lock to EXCLUSIVE so that it can spill cache
2079 ** pages into the database file without harm to concurrent
2080 ** readers. ^If it is unable to promote the lock, then the in-memory
2081 ** cache will be left in an inconsistent state and so the error
2082 ** code is promoted from the relatively benign [SQLITE_BUSY] to
2083 ** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
2084 ** forces an automatic rollback of the changes. See the
2085 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2086 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2087 ** this is important.
2088 **
2089 ** ^(There can only be a single busy handler defined for each
2090 ** [database connection]. Setting a new busy handler clears any
2091 ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2092 ** will also set or clear the busy handler.
 
2093 **
2094 ** The busy callback should not take any actions which modify the
2095 ** database connection that invoked the busy handler. Any such actions
 
2096 ** result in undefined behavior.
2097 **
2098 ** A busy handler must not close the database connection
2099 ** or [prepared statement] that invoked the busy handler.
2100 */
@@ -2106,19 +2091,21 @@
2106 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2107 ** for a specified amount of time when a table is locked. ^The handler
2108 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2109 ** have accumulated. ^After at least "ms" milliseconds of sleeping,
2110 ** the handler returns 0 which causes [sqlite3_step()] to return
2111 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2112 **
2113 ** ^Calling this routine with an argument less than or equal to zero
2114 ** turns off all busy handlers.
2115 **
2116 ** ^(There can only be a single busy handler for a particular
2117 ** [database connection] any any given moment. If another busy handler
2118 ** was defined (using [sqlite3_busy_handler()]) prior to calling
2119 ** this routine, that other busy handler is cleared.)^
 
 
2120 */
2121 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2122
2123 /*
2124 ** CAPI3REF: Convenience Routines For Running Queries
@@ -2516,12 +2503,12 @@
2516 ** return either [SQLITE_OK] or one of these two constants in order
2517 ** to signal SQLite whether or not the action is permitted. See the
2518 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2519 ** information.
2520 **
2521 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2522 ** from the [sqlite3_vtab_on_conflict()] interface.
2523 */
2524 #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
2525 #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
2526
2527 /*
@@ -4703,10 +4690,17 @@
4703 ** the name of a folder (a.k.a. directory), then all temporary files
4704 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4705 ** will be placed in that directory.)^ ^If this variable
4706 ** is a NULL pointer, then SQLite performs a search for an appropriate
4707 ** temporary file directory.
 
 
 
 
 
 
 
4708 **
4709 ** It is not safe to read or modify this variable in more than one
4710 ** thread at a time. It is not safe to read or modify this variable
4711 ** if a [database connection] is being used at the same time in a separate
4712 ** thread.
@@ -4722,10 +4716,15 @@
4722 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4723 ** using [sqlite3_free].
4724 ** Hence, if this variable is modified directly, either it should be
4725 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4726 ** or else the use of the [temp_store_directory pragma] should be avoided.
 
 
 
 
 
4727 **
4728 ** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4729 ** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
4730 ** features that require the use of temporary files may fail. Here is an
4731 ** example of how to do this using C++ with the Windows Runtime:
@@ -5856,14 +5855,16 @@
5856 ** <ul>
5857 ** <li> SQLITE_MUTEX_FAST
5858 ** <li> SQLITE_MUTEX_RECURSIVE
5859 ** <li> SQLITE_MUTEX_STATIC_MASTER
5860 ** <li> SQLITE_MUTEX_STATIC_MEM
5861 ** <li> SQLITE_MUTEX_STATIC_MEM2
5862 ** <li> SQLITE_MUTEX_STATIC_PRNG
5863 ** <li> SQLITE_MUTEX_STATIC_LRU
5864 ** <li> SQLITE_MUTEX_STATIC_LRU2
 
 
5865 ** </ul>)^
5866 **
5867 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5868 ** cause sqlite3_mutex_alloc() to create
5869 ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6063,10 +6064,13 @@
6063 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
6064 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
6065 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
6066 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
6067 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
 
 
 
6068
6069 /*
6070 ** CAPI3REF: Retrieve the mutex for a database connection
6071 **
6072 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6158,11 +6162,12 @@
6158 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6159 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6160 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6161 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6162 #define SQLITE_TESTCTRL_BYTEORDER 22
6163 #define SQLITE_TESTCTRL_LAST 22
 
6164
6165 /*
6166 ** CAPI3REF: SQLite Runtime Status
6167 **
6168 ** ^This interface is used to retrieve runtime status information
@@ -7141,10 +7146,13 @@
7141 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7142 ** configured by this function.
7143 **
7144 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7145 ** from SQL.
 
 
 
7146 **
7147 ** ^Every new [database connection] defaults to having the auto-checkpoint
7148 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7149 ** pages. The use of this interface
7150 ** is only necessary if the default setting is found to be suboptimal
@@ -7158,10 +7166,14 @@
7158 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7159 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
7160 ** empty string, then a checkpoint is run on all databases of
7161 ** connection D. ^If the database connection D is not in
7162 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
 
 
 
 
7163 **
7164 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7165 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
7166 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7167 ** run whenever the WAL reaches a certain size threshold.
@@ -7180,22 +7192,25 @@
7180 ** <dl>
7181 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7182 ** Checkpoint as many frames as possible without waiting for any database
7183 ** readers or writers to finish. Sync the db file if all frames in the log
7184 ** are checkpointed. This mode is the same as calling
7185 ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
 
7186 **
7187 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7188 ** This mode blocks (calls the busy-handler callback) until there is no
 
7189 ** database writer and all readers are reading from the most recent database
7190 ** snapshot. It then checkpoints all frames in the log file and syncs the
7191 ** database file. This call blocks database writers while it is running,
7192 ** but not database readers.
7193 **
7194 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7195 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7196 ** checkpointing the log file it blocks (calls the busy-handler callback)
 
7197 ** until all readers are reading from the database file only. This ensures
7198 ** that the next client to write to the database file restarts the log file
7199 ** from the beginning. This call blocks database writers while it is running,
7200 ** but not database readers.
7201 ** </dl>
@@ -7329,10 +7344,11 @@
7329 */
7330 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7331
7332 /*
7333 ** CAPI3REF: Conflict resolution modes
 
7334 **
7335 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7336 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7337 ** is for the SQL statement being evaluated.
7338 **
7339
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.6"
111 #define SQLITE_VERSION_NUMBER 3008006
112 #define SQLITE_SOURCE_ID "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -267,19 +267,19 @@
267 /*
268 ** CAPI3REF: Closing A Database Connection
269 **
270 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
271 ** for the [sqlite3] object.
272 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
273 ** the [sqlite3] object is successfully destroyed and all associated
274 ** resources are deallocated.
275 **
276 ** ^If the database connection is associated with unfinalized prepared
277 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
278 ** will leave the database connection open and return [SQLITE_BUSY].
279 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
280 ** and/or unfinished sqlite3_backups, then the database connection becomes
281 ** an unusable "zombie" which will automatically be deallocated when the
282 ** last prepared statement is finalized or the last sqlite3_backup is
283 ** finished. The sqlite3_close_v2() interface is intended for use with
284 ** host languages that are garbage collected, and where the order in which
285 ** destructors are called is arbitrary.
@@ -288,11 +288,11 @@
288 ** [sqlite3_blob_close | close] all [BLOB handles], and
289 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
290 ** with the [sqlite3] object prior to attempting to close the object. ^If
291 ** sqlite3_close_v2() is called on a [database connection] that still has
292 ** outstanding [prepared statements], [BLOB handles], and/or
293 ** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
294 ** of resources is deferred until all [prepared statements], [BLOB handles],
295 ** and [sqlite3_backup] objects are also destroyed.
296 **
297 ** ^If an [sqlite3] object is destroyed while a transaction is open,
298 ** the transaction is automatically rolled back.
@@ -384,20 +384,18 @@
384 char **errmsg /* Error msg written here */
385 );
386
387 /*
388 ** CAPI3REF: Result Codes
389 ** KEYWORDS: {result code definitions}
 
390 **
391 ** Many SQLite functions return an integer result code from the set shown
392 ** here in order to indicate success or failure.
393 **
394 ** New error codes may be added in future versions of SQLite.
395 **
396 ** See also: [extended result code definitions]
 
397 */
398 #define SQLITE_OK 0 /* Successful result */
399 /* beginning-of-error-codes */
400 #define SQLITE_ERROR 1 /* SQL error or missing database */
401 #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
@@ -431,30 +429,23 @@
429 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
430 /* end-of-error-codes */
431
432 /*
433 ** CAPI3REF: Extended Result Codes
434 ** KEYWORDS: {extended result code definitions}
 
435 **
436 ** In its default configuration, SQLite API routines return one of 30 integer
437 ** [result codes]. However, experience has shown that many of
438 ** these result codes are too coarse-grained. They do not provide as
439 ** much information about problems as programmers might like. In an effort to
440 ** address this, newer versions of SQLite (version 3.3.8 and later) include
441 ** support for additional result codes that provide more detailed information
442 ** about errors. These [extended result codes] are enabled or disabled
443 ** on a per database connection basis using the
444 ** [sqlite3_extended_result_codes()] API. Or, the extended code for
445 ** the most recent error can be obtained using
446 ** [sqlite3_extended_errcode()].
 
 
 
 
 
 
447 */
448 #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
449 #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
450 #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
451 #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
@@ -683,11 +674,11 @@
674 ** write return values. Potential uses for xFileControl() might be
675 ** functions to enable blocking locks with timeouts, to change the
676 ** locking strategy (for example to use dot-file locks), to inquire
677 ** about the status of a lock, or to break stale locks. The SQLite
678 ** core reserves all opcodes less than 100 for its own use.
679 ** A [file control opcodes | list of opcodes] less than 100 is available.
680 ** Applications that define a custom xFileControl method should use opcodes
681 ** greater than 100 to avoid conflicts. VFS implementations should
682 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
683 ** recognize.
684 **
@@ -756,10 +747,11 @@
747 /* Additional methods may be added in future releases */
748 };
749
750 /*
751 ** CAPI3REF: Standard File Control Opcodes
752 ** KEYWORDS: {file control opcodes} {file control opcode}
753 **
754 ** These integer constants are opcodes for the xFileControl method
755 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
756 ** interface.
757 **
@@ -2035,31 +2027,37 @@
2027 SQLITE_API int sqlite3_complete16(const void *sql);
2028
2029 /*
2030 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2031 **
2032 ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2033 ** that might be invoked with argument P whenever
2034 ** an attempt is made to access a database table associated with
2035 ** [database connection] D when another thread
2036 ** or process has the table locked.
2037 ** The sqlite3_busy_handler() interface is used to implement
2038 ** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2039 **
2040 ** ^If the busy callback is NULL, then [SQLITE_BUSY]
2041 ** is returned immediately upon encountering the lock. ^If the busy callback
2042 ** is not NULL, then the callback might be invoked with two arguments.
2043 **
2044 ** ^The first argument to the busy handler is a copy of the void* pointer which
2045 ** is the third argument to sqlite3_busy_handler(). ^The second argument to
2046 ** the busy handler callback is the number of times that the busy handler has
2047 ** been invoked for the same locking event. ^If the
2048 ** busy callback returns 0, then no additional attempts are made to
2049 ** access the database and [SQLITE_BUSY] is returned
2050 ** to the application.
2051 ** ^If the callback returns non-zero, then another attempt
2052 ** is made to access the database and the cycle repeats.
2053 **
2054 ** The presence of a busy handler does not guarantee that it will be invoked
2055 ** when there is lock contention. ^If SQLite determines that invoking the busy
2056 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2057 ** to the application instead of invoking the
2058 ** busy handler.
2059 ** Consider a scenario where one process is holding a read lock that
2060 ** it is trying to promote to a reserved lock and
2061 ** a second process is holding a reserved lock that it is trying
2062 ** to promote to an exclusive lock. The first process cannot proceed
2063 ** because it is blocked by the second and the second process cannot
@@ -2069,32 +2067,19 @@
2067 ** will induce the first process to release its read lock and allow
2068 ** the second process to proceed.
2069 **
2070 ** ^The default busy callback is NULL.
2071 **
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2072 ** ^(There can only be a single busy handler defined for each
2073 ** [database connection]. Setting a new busy handler clears any
2074 ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
2075 ** or evaluating [PRAGMA busy_timeout=N] will change the
2076 ** busy handler and thus clear any previously set busy handler.
2077 **
2078 ** The busy callback should not take any actions which modify the
2079 ** database connection that invoked the busy handler. In other words,
2080 ** the busy handler is not reentrant. Any such actions
2081 ** result in undefined behavior.
2082 **
2083 ** A busy handler must not close the database connection
2084 ** or [prepared statement] that invoked the busy handler.
2085 */
@@ -2106,19 +2091,21 @@
2091 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2092 ** for a specified amount of time when a table is locked. ^The handler
2093 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2094 ** have accumulated. ^After at least "ms" milliseconds of sleeping,
2095 ** the handler returns 0 which causes [sqlite3_step()] to return
2096 ** [SQLITE_BUSY].
2097 **
2098 ** ^Calling this routine with an argument less than or equal to zero
2099 ** turns off all busy handlers.
2100 **
2101 ** ^(There can only be a single busy handler for a particular
2102 ** [database connection] any any given moment. If another busy handler
2103 ** was defined (using [sqlite3_busy_handler()]) prior to calling
2104 ** this routine, that other busy handler is cleared.)^
2105 **
2106 ** See also: [PRAGMA busy_timeout]
2107 */
2108 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2109
2110 /*
2111 ** CAPI3REF: Convenience Routines For Running Queries
@@ -2516,12 +2503,12 @@
2503 ** return either [SQLITE_OK] or one of these two constants in order
2504 ** to signal SQLite whether or not the action is permitted. See the
2505 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2506 ** information.
2507 **
2508 ** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
2509 ** returned from the [sqlite3_vtab_on_conflict()] interface.
2510 */
2511 #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
2512 #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
2513
2514 /*
@@ -4703,10 +4690,17 @@
4690 ** the name of a folder (a.k.a. directory), then all temporary files
4691 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4692 ** will be placed in that directory.)^ ^If this variable
4693 ** is a NULL pointer, then SQLite performs a search for an appropriate
4694 ** temporary file directory.
4695 **
4696 ** Applications are strongly discouraged from using this global variable.
4697 ** It is required to set a temporary folder on Windows Runtime (WinRT).
4698 ** But for all other platforms, it is highly recommended that applications
4699 ** neither read nor write this variable. This global variable is a relic
4700 ** that exists for backwards compatibility of legacy applications and should
4701 ** be avoided in new projects.
4702 **
4703 ** It is not safe to read or modify this variable in more than one
4704 ** thread at a time. It is not safe to read or modify this variable
4705 ** if a [database connection] is being used at the same time in a separate
4706 ** thread.
@@ -4722,10 +4716,15 @@
4716 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4717 ** using [sqlite3_free].
4718 ** Hence, if this variable is modified directly, either it should be
4719 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4720 ** or else the use of the [temp_store_directory pragma] should be avoided.
4721 ** Except when requested by the [temp_store_directory pragma], SQLite
4722 ** does not free the memory that sqlite3_temp_directory points to. If
4723 ** the application wants that memory to be freed, it must do
4724 ** so itself, taking care to only do so after all [database connection]
4725 ** objects have been destroyed.
4726 **
4727 ** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4728 ** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
4729 ** features that require the use of temporary files may fail. Here is an
4730 ** example of how to do this using C++ with the Windows Runtime:
@@ -5856,14 +5855,16 @@
5855 ** <ul>
5856 ** <li> SQLITE_MUTEX_FAST
5857 ** <li> SQLITE_MUTEX_RECURSIVE
5858 ** <li> SQLITE_MUTEX_STATIC_MASTER
5859 ** <li> SQLITE_MUTEX_STATIC_MEM
5860 ** <li> SQLITE_MUTEX_STATIC_OPEN
5861 ** <li> SQLITE_MUTEX_STATIC_PRNG
5862 ** <li> SQLITE_MUTEX_STATIC_LRU
5863 ** <li> SQLITE_MUTEX_STATIC_PMEM
5864 ** <li> SQLITE_MUTEX_STATIC_APP1
5865 ** <li> SQLITE_MUTEX_STATIC_APP2
5866 ** </ul>)^
5867 **
5868 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5869 ** cause sqlite3_mutex_alloc() to create
5870 ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
@@ -6063,10 +6064,13 @@
6064 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
6065 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
6066 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
6067 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
6068 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
6069 #define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
6070 #define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
6071 #define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
6072
6073 /*
6074 ** CAPI3REF: Retrieve the mutex for a database connection
6075 **
6076 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -6158,11 +6162,12 @@
6162 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6163 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6164 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20
6165 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21
6166 #define SQLITE_TESTCTRL_BYTEORDER 22
6167 #define SQLITE_TESTCTRL_ISINIT 23
6168 #define SQLITE_TESTCTRL_LAST 23
6169
6170 /*
6171 ** CAPI3REF: SQLite Runtime Status
6172 **
6173 ** ^This interface is used to retrieve runtime status information
@@ -7141,10 +7146,13 @@
7146 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7147 ** configured by this function.
7148 **
7149 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7150 ** from SQL.
7151 **
7152 ** ^Checkpoints initiated by this mechanism are
7153 ** [sqlite3_wal_checkpoint_v2|PASSIVE].
7154 **
7155 ** ^Every new [database connection] defaults to having the auto-checkpoint
7156 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7157 ** pages. The use of this interface
7158 ** is only necessary if the default setting is found to be suboptimal
@@ -7158,10 +7166,14 @@
7166 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7167 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
7168 ** empty string, then a checkpoint is run on all databases of
7169 ** connection D. ^If the database connection D is not in
7170 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7171 ** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
7172 ** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
7173 ** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
7174 ** or RESET checkpoint.
7175 **
7176 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7177 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
7178 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7179 ** run whenever the WAL reaches a certain size threshold.
@@ -7180,22 +7192,25 @@
7192 ** <dl>
7193 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7194 ** Checkpoint as many frames as possible without waiting for any database
7195 ** readers or writers to finish. Sync the db file if all frames in the log
7196 ** are checkpointed. This mode is the same as calling
7197 ** sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
7198 ** is never invoked.
7199 **
7200 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7201 ** This mode blocks (it invokes the
7202 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
7203 ** database writer and all readers are reading from the most recent database
7204 ** snapshot. It then checkpoints all frames in the log file and syncs the
7205 ** database file. This call blocks database writers while it is running,
7206 ** but not database readers.
7207 **
7208 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7209 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7210 ** checkpointing the log file it blocks (calls the
7211 ** [sqlite3_busy_handler|busy-handler callback])
7212 ** until all readers are reading from the database file only. This ensures
7213 ** that the next client to write to the database file restarts the log file
7214 ** from the beginning. This call blocks database writers while it is running,
7215 ** but not database readers.
7216 ** </dl>
@@ -7329,10 +7344,11 @@
7344 */
7345 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7346
7347 /*
7348 ** CAPI3REF: Conflict resolution modes
7349 ** KEYWORDS: {conflict resolution mode}
7350 **
7351 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7352 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7353 ** is for the SQL statement being evaluated.
7354 **
7355

Keyboard Shortcuts

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