Fossil SCM

Update to the 3.8.8 alpha of SQLite for testing (of SQLite).

drh 2014-11-11 20:25 trunk
Commit 6b2f0b209f9988153cfaf0ac5d5930bfa8d139fe
3 files changed +132 -43 +1941 -823 +308 -182
+132 -43
--- src/shell.c
+++ src/shell.c
@@ -170,11 +170,12 @@
170170
/* Saved resource information for the beginning of an operation */
171171
static HANDLE hProcess;
172172
static FILETIME ftKernelBegin;
173173
static FILETIME ftUserBegin;
174174
static sqlite3_int64 ftWallBegin;
175
-typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
175
+typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
176
+ LPFILETIME, LPFILETIME);
176177
static GETPROCTIMES getProcessTimesAddr = NULL;
177178
178179
/*
179180
** Check to see if we have timer support. Return 1 if necessary
180181
** support found (or found previously).
@@ -181,19 +182,20 @@
181182
*/
182183
static int hasTimer(void){
183184
if( getProcessTimesAddr ){
184185
return 1;
185186
} else {
186
- /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
187
- ** See if the version we are running on has it, and if it does, save off
188
- ** a pointer to it and the current process handle.
187
+ /* GetProcessTimes() isn't supported in WIN95 and some other Windows
188
+ ** versions. See if the version we are running on has it, and if it
189
+ ** does, save off a pointer to it and the current process handle.
189190
*/
190191
hProcess = GetCurrentProcess();
191192
if( hProcess ){
192193
HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
193194
if( NULL != hinstLib ){
194
- getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
195
+ getProcessTimesAddr =
196
+ (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
195197
if( NULL != getProcessTimesAddr ){
196198
return 1;
197199
}
198200
FreeLibrary(hinstLib);
199201
}
@@ -206,11 +208,12 @@
206208
** Begin timing an operation
207209
*/
208210
static void beginTimer(void){
209211
if( enableTimer && getProcessTimesAddr ){
210212
FILETIME ftCreation, ftExit;
211
- getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
213
+ getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
214
+ &ftKernelBegin,&ftUserBegin);
212215
ftWallBegin = timeOfDay();
213216
}
214217
}
215218
216219
/* Return the difference of two FILETIME structs in seconds */
@@ -225,11 +228,11 @@
225228
*/
226229
static void endTimer(void){
227230
if( enableTimer && getProcessTimesAddr){
228231
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
229232
sqlite3_int64 ftWallEnd = timeOfDay();
230
- getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
233
+ getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
231234
printf("Run Time: real %.3f user %f sys %f\n",
232235
(ftWallEnd - ftWallBegin)*0.001,
233236
timeDiff(&ftUserBegin, &ftUserEnd),
234237
timeDiff(&ftKernelBegin, &ftKernelEnd));
235238
}
@@ -455,10 +458,11 @@
455458
struct ShellState {
456459
sqlite3 *db; /* The database */
457460
int echoOn; /* True to echo input commands */
458461
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
459462
int statsOn; /* True to display memory stats before each finalize */
463
+ int scanstatsOn; /* True to display scan stats before each finalize */
460464
int outCount; /* Revert to stdout when reaching zero */
461465
int cnt; /* Number of records displayed so far */
462466
FILE *out; /* Write results here */
463467
FILE *traceOut; /* Output for sqlite3_trace() */
464468
int nErr; /* Number of errors seen */
@@ -723,11 +727,17 @@
723727
724728
/*
725729
** This is the callback routine that the shell
726730
** invokes for each row of a query result.
727731
*/
728
-static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
732
+static int shell_callback(
733
+ void *pArg,
734
+ int nArg, /* Number of result columns */
735
+ char **azArg, /* Text of each result column */
736
+ char **azCol, /* Column names */
737
+ int *aiType /* Column types */
738
+){
729739
int i;
730740
ShellState *p = (ShellState*)pArg;
731741
732742
switch( p->mode ){
733743
case MODE_Line: {
@@ -1102,61 +1112,81 @@
11021112
11031113
if( pArg && pArg->out ){
11041114
11051115
iHiwtr = iCur = -1;
11061116
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1107
- fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1117
+ fprintf(pArg->out,
1118
+ "Memory Used: %d (max %d) bytes\n",
1119
+ iCur, iHiwtr);
11081120
iHiwtr = iCur = -1;
11091121
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1110
- fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1122
+ fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
1123
+ iCur, iHiwtr);
11111124
if( pArg->shellFlgs & SHFLG_Pagecache ){
11121125
iHiwtr = iCur = -1;
11131126
sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1114
- fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1127
+ fprintf(pArg->out,
1128
+ "Number of Pcache Pages Used: %d (max %d) pages\n",
1129
+ iCur, iHiwtr);
11151130
}
11161131
iHiwtr = iCur = -1;
11171132
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1118
- fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1133
+ fprintf(pArg->out,
1134
+ "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
1135
+ iCur, iHiwtr);
11191136
if( pArg->shellFlgs & SHFLG_Scratch ){
11201137
iHiwtr = iCur = -1;
11211138
sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1122
- fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1139
+ fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n",
1140
+ iCur, iHiwtr);
11231141
}
11241142
iHiwtr = iCur = -1;
11251143
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1126
- fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1144
+ fprintf(pArg->out,
1145
+ "Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
1146
+ iCur, iHiwtr);
11271147
iHiwtr = iCur = -1;
11281148
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1129
- fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr);
1149
+ fprintf(pArg->out, "Largest Allocation: %d bytes\n",
1150
+ iHiwtr);
11301151
iHiwtr = iCur = -1;
11311152
sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1132
- fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr);
1153
+ fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
1154
+ iHiwtr);
11331155
iHiwtr = iCur = -1;
11341156
sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1135
- fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr);
1157
+ fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
1158
+ iHiwtr);
11361159
#ifdef YYTRACKMAXSTACKDEPTH
11371160
iHiwtr = iCur = -1;
11381161
sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1139
- fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1162
+ fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
1163
+ iCur, iHiwtr);
11401164
#endif
11411165
}
11421166
11431167
if( pArg && pArg->out && db ){
11441168
if( pArg->shellFlgs & SHFLG_Lookaside ){
11451169
iHiwtr = iCur = -1;
1146
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1147
- fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1148
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1170
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1171
+ &iCur, &iHiwtr, bReset);
1172
+ fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n",
1173
+ iCur, iHiwtr);
1174
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1175
+ &iCur, &iHiwtr, bReset);
11491176
fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1150
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1177
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1178
+ &iCur, &iHiwtr, bReset);
11511179
fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1152
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1180
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1181
+ &iCur, &iHiwtr, bReset);
11531182
fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
11541183
}
11551184
iHiwtr = iCur = -1;
11561185
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1157
- fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1186
+ fprintf(pArg->out, "Pager Heap Usage: %d bytes\n",iCur);
1187
+ iHiwtr = iCur = -1;
11581188
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11591189
fprintf(pArg->out, "Page cache hits: %d\n", iCur);
11601190
iHiwtr = iCur = -1;
11611191
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
11621192
fprintf(pArg->out, "Page cache misses: %d\n", iCur);
@@ -1163,29 +1193,75 @@
11631193
iHiwtr = iCur = -1;
11641194
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
11651195
fprintf(pArg->out, "Page cache writes: %d\n", iCur);
11661196
iHiwtr = iCur = -1;
11671197
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1168
- fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
1198
+ fprintf(pArg->out, "Schema Heap Usage: %d bytes\n",iCur);
11691199
iHiwtr = iCur = -1;
11701200
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1171
- fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
1201
+ fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",iCur);
11721202
}
11731203
11741204
if( pArg && pArg->out && db && pArg->pStmt ){
1175
- iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
1205
+ iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1206
+ bReset);
11761207
fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
11771208
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
11781209
fprintf(pArg->out, "Sort Operations: %d\n", iCur);
1179
- iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
1210
+ iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
11801211
fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
11811212
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
11821213
fprintf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
11831214
}
11841215
11851216
return 0;
11861217
}
1218
+
1219
+/*
1220
+** Display scan stats.
1221
+*/
1222
+static void display_scanstats(
1223
+ sqlite3 *db, /* Database to query */
1224
+ ShellState *pArg /* Pointer to ShellState */
1225
+){
1226
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1227
+ int i, k, n, mx;
1228
+ fprintf(pArg->out, "-------- scanstats --------\n");
1229
+ mx = 0;
1230
+ for(k=0; k<=mx; k++){
1231
+ double rEstLoop = 1.0;
1232
+ for(i=n=0; 1; i++){
1233
+ sqlite3_stmt *p = pArg->pStmt;
1234
+ sqlite3_int64 nLoop, nVisit;
1235
+ double rEst;
1236
+ int iSid;
1237
+ const char *zExplain;
1238
+ if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
1239
+ break;
1240
+ }
1241
+ sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
1242
+ if( iSid>mx ) mx = iSid;
1243
+ if( iSid!=k ) continue;
1244
+ if( n==0 ){
1245
+ rEstLoop = (double)nLoop;
1246
+ if( k>0 ) fprintf(pArg->out, "-------- subquery %d -------\n", k);
1247
+ }
1248
+ n++;
1249
+ sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
1250
+ sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
1251
+ sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
1252
+ fprintf(pArg->out, "Loop %2d: %s\n", n, zExplain);
1253
+ rEstLoop *= rEst;
1254
+ fprintf(pArg->out,
1255
+ " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
1256
+ nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
1257
+ );
1258
+ }
1259
+ }
1260
+ fprintf(pArg->out, "---------------------------\n");
1261
+#endif
1262
+}
11871263
11881264
/*
11891265
** Parameter azArray points to a zero-terminated array of strings. zStr
11901266
** points to a single nul-terminated string. Return non-zero if zStr
11911267
** is equal, according to strcmp(), to any of the strings in the array.
@@ -1224,11 +1300,12 @@
12241300
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12251301
int iOp; /* Index of operation in p->aiIndent[] */
12261302
12271303
const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
12281304
"NextIfOpen", "PrevIfOpen", 0 };
1229
- const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", "Rewind", 0 };
1305
+ const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
1306
+ "Rewind", 0 };
12301307
const char *azGoto[] = { "Goto", 0 };
12311308
12321309
/* Try to figure out if this is really an EXPLAIN statement. If this
12331310
** cannot be verified, return early. */
12341311
zSql = sqlite3_sql(pSql);
@@ -1337,11 +1414,12 @@
13371414
}
13381415
13391416
/* Show the EXPLAIN QUERY PLAN if .eqp is on */
13401417
if( pArg && pArg->autoEQP ){
13411418
sqlite3_stmt *pExplain;
1342
- char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", sqlite3_sql(pStmt));
1419
+ char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s",
1420
+ sqlite3_sql(pStmt));
13431421
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13441422
if( rc==SQLITE_OK ){
13451423
while( sqlite3_step(pExplain)==SQLITE_ROW ){
13461424
fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
13471425
fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
@@ -1351,21 +1429,10 @@
13511429
}
13521430
sqlite3_finalize(pExplain);
13531431
sqlite3_free(zEQP);
13541432
}
13551433
1356
-#if USE_SYSTEM_SQLITE+0==1
1357
- /* Output TESTCTRL_EXPLAIN text of requested */
1358
- if( pArg && pArg->mode==MODE_Explain && sqlite3_libversion_number()<3008007 ){
1359
- const char *zExplain = 0;
1360
- sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1361
- if( zExplain && zExplain[0] ){
1362
- fprintf(pArg->out, "%s", zExplain);
1363
- }
1364
- }
1365
-#endif
1366
-
13671434
/* If the shell is currently in ".explain" mode, gather the extra
13681435
** data required to add indents to the output.*/
13691436
if( pArg && pArg->mode==MODE_Explain ){
13701437
explain_data_prepare(pArg, pStmt);
13711438
}
@@ -1431,10 +1498,15 @@
14311498
14321499
/* print usage stats if stats on */
14331500
if( pArg && pArg->statsOn ){
14341501
display_stats(db, pArg, 0);
14351502
}
1503
+
1504
+ /* print loop-counters if required */
1505
+ if( pArg && pArg->scanstatsOn ){
1506
+ display_scanstats(db, pArg);
1507
+ }
14361508
14371509
/* Finalize the statement just executed. If this fails, save a
14381510
** copy of the error message. Otherwise, set zSql to point to the
14391511
** next statement to execute. */
14401512
rc2 = sqlite3_finalize(pStmt);
@@ -1642,10 +1714,11 @@
16421714
".prompt MAIN CONTINUE Replace the standard prompts\n"
16431715
".quit Exit this program\n"
16441716
".read FILENAME Execute SQL in FILENAME\n"
16451717
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
16461718
".save FILE Write in-memory database into FILE\n"
1719
+ ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
16471720
".schema ?TABLE? Show the CREATE statements\n"
16481721
" If TABLE specified, only show tables matching\n"
16491722
" LIKE pattern TABLE.\n"
16501723
".separator STRING ?NL? Change separator used by output mode and .import\n"
16511724
" NL is the end-of-line mark for CSV\n"
@@ -3023,10 +3096,23 @@
30233096
rc = 1;
30243097
}
30253098
sqlite3_close(pSrc);
30263099
}else
30273100
3101
+
3102
+ if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
3103
+ if( nArg==2 ){
3104
+ p->scanstatsOn = booleanValue(azArg[1]);
3105
+#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3106
+ fprintf(stderr, "Warning: .scanstats not available in this build.\n");
3107
+#endif
3108
+ }else{
3109
+ fprintf(stderr, "Usage: .scanstats on|off\n");
3110
+ rc = 1;
3111
+ }
3112
+ }else
3113
+
30283114
if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
30293115
ShellState data;
30303116
char *zErrMsg = 0;
30313117
open_db(p, 0);
30323118
memcpy(&data, p, sizeof(data));
@@ -3280,11 +3366,11 @@
32803366
if( nPrintCol<1 ) nPrintCol = 1;
32813367
nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
32823368
for(i=0; i<nPrintRow; i++){
32833369
for(j=i; j<nRow; j+=nPrintRow){
32843370
char *zSp = j<nPrintRow ? "" : " ";
3285
- fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
3371
+ fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
32863372
}
32873373
fprintf(p->out, "\n");
32883374
}
32893375
}
32903376
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
@@ -3750,11 +3836,12 @@
37503836
*/
37513837
static char *find_home_dir(void){
37523838
static char *home_dir = NULL;
37533839
if( home_dir ) return home_dir;
37543840
3755
-#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
3841
+#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
3842
+ && !defined(__RTP__) && !defined(_WRS_KERNEL)
37563843
{
37573844
struct passwd *pwent;
37583845
uid_t uid = getuid();
37593846
if( (pwent=getpwuid(uid)) != NULL) {
37603847
home_dir = pwent->pw_dir;
@@ -4149,10 +4236,12 @@
41494236
data.echoOn = 1;
41504237
}else if( strcmp(z,"-eqp")==0 ){
41514238
data.autoEQP = 1;
41524239
}else if( strcmp(z,"-stats")==0 ){
41534240
data.statsOn = 1;
4241
+ }else if( strcmp(z,"-scanstats")==0 ){
4242
+ data.scanstatsOn = 1;
41544243
}else if( strcmp(z,"-bail")==0 ){
41554244
bail_on_error = 1;
41564245
}else if( strcmp(z,"-version")==0 ){
41574246
printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
41584247
return 0;
41594248
--- src/shell.c
+++ src/shell.c
@@ -170,11 +170,12 @@
170 /* Saved resource information for the beginning of an operation */
171 static HANDLE hProcess;
172 static FILETIME ftKernelBegin;
173 static FILETIME ftUserBegin;
174 static sqlite3_int64 ftWallBegin;
175 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
 
176 static GETPROCTIMES getProcessTimesAddr = NULL;
177
178 /*
179 ** Check to see if we have timer support. Return 1 if necessary
180 ** support found (or found previously).
@@ -181,19 +182,20 @@
181 */
182 static int hasTimer(void){
183 if( getProcessTimesAddr ){
184 return 1;
185 } else {
186 /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
187 ** See if the version we are running on has it, and if it does, save off
188 ** a pointer to it and the current process handle.
189 */
190 hProcess = GetCurrentProcess();
191 if( hProcess ){
192 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
193 if( NULL != hinstLib ){
194 getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
 
195 if( NULL != getProcessTimesAddr ){
196 return 1;
197 }
198 FreeLibrary(hinstLib);
199 }
@@ -206,11 +208,12 @@
206 ** Begin timing an operation
207 */
208 static void beginTimer(void){
209 if( enableTimer && getProcessTimesAddr ){
210 FILETIME ftCreation, ftExit;
211 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
 
212 ftWallBegin = timeOfDay();
213 }
214 }
215
216 /* Return the difference of two FILETIME structs in seconds */
@@ -225,11 +228,11 @@
225 */
226 static void endTimer(void){
227 if( enableTimer && getProcessTimesAddr){
228 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
229 sqlite3_int64 ftWallEnd = timeOfDay();
230 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
231 printf("Run Time: real %.3f user %f sys %f\n",
232 (ftWallEnd - ftWallBegin)*0.001,
233 timeDiff(&ftUserBegin, &ftUserEnd),
234 timeDiff(&ftKernelBegin, &ftKernelEnd));
235 }
@@ -455,10 +458,11 @@
455 struct ShellState {
456 sqlite3 *db; /* The database */
457 int echoOn; /* True to echo input commands */
458 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
459 int statsOn; /* True to display memory stats before each finalize */
 
460 int outCount; /* Revert to stdout when reaching zero */
461 int cnt; /* Number of records displayed so far */
462 FILE *out; /* Write results here */
463 FILE *traceOut; /* Output for sqlite3_trace() */
464 int nErr; /* Number of errors seen */
@@ -723,11 +727,17 @@
723
724 /*
725 ** This is the callback routine that the shell
726 ** invokes for each row of a query result.
727 */
728 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
 
 
 
 
 
 
729 int i;
730 ShellState *p = (ShellState*)pArg;
731
732 switch( p->mode ){
733 case MODE_Line: {
@@ -1102,61 +1112,81 @@
1102
1103 if( pArg && pArg->out ){
1104
1105 iHiwtr = iCur = -1;
1106 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1107 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
 
 
1108 iHiwtr = iCur = -1;
1109 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1110 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
 
1111 if( pArg->shellFlgs & SHFLG_Pagecache ){
1112 iHiwtr = iCur = -1;
1113 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1114 fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
 
 
1115 }
1116 iHiwtr = iCur = -1;
1117 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1118 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
 
 
1119 if( pArg->shellFlgs & SHFLG_Scratch ){
1120 iHiwtr = iCur = -1;
1121 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1122 fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
 
1123 }
1124 iHiwtr = iCur = -1;
1125 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1126 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
 
 
1127 iHiwtr = iCur = -1;
1128 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1129 fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr);
 
1130 iHiwtr = iCur = -1;
1131 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1132 fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr);
 
1133 iHiwtr = iCur = -1;
1134 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1135 fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr);
 
1136 #ifdef YYTRACKMAXSTACKDEPTH
1137 iHiwtr = iCur = -1;
1138 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1139 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
 
1140 #endif
1141 }
1142
1143 if( pArg && pArg->out && db ){
1144 if( pArg->shellFlgs & SHFLG_Lookaside ){
1145 iHiwtr = iCur = -1;
1146 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1147 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1148 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
 
 
 
1149 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1150 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
 
1151 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1152 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
 
1153 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1154 }
1155 iHiwtr = iCur = -1;
1156 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1157 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
 
1158 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1159 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
1160 iHiwtr = iCur = -1;
1161 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1162 fprintf(pArg->out, "Page cache misses: %d\n", iCur);
@@ -1163,29 +1193,75 @@
1163 iHiwtr = iCur = -1;
1164 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1165 fprintf(pArg->out, "Page cache writes: %d\n", iCur);
1166 iHiwtr = iCur = -1;
1167 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1168 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
1169 iHiwtr = iCur = -1;
1170 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1171 fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
1172 }
1173
1174 if( pArg && pArg->out && db && pArg->pStmt ){
1175 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
 
1176 fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
1177 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1178 fprintf(pArg->out, "Sort Operations: %d\n", iCur);
1179 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
1180 fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1181 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1182 fprintf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1183 }
1184
1185 return 0;
1186 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1187
1188 /*
1189 ** Parameter azArray points to a zero-terminated array of strings. zStr
1190 ** points to a single nul-terminated string. Return non-zero if zStr
1191 ** is equal, according to strcmp(), to any of the strings in the array.
@@ -1224,11 +1300,12 @@
1224 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1225 int iOp; /* Index of operation in p->aiIndent[] */
1226
1227 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
1228 "NextIfOpen", "PrevIfOpen", 0 };
1229 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", "Rewind", 0 };
 
1230 const char *azGoto[] = { "Goto", 0 };
1231
1232 /* Try to figure out if this is really an EXPLAIN statement. If this
1233 ** cannot be verified, return early. */
1234 zSql = sqlite3_sql(pSql);
@@ -1337,11 +1414,12 @@
1337 }
1338
1339 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1340 if( pArg && pArg->autoEQP ){
1341 sqlite3_stmt *pExplain;
1342 char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", sqlite3_sql(pStmt));
 
1343 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1344 if( rc==SQLITE_OK ){
1345 while( sqlite3_step(pExplain)==SQLITE_ROW ){
1346 fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
1347 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
@@ -1351,21 +1429,10 @@
1351 }
1352 sqlite3_finalize(pExplain);
1353 sqlite3_free(zEQP);
1354 }
1355
1356 #if USE_SYSTEM_SQLITE+0==1
1357 /* Output TESTCTRL_EXPLAIN text of requested */
1358 if( pArg && pArg->mode==MODE_Explain && sqlite3_libversion_number()<3008007 ){
1359 const char *zExplain = 0;
1360 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1361 if( zExplain && zExplain[0] ){
1362 fprintf(pArg->out, "%s", zExplain);
1363 }
1364 }
1365 #endif
1366
1367 /* If the shell is currently in ".explain" mode, gather the extra
1368 ** data required to add indents to the output.*/
1369 if( pArg && pArg->mode==MODE_Explain ){
1370 explain_data_prepare(pArg, pStmt);
1371 }
@@ -1431,10 +1498,15 @@
1431
1432 /* print usage stats if stats on */
1433 if( pArg && pArg->statsOn ){
1434 display_stats(db, pArg, 0);
1435 }
 
 
 
 
 
1436
1437 /* Finalize the statement just executed. If this fails, save a
1438 ** copy of the error message. Otherwise, set zSql to point to the
1439 ** next statement to execute. */
1440 rc2 = sqlite3_finalize(pStmt);
@@ -1642,10 +1714,11 @@
1642 ".prompt MAIN CONTINUE Replace the standard prompts\n"
1643 ".quit Exit this program\n"
1644 ".read FILENAME Execute SQL in FILENAME\n"
1645 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
1646 ".save FILE Write in-memory database into FILE\n"
 
1647 ".schema ?TABLE? Show the CREATE statements\n"
1648 " If TABLE specified, only show tables matching\n"
1649 " LIKE pattern TABLE.\n"
1650 ".separator STRING ?NL? Change separator used by output mode and .import\n"
1651 " NL is the end-of-line mark for CSV\n"
@@ -3023,10 +3096,23 @@
3023 rc = 1;
3024 }
3025 sqlite3_close(pSrc);
3026 }else
3027
 
 
 
 
 
 
 
 
 
 
 
 
 
3028 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3029 ShellState data;
3030 char *zErrMsg = 0;
3031 open_db(p, 0);
3032 memcpy(&data, p, sizeof(data));
@@ -3280,11 +3366,11 @@
3280 if( nPrintCol<1 ) nPrintCol = 1;
3281 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
3282 for(i=0; i<nPrintRow; i++){
3283 for(j=i; j<nRow; j+=nPrintRow){
3284 char *zSp = j<nPrintRow ? "" : " ";
3285 fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
3286 }
3287 fprintf(p->out, "\n");
3288 }
3289 }
3290 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
@@ -3750,11 +3836,12 @@
3750 */
3751 static char *find_home_dir(void){
3752 static char *home_dir = NULL;
3753 if( home_dir ) return home_dir;
3754
3755 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
 
3756 {
3757 struct passwd *pwent;
3758 uid_t uid = getuid();
3759 if( (pwent=getpwuid(uid)) != NULL) {
3760 home_dir = pwent->pw_dir;
@@ -4149,10 +4236,12 @@
4149 data.echoOn = 1;
4150 }else if( strcmp(z,"-eqp")==0 ){
4151 data.autoEQP = 1;
4152 }else if( strcmp(z,"-stats")==0 ){
4153 data.statsOn = 1;
 
 
4154 }else if( strcmp(z,"-bail")==0 ){
4155 bail_on_error = 1;
4156 }else if( strcmp(z,"-version")==0 ){
4157 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
4158 return 0;
4159
--- src/shell.c
+++ src/shell.c
@@ -170,11 +170,12 @@
170 /* Saved resource information for the beginning of an operation */
171 static HANDLE hProcess;
172 static FILETIME ftKernelBegin;
173 static FILETIME ftUserBegin;
174 static sqlite3_int64 ftWallBegin;
175 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
176 LPFILETIME, LPFILETIME);
177 static GETPROCTIMES getProcessTimesAddr = NULL;
178
179 /*
180 ** Check to see if we have timer support. Return 1 if necessary
181 ** support found (or found previously).
@@ -181,19 +182,20 @@
182 */
183 static int hasTimer(void){
184 if( getProcessTimesAddr ){
185 return 1;
186 } else {
187 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
188 ** versions. See if the version we are running on has it, and if it
189 ** does, save off a pointer to it and the current process handle.
190 */
191 hProcess = GetCurrentProcess();
192 if( hProcess ){
193 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
194 if( NULL != hinstLib ){
195 getProcessTimesAddr =
196 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
197 if( NULL != getProcessTimesAddr ){
198 return 1;
199 }
200 FreeLibrary(hinstLib);
201 }
@@ -206,11 +208,12 @@
208 ** Begin timing an operation
209 */
210 static void beginTimer(void){
211 if( enableTimer && getProcessTimesAddr ){
212 FILETIME ftCreation, ftExit;
213 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
214 &ftKernelBegin,&ftUserBegin);
215 ftWallBegin = timeOfDay();
216 }
217 }
218
219 /* Return the difference of two FILETIME structs in seconds */
@@ -225,11 +228,11 @@
228 */
229 static void endTimer(void){
230 if( enableTimer && getProcessTimesAddr){
231 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
232 sqlite3_int64 ftWallEnd = timeOfDay();
233 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
234 printf("Run Time: real %.3f user %f sys %f\n",
235 (ftWallEnd - ftWallBegin)*0.001,
236 timeDiff(&ftUserBegin, &ftUserEnd),
237 timeDiff(&ftKernelBegin, &ftKernelEnd));
238 }
@@ -455,10 +458,11 @@
458 struct ShellState {
459 sqlite3 *db; /* The database */
460 int echoOn; /* True to echo input commands */
461 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
462 int statsOn; /* True to display memory stats before each finalize */
463 int scanstatsOn; /* True to display scan stats before each finalize */
464 int outCount; /* Revert to stdout when reaching zero */
465 int cnt; /* Number of records displayed so far */
466 FILE *out; /* Write results here */
467 FILE *traceOut; /* Output for sqlite3_trace() */
468 int nErr; /* Number of errors seen */
@@ -723,11 +727,17 @@
727
728 /*
729 ** This is the callback routine that the shell
730 ** invokes for each row of a query result.
731 */
732 static int shell_callback(
733 void *pArg,
734 int nArg, /* Number of result columns */
735 char **azArg, /* Text of each result column */
736 char **azCol, /* Column names */
737 int *aiType /* Column types */
738 ){
739 int i;
740 ShellState *p = (ShellState*)pArg;
741
742 switch( p->mode ){
743 case MODE_Line: {
@@ -1102,61 +1112,81 @@
1112
1113 if( pArg && pArg->out ){
1114
1115 iHiwtr = iCur = -1;
1116 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1117 fprintf(pArg->out,
1118 "Memory Used: %d (max %d) bytes\n",
1119 iCur, iHiwtr);
1120 iHiwtr = iCur = -1;
1121 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1122 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
1123 iCur, iHiwtr);
1124 if( pArg->shellFlgs & SHFLG_Pagecache ){
1125 iHiwtr = iCur = -1;
1126 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1127 fprintf(pArg->out,
1128 "Number of Pcache Pages Used: %d (max %d) pages\n",
1129 iCur, iHiwtr);
1130 }
1131 iHiwtr = iCur = -1;
1132 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1133 fprintf(pArg->out,
1134 "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
1135 iCur, iHiwtr);
1136 if( pArg->shellFlgs & SHFLG_Scratch ){
1137 iHiwtr = iCur = -1;
1138 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1139 fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n",
1140 iCur, iHiwtr);
1141 }
1142 iHiwtr = iCur = -1;
1143 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1144 fprintf(pArg->out,
1145 "Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
1146 iCur, iHiwtr);
1147 iHiwtr = iCur = -1;
1148 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1149 fprintf(pArg->out, "Largest Allocation: %d bytes\n",
1150 iHiwtr);
1151 iHiwtr = iCur = -1;
1152 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1153 fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
1154 iHiwtr);
1155 iHiwtr = iCur = -1;
1156 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1157 fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
1158 iHiwtr);
1159 #ifdef YYTRACKMAXSTACKDEPTH
1160 iHiwtr = iCur = -1;
1161 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1162 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
1163 iCur, iHiwtr);
1164 #endif
1165 }
1166
1167 if( pArg && pArg->out && db ){
1168 if( pArg->shellFlgs & SHFLG_Lookaside ){
1169 iHiwtr = iCur = -1;
1170 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1171 &iCur, &iHiwtr, bReset);
1172 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n",
1173 iCur, iHiwtr);
1174 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1175 &iCur, &iHiwtr, bReset);
1176 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1177 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1178 &iCur, &iHiwtr, bReset);
1179 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1180 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1181 &iCur, &iHiwtr, bReset);
1182 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1183 }
1184 iHiwtr = iCur = -1;
1185 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1186 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n",iCur);
1187 iHiwtr = iCur = -1;
1188 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1189 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
1190 iHiwtr = iCur = -1;
1191 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1192 fprintf(pArg->out, "Page cache misses: %d\n", iCur);
@@ -1163,29 +1193,75 @@
1193 iHiwtr = iCur = -1;
1194 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1195 fprintf(pArg->out, "Page cache writes: %d\n", iCur);
1196 iHiwtr = iCur = -1;
1197 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1198 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n",iCur);
1199 iHiwtr = iCur = -1;
1200 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1201 fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",iCur);
1202 }
1203
1204 if( pArg && pArg->out && db && pArg->pStmt ){
1205 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1206 bReset);
1207 fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
1208 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1209 fprintf(pArg->out, "Sort Operations: %d\n", iCur);
1210 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
1211 fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1212 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1213 fprintf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1214 }
1215
1216 return 0;
1217 }
1218
1219 /*
1220 ** Display scan stats.
1221 */
1222 static void display_scanstats(
1223 sqlite3 *db, /* Database to query */
1224 ShellState *pArg /* Pointer to ShellState */
1225 ){
1226 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1227 int i, k, n, mx;
1228 fprintf(pArg->out, "-------- scanstats --------\n");
1229 mx = 0;
1230 for(k=0; k<=mx; k++){
1231 double rEstLoop = 1.0;
1232 for(i=n=0; 1; i++){
1233 sqlite3_stmt *p = pArg->pStmt;
1234 sqlite3_int64 nLoop, nVisit;
1235 double rEst;
1236 int iSid;
1237 const char *zExplain;
1238 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
1239 break;
1240 }
1241 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
1242 if( iSid>mx ) mx = iSid;
1243 if( iSid!=k ) continue;
1244 if( n==0 ){
1245 rEstLoop = (double)nLoop;
1246 if( k>0 ) fprintf(pArg->out, "-------- subquery %d -------\n", k);
1247 }
1248 n++;
1249 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
1250 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
1251 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
1252 fprintf(pArg->out, "Loop %2d: %s\n", n, zExplain);
1253 rEstLoop *= rEst;
1254 fprintf(pArg->out,
1255 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
1256 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
1257 );
1258 }
1259 }
1260 fprintf(pArg->out, "---------------------------\n");
1261 #endif
1262 }
1263
1264 /*
1265 ** Parameter azArray points to a zero-terminated array of strings. zStr
1266 ** points to a single nul-terminated string. Return non-zero if zStr
1267 ** is equal, according to strcmp(), to any of the strings in the array.
@@ -1224,11 +1300,12 @@
1300 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1301 int iOp; /* Index of operation in p->aiIndent[] */
1302
1303 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
1304 "NextIfOpen", "PrevIfOpen", 0 };
1305 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
1306 "Rewind", 0 };
1307 const char *azGoto[] = { "Goto", 0 };
1308
1309 /* Try to figure out if this is really an EXPLAIN statement. If this
1310 ** cannot be verified, return early. */
1311 zSql = sqlite3_sql(pSql);
@@ -1337,11 +1414,12 @@
1414 }
1415
1416 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1417 if( pArg && pArg->autoEQP ){
1418 sqlite3_stmt *pExplain;
1419 char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s",
1420 sqlite3_sql(pStmt));
1421 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1422 if( rc==SQLITE_OK ){
1423 while( sqlite3_step(pExplain)==SQLITE_ROW ){
1424 fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
1425 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
@@ -1351,21 +1429,10 @@
1429 }
1430 sqlite3_finalize(pExplain);
1431 sqlite3_free(zEQP);
1432 }
1433
 
 
 
 
 
 
 
 
 
 
 
1434 /* If the shell is currently in ".explain" mode, gather the extra
1435 ** data required to add indents to the output.*/
1436 if( pArg && pArg->mode==MODE_Explain ){
1437 explain_data_prepare(pArg, pStmt);
1438 }
@@ -1431,10 +1498,15 @@
1498
1499 /* print usage stats if stats on */
1500 if( pArg && pArg->statsOn ){
1501 display_stats(db, pArg, 0);
1502 }
1503
1504 /* print loop-counters if required */
1505 if( pArg && pArg->scanstatsOn ){
1506 display_scanstats(db, pArg);
1507 }
1508
1509 /* Finalize the statement just executed. If this fails, save a
1510 ** copy of the error message. Otherwise, set zSql to point to the
1511 ** next statement to execute. */
1512 rc2 = sqlite3_finalize(pStmt);
@@ -1642,10 +1714,11 @@
1714 ".prompt MAIN CONTINUE Replace the standard prompts\n"
1715 ".quit Exit this program\n"
1716 ".read FILENAME Execute SQL in FILENAME\n"
1717 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
1718 ".save FILE Write in-memory database into FILE\n"
1719 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
1720 ".schema ?TABLE? Show the CREATE statements\n"
1721 " If TABLE specified, only show tables matching\n"
1722 " LIKE pattern TABLE.\n"
1723 ".separator STRING ?NL? Change separator used by output mode and .import\n"
1724 " NL is the end-of-line mark for CSV\n"
@@ -3023,10 +3096,23 @@
3096 rc = 1;
3097 }
3098 sqlite3_close(pSrc);
3099 }else
3100
3101
3102 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
3103 if( nArg==2 ){
3104 p->scanstatsOn = booleanValue(azArg[1]);
3105 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3106 fprintf(stderr, "Warning: .scanstats not available in this build.\n");
3107 #endif
3108 }else{
3109 fprintf(stderr, "Usage: .scanstats on|off\n");
3110 rc = 1;
3111 }
3112 }else
3113
3114 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
3115 ShellState data;
3116 char *zErrMsg = 0;
3117 open_db(p, 0);
3118 memcpy(&data, p, sizeof(data));
@@ -3280,11 +3366,11 @@
3366 if( nPrintCol<1 ) nPrintCol = 1;
3367 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
3368 for(i=0; i<nPrintRow; i++){
3369 for(j=i; j<nRow; j+=nPrintRow){
3370 char *zSp = j<nPrintRow ? "" : " ";
3371 fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
3372 }
3373 fprintf(p->out, "\n");
3374 }
3375 }
3376 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
@@ -3750,11 +3836,12 @@
3836 */
3837 static char *find_home_dir(void){
3838 static char *home_dir = NULL;
3839 if( home_dir ) return home_dir;
3840
3841 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
3842 && !defined(__RTP__) && !defined(_WRS_KERNEL)
3843 {
3844 struct passwd *pwent;
3845 uid_t uid = getuid();
3846 if( (pwent=getpwuid(uid)) != NULL) {
3847 home_dir = pwent->pw_dir;
@@ -4149,10 +4236,12 @@
4236 data.echoOn = 1;
4237 }else if( strcmp(z,"-eqp")==0 ){
4238 data.autoEQP = 1;
4239 }else if( strcmp(z,"-stats")==0 ){
4240 data.statsOn = 1;
4241 }else if( strcmp(z,"-scanstats")==0 ){
4242 data.scanstatsOn = 1;
4243 }else if( strcmp(z,"-bail")==0 ){
4244 bail_on_error = 1;
4245 }else if( strcmp(z,"-version")==0 ){
4246 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
4247 return 0;
4248
+1941 -823
--- 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.7.1. By combining all the individual C code files into this
3
+** version 3.8.8. 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.
@@ -179,11 +179,11 @@
179179
180180
181181
/*
182182
** These no-op macros are used in front of interfaces to mark those
183183
** interfaces as either deprecated or experimental. New applications
184
-** should not use deprecated interfaces - they are support for backwards
184
+** should not use deprecated interfaces - they are supported for backwards
185185
** compatibility only. Application writers should be aware that
186186
** experimental interfaces are subject to change in point releases.
187187
**
188188
** These macros used to resolve to various kinds of compiler magic that
189189
** would generate warning messages when they were used. But that
@@ -229,13 +229,13 @@
229229
**
230230
** See also: [sqlite3_libversion()],
231231
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232232
** [sqlite_version()] and [sqlite_source_id()].
233233
*/
234
-#define SQLITE_VERSION "3.8.7.1"
235
-#define SQLITE_VERSION_NUMBER 3008007
236
-#define SQLITE_SOURCE_ID "2014-10-29 01:27:43 83afe23e553e802c0947c80d0ffdd120423e7c52"
234
+#define SQLITE_VERSION "3.8.8"
235
+#define SQLITE_VERSION_NUMBER 3008008
236
+#define SQLITE_SOURCE_ID "2014-11-11 19:07:56 1412fcc480799ecbd68d44dd18d5bad40e20ccf1"
237237
238238
/*
239239
** CAPI3REF: Run-Time Library Version Numbers
240240
** KEYWORDS: sqlite3_version, sqlite3_sourceid
241241
**
@@ -1626,29 +1626,31 @@
16261626
** it is not possible to set the Serialized [threading mode] and
16271627
** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
16281628
** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
16291629
**
16301630
** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1631
-** <dd> ^(This option takes a single argument which is a pointer to an
1632
-** instance of the [sqlite3_mem_methods] structure. The argument specifies
1631
+** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1632
+** a pointer to an instance of the [sqlite3_mem_methods] structure.
1633
+** The argument specifies
16331634
** alternative low-level memory allocation routines to be used in place of
16341635
** the memory allocation routines built into SQLite.)^ ^SQLite makes
16351636
** its own private copy of the content of the [sqlite3_mem_methods] structure
16361637
** before the [sqlite3_config()] call returns.</dd>
16371638
**
16381639
** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1639
-** <dd> ^(This option takes a single argument which is a pointer to an
1640
-** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
1640
+** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1641
+** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1642
+** The [sqlite3_mem_methods]
16411643
** structure is filled with the currently defined memory allocation routines.)^
16421644
** This option can be used to overload the default memory allocation
16431645
** routines with a wrapper that simulations memory allocation failure or
16441646
** tracks memory usage, for example. </dd>
16451647
**
16461648
** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1647
-** <dd> ^This option takes single argument of type int, interpreted as a
1648
-** boolean, which enables or disables the collection of memory allocation
1649
-** statistics. ^(When memory allocation statistics are disabled, the
1649
+** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1650
+** interpreted as a boolean, which enables or disables the collection of
1651
+** memory allocation statistics. ^(When memory allocation statistics are disabled, the
16501652
** following SQLite interfaces become non-operational:
16511653
** <ul>
16521654
** <li> [sqlite3_memory_used()]
16531655
** <li> [sqlite3_memory_highwater()]
16541656
** <li> [sqlite3_soft_heap_limit64()]
@@ -1658,78 +1660,90 @@
16581660
** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
16591661
** allocation statistics are disabled by default.
16601662
** </dd>
16611663
**
16621664
** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1663
-** <dd> ^This option specifies a static memory buffer that SQLite can use for
1664
-** scratch memory. There are three arguments: A pointer an 8-byte
1665
+** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
1666
+** that SQLite can use for scratch memory. ^(There are three arguments
1667
+** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
16651668
** aligned memory buffer from which the scratch allocations will be
16661669
** drawn, the size of each scratch allocation (sz),
1667
-** and the maximum number of scratch allocations (N). The sz
1668
-** argument must be a multiple of 16.
1670
+** and the maximum number of scratch allocations (N).)^
16691671
** The first argument must be a pointer to an 8-byte aligned buffer
16701672
** of at least sz*N bytes of memory.
1671
-** ^SQLite will use no more than two scratch buffers per thread. So
1672
-** N should be set to twice the expected maximum number of threads.
1673
-** ^SQLite will never require a scratch buffer that is more than 6
1674
-** times the database page size. ^If SQLite needs needs additional
1673
+** ^SQLite will not use more than one scratch buffers per thread.
1674
+** ^SQLite will never request a scratch buffer that is more than 6
1675
+** times the database page size.
1676
+** ^If SQLite needs needs additional
16751677
** scratch memory beyond what is provided by this configuration option, then
1676
-** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1678
+** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
1679
+** ^When the application provides any amount of scratch memory using
1680
+** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
1681
+** [sqlite3_malloc|heap allocations].
1682
+** This can help [Robson proof|prevent memory allocation failures] due to heap
1683
+** fragmentation in low-memory embedded systems.
1684
+** </dd>
16771685
**
16781686
** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1679
-** <dd> ^This option specifies a static memory buffer that SQLite can use for
1680
-** the database page cache with the default page cache implementation.
1687
+** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
1688
+** that SQLite can use for the database page cache with the default page
1689
+** cache implementation.
16811690
** This configuration should not be used if an application-define page
1682
-** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1683
-** There are three arguments to this option: A pointer to 8-byte aligned
1691
+** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
1692
+** configuration option.
1693
+** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned
16841694
** memory, the size of each page buffer (sz), and the number of pages (N).
16851695
** The sz argument should be the size of the largest database page
1686
-** (a power of two between 512 and 32768) plus a little extra for each
1687
-** page header. ^The page header size is 20 to 40 bytes depending on
1688
-** the host architecture. ^It is harmless, apart from the wasted memory,
1689
-** to make sz a little too large. The first
1690
-** argument should point to an allocation of at least sz*N bytes of memory.
1696
+** (a power of two between 512 and 32768) plus some extra bytes for each
1697
+** page header. ^The number of extra bytes needed by the page header
1698
+** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option
1699
+** to [sqlite3_config()].
1700
+** ^It is harmless, apart from the wasted memory,
1701
+** for the sz parameter to be larger than necessary. The first
1702
+** argument should pointer to an 8-byte aligned block of memory that
1703
+** is at least sz*N bytes of memory, otherwise subsequent behavior is
1704
+** undefined.
16911705
** ^SQLite will use the memory provided by the first argument to satisfy its
16921706
** memory needs for the first N pages that it adds to cache. ^If additional
16931707
** page cache memory is needed beyond what is provided by this option, then
1694
-** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1695
-** The pointer in the first argument must
1696
-** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1697
-** will be undefined.</dd>
1708
+** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
16981709
**
16991710
** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1700
-** <dd> ^This option specifies a static memory buffer that SQLite will use
1701
-** for all of its dynamic memory allocation needs beyond those provided
1702
-** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1703
-** There are three arguments: An 8-byte aligned pointer to the memory,
1711
+** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1712
+** that SQLite will use for all of its dynamic memory allocation needs
1713
+** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1714
+** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1715
+** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1716
+** [SQLITE_ERROR] if invoked otherwise.
1717
+** ^There are three arguments to SQLITE_CONFIG_HEAP:
1718
+** An 8-byte aligned pointer to the memory,
17041719
** the number of bytes in the memory buffer, and the minimum allocation size.
17051720
** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
17061721
** to using its default memory allocator (the system malloc() implementation),
17071722
** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1708
-** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1709
-** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1723
+** memory pointer is not NULL then the alternative memory
17101724
** allocator is engaged to handle all of SQLites memory allocation needs.
17111725
** The first pointer (the memory pointer) must be aligned to an 8-byte
17121726
** boundary or subsequent behavior of SQLite will be undefined.
17131727
** The minimum allocation size is capped at 2**12. Reasonable values
17141728
** for the minimum allocation size are 2**5 through 2**8.</dd>
17151729
**
17161730
** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1717
-** <dd> ^(This option takes a single argument which is a pointer to an
1718
-** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1719
-** alternative low-level mutex routines to be used in place
1731
+** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1732
+** pointer to an instance of the [sqlite3_mutex_methods] structure.
1733
+** The argument specifies alternative low-level mutex routines to be used in place
17201734
** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
17211735
** content of the [sqlite3_mutex_methods] structure before the call to
17221736
** [sqlite3_config()] returns. ^If SQLite is compiled with
17231737
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
17241738
** the entire mutexing subsystem is omitted from the build and hence calls to
17251739
** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
17261740
** return [SQLITE_ERROR].</dd>
17271741
**
17281742
** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1729
-** <dd> ^(This option takes a single argument which is a pointer to an
1730
-** instance of the [sqlite3_mutex_methods] structure. The
1743
+** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1744
+** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
17311745
** [sqlite3_mutex_methods]
17321746
** structure is filled with the currently defined mutex routines.)^
17331747
** This option can be used to overload the default mutex allocation
17341748
** routines with a wrapper used to track mutex usage for performance
17351749
** profiling or testing, for example. ^If SQLite is compiled with
@@ -1737,28 +1751,28 @@
17371751
** the entire mutexing subsystem is omitted from the build and hence calls to
17381752
** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
17391753
** return [SQLITE_ERROR].</dd>
17401754
**
17411755
** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1742
-** <dd> ^(This option takes two arguments that determine the default
1743
-** memory allocation for the lookaside memory allocator on each
1744
-** [database connection]. The first argument is the
1756
+** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1757
+** the default size of lookaside memory on each [database connection].
1758
+** The first argument is the
17451759
** size of each lookaside buffer slot and the second is the number of
1746
-** slots allocated to each database connection.)^ ^(This option sets the
1747
-** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1748
-** verb to [sqlite3_db_config()] can be used to change the lookaside
1760
+** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
1761
+** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1762
+** option to [sqlite3_db_config()] can be used to change the lookaside
17491763
** configuration on individual connections.)^ </dd>
17501764
**
17511765
** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1752
-** <dd> ^(This option takes a single argument which is a pointer to
1753
-** an [sqlite3_pcache_methods2] object. This object specifies the interface
1754
-** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1755
-** object and uses it for page cache memory allocations.</dd>
1766
+** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1767
+** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
1768
+** the interface to a custom page cache implementation.)^
1769
+** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
17561770
**
17571771
** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1758
-** <dd> ^(This option takes a single argument which is a pointer to an
1759
-** [sqlite3_pcache_methods2] object. SQLite copies of the current
1772
+** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1773
+** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current
17601774
** page cache implementation into that object.)^ </dd>
17611775
**
17621776
** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
17631777
** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
17641778
** global [error log].
@@ -1778,26 +1792,27 @@
17781792
** supplied by the application must not invoke any SQLite interface.
17791793
** In a multi-threaded application, the application-defined logger
17801794
** function must be threadsafe. </dd>
17811795
**
17821796
** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1783
-** <dd>^(This option takes a single argument of type int. If non-zero, then
1784
-** URI handling is globally enabled. If the parameter is zero, then URI handling
1785
-** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1786
-** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1797
+** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1798
+** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1799
+** then URI handling is globally disabled.)^ ^If URI handling is globally enabled,
1800
+** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
17871801
** specified as part of [ATTACH] commands are interpreted as URIs, regardless
17881802
** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
17891803
** connection is opened. ^If it is globally disabled, filenames are
17901804
** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
17911805
** database connection is opened. ^(By default, URI handling is globally
17921806
** disabled. The default value may be changed by compiling with the
17931807
** [SQLITE_USE_URI] symbol defined.)^
17941808
**
17951809
** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1796
-** <dd>^This option takes a single integer argument which is interpreted as
1797
-** a boolean in order to enable or disable the use of covering indices for
1798
-** full table scans in the query optimizer. ^The default setting is determined
1810
+** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1811
+** argument which is interpreted as a boolean in order to enable or disable
1812
+** the use of covering indices for full table scans in the query optimizer.
1813
+** ^The default setting is determined
17991814
** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
18001815
** if that compile-time option is omitted.
18011816
** The ability to disable the use of covering indices for full table scans
18021817
** is because some incorrectly coded legacy applications might malfunction
18031818
** when the optimization is enabled. Providing the ability to
@@ -1833,23 +1848,32 @@
18331848
** that are the default mmap size limit (the default setting for
18341849
** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
18351850
** ^The default setting can be overridden by each database connection using
18361851
** either the [PRAGMA mmap_size] command, or by using the
18371852
** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1838
-** cannot be changed at run-time. Nor may the maximum allowed mmap size
1839
-** exceed the compile-time maximum mmap size set by the
1853
+** will be silently truncated if necessary so that it does not exceed the
1854
+** compile-time maximum mmap size set by the
18401855
** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
18411856
** ^If either argument to this option is negative, then that argument is
18421857
** changed to its compile-time default.
18431858
**
18441859
** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
18451860
** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1846
-** <dd>^This option is only available if SQLite is compiled for Windows
1847
-** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1848
-** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1861
+** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1862
+** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1863
+** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
18491864
** that specifies the maximum size of the created heap.
18501865
** </dl>
1866
+**
1867
+** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1868
+** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1869
+** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1870
+** is a pointer to an integer and writes into that integer the number of extra
1871
+** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of
1872
+** extra space required can change depending on the compiler,
1873
+** target platform, and SQLite version.
1874
+** </dl>
18511875
*/
18521876
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
18531877
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
18541878
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
18551879
#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1870,10 +1894,11 @@
18701894
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
18711895
#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
18721896
#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
18731897
#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
18741898
#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
1899
+#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
18751900
18761901
/*
18771902
** CAPI3REF: Database Connection Configuration Options
18781903
**
18791904
** These constants are the available integer configuration options that
@@ -1997,51 +2022,49 @@
19972022
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
19982023
19992024
/*
20002025
** CAPI3REF: Count The Number Of Rows Modified
20012026
**
2002
-** ^This function returns the number of database rows that were changed
2003
-** or inserted or deleted by the most recently completed SQL statement
2004
-** on the [database connection] specified by the first parameter.
2005
-** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
2006
-** or [DELETE] statement are counted. Auxiliary changes caused by
2007
-** triggers or [foreign key actions] are not counted.)^ Use the
2008
-** [sqlite3_total_changes()] function to find the total number of changes
2009
-** including changes caused by triggers and foreign key actions.
2010
-**
2011
-** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
2012
-** are not counted. Only real table changes are counted.
2013
-**
2014
-** ^(A "row change" is a change to a single row of a single table
2015
-** caused by an INSERT, DELETE, or UPDATE statement. Rows that
2016
-** are changed as side effects of [REPLACE] constraint resolution,
2017
-** rollback, ABORT processing, [DROP TABLE], or by any other
2018
-** mechanisms do not count as direct row changes.)^
2019
-**
2020
-** A "trigger context" is a scope of execution that begins and
2021
-** ends with the script of a [CREATE TRIGGER | trigger].
2022
-** Most SQL statements are
2023
-** evaluated outside of any trigger. This is the "top level"
2024
-** trigger context. If a trigger fires from the top level, a
2025
-** new trigger context is entered for the duration of that one
2026
-** trigger. Subtriggers create subcontexts for their duration.
2027
-**
2028
-** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2029
-** not create a new trigger context.
2030
-**
2031
-** ^This function returns the number of direct row changes in the
2032
-** most recent INSERT, UPDATE, or DELETE statement within the same
2033
-** trigger context.
2034
-**
2035
-** ^Thus, when called from the top level, this function returns the
2036
-** number of changes in the most recent INSERT, UPDATE, or DELETE
2037
-** that also occurred at the top level. ^(Within the body of a trigger,
2038
-** the sqlite3_changes() interface can be called to find the number of
2039
-** changes in the most recently completed INSERT, UPDATE, or DELETE
2040
-** statement within the body of the same trigger.
2041
-** However, the number returned does not include changes
2042
-** caused by subtriggers since those have their own context.)^
2027
+** ^This function returns the number of rows modified, inserted or
2028
+** deleted by the most recently completed INSERT, UPDATE or DELETE
2029
+** statement on the database connection specified by the only parameter.
2030
+** ^Executing any other type of SQL statement does not modify the value
2031
+** returned by this function.
2032
+**
2033
+** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2034
+** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2035
+** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2036
+**
2037
+** Changes to a view that are intercepted by
2038
+** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2039
+** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2040
+** DELETE statement run on a view is always zero. Only changes made to real
2041
+** tables are counted.
2042
+**
2043
+** Things are more complicated if the sqlite3_changes() function is
2044
+** executed while a trigger program is running. This may happen if the
2045
+** program uses the [changes() SQL function], or if some other callback
2046
+** function invokes sqlite3_changes() directly. Essentially:
2047
+**
2048
+** <ul>
2049
+** <li> ^(Before entering a trigger program the value returned by
2050
+** sqlite3_changes() function is saved. After the trigger program
2051
+** has finished, the original value is restored.)^
2052
+**
2053
+** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2054
+** statement sets the value returned by sqlite3_changes()
2055
+** upon completion as normal. Of course, this value will not include
2056
+** any changes performed by sub-triggers, as the sqlite3_changes()
2057
+** value will be saved and restored after each sub-trigger has run.)^
2058
+** </ul>
2059
+**
2060
+** ^This means that if the changes() SQL function (or similar) is used
2061
+** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2062
+** returns the value as set when the calling statement began executing.
2063
+** ^If it is used by the second or subsequent such statement within a trigger
2064
+** program, the value returned reflects the number of rows modified by the
2065
+** previous INSERT, UPDATE or DELETE statement within the same trigger.
20432066
**
20442067
** See also the [sqlite3_total_changes()] interface, the
20452068
** [count_changes pragma], and the [changes() SQL function].
20462069
**
20472070
** If a separate thread makes changes on the same database connection
@@ -2051,24 +2074,21 @@
20512074
SQLITE_API int sqlite3_changes(sqlite3*);
20522075
20532076
/*
20542077
** CAPI3REF: Total Number Of Rows Modified
20552078
**
2056
-** ^This function returns the number of row changes caused by [INSERT],
2057
-** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2058
-** ^(The count returned by sqlite3_total_changes() includes all changes
2059
-** from all [CREATE TRIGGER | trigger] contexts and changes made by
2060
-** [foreign key actions]. However,
2061
-** the count does not include changes used to implement [REPLACE] constraints,
2062
-** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
2063
-** count does not include rows of views that fire an [INSTEAD OF trigger],
2064
-** though if the INSTEAD OF trigger makes changes of its own, those changes
2065
-** are counted.)^
2066
-** ^The sqlite3_total_changes() function counts the changes as soon as
2067
-** the statement that makes them is completed (when the statement handle
2068
-** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2069
-**
2079
+** ^This function returns the total number of rows inserted, modified or
2080
+** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2081
+** since the database connection was opened, including those executed as
2082
+** part of trigger programs. ^Executing any other type of SQL statement
2083
+** does not affect the value returned by sqlite3_total_changes().
2084
+**
2085
+** ^Changes made as part of [foreign key actions] are included in the
2086
+** count, but those made as part of REPLACE constraint resolution are
2087
+** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2088
+** are not counted.
2089
+**
20702090
** See also the [sqlite3_changes()] interface, the
20712091
** [count_changes pragma], and the [total_changes() SQL function].
20722092
**
20732093
** If a separate thread makes changes on the same database connection
20742094
** while [sqlite3_total_changes()] is running then the value
@@ -2542,17 +2562,18 @@
25422562
** already uses the largest possible [ROWID]. The PRNG is also used for
25432563
** the build-in random() and randomblob() SQL functions. This interface allows
25442564
** applications to access the same PRNG for other purposes.
25452565
**
25462566
** ^A call to this routine stores N bytes of randomness into buffer P.
2547
-** ^If N is less than one, then P can be a NULL pointer.
2567
+** ^The P parameter can be a NULL pointer.
25482568
**
25492569
** ^If this routine has not been previously called or if the previous
2550
-** call had N less than one, then the PRNG is seeded using randomness
2551
-** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2552
-** ^If the previous call to this routine had an N of 1 or more then
2553
-** the pseudo-randomness is generated
2570
+** call had N less than one or a NULL pointer for P, then the PRNG is
2571
+** seeded using randomness obtained from the xRandomness method of
2572
+** the default [sqlite3_vfs] object.
2573
+** ^If the previous call to this routine had an N of 1 or more and a
2574
+** non-NULL P then the pseudo-randomness is generated
25542575
** internally and without recourse to the [sqlite3_vfs] xRandomness
25552576
** method.
25562577
*/
25572578
SQLITE_API void sqlite3_randomness(int N, void *P);
25582579
@@ -5762,31 +5783,47 @@
57625783
** in other words, the same BLOB that would be selected by:
57635784
**
57645785
** <pre>
57655786
** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
57665787
** </pre>)^
5788
+**
5789
+** ^(Parameter zDb is not the filename that contains the database, but
5790
+** rather the symbolic name of the database. For attached databases, this is
5791
+** the name that appears after the AS keyword in the [ATTACH] statement.
5792
+** For the main database file, the database name is "main". For TEMP
5793
+** tables, the database name is "temp".)^
57675794
**
57685795
** ^If the flags parameter is non-zero, then the BLOB is opened for read
5769
-** and write access. ^If it is zero, the BLOB is opened for read access.
5770
-** ^It is not possible to open a column that is part of an index or primary
5771
-** key for writing. ^If [foreign key constraints] are enabled, it is
5772
-** not possible to open a column that is part of a [child key] for writing.
5773
-**
5774
-** ^Note that the database name is not the filename that contains
5775
-** the database but rather the symbolic name of the database that
5776
-** appears after the AS keyword when the database is connected using [ATTACH].
5777
-** ^For the main database file, the database name is "main".
5778
-** ^For TEMP tables, the database name is "temp".
5779
-**
5780
-** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5781
-** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5782
-** to be a null pointer.)^
5783
-** ^This function sets the [database connection] error code and message
5784
-** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5785
-** functions. ^Note that the *ppBlob variable is always initialized in a
5786
-** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5787
-** regardless of the success or failure of this routine.
5796
+** and write access. ^If the flags parameter is zero, the BLOB is opened for
5797
+** read-only access.
5798
+**
5799
+** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
5800
+** in *ppBlob. Otherwise an [error code] is returned and, unless the error
5801
+** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
5802
+** the API is not misused, it is always safe to call [sqlite3_blob_close()]
5803
+** on *ppBlob after this function it returns.
5804
+**
5805
+** This function fails with SQLITE_ERROR if any of the following are true:
5806
+** <ul>
5807
+** <li> ^(Database zDb does not exist)^,
5808
+** <li> ^(Table zTable does not exist within database zDb)^,
5809
+** <li> ^(Table zTable is a WITHOUT ROWID table)^,
5810
+** <li> ^(Column zColumn does not exist)^,
5811
+** <li> ^(Row iRow is not present in the table)^,
5812
+** <li> ^(The specified column of row iRow contains a value that is not
5813
+** a TEXT or BLOB value)^,
5814
+** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
5815
+** constraint and the blob is being opened for read/write access)^,
5816
+** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
5817
+** column zColumn is part of a [child key] definition and the blob is
5818
+** being opened for read/write access)^.
5819
+** </ul>
5820
+**
5821
+** ^Unless it returns SQLITE_MISUSE, this function sets the
5822
+** [database connection] error code and message accessible via
5823
+** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5824
+**
57885825
**
57895826
** ^(If the row that a BLOB handle points to is modified by an
57905827
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
57915828
** then the BLOB handle is marked as "expired".
57925829
** This is true if any column of the row is changed, even a column
@@ -5800,17 +5837,13 @@
58005837
** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
58015838
** the opened blob. ^The size of a blob may not be changed by this
58025839
** interface. Use the [UPDATE] SQL command to change the size of a
58035840
** blob.
58045841
**
5805
-** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5806
-** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5807
-**
58085842
** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5809
-** and the built-in [zeroblob] SQL function can be used, if desired,
5810
-** to create an empty, zero-filled blob in which to read or write using
5811
-** this interface.
5843
+** and the built-in [zeroblob] SQL function may be used to create a
5844
+** zero-filled blob to read or write using the incremental-blob interface.
58125845
**
58135846
** To avoid a resource leak, every open [BLOB handle] should eventually
58145847
** be released by a call to [sqlite3_blob_close()].
58155848
*/
58165849
SQLITE_API int sqlite3_blob_open(
@@ -5848,28 +5881,26 @@
58485881
SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
58495882
58505883
/*
58515884
** CAPI3REF: Close A BLOB Handle
58525885
**
5853
-** ^Closes an open [BLOB handle].
5854
-**
5855
-** ^Closing a BLOB shall cause the current transaction to commit
5856
-** if there are no other BLOBs, no pending prepared statements, and the
5857
-** database connection is in [autocommit mode].
5858
-** ^If any writes were made to the BLOB, they might be held in cache
5859
-** until the close operation if they will fit.
5860
-**
5861
-** ^(Closing the BLOB often forces the changes
5862
-** out to disk and so if any I/O errors occur, they will likely occur
5863
-** at the time when the BLOB is closed. Any errors that occur during
5864
-** closing are reported as a non-zero return value.)^
5865
-**
5866
-** ^(The BLOB is closed unconditionally. Even if this routine returns
5867
-** an error code, the BLOB is still closed.)^
5868
-**
5869
-** ^Calling this routine with a null pointer (such as would be returned
5870
-** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5886
+** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
5887
+** unconditionally. Even if this routine returns an error code, the
5888
+** handle is still closed.)^
5889
+**
5890
+** ^If the blob handle being closed was opened for read-write access, and if
5891
+** the database is in auto-commit mode and there are no other open read-write
5892
+** blob handles or active write statements, the current transaction is
5893
+** committed. ^If an error occurs while committing the transaction, an error
5894
+** code is returned and the transaction rolled back.
5895
+**
5896
+** Calling this function with an argument that is not a NULL pointer or an
5897
+** open blob handle results in undefined behaviour. ^Calling this routine
5898
+** with a null pointer (such as would be returned by a failed call to
5899
+** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
5900
+** is passed a valid open blob handle, the values returned by the
5901
+** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
58715902
*/
58725903
SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
58735904
58745905
/*
58755906
** CAPI3REF: Return The Size Of An Open BLOB
@@ -5915,36 +5946,39 @@
59155946
SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
59165947
59175948
/*
59185949
** CAPI3REF: Write Data Into A BLOB Incrementally
59195950
**
5920
-** ^This function is used to write data into an open [BLOB handle] from a
5921
-** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5922
-** into the open BLOB, starting at offset iOffset.
5951
+** ^(This function is used to write data into an open [BLOB handle] from a
5952
+** caller-supplied buffer. N bytes of data are copied from the buffer Z
5953
+** into the open BLOB, starting at offset iOffset.)^
5954
+**
5955
+** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5956
+** Otherwise, an [error code] or an [extended error code] is returned.)^
5957
+** ^Unless SQLITE_MISUSE is returned, this function sets the
5958
+** [database connection] error code and message accessible via
5959
+** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
59235960
**
59245961
** ^If the [BLOB handle] passed as the first argument was not opened for
59255962
** writing (the flags parameter to [sqlite3_blob_open()] was zero),
59265963
** this function returns [SQLITE_READONLY].
59275964
**
5928
-** ^This function may only modify the contents of the BLOB; it is
5965
+** This function may only modify the contents of the BLOB; it is
59295966
** not possible to increase the size of a BLOB using this API.
59305967
** ^If offset iOffset is less than N bytes from the end of the BLOB,
5931
-** [SQLITE_ERROR] is returned and no data is written. ^If N is
5932
-** less than zero [SQLITE_ERROR] is returned and no data is written.
5933
-** The size of the BLOB (and hence the maximum value of N+iOffset)
5934
-** can be determined using the [sqlite3_blob_bytes()] interface.
5968
+** [SQLITE_ERROR] is returned and no data is written. The size of the
5969
+** BLOB (and hence the maximum value of N+iOffset) can be determined
5970
+** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
5971
+** than zero [SQLITE_ERROR] is returned and no data is written.
59355972
**
59365973
** ^An attempt to write to an expired [BLOB handle] fails with an
59375974
** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
59385975
** before the [BLOB handle] expired are not rolled back by the
59395976
** expiration of the handle, though of course those changes might
59405977
** have been overwritten by the statement that expired the BLOB handle
59415978
** or by other independent statements.
59425979
**
5943
-** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5944
-** Otherwise, an [error code] or an [extended error code] is returned.)^
5945
-**
59465980
** This routine only works on a [BLOB handle] which has been created
59475981
** by a prior successful call to [sqlite3_blob_open()] and which has not
59485982
** been closed by [sqlite3_blob_close()]. Passing any other pointer in
59495983
** to this routine results in undefined and probably undesirable behavior.
59505984
**
@@ -7533,10 +7567,102 @@
75337567
/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
75347568
#define SQLITE_FAIL 3
75357569
/* #define SQLITE_ABORT 4 // Also an error code */
75367570
#define SQLITE_REPLACE 5
75377571
7572
+/*
7573
+** CAPI3REF: Prepared Statement Scan Status Opcodes
7574
+** KEYWORDS: {scanstatus options}
7575
+**
7576
+** The following constants can be used for the T parameter to the
7577
+** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
7578
+** different metric for sqlite3_stmt_scanstatus() to return.
7579
+**
7580
+** <dl>
7581
+** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
7582
+** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7583
+** total number of times that the X-th loop has run.</dd>
7584
+**
7585
+** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
7586
+** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7587
+** total number of rows examined by all iterations of the X-th loop.</dd>
7588
+**
7589
+** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
7590
+** <dd>^The "double" variable pointed to by the T parameter will be set to the
7591
+** query planner's estimate for the average number of rows output from each
7592
+** iteration of the X-th loop. If the query planner's estimates was accurate,
7593
+** then this value will approximate the quotient NVISIT/NLOOP and the
7594
+** product of this value for all prior loops with the same SELECTID will
7595
+** be the NLOOP value for the current loop.
7596
+**
7597
+** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
7598
+** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7599
+** a zero-terminated UTF-8 string containing the name of the index or table used
7600
+** for the X-th loop.
7601
+**
7602
+** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
7603
+** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7604
+** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description
7605
+** for the X-th loop.
7606
+**
7607
+** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
7608
+** <dd>^The "int" variable pointed to by the T parameter will be set to the
7609
+** "select-id" for the X-th loop. The select-id identifies which query or
7610
+** subquery the loop is part of. The main query has a select-id of zero.
7611
+** The select-id is the same value as is output in the first column
7612
+** of an [EXPLAIN QUERY PLAN] query.
7613
+** </dl>
7614
+*/
7615
+#define SQLITE_SCANSTAT_NLOOP 0
7616
+#define SQLITE_SCANSTAT_NVISIT 1
7617
+#define SQLITE_SCANSTAT_EST 2
7618
+#define SQLITE_SCANSTAT_NAME 3
7619
+#define SQLITE_SCANSTAT_EXPLAIN 4
7620
+#define SQLITE_SCANSTAT_SELECTID 5
7621
+
7622
+/*
7623
+** CAPI3REF: Prepared Statement Scan Status
7624
+**
7625
+** Return status data for a single loop within query pStmt.
7626
+**
7627
+** The "iScanStatusOp" parameter determines which status information to return.
7628
+** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of
7629
+** this interface is undefined.
7630
+** ^The requested measurement is written into a variable pointed to by
7631
+** the "pOut" parameter.
7632
+** Parameter "idx" identifies the specific loop to retrieve statistics for.
7633
+** Loops are numbered starting from zero. ^If idx is out of range - less than
7634
+** zero or greater than or equal to the total number of loops used to implement
7635
+** the statement - a non-zero value is returned and the variable that pOut
7636
+** points to is unchanged.
7637
+**
7638
+** ^Statistics might not be available for all loops in all statements. ^In cases
7639
+** where there exist loops with no available statistics, this function behaves
7640
+** as if the loop did not exist - it returns non-zero and leave the variable
7641
+** that pOut points to unchanged.
7642
+**
7643
+** This API is only available if the library is built with pre-processor
7644
+** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7645
+**
7646
+** See also: [sqlite3_stmt_scanstatus_reset()]
7647
+*/
7648
+SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
7649
+ sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7650
+ int idx, /* Index of loop to report on */
7651
+ int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7652
+ void *pOut /* Result written here */
7653
+);
7654
+
7655
+/*
7656
+** CAPI3REF: Zero Scan-Status Counters
7657
+**
7658
+** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7659
+**
7660
+** This API is only available if the library is built with pre-processor
7661
+** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7662
+*/
7663
+SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
75387664
75397665
75407666
/*
75417667
** Undo the hack that converts floating point types to integer for
75427668
** builds on processors without floating point support.
@@ -7978,14 +8104,13 @@
79788104
#ifndef SQLITE_POWERSAFE_OVERWRITE
79798105
# define SQLITE_POWERSAFE_OVERWRITE 1
79808106
#endif
79818107
79828108
/*
7983
-** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
7984
-** It determines whether or not the features related to
7985
-** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
7986
-** be overridden at runtime using the sqlite3_config() API.
8109
+** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
8110
+** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
8111
+** which case memory allocation statistics are disabled by default.
79878112
*/
79888113
#if !defined(SQLITE_DEFAULT_MEMSTATUS)
79898114
# define SQLITE_DEFAULT_MEMSTATUS 1
79908115
#endif
79918116
@@ -8611,11 +8736,11 @@
86118736
** Estimated quantities used for query planning are stored as 16-bit
86128737
** logarithms. For quantity X, the value stored is 10*log2(X). This
86138738
** gives a possible range of values of approximately 1.0e986 to 1e-986.
86148739
** But the allowed values are "grainy". Not every value is representable.
86158740
** For example, quantities 16 and 17 are both represented by a LogEst
8616
-** of 40. However, since LogEst quantaties are suppose to be estimates,
8741
+** of 40. However, since LogEst quantities are suppose to be estimates,
86178742
** not exact values, this imprecision is not a problem.
86188743
**
86198744
** "LogEst" is short for "Logarithmic Estimate".
86208745
**
86218746
** Examples:
@@ -9011,11 +9136,11 @@
90119136
SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
90129137
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
90139138
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
90149139
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
90159140
SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
9016
-SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
9141
+SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
90179142
SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
90189143
SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
90199144
SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
90209145
SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
90219146
SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
@@ -9044,11 +9169,11 @@
90449169
#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
90459170
90469171
SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
90479172
SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
90489173
SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
9049
-SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
9174
+SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int, int);
90509175
90519176
SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
90529177
SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
90539178
90549179
SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
@@ -9124,10 +9249,11 @@
91249249
SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
91259250
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
91269251
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
91279252
SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
91289253
SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9254
+SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
91299255
91309256
#ifndef NDEBUG
91319257
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
91329258
#endif
91339259
@@ -9666,10 +9792,16 @@
96669792
# define VdbeCoverageAlwaysTaken(v)
96679793
# define VdbeCoverageNeverTaken(v)
96689794
# define VDBE_OFFSET_LINENO(x) 0
96699795
#endif
96709796
9797
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
9798
+SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
9799
+#else
9800
+# define sqlite3VdbeScanStatus(a,b,c,d,e)
9801
+#endif
9802
+
96719803
#endif
96729804
96739805
/************** End of vdbe.h ************************************************/
96749806
/************** Continuing where we left off in sqliteInt.h ******************/
96759807
/************** Include pager.h in the middle of sqliteInt.h *****************/
@@ -9862,10 +9994,12 @@
98629994
SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
98639995
98649996
/* Functions used to truncate the database file. */
98659997
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
98669998
9999
+SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
10000
+
986710001
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
986810002
SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
986910003
#endif
987010004
987110005
/* Functions to support testing and debugging. */
@@ -10049,10 +10183,14 @@
1004910183
SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
1005010184
#endif
1005110185
1005210186
SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
1005310187
10188
+/* Return the header size */
10189
+SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
10190
+SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
10191
+
1005410192
#endif /* _PCACHE_H_ */
1005510193
1005610194
/************** End of pcache.h **********************************************/
1005710195
/************** Continuing where we left off in sqliteInt.h ******************/
1005810196
@@ -10735,11 +10873,11 @@
1073510873
#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
1073610874
#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
1073710875
#define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */
1073810876
#define SQLITE_Transitive 0x0200 /* Transitive constraints */
1073910877
#define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */
10740
-#define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */
10878
+#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */
1074110879
#define SQLITE_AllOpts 0xffff /* All optimizations */
1074210880
1074310881
/*
1074410882
** Macros for testing whether or not optimizations are enabled or disabled.
1074510883
*/
@@ -11322,11 +11460,12 @@
1132211460
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1132311461
int nSample; /* Number of elements in aSample[] */
1132411462
int nSampleCol; /* Size of IndexSample.anEq[] and so on */
1132511463
tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
1132611464
IndexSample *aSample; /* Samples of the left-most key */
11327
- tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */
11465
+ tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
11466
+ tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
1132811467
#endif
1132911468
};
1133011469
1133111470
/*
1133211471
** Allowed values for Index.idxType
@@ -11520,11 +11659,11 @@
1152011659
int nHeight; /* Height of the tree headed by this node */
1152111660
#endif
1152211661
int iTable; /* TK_COLUMN: cursor number of table holding column
1152311662
** TK_REGISTER: register number
1152411663
** TK_TRIGGER: 1 -> new, 0 -> old
11525
- ** EP_Unlikely: 1000 times likelihood */
11664
+ ** EP_Unlikely: 134217728 times likelihood */
1152611665
ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
1152711666
** TK_VARIABLE: variable number (always >= 1). */
1152811667
i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
1152911668
i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
1153011669
u8 op2; /* TK_REGISTER: original value of Expr.op
@@ -12412,13 +12551,15 @@
1241212551
int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
1241312552
int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
1241412553
void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
1241512554
Parse *pParse; /* Parser context. */
1241612555
int walkerDepth; /* Number of subqueries */
12556
+ u8 eCode; /* A small processing code */
1241712557
union { /* Extra data for callback */
1241812558
NameContext *pNC; /* Naming context */
12419
- int i; /* Integer value */
12559
+ int n; /* A counter */
12560
+ int iCur; /* A cursor number */
1242012561
SrcList *pSrcList; /* FROM clause */
1242112562
struct SrcCount *pSrcCount; /* Counting column references */
1242212563
} u;
1242312564
};
1242412565
@@ -12815,10 +12956,11 @@
1281512956
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
1281612957
SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
1281712958
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
1281812959
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
1281912960
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
12961
+SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
1282012962
SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
1282112963
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
1282212964
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
1282312965
SQLITE_PRIVATE int sqlite3IsRowid(const char*);
1282412966
SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -13060,11 +13202,11 @@
1306013202
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
1306113203
1306213204
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
1306313205
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
1306413206
SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
13065
-SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
13207
+SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
1306613208
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
1306713209
SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
1306813210
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
1306913211
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
1307013212
@@ -13472,15 +13614,23 @@
1347213614
** compatibility for legacy applications, the URI filename capability is
1347313615
** disabled by default.
1347413616
**
1347513617
** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
1347613618
** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
13619
+**
13620
+** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
13621
+** disabled. The default value may be changed by compiling with the
13622
+** SQLITE_USE_URI symbol defined.
1347713623
*/
1347813624
#ifndef SQLITE_USE_URI
1347913625
# define SQLITE_USE_URI 0
1348013626
#endif
1348113627
13628
+/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
13629
+** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
13630
+** that compile-time option is omitted.
13631
+*/
1348213632
#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
1348313633
# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
1348413634
#endif
1348513635
1348613636
/*
@@ -13566,12 +13716,12 @@
1356613716
** than 1 GiB. The sqlite3_test_control() interface can be used to
1356713717
** move the pending byte.
1356813718
**
1356913719
** IMPORTANT: Changing the pending byte to any value other than
1357013720
** 0x40000000 results in an incompatible database file format!
13571
-** Changing the pending byte during operating results in undefined
13572
-** and dileterious behavior.
13721
+** Changing the pending byte during operation will result in undefined
13722
+** and incorrect behavior.
1357313723
*/
1357413724
#ifndef SQLITE_OMIT_WSD
1357513725
SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
1357613726
#endif
1357713727
@@ -13646,10 +13796,13 @@
1364613796
#ifdef SQLITE_DISABLE_DIRSYNC
1364713797
"DISABLE_DIRSYNC",
1364813798
#endif
1364913799
#ifdef SQLITE_DISABLE_LFS
1365013800
"DISABLE_LFS",
13801
+#endif
13802
+#ifdef SQLITE_ENABLE_API_ARMOR
13803
+ "ENABLE_API_ARMOR",
1365113804
#endif
1365213805
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
1365313806
"ENABLE_ATOMIC_WRITE",
1365413807
#endif
1365513808
#ifdef SQLITE_ENABLE_CEROD
@@ -13972,10 +14125,17 @@
1397214125
** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
1397314126
** is not required for a match.
1397414127
*/
1397514128
SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
1397614129
int i, n;
14130
+
14131
+#ifdef SQLITE_ENABLE_API_ARMOR
14132
+ if( zOptName==0 ){
14133
+ (void)SQLITE_MISUSE_BKPT;
14134
+ return 0;
14135
+ }
14136
+#endif
1397714137
if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
1397814138
n = sqlite3Strlen30(zOptName);
1397914139
1398014140
/* Since ArraySize(azCompileOpt) is normally in single digits, a
1398114141
** linear search is adequate. No need for a binary search. */
@@ -14153,10 +14313,11 @@
1415314313
typedef struct VdbeFrame VdbeFrame;
1415414314
struct VdbeFrame {
1415514315
Vdbe *v; /* VM this frame belongs to */
1415614316
VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
1415714317
Op *aOp; /* Program instructions for parent frame */
14318
+ i64 *anExec; /* Event counters from parent frame */
1415814319
Mem *aMem; /* Array of memory cells for parent frame */
1415914320
u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
1416014321
VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
1416114322
void *token; /* Copy of SubProgram.token */
1416214323
i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
@@ -14165,11 +14326,12 @@
1416514326
int nOp; /* Size of aOp array */
1416614327
int nMem; /* Number of entries in aMem */
1416714328
int nOnceFlag; /* Number of entries in aOnceFlag */
1416814329
int nChildMem; /* Number of memory cells for child frame */
1416914330
int nChildCsr; /* Number of cursors for child frame */
14170
- int nChange; /* Statement changes (Vdbe.nChanges) */
14331
+ int nChange; /* Statement changes (Vdbe.nChange) */
14332
+ int nDbChange; /* Value of db->nChange */
1417114333
};
1417214334
1417314335
#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
1417414336
1417514337
/*
@@ -14316,10 +14478,20 @@
1431614478
/* A bitfield type for use inside of structures. Always follow with :N where
1431714479
** N is the number of bits.
1431814480
*/
1431914481
typedef unsigned bft; /* Bit Field Type */
1432014482
14483
+typedef struct ScanStatus ScanStatus;
14484
+struct ScanStatus {
14485
+ int addrExplain; /* OP_Explain for loop */
14486
+ int addrLoop; /* Address of "loops" counter */
14487
+ int addrVisit; /* Address of "rows visited" counter */
14488
+ int iSelectID; /* The "Select-ID" for this loop */
14489
+ LogEst nEst; /* Estimated output rows per loop */
14490
+ char *zName; /* Name of table or index */
14491
+};
14492
+
1432114493
/*
1432214494
** An instance of the virtual machine. This structure contains the complete
1432314495
** state of the virtual machine.
1432414496
**
1432514497
** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -14388,10 +14560,15 @@
1438814560
u32 expmask; /* Binding to these vars invalidates VM */
1438914561
SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
1439014562
int nOnceFlag; /* Size of array aOnceFlag[] */
1439114563
u8 *aOnceFlag; /* Flags for OP_Once */
1439214564
AuxData *pAuxData; /* Linked list of auxdata allocations */
14565
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14566
+ i64 *anExec; /* Number of times each op has been executed */
14567
+ int nScan; /* Entries in aScan[] */
14568
+ ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
14569
+#endif
1439314570
};
1439414571
1439514572
/*
1439614573
** The following are allowed values for Vdbe.magic
1439714574
*/
@@ -14577,10 +14754,13 @@
1457714754
SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
1457814755
wsdStatInit;
1457914756
if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
1458014757
return SQLITE_MISUSE_BKPT;
1458114758
}
14759
+#ifdef SQLITE_ENABLE_API_ARMOR
14760
+ if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
14761
+#endif
1458214762
*pCurrent = wsdStat.nowValue[op];
1458314763
*pHighwater = wsdStat.mxValue[op];
1458414764
if( resetFlag ){
1458514765
wsdStat.mxValue[op] = wsdStat.nowValue[op];
1458614766
}
@@ -14596,10 +14776,15 @@
1459614776
int *pCurrent, /* Write current value here */
1459714777
int *pHighwater, /* Write high-water mark here */
1459814778
int resetFlag /* Reset high-water mark if true */
1459914779
){
1460014780
int rc = SQLITE_OK; /* Return code */
14781
+#ifdef SQLITE_ENABLE_API_ARMOR
14782
+ if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
14783
+ return SQLITE_MISUSE_BKPT;
14784
+ }
14785
+#endif
1460114786
sqlite3_mutex_enter(db->mutex);
1460214787
switch( op ){
1460314788
case SQLITE_DBSTATUS_LOOKASIDE_USED: {
1460414789
*pCurrent = db->lookaside.nOut;
1460514790
*pHighwater = db->lookaside.mxOut;
@@ -14774,11 +14959,11 @@
1477414959
**
1477514960
** There is only one exported symbol in this file - the function
1477614961
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
1477714962
** All other code has file scope.
1477814963
**
14779
-** SQLite processes all times and dates as Julian Day numbers. The
14964
+** SQLite processes all times and dates as julian day numbers. The
1478014965
** dates and times are stored as the number of days since noon
1478114966
** in Greenwich on November 24, 4714 B.C. according to the Gregorian
1478214967
** calendar system.
1478314968
**
1478414969
** 1970-01-01 00:00:00 is JD 2440587.5
@@ -14789,11 +14974,11 @@
1478914974
** be represented, even though julian day numbers allow a much wider
1479014975
** range of dates.
1479114976
**
1479214977
** The Gregorian calendar system is used for all dates and times,
1479314978
** even those that predate the Gregorian calendar. Historians usually
14794
-** use the Julian calendar for dates prior to 1582-10-15 and for some
14979
+** use the julian calendar for dates prior to 1582-10-15 and for some
1479514980
** dates afterwards, depending on locale. Beware of this difference.
1479614981
**
1479714982
** The conversion algorithms are implemented based on descriptions
1479814983
** in the following text:
1479914984
**
@@ -15061,11 +15246,11 @@
1506115246
return 1;
1506215247
}
1506315248
}
1506415249
1506515250
/*
15066
-** Attempt to parse the given string into a Julian Day Number. Return
15251
+** Attempt to parse the given string into a julian day number. Return
1506715252
** the number of errors.
1506815253
**
1506915254
** The following are acceptable forms for the input string:
1507015255
**
1507115256
** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
@@ -15632,11 +15817,11 @@
1563215817
**
1563315818
** %d day of month
1563415819
** %f ** fractional seconds SS.SSS
1563515820
** %H hour 00-24
1563615821
** %j day of year 000-366
15637
-** %J ** Julian day number
15822
+** %J ** julian day number
1563815823
** %m month 01-12
1563915824
** %M minute 00-59
1564015825
** %s seconds since 1970-01-01
1564115826
** %S seconds 00-59
1564215827
** %w day of week 0-6 sunday==0
@@ -16257,10 +16442,14 @@
1625716442
MUTEX_LOGIC(sqlite3_mutex *mutex;)
1625816443
#ifndef SQLITE_OMIT_AUTOINIT
1625916444
int rc = sqlite3_initialize();
1626016445
if( rc ) return rc;
1626116446
#endif
16447
+#ifdef SQLITE_ENABLE_API_ARMOR
16448
+ if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
16449
+#endif
16450
+
1626216451
MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
1626316452
sqlite3_mutex_enter(mutex);
1626416453
vfsUnlink(pVfs);
1626516454
if( makeDflt || vfsList==0 ){
1626616455
pVfs->pNext = vfsList;
@@ -18614,10 +18803,11 @@
1861418803
** Retrieve a pointer to a static mutex or allocate a new dynamic one.
1861518804
*/
1861618805
SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
1861718806
#ifndef SQLITE_OMIT_AUTOINIT
1861818807
if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
18808
+ if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
1861918809
#endif
1862018810
return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
1862118811
}
1862218812
1862318813
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -19070,12 +19260,16 @@
1907019260
pthread_mutex_init(&p->mutex, 0);
1907119261
}
1907219262
break;
1907319263
}
1907419264
default: {
19075
- assert( iType-2 >= 0 );
19076
- assert( iType-2 < ArraySize(staticMutexes) );
19265
+#ifdef SQLITE_ENABLE_API_ARMOR
19266
+ if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
19267
+ (void)SQLITE_MISUSE_BKPT;
19268
+ return 0;
19269
+ }
19270
+#endif
1907719271
p = &staticMutexes[iType-2];
1907819272
#if SQLITE_MUTEX_NREF
1907919273
p->id = iType;
1908019274
#endif
1908119275
break;
@@ -20293,15 +20487,16 @@
2029320487
}
2029420488
assert( sqlite3_mutex_notheld(mem0.mutex) );
2029520489
2029620490
2029720491
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
20298
- /* Verify that no more than two scratch allocations per thread
20299
- ** are outstanding at one time. (This is only checked in the
20300
- ** single-threaded case since checking in the multi-threaded case
20301
- ** would be much more complicated.) */
20302
- assert( scratchAllocOut<=1 );
20492
+ /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
20493
+ ** buffers per thread.
20494
+ **
20495
+ ** This can only be checked in single-threaded mode.
20496
+ */
20497
+ assert( scratchAllocOut==0 );
2030320498
if( p ) scratchAllocOut++;
2030420499
#endif
2030520500
2030620501
return p;
2030720502
}
@@ -20945,11 +21140,11 @@
2094521140
sqlite_uint64 longvalue; /* Value for integer types */
2094621141
LONGDOUBLE_TYPE realvalue; /* Value for real types */
2094721142
const et_info *infop; /* Pointer to the appropriate info structure */
2094821143
char *zOut; /* Rendering buffer */
2094921144
int nOut; /* Size of the rendering buffer */
20950
- char *zExtra; /* Malloced memory used by some conversion */
21145
+ char *zExtra = 0; /* Malloced memory used by some conversion */
2095121146
#ifndef SQLITE_OMIT_FLOATING_POINT
2095221147
int exp, e2; /* exponent of real numbers */
2095321148
int nsd; /* Number of significant digits returned */
2095421149
double rounder; /* Used for rounding floating point values */
2095521150
etByte flag_dp; /* True if decimal point should be shown */
@@ -20956,10 +21151,17 @@
2095621151
etByte flag_rtz; /* True if trailing zeros should be removed */
2095721152
#endif
2095821153
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
2095921154
char buf[etBUFSIZE]; /* Conversion buffer */
2096021155
21156
+#ifdef SQLITE_ENABLE_API_ARMOR
21157
+ if( ap==0 ){
21158
+ (void)SQLITE_MISUSE_BKPT;
21159
+ sqlite3StrAccumReset(pAccum);
21160
+ return;
21161
+ }
21162
+#endif
2096121163
bufpt = 0;
2096221164
if( bFlags ){
2096321165
if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
2096421166
pArgList = va_arg(ap, PrintfArguments*);
2096521167
}
@@ -21062,11 +21264,10 @@
2106221264
return;
2106321265
}
2106421266
break;
2106521267
}
2106621268
}
21067
- zExtra = 0;
2106821269
2106921270
/*
2107021271
** At this point, variables are initialized as follows:
2107121272
**
2107221273
** flag_alternateform TRUE if a '#' is present.
@@ -21353,17 +21554,20 @@
2135321554
bufpt = getTextArg(pArgList);
2135421555
c = bufpt ? bufpt[0] : 0;
2135521556
}else{
2135621557
c = va_arg(ap,int);
2135721558
}
21358
- buf[0] = (char)c;
21359
- if( precision>=0 ){
21360
- for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
21361
- length = precision;
21362
- }else{
21363
- length =1;
21559
+ if( precision>1 ){
21560
+ width -= precision-1;
21561
+ if( width>1 && !flag_leftjustify ){
21562
+ sqlite3AppendChar(pAccum, width-1, ' ');
21563
+ width = 0;
21564
+ }
21565
+ sqlite3AppendChar(pAccum, precision-1, c);
2136421566
}
21567
+ length = 1;
21568
+ buf[0] = c;
2136521569
bufpt = buf;
2136621570
break;
2136721571
case etSTRING:
2136821572
case etDYNSTRING:
2136921573
if( bArgList ){
@@ -21460,15 +21664,18 @@
2146021664
** The text of the conversion is pointed to by "bufpt" and is
2146121665
** "length" characters long. The field width is "width". Do
2146221666
** the output.
2146321667
*/
2146421668
width -= length;
21465
- if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
21669
+ if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
2146621670
sqlite3StrAccumAppend(pAccum, bufpt, length);
21467
- if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
21671
+ if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
2146821672
21469
- if( zExtra ) sqlite3_free(zExtra);
21673
+ if( zExtra ){
21674
+ sqlite3_free(zExtra);
21675
+ zExtra = 0;
21676
+ }
2147021677
}/* End for loop over the format string */
2147121678
} /* End of function */
2147221679
2147321680
/*
2147421681
** Enlarge the memory allocation on a StrAccum object so that it is
@@ -21491,10 +21698,15 @@
2149121698
return N;
2149221699
}else{
2149321700
char *zOld = (p->zText==p->zBase ? 0 : p->zText);
2149421701
i64 szNew = p->nChar;
2149521702
szNew += N + 1;
21703
+ if( szNew+p->nChar<=p->mxAlloc ){
21704
+ /* Force exponential buffer size growth as long as it does not overflow,
21705
+ ** to avoid having to call this routine too often */
21706
+ szNew += p->nChar;
21707
+ }
2149621708
if( szNew > p->mxAlloc ){
2149721709
sqlite3StrAccumReset(p);
2149821710
setStrAccumError(p, STRACCUM_TOOBIG);
2149921711
return 0;
2150021712
}else{
@@ -21507,10 +21719,11 @@
2150721719
}
2150821720
if( zNew ){
2150921721
assert( p->zText!=0 || p->nChar==0 );
2151021722
if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
2151121723
p->zText = zNew;
21724
+ p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
2151221725
}else{
2151321726
sqlite3StrAccumReset(p);
2151421727
setStrAccumError(p, STRACCUM_NOMEM);
2151521728
return 0;
2151621729
}
@@ -21517,15 +21730,15 @@
2151721730
}
2151821731
return N;
2151921732
}
2152021733
2152121734
/*
21522
-** Append N space characters to the given string buffer.
21735
+** Append N copies of character c to the given string buffer.
2152321736
*/
21524
-SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){
21737
+SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
2152521738
if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
21526
- while( (N--)>0 ) p->zText[p->nChar++] = ' ';
21739
+ while( (N--)>0 ) p->zText[p->nChar++] = c;
2152721740
}
2152821741
2152921742
/*
2153021743
** The StrAccum "p" is not large enough to accept N new bytes of z[].
2153121744
** So enlarge if first, then do the append.
@@ -21676,10 +21889,17 @@
2167621889
*/
2167721890
SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
2167821891
char *z;
2167921892
char zBase[SQLITE_PRINT_BUF_SIZE];
2168021893
StrAccum acc;
21894
+
21895
+#ifdef SQLITE_ENABLE_API_ARMOR
21896
+ if( zFormat==0 ){
21897
+ (void)SQLITE_MISUSE_BKPT;
21898
+ return 0;
21899
+ }
21900
+#endif
2168121901
#ifndef SQLITE_OMIT_AUTOINIT
2168221902
if( sqlite3_initialize() ) return 0;
2168321903
#endif
2168421904
sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
2168521905
acc.useMalloc = 2;
@@ -21718,10 +21938,17 @@
2171821938
** sqlite3_vsnprintf() is the varargs version.
2171921939
*/
2172021940
SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
2172121941
StrAccum acc;
2172221942
if( n<=0 ) return zBuf;
21943
+#ifdef SQLITE_ENABLE_API_ARMOR
21944
+ if( zBuf==0 || zFormat==0 ) {
21945
+ (void)SQLITE_MISUSE_BKPT;
21946
+ if( zBuf && n>0 ) zBuf[0] = 0;
21947
+ return zBuf;
21948
+ }
21949
+#endif
2172321950
sqlite3StrAccumInit(&acc, zBuf, n, 0);
2172421951
acc.useMalloc = 0;
2172521952
sqlite3VXPrintf(&acc, 0, zFormat, ap);
2172621953
return sqlite3StrAccumFinish(&acc);
2172721954
}
@@ -21909,15 +22136,23 @@
2190922136
#else
2191022137
# define wsdPrng sqlite3Prng
2191122138
#endif
2191222139
2191322140
#if SQLITE_THREADSAFE
21914
- sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
22141
+ sqlite3_mutex *mutex;
22142
+#endif
22143
+
22144
+#ifndef SQLITE_OMIT_AUTOINIT
22145
+ if( sqlite3_initialize() ) return;
22146
+#endif
22147
+
22148
+#if SQLITE_THREADSAFE
22149
+ mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
22150
+#endif
22151
+
2191522152
sqlite3_mutex_enter(mutex);
21916
-#endif
21917
-
21918
- if( N<=0 ){
22153
+ if( N<=0 || pBuf==0 ){
2191922154
wsdPrng.isInit = 0;
2192022155
sqlite3_mutex_leave(mutex);
2192122156
return;
2192222157
}
2192322158
@@ -23035,17 +23270,27 @@
2303523270
** case-independent fashion, using the same definition of "case
2303623271
** independence" that SQLite uses internally when comparing identifiers.
2303723272
*/
2303823273
SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
2303923274
register unsigned char *a, *b;
23275
+ if( zLeft==0 ){
23276
+ return zRight ? -1 : 0;
23277
+ }else if( zRight==0 ){
23278
+ return 1;
23279
+ }
2304023280
a = (unsigned char *)zLeft;
2304123281
b = (unsigned char *)zRight;
2304223282
while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
2304323283
return UpperToLower[*a] - UpperToLower[*b];
2304423284
}
2304523285
SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
2304623286
register unsigned char *a, *b;
23287
+ if( zLeft==0 ){
23288
+ return zRight ? -1 : 0;
23289
+ }else if( zRight==0 ){
23290
+ return 1;
23291
+ }
2304723292
a = (unsigned char *)zLeft;
2304823293
b = (unsigned char *)zRight;
2304923294
while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
2305023295
return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
2305123296
}
@@ -32574,10 +32819,15 @@
3257432819
#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
3257532820
# error "WAL mode requires support from the Windows NT kernel, compile\
3257632821
with SQLITE_OMIT_WAL."
3257732822
#endif
3257832823
32824
+#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
32825
+# error "Memory mapped files require support from the Windows NT kernel,\
32826
+ compile with SQLITE_MAX_MMAP_SIZE=0."
32827
+#endif
32828
+
3257932829
/*
3258032830
** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
3258132831
** based on the sub-platform)?
3258232832
*/
3258332833
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
@@ -32703,14 +32953,15 @@
3270332953
# define winGetDirSep() '\\'
3270432954
#endif
3270532955
3270632956
/*
3270732957
** Do we need to manually define the Win32 file mapping APIs for use with WAL
32708
-** mode (e.g. these APIs are available in the Windows CE SDK; however, they
32709
-** are not present in the header file)?
32958
+** mode or memory mapped files (e.g. these APIs are available in the Windows
32959
+** CE SDK; however, they are not present in the header file)?
3271032960
*/
32711
-#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
32961
+#if SQLITE_WIN32_FILEMAPPING_API && \
32962
+ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
3271232963
/*
3271332964
** Two of the file mapping APIs are different under WinRT. Figure out which
3271432965
** set we need.
3271532966
*/
3271632967
#if SQLITE_OS_WINRT
@@ -32734,11 +32985,11 @@
3273432985
3273532986
/*
3273632987
** This file mapping API is common to both Win32 and WinRT.
3273732988
*/
3273832989
WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
32739
-#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
32990
+#endif /* SQLITE_WIN32_FILEMAPPING_API */
3274032991
3274132992
/*
3274232993
** Some Microsoft compilers lack this definition.
3274332994
*/
3274432995
#ifndef INVALID_FILE_ATTRIBUTES
@@ -33027,21 +33278,21 @@
3302733278
3302833279
#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
3302933280
LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
3303033281
3303133282
#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
33032
- !defined(SQLITE_OMIT_WAL))
33283
+ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
3303333284
{ "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
3303433285
#else
3303533286
{ "CreateFileMappingA", (SYSCALL)0, 0 },
3303633287
#endif
3303733288
3303833289
#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
3303933290
DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
3304033291
3304133292
#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
33042
- !defined(SQLITE_OMIT_WAL))
33293
+ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
3304333294
{ "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
3304433295
#else
3304533296
{ "CreateFileMappingW", (SYSCALL)0, 0 },
3304633297
#endif
3304733298
@@ -33377,11 +33628,12 @@
3337733628
#ifndef osLockFileEx
3337833629
#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
3337933630
LPOVERLAPPED))aSyscall[48].pCurrent)
3338033631
#endif
3338133632
33382
-#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
33633
+#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
33634
+ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
3338333635
{ "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
3338433636
#else
3338533637
{ "MapViewOfFile", (SYSCALL)0, 0 },
3338633638
#endif
3338733639
@@ -33447,11 +33699,11 @@
3344733699
#endif
3344833700
3344933701
#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
3345033702
LPOVERLAPPED))aSyscall[58].pCurrent)
3345133703
33452
-#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
33704
+#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3345333705
{ "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
3345433706
#else
3345533707
{ "UnmapViewOfFile", (SYSCALL)0, 0 },
3345633708
#endif
3345733709
@@ -33510,11 +33762,11 @@
3351033762
#endif
3351133763
3351233764
#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
3351333765
FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
3351433766
33515
-#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
33767
+#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
3351633768
{ "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
3351733769
#else
3351833770
{ "MapViewOfFileFromApp", (SYSCALL)0, 0 },
3351933771
#endif
3352033772
@@ -33574,11 +33826,11 @@
3357433826
3357533827
{ "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
3357633828
3357733829
#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
3357833830
33579
-#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
33831
+#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
3358033832
{ "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
3358133833
#else
3358233834
{ "CreateFileMappingFromApp", (SYSCALL)0, 0 },
3358333835
#endif
3358433836
@@ -39150,10 +39402,17 @@
3915039402
*/
3915139403
SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
3915239404
assert( pCache->pCache!=0 );
3915339405
sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
3915439406
}
39407
+
39408
+/*
39409
+** Return the size of the header added by this middleware layer
39410
+** in the page-cache hierarchy.
39411
+*/
39412
+SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); }
39413
+
3915539414
3915639415
#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
3915739416
/*
3915839417
** For all dirty pages currently in the cache, invoke the specified
3915939418
** callback. This is only used if the SQLITE_CHECK_PAGES macro is
@@ -40149,10 +40408,15 @@
4014940408
pcache1Shrink /* xShrink */
4015040409
};
4015140410
sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
4015240411
}
4015340412
40413
+/*
40414
+** Return the size of the header on each page of this PCACHE implementation.
40415
+*/
40416
+SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); }
40417
+
4015440418
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
4015540419
/*
4015640420
** This function is called to free superfluous dynamically allocated memory
4015740421
** held by the pager system. Memory in use by any SQLite pager allocated
4015840422
** by the current thread may be sqlite3_free()ed.
@@ -47705,10 +47969,22 @@
4770547969
}
4770647970
4770747971
return SQLITE_OK;
4770847972
}
4770947973
#endif
47974
+
47975
+/*
47976
+** The page handle passed as the first argument refers to a dirty page
47977
+** with a page number other than iNew. This function changes the page's
47978
+** page number to iNew and sets the value of the PgHdr.flags field to
47979
+** the value passed as the third parameter.
47980
+*/
47981
+SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
47982
+ assert( pPg->pgno!=iNew );
47983
+ pPg->flags = flags;
47984
+ sqlite3PcacheMove(pPg, iNew);
47985
+}
4771047986
4771147987
/*
4771247988
** Return a pointer to the data for the specified page.
4771347989
*/
4771447990
SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
@@ -48103,10 +48379,11 @@
4810348379
SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
4810448380
assert( pPager->eState>=PAGER_READER );
4810548381
return sqlite3WalFramesize(pPager->pWal);
4810648382
}
4810748383
#endif
48384
+
4810848385
4810948386
#endif /* SQLITE_OMIT_DISKIO */
4811048387
4811148388
/************** End of pager.c ***********************************************/
4811248389
/************** Begin file wal.c *********************************************/
@@ -49613,11 +49890,11 @@
4961349890
4961449891
/*
4961549892
** Free an iterator allocated by walIteratorInit().
4961649893
*/
4961749894
static void walIteratorFree(WalIterator *p){
49618
- sqlite3ScratchFree(p);
49895
+ sqlite3_free(p);
4961949896
}
4962049897
4962149898
/*
4962249899
** Construct a WalInterator object that can be used to loop over all
4962349900
** pages in the WAL in ascending order. The caller must hold the checkpoint
@@ -49648,21 +49925,21 @@
4964849925
/* Allocate space for the WalIterator object. */
4964949926
nSegment = walFramePage(iLast) + 1;
4965049927
nByte = sizeof(WalIterator)
4965149928
+ (nSegment-1)*sizeof(struct WalSegment)
4965249929
+ iLast*sizeof(ht_slot);
49653
- p = (WalIterator *)sqlite3ScratchMalloc(nByte);
49930
+ p = (WalIterator *)sqlite3_malloc(nByte);
4965449931
if( !p ){
4965549932
return SQLITE_NOMEM;
4965649933
}
4965749934
memset(p, 0, nByte);
4965849935
p->nSegment = nSegment;
4965949936
4966049937
/* Allocate temporary space used by the merge-sort routine. This block
4966149938
** of memory will be freed before this function returns.
4966249939
*/
49663
- aTmp = (ht_slot *)sqlite3ScratchMalloc(
49940
+ aTmp = (ht_slot *)sqlite3_malloc(
4966449941
sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
4966549942
);
4966649943
if( !aTmp ){
4966749944
rc = SQLITE_NOMEM;
4966849945
}
@@ -49695,11 +49972,11 @@
4969549972
p->aSegment[i].nEntry = nEntry;
4969649973
p->aSegment[i].aIndex = aIndex;
4969749974
p->aSegment[i].aPgno = (u32 *)aPgno;
4969849975
}
4969949976
}
49700
- sqlite3ScratchFree(aTmp);
49977
+ sqlite3_free(aTmp);
4970149978
4970249979
if( rc!=SQLITE_OK ){
4970349980
walIteratorFree(p);
4970449981
}
4970549982
*pp = p;
@@ -50615,11 +50892,11 @@
5061550892
** was in before the client began writing to the database.
5061650893
*/
5061750894
memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
5061850895
5061950896
for(iFrame=pWal->hdr.mxFrame+1;
50620
- ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
50897
+ rc==SQLITE_OK && iFrame<=iMax;
5062150898
iFrame++
5062250899
){
5062350900
/* This call cannot fail. Unless the page for which the page number
5062450901
** is passed as the second argument is (a) in the cache and
5062550902
** (b) has an outstanding reference, then xUndo is either a no-op
@@ -50634,11 +50911,10 @@
5063450911
assert( walFramePgno(pWal, iFrame)!=1 );
5063550912
rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
5063650913
}
5063750914
if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
5063850915
}
50639
- assert( rc==SQLITE_OK );
5064050916
return rc;
5064150917
}
5064250918
5064350919
/*
5064450920
** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
@@ -53333,28 +53609,27 @@
5333353609
int cellOffset; /* Offset to the cell pointer array */
5333453610
int cbrk; /* Offset to the cell content area */
5333553611
int nCell; /* Number of cells on the page */
5333653612
unsigned char *data; /* The page data */
5333753613
unsigned char *temp; /* Temp area for cell content */
53614
+ unsigned char *src; /* Source of content */
5333853615
int iCellFirst; /* First allowable cell index */
5333953616
int iCellLast; /* Last possible cell index */
5334053617
5334153618
5334253619
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5334353620
assert( pPage->pBt!=0 );
5334453621
assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
5334553622
assert( pPage->nOverflow==0 );
5334653623
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53347
- temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
53348
- data = pPage->aData;
53624
+ temp = 0;
53625
+ src = data = pPage->aData;
5334953626
hdr = pPage->hdrOffset;
5335053627
cellOffset = pPage->cellOffset;
5335153628
nCell = pPage->nCell;
5335253629
assert( nCell==get2byte(&data[hdr+3]) );
5335353630
usableSize = pPage->pBt->usableSize;
53354
- cbrk = get2byte(&data[hdr+5]);
53355
- memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
5335653631
cbrk = usableSize;
5335753632
iCellFirst = cellOffset + 2*nCell;
5335853633
iCellLast = usableSize - 4;
5335953634
for(i=0; i<nCell; i++){
5336053635
u8 *pAddr; /* The i-th cell pointer */
@@ -53369,11 +53644,11 @@
5336953644
if( pc<iCellFirst || pc>iCellLast ){
5337053645
return SQLITE_CORRUPT_BKPT;
5337153646
}
5337253647
#endif
5337353648
assert( pc>=iCellFirst && pc<=iCellLast );
53374
- size = cellSizePtr(pPage, &temp[pc]);
53649
+ size = cellSizePtr(pPage, &src[pc]);
5337553650
cbrk -= size;
5337653651
#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
5337753652
if( cbrk<iCellFirst ){
5337853653
return SQLITE_CORRUPT_BKPT;
5337953654
}
@@ -53383,12 +53658,20 @@
5338353658
}
5338453659
#endif
5338553660
assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
5338653661
testcase( cbrk+size==usableSize );
5338753662
testcase( pc+size==usableSize );
53388
- memcpy(&data[cbrk], &temp[pc], size);
5338953663
put2byte(pAddr, cbrk);
53664
+ if( temp==0 ){
53665
+ int x;
53666
+ if( cbrk==pc ) continue;
53667
+ temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
53668
+ x = get2byte(&data[hdr+5]);
53669
+ memcpy(&temp[x], &data[x], (cbrk+size) - x);
53670
+ src = temp;
53671
+ }
53672
+ memcpy(&data[cbrk], &src[pc], size);
5339053673
}
5339153674
assert( cbrk>=iCellFirst );
5339253675
put2byte(&data[hdr+5], cbrk);
5339353676
data[hdr+1] = 0;
5339453677
data[hdr+2] = 0;
@@ -53398,10 +53681,66 @@
5339853681
if( cbrk-iCellFirst!=pPage->nFree ){
5339953682
return SQLITE_CORRUPT_BKPT;
5340053683
}
5340153684
return SQLITE_OK;
5340253685
}
53686
+
53687
+/*
53688
+** Search the free-list on page pPg for space to store a cell nByte bytes in
53689
+** size. If one can be found, return a pointer to the space and remove it
53690
+** from the free-list.
53691
+**
53692
+** If no suitable space can be found on the free-list, return NULL.
53693
+**
53694
+** This function may detect corruption within pPg. If corruption is
53695
+** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
53696
+**
53697
+** If a slot of at least nByte bytes is found but cannot be used because
53698
+** there are already at least 60 fragmented bytes on the page, return NULL.
53699
+** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
53700
+*/
53701
+static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
53702
+ const int hdr = pPg->hdrOffset;
53703
+ u8 * const aData = pPg->aData;
53704
+ int iAddr;
53705
+ int pc;
53706
+ int usableSize = pPg->pBt->usableSize;
53707
+
53708
+ for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
53709
+ int size; /* Size of the free slot */
53710
+ if( pc>usableSize-4 || pc<iAddr+4 ){
53711
+ *pRc = SQLITE_CORRUPT_BKPT;
53712
+ return 0;
53713
+ }
53714
+ size = get2byte(&aData[pc+2]);
53715
+ if( size>=nByte ){
53716
+ int x = size - nByte;
53717
+ testcase( x==4 );
53718
+ testcase( x==3 );
53719
+ if( x<4 ){
53720
+ if( aData[hdr+7]>=60 ){
53721
+ if( pbDefrag ) *pbDefrag = 1;
53722
+ return 0;
53723
+ }
53724
+ /* Remove the slot from the free-list. Update the number of
53725
+ ** fragmented bytes within the page. */
53726
+ memcpy(&aData[iAddr], &aData[pc], 2);
53727
+ aData[hdr+7] += (u8)x;
53728
+ }else if( size+pc > usableSize ){
53729
+ *pRc = SQLITE_CORRUPT_BKPT;
53730
+ return 0;
53731
+ }else{
53732
+ /* The slot remains on the free-list. Reduce its size to account
53733
+ ** for the portion used by the new allocation. */
53734
+ put2byte(&aData[pc+2], x);
53735
+ }
53736
+ return &aData[pc + x];
53737
+ }
53738
+ }
53739
+
53740
+ return 0;
53741
+}
5340353742
5340453743
/*
5340553744
** Allocate nByte bytes of space from within the B-Tree page passed
5340653745
** as the first argument. Write into *pIdx the index into pPage->aData[]
5340753746
** of the first byte of allocated space. Return either SQLITE_OK or
@@ -53416,22 +53755,20 @@
5341653755
*/
5341753756
static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
5341853757
const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
5341953758
u8 * const data = pPage->aData; /* Local cache of pPage->aData */
5342053759
int top; /* First byte of cell content area */
53760
+ int rc = SQLITE_OK; /* Integer return code */
5342153761
int gap; /* First byte of gap between cell pointers and cell content */
53422
- int rc; /* Integer return code */
53423
- int usableSize; /* Usable size of the page */
5342453762
5342553763
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5342653764
assert( pPage->pBt );
5342753765
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5342853766
assert( nByte>=0 ); /* Minimum cell size is 4 */
5342953767
assert( pPage->nFree>=nByte );
5343053768
assert( pPage->nOverflow==0 );
53431
- usableSize = pPage->pBt->usableSize;
53432
- assert( nByte < usableSize-8 );
53769
+ assert( nByte < (int)(pPage->pBt->usableSize-8) );
5343353770
5343453771
assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
5343553772
gap = pPage->cellOffset + 2*pPage->nCell;
5343653773
assert( gap<=65536 );
5343753774
top = get2byte(&data[hdr+5]);
@@ -53449,46 +53786,27 @@
5344953786
*/
5345053787
testcase( gap+2==top );
5345153788
testcase( gap+1==top );
5345253789
testcase( gap==top );
5345353790
if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
53454
- int pc, addr;
53455
- for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
53456
- int size; /* Size of the free slot */
53457
- if( pc>usableSize-4 || pc<addr+4 ){
53458
- return SQLITE_CORRUPT_BKPT;
53459
- }
53460
- size = get2byte(&data[pc+2]);
53461
- if( size>=nByte ){
53462
- int x = size - nByte;
53463
- testcase( x==4 );
53464
- testcase( x==3 );
53465
- if( x<4 ){
53466
- if( data[hdr+7]>=60 ) goto defragment_page;
53467
- /* Remove the slot from the free-list. Update the number of
53468
- ** fragmented bytes within the page. */
53469
- memcpy(&data[addr], &data[pc], 2);
53470
- data[hdr+7] += (u8)x;
53471
- }else if( size+pc > usableSize ){
53472
- return SQLITE_CORRUPT_BKPT;
53473
- }else{
53474
- /* The slot remains on the free-list. Reduce its size to account
53475
- ** for the portion used by the new allocation. */
53476
- put2byte(&data[pc+2], x);
53477
- }
53478
- *pIdx = pc + x;
53479
- return SQLITE_OK;
53480
- }
53791
+ int bDefrag = 0;
53792
+ u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
53793
+ if( rc ) return rc;
53794
+ if( bDefrag ) goto defragment_page;
53795
+ if( pSpace ){
53796
+ assert( pSpace>=data && (pSpace - data)<65536 );
53797
+ *pIdx = (int)(pSpace - data);
53798
+ return SQLITE_OK;
5348153799
}
5348253800
}
5348353801
5348453802
/* The request could not be fulfilled using a freelist slot. Check
5348553803
** to see if defragmentation is necessary.
5348653804
*/
5348753805
testcase( gap+2+nByte==top );
5348853806
if( gap+2+nByte>top ){
53489
-defragment_page:
53807
+ defragment_page:
5349053808
testcase( pPage->nCell==0 );
5349153809
rc = defragmentPage(pPage);
5349253810
if( rc ) return rc;
5349353811
top = get2byteNotZero(&data[hdr+5]);
5349453812
assert( gap+nByte<=top );
@@ -53532,11 +53850,11 @@
5353253850
unsigned char *data = pPage->aData; /* Page content */
5353353851
5353453852
assert( pPage->pBt!=0 );
5353553853
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5353653854
assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
53537
- assert( iEnd <= pPage->pBt->usableSize );
53855
+ assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
5353853856
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5353953857
assert( iSize>=4 ); /* Minimum cell size is 4 */
5354053858
assert( iStart<=iLast );
5354153859
5354253860
/* Overwrite deleted information with zeros when the secure_delete
@@ -54353,11 +54671,11 @@
5435354671
5435454672
/* Rollback any active transaction and free the handle structure.
5435554673
** The call to sqlite3BtreeRollback() drops any table-locks held by
5435654674
** this handle.
5435754675
*/
54358
- sqlite3BtreeRollback(p, SQLITE_OK);
54676
+ sqlite3BtreeRollback(p, SQLITE_OK, 0);
5435954677
sqlite3BtreeLeave(p);
5436054678
5436154679
/* If there are still other outstanding references to the shared-btree
5436254680
** structure, return now. The remainder of this procedure cleans
5436354681
** up the shared-btree.
@@ -55646,31 +55964,32 @@
5564655964
return rc;
5564755965
}
5564855966
5564955967
/*
5565055968
** This routine sets the state to CURSOR_FAULT and the error
55651
-** code to errCode for every cursor on BtShared that pBtree
55652
-** references.
55653
-**
55654
-** Every cursor is tripped, including cursors that belong
55655
-** to other database connections that happen to be sharing
55656
-** the cache with pBtree.
55657
-**
55658
-** This routine gets called when a rollback occurs.
55659
-** All cursors using the same cache must be tripped
55660
-** to prevent them from trying to use the btree after
55661
-** the rollback. The rollback may have deleted tables
55662
-** or moved root pages, so it is not sufficient to
55663
-** save the state of the cursor. The cursor must be
55664
-** invalidated.
55665
-*/
55666
-SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
55969
+** code to errCode for every cursor on any BtShared that pBtree
55970
+** references. Or if the writeOnly flag is set to 1, then only
55971
+** trip write cursors and leave read cursors unchanged.
55972
+**
55973
+** Every cursor is a candidate to be tripped, including cursors
55974
+** that belong to other database connections that happen to be
55975
+** sharing the cache with pBtree.
55976
+**
55977
+** This routine gets called when a rollback occurs. The writeOnly
55978
+** flag is set to 1 if the transaction did not make any schema
55979
+** changes, in which case the read cursors can continue operating.
55980
+** If schema changes did occur in the transaction, then both read
55981
+** and write cursors must both be tripped.
55982
+*/
55983
+SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
5566755984
BtCursor *p;
55985
+ assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
5566855986
if( pBtree==0 ) return;
5566955987
sqlite3BtreeEnter(pBtree);
5567055988
for(p=pBtree->pBt->pCursor; p; p=p->pNext){
5567155989
int i;
55990
+ if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ) continue;
5567255991
sqlite3BtreeClearCursor(p);
5567355992
p->eState = CURSOR_FAULT;
5567455993
p->skipNext = errCode;
5567555994
for(i=0; i<=p->iPage; i++){
5567655995
releasePage(p->apPage[i]);
@@ -55679,31 +55998,36 @@
5567955998
}
5568055999
sqlite3BtreeLeave(pBtree);
5568156000
}
5568256001
5568356002
/*
55684
-** Rollback the transaction in progress. All cursors will be
55685
-** invalided by this operation. Any attempt to use a cursor
55686
-** that was open at the beginning of this operation will result
55687
-** in an error.
56003
+** Rollback the transaction in progress.
56004
+**
56005
+** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
56006
+** Only write cursors are tripped if writeOnly is true but all cursors are
56007
+** tripped if writeOnly is false. Any attempt to use
56008
+** a tripped cursor will result in an error.
5568856009
**
5568956010
** This will release the write lock on the database file. If there
5569056011
** are no active cursors, it also releases the read lock.
5569156012
*/
55692
-SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
56013
+SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
5569356014
int rc;
5569456015
BtShared *pBt = p->pBt;
5569556016
MemPage *pPage1;
5569656017
56018
+ assert( writeOnly==1 || writeOnly==0 );
56019
+ assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
5569756020
sqlite3BtreeEnter(p);
5569856021
if( tripCode==SQLITE_OK ){
5569956022
rc = tripCode = saveAllCursors(pBt, 0, 0);
56023
+ if( rc ) writeOnly = 0;
5570056024
}else{
5570156025
rc = SQLITE_OK;
5570256026
}
5570356027
if( tripCode ){
55704
- sqlite3BtreeTripAllCursors(p, tripCode);
56028
+ sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
5570556029
}
5570656030
btreeIntegrity(p);
5570756031
5570856032
if( p->inTrans==TRANS_WRITE ){
5570956033
int rc2;
@@ -58120,49 +58444,266 @@
5812058444
#endif
5812158445
}
5812258446
}
5812358447
5812458448
/*
58125
-** Add a list of cells to a page. The page should be initially empty.
58126
-** The cells are guaranteed to fit on the page.
58127
-*/
58128
-static void assemblePage(
58129
- MemPage *pPage, /* The page to be assembled */
58130
- int nCell, /* The number of cells to add to this page */
58131
- u8 **apCell, /* Pointers to cell bodies */
58132
- u16 *aSize /* Sizes of the cells */
58133
-){
58134
- int i; /* Loop counter */
58135
- u8 *pCellptr; /* Address of next cell pointer */
58136
- int cellbody; /* Address of next cell body */
58137
- u8 * const data = pPage->aData; /* Pointer to data for pPage */
58138
- const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
58139
- const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
58140
-
58141
- assert( pPage->nOverflow==0 );
58142
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
58143
- assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
58144
- && (int)MX_CELL(pPage->pBt)<=10921);
58145
- assert( sqlite3PagerIswriteable(pPage->pDbPage) );
58146
-
58147
- /* Check that the page has just been zeroed by zeroPage() */
58148
- assert( pPage->nCell==0 );
58149
- assert( get2byteNotZero(&data[hdr+5])==nUsable );
58150
-
58151
- pCellptr = &pPage->aCellIdx[nCell*2];
58152
- cellbody = nUsable;
58153
- for(i=nCell-1; i>=0; i--){
58154
- u16 sz = aSize[i];
58155
- pCellptr -= 2;
58156
- cellbody -= sz;
58157
- put2byte(pCellptr, cellbody);
58158
- memcpy(&data[cellbody], apCell[i], sz);
58159
- }
58160
- put2byte(&data[hdr+3], nCell);
58161
- put2byte(&data[hdr+5], cellbody);
58162
- pPage->nFree -= (nCell*2 + nUsable - cellbody);
58163
- pPage->nCell = (u16)nCell;
58449
+** Array apCell[] contains pointers to nCell b-tree page cells. The
58450
+** szCell[] array contains the size in bytes of each cell. This function
58451
+** replaces the current contents of page pPg with the contents of the cell
58452
+** array.
58453
+**
58454
+** Some of the cells in apCell[] may currently be stored in pPg. This
58455
+** function works around problems caused by this by making a copy of any
58456
+** such cells before overwriting the page data.
58457
+**
58458
+** The MemPage.nFree field is invalidated by this function. It is the
58459
+** responsibility of the caller to set it correctly.
58460
+*/
58461
+static void rebuildPage(
58462
+ MemPage *pPg, /* Edit this page */
58463
+ int nCell, /* Final number of cells on page */
58464
+ u8 **apCell, /* Array of cells */
58465
+ u16 *szCell /* Array of cell sizes */
58466
+){
58467
+ const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
58468
+ u8 * const aData = pPg->aData; /* Pointer to data for pPg */
58469
+ const int usableSize = pPg->pBt->usableSize;
58470
+ u8 * const pEnd = &aData[usableSize];
58471
+ int i;
58472
+ u8 *pCellptr = pPg->aCellIdx;
58473
+ u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
58474
+ u8 *pData;
58475
+
58476
+ i = get2byte(&aData[hdr+5]);
58477
+ memcpy(&pTmp[i], &aData[i], usableSize - i);
58478
+
58479
+ pData = pEnd;
58480
+ for(i=0; i<nCell; i++){
58481
+ u8 *pCell = apCell[i];
58482
+ if( pCell>aData && pCell<pEnd ){
58483
+ pCell = &pTmp[pCell - aData];
58484
+ }
58485
+ pData -= szCell[i];
58486
+ memcpy(pData, pCell, szCell[i]);
58487
+ put2byte(pCellptr, (pData - aData));
58488
+ pCellptr += 2;
58489
+ assert( szCell[i]==cellSizePtr(pPg, pCell) );
58490
+ }
58491
+
58492
+ /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
58493
+ pPg->nCell = nCell;
58494
+ pPg->nOverflow = 0;
58495
+
58496
+ put2byte(&aData[hdr+1], 0);
58497
+ put2byte(&aData[hdr+3], pPg->nCell);
58498
+ put2byte(&aData[hdr+5], pData - aData);
58499
+ aData[hdr+7] = 0x00;
58500
+}
58501
+
58502
+/*
58503
+** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
58504
+** contains the size in bytes of each such cell. This function attempts to
58505
+** add the cells stored in the array to page pPg. If it cannot (because
58506
+** the page needs to be defragmented before the cells will fit), non-zero
58507
+** is returned. Otherwise, if the cells are added successfully, zero is
58508
+** returned.
58509
+**
58510
+** Argument pCellptr points to the first entry in the cell-pointer array
58511
+** (part of page pPg) to populate. After cell apCell[0] is written to the
58512
+** page body, a 16-bit offset is written to pCellptr. And so on, for each
58513
+** cell in the array. It is the responsibility of the caller to ensure
58514
+** that it is safe to overwrite this part of the cell-pointer array.
58515
+**
58516
+** When this function is called, *ppData points to the start of the
58517
+** content area on page pPg. If the size of the content area is extended,
58518
+** *ppData is updated to point to the new start of the content area
58519
+** before returning.
58520
+**
58521
+** Finally, argument pBegin points to the byte immediately following the
58522
+** end of the space required by this page for the cell-pointer area (for
58523
+** all cells - not just those inserted by the current call). If the content
58524
+** area must be extended to before this point in order to accomodate all
58525
+** cells in apCell[], then the cells do not fit and non-zero is returned.
58526
+*/
58527
+static int pageInsertArray(
58528
+ MemPage *pPg, /* Page to add cells to */
58529
+ u8 *pBegin, /* End of cell-pointer array */
58530
+ u8 **ppData, /* IN/OUT: Page content -area pointer */
58531
+ u8 *pCellptr, /* Pointer to cell-pointer area */
58532
+ int nCell, /* Number of cells to add to pPg */
58533
+ u8 **apCell, /* Array of cells */
58534
+ u16 *szCell /* Array of cell sizes */
58535
+){
58536
+ int i;
58537
+ u8 *aData = pPg->aData;
58538
+ u8 *pData = *ppData;
58539
+ const int bFreelist = aData[1] || aData[2];
58540
+ assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
58541
+ for(i=0; i<nCell; i++){
58542
+ int sz = szCell[i];
58543
+ int rc;
58544
+ u8 *pSlot;
58545
+ if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
58546
+ pData -= sz;
58547
+ if( pData<pBegin ) return 1;
58548
+ pSlot = pData;
58549
+ }
58550
+ memcpy(pSlot, apCell[i], sz);
58551
+ put2byte(pCellptr, (pSlot - aData));
58552
+ pCellptr += 2;
58553
+ }
58554
+ *ppData = pData;
58555
+ return 0;
58556
+}
58557
+
58558
+/*
58559
+** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
58560
+** contains the size in bytes of each such cell. This function adds the
58561
+** space associated with each cell in the array that is currently stored
58562
+** within the body of pPg to the pPg free-list. The cell-pointers and other
58563
+** fields of the page are not updated.
58564
+**
58565
+** This function returns the total number of cells added to the free-list.
58566
+*/
58567
+static int pageFreeArray(
58568
+ MemPage *pPg, /* Page to edit */
58569
+ int nCell, /* Cells to delete */
58570
+ u8 **apCell, /* Array of cells */
58571
+ u16 *szCell /* Array of cell sizes */
58572
+){
58573
+ u8 * const aData = pPg->aData;
58574
+ u8 * const pEnd = &aData[pPg->pBt->usableSize];
58575
+ u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
58576
+ int nRet = 0;
58577
+ int i;
58578
+ u8 *pFree = 0;
58579
+ int szFree = 0;
58580
+
58581
+ for(i=0; i<nCell; i++){
58582
+ u8 *pCell = apCell[i];
58583
+ if( pCell>=pStart && pCell<pEnd ){
58584
+ int sz = szCell[i];
58585
+ if( pFree!=(pCell + sz) ){
58586
+ if( pFree ){
58587
+ assert( pFree>aData && (pFree - aData)<65536 );
58588
+ freeSpace(pPg, (u16)(pFree - aData), szFree);
58589
+ }
58590
+ pFree = pCell;
58591
+ szFree = sz;
58592
+ if( pFree+sz>pEnd ) return 0;
58593
+ }else{
58594
+ pFree = pCell;
58595
+ szFree += sz;
58596
+ }
58597
+ nRet++;
58598
+ }
58599
+ }
58600
+ if( pFree ){
58601
+ assert( pFree>aData && (pFree - aData)<65536 );
58602
+ freeSpace(pPg, (u16)(pFree - aData), szFree);
58603
+ }
58604
+ return nRet;
58605
+}
58606
+
58607
+/*
58608
+** The pPg->nFree field is invalid when this function returns. It is the
58609
+** responsibility of the caller to set it correctly.
58610
+*/
58611
+static void editPage(
58612
+ MemPage *pPg, /* Edit this page */
58613
+ int iOld, /* Index of first cell currently on page */
58614
+ int iNew, /* Index of new first cell on page */
58615
+ int nNew, /* Final number of cells on page */
58616
+ u8 **apCell, /* Array of cells */
58617
+ u16 *szCell /* Array of cell sizes */
58618
+){
58619
+ u8 * const aData = pPg->aData;
58620
+ const int hdr = pPg->hdrOffset;
58621
+ u8 *pBegin = &pPg->aCellIdx[nNew * 2];
58622
+ int nCell = pPg->nCell; /* Cells stored on pPg */
58623
+ u8 *pData;
58624
+ u8 *pCellptr;
58625
+ int i;
58626
+ int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
58627
+ int iNewEnd = iNew + nNew;
58628
+
58629
+#ifdef SQLITE_DEBUG
58630
+ u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
58631
+ memcpy(pTmp, aData, pPg->pBt->usableSize);
58632
+#endif
58633
+
58634
+ /* Remove cells from the start and end of the page */
58635
+ if( iOld<iNew ){
58636
+ int nShift = pageFreeArray(
58637
+ pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
58638
+ );
58639
+ memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
58640
+ nCell -= nShift;
58641
+ }
58642
+ if( iNewEnd < iOldEnd ){
58643
+ nCell -= pageFreeArray(
58644
+ pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
58645
+ );
58646
+ }
58647
+
58648
+ pData = &aData[get2byte(&aData[hdr+5])];
58649
+ if( pData<pBegin ) goto editpage_fail;
58650
+
58651
+ /* Add cells to the start of the page */
58652
+ if( iNew<iOld ){
58653
+ int nAdd = iOld-iNew;
58654
+ pCellptr = pPg->aCellIdx;
58655
+ memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
58656
+ if( pageInsertArray(
58657
+ pPg, pBegin, &pData, pCellptr,
58658
+ nAdd, &apCell[iNew], &szCell[iNew]
58659
+ ) ) goto editpage_fail;
58660
+ nCell += nAdd;
58661
+ }
58662
+
58663
+ /* Add any overflow cells */
58664
+ for(i=0; i<pPg->nOverflow; i++){
58665
+ int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
58666
+ if( iCell>=0 && iCell<nNew ){
58667
+ pCellptr = &pPg->aCellIdx[iCell * 2];
58668
+ memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
58669
+ nCell++;
58670
+ if( pageInsertArray(
58671
+ pPg, pBegin, &pData, pCellptr,
58672
+ 1, &apCell[iCell + iNew], &szCell[iCell + iNew]
58673
+ ) ) goto editpage_fail;
58674
+ }
58675
+ }
58676
+
58677
+ /* Append cells to the end of the page */
58678
+ pCellptr = &pPg->aCellIdx[nCell*2];
58679
+ if( pageInsertArray(
58680
+ pPg, pBegin, &pData, pCellptr,
58681
+ nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
58682
+ ) ) goto editpage_fail;
58683
+
58684
+ pPg->nCell = nNew;
58685
+ pPg->nOverflow = 0;
58686
+
58687
+ put2byte(&aData[hdr+3], pPg->nCell);
58688
+ put2byte(&aData[hdr+5], pData - aData);
58689
+
58690
+#ifdef SQLITE_DEBUG
58691
+ for(i=0; i<nNew && !CORRUPT_DB; i++){
58692
+ u8 *pCell = apCell[i+iNew];
58693
+ int iOff = get2byte(&pPg->aCellIdx[i*2]);
58694
+ if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
58695
+ pCell = &pTmp[pCell - aData];
58696
+ }
58697
+ assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
58698
+ }
58699
+#endif
58700
+
58701
+ return;
58702
+ editpage_fail:
58703
+ /* Unable to edit this page. Rebuild it from scratch instead. */
58704
+ rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
5816458705
}
5816558706
5816658707
/*
5816758708
** The following parameters determine how many adjacent pages get involved
5816858709
** in a balancing operation. NN is the number of neighbors on either side
@@ -58230,11 +58771,12 @@
5823058771
u8 *pStop;
5823158772
5823258773
assert( sqlite3PagerIswriteable(pNew->pDbPage) );
5823358774
assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
5823458775
zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
58235
- assemblePage(pNew, 1, &pCell, &szCell);
58776
+ rebuildPage(pNew, 1, &pCell, &szCell);
58777
+ pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
5823658778
5823758779
/* If this is an auto-vacuum database, update the pointer map
5823858780
** with entries for the new page, and any pointer from the
5823958781
** cell on the page to an overflow page. If either of these
5824058782
** operations fails, the return code is set, but the contents
@@ -58449,21 +58991,26 @@
5844958991
int subtotal; /* Subtotal of bytes in cells on one page */
5845058992
int iSpace1 = 0; /* First unused byte of aSpace1[] */
5845158993
int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
5845258994
int szScratch; /* Size of scratch memory requested */
5845358995
MemPage *apOld[NB]; /* pPage and up to two siblings */
58454
- MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
5845558996
MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
5845658997
u8 *pRight; /* Location in parent of right-sibling pointer */
5845758998
u8 *apDiv[NB-1]; /* Divider cells in pParent */
5845858999
int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
58459
- int szNew[NB+2]; /* Combined size of cells place on i-th page */
59000
+ int cntOld[NB+2]; /* Old index in aCell[] after i-th page */
59001
+ int szNew[NB+2]; /* Combined size of cells placed on i-th page */
5846059002
u8 **apCell = 0; /* All cells begin balanced */
5846159003
u16 *szCell; /* Local size of all cells in apCell[] */
5846259004
u8 *aSpace1; /* Space for copies of dividers cells */
5846359005
Pgno pgno; /* Temp var to store a page number in */
59006
+ u8 abDone[NB+2]; /* True after i'th new page is populated */
59007
+ Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
59008
+ Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
59009
+ u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
5846459010
59011
+ memset(abDone, 0, sizeof(abDone));
5846559012
pBt = pParent->pBt;
5846659013
assert( sqlite3_mutex_held(pBt->mutex) );
5846759014
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
5846859015
5846959016
#if 0
@@ -58568,16 +59115,18 @@
5856859115
nMaxCells = (nMaxCells + 3)&~3;
5856959116
5857059117
/*
5857159118
** Allocate space for memory structures
5857259119
*/
58573
- k = pBt->pageSize + ROUND8(sizeof(MemPage));
5857459120
szScratch =
5857559121
nMaxCells*sizeof(u8*) /* apCell */
5857659122
+ nMaxCells*sizeof(u16) /* szCell */
58577
- + pBt->pageSize /* aSpace1 */
58578
- + k*nOld; /* Page copies (apCopy) */
59123
+ + pBt->pageSize; /* aSpace1 */
59124
+
59125
+ /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
59126
+ ** that is more than 6 times the database page size. */
59127
+ assert( szScratch<=6*pBt->pageSize );
5857959128
apCell = sqlite3ScratchMalloc( szScratch );
5858059129
if( apCell==0 ){
5858159130
rc = SQLITE_NOMEM;
5858259131
goto balance_cleanup;
5858359132
}
@@ -58586,12 +59135,12 @@
5858659135
assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
5858759136
5858859137
/*
5858959138
** Load pointers to all cells on sibling pages and the divider cells
5859059139
** into the local apCell[] array. Make copies of the divider cells
58591
- ** into space obtained from aSpace1[] and remove the divider cells
58592
- ** from pParent.
59140
+ ** into space obtained from aSpace1[]. The divider cells have already
59141
+ ** been removed from pParent.
5859359142
**
5859459143
** If the siblings are on leaf pages, then the child pointers of the
5859559144
** divider cells are stripped from the cells before they are copied
5859659145
** into aSpace1[]. In this way, all cells in apCell[] are without
5859759146
** child pointers. If siblings are not leaves, then all cell in
@@ -58603,19 +59152,11 @@
5860359152
*/
5860459153
leafCorrection = apOld[0]->leaf*4;
5860559154
leafData = apOld[0]->intKeyLeaf;
5860659155
for(i=0; i<nOld; i++){
5860759156
int limit;
58608
-
58609
- /* Before doing anything else, take a copy of the i'th original sibling
58610
- ** The rest of this function will use data from the copies rather
58611
- ** that the original pages since the original pages will be in the
58612
- ** process of being overwritten. */
58613
- MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
58614
- memcpy(pOld, apOld[i], sizeof(MemPage));
58615
- pOld->aData = (void*)&pOld[1];
58616
- memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
59157
+ MemPage *pOld = apOld[i];
5861759158
5861859159
limit = pOld->nCell+pOld->nOverflow;
5861959160
if( pOld->nOverflow>0 ){
5862059161
for(j=0; j<limit; j++){
5862159162
assert( nCell<nMaxCells );
@@ -58632,10 +59173,11 @@
5863259173
apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
5863359174
szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
5863459175
nCell++;
5863559176
}
5863659177
}
59178
+ cntOld[i] = nCell;
5863759179
if( i<nOld-1 && !leafData){
5863859180
u16 sz = (u16)szNew[i];
5863959181
u8 *pTemp;
5864059182
assert( nCell<nMaxCells );
5864159183
szCell[nCell] = sz;
@@ -58683,11 +59225,11 @@
5868359225
usableSpace = pBt->usableSize - 12 + leafCorrection;
5868459226
for(subtotal=k=i=0; i<nCell; i++){
5868559227
assert( i<nMaxCells );
5868659228
subtotal += szCell[i] + 2;
5868759229
if( subtotal > usableSpace ){
58688
- szNew[k] = subtotal - szCell[i];
59230
+ szNew[k] = subtotal - szCell[i] - 2;
5868959231
cntNew[k] = i;
5869059232
if( leafData ){ i--; }
5869159233
subtotal = 0;
5869259234
k++;
5869359235
if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
@@ -58697,13 +59239,14 @@
5869759239
cntNew[k] = nCell;
5869859240
k++;
5869959241
5870059242
/*
5870159243
** The packing computed by the previous block is biased toward the siblings
58702
- ** on the left side. The left siblings are always nearly full, while the
58703
- ** right-most sibling might be nearly empty. This block of code attempts
58704
- ** to adjust the packing of siblings to get a better balance.
59244
+ ** on the left side (siblings with smaller keys). The left siblings are
59245
+ ** always nearly full, while the right-most sibling might be nearly empty.
59246
+ ** The next block of code attempts to adjust the packing of siblings to
59247
+ ** get a better balance.
5870559248
**
5870659249
** This adjustment is more than an optimization. The packing above might
5870759250
** be so out of balance as to be illegal. For example, the right-most
5870859251
** sibling might be completely empty. This adjustment is not optional.
5870959252
*/
@@ -58728,26 +59271,22 @@
5872859271
}
5872959272
szNew[i] = szRight;
5873059273
szNew[i-1] = szLeft;
5873159274
}
5873259275
58733
- /* Either we found one or more cells (cntnew[0])>0) or pPage is
58734
- ** a virtual root page. A virtual root page is when the real root
58735
- ** page is page 1 and we are the only child of that page.
58736
- **
58737
- ** UPDATE: The assert() below is not necessarily true if the database
58738
- ** file is corrupt. The corruption will be detected and reported later
58739
- ** in this procedure so there is no need to act upon it now.
59276
+ /* Sanity check: For a non-corrupt database file one of the follwing
59277
+ ** must be true:
59278
+ ** (1) We found one or more cells (cntNew[0])>0), or
59279
+ ** (2) pPage is a virtual root page. A virtual root page is when
59280
+ ** the real root page is page 1 and we are the only child of
59281
+ ** that page.
5874059282
*/
58741
-#if 0
58742
- assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
58743
-#endif
58744
-
58745
- TRACE(("BALANCE: old: %d %d %d ",
58746
- apOld[0]->pgno,
58747
- nOld>=2 ? apOld[1]->pgno : 0,
58748
- nOld>=3 ? apOld[2]->pgno : 0
59283
+ assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
59284
+ TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
59285
+ apOld[0]->pgno, apOld[0]->nCell,
59286
+ nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
59287
+ nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
5874959288
));
5875059289
5875159290
/*
5875259291
** Allocate k new pages. Reuse old pages where possible.
5875359292
*/
@@ -58766,12 +59305,14 @@
5876659305
if( rc ) goto balance_cleanup;
5876759306
}else{
5876859307
assert( i>0 );
5876959308
rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
5877059309
if( rc ) goto balance_cleanup;
59310
+ zeroPage(pNew, pageFlags);
5877159311
apNew[i] = pNew;
5877259312
nNew++;
59313
+ cntOld[i] = nCell;
5877359314
5877459315
/* Set the pointer-map entry for the new sibling page. */
5877559316
if( ISAUTOVACUUM ){
5877659317
ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
5877759318
if( rc!=SQLITE_OK ){
@@ -58779,139 +59320,247 @@
5877959320
}
5878059321
}
5878159322
}
5878259323
}
5878359324
58784
- /* Free any old pages that were not reused as new pages.
58785
- */
58786
- while( i<nOld ){
58787
- freePage(apOld[i], &rc);
58788
- if( rc ) goto balance_cleanup;
58789
- releasePage(apOld[i]);
58790
- apOld[i] = 0;
58791
- i++;
58792
- }
58793
-
5879459325
/*
58795
- ** Put the new pages in ascending order. This helps to
58796
- ** keep entries in the disk file in order so that a scan
58797
- ** of the table is a linear scan through the file. That
58798
- ** in turn helps the operating system to deliver pages
58799
- ** from the disk more rapidly.
58800
- **
58801
- ** An O(n^2) insertion sort algorithm is used, but since
58802
- ** n is never more than NB (a small constant), that should
58803
- ** not be a problem.
58804
- **
58805
- ** When NB==3, this one optimization makes the database
58806
- ** about 25% faster for large insertions and deletions.
58807
- */
58808
- for(i=0; i<k-1; i++){
58809
- int minV = apNew[i]->pgno;
58810
- int minI = i;
58811
- for(j=i+1; j<k; j++){
58812
- if( apNew[j]->pgno<(unsigned)minV ){
58813
- minI = j;
58814
- minV = apNew[j]->pgno;
58815
- }
58816
- }
58817
- if( minI>i ){
58818
- MemPage *pT;
58819
- pT = apNew[i];
58820
- apNew[i] = apNew[minI];
58821
- apNew[minI] = pT;
58822
- }
58823
- }
58824
- TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
58825
- apNew[0]->pgno, szNew[0],
59326
+ ** Reassign page numbers so that the new pages are in ascending order.
59327
+ ** This helps to keep entries in the disk file in order so that a scan
59328
+ ** of the table is closer to a linear scan through the file. That in turn
59329
+ ** helps the operating system to deliver pages from the disk more rapidly.
59330
+ **
59331
+ ** An O(n^2) insertion sort algorithm is used, but since n is never more
59332
+ ** than (NB+2) (a small constant), that should not be a problem.
59333
+ **
59334
+ ** When NB==3, this one optimization makes the database about 25% faster
59335
+ ** for large insertions and deletions.
59336
+ */
59337
+ for(i=0; i<nNew; i++){
59338
+ aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
59339
+ aPgFlags[i] = apNew[i]->pDbPage->flags;
59340
+ for(j=0; j<i; j++){
59341
+ if( aPgno[j]==aPgno[i] ){
59342
+ /* This branch is taken if the set of sibling pages somehow contains
59343
+ ** duplicate entries. This can happen if the database is corrupt.
59344
+ ** It would be simpler to detect this as part of the loop below, but
59345
+ ** we do the detection here in order to avoid populating the pager
59346
+ ** cache with two separate objects associated with the same
59347
+ ** page number. */
59348
+ assert( CORRUPT_DB );
59349
+ rc = SQLITE_CORRUPT_BKPT;
59350
+ goto balance_cleanup;
59351
+ }
59352
+ }
59353
+ }
59354
+ for(i=0; i<nNew; i++){
59355
+ int iBest = 0; /* aPgno[] index of page number to use */
59356
+ for(j=1; j<nNew; j++){
59357
+ if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
59358
+ }
59359
+ pgno = aPgOrder[iBest];
59360
+ aPgOrder[iBest] = 0xffffffff;
59361
+ if( iBest!=i ){
59362
+ if( iBest>i ){
59363
+ sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
59364
+ }
59365
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
59366
+ apNew[i]->pgno = pgno;
59367
+ }
59368
+ }
59369
+
59370
+ TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
59371
+ "%d(%d nc=%d) %d(%d nc=%d)\n",
59372
+ apNew[0]->pgno, szNew[0], cntNew[0],
5882659373
nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
59374
+ nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
5882759375
nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
59376
+ nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
5882859377
nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
58829
- nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
59378
+ nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
59379
+ nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
59380
+ nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
59381
+ ));
5883059382
5883159383
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
5883259384
put4byte(pRight, apNew[nNew-1]->pgno);
5883359385
58834
- /*
58835
- ** Evenly distribute the data in apCell[] across the new pages.
58836
- ** Insert divider cells into pParent as necessary.
59386
+ /* If the sibling pages are not leaves, ensure that the right-child pointer
59387
+ ** of the right-most new sibling page is set to the value that was
59388
+ ** originally in the same field of the right-most old sibling page. */
59389
+ if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
59390
+ MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
59391
+ memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
59392
+ }
59393
+
59394
+ /* Make any required updates to pointer map entries associated with
59395
+ ** cells stored on sibling pages following the balance operation. Pointer
59396
+ ** map entries associated with divider cells are set by the insertCell()
59397
+ ** routine. The associated pointer map entries are:
59398
+ **
59399
+ ** a) if the cell contains a reference to an overflow chain, the
59400
+ ** entry associated with the first page in the overflow chain, and
59401
+ **
59402
+ ** b) if the sibling pages are not leaves, the child page associated
59403
+ ** with the cell.
59404
+ **
59405
+ ** If the sibling pages are not leaves, then the pointer map entry
59406
+ ** associated with the right-child of each sibling may also need to be
59407
+ ** updated. This happens below, after the sibling pages have been
59408
+ ** populated, not here.
5883759409
*/
58838
- j = 0;
58839
- for(i=0; i<nNew; i++){
58840
- /* Assemble the new sibling page. */
59410
+ if( ISAUTOVACUUM ){
59411
+ MemPage *pNew = apNew[0];
59412
+ u8 *aOld = pNew->aData;
59413
+ int cntOldNext = pNew->nCell + pNew->nOverflow;
59414
+ int usableSize = pBt->usableSize;
59415
+ int iNew = 0;
59416
+ int iOld = 0;
59417
+
59418
+ for(i=0; i<nCell; i++){
59419
+ u8 *pCell = apCell[i];
59420
+ if( i==cntOldNext ){
59421
+ MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
59422
+ cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
59423
+ aOld = pOld->aData;
59424
+ }
59425
+ if( i==cntNew[iNew] ){
59426
+ pNew = apNew[++iNew];
59427
+ if( !leafData ) continue;
59428
+ }
59429
+
59430
+ /* Cell pCell is destined for new sibling page pNew. Originally, it
59431
+ ** was either part of sibling page iOld (possibly an overflow cell),
59432
+ ** or else the divider cell to the left of sibling page iOld. So,
59433
+ ** if sibling page iOld had the same page number as pNew, and if
59434
+ ** pCell really was a part of sibling page iOld (not a divider or
59435
+ ** overflow cell), we can skip updating the pointer map entries. */
59436
+ if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){
59437
+ if( !leafCorrection ){
59438
+ ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
59439
+ }
59440
+ if( szCell[i]>pNew->minLocal ){
59441
+ ptrmapPutOvflPtr(pNew, pCell, &rc);
59442
+ }
59443
+ }
59444
+ }
59445
+ }
59446
+
59447
+ /* Insert new divider cells into pParent. */
59448
+ for(i=0; i<nNew-1; i++){
59449
+ u8 *pCell;
59450
+ u8 *pTemp;
59451
+ int sz;
5884159452
MemPage *pNew = apNew[i];
58842
- assert( j<nMaxCells );
58843
- zeroPage(pNew, pageFlags);
58844
- assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
58845
- assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
58846
- assert( pNew->nOverflow==0 );
58847
-
5884859453
j = cntNew[i];
5884959454
58850
- /* If the sibling page assembled above was not the right-most sibling,
58851
- ** insert a divider cell into the parent page.
58852
- */
58853
- assert( i<nNew-1 || j==nCell );
58854
- if( j<nCell ){
58855
- u8 *pCell;
58856
- u8 *pTemp;
58857
- int sz;
58858
-
58859
- assert( j<nMaxCells );
58860
- pCell = apCell[j];
58861
- sz = szCell[j] + leafCorrection;
58862
- pTemp = &aOvflSpace[iOvflSpace];
58863
- if( !pNew->leaf ){
58864
- memcpy(&pNew->aData[8], pCell, 4);
58865
- }else if( leafData ){
58866
- /* If the tree is a leaf-data tree, and the siblings are leaves,
58867
- ** then there is no divider cell in apCell[]. Instead, the divider
58868
- ** cell consists of the integer key for the right-most cell of
58869
- ** the sibling-page assembled above only.
58870
- */
58871
- CellInfo info;
58872
- j--;
58873
- btreeParseCellPtr(pNew, apCell[j], &info);
58874
- pCell = pTemp;
58875
- sz = 4 + putVarint(&pCell[4], info.nKey);
58876
- pTemp = 0;
58877
- }else{
58878
- pCell -= 4;
58879
- /* Obscure case for non-leaf-data trees: If the cell at pCell was
58880
- ** previously stored on a leaf node, and its reported size was 4
58881
- ** bytes, then it may actually be smaller than this
58882
- ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
58883
- ** any cell). But it is important to pass the correct size to
58884
- ** insertCell(), so reparse the cell now.
58885
- **
58886
- ** Note that this can never happen in an SQLite data file, as all
58887
- ** cells are at least 4 bytes. It only happens in b-trees used
58888
- ** to evaluate "IN (SELECT ...)" and similar clauses.
58889
- */
58890
- if( szCell[j]==4 ){
58891
- assert(leafCorrection==4);
58892
- sz = cellSizePtr(pParent, pCell);
58893
- }
58894
- }
58895
- iOvflSpace += sz;
58896
- assert( sz<=pBt->maxLocal+23 );
58897
- assert( iOvflSpace <= (int)pBt->pageSize );
58898
- insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
58899
- if( rc!=SQLITE_OK ) goto balance_cleanup;
58900
- assert( sqlite3PagerIswriteable(pParent->pDbPage) );
58901
-
58902
- j++;
58903
- nxDiv++;
58904
- }
58905
- }
58906
- assert( j==nCell );
59455
+ assert( j<nMaxCells );
59456
+ pCell = apCell[j];
59457
+ sz = szCell[j] + leafCorrection;
59458
+ pTemp = &aOvflSpace[iOvflSpace];
59459
+ if( !pNew->leaf ){
59460
+ memcpy(&pNew->aData[8], pCell, 4);
59461
+ }else if( leafData ){
59462
+ /* If the tree is a leaf-data tree, and the siblings are leaves,
59463
+ ** then there is no divider cell in apCell[]. Instead, the divider
59464
+ ** cell consists of the integer key for the right-most cell of
59465
+ ** the sibling-page assembled above only.
59466
+ */
59467
+ CellInfo info;
59468
+ j--;
59469
+ btreeParseCellPtr(pNew, apCell[j], &info);
59470
+ pCell = pTemp;
59471
+ sz = 4 + putVarint(&pCell[4], info.nKey);
59472
+ pTemp = 0;
59473
+ }else{
59474
+ pCell -= 4;
59475
+ /* Obscure case for non-leaf-data trees: If the cell at pCell was
59476
+ ** previously stored on a leaf node, and its reported size was 4
59477
+ ** bytes, then it may actually be smaller than this
59478
+ ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
59479
+ ** any cell). But it is important to pass the correct size to
59480
+ ** insertCell(), so reparse the cell now.
59481
+ **
59482
+ ** Note that this can never happen in an SQLite data file, as all
59483
+ ** cells are at least 4 bytes. It only happens in b-trees used
59484
+ ** to evaluate "IN (SELECT ...)" and similar clauses.
59485
+ */
59486
+ if( szCell[j]==4 ){
59487
+ assert(leafCorrection==4);
59488
+ sz = cellSizePtr(pParent, pCell);
59489
+ }
59490
+ }
59491
+ iOvflSpace += sz;
59492
+ assert( sz<=pBt->maxLocal+23 );
59493
+ assert( iOvflSpace <= (int)pBt->pageSize );
59494
+ insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
59495
+ if( rc!=SQLITE_OK ) goto balance_cleanup;
59496
+ assert( sqlite3PagerIswriteable(pParent->pDbPage) );
59497
+ }
59498
+
59499
+ /* Now update the actual sibling pages. The order in which they are updated
59500
+ ** is important, as this code needs to avoid disrupting any page from which
59501
+ ** cells may still to be read. In practice, this means:
59502
+ **
59503
+ ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
59504
+ ** then it is not safe to update page apNew[iPg] until after
59505
+ ** the left-hand sibling apNew[iPg-1] has been updated.
59506
+ **
59507
+ ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
59508
+ ** then it is not safe to update page apNew[iPg] until after
59509
+ ** the right-hand sibling apNew[iPg+1] has been updated.
59510
+ **
59511
+ ** If neither of the above apply, the page is safe to update.
59512
+ **
59513
+ ** The iPg value in the following loop starts at nNew-1 goes down
59514
+ ** to 0, then back up to nNew-1 again, thus making two passes over
59515
+ ** the pages. On the initial downward pass, only condition (1) above
59516
+ ** needs to be tested because (2) will always be true from the previous
59517
+ ** step. On the upward pass, both conditions are always true, so the
59518
+ ** upwards pass simply processes pages that were missed on the downward
59519
+ ** pass.
59520
+ */
59521
+ for(i=1-nNew; i<nNew; i++){
59522
+ int iPg = i<0 ? -i : i;
59523
+ assert( iPg>=0 && iPg<nNew );
59524
+ if( abDone[iPg] ) continue; /* Skip pages already processed */
59525
+ if( i>=0 /* On the upwards pass, or... */
59526
+ || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
59527
+ ){
59528
+ int iNew;
59529
+ int iOld;
59530
+ int nNewCell;
59531
+
59532
+ /* Verify condition (1): If cells are moving left, update iPg
59533
+ ** only after iPg-1 has already been updated. */
59534
+ assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
59535
+
59536
+ /* Verify condition (2): If cells are moving right, update iPg
59537
+ ** only after iPg+1 has already been updated. */
59538
+ assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
59539
+
59540
+ if( iPg==0 ){
59541
+ iNew = iOld = 0;
59542
+ nNewCell = cntNew[0];
59543
+ }else{
59544
+ iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
59545
+ iNew = cntNew[iPg-1] + !leafData;
59546
+ nNewCell = cntNew[iPg] - iNew;
59547
+ }
59548
+
59549
+ editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
59550
+ abDone[iPg]++;
59551
+ apNew[iPg]->nFree = usableSpace-szNew[iPg];
59552
+ assert( apNew[iPg]->nOverflow==0 );
59553
+ assert( apNew[iPg]->nCell==nNewCell );
59554
+ }
59555
+ }
59556
+
59557
+ /* All pages have been processed exactly once */
59558
+ assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
59559
+
5890759560
assert( nOld>0 );
5890859561
assert( nNew>0 );
58909
- if( (pageFlags & PTF_LEAF)==0 ){
58910
- u8 *zChild = &apCopy[nOld-1]->aData[8];
58911
- memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
58912
- }
5891359562
5891459563
if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
5891559564
/* The root page of the b-tree now contains no cells. The only sibling
5891659565
** page is the right-child of the parent. Copy the contents of the
5891759566
** child page into the parent, decreasing the overall height of the
@@ -58920,130 +59569,54 @@
5892059569
**
5892159570
** If this is an auto-vacuum database, the call to copyNodeContent()
5892259571
** sets all pointer-map entries corresponding to database image pages
5892359572
** for which the pointer is stored within the content being copied.
5892459573
**
58925
- ** The second assert below verifies that the child page is defragmented
58926
- ** (it must be, as it was just reconstructed using assemblePage()). This
58927
- ** is important if the parent page happens to be page 1 of the database
58928
- ** image. */
59574
+ ** It is critical that the child page be defragmented before being
59575
+ ** copied into the parent, because if the parent is page 1 then it will
59576
+ ** by smaller than the child due to the database header, and so all the
59577
+ ** free space needs to be up front.
59578
+ */
5892959579
assert( nNew==1 );
59580
+ rc = defragmentPage(apNew[0]);
59581
+ testcase( rc!=SQLITE_OK );
5893059582
assert( apNew[0]->nFree ==
58931
- (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
59583
+ (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
59584
+ || rc!=SQLITE_OK
5893259585
);
5893359586
copyNodeContent(apNew[0], pParent, &rc);
5893459587
freePage(apNew[0], &rc);
58935
- }else if( ISAUTOVACUUM ){
58936
- /* Fix the pointer-map entries for all the cells that were shifted around.
58937
- ** There are several different types of pointer-map entries that need to
58938
- ** be dealt with by this routine. Some of these have been set already, but
58939
- ** many have not. The following is a summary:
58940
- **
58941
- ** 1) The entries associated with new sibling pages that were not
58942
- ** siblings when this function was called. These have already
58943
- ** been set. We don't need to worry about old siblings that were
58944
- ** moved to the free-list - the freePage() code has taken care
58945
- ** of those.
58946
- **
58947
- ** 2) The pointer-map entries associated with the first overflow
58948
- ** page in any overflow chains used by new divider cells. These
58949
- ** have also already been taken care of by the insertCell() code.
58950
- **
58951
- ** 3) If the sibling pages are not leaves, then the child pages of
58952
- ** cells stored on the sibling pages may need to be updated.
58953
- **
58954
- ** 4) If the sibling pages are not internal intkey nodes, then any
58955
- ** overflow pages used by these cells may need to be updated
58956
- ** (internal intkey nodes never contain pointers to overflow pages).
58957
- **
58958
- ** 5) If the sibling pages are not leaves, then the pointer-map
58959
- ** entries for the right-child pages of each sibling may need
58960
- ** to be updated.
58961
- **
58962
- ** Cases 1 and 2 are dealt with above by other code. The next
58963
- ** block deals with cases 3 and 4 and the one after that, case 5. Since
58964
- ** setting a pointer map entry is a relatively expensive operation, this
58965
- ** code only sets pointer map entries for child or overflow pages that have
58966
- ** actually moved between pages. */
58967
- MemPage *pNew = apNew[0];
58968
- MemPage *pOld = apCopy[0];
58969
- int nOverflow = pOld->nOverflow;
58970
- int iNextOld = pOld->nCell + nOverflow;
58971
- int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
58972
- j = 0; /* Current 'old' sibling page */
58973
- k = 0; /* Current 'new' sibling page */
58974
- for(i=0; i<nCell; i++){
58975
- int isDivider = 0;
58976
- while( i==iNextOld ){
58977
- /* Cell i is the cell immediately following the last cell on old
58978
- ** sibling page j. If the siblings are not leaf pages of an
58979
- ** intkey b-tree, then cell i was a divider cell. */
58980
- assert( j+1 < ArraySize(apCopy) );
58981
- assert( j+1 < nOld );
58982
- pOld = apCopy[++j];
58983
- iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
58984
- if( pOld->nOverflow ){
58985
- nOverflow = pOld->nOverflow;
58986
- iOverflow = i + !leafData + pOld->aiOvfl[0];
58987
- }
58988
- isDivider = !leafData;
58989
- }
58990
-
58991
- assert(nOverflow>0 || iOverflow<i );
58992
- assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
58993
- assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
58994
- if( i==iOverflow ){
58995
- isDivider = 1;
58996
- if( (--nOverflow)>0 ){
58997
- iOverflow++;
58998
- }
58999
- }
59000
-
59001
- if( i==cntNew[k] ){
59002
- /* Cell i is the cell immediately following the last cell on new
59003
- ** sibling page k. If the siblings are not leaf pages of an
59004
- ** intkey b-tree, then cell i is a divider cell. */
59005
- pNew = apNew[++k];
59006
- if( !leafData ) continue;
59007
- }
59008
- assert( j<nOld );
59009
- assert( k<nNew );
59010
-
59011
- /* If the cell was originally divider cell (and is not now) or
59012
- ** an overflow cell, or if the cell was located on a different sibling
59013
- ** page before the balancing, then the pointer map entries associated
59014
- ** with any child or overflow pages need to be updated. */
59015
- if( isDivider || pOld->pgno!=pNew->pgno ){
59016
- if( !leafCorrection ){
59017
- ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
59018
- }
59019
- if( szCell[i]>pNew->minLocal ){
59020
- ptrmapPutOvflPtr(pNew, apCell[i], &rc);
59021
- }
59022
- }
59023
- }
59024
-
59025
- if( !leafCorrection ){
59026
- for(i=0; i<nNew; i++){
59027
- u32 key = get4byte(&apNew[i]->aData[8]);
59028
- ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
59029
- }
59030
- }
59588
+ }else if( ISAUTOVACUUM && !leafCorrection ){
59589
+ /* Fix the pointer map entries associated with the right-child of each
59590
+ ** sibling page. All other pointer map entries have already been taken
59591
+ ** care of. */
59592
+ for(i=0; i<nNew; i++){
59593
+ u32 key = get4byte(&apNew[i]->aData[8]);
59594
+ ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
59595
+ }
59596
+ }
59597
+
59598
+ assert( pParent->isInit );
59599
+ TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
59600
+ nOld, nNew, nCell));
59601
+
59602
+ /* Free any old pages that were not reused as new pages.
59603
+ */
59604
+ for(i=nNew; i<nOld; i++){
59605
+ freePage(apOld[i], &rc);
59606
+ }
5903159607
5903259608
#if 0
59609
+ if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
5903359610
/* The ptrmapCheckPages() contains assert() statements that verify that
5903459611
** all pointer map pages are set correctly. This is helpful while
5903559612
** debugging. This is usually disabled because a corrupt database may
5903659613
** cause an assert() statement to fail. */
5903759614
ptrmapCheckPages(apNew, nNew);
5903859615
ptrmapCheckPages(&pParent, 1);
59616
+ }
5903959617
#endif
59040
- }
59041
-
59042
- assert( pParent->isInit );
59043
- TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
59044
- nOld, nNew, nCell));
5904559618
5904659619
/*
5904759620
** Cleanup before returning.
5904859621
*/
5904959622
balance_cleanup:
@@ -60865,10 +61438,15 @@
6086561438
*/
6086661439
SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
6086761440
return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
6086861441
}
6086961442
61443
+/*
61444
+** Return the size of the header added to each page by this module.
61445
+*/
61446
+SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); }
61447
+
6087061448
/************** End of btree.c ***********************************************/
6087161449
/************** Begin file backup.c ******************************************/
6087261450
/*
6087361451
** 2009 January 28
6087461452
**
@@ -61004,10 +61582,17 @@
6100461582
const char *zDestDb, /* Name of database within pDestDb */
6100561583
sqlite3* pSrcDb, /* Database connection to read from */
6100661584
const char *zSrcDb /* Name of database within pSrcDb */
6100761585
){
6100861586
sqlite3_backup *p; /* Value to return */
61587
+
61588
+#ifdef SQLITE_ENABLE_API_ARMOR
61589
+ if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
61590
+ (void)SQLITE_MISUSE_BKPT;
61591
+ return 0;
61592
+ }
61593
+#endif
6100961594
6101061595
/* Lock the source database handle. The destination database
6101161596
** handle is not locked in this routine, but it is locked in
6101261597
** sqlite3_backup_step(). The user is required to ensure that no
6101361598
** other thread accesses the destination handle for the duration
@@ -61201,10 +61786,13 @@
6120161786
int rc;
6120261787
int destMode; /* Destination journal mode */
6120361788
int pgszSrc = 0; /* Source page size */
6120461789
int pgszDest = 0; /* Destination page size */
6120561790
61791
+#ifdef SQLITE_ENABLE_API_ARMOR
61792
+ if( p==0 ) return SQLITE_MISUSE_BKPT;
61793
+#endif
6120661794
sqlite3_mutex_enter(p->pSrcDb->mutex);
6120761795
sqlite3BtreeEnter(p->pSrc);
6120861796
if( p->pDestDb ){
6120961797
sqlite3_mutex_enter(p->pDestDb->mutex);
6121061798
}
@@ -61464,11 +62052,11 @@
6146462052
}
6146562053
*pp = p->pNext;
6146662054
}
6146762055
6146862056
/* If a transaction is still open on the Btree, roll it back. */
61469
- sqlite3BtreeRollback(p->pDest, SQLITE_OK);
62057
+ sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
6147062058
6147162059
/* Set the error code of the destination database handle. */
6147262060
rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
6147362061
if( p->pDestDb ){
6147462062
sqlite3Error(p->pDestDb, rc);
@@ -61490,18 +62078,30 @@
6149062078
/*
6149162079
** Return the number of pages still to be backed up as of the most recent
6149262080
** call to sqlite3_backup_step().
6149362081
*/
6149462082
SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
62083
+#ifdef SQLITE_ENABLE_API_ARMOR
62084
+ if( p==0 ){
62085
+ (void)SQLITE_MISUSE_BKPT;
62086
+ return 0;
62087
+ }
62088
+#endif
6149562089
return p->nRemaining;
6149662090
}
6149762091
6149862092
/*
6149962093
** Return the total number of pages in the source database as of the most
6150062094
** recent call to sqlite3_backup_step().
6150162095
*/
6150262096
SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
62097
+#ifdef SQLITE_ENABLE_API_ARMOR
62098
+ if( p==0 ){
62099
+ (void)SQLITE_MISUSE_BKPT;
62100
+ return 0;
62101
+ }
62102
+#endif
6150362103
return p->nPagecount;
6150462104
}
6150562105
6150662106
/*
6150762107
** This function is called after the contents of page iPage of the
@@ -63788,10 +64388,38 @@
6378864388
}
6378964389
p->nOp += nOp;
6379064390
}
6379164391
return addr;
6379264392
}
64393
+
64394
+#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
64395
+/*
64396
+** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
64397
+*/
64398
+SQLITE_PRIVATE void sqlite3VdbeScanStatus(
64399
+ Vdbe *p, /* VM to add scanstatus() to */
64400
+ int addrExplain, /* Address of OP_Explain (or 0) */
64401
+ int addrLoop, /* Address of loop counter */
64402
+ int addrVisit, /* Address of rows visited counter */
64403
+ LogEst nEst, /* Estimated number of output rows */
64404
+ const char *zName /* Name of table or index being scanned */
64405
+){
64406
+ int nByte = (p->nScan+1) * sizeof(ScanStatus);
64407
+ ScanStatus *aNew;
64408
+ aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
64409
+ if( aNew ){
64410
+ ScanStatus *pNew = &aNew[p->nScan++];
64411
+ pNew->addrExplain = addrExplain;
64412
+ pNew->addrLoop = addrLoop;
64413
+ pNew->addrVisit = addrVisit;
64414
+ pNew->nEst = nEst;
64415
+ pNew->zName = sqlite3DbStrDup(p->db, zName);
64416
+ p->aScan = aNew;
64417
+ }
64418
+}
64419
+#endif
64420
+
6379364421
6379464422
/*
6379564423
** Change the value of the P1 operand for a specific instruction.
6379664424
** This routine is useful when a large program is loaded from a
6379764425
** static array using sqlite3VdbeAddOpList but we want to make a
@@ -64887,10 +65515,13 @@
6488765515
p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
6488865516
p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
6488965517
p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
6489065518
&zCsr, zEnd, &nByte);
6489165519
p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
65520
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
65521
+ p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
65522
+#endif
6489265523
if( nByte ){
6489365524
p->pFree = sqlite3DbMallocZero(db, nByte);
6489465525
}
6489565526
zCsr = p->pFree;
6489665527
zEnd = &zCsr[nByte];
@@ -64954,10 +65585,13 @@
6495465585
** is used, for example, when a trigger sub-program is halted to restore
6495565586
** control to the main program.
6495665587
*/
6495765588
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
6495865589
Vdbe *v = pFrame->v;
65590
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
65591
+ v->anExec = pFrame->anExec;
65592
+#endif
6495965593
v->aOnceFlag = pFrame->aOnceFlag;
6496065594
v->nOnceFlag = pFrame->nOnceFlag;
6496165595
v->aOp = pFrame->aOp;
6496265596
v->nOp = pFrame->nOp;
6496365597
v->aMem = pFrame->aMem;
@@ -64964,10 +65598,11 @@
6496465598
v->nMem = pFrame->nMem;
6496565599
v->apCsr = pFrame->apCsr;
6496665600
v->nCursor = pFrame->nCursor;
6496765601
v->db->lastRowid = pFrame->lastRowid;
6496865602
v->nChange = pFrame->nChange;
65603
+ v->db->nChange = pFrame->nDbChange;
6496965604
return pFrame->pc;
6497065605
}
6497165606
6497265607
/*
6497365608
** Close all cursors.
@@ -65531,10 +66166,11 @@
6553166166
** so, abort any other statements this handle currently has active.
6553266167
*/
6553366168
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
6553466169
sqlite3CloseSavepoints(db);
6553566170
db->autoCommit = 1;
66171
+ p->nChange = 0;
6553666172
}
6553766173
}
6553866174
}
6553966175
6554066176
/* Check for immediate foreign key violations. */
@@ -65571,18 +66207,20 @@
6557166207
sqlite3VdbeLeave(p);
6557266208
return SQLITE_BUSY;
6557366209
}else if( rc!=SQLITE_OK ){
6557466210
p->rc = rc;
6557566211
sqlite3RollbackAll(db, SQLITE_OK);
66212
+ p->nChange = 0;
6557666213
}else{
6557766214
db->nDeferredCons = 0;
6557866215
db->nDeferredImmCons = 0;
6557966216
db->flags &= ~SQLITE_DeferFKs;
6558066217
sqlite3CommitInternalChanges(db);
6558166218
}
6558266219
}else{
6558366220
sqlite3RollbackAll(db, SQLITE_OK);
66221
+ p->nChange = 0;
6558466222
}
6558566223
db->nStatement = 0;
6558666224
}else if( eStatementOp==0 ){
6558766225
if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
6558866226
eStatementOp = SAVEPOINT_RELEASE;
@@ -65590,10 +66228,11 @@
6559066228
eStatementOp = SAVEPOINT_ROLLBACK;
6559166229
}else{
6559266230
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
6559366231
sqlite3CloseSavepoints(db);
6559466232
db->autoCommit = 1;
66233
+ p->nChange = 0;
6559566234
}
6559666235
}
6559766236
6559866237
/* If eStatementOp is non-zero, then a statement transaction needs to
6559966238
** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
@@ -65610,10 +66249,11 @@
6561066249
p->zErrMsg = 0;
6561166250
}
6561266251
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
6561366252
sqlite3CloseSavepoints(db);
6561466253
db->autoCommit = 1;
66254
+ p->nChange = 0;
6561566255
}
6561666256
}
6561766257
6561866258
/* If this was an INSERT, UPDATE or DELETE and no statement transaction
6561966259
** has been rolled back, update the database connection change-counter.
@@ -65871,10 +66511,16 @@
6587166511
for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
6587266512
vdbeFreeOpArray(db, p->aOp, p->nOp);
6587366513
sqlite3DbFree(db, p->aColName);
6587466514
sqlite3DbFree(db, p->zSql);
6587566515
sqlite3DbFree(db, p->pFree);
66516
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
66517
+ for(i=0; i<p->nScan; i++){
66518
+ sqlite3DbFree(db, p->aScan[i].zName);
66519
+ }
66520
+ sqlite3DbFree(db, p->aScan);
66521
+#endif
6587666522
}
6587766523
6587866524
/*
6587966525
** Delete an entire VDBE.
6588066526
*/
@@ -68238,15 +68884,23 @@
6823868884
sqlite3_stmt *pStmt,
6823968885
int N,
6824068886
const void *(*xFunc)(Mem*),
6824168887
int useType
6824268888
){
68243
- const void *ret = 0;
68244
- Vdbe *p = (Vdbe *)pStmt;
68889
+ const void *ret;
68890
+ Vdbe *p;
6824568891
int n;
68246
- sqlite3 *db = p->db;
68247
-
68892
+ sqlite3 *db;
68893
+#ifdef SQLITE_ENABLE_API_ARMOR
68894
+ if( pStmt==0 ){
68895
+ (void)SQLITE_MISUSE_BKPT;
68896
+ return 0;
68897
+ }
68898
+#endif
68899
+ ret = 0;
68900
+ p = (Vdbe *)pStmt;
68901
+ db = p->db;
6824868902
assert( db!=0 );
6824968903
n = sqlite3_column_count(pStmt);
6825068904
if( N<n && N>=0 ){
6825168905
N += useType*n;
6825268906
sqlite3_mutex_enter(db->mutex);
@@ -68707,10 +69361,16 @@
6870769361
** prepared statement for the database connection. Return NULL if there
6870869362
** are no more.
6870969363
*/
6871069364
SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
6871169365
sqlite3_stmt *pNext;
69366
+#ifdef SQLITE_ENABLE_API_ARMOR
69367
+ if( !sqlite3SafetyCheckOk(pDb) ){
69368
+ (void)SQLITE_MISUSE_BKPT;
69369
+ return 0;
69370
+ }
69371
+#endif
6871269372
sqlite3_mutex_enter(pDb->mutex);
6871369373
if( pStmt==0 ){
6871469374
pNext = (sqlite3_stmt*)pDb->pVdbe;
6871569375
}else{
6871669376
pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
@@ -68722,15 +69382,91 @@
6872269382
/*
6872369383
** Return the value of a status counter for a prepared statement
6872469384
*/
6872569385
SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
6872669386
Vdbe *pVdbe = (Vdbe*)pStmt;
68727
- u32 v = pVdbe->aCounter[op];
69387
+ u32 v;
69388
+#ifdef SQLITE_ENABLE_API_ARMOR
69389
+ if( !pStmt ){
69390
+ (void)SQLITE_MISUSE_BKPT;
69391
+ return 0;
69392
+ }
69393
+#endif
69394
+ v = pVdbe->aCounter[op];
6872869395
if( resetFlag ) pVdbe->aCounter[op] = 0;
6872969396
return (int)v;
6873069397
}
6873169398
69399
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69400
+/*
69401
+** Return status data for a single loop within query pStmt.
69402
+*/
69403
+SQLITE_API int sqlite3_stmt_scanstatus(
69404
+ sqlite3_stmt *pStmt, /* Prepared statement being queried */
69405
+ int idx, /* Index of loop to report on */
69406
+ int iScanStatusOp, /* Which metric to return */
69407
+ void *pOut /* OUT: Write the answer here */
69408
+){
69409
+ Vdbe *p = (Vdbe*)pStmt;
69410
+ ScanStatus *pScan;
69411
+ if( idx<0 || idx>=p->nScan ) return 1;
69412
+ pScan = &p->aScan[idx];
69413
+ switch( iScanStatusOp ){
69414
+ case SQLITE_SCANSTAT_NLOOP: {
69415
+ *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
69416
+ break;
69417
+ }
69418
+ case SQLITE_SCANSTAT_NVISIT: {
69419
+ *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
69420
+ break;
69421
+ }
69422
+ case SQLITE_SCANSTAT_EST: {
69423
+ double r = 1.0;
69424
+ LogEst x = pScan->nEst;
69425
+ while( x<100 ){
69426
+ x += 10;
69427
+ r *= 0.5;
69428
+ }
69429
+ *(double*)pOut = r*sqlite3LogEstToInt(x);
69430
+ break;
69431
+ }
69432
+ case SQLITE_SCANSTAT_NAME: {
69433
+ *(const char**)pOut = pScan->zName;
69434
+ break;
69435
+ }
69436
+ case SQLITE_SCANSTAT_EXPLAIN: {
69437
+ if( pScan->addrExplain ){
69438
+ *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
69439
+ }else{
69440
+ *(const char**)pOut = 0;
69441
+ }
69442
+ break;
69443
+ }
69444
+ case SQLITE_SCANSTAT_SELECTID: {
69445
+ if( pScan->addrExplain ){
69446
+ *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
69447
+ }else{
69448
+ *(int*)pOut = -1;
69449
+ }
69450
+ break;
69451
+ }
69452
+ default: {
69453
+ return 1;
69454
+ }
69455
+ }
69456
+ return 0;
69457
+}
69458
+
69459
+/*
69460
+** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
69461
+*/
69462
+SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
69463
+ Vdbe *p = (Vdbe*)pStmt;
69464
+ memset(p->anExec, 0, p->nOp * sizeof(i64));
69465
+}
69466
+#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
69467
+
6873269468
/************** End of vdbeapi.c *********************************************/
6873369469
/************** Begin file vdbetrace.c ***************************************/
6873469470
/*
6873569471
** 2009 November 25
6873669472
**
@@ -69612,10 +70348,13 @@
6961270348
#ifdef VDBE_PROFILE
6961370349
start = sqlite3Hwtime();
6961470350
#endif
6961570351
nVmStep++;
6961670352
pOp = &aOp[pc];
70353
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
70354
+ if( p->anExec ) p->anExec[pc]++;
70355
+#endif
6961770356
6961870357
/* Only allow tracing if SQLITE_DEBUG is defined.
6961970358
*/
6962070359
#ifdef SQLITE_DEBUG
6962170360
if( db->flags & SQLITE_VdbeTrace ){
@@ -71302,11 +72041,11 @@
7130272041
assert( pReg->flags & MEM_Blob );
7130372042
assert( memIsValid(pReg) );
7130472043
pC->payloadSize = pC->szRow = avail = pReg->n;
7130572044
pC->aRow = (u8*)pReg->z;
7130672045
}else{
71307
- MemSetTypeFlag(pDest, MEM_Null);
72046
+ sqlite3VdbeMemSetNull(pDest);
7130872047
goto op_column_out;
7130972048
}
7131072049
}else{
7131172050
assert( pCrsr );
7131272051
if( pC->isTable==0 ){
@@ -71826,23 +72565,28 @@
7182672565
goto vdbe_return;
7182772566
}
7182872567
db->isTransactionSavepoint = 0;
7182972568
rc = p->rc;
7183072569
}else{
72570
+ int isSchemaChange;
7183172571
iSavepoint = db->nSavepoint - iSavepoint - 1;
7183272572
if( p1==SAVEPOINT_ROLLBACK ){
72573
+ isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
7183372574
for(ii=0; ii<db->nDb; ii++){
71834
- sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
72575
+ sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT,
72576
+ isSchemaChange==0);
7183572577
}
72578
+ }else{
72579
+ isSchemaChange = 0;
7183672580
}
7183772581
for(ii=0; ii<db->nDb; ii++){
7183872582
rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
7183972583
if( rc!=SQLITE_OK ){
7184072584
goto abort_due_to_error;
7184172585
}
7184272586
}
71843
- if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
72587
+ if( isSchemaChange ){
7184472588
sqlite3ExpirePreparedStatements(db);
7184572589
sqlite3ResetAllSchemasOfConnection(db);
7184672590
db->flags = (db->flags | SQLITE_InternChanges);
7184772591
}
7184872592
}
@@ -72235,11 +72979,11 @@
7223572979
assert( p->bIsReader );
7223672980
assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
7223772981
|| p->readOnly==0 );
7223872982
7223972983
if( p->expired ){
72240
- rc = SQLITE_ABORT;
72984
+ rc = SQLITE_ABORT_ROLLBACK;
7224172985
break;
7224272986
}
7224372987
7224472988
nField = 0;
7224572989
pKeyInfo = 0;
@@ -72799,14 +73543,15 @@
7279973543
}
7280073544
pIdxKey = &r;
7280173545
}else{
7280273546
pIdxKey = sqlite3VdbeAllocUnpackedRecord(
7280373547
pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
72804
- );
73548
+ );
7280573549
if( pIdxKey==0 ) goto no_mem;
7280673550
assert( pIn3->flags & MEM_Blob );
72807
- assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
73551
+ /* assert( (pIn3->flags & MEM_Zero)==0 ); // zeroblobs already expanded */
73552
+ ExpandBlob(pIn3);
7280873553
sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
7280973554
}
7281073555
pIdxKey->default_rc = 0;
7281173556
if( pOp->opcode==OP_NoConflict ){
7281273557
/* For the OP_NoConflict opcode, take the jump if any of the
@@ -73492,13 +74237,13 @@
7349274237
}
7349374238
/* Opcode: Rewind P1 P2 * * *
7349474239
**
7349574240
** The next use of the Rowid or Column or Next instruction for P1
7349674241
** will refer to the first entry in the database table or index.
73497
-** If the table or index is empty and P2>0, then jump immediately to P2.
73498
-** If P2 is 0 or if the table or index is not empty, fall through
73499
-** to the following instruction.
74242
+** If the table or index is empty, jump immediately to P2.
74243
+** If the table or index is not empty, fall through to the following
74244
+** instruction.
7350074245
**
7350174246
** This opcode leaves the cursor configured to move in forward order,
7350274247
** from the beginning toward the end. In other words, the cursor is
7350374248
** configured to use Next, not Prev.
7350474249
*/
@@ -74410,10 +75155,13 @@
7441075155
pFrame->aOp = p->aOp;
7441175156
pFrame->nOp = p->nOp;
7441275157
pFrame->token = pProgram->token;
7441375158
pFrame->aOnceFlag = p->aOnceFlag;
7441475159
pFrame->nOnceFlag = p->nOnceFlag;
75160
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
75161
+ pFrame->anExec = p->anExec;
75162
+#endif
7441575163
7441675164
pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
7441775165
for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
7441875166
pMem->flags = MEM_Undefined;
7441975167
pMem->db = db;
@@ -74427,10 +75175,11 @@
7442775175
7442875176
p->nFrame++;
7442975177
pFrame->pParent = p->pFrame;
7443075178
pFrame->lastRowid = lastRowid;
7443175179
pFrame->nChange = p->nChange;
75180
+ pFrame->nDbChange = p->db->nChange;
7443275181
p->nChange = 0;
7443375182
p->pFrame = pFrame;
7443475183
p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
7443575184
p->nMem = pFrame->nChildMem;
7443675185
p->nCursor = (u16)pFrame->nChildCsr;
@@ -74437,10 +75186,13 @@
7443775186
p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
7443875187
p->aOp = aOp = pProgram->aOp;
7443975188
p->nOp = pProgram->nOp;
7444075189
p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
7444175190
p->nOnceFlag = pProgram->nOnce;
75191
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
75192
+ p->anExec = 0;
75193
+#endif
7444275194
pc = -1;
7444375195
memset(p->aOnceFlag, 0, p->nOnceFlag);
7444475196
7444575197
break;
7444675198
}
@@ -75625,10 +76377,15 @@
7562576377
char *zErr = 0;
7562676378
Table *pTab;
7562776379
Parse *pParse = 0;
7562876380
Incrblob *pBlob = 0;
7562976381
76382
+#ifdef SQLITE_ENABLE_API_ARMOR
76383
+ if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){
76384
+ return SQLITE_MISUSE_BKPT;
76385
+ }
76386
+#endif
7563076387
flags = !!flags; /* flags = (flags ? 1 : 0); */
7563176388
*ppBlob = 0;
7563276389
7563376390
sqlite3_mutex_enter(db->mutex);
7563476391
@@ -75843,11 +76600,10 @@
7584376600
v = (Vdbe*)p->pStmt;
7584476601
7584576602
if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
7584676603
/* Request is out of range. Return a transient error. */
7584776604
rc = SQLITE_ERROR;
75848
- sqlite3Error(db, SQLITE_ERROR);
7584976605
}else if( v==0 ){
7585076606
/* If there is no statement handle, then the blob-handle has
7585176607
** already been invalidated. Return SQLITE_ABORT in this case.
7585276608
*/
7585376609
rc = SQLITE_ABORT;
@@ -75861,14 +76617,14 @@
7586176617
sqlite3BtreeLeaveCursor(p->pCsr);
7586276618
if( rc==SQLITE_ABORT ){
7586376619
sqlite3VdbeFinalize(v);
7586476620
p->pStmt = 0;
7586576621
}else{
75866
- db->errCode = rc;
7586776622
v->rc = rc;
7586876623
}
7586976624
}
76625
+ sqlite3Error(db, rc);
7587076626
rc = sqlite3ApiExit(db, rc);
7587176627
sqlite3_mutex_leave(db->mutex);
7587276628
return rc;
7587376629
}
7587476630
@@ -76041,11 +76797,11 @@
7604176797
** itself.
7604276798
**
7604376799
** The sorter is running in multi-threaded mode if (a) the library was built
7604476800
** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
7604576801
** than zero, and (b) worker threads have been enabled at runtime by calling
76046
-** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...).
76802
+** "PRAGMA threads=N" with some value of N greater than 0.
7604776803
**
7604876804
** When Rewind() is called, any data remaining in memory is flushed to a
7604976805
** final PMA. So at this point the data is stored in some number of sorted
7605076806
** PMAs within temporary files on disk.
7605176807
**
@@ -76786,15 +77542,13 @@
7678677542
pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
7678777543
mxCache = db->aDb[0].pSchema->cache_size;
7678877544
if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
7678977545
pSorter->mxPmaSize = mxCache * pgsz;
7679077546
76791
- /* If the application has not configure scratch memory using
76792
- ** SQLITE_CONFIG_SCRATCH then we assume it is OK to do large memory
76793
- ** allocations. If scratch memory has been configured, then assume
76794
- ** large memory allocations should be avoided to prevent heap
76795
- ** fragmentation.
77547
+ /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
77548
+ ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
77549
+ ** large heap allocations.
7679677550
*/
7679777551
if( sqlite3GlobalConfig.pScratch==0 ){
7679877552
assert( pSorter->iMemory==0 );
7679977553
pSorter->nMemory = pgsz;
7680077554
pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
@@ -79162,19 +79916,19 @@
7916279916
**
7916379917
** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
7916479918
** is a helper function - a callback for the tree walker.
7916579919
*/
7916679920
static int incrAggDepth(Walker *pWalker, Expr *pExpr){
79167
- if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
79921
+ if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
7916879922
return WRC_Continue;
7916979923
}
7917079924
static void incrAggFunctionDepth(Expr *pExpr, int N){
7917179925
if( N>0 ){
7917279926
Walker w;
7917379927
memset(&w, 0, sizeof(w));
7917479928
w.xExprCallback = incrAggDepth;
79175
- w.u.i = N;
79929
+ w.u.n = N;
7917679930
sqlite3WalkExpr(&w, pExpr);
7917779931
}
7917879932
}
7917979933
7918079934
/*
@@ -79718,11 +80472,11 @@
7971880472
double r = -1.0;
7971980473
if( p->op!=TK_FLOAT ) return -1;
7972080474
sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
7972180475
assert( r>=0.0 );
7972280476
if( r>1.0 ) return -1;
79723
- return (int)(r*1000.0);
80477
+ return (int)(r*134217728.0);
7972480478
}
7972580479
7972680480
/*
7972780481
** This routine is callback for sqlite3WalkExpr().
7972880482
**
@@ -79850,11 +80604,11 @@
7985080604
** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
7985180605
** likelihood(X,0.9375).
7985280606
** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
7985380607
** likelihood(X,0.9375). */
7985480608
/* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
79855
- pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
80609
+ pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
7985680610
}
7985780611
}
7985880612
#ifndef SQLITE_OMIT_AUTHORIZATION
7985980613
auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
7986080614
if( auth!=SQLITE_OK ){
@@ -81807,69 +82561,79 @@
8180782561
sqlite3DbFree(db, pList->a);
8180882562
sqlite3DbFree(db, pList);
8180982563
}
8181082564
8181182565
/*
81812
-** These routines are Walker callbacks. Walker.u.pi is a pointer
81813
-** to an integer. These routines are checking an expression to see
81814
-** if it is a constant. Set *Walker.u.i to 0 if the expression is
81815
-** not constant.
82566
+** These routines are Walker callbacks used to check expressions to
82567
+** see if they are "constant" for some definition of constant. The
82568
+** Walker.eCode value determines the type of "constant" we are looking
82569
+** for.
8181682570
**
8181782571
** These callback routines are used to implement the following:
8181882572
**
81819
-** sqlite3ExprIsConstant() pWalker->u.i==1
81820
-** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2
81821
-** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4
82573
+** sqlite3ExprIsConstant() pWalker->eCode==1
82574
+** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
82575
+** sqlite3ExprRefOneTableOnly() pWalker->eCode==3
82576
+** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
82577
+**
82578
+** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
82579
+** is found to not be a constant.
8182282580
**
8182382581
** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
81824
-** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing
81825
-** an existing schema and 3 when processing a new statement. A bound
82582
+** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
82583
+** an existing schema and 4 when processing a new statement. A bound
8182682584
** parameter raises an error for new statements, but is silently converted
8182782585
** to NULL for existing schemas. This allows sqlite_master tables that
8182882586
** contain a bound parameter because they were generated by older versions
8182982587
** of SQLite to be parsed by newer versions of SQLite without raising a
8183082588
** malformed schema error.
8183182589
*/
8183282590
static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
8183382591
81834
- /* If pWalker->u.i is 2 then any term of the expression that comes from
81835
- ** the ON or USING clauses of a join disqualifies the expression
82592
+ /* If pWalker->eCode is 2 then any term of the expression that comes from
82593
+ ** the ON or USING clauses of a left join disqualifies the expression
8183682594
** from being considered constant. */
81837
- if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
81838
- pWalker->u.i = 0;
82595
+ if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
82596
+ pWalker->eCode = 0;
8183982597
return WRC_Abort;
8184082598
}
8184182599
8184282600
switch( pExpr->op ){
8184382601
/* Consider functions to be constant if all their arguments are constant
81844
- ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST
81845
- ** flag. */
82602
+ ** and either pWalker->eCode==4 or 5 or the function has the
82603
+ ** SQLITE_FUNC_CONST flag. */
8184682604
case TK_FUNCTION:
81847
- if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){
82605
+ if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){
8184882606
return WRC_Continue;
82607
+ }else{
82608
+ pWalker->eCode = 0;
82609
+ return WRC_Abort;
8184982610
}
81850
- /* Fall through */
8185182611
case TK_ID:
8185282612
case TK_COLUMN:
8185382613
case TK_AGG_FUNCTION:
8185482614
case TK_AGG_COLUMN:
8185582615
testcase( pExpr->op==TK_ID );
8185682616
testcase( pExpr->op==TK_COLUMN );
8185782617
testcase( pExpr->op==TK_AGG_FUNCTION );
8185882618
testcase( pExpr->op==TK_AGG_COLUMN );
81859
- pWalker->u.i = 0;
81860
- return WRC_Abort;
82619
+ if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
82620
+ return WRC_Continue;
82621
+ }else{
82622
+ pWalker->eCode = 0;
82623
+ return WRC_Abort;
82624
+ }
8186182625
case TK_VARIABLE:
81862
- if( pWalker->u.i==4 ){
82626
+ if( pWalker->eCode==5 ){
8186382627
/* Silently convert bound parameters that appear inside of CREATE
8186482628
** statements into a NULL when parsing the CREATE statement text out
8186582629
** of the sqlite_master table */
8186682630
pExpr->op = TK_NULL;
81867
- }else if( pWalker->u.i==3 ){
82631
+ }else if( pWalker->eCode==4 ){
8186882632
/* A bound parameter in a CREATE statement that originates from
8186982633
** sqlite3_prepare() causes an error */
81870
- pWalker->u.i = 0;
82634
+ pWalker->eCode = 0;
8187182635
return WRC_Abort;
8187282636
}
8187382637
/* Fall through */
8187482638
default:
8187582639
testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
@@ -81877,57 +82641,68 @@
8187782641
return WRC_Continue;
8187882642
}
8187982643
}
8188082644
static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
8188182645
UNUSED_PARAMETER(NotUsed);
81882
- pWalker->u.i = 0;
82646
+ pWalker->eCode = 0;
8188382647
return WRC_Abort;
8188482648
}
81885
-static int exprIsConst(Expr *p, int initFlag){
82649
+static int exprIsConst(Expr *p, int initFlag, int iCur){
8188682650
Walker w;
8188782651
memset(&w, 0, sizeof(w));
81888
- w.u.i = initFlag;
82652
+ w.eCode = initFlag;
8188982653
w.xExprCallback = exprNodeIsConstant;
8189082654
w.xSelectCallback = selectNodeIsConstant;
82655
+ w.u.iCur = iCur;
8189182656
sqlite3WalkExpr(&w, p);
81892
- return w.u.i;
82657
+ return w.eCode;
8189382658
}
8189482659
8189582660
/*
81896
-** Walk an expression tree. Return 1 if the expression is constant
82661
+** Walk an expression tree. Return non-zero if the expression is constant
8189782662
** and 0 if it involves variables or function calls.
8189882663
**
8189982664
** For the purposes of this function, a double-quoted string (ex: "abc")
8190082665
** is considered a variable but a single-quoted string (ex: 'abc') is
8190182666
** a constant.
8190282667
*/
8190382668
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
81904
- return exprIsConst(p, 1);
82669
+ return exprIsConst(p, 1, 0);
8190582670
}
8190682671
8190782672
/*
81908
-** Walk an expression tree. Return 1 if the expression is constant
82673
+** Walk an expression tree. Return non-zero if the expression is constant
8190982674
** that does no originate from the ON or USING clauses of a join.
8191082675
** Return 0 if it involves variables or function calls or terms from
8191182676
** an ON or USING clause.
8191282677
*/
8191382678
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
81914
- return exprIsConst(p, 2);
82679
+ return exprIsConst(p, 2, 0);
8191582680
}
8191682681
8191782682
/*
81918
-** Walk an expression tree. Return 1 if the expression is constant
82683
+** Walk an expression tree. Return non-zero if the expression constant
82684
+** for any single row of the table with cursor iCur. In other words, the
82685
+** expression must not refer to any non-deterministic function nor any
82686
+** table other than iCur.
82687
+*/
82688
+SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
82689
+ return exprIsConst(p, 3, iCur);
82690
+}
82691
+
82692
+/*
82693
+** Walk an expression tree. Return non-zero if the expression is constant
8191982694
** or a function call with constant arguments. Return and 0 if there
8192082695
** are any variables.
8192182696
**
8192282697
** For the purposes of this function, a double-quoted string (ex: "abc")
8192382698
** is considered a variable but a single-quoted string (ex: 'abc') is
8192482699
** a constant.
8192582700
*/
8192682701
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
8192782702
assert( isInit==0 || isInit==1 );
81928
- return exprIsConst(p, 3+isInit);
82703
+ return exprIsConst(p, 4+isInit, 0);
8192982704
}
8193082705
8193182706
/*
8193282707
** If the expression p codes a constant integer that is small enough
8193382708
** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -87374,10 +88149,11 @@
8737488149
nSample--;
8737588150
}else{
8737688151
nRow = pIdx->aiRowEst[0];
8737788152
nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
8737888153
}
88154
+ pIdx->nRowEst0 = nRow;
8737988155
8738088156
/* Set nSum to the number of distinct (iCol+1) field prefixes that
8738188157
** occur in the stat4 table for this index. Set sumEq to the sum of
8738288158
** the nEq values for column iCol for the same set (adding the value
8738388159
** only once where there exist duplicate prefixes). */
@@ -87635,11 +88411,11 @@
8763588411
}
8763688412
8763788413
8763888414
/* Load the statistics from the sqlite_stat4 table. */
8763988415
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
87640
- if( rc==SQLITE_OK ){
88416
+ if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
8764188417
int lookasideEnabled = db->lookaside.bEnabled;
8764288418
db->lookaside.bEnabled = 0;
8764388419
rc = loadStat4(db, sInfo.zDatabase);
8764488420
db->lookaside.bEnabled = lookasideEnabled;
8764588421
}
@@ -88317,10 +89093,13 @@
8831789093
SQLITE_API int sqlite3_set_authorizer(
8831889094
sqlite3 *db,
8831989095
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
8832089096
void *pArg
8832189097
){
89098
+#ifdef SQLITE_ENABLE_API_ARMOR
89099
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
89100
+#endif
8832289101
sqlite3_mutex_enter(db->mutex);
8832389102
db->xAuth = (sqlite3_xauth)xAuth;
8832489103
db->pAuthArg = pArg;
8832589104
sqlite3ExpirePreparedStatements(db);
8832689105
sqlite3_mutex_leave(db->mutex);
@@ -88811,11 +89590,15 @@
8881189590
** See also sqlite3LocateTable().
8881289591
*/
8881389592
SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
8881489593
Table *p = 0;
8881589594
int i;
88816
- assert( zName!=0 );
89595
+
89596
+#ifdef SQLITE_ENABLE_API_ARMOR
89597
+ if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0;
89598
+#endif
89599
+
8881789600
/* All mutexes are required for schema access. Make sure we hold them. */
8881889601
assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
8881989602
#if SQLITE_USER_AUTHENTICATION
8882089603
/* Only the admin user is allowed to know that the sqlite_user table
8882189604
** exists */
@@ -103834,13 +104617,16 @@
103834104617
Vdbe *pOld, /* VM being reprepared */
103835104618
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
103836104619
const char **pzTail /* OUT: End of parsed string */
103837104620
){
103838104621
int rc;
103839
- assert( ppStmt!=0 );
104622
+
104623
+#ifdef SQLITE_ENABLE_API_ARMOR
104624
+ if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
104625
+#endif
103840104626
*ppStmt = 0;
103841
- if( !sqlite3SafetyCheckOk(db) ){
104627
+ if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
103842104628
return SQLITE_MISUSE_BKPT;
103843104629
}
103844104630
sqlite3_mutex_enter(db->mutex);
103845104631
sqlite3BtreeEnterAll(db);
103846104632
rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
@@ -103943,13 +104729,15 @@
103943104729
*/
103944104730
char *zSql8;
103945104731
const char *zTail8 = 0;
103946104732
int rc = SQLITE_OK;
103947104733
103948
- assert( ppStmt );
104734
+#ifdef SQLITE_ENABLE_API_ARMOR
104735
+ if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
104736
+#endif
103949104737
*ppStmt = 0;
103950
- if( !sqlite3SafetyCheckOk(db) ){
104738
+ if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
103951104739
return SQLITE_MISUSE_BKPT;
103952104740
}
103953104741
if( nBytes>=0 ){
103954104742
int sz;
103955104743
const char *z = (const char*)zSql;
@@ -109658,10 +110446,13 @@
109658110446
char **pzErrMsg /* Write error messages here */
109659110447
){
109660110448
int rc;
109661110449
TabResult res;
109662110450
110451
+#ifdef SQLITE_ENABLE_API_ARMOR
110452
+ if( pazResult==0 ) return SQLITE_MISUSE_BKPT;
110453
+#endif
109663110454
*pazResult = 0;
109664110455
if( pnColumn ) *pnColumn = 0;
109665110456
if( pnRow ) *pnRow = 0;
109666110457
if( pzErrMsg ) *pzErrMsg = 0;
109667110458
res.zErrMsg = 0;
@@ -111721,11 +112512,11 @@
111721112512
** Two writes per page are required in step (3) because the original
111722112513
** database content must be written into the rollback journal prior to
111723112514
** overwriting the database with the vacuumed content.
111724112515
**
111725112516
** Only 1x temporary space and only 1x writes would be required if
111726
-** the copy of step (3) were replace by deleting the original database
112517
+** the copy of step (3) were replaced by deleting the original database
111727112518
** and renaming the transient database as the original. But that will
111728112519
** not work if other processes are attached to the original database.
111729112520
** And a power loss in between deleting the original and renaming the
111730112521
** transient would cause the database file to appear to be deleted
111731112522
** following reboot.
@@ -112079,10 +112870,13 @@
112079112870
sqlite3 *db, /* Database in which module is registered */
112080112871
const char *zName, /* Name assigned to this module */
112081112872
const sqlite3_module *pModule, /* The definition of the module */
112082112873
void *pAux /* Context pointer for xCreate/xConnect */
112083112874
){
112875
+#ifdef SQLITE_ENABLE_API_ARMOR
112876
+ if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
112877
+#endif
112084112878
return createModule(db, zName, pModule, pAux, 0);
112085112879
}
112086112880
112087112881
/*
112088112882
** External API function used to create a new virtual-table module.
@@ -112092,10 +112886,13 @@
112092112886
const char *zName, /* Name assigned to this module */
112093112887
const sqlite3_module *pModule, /* The definition of the module */
112094112888
void *pAux, /* Context pointer for xCreate/xConnect */
112095112889
void (*xDestroy)(void *) /* Module destructor function */
112096112890
){
112891
+#ifdef SQLITE_ENABLE_API_ARMOR
112892
+ if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
112893
+#endif
112097112894
return createModule(db, zName, pModule, pAux, xDestroy);
112098112895
}
112099112896
112100112897
/*
112101112898
** Lock the virtual table so that it cannot be disconnected.
@@ -112696,10 +113493,13 @@
112696113493
112697113494
int rc = SQLITE_OK;
112698113495
Table *pTab;
112699113496
char *zErr = 0;
112700113497
113498
+#ifdef SQLITE_ENABLE_API_ARMOR
113499
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113500
+#endif
112701113501
sqlite3_mutex_enter(db->mutex);
112702113502
if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
112703113503
sqlite3Error(db, SQLITE_MISUSE);
112704113504
sqlite3_mutex_leave(db->mutex);
112705113505
return SQLITE_MISUSE_BKPT;
@@ -113052,10 +113852,13 @@
113052113852
*/
113053113853
SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
113054113854
static const unsigned char aMap[] = {
113055113855
SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
113056113856
};
113857
+#ifdef SQLITE_ENABLE_API_ARMOR
113858
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113859
+#endif
113057113860
assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
113058113861
assert( OE_Ignore==4 && OE_Replace==5 );
113059113862
assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
113060113863
return (int)aMap[db->vtabOnConflict-1];
113061113864
}
@@ -113067,12 +113870,14 @@
113067113870
*/
113068113871
SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
113069113872
va_list ap;
113070113873
int rc = SQLITE_OK;
113071113874
113875
+#ifdef SQLITE_ENABLE_API_ARMOR
113876
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113877
+#endif
113072113878
sqlite3_mutex_enter(db->mutex);
113073
-
113074113879
va_start(ap, op);
113075113880
switch( op ){
113076113881
case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
113077113882
VtabCtx *p = db->pVtabCtx;
113078113883
if( !p ){
@@ -113203,10 +114008,13 @@
113203114008
} in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
113204114009
Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
113205114010
} u;
113206114011
struct WhereLoop *pWLoop; /* The selected WhereLoop object */
113207114012
Bitmask notReady; /* FROM entries not usable at this level */
114013
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
114014
+ int addrVisit; /* Address at which row is visited */
114015
+#endif
113208114016
};
113209114017
113210114018
/*
113211114019
** Each instance of this object represents an algorithm for evaluating one
113212114020
** term of a join. Every term of the FROM clause will have at least
@@ -113233,11 +114041,10 @@
113233114041
LogEst rRun; /* Cost of running each loop */
113234114042
LogEst nOut; /* Estimated number of output rows */
113235114043
union {
113236114044
struct { /* Information for internal btree tables */
113237114045
u16 nEq; /* Number of equality constraints */
113238
- u16 nSkip; /* Number of initial index columns to skip */
113239114046
Index *pIndex; /* Index used, or NULL */
113240114047
} btree;
113241114048
struct { /* Information for virtual tables */
113242114049
int idxNum; /* Index number */
113243114050
u8 needFree; /* True if sqlite3_free(idxStr) is needed */
@@ -113246,16 +114053,17 @@
113246114053
char *idxStr; /* Index identifier string */
113247114054
} vtab;
113248114055
} u;
113249114056
u32 wsFlags; /* WHERE_* flags describing the plan */
113250114057
u16 nLTerm; /* Number of entries in aLTerm[] */
114058
+ u16 nSkip; /* Number of NULL aLTerm[] entries */
113251114059
/**** whereLoopXfer() copies fields above ***********************/
113252114060
# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
113253114061
u16 nLSlot; /* Number of slots allocated for aLTerm[] */
113254114062
WhereTerm **aLTerm; /* WhereTerms used */
113255114063
WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
113256
- WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */
114064
+ WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
113257114065
};
113258114066
113259114067
/* This object holds the prerequisites and the cost of running a
113260114068
** subquery on one operand of an OR operator in the WHERE clause.
113261114069
** See WhereOrSet for additional information
@@ -113577,10 +114385,11 @@
113577114385
#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
113578114386
#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
113579114387
#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
113580114388
#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
113581114389
#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
114390
+#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
113582114391
113583114392
/************** End of whereInt.h ********************************************/
113584114393
/************** Continuing where we left off in where.c **********************/
113585114394
113586114395
/*
@@ -113787,11 +114596,11 @@
113787114596
}
113788114597
pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
113789114598
}
113790114599
pTerm = &pWC->a[idx = pWC->nTerm++];
113791114600
if( p && ExprHasProperty(p, EP_Unlikely) ){
113792
- pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
114601
+ pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
113793114602
}else{
113794114603
pTerm->truthProb = 1;
113795114604
}
113796114605
pTerm->pExpr = sqlite3ExprSkipCollate(p);
113797114606
pTerm->wtFlags = wtFlags;
@@ -114317,10 +115126,19 @@
114317115126
if( pDerived ){
114318115127
pDerived->flags |= pBase->flags & EP_FromJoin;
114319115128
pDerived->iRightJoinTable = pBase->iRightJoinTable;
114320115129
}
114321115130
}
115131
+
115132
+/*
115133
+** Mark term iChild as being a child of term iParent
115134
+*/
115135
+static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
115136
+ pWC->a[iChild].iParent = iParent;
115137
+ pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
115138
+ pWC->a[iParent].nChild++;
115139
+}
114322115140
114323115141
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
114324115142
/*
114325115143
** Analyze a term that consists of two or more OR-connected
114326115144
** subterms. So in:
@@ -114615,12 +115433,11 @@
114615115433
pNew->x.pList = pList;
114616115434
idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
114617115435
testcase( idxNew==0 );
114618115436
exprAnalyze(pSrc, pWC, idxNew);
114619115437
pTerm = &pWC->a[idxTerm];
114620
- pWC->a[idxNew].iParent = idxTerm;
114621
- pTerm->nChild = 1;
115438
+ markTermAsChild(pWC, idxNew, idxTerm);
114622115439
}else{
114623115440
sqlite3ExprListDelete(db, pList);
114624115441
}
114625115442
pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
114626115443
}
@@ -114718,13 +115535,12 @@
114718115535
return;
114719115536
}
114720115537
idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
114721115538
if( idxNew==0 ) return;
114722115539
pNew = &pWC->a[idxNew];
114723
- pNew->iParent = idxTerm;
115540
+ markTermAsChild(pWC, idxNew, idxTerm);
114724115541
pTerm = &pWC->a[idxTerm];
114725
- pTerm->nChild = 1;
114726115542
pTerm->wtFlags |= TERM_COPIED;
114727115543
if( pExpr->op==TK_EQ
114728115544
&& !ExprHasProperty(pExpr, EP_FromJoin)
114729115545
&& OptimizationEnabled(db, SQLITE_Transitive)
114730115546
){
@@ -114777,13 +115593,12 @@
114777115593
transferJoinMarkings(pNewExpr, pExpr);
114778115594
idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
114779115595
testcase( idxNew==0 );
114780115596
exprAnalyze(pSrc, pWC, idxNew);
114781115597
pTerm = &pWC->a[idxTerm];
114782
- pWC->a[idxNew].iParent = idxTerm;
115598
+ markTermAsChild(pWC, idxNew, idxTerm);
114783115599
}
114784
- pTerm->nChild = 2;
114785115600
}
114786115601
#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
114787115602
114788115603
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
114789115604
/* Analyze a term that is composed of two or more subterms connected by
@@ -114854,13 +115669,12 @@
114854115669
idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
114855115670
testcase( idxNew2==0 );
114856115671
exprAnalyze(pSrc, pWC, idxNew2);
114857115672
pTerm = &pWC->a[idxTerm];
114858115673
if( isComplete ){
114859
- pWC->a[idxNew1].iParent = idxTerm;
114860
- pWC->a[idxNew2].iParent = idxTerm;
114861
- pTerm->nChild = 2;
115674
+ markTermAsChild(pWC, idxNew1, idxTerm);
115675
+ markTermAsChild(pWC, idxNew2, idxTerm);
114862115676
}
114863115677
}
114864115678
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
114865115679
114866115680
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -114889,13 +115703,12 @@
114889115703
pNewTerm = &pWC->a[idxNew];
114890115704
pNewTerm->prereqRight = prereqExpr;
114891115705
pNewTerm->leftCursor = pLeft->iTable;
114892115706
pNewTerm->u.leftColumn = pLeft->iColumn;
114893115707
pNewTerm->eOperator = WO_MATCH;
114894
- pNewTerm->iParent = idxTerm;
115708
+ markTermAsChild(pWC, idxNew, idxTerm);
114895115709
pTerm = &pWC->a[idxTerm];
114896
- pTerm->nChild = 1;
114897115710
pTerm->wtFlags |= TERM_COPIED;
114898115711
pNewTerm->prereqAll = pTerm->prereqAll;
114899115712
}
114900115713
}
114901115714
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -114912,11 +115725,11 @@
114912115725
** the start of the loop will prevent any results from being returned.
114913115726
*/
114914115727
if( pExpr->op==TK_NOTNULL
114915115728
&& pExpr->pLeft->op==TK_COLUMN
114916115729
&& pExpr->pLeft->iColumn>=0
114917
- && OptimizationEnabled(db, SQLITE_Stat3)
115730
+ && OptimizationEnabled(db, SQLITE_Stat34)
114918115731
){
114919115732
Expr *pNewExpr;
114920115733
Expr *pLeft = pExpr->pLeft;
114921115734
int idxNew;
114922115735
WhereTerm *pNewTerm;
@@ -114931,13 +115744,12 @@
114931115744
pNewTerm = &pWC->a[idxNew];
114932115745
pNewTerm->prereqRight = 0;
114933115746
pNewTerm->leftCursor = pLeft->iTable;
114934115747
pNewTerm->u.leftColumn = pLeft->iColumn;
114935115748
pNewTerm->eOperator = WO_GT;
114936
- pNewTerm->iParent = idxTerm;
115749
+ markTermAsChild(pWC, idxNew, idxTerm);
114937115750
pTerm = &pWC->a[idxTerm];
114938
- pTerm->nChild = 1;
114939115751
pTerm->wtFlags |= TERM_COPIED;
114940115752
pNewTerm->prereqAll = pTerm->prereqAll;
114941115753
}
114942115754
}
114943115755
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
@@ -115153,10 +115965,12 @@
115153115965
WhereLoop *pLoop; /* The Loop object */
115154115966
char *zNotUsed; /* Extra space on the end of pIdx */
115155115967
Bitmask idxCols; /* Bitmap of columns used for indexing */
115156115968
Bitmask extraCols; /* Bitmap of additional columns */
115157115969
u8 sentWarning = 0; /* True if a warnning has been issued */
115970
+ Expr *pPartial = 0; /* Partial Index Expression */
115971
+ int iContinue = 0; /* Jump here to skip excluded rows */
115158115972
115159115973
/* Generate code to skip over the creation and initialization of the
115160115974
** transient index on 2nd and subsequent iterations of the loop. */
115161115975
v = pParse->pVdbe;
115162115976
assert( v!=0 );
@@ -115168,10 +115982,16 @@
115168115982
pTable = pSrc->pTab;
115169115983
pWCEnd = &pWC->a[pWC->nTerm];
115170115984
pLoop = pLevel->pWLoop;
115171115985
idxCols = 0;
115172115986
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
115987
+ if( pLoop->prereq==0
115988
+ && (pTerm->wtFlags & TERM_VIRTUAL)==0
115989
+ && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){
115990
+ pPartial = sqlite3ExprAnd(pParse->db, pPartial,
115991
+ sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
115992
+ }
115173115993
if( termCanDriveIndex(pTerm, pSrc, notReady) ){
115174115994
int iCol = pTerm->u.leftColumn;
115175115995
Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
115176115996
testcase( iCol==BMS );
115177115997
testcase( iCol==BMS-1 );
@@ -115180,11 +116000,13 @@
115180116000
"automatic index on %s(%s)", pTable->zName,
115181116001
pTable->aCol[iCol].zName);
115182116002
sentWarning = 1;
115183116003
}
115184116004
if( (idxCols & cMask)==0 ){
115185
- if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
116005
+ if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
116006
+ goto end_auto_index_create;
116007
+ }
115186116008
pLoop->aLTerm[nKeyCol++] = pTerm;
115187116009
idxCols |= cMask;
115188116010
}
115189116011
}
115190116012
}
@@ -115200,24 +116022,23 @@
115200116022
** be a covering index because the index will not be updated if the
115201116023
** original table changes and the index and table cannot both be used
115202116024
** if they go out of sync.
115203116025
*/
115204116026
extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
115205
- mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
116027
+ mxBitCol = MIN(BMS-1,pTable->nCol);
115206116028
testcase( pTable->nCol==BMS-1 );
115207116029
testcase( pTable->nCol==BMS-2 );
115208116030
for(i=0; i<mxBitCol; i++){
115209116031
if( extraCols & MASKBIT(i) ) nKeyCol++;
115210116032
}
115211116033
if( pSrc->colUsed & MASKBIT(BMS-1) ){
115212116034
nKeyCol += pTable->nCol - BMS + 1;
115213116035
}
115214
- pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
115215116036
115216116037
/* Construct the Index object to describe this index */
115217116038
pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
115218
- if( pIdx==0 ) return;
116039
+ if( pIdx==0 ) goto end_auto_index_create;
115219116040
pLoop->u.btree.pIndex = pIdx;
115220116041
pIdx->zName = "auto-index";
115221116042
pIdx->pTable = pTable;
115222116043
n = 0;
115223116044
idxCols = 0;
@@ -115265,22 +116086,33 @@
115265116086
sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
115266116087
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
115267116088
VdbeComment((v, "for %s", pTable->zName));
115268116089
115269116090
/* Fill the automatic index with content */
116091
+ sqlite3ExprCachePush(pParse);
115270116092
addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
116093
+ if( pPartial ){
116094
+ iContinue = sqlite3VdbeMakeLabel(v);
116095
+ sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
116096
+ pLoop->wsFlags |= WHERE_PARTIALIDX;
116097
+ }
115271116098
regRecord = sqlite3GetTempReg(pParse);
115272116099
sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
115273116100
sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
115274116101
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116102
+ if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
115275116103
sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
115276116104
sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
115277116105
sqlite3VdbeJumpHere(v, addrTop);
115278116106
sqlite3ReleaseTempReg(pParse, regRecord);
116107
+ sqlite3ExprCachePop(pParse);
115279116108
115280116109
/* Jump here when skipping the initialization */
115281116110
sqlite3VdbeJumpHere(v, addrInit);
116111
+
116112
+end_auto_index_create:
116113
+ sqlite3ExprDelete(pParse->db, pPartial);
115282116114
}
115283116115
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
115284116116
115285116117
#ifndef SQLITE_OMIT_VIRTUALTABLE
115286116118
/*
@@ -115436,22 +116268,22 @@
115436116268
115437116269
return pParse->nErr;
115438116270
}
115439116271
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
115440116272
115441
-
115442116273
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
115443116274
/*
115444116275
** Estimate the location of a particular key among all keys in an
115445116276
** index. Store the results in aStat as follows:
115446116277
**
115447116278
** aStat[0] Est. number of rows less than pVal
115448116279
** aStat[1] Est. number of rows equal to pVal
115449116280
**
115450
-** Return SQLITE_OK on success.
116281
+** Return the index of the sample that is the smallest sample that
116282
+** is greater than or equal to pRec.
115451116283
*/
115452
-static void whereKeyStats(
116284
+static int whereKeyStats(
115453116285
Parse *pParse, /* Database connection */
115454116286
Index *pIdx, /* Index to consider domain of */
115455116287
UnpackedRecord *pRec, /* Vector of values to consider */
115456116288
int roundUp, /* Round up if true. Round down if false */
115457116289
tRowcnt *aStat /* OUT: stats written here */
@@ -115529,10 +116361,11 @@
115529116361
}else{
115530116362
iGap = iGap/3;
115531116363
}
115532116364
aStat[0] = iLower + iGap;
115533116365
}
116366
+ return i;
115534116367
}
115535116368
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
115536116369
115537116370
/*
115538116371
** If it is not NULL, pTerm is a term that provides an upper or lower
@@ -115679,11 +116512,11 @@
115679116512
** pLower pUpper
115680116513
**
115681116514
** If either of the upper or lower bound is not present, then NULL is passed in
115682116515
** place of the corresponding WhereTerm.
115683116516
**
115684
-** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
116517
+** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
115685116518
** column subject to the range constraint. Or, equivalently, the number of
115686116519
** equality constraints optimized by the proposed index scan. For example,
115687116520
** assuming index p is on t1(a, b), and the SQL query is:
115688116521
**
115689116522
** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
@@ -115695,11 +116528,11 @@
115695116528
**
115696116529
** then nEq is set to 0.
115697116530
**
115698116531
** When this function is called, *pnOut is set to the sqlite3LogEst() of the
115699116532
** number of rows that the index scan is expected to visit without
115700
-** considering the range constraints. If nEq is 0, this is the number of
116533
+** considering the range constraints. If nEq is 0, then *pnOut is the number of
115701116534
** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
115702116535
** to account for the range constraints pLower and pUpper.
115703116536
**
115704116537
** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
115705116538
** used, a single range inequality reduces the search space by a factor of 4.
@@ -115719,14 +116552,11 @@
115719116552
115720116553
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
115721116554
Index *p = pLoop->u.btree.pIndex;
115722116555
int nEq = pLoop->u.btree.nEq;
115723116556
115724
- if( p->nSample>0
115725
- && nEq<p->nSampleCol
115726
- && OptimizationEnabled(pParse->db, SQLITE_Stat3)
115727
- ){
116557
+ if( p->nSample>0 && nEq<p->nSampleCol ){
115728116558
if( nEq==pBuilder->nRecValid ){
115729116559
UnpackedRecord *pRec = pBuilder->pRec;
115730116560
tRowcnt a[2];
115731116561
u8 aff;
115732116562
@@ -115738,19 +116568,23 @@
115738116568
**
115739116569
** Or, if pLower is NULL or $L cannot be extracted from it (because it
115740116570
** is not a simple variable or literal value), the lower bound of the
115741116571
** range is $P. Due to a quirk in the way whereKeyStats() works, even
115742116572
** if $L is available, whereKeyStats() is called for both ($P) and
115743
- ** ($P:$L) and the larger of the two returned values used.
116573
+ ** ($P:$L) and the larger of the two returned values is used.
115744116574
**
115745116575
** Similarly, iUpper is to be set to the estimate of the number of rows
115746116576
** less than the upper bound of the range query. Where the upper bound
115747116577
** is either ($P) or ($P:$U). Again, even if $U is available, both values
115748116578
** of iUpper are requested of whereKeyStats() and the smaller used.
116579
+ **
116580
+ ** The number of rows between the two bounds is then just iUpper-iLower.
115749116581
*/
115750
- tRowcnt iLower;
115751
- tRowcnt iUpper;
116582
+ tRowcnt iLower; /* Rows less than the lower bound */
116583
+ tRowcnt iUpper; /* Rows less than the upper bound */
116584
+ int iLwrIdx = -2; /* aSample[] for the lower bound */
116585
+ int iUprIdx = -1; /* aSample[] for the upper bound */
115752116586
115753116587
if( pRec ){
115754116588
testcase( pRec->nField!=pBuilder->nRecValid );
115755116589
pRec->nField = pBuilder->nRecValid;
115756116590
}
@@ -115760,11 +116594,11 @@
115760116594
aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
115761116595
}
115762116596
/* Determine iLower and iUpper using ($P) only. */
115763116597
if( nEq==0 ){
115764116598
iLower = 0;
115765
- iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
116599
+ iUpper = p->nRowEst0;
115766116600
}else{
115767116601
/* Note: this call could be optimized away - since the same values must
115768116602
** have been requested when testing key $P in whereEqualScanEst(). */
115769116603
whereKeyStats(pParse, p, pRec, 0, a);
115770116604
iLower = a[0];
@@ -115784,11 +116618,11 @@
115784116618
int bOk; /* True if value is extracted from pExpr */
115785116619
Expr *pExpr = pLower->pExpr->pRight;
115786116620
rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
115787116621
if( rc==SQLITE_OK && bOk ){
115788116622
tRowcnt iNew;
115789
- whereKeyStats(pParse, p, pRec, 0, a);
116623
+ iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
115790116624
iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
115791116625
if( iNew>iLower ) iLower = iNew;
115792116626
nOut--;
115793116627
pLower = 0;
115794116628
}
@@ -115799,11 +116633,11 @@
115799116633
int bOk; /* True if value is extracted from pExpr */
115800116634
Expr *pExpr = pUpper->pExpr->pRight;
115801116635
rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
115802116636
if( rc==SQLITE_OK && bOk ){
115803116637
tRowcnt iNew;
115804
- whereKeyStats(pParse, p, pRec, 1, a);
116638
+ iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
115805116639
iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
115806116640
if( iNew<iUpper ) iUpper = iNew;
115807116641
nOut--;
115808116642
pUpper = 0;
115809116643
}
@@ -115811,10 +116645,15 @@
115811116645
115812116646
pBuilder->pRec = pRec;
115813116647
if( rc==SQLITE_OK ){
115814116648
if( iUpper>iLower ){
115815116649
nNew = sqlite3LogEst(iUpper - iLower);
116650
+ /* TUNING: If both iUpper and iLower are derived from the same
116651
+ ** sample, then assume they are 4x more selective. This brings
116652
+ ** the estimated selectivity more in line with what it would be
116653
+ ** if estimated without the use of STAT3/4 tables. */
116654
+ if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
115816116655
}else{
115817116656
nNew = 10; assert( 10==sqlite3LogEst(2) );
115818116657
}
115819116658
if( nNew<nOut ){
115820116659
nOut = nNew;
@@ -115835,16 +116674,19 @@
115835116674
#endif
115836116675
assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
115837116676
nNew = whereRangeAdjust(pLower, nOut);
115838116677
nNew = whereRangeAdjust(pUpper, nNew);
115839116678
115840
- /* TUNING: If there is both an upper and lower limit, assume the range is
116679
+ /* TUNING: If there is both an upper and lower limit and neither limit
116680
+ ** has an application-defined likelihood(), assume the range is
115841116681
** reduced by an additional 75%. This means that, by default, an open-ended
115842116682
** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
115843116683
** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
115844116684
** match 1/64 of the index. */
115845
- if( pLower && pUpper ) nNew -= 20;
116685
+ if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
116686
+ nNew -= 20;
116687
+ }
115846116688
115847116689
nOut -= (pLower!=0) + (pUpper!=0);
115848116690
if( nNew<10 ) nNew = 10;
115849116691
if( nNew<nOut ) nOut = nNew;
115850116692
#if defined(WHERETRACE_ENABLED)
@@ -116200,11 +117042,11 @@
116200117042
116201117043
/* This module is only called on query plans that use an index. */
116202117044
pLoop = pLevel->pWLoop;
116203117045
assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
116204117046
nEq = pLoop->u.btree.nEq;
116205
- nSkip = pLoop->u.btree.nSkip;
117047
+ nSkip = pLoop->nSkip;
116206117048
pIdx = pLoop->u.btree.pIndex;
116207117049
assert( pIdx!=0 );
116208117050
116209117051
/* Figure out how many memory cells we will need then allocate them.
116210117052
*/
@@ -116314,11 +117156,11 @@
116314117156
** "a=? AND b>?"
116315117157
*/
116316117158
static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
116317117159
Index *pIndex = pLoop->u.btree.pIndex;
116318117160
u16 nEq = pLoop->u.btree.nEq;
116319
- u16 nSkip = pLoop->u.btree.nSkip;
117161
+ u16 nSkip = pLoop->nSkip;
116320117162
int i, j;
116321117163
Column *aCol = pTab->aCol;
116322117164
i16 *aiColumn = pIndex->aiColumn;
116323117165
116324117166
if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
@@ -116345,23 +117187,27 @@
116345117187
sqlite3StrAccumAppend(pStr, ")", 1);
116346117188
}
116347117189
116348117190
/*
116349117191
** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
116350
-** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
116351
-** record is added to the output to describe the table scan strategy in
116352
-** pLevel.
117192
+** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
117193
+** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
117194
+** is added to the output to describe the table scan strategy in pLevel.
117195
+**
117196
+** If an OP_Explain opcode is added to the VM, its address is returned.
117197
+** Otherwise, if no OP_Explain is coded, zero is returned.
116353117198
*/
116354
-static void explainOneScan(
117199
+static int explainOneScan(
116355117200
Parse *pParse, /* Parse context */
116356117201
SrcList *pTabList, /* Table list this loop refers to */
116357117202
WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
116358117203
int iLevel, /* Value for "level" column of output */
116359117204
int iFrom, /* Value for "from" column of output */
116360117205
u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
116361117206
){
116362
-#ifndef SQLITE_DEBUG
117207
+ int ret = 0;
117208
+#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
116363117209
if( pParse->explain==2 )
116364117210
#endif
116365117211
{
116366117212
struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
116367117213
Vdbe *v = pParse->pVdbe; /* VM being constructed */
@@ -116374,11 +117220,11 @@
116374117220
StrAccum str; /* EQP output string */
116375117221
char zBuf[100]; /* Initial space for EQP output string */
116376117222
116377117223
pLoop = pLevel->pWLoop;
116378117224
flags = pLoop->wsFlags;
116379
- if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
117225
+ if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
116380117226
116381117227
isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
116382117228
|| ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
116383117229
|| (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
116384117230
@@ -116403,10 +117249,12 @@
116403117249
assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
116404117250
if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
116405117251
if( isSearch ){
116406117252
zFmt = "PRIMARY KEY";
116407117253
}
117254
+ }else if( flags & WHERE_PARTIALIDX ){
117255
+ zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
116408117256
}else if( flags & WHERE_AUTO_INDEX ){
116409117257
zFmt = "AUTOMATIC COVERING INDEX";
116410117258
}else if( flags & WHERE_IDX_ONLY ){
116411117259
zFmt = "COVERING INDEX %s";
116412117260
}else{
@@ -116444,16 +117292,49 @@
116444117292
}else{
116445117293
sqlite3StrAccumAppend(&str, " (~1 row)", 9);
116446117294
}
116447117295
#endif
116448117296
zMsg = sqlite3StrAccumFinish(&str);
116449
- sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
117297
+ ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
116450117298
}
117299
+ return ret;
116451117300
}
116452117301
#else
116453
-# define explainOneScan(u,v,w,x,y,z)
117302
+# define explainOneScan(u,v,w,x,y,z) 0
116454117303
#endif /* SQLITE_OMIT_EXPLAIN */
117304
+
117305
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117306
+/*
117307
+** Configure the VM passed as the first argument with an
117308
+** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
117309
+** implement level pLvl. Argument pSrclist is a pointer to the FROM
117310
+** clause that the scan reads data from.
117311
+**
117312
+** If argument addrExplain is not 0, it must be the address of an
117313
+** OP_Explain instruction that describes the same loop.
117314
+*/
117315
+static void addScanStatus(
117316
+ Vdbe *v, /* Vdbe to add scanstatus entry to */
117317
+ SrcList *pSrclist, /* FROM clause pLvl reads data from */
117318
+ WhereLevel *pLvl, /* Level to add scanstatus() entry for */
117319
+ int addrExplain /* Address of OP_Explain (or 0) */
117320
+){
117321
+ const char *zObj = 0;
117322
+ WhereLoop *pLoop = pLvl->pWLoop;
117323
+ if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
117324
+ zObj = pLoop->u.btree.pIndex->zName;
117325
+ }else{
117326
+ zObj = pSrclist->a[pLvl->iFrom].zName;
117327
+ }
117328
+ sqlite3VdbeScanStatus(
117329
+ v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
117330
+ );
117331
+}
117332
+#else
117333
+# define addScanStatus(a, b, c, d) ((void)d)
117334
+#endif
117335
+
116455117336
116456117337
116457117338
/*
116458117339
** Generate code for the start of the iLevel-th loop in the WHERE clause
116459117340
** implementation described by pWInfo.
@@ -116751,11 +117632,11 @@
116751117632
u8 bSeekPastNull = 0; /* True to seek past initial nulls */
116752117633
u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
116753117634
116754117635
pIdx = pLoop->u.btree.pIndex;
116755117636
iIdxCur = pLevel->iIdxCur;
116756
- assert( nEq>=pLoop->u.btree.nSkip );
117637
+ assert( nEq>=pLoop->nSkip );
116757117638
116758117639
/* If this loop satisfies a sort order (pOrderBy) request that
116759117640
** was passed to this function to implement a "SELECT min(x) ..."
116760117641
** query, then the caller will only allow the loop to run for
116761117642
** a single iteration. This means that the first row returned
@@ -116768,11 +117649,11 @@
116768117649
|| (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
116769117650
if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
116770117651
&& pWInfo->nOBSat>0
116771117652
&& (pIdx->nKeyCol>nEq)
116772117653
){
116773
- assert( pLoop->u.btree.nSkip==0 );
117654
+ assert( pLoop->nSkip==0 );
116774117655
bSeekPastNull = 1;
116775117656
nExtraReg = 1;
116776117657
}
116777117658
116778117659
/* Find any inequality constraint terms for the start and end
@@ -117117,13 +117998,15 @@
117117117998
pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
117118117999
wctrlFlags, iCovCur);
117119118000
assert( pSubWInfo || pParse->nErr || db->mallocFailed );
117120118001
if( pSubWInfo ){
117121118002
WhereLoop *pSubLoop;
117122
- explainOneScan(
118003
+ int addrExplain = explainOneScan(
117123118004
pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
117124118005
);
118006
+ addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
118007
+
117125118008
/* This is the sub-WHERE clause body. First skip over
117126118009
** duplicate rows from prior sub-WHERE clauses, and record the
117127118010
** rowid (or PRIMARY KEY) for the current row so that the same
117128118011
** row will be skipped in subsequent sub-WHERE clauses.
117129118012
*/
@@ -117249,10 +118132,14 @@
117249118132
VdbeCoverageIf(v, bRev==0);
117250118133
VdbeCoverageIf(v, bRev!=0);
117251118134
pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
117252118135
}
117253118136
}
118137
+
118138
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
118139
+ pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
118140
+#endif
117254118141
117255118142
/* Insert code to test every subexpression that can be completely
117256118143
** computed using the current set of tables.
117257118144
*/
117258118145
for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
@@ -117389,11 +118276,11 @@
117389118276
}
117390118277
sqlite3DebugPrintf(" %-19s", z);
117391118278
sqlite3_free(z);
117392118279
}
117393118280
if( p->wsFlags & WHERE_SKIPSCAN ){
117394
- sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
118281
+ sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
117395118282
}else{
117396118283
sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
117397118284
}
117398118285
sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
117399118286
if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
@@ -117500,34 +118387,41 @@
117500118387
sqlite3DbFree(db, pWInfo);
117501118388
}
117502118389
}
117503118390
117504118391
/*
117505
-** Return TRUE if both of the following are true:
118392
+** Return TRUE if all of the following are true:
117506118393
**
117507118394
** (1) X has the same or lower cost that Y
117508118395
** (2) X is a proper subset of Y
118396
+** (3) X skips at least as many columns as Y
117509118397
**
117510118398
** By "proper subset" we mean that X uses fewer WHERE clause terms
117511118399
** than Y and that every WHERE clause term used by X is also used
117512118400
** by Y.
117513118401
**
117514118402
** If X is a proper subset of Y then Y is a better choice and ought
117515118403
** to have a lower cost. This routine returns TRUE when that cost
117516
-** relationship is inverted and needs to be adjusted.
118404
+** relationship is inverted and needs to be adjusted. The third rule
118405
+** was added because if X uses skip-scan less than Y it still might
118406
+** deserve a lower cost even if it is a proper subset of Y.
117517118407
*/
117518118408
static int whereLoopCheaperProperSubset(
117519118409
const WhereLoop *pX, /* First WhereLoop to compare */
117520118410
const WhereLoop *pY /* Compare against this WhereLoop */
117521118411
){
117522118412
int i, j;
117523
- if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
118413
+ if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
118414
+ return 0; /* X is not a subset of Y */
118415
+ }
118416
+ if( pY->nSkip > pX->nSkip ) return 0;
117524118417
if( pX->rRun >= pY->rRun ){
117525118418
if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
117526118419
if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
117527118420
}
117528118421
for(i=pX->nLTerm-1; i>=0; i--){
118422
+ if( pX->aLTerm[i]==0 ) continue;
117529118423
for(j=pY->nLTerm-1; j>=0; j--){
117530118424
if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
117531118425
}
117532118426
if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
117533118427
}
@@ -117545,37 +118439,28 @@
117545118439
** is a proper subset.
117546118440
**
117547118441
** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
117548118442
** WHERE clause terms than Y and that every WHERE clause term used by X is
117549118443
** also used by Y.
117550
-**
117551
-** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the
117552
-** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
117553
-** clause terms covered, since some of the first nLTerm entries in aLTerm[]
117554
-** will be NULL (because they are skipped). That makes it more difficult
117555
-** to compare the loops. We could add extra code to do the comparison, and
117556
-** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this
117557
-** adjustment is sufficient minor, that it is very difficult to construct
117558
-** a test case where the extra code would improve the query plan. Better
117559
-** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
117560
-** loops.
117561118444
*/
117562118445
static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
117563118446
if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
117564
- if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
117565118447
for(; p; p=p->pNextLoop){
117566118448
if( p->iTab!=pTemplate->iTab ) continue;
117567118449
if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
117568
- if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
117569118450
if( whereLoopCheaperProperSubset(p, pTemplate) ){
117570118451
/* Adjust pTemplate cost downward so that it is cheaper than its
117571
- ** subset p */
118452
+ ** subset p. */
118453
+ WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
118454
+ pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
117572118455
pTemplate->rRun = p->rRun;
117573118456
pTemplate->nOut = p->nOut - 1;
117574118457
}else if( whereLoopCheaperProperSubset(pTemplate, p) ){
117575118458
/* Adjust pTemplate cost upward so that it is costlier than p since
117576118459
** pTemplate is a proper subset of p */
118460
+ WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
118461
+ pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
117577118462
pTemplate->rRun = p->rRun;
117578118463
pTemplate->nOut = p->nOut + 1;
117579118464
}
117580118465
}
117581118466
}
@@ -117857,11 +118742,11 @@
117857118742
int opMask; /* Valid operators for constraints */
117858118743
WhereScan scan; /* Iterator for WHERE terms */
117859118744
Bitmask saved_prereq; /* Original value of pNew->prereq */
117860118745
u16 saved_nLTerm; /* Original value of pNew->nLTerm */
117861118746
u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
117862
- u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
118747
+ u16 saved_nSkip; /* Original value of pNew->nSkip */
117863118748
u32 saved_wsFlags; /* Original value of pNew->wsFlags */
117864118749
LogEst saved_nOut; /* Original value of pNew->nOut */
117865118750
int iCol; /* Index of the column in the table */
117866118751
int rc = SQLITE_OK; /* Return code */
117867118752
LogEst rSize; /* Number of rows in the table */
@@ -117886,56 +118771,18 @@
117886118771
iCol = pProbe->aiColumn[pNew->u.btree.nEq];
117887118772
117888118773
pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
117889118774
opMask, pProbe);
117890118775
saved_nEq = pNew->u.btree.nEq;
117891
- saved_nSkip = pNew->u.btree.nSkip;
118776
+ saved_nSkip = pNew->nSkip;
117892118777
saved_nLTerm = pNew->nLTerm;
117893118778
saved_wsFlags = pNew->wsFlags;
117894118779
saved_prereq = pNew->prereq;
117895118780
saved_nOut = pNew->nOut;
117896118781
pNew->rSetup = 0;
117897118782
rSize = pProbe->aiRowLogEst[0];
117898118783
rLogSize = estLog(rSize);
117899
-
117900
- /* Consider using a skip-scan if there are no WHERE clause constraints
117901
- ** available for the left-most terms of the index, and if the average
117902
- ** number of repeats in the left-most terms is at least 18.
117903
- **
117904
- ** The magic number 18 is selected on the basis that scanning 17 rows
117905
- ** is almost always quicker than an index seek (even though if the index
117906
- ** contains fewer than 2^17 rows we assume otherwise in other parts of
117907
- ** the code). And, even if it is not, it should not be too much slower.
117908
- ** On the other hand, the extra seeks could end up being significantly
117909
- ** more expensive. */
117910
- assert( 42==sqlite3LogEst(18) );
117911
- if( saved_nEq==saved_nSkip
117912
- && saved_nEq+1<pProbe->nKeyCol
117913
- && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
117914
- && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
117915
- ){
117916
- LogEst nIter;
117917
- pNew->u.btree.nEq++;
117918
- pNew->u.btree.nSkip++;
117919
- pNew->aLTerm[pNew->nLTerm++] = 0;
117920
- pNew->wsFlags |= WHERE_SKIPSCAN;
117921
- nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
117922
- if( pTerm ){
117923
- /* TUNING: When estimating skip-scan for a term that is also indexable,
117924
- ** multiply the cost of the skip-scan by 2.0, to make it a little less
117925
- ** desirable than the regular index lookup. */
117926
- nIter += 10; assert( 10==sqlite3LogEst(2) );
117927
- }
117928
- pNew->nOut -= nIter;
117929
- /* TUNING: Because uncertainties in the estimates for skip-scan queries,
117930
- ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
117931
- nIter += 5;
117932
- whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
117933
- pNew->nOut = saved_nOut;
117934
- pNew->u.btree.nEq = saved_nEq;
117935
- pNew->u.btree.nSkip = saved_nSkip;
117936
- }
117937118784
for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
117938118785
u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
117939118786
LogEst rCostIdx;
117940118787
LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
117941118788
int nIn = 0;
@@ -118026,11 +118873,10 @@
118026118873
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
118027118874
tRowcnt nOut = 0;
118028118875
if( nInMul==0
118029118876
&& pProbe->nSample
118030118877
&& pNew->u.btree.nEq<=pProbe->nSampleCol
118031
- && OptimizationEnabled(db, SQLITE_Stat3)
118032118878
&& ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
118033118879
){
118034118880
Expr *pExpr = pTerm->pExpr;
118035118881
if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
118036118882
testcase( eOp & WO_EQ );
@@ -118094,14 +118940,48 @@
118094118940
pBuilder->nRecValid = nRecValid;
118095118941
#endif
118096118942
}
118097118943
pNew->prereq = saved_prereq;
118098118944
pNew->u.btree.nEq = saved_nEq;
118099
- pNew->u.btree.nSkip = saved_nSkip;
118945
+ pNew->nSkip = saved_nSkip;
118100118946
pNew->wsFlags = saved_wsFlags;
118101118947
pNew->nOut = saved_nOut;
118102118948
pNew->nLTerm = saved_nLTerm;
118949
+
118950
+ /* Consider using a skip-scan if there are no WHERE clause constraints
118951
+ ** available for the left-most terms of the index, and if the average
118952
+ ** number of repeats in the left-most terms is at least 18.
118953
+ **
118954
+ ** The magic number 18 is selected on the basis that scanning 17 rows
118955
+ ** is almost always quicker than an index seek (even though if the index
118956
+ ** contains fewer than 2^17 rows we assume otherwise in other parts of
118957
+ ** the code). And, even if it is not, it should not be too much slower.
118958
+ ** On the other hand, the extra seeks could end up being significantly
118959
+ ** more expensive. */
118960
+ assert( 42==sqlite3LogEst(18) );
118961
+ if( saved_nEq==saved_nSkip
118962
+ && saved_nEq+1<pProbe->nKeyCol
118963
+ && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
118964
+ && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
118965
+ ){
118966
+ LogEst nIter;
118967
+ pNew->u.btree.nEq++;
118968
+ pNew->nSkip++;
118969
+ pNew->aLTerm[pNew->nLTerm++] = 0;
118970
+ pNew->wsFlags |= WHERE_SKIPSCAN;
118971
+ nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
118972
+ pNew->nOut -= nIter;
118973
+ /* TUNING: Because uncertainties in the estimates for skip-scan queries,
118974
+ ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
118975
+ nIter += 5;
118976
+ whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
118977
+ pNew->nOut = saved_nOut;
118978
+ pNew->u.btree.nEq = saved_nEq;
118979
+ pNew->nSkip = saved_nSkip;
118980
+ pNew->wsFlags = saved_wsFlags;
118981
+ }
118982
+
118103118983
return rc;
118104118984
}
118105118985
118106118986
/*
118107118987
** Return True if it is possible that pIndex might be useful in
@@ -118276,11 +119156,11 @@
118276119156
WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
118277119157
for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
118278119158
if( pTerm->prereqRight & pNew->maskSelf ) continue;
118279119159
if( termCanDriveIndex(pTerm, pSrc, 0) ){
118280119160
pNew->u.btree.nEq = 1;
118281
- pNew->u.btree.nSkip = 0;
119161
+ pNew->nSkip = 0;
118282119162
pNew->u.btree.pIndex = 0;
118283119163
pNew->nLTerm = 1;
118284119164
pNew->aLTerm[0] = pTerm;
118285119165
/* TUNING: One-time cost for computing the automatic index is
118286119166
** estimated to be X*N*log2(N) where N is the number of rows in
@@ -118317,11 +119197,11 @@
118317119197
testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
118318119198
continue; /* Partial index inappropriate for this query */
118319119199
}
118320119200
rSize = pProbe->aiRowLogEst[0];
118321119201
pNew->u.btree.nEq = 0;
118322
- pNew->u.btree.nSkip = 0;
119202
+ pNew->nSkip = 0;
118323119203
pNew->nLTerm = 0;
118324119204
pNew->iSortIdx = 0;
118325119205
pNew->rSetup = 0;
118326119206
pNew->prereq = mExtra;
118327119207
pNew->nOut = rSize;
@@ -118867,11 +119747,11 @@
118867119747
for(j=0; j<nColumn; j++){
118868119748
u8 bOnce; /* True to run the ORDER BY search loop */
118869119749
118870119750
/* Skip over == and IS NULL terms */
118871119751
if( j<pLoop->u.btree.nEq
118872
- && pLoop->u.btree.nSkip==0
119752
+ && pLoop->nSkip==0
118873119753
&& ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
118874119754
){
118875119755
if( i & WO_ISNULL ){
118876119756
testcase( isOrderDistinct );
118877119757
isOrderDistinct = 0;
@@ -119321,11 +120201,11 @@
119321120201
}
119322120202
}
119323120203
}
119324120204
119325120205
#ifdef WHERETRACE_ENABLED /* >=2 */
119326
- if( sqlite3WhereTrace>=2 ){
120206
+ if( sqlite3WhereTrace & 0x02 ){
119327120207
sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
119328120208
for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
119329120209
sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
119330120210
wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
119331120211
pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
@@ -119440,11 +120320,11 @@
119440120320
if( pItem->zIndex ) return 0;
119441120321
iCur = pItem->iCursor;
119442120322
pWC = &pWInfo->sWC;
119443120323
pLoop = pBuilder->pNew;
119444120324
pLoop->wsFlags = 0;
119445
- pLoop->u.btree.nSkip = 0;
120325
+ pLoop->nSkip = 0;
119446120326
pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
119447120327
if( pTerm ){
119448120328
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
119449120329
pLoop->aLTerm[0] = pTerm;
119450120330
pLoop->nLTerm = 1;
@@ -119452,11 +120332,10 @@
119452120332
/* TUNING: Cost of a rowid lookup is 10 */
119453120333
pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
119454120334
}else{
119455120335
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119456120336
assert( pLoop->aLTermSpace==pLoop->aLTerm );
119457
- assert( ArraySize(pLoop->aLTermSpace)==4 );
119458120337
if( !IsUniqueIndex(pIdx)
119459120338
|| pIdx->pPartIdxWhere!=0
119460120339
|| pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
119461120340
) continue;
119462120341
for(j=0; j<pIdx->nKeyCol; j++){
@@ -119961,22 +120840,30 @@
119961120840
** loop below generates code for a single nested loop of the VM
119962120841
** program.
119963120842
*/
119964120843
notReady = ~(Bitmask)0;
119965120844
for(ii=0; ii<nTabList; ii++){
120845
+ int addrExplain;
120846
+ int wsFlags;
119966120847
pLevel = &pWInfo->a[ii];
120848
+ wsFlags = pLevel->pWLoop->wsFlags;
119967120849
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
119968120850
if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
119969120851
constructAutomaticIndex(pParse, &pWInfo->sWC,
119970120852
&pTabList->a[pLevel->iFrom], notReady, pLevel);
119971120853
if( db->mallocFailed ) goto whereBeginError;
119972120854
}
119973120855
#endif
119974
- explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
120856
+ addrExplain = explainOneScan(
120857
+ pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
120858
+ );
119975120859
pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
119976120860
notReady = codeOneLoopStart(pWInfo, ii, notReady);
119977120861
pWInfo->iContinue = pLevel->addrCont;
120862
+ if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
120863
+ addScanStatus(v, pTabList, pLevel, addrExplain);
120864
+ }
119978120865
}
119979120866
119980120867
/* Done. */
119981120868
VdbeModuleComment((v, "Begin WHERE-core"));
119982120869
return pWInfo;
@@ -124640,10 +125527,17 @@
124640125527
** is look for a semicolon that is not part of an string or comment.
124641125528
*/
124642125529
SQLITE_API int sqlite3_complete(const char *zSql){
124643125530
u8 state = 0; /* Current state, using numbers defined in header comment */
124644125531
u8 token; /* Value of the next token */
125532
+
125533
+#ifdef SQLITE_ENABLE_API_ARMOR
125534
+ if( zSql==0 ){
125535
+ (void)SQLITE_MISUSE_BKPT;
125536
+ return 0;
125537
+ }
125538
+#endif
124645125539
124646125540
#ifndef SQLITE_OMIT_TRIGGER
124647125541
/* A complex statement machine used to detect the end of a CREATE TRIGGER
124648125542
** statement. This is the normal case.
124649125543
*/
@@ -125238,74 +126132,106 @@
125238126132
125239126133
va_start(ap, op);
125240126134
switch( op ){
125241126135
125242126136
/* Mutex configuration options are only available in a threadsafe
125243
- ** compile.
126137
+ ** compile.
125244126138
*/
125245
-#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
126139
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
125246126140
case SQLITE_CONFIG_SINGLETHREAD: {
125247126141
/* Disable all mutexing */
125248126142
sqlite3GlobalConfig.bCoreMutex = 0;
125249126143
sqlite3GlobalConfig.bFullMutex = 0;
125250126144
break;
125251126145
}
126146
+#endif
126147
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
125252126148
case SQLITE_CONFIG_MULTITHREAD: {
125253126149
/* Disable mutexing of database connections */
125254126150
/* Enable mutexing of core data structures */
125255126151
sqlite3GlobalConfig.bCoreMutex = 1;
125256126152
sqlite3GlobalConfig.bFullMutex = 0;
125257126153
break;
125258126154
}
126155
+#endif
126156
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
125259126157
case SQLITE_CONFIG_SERIALIZED: {
125260126158
/* Enable all mutexing */
125261126159
sqlite3GlobalConfig.bCoreMutex = 1;
125262126160
sqlite3GlobalConfig.bFullMutex = 1;
125263126161
break;
125264126162
}
126163
+#endif
126164
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
125265126165
case SQLITE_CONFIG_MUTEX: {
125266126166
/* Specify an alternative mutex implementation */
125267126167
sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
125268126168
break;
125269126169
}
126170
+#endif
126171
+#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
125270126172
case SQLITE_CONFIG_GETMUTEX: {
125271126173
/* Retrieve the current mutex implementation */
125272126174
*va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
125273126175
break;
125274126176
}
125275126177
#endif
125276126178
125277
-
125278126179
case SQLITE_CONFIG_MALLOC: {
125279
- /* Specify an alternative malloc implementation */
126180
+ /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
126181
+ ** single argument which is a pointer to an instance of the
126182
+ ** sqlite3_mem_methods structure. The argument specifies alternative
126183
+ ** low-level memory allocation routines to be used in place of the memory
126184
+ ** allocation routines built into SQLite. */
125280126185
sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
125281126186
break;
125282126187
}
125283126188
case SQLITE_CONFIG_GETMALLOC: {
125284
- /* Retrieve the current malloc() implementation */
126189
+ /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
126190
+ ** single argument which is a pointer to an instance of the
126191
+ ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
126192
+ ** filled with the currently defined memory allocation routines. */
125285126193
if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
125286126194
*va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
125287126195
break;
125288126196
}
125289126197
case SQLITE_CONFIG_MEMSTATUS: {
125290
- /* Enable or disable the malloc status collection */
126198
+ /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
126199
+ ** single argument of type int, interpreted as a boolean, which enables
126200
+ ** or disables the collection of memory allocation statistics. */
125291126201
sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
125292126202
break;
125293126203
}
125294126204
case SQLITE_CONFIG_SCRATCH: {
125295
- /* Designate a buffer for scratch memory space */
126205
+ /* EVIDENCE-OF: R-08404-60887 There are three arguments to
126206
+ ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
126207
+ ** which the scratch allocations will be drawn, the size of each scratch
126208
+ ** allocation (sz), and the maximum number of scratch allocations (N). */
125296126209
sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
125297126210
sqlite3GlobalConfig.szScratch = va_arg(ap, int);
125298126211
sqlite3GlobalConfig.nScratch = va_arg(ap, int);
125299126212
break;
125300126213
}
125301126214
case SQLITE_CONFIG_PAGECACHE: {
125302
- /* Designate a buffer for page cache memory space */
126215
+ /* EVIDENCE-OF: R-31408-40510 There are three arguments to
126216
+ ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
126217
+ ** of each page buffer (sz), and the number of pages (N). */
125303126218
sqlite3GlobalConfig.pPage = va_arg(ap, void*);
125304126219
sqlite3GlobalConfig.szPage = va_arg(ap, int);
125305126220
sqlite3GlobalConfig.nPage = va_arg(ap, int);
125306126221
break;
126222
+ }
126223
+ case SQLITE_CONFIG_PCACHE_HDRSZ: {
126224
+ /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
126225
+ ** a single parameter which is a pointer to an integer and writes into
126226
+ ** that integer the number of extra bytes per page required for each page
126227
+ ** in SQLITE_CONFIG_PAGECACHE. */
126228
+ *va_arg(ap, int*) =
126229
+ sqlite3HeaderSizeBtree() +
126230
+ sqlite3HeaderSizePcache() +
126231
+ sqlite3HeaderSizePcache1();
126232
+ break;
125307126233
}
125308126234
125309126235
case SQLITE_CONFIG_PCACHE: {
125310126236
/* no-op */
125311126237
break;
@@ -125315,25 +126241,37 @@
125315126241
rc = SQLITE_ERROR;
125316126242
break;
125317126243
}
125318126244
125319126245
case SQLITE_CONFIG_PCACHE2: {
125320
- /* Specify an alternative page cache implementation */
126246
+ /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
126247
+ ** single argument which is a pointer to an sqlite3_pcache_methods2
126248
+ ** object. This object specifies the interface to a custom page cache
126249
+ ** implementation. */
125321126250
sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
125322126251
break;
125323126252
}
125324126253
case SQLITE_CONFIG_GETPCACHE2: {
126254
+ /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
126255
+ ** single argument which is a pointer to an sqlite3_pcache_methods2
126256
+ ** object. SQLite copies of the current page cache implementation into
126257
+ ** that object. */
125325126258
if( sqlite3GlobalConfig.pcache2.xInit==0 ){
125326126259
sqlite3PCacheSetDefault();
125327126260
}
125328126261
*va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
125329126262
break;
125330126263
}
125331126264
126265
+/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
126266
+** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
126267
+** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
125332126268
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
125333126269
case SQLITE_CONFIG_HEAP: {
125334
- /* Designate a buffer for heap memory space */
126270
+ /* EVIDENCE-OF: R-19854-42126 There are three arguments to
126271
+ ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
126272
+ ** number of bytes in the memory buffer, and the minimum allocation size. */
125335126273
sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
125336126274
sqlite3GlobalConfig.nHeap = va_arg(ap, int);
125337126275
sqlite3GlobalConfig.mnReq = va_arg(ap, int);
125338126276
125339126277
if( sqlite3GlobalConfig.mnReq<1 ){
@@ -125342,21 +126280,23 @@
125342126280
/* cap min request size at 2^12 */
125343126281
sqlite3GlobalConfig.mnReq = (1<<12);
125344126282
}
125345126283
125346126284
if( sqlite3GlobalConfig.pHeap==0 ){
125347
- /* If the heap pointer is NULL, then restore the malloc implementation
125348
- ** back to NULL pointers too. This will cause the malloc to go
125349
- ** back to its default implementation when sqlite3_initialize() is
125350
- ** run.
126285
+ /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
126286
+ ** is NULL, then SQLite reverts to using its default memory allocator
126287
+ ** (the system malloc() implementation), undoing any prior invocation of
126288
+ ** SQLITE_CONFIG_MALLOC.
126289
+ **
126290
+ ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
126291
+ ** revert to its default implementation when sqlite3_initialize() is run
125351126292
*/
125352126293
memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
125353126294
}else{
125354
- /* The heap pointer is not NULL, then install one of the
125355
- ** mem5.c/mem3.c methods. The enclosing #if guarantees at
125356
- ** least one of these methods is currently enabled.
125357
- */
126295
+ /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
126296
+ ** alternative memory allocator is engaged to handle all of SQLites
126297
+ ** memory allocation needs. */
125358126298
#ifdef SQLITE_ENABLE_MEMSYS3
125359126299
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
125360126300
#endif
125361126301
#ifdef SQLITE_ENABLE_MEMSYS5
125362126302
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
@@ -125391,15 +126331,23 @@
125391126331
** can be changed at start-time using the
125392126332
** sqlite3_config(SQLITE_CONFIG_URI,1) or
125393126333
** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
125394126334
*/
125395126335
case SQLITE_CONFIG_URI: {
126336
+ /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
126337
+ ** argument of type int. If non-zero, then URI handling is globally
126338
+ ** enabled. If the parameter is zero, then URI handling is globally
126339
+ ** disabled. */
125396126340
sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
125397126341
break;
125398126342
}
125399126343
125400126344
case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
126345
+ /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
126346
+ ** option takes a single integer argument which is interpreted as a
126347
+ ** boolean in order to enable or disable the use of covering indices for
126348
+ ** full table scans in the query optimizer. */
125401126349
sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
125402126350
break;
125403126351
}
125404126352
125405126353
#ifdef SQLITE_ENABLE_SQLLOG
@@ -125410,24 +126358,37 @@
125410126358
break;
125411126359
}
125412126360
#endif
125413126361
125414126362
case SQLITE_CONFIG_MMAP_SIZE: {
126363
+ /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
126364
+ ** integer (sqlite3_int64) values that are the default mmap size limit
126365
+ ** (the default setting for PRAGMA mmap_size) and the maximum allowed
126366
+ ** mmap size limit. */
125415126367
sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
125416126368
sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
125417
- if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
125418
- mxMmap = SQLITE_MAX_MMAP_SIZE;
125419
- }
125420
- sqlite3GlobalConfig.mxMmap = mxMmap;
126369
+ /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
126370
+ ** negative, then that argument is changed to its compile-time default.
126371
+ **
126372
+ ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
126373
+ ** silently truncated if necessary so that it does not exceed the
126374
+ ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
126375
+ ** compile-time option.
126376
+ */
126377
+ if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE;
125421126378
if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
125422126379
if( szMmap>mxMmap) szMmap = mxMmap;
126380
+ sqlite3GlobalConfig.mxMmap = mxMmap;
125423126381
sqlite3GlobalConfig.szMmap = szMmap;
125424126382
break;
125425126383
}
125426126384
125427
-#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
126385
+#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
125428126386
case SQLITE_CONFIG_WIN32_HEAPSIZE: {
126387
+ /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
126388
+ ** unsigned integer value that specifies the maximum size of the created
126389
+ ** heap. */
125429126390
sqlite3GlobalConfig.nHeap = va_arg(ap, int);
125430126391
break;
125431126392
}
125432126393
#endif
125433126394
@@ -125507,19 +126468,29 @@
125507126468
125508126469
/*
125509126470
** Return the mutex associated with a database connection.
125510126471
*/
125511126472
SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
126473
+#ifdef SQLITE_ENABLE_API_ARMOR
126474
+ if( !sqlite3SafetyCheckOk(db) ){
126475
+ (void)SQLITE_MISUSE_BKPT;
126476
+ return 0;
126477
+ }
126478
+#endif
125512126479
return db->mutex;
125513126480
}
125514126481
125515126482
/*
125516126483
** Free up as much memory as we can from the given database
125517126484
** connection.
125518126485
*/
125519126486
SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
125520126487
int i;
126488
+
126489
+#ifdef SQLITE_ENABLE_API_ARMOR
126490
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
126491
+#endif
125521126492
sqlite3_mutex_enter(db->mutex);
125522126493
sqlite3BtreeEnterAll(db);
125523126494
for(i=0; i<db->nDb; i++){
125524126495
Btree *pBt = db->aDb[i].pBt;
125525126496
if( pBt ){
@@ -125646,24 +126617,42 @@
125646126617
125647126618
/*
125648126619
** Return the ROWID of the most recent insert
125649126620
*/
125650126621
SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
126622
+#ifdef SQLITE_ENABLE_API_ARMOR
126623
+ if( !sqlite3SafetyCheckOk(db) ){
126624
+ (void)SQLITE_MISUSE_BKPT;
126625
+ return 0;
126626
+ }
126627
+#endif
125651126628
return db->lastRowid;
125652126629
}
125653126630
125654126631
/*
125655126632
** Return the number of changes in the most recent call to sqlite3_exec().
125656126633
*/
125657126634
SQLITE_API int sqlite3_changes(sqlite3 *db){
126635
+#ifdef SQLITE_ENABLE_API_ARMOR
126636
+ if( !sqlite3SafetyCheckOk(db) ){
126637
+ (void)SQLITE_MISUSE_BKPT;
126638
+ return 0;
126639
+ }
126640
+#endif
125658126641
return db->nChange;
125659126642
}
125660126643
125661126644
/*
125662126645
** Return the number of changes since the database handle was opened.
125663126646
*/
125664126647
SQLITE_API int sqlite3_total_changes(sqlite3 *db){
126648
+#ifdef SQLITE_ENABLE_API_ARMOR
126649
+ if( !sqlite3SafetyCheckOk(db) ){
126650
+ (void)SQLITE_MISUSE_BKPT;
126651
+ return 0;
126652
+ }
126653
+#endif
125665126654
return db->nTotalChange;
125666126655
}
125667126656
125668126657
/*
125669126658
** Close all open savepoints. This function only manipulates fields of the
@@ -125925,17 +126914,19 @@
125925126914
sqlite3_free(db);
125926126915
}
125927126916
125928126917
/*
125929126918
** Rollback all database files. If tripCode is not SQLITE_OK, then
125930
-** any open cursors are invalidated ("tripped" - as in "tripping a circuit
126919
+** any write cursors are invalidated ("tripped" - as in "tripping a circuit
125931126920
** breaker") and made to return tripCode if there are any further
125932
-** attempts to use that cursor.
126921
+** attempts to use that cursor. Read cursors remain open and valid
126922
+** but are "saved" in case the table pages are moved around.
125933126923
*/
125934126924
SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
125935126925
int i;
125936126926
int inTrans = 0;
126927
+ int schemaChange;
125937126928
assert( sqlite3_mutex_held(db->mutex) );
125938126929
sqlite3BeginBenignMalloc();
125939126930
125940126931
/* Obtain all b-tree mutexes before making any calls to BtreeRollback().
125941126932
** This is important in case the transaction being rolled back has
@@ -125942,18 +126933,19 @@
125942126933
** modified the database schema. If the b-tree mutexes are not taken
125943126934
** here, then another shared-cache connection might sneak in between
125944126935
** the database rollback and schema reset, which can cause false
125945126936
** corruption reports in some cases. */
125946126937
sqlite3BtreeEnterAll(db);
126938
+ schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
125947126939
125948126940
for(i=0; i<db->nDb; i++){
125949126941
Btree *p = db->aDb[i].pBt;
125950126942
if( p ){
125951126943
if( sqlite3BtreeIsInTrans(p) ){
125952126944
inTrans = 1;
125953126945
}
125954
- sqlite3BtreeRollback(p, tripCode);
126946
+ sqlite3BtreeRollback(p, tripCode, !schemaChange);
125955126947
}
125956126948
}
125957126949
sqlite3VtabRollback(db);
125958126950
sqlite3EndBenignMalloc();
125959126951
@@ -126205,10 +127197,13 @@
126205127197
SQLITE_API int sqlite3_busy_handler(
126206127198
sqlite3 *db,
126207127199
int (*xBusy)(void*,int),
126208127200
void *pArg
126209127201
){
127202
+#ifdef SQLITE_ENABLE_API_ARMOR
127203
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE;
127204
+#endif
126210127205
sqlite3_mutex_enter(db->mutex);
126211127206
db->busyHandler.xFunc = xBusy;
126212127207
db->busyHandler.pArg = pArg;
126213127208
db->busyHandler.nBusy = 0;
126214127209
db->busyTimeout = 0;
@@ -126226,10 +127221,16 @@
126226127221
sqlite3 *db,
126227127222
int nOps,
126228127223
int (*xProgress)(void*),
126229127224
void *pArg
126230127225
){
127226
+#ifdef SQLITE_ENABLE_API_ARMOR
127227
+ if( !sqlite3SafetyCheckOk(db) ){
127228
+ (void)SQLITE_MISUSE_BKPT;
127229
+ return;
127230
+ }
127231
+#endif
126231127232
sqlite3_mutex_enter(db->mutex);
126232127233
if( nOps>0 ){
126233127234
db->xProgress = xProgress;
126234127235
db->nProgressOps = (unsigned)nOps;
126235127236
db->pProgressArg = pArg;
@@ -126246,10 +127247,13 @@
126246127247
/*
126247127248
** This routine installs a default busy handler that waits for the
126248127249
** specified number of milliseconds before returning 0.
126249127250
*/
126250127251
SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
127252
+#ifdef SQLITE_ENABLE_API_ARMOR
127253
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127254
+#endif
126251127255
if( ms>0 ){
126252127256
sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
126253127257
db->busyTimeout = ms;
126254127258
}else{
126255127259
sqlite3_busy_handler(db, 0, 0);
@@ -126259,10 +127263,16 @@
126259127263
126260127264
/*
126261127265
** Cause any pending operation to stop at its earliest opportunity.
126262127266
*/
126263127267
SQLITE_API void sqlite3_interrupt(sqlite3 *db){
127268
+#ifdef SQLITE_ENABLE_API_ARMOR
127269
+ if( !sqlite3SafetyCheckOk(db) ){
127270
+ (void)SQLITE_MISUSE_BKPT;
127271
+ return;
127272
+ }
127273
+#endif
126264127274
db->u1.isInterrupted = 1;
126265127275
}
126266127276
126267127277
126268127278
/*
@@ -126396,10 +127406,16 @@
126396127406
void (*xFinal)(sqlite3_context*),
126397127407
void (*xDestroy)(void *)
126398127408
){
126399127409
int rc = SQLITE_ERROR;
126400127410
FuncDestructor *pArg = 0;
127411
+
127412
+#ifdef SQLITE_ENABLE_API_ARMOR
127413
+ if( !sqlite3SafetyCheckOk(db) ){
127414
+ return SQLITE_MISUSE_BKPT;
127415
+ }
127416
+#endif
126401127417
sqlite3_mutex_enter(db->mutex);
126402127418
if( xDestroy ){
126403127419
pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
126404127420
if( !pArg ){
126405127421
xDestroy(p);
@@ -126432,10 +127448,14 @@
126432127448
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
126433127449
void (*xFinal)(sqlite3_context*)
126434127450
){
126435127451
int rc;
126436127452
char *zFunc8;
127453
+
127454
+#ifdef SQLITE_ENABLE_API_ARMOR
127455
+ if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
127456
+#endif
126437127457
sqlite3_mutex_enter(db->mutex);
126438127458
assert( !db->mallocFailed );
126439127459
zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
126440127460
rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
126441127461
sqlite3DbFree(db, zFunc8);
@@ -126463,10 +127483,16 @@
126463127483
const char *zName,
126464127484
int nArg
126465127485
){
126466127486
int nName = sqlite3Strlen30(zName);
126467127487
int rc = SQLITE_OK;
127488
+
127489
+#ifdef SQLITE_ENABLE_API_ARMOR
127490
+ if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
127491
+ return SQLITE_MISUSE_BKPT;
127492
+ }
127493
+#endif
126468127494
sqlite3_mutex_enter(db->mutex);
126469127495
if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
126470127496
rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
126471127497
0, sqlite3InvalidFunction, 0, 0, 0);
126472127498
}
@@ -126484,10 +127510,17 @@
126484127510
** trace is a pointer to a function that is invoked at the start of each
126485127511
** SQL statement.
126486127512
*/
126487127513
SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
126488127514
void *pOld;
127515
+
127516
+#ifdef SQLITE_ENABLE_API_ARMOR
127517
+ if( !sqlite3SafetyCheckOk(db) ){
127518
+ (void)SQLITE_MISUSE_BKPT;
127519
+ return 0;
127520
+ }
127521
+#endif
126489127522
sqlite3_mutex_enter(db->mutex);
126490127523
pOld = db->pTraceArg;
126491127524
db->xTrace = xTrace;
126492127525
db->pTraceArg = pArg;
126493127526
sqlite3_mutex_leave(db->mutex);
@@ -126505,10 +127538,17 @@
126505127538
sqlite3 *db,
126506127539
void (*xProfile)(void*,const char*,sqlite_uint64),
126507127540
void *pArg
126508127541
){
126509127542
void *pOld;
127543
+
127544
+#ifdef SQLITE_ENABLE_API_ARMOR
127545
+ if( !sqlite3SafetyCheckOk(db) ){
127546
+ (void)SQLITE_MISUSE_BKPT;
127547
+ return 0;
127548
+ }
127549
+#endif
126510127550
sqlite3_mutex_enter(db->mutex);
126511127551
pOld = db->pProfileArg;
126512127552
db->xProfile = xProfile;
126513127553
db->pProfileArg = pArg;
126514127554
sqlite3_mutex_leave(db->mutex);
@@ -126525,10 +127565,17 @@
126525127565
sqlite3 *db, /* Attach the hook to this database */
126526127566
int (*xCallback)(void*), /* Function to invoke on each commit */
126527127567
void *pArg /* Argument to the function */
126528127568
){
126529127569
void *pOld;
127570
+
127571
+#ifdef SQLITE_ENABLE_API_ARMOR
127572
+ if( !sqlite3SafetyCheckOk(db) ){
127573
+ (void)SQLITE_MISUSE_BKPT;
127574
+ return 0;
127575
+ }
127576
+#endif
126530127577
sqlite3_mutex_enter(db->mutex);
126531127578
pOld = db->pCommitArg;
126532127579
db->xCommitCallback = xCallback;
126533127580
db->pCommitArg = pArg;
126534127581
sqlite3_mutex_leave(db->mutex);
@@ -126543,10 +127590,17 @@
126543127590
sqlite3 *db, /* Attach the hook to this database */
126544127591
void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
126545127592
void *pArg /* Argument to the function */
126546127593
){
126547127594
void *pRet;
127595
+
127596
+#ifdef SQLITE_ENABLE_API_ARMOR
127597
+ if( !sqlite3SafetyCheckOk(db) ){
127598
+ (void)SQLITE_MISUSE_BKPT;
127599
+ return 0;
127600
+ }
127601
+#endif
126548127602
sqlite3_mutex_enter(db->mutex);
126549127603
pRet = db->pUpdateArg;
126550127604
db->xUpdateCallback = xCallback;
126551127605
db->pUpdateArg = pArg;
126552127606
sqlite3_mutex_leave(db->mutex);
@@ -126561,10 +127615,17 @@
126561127615
sqlite3 *db, /* Attach the hook to this database */
126562127616
void (*xCallback)(void*), /* Callback function */
126563127617
void *pArg /* Argument to the function */
126564127618
){
126565127619
void *pRet;
127620
+
127621
+#ifdef SQLITE_ENABLE_API_ARMOR
127622
+ if( !sqlite3SafetyCheckOk(db) ){
127623
+ (void)SQLITE_MISUSE_BKPT;
127624
+ return 0;
127625
+ }
127626
+#endif
126566127627
sqlite3_mutex_enter(db->mutex);
126567127628
pRet = db->pRollbackArg;
126568127629
db->xRollbackCallback = xCallback;
126569127630
db->pRollbackArg = pArg;
126570127631
sqlite3_mutex_leave(db->mutex);
@@ -126607,10 +127668,13 @@
126607127668
SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
126608127669
#ifdef SQLITE_OMIT_WAL
126609127670
UNUSED_PARAMETER(db);
126610127671
UNUSED_PARAMETER(nFrame);
126611127672
#else
127673
+#ifdef SQLITE_ENABLE_API_ARMOR
127674
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127675
+#endif
126612127676
if( nFrame>0 ){
126613127677
sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
126614127678
}else{
126615127679
sqlite3_wal_hook(db, 0, 0);
126616127680
}
@@ -126627,10 +127691,16 @@
126627127691
int(*xCallback)(void *, sqlite3*, const char*, int),
126628127692
void *pArg /* First argument passed to xCallback() */
126629127693
){
126630127694
#ifndef SQLITE_OMIT_WAL
126631127695
void *pRet;
127696
+#ifdef SQLITE_ENABLE_API_ARMOR
127697
+ if( !sqlite3SafetyCheckOk(db) ){
127698
+ (void)SQLITE_MISUSE_BKPT;
127699
+ return 0;
127700
+ }
127701
+#endif
126632127702
sqlite3_mutex_enter(db->mutex);
126633127703
pRet = db->pWalArg;
126634127704
db->xWalCallback = xCallback;
126635127705
db->pWalArg = pArg;
126636127706
sqlite3_mutex_leave(db->mutex);
@@ -126653,10 +127723,14 @@
126653127723
#ifdef SQLITE_OMIT_WAL
126654127724
return SQLITE_OK;
126655127725
#else
126656127726
int rc; /* Return code */
126657127727
int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
127728
+
127729
+#ifdef SQLITE_ENABLE_API_ARMOR
127730
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127731
+#endif
126658127732
126659127733
/* Initialize the output variables to -1 in case an error occurs. */
126660127734
if( pnLog ) *pnLog = -1;
126661127735
if( pnCkpt ) *pnCkpt = -1;
126662127736
@@ -127050,10 +128124,16 @@
127050128124
** from forming.
127051128125
*/
127052128126
SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
127053128127
int oldLimit;
127054128128
128129
+#ifdef SQLITE_ENABLE_API_ARMOR
128130
+ if( !sqlite3SafetyCheckOk(db) ){
128131
+ (void)SQLITE_MISUSE_BKPT;
128132
+ return -1;
128133
+ }
128134
+#endif
127055128135
127056128136
/* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
127057128137
** there is a hard upper bound set at compile-time by a C preprocessor
127058128138
** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
127059128139
** "_MAX_".)
@@ -127126,11 +128206,12 @@
127126128206
char c;
127127128207
int nUri = sqlite3Strlen30(zUri);
127128128208
127129128209
assert( *pzErrMsg==0 );
127130128210
127131
- if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
128211
+ if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
128212
+ || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
127132128213
&& nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
127133128214
){
127134128215
char *zOpt;
127135128216
int eState; /* Parser state when parsing URI */
127136128217
int iIn; /* Input character index */
@@ -127335,10 +128416,13 @@
127335128416
int rc; /* Return code */
127336128417
int isThreadsafe; /* True for threadsafe connections */
127337128418
char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
127338128419
char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
127339128420
128421
+#ifdef SQLITE_ENABLE_API_ARMOR
128422
+ if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
128423
+#endif
127340128424
*ppDb = 0;
127341128425
#ifndef SQLITE_OMIT_AUTOINIT
127342128426
rc = sqlite3_initialize();
127343128427
if( rc ) return rc;
127344128428
#endif
@@ -127624,17 +128708,19 @@
127624128708
){
127625128709
char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
127626128710
sqlite3_value *pVal;
127627128711
int rc;
127628128712
127629
- assert( zFilename );
127630
- assert( ppDb );
128713
+#ifdef SQLITE_ENABLE_API_ARMOR
128714
+ if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
128715
+#endif
127631128716
*ppDb = 0;
127632128717
#ifndef SQLITE_OMIT_AUTOINIT
127633128718
rc = sqlite3_initialize();
127634128719
if( rc ) return rc;
127635128720
#endif
128721
+ if( zFilename==0 ) zFilename = "\000\000";
127636128722
pVal = sqlite3ValueNew(0);
127637128723
sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
127638128724
zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
127639128725
if( zFilename8 ){
127640128726
rc = openDatabase(zFilename8, ppDb,
@@ -127660,17 +128746,11 @@
127660128746
const char *zName,
127661128747
int enc,
127662128748
void* pCtx,
127663128749
int(*xCompare)(void*,int,const void*,int,const void*)
127664128750
){
127665
- int rc;
127666
- sqlite3_mutex_enter(db->mutex);
127667
- assert( !db->mallocFailed );
127668
- rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
127669
- rc = sqlite3ApiExit(db, rc);
127670
- sqlite3_mutex_leave(db->mutex);
127671
- return rc;
128751
+ return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
127672128752
}
127673128753
127674128754
/*
127675128755
** Register a new collation sequence with the database handle db.
127676128756
*/
@@ -127681,10 +128761,14 @@
127681128761
void* pCtx,
127682128762
int(*xCompare)(void*,int,const void*,int,const void*),
127683128763
void(*xDel)(void*)
127684128764
){
127685128765
int rc;
128766
+
128767
+#ifdef SQLITE_ENABLE_API_ARMOR
128768
+ if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
128769
+#endif
127686128770
sqlite3_mutex_enter(db->mutex);
127687128771
assert( !db->mallocFailed );
127688128772
rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
127689128773
rc = sqlite3ApiExit(db, rc);
127690128774
sqlite3_mutex_leave(db->mutex);
@@ -127702,10 +128786,14 @@
127702128786
void* pCtx,
127703128787
int(*xCompare)(void*,int,const void*,int,const void*)
127704128788
){
127705128789
int rc = SQLITE_OK;
127706128790
char *zName8;
128791
+
128792
+#ifdef SQLITE_ENABLE_API_ARMOR
128793
+ if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
128794
+#endif
127707128795
sqlite3_mutex_enter(db->mutex);
127708128796
assert( !db->mallocFailed );
127709128797
zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
127710128798
if( zName8 ){
127711128799
rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
@@ -127724,10 +128812,13 @@
127724128812
SQLITE_API int sqlite3_collation_needed(
127725128813
sqlite3 *db,
127726128814
void *pCollNeededArg,
127727128815
void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
127728128816
){
128817
+#ifdef SQLITE_ENABLE_API_ARMOR
128818
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
128819
+#endif
127729128820
sqlite3_mutex_enter(db->mutex);
127730128821
db->xCollNeeded = xCollNeeded;
127731128822
db->xCollNeeded16 = 0;
127732128823
db->pCollNeededArg = pCollNeededArg;
127733128824
sqlite3_mutex_leave(db->mutex);
@@ -127742,10 +128833,13 @@
127742128833
SQLITE_API int sqlite3_collation_needed16(
127743128834
sqlite3 *db,
127744128835
void *pCollNeededArg,
127745128836
void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
127746128837
){
128838
+#ifdef SQLITE_ENABLE_API_ARMOR
128839
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
128840
+#endif
127747128841
sqlite3_mutex_enter(db->mutex);
127748128842
db->xCollNeeded = 0;
127749128843
db->xCollNeeded16 = xCollNeeded16;
127750128844
db->pCollNeededArg = pCollNeededArg;
127751128845
sqlite3_mutex_leave(db->mutex);
@@ -127768,10 +128862,16 @@
127768128862
** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
127769128863
** by default. Autocommit is disabled by a BEGIN statement and reenabled
127770128864
** by the next COMMIT or ROLLBACK.
127771128865
*/
127772128866
SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
128867
+#ifdef SQLITE_ENABLE_API_ARMOR
128868
+ if( !sqlite3SafetyCheckOk(db) ){
128869
+ (void)SQLITE_MISUSE_BKPT;
128870
+ return 0;
128871
+ }
128872
+#endif
127773128873
return db->autoCommit;
127774128874
}
127775128875
127776128876
/*
127777128877
** The following routines are substitutes for constants SQLITE_CORRUPT,
@@ -127950,10 +129050,13 @@
127950129050
127951129051
/*
127952129052
** Enable or disable the extended result codes.
127953129053
*/
127954129054
SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
129055
+#ifdef SQLITE_ENABLE_API_ARMOR
129056
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
129057
+#endif
127955129058
sqlite3_mutex_enter(db->mutex);
127956129059
db->errMask = onoff ? 0xffffffff : 0xff;
127957129060
sqlite3_mutex_leave(db->mutex);
127958129061
return SQLITE_OK;
127959129062
}
@@ -127963,10 +129066,13 @@
127963129066
*/
127964129067
SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
127965129068
int rc = SQLITE_ERROR;
127966129069
Btree *pBtree;
127967129070
129071
+#ifdef SQLITE_ENABLE_API_ARMOR
129072
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
129073
+#endif
127968129074
sqlite3_mutex_enter(db->mutex);
127969129075
pBtree = sqlite3DbNameToBtree(db, zDbName);
127970129076
if( pBtree ){
127971129077
Pager *pPager;
127972129078
sqlite3_file *fd;
@@ -128305,11 +129411,11 @@
128305129411
** query parameter we seek. This routine returns the value of the zParam
128306129412
** parameter if it exists. If the parameter does not exist, this routine
128307129413
** returns a NULL pointer.
128308129414
*/
128309129415
SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
128310
- if( zFilename==0 ) return 0;
129416
+ if( zFilename==0 || zParam==0 ) return 0;
128311129417
zFilename += sqlite3Strlen30(zFilename) + 1;
128312129418
while( zFilename[0] ){
128313129419
int x = strcmp(zFilename, zParam);
128314129420
zFilename += sqlite3Strlen30(zFilename) + 1;
128315129421
if( x==0 ) return zFilename;
@@ -128361,19 +129467,31 @@
128361129467
/*
128362129468
** Return the filename of the database associated with a database
128363129469
** connection.
128364129470
*/
128365129471
SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
129472
+#ifdef SQLITE_ENABLE_API_ARMOR
129473
+ if( !sqlite3SafetyCheckOk(db) ){
129474
+ (void)SQLITE_MISUSE_BKPT;
129475
+ return 0;
129476
+ }
129477
+#endif
128366129478
Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
128367129479
return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
128368129480
}
128369129481
128370129482
/*
128371129483
** Return 1 if database is read-only or 0 if read/write. Return -1 if
128372129484
** no such database exists.
128373129485
*/
128374129486
SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
129487
+#ifdef SQLITE_ENABLE_API_ARMOR
129488
+ if( !sqlite3SafetyCheckOk(db) ){
129489
+ (void)SQLITE_MISUSE_BKPT;
129490
+ return -1;
129491
+ }
129492
+#endif
128375129493
Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
128376129494
return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
128377129495
}
128378129496
128379129497
/************** End of main.c ************************************************/
128380129498
--- 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.7.1. 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.
@@ -179,11 +179,11 @@
179
180
181 /*
182 ** These no-op macros are used in front of interfaces to mark those
183 ** interfaces as either deprecated or experimental. New applications
184 ** should not use deprecated interfaces - they are support for backwards
185 ** compatibility only. Application writers should be aware that
186 ** experimental interfaces are subject to change in point releases.
187 **
188 ** These macros used to resolve to various kinds of compiler magic that
189 ** would generate warning messages when they were used. But that
@@ -229,13 +229,13 @@
229 **
230 ** See also: [sqlite3_libversion()],
231 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232 ** [sqlite_version()] and [sqlite_source_id()].
233 */
234 #define SQLITE_VERSION "3.8.7.1"
235 #define SQLITE_VERSION_NUMBER 3008007
236 #define SQLITE_SOURCE_ID "2014-10-29 01:27:43 83afe23e553e802c0947c80d0ffdd120423e7c52"
237
238 /*
239 ** CAPI3REF: Run-Time Library Version Numbers
240 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
241 **
@@ -1626,29 +1626,31 @@
1626 ** it is not possible to set the Serialized [threading mode] and
1627 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1628 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1629 **
1630 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1631 ** <dd> ^(This option takes a single argument which is a pointer to an
1632 ** instance of the [sqlite3_mem_methods] structure. The argument specifies
 
1633 ** alternative low-level memory allocation routines to be used in place of
1634 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1635 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1636 ** before the [sqlite3_config()] call returns.</dd>
1637 **
1638 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1639 ** <dd> ^(This option takes a single argument which is a pointer to an
1640 ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
 
1641 ** structure is filled with the currently defined memory allocation routines.)^
1642 ** This option can be used to overload the default memory allocation
1643 ** routines with a wrapper that simulations memory allocation failure or
1644 ** tracks memory usage, for example. </dd>
1645 **
1646 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1647 ** <dd> ^This option takes single argument of type int, interpreted as a
1648 ** boolean, which enables or disables the collection of memory allocation
1649 ** statistics. ^(When memory allocation statistics are disabled, the
1650 ** following SQLite interfaces become non-operational:
1651 ** <ul>
1652 ** <li> [sqlite3_memory_used()]
1653 ** <li> [sqlite3_memory_highwater()]
1654 ** <li> [sqlite3_soft_heap_limit64()]
@@ -1658,78 +1660,90 @@
1658 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1659 ** allocation statistics are disabled by default.
1660 ** </dd>
1661 **
1662 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1663 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1664 ** scratch memory. There are three arguments: A pointer an 8-byte
 
1665 ** aligned memory buffer from which the scratch allocations will be
1666 ** drawn, the size of each scratch allocation (sz),
1667 ** and the maximum number of scratch allocations (N). The sz
1668 ** argument must be a multiple of 16.
1669 ** The first argument must be a pointer to an 8-byte aligned buffer
1670 ** of at least sz*N bytes of memory.
1671 ** ^SQLite will use no more than two scratch buffers per thread. So
1672 ** N should be set to twice the expected maximum number of threads.
1673 ** ^SQLite will never require a scratch buffer that is more than 6
1674 ** times the database page size. ^If SQLite needs needs additional
1675 ** scratch memory beyond what is provided by this configuration option, then
1676 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
 
 
 
 
 
 
1677 **
1678 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1679 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1680 ** the database page cache with the default page cache implementation.
 
1681 ** This configuration should not be used if an application-define page
1682 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1683 ** There are three arguments to this option: A pointer to 8-byte aligned
 
1684 ** memory, the size of each page buffer (sz), and the number of pages (N).
1685 ** The sz argument should be the size of the largest database page
1686 ** (a power of two between 512 and 32768) plus a little extra for each
1687 ** page header. ^The page header size is 20 to 40 bytes depending on
1688 ** the host architecture. ^It is harmless, apart from the wasted memory,
1689 ** to make sz a little too large. The first
1690 ** argument should point to an allocation of at least sz*N bytes of memory.
 
 
 
 
1691 ** ^SQLite will use the memory provided by the first argument to satisfy its
1692 ** memory needs for the first N pages that it adds to cache. ^If additional
1693 ** page cache memory is needed beyond what is provided by this option, then
1694 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1695 ** The pointer in the first argument must
1696 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1697 ** will be undefined.</dd>
1698 **
1699 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1700 ** <dd> ^This option specifies a static memory buffer that SQLite will use
1701 ** for all of its dynamic memory allocation needs beyond those provided
1702 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1703 ** There are three arguments: An 8-byte aligned pointer to the memory,
 
 
 
 
1704 ** the number of bytes in the memory buffer, and the minimum allocation size.
1705 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1706 ** to using its default memory allocator (the system malloc() implementation),
1707 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1708 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1709 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1710 ** allocator is engaged to handle all of SQLites memory allocation needs.
1711 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1712 ** boundary or subsequent behavior of SQLite will be undefined.
1713 ** The minimum allocation size is capped at 2**12. Reasonable values
1714 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1715 **
1716 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1717 ** <dd> ^(This option takes a single argument which is a pointer to an
1718 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1719 ** alternative low-level mutex routines to be used in place
1720 ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
1721 ** content of the [sqlite3_mutex_methods] structure before the call to
1722 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1723 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1724 ** the entire mutexing subsystem is omitted from the build and hence calls to
1725 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1726 ** return [SQLITE_ERROR].</dd>
1727 **
1728 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1729 ** <dd> ^(This option takes a single argument which is a pointer to an
1730 ** instance of the [sqlite3_mutex_methods] structure. The
1731 ** [sqlite3_mutex_methods]
1732 ** structure is filled with the currently defined mutex routines.)^
1733 ** This option can be used to overload the default mutex allocation
1734 ** routines with a wrapper used to track mutex usage for performance
1735 ** profiling or testing, for example. ^If SQLite is compiled with
@@ -1737,28 +1751,28 @@
1737 ** the entire mutexing subsystem is omitted from the build and hence calls to
1738 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1739 ** return [SQLITE_ERROR].</dd>
1740 **
1741 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1742 ** <dd> ^(This option takes two arguments that determine the default
1743 ** memory allocation for the lookaside memory allocator on each
1744 ** [database connection]. The first argument is the
1745 ** size of each lookaside buffer slot and the second is the number of
1746 ** slots allocated to each database connection.)^ ^(This option sets the
1747 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1748 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1749 ** configuration on individual connections.)^ </dd>
1750 **
1751 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1752 ** <dd> ^(This option takes a single argument which is a pointer to
1753 ** an [sqlite3_pcache_methods2] object. This object specifies the interface
1754 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1755 ** object and uses it for page cache memory allocations.</dd>
1756 **
1757 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1758 ** <dd> ^(This option takes a single argument which is a pointer to an
1759 ** [sqlite3_pcache_methods2] object. SQLite copies of the current
1760 ** page cache implementation into that object.)^ </dd>
1761 **
1762 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1763 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1764 ** global [error log].
@@ -1778,26 +1792,27 @@
1778 ** supplied by the application must not invoke any SQLite interface.
1779 ** In a multi-threaded application, the application-defined logger
1780 ** function must be threadsafe. </dd>
1781 **
1782 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1783 ** <dd>^(This option takes a single argument of type int. If non-zero, then
1784 ** URI handling is globally enabled. If the parameter is zero, then URI handling
1785 ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1786 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1787 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1788 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1789 ** connection is opened. ^If it is globally disabled, filenames are
1790 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1791 ** database connection is opened. ^(By default, URI handling is globally
1792 ** disabled. The default value may be changed by compiling with the
1793 ** [SQLITE_USE_URI] symbol defined.)^
1794 **
1795 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1796 ** <dd>^This option takes a single integer argument which is interpreted as
1797 ** a boolean in order to enable or disable the use of covering indices for
1798 ** full table scans in the query optimizer. ^The default setting is determined
 
1799 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1800 ** if that compile-time option is omitted.
1801 ** The ability to disable the use of covering indices for full table scans
1802 ** is because some incorrectly coded legacy applications might malfunction
1803 ** when the optimization is enabled. Providing the ability to
@@ -1833,23 +1848,32 @@
1833 ** that are the default mmap size limit (the default setting for
1834 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1835 ** ^The default setting can be overridden by each database connection using
1836 ** either the [PRAGMA mmap_size] command, or by using the
1837 ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1838 ** cannot be changed at run-time. Nor may the maximum allowed mmap size
1839 ** exceed the compile-time maximum mmap size set by the
1840 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1841 ** ^If either argument to this option is negative, then that argument is
1842 ** changed to its compile-time default.
1843 **
1844 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1845 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1846 ** <dd>^This option is only available if SQLite is compiled for Windows
1847 ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1848 ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1849 ** that specifies the maximum size of the created heap.
1850 ** </dl>
 
 
 
 
 
 
 
 
 
1851 */
1852 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1853 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1854 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
1855 #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1870,10 +1894,11 @@
1870 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1871 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1872 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1873 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
1874 #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
 
1875
1876 /*
1877 ** CAPI3REF: Database Connection Configuration Options
1878 **
1879 ** These constants are the available integer configuration options that
@@ -1997,51 +2022,49 @@
1997 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
1998
1999 /*
2000 ** CAPI3REF: Count The Number Of Rows Modified
2001 **
2002 ** ^This function returns the number of database rows that were changed
2003 ** or inserted or deleted by the most recently completed SQL statement
2004 ** on the [database connection] specified by the first parameter.
2005 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
2006 ** or [DELETE] statement are counted. Auxiliary changes caused by
2007 ** triggers or [foreign key actions] are not counted.)^ Use the
2008 ** [sqlite3_total_changes()] function to find the total number of changes
2009 ** including changes caused by triggers and foreign key actions.
2010 **
2011 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
2012 ** are not counted. Only real table changes are counted.
2013 **
2014 ** ^(A "row change" is a change to a single row of a single table
2015 ** caused by an INSERT, DELETE, or UPDATE statement. Rows that
2016 ** are changed as side effects of [REPLACE] constraint resolution,
2017 ** rollback, ABORT processing, [DROP TABLE], or by any other
2018 ** mechanisms do not count as direct row changes.)^
2019 **
2020 ** A "trigger context" is a scope of execution that begins and
2021 ** ends with the script of a [CREATE TRIGGER | trigger].
2022 ** Most SQL statements are
2023 ** evaluated outside of any trigger. This is the "top level"
2024 ** trigger context. If a trigger fires from the top level, a
2025 ** new trigger context is entered for the duration of that one
2026 ** trigger. Subtriggers create subcontexts for their duration.
2027 **
2028 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2029 ** not create a new trigger context.
2030 **
2031 ** ^This function returns the number of direct row changes in the
2032 ** most recent INSERT, UPDATE, or DELETE statement within the same
2033 ** trigger context.
2034 **
2035 ** ^Thus, when called from the top level, this function returns the
2036 ** number of changes in the most recent INSERT, UPDATE, or DELETE
2037 ** that also occurred at the top level. ^(Within the body of a trigger,
2038 ** the sqlite3_changes() interface can be called to find the number of
2039 ** changes in the most recently completed INSERT, UPDATE, or DELETE
2040 ** statement within the body of the same trigger.
2041 ** However, the number returned does not include changes
2042 ** caused by subtriggers since those have their own context.)^
2043 **
2044 ** See also the [sqlite3_total_changes()] interface, the
2045 ** [count_changes pragma], and the [changes() SQL function].
2046 **
2047 ** If a separate thread makes changes on the same database connection
@@ -2051,24 +2074,21 @@
2051 SQLITE_API int sqlite3_changes(sqlite3*);
2052
2053 /*
2054 ** CAPI3REF: Total Number Of Rows Modified
2055 **
2056 ** ^This function returns the number of row changes caused by [INSERT],
2057 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2058 ** ^(The count returned by sqlite3_total_changes() includes all changes
2059 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
2060 ** [foreign key actions]. However,
2061 ** the count does not include changes used to implement [REPLACE] constraints,
2062 ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
2063 ** count does not include rows of views that fire an [INSTEAD OF trigger],
2064 ** though if the INSTEAD OF trigger makes changes of its own, those changes
2065 ** are counted.)^
2066 ** ^The sqlite3_total_changes() function counts the changes as soon as
2067 ** the statement that makes them is completed (when the statement handle
2068 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2069 **
2070 ** See also the [sqlite3_changes()] interface, the
2071 ** [count_changes pragma], and the [total_changes() SQL function].
2072 **
2073 ** If a separate thread makes changes on the same database connection
2074 ** while [sqlite3_total_changes()] is running then the value
@@ -2542,17 +2562,18 @@
2542 ** already uses the largest possible [ROWID]. The PRNG is also used for
2543 ** the build-in random() and randomblob() SQL functions. This interface allows
2544 ** applications to access the same PRNG for other purposes.
2545 **
2546 ** ^A call to this routine stores N bytes of randomness into buffer P.
2547 ** ^If N is less than one, then P can be a NULL pointer.
2548 **
2549 ** ^If this routine has not been previously called or if the previous
2550 ** call had N less than one, then the PRNG is seeded using randomness
2551 ** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2552 ** ^If the previous call to this routine had an N of 1 or more then
2553 ** the pseudo-randomness is generated
 
2554 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2555 ** method.
2556 */
2557 SQLITE_API void sqlite3_randomness(int N, void *P);
2558
@@ -5762,31 +5783,47 @@
5762 ** in other words, the same BLOB that would be selected by:
5763 **
5764 ** <pre>
5765 ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5766 ** </pre>)^
 
 
 
 
 
 
5767 **
5768 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5769 ** and write access. ^If it is zero, the BLOB is opened for read access.
5770 ** ^It is not possible to open a column that is part of an index or primary
5771 ** key for writing. ^If [foreign key constraints] are enabled, it is
5772 ** not possible to open a column that is part of a [child key] for writing.
5773 **
5774 ** ^Note that the database name is not the filename that contains
5775 ** the database but rather the symbolic name of the database that
5776 ** appears after the AS keyword when the database is connected using [ATTACH].
5777 ** ^For the main database file, the database name is "main".
5778 ** ^For TEMP tables, the database name is "temp".
5779 **
5780 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5781 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5782 ** to be a null pointer.)^
5783 ** ^This function sets the [database connection] error code and message
5784 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5785 ** functions. ^Note that the *ppBlob variable is always initialized in a
5786 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5787 ** regardless of the success or failure of this routine.
 
 
 
 
 
 
 
 
 
 
5788 **
5789 ** ^(If the row that a BLOB handle points to is modified by an
5790 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5791 ** then the BLOB handle is marked as "expired".
5792 ** This is true if any column of the row is changed, even a column
@@ -5800,17 +5837,13 @@
5800 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5801 ** the opened blob. ^The size of a blob may not be changed by this
5802 ** interface. Use the [UPDATE] SQL command to change the size of a
5803 ** blob.
5804 **
5805 ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5806 ** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5807 **
5808 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5809 ** and the built-in [zeroblob] SQL function can be used, if desired,
5810 ** to create an empty, zero-filled blob in which to read or write using
5811 ** this interface.
5812 **
5813 ** To avoid a resource leak, every open [BLOB handle] should eventually
5814 ** be released by a call to [sqlite3_blob_close()].
5815 */
5816 SQLITE_API int sqlite3_blob_open(
@@ -5848,28 +5881,26 @@
5848 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5849
5850 /*
5851 ** CAPI3REF: Close A BLOB Handle
5852 **
5853 ** ^Closes an open [BLOB handle].
5854 **
5855 ** ^Closing a BLOB shall cause the current transaction to commit
5856 ** if there are no other BLOBs, no pending prepared statements, and the
5857 ** database connection is in [autocommit mode].
5858 ** ^If any writes were made to the BLOB, they might be held in cache
5859 ** until the close operation if they will fit.
5860 **
5861 ** ^(Closing the BLOB often forces the changes
5862 ** out to disk and so if any I/O errors occur, they will likely occur
5863 ** at the time when the BLOB is closed. Any errors that occur during
5864 ** closing are reported as a non-zero return value.)^
5865 **
5866 ** ^(The BLOB is closed unconditionally. Even if this routine returns
5867 ** an error code, the BLOB is still closed.)^
5868 **
5869 ** ^Calling this routine with a null pointer (such as would be returned
5870 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5871 */
5872 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5873
5874 /*
5875 ** CAPI3REF: Return The Size Of An Open BLOB
@@ -5915,36 +5946,39 @@
5915 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5916
5917 /*
5918 ** CAPI3REF: Write Data Into A BLOB Incrementally
5919 **
5920 ** ^This function is used to write data into an open [BLOB handle] from a
5921 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5922 ** into the open BLOB, starting at offset iOffset.
 
 
 
 
 
 
5923 **
5924 ** ^If the [BLOB handle] passed as the first argument was not opened for
5925 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5926 ** this function returns [SQLITE_READONLY].
5927 **
5928 ** ^This function may only modify the contents of the BLOB; it is
5929 ** not possible to increase the size of a BLOB using this API.
5930 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5931 ** [SQLITE_ERROR] is returned and no data is written. ^If N is
5932 ** less than zero [SQLITE_ERROR] is returned and no data is written.
5933 ** The size of the BLOB (and hence the maximum value of N+iOffset)
5934 ** can be determined using the [sqlite3_blob_bytes()] interface.
5935 **
5936 ** ^An attempt to write to an expired [BLOB handle] fails with an
5937 ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
5938 ** before the [BLOB handle] expired are not rolled back by the
5939 ** expiration of the handle, though of course those changes might
5940 ** have been overwritten by the statement that expired the BLOB handle
5941 ** or by other independent statements.
5942 **
5943 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5944 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5945 **
5946 ** This routine only works on a [BLOB handle] which has been created
5947 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5948 ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
5949 ** to this routine results in undefined and probably undesirable behavior.
5950 **
@@ -7533,10 +7567,102 @@
7533 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7534 #define SQLITE_FAIL 3
7535 /* #define SQLITE_ABORT 4 // Also an error code */
7536 #define SQLITE_REPLACE 5
7537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7538
7539
7540 /*
7541 ** Undo the hack that converts floating point types to integer for
7542 ** builds on processors without floating point support.
@@ -7978,14 +8104,13 @@
7978 #ifndef SQLITE_POWERSAFE_OVERWRITE
7979 # define SQLITE_POWERSAFE_OVERWRITE 1
7980 #endif
7981
7982 /*
7983 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
7984 ** It determines whether or not the features related to
7985 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
7986 ** be overridden at runtime using the sqlite3_config() API.
7987 */
7988 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
7989 # define SQLITE_DEFAULT_MEMSTATUS 1
7990 #endif
7991
@@ -8611,11 +8736,11 @@
8611 ** Estimated quantities used for query planning are stored as 16-bit
8612 ** logarithms. For quantity X, the value stored is 10*log2(X). This
8613 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8614 ** But the allowed values are "grainy". Not every value is representable.
8615 ** For example, quantities 16 and 17 are both represented by a LogEst
8616 ** of 40. However, since LogEst quantaties are suppose to be estimates,
8617 ** not exact values, this imprecision is not a problem.
8618 **
8619 ** "LogEst" is short for "Logarithmic Estimate".
8620 **
8621 ** Examples:
@@ -9011,11 +9136,11 @@
9011 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
9012 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
9013 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
9014 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
9015 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
9016 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
9017 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
9018 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
9019 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
9020 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
9021 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
@@ -9044,11 +9169,11 @@
9044 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
9045
9046 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
9047 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
9048 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
9049 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
9050
9051 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
9052 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
9053
9054 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
@@ -9124,10 +9249,11 @@
9124 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9125 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9126 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9127 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9128 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
 
9129
9130 #ifndef NDEBUG
9131 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
9132 #endif
9133
@@ -9666,10 +9792,16 @@
9666 # define VdbeCoverageAlwaysTaken(v)
9667 # define VdbeCoverageNeverTaken(v)
9668 # define VDBE_OFFSET_LINENO(x) 0
9669 #endif
9670
 
 
 
 
 
 
9671 #endif
9672
9673 /************** End of vdbe.h ************************************************/
9674 /************** Continuing where we left off in sqliteInt.h ******************/
9675 /************** Include pager.h in the middle of sqliteInt.h *****************/
@@ -9862,10 +9994,12 @@
9862 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
9863
9864 /* Functions used to truncate the database file. */
9865 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9866
 
 
9867 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
9868 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
9869 #endif
9870
9871 /* Functions to support testing and debugging. */
@@ -10049,10 +10183,14 @@
10049 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
10050 #endif
10051
10052 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
10053
 
 
 
 
10054 #endif /* _PCACHE_H_ */
10055
10056 /************** End of pcache.h **********************************************/
10057 /************** Continuing where we left off in sqliteInt.h ******************/
10058
@@ -10735,11 +10873,11 @@
10735 #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
10736 #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
10737 #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */
10738 #define SQLITE_Transitive 0x0200 /* Transitive constraints */
10739 #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */
10740 #define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */
10741 #define SQLITE_AllOpts 0xffff /* All optimizations */
10742
10743 /*
10744 ** Macros for testing whether or not optimizations are enabled or disabled.
10745 */
@@ -11322,11 +11460,12 @@
11322 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11323 int nSample; /* Number of elements in aSample[] */
11324 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
11325 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
11326 IndexSample *aSample; /* Samples of the left-most key */
11327 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */
 
11328 #endif
11329 };
11330
11331 /*
11332 ** Allowed values for Index.idxType
@@ -11520,11 +11659,11 @@
11520 int nHeight; /* Height of the tree headed by this node */
11521 #endif
11522 int iTable; /* TK_COLUMN: cursor number of table holding column
11523 ** TK_REGISTER: register number
11524 ** TK_TRIGGER: 1 -> new, 0 -> old
11525 ** EP_Unlikely: 1000 times likelihood */
11526 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
11527 ** TK_VARIABLE: variable number (always >= 1). */
11528 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
11529 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
11530 u8 op2; /* TK_REGISTER: original value of Expr.op
@@ -12412,13 +12551,15 @@
12412 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
12413 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
12414 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
12415 Parse *pParse; /* Parser context. */
12416 int walkerDepth; /* Number of subqueries */
 
12417 union { /* Extra data for callback */
12418 NameContext *pNC; /* Naming context */
12419 int i; /* Integer value */
 
12420 SrcList *pSrcList; /* FROM clause */
12421 struct SrcCount *pSrcCount; /* Counting column references */
12422 } u;
12423 };
12424
@@ -12815,10 +12956,11 @@
12815 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12816 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12817 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12818 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12819 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
 
12820 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12821 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12822 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12823 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12824 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -13060,11 +13202,11 @@
13060 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
13061
13062 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
13063 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
13064 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
13065 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
13066 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
13067 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
13068 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
13069 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
13070
@@ -13472,15 +13614,23 @@
13472 ** compatibility for legacy applications, the URI filename capability is
13473 ** disabled by default.
13474 **
13475 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
13476 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
 
 
 
 
13477 */
13478 #ifndef SQLITE_USE_URI
13479 # define SQLITE_USE_URI 0
13480 #endif
13481
 
 
 
 
13482 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
13483 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
13484 #endif
13485
13486 /*
@@ -13566,12 +13716,12 @@
13566 ** than 1 GiB. The sqlite3_test_control() interface can be used to
13567 ** move the pending byte.
13568 **
13569 ** IMPORTANT: Changing the pending byte to any value other than
13570 ** 0x40000000 results in an incompatible database file format!
13571 ** Changing the pending byte during operating results in undefined
13572 ** and dileterious behavior.
13573 */
13574 #ifndef SQLITE_OMIT_WSD
13575 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
13576 #endif
13577
@@ -13646,10 +13796,13 @@
13646 #ifdef SQLITE_DISABLE_DIRSYNC
13647 "DISABLE_DIRSYNC",
13648 #endif
13649 #ifdef SQLITE_DISABLE_LFS
13650 "DISABLE_LFS",
 
 
 
13651 #endif
13652 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13653 "ENABLE_ATOMIC_WRITE",
13654 #endif
13655 #ifdef SQLITE_ENABLE_CEROD
@@ -13972,10 +14125,17 @@
13972 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
13973 ** is not required for a match.
13974 */
13975 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
13976 int i, n;
 
 
 
 
 
 
 
13977 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
13978 n = sqlite3Strlen30(zOptName);
13979
13980 /* Since ArraySize(azCompileOpt) is normally in single digits, a
13981 ** linear search is adequate. No need for a binary search. */
@@ -14153,10 +14313,11 @@
14153 typedef struct VdbeFrame VdbeFrame;
14154 struct VdbeFrame {
14155 Vdbe *v; /* VM this frame belongs to */
14156 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
14157 Op *aOp; /* Program instructions for parent frame */
 
14158 Mem *aMem; /* Array of memory cells for parent frame */
14159 u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
14160 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
14161 void *token; /* Copy of SubProgram.token */
14162 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
@@ -14165,11 +14326,12 @@
14165 int nOp; /* Size of aOp array */
14166 int nMem; /* Number of entries in aMem */
14167 int nOnceFlag; /* Number of entries in aOnceFlag */
14168 int nChildMem; /* Number of memory cells for child frame */
14169 int nChildCsr; /* Number of cursors for child frame */
14170 int nChange; /* Statement changes (Vdbe.nChanges) */
 
14171 };
14172
14173 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
14174
14175 /*
@@ -14316,10 +14478,20 @@
14316 /* A bitfield type for use inside of structures. Always follow with :N where
14317 ** N is the number of bits.
14318 */
14319 typedef unsigned bft; /* Bit Field Type */
14320
 
 
 
 
 
 
 
 
 
 
14321 /*
14322 ** An instance of the virtual machine. This structure contains the complete
14323 ** state of the virtual machine.
14324 **
14325 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -14388,10 +14560,15 @@
14388 u32 expmask; /* Binding to these vars invalidates VM */
14389 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
14390 int nOnceFlag; /* Size of array aOnceFlag[] */
14391 u8 *aOnceFlag; /* Flags for OP_Once */
14392 AuxData *pAuxData; /* Linked list of auxdata allocations */
 
 
 
 
 
14393 };
14394
14395 /*
14396 ** The following are allowed values for Vdbe.magic
14397 */
@@ -14577,10 +14754,13 @@
14577 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
14578 wsdStatInit;
14579 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
14580 return SQLITE_MISUSE_BKPT;
14581 }
 
 
 
14582 *pCurrent = wsdStat.nowValue[op];
14583 *pHighwater = wsdStat.mxValue[op];
14584 if( resetFlag ){
14585 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14586 }
@@ -14596,10 +14776,15 @@
14596 int *pCurrent, /* Write current value here */
14597 int *pHighwater, /* Write high-water mark here */
14598 int resetFlag /* Reset high-water mark if true */
14599 ){
14600 int rc = SQLITE_OK; /* Return code */
 
 
 
 
 
14601 sqlite3_mutex_enter(db->mutex);
14602 switch( op ){
14603 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
14604 *pCurrent = db->lookaside.nOut;
14605 *pHighwater = db->lookaside.mxOut;
@@ -14774,11 +14959,11 @@
14774 **
14775 ** There is only one exported symbol in this file - the function
14776 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
14777 ** All other code has file scope.
14778 **
14779 ** SQLite processes all times and dates as Julian Day numbers. The
14780 ** dates and times are stored as the number of days since noon
14781 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
14782 ** calendar system.
14783 **
14784 ** 1970-01-01 00:00:00 is JD 2440587.5
@@ -14789,11 +14974,11 @@
14789 ** be represented, even though julian day numbers allow a much wider
14790 ** range of dates.
14791 **
14792 ** The Gregorian calendar system is used for all dates and times,
14793 ** even those that predate the Gregorian calendar. Historians usually
14794 ** use the Julian calendar for dates prior to 1582-10-15 and for some
14795 ** dates afterwards, depending on locale. Beware of this difference.
14796 **
14797 ** The conversion algorithms are implemented based on descriptions
14798 ** in the following text:
14799 **
@@ -15061,11 +15246,11 @@
15061 return 1;
15062 }
15063 }
15064
15065 /*
15066 ** Attempt to parse the given string into a Julian Day Number. Return
15067 ** the number of errors.
15068 **
15069 ** The following are acceptable forms for the input string:
15070 **
15071 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
@@ -15632,11 +15817,11 @@
15632 **
15633 ** %d day of month
15634 ** %f ** fractional seconds SS.SSS
15635 ** %H hour 00-24
15636 ** %j day of year 000-366
15637 ** %J ** Julian day number
15638 ** %m month 01-12
15639 ** %M minute 00-59
15640 ** %s seconds since 1970-01-01
15641 ** %S seconds 00-59
15642 ** %w day of week 0-6 sunday==0
@@ -16257,10 +16442,14 @@
16257 MUTEX_LOGIC(sqlite3_mutex *mutex;)
16258 #ifndef SQLITE_OMIT_AUTOINIT
16259 int rc = sqlite3_initialize();
16260 if( rc ) return rc;
16261 #endif
 
 
 
 
16262 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
16263 sqlite3_mutex_enter(mutex);
16264 vfsUnlink(pVfs);
16265 if( makeDflt || vfsList==0 ){
16266 pVfs->pNext = vfsList;
@@ -18614,10 +18803,11 @@
18614 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18615 */
18616 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18617 #ifndef SQLITE_OMIT_AUTOINIT
18618 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
 
18619 #endif
18620 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18621 }
18622
18623 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -19070,12 +19260,16 @@
19070 pthread_mutex_init(&p->mutex, 0);
19071 }
19072 break;
19073 }
19074 default: {
19075 assert( iType-2 >= 0 );
19076 assert( iType-2 < ArraySize(staticMutexes) );
 
 
 
 
19077 p = &staticMutexes[iType-2];
19078 #if SQLITE_MUTEX_NREF
19079 p->id = iType;
19080 #endif
19081 break;
@@ -20293,15 +20487,16 @@
20293 }
20294 assert( sqlite3_mutex_notheld(mem0.mutex) );
20295
20296
20297 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
20298 /* Verify that no more than two scratch allocations per thread
20299 ** are outstanding at one time. (This is only checked in the
20300 ** single-threaded case since checking in the multi-threaded case
20301 ** would be much more complicated.) */
20302 assert( scratchAllocOut<=1 );
 
20303 if( p ) scratchAllocOut++;
20304 #endif
20305
20306 return p;
20307 }
@@ -20945,11 +21140,11 @@
20945 sqlite_uint64 longvalue; /* Value for integer types */
20946 LONGDOUBLE_TYPE realvalue; /* Value for real types */
20947 const et_info *infop; /* Pointer to the appropriate info structure */
20948 char *zOut; /* Rendering buffer */
20949 int nOut; /* Size of the rendering buffer */
20950 char *zExtra; /* Malloced memory used by some conversion */
20951 #ifndef SQLITE_OMIT_FLOATING_POINT
20952 int exp, e2; /* exponent of real numbers */
20953 int nsd; /* Number of significant digits returned */
20954 double rounder; /* Used for rounding floating point values */
20955 etByte flag_dp; /* True if decimal point should be shown */
@@ -20956,10 +21151,17 @@
20956 etByte flag_rtz; /* True if trailing zeros should be removed */
20957 #endif
20958 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
20959 char buf[etBUFSIZE]; /* Conversion buffer */
20960
 
 
 
 
 
 
 
20961 bufpt = 0;
20962 if( bFlags ){
20963 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
20964 pArgList = va_arg(ap, PrintfArguments*);
20965 }
@@ -21062,11 +21264,10 @@
21062 return;
21063 }
21064 break;
21065 }
21066 }
21067 zExtra = 0;
21068
21069 /*
21070 ** At this point, variables are initialized as follows:
21071 **
21072 ** flag_alternateform TRUE if a '#' is present.
@@ -21353,17 +21554,20 @@
21353 bufpt = getTextArg(pArgList);
21354 c = bufpt ? bufpt[0] : 0;
21355 }else{
21356 c = va_arg(ap,int);
21357 }
21358 buf[0] = (char)c;
21359 if( precision>=0 ){
21360 for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
21361 length = precision;
21362 }else{
21363 length =1;
 
21364 }
 
 
21365 bufpt = buf;
21366 break;
21367 case etSTRING:
21368 case etDYNSTRING:
21369 if( bArgList ){
@@ -21460,15 +21664,18 @@
21460 ** The text of the conversion is pointed to by "bufpt" and is
21461 ** "length" characters long. The field width is "width". Do
21462 ** the output.
21463 */
21464 width -= length;
21465 if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
21466 sqlite3StrAccumAppend(pAccum, bufpt, length);
21467 if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
21468
21469 if( zExtra ) sqlite3_free(zExtra);
 
 
 
21470 }/* End for loop over the format string */
21471 } /* End of function */
21472
21473 /*
21474 ** Enlarge the memory allocation on a StrAccum object so that it is
@@ -21491,10 +21698,15 @@
21491 return N;
21492 }else{
21493 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
21494 i64 szNew = p->nChar;
21495 szNew += N + 1;
 
 
 
 
 
21496 if( szNew > p->mxAlloc ){
21497 sqlite3StrAccumReset(p);
21498 setStrAccumError(p, STRACCUM_TOOBIG);
21499 return 0;
21500 }else{
@@ -21507,10 +21719,11 @@
21507 }
21508 if( zNew ){
21509 assert( p->zText!=0 || p->nChar==0 );
21510 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
21511 p->zText = zNew;
 
21512 }else{
21513 sqlite3StrAccumReset(p);
21514 setStrAccumError(p, STRACCUM_NOMEM);
21515 return 0;
21516 }
@@ -21517,15 +21730,15 @@
21517 }
21518 return N;
21519 }
21520
21521 /*
21522 ** Append N space characters to the given string buffer.
21523 */
21524 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){
21525 if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
21526 while( (N--)>0 ) p->zText[p->nChar++] = ' ';
21527 }
21528
21529 /*
21530 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
21531 ** So enlarge if first, then do the append.
@@ -21676,10 +21889,17 @@
21676 */
21677 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
21678 char *z;
21679 char zBase[SQLITE_PRINT_BUF_SIZE];
21680 StrAccum acc;
 
 
 
 
 
 
 
21681 #ifndef SQLITE_OMIT_AUTOINIT
21682 if( sqlite3_initialize() ) return 0;
21683 #endif
21684 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
21685 acc.useMalloc = 2;
@@ -21718,10 +21938,17 @@
21718 ** sqlite3_vsnprintf() is the varargs version.
21719 */
21720 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
21721 StrAccum acc;
21722 if( n<=0 ) return zBuf;
 
 
 
 
 
 
 
21723 sqlite3StrAccumInit(&acc, zBuf, n, 0);
21724 acc.useMalloc = 0;
21725 sqlite3VXPrintf(&acc, 0, zFormat, ap);
21726 return sqlite3StrAccumFinish(&acc);
21727 }
@@ -21909,15 +22136,23 @@
21909 #else
21910 # define wsdPrng sqlite3Prng
21911 #endif
21912
21913 #if SQLITE_THREADSAFE
21914 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
 
 
 
 
 
 
 
 
 
 
21915 sqlite3_mutex_enter(mutex);
21916 #endif
21917
21918 if( N<=0 ){
21919 wsdPrng.isInit = 0;
21920 sqlite3_mutex_leave(mutex);
21921 return;
21922 }
21923
@@ -23035,17 +23270,27 @@
23035 ** case-independent fashion, using the same definition of "case
23036 ** independence" that SQLite uses internally when comparing identifiers.
23037 */
23038 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
23039 register unsigned char *a, *b;
 
 
 
 
 
23040 a = (unsigned char *)zLeft;
23041 b = (unsigned char *)zRight;
23042 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
23043 return UpperToLower[*a] - UpperToLower[*b];
23044 }
23045 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
23046 register unsigned char *a, *b;
 
 
 
 
 
23047 a = (unsigned char *)zLeft;
23048 b = (unsigned char *)zRight;
23049 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
23050 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
23051 }
@@ -32574,10 +32819,15 @@
32574 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
32575 # error "WAL mode requires support from the Windows NT kernel, compile\
32576 with SQLITE_OMIT_WAL."
32577 #endif
32578
 
 
 
 
 
32579 /*
32580 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
32581 ** based on the sub-platform)?
32582 */
32583 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
@@ -32703,14 +32953,15 @@
32703 # define winGetDirSep() '\\'
32704 #endif
32705
32706 /*
32707 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
32708 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
32709 ** are not present in the header file)?
32710 */
32711 #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
 
32712 /*
32713 ** Two of the file mapping APIs are different under WinRT. Figure out which
32714 ** set we need.
32715 */
32716 #if SQLITE_OS_WINRT
@@ -32734,11 +32985,11 @@
32734
32735 /*
32736 ** This file mapping API is common to both Win32 and WinRT.
32737 */
32738 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
32739 #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
32740
32741 /*
32742 ** Some Microsoft compilers lack this definition.
32743 */
32744 #ifndef INVALID_FILE_ATTRIBUTES
@@ -33027,21 +33278,21 @@
33027
33028 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
33029 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
33030
33031 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
33032 !defined(SQLITE_OMIT_WAL))
33033 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
33034 #else
33035 { "CreateFileMappingA", (SYSCALL)0, 0 },
33036 #endif
33037
33038 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
33039 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
33040
33041 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
33042 !defined(SQLITE_OMIT_WAL))
33043 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
33044 #else
33045 { "CreateFileMappingW", (SYSCALL)0, 0 },
33046 #endif
33047
@@ -33377,11 +33628,12 @@
33377 #ifndef osLockFileEx
33378 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
33379 LPOVERLAPPED))aSyscall[48].pCurrent)
33380 #endif
33381
33382 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
 
33383 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
33384 #else
33385 { "MapViewOfFile", (SYSCALL)0, 0 },
33386 #endif
33387
@@ -33447,11 +33699,11 @@
33447 #endif
33448
33449 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
33450 LPOVERLAPPED))aSyscall[58].pCurrent)
33451
33452 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
33453 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
33454 #else
33455 { "UnmapViewOfFile", (SYSCALL)0, 0 },
33456 #endif
33457
@@ -33510,11 +33762,11 @@
33510 #endif
33511
33512 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
33513 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
33514
33515 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
33516 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
33517 #else
33518 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
33519 #endif
33520
@@ -33574,11 +33826,11 @@
33574
33575 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
33576
33577 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
33578
33579 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
33580 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
33581 #else
33582 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
33583 #endif
33584
@@ -39150,10 +39402,17 @@
39150 */
39151 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
39152 assert( pCache->pCache!=0 );
39153 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
39154 }
 
 
 
 
 
 
 
39155
39156 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
39157 /*
39158 ** For all dirty pages currently in the cache, invoke the specified
39159 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
@@ -40149,10 +40408,15 @@
40149 pcache1Shrink /* xShrink */
40150 };
40151 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
40152 }
40153
 
 
 
 
 
40154 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40155 /*
40156 ** This function is called to free superfluous dynamically allocated memory
40157 ** held by the pager system. Memory in use by any SQLite pager allocated
40158 ** by the current thread may be sqlite3_free()ed.
@@ -47705,10 +47969,22 @@
47705 }
47706
47707 return SQLITE_OK;
47708 }
47709 #endif
 
 
 
 
 
 
 
 
 
 
 
 
47710
47711 /*
47712 ** Return a pointer to the data for the specified page.
47713 */
47714 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
@@ -48103,10 +48379,11 @@
48103 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
48104 assert( pPager->eState>=PAGER_READER );
48105 return sqlite3WalFramesize(pPager->pWal);
48106 }
48107 #endif
 
48108
48109 #endif /* SQLITE_OMIT_DISKIO */
48110
48111 /************** End of pager.c ***********************************************/
48112 /************** Begin file wal.c *********************************************/
@@ -49613,11 +49890,11 @@
49613
49614 /*
49615 ** Free an iterator allocated by walIteratorInit().
49616 */
49617 static void walIteratorFree(WalIterator *p){
49618 sqlite3ScratchFree(p);
49619 }
49620
49621 /*
49622 ** Construct a WalInterator object that can be used to loop over all
49623 ** pages in the WAL in ascending order. The caller must hold the checkpoint
@@ -49648,21 +49925,21 @@
49648 /* Allocate space for the WalIterator object. */
49649 nSegment = walFramePage(iLast) + 1;
49650 nByte = sizeof(WalIterator)
49651 + (nSegment-1)*sizeof(struct WalSegment)
49652 + iLast*sizeof(ht_slot);
49653 p = (WalIterator *)sqlite3ScratchMalloc(nByte);
49654 if( !p ){
49655 return SQLITE_NOMEM;
49656 }
49657 memset(p, 0, nByte);
49658 p->nSegment = nSegment;
49659
49660 /* Allocate temporary space used by the merge-sort routine. This block
49661 ** of memory will be freed before this function returns.
49662 */
49663 aTmp = (ht_slot *)sqlite3ScratchMalloc(
49664 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
49665 );
49666 if( !aTmp ){
49667 rc = SQLITE_NOMEM;
49668 }
@@ -49695,11 +49972,11 @@
49695 p->aSegment[i].nEntry = nEntry;
49696 p->aSegment[i].aIndex = aIndex;
49697 p->aSegment[i].aPgno = (u32 *)aPgno;
49698 }
49699 }
49700 sqlite3ScratchFree(aTmp);
49701
49702 if( rc!=SQLITE_OK ){
49703 walIteratorFree(p);
49704 }
49705 *pp = p;
@@ -50615,11 +50892,11 @@
50615 ** was in before the client began writing to the database.
50616 */
50617 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
50618
50619 for(iFrame=pWal->hdr.mxFrame+1;
50620 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
50621 iFrame++
50622 ){
50623 /* This call cannot fail. Unless the page for which the page number
50624 ** is passed as the second argument is (a) in the cache and
50625 ** (b) has an outstanding reference, then xUndo is either a no-op
@@ -50634,11 +50911,10 @@
50634 assert( walFramePgno(pWal, iFrame)!=1 );
50635 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
50636 }
50637 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
50638 }
50639 assert( rc==SQLITE_OK );
50640 return rc;
50641 }
50642
50643 /*
50644 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
@@ -53333,28 +53609,27 @@
53333 int cellOffset; /* Offset to the cell pointer array */
53334 int cbrk; /* Offset to the cell content area */
53335 int nCell; /* Number of cells on the page */
53336 unsigned char *data; /* The page data */
53337 unsigned char *temp; /* Temp area for cell content */
 
53338 int iCellFirst; /* First allowable cell index */
53339 int iCellLast; /* Last possible cell index */
53340
53341
53342 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53343 assert( pPage->pBt!=0 );
53344 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
53345 assert( pPage->nOverflow==0 );
53346 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53347 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
53348 data = pPage->aData;
53349 hdr = pPage->hdrOffset;
53350 cellOffset = pPage->cellOffset;
53351 nCell = pPage->nCell;
53352 assert( nCell==get2byte(&data[hdr+3]) );
53353 usableSize = pPage->pBt->usableSize;
53354 cbrk = get2byte(&data[hdr+5]);
53355 memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
53356 cbrk = usableSize;
53357 iCellFirst = cellOffset + 2*nCell;
53358 iCellLast = usableSize - 4;
53359 for(i=0; i<nCell; i++){
53360 u8 *pAddr; /* The i-th cell pointer */
@@ -53369,11 +53644,11 @@
53369 if( pc<iCellFirst || pc>iCellLast ){
53370 return SQLITE_CORRUPT_BKPT;
53371 }
53372 #endif
53373 assert( pc>=iCellFirst && pc<=iCellLast );
53374 size = cellSizePtr(pPage, &temp[pc]);
53375 cbrk -= size;
53376 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
53377 if( cbrk<iCellFirst ){
53378 return SQLITE_CORRUPT_BKPT;
53379 }
@@ -53383,12 +53658,20 @@
53383 }
53384 #endif
53385 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
53386 testcase( cbrk+size==usableSize );
53387 testcase( pc+size==usableSize );
53388 memcpy(&data[cbrk], &temp[pc], size);
53389 put2byte(pAddr, cbrk);
 
 
 
 
 
 
 
 
 
53390 }
53391 assert( cbrk>=iCellFirst );
53392 put2byte(&data[hdr+5], cbrk);
53393 data[hdr+1] = 0;
53394 data[hdr+2] = 0;
@@ -53398,10 +53681,66 @@
53398 if( cbrk-iCellFirst!=pPage->nFree ){
53399 return SQLITE_CORRUPT_BKPT;
53400 }
53401 return SQLITE_OK;
53402 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53403
53404 /*
53405 ** Allocate nByte bytes of space from within the B-Tree page passed
53406 ** as the first argument. Write into *pIdx the index into pPage->aData[]
53407 ** of the first byte of allocated space. Return either SQLITE_OK or
@@ -53416,22 +53755,20 @@
53416 */
53417 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
53418 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
53419 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
53420 int top; /* First byte of cell content area */
 
53421 int gap; /* First byte of gap between cell pointers and cell content */
53422 int rc; /* Integer return code */
53423 int usableSize; /* Usable size of the page */
53424
53425 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53426 assert( pPage->pBt );
53427 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53428 assert( nByte>=0 ); /* Minimum cell size is 4 */
53429 assert( pPage->nFree>=nByte );
53430 assert( pPage->nOverflow==0 );
53431 usableSize = pPage->pBt->usableSize;
53432 assert( nByte < usableSize-8 );
53433
53434 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
53435 gap = pPage->cellOffset + 2*pPage->nCell;
53436 assert( gap<=65536 );
53437 top = get2byte(&data[hdr+5]);
@@ -53449,46 +53786,27 @@
53449 */
53450 testcase( gap+2==top );
53451 testcase( gap+1==top );
53452 testcase( gap==top );
53453 if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
53454 int pc, addr;
53455 for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
53456 int size; /* Size of the free slot */
53457 if( pc>usableSize-4 || pc<addr+4 ){
53458 return SQLITE_CORRUPT_BKPT;
53459 }
53460 size = get2byte(&data[pc+2]);
53461 if( size>=nByte ){
53462 int x = size - nByte;
53463 testcase( x==4 );
53464 testcase( x==3 );
53465 if( x<4 ){
53466 if( data[hdr+7]>=60 ) goto defragment_page;
53467 /* Remove the slot from the free-list. Update the number of
53468 ** fragmented bytes within the page. */
53469 memcpy(&data[addr], &data[pc], 2);
53470 data[hdr+7] += (u8)x;
53471 }else if( size+pc > usableSize ){
53472 return SQLITE_CORRUPT_BKPT;
53473 }else{
53474 /* The slot remains on the free-list. Reduce its size to account
53475 ** for the portion used by the new allocation. */
53476 put2byte(&data[pc+2], x);
53477 }
53478 *pIdx = pc + x;
53479 return SQLITE_OK;
53480 }
53481 }
53482 }
53483
53484 /* The request could not be fulfilled using a freelist slot. Check
53485 ** to see if defragmentation is necessary.
53486 */
53487 testcase( gap+2+nByte==top );
53488 if( gap+2+nByte>top ){
53489 defragment_page:
53490 testcase( pPage->nCell==0 );
53491 rc = defragmentPage(pPage);
53492 if( rc ) return rc;
53493 top = get2byteNotZero(&data[hdr+5]);
53494 assert( gap+nByte<=top );
@@ -53532,11 +53850,11 @@
53532 unsigned char *data = pPage->aData; /* Page content */
53533
53534 assert( pPage->pBt!=0 );
53535 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53536 assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
53537 assert( iEnd <= pPage->pBt->usableSize );
53538 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53539 assert( iSize>=4 ); /* Minimum cell size is 4 */
53540 assert( iStart<=iLast );
53541
53542 /* Overwrite deleted information with zeros when the secure_delete
@@ -54353,11 +54671,11 @@
54353
54354 /* Rollback any active transaction and free the handle structure.
54355 ** The call to sqlite3BtreeRollback() drops any table-locks held by
54356 ** this handle.
54357 */
54358 sqlite3BtreeRollback(p, SQLITE_OK);
54359 sqlite3BtreeLeave(p);
54360
54361 /* If there are still other outstanding references to the shared-btree
54362 ** structure, return now. The remainder of this procedure cleans
54363 ** up the shared-btree.
@@ -55646,31 +55964,32 @@
55646 return rc;
55647 }
55648
55649 /*
55650 ** This routine sets the state to CURSOR_FAULT and the error
55651 ** code to errCode for every cursor on BtShared that pBtree
55652 ** references.
55653 **
55654 ** Every cursor is tripped, including cursors that belong
55655 ** to other database connections that happen to be sharing
55656 ** the cache with pBtree.
55657 **
55658 ** This routine gets called when a rollback occurs.
55659 ** All cursors using the same cache must be tripped
55660 ** to prevent them from trying to use the btree after
55661 ** the rollback. The rollback may have deleted tables
55662 ** or moved root pages, so it is not sufficient to
55663 ** save the state of the cursor. The cursor must be
55664 ** invalidated.
55665 */
55666 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
55667 BtCursor *p;
 
55668 if( pBtree==0 ) return;
55669 sqlite3BtreeEnter(pBtree);
55670 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
55671 int i;
 
55672 sqlite3BtreeClearCursor(p);
55673 p->eState = CURSOR_FAULT;
55674 p->skipNext = errCode;
55675 for(i=0; i<=p->iPage; i++){
55676 releasePage(p->apPage[i]);
@@ -55679,31 +55998,36 @@
55679 }
55680 sqlite3BtreeLeave(pBtree);
55681 }
55682
55683 /*
55684 ** Rollback the transaction in progress. All cursors will be
55685 ** invalided by this operation. Any attempt to use a cursor
55686 ** that was open at the beginning of this operation will result
55687 ** in an error.
 
 
55688 **
55689 ** This will release the write lock on the database file. If there
55690 ** are no active cursors, it also releases the read lock.
55691 */
55692 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
55693 int rc;
55694 BtShared *pBt = p->pBt;
55695 MemPage *pPage1;
55696
 
 
55697 sqlite3BtreeEnter(p);
55698 if( tripCode==SQLITE_OK ){
55699 rc = tripCode = saveAllCursors(pBt, 0, 0);
 
55700 }else{
55701 rc = SQLITE_OK;
55702 }
55703 if( tripCode ){
55704 sqlite3BtreeTripAllCursors(p, tripCode);
55705 }
55706 btreeIntegrity(p);
55707
55708 if( p->inTrans==TRANS_WRITE ){
55709 int rc2;
@@ -58120,49 +58444,266 @@
58120 #endif
58121 }
58122 }
58123
58124 /*
58125 ** Add a list of cells to a page. The page should be initially empty.
58126 ** The cells are guaranteed to fit on the page.
58127 */
58128 static void assemblePage(
58129 MemPage *pPage, /* The page to be assembled */
58130 int nCell, /* The number of cells to add to this page */
58131 u8 **apCell, /* Pointers to cell bodies */
58132 u16 *aSize /* Sizes of the cells */
58133 ){
58134 int i; /* Loop counter */
58135 u8 *pCellptr; /* Address of next cell pointer */
58136 int cellbody; /* Address of next cell body */
58137 u8 * const data = pPage->aData; /* Pointer to data for pPage */
58138 const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
58139 const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
58140
58141 assert( pPage->nOverflow==0 );
58142 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
58143 assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
58144 && (int)MX_CELL(pPage->pBt)<=10921);
58145 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
58146
58147 /* Check that the page has just been zeroed by zeroPage() */
58148 assert( pPage->nCell==0 );
58149 assert( get2byteNotZero(&data[hdr+5])==nUsable );
58150
58151 pCellptr = &pPage->aCellIdx[nCell*2];
58152 cellbody = nUsable;
58153 for(i=nCell-1; i>=0; i--){
58154 u16 sz = aSize[i];
58155 pCellptr -= 2;
58156 cellbody -= sz;
58157 put2byte(pCellptr, cellbody);
58158 memcpy(&data[cellbody], apCell[i], sz);
58159 }
58160 put2byte(&data[hdr+3], nCell);
58161 put2byte(&data[hdr+5], cellbody);
58162 pPage->nFree -= (nCell*2 + nUsable - cellbody);
58163 pPage->nCell = (u16)nCell;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58164 }
58165
58166 /*
58167 ** The following parameters determine how many adjacent pages get involved
58168 ** in a balancing operation. NN is the number of neighbors on either side
@@ -58230,11 +58771,12 @@
58230 u8 *pStop;
58231
58232 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
58233 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
58234 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
58235 assemblePage(pNew, 1, &pCell, &szCell);
 
58236
58237 /* If this is an auto-vacuum database, update the pointer map
58238 ** with entries for the new page, and any pointer from the
58239 ** cell on the page to an overflow page. If either of these
58240 ** operations fails, the return code is set, but the contents
@@ -58449,21 +58991,26 @@
58449 int subtotal; /* Subtotal of bytes in cells on one page */
58450 int iSpace1 = 0; /* First unused byte of aSpace1[] */
58451 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
58452 int szScratch; /* Size of scratch memory requested */
58453 MemPage *apOld[NB]; /* pPage and up to two siblings */
58454 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
58455 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
58456 u8 *pRight; /* Location in parent of right-sibling pointer */
58457 u8 *apDiv[NB-1]; /* Divider cells in pParent */
58458 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
58459 int szNew[NB+2]; /* Combined size of cells place on i-th page */
 
58460 u8 **apCell = 0; /* All cells begin balanced */
58461 u16 *szCell; /* Local size of all cells in apCell[] */
58462 u8 *aSpace1; /* Space for copies of dividers cells */
58463 Pgno pgno; /* Temp var to store a page number in */
 
 
 
 
58464
 
58465 pBt = pParent->pBt;
58466 assert( sqlite3_mutex_held(pBt->mutex) );
58467 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
58468
58469 #if 0
@@ -58568,16 +59115,18 @@
58568 nMaxCells = (nMaxCells + 3)&~3;
58569
58570 /*
58571 ** Allocate space for memory structures
58572 */
58573 k = pBt->pageSize + ROUND8(sizeof(MemPage));
58574 szScratch =
58575 nMaxCells*sizeof(u8*) /* apCell */
58576 + nMaxCells*sizeof(u16) /* szCell */
58577 + pBt->pageSize /* aSpace1 */
58578 + k*nOld; /* Page copies (apCopy) */
 
 
 
58579 apCell = sqlite3ScratchMalloc( szScratch );
58580 if( apCell==0 ){
58581 rc = SQLITE_NOMEM;
58582 goto balance_cleanup;
58583 }
@@ -58586,12 +59135,12 @@
58586 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
58587
58588 /*
58589 ** Load pointers to all cells on sibling pages and the divider cells
58590 ** into the local apCell[] array. Make copies of the divider cells
58591 ** into space obtained from aSpace1[] and remove the divider cells
58592 ** from pParent.
58593 **
58594 ** If the siblings are on leaf pages, then the child pointers of the
58595 ** divider cells are stripped from the cells before they are copied
58596 ** into aSpace1[]. In this way, all cells in apCell[] are without
58597 ** child pointers. If siblings are not leaves, then all cell in
@@ -58603,19 +59152,11 @@
58603 */
58604 leafCorrection = apOld[0]->leaf*4;
58605 leafData = apOld[0]->intKeyLeaf;
58606 for(i=0; i<nOld; i++){
58607 int limit;
58608
58609 /* Before doing anything else, take a copy of the i'th original sibling
58610 ** The rest of this function will use data from the copies rather
58611 ** that the original pages since the original pages will be in the
58612 ** process of being overwritten. */
58613 MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
58614 memcpy(pOld, apOld[i], sizeof(MemPage));
58615 pOld->aData = (void*)&pOld[1];
58616 memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
58617
58618 limit = pOld->nCell+pOld->nOverflow;
58619 if( pOld->nOverflow>0 ){
58620 for(j=0; j<limit; j++){
58621 assert( nCell<nMaxCells );
@@ -58632,10 +59173,11 @@
58632 apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
58633 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
58634 nCell++;
58635 }
58636 }
 
58637 if( i<nOld-1 && !leafData){
58638 u16 sz = (u16)szNew[i];
58639 u8 *pTemp;
58640 assert( nCell<nMaxCells );
58641 szCell[nCell] = sz;
@@ -58683,11 +59225,11 @@
58683 usableSpace = pBt->usableSize - 12 + leafCorrection;
58684 for(subtotal=k=i=0; i<nCell; i++){
58685 assert( i<nMaxCells );
58686 subtotal += szCell[i] + 2;
58687 if( subtotal > usableSpace ){
58688 szNew[k] = subtotal - szCell[i];
58689 cntNew[k] = i;
58690 if( leafData ){ i--; }
58691 subtotal = 0;
58692 k++;
58693 if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
@@ -58697,13 +59239,14 @@
58697 cntNew[k] = nCell;
58698 k++;
58699
58700 /*
58701 ** The packing computed by the previous block is biased toward the siblings
58702 ** on the left side. The left siblings are always nearly full, while the
58703 ** right-most sibling might be nearly empty. This block of code attempts
58704 ** to adjust the packing of siblings to get a better balance.
 
58705 **
58706 ** This adjustment is more than an optimization. The packing above might
58707 ** be so out of balance as to be illegal. For example, the right-most
58708 ** sibling might be completely empty. This adjustment is not optional.
58709 */
@@ -58728,26 +59271,22 @@
58728 }
58729 szNew[i] = szRight;
58730 szNew[i-1] = szLeft;
58731 }
58732
58733 /* Either we found one or more cells (cntnew[0])>0) or pPage is
58734 ** a virtual root page. A virtual root page is when the real root
58735 ** page is page 1 and we are the only child of that page.
58736 **
58737 ** UPDATE: The assert() below is not necessarily true if the database
58738 ** file is corrupt. The corruption will be detected and reported later
58739 ** in this procedure so there is no need to act upon it now.
58740 */
58741 #if 0
58742 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
58743 #endif
58744
58745 TRACE(("BALANCE: old: %d %d %d ",
58746 apOld[0]->pgno,
58747 nOld>=2 ? apOld[1]->pgno : 0,
58748 nOld>=3 ? apOld[2]->pgno : 0
58749 ));
58750
58751 /*
58752 ** Allocate k new pages. Reuse old pages where possible.
58753 */
@@ -58766,12 +59305,14 @@
58766 if( rc ) goto balance_cleanup;
58767 }else{
58768 assert( i>0 );
58769 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
58770 if( rc ) goto balance_cleanup;
 
58771 apNew[i] = pNew;
58772 nNew++;
 
58773
58774 /* Set the pointer-map entry for the new sibling page. */
58775 if( ISAUTOVACUUM ){
58776 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
58777 if( rc!=SQLITE_OK ){
@@ -58779,139 +59320,247 @@
58779 }
58780 }
58781 }
58782 }
58783
58784 /* Free any old pages that were not reused as new pages.
58785 */
58786 while( i<nOld ){
58787 freePage(apOld[i], &rc);
58788 if( rc ) goto balance_cleanup;
58789 releasePage(apOld[i]);
58790 apOld[i] = 0;
58791 i++;
58792 }
58793
58794 /*
58795 ** Put the new pages in ascending order. This helps to
58796 ** keep entries in the disk file in order so that a scan
58797 ** of the table is a linear scan through the file. That
58798 ** in turn helps the operating system to deliver pages
58799 ** from the disk more rapidly.
58800 **
58801 ** An O(n^2) insertion sort algorithm is used, but since
58802 ** n is never more than NB (a small constant), that should
58803 ** not be a problem.
58804 **
58805 ** When NB==3, this one optimization makes the database
58806 ** about 25% faster for large insertions and deletions.
58807 */
58808 for(i=0; i<k-1; i++){
58809 int minV = apNew[i]->pgno;
58810 int minI = i;
58811 for(j=i+1; j<k; j++){
58812 if( apNew[j]->pgno<(unsigned)minV ){
58813 minI = j;
58814 minV = apNew[j]->pgno;
58815 }
58816 }
58817 if( minI>i ){
58818 MemPage *pT;
58819 pT = apNew[i];
58820 apNew[i] = apNew[minI];
58821 apNew[minI] = pT;
58822 }
58823 }
58824 TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
58825 apNew[0]->pgno, szNew[0],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58826 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
 
58827 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
 
58828 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
58829 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
 
 
 
58830
58831 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
58832 put4byte(pRight, apNew[nNew-1]->pgno);
58833
58834 /*
58835 ** Evenly distribute the data in apCell[] across the new pages.
58836 ** Insert divider cells into pParent as necessary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58837 */
58838 j = 0;
58839 for(i=0; i<nNew; i++){
58840 /* Assemble the new sibling page. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58841 MemPage *pNew = apNew[i];
58842 assert( j<nMaxCells );
58843 zeroPage(pNew, pageFlags);
58844 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
58845 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
58846 assert( pNew->nOverflow==0 );
58847
58848 j = cntNew[i];
58849
58850 /* If the sibling page assembled above was not the right-most sibling,
58851 ** insert a divider cell into the parent page.
58852 */
58853 assert( i<nNew-1 || j==nCell );
58854 if( j<nCell ){
58855 u8 *pCell;
58856 u8 *pTemp;
58857 int sz;
58858
58859 assert( j<nMaxCells );
58860 pCell = apCell[j];
58861 sz = szCell[j] + leafCorrection;
58862 pTemp = &aOvflSpace[iOvflSpace];
58863 if( !pNew->leaf ){
58864 memcpy(&pNew->aData[8], pCell, 4);
58865 }else if( leafData ){
58866 /* If the tree is a leaf-data tree, and the siblings are leaves,
58867 ** then there is no divider cell in apCell[]. Instead, the divider
58868 ** cell consists of the integer key for the right-most cell of
58869 ** the sibling-page assembled above only.
58870 */
58871 CellInfo info;
58872 j--;
58873 btreeParseCellPtr(pNew, apCell[j], &info);
58874 pCell = pTemp;
58875 sz = 4 + putVarint(&pCell[4], info.nKey);
58876 pTemp = 0;
58877 }else{
58878 pCell -= 4;
58879 /* Obscure case for non-leaf-data trees: If the cell at pCell was
58880 ** previously stored on a leaf node, and its reported size was 4
58881 ** bytes, then it may actually be smaller than this
58882 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
58883 ** any cell). But it is important to pass the correct size to
58884 ** insertCell(), so reparse the cell now.
58885 **
58886 ** Note that this can never happen in an SQLite data file, as all
58887 ** cells are at least 4 bytes. It only happens in b-trees used
58888 ** to evaluate "IN (SELECT ...)" and similar clauses.
58889 */
58890 if( szCell[j]==4 ){
58891 assert(leafCorrection==4);
58892 sz = cellSizePtr(pParent, pCell);
58893 }
58894 }
58895 iOvflSpace += sz;
58896 assert( sz<=pBt->maxLocal+23 );
58897 assert( iOvflSpace <= (int)pBt->pageSize );
58898 insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
58899 if( rc!=SQLITE_OK ) goto balance_cleanup;
58900 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
58901
58902 j++;
58903 nxDiv++;
58904 }
58905 }
58906 assert( j==nCell );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58907 assert( nOld>0 );
58908 assert( nNew>0 );
58909 if( (pageFlags & PTF_LEAF)==0 ){
58910 u8 *zChild = &apCopy[nOld-1]->aData[8];
58911 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
58912 }
58913
58914 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
58915 /* The root page of the b-tree now contains no cells. The only sibling
58916 ** page is the right-child of the parent. Copy the contents of the
58917 ** child page into the parent, decreasing the overall height of the
@@ -58920,130 +59569,54 @@
58920 **
58921 ** If this is an auto-vacuum database, the call to copyNodeContent()
58922 ** sets all pointer-map entries corresponding to database image pages
58923 ** for which the pointer is stored within the content being copied.
58924 **
58925 ** The second assert below verifies that the child page is defragmented
58926 ** (it must be, as it was just reconstructed using assemblePage()). This
58927 ** is important if the parent page happens to be page 1 of the database
58928 ** image. */
 
58929 assert( nNew==1 );
 
 
58930 assert( apNew[0]->nFree ==
58931 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
 
58932 );
58933 copyNodeContent(apNew[0], pParent, &rc);
58934 freePage(apNew[0], &rc);
58935 }else if( ISAUTOVACUUM ){
58936 /* Fix the pointer-map entries for all the cells that were shifted around.
58937 ** There are several different types of pointer-map entries that need to
58938 ** be dealt with by this routine. Some of these have been set already, but
58939 ** many have not. The following is a summary:
58940 **
58941 ** 1) The entries associated with new sibling pages that were not
58942 ** siblings when this function was called. These have already
58943 ** been set. We don't need to worry about old siblings that were
58944 ** moved to the free-list - the freePage() code has taken care
58945 ** of those.
58946 **
58947 ** 2) The pointer-map entries associated with the first overflow
58948 ** page in any overflow chains used by new divider cells. These
58949 ** have also already been taken care of by the insertCell() code.
58950 **
58951 ** 3) If the sibling pages are not leaves, then the child pages of
58952 ** cells stored on the sibling pages may need to be updated.
58953 **
58954 ** 4) If the sibling pages are not internal intkey nodes, then any
58955 ** overflow pages used by these cells may need to be updated
58956 ** (internal intkey nodes never contain pointers to overflow pages).
58957 **
58958 ** 5) If the sibling pages are not leaves, then the pointer-map
58959 ** entries for the right-child pages of each sibling may need
58960 ** to be updated.
58961 **
58962 ** Cases 1 and 2 are dealt with above by other code. The next
58963 ** block deals with cases 3 and 4 and the one after that, case 5. Since
58964 ** setting a pointer map entry is a relatively expensive operation, this
58965 ** code only sets pointer map entries for child or overflow pages that have
58966 ** actually moved between pages. */
58967 MemPage *pNew = apNew[0];
58968 MemPage *pOld = apCopy[0];
58969 int nOverflow = pOld->nOverflow;
58970 int iNextOld = pOld->nCell + nOverflow;
58971 int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
58972 j = 0; /* Current 'old' sibling page */
58973 k = 0; /* Current 'new' sibling page */
58974 for(i=0; i<nCell; i++){
58975 int isDivider = 0;
58976 while( i==iNextOld ){
58977 /* Cell i is the cell immediately following the last cell on old
58978 ** sibling page j. If the siblings are not leaf pages of an
58979 ** intkey b-tree, then cell i was a divider cell. */
58980 assert( j+1 < ArraySize(apCopy) );
58981 assert( j+1 < nOld );
58982 pOld = apCopy[++j];
58983 iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
58984 if( pOld->nOverflow ){
58985 nOverflow = pOld->nOverflow;
58986 iOverflow = i + !leafData + pOld->aiOvfl[0];
58987 }
58988 isDivider = !leafData;
58989 }
58990
58991 assert(nOverflow>0 || iOverflow<i );
58992 assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
58993 assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
58994 if( i==iOverflow ){
58995 isDivider = 1;
58996 if( (--nOverflow)>0 ){
58997 iOverflow++;
58998 }
58999 }
59000
59001 if( i==cntNew[k] ){
59002 /* Cell i is the cell immediately following the last cell on new
59003 ** sibling page k. If the siblings are not leaf pages of an
59004 ** intkey b-tree, then cell i is a divider cell. */
59005 pNew = apNew[++k];
59006 if( !leafData ) continue;
59007 }
59008 assert( j<nOld );
59009 assert( k<nNew );
59010
59011 /* If the cell was originally divider cell (and is not now) or
59012 ** an overflow cell, or if the cell was located on a different sibling
59013 ** page before the balancing, then the pointer map entries associated
59014 ** with any child or overflow pages need to be updated. */
59015 if( isDivider || pOld->pgno!=pNew->pgno ){
59016 if( !leafCorrection ){
59017 ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
59018 }
59019 if( szCell[i]>pNew->minLocal ){
59020 ptrmapPutOvflPtr(pNew, apCell[i], &rc);
59021 }
59022 }
59023 }
59024
59025 if( !leafCorrection ){
59026 for(i=0; i<nNew; i++){
59027 u32 key = get4byte(&apNew[i]->aData[8]);
59028 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
59029 }
59030 }
59031
59032 #if 0
 
59033 /* The ptrmapCheckPages() contains assert() statements that verify that
59034 ** all pointer map pages are set correctly. This is helpful while
59035 ** debugging. This is usually disabled because a corrupt database may
59036 ** cause an assert() statement to fail. */
59037 ptrmapCheckPages(apNew, nNew);
59038 ptrmapCheckPages(&pParent, 1);
 
59039 #endif
59040 }
59041
59042 assert( pParent->isInit );
59043 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
59044 nOld, nNew, nCell));
59045
59046 /*
59047 ** Cleanup before returning.
59048 */
59049 balance_cleanup:
@@ -60865,10 +61438,15 @@
60865 */
60866 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
60867 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
60868 }
60869
 
 
 
 
 
60870 /************** End of btree.c ***********************************************/
60871 /************** Begin file backup.c ******************************************/
60872 /*
60873 ** 2009 January 28
60874 **
@@ -61004,10 +61582,17 @@
61004 const char *zDestDb, /* Name of database within pDestDb */
61005 sqlite3* pSrcDb, /* Database connection to read from */
61006 const char *zSrcDb /* Name of database within pSrcDb */
61007 ){
61008 sqlite3_backup *p; /* Value to return */
 
 
 
 
 
 
 
61009
61010 /* Lock the source database handle. The destination database
61011 ** handle is not locked in this routine, but it is locked in
61012 ** sqlite3_backup_step(). The user is required to ensure that no
61013 ** other thread accesses the destination handle for the duration
@@ -61201,10 +61786,13 @@
61201 int rc;
61202 int destMode; /* Destination journal mode */
61203 int pgszSrc = 0; /* Source page size */
61204 int pgszDest = 0; /* Destination page size */
61205
 
 
 
61206 sqlite3_mutex_enter(p->pSrcDb->mutex);
61207 sqlite3BtreeEnter(p->pSrc);
61208 if( p->pDestDb ){
61209 sqlite3_mutex_enter(p->pDestDb->mutex);
61210 }
@@ -61464,11 +62052,11 @@
61464 }
61465 *pp = p->pNext;
61466 }
61467
61468 /* If a transaction is still open on the Btree, roll it back. */
61469 sqlite3BtreeRollback(p->pDest, SQLITE_OK);
61470
61471 /* Set the error code of the destination database handle. */
61472 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
61473 if( p->pDestDb ){
61474 sqlite3Error(p->pDestDb, rc);
@@ -61490,18 +62078,30 @@
61490 /*
61491 ** Return the number of pages still to be backed up as of the most recent
61492 ** call to sqlite3_backup_step().
61493 */
61494 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
 
 
 
 
 
 
61495 return p->nRemaining;
61496 }
61497
61498 /*
61499 ** Return the total number of pages in the source database as of the most
61500 ** recent call to sqlite3_backup_step().
61501 */
61502 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
 
 
 
 
 
 
61503 return p->nPagecount;
61504 }
61505
61506 /*
61507 ** This function is called after the contents of page iPage of the
@@ -63788,10 +64388,38 @@
63788 }
63789 p->nOp += nOp;
63790 }
63791 return addr;
63792 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63793
63794 /*
63795 ** Change the value of the P1 operand for a specific instruction.
63796 ** This routine is useful when a large program is loaded from a
63797 ** static array using sqlite3VdbeAddOpList but we want to make a
@@ -64887,10 +65515,13 @@
64887 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
64888 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
64889 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
64890 &zCsr, zEnd, &nByte);
64891 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
 
 
 
64892 if( nByte ){
64893 p->pFree = sqlite3DbMallocZero(db, nByte);
64894 }
64895 zCsr = p->pFree;
64896 zEnd = &zCsr[nByte];
@@ -64954,10 +65585,13 @@
64954 ** is used, for example, when a trigger sub-program is halted to restore
64955 ** control to the main program.
64956 */
64957 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
64958 Vdbe *v = pFrame->v;
 
 
 
64959 v->aOnceFlag = pFrame->aOnceFlag;
64960 v->nOnceFlag = pFrame->nOnceFlag;
64961 v->aOp = pFrame->aOp;
64962 v->nOp = pFrame->nOp;
64963 v->aMem = pFrame->aMem;
@@ -64964,10 +65598,11 @@
64964 v->nMem = pFrame->nMem;
64965 v->apCsr = pFrame->apCsr;
64966 v->nCursor = pFrame->nCursor;
64967 v->db->lastRowid = pFrame->lastRowid;
64968 v->nChange = pFrame->nChange;
 
64969 return pFrame->pc;
64970 }
64971
64972 /*
64973 ** Close all cursors.
@@ -65531,10 +66166,11 @@
65531 ** so, abort any other statements this handle currently has active.
65532 */
65533 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
65534 sqlite3CloseSavepoints(db);
65535 db->autoCommit = 1;
 
65536 }
65537 }
65538 }
65539
65540 /* Check for immediate foreign key violations. */
@@ -65571,18 +66207,20 @@
65571 sqlite3VdbeLeave(p);
65572 return SQLITE_BUSY;
65573 }else if( rc!=SQLITE_OK ){
65574 p->rc = rc;
65575 sqlite3RollbackAll(db, SQLITE_OK);
 
65576 }else{
65577 db->nDeferredCons = 0;
65578 db->nDeferredImmCons = 0;
65579 db->flags &= ~SQLITE_DeferFKs;
65580 sqlite3CommitInternalChanges(db);
65581 }
65582 }else{
65583 sqlite3RollbackAll(db, SQLITE_OK);
 
65584 }
65585 db->nStatement = 0;
65586 }else if( eStatementOp==0 ){
65587 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
65588 eStatementOp = SAVEPOINT_RELEASE;
@@ -65590,10 +66228,11 @@
65590 eStatementOp = SAVEPOINT_ROLLBACK;
65591 }else{
65592 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
65593 sqlite3CloseSavepoints(db);
65594 db->autoCommit = 1;
 
65595 }
65596 }
65597
65598 /* If eStatementOp is non-zero, then a statement transaction needs to
65599 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
@@ -65610,10 +66249,11 @@
65610 p->zErrMsg = 0;
65611 }
65612 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
65613 sqlite3CloseSavepoints(db);
65614 db->autoCommit = 1;
 
65615 }
65616 }
65617
65618 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
65619 ** has been rolled back, update the database connection change-counter.
@@ -65871,10 +66511,16 @@
65871 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
65872 vdbeFreeOpArray(db, p->aOp, p->nOp);
65873 sqlite3DbFree(db, p->aColName);
65874 sqlite3DbFree(db, p->zSql);
65875 sqlite3DbFree(db, p->pFree);
 
 
 
 
 
 
65876 }
65877
65878 /*
65879 ** Delete an entire VDBE.
65880 */
@@ -68238,15 +68884,23 @@
68238 sqlite3_stmt *pStmt,
68239 int N,
68240 const void *(*xFunc)(Mem*),
68241 int useType
68242 ){
68243 const void *ret = 0;
68244 Vdbe *p = (Vdbe *)pStmt;
68245 int n;
68246 sqlite3 *db = p->db;
68247
 
 
 
 
 
 
 
 
68248 assert( db!=0 );
68249 n = sqlite3_column_count(pStmt);
68250 if( N<n && N>=0 ){
68251 N += useType*n;
68252 sqlite3_mutex_enter(db->mutex);
@@ -68707,10 +69361,16 @@
68707 ** prepared statement for the database connection. Return NULL if there
68708 ** are no more.
68709 */
68710 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
68711 sqlite3_stmt *pNext;
 
 
 
 
 
 
68712 sqlite3_mutex_enter(pDb->mutex);
68713 if( pStmt==0 ){
68714 pNext = (sqlite3_stmt*)pDb->pVdbe;
68715 }else{
68716 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
@@ -68722,15 +69382,91 @@
68722 /*
68723 ** Return the value of a status counter for a prepared statement
68724 */
68725 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
68726 Vdbe *pVdbe = (Vdbe*)pStmt;
68727 u32 v = pVdbe->aCounter[op];
 
 
 
 
 
 
 
68728 if( resetFlag ) pVdbe->aCounter[op] = 0;
68729 return (int)v;
68730 }
68731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68732 /************** End of vdbeapi.c *********************************************/
68733 /************** Begin file vdbetrace.c ***************************************/
68734 /*
68735 ** 2009 November 25
68736 **
@@ -69612,10 +70348,13 @@
69612 #ifdef VDBE_PROFILE
69613 start = sqlite3Hwtime();
69614 #endif
69615 nVmStep++;
69616 pOp = &aOp[pc];
 
 
 
69617
69618 /* Only allow tracing if SQLITE_DEBUG is defined.
69619 */
69620 #ifdef SQLITE_DEBUG
69621 if( db->flags & SQLITE_VdbeTrace ){
@@ -71302,11 +72041,11 @@
71302 assert( pReg->flags & MEM_Blob );
71303 assert( memIsValid(pReg) );
71304 pC->payloadSize = pC->szRow = avail = pReg->n;
71305 pC->aRow = (u8*)pReg->z;
71306 }else{
71307 MemSetTypeFlag(pDest, MEM_Null);
71308 goto op_column_out;
71309 }
71310 }else{
71311 assert( pCrsr );
71312 if( pC->isTable==0 ){
@@ -71826,23 +72565,28 @@
71826 goto vdbe_return;
71827 }
71828 db->isTransactionSavepoint = 0;
71829 rc = p->rc;
71830 }else{
 
71831 iSavepoint = db->nSavepoint - iSavepoint - 1;
71832 if( p1==SAVEPOINT_ROLLBACK ){
 
71833 for(ii=0; ii<db->nDb; ii++){
71834 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
 
71835 }
 
 
71836 }
71837 for(ii=0; ii<db->nDb; ii++){
71838 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
71839 if( rc!=SQLITE_OK ){
71840 goto abort_due_to_error;
71841 }
71842 }
71843 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
71844 sqlite3ExpirePreparedStatements(db);
71845 sqlite3ResetAllSchemasOfConnection(db);
71846 db->flags = (db->flags | SQLITE_InternChanges);
71847 }
71848 }
@@ -72235,11 +72979,11 @@
72235 assert( p->bIsReader );
72236 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
72237 || p->readOnly==0 );
72238
72239 if( p->expired ){
72240 rc = SQLITE_ABORT;
72241 break;
72242 }
72243
72244 nField = 0;
72245 pKeyInfo = 0;
@@ -72799,14 +73543,15 @@
72799 }
72800 pIdxKey = &r;
72801 }else{
72802 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
72803 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
72804 );
72805 if( pIdxKey==0 ) goto no_mem;
72806 assert( pIn3->flags & MEM_Blob );
72807 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
 
72808 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
72809 }
72810 pIdxKey->default_rc = 0;
72811 if( pOp->opcode==OP_NoConflict ){
72812 /* For the OP_NoConflict opcode, take the jump if any of the
@@ -73492,13 +74237,13 @@
73492 }
73493 /* Opcode: Rewind P1 P2 * * *
73494 **
73495 ** The next use of the Rowid or Column or Next instruction for P1
73496 ** will refer to the first entry in the database table or index.
73497 ** If the table or index is empty and P2>0, then jump immediately to P2.
73498 ** If P2 is 0 or if the table or index is not empty, fall through
73499 ** to the following instruction.
73500 **
73501 ** This opcode leaves the cursor configured to move in forward order,
73502 ** from the beginning toward the end. In other words, the cursor is
73503 ** configured to use Next, not Prev.
73504 */
@@ -74410,10 +75155,13 @@
74410 pFrame->aOp = p->aOp;
74411 pFrame->nOp = p->nOp;
74412 pFrame->token = pProgram->token;
74413 pFrame->aOnceFlag = p->aOnceFlag;
74414 pFrame->nOnceFlag = p->nOnceFlag;
 
 
 
74415
74416 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
74417 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
74418 pMem->flags = MEM_Undefined;
74419 pMem->db = db;
@@ -74427,10 +75175,11 @@
74427
74428 p->nFrame++;
74429 pFrame->pParent = p->pFrame;
74430 pFrame->lastRowid = lastRowid;
74431 pFrame->nChange = p->nChange;
 
74432 p->nChange = 0;
74433 p->pFrame = pFrame;
74434 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
74435 p->nMem = pFrame->nChildMem;
74436 p->nCursor = (u16)pFrame->nChildCsr;
@@ -74437,10 +75186,13 @@
74437 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
74438 p->aOp = aOp = pProgram->aOp;
74439 p->nOp = pProgram->nOp;
74440 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
74441 p->nOnceFlag = pProgram->nOnce;
 
 
 
74442 pc = -1;
74443 memset(p->aOnceFlag, 0, p->nOnceFlag);
74444
74445 break;
74446 }
@@ -75625,10 +76377,15 @@
75625 char *zErr = 0;
75626 Table *pTab;
75627 Parse *pParse = 0;
75628 Incrblob *pBlob = 0;
75629
 
 
 
 
 
75630 flags = !!flags; /* flags = (flags ? 1 : 0); */
75631 *ppBlob = 0;
75632
75633 sqlite3_mutex_enter(db->mutex);
75634
@@ -75843,11 +76600,10 @@
75843 v = (Vdbe*)p->pStmt;
75844
75845 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
75846 /* Request is out of range. Return a transient error. */
75847 rc = SQLITE_ERROR;
75848 sqlite3Error(db, SQLITE_ERROR);
75849 }else if( v==0 ){
75850 /* If there is no statement handle, then the blob-handle has
75851 ** already been invalidated. Return SQLITE_ABORT in this case.
75852 */
75853 rc = SQLITE_ABORT;
@@ -75861,14 +76617,14 @@
75861 sqlite3BtreeLeaveCursor(p->pCsr);
75862 if( rc==SQLITE_ABORT ){
75863 sqlite3VdbeFinalize(v);
75864 p->pStmt = 0;
75865 }else{
75866 db->errCode = rc;
75867 v->rc = rc;
75868 }
75869 }
 
75870 rc = sqlite3ApiExit(db, rc);
75871 sqlite3_mutex_leave(db->mutex);
75872 return rc;
75873 }
75874
@@ -76041,11 +76797,11 @@
76041 ** itself.
76042 **
76043 ** The sorter is running in multi-threaded mode if (a) the library was built
76044 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
76045 ** than zero, and (b) worker threads have been enabled at runtime by calling
76046 ** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...).
76047 **
76048 ** When Rewind() is called, any data remaining in memory is flushed to a
76049 ** final PMA. So at this point the data is stored in some number of sorted
76050 ** PMAs within temporary files on disk.
76051 **
@@ -76786,15 +77542,13 @@
76786 pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
76787 mxCache = db->aDb[0].pSchema->cache_size;
76788 if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
76789 pSorter->mxPmaSize = mxCache * pgsz;
76790
76791 /* If the application has not configure scratch memory using
76792 ** SQLITE_CONFIG_SCRATCH then we assume it is OK to do large memory
76793 ** allocations. If scratch memory has been configured, then assume
76794 ** large memory allocations should be avoided to prevent heap
76795 ** fragmentation.
76796 */
76797 if( sqlite3GlobalConfig.pScratch==0 ){
76798 assert( pSorter->iMemory==0 );
76799 pSorter->nMemory = pgsz;
76800 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
@@ -79162,19 +79916,19 @@
79162 **
79163 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
79164 ** is a helper function - a callback for the tree walker.
79165 */
79166 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
79167 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
79168 return WRC_Continue;
79169 }
79170 static void incrAggFunctionDepth(Expr *pExpr, int N){
79171 if( N>0 ){
79172 Walker w;
79173 memset(&w, 0, sizeof(w));
79174 w.xExprCallback = incrAggDepth;
79175 w.u.i = N;
79176 sqlite3WalkExpr(&w, pExpr);
79177 }
79178 }
79179
79180 /*
@@ -79718,11 +80472,11 @@
79718 double r = -1.0;
79719 if( p->op!=TK_FLOAT ) return -1;
79720 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
79721 assert( r>=0.0 );
79722 if( r>1.0 ) return -1;
79723 return (int)(r*1000.0);
79724 }
79725
79726 /*
79727 ** This routine is callback for sqlite3WalkExpr().
79728 **
@@ -79850,11 +80604,11 @@
79850 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
79851 ** likelihood(X,0.9375).
79852 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
79853 ** likelihood(X,0.9375). */
79854 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
79855 pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
79856 }
79857 }
79858 #ifndef SQLITE_OMIT_AUTHORIZATION
79859 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
79860 if( auth!=SQLITE_OK ){
@@ -81807,69 +82561,79 @@
81807 sqlite3DbFree(db, pList->a);
81808 sqlite3DbFree(db, pList);
81809 }
81810
81811 /*
81812 ** These routines are Walker callbacks. Walker.u.pi is a pointer
81813 ** to an integer. These routines are checking an expression to see
81814 ** if it is a constant. Set *Walker.u.i to 0 if the expression is
81815 ** not constant.
81816 **
81817 ** These callback routines are used to implement the following:
81818 **
81819 ** sqlite3ExprIsConstant() pWalker->u.i==1
81820 ** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2
81821 ** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4
 
 
 
 
81822 **
81823 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
81824 ** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing
81825 ** an existing schema and 3 when processing a new statement. A bound
81826 ** parameter raises an error for new statements, but is silently converted
81827 ** to NULL for existing schemas. This allows sqlite_master tables that
81828 ** contain a bound parameter because they were generated by older versions
81829 ** of SQLite to be parsed by newer versions of SQLite without raising a
81830 ** malformed schema error.
81831 */
81832 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
81833
81834 /* If pWalker->u.i is 2 then any term of the expression that comes from
81835 ** the ON or USING clauses of a join disqualifies the expression
81836 ** from being considered constant. */
81837 if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
81838 pWalker->u.i = 0;
81839 return WRC_Abort;
81840 }
81841
81842 switch( pExpr->op ){
81843 /* Consider functions to be constant if all their arguments are constant
81844 ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST
81845 ** flag. */
81846 case TK_FUNCTION:
81847 if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){
81848 return WRC_Continue;
 
 
 
81849 }
81850 /* Fall through */
81851 case TK_ID:
81852 case TK_COLUMN:
81853 case TK_AGG_FUNCTION:
81854 case TK_AGG_COLUMN:
81855 testcase( pExpr->op==TK_ID );
81856 testcase( pExpr->op==TK_COLUMN );
81857 testcase( pExpr->op==TK_AGG_FUNCTION );
81858 testcase( pExpr->op==TK_AGG_COLUMN );
81859 pWalker->u.i = 0;
81860 return WRC_Abort;
 
 
 
 
81861 case TK_VARIABLE:
81862 if( pWalker->u.i==4 ){
81863 /* Silently convert bound parameters that appear inside of CREATE
81864 ** statements into a NULL when parsing the CREATE statement text out
81865 ** of the sqlite_master table */
81866 pExpr->op = TK_NULL;
81867 }else if( pWalker->u.i==3 ){
81868 /* A bound parameter in a CREATE statement that originates from
81869 ** sqlite3_prepare() causes an error */
81870 pWalker->u.i = 0;
81871 return WRC_Abort;
81872 }
81873 /* Fall through */
81874 default:
81875 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
@@ -81877,57 +82641,68 @@
81877 return WRC_Continue;
81878 }
81879 }
81880 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
81881 UNUSED_PARAMETER(NotUsed);
81882 pWalker->u.i = 0;
81883 return WRC_Abort;
81884 }
81885 static int exprIsConst(Expr *p, int initFlag){
81886 Walker w;
81887 memset(&w, 0, sizeof(w));
81888 w.u.i = initFlag;
81889 w.xExprCallback = exprNodeIsConstant;
81890 w.xSelectCallback = selectNodeIsConstant;
 
81891 sqlite3WalkExpr(&w, p);
81892 return w.u.i;
81893 }
81894
81895 /*
81896 ** Walk an expression tree. Return 1 if the expression is constant
81897 ** and 0 if it involves variables or function calls.
81898 **
81899 ** For the purposes of this function, a double-quoted string (ex: "abc")
81900 ** is considered a variable but a single-quoted string (ex: 'abc') is
81901 ** a constant.
81902 */
81903 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
81904 return exprIsConst(p, 1);
81905 }
81906
81907 /*
81908 ** Walk an expression tree. Return 1 if the expression is constant
81909 ** that does no originate from the ON or USING clauses of a join.
81910 ** Return 0 if it involves variables or function calls or terms from
81911 ** an ON or USING clause.
81912 */
81913 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
81914 return exprIsConst(p, 2);
81915 }
81916
81917 /*
81918 ** Walk an expression tree. Return 1 if the expression is constant
 
 
 
 
 
 
 
 
 
 
81919 ** or a function call with constant arguments. Return and 0 if there
81920 ** are any variables.
81921 **
81922 ** For the purposes of this function, a double-quoted string (ex: "abc")
81923 ** is considered a variable but a single-quoted string (ex: 'abc') is
81924 ** a constant.
81925 */
81926 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
81927 assert( isInit==0 || isInit==1 );
81928 return exprIsConst(p, 3+isInit);
81929 }
81930
81931 /*
81932 ** If the expression p codes a constant integer that is small enough
81933 ** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -87374,10 +88149,11 @@
87374 nSample--;
87375 }else{
87376 nRow = pIdx->aiRowEst[0];
87377 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
87378 }
 
87379
87380 /* Set nSum to the number of distinct (iCol+1) field prefixes that
87381 ** occur in the stat4 table for this index. Set sumEq to the sum of
87382 ** the nEq values for column iCol for the same set (adding the value
87383 ** only once where there exist duplicate prefixes). */
@@ -87635,11 +88411,11 @@
87635 }
87636
87637
87638 /* Load the statistics from the sqlite_stat4 table. */
87639 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
87640 if( rc==SQLITE_OK ){
87641 int lookasideEnabled = db->lookaside.bEnabled;
87642 db->lookaside.bEnabled = 0;
87643 rc = loadStat4(db, sInfo.zDatabase);
87644 db->lookaside.bEnabled = lookasideEnabled;
87645 }
@@ -88317,10 +89093,13 @@
88317 SQLITE_API int sqlite3_set_authorizer(
88318 sqlite3 *db,
88319 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
88320 void *pArg
88321 ){
 
 
 
88322 sqlite3_mutex_enter(db->mutex);
88323 db->xAuth = (sqlite3_xauth)xAuth;
88324 db->pAuthArg = pArg;
88325 sqlite3ExpirePreparedStatements(db);
88326 sqlite3_mutex_leave(db->mutex);
@@ -88811,11 +89590,15 @@
88811 ** See also sqlite3LocateTable().
88812 */
88813 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
88814 Table *p = 0;
88815 int i;
88816 assert( zName!=0 );
 
 
 
 
88817 /* All mutexes are required for schema access. Make sure we hold them. */
88818 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
88819 #if SQLITE_USER_AUTHENTICATION
88820 /* Only the admin user is allowed to know that the sqlite_user table
88821 ** exists */
@@ -103834,13 +104617,16 @@
103834 Vdbe *pOld, /* VM being reprepared */
103835 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
103836 const char **pzTail /* OUT: End of parsed string */
103837 ){
103838 int rc;
103839 assert( ppStmt!=0 );
 
 
 
103840 *ppStmt = 0;
103841 if( !sqlite3SafetyCheckOk(db) ){
103842 return SQLITE_MISUSE_BKPT;
103843 }
103844 sqlite3_mutex_enter(db->mutex);
103845 sqlite3BtreeEnterAll(db);
103846 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
@@ -103943,13 +104729,15 @@
103943 */
103944 char *zSql8;
103945 const char *zTail8 = 0;
103946 int rc = SQLITE_OK;
103947
103948 assert( ppStmt );
 
 
103949 *ppStmt = 0;
103950 if( !sqlite3SafetyCheckOk(db) ){
103951 return SQLITE_MISUSE_BKPT;
103952 }
103953 if( nBytes>=0 ){
103954 int sz;
103955 const char *z = (const char*)zSql;
@@ -109658,10 +110446,13 @@
109658 char **pzErrMsg /* Write error messages here */
109659 ){
109660 int rc;
109661 TabResult res;
109662
 
 
 
109663 *pazResult = 0;
109664 if( pnColumn ) *pnColumn = 0;
109665 if( pnRow ) *pnRow = 0;
109666 if( pzErrMsg ) *pzErrMsg = 0;
109667 res.zErrMsg = 0;
@@ -111721,11 +112512,11 @@
111721 ** Two writes per page are required in step (3) because the original
111722 ** database content must be written into the rollback journal prior to
111723 ** overwriting the database with the vacuumed content.
111724 **
111725 ** Only 1x temporary space and only 1x writes would be required if
111726 ** the copy of step (3) were replace by deleting the original database
111727 ** and renaming the transient database as the original. But that will
111728 ** not work if other processes are attached to the original database.
111729 ** And a power loss in between deleting the original and renaming the
111730 ** transient would cause the database file to appear to be deleted
111731 ** following reboot.
@@ -112079,10 +112870,13 @@
112079 sqlite3 *db, /* Database in which module is registered */
112080 const char *zName, /* Name assigned to this module */
112081 const sqlite3_module *pModule, /* The definition of the module */
112082 void *pAux /* Context pointer for xCreate/xConnect */
112083 ){
 
 
 
112084 return createModule(db, zName, pModule, pAux, 0);
112085 }
112086
112087 /*
112088 ** External API function used to create a new virtual-table module.
@@ -112092,10 +112886,13 @@
112092 const char *zName, /* Name assigned to this module */
112093 const sqlite3_module *pModule, /* The definition of the module */
112094 void *pAux, /* Context pointer for xCreate/xConnect */
112095 void (*xDestroy)(void *) /* Module destructor function */
112096 ){
 
 
 
112097 return createModule(db, zName, pModule, pAux, xDestroy);
112098 }
112099
112100 /*
112101 ** Lock the virtual table so that it cannot be disconnected.
@@ -112696,10 +113493,13 @@
112696
112697 int rc = SQLITE_OK;
112698 Table *pTab;
112699 char *zErr = 0;
112700
 
 
 
112701 sqlite3_mutex_enter(db->mutex);
112702 if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
112703 sqlite3Error(db, SQLITE_MISUSE);
112704 sqlite3_mutex_leave(db->mutex);
112705 return SQLITE_MISUSE_BKPT;
@@ -113052,10 +113852,13 @@
113052 */
113053 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
113054 static const unsigned char aMap[] = {
113055 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
113056 };
 
 
 
113057 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
113058 assert( OE_Ignore==4 && OE_Replace==5 );
113059 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
113060 return (int)aMap[db->vtabOnConflict-1];
113061 }
@@ -113067,12 +113870,14 @@
113067 */
113068 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
113069 va_list ap;
113070 int rc = SQLITE_OK;
113071
 
 
 
113072 sqlite3_mutex_enter(db->mutex);
113073
113074 va_start(ap, op);
113075 switch( op ){
113076 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
113077 VtabCtx *p = db->pVtabCtx;
113078 if( !p ){
@@ -113203,10 +114008,13 @@
113203 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
113204 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
113205 } u;
113206 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
113207 Bitmask notReady; /* FROM entries not usable at this level */
 
 
 
113208 };
113209
113210 /*
113211 ** Each instance of this object represents an algorithm for evaluating one
113212 ** term of a join. Every term of the FROM clause will have at least
@@ -113233,11 +114041,10 @@
113233 LogEst rRun; /* Cost of running each loop */
113234 LogEst nOut; /* Estimated number of output rows */
113235 union {
113236 struct { /* Information for internal btree tables */
113237 u16 nEq; /* Number of equality constraints */
113238 u16 nSkip; /* Number of initial index columns to skip */
113239 Index *pIndex; /* Index used, or NULL */
113240 } btree;
113241 struct { /* Information for virtual tables */
113242 int idxNum; /* Index number */
113243 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
@@ -113246,16 +114053,17 @@
113246 char *idxStr; /* Index identifier string */
113247 } vtab;
113248 } u;
113249 u32 wsFlags; /* WHERE_* flags describing the plan */
113250 u16 nLTerm; /* Number of entries in aLTerm[] */
 
113251 /**** whereLoopXfer() copies fields above ***********************/
113252 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
113253 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
113254 WhereTerm **aLTerm; /* WhereTerms used */
113255 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
113256 WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */
113257 };
113258
113259 /* This object holds the prerequisites and the cost of running a
113260 ** subquery on one operand of an OR operator in the WHERE clause.
113261 ** See WhereOrSet for additional information
@@ -113577,10 +114385,11 @@
113577 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
113578 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
113579 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
113580 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
113581 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
 
113582
113583 /************** End of whereInt.h ********************************************/
113584 /************** Continuing where we left off in where.c **********************/
113585
113586 /*
@@ -113787,11 +114596,11 @@
113787 }
113788 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
113789 }
113790 pTerm = &pWC->a[idx = pWC->nTerm++];
113791 if( p && ExprHasProperty(p, EP_Unlikely) ){
113792 pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
113793 }else{
113794 pTerm->truthProb = 1;
113795 }
113796 pTerm->pExpr = sqlite3ExprSkipCollate(p);
113797 pTerm->wtFlags = wtFlags;
@@ -114317,10 +115126,19 @@
114317 if( pDerived ){
114318 pDerived->flags |= pBase->flags & EP_FromJoin;
114319 pDerived->iRightJoinTable = pBase->iRightJoinTable;
114320 }
114321 }
 
 
 
 
 
 
 
 
 
114322
114323 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
114324 /*
114325 ** Analyze a term that consists of two or more OR-connected
114326 ** subterms. So in:
@@ -114615,12 +115433,11 @@
114615 pNew->x.pList = pList;
114616 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
114617 testcase( idxNew==0 );
114618 exprAnalyze(pSrc, pWC, idxNew);
114619 pTerm = &pWC->a[idxTerm];
114620 pWC->a[idxNew].iParent = idxTerm;
114621 pTerm->nChild = 1;
114622 }else{
114623 sqlite3ExprListDelete(db, pList);
114624 }
114625 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
114626 }
@@ -114718,13 +115535,12 @@
114718 return;
114719 }
114720 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
114721 if( idxNew==0 ) return;
114722 pNew = &pWC->a[idxNew];
114723 pNew->iParent = idxTerm;
114724 pTerm = &pWC->a[idxTerm];
114725 pTerm->nChild = 1;
114726 pTerm->wtFlags |= TERM_COPIED;
114727 if( pExpr->op==TK_EQ
114728 && !ExprHasProperty(pExpr, EP_FromJoin)
114729 && OptimizationEnabled(db, SQLITE_Transitive)
114730 ){
@@ -114777,13 +115593,12 @@
114777 transferJoinMarkings(pNewExpr, pExpr);
114778 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
114779 testcase( idxNew==0 );
114780 exprAnalyze(pSrc, pWC, idxNew);
114781 pTerm = &pWC->a[idxTerm];
114782 pWC->a[idxNew].iParent = idxTerm;
114783 }
114784 pTerm->nChild = 2;
114785 }
114786 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
114787
114788 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
114789 /* Analyze a term that is composed of two or more subterms connected by
@@ -114854,13 +115669,12 @@
114854 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
114855 testcase( idxNew2==0 );
114856 exprAnalyze(pSrc, pWC, idxNew2);
114857 pTerm = &pWC->a[idxTerm];
114858 if( isComplete ){
114859 pWC->a[idxNew1].iParent = idxTerm;
114860 pWC->a[idxNew2].iParent = idxTerm;
114861 pTerm->nChild = 2;
114862 }
114863 }
114864 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
114865
114866 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -114889,13 +115703,12 @@
114889 pNewTerm = &pWC->a[idxNew];
114890 pNewTerm->prereqRight = prereqExpr;
114891 pNewTerm->leftCursor = pLeft->iTable;
114892 pNewTerm->u.leftColumn = pLeft->iColumn;
114893 pNewTerm->eOperator = WO_MATCH;
114894 pNewTerm->iParent = idxTerm;
114895 pTerm = &pWC->a[idxTerm];
114896 pTerm->nChild = 1;
114897 pTerm->wtFlags |= TERM_COPIED;
114898 pNewTerm->prereqAll = pTerm->prereqAll;
114899 }
114900 }
114901 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -114912,11 +115725,11 @@
114912 ** the start of the loop will prevent any results from being returned.
114913 */
114914 if( pExpr->op==TK_NOTNULL
114915 && pExpr->pLeft->op==TK_COLUMN
114916 && pExpr->pLeft->iColumn>=0
114917 && OptimizationEnabled(db, SQLITE_Stat3)
114918 ){
114919 Expr *pNewExpr;
114920 Expr *pLeft = pExpr->pLeft;
114921 int idxNew;
114922 WhereTerm *pNewTerm;
@@ -114931,13 +115744,12 @@
114931 pNewTerm = &pWC->a[idxNew];
114932 pNewTerm->prereqRight = 0;
114933 pNewTerm->leftCursor = pLeft->iTable;
114934 pNewTerm->u.leftColumn = pLeft->iColumn;
114935 pNewTerm->eOperator = WO_GT;
114936 pNewTerm->iParent = idxTerm;
114937 pTerm = &pWC->a[idxTerm];
114938 pTerm->nChild = 1;
114939 pTerm->wtFlags |= TERM_COPIED;
114940 pNewTerm->prereqAll = pTerm->prereqAll;
114941 }
114942 }
114943 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
@@ -115153,10 +115965,12 @@
115153 WhereLoop *pLoop; /* The Loop object */
115154 char *zNotUsed; /* Extra space on the end of pIdx */
115155 Bitmask idxCols; /* Bitmap of columns used for indexing */
115156 Bitmask extraCols; /* Bitmap of additional columns */
115157 u8 sentWarning = 0; /* True if a warnning has been issued */
 
 
115158
115159 /* Generate code to skip over the creation and initialization of the
115160 ** transient index on 2nd and subsequent iterations of the loop. */
115161 v = pParse->pVdbe;
115162 assert( v!=0 );
@@ -115168,10 +115982,16 @@
115168 pTable = pSrc->pTab;
115169 pWCEnd = &pWC->a[pWC->nTerm];
115170 pLoop = pLevel->pWLoop;
115171 idxCols = 0;
115172 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 
 
 
 
 
 
115173 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
115174 int iCol = pTerm->u.leftColumn;
115175 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
115176 testcase( iCol==BMS );
115177 testcase( iCol==BMS-1 );
@@ -115180,11 +116000,13 @@
115180 "automatic index on %s(%s)", pTable->zName,
115181 pTable->aCol[iCol].zName);
115182 sentWarning = 1;
115183 }
115184 if( (idxCols & cMask)==0 ){
115185 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
 
 
115186 pLoop->aLTerm[nKeyCol++] = pTerm;
115187 idxCols |= cMask;
115188 }
115189 }
115190 }
@@ -115200,24 +116022,23 @@
115200 ** be a covering index because the index will not be updated if the
115201 ** original table changes and the index and table cannot both be used
115202 ** if they go out of sync.
115203 */
115204 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
115205 mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
115206 testcase( pTable->nCol==BMS-1 );
115207 testcase( pTable->nCol==BMS-2 );
115208 for(i=0; i<mxBitCol; i++){
115209 if( extraCols & MASKBIT(i) ) nKeyCol++;
115210 }
115211 if( pSrc->colUsed & MASKBIT(BMS-1) ){
115212 nKeyCol += pTable->nCol - BMS + 1;
115213 }
115214 pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
115215
115216 /* Construct the Index object to describe this index */
115217 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
115218 if( pIdx==0 ) return;
115219 pLoop->u.btree.pIndex = pIdx;
115220 pIdx->zName = "auto-index";
115221 pIdx->pTable = pTable;
115222 n = 0;
115223 idxCols = 0;
@@ -115265,22 +116086,33 @@
115265 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
115266 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
115267 VdbeComment((v, "for %s", pTable->zName));
115268
115269 /* Fill the automatic index with content */
 
115270 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
 
 
 
 
 
115271 regRecord = sqlite3GetTempReg(pParse);
115272 sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
115273 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
115274 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 
115275 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
115276 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
115277 sqlite3VdbeJumpHere(v, addrTop);
115278 sqlite3ReleaseTempReg(pParse, regRecord);
 
115279
115280 /* Jump here when skipping the initialization */
115281 sqlite3VdbeJumpHere(v, addrInit);
 
 
 
115282 }
115283 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
115284
115285 #ifndef SQLITE_OMIT_VIRTUALTABLE
115286 /*
@@ -115436,22 +116268,22 @@
115436
115437 return pParse->nErr;
115438 }
115439 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
115440
115441
115442 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
115443 /*
115444 ** Estimate the location of a particular key among all keys in an
115445 ** index. Store the results in aStat as follows:
115446 **
115447 ** aStat[0] Est. number of rows less than pVal
115448 ** aStat[1] Est. number of rows equal to pVal
115449 **
115450 ** Return SQLITE_OK on success.
 
115451 */
115452 static void whereKeyStats(
115453 Parse *pParse, /* Database connection */
115454 Index *pIdx, /* Index to consider domain of */
115455 UnpackedRecord *pRec, /* Vector of values to consider */
115456 int roundUp, /* Round up if true. Round down if false */
115457 tRowcnt *aStat /* OUT: stats written here */
@@ -115529,10 +116361,11 @@
115529 }else{
115530 iGap = iGap/3;
115531 }
115532 aStat[0] = iLower + iGap;
115533 }
 
115534 }
115535 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
115536
115537 /*
115538 ** If it is not NULL, pTerm is a term that provides an upper or lower
@@ -115679,11 +116512,11 @@
115679 ** pLower pUpper
115680 **
115681 ** If either of the upper or lower bound is not present, then NULL is passed in
115682 ** place of the corresponding WhereTerm.
115683 **
115684 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
115685 ** column subject to the range constraint. Or, equivalently, the number of
115686 ** equality constraints optimized by the proposed index scan. For example,
115687 ** assuming index p is on t1(a, b), and the SQL query is:
115688 **
115689 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
@@ -115695,11 +116528,11 @@
115695 **
115696 ** then nEq is set to 0.
115697 **
115698 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
115699 ** number of rows that the index scan is expected to visit without
115700 ** considering the range constraints. If nEq is 0, this is the number of
115701 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
115702 ** to account for the range constraints pLower and pUpper.
115703 **
115704 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
115705 ** used, a single range inequality reduces the search space by a factor of 4.
@@ -115719,14 +116552,11 @@
115719
115720 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
115721 Index *p = pLoop->u.btree.pIndex;
115722 int nEq = pLoop->u.btree.nEq;
115723
115724 if( p->nSample>0
115725 && nEq<p->nSampleCol
115726 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
115727 ){
115728 if( nEq==pBuilder->nRecValid ){
115729 UnpackedRecord *pRec = pBuilder->pRec;
115730 tRowcnt a[2];
115731 u8 aff;
115732
@@ -115738,19 +116568,23 @@
115738 **
115739 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
115740 ** is not a simple variable or literal value), the lower bound of the
115741 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
115742 ** if $L is available, whereKeyStats() is called for both ($P) and
115743 ** ($P:$L) and the larger of the two returned values used.
115744 **
115745 ** Similarly, iUpper is to be set to the estimate of the number of rows
115746 ** less than the upper bound of the range query. Where the upper bound
115747 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
115748 ** of iUpper are requested of whereKeyStats() and the smaller used.
 
 
115749 */
115750 tRowcnt iLower;
115751 tRowcnt iUpper;
 
 
115752
115753 if( pRec ){
115754 testcase( pRec->nField!=pBuilder->nRecValid );
115755 pRec->nField = pBuilder->nRecValid;
115756 }
@@ -115760,11 +116594,11 @@
115760 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
115761 }
115762 /* Determine iLower and iUpper using ($P) only. */
115763 if( nEq==0 ){
115764 iLower = 0;
115765 iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
115766 }else{
115767 /* Note: this call could be optimized away - since the same values must
115768 ** have been requested when testing key $P in whereEqualScanEst(). */
115769 whereKeyStats(pParse, p, pRec, 0, a);
115770 iLower = a[0];
@@ -115784,11 +116618,11 @@
115784 int bOk; /* True if value is extracted from pExpr */
115785 Expr *pExpr = pLower->pExpr->pRight;
115786 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
115787 if( rc==SQLITE_OK && bOk ){
115788 tRowcnt iNew;
115789 whereKeyStats(pParse, p, pRec, 0, a);
115790 iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
115791 if( iNew>iLower ) iLower = iNew;
115792 nOut--;
115793 pLower = 0;
115794 }
@@ -115799,11 +116633,11 @@
115799 int bOk; /* True if value is extracted from pExpr */
115800 Expr *pExpr = pUpper->pExpr->pRight;
115801 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
115802 if( rc==SQLITE_OK && bOk ){
115803 tRowcnt iNew;
115804 whereKeyStats(pParse, p, pRec, 1, a);
115805 iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
115806 if( iNew<iUpper ) iUpper = iNew;
115807 nOut--;
115808 pUpper = 0;
115809 }
@@ -115811,10 +116645,15 @@
115811
115812 pBuilder->pRec = pRec;
115813 if( rc==SQLITE_OK ){
115814 if( iUpper>iLower ){
115815 nNew = sqlite3LogEst(iUpper - iLower);
 
 
 
 
 
115816 }else{
115817 nNew = 10; assert( 10==sqlite3LogEst(2) );
115818 }
115819 if( nNew<nOut ){
115820 nOut = nNew;
@@ -115835,16 +116674,19 @@
115835 #endif
115836 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
115837 nNew = whereRangeAdjust(pLower, nOut);
115838 nNew = whereRangeAdjust(pUpper, nNew);
115839
115840 /* TUNING: If there is both an upper and lower limit, assume the range is
 
115841 ** reduced by an additional 75%. This means that, by default, an open-ended
115842 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
115843 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
115844 ** match 1/64 of the index. */
115845 if( pLower && pUpper ) nNew -= 20;
 
 
115846
115847 nOut -= (pLower!=0) + (pUpper!=0);
115848 if( nNew<10 ) nNew = 10;
115849 if( nNew<nOut ) nOut = nNew;
115850 #if defined(WHERETRACE_ENABLED)
@@ -116200,11 +117042,11 @@
116200
116201 /* This module is only called on query plans that use an index. */
116202 pLoop = pLevel->pWLoop;
116203 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
116204 nEq = pLoop->u.btree.nEq;
116205 nSkip = pLoop->u.btree.nSkip;
116206 pIdx = pLoop->u.btree.pIndex;
116207 assert( pIdx!=0 );
116208
116209 /* Figure out how many memory cells we will need then allocate them.
116210 */
@@ -116314,11 +117156,11 @@
116314 ** "a=? AND b>?"
116315 */
116316 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
116317 Index *pIndex = pLoop->u.btree.pIndex;
116318 u16 nEq = pLoop->u.btree.nEq;
116319 u16 nSkip = pLoop->u.btree.nSkip;
116320 int i, j;
116321 Column *aCol = pTab->aCol;
116322 i16 *aiColumn = pIndex->aiColumn;
116323
116324 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
@@ -116345,23 +117187,27 @@
116345 sqlite3StrAccumAppend(pStr, ")", 1);
116346 }
116347
116348 /*
116349 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
116350 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
116351 ** record is added to the output to describe the table scan strategy in
116352 ** pLevel.
 
 
 
116353 */
116354 static void explainOneScan(
116355 Parse *pParse, /* Parse context */
116356 SrcList *pTabList, /* Table list this loop refers to */
116357 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
116358 int iLevel, /* Value for "level" column of output */
116359 int iFrom, /* Value for "from" column of output */
116360 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
116361 ){
116362 #ifndef SQLITE_DEBUG
 
116363 if( pParse->explain==2 )
116364 #endif
116365 {
116366 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
116367 Vdbe *v = pParse->pVdbe; /* VM being constructed */
@@ -116374,11 +117220,11 @@
116374 StrAccum str; /* EQP output string */
116375 char zBuf[100]; /* Initial space for EQP output string */
116376
116377 pLoop = pLevel->pWLoop;
116378 flags = pLoop->wsFlags;
116379 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
116380
116381 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
116382 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
116383 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
116384
@@ -116403,10 +117249,12 @@
116403 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
116404 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
116405 if( isSearch ){
116406 zFmt = "PRIMARY KEY";
116407 }
 
 
116408 }else if( flags & WHERE_AUTO_INDEX ){
116409 zFmt = "AUTOMATIC COVERING INDEX";
116410 }else if( flags & WHERE_IDX_ONLY ){
116411 zFmt = "COVERING INDEX %s";
116412 }else{
@@ -116444,16 +117292,49 @@
116444 }else{
116445 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
116446 }
116447 #endif
116448 zMsg = sqlite3StrAccumFinish(&str);
116449 sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
116450 }
 
116451 }
116452 #else
116453 # define explainOneScan(u,v,w,x,y,z)
116454 #endif /* SQLITE_OMIT_EXPLAIN */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116455
116456
116457 /*
116458 ** Generate code for the start of the iLevel-th loop in the WHERE clause
116459 ** implementation described by pWInfo.
@@ -116751,11 +117632,11 @@
116751 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
116752 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
116753
116754 pIdx = pLoop->u.btree.pIndex;
116755 iIdxCur = pLevel->iIdxCur;
116756 assert( nEq>=pLoop->u.btree.nSkip );
116757
116758 /* If this loop satisfies a sort order (pOrderBy) request that
116759 ** was passed to this function to implement a "SELECT min(x) ..."
116760 ** query, then the caller will only allow the loop to run for
116761 ** a single iteration. This means that the first row returned
@@ -116768,11 +117649,11 @@
116768 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
116769 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
116770 && pWInfo->nOBSat>0
116771 && (pIdx->nKeyCol>nEq)
116772 ){
116773 assert( pLoop->u.btree.nSkip==0 );
116774 bSeekPastNull = 1;
116775 nExtraReg = 1;
116776 }
116777
116778 /* Find any inequality constraint terms for the start and end
@@ -117117,13 +117998,15 @@
117117 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
117118 wctrlFlags, iCovCur);
117119 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
117120 if( pSubWInfo ){
117121 WhereLoop *pSubLoop;
117122 explainOneScan(
117123 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
117124 );
 
 
117125 /* This is the sub-WHERE clause body. First skip over
117126 ** duplicate rows from prior sub-WHERE clauses, and record the
117127 ** rowid (or PRIMARY KEY) for the current row so that the same
117128 ** row will be skipped in subsequent sub-WHERE clauses.
117129 */
@@ -117249,10 +118132,14 @@
117249 VdbeCoverageIf(v, bRev==0);
117250 VdbeCoverageIf(v, bRev!=0);
117251 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
117252 }
117253 }
 
 
 
 
117254
117255 /* Insert code to test every subexpression that can be completely
117256 ** computed using the current set of tables.
117257 */
117258 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
@@ -117389,11 +118276,11 @@
117389 }
117390 sqlite3DebugPrintf(" %-19s", z);
117391 sqlite3_free(z);
117392 }
117393 if( p->wsFlags & WHERE_SKIPSCAN ){
117394 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
117395 }else{
117396 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
117397 }
117398 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
117399 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
@@ -117500,34 +118387,41 @@
117500 sqlite3DbFree(db, pWInfo);
117501 }
117502 }
117503
117504 /*
117505 ** Return TRUE if both of the following are true:
117506 **
117507 ** (1) X has the same or lower cost that Y
117508 ** (2) X is a proper subset of Y
 
117509 **
117510 ** By "proper subset" we mean that X uses fewer WHERE clause terms
117511 ** than Y and that every WHERE clause term used by X is also used
117512 ** by Y.
117513 **
117514 ** If X is a proper subset of Y then Y is a better choice and ought
117515 ** to have a lower cost. This routine returns TRUE when that cost
117516 ** relationship is inverted and needs to be adjusted.
 
 
117517 */
117518 static int whereLoopCheaperProperSubset(
117519 const WhereLoop *pX, /* First WhereLoop to compare */
117520 const WhereLoop *pY /* Compare against this WhereLoop */
117521 ){
117522 int i, j;
117523 if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
 
 
 
117524 if( pX->rRun >= pY->rRun ){
117525 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
117526 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
117527 }
117528 for(i=pX->nLTerm-1; i>=0; i--){
 
117529 for(j=pY->nLTerm-1; j>=0; j--){
117530 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
117531 }
117532 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
117533 }
@@ -117545,37 +118439,28 @@
117545 ** is a proper subset.
117546 **
117547 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
117548 ** WHERE clause terms than Y and that every WHERE clause term used by X is
117549 ** also used by Y.
117550 **
117551 ** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the
117552 ** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
117553 ** clause terms covered, since some of the first nLTerm entries in aLTerm[]
117554 ** will be NULL (because they are skipped). That makes it more difficult
117555 ** to compare the loops. We could add extra code to do the comparison, and
117556 ** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this
117557 ** adjustment is sufficient minor, that it is very difficult to construct
117558 ** a test case where the extra code would improve the query plan. Better
117559 ** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
117560 ** loops.
117561 */
117562 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
117563 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
117564 if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
117565 for(; p; p=p->pNextLoop){
117566 if( p->iTab!=pTemplate->iTab ) continue;
117567 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
117568 if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
117569 if( whereLoopCheaperProperSubset(p, pTemplate) ){
117570 /* Adjust pTemplate cost downward so that it is cheaper than its
117571 ** subset p */
 
 
117572 pTemplate->rRun = p->rRun;
117573 pTemplate->nOut = p->nOut - 1;
117574 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
117575 /* Adjust pTemplate cost upward so that it is costlier than p since
117576 ** pTemplate is a proper subset of p */
 
 
117577 pTemplate->rRun = p->rRun;
117578 pTemplate->nOut = p->nOut + 1;
117579 }
117580 }
117581 }
@@ -117857,11 +118742,11 @@
117857 int opMask; /* Valid operators for constraints */
117858 WhereScan scan; /* Iterator for WHERE terms */
117859 Bitmask saved_prereq; /* Original value of pNew->prereq */
117860 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
117861 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
117862 u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
117863 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
117864 LogEst saved_nOut; /* Original value of pNew->nOut */
117865 int iCol; /* Index of the column in the table */
117866 int rc = SQLITE_OK; /* Return code */
117867 LogEst rSize; /* Number of rows in the table */
@@ -117886,56 +118771,18 @@
117886 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
117887
117888 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
117889 opMask, pProbe);
117890 saved_nEq = pNew->u.btree.nEq;
117891 saved_nSkip = pNew->u.btree.nSkip;
117892 saved_nLTerm = pNew->nLTerm;
117893 saved_wsFlags = pNew->wsFlags;
117894 saved_prereq = pNew->prereq;
117895 saved_nOut = pNew->nOut;
117896 pNew->rSetup = 0;
117897 rSize = pProbe->aiRowLogEst[0];
117898 rLogSize = estLog(rSize);
117899
117900 /* Consider using a skip-scan if there are no WHERE clause constraints
117901 ** available for the left-most terms of the index, and if the average
117902 ** number of repeats in the left-most terms is at least 18.
117903 **
117904 ** The magic number 18 is selected on the basis that scanning 17 rows
117905 ** is almost always quicker than an index seek (even though if the index
117906 ** contains fewer than 2^17 rows we assume otherwise in other parts of
117907 ** the code). And, even if it is not, it should not be too much slower.
117908 ** On the other hand, the extra seeks could end up being significantly
117909 ** more expensive. */
117910 assert( 42==sqlite3LogEst(18) );
117911 if( saved_nEq==saved_nSkip
117912 && saved_nEq+1<pProbe->nKeyCol
117913 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
117914 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
117915 ){
117916 LogEst nIter;
117917 pNew->u.btree.nEq++;
117918 pNew->u.btree.nSkip++;
117919 pNew->aLTerm[pNew->nLTerm++] = 0;
117920 pNew->wsFlags |= WHERE_SKIPSCAN;
117921 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
117922 if( pTerm ){
117923 /* TUNING: When estimating skip-scan for a term that is also indexable,
117924 ** multiply the cost of the skip-scan by 2.0, to make it a little less
117925 ** desirable than the regular index lookup. */
117926 nIter += 10; assert( 10==sqlite3LogEst(2) );
117927 }
117928 pNew->nOut -= nIter;
117929 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
117930 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
117931 nIter += 5;
117932 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
117933 pNew->nOut = saved_nOut;
117934 pNew->u.btree.nEq = saved_nEq;
117935 pNew->u.btree.nSkip = saved_nSkip;
117936 }
117937 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
117938 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
117939 LogEst rCostIdx;
117940 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
117941 int nIn = 0;
@@ -118026,11 +118873,10 @@
118026 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
118027 tRowcnt nOut = 0;
118028 if( nInMul==0
118029 && pProbe->nSample
118030 && pNew->u.btree.nEq<=pProbe->nSampleCol
118031 && OptimizationEnabled(db, SQLITE_Stat3)
118032 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
118033 ){
118034 Expr *pExpr = pTerm->pExpr;
118035 if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
118036 testcase( eOp & WO_EQ );
@@ -118094,14 +118940,48 @@
118094 pBuilder->nRecValid = nRecValid;
118095 #endif
118096 }
118097 pNew->prereq = saved_prereq;
118098 pNew->u.btree.nEq = saved_nEq;
118099 pNew->u.btree.nSkip = saved_nSkip;
118100 pNew->wsFlags = saved_wsFlags;
118101 pNew->nOut = saved_nOut;
118102 pNew->nLTerm = saved_nLTerm;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118103 return rc;
118104 }
118105
118106 /*
118107 ** Return True if it is possible that pIndex might be useful in
@@ -118276,11 +119156,11 @@
118276 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
118277 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
118278 if( pTerm->prereqRight & pNew->maskSelf ) continue;
118279 if( termCanDriveIndex(pTerm, pSrc, 0) ){
118280 pNew->u.btree.nEq = 1;
118281 pNew->u.btree.nSkip = 0;
118282 pNew->u.btree.pIndex = 0;
118283 pNew->nLTerm = 1;
118284 pNew->aLTerm[0] = pTerm;
118285 /* TUNING: One-time cost for computing the automatic index is
118286 ** estimated to be X*N*log2(N) where N is the number of rows in
@@ -118317,11 +119197,11 @@
118317 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
118318 continue; /* Partial index inappropriate for this query */
118319 }
118320 rSize = pProbe->aiRowLogEst[0];
118321 pNew->u.btree.nEq = 0;
118322 pNew->u.btree.nSkip = 0;
118323 pNew->nLTerm = 0;
118324 pNew->iSortIdx = 0;
118325 pNew->rSetup = 0;
118326 pNew->prereq = mExtra;
118327 pNew->nOut = rSize;
@@ -118867,11 +119747,11 @@
118867 for(j=0; j<nColumn; j++){
118868 u8 bOnce; /* True to run the ORDER BY search loop */
118869
118870 /* Skip over == and IS NULL terms */
118871 if( j<pLoop->u.btree.nEq
118872 && pLoop->u.btree.nSkip==0
118873 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
118874 ){
118875 if( i & WO_ISNULL ){
118876 testcase( isOrderDistinct );
118877 isOrderDistinct = 0;
@@ -119321,11 +120201,11 @@
119321 }
119322 }
119323 }
119324
119325 #ifdef WHERETRACE_ENABLED /* >=2 */
119326 if( sqlite3WhereTrace>=2 ){
119327 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
119328 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
119329 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
119330 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
119331 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
@@ -119440,11 +120320,11 @@
119440 if( pItem->zIndex ) return 0;
119441 iCur = pItem->iCursor;
119442 pWC = &pWInfo->sWC;
119443 pLoop = pBuilder->pNew;
119444 pLoop->wsFlags = 0;
119445 pLoop->u.btree.nSkip = 0;
119446 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
119447 if( pTerm ){
119448 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
119449 pLoop->aLTerm[0] = pTerm;
119450 pLoop->nLTerm = 1;
@@ -119452,11 +120332,10 @@
119452 /* TUNING: Cost of a rowid lookup is 10 */
119453 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
119454 }else{
119455 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119456 assert( pLoop->aLTermSpace==pLoop->aLTerm );
119457 assert( ArraySize(pLoop->aLTermSpace)==4 );
119458 if( !IsUniqueIndex(pIdx)
119459 || pIdx->pPartIdxWhere!=0
119460 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
119461 ) continue;
119462 for(j=0; j<pIdx->nKeyCol; j++){
@@ -119961,22 +120840,30 @@
119961 ** loop below generates code for a single nested loop of the VM
119962 ** program.
119963 */
119964 notReady = ~(Bitmask)0;
119965 for(ii=0; ii<nTabList; ii++){
 
 
119966 pLevel = &pWInfo->a[ii];
 
119967 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
119968 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
119969 constructAutomaticIndex(pParse, &pWInfo->sWC,
119970 &pTabList->a[pLevel->iFrom], notReady, pLevel);
119971 if( db->mallocFailed ) goto whereBeginError;
119972 }
119973 #endif
119974 explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
 
 
119975 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
119976 notReady = codeOneLoopStart(pWInfo, ii, notReady);
119977 pWInfo->iContinue = pLevel->addrCont;
 
 
 
119978 }
119979
119980 /* Done. */
119981 VdbeModuleComment((v, "Begin WHERE-core"));
119982 return pWInfo;
@@ -124640,10 +125527,17 @@
124640 ** is look for a semicolon that is not part of an string or comment.
124641 */
124642 SQLITE_API int sqlite3_complete(const char *zSql){
124643 u8 state = 0; /* Current state, using numbers defined in header comment */
124644 u8 token; /* Value of the next token */
 
 
 
 
 
 
 
124645
124646 #ifndef SQLITE_OMIT_TRIGGER
124647 /* A complex statement machine used to detect the end of a CREATE TRIGGER
124648 ** statement. This is the normal case.
124649 */
@@ -125238,74 +126132,106 @@
125238
125239 va_start(ap, op);
125240 switch( op ){
125241
125242 /* Mutex configuration options are only available in a threadsafe
125243 ** compile.
125244 */
125245 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
125246 case SQLITE_CONFIG_SINGLETHREAD: {
125247 /* Disable all mutexing */
125248 sqlite3GlobalConfig.bCoreMutex = 0;
125249 sqlite3GlobalConfig.bFullMutex = 0;
125250 break;
125251 }
 
 
125252 case SQLITE_CONFIG_MULTITHREAD: {
125253 /* Disable mutexing of database connections */
125254 /* Enable mutexing of core data structures */
125255 sqlite3GlobalConfig.bCoreMutex = 1;
125256 sqlite3GlobalConfig.bFullMutex = 0;
125257 break;
125258 }
 
 
125259 case SQLITE_CONFIG_SERIALIZED: {
125260 /* Enable all mutexing */
125261 sqlite3GlobalConfig.bCoreMutex = 1;
125262 sqlite3GlobalConfig.bFullMutex = 1;
125263 break;
125264 }
 
 
125265 case SQLITE_CONFIG_MUTEX: {
125266 /* Specify an alternative mutex implementation */
125267 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
125268 break;
125269 }
 
 
125270 case SQLITE_CONFIG_GETMUTEX: {
125271 /* Retrieve the current mutex implementation */
125272 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
125273 break;
125274 }
125275 #endif
125276
125277
125278 case SQLITE_CONFIG_MALLOC: {
125279 /* Specify an alternative malloc implementation */
 
 
 
 
125280 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
125281 break;
125282 }
125283 case SQLITE_CONFIG_GETMALLOC: {
125284 /* Retrieve the current malloc() implementation */
 
 
 
125285 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
125286 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
125287 break;
125288 }
125289 case SQLITE_CONFIG_MEMSTATUS: {
125290 /* Enable or disable the malloc status collection */
 
 
125291 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
125292 break;
125293 }
125294 case SQLITE_CONFIG_SCRATCH: {
125295 /* Designate a buffer for scratch memory space */
 
 
 
125296 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
125297 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
125298 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
125299 break;
125300 }
125301 case SQLITE_CONFIG_PAGECACHE: {
125302 /* Designate a buffer for page cache memory space */
 
 
125303 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
125304 sqlite3GlobalConfig.szPage = va_arg(ap, int);
125305 sqlite3GlobalConfig.nPage = va_arg(ap, int);
125306 break;
 
 
 
 
 
 
 
 
 
 
 
125307 }
125308
125309 case SQLITE_CONFIG_PCACHE: {
125310 /* no-op */
125311 break;
@@ -125315,25 +126241,37 @@
125315 rc = SQLITE_ERROR;
125316 break;
125317 }
125318
125319 case SQLITE_CONFIG_PCACHE2: {
125320 /* Specify an alternative page cache implementation */
 
 
 
125321 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
125322 break;
125323 }
125324 case SQLITE_CONFIG_GETPCACHE2: {
 
 
 
 
125325 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
125326 sqlite3PCacheSetDefault();
125327 }
125328 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
125329 break;
125330 }
125331
 
 
 
125332 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
125333 case SQLITE_CONFIG_HEAP: {
125334 /* Designate a buffer for heap memory space */
 
 
125335 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
125336 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
125337 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
125338
125339 if( sqlite3GlobalConfig.mnReq<1 ){
@@ -125342,21 +126280,23 @@
125342 /* cap min request size at 2^12 */
125343 sqlite3GlobalConfig.mnReq = (1<<12);
125344 }
125345
125346 if( sqlite3GlobalConfig.pHeap==0 ){
125347 /* If the heap pointer is NULL, then restore the malloc implementation
125348 ** back to NULL pointers too. This will cause the malloc to go
125349 ** back to its default implementation when sqlite3_initialize() is
125350 ** run.
 
 
 
125351 */
125352 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
125353 }else{
125354 /* The heap pointer is not NULL, then install one of the
125355 ** mem5.c/mem3.c methods. The enclosing #if guarantees at
125356 ** least one of these methods is currently enabled.
125357 */
125358 #ifdef SQLITE_ENABLE_MEMSYS3
125359 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
125360 #endif
125361 #ifdef SQLITE_ENABLE_MEMSYS5
125362 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
@@ -125391,15 +126331,23 @@
125391 ** can be changed at start-time using the
125392 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
125393 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
125394 */
125395 case SQLITE_CONFIG_URI: {
 
 
 
 
125396 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
125397 break;
125398 }
125399
125400 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
 
 
 
 
125401 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
125402 break;
125403 }
125404
125405 #ifdef SQLITE_ENABLE_SQLLOG
@@ -125410,24 +126358,37 @@
125410 break;
125411 }
125412 #endif
125413
125414 case SQLITE_CONFIG_MMAP_SIZE: {
 
 
 
 
125415 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
125416 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
125417 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
125418 mxMmap = SQLITE_MAX_MMAP_SIZE;
125419 }
125420 sqlite3GlobalConfig.mxMmap = mxMmap;
 
 
 
 
 
125421 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
125422 if( szMmap>mxMmap) szMmap = mxMmap;
 
125423 sqlite3GlobalConfig.szMmap = szMmap;
125424 break;
125425 }
125426
125427 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
125428 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
 
 
 
125429 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
125430 break;
125431 }
125432 #endif
125433
@@ -125507,19 +126468,29 @@
125507
125508 /*
125509 ** Return the mutex associated with a database connection.
125510 */
125511 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
 
 
 
 
 
 
125512 return db->mutex;
125513 }
125514
125515 /*
125516 ** Free up as much memory as we can from the given database
125517 ** connection.
125518 */
125519 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
125520 int i;
 
 
 
 
125521 sqlite3_mutex_enter(db->mutex);
125522 sqlite3BtreeEnterAll(db);
125523 for(i=0; i<db->nDb; i++){
125524 Btree *pBt = db->aDb[i].pBt;
125525 if( pBt ){
@@ -125646,24 +126617,42 @@
125646
125647 /*
125648 ** Return the ROWID of the most recent insert
125649 */
125650 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
 
 
 
 
 
 
125651 return db->lastRowid;
125652 }
125653
125654 /*
125655 ** Return the number of changes in the most recent call to sqlite3_exec().
125656 */
125657 SQLITE_API int sqlite3_changes(sqlite3 *db){
 
 
 
 
 
 
125658 return db->nChange;
125659 }
125660
125661 /*
125662 ** Return the number of changes since the database handle was opened.
125663 */
125664 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
 
 
 
 
 
 
125665 return db->nTotalChange;
125666 }
125667
125668 /*
125669 ** Close all open savepoints. This function only manipulates fields of the
@@ -125925,17 +126914,19 @@
125925 sqlite3_free(db);
125926 }
125927
125928 /*
125929 ** Rollback all database files. If tripCode is not SQLITE_OK, then
125930 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
125931 ** breaker") and made to return tripCode if there are any further
125932 ** attempts to use that cursor.
 
125933 */
125934 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
125935 int i;
125936 int inTrans = 0;
 
125937 assert( sqlite3_mutex_held(db->mutex) );
125938 sqlite3BeginBenignMalloc();
125939
125940 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
125941 ** This is important in case the transaction being rolled back has
@@ -125942,18 +126933,19 @@
125942 ** modified the database schema. If the b-tree mutexes are not taken
125943 ** here, then another shared-cache connection might sneak in between
125944 ** the database rollback and schema reset, which can cause false
125945 ** corruption reports in some cases. */
125946 sqlite3BtreeEnterAll(db);
 
125947
125948 for(i=0; i<db->nDb; i++){
125949 Btree *p = db->aDb[i].pBt;
125950 if( p ){
125951 if( sqlite3BtreeIsInTrans(p) ){
125952 inTrans = 1;
125953 }
125954 sqlite3BtreeRollback(p, tripCode);
125955 }
125956 }
125957 sqlite3VtabRollback(db);
125958 sqlite3EndBenignMalloc();
125959
@@ -126205,10 +127197,13 @@
126205 SQLITE_API int sqlite3_busy_handler(
126206 sqlite3 *db,
126207 int (*xBusy)(void*,int),
126208 void *pArg
126209 ){
 
 
 
126210 sqlite3_mutex_enter(db->mutex);
126211 db->busyHandler.xFunc = xBusy;
126212 db->busyHandler.pArg = pArg;
126213 db->busyHandler.nBusy = 0;
126214 db->busyTimeout = 0;
@@ -126226,10 +127221,16 @@
126226 sqlite3 *db,
126227 int nOps,
126228 int (*xProgress)(void*),
126229 void *pArg
126230 ){
 
 
 
 
 
 
126231 sqlite3_mutex_enter(db->mutex);
126232 if( nOps>0 ){
126233 db->xProgress = xProgress;
126234 db->nProgressOps = (unsigned)nOps;
126235 db->pProgressArg = pArg;
@@ -126246,10 +127247,13 @@
126246 /*
126247 ** This routine installs a default busy handler that waits for the
126248 ** specified number of milliseconds before returning 0.
126249 */
126250 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
 
 
 
126251 if( ms>0 ){
126252 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
126253 db->busyTimeout = ms;
126254 }else{
126255 sqlite3_busy_handler(db, 0, 0);
@@ -126259,10 +127263,16 @@
126259
126260 /*
126261 ** Cause any pending operation to stop at its earliest opportunity.
126262 */
126263 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
 
 
 
 
 
 
126264 db->u1.isInterrupted = 1;
126265 }
126266
126267
126268 /*
@@ -126396,10 +127406,16 @@
126396 void (*xFinal)(sqlite3_context*),
126397 void (*xDestroy)(void *)
126398 ){
126399 int rc = SQLITE_ERROR;
126400 FuncDestructor *pArg = 0;
 
 
 
 
 
 
126401 sqlite3_mutex_enter(db->mutex);
126402 if( xDestroy ){
126403 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
126404 if( !pArg ){
126405 xDestroy(p);
@@ -126432,10 +127448,14 @@
126432 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
126433 void (*xFinal)(sqlite3_context*)
126434 ){
126435 int rc;
126436 char *zFunc8;
 
 
 
 
126437 sqlite3_mutex_enter(db->mutex);
126438 assert( !db->mallocFailed );
126439 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
126440 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
126441 sqlite3DbFree(db, zFunc8);
@@ -126463,10 +127483,16 @@
126463 const char *zName,
126464 int nArg
126465 ){
126466 int nName = sqlite3Strlen30(zName);
126467 int rc = SQLITE_OK;
 
 
 
 
 
 
126468 sqlite3_mutex_enter(db->mutex);
126469 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
126470 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
126471 0, sqlite3InvalidFunction, 0, 0, 0);
126472 }
@@ -126484,10 +127510,17 @@
126484 ** trace is a pointer to a function that is invoked at the start of each
126485 ** SQL statement.
126486 */
126487 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
126488 void *pOld;
 
 
 
 
 
 
 
126489 sqlite3_mutex_enter(db->mutex);
126490 pOld = db->pTraceArg;
126491 db->xTrace = xTrace;
126492 db->pTraceArg = pArg;
126493 sqlite3_mutex_leave(db->mutex);
@@ -126505,10 +127538,17 @@
126505 sqlite3 *db,
126506 void (*xProfile)(void*,const char*,sqlite_uint64),
126507 void *pArg
126508 ){
126509 void *pOld;
 
 
 
 
 
 
 
126510 sqlite3_mutex_enter(db->mutex);
126511 pOld = db->pProfileArg;
126512 db->xProfile = xProfile;
126513 db->pProfileArg = pArg;
126514 sqlite3_mutex_leave(db->mutex);
@@ -126525,10 +127565,17 @@
126525 sqlite3 *db, /* Attach the hook to this database */
126526 int (*xCallback)(void*), /* Function to invoke on each commit */
126527 void *pArg /* Argument to the function */
126528 ){
126529 void *pOld;
 
 
 
 
 
 
 
126530 sqlite3_mutex_enter(db->mutex);
126531 pOld = db->pCommitArg;
126532 db->xCommitCallback = xCallback;
126533 db->pCommitArg = pArg;
126534 sqlite3_mutex_leave(db->mutex);
@@ -126543,10 +127590,17 @@
126543 sqlite3 *db, /* Attach the hook to this database */
126544 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
126545 void *pArg /* Argument to the function */
126546 ){
126547 void *pRet;
 
 
 
 
 
 
 
126548 sqlite3_mutex_enter(db->mutex);
126549 pRet = db->pUpdateArg;
126550 db->xUpdateCallback = xCallback;
126551 db->pUpdateArg = pArg;
126552 sqlite3_mutex_leave(db->mutex);
@@ -126561,10 +127615,17 @@
126561 sqlite3 *db, /* Attach the hook to this database */
126562 void (*xCallback)(void*), /* Callback function */
126563 void *pArg /* Argument to the function */
126564 ){
126565 void *pRet;
 
 
 
 
 
 
 
126566 sqlite3_mutex_enter(db->mutex);
126567 pRet = db->pRollbackArg;
126568 db->xRollbackCallback = xCallback;
126569 db->pRollbackArg = pArg;
126570 sqlite3_mutex_leave(db->mutex);
@@ -126607,10 +127668,13 @@
126607 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
126608 #ifdef SQLITE_OMIT_WAL
126609 UNUSED_PARAMETER(db);
126610 UNUSED_PARAMETER(nFrame);
126611 #else
 
 
 
126612 if( nFrame>0 ){
126613 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
126614 }else{
126615 sqlite3_wal_hook(db, 0, 0);
126616 }
@@ -126627,10 +127691,16 @@
126627 int(*xCallback)(void *, sqlite3*, const char*, int),
126628 void *pArg /* First argument passed to xCallback() */
126629 ){
126630 #ifndef SQLITE_OMIT_WAL
126631 void *pRet;
 
 
 
 
 
 
126632 sqlite3_mutex_enter(db->mutex);
126633 pRet = db->pWalArg;
126634 db->xWalCallback = xCallback;
126635 db->pWalArg = pArg;
126636 sqlite3_mutex_leave(db->mutex);
@@ -126653,10 +127723,14 @@
126653 #ifdef SQLITE_OMIT_WAL
126654 return SQLITE_OK;
126655 #else
126656 int rc; /* Return code */
126657 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
 
 
 
 
126658
126659 /* Initialize the output variables to -1 in case an error occurs. */
126660 if( pnLog ) *pnLog = -1;
126661 if( pnCkpt ) *pnCkpt = -1;
126662
@@ -127050,10 +128124,16 @@
127050 ** from forming.
127051 */
127052 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
127053 int oldLimit;
127054
 
 
 
 
 
 
127055
127056 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
127057 ** there is a hard upper bound set at compile-time by a C preprocessor
127058 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
127059 ** "_MAX_".)
@@ -127126,11 +128206,12 @@
127126 char c;
127127 int nUri = sqlite3Strlen30(zUri);
127128
127129 assert( *pzErrMsg==0 );
127130
127131 if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
 
127132 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
127133 ){
127134 char *zOpt;
127135 int eState; /* Parser state when parsing URI */
127136 int iIn; /* Input character index */
@@ -127335,10 +128416,13 @@
127335 int rc; /* Return code */
127336 int isThreadsafe; /* True for threadsafe connections */
127337 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
127338 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
127339
 
 
 
127340 *ppDb = 0;
127341 #ifndef SQLITE_OMIT_AUTOINIT
127342 rc = sqlite3_initialize();
127343 if( rc ) return rc;
127344 #endif
@@ -127624,17 +128708,19 @@
127624 ){
127625 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
127626 sqlite3_value *pVal;
127627 int rc;
127628
127629 assert( zFilename );
127630 assert( ppDb );
 
127631 *ppDb = 0;
127632 #ifndef SQLITE_OMIT_AUTOINIT
127633 rc = sqlite3_initialize();
127634 if( rc ) return rc;
127635 #endif
 
127636 pVal = sqlite3ValueNew(0);
127637 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
127638 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
127639 if( zFilename8 ){
127640 rc = openDatabase(zFilename8, ppDb,
@@ -127660,17 +128746,11 @@
127660 const char *zName,
127661 int enc,
127662 void* pCtx,
127663 int(*xCompare)(void*,int,const void*,int,const void*)
127664 ){
127665 int rc;
127666 sqlite3_mutex_enter(db->mutex);
127667 assert( !db->mallocFailed );
127668 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
127669 rc = sqlite3ApiExit(db, rc);
127670 sqlite3_mutex_leave(db->mutex);
127671 return rc;
127672 }
127673
127674 /*
127675 ** Register a new collation sequence with the database handle db.
127676 */
@@ -127681,10 +128761,14 @@
127681 void* pCtx,
127682 int(*xCompare)(void*,int,const void*,int,const void*),
127683 void(*xDel)(void*)
127684 ){
127685 int rc;
 
 
 
 
127686 sqlite3_mutex_enter(db->mutex);
127687 assert( !db->mallocFailed );
127688 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
127689 rc = sqlite3ApiExit(db, rc);
127690 sqlite3_mutex_leave(db->mutex);
@@ -127702,10 +128786,14 @@
127702 void* pCtx,
127703 int(*xCompare)(void*,int,const void*,int,const void*)
127704 ){
127705 int rc = SQLITE_OK;
127706 char *zName8;
 
 
 
 
127707 sqlite3_mutex_enter(db->mutex);
127708 assert( !db->mallocFailed );
127709 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
127710 if( zName8 ){
127711 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
@@ -127724,10 +128812,13 @@
127724 SQLITE_API int sqlite3_collation_needed(
127725 sqlite3 *db,
127726 void *pCollNeededArg,
127727 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
127728 ){
 
 
 
127729 sqlite3_mutex_enter(db->mutex);
127730 db->xCollNeeded = xCollNeeded;
127731 db->xCollNeeded16 = 0;
127732 db->pCollNeededArg = pCollNeededArg;
127733 sqlite3_mutex_leave(db->mutex);
@@ -127742,10 +128833,13 @@
127742 SQLITE_API int sqlite3_collation_needed16(
127743 sqlite3 *db,
127744 void *pCollNeededArg,
127745 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
127746 ){
 
 
 
127747 sqlite3_mutex_enter(db->mutex);
127748 db->xCollNeeded = 0;
127749 db->xCollNeeded16 = xCollNeeded16;
127750 db->pCollNeededArg = pCollNeededArg;
127751 sqlite3_mutex_leave(db->mutex);
@@ -127768,10 +128862,16 @@
127768 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
127769 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
127770 ** by the next COMMIT or ROLLBACK.
127771 */
127772 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
 
 
 
 
 
 
127773 return db->autoCommit;
127774 }
127775
127776 /*
127777 ** The following routines are substitutes for constants SQLITE_CORRUPT,
@@ -127950,10 +129050,13 @@
127950
127951 /*
127952 ** Enable or disable the extended result codes.
127953 */
127954 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
 
 
 
127955 sqlite3_mutex_enter(db->mutex);
127956 db->errMask = onoff ? 0xffffffff : 0xff;
127957 sqlite3_mutex_leave(db->mutex);
127958 return SQLITE_OK;
127959 }
@@ -127963,10 +129066,13 @@
127963 */
127964 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
127965 int rc = SQLITE_ERROR;
127966 Btree *pBtree;
127967
 
 
 
127968 sqlite3_mutex_enter(db->mutex);
127969 pBtree = sqlite3DbNameToBtree(db, zDbName);
127970 if( pBtree ){
127971 Pager *pPager;
127972 sqlite3_file *fd;
@@ -128305,11 +129411,11 @@
128305 ** query parameter we seek. This routine returns the value of the zParam
128306 ** parameter if it exists. If the parameter does not exist, this routine
128307 ** returns a NULL pointer.
128308 */
128309 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
128310 if( zFilename==0 ) return 0;
128311 zFilename += sqlite3Strlen30(zFilename) + 1;
128312 while( zFilename[0] ){
128313 int x = strcmp(zFilename, zParam);
128314 zFilename += sqlite3Strlen30(zFilename) + 1;
128315 if( x==0 ) return zFilename;
@@ -128361,19 +129467,31 @@
128361 /*
128362 ** Return the filename of the database associated with a database
128363 ** connection.
128364 */
128365 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
 
 
 
 
 
 
128366 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
128367 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
128368 }
128369
128370 /*
128371 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
128372 ** no such database exists.
128373 */
128374 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
 
 
 
 
 
 
128375 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
128376 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
128377 }
128378
128379 /************** End of main.c ************************************************/
128380
--- 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.8. 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.
@@ -179,11 +179,11 @@
179
180
181 /*
182 ** These no-op macros are used in front of interfaces to mark those
183 ** interfaces as either deprecated or experimental. New applications
184 ** should not use deprecated interfaces - they are supported for backwards
185 ** compatibility only. Application writers should be aware that
186 ** experimental interfaces are subject to change in point releases.
187 **
188 ** These macros used to resolve to various kinds of compiler magic that
189 ** would generate warning messages when they were used. But that
@@ -229,13 +229,13 @@
229 **
230 ** See also: [sqlite3_libversion()],
231 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
232 ** [sqlite_version()] and [sqlite_source_id()].
233 */
234 #define SQLITE_VERSION "3.8.8"
235 #define SQLITE_VERSION_NUMBER 3008008
236 #define SQLITE_SOURCE_ID "2014-11-11 19:07:56 1412fcc480799ecbd68d44dd18d5bad40e20ccf1"
237
238 /*
239 ** CAPI3REF: Run-Time Library Version Numbers
240 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
241 **
@@ -1626,29 +1626,31 @@
1626 ** it is not possible to set the Serialized [threading mode] and
1627 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1628 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1629 **
1630 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1631 ** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1632 ** a pointer to an instance of the [sqlite3_mem_methods] structure.
1633 ** The argument specifies
1634 ** alternative low-level memory allocation routines to be used in place of
1635 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1636 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1637 ** before the [sqlite3_config()] call returns.</dd>
1638 **
1639 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1640 ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1641 ** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1642 ** The [sqlite3_mem_methods]
1643 ** structure is filled with the currently defined memory allocation routines.)^
1644 ** This option can be used to overload the default memory allocation
1645 ** routines with a wrapper that simulations memory allocation failure or
1646 ** tracks memory usage, for example. </dd>
1647 **
1648 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1649 ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1650 ** interpreted as a boolean, which enables or disables the collection of
1651 ** memory allocation statistics. ^(When memory allocation statistics are disabled, the
1652 ** following SQLite interfaces become non-operational:
1653 ** <ul>
1654 ** <li> [sqlite3_memory_used()]
1655 ** <li> [sqlite3_memory_highwater()]
1656 ** <li> [sqlite3_soft_heap_limit64()]
@@ -1658,78 +1660,90 @@
1660 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1661 ** allocation statistics are disabled by default.
1662 ** </dd>
1663 **
1664 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1665 ** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
1666 ** that SQLite can use for scratch memory. ^(There are three arguments
1667 ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
1668 ** aligned memory buffer from which the scratch allocations will be
1669 ** drawn, the size of each scratch allocation (sz),
1670 ** and the maximum number of scratch allocations (N).)^
 
1671 ** The first argument must be a pointer to an 8-byte aligned buffer
1672 ** of at least sz*N bytes of memory.
1673 ** ^SQLite will not use more than one scratch buffers per thread.
1674 ** ^SQLite will never request a scratch buffer that is more than 6
1675 ** times the database page size.
1676 ** ^If SQLite needs needs additional
1677 ** scratch memory beyond what is provided by this configuration option, then
1678 ** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
1679 ** ^When the application provides any amount of scratch memory using
1680 ** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
1681 ** [sqlite3_malloc|heap allocations].
1682 ** This can help [Robson proof|prevent memory allocation failures] due to heap
1683 ** fragmentation in low-memory embedded systems.
1684 ** </dd>
1685 **
1686 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1687 ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
1688 ** that SQLite can use for the database page cache with the default page
1689 ** cache implementation.
1690 ** This configuration should not be used if an application-define page
1691 ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
1692 ** configuration option.
1693 ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned
1694 ** memory, the size of each page buffer (sz), and the number of pages (N).
1695 ** The sz argument should be the size of the largest database page
1696 ** (a power of two between 512 and 32768) plus some extra bytes for each
1697 ** page header. ^The number of extra bytes needed by the page header
1698 ** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option
1699 ** to [sqlite3_config()].
1700 ** ^It is harmless, apart from the wasted memory,
1701 ** for the sz parameter to be larger than necessary. The first
1702 ** argument should pointer to an 8-byte aligned block of memory that
1703 ** is at least sz*N bytes of memory, otherwise subsequent behavior is
1704 ** undefined.
1705 ** ^SQLite will use the memory provided by the first argument to satisfy its
1706 ** memory needs for the first N pages that it adds to cache. ^If additional
1707 ** page cache memory is needed beyond what is provided by this option, then
1708 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
 
 
 
1709 **
1710 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1711 ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1712 ** that SQLite will use for all of its dynamic memory allocation needs
1713 ** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1714 ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1715 ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1716 ** [SQLITE_ERROR] if invoked otherwise.
1717 ** ^There are three arguments to SQLITE_CONFIG_HEAP:
1718 ** An 8-byte aligned pointer to the memory,
1719 ** the number of bytes in the memory buffer, and the minimum allocation size.
1720 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1721 ** to using its default memory allocator (the system malloc() implementation),
1722 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1723 ** memory pointer is not NULL then the alternative memory
 
1724 ** allocator is engaged to handle all of SQLites memory allocation needs.
1725 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1726 ** boundary or subsequent behavior of SQLite will be undefined.
1727 ** The minimum allocation size is capped at 2**12. Reasonable values
1728 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1729 **
1730 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1731 ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1732 ** pointer to an instance of the [sqlite3_mutex_methods] structure.
1733 ** The argument specifies alternative low-level mutex routines to be used in place
1734 ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
1735 ** content of the [sqlite3_mutex_methods] structure before the call to
1736 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1737 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1738 ** the entire mutexing subsystem is omitted from the build and hence calls to
1739 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1740 ** return [SQLITE_ERROR].</dd>
1741 **
1742 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1743 ** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1744 ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
1745 ** [sqlite3_mutex_methods]
1746 ** structure is filled with the currently defined mutex routines.)^
1747 ** This option can be used to overload the default mutex allocation
1748 ** routines with a wrapper used to track mutex usage for performance
1749 ** profiling or testing, for example. ^If SQLite is compiled with
@@ -1737,28 +1751,28 @@
1751 ** the entire mutexing subsystem is omitted from the build and hence calls to
1752 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1753 ** return [SQLITE_ERROR].</dd>
1754 **
1755 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1756 ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1757 ** the default size of lookaside memory on each [database connection].
1758 ** The first argument is the
1759 ** size of each lookaside buffer slot and the second is the number of
1760 ** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
1761 ** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1762 ** option to [sqlite3_db_config()] can be used to change the lookaside
1763 ** configuration on individual connections.)^ </dd>
1764 **
1765 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1766 ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1767 ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
1768 ** the interface to a custom page cache implementation.)^
1769 ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
1770 **
1771 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1772 ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1773 ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current
1774 ** page cache implementation into that object.)^ </dd>
1775 **
1776 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1777 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1778 ** global [error log].
@@ -1778,26 +1792,27 @@
1792 ** supplied by the application must not invoke any SQLite interface.
1793 ** In a multi-threaded application, the application-defined logger
1794 ** function must be threadsafe. </dd>
1795 **
1796 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1797 ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1798 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1799 ** then URI handling is globally disabled.)^ ^If URI handling is globally enabled,
1800 ** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1801 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1802 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1803 ** connection is opened. ^If it is globally disabled, filenames are
1804 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1805 ** database connection is opened. ^(By default, URI handling is globally
1806 ** disabled. The default value may be changed by compiling with the
1807 ** [SQLITE_USE_URI] symbol defined.)^
1808 **
1809 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1810 ** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1811 ** argument which is interpreted as a boolean in order to enable or disable
1812 ** the use of covering indices for full table scans in the query optimizer.
1813 ** ^The default setting is determined
1814 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1815 ** if that compile-time option is omitted.
1816 ** The ability to disable the use of covering indices for full table scans
1817 ** is because some incorrectly coded legacy applications might malfunction
1818 ** when the optimization is enabled. Providing the ability to
@@ -1833,23 +1848,32 @@
1848 ** that are the default mmap size limit (the default setting for
1849 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1850 ** ^The default setting can be overridden by each database connection using
1851 ** either the [PRAGMA mmap_size] command, or by using the
1852 ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1853 ** will be silently truncated if necessary so that it does not exceed the
1854 ** compile-time maximum mmap size set by the
1855 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1856 ** ^If either argument to this option is negative, then that argument is
1857 ** changed to its compile-time default.
1858 **
1859 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1860 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1861 ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1862 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1863 ** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1864 ** that specifies the maximum size of the created heap.
1865 ** </dl>
1866 **
1867 ** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1868 ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1869 ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1870 ** is a pointer to an integer and writes into that integer the number of extra
1871 ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of
1872 ** extra space required can change depending on the compiler,
1873 ** target platform, and SQLite version.
1874 ** </dl>
1875 */
1876 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1877 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1878 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
1879 #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1870,10 +1894,11 @@
1894 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1895 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1896 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1897 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
1898 #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
1899 #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
1900
1901 /*
1902 ** CAPI3REF: Database Connection Configuration Options
1903 **
1904 ** These constants are the available integer configuration options that
@@ -1997,51 +2022,49 @@
2022 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2023
2024 /*
2025 ** CAPI3REF: Count The Number Of Rows Modified
2026 **
2027 ** ^This function returns the number of rows modified, inserted or
2028 ** deleted by the most recently completed INSERT, UPDATE or DELETE
2029 ** statement on the database connection specified by the only parameter.
2030 ** ^Executing any other type of SQL statement does not modify the value
2031 ** returned by this function.
2032 **
2033 ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2034 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2035 ** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2036 **
2037 ** Changes to a view that are intercepted by
2038 ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2039 ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2040 ** DELETE statement run on a view is always zero. Only changes made to real
2041 ** tables are counted.
2042 **
2043 ** Things are more complicated if the sqlite3_changes() function is
2044 ** executed while a trigger program is running. This may happen if the
2045 ** program uses the [changes() SQL function], or if some other callback
2046 ** function invokes sqlite3_changes() directly. Essentially:
2047 **
2048 ** <ul>
2049 ** <li> ^(Before entering a trigger program the value returned by
2050 ** sqlite3_changes() function is saved. After the trigger program
2051 ** has finished, the original value is restored.)^
2052 **
2053 ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2054 ** statement sets the value returned by sqlite3_changes()
2055 ** upon completion as normal. Of course, this value will not include
2056 ** any changes performed by sub-triggers, as the sqlite3_changes()
2057 ** value will be saved and restored after each sub-trigger has run.)^
2058 ** </ul>
2059 **
2060 ** ^This means that if the changes() SQL function (or similar) is used
2061 ** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2062 ** returns the value as set when the calling statement began executing.
2063 ** ^If it is used by the second or subsequent such statement within a trigger
2064 ** program, the value returned reflects the number of rows modified by the
2065 ** previous INSERT, UPDATE or DELETE statement within the same trigger.
 
 
2066 **
2067 ** See also the [sqlite3_total_changes()] interface, the
2068 ** [count_changes pragma], and the [changes() SQL function].
2069 **
2070 ** If a separate thread makes changes on the same database connection
@@ -2051,24 +2074,21 @@
2074 SQLITE_API int sqlite3_changes(sqlite3*);
2075
2076 /*
2077 ** CAPI3REF: Total Number Of Rows Modified
2078 **
2079 ** ^This function returns the total number of rows inserted, modified or
2080 ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2081 ** since the database connection was opened, including those executed as
2082 ** part of trigger programs. ^Executing any other type of SQL statement
2083 ** does not affect the value returned by sqlite3_total_changes().
2084 **
2085 ** ^Changes made as part of [foreign key actions] are included in the
2086 ** count, but those made as part of REPLACE constraint resolution are
2087 ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2088 ** are not counted.
2089 **
 
 
 
2090 ** See also the [sqlite3_changes()] interface, the
2091 ** [count_changes pragma], and the [total_changes() SQL function].
2092 **
2093 ** If a separate thread makes changes on the same database connection
2094 ** while [sqlite3_total_changes()] is running then the value
@@ -2542,17 +2562,18 @@
2562 ** already uses the largest possible [ROWID]. The PRNG is also used for
2563 ** the build-in random() and randomblob() SQL functions. This interface allows
2564 ** applications to access the same PRNG for other purposes.
2565 **
2566 ** ^A call to this routine stores N bytes of randomness into buffer P.
2567 ** ^The P parameter can be a NULL pointer.
2568 **
2569 ** ^If this routine has not been previously called or if the previous
2570 ** call had N less than one or a NULL pointer for P, then the PRNG is
2571 ** seeded using randomness obtained from the xRandomness method of
2572 ** the default [sqlite3_vfs] object.
2573 ** ^If the previous call to this routine had an N of 1 or more and a
2574 ** non-NULL P then the pseudo-randomness is generated
2575 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2576 ** method.
2577 */
2578 SQLITE_API void sqlite3_randomness(int N, void *P);
2579
@@ -5762,31 +5783,47 @@
5783 ** in other words, the same BLOB that would be selected by:
5784 **
5785 ** <pre>
5786 ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5787 ** </pre>)^
5788 **
5789 ** ^(Parameter zDb is not the filename that contains the database, but
5790 ** rather the symbolic name of the database. For attached databases, this is
5791 ** the name that appears after the AS keyword in the [ATTACH] statement.
5792 ** For the main database file, the database name is "main". For TEMP
5793 ** tables, the database name is "temp".)^
5794 **
5795 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5796 ** and write access. ^If the flags parameter is zero, the BLOB is opened for
5797 ** read-only access.
5798 **
5799 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
5800 ** in *ppBlob. Otherwise an [error code] is returned and, unless the error
5801 ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
5802 ** the API is not misused, it is always safe to call [sqlite3_blob_close()]
5803 ** on *ppBlob after this function it returns.
5804 **
5805 ** This function fails with SQLITE_ERROR if any of the following are true:
5806 ** <ul>
5807 ** <li> ^(Database zDb does not exist)^,
5808 ** <li> ^(Table zTable does not exist within database zDb)^,
5809 ** <li> ^(Table zTable is a WITHOUT ROWID table)^,
5810 ** <li> ^(Column zColumn does not exist)^,
5811 ** <li> ^(Row iRow is not present in the table)^,
5812 ** <li> ^(The specified column of row iRow contains a value that is not
5813 ** a TEXT or BLOB value)^,
5814 ** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
5815 ** constraint and the blob is being opened for read/write access)^,
5816 ** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
5817 ** column zColumn is part of a [child key] definition and the blob is
5818 ** being opened for read/write access)^.
5819 ** </ul>
5820 **
5821 ** ^Unless it returns SQLITE_MISUSE, this function sets the
5822 ** [database connection] error code and message accessible via
5823 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5824 **
5825 **
5826 ** ^(If the row that a BLOB handle points to is modified by an
5827 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5828 ** then the BLOB handle is marked as "expired".
5829 ** This is true if any column of the row is changed, even a column
@@ -5800,17 +5837,13 @@
5837 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5838 ** the opened blob. ^The size of a blob may not be changed by this
5839 ** interface. Use the [UPDATE] SQL command to change the size of a
5840 ** blob.
5841 **
 
 
 
5842 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5843 ** and the built-in [zeroblob] SQL function may be used to create a
5844 ** zero-filled blob to read or write using the incremental-blob interface.
 
5845 **
5846 ** To avoid a resource leak, every open [BLOB handle] should eventually
5847 ** be released by a call to [sqlite3_blob_close()].
5848 */
5849 SQLITE_API int sqlite3_blob_open(
@@ -5848,28 +5881,26 @@
5881 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5882
5883 /*
5884 ** CAPI3REF: Close A BLOB Handle
5885 **
5886 ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
5887 ** unconditionally. Even if this routine returns an error code, the
5888 ** handle is still closed.)^
5889 **
5890 ** ^If the blob handle being closed was opened for read-write access, and if
5891 ** the database is in auto-commit mode and there are no other open read-write
5892 ** blob handles or active write statements, the current transaction is
5893 ** committed. ^If an error occurs while committing the transaction, an error
5894 ** code is returned and the transaction rolled back.
5895 **
5896 ** Calling this function with an argument that is not a NULL pointer or an
5897 ** open blob handle results in undefined behaviour. ^Calling this routine
5898 ** with a null pointer (such as would be returned by a failed call to
5899 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
5900 ** is passed a valid open blob handle, the values returned by the
5901 ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
 
 
5902 */
5903 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5904
5905 /*
5906 ** CAPI3REF: Return The Size Of An Open BLOB
@@ -5915,36 +5946,39 @@
5946 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5947
5948 /*
5949 ** CAPI3REF: Write Data Into A BLOB Incrementally
5950 **
5951 ** ^(This function is used to write data into an open [BLOB handle] from a
5952 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
5953 ** into the open BLOB, starting at offset iOffset.)^
5954 **
5955 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5956 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5957 ** ^Unless SQLITE_MISUSE is returned, this function sets the
5958 ** [database connection] error code and message accessible via
5959 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5960 **
5961 ** ^If the [BLOB handle] passed as the first argument was not opened for
5962 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5963 ** this function returns [SQLITE_READONLY].
5964 **
5965 ** This function may only modify the contents of the BLOB; it is
5966 ** not possible to increase the size of a BLOB using this API.
5967 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5968 ** [SQLITE_ERROR] is returned and no data is written. The size of the
5969 ** BLOB (and hence the maximum value of N+iOffset) can be determined
5970 ** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
5971 ** than zero [SQLITE_ERROR] is returned and no data is written.
5972 **
5973 ** ^An attempt to write to an expired [BLOB handle] fails with an
5974 ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
5975 ** before the [BLOB handle] expired are not rolled back by the
5976 ** expiration of the handle, though of course those changes might
5977 ** have been overwritten by the statement that expired the BLOB handle
5978 ** or by other independent statements.
5979 **
 
 
 
5980 ** This routine only works on a [BLOB handle] which has been created
5981 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5982 ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
5983 ** to this routine results in undefined and probably undesirable behavior.
5984 **
@@ -7533,10 +7567,102 @@
7567 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7568 #define SQLITE_FAIL 3
7569 /* #define SQLITE_ABORT 4 // Also an error code */
7570 #define SQLITE_REPLACE 5
7571
7572 /*
7573 ** CAPI3REF: Prepared Statement Scan Status Opcodes
7574 ** KEYWORDS: {scanstatus options}
7575 **
7576 ** The following constants can be used for the T parameter to the
7577 ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
7578 ** different metric for sqlite3_stmt_scanstatus() to return.
7579 **
7580 ** <dl>
7581 ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
7582 ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7583 ** total number of times that the X-th loop has run.</dd>
7584 **
7585 ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
7586 ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7587 ** total number of rows examined by all iterations of the X-th loop.</dd>
7588 **
7589 ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
7590 ** <dd>^The "double" variable pointed to by the T parameter will be set to the
7591 ** query planner's estimate for the average number of rows output from each
7592 ** iteration of the X-th loop. If the query planner's estimates was accurate,
7593 ** then this value will approximate the quotient NVISIT/NLOOP and the
7594 ** product of this value for all prior loops with the same SELECTID will
7595 ** be the NLOOP value for the current loop.
7596 **
7597 ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
7598 ** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7599 ** a zero-terminated UTF-8 string containing the name of the index or table used
7600 ** for the X-th loop.
7601 **
7602 ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
7603 ** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7604 ** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description
7605 ** for the X-th loop.
7606 **
7607 ** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
7608 ** <dd>^The "int" variable pointed to by the T parameter will be set to the
7609 ** "select-id" for the X-th loop. The select-id identifies which query or
7610 ** subquery the loop is part of. The main query has a select-id of zero.
7611 ** The select-id is the same value as is output in the first column
7612 ** of an [EXPLAIN QUERY PLAN] query.
7613 ** </dl>
7614 */
7615 #define SQLITE_SCANSTAT_NLOOP 0
7616 #define SQLITE_SCANSTAT_NVISIT 1
7617 #define SQLITE_SCANSTAT_EST 2
7618 #define SQLITE_SCANSTAT_NAME 3
7619 #define SQLITE_SCANSTAT_EXPLAIN 4
7620 #define SQLITE_SCANSTAT_SELECTID 5
7621
7622 /*
7623 ** CAPI3REF: Prepared Statement Scan Status
7624 **
7625 ** Return status data for a single loop within query pStmt.
7626 **
7627 ** The "iScanStatusOp" parameter determines which status information to return.
7628 ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of
7629 ** this interface is undefined.
7630 ** ^The requested measurement is written into a variable pointed to by
7631 ** the "pOut" parameter.
7632 ** Parameter "idx" identifies the specific loop to retrieve statistics for.
7633 ** Loops are numbered starting from zero. ^If idx is out of range - less than
7634 ** zero or greater than or equal to the total number of loops used to implement
7635 ** the statement - a non-zero value is returned and the variable that pOut
7636 ** points to is unchanged.
7637 **
7638 ** ^Statistics might not be available for all loops in all statements. ^In cases
7639 ** where there exist loops with no available statistics, this function behaves
7640 ** as if the loop did not exist - it returns non-zero and leave the variable
7641 ** that pOut points to unchanged.
7642 **
7643 ** This API is only available if the library is built with pre-processor
7644 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7645 **
7646 ** See also: [sqlite3_stmt_scanstatus_reset()]
7647 */
7648 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
7649 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7650 int idx, /* Index of loop to report on */
7651 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7652 void *pOut /* Result written here */
7653 );
7654
7655 /*
7656 ** CAPI3REF: Zero Scan-Status Counters
7657 **
7658 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7659 **
7660 ** This API is only available if the library is built with pre-processor
7661 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7662 */
7663 SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7664
7665
7666 /*
7667 ** Undo the hack that converts floating point types to integer for
7668 ** builds on processors without floating point support.
@@ -7978,14 +8104,13 @@
8104 #ifndef SQLITE_POWERSAFE_OVERWRITE
8105 # define SQLITE_POWERSAFE_OVERWRITE 1
8106 #endif
8107
8108 /*
8109 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
8110 ** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
8111 ** which case memory allocation statistics are disabled by default.
 
8112 */
8113 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
8114 # define SQLITE_DEFAULT_MEMSTATUS 1
8115 #endif
8116
@@ -8611,11 +8736,11 @@
8736 ** Estimated quantities used for query planning are stored as 16-bit
8737 ** logarithms. For quantity X, the value stored is 10*log2(X). This
8738 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8739 ** But the allowed values are "grainy". Not every value is representable.
8740 ** For example, quantities 16 and 17 are both represented by a LogEst
8741 ** of 40. However, since LogEst quantities are suppose to be estimates,
8742 ** not exact values, this imprecision is not a problem.
8743 **
8744 ** "LogEst" is short for "Logarithmic Estimate".
8745 **
8746 ** Examples:
@@ -9011,11 +9136,11 @@
9136 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
9137 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
9138 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
9139 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
9140 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
9141 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
9142 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
9143 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
9144 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
9145 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
9146 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
@@ -9044,11 +9169,11 @@
9169 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
9170
9171 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
9172 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
9173 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
9174 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int, int);
9175
9176 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
9177 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
9178
9179 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
@@ -9124,10 +9249,11 @@
9249 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9250 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9251 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9252 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9253 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9254 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
9255
9256 #ifndef NDEBUG
9257 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
9258 #endif
9259
@@ -9666,10 +9792,16 @@
9792 # define VdbeCoverageAlwaysTaken(v)
9793 # define VdbeCoverageNeverTaken(v)
9794 # define VDBE_OFFSET_LINENO(x) 0
9795 #endif
9796
9797 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
9798 SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
9799 #else
9800 # define sqlite3VdbeScanStatus(a,b,c,d,e)
9801 #endif
9802
9803 #endif
9804
9805 /************** End of vdbe.h ************************************************/
9806 /************** Continuing where we left off in sqliteInt.h ******************/
9807 /************** Include pager.h in the middle of sqliteInt.h *****************/
@@ -9862,10 +9994,12 @@
9994 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
9995
9996 /* Functions used to truncate the database file. */
9997 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9998
9999 SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
10000
10001 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
10002 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
10003 #endif
10004
10005 /* Functions to support testing and debugging. */
@@ -10049,10 +10183,14 @@
10183 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
10184 #endif
10185
10186 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
10187
10188 /* Return the header size */
10189 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
10190 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
10191
10192 #endif /* _PCACHE_H_ */
10193
10194 /************** End of pcache.h **********************************************/
10195 /************** Continuing where we left off in sqliteInt.h ******************/
10196
@@ -10735,11 +10873,11 @@
10873 #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
10874 #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
10875 #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */
10876 #define SQLITE_Transitive 0x0200 /* Transitive constraints */
10877 #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */
10878 #define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */
10879 #define SQLITE_AllOpts 0xffff /* All optimizations */
10880
10881 /*
10882 ** Macros for testing whether or not optimizations are enabled or disabled.
10883 */
@@ -11322,11 +11460,12 @@
11460 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11461 int nSample; /* Number of elements in aSample[] */
11462 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
11463 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
11464 IndexSample *aSample; /* Samples of the left-most key */
11465 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
11466 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
11467 #endif
11468 };
11469
11470 /*
11471 ** Allowed values for Index.idxType
@@ -11520,11 +11659,11 @@
11659 int nHeight; /* Height of the tree headed by this node */
11660 #endif
11661 int iTable; /* TK_COLUMN: cursor number of table holding column
11662 ** TK_REGISTER: register number
11663 ** TK_TRIGGER: 1 -> new, 0 -> old
11664 ** EP_Unlikely: 134217728 times likelihood */
11665 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
11666 ** TK_VARIABLE: variable number (always >= 1). */
11667 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
11668 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
11669 u8 op2; /* TK_REGISTER: original value of Expr.op
@@ -12412,13 +12551,15 @@
12551 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
12552 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
12553 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
12554 Parse *pParse; /* Parser context. */
12555 int walkerDepth; /* Number of subqueries */
12556 u8 eCode; /* A small processing code */
12557 union { /* Extra data for callback */
12558 NameContext *pNC; /* Naming context */
12559 int n; /* A counter */
12560 int iCur; /* A cursor number */
12561 SrcList *pSrcList; /* FROM clause */
12562 struct SrcCount *pSrcCount; /* Counting column references */
12563 } u;
12564 };
12565
@@ -12815,10 +12956,11 @@
12956 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12957 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12958 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12959 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12960 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
12961 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
12962 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12963 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12964 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12965 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12966 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
@@ -13060,11 +13202,11 @@
13202 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
13203
13204 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
13205 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
13206 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
13207 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
13208 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
13209 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
13210 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
13211 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
13212
@@ -13472,15 +13614,23 @@
13614 ** compatibility for legacy applications, the URI filename capability is
13615 ** disabled by default.
13616 **
13617 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
13618 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
13619 **
13620 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
13621 ** disabled. The default value may be changed by compiling with the
13622 ** SQLITE_USE_URI symbol defined.
13623 */
13624 #ifndef SQLITE_USE_URI
13625 # define SQLITE_USE_URI 0
13626 #endif
13627
13628 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
13629 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
13630 ** that compile-time option is omitted.
13631 */
13632 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
13633 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
13634 #endif
13635
13636 /*
@@ -13566,12 +13716,12 @@
13716 ** than 1 GiB. The sqlite3_test_control() interface can be used to
13717 ** move the pending byte.
13718 **
13719 ** IMPORTANT: Changing the pending byte to any value other than
13720 ** 0x40000000 results in an incompatible database file format!
13721 ** Changing the pending byte during operation will result in undefined
13722 ** and incorrect behavior.
13723 */
13724 #ifndef SQLITE_OMIT_WSD
13725 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
13726 #endif
13727
@@ -13646,10 +13796,13 @@
13796 #ifdef SQLITE_DISABLE_DIRSYNC
13797 "DISABLE_DIRSYNC",
13798 #endif
13799 #ifdef SQLITE_DISABLE_LFS
13800 "DISABLE_LFS",
13801 #endif
13802 #ifdef SQLITE_ENABLE_API_ARMOR
13803 "ENABLE_API_ARMOR",
13804 #endif
13805 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13806 "ENABLE_ATOMIC_WRITE",
13807 #endif
13808 #ifdef SQLITE_ENABLE_CEROD
@@ -13972,10 +14125,17 @@
14125 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
14126 ** is not required for a match.
14127 */
14128 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
14129 int i, n;
14130
14131 #ifdef SQLITE_ENABLE_API_ARMOR
14132 if( zOptName==0 ){
14133 (void)SQLITE_MISUSE_BKPT;
14134 return 0;
14135 }
14136 #endif
14137 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
14138 n = sqlite3Strlen30(zOptName);
14139
14140 /* Since ArraySize(azCompileOpt) is normally in single digits, a
14141 ** linear search is adequate. No need for a binary search. */
@@ -14153,10 +14313,11 @@
14313 typedef struct VdbeFrame VdbeFrame;
14314 struct VdbeFrame {
14315 Vdbe *v; /* VM this frame belongs to */
14316 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
14317 Op *aOp; /* Program instructions for parent frame */
14318 i64 *anExec; /* Event counters from parent frame */
14319 Mem *aMem; /* Array of memory cells for parent frame */
14320 u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
14321 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
14322 void *token; /* Copy of SubProgram.token */
14323 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
@@ -14165,11 +14326,12 @@
14326 int nOp; /* Size of aOp array */
14327 int nMem; /* Number of entries in aMem */
14328 int nOnceFlag; /* Number of entries in aOnceFlag */
14329 int nChildMem; /* Number of memory cells for child frame */
14330 int nChildCsr; /* Number of cursors for child frame */
14331 int nChange; /* Statement changes (Vdbe.nChange) */
14332 int nDbChange; /* Value of db->nChange */
14333 };
14334
14335 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
14336
14337 /*
@@ -14316,10 +14478,20 @@
14478 /* A bitfield type for use inside of structures. Always follow with :N where
14479 ** N is the number of bits.
14480 */
14481 typedef unsigned bft; /* Bit Field Type */
14482
14483 typedef struct ScanStatus ScanStatus;
14484 struct ScanStatus {
14485 int addrExplain; /* OP_Explain for loop */
14486 int addrLoop; /* Address of "loops" counter */
14487 int addrVisit; /* Address of "rows visited" counter */
14488 int iSelectID; /* The "Select-ID" for this loop */
14489 LogEst nEst; /* Estimated output rows per loop */
14490 char *zName; /* Name of table or index */
14491 };
14492
14493 /*
14494 ** An instance of the virtual machine. This structure contains the complete
14495 ** state of the virtual machine.
14496 **
14497 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -14388,10 +14560,15 @@
14560 u32 expmask; /* Binding to these vars invalidates VM */
14561 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
14562 int nOnceFlag; /* Size of array aOnceFlag[] */
14563 u8 *aOnceFlag; /* Flags for OP_Once */
14564 AuxData *pAuxData; /* Linked list of auxdata allocations */
14565 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14566 i64 *anExec; /* Number of times each op has been executed */
14567 int nScan; /* Entries in aScan[] */
14568 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
14569 #endif
14570 };
14571
14572 /*
14573 ** The following are allowed values for Vdbe.magic
14574 */
@@ -14577,10 +14754,13 @@
14754 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
14755 wsdStatInit;
14756 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
14757 return SQLITE_MISUSE_BKPT;
14758 }
14759 #ifdef SQLITE_ENABLE_API_ARMOR
14760 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
14761 #endif
14762 *pCurrent = wsdStat.nowValue[op];
14763 *pHighwater = wsdStat.mxValue[op];
14764 if( resetFlag ){
14765 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14766 }
@@ -14596,10 +14776,15 @@
14776 int *pCurrent, /* Write current value here */
14777 int *pHighwater, /* Write high-water mark here */
14778 int resetFlag /* Reset high-water mark if true */
14779 ){
14780 int rc = SQLITE_OK; /* Return code */
14781 #ifdef SQLITE_ENABLE_API_ARMOR
14782 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
14783 return SQLITE_MISUSE_BKPT;
14784 }
14785 #endif
14786 sqlite3_mutex_enter(db->mutex);
14787 switch( op ){
14788 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
14789 *pCurrent = db->lookaside.nOut;
14790 *pHighwater = db->lookaside.mxOut;
@@ -14774,11 +14959,11 @@
14959 **
14960 ** There is only one exported symbol in this file - the function
14961 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
14962 ** All other code has file scope.
14963 **
14964 ** SQLite processes all times and dates as julian day numbers. The
14965 ** dates and times are stored as the number of days since noon
14966 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
14967 ** calendar system.
14968 **
14969 ** 1970-01-01 00:00:00 is JD 2440587.5
@@ -14789,11 +14974,11 @@
14974 ** be represented, even though julian day numbers allow a much wider
14975 ** range of dates.
14976 **
14977 ** The Gregorian calendar system is used for all dates and times,
14978 ** even those that predate the Gregorian calendar. Historians usually
14979 ** use the julian calendar for dates prior to 1582-10-15 and for some
14980 ** dates afterwards, depending on locale. Beware of this difference.
14981 **
14982 ** The conversion algorithms are implemented based on descriptions
14983 ** in the following text:
14984 **
@@ -15061,11 +15246,11 @@
15246 return 1;
15247 }
15248 }
15249
15250 /*
15251 ** Attempt to parse the given string into a julian day number. Return
15252 ** the number of errors.
15253 **
15254 ** The following are acceptable forms for the input string:
15255 **
15256 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
@@ -15632,11 +15817,11 @@
15817 **
15818 ** %d day of month
15819 ** %f ** fractional seconds SS.SSS
15820 ** %H hour 00-24
15821 ** %j day of year 000-366
15822 ** %J ** julian day number
15823 ** %m month 01-12
15824 ** %M minute 00-59
15825 ** %s seconds since 1970-01-01
15826 ** %S seconds 00-59
15827 ** %w day of week 0-6 sunday==0
@@ -16257,10 +16442,14 @@
16442 MUTEX_LOGIC(sqlite3_mutex *mutex;)
16443 #ifndef SQLITE_OMIT_AUTOINIT
16444 int rc = sqlite3_initialize();
16445 if( rc ) return rc;
16446 #endif
16447 #ifdef SQLITE_ENABLE_API_ARMOR
16448 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
16449 #endif
16450
16451 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
16452 sqlite3_mutex_enter(mutex);
16453 vfsUnlink(pVfs);
16454 if( makeDflt || vfsList==0 ){
16455 pVfs->pNext = vfsList;
@@ -18614,10 +18803,11 @@
18803 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18804 */
18805 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18806 #ifndef SQLITE_OMIT_AUTOINIT
18807 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
18808 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
18809 #endif
18810 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18811 }
18812
18813 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
@@ -19070,12 +19260,16 @@
19260 pthread_mutex_init(&p->mutex, 0);
19261 }
19262 break;
19263 }
19264 default: {
19265 #ifdef SQLITE_ENABLE_API_ARMOR
19266 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
19267 (void)SQLITE_MISUSE_BKPT;
19268 return 0;
19269 }
19270 #endif
19271 p = &staticMutexes[iType-2];
19272 #if SQLITE_MUTEX_NREF
19273 p->id = iType;
19274 #endif
19275 break;
@@ -20293,15 +20487,16 @@
20487 }
20488 assert( sqlite3_mutex_notheld(mem0.mutex) );
20489
20490
20491 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
20492 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
20493 ** buffers per thread.
20494 **
20495 ** This can only be checked in single-threaded mode.
20496 */
20497 assert( scratchAllocOut==0 );
20498 if( p ) scratchAllocOut++;
20499 #endif
20500
20501 return p;
20502 }
@@ -20945,11 +21140,11 @@
21140 sqlite_uint64 longvalue; /* Value for integer types */
21141 LONGDOUBLE_TYPE realvalue; /* Value for real types */
21142 const et_info *infop; /* Pointer to the appropriate info structure */
21143 char *zOut; /* Rendering buffer */
21144 int nOut; /* Size of the rendering buffer */
21145 char *zExtra = 0; /* Malloced memory used by some conversion */
21146 #ifndef SQLITE_OMIT_FLOATING_POINT
21147 int exp, e2; /* exponent of real numbers */
21148 int nsd; /* Number of significant digits returned */
21149 double rounder; /* Used for rounding floating point values */
21150 etByte flag_dp; /* True if decimal point should be shown */
@@ -20956,10 +21151,17 @@
21151 etByte flag_rtz; /* True if trailing zeros should be removed */
21152 #endif
21153 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
21154 char buf[etBUFSIZE]; /* Conversion buffer */
21155
21156 #ifdef SQLITE_ENABLE_API_ARMOR
21157 if( ap==0 ){
21158 (void)SQLITE_MISUSE_BKPT;
21159 sqlite3StrAccumReset(pAccum);
21160 return;
21161 }
21162 #endif
21163 bufpt = 0;
21164 if( bFlags ){
21165 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
21166 pArgList = va_arg(ap, PrintfArguments*);
21167 }
@@ -21062,11 +21264,10 @@
21264 return;
21265 }
21266 break;
21267 }
21268 }
 
21269
21270 /*
21271 ** At this point, variables are initialized as follows:
21272 **
21273 ** flag_alternateform TRUE if a '#' is present.
@@ -21353,17 +21554,20 @@
21554 bufpt = getTextArg(pArgList);
21555 c = bufpt ? bufpt[0] : 0;
21556 }else{
21557 c = va_arg(ap,int);
21558 }
21559 if( precision>1 ){
21560 width -= precision-1;
21561 if( width>1 && !flag_leftjustify ){
21562 sqlite3AppendChar(pAccum, width-1, ' ');
21563 width = 0;
21564 }
21565 sqlite3AppendChar(pAccum, precision-1, c);
21566 }
21567 length = 1;
21568 buf[0] = c;
21569 bufpt = buf;
21570 break;
21571 case etSTRING:
21572 case etDYNSTRING:
21573 if( bArgList ){
@@ -21460,15 +21664,18 @@
21664 ** The text of the conversion is pointed to by "bufpt" and is
21665 ** "length" characters long. The field width is "width". Do
21666 ** the output.
21667 */
21668 width -= length;
21669 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
21670 sqlite3StrAccumAppend(pAccum, bufpt, length);
21671 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
21672
21673 if( zExtra ){
21674 sqlite3_free(zExtra);
21675 zExtra = 0;
21676 }
21677 }/* End for loop over the format string */
21678 } /* End of function */
21679
21680 /*
21681 ** Enlarge the memory allocation on a StrAccum object so that it is
@@ -21491,10 +21698,15 @@
21698 return N;
21699 }else{
21700 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
21701 i64 szNew = p->nChar;
21702 szNew += N + 1;
21703 if( szNew+p->nChar<=p->mxAlloc ){
21704 /* Force exponential buffer size growth as long as it does not overflow,
21705 ** to avoid having to call this routine too often */
21706 szNew += p->nChar;
21707 }
21708 if( szNew > p->mxAlloc ){
21709 sqlite3StrAccumReset(p);
21710 setStrAccumError(p, STRACCUM_TOOBIG);
21711 return 0;
21712 }else{
@@ -21507,10 +21719,11 @@
21719 }
21720 if( zNew ){
21721 assert( p->zText!=0 || p->nChar==0 );
21722 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
21723 p->zText = zNew;
21724 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
21725 }else{
21726 sqlite3StrAccumReset(p);
21727 setStrAccumError(p, STRACCUM_NOMEM);
21728 return 0;
21729 }
@@ -21517,15 +21730,15 @@
21730 }
21731 return N;
21732 }
21733
21734 /*
21735 ** Append N copies of character c to the given string buffer.
21736 */
21737 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
21738 if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
21739 while( (N--)>0 ) p->zText[p->nChar++] = c;
21740 }
21741
21742 /*
21743 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
21744 ** So enlarge if first, then do the append.
@@ -21676,10 +21889,17 @@
21889 */
21890 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
21891 char *z;
21892 char zBase[SQLITE_PRINT_BUF_SIZE];
21893 StrAccum acc;
21894
21895 #ifdef SQLITE_ENABLE_API_ARMOR
21896 if( zFormat==0 ){
21897 (void)SQLITE_MISUSE_BKPT;
21898 return 0;
21899 }
21900 #endif
21901 #ifndef SQLITE_OMIT_AUTOINIT
21902 if( sqlite3_initialize() ) return 0;
21903 #endif
21904 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
21905 acc.useMalloc = 2;
@@ -21718,10 +21938,17 @@
21938 ** sqlite3_vsnprintf() is the varargs version.
21939 */
21940 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
21941 StrAccum acc;
21942 if( n<=0 ) return zBuf;
21943 #ifdef SQLITE_ENABLE_API_ARMOR
21944 if( zBuf==0 || zFormat==0 ) {
21945 (void)SQLITE_MISUSE_BKPT;
21946 if( zBuf && n>0 ) zBuf[0] = 0;
21947 return zBuf;
21948 }
21949 #endif
21950 sqlite3StrAccumInit(&acc, zBuf, n, 0);
21951 acc.useMalloc = 0;
21952 sqlite3VXPrintf(&acc, 0, zFormat, ap);
21953 return sqlite3StrAccumFinish(&acc);
21954 }
@@ -21909,15 +22136,23 @@
22136 #else
22137 # define wsdPrng sqlite3Prng
22138 #endif
22139
22140 #if SQLITE_THREADSAFE
22141 sqlite3_mutex *mutex;
22142 #endif
22143
22144 #ifndef SQLITE_OMIT_AUTOINIT
22145 if( sqlite3_initialize() ) return;
22146 #endif
22147
22148 #if SQLITE_THREADSAFE
22149 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
22150 #endif
22151
22152 sqlite3_mutex_enter(mutex);
22153 if( N<=0 || pBuf==0 ){
 
 
22154 wsdPrng.isInit = 0;
22155 sqlite3_mutex_leave(mutex);
22156 return;
22157 }
22158
@@ -23035,17 +23270,27 @@
23270 ** case-independent fashion, using the same definition of "case
23271 ** independence" that SQLite uses internally when comparing identifiers.
23272 */
23273 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
23274 register unsigned char *a, *b;
23275 if( zLeft==0 ){
23276 return zRight ? -1 : 0;
23277 }else if( zRight==0 ){
23278 return 1;
23279 }
23280 a = (unsigned char *)zLeft;
23281 b = (unsigned char *)zRight;
23282 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
23283 return UpperToLower[*a] - UpperToLower[*b];
23284 }
23285 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
23286 register unsigned char *a, *b;
23287 if( zLeft==0 ){
23288 return zRight ? -1 : 0;
23289 }else if( zRight==0 ){
23290 return 1;
23291 }
23292 a = (unsigned char *)zLeft;
23293 b = (unsigned char *)zRight;
23294 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
23295 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
23296 }
@@ -32574,10 +32819,15 @@
32819 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
32820 # error "WAL mode requires support from the Windows NT kernel, compile\
32821 with SQLITE_OMIT_WAL."
32822 #endif
32823
32824 #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
32825 # error "Memory mapped files require support from the Windows NT kernel,\
32826 compile with SQLITE_MAX_MMAP_SIZE=0."
32827 #endif
32828
32829 /*
32830 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
32831 ** based on the sub-platform)?
32832 */
32833 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
@@ -32703,14 +32953,15 @@
32953 # define winGetDirSep() '\\'
32954 #endif
32955
32956 /*
32957 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
32958 ** mode or memory mapped files (e.g. these APIs are available in the Windows
32959 ** CE SDK; however, they are not present in the header file)?
32960 */
32961 #if SQLITE_WIN32_FILEMAPPING_API && \
32962 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
32963 /*
32964 ** Two of the file mapping APIs are different under WinRT. Figure out which
32965 ** set we need.
32966 */
32967 #if SQLITE_OS_WINRT
@@ -32734,11 +32985,11 @@
32985
32986 /*
32987 ** This file mapping API is common to both Win32 and WinRT.
32988 */
32989 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
32990 #endif /* SQLITE_WIN32_FILEMAPPING_API */
32991
32992 /*
32993 ** Some Microsoft compilers lack this definition.
32994 */
32995 #ifndef INVALID_FILE_ATTRIBUTES
@@ -33027,21 +33278,21 @@
33278
33279 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
33280 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
33281
33282 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
33283 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
33284 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
33285 #else
33286 { "CreateFileMappingA", (SYSCALL)0, 0 },
33287 #endif
33288
33289 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
33290 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
33291
33292 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
33293 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
33294 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
33295 #else
33296 { "CreateFileMappingW", (SYSCALL)0, 0 },
33297 #endif
33298
@@ -33377,11 +33628,12 @@
33628 #ifndef osLockFileEx
33629 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
33630 LPOVERLAPPED))aSyscall[48].pCurrent)
33631 #endif
33632
33633 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
33634 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
33635 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
33636 #else
33637 { "MapViewOfFile", (SYSCALL)0, 0 },
33638 #endif
33639
@@ -33447,11 +33699,11 @@
33699 #endif
33700
33701 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
33702 LPOVERLAPPED))aSyscall[58].pCurrent)
33703
33704 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33705 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
33706 #else
33707 { "UnmapViewOfFile", (SYSCALL)0, 0 },
33708 #endif
33709
@@ -33510,11 +33762,11 @@
33762 #endif
33763
33764 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
33765 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
33766
33767 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
33768 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
33769 #else
33770 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
33771 #endif
33772
@@ -33574,11 +33826,11 @@
33826
33827 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
33828
33829 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
33830
33831 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
33832 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
33833 #else
33834 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
33835 #endif
33836
@@ -39150,10 +39402,17 @@
39402 */
39403 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
39404 assert( pCache->pCache!=0 );
39405 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
39406 }
39407
39408 /*
39409 ** Return the size of the header added by this middleware layer
39410 ** in the page-cache hierarchy.
39411 */
39412 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); }
39413
39414
39415 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
39416 /*
39417 ** For all dirty pages currently in the cache, invoke the specified
39418 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
@@ -40149,10 +40408,15 @@
40408 pcache1Shrink /* xShrink */
40409 };
40410 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
40411 }
40412
40413 /*
40414 ** Return the size of the header on each page of this PCACHE implementation.
40415 */
40416 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); }
40417
40418 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40419 /*
40420 ** This function is called to free superfluous dynamically allocated memory
40421 ** held by the pager system. Memory in use by any SQLite pager allocated
40422 ** by the current thread may be sqlite3_free()ed.
@@ -47705,10 +47969,22 @@
47969 }
47970
47971 return SQLITE_OK;
47972 }
47973 #endif
47974
47975 /*
47976 ** The page handle passed as the first argument refers to a dirty page
47977 ** with a page number other than iNew. This function changes the page's
47978 ** page number to iNew and sets the value of the PgHdr.flags field to
47979 ** the value passed as the third parameter.
47980 */
47981 SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
47982 assert( pPg->pgno!=iNew );
47983 pPg->flags = flags;
47984 sqlite3PcacheMove(pPg, iNew);
47985 }
47986
47987 /*
47988 ** Return a pointer to the data for the specified page.
47989 */
47990 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
@@ -48103,10 +48379,11 @@
48379 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
48380 assert( pPager->eState>=PAGER_READER );
48381 return sqlite3WalFramesize(pPager->pWal);
48382 }
48383 #endif
48384
48385
48386 #endif /* SQLITE_OMIT_DISKIO */
48387
48388 /************** End of pager.c ***********************************************/
48389 /************** Begin file wal.c *********************************************/
@@ -49613,11 +49890,11 @@
49890
49891 /*
49892 ** Free an iterator allocated by walIteratorInit().
49893 */
49894 static void walIteratorFree(WalIterator *p){
49895 sqlite3_free(p);
49896 }
49897
49898 /*
49899 ** Construct a WalInterator object that can be used to loop over all
49900 ** pages in the WAL in ascending order. The caller must hold the checkpoint
@@ -49648,21 +49925,21 @@
49925 /* Allocate space for the WalIterator object. */
49926 nSegment = walFramePage(iLast) + 1;
49927 nByte = sizeof(WalIterator)
49928 + (nSegment-1)*sizeof(struct WalSegment)
49929 + iLast*sizeof(ht_slot);
49930 p = (WalIterator *)sqlite3_malloc(nByte);
49931 if( !p ){
49932 return SQLITE_NOMEM;
49933 }
49934 memset(p, 0, nByte);
49935 p->nSegment = nSegment;
49936
49937 /* Allocate temporary space used by the merge-sort routine. This block
49938 ** of memory will be freed before this function returns.
49939 */
49940 aTmp = (ht_slot *)sqlite3_malloc(
49941 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
49942 );
49943 if( !aTmp ){
49944 rc = SQLITE_NOMEM;
49945 }
@@ -49695,11 +49972,11 @@
49972 p->aSegment[i].nEntry = nEntry;
49973 p->aSegment[i].aIndex = aIndex;
49974 p->aSegment[i].aPgno = (u32 *)aPgno;
49975 }
49976 }
49977 sqlite3_free(aTmp);
49978
49979 if( rc!=SQLITE_OK ){
49980 walIteratorFree(p);
49981 }
49982 *pp = p;
@@ -50615,11 +50892,11 @@
50892 ** was in before the client began writing to the database.
50893 */
50894 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
50895
50896 for(iFrame=pWal->hdr.mxFrame+1;
50897 rc==SQLITE_OK && iFrame<=iMax;
50898 iFrame++
50899 ){
50900 /* This call cannot fail. Unless the page for which the page number
50901 ** is passed as the second argument is (a) in the cache and
50902 ** (b) has an outstanding reference, then xUndo is either a no-op
@@ -50634,11 +50911,10 @@
50911 assert( walFramePgno(pWal, iFrame)!=1 );
50912 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
50913 }
50914 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
50915 }
 
50916 return rc;
50917 }
50918
50919 /*
50920 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
@@ -53333,28 +53609,27 @@
53609 int cellOffset; /* Offset to the cell pointer array */
53610 int cbrk; /* Offset to the cell content area */
53611 int nCell; /* Number of cells on the page */
53612 unsigned char *data; /* The page data */
53613 unsigned char *temp; /* Temp area for cell content */
53614 unsigned char *src; /* Source of content */
53615 int iCellFirst; /* First allowable cell index */
53616 int iCellLast; /* Last possible cell index */
53617
53618
53619 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53620 assert( pPage->pBt!=0 );
53621 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
53622 assert( pPage->nOverflow==0 );
53623 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53624 temp = 0;
53625 src = data = pPage->aData;
53626 hdr = pPage->hdrOffset;
53627 cellOffset = pPage->cellOffset;
53628 nCell = pPage->nCell;
53629 assert( nCell==get2byte(&data[hdr+3]) );
53630 usableSize = pPage->pBt->usableSize;
 
 
53631 cbrk = usableSize;
53632 iCellFirst = cellOffset + 2*nCell;
53633 iCellLast = usableSize - 4;
53634 for(i=0; i<nCell; i++){
53635 u8 *pAddr; /* The i-th cell pointer */
@@ -53369,11 +53644,11 @@
53644 if( pc<iCellFirst || pc>iCellLast ){
53645 return SQLITE_CORRUPT_BKPT;
53646 }
53647 #endif
53648 assert( pc>=iCellFirst && pc<=iCellLast );
53649 size = cellSizePtr(pPage, &src[pc]);
53650 cbrk -= size;
53651 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
53652 if( cbrk<iCellFirst ){
53653 return SQLITE_CORRUPT_BKPT;
53654 }
@@ -53383,12 +53658,20 @@
53658 }
53659 #endif
53660 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
53661 testcase( cbrk+size==usableSize );
53662 testcase( pc+size==usableSize );
 
53663 put2byte(pAddr, cbrk);
53664 if( temp==0 ){
53665 int x;
53666 if( cbrk==pc ) continue;
53667 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
53668 x = get2byte(&data[hdr+5]);
53669 memcpy(&temp[x], &data[x], (cbrk+size) - x);
53670 src = temp;
53671 }
53672 memcpy(&data[cbrk], &src[pc], size);
53673 }
53674 assert( cbrk>=iCellFirst );
53675 put2byte(&data[hdr+5], cbrk);
53676 data[hdr+1] = 0;
53677 data[hdr+2] = 0;
@@ -53398,10 +53681,66 @@
53681 if( cbrk-iCellFirst!=pPage->nFree ){
53682 return SQLITE_CORRUPT_BKPT;
53683 }
53684 return SQLITE_OK;
53685 }
53686
53687 /*
53688 ** Search the free-list on page pPg for space to store a cell nByte bytes in
53689 ** size. If one can be found, return a pointer to the space and remove it
53690 ** from the free-list.
53691 **
53692 ** If no suitable space can be found on the free-list, return NULL.
53693 **
53694 ** This function may detect corruption within pPg. If corruption is
53695 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
53696 **
53697 ** If a slot of at least nByte bytes is found but cannot be used because
53698 ** there are already at least 60 fragmented bytes on the page, return NULL.
53699 ** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
53700 */
53701 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
53702 const int hdr = pPg->hdrOffset;
53703 u8 * const aData = pPg->aData;
53704 int iAddr;
53705 int pc;
53706 int usableSize = pPg->pBt->usableSize;
53707
53708 for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
53709 int size; /* Size of the free slot */
53710 if( pc>usableSize-4 || pc<iAddr+4 ){
53711 *pRc = SQLITE_CORRUPT_BKPT;
53712 return 0;
53713 }
53714 size = get2byte(&aData[pc+2]);
53715 if( size>=nByte ){
53716 int x = size - nByte;
53717 testcase( x==4 );
53718 testcase( x==3 );
53719 if( x<4 ){
53720 if( aData[hdr+7]>=60 ){
53721 if( pbDefrag ) *pbDefrag = 1;
53722 return 0;
53723 }
53724 /* Remove the slot from the free-list. Update the number of
53725 ** fragmented bytes within the page. */
53726 memcpy(&aData[iAddr], &aData[pc], 2);
53727 aData[hdr+7] += (u8)x;
53728 }else if( size+pc > usableSize ){
53729 *pRc = SQLITE_CORRUPT_BKPT;
53730 return 0;
53731 }else{
53732 /* The slot remains on the free-list. Reduce its size to account
53733 ** for the portion used by the new allocation. */
53734 put2byte(&aData[pc+2], x);
53735 }
53736 return &aData[pc + x];
53737 }
53738 }
53739
53740 return 0;
53741 }
53742
53743 /*
53744 ** Allocate nByte bytes of space from within the B-Tree page passed
53745 ** as the first argument. Write into *pIdx the index into pPage->aData[]
53746 ** of the first byte of allocated space. Return either SQLITE_OK or
@@ -53416,22 +53755,20 @@
53755 */
53756 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
53757 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
53758 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
53759 int top; /* First byte of cell content area */
53760 int rc = SQLITE_OK; /* Integer return code */
53761 int gap; /* First byte of gap between cell pointers and cell content */
 
 
53762
53763 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53764 assert( pPage->pBt );
53765 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53766 assert( nByte>=0 ); /* Minimum cell size is 4 */
53767 assert( pPage->nFree>=nByte );
53768 assert( pPage->nOverflow==0 );
53769 assert( nByte < (int)(pPage->pBt->usableSize-8) );
 
53770
53771 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
53772 gap = pPage->cellOffset + 2*pPage->nCell;
53773 assert( gap<=65536 );
53774 top = get2byte(&data[hdr+5]);
@@ -53449,46 +53786,27 @@
53786 */
53787 testcase( gap+2==top );
53788 testcase( gap+1==top );
53789 testcase( gap==top );
53790 if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
53791 int bDefrag = 0;
53792 u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
53793 if( rc ) return rc;
53794 if( bDefrag ) goto defragment_page;
53795 if( pSpace ){
53796 assert( pSpace>=data && (pSpace - data)<65536 );
53797 *pIdx = (int)(pSpace - data);
53798 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53799 }
53800 }
53801
53802 /* The request could not be fulfilled using a freelist slot. Check
53803 ** to see if defragmentation is necessary.
53804 */
53805 testcase( gap+2+nByte==top );
53806 if( gap+2+nByte>top ){
53807 defragment_page:
53808 testcase( pPage->nCell==0 );
53809 rc = defragmentPage(pPage);
53810 if( rc ) return rc;
53811 top = get2byteNotZero(&data[hdr+5]);
53812 assert( gap+nByte<=top );
@@ -53532,11 +53850,11 @@
53850 unsigned char *data = pPage->aData; /* Page content */
53851
53852 assert( pPage->pBt!=0 );
53853 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53854 assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
53855 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
53856 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53857 assert( iSize>=4 ); /* Minimum cell size is 4 */
53858 assert( iStart<=iLast );
53859
53860 /* Overwrite deleted information with zeros when the secure_delete
@@ -54353,11 +54671,11 @@
54671
54672 /* Rollback any active transaction and free the handle structure.
54673 ** The call to sqlite3BtreeRollback() drops any table-locks held by
54674 ** this handle.
54675 */
54676 sqlite3BtreeRollback(p, SQLITE_OK, 0);
54677 sqlite3BtreeLeave(p);
54678
54679 /* If there are still other outstanding references to the shared-btree
54680 ** structure, return now. The remainder of this procedure cleans
54681 ** up the shared-btree.
@@ -55646,31 +55964,32 @@
55964 return rc;
55965 }
55966
55967 /*
55968 ** This routine sets the state to CURSOR_FAULT and the error
55969 ** code to errCode for every cursor on any BtShared that pBtree
55970 ** references. Or if the writeOnly flag is set to 1, then only
55971 ** trip write cursors and leave read cursors unchanged.
55972 **
55973 ** Every cursor is a candidate to be tripped, including cursors
55974 ** that belong to other database connections that happen to be
55975 ** sharing the cache with pBtree.
55976 **
55977 ** This routine gets called when a rollback occurs. The writeOnly
55978 ** flag is set to 1 if the transaction did not make any schema
55979 ** changes, in which case the read cursors can continue operating.
55980 ** If schema changes did occur in the transaction, then both read
55981 ** and write cursors must both be tripped.
55982 */
55983 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
 
55984 BtCursor *p;
55985 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
55986 if( pBtree==0 ) return;
55987 sqlite3BtreeEnter(pBtree);
55988 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
55989 int i;
55990 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ) continue;
55991 sqlite3BtreeClearCursor(p);
55992 p->eState = CURSOR_FAULT;
55993 p->skipNext = errCode;
55994 for(i=0; i<=p->iPage; i++){
55995 releasePage(p->apPage[i]);
@@ -55679,31 +55998,36 @@
55998 }
55999 sqlite3BtreeLeave(pBtree);
56000 }
56001
56002 /*
56003 ** Rollback the transaction in progress.
56004 **
56005 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
56006 ** Only write cursors are tripped if writeOnly is true but all cursors are
56007 ** tripped if writeOnly is false. Any attempt to use
56008 ** a tripped cursor will result in an error.
56009 **
56010 ** This will release the write lock on the database file. If there
56011 ** are no active cursors, it also releases the read lock.
56012 */
56013 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
56014 int rc;
56015 BtShared *pBt = p->pBt;
56016 MemPage *pPage1;
56017
56018 assert( writeOnly==1 || writeOnly==0 );
56019 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
56020 sqlite3BtreeEnter(p);
56021 if( tripCode==SQLITE_OK ){
56022 rc = tripCode = saveAllCursors(pBt, 0, 0);
56023 if( rc ) writeOnly = 0;
56024 }else{
56025 rc = SQLITE_OK;
56026 }
56027 if( tripCode ){
56028 sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
56029 }
56030 btreeIntegrity(p);
56031
56032 if( p->inTrans==TRANS_WRITE ){
56033 int rc2;
@@ -58120,49 +58444,266 @@
58444 #endif
58445 }
58446 }
58447
58448 /*
58449 ** Array apCell[] contains pointers to nCell b-tree page cells. The
58450 ** szCell[] array contains the size in bytes of each cell. This function
58451 ** replaces the current contents of page pPg with the contents of the cell
58452 ** array.
58453 **
58454 ** Some of the cells in apCell[] may currently be stored in pPg. This
58455 ** function works around problems caused by this by making a copy of any
58456 ** such cells before overwriting the page data.
58457 **
58458 ** The MemPage.nFree field is invalidated by this function. It is the
58459 ** responsibility of the caller to set it correctly.
58460 */
58461 static void rebuildPage(
58462 MemPage *pPg, /* Edit this page */
58463 int nCell, /* Final number of cells on page */
58464 u8 **apCell, /* Array of cells */
58465 u16 *szCell /* Array of cell sizes */
58466 ){
58467 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
58468 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
58469 const int usableSize = pPg->pBt->usableSize;
58470 u8 * const pEnd = &aData[usableSize];
58471 int i;
58472 u8 *pCellptr = pPg->aCellIdx;
58473 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
58474 u8 *pData;
58475
58476 i = get2byte(&aData[hdr+5]);
58477 memcpy(&pTmp[i], &aData[i], usableSize - i);
58478
58479 pData = pEnd;
58480 for(i=0; i<nCell; i++){
58481 u8 *pCell = apCell[i];
58482 if( pCell>aData && pCell<pEnd ){
58483 pCell = &pTmp[pCell - aData];
58484 }
58485 pData -= szCell[i];
58486 memcpy(pData, pCell, szCell[i]);
58487 put2byte(pCellptr, (pData - aData));
58488 pCellptr += 2;
58489 assert( szCell[i]==cellSizePtr(pPg, pCell) );
58490 }
58491
58492 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
58493 pPg->nCell = nCell;
58494 pPg->nOverflow = 0;
58495
58496 put2byte(&aData[hdr+1], 0);
58497 put2byte(&aData[hdr+3], pPg->nCell);
58498 put2byte(&aData[hdr+5], pData - aData);
58499 aData[hdr+7] = 0x00;
58500 }
58501
58502 /*
58503 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
58504 ** contains the size in bytes of each such cell. This function attempts to
58505 ** add the cells stored in the array to page pPg. If it cannot (because
58506 ** the page needs to be defragmented before the cells will fit), non-zero
58507 ** is returned. Otherwise, if the cells are added successfully, zero is
58508 ** returned.
58509 **
58510 ** Argument pCellptr points to the first entry in the cell-pointer array
58511 ** (part of page pPg) to populate. After cell apCell[0] is written to the
58512 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
58513 ** cell in the array. It is the responsibility of the caller to ensure
58514 ** that it is safe to overwrite this part of the cell-pointer array.
58515 **
58516 ** When this function is called, *ppData points to the start of the
58517 ** content area on page pPg. If the size of the content area is extended,
58518 ** *ppData is updated to point to the new start of the content area
58519 ** before returning.
58520 **
58521 ** Finally, argument pBegin points to the byte immediately following the
58522 ** end of the space required by this page for the cell-pointer area (for
58523 ** all cells - not just those inserted by the current call). If the content
58524 ** area must be extended to before this point in order to accomodate all
58525 ** cells in apCell[], then the cells do not fit and non-zero is returned.
58526 */
58527 static int pageInsertArray(
58528 MemPage *pPg, /* Page to add cells to */
58529 u8 *pBegin, /* End of cell-pointer array */
58530 u8 **ppData, /* IN/OUT: Page content -area pointer */
58531 u8 *pCellptr, /* Pointer to cell-pointer area */
58532 int nCell, /* Number of cells to add to pPg */
58533 u8 **apCell, /* Array of cells */
58534 u16 *szCell /* Array of cell sizes */
58535 ){
58536 int i;
58537 u8 *aData = pPg->aData;
58538 u8 *pData = *ppData;
58539 const int bFreelist = aData[1] || aData[2];
58540 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
58541 for(i=0; i<nCell; i++){
58542 int sz = szCell[i];
58543 int rc;
58544 u8 *pSlot;
58545 if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
58546 pData -= sz;
58547 if( pData<pBegin ) return 1;
58548 pSlot = pData;
58549 }
58550 memcpy(pSlot, apCell[i], sz);
58551 put2byte(pCellptr, (pSlot - aData));
58552 pCellptr += 2;
58553 }
58554 *ppData = pData;
58555 return 0;
58556 }
58557
58558 /*
58559 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
58560 ** contains the size in bytes of each such cell. This function adds the
58561 ** space associated with each cell in the array that is currently stored
58562 ** within the body of pPg to the pPg free-list. The cell-pointers and other
58563 ** fields of the page are not updated.
58564 **
58565 ** This function returns the total number of cells added to the free-list.
58566 */
58567 static int pageFreeArray(
58568 MemPage *pPg, /* Page to edit */
58569 int nCell, /* Cells to delete */
58570 u8 **apCell, /* Array of cells */
58571 u16 *szCell /* Array of cell sizes */
58572 ){
58573 u8 * const aData = pPg->aData;
58574 u8 * const pEnd = &aData[pPg->pBt->usableSize];
58575 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
58576 int nRet = 0;
58577 int i;
58578 u8 *pFree = 0;
58579 int szFree = 0;
58580
58581 for(i=0; i<nCell; i++){
58582 u8 *pCell = apCell[i];
58583 if( pCell>=pStart && pCell<pEnd ){
58584 int sz = szCell[i];
58585 if( pFree!=(pCell + sz) ){
58586 if( pFree ){
58587 assert( pFree>aData && (pFree - aData)<65536 );
58588 freeSpace(pPg, (u16)(pFree - aData), szFree);
58589 }
58590 pFree = pCell;
58591 szFree = sz;
58592 if( pFree+sz>pEnd ) return 0;
58593 }else{
58594 pFree = pCell;
58595 szFree += sz;
58596 }
58597 nRet++;
58598 }
58599 }
58600 if( pFree ){
58601 assert( pFree>aData && (pFree - aData)<65536 );
58602 freeSpace(pPg, (u16)(pFree - aData), szFree);
58603 }
58604 return nRet;
58605 }
58606
58607 /*
58608 ** The pPg->nFree field is invalid when this function returns. It is the
58609 ** responsibility of the caller to set it correctly.
58610 */
58611 static void editPage(
58612 MemPage *pPg, /* Edit this page */
58613 int iOld, /* Index of first cell currently on page */
58614 int iNew, /* Index of new first cell on page */
58615 int nNew, /* Final number of cells on page */
58616 u8 **apCell, /* Array of cells */
58617 u16 *szCell /* Array of cell sizes */
58618 ){
58619 u8 * const aData = pPg->aData;
58620 const int hdr = pPg->hdrOffset;
58621 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
58622 int nCell = pPg->nCell; /* Cells stored on pPg */
58623 u8 *pData;
58624 u8 *pCellptr;
58625 int i;
58626 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
58627 int iNewEnd = iNew + nNew;
58628
58629 #ifdef SQLITE_DEBUG
58630 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
58631 memcpy(pTmp, aData, pPg->pBt->usableSize);
58632 #endif
58633
58634 /* Remove cells from the start and end of the page */
58635 if( iOld<iNew ){
58636 int nShift = pageFreeArray(
58637 pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
58638 );
58639 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
58640 nCell -= nShift;
58641 }
58642 if( iNewEnd < iOldEnd ){
58643 nCell -= pageFreeArray(
58644 pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
58645 );
58646 }
58647
58648 pData = &aData[get2byte(&aData[hdr+5])];
58649 if( pData<pBegin ) goto editpage_fail;
58650
58651 /* Add cells to the start of the page */
58652 if( iNew<iOld ){
58653 int nAdd = iOld-iNew;
58654 pCellptr = pPg->aCellIdx;
58655 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
58656 if( pageInsertArray(
58657 pPg, pBegin, &pData, pCellptr,
58658 nAdd, &apCell[iNew], &szCell[iNew]
58659 ) ) goto editpage_fail;
58660 nCell += nAdd;
58661 }
58662
58663 /* Add any overflow cells */
58664 for(i=0; i<pPg->nOverflow; i++){
58665 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
58666 if( iCell>=0 && iCell<nNew ){
58667 pCellptr = &pPg->aCellIdx[iCell * 2];
58668 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
58669 nCell++;
58670 if( pageInsertArray(
58671 pPg, pBegin, &pData, pCellptr,
58672 1, &apCell[iCell + iNew], &szCell[iCell + iNew]
58673 ) ) goto editpage_fail;
58674 }
58675 }
58676
58677 /* Append cells to the end of the page */
58678 pCellptr = &pPg->aCellIdx[nCell*2];
58679 if( pageInsertArray(
58680 pPg, pBegin, &pData, pCellptr,
58681 nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
58682 ) ) goto editpage_fail;
58683
58684 pPg->nCell = nNew;
58685 pPg->nOverflow = 0;
58686
58687 put2byte(&aData[hdr+3], pPg->nCell);
58688 put2byte(&aData[hdr+5], pData - aData);
58689
58690 #ifdef SQLITE_DEBUG
58691 for(i=0; i<nNew && !CORRUPT_DB; i++){
58692 u8 *pCell = apCell[i+iNew];
58693 int iOff = get2byte(&pPg->aCellIdx[i*2]);
58694 if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
58695 pCell = &pTmp[pCell - aData];
58696 }
58697 assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
58698 }
58699 #endif
58700
58701 return;
58702 editpage_fail:
58703 /* Unable to edit this page. Rebuild it from scratch instead. */
58704 rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
58705 }
58706
58707 /*
58708 ** The following parameters determine how many adjacent pages get involved
58709 ** in a balancing operation. NN is the number of neighbors on either side
@@ -58230,11 +58771,12 @@
58771 u8 *pStop;
58772
58773 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
58774 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
58775 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
58776 rebuildPage(pNew, 1, &pCell, &szCell);
58777 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
58778
58779 /* If this is an auto-vacuum database, update the pointer map
58780 ** with entries for the new page, and any pointer from the
58781 ** cell on the page to an overflow page. If either of these
58782 ** operations fails, the return code is set, but the contents
@@ -58449,21 +58991,26 @@
58991 int subtotal; /* Subtotal of bytes in cells on one page */
58992 int iSpace1 = 0; /* First unused byte of aSpace1[] */
58993 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
58994 int szScratch; /* Size of scratch memory requested */
58995 MemPage *apOld[NB]; /* pPage and up to two siblings */
 
58996 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
58997 u8 *pRight; /* Location in parent of right-sibling pointer */
58998 u8 *apDiv[NB-1]; /* Divider cells in pParent */
58999 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
59000 int cntOld[NB+2]; /* Old index in aCell[] after i-th page */
59001 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
59002 u8 **apCell = 0; /* All cells begin balanced */
59003 u16 *szCell; /* Local size of all cells in apCell[] */
59004 u8 *aSpace1; /* Space for copies of dividers cells */
59005 Pgno pgno; /* Temp var to store a page number in */
59006 u8 abDone[NB+2]; /* True after i'th new page is populated */
59007 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
59008 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
59009 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
59010
59011 memset(abDone, 0, sizeof(abDone));
59012 pBt = pParent->pBt;
59013 assert( sqlite3_mutex_held(pBt->mutex) );
59014 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
59015
59016 #if 0
@@ -58568,16 +59115,18 @@
59115 nMaxCells = (nMaxCells + 3)&~3;
59116
59117 /*
59118 ** Allocate space for memory structures
59119 */
 
59120 szScratch =
59121 nMaxCells*sizeof(u8*) /* apCell */
59122 + nMaxCells*sizeof(u16) /* szCell */
59123 + pBt->pageSize; /* aSpace1 */
59124
59125 /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
59126 ** that is more than 6 times the database page size. */
59127 assert( szScratch<=6*pBt->pageSize );
59128 apCell = sqlite3ScratchMalloc( szScratch );
59129 if( apCell==0 ){
59130 rc = SQLITE_NOMEM;
59131 goto balance_cleanup;
59132 }
@@ -58586,12 +59135,12 @@
59135 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
59136
59137 /*
59138 ** Load pointers to all cells on sibling pages and the divider cells
59139 ** into the local apCell[] array. Make copies of the divider cells
59140 ** into space obtained from aSpace1[]. The divider cells have already
59141 ** been removed from pParent.
59142 **
59143 ** If the siblings are on leaf pages, then the child pointers of the
59144 ** divider cells are stripped from the cells before they are copied
59145 ** into aSpace1[]. In this way, all cells in apCell[] are without
59146 ** child pointers. If siblings are not leaves, then all cell in
@@ -58603,19 +59152,11 @@
59152 */
59153 leafCorrection = apOld[0]->leaf*4;
59154 leafData = apOld[0]->intKeyLeaf;
59155 for(i=0; i<nOld; i++){
59156 int limit;
59157 MemPage *pOld = apOld[i];
 
 
 
 
 
 
 
 
59158
59159 limit = pOld->nCell+pOld->nOverflow;
59160 if( pOld->nOverflow>0 ){
59161 for(j=0; j<limit; j++){
59162 assert( nCell<nMaxCells );
@@ -58632,10 +59173,11 @@
59173 apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
59174 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
59175 nCell++;
59176 }
59177 }
59178 cntOld[i] = nCell;
59179 if( i<nOld-1 && !leafData){
59180 u16 sz = (u16)szNew[i];
59181 u8 *pTemp;
59182 assert( nCell<nMaxCells );
59183 szCell[nCell] = sz;
@@ -58683,11 +59225,11 @@
59225 usableSpace = pBt->usableSize - 12 + leafCorrection;
59226 for(subtotal=k=i=0; i<nCell; i++){
59227 assert( i<nMaxCells );
59228 subtotal += szCell[i] + 2;
59229 if( subtotal > usableSpace ){
59230 szNew[k] = subtotal - szCell[i] - 2;
59231 cntNew[k] = i;
59232 if( leafData ){ i--; }
59233 subtotal = 0;
59234 k++;
59235 if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
@@ -58697,13 +59239,14 @@
59239 cntNew[k] = nCell;
59240 k++;
59241
59242 /*
59243 ** The packing computed by the previous block is biased toward the siblings
59244 ** on the left side (siblings with smaller keys). The left siblings are
59245 ** always nearly full, while the right-most sibling might be nearly empty.
59246 ** The next block of code attempts to adjust the packing of siblings to
59247 ** get a better balance.
59248 **
59249 ** This adjustment is more than an optimization. The packing above might
59250 ** be so out of balance as to be illegal. For example, the right-most
59251 ** sibling might be completely empty. This adjustment is not optional.
59252 */
@@ -58728,26 +59271,22 @@
59271 }
59272 szNew[i] = szRight;
59273 szNew[i-1] = szLeft;
59274 }
59275
59276 /* Sanity check: For a non-corrupt database file one of the follwing
59277 ** must be true:
59278 ** (1) We found one or more cells (cntNew[0])>0), or
59279 ** (2) pPage is a virtual root page. A virtual root page is when
59280 ** the real root page is page 1 and we are the only child of
59281 ** that page.
 
59282 */
59283 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
59284 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
59285 apOld[0]->pgno, apOld[0]->nCell,
59286 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
59287 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
 
 
 
59288 ));
59289
59290 /*
59291 ** Allocate k new pages. Reuse old pages where possible.
59292 */
@@ -58766,12 +59305,14 @@
59305 if( rc ) goto balance_cleanup;
59306 }else{
59307 assert( i>0 );
59308 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
59309 if( rc ) goto balance_cleanup;
59310 zeroPage(pNew, pageFlags);
59311 apNew[i] = pNew;
59312 nNew++;
59313 cntOld[i] = nCell;
59314
59315 /* Set the pointer-map entry for the new sibling page. */
59316 if( ISAUTOVACUUM ){
59317 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
59318 if( rc!=SQLITE_OK ){
@@ -58779,139 +59320,247 @@
59320 }
59321 }
59322 }
59323 }
59324
 
 
 
 
 
 
 
 
 
 
59325 /*
59326 ** Reassign page numbers so that the new pages are in ascending order.
59327 ** This helps to keep entries in the disk file in order so that a scan
59328 ** of the table is closer to a linear scan through the file. That in turn
59329 ** helps the operating system to deliver pages from the disk more rapidly.
59330 **
59331 ** An O(n^2) insertion sort algorithm is used, but since n is never more
59332 ** than (NB+2) (a small constant), that should not be a problem.
59333 **
59334 ** When NB==3, this one optimization makes the database about 25% faster
59335 ** for large insertions and deletions.
59336 */
59337 for(i=0; i<nNew; i++){
59338 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
59339 aPgFlags[i] = apNew[i]->pDbPage->flags;
59340 for(j=0; j<i; j++){
59341 if( aPgno[j]==aPgno[i] ){
59342 /* This branch is taken if the set of sibling pages somehow contains
59343 ** duplicate entries. This can happen if the database is corrupt.
59344 ** It would be simpler to detect this as part of the loop below, but
59345 ** we do the detection here in order to avoid populating the pager
59346 ** cache with two separate objects associated with the same
59347 ** page number. */
59348 assert( CORRUPT_DB );
59349 rc = SQLITE_CORRUPT_BKPT;
59350 goto balance_cleanup;
59351 }
59352 }
59353 }
59354 for(i=0; i<nNew; i++){
59355 int iBest = 0; /* aPgno[] index of page number to use */
59356 for(j=1; j<nNew; j++){
59357 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
59358 }
59359 pgno = aPgOrder[iBest];
59360 aPgOrder[iBest] = 0xffffffff;
59361 if( iBest!=i ){
59362 if( iBest>i ){
59363 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
59364 }
59365 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
59366 apNew[i]->pgno = pgno;
59367 }
59368 }
59369
59370 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
59371 "%d(%d nc=%d) %d(%d nc=%d)\n",
59372 apNew[0]->pgno, szNew[0], cntNew[0],
59373 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
59374 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
59375 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
59376 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
59377 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
59378 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
59379 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
59380 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
59381 ));
59382
59383 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
59384 put4byte(pRight, apNew[nNew-1]->pgno);
59385
59386 /* If the sibling pages are not leaves, ensure that the right-child pointer
59387 ** of the right-most new sibling page is set to the value that was
59388 ** originally in the same field of the right-most old sibling page. */
59389 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
59390 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
59391 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
59392 }
59393
59394 /* Make any required updates to pointer map entries associated with
59395 ** cells stored on sibling pages following the balance operation. Pointer
59396 ** map entries associated with divider cells are set by the insertCell()
59397 ** routine. The associated pointer map entries are:
59398 **
59399 ** a) if the cell contains a reference to an overflow chain, the
59400 ** entry associated with the first page in the overflow chain, and
59401 **
59402 ** b) if the sibling pages are not leaves, the child page associated
59403 ** with the cell.
59404 **
59405 ** If the sibling pages are not leaves, then the pointer map entry
59406 ** associated with the right-child of each sibling may also need to be
59407 ** updated. This happens below, after the sibling pages have been
59408 ** populated, not here.
59409 */
59410 if( ISAUTOVACUUM ){
59411 MemPage *pNew = apNew[0];
59412 u8 *aOld = pNew->aData;
59413 int cntOldNext = pNew->nCell + pNew->nOverflow;
59414 int usableSize = pBt->usableSize;
59415 int iNew = 0;
59416 int iOld = 0;
59417
59418 for(i=0; i<nCell; i++){
59419 u8 *pCell = apCell[i];
59420 if( i==cntOldNext ){
59421 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
59422 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
59423 aOld = pOld->aData;
59424 }
59425 if( i==cntNew[iNew] ){
59426 pNew = apNew[++iNew];
59427 if( !leafData ) continue;
59428 }
59429
59430 /* Cell pCell is destined for new sibling page pNew. Originally, it
59431 ** was either part of sibling page iOld (possibly an overflow cell),
59432 ** or else the divider cell to the left of sibling page iOld. So,
59433 ** if sibling page iOld had the same page number as pNew, and if
59434 ** pCell really was a part of sibling page iOld (not a divider or
59435 ** overflow cell), we can skip updating the pointer map entries. */
59436 if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){
59437 if( !leafCorrection ){
59438 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
59439 }
59440 if( szCell[i]>pNew->minLocal ){
59441 ptrmapPutOvflPtr(pNew, pCell, &rc);
59442 }
59443 }
59444 }
59445 }
59446
59447 /* Insert new divider cells into pParent. */
59448 for(i=0; i<nNew-1; i++){
59449 u8 *pCell;
59450 u8 *pTemp;
59451 int sz;
59452 MemPage *pNew = apNew[i];
 
 
 
 
 
 
59453 j = cntNew[i];
59454
59455 assert( j<nMaxCells );
59456 pCell = apCell[j];
59457 sz = szCell[j] + leafCorrection;
59458 pTemp = &aOvflSpace[iOvflSpace];
59459 if( !pNew->leaf ){
59460 memcpy(&pNew->aData[8], pCell, 4);
59461 }else if( leafData ){
59462 /* If the tree is a leaf-data tree, and the siblings are leaves,
59463 ** then there is no divider cell in apCell[]. Instead, the divider
59464 ** cell consists of the integer key for the right-most cell of
59465 ** the sibling-page assembled above only.
59466 */
59467 CellInfo info;
59468 j--;
59469 btreeParseCellPtr(pNew, apCell[j], &info);
59470 pCell = pTemp;
59471 sz = 4 + putVarint(&pCell[4], info.nKey);
59472 pTemp = 0;
59473 }else{
59474 pCell -= 4;
59475 /* Obscure case for non-leaf-data trees: If the cell at pCell was
59476 ** previously stored on a leaf node, and its reported size was 4
59477 ** bytes, then it may actually be smaller than this
59478 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
59479 ** any cell). But it is important to pass the correct size to
59480 ** insertCell(), so reparse the cell now.
59481 **
59482 ** Note that this can never happen in an SQLite data file, as all
59483 ** cells are at least 4 bytes. It only happens in b-trees used
59484 ** to evaluate "IN (SELECT ...)" and similar clauses.
59485 */
59486 if( szCell[j]==4 ){
59487 assert(leafCorrection==4);
59488 sz = cellSizePtr(pParent, pCell);
59489 }
59490 }
59491 iOvflSpace += sz;
59492 assert( sz<=pBt->maxLocal+23 );
59493 assert( iOvflSpace <= (int)pBt->pageSize );
59494 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
59495 if( rc!=SQLITE_OK ) goto balance_cleanup;
59496 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
59497 }
59498
59499 /* Now update the actual sibling pages. The order in which they are updated
59500 ** is important, as this code needs to avoid disrupting any page from which
59501 ** cells may still to be read. In practice, this means:
59502 **
59503 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
59504 ** then it is not safe to update page apNew[iPg] until after
59505 ** the left-hand sibling apNew[iPg-1] has been updated.
59506 **
59507 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
59508 ** then it is not safe to update page apNew[iPg] until after
59509 ** the right-hand sibling apNew[iPg+1] has been updated.
59510 **
59511 ** If neither of the above apply, the page is safe to update.
59512 **
59513 ** The iPg value in the following loop starts at nNew-1 goes down
59514 ** to 0, then back up to nNew-1 again, thus making two passes over
59515 ** the pages. On the initial downward pass, only condition (1) above
59516 ** needs to be tested because (2) will always be true from the previous
59517 ** step. On the upward pass, both conditions are always true, so the
59518 ** upwards pass simply processes pages that were missed on the downward
59519 ** pass.
59520 */
59521 for(i=1-nNew; i<nNew; i++){
59522 int iPg = i<0 ? -i : i;
59523 assert( iPg>=0 && iPg<nNew );
59524 if( abDone[iPg] ) continue; /* Skip pages already processed */
59525 if( i>=0 /* On the upwards pass, or... */
59526 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
59527 ){
59528 int iNew;
59529 int iOld;
59530 int nNewCell;
59531
59532 /* Verify condition (1): If cells are moving left, update iPg
59533 ** only after iPg-1 has already been updated. */
59534 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
59535
59536 /* Verify condition (2): If cells are moving right, update iPg
59537 ** only after iPg+1 has already been updated. */
59538 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
59539
59540 if( iPg==0 ){
59541 iNew = iOld = 0;
59542 nNewCell = cntNew[0];
59543 }else{
59544 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
59545 iNew = cntNew[iPg-1] + !leafData;
59546 nNewCell = cntNew[iPg] - iNew;
59547 }
59548
59549 editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
59550 abDone[iPg]++;
59551 apNew[iPg]->nFree = usableSpace-szNew[iPg];
59552 assert( apNew[iPg]->nOverflow==0 );
59553 assert( apNew[iPg]->nCell==nNewCell );
59554 }
59555 }
59556
59557 /* All pages have been processed exactly once */
59558 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
59559
59560 assert( nOld>0 );
59561 assert( nNew>0 );
 
 
 
 
59562
59563 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
59564 /* The root page of the b-tree now contains no cells. The only sibling
59565 ** page is the right-child of the parent. Copy the contents of the
59566 ** child page into the parent, decreasing the overall height of the
@@ -58920,130 +59569,54 @@
59569 **
59570 ** If this is an auto-vacuum database, the call to copyNodeContent()
59571 ** sets all pointer-map entries corresponding to database image pages
59572 ** for which the pointer is stored within the content being copied.
59573 **
59574 ** It is critical that the child page be defragmented before being
59575 ** copied into the parent, because if the parent is page 1 then it will
59576 ** by smaller than the child due to the database header, and so all the
59577 ** free space needs to be up front.
59578 */
59579 assert( nNew==1 );
59580 rc = defragmentPage(apNew[0]);
59581 testcase( rc!=SQLITE_OK );
59582 assert( apNew[0]->nFree ==
59583 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
59584 || rc!=SQLITE_OK
59585 );
59586 copyNodeContent(apNew[0], pParent, &rc);
59587 freePage(apNew[0], &rc);
59588 }else if( ISAUTOVACUUM && !leafCorrection ){
59589 /* Fix the pointer map entries associated with the right-child of each
59590 ** sibling page. All other pointer map entries have already been taken
59591 ** care of. */
59592 for(i=0; i<nNew; i++){
59593 u32 key = get4byte(&apNew[i]->aData[8]);
59594 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
59595 }
59596 }
59597
59598 assert( pParent->isInit );
59599 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
59600 nOld, nNew, nCell));
59601
59602 /* Free any old pages that were not reused as new pages.
59603 */
59604 for(i=nNew; i<nOld; i++){
59605 freePage(apOld[i], &rc);
59606 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59607
59608 #if 0
59609 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
59610 /* The ptrmapCheckPages() contains assert() statements that verify that
59611 ** all pointer map pages are set correctly. This is helpful while
59612 ** debugging. This is usually disabled because a corrupt database may
59613 ** cause an assert() statement to fail. */
59614 ptrmapCheckPages(apNew, nNew);
59615 ptrmapCheckPages(&pParent, 1);
59616 }
59617 #endif
 
 
 
 
 
59618
59619 /*
59620 ** Cleanup before returning.
59621 */
59622 balance_cleanup:
@@ -60865,10 +61438,15 @@
61438 */
61439 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
61440 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
61441 }
61442
61443 /*
61444 ** Return the size of the header added to each page by this module.
61445 */
61446 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); }
61447
61448 /************** End of btree.c ***********************************************/
61449 /************** Begin file backup.c ******************************************/
61450 /*
61451 ** 2009 January 28
61452 **
@@ -61004,10 +61582,17 @@
61582 const char *zDestDb, /* Name of database within pDestDb */
61583 sqlite3* pSrcDb, /* Database connection to read from */
61584 const char *zSrcDb /* Name of database within pSrcDb */
61585 ){
61586 sqlite3_backup *p; /* Value to return */
61587
61588 #ifdef SQLITE_ENABLE_API_ARMOR
61589 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
61590 (void)SQLITE_MISUSE_BKPT;
61591 return 0;
61592 }
61593 #endif
61594
61595 /* Lock the source database handle. The destination database
61596 ** handle is not locked in this routine, but it is locked in
61597 ** sqlite3_backup_step(). The user is required to ensure that no
61598 ** other thread accesses the destination handle for the duration
@@ -61201,10 +61786,13 @@
61786 int rc;
61787 int destMode; /* Destination journal mode */
61788 int pgszSrc = 0; /* Source page size */
61789 int pgszDest = 0; /* Destination page size */
61790
61791 #ifdef SQLITE_ENABLE_API_ARMOR
61792 if( p==0 ) return SQLITE_MISUSE_BKPT;
61793 #endif
61794 sqlite3_mutex_enter(p->pSrcDb->mutex);
61795 sqlite3BtreeEnter(p->pSrc);
61796 if( p->pDestDb ){
61797 sqlite3_mutex_enter(p->pDestDb->mutex);
61798 }
@@ -61464,11 +62052,11 @@
62052 }
62053 *pp = p->pNext;
62054 }
62055
62056 /* If a transaction is still open on the Btree, roll it back. */
62057 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
62058
62059 /* Set the error code of the destination database handle. */
62060 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
62061 if( p->pDestDb ){
62062 sqlite3Error(p->pDestDb, rc);
@@ -61490,18 +62078,30 @@
62078 /*
62079 ** Return the number of pages still to be backed up as of the most recent
62080 ** call to sqlite3_backup_step().
62081 */
62082 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
62083 #ifdef SQLITE_ENABLE_API_ARMOR
62084 if( p==0 ){
62085 (void)SQLITE_MISUSE_BKPT;
62086 return 0;
62087 }
62088 #endif
62089 return p->nRemaining;
62090 }
62091
62092 /*
62093 ** Return the total number of pages in the source database as of the most
62094 ** recent call to sqlite3_backup_step().
62095 */
62096 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
62097 #ifdef SQLITE_ENABLE_API_ARMOR
62098 if( p==0 ){
62099 (void)SQLITE_MISUSE_BKPT;
62100 return 0;
62101 }
62102 #endif
62103 return p->nPagecount;
62104 }
62105
62106 /*
62107 ** This function is called after the contents of page iPage of the
@@ -63788,10 +64388,38 @@
64388 }
64389 p->nOp += nOp;
64390 }
64391 return addr;
64392 }
64393
64394 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
64395 /*
64396 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
64397 */
64398 SQLITE_PRIVATE void sqlite3VdbeScanStatus(
64399 Vdbe *p, /* VM to add scanstatus() to */
64400 int addrExplain, /* Address of OP_Explain (or 0) */
64401 int addrLoop, /* Address of loop counter */
64402 int addrVisit, /* Address of rows visited counter */
64403 LogEst nEst, /* Estimated number of output rows */
64404 const char *zName /* Name of table or index being scanned */
64405 ){
64406 int nByte = (p->nScan+1) * sizeof(ScanStatus);
64407 ScanStatus *aNew;
64408 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
64409 if( aNew ){
64410 ScanStatus *pNew = &aNew[p->nScan++];
64411 pNew->addrExplain = addrExplain;
64412 pNew->addrLoop = addrLoop;
64413 pNew->addrVisit = addrVisit;
64414 pNew->nEst = nEst;
64415 pNew->zName = sqlite3DbStrDup(p->db, zName);
64416 p->aScan = aNew;
64417 }
64418 }
64419 #endif
64420
64421
64422 /*
64423 ** Change the value of the P1 operand for a specific instruction.
64424 ** This routine is useful when a large program is loaded from a
64425 ** static array using sqlite3VdbeAddOpList but we want to make a
@@ -64887,10 +65515,13 @@
65515 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
65516 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
65517 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
65518 &zCsr, zEnd, &nByte);
65519 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
65520 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
65521 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
65522 #endif
65523 if( nByte ){
65524 p->pFree = sqlite3DbMallocZero(db, nByte);
65525 }
65526 zCsr = p->pFree;
65527 zEnd = &zCsr[nByte];
@@ -64954,10 +65585,13 @@
65585 ** is used, for example, when a trigger sub-program is halted to restore
65586 ** control to the main program.
65587 */
65588 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
65589 Vdbe *v = pFrame->v;
65590 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
65591 v->anExec = pFrame->anExec;
65592 #endif
65593 v->aOnceFlag = pFrame->aOnceFlag;
65594 v->nOnceFlag = pFrame->nOnceFlag;
65595 v->aOp = pFrame->aOp;
65596 v->nOp = pFrame->nOp;
65597 v->aMem = pFrame->aMem;
@@ -64964,10 +65598,11 @@
65598 v->nMem = pFrame->nMem;
65599 v->apCsr = pFrame->apCsr;
65600 v->nCursor = pFrame->nCursor;
65601 v->db->lastRowid = pFrame->lastRowid;
65602 v->nChange = pFrame->nChange;
65603 v->db->nChange = pFrame->nDbChange;
65604 return pFrame->pc;
65605 }
65606
65607 /*
65608 ** Close all cursors.
@@ -65531,10 +66166,11 @@
66166 ** so, abort any other statements this handle currently has active.
66167 */
66168 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
66169 sqlite3CloseSavepoints(db);
66170 db->autoCommit = 1;
66171 p->nChange = 0;
66172 }
66173 }
66174 }
66175
66176 /* Check for immediate foreign key violations. */
@@ -65571,18 +66207,20 @@
66207 sqlite3VdbeLeave(p);
66208 return SQLITE_BUSY;
66209 }else if( rc!=SQLITE_OK ){
66210 p->rc = rc;
66211 sqlite3RollbackAll(db, SQLITE_OK);
66212 p->nChange = 0;
66213 }else{
66214 db->nDeferredCons = 0;
66215 db->nDeferredImmCons = 0;
66216 db->flags &= ~SQLITE_DeferFKs;
66217 sqlite3CommitInternalChanges(db);
66218 }
66219 }else{
66220 sqlite3RollbackAll(db, SQLITE_OK);
66221 p->nChange = 0;
66222 }
66223 db->nStatement = 0;
66224 }else if( eStatementOp==0 ){
66225 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
66226 eStatementOp = SAVEPOINT_RELEASE;
@@ -65590,10 +66228,11 @@
66228 eStatementOp = SAVEPOINT_ROLLBACK;
66229 }else{
66230 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
66231 sqlite3CloseSavepoints(db);
66232 db->autoCommit = 1;
66233 p->nChange = 0;
66234 }
66235 }
66236
66237 /* If eStatementOp is non-zero, then a statement transaction needs to
66238 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
@@ -65610,10 +66249,11 @@
66249 p->zErrMsg = 0;
66250 }
66251 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
66252 sqlite3CloseSavepoints(db);
66253 db->autoCommit = 1;
66254 p->nChange = 0;
66255 }
66256 }
66257
66258 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
66259 ** has been rolled back, update the database connection change-counter.
@@ -65871,10 +66511,16 @@
66511 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
66512 vdbeFreeOpArray(db, p->aOp, p->nOp);
66513 sqlite3DbFree(db, p->aColName);
66514 sqlite3DbFree(db, p->zSql);
66515 sqlite3DbFree(db, p->pFree);
66516 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
66517 for(i=0; i<p->nScan; i++){
66518 sqlite3DbFree(db, p->aScan[i].zName);
66519 }
66520 sqlite3DbFree(db, p->aScan);
66521 #endif
66522 }
66523
66524 /*
66525 ** Delete an entire VDBE.
66526 */
@@ -68238,15 +68884,23 @@
68884 sqlite3_stmt *pStmt,
68885 int N,
68886 const void *(*xFunc)(Mem*),
68887 int useType
68888 ){
68889 const void *ret;
68890 Vdbe *p;
68891 int n;
68892 sqlite3 *db;
68893 #ifdef SQLITE_ENABLE_API_ARMOR
68894 if( pStmt==0 ){
68895 (void)SQLITE_MISUSE_BKPT;
68896 return 0;
68897 }
68898 #endif
68899 ret = 0;
68900 p = (Vdbe *)pStmt;
68901 db = p->db;
68902 assert( db!=0 );
68903 n = sqlite3_column_count(pStmt);
68904 if( N<n && N>=0 ){
68905 N += useType*n;
68906 sqlite3_mutex_enter(db->mutex);
@@ -68707,10 +69361,16 @@
69361 ** prepared statement for the database connection. Return NULL if there
69362 ** are no more.
69363 */
69364 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
69365 sqlite3_stmt *pNext;
69366 #ifdef SQLITE_ENABLE_API_ARMOR
69367 if( !sqlite3SafetyCheckOk(pDb) ){
69368 (void)SQLITE_MISUSE_BKPT;
69369 return 0;
69370 }
69371 #endif
69372 sqlite3_mutex_enter(pDb->mutex);
69373 if( pStmt==0 ){
69374 pNext = (sqlite3_stmt*)pDb->pVdbe;
69375 }else{
69376 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
@@ -68722,15 +69382,91 @@
69382 /*
69383 ** Return the value of a status counter for a prepared statement
69384 */
69385 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
69386 Vdbe *pVdbe = (Vdbe*)pStmt;
69387 u32 v;
69388 #ifdef SQLITE_ENABLE_API_ARMOR
69389 if( !pStmt ){
69390 (void)SQLITE_MISUSE_BKPT;
69391 return 0;
69392 }
69393 #endif
69394 v = pVdbe->aCounter[op];
69395 if( resetFlag ) pVdbe->aCounter[op] = 0;
69396 return (int)v;
69397 }
69398
69399 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69400 /*
69401 ** Return status data for a single loop within query pStmt.
69402 */
69403 SQLITE_API int sqlite3_stmt_scanstatus(
69404 sqlite3_stmt *pStmt, /* Prepared statement being queried */
69405 int idx, /* Index of loop to report on */
69406 int iScanStatusOp, /* Which metric to return */
69407 void *pOut /* OUT: Write the answer here */
69408 ){
69409 Vdbe *p = (Vdbe*)pStmt;
69410 ScanStatus *pScan;
69411 if( idx<0 || idx>=p->nScan ) return 1;
69412 pScan = &p->aScan[idx];
69413 switch( iScanStatusOp ){
69414 case SQLITE_SCANSTAT_NLOOP: {
69415 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
69416 break;
69417 }
69418 case SQLITE_SCANSTAT_NVISIT: {
69419 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
69420 break;
69421 }
69422 case SQLITE_SCANSTAT_EST: {
69423 double r = 1.0;
69424 LogEst x = pScan->nEst;
69425 while( x<100 ){
69426 x += 10;
69427 r *= 0.5;
69428 }
69429 *(double*)pOut = r*sqlite3LogEstToInt(x);
69430 break;
69431 }
69432 case SQLITE_SCANSTAT_NAME: {
69433 *(const char**)pOut = pScan->zName;
69434 break;
69435 }
69436 case SQLITE_SCANSTAT_EXPLAIN: {
69437 if( pScan->addrExplain ){
69438 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
69439 }else{
69440 *(const char**)pOut = 0;
69441 }
69442 break;
69443 }
69444 case SQLITE_SCANSTAT_SELECTID: {
69445 if( pScan->addrExplain ){
69446 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
69447 }else{
69448 *(int*)pOut = -1;
69449 }
69450 break;
69451 }
69452 default: {
69453 return 1;
69454 }
69455 }
69456 return 0;
69457 }
69458
69459 /*
69460 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
69461 */
69462 SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
69463 Vdbe *p = (Vdbe*)pStmt;
69464 memset(p->anExec, 0, p->nOp * sizeof(i64));
69465 }
69466 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
69467
69468 /************** End of vdbeapi.c *********************************************/
69469 /************** Begin file vdbetrace.c ***************************************/
69470 /*
69471 ** 2009 November 25
69472 **
@@ -69612,10 +70348,13 @@
70348 #ifdef VDBE_PROFILE
70349 start = sqlite3Hwtime();
70350 #endif
70351 nVmStep++;
70352 pOp = &aOp[pc];
70353 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
70354 if( p->anExec ) p->anExec[pc]++;
70355 #endif
70356
70357 /* Only allow tracing if SQLITE_DEBUG is defined.
70358 */
70359 #ifdef SQLITE_DEBUG
70360 if( db->flags & SQLITE_VdbeTrace ){
@@ -71302,11 +72041,11 @@
72041 assert( pReg->flags & MEM_Blob );
72042 assert( memIsValid(pReg) );
72043 pC->payloadSize = pC->szRow = avail = pReg->n;
72044 pC->aRow = (u8*)pReg->z;
72045 }else{
72046 sqlite3VdbeMemSetNull(pDest);
72047 goto op_column_out;
72048 }
72049 }else{
72050 assert( pCrsr );
72051 if( pC->isTable==0 ){
@@ -71826,23 +72565,28 @@
72565 goto vdbe_return;
72566 }
72567 db->isTransactionSavepoint = 0;
72568 rc = p->rc;
72569 }else{
72570 int isSchemaChange;
72571 iSavepoint = db->nSavepoint - iSavepoint - 1;
72572 if( p1==SAVEPOINT_ROLLBACK ){
72573 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
72574 for(ii=0; ii<db->nDb; ii++){
72575 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT,
72576 isSchemaChange==0);
72577 }
72578 }else{
72579 isSchemaChange = 0;
72580 }
72581 for(ii=0; ii<db->nDb; ii++){
72582 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
72583 if( rc!=SQLITE_OK ){
72584 goto abort_due_to_error;
72585 }
72586 }
72587 if( isSchemaChange ){
72588 sqlite3ExpirePreparedStatements(db);
72589 sqlite3ResetAllSchemasOfConnection(db);
72590 db->flags = (db->flags | SQLITE_InternChanges);
72591 }
72592 }
@@ -72235,11 +72979,11 @@
72979 assert( p->bIsReader );
72980 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
72981 || p->readOnly==0 );
72982
72983 if( p->expired ){
72984 rc = SQLITE_ABORT_ROLLBACK;
72985 break;
72986 }
72987
72988 nField = 0;
72989 pKeyInfo = 0;
@@ -72799,14 +73543,15 @@
73543 }
73544 pIdxKey = &r;
73545 }else{
73546 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
73547 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
73548 );
73549 if( pIdxKey==0 ) goto no_mem;
73550 assert( pIn3->flags & MEM_Blob );
73551 /* assert( (pIn3->flags & MEM_Zero)==0 ); // zeroblobs already expanded */
73552 ExpandBlob(pIn3);
73553 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
73554 }
73555 pIdxKey->default_rc = 0;
73556 if( pOp->opcode==OP_NoConflict ){
73557 /* For the OP_NoConflict opcode, take the jump if any of the
@@ -73492,13 +74237,13 @@
74237 }
74238 /* Opcode: Rewind P1 P2 * * *
74239 **
74240 ** The next use of the Rowid or Column or Next instruction for P1
74241 ** will refer to the first entry in the database table or index.
74242 ** If the table or index is empty, jump immediately to P2.
74243 ** If the table or index is not empty, fall through to the following
74244 ** instruction.
74245 **
74246 ** This opcode leaves the cursor configured to move in forward order,
74247 ** from the beginning toward the end. In other words, the cursor is
74248 ** configured to use Next, not Prev.
74249 */
@@ -74410,10 +75155,13 @@
75155 pFrame->aOp = p->aOp;
75156 pFrame->nOp = p->nOp;
75157 pFrame->token = pProgram->token;
75158 pFrame->aOnceFlag = p->aOnceFlag;
75159 pFrame->nOnceFlag = p->nOnceFlag;
75160 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
75161 pFrame->anExec = p->anExec;
75162 #endif
75163
75164 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
75165 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
75166 pMem->flags = MEM_Undefined;
75167 pMem->db = db;
@@ -74427,10 +75175,11 @@
75175
75176 p->nFrame++;
75177 pFrame->pParent = p->pFrame;
75178 pFrame->lastRowid = lastRowid;
75179 pFrame->nChange = p->nChange;
75180 pFrame->nDbChange = p->db->nChange;
75181 p->nChange = 0;
75182 p->pFrame = pFrame;
75183 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
75184 p->nMem = pFrame->nChildMem;
75185 p->nCursor = (u16)pFrame->nChildCsr;
@@ -74437,10 +75186,13 @@
75186 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
75187 p->aOp = aOp = pProgram->aOp;
75188 p->nOp = pProgram->nOp;
75189 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
75190 p->nOnceFlag = pProgram->nOnce;
75191 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
75192 p->anExec = 0;
75193 #endif
75194 pc = -1;
75195 memset(p->aOnceFlag, 0, p->nOnceFlag);
75196
75197 break;
75198 }
@@ -75625,10 +76377,15 @@
76377 char *zErr = 0;
76378 Table *pTab;
76379 Parse *pParse = 0;
76380 Incrblob *pBlob = 0;
76381
76382 #ifdef SQLITE_ENABLE_API_ARMOR
76383 if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){
76384 return SQLITE_MISUSE_BKPT;
76385 }
76386 #endif
76387 flags = !!flags; /* flags = (flags ? 1 : 0); */
76388 *ppBlob = 0;
76389
76390 sqlite3_mutex_enter(db->mutex);
76391
@@ -75843,11 +76600,10 @@
76600 v = (Vdbe*)p->pStmt;
76601
76602 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
76603 /* Request is out of range. Return a transient error. */
76604 rc = SQLITE_ERROR;
 
76605 }else if( v==0 ){
76606 /* If there is no statement handle, then the blob-handle has
76607 ** already been invalidated. Return SQLITE_ABORT in this case.
76608 */
76609 rc = SQLITE_ABORT;
@@ -75861,14 +76617,14 @@
76617 sqlite3BtreeLeaveCursor(p->pCsr);
76618 if( rc==SQLITE_ABORT ){
76619 sqlite3VdbeFinalize(v);
76620 p->pStmt = 0;
76621 }else{
 
76622 v->rc = rc;
76623 }
76624 }
76625 sqlite3Error(db, rc);
76626 rc = sqlite3ApiExit(db, rc);
76627 sqlite3_mutex_leave(db->mutex);
76628 return rc;
76629 }
76630
@@ -76041,11 +76797,11 @@
76797 ** itself.
76798 **
76799 ** The sorter is running in multi-threaded mode if (a) the library was built
76800 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
76801 ** than zero, and (b) worker threads have been enabled at runtime by calling
76802 ** "PRAGMA threads=N" with some value of N greater than 0.
76803 **
76804 ** When Rewind() is called, any data remaining in memory is flushed to a
76805 ** final PMA. So at this point the data is stored in some number of sorted
76806 ** PMAs within temporary files on disk.
76807 **
@@ -76786,15 +77542,13 @@
77542 pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
77543 mxCache = db->aDb[0].pSchema->cache_size;
77544 if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
77545 pSorter->mxPmaSize = mxCache * pgsz;
77546
77547 /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
77548 ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
77549 ** large heap allocations.
 
 
77550 */
77551 if( sqlite3GlobalConfig.pScratch==0 ){
77552 assert( pSorter->iMemory==0 );
77553 pSorter->nMemory = pgsz;
77554 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
@@ -79162,19 +79916,19 @@
79916 **
79917 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
79918 ** is a helper function - a callback for the tree walker.
79919 */
79920 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
79921 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
79922 return WRC_Continue;
79923 }
79924 static void incrAggFunctionDepth(Expr *pExpr, int N){
79925 if( N>0 ){
79926 Walker w;
79927 memset(&w, 0, sizeof(w));
79928 w.xExprCallback = incrAggDepth;
79929 w.u.n = N;
79930 sqlite3WalkExpr(&w, pExpr);
79931 }
79932 }
79933
79934 /*
@@ -79718,11 +80472,11 @@
80472 double r = -1.0;
80473 if( p->op!=TK_FLOAT ) return -1;
80474 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
80475 assert( r>=0.0 );
80476 if( r>1.0 ) return -1;
80477 return (int)(r*134217728.0);
80478 }
80479
80480 /*
80481 ** This routine is callback for sqlite3WalkExpr().
80482 **
@@ -79850,11 +80604,11 @@
80604 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
80605 ** likelihood(X,0.9375).
80606 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
80607 ** likelihood(X,0.9375). */
80608 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
80609 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
80610 }
80611 }
80612 #ifndef SQLITE_OMIT_AUTHORIZATION
80613 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
80614 if( auth!=SQLITE_OK ){
@@ -81807,69 +82561,79 @@
82561 sqlite3DbFree(db, pList->a);
82562 sqlite3DbFree(db, pList);
82563 }
82564
82565 /*
82566 ** These routines are Walker callbacks used to check expressions to
82567 ** see if they are "constant" for some definition of constant. The
82568 ** Walker.eCode value determines the type of "constant" we are looking
82569 ** for.
82570 **
82571 ** These callback routines are used to implement the following:
82572 **
82573 ** sqlite3ExprIsConstant() pWalker->eCode==1
82574 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
82575 ** sqlite3ExprRefOneTableOnly() pWalker->eCode==3
82576 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
82577 **
82578 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
82579 ** is found to not be a constant.
82580 **
82581 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
82582 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
82583 ** an existing schema and 4 when processing a new statement. A bound
82584 ** parameter raises an error for new statements, but is silently converted
82585 ** to NULL for existing schemas. This allows sqlite_master tables that
82586 ** contain a bound parameter because they were generated by older versions
82587 ** of SQLite to be parsed by newer versions of SQLite without raising a
82588 ** malformed schema error.
82589 */
82590 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
82591
82592 /* If pWalker->eCode is 2 then any term of the expression that comes from
82593 ** the ON or USING clauses of a left join disqualifies the expression
82594 ** from being considered constant. */
82595 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
82596 pWalker->eCode = 0;
82597 return WRC_Abort;
82598 }
82599
82600 switch( pExpr->op ){
82601 /* Consider functions to be constant if all their arguments are constant
82602 ** and either pWalker->eCode==4 or 5 or the function has the
82603 ** SQLITE_FUNC_CONST flag. */
82604 case TK_FUNCTION:
82605 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){
82606 return WRC_Continue;
82607 }else{
82608 pWalker->eCode = 0;
82609 return WRC_Abort;
82610 }
 
82611 case TK_ID:
82612 case TK_COLUMN:
82613 case TK_AGG_FUNCTION:
82614 case TK_AGG_COLUMN:
82615 testcase( pExpr->op==TK_ID );
82616 testcase( pExpr->op==TK_COLUMN );
82617 testcase( pExpr->op==TK_AGG_FUNCTION );
82618 testcase( pExpr->op==TK_AGG_COLUMN );
82619 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
82620 return WRC_Continue;
82621 }else{
82622 pWalker->eCode = 0;
82623 return WRC_Abort;
82624 }
82625 case TK_VARIABLE:
82626 if( pWalker->eCode==5 ){
82627 /* Silently convert bound parameters that appear inside of CREATE
82628 ** statements into a NULL when parsing the CREATE statement text out
82629 ** of the sqlite_master table */
82630 pExpr->op = TK_NULL;
82631 }else if( pWalker->eCode==4 ){
82632 /* A bound parameter in a CREATE statement that originates from
82633 ** sqlite3_prepare() causes an error */
82634 pWalker->eCode = 0;
82635 return WRC_Abort;
82636 }
82637 /* Fall through */
82638 default:
82639 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
@@ -81877,57 +82641,68 @@
82641 return WRC_Continue;
82642 }
82643 }
82644 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
82645 UNUSED_PARAMETER(NotUsed);
82646 pWalker->eCode = 0;
82647 return WRC_Abort;
82648 }
82649 static int exprIsConst(Expr *p, int initFlag, int iCur){
82650 Walker w;
82651 memset(&w, 0, sizeof(w));
82652 w.eCode = initFlag;
82653 w.xExprCallback = exprNodeIsConstant;
82654 w.xSelectCallback = selectNodeIsConstant;
82655 w.u.iCur = iCur;
82656 sqlite3WalkExpr(&w, p);
82657 return w.eCode;
82658 }
82659
82660 /*
82661 ** Walk an expression tree. Return non-zero if the expression is constant
82662 ** and 0 if it involves variables or function calls.
82663 **
82664 ** For the purposes of this function, a double-quoted string (ex: "abc")
82665 ** is considered a variable but a single-quoted string (ex: 'abc') is
82666 ** a constant.
82667 */
82668 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
82669 return exprIsConst(p, 1, 0);
82670 }
82671
82672 /*
82673 ** Walk an expression tree. Return non-zero if the expression is constant
82674 ** that does no originate from the ON or USING clauses of a join.
82675 ** Return 0 if it involves variables or function calls or terms from
82676 ** an ON or USING clause.
82677 */
82678 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
82679 return exprIsConst(p, 2, 0);
82680 }
82681
82682 /*
82683 ** Walk an expression tree. Return non-zero if the expression constant
82684 ** for any single row of the table with cursor iCur. In other words, the
82685 ** expression must not refer to any non-deterministic function nor any
82686 ** table other than iCur.
82687 */
82688 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
82689 return exprIsConst(p, 3, iCur);
82690 }
82691
82692 /*
82693 ** Walk an expression tree. Return non-zero if the expression is constant
82694 ** or a function call with constant arguments. Return and 0 if there
82695 ** are any variables.
82696 **
82697 ** For the purposes of this function, a double-quoted string (ex: "abc")
82698 ** is considered a variable but a single-quoted string (ex: 'abc') is
82699 ** a constant.
82700 */
82701 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
82702 assert( isInit==0 || isInit==1 );
82703 return exprIsConst(p, 4+isInit, 0);
82704 }
82705
82706 /*
82707 ** If the expression p codes a constant integer that is small enough
82708 ** to fit in a 32-bit integer, return 1 and put the value of the integer
@@ -87374,10 +88149,11 @@
88149 nSample--;
88150 }else{
88151 nRow = pIdx->aiRowEst[0];
88152 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
88153 }
88154 pIdx->nRowEst0 = nRow;
88155
88156 /* Set nSum to the number of distinct (iCol+1) field prefixes that
88157 ** occur in the stat4 table for this index. Set sumEq to the sum of
88158 ** the nEq values for column iCol for the same set (adding the value
88159 ** only once where there exist duplicate prefixes). */
@@ -87635,11 +88411,11 @@
88411 }
88412
88413
88414 /* Load the statistics from the sqlite_stat4 table. */
88415 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
88416 if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
88417 int lookasideEnabled = db->lookaside.bEnabled;
88418 db->lookaside.bEnabled = 0;
88419 rc = loadStat4(db, sInfo.zDatabase);
88420 db->lookaside.bEnabled = lookasideEnabled;
88421 }
@@ -88317,10 +89093,13 @@
89093 SQLITE_API int sqlite3_set_authorizer(
89094 sqlite3 *db,
89095 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
89096 void *pArg
89097 ){
89098 #ifdef SQLITE_ENABLE_API_ARMOR
89099 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
89100 #endif
89101 sqlite3_mutex_enter(db->mutex);
89102 db->xAuth = (sqlite3_xauth)xAuth;
89103 db->pAuthArg = pArg;
89104 sqlite3ExpirePreparedStatements(db);
89105 sqlite3_mutex_leave(db->mutex);
@@ -88811,11 +89590,15 @@
89590 ** See also sqlite3LocateTable().
89591 */
89592 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
89593 Table *p = 0;
89594 int i;
89595
89596 #ifdef SQLITE_ENABLE_API_ARMOR
89597 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0;
89598 #endif
89599
89600 /* All mutexes are required for schema access. Make sure we hold them. */
89601 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
89602 #if SQLITE_USER_AUTHENTICATION
89603 /* Only the admin user is allowed to know that the sqlite_user table
89604 ** exists */
@@ -103834,13 +104617,16 @@
104617 Vdbe *pOld, /* VM being reprepared */
104618 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
104619 const char **pzTail /* OUT: End of parsed string */
104620 ){
104621 int rc;
104622
104623 #ifdef SQLITE_ENABLE_API_ARMOR
104624 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
104625 #endif
104626 *ppStmt = 0;
104627 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
104628 return SQLITE_MISUSE_BKPT;
104629 }
104630 sqlite3_mutex_enter(db->mutex);
104631 sqlite3BtreeEnterAll(db);
104632 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
@@ -103943,13 +104729,15 @@
104729 */
104730 char *zSql8;
104731 const char *zTail8 = 0;
104732 int rc = SQLITE_OK;
104733
104734 #ifdef SQLITE_ENABLE_API_ARMOR
104735 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
104736 #endif
104737 *ppStmt = 0;
104738 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
104739 return SQLITE_MISUSE_BKPT;
104740 }
104741 if( nBytes>=0 ){
104742 int sz;
104743 const char *z = (const char*)zSql;
@@ -109658,10 +110446,13 @@
110446 char **pzErrMsg /* Write error messages here */
110447 ){
110448 int rc;
110449 TabResult res;
110450
110451 #ifdef SQLITE_ENABLE_API_ARMOR
110452 if( pazResult==0 ) return SQLITE_MISUSE_BKPT;
110453 #endif
110454 *pazResult = 0;
110455 if( pnColumn ) *pnColumn = 0;
110456 if( pnRow ) *pnRow = 0;
110457 if( pzErrMsg ) *pzErrMsg = 0;
110458 res.zErrMsg = 0;
@@ -111721,11 +112512,11 @@
112512 ** Two writes per page are required in step (3) because the original
112513 ** database content must be written into the rollback journal prior to
112514 ** overwriting the database with the vacuumed content.
112515 **
112516 ** Only 1x temporary space and only 1x writes would be required if
112517 ** the copy of step (3) were replaced by deleting the original database
112518 ** and renaming the transient database as the original. But that will
112519 ** not work if other processes are attached to the original database.
112520 ** And a power loss in between deleting the original and renaming the
112521 ** transient would cause the database file to appear to be deleted
112522 ** following reboot.
@@ -112079,10 +112870,13 @@
112870 sqlite3 *db, /* Database in which module is registered */
112871 const char *zName, /* Name assigned to this module */
112872 const sqlite3_module *pModule, /* The definition of the module */
112873 void *pAux /* Context pointer for xCreate/xConnect */
112874 ){
112875 #ifdef SQLITE_ENABLE_API_ARMOR
112876 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
112877 #endif
112878 return createModule(db, zName, pModule, pAux, 0);
112879 }
112880
112881 /*
112882 ** External API function used to create a new virtual-table module.
@@ -112092,10 +112886,13 @@
112886 const char *zName, /* Name assigned to this module */
112887 const sqlite3_module *pModule, /* The definition of the module */
112888 void *pAux, /* Context pointer for xCreate/xConnect */
112889 void (*xDestroy)(void *) /* Module destructor function */
112890 ){
112891 #ifdef SQLITE_ENABLE_API_ARMOR
112892 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
112893 #endif
112894 return createModule(db, zName, pModule, pAux, xDestroy);
112895 }
112896
112897 /*
112898 ** Lock the virtual table so that it cannot be disconnected.
@@ -112696,10 +113493,13 @@
113493
113494 int rc = SQLITE_OK;
113495 Table *pTab;
113496 char *zErr = 0;
113497
113498 #ifdef SQLITE_ENABLE_API_ARMOR
113499 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113500 #endif
113501 sqlite3_mutex_enter(db->mutex);
113502 if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
113503 sqlite3Error(db, SQLITE_MISUSE);
113504 sqlite3_mutex_leave(db->mutex);
113505 return SQLITE_MISUSE_BKPT;
@@ -113052,10 +113852,13 @@
113852 */
113853 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
113854 static const unsigned char aMap[] = {
113855 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
113856 };
113857 #ifdef SQLITE_ENABLE_API_ARMOR
113858 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113859 #endif
113860 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
113861 assert( OE_Ignore==4 && OE_Replace==5 );
113862 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
113863 return (int)aMap[db->vtabOnConflict-1];
113864 }
@@ -113067,12 +113870,14 @@
113870 */
113871 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
113872 va_list ap;
113873 int rc = SQLITE_OK;
113874
113875 #ifdef SQLITE_ENABLE_API_ARMOR
113876 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
113877 #endif
113878 sqlite3_mutex_enter(db->mutex);
 
113879 va_start(ap, op);
113880 switch( op ){
113881 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
113882 VtabCtx *p = db->pVtabCtx;
113883 if( !p ){
@@ -113203,10 +114008,13 @@
114008 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
114009 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
114010 } u;
114011 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
114012 Bitmask notReady; /* FROM entries not usable at this level */
114013 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
114014 int addrVisit; /* Address at which row is visited */
114015 #endif
114016 };
114017
114018 /*
114019 ** Each instance of this object represents an algorithm for evaluating one
114020 ** term of a join. Every term of the FROM clause will have at least
@@ -113233,11 +114041,10 @@
114041 LogEst rRun; /* Cost of running each loop */
114042 LogEst nOut; /* Estimated number of output rows */
114043 union {
114044 struct { /* Information for internal btree tables */
114045 u16 nEq; /* Number of equality constraints */
 
114046 Index *pIndex; /* Index used, or NULL */
114047 } btree;
114048 struct { /* Information for virtual tables */
114049 int idxNum; /* Index number */
114050 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
@@ -113246,16 +114053,17 @@
114053 char *idxStr; /* Index identifier string */
114054 } vtab;
114055 } u;
114056 u32 wsFlags; /* WHERE_* flags describing the plan */
114057 u16 nLTerm; /* Number of entries in aLTerm[] */
114058 u16 nSkip; /* Number of NULL aLTerm[] entries */
114059 /**** whereLoopXfer() copies fields above ***********************/
114060 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
114061 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
114062 WhereTerm **aLTerm; /* WhereTerms used */
114063 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
114064 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
114065 };
114066
114067 /* This object holds the prerequisites and the cost of running a
114068 ** subquery on one operand of an OR operator in the WHERE clause.
114069 ** See WhereOrSet for additional information
@@ -113577,10 +114385,11 @@
114385 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
114386 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
114387 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
114388 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
114389 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
114390 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
114391
114392 /************** End of whereInt.h ********************************************/
114393 /************** Continuing where we left off in where.c **********************/
114394
114395 /*
@@ -113787,11 +114596,11 @@
114596 }
114597 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
114598 }
114599 pTerm = &pWC->a[idx = pWC->nTerm++];
114600 if( p && ExprHasProperty(p, EP_Unlikely) ){
114601 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
114602 }else{
114603 pTerm->truthProb = 1;
114604 }
114605 pTerm->pExpr = sqlite3ExprSkipCollate(p);
114606 pTerm->wtFlags = wtFlags;
@@ -114317,10 +115126,19 @@
115126 if( pDerived ){
115127 pDerived->flags |= pBase->flags & EP_FromJoin;
115128 pDerived->iRightJoinTable = pBase->iRightJoinTable;
115129 }
115130 }
115131
115132 /*
115133 ** Mark term iChild as being a child of term iParent
115134 */
115135 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
115136 pWC->a[iChild].iParent = iParent;
115137 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
115138 pWC->a[iParent].nChild++;
115139 }
115140
115141 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
115142 /*
115143 ** Analyze a term that consists of two or more OR-connected
115144 ** subterms. So in:
@@ -114615,12 +115433,11 @@
115433 pNew->x.pList = pList;
115434 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
115435 testcase( idxNew==0 );
115436 exprAnalyze(pSrc, pWC, idxNew);
115437 pTerm = &pWC->a[idxTerm];
115438 markTermAsChild(pWC, idxNew, idxTerm);
 
115439 }else{
115440 sqlite3ExprListDelete(db, pList);
115441 }
115442 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
115443 }
@@ -114718,13 +115535,12 @@
115535 return;
115536 }
115537 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
115538 if( idxNew==0 ) return;
115539 pNew = &pWC->a[idxNew];
115540 markTermAsChild(pWC, idxNew, idxTerm);
115541 pTerm = &pWC->a[idxTerm];
 
115542 pTerm->wtFlags |= TERM_COPIED;
115543 if( pExpr->op==TK_EQ
115544 && !ExprHasProperty(pExpr, EP_FromJoin)
115545 && OptimizationEnabled(db, SQLITE_Transitive)
115546 ){
@@ -114777,13 +115593,12 @@
115593 transferJoinMarkings(pNewExpr, pExpr);
115594 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
115595 testcase( idxNew==0 );
115596 exprAnalyze(pSrc, pWC, idxNew);
115597 pTerm = &pWC->a[idxTerm];
115598 markTermAsChild(pWC, idxNew, idxTerm);
115599 }
 
115600 }
115601 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
115602
115603 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
115604 /* Analyze a term that is composed of two or more subterms connected by
@@ -114854,13 +115669,12 @@
115669 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
115670 testcase( idxNew2==0 );
115671 exprAnalyze(pSrc, pWC, idxNew2);
115672 pTerm = &pWC->a[idxTerm];
115673 if( isComplete ){
115674 markTermAsChild(pWC, idxNew1, idxTerm);
115675 markTermAsChild(pWC, idxNew2, idxTerm);
 
115676 }
115677 }
115678 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
115679
115680 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -114889,13 +115703,12 @@
115703 pNewTerm = &pWC->a[idxNew];
115704 pNewTerm->prereqRight = prereqExpr;
115705 pNewTerm->leftCursor = pLeft->iTable;
115706 pNewTerm->u.leftColumn = pLeft->iColumn;
115707 pNewTerm->eOperator = WO_MATCH;
115708 markTermAsChild(pWC, idxNew, idxTerm);
115709 pTerm = &pWC->a[idxTerm];
 
115710 pTerm->wtFlags |= TERM_COPIED;
115711 pNewTerm->prereqAll = pTerm->prereqAll;
115712 }
115713 }
115714 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -114912,11 +115725,11 @@
115725 ** the start of the loop will prevent any results from being returned.
115726 */
115727 if( pExpr->op==TK_NOTNULL
115728 && pExpr->pLeft->op==TK_COLUMN
115729 && pExpr->pLeft->iColumn>=0
115730 && OptimizationEnabled(db, SQLITE_Stat34)
115731 ){
115732 Expr *pNewExpr;
115733 Expr *pLeft = pExpr->pLeft;
115734 int idxNew;
115735 WhereTerm *pNewTerm;
@@ -114931,13 +115744,12 @@
115744 pNewTerm = &pWC->a[idxNew];
115745 pNewTerm->prereqRight = 0;
115746 pNewTerm->leftCursor = pLeft->iTable;
115747 pNewTerm->u.leftColumn = pLeft->iColumn;
115748 pNewTerm->eOperator = WO_GT;
115749 markTermAsChild(pWC, idxNew, idxTerm);
115750 pTerm = &pWC->a[idxTerm];
 
115751 pTerm->wtFlags |= TERM_COPIED;
115752 pNewTerm->prereqAll = pTerm->prereqAll;
115753 }
115754 }
115755 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
@@ -115153,10 +115965,12 @@
115965 WhereLoop *pLoop; /* The Loop object */
115966 char *zNotUsed; /* Extra space on the end of pIdx */
115967 Bitmask idxCols; /* Bitmap of columns used for indexing */
115968 Bitmask extraCols; /* Bitmap of additional columns */
115969 u8 sentWarning = 0; /* True if a warnning has been issued */
115970 Expr *pPartial = 0; /* Partial Index Expression */
115971 int iContinue = 0; /* Jump here to skip excluded rows */
115972
115973 /* Generate code to skip over the creation and initialization of the
115974 ** transient index on 2nd and subsequent iterations of the loop. */
115975 v = pParse->pVdbe;
115976 assert( v!=0 );
@@ -115168,10 +115982,16 @@
115982 pTable = pSrc->pTab;
115983 pWCEnd = &pWC->a[pWC->nTerm];
115984 pLoop = pLevel->pWLoop;
115985 idxCols = 0;
115986 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
115987 if( pLoop->prereq==0
115988 && (pTerm->wtFlags & TERM_VIRTUAL)==0
115989 && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){
115990 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
115991 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
115992 }
115993 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
115994 int iCol = pTerm->u.leftColumn;
115995 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
115996 testcase( iCol==BMS );
115997 testcase( iCol==BMS-1 );
@@ -115180,11 +116000,13 @@
116000 "automatic index on %s(%s)", pTable->zName,
116001 pTable->aCol[iCol].zName);
116002 sentWarning = 1;
116003 }
116004 if( (idxCols & cMask)==0 ){
116005 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
116006 goto end_auto_index_create;
116007 }
116008 pLoop->aLTerm[nKeyCol++] = pTerm;
116009 idxCols |= cMask;
116010 }
116011 }
116012 }
@@ -115200,24 +116022,23 @@
116022 ** be a covering index because the index will not be updated if the
116023 ** original table changes and the index and table cannot both be used
116024 ** if they go out of sync.
116025 */
116026 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
116027 mxBitCol = MIN(BMS-1,pTable->nCol);
116028 testcase( pTable->nCol==BMS-1 );
116029 testcase( pTable->nCol==BMS-2 );
116030 for(i=0; i<mxBitCol; i++){
116031 if( extraCols & MASKBIT(i) ) nKeyCol++;
116032 }
116033 if( pSrc->colUsed & MASKBIT(BMS-1) ){
116034 nKeyCol += pTable->nCol - BMS + 1;
116035 }
 
116036
116037 /* Construct the Index object to describe this index */
116038 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
116039 if( pIdx==0 ) goto end_auto_index_create;
116040 pLoop->u.btree.pIndex = pIdx;
116041 pIdx->zName = "auto-index";
116042 pIdx->pTable = pTable;
116043 n = 0;
116044 idxCols = 0;
@@ -115265,22 +116086,33 @@
116086 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
116087 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
116088 VdbeComment((v, "for %s", pTable->zName));
116089
116090 /* Fill the automatic index with content */
116091 sqlite3ExprCachePush(pParse);
116092 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
116093 if( pPartial ){
116094 iContinue = sqlite3VdbeMakeLabel(v);
116095 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
116096 pLoop->wsFlags |= WHERE_PARTIALIDX;
116097 }
116098 regRecord = sqlite3GetTempReg(pParse);
116099 sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
116100 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
116101 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116102 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
116103 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
116104 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
116105 sqlite3VdbeJumpHere(v, addrTop);
116106 sqlite3ReleaseTempReg(pParse, regRecord);
116107 sqlite3ExprCachePop(pParse);
116108
116109 /* Jump here when skipping the initialization */
116110 sqlite3VdbeJumpHere(v, addrInit);
116111
116112 end_auto_index_create:
116113 sqlite3ExprDelete(pParse->db, pPartial);
116114 }
116115 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
116116
116117 #ifndef SQLITE_OMIT_VIRTUALTABLE
116118 /*
@@ -115436,22 +116268,22 @@
116268
116269 return pParse->nErr;
116270 }
116271 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
116272
 
116273 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
116274 /*
116275 ** Estimate the location of a particular key among all keys in an
116276 ** index. Store the results in aStat as follows:
116277 **
116278 ** aStat[0] Est. number of rows less than pVal
116279 ** aStat[1] Est. number of rows equal to pVal
116280 **
116281 ** Return the index of the sample that is the smallest sample that
116282 ** is greater than or equal to pRec.
116283 */
116284 static int whereKeyStats(
116285 Parse *pParse, /* Database connection */
116286 Index *pIdx, /* Index to consider domain of */
116287 UnpackedRecord *pRec, /* Vector of values to consider */
116288 int roundUp, /* Round up if true. Round down if false */
116289 tRowcnt *aStat /* OUT: stats written here */
@@ -115529,10 +116361,11 @@
116361 }else{
116362 iGap = iGap/3;
116363 }
116364 aStat[0] = iLower + iGap;
116365 }
116366 return i;
116367 }
116368 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
116369
116370 /*
116371 ** If it is not NULL, pTerm is a term that provides an upper or lower
@@ -115679,11 +116512,11 @@
116512 ** pLower pUpper
116513 **
116514 ** If either of the upper or lower bound is not present, then NULL is passed in
116515 ** place of the corresponding WhereTerm.
116516 **
116517 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
116518 ** column subject to the range constraint. Or, equivalently, the number of
116519 ** equality constraints optimized by the proposed index scan. For example,
116520 ** assuming index p is on t1(a, b), and the SQL query is:
116521 **
116522 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
@@ -115695,11 +116528,11 @@
116528 **
116529 ** then nEq is set to 0.
116530 **
116531 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
116532 ** number of rows that the index scan is expected to visit without
116533 ** considering the range constraints. If nEq is 0, then *pnOut is the number of
116534 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
116535 ** to account for the range constraints pLower and pUpper.
116536 **
116537 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
116538 ** used, a single range inequality reduces the search space by a factor of 4.
@@ -115719,14 +116552,11 @@
116552
116553 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
116554 Index *p = pLoop->u.btree.pIndex;
116555 int nEq = pLoop->u.btree.nEq;
116556
116557 if( p->nSample>0 && nEq<p->nSampleCol ){
 
 
 
116558 if( nEq==pBuilder->nRecValid ){
116559 UnpackedRecord *pRec = pBuilder->pRec;
116560 tRowcnt a[2];
116561 u8 aff;
116562
@@ -115738,19 +116568,23 @@
116568 **
116569 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
116570 ** is not a simple variable or literal value), the lower bound of the
116571 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
116572 ** if $L is available, whereKeyStats() is called for both ($P) and
116573 ** ($P:$L) and the larger of the two returned values is used.
116574 **
116575 ** Similarly, iUpper is to be set to the estimate of the number of rows
116576 ** less than the upper bound of the range query. Where the upper bound
116577 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
116578 ** of iUpper are requested of whereKeyStats() and the smaller used.
116579 **
116580 ** The number of rows between the two bounds is then just iUpper-iLower.
116581 */
116582 tRowcnt iLower; /* Rows less than the lower bound */
116583 tRowcnt iUpper; /* Rows less than the upper bound */
116584 int iLwrIdx = -2; /* aSample[] for the lower bound */
116585 int iUprIdx = -1; /* aSample[] for the upper bound */
116586
116587 if( pRec ){
116588 testcase( pRec->nField!=pBuilder->nRecValid );
116589 pRec->nField = pBuilder->nRecValid;
116590 }
@@ -115760,11 +116594,11 @@
116594 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
116595 }
116596 /* Determine iLower and iUpper using ($P) only. */
116597 if( nEq==0 ){
116598 iLower = 0;
116599 iUpper = p->nRowEst0;
116600 }else{
116601 /* Note: this call could be optimized away - since the same values must
116602 ** have been requested when testing key $P in whereEqualScanEst(). */
116603 whereKeyStats(pParse, p, pRec, 0, a);
116604 iLower = a[0];
@@ -115784,11 +116618,11 @@
116618 int bOk; /* True if value is extracted from pExpr */
116619 Expr *pExpr = pLower->pExpr->pRight;
116620 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
116621 if( rc==SQLITE_OK && bOk ){
116622 tRowcnt iNew;
116623 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
116624 iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
116625 if( iNew>iLower ) iLower = iNew;
116626 nOut--;
116627 pLower = 0;
116628 }
@@ -115799,11 +116633,11 @@
116633 int bOk; /* True if value is extracted from pExpr */
116634 Expr *pExpr = pUpper->pExpr->pRight;
116635 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
116636 if( rc==SQLITE_OK && bOk ){
116637 tRowcnt iNew;
116638 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
116639 iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
116640 if( iNew<iUpper ) iUpper = iNew;
116641 nOut--;
116642 pUpper = 0;
116643 }
@@ -115811,10 +116645,15 @@
116645
116646 pBuilder->pRec = pRec;
116647 if( rc==SQLITE_OK ){
116648 if( iUpper>iLower ){
116649 nNew = sqlite3LogEst(iUpper - iLower);
116650 /* TUNING: If both iUpper and iLower are derived from the same
116651 ** sample, then assume they are 4x more selective. This brings
116652 ** the estimated selectivity more in line with what it would be
116653 ** if estimated without the use of STAT3/4 tables. */
116654 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
116655 }else{
116656 nNew = 10; assert( 10==sqlite3LogEst(2) );
116657 }
116658 if( nNew<nOut ){
116659 nOut = nNew;
@@ -115835,16 +116674,19 @@
116674 #endif
116675 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
116676 nNew = whereRangeAdjust(pLower, nOut);
116677 nNew = whereRangeAdjust(pUpper, nNew);
116678
116679 /* TUNING: If there is both an upper and lower limit and neither limit
116680 ** has an application-defined likelihood(), assume the range is
116681 ** reduced by an additional 75%. This means that, by default, an open-ended
116682 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
116683 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
116684 ** match 1/64 of the index. */
116685 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
116686 nNew -= 20;
116687 }
116688
116689 nOut -= (pLower!=0) + (pUpper!=0);
116690 if( nNew<10 ) nNew = 10;
116691 if( nNew<nOut ) nOut = nNew;
116692 #if defined(WHERETRACE_ENABLED)
@@ -116200,11 +117042,11 @@
117042
117043 /* This module is only called on query plans that use an index. */
117044 pLoop = pLevel->pWLoop;
117045 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
117046 nEq = pLoop->u.btree.nEq;
117047 nSkip = pLoop->nSkip;
117048 pIdx = pLoop->u.btree.pIndex;
117049 assert( pIdx!=0 );
117050
117051 /* Figure out how many memory cells we will need then allocate them.
117052 */
@@ -116314,11 +117156,11 @@
117156 ** "a=? AND b>?"
117157 */
117158 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
117159 Index *pIndex = pLoop->u.btree.pIndex;
117160 u16 nEq = pLoop->u.btree.nEq;
117161 u16 nSkip = pLoop->nSkip;
117162 int i, j;
117163 Column *aCol = pTab->aCol;
117164 i16 *aiColumn = pIndex->aiColumn;
117165
117166 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
@@ -116345,23 +117187,27 @@
117187 sqlite3StrAccumAppend(pStr, ")", 1);
117188 }
117189
117190 /*
117191 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
117192 ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
117193 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
117194 ** is added to the output to describe the table scan strategy in pLevel.
117195 **
117196 ** If an OP_Explain opcode is added to the VM, its address is returned.
117197 ** Otherwise, if no OP_Explain is coded, zero is returned.
117198 */
117199 static int explainOneScan(
117200 Parse *pParse, /* Parse context */
117201 SrcList *pTabList, /* Table list this loop refers to */
117202 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
117203 int iLevel, /* Value for "level" column of output */
117204 int iFrom, /* Value for "from" column of output */
117205 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
117206 ){
117207 int ret = 0;
117208 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
117209 if( pParse->explain==2 )
117210 #endif
117211 {
117212 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
117213 Vdbe *v = pParse->pVdbe; /* VM being constructed */
@@ -116374,11 +117220,11 @@
117220 StrAccum str; /* EQP output string */
117221 char zBuf[100]; /* Initial space for EQP output string */
117222
117223 pLoop = pLevel->pWLoop;
117224 flags = pLoop->wsFlags;
117225 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
117226
117227 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
117228 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
117229 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
117230
@@ -116403,10 +117249,12 @@
117249 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
117250 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
117251 if( isSearch ){
117252 zFmt = "PRIMARY KEY";
117253 }
117254 }else if( flags & WHERE_PARTIALIDX ){
117255 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
117256 }else if( flags & WHERE_AUTO_INDEX ){
117257 zFmt = "AUTOMATIC COVERING INDEX";
117258 }else if( flags & WHERE_IDX_ONLY ){
117259 zFmt = "COVERING INDEX %s";
117260 }else{
@@ -116444,16 +117292,49 @@
117292 }else{
117293 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
117294 }
117295 #endif
117296 zMsg = sqlite3StrAccumFinish(&str);
117297 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
117298 }
117299 return ret;
117300 }
117301 #else
117302 # define explainOneScan(u,v,w,x,y,z) 0
117303 #endif /* SQLITE_OMIT_EXPLAIN */
117304
117305 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117306 /*
117307 ** Configure the VM passed as the first argument with an
117308 ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
117309 ** implement level pLvl. Argument pSrclist is a pointer to the FROM
117310 ** clause that the scan reads data from.
117311 **
117312 ** If argument addrExplain is not 0, it must be the address of an
117313 ** OP_Explain instruction that describes the same loop.
117314 */
117315 static void addScanStatus(
117316 Vdbe *v, /* Vdbe to add scanstatus entry to */
117317 SrcList *pSrclist, /* FROM clause pLvl reads data from */
117318 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
117319 int addrExplain /* Address of OP_Explain (or 0) */
117320 ){
117321 const char *zObj = 0;
117322 WhereLoop *pLoop = pLvl->pWLoop;
117323 if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
117324 zObj = pLoop->u.btree.pIndex->zName;
117325 }else{
117326 zObj = pSrclist->a[pLvl->iFrom].zName;
117327 }
117328 sqlite3VdbeScanStatus(
117329 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
117330 );
117331 }
117332 #else
117333 # define addScanStatus(a, b, c, d) ((void)d)
117334 #endif
117335
117336
117337
117338 /*
117339 ** Generate code for the start of the iLevel-th loop in the WHERE clause
117340 ** implementation described by pWInfo.
@@ -116751,11 +117632,11 @@
117632 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
117633 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
117634
117635 pIdx = pLoop->u.btree.pIndex;
117636 iIdxCur = pLevel->iIdxCur;
117637 assert( nEq>=pLoop->nSkip );
117638
117639 /* If this loop satisfies a sort order (pOrderBy) request that
117640 ** was passed to this function to implement a "SELECT min(x) ..."
117641 ** query, then the caller will only allow the loop to run for
117642 ** a single iteration. This means that the first row returned
@@ -116768,11 +117649,11 @@
117649 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
117650 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
117651 && pWInfo->nOBSat>0
117652 && (pIdx->nKeyCol>nEq)
117653 ){
117654 assert( pLoop->nSkip==0 );
117655 bSeekPastNull = 1;
117656 nExtraReg = 1;
117657 }
117658
117659 /* Find any inequality constraint terms for the start and end
@@ -117117,13 +117998,15 @@
117998 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
117999 wctrlFlags, iCovCur);
118000 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
118001 if( pSubWInfo ){
118002 WhereLoop *pSubLoop;
118003 int addrExplain = explainOneScan(
118004 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
118005 );
118006 addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
118007
118008 /* This is the sub-WHERE clause body. First skip over
118009 ** duplicate rows from prior sub-WHERE clauses, and record the
118010 ** rowid (or PRIMARY KEY) for the current row so that the same
118011 ** row will be skipped in subsequent sub-WHERE clauses.
118012 */
@@ -117249,10 +118132,14 @@
118132 VdbeCoverageIf(v, bRev==0);
118133 VdbeCoverageIf(v, bRev!=0);
118134 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118135 }
118136 }
118137
118138 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
118139 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
118140 #endif
118141
118142 /* Insert code to test every subexpression that can be completely
118143 ** computed using the current set of tables.
118144 */
118145 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
@@ -117389,11 +118276,11 @@
118276 }
118277 sqlite3DebugPrintf(" %-19s", z);
118278 sqlite3_free(z);
118279 }
118280 if( p->wsFlags & WHERE_SKIPSCAN ){
118281 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
118282 }else{
118283 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
118284 }
118285 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
118286 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
@@ -117500,34 +118387,41 @@
118387 sqlite3DbFree(db, pWInfo);
118388 }
118389 }
118390
118391 /*
118392 ** Return TRUE if all of the following are true:
118393 **
118394 ** (1) X has the same or lower cost that Y
118395 ** (2) X is a proper subset of Y
118396 ** (3) X skips at least as many columns as Y
118397 **
118398 ** By "proper subset" we mean that X uses fewer WHERE clause terms
118399 ** than Y and that every WHERE clause term used by X is also used
118400 ** by Y.
118401 **
118402 ** If X is a proper subset of Y then Y is a better choice and ought
118403 ** to have a lower cost. This routine returns TRUE when that cost
118404 ** relationship is inverted and needs to be adjusted. The third rule
118405 ** was added because if X uses skip-scan less than Y it still might
118406 ** deserve a lower cost even if it is a proper subset of Y.
118407 */
118408 static int whereLoopCheaperProperSubset(
118409 const WhereLoop *pX, /* First WhereLoop to compare */
118410 const WhereLoop *pY /* Compare against this WhereLoop */
118411 ){
118412 int i, j;
118413 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
118414 return 0; /* X is not a subset of Y */
118415 }
118416 if( pY->nSkip > pX->nSkip ) return 0;
118417 if( pX->rRun >= pY->rRun ){
118418 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
118419 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
118420 }
118421 for(i=pX->nLTerm-1; i>=0; i--){
118422 if( pX->aLTerm[i]==0 ) continue;
118423 for(j=pY->nLTerm-1; j>=0; j--){
118424 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
118425 }
118426 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
118427 }
@@ -117545,37 +118439,28 @@
118439 ** is a proper subset.
118440 **
118441 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
118442 ** WHERE clause terms than Y and that every WHERE clause term used by X is
118443 ** also used by Y.
 
 
 
 
 
 
 
 
 
 
 
118444 */
118445 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
118446 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
 
118447 for(; p; p=p->pNextLoop){
118448 if( p->iTab!=pTemplate->iTab ) continue;
118449 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
 
118450 if( whereLoopCheaperProperSubset(p, pTemplate) ){
118451 /* Adjust pTemplate cost downward so that it is cheaper than its
118452 ** subset p. */
118453 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
118454 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
118455 pTemplate->rRun = p->rRun;
118456 pTemplate->nOut = p->nOut - 1;
118457 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
118458 /* Adjust pTemplate cost upward so that it is costlier than p since
118459 ** pTemplate is a proper subset of p */
118460 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
118461 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
118462 pTemplate->rRun = p->rRun;
118463 pTemplate->nOut = p->nOut + 1;
118464 }
118465 }
118466 }
@@ -117857,11 +118742,11 @@
118742 int opMask; /* Valid operators for constraints */
118743 WhereScan scan; /* Iterator for WHERE terms */
118744 Bitmask saved_prereq; /* Original value of pNew->prereq */
118745 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
118746 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
118747 u16 saved_nSkip; /* Original value of pNew->nSkip */
118748 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
118749 LogEst saved_nOut; /* Original value of pNew->nOut */
118750 int iCol; /* Index of the column in the table */
118751 int rc = SQLITE_OK; /* Return code */
118752 LogEst rSize; /* Number of rows in the table */
@@ -117886,56 +118771,18 @@
118771 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
118772
118773 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
118774 opMask, pProbe);
118775 saved_nEq = pNew->u.btree.nEq;
118776 saved_nSkip = pNew->nSkip;
118777 saved_nLTerm = pNew->nLTerm;
118778 saved_wsFlags = pNew->wsFlags;
118779 saved_prereq = pNew->prereq;
118780 saved_nOut = pNew->nOut;
118781 pNew->rSetup = 0;
118782 rSize = pProbe->aiRowLogEst[0];
118783 rLogSize = estLog(rSize);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118784 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
118785 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
118786 LogEst rCostIdx;
118787 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
118788 int nIn = 0;
@@ -118026,11 +118873,10 @@
118873 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
118874 tRowcnt nOut = 0;
118875 if( nInMul==0
118876 && pProbe->nSample
118877 && pNew->u.btree.nEq<=pProbe->nSampleCol
 
118878 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
118879 ){
118880 Expr *pExpr = pTerm->pExpr;
118881 if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
118882 testcase( eOp & WO_EQ );
@@ -118094,14 +118940,48 @@
118940 pBuilder->nRecValid = nRecValid;
118941 #endif
118942 }
118943 pNew->prereq = saved_prereq;
118944 pNew->u.btree.nEq = saved_nEq;
118945 pNew->nSkip = saved_nSkip;
118946 pNew->wsFlags = saved_wsFlags;
118947 pNew->nOut = saved_nOut;
118948 pNew->nLTerm = saved_nLTerm;
118949
118950 /* Consider using a skip-scan if there are no WHERE clause constraints
118951 ** available for the left-most terms of the index, and if the average
118952 ** number of repeats in the left-most terms is at least 18.
118953 **
118954 ** The magic number 18 is selected on the basis that scanning 17 rows
118955 ** is almost always quicker than an index seek (even though if the index
118956 ** contains fewer than 2^17 rows we assume otherwise in other parts of
118957 ** the code). And, even if it is not, it should not be too much slower.
118958 ** On the other hand, the extra seeks could end up being significantly
118959 ** more expensive. */
118960 assert( 42==sqlite3LogEst(18) );
118961 if( saved_nEq==saved_nSkip
118962 && saved_nEq+1<pProbe->nKeyCol
118963 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
118964 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
118965 ){
118966 LogEst nIter;
118967 pNew->u.btree.nEq++;
118968 pNew->nSkip++;
118969 pNew->aLTerm[pNew->nLTerm++] = 0;
118970 pNew->wsFlags |= WHERE_SKIPSCAN;
118971 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
118972 pNew->nOut -= nIter;
118973 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
118974 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
118975 nIter += 5;
118976 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
118977 pNew->nOut = saved_nOut;
118978 pNew->u.btree.nEq = saved_nEq;
118979 pNew->nSkip = saved_nSkip;
118980 pNew->wsFlags = saved_wsFlags;
118981 }
118982
118983 return rc;
118984 }
118985
118986 /*
118987 ** Return True if it is possible that pIndex might be useful in
@@ -118276,11 +119156,11 @@
119156 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
119157 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
119158 if( pTerm->prereqRight & pNew->maskSelf ) continue;
119159 if( termCanDriveIndex(pTerm, pSrc, 0) ){
119160 pNew->u.btree.nEq = 1;
119161 pNew->nSkip = 0;
119162 pNew->u.btree.pIndex = 0;
119163 pNew->nLTerm = 1;
119164 pNew->aLTerm[0] = pTerm;
119165 /* TUNING: One-time cost for computing the automatic index is
119166 ** estimated to be X*N*log2(N) where N is the number of rows in
@@ -118317,11 +119197,11 @@
119197 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
119198 continue; /* Partial index inappropriate for this query */
119199 }
119200 rSize = pProbe->aiRowLogEst[0];
119201 pNew->u.btree.nEq = 0;
119202 pNew->nSkip = 0;
119203 pNew->nLTerm = 0;
119204 pNew->iSortIdx = 0;
119205 pNew->rSetup = 0;
119206 pNew->prereq = mExtra;
119207 pNew->nOut = rSize;
@@ -118867,11 +119747,11 @@
119747 for(j=0; j<nColumn; j++){
119748 u8 bOnce; /* True to run the ORDER BY search loop */
119749
119750 /* Skip over == and IS NULL terms */
119751 if( j<pLoop->u.btree.nEq
119752 && pLoop->nSkip==0
119753 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
119754 ){
119755 if( i & WO_ISNULL ){
119756 testcase( isOrderDistinct );
119757 isOrderDistinct = 0;
@@ -119321,11 +120201,11 @@
120201 }
120202 }
120203 }
120204
120205 #ifdef WHERETRACE_ENABLED /* >=2 */
120206 if( sqlite3WhereTrace & 0x02 ){
120207 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
120208 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
120209 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
120210 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
120211 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
@@ -119440,11 +120320,11 @@
120320 if( pItem->zIndex ) return 0;
120321 iCur = pItem->iCursor;
120322 pWC = &pWInfo->sWC;
120323 pLoop = pBuilder->pNew;
120324 pLoop->wsFlags = 0;
120325 pLoop->nSkip = 0;
120326 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
120327 if( pTerm ){
120328 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
120329 pLoop->aLTerm[0] = pTerm;
120330 pLoop->nLTerm = 1;
@@ -119452,11 +120332,10 @@
120332 /* TUNING: Cost of a rowid lookup is 10 */
120333 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
120334 }else{
120335 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120336 assert( pLoop->aLTermSpace==pLoop->aLTerm );
 
120337 if( !IsUniqueIndex(pIdx)
120338 || pIdx->pPartIdxWhere!=0
120339 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
120340 ) continue;
120341 for(j=0; j<pIdx->nKeyCol; j++){
@@ -119961,22 +120840,30 @@
120840 ** loop below generates code for a single nested loop of the VM
120841 ** program.
120842 */
120843 notReady = ~(Bitmask)0;
120844 for(ii=0; ii<nTabList; ii++){
120845 int addrExplain;
120846 int wsFlags;
120847 pLevel = &pWInfo->a[ii];
120848 wsFlags = pLevel->pWLoop->wsFlags;
120849 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
120850 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
120851 constructAutomaticIndex(pParse, &pWInfo->sWC,
120852 &pTabList->a[pLevel->iFrom], notReady, pLevel);
120853 if( db->mallocFailed ) goto whereBeginError;
120854 }
120855 #endif
120856 addrExplain = explainOneScan(
120857 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
120858 );
120859 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
120860 notReady = codeOneLoopStart(pWInfo, ii, notReady);
120861 pWInfo->iContinue = pLevel->addrCont;
120862 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
120863 addScanStatus(v, pTabList, pLevel, addrExplain);
120864 }
120865 }
120866
120867 /* Done. */
120868 VdbeModuleComment((v, "Begin WHERE-core"));
120869 return pWInfo;
@@ -124640,10 +125527,17 @@
125527 ** is look for a semicolon that is not part of an string or comment.
125528 */
125529 SQLITE_API int sqlite3_complete(const char *zSql){
125530 u8 state = 0; /* Current state, using numbers defined in header comment */
125531 u8 token; /* Value of the next token */
125532
125533 #ifdef SQLITE_ENABLE_API_ARMOR
125534 if( zSql==0 ){
125535 (void)SQLITE_MISUSE_BKPT;
125536 return 0;
125537 }
125538 #endif
125539
125540 #ifndef SQLITE_OMIT_TRIGGER
125541 /* A complex statement machine used to detect the end of a CREATE TRIGGER
125542 ** statement. This is the normal case.
125543 */
@@ -125238,74 +126132,106 @@
126132
126133 va_start(ap, op);
126134 switch( op ){
126135
126136 /* Mutex configuration options are only available in a threadsafe
126137 ** compile.
126138 */
126139 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
126140 case SQLITE_CONFIG_SINGLETHREAD: {
126141 /* Disable all mutexing */
126142 sqlite3GlobalConfig.bCoreMutex = 0;
126143 sqlite3GlobalConfig.bFullMutex = 0;
126144 break;
126145 }
126146 #endif
126147 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
126148 case SQLITE_CONFIG_MULTITHREAD: {
126149 /* Disable mutexing of database connections */
126150 /* Enable mutexing of core data structures */
126151 sqlite3GlobalConfig.bCoreMutex = 1;
126152 sqlite3GlobalConfig.bFullMutex = 0;
126153 break;
126154 }
126155 #endif
126156 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
126157 case SQLITE_CONFIG_SERIALIZED: {
126158 /* Enable all mutexing */
126159 sqlite3GlobalConfig.bCoreMutex = 1;
126160 sqlite3GlobalConfig.bFullMutex = 1;
126161 break;
126162 }
126163 #endif
126164 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
126165 case SQLITE_CONFIG_MUTEX: {
126166 /* Specify an alternative mutex implementation */
126167 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
126168 break;
126169 }
126170 #endif
126171 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
126172 case SQLITE_CONFIG_GETMUTEX: {
126173 /* Retrieve the current mutex implementation */
126174 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
126175 break;
126176 }
126177 #endif
126178
 
126179 case SQLITE_CONFIG_MALLOC: {
126180 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
126181 ** single argument which is a pointer to an instance of the
126182 ** sqlite3_mem_methods structure. The argument specifies alternative
126183 ** low-level memory allocation routines to be used in place of the memory
126184 ** allocation routines built into SQLite. */
126185 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
126186 break;
126187 }
126188 case SQLITE_CONFIG_GETMALLOC: {
126189 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
126190 ** single argument which is a pointer to an instance of the
126191 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
126192 ** filled with the currently defined memory allocation routines. */
126193 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
126194 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
126195 break;
126196 }
126197 case SQLITE_CONFIG_MEMSTATUS: {
126198 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
126199 ** single argument of type int, interpreted as a boolean, which enables
126200 ** or disables the collection of memory allocation statistics. */
126201 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
126202 break;
126203 }
126204 case SQLITE_CONFIG_SCRATCH: {
126205 /* EVIDENCE-OF: R-08404-60887 There are three arguments to
126206 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
126207 ** which the scratch allocations will be drawn, the size of each scratch
126208 ** allocation (sz), and the maximum number of scratch allocations (N). */
126209 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
126210 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
126211 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
126212 break;
126213 }
126214 case SQLITE_CONFIG_PAGECACHE: {
126215 /* EVIDENCE-OF: R-31408-40510 There are three arguments to
126216 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
126217 ** of each page buffer (sz), and the number of pages (N). */
126218 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
126219 sqlite3GlobalConfig.szPage = va_arg(ap, int);
126220 sqlite3GlobalConfig.nPage = va_arg(ap, int);
126221 break;
126222 }
126223 case SQLITE_CONFIG_PCACHE_HDRSZ: {
126224 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
126225 ** a single parameter which is a pointer to an integer and writes into
126226 ** that integer the number of extra bytes per page required for each page
126227 ** in SQLITE_CONFIG_PAGECACHE. */
126228 *va_arg(ap, int*) =
126229 sqlite3HeaderSizeBtree() +
126230 sqlite3HeaderSizePcache() +
126231 sqlite3HeaderSizePcache1();
126232 break;
126233 }
126234
126235 case SQLITE_CONFIG_PCACHE: {
126236 /* no-op */
126237 break;
@@ -125315,25 +126241,37 @@
126241 rc = SQLITE_ERROR;
126242 break;
126243 }
126244
126245 case SQLITE_CONFIG_PCACHE2: {
126246 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
126247 ** single argument which is a pointer to an sqlite3_pcache_methods2
126248 ** object. This object specifies the interface to a custom page cache
126249 ** implementation. */
126250 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
126251 break;
126252 }
126253 case SQLITE_CONFIG_GETPCACHE2: {
126254 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
126255 ** single argument which is a pointer to an sqlite3_pcache_methods2
126256 ** object. SQLite copies of the current page cache implementation into
126257 ** that object. */
126258 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
126259 sqlite3PCacheSetDefault();
126260 }
126261 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
126262 break;
126263 }
126264
126265 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
126266 ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
126267 ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
126268 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
126269 case SQLITE_CONFIG_HEAP: {
126270 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
126271 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
126272 ** number of bytes in the memory buffer, and the minimum allocation size. */
126273 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
126274 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
126275 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
126276
126277 if( sqlite3GlobalConfig.mnReq<1 ){
@@ -125342,21 +126280,23 @@
126280 /* cap min request size at 2^12 */
126281 sqlite3GlobalConfig.mnReq = (1<<12);
126282 }
126283
126284 if( sqlite3GlobalConfig.pHeap==0 ){
126285 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
126286 ** is NULL, then SQLite reverts to using its default memory allocator
126287 ** (the system malloc() implementation), undoing any prior invocation of
126288 ** SQLITE_CONFIG_MALLOC.
126289 **
126290 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
126291 ** revert to its default implementation when sqlite3_initialize() is run
126292 */
126293 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
126294 }else{
126295 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
126296 ** alternative memory allocator is engaged to handle all of SQLites
126297 ** memory allocation needs. */
 
126298 #ifdef SQLITE_ENABLE_MEMSYS3
126299 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
126300 #endif
126301 #ifdef SQLITE_ENABLE_MEMSYS5
126302 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
@@ -125391,15 +126331,23 @@
126331 ** can be changed at start-time using the
126332 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
126333 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
126334 */
126335 case SQLITE_CONFIG_URI: {
126336 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
126337 ** argument of type int. If non-zero, then URI handling is globally
126338 ** enabled. If the parameter is zero, then URI handling is globally
126339 ** disabled. */
126340 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
126341 break;
126342 }
126343
126344 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
126345 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
126346 ** option takes a single integer argument which is interpreted as a
126347 ** boolean in order to enable or disable the use of covering indices for
126348 ** full table scans in the query optimizer. */
126349 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
126350 break;
126351 }
126352
126353 #ifdef SQLITE_ENABLE_SQLLOG
@@ -125410,24 +126358,37 @@
126358 break;
126359 }
126360 #endif
126361
126362 case SQLITE_CONFIG_MMAP_SIZE: {
126363 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
126364 ** integer (sqlite3_int64) values that are the default mmap size limit
126365 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
126366 ** mmap size limit. */
126367 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
126368 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
126369 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
126370 ** negative, then that argument is changed to its compile-time default.
126371 **
126372 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
126373 ** silently truncated if necessary so that it does not exceed the
126374 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
126375 ** compile-time option.
126376 */
126377 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE;
126378 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
126379 if( szMmap>mxMmap) szMmap = mxMmap;
126380 sqlite3GlobalConfig.mxMmap = mxMmap;
126381 sqlite3GlobalConfig.szMmap = szMmap;
126382 break;
126383 }
126384
126385 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
126386 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
126387 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
126388 ** unsigned integer value that specifies the maximum size of the created
126389 ** heap. */
126390 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
126391 break;
126392 }
126393 #endif
126394
@@ -125507,19 +126468,29 @@
126468
126469 /*
126470 ** Return the mutex associated with a database connection.
126471 */
126472 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
126473 #ifdef SQLITE_ENABLE_API_ARMOR
126474 if( !sqlite3SafetyCheckOk(db) ){
126475 (void)SQLITE_MISUSE_BKPT;
126476 return 0;
126477 }
126478 #endif
126479 return db->mutex;
126480 }
126481
126482 /*
126483 ** Free up as much memory as we can from the given database
126484 ** connection.
126485 */
126486 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
126487 int i;
126488
126489 #ifdef SQLITE_ENABLE_API_ARMOR
126490 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
126491 #endif
126492 sqlite3_mutex_enter(db->mutex);
126493 sqlite3BtreeEnterAll(db);
126494 for(i=0; i<db->nDb; i++){
126495 Btree *pBt = db->aDb[i].pBt;
126496 if( pBt ){
@@ -125646,24 +126617,42 @@
126617
126618 /*
126619 ** Return the ROWID of the most recent insert
126620 */
126621 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
126622 #ifdef SQLITE_ENABLE_API_ARMOR
126623 if( !sqlite3SafetyCheckOk(db) ){
126624 (void)SQLITE_MISUSE_BKPT;
126625 return 0;
126626 }
126627 #endif
126628 return db->lastRowid;
126629 }
126630
126631 /*
126632 ** Return the number of changes in the most recent call to sqlite3_exec().
126633 */
126634 SQLITE_API int sqlite3_changes(sqlite3 *db){
126635 #ifdef SQLITE_ENABLE_API_ARMOR
126636 if( !sqlite3SafetyCheckOk(db) ){
126637 (void)SQLITE_MISUSE_BKPT;
126638 return 0;
126639 }
126640 #endif
126641 return db->nChange;
126642 }
126643
126644 /*
126645 ** Return the number of changes since the database handle was opened.
126646 */
126647 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
126648 #ifdef SQLITE_ENABLE_API_ARMOR
126649 if( !sqlite3SafetyCheckOk(db) ){
126650 (void)SQLITE_MISUSE_BKPT;
126651 return 0;
126652 }
126653 #endif
126654 return db->nTotalChange;
126655 }
126656
126657 /*
126658 ** Close all open savepoints. This function only manipulates fields of the
@@ -125925,17 +126914,19 @@
126914 sqlite3_free(db);
126915 }
126916
126917 /*
126918 ** Rollback all database files. If tripCode is not SQLITE_OK, then
126919 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
126920 ** breaker") and made to return tripCode if there are any further
126921 ** attempts to use that cursor. Read cursors remain open and valid
126922 ** but are "saved" in case the table pages are moved around.
126923 */
126924 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
126925 int i;
126926 int inTrans = 0;
126927 int schemaChange;
126928 assert( sqlite3_mutex_held(db->mutex) );
126929 sqlite3BeginBenignMalloc();
126930
126931 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
126932 ** This is important in case the transaction being rolled back has
@@ -125942,18 +126933,19 @@
126933 ** modified the database schema. If the b-tree mutexes are not taken
126934 ** here, then another shared-cache connection might sneak in between
126935 ** the database rollback and schema reset, which can cause false
126936 ** corruption reports in some cases. */
126937 sqlite3BtreeEnterAll(db);
126938 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
126939
126940 for(i=0; i<db->nDb; i++){
126941 Btree *p = db->aDb[i].pBt;
126942 if( p ){
126943 if( sqlite3BtreeIsInTrans(p) ){
126944 inTrans = 1;
126945 }
126946 sqlite3BtreeRollback(p, tripCode, !schemaChange);
126947 }
126948 }
126949 sqlite3VtabRollback(db);
126950 sqlite3EndBenignMalloc();
126951
@@ -126205,10 +127197,13 @@
127197 SQLITE_API int sqlite3_busy_handler(
127198 sqlite3 *db,
127199 int (*xBusy)(void*,int),
127200 void *pArg
127201 ){
127202 #ifdef SQLITE_ENABLE_API_ARMOR
127203 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE;
127204 #endif
127205 sqlite3_mutex_enter(db->mutex);
127206 db->busyHandler.xFunc = xBusy;
127207 db->busyHandler.pArg = pArg;
127208 db->busyHandler.nBusy = 0;
127209 db->busyTimeout = 0;
@@ -126226,10 +127221,16 @@
127221 sqlite3 *db,
127222 int nOps,
127223 int (*xProgress)(void*),
127224 void *pArg
127225 ){
127226 #ifdef SQLITE_ENABLE_API_ARMOR
127227 if( !sqlite3SafetyCheckOk(db) ){
127228 (void)SQLITE_MISUSE_BKPT;
127229 return;
127230 }
127231 #endif
127232 sqlite3_mutex_enter(db->mutex);
127233 if( nOps>0 ){
127234 db->xProgress = xProgress;
127235 db->nProgressOps = (unsigned)nOps;
127236 db->pProgressArg = pArg;
@@ -126246,10 +127247,13 @@
127247 /*
127248 ** This routine installs a default busy handler that waits for the
127249 ** specified number of milliseconds before returning 0.
127250 */
127251 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
127252 #ifdef SQLITE_ENABLE_API_ARMOR
127253 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127254 #endif
127255 if( ms>0 ){
127256 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
127257 db->busyTimeout = ms;
127258 }else{
127259 sqlite3_busy_handler(db, 0, 0);
@@ -126259,10 +127263,16 @@
127263
127264 /*
127265 ** Cause any pending operation to stop at its earliest opportunity.
127266 */
127267 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
127268 #ifdef SQLITE_ENABLE_API_ARMOR
127269 if( !sqlite3SafetyCheckOk(db) ){
127270 (void)SQLITE_MISUSE_BKPT;
127271 return;
127272 }
127273 #endif
127274 db->u1.isInterrupted = 1;
127275 }
127276
127277
127278 /*
@@ -126396,10 +127406,16 @@
127406 void (*xFinal)(sqlite3_context*),
127407 void (*xDestroy)(void *)
127408 ){
127409 int rc = SQLITE_ERROR;
127410 FuncDestructor *pArg = 0;
127411
127412 #ifdef SQLITE_ENABLE_API_ARMOR
127413 if( !sqlite3SafetyCheckOk(db) ){
127414 return SQLITE_MISUSE_BKPT;
127415 }
127416 #endif
127417 sqlite3_mutex_enter(db->mutex);
127418 if( xDestroy ){
127419 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
127420 if( !pArg ){
127421 xDestroy(p);
@@ -126432,10 +127448,14 @@
127448 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
127449 void (*xFinal)(sqlite3_context*)
127450 ){
127451 int rc;
127452 char *zFunc8;
127453
127454 #ifdef SQLITE_ENABLE_API_ARMOR
127455 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
127456 #endif
127457 sqlite3_mutex_enter(db->mutex);
127458 assert( !db->mallocFailed );
127459 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
127460 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
127461 sqlite3DbFree(db, zFunc8);
@@ -126463,10 +127483,16 @@
127483 const char *zName,
127484 int nArg
127485 ){
127486 int nName = sqlite3Strlen30(zName);
127487 int rc = SQLITE_OK;
127488
127489 #ifdef SQLITE_ENABLE_API_ARMOR
127490 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
127491 return SQLITE_MISUSE_BKPT;
127492 }
127493 #endif
127494 sqlite3_mutex_enter(db->mutex);
127495 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
127496 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
127497 0, sqlite3InvalidFunction, 0, 0, 0);
127498 }
@@ -126484,10 +127510,17 @@
127510 ** trace is a pointer to a function that is invoked at the start of each
127511 ** SQL statement.
127512 */
127513 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
127514 void *pOld;
127515
127516 #ifdef SQLITE_ENABLE_API_ARMOR
127517 if( !sqlite3SafetyCheckOk(db) ){
127518 (void)SQLITE_MISUSE_BKPT;
127519 return 0;
127520 }
127521 #endif
127522 sqlite3_mutex_enter(db->mutex);
127523 pOld = db->pTraceArg;
127524 db->xTrace = xTrace;
127525 db->pTraceArg = pArg;
127526 sqlite3_mutex_leave(db->mutex);
@@ -126505,10 +127538,17 @@
127538 sqlite3 *db,
127539 void (*xProfile)(void*,const char*,sqlite_uint64),
127540 void *pArg
127541 ){
127542 void *pOld;
127543
127544 #ifdef SQLITE_ENABLE_API_ARMOR
127545 if( !sqlite3SafetyCheckOk(db) ){
127546 (void)SQLITE_MISUSE_BKPT;
127547 return 0;
127548 }
127549 #endif
127550 sqlite3_mutex_enter(db->mutex);
127551 pOld = db->pProfileArg;
127552 db->xProfile = xProfile;
127553 db->pProfileArg = pArg;
127554 sqlite3_mutex_leave(db->mutex);
@@ -126525,10 +127565,17 @@
127565 sqlite3 *db, /* Attach the hook to this database */
127566 int (*xCallback)(void*), /* Function to invoke on each commit */
127567 void *pArg /* Argument to the function */
127568 ){
127569 void *pOld;
127570
127571 #ifdef SQLITE_ENABLE_API_ARMOR
127572 if( !sqlite3SafetyCheckOk(db) ){
127573 (void)SQLITE_MISUSE_BKPT;
127574 return 0;
127575 }
127576 #endif
127577 sqlite3_mutex_enter(db->mutex);
127578 pOld = db->pCommitArg;
127579 db->xCommitCallback = xCallback;
127580 db->pCommitArg = pArg;
127581 sqlite3_mutex_leave(db->mutex);
@@ -126543,10 +127590,17 @@
127590 sqlite3 *db, /* Attach the hook to this database */
127591 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
127592 void *pArg /* Argument to the function */
127593 ){
127594 void *pRet;
127595
127596 #ifdef SQLITE_ENABLE_API_ARMOR
127597 if( !sqlite3SafetyCheckOk(db) ){
127598 (void)SQLITE_MISUSE_BKPT;
127599 return 0;
127600 }
127601 #endif
127602 sqlite3_mutex_enter(db->mutex);
127603 pRet = db->pUpdateArg;
127604 db->xUpdateCallback = xCallback;
127605 db->pUpdateArg = pArg;
127606 sqlite3_mutex_leave(db->mutex);
@@ -126561,10 +127615,17 @@
127615 sqlite3 *db, /* Attach the hook to this database */
127616 void (*xCallback)(void*), /* Callback function */
127617 void *pArg /* Argument to the function */
127618 ){
127619 void *pRet;
127620
127621 #ifdef SQLITE_ENABLE_API_ARMOR
127622 if( !sqlite3SafetyCheckOk(db) ){
127623 (void)SQLITE_MISUSE_BKPT;
127624 return 0;
127625 }
127626 #endif
127627 sqlite3_mutex_enter(db->mutex);
127628 pRet = db->pRollbackArg;
127629 db->xRollbackCallback = xCallback;
127630 db->pRollbackArg = pArg;
127631 sqlite3_mutex_leave(db->mutex);
@@ -126607,10 +127668,13 @@
127668 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
127669 #ifdef SQLITE_OMIT_WAL
127670 UNUSED_PARAMETER(db);
127671 UNUSED_PARAMETER(nFrame);
127672 #else
127673 #ifdef SQLITE_ENABLE_API_ARMOR
127674 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127675 #endif
127676 if( nFrame>0 ){
127677 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
127678 }else{
127679 sqlite3_wal_hook(db, 0, 0);
127680 }
@@ -126627,10 +127691,16 @@
127691 int(*xCallback)(void *, sqlite3*, const char*, int),
127692 void *pArg /* First argument passed to xCallback() */
127693 ){
127694 #ifndef SQLITE_OMIT_WAL
127695 void *pRet;
127696 #ifdef SQLITE_ENABLE_API_ARMOR
127697 if( !sqlite3SafetyCheckOk(db) ){
127698 (void)SQLITE_MISUSE_BKPT;
127699 return 0;
127700 }
127701 #endif
127702 sqlite3_mutex_enter(db->mutex);
127703 pRet = db->pWalArg;
127704 db->xWalCallback = xCallback;
127705 db->pWalArg = pArg;
127706 sqlite3_mutex_leave(db->mutex);
@@ -126653,10 +127723,14 @@
127723 #ifdef SQLITE_OMIT_WAL
127724 return SQLITE_OK;
127725 #else
127726 int rc; /* Return code */
127727 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
127728
127729 #ifdef SQLITE_ENABLE_API_ARMOR
127730 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127731 #endif
127732
127733 /* Initialize the output variables to -1 in case an error occurs. */
127734 if( pnLog ) *pnLog = -1;
127735 if( pnCkpt ) *pnCkpt = -1;
127736
@@ -127050,10 +128124,16 @@
128124 ** from forming.
128125 */
128126 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
128127 int oldLimit;
128128
128129 #ifdef SQLITE_ENABLE_API_ARMOR
128130 if( !sqlite3SafetyCheckOk(db) ){
128131 (void)SQLITE_MISUSE_BKPT;
128132 return -1;
128133 }
128134 #endif
128135
128136 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
128137 ** there is a hard upper bound set at compile-time by a C preprocessor
128138 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
128139 ** "_MAX_".)
@@ -127126,11 +128206,12 @@
128206 char c;
128207 int nUri = sqlite3Strlen30(zUri);
128208
128209 assert( *pzErrMsg==0 );
128210
128211 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
128212 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
128213 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
128214 ){
128215 char *zOpt;
128216 int eState; /* Parser state when parsing URI */
128217 int iIn; /* Input character index */
@@ -127335,10 +128416,13 @@
128416 int rc; /* Return code */
128417 int isThreadsafe; /* True for threadsafe connections */
128418 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
128419 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
128420
128421 #ifdef SQLITE_ENABLE_API_ARMOR
128422 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
128423 #endif
128424 *ppDb = 0;
128425 #ifndef SQLITE_OMIT_AUTOINIT
128426 rc = sqlite3_initialize();
128427 if( rc ) return rc;
128428 #endif
@@ -127624,17 +128708,19 @@
128708 ){
128709 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
128710 sqlite3_value *pVal;
128711 int rc;
128712
128713 #ifdef SQLITE_ENABLE_API_ARMOR
128714 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
128715 #endif
128716 *ppDb = 0;
128717 #ifndef SQLITE_OMIT_AUTOINIT
128718 rc = sqlite3_initialize();
128719 if( rc ) return rc;
128720 #endif
128721 if( zFilename==0 ) zFilename = "\000\000";
128722 pVal = sqlite3ValueNew(0);
128723 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
128724 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
128725 if( zFilename8 ){
128726 rc = openDatabase(zFilename8, ppDb,
@@ -127660,17 +128746,11 @@
128746 const char *zName,
128747 int enc,
128748 void* pCtx,
128749 int(*xCompare)(void*,int,const void*,int,const void*)
128750 ){
128751 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
 
 
 
 
 
 
128752 }
128753
128754 /*
128755 ** Register a new collation sequence with the database handle db.
128756 */
@@ -127681,10 +128761,14 @@
128761 void* pCtx,
128762 int(*xCompare)(void*,int,const void*,int,const void*),
128763 void(*xDel)(void*)
128764 ){
128765 int rc;
128766
128767 #ifdef SQLITE_ENABLE_API_ARMOR
128768 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
128769 #endif
128770 sqlite3_mutex_enter(db->mutex);
128771 assert( !db->mallocFailed );
128772 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
128773 rc = sqlite3ApiExit(db, rc);
128774 sqlite3_mutex_leave(db->mutex);
@@ -127702,10 +128786,14 @@
128786 void* pCtx,
128787 int(*xCompare)(void*,int,const void*,int,const void*)
128788 ){
128789 int rc = SQLITE_OK;
128790 char *zName8;
128791
128792 #ifdef SQLITE_ENABLE_API_ARMOR
128793 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
128794 #endif
128795 sqlite3_mutex_enter(db->mutex);
128796 assert( !db->mallocFailed );
128797 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
128798 if( zName8 ){
128799 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
@@ -127724,10 +128812,13 @@
128812 SQLITE_API int sqlite3_collation_needed(
128813 sqlite3 *db,
128814 void *pCollNeededArg,
128815 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
128816 ){
128817 #ifdef SQLITE_ENABLE_API_ARMOR
128818 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
128819 #endif
128820 sqlite3_mutex_enter(db->mutex);
128821 db->xCollNeeded = xCollNeeded;
128822 db->xCollNeeded16 = 0;
128823 db->pCollNeededArg = pCollNeededArg;
128824 sqlite3_mutex_leave(db->mutex);
@@ -127742,10 +128833,13 @@
128833 SQLITE_API int sqlite3_collation_needed16(
128834 sqlite3 *db,
128835 void *pCollNeededArg,
128836 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
128837 ){
128838 #ifdef SQLITE_ENABLE_API_ARMOR
128839 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
128840 #endif
128841 sqlite3_mutex_enter(db->mutex);
128842 db->xCollNeeded = 0;
128843 db->xCollNeeded16 = xCollNeeded16;
128844 db->pCollNeededArg = pCollNeededArg;
128845 sqlite3_mutex_leave(db->mutex);
@@ -127768,10 +128862,16 @@
128862 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
128863 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
128864 ** by the next COMMIT or ROLLBACK.
128865 */
128866 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
128867 #ifdef SQLITE_ENABLE_API_ARMOR
128868 if( !sqlite3SafetyCheckOk(db) ){
128869 (void)SQLITE_MISUSE_BKPT;
128870 return 0;
128871 }
128872 #endif
128873 return db->autoCommit;
128874 }
128875
128876 /*
128877 ** The following routines are substitutes for constants SQLITE_CORRUPT,
@@ -127950,10 +129050,13 @@
129050
129051 /*
129052 ** Enable or disable the extended result codes.
129053 */
129054 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
129055 #ifdef SQLITE_ENABLE_API_ARMOR
129056 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
129057 #endif
129058 sqlite3_mutex_enter(db->mutex);
129059 db->errMask = onoff ? 0xffffffff : 0xff;
129060 sqlite3_mutex_leave(db->mutex);
129061 return SQLITE_OK;
129062 }
@@ -127963,10 +129066,13 @@
129066 */
129067 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
129068 int rc = SQLITE_ERROR;
129069 Btree *pBtree;
129070
129071 #ifdef SQLITE_ENABLE_API_ARMOR
129072 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
129073 #endif
129074 sqlite3_mutex_enter(db->mutex);
129075 pBtree = sqlite3DbNameToBtree(db, zDbName);
129076 if( pBtree ){
129077 Pager *pPager;
129078 sqlite3_file *fd;
@@ -128305,11 +129411,11 @@
129411 ** query parameter we seek. This routine returns the value of the zParam
129412 ** parameter if it exists. If the parameter does not exist, this routine
129413 ** returns a NULL pointer.
129414 */
129415 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
129416 if( zFilename==0 || zParam==0 ) return 0;
129417 zFilename += sqlite3Strlen30(zFilename) + 1;
129418 while( zFilename[0] ){
129419 int x = strcmp(zFilename, zParam);
129420 zFilename += sqlite3Strlen30(zFilename) + 1;
129421 if( x==0 ) return zFilename;
@@ -128361,19 +129467,31 @@
129467 /*
129468 ** Return the filename of the database associated with a database
129469 ** connection.
129470 */
129471 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
129472 #ifdef SQLITE_ENABLE_API_ARMOR
129473 if( !sqlite3SafetyCheckOk(db) ){
129474 (void)SQLITE_MISUSE_BKPT;
129475 return 0;
129476 }
129477 #endif
129478 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
129479 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
129480 }
129481
129482 /*
129483 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
129484 ** no such database exists.
129485 */
129486 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
129487 #ifdef SQLITE_ENABLE_API_ARMOR
129488 if( !sqlite3SafetyCheckOk(db) ){
129489 (void)SQLITE_MISUSE_BKPT;
129490 return -1;
129491 }
129492 #endif
129493 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
129494 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
129495 }
129496
129497 /************** End of main.c ************************************************/
129498
+308 -182
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -55,11 +55,11 @@
5555
5656
5757
/*
5858
** These no-op macros are used in front of interfaces to mark those
5959
** interfaces as either deprecated or experimental. New applications
60
-** should not use deprecated interfaces - they are support for backwards
60
+** should not use deprecated interfaces - they are supported for backwards
6161
** compatibility only. Application writers should be aware that
6262
** experimental interfaces are subject to change in point releases.
6363
**
6464
** These macros used to resolve to various kinds of compiler magic that
6565
** would generate warning messages when they were used. But that
@@ -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.7.1"
111
-#define SQLITE_VERSION_NUMBER 3008007
112
-#define SQLITE_SOURCE_ID "2014-10-29 01:27:43 83afe23e553e802c0947c80d0ffdd120423e7c52"
110
+#define SQLITE_VERSION "3.8.8"
111
+#define SQLITE_VERSION_NUMBER 3008008
112
+#define SQLITE_SOURCE_ID "2014-11-11 19:07:56 1412fcc480799ecbd68d44dd18d5bad40e20ccf1"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -1502,29 +1502,31 @@
15021502
** it is not possible to set the Serialized [threading mode] and
15031503
** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
15041504
** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
15051505
**
15061506
** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1507
-** <dd> ^(This option takes a single argument which is a pointer to an
1508
-** instance of the [sqlite3_mem_methods] structure. The argument specifies
1507
+** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1508
+** a pointer to an instance of the [sqlite3_mem_methods] structure.
1509
+** The argument specifies
15091510
** alternative low-level memory allocation routines to be used in place of
15101511
** the memory allocation routines built into SQLite.)^ ^SQLite makes
15111512
** its own private copy of the content of the [sqlite3_mem_methods] structure
15121513
** before the [sqlite3_config()] call returns.</dd>
15131514
**
15141515
** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1515
-** <dd> ^(This option takes a single argument which is a pointer to an
1516
-** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
1516
+** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1517
+** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1518
+** The [sqlite3_mem_methods]
15171519
** structure is filled with the currently defined memory allocation routines.)^
15181520
** This option can be used to overload the default memory allocation
15191521
** routines with a wrapper that simulations memory allocation failure or
15201522
** tracks memory usage, for example. </dd>
15211523
**
15221524
** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1523
-** <dd> ^This option takes single argument of type int, interpreted as a
1524
-** boolean, which enables or disables the collection of memory allocation
1525
-** statistics. ^(When memory allocation statistics are disabled, the
1525
+** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1526
+** interpreted as a boolean, which enables or disables the collection of
1527
+** memory allocation statistics. ^(When memory allocation statistics are disabled, the
15261528
** following SQLite interfaces become non-operational:
15271529
** <ul>
15281530
** <li> [sqlite3_memory_used()]
15291531
** <li> [sqlite3_memory_highwater()]
15301532
** <li> [sqlite3_soft_heap_limit64()]
@@ -1534,78 +1536,90 @@
15341536
** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
15351537
** allocation statistics are disabled by default.
15361538
** </dd>
15371539
**
15381540
** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1539
-** <dd> ^This option specifies a static memory buffer that SQLite can use for
1540
-** scratch memory. There are three arguments: A pointer an 8-byte
1541
+** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
1542
+** that SQLite can use for scratch memory. ^(There are three arguments
1543
+** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
15411544
** aligned memory buffer from which the scratch allocations will be
15421545
** drawn, the size of each scratch allocation (sz),
1543
-** and the maximum number of scratch allocations (N). The sz
1544
-** argument must be a multiple of 16.
1546
+** and the maximum number of scratch allocations (N).)^
15451547
** The first argument must be a pointer to an 8-byte aligned buffer
15461548
** of at least sz*N bytes of memory.
1547
-** ^SQLite will use no more than two scratch buffers per thread. So
1548
-** N should be set to twice the expected maximum number of threads.
1549
-** ^SQLite will never require a scratch buffer that is more than 6
1550
-** times the database page size. ^If SQLite needs needs additional
1549
+** ^SQLite will not use more than one scratch buffers per thread.
1550
+** ^SQLite will never request a scratch buffer that is more than 6
1551
+** times the database page size.
1552
+** ^If SQLite needs needs additional
15511553
** scratch memory beyond what is provided by this configuration option, then
1552
-** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1554
+** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
1555
+** ^When the application provides any amount of scratch memory using
1556
+** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
1557
+** [sqlite3_malloc|heap allocations].
1558
+** This can help [Robson proof|prevent memory allocation failures] due to heap
1559
+** fragmentation in low-memory embedded systems.
1560
+** </dd>
15531561
**
15541562
** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1555
-** <dd> ^This option specifies a static memory buffer that SQLite can use for
1556
-** the database page cache with the default page cache implementation.
1563
+** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
1564
+** that SQLite can use for the database page cache with the default page
1565
+** cache implementation.
15571566
** This configuration should not be used if an application-define page
1558
-** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1559
-** There are three arguments to this option: A pointer to 8-byte aligned
1567
+** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
1568
+** configuration option.
1569
+** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned
15601570
** memory, the size of each page buffer (sz), and the number of pages (N).
15611571
** The sz argument should be the size of the largest database page
1562
-** (a power of two between 512 and 32768) plus a little extra for each
1563
-** page header. ^The page header size is 20 to 40 bytes depending on
1564
-** the host architecture. ^It is harmless, apart from the wasted memory,
1565
-** to make sz a little too large. The first
1566
-** argument should point to an allocation of at least sz*N bytes of memory.
1572
+** (a power of two between 512 and 32768) plus some extra bytes for each
1573
+** page header. ^The number of extra bytes needed by the page header
1574
+** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option
1575
+** to [sqlite3_config()].
1576
+** ^It is harmless, apart from the wasted memory,
1577
+** for the sz parameter to be larger than necessary. The first
1578
+** argument should pointer to an 8-byte aligned block of memory that
1579
+** is at least sz*N bytes of memory, otherwise subsequent behavior is
1580
+** undefined.
15671581
** ^SQLite will use the memory provided by the first argument to satisfy its
15681582
** memory needs for the first N pages that it adds to cache. ^If additional
15691583
** page cache memory is needed beyond what is provided by this option, then
1570
-** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1571
-** The pointer in the first argument must
1572
-** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1573
-** will be undefined.</dd>
1584
+** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
15741585
**
15751586
** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1576
-** <dd> ^This option specifies a static memory buffer that SQLite will use
1577
-** for all of its dynamic memory allocation needs beyond those provided
1578
-** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1579
-** There are three arguments: An 8-byte aligned pointer to the memory,
1587
+** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1588
+** that SQLite will use for all of its dynamic memory allocation needs
1589
+** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1590
+** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1591
+** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1592
+** [SQLITE_ERROR] if invoked otherwise.
1593
+** ^There are three arguments to SQLITE_CONFIG_HEAP:
1594
+** An 8-byte aligned pointer to the memory,
15801595
** the number of bytes in the memory buffer, and the minimum allocation size.
15811596
** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
15821597
** to using its default memory allocator (the system malloc() implementation),
15831598
** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1584
-** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1585
-** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1599
+** memory pointer is not NULL then the alternative memory
15861600
** allocator is engaged to handle all of SQLites memory allocation needs.
15871601
** The first pointer (the memory pointer) must be aligned to an 8-byte
15881602
** boundary or subsequent behavior of SQLite will be undefined.
15891603
** The minimum allocation size is capped at 2**12. Reasonable values
15901604
** for the minimum allocation size are 2**5 through 2**8.</dd>
15911605
**
15921606
** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1593
-** <dd> ^(This option takes a single argument which is a pointer to an
1594
-** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1595
-** alternative low-level mutex routines to be used in place
1607
+** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1608
+** pointer to an instance of the [sqlite3_mutex_methods] structure.
1609
+** The argument specifies alternative low-level mutex routines to be used in place
15961610
** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
15971611
** content of the [sqlite3_mutex_methods] structure before the call to
15981612
** [sqlite3_config()] returns. ^If SQLite is compiled with
15991613
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
16001614
** the entire mutexing subsystem is omitted from the build and hence calls to
16011615
** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
16021616
** return [SQLITE_ERROR].</dd>
16031617
**
16041618
** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1605
-** <dd> ^(This option takes a single argument which is a pointer to an
1606
-** instance of the [sqlite3_mutex_methods] structure. The
1619
+** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1620
+** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
16071621
** [sqlite3_mutex_methods]
16081622
** structure is filled with the currently defined mutex routines.)^
16091623
** This option can be used to overload the default mutex allocation
16101624
** routines with a wrapper used to track mutex usage for performance
16111625
** profiling or testing, for example. ^If SQLite is compiled with
@@ -1613,28 +1627,28 @@
16131627
** the entire mutexing subsystem is omitted from the build and hence calls to
16141628
** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
16151629
** return [SQLITE_ERROR].</dd>
16161630
**
16171631
** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1618
-** <dd> ^(This option takes two arguments that determine the default
1619
-** memory allocation for the lookaside memory allocator on each
1620
-** [database connection]. The first argument is the
1632
+** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1633
+** the default size of lookaside memory on each [database connection].
1634
+** The first argument is the
16211635
** size of each lookaside buffer slot and the second is the number of
1622
-** slots allocated to each database connection.)^ ^(This option sets the
1623
-** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1624
-** verb to [sqlite3_db_config()] can be used to change the lookaside
1636
+** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
1637
+** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1638
+** option to [sqlite3_db_config()] can be used to change the lookaside
16251639
** configuration on individual connections.)^ </dd>
16261640
**
16271641
** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1628
-** <dd> ^(This option takes a single argument which is a pointer to
1629
-** an [sqlite3_pcache_methods2] object. This object specifies the interface
1630
-** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1631
-** object and uses it for page cache memory allocations.</dd>
1642
+** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1643
+** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
1644
+** the interface to a custom page cache implementation.)^
1645
+** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
16321646
**
16331647
** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1634
-** <dd> ^(This option takes a single argument which is a pointer to an
1635
-** [sqlite3_pcache_methods2] object. SQLite copies of the current
1648
+** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1649
+** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current
16361650
** page cache implementation into that object.)^ </dd>
16371651
**
16381652
** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
16391653
** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
16401654
** global [error log].
@@ -1654,26 +1668,27 @@
16541668
** supplied by the application must not invoke any SQLite interface.
16551669
** In a multi-threaded application, the application-defined logger
16561670
** function must be threadsafe. </dd>
16571671
**
16581672
** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1659
-** <dd>^(This option takes a single argument of type int. If non-zero, then
1660
-** URI handling is globally enabled. If the parameter is zero, then URI handling
1661
-** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1662
-** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1673
+** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1674
+** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1675
+** then URI handling is globally disabled.)^ ^If URI handling is globally enabled,
1676
+** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
16631677
** specified as part of [ATTACH] commands are interpreted as URIs, regardless
16641678
** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
16651679
** connection is opened. ^If it is globally disabled, filenames are
16661680
** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
16671681
** database connection is opened. ^(By default, URI handling is globally
16681682
** disabled. The default value may be changed by compiling with the
16691683
** [SQLITE_USE_URI] symbol defined.)^
16701684
**
16711685
** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1672
-** <dd>^This option takes a single integer argument which is interpreted as
1673
-** a boolean in order to enable or disable the use of covering indices for
1674
-** full table scans in the query optimizer. ^The default setting is determined
1686
+** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1687
+** argument which is interpreted as a boolean in order to enable or disable
1688
+** the use of covering indices for full table scans in the query optimizer.
1689
+** ^The default setting is determined
16751690
** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
16761691
** if that compile-time option is omitted.
16771692
** The ability to disable the use of covering indices for full table scans
16781693
** is because some incorrectly coded legacy applications might malfunction
16791694
** when the optimization is enabled. Providing the ability to
@@ -1709,23 +1724,32 @@
17091724
** that are the default mmap size limit (the default setting for
17101725
** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
17111726
** ^The default setting can be overridden by each database connection using
17121727
** either the [PRAGMA mmap_size] command, or by using the
17131728
** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1714
-** cannot be changed at run-time. Nor may the maximum allowed mmap size
1715
-** exceed the compile-time maximum mmap size set by the
1729
+** will be silently truncated if necessary so that it does not exceed the
1730
+** compile-time maximum mmap size set by the
17161731
** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
17171732
** ^If either argument to this option is negative, then that argument is
17181733
** changed to its compile-time default.
17191734
**
17201735
** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
17211736
** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1722
-** <dd>^This option is only available if SQLite is compiled for Windows
1723
-** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1724
-** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1737
+** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1738
+** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1739
+** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
17251740
** that specifies the maximum size of the created heap.
17261741
** </dl>
1742
+**
1743
+** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1744
+** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1745
+** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1746
+** is a pointer to an integer and writes into that integer the number of extra
1747
+** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of
1748
+** extra space required can change depending on the compiler,
1749
+** target platform, and SQLite version.
1750
+** </dl>
17271751
*/
17281752
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
17291753
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
17301754
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
17311755
#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1746,10 +1770,11 @@
17461770
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
17471771
#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
17481772
#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
17491773
#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
17501774
#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
1775
+#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
17511776
17521777
/*
17531778
** CAPI3REF: Database Connection Configuration Options
17541779
**
17551780
** These constants are the available integer configuration options that
@@ -1873,51 +1898,49 @@
18731898
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
18741899
18751900
/*
18761901
** CAPI3REF: Count The Number Of Rows Modified
18771902
**
1878
-** ^This function returns the number of database rows that were changed
1879
-** or inserted or deleted by the most recently completed SQL statement
1880
-** on the [database connection] specified by the first parameter.
1881
-** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
1882
-** or [DELETE] statement are counted. Auxiliary changes caused by
1883
-** triggers or [foreign key actions] are not counted.)^ Use the
1884
-** [sqlite3_total_changes()] function to find the total number of changes
1885
-** including changes caused by triggers and foreign key actions.
1886
-**
1887
-** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
1888
-** are not counted. Only real table changes are counted.
1889
-**
1890
-** ^(A "row change" is a change to a single row of a single table
1891
-** caused by an INSERT, DELETE, or UPDATE statement. Rows that
1892
-** are changed as side effects of [REPLACE] constraint resolution,
1893
-** rollback, ABORT processing, [DROP TABLE], or by any other
1894
-** mechanisms do not count as direct row changes.)^
1895
-**
1896
-** A "trigger context" is a scope of execution that begins and
1897
-** ends with the script of a [CREATE TRIGGER | trigger].
1898
-** Most SQL statements are
1899
-** evaluated outside of any trigger. This is the "top level"
1900
-** trigger context. If a trigger fires from the top level, a
1901
-** new trigger context is entered for the duration of that one
1902
-** trigger. Subtriggers create subcontexts for their duration.
1903
-**
1904
-** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
1905
-** not create a new trigger context.
1906
-**
1907
-** ^This function returns the number of direct row changes in the
1908
-** most recent INSERT, UPDATE, or DELETE statement within the same
1909
-** trigger context.
1910
-**
1911
-** ^Thus, when called from the top level, this function returns the
1912
-** number of changes in the most recent INSERT, UPDATE, or DELETE
1913
-** that also occurred at the top level. ^(Within the body of a trigger,
1914
-** the sqlite3_changes() interface can be called to find the number of
1915
-** changes in the most recently completed INSERT, UPDATE, or DELETE
1916
-** statement within the body of the same trigger.
1917
-** However, the number returned does not include changes
1918
-** caused by subtriggers since those have their own context.)^
1903
+** ^This function returns the number of rows modified, inserted or
1904
+** deleted by the most recently completed INSERT, UPDATE or DELETE
1905
+** statement on the database connection specified by the only parameter.
1906
+** ^Executing any other type of SQL statement does not modify the value
1907
+** returned by this function.
1908
+**
1909
+** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
1910
+** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
1911
+** [foreign key actions] or [REPLACE] constraint resolution are not counted.
1912
+**
1913
+** Changes to a view that are intercepted by
1914
+** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
1915
+** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
1916
+** DELETE statement run on a view is always zero. Only changes made to real
1917
+** tables are counted.
1918
+**
1919
+** Things are more complicated if the sqlite3_changes() function is
1920
+** executed while a trigger program is running. This may happen if the
1921
+** program uses the [changes() SQL function], or if some other callback
1922
+** function invokes sqlite3_changes() directly. Essentially:
1923
+**
1924
+** <ul>
1925
+** <li> ^(Before entering a trigger program the value returned by
1926
+** sqlite3_changes() function is saved. After the trigger program
1927
+** has finished, the original value is restored.)^
1928
+**
1929
+** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
1930
+** statement sets the value returned by sqlite3_changes()
1931
+** upon completion as normal. Of course, this value will not include
1932
+** any changes performed by sub-triggers, as the sqlite3_changes()
1933
+** value will be saved and restored after each sub-trigger has run.)^
1934
+** </ul>
1935
+**
1936
+** ^This means that if the changes() SQL function (or similar) is used
1937
+** by the first INSERT, UPDATE or DELETE statement within a trigger, it
1938
+** returns the value as set when the calling statement began executing.
1939
+** ^If it is used by the second or subsequent such statement within a trigger
1940
+** program, the value returned reflects the number of rows modified by the
1941
+** previous INSERT, UPDATE or DELETE statement within the same trigger.
19191942
**
19201943
** See also the [sqlite3_total_changes()] interface, the
19211944
** [count_changes pragma], and the [changes() SQL function].
19221945
**
19231946
** If a separate thread makes changes on the same database connection
@@ -1927,24 +1950,21 @@
19271950
SQLITE_API int sqlite3_changes(sqlite3*);
19281951
19291952
/*
19301953
** CAPI3REF: Total Number Of Rows Modified
19311954
**
1932
-** ^This function returns the number of row changes caused by [INSERT],
1933
-** [UPDATE] or [DELETE] statements since the [database connection] was opened.
1934
-** ^(The count returned by sqlite3_total_changes() includes all changes
1935
-** from all [CREATE TRIGGER | trigger] contexts and changes made by
1936
-** [foreign key actions]. However,
1937
-** the count does not include changes used to implement [REPLACE] constraints,
1938
-** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
1939
-** count does not include rows of views that fire an [INSTEAD OF trigger],
1940
-** though if the INSTEAD OF trigger makes changes of its own, those changes
1941
-** are counted.)^
1942
-** ^The sqlite3_total_changes() function counts the changes as soon as
1943
-** the statement that makes them is completed (when the statement handle
1944
-** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
1945
-**
1955
+** ^This function returns the total number of rows inserted, modified or
1956
+** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
1957
+** since the database connection was opened, including those executed as
1958
+** part of trigger programs. ^Executing any other type of SQL statement
1959
+** does not affect the value returned by sqlite3_total_changes().
1960
+**
1961
+** ^Changes made as part of [foreign key actions] are included in the
1962
+** count, but those made as part of REPLACE constraint resolution are
1963
+** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
1964
+** are not counted.
1965
+**
19461966
** See also the [sqlite3_changes()] interface, the
19471967
** [count_changes pragma], and the [total_changes() SQL function].
19481968
**
19491969
** If a separate thread makes changes on the same database connection
19501970
** while [sqlite3_total_changes()] is running then the value
@@ -2418,17 +2438,18 @@
24182438
** already uses the largest possible [ROWID]. The PRNG is also used for
24192439
** the build-in random() and randomblob() SQL functions. This interface allows
24202440
** applications to access the same PRNG for other purposes.
24212441
**
24222442
** ^A call to this routine stores N bytes of randomness into buffer P.
2423
-** ^If N is less than one, then P can be a NULL pointer.
2443
+** ^The P parameter can be a NULL pointer.
24242444
**
24252445
** ^If this routine has not been previously called or if the previous
2426
-** call had N less than one, then the PRNG is seeded using randomness
2427
-** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2428
-** ^If the previous call to this routine had an N of 1 or more then
2429
-** the pseudo-randomness is generated
2446
+** call had N less than one or a NULL pointer for P, then the PRNG is
2447
+** seeded using randomness obtained from the xRandomness method of
2448
+** the default [sqlite3_vfs] object.
2449
+** ^If the previous call to this routine had an N of 1 or more and a
2450
+** non-NULL P then the pseudo-randomness is generated
24302451
** internally and without recourse to the [sqlite3_vfs] xRandomness
24312452
** method.
24322453
*/
24332454
SQLITE_API void sqlite3_randomness(int N, void *P);
24342455
@@ -5638,31 +5659,47 @@
56385659
** in other words, the same BLOB that would be selected by:
56395660
**
56405661
** <pre>
56415662
** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
56425663
** </pre>)^
5664
+**
5665
+** ^(Parameter zDb is not the filename that contains the database, but
5666
+** rather the symbolic name of the database. For attached databases, this is
5667
+** the name that appears after the AS keyword in the [ATTACH] statement.
5668
+** For the main database file, the database name is "main". For TEMP
5669
+** tables, the database name is "temp".)^
56435670
**
56445671
** ^If the flags parameter is non-zero, then the BLOB is opened for read
5645
-** and write access. ^If it is zero, the BLOB is opened for read access.
5646
-** ^It is not possible to open a column that is part of an index or primary
5647
-** key for writing. ^If [foreign key constraints] are enabled, it is
5648
-** not possible to open a column that is part of a [child key] for writing.
5649
-**
5650
-** ^Note that the database name is not the filename that contains
5651
-** the database but rather the symbolic name of the database that
5652
-** appears after the AS keyword when the database is connected using [ATTACH].
5653
-** ^For the main database file, the database name is "main".
5654
-** ^For TEMP tables, the database name is "temp".
5655
-**
5656
-** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5657
-** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5658
-** to be a null pointer.)^
5659
-** ^This function sets the [database connection] error code and message
5660
-** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5661
-** functions. ^Note that the *ppBlob variable is always initialized in a
5662
-** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5663
-** regardless of the success or failure of this routine.
5672
+** and write access. ^If the flags parameter is zero, the BLOB is opened for
5673
+** read-only access.
5674
+**
5675
+** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
5676
+** in *ppBlob. Otherwise an [error code] is returned and, unless the error
5677
+** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
5678
+** the API is not misused, it is always safe to call [sqlite3_blob_close()]
5679
+** on *ppBlob after this function it returns.
5680
+**
5681
+** This function fails with SQLITE_ERROR if any of the following are true:
5682
+** <ul>
5683
+** <li> ^(Database zDb does not exist)^,
5684
+** <li> ^(Table zTable does not exist within database zDb)^,
5685
+** <li> ^(Table zTable is a WITHOUT ROWID table)^,
5686
+** <li> ^(Column zColumn does not exist)^,
5687
+** <li> ^(Row iRow is not present in the table)^,
5688
+** <li> ^(The specified column of row iRow contains a value that is not
5689
+** a TEXT or BLOB value)^,
5690
+** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
5691
+** constraint and the blob is being opened for read/write access)^,
5692
+** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
5693
+** column zColumn is part of a [child key] definition and the blob is
5694
+** being opened for read/write access)^.
5695
+** </ul>
5696
+**
5697
+** ^Unless it returns SQLITE_MISUSE, this function sets the
5698
+** [database connection] error code and message accessible via
5699
+** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5700
+**
56645701
**
56655702
** ^(If the row that a BLOB handle points to is modified by an
56665703
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
56675704
** then the BLOB handle is marked as "expired".
56685705
** This is true if any column of the row is changed, even a column
@@ -5676,17 +5713,13 @@
56765713
** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
56775714
** the opened blob. ^The size of a blob may not be changed by this
56785715
** interface. Use the [UPDATE] SQL command to change the size of a
56795716
** blob.
56805717
**
5681
-** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5682
-** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5683
-**
56845718
** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5685
-** and the built-in [zeroblob] SQL function can be used, if desired,
5686
-** to create an empty, zero-filled blob in which to read or write using
5687
-** this interface.
5719
+** and the built-in [zeroblob] SQL function may be used to create a
5720
+** zero-filled blob to read or write using the incremental-blob interface.
56885721
**
56895722
** To avoid a resource leak, every open [BLOB handle] should eventually
56905723
** be released by a call to [sqlite3_blob_close()].
56915724
*/
56925725
SQLITE_API int sqlite3_blob_open(
@@ -5724,28 +5757,26 @@
57245757
SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
57255758
57265759
/*
57275760
** CAPI3REF: Close A BLOB Handle
57285761
**
5729
-** ^Closes an open [BLOB handle].
5730
-**
5731
-** ^Closing a BLOB shall cause the current transaction to commit
5732
-** if there are no other BLOBs, no pending prepared statements, and the
5733
-** database connection is in [autocommit mode].
5734
-** ^If any writes were made to the BLOB, they might be held in cache
5735
-** until the close operation if they will fit.
5736
-**
5737
-** ^(Closing the BLOB often forces the changes
5738
-** out to disk and so if any I/O errors occur, they will likely occur
5739
-** at the time when the BLOB is closed. Any errors that occur during
5740
-** closing are reported as a non-zero return value.)^
5741
-**
5742
-** ^(The BLOB is closed unconditionally. Even if this routine returns
5743
-** an error code, the BLOB is still closed.)^
5744
-**
5745
-** ^Calling this routine with a null pointer (such as would be returned
5746
-** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5762
+** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
5763
+** unconditionally. Even if this routine returns an error code, the
5764
+** handle is still closed.)^
5765
+**
5766
+** ^If the blob handle being closed was opened for read-write access, and if
5767
+** the database is in auto-commit mode and there are no other open read-write
5768
+** blob handles or active write statements, the current transaction is
5769
+** committed. ^If an error occurs while committing the transaction, an error
5770
+** code is returned and the transaction rolled back.
5771
+**
5772
+** Calling this function with an argument that is not a NULL pointer or an
5773
+** open blob handle results in undefined behaviour. ^Calling this routine
5774
+** with a null pointer (such as would be returned by a failed call to
5775
+** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
5776
+** is passed a valid open blob handle, the values returned by the
5777
+** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
57475778
*/
57485779
SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
57495780
57505781
/*
57515782
** CAPI3REF: Return The Size Of An Open BLOB
@@ -5791,36 +5822,39 @@
57915822
SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
57925823
57935824
/*
57945825
** CAPI3REF: Write Data Into A BLOB Incrementally
57955826
**
5796
-** ^This function is used to write data into an open [BLOB handle] from a
5797
-** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5798
-** into the open BLOB, starting at offset iOffset.
5827
+** ^(This function is used to write data into an open [BLOB handle] from a
5828
+** caller-supplied buffer. N bytes of data are copied from the buffer Z
5829
+** into the open BLOB, starting at offset iOffset.)^
5830
+**
5831
+** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5832
+** Otherwise, an [error code] or an [extended error code] is returned.)^
5833
+** ^Unless SQLITE_MISUSE is returned, this function sets the
5834
+** [database connection] error code and message accessible via
5835
+** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
57995836
**
58005837
** ^If the [BLOB handle] passed as the first argument was not opened for
58015838
** writing (the flags parameter to [sqlite3_blob_open()] was zero),
58025839
** this function returns [SQLITE_READONLY].
58035840
**
5804
-** ^This function may only modify the contents of the BLOB; it is
5841
+** This function may only modify the contents of the BLOB; it is
58055842
** not possible to increase the size of a BLOB using this API.
58065843
** ^If offset iOffset is less than N bytes from the end of the BLOB,
5807
-** [SQLITE_ERROR] is returned and no data is written. ^If N is
5808
-** less than zero [SQLITE_ERROR] is returned and no data is written.
5809
-** The size of the BLOB (and hence the maximum value of N+iOffset)
5810
-** can be determined using the [sqlite3_blob_bytes()] interface.
5844
+** [SQLITE_ERROR] is returned and no data is written. The size of the
5845
+** BLOB (and hence the maximum value of N+iOffset) can be determined
5846
+** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
5847
+** than zero [SQLITE_ERROR] is returned and no data is written.
58115848
**
58125849
** ^An attempt to write to an expired [BLOB handle] fails with an
58135850
** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
58145851
** before the [BLOB handle] expired are not rolled back by the
58155852
** expiration of the handle, though of course those changes might
58165853
** have been overwritten by the statement that expired the BLOB handle
58175854
** or by other independent statements.
58185855
**
5819
-** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5820
-** Otherwise, an [error code] or an [extended error code] is returned.)^
5821
-**
58225856
** This routine only works on a [BLOB handle] which has been created
58235857
** by a prior successful call to [sqlite3_blob_open()] and which has not
58245858
** been closed by [sqlite3_blob_close()]. Passing any other pointer in
58255859
** to this routine results in undefined and probably undesirable behavior.
58265860
**
@@ -7409,10 +7443,102 @@
74097443
/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
74107444
#define SQLITE_FAIL 3
74117445
/* #define SQLITE_ABORT 4 // Also an error code */
74127446
#define SQLITE_REPLACE 5
74137447
7448
+/*
7449
+** CAPI3REF: Prepared Statement Scan Status Opcodes
7450
+** KEYWORDS: {scanstatus options}
7451
+**
7452
+** The following constants can be used for the T parameter to the
7453
+** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
7454
+** different metric for sqlite3_stmt_scanstatus() to return.
7455
+**
7456
+** <dl>
7457
+** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
7458
+** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7459
+** total number of times that the X-th loop has run.</dd>
7460
+**
7461
+** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
7462
+** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7463
+** total number of rows examined by all iterations of the X-th loop.</dd>
7464
+**
7465
+** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
7466
+** <dd>^The "double" variable pointed to by the T parameter will be set to the
7467
+** query planner's estimate for the average number of rows output from each
7468
+** iteration of the X-th loop. If the query planner's estimates was accurate,
7469
+** then this value will approximate the quotient NVISIT/NLOOP and the
7470
+** product of this value for all prior loops with the same SELECTID will
7471
+** be the NLOOP value for the current loop.
7472
+**
7473
+** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
7474
+** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7475
+** a zero-terminated UTF-8 string containing the name of the index or table used
7476
+** for the X-th loop.
7477
+**
7478
+** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
7479
+** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7480
+** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description
7481
+** for the X-th loop.
7482
+**
7483
+** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
7484
+** <dd>^The "int" variable pointed to by the T parameter will be set to the
7485
+** "select-id" for the X-th loop. The select-id identifies which query or
7486
+** subquery the loop is part of. The main query has a select-id of zero.
7487
+** The select-id is the same value as is output in the first column
7488
+** of an [EXPLAIN QUERY PLAN] query.
7489
+** </dl>
7490
+*/
7491
+#define SQLITE_SCANSTAT_NLOOP 0
7492
+#define SQLITE_SCANSTAT_NVISIT 1
7493
+#define SQLITE_SCANSTAT_EST 2
7494
+#define SQLITE_SCANSTAT_NAME 3
7495
+#define SQLITE_SCANSTAT_EXPLAIN 4
7496
+#define SQLITE_SCANSTAT_SELECTID 5
7497
+
7498
+/*
7499
+** CAPI3REF: Prepared Statement Scan Status
7500
+**
7501
+** Return status data for a single loop within query pStmt.
7502
+**
7503
+** The "iScanStatusOp" parameter determines which status information to return.
7504
+** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of
7505
+** this interface is undefined.
7506
+** ^The requested measurement is written into a variable pointed to by
7507
+** the "pOut" parameter.
7508
+** Parameter "idx" identifies the specific loop to retrieve statistics for.
7509
+** Loops are numbered starting from zero. ^If idx is out of range - less than
7510
+** zero or greater than or equal to the total number of loops used to implement
7511
+** the statement - a non-zero value is returned and the variable that pOut
7512
+** points to is unchanged.
7513
+**
7514
+** ^Statistics might not be available for all loops in all statements. ^In cases
7515
+** where there exist loops with no available statistics, this function behaves
7516
+** as if the loop did not exist - it returns non-zero and leave the variable
7517
+** that pOut points to unchanged.
7518
+**
7519
+** This API is only available if the library is built with pre-processor
7520
+** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7521
+**
7522
+** See also: [sqlite3_stmt_scanstatus_reset()]
7523
+*/
7524
+SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
7525
+ sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7526
+ int idx, /* Index of loop to report on */
7527
+ int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7528
+ void *pOut /* Result written here */
7529
+);
7530
+
7531
+/*
7532
+** CAPI3REF: Zero Scan-Status Counters
7533
+**
7534
+** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7535
+**
7536
+** This API is only available if the library is built with pre-processor
7537
+** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7538
+*/
7539
+SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
74147540
74157541
74167542
/*
74177543
** Undo the hack that converts floating point types to integer for
74187544
** builds on processors without floating point support.
74197545
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -55,11 +55,11 @@
55
56
57 /*
58 ** These no-op macros are used in front of interfaces to mark those
59 ** interfaces as either deprecated or experimental. New applications
60 ** should not use deprecated interfaces - they are support for backwards
61 ** compatibility only. Application writers should be aware that
62 ** experimental interfaces are subject to change in point releases.
63 **
64 ** These macros used to resolve to various kinds of compiler magic that
65 ** would generate warning messages when they were used. But that
@@ -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.7.1"
111 #define SQLITE_VERSION_NUMBER 3008007
112 #define SQLITE_SOURCE_ID "2014-10-29 01:27:43 83afe23e553e802c0947c80d0ffdd120423e7c52"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1502,29 +1502,31 @@
1502 ** it is not possible to set the Serialized [threading mode] and
1503 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1504 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1505 **
1506 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1507 ** <dd> ^(This option takes a single argument which is a pointer to an
1508 ** instance of the [sqlite3_mem_methods] structure. The argument specifies
 
1509 ** alternative low-level memory allocation routines to be used in place of
1510 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1511 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1512 ** before the [sqlite3_config()] call returns.</dd>
1513 **
1514 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1515 ** <dd> ^(This option takes a single argument which is a pointer to an
1516 ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
 
1517 ** structure is filled with the currently defined memory allocation routines.)^
1518 ** This option can be used to overload the default memory allocation
1519 ** routines with a wrapper that simulations memory allocation failure or
1520 ** tracks memory usage, for example. </dd>
1521 **
1522 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1523 ** <dd> ^This option takes single argument of type int, interpreted as a
1524 ** boolean, which enables or disables the collection of memory allocation
1525 ** statistics. ^(When memory allocation statistics are disabled, the
1526 ** following SQLite interfaces become non-operational:
1527 ** <ul>
1528 ** <li> [sqlite3_memory_used()]
1529 ** <li> [sqlite3_memory_highwater()]
1530 ** <li> [sqlite3_soft_heap_limit64()]
@@ -1534,78 +1536,90 @@
1534 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1535 ** allocation statistics are disabled by default.
1536 ** </dd>
1537 **
1538 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1539 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1540 ** scratch memory. There are three arguments: A pointer an 8-byte
 
1541 ** aligned memory buffer from which the scratch allocations will be
1542 ** drawn, the size of each scratch allocation (sz),
1543 ** and the maximum number of scratch allocations (N). The sz
1544 ** argument must be a multiple of 16.
1545 ** The first argument must be a pointer to an 8-byte aligned buffer
1546 ** of at least sz*N bytes of memory.
1547 ** ^SQLite will use no more than two scratch buffers per thread. So
1548 ** N should be set to twice the expected maximum number of threads.
1549 ** ^SQLite will never require a scratch buffer that is more than 6
1550 ** times the database page size. ^If SQLite needs needs additional
1551 ** scratch memory beyond what is provided by this configuration option, then
1552 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
 
 
 
 
 
 
1553 **
1554 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1555 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1556 ** the database page cache with the default page cache implementation.
 
1557 ** This configuration should not be used if an application-define page
1558 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1559 ** There are three arguments to this option: A pointer to 8-byte aligned
 
1560 ** memory, the size of each page buffer (sz), and the number of pages (N).
1561 ** The sz argument should be the size of the largest database page
1562 ** (a power of two between 512 and 32768) plus a little extra for each
1563 ** page header. ^The page header size is 20 to 40 bytes depending on
1564 ** the host architecture. ^It is harmless, apart from the wasted memory,
1565 ** to make sz a little too large. The first
1566 ** argument should point to an allocation of at least sz*N bytes of memory.
 
 
 
 
1567 ** ^SQLite will use the memory provided by the first argument to satisfy its
1568 ** memory needs for the first N pages that it adds to cache. ^If additional
1569 ** page cache memory is needed beyond what is provided by this option, then
1570 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1571 ** The pointer in the first argument must
1572 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1573 ** will be undefined.</dd>
1574 **
1575 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1576 ** <dd> ^This option specifies a static memory buffer that SQLite will use
1577 ** for all of its dynamic memory allocation needs beyond those provided
1578 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1579 ** There are three arguments: An 8-byte aligned pointer to the memory,
 
 
 
 
1580 ** the number of bytes in the memory buffer, and the minimum allocation size.
1581 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1582 ** to using its default memory allocator (the system malloc() implementation),
1583 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1584 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1585 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1586 ** allocator is engaged to handle all of SQLites memory allocation needs.
1587 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1588 ** boundary or subsequent behavior of SQLite will be undefined.
1589 ** The minimum allocation size is capped at 2**12. Reasonable values
1590 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1591 **
1592 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1593 ** <dd> ^(This option takes a single argument which is a pointer to an
1594 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1595 ** alternative low-level mutex routines to be used in place
1596 ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
1597 ** content of the [sqlite3_mutex_methods] structure before the call to
1598 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1599 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1600 ** the entire mutexing subsystem is omitted from the build and hence calls to
1601 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1602 ** return [SQLITE_ERROR].</dd>
1603 **
1604 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1605 ** <dd> ^(This option takes a single argument which is a pointer to an
1606 ** instance of the [sqlite3_mutex_methods] structure. The
1607 ** [sqlite3_mutex_methods]
1608 ** structure is filled with the currently defined mutex routines.)^
1609 ** This option can be used to overload the default mutex allocation
1610 ** routines with a wrapper used to track mutex usage for performance
1611 ** profiling or testing, for example. ^If SQLite is compiled with
@@ -1613,28 +1627,28 @@
1613 ** the entire mutexing subsystem is omitted from the build and hence calls to
1614 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1615 ** return [SQLITE_ERROR].</dd>
1616 **
1617 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1618 ** <dd> ^(This option takes two arguments that determine the default
1619 ** memory allocation for the lookaside memory allocator on each
1620 ** [database connection]. The first argument is the
1621 ** size of each lookaside buffer slot and the second is the number of
1622 ** slots allocated to each database connection.)^ ^(This option sets the
1623 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1624 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1625 ** configuration on individual connections.)^ </dd>
1626 **
1627 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1628 ** <dd> ^(This option takes a single argument which is a pointer to
1629 ** an [sqlite3_pcache_methods2] object. This object specifies the interface
1630 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1631 ** object and uses it for page cache memory allocations.</dd>
1632 **
1633 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1634 ** <dd> ^(This option takes a single argument which is a pointer to an
1635 ** [sqlite3_pcache_methods2] object. SQLite copies of the current
1636 ** page cache implementation into that object.)^ </dd>
1637 **
1638 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1639 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1640 ** global [error log].
@@ -1654,26 +1668,27 @@
1654 ** supplied by the application must not invoke any SQLite interface.
1655 ** In a multi-threaded application, the application-defined logger
1656 ** function must be threadsafe. </dd>
1657 **
1658 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1659 ** <dd>^(This option takes a single argument of type int. If non-zero, then
1660 ** URI handling is globally enabled. If the parameter is zero, then URI handling
1661 ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1662 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1663 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1664 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1665 ** connection is opened. ^If it is globally disabled, filenames are
1666 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1667 ** database connection is opened. ^(By default, URI handling is globally
1668 ** disabled. The default value may be changed by compiling with the
1669 ** [SQLITE_USE_URI] symbol defined.)^
1670 **
1671 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1672 ** <dd>^This option takes a single integer argument which is interpreted as
1673 ** a boolean in order to enable or disable the use of covering indices for
1674 ** full table scans in the query optimizer. ^The default setting is determined
 
1675 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1676 ** if that compile-time option is omitted.
1677 ** The ability to disable the use of covering indices for full table scans
1678 ** is because some incorrectly coded legacy applications might malfunction
1679 ** when the optimization is enabled. Providing the ability to
@@ -1709,23 +1724,32 @@
1709 ** that are the default mmap size limit (the default setting for
1710 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1711 ** ^The default setting can be overridden by each database connection using
1712 ** either the [PRAGMA mmap_size] command, or by using the
1713 ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1714 ** cannot be changed at run-time. Nor may the maximum allowed mmap size
1715 ** exceed the compile-time maximum mmap size set by the
1716 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1717 ** ^If either argument to this option is negative, then that argument is
1718 ** changed to its compile-time default.
1719 **
1720 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1721 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1722 ** <dd>^This option is only available if SQLite is compiled for Windows
1723 ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1724 ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1725 ** that specifies the maximum size of the created heap.
1726 ** </dl>
 
 
 
 
 
 
 
 
 
1727 */
1728 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1729 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1730 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
1731 #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1746,10 +1770,11 @@
1746 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1747 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1748 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1749 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
1750 #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
 
1751
1752 /*
1753 ** CAPI3REF: Database Connection Configuration Options
1754 **
1755 ** These constants are the available integer configuration options that
@@ -1873,51 +1898,49 @@
1873 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
1874
1875 /*
1876 ** CAPI3REF: Count The Number Of Rows Modified
1877 **
1878 ** ^This function returns the number of database rows that were changed
1879 ** or inserted or deleted by the most recently completed SQL statement
1880 ** on the [database connection] specified by the first parameter.
1881 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
1882 ** or [DELETE] statement are counted. Auxiliary changes caused by
1883 ** triggers or [foreign key actions] are not counted.)^ Use the
1884 ** [sqlite3_total_changes()] function to find the total number of changes
1885 ** including changes caused by triggers and foreign key actions.
1886 **
1887 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
1888 ** are not counted. Only real table changes are counted.
1889 **
1890 ** ^(A "row change" is a change to a single row of a single table
1891 ** caused by an INSERT, DELETE, or UPDATE statement. Rows that
1892 ** are changed as side effects of [REPLACE] constraint resolution,
1893 ** rollback, ABORT processing, [DROP TABLE], or by any other
1894 ** mechanisms do not count as direct row changes.)^
1895 **
1896 ** A "trigger context" is a scope of execution that begins and
1897 ** ends with the script of a [CREATE TRIGGER | trigger].
1898 ** Most SQL statements are
1899 ** evaluated outside of any trigger. This is the "top level"
1900 ** trigger context. If a trigger fires from the top level, a
1901 ** new trigger context is entered for the duration of that one
1902 ** trigger. Subtriggers create subcontexts for their duration.
1903 **
1904 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
1905 ** not create a new trigger context.
1906 **
1907 ** ^This function returns the number of direct row changes in the
1908 ** most recent INSERT, UPDATE, or DELETE statement within the same
1909 ** trigger context.
1910 **
1911 ** ^Thus, when called from the top level, this function returns the
1912 ** number of changes in the most recent INSERT, UPDATE, or DELETE
1913 ** that also occurred at the top level. ^(Within the body of a trigger,
1914 ** the sqlite3_changes() interface can be called to find the number of
1915 ** changes in the most recently completed INSERT, UPDATE, or DELETE
1916 ** statement within the body of the same trigger.
1917 ** However, the number returned does not include changes
1918 ** caused by subtriggers since those have their own context.)^
1919 **
1920 ** See also the [sqlite3_total_changes()] interface, the
1921 ** [count_changes pragma], and the [changes() SQL function].
1922 **
1923 ** If a separate thread makes changes on the same database connection
@@ -1927,24 +1950,21 @@
1927 SQLITE_API int sqlite3_changes(sqlite3*);
1928
1929 /*
1930 ** CAPI3REF: Total Number Of Rows Modified
1931 **
1932 ** ^This function returns the number of row changes caused by [INSERT],
1933 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
1934 ** ^(The count returned by sqlite3_total_changes() includes all changes
1935 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
1936 ** [foreign key actions]. However,
1937 ** the count does not include changes used to implement [REPLACE] constraints,
1938 ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
1939 ** count does not include rows of views that fire an [INSTEAD OF trigger],
1940 ** though if the INSTEAD OF trigger makes changes of its own, those changes
1941 ** are counted.)^
1942 ** ^The sqlite3_total_changes() function counts the changes as soon as
1943 ** the statement that makes them is completed (when the statement handle
1944 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
1945 **
1946 ** See also the [sqlite3_changes()] interface, the
1947 ** [count_changes pragma], and the [total_changes() SQL function].
1948 **
1949 ** If a separate thread makes changes on the same database connection
1950 ** while [sqlite3_total_changes()] is running then the value
@@ -2418,17 +2438,18 @@
2418 ** already uses the largest possible [ROWID]. The PRNG is also used for
2419 ** the build-in random() and randomblob() SQL functions. This interface allows
2420 ** applications to access the same PRNG for other purposes.
2421 **
2422 ** ^A call to this routine stores N bytes of randomness into buffer P.
2423 ** ^If N is less than one, then P can be a NULL pointer.
2424 **
2425 ** ^If this routine has not been previously called or if the previous
2426 ** call had N less than one, then the PRNG is seeded using randomness
2427 ** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2428 ** ^If the previous call to this routine had an N of 1 or more then
2429 ** the pseudo-randomness is generated
 
2430 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2431 ** method.
2432 */
2433 SQLITE_API void sqlite3_randomness(int N, void *P);
2434
@@ -5638,31 +5659,47 @@
5638 ** in other words, the same BLOB that would be selected by:
5639 **
5640 ** <pre>
5641 ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5642 ** </pre>)^
 
 
 
 
 
 
5643 **
5644 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5645 ** and write access. ^If it is zero, the BLOB is opened for read access.
5646 ** ^It is not possible to open a column that is part of an index or primary
5647 ** key for writing. ^If [foreign key constraints] are enabled, it is
5648 ** not possible to open a column that is part of a [child key] for writing.
5649 **
5650 ** ^Note that the database name is not the filename that contains
5651 ** the database but rather the symbolic name of the database that
5652 ** appears after the AS keyword when the database is connected using [ATTACH].
5653 ** ^For the main database file, the database name is "main".
5654 ** ^For TEMP tables, the database name is "temp".
5655 **
5656 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5657 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5658 ** to be a null pointer.)^
5659 ** ^This function sets the [database connection] error code and message
5660 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5661 ** functions. ^Note that the *ppBlob variable is always initialized in a
5662 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5663 ** regardless of the success or failure of this routine.
 
 
 
 
 
 
 
 
 
 
5664 **
5665 ** ^(If the row that a BLOB handle points to is modified by an
5666 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5667 ** then the BLOB handle is marked as "expired".
5668 ** This is true if any column of the row is changed, even a column
@@ -5676,17 +5713,13 @@
5676 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5677 ** the opened blob. ^The size of a blob may not be changed by this
5678 ** interface. Use the [UPDATE] SQL command to change the size of a
5679 ** blob.
5680 **
5681 ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5682 ** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5683 **
5684 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5685 ** and the built-in [zeroblob] SQL function can be used, if desired,
5686 ** to create an empty, zero-filled blob in which to read or write using
5687 ** this interface.
5688 **
5689 ** To avoid a resource leak, every open [BLOB handle] should eventually
5690 ** be released by a call to [sqlite3_blob_close()].
5691 */
5692 SQLITE_API int sqlite3_blob_open(
@@ -5724,28 +5757,26 @@
5724 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5725
5726 /*
5727 ** CAPI3REF: Close A BLOB Handle
5728 **
5729 ** ^Closes an open [BLOB handle].
5730 **
5731 ** ^Closing a BLOB shall cause the current transaction to commit
5732 ** if there are no other BLOBs, no pending prepared statements, and the
5733 ** database connection is in [autocommit mode].
5734 ** ^If any writes were made to the BLOB, they might be held in cache
5735 ** until the close operation if they will fit.
5736 **
5737 ** ^(Closing the BLOB often forces the changes
5738 ** out to disk and so if any I/O errors occur, they will likely occur
5739 ** at the time when the BLOB is closed. Any errors that occur during
5740 ** closing are reported as a non-zero return value.)^
5741 **
5742 ** ^(The BLOB is closed unconditionally. Even if this routine returns
5743 ** an error code, the BLOB is still closed.)^
5744 **
5745 ** ^Calling this routine with a null pointer (such as would be returned
5746 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5747 */
5748 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5749
5750 /*
5751 ** CAPI3REF: Return The Size Of An Open BLOB
@@ -5791,36 +5822,39 @@
5791 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5792
5793 /*
5794 ** CAPI3REF: Write Data Into A BLOB Incrementally
5795 **
5796 ** ^This function is used to write data into an open [BLOB handle] from a
5797 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5798 ** into the open BLOB, starting at offset iOffset.
 
 
 
 
 
 
5799 **
5800 ** ^If the [BLOB handle] passed as the first argument was not opened for
5801 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5802 ** this function returns [SQLITE_READONLY].
5803 **
5804 ** ^This function may only modify the contents of the BLOB; it is
5805 ** not possible to increase the size of a BLOB using this API.
5806 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5807 ** [SQLITE_ERROR] is returned and no data is written. ^If N is
5808 ** less than zero [SQLITE_ERROR] is returned and no data is written.
5809 ** The size of the BLOB (and hence the maximum value of N+iOffset)
5810 ** can be determined using the [sqlite3_blob_bytes()] interface.
5811 **
5812 ** ^An attempt to write to an expired [BLOB handle] fails with an
5813 ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
5814 ** before the [BLOB handle] expired are not rolled back by the
5815 ** expiration of the handle, though of course those changes might
5816 ** have been overwritten by the statement that expired the BLOB handle
5817 ** or by other independent statements.
5818 **
5819 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5820 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5821 **
5822 ** This routine only works on a [BLOB handle] which has been created
5823 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5824 ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
5825 ** to this routine results in undefined and probably undesirable behavior.
5826 **
@@ -7409,10 +7443,102 @@
7409 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7410 #define SQLITE_FAIL 3
7411 /* #define SQLITE_ABORT 4 // Also an error code */
7412 #define SQLITE_REPLACE 5
7413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7414
7415
7416 /*
7417 ** Undo the hack that converts floating point types to integer for
7418 ** builds on processors without floating point support.
7419
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -55,11 +55,11 @@
55
56
57 /*
58 ** These no-op macros are used in front of interfaces to mark those
59 ** interfaces as either deprecated or experimental. New applications
60 ** should not use deprecated interfaces - they are supported for backwards
61 ** compatibility only. Application writers should be aware that
62 ** experimental interfaces are subject to change in point releases.
63 **
64 ** These macros used to resolve to various kinds of compiler magic that
65 ** would generate warning messages when they were used. But that
@@ -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.8"
111 #define SQLITE_VERSION_NUMBER 3008008
112 #define SQLITE_SOURCE_ID "2014-11-11 19:07:56 1412fcc480799ecbd68d44dd18d5bad40e20ccf1"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1502,29 +1502,31 @@
1502 ** it is not possible to set the Serialized [threading mode] and
1503 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1504 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1505 **
1506 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1507 ** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1508 ** a pointer to an instance of the [sqlite3_mem_methods] structure.
1509 ** The argument specifies
1510 ** alternative low-level memory allocation routines to be used in place of
1511 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1512 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1513 ** before the [sqlite3_config()] call returns.</dd>
1514 **
1515 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1516 ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1517 ** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1518 ** The [sqlite3_mem_methods]
1519 ** structure is filled with the currently defined memory allocation routines.)^
1520 ** This option can be used to overload the default memory allocation
1521 ** routines with a wrapper that simulations memory allocation failure or
1522 ** tracks memory usage, for example. </dd>
1523 **
1524 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1525 ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1526 ** interpreted as a boolean, which enables or disables the collection of
1527 ** memory allocation statistics. ^(When memory allocation statistics are disabled, the
1528 ** following SQLite interfaces become non-operational:
1529 ** <ul>
1530 ** <li> [sqlite3_memory_used()]
1531 ** <li> [sqlite3_memory_highwater()]
1532 ** <li> [sqlite3_soft_heap_limit64()]
@@ -1534,78 +1536,90 @@
1536 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1537 ** allocation statistics are disabled by default.
1538 ** </dd>
1539 **
1540 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1541 ** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
1542 ** that SQLite can use for scratch memory. ^(There are three arguments
1543 ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte
1544 ** aligned memory buffer from which the scratch allocations will be
1545 ** drawn, the size of each scratch allocation (sz),
1546 ** and the maximum number of scratch allocations (N).)^
 
1547 ** The first argument must be a pointer to an 8-byte aligned buffer
1548 ** of at least sz*N bytes of memory.
1549 ** ^SQLite will not use more than one scratch buffers per thread.
1550 ** ^SQLite will never request a scratch buffer that is more than 6
1551 ** times the database page size.
1552 ** ^If SQLite needs needs additional
1553 ** scratch memory beyond what is provided by this configuration option, then
1554 ** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
1555 ** ^When the application provides any amount of scratch memory using
1556 ** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
1557 ** [sqlite3_malloc|heap allocations].
1558 ** This can help [Robson proof|prevent memory allocation failures] due to heap
1559 ** fragmentation in low-memory embedded systems.
1560 ** </dd>
1561 **
1562 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1563 ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
1564 ** that SQLite can use for the database page cache with the default page
1565 ** cache implementation.
1566 ** This configuration should not be used if an application-define page
1567 ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
1568 ** configuration option.
1569 ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned
1570 ** memory, the size of each page buffer (sz), and the number of pages (N).
1571 ** The sz argument should be the size of the largest database page
1572 ** (a power of two between 512 and 32768) plus some extra bytes for each
1573 ** page header. ^The number of extra bytes needed by the page header
1574 ** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option
1575 ** to [sqlite3_config()].
1576 ** ^It is harmless, apart from the wasted memory,
1577 ** for the sz parameter to be larger than necessary. The first
1578 ** argument should pointer to an 8-byte aligned block of memory that
1579 ** is at least sz*N bytes of memory, otherwise subsequent behavior is
1580 ** undefined.
1581 ** ^SQLite will use the memory provided by the first argument to satisfy its
1582 ** memory needs for the first N pages that it adds to cache. ^If additional
1583 ** page cache memory is needed beyond what is provided by this option, then
1584 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
 
 
 
1585 **
1586 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1587 ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1588 ** that SQLite will use for all of its dynamic memory allocation needs
1589 ** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1590 ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1591 ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1592 ** [SQLITE_ERROR] if invoked otherwise.
1593 ** ^There are three arguments to SQLITE_CONFIG_HEAP:
1594 ** An 8-byte aligned pointer to the memory,
1595 ** the number of bytes in the memory buffer, and the minimum allocation size.
1596 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1597 ** to using its default memory allocator (the system malloc() implementation),
1598 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
1599 ** memory pointer is not NULL then the alternative memory
 
1600 ** allocator is engaged to handle all of SQLites memory allocation needs.
1601 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1602 ** boundary or subsequent behavior of SQLite will be undefined.
1603 ** The minimum allocation size is capped at 2**12. Reasonable values
1604 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1605 **
1606 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1607 ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1608 ** pointer to an instance of the [sqlite3_mutex_methods] structure.
1609 ** The argument specifies alternative low-level mutex routines to be used in place
1610 ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
1611 ** content of the [sqlite3_mutex_methods] structure before the call to
1612 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1613 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1614 ** the entire mutexing subsystem is omitted from the build and hence calls to
1615 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1616 ** return [SQLITE_ERROR].</dd>
1617 **
1618 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1619 ** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1620 ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
1621 ** [sqlite3_mutex_methods]
1622 ** structure is filled with the currently defined mutex routines.)^
1623 ** This option can be used to overload the default mutex allocation
1624 ** routines with a wrapper used to track mutex usage for performance
1625 ** profiling or testing, for example. ^If SQLite is compiled with
@@ -1613,28 +1627,28 @@
1627 ** the entire mutexing subsystem is omitted from the build and hence calls to
1628 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1629 ** return [SQLITE_ERROR].</dd>
1630 **
1631 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1632 ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1633 ** the default size of lookaside memory on each [database connection].
1634 ** The first argument is the
1635 ** size of each lookaside buffer slot and the second is the number of
1636 ** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
1637 ** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1638 ** option to [sqlite3_db_config()] can be used to change the lookaside
1639 ** configuration on individual connections.)^ </dd>
1640 **
1641 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1642 ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1643 ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
1644 ** the interface to a custom page cache implementation.)^
1645 ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
1646 **
1647 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1648 ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1649 ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current
1650 ** page cache implementation into that object.)^ </dd>
1651 **
1652 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1653 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1654 ** global [error log].
@@ -1654,26 +1668,27 @@
1668 ** supplied by the application must not invoke any SQLite interface.
1669 ** In a multi-threaded application, the application-defined logger
1670 ** function must be threadsafe. </dd>
1671 **
1672 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1673 ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1674 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1675 ** then URI handling is globally disabled.)^ ^If URI handling is globally enabled,
1676 ** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1677 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1678 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1679 ** connection is opened. ^If it is globally disabled, filenames are
1680 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1681 ** database connection is opened. ^(By default, URI handling is globally
1682 ** disabled. The default value may be changed by compiling with the
1683 ** [SQLITE_USE_URI] symbol defined.)^
1684 **
1685 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1686 ** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1687 ** argument which is interpreted as a boolean in order to enable or disable
1688 ** the use of covering indices for full table scans in the query optimizer.
1689 ** ^The default setting is determined
1690 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1691 ** if that compile-time option is omitted.
1692 ** The ability to disable the use of covering indices for full table scans
1693 ** is because some incorrectly coded legacy applications might malfunction
1694 ** when the optimization is enabled. Providing the ability to
@@ -1709,23 +1724,32 @@
1724 ** that are the default mmap size limit (the default setting for
1725 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1726 ** ^The default setting can be overridden by each database connection using
1727 ** either the [PRAGMA mmap_size] command, or by using the
1728 ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
1729 ** will be silently truncated if necessary so that it does not exceed the
1730 ** compile-time maximum mmap size set by the
1731 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1732 ** ^If either argument to this option is negative, then that argument is
1733 ** changed to its compile-time default.
1734 **
1735 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1736 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1737 ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1738 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1739 ** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1740 ** that specifies the maximum size of the created heap.
1741 ** </dl>
1742 **
1743 ** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1744 ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1745 ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1746 ** is a pointer to an integer and writes into that integer the number of extra
1747 ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of
1748 ** extra space required can change depending on the compiler,
1749 ** target platform, and SQLite version.
1750 ** </dl>
1751 */
1752 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1753 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1754 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
1755 #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
@@ -1746,10 +1770,11 @@
1770 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1771 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1772 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1773 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
1774 #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
1775 #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
1776
1777 /*
1778 ** CAPI3REF: Database Connection Configuration Options
1779 **
1780 ** These constants are the available integer configuration options that
@@ -1873,51 +1898,49 @@
1898 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
1899
1900 /*
1901 ** CAPI3REF: Count The Number Of Rows Modified
1902 **
1903 ** ^This function returns the number of rows modified, inserted or
1904 ** deleted by the most recently completed INSERT, UPDATE or DELETE
1905 ** statement on the database connection specified by the only parameter.
1906 ** ^Executing any other type of SQL statement does not modify the value
1907 ** returned by this function.
1908 **
1909 ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
1910 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
1911 ** [foreign key actions] or [REPLACE] constraint resolution are not counted.
1912 **
1913 ** Changes to a view that are intercepted by
1914 ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
1915 ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
1916 ** DELETE statement run on a view is always zero. Only changes made to real
1917 ** tables are counted.
1918 **
1919 ** Things are more complicated if the sqlite3_changes() function is
1920 ** executed while a trigger program is running. This may happen if the
1921 ** program uses the [changes() SQL function], or if some other callback
1922 ** function invokes sqlite3_changes() directly. Essentially:
1923 **
1924 ** <ul>
1925 ** <li> ^(Before entering a trigger program the value returned by
1926 ** sqlite3_changes() function is saved. After the trigger program
1927 ** has finished, the original value is restored.)^
1928 **
1929 ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
1930 ** statement sets the value returned by sqlite3_changes()
1931 ** upon completion as normal. Of course, this value will not include
1932 ** any changes performed by sub-triggers, as the sqlite3_changes()
1933 ** value will be saved and restored after each sub-trigger has run.)^
1934 ** </ul>
1935 **
1936 ** ^This means that if the changes() SQL function (or similar) is used
1937 ** by the first INSERT, UPDATE or DELETE statement within a trigger, it
1938 ** returns the value as set when the calling statement began executing.
1939 ** ^If it is used by the second or subsequent such statement within a trigger
1940 ** program, the value returned reflects the number of rows modified by the
1941 ** previous INSERT, UPDATE or DELETE statement within the same trigger.
 
 
1942 **
1943 ** See also the [sqlite3_total_changes()] interface, the
1944 ** [count_changes pragma], and the [changes() SQL function].
1945 **
1946 ** If a separate thread makes changes on the same database connection
@@ -1927,24 +1950,21 @@
1950 SQLITE_API int sqlite3_changes(sqlite3*);
1951
1952 /*
1953 ** CAPI3REF: Total Number Of Rows Modified
1954 **
1955 ** ^This function returns the total number of rows inserted, modified or
1956 ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
1957 ** since the database connection was opened, including those executed as
1958 ** part of trigger programs. ^Executing any other type of SQL statement
1959 ** does not affect the value returned by sqlite3_total_changes().
1960 **
1961 ** ^Changes made as part of [foreign key actions] are included in the
1962 ** count, but those made as part of REPLACE constraint resolution are
1963 ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
1964 ** are not counted.
1965 **
 
 
 
1966 ** See also the [sqlite3_changes()] interface, the
1967 ** [count_changes pragma], and the [total_changes() SQL function].
1968 **
1969 ** If a separate thread makes changes on the same database connection
1970 ** while [sqlite3_total_changes()] is running then the value
@@ -2418,17 +2438,18 @@
2438 ** already uses the largest possible [ROWID]. The PRNG is also used for
2439 ** the build-in random() and randomblob() SQL functions. This interface allows
2440 ** applications to access the same PRNG for other purposes.
2441 **
2442 ** ^A call to this routine stores N bytes of randomness into buffer P.
2443 ** ^The P parameter can be a NULL pointer.
2444 **
2445 ** ^If this routine has not been previously called or if the previous
2446 ** call had N less than one or a NULL pointer for P, then the PRNG is
2447 ** seeded using randomness obtained from the xRandomness method of
2448 ** the default [sqlite3_vfs] object.
2449 ** ^If the previous call to this routine had an N of 1 or more and a
2450 ** non-NULL P then the pseudo-randomness is generated
2451 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2452 ** method.
2453 */
2454 SQLITE_API void sqlite3_randomness(int N, void *P);
2455
@@ -5638,31 +5659,47 @@
5659 ** in other words, the same BLOB that would be selected by:
5660 **
5661 ** <pre>
5662 ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5663 ** </pre>)^
5664 **
5665 ** ^(Parameter zDb is not the filename that contains the database, but
5666 ** rather the symbolic name of the database. For attached databases, this is
5667 ** the name that appears after the AS keyword in the [ATTACH] statement.
5668 ** For the main database file, the database name is "main". For TEMP
5669 ** tables, the database name is "temp".)^
5670 **
5671 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5672 ** and write access. ^If the flags parameter is zero, the BLOB is opened for
5673 ** read-only access.
5674 **
5675 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
5676 ** in *ppBlob. Otherwise an [error code] is returned and, unless the error
5677 ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
5678 ** the API is not misused, it is always safe to call [sqlite3_blob_close()]
5679 ** on *ppBlob after this function it returns.
5680 **
5681 ** This function fails with SQLITE_ERROR if any of the following are true:
5682 ** <ul>
5683 ** <li> ^(Database zDb does not exist)^,
5684 ** <li> ^(Table zTable does not exist within database zDb)^,
5685 ** <li> ^(Table zTable is a WITHOUT ROWID table)^,
5686 ** <li> ^(Column zColumn does not exist)^,
5687 ** <li> ^(Row iRow is not present in the table)^,
5688 ** <li> ^(The specified column of row iRow contains a value that is not
5689 ** a TEXT or BLOB value)^,
5690 ** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
5691 ** constraint and the blob is being opened for read/write access)^,
5692 ** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
5693 ** column zColumn is part of a [child key] definition and the blob is
5694 ** being opened for read/write access)^.
5695 ** </ul>
5696 **
5697 ** ^Unless it returns SQLITE_MISUSE, this function sets the
5698 ** [database connection] error code and message accessible via
5699 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5700 **
5701 **
5702 ** ^(If the row that a BLOB handle points to is modified by an
5703 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5704 ** then the BLOB handle is marked as "expired".
5705 ** This is true if any column of the row is changed, even a column
@@ -5676,17 +5713,13 @@
5713 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5714 ** the opened blob. ^The size of a blob may not be changed by this
5715 ** interface. Use the [UPDATE] SQL command to change the size of a
5716 ** blob.
5717 **
 
 
 
5718 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5719 ** and the built-in [zeroblob] SQL function may be used to create a
5720 ** zero-filled blob to read or write using the incremental-blob interface.
 
5721 **
5722 ** To avoid a resource leak, every open [BLOB handle] should eventually
5723 ** be released by a call to [sqlite3_blob_close()].
5724 */
5725 SQLITE_API int sqlite3_blob_open(
@@ -5724,28 +5757,26 @@
5757 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5758
5759 /*
5760 ** CAPI3REF: Close A BLOB Handle
5761 **
5762 ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
5763 ** unconditionally. Even if this routine returns an error code, the
5764 ** handle is still closed.)^
5765 **
5766 ** ^If the blob handle being closed was opened for read-write access, and if
5767 ** the database is in auto-commit mode and there are no other open read-write
5768 ** blob handles or active write statements, the current transaction is
5769 ** committed. ^If an error occurs while committing the transaction, an error
5770 ** code is returned and the transaction rolled back.
5771 **
5772 ** Calling this function with an argument that is not a NULL pointer or an
5773 ** open blob handle results in undefined behaviour. ^Calling this routine
5774 ** with a null pointer (such as would be returned by a failed call to
5775 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
5776 ** is passed a valid open blob handle, the values returned by the
5777 ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
 
 
5778 */
5779 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5780
5781 /*
5782 ** CAPI3REF: Return The Size Of An Open BLOB
@@ -5791,36 +5822,39 @@
5822 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5823
5824 /*
5825 ** CAPI3REF: Write Data Into A BLOB Incrementally
5826 **
5827 ** ^(This function is used to write data into an open [BLOB handle] from a
5828 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
5829 ** into the open BLOB, starting at offset iOffset.)^
5830 **
5831 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5832 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5833 ** ^Unless SQLITE_MISUSE is returned, this function sets the
5834 ** [database connection] error code and message accessible via
5835 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
5836 **
5837 ** ^If the [BLOB handle] passed as the first argument was not opened for
5838 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5839 ** this function returns [SQLITE_READONLY].
5840 **
5841 ** This function may only modify the contents of the BLOB; it is
5842 ** not possible to increase the size of a BLOB using this API.
5843 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5844 ** [SQLITE_ERROR] is returned and no data is written. The size of the
5845 ** BLOB (and hence the maximum value of N+iOffset) can be determined
5846 ** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
5847 ** than zero [SQLITE_ERROR] is returned and no data is written.
5848 **
5849 ** ^An attempt to write to an expired [BLOB handle] fails with an
5850 ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
5851 ** before the [BLOB handle] expired are not rolled back by the
5852 ** expiration of the handle, though of course those changes might
5853 ** have been overwritten by the statement that expired the BLOB handle
5854 ** or by other independent statements.
5855 **
 
 
 
5856 ** This routine only works on a [BLOB handle] which has been created
5857 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5858 ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
5859 ** to this routine results in undefined and probably undesirable behavior.
5860 **
@@ -7409,10 +7443,102 @@
7443 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7444 #define SQLITE_FAIL 3
7445 /* #define SQLITE_ABORT 4 // Also an error code */
7446 #define SQLITE_REPLACE 5
7447
7448 /*
7449 ** CAPI3REF: Prepared Statement Scan Status Opcodes
7450 ** KEYWORDS: {scanstatus options}
7451 **
7452 ** The following constants can be used for the T parameter to the
7453 ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
7454 ** different metric for sqlite3_stmt_scanstatus() to return.
7455 **
7456 ** <dl>
7457 ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
7458 ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7459 ** total number of times that the X-th loop has run.</dd>
7460 **
7461 ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
7462 ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the
7463 ** total number of rows examined by all iterations of the X-th loop.</dd>
7464 **
7465 ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
7466 ** <dd>^The "double" variable pointed to by the T parameter will be set to the
7467 ** query planner's estimate for the average number of rows output from each
7468 ** iteration of the X-th loop. If the query planner's estimates was accurate,
7469 ** then this value will approximate the quotient NVISIT/NLOOP and the
7470 ** product of this value for all prior loops with the same SELECTID will
7471 ** be the NLOOP value for the current loop.
7472 **
7473 ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
7474 ** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7475 ** a zero-terminated UTF-8 string containing the name of the index or table used
7476 ** for the X-th loop.
7477 **
7478 ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
7479 ** <dd>^The "const char *" variable pointed to by the T parameter will be set to
7480 ** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description
7481 ** for the X-th loop.
7482 **
7483 ** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
7484 ** <dd>^The "int" variable pointed to by the T parameter will be set to the
7485 ** "select-id" for the X-th loop. The select-id identifies which query or
7486 ** subquery the loop is part of. The main query has a select-id of zero.
7487 ** The select-id is the same value as is output in the first column
7488 ** of an [EXPLAIN QUERY PLAN] query.
7489 ** </dl>
7490 */
7491 #define SQLITE_SCANSTAT_NLOOP 0
7492 #define SQLITE_SCANSTAT_NVISIT 1
7493 #define SQLITE_SCANSTAT_EST 2
7494 #define SQLITE_SCANSTAT_NAME 3
7495 #define SQLITE_SCANSTAT_EXPLAIN 4
7496 #define SQLITE_SCANSTAT_SELECTID 5
7497
7498 /*
7499 ** CAPI3REF: Prepared Statement Scan Status
7500 **
7501 ** Return status data for a single loop within query pStmt.
7502 **
7503 ** The "iScanStatusOp" parameter determines which status information to return.
7504 ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of
7505 ** this interface is undefined.
7506 ** ^The requested measurement is written into a variable pointed to by
7507 ** the "pOut" parameter.
7508 ** Parameter "idx" identifies the specific loop to retrieve statistics for.
7509 ** Loops are numbered starting from zero. ^If idx is out of range - less than
7510 ** zero or greater than or equal to the total number of loops used to implement
7511 ** the statement - a non-zero value is returned and the variable that pOut
7512 ** points to is unchanged.
7513 **
7514 ** ^Statistics might not be available for all loops in all statements. ^In cases
7515 ** where there exist loops with no available statistics, this function behaves
7516 ** as if the loop did not exist - it returns non-zero and leave the variable
7517 ** that pOut points to unchanged.
7518 **
7519 ** This API is only available if the library is built with pre-processor
7520 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7521 **
7522 ** See also: [sqlite3_stmt_scanstatus_reset()]
7523 */
7524 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
7525 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7526 int idx, /* Index of loop to report on */
7527 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7528 void *pOut /* Result written here */
7529 );
7530
7531 /*
7532 ** CAPI3REF: Zero Scan-Status Counters
7533 **
7534 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7535 **
7536 ** This API is only available if the library is built with pre-processor
7537 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7538 */
7539 SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7540
7541
7542 /*
7543 ** Undo the hack that converts floating point types to integer for
7544 ** builds on processors without floating point support.
7545

Keyboard Shortcuts

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