Fossil SCM

Update the built-in SQLite to the latest 3.40.0 alpha.

drh 2022-09-05 14:03 trunk
Commit 9d12e96440159365e917a2b6a04c48346004a04fd6a455f576dcd607cff6fd2c
3 files changed +232 -88 +1008 -640 +16 -8
+232 -88
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -53,10 +53,19 @@
5353
*/
5454
#if !defined(SQLITE_OS_WINRT)
5555
# define SQLITE_OS_WINRT 0
5656
#endif
5757
58
+/*
59
+** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
60
+** somewhat for use as a WASM module in a web browser. This flag
61
+** should only be used when building the "fiddle" web application, as
62
+** the browser-mode build has much different user input requirements
63
+** and this build mode rewires the user input subsystem to account for
64
+** that.
65
+*/
66
+
5867
/*
5968
** Warning pragmas copied from msvc.h in the core.
6069
*/
6170
#if defined(_MSC_VER)
6271
#pragma warning(disable : 4054)
@@ -245,21 +254,10 @@
245254
#else
246255
# define setBinaryMode(X,Y)
247256
# define setTextMode(X,Y)
248257
#endif
249258
250
-/*
251
-** When compiling with emcc (a.k.a. emscripten), we're building a
252
-** WebAssembly (WASM) bundle and need to disable and rewire a few
253
-** things.
254
-*/
255
-#ifdef __EMSCRIPTEN__
256
-#define SQLITE_SHELL_WASM_MODE
257
-#else
258
-#undef SQLITE_SHELL_WASM_MODE
259
-#endif
260
-
261259
/* True if the timer is enabled */
262260
static int enableTimer = 0;
263261
264262
/* Return the current wall-clock time */
265263
static sqlite3_int64 timeOfDay(void){
@@ -717,11 +715,11 @@
717715
**
718716
** The result is stored in space obtained from malloc() and must either
719717
** be freed by the caller or else passed back into this routine via the
720718
** zPrior argument for reuse.
721719
*/
722
-#ifndef SQLITE_SHELL_WASM_MODE
720
+#ifndef SQLITE_SHELL_FIDDLE
723721
static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
724722
char *zPrompt;
725723
char *zResult;
726724
if( in!=0 ){
727725
zResult = local_getline(zPrior, in);
@@ -737,11 +735,11 @@
737735
if( zResult && *zResult ) shell_add_history(zResult);
738736
#endif
739737
}
740738
return zResult;
741739
}
742
-#endif /* !SQLITE_SHELL_WASM_MODE */
740
+#endif /* !SQLITE_SHELL_FIDDLE */
743741
744742
/*
745743
** Return the value of a hexadecimal digit. Return -1 if the input
746744
** is not a hex digit.
747745
*/
@@ -3794,10 +3792,11 @@
37943792
#define re_compile sqlite3re_compile
37953793
#define re_free sqlite3re_free
37963794
37973795
/* The end-of-input character */
37983796
#define RE_EOF 0 /* End of input */
3797
+#define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
37993798
38003799
/* The NFA is implemented as sequence of opcodes taken from the following
38013800
** set. Each opcode has a single integer argument.
38023801
*/
38033802
#define RE_OP_MATCH 1 /* Match the one character in the argument */
@@ -3815,10 +3814,37 @@
38153814
#define RE_OP_DIGIT 13 /* digit: [0-9] */
38163815
#define RE_OP_NOTDIGIT 14 /* Not a digit */
38173816
#define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
38183817
#define RE_OP_NOTSPACE 16 /* Not a digit */
38193818
#define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
3819
+#define RE_OP_ATSTART 18 /* Currently at the start of the string */
3820
+
3821
+#if defined(SQLITE_DEBUG)
3822
+/* Opcode names used for symbolic debugging */
3823
+static const char *ReOpName[] = {
3824
+ "EOF",
3825
+ "MATCH",
3826
+ "ANY",
3827
+ "ANYSTAR",
3828
+ "FORK",
3829
+ "GOTO",
3830
+ "ACCEPT",
3831
+ "CC_INC",
3832
+ "CC_EXC",
3833
+ "CC_VALUE",
3834
+ "CC_RANGE",
3835
+ "WORD",
3836
+ "NOTWORD",
3837
+ "DIGIT",
3838
+ "NOTDIGIT",
3839
+ "SPACE",
3840
+ "NOTSPACE",
3841
+ "BOUNDARY",
3842
+ "ATSTART",
3843
+};
3844
+#endif /* SQLITE_DEBUG */
3845
+
38203846
38213847
/* Each opcode is a "state" in the NFA */
38223848
typedef unsigned short ReStateNumber;
38233849
38243850
/* Because this is an NFA and not a DFA, multiple states can be active at
@@ -3849,11 +3875,11 @@
38493875
const char *zErr; /* Error message to return */
38503876
char *aOp; /* Operators for the virtual machine */
38513877
int *aArg; /* Arguments to each operator */
38523878
unsigned (*xNextChar)(ReInput*); /* Next character function */
38533879
unsigned char zInit[12]; /* Initial text to match */
3854
- int nInit; /* Number of characters in zInit */
3880
+ int nInit; /* Number of bytes in zInit */
38553881
unsigned nState; /* Number of entries in aOp[] and aArg[] */
38563882
unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
38573883
};
38583884
38593885
/* Add a state to the given state set if it is not already there */
@@ -3922,11 +3948,11 @@
39223948
ReStateSet aStateSet[2], *pThis, *pNext;
39233949
ReStateNumber aSpace[100];
39243950
ReStateNumber *pToFree;
39253951
unsigned int i = 0;
39263952
unsigned int iSwap = 0;
3927
- int c = RE_EOF+1;
3953
+ int c = RE_START;
39283954
int cPrev = 0;
39293955
int rc = 0;
39303956
ReInput in;
39313957
39323958
in.z = zIn;
@@ -3941,10 +3967,11 @@
39413967
strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
39423968
){
39433969
in.i++;
39443970
}
39453971
if( in.i+pRe->nInit>in.mx ) return 0;
3972
+ c = RE_START-1;
39463973
}
39473974
39483975
if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
39493976
pToFree = 0;
39503977
aStateSet[0].aState = aSpace;
@@ -3968,10 +3995,14 @@
39683995
int x = pThis->aState[i];
39693996
switch( pRe->aOp[x] ){
39703997
case RE_OP_MATCH: {
39713998
if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
39723999
break;
4000
+ }
4001
+ case RE_OP_ATSTART: {
4002
+ if( cPrev==RE_START ) re_add_state(pThis, x+1);
4003
+ break;
39734004
}
39744005
case RE_OP_ANY: {
39754006
if( c!=0 ) re_add_state(pNext, x+1);
39764007
break;
39774008
}
@@ -4050,11 +4081,13 @@
40504081
}
40514082
}
40524083
}
40534084
}
40544085
for(i=0; i<pNext->nState; i++){
4055
- if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
4086
+ int x = pNext->aState[i];
4087
+ while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x];
4088
+ if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; }
40564089
}
40574090
re_match_end:
40584091
sqlite3_free(pToFree);
40594092
return rc;
40604093
}
@@ -4205,11 +4238,10 @@
42054238
const char *zErr;
42064239
while( (c = p->xNextChar(&p->sIn))!=0 ){
42074240
iStart = p->nState;
42084241
switch( c ){
42094242
case '|':
4210
- case '$':
42114243
case ')': {
42124244
p->sIn.i--;
42134245
return 0;
42144246
}
42154247
case '(': {
@@ -4241,10 +4273,18 @@
42414273
}
42424274
case '?': {
42434275
if( iPrev<0 ) return "'?' without operand";
42444276
re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
42454277
break;
4278
+ }
4279
+ case '$': {
4280
+ re_append(p, RE_OP_MATCH, RE_EOF);
4281
+ break;
4282
+ }
4283
+ case '^': {
4284
+ re_append(p, RE_OP_ATSTART, 0);
4285
+ break;
42464286
}
42474287
case '{': {
42484288
int m = 0, n = 0;
42494289
int sz, j;
42504290
if( iPrev<0 ) return "'{m,n}' without operand";
@@ -4260,10 +4300,11 @@
42604300
p->sIn.i++;
42614301
sz = p->nState - iPrev;
42624302
if( m==0 ){
42634303
if( n==0 ) return "both m and n are zero in '{m,n}'";
42644304
re_insert(p, iPrev, RE_OP_FORK, sz+1);
4305
+ iPrev++;
42654306
n--;
42664307
}else{
42674308
for(j=1; j<m; j++) re_copy(p, iPrev, sz);
42684309
}
42694310
for(j=m; j<n; j++){
@@ -4378,15 +4419,11 @@
43784419
zErr = re_subcompile_re(pRe);
43794420
if( zErr ){
43804421
re_free(pRe);
43814422
return zErr;
43824423
}
4383
- if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
4384
- re_append(pRe, RE_OP_MATCH, RE_EOF);
4385
- re_append(pRe, RE_OP_ACCEPT, 0);
4386
- *ppRe = pRe;
4387
- }else if( pRe->sIn.i>=pRe->sIn.mx ){
4424
+ if( pRe->sIn.i>=pRe->sIn.mx ){
43884425
re_append(pRe, RE_OP_ACCEPT, 0);
43894426
*ppRe = pRe;
43904427
}else{
43914428
re_free(pRe);
43924429
return "unrecognized character";
@@ -4465,10 +4502,71 @@
44654502
}
44664503
if( setAux ){
44674504
sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
44684505
}
44694506
}
4507
+
4508
+#if defined(SQLITE_DEBUG)
4509
+/*
4510
+** This function is used for testing and debugging only. It is only available
4511
+** if the SQLITE_DEBUG compile-time option is used.
4512
+**
4513
+** Compile a regular expression and then convert the compiled expression into
4514
+** text and return that text.
4515
+*/
4516
+static void re_bytecode_func(
4517
+ sqlite3_context *context,
4518
+ int argc,
4519
+ sqlite3_value **argv
4520
+){
4521
+ const char *zPattern;
4522
+ const char *zErr;
4523
+ ReCompiled *pRe;
4524
+ sqlite3_str *pStr;
4525
+ int i;
4526
+ int n;
4527
+ char *z;
4528
+
4529
+ zPattern = (const char*)sqlite3_value_text(argv[0]);
4530
+ if( zPattern==0 ) return;
4531
+ zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
4532
+ if( zErr ){
4533
+ re_free(pRe);
4534
+ sqlite3_result_error(context, zErr, -1);
4535
+ return;
4536
+ }
4537
+ if( pRe==0 ){
4538
+ sqlite3_result_error_nomem(context);
4539
+ return;
4540
+ }
4541
+ pStr = sqlite3_str_new(0);
4542
+ if( pStr==0 ) goto re_bytecode_func_err;
4543
+ if( pRe->nInit>0 ){
4544
+ sqlite3_str_appendf(pStr, "INIT ");
4545
+ for(i=0; i<pRe->nInit; i++){
4546
+ sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]);
4547
+ }
4548
+ sqlite3_str_appendf(pStr, "\n");
4549
+ }
4550
+ for(i=0; (unsigned)i<pRe->nState; i++){
4551
+ sqlite3_str_appendf(pStr, "%-8s %4d\n",
4552
+ ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]);
4553
+ }
4554
+ n = sqlite3_str_length(pStr);
4555
+ z = sqlite3_str_finish(pStr);
4556
+ if( n==0 ){
4557
+ sqlite3_free(z);
4558
+ }else{
4559
+ sqlite3_result_text(context, z, n-1, sqlite3_free);
4560
+ }
4561
+
4562
+re_bytecode_func_err:
4563
+ re_free(pRe);
4564
+}
4565
+
4566
+#endif /* SQLITE_DEBUG */
4567
+
44704568
44714569
/*
44724570
** Invoke this routine to register the regexp() function with the
44734571
** SQLite database connection.
44744572
*/
@@ -4490,16 +4588,23 @@
44904588
/* The regexpi(PATTERN,STRING) function is a case-insensitive version
44914589
** of regexp(PATTERN,STRING). */
44924590
rc = sqlite3_create_function(db, "regexpi", 2,
44934591
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
44944592
(void*)db, re_sql_func, 0, 0);
4593
+#if defined(SQLITE_DEBUG)
4594
+ if( rc==SQLITE_OK ){
4595
+ rc = sqlite3_create_function(db, "regexp_bytecode", 1,
4596
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4597
+ 0, re_bytecode_func, 0, 0);
4598
+ }
4599
+#endif /* SQLITE_DEBUG */
44954600
}
44964601
return rc;
44974602
}
44984603
44994604
/************************* End ../ext/misc/regexp.c ********************/
4500
-#ifndef SQLITE_SHELL_WASM_MODE
4605
+#ifndef SQLITE_SHELL_FIDDLE
45014606
/************************* Begin ../ext/misc/fileio.c ******************/
45024607
/*
45034608
** 2014-06-13
45044609
**
45054610
** The author disclaims copyright to this source code. In place of
@@ -10047,10 +10152,14 @@
1004710152
** Return true if zId must be quoted in order to use it as an SQL
1004810153
** identifier, or false otherwise.
1004910154
*/
1005010155
static int idxIdentifierRequiresQuotes(const char *zId){
1005110156
int i;
10157
+ int nId = STRLEN(zId);
10158
+
10159
+ if( sqlite3_keyword_check(zId, nId) ) return 1;
10160
+
1005210161
for(i=0; zId[i]; i++){
1005310162
if( !(zId[i]=='_')
1005410163
&& !(zId[i]>='0' && zId[i]<='9')
1005510164
&& !(zId[i]>='a' && zId[i]<='z')
1005610165
&& !(zId[i]>='A' && zId[i]<='Z')
@@ -12248,19 +12357,19 @@
1224812357
int nIndent; /* Size of array aiIndent[] */
1224912358
int iIndent; /* Index of current op in aiIndent[] */
1225012359
char *zNonce; /* Nonce for temporary safe-mode excapes */
1225112360
EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
1225212361
ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12253
-#ifdef SQLITE_SHELL_WASM_MODE
12362
+#ifdef SQLITE_SHELL_FIDDLE
1225412363
struct {
1225512364
const char * zInput; /* Input string from wasm/JS proxy */
1225612365
const char * zPos; /* Cursor pos into zInput */
1225712366
} wasm;
1225812367
#endif
1225912368
};
1226012369
12261
-#ifdef SQLITE_SHELL_WASM_MODE
12370
+#ifdef SQLITE_SHELL_FIDDLE
1226212371
static ShellState shellState;
1226312372
#endif
1226412373
1226512374
1226612375
/* Allowed values for ShellState.autoEQP
@@ -12588,14 +12697,27 @@
1258812697
/*
1258912698
** Output the given string as a hex-encoded blob (eg. X'1234' )
1259012699
*/
1259112700
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1259212701
int i;
12593
- char *zBlob = (char *)pBlob;
12594
- raw_printf(out,"X'");
12595
- for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12596
- raw_printf(out,"'");
12702
+ unsigned char *aBlob = (unsigned char*)pBlob;
12703
+
12704
+ char *zStr = sqlite3_malloc(nBlob*2 + 1);
12705
+ shell_check_oom(zStr);
12706
+
12707
+ for(i=0; i<nBlob; i++){
12708
+ static const char aHex[] = {
12709
+ '0', '1', '2', '3', '4', '5', '6', '7',
12710
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
12711
+ };
12712
+ zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
12713
+ zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
12714
+ }
12715
+ zStr[i*2] = '\0';
12716
+
12717
+ raw_printf(out,"X'%s'", zStr);
12718
+ sqlite3_free(zStr);
1259712719
}
1259812720
1259912721
/*
1260012722
** Find a string that is not found anywhere in z[]. Return a pointer
1260112723
** to that string.
@@ -12924,11 +13046,11 @@
1292413046
UNUSED_PARAMETER(zA2);
1292513047
UNUSED_PARAMETER(zA3);
1292613048
UNUSED_PARAMETER(zA4);
1292713049
switch( op ){
1292813050
case SQLITE_ATTACH: {
12929
-#ifndef SQLITE_SHELL_WASM_MODE
13051
+#ifndef SQLITE_SHELL_FIDDLE
1293013052
/* In WASM builds the filesystem is a virtual sandbox, so
1293113053
** there's no harm in using ATTACH. */
1293213054
failIfSafeMode(p, "cannot run ATTACH in safe mode");
1293313055
#endif
1293413056
break;
@@ -12997,19 +13119,41 @@
1299713119
/*
1299813120
** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
1299913121
**
1300013122
** This routine converts some CREATE TABLE statements for shadow tables
1300113123
** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
13124
+**
13125
+** If the schema statement in z[] contains a start-of-comment and if
13126
+** sqlite3_complete() returns false, try to terminate the comment before
13127
+** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
1300213128
*/
1300313129
static void printSchemaLine(FILE *out, const char *z, const char *zTail){
13130
+ char *zToFree = 0;
1300413131
if( z==0 ) return;
1300513132
if( zTail==0 ) return;
13133
+ if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
13134
+ const char *zOrig = z;
13135
+ static const char *azTerm[] = { "", "*/", "\n" };
13136
+ int i;
13137
+ for(i=0; i<ArraySize(azTerm); i++){
13138
+ char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
13139
+ if( sqlite3_complete(zNew) ){
13140
+ size_t n = strlen(zNew);
13141
+ zNew[n-1] = 0;
13142
+ zToFree = zNew;
13143
+ z = zNew;
13144
+ break;
13145
+ }
13146
+ sqlite3_free(zNew);
13147
+ }
13148
+ }
1300613149
if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1300713150
utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1300813151
}else{
1300913152
utf8_printf(out, "%s%s", z, zTail);
1301013153
}
13154
+ sqlite3_free(zToFree);
1301113155
}
1301213156
static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1301313157
char c = z[n];
1301413158
z[n] = 0;
1301513159
printSchemaLine(out, z, zTail);
@@ -15338,11 +15482,11 @@
1533815482
** There must be two or more spaces between the end of the command and the
1533915483
** start of the description of what that command does.
1534015484
*/
1534115485
static const char *(azHelp[]) = {
1534215486
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
15343
- && !defined(SQLITE_SHELL_WASM_MODE)
15487
+ && !defined(SQLITE_SHELL_FIDDLE)
1534415488
".archive ... Manage SQL archives",
1534515489
" Each command must have exactly one of the following options:",
1534615490
" -c, --create Create a new archive",
1534715491
" -u, --update Add or update files with changed mtime",
1534815492
" -i, --insert Like -u but always add even if unchanged",
@@ -15364,23 +15508,23 @@
1536415508
" http://sqlite.org/cli.html#sqlite_archive_support",
1536515509
#endif
1536615510
#ifndef SQLITE_OMIT_AUTHORIZATION
1536715511
".auth ON|OFF Show authorizer callbacks",
1536815512
#endif
15369
-#ifndef SQLITE_SHELL_WASM_MODE
15513
+#ifndef SQLITE_SHELL_FIDDLE
1537015514
".backup ?DB? FILE Backup DB (default \"main\") to FILE",
1537115515
" Options:",
1537215516
" --append Use the appendvfs",
1537315517
" --async Write to FILE without journal and fsync()",
1537415518
#endif
1537515519
".bail on|off Stop after hitting an error. Default OFF",
1537615520
".binary on|off Turn binary output on or off. Default OFF",
15377
-#ifndef SQLITE_SHELL_WASM_MODE
15521
+#ifndef SQLITE_SHELL_FIDDLE
1537815522
".cd DIRECTORY Change the working directory to DIRECTORY",
1537915523
#endif
1538015524
".changes on|off Show number of rows changed by SQL",
15381
-#ifndef SQLITE_SHELL_WASM_MODE
15525
+#ifndef SQLITE_SHELL_FIDDLE
1538215526
".check GLOB Fail if output since .testcase does not match",
1538315527
".clone NEWDB Clone data into NEWDB from the existing database",
1538415528
#endif
1538515529
".connection [close] [#] Open or close an auxiliary database connection",
1538615530
".databases List names and files of attached databases",
@@ -15402,15 +15546,15 @@
1540215546
#ifdef SQLITE_DEBUG
1540315547
" test Show raw EXPLAIN QUERY PLAN output",
1540415548
" trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
1540515549
#endif
1540615550
" trigger Like \"full\" but also show trigger bytecode",
15407
-#ifndef SQLITE_SHELL_WASM_MODE
15551
+#ifndef SQLITE_SHELL_FIDDLE
1540815552
".excel Display the output of next command in spreadsheet",
1540915553
" --bom Put a UTF8 byte-order mark on intermediate file",
1541015554
#endif
15411
-#ifndef SQLITE_SHELL_WASM_MODE
15555
+#ifndef SQLITE_SHELL_FIDDLE
1541215556
".exit ?CODE? Exit this program with return-code CODE",
1541315557
#endif
1541415558
".expert EXPERIMENTAL. Suggest indexes for queries",
1541515559
".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
1541615560
".filectrl CMD ... Run various sqlite3_file_control() operations",
@@ -15417,11 +15561,11 @@
1541715561
" --schema SCHEMA Use SCHEMA instead of \"main\"",
1541815562
" --help Show CMD details",
1541915563
".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
1542015564
".headers on|off Turn display of headers on or off",
1542115565
".help ?-all? ?PATTERN? Show help text for PATTERN",
15422
-#ifndef SQLITE_SHELL_WASM_MODE
15566
+#ifndef SQLITE_SHELL_FIDDLE
1542315567
".import FILE TABLE Import data from FILE into TABLE",
1542415568
" Options:",
1542515569
" --ascii Use \\037 and \\036 as column and row separators",
1542615570
" --csv Use , and \\n as column and row separators",
1542715571
" --skip N Skip the first N rows of input",
@@ -15446,14 +15590,14 @@
1544615590
#endif
1544715591
".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
1544815592
".lint OPTIONS Report potential schema issues.",
1544915593
" Options:",
1545015594
" fkey-indexes Find missing foreign key indexes",
15451
-#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
15595
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
1545215596
".load FILE ?ENTRY? Load an extension library",
1545315597
#endif
15454
-#ifndef SQLITE_SHELL_WASM_MODE
15598
+#ifndef SQLITE_SHELL_FIDDLE
1545515599
".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
1545615600
#endif
1545715601
".mode MODE ?OPTIONS? Set output mode",
1545815602
" MODE is one of:",
1545915603
" ascii Columns/rows delimited by 0x1F and 0x1E",
@@ -15476,15 +15620,15 @@
1547615620
" --wordwrap B Wrap or not at word boundaries per B (on/off)",
1547715621
" --ww Shorthand for \"--wordwrap 1\"",
1547815622
" --quote Quote output text as SQL literals",
1547915623
" --noquote Do not quote output text",
1548015624
" TABLE The name of SQL table used for \"insert\" mode",
15481
-#ifndef SQLITE_SHELL_WASM_MODE
15625
+#ifndef SQLITE_SHELL_FIDDLE
1548215626
".nonce STRING Suspend safe mode for one command if nonce matches",
1548315627
#endif
1548415628
".nullvalue STRING Use STRING in place of NULL values",
15485
-#ifndef SQLITE_SHELL_WASM_MODE
15629
+#ifndef SQLITE_SHELL_FIDDLE
1548615630
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
1548715631
" If FILE begins with '|' then open as a pipe",
1548815632
" --bom Put a UTF8 byte-order mark at the beginning",
1548915633
" -e Send output to the system text editor",
1549015634
" -x Send output as CSV to a spreadsheet (same as \".excel\")",
@@ -15502,11 +15646,11 @@
1550215646
#endif
1550315647
" --new Initialize FILE to an empty database",
1550415648
" --nofollow Do not follow symbolic links",
1550515649
" --readonly Open FILE readonly",
1550615650
" --zip FILE is a ZIP archive",
15507
-#ifndef SQLITE_SHELL_WASM_MODE
15651
+#ifndef SQLITE_SHELL_FIDDLE
1550815652
".output ?FILE? Send output to FILE or stdout if FILE is omitted",
1550915653
" If FILE begins with '|' then open it as a pipe.",
1551015654
" Options:",
1551115655
" --bom Prefix output with a UTF8 byte-order mark",
1551215656
" -e Send output to the system text editor",
@@ -15526,11 +15670,11 @@
1552615670
" --once Do no more than one progress interrupt",
1552715671
" --quiet|-q No output except at interrupts",
1552815672
" --reset Reset the count for each input and interrupt",
1552915673
#endif
1553015674
".prompt MAIN CONTINUE Replace the standard prompts",
15531
-#ifndef SQLITE_SHELL_WASM_MODE
15675
+#ifndef SQLITE_SHELL_FIDDLE
1553215676
".quit Exit this program",
1553315677
".read FILE Read input from FILE or command output",
1553415678
" If FILE begins with \"|\", it is a command that generates the input.",
1553515679
#endif
1553615680
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
@@ -15539,11 +15683,11 @@
1553915683
" --recovery-db NAME Store recovery metadata in database file NAME",
1554015684
" --lost-and-found TABLE Alternative name for the lost-and-found table",
1554115685
" --no-rowids Do not attempt to recover rowid values",
1554215686
" that are not also INTEGER PRIMARY KEYs",
1554315687
#endif
15544
-#ifndef SQLITE_SHELL_WASM_MODE
15688
+#ifndef SQLITE_SHELL_FIDDLE
1554515689
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
1554615690
".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
1554715691
#endif
1554815692
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
1554915693
".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15576,24 +15720,24 @@
1557615720
" --sha3-224 Use the sha3-224 algorithm",
1557715721
" --sha3-256 Use the sha3-256 algorithm (default)",
1557815722
" --sha3-384 Use the sha3-384 algorithm",
1557915723
" --sha3-512 Use the sha3-512 algorithm",
1558015724
" Any other argument is a LIKE pattern for tables to hash",
15581
-#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15725
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
1558215726
".shell CMD ARGS... Run CMD ARGS... in a system shell",
1558315727
#endif
1558415728
".show Show the current values for various settings",
1558515729
".stats ?ARG? Show stats or turn stats on or off",
1558615730
" off Turn off automatic stat display",
1558715731
" on Turn on automatic stat display",
1558815732
" stmt Show statement stats",
1558915733
" vmstep Show the virtual machine step count only",
15590
-#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15734
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
1559115735
".system CMD ARGS... Run CMD ARGS... in a system shell",
1559215736
#endif
1559315737
".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15594
-#ifndef SQLITE_SHELL_WASM_MODE
15738
+#ifndef SQLITE_SHELL_FIDDLE
1559515739
".testcase NAME Begin redirecting output to 'testcase-out.txt'",
1559615740
#endif
1559715741
".testctrl CMD ... Run various sqlite3_test_control() operations",
1559815742
" Run \".testctrl\" with no arguments for details",
1559915743
".timeout MS Try opening locked tables for MS milliseconds",
@@ -16142,11 +16286,11 @@
1614216286
sqlite3_uint_init(p->db, 0, 0);
1614316287
sqlite3_decimal_init(p->db, 0, 0);
1614416288
sqlite3_regexp_init(p->db, 0, 0);
1614516289
sqlite3_ieee_init(p->db, 0, 0);
1614616290
sqlite3_series_init(p->db, 0, 0);
16147
-#ifndef SQLITE_SHELL_WASM_MODE
16291
+#ifndef SQLITE_SHELL_FIDDLE
1614816292
sqlite3_fileio_init(p->db, 0, 0);
1614916293
sqlite3_completion_init(p->db, 0, 0);
1615016294
#endif
1615116295
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1615216296
sqlite3_dbdata_init(p->db, 0, 0);
@@ -19272,19 +19416,19 @@
1927219416
}
1927319417
}else
1927419418
#endif
1927519419
1927619420
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19277
- && !defined(SQLITE_SHELL_WASM_MODE)
19421
+ && !defined(SQLITE_SHELL_FIDDLE)
1927819422
if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
1927919423
open_db(p, 0);
1928019424
failIfSafeMode(p, "cannot run .archive in safe mode");
1928119425
rc = arDotCommand(p, 0, azArg, nArg);
1928219426
}else
1928319427
#endif
1928419428
19285
-#ifndef SQLITE_SHELL_WASM_MODE
19429
+#ifndef SQLITE_SHELL_FIDDLE
1928619430
if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
1928719431
|| (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
1928819432
){
1928919433
const char *zDestFile = 0;
1929019434
const char *zDb = 0;
@@ -19349,11 +19493,11 @@
1934919493
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
1935019494
rc = 1;
1935119495
}
1935219496
close_db(pDest);
1935319497
}else
19354
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19498
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1935519499
1935619500
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
1935719501
if( nArg==2 ){
1935819502
bail_on_error = booleanValue(azArg[1]);
1935919503
}else{
@@ -19380,11 +19524,11 @@
1938019524
*/
1938119525
if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
1938219526
test_breakpoint();
1938319527
}else
1938419528
19385
-#ifndef SQLITE_SHELL_WASM_MODE
19529
+#ifndef SQLITE_SHELL_FIDDLE
1938619530
if( c=='c' && strcmp(azArg[0],"cd")==0 ){
1938719531
failIfSafeMode(p, "cannot run .cd in safe mode");
1938819532
if( nArg==2 ){
1938919533
#if defined(_WIN32) || defined(WIN32)
1939019534
wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19400,11 +19544,11 @@
1940019544
}else{
1940119545
raw_printf(stderr, "Usage: .cd DIRECTORY\n");
1940219546
rc = 1;
1940319547
}
1940419548
}else
19405
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19549
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1940619550
1940719551
if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
1940819552
if( nArg==2 ){
1940919553
setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
1941019554
}else{
@@ -19411,11 +19555,11 @@
1941119555
raw_printf(stderr, "Usage: .changes on|off\n");
1941219556
rc = 1;
1941319557
}
1941419558
}else
1941519559
19416
-#ifndef SQLITE_SHELL_WASM_MODE
19560
+#ifndef SQLITE_SHELL_FIDDLE
1941719561
/* Cancel output redirection, if it is currently set (by .testcase)
1941819562
** Then read the content of the testcase-out.txt file and compare against
1941919563
** azArg[1]. If there are differences, report an error and exit.
1942019564
*/
1942119565
if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19436,23 +19580,23 @@
1943619580
utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
1943719581
p->nCheck++;
1943819582
}
1943919583
sqlite3_free(zRes);
1944019584
}else
19441
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19585
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1944219586
19443
-#ifndef SQLITE_SHELL_WASM_MODE
19587
+#ifndef SQLITE_SHELL_FIDDLE
1944419588
if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
1944519589
failIfSafeMode(p, "cannot run .clone in safe mode");
1944619590
if( nArg==2 ){
1944719591
tryToClone(p, azArg[1]);
1944819592
}else{
1944919593
raw_printf(stderr, "Usage: .clone FILENAME\n");
1945019594
rc = 1;
1945119595
}
1945219596
}else
19453
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19597
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1945419598
1945519599
if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
1945619600
if( nArg==1 ){
1945719601
/* List available connections */
1945819602
int i;
@@ -19737,11 +19881,11 @@
1973719881
raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
1973819882
rc = 1;
1973919883
}
1974019884
}else
1974119885
19742
-#ifndef SQLITE_SHELL_WASM_MODE
19886
+#ifndef SQLITE_SHELL_FIDDLE
1974319887
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
1974419888
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
1974519889
rc = 2;
1974619890
}else
1974719891
#endif
@@ -19997,11 +20141,11 @@
1999720141
}else{
1999820142
showHelp(p->out, 0);
1999920143
}
2000020144
}else
2000120145
20002
-#ifndef SQLITE_SHELL_WASM_MODE
20146
+#ifndef SQLITE_SHELL_FIDDLE
2000320147
if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
2000420148
char *zTable = 0; /* Insert data into this table */
2000520149
char *zSchema = 0; /* within this schema (may default to "main") */
2000620150
char *zFile = 0; /* Name of file to extra content from */
2000720151
sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20288,11 +20432,11 @@
2028820432
utf8_printf(p->out,
2028920433
"Added %d rows with %d errors using %d lines of input\n",
2029020434
sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
2029120435
}
2029220436
}else
20293
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20437
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2029420438
2029520439
#ifndef SQLITE_UNTESTABLE
2029620440
if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
2029720441
char *zSql;
2029820442
char *zCollist = 0;
@@ -20478,11 +20622,11 @@
2047820622
if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
2047920623
open_db(p, 0);
2048020624
lintDotCommand(p, azArg, nArg);
2048120625
}else
2048220626
20483
-#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
20627
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
2048420628
if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
2048520629
const char *zFile, *zProc;
2048620630
char *zErrMsg = 0;
2048720631
failIfSafeMode(p, "cannot run .load in safe mode");
2048820632
if( nArg<2 ){
@@ -20500,11 +20644,11 @@
2050020644
rc = 1;
2050120645
}
2050220646
}else
2050320647
#endif
2050420648
20505
-#ifndef SQLITE_SHELL_WASM_MODE
20649
+#ifndef SQLITE_SHELL_FIDDLE
2050620650
if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
2050720651
failIfSafeMode(p, "cannot run .log in safe mode");
2050820652
if( nArg!=2 ){
2050920653
raw_printf(stderr, "Usage: .log FILENAME\n");
2051020654
rc = 1;
@@ -20637,11 +20781,11 @@
2063720781
rc = 1;
2063820782
}
2063920783
p->cMode = p->mode;
2064020784
}else
2064120785
20642
-#ifndef SQLITE_SHELL_WASM_MODE
20786
+#ifndef SQLITE_SHELL_FIDDLE
2064320787
if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
2064420788
if( nArg!=2 ){
2064520789
raw_printf(stderr, "Usage: .nonce NONCE\n");
2064620790
rc = 1;
2064720791
}else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20652,11 +20796,11 @@
2065220796
p->bSafeMode = 0;
2065320797
return 0; /* Return immediately to bypass the safe mode reset
2065420798
** at the end of this procedure */
2065520799
}
2065620800
}else
20657
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20801
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2065820802
2065920803
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
2066020804
if( nArg==2 ){
2066120805
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
2066220806
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20674,11 +20818,11 @@
2067420818
int openMode = SHELL_OPEN_UNSPEC;
2067520819
2067620820
/* Check for command-line arguments */
2067720821
for(iName=1; iName<nArg; iName++){
2067820822
const char *z = azArg[iName];
20679
-#ifndef SQLITE_SHELL_WASM_MODE
20823
+#ifndef SQLITE_SHELL_FIDDLE
2068020824
if( optionMatch(z,"new") ){
2068120825
newFlag = 1;
2068220826
#ifdef SQLITE_HAVE_ZLIB
2068320827
}else if( optionMatch(z, "zip") ){
2068420828
openMode = SHELL_OPEN_ZIPFILE;
@@ -20696,11 +20840,11 @@
2069620840
openMode = SHELL_OPEN_HEXDB;
2069720841
}else if( optionMatch(z, "maxsize") && iName+1<nArg ){
2069820842
p->szMax = integerValue(azArg[++iName]);
2069920843
#endif /* SQLITE_OMIT_DESERIALIZE */
2070020844
}else
20701
-#endif /* !SQLITE_SHELL_WASM_MODE */
20845
+#endif /* !SQLITE_SHELL_FIDDLE */
2070220846
if( z[0]=='-' ){
2070320847
utf8_printf(stderr, "unknown option: %s\n", z);
2070420848
rc = 1;
2070520849
goto meta_command_exit;
2070620850
}else if( zFN ){
@@ -20724,11 +20868,11 @@
2072420868
p->szMax = 0;
2072520869
2072620870
/* If a filename is specified, try to open it first */
2072720871
if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
2072820872
if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20729
-#ifndef SQLITE_SHELL_WASM_MODE
20873
+#ifndef SQLITE_SHELL_FIDDLE
2073020874
if( p->bSafeMode
2073120875
&& p->openMode!=SHELL_OPEN_HEXDB
2073220876
&& zFN
2073320877
&& strcmp(zFN,":memory:")!=0
2073420878
){
@@ -20757,11 +20901,11 @@
2075720901
p->pAuxDb->zDbFilename = 0;
2075820902
open_db(p, 0);
2075920903
}
2076020904
}else
2076120905
20762
-#ifndef SQLITE_SHELL_WASM_MODE
20906
+#ifndef SQLITE_SHELL_FIDDLE
2076320907
if( (c=='o'
2076420908
&& (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
2076520909
|| (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
2076620910
){
2076720911
char *zFile = 0;
@@ -20873,11 +21017,11 @@
2087321017
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2087421018
}
2087521019
}
2087621020
sqlite3_free(zFile);
2087721021
}else
20878
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21022
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2087921023
2088021024
if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
2088121025
open_db(p,0);
2088221026
if( nArg<=1 ) goto parameter_syntax_error;
2088321027
@@ -21043,17 +21187,17 @@
2104321187
if( nArg >= 3) {
2104421188
strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
2104521189
}
2104621190
}else
2104721191
21048
-#ifndef SQLITE_SHELL_WASM_MODE
21192
+#ifndef SQLITE_SHELL_FIDDLE
2104921193
if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
2105021194
rc = 2;
2105121195
}else
2105221196
#endif
2105321197
21054
-#ifndef SQLITE_SHELL_WASM_MODE
21198
+#ifndef SQLITE_SHELL_FIDDLE
2105521199
if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
2105621200
FILE *inSaved = p->in;
2105721201
int savedLineno = p->lineno;
2105821202
failIfSafeMode(p, "cannot run .read in safe mode");
2105921203
if( nArg!=2 ){
@@ -21084,13 +21228,13 @@
2108421228
fclose(p->in);
2108521229
}
2108621230
p->in = inSaved;
2108721231
p->lineno = savedLineno;
2108821232
}else
21089
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21233
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2109021234
21091
-#ifndef SQLITE_SHELL_WASM_MODE
21235
+#ifndef SQLITE_SHELL_FIDDLE
2109221236
if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
2109321237
const char *zSrcFile;
2109421238
const char *zDb;
2109521239
sqlite3 *pSrc;
2109621240
sqlite3_backup *pBackup;
@@ -21138,11 +21282,11 @@
2113821282
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2113921283
rc = 1;
2114021284
}
2114121285
close_db(pSrc);
2114221286
}else
21143
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21287
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2114421288
2114521289
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
2114621290
if( nArg==2 ){
2114721291
p->scanstatsOn = (u8)booleanValue(azArg[1]);
2114821292
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21764,11 +21908,11 @@
2176421908
shell_exec(p, zSql, 0);
2176521909
}
2176621910
sqlite3_free(zSql);
2176721911
}else
2176821912
21769
-#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
21913
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
2177021914
if( c=='s'
2177121915
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
2177221916
){
2177321917
char *zCmd;
2177421918
int i, x;
@@ -21785,11 +21929,11 @@
2178521929
}
2178621930
x = zCmd!=0 ? system(zCmd) : 1;
2178721931
sqlite3_free(zCmd);
2178821932
if( x ) raw_printf(stderr, "System command returns %d\n", x);
2178921933
}else
21790
-#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) */
21934
+#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
2179121935
2179221936
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
2179321937
static const char *azBool[] = { "off", "on", "trigger", "full"};
2179421938
const char *zOut;
2179521939
int i;
@@ -21965,11 +22109,11 @@
2196522109
2196622110
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
2196722111
sqlite3_free(azResult);
2196822112
}else
2196922113
21970
-#ifndef SQLITE_SHELL_WASM_MODE
22114
+#ifndef SQLITE_SHELL_FIDDLE
2197122115
/* Begin redirecting output to the file "testcase-out.txt" */
2197222116
if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
2197322117
output_reset(p);
2197422118
p->out = output_file_open("testcase-out.txt", 0);
2197522119
if( p->out==0 ){
@@ -21979,11 +22123,11 @@
2197922123
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
2198022124
}else{
2198122125
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
2198222126
}
2198322127
}else
21984
-#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
22128
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2198522129
2198622130
#ifndef SQLITE_UNTESTABLE
2198722131
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
2198822132
static const struct {
2198922133
const char *zCtrlName; /* Name of a test-control option */
@@ -22651,11 +22795,11 @@
2265122795
2265222796
static void echo_group_input(ShellState *p, const char *zDo){
2265322797
if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
2265422798
}
2265522799
22656
-#ifdef SQLITE_SHELL_WASM_MODE
22800
+#ifdef SQLITE_SHELL_FIDDLE
2265722801
/*
2265822802
** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
2265922803
** because we need the global shellState and cannot access it from that function
2266022804
** without moving lots of code around (creating a larger/messier diff).
2266122805
*/
@@ -22682,11 +22826,11 @@
2268222826
shell_check_oom(zLine);
2268322827
memcpy(zLine, zBegin, (size_t)nZ);
2268422828
zLine[nZ] = 0;
2268522829
return zLine;
2268622830
}
22687
-#endif /* SQLITE_SHELL_WASM_MODE */
22831
+#endif /* SQLITE_SHELL_FIDDLE */
2268822832
2268922833
/*
2269022834
** Read input from *in and process it. If *in==0 then input
2269122835
** is interactive - the user is typing it it. Otherwise, input
2269222836
** is coming from a file or device. A prompt is issued and history
@@ -23065,11 +23209,11 @@
2306523209
# else
2306623210
# define SQLITE_SHELL_IS_UTF8 (1)
2306723211
# endif
2306823212
#endif
2306923213
23070
-#ifdef SQLITE_SHELL_WASM_MODE
23214
+#ifdef SQLITE_SHELL_FIDDLE
2307123215
# define main fiddle_main
2307223216
#endif
2307323217
2307423218
#if SQLITE_SHELL_IS_UTF8
2307523219
int SQLITE_CDECL main(int argc, char **argv){
@@ -23079,11 +23223,11 @@
2307923223
#endif
2308023224
#ifdef SQLITE_DEBUG
2308123225
sqlite3_int64 mem_main_enter = sqlite3_memory_used();
2308223226
#endif
2308323227
char *zErrMsg = 0;
23084
-#ifdef SQLITE_SHELL_WASM_MODE
23228
+#ifdef SQLITE_SHELL_FIDDLE
2308523229
# define data shellState
2308623230
#else
2308723231
ShellState data;
2308823232
#endif
2308923233
const char *zInitFile = 0;
@@ -23099,11 +23243,11 @@
2309923243
int argcToFree = 0;
2310023244
#endif
2310123245
2310223246
setBinaryMode(stdin, 0);
2310323247
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23104
-#ifdef SQLITE_SHELL_WASM_MODE
23248
+#ifdef SQLITE_SHELL_FIDDLE
2310523249
stdin_is_interactive = 0;
2310623250
stdout_is_console = 1;
2310723251
#else
2310823252
stdin_is_interactive = isatty(0);
2310923253
stdout_is_console = isatty(1);
@@ -23361,11 +23505,11 @@
2336123505
utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
2336223506
return 1;
2336323507
#endif
2336423508
}
2336523509
data.out = stdout;
23366
-#ifndef SQLITE_SHELL_WASM_MODE
23510
+#ifndef SQLITE_SHELL_FIDDLE
2336723511
sqlite3_appendvfs_init(0,0,0);
2336823512
#endif
2336923513
2337023514
/* Go ahead and open the database file if it already exists. If the
2337123515
** file does not exist, delay opening it. This prevents empty database
@@ -23629,11 +23773,11 @@
2362923773
}else{
2363023774
data.in = stdin;
2363123775
rc = process_input(&data);
2363223776
}
2363323777
}
23634
-#ifndef SQLITE_SHELL_WASM_MODE
23778
+#ifndef SQLITE_SHELL_FIDDLE
2363523779
/* In WASM mode we have to leave the db state in place so that
2363623780
** client code can "push" SQL into it after this call returns. */
2363723781
free(azCmd);
2363823782
set_table_name(&data, 0);
2363923783
if( data.db ){
@@ -23664,16 +23808,16 @@
2366423808
if( sqlite3_memory_used()>mem_main_enter ){
2366523809
utf8_printf(stderr, "Memory leaked: %u bytes\n",
2366623810
(unsigned int)(sqlite3_memory_used()-mem_main_enter));
2366723811
}
2366823812
#endif
23669
-#endif /* !SQLITE_SHELL_WASM_MODE */
23813
+#endif /* !SQLITE_SHELL_FIDDLE */
2367023814
return rc;
2367123815
}
2367223816
2367323817
23674
-#ifdef SQLITE_SHELL_WASM_MODE
23818
+#ifdef SQLITE_SHELL_FIDDLE
2367523819
/* Only for emcc experimentation purposes. */
2367623820
int fiddle_experiment(int a,int b){
2367723821
return a + b;
2367823822
}
2367923823
@@ -23790,6 +23934,6 @@
2379023934
shellState.wasm.zPos = zSql;
2379123935
process_input(&shellState);
2379223936
memset(&shellState.wasm, 0, sizeof(shellState.wasm));
2379323937
}
2379423938
}
23795
-#endif /* SQLITE_SHELL_WASM_MODE */
23939
+#endif /* SQLITE_SHELL_FIDDLE */
2379623940
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -53,10 +53,19 @@
53 */
54 #if !defined(SQLITE_OS_WINRT)
55 # define SQLITE_OS_WINRT 0
56 #endif
57
 
 
 
 
 
 
 
 
 
58 /*
59 ** Warning pragmas copied from msvc.h in the core.
60 */
61 #if defined(_MSC_VER)
62 #pragma warning(disable : 4054)
@@ -245,21 +254,10 @@
245 #else
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
248 #endif
249
250 /*
251 ** When compiling with emcc (a.k.a. emscripten), we're building a
252 ** WebAssembly (WASM) bundle and need to disable and rewire a few
253 ** things.
254 */
255 #ifdef __EMSCRIPTEN__
256 #define SQLITE_SHELL_WASM_MODE
257 #else
258 #undef SQLITE_SHELL_WASM_MODE
259 #endif
260
261 /* True if the timer is enabled */
262 static int enableTimer = 0;
263
264 /* Return the current wall-clock time */
265 static sqlite3_int64 timeOfDay(void){
@@ -717,11 +715,11 @@
717 **
718 ** The result is stored in space obtained from malloc() and must either
719 ** be freed by the caller or else passed back into this routine via the
720 ** zPrior argument for reuse.
721 */
722 #ifndef SQLITE_SHELL_WASM_MODE
723 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
724 char *zPrompt;
725 char *zResult;
726 if( in!=0 ){
727 zResult = local_getline(zPrior, in);
@@ -737,11 +735,11 @@
737 if( zResult && *zResult ) shell_add_history(zResult);
738 #endif
739 }
740 return zResult;
741 }
742 #endif /* !SQLITE_SHELL_WASM_MODE */
743
744 /*
745 ** Return the value of a hexadecimal digit. Return -1 if the input
746 ** is not a hex digit.
747 */
@@ -3794,10 +3792,11 @@
3794 #define re_compile sqlite3re_compile
3795 #define re_free sqlite3re_free
3796
3797 /* The end-of-input character */
3798 #define RE_EOF 0 /* End of input */
 
3799
3800 /* The NFA is implemented as sequence of opcodes taken from the following
3801 ** set. Each opcode has a single integer argument.
3802 */
3803 #define RE_OP_MATCH 1 /* Match the one character in the argument */
@@ -3815,10 +3814,37 @@
3815 #define RE_OP_DIGIT 13 /* digit: [0-9] */
3816 #define RE_OP_NOTDIGIT 14 /* Not a digit */
3817 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
3818 #define RE_OP_NOTSPACE 16 /* Not a digit */
3819 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3820
3821 /* Each opcode is a "state" in the NFA */
3822 typedef unsigned short ReStateNumber;
3823
3824 /* Because this is an NFA and not a DFA, multiple states can be active at
@@ -3849,11 +3875,11 @@
3849 const char *zErr; /* Error message to return */
3850 char *aOp; /* Operators for the virtual machine */
3851 int *aArg; /* Arguments to each operator */
3852 unsigned (*xNextChar)(ReInput*); /* Next character function */
3853 unsigned char zInit[12]; /* Initial text to match */
3854 int nInit; /* Number of characters in zInit */
3855 unsigned nState; /* Number of entries in aOp[] and aArg[] */
3856 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
3857 };
3858
3859 /* Add a state to the given state set if it is not already there */
@@ -3922,11 +3948,11 @@
3922 ReStateSet aStateSet[2], *pThis, *pNext;
3923 ReStateNumber aSpace[100];
3924 ReStateNumber *pToFree;
3925 unsigned int i = 0;
3926 unsigned int iSwap = 0;
3927 int c = RE_EOF+1;
3928 int cPrev = 0;
3929 int rc = 0;
3930 ReInput in;
3931
3932 in.z = zIn;
@@ -3941,10 +3967,11 @@
3941 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
3942 ){
3943 in.i++;
3944 }
3945 if( in.i+pRe->nInit>in.mx ) return 0;
 
3946 }
3947
3948 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
3949 pToFree = 0;
3950 aStateSet[0].aState = aSpace;
@@ -3968,10 +3995,14 @@
3968 int x = pThis->aState[i];
3969 switch( pRe->aOp[x] ){
3970 case RE_OP_MATCH: {
3971 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
3972 break;
 
 
 
 
3973 }
3974 case RE_OP_ANY: {
3975 if( c!=0 ) re_add_state(pNext, x+1);
3976 break;
3977 }
@@ -4050,11 +4081,13 @@
4050 }
4051 }
4052 }
4053 }
4054 for(i=0; i<pNext->nState; i++){
4055 if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
 
 
4056 }
4057 re_match_end:
4058 sqlite3_free(pToFree);
4059 return rc;
4060 }
@@ -4205,11 +4238,10 @@
4205 const char *zErr;
4206 while( (c = p->xNextChar(&p->sIn))!=0 ){
4207 iStart = p->nState;
4208 switch( c ){
4209 case '|':
4210 case '$':
4211 case ')': {
4212 p->sIn.i--;
4213 return 0;
4214 }
4215 case '(': {
@@ -4241,10 +4273,18 @@
4241 }
4242 case '?': {
4243 if( iPrev<0 ) return "'?' without operand";
4244 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
4245 break;
 
 
 
 
 
 
 
 
4246 }
4247 case '{': {
4248 int m = 0, n = 0;
4249 int sz, j;
4250 if( iPrev<0 ) return "'{m,n}' without operand";
@@ -4260,10 +4300,11 @@
4260 p->sIn.i++;
4261 sz = p->nState - iPrev;
4262 if( m==0 ){
4263 if( n==0 ) return "both m and n are zero in '{m,n}'";
4264 re_insert(p, iPrev, RE_OP_FORK, sz+1);
 
4265 n--;
4266 }else{
4267 for(j=1; j<m; j++) re_copy(p, iPrev, sz);
4268 }
4269 for(j=m; j<n; j++){
@@ -4378,15 +4419,11 @@
4378 zErr = re_subcompile_re(pRe);
4379 if( zErr ){
4380 re_free(pRe);
4381 return zErr;
4382 }
4383 if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
4384 re_append(pRe, RE_OP_MATCH, RE_EOF);
4385 re_append(pRe, RE_OP_ACCEPT, 0);
4386 *ppRe = pRe;
4387 }else if( pRe->sIn.i>=pRe->sIn.mx ){
4388 re_append(pRe, RE_OP_ACCEPT, 0);
4389 *ppRe = pRe;
4390 }else{
4391 re_free(pRe);
4392 return "unrecognized character";
@@ -4465,10 +4502,71 @@
4465 }
4466 if( setAux ){
4467 sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
4468 }
4469 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4470
4471 /*
4472 ** Invoke this routine to register the regexp() function with the
4473 ** SQLite database connection.
4474 */
@@ -4490,16 +4588,23 @@
4490 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
4491 ** of regexp(PATTERN,STRING). */
4492 rc = sqlite3_create_function(db, "regexpi", 2,
4493 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4494 (void*)db, re_sql_func, 0, 0);
 
 
 
 
 
 
 
4495 }
4496 return rc;
4497 }
4498
4499 /************************* End ../ext/misc/regexp.c ********************/
4500 #ifndef SQLITE_SHELL_WASM_MODE
4501 /************************* Begin ../ext/misc/fileio.c ******************/
4502 /*
4503 ** 2014-06-13
4504 **
4505 ** The author disclaims copyright to this source code. In place of
@@ -10047,10 +10152,14 @@
10047 ** Return true if zId must be quoted in order to use it as an SQL
10048 ** identifier, or false otherwise.
10049 */
10050 static int idxIdentifierRequiresQuotes(const char *zId){
10051 int i;
 
 
 
 
10052 for(i=0; zId[i]; i++){
10053 if( !(zId[i]=='_')
10054 && !(zId[i]>='0' && zId[i]<='9')
10055 && !(zId[i]>='a' && zId[i]<='z')
10056 && !(zId[i]>='A' && zId[i]<='Z')
@@ -12248,19 +12357,19 @@
12248 int nIndent; /* Size of array aiIndent[] */
12249 int iIndent; /* Index of current op in aiIndent[] */
12250 char *zNonce; /* Nonce for temporary safe-mode excapes */
12251 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12252 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12253 #ifdef SQLITE_SHELL_WASM_MODE
12254 struct {
12255 const char * zInput; /* Input string from wasm/JS proxy */
12256 const char * zPos; /* Cursor pos into zInput */
12257 } wasm;
12258 #endif
12259 };
12260
12261 #ifdef SQLITE_SHELL_WASM_MODE
12262 static ShellState shellState;
12263 #endif
12264
12265
12266 /* Allowed values for ShellState.autoEQP
@@ -12588,14 +12697,27 @@
12588 /*
12589 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12590 */
12591 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12592 int i;
12593 char *zBlob = (char *)pBlob;
12594 raw_printf(out,"X'");
12595 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12596 raw_printf(out,"'");
 
 
 
 
 
 
 
 
 
 
 
 
 
12597 }
12598
12599 /*
12600 ** Find a string that is not found anywhere in z[]. Return a pointer
12601 ** to that string.
@@ -12924,11 +13046,11 @@
12924 UNUSED_PARAMETER(zA2);
12925 UNUSED_PARAMETER(zA3);
12926 UNUSED_PARAMETER(zA4);
12927 switch( op ){
12928 case SQLITE_ATTACH: {
12929 #ifndef SQLITE_SHELL_WASM_MODE
12930 /* In WASM builds the filesystem is a virtual sandbox, so
12931 ** there's no harm in using ATTACH. */
12932 failIfSafeMode(p, "cannot run ATTACH in safe mode");
12933 #endif
12934 break;
@@ -12997,19 +13119,41 @@
12997 /*
12998 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
12999 **
13000 ** This routine converts some CREATE TABLE statements for shadow tables
13001 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
 
 
 
 
13002 */
13003 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
 
13004 if( z==0 ) return;
13005 if( zTail==0 ) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13006 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
13007 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
13008 }else{
13009 utf8_printf(out, "%s%s", z, zTail);
13010 }
 
13011 }
13012 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
13013 char c = z[n];
13014 z[n] = 0;
13015 printSchemaLine(out, z, zTail);
@@ -15338,11 +15482,11 @@
15338 ** There must be two or more spaces between the end of the command and the
15339 ** start of the description of what that command does.
15340 */
15341 static const char *(azHelp[]) = {
15342 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
15343 && !defined(SQLITE_SHELL_WASM_MODE)
15344 ".archive ... Manage SQL archives",
15345 " Each command must have exactly one of the following options:",
15346 " -c, --create Create a new archive",
15347 " -u, --update Add or update files with changed mtime",
15348 " -i, --insert Like -u but always add even if unchanged",
@@ -15364,23 +15508,23 @@
15364 " http://sqlite.org/cli.html#sqlite_archive_support",
15365 #endif
15366 #ifndef SQLITE_OMIT_AUTHORIZATION
15367 ".auth ON|OFF Show authorizer callbacks",
15368 #endif
15369 #ifndef SQLITE_SHELL_WASM_MODE
15370 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15371 " Options:",
15372 " --append Use the appendvfs",
15373 " --async Write to FILE without journal and fsync()",
15374 #endif
15375 ".bail on|off Stop after hitting an error. Default OFF",
15376 ".binary on|off Turn binary output on or off. Default OFF",
15377 #ifndef SQLITE_SHELL_WASM_MODE
15378 ".cd DIRECTORY Change the working directory to DIRECTORY",
15379 #endif
15380 ".changes on|off Show number of rows changed by SQL",
15381 #ifndef SQLITE_SHELL_WASM_MODE
15382 ".check GLOB Fail if output since .testcase does not match",
15383 ".clone NEWDB Clone data into NEWDB from the existing database",
15384 #endif
15385 ".connection [close] [#] Open or close an auxiliary database connection",
15386 ".databases List names and files of attached databases",
@@ -15402,15 +15546,15 @@
15402 #ifdef SQLITE_DEBUG
15403 " test Show raw EXPLAIN QUERY PLAN output",
15404 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15405 #endif
15406 " trigger Like \"full\" but also show trigger bytecode",
15407 #ifndef SQLITE_SHELL_WASM_MODE
15408 ".excel Display the output of next command in spreadsheet",
15409 " --bom Put a UTF8 byte-order mark on intermediate file",
15410 #endif
15411 #ifndef SQLITE_SHELL_WASM_MODE
15412 ".exit ?CODE? Exit this program with return-code CODE",
15413 #endif
15414 ".expert EXPERIMENTAL. Suggest indexes for queries",
15415 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
15416 ".filectrl CMD ... Run various sqlite3_file_control() operations",
@@ -15417,11 +15561,11 @@
15417 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15418 " --help Show CMD details",
15419 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15420 ".headers on|off Turn display of headers on or off",
15421 ".help ?-all? ?PATTERN? Show help text for PATTERN",
15422 #ifndef SQLITE_SHELL_WASM_MODE
15423 ".import FILE TABLE Import data from FILE into TABLE",
15424 " Options:",
15425 " --ascii Use \\037 and \\036 as column and row separators",
15426 " --csv Use , and \\n as column and row separators",
15427 " --skip N Skip the first N rows of input",
@@ -15446,14 +15590,14 @@
15446 #endif
15447 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
15448 ".lint OPTIONS Report potential schema issues.",
15449 " Options:",
15450 " fkey-indexes Find missing foreign key indexes",
15451 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
15452 ".load FILE ?ENTRY? Load an extension library",
15453 #endif
15454 #ifndef SQLITE_SHELL_WASM_MODE
15455 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15456 #endif
15457 ".mode MODE ?OPTIONS? Set output mode",
15458 " MODE is one of:",
15459 " ascii Columns/rows delimited by 0x1F and 0x1E",
@@ -15476,15 +15620,15 @@
15476 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
15477 " --ww Shorthand for \"--wordwrap 1\"",
15478 " --quote Quote output text as SQL literals",
15479 " --noquote Do not quote output text",
15480 " TABLE The name of SQL table used for \"insert\" mode",
15481 #ifndef SQLITE_SHELL_WASM_MODE
15482 ".nonce STRING Suspend safe mode for one command if nonce matches",
15483 #endif
15484 ".nullvalue STRING Use STRING in place of NULL values",
15485 #ifndef SQLITE_SHELL_WASM_MODE
15486 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15487 " If FILE begins with '|' then open as a pipe",
15488 " --bom Put a UTF8 byte-order mark at the beginning",
15489 " -e Send output to the system text editor",
15490 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
@@ -15502,11 +15646,11 @@
15502 #endif
15503 " --new Initialize FILE to an empty database",
15504 " --nofollow Do not follow symbolic links",
15505 " --readonly Open FILE readonly",
15506 " --zip FILE is a ZIP archive",
15507 #ifndef SQLITE_SHELL_WASM_MODE
15508 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
15509 " If FILE begins with '|' then open it as a pipe.",
15510 " Options:",
15511 " --bom Prefix output with a UTF8 byte-order mark",
15512 " -e Send output to the system text editor",
@@ -15526,11 +15670,11 @@
15526 " --once Do no more than one progress interrupt",
15527 " --quiet|-q No output except at interrupts",
15528 " --reset Reset the count for each input and interrupt",
15529 #endif
15530 ".prompt MAIN CONTINUE Replace the standard prompts",
15531 #ifndef SQLITE_SHELL_WASM_MODE
15532 ".quit Exit this program",
15533 ".read FILE Read input from FILE or command output",
15534 " If FILE begins with \"|\", it is a command that generates the input.",
15535 #endif
15536 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
@@ -15539,11 +15683,11 @@
15539 " --recovery-db NAME Store recovery metadata in database file NAME",
15540 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15541 " --no-rowids Do not attempt to recover rowid values",
15542 " that are not also INTEGER PRIMARY KEYs",
15543 #endif
15544 #ifndef SQLITE_SHELL_WASM_MODE
15545 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15546 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
15547 #endif
15548 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
15549 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15576,24 +15720,24 @@
15576 " --sha3-224 Use the sha3-224 algorithm",
15577 " --sha3-256 Use the sha3-256 algorithm (default)",
15578 " --sha3-384 Use the sha3-384 algorithm",
15579 " --sha3-512 Use the sha3-512 algorithm",
15580 " Any other argument is a LIKE pattern for tables to hash",
15581 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15582 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15583 #endif
15584 ".show Show the current values for various settings",
15585 ".stats ?ARG? Show stats or turn stats on or off",
15586 " off Turn off automatic stat display",
15587 " on Turn on automatic stat display",
15588 " stmt Show statement stats",
15589 " vmstep Show the virtual machine step count only",
15590 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15591 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15592 #endif
15593 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15594 #ifndef SQLITE_SHELL_WASM_MODE
15595 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15596 #endif
15597 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15598 " Run \".testctrl\" with no arguments for details",
15599 ".timeout MS Try opening locked tables for MS milliseconds",
@@ -16142,11 +16286,11 @@
16142 sqlite3_uint_init(p->db, 0, 0);
16143 sqlite3_decimal_init(p->db, 0, 0);
16144 sqlite3_regexp_init(p->db, 0, 0);
16145 sqlite3_ieee_init(p->db, 0, 0);
16146 sqlite3_series_init(p->db, 0, 0);
16147 #ifndef SQLITE_SHELL_WASM_MODE
16148 sqlite3_fileio_init(p->db, 0, 0);
16149 sqlite3_completion_init(p->db, 0, 0);
16150 #endif
16151 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16152 sqlite3_dbdata_init(p->db, 0, 0);
@@ -19272,19 +19416,19 @@
19272 }
19273 }else
19274 #endif
19275
19276 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19277 && !defined(SQLITE_SHELL_WASM_MODE)
19278 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19279 open_db(p, 0);
19280 failIfSafeMode(p, "cannot run .archive in safe mode");
19281 rc = arDotCommand(p, 0, azArg, nArg);
19282 }else
19283 #endif
19284
19285 #ifndef SQLITE_SHELL_WASM_MODE
19286 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
19287 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
19288 ){
19289 const char *zDestFile = 0;
19290 const char *zDb = 0;
@@ -19349,11 +19493,11 @@
19349 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19350 rc = 1;
19351 }
19352 close_db(pDest);
19353 }else
19354 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19355
19356 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19357 if( nArg==2 ){
19358 bail_on_error = booleanValue(azArg[1]);
19359 }else{
@@ -19380,11 +19524,11 @@
19380 */
19381 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19382 test_breakpoint();
19383 }else
19384
19385 #ifndef SQLITE_SHELL_WASM_MODE
19386 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19387 failIfSafeMode(p, "cannot run .cd in safe mode");
19388 if( nArg==2 ){
19389 #if defined(_WIN32) || defined(WIN32)
19390 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19400,11 +19544,11 @@
19400 }else{
19401 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
19402 rc = 1;
19403 }
19404 }else
19405 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19406
19407 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19408 if( nArg==2 ){
19409 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19410 }else{
@@ -19411,11 +19555,11 @@
19411 raw_printf(stderr, "Usage: .changes on|off\n");
19412 rc = 1;
19413 }
19414 }else
19415
19416 #ifndef SQLITE_SHELL_WASM_MODE
19417 /* Cancel output redirection, if it is currently set (by .testcase)
19418 ** Then read the content of the testcase-out.txt file and compare against
19419 ** azArg[1]. If there are differences, report an error and exit.
19420 */
19421 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19436,23 +19580,23 @@
19436 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
19437 p->nCheck++;
19438 }
19439 sqlite3_free(zRes);
19440 }else
19441 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19442
19443 #ifndef SQLITE_SHELL_WASM_MODE
19444 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19445 failIfSafeMode(p, "cannot run .clone in safe mode");
19446 if( nArg==2 ){
19447 tryToClone(p, azArg[1]);
19448 }else{
19449 raw_printf(stderr, "Usage: .clone FILENAME\n");
19450 rc = 1;
19451 }
19452 }else
19453 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19454
19455 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19456 if( nArg==1 ){
19457 /* List available connections */
19458 int i;
@@ -19737,11 +19881,11 @@
19737 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19738 rc = 1;
19739 }
19740 }else
19741
19742 #ifndef SQLITE_SHELL_WASM_MODE
19743 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19744 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19745 rc = 2;
19746 }else
19747 #endif
@@ -19997,11 +20141,11 @@
19997 }else{
19998 showHelp(p->out, 0);
19999 }
20000 }else
20001
20002 #ifndef SQLITE_SHELL_WASM_MODE
20003 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
20004 char *zTable = 0; /* Insert data into this table */
20005 char *zSchema = 0; /* within this schema (may default to "main") */
20006 char *zFile = 0; /* Name of file to extra content from */
20007 sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20288,11 +20432,11 @@
20288 utf8_printf(p->out,
20289 "Added %d rows with %d errors using %d lines of input\n",
20290 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
20291 }
20292 }else
20293 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20294
20295 #ifndef SQLITE_UNTESTABLE
20296 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20297 char *zSql;
20298 char *zCollist = 0;
@@ -20478,11 +20622,11 @@
20478 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20479 open_db(p, 0);
20480 lintDotCommand(p, azArg, nArg);
20481 }else
20482
20483 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
20484 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20485 const char *zFile, *zProc;
20486 char *zErrMsg = 0;
20487 failIfSafeMode(p, "cannot run .load in safe mode");
20488 if( nArg<2 ){
@@ -20500,11 +20644,11 @@
20500 rc = 1;
20501 }
20502 }else
20503 #endif
20504
20505 #ifndef SQLITE_SHELL_WASM_MODE
20506 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20507 failIfSafeMode(p, "cannot run .log in safe mode");
20508 if( nArg!=2 ){
20509 raw_printf(stderr, "Usage: .log FILENAME\n");
20510 rc = 1;
@@ -20637,11 +20781,11 @@
20637 rc = 1;
20638 }
20639 p->cMode = p->mode;
20640 }else
20641
20642 #ifndef SQLITE_SHELL_WASM_MODE
20643 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20644 if( nArg!=2 ){
20645 raw_printf(stderr, "Usage: .nonce NONCE\n");
20646 rc = 1;
20647 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20652,11 +20796,11 @@
20652 p->bSafeMode = 0;
20653 return 0; /* Return immediately to bypass the safe mode reset
20654 ** at the end of this procedure */
20655 }
20656 }else
20657 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20658
20659 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20660 if( nArg==2 ){
20661 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20662 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20674,11 +20818,11 @@
20674 int openMode = SHELL_OPEN_UNSPEC;
20675
20676 /* Check for command-line arguments */
20677 for(iName=1; iName<nArg; iName++){
20678 const char *z = azArg[iName];
20679 #ifndef SQLITE_SHELL_WASM_MODE
20680 if( optionMatch(z,"new") ){
20681 newFlag = 1;
20682 #ifdef SQLITE_HAVE_ZLIB
20683 }else if( optionMatch(z, "zip") ){
20684 openMode = SHELL_OPEN_ZIPFILE;
@@ -20696,11 +20840,11 @@
20696 openMode = SHELL_OPEN_HEXDB;
20697 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20698 p->szMax = integerValue(azArg[++iName]);
20699 #endif /* SQLITE_OMIT_DESERIALIZE */
20700 }else
20701 #endif /* !SQLITE_SHELL_WASM_MODE */
20702 if( z[0]=='-' ){
20703 utf8_printf(stderr, "unknown option: %s\n", z);
20704 rc = 1;
20705 goto meta_command_exit;
20706 }else if( zFN ){
@@ -20724,11 +20868,11 @@
20724 p->szMax = 0;
20725
20726 /* If a filename is specified, try to open it first */
20727 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20728 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20729 #ifndef SQLITE_SHELL_WASM_MODE
20730 if( p->bSafeMode
20731 && p->openMode!=SHELL_OPEN_HEXDB
20732 && zFN
20733 && strcmp(zFN,":memory:")!=0
20734 ){
@@ -20757,11 +20901,11 @@
20757 p->pAuxDb->zDbFilename = 0;
20758 open_db(p, 0);
20759 }
20760 }else
20761
20762 #ifndef SQLITE_SHELL_WASM_MODE
20763 if( (c=='o'
20764 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20765 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20766 ){
20767 char *zFile = 0;
@@ -20873,11 +21017,11 @@
20873 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20874 }
20875 }
20876 sqlite3_free(zFile);
20877 }else
20878 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20879
20880 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20881 open_db(p,0);
20882 if( nArg<=1 ) goto parameter_syntax_error;
20883
@@ -21043,17 +21187,17 @@
21043 if( nArg >= 3) {
21044 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
21045 }
21046 }else
21047
21048 #ifndef SQLITE_SHELL_WASM_MODE
21049 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
21050 rc = 2;
21051 }else
21052 #endif
21053
21054 #ifndef SQLITE_SHELL_WASM_MODE
21055 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
21056 FILE *inSaved = p->in;
21057 int savedLineno = p->lineno;
21058 failIfSafeMode(p, "cannot run .read in safe mode");
21059 if( nArg!=2 ){
@@ -21084,13 +21228,13 @@
21084 fclose(p->in);
21085 }
21086 p->in = inSaved;
21087 p->lineno = savedLineno;
21088 }else
21089 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21090
21091 #ifndef SQLITE_SHELL_WASM_MODE
21092 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
21093 const char *zSrcFile;
21094 const char *zDb;
21095 sqlite3 *pSrc;
21096 sqlite3_backup *pBackup;
@@ -21138,11 +21282,11 @@
21138 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21139 rc = 1;
21140 }
21141 close_db(pSrc);
21142 }else
21143 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21144
21145 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21146 if( nArg==2 ){
21147 p->scanstatsOn = (u8)booleanValue(azArg[1]);
21148 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21764,11 +21908,11 @@
21764 shell_exec(p, zSql, 0);
21765 }
21766 sqlite3_free(zSql);
21767 }else
21768
21769 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
21770 if( c=='s'
21771 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21772 ){
21773 char *zCmd;
21774 int i, x;
@@ -21785,11 +21929,11 @@
21785 }
21786 x = zCmd!=0 ? system(zCmd) : 1;
21787 sqlite3_free(zCmd);
21788 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21789 }else
21790 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) */
21791
21792 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21793 static const char *azBool[] = { "off", "on", "trigger", "full"};
21794 const char *zOut;
21795 int i;
@@ -21965,11 +22109,11 @@
21965
21966 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21967 sqlite3_free(azResult);
21968 }else
21969
21970 #ifndef SQLITE_SHELL_WASM_MODE
21971 /* Begin redirecting output to the file "testcase-out.txt" */
21972 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21973 output_reset(p);
21974 p->out = output_file_open("testcase-out.txt", 0);
21975 if( p->out==0 ){
@@ -21979,11 +22123,11 @@
21979 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21980 }else{
21981 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21982 }
21983 }else
21984 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21985
21986 #ifndef SQLITE_UNTESTABLE
21987 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21988 static const struct {
21989 const char *zCtrlName; /* Name of a test-control option */
@@ -22651,11 +22795,11 @@
22651
22652 static void echo_group_input(ShellState *p, const char *zDo){
22653 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
22654 }
22655
22656 #ifdef SQLITE_SHELL_WASM_MODE
22657 /*
22658 ** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
22659 ** because we need the global shellState and cannot access it from that function
22660 ** without moving lots of code around (creating a larger/messier diff).
22661 */
@@ -22682,11 +22826,11 @@
22682 shell_check_oom(zLine);
22683 memcpy(zLine, zBegin, (size_t)nZ);
22684 zLine[nZ] = 0;
22685 return zLine;
22686 }
22687 #endif /* SQLITE_SHELL_WASM_MODE */
22688
22689 /*
22690 ** Read input from *in and process it. If *in==0 then input
22691 ** is interactive - the user is typing it it. Otherwise, input
22692 ** is coming from a file or device. A prompt is issued and history
@@ -23065,11 +23209,11 @@
23065 # else
23066 # define SQLITE_SHELL_IS_UTF8 (1)
23067 # endif
23068 #endif
23069
23070 #ifdef SQLITE_SHELL_WASM_MODE
23071 # define main fiddle_main
23072 #endif
23073
23074 #if SQLITE_SHELL_IS_UTF8
23075 int SQLITE_CDECL main(int argc, char **argv){
@@ -23079,11 +23223,11 @@
23079 #endif
23080 #ifdef SQLITE_DEBUG
23081 sqlite3_int64 mem_main_enter = sqlite3_memory_used();
23082 #endif
23083 char *zErrMsg = 0;
23084 #ifdef SQLITE_SHELL_WASM_MODE
23085 # define data shellState
23086 #else
23087 ShellState data;
23088 #endif
23089 const char *zInitFile = 0;
@@ -23099,11 +23243,11 @@
23099 int argcToFree = 0;
23100 #endif
23101
23102 setBinaryMode(stdin, 0);
23103 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23104 #ifdef SQLITE_SHELL_WASM_MODE
23105 stdin_is_interactive = 0;
23106 stdout_is_console = 1;
23107 #else
23108 stdin_is_interactive = isatty(0);
23109 stdout_is_console = isatty(1);
@@ -23361,11 +23505,11 @@
23361 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
23362 return 1;
23363 #endif
23364 }
23365 data.out = stdout;
23366 #ifndef SQLITE_SHELL_WASM_MODE
23367 sqlite3_appendvfs_init(0,0,0);
23368 #endif
23369
23370 /* Go ahead and open the database file if it already exists. If the
23371 ** file does not exist, delay opening it. This prevents empty database
@@ -23629,11 +23773,11 @@
23629 }else{
23630 data.in = stdin;
23631 rc = process_input(&data);
23632 }
23633 }
23634 #ifndef SQLITE_SHELL_WASM_MODE
23635 /* In WASM mode we have to leave the db state in place so that
23636 ** client code can "push" SQL into it after this call returns. */
23637 free(azCmd);
23638 set_table_name(&data, 0);
23639 if( data.db ){
@@ -23664,16 +23808,16 @@
23664 if( sqlite3_memory_used()>mem_main_enter ){
23665 utf8_printf(stderr, "Memory leaked: %u bytes\n",
23666 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
23667 }
23668 #endif
23669 #endif /* !SQLITE_SHELL_WASM_MODE */
23670 return rc;
23671 }
23672
23673
23674 #ifdef SQLITE_SHELL_WASM_MODE
23675 /* Only for emcc experimentation purposes. */
23676 int fiddle_experiment(int a,int b){
23677 return a + b;
23678 }
23679
@@ -23790,6 +23934,6 @@
23790 shellState.wasm.zPos = zSql;
23791 process_input(&shellState);
23792 memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23793 }
23794 }
23795 #endif /* SQLITE_SHELL_WASM_MODE */
23796
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -53,10 +53,19 @@
53 */
54 #if !defined(SQLITE_OS_WINRT)
55 # define SQLITE_OS_WINRT 0
56 #endif
57
58 /*
59 ** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
60 ** somewhat for use as a WASM module in a web browser. This flag
61 ** should only be used when building the "fiddle" web application, as
62 ** the browser-mode build has much different user input requirements
63 ** and this build mode rewires the user input subsystem to account for
64 ** that.
65 */
66
67 /*
68 ** Warning pragmas copied from msvc.h in the core.
69 */
70 #if defined(_MSC_VER)
71 #pragma warning(disable : 4054)
@@ -245,21 +254,10 @@
254 #else
255 # define setBinaryMode(X,Y)
256 # define setTextMode(X,Y)
257 #endif
258
 
 
 
 
 
 
 
 
 
 
 
259 /* True if the timer is enabled */
260 static int enableTimer = 0;
261
262 /* Return the current wall-clock time */
263 static sqlite3_int64 timeOfDay(void){
@@ -717,11 +715,11 @@
715 **
716 ** The result is stored in space obtained from malloc() and must either
717 ** be freed by the caller or else passed back into this routine via the
718 ** zPrior argument for reuse.
719 */
720 #ifndef SQLITE_SHELL_FIDDLE
721 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
722 char *zPrompt;
723 char *zResult;
724 if( in!=0 ){
725 zResult = local_getline(zPrior, in);
@@ -737,11 +735,11 @@
735 if( zResult && *zResult ) shell_add_history(zResult);
736 #endif
737 }
738 return zResult;
739 }
740 #endif /* !SQLITE_SHELL_FIDDLE */
741
742 /*
743 ** Return the value of a hexadecimal digit. Return -1 if the input
744 ** is not a hex digit.
745 */
@@ -3794,10 +3792,11 @@
3792 #define re_compile sqlite3re_compile
3793 #define re_free sqlite3re_free
3794
3795 /* The end-of-input character */
3796 #define RE_EOF 0 /* End of input */
3797 #define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
3798
3799 /* The NFA is implemented as sequence of opcodes taken from the following
3800 ** set. Each opcode has a single integer argument.
3801 */
3802 #define RE_OP_MATCH 1 /* Match the one character in the argument */
@@ -3815,10 +3814,37 @@
3814 #define RE_OP_DIGIT 13 /* digit: [0-9] */
3815 #define RE_OP_NOTDIGIT 14 /* Not a digit */
3816 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
3817 #define RE_OP_NOTSPACE 16 /* Not a digit */
3818 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
3819 #define RE_OP_ATSTART 18 /* Currently at the start of the string */
3820
3821 #if defined(SQLITE_DEBUG)
3822 /* Opcode names used for symbolic debugging */
3823 static const char *ReOpName[] = {
3824 "EOF",
3825 "MATCH",
3826 "ANY",
3827 "ANYSTAR",
3828 "FORK",
3829 "GOTO",
3830 "ACCEPT",
3831 "CC_INC",
3832 "CC_EXC",
3833 "CC_VALUE",
3834 "CC_RANGE",
3835 "WORD",
3836 "NOTWORD",
3837 "DIGIT",
3838 "NOTDIGIT",
3839 "SPACE",
3840 "NOTSPACE",
3841 "BOUNDARY",
3842 "ATSTART",
3843 };
3844 #endif /* SQLITE_DEBUG */
3845
3846
3847 /* Each opcode is a "state" in the NFA */
3848 typedef unsigned short ReStateNumber;
3849
3850 /* Because this is an NFA and not a DFA, multiple states can be active at
@@ -3849,11 +3875,11 @@
3875 const char *zErr; /* Error message to return */
3876 char *aOp; /* Operators for the virtual machine */
3877 int *aArg; /* Arguments to each operator */
3878 unsigned (*xNextChar)(ReInput*); /* Next character function */
3879 unsigned char zInit[12]; /* Initial text to match */
3880 int nInit; /* Number of bytes in zInit */
3881 unsigned nState; /* Number of entries in aOp[] and aArg[] */
3882 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
3883 };
3884
3885 /* Add a state to the given state set if it is not already there */
@@ -3922,11 +3948,11 @@
3948 ReStateSet aStateSet[2], *pThis, *pNext;
3949 ReStateNumber aSpace[100];
3950 ReStateNumber *pToFree;
3951 unsigned int i = 0;
3952 unsigned int iSwap = 0;
3953 int c = RE_START;
3954 int cPrev = 0;
3955 int rc = 0;
3956 ReInput in;
3957
3958 in.z = zIn;
@@ -3941,10 +3967,11 @@
3967 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
3968 ){
3969 in.i++;
3970 }
3971 if( in.i+pRe->nInit>in.mx ) return 0;
3972 c = RE_START-1;
3973 }
3974
3975 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
3976 pToFree = 0;
3977 aStateSet[0].aState = aSpace;
@@ -3968,10 +3995,14 @@
3995 int x = pThis->aState[i];
3996 switch( pRe->aOp[x] ){
3997 case RE_OP_MATCH: {
3998 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
3999 break;
4000 }
4001 case RE_OP_ATSTART: {
4002 if( cPrev==RE_START ) re_add_state(pThis, x+1);
4003 break;
4004 }
4005 case RE_OP_ANY: {
4006 if( c!=0 ) re_add_state(pNext, x+1);
4007 break;
4008 }
@@ -4050,11 +4081,13 @@
4081 }
4082 }
4083 }
4084 }
4085 for(i=0; i<pNext->nState; i++){
4086 int x = pNext->aState[i];
4087 while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x];
4088 if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; }
4089 }
4090 re_match_end:
4091 sqlite3_free(pToFree);
4092 return rc;
4093 }
@@ -4205,11 +4238,10 @@
4238 const char *zErr;
4239 while( (c = p->xNextChar(&p->sIn))!=0 ){
4240 iStart = p->nState;
4241 switch( c ){
4242 case '|':
 
4243 case ')': {
4244 p->sIn.i--;
4245 return 0;
4246 }
4247 case '(': {
@@ -4241,10 +4273,18 @@
4273 }
4274 case '?': {
4275 if( iPrev<0 ) return "'?' without operand";
4276 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
4277 break;
4278 }
4279 case '$': {
4280 re_append(p, RE_OP_MATCH, RE_EOF);
4281 break;
4282 }
4283 case '^': {
4284 re_append(p, RE_OP_ATSTART, 0);
4285 break;
4286 }
4287 case '{': {
4288 int m = 0, n = 0;
4289 int sz, j;
4290 if( iPrev<0 ) return "'{m,n}' without operand";
@@ -4260,10 +4300,11 @@
4300 p->sIn.i++;
4301 sz = p->nState - iPrev;
4302 if( m==0 ){
4303 if( n==0 ) return "both m and n are zero in '{m,n}'";
4304 re_insert(p, iPrev, RE_OP_FORK, sz+1);
4305 iPrev++;
4306 n--;
4307 }else{
4308 for(j=1; j<m; j++) re_copy(p, iPrev, sz);
4309 }
4310 for(j=m; j<n; j++){
@@ -4378,15 +4419,11 @@
4419 zErr = re_subcompile_re(pRe);
4420 if( zErr ){
4421 re_free(pRe);
4422 return zErr;
4423 }
4424 if( pRe->sIn.i>=pRe->sIn.mx ){
 
 
 
 
4425 re_append(pRe, RE_OP_ACCEPT, 0);
4426 *ppRe = pRe;
4427 }else{
4428 re_free(pRe);
4429 return "unrecognized character";
@@ -4465,10 +4502,71 @@
4502 }
4503 if( setAux ){
4504 sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
4505 }
4506 }
4507
4508 #if defined(SQLITE_DEBUG)
4509 /*
4510 ** This function is used for testing and debugging only. It is only available
4511 ** if the SQLITE_DEBUG compile-time option is used.
4512 **
4513 ** Compile a regular expression and then convert the compiled expression into
4514 ** text and return that text.
4515 */
4516 static void re_bytecode_func(
4517 sqlite3_context *context,
4518 int argc,
4519 sqlite3_value **argv
4520 ){
4521 const char *zPattern;
4522 const char *zErr;
4523 ReCompiled *pRe;
4524 sqlite3_str *pStr;
4525 int i;
4526 int n;
4527 char *z;
4528
4529 zPattern = (const char*)sqlite3_value_text(argv[0]);
4530 if( zPattern==0 ) return;
4531 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
4532 if( zErr ){
4533 re_free(pRe);
4534 sqlite3_result_error(context, zErr, -1);
4535 return;
4536 }
4537 if( pRe==0 ){
4538 sqlite3_result_error_nomem(context);
4539 return;
4540 }
4541 pStr = sqlite3_str_new(0);
4542 if( pStr==0 ) goto re_bytecode_func_err;
4543 if( pRe->nInit>0 ){
4544 sqlite3_str_appendf(pStr, "INIT ");
4545 for(i=0; i<pRe->nInit; i++){
4546 sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]);
4547 }
4548 sqlite3_str_appendf(pStr, "\n");
4549 }
4550 for(i=0; (unsigned)i<pRe->nState; i++){
4551 sqlite3_str_appendf(pStr, "%-8s %4d\n",
4552 ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]);
4553 }
4554 n = sqlite3_str_length(pStr);
4555 z = sqlite3_str_finish(pStr);
4556 if( n==0 ){
4557 sqlite3_free(z);
4558 }else{
4559 sqlite3_result_text(context, z, n-1, sqlite3_free);
4560 }
4561
4562 re_bytecode_func_err:
4563 re_free(pRe);
4564 }
4565
4566 #endif /* SQLITE_DEBUG */
4567
4568
4569 /*
4570 ** Invoke this routine to register the regexp() function with the
4571 ** SQLite database connection.
4572 */
@@ -4490,16 +4588,23 @@
4588 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
4589 ** of regexp(PATTERN,STRING). */
4590 rc = sqlite3_create_function(db, "regexpi", 2,
4591 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4592 (void*)db, re_sql_func, 0, 0);
4593 #if defined(SQLITE_DEBUG)
4594 if( rc==SQLITE_OK ){
4595 rc = sqlite3_create_function(db, "regexp_bytecode", 1,
4596 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4597 0, re_bytecode_func, 0, 0);
4598 }
4599 #endif /* SQLITE_DEBUG */
4600 }
4601 return rc;
4602 }
4603
4604 /************************* End ../ext/misc/regexp.c ********************/
4605 #ifndef SQLITE_SHELL_FIDDLE
4606 /************************* Begin ../ext/misc/fileio.c ******************/
4607 /*
4608 ** 2014-06-13
4609 **
4610 ** The author disclaims copyright to this source code. In place of
@@ -10047,10 +10152,14 @@
10152 ** Return true if zId must be quoted in order to use it as an SQL
10153 ** identifier, or false otherwise.
10154 */
10155 static int idxIdentifierRequiresQuotes(const char *zId){
10156 int i;
10157 int nId = STRLEN(zId);
10158
10159 if( sqlite3_keyword_check(zId, nId) ) return 1;
10160
10161 for(i=0; zId[i]; i++){
10162 if( !(zId[i]=='_')
10163 && !(zId[i]>='0' && zId[i]<='9')
10164 && !(zId[i]>='a' && zId[i]<='z')
10165 && !(zId[i]>='A' && zId[i]<='Z')
@@ -12248,19 +12357,19 @@
12357 int nIndent; /* Size of array aiIndent[] */
12358 int iIndent; /* Index of current op in aiIndent[] */
12359 char *zNonce; /* Nonce for temporary safe-mode excapes */
12360 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12361 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12362 #ifdef SQLITE_SHELL_FIDDLE
12363 struct {
12364 const char * zInput; /* Input string from wasm/JS proxy */
12365 const char * zPos; /* Cursor pos into zInput */
12366 } wasm;
12367 #endif
12368 };
12369
12370 #ifdef SQLITE_SHELL_FIDDLE
12371 static ShellState shellState;
12372 #endif
12373
12374
12375 /* Allowed values for ShellState.autoEQP
@@ -12588,14 +12697,27 @@
12697 /*
12698 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12699 */
12700 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12701 int i;
12702 unsigned char *aBlob = (unsigned char*)pBlob;
12703
12704 char *zStr = sqlite3_malloc(nBlob*2 + 1);
12705 shell_check_oom(zStr);
12706
12707 for(i=0; i<nBlob; i++){
12708 static const char aHex[] = {
12709 '0', '1', '2', '3', '4', '5', '6', '7',
12710 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
12711 };
12712 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
12713 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
12714 }
12715 zStr[i*2] = '\0';
12716
12717 raw_printf(out,"X'%s'", zStr);
12718 sqlite3_free(zStr);
12719 }
12720
12721 /*
12722 ** Find a string that is not found anywhere in z[]. Return a pointer
12723 ** to that string.
@@ -12924,11 +13046,11 @@
13046 UNUSED_PARAMETER(zA2);
13047 UNUSED_PARAMETER(zA3);
13048 UNUSED_PARAMETER(zA4);
13049 switch( op ){
13050 case SQLITE_ATTACH: {
13051 #ifndef SQLITE_SHELL_FIDDLE
13052 /* In WASM builds the filesystem is a virtual sandbox, so
13053 ** there's no harm in using ATTACH. */
13054 failIfSafeMode(p, "cannot run ATTACH in safe mode");
13055 #endif
13056 break;
@@ -12997,19 +13119,41 @@
13119 /*
13120 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
13121 **
13122 ** This routine converts some CREATE TABLE statements for shadow tables
13123 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
13124 **
13125 ** If the schema statement in z[] contains a start-of-comment and if
13126 ** sqlite3_complete() returns false, try to terminate the comment before
13127 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
13128 */
13129 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
13130 char *zToFree = 0;
13131 if( z==0 ) return;
13132 if( zTail==0 ) return;
13133 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
13134 const char *zOrig = z;
13135 static const char *azTerm[] = { "", "*/", "\n" };
13136 int i;
13137 for(i=0; i<ArraySize(azTerm); i++){
13138 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
13139 if( sqlite3_complete(zNew) ){
13140 size_t n = strlen(zNew);
13141 zNew[n-1] = 0;
13142 zToFree = zNew;
13143 z = zNew;
13144 break;
13145 }
13146 sqlite3_free(zNew);
13147 }
13148 }
13149 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
13150 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
13151 }else{
13152 utf8_printf(out, "%s%s", z, zTail);
13153 }
13154 sqlite3_free(zToFree);
13155 }
13156 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
13157 char c = z[n];
13158 z[n] = 0;
13159 printSchemaLine(out, z, zTail);
@@ -15338,11 +15482,11 @@
15482 ** There must be two or more spaces between the end of the command and the
15483 ** start of the description of what that command does.
15484 */
15485 static const char *(azHelp[]) = {
15486 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
15487 && !defined(SQLITE_SHELL_FIDDLE)
15488 ".archive ... Manage SQL archives",
15489 " Each command must have exactly one of the following options:",
15490 " -c, --create Create a new archive",
15491 " -u, --update Add or update files with changed mtime",
15492 " -i, --insert Like -u but always add even if unchanged",
@@ -15364,23 +15508,23 @@
15508 " http://sqlite.org/cli.html#sqlite_archive_support",
15509 #endif
15510 #ifndef SQLITE_OMIT_AUTHORIZATION
15511 ".auth ON|OFF Show authorizer callbacks",
15512 #endif
15513 #ifndef SQLITE_SHELL_FIDDLE
15514 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15515 " Options:",
15516 " --append Use the appendvfs",
15517 " --async Write to FILE without journal and fsync()",
15518 #endif
15519 ".bail on|off Stop after hitting an error. Default OFF",
15520 ".binary on|off Turn binary output on or off. Default OFF",
15521 #ifndef SQLITE_SHELL_FIDDLE
15522 ".cd DIRECTORY Change the working directory to DIRECTORY",
15523 #endif
15524 ".changes on|off Show number of rows changed by SQL",
15525 #ifndef SQLITE_SHELL_FIDDLE
15526 ".check GLOB Fail if output since .testcase does not match",
15527 ".clone NEWDB Clone data into NEWDB from the existing database",
15528 #endif
15529 ".connection [close] [#] Open or close an auxiliary database connection",
15530 ".databases List names and files of attached databases",
@@ -15402,15 +15546,15 @@
15546 #ifdef SQLITE_DEBUG
15547 " test Show raw EXPLAIN QUERY PLAN output",
15548 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15549 #endif
15550 " trigger Like \"full\" but also show trigger bytecode",
15551 #ifndef SQLITE_SHELL_FIDDLE
15552 ".excel Display the output of next command in spreadsheet",
15553 " --bom Put a UTF8 byte-order mark on intermediate file",
15554 #endif
15555 #ifndef SQLITE_SHELL_FIDDLE
15556 ".exit ?CODE? Exit this program with return-code CODE",
15557 #endif
15558 ".expert EXPERIMENTAL. Suggest indexes for queries",
15559 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
15560 ".filectrl CMD ... Run various sqlite3_file_control() operations",
@@ -15417,11 +15561,11 @@
15561 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15562 " --help Show CMD details",
15563 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15564 ".headers on|off Turn display of headers on or off",
15565 ".help ?-all? ?PATTERN? Show help text for PATTERN",
15566 #ifndef SQLITE_SHELL_FIDDLE
15567 ".import FILE TABLE Import data from FILE into TABLE",
15568 " Options:",
15569 " --ascii Use \\037 and \\036 as column and row separators",
15570 " --csv Use , and \\n as column and row separators",
15571 " --skip N Skip the first N rows of input",
@@ -15446,14 +15590,14 @@
15590 #endif
15591 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
15592 ".lint OPTIONS Report potential schema issues.",
15593 " Options:",
15594 " fkey-indexes Find missing foreign key indexes",
15595 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
15596 ".load FILE ?ENTRY? Load an extension library",
15597 #endif
15598 #ifndef SQLITE_SHELL_FIDDLE
15599 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15600 #endif
15601 ".mode MODE ?OPTIONS? Set output mode",
15602 " MODE is one of:",
15603 " ascii Columns/rows delimited by 0x1F and 0x1E",
@@ -15476,15 +15620,15 @@
15620 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
15621 " --ww Shorthand for \"--wordwrap 1\"",
15622 " --quote Quote output text as SQL literals",
15623 " --noquote Do not quote output text",
15624 " TABLE The name of SQL table used for \"insert\" mode",
15625 #ifndef SQLITE_SHELL_FIDDLE
15626 ".nonce STRING Suspend safe mode for one command if nonce matches",
15627 #endif
15628 ".nullvalue STRING Use STRING in place of NULL values",
15629 #ifndef SQLITE_SHELL_FIDDLE
15630 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15631 " If FILE begins with '|' then open as a pipe",
15632 " --bom Put a UTF8 byte-order mark at the beginning",
15633 " -e Send output to the system text editor",
15634 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
@@ -15502,11 +15646,11 @@
15646 #endif
15647 " --new Initialize FILE to an empty database",
15648 " --nofollow Do not follow symbolic links",
15649 " --readonly Open FILE readonly",
15650 " --zip FILE is a ZIP archive",
15651 #ifndef SQLITE_SHELL_FIDDLE
15652 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
15653 " If FILE begins with '|' then open it as a pipe.",
15654 " Options:",
15655 " --bom Prefix output with a UTF8 byte-order mark",
15656 " -e Send output to the system text editor",
@@ -15526,11 +15670,11 @@
15670 " --once Do no more than one progress interrupt",
15671 " --quiet|-q No output except at interrupts",
15672 " --reset Reset the count for each input and interrupt",
15673 #endif
15674 ".prompt MAIN CONTINUE Replace the standard prompts",
15675 #ifndef SQLITE_SHELL_FIDDLE
15676 ".quit Exit this program",
15677 ".read FILE Read input from FILE or command output",
15678 " If FILE begins with \"|\", it is a command that generates the input.",
15679 #endif
15680 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
@@ -15539,11 +15683,11 @@
15683 " --recovery-db NAME Store recovery metadata in database file NAME",
15684 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15685 " --no-rowids Do not attempt to recover rowid values",
15686 " that are not also INTEGER PRIMARY KEYs",
15687 #endif
15688 #ifndef SQLITE_SHELL_FIDDLE
15689 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15690 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
15691 #endif
15692 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
15693 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15576,24 +15720,24 @@
15720 " --sha3-224 Use the sha3-224 algorithm",
15721 " --sha3-256 Use the sha3-256 algorithm (default)",
15722 " --sha3-384 Use the sha3-384 algorithm",
15723 " --sha3-512 Use the sha3-512 algorithm",
15724 " Any other argument is a LIKE pattern for tables to hash",
15725 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
15726 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15727 #endif
15728 ".show Show the current values for various settings",
15729 ".stats ?ARG? Show stats or turn stats on or off",
15730 " off Turn off automatic stat display",
15731 " on Turn on automatic stat display",
15732 " stmt Show statement stats",
15733 " vmstep Show the virtual machine step count only",
15734 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
15735 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15736 #endif
15737 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15738 #ifndef SQLITE_SHELL_FIDDLE
15739 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15740 #endif
15741 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15742 " Run \".testctrl\" with no arguments for details",
15743 ".timeout MS Try opening locked tables for MS milliseconds",
@@ -16142,11 +16286,11 @@
16286 sqlite3_uint_init(p->db, 0, 0);
16287 sqlite3_decimal_init(p->db, 0, 0);
16288 sqlite3_regexp_init(p->db, 0, 0);
16289 sqlite3_ieee_init(p->db, 0, 0);
16290 sqlite3_series_init(p->db, 0, 0);
16291 #ifndef SQLITE_SHELL_FIDDLE
16292 sqlite3_fileio_init(p->db, 0, 0);
16293 sqlite3_completion_init(p->db, 0, 0);
16294 #endif
16295 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16296 sqlite3_dbdata_init(p->db, 0, 0);
@@ -19272,19 +19416,19 @@
19416 }
19417 }else
19418 #endif
19419
19420 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19421 && !defined(SQLITE_SHELL_FIDDLE)
19422 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19423 open_db(p, 0);
19424 failIfSafeMode(p, "cannot run .archive in safe mode");
19425 rc = arDotCommand(p, 0, azArg, nArg);
19426 }else
19427 #endif
19428
19429 #ifndef SQLITE_SHELL_FIDDLE
19430 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
19431 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
19432 ){
19433 const char *zDestFile = 0;
19434 const char *zDb = 0;
@@ -19349,11 +19493,11 @@
19493 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19494 rc = 1;
19495 }
19496 close_db(pDest);
19497 }else
19498 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19499
19500 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19501 if( nArg==2 ){
19502 bail_on_error = booleanValue(azArg[1]);
19503 }else{
@@ -19380,11 +19524,11 @@
19524 */
19525 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19526 test_breakpoint();
19527 }else
19528
19529 #ifndef SQLITE_SHELL_FIDDLE
19530 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19531 failIfSafeMode(p, "cannot run .cd in safe mode");
19532 if( nArg==2 ){
19533 #if defined(_WIN32) || defined(WIN32)
19534 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19400,11 +19544,11 @@
19544 }else{
19545 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
19546 rc = 1;
19547 }
19548 }else
19549 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19550
19551 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19552 if( nArg==2 ){
19553 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19554 }else{
@@ -19411,11 +19555,11 @@
19555 raw_printf(stderr, "Usage: .changes on|off\n");
19556 rc = 1;
19557 }
19558 }else
19559
19560 #ifndef SQLITE_SHELL_FIDDLE
19561 /* Cancel output redirection, if it is currently set (by .testcase)
19562 ** Then read the content of the testcase-out.txt file and compare against
19563 ** azArg[1]. If there are differences, report an error and exit.
19564 */
19565 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19436,23 +19580,23 @@
19580 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
19581 p->nCheck++;
19582 }
19583 sqlite3_free(zRes);
19584 }else
19585 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19586
19587 #ifndef SQLITE_SHELL_FIDDLE
19588 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19589 failIfSafeMode(p, "cannot run .clone in safe mode");
19590 if( nArg==2 ){
19591 tryToClone(p, azArg[1]);
19592 }else{
19593 raw_printf(stderr, "Usage: .clone FILENAME\n");
19594 rc = 1;
19595 }
19596 }else
19597 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19598
19599 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19600 if( nArg==1 ){
19601 /* List available connections */
19602 int i;
@@ -19737,11 +19881,11 @@
19881 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19882 rc = 1;
19883 }
19884 }else
19885
19886 #ifndef SQLITE_SHELL_FIDDLE
19887 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19888 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19889 rc = 2;
19890 }else
19891 #endif
@@ -19997,11 +20141,11 @@
20141 }else{
20142 showHelp(p->out, 0);
20143 }
20144 }else
20145
20146 #ifndef SQLITE_SHELL_FIDDLE
20147 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
20148 char *zTable = 0; /* Insert data into this table */
20149 char *zSchema = 0; /* within this schema (may default to "main") */
20150 char *zFile = 0; /* Name of file to extra content from */
20151 sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20288,11 +20432,11 @@
20432 utf8_printf(p->out,
20433 "Added %d rows with %d errors using %d lines of input\n",
20434 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
20435 }
20436 }else
20437 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
20438
20439 #ifndef SQLITE_UNTESTABLE
20440 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20441 char *zSql;
20442 char *zCollist = 0;
@@ -20478,11 +20622,11 @@
20622 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20623 open_db(p, 0);
20624 lintDotCommand(p, azArg, nArg);
20625 }else
20626
20627 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
20628 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20629 const char *zFile, *zProc;
20630 char *zErrMsg = 0;
20631 failIfSafeMode(p, "cannot run .load in safe mode");
20632 if( nArg<2 ){
@@ -20500,11 +20644,11 @@
20644 rc = 1;
20645 }
20646 }else
20647 #endif
20648
20649 #ifndef SQLITE_SHELL_FIDDLE
20650 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20651 failIfSafeMode(p, "cannot run .log in safe mode");
20652 if( nArg!=2 ){
20653 raw_printf(stderr, "Usage: .log FILENAME\n");
20654 rc = 1;
@@ -20637,11 +20781,11 @@
20781 rc = 1;
20782 }
20783 p->cMode = p->mode;
20784 }else
20785
20786 #ifndef SQLITE_SHELL_FIDDLE
20787 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20788 if( nArg!=2 ){
20789 raw_printf(stderr, "Usage: .nonce NONCE\n");
20790 rc = 1;
20791 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20652,11 +20796,11 @@
20796 p->bSafeMode = 0;
20797 return 0; /* Return immediately to bypass the safe mode reset
20798 ** at the end of this procedure */
20799 }
20800 }else
20801 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
20802
20803 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20804 if( nArg==2 ){
20805 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20806 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20674,11 +20818,11 @@
20818 int openMode = SHELL_OPEN_UNSPEC;
20819
20820 /* Check for command-line arguments */
20821 for(iName=1; iName<nArg; iName++){
20822 const char *z = azArg[iName];
20823 #ifndef SQLITE_SHELL_FIDDLE
20824 if( optionMatch(z,"new") ){
20825 newFlag = 1;
20826 #ifdef SQLITE_HAVE_ZLIB
20827 }else if( optionMatch(z, "zip") ){
20828 openMode = SHELL_OPEN_ZIPFILE;
@@ -20696,11 +20840,11 @@
20840 openMode = SHELL_OPEN_HEXDB;
20841 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20842 p->szMax = integerValue(azArg[++iName]);
20843 #endif /* SQLITE_OMIT_DESERIALIZE */
20844 }else
20845 #endif /* !SQLITE_SHELL_FIDDLE */
20846 if( z[0]=='-' ){
20847 utf8_printf(stderr, "unknown option: %s\n", z);
20848 rc = 1;
20849 goto meta_command_exit;
20850 }else if( zFN ){
@@ -20724,11 +20868,11 @@
20868 p->szMax = 0;
20869
20870 /* If a filename is specified, try to open it first */
20871 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20872 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20873 #ifndef SQLITE_SHELL_FIDDLE
20874 if( p->bSafeMode
20875 && p->openMode!=SHELL_OPEN_HEXDB
20876 && zFN
20877 && strcmp(zFN,":memory:")!=0
20878 ){
@@ -20757,11 +20901,11 @@
20901 p->pAuxDb->zDbFilename = 0;
20902 open_db(p, 0);
20903 }
20904 }else
20905
20906 #ifndef SQLITE_SHELL_FIDDLE
20907 if( (c=='o'
20908 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20909 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20910 ){
20911 char *zFile = 0;
@@ -20873,11 +21017,11 @@
21017 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
21018 }
21019 }
21020 sqlite3_free(zFile);
21021 }else
21022 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21023
21024 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
21025 open_db(p,0);
21026 if( nArg<=1 ) goto parameter_syntax_error;
21027
@@ -21043,17 +21187,17 @@
21187 if( nArg >= 3) {
21188 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
21189 }
21190 }else
21191
21192 #ifndef SQLITE_SHELL_FIDDLE
21193 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
21194 rc = 2;
21195 }else
21196 #endif
21197
21198 #ifndef SQLITE_SHELL_FIDDLE
21199 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
21200 FILE *inSaved = p->in;
21201 int savedLineno = p->lineno;
21202 failIfSafeMode(p, "cannot run .read in safe mode");
21203 if( nArg!=2 ){
@@ -21084,13 +21228,13 @@
21228 fclose(p->in);
21229 }
21230 p->in = inSaved;
21231 p->lineno = savedLineno;
21232 }else
21233 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21234
21235 #ifndef SQLITE_SHELL_FIDDLE
21236 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
21237 const char *zSrcFile;
21238 const char *zDb;
21239 sqlite3 *pSrc;
21240 sqlite3_backup *pBackup;
@@ -21138,11 +21282,11 @@
21282 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21283 rc = 1;
21284 }
21285 close_db(pSrc);
21286 }else
21287 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21288
21289 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21290 if( nArg==2 ){
21291 p->scanstatsOn = (u8)booleanValue(azArg[1]);
21292 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21764,11 +21908,11 @@
21908 shell_exec(p, zSql, 0);
21909 }
21910 sqlite3_free(zSql);
21911 }else
21912
21913 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21914 if( c=='s'
21915 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21916 ){
21917 char *zCmd;
21918 int i, x;
@@ -21785,11 +21929,11 @@
21929 }
21930 x = zCmd!=0 ? system(zCmd) : 1;
21931 sqlite3_free(zCmd);
21932 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21933 }else
21934 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
21935
21936 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21937 static const char *azBool[] = { "off", "on", "trigger", "full"};
21938 const char *zOut;
21939 int i;
@@ -21965,11 +22109,11 @@
22109
22110 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
22111 sqlite3_free(azResult);
22112 }else
22113
22114 #ifndef SQLITE_SHELL_FIDDLE
22115 /* Begin redirecting output to the file "testcase-out.txt" */
22116 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
22117 output_reset(p);
22118 p->out = output_file_open("testcase-out.txt", 0);
22119 if( p->out==0 ){
@@ -21979,11 +22123,11 @@
22123 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
22124 }else{
22125 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
22126 }
22127 }else
22128 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
22129
22130 #ifndef SQLITE_UNTESTABLE
22131 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
22132 static const struct {
22133 const char *zCtrlName; /* Name of a test-control option */
@@ -22651,11 +22795,11 @@
22795
22796 static void echo_group_input(ShellState *p, const char *zDo){
22797 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
22798 }
22799
22800 #ifdef SQLITE_SHELL_FIDDLE
22801 /*
22802 ** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
22803 ** because we need the global shellState and cannot access it from that function
22804 ** without moving lots of code around (creating a larger/messier diff).
22805 */
@@ -22682,11 +22826,11 @@
22826 shell_check_oom(zLine);
22827 memcpy(zLine, zBegin, (size_t)nZ);
22828 zLine[nZ] = 0;
22829 return zLine;
22830 }
22831 #endif /* SQLITE_SHELL_FIDDLE */
22832
22833 /*
22834 ** Read input from *in and process it. If *in==0 then input
22835 ** is interactive - the user is typing it it. Otherwise, input
22836 ** is coming from a file or device. A prompt is issued and history
@@ -23065,11 +23209,11 @@
23209 # else
23210 # define SQLITE_SHELL_IS_UTF8 (1)
23211 # endif
23212 #endif
23213
23214 #ifdef SQLITE_SHELL_FIDDLE
23215 # define main fiddle_main
23216 #endif
23217
23218 #if SQLITE_SHELL_IS_UTF8
23219 int SQLITE_CDECL main(int argc, char **argv){
@@ -23079,11 +23223,11 @@
23223 #endif
23224 #ifdef SQLITE_DEBUG
23225 sqlite3_int64 mem_main_enter = sqlite3_memory_used();
23226 #endif
23227 char *zErrMsg = 0;
23228 #ifdef SQLITE_SHELL_FIDDLE
23229 # define data shellState
23230 #else
23231 ShellState data;
23232 #endif
23233 const char *zInitFile = 0;
@@ -23099,11 +23243,11 @@
23243 int argcToFree = 0;
23244 #endif
23245
23246 setBinaryMode(stdin, 0);
23247 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23248 #ifdef SQLITE_SHELL_FIDDLE
23249 stdin_is_interactive = 0;
23250 stdout_is_console = 1;
23251 #else
23252 stdin_is_interactive = isatty(0);
23253 stdout_is_console = isatty(1);
@@ -23361,11 +23505,11 @@
23505 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
23506 return 1;
23507 #endif
23508 }
23509 data.out = stdout;
23510 #ifndef SQLITE_SHELL_FIDDLE
23511 sqlite3_appendvfs_init(0,0,0);
23512 #endif
23513
23514 /* Go ahead and open the database file if it already exists. If the
23515 ** file does not exist, delay opening it. This prevents empty database
@@ -23629,11 +23773,11 @@
23773 }else{
23774 data.in = stdin;
23775 rc = process_input(&data);
23776 }
23777 }
23778 #ifndef SQLITE_SHELL_FIDDLE
23779 /* In WASM mode we have to leave the db state in place so that
23780 ** client code can "push" SQL into it after this call returns. */
23781 free(azCmd);
23782 set_table_name(&data, 0);
23783 if( data.db ){
@@ -23664,16 +23808,16 @@
23808 if( sqlite3_memory_used()>mem_main_enter ){
23809 utf8_printf(stderr, "Memory leaked: %u bytes\n",
23810 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
23811 }
23812 #endif
23813 #endif /* !SQLITE_SHELL_FIDDLE */
23814 return rc;
23815 }
23816
23817
23818 #ifdef SQLITE_SHELL_FIDDLE
23819 /* Only for emcc experimentation purposes. */
23820 int fiddle_experiment(int a,int b){
23821 return a + b;
23822 }
23823
@@ -23790,6 +23934,6 @@
23934 shellState.wasm.zPos = zSql;
23935 process_input(&shellState);
23936 memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23937 }
23938 }
23939 #endif /* SQLITE_SHELL_FIDDLE */
23940
+1008 -640
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.39.2. By combining all the individual C code files into this
3
+** version 3.40.0. 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.
@@ -450,13 +450,13 @@
450450
**
451451
** See also: [sqlite3_libversion()],
452452
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453
** [sqlite_version()] and [sqlite_source_id()].
454454
*/
455
-#define SQLITE_VERSION "3.39.2"
456
-#define SQLITE_VERSION_NUMBER 3039002
457
-#define SQLITE_SOURCE_ID "2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21"
455
+#define SQLITE_VERSION "3.40.0"
456
+#define SQLITE_VERSION_NUMBER 3040000
457
+#define SQLITE_SOURCE_ID "2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -3728,10 +3728,13 @@
37283728
**
37293729
** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
37303730
** <dd>The database is opened [shared cache] enabled, overriding
37313731
** the default shared cache setting provided by
37323732
** [sqlite3_enable_shared_cache()].)^
3733
+** The [use of shared cache mode is discouraged] and hence shared cache
3734
+** capabilities may be omitted from many builds of SQLite. In such cases,
3735
+** this option is a no-op.
37333736
**
37343737
** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
37353738
** <dd>The database is opened [shared cache] disabled, overriding
37363739
** the default shared cache setting provided by
37373740
** [sqlite3_enable_shared_cache()].)^
@@ -3743,11 +3746,11 @@
37433746
** connection as soon as the connection is created. In addition to setting
37443747
** the extended result code mode, this flag also causes [sqlite3_open_v2()]
37453748
** to return an extended result code.</dd>
37463749
**
37473750
** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3748
-** <dd>The database filename is not allowed to be a symbolic link</dd>
3751
+** <dd>The database filename is not allowed to contain a symbolic link</dd>
37493752
** </dl>)^
37503753
**
37513754
** If the 3rd parameter to sqlite3_open_v2() is not one of the
37523755
** required combinations shown above optionally combined with other
37533756
** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6769,11 +6772,11 @@
67696772
**
67706773
** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
67716774
** function C that is invoked prior to each autovacuum of the database
67726775
** file. ^The callback is passed a copy of the generic data pointer (P),
67736776
** the schema-name of the attached database that is being autovacuumed,
6774
-** the the size of the database file in pages, the number of free pages,
6777
+** the size of the database file in pages, the number of free pages,
67756778
** and the number of bytes per page, respectively. The callback should
67766779
** return the number of free pages that should be removed by the
67776780
** autovacuum. ^If the callback returns zero, then no autovacuum happens.
67786781
** ^If the value returned is greater than or equal to the number of
67796782
** free pages, then a complete autovacuum happens.
@@ -6889,10 +6892,15 @@
68896892
**
68906893
** ^(This routine enables or disables the sharing of the database cache
68916894
** and schema data structures between [database connection | connections]
68926895
** to the same database. Sharing is enabled if the argument is true
68936896
** and disabled if the argument is false.)^
6897
+**
6898
+** This interface is omitted if SQLite is compiled with
6899
+** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
6900
+** compile-time option is recommended because the
6901
+** [use of shared cache mode is discouraged].
68946902
**
68956903
** ^Cache sharing is enabled and disabled for an entire process.
68966904
** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
68976905
** In prior versions of SQLite,
68986906
** sharing was enabled or disabled for each thread separately.
@@ -6988,11 +6996,11 @@
69886996
** ^Setting the heap limits to zero disables the heap limiter mechanism.
69896997
**
69906998
** ^The soft heap limit may not be greater than the hard heap limit.
69916999
** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
69927000
** is invoked with a value of N that is greater than the hard heap limit,
6993
-** the the soft heap limit is set to the value of the hard heap limit.
7001
+** the soft heap limit is set to the value of the hard heap limit.
69947002
** ^The soft heap limit is automatically enabled whenever the hard heap
69957003
** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
69967004
** the soft heap limit is outside the range of 1..N, then the soft heap
69977005
** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
69987006
** hard heap limit is enabled makes the soft heap limit equal to the
@@ -9283,11 +9291,11 @@
92839291
** sqlite3_backup_init() is called and before the corresponding call to
92849292
** sqlite3_backup_finish(). SQLite does not currently check to see
92859293
** if the application incorrectly accesses the destination [database connection]
92869294
** and so no error code is reported, but the operations may malfunction
92879295
** nevertheless. Use of the destination database connection while a
9288
-** backup is in progress might also also cause a mutex deadlock.
9296
+** backup is in progress might also cause a mutex deadlock.
92899297
**
92909298
** If running in [shared cache mode], the application must
92919299
** guarantee that the shared cache used by the destination database
92929300
** is not accessed while the backup is running. In practice this means
92939301
** that the application must guarantee that the disk file being
@@ -9711,11 +9719,11 @@
97119719
** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
97129720
** meaning of each of these checkpoint modes.
97139721
*/
97149722
#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
97159723
#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9716
-#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9724
+#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
97179725
#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
97189726
97199727
/*
97209728
** CAPI3REF: Virtual Table Interface Configuration
97219729
**
@@ -13142,10 +13150,15 @@
1314213150
/******** End of fts5.h *********/
1314313151
1314413152
/************** End of sqlite3.h *********************************************/
1314513153
/************** Continuing where we left off in sqliteInt.h ******************/
1314613154
13155
+/*
13156
+** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
13157
+*/
13158
+#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1
13159
+
1314713160
/*
1314813161
** Include the configuration header output by 'configure' if we're using the
1314913162
** autoconf-based build
1315013163
*/
1315113164
#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
@@ -15553,67 +15566,67 @@
1555315566
#define OP_Checkpoint 3
1555415567
#define OP_JournalMode 4
1555515568
#define OP_Vacuum 5
1555615569
#define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
1555715570
#define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */
15558
-#define OP_Goto 8 /* jump */
15559
-#define OP_Gosub 9 /* jump */
15560
-#define OP_InitCoroutine 10 /* jump */
15561
-#define OP_Yield 11 /* jump */
15562
-#define OP_MustBeInt 12 /* jump */
15563
-#define OP_Jump 13 /* jump */
15564
-#define OP_Once 14 /* jump */
15565
-#define OP_If 15 /* jump */
15566
-#define OP_IfNot 16 /* jump */
15567
-#define OP_IsNullOrType 17 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15568
-#define OP_IfNullRow 18 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15571
+#define OP_Init 8 /* jump, synopsis: Start at P2 */
15572
+#define OP_Goto 9 /* jump */
15573
+#define OP_Gosub 10 /* jump */
15574
+#define OP_InitCoroutine 11 /* jump */
15575
+#define OP_Yield 12 /* jump */
15576
+#define OP_MustBeInt 13 /* jump */
15577
+#define OP_Jump 14 /* jump */
15578
+#define OP_Once 15 /* jump */
15579
+#define OP_If 16 /* jump */
15580
+#define OP_IfNot 17 /* jump */
15581
+#define OP_IsNullOrType 18 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
1556915582
#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15570
-#define OP_SeekLT 20 /* jump, synopsis: key=r[P3@P4] */
15571
-#define OP_SeekLE 21 /* jump, synopsis: key=r[P3@P4] */
15572
-#define OP_SeekGE 22 /* jump, synopsis: key=r[P3@P4] */
15573
-#define OP_SeekGT 23 /* jump, synopsis: key=r[P3@P4] */
15574
-#define OP_IfNotOpen 24 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15575
-#define OP_IfNoHope 25 /* jump, synopsis: key=r[P3@P4] */
15576
-#define OP_NoConflict 26 /* jump, synopsis: key=r[P3@P4] */
15577
-#define OP_NotFound 27 /* jump, synopsis: key=r[P3@P4] */
15578
-#define OP_Found 28 /* jump, synopsis: key=r[P3@P4] */
15579
-#define OP_SeekRowid 29 /* jump, synopsis: intkey=r[P3] */
15580
-#define OP_NotExists 30 /* jump, synopsis: intkey=r[P3] */
15581
-#define OP_Last 31 /* jump */
15582
-#define OP_IfSmaller 32 /* jump */
15583
-#define OP_SorterSort 33 /* jump */
15584
-#define OP_Sort 34 /* jump */
15585
-#define OP_Rewind 35 /* jump */
15586
-#define OP_SorterNext 36 /* jump */
15587
-#define OP_Prev 37 /* jump */
15588
-#define OP_Next 38 /* jump */
15589
-#define OP_IdxLE 39 /* jump, synopsis: key=r[P3@P4] */
15590
-#define OP_IdxGT 40 /* jump, synopsis: key=r[P3@P4] */
15591
-#define OP_IdxLT 41 /* jump, synopsis: key=r[P3@P4] */
15592
-#define OP_IdxGE 42 /* jump, synopsis: key=r[P3@P4] */
15583
+#define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15584
+#define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
15585
+#define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
15586
+#define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
15587
+#define OP_SeekGT 24 /* jump, synopsis: key=r[P3@P4] */
15588
+#define OP_IfNotOpen 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15589
+#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
15590
+#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
15591
+#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
15592
+#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
15593
+#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
15594
+#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
15595
+#define OP_Last 32 /* jump */
15596
+#define OP_IfSmaller 33 /* jump */
15597
+#define OP_SorterSort 34 /* jump */
15598
+#define OP_Sort 35 /* jump */
15599
+#define OP_Rewind 36 /* jump */
15600
+#define OP_SorterNext 37 /* jump */
15601
+#define OP_Prev 38 /* jump */
15602
+#define OP_Next 39 /* jump */
15603
+#define OP_IdxLE 40 /* jump, synopsis: key=r[P3@P4] */
15604
+#define OP_IdxGT 41 /* jump, synopsis: key=r[P3@P4] */
15605
+#define OP_IdxLT 42 /* jump, synopsis: key=r[P3@P4] */
1559315606
#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
1559415607
#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15595
-#define OP_RowSetRead 45 /* jump, synopsis: r[P3]=rowset(P1) */
15596
-#define OP_RowSetTest 46 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15597
-#define OP_Program 47 /* jump */
15598
-#define OP_FkIfZero 48 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15599
-#define OP_IfPos 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15608
+#define OP_IdxGE 45 /* jump, synopsis: key=r[P3@P4] */
15609
+#define OP_RowSetRead 46 /* jump, synopsis: r[P3]=rowset(P1) */
15610
+#define OP_RowSetTest 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15611
+#define OP_Program 48 /* jump */
15612
+#define OP_FkIfZero 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
1560015613
#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
1560115614
#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
1560215615
#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
1560315616
#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
1560415617
#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
1560515618
#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
1560615619
#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
1560715620
#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
1560815621
#define OP_ElseEq 58 /* jump, same as TK_ESCAPE */
15609
-#define OP_IfNotZero 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15610
-#define OP_DecrJumpZero 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15611
-#define OP_IncrVacuum 61 /* jump */
15612
-#define OP_VNext 62 /* jump */
15613
-#define OP_Filter 63 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
15614
-#define OP_Init 64 /* jump, synopsis: Start at P2 */
15622
+#define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15623
+#define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15624
+#define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15625
+#define OP_IncrVacuum 62 /* jump */
15626
+#define OP_VNext 63 /* jump */
15627
+#define OP_Filter 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
1561515628
#define OP_PureFunc 65 /* synopsis: r[P3]=func(r[P2@NP]) */
1561615629
#define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */
1561715630
#define OP_Return 67
1561815631
#define OP_EndCoroutine 68
1561915632
#define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */
@@ -15745,17 +15758,17 @@
1574515758
#define OPFLG_IN3 0x08 /* in3: P3 is an input */
1574615759
#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
1574715760
#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
1574815761
#define OPFLG_INITIALIZER {\
1574915762
/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
15750
-/* 8 */ 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03,\
15751
-/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x09, 0x09, 0x09, 0x09,\
15752
-/* 24 */ 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,\
15763
+/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
15764
+/* 16 */ 0x03, 0x03, 0x03, 0x12, 0x01, 0x09, 0x09, 0x09,\
15765
+/* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
1575315766
/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15754
-/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\
15755
-/* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15756
-/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\
15767
+/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\
15768
+/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15769
+/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\
1575715770
/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
1575815771
/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
1575915772
/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
1576015773
/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\
1576115774
/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\
@@ -15857,10 +15870,11 @@
1585715870
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
1585815871
SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
1585915872
SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
1586015873
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
1586115874
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15875
+SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetLastOp(Vdbe*);
1586215876
SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
1586315877
SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
1586415878
SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
1586515879
SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
1586615880
SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
@@ -16741,10 +16755,11 @@
1674116755
void *pMiddle; /* First byte past end of full-size buffers and
1674216756
** the first byte of LOOKASIDE_SMALL buffers */
1674316757
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
1674416758
void *pStart; /* First byte of available memory space */
1674516759
void *pEnd; /* First byte past end of available space */
16760
+ void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
1674616761
};
1674716762
struct LookasideSlot {
1674816763
LookasideSlot *pNext; /* Next buffer in the list of free buffers */
1674916764
};
1675016765
@@ -18189,11 +18204,11 @@
1818918204
#define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
1819018205
#define EP_Skip 0x002000 /* Operator does not contribute to affinity */
1819118206
#define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
1819218207
#define EP_Win 0x008000 /* Contains window functions */
1819318208
#define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18194
-#define EP_MemToken 0x020000 /* Need to sqlite3DbFree() Expr.zToken */
18209
+ /* 0x020000 // Available for reuse */
1819518210
#define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
1819618211
#define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
1819718212
#define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
1819818213
#define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */
1819918214
#define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */
@@ -19667,10 +19682,11 @@
1966719682
SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
1966819683
SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
1966919684
SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
1967019685
SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
1967119686
SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
19687
+SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3*, void*);
1967219688
SQLITE_PRIVATE int sqlite3MallocSize(const void*);
1967319689
SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
1967419690
SQLITE_PRIVATE void *sqlite3PageMalloc(int);
1967519691
SQLITE_PRIVATE void sqlite3PageFree(void*);
1967619692
SQLITE_PRIVATE void sqlite3MemSetDefault(void);
@@ -20191,10 +20207,11 @@
2019120207
SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
2019220208
SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
2019320209
SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
2019420210
SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
2019520211
SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
20212
+SQLITE_PRIVATE i64 sqlite3RealToI64(double);
2019620213
SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*);
2019720214
SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
2019820215
SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
2019920216
SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
2020020217
SQLITE_PRIVATE int sqlite3Atoi(const char*);
@@ -21637,13 +21654,10 @@
2163721654
"OMIT_WSD",
2163821655
#endif
2163921656
#ifdef SQLITE_OMIT_XFER_OPT
2164021657
"OMIT_XFER_OPT",
2164121658
#endif
21642
-#ifdef SQLITE_PCACHE_SEPARATE_HEADER
21643
- "PCACHE_SEPARATE_HEADER",
21644
-#endif
2164521659
#ifdef SQLITE_PERFORMANCE_TRACE
2164621660
"PERFORMANCE_TRACE",
2164721661
#endif
2164821662
#ifdef SQLITE_POWERSAFE_OVERWRITE
2164921663
# if SQLITE_POWERSAFE_OVERWRITE != 1
@@ -22592,11 +22606,11 @@
2259222606
** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
2259322607
** is really a pointer to an instance of this structure.
2259422608
*/
2259522609
struct Vdbe {
2259622610
sqlite3 *db; /* The database connection that owns this statement */
22597
- Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
22611
+ Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */
2259822612
Parse *pParse; /* Parsing context used to create this Vdbe */
2259922613
ynVar nVar; /* Number of entries in aVar[] */
2260022614
int nMem; /* Number of memory locations currently allocated */
2260122615
int nCursor; /* Number of slots in apCsr[] */
2260222616
u32 cacheCtr; /* VdbeCursor row cache generation counter */
@@ -23150,10 +23164,12 @@
2315023164
int i; /* Used to iterate through schemas */
2315123165
int nByte = 0; /* Used to accumulate return value */
2315223166
2315323167
sqlite3BtreeEnterAll(db);
2315423168
db->pnBytesFreed = &nByte;
23169
+ assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23170
+ db->lookaside.pEnd = db->lookaside.pStart;
2315523171
for(i=0; i<db->nDb; i++){
2315623172
Schema *pSchema = db->aDb[i].pSchema;
2315723173
if( ALWAYS(pSchema!=0) ){
2315823174
HashElem *p;
2315923175
@@ -23175,10 +23191,11 @@
2317523191
sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
2317623192
}
2317723193
}
2317823194
}
2317923195
db->pnBytesFreed = 0;
23196
+ db->lookaside.pEnd = db->lookaside.pTrueEnd;
2318023197
sqlite3BtreeLeaveAll(db);
2318123198
2318223199
*pHighwater = 0;
2318323200
*pCurrent = nByte;
2318423201
break;
@@ -23192,13 +23209,16 @@
2319223209
case SQLITE_DBSTATUS_STMT_USED: {
2319323210
struct Vdbe *pVdbe; /* Used to iterate through VMs */
2319423211
int nByte = 0; /* Used to accumulate return value */
2319523212
2319623213
db->pnBytesFreed = &nByte;
23197
- for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
23214
+ assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23215
+ db->lookaside.pEnd = db->lookaside.pStart;
23216
+ for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
2319823217
sqlite3VdbeDelete(pVdbe);
2319923218
}
23219
+ db->lookaside.pEnd = db->lookaside.pTrueEnd;
2320023220
db->pnBytesFreed = 0;
2320123221
2320223222
*pHighwater = 0; /* IMP: R-64479-57858 */
2320323223
*pCurrent = nByte;
2320423224
@@ -23530,11 +23550,11 @@
2353023550
X1 = 36525*(Y+4716)/100;
2353123551
X2 = 306001*(M+1)/10000;
2353223552
p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
2353323553
p->validJD = 1;
2353423554
if( p->validHMS ){
23535
- p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
23555
+ p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
2353623556
if( p->validTZ ){
2353723557
p->iJD -= p->tz*60000;
2353823558
p->validYMD = 0;
2353923559
p->validHMS = 0;
2354023560
p->validTZ = 0;
@@ -24039,11 +24059,11 @@
2403924059
** weekday N where 0==Sunday, 1==Monday, and so forth. If the
2404024060
** date is already on the appropriate weekday, this is a no-op.
2404124061
*/
2404224062
if( sqlite3_strnicmp(z, "weekday ", 8)==0
2404324063
&& sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
24044
- && (n=(int)r)==r && n>=0 && r<7 ){
24064
+ && r>=0.0 && r<7.0 && (n=(int)r)==r ){
2404524065
sqlite3_int64 Z;
2404624066
computeYMD_HMS(p);
2404724067
p->validTZ = 0;
2404824068
p->validJD = 0;
2404924069
computeJD(p);
@@ -24837,10 +24857,11 @@
2483724857
DO_OS_MALLOC_TEST(0);
2483824858
/* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
2483924859
** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
2484024860
** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
2484124861
** reaching the VFS. */
24862
+ assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) );
2484224863
rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
2484324864
assert( rc==SQLITE_OK || pFile->pMethods==0 );
2484424865
return rc;
2484524866
}
2484624867
SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
@@ -29102,11 +29123,11 @@
2910229123
/*
2910329124
** TRUE if p is a lookaside memory allocation from db
2910429125
*/
2910529126
#ifndef SQLITE_OMIT_LOOKASIDE
2910629127
static int isLookaside(sqlite3 *db, const void *p){
29107
- return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
29128
+ return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
2910829129
}
2910929130
#else
2911029131
#define isLookaside(A,B) 0
2911129132
#endif
2911229133
@@ -29126,22 +29147,20 @@
2912629147
#endif
2912729148
}
2912829149
SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){
2912929150
assert( p!=0 );
2913029151
#ifdef SQLITE_DEBUG
29131
- if( db==0 || !isLookaside(db,p) ){
29132
- if( db==0 ){
29133
- assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29134
- assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29135
- }else{
29136
- assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29137
- assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29138
- }
29152
+ if( db==0 ){
29153
+ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29154
+ assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29155
+ }else if( !isLookaside(db,p) ){
29156
+ assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29157
+ assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2913929158
}
2914029159
#endif
2914129160
if( db ){
29142
- if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29161
+ if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
2914329162
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
2914429163
if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
2914529164
assert( sqlite3_mutex_held(db->mutex) );
2914629165
return LOOKASIDE_SMALL;
2914729166
}
@@ -29193,18 +29212,15 @@
2919329212
*/
2919429213
SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
2919529214
assert( db==0 || sqlite3_mutex_held(db->mutex) );
2919629215
assert( p!=0 );
2919729216
if( db ){
29198
- if( db->pnBytesFreed ){
29199
- measureAllocationSize(db, p);
29200
- return;
29201
- }
2920229217
if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
2920329218
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
2920429219
if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
2920529220
LookasideSlot *pBuf = (LookasideSlot*)p;
29221
+ assert( db->pnBytesFreed==0 );
2920629222
#ifdef SQLITE_DEBUG
2920729223
memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
2920829224
#endif
2920929225
pBuf->pNext = db->lookaside.pSmallFree;
2921029226
db->lookaside.pSmallFree = pBuf;
@@ -29211,24 +29227,66 @@
2921129227
return;
2921229228
}
2921329229
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
2921429230
if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
2921529231
LookasideSlot *pBuf = (LookasideSlot*)p;
29232
+ assert( db->pnBytesFreed==0 );
2921629233
#ifdef SQLITE_DEBUG
2921729234
memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
2921829235
#endif
2921929236
pBuf->pNext = db->lookaside.pFree;
2922029237
db->lookaside.pFree = pBuf;
2922129238
return;
2922229239
}
2922329240
}
29241
+ if( db->pnBytesFreed ){
29242
+ measureAllocationSize(db, p);
29243
+ return;
29244
+ }
2922429245
}
2922529246
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2922629247
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2922729248
assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
2922829249
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
2922929250
sqlite3_free(p);
29251
+}
29252
+SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
29253
+ assert( db!=0 );
29254
+ assert( sqlite3_mutex_held(db->mutex) );
29255
+ assert( p!=0 );
29256
+ if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29257
+#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29258
+ if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29259
+ LookasideSlot *pBuf = (LookasideSlot*)p;
29260
+ assert( db->pnBytesFreed==0 );
29261
+#ifdef SQLITE_DEBUG
29262
+ memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
29263
+#endif
29264
+ pBuf->pNext = db->lookaside.pSmallFree;
29265
+ db->lookaside.pSmallFree = pBuf;
29266
+ return;
29267
+ }
29268
+#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29269
+ if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29270
+ LookasideSlot *pBuf = (LookasideSlot*)p;
29271
+ assert( db->pnBytesFreed==0 );
29272
+#ifdef SQLITE_DEBUG
29273
+ memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29274
+#endif
29275
+ pBuf->pNext = db->lookaside.pFree;
29276
+ db->lookaside.pFree = pBuf;
29277
+ return;
29278
+ }
29279
+ }
29280
+ if( db->pnBytesFreed ){
29281
+ measureAllocationSize(db, p);
29282
+ return;
29283
+ }
29284
+ assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29285
+ assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29286
+ sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29287
+ sqlite3_free(p);
2923029288
}
2923129289
SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
2923229290
assert( db==0 || sqlite3_mutex_held(db->mutex) );
2923329291
if( p ) sqlite3DbFreeNN(db, p);
2923429292
}
@@ -29561,12 +29619,17 @@
2956129619
if( db->nVdbeExec>0 ){
2956229620
AtomicStore(&db->u1.isInterrupted, 1);
2956329621
}
2956429622
DisableLookaside;
2956529623
if( db->pParse ){
29624
+ Parse *pParse;
2956629625
sqlite3ErrorMsg(db->pParse, "out of memory");
2956729626
db->pParse->rc = SQLITE_NOMEM_BKPT;
29627
+ for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
29628
+ pParse->nErr++;
29629
+ pParse->rc = SQLITE_NOMEM;
29630
+ }
2956829631
}
2956929632
}
2957029633
return 0;
2957129634
}
2957229635
@@ -32332,20 +32395,45 @@
3233232395
3233332396
/* All threads share a single random number generator.
3233432397
** This structure is the current state of the generator.
3233532398
*/
3233632399
static SQLITE_WSD struct sqlite3PrngType {
32337
- unsigned char isInit; /* True if initialized */
32338
- unsigned char i, j; /* State variables */
32339
- unsigned char s[256]; /* State variables */
32400
+ u32 s[16]; /* 64 bytes of chacha20 state */
32401
+ u8 out[64]; /* Output bytes */
32402
+ u8 n; /* Output bytes remaining */
3234032403
} sqlite3Prng;
3234132404
32405
+
32406
+/* The RFC-7539 ChaCha20 block function
32407
+*/
32408
+#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
32409
+#define QR(a, b, c, d) ( \
32410
+ a += b, d ^= a, d = ROTL(d,16), \
32411
+ c += d, b ^= c, b = ROTL(b,12), \
32412
+ a += b, d ^= a, d = ROTL(d, 8), \
32413
+ c += d, b ^= c, b = ROTL(b, 7))
32414
+static void chacha_block(u32 *out, const u32 *in){
32415
+ int i;
32416
+ u32 x[16];
32417
+ memcpy(x, in, 64);
32418
+ for(i=0; i<10; i++){
32419
+ QR(x[0], x[4], x[ 8], x[12]);
32420
+ QR(x[1], x[5], x[ 9], x[13]);
32421
+ QR(x[2], x[6], x[10], x[14]);
32422
+ QR(x[3], x[7], x[11], x[15]);
32423
+ QR(x[0], x[5], x[10], x[15]);
32424
+ QR(x[1], x[6], x[11], x[12]);
32425
+ QR(x[2], x[7], x[ 8], x[13]);
32426
+ QR(x[3], x[4], x[ 9], x[14]);
32427
+ }
32428
+ for(i=0; i<16; i++) out[i] = x[i]+in[i];
32429
+}
32430
+
3234232431
/*
3234332432
** Return N random bytes.
3234432433
*/
3234532434
SQLITE_API void sqlite3_randomness(int N, void *pBuf){
32346
- unsigned char t;
3234732435
unsigned char *zBuf = pBuf;
3234832436
3234932437
/* The "wsdPrng" macro will resolve to the pseudo-random number generator
3235032438
** state vector. If writable static data is unsupported on the target,
3235132439
** we have to locate the state vector at run-time. In the more common
@@ -32371,57 +32459,50 @@
3237132459
mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
3237232460
#endif
3237332461
3237432462
sqlite3_mutex_enter(mutex);
3237532463
if( N<=0 || pBuf==0 ){
32376
- wsdPrng.isInit = 0;
32464
+ wsdPrng.s[0] = 0;
3237732465
sqlite3_mutex_leave(mutex);
3237832466
return;
3237932467
}
3238032468
3238132469
/* Initialize the state of the random number generator once,
32382
- ** the first time this routine is called. The seed value does
32383
- ** not need to contain a lot of randomness since we are not
32384
- ** trying to do secure encryption or anything like that...
32385
- **
32386
- ** Nothing in this file or anywhere else in SQLite does any kind of
32387
- ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
32388
- ** number generator) not as an encryption device.
32470
+ ** the first time this routine is called.
3238932471
*/
32390
- if( !wsdPrng.isInit ){
32472
+ if( wsdPrng.s[0]==0 ){
3239132473
sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
32392
- int i;
32393
- char k[256];
32394
- wsdPrng.j = 0;
32395
- wsdPrng.i = 0;
32474
+ static const u32 chacha20_init[] = {
32475
+ 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
32476
+ };
32477
+ memcpy(&wsdPrng.s[0], chacha20_init, 16);
3239632478
if( NEVER(pVfs==0) ){
32397
- memset(k, 0, sizeof(k));
32479
+ memset(&wsdPrng.s[4], 0, 44);
3239832480
}else{
32399
- sqlite3OsRandomness(pVfs, 256, k);
32400
- }
32401
- for(i=0; i<256; i++){
32402
- wsdPrng.s[i] = (u8)i;
32403
- }
32404
- for(i=0; i<256; i++){
32405
- wsdPrng.j += wsdPrng.s[i] + k[i];
32406
- t = wsdPrng.s[wsdPrng.j];
32407
- wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
32408
- wsdPrng.s[i] = t;
32409
- }
32410
- wsdPrng.isInit = 1;
32481
+ sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrng.s[4]);
32482
+ }
32483
+ wsdPrng.s[15] = wsdPrng.s[12];
32484
+ wsdPrng.s[12] = 0;
32485
+ wsdPrng.n = 0;
3241132486
}
3241232487
3241332488
assert( N>0 );
32414
- do{
32415
- wsdPrng.i++;
32416
- t = wsdPrng.s[wsdPrng.i];
32417
- wsdPrng.j += t;
32418
- wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
32419
- wsdPrng.s[wsdPrng.j] = t;
32420
- t += wsdPrng.s[wsdPrng.i];
32421
- *(zBuf++) = wsdPrng.s[t];
32422
- }while( --N );
32489
+ while( 1 /* exit by break */ ){
32490
+ if( N<=wsdPrng.n ){
32491
+ memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N);
32492
+ wsdPrng.n -= N;
32493
+ break;
32494
+ }
32495
+ if( wsdPrng.n>0 ){
32496
+ memcpy(zBuf, wsdPrng.out, wsdPrng.n);
32497
+ N -= wsdPrng.n;
32498
+ zBuf += wsdPrng.n;
32499
+ }
32500
+ wsdPrng.s[12]++;
32501
+ chacha_block((u32*)wsdPrng.out, wsdPrng.s);
32502
+ wsdPrng.n = 64;
32503
+ }
3242332504
sqlite3_mutex_leave(mutex);
3242432505
}
3242532506
3242632507
#ifndef SQLITE_UNTESTABLE
3242732508
/*
@@ -33457,11 +33538,11 @@
3345733538
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
3345833539
char *zMsg;
3345933540
va_list ap;
3346033541
sqlite3 *db = pParse->db;
3346133542
assert( db!=0 );
33462
- assert( db->pParse==pParse );
33543
+ assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
3346333544
db->errByteOffset = -2;
3346433545
va_start(ap, zFormat);
3346533546
zMsg = sqlite3VMPrintf(db, zFormat, ap);
3346633547
va_end(ap);
3346733548
if( db->errByteOffset<-1 ) db->errByteOffset = -1;
@@ -35275,67 +35356,67 @@
3527535356
/* 3 */ "Checkpoint" OpHelp(""),
3527635357
/* 4 */ "JournalMode" OpHelp(""),
3527735358
/* 5 */ "Vacuum" OpHelp(""),
3527835359
/* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
3527935360
/* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
35280
- /* 8 */ "Goto" OpHelp(""),
35281
- /* 9 */ "Gosub" OpHelp(""),
35282
- /* 10 */ "InitCoroutine" OpHelp(""),
35283
- /* 11 */ "Yield" OpHelp(""),
35284
- /* 12 */ "MustBeInt" OpHelp(""),
35285
- /* 13 */ "Jump" OpHelp(""),
35286
- /* 14 */ "Once" OpHelp(""),
35287
- /* 15 */ "If" OpHelp(""),
35288
- /* 16 */ "IfNot" OpHelp(""),
35289
- /* 17 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35290
- /* 18 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35361
+ /* 8 */ "Init" OpHelp("Start at P2"),
35362
+ /* 9 */ "Goto" OpHelp(""),
35363
+ /* 10 */ "Gosub" OpHelp(""),
35364
+ /* 11 */ "InitCoroutine" OpHelp(""),
35365
+ /* 12 */ "Yield" OpHelp(""),
35366
+ /* 13 */ "MustBeInt" OpHelp(""),
35367
+ /* 14 */ "Jump" OpHelp(""),
35368
+ /* 15 */ "Once" OpHelp(""),
35369
+ /* 16 */ "If" OpHelp(""),
35370
+ /* 17 */ "IfNot" OpHelp(""),
35371
+ /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
3529135372
/* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35292
- /* 20 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35293
- /* 21 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35294
- /* 22 */ "SeekGE" OpHelp("key=r[P3@P4]"),
35295
- /* 23 */ "SeekGT" OpHelp("key=r[P3@P4]"),
35296
- /* 24 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
35297
- /* 25 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
35298
- /* 26 */ "NoConflict" OpHelp("key=r[P3@P4]"),
35299
- /* 27 */ "NotFound" OpHelp("key=r[P3@P4]"),
35300
- /* 28 */ "Found" OpHelp("key=r[P3@P4]"),
35301
- /* 29 */ "SeekRowid" OpHelp("intkey=r[P3]"),
35302
- /* 30 */ "NotExists" OpHelp("intkey=r[P3]"),
35303
- /* 31 */ "Last" OpHelp(""),
35304
- /* 32 */ "IfSmaller" OpHelp(""),
35305
- /* 33 */ "SorterSort" OpHelp(""),
35306
- /* 34 */ "Sort" OpHelp(""),
35307
- /* 35 */ "Rewind" OpHelp(""),
35308
- /* 36 */ "SorterNext" OpHelp(""),
35309
- /* 37 */ "Prev" OpHelp(""),
35310
- /* 38 */ "Next" OpHelp(""),
35311
- /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"),
35312
- /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"),
35313
- /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"),
35314
- /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"),
35373
+ /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35374
+ /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35375
+ /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35376
+ /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
35377
+ /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"),
35378
+ /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
35379
+ /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
35380
+ /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
35381
+ /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
35382
+ /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
35383
+ /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
35384
+ /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
35385
+ /* 32 */ "Last" OpHelp(""),
35386
+ /* 33 */ "IfSmaller" OpHelp(""),
35387
+ /* 34 */ "SorterSort" OpHelp(""),
35388
+ /* 35 */ "Sort" OpHelp(""),
35389
+ /* 36 */ "Rewind" OpHelp(""),
35390
+ /* 37 */ "SorterNext" OpHelp(""),
35391
+ /* 38 */ "Prev" OpHelp(""),
35392
+ /* 39 */ "Next" OpHelp(""),
35393
+ /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"),
35394
+ /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"),
35395
+ /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"),
3531535396
/* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
3531635397
/* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
35317
- /* 45 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
35318
- /* 46 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
35319
- /* 47 */ "Program" OpHelp(""),
35320
- /* 48 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
35321
- /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35398
+ /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"),
35399
+ /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
35400
+ /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
35401
+ /* 48 */ "Program" OpHelp(""),
35402
+ /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
3532235403
/* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
3532335404
/* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
3532435405
/* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
3532535406
/* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
3532635407
/* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
3532735408
/* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
3532835409
/* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
3532935410
/* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
3533035411
/* 58 */ "ElseEq" OpHelp(""),
35331
- /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35332
- /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35333
- /* 61 */ "IncrVacuum" OpHelp(""),
35334
- /* 62 */ "VNext" OpHelp(""),
35335
- /* 63 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
35336
- /* 64 */ "Init" OpHelp("Start at P2"),
35412
+ /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35413
+ /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35414
+ /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35415
+ /* 62 */ "IncrVacuum" OpHelp(""),
35416
+ /* 63 */ "VNext" OpHelp(""),
35417
+ /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
3533735418
/* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
3533835419
/* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
3533935420
/* 67 */ "Return" OpHelp(""),
3534035421
/* 68 */ "EndCoroutine" OpHelp(""),
3534135422
/* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
@@ -41318,30 +41399,39 @@
4131841399
** pVfs->mxPathname bytes.
4131941400
*/
4132041401
static int unixGetTempname(int nBuf, char *zBuf){
4132141402
const char *zDir;
4132241403
int iLimit = 0;
41404
+ int rc = SQLITE_OK;
4132341405
4132441406
/* It's odd to simulate an io-error here, but really this is just
4132541407
** using the io-error infrastructure to test that SQLite handles this
4132641408
** function failing.
4132741409
*/
4132841410
zBuf[0] = 0;
4132941411
SimulateIOError( return SQLITE_IOERR );
4133041412
41413
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4133141414
zDir = unixTempFileDir();
41332
- if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
41333
- do{
41334
- u64 r;
41335
- sqlite3_randomness(sizeof(r), &r);
41336
- assert( nBuf>2 );
41337
- zBuf[nBuf-2] = 0;
41338
- sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41339
- zDir, r, 0);
41340
- if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
41341
- }while( osAccess(zBuf,0)==0 );
41342
- return SQLITE_OK;
41415
+ if( zDir==0 ){
41416
+ rc = SQLITE_IOERR_GETTEMPPATH;
41417
+ }else{
41418
+ do{
41419
+ u64 r;
41420
+ sqlite3_randomness(sizeof(r), &r);
41421
+ assert( nBuf>2 );
41422
+ zBuf[nBuf-2] = 0;
41423
+ sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41424
+ zDir, r, 0);
41425
+ if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
41426
+ rc = SQLITE_ERROR;
41427
+ break;
41428
+ }
41429
+ }while( osAccess(zBuf,0)==0 );
41430
+ }
41431
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
41432
+ return rc;
4134341433
}
4134441434
4134541435
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
4134641436
/*
4134741437
** Routine to transform a unixFile into a proxy-locking unixFile.
@@ -43512,11 +43602,16 @@
4351243602
** correctly. See ticket [bb3a86e890c8e96ab] */
4351343603
assert( ArraySize(aSyscall)==29 );
4351443604
4351543605
/* Register all VFSes defined in the aVfs[] array */
4351643606
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
43607
+#ifdef SQLITE_DEFAULT_UNIX_VFS
43608
+ sqlite3_vfs_register(&aVfs[i],
43609
+ 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
43610
+#else
4351743611
sqlite3_vfs_register(&aVfs[i], i==0);
43612
+#endif
4351843613
}
4351943614
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
4352043615
4352143616
#ifndef SQLITE_OMIT_WAL
4352243617
/* Validate lock assumptions */
@@ -45480,10 +45575,11 @@
4548045575
char **ppDirectory = 0;
4548145576
#ifndef SQLITE_OMIT_AUTOINIT
4548245577
int rc = sqlite3_initialize();
4548345578
if( rc ) return rc;
4548445579
#endif
45580
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4548545581
if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
4548645582
ppDirectory = &sqlite3_data_directory;
4548745583
}else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
4548845584
ppDirectory = &sqlite3_temp_directory;
4548945585
}
@@ -45494,18 +45590,23 @@
4549445590
if( ppDirectory ){
4549545591
char *zCopy = 0;
4549645592
if( zValue && zValue[0] ){
4549745593
zCopy = sqlite3_mprintf("%s", zValue);
4549845594
if ( zCopy==0 ){
45499
- return SQLITE_NOMEM_BKPT;
45595
+ rc = SQLITE_NOMEM_BKPT;
45596
+ goto set_directory8_done;
4550045597
}
4550145598
}
4550245599
sqlite3_free(*ppDirectory);
4550345600
*ppDirectory = zCopy;
45504
- return SQLITE_OK;
45601
+ rc = SQLITE_OK;
45602
+ }else{
45603
+ rc = SQLITE_ERROR;
4550545604
}
45506
- return SQLITE_ERROR;
45605
+set_directory8_done:
45606
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
45607
+ return rc;
4550745608
}
4550845609
4550945610
/*
4551045611
** This function is the same as sqlite3_win32_set_directory (below); however,
4551145612
** it accepts a UTF-16 string.
@@ -48274,10 +48375,22 @@
4827448375
}
4827548376
}
4827648377
}
4827748378
return 0;
4827848379
}
48380
+
48381
+/*
48382
+** If sqlite3_temp_directory is not, take the mutex and return true.
48383
+**
48384
+** If sqlite3_temp_directory is NULL, omit the mutex and return false.
48385
+*/
48386
+static int winTempDirDefined(void){
48387
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48388
+ if( sqlite3_temp_directory!=0 ) return 1;
48389
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48390
+ return 0;
48391
+}
4827948392
4828048393
/*
4828148394
** Create a temporary file name and store the resulting pointer into pzBuf.
4828248395
** The pointer returned in pzBuf must be freed via sqlite3_free().
4828348396
*/
@@ -48311,24 +48424,27 @@
4831148424
** has been explicitly set by the application; otherwise, use the one
4831248425
** configured by the operating system.
4831348426
*/
4831448427
nDir = nMax - (nPre + 15);
4831548428
assert( nDir>0 );
48316
- if( sqlite3_temp_directory ){
48429
+ if( winTempDirDefined() ){
4831748430
int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
4831848431
if( nDirLen>0 ){
4831948432
if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
4832048433
nDirLen++;
4832148434
}
4832248435
if( nDirLen>nDir ){
48436
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4832348437
sqlite3_free(zBuf);
4832448438
OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
4832548439
return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
4832648440
}
4832748441
sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
4832848442
}
48443
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4832948444
}
48445
+
4833048446
#if defined(__CYGWIN__)
4833148447
else{
4833248448
static const char *azDirs[] = {
4833348449
0, /* getenv("SQLITE_TMPDIR") */
4833448450
0, /* getenv("TMPDIR") */
@@ -49113,11 +49229,11 @@
4911349229
/*
4911449230
** Turn a relative pathname into a full pathname. Write the full
4911549231
** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
4911649232
** bytes in size.
4911749233
*/
49118
-static int winFullPathname(
49234
+static int winFullPathnameNoMutex(
4911949235
sqlite3_vfs *pVfs, /* Pointer to vfs object */
4912049236
const char *zRelative, /* Possibly relative input path */
4912149237
int nFull, /* Size of output buffer in bytes */
4912249238
char *zFull /* Output buffer */
4912349239
){
@@ -49291,10 +49407,23 @@
4929149407
return SQLITE_OK;
4929249408
}else{
4929349409
return SQLITE_IOERR_NOMEM_BKPT;
4929449410
}
4929549411
#endif
49412
+}
49413
+static int winFullPathname(
49414
+ sqlite3_vfs *pVfs, /* Pointer to vfs object */
49415
+ const char *zRelative, /* Possibly relative input path */
49416
+ int nFull, /* Size of output buffer in bytes */
49417
+ char *zFull /* Output buffer */
49418
+){
49419
+ int rc;
49420
+ sqlite3_mutex *pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR);
49421
+ sqlite3_mutex_enter(pMutex);
49422
+ rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
49423
+ sqlite3_mutex_leave(pMutex);
49424
+ return rc;
4929649425
}
4929749426
4929849427
#ifndef SQLITE_OMIT_LOAD_EXTENSION
4929949428
/*
4930049429
** Interfaces for opening a shared library, finding entry points
@@ -51080,39 +51209,58 @@
5108051209
*/
5108151210
#if defined(SQLITE_DEBUG) && 0
5108251211
int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
5108351212
int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
5108451213
# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
51085
- void pcacheDump(PCache *pCache){
51086
- int N;
51087
- int i, j;
51088
- sqlite3_pcache_page *pLower;
51214
+ static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
5108951215
PgHdr *pPg;
5109051216
unsigned char *a;
51217
+ int j;
51218
+ pPg = (PgHdr*)pLower->pExtra;
51219
+ printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51220
+ a = (unsigned char *)pLower->pBuf;
51221
+ for(j=0; j<12; j++) printf("%02x", a[j]);
51222
+ printf(" ptr %p\n", pPg);
51223
+ }
51224
+ static void pcacheDump(PCache *pCache){
51225
+ int N;
51226
+ int i;
51227
+ sqlite3_pcache_page *pLower;
5109151228
5109251229
if( sqlite3PcacheTrace<2 ) return;
5109351230
if( pCache->pCache==0 ) return;
5109451231
N = sqlite3PcachePagecount(pCache);
5109551232
if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
5109651233
for(i=1; i<=N; i++){
5109751234
pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
5109851235
if( pLower==0 ) continue;
51099
- pPg = (PgHdr*)pLower->pExtra;
51100
- printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51101
- a = (unsigned char *)pLower->pBuf;
51102
- for(j=0; j<12; j++) printf("%02x", a[j]);
51103
- printf("\n");
51104
- if( pPg->pPage==0 ){
51236
+ pcachePageTrace(i, pLower);
51237
+ if( ((PgHdr*)pLower)->pPage==0 ){
5110551238
sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
5110651239
}
5110751240
}
5110851241
}
51109
- #else
51242
+#else
5111051243
# define pcacheTrace(X)
51244
+# define pcachePageTrace(PGNO, X)
5111151245
# define pcacheDump(X)
5111251246
#endif
5111351247
51248
+/*
51249
+** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
51250
+** This routine runs inside of assert() statements only.
51251
+*/
51252
+#ifdef SQLITE_DEBUG
51253
+static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
51254
+ PgHdr *p;
51255
+ for(p=pCache->pDirty; p; p=p->pDirtyNext){
51256
+ if( p==pPg ) return 1;
51257
+ }
51258
+ return 0;
51259
+}
51260
+#endif
51261
+
5111451262
/*
5111551263
** Check invariants on a PgHdr entry. Return true if everything is OK.
5111651264
** Return false if any invariant is violated.
5111751265
**
5111851266
** This routine is for use inside of assert() statements only. For
@@ -51127,12 +51275,17 @@
5112751275
assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
5112851276
pCache = pPg->pCache;
5112951277
assert( pCache!=0 ); /* Every page has an associated PCache */
5113051278
if( pPg->flags & PGHDR_CLEAN ){
5113151279
assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
51132
- assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
51133
- assert( pCache->pDirtyTail!=pPg );
51280
+ assert( !pageOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirty list */
51281
+ }else{
51282
+ assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
51283
+ assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
51284
+ assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
51285
+ assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
51286
+ assert( pageOnDirtyList(pCache, pPg) );
5113451287
}
5113551288
/* WRITEABLE pages must also be DIRTY */
5113651289
if( pPg->flags & PGHDR_WRITEABLE ){
5113751290
assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
5113851291
}
@@ -51402,12 +51555,13 @@
5140251555
eCreate = createFlag & pCache->eCreate;
5140351556
assert( eCreate==0 || eCreate==1 || eCreate==2 );
5140451557
assert( createFlag==0 || pCache->eCreate==eCreate );
5140551558
assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
5140651559
pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51407
- pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
51560
+ pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
5140851561
createFlag?" create":"",pRes));
51562
+ pcachePageTrace(pgno, pRes);
5140951563
return pRes;
5141051564
}
5141151565
5141251566
/*
5141351567
** If the sqlite3PcacheFetch() routine is unable to allocate a new
@@ -51531,10 +51685,11 @@
5153151685
if( (--p->nRef)==0 ){
5153251686
if( p->flags&PGHDR_CLEAN ){
5153351687
pcacheUnpin(p);
5153451688
}else{
5153551689
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
51690
+ assert( sqlite3PcachePageSanity(p) );
5153651691
}
5153751692
}
5153851693
}
5153951694
5154051695
/*
@@ -51574,10 +51729,11 @@
5157451729
if( p->flags & PGHDR_CLEAN ){
5157551730
p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
5157651731
pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
5157751732
assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
5157851733
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
51734
+ assert( sqlite3PcachePageSanity(p) );
5157951735
}
5158051736
assert( sqlite3PcachePageSanity(p) );
5158151737
}
5158251738
}
5158351739
@@ -51636,18 +51792,28 @@
5163651792
/*
5163751793
** Change the page number of page p to newPgno.
5163851794
*/
5163951795
SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
5164051796
PCache *pCache = p->pCache;
51797
+ sqlite3_pcache_page *pOther;
5164151798
assert( p->nRef>0 );
5164251799
assert( newPgno>0 );
5164351800
assert( sqlite3PcachePageSanity(p) );
5164451801
pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51802
+ pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
5164551803
sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51804
+ if( pOther ){
51805
+ PgHdr *pPg = (PgHdr*)pOther->pExtra;
51806
+ pPg->pgno = p->pgno;
51807
+ if( pPg->pPage==0 ){
51808
+ sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pOther, 0);
51809
+ }
51810
+ }
5164651811
p->pgno = newPgno;
5164751812
if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
5164851813
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
51814
+ assert( sqlite3PcachePageSanity(p) );
5164951815
}
5165051816
}
5165151817
5165251818
/*
5165351819
** Drop every cache entry whose page number is greater than "pgno". The
@@ -51941,16 +52107,17 @@
5194152107
** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
5194252108
** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
5194352109
** size can vary according to architecture, compile-time options, and
5194452110
** SQLite library version number.
5194552111
**
51946
-** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
51947
-** using a separate memory allocation from the database page content. This
51948
-** seeks to overcome the "clownshoe" problem (also called "internal
51949
-** fragmentation" in academic literature) of allocating a few bytes more
51950
-** than a power of two with the memory allocator rounding up to the next
51951
-** power of two, and leaving the rounded-up space unused.
52112
+** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
52113
+** was defined, then the page content would be held in a separate memory
52114
+** allocation from the PgHdr1. This was intended to avoid clownshoe memory
52115
+** allocations. However, the btree layer needs a small (16-byte) overrun
52116
+** area after the page content buffer. The header serves as that overrun
52117
+** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
52118
+** any possibility of a memory error.
5195252119
**
5195352120
** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
5195452121
** with this module. Information is passed back and forth as PgHdr1 pointers.
5195552122
**
5195652123
** The pcache.c and pager.c modules deal pointers to PgHdr objects.
@@ -51991,34 +52158,44 @@
5199152158
typedef struct PgFreeslot PgFreeslot;
5199252159
typedef struct PGroup PGroup;
5199352160
5199452161
/*
5199552162
** Each cache entry is represented by an instance of the following
51996
-** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
51997
-** PgHdr1.pCache->szPage bytes is allocated directly before this structure
51998
-** in memory.
52163
+** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
52164
+** directly before this structure and is used to cache the page content.
5199952165
**
52000
-** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
52166
+** When reading a corrupt database file, it is possible that SQLite might
52167
+** read a few bytes (no more than 16 bytes) past the end of the page buffer.
52168
+** It will only read past the end of the page buffer, never write. This
52169
+** object is positioned immediately after the page buffer to serve as an
52170
+** overrun area, so that overreads are harmless.
52171
+**
52172
+** Variables isBulkLocal and isAnchor were once type "u8". That works,
5200152173
** but causes a 2-byte gap in the structure for most architectures (since
5200252174
** pointers must be either 4 or 8-byte aligned). As this structure is located
5200352175
** in memory directly after the associated page data, if the database is
5200452176
** corrupt, code at the b-tree layer may overread the page buffer and
5200552177
** read part of this structure before the corruption is detected. This
5200652178
** can cause a valgrind error if the unitialized gap is accessed. Using u16
52007
-** ensures there is no such gap, and therefore no bytes of unitialized memory
52008
-** in the structure.
52179
+** ensures there is no such gap, and therefore no bytes of uninitialized
52180
+** memory in the structure.
52181
+**
52182
+** The pLruNext and pLruPrev pointers form a double-linked circular list
52183
+** of all pages that are unpinned. The PGroup.lru element (which should be
52184
+** the only element on the list with PgHdr1.isAnchor set to 1) forms the
52185
+** beginning and the end of the list.
5200952186
*/
5201052187
struct PgHdr1 {
52011
- sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
52012
- unsigned int iKey; /* Key value (page number) */
52013
- u16 isBulkLocal; /* This page from bulk local storage */
52014
- u16 isAnchor; /* This is the PGroup.lru element */
52015
- PgHdr1 *pNext; /* Next in hash table chain */
52016
- PCache1 *pCache; /* Cache that currently owns this page */
52017
- PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
52018
- PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
52019
- /* NB: pLruPrev is only valid if pLruNext!=0 */
52188
+ sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
52189
+ unsigned int iKey; /* Key value (page number) */
52190
+ u16 isBulkLocal; /* This page from bulk local storage */
52191
+ u16 isAnchor; /* This is the PGroup.lru element */
52192
+ PgHdr1 *pNext; /* Next in hash table chain */
52193
+ PCache1 *pCache; /* Cache that currently owns this page */
52194
+ PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
52195
+ PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
52196
+ /* NB: pLruPrev is only valid if pLruNext!=0 */
5202052197
};
5202152198
5202252199
/*
5202352200
** A page is pinned if it is not on the LRU list. To be "pinned" means
5202452201
** that the page is in active use and must not be deallocated.
@@ -52340,29 +52517,17 @@
5234052517
assert( pcache1.separateCache==0 );
5234152518
assert( pCache->pGroup==&pcache1.grp );
5234252519
pcache1LeaveMutex(pCache->pGroup);
5234352520
#endif
5234452521
if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
52345
-#ifdef SQLITE_PCACHE_SEPARATE_HEADER
52346
- pPg = pcache1Alloc(pCache->szPage);
52347
- p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
52348
- if( !pPg || !p ){
52349
- pcache1Free(pPg);
52350
- sqlite3_free(p);
52351
- pPg = 0;
52352
- }
52353
-#else
5235452522
pPg = pcache1Alloc(pCache->szAlloc);
52355
-#endif
5235652523
if( benignMalloc ){ sqlite3EndBenignMalloc(); }
5235752524
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
5235852525
pcache1EnterMutex(pCache->pGroup);
5235952526
#endif
5236052527
if( pPg==0 ) return 0;
52361
-#ifndef SQLITE_PCACHE_SEPARATE_HEADER
5236252528
p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
52363
-#endif
5236452529
p->page.pBuf = pPg;
5236552530
p->page.pExtra = &p[1];
5236652531
p->isBulkLocal = 0;
5236752532
p->isAnchor = 0;
5236852533
p->pLruPrev = 0; /* Initializing this saves a valgrind error */
@@ -52382,13 +52547,10 @@
5238252547
if( p->isBulkLocal ){
5238352548
p->pNext = pCache->pFree;
5238452549
pCache->pFree = p;
5238552550
}else{
5238652551
pcache1Free(p->page.pBuf);
52387
-#ifdef SQLITE_PCACHE_SEPARATE_HEADER
52388
- sqlite3_free(p);
52389
-#endif
5239052552
}
5239152553
(*pCache->pnPurgeable)--;
5239252554
}
5239352555
5239452556
/*
@@ -53025,27 +53187,45 @@
5302553187
unsigned int iNew
5302653188
){
5302753189
PCache1 *pCache = (PCache1 *)p;
5302853190
PgHdr1 *pPage = (PgHdr1 *)pPg;
5302953191
PgHdr1 **pp;
53030
- unsigned int h;
53192
+ unsigned int hOld, hNew;
5303153193
assert( pPage->iKey==iOld );
5303253194
assert( pPage->pCache==pCache );
53195
+ assert( iOld!=iNew ); /* The page number really is changing */
5303353196
5303453197
pcache1EnterMutex(pCache->pGroup);
5303553198
53036
- h = iOld%pCache->nHash;
53037
- pp = &pCache->apHash[h];
53199
+ assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
53200
+ hOld = iOld%pCache->nHash;
53201
+ pp = &pCache->apHash[hOld];
5303853202
while( (*pp)!=pPage ){
5303953203
pp = &(*pp)->pNext;
5304053204
}
5304153205
*pp = pPage->pNext;
5304253206
53043
- h = iNew%pCache->nHash;
53207
+ hNew = iNew%pCache->nHash;
53208
+ pp = &pCache->apHash[hNew];
53209
+ while( *pp ){
53210
+ if( (*pp)->iKey==iNew ){
53211
+ /* If there is already another pcache entry at iNew, change it to iOld,
53212
+ ** thus swapping the positions of iNew and iOld */
53213
+ PgHdr1 *pOld = *pp;
53214
+ *pp = pOld->pNext;
53215
+ pOld->pNext = pCache->apHash[hOld];
53216
+ pCache->apHash[hOld] = pOld;
53217
+ pOld->iKey = iOld;
53218
+ break;
53219
+ }else{
53220
+ pp = &(*pp)->pNext;
53221
+ }
53222
+ }
53223
+
5304453224
pPage->iKey = iNew;
53045
- pPage->pNext = pCache->apHash[h];
53046
- pCache->apHash[h] = pPage;
53225
+ pPage->pNext = pCache->apHash[hNew];
53226
+ pCache->apHash[hNew] = pPage;
5304753227
if( iNew>pCache->iMaxKey ){
5304853228
pCache->iMaxKey = iNew;
5304953229
}
5305053230
5305153231
pcache1LeaveMutex(pCache->pGroup);
@@ -53148,13 +53328,10 @@
5314853328
while( (nReq<0 || nFree<nReq)
5314953329
&& (p=pcache1.grp.lru.pLruPrev)!=0
5315053330
&& p->isAnchor==0
5315153331
){
5315253332
nFree += pcache1MemSize(p->page.pBuf);
53153
-#ifdef SQLITE_PCACHE_SEPARATE_HEADER
53154
- nFree += sqlite3MemSize(p);
53155
-#endif
5315653333
assert( PAGE_IS_UNPINNED(p) );
5315753334
pcache1PinPage(p);
5315853335
pcache1RemoveFromHash(p, 1);
5315953336
}
5316053337
pcache1LeaveMutex(&pcache1.grp);
@@ -59639,10 +59816,11 @@
5963959816
int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
5964059817
int nSpill;
5964159818
5964259819
if( pPager->tempFile ){
5964359820
flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
59821
+ flags |= SQLITE_OPEN_EXCLUSIVE;
5964459822
nSpill = sqlite3Config.nStmtSpill;
5964559823
}else{
5964659824
flags |= SQLITE_OPEN_MAIN_JOURNAL;
5964759825
nSpill = jrnlBufferSize(pPager);
5964859826
}
@@ -59674,10 +59852,11 @@
5967459852
}
5967559853
5967659854
if( rc!=SQLITE_OK ){
5967759855
sqlite3BitvecDestroy(pPager->pInJournal);
5967859856
pPager->pInJournal = 0;
59857
+ pPager->journalOff = 0;
5967959858
}else{
5968059859
assert( pPager->eState==PAGER_WRITER_LOCKED );
5968159860
pPager->eState = PAGER_WRITER_CACHEMOD;
5968259861
}
5968359862
@@ -66737,10 +66916,11 @@
6673766916
** db using sqlite3SchemaToIndex().
6673866917
*/
6673966918
SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
6674066919
Btree *p;
6674166920
assert( db!=0 );
66921
+ if( db->pVfs==0 && db->nDb==0 ) return 1;
6674266922
if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
6674366923
assert( iDb>=0 && iDb<db->nDb );
6674466924
if( !sqlite3_mutex_held(db->mutex) ) return 0;
6674566925
if( iDb==1 ) return 1;
6674666926
p = db->aDb[iDb].pBt;
@@ -68309,12 +68489,11 @@
6830968489
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6831068490
assert( pPage->pBt!=0 );
6831168491
assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
6831268492
assert( pPage->nOverflow==0 );
6831368493
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68314
- temp = 0;
68315
- src = data = pPage->aData;
68494
+ data = pPage->aData;
6831668495
hdr = pPage->hdrOffset;
6831768496
cellOffset = pPage->cellOffset;
6831868497
nCell = pPage->nCell;
6831968498
assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
6832068499
iCellFirst = cellOffset + 2*nCell;
@@ -68364,43 +68543,42 @@
6836468543
}
6836568544
6836668545
cbrk = usableSize;
6836768546
iCellLast = usableSize - 4;
6836868547
iCellStart = get2byte(&data[hdr+5]);
68369
- for(i=0; i<nCell; i++){
68370
- u8 *pAddr; /* The i-th cell pointer */
68371
- pAddr = &data[cellOffset + i*2];
68372
- pc = get2byte(pAddr);
68373
- testcase( pc==iCellFirst );
68374
- testcase( pc==iCellLast );
68375
- /* These conditions have already been verified in btreeInitPage()
68376
- ** if PRAGMA cell_size_check=ON.
68377
- */
68378
- if( pc<iCellStart || pc>iCellLast ){
68379
- return SQLITE_CORRUPT_PAGE(pPage);
68380
- }
68381
- assert( pc>=iCellStart && pc<=iCellLast );
68382
- size = pPage->xCellSize(pPage, &src[pc]);
68383
- cbrk -= size;
68384
- if( cbrk<iCellStart || pc+size>usableSize ){
68385
- return SQLITE_CORRUPT_PAGE(pPage);
68386
- }
68387
- assert( cbrk+size<=usableSize && cbrk>=iCellStart );
68388
- testcase( cbrk+size==usableSize );
68389
- testcase( pc+size==usableSize );
68390
- put2byte(pAddr, cbrk);
68391
- if( temp==0 ){
68392
- if( cbrk==pc ) continue;
68393
- temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68394
- memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68395
- src = temp;
68396
- }
68397
- memcpy(&data[cbrk], &src[pc], size);
68548
+ if( nCell>0 ){
68549
+ temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68550
+ memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68551
+ src = temp;
68552
+ for(i=0; i<nCell; i++){
68553
+ u8 *pAddr; /* The i-th cell pointer */
68554
+ pAddr = &data[cellOffset + i*2];
68555
+ pc = get2byte(pAddr);
68556
+ testcase( pc==iCellFirst );
68557
+ testcase( pc==iCellLast );
68558
+ /* These conditions have already been verified in btreeInitPage()
68559
+ ** if PRAGMA cell_size_check=ON.
68560
+ */
68561
+ if( pc<iCellStart || pc>iCellLast ){
68562
+ return SQLITE_CORRUPT_PAGE(pPage);
68563
+ }
68564
+ assert( pc>=iCellStart && pc<=iCellLast );
68565
+ size = pPage->xCellSize(pPage, &src[pc]);
68566
+ cbrk -= size;
68567
+ if( cbrk<iCellStart || pc+size>usableSize ){
68568
+ return SQLITE_CORRUPT_PAGE(pPage);
68569
+ }
68570
+ assert( cbrk+size<=usableSize && cbrk>=iCellStart );
68571
+ testcase( cbrk+size==usableSize );
68572
+ testcase( pc+size==usableSize );
68573
+ put2byte(pAddr, cbrk);
68574
+ memcpy(&data[cbrk], &src[pc], size);
68575
+ }
6839868576
}
6839968577
data[hdr+7] = 0;
6840068578
68401
- defragment_out:
68579
+defragment_out:
6840268580
assert( pPage->nFree>=0 );
6840368581
if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
6840468582
return SQLITE_CORRUPT_PAGE(pPage);
6840568583
}
6840668584
assert( cbrk>=iCellFirst );
@@ -68469,13 +68647,13 @@
6846968647
return &aData[pc + x];
6847068648
}
6847168649
iAddr = pc;
6847268650
pTmp = &aData[pc];
6847368651
pc = get2byte(pTmp);
68474
- if( pc<=iAddr+size ){
68652
+ if( pc<=iAddr ){
6847568653
if( pc ){
68476
- /* The next slot in the chain is not past the end of the current slot */
68654
+ /* The next slot in the chain comes before the current slot */
6847768655
*pRc = SQLITE_CORRUPT_PAGE(pPg);
6847868656
}
6847968657
return 0;
6848068658
}
6848168659
}
@@ -68623,11 +68801,11 @@
6862368801
iPtr = hdr + 1;
6862468802
if( data[iPtr+1]==0 && data[iPtr]==0 ){
6862568803
iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
6862668804
}else{
6862768805
while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
68628
- if( iFreeBlk<iPtr+4 ){
68806
+ if( iFreeBlk<=iPtr ){
6862968807
if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
6863068808
return SQLITE_CORRUPT_PAGE(pPage);
6863168809
}
6863268810
iPtr = iFreeBlk;
6863368811
}
@@ -69105,13 +69283,11 @@
6910569283
if( pCur ){
6910669284
pCur->iPage--;
6910769285
pCur->pPage = pCur->apPage[pCur->iPage];
6910869286
}
6910969287
testcase( pgno==0 );
69110
- assert( pgno!=0 || rc==SQLITE_CORRUPT
69111
- || rc==SQLITE_IOERR_NOMEM
69112
- || rc==SQLITE_NOMEM );
69288
+ assert( pgno!=0 || rc!=SQLITE_OK );
6911369289
return rc;
6911469290
}
6911569291
6911669292
/*
6911769293
** Release a MemPage. This should be called once for each prior
@@ -72049,12 +72225,10 @@
7204972225
** the new child page does not match the flags field of the parent (i.e.
7205072226
** if an intkey page appears to be the parent of a non-intkey page, or
7205172227
** vice-versa).
7205272228
*/
7205372229
static int moveToChild(BtCursor *pCur, u32 newPgno){
72054
- BtShared *pBt = pCur->pBt;
72055
-
7205672230
assert( cursorOwnsBtShared(pCur) );
7205772231
assert( pCur->eState==CURSOR_VALID );
7205872232
assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
7205972233
assert( pCur->iPage>=0 );
7206072234
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
@@ -72064,11 +72238,12 @@
7206472238
pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
7206572239
pCur->aiIdx[pCur->iPage] = pCur->ix;
7206672240
pCur->apPage[pCur->iPage] = pCur->pPage;
7206772241
pCur->ix = 0;
7206872242
pCur->iPage++;
72069
- return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
72243
+ return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur,
72244
+ pCur->curPagerFlags);
7207072245
}
7207172246
7207272247
#ifdef SQLITE_DEBUG
7207372248
/*
7207472249
** Page pParent is an internal (non-leaf) tree page. This function
@@ -72170,11 +72345,11 @@
7217072345
assert( pCur->skipNext!=SQLITE_OK );
7217172346
return pCur->skipNext;
7217272347
}
7217372348
sqlite3BtreeClearCursor(pCur);
7217472349
}
72175
- rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
72350
+ rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
7217672351
0, pCur->curPagerFlags);
7217772352
if( rc!=SQLITE_OK ){
7217872353
pCur->eState = CURSOR_INVALID;
7217972354
return rc;
7218072355
}
@@ -73811,16 +73986,10 @@
7381173986
data = pPage->aData;
7381273987
ptr = &pPage->aCellIdx[2*idx];
7381373988
assert( pPage->pBt->usableSize > (u32)(ptr-data) );
7381473989
pc = get2byte(ptr);
7381573990
hdr = pPage->hdrOffset;
73816
-#if 0 /* Not required. Omit for efficiency */
73817
- if( pc<hdr+pPage->nCell*2 ){
73818
- *pRC = SQLITE_CORRUPT_BKPT;
73819
- return;
73820
- }
73821
-#endif
7382273991
testcase( pc==(u32)get2byte(&data[hdr+5]) );
7382373992
testcase( pc+sz==pPage->pBt->usableSize );
7382473993
if( pc+sz > pPage->pBt->usableSize ){
7382573994
*pRC = SQLITE_CORRUPT_BKPT;
7382673995
return;
@@ -74700,12 +74869,10 @@
7470074869
int szNew[NB+2]; /* Combined size of cells placed on i-th page */
7470174870
u8 *aSpace1; /* Space for copies of dividers cells */
7470274871
Pgno pgno; /* Temp var to store a page number in */
7470374872
u8 abDone[NB+2]; /* True after i'th new page is populated */
7470474873
Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
74705
- Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
74706
- u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
7470774874
CellArray b; /* Parsed information on cells being balanced */
7470874875
7470974876
memset(abDone, 0, sizeof(abDone));
7471074877
memset(&b, 0, sizeof(b));
7471174878
pBt = pParent->pBt;
@@ -75125,46 +75292,43 @@
7512575292
** Reassign page numbers so that the new pages are in ascending order.
7512675293
** This helps to keep entries in the disk file in order so that a scan
7512775294
** of the table is closer to a linear scan through the file. That in turn
7512875295
** helps the operating system to deliver pages from the disk more rapidly.
7512975296
**
75130
- ** An O(n^2) insertion sort algorithm is used, but since n is never more
75131
- ** than (NB+2) (a small constant), that should not be a problem.
75297
+ ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
75298
+ ** (5), that is not a performance concern.
7513275299
**
7513375300
** When NB==3, this one optimization makes the database about 25% faster
7513475301
** for large insertions and deletions.
7513575302
*/
7513675303
for(i=0; i<nNew; i++){
75137
- aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75138
- aPgFlags[i] = apNew[i]->pDbPage->flags;
75139
- for(j=0; j<i; j++){
75140
- if( NEVER(aPgno[j]==aPgno[i]) ){
75141
- /* This branch is taken if the set of sibling pages somehow contains
75142
- ** duplicate entries. This can happen if the database is corrupt.
75143
- ** It would be simpler to detect this as part of the loop below, but
75144
- ** we do the detection here in order to avoid populating the pager
75145
- ** cache with two separate objects associated with the same
75146
- ** page number. */
75147
- assert( CORRUPT_DB );
75148
- rc = SQLITE_CORRUPT_BKPT;
75149
- goto balance_cleanup;
75150
- }
75151
- }
75152
- }
75153
- for(i=0; i<nNew; i++){
75154
- int iBest = 0; /* aPgno[] index of page number to use */
75155
- for(j=1; j<nNew; j++){
75156
- if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
75157
- }
75158
- pgno = aPgOrder[iBest];
75159
- aPgOrder[iBest] = 0xffffffff;
75160
- if( iBest!=i ){
75161
- if( iBest>i ){
75162
- sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75163
- }
75164
- sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75165
- apNew[i]->pgno = pgno;
75304
+ aPgno[i] = apNew[i]->pgno;
75305
+ assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
75306
+ assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
75307
+ }
75308
+ for(i=0; i<nNew-1; i++){
75309
+ int iB = i;
75310
+ for(j=i+1; j<nNew; j++){
75311
+ if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
75312
+ }
75313
+
75314
+ /* If apNew[i] has a page number that is bigger than any of the
75315
+ ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
75316
+ ** entry that has the smallest page number (which we know to be
75317
+ ** entry apNew[iB]).
75318
+ */
75319
+ if( iB!=i ){
75320
+ Pgno pgnoA = apNew[i]->pgno;
75321
+ Pgno pgnoB = apNew[iB]->pgno;
75322
+ Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
75323
+ u16 fgA = apNew[i]->pDbPage->flags;
75324
+ u16 fgB = apNew[iB]->pDbPage->flags;
75325
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
75326
+ sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
75327
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
75328
+ apNew[i]->pgno = pgnoB;
75329
+ apNew[iB]->pgno = pgnoA;
7516675330
}
7516775331
}
7516875332
7516975333
TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
7517075334
"%d(%d nc=%d) %d(%d nc=%d)\n",
@@ -79428,10 +79592,20 @@
7942879592
double r2 = (double)i;
7942979593
return r1==0.0
7943079594
|| (memcmp(&r1, &r2, sizeof(r1))==0
7943179595
&& i >= -2251799813685248LL && i < 2251799813685248LL);
7943279596
}
79597
+
79598
+/* Convert a floating point value to its closest integer. Do so in
79599
+** a way that avoids 'outside the range of representable values' warnings
79600
+** from UBSAN.
79601
+*/
79602
+SQLITE_PRIVATE i64 sqlite3RealToI64(double r){
79603
+ if( r<=(double)SMALLEST_INT64 ) return SMALLEST_INT64;
79604
+ if( r>=(double)LARGEST_INT64) return LARGEST_INT64;
79605
+ return (i64)r;
79606
+}
7943379607
7943479608
/*
7943579609
** Convert pMem so that it has type MEM_Real or MEM_Int.
7943679610
** Invalidate any prior representations.
7943779611
**
@@ -79450,11 +79624,11 @@
7945079624
sqlite3_int64 ix;
7945179625
assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
7945279626
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
7945379627
rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
7945479628
if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79455
- || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
79629
+ || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
7945679630
){
7945779631
pMem->u.i = ix;
7945879632
MemSetTypeFlag(pMem, MEM_Int);
7945979633
}else{
7946079634
MemSetTypeFlag(pMem, MEM_Real);
@@ -80682,14 +80856,14 @@
8068280856
p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
8068380857
if( p==0 ) return 0;
8068480858
memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
8068580859
p->db = db;
8068680860
if( db->pVdbe ){
80687
- db->pVdbe->pPrev = p;
80861
+ db->pVdbe->ppVPrev = &p->pVNext;
8068880862
}
80689
- p->pNext = db->pVdbe;
80690
- p->pPrev = 0;
80863
+ p->pVNext = db->pVdbe;
80864
+ p->ppVPrev = &db->pVdbe;
8069180865
db->pVdbe = p;
8069280866
assert( p->eVdbeState==VDBE_INIT_STATE );
8069380867
p->pParse = pParse;
8069480868
pParse->pVdbe = p;
8069580869
assert( pParse->aLabel==0 );
@@ -80767,25 +80941,32 @@
8076780941
return 0;
8076880942
}
8076980943
#endif
8077080944
8077180945
/*
80772
-** Swap all content between two VDBE structures.
80946
+** Swap byte-code between two VDBE structures.
80947
+**
80948
+** This happens after pB was previously run and returned
80949
+** SQLITE_SCHEMA. The statement was then reprepared in pA.
80950
+** This routine transfers the new bytecode in pA over to pB
80951
+** so that pB can be run again. The old pB byte code is
80952
+** moved back to pA so that it will be cleaned up when pA is
80953
+** finalized.
8077380954
*/
8077480955
SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
80775
- Vdbe tmp, *pTmp;
80956
+ Vdbe tmp, *pTmp, **ppTmp;
8077680957
char *zTmp;
8077780958
assert( pA->db==pB->db );
8077880959
tmp = *pA;
8077980960
*pA = *pB;
8078080961
*pB = tmp;
80781
- pTmp = pA->pNext;
80782
- pA->pNext = pB->pNext;
80783
- pB->pNext = pTmp;
80784
- pTmp = pA->pPrev;
80785
- pA->pPrev = pB->pPrev;
80786
- pB->pPrev = pTmp;
80962
+ pTmp = pA->pVNext;
80963
+ pA->pVNext = pB->pVNext;
80964
+ pB->pVNext = pTmp;
80965
+ ppTmp = pA->ppVPrev;
80966
+ pA->ppVPrev = pB->ppVPrev;
80967
+ pB->ppVPrev = ppTmp;
8078780968
zTmp = pA->zSql;
8078880969
pA->zSql = pB->zSql;
8078980970
pB->zSql = zTmp;
8079080971
#ifdef SQLITE_ENABLE_NORMALIZE
8079180972
zTmp = pA->zNormSql;
@@ -81033,10 +81214,11 @@
8103381214
pCtx->argc = nArg;
8103481215
pCtx->iOp = sqlite3VdbeCurrentAddr(v);
8103581216
addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
8103681217
p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
8103781218
sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
81219
+ sqlite3MayAbort(pParse);
8103881220
return addr;
8103981221
}
8104081222
8104181223
/*
8104281224
** Add an opcode that includes the p4 value with a P4_INT64 or
@@ -81101,11 +81283,11 @@
8110181283
va_end(ap);
8110281284
v = pParse->pVdbe;
8110381285
iThis = v->nOp;
8110481286
sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
8110581287
zMsg, P4_DYNAMIC);
81106
- sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
81288
+ sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
8110781289
if( bPush){
8110881290
pParse->addrExplain = iThis;
8110981291
}
8111081292
}
8111181293
}
@@ -81368,10 +81550,11 @@
8136881550
int opcode = pOp->opcode;
8136981551
if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
8137081552
|| opcode==OP_VDestroy
8137181553
|| opcode==OP_VCreate
8137281554
|| opcode==OP_ParseSchema
81555
+ || opcode==OP_Function || opcode==OP_PureFunc
8137381556
|| ((opcode==OP_Halt || opcode==OP_HaltIfNull)
8137481557
&& ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
8137581558
){
8137681559
hasAbort = 1;
8137781560
break;
@@ -81458,12 +81641,12 @@
8145881641
Parse *pParse = p->pParse;
8145981642
int *aLabel = pParse->aLabel;
8146081643
p->readOnly = 1;
8146181644
p->bIsReader = 0;
8146281645
pOp = &p->aOp[p->nOp-1];
81463
- while(1){
81464
-
81646
+ assert( p->aOp[0].opcode==OP_Init );
81647
+ while( 1 /* Loop termates when it reaches the OP_Init opcode */ ){
8146581648
/* Only JUMP opcodes and the short list of special opcodes in the switch
8146681649
** below need to be considered. The mkopcodeh.tcl generator script groups
8146781650
** all these opcodes together near the front of the opcode list. Skip
8146881651
** any opcode that does not need processing by virtual of the fact that
8146981652
** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
@@ -81488,10 +81671,14 @@
8148881671
case OP_JournalMode: {
8148981672
p->readOnly = 0;
8149081673
p->bIsReader = 1;
8149181674
break;
8149281675
}
81676
+ case OP_Init: {
81677
+ assert( pOp->p2>=0 );
81678
+ goto resolve_p2_values_loop_exit;
81679
+ }
8149381680
#ifndef SQLITE_OMIT_VIRTUALTABLE
8149481681
case OP_VUpdate: {
8149581682
if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
8149681683
break;
8149781684
}
@@ -81520,15 +81707,16 @@
8152081707
/* The mkopcodeh.tcl script has so arranged things that the only
8152181708
** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
8152281709
** have non-negative values for P2. */
8152381710
assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
8152481711
}
81525
- if( pOp==p->aOp ) break;
81712
+ assert( pOp>p->aOp );
8152681713
pOp--;
8152781714
}
81715
+resolve_p2_values_loop_exit:
8152881716
if( aLabel ){
81529
- sqlite3DbFreeNN(p->db, pParse->aLabel);
81717
+ sqlite3DbNNFreeNN(p->db, pParse->aLabel);
8153081718
pParse->aLabel = 0;
8153181719
}
8153281720
pParse->nLabel = 0;
8153381721
*pMaxFuncArgs = nMaxArgs;
8153481722
assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
@@ -81773,19 +81961,23 @@
8177381961
/*
8177481962
** Change the value of the opcode, or P1, P2, P3, or P5 operands
8177581963
** for a specific instruction.
8177681964
*/
8177781965
SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
81966
+ assert( addr>=0 );
8177881967
sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
8177981968
}
8178081969
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
81970
+ assert( addr>=0 );
8178181971
sqlite3VdbeGetOp(p,addr)->p1 = val;
8178281972
}
8178381973
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
81974
+ assert( addr>=0 || p->db->mallocFailed );
8178481975
sqlite3VdbeGetOp(p,addr)->p2 = val;
8178581976
}
8178681977
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
81978
+ assert( addr>=0 );
8178781979
sqlite3VdbeGetOp(p,addr)->p3 = val;
8178881980
}
8178981981
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
8179081982
assert( p->nOp>0 || p->db->mallocFailed );
8179181983
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
@@ -81817,11 +82009,11 @@
8181782009
assert( p->aOp[addr].opcode==OP_Once
8181882010
|| p->aOp[addr].opcode==OP_If
8181982011
|| p->aOp[addr].opcode==OP_FkIfZero );
8182082012
assert( p->aOp[addr].p4type==0 );
8182182013
#ifdef SQLITE_VDBE_COVERAGE
81822
- sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
82014
+ sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
8182382015
#endif
8182482016
p->nOp--;
8182582017
}else{
8182682018
sqlite3VdbeChangeP2(p, addr, p->nOp);
8182782019
}
@@ -81831,25 +82023,27 @@
8183182023
/*
8183282024
** If the input FuncDef structure is ephemeral, then free it. If
8183382025
** the FuncDef is not ephermal, then do nothing.
8183482026
*/
8183582027
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
82028
+ assert( db!=0 );
8183682029
if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
81837
- sqlite3DbFreeNN(db, pDef);
82030
+ sqlite3DbNNFreeNN(db, pDef);
8183882031
}
8183982032
}
8184082033
8184182034
/*
8184282035
** Delete a P4 value if necessary.
8184382036
*/
8184482037
static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
8184582038
if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
81846
- sqlite3DbFreeNN(db, p);
82039
+ sqlite3DbNNFreeNN(db, p);
8184782040
}
8184882041
static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
82042
+ assert( db!=0 );
8184982043
freeEphemeralFunction(db, p->pFunc);
81850
- sqlite3DbFreeNN(db, p);
82044
+ sqlite3DbNNFreeNN(db, p);
8185182045
}
8185282046
static void freeP4(sqlite3 *db, int p4type, void *p4){
8185382047
assert( db );
8185482048
switch( p4type ){
8185582049
case P4_FUNCCTX: {
@@ -81858,11 +82052,11 @@
8185882052
}
8185982053
case P4_REAL:
8186082054
case P4_INT64:
8186182055
case P4_DYNAMIC:
8186282056
case P4_INTARRAY: {
81863
- sqlite3DbFree(db, p4);
82057
+ if( p4 ) sqlite3DbNNFreeNN(db, p4);
8186482058
break;
8186582059
}
8186682060
case P4_KEYINFO: {
8186782061
if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
8186882062
break;
@@ -81897,10 +82091,11 @@
8189782091
** opcodes contained within. If aOp is not NULL it is assumed to contain
8189882092
** nOp entries.
8189982093
*/
8190082094
static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
8190182095
assert( nOp>=0 );
82096
+ assert( db!=0 );
8190282097
if( aOp ){
8190382098
Op *pOp = &aOp[nOp-1];
8190482099
while(1){ /* Exit via break */
8190582100
if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
8190682101
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -81907,11 +82102,11 @@
8190782102
sqlite3DbFree(db, pOp->zComment);
8190882103
#endif
8190982104
if( pOp==aOp ) break;
8191082105
pOp--;
8191182106
}
81912
- sqlite3DbFreeNN(db, aOp);
82107
+ sqlite3DbNNFreeNN(db, aOp);
8191382108
}
8191482109
}
8191582110
8191682111
/*
8191782112
** Link the SubProgram object passed as the second argument into the linked
@@ -82138,17 +82333,17 @@
8213882333
#ifdef SQLITE_VDBE_COVERAGE
8213982334
/*
8214082335
** Set the value if the iSrcLine field for the previously coded instruction.
8214182336
*/
8214282337
SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
82143
- sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
82338
+ sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
8214482339
}
8214582340
#endif /* SQLITE_VDBE_COVERAGE */
8214682341
8214782342
/*
82148
-** Return the opcode for a given address. If the address is -1, then
82149
-** return the most recently inserted opcode.
82343
+** Return the opcode for a given address. The address must be non-negative.
82344
+** See sqlite3VdbeGetLastOp() to get the most recently added opcode.
8215082345
**
8215182346
** If a memory allocation error has occurred prior to the calling of this
8215282347
** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
8215382348
** is readable but not writable, though it is cast to a writable value.
8215482349
** The return of a dummy opcode allows the call to continue functioning
@@ -82160,20 +82355,23 @@
8216082355
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
8216182356
/* C89 specifies that the constant "dummy" will be initialized to all
8216282357
** zeros, which is correct. MSVC generates a warning, nevertheless. */
8216382358
static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
8216482359
assert( p->eVdbeState==VDBE_INIT_STATE );
82165
- if( addr<0 ){
82166
- addr = p->nOp - 1;
82167
- }
8216882360
assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
8216982361
if( p->db->mallocFailed ){
8217082362
return (VdbeOp*)&dummy;
8217182363
}else{
8217282364
return &p->aOp[addr];
8217382365
}
8217482366
}
82367
+
82368
+/* Return the most recently added opcode
82369
+*/
82370
+VdbeOp * sqlite3VdbeGetLastOp(Vdbe *p){
82371
+ return sqlite3VdbeGetOp(p, p->nOp - 1);
82372
+}
8217582373
8217682374
#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
8217782375
/*
8217882376
** Return an integer value for one of the parameters to the opcode pOp
8217982377
** determined by character c.
@@ -82658,11 +82856,11 @@
8265882856
if( p->flags&(MEM_Agg|MEM_Dyn) ){
8265982857
testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
8266082858
sqlite3VdbeMemRelease(p);
8266182859
p->flags = MEM_Undefined;
8266282860
}else if( p->szMalloc ){
82663
- sqlite3DbFreeNN(db, p->zMalloc);
82861
+ sqlite3DbNNFreeNN(db, p->zMalloc);
8266482862
p->szMalloc = 0;
8266582863
p->flags = MEM_Undefined;
8266682864
}
8266782865
#ifdef SQLITE_DEBUG
8266882866
else{
@@ -83650,11 +83848,11 @@
8365083848
if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
8365183849
cnt++;
8365283850
if( p->readOnly==0 ) nWrite++;
8365383851
if( p->bIsReader ) nRead++;
8365483852
}
83655
- p = p->pNext;
83853
+ p = p->pVNext;
8365683854
}
8365783855
assert( cnt==db->nVdbeActive );
8365883856
assert( nWrite==db->nVdbeWrite );
8365983857
assert( nRead==db->nVdbeRead );
8366083858
}
@@ -84179,27 +84377,28 @@
8417984377
** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
8418084378
** the database connection and frees the object itself.
8418184379
*/
8418284380
static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
8418384381
SubProgram *pSub, *pNext;
84382
+ assert( db!=0 );
8418484383
assert( p->db==0 || p->db==db );
8418584384
if( p->aColName ){
8418684385
releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
84187
- sqlite3DbFreeNN(db, p->aColName);
84386
+ sqlite3DbNNFreeNN(db, p->aColName);
8418884387
}
8418984388
for(pSub=p->pProgram; pSub; pSub=pNext){
8419084389
pNext = pSub->pNext;
8419184390
vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
8419284391
sqlite3DbFree(db, pSub);
8419384392
}
8419484393
if( p->eVdbeState!=VDBE_INIT_STATE ){
8419584394
releaseMemArray(p->aVar, p->nVar);
84196
- if( p->pVList ) sqlite3DbFreeNN(db, p->pVList);
84197
- if( p->pFree ) sqlite3DbFreeNN(db, p->pFree);
84395
+ if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
84396
+ if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
8419884397
}
8419984398
vdbeFreeOpArray(db, p->aOp, p->nOp);
84200
- sqlite3DbFree(db, p->zSql);
84399
+ if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
8420184400
#ifdef SQLITE_ENABLE_NORMALIZE
8420284401
sqlite3DbFree(db, p->zNormSql);
8420384402
{
8420484403
DblquoteStr *pThis, *pNext;
8420584404
for(pThis=p->pDblStr; pThis; pThis=pNext){
@@ -84225,24 +84424,21 @@
8422584424
SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
8422684425
sqlite3 *db;
8422784426
8422884427
assert( p!=0 );
8422984428
db = p->db;
84429
+ assert( db!=0 );
8423084430
assert( sqlite3_mutex_held(db->mutex) );
8423184431
sqlite3VdbeClearObject(db, p);
8423284432
if( db->pnBytesFreed==0 ){
84233
- if( p->pPrev ){
84234
- p->pPrev->pNext = p->pNext;
84235
- }else{
84236
- assert( db->pVdbe==p );
84237
- db->pVdbe = p->pNext;
84238
- }
84239
- if( p->pNext ){
84240
- p->pNext->pPrev = p->pPrev;
84433
+ assert( p->ppVPrev!=0 );
84434
+ *p->ppVPrev = p->pVNext;
84435
+ if( p->pVNext ){
84436
+ p->pVNext->ppVPrev = p->ppVPrev;
8424184437
}
8424284438
}
84243
- sqlite3DbFreeNN(db, p);
84439
+ sqlite3DbNNFreeNN(db, p);
8424484440
}
8424584441
8424684442
/*
8424784443
** The cursor "p" has a pending seek operation that has not yet been
8424884444
** carried out. Seek the cursor now. If an error occurs, return
@@ -85733,11 +85929,11 @@
8573385929
** prepared statements. The flag is set to 1 for an immediate expiration
8573485930
** and set to 2 for an advisory expiration.
8573585931
*/
8573685932
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
8573785933
Vdbe *p;
85738
- for(p = db->pVdbe; p; p=p->pNext){
85934
+ for(p = db->pVdbe; p; p=p->pVNext){
8573985935
p->expired = iCode+1;
8574085936
}
8574185937
}
8574285938
8574385939
/*
@@ -85854,17 +86050,18 @@
8585486050
**
8585586051
** This function is used to free UnpackedRecord structures allocated by
8585686052
** the vdbeUnpackRecord() function found in vdbeapi.c.
8585786053
*/
8585886054
static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
86055
+ assert( db!=0 );
8585986056
if( p ){
8586086057
int i;
8586186058
for(i=0; i<nField; i++){
8586286059
Mem *pMem = &p->aMem[i];
8586386060
if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
8586486061
}
85865
- sqlite3DbFreeNN(db, p);
86062
+ sqlite3DbNNFreeNN(db, p);
8586686063
}
8586786064
}
8586886065
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
8586986066
8587086067
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
@@ -85931,11 +86128,11 @@
8593186128
if( preupdate.aNew ){
8593286129
int i;
8593386130
for(i=0; i<pCsr->nField; i++){
8593486131
sqlite3VdbeMemRelease(&preupdate.aNew[i]);
8593586132
}
85936
- sqlite3DbFreeNN(db, preupdate.aNew);
86133
+ sqlite3DbNNFreeNN(db, preupdate.aNew);
8593786134
}
8593886135
}
8593986136
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
8594086137
8594186138
/************** End of vdbeaux.c *********************************************/
@@ -86048,11 +86245,13 @@
8604886245
Vdbe *v = (Vdbe*)pStmt;
8604986246
sqlite3 *db = v->db;
8605086247
if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
8605186248
sqlite3_mutex_enter(db->mutex);
8605286249
checkProfileCallback(db, v);
86053
- rc = sqlite3VdbeFinalize(v);
86250
+ assert( v->eVdbeState>=VDBE_READY_STATE );
86251
+ rc = sqlite3VdbeReset(v);
86252
+ sqlite3VdbeDelete(v);
8605486253
rc = sqlite3ApiExit(db, rc);
8605586254
sqlite3LeaveMutexAndCloseZombie(db);
8605686255
}
8605786256
return rc;
8605886257
}
@@ -87370,11 +87569,11 @@
8737087569
** the mutex is released if any kind of error occurs.
8737187570
**
8737287571
** The error code stored in database p->db is overwritten with the return
8737387572
** value in any case.
8737487573
*/
87375
-static int vdbeUnbind(Vdbe *p, int i){
87574
+static int vdbeUnbind(Vdbe *p, unsigned int i){
8737687575
Mem *pVar;
8737787576
if( vdbeSafetyNotNull(p) ){
8737887577
return SQLITE_MISUSE_BKPT;
8737987578
}
8738087579
sqlite3_mutex_enter(p->db->mutex);
@@ -87383,16 +87582,15 @@
8738387582
sqlite3_mutex_leave(p->db->mutex);
8738487583
sqlite3_log(SQLITE_MISUSE,
8738587584
"bind on a busy prepared statement: [%s]", p->zSql);
8738687585
return SQLITE_MISUSE_BKPT;
8738787586
}
87388
- if( i<1 || i>p->nVar ){
87587
+ if( i>=(unsigned int)p->nVar ){
8738987588
sqlite3Error(p->db, SQLITE_RANGE);
8739087589
sqlite3_mutex_leave(p->db->mutex);
8739187590
return SQLITE_RANGE;
8739287591
}
87393
- i--;
8739487592
pVar = &p->aVar[i];
8739587593
sqlite3VdbeMemRelease(pVar);
8739687594
pVar->flags = MEM_Null;
8739787595
p->db->errCode = SQLITE_OK;
8739887596
@@ -87425,11 +87623,11 @@
8742587623
){
8742687624
Vdbe *p = (Vdbe *)pStmt;
8742787625
Mem *pVar;
8742887626
int rc;
8742987627
87430
- rc = vdbeUnbind(p, i);
87628
+ rc = vdbeUnbind(p, (u32)(i-1));
8743187629
if( rc==SQLITE_OK ){
8743287630
if( zData!=0 ){
8743387631
pVar = &p->aVar[i-1];
8743487632
rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
8743587633
if( rc==SQLITE_OK && encoding!=0 ){
@@ -87474,11 +87672,11 @@
8747487672
return bindText(pStmt, i, zData, nData, xDel, 0);
8747587673
}
8747687674
SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
8747787675
int rc;
8747887676
Vdbe *p = (Vdbe *)pStmt;
87479
- rc = vdbeUnbind(p, i);
87677
+ rc = vdbeUnbind(p, (u32)(i-1));
8748087678
if( rc==SQLITE_OK ){
8748187679
sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
8748287680
sqlite3_mutex_leave(p->db->mutex);
8748387681
}
8748487682
return rc;
@@ -87487,21 +87685,21 @@
8748787685
return sqlite3_bind_int64(p, i, (i64)iValue);
8748887686
}
8748987687
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
8749087688
int rc;
8749187689
Vdbe *p = (Vdbe *)pStmt;
87492
- rc = vdbeUnbind(p, i);
87690
+ rc = vdbeUnbind(p, (u32)(i-1));
8749387691
if( rc==SQLITE_OK ){
8749487692
sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
8749587693
sqlite3_mutex_leave(p->db->mutex);
8749687694
}
8749787695
return rc;
8749887696
}
8749987697
SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
8750087698
int rc;
8750187699
Vdbe *p = (Vdbe*)pStmt;
87502
- rc = vdbeUnbind(p, i);
87700
+ rc = vdbeUnbind(p, (u32)(i-1));
8750387701
if( rc==SQLITE_OK ){
8750487702
sqlite3_mutex_leave(p->db->mutex);
8750587703
}
8750687704
return rc;
8750787705
}
@@ -87512,11 +87710,11 @@
8751287710
const char *zPTtype,
8751387711
void (*xDestructor)(void*)
8751487712
){
8751587713
int rc;
8751687714
Vdbe *p = (Vdbe*)pStmt;
87517
- rc = vdbeUnbind(p, i);
87715
+ rc = vdbeUnbind(p, (u32)(i-1));
8751887716
if( rc==SQLITE_OK ){
8751987717
sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
8752087718
sqlite3_mutex_leave(p->db->mutex);
8752187719
}else if( xDestructor ){
8752287720
xDestructor(pPtr);
@@ -87590,11 +87788,11 @@
8759087788
return rc;
8759187789
}
8759287790
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
8759387791
int rc;
8759487792
Vdbe *p = (Vdbe *)pStmt;
87595
- rc = vdbeUnbind(p, i);
87793
+ rc = vdbeUnbind(p, (u32)(i-1));
8759687794
if( rc==SQLITE_OK ){
8759787795
#ifndef SQLITE_OMIT_INCRBLOB
8759887796
sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
8759987797
#else
8760087798
rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
@@ -87750,11 +87948,11 @@
8775087948
#endif
8775187949
sqlite3_mutex_enter(pDb->mutex);
8775287950
if( pStmt==0 ){
8775387951
pNext = (sqlite3_stmt*)pDb->pVdbe;
8775487952
}else{
87755
- pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
87953
+ pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
8775687954
}
8775787955
sqlite3_mutex_leave(pDb->mutex);
8775887956
return pNext;
8775987957
}
8776087958
@@ -87775,12 +87973,15 @@
8777587973
if( op==SQLITE_STMTSTATUS_MEMUSED ){
8777687974
sqlite3 *db = pVdbe->db;
8777787975
sqlite3_mutex_enter(db->mutex);
8777887976
v = 0;
8777987977
db->pnBytesFreed = (int*)&v;
87978
+ assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
87979
+ db->lookaside.pEnd = db->lookaside.pStart;
8778087980
sqlite3VdbeDelete(pVdbe);
8778187981
db->pnBytesFreed = 0;
87982
+ db->lookaside.pEnd = db->lookaside.pTrueEnd;
8778287983
sqlite3_mutex_leave(db->mutex);
8778387984
}else{
8778487985
v = pVdbe->aCounter[op];
8778587986
if( resetFlag ) pVdbe->aCounter[op] = 0;
8778687987
}
@@ -88616,11 +88817,12 @@
8861688817
** floating point value of rValue. Return true and set *piValue to the
8861788818
** integer value if the string is in range to be an integer. Otherwise,
8861888819
** return false.
8861988820
*/
8862088821
static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
88621
- i64 iValue = (double)rValue;
88822
+ i64 iValue;
88823
+ iValue = sqlite3RealToI64(rValue);
8862288824
if( sqlite3RealSameAsInt(rValue,iValue) ){
8862388825
*piValue = iValue;
8862488826
return 1;
8862588827
}
8862688828
return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
@@ -88778,21 +88980,22 @@
8877888980
**
8877988981
** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
8878088982
** But it does set pMem->u.r and pMem->u.i appropriately.
8878188983
*/
8878288984
static u16 numericType(Mem *pMem){
88783
- if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
88985
+ assert( (pMem->flags & MEM_Null)==0
88986
+ || pMem->db==0 || pMem->db->mallocFailed );
88987
+ if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
8878488988
testcase( pMem->flags & MEM_Int );
8878588989
testcase( pMem->flags & MEM_Real );
8878688990
testcase( pMem->flags & MEM_IntReal );
88787
- return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
88991
+ return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
8878888992
}
88789
- if( pMem->flags & (MEM_Str|MEM_Blob) ){
88790
- testcase( pMem->flags & MEM_Str );
88791
- testcase( pMem->flags & MEM_Blob );
88792
- return computeNumericType(pMem);
88793
- }
88993
+ assert( pMem->flags & (MEM_Str|MEM_Blob) );
88994
+ testcase( pMem->flags & MEM_Str );
88995
+ testcase( pMem->flags & MEM_Blob );
88996
+ return computeNumericType(pMem);
8879488997
return 0;
8879588998
}
8879688999
8879789000
#ifdef SQLITE_DEBUG
8879889001
/*
@@ -90033,25 +90236,24 @@
9003390236
case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
9003490237
case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
9003590238
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
9003690239
case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
9003790240
case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
90038
- u16 flags; /* Combined MEM_* flags from both inputs */
9003990241
u16 type1; /* Numeric type of left operand */
9004090242
u16 type2; /* Numeric type of right operand */
9004190243
i64 iA; /* Integer value of left operand */
9004290244
i64 iB; /* Integer value of right operand */
9004390245
double rA; /* Real value of left operand */
9004490246
double rB; /* Real value of right operand */
9004590247
9004690248
pIn1 = &aMem[pOp->p1];
90047
- type1 = numericType(pIn1);
90249
+ type1 = pIn1->flags;
9004890250
pIn2 = &aMem[pOp->p2];
90049
- type2 = numericType(pIn2);
90251
+ type2 = pIn2->flags;
9005090252
pOut = &aMem[pOp->p3];
90051
- flags = pIn1->flags | pIn2->flags;
9005290253
if( (type1 & type2 & MEM_Int)!=0 ){
90254
+int_math:
9005390255
iA = pIn1->u.i;
9005490256
iB = pIn2->u.i;
9005590257
switch( pOp->opcode ){
9005690258
case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
9005790259
case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
@@ -90069,13 +90271,16 @@
9006990271
break;
9007090272
}
9007190273
}
9007290274
pOut->u.i = iB;
9007390275
MemSetTypeFlag(pOut, MEM_Int);
90074
- }else if( (flags & MEM_Null)!=0 ){
90276
+ }else if( ((type1 | type2) & MEM_Null)!=0 ){
9007590277
goto arithmetic_result_is_null;
9007690278
}else{
90279
+ type1 = numericType(pIn1);
90280
+ type2 = numericType(pIn2);
90281
+ if( (type1 & type2 & MEM_Int)!=0 ) goto int_math;
9007790282
fp_math:
9007890283
rA = sqlite3VdbeRealValue(pIn1);
9007990284
rB = sqlite3VdbeRealValue(pIn2);
9008090285
switch( pOp->opcode ){
9008190286
case OP_Add: rB += rA; break;
@@ -90941,15 +91146,18 @@
9094191146
**
9094291147
** Check the cursor P1 to see if it is currently pointing at a NULL row.
9094391148
** If it is, then set register P3 to NULL and jump immediately to P2.
9094491149
** If P1 is not on a NULL row, then fall through without making any
9094591150
** changes.
91151
+**
91152
+** If P1 is not an open cursor, then this opcode is a no-op.
9094691153
*/
9094791154
case OP_IfNullRow: { /* jump */
91155
+ VdbeCursor *pC;
9094891156
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90949
- assert( p->apCsr[pOp->p1]!=0 );
90950
- if( p->apCsr[pOp->p1]->nullRow ){
91157
+ pC = p->apCsr[pOp->p1];
91158
+ if( ALWAYS(pC) && pC->nullRow ){
9095191159
sqlite3VdbeMemSetNull(aMem + pOp->p3);
9095291160
goto jump_to_p2;
9095391161
}
9095491162
break;
9095591163
}
@@ -93052,11 +93260,11 @@
9305293260
**
9305393261
** <li> If the cursor is successfully moved to the target row by 0 or more
9305493262
** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
9305593263
** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
9305693264
**
93057
-** <li> If the cursor ends up past the target row (indicating the the target
93265
+** <li> If the cursor ends up past the target row (indicating that the target
9305893266
** row does not exist in the btree) then jump to SeekOP.P2.
9305993267
** </ol>
9306093268
*/
9306193269
case OP_SeekScan: {
9306293270
VdbeCursor *pC;
@@ -94388,11 +94596,13 @@
9438894596
rc = sqlite3VdbeSorterNext(db, pC);
9438994597
goto next_tail;
9439094598
9439194599
case OP_Prev: /* jump */
9439294600
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94393
- assert( pOp->p5<ArraySize(p->aCounter) );
94601
+ assert( pOp->p5==0
94602
+ || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
94603
+ || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
9439494604
pC = p->apCsr[pOp->p1];
9439594605
assert( pC!=0 );
9439694606
assert( pC->deferredMoveto==0 );
9439794607
assert( pC->eCurType==CURTYPE_BTREE );
9439894608
assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
@@ -94401,11 +94611,13 @@
9440194611
rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
9440294612
goto next_tail;
9440394613
9440494614
case OP_Next: /* jump */
9440594615
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94406
- assert( pOp->p5<ArraySize(p->aCounter) );
94616
+ assert( pOp->p5==0
94617
+ || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
94618
+ || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
9440794619
pC = p->apCsr[pOp->p1];
9440894620
assert( pC!=0 );
9440994621
assert( pC->deferredMoveto==0 );
9441094622
assert( pC->eCurType==CURTYPE_BTREE );
9441194623
assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
@@ -94608,14 +94820,14 @@
9460894820
9460994821
/* The IdxRowid and Seek opcodes are combined because of the commonality
9461094822
** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
9461194823
rc = sqlite3VdbeCursorRestore(pC);
9461294824
94613
- /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
94614
- ** out from under the cursor. That will never happens for an IdxRowid
94615
- ** or Seek opcode */
94616
- if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
94825
+ /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
94826
+ ** since it was last positioned and an error (e.g. OOM or an IO error)
94827
+ ** occurs while trying to reposition it. */
94828
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
9461794829
9461894830
if( !pC->nullRow ){
9461994831
rowid = 0; /* Not needed. Only used to silence a warning. */
9462094832
rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
9462194833
if( rc!=SQLITE_OK ){
@@ -95513,11 +95725,11 @@
9551395725
9551495726
/* Opcode: OffsetLimit P1 P2 P3 * *
9551595727
** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
9551695728
**
9551795729
** This opcode performs a commonly used computation associated with
95518
-** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
95730
+** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
9551995731
** holds the offset counter. The opcode computes the combined value
9552095732
** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
9552195733
** value computed is the total number of rows that will need to be
9552295734
** visited in order to complete the query.
9552395735
**
@@ -101110,10 +101322,12 @@
101110101322
sqlite3_file *pJfd, /* Preallocated, blank file handle */
101111101323
int flags, /* Opening flags */
101112101324
int nSpill /* Bytes buffered before opening the file */
101113101325
){
101114101326
MemJournal *p = (MemJournal*)pJfd;
101327
+
101328
+ assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) );
101115101329
101116101330
/* Zero the file-handle object. If nSpill was passed zero, initialize
101117101331
** it using the sqlite3OsOpen() function of the underlying VFS. In this
101118101332
** case none of the code in this module is executed as a result of calls
101119101333
** made on the journal file-handle. */
@@ -101552,13 +101766,11 @@
101552101766
if( ExprHasProperty(pExpr, EP_WinFunc) ){
101553101767
if( ALWAYS(pExpr->y.pWin!=0) ){
101554101768
pExpr->y.pWin->pOwner = pExpr;
101555101769
}
101556101770
}
101557
- sqlite3ParserAddCleanup(pParse,
101558
- (void(*)(sqlite3*,void*))sqlite3ExprDelete,
101559
- pDup);
101771
+ sqlite3ExprDeferredDelete(pParse, pDup);
101560101772
}
101561101773
}
101562101774
101563101775
/*
101564101776
** Subqueries stores the original database, table and column names for their
@@ -104363,11 +104575,13 @@
104363104575
** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
104364104576
** if appropriate.
104365104577
*/
104366104578
static void exprSetHeight(Expr *p){
104367104579
int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104368
- if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight;
104580
+ if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
104581
+ nHeight = p->pRight->nHeight;
104582
+ }
104369104583
if( ExprUseXSelect(p) ){
104370104584
heightOfSelect(p->x.pSelect, &nHeight);
104371104585
}else if( p->x.pList ){
104372104586
heightOfExprList(p->x.pList, &nHeight);
104373104587
p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
@@ -104506,19 +104720,30 @@
104506104720
if( pRoot==0 ){
104507104721
assert( db->mallocFailed );
104508104722
sqlite3ExprDelete(db, pLeft);
104509104723
sqlite3ExprDelete(db, pRight);
104510104724
}else{
104725
+ assert( ExprUseXList(pRoot) );
104726
+ assert( pRoot->x.pSelect==0 );
104511104727
if( pRight ){
104512104728
pRoot->pRight = pRight;
104513104729
pRoot->flags |= EP_Propagate & pRight->flags;
104730
+#if SQLITE_MAX_EXPR_DEPTH>0
104731
+ pRoot->nHeight = pRight->nHeight+1;
104732
+ }else{
104733
+ pRoot->nHeight = 1;
104734
+#endif
104514104735
}
104515104736
if( pLeft ){
104516104737
pRoot->pLeft = pLeft;
104517104738
pRoot->flags |= EP_Propagate & pLeft->flags;
104739
+#if SQLITE_MAX_EXPR_DEPTH>0
104740
+ if( pLeft->nHeight>=pRoot->nHeight ){
104741
+ pRoot->nHeight = pLeft->nHeight+1;
104742
+ }
104743
+#endif
104518104744
}
104519
- exprSetHeight(pRoot);
104520104745
}
104521104746
}
104522104747
104523104748
/*
104524104749
** Allocate an Expr node which joins as many as two subtrees.
@@ -104800,10 +105025,11 @@
104800105025
/*
104801105026
** Recursively delete an expression tree.
104802105027
*/
104803105028
static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
104804105029
assert( p!=0 );
105030
+ assert( db!=0 );
104805105031
assert( !ExprUseUValue(p) || p->u.iValue>=0 );
104806105032
assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
104807105033
assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
104808105034
assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
104809105035
#ifdef SQLITE_DEBUG
@@ -104831,16 +105057,12 @@
104831105057
sqlite3WindowDelete(db, p->y.pWin);
104832105058
}
104833105059
#endif
104834105060
}
104835105061
}
104836
- if( ExprHasProperty(p, EP_MemToken) ){
104837
- assert( !ExprHasProperty(p, EP_IntValue) );
104838
- sqlite3DbFree(db, p->u.zToken);
104839
- }
104840105062
if( !ExprHasProperty(p, EP_Static) ){
104841
- sqlite3DbFreeNN(db, p);
105063
+ sqlite3DbNNFreeNN(db, p);
104842105064
}
104843105065
}
104844105066
SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
104845105067
if( p ) sqlite3ExprDeleteNN(db, p);
104846105068
}
@@ -104867,12 +105089,13 @@
104867105089
**
104868105090
** The deferred delete is (currently) implemented by adding the
104869105091
** pExpr to the pParse->pConstExpr list with a register number of 0.
104870105092
*/
104871105093
SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
104872
- pParse->pConstExpr =
104873
- sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
105094
+ sqlite3ParserAddCleanup(pParse,
105095
+ (void(*)(sqlite3*,void*))sqlite3ExprDelete,
105096
+ pExpr);
104874105097
}
104875105098
104876105099
/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
104877105100
** expression.
104878105101
*/
@@ -104942,11 +105165,10 @@
104942105165
){
104943105166
nSize = EXPR_FULLSIZE;
104944105167
}else{
104945105168
assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
104946105169
assert( !ExprHasProperty(p, EP_OuterON) );
104947
- assert( !ExprHasProperty(p, EP_MemToken) );
104948105170
assert( !ExprHasVVAProperty(p, EP_NoReduce) );
104949105171
if( p->pLeft || p->x.pList ){
104950105172
nSize = EXPR_REDUCEDSIZE | EP_Reduced;
104951105173
}else{
104952105174
assert( p->pRight==0 );
@@ -105046,11 +105268,11 @@
105046105268
memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
105047105269
}
105048105270
}
105049105271
105050105272
/* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
105051
- pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
105273
+ pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
105052105274
pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
105053105275
pNew->flags |= staticFlag;
105054105276
ExprClearVVAProperties(pNew);
105055105277
if( dupFlags ){
105056105278
ExprSetVVAProperty(pNew, EP_Immutable);
@@ -105622,16 +105844,17 @@
105622105844
*/
105623105845
static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
105624105846
int i = pList->nExpr;
105625105847
struct ExprList_item *pItem = pList->a;
105626105848
assert( pList->nExpr>0 );
105849
+ assert( db!=0 );
105627105850
do{
105628105851
sqlite3ExprDelete(db, pItem->pExpr);
105629
- sqlite3DbFree(db, pItem->zEName);
105852
+ if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
105630105853
pItem++;
105631105854
}while( --i>0 );
105632
- sqlite3DbFreeNN(db, pList);
105855
+ sqlite3DbNNFreeNN(db, pList);
105633105856
}
105634105857
SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
105635105858
if( pList ) exprListDeleteNN(db, pList);
105636105859
}
105637105860
@@ -106918,11 +107141,11 @@
106918107141
if( pLimit ){
106919107142
pLimit->affExpr = SQLITE_AFF_NUMERIC;
106920107143
pLimit = sqlite3PExpr(pParse, TK_NE,
106921107144
sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
106922107145
}
106923
- sqlite3ExprDelete(db, pSel->pLimit->pLeft);
107146
+ sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
106924107147
pSel->pLimit->pLeft = pLimit;
106925107148
}else{
106926107149
/* If there is no pre-existing limit add a limit of 1 */
106927107150
pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
106928107151
pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
@@ -107432,11 +107655,11 @@
107432107655
u8 p5 /* P5 value for OP_Column + FLAGS */
107433107656
){
107434107657
assert( pParse->pVdbe!=0 );
107435107658
sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107436107659
if( p5 ){
107437
- VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
107660
+ VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
107438107661
if( pOp->opcode==OP_Column ) pOp->p5 = p5;
107439107662
}
107440107663
return iReg;
107441107664
}
107442107665
@@ -107501,11 +107724,11 @@
107501107724
/*
107502107725
** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107503107726
** so that a subsequent copy will not be merged into this one.
107504107727
*/
107505107728
static void setDoNotMergeFlagOnCopy(Vdbe *v){
107506
- if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
107729
+ if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
107507107730
sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */
107508107731
}
107509107732
}
107510107733
107511107734
/*
@@ -107672,11 +107895,11 @@
107672107895
Table *pTab = pCol->pTab;
107673107896
sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
107674107897
pCol->iSorterColumn, target);
107675107898
if( pCol->iColumn<0 ){
107676107899
VdbeComment((v,"%s.rowid",pTab->zName));
107677
- }else{
107900
+ }else if( ALWAYS(pTab!=0) ){
107678107901
VdbeComment((v,"%s.%s",
107679107902
pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107680107903
if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
107681107904
sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
107682107905
}
@@ -108267,10 +108490,25 @@
108267108490
** on a LEFT JOIN NULL row.
108268108491
*/
108269108492
case TK_IF_NULL_ROW: {
108270108493
int addrINR;
108271108494
u8 okConstFactor = pParse->okConstFactor;
108495
+ AggInfo *pAggInfo = pExpr->pAggInfo;
108496
+ if( pAggInfo ){
108497
+ assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
108498
+ if( !pAggInfo->directMode ){
108499
+ inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
108500
+ break;
108501
+ }
108502
+ if( pExpr->pAggInfo->useSortingIdx ){
108503
+ sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
108504
+ pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
108505
+ target);
108506
+ inReg = target;
108507
+ break;
108508
+ }
108509
+ }
108272108510
addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108273108511
/* Temporarily disable factoring of constant expressions, since
108274108512
** even though expressions may appear to be constant, they are not
108275108513
** really constant because they originate from the right-hand side
108276108514
** of a LEFT JOIN. */
@@ -108608,11 +108846,11 @@
108608108846
}else{
108609108847
int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
108610108848
if( inReg!=target+i ){
108611108849
VdbeOp *pOp;
108612108850
if( copyOp==OP_Copy
108613
- && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
108851
+ && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
108614108852
&& pOp->p1+pOp->p3+1==inReg
108615108853
&& pOp->p2+pOp->p3+1==target+i
108616108854
&& pOp->p5==0 /* The do-not-merge flag must be clear */
108617108855
){
108618108856
pOp->p3++;
@@ -109644,10 +109882,11 @@
109644109882
** fact is exploited for efficiency.
109645109883
*/
109646109884
SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
109647109885
Walker w;
109648109886
struct RefSrcList x;
109887
+ assert( pParse->db!=0 );
109649109888
memset(&w, 0, sizeof(w));
109650109889
memset(&x, 0, sizeof(x));
109651109890
w.xExprCallback = exprRefToSrcList;
109652109891
w.xSelectCallback = selectRefEnter;
109653109892
w.xSelectCallback2 = selectRefLeave;
@@ -109660,11 +109899,11 @@
109660109899
#ifndef SQLITE_OMIT_WINDOWFUNC
109661109900
if( ExprHasProperty(pExpr, EP_WinFunc) ){
109662109901
sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109663109902
}
109664109903
#endif
109665
- sqlite3DbFree(pParse->db, x.aiExclude);
109904
+ if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
109666109905
if( w.eCode & 0x01 ){
109667109906
return 1;
109668109907
}else if( w.eCode ){
109669109908
return 0;
109670109909
}else{
@@ -109691,21 +109930,22 @@
109691109930
){
109692109931
AggInfo *pAggInfo = pExpr->pAggInfo;
109693109932
int iAgg = pExpr->iAgg;
109694109933
Parse *pParse = pWalker->pParse;
109695109934
sqlite3 *db = pParse->db;
109696
- assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
109697
- if( pExpr->op==TK_AGG_COLUMN ){
109935
+ if( pExpr->op!=TK_AGG_FUNCTION ){
109936
+ assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_IF_NULL_ROW );
109698109937
assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
109699109938
if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109700109939
pExpr = sqlite3ExprDup(db, pExpr, 0);
109701109940
if( pExpr ){
109702109941
pAggInfo->aCol[iAgg].pCExpr = pExpr;
109703109942
sqlite3ExprDeferredDelete(pParse, pExpr);
109704109943
}
109705109944
}
109706109945
}else{
109946
+ assert( pExpr->op==TK_AGG_FUNCTION );
109707109947
assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
109708109948
if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109709109949
pExpr = sqlite3ExprDup(db, pExpr, 0);
109710109950
if( pExpr ){
109711109951
pAggInfo->aFunc[iAgg].pFExpr = pExpr;
@@ -109772,14 +110012,16 @@
109772110012
SrcList *pSrcList = pNC->pSrcList;
109773110013
AggInfo *pAggInfo = pNC->uNC.pAggInfo;
109774110014
109775110015
assert( pNC->ncFlags & NC_UAggInfo );
109776110016
switch( pExpr->op ){
110017
+ case TK_IF_NULL_ROW:
109777110018
case TK_AGG_COLUMN:
109778110019
case TK_COLUMN: {
109779110020
testcase( pExpr->op==TK_AGG_COLUMN );
109780110021
testcase( pExpr->op==TK_COLUMN );
110022
+ testcase( pExpr->op==TK_IF_NULL_ROW );
109781110023
/* Check to see if the column is in one of the tables in the FROM
109782110024
** clause of the aggregate query */
109783110025
if( ALWAYS(pSrcList!=0) ){
109784110026
SrcItem *pItem = pSrcList->a;
109785110027
for(i=0; i<pSrcList->nSrc; i++, pItem++){
@@ -109793,12 +110035,14 @@
109793110035
** is not an entry there already.
109794110036
*/
109795110037
int k;
109796110038
pCol = pAggInfo->aCol;
109797110039
for(k=0; k<pAggInfo->nColumn; k++, pCol++){
109798
- if( pCol->iTable==pExpr->iTable &&
109799
- pCol->iColumn==pExpr->iColumn ){
110040
+ if( pCol->iTable==pExpr->iTable
110041
+ && pCol->iColumn==pExpr->iColumn
110042
+ && pExpr->op!=TK_IF_NULL_ROW
110043
+ ){
109800110044
break;
109801110045
}
109802110046
}
109803110047
if( (k>=pAggInfo->nColumn)
109804110048
&& (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
@@ -109809,19 +110053,21 @@
109809110053
pCol->iTable = pExpr->iTable;
109810110054
pCol->iColumn = pExpr->iColumn;
109811110055
pCol->iMem = ++pParse->nMem;
109812110056
pCol->iSorterColumn = -1;
109813110057
pCol->pCExpr = pExpr;
109814
- if( pAggInfo->pGroupBy ){
110058
+ if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
109815110059
int j, n;
109816110060
ExprList *pGB = pAggInfo->pGroupBy;
109817110061
struct ExprList_item *pTerm = pGB->a;
109818110062
n = pGB->nExpr;
109819110063
for(j=0; j<n; j++, pTerm++){
109820110064
Expr *pE = pTerm->pExpr;
109821
- if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
109822
- pE->iColumn==pExpr->iColumn ){
110065
+ if( pE->op==TK_COLUMN
110066
+ && pE->iTable==pExpr->iTable
110067
+ && pE->iColumn==pExpr->iColumn
110068
+ ){
109823110069
pCol->iSorterColumn = j;
109824110070
break;
109825110071
}
109826110072
}
109827110073
}
@@ -109834,11 +110080,13 @@
109834110080
** Convert the pExpr to be a TK_AGG_COLUMN referring to that
109835110081
** pAggInfo->aCol[] entry.
109836110082
*/
109837110083
ExprSetVVAProperty(pExpr, EP_NoReduce);
109838110084
pExpr->pAggInfo = pAggInfo;
109839
- pExpr->op = TK_AGG_COLUMN;
110085
+ if( pExpr->op==TK_COLUMN ){
110086
+ pExpr->op = TK_AGG_COLUMN;
110087
+ }
109840110088
pExpr->iAgg = (i16)k;
109841110089
break;
109842110090
} /* endif pExpr->iTable==pItem->iCursor */
109843110091
} /* end loop over pSrcList */
109844110092
}
@@ -115256,10 +115504,11 @@
115256115504
** no VDBE code was generated.
115257115505
*/
115258115506
SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
115259115507
sqlite3 *db;
115260115508
Vdbe *v;
115509
+ int iDb, i;
115261115510
115262115511
assert( pParse->pToplevel==0 );
115263115512
db = pParse->db;
115264115513
assert( db->pParse==pParse );
115265115514
if( pParse->nested ) return;
@@ -115285,11 +115534,10 @@
115285115534
|| sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
115286115535
if( v ){
115287115536
if( pParse->bReturning ){
115288115537
Returning *pReturning = pParse->u1.pReturning;
115289115538
int addrRewind;
115290
- int i;
115291115539
int reg;
115292115540
115293115541
if( pReturning->nRetCol ){
115294115542
sqlite3VdbeAddOp0(v, OP_FkCheck);
115295115543
addrRewind =
@@ -115322,80 +115570,73 @@
115322115570
** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
115323115571
** set for each database that is used. Generate code to start a
115324115572
** transaction on each used database and to verify the schema cookie
115325115573
** on each used database.
115326115574
*/
115327
- if( db->mallocFailed==0
115328
- && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
115329
- ){
115330
- int iDb, i;
115331
- assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115332
- sqlite3VdbeJumpHere(v, 0);
115333
- assert( db->nDb>0 );
115334
- iDb = 0;
115335
- do{
115336
- Schema *pSchema;
115337
- if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115338
- sqlite3VdbeUsesBtree(v, iDb);
115339
- pSchema = db->aDb[iDb].pSchema;
115340
- sqlite3VdbeAddOp4Int(v,
115341
- OP_Transaction, /* Opcode */
115342
- iDb, /* P1 */
115343
- DbMaskTest(pParse->writeMask,iDb), /* P2 */
115344
- pSchema->schema_cookie, /* P3 */
115345
- pSchema->iGeneration /* P4 */
115346
- );
115347
- if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115348
- VdbeComment((v,
115349
- "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115350
- }while( ++iDb<db->nDb );
115575
+ assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115576
+ sqlite3VdbeJumpHere(v, 0);
115577
+ assert( db->nDb>0 );
115578
+ iDb = 0;
115579
+ do{
115580
+ Schema *pSchema;
115581
+ if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115582
+ sqlite3VdbeUsesBtree(v, iDb);
115583
+ pSchema = db->aDb[iDb].pSchema;
115584
+ sqlite3VdbeAddOp4Int(v,
115585
+ OP_Transaction, /* Opcode */
115586
+ iDb, /* P1 */
115587
+ DbMaskTest(pParse->writeMask,iDb), /* P2 */
115588
+ pSchema->schema_cookie, /* P3 */
115589
+ pSchema->iGeneration /* P4 */
115590
+ );
115591
+ if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115592
+ VdbeComment((v,
115593
+ "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115594
+ }while( ++iDb<db->nDb );
115351115595
#ifndef SQLITE_OMIT_VIRTUALTABLE
115352
- for(i=0; i<pParse->nVtabLock; i++){
115353
- char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115354
- sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
115355
- }
115356
- pParse->nVtabLock = 0;
115596
+ for(i=0; i<pParse->nVtabLock; i++){
115597
+ char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115598
+ sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
115599
+ }
115600
+ pParse->nVtabLock = 0;
115357115601
#endif
115358115602
115359
- /* Once all the cookies have been verified and transactions opened,
115360
- ** obtain the required table-locks. This is a no-op unless the
115361
- ** shared-cache feature is enabled.
115362
- */
115363
- codeTableLocks(pParse);
115364
-
115365
- /* Initialize any AUTOINCREMENT data structures required.
115366
- */
115367
- sqlite3AutoincrementBegin(pParse);
115368
-
115369
- /* Code constant expressions that where factored out of inner loops.
115370
- **
115371
- ** The pConstExpr list might also contain expressions that we simply
115372
- ** want to keep around until the Parse object is deleted. Such
115373
- ** expressions have iConstExprReg==0. Do not generate code for
115374
- ** those expressions, of course.
115375
- */
115376
- if( pParse->pConstExpr ){
115377
- ExprList *pEL = pParse->pConstExpr;
115378
- pParse->okConstFactor = 0;
115379
- for(i=0; i<pEL->nExpr; i++){
115380
- int iReg = pEL->a[i].u.iConstExprReg;
115381
- if( iReg>0 ){
115382
- sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115383
- }
115384
- }
115385
- }
115386
-
115387
- if( pParse->bReturning ){
115388
- Returning *pRet = pParse->u1.pReturning;
115389
- if( pRet->nRetCol ){
115390
- sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115391
- }
115392
- }
115393
-
115394
- /* Finally, jump back to the beginning of the executable code. */
115395
- sqlite3VdbeGoto(v, 1);
115396
- }
115603
+ /* Once all the cookies have been verified and transactions opened,
115604
+ ** obtain the required table-locks. This is a no-op unless the
115605
+ ** shared-cache feature is enabled.
115606
+ */
115607
+ codeTableLocks(pParse);
115608
+
115609
+ /* Initialize any AUTOINCREMENT data structures required.
115610
+ */
115611
+ sqlite3AutoincrementBegin(pParse);
115612
+
115613
+ /* Code constant expressions that where factored out of inner loops.
115614
+ **
115615
+ ** The pConstExpr list might also contain expressions that we simply
115616
+ ** want to keep around until the Parse object is deleted. Such
115617
+ ** expressions have iConstExprReg==0. Do not generate code for
115618
+ ** those expressions, of course.
115619
+ */
115620
+ if( pParse->pConstExpr ){
115621
+ ExprList *pEL = pParse->pConstExpr;
115622
+ pParse->okConstFactor = 0;
115623
+ for(i=0; i<pEL->nExpr; i++){
115624
+ int iReg = pEL->a[i].u.iConstExprReg;
115625
+ sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115626
+ }
115627
+ }
115628
+
115629
+ if( pParse->bReturning ){
115630
+ Returning *pRet = pParse->u1.pReturning;
115631
+ if( pRet->nRetCol ){
115632
+ sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115633
+ }
115634
+ }
115635
+
115636
+ /* Finally, jump back to the beginning of the executable code. */
115637
+ sqlite3VdbeGoto(v, 1);
115397115638
}
115398115639
115399115640
/* Get the VDBE program ready for execution
115400115641
*/
115401115642
assert( v!=0 || pParse->nErr );
@@ -115898,20 +116139,21 @@
115898116139
*/
115899116140
SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
115900116141
int i;
115901116142
Column *pCol;
115902116143
assert( pTable!=0 );
116144
+ assert( db!=0 );
115903116145
if( (pCol = pTable->aCol)!=0 ){
115904116146
for(i=0; i<pTable->nCol; i++, pCol++){
115905116147
assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
115906116148
sqlite3DbFree(db, pCol->zCnName);
115907116149
}
115908
- sqlite3DbFree(db, pTable->aCol);
116150
+ sqlite3DbNNFreeNN(db, pTable->aCol);
115909116151
if( IsOrdinaryTable(pTable) ){
115910116152
sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
115911116153
}
115912
- if( db==0 || db->pnBytesFreed==0 ){
116154
+ if( db->pnBytesFreed==0 ){
115913116155
pTable->aCol = 0;
115914116156
pTable->nCol = 0;
115915116157
if( IsOrdinaryTable(pTable) ){
115916116158
pTable->u.tab.pDfltList = 0;
115917116159
}
@@ -115944,21 +116186,22 @@
115944116186
**
115945116187
** If malloc has already failed, it may be that it failed while allocating
115946116188
** a Table object that was going to be marked ephemeral. So do not check
115947116189
** that no lookaside memory is used in this case either. */
115948116190
int nLookaside = 0;
115949
- if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
116191
+ assert( db!=0 );
116192
+ if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
115950116193
nLookaside = sqlite3LookasideUsed(db, 0);
115951116194
}
115952116195
#endif
115953116196
115954116197
/* Delete all indices associated with this table. */
115955116198
for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
115956116199
pNext = pIndex->pNext;
115957116200
assert( pIndex->pSchema==pTable->pSchema
115958116201
|| (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
115959
- if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
116202
+ if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
115960116203
char *zName = pIndex->zName;
115961116204
TESTONLY ( Index *pOld = ) sqlite3HashInsert(
115962116205
&pIndex->pSchema->idxHash, zName, 0
115963116206
);
115964116207
assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
@@ -115991,12 +116234,13 @@
115991116234
/* Verify that no lookaside memory was used by schema tables */
115992116235
assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
115993116236
}
115994116237
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
115995116238
/* Do not delete the table until the reference count reaches zero. */
116239
+ assert( db!=0 );
115996116240
if( !pTable ) return;
115997
- if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
116241
+ if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
115998116242
deleteTable(db, pTable);
115999116243
}
116000116244
116001116245
116002116246
/*
@@ -118165,11 +118409,11 @@
118165118409
/*
118166118410
** The Table structure pTable is really a VIEW. Fill in the names of
118167118411
** the columns of the view in the pTable structure. Return the number
118168118412
** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118169118413
*/
118170
-SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
118414
+static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
118171118415
Table *pSelTab; /* A fake table from which we get the result set */
118172118416
Select *pSel; /* Copy of the SELECT that implements the view */
118173118417
int nErr = 0; /* Number of errors encountered */
118174118418
sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118175118419
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -118190,13 +118434,14 @@
118190118434
}
118191118435
#endif
118192118436
118193118437
#ifndef SQLITE_OMIT_VIEW
118194118438
/* A positive nCol means the columns names for this view are
118195
- ** already known.
118439
+ ** already known. This routine is not called unless either the
118440
+ ** table is virtual or nCol is zero.
118196118441
*/
118197
- if( pTable->nCol>0 ) return 0;
118442
+ assert( pTable->nCol<=0 );
118198118443
118199118444
/* A negative nCol is a special marker meaning that we are currently
118200118445
** trying to compute the column names. If we enter this routine with
118201118446
** a negative nCol, it means two or more views form a loop, like this:
118202118447
**
@@ -118287,10 +118532,15 @@
118287118532
if( db->mallocFailed ){
118288118533
sqlite3DeleteColumnNames(db, pTable);
118289118534
}
118290118535
#endif /* SQLITE_OMIT_VIEW */
118291118536
return nErr;
118537
+}
118538
+SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
118539
+ assert( pTable!=0 );
118540
+ if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
118541
+ return viewGetColumnNames(pParse, pTable);
118292118542
}
118293118543
#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
118294118544
118295118545
#ifndef SQLITE_OMIT_VIEW
118296118546
/*
@@ -119153,11 +119403,11 @@
119153119403
if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119154119404
goto exit_create_index;
119155119405
}
119156119406
if( !IN_RENAME_OBJECT ){
119157119407
if( !db->init.busy ){
119158
- if( sqlite3FindTable(db, zName, 0)!=0 ){
119408
+ if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
119159119409
sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
119160119410
goto exit_create_index;
119161119411
}
119162119412
}
119163119413
if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
@@ -119806,16 +120056,17 @@
119806120056
/*
119807120057
** Delete an IdList.
119808120058
*/
119809120059
SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
119810120060
int i;
120061
+ assert( db!=0 );
119811120062
if( pList==0 ) return;
119812120063
assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
119813120064
for(i=0; i<pList->nId; i++){
119814120065
sqlite3DbFree(db, pList->a[i].zName);
119815120066
}
119816
- sqlite3DbFreeNN(db, pList);
120067
+ sqlite3DbNNFreeNN(db, pList);
119817120068
}
119818120069
119819120070
/*
119820120071
** Return the index in pList of the identifier named zId. Return -1
119821120072
** if not found.
@@ -120014,15 +120265,16 @@
120014120265
** Delete an entire SrcList including all its substructure.
120015120266
*/
120016120267
SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
120017120268
int i;
120018120269
SrcItem *pItem;
120270
+ assert( db!=0 );
120019120271
if( pList==0 ) return;
120020120272
for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120021
- if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
120022
- sqlite3DbFree(db, pItem->zName);
120023
- if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
120273
+ if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
120274
+ if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
120275
+ if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
120024120276
if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120025120277
if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120026120278
sqlite3DeleteTable(db, pItem->pTab);
120027120279
if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120028120280
if( pItem->fg.isUsing ){
@@ -120029,11 +120281,11 @@
120029120281
sqlite3IdListDelete(db, pItem->u3.pUsing);
120030120282
}else if( pItem->u3.pOn ){
120031120283
sqlite3ExprDelete(db, pItem->u3.pOn);
120032120284
}
120033120285
}
120034
- sqlite3DbFreeNN(db, pList);
120286
+ sqlite3DbNNFreeNN(db, pList);
120035120287
}
120036120288
120037120289
/*
120038120290
** This routine is called by the parser to add a new term to the
120039120291
** end of a growing FROM clause. The "p" parameter is the part of
@@ -121281,23 +121533,25 @@
121281121533
SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
121282121534
Hash temp1;
121283121535
Hash temp2;
121284121536
HashElem *pElem;
121285121537
Schema *pSchema = (Schema *)p;
121538
+ sqlite3 xdb;
121286121539
121540
+ memset(&xdb, 0, sizeof(xdb));
121287121541
temp1 = pSchema->tblHash;
121288121542
temp2 = pSchema->trigHash;
121289121543
sqlite3HashInit(&pSchema->trigHash);
121290121544
sqlite3HashClear(&pSchema->idxHash);
121291121545
for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
121292
- sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
121546
+ sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
121293121547
}
121294121548
sqlite3HashClear(&temp2);
121295121549
sqlite3HashInit(&pSchema->tblHash);
121296121550
for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
121297121551
Table *pTab = sqliteHashData(pElem);
121298
- sqlite3DeleteTable(0, pTab);
121552
+ sqlite3DeleteTable(&xdb, pTab);
121299121553
}
121300121554
sqlite3HashClear(&temp1);
121301121555
sqlite3HashClear(&pSchema->fkeyHash);
121302121556
pSchema->pSeqTab = 0;
121303121557
if( pSchema->schemaFlags & DB_SchemaLoaded ){
@@ -121392,22 +121646,46 @@
121392121646
** A table is read-only if any of the following are true:
121393121647
**
121394121648
** 1) It is a virtual table and no implementation of the xUpdate method
121395121649
** has been provided
121396121650
**
121397
-** 2) It is a system table (i.e. sqlite_schema), this call is not
121651
+** 2) A trigger is currently being coded and the table is a virtual table
121652
+** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
121653
+** the table is not SQLITE_VTAB_INNOCUOUS.
121654
+**
121655
+** 3) It is a system table (i.e. sqlite_schema), this call is not
121398121656
** part of a nested parse and writable_schema pragma has not
121399121657
** been specified
121400121658
**
121401
-** 3) The table is a shadow table, the database connection is in
121659
+** 4) The table is a shadow table, the database connection is in
121402121660
** defensive mode, and the current sqlite3_prepare()
121403121661
** is for a top-level SQL statement.
121404121662
*/
121663
+static int vtabIsReadOnly(Parse *pParse, Table *pTab){
121664
+ if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
121665
+ return 1;
121666
+ }
121667
+
121668
+ /* Within triggers:
121669
+ ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
121670
+ ** virtual tables
121671
+ ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
121672
+ ** virtual tables if PRAGMA trusted_schema=ON.
121673
+ */
121674
+ if( pParse->pToplevel!=0
121675
+ && pTab->u.vtab.p->eVtabRisk >
121676
+ ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
121677
+ ){
121678
+ sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
121679
+ pTab->zName);
121680
+ }
121681
+ return 0;
121682
+}
121405121683
static int tabIsReadOnly(Parse *pParse, Table *pTab){
121406121684
sqlite3 *db;
121407121685
if( IsVirtual(pTab) ){
121408
- return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
121686
+ return vtabIsReadOnly(pParse, pTab);
121409121687
}
121410121688
if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
121411121689
db = pParse->db;
121412121690
if( (pTab->tabFlags & TF_Readonly)!=0 ){
121413121691
return sqlite3WritableSchema(db)==0 && pParse->nested==0;
@@ -121415,13 +121693,15 @@
121415121693
assert( pTab->tabFlags & TF_Shadow );
121416121694
return sqlite3ReadOnlyShadowTables(db);
121417121695
}
121418121696
121419121697
/*
121420
-** Check to make sure the given table is writable. If it is not
121421
-** writable, generate an error message and return 1. If it is
121422
-** writable return 0;
121698
+** Check to make sure the given table is writable.
121699
+**
121700
+** If pTab is not writable -> generate an error message and return 1.
121701
+** If pTab is writable but other errors have occurred -> return 1.
121702
+** If pTab is writable and no prior errors -> return 0;
121423121703
*/
121424121704
SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
121425121705
if( tabIsReadOnly(pParse, pTab) ){
121426121706
sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121427121707
return 1;
@@ -121778,13 +122058,14 @@
121778122058
sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
121779122059
pTab->zName, P4_STATIC);
121780122060
}
121781122061
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121782122062
assert( pIdx->pSchema==pTab->pSchema );
121783
- sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
121784122063
if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
121785
- sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
122064
+ sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
122065
+ }else{
122066
+ sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
121786122067
}
121787122068
}
121788122069
}else
121789122070
#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
121790122071
{
@@ -121980,11 +122261,11 @@
121980122261
sqlite3ExprDelete(db, pWhere);
121981122262
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
121982122263
sqlite3ExprListDelete(db, pOrderBy);
121983122264
sqlite3ExprDelete(db, pLimit);
121984122265
#endif
121985
- sqlite3DbFree(db, aToOpen);
122266
+ if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen);
121986122267
return;
121987122268
}
121988122269
/* Make sure "isView" and other macros defined above are undefined. Otherwise
121989122270
** they may interfere with compilation of other functions in this file
121990122271
** (or in another file, if this file becomes part of the amalgamation). */
@@ -126148,15 +126429,16 @@
126148126429
SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
126149126430
FKey *pFKey; /* Iterator variable */
126150126431
FKey *pNext; /* Copy of pFKey->pNextFrom */
126151126432
126152126433
assert( IsOrdinaryTable(pTab) );
126434
+ assert( db!=0 );
126153126435
for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126154126436
assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
126155126437
126156126438
/* Remove the FK from the fkeyHash hash table. */
126157
- if( !db || db->pnBytesFreed==0 ){
126439
+ if( db->pnBytesFreed==0 ){
126158126440
if( pFKey->pPrevTo ){
126159126441
pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126160126442
}else{
126161126443
void *p = (void *)pFKey->pNextTo;
126162126444
const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
@@ -126345,11 +126627,11 @@
126345126627
/* Move the previous opcode (which should be OP_MakeRecord) forward
126346126628
** by one slot and insert a new OP_TypeCheck where the current
126347126629
** OP_MakeRecord is found */
126348126630
VdbeOp *pPrev;
126349126631
sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
126350
- pPrev = sqlite3VdbeGetOp(v, -1);
126632
+ pPrev = sqlite3VdbeGetLastOp(v);
126351126633
assert( pPrev!=0 );
126352126634
assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
126353126635
pPrev->opcode = OP_TypeCheck;
126354126636
sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
126355126637
}else{
@@ -126383,11 +126665,11 @@
126383126665
i = sqlite3Strlen30NN(zColAff);
126384126666
if( i ){
126385126667
if( iReg ){
126386126668
sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
126387126669
}else{
126388
- assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord
126670
+ assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
126389126671
|| sqlite3VdbeDb(v)->mallocFailed );
126390126672
sqlite3VdbeChangeP4(v, -1, zColAff, i);
126391126673
}
126392126674
}
126393126675
}
@@ -126469,11 +126751,11 @@
126469126751
/* Before computing generated columns, first go through and make sure
126470126752
** that appropriate affinity has been applied to the regular columns
126471126753
*/
126472126754
sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126473126755
if( (pTab->tabFlags & TF_HasStored)!=0 ){
126474
- pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
126756
+ pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
126475126757
if( pOp->opcode==OP_Affinity ){
126476126758
/* Change the OP_Affinity argument to '@' (NONE) for all stored
126477126759
** columns. '@' is the no-op affinity and those columns have not
126478126760
** yet been computed. */
126479126761
int ii, jj;
@@ -127375,11 +127657,16 @@
127375127657
}else if( pSelect ){
127376127658
if( regFromSelect!=regData ){
127377127659
sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
127378127660
}
127379127661
}else{
127380
- sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
127662
+ Expr *pX = pList->a[k].pExpr;
127663
+ int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
127664
+ if( y!=iRegStore ){
127665
+ sqlite3VdbeAddOp2(v,
127666
+ ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore);
127667
+ }
127381127668
}
127382127669
}
127383127670
127384127671
127385127672
/* Run the BEFORE and INSTEAD OF triggers, if there are any
@@ -127512,11 +127799,13 @@
127512127799
int isReplace = 0;/* Set to true if constraints may cause a replace */
127513127800
int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
127514127801
sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
127515127802
regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
127516127803
);
127517
- sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
127804
+ if( db->flags & SQLITE_ForeignKeys ){
127805
+ sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
127806
+ }
127518127807
127519127808
/* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
127520127809
** constraints or (b) there are no triggers and this table is not a
127521127810
** parent table in a foreign key constraint. It is safe to set the
127522127811
** flag in the second case as if any REPLACE constraint is hit, an
@@ -127596,11 +127885,11 @@
127596127885
sqlite3SrcListDelete(db, pTabList);
127597127886
sqlite3ExprListDelete(db, pList);
127598127887
sqlite3UpsertDelete(db, pUpsert);
127599127888
sqlite3SelectDelete(db, pSelect);
127600127889
sqlite3IdListDelete(db, pColumn);
127601
- sqlite3DbFree(db, aRegIdx);
127890
+ if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
127602127891
}
127603127892
127604127893
/* Make sure "isView" and other macros defined above are undefined. Otherwise
127605127894
** they may interfere with compilation of other functions in this file
127606127895
** (or in another file, if this file becomes part of the amalgamation). */
@@ -132702,19 +132991,21 @@
132702132991
** Setting to a null string reverts to the default temporary directory search.
132703132992
** If temporary directory is changed, then invalidateTempStorage.
132704132993
**
132705132994
*/
132706132995
case PragTyp_TEMP_STORE_DIRECTORY: {
132996
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132707132997
if( !zRight ){
132708132998
returnSingleText(v, sqlite3_temp_directory);
132709132999
}else{
132710133000
#ifndef SQLITE_OMIT_WSD
132711133001
if( zRight[0] ){
132712133002
int res;
132713133003
rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132714133004
if( rc!=SQLITE_OK || res==0 ){
132715133005
sqlite3ErrorMsg(pParse, "not a writable directory");
133006
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132716133007
goto pragma_out;
132717133008
}
132718133009
}
132719133010
if( SQLITE_TEMP_STORE==0
132720133011
|| (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
@@ -132728,10 +133019,11 @@
132728133019
}else{
132729133020
sqlite3_temp_directory = 0;
132730133021
}
132731133022
#endif /* SQLITE_OMIT_WSD */
132732133023
}
133024
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132733133025
break;
132734133026
}
132735133027
132736133028
#if SQLITE_OS_WIN
132737133029
/*
@@ -132746,19 +133038,21 @@
132746133038
** process. Database file specified with an absolute path are not impacted
132747133039
** by this setting, regardless of its value.
132748133040
**
132749133041
*/
132750133042
case PragTyp_DATA_STORE_DIRECTORY: {
133043
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132751133044
if( !zRight ){
132752133045
returnSingleText(v, sqlite3_data_directory);
132753133046
}else{
132754133047
#ifndef SQLITE_OMIT_WSD
132755133048
if( zRight[0] ){
132756133049
int res;
132757133050
rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132758133051
if( rc!=SQLITE_OK || res==0 ){
132759133052
sqlite3ErrorMsg(pParse, "not a writable directory");
133053
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132760133054
goto pragma_out;
132761133055
}
132762133056
}
132763133057
sqlite3_free(sqlite3_data_directory);
132764133058
if( zRight[0] ){
@@ -132766,10 +133060,11 @@
132766133060
}else{
132767133061
sqlite3_data_directory = 0;
132768133062
}
132769133063
#endif /* SQLITE_OMIT_WSD */
132770133064
}
133065
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132771133066
break;
132772133067
}
132773133068
#endif
132774133069
132775133070
#if SQLITE_ENABLE_LOCKING_STYLE
@@ -133479,19 +133774,27 @@
133479133774
/* Make sure all the indices are constructed correctly.
133480133775
*/
133481133776
for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
133482133777
Table *pTab = sqliteHashData(x);
133483133778
Index *pIdx, *pPk;
133484
- Index *pPrior = 0;
133779
+ Index *pPrior = 0; /* Previous index */
133485133780
int loopTop;
133486133781
int iDataCur, iIdxCur;
133487133782
int r1 = -1;
133488133783
int bStrict;
133784
+ int r2; /* Previous key for WITHOUT ROWID tables */
133489133785
133490133786
if( !IsOrdinaryTable(pTab) ) continue;
133491133787
if( pObjTab && pObjTab!=pTab ) continue;
133492
- pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
133788
+ if( isQuick || HasRowid(pTab) ){
133789
+ pPk = 0;
133790
+ r2 = 0;
133791
+ }else{
133792
+ pPk = sqlite3PrimaryKeyIndex(pTab);
133793
+ r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
133794
+ sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
133795
+ }
133493133796
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
133494133797
1, 0, &iDataCur, &iIdxCur);
133495133798
/* reg[7] counts the number of entries in the table.
133496133799
** reg[8+i] counts the number of entries in the i-th index
133497133800
*/
@@ -133506,10 +133809,28 @@
133506133809
if( !isQuick ){
133507133810
/* Sanity check on record header decoding */
133508133811
sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133509133812
sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133510133813
VdbeComment((v, "(right-most column)"));
133814
+ if( pPk ){
133815
+ /* Verify WITHOUT ROWID keys are in ascending order */
133816
+ int a1;
133817
+ char *zErr;
133818
+ a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
133819
+ VdbeCoverage(v);
133820
+ sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v);
133821
+ zErr = sqlite3MPrintf(db,
133822
+ "row not in PRIMARY KEY order for %s",
133823
+ pTab->zName);
133824
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133825
+ integrityCheckResultRow(v);
133826
+ sqlite3VdbeJumpHere(v, a1);
133827
+ sqlite3VdbeJumpHere(v, a1+1);
133828
+ for(j=0; j<pPk->nKeyCol; j++){
133829
+ sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
133830
+ }
133831
+ }
133511133832
}
133512133833
/* Verify that all NOT NULL columns really are NOT NULL. At the
133513133834
** same time verify the type of the content of STRICT tables */
133514133835
bStrict = (pTab->tabFlags & TF_Strict)!=0;
133515133836
for(j=0; j<pTab->nCol; j++){
@@ -133518,11 +133839,11 @@
133518133839
int doError, jmp2;
133519133840
if( j==pTab->iPKey ) continue;
133520133841
if( pCol->notNull==0 && !bStrict ) continue;
133521133842
doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133522133843
sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133523
- if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
133844
+ if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){
133524133845
sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133525133846
}
133526133847
if( pCol->notNull ){
133527133848
jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
133528133849
zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
@@ -133533,13 +133854,11 @@
133533133854
}else{
133534133855
integrityCheckResultRow(v);
133535133856
}
133536133857
sqlite3VdbeJumpHere(v, jmp2);
133537133858
}
133538
- if( (pTab->tabFlags & TF_Strict)!=0
133539
- && pCol->eCType!=COLTYPE_ANY
133540
- ){
133859
+ if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133541133860
jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133542133861
sqlite3StdTypeMap[pCol->eCType-1]);
133543133862
VdbeCoverage(v);
133544133863
zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133545133864
sqlite3StdType[pCol->eCType-1],
@@ -133634,10 +133953,13 @@
133634133953
sqlite3VdbeLoadString(v, 4, pIdx->zName);
133635133954
sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
133636133955
integrityCheckResultRow(v);
133637133956
sqlite3VdbeJumpHere(v, addr);
133638133957
}
133958
+ if( pPk ){
133959
+ sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
133960
+ }
133639133961
}
133640133962
}
133641133963
}
133642133964
{
133643133965
static const int iLn = VDBE_OFFSET_LINENO(2);
@@ -135032,19 +135354,19 @@
135032135354
sqlite3 *db = pParse->db;
135033135355
assert( db!=0 );
135034135356
assert( db->pParse==pParse );
135035135357
assert( pParse->nested==0 );
135036135358
#ifndef SQLITE_OMIT_SHARED_CACHE
135037
- sqlite3DbFree(db, pParse->aTableLock);
135359
+ if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
135038135360
#endif
135039135361
while( pParse->pCleanup ){
135040135362
ParseCleanup *pCleanup = pParse->pCleanup;
135041135363
pParse->pCleanup = pCleanup->pNext;
135042135364
pCleanup->xCleanup(db, pCleanup->pPtr);
135043
- sqlite3DbFreeNN(db, pCleanup);
135365
+ sqlite3DbNNFreeNN(db, pCleanup);
135044135366
}
135045
- sqlite3DbFree(db, pParse->aLabel);
135367
+ if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
135046135368
if( pParse->pConstExpr ){
135047135369
sqlite3ExprListDelete(db, pParse->pConstExpr);
135048135370
}
135049135371
assert( db->lookaside.bDisable >= pParse->disableLookaside );
135050135372
db->lookaside.bDisable -= pParse->disableLookaside;
@@ -135599,10 +135921,11 @@
135599135921
**
135600135922
** If bFree==1, call sqlite3DbFree() on the p object.
135601135923
** If bFree==0, Leave the first Select object unfreed
135602135924
*/
135603135925
static void clearSelect(sqlite3 *db, Select *p, int bFree){
135926
+ assert( db!=0 );
135604135927
while( p ){
135605135928
Select *pPrior = p->pPrior;
135606135929
sqlite3ExprListDelete(db, p->pEList);
135607135930
sqlite3SrcListDelete(db, p->pSrc);
135608135931
sqlite3ExprDelete(db, p->pWhere);
@@ -135618,11 +135941,11 @@
135618135941
while( p->pWin ){
135619135942
assert( p->pWin->ppThis==&p->pWin );
135620135943
sqlite3WindowUnlinkFromSelect(p->pWin);
135621135944
}
135622135945
#endif
135623
- if( bFree ) sqlite3DbFreeNN(db, p);
135946
+ if( bFree ) sqlite3DbNNFreeNN(db, p);
135624135947
p = pPrior;
135625135948
bFree = 1;
135626135949
}
135627135950
}
135628135951
@@ -137024,13 +137347,14 @@
137024137347
/*
137025137348
** Deallocate a KeyInfo object
137026137349
*/
137027137350
SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
137028137351
if( p ){
137352
+ assert( p->db!=0 );
137029137353
assert( p->nRef>0 );
137030137354
p->nRef--;
137031
- if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
137355
+ if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
137032137356
}
137033137357
}
137034137358
137035137359
/*
137036137360
** Make a new pointer to a KeyInfo object
@@ -137211,18 +137535,21 @@
137211137535
sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
137212137536
nKey+1+nColumn+nRefKey);
137213137537
if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137214137538
addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
137215137539
VdbeCoverage(v);
137216
- codeOffset(v, p->iOffset, addrContinue);
137540
+ assert( p->iLimit==0 && p->iOffset==0 );
137217137541
sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
137218137542
bSeq = 0;
137219137543
}else{
137220137544
addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
137221137545
codeOffset(v, p->iOffset, addrContinue);
137222137546
iSortTab = iTab;
137223137547
bSeq = 1;
137548
+ if( p->iOffset>0 ){
137549
+ sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
137550
+ }
137224137551
}
137225137552
for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137226137553
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
137227137554
if( aOutEx[i].fg.bSorterRef ) continue;
137228137555
#endif
@@ -137343,13 +137670,10 @@
137343137670
137344137671
/*
137345137672
** Return a pointer to a string containing the 'declaration type' of the
137346137673
** expression pExpr. The string may be treated as static by the caller.
137347137674
**
137348
-** Also try to estimate the size of the returned value and return that
137349
-** result in *pEstWidth.
137350
-**
137351137675
** The declaration type is the exact datatype definition extracted from the
137352137676
** original CREATE TABLE statement if the expression is a column. The
137353137677
** declaration type for a ROWID field is INTEGER. Exactly when an expression
137354137678
** is considered a column can be complex in the presence of subqueries. The
137355137679
** result-set expression in all of the following SELECT statements is
@@ -139211,14 +139535,15 @@
139211139535
139212139536
/* Jump to the this point in order to terminate the query.
139213139537
*/
139214139538
sqlite3VdbeResolveLabel(v, labelEnd);
139215139539
139216
- /* Reassembly the compound query so that it will be freed correctly
139540
+ /* Reassemble the compound query so that it will be freed correctly
139217139541
** by the calling function */
139218139542
if( pSplit->pPrior ){
139219
- sqlite3SelectDelete(db, pSplit->pPrior);
139543
+ sqlite3ParserAddCleanup(pParse,
139544
+ (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
139220139545
}
139221139546
pSplit->pPrior = pPrior;
139222139547
pPrior->pNext = pSplit;
139223139548
sqlite3ExprListDelete(db, pPrior->pOrderBy);
139224139549
pPrior->pOrderBy = 0;
@@ -139324,10 +139649,11 @@
139324139649
if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
139325139650
memset(&ifNullRow, 0, sizeof(ifNullRow));
139326139651
ifNullRow.op = TK_IF_NULL_ROW;
139327139652
ifNullRow.pLeft = pCopy;
139328139653
ifNullRow.iTable = pSubst->iNewTable;
139654
+ ifNullRow.iColumn = -99;
139329139655
ifNullRow.flags = EP_IfNullRow;
139330139656
pCopy = &ifNullRow;
139331139657
}
139332139658
testcase( ExprHasProperty(pCopy, EP_Subquery) );
139333139659
pNew = sqlite3ExprDup(db, pCopy, 0);
@@ -139591,11 +139917,12 @@
139591139917
**
139592139918
** (3) If the subquery is the right operand of a LEFT JOIN then
139593139919
** (3a) the subquery may not be a join and
139594139920
** (3b) the FROM clause of the subquery may not contain a virtual
139595139921
** table and
139596
-** (3c) the outer query may not be an aggregate.
139922
+** (**) Was: "The outer query may not have a GROUP BY." This case
139923
+** is now managed correctly
139597139924
** (3d) the outer query may not be DISTINCT.
139598139925
** See also (26) for restrictions on RIGHT JOIN.
139599139926
**
139600139927
** (4) The subquery can not be DISTINCT.
139601139928
**
@@ -139803,20 +140130,14 @@
139803140130
**
139804140131
** (t1 LEFT OUTER JOIN t2) JOIN t3
139805140132
**
139806140133
** which is not at all the same thing.
139807140134
**
139808
- ** If the subquery is the right operand of a LEFT JOIN, then the outer
139809
- ** query cannot be an aggregate. (3c) This is an artifact of the way
139810
- ** aggregates are processed - there is no mechanism to determine if
139811
- ** the LEFT JOIN table should be all-NULL.
139812
- **
139813140135
** See also tickets #306, #350, and #3300.
139814140136
*/
139815140137
if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
139816140138
if( pSubSrc->nSrc>1 /* (3a) */
139817
- || isAgg /* (3c) */
139818140139
|| IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
139819140140
|| (p->selFlags & SF_Distinct)!=0 /* (3d) */
139820140141
|| (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
139821140142
){
139822140143
return 0;
@@ -140733,10 +141054,11 @@
140733141054
if( p->pWhere
140734141055
|| p->pEList->nExpr!=1
140735141056
|| p->pSrc->nSrc!=1
140736141057
|| p->pSrc->a[0].pSelect
140737141058
|| pAggInfo->nFunc!=1
141059
+ || p->pHaving
140738141060
){
140739141061
return 0;
140740141062
}
140741141063
pTab = p->pSrc->a[0].pTab;
140742141064
assert( pTab!=0 );
@@ -142726,11 +143048,11 @@
142726143048
*/
142727143049
iEnd = sqlite3VdbeMakeLabel(pParse);
142728143050
if( (p->selFlags & SF_FixedLimit)==0 ){
142729143051
p->nSelectRow = 320; /* 4 billion rows */
142730143052
}
142731
- computeLimitRegisters(pParse, p, iEnd);
143053
+ if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
142732143054
if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
142733143055
sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
142734143056
sSort.sortFlags |= SORTFLAG_UseSorter;
142735143057
}
142736143058
@@ -142948,12 +143270,17 @@
142948143270
if( minMaxFlag ){
142949143271
sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
142950143272
sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
142951143273
}
142952143274
for(ii=0; ii<pAggInfo->nColumn; ii++){
142953
- sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
142954
- ii, pAggInfo->aCol[ii].iMem);
143275
+ struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
143276
+ sqlite3DebugPrintf(
143277
+ "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
143278
+ " iSorterColumn=%d\n",
143279
+ ii, pCol->pTab ? pCol->pTab->zName : "NULL",
143280
+ pCol->iTable, pCol->iColumn, pCol->iMem,
143281
+ pCol->iSorterColumn);
142955143282
sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
142956143283
}
142957143284
for(ii=0; ii<pAggInfo->nFunc; ii++){
142958143285
sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
142959143286
ii, pAggInfo->aFunc[ii].iMem);
@@ -143070,19 +143397,19 @@
143070143397
}
143071143398
}
143072143399
regBase = sqlite3GetTempRange(pParse, nCol);
143073143400
sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
143074143401
j = nGroupBy;
143402
+ pAggInfo->directMode = 1;
143075143403
for(i=0; i<pAggInfo->nColumn; i++){
143076143404
struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143077143405
if( pCol->iSorterColumn>=j ){
143078
- int r1 = j + regBase;
143079
- sqlite3ExprCodeGetColumnOfTable(v,
143080
- pCol->pTab, pCol->iTable, pCol->iColumn, r1);
143406
+ sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
143081143407
j++;
143082143408
}
143083143409
}
143410
+ pAggInfo->directMode = 0;
143084143411
regRecord = sqlite3GetTempReg(pParse);
143085143412
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
143086143413
sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
143087143414
sqlite3ReleaseTempReg(pParse, regRecord);
143088143415
sqlite3ReleaseTempRange(pParse, regBase, nCol);
@@ -147496,11 +147823,12 @@
147496147823
** in the list are moved to the sqlite3.pDisconnect list of the associated
147497147824
** database connection.
147498147825
*/
147499147826
SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
147500147827
assert( IsVirtual(p) );
147501
- if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147828
+ assert( db!=0 );
147829
+ if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147502147830
if( p->u.vtab.azArg ){
147503147831
int i;
147504147832
for(i=0; i<p->u.vtab.nArg; i++){
147505147833
if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147506147834
}
@@ -149173,10 +149501,11 @@
149173149501
#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
149174149502
#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
149175149503
#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
149176149504
#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
149177149505
#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
149506
+#define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
149178149507
149179149508
#endif /* !defined(SQLITE_WHEREINT_H) */
149180149509
149181149510
/************** End of whereInt.h ********************************************/
149182149511
/************** Continuing where we left off in wherecode.c ******************/
@@ -149781,11 +150110,12 @@
149781150110
eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap,&iTab);
149782150111
pExpr->iTable = iTab;
149783150112
}
149784150113
sqlite3ExprDelete(db, pX);
149785150114
}else{
149786
- aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
150115
+ int n = sqlite3ExprVectorSize(pX->pLeft);
150116
+ aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
149787150117
eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
149788150118
}
149789150119
pX = pExpr;
149790150120
}
149791150121
@@ -150051,11 +150381,11 @@
150051150381
WhereTerm *pTerm /* The upper or lower bound just coded */
150052150382
){
150053150383
if( pTerm->wtFlags & TERM_LIKEOPT ){
150054150384
VdbeOp *pOp;
150055150385
assert( pLevel->iLikeRepCntr>0 );
150056
- pOp = sqlite3VdbeGetOp(v, -1);
150386
+ pOp = sqlite3VdbeGetLastOp(v);
150057150387
assert( pOp!=0 );
150058150388
assert( pOp->opcode==OP_String8
150059150389
|| pTerm->pWC->pWInfo->pParse->db->mallocFailed );
150060150390
pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150061150391
pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
@@ -151267,12 +151597,12 @@
151267151597
sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
151268151598
endEq = 0;
151269151599
}
151270151600
nConstraint++;
151271151601
}
151272
- sqlite3DbFree(db, zStartAff);
151273
- sqlite3DbFree(db, zEndAff);
151602
+ if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff);
151603
+ if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff);
151274151604
151275151605
/* Top of the loop body */
151276151606
if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151277151607
151278151608
/* Check if the index cursor is past the end of the range. */
@@ -155499,11 +155829,11 @@
155499155829
/* At this point, the (iCol+1) field prefix of aSample[i] is the first
155500155830
** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155501155831
** is larger than all samples in the array. */
155502155832
tRowcnt iUpper, iGap;
155503155833
if( i>=pIdx->nSample ){
155504
- iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
155834
+ iUpper = pIdx->nRowEst0;
155505155835
}else{
155506155836
iUpper = aSample[i].anLt[iCol];
155507155837
}
155508155838
155509155839
if( iLower>=iUpper ){
@@ -156128,16 +156458,22 @@
156128156458
}
156129156459
}
156130156460
}
156131156461
156132156462
/*
156133
-** Deallocate internal memory used by a WhereLoop object
156463
+** Deallocate internal memory used by a WhereLoop object. Leave the
156464
+** object in an initialized state, as if it had been newly allocated.
156134156465
*/
156135156466
static void whereLoopClear(sqlite3 *db, WhereLoop *p){
156136
- if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156467
+ if( p->aLTerm!=p->aLTermSpace ){
156468
+ sqlite3DbFreeNN(db, p->aLTerm);
156469
+ p->aLTerm = p->aLTermSpace;
156470
+ p->nLSlot = ArraySize(p->aLTermSpace);
156471
+ }
156137156472
whereLoopClearUnion(db, p);
156138
- whereLoopInit(p);
156473
+ p->nLTerm = 0;
156474
+ p->wsFlags = 0;
156139156475
}
156140156476
156141156477
/*
156142156478
** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156143156479
*/
@@ -156157,11 +156493,13 @@
156157156493
/*
156158156494
** Transfer content from the second pLoop into the first.
156159156495
*/
156160156496
static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
156161156497
whereLoopClearUnion(db, pTo);
156162
- if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
156498
+ if( pFrom->nLTerm > pTo->nLSlot
156499
+ && whereLoopResize(db, pTo, pFrom->nLTerm)
156500
+ ){
156163156501
memset(pTo, 0, WHERE_LOOP_XFER_SZ);
156164156502
return SQLITE_NOMEM_BKPT;
156165156503
}
156166156504
memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
156167156505
memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
@@ -156175,32 +156513,34 @@
156175156513
156176156514
/*
156177156515
** Delete a WhereLoop object
156178156516
*/
156179156517
static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
156518
+ assert( db!=0 );
156180156519
whereLoopClear(db, p);
156181
- sqlite3DbFreeNN(db, p);
156520
+ sqlite3DbNNFreeNN(db, p);
156182156521
}
156183156522
156184156523
/*
156185156524
** Free a WhereInfo structure
156186156525
*/
156187156526
static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
156188156527
assert( pWInfo!=0 );
156528
+ assert( db!=0 );
156189156529
sqlite3WhereClauseClear(&pWInfo->sWC);
156190156530
while( pWInfo->pLoops ){
156191156531
WhereLoop *p = pWInfo->pLoops;
156192156532
pWInfo->pLoops = p->pNextLoop;
156193156533
whereLoopDelete(db, p);
156194156534
}
156195156535
assert( pWInfo->pExprMods==0 );
156196156536
while( pWInfo->pMemToFree ){
156197156537
WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156198
- sqlite3DbFreeNN(db, pWInfo->pMemToFree);
156538
+ sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
156199156539
pWInfo->pMemToFree = pNext;
156200156540
}
156201
- sqlite3DbFreeNN(db, pWInfo);
156541
+ sqlite3DbNNFreeNN(db, pWInfo);
156202156542
}
156203156543
156204156544
/* Undo all Expr node modifications
156205156545
*/
156206156546
static void whereUndoExprMods(WhereInfo *pWInfo){
@@ -156810,11 +157150,15 @@
156810157150
pNew->wsFlags = saved_wsFlags;
156811157151
pNew->u.btree.nEq = saved_nEq;
156812157152
pNew->u.btree.nBtm = saved_nBtm;
156813157153
pNew->u.btree.nTop = saved_nTop;
156814157154
pNew->nLTerm = saved_nLTerm;
156815
- if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
157155
+ if( pNew->nLTerm>=pNew->nLSlot
157156
+ && whereLoopResize(db, pNew, pNew->nLTerm+1)
157157
+ ){
157158
+ break; /* OOM while trying to enlarge the pNew->aLTerm array */
157159
+ }
156816157160
pNew->aLTerm[pNew->nLTerm++] = pTerm;
156817157161
pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
156818157162
156819157163
assert( nInMul==0
156820157164
|| (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
@@ -156903,42 +157247,43 @@
156903157247
}
156904157248
}
156905157249
if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
156906157250
}else if( eOp & WO_ISNULL ){
156907157251
pNew->wsFlags |= WHERE_COLUMN_NULL;
156908
- }else if( eOp & (WO_GT|WO_GE) ){
156909
- testcase( eOp & WO_GT );
156910
- testcase( eOp & WO_GE );
156911
- pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
156912
- pNew->u.btree.nBtm = whereRangeVectorLen(
156913
- pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156914
- );
156915
- pBtm = pTerm;
156916
- pTop = 0;
156917
- if( pTerm->wtFlags & TERM_LIKEOPT ){
156918
- /* Range constraints that come from the LIKE optimization are
156919
- ** always used in pairs. */
156920
- pTop = &pTerm[1];
156921
- assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
156922
- assert( pTop->wtFlags & TERM_LIKEOPT );
156923
- assert( pTop->eOperator==WO_LT );
156924
- if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156925
- pNew->aLTerm[pNew->nLTerm++] = pTop;
156926
- pNew->wsFlags |= WHERE_TOP_LIMIT;
156927
- pNew->u.btree.nTop = 1;
156928
- }
156929
- }else{
156930
- assert( eOp & (WO_LT|WO_LE) );
156931
- testcase( eOp & WO_LT );
156932
- testcase( eOp & WO_LE );
156933
- pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
156934
- pNew->u.btree.nTop = whereRangeVectorLen(
156935
- pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156936
- );
156937
- pTop = pTerm;
156938
- pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
156939
- pNew->aLTerm[pNew->nLTerm-2] : 0;
157252
+ }else{
157253
+ int nVecLen = whereRangeVectorLen(
157254
+ pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
157255
+ );
157256
+ if( eOp & (WO_GT|WO_GE) ){
157257
+ testcase( eOp & WO_GT );
157258
+ testcase( eOp & WO_GE );
157259
+ pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
157260
+ pNew->u.btree.nBtm = nVecLen;
157261
+ pBtm = pTerm;
157262
+ pTop = 0;
157263
+ if( pTerm->wtFlags & TERM_LIKEOPT ){
157264
+ /* Range constraints that come from the LIKE optimization are
157265
+ ** always used in pairs. */
157266
+ pTop = &pTerm[1];
157267
+ assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
157268
+ assert( pTop->wtFlags & TERM_LIKEOPT );
157269
+ assert( pTop->eOperator==WO_LT );
157270
+ if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
157271
+ pNew->aLTerm[pNew->nLTerm++] = pTop;
157272
+ pNew->wsFlags |= WHERE_TOP_LIMIT;
157273
+ pNew->u.btree.nTop = 1;
157274
+ }
157275
+ }else{
157276
+ assert( eOp & (WO_LT|WO_LE) );
157277
+ testcase( eOp & WO_LT );
157278
+ testcase( eOp & WO_LE );
157279
+ pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
157280
+ pNew->u.btree.nTop = nVecLen;
157281
+ pTop = pTerm;
157282
+ pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
157283
+ pNew->aLTerm[pNew->nLTerm-2] : 0;
157284
+ }
156940157285
}
156941157286
156942157287
/* At this point pNew->nOut is set to the number of rows expected to
156943157288
** be visited by the index scan before considering term pTerm, or the
156944157289
** values of nIn and nInMul. In other words, assuming that all
@@ -157380,10 +157725,13 @@
157380157725
#ifdef SQLITE_ENABLE_STAT4
157381157726
pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
157382157727
#else
157383157728
pNew->rRun = rSize + 16;
157384157729
#endif
157730
+ if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
157731
+ pNew->wsFlags |= WHERE_VIEWSCAN;
157732
+ }
157385157733
ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157386157734
whereLoopOutputAdjust(pWC, pNew, rSize);
157387157735
rc = whereLoopInsert(pBuilder, pNew);
157388157736
pNew->nOut = rSize;
157389157737
if( rc ) break;
@@ -158106,11 +158454,17 @@
158106158454
WhereLoop *pNew;
158107158455
158108158456
158109158457
/* Loop over the tables in the join, from left to right */
158110158458
pNew = pBuilder->pNew;
158111
- whereLoopInit(pNew);
158459
+
158460
+ /* Verify that pNew has already been initialized */
158461
+ assert( pNew->nLTerm==0 );
158462
+ assert( pNew->wsFlags==0 );
158463
+ assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
158464
+ assert( pNew->aLTerm!=0 );
158465
+
158112158466
pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
158113158467
for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158114158468
Bitmask mUnusable = 0;
158115158469
pNew->iTab = iTab;
158116158470
pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
@@ -158703,13 +159057,13 @@
158703159057
for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
158704159058
for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
158705159059
LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
158706159060
LogEst rCost; /* Cost of path (pFrom+pWLoop) */
158707159061
LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
158708
- i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
159062
+ i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */
158709159063
Bitmask maskNew; /* Mask of src visited by (..) */
158710
- Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
159064
+ Bitmask revMask; /* Mask of rev-order loops for (..) */
158711159065
158712159066
if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
158713159067
if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
158714159068
if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
158715159069
/* Do not use an automatic index if the this loop is expected
@@ -158724,11 +159078,13 @@
158724159078
** Compute its cost */
158725159079
rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
158726159080
rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
158727159081
nOut = pFrom->nRow + pWLoop->nOut;
158728159082
maskNew = pFrom->maskLoop | pWLoop->maskSelf;
159083
+ isOrdered = pFrom->isOrdered;
158729159084
if( isOrdered<0 ){
159085
+ revMask = 0;
158730159086
isOrdered = wherePathSatisfiesOrderBy(pWInfo,
158731159087
pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
158732159088
iLoop, pWLoop, &revMask);
158733159089
}else{
158734159090
revMask = pFrom->revLoop;
@@ -158751,10 +159107,17 @@
158751159107
rUnsorted, rCost));
158752159108
}else{
158753159109
rCost = rUnsorted;
158754159110
rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
158755159111
}
159112
+
159113
+ /* TUNING: A full-scan of a VIEW or subquery in the outer loop
159114
+ ** is not so bad. */
159115
+ if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){
159116
+ rCost += -10;
159117
+ nOut += -30;
159118
+ }
158756159119
158757159120
/* Check to see if pWLoop should be added to the set of
158758159121
** mxChoice best-so-far paths.
158759159122
**
158760159123
** First look for an existing path among best-so-far paths
@@ -158984,11 +159347,12 @@
158984159347
158985159348
158986159349
pWInfo->nRowOut = pFrom->nRow;
158987159350
158988159351
/* Free temporary memory and return success */
158989
- sqlite3DbFreeNN(db, pSpace);
159352
+ assert( db!=0 );
159353
+ sqlite3DbNNFreeNN(db, pSpace);
158990159354
return SQLITE_OK;
158991159355
}
158992159356
158993159357
/*
158994159358
** Most queries use only a single table (they are not joins) and have
@@ -161217,11 +161581,10 @@
161217161581
int i;
161218161582
int nInit = pList ? pList->nExpr : 0;
161219161583
for(i=0; i<pAppend->nExpr; i++){
161220161584
sqlite3 *db = pParse->db;
161221161585
Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
161222
- assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) );
161223161586
if( db->mallocFailed ){
161224161587
sqlite3ExprDelete(db, pDup);
161225161588
break;
161226161589
}
161227161590
if( bIntToNull ){
@@ -162488,14 +162851,13 @@
162488162851
}
162489162852
sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone);
162490162853
162491162854
/* This block runs if reg1 is not NULL, but reg2 is. */
162492162855
sqlite3VdbeJumpHere(v, addr);
162493
- sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); VdbeCoverage(v);
162494
- if( op==OP_Gt || op==OP_Ge ){
162495
- sqlite3VdbeChangeP2(v, -1, addrDone);
162496
- }
162856
+ sqlite3VdbeAddOp2(v, OP_IsNull, reg2,
162857
+ (op==OP_Gt || op==OP_Ge) ? addrDone : lbl);
162858
+ VdbeCoverage(v);
162497162859
}
162498162860
162499162861
/* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162500162862
** This block adds (or subtracts for DESC) the numeric value in regVal
162501162863
** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
@@ -170068,11 +170430,11 @@
170068170430
sqlite3DeleteTable(db, pParse->pNewTable);
170069170431
}
170070170432
if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
170071170433
sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170072170434
}
170073
- if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList);
170435
+ if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
170074170436
db->pParse = pParentParse;
170075170437
assert( nErr==0 || pParse->rc!=SQLITE_OK );
170076170438
return nErr;
170077170439
}
170078170440
@@ -171424,22 +171786,23 @@
171424171786
db->lookaside.pEnd = p;
171425171787
db->lookaside.bDisable = 0;
171426171788
db->lookaside.bMalloced = pBuf==0 ?1:0;
171427171789
db->lookaside.nSlot = nBig+nSm;
171428171790
}else{
171429
- db->lookaside.pStart = db;
171791
+ db->lookaside.pStart = 0;
171430171792
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171431171793
db->lookaside.pSmallInit = 0;
171432171794
db->lookaside.pSmallFree = 0;
171433
- db->lookaside.pMiddle = db;
171795
+ db->lookaside.pMiddle = 0;
171434171796
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171435
- db->lookaside.pEnd = db;
171797
+ db->lookaside.pEnd = 0;
171436171798
db->lookaside.bDisable = 1;
171437171799
db->lookaside.sz = 0;
171438171800
db->lookaside.bMalloced = 0;
171439171801
db->lookaside.nSlot = 0;
171440171802
}
171803
+ db->lookaside.pTrueEnd = db->lookaside.pEnd;
171441171804
assert( sqlite3LookasideUsed(db,0)==0 );
171442171805
#endif /* SQLITE_OMIT_LOOKASIDE */
171443171806
return SQLITE_OK;
171444171807
}
171445171808
@@ -171514,10 +171877,11 @@
171514171877
** Configuration settings for an individual database connection
171515171878
*/
171516171879
SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
171517171880
va_list ap;
171518171881
int rc;
171882
+ sqlite3_mutex_enter(db->mutex);
171519171883
va_start(ap, op);
171520171884
switch( op ){
171521171885
case SQLITE_DBCONFIG_MAINDBNAME: {
171522171886
/* IMP: R-06824-28531 */
171523171887
/* IMP: R-36257-52125 */
@@ -171579,10 +171943,11 @@
171579171943
}
171580171944
break;
171581171945
}
171582171946
}
171583171947
va_end(ap);
171948
+ sqlite3_mutex_leave(db->mutex);
171584171949
return rc;
171585171950
}
171586171951
171587171952
/*
171588171953
** This is the default collating function named "BINARY" which is always
@@ -181118,11 +181483,11 @@
181118181483
p1 = pPhrase->doclist.pList;
181119181484
p2 = aPoslist;
181120181485
nDistance = iPrev - nMaxUndeferred;
181121181486
}
181122181487
181123
- aOut = (char *)sqlite3_malloc(nPoslist+8);
181488
+ aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING);
181124181489
if( !aOut ){
181125181490
sqlite3_free(aPoslist);
181126181491
return SQLITE_NOMEM;
181127181492
}
181128181493
@@ -204144,11 +204509,11 @@
204144204509
sqlite3_bind_value(pUp, 2, aData[2]);
204145204510
}
204146204511
sqlite3_free(p);
204147204512
nChange = 1;
204148204513
}
204149
- for(jj=1; jj<pRtree->nAux; jj++){
204514
+ for(jj=1; jj<nData-2; jj++){
204150204515
nChange++;
204151204516
sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204152204517
}
204153204518
if( nChange ){
204154204519
sqlite3_step(pUp);
@@ -212503,15 +212868,16 @@
212503212868
*/
212504212869
static int dbpageBegin(sqlite3_vtab *pVtab){
212505212870
DbpageTable *pTab = (DbpageTable *)pVtab;
212506212871
sqlite3 *db = pTab->db;
212507212872
int i;
212508
- for(i=0; i<db->nDb; i++){
212873
+ int rc = SQLITE_OK;
212874
+ for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
212509212875
Btree *pBt = db->aDb[i].pBt;
212510
- if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
212876
+ if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0);
212511212877
}
212512
- return SQLITE_OK;
212878
+ return rc;
212513212879
}
212514212880
212515212881
212516212882
/*
212517212883
** Invoke this routine to register the "dbpage" virtual table module
@@ -219231,11 +219597,11 @@
219231219597
static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
219232219598
219233219599
static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
219234219600
219235219601
#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
219236
-#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
219602
+#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c)
219237219603
#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
219238219604
#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
219239219605
#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
219240219606
219241219607
#define fts5BufferGrow(pRc,pBuf,nn) ( \
@@ -231108,11 +231474,13 @@
231108231474
/* Write the rowid. */
231109231475
if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
231110231476
fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
231111231477
}else{
231112231478
assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
231113
- fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
231479
+ fts5BufferAppendVarint(&p->rc, &pPage->buf,
231480
+ (u64)iRowid - (u64)pWriter->iPrevRowid
231481
+ );
231114231482
}
231115231483
pWriter->iPrevRowid = iRowid;
231116231484
pWriter->bFirstRowidInDoclist = 0;
231117231485
pWriter->bFirstRowidInPage = 0;
231118231486
}
@@ -231872,21 +232240,21 @@
231872232240
return fts5IndexReturn(p);
231873232241
}
231874232242
231875232243
static void fts5AppendRowid(
231876232244
Fts5Index *p,
231877
- i64 iDelta,
232245
+ u64 iDelta,
231878232246
Fts5Iter *pUnused,
231879232247
Fts5Buffer *pBuf
231880232248
){
231881232249
UNUSED_PARAM(pUnused);
231882232250
fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
231883232251
}
231884232252
231885232253
static void fts5AppendPoslist(
231886232254
Fts5Index *p,
231887
- i64 iDelta,
232255
+ u64 iDelta,
231888232256
Fts5Iter *pMulti,
231889232257
Fts5Buffer *pBuf
231890232258
){
231891232259
int nData = pMulti->base.nData;
231892232260
int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
@@ -231957,14 +232325,14 @@
231957232325
fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
231958232326
*piLastRowid = iRowid;
231959232327
}
231960232328
#endif
231961232329
231962
-#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
231963
- assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
231964
- fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
231965
- (iLastRowid) = (iRowid); \
232330
+#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
232331
+ assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
232332
+ fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \
232333
+ (iLastRowid) = (iRowid); \
231966232334
}
231967232335
231968232336
/*
231969232337
** Swap the contents of buffer *p1 with that of *p2.
231970232338
*/
@@ -232231,11 +232599,11 @@
232231232599
Fts5Buffer *aBuf;
232232232600
int nBuf = 32;
232233232601
int nMerge = 1;
232234232602
232235232603
void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
232236
- void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
232604
+ void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
232237232605
if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
232238232606
xMerge = fts5MergeRowidLists;
232239232607
xAppend = fts5AppendRowid;
232240232608
}else{
232241232609
nMerge = FTS5_MERGE_NLIST-1;
@@ -232270,11 +232638,11 @@
232270232638
fts5MultiIterNext2(p, p1, &dummy)
232271232639
){
232272232640
Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
232273232641
p1->xSetOutputs(p1, pSeg);
232274232642
if( p1->base.nData ){
232275
- xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232643
+ xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
232276232644
iLastRowid = p1->base.iRowid;
232277232645
}
232278232646
}
232279232647
fts5MultiIterFree(p1);
232280232648
}
@@ -232318,11 +232686,11 @@
232318232686
}
232319232687
}
232320232688
iLastRowid = 0;
232321232689
}
232322232690
232323
- xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232691
+ xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
232324232692
iLastRowid = p1->base.iRowid;
232325232693
}
232326232694
232327232695
assert( (nBuf%nMerge)==0 );
232328232696
for(i=0; i<nBuf; i+=nMerge){
@@ -236634,11 +237002,11 @@
236634237002
int nArg, /* Number of args */
236635237003
sqlite3_value **apUnused /* Function arguments */
236636237004
){
236637237005
assert( nArg==0 );
236638237006
UNUSED_PARAM2(nArg, apUnused);
236639
- sqlite3_result_text(pCtx, "fts5: 2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21", -1, SQLITE_TRANSIENT);
237007
+ sqlite3_result_text(pCtx, "fts5: 2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c", -1, SQLITE_TRANSIENT);
236640237008
}
236641237009
236642237010
/*
236643237011
** Return true if zName is the extension on one of the shadow tables used
236644237012
** by this module.
236645237013
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.39.2. 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.
@@ -450,13 +450,13 @@
450 **
451 ** See also: [sqlite3_libversion()],
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.39.2"
456 #define SQLITE_VERSION_NUMBER 3039002
457 #define SQLITE_SOURCE_ID "2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -3728,10 +3728,13 @@
3728 **
3729 ** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3730 ** <dd>The database is opened [shared cache] enabled, overriding
3731 ** the default shared cache setting provided by
3732 ** [sqlite3_enable_shared_cache()].)^
 
 
 
3733 **
3734 ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3735 ** <dd>The database is opened [shared cache] disabled, overriding
3736 ** the default shared cache setting provided by
3737 ** [sqlite3_enable_shared_cache()].)^
@@ -3743,11 +3746,11 @@
3743 ** connection as soon as the connection is created. In addition to setting
3744 ** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3745 ** to return an extended result code.</dd>
3746 **
3747 ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3748 ** <dd>The database filename is not allowed to be a symbolic link</dd>
3749 ** </dl>)^
3750 **
3751 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
3752 ** required combinations shown above optionally combined with other
3753 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6769,11 +6772,11 @@
6769 **
6770 ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6771 ** function C that is invoked prior to each autovacuum of the database
6772 ** file. ^The callback is passed a copy of the generic data pointer (P),
6773 ** the schema-name of the attached database that is being autovacuumed,
6774 ** the the size of the database file in pages, the number of free pages,
6775 ** and the number of bytes per page, respectively. The callback should
6776 ** return the number of free pages that should be removed by the
6777 ** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6778 ** ^If the value returned is greater than or equal to the number of
6779 ** free pages, then a complete autovacuum happens.
@@ -6889,10 +6892,15 @@
6889 **
6890 ** ^(This routine enables or disables the sharing of the database cache
6891 ** and schema data structures between [database connection | connections]
6892 ** to the same database. Sharing is enabled if the argument is true
6893 ** and disabled if the argument is false.)^
 
 
 
 
 
6894 **
6895 ** ^Cache sharing is enabled and disabled for an entire process.
6896 ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6897 ** In prior versions of SQLite,
6898 ** sharing was enabled or disabled for each thread separately.
@@ -6988,11 +6996,11 @@
6988 ** ^Setting the heap limits to zero disables the heap limiter mechanism.
6989 **
6990 ** ^The soft heap limit may not be greater than the hard heap limit.
6991 ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6992 ** is invoked with a value of N that is greater than the hard heap limit,
6993 ** the the soft heap limit is set to the value of the hard heap limit.
6994 ** ^The soft heap limit is automatically enabled whenever the hard heap
6995 ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6996 ** the soft heap limit is outside the range of 1..N, then the soft heap
6997 ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
6998 ** hard heap limit is enabled makes the soft heap limit equal to the
@@ -9283,11 +9291,11 @@
9283 ** sqlite3_backup_init() is called and before the corresponding call to
9284 ** sqlite3_backup_finish(). SQLite does not currently check to see
9285 ** if the application incorrectly accesses the destination [database connection]
9286 ** and so no error code is reported, but the operations may malfunction
9287 ** nevertheless. Use of the destination database connection while a
9288 ** backup is in progress might also also cause a mutex deadlock.
9289 **
9290 ** If running in [shared cache mode], the application must
9291 ** guarantee that the shared cache used by the destination database
9292 ** is not accessed while the backup is running. In practice this means
9293 ** that the application must guarantee that the disk file being
@@ -9711,11 +9719,11 @@
9711 ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9712 ** meaning of each of these checkpoint modes.
9713 */
9714 #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9715 #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9716 #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9717 #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9718
9719 /*
9720 ** CAPI3REF: Virtual Table Interface Configuration
9721 **
@@ -13142,10 +13150,15 @@
13142 /******** End of fts5.h *********/
13143
13144 /************** End of sqlite3.h *********************************************/
13145 /************** Continuing where we left off in sqliteInt.h ******************/
13146
 
 
 
 
 
13147 /*
13148 ** Include the configuration header output by 'configure' if we're using the
13149 ** autoconf-based build
13150 */
13151 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
@@ -15553,67 +15566,67 @@
15553 #define OP_Checkpoint 3
15554 #define OP_JournalMode 4
15555 #define OP_Vacuum 5
15556 #define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
15557 #define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */
15558 #define OP_Goto 8 /* jump */
15559 #define OP_Gosub 9 /* jump */
15560 #define OP_InitCoroutine 10 /* jump */
15561 #define OP_Yield 11 /* jump */
15562 #define OP_MustBeInt 12 /* jump */
15563 #define OP_Jump 13 /* jump */
15564 #define OP_Once 14 /* jump */
15565 #define OP_If 15 /* jump */
15566 #define OP_IfNot 16 /* jump */
15567 #define OP_IsNullOrType 17 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15568 #define OP_IfNullRow 18 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15569 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15570 #define OP_SeekLT 20 /* jump, synopsis: key=r[P3@P4] */
15571 #define OP_SeekLE 21 /* jump, synopsis: key=r[P3@P4] */
15572 #define OP_SeekGE 22 /* jump, synopsis: key=r[P3@P4] */
15573 #define OP_SeekGT 23 /* jump, synopsis: key=r[P3@P4] */
15574 #define OP_IfNotOpen 24 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15575 #define OP_IfNoHope 25 /* jump, synopsis: key=r[P3@P4] */
15576 #define OP_NoConflict 26 /* jump, synopsis: key=r[P3@P4] */
15577 #define OP_NotFound 27 /* jump, synopsis: key=r[P3@P4] */
15578 #define OP_Found 28 /* jump, synopsis: key=r[P3@P4] */
15579 #define OP_SeekRowid 29 /* jump, synopsis: intkey=r[P3] */
15580 #define OP_NotExists 30 /* jump, synopsis: intkey=r[P3] */
15581 #define OP_Last 31 /* jump */
15582 #define OP_IfSmaller 32 /* jump */
15583 #define OP_SorterSort 33 /* jump */
15584 #define OP_Sort 34 /* jump */
15585 #define OP_Rewind 35 /* jump */
15586 #define OP_SorterNext 36 /* jump */
15587 #define OP_Prev 37 /* jump */
15588 #define OP_Next 38 /* jump */
15589 #define OP_IdxLE 39 /* jump, synopsis: key=r[P3@P4] */
15590 #define OP_IdxGT 40 /* jump, synopsis: key=r[P3@P4] */
15591 #define OP_IdxLT 41 /* jump, synopsis: key=r[P3@P4] */
15592 #define OP_IdxGE 42 /* jump, synopsis: key=r[P3@P4] */
15593 #define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15594 #define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15595 #define OP_RowSetRead 45 /* jump, synopsis: r[P3]=rowset(P1) */
15596 #define OP_RowSetTest 46 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15597 #define OP_Program 47 /* jump */
15598 #define OP_FkIfZero 48 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15599 #define OP_IfPos 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15600 #define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15601 #define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15602 #define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15603 #define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15604 #define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15605 #define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15606 #define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15607 #define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15608 #define OP_ElseEq 58 /* jump, same as TK_ESCAPE */
15609 #define OP_IfNotZero 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15610 #define OP_DecrJumpZero 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15611 #define OP_IncrVacuum 61 /* jump */
15612 #define OP_VNext 62 /* jump */
15613 #define OP_Filter 63 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
15614 #define OP_Init 64 /* jump, synopsis: Start at P2 */
15615 #define OP_PureFunc 65 /* synopsis: r[P3]=func(r[P2@NP]) */
15616 #define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */
15617 #define OP_Return 67
15618 #define OP_EndCoroutine 68
15619 #define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */
@@ -15745,17 +15758,17 @@
15745 #define OPFLG_IN3 0x08 /* in3: P3 is an input */
15746 #define OPFLG_OUT2 0x10 /* out2: P2 is an output */
15747 #define OPFLG_OUT3 0x20 /* out3: P3 is an output */
15748 #define OPFLG_INITIALIZER {\
15749 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
15750 /* 8 */ 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03,\
15751 /* 16 */ 0x03, 0x03, 0x01, 0x12, 0x09, 0x09, 0x09, 0x09,\
15752 /* 24 */ 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,\
15753 /* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15754 /* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\
15755 /* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15756 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\
15757 /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
15758 /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
15759 /* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
15760 /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\
15761 /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\
@@ -15857,10 +15870,11 @@
15857 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15858 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15859 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15860 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15861 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
 
15862 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
15863 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15864 SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15865 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15866 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
@@ -16741,10 +16755,11 @@
16741 void *pMiddle; /* First byte past end of full-size buffers and
16742 ** the first byte of LOOKASIDE_SMALL buffers */
16743 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16744 void *pStart; /* First byte of available memory space */
16745 void *pEnd; /* First byte past end of available space */
 
16746 };
16747 struct LookasideSlot {
16748 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16749 };
16750
@@ -18189,11 +18204,11 @@
18189 #define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
18190 #define EP_Skip 0x002000 /* Operator does not contribute to affinity */
18191 #define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18192 #define EP_Win 0x008000 /* Contains window functions */
18193 #define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18194 #define EP_MemToken 0x020000 /* Need to sqlite3DbFree() Expr.zToken */
18195 #define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
18196 #define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
18197 #define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18198 #define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */
18199 #define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */
@@ -19667,10 +19682,11 @@
19667 SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
19668 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19669 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19670 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
19671 SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
 
19672 SQLITE_PRIVATE int sqlite3MallocSize(const void*);
19673 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
19674 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
19675 SQLITE_PRIVATE void sqlite3PageFree(void*);
19676 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
@@ -20191,10 +20207,11 @@
20191 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
20192 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
20193 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
20194 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20195 SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
 
20196 SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*);
20197 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
20198 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
20199 SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
20200 SQLITE_PRIVATE int sqlite3Atoi(const char*);
@@ -21637,13 +21654,10 @@
21637 "OMIT_WSD",
21638 #endif
21639 #ifdef SQLITE_OMIT_XFER_OPT
21640 "OMIT_XFER_OPT",
21641 #endif
21642 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
21643 "PCACHE_SEPARATE_HEADER",
21644 #endif
21645 #ifdef SQLITE_PERFORMANCE_TRACE
21646 "PERFORMANCE_TRACE",
21647 #endif
21648 #ifdef SQLITE_POWERSAFE_OVERWRITE
21649 # if SQLITE_POWERSAFE_OVERWRITE != 1
@@ -22592,11 +22606,11 @@
22592 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
22593 ** is really a pointer to an instance of this structure.
22594 */
22595 struct Vdbe {
22596 sqlite3 *db; /* The database connection that owns this statement */
22597 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
22598 Parse *pParse; /* Parsing context used to create this Vdbe */
22599 ynVar nVar; /* Number of entries in aVar[] */
22600 int nMem; /* Number of memory locations currently allocated */
22601 int nCursor; /* Number of slots in apCsr[] */
22602 u32 cacheCtr; /* VdbeCursor row cache generation counter */
@@ -23150,10 +23164,12 @@
23150 int i; /* Used to iterate through schemas */
23151 int nByte = 0; /* Used to accumulate return value */
23152
23153 sqlite3BtreeEnterAll(db);
23154 db->pnBytesFreed = &nByte;
 
 
23155 for(i=0; i<db->nDb; i++){
23156 Schema *pSchema = db->aDb[i].pSchema;
23157 if( ALWAYS(pSchema!=0) ){
23158 HashElem *p;
23159
@@ -23175,10 +23191,11 @@
23175 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
23176 }
23177 }
23178 }
23179 db->pnBytesFreed = 0;
 
23180 sqlite3BtreeLeaveAll(db);
23181
23182 *pHighwater = 0;
23183 *pCurrent = nByte;
23184 break;
@@ -23192,13 +23209,16 @@
23192 case SQLITE_DBSTATUS_STMT_USED: {
23193 struct Vdbe *pVdbe; /* Used to iterate through VMs */
23194 int nByte = 0; /* Used to accumulate return value */
23195
23196 db->pnBytesFreed = &nByte;
23197 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
 
 
23198 sqlite3VdbeDelete(pVdbe);
23199 }
 
23200 db->pnBytesFreed = 0;
23201
23202 *pHighwater = 0; /* IMP: R-64479-57858 */
23203 *pCurrent = nByte;
23204
@@ -23530,11 +23550,11 @@
23530 X1 = 36525*(Y+4716)/100;
23531 X2 = 306001*(M+1)/10000;
23532 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23533 p->validJD = 1;
23534 if( p->validHMS ){
23535 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
23536 if( p->validTZ ){
23537 p->iJD -= p->tz*60000;
23538 p->validYMD = 0;
23539 p->validHMS = 0;
23540 p->validTZ = 0;
@@ -24039,11 +24059,11 @@
24039 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
24040 ** date is already on the appropriate weekday, this is a no-op.
24041 */
24042 if( sqlite3_strnicmp(z, "weekday ", 8)==0
24043 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
24044 && (n=(int)r)==r && n>=0 && r<7 ){
24045 sqlite3_int64 Z;
24046 computeYMD_HMS(p);
24047 p->validTZ = 0;
24048 p->validJD = 0;
24049 computeJD(p);
@@ -24837,10 +24857,11 @@
24837 DO_OS_MALLOC_TEST(0);
24838 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
24839 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
24840 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
24841 ** reaching the VFS. */
 
24842 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
24843 assert( rc==SQLITE_OK || pFile->pMethods==0 );
24844 return rc;
24845 }
24846 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
@@ -29102,11 +29123,11 @@
29102 /*
29103 ** TRUE if p is a lookaside memory allocation from db
29104 */
29105 #ifndef SQLITE_OMIT_LOOKASIDE
29106 static int isLookaside(sqlite3 *db, const void *p){
29107 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
29108 }
29109 #else
29110 #define isLookaside(A,B) 0
29111 #endif
29112
@@ -29126,22 +29147,20 @@
29126 #endif
29127 }
29128 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){
29129 assert( p!=0 );
29130 #ifdef SQLITE_DEBUG
29131 if( db==0 || !isLookaside(db,p) ){
29132 if( db==0 ){
29133 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29134 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29135 }else{
29136 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29137 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29138 }
29139 }
29140 #endif
29141 if( db ){
29142 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29143 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29144 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29145 assert( sqlite3_mutex_held(db->mutex) );
29146 return LOOKASIDE_SMALL;
29147 }
@@ -29193,18 +29212,15 @@
29193 */
29194 SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
29195 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29196 assert( p!=0 );
29197 if( db ){
29198 if( db->pnBytesFreed ){
29199 measureAllocationSize(db, p);
29200 return;
29201 }
29202 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29203 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29204 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29205 LookasideSlot *pBuf = (LookasideSlot*)p;
 
29206 #ifdef SQLITE_DEBUG
29207 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
29208 #endif
29209 pBuf->pNext = db->lookaside.pSmallFree;
29210 db->lookaside.pSmallFree = pBuf;
@@ -29211,24 +29227,66 @@
29211 return;
29212 }
29213 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29214 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29215 LookasideSlot *pBuf = (LookasideSlot*)p;
 
29216 #ifdef SQLITE_DEBUG
29217 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29218 #endif
29219 pBuf->pNext = db->lookaside.pFree;
29220 db->lookaside.pFree = pBuf;
29221 return;
29222 }
29223 }
 
 
 
 
29224 }
29225 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29226 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29227 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
29228 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29229 sqlite3_free(p);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29230 }
29231 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
29232 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29233 if( p ) sqlite3DbFreeNN(db, p);
29234 }
@@ -29561,12 +29619,17 @@
29561 if( db->nVdbeExec>0 ){
29562 AtomicStore(&db->u1.isInterrupted, 1);
29563 }
29564 DisableLookaside;
29565 if( db->pParse ){
 
29566 sqlite3ErrorMsg(db->pParse, "out of memory");
29567 db->pParse->rc = SQLITE_NOMEM_BKPT;
 
 
 
 
29568 }
29569 }
29570 return 0;
29571 }
29572
@@ -32332,20 +32395,45 @@
32332
32333 /* All threads share a single random number generator.
32334 ** This structure is the current state of the generator.
32335 */
32336 static SQLITE_WSD struct sqlite3PrngType {
32337 unsigned char isInit; /* True if initialized */
32338 unsigned char i, j; /* State variables */
32339 unsigned char s[256]; /* State variables */
32340 } sqlite3Prng;
32341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32342 /*
32343 ** Return N random bytes.
32344 */
32345 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
32346 unsigned char t;
32347 unsigned char *zBuf = pBuf;
32348
32349 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32350 ** state vector. If writable static data is unsupported on the target,
32351 ** we have to locate the state vector at run-time. In the more common
@@ -32371,57 +32459,50 @@
32371 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
32372 #endif
32373
32374 sqlite3_mutex_enter(mutex);
32375 if( N<=0 || pBuf==0 ){
32376 wsdPrng.isInit = 0;
32377 sqlite3_mutex_leave(mutex);
32378 return;
32379 }
32380
32381 /* Initialize the state of the random number generator once,
32382 ** the first time this routine is called. The seed value does
32383 ** not need to contain a lot of randomness since we are not
32384 ** trying to do secure encryption or anything like that...
32385 **
32386 ** Nothing in this file or anywhere else in SQLite does any kind of
32387 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
32388 ** number generator) not as an encryption device.
32389 */
32390 if( !wsdPrng.isInit ){
32391 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
32392 int i;
32393 char k[256];
32394 wsdPrng.j = 0;
32395 wsdPrng.i = 0;
32396 if( NEVER(pVfs==0) ){
32397 memset(k, 0, sizeof(k));
32398 }else{
32399 sqlite3OsRandomness(pVfs, 256, k);
32400 }
32401 for(i=0; i<256; i++){
32402 wsdPrng.s[i] = (u8)i;
32403 }
32404 for(i=0; i<256; i++){
32405 wsdPrng.j += wsdPrng.s[i] + k[i];
32406 t = wsdPrng.s[wsdPrng.j];
32407 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
32408 wsdPrng.s[i] = t;
32409 }
32410 wsdPrng.isInit = 1;
32411 }
32412
32413 assert( N>0 );
32414 do{
32415 wsdPrng.i++;
32416 t = wsdPrng.s[wsdPrng.i];
32417 wsdPrng.j += t;
32418 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
32419 wsdPrng.s[wsdPrng.j] = t;
32420 t += wsdPrng.s[wsdPrng.i];
32421 *(zBuf++) = wsdPrng.s[t];
32422 }while( --N );
 
 
 
 
 
 
32423 sqlite3_mutex_leave(mutex);
32424 }
32425
32426 #ifndef SQLITE_UNTESTABLE
32427 /*
@@ -33457,11 +33538,11 @@
33457 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
33458 char *zMsg;
33459 va_list ap;
33460 sqlite3 *db = pParse->db;
33461 assert( db!=0 );
33462 assert( db->pParse==pParse );
33463 db->errByteOffset = -2;
33464 va_start(ap, zFormat);
33465 zMsg = sqlite3VMPrintf(db, zFormat, ap);
33466 va_end(ap);
33467 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
@@ -35275,67 +35356,67 @@
35275 /* 3 */ "Checkpoint" OpHelp(""),
35276 /* 4 */ "JournalMode" OpHelp(""),
35277 /* 5 */ "Vacuum" OpHelp(""),
35278 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
35279 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
35280 /* 8 */ "Goto" OpHelp(""),
35281 /* 9 */ "Gosub" OpHelp(""),
35282 /* 10 */ "InitCoroutine" OpHelp(""),
35283 /* 11 */ "Yield" OpHelp(""),
35284 /* 12 */ "MustBeInt" OpHelp(""),
35285 /* 13 */ "Jump" OpHelp(""),
35286 /* 14 */ "Once" OpHelp(""),
35287 /* 15 */ "If" OpHelp(""),
35288 /* 16 */ "IfNot" OpHelp(""),
35289 /* 17 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35290 /* 18 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35291 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35292 /* 20 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35293 /* 21 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35294 /* 22 */ "SeekGE" OpHelp("key=r[P3@P4]"),
35295 /* 23 */ "SeekGT" OpHelp("key=r[P3@P4]"),
35296 /* 24 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
35297 /* 25 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
35298 /* 26 */ "NoConflict" OpHelp("key=r[P3@P4]"),
35299 /* 27 */ "NotFound" OpHelp("key=r[P3@P4]"),
35300 /* 28 */ "Found" OpHelp("key=r[P3@P4]"),
35301 /* 29 */ "SeekRowid" OpHelp("intkey=r[P3]"),
35302 /* 30 */ "NotExists" OpHelp("intkey=r[P3]"),
35303 /* 31 */ "Last" OpHelp(""),
35304 /* 32 */ "IfSmaller" OpHelp(""),
35305 /* 33 */ "SorterSort" OpHelp(""),
35306 /* 34 */ "Sort" OpHelp(""),
35307 /* 35 */ "Rewind" OpHelp(""),
35308 /* 36 */ "SorterNext" OpHelp(""),
35309 /* 37 */ "Prev" OpHelp(""),
35310 /* 38 */ "Next" OpHelp(""),
35311 /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"),
35312 /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"),
35313 /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"),
35314 /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"),
35315 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
35316 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
35317 /* 45 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
35318 /* 46 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
35319 /* 47 */ "Program" OpHelp(""),
35320 /* 48 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
35321 /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35322 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
35323 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
35324 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
35325 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
35326 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
35327 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
35328 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
35329 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
35330 /* 58 */ "ElseEq" OpHelp(""),
35331 /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35332 /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35333 /* 61 */ "IncrVacuum" OpHelp(""),
35334 /* 62 */ "VNext" OpHelp(""),
35335 /* 63 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
35336 /* 64 */ "Init" OpHelp("Start at P2"),
35337 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
35338 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
35339 /* 67 */ "Return" OpHelp(""),
35340 /* 68 */ "EndCoroutine" OpHelp(""),
35341 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
@@ -41318,30 +41399,39 @@
41318 ** pVfs->mxPathname bytes.
41319 */
41320 static int unixGetTempname(int nBuf, char *zBuf){
41321 const char *zDir;
41322 int iLimit = 0;
 
41323
41324 /* It's odd to simulate an io-error here, but really this is just
41325 ** using the io-error infrastructure to test that SQLite handles this
41326 ** function failing.
41327 */
41328 zBuf[0] = 0;
41329 SimulateIOError( return SQLITE_IOERR );
41330
 
41331 zDir = unixTempFileDir();
41332 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
41333 do{
41334 u64 r;
41335 sqlite3_randomness(sizeof(r), &r);
41336 assert( nBuf>2 );
41337 zBuf[nBuf-2] = 0;
41338 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41339 zDir, r, 0);
41340 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
41341 }while( osAccess(zBuf,0)==0 );
41342 return SQLITE_OK;
 
 
 
 
 
 
 
41343 }
41344
41345 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41346 /*
41347 ** Routine to transform a unixFile into a proxy-locking unixFile.
@@ -43512,11 +43602,16 @@
43512 ** correctly. See ticket [bb3a86e890c8e96ab] */
43513 assert( ArraySize(aSyscall)==29 );
43514
43515 /* Register all VFSes defined in the aVfs[] array */
43516 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 
 
 
 
43517 sqlite3_vfs_register(&aVfs[i], i==0);
 
43518 }
43519 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
43520
43521 #ifndef SQLITE_OMIT_WAL
43522 /* Validate lock assumptions */
@@ -45480,10 +45575,11 @@
45480 char **ppDirectory = 0;
45481 #ifndef SQLITE_OMIT_AUTOINIT
45482 int rc = sqlite3_initialize();
45483 if( rc ) return rc;
45484 #endif
 
45485 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
45486 ppDirectory = &sqlite3_data_directory;
45487 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
45488 ppDirectory = &sqlite3_temp_directory;
45489 }
@@ -45494,18 +45590,23 @@
45494 if( ppDirectory ){
45495 char *zCopy = 0;
45496 if( zValue && zValue[0] ){
45497 zCopy = sqlite3_mprintf("%s", zValue);
45498 if ( zCopy==0 ){
45499 return SQLITE_NOMEM_BKPT;
 
45500 }
45501 }
45502 sqlite3_free(*ppDirectory);
45503 *ppDirectory = zCopy;
45504 return SQLITE_OK;
 
 
45505 }
45506 return SQLITE_ERROR;
 
 
45507 }
45508
45509 /*
45510 ** This function is the same as sqlite3_win32_set_directory (below); however,
45511 ** it accepts a UTF-16 string.
@@ -48274,10 +48375,22 @@
48274 }
48275 }
48276 }
48277 return 0;
48278 }
 
 
 
 
 
 
 
 
 
 
 
 
48279
48280 /*
48281 ** Create a temporary file name and store the resulting pointer into pzBuf.
48282 ** The pointer returned in pzBuf must be freed via sqlite3_free().
48283 */
@@ -48311,24 +48424,27 @@
48311 ** has been explicitly set by the application; otherwise, use the one
48312 ** configured by the operating system.
48313 */
48314 nDir = nMax - (nPre + 15);
48315 assert( nDir>0 );
48316 if( sqlite3_temp_directory ){
48317 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
48318 if( nDirLen>0 ){
48319 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48320 nDirLen++;
48321 }
48322 if( nDirLen>nDir ){
 
48323 sqlite3_free(zBuf);
48324 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48325 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
48326 }
48327 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
48328 }
 
48329 }
 
48330 #if defined(__CYGWIN__)
48331 else{
48332 static const char *azDirs[] = {
48333 0, /* getenv("SQLITE_TMPDIR") */
48334 0, /* getenv("TMPDIR") */
@@ -49113,11 +49229,11 @@
49113 /*
49114 ** Turn a relative pathname into a full pathname. Write the full
49115 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49116 ** bytes in size.
49117 */
49118 static int winFullPathname(
49119 sqlite3_vfs *pVfs, /* Pointer to vfs object */
49120 const char *zRelative, /* Possibly relative input path */
49121 int nFull, /* Size of output buffer in bytes */
49122 char *zFull /* Output buffer */
49123 ){
@@ -49291,10 +49407,23 @@
49291 return SQLITE_OK;
49292 }else{
49293 return SQLITE_IOERR_NOMEM_BKPT;
49294 }
49295 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
49296 }
49297
49298 #ifndef SQLITE_OMIT_LOAD_EXTENSION
49299 /*
49300 ** Interfaces for opening a shared library, finding entry points
@@ -51080,39 +51209,58 @@
51080 */
51081 #if defined(SQLITE_DEBUG) && 0
51082 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
51083 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
51084 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
51085 void pcacheDump(PCache *pCache){
51086 int N;
51087 int i, j;
51088 sqlite3_pcache_page *pLower;
51089 PgHdr *pPg;
51090 unsigned char *a;
 
 
 
 
 
 
 
 
 
 
 
51091
51092 if( sqlite3PcacheTrace<2 ) return;
51093 if( pCache->pCache==0 ) return;
51094 N = sqlite3PcachePagecount(pCache);
51095 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
51096 for(i=1; i<=N; i++){
51097 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
51098 if( pLower==0 ) continue;
51099 pPg = (PgHdr*)pLower->pExtra;
51100 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51101 a = (unsigned char *)pLower->pBuf;
51102 for(j=0; j<12; j++) printf("%02x", a[j]);
51103 printf("\n");
51104 if( pPg->pPage==0 ){
51105 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
51106 }
51107 }
51108 }
51109 #else
51110 # define pcacheTrace(X)
 
51111 # define pcacheDump(X)
51112 #endif
51113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51114 /*
51115 ** Check invariants on a PgHdr entry. Return true if everything is OK.
51116 ** Return false if any invariant is violated.
51117 **
51118 ** This routine is for use inside of assert() statements only. For
@@ -51127,12 +51275,17 @@
51127 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
51128 pCache = pPg->pCache;
51129 assert( pCache!=0 ); /* Every page has an associated PCache */
51130 if( pPg->flags & PGHDR_CLEAN ){
51131 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
51132 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
51133 assert( pCache->pDirtyTail!=pPg );
 
 
 
 
 
51134 }
51135 /* WRITEABLE pages must also be DIRTY */
51136 if( pPg->flags & PGHDR_WRITEABLE ){
51137 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
51138 }
@@ -51402,12 +51555,13 @@
51402 eCreate = createFlag & pCache->eCreate;
51403 assert( eCreate==0 || eCreate==1 || eCreate==2 );
51404 assert( createFlag==0 || pCache->eCreate==eCreate );
51405 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
51406 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51407 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
51408 createFlag?" create":"",pRes));
 
51409 return pRes;
51410 }
51411
51412 /*
51413 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
@@ -51531,10 +51685,11 @@
51531 if( (--p->nRef)==0 ){
51532 if( p->flags&PGHDR_CLEAN ){
51533 pcacheUnpin(p);
51534 }else{
51535 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
 
51536 }
51537 }
51538 }
51539
51540 /*
@@ -51574,10 +51729,11 @@
51574 if( p->flags & PGHDR_CLEAN ){
51575 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
51576 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
51577 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
51578 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
 
51579 }
51580 assert( sqlite3PcachePageSanity(p) );
51581 }
51582 }
51583
@@ -51636,18 +51792,28 @@
51636 /*
51637 ** Change the page number of page p to newPgno.
51638 */
51639 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
51640 PCache *pCache = p->pCache;
 
51641 assert( p->nRef>0 );
51642 assert( newPgno>0 );
51643 assert( sqlite3PcachePageSanity(p) );
51644 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
 
51645 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
 
 
 
 
 
 
 
51646 p->pgno = newPgno;
51647 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
51648 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
 
51649 }
51650 }
51651
51652 /*
51653 ** Drop every cache entry whose page number is greater than "pgno". The
@@ -51941,16 +52107,17 @@
51941 ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
51942 ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
51943 ** size can vary according to architecture, compile-time options, and
51944 ** SQLite library version number.
51945 **
51946 ** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
51947 ** using a separate memory allocation from the database page content. This
51948 ** seeks to overcome the "clownshoe" problem (also called "internal
51949 ** fragmentation" in academic literature) of allocating a few bytes more
51950 ** than a power of two with the memory allocator rounding up to the next
51951 ** power of two, and leaving the rounded-up space unused.
 
51952 **
51953 ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
51954 ** with this module. Information is passed back and forth as PgHdr1 pointers.
51955 **
51956 ** The pcache.c and pager.c modules deal pointers to PgHdr objects.
@@ -51991,34 +52158,44 @@
51991 typedef struct PgFreeslot PgFreeslot;
51992 typedef struct PGroup PGroup;
51993
51994 /*
51995 ** Each cache entry is represented by an instance of the following
51996 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
51997 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
51998 ** in memory.
51999 **
52000 ** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
 
 
 
 
 
 
52001 ** but causes a 2-byte gap in the structure for most architectures (since
52002 ** pointers must be either 4 or 8-byte aligned). As this structure is located
52003 ** in memory directly after the associated page data, if the database is
52004 ** corrupt, code at the b-tree layer may overread the page buffer and
52005 ** read part of this structure before the corruption is detected. This
52006 ** can cause a valgrind error if the unitialized gap is accessed. Using u16
52007 ** ensures there is no such gap, and therefore no bytes of unitialized memory
52008 ** in the structure.
 
 
 
 
 
52009 */
52010 struct PgHdr1 {
52011 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
52012 unsigned int iKey; /* Key value (page number) */
52013 u16 isBulkLocal; /* This page from bulk local storage */
52014 u16 isAnchor; /* This is the PGroup.lru element */
52015 PgHdr1 *pNext; /* Next in hash table chain */
52016 PCache1 *pCache; /* Cache that currently owns this page */
52017 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
52018 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
52019 /* NB: pLruPrev is only valid if pLruNext!=0 */
52020 };
52021
52022 /*
52023 ** A page is pinned if it is not on the LRU list. To be "pinned" means
52024 ** that the page is in active use and must not be deallocated.
@@ -52340,29 +52517,17 @@
52340 assert( pcache1.separateCache==0 );
52341 assert( pCache->pGroup==&pcache1.grp );
52342 pcache1LeaveMutex(pCache->pGroup);
52343 #endif
52344 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
52345 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
52346 pPg = pcache1Alloc(pCache->szPage);
52347 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
52348 if( !pPg || !p ){
52349 pcache1Free(pPg);
52350 sqlite3_free(p);
52351 pPg = 0;
52352 }
52353 #else
52354 pPg = pcache1Alloc(pCache->szAlloc);
52355 #endif
52356 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
52357 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
52358 pcache1EnterMutex(pCache->pGroup);
52359 #endif
52360 if( pPg==0 ) return 0;
52361 #ifndef SQLITE_PCACHE_SEPARATE_HEADER
52362 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
52363 #endif
52364 p->page.pBuf = pPg;
52365 p->page.pExtra = &p[1];
52366 p->isBulkLocal = 0;
52367 p->isAnchor = 0;
52368 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
@@ -52382,13 +52547,10 @@
52382 if( p->isBulkLocal ){
52383 p->pNext = pCache->pFree;
52384 pCache->pFree = p;
52385 }else{
52386 pcache1Free(p->page.pBuf);
52387 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
52388 sqlite3_free(p);
52389 #endif
52390 }
52391 (*pCache->pnPurgeable)--;
52392 }
52393
52394 /*
@@ -53025,27 +53187,45 @@
53025 unsigned int iNew
53026 ){
53027 PCache1 *pCache = (PCache1 *)p;
53028 PgHdr1 *pPage = (PgHdr1 *)pPg;
53029 PgHdr1 **pp;
53030 unsigned int h;
53031 assert( pPage->iKey==iOld );
53032 assert( pPage->pCache==pCache );
 
53033
53034 pcache1EnterMutex(pCache->pGroup);
53035
53036 h = iOld%pCache->nHash;
53037 pp = &pCache->apHash[h];
 
53038 while( (*pp)!=pPage ){
53039 pp = &(*pp)->pNext;
53040 }
53041 *pp = pPage->pNext;
53042
53043 h = iNew%pCache->nHash;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53044 pPage->iKey = iNew;
53045 pPage->pNext = pCache->apHash[h];
53046 pCache->apHash[h] = pPage;
53047 if( iNew>pCache->iMaxKey ){
53048 pCache->iMaxKey = iNew;
53049 }
53050
53051 pcache1LeaveMutex(pCache->pGroup);
@@ -53148,13 +53328,10 @@
53148 while( (nReq<0 || nFree<nReq)
53149 && (p=pcache1.grp.lru.pLruPrev)!=0
53150 && p->isAnchor==0
53151 ){
53152 nFree += pcache1MemSize(p->page.pBuf);
53153 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
53154 nFree += sqlite3MemSize(p);
53155 #endif
53156 assert( PAGE_IS_UNPINNED(p) );
53157 pcache1PinPage(p);
53158 pcache1RemoveFromHash(p, 1);
53159 }
53160 pcache1LeaveMutex(&pcache1.grp);
@@ -59639,10 +59816,11 @@
59639 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
59640 int nSpill;
59641
59642 if( pPager->tempFile ){
59643 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
 
59644 nSpill = sqlite3Config.nStmtSpill;
59645 }else{
59646 flags |= SQLITE_OPEN_MAIN_JOURNAL;
59647 nSpill = jrnlBufferSize(pPager);
59648 }
@@ -59674,10 +59852,11 @@
59674 }
59675
59676 if( rc!=SQLITE_OK ){
59677 sqlite3BitvecDestroy(pPager->pInJournal);
59678 pPager->pInJournal = 0;
 
59679 }else{
59680 assert( pPager->eState==PAGER_WRITER_LOCKED );
59681 pPager->eState = PAGER_WRITER_CACHEMOD;
59682 }
59683
@@ -66737,10 +66916,11 @@
66737 ** db using sqlite3SchemaToIndex().
66738 */
66739 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
66740 Btree *p;
66741 assert( db!=0 );
 
66742 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
66743 assert( iDb>=0 && iDb<db->nDb );
66744 if( !sqlite3_mutex_held(db->mutex) ) return 0;
66745 if( iDb==1 ) return 1;
66746 p = db->aDb[iDb].pBt;
@@ -68309,12 +68489,11 @@
68309 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68310 assert( pPage->pBt!=0 );
68311 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
68312 assert( pPage->nOverflow==0 );
68313 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68314 temp = 0;
68315 src = data = pPage->aData;
68316 hdr = pPage->hdrOffset;
68317 cellOffset = pPage->cellOffset;
68318 nCell = pPage->nCell;
68319 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
68320 iCellFirst = cellOffset + 2*nCell;
@@ -68364,43 +68543,42 @@
68364 }
68365
68366 cbrk = usableSize;
68367 iCellLast = usableSize - 4;
68368 iCellStart = get2byte(&data[hdr+5]);
68369 for(i=0; i<nCell; i++){
68370 u8 *pAddr; /* The i-th cell pointer */
68371 pAddr = &data[cellOffset + i*2];
68372 pc = get2byte(pAddr);
68373 testcase( pc==iCellFirst );
68374 testcase( pc==iCellLast );
68375 /* These conditions have already been verified in btreeInitPage()
68376 ** if PRAGMA cell_size_check=ON.
68377 */
68378 if( pc<iCellStart || pc>iCellLast ){
68379 return SQLITE_CORRUPT_PAGE(pPage);
68380 }
68381 assert( pc>=iCellStart && pc<=iCellLast );
68382 size = pPage->xCellSize(pPage, &src[pc]);
68383 cbrk -= size;
68384 if( cbrk<iCellStart || pc+size>usableSize ){
68385 return SQLITE_CORRUPT_PAGE(pPage);
68386 }
68387 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
68388 testcase( cbrk+size==usableSize );
68389 testcase( pc+size==usableSize );
68390 put2byte(pAddr, cbrk);
68391 if( temp==0 ){
68392 if( cbrk==pc ) continue;
68393 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68394 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68395 src = temp;
68396 }
68397 memcpy(&data[cbrk], &src[pc], size);
68398 }
68399 data[hdr+7] = 0;
68400
68401 defragment_out:
68402 assert( pPage->nFree>=0 );
68403 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
68404 return SQLITE_CORRUPT_PAGE(pPage);
68405 }
68406 assert( cbrk>=iCellFirst );
@@ -68469,13 +68647,13 @@
68469 return &aData[pc + x];
68470 }
68471 iAddr = pc;
68472 pTmp = &aData[pc];
68473 pc = get2byte(pTmp);
68474 if( pc<=iAddr+size ){
68475 if( pc ){
68476 /* The next slot in the chain is not past the end of the current slot */
68477 *pRc = SQLITE_CORRUPT_PAGE(pPg);
68478 }
68479 return 0;
68480 }
68481 }
@@ -68623,11 +68801,11 @@
68623 iPtr = hdr + 1;
68624 if( data[iPtr+1]==0 && data[iPtr]==0 ){
68625 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
68626 }else{
68627 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
68628 if( iFreeBlk<iPtr+4 ){
68629 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
68630 return SQLITE_CORRUPT_PAGE(pPage);
68631 }
68632 iPtr = iFreeBlk;
68633 }
@@ -69105,13 +69283,11 @@
69105 if( pCur ){
69106 pCur->iPage--;
69107 pCur->pPage = pCur->apPage[pCur->iPage];
69108 }
69109 testcase( pgno==0 );
69110 assert( pgno!=0 || rc==SQLITE_CORRUPT
69111 || rc==SQLITE_IOERR_NOMEM
69112 || rc==SQLITE_NOMEM );
69113 return rc;
69114 }
69115
69116 /*
69117 ** Release a MemPage. This should be called once for each prior
@@ -72049,12 +72225,10 @@
72049 ** the new child page does not match the flags field of the parent (i.e.
72050 ** if an intkey page appears to be the parent of a non-intkey page, or
72051 ** vice-versa).
72052 */
72053 static int moveToChild(BtCursor *pCur, u32 newPgno){
72054 BtShared *pBt = pCur->pBt;
72055
72056 assert( cursorOwnsBtShared(pCur) );
72057 assert( pCur->eState==CURSOR_VALID );
72058 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
72059 assert( pCur->iPage>=0 );
72060 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
@@ -72064,11 +72238,12 @@
72064 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72065 pCur->aiIdx[pCur->iPage] = pCur->ix;
72066 pCur->apPage[pCur->iPage] = pCur->pPage;
72067 pCur->ix = 0;
72068 pCur->iPage++;
72069 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
 
72070 }
72071
72072 #ifdef SQLITE_DEBUG
72073 /*
72074 ** Page pParent is an internal (non-leaf) tree page. This function
@@ -72170,11 +72345,11 @@
72170 assert( pCur->skipNext!=SQLITE_OK );
72171 return pCur->skipNext;
72172 }
72173 sqlite3BtreeClearCursor(pCur);
72174 }
72175 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
72176 0, pCur->curPagerFlags);
72177 if( rc!=SQLITE_OK ){
72178 pCur->eState = CURSOR_INVALID;
72179 return rc;
72180 }
@@ -73811,16 +73986,10 @@
73811 data = pPage->aData;
73812 ptr = &pPage->aCellIdx[2*idx];
73813 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
73814 pc = get2byte(ptr);
73815 hdr = pPage->hdrOffset;
73816 #if 0 /* Not required. Omit for efficiency */
73817 if( pc<hdr+pPage->nCell*2 ){
73818 *pRC = SQLITE_CORRUPT_BKPT;
73819 return;
73820 }
73821 #endif
73822 testcase( pc==(u32)get2byte(&data[hdr+5]) );
73823 testcase( pc+sz==pPage->pBt->usableSize );
73824 if( pc+sz > pPage->pBt->usableSize ){
73825 *pRC = SQLITE_CORRUPT_BKPT;
73826 return;
@@ -74700,12 +74869,10 @@
74700 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
74701 u8 *aSpace1; /* Space for copies of dividers cells */
74702 Pgno pgno; /* Temp var to store a page number in */
74703 u8 abDone[NB+2]; /* True after i'th new page is populated */
74704 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
74705 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
74706 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
74707 CellArray b; /* Parsed information on cells being balanced */
74708
74709 memset(abDone, 0, sizeof(abDone));
74710 memset(&b, 0, sizeof(b));
74711 pBt = pParent->pBt;
@@ -75125,46 +75292,43 @@
75125 ** Reassign page numbers so that the new pages are in ascending order.
75126 ** This helps to keep entries in the disk file in order so that a scan
75127 ** of the table is closer to a linear scan through the file. That in turn
75128 ** helps the operating system to deliver pages from the disk more rapidly.
75129 **
75130 ** An O(n^2) insertion sort algorithm is used, but since n is never more
75131 ** than (NB+2) (a small constant), that should not be a problem.
75132 **
75133 ** When NB==3, this one optimization makes the database about 25% faster
75134 ** for large insertions and deletions.
75135 */
75136 for(i=0; i<nNew; i++){
75137 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75138 aPgFlags[i] = apNew[i]->pDbPage->flags;
75139 for(j=0; j<i; j++){
75140 if( NEVER(aPgno[j]==aPgno[i]) ){
75141 /* This branch is taken if the set of sibling pages somehow contains
75142 ** duplicate entries. This can happen if the database is corrupt.
75143 ** It would be simpler to detect this as part of the loop below, but
75144 ** we do the detection here in order to avoid populating the pager
75145 ** cache with two separate objects associated with the same
75146 ** page number. */
75147 assert( CORRUPT_DB );
75148 rc = SQLITE_CORRUPT_BKPT;
75149 goto balance_cleanup;
75150 }
75151 }
75152 }
75153 for(i=0; i<nNew; i++){
75154 int iBest = 0; /* aPgno[] index of page number to use */
75155 for(j=1; j<nNew; j++){
75156 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
75157 }
75158 pgno = aPgOrder[iBest];
75159 aPgOrder[iBest] = 0xffffffff;
75160 if( iBest!=i ){
75161 if( iBest>i ){
75162 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75163 }
75164 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75165 apNew[i]->pgno = pgno;
75166 }
75167 }
75168
75169 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
75170 "%d(%d nc=%d) %d(%d nc=%d)\n",
@@ -79428,10 +79592,20 @@
79428 double r2 = (double)i;
79429 return r1==0.0
79430 || (memcmp(&r1, &r2, sizeof(r1))==0
79431 && i >= -2251799813685248LL && i < 2251799813685248LL);
79432 }
 
 
 
 
 
 
 
 
 
 
79433
79434 /*
79435 ** Convert pMem so that it has type MEM_Real or MEM_Int.
79436 ** Invalidate any prior representations.
79437 **
@@ -79450,11 +79624,11 @@
79450 sqlite3_int64 ix;
79451 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
79452 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79453 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
79454 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79455 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
79456 ){
79457 pMem->u.i = ix;
79458 MemSetTypeFlag(pMem, MEM_Int);
79459 }else{
79460 MemSetTypeFlag(pMem, MEM_Real);
@@ -80682,14 +80856,14 @@
80682 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
80683 if( p==0 ) return 0;
80684 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
80685 p->db = db;
80686 if( db->pVdbe ){
80687 db->pVdbe->pPrev = p;
80688 }
80689 p->pNext = db->pVdbe;
80690 p->pPrev = 0;
80691 db->pVdbe = p;
80692 assert( p->eVdbeState==VDBE_INIT_STATE );
80693 p->pParse = pParse;
80694 pParse->pVdbe = p;
80695 assert( pParse->aLabel==0 );
@@ -80767,25 +80941,32 @@
80767 return 0;
80768 }
80769 #endif
80770
80771 /*
80772 ** Swap all content between two VDBE structures.
 
 
 
 
 
 
 
80773 */
80774 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
80775 Vdbe tmp, *pTmp;
80776 char *zTmp;
80777 assert( pA->db==pB->db );
80778 tmp = *pA;
80779 *pA = *pB;
80780 *pB = tmp;
80781 pTmp = pA->pNext;
80782 pA->pNext = pB->pNext;
80783 pB->pNext = pTmp;
80784 pTmp = pA->pPrev;
80785 pA->pPrev = pB->pPrev;
80786 pB->pPrev = pTmp;
80787 zTmp = pA->zSql;
80788 pA->zSql = pB->zSql;
80789 pB->zSql = zTmp;
80790 #ifdef SQLITE_ENABLE_NORMALIZE
80791 zTmp = pA->zNormSql;
@@ -81033,10 +81214,11 @@
81033 pCtx->argc = nArg;
81034 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
81035 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
81036 p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
81037 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
 
81038 return addr;
81039 }
81040
81041 /*
81042 ** Add an opcode that includes the p4 value with a P4_INT64 or
@@ -81101,11 +81283,11 @@
81101 va_end(ap);
81102 v = pParse->pVdbe;
81103 iThis = v->nOp;
81104 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
81105 zMsg, P4_DYNAMIC);
81106 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
81107 if( bPush){
81108 pParse->addrExplain = iThis;
81109 }
81110 }
81111 }
@@ -81368,10 +81550,11 @@
81368 int opcode = pOp->opcode;
81369 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
81370 || opcode==OP_VDestroy
81371 || opcode==OP_VCreate
81372 || opcode==OP_ParseSchema
 
81373 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
81374 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
81375 ){
81376 hasAbort = 1;
81377 break;
@@ -81458,12 +81641,12 @@
81458 Parse *pParse = p->pParse;
81459 int *aLabel = pParse->aLabel;
81460 p->readOnly = 1;
81461 p->bIsReader = 0;
81462 pOp = &p->aOp[p->nOp-1];
81463 while(1){
81464
81465 /* Only JUMP opcodes and the short list of special opcodes in the switch
81466 ** below need to be considered. The mkopcodeh.tcl generator script groups
81467 ** all these opcodes together near the front of the opcode list. Skip
81468 ** any opcode that does not need processing by virtual of the fact that
81469 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
@@ -81488,10 +81671,14 @@
81488 case OP_JournalMode: {
81489 p->readOnly = 0;
81490 p->bIsReader = 1;
81491 break;
81492 }
 
 
 
 
81493 #ifndef SQLITE_OMIT_VIRTUALTABLE
81494 case OP_VUpdate: {
81495 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
81496 break;
81497 }
@@ -81520,15 +81707,16 @@
81520 /* The mkopcodeh.tcl script has so arranged things that the only
81521 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81522 ** have non-negative values for P2. */
81523 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
81524 }
81525 if( pOp==p->aOp ) break;
81526 pOp--;
81527 }
 
81528 if( aLabel ){
81529 sqlite3DbFreeNN(p->db, pParse->aLabel);
81530 pParse->aLabel = 0;
81531 }
81532 pParse->nLabel = 0;
81533 *pMaxFuncArgs = nMaxArgs;
81534 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
@@ -81773,19 +81961,23 @@
81773 /*
81774 ** Change the value of the opcode, or P1, P2, P3, or P5 operands
81775 ** for a specific instruction.
81776 */
81777 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
 
81778 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
81779 }
81780 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
 
81781 sqlite3VdbeGetOp(p,addr)->p1 = val;
81782 }
81783 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
 
81784 sqlite3VdbeGetOp(p,addr)->p2 = val;
81785 }
81786 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
 
81787 sqlite3VdbeGetOp(p,addr)->p3 = val;
81788 }
81789 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
81790 assert( p->nOp>0 || p->db->mallocFailed );
81791 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
@@ -81817,11 +82009,11 @@
81817 assert( p->aOp[addr].opcode==OP_Once
81818 || p->aOp[addr].opcode==OP_If
81819 || p->aOp[addr].opcode==OP_FkIfZero );
81820 assert( p->aOp[addr].p4type==0 );
81821 #ifdef SQLITE_VDBE_COVERAGE
81822 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
81823 #endif
81824 p->nOp--;
81825 }else{
81826 sqlite3VdbeChangeP2(p, addr, p->nOp);
81827 }
@@ -81831,25 +82023,27 @@
81831 /*
81832 ** If the input FuncDef structure is ephemeral, then free it. If
81833 ** the FuncDef is not ephermal, then do nothing.
81834 */
81835 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
 
81836 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
81837 sqlite3DbFreeNN(db, pDef);
81838 }
81839 }
81840
81841 /*
81842 ** Delete a P4 value if necessary.
81843 */
81844 static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
81845 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
81846 sqlite3DbFreeNN(db, p);
81847 }
81848 static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
 
81849 freeEphemeralFunction(db, p->pFunc);
81850 sqlite3DbFreeNN(db, p);
81851 }
81852 static void freeP4(sqlite3 *db, int p4type, void *p4){
81853 assert( db );
81854 switch( p4type ){
81855 case P4_FUNCCTX: {
@@ -81858,11 +82052,11 @@
81858 }
81859 case P4_REAL:
81860 case P4_INT64:
81861 case P4_DYNAMIC:
81862 case P4_INTARRAY: {
81863 sqlite3DbFree(db, p4);
81864 break;
81865 }
81866 case P4_KEYINFO: {
81867 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
81868 break;
@@ -81897,10 +82091,11 @@
81897 ** opcodes contained within. If aOp is not NULL it is assumed to contain
81898 ** nOp entries.
81899 */
81900 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
81901 assert( nOp>=0 );
 
81902 if( aOp ){
81903 Op *pOp = &aOp[nOp-1];
81904 while(1){ /* Exit via break */
81905 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
81906 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -81907,11 +82102,11 @@
81907 sqlite3DbFree(db, pOp->zComment);
81908 #endif
81909 if( pOp==aOp ) break;
81910 pOp--;
81911 }
81912 sqlite3DbFreeNN(db, aOp);
81913 }
81914 }
81915
81916 /*
81917 ** Link the SubProgram object passed as the second argument into the linked
@@ -82138,17 +82333,17 @@
82138 #ifdef SQLITE_VDBE_COVERAGE
82139 /*
82140 ** Set the value if the iSrcLine field for the previously coded instruction.
82141 */
82142 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
82143 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
82144 }
82145 #endif /* SQLITE_VDBE_COVERAGE */
82146
82147 /*
82148 ** Return the opcode for a given address. If the address is -1, then
82149 ** return the most recently inserted opcode.
82150 **
82151 ** If a memory allocation error has occurred prior to the calling of this
82152 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
82153 ** is readable but not writable, though it is cast to a writable value.
82154 ** The return of a dummy opcode allows the call to continue functioning
@@ -82160,20 +82355,23 @@
82160 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
82161 /* C89 specifies that the constant "dummy" will be initialized to all
82162 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
82163 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
82164 assert( p->eVdbeState==VDBE_INIT_STATE );
82165 if( addr<0 ){
82166 addr = p->nOp - 1;
82167 }
82168 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
82169 if( p->db->mallocFailed ){
82170 return (VdbeOp*)&dummy;
82171 }else{
82172 return &p->aOp[addr];
82173 }
82174 }
 
 
 
 
 
 
82175
82176 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
82177 /*
82178 ** Return an integer value for one of the parameters to the opcode pOp
82179 ** determined by character c.
@@ -82658,11 +82856,11 @@
82658 if( p->flags&(MEM_Agg|MEM_Dyn) ){
82659 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
82660 sqlite3VdbeMemRelease(p);
82661 p->flags = MEM_Undefined;
82662 }else if( p->szMalloc ){
82663 sqlite3DbFreeNN(db, p->zMalloc);
82664 p->szMalloc = 0;
82665 p->flags = MEM_Undefined;
82666 }
82667 #ifdef SQLITE_DEBUG
82668 else{
@@ -83650,11 +83848,11 @@
83650 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
83651 cnt++;
83652 if( p->readOnly==0 ) nWrite++;
83653 if( p->bIsReader ) nRead++;
83654 }
83655 p = p->pNext;
83656 }
83657 assert( cnt==db->nVdbeActive );
83658 assert( nWrite==db->nVdbeWrite );
83659 assert( nRead==db->nVdbeRead );
83660 }
@@ -84179,27 +84377,28 @@
84179 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
84180 ** the database connection and frees the object itself.
84181 */
84182 static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
84183 SubProgram *pSub, *pNext;
 
84184 assert( p->db==0 || p->db==db );
84185 if( p->aColName ){
84186 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
84187 sqlite3DbFreeNN(db, p->aColName);
84188 }
84189 for(pSub=p->pProgram; pSub; pSub=pNext){
84190 pNext = pSub->pNext;
84191 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
84192 sqlite3DbFree(db, pSub);
84193 }
84194 if( p->eVdbeState!=VDBE_INIT_STATE ){
84195 releaseMemArray(p->aVar, p->nVar);
84196 if( p->pVList ) sqlite3DbFreeNN(db, p->pVList);
84197 if( p->pFree ) sqlite3DbFreeNN(db, p->pFree);
84198 }
84199 vdbeFreeOpArray(db, p->aOp, p->nOp);
84200 sqlite3DbFree(db, p->zSql);
84201 #ifdef SQLITE_ENABLE_NORMALIZE
84202 sqlite3DbFree(db, p->zNormSql);
84203 {
84204 DblquoteStr *pThis, *pNext;
84205 for(pThis=p->pDblStr; pThis; pThis=pNext){
@@ -84225,24 +84424,21 @@
84225 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
84226 sqlite3 *db;
84227
84228 assert( p!=0 );
84229 db = p->db;
 
84230 assert( sqlite3_mutex_held(db->mutex) );
84231 sqlite3VdbeClearObject(db, p);
84232 if( db->pnBytesFreed==0 ){
84233 if( p->pPrev ){
84234 p->pPrev->pNext = p->pNext;
84235 }else{
84236 assert( db->pVdbe==p );
84237 db->pVdbe = p->pNext;
84238 }
84239 if( p->pNext ){
84240 p->pNext->pPrev = p->pPrev;
84241 }
84242 }
84243 sqlite3DbFreeNN(db, p);
84244 }
84245
84246 /*
84247 ** The cursor "p" has a pending seek operation that has not yet been
84248 ** carried out. Seek the cursor now. If an error occurs, return
@@ -85733,11 +85929,11 @@
85733 ** prepared statements. The flag is set to 1 for an immediate expiration
85734 ** and set to 2 for an advisory expiration.
85735 */
85736 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
85737 Vdbe *p;
85738 for(p = db->pVdbe; p; p=p->pNext){
85739 p->expired = iCode+1;
85740 }
85741 }
85742
85743 /*
@@ -85854,17 +86050,18 @@
85854 **
85855 ** This function is used to free UnpackedRecord structures allocated by
85856 ** the vdbeUnpackRecord() function found in vdbeapi.c.
85857 */
85858 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
 
85859 if( p ){
85860 int i;
85861 for(i=0; i<nField; i++){
85862 Mem *pMem = &p->aMem[i];
85863 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
85864 }
85865 sqlite3DbFreeNN(db, p);
85866 }
85867 }
85868 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
85869
85870 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
@@ -85931,11 +86128,11 @@
85931 if( preupdate.aNew ){
85932 int i;
85933 for(i=0; i<pCsr->nField; i++){
85934 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
85935 }
85936 sqlite3DbFreeNN(db, preupdate.aNew);
85937 }
85938 }
85939 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
85940
85941 /************** End of vdbeaux.c *********************************************/
@@ -86048,11 +86245,13 @@
86048 Vdbe *v = (Vdbe*)pStmt;
86049 sqlite3 *db = v->db;
86050 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
86051 sqlite3_mutex_enter(db->mutex);
86052 checkProfileCallback(db, v);
86053 rc = sqlite3VdbeFinalize(v);
 
 
86054 rc = sqlite3ApiExit(db, rc);
86055 sqlite3LeaveMutexAndCloseZombie(db);
86056 }
86057 return rc;
86058 }
@@ -87370,11 +87569,11 @@
87370 ** the mutex is released if any kind of error occurs.
87371 **
87372 ** The error code stored in database p->db is overwritten with the return
87373 ** value in any case.
87374 */
87375 static int vdbeUnbind(Vdbe *p, int i){
87376 Mem *pVar;
87377 if( vdbeSafetyNotNull(p) ){
87378 return SQLITE_MISUSE_BKPT;
87379 }
87380 sqlite3_mutex_enter(p->db->mutex);
@@ -87383,16 +87582,15 @@
87383 sqlite3_mutex_leave(p->db->mutex);
87384 sqlite3_log(SQLITE_MISUSE,
87385 "bind on a busy prepared statement: [%s]", p->zSql);
87386 return SQLITE_MISUSE_BKPT;
87387 }
87388 if( i<1 || i>p->nVar ){
87389 sqlite3Error(p->db, SQLITE_RANGE);
87390 sqlite3_mutex_leave(p->db->mutex);
87391 return SQLITE_RANGE;
87392 }
87393 i--;
87394 pVar = &p->aVar[i];
87395 sqlite3VdbeMemRelease(pVar);
87396 pVar->flags = MEM_Null;
87397 p->db->errCode = SQLITE_OK;
87398
@@ -87425,11 +87623,11 @@
87425 ){
87426 Vdbe *p = (Vdbe *)pStmt;
87427 Mem *pVar;
87428 int rc;
87429
87430 rc = vdbeUnbind(p, i);
87431 if( rc==SQLITE_OK ){
87432 if( zData!=0 ){
87433 pVar = &p->aVar[i-1];
87434 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
87435 if( rc==SQLITE_OK && encoding!=0 ){
@@ -87474,11 +87672,11 @@
87474 return bindText(pStmt, i, zData, nData, xDel, 0);
87475 }
87476 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
87477 int rc;
87478 Vdbe *p = (Vdbe *)pStmt;
87479 rc = vdbeUnbind(p, i);
87480 if( rc==SQLITE_OK ){
87481 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
87482 sqlite3_mutex_leave(p->db->mutex);
87483 }
87484 return rc;
@@ -87487,21 +87685,21 @@
87487 return sqlite3_bind_int64(p, i, (i64)iValue);
87488 }
87489 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
87490 int rc;
87491 Vdbe *p = (Vdbe *)pStmt;
87492 rc = vdbeUnbind(p, i);
87493 if( rc==SQLITE_OK ){
87494 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
87495 sqlite3_mutex_leave(p->db->mutex);
87496 }
87497 return rc;
87498 }
87499 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
87500 int rc;
87501 Vdbe *p = (Vdbe*)pStmt;
87502 rc = vdbeUnbind(p, i);
87503 if( rc==SQLITE_OK ){
87504 sqlite3_mutex_leave(p->db->mutex);
87505 }
87506 return rc;
87507 }
@@ -87512,11 +87710,11 @@
87512 const char *zPTtype,
87513 void (*xDestructor)(void*)
87514 ){
87515 int rc;
87516 Vdbe *p = (Vdbe*)pStmt;
87517 rc = vdbeUnbind(p, i);
87518 if( rc==SQLITE_OK ){
87519 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
87520 sqlite3_mutex_leave(p->db->mutex);
87521 }else if( xDestructor ){
87522 xDestructor(pPtr);
@@ -87590,11 +87788,11 @@
87590 return rc;
87591 }
87592 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
87593 int rc;
87594 Vdbe *p = (Vdbe *)pStmt;
87595 rc = vdbeUnbind(p, i);
87596 if( rc==SQLITE_OK ){
87597 #ifndef SQLITE_OMIT_INCRBLOB
87598 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87599 #else
87600 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
@@ -87750,11 +87948,11 @@
87750 #endif
87751 sqlite3_mutex_enter(pDb->mutex);
87752 if( pStmt==0 ){
87753 pNext = (sqlite3_stmt*)pDb->pVdbe;
87754 }else{
87755 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
87756 }
87757 sqlite3_mutex_leave(pDb->mutex);
87758 return pNext;
87759 }
87760
@@ -87775,12 +87973,15 @@
87775 if( op==SQLITE_STMTSTATUS_MEMUSED ){
87776 sqlite3 *db = pVdbe->db;
87777 sqlite3_mutex_enter(db->mutex);
87778 v = 0;
87779 db->pnBytesFreed = (int*)&v;
 
 
87780 sqlite3VdbeDelete(pVdbe);
87781 db->pnBytesFreed = 0;
 
87782 sqlite3_mutex_leave(db->mutex);
87783 }else{
87784 v = pVdbe->aCounter[op];
87785 if( resetFlag ) pVdbe->aCounter[op] = 0;
87786 }
@@ -88616,11 +88817,12 @@
88616 ** floating point value of rValue. Return true and set *piValue to the
88617 ** integer value if the string is in range to be an integer. Otherwise,
88618 ** return false.
88619 */
88620 static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
88621 i64 iValue = (double)rValue;
 
88622 if( sqlite3RealSameAsInt(rValue,iValue) ){
88623 *piValue = iValue;
88624 return 1;
88625 }
88626 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
@@ -88778,21 +88980,22 @@
88778 **
88779 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
88780 ** But it does set pMem->u.r and pMem->u.i appropriately.
88781 */
88782 static u16 numericType(Mem *pMem){
88783 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
 
 
88784 testcase( pMem->flags & MEM_Int );
88785 testcase( pMem->flags & MEM_Real );
88786 testcase( pMem->flags & MEM_IntReal );
88787 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
88788 }
88789 if( pMem->flags & (MEM_Str|MEM_Blob) ){
88790 testcase( pMem->flags & MEM_Str );
88791 testcase( pMem->flags & MEM_Blob );
88792 return computeNumericType(pMem);
88793 }
88794 return 0;
88795 }
88796
88797 #ifdef SQLITE_DEBUG
88798 /*
@@ -90033,25 +90236,24 @@
90033 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
90034 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
90035 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
90036 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
90037 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
90038 u16 flags; /* Combined MEM_* flags from both inputs */
90039 u16 type1; /* Numeric type of left operand */
90040 u16 type2; /* Numeric type of right operand */
90041 i64 iA; /* Integer value of left operand */
90042 i64 iB; /* Integer value of right operand */
90043 double rA; /* Real value of left operand */
90044 double rB; /* Real value of right operand */
90045
90046 pIn1 = &aMem[pOp->p1];
90047 type1 = numericType(pIn1);
90048 pIn2 = &aMem[pOp->p2];
90049 type2 = numericType(pIn2);
90050 pOut = &aMem[pOp->p3];
90051 flags = pIn1->flags | pIn2->flags;
90052 if( (type1 & type2 & MEM_Int)!=0 ){
 
90053 iA = pIn1->u.i;
90054 iB = pIn2->u.i;
90055 switch( pOp->opcode ){
90056 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
90057 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
@@ -90069,13 +90271,16 @@
90069 break;
90070 }
90071 }
90072 pOut->u.i = iB;
90073 MemSetTypeFlag(pOut, MEM_Int);
90074 }else if( (flags & MEM_Null)!=0 ){
90075 goto arithmetic_result_is_null;
90076 }else{
 
 
 
90077 fp_math:
90078 rA = sqlite3VdbeRealValue(pIn1);
90079 rB = sqlite3VdbeRealValue(pIn2);
90080 switch( pOp->opcode ){
90081 case OP_Add: rB += rA; break;
@@ -90941,15 +91146,18 @@
90941 **
90942 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
90943 ** If it is, then set register P3 to NULL and jump immediately to P2.
90944 ** If P1 is not on a NULL row, then fall through without making any
90945 ** changes.
 
 
90946 */
90947 case OP_IfNullRow: { /* jump */
 
90948 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90949 assert( p->apCsr[pOp->p1]!=0 );
90950 if( p->apCsr[pOp->p1]->nullRow ){
90951 sqlite3VdbeMemSetNull(aMem + pOp->p3);
90952 goto jump_to_p2;
90953 }
90954 break;
90955 }
@@ -93052,11 +93260,11 @@
93052 **
93053 ** <li> If the cursor is successfully moved to the target row by 0 or more
93054 ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
93055 ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
93056 **
93057 ** <li> If the cursor ends up past the target row (indicating the the target
93058 ** row does not exist in the btree) then jump to SeekOP.P2.
93059 ** </ol>
93060 */
93061 case OP_SeekScan: {
93062 VdbeCursor *pC;
@@ -94388,11 +94596,13 @@
94388 rc = sqlite3VdbeSorterNext(db, pC);
94389 goto next_tail;
94390
94391 case OP_Prev: /* jump */
94392 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94393 assert( pOp->p5<ArraySize(p->aCounter) );
 
 
94394 pC = p->apCsr[pOp->p1];
94395 assert( pC!=0 );
94396 assert( pC->deferredMoveto==0 );
94397 assert( pC->eCurType==CURTYPE_BTREE );
94398 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
@@ -94401,11 +94611,13 @@
94401 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
94402 goto next_tail;
94403
94404 case OP_Next: /* jump */
94405 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94406 assert( pOp->p5<ArraySize(p->aCounter) );
 
 
94407 pC = p->apCsr[pOp->p1];
94408 assert( pC!=0 );
94409 assert( pC->deferredMoveto==0 );
94410 assert( pC->eCurType==CURTYPE_BTREE );
94411 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
@@ -94608,14 +94820,14 @@
94608
94609 /* The IdxRowid and Seek opcodes are combined because of the commonality
94610 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
94611 rc = sqlite3VdbeCursorRestore(pC);
94612
94613 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
94614 ** out from under the cursor. That will never happens for an IdxRowid
94615 ** or Seek opcode */
94616 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
94617
94618 if( !pC->nullRow ){
94619 rowid = 0; /* Not needed. Only used to silence a warning. */
94620 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
94621 if( rc!=SQLITE_OK ){
@@ -95513,11 +95725,11 @@
95513
95514 /* Opcode: OffsetLimit P1 P2 P3 * *
95515 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
95516 **
95517 ** This opcode performs a commonly used computation associated with
95518 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
95519 ** holds the offset counter. The opcode computes the combined value
95520 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
95521 ** value computed is the total number of rows that will need to be
95522 ** visited in order to complete the query.
95523 **
@@ -101110,10 +101322,12 @@
101110 sqlite3_file *pJfd, /* Preallocated, blank file handle */
101111 int flags, /* Opening flags */
101112 int nSpill /* Bytes buffered before opening the file */
101113 ){
101114 MemJournal *p = (MemJournal*)pJfd;
 
 
101115
101116 /* Zero the file-handle object. If nSpill was passed zero, initialize
101117 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
101118 ** case none of the code in this module is executed as a result of calls
101119 ** made on the journal file-handle. */
@@ -101552,13 +101766,11 @@
101552 if( ExprHasProperty(pExpr, EP_WinFunc) ){
101553 if( ALWAYS(pExpr->y.pWin!=0) ){
101554 pExpr->y.pWin->pOwner = pExpr;
101555 }
101556 }
101557 sqlite3ParserAddCleanup(pParse,
101558 (void(*)(sqlite3*,void*))sqlite3ExprDelete,
101559 pDup);
101560 }
101561 }
101562
101563 /*
101564 ** Subqueries stores the original database, table and column names for their
@@ -104363,11 +104575,13 @@
104363 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
104364 ** if appropriate.
104365 */
104366 static void exprSetHeight(Expr *p){
104367 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104368 if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight;
 
 
104369 if( ExprUseXSelect(p) ){
104370 heightOfSelect(p->x.pSelect, &nHeight);
104371 }else if( p->x.pList ){
104372 heightOfExprList(p->x.pList, &nHeight);
104373 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
@@ -104506,19 +104720,30 @@
104506 if( pRoot==0 ){
104507 assert( db->mallocFailed );
104508 sqlite3ExprDelete(db, pLeft);
104509 sqlite3ExprDelete(db, pRight);
104510 }else{
 
 
104511 if( pRight ){
104512 pRoot->pRight = pRight;
104513 pRoot->flags |= EP_Propagate & pRight->flags;
 
 
 
 
 
104514 }
104515 if( pLeft ){
104516 pRoot->pLeft = pLeft;
104517 pRoot->flags |= EP_Propagate & pLeft->flags;
 
 
 
 
 
104518 }
104519 exprSetHeight(pRoot);
104520 }
104521 }
104522
104523 /*
104524 ** Allocate an Expr node which joins as many as two subtrees.
@@ -104800,10 +105025,11 @@
104800 /*
104801 ** Recursively delete an expression tree.
104802 */
104803 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
104804 assert( p!=0 );
 
104805 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
104806 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
104807 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
104808 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
104809 #ifdef SQLITE_DEBUG
@@ -104831,16 +105057,12 @@
104831 sqlite3WindowDelete(db, p->y.pWin);
104832 }
104833 #endif
104834 }
104835 }
104836 if( ExprHasProperty(p, EP_MemToken) ){
104837 assert( !ExprHasProperty(p, EP_IntValue) );
104838 sqlite3DbFree(db, p->u.zToken);
104839 }
104840 if( !ExprHasProperty(p, EP_Static) ){
104841 sqlite3DbFreeNN(db, p);
104842 }
104843 }
104844 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
104845 if( p ) sqlite3ExprDeleteNN(db, p);
104846 }
@@ -104867,12 +105089,13 @@
104867 **
104868 ** The deferred delete is (currently) implemented by adding the
104869 ** pExpr to the pParse->pConstExpr list with a register number of 0.
104870 */
104871 SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
104872 pParse->pConstExpr =
104873 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
 
104874 }
104875
104876 /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
104877 ** expression.
104878 */
@@ -104942,11 +105165,10 @@
104942 ){
104943 nSize = EXPR_FULLSIZE;
104944 }else{
104945 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
104946 assert( !ExprHasProperty(p, EP_OuterON) );
104947 assert( !ExprHasProperty(p, EP_MemToken) );
104948 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
104949 if( p->pLeft || p->x.pList ){
104950 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
104951 }else{
104952 assert( p->pRight==0 );
@@ -105046,11 +105268,11 @@
105046 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
105047 }
105048 }
105049
105050 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
105051 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
105052 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
105053 pNew->flags |= staticFlag;
105054 ExprClearVVAProperties(pNew);
105055 if( dupFlags ){
105056 ExprSetVVAProperty(pNew, EP_Immutable);
@@ -105622,16 +105844,17 @@
105622 */
105623 static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
105624 int i = pList->nExpr;
105625 struct ExprList_item *pItem = pList->a;
105626 assert( pList->nExpr>0 );
 
105627 do{
105628 sqlite3ExprDelete(db, pItem->pExpr);
105629 sqlite3DbFree(db, pItem->zEName);
105630 pItem++;
105631 }while( --i>0 );
105632 sqlite3DbFreeNN(db, pList);
105633 }
105634 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
105635 if( pList ) exprListDeleteNN(db, pList);
105636 }
105637
@@ -106918,11 +107141,11 @@
106918 if( pLimit ){
106919 pLimit->affExpr = SQLITE_AFF_NUMERIC;
106920 pLimit = sqlite3PExpr(pParse, TK_NE,
106921 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
106922 }
106923 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
106924 pSel->pLimit->pLeft = pLimit;
106925 }else{
106926 /* If there is no pre-existing limit add a limit of 1 */
106927 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
106928 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
@@ -107432,11 +107655,11 @@
107432 u8 p5 /* P5 value for OP_Column + FLAGS */
107433 ){
107434 assert( pParse->pVdbe!=0 );
107435 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107436 if( p5 ){
107437 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
107438 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
107439 }
107440 return iReg;
107441 }
107442
@@ -107501,11 +107724,11 @@
107501 /*
107502 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107503 ** so that a subsequent copy will not be merged into this one.
107504 */
107505 static void setDoNotMergeFlagOnCopy(Vdbe *v){
107506 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
107507 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */
107508 }
107509 }
107510
107511 /*
@@ -107672,11 +107895,11 @@
107672 Table *pTab = pCol->pTab;
107673 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
107674 pCol->iSorterColumn, target);
107675 if( pCol->iColumn<0 ){
107676 VdbeComment((v,"%s.rowid",pTab->zName));
107677 }else{
107678 VdbeComment((v,"%s.%s",
107679 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107680 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
107681 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
107682 }
@@ -108267,10 +108490,25 @@
108267 ** on a LEFT JOIN NULL row.
108268 */
108269 case TK_IF_NULL_ROW: {
108270 int addrINR;
108271 u8 okConstFactor = pParse->okConstFactor;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108272 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108273 /* Temporarily disable factoring of constant expressions, since
108274 ** even though expressions may appear to be constant, they are not
108275 ** really constant because they originate from the right-hand side
108276 ** of a LEFT JOIN. */
@@ -108608,11 +108846,11 @@
108608 }else{
108609 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
108610 if( inReg!=target+i ){
108611 VdbeOp *pOp;
108612 if( copyOp==OP_Copy
108613 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
108614 && pOp->p1+pOp->p3+1==inReg
108615 && pOp->p2+pOp->p3+1==target+i
108616 && pOp->p5==0 /* The do-not-merge flag must be clear */
108617 ){
108618 pOp->p3++;
@@ -109644,10 +109882,11 @@
109644 ** fact is exploited for efficiency.
109645 */
109646 SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
109647 Walker w;
109648 struct RefSrcList x;
 
109649 memset(&w, 0, sizeof(w));
109650 memset(&x, 0, sizeof(x));
109651 w.xExprCallback = exprRefToSrcList;
109652 w.xSelectCallback = selectRefEnter;
109653 w.xSelectCallback2 = selectRefLeave;
@@ -109660,11 +109899,11 @@
109660 #ifndef SQLITE_OMIT_WINDOWFUNC
109661 if( ExprHasProperty(pExpr, EP_WinFunc) ){
109662 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109663 }
109664 #endif
109665 sqlite3DbFree(pParse->db, x.aiExclude);
109666 if( w.eCode & 0x01 ){
109667 return 1;
109668 }else if( w.eCode ){
109669 return 0;
109670 }else{
@@ -109691,21 +109930,22 @@
109691 ){
109692 AggInfo *pAggInfo = pExpr->pAggInfo;
109693 int iAgg = pExpr->iAgg;
109694 Parse *pParse = pWalker->pParse;
109695 sqlite3 *db = pParse->db;
109696 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
109697 if( pExpr->op==TK_AGG_COLUMN ){
109698 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
109699 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109700 pExpr = sqlite3ExprDup(db, pExpr, 0);
109701 if( pExpr ){
109702 pAggInfo->aCol[iAgg].pCExpr = pExpr;
109703 sqlite3ExprDeferredDelete(pParse, pExpr);
109704 }
109705 }
109706 }else{
 
109707 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
109708 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109709 pExpr = sqlite3ExprDup(db, pExpr, 0);
109710 if( pExpr ){
109711 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
@@ -109772,14 +110012,16 @@
109772 SrcList *pSrcList = pNC->pSrcList;
109773 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
109774
109775 assert( pNC->ncFlags & NC_UAggInfo );
109776 switch( pExpr->op ){
 
109777 case TK_AGG_COLUMN:
109778 case TK_COLUMN: {
109779 testcase( pExpr->op==TK_AGG_COLUMN );
109780 testcase( pExpr->op==TK_COLUMN );
 
109781 /* Check to see if the column is in one of the tables in the FROM
109782 ** clause of the aggregate query */
109783 if( ALWAYS(pSrcList!=0) ){
109784 SrcItem *pItem = pSrcList->a;
109785 for(i=0; i<pSrcList->nSrc; i++, pItem++){
@@ -109793,12 +110035,14 @@
109793 ** is not an entry there already.
109794 */
109795 int k;
109796 pCol = pAggInfo->aCol;
109797 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
109798 if( pCol->iTable==pExpr->iTable &&
109799 pCol->iColumn==pExpr->iColumn ){
 
 
109800 break;
109801 }
109802 }
109803 if( (k>=pAggInfo->nColumn)
109804 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
@@ -109809,19 +110053,21 @@
109809 pCol->iTable = pExpr->iTable;
109810 pCol->iColumn = pExpr->iColumn;
109811 pCol->iMem = ++pParse->nMem;
109812 pCol->iSorterColumn = -1;
109813 pCol->pCExpr = pExpr;
109814 if( pAggInfo->pGroupBy ){
109815 int j, n;
109816 ExprList *pGB = pAggInfo->pGroupBy;
109817 struct ExprList_item *pTerm = pGB->a;
109818 n = pGB->nExpr;
109819 for(j=0; j<n; j++, pTerm++){
109820 Expr *pE = pTerm->pExpr;
109821 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
109822 pE->iColumn==pExpr->iColumn ){
 
 
109823 pCol->iSorterColumn = j;
109824 break;
109825 }
109826 }
109827 }
@@ -109834,11 +110080,13 @@
109834 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
109835 ** pAggInfo->aCol[] entry.
109836 */
109837 ExprSetVVAProperty(pExpr, EP_NoReduce);
109838 pExpr->pAggInfo = pAggInfo;
109839 pExpr->op = TK_AGG_COLUMN;
 
 
109840 pExpr->iAgg = (i16)k;
109841 break;
109842 } /* endif pExpr->iTable==pItem->iCursor */
109843 } /* end loop over pSrcList */
109844 }
@@ -115256,10 +115504,11 @@
115256 ** no VDBE code was generated.
115257 */
115258 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
115259 sqlite3 *db;
115260 Vdbe *v;
 
115261
115262 assert( pParse->pToplevel==0 );
115263 db = pParse->db;
115264 assert( db->pParse==pParse );
115265 if( pParse->nested ) return;
@@ -115285,11 +115534,10 @@
115285 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
115286 if( v ){
115287 if( pParse->bReturning ){
115288 Returning *pReturning = pParse->u1.pReturning;
115289 int addrRewind;
115290 int i;
115291 int reg;
115292
115293 if( pReturning->nRetCol ){
115294 sqlite3VdbeAddOp0(v, OP_FkCheck);
115295 addrRewind =
@@ -115322,80 +115570,73 @@
115322 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
115323 ** set for each database that is used. Generate code to start a
115324 ** transaction on each used database and to verify the schema cookie
115325 ** on each used database.
115326 */
115327 if( db->mallocFailed==0
115328 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
115329 ){
115330 int iDb, i;
115331 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115332 sqlite3VdbeJumpHere(v, 0);
115333 assert( db->nDb>0 );
115334 iDb = 0;
115335 do{
115336 Schema *pSchema;
115337 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115338 sqlite3VdbeUsesBtree(v, iDb);
115339 pSchema = db->aDb[iDb].pSchema;
115340 sqlite3VdbeAddOp4Int(v,
115341 OP_Transaction, /* Opcode */
115342 iDb, /* P1 */
115343 DbMaskTest(pParse->writeMask,iDb), /* P2 */
115344 pSchema->schema_cookie, /* P3 */
115345 pSchema->iGeneration /* P4 */
115346 );
115347 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115348 VdbeComment((v,
115349 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115350 }while( ++iDb<db->nDb );
115351 #ifndef SQLITE_OMIT_VIRTUALTABLE
115352 for(i=0; i<pParse->nVtabLock; i++){
115353 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115354 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
115355 }
115356 pParse->nVtabLock = 0;
115357 #endif
115358
115359 /* Once all the cookies have been verified and transactions opened,
115360 ** obtain the required table-locks. This is a no-op unless the
115361 ** shared-cache feature is enabled.
115362 */
115363 codeTableLocks(pParse);
115364
115365 /* Initialize any AUTOINCREMENT data structures required.
115366 */
115367 sqlite3AutoincrementBegin(pParse);
115368
115369 /* Code constant expressions that where factored out of inner loops.
115370 **
115371 ** The pConstExpr list might also contain expressions that we simply
115372 ** want to keep around until the Parse object is deleted. Such
115373 ** expressions have iConstExprReg==0. Do not generate code for
115374 ** those expressions, of course.
115375 */
115376 if( pParse->pConstExpr ){
115377 ExprList *pEL = pParse->pConstExpr;
115378 pParse->okConstFactor = 0;
115379 for(i=0; i<pEL->nExpr; i++){
115380 int iReg = pEL->a[i].u.iConstExprReg;
115381 if( iReg>0 ){
115382 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115383 }
115384 }
115385 }
115386
115387 if( pParse->bReturning ){
115388 Returning *pRet = pParse->u1.pReturning;
115389 if( pRet->nRetCol ){
115390 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115391 }
115392 }
115393
115394 /* Finally, jump back to the beginning of the executable code. */
115395 sqlite3VdbeGoto(v, 1);
115396 }
115397 }
115398
115399 /* Get the VDBE program ready for execution
115400 */
115401 assert( v!=0 || pParse->nErr );
@@ -115898,20 +116139,21 @@
115898 */
115899 SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
115900 int i;
115901 Column *pCol;
115902 assert( pTable!=0 );
 
115903 if( (pCol = pTable->aCol)!=0 ){
115904 for(i=0; i<pTable->nCol; i++, pCol++){
115905 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
115906 sqlite3DbFree(db, pCol->zCnName);
115907 }
115908 sqlite3DbFree(db, pTable->aCol);
115909 if( IsOrdinaryTable(pTable) ){
115910 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
115911 }
115912 if( db==0 || db->pnBytesFreed==0 ){
115913 pTable->aCol = 0;
115914 pTable->nCol = 0;
115915 if( IsOrdinaryTable(pTable) ){
115916 pTable->u.tab.pDfltList = 0;
115917 }
@@ -115944,21 +116186,22 @@
115944 **
115945 ** If malloc has already failed, it may be that it failed while allocating
115946 ** a Table object that was going to be marked ephemeral. So do not check
115947 ** that no lookaside memory is used in this case either. */
115948 int nLookaside = 0;
115949 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
 
115950 nLookaside = sqlite3LookasideUsed(db, 0);
115951 }
115952 #endif
115953
115954 /* Delete all indices associated with this table. */
115955 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
115956 pNext = pIndex->pNext;
115957 assert( pIndex->pSchema==pTable->pSchema
115958 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
115959 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
115960 char *zName = pIndex->zName;
115961 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
115962 &pIndex->pSchema->idxHash, zName, 0
115963 );
115964 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
@@ -115991,12 +116234,13 @@
115991 /* Verify that no lookaside memory was used by schema tables */
115992 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
115993 }
115994 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
115995 /* Do not delete the table until the reference count reaches zero. */
 
115996 if( !pTable ) return;
115997 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
115998 deleteTable(db, pTable);
115999 }
116000
116001
116002 /*
@@ -118165,11 +118409,11 @@
118165 /*
118166 ** The Table structure pTable is really a VIEW. Fill in the names of
118167 ** the columns of the view in the pTable structure. Return the number
118168 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118169 */
118170 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
118171 Table *pSelTab; /* A fake table from which we get the result set */
118172 Select *pSel; /* Copy of the SELECT that implements the view */
118173 int nErr = 0; /* Number of errors encountered */
118174 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118175 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -118190,13 +118434,14 @@
118190 }
118191 #endif
118192
118193 #ifndef SQLITE_OMIT_VIEW
118194 /* A positive nCol means the columns names for this view are
118195 ** already known.
 
118196 */
118197 if( pTable->nCol>0 ) return 0;
118198
118199 /* A negative nCol is a special marker meaning that we are currently
118200 ** trying to compute the column names. If we enter this routine with
118201 ** a negative nCol, it means two or more views form a loop, like this:
118202 **
@@ -118287,10 +118532,15 @@
118287 if( db->mallocFailed ){
118288 sqlite3DeleteColumnNames(db, pTable);
118289 }
118290 #endif /* SQLITE_OMIT_VIEW */
118291 return nErr;
 
 
 
 
 
118292 }
118293 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
118294
118295 #ifndef SQLITE_OMIT_VIEW
118296 /*
@@ -119153,11 +119403,11 @@
119153 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119154 goto exit_create_index;
119155 }
119156 if( !IN_RENAME_OBJECT ){
119157 if( !db->init.busy ){
119158 if( sqlite3FindTable(db, zName, 0)!=0 ){
119159 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
119160 goto exit_create_index;
119161 }
119162 }
119163 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
@@ -119806,16 +120056,17 @@
119806 /*
119807 ** Delete an IdList.
119808 */
119809 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
119810 int i;
 
119811 if( pList==0 ) return;
119812 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
119813 for(i=0; i<pList->nId; i++){
119814 sqlite3DbFree(db, pList->a[i].zName);
119815 }
119816 sqlite3DbFreeNN(db, pList);
119817 }
119818
119819 /*
119820 ** Return the index in pList of the identifier named zId. Return -1
119821 ** if not found.
@@ -120014,15 +120265,16 @@
120014 ** Delete an entire SrcList including all its substructure.
120015 */
120016 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
120017 int i;
120018 SrcItem *pItem;
 
120019 if( pList==0 ) return;
120020 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120021 if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
120022 sqlite3DbFree(db, pItem->zName);
120023 if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
120024 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120025 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120026 sqlite3DeleteTable(db, pItem->pTab);
120027 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120028 if( pItem->fg.isUsing ){
@@ -120029,11 +120281,11 @@
120029 sqlite3IdListDelete(db, pItem->u3.pUsing);
120030 }else if( pItem->u3.pOn ){
120031 sqlite3ExprDelete(db, pItem->u3.pOn);
120032 }
120033 }
120034 sqlite3DbFreeNN(db, pList);
120035 }
120036
120037 /*
120038 ** This routine is called by the parser to add a new term to the
120039 ** end of a growing FROM clause. The "p" parameter is the part of
@@ -121281,23 +121533,25 @@
121281 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
121282 Hash temp1;
121283 Hash temp2;
121284 HashElem *pElem;
121285 Schema *pSchema = (Schema *)p;
 
121286
 
121287 temp1 = pSchema->tblHash;
121288 temp2 = pSchema->trigHash;
121289 sqlite3HashInit(&pSchema->trigHash);
121290 sqlite3HashClear(&pSchema->idxHash);
121291 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
121292 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
121293 }
121294 sqlite3HashClear(&temp2);
121295 sqlite3HashInit(&pSchema->tblHash);
121296 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
121297 Table *pTab = sqliteHashData(pElem);
121298 sqlite3DeleteTable(0, pTab);
121299 }
121300 sqlite3HashClear(&temp1);
121301 sqlite3HashClear(&pSchema->fkeyHash);
121302 pSchema->pSeqTab = 0;
121303 if( pSchema->schemaFlags & DB_SchemaLoaded ){
@@ -121392,22 +121646,46 @@
121392 ** A table is read-only if any of the following are true:
121393 **
121394 ** 1) It is a virtual table and no implementation of the xUpdate method
121395 ** has been provided
121396 **
121397 ** 2) It is a system table (i.e. sqlite_schema), this call is not
 
 
 
 
121398 ** part of a nested parse and writable_schema pragma has not
121399 ** been specified
121400 **
121401 ** 3) The table is a shadow table, the database connection is in
121402 ** defensive mode, and the current sqlite3_prepare()
121403 ** is for a top-level SQL statement.
121404 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121405 static int tabIsReadOnly(Parse *pParse, Table *pTab){
121406 sqlite3 *db;
121407 if( IsVirtual(pTab) ){
121408 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
121409 }
121410 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
121411 db = pParse->db;
121412 if( (pTab->tabFlags & TF_Readonly)!=0 ){
121413 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
@@ -121415,13 +121693,15 @@
121415 assert( pTab->tabFlags & TF_Shadow );
121416 return sqlite3ReadOnlyShadowTables(db);
121417 }
121418
121419 /*
121420 ** Check to make sure the given table is writable. If it is not
121421 ** writable, generate an error message and return 1. If it is
121422 ** writable return 0;
 
 
121423 */
121424 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
121425 if( tabIsReadOnly(pParse, pTab) ){
121426 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121427 return 1;
@@ -121778,13 +122058,14 @@
121778 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
121779 pTab->zName, P4_STATIC);
121780 }
121781 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121782 assert( pIdx->pSchema==pTab->pSchema );
121783 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
121784 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
121785 sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
 
 
121786 }
121787 }
121788 }else
121789 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
121790 {
@@ -121980,11 +122261,11 @@
121980 sqlite3ExprDelete(db, pWhere);
121981 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
121982 sqlite3ExprListDelete(db, pOrderBy);
121983 sqlite3ExprDelete(db, pLimit);
121984 #endif
121985 sqlite3DbFree(db, aToOpen);
121986 return;
121987 }
121988 /* Make sure "isView" and other macros defined above are undefined. Otherwise
121989 ** they may interfere with compilation of other functions in this file
121990 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -126148,15 +126429,16 @@
126148 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
126149 FKey *pFKey; /* Iterator variable */
126150 FKey *pNext; /* Copy of pFKey->pNextFrom */
126151
126152 assert( IsOrdinaryTable(pTab) );
 
126153 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126154 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
126155
126156 /* Remove the FK from the fkeyHash hash table. */
126157 if( !db || db->pnBytesFreed==0 ){
126158 if( pFKey->pPrevTo ){
126159 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126160 }else{
126161 void *p = (void *)pFKey->pNextTo;
126162 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
@@ -126345,11 +126627,11 @@
126345 /* Move the previous opcode (which should be OP_MakeRecord) forward
126346 ** by one slot and insert a new OP_TypeCheck where the current
126347 ** OP_MakeRecord is found */
126348 VdbeOp *pPrev;
126349 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
126350 pPrev = sqlite3VdbeGetOp(v, -1);
126351 assert( pPrev!=0 );
126352 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
126353 pPrev->opcode = OP_TypeCheck;
126354 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
126355 }else{
@@ -126383,11 +126665,11 @@
126383 i = sqlite3Strlen30NN(zColAff);
126384 if( i ){
126385 if( iReg ){
126386 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
126387 }else{
126388 assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord
126389 || sqlite3VdbeDb(v)->mallocFailed );
126390 sqlite3VdbeChangeP4(v, -1, zColAff, i);
126391 }
126392 }
126393 }
@@ -126469,11 +126751,11 @@
126469 /* Before computing generated columns, first go through and make sure
126470 ** that appropriate affinity has been applied to the regular columns
126471 */
126472 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126473 if( (pTab->tabFlags & TF_HasStored)!=0 ){
126474 pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
126475 if( pOp->opcode==OP_Affinity ){
126476 /* Change the OP_Affinity argument to '@' (NONE) for all stored
126477 ** columns. '@' is the no-op affinity and those columns have not
126478 ** yet been computed. */
126479 int ii, jj;
@@ -127375,11 +127657,16 @@
127375 }else if( pSelect ){
127376 if( regFromSelect!=regData ){
127377 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
127378 }
127379 }else{
127380 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
 
 
 
 
 
127381 }
127382 }
127383
127384
127385 /* Run the BEFORE and INSTEAD OF triggers, if there are any
@@ -127512,11 +127799,13 @@
127512 int isReplace = 0;/* Set to true if constraints may cause a replace */
127513 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
127514 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
127515 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
127516 );
127517 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
 
 
127518
127519 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
127520 ** constraints or (b) there are no triggers and this table is not a
127521 ** parent table in a foreign key constraint. It is safe to set the
127522 ** flag in the second case as if any REPLACE constraint is hit, an
@@ -127596,11 +127885,11 @@
127596 sqlite3SrcListDelete(db, pTabList);
127597 sqlite3ExprListDelete(db, pList);
127598 sqlite3UpsertDelete(db, pUpsert);
127599 sqlite3SelectDelete(db, pSelect);
127600 sqlite3IdListDelete(db, pColumn);
127601 sqlite3DbFree(db, aRegIdx);
127602 }
127603
127604 /* Make sure "isView" and other macros defined above are undefined. Otherwise
127605 ** they may interfere with compilation of other functions in this file
127606 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -132702,19 +132991,21 @@
132702 ** Setting to a null string reverts to the default temporary directory search.
132703 ** If temporary directory is changed, then invalidateTempStorage.
132704 **
132705 */
132706 case PragTyp_TEMP_STORE_DIRECTORY: {
 
132707 if( !zRight ){
132708 returnSingleText(v, sqlite3_temp_directory);
132709 }else{
132710 #ifndef SQLITE_OMIT_WSD
132711 if( zRight[0] ){
132712 int res;
132713 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132714 if( rc!=SQLITE_OK || res==0 ){
132715 sqlite3ErrorMsg(pParse, "not a writable directory");
 
132716 goto pragma_out;
132717 }
132718 }
132719 if( SQLITE_TEMP_STORE==0
132720 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
@@ -132728,10 +133019,11 @@
132728 }else{
132729 sqlite3_temp_directory = 0;
132730 }
132731 #endif /* SQLITE_OMIT_WSD */
132732 }
 
132733 break;
132734 }
132735
132736 #if SQLITE_OS_WIN
132737 /*
@@ -132746,19 +133038,21 @@
132746 ** process. Database file specified with an absolute path are not impacted
132747 ** by this setting, regardless of its value.
132748 **
132749 */
132750 case PragTyp_DATA_STORE_DIRECTORY: {
 
132751 if( !zRight ){
132752 returnSingleText(v, sqlite3_data_directory);
132753 }else{
132754 #ifndef SQLITE_OMIT_WSD
132755 if( zRight[0] ){
132756 int res;
132757 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132758 if( rc!=SQLITE_OK || res==0 ){
132759 sqlite3ErrorMsg(pParse, "not a writable directory");
 
132760 goto pragma_out;
132761 }
132762 }
132763 sqlite3_free(sqlite3_data_directory);
132764 if( zRight[0] ){
@@ -132766,10 +133060,11 @@
132766 }else{
132767 sqlite3_data_directory = 0;
132768 }
132769 #endif /* SQLITE_OMIT_WSD */
132770 }
 
132771 break;
132772 }
132773 #endif
132774
132775 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -133479,19 +133774,27 @@
133479 /* Make sure all the indices are constructed correctly.
133480 */
133481 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
133482 Table *pTab = sqliteHashData(x);
133483 Index *pIdx, *pPk;
133484 Index *pPrior = 0;
133485 int loopTop;
133486 int iDataCur, iIdxCur;
133487 int r1 = -1;
133488 int bStrict;
 
133489
133490 if( !IsOrdinaryTable(pTab) ) continue;
133491 if( pObjTab && pObjTab!=pTab ) continue;
133492 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
 
 
 
 
 
 
 
133493 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
133494 1, 0, &iDataCur, &iIdxCur);
133495 /* reg[7] counts the number of entries in the table.
133496 ** reg[8+i] counts the number of entries in the i-th index
133497 */
@@ -133506,10 +133809,28 @@
133506 if( !isQuick ){
133507 /* Sanity check on record header decoding */
133508 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133509 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133510 VdbeComment((v, "(right-most column)"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133511 }
133512 /* Verify that all NOT NULL columns really are NOT NULL. At the
133513 ** same time verify the type of the content of STRICT tables */
133514 bStrict = (pTab->tabFlags & TF_Strict)!=0;
133515 for(j=0; j<pTab->nCol; j++){
@@ -133518,11 +133839,11 @@
133518 int doError, jmp2;
133519 if( j==pTab->iPKey ) continue;
133520 if( pCol->notNull==0 && !bStrict ) continue;
133521 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133522 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133523 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
133524 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133525 }
133526 if( pCol->notNull ){
133527 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
133528 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
@@ -133533,13 +133854,11 @@
133533 }else{
133534 integrityCheckResultRow(v);
133535 }
133536 sqlite3VdbeJumpHere(v, jmp2);
133537 }
133538 if( (pTab->tabFlags & TF_Strict)!=0
133539 && pCol->eCType!=COLTYPE_ANY
133540 ){
133541 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133542 sqlite3StdTypeMap[pCol->eCType-1]);
133543 VdbeCoverage(v);
133544 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133545 sqlite3StdType[pCol->eCType-1],
@@ -133634,10 +133953,13 @@
133634 sqlite3VdbeLoadString(v, 4, pIdx->zName);
133635 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
133636 integrityCheckResultRow(v);
133637 sqlite3VdbeJumpHere(v, addr);
133638 }
 
 
 
133639 }
133640 }
133641 }
133642 {
133643 static const int iLn = VDBE_OFFSET_LINENO(2);
@@ -135032,19 +135354,19 @@
135032 sqlite3 *db = pParse->db;
135033 assert( db!=0 );
135034 assert( db->pParse==pParse );
135035 assert( pParse->nested==0 );
135036 #ifndef SQLITE_OMIT_SHARED_CACHE
135037 sqlite3DbFree(db, pParse->aTableLock);
135038 #endif
135039 while( pParse->pCleanup ){
135040 ParseCleanup *pCleanup = pParse->pCleanup;
135041 pParse->pCleanup = pCleanup->pNext;
135042 pCleanup->xCleanup(db, pCleanup->pPtr);
135043 sqlite3DbFreeNN(db, pCleanup);
135044 }
135045 sqlite3DbFree(db, pParse->aLabel);
135046 if( pParse->pConstExpr ){
135047 sqlite3ExprListDelete(db, pParse->pConstExpr);
135048 }
135049 assert( db->lookaside.bDisable >= pParse->disableLookaside );
135050 db->lookaside.bDisable -= pParse->disableLookaside;
@@ -135599,10 +135921,11 @@
135599 **
135600 ** If bFree==1, call sqlite3DbFree() on the p object.
135601 ** If bFree==0, Leave the first Select object unfreed
135602 */
135603 static void clearSelect(sqlite3 *db, Select *p, int bFree){
 
135604 while( p ){
135605 Select *pPrior = p->pPrior;
135606 sqlite3ExprListDelete(db, p->pEList);
135607 sqlite3SrcListDelete(db, p->pSrc);
135608 sqlite3ExprDelete(db, p->pWhere);
@@ -135618,11 +135941,11 @@
135618 while( p->pWin ){
135619 assert( p->pWin->ppThis==&p->pWin );
135620 sqlite3WindowUnlinkFromSelect(p->pWin);
135621 }
135622 #endif
135623 if( bFree ) sqlite3DbFreeNN(db, p);
135624 p = pPrior;
135625 bFree = 1;
135626 }
135627 }
135628
@@ -137024,13 +137347,14 @@
137024 /*
137025 ** Deallocate a KeyInfo object
137026 */
137027 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
137028 if( p ){
 
137029 assert( p->nRef>0 );
137030 p->nRef--;
137031 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
137032 }
137033 }
137034
137035 /*
137036 ** Make a new pointer to a KeyInfo object
@@ -137211,18 +137535,21 @@
137211 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
137212 nKey+1+nColumn+nRefKey);
137213 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137214 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
137215 VdbeCoverage(v);
137216 codeOffset(v, p->iOffset, addrContinue);
137217 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
137218 bSeq = 0;
137219 }else{
137220 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
137221 codeOffset(v, p->iOffset, addrContinue);
137222 iSortTab = iTab;
137223 bSeq = 1;
 
 
 
137224 }
137225 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137226 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
137227 if( aOutEx[i].fg.bSorterRef ) continue;
137228 #endif
@@ -137343,13 +137670,10 @@
137343
137344 /*
137345 ** Return a pointer to a string containing the 'declaration type' of the
137346 ** expression pExpr. The string may be treated as static by the caller.
137347 **
137348 ** Also try to estimate the size of the returned value and return that
137349 ** result in *pEstWidth.
137350 **
137351 ** The declaration type is the exact datatype definition extracted from the
137352 ** original CREATE TABLE statement if the expression is a column. The
137353 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
137354 ** is considered a column can be complex in the presence of subqueries. The
137355 ** result-set expression in all of the following SELECT statements is
@@ -139211,14 +139535,15 @@
139211
139212 /* Jump to the this point in order to terminate the query.
139213 */
139214 sqlite3VdbeResolveLabel(v, labelEnd);
139215
139216 /* Reassembly the compound query so that it will be freed correctly
139217 ** by the calling function */
139218 if( pSplit->pPrior ){
139219 sqlite3SelectDelete(db, pSplit->pPrior);
 
139220 }
139221 pSplit->pPrior = pPrior;
139222 pPrior->pNext = pSplit;
139223 sqlite3ExprListDelete(db, pPrior->pOrderBy);
139224 pPrior->pOrderBy = 0;
@@ -139324,10 +139649,11 @@
139324 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
139325 memset(&ifNullRow, 0, sizeof(ifNullRow));
139326 ifNullRow.op = TK_IF_NULL_ROW;
139327 ifNullRow.pLeft = pCopy;
139328 ifNullRow.iTable = pSubst->iNewTable;
 
139329 ifNullRow.flags = EP_IfNullRow;
139330 pCopy = &ifNullRow;
139331 }
139332 testcase( ExprHasProperty(pCopy, EP_Subquery) );
139333 pNew = sqlite3ExprDup(db, pCopy, 0);
@@ -139591,11 +139917,12 @@
139591 **
139592 ** (3) If the subquery is the right operand of a LEFT JOIN then
139593 ** (3a) the subquery may not be a join and
139594 ** (3b) the FROM clause of the subquery may not contain a virtual
139595 ** table and
139596 ** (3c) the outer query may not be an aggregate.
 
139597 ** (3d) the outer query may not be DISTINCT.
139598 ** See also (26) for restrictions on RIGHT JOIN.
139599 **
139600 ** (4) The subquery can not be DISTINCT.
139601 **
@@ -139803,20 +140130,14 @@
139803 **
139804 ** (t1 LEFT OUTER JOIN t2) JOIN t3
139805 **
139806 ** which is not at all the same thing.
139807 **
139808 ** If the subquery is the right operand of a LEFT JOIN, then the outer
139809 ** query cannot be an aggregate. (3c) This is an artifact of the way
139810 ** aggregates are processed - there is no mechanism to determine if
139811 ** the LEFT JOIN table should be all-NULL.
139812 **
139813 ** See also tickets #306, #350, and #3300.
139814 */
139815 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
139816 if( pSubSrc->nSrc>1 /* (3a) */
139817 || isAgg /* (3c) */
139818 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
139819 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
139820 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
139821 ){
139822 return 0;
@@ -140733,10 +141054,11 @@
140733 if( p->pWhere
140734 || p->pEList->nExpr!=1
140735 || p->pSrc->nSrc!=1
140736 || p->pSrc->a[0].pSelect
140737 || pAggInfo->nFunc!=1
 
140738 ){
140739 return 0;
140740 }
140741 pTab = p->pSrc->a[0].pTab;
140742 assert( pTab!=0 );
@@ -142726,11 +143048,11 @@
142726 */
142727 iEnd = sqlite3VdbeMakeLabel(pParse);
142728 if( (p->selFlags & SF_FixedLimit)==0 ){
142729 p->nSelectRow = 320; /* 4 billion rows */
142730 }
142731 computeLimitRegisters(pParse, p, iEnd);
142732 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
142733 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
142734 sSort.sortFlags |= SORTFLAG_UseSorter;
142735 }
142736
@@ -142948,12 +143270,17 @@
142948 if( minMaxFlag ){
142949 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
142950 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
142951 }
142952 for(ii=0; ii<pAggInfo->nColumn; ii++){
142953 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
142954 ii, pAggInfo->aCol[ii].iMem);
 
 
 
 
 
142955 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
142956 }
142957 for(ii=0; ii<pAggInfo->nFunc; ii++){
142958 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
142959 ii, pAggInfo->aFunc[ii].iMem);
@@ -143070,19 +143397,19 @@
143070 }
143071 }
143072 regBase = sqlite3GetTempRange(pParse, nCol);
143073 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
143074 j = nGroupBy;
 
143075 for(i=0; i<pAggInfo->nColumn; i++){
143076 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143077 if( pCol->iSorterColumn>=j ){
143078 int r1 = j + regBase;
143079 sqlite3ExprCodeGetColumnOfTable(v,
143080 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
143081 j++;
143082 }
143083 }
 
143084 regRecord = sqlite3GetTempReg(pParse);
143085 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
143086 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
143087 sqlite3ReleaseTempReg(pParse, regRecord);
143088 sqlite3ReleaseTempRange(pParse, regBase, nCol);
@@ -147496,11 +147823,12 @@
147496 ** in the list are moved to the sqlite3.pDisconnect list of the associated
147497 ** database connection.
147498 */
147499 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
147500 assert( IsVirtual(p) );
147501 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
 
147502 if( p->u.vtab.azArg ){
147503 int i;
147504 for(i=0; i<p->u.vtab.nArg; i++){
147505 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147506 }
@@ -149173,10 +149501,11 @@
149173 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
149174 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
149175 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
149176 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
149177 #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
 
149178
149179 #endif /* !defined(SQLITE_WHEREINT_H) */
149180
149181 /************** End of whereInt.h ********************************************/
149182 /************** Continuing where we left off in wherecode.c ******************/
@@ -149781,11 +150110,12 @@
149781 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap,&iTab);
149782 pExpr->iTable = iTab;
149783 }
149784 sqlite3ExprDelete(db, pX);
149785 }else{
149786 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
 
149787 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
149788 }
149789 pX = pExpr;
149790 }
149791
@@ -150051,11 +150381,11 @@
150051 WhereTerm *pTerm /* The upper or lower bound just coded */
150052 ){
150053 if( pTerm->wtFlags & TERM_LIKEOPT ){
150054 VdbeOp *pOp;
150055 assert( pLevel->iLikeRepCntr>0 );
150056 pOp = sqlite3VdbeGetOp(v, -1);
150057 assert( pOp!=0 );
150058 assert( pOp->opcode==OP_String8
150059 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
150060 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150061 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
@@ -151267,12 +151597,12 @@
151267 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
151268 endEq = 0;
151269 }
151270 nConstraint++;
151271 }
151272 sqlite3DbFree(db, zStartAff);
151273 sqlite3DbFree(db, zEndAff);
151274
151275 /* Top of the loop body */
151276 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151277
151278 /* Check if the index cursor is past the end of the range. */
@@ -155499,11 +155829,11 @@
155499 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
155500 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155501 ** is larger than all samples in the array. */
155502 tRowcnt iUpper, iGap;
155503 if( i>=pIdx->nSample ){
155504 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
155505 }else{
155506 iUpper = aSample[i].anLt[iCol];
155507 }
155508
155509 if( iLower>=iUpper ){
@@ -156128,16 +156458,22 @@
156128 }
156129 }
156130 }
156131
156132 /*
156133 ** Deallocate internal memory used by a WhereLoop object
 
156134 */
156135 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
156136 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
 
 
 
 
156137 whereLoopClearUnion(db, p);
156138 whereLoopInit(p);
 
156139 }
156140
156141 /*
156142 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156143 */
@@ -156157,11 +156493,13 @@
156157 /*
156158 ** Transfer content from the second pLoop into the first.
156159 */
156160 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
156161 whereLoopClearUnion(db, pTo);
156162 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
 
 
156163 memset(pTo, 0, WHERE_LOOP_XFER_SZ);
156164 return SQLITE_NOMEM_BKPT;
156165 }
156166 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
156167 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
@@ -156175,32 +156513,34 @@
156175
156176 /*
156177 ** Delete a WhereLoop object
156178 */
156179 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
 
156180 whereLoopClear(db, p);
156181 sqlite3DbFreeNN(db, p);
156182 }
156183
156184 /*
156185 ** Free a WhereInfo structure
156186 */
156187 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
156188 assert( pWInfo!=0 );
 
156189 sqlite3WhereClauseClear(&pWInfo->sWC);
156190 while( pWInfo->pLoops ){
156191 WhereLoop *p = pWInfo->pLoops;
156192 pWInfo->pLoops = p->pNextLoop;
156193 whereLoopDelete(db, p);
156194 }
156195 assert( pWInfo->pExprMods==0 );
156196 while( pWInfo->pMemToFree ){
156197 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156198 sqlite3DbFreeNN(db, pWInfo->pMemToFree);
156199 pWInfo->pMemToFree = pNext;
156200 }
156201 sqlite3DbFreeNN(db, pWInfo);
156202 }
156203
156204 /* Undo all Expr node modifications
156205 */
156206 static void whereUndoExprMods(WhereInfo *pWInfo){
@@ -156810,11 +157150,15 @@
156810 pNew->wsFlags = saved_wsFlags;
156811 pNew->u.btree.nEq = saved_nEq;
156812 pNew->u.btree.nBtm = saved_nBtm;
156813 pNew->u.btree.nTop = saved_nTop;
156814 pNew->nLTerm = saved_nLTerm;
156815 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
 
 
 
 
156816 pNew->aLTerm[pNew->nLTerm++] = pTerm;
156817 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
156818
156819 assert( nInMul==0
156820 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
@@ -156903,42 +157247,43 @@
156903 }
156904 }
156905 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
156906 }else if( eOp & WO_ISNULL ){
156907 pNew->wsFlags |= WHERE_COLUMN_NULL;
156908 }else if( eOp & (WO_GT|WO_GE) ){
156909 testcase( eOp & WO_GT );
156910 testcase( eOp & WO_GE );
156911 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
156912 pNew->u.btree.nBtm = whereRangeVectorLen(
156913 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156914 );
156915 pBtm = pTerm;
156916 pTop = 0;
156917 if( pTerm->wtFlags & TERM_LIKEOPT ){
156918 /* Range constraints that come from the LIKE optimization are
156919 ** always used in pairs. */
156920 pTop = &pTerm[1];
156921 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
156922 assert( pTop->wtFlags & TERM_LIKEOPT );
156923 assert( pTop->eOperator==WO_LT );
156924 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156925 pNew->aLTerm[pNew->nLTerm++] = pTop;
156926 pNew->wsFlags |= WHERE_TOP_LIMIT;
156927 pNew->u.btree.nTop = 1;
156928 }
156929 }else{
156930 assert( eOp & (WO_LT|WO_LE) );
156931 testcase( eOp & WO_LT );
156932 testcase( eOp & WO_LE );
156933 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
156934 pNew->u.btree.nTop = whereRangeVectorLen(
156935 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156936 );
156937 pTop = pTerm;
156938 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
156939 pNew->aLTerm[pNew->nLTerm-2] : 0;
 
156940 }
156941
156942 /* At this point pNew->nOut is set to the number of rows expected to
156943 ** be visited by the index scan before considering term pTerm, or the
156944 ** values of nIn and nInMul. In other words, assuming that all
@@ -157380,10 +157725,13 @@
157380 #ifdef SQLITE_ENABLE_STAT4
157381 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
157382 #else
157383 pNew->rRun = rSize + 16;
157384 #endif
 
 
 
157385 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157386 whereLoopOutputAdjust(pWC, pNew, rSize);
157387 rc = whereLoopInsert(pBuilder, pNew);
157388 pNew->nOut = rSize;
157389 if( rc ) break;
@@ -158106,11 +158454,17 @@
158106 WhereLoop *pNew;
158107
158108
158109 /* Loop over the tables in the join, from left to right */
158110 pNew = pBuilder->pNew;
158111 whereLoopInit(pNew);
 
 
 
 
 
 
158112 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
158113 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158114 Bitmask mUnusable = 0;
158115 pNew->iTab = iTab;
158116 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
@@ -158703,13 +159057,13 @@
158703 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
158704 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
158705 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
158706 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
158707 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
158708 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
158709 Bitmask maskNew; /* Mask of src visited by (..) */
158710 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
158711
158712 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
158713 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
158714 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
158715 /* Do not use an automatic index if the this loop is expected
@@ -158724,11 +159078,13 @@
158724 ** Compute its cost */
158725 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
158726 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
158727 nOut = pFrom->nRow + pWLoop->nOut;
158728 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
 
158729 if( isOrdered<0 ){
 
158730 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
158731 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
158732 iLoop, pWLoop, &revMask);
158733 }else{
158734 revMask = pFrom->revLoop;
@@ -158751,10 +159107,17 @@
158751 rUnsorted, rCost));
158752 }else{
158753 rCost = rUnsorted;
158754 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
158755 }
 
 
 
 
 
 
 
158756
158757 /* Check to see if pWLoop should be added to the set of
158758 ** mxChoice best-so-far paths.
158759 **
158760 ** First look for an existing path among best-so-far paths
@@ -158984,11 +159347,12 @@
158984
158985
158986 pWInfo->nRowOut = pFrom->nRow;
158987
158988 /* Free temporary memory and return success */
158989 sqlite3DbFreeNN(db, pSpace);
 
158990 return SQLITE_OK;
158991 }
158992
158993 /*
158994 ** Most queries use only a single table (they are not joins) and have
@@ -161217,11 +161581,10 @@
161217 int i;
161218 int nInit = pList ? pList->nExpr : 0;
161219 for(i=0; i<pAppend->nExpr; i++){
161220 sqlite3 *db = pParse->db;
161221 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
161222 assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) );
161223 if( db->mallocFailed ){
161224 sqlite3ExprDelete(db, pDup);
161225 break;
161226 }
161227 if( bIntToNull ){
@@ -162488,14 +162851,13 @@
162488 }
162489 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone);
162490
162491 /* This block runs if reg1 is not NULL, but reg2 is. */
162492 sqlite3VdbeJumpHere(v, addr);
162493 sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); VdbeCoverage(v);
162494 if( op==OP_Gt || op==OP_Ge ){
162495 sqlite3VdbeChangeP2(v, -1, addrDone);
162496 }
162497 }
162498
162499 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162500 ** This block adds (or subtracts for DESC) the numeric value in regVal
162501 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
@@ -170068,11 +170430,11 @@
170068 sqlite3DeleteTable(db, pParse->pNewTable);
170069 }
170070 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
170071 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170072 }
170073 if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList);
170074 db->pParse = pParentParse;
170075 assert( nErr==0 || pParse->rc!=SQLITE_OK );
170076 return nErr;
170077 }
170078
@@ -171424,22 +171786,23 @@
171424 db->lookaside.pEnd = p;
171425 db->lookaside.bDisable = 0;
171426 db->lookaside.bMalloced = pBuf==0 ?1:0;
171427 db->lookaside.nSlot = nBig+nSm;
171428 }else{
171429 db->lookaside.pStart = db;
171430 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171431 db->lookaside.pSmallInit = 0;
171432 db->lookaside.pSmallFree = 0;
171433 db->lookaside.pMiddle = db;
171434 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171435 db->lookaside.pEnd = db;
171436 db->lookaside.bDisable = 1;
171437 db->lookaside.sz = 0;
171438 db->lookaside.bMalloced = 0;
171439 db->lookaside.nSlot = 0;
171440 }
 
171441 assert( sqlite3LookasideUsed(db,0)==0 );
171442 #endif /* SQLITE_OMIT_LOOKASIDE */
171443 return SQLITE_OK;
171444 }
171445
@@ -171514,10 +171877,11 @@
171514 ** Configuration settings for an individual database connection
171515 */
171516 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
171517 va_list ap;
171518 int rc;
 
171519 va_start(ap, op);
171520 switch( op ){
171521 case SQLITE_DBCONFIG_MAINDBNAME: {
171522 /* IMP: R-06824-28531 */
171523 /* IMP: R-36257-52125 */
@@ -171579,10 +171943,11 @@
171579 }
171580 break;
171581 }
171582 }
171583 va_end(ap);
 
171584 return rc;
171585 }
171586
171587 /*
171588 ** This is the default collating function named "BINARY" which is always
@@ -181118,11 +181483,11 @@
181118 p1 = pPhrase->doclist.pList;
181119 p2 = aPoslist;
181120 nDistance = iPrev - nMaxUndeferred;
181121 }
181122
181123 aOut = (char *)sqlite3_malloc(nPoslist+8);
181124 if( !aOut ){
181125 sqlite3_free(aPoslist);
181126 return SQLITE_NOMEM;
181127 }
181128
@@ -204144,11 +204509,11 @@
204144 sqlite3_bind_value(pUp, 2, aData[2]);
204145 }
204146 sqlite3_free(p);
204147 nChange = 1;
204148 }
204149 for(jj=1; jj<pRtree->nAux; jj++){
204150 nChange++;
204151 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204152 }
204153 if( nChange ){
204154 sqlite3_step(pUp);
@@ -212503,15 +212868,16 @@
212503 */
212504 static int dbpageBegin(sqlite3_vtab *pVtab){
212505 DbpageTable *pTab = (DbpageTable *)pVtab;
212506 sqlite3 *db = pTab->db;
212507 int i;
212508 for(i=0; i<db->nDb; i++){
 
212509 Btree *pBt = db->aDb[i].pBt;
212510 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
212511 }
212512 return SQLITE_OK;
212513 }
212514
212515
212516 /*
212517 ** Invoke this routine to register the "dbpage" virtual table module
@@ -219231,11 +219597,11 @@
219231 static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
219232
219233 static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
219234
219235 #define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
219236 #define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
219237 #define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
219238 #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
219239 #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
219240
219241 #define fts5BufferGrow(pRc,pBuf,nn) ( \
@@ -231108,11 +231474,13 @@
231108 /* Write the rowid. */
231109 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
231110 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
231111 }else{
231112 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
231113 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
 
 
231114 }
231115 pWriter->iPrevRowid = iRowid;
231116 pWriter->bFirstRowidInDoclist = 0;
231117 pWriter->bFirstRowidInPage = 0;
231118 }
@@ -231872,21 +232240,21 @@
231872 return fts5IndexReturn(p);
231873 }
231874
231875 static void fts5AppendRowid(
231876 Fts5Index *p,
231877 i64 iDelta,
231878 Fts5Iter *pUnused,
231879 Fts5Buffer *pBuf
231880 ){
231881 UNUSED_PARAM(pUnused);
231882 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
231883 }
231884
231885 static void fts5AppendPoslist(
231886 Fts5Index *p,
231887 i64 iDelta,
231888 Fts5Iter *pMulti,
231889 Fts5Buffer *pBuf
231890 ){
231891 int nData = pMulti->base.nData;
231892 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
@@ -231957,14 +232325,14 @@
231957 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
231958 *piLastRowid = iRowid;
231959 }
231960 #endif
231961
231962 #define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
231963 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
231964 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
231965 (iLastRowid) = (iRowid); \
231966 }
231967
231968 /*
231969 ** Swap the contents of buffer *p1 with that of *p2.
231970 */
@@ -232231,11 +232599,11 @@
232231 Fts5Buffer *aBuf;
232232 int nBuf = 32;
232233 int nMerge = 1;
232234
232235 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
232236 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
232237 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
232238 xMerge = fts5MergeRowidLists;
232239 xAppend = fts5AppendRowid;
232240 }else{
232241 nMerge = FTS5_MERGE_NLIST-1;
@@ -232270,11 +232638,11 @@
232270 fts5MultiIterNext2(p, p1, &dummy)
232271 ){
232272 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
232273 p1->xSetOutputs(p1, pSeg);
232274 if( p1->base.nData ){
232275 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232276 iLastRowid = p1->base.iRowid;
232277 }
232278 }
232279 fts5MultiIterFree(p1);
232280 }
@@ -232318,11 +232686,11 @@
232318 }
232319 }
232320 iLastRowid = 0;
232321 }
232322
232323 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232324 iLastRowid = p1->base.iRowid;
232325 }
232326
232327 assert( (nBuf%nMerge)==0 );
232328 for(i=0; i<nBuf; i+=nMerge){
@@ -236634,11 +237002,11 @@
236634 int nArg, /* Number of args */
236635 sqlite3_value **apUnused /* Function arguments */
236636 ){
236637 assert( nArg==0 );
236638 UNUSED_PARAM2(nArg, apUnused);
236639 sqlite3_result_text(pCtx, "fts5: 2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21", -1, SQLITE_TRANSIENT);
236640 }
236641
236642 /*
236643 ** Return true if zName is the extension on one of the shadow tables used
236644 ** by this module.
236645
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.40.0. 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.
@@ -450,13 +450,13 @@
450 **
451 ** See also: [sqlite3_libversion()],
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.40.0"
456 #define SQLITE_VERSION_NUMBER 3040000
457 #define SQLITE_SOURCE_ID "2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -3728,10 +3728,13 @@
3728 **
3729 ** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3730 ** <dd>The database is opened [shared cache] enabled, overriding
3731 ** the default shared cache setting provided by
3732 ** [sqlite3_enable_shared_cache()].)^
3733 ** The [use of shared cache mode is discouraged] and hence shared cache
3734 ** capabilities may be omitted from many builds of SQLite. In such cases,
3735 ** this option is a no-op.
3736 **
3737 ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3738 ** <dd>The database is opened [shared cache] disabled, overriding
3739 ** the default shared cache setting provided by
3740 ** [sqlite3_enable_shared_cache()].)^
@@ -3743,11 +3746,11 @@
3746 ** connection as soon as the connection is created. In addition to setting
3747 ** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3748 ** to return an extended result code.</dd>
3749 **
3750 ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3751 ** <dd>The database filename is not allowed to contain a symbolic link</dd>
3752 ** </dl>)^
3753 **
3754 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
3755 ** required combinations shown above optionally combined with other
3756 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6769,11 +6772,11 @@
6772 **
6773 ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6774 ** function C that is invoked prior to each autovacuum of the database
6775 ** file. ^The callback is passed a copy of the generic data pointer (P),
6776 ** the schema-name of the attached database that is being autovacuumed,
6777 ** the size of the database file in pages, the number of free pages,
6778 ** and the number of bytes per page, respectively. The callback should
6779 ** return the number of free pages that should be removed by the
6780 ** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6781 ** ^If the value returned is greater than or equal to the number of
6782 ** free pages, then a complete autovacuum happens.
@@ -6889,10 +6892,15 @@
6892 **
6893 ** ^(This routine enables or disables the sharing of the database cache
6894 ** and schema data structures between [database connection | connections]
6895 ** to the same database. Sharing is enabled if the argument is true
6896 ** and disabled if the argument is false.)^
6897 **
6898 ** This interface is omitted if SQLite is compiled with
6899 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
6900 ** compile-time option is recommended because the
6901 ** [use of shared cache mode is discouraged].
6902 **
6903 ** ^Cache sharing is enabled and disabled for an entire process.
6904 ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6905 ** In prior versions of SQLite,
6906 ** sharing was enabled or disabled for each thread separately.
@@ -6988,11 +6996,11 @@
6996 ** ^Setting the heap limits to zero disables the heap limiter mechanism.
6997 **
6998 ** ^The soft heap limit may not be greater than the hard heap limit.
6999 ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7000 ** is invoked with a value of N that is greater than the hard heap limit,
7001 ** the soft heap limit is set to the value of the hard heap limit.
7002 ** ^The soft heap limit is automatically enabled whenever the hard heap
7003 ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7004 ** the soft heap limit is outside the range of 1..N, then the soft heap
7005 ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
7006 ** hard heap limit is enabled makes the soft heap limit equal to the
@@ -9283,11 +9291,11 @@
9291 ** sqlite3_backup_init() is called and before the corresponding call to
9292 ** sqlite3_backup_finish(). SQLite does not currently check to see
9293 ** if the application incorrectly accesses the destination [database connection]
9294 ** and so no error code is reported, but the operations may malfunction
9295 ** nevertheless. Use of the destination database connection while a
9296 ** backup is in progress might also cause a mutex deadlock.
9297 **
9298 ** If running in [shared cache mode], the application must
9299 ** guarantee that the shared cache used by the destination database
9300 ** is not accessed while the backup is running. In practice this means
9301 ** that the application must guarantee that the disk file being
@@ -9711,11 +9719,11 @@
9719 ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9720 ** meaning of each of these checkpoint modes.
9721 */
9722 #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9723 #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9724 #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
9725 #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9726
9727 /*
9728 ** CAPI3REF: Virtual Table Interface Configuration
9729 **
@@ -13142,10 +13150,15 @@
13150 /******** End of fts5.h *********/
13151
13152 /************** End of sqlite3.h *********************************************/
13153 /************** Continuing where we left off in sqliteInt.h ******************/
13154
13155 /*
13156 ** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
13157 */
13158 #define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1
13159
13160 /*
13161 ** Include the configuration header output by 'configure' if we're using the
13162 ** autoconf-based build
13163 */
13164 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
@@ -15553,67 +15566,67 @@
15566 #define OP_Checkpoint 3
15567 #define OP_JournalMode 4
15568 #define OP_Vacuum 5
15569 #define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
15570 #define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */
15571 #define OP_Init 8 /* jump, synopsis: Start at P2 */
15572 #define OP_Goto 9 /* jump */
15573 #define OP_Gosub 10 /* jump */
15574 #define OP_InitCoroutine 11 /* jump */
15575 #define OP_Yield 12 /* jump */
15576 #define OP_MustBeInt 13 /* jump */
15577 #define OP_Jump 14 /* jump */
15578 #define OP_Once 15 /* jump */
15579 #define OP_If 16 /* jump */
15580 #define OP_IfNot 17 /* jump */
15581 #define OP_IsNullOrType 18 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15582 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15583 #define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15584 #define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
15585 #define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
15586 #define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
15587 #define OP_SeekGT 24 /* jump, synopsis: key=r[P3@P4] */
15588 #define OP_IfNotOpen 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15589 #define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
15590 #define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
15591 #define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
15592 #define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
15593 #define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
15594 #define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
15595 #define OP_Last 32 /* jump */
15596 #define OP_IfSmaller 33 /* jump */
15597 #define OP_SorterSort 34 /* jump */
15598 #define OP_Sort 35 /* jump */
15599 #define OP_Rewind 36 /* jump */
15600 #define OP_SorterNext 37 /* jump */
15601 #define OP_Prev 38 /* jump */
15602 #define OP_Next 39 /* jump */
15603 #define OP_IdxLE 40 /* jump, synopsis: key=r[P3@P4] */
15604 #define OP_IdxGT 41 /* jump, synopsis: key=r[P3@P4] */
15605 #define OP_IdxLT 42 /* jump, synopsis: key=r[P3@P4] */
15606 #define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15607 #define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15608 #define OP_IdxGE 45 /* jump, synopsis: key=r[P3@P4] */
15609 #define OP_RowSetRead 46 /* jump, synopsis: r[P3]=rowset(P1) */
15610 #define OP_RowSetTest 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15611 #define OP_Program 48 /* jump */
15612 #define OP_FkIfZero 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15613 #define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15614 #define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15615 #define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15616 #define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15617 #define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15618 #define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15619 #define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15620 #define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15621 #define OP_ElseEq 58 /* jump, same as TK_ESCAPE */
15622 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15623 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15624 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15625 #define OP_IncrVacuum 62 /* jump */
15626 #define OP_VNext 63 /* jump */
15627 #define OP_Filter 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
15628 #define OP_PureFunc 65 /* synopsis: r[P3]=func(r[P2@NP]) */
15629 #define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */
15630 #define OP_Return 67
15631 #define OP_EndCoroutine 68
15632 #define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */
@@ -15745,17 +15758,17 @@
15758 #define OPFLG_IN3 0x08 /* in3: P3 is an input */
15759 #define OPFLG_OUT2 0x10 /* out2: P2 is an output */
15760 #define OPFLG_OUT3 0x20 /* out3: P3 is an output */
15761 #define OPFLG_INITIALIZER {\
15762 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
15763 /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
15764 /* 16 */ 0x03, 0x03, 0x03, 0x12, 0x01, 0x09, 0x09, 0x09,\
15765 /* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
15766 /* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15767 /* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\
15768 /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15769 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\
15770 /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
15771 /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
15772 /* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
15773 /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\
15774 /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\
@@ -15857,10 +15870,11 @@
15870 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15871 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15872 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15873 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15874 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15875 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetLastOp(Vdbe*);
15876 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
15877 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15878 SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15879 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15880 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
@@ -16741,10 +16755,11 @@
16755 void *pMiddle; /* First byte past end of full-size buffers and
16756 ** the first byte of LOOKASIDE_SMALL buffers */
16757 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16758 void *pStart; /* First byte of available memory space */
16759 void *pEnd; /* First byte past end of available space */
16760 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
16761 };
16762 struct LookasideSlot {
16763 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16764 };
16765
@@ -18189,11 +18204,11 @@
18204 #define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
18205 #define EP_Skip 0x002000 /* Operator does not contribute to affinity */
18206 #define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18207 #define EP_Win 0x008000 /* Contains window functions */
18208 #define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18209 /* 0x020000 // Available for reuse */
18210 #define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
18211 #define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
18212 #define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18213 #define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */
18214 #define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */
@@ -19667,10 +19682,11 @@
19682 SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
19683 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19684 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19685 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
19686 SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
19687 SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3*, void*);
19688 SQLITE_PRIVATE int sqlite3MallocSize(const void*);
19689 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
19690 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
19691 SQLITE_PRIVATE void sqlite3PageFree(void*);
19692 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
@@ -20191,10 +20207,11 @@
20207 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
20208 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
20209 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
20210 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20211 SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
20212 SQLITE_PRIVATE i64 sqlite3RealToI64(double);
20213 SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*);
20214 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
20215 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
20216 SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
20217 SQLITE_PRIVATE int sqlite3Atoi(const char*);
@@ -21637,13 +21654,10 @@
21654 "OMIT_WSD",
21655 #endif
21656 #ifdef SQLITE_OMIT_XFER_OPT
21657 "OMIT_XFER_OPT",
21658 #endif
 
 
 
21659 #ifdef SQLITE_PERFORMANCE_TRACE
21660 "PERFORMANCE_TRACE",
21661 #endif
21662 #ifdef SQLITE_POWERSAFE_OVERWRITE
21663 # if SQLITE_POWERSAFE_OVERWRITE != 1
@@ -22592,11 +22606,11 @@
22606 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
22607 ** is really a pointer to an instance of this structure.
22608 */
22609 struct Vdbe {
22610 sqlite3 *db; /* The database connection that owns this statement */
22611 Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */
22612 Parse *pParse; /* Parsing context used to create this Vdbe */
22613 ynVar nVar; /* Number of entries in aVar[] */
22614 int nMem; /* Number of memory locations currently allocated */
22615 int nCursor; /* Number of slots in apCsr[] */
22616 u32 cacheCtr; /* VdbeCursor row cache generation counter */
@@ -23150,10 +23164,12 @@
23164 int i; /* Used to iterate through schemas */
23165 int nByte = 0; /* Used to accumulate return value */
23166
23167 sqlite3BtreeEnterAll(db);
23168 db->pnBytesFreed = &nByte;
23169 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23170 db->lookaside.pEnd = db->lookaside.pStart;
23171 for(i=0; i<db->nDb; i++){
23172 Schema *pSchema = db->aDb[i].pSchema;
23173 if( ALWAYS(pSchema!=0) ){
23174 HashElem *p;
23175
@@ -23175,10 +23191,11 @@
23191 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
23192 }
23193 }
23194 }
23195 db->pnBytesFreed = 0;
23196 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23197 sqlite3BtreeLeaveAll(db);
23198
23199 *pHighwater = 0;
23200 *pCurrent = nByte;
23201 break;
@@ -23192,13 +23209,16 @@
23209 case SQLITE_DBSTATUS_STMT_USED: {
23210 struct Vdbe *pVdbe; /* Used to iterate through VMs */
23211 int nByte = 0; /* Used to accumulate return value */
23212
23213 db->pnBytesFreed = &nByte;
23214 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23215 db->lookaside.pEnd = db->lookaside.pStart;
23216 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
23217 sqlite3VdbeDelete(pVdbe);
23218 }
23219 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23220 db->pnBytesFreed = 0;
23221
23222 *pHighwater = 0; /* IMP: R-64479-57858 */
23223 *pCurrent = nByte;
23224
@@ -23530,11 +23550,11 @@
23550 X1 = 36525*(Y+4716)/100;
23551 X2 = 306001*(M+1)/10000;
23552 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23553 p->validJD = 1;
23554 if( p->validHMS ){
23555 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
23556 if( p->validTZ ){
23557 p->iJD -= p->tz*60000;
23558 p->validYMD = 0;
23559 p->validHMS = 0;
23560 p->validTZ = 0;
@@ -24039,11 +24059,11 @@
24059 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
24060 ** date is already on the appropriate weekday, this is a no-op.
24061 */
24062 if( sqlite3_strnicmp(z, "weekday ", 8)==0
24063 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
24064 && r>=0.0 && r<7.0 && (n=(int)r)==r ){
24065 sqlite3_int64 Z;
24066 computeYMD_HMS(p);
24067 p->validTZ = 0;
24068 p->validJD = 0;
24069 computeJD(p);
@@ -24837,10 +24857,11 @@
24857 DO_OS_MALLOC_TEST(0);
24858 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
24859 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
24860 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
24861 ** reaching the VFS. */
24862 assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) );
24863 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
24864 assert( rc==SQLITE_OK || pFile->pMethods==0 );
24865 return rc;
24866 }
24867 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
@@ -29102,11 +29123,11 @@
29123 /*
29124 ** TRUE if p is a lookaside memory allocation from db
29125 */
29126 #ifndef SQLITE_OMIT_LOOKASIDE
29127 static int isLookaside(sqlite3 *db, const void *p){
29128 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
29129 }
29130 #else
29131 #define isLookaside(A,B) 0
29132 #endif
29133
@@ -29126,22 +29147,20 @@
29147 #endif
29148 }
29149 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){
29150 assert( p!=0 );
29151 #ifdef SQLITE_DEBUG
29152 if( db==0 ){
29153 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
29154 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
29155 }else if( !isLookaside(db,p) ){
29156 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29157 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
 
 
29158 }
29159 #endif
29160 if( db ){
29161 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
29162 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29163 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29164 assert( sqlite3_mutex_held(db->mutex) );
29165 return LOOKASIDE_SMALL;
29166 }
@@ -29193,18 +29212,15 @@
29212 */
29213 SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
29214 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29215 assert( p!=0 );
29216 if( db ){
 
 
 
 
29217 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29218 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29219 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29220 LookasideSlot *pBuf = (LookasideSlot*)p;
29221 assert( db->pnBytesFreed==0 );
29222 #ifdef SQLITE_DEBUG
29223 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
29224 #endif
29225 pBuf->pNext = db->lookaside.pSmallFree;
29226 db->lookaside.pSmallFree = pBuf;
@@ -29211,24 +29227,66 @@
29227 return;
29228 }
29229 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29230 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29231 LookasideSlot *pBuf = (LookasideSlot*)p;
29232 assert( db->pnBytesFreed==0 );
29233 #ifdef SQLITE_DEBUG
29234 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29235 #endif
29236 pBuf->pNext = db->lookaside.pFree;
29237 db->lookaside.pFree = pBuf;
29238 return;
29239 }
29240 }
29241 if( db->pnBytesFreed ){
29242 measureAllocationSize(db, p);
29243 return;
29244 }
29245 }
29246 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29247 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29248 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
29249 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29250 sqlite3_free(p);
29251 }
29252 SQLITE_PRIVATE void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
29253 assert( db!=0 );
29254 assert( sqlite3_mutex_held(db->mutex) );
29255 assert( p!=0 );
29256 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29257 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29258 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29259 LookasideSlot *pBuf = (LookasideSlot*)p;
29260 assert( db->pnBytesFreed==0 );
29261 #ifdef SQLITE_DEBUG
29262 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
29263 #endif
29264 pBuf->pNext = db->lookaside.pSmallFree;
29265 db->lookaside.pSmallFree = pBuf;
29266 return;
29267 }
29268 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29269 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29270 LookasideSlot *pBuf = (LookasideSlot*)p;
29271 assert( db->pnBytesFreed==0 );
29272 #ifdef SQLITE_DEBUG
29273 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29274 #endif
29275 pBuf->pNext = db->lookaside.pFree;
29276 db->lookaside.pFree = pBuf;
29277 return;
29278 }
29279 }
29280 if( db->pnBytesFreed ){
29281 measureAllocationSize(db, p);
29282 return;
29283 }
29284 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29285 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
29286 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29287 sqlite3_free(p);
29288 }
29289 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
29290 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29291 if( p ) sqlite3DbFreeNN(db, p);
29292 }
@@ -29561,12 +29619,17 @@
29619 if( db->nVdbeExec>0 ){
29620 AtomicStore(&db->u1.isInterrupted, 1);
29621 }
29622 DisableLookaside;
29623 if( db->pParse ){
29624 Parse *pParse;
29625 sqlite3ErrorMsg(db->pParse, "out of memory");
29626 db->pParse->rc = SQLITE_NOMEM_BKPT;
29627 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
29628 pParse->nErr++;
29629 pParse->rc = SQLITE_NOMEM;
29630 }
29631 }
29632 }
29633 return 0;
29634 }
29635
@@ -32332,20 +32395,45 @@
32395
32396 /* All threads share a single random number generator.
32397 ** This structure is the current state of the generator.
32398 */
32399 static SQLITE_WSD struct sqlite3PrngType {
32400 u32 s[16]; /* 64 bytes of chacha20 state */
32401 u8 out[64]; /* Output bytes */
32402 u8 n; /* Output bytes remaining */
32403 } sqlite3Prng;
32404
32405
32406 /* The RFC-7539 ChaCha20 block function
32407 */
32408 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
32409 #define QR(a, b, c, d) ( \
32410 a += b, d ^= a, d = ROTL(d,16), \
32411 c += d, b ^= c, b = ROTL(b,12), \
32412 a += b, d ^= a, d = ROTL(d, 8), \
32413 c += d, b ^= c, b = ROTL(b, 7))
32414 static void chacha_block(u32 *out, const u32 *in){
32415 int i;
32416 u32 x[16];
32417 memcpy(x, in, 64);
32418 for(i=0; i<10; i++){
32419 QR(x[0], x[4], x[ 8], x[12]);
32420 QR(x[1], x[5], x[ 9], x[13]);
32421 QR(x[2], x[6], x[10], x[14]);
32422 QR(x[3], x[7], x[11], x[15]);
32423 QR(x[0], x[5], x[10], x[15]);
32424 QR(x[1], x[6], x[11], x[12]);
32425 QR(x[2], x[7], x[ 8], x[13]);
32426 QR(x[3], x[4], x[ 9], x[14]);
32427 }
32428 for(i=0; i<16; i++) out[i] = x[i]+in[i];
32429 }
32430
32431 /*
32432 ** Return N random bytes.
32433 */
32434 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
 
32435 unsigned char *zBuf = pBuf;
32436
32437 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32438 ** state vector. If writable static data is unsupported on the target,
32439 ** we have to locate the state vector at run-time. In the more common
@@ -32371,57 +32459,50 @@
32459 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
32460 #endif
32461
32462 sqlite3_mutex_enter(mutex);
32463 if( N<=0 || pBuf==0 ){
32464 wsdPrng.s[0] = 0;
32465 sqlite3_mutex_leave(mutex);
32466 return;
32467 }
32468
32469 /* Initialize the state of the random number generator once,
32470 ** the first time this routine is called.
 
 
 
 
 
 
32471 */
32472 if( wsdPrng.s[0]==0 ){
32473 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
32474 static const u32 chacha20_init[] = {
32475 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
32476 };
32477 memcpy(&wsdPrng.s[0], chacha20_init, 16);
32478 if( NEVER(pVfs==0) ){
32479 memset(&wsdPrng.s[4], 0, 44);
32480 }else{
32481 sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrng.s[4]);
32482 }
32483 wsdPrng.s[15] = wsdPrng.s[12];
32484 wsdPrng.s[12] = 0;
32485 wsdPrng.n = 0;
 
 
 
 
 
 
 
32486 }
32487
32488 assert( N>0 );
32489 while( 1 /* exit by break */ ){
32490 if( N<=wsdPrng.n ){
32491 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N);
32492 wsdPrng.n -= N;
32493 break;
32494 }
32495 if( wsdPrng.n>0 ){
32496 memcpy(zBuf, wsdPrng.out, wsdPrng.n);
32497 N -= wsdPrng.n;
32498 zBuf += wsdPrng.n;
32499 }
32500 wsdPrng.s[12]++;
32501 chacha_block((u32*)wsdPrng.out, wsdPrng.s);
32502 wsdPrng.n = 64;
32503 }
32504 sqlite3_mutex_leave(mutex);
32505 }
32506
32507 #ifndef SQLITE_UNTESTABLE
32508 /*
@@ -33457,11 +33538,11 @@
33538 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
33539 char *zMsg;
33540 va_list ap;
33541 sqlite3 *db = pParse->db;
33542 assert( db!=0 );
33543 assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
33544 db->errByteOffset = -2;
33545 va_start(ap, zFormat);
33546 zMsg = sqlite3VMPrintf(db, zFormat, ap);
33547 va_end(ap);
33548 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
@@ -35275,67 +35356,67 @@
35356 /* 3 */ "Checkpoint" OpHelp(""),
35357 /* 4 */ "JournalMode" OpHelp(""),
35358 /* 5 */ "Vacuum" OpHelp(""),
35359 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
35360 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
35361 /* 8 */ "Init" OpHelp("Start at P2"),
35362 /* 9 */ "Goto" OpHelp(""),
35363 /* 10 */ "Gosub" OpHelp(""),
35364 /* 11 */ "InitCoroutine" OpHelp(""),
35365 /* 12 */ "Yield" OpHelp(""),
35366 /* 13 */ "MustBeInt" OpHelp(""),
35367 /* 14 */ "Jump" OpHelp(""),
35368 /* 15 */ "Once" OpHelp(""),
35369 /* 16 */ "If" OpHelp(""),
35370 /* 17 */ "IfNot" OpHelp(""),
35371 /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35372 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35373 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35374 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35375 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35376 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
35377 /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"),
35378 /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
35379 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
35380 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
35381 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
35382 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
35383 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
35384 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
35385 /* 32 */ "Last" OpHelp(""),
35386 /* 33 */ "IfSmaller" OpHelp(""),
35387 /* 34 */ "SorterSort" OpHelp(""),
35388 /* 35 */ "Sort" OpHelp(""),
35389 /* 36 */ "Rewind" OpHelp(""),
35390 /* 37 */ "SorterNext" OpHelp(""),
35391 /* 38 */ "Prev" OpHelp(""),
35392 /* 39 */ "Next" OpHelp(""),
35393 /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"),
35394 /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"),
35395 /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"),
35396 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
35397 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
35398 /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"),
35399 /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
35400 /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
35401 /* 48 */ "Program" OpHelp(""),
35402 /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
35403 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
35404 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
35405 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
35406 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
35407 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
35408 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
35409 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
35410 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
35411 /* 58 */ "ElseEq" OpHelp(""),
35412 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35413 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35414 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35415 /* 62 */ "IncrVacuum" OpHelp(""),
35416 /* 63 */ "VNext" OpHelp(""),
35417 /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
35418 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
35419 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
35420 /* 67 */ "Return" OpHelp(""),
35421 /* 68 */ "EndCoroutine" OpHelp(""),
35422 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
@@ -41318,30 +41399,39 @@
41399 ** pVfs->mxPathname bytes.
41400 */
41401 static int unixGetTempname(int nBuf, char *zBuf){
41402 const char *zDir;
41403 int iLimit = 0;
41404 int rc = SQLITE_OK;
41405
41406 /* It's odd to simulate an io-error here, but really this is just
41407 ** using the io-error infrastructure to test that SQLite handles this
41408 ** function failing.
41409 */
41410 zBuf[0] = 0;
41411 SimulateIOError( return SQLITE_IOERR );
41412
41413 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
41414 zDir = unixTempFileDir();
41415 if( zDir==0 ){
41416 rc = SQLITE_IOERR_GETTEMPPATH;
41417 }else{
41418 do{
41419 u64 r;
41420 sqlite3_randomness(sizeof(r), &r);
41421 assert( nBuf>2 );
41422 zBuf[nBuf-2] = 0;
41423 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41424 zDir, r, 0);
41425 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
41426 rc = SQLITE_ERROR;
41427 break;
41428 }
41429 }while( osAccess(zBuf,0)==0 );
41430 }
41431 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
41432 return rc;
41433 }
41434
41435 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41436 /*
41437 ** Routine to transform a unixFile into a proxy-locking unixFile.
@@ -43512,11 +43602,16 @@
43602 ** correctly. See ticket [bb3a86e890c8e96ab] */
43603 assert( ArraySize(aSyscall)==29 );
43604
43605 /* Register all VFSes defined in the aVfs[] array */
43606 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
43607 #ifdef SQLITE_DEFAULT_UNIX_VFS
43608 sqlite3_vfs_register(&aVfs[i],
43609 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
43610 #else
43611 sqlite3_vfs_register(&aVfs[i], i==0);
43612 #endif
43613 }
43614 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
43615
43616 #ifndef SQLITE_OMIT_WAL
43617 /* Validate lock assumptions */
@@ -45480,10 +45575,11 @@
45575 char **ppDirectory = 0;
45576 #ifndef SQLITE_OMIT_AUTOINIT
45577 int rc = sqlite3_initialize();
45578 if( rc ) return rc;
45579 #endif
45580 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
45581 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
45582 ppDirectory = &sqlite3_data_directory;
45583 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
45584 ppDirectory = &sqlite3_temp_directory;
45585 }
@@ -45494,18 +45590,23 @@
45590 if( ppDirectory ){
45591 char *zCopy = 0;
45592 if( zValue && zValue[0] ){
45593 zCopy = sqlite3_mprintf("%s", zValue);
45594 if ( zCopy==0 ){
45595 rc = SQLITE_NOMEM_BKPT;
45596 goto set_directory8_done;
45597 }
45598 }
45599 sqlite3_free(*ppDirectory);
45600 *ppDirectory = zCopy;
45601 rc = SQLITE_OK;
45602 }else{
45603 rc = SQLITE_ERROR;
45604 }
45605 set_directory8_done:
45606 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
45607 return rc;
45608 }
45609
45610 /*
45611 ** This function is the same as sqlite3_win32_set_directory (below); however,
45612 ** it accepts a UTF-16 string.
@@ -48274,10 +48375,22 @@
48375 }
48376 }
48377 }
48378 return 0;
48379 }
48380
48381 /*
48382 ** If sqlite3_temp_directory is not, take the mutex and return true.
48383 **
48384 ** If sqlite3_temp_directory is NULL, omit the mutex and return false.
48385 */
48386 static int winTempDirDefined(void){
48387 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48388 if( sqlite3_temp_directory!=0 ) return 1;
48389 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48390 return 0;
48391 }
48392
48393 /*
48394 ** Create a temporary file name and store the resulting pointer into pzBuf.
48395 ** The pointer returned in pzBuf must be freed via sqlite3_free().
48396 */
@@ -48311,24 +48424,27 @@
48424 ** has been explicitly set by the application; otherwise, use the one
48425 ** configured by the operating system.
48426 */
48427 nDir = nMax - (nPre + 15);
48428 assert( nDir>0 );
48429 if( winTempDirDefined() ){
48430 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
48431 if( nDirLen>0 ){
48432 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48433 nDirLen++;
48434 }
48435 if( nDirLen>nDir ){
48436 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48437 sqlite3_free(zBuf);
48438 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48439 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
48440 }
48441 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
48442 }
48443 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48444 }
48445
48446 #if defined(__CYGWIN__)
48447 else{
48448 static const char *azDirs[] = {
48449 0, /* getenv("SQLITE_TMPDIR") */
48450 0, /* getenv("TMPDIR") */
@@ -49113,11 +49229,11 @@
49229 /*
49230 ** Turn a relative pathname into a full pathname. Write the full
49231 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49232 ** bytes in size.
49233 */
49234 static int winFullPathnameNoMutex(
49235 sqlite3_vfs *pVfs, /* Pointer to vfs object */
49236 const char *zRelative, /* Possibly relative input path */
49237 int nFull, /* Size of output buffer in bytes */
49238 char *zFull /* Output buffer */
49239 ){
@@ -49291,10 +49407,23 @@
49407 return SQLITE_OK;
49408 }else{
49409 return SQLITE_IOERR_NOMEM_BKPT;
49410 }
49411 #endif
49412 }
49413 static int winFullPathname(
49414 sqlite3_vfs *pVfs, /* Pointer to vfs object */
49415 const char *zRelative, /* Possibly relative input path */
49416 int nFull, /* Size of output buffer in bytes */
49417 char *zFull /* Output buffer */
49418 ){
49419 int rc;
49420 sqlite3_mutex *pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR);
49421 sqlite3_mutex_enter(pMutex);
49422 rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
49423 sqlite3_mutex_leave(pMutex);
49424 return rc;
49425 }
49426
49427 #ifndef SQLITE_OMIT_LOAD_EXTENSION
49428 /*
49429 ** Interfaces for opening a shared library, finding entry points
@@ -51080,39 +51209,58 @@
51209 */
51210 #if defined(SQLITE_DEBUG) && 0
51211 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
51212 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
51213 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
51214 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
 
 
 
51215 PgHdr *pPg;
51216 unsigned char *a;
51217 int j;
51218 pPg = (PgHdr*)pLower->pExtra;
51219 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51220 a = (unsigned char *)pLower->pBuf;
51221 for(j=0; j<12; j++) printf("%02x", a[j]);
51222 printf(" ptr %p\n", pPg);
51223 }
51224 static void pcacheDump(PCache *pCache){
51225 int N;
51226 int i;
51227 sqlite3_pcache_page *pLower;
51228
51229 if( sqlite3PcacheTrace<2 ) return;
51230 if( pCache->pCache==0 ) return;
51231 N = sqlite3PcachePagecount(pCache);
51232 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
51233 for(i=1; i<=N; i++){
51234 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
51235 if( pLower==0 ) continue;
51236 pcachePageTrace(i, pLower);
51237 if( ((PgHdr*)pLower)->pPage==0 ){
 
 
 
 
51238 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
51239 }
51240 }
51241 }
51242 #else
51243 # define pcacheTrace(X)
51244 # define pcachePageTrace(PGNO, X)
51245 # define pcacheDump(X)
51246 #endif
51247
51248 /*
51249 ** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
51250 ** This routine runs inside of assert() statements only.
51251 */
51252 #ifdef SQLITE_DEBUG
51253 static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
51254 PgHdr *p;
51255 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51256 if( p==pPg ) return 1;
51257 }
51258 return 0;
51259 }
51260 #endif
51261
51262 /*
51263 ** Check invariants on a PgHdr entry. Return true if everything is OK.
51264 ** Return false if any invariant is violated.
51265 **
51266 ** This routine is for use inside of assert() statements only. For
@@ -51127,12 +51275,17 @@
51275 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
51276 pCache = pPg->pCache;
51277 assert( pCache!=0 ); /* Every page has an associated PCache */
51278 if( pPg->flags & PGHDR_CLEAN ){
51279 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
51280 assert( !pageOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirty list */
51281 }else{
51282 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
51283 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
51284 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
51285 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
51286 assert( pageOnDirtyList(pCache, pPg) );
51287 }
51288 /* WRITEABLE pages must also be DIRTY */
51289 if( pPg->flags & PGHDR_WRITEABLE ){
51290 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
51291 }
@@ -51402,12 +51555,13 @@
51555 eCreate = createFlag & pCache->eCreate;
51556 assert( eCreate==0 || eCreate==1 || eCreate==2 );
51557 assert( createFlag==0 || pCache->eCreate==eCreate );
51558 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
51559 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51560 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
51561 createFlag?" create":"",pRes));
51562 pcachePageTrace(pgno, pRes);
51563 return pRes;
51564 }
51565
51566 /*
51567 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
@@ -51531,10 +51685,11 @@
51685 if( (--p->nRef)==0 ){
51686 if( p->flags&PGHDR_CLEAN ){
51687 pcacheUnpin(p);
51688 }else{
51689 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
51690 assert( sqlite3PcachePageSanity(p) );
51691 }
51692 }
51693 }
51694
51695 /*
@@ -51574,10 +51729,11 @@
51729 if( p->flags & PGHDR_CLEAN ){
51730 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
51731 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
51732 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
51733 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
51734 assert( sqlite3PcachePageSanity(p) );
51735 }
51736 assert( sqlite3PcachePageSanity(p) );
51737 }
51738 }
51739
@@ -51636,18 +51792,28 @@
51792 /*
51793 ** Change the page number of page p to newPgno.
51794 */
51795 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
51796 PCache *pCache = p->pCache;
51797 sqlite3_pcache_page *pOther;
51798 assert( p->nRef>0 );
51799 assert( newPgno>0 );
51800 assert( sqlite3PcachePageSanity(p) );
51801 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51802 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
51803 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51804 if( pOther ){
51805 PgHdr *pPg = (PgHdr*)pOther->pExtra;
51806 pPg->pgno = p->pgno;
51807 if( pPg->pPage==0 ){
51808 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pOther, 0);
51809 }
51810 }
51811 p->pgno = newPgno;
51812 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
51813 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
51814 assert( sqlite3PcachePageSanity(p) );
51815 }
51816 }
51817
51818 /*
51819 ** Drop every cache entry whose page number is greater than "pgno". The
@@ -51941,16 +52107,17 @@
52107 ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
52108 ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
52109 ** size can vary according to architecture, compile-time options, and
52110 ** SQLite library version number.
52111 **
52112 ** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
52113 ** was defined, then the page content would be held in a separate memory
52114 ** allocation from the PgHdr1. This was intended to avoid clownshoe memory
52115 ** allocations. However, the btree layer needs a small (16-byte) overrun
52116 ** area after the page content buffer. The header serves as that overrun
52117 ** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
52118 ** any possibility of a memory error.
52119 **
52120 ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
52121 ** with this module. Information is passed back and forth as PgHdr1 pointers.
52122 **
52123 ** The pcache.c and pager.c modules deal pointers to PgHdr objects.
@@ -51991,34 +52158,44 @@
52158 typedef struct PgFreeslot PgFreeslot;
52159 typedef struct PGroup PGroup;
52160
52161 /*
52162 ** Each cache entry is represented by an instance of the following
52163 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
52164 ** directly before this structure and is used to cache the page content.
 
52165 **
52166 ** When reading a corrupt database file, it is possible that SQLite might
52167 ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
52168 ** It will only read past the end of the page buffer, never write. This
52169 ** object is positioned immediately after the page buffer to serve as an
52170 ** overrun area, so that overreads are harmless.
52171 **
52172 ** Variables isBulkLocal and isAnchor were once type "u8". That works,
52173 ** but causes a 2-byte gap in the structure for most architectures (since
52174 ** pointers must be either 4 or 8-byte aligned). As this structure is located
52175 ** in memory directly after the associated page data, if the database is
52176 ** corrupt, code at the b-tree layer may overread the page buffer and
52177 ** read part of this structure before the corruption is detected. This
52178 ** can cause a valgrind error if the unitialized gap is accessed. Using u16
52179 ** ensures there is no such gap, and therefore no bytes of uninitialized
52180 ** memory in the structure.
52181 **
52182 ** The pLruNext and pLruPrev pointers form a double-linked circular list
52183 ** of all pages that are unpinned. The PGroup.lru element (which should be
52184 ** the only element on the list with PgHdr1.isAnchor set to 1) forms the
52185 ** beginning and the end of the list.
52186 */
52187 struct PgHdr1 {
52188 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
52189 unsigned int iKey; /* Key value (page number) */
52190 u16 isBulkLocal; /* This page from bulk local storage */
52191 u16 isAnchor; /* This is the PGroup.lru element */
52192 PgHdr1 *pNext; /* Next in hash table chain */
52193 PCache1 *pCache; /* Cache that currently owns this page */
52194 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
52195 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
52196 /* NB: pLruPrev is only valid if pLruNext!=0 */
52197 };
52198
52199 /*
52200 ** A page is pinned if it is not on the LRU list. To be "pinned" means
52201 ** that the page is in active use and must not be deallocated.
@@ -52340,29 +52517,17 @@
52517 assert( pcache1.separateCache==0 );
52518 assert( pCache->pGroup==&pcache1.grp );
52519 pcache1LeaveMutex(pCache->pGroup);
52520 #endif
52521 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
 
 
 
 
 
 
 
 
 
52522 pPg = pcache1Alloc(pCache->szAlloc);
 
52523 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
52524 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
52525 pcache1EnterMutex(pCache->pGroup);
52526 #endif
52527 if( pPg==0 ) return 0;
 
52528 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
 
52529 p->page.pBuf = pPg;
52530 p->page.pExtra = &p[1];
52531 p->isBulkLocal = 0;
52532 p->isAnchor = 0;
52533 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
@@ -52382,13 +52547,10 @@
52547 if( p->isBulkLocal ){
52548 p->pNext = pCache->pFree;
52549 pCache->pFree = p;
52550 }else{
52551 pcache1Free(p->page.pBuf);
 
 
 
52552 }
52553 (*pCache->pnPurgeable)--;
52554 }
52555
52556 /*
@@ -53025,27 +53187,45 @@
53187 unsigned int iNew
53188 ){
53189 PCache1 *pCache = (PCache1 *)p;
53190 PgHdr1 *pPage = (PgHdr1 *)pPg;
53191 PgHdr1 **pp;
53192 unsigned int hOld, hNew;
53193 assert( pPage->iKey==iOld );
53194 assert( pPage->pCache==pCache );
53195 assert( iOld!=iNew ); /* The page number really is changing */
53196
53197 pcache1EnterMutex(pCache->pGroup);
53198
53199 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
53200 hOld = iOld%pCache->nHash;
53201 pp = &pCache->apHash[hOld];
53202 while( (*pp)!=pPage ){
53203 pp = &(*pp)->pNext;
53204 }
53205 *pp = pPage->pNext;
53206
53207 hNew = iNew%pCache->nHash;
53208 pp = &pCache->apHash[hNew];
53209 while( *pp ){
53210 if( (*pp)->iKey==iNew ){
53211 /* If there is already another pcache entry at iNew, change it to iOld,
53212 ** thus swapping the positions of iNew and iOld */
53213 PgHdr1 *pOld = *pp;
53214 *pp = pOld->pNext;
53215 pOld->pNext = pCache->apHash[hOld];
53216 pCache->apHash[hOld] = pOld;
53217 pOld->iKey = iOld;
53218 break;
53219 }else{
53220 pp = &(*pp)->pNext;
53221 }
53222 }
53223
53224 pPage->iKey = iNew;
53225 pPage->pNext = pCache->apHash[hNew];
53226 pCache->apHash[hNew] = pPage;
53227 if( iNew>pCache->iMaxKey ){
53228 pCache->iMaxKey = iNew;
53229 }
53230
53231 pcache1LeaveMutex(pCache->pGroup);
@@ -53148,13 +53328,10 @@
53328 while( (nReq<0 || nFree<nReq)
53329 && (p=pcache1.grp.lru.pLruPrev)!=0
53330 && p->isAnchor==0
53331 ){
53332 nFree += pcache1MemSize(p->page.pBuf);
 
 
 
53333 assert( PAGE_IS_UNPINNED(p) );
53334 pcache1PinPage(p);
53335 pcache1RemoveFromHash(p, 1);
53336 }
53337 pcache1LeaveMutex(&pcache1.grp);
@@ -59639,10 +59816,11 @@
59816 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
59817 int nSpill;
59818
59819 if( pPager->tempFile ){
59820 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
59821 flags |= SQLITE_OPEN_EXCLUSIVE;
59822 nSpill = sqlite3Config.nStmtSpill;
59823 }else{
59824 flags |= SQLITE_OPEN_MAIN_JOURNAL;
59825 nSpill = jrnlBufferSize(pPager);
59826 }
@@ -59674,10 +59852,11 @@
59852 }
59853
59854 if( rc!=SQLITE_OK ){
59855 sqlite3BitvecDestroy(pPager->pInJournal);
59856 pPager->pInJournal = 0;
59857 pPager->journalOff = 0;
59858 }else{
59859 assert( pPager->eState==PAGER_WRITER_LOCKED );
59860 pPager->eState = PAGER_WRITER_CACHEMOD;
59861 }
59862
@@ -66737,10 +66916,11 @@
66916 ** db using sqlite3SchemaToIndex().
66917 */
66918 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
66919 Btree *p;
66920 assert( db!=0 );
66921 if( db->pVfs==0 && db->nDb==0 ) return 1;
66922 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
66923 assert( iDb>=0 && iDb<db->nDb );
66924 if( !sqlite3_mutex_held(db->mutex) ) return 0;
66925 if( iDb==1 ) return 1;
66926 p = db->aDb[iDb].pBt;
@@ -68309,12 +68489,11 @@
68489 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68490 assert( pPage->pBt!=0 );
68491 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
68492 assert( pPage->nOverflow==0 );
68493 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68494 data = pPage->aData;
 
68495 hdr = pPage->hdrOffset;
68496 cellOffset = pPage->cellOffset;
68497 nCell = pPage->nCell;
68498 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
68499 iCellFirst = cellOffset + 2*nCell;
@@ -68364,43 +68543,42 @@
68543 }
68544
68545 cbrk = usableSize;
68546 iCellLast = usableSize - 4;
68547 iCellStart = get2byte(&data[hdr+5]);
68548 if( nCell>0 ){
68549 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68550 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68551 src = temp;
68552 for(i=0; i<nCell; i++){
68553 u8 *pAddr; /* The i-th cell pointer */
68554 pAddr = &data[cellOffset + i*2];
68555 pc = get2byte(pAddr);
68556 testcase( pc==iCellFirst );
68557 testcase( pc==iCellLast );
68558 /* These conditions have already been verified in btreeInitPage()
68559 ** if PRAGMA cell_size_check=ON.
68560 */
68561 if( pc<iCellStart || pc>iCellLast ){
68562 return SQLITE_CORRUPT_PAGE(pPage);
68563 }
68564 assert( pc>=iCellStart && pc<=iCellLast );
68565 size = pPage->xCellSize(pPage, &src[pc]);
68566 cbrk -= size;
68567 if( cbrk<iCellStart || pc+size>usableSize ){
68568 return SQLITE_CORRUPT_PAGE(pPage);
68569 }
68570 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
68571 testcase( cbrk+size==usableSize );
68572 testcase( pc+size==usableSize );
68573 put2byte(pAddr, cbrk);
68574 memcpy(&data[cbrk], &src[pc], size);
68575 }
 
68576 }
68577 data[hdr+7] = 0;
68578
68579 defragment_out:
68580 assert( pPage->nFree>=0 );
68581 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
68582 return SQLITE_CORRUPT_PAGE(pPage);
68583 }
68584 assert( cbrk>=iCellFirst );
@@ -68469,13 +68647,13 @@
68647 return &aData[pc + x];
68648 }
68649 iAddr = pc;
68650 pTmp = &aData[pc];
68651 pc = get2byte(pTmp);
68652 if( pc<=iAddr ){
68653 if( pc ){
68654 /* The next slot in the chain comes before the current slot */
68655 *pRc = SQLITE_CORRUPT_PAGE(pPg);
68656 }
68657 return 0;
68658 }
68659 }
@@ -68623,11 +68801,11 @@
68801 iPtr = hdr + 1;
68802 if( data[iPtr+1]==0 && data[iPtr]==0 ){
68803 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
68804 }else{
68805 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
68806 if( iFreeBlk<=iPtr ){
68807 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
68808 return SQLITE_CORRUPT_PAGE(pPage);
68809 }
68810 iPtr = iFreeBlk;
68811 }
@@ -69105,13 +69283,11 @@
69283 if( pCur ){
69284 pCur->iPage--;
69285 pCur->pPage = pCur->apPage[pCur->iPage];
69286 }
69287 testcase( pgno==0 );
69288 assert( pgno!=0 || rc!=SQLITE_OK );
 
 
69289 return rc;
69290 }
69291
69292 /*
69293 ** Release a MemPage. This should be called once for each prior
@@ -72049,12 +72225,10 @@
72225 ** the new child page does not match the flags field of the parent (i.e.
72226 ** if an intkey page appears to be the parent of a non-intkey page, or
72227 ** vice-versa).
72228 */
72229 static int moveToChild(BtCursor *pCur, u32 newPgno){
 
 
72230 assert( cursorOwnsBtShared(pCur) );
72231 assert( pCur->eState==CURSOR_VALID );
72232 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
72233 assert( pCur->iPage>=0 );
72234 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
@@ -72064,11 +72238,12 @@
72238 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72239 pCur->aiIdx[pCur->iPage] = pCur->ix;
72240 pCur->apPage[pCur->iPage] = pCur->pPage;
72241 pCur->ix = 0;
72242 pCur->iPage++;
72243 return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur,
72244 pCur->curPagerFlags);
72245 }
72246
72247 #ifdef SQLITE_DEBUG
72248 /*
72249 ** Page pParent is an internal (non-leaf) tree page. This function
@@ -72170,11 +72345,11 @@
72345 assert( pCur->skipNext!=SQLITE_OK );
72346 return pCur->skipNext;
72347 }
72348 sqlite3BtreeClearCursor(pCur);
72349 }
72350 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
72351 0, pCur->curPagerFlags);
72352 if( rc!=SQLITE_OK ){
72353 pCur->eState = CURSOR_INVALID;
72354 return rc;
72355 }
@@ -73811,16 +73986,10 @@
73986 data = pPage->aData;
73987 ptr = &pPage->aCellIdx[2*idx];
73988 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
73989 pc = get2byte(ptr);
73990 hdr = pPage->hdrOffset;
 
 
 
 
 
 
73991 testcase( pc==(u32)get2byte(&data[hdr+5]) );
73992 testcase( pc+sz==pPage->pBt->usableSize );
73993 if( pc+sz > pPage->pBt->usableSize ){
73994 *pRC = SQLITE_CORRUPT_BKPT;
73995 return;
@@ -74700,12 +74869,10 @@
74869 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
74870 u8 *aSpace1; /* Space for copies of dividers cells */
74871 Pgno pgno; /* Temp var to store a page number in */
74872 u8 abDone[NB+2]; /* True after i'th new page is populated */
74873 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
 
 
74874 CellArray b; /* Parsed information on cells being balanced */
74875
74876 memset(abDone, 0, sizeof(abDone));
74877 memset(&b, 0, sizeof(b));
74878 pBt = pParent->pBt;
@@ -75125,46 +75292,43 @@
75292 ** Reassign page numbers so that the new pages are in ascending order.
75293 ** This helps to keep entries in the disk file in order so that a scan
75294 ** of the table is closer to a linear scan through the file. That in turn
75295 ** helps the operating system to deliver pages from the disk more rapidly.
75296 **
75297 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
75298 ** (5), that is not a performance concern.
75299 **
75300 ** When NB==3, this one optimization makes the database about 25% faster
75301 ** for large insertions and deletions.
75302 */
75303 for(i=0; i<nNew; i++){
75304 aPgno[i] = apNew[i]->pgno;
75305 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
75306 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
75307 }
75308 for(i=0; i<nNew-1; i++){
75309 int iB = i;
75310 for(j=i+1; j<nNew; j++){
75311 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
75312 }
75313
75314 /* If apNew[i] has a page number that is bigger than any of the
75315 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
75316 ** entry that has the smallest page number (which we know to be
75317 ** entry apNew[iB]).
75318 */
75319 if( iB!=i ){
75320 Pgno pgnoA = apNew[i]->pgno;
75321 Pgno pgnoB = apNew[iB]->pgno;
75322 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
75323 u16 fgA = apNew[i]->pDbPage->flags;
75324 u16 fgB = apNew[iB]->pDbPage->flags;
75325 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
75326 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
75327 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
75328 apNew[i]->pgno = pgnoB;
75329 apNew[iB]->pgno = pgnoA;
 
 
 
75330 }
75331 }
75332
75333 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
75334 "%d(%d nc=%d) %d(%d nc=%d)\n",
@@ -79428,10 +79592,20 @@
79592 double r2 = (double)i;
79593 return r1==0.0
79594 || (memcmp(&r1, &r2, sizeof(r1))==0
79595 && i >= -2251799813685248LL && i < 2251799813685248LL);
79596 }
79597
79598 /* Convert a floating point value to its closest integer. Do so in
79599 ** a way that avoids 'outside the range of representable values' warnings
79600 ** from UBSAN.
79601 */
79602 SQLITE_PRIVATE i64 sqlite3RealToI64(double r){
79603 if( r<=(double)SMALLEST_INT64 ) return SMALLEST_INT64;
79604 if( r>=(double)LARGEST_INT64) return LARGEST_INT64;
79605 return (i64)r;
79606 }
79607
79608 /*
79609 ** Convert pMem so that it has type MEM_Real or MEM_Int.
79610 ** Invalidate any prior representations.
79611 **
@@ -79450,11 +79624,11 @@
79624 sqlite3_int64 ix;
79625 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
79626 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79627 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
79628 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79629 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
79630 ){
79631 pMem->u.i = ix;
79632 MemSetTypeFlag(pMem, MEM_Int);
79633 }else{
79634 MemSetTypeFlag(pMem, MEM_Real);
@@ -80682,14 +80856,14 @@
80856 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
80857 if( p==0 ) return 0;
80858 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
80859 p->db = db;
80860 if( db->pVdbe ){
80861 db->pVdbe->ppVPrev = &p->pVNext;
80862 }
80863 p->pVNext = db->pVdbe;
80864 p->ppVPrev = &db->pVdbe;
80865 db->pVdbe = p;
80866 assert( p->eVdbeState==VDBE_INIT_STATE );
80867 p->pParse = pParse;
80868 pParse->pVdbe = p;
80869 assert( pParse->aLabel==0 );
@@ -80767,25 +80941,32 @@
80941 return 0;
80942 }
80943 #endif
80944
80945 /*
80946 ** Swap byte-code between two VDBE structures.
80947 **
80948 ** This happens after pB was previously run and returned
80949 ** SQLITE_SCHEMA. The statement was then reprepared in pA.
80950 ** This routine transfers the new bytecode in pA over to pB
80951 ** so that pB can be run again. The old pB byte code is
80952 ** moved back to pA so that it will be cleaned up when pA is
80953 ** finalized.
80954 */
80955 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
80956 Vdbe tmp, *pTmp, **ppTmp;
80957 char *zTmp;
80958 assert( pA->db==pB->db );
80959 tmp = *pA;
80960 *pA = *pB;
80961 *pB = tmp;
80962 pTmp = pA->pVNext;
80963 pA->pVNext = pB->pVNext;
80964 pB->pVNext = pTmp;
80965 ppTmp = pA->ppVPrev;
80966 pA->ppVPrev = pB->ppVPrev;
80967 pB->ppVPrev = ppTmp;
80968 zTmp = pA->zSql;
80969 pA->zSql = pB->zSql;
80970 pB->zSql = zTmp;
80971 #ifdef SQLITE_ENABLE_NORMALIZE
80972 zTmp = pA->zNormSql;
@@ -81033,10 +81214,11 @@
81214 pCtx->argc = nArg;
81215 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
81216 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
81217 p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
81218 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
81219 sqlite3MayAbort(pParse);
81220 return addr;
81221 }
81222
81223 /*
81224 ** Add an opcode that includes the p4 value with a P4_INT64 or
@@ -81101,11 +81283,11 @@
81283 va_end(ap);
81284 v = pParse->pVdbe;
81285 iThis = v->nOp;
81286 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
81287 zMsg, P4_DYNAMIC);
81288 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
81289 if( bPush){
81290 pParse->addrExplain = iThis;
81291 }
81292 }
81293 }
@@ -81368,10 +81550,11 @@
81550 int opcode = pOp->opcode;
81551 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
81552 || opcode==OP_VDestroy
81553 || opcode==OP_VCreate
81554 || opcode==OP_ParseSchema
81555 || opcode==OP_Function || opcode==OP_PureFunc
81556 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
81557 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
81558 ){
81559 hasAbort = 1;
81560 break;
@@ -81458,12 +81641,12 @@
81641 Parse *pParse = p->pParse;
81642 int *aLabel = pParse->aLabel;
81643 p->readOnly = 1;
81644 p->bIsReader = 0;
81645 pOp = &p->aOp[p->nOp-1];
81646 assert( p->aOp[0].opcode==OP_Init );
81647 while( 1 /* Loop termates when it reaches the OP_Init opcode */ ){
81648 /* Only JUMP opcodes and the short list of special opcodes in the switch
81649 ** below need to be considered. The mkopcodeh.tcl generator script groups
81650 ** all these opcodes together near the front of the opcode list. Skip
81651 ** any opcode that does not need processing by virtual of the fact that
81652 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
@@ -81488,10 +81671,14 @@
81671 case OP_JournalMode: {
81672 p->readOnly = 0;
81673 p->bIsReader = 1;
81674 break;
81675 }
81676 case OP_Init: {
81677 assert( pOp->p2>=0 );
81678 goto resolve_p2_values_loop_exit;
81679 }
81680 #ifndef SQLITE_OMIT_VIRTUALTABLE
81681 case OP_VUpdate: {
81682 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
81683 break;
81684 }
@@ -81520,15 +81707,16 @@
81707 /* The mkopcodeh.tcl script has so arranged things that the only
81708 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81709 ** have non-negative values for P2. */
81710 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
81711 }
81712 assert( pOp>p->aOp );
81713 pOp--;
81714 }
81715 resolve_p2_values_loop_exit:
81716 if( aLabel ){
81717 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
81718 pParse->aLabel = 0;
81719 }
81720 pParse->nLabel = 0;
81721 *pMaxFuncArgs = nMaxArgs;
81722 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
@@ -81773,19 +81961,23 @@
81961 /*
81962 ** Change the value of the opcode, or P1, P2, P3, or P5 operands
81963 ** for a specific instruction.
81964 */
81965 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
81966 assert( addr>=0 );
81967 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
81968 }
81969 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
81970 assert( addr>=0 );
81971 sqlite3VdbeGetOp(p,addr)->p1 = val;
81972 }
81973 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
81974 assert( addr>=0 || p->db->mallocFailed );
81975 sqlite3VdbeGetOp(p,addr)->p2 = val;
81976 }
81977 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
81978 assert( addr>=0 );
81979 sqlite3VdbeGetOp(p,addr)->p3 = val;
81980 }
81981 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
81982 assert( p->nOp>0 || p->db->mallocFailed );
81983 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
@@ -81817,11 +82009,11 @@
82009 assert( p->aOp[addr].opcode==OP_Once
82010 || p->aOp[addr].opcode==OP_If
82011 || p->aOp[addr].opcode==OP_FkIfZero );
82012 assert( p->aOp[addr].p4type==0 );
82013 #ifdef SQLITE_VDBE_COVERAGE
82014 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
82015 #endif
82016 p->nOp--;
82017 }else{
82018 sqlite3VdbeChangeP2(p, addr, p->nOp);
82019 }
@@ -81831,25 +82023,27 @@
82023 /*
82024 ** If the input FuncDef structure is ephemeral, then free it. If
82025 ** the FuncDef is not ephermal, then do nothing.
82026 */
82027 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
82028 assert( db!=0 );
82029 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
82030 sqlite3DbNNFreeNN(db, pDef);
82031 }
82032 }
82033
82034 /*
82035 ** Delete a P4 value if necessary.
82036 */
82037 static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
82038 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
82039 sqlite3DbNNFreeNN(db, p);
82040 }
82041 static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
82042 assert( db!=0 );
82043 freeEphemeralFunction(db, p->pFunc);
82044 sqlite3DbNNFreeNN(db, p);
82045 }
82046 static void freeP4(sqlite3 *db, int p4type, void *p4){
82047 assert( db );
82048 switch( p4type ){
82049 case P4_FUNCCTX: {
@@ -81858,11 +82052,11 @@
82052 }
82053 case P4_REAL:
82054 case P4_INT64:
82055 case P4_DYNAMIC:
82056 case P4_INTARRAY: {
82057 if( p4 ) sqlite3DbNNFreeNN(db, p4);
82058 break;
82059 }
82060 case P4_KEYINFO: {
82061 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
82062 break;
@@ -81897,10 +82091,11 @@
82091 ** opcodes contained within. If aOp is not NULL it is assumed to contain
82092 ** nOp entries.
82093 */
82094 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
82095 assert( nOp>=0 );
82096 assert( db!=0 );
82097 if( aOp ){
82098 Op *pOp = &aOp[nOp-1];
82099 while(1){ /* Exit via break */
82100 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
82101 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -81907,11 +82102,11 @@
82102 sqlite3DbFree(db, pOp->zComment);
82103 #endif
82104 if( pOp==aOp ) break;
82105 pOp--;
82106 }
82107 sqlite3DbNNFreeNN(db, aOp);
82108 }
82109 }
82110
82111 /*
82112 ** Link the SubProgram object passed as the second argument into the linked
@@ -82138,17 +82333,17 @@
82333 #ifdef SQLITE_VDBE_COVERAGE
82334 /*
82335 ** Set the value if the iSrcLine field for the previously coded instruction.
82336 */
82337 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
82338 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
82339 }
82340 #endif /* SQLITE_VDBE_COVERAGE */
82341
82342 /*
82343 ** Return the opcode for a given address. The address must be non-negative.
82344 ** See sqlite3VdbeGetLastOp() to get the most recently added opcode.
82345 **
82346 ** If a memory allocation error has occurred prior to the calling of this
82347 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
82348 ** is readable but not writable, though it is cast to a writable value.
82349 ** The return of a dummy opcode allows the call to continue functioning
@@ -82160,20 +82355,23 @@
82355 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
82356 /* C89 specifies that the constant "dummy" will be initialized to all
82357 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
82358 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
82359 assert( p->eVdbeState==VDBE_INIT_STATE );
 
 
 
82360 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
82361 if( p->db->mallocFailed ){
82362 return (VdbeOp*)&dummy;
82363 }else{
82364 return &p->aOp[addr];
82365 }
82366 }
82367
82368 /* Return the most recently added opcode
82369 */
82370 VdbeOp * sqlite3VdbeGetLastOp(Vdbe *p){
82371 return sqlite3VdbeGetOp(p, p->nOp - 1);
82372 }
82373
82374 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
82375 /*
82376 ** Return an integer value for one of the parameters to the opcode pOp
82377 ** determined by character c.
@@ -82658,11 +82856,11 @@
82856 if( p->flags&(MEM_Agg|MEM_Dyn) ){
82857 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
82858 sqlite3VdbeMemRelease(p);
82859 p->flags = MEM_Undefined;
82860 }else if( p->szMalloc ){
82861 sqlite3DbNNFreeNN(db, p->zMalloc);
82862 p->szMalloc = 0;
82863 p->flags = MEM_Undefined;
82864 }
82865 #ifdef SQLITE_DEBUG
82866 else{
@@ -83650,11 +83848,11 @@
83848 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
83849 cnt++;
83850 if( p->readOnly==0 ) nWrite++;
83851 if( p->bIsReader ) nRead++;
83852 }
83853 p = p->pVNext;
83854 }
83855 assert( cnt==db->nVdbeActive );
83856 assert( nWrite==db->nVdbeWrite );
83857 assert( nRead==db->nVdbeRead );
83858 }
@@ -84179,27 +84377,28 @@
84377 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
84378 ** the database connection and frees the object itself.
84379 */
84380 static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
84381 SubProgram *pSub, *pNext;
84382 assert( db!=0 );
84383 assert( p->db==0 || p->db==db );
84384 if( p->aColName ){
84385 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
84386 sqlite3DbNNFreeNN(db, p->aColName);
84387 }
84388 for(pSub=p->pProgram; pSub; pSub=pNext){
84389 pNext = pSub->pNext;
84390 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
84391 sqlite3DbFree(db, pSub);
84392 }
84393 if( p->eVdbeState!=VDBE_INIT_STATE ){
84394 releaseMemArray(p->aVar, p->nVar);
84395 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
84396 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
84397 }
84398 vdbeFreeOpArray(db, p->aOp, p->nOp);
84399 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
84400 #ifdef SQLITE_ENABLE_NORMALIZE
84401 sqlite3DbFree(db, p->zNormSql);
84402 {
84403 DblquoteStr *pThis, *pNext;
84404 for(pThis=p->pDblStr; pThis; pThis=pNext){
@@ -84225,24 +84424,21 @@
84424 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
84425 sqlite3 *db;
84426
84427 assert( p!=0 );
84428 db = p->db;
84429 assert( db!=0 );
84430 assert( sqlite3_mutex_held(db->mutex) );
84431 sqlite3VdbeClearObject(db, p);
84432 if( db->pnBytesFreed==0 ){
84433 assert( p->ppVPrev!=0 );
84434 *p->ppVPrev = p->pVNext;
84435 if( p->pVNext ){
84436 p->pVNext->ppVPrev = p->ppVPrev;
 
 
 
 
84437 }
84438 }
84439 sqlite3DbNNFreeNN(db, p);
84440 }
84441
84442 /*
84443 ** The cursor "p" has a pending seek operation that has not yet been
84444 ** carried out. Seek the cursor now. If an error occurs, return
@@ -85733,11 +85929,11 @@
85929 ** prepared statements. The flag is set to 1 for an immediate expiration
85930 ** and set to 2 for an advisory expiration.
85931 */
85932 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
85933 Vdbe *p;
85934 for(p = db->pVdbe; p; p=p->pVNext){
85935 p->expired = iCode+1;
85936 }
85937 }
85938
85939 /*
@@ -85854,17 +86050,18 @@
86050 **
86051 ** This function is used to free UnpackedRecord structures allocated by
86052 ** the vdbeUnpackRecord() function found in vdbeapi.c.
86053 */
86054 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
86055 assert( db!=0 );
86056 if( p ){
86057 int i;
86058 for(i=0; i<nField; i++){
86059 Mem *pMem = &p->aMem[i];
86060 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
86061 }
86062 sqlite3DbNNFreeNN(db, p);
86063 }
86064 }
86065 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86066
86067 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
@@ -85931,11 +86128,11 @@
86128 if( preupdate.aNew ){
86129 int i;
86130 for(i=0; i<pCsr->nField; i++){
86131 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
86132 }
86133 sqlite3DbNNFreeNN(db, preupdate.aNew);
86134 }
86135 }
86136 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86137
86138 /************** End of vdbeaux.c *********************************************/
@@ -86048,11 +86245,13 @@
86245 Vdbe *v = (Vdbe*)pStmt;
86246 sqlite3 *db = v->db;
86247 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
86248 sqlite3_mutex_enter(db->mutex);
86249 checkProfileCallback(db, v);
86250 assert( v->eVdbeState>=VDBE_READY_STATE );
86251 rc = sqlite3VdbeReset(v);
86252 sqlite3VdbeDelete(v);
86253 rc = sqlite3ApiExit(db, rc);
86254 sqlite3LeaveMutexAndCloseZombie(db);
86255 }
86256 return rc;
86257 }
@@ -87370,11 +87569,11 @@
87569 ** the mutex is released if any kind of error occurs.
87570 **
87571 ** The error code stored in database p->db is overwritten with the return
87572 ** value in any case.
87573 */
87574 static int vdbeUnbind(Vdbe *p, unsigned int i){
87575 Mem *pVar;
87576 if( vdbeSafetyNotNull(p) ){
87577 return SQLITE_MISUSE_BKPT;
87578 }
87579 sqlite3_mutex_enter(p->db->mutex);
@@ -87383,16 +87582,15 @@
87582 sqlite3_mutex_leave(p->db->mutex);
87583 sqlite3_log(SQLITE_MISUSE,
87584 "bind on a busy prepared statement: [%s]", p->zSql);
87585 return SQLITE_MISUSE_BKPT;
87586 }
87587 if( i>=(unsigned int)p->nVar ){
87588 sqlite3Error(p->db, SQLITE_RANGE);
87589 sqlite3_mutex_leave(p->db->mutex);
87590 return SQLITE_RANGE;
87591 }
 
87592 pVar = &p->aVar[i];
87593 sqlite3VdbeMemRelease(pVar);
87594 pVar->flags = MEM_Null;
87595 p->db->errCode = SQLITE_OK;
87596
@@ -87425,11 +87623,11 @@
87623 ){
87624 Vdbe *p = (Vdbe *)pStmt;
87625 Mem *pVar;
87626 int rc;
87627
87628 rc = vdbeUnbind(p, (u32)(i-1));
87629 if( rc==SQLITE_OK ){
87630 if( zData!=0 ){
87631 pVar = &p->aVar[i-1];
87632 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
87633 if( rc==SQLITE_OK && encoding!=0 ){
@@ -87474,11 +87672,11 @@
87672 return bindText(pStmt, i, zData, nData, xDel, 0);
87673 }
87674 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
87675 int rc;
87676 Vdbe *p = (Vdbe *)pStmt;
87677 rc = vdbeUnbind(p, (u32)(i-1));
87678 if( rc==SQLITE_OK ){
87679 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
87680 sqlite3_mutex_leave(p->db->mutex);
87681 }
87682 return rc;
@@ -87487,21 +87685,21 @@
87685 return sqlite3_bind_int64(p, i, (i64)iValue);
87686 }
87687 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
87688 int rc;
87689 Vdbe *p = (Vdbe *)pStmt;
87690 rc = vdbeUnbind(p, (u32)(i-1));
87691 if( rc==SQLITE_OK ){
87692 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
87693 sqlite3_mutex_leave(p->db->mutex);
87694 }
87695 return rc;
87696 }
87697 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
87698 int rc;
87699 Vdbe *p = (Vdbe*)pStmt;
87700 rc = vdbeUnbind(p, (u32)(i-1));
87701 if( rc==SQLITE_OK ){
87702 sqlite3_mutex_leave(p->db->mutex);
87703 }
87704 return rc;
87705 }
@@ -87512,11 +87710,11 @@
87710 const char *zPTtype,
87711 void (*xDestructor)(void*)
87712 ){
87713 int rc;
87714 Vdbe *p = (Vdbe*)pStmt;
87715 rc = vdbeUnbind(p, (u32)(i-1));
87716 if( rc==SQLITE_OK ){
87717 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
87718 sqlite3_mutex_leave(p->db->mutex);
87719 }else if( xDestructor ){
87720 xDestructor(pPtr);
@@ -87590,11 +87788,11 @@
87788 return rc;
87789 }
87790 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
87791 int rc;
87792 Vdbe *p = (Vdbe *)pStmt;
87793 rc = vdbeUnbind(p, (u32)(i-1));
87794 if( rc==SQLITE_OK ){
87795 #ifndef SQLITE_OMIT_INCRBLOB
87796 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87797 #else
87798 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
@@ -87750,11 +87948,11 @@
87948 #endif
87949 sqlite3_mutex_enter(pDb->mutex);
87950 if( pStmt==0 ){
87951 pNext = (sqlite3_stmt*)pDb->pVdbe;
87952 }else{
87953 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
87954 }
87955 sqlite3_mutex_leave(pDb->mutex);
87956 return pNext;
87957 }
87958
@@ -87775,12 +87973,15 @@
87973 if( op==SQLITE_STMTSTATUS_MEMUSED ){
87974 sqlite3 *db = pVdbe->db;
87975 sqlite3_mutex_enter(db->mutex);
87976 v = 0;
87977 db->pnBytesFreed = (int*)&v;
87978 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
87979 db->lookaside.pEnd = db->lookaside.pStart;
87980 sqlite3VdbeDelete(pVdbe);
87981 db->pnBytesFreed = 0;
87982 db->lookaside.pEnd = db->lookaside.pTrueEnd;
87983 sqlite3_mutex_leave(db->mutex);
87984 }else{
87985 v = pVdbe->aCounter[op];
87986 if( resetFlag ) pVdbe->aCounter[op] = 0;
87987 }
@@ -88616,11 +88817,12 @@
88817 ** floating point value of rValue. Return true and set *piValue to the
88818 ** integer value if the string is in range to be an integer. Otherwise,
88819 ** return false.
88820 */
88821 static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
88822 i64 iValue;
88823 iValue = sqlite3RealToI64(rValue);
88824 if( sqlite3RealSameAsInt(rValue,iValue) ){
88825 *piValue = iValue;
88826 return 1;
88827 }
88828 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
@@ -88778,21 +88980,22 @@
88980 **
88981 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
88982 ** But it does set pMem->u.r and pMem->u.i appropriately.
88983 */
88984 static u16 numericType(Mem *pMem){
88985 assert( (pMem->flags & MEM_Null)==0
88986 || pMem->db==0 || pMem->db->mallocFailed );
88987 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
88988 testcase( pMem->flags & MEM_Int );
88989 testcase( pMem->flags & MEM_Real );
88990 testcase( pMem->flags & MEM_IntReal );
88991 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
88992 }
88993 assert( pMem->flags & (MEM_Str|MEM_Blob) );
88994 testcase( pMem->flags & MEM_Str );
88995 testcase( pMem->flags & MEM_Blob );
88996 return computeNumericType(pMem);
 
88997 return 0;
88998 }
88999
89000 #ifdef SQLITE_DEBUG
89001 /*
@@ -90033,25 +90236,24 @@
90236 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
90237 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
90238 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
90239 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
90240 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
 
90241 u16 type1; /* Numeric type of left operand */
90242 u16 type2; /* Numeric type of right operand */
90243 i64 iA; /* Integer value of left operand */
90244 i64 iB; /* Integer value of right operand */
90245 double rA; /* Real value of left operand */
90246 double rB; /* Real value of right operand */
90247
90248 pIn1 = &aMem[pOp->p1];
90249 type1 = pIn1->flags;
90250 pIn2 = &aMem[pOp->p2];
90251 type2 = pIn2->flags;
90252 pOut = &aMem[pOp->p3];
 
90253 if( (type1 & type2 & MEM_Int)!=0 ){
90254 int_math:
90255 iA = pIn1->u.i;
90256 iB = pIn2->u.i;
90257 switch( pOp->opcode ){
90258 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
90259 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
@@ -90069,13 +90271,16 @@
90271 break;
90272 }
90273 }
90274 pOut->u.i = iB;
90275 MemSetTypeFlag(pOut, MEM_Int);
90276 }else if( ((type1 | type2) & MEM_Null)!=0 ){
90277 goto arithmetic_result_is_null;
90278 }else{
90279 type1 = numericType(pIn1);
90280 type2 = numericType(pIn2);
90281 if( (type1 & type2 & MEM_Int)!=0 ) goto int_math;
90282 fp_math:
90283 rA = sqlite3VdbeRealValue(pIn1);
90284 rB = sqlite3VdbeRealValue(pIn2);
90285 switch( pOp->opcode ){
90286 case OP_Add: rB += rA; break;
@@ -90941,15 +91146,18 @@
91146 **
91147 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
91148 ** If it is, then set register P3 to NULL and jump immediately to P2.
91149 ** If P1 is not on a NULL row, then fall through without making any
91150 ** changes.
91151 **
91152 ** If P1 is not an open cursor, then this opcode is a no-op.
91153 */
91154 case OP_IfNullRow: { /* jump */
91155 VdbeCursor *pC;
91156 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91157 pC = p->apCsr[pOp->p1];
91158 if( ALWAYS(pC) && pC->nullRow ){
91159 sqlite3VdbeMemSetNull(aMem + pOp->p3);
91160 goto jump_to_p2;
91161 }
91162 break;
91163 }
@@ -93052,11 +93260,11 @@
93260 **
93261 ** <li> If the cursor is successfully moved to the target row by 0 or more
93262 ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
93263 ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
93264 **
93265 ** <li> If the cursor ends up past the target row (indicating that the target
93266 ** row does not exist in the btree) then jump to SeekOP.P2.
93267 ** </ol>
93268 */
93269 case OP_SeekScan: {
93270 VdbeCursor *pC;
@@ -94388,11 +94596,13 @@
94596 rc = sqlite3VdbeSorterNext(db, pC);
94597 goto next_tail;
94598
94599 case OP_Prev: /* jump */
94600 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94601 assert( pOp->p5==0
94602 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
94603 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
94604 pC = p->apCsr[pOp->p1];
94605 assert( pC!=0 );
94606 assert( pC->deferredMoveto==0 );
94607 assert( pC->eCurType==CURTYPE_BTREE );
94608 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
@@ -94401,11 +94611,13 @@
94611 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
94612 goto next_tail;
94613
94614 case OP_Next: /* jump */
94615 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94616 assert( pOp->p5==0
94617 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
94618 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
94619 pC = p->apCsr[pOp->p1];
94620 assert( pC!=0 );
94621 assert( pC->deferredMoveto==0 );
94622 assert( pC->eCurType==CURTYPE_BTREE );
94623 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
@@ -94608,14 +94820,14 @@
94820
94821 /* The IdxRowid and Seek opcodes are combined because of the commonality
94822 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
94823 rc = sqlite3VdbeCursorRestore(pC);
94824
94825 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
94826 ** since it was last positioned and an error (e.g. OOM or an IO error)
94827 ** occurs while trying to reposition it. */
94828 if( rc!=SQLITE_OK ) goto abort_due_to_error;
94829
94830 if( !pC->nullRow ){
94831 rowid = 0; /* Not needed. Only used to silence a warning. */
94832 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
94833 if( rc!=SQLITE_OK ){
@@ -95513,11 +95725,11 @@
95725
95726 /* Opcode: OffsetLimit P1 P2 P3 * *
95727 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
95728 **
95729 ** This opcode performs a commonly used computation associated with
95730 ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
95731 ** holds the offset counter. The opcode computes the combined value
95732 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
95733 ** value computed is the total number of rows that will need to be
95734 ** visited in order to complete the query.
95735 **
@@ -101110,10 +101322,12 @@
101322 sqlite3_file *pJfd, /* Preallocated, blank file handle */
101323 int flags, /* Opening flags */
101324 int nSpill /* Bytes buffered before opening the file */
101325 ){
101326 MemJournal *p = (MemJournal*)pJfd;
101327
101328 assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) );
101329
101330 /* Zero the file-handle object. If nSpill was passed zero, initialize
101331 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
101332 ** case none of the code in this module is executed as a result of calls
101333 ** made on the journal file-handle. */
@@ -101552,13 +101766,11 @@
101766 if( ExprHasProperty(pExpr, EP_WinFunc) ){
101767 if( ALWAYS(pExpr->y.pWin!=0) ){
101768 pExpr->y.pWin->pOwner = pExpr;
101769 }
101770 }
101771 sqlite3ExprDeferredDelete(pParse, pDup);
 
 
101772 }
101773 }
101774
101775 /*
101776 ** Subqueries stores the original database, table and column names for their
@@ -104363,11 +104575,13 @@
104575 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
104576 ** if appropriate.
104577 */
104578 static void exprSetHeight(Expr *p){
104579 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104580 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
104581 nHeight = p->pRight->nHeight;
104582 }
104583 if( ExprUseXSelect(p) ){
104584 heightOfSelect(p->x.pSelect, &nHeight);
104585 }else if( p->x.pList ){
104586 heightOfExprList(p->x.pList, &nHeight);
104587 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
@@ -104506,19 +104720,30 @@
104720 if( pRoot==0 ){
104721 assert( db->mallocFailed );
104722 sqlite3ExprDelete(db, pLeft);
104723 sqlite3ExprDelete(db, pRight);
104724 }else{
104725 assert( ExprUseXList(pRoot) );
104726 assert( pRoot->x.pSelect==0 );
104727 if( pRight ){
104728 pRoot->pRight = pRight;
104729 pRoot->flags |= EP_Propagate & pRight->flags;
104730 #if SQLITE_MAX_EXPR_DEPTH>0
104731 pRoot->nHeight = pRight->nHeight+1;
104732 }else{
104733 pRoot->nHeight = 1;
104734 #endif
104735 }
104736 if( pLeft ){
104737 pRoot->pLeft = pLeft;
104738 pRoot->flags |= EP_Propagate & pLeft->flags;
104739 #if SQLITE_MAX_EXPR_DEPTH>0
104740 if( pLeft->nHeight>=pRoot->nHeight ){
104741 pRoot->nHeight = pLeft->nHeight+1;
104742 }
104743 #endif
104744 }
 
104745 }
104746 }
104747
104748 /*
104749 ** Allocate an Expr node which joins as many as two subtrees.
@@ -104800,10 +105025,11 @@
105025 /*
105026 ** Recursively delete an expression tree.
105027 */
105028 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
105029 assert( p!=0 );
105030 assert( db!=0 );
105031 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
105032 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
105033 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
105034 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
105035 #ifdef SQLITE_DEBUG
@@ -104831,16 +105057,12 @@
105057 sqlite3WindowDelete(db, p->y.pWin);
105058 }
105059 #endif
105060 }
105061 }
 
 
 
 
105062 if( !ExprHasProperty(p, EP_Static) ){
105063 sqlite3DbNNFreeNN(db, p);
105064 }
105065 }
105066 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
105067 if( p ) sqlite3ExprDeleteNN(db, p);
105068 }
@@ -104867,12 +105089,13 @@
105089 **
105090 ** The deferred delete is (currently) implemented by adding the
105091 ** pExpr to the pParse->pConstExpr list with a register number of 0.
105092 */
105093 SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
105094 sqlite3ParserAddCleanup(pParse,
105095 (void(*)(sqlite3*,void*))sqlite3ExprDelete,
105096 pExpr);
105097 }
105098
105099 /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
105100 ** expression.
105101 */
@@ -104942,11 +105165,10 @@
105165 ){
105166 nSize = EXPR_FULLSIZE;
105167 }else{
105168 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
105169 assert( !ExprHasProperty(p, EP_OuterON) );
 
105170 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
105171 if( p->pLeft || p->x.pList ){
105172 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
105173 }else{
105174 assert( p->pRight==0 );
@@ -105046,11 +105268,11 @@
105268 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
105269 }
105270 }
105271
105272 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
105273 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
105274 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
105275 pNew->flags |= staticFlag;
105276 ExprClearVVAProperties(pNew);
105277 if( dupFlags ){
105278 ExprSetVVAProperty(pNew, EP_Immutable);
@@ -105622,16 +105844,17 @@
105844 */
105845 static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
105846 int i = pList->nExpr;
105847 struct ExprList_item *pItem = pList->a;
105848 assert( pList->nExpr>0 );
105849 assert( db!=0 );
105850 do{
105851 sqlite3ExprDelete(db, pItem->pExpr);
105852 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
105853 pItem++;
105854 }while( --i>0 );
105855 sqlite3DbNNFreeNN(db, pList);
105856 }
105857 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
105858 if( pList ) exprListDeleteNN(db, pList);
105859 }
105860
@@ -106918,11 +107141,11 @@
107141 if( pLimit ){
107142 pLimit->affExpr = SQLITE_AFF_NUMERIC;
107143 pLimit = sqlite3PExpr(pParse, TK_NE,
107144 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
107145 }
107146 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
107147 pSel->pLimit->pLeft = pLimit;
107148 }else{
107149 /* If there is no pre-existing limit add a limit of 1 */
107150 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
107151 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
@@ -107432,11 +107655,11 @@
107655 u8 p5 /* P5 value for OP_Column + FLAGS */
107656 ){
107657 assert( pParse->pVdbe!=0 );
107658 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107659 if( p5 ){
107660 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
107661 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
107662 }
107663 return iReg;
107664 }
107665
@@ -107501,11 +107724,11 @@
107724 /*
107725 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107726 ** so that a subsequent copy will not be merged into this one.
107727 */
107728 static void setDoNotMergeFlagOnCopy(Vdbe *v){
107729 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
107730 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */
107731 }
107732 }
107733
107734 /*
@@ -107672,11 +107895,11 @@
107895 Table *pTab = pCol->pTab;
107896 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
107897 pCol->iSorterColumn, target);
107898 if( pCol->iColumn<0 ){
107899 VdbeComment((v,"%s.rowid",pTab->zName));
107900 }else if( ALWAYS(pTab!=0) ){
107901 VdbeComment((v,"%s.%s",
107902 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107903 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
107904 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
107905 }
@@ -108267,10 +108490,25 @@
108490 ** on a LEFT JOIN NULL row.
108491 */
108492 case TK_IF_NULL_ROW: {
108493 int addrINR;
108494 u8 okConstFactor = pParse->okConstFactor;
108495 AggInfo *pAggInfo = pExpr->pAggInfo;
108496 if( pAggInfo ){
108497 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
108498 if( !pAggInfo->directMode ){
108499 inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
108500 break;
108501 }
108502 if( pExpr->pAggInfo->useSortingIdx ){
108503 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
108504 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
108505 target);
108506 inReg = target;
108507 break;
108508 }
108509 }
108510 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108511 /* Temporarily disable factoring of constant expressions, since
108512 ** even though expressions may appear to be constant, they are not
108513 ** really constant because they originate from the right-hand side
108514 ** of a LEFT JOIN. */
@@ -108608,11 +108846,11 @@
108846 }else{
108847 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
108848 if( inReg!=target+i ){
108849 VdbeOp *pOp;
108850 if( copyOp==OP_Copy
108851 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
108852 && pOp->p1+pOp->p3+1==inReg
108853 && pOp->p2+pOp->p3+1==target+i
108854 && pOp->p5==0 /* The do-not-merge flag must be clear */
108855 ){
108856 pOp->p3++;
@@ -109644,10 +109882,11 @@
109882 ** fact is exploited for efficiency.
109883 */
109884 SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
109885 Walker w;
109886 struct RefSrcList x;
109887 assert( pParse->db!=0 );
109888 memset(&w, 0, sizeof(w));
109889 memset(&x, 0, sizeof(x));
109890 w.xExprCallback = exprRefToSrcList;
109891 w.xSelectCallback = selectRefEnter;
109892 w.xSelectCallback2 = selectRefLeave;
@@ -109660,11 +109899,11 @@
109899 #ifndef SQLITE_OMIT_WINDOWFUNC
109900 if( ExprHasProperty(pExpr, EP_WinFunc) ){
109901 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109902 }
109903 #endif
109904 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
109905 if( w.eCode & 0x01 ){
109906 return 1;
109907 }else if( w.eCode ){
109908 return 0;
109909 }else{
@@ -109691,21 +109930,22 @@
109930 ){
109931 AggInfo *pAggInfo = pExpr->pAggInfo;
109932 int iAgg = pExpr->iAgg;
109933 Parse *pParse = pWalker->pParse;
109934 sqlite3 *db = pParse->db;
109935 if( pExpr->op!=TK_AGG_FUNCTION ){
109936 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_IF_NULL_ROW );
109937 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
109938 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109939 pExpr = sqlite3ExprDup(db, pExpr, 0);
109940 if( pExpr ){
109941 pAggInfo->aCol[iAgg].pCExpr = pExpr;
109942 sqlite3ExprDeferredDelete(pParse, pExpr);
109943 }
109944 }
109945 }else{
109946 assert( pExpr->op==TK_AGG_FUNCTION );
109947 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
109948 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109949 pExpr = sqlite3ExprDup(db, pExpr, 0);
109950 if( pExpr ){
109951 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
@@ -109772,14 +110012,16 @@
110012 SrcList *pSrcList = pNC->pSrcList;
110013 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
110014
110015 assert( pNC->ncFlags & NC_UAggInfo );
110016 switch( pExpr->op ){
110017 case TK_IF_NULL_ROW:
110018 case TK_AGG_COLUMN:
110019 case TK_COLUMN: {
110020 testcase( pExpr->op==TK_AGG_COLUMN );
110021 testcase( pExpr->op==TK_COLUMN );
110022 testcase( pExpr->op==TK_IF_NULL_ROW );
110023 /* Check to see if the column is in one of the tables in the FROM
110024 ** clause of the aggregate query */
110025 if( ALWAYS(pSrcList!=0) ){
110026 SrcItem *pItem = pSrcList->a;
110027 for(i=0; i<pSrcList->nSrc; i++, pItem++){
@@ -109793,12 +110035,14 @@
110035 ** is not an entry there already.
110036 */
110037 int k;
110038 pCol = pAggInfo->aCol;
110039 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
110040 if( pCol->iTable==pExpr->iTable
110041 && pCol->iColumn==pExpr->iColumn
110042 && pExpr->op!=TK_IF_NULL_ROW
110043 ){
110044 break;
110045 }
110046 }
110047 if( (k>=pAggInfo->nColumn)
110048 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
@@ -109809,19 +110053,21 @@
110053 pCol->iTable = pExpr->iTable;
110054 pCol->iColumn = pExpr->iColumn;
110055 pCol->iMem = ++pParse->nMem;
110056 pCol->iSorterColumn = -1;
110057 pCol->pCExpr = pExpr;
110058 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
110059 int j, n;
110060 ExprList *pGB = pAggInfo->pGroupBy;
110061 struct ExprList_item *pTerm = pGB->a;
110062 n = pGB->nExpr;
110063 for(j=0; j<n; j++, pTerm++){
110064 Expr *pE = pTerm->pExpr;
110065 if( pE->op==TK_COLUMN
110066 && pE->iTable==pExpr->iTable
110067 && pE->iColumn==pExpr->iColumn
110068 ){
110069 pCol->iSorterColumn = j;
110070 break;
110071 }
110072 }
110073 }
@@ -109834,11 +110080,13 @@
110080 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
110081 ** pAggInfo->aCol[] entry.
110082 */
110083 ExprSetVVAProperty(pExpr, EP_NoReduce);
110084 pExpr->pAggInfo = pAggInfo;
110085 if( pExpr->op==TK_COLUMN ){
110086 pExpr->op = TK_AGG_COLUMN;
110087 }
110088 pExpr->iAgg = (i16)k;
110089 break;
110090 } /* endif pExpr->iTable==pItem->iCursor */
110091 } /* end loop over pSrcList */
110092 }
@@ -115256,10 +115504,11 @@
115504 ** no VDBE code was generated.
115505 */
115506 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
115507 sqlite3 *db;
115508 Vdbe *v;
115509 int iDb, i;
115510
115511 assert( pParse->pToplevel==0 );
115512 db = pParse->db;
115513 assert( db->pParse==pParse );
115514 if( pParse->nested ) return;
@@ -115285,11 +115534,10 @@
115534 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
115535 if( v ){
115536 if( pParse->bReturning ){
115537 Returning *pReturning = pParse->u1.pReturning;
115538 int addrRewind;
 
115539 int reg;
115540
115541 if( pReturning->nRetCol ){
115542 sqlite3VdbeAddOp0(v, OP_FkCheck);
115543 addrRewind =
@@ -115322,80 +115570,73 @@
115570 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
115571 ** set for each database that is used. Generate code to start a
115572 ** transaction on each used database and to verify the schema cookie
115573 ** on each used database.
115574 */
115575 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115576 sqlite3VdbeJumpHere(v, 0);
115577 assert( db->nDb>0 );
115578 iDb = 0;
115579 do{
115580 Schema *pSchema;
115581 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115582 sqlite3VdbeUsesBtree(v, iDb);
115583 pSchema = db->aDb[iDb].pSchema;
115584 sqlite3VdbeAddOp4Int(v,
115585 OP_Transaction, /* Opcode */
115586 iDb, /* P1 */
115587 DbMaskTest(pParse->writeMask,iDb), /* P2 */
115588 pSchema->schema_cookie, /* P3 */
115589 pSchema->iGeneration /* P4 */
115590 );
115591 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115592 VdbeComment((v,
115593 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115594 }while( ++iDb<db->nDb );
 
 
 
 
115595 #ifndef SQLITE_OMIT_VIRTUALTABLE
115596 for(i=0; i<pParse->nVtabLock; i++){
115597 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115598 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
115599 }
115600 pParse->nVtabLock = 0;
115601 #endif
115602
115603 /* Once all the cookies have been verified and transactions opened,
115604 ** obtain the required table-locks. This is a no-op unless the
115605 ** shared-cache feature is enabled.
115606 */
115607 codeTableLocks(pParse);
115608
115609 /* Initialize any AUTOINCREMENT data structures required.
115610 */
115611 sqlite3AutoincrementBegin(pParse);
115612
115613 /* Code constant expressions that where factored out of inner loops.
115614 **
115615 ** The pConstExpr list might also contain expressions that we simply
115616 ** want to keep around until the Parse object is deleted. Such
115617 ** expressions have iConstExprReg==0. Do not generate code for
115618 ** those expressions, of course.
115619 */
115620 if( pParse->pConstExpr ){
115621 ExprList *pEL = pParse->pConstExpr;
115622 pParse->okConstFactor = 0;
115623 for(i=0; i<pEL->nExpr; i++){
115624 int iReg = pEL->a[i].u.iConstExprReg;
115625 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115626 }
115627 }
115628
115629 if( pParse->bReturning ){
115630 Returning *pRet = pParse->u1.pReturning;
115631 if( pRet->nRetCol ){
115632 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115633 }
115634 }
115635
115636 /* Finally, jump back to the beginning of the executable code. */
115637 sqlite3VdbeGoto(v, 1);
 
 
 
115638 }
115639
115640 /* Get the VDBE program ready for execution
115641 */
115642 assert( v!=0 || pParse->nErr );
@@ -115898,20 +116139,21 @@
116139 */
116140 SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
116141 int i;
116142 Column *pCol;
116143 assert( pTable!=0 );
116144 assert( db!=0 );
116145 if( (pCol = pTable->aCol)!=0 ){
116146 for(i=0; i<pTable->nCol; i++, pCol++){
116147 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
116148 sqlite3DbFree(db, pCol->zCnName);
116149 }
116150 sqlite3DbNNFreeNN(db, pTable->aCol);
116151 if( IsOrdinaryTable(pTable) ){
116152 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
116153 }
116154 if( db->pnBytesFreed==0 ){
116155 pTable->aCol = 0;
116156 pTable->nCol = 0;
116157 if( IsOrdinaryTable(pTable) ){
116158 pTable->u.tab.pDfltList = 0;
116159 }
@@ -115944,21 +116186,22 @@
116186 **
116187 ** If malloc has already failed, it may be that it failed while allocating
116188 ** a Table object that was going to be marked ephemeral. So do not check
116189 ** that no lookaside memory is used in this case either. */
116190 int nLookaside = 0;
116191 assert( db!=0 );
116192 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
116193 nLookaside = sqlite3LookasideUsed(db, 0);
116194 }
116195 #endif
116196
116197 /* Delete all indices associated with this table. */
116198 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
116199 pNext = pIndex->pNext;
116200 assert( pIndex->pSchema==pTable->pSchema
116201 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
116202 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
116203 char *zName = pIndex->zName;
116204 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
116205 &pIndex->pSchema->idxHash, zName, 0
116206 );
116207 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
@@ -115991,12 +116234,13 @@
116234 /* Verify that no lookaside memory was used by schema tables */
116235 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
116236 }
116237 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
116238 /* Do not delete the table until the reference count reaches zero. */
116239 assert( db!=0 );
116240 if( !pTable ) return;
116241 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
116242 deleteTable(db, pTable);
116243 }
116244
116245
116246 /*
@@ -118165,11 +118409,11 @@
118409 /*
118410 ** The Table structure pTable is really a VIEW. Fill in the names of
118411 ** the columns of the view in the pTable structure. Return the number
118412 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118413 */
118414 static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
118415 Table *pSelTab; /* A fake table from which we get the result set */
118416 Select *pSel; /* Copy of the SELECT that implements the view */
118417 int nErr = 0; /* Number of errors encountered */
118418 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118419 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -118190,13 +118434,14 @@
118434 }
118435 #endif
118436
118437 #ifndef SQLITE_OMIT_VIEW
118438 /* A positive nCol means the columns names for this view are
118439 ** already known. This routine is not called unless either the
118440 ** table is virtual or nCol is zero.
118441 */
118442 assert( pTable->nCol<=0 );
118443
118444 /* A negative nCol is a special marker meaning that we are currently
118445 ** trying to compute the column names. If we enter this routine with
118446 ** a negative nCol, it means two or more views form a loop, like this:
118447 **
@@ -118287,10 +118532,15 @@
118532 if( db->mallocFailed ){
118533 sqlite3DeleteColumnNames(db, pTable);
118534 }
118535 #endif /* SQLITE_OMIT_VIEW */
118536 return nErr;
118537 }
118538 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
118539 assert( pTable!=0 );
118540 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
118541 return viewGetColumnNames(pParse, pTable);
118542 }
118543 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
118544
118545 #ifndef SQLITE_OMIT_VIEW
118546 /*
@@ -119153,11 +119403,11 @@
119403 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119404 goto exit_create_index;
119405 }
119406 if( !IN_RENAME_OBJECT ){
119407 if( !db->init.busy ){
119408 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
119409 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
119410 goto exit_create_index;
119411 }
119412 }
119413 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
@@ -119806,16 +120056,17 @@
120056 /*
120057 ** Delete an IdList.
120058 */
120059 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
120060 int i;
120061 assert( db!=0 );
120062 if( pList==0 ) return;
120063 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
120064 for(i=0; i<pList->nId; i++){
120065 sqlite3DbFree(db, pList->a[i].zName);
120066 }
120067 sqlite3DbNNFreeNN(db, pList);
120068 }
120069
120070 /*
120071 ** Return the index in pList of the identifier named zId. Return -1
120072 ** if not found.
@@ -120014,15 +120265,16 @@
120265 ** Delete an entire SrcList including all its substructure.
120266 */
120267 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
120268 int i;
120269 SrcItem *pItem;
120270 assert( db!=0 );
120271 if( pList==0 ) return;
120272 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120273 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
120274 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
120275 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
120276 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120277 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120278 sqlite3DeleteTable(db, pItem->pTab);
120279 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120280 if( pItem->fg.isUsing ){
@@ -120029,11 +120281,11 @@
120281 sqlite3IdListDelete(db, pItem->u3.pUsing);
120282 }else if( pItem->u3.pOn ){
120283 sqlite3ExprDelete(db, pItem->u3.pOn);
120284 }
120285 }
120286 sqlite3DbNNFreeNN(db, pList);
120287 }
120288
120289 /*
120290 ** This routine is called by the parser to add a new term to the
120291 ** end of a growing FROM clause. The "p" parameter is the part of
@@ -121281,23 +121533,25 @@
121533 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
121534 Hash temp1;
121535 Hash temp2;
121536 HashElem *pElem;
121537 Schema *pSchema = (Schema *)p;
121538 sqlite3 xdb;
121539
121540 memset(&xdb, 0, sizeof(xdb));
121541 temp1 = pSchema->tblHash;
121542 temp2 = pSchema->trigHash;
121543 sqlite3HashInit(&pSchema->trigHash);
121544 sqlite3HashClear(&pSchema->idxHash);
121545 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
121546 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
121547 }
121548 sqlite3HashClear(&temp2);
121549 sqlite3HashInit(&pSchema->tblHash);
121550 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
121551 Table *pTab = sqliteHashData(pElem);
121552 sqlite3DeleteTable(&xdb, pTab);
121553 }
121554 sqlite3HashClear(&temp1);
121555 sqlite3HashClear(&pSchema->fkeyHash);
121556 pSchema->pSeqTab = 0;
121557 if( pSchema->schemaFlags & DB_SchemaLoaded ){
@@ -121392,22 +121646,46 @@
121646 ** A table is read-only if any of the following are true:
121647 **
121648 ** 1) It is a virtual table and no implementation of the xUpdate method
121649 ** has been provided
121650 **
121651 ** 2) A trigger is currently being coded and the table is a virtual table
121652 ** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
121653 ** the table is not SQLITE_VTAB_INNOCUOUS.
121654 **
121655 ** 3) It is a system table (i.e. sqlite_schema), this call is not
121656 ** part of a nested parse and writable_schema pragma has not
121657 ** been specified
121658 **
121659 ** 4) The table is a shadow table, the database connection is in
121660 ** defensive mode, and the current sqlite3_prepare()
121661 ** is for a top-level SQL statement.
121662 */
121663 static int vtabIsReadOnly(Parse *pParse, Table *pTab){
121664 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
121665 return 1;
121666 }
121667
121668 /* Within triggers:
121669 ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
121670 ** virtual tables
121671 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
121672 ** virtual tables if PRAGMA trusted_schema=ON.
121673 */
121674 if( pParse->pToplevel!=0
121675 && pTab->u.vtab.p->eVtabRisk >
121676 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
121677 ){
121678 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
121679 pTab->zName);
121680 }
121681 return 0;
121682 }
121683 static int tabIsReadOnly(Parse *pParse, Table *pTab){
121684 sqlite3 *db;
121685 if( IsVirtual(pTab) ){
121686 return vtabIsReadOnly(pParse, pTab);
121687 }
121688 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
121689 db = pParse->db;
121690 if( (pTab->tabFlags & TF_Readonly)!=0 ){
121691 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
@@ -121415,13 +121693,15 @@
121693 assert( pTab->tabFlags & TF_Shadow );
121694 return sqlite3ReadOnlyShadowTables(db);
121695 }
121696
121697 /*
121698 ** Check to make sure the given table is writable.
121699 **
121700 ** If pTab is not writable -> generate an error message and return 1.
121701 ** If pTab is writable but other errors have occurred -> return 1.
121702 ** If pTab is writable and no prior errors -> return 0;
121703 */
121704 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
121705 if( tabIsReadOnly(pParse, pTab) ){
121706 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121707 return 1;
@@ -121778,13 +122058,14 @@
122058 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
122059 pTab->zName, P4_STATIC);
122060 }
122061 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122062 assert( pIdx->pSchema==pTab->pSchema );
 
122063 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
122064 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
122065 }else{
122066 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
122067 }
122068 }
122069 }else
122070 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
122071 {
@@ -121980,11 +122261,11 @@
122261 sqlite3ExprDelete(db, pWhere);
122262 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
122263 sqlite3ExprListDelete(db, pOrderBy);
122264 sqlite3ExprDelete(db, pLimit);
122265 #endif
122266 if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen);
122267 return;
122268 }
122269 /* Make sure "isView" and other macros defined above are undefined. Otherwise
122270 ** they may interfere with compilation of other functions in this file
122271 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -126148,15 +126429,16 @@
126429 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
126430 FKey *pFKey; /* Iterator variable */
126431 FKey *pNext; /* Copy of pFKey->pNextFrom */
126432
126433 assert( IsOrdinaryTable(pTab) );
126434 assert( db!=0 );
126435 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126436 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
126437
126438 /* Remove the FK from the fkeyHash hash table. */
126439 if( db->pnBytesFreed==0 ){
126440 if( pFKey->pPrevTo ){
126441 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126442 }else{
126443 void *p = (void *)pFKey->pNextTo;
126444 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
@@ -126345,11 +126627,11 @@
126627 /* Move the previous opcode (which should be OP_MakeRecord) forward
126628 ** by one slot and insert a new OP_TypeCheck where the current
126629 ** OP_MakeRecord is found */
126630 VdbeOp *pPrev;
126631 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
126632 pPrev = sqlite3VdbeGetLastOp(v);
126633 assert( pPrev!=0 );
126634 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
126635 pPrev->opcode = OP_TypeCheck;
126636 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
126637 }else{
@@ -126383,11 +126665,11 @@
126665 i = sqlite3Strlen30NN(zColAff);
126666 if( i ){
126667 if( iReg ){
126668 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
126669 }else{
126670 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
126671 || sqlite3VdbeDb(v)->mallocFailed );
126672 sqlite3VdbeChangeP4(v, -1, zColAff, i);
126673 }
126674 }
126675 }
@@ -126469,11 +126751,11 @@
126751 /* Before computing generated columns, first go through and make sure
126752 ** that appropriate affinity has been applied to the regular columns
126753 */
126754 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126755 if( (pTab->tabFlags & TF_HasStored)!=0 ){
126756 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
126757 if( pOp->opcode==OP_Affinity ){
126758 /* Change the OP_Affinity argument to '@' (NONE) for all stored
126759 ** columns. '@' is the no-op affinity and those columns have not
126760 ** yet been computed. */
126761 int ii, jj;
@@ -127375,11 +127657,16 @@
127657 }else if( pSelect ){
127658 if( regFromSelect!=regData ){
127659 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
127660 }
127661 }else{
127662 Expr *pX = pList->a[k].pExpr;
127663 int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
127664 if( y!=iRegStore ){
127665 sqlite3VdbeAddOp2(v,
127666 ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore);
127667 }
127668 }
127669 }
127670
127671
127672 /* Run the BEFORE and INSTEAD OF triggers, if there are any
@@ -127512,11 +127799,13 @@
127799 int isReplace = 0;/* Set to true if constraints may cause a replace */
127800 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
127801 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
127802 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
127803 );
127804 if( db->flags & SQLITE_ForeignKeys ){
127805 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
127806 }
127807
127808 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
127809 ** constraints or (b) there are no triggers and this table is not a
127810 ** parent table in a foreign key constraint. It is safe to set the
127811 ** flag in the second case as if any REPLACE constraint is hit, an
@@ -127596,11 +127885,11 @@
127885 sqlite3SrcListDelete(db, pTabList);
127886 sqlite3ExprListDelete(db, pList);
127887 sqlite3UpsertDelete(db, pUpsert);
127888 sqlite3SelectDelete(db, pSelect);
127889 sqlite3IdListDelete(db, pColumn);
127890 if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
127891 }
127892
127893 /* Make sure "isView" and other macros defined above are undefined. Otherwise
127894 ** they may interfere with compilation of other functions in this file
127895 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -132702,19 +132991,21 @@
132991 ** Setting to a null string reverts to the default temporary directory search.
132992 ** If temporary directory is changed, then invalidateTempStorage.
132993 **
132994 */
132995 case PragTyp_TEMP_STORE_DIRECTORY: {
132996 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132997 if( !zRight ){
132998 returnSingleText(v, sqlite3_temp_directory);
132999 }else{
133000 #ifndef SQLITE_OMIT_WSD
133001 if( zRight[0] ){
133002 int res;
133003 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
133004 if( rc!=SQLITE_OK || res==0 ){
133005 sqlite3ErrorMsg(pParse, "not a writable directory");
133006 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
133007 goto pragma_out;
133008 }
133009 }
133010 if( SQLITE_TEMP_STORE==0
133011 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
@@ -132728,10 +133019,11 @@
133019 }else{
133020 sqlite3_temp_directory = 0;
133021 }
133022 #endif /* SQLITE_OMIT_WSD */
133023 }
133024 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
133025 break;
133026 }
133027
133028 #if SQLITE_OS_WIN
133029 /*
@@ -132746,19 +133038,21 @@
133038 ** process. Database file specified with an absolute path are not impacted
133039 ** by this setting, regardless of its value.
133040 **
133041 */
133042 case PragTyp_DATA_STORE_DIRECTORY: {
133043 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
133044 if( !zRight ){
133045 returnSingleText(v, sqlite3_data_directory);
133046 }else{
133047 #ifndef SQLITE_OMIT_WSD
133048 if( zRight[0] ){
133049 int res;
133050 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
133051 if( rc!=SQLITE_OK || res==0 ){
133052 sqlite3ErrorMsg(pParse, "not a writable directory");
133053 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
133054 goto pragma_out;
133055 }
133056 }
133057 sqlite3_free(sqlite3_data_directory);
133058 if( zRight[0] ){
@@ -132766,10 +133060,11 @@
133060 }else{
133061 sqlite3_data_directory = 0;
133062 }
133063 #endif /* SQLITE_OMIT_WSD */
133064 }
133065 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
133066 break;
133067 }
133068 #endif
133069
133070 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -133479,19 +133774,27 @@
133774 /* Make sure all the indices are constructed correctly.
133775 */
133776 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
133777 Table *pTab = sqliteHashData(x);
133778 Index *pIdx, *pPk;
133779 Index *pPrior = 0; /* Previous index */
133780 int loopTop;
133781 int iDataCur, iIdxCur;
133782 int r1 = -1;
133783 int bStrict;
133784 int r2; /* Previous key for WITHOUT ROWID tables */
133785
133786 if( !IsOrdinaryTable(pTab) ) continue;
133787 if( pObjTab && pObjTab!=pTab ) continue;
133788 if( isQuick || HasRowid(pTab) ){
133789 pPk = 0;
133790 r2 = 0;
133791 }else{
133792 pPk = sqlite3PrimaryKeyIndex(pTab);
133793 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
133794 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
133795 }
133796 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
133797 1, 0, &iDataCur, &iIdxCur);
133798 /* reg[7] counts the number of entries in the table.
133799 ** reg[8+i] counts the number of entries in the i-th index
133800 */
@@ -133506,10 +133809,28 @@
133809 if( !isQuick ){
133810 /* Sanity check on record header decoding */
133811 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133812 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133813 VdbeComment((v, "(right-most column)"));
133814 if( pPk ){
133815 /* Verify WITHOUT ROWID keys are in ascending order */
133816 int a1;
133817 char *zErr;
133818 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
133819 VdbeCoverage(v);
133820 sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v);
133821 zErr = sqlite3MPrintf(db,
133822 "row not in PRIMARY KEY order for %s",
133823 pTab->zName);
133824 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133825 integrityCheckResultRow(v);
133826 sqlite3VdbeJumpHere(v, a1);
133827 sqlite3VdbeJumpHere(v, a1+1);
133828 for(j=0; j<pPk->nKeyCol; j++){
133829 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
133830 }
133831 }
133832 }
133833 /* Verify that all NOT NULL columns really are NOT NULL. At the
133834 ** same time verify the type of the content of STRICT tables */
133835 bStrict = (pTab->tabFlags & TF_Strict)!=0;
133836 for(j=0; j<pTab->nCol; j++){
@@ -133518,11 +133839,11 @@
133839 int doError, jmp2;
133840 if( j==pTab->iPKey ) continue;
133841 if( pCol->notNull==0 && !bStrict ) continue;
133842 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133843 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133844 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){
133845 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133846 }
133847 if( pCol->notNull ){
133848 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
133849 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
@@ -133533,13 +133854,11 @@
133854 }else{
133855 integrityCheckResultRow(v);
133856 }
133857 sqlite3VdbeJumpHere(v, jmp2);
133858 }
133859 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
 
 
133860 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133861 sqlite3StdTypeMap[pCol->eCType-1]);
133862 VdbeCoverage(v);
133863 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133864 sqlite3StdType[pCol->eCType-1],
@@ -133634,10 +133953,13 @@
133953 sqlite3VdbeLoadString(v, 4, pIdx->zName);
133954 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
133955 integrityCheckResultRow(v);
133956 sqlite3VdbeJumpHere(v, addr);
133957 }
133958 if( pPk ){
133959 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
133960 }
133961 }
133962 }
133963 }
133964 {
133965 static const int iLn = VDBE_OFFSET_LINENO(2);
@@ -135032,19 +135354,19 @@
135354 sqlite3 *db = pParse->db;
135355 assert( db!=0 );
135356 assert( db->pParse==pParse );
135357 assert( pParse->nested==0 );
135358 #ifndef SQLITE_OMIT_SHARED_CACHE
135359 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
135360 #endif
135361 while( pParse->pCleanup ){
135362 ParseCleanup *pCleanup = pParse->pCleanup;
135363 pParse->pCleanup = pCleanup->pNext;
135364 pCleanup->xCleanup(db, pCleanup->pPtr);
135365 sqlite3DbNNFreeNN(db, pCleanup);
135366 }
135367 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
135368 if( pParse->pConstExpr ){
135369 sqlite3ExprListDelete(db, pParse->pConstExpr);
135370 }
135371 assert( db->lookaside.bDisable >= pParse->disableLookaside );
135372 db->lookaside.bDisable -= pParse->disableLookaside;
@@ -135599,10 +135921,11 @@
135921 **
135922 ** If bFree==1, call sqlite3DbFree() on the p object.
135923 ** If bFree==0, Leave the first Select object unfreed
135924 */
135925 static void clearSelect(sqlite3 *db, Select *p, int bFree){
135926 assert( db!=0 );
135927 while( p ){
135928 Select *pPrior = p->pPrior;
135929 sqlite3ExprListDelete(db, p->pEList);
135930 sqlite3SrcListDelete(db, p->pSrc);
135931 sqlite3ExprDelete(db, p->pWhere);
@@ -135618,11 +135941,11 @@
135941 while( p->pWin ){
135942 assert( p->pWin->ppThis==&p->pWin );
135943 sqlite3WindowUnlinkFromSelect(p->pWin);
135944 }
135945 #endif
135946 if( bFree ) sqlite3DbNNFreeNN(db, p);
135947 p = pPrior;
135948 bFree = 1;
135949 }
135950 }
135951
@@ -137024,13 +137347,14 @@
137347 /*
137348 ** Deallocate a KeyInfo object
137349 */
137350 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
137351 if( p ){
137352 assert( p->db!=0 );
137353 assert( p->nRef>0 );
137354 p->nRef--;
137355 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
137356 }
137357 }
137358
137359 /*
137360 ** Make a new pointer to a KeyInfo object
@@ -137211,18 +137535,21 @@
137535 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
137536 nKey+1+nColumn+nRefKey);
137537 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137538 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
137539 VdbeCoverage(v);
137540 assert( p->iLimit==0 && p->iOffset==0 );
137541 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
137542 bSeq = 0;
137543 }else{
137544 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
137545 codeOffset(v, p->iOffset, addrContinue);
137546 iSortTab = iTab;
137547 bSeq = 1;
137548 if( p->iOffset>0 ){
137549 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
137550 }
137551 }
137552 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137553 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
137554 if( aOutEx[i].fg.bSorterRef ) continue;
137555 #endif
@@ -137343,13 +137670,10 @@
137670
137671 /*
137672 ** Return a pointer to a string containing the 'declaration type' of the
137673 ** expression pExpr. The string may be treated as static by the caller.
137674 **
 
 
 
137675 ** The declaration type is the exact datatype definition extracted from the
137676 ** original CREATE TABLE statement if the expression is a column. The
137677 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
137678 ** is considered a column can be complex in the presence of subqueries. The
137679 ** result-set expression in all of the following SELECT statements is
@@ -139211,14 +139535,15 @@
139535
139536 /* Jump to the this point in order to terminate the query.
139537 */
139538 sqlite3VdbeResolveLabel(v, labelEnd);
139539
139540 /* Reassemble the compound query so that it will be freed correctly
139541 ** by the calling function */
139542 if( pSplit->pPrior ){
139543 sqlite3ParserAddCleanup(pParse,
139544 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
139545 }
139546 pSplit->pPrior = pPrior;
139547 pPrior->pNext = pSplit;
139548 sqlite3ExprListDelete(db, pPrior->pOrderBy);
139549 pPrior->pOrderBy = 0;
@@ -139324,10 +139649,11 @@
139649 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
139650 memset(&ifNullRow, 0, sizeof(ifNullRow));
139651 ifNullRow.op = TK_IF_NULL_ROW;
139652 ifNullRow.pLeft = pCopy;
139653 ifNullRow.iTable = pSubst->iNewTable;
139654 ifNullRow.iColumn = -99;
139655 ifNullRow.flags = EP_IfNullRow;
139656 pCopy = &ifNullRow;
139657 }
139658 testcase( ExprHasProperty(pCopy, EP_Subquery) );
139659 pNew = sqlite3ExprDup(db, pCopy, 0);
@@ -139591,11 +139917,12 @@
139917 **
139918 ** (3) If the subquery is the right operand of a LEFT JOIN then
139919 ** (3a) the subquery may not be a join and
139920 ** (3b) the FROM clause of the subquery may not contain a virtual
139921 ** table and
139922 ** (**) Was: "The outer query may not have a GROUP BY." This case
139923 ** is now managed correctly
139924 ** (3d) the outer query may not be DISTINCT.
139925 ** See also (26) for restrictions on RIGHT JOIN.
139926 **
139927 ** (4) The subquery can not be DISTINCT.
139928 **
@@ -139803,20 +140130,14 @@
140130 **
140131 ** (t1 LEFT OUTER JOIN t2) JOIN t3
140132 **
140133 ** which is not at all the same thing.
140134 **
 
 
 
 
 
140135 ** See also tickets #306, #350, and #3300.
140136 */
140137 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
140138 if( pSubSrc->nSrc>1 /* (3a) */
 
140139 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
140140 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
140141 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
140142 ){
140143 return 0;
@@ -140733,10 +141054,11 @@
141054 if( p->pWhere
141055 || p->pEList->nExpr!=1
141056 || p->pSrc->nSrc!=1
141057 || p->pSrc->a[0].pSelect
141058 || pAggInfo->nFunc!=1
141059 || p->pHaving
141060 ){
141061 return 0;
141062 }
141063 pTab = p->pSrc->a[0].pTab;
141064 assert( pTab!=0 );
@@ -142726,11 +143048,11 @@
143048 */
143049 iEnd = sqlite3VdbeMakeLabel(pParse);
143050 if( (p->selFlags & SF_FixedLimit)==0 ){
143051 p->nSelectRow = 320; /* 4 billion rows */
143052 }
143053 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
143054 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
143055 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
143056 sSort.sortFlags |= SORTFLAG_UseSorter;
143057 }
143058
@@ -142948,12 +143270,17 @@
143270 if( minMaxFlag ){
143271 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
143272 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
143273 }
143274 for(ii=0; ii<pAggInfo->nColumn; ii++){
143275 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
143276 sqlite3DebugPrintf(
143277 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
143278 " iSorterColumn=%d\n",
143279 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
143280 pCol->iTable, pCol->iColumn, pCol->iMem,
143281 pCol->iSorterColumn);
143282 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
143283 }
143284 for(ii=0; ii<pAggInfo->nFunc; ii++){
143285 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
143286 ii, pAggInfo->aFunc[ii].iMem);
@@ -143070,19 +143397,19 @@
143397 }
143398 }
143399 regBase = sqlite3GetTempRange(pParse, nCol);
143400 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
143401 j = nGroupBy;
143402 pAggInfo->directMode = 1;
143403 for(i=0; i<pAggInfo->nColumn; i++){
143404 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143405 if( pCol->iSorterColumn>=j ){
143406 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
 
 
143407 j++;
143408 }
143409 }
143410 pAggInfo->directMode = 0;
143411 regRecord = sqlite3GetTempReg(pParse);
143412 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
143413 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
143414 sqlite3ReleaseTempReg(pParse, regRecord);
143415 sqlite3ReleaseTempRange(pParse, regBase, nCol);
@@ -147496,11 +147823,12 @@
147823 ** in the list are moved to the sqlite3.pDisconnect list of the associated
147824 ** database connection.
147825 */
147826 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
147827 assert( IsVirtual(p) );
147828 assert( db!=0 );
147829 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147830 if( p->u.vtab.azArg ){
147831 int i;
147832 for(i=0; i<p->u.vtab.nArg; i++){
147833 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147834 }
@@ -149173,10 +149501,11 @@
149501 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
149502 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
149503 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
149504 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
149505 #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
149506 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
149507
149508 #endif /* !defined(SQLITE_WHEREINT_H) */
149509
149510 /************** End of whereInt.h ********************************************/
149511 /************** Continuing where we left off in wherecode.c ******************/
@@ -149781,11 +150110,12 @@
150110 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap,&iTab);
150111 pExpr->iTable = iTab;
150112 }
150113 sqlite3ExprDelete(db, pX);
150114 }else{
150115 int n = sqlite3ExprVectorSize(pX->pLeft);
150116 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
150117 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
150118 }
150119 pX = pExpr;
150120 }
150121
@@ -150051,11 +150381,11 @@
150381 WhereTerm *pTerm /* The upper or lower bound just coded */
150382 ){
150383 if( pTerm->wtFlags & TERM_LIKEOPT ){
150384 VdbeOp *pOp;
150385 assert( pLevel->iLikeRepCntr>0 );
150386 pOp = sqlite3VdbeGetLastOp(v);
150387 assert( pOp!=0 );
150388 assert( pOp->opcode==OP_String8
150389 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
150390 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150391 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
@@ -151267,12 +151597,12 @@
151597 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
151598 endEq = 0;
151599 }
151600 nConstraint++;
151601 }
151602 if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff);
151603 if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff);
151604
151605 /* Top of the loop body */
151606 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151607
151608 /* Check if the index cursor is past the end of the range. */
@@ -155499,11 +155829,11 @@
155829 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
155830 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155831 ** is larger than all samples in the array. */
155832 tRowcnt iUpper, iGap;
155833 if( i>=pIdx->nSample ){
155834 iUpper = pIdx->nRowEst0;
155835 }else{
155836 iUpper = aSample[i].anLt[iCol];
155837 }
155838
155839 if( iLower>=iUpper ){
@@ -156128,16 +156458,22 @@
156458 }
156459 }
156460 }
156461
156462 /*
156463 ** Deallocate internal memory used by a WhereLoop object. Leave the
156464 ** object in an initialized state, as if it had been newly allocated.
156465 */
156466 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
156467 if( p->aLTerm!=p->aLTermSpace ){
156468 sqlite3DbFreeNN(db, p->aLTerm);
156469 p->aLTerm = p->aLTermSpace;
156470 p->nLSlot = ArraySize(p->aLTermSpace);
156471 }
156472 whereLoopClearUnion(db, p);
156473 p->nLTerm = 0;
156474 p->wsFlags = 0;
156475 }
156476
156477 /*
156478 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156479 */
@@ -156157,11 +156493,13 @@
156493 /*
156494 ** Transfer content from the second pLoop into the first.
156495 */
156496 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
156497 whereLoopClearUnion(db, pTo);
156498 if( pFrom->nLTerm > pTo->nLSlot
156499 && whereLoopResize(db, pTo, pFrom->nLTerm)
156500 ){
156501 memset(pTo, 0, WHERE_LOOP_XFER_SZ);
156502 return SQLITE_NOMEM_BKPT;
156503 }
156504 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
156505 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
@@ -156175,32 +156513,34 @@
156513
156514 /*
156515 ** Delete a WhereLoop object
156516 */
156517 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
156518 assert( db!=0 );
156519 whereLoopClear(db, p);
156520 sqlite3DbNNFreeNN(db, p);
156521 }
156522
156523 /*
156524 ** Free a WhereInfo structure
156525 */
156526 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
156527 assert( pWInfo!=0 );
156528 assert( db!=0 );
156529 sqlite3WhereClauseClear(&pWInfo->sWC);
156530 while( pWInfo->pLoops ){
156531 WhereLoop *p = pWInfo->pLoops;
156532 pWInfo->pLoops = p->pNextLoop;
156533 whereLoopDelete(db, p);
156534 }
156535 assert( pWInfo->pExprMods==0 );
156536 while( pWInfo->pMemToFree ){
156537 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156538 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
156539 pWInfo->pMemToFree = pNext;
156540 }
156541 sqlite3DbNNFreeNN(db, pWInfo);
156542 }
156543
156544 /* Undo all Expr node modifications
156545 */
156546 static void whereUndoExprMods(WhereInfo *pWInfo){
@@ -156810,11 +157150,15 @@
157150 pNew->wsFlags = saved_wsFlags;
157151 pNew->u.btree.nEq = saved_nEq;
157152 pNew->u.btree.nBtm = saved_nBtm;
157153 pNew->u.btree.nTop = saved_nTop;
157154 pNew->nLTerm = saved_nLTerm;
157155 if( pNew->nLTerm>=pNew->nLSlot
157156 && whereLoopResize(db, pNew, pNew->nLTerm+1)
157157 ){
157158 break; /* OOM while trying to enlarge the pNew->aLTerm array */
157159 }
157160 pNew->aLTerm[pNew->nLTerm++] = pTerm;
157161 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
157162
157163 assert( nInMul==0
157164 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
@@ -156903,42 +157247,43 @@
157247 }
157248 }
157249 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
157250 }else if( eOp & WO_ISNULL ){
157251 pNew->wsFlags |= WHERE_COLUMN_NULL;
157252 }else{
157253 int nVecLen = whereRangeVectorLen(
157254 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
157255 );
157256 if( eOp & (WO_GT|WO_GE) ){
157257 testcase( eOp & WO_GT );
157258 testcase( eOp & WO_GE );
157259 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
157260 pNew->u.btree.nBtm = nVecLen;
157261 pBtm = pTerm;
157262 pTop = 0;
157263 if( pTerm->wtFlags & TERM_LIKEOPT ){
157264 /* Range constraints that come from the LIKE optimization are
157265 ** always used in pairs. */
157266 pTop = &pTerm[1];
157267 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
157268 assert( pTop->wtFlags & TERM_LIKEOPT );
157269 assert( pTop->eOperator==WO_LT );
157270 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
157271 pNew->aLTerm[pNew->nLTerm++] = pTop;
157272 pNew->wsFlags |= WHERE_TOP_LIMIT;
157273 pNew->u.btree.nTop = 1;
157274 }
157275 }else{
157276 assert( eOp & (WO_LT|WO_LE) );
157277 testcase( eOp & WO_LT );
157278 testcase( eOp & WO_LE );
157279 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
157280 pNew->u.btree.nTop = nVecLen;
157281 pTop = pTerm;
157282 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
157283 pNew->aLTerm[pNew->nLTerm-2] : 0;
157284 }
157285 }
157286
157287 /* At this point pNew->nOut is set to the number of rows expected to
157288 ** be visited by the index scan before considering term pTerm, or the
157289 ** values of nIn and nInMul. In other words, assuming that all
@@ -157380,10 +157725,13 @@
157725 #ifdef SQLITE_ENABLE_STAT4
157726 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
157727 #else
157728 pNew->rRun = rSize + 16;
157729 #endif
157730 if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
157731 pNew->wsFlags |= WHERE_VIEWSCAN;
157732 }
157733 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157734 whereLoopOutputAdjust(pWC, pNew, rSize);
157735 rc = whereLoopInsert(pBuilder, pNew);
157736 pNew->nOut = rSize;
157737 if( rc ) break;
@@ -158106,11 +158454,17 @@
158454 WhereLoop *pNew;
158455
158456
158457 /* Loop over the tables in the join, from left to right */
158458 pNew = pBuilder->pNew;
158459
158460 /* Verify that pNew has already been initialized */
158461 assert( pNew->nLTerm==0 );
158462 assert( pNew->wsFlags==0 );
158463 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
158464 assert( pNew->aLTerm!=0 );
158465
158466 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
158467 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158468 Bitmask mUnusable = 0;
158469 pNew->iTab = iTab;
158470 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
@@ -158703,13 +159057,13 @@
159057 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
159058 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
159059 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
159060 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
159061 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
159062 i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */
159063 Bitmask maskNew; /* Mask of src visited by (..) */
159064 Bitmask revMask; /* Mask of rev-order loops for (..) */
159065
159066 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
159067 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
159068 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
159069 /* Do not use an automatic index if the this loop is expected
@@ -158724,11 +159078,13 @@
159078 ** Compute its cost */
159079 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
159080 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
159081 nOut = pFrom->nRow + pWLoop->nOut;
159082 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
159083 isOrdered = pFrom->isOrdered;
159084 if( isOrdered<0 ){
159085 revMask = 0;
159086 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
159087 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
159088 iLoop, pWLoop, &revMask);
159089 }else{
159090 revMask = pFrom->revLoop;
@@ -158751,10 +159107,17 @@
159107 rUnsorted, rCost));
159108 }else{
159109 rCost = rUnsorted;
159110 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
159111 }
159112
159113 /* TUNING: A full-scan of a VIEW or subquery in the outer loop
159114 ** is not so bad. */
159115 if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){
159116 rCost += -10;
159117 nOut += -30;
159118 }
159119
159120 /* Check to see if pWLoop should be added to the set of
159121 ** mxChoice best-so-far paths.
159122 **
159123 ** First look for an existing path among best-so-far paths
@@ -158984,11 +159347,12 @@
159347
159348
159349 pWInfo->nRowOut = pFrom->nRow;
159350
159351 /* Free temporary memory and return success */
159352 assert( db!=0 );
159353 sqlite3DbNNFreeNN(db, pSpace);
159354 return SQLITE_OK;
159355 }
159356
159357 /*
159358 ** Most queries use only a single table (they are not joins) and have
@@ -161217,11 +161581,10 @@
161581 int i;
161582 int nInit = pList ? pList->nExpr : 0;
161583 for(i=0; i<pAppend->nExpr; i++){
161584 sqlite3 *db = pParse->db;
161585 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
 
161586 if( db->mallocFailed ){
161587 sqlite3ExprDelete(db, pDup);
161588 break;
161589 }
161590 if( bIntToNull ){
@@ -162488,14 +162851,13 @@
162851 }
162852 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone);
162853
162854 /* This block runs if reg1 is not NULL, but reg2 is. */
162855 sqlite3VdbeJumpHere(v, addr);
162856 sqlite3VdbeAddOp2(v, OP_IsNull, reg2,
162857 (op==OP_Gt || op==OP_Ge) ? addrDone : lbl);
162858 VdbeCoverage(v);
 
162859 }
162860
162861 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162862 ** This block adds (or subtracts for DESC) the numeric value in regVal
162863 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
@@ -170068,11 +170430,11 @@
170430 sqlite3DeleteTable(db, pParse->pNewTable);
170431 }
170432 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
170433 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170434 }
170435 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
170436 db->pParse = pParentParse;
170437 assert( nErr==0 || pParse->rc!=SQLITE_OK );
170438 return nErr;
170439 }
170440
@@ -171424,22 +171786,23 @@
171786 db->lookaside.pEnd = p;
171787 db->lookaside.bDisable = 0;
171788 db->lookaside.bMalloced = pBuf==0 ?1:0;
171789 db->lookaside.nSlot = nBig+nSm;
171790 }else{
171791 db->lookaside.pStart = 0;
171792 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171793 db->lookaside.pSmallInit = 0;
171794 db->lookaside.pSmallFree = 0;
171795 db->lookaside.pMiddle = 0;
171796 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171797 db->lookaside.pEnd = 0;
171798 db->lookaside.bDisable = 1;
171799 db->lookaside.sz = 0;
171800 db->lookaside.bMalloced = 0;
171801 db->lookaside.nSlot = 0;
171802 }
171803 db->lookaside.pTrueEnd = db->lookaside.pEnd;
171804 assert( sqlite3LookasideUsed(db,0)==0 );
171805 #endif /* SQLITE_OMIT_LOOKASIDE */
171806 return SQLITE_OK;
171807 }
171808
@@ -171514,10 +171877,11 @@
171877 ** Configuration settings for an individual database connection
171878 */
171879 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
171880 va_list ap;
171881 int rc;
171882 sqlite3_mutex_enter(db->mutex);
171883 va_start(ap, op);
171884 switch( op ){
171885 case SQLITE_DBCONFIG_MAINDBNAME: {
171886 /* IMP: R-06824-28531 */
171887 /* IMP: R-36257-52125 */
@@ -171579,10 +171943,11 @@
171943 }
171944 break;
171945 }
171946 }
171947 va_end(ap);
171948 sqlite3_mutex_leave(db->mutex);
171949 return rc;
171950 }
171951
171952 /*
171953 ** This is the default collating function named "BINARY" which is always
@@ -181118,11 +181483,11 @@
181483 p1 = pPhrase->doclist.pList;
181484 p2 = aPoslist;
181485 nDistance = iPrev - nMaxUndeferred;
181486 }
181487
181488 aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING);
181489 if( !aOut ){
181490 sqlite3_free(aPoslist);
181491 return SQLITE_NOMEM;
181492 }
181493
@@ -204144,11 +204509,11 @@
204509 sqlite3_bind_value(pUp, 2, aData[2]);
204510 }
204511 sqlite3_free(p);
204512 nChange = 1;
204513 }
204514 for(jj=1; jj<nData-2; jj++){
204515 nChange++;
204516 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204517 }
204518 if( nChange ){
204519 sqlite3_step(pUp);
@@ -212503,15 +212868,16 @@
212868 */
212869 static int dbpageBegin(sqlite3_vtab *pVtab){
212870 DbpageTable *pTab = (DbpageTable *)pVtab;
212871 sqlite3 *db = pTab->db;
212872 int i;
212873 int rc = SQLITE_OK;
212874 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
212875 Btree *pBt = db->aDb[i].pBt;
212876 if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0);
212877 }
212878 return rc;
212879 }
212880
212881
212882 /*
212883 ** Invoke this routine to register the "dbpage" virtual table module
@@ -219231,11 +219597,11 @@
219597 static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
219598
219599 static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
219600
219601 #define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
219602 #define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c)
219603 #define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
219604 #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
219605 #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
219606
219607 #define fts5BufferGrow(pRc,pBuf,nn) ( \
@@ -231108,11 +231474,13 @@
231474 /* Write the rowid. */
231475 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
231476 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
231477 }else{
231478 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
231479 fts5BufferAppendVarint(&p->rc, &pPage->buf,
231480 (u64)iRowid - (u64)pWriter->iPrevRowid
231481 );
231482 }
231483 pWriter->iPrevRowid = iRowid;
231484 pWriter->bFirstRowidInDoclist = 0;
231485 pWriter->bFirstRowidInPage = 0;
231486 }
@@ -231872,21 +232240,21 @@
232240 return fts5IndexReturn(p);
232241 }
232242
232243 static void fts5AppendRowid(
232244 Fts5Index *p,
232245 u64 iDelta,
232246 Fts5Iter *pUnused,
232247 Fts5Buffer *pBuf
232248 ){
232249 UNUSED_PARAM(pUnused);
232250 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
232251 }
232252
232253 static void fts5AppendPoslist(
232254 Fts5Index *p,
232255 u64 iDelta,
232256 Fts5Iter *pMulti,
232257 Fts5Buffer *pBuf
232258 ){
232259 int nData = pMulti->base.nData;
232260 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
@@ -231957,14 +232325,14 @@
232325 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
232326 *piLastRowid = iRowid;
232327 }
232328 #endif
232329
232330 #define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
232331 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
232332 fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \
232333 (iLastRowid) = (iRowid); \
232334 }
232335
232336 /*
232337 ** Swap the contents of buffer *p1 with that of *p2.
232338 */
@@ -232231,11 +232599,11 @@
232599 Fts5Buffer *aBuf;
232600 int nBuf = 32;
232601 int nMerge = 1;
232602
232603 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
232604 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
232605 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
232606 xMerge = fts5MergeRowidLists;
232607 xAppend = fts5AppendRowid;
232608 }else{
232609 nMerge = FTS5_MERGE_NLIST-1;
@@ -232270,11 +232638,11 @@
232638 fts5MultiIterNext2(p, p1, &dummy)
232639 ){
232640 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
232641 p1->xSetOutputs(p1, pSeg);
232642 if( p1->base.nData ){
232643 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
232644 iLastRowid = p1->base.iRowid;
232645 }
232646 }
232647 fts5MultiIterFree(p1);
232648 }
@@ -232318,11 +232686,11 @@
232686 }
232687 }
232688 iLastRowid = 0;
232689 }
232690
232691 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
232692 iLastRowid = p1->base.iRowid;
232693 }
232694
232695 assert( (nBuf%nMerge)==0 );
232696 for(i=0; i<nBuf; i+=nMerge){
@@ -236634,11 +237002,11 @@
237002 int nArg, /* Number of args */
237003 sqlite3_value **apUnused /* Function arguments */
237004 ){
237005 assert( nArg==0 );
237006 UNUSED_PARAM2(nArg, apUnused);
237007 sqlite3_result_text(pCtx, "fts5: 2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c", -1, SQLITE_TRANSIENT);
237008 }
237009
237010 /*
237011 ** Return true if zName is the extension on one of the shadow tables used
237012 ** by this module.
237013
+16 -8
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144144
**
145145
** See also: [sqlite3_libversion()],
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149
-#define SQLITE_VERSION "3.39.2"
150
-#define SQLITE_VERSION_NUMBER 3039002
151
-#define SQLITE_SOURCE_ID "2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21"
149
+#define SQLITE_VERSION "3.40.0"
150
+#define SQLITE_VERSION_NUMBER 3040000
151
+#define SQLITE_SOURCE_ID "2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -3422,10 +3422,13 @@
34223422
**
34233423
** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
34243424
** <dd>The database is opened [shared cache] enabled, overriding
34253425
** the default shared cache setting provided by
34263426
** [sqlite3_enable_shared_cache()].)^
3427
+** The [use of shared cache mode is discouraged] and hence shared cache
3428
+** capabilities may be omitted from many builds of SQLite. In such cases,
3429
+** this option is a no-op.
34273430
**
34283431
** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
34293432
** <dd>The database is opened [shared cache] disabled, overriding
34303433
** the default shared cache setting provided by
34313434
** [sqlite3_enable_shared_cache()].)^
@@ -3437,11 +3440,11 @@
34373440
** connection as soon as the connection is created. In addition to setting
34383441
** the extended result code mode, this flag also causes [sqlite3_open_v2()]
34393442
** to return an extended result code.</dd>
34403443
**
34413444
** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3442
-** <dd>The database filename is not allowed to be a symbolic link</dd>
3445
+** <dd>The database filename is not allowed to contain a symbolic link</dd>
34433446
** </dl>)^
34443447
**
34453448
** If the 3rd parameter to sqlite3_open_v2() is not one of the
34463449
** required combinations shown above optionally combined with other
34473450
** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6463,11 +6466,11 @@
64636466
**
64646467
** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
64656468
** function C that is invoked prior to each autovacuum of the database
64666469
** file. ^The callback is passed a copy of the generic data pointer (P),
64676470
** the schema-name of the attached database that is being autovacuumed,
6468
-** the the size of the database file in pages, the number of free pages,
6471
+** the size of the database file in pages, the number of free pages,
64696472
** and the number of bytes per page, respectively. The callback should
64706473
** return the number of free pages that should be removed by the
64716474
** autovacuum. ^If the callback returns zero, then no autovacuum happens.
64726475
** ^If the value returned is greater than or equal to the number of
64736476
** free pages, then a complete autovacuum happens.
@@ -6583,10 +6586,15 @@
65836586
**
65846587
** ^(This routine enables or disables the sharing of the database cache
65856588
** and schema data structures between [database connection | connections]
65866589
** to the same database. Sharing is enabled if the argument is true
65876590
** and disabled if the argument is false.)^
6591
+**
6592
+** This interface is omitted if SQLite is compiled with
6593
+** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
6594
+** compile-time option is recommended because the
6595
+** [use of shared cache mode is discouraged].
65886596
**
65896597
** ^Cache sharing is enabled and disabled for an entire process.
65906598
** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
65916599
** In prior versions of SQLite,
65926600
** sharing was enabled or disabled for each thread separately.
@@ -6682,11 +6690,11 @@
66826690
** ^Setting the heap limits to zero disables the heap limiter mechanism.
66836691
**
66846692
** ^The soft heap limit may not be greater than the hard heap limit.
66856693
** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
66866694
** is invoked with a value of N that is greater than the hard heap limit,
6687
-** the the soft heap limit is set to the value of the hard heap limit.
6695
+** the soft heap limit is set to the value of the hard heap limit.
66886696
** ^The soft heap limit is automatically enabled whenever the hard heap
66896697
** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
66906698
** the soft heap limit is outside the range of 1..N, then the soft heap
66916699
** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
66926700
** hard heap limit is enabled makes the soft heap limit equal to the
@@ -8977,11 +8985,11 @@
89778985
** sqlite3_backup_init() is called and before the corresponding call to
89788986
** sqlite3_backup_finish(). SQLite does not currently check to see
89798987
** if the application incorrectly accesses the destination [database connection]
89808988
** and so no error code is reported, but the operations may malfunction
89818989
** nevertheless. Use of the destination database connection while a
8982
-** backup is in progress might also also cause a mutex deadlock.
8990
+** backup is in progress might also cause a mutex deadlock.
89838991
**
89848992
** If running in [shared cache mode], the application must
89858993
** guarantee that the shared cache used by the destination database
89868994
** is not accessed while the backup is running. In practice this means
89878995
** that the application must guarantee that the disk file being
@@ -9405,11 +9413,11 @@
94059413
** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
94069414
** meaning of each of these checkpoint modes.
94079415
*/
94089416
#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
94099417
#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9410
-#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9418
+#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
94119419
#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
94129420
94139421
/*
94149422
** CAPI3REF: Virtual Table Interface Configuration
94159423
**
94169424
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.39.2"
150 #define SQLITE_VERSION_NUMBER 3039002
151 #define SQLITE_SOURCE_ID "2022-07-21 12:26:01 9141e873c575b049ce7aeaf313d05966f1966087caf33a6c80d2416a28571a21"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -3422,10 +3422,13 @@
3422 **
3423 ** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3424 ** <dd>The database is opened [shared cache] enabled, overriding
3425 ** the default shared cache setting provided by
3426 ** [sqlite3_enable_shared_cache()].)^
 
 
 
3427 **
3428 ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3429 ** <dd>The database is opened [shared cache] disabled, overriding
3430 ** the default shared cache setting provided by
3431 ** [sqlite3_enable_shared_cache()].)^
@@ -3437,11 +3440,11 @@
3437 ** connection as soon as the connection is created. In addition to setting
3438 ** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3439 ** to return an extended result code.</dd>
3440 **
3441 ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3442 ** <dd>The database filename is not allowed to be a symbolic link</dd>
3443 ** </dl>)^
3444 **
3445 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
3446 ** required combinations shown above optionally combined with other
3447 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6463,11 +6466,11 @@
6463 **
6464 ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6465 ** function C that is invoked prior to each autovacuum of the database
6466 ** file. ^The callback is passed a copy of the generic data pointer (P),
6467 ** the schema-name of the attached database that is being autovacuumed,
6468 ** the the size of the database file in pages, the number of free pages,
6469 ** and the number of bytes per page, respectively. The callback should
6470 ** return the number of free pages that should be removed by the
6471 ** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6472 ** ^If the value returned is greater than or equal to the number of
6473 ** free pages, then a complete autovacuum happens.
@@ -6583,10 +6586,15 @@
6583 **
6584 ** ^(This routine enables or disables the sharing of the database cache
6585 ** and schema data structures between [database connection | connections]
6586 ** to the same database. Sharing is enabled if the argument is true
6587 ** and disabled if the argument is false.)^
 
 
 
 
 
6588 **
6589 ** ^Cache sharing is enabled and disabled for an entire process.
6590 ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6591 ** In prior versions of SQLite,
6592 ** sharing was enabled or disabled for each thread separately.
@@ -6682,11 +6690,11 @@
6682 ** ^Setting the heap limits to zero disables the heap limiter mechanism.
6683 **
6684 ** ^The soft heap limit may not be greater than the hard heap limit.
6685 ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6686 ** is invoked with a value of N that is greater than the hard heap limit,
6687 ** the the soft heap limit is set to the value of the hard heap limit.
6688 ** ^The soft heap limit is automatically enabled whenever the hard heap
6689 ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6690 ** the soft heap limit is outside the range of 1..N, then the soft heap
6691 ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
6692 ** hard heap limit is enabled makes the soft heap limit equal to the
@@ -8977,11 +8985,11 @@
8977 ** sqlite3_backup_init() is called and before the corresponding call to
8978 ** sqlite3_backup_finish(). SQLite does not currently check to see
8979 ** if the application incorrectly accesses the destination [database connection]
8980 ** and so no error code is reported, but the operations may malfunction
8981 ** nevertheless. Use of the destination database connection while a
8982 ** backup is in progress might also also cause a mutex deadlock.
8983 **
8984 ** If running in [shared cache mode], the application must
8985 ** guarantee that the shared cache used by the destination database
8986 ** is not accessed while the backup is running. In practice this means
8987 ** that the application must guarantee that the disk file being
@@ -9405,11 +9413,11 @@
9405 ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9406 ** meaning of each of these checkpoint modes.
9407 */
9408 #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9409 #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9410 #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9411 #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9412
9413 /*
9414 ** CAPI3REF: Virtual Table Interface Configuration
9415 **
9416
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.40.0"
150 #define SQLITE_VERSION_NUMBER 3040000
151 #define SQLITE_SOURCE_ID "2022-09-02 21:19:24 da7af290960ab8a04a1f55cdc5eeac36b47fa194edf67f0a05daa4b7f2a4071c"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -3422,10 +3422,13 @@
3422 **
3423 ** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3424 ** <dd>The database is opened [shared cache] enabled, overriding
3425 ** the default shared cache setting provided by
3426 ** [sqlite3_enable_shared_cache()].)^
3427 ** The [use of shared cache mode is discouraged] and hence shared cache
3428 ** capabilities may be omitted from many builds of SQLite. In such cases,
3429 ** this option is a no-op.
3430 **
3431 ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3432 ** <dd>The database is opened [shared cache] disabled, overriding
3433 ** the default shared cache setting provided by
3434 ** [sqlite3_enable_shared_cache()].)^
@@ -3437,11 +3440,11 @@
3440 ** connection as soon as the connection is created. In addition to setting
3441 ** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3442 ** to return an extended result code.</dd>
3443 **
3444 ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3445 ** <dd>The database filename is not allowed to contain a symbolic link</dd>
3446 ** </dl>)^
3447 **
3448 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
3449 ** required combinations shown above optionally combined with other
3450 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
@@ -6463,11 +6466,11 @@
6466 **
6467 ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6468 ** function C that is invoked prior to each autovacuum of the database
6469 ** file. ^The callback is passed a copy of the generic data pointer (P),
6470 ** the schema-name of the attached database that is being autovacuumed,
6471 ** the size of the database file in pages, the number of free pages,
6472 ** and the number of bytes per page, respectively. The callback should
6473 ** return the number of free pages that should be removed by the
6474 ** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6475 ** ^If the value returned is greater than or equal to the number of
6476 ** free pages, then a complete autovacuum happens.
@@ -6583,10 +6586,15 @@
6586 **
6587 ** ^(This routine enables or disables the sharing of the database cache
6588 ** and schema data structures between [database connection | connections]
6589 ** to the same database. Sharing is enabled if the argument is true
6590 ** and disabled if the argument is false.)^
6591 **
6592 ** This interface is omitted if SQLite is compiled with
6593 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
6594 ** compile-time option is recommended because the
6595 ** [use of shared cache mode is discouraged].
6596 **
6597 ** ^Cache sharing is enabled and disabled for an entire process.
6598 ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6599 ** In prior versions of SQLite,
6600 ** sharing was enabled or disabled for each thread separately.
@@ -6682,11 +6690,11 @@
6690 ** ^Setting the heap limits to zero disables the heap limiter mechanism.
6691 **
6692 ** ^The soft heap limit may not be greater than the hard heap limit.
6693 ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6694 ** is invoked with a value of N that is greater than the hard heap limit,
6695 ** the soft heap limit is set to the value of the hard heap limit.
6696 ** ^The soft heap limit is automatically enabled whenever the hard heap
6697 ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6698 ** the soft heap limit is outside the range of 1..N, then the soft heap
6699 ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
6700 ** hard heap limit is enabled makes the soft heap limit equal to the
@@ -8977,11 +8985,11 @@
8985 ** sqlite3_backup_init() is called and before the corresponding call to
8986 ** sqlite3_backup_finish(). SQLite does not currently check to see
8987 ** if the application incorrectly accesses the destination [database connection]
8988 ** and so no error code is reported, but the operations may malfunction
8989 ** nevertheless. Use of the destination database connection while a
8990 ** backup is in progress might also cause a mutex deadlock.
8991 **
8992 ** If running in [shared cache mode], the application must
8993 ** guarantee that the shared cache used by the destination database
8994 ** is not accessed while the backup is running. In practice this means
8995 ** that the application must guarantee that the disk file being
@@ -9405,11 +9413,11 @@
9413 ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9414 ** meaning of each of these checkpoint modes.
9415 */
9416 #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9417 #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9418 #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
9419 #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9420
9421 /*
9422 ** CAPI3REF: Virtual Table Interface Configuration
9423 **
9424

Keyboard Shortcuts

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